|
| 1 | +// altimate_change - new file |
| 2 | +// |
| 3 | +// Model-facing statement of Altimate Workspace identity: which workspace (if any) this |
| 4 | +// project is linked to, rendered UNCONDITIONALLY so "this/current/active workspace" has |
| 5 | +// exactly one deterministic answer every turn. Independent of `awareness.ts`, which |
| 6 | +// exists to steer warehouse tool-call routing and is silent by design whenever no |
| 7 | +// integration is served — correct for routing, wrong for identity. A user can ask |
| 8 | +// "which workspace is my project connected to" on a session with zero served |
| 9 | +// connections and still deserves a real answer. |
| 10 | +// |
| 11 | +// The naming conflict: "workspace" names two unrelated things in this product — the |
| 12 | +// Altimate Workspace a project is *linked* to (`state.ts`'s binding), and other |
| 13 | +// services' own use of the word (Databricks' native "workspace" concept, in |
| 14 | +// particular). Nothing previously told the model which one "workspace" means when the |
| 15 | +// user says it bare, so it could answer with whichever "workspace" happened to be |
| 16 | +// nearby in context — confidently, and wrong. This module states the linked workspace |
| 17 | +// (or its absence, or that it's currently unverifiable) explicitly, and instructs the |
| 18 | +// model to resolve a genuine workspace-identity QUESTION to it — never to substitute |
| 19 | +// another service's "workspace" as the answer. That instruction is deliberately scoped |
| 20 | +// to an actual identity question (see `TRIGGER` below), not to every incidental mention |
| 21 | +// of the word: an earlier draft fired on any appearance of "workspace" at all, which |
| 22 | +// meant nagging about linking mid-conversation about an unrelated Databricks topic, or |
| 23 | +// pedantically re-qualifying every casual mention of one. Neither is this feature's |
| 24 | +// job — resolving "this/current/active workspace" is. |
| 25 | +import { resolveBindingOutcome, type BindingOutcome } from "./state" |
| 26 | +import { inertWorkspaceName } from "./workspace-name" |
| 27 | +import { isEnabled } from "./engine-seams" |
| 28 | +import { Instance } from "../../project/instance" |
| 29 | + |
| 30 | +/** Independent of `awareness.ts`'s MAX_SECTION_CHARS (2,000) — this section is a short, |
| 31 | + * fixed-shape identity statement, not an open-ended list of served integrations, so a |
| 32 | + * much smaller ceiling is enough. Exists mainly as a guard against a pathological |
| 33 | + * workspace name defeating `inertWorkspaceName`'s own 80-code-point cap. */ |
| 34 | +export const MAX_SECTION_CHARS = 800 |
| 35 | + |
| 36 | +const HEADING = "## Altimate Workspace" |
| 37 | + |
| 38 | +/** The trigger for every active instruction below: a genuine workspace-IDENTITY |
| 39 | + * question from the user, not any incidental appearance of the word. Scoped this way |
| 40 | + * on purpose — an earlier draft said "whenever 'workspace' comes up" for the unbound |
| 41 | + * case, and an unconditional disambiguation rule for the bound case, and both were |
| 42 | + * over-triggering: a model that takes either literally interjects a linking pitch, or |
| 43 | + * pedantically re-qualifies every mention of a Databricks workspace, in the middle of |
| 44 | + * a conversation that was never about the Altimate Workspace at all. Restated once |
| 45 | + * here so both branches phrase the same condition identically. */ |
| 46 | +const TRIGGER = |
| 47 | + 'the user\'s own message asks a workspace-IDENTITY question — "workspace" ' + |
| 48 | + 'unqualified, or "this"/"current"/"active" workspace, used to ask what THIS ' + |
| 49 | + "project is connected to (not a passing mention of some other service's workspace)" |
| 50 | + |
| 51 | +const LINK_HINT = |
| 52 | + 'To link one: in this session, open the command palette and run "Link this project ' + |
| 53 | + "to a workspace\"; or run `altimate-code link` in a terminal." |
| 54 | + |
| 55 | +/** Pure formatter — takes an already-resolved outcome so it is testable without |
| 56 | + * mocking the binding cache / network. `systemSection` below is the thin async |
| 57 | + * wrapper that actually resolves one. Enforces `MAX_SECTION_CHARS` itself (rather than |
| 58 | + * leaving it to the caller) so the cap is part of the pure, testable surface — the |
| 59 | + * guard is against a pathological workspace name, and every branch below is built from |
| 60 | + * one, so it belongs where the name is rendered. */ |
| 61 | +export function render(outcome: BindingOutcome): string { |
| 62 | + return capSection(renderBody(outcome)) |
| 63 | +} |
| 64 | + |
| 65 | +/** Exported so the cap's own contract has direct coverage — `inertWorkspaceName` |
| 66 | + * already bounds the one variable input (the workspace name) to 80 code points, so no |
| 67 | + * real `render()` call can currently produce output long enough to exercise this via |
| 68 | + * `render()` alone. It stays as defense in depth against a future branch that adds |
| 69 | + * unbounded text. */ |
| 70 | +export function capSection(out: string): string { |
| 71 | + return out.length > MAX_SECTION_CHARS ? out.slice(0, MAX_SECTION_CHARS) : out |
| 72 | +} |
| 73 | + |
| 74 | +function renderBody(outcome: BindingOutcome): string { |
| 75 | + if (outcome.status === "bound") { |
| 76 | + const label = workspaceLabel(outcome.binding.datamateName, String(outcome.binding.datamateId)) |
| 77 | + return [ |
| 78 | + HEADING, |
| 79 | + "", |
| 80 | + `This project is linked to Altimate Workspace ${label}.`, |
| 81 | + `When ${TRIGGER}, the answer is this Altimate Workspace — never substitute ` + |
| 82 | + "another service's own \"workspace\" (a Databricks workspace, an IDE's " + |
| 83 | + "workspace folder, etc.) for it. Outside such a question, other services' own " + |
| 84 | + '"workspace" concepts can be discussed normally — there is no need to relabel ' + |
| 85 | + "or footnote every incidental mention of one.", |
| 86 | + ].join("\n") |
| 87 | + } |
| 88 | + |
| 89 | + if (outcome.status === "unbound") { |
| 90 | + // No Altimate Workspace is linked, so there is nothing to protect the bare word |
| 91 | + // "workspace" for in casual conversation — another service's own "workspace" can |
| 92 | + // come up normally. The active instruction is scoped to TRIGGER (a real identity |
| 93 | + // question), not "any mention of the word" — the earlier draft's "whenever |
| 94 | + // 'workspace' comes up" phrasing was exactly the over-triggering this fixes. |
| 95 | + return [ |
| 96 | + HEADING, |
| 97 | + "", |
| 98 | + "No Altimate Workspace is linked to this project.", |
| 99 | + `When ${TRIGGER}, say plainly that none is linked yet and offer to help link ` + |
| 100 | + "one.", |
| 101 | + LINK_HINT, |
| 102 | + "Outside such a question, other services' own \"workspace\" concepts (e.g. a " + |
| 103 | + "Databricks workspace) are unrelated — discuss them normally, with no linking " + |
| 104 | + "pitch attached.", |
| 105 | + ].join("\n") |
| 106 | + } |
| 107 | + |
| 108 | + // "unknown" — the local cache and the server disagree, or neither is reachable this |
| 109 | + // turn. Assert nothing about the Altimate Workspace: not a specific one, and not |
| 110 | + // "unlinked" either — both would be a guess the next revalidation could contradict. |
| 111 | + // Other services' own "workspace" concepts are unaffected by this uncertainty. |
| 112 | + return [ |
| 113 | + HEADING, |
| 114 | + "", |
| 115 | + "Whether this project is linked to an Altimate Workspace could not be verified " + |
| 116 | + "this turn.", |
| 117 | + `When ${TRIGGER}, say link status is temporarily unavailable and to try again ` + |
| 118 | + "shortly. Do not name a specific Altimate Workspace and do not say none is " + |
| 119 | + "linked.", |
| 120 | + "Outside such a question, other services' own \"workspace\" concepts (e.g. a " + |
| 121 | + "Databricks workspace) are unaffected and can be discussed normally.", |
| 122 | + ].join("\n") |
| 123 | +} |
| 124 | + |
| 125 | +function workspaceLabel(name: string, id: string): string { |
| 126 | + // A name that sanitises to nothing must not erase the identity: the id is the |
| 127 | + // stable half, and `""` reads as a bug. |
| 128 | + return `${JSON.stringify(inertWorkspaceName(name) || "(unnamed)")} (id ${id})` |
| 129 | +} |
| 130 | + |
| 131 | +/** Called on every step of the agentic loop, same as `awareness.ts`'s section — |
| 132 | + * `resolveBindingOutcome` is a cached local read (5-minute revalidation window), so |
| 133 | + * this stays cheap. Reads `Instance.directory` ITSELF, inside the same try/catch as |
| 134 | + * the resolve call — not as a caller-supplied argument evaluated at the call site. |
| 135 | + * `Instance.directory` is an `AsyncLocalStorage`-backed getter (`project/instance.ts`) |
| 136 | + * that throws `Context.NotFound` outside an established instance context (some test |
| 137 | + * harnesses, or an edge case in a future call path); evaluating it as an argument to |
| 138 | + * this function — `systemSection(Instance.directory)` — would throw synchronously at |
| 139 | + * the CALLER, before this function's own try/catch ever runs, defeating it entirely. |
| 140 | + * Mirrors how `precedence.ts`'s `currentBinding()` reads `Instance.directory` inside |
| 141 | + * its own try/catch for the same reason. Any failure — a missing instance context, a |
| 142 | + * binding-cache read error — degrades to the "unknown" copy rather than breaking |
| 143 | + * prompt assembly. */ |
| 144 | +export async function systemSection(): Promise<string> { |
| 145 | + // Behind the same opt-in as everything else about workspaces. A user outside |
| 146 | + // the pilot has no Altimate Workspace to be linked to, and must not be told |
| 147 | + // every turn that none is linked and how to link one. |
| 148 | + if (!isEnabled()) return "" |
| 149 | + try { |
| 150 | + const outcome = await resolveBindingOutcome(Instance.directory) |
| 151 | + return render(outcome) |
| 152 | + } catch { |
| 153 | + return render({ status: "unknown" }) |
| 154 | + } |
| 155 | +} |
| 156 | +// altimate_change end |
0 commit comments