Skip to content

Commit 686b2e5

Browse files
feat(workspace): tell the model about extension tools a live IDE bridge serves
The `## Workspace integrations` section projected only the three warehouse capabilities per connection type, so extension-type tools the engine serves through a live VS Code bridge (the dbt project tools, sql-tools) never reached the model even when they sat in its catalog. The presentation surfaces learned about them in #1236; this is the model-facing half. - `declared()` groups the extension keys under their catalog integration (`Declared.extensions`, optional so the flat lists stay the contract), and the attach outcome carries the groups so precedence never makes a second lookup. - `derive` names an extension tool only when two signals agree: its key materialised in the live catalog AND a bridge for this project is live now. Either missing is silence, not a warning. The tools ride on the `nothing-materialised` snapshot too, since a workspace can serve extension tools and no warehouse capability at all. - `servedExtensions` applies the same reachability filter as the warehouse inventory, at projection time, so an `analyst` is never told about tools it may not call. - The section appends the groups under a fixed intro naming the condition they depend on; a workspace with extension tools and nothing routed gets an extension-only shape that keeps the local-tools sentence. Extension lines are dropped before any warehouse type when the cap is hit. Every snapshot without extension tools renders byte-for-byte what it did before, including all silent disabled states. Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_01GHBUvb843k1R7UAGi8Ya9b
1 parent 89ddf85 commit 686b2e5

9 files changed

Lines changed: 443 additions & 31 deletions

File tree

‎packages/opencode/src/altimate/workspace/awareness.ts‎

Lines changed: 100 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,25 @@
3030
// still be in the catalog while routing refuses them, and silence would leave the
3131
// model free to call what it can see. `DISABLED_COPY` below is the decision table.
3232
//
33+
// Extension-type tools (served through a live VS Code bridge) are the section's other
34+
// list. They shadow nothing, so they are awareness only; they are named only when
35+
// precedence has them as really served (in the catalog AND behind a live bridge —
36+
// see `extensionsServed` in precedence.ts), and a dormant bridge is silence, not a
37+
// warning. One consequence for the table: `nothing-materialised` speaks when it
38+
// carries extension tools — a workspace can serve those and no warehouse capability
39+
// at all — and stays silent otherwise, which keeps the byte-identical claim intact.
40+
//
3341
// SERVER-SIDE ONLY, for the same reason `precedence.ts` is: the TUI plugin runtime
3442
// loads plugins in a separate module realm, so an import from there would read a
3543
// different, always-empty `Precedence` map. Import this only from the session layer.
36-
import { type Capability, type Precedence, inertWorkspaceName, servedInventory } from "./precedence"
44+
import {
45+
type Capability,
46+
type Precedence,
47+
type ServedExtension,
48+
inertWorkspaceName,
49+
servedExtensions,
50+
servedInventory,
51+
} from "./precedence"
3752

3853
/** Hard ceiling on the rendered section. Deliberately independent of
3954
* `UNIFIED_INJECTION_BUDGET`: this is a routing directive, not knowledge, and must
@@ -130,10 +145,25 @@ const DISABLED_COPY: Record<NonNullable<Precedence["disabledReason"]>, string> =
130145
*/
131146
export function systemSection(precedence: Precedence | undefined): string {
132147
if (!precedence) return ""
133-
if (!precedence.enabled) return precedence.disabledReason ? DISABLED_COPY[precedence.disabledReason] : ""
148+
const extLines = servedExtensions(precedence).map(extensionLine)
149+
if (!precedence.enabled) {
150+
// The one disabled state that can carry served extension tools (see `derive`):
151+
// no warehouse capability is routed, but the bridge is serving, and silence
152+
// would leave the model unaware of tools it can see. Without them the table's
153+
// entry renders exactly as before.
154+
if (precedence.disabledReason === "nothing-materialised" && extLines.length > 0) {
155+
return assembleExtensionsOnly(precedence.workspaceName, precedence.workspaceId, extLines)
156+
}
157+
return precedence.disabledReason ? DISABLED_COPY[precedence.disabledReason] : ""
158+
}
134159

135160
const served = servedInventory(precedence)
136-
if (served.length === 0) return ""
161+
// Enabled but no warehouse capability reachable (the analyst shape). The same
162+
// ruleset filters the extension tools, so normally none survive either; any that
163+
// do are still real and still callable, so they are said.
164+
if (served.length === 0) {
165+
return extLines.length > 0 ? assembleExtensionsOnly(precedence.workspaceName, precedence.workspaceId, extLines) : ""
166+
}
137167

138168
// `type` is the canonical local driver type (`postgres`), not the user-facing
139169
// connection name nor the engine's integration id (`postgresql`) — it is what the
@@ -147,7 +177,53 @@ export function systemSection(precedence: Precedence | undefined): string {
147177
return `- ${type} — ${servedPart}${localPart}`
148178
})
149179

150-
return assemble(precedence.workspaceName, precedence.workspaceId, typeLines)
180+
return assemble(precedence.workspaceName, precedence.workspaceId, typeLines, extLines)
181+
}
182+
183+
/** One extension-type integration and every tool of it the caller can call. The
184+
* integration name is catalog-authored and precedence already made it inert; the
185+
* keys are engine tool names, quoted the way the warehouse lines quote theirs. */
186+
function extensionLine(group: ServedExtension): string {
187+
return `- ${group.integration} — ${group.tools.map((t) => `\`${t.modelKey}\``).join(", ")}`
188+
}
189+
190+
/** Above the extension lines in both shapes of the section. It names the condition
191+
* the tools depend on, so a failure after the window closes can be explained
192+
* rather than retried blindly. */
193+
const EXTENSION_INTRO =
194+
"The VS Code window open on this project serves these extension tools through the workspace. Call them " +
195+
"like any other tool; they are unavailable while that window is closed:"
196+
197+
const extensionOmission = (n: number) =>
198+
`- …and ${n} further extension integration${n === 1 ? "" : "s"} served through the connected VS Code window.`
199+
200+
/** The section when extension tools are served and no warehouse capability is
201+
* routed. The local-tools sentence is kept: with nothing shadowed, every
202+
* connection really does stay local, and the model should not infer otherwise
203+
* from seeing `datamate_*` keys listed. Same cap, same drop rule as `assemble`. */
204+
function assembleExtensionsOnly(workspaceName: string, workspaceId: string | undefined, extLines: string[]): string {
205+
const label = workspaceLabel(workspaceName, workspaceId)
206+
const render = (ext: string[]) => {
207+
const omitted = extLines.length - ext.length
208+
return [
209+
HEADING,
210+
"",
211+
`This project is bound to Altimate workspace ${label}. No warehouse capability is routed through it in ` +
212+
`this session: every connection uses the local tools (${ALL_LOCAL_TOOLS}).`,
213+
"",
214+
EXTENSION_INTRO,
215+
"",
216+
...ext,
217+
...(omitted > 0 ? [extensionOmission(omitted)] : []),
218+
].join("\n")
219+
}
220+
let ext = extLines
221+
let out = render(ext)
222+
while (out.length > MAX_SECTION_CHARS && ext.length > 0) {
223+
ext = ext.slice(0, -1)
224+
out = render(ext)
225+
}
226+
return out
151227
}
152228

153229
/** The workspace name is customer-authored and lands in the system prompt — the
@@ -171,10 +247,16 @@ function workspaceLabel(name: string, id: string | undefined): string {
171247
* be partial instead, and the prohibition is kept only for types the workspace does
172248
* not serve. The count is stated once, on the list where it belongs; the converse
173249
* carries only what the model should DO about the omission. */
174-
function assemble(workspaceName: string, workspaceId: string | undefined, typeLines: string[]): string {
250+
function assemble(
251+
workspaceName: string,
252+
workspaceId: string | undefined,
253+
typeLines: string[],
254+
extLines: string[] = [],
255+
): string {
175256
const label = workspaceLabel(workspaceName, workspaceId)
176-
const render = (lines: string[]) => {
257+
const render = (lines: string[], ext: string[]) => {
177258
const omitted = typeLines.length - lines.length
259+
const extOmitted = extLines.length - ext.length
178260
const converse =
179261
omitted > 0
180262
? "For the served types omitted above, prefer the `datamate_*` tool for that type when one is in the " +
@@ -193,16 +275,24 @@ function assemble(workspaceName: string, workspaceId: string | undefined, typeLi
193275
...(omitted > 0
194276
? [`- …and ${omitted} further connection type${omitted === 1 ? "" : "s"} served by this workspace.`]
195277
: []),
278+
...(extLines.length > 0
279+
? ["", EXTENSION_INTRO, "", ...ext, ...(extOmitted > 0 ? [extensionOmission(extOmitted)] : [])]
280+
: []),
196281
"",
197282
converse,
198283
].join("\n")
199284
}
200285

201286
let lines = typeLines
202-
let out = render(lines)
203-
while (out.length > MAX_SECTION_CHARS && lines.length > 0) {
204-
lines = lines.slice(0, -1)
205-
out = render(lines)
287+
let ext = extLines
288+
let out = render(lines, ext)
289+
// Extension lines are dropped first: they are awareness, while the type lines
290+
// are directives the guard will enforce, and a redirect the model was never
291+
// warned of is the worse failure. Type lines go only once none are left.
292+
while (out.length > MAX_SECTION_CHARS && (ext.length > 0 || lines.length > 0)) {
293+
if (ext.length > 0) ext = ext.slice(0, -1)
294+
else lines = lines.slice(0, -1)
295+
out = render(lines, ext)
206296
}
207297
return out
208298
}

‎packages/opencode/src/altimate/workspace/engine-overlay.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,7 @@ async function reconcile(sessionID: string, directory: string, state: DirectoryS
659659
kind: "attached",
660660
available: present.size,
661661
...(declared ? { declared: declared.keys.length, missing } : {}),
662+
...(declared?.extensions?.length ? { extensions: declared.extensions } : {}),
662663
}
663664
const rec = record(sessionID, outcome)
664665
// Keyed on the workspace too: a re-link with an identical inventory is still

‎packages/opencode/src/altimate/workspace/engine-probes.ts‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { EventV2Bridge } from "@/event-v2-bridge"
1313
import { TuiEvent } from "@/server/tui-event"
1414
import { readLocalBindingScopedStrict } from "./state"
1515
import { log, syncInternals, type BindingRead, type ScopedBinding } from "./engine-seams"
16-
import type { Declared, Toast } from "./engine-types"
16+
import type { Declared, DeclaredExtension, Toast } from "./engine-types"
1717

1818
/** How long the allowlist lookup may hold a turn. Once per workspace per process. */
1919
export const DECLARED_TIMEOUT_MS = 4_000
@@ -129,14 +129,23 @@ export async function declared(workspaceId: string): Promise<Declared | null> {
129129
AltimateApi.getDatamate(workspaceId),
130130
AltimateApi.listIntegrations(),
131131
])
132-
const extensionIds = new Set(catalog.filter((i) => i.type === "extension").map((i) => i.id))
132+
const extensionNames = new Map(
133+
catalog.filter((i) => i.type === "extension").map((i): [string, string] => [i.id, i.name ?? i.id]),
134+
)
133135
const keys: string[] = []
134136
const extensionKeys: string[] = []
137+
const extensions: DeclaredExtension[] = []
135138
for (const integration of workspace.integrations ?? []) {
136-
const target = extensionIds.has(integration.id) ? extensionKeys : keys
137-
for (const tool of integration.tools ?? []) target.push(tool.key)
139+
const toolKeys = (integration.tools ?? []).map((tool) => tool.key)
140+
const name = extensionNames.get(integration.id)
141+
if (name === undefined) {
142+
keys.push(...toolKeys)
143+
continue
144+
}
145+
extensionKeys.push(...toolKeys)
146+
if (toolKeys.length > 0) extensions.push({ id: integration.id, name, keys: toolKeys })
138147
}
139-
return { keys, extensionKeys }
148+
return { keys, extensionKeys, ...(extensions.length > 0 ? { extensions } : {}) }
140149
} catch (err) {
141150
log.warn("could not read the declared workspace integrations", { workspaceId, err: String(err) })
142151
return null

‎packages/opencode/src/altimate/workspace/engine-types.ts‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,16 @@ export const TOOL_PREFIX = `${DATAMATE_KEY}_`
2929
export type Outcome =
3030
| { kind: "disabled" }
3131
| { kind: "unbound" }
32-
| { kind: "attached"; available: number; declared?: number; missing?: string[] }
32+
| {
33+
kind: "attached"
34+
available: number
35+
declared?: number
36+
missing?: string[]
37+
/** The allowlist's extension-type integrations, when it names any: what a
38+
* live IDE bridge could serve. Whether they are present is decided per turn
39+
* against the catalog, never recorded here. */
40+
extensions?: DeclaredExtension[]
41+
}
3342
| { kind: "engine-missing"; declared?: number }
3443
/** `found` is null when the binary ran but printed nothing usable — broken
3544
* rather than old; the message says so. */
@@ -57,7 +66,16 @@ export type McpStatus = Record<string, { status: string; error?: string } | unde
5766
/** Declared allowlist for a workspace, split by whether the CLI can serve it.
5867
* Extension-type integrations are RPC into a live VS Code host and have no
5968
* meaning on the CLI surface, so they are excluded from the reported gap. */
60-
export type Declared = { keys: string[]; extensionKeys: string[] }
69+
export type Declared = {
70+
keys: string[]
71+
extensionKeys: string[]
72+
/** The extension keys again, grouped under their catalog integration, for the
73+
* surfaces that name them rather than count them. Optional: the flat lists are
74+
* the contract every existing reader was written against. */
75+
extensions?: DeclaredExtension[]
76+
}
77+
78+
export type DeclaredExtension = { id: string; name: string; keys: string[] }
6179

6280
/** A configured MCP entry in either shape it can reach us: opencode's own
6381
* `command: string[]` argv, or the `{ command, args }` split an IDE writes. */

‎packages/opencode/src/altimate/workspace/precedence.ts‎

Lines changed: 99 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ import {
6767
type Outcome,
6868
} from "./engine-overlay"
6969
import { readLocalBindingScopedStrict } from "./state"
70+
import { liveBridge } from "./engine-probes"
71+
import { syncInternals } from "./engine-seams"
7072
import { canonicalType } from "../native/connections/registry"
7173
import * as Registry from "../native/connections/registry"
7274

@@ -148,6 +150,19 @@ export interface Precedence {
148150
* engine keys, so redirecting its permitted reads would take away the one thing
149151
* it exists to do. Absent means "unknown", which is treated as reachable. */
150152
ruleset?: PermissionNext.Ruleset
153+
/** Extension-type tools the engine is serving through a live IDE bridge, grouped
154+
* by integration. Nothing here is shadowed — there is no native counterpart to
155+
* redirect — so this is awareness only, and it is present on a
156+
* `nothing-materialised` snapshot too: a workspace can serve extension tools and
157+
* no warehouse capability at all. Absent or empty when no bridge is live. */
158+
extensions?: ServedExtension[]
159+
}
160+
161+
/** One extension-type integration and the tools of it that materialised. */
162+
export interface ServedExtension {
163+
/** The catalog's display name, made inert the same way the workspace name is. */
164+
integration: string
165+
tools: { engineTool: string; modelKey: string }[]
151166
}
152167

153168
/** The workspace name as model-visible text: control characters stripped (C0, DEL and
@@ -242,14 +257,52 @@ function remember(sessionID: string, value: Precedence): void {
242257
* a notice. Being wrong in that direction costs a turn's routing, which the next turn
243258
* repairs; being wrong the other way routes credentials into someone else's engine.
244259
*/
245-
async function attested(sessionID: string): Promise<boolean> {
246-
const outcome = precedenceInternals.attachOutcome
260+
async function attachOutcome(sessionID: string): Promise<Outcome | undefined> {
261+
return precedenceInternals.attachOutcome
247262
? await precedenceInternals.attachOutcome().catch(() => undefined)
248263
: settledOutcome(sessionID)
249-
if (!outcome) return false
250-
// The attach module owns the allowlist; a new outcome kind refuses until it is
251-
// named there (see SERVING in engine-types).
252-
return attributableEngine(outcome)
264+
}
265+
266+
/** The project directory this process serves, or null outside an instance. Read
267+
* defensively for the same reason `currentBinding` is: the accessor throws when
268+
* there is no instance, and a bridge probe must never cost the turn its tools. */
269+
function projectDirectory(): string | null {
270+
try {
271+
return Instance.directory || null
272+
} catch {
273+
return null
274+
}
275+
}
276+
277+
/**
278+
* Extension-type tools the session really has, grouped by integration. Two signals
279+
* must agree, in the same spirit as attribution: the key is in the live catalog
280+
* (the engine held a bridge when it spawned and is serving the tool now) AND a
281+
* bridge for this project is live at this turn (the window it needs is still
282+
* open). The catalog alone would go on advertising tools whose window has since
283+
* closed — the engine discovers the bridge at spawn and does not re-list; the
284+
* bridge alone says nothing about what materialised. Either missing renders
285+
* nothing, which is the silence the awareness section wants for a dormant bridge.
286+
*/
287+
function extensionsServed(outcome: Outcome, present: Set<string>): ServedExtension[] {
288+
if (outcome.kind !== "attached" || !outcome.extensions?.length) return []
289+
const groups: ServedExtension[] = []
290+
for (const ext of outcome.extensions) {
291+
const tools = ext.keys
292+
.filter((key) => present.has(key))
293+
.map((engineTool) => ({ engineTool, modelKey: `${DATAMATE_KEY}_${engineTool}` }))
294+
if (tools.length > 0) groups.push({ integration: inertWorkspaceName(ext.name), tools })
295+
}
296+
if (groups.length === 0) return []
297+
const cwd = projectDirectory()
298+
// No directory and no seam: nothing to match a sidecar against, so no claim.
299+
if (cwd === null && !syncInternals.liveBridge) return []
300+
try {
301+
if (!liveBridge(cwd ?? "")) return []
302+
} catch {
303+
return []
304+
}
305+
return groups
253306
}
254307

255308
/** Sessions whose inventory line has already been reported. Precedence is re-derived
@@ -534,7 +587,10 @@ async function derive(sessionID: string, tools: Record<string, unknown>): Promis
534587
// one we established; the configured pin says it still names this workspace. Config
535588
// alone is not enough — it can be rewritten under a live connection — and the
536589
// outcome alone would not notice a later rewrite pointing somewhere else.
537-
if (!(await attested(sessionID))) {
590+
// The attach module owns the allowlist; a new outcome kind refuses until it is
591+
// named there (see SERVING in engine-types).
592+
const outcome = await attachOutcome(sessionID)
593+
if (!outcome || !attributableEngine(outcome)) {
538594
log.info("no attach established this session's engine; precedence off", { bound: binding.datamateId })
539595
return EMPTY("unattributed", workspaceName)
540596
}
@@ -552,6 +608,7 @@ async function derive(sessionID: string, tools: Record<string, unknown>): Promis
552608
warnForeign(sessionID, tools)
553609
if (present.size === 0) return EMPTY("nothing-materialised", workspaceName)
554610
warnUnrecognised(sessionID, present)
611+
const extensions = extensionsServed(outcome, present)
555612

556613
// Mechanism 2 — capability by capability, only where the key is really there.
557614
const shadowed = new Map<string, Map<Capability, ShadowEntry>>()
@@ -571,8 +628,24 @@ async function derive(sessionID: string, tools: Record<string, unknown>): Promis
571628
})
572629
}
573630
}
574-
if (shadowed.size === 0) return EMPTY("nothing-materialised", workspaceName)
575-
return { workspaceName, workspaceId: String(binding.datamateId), enabled: true, shadowed }
631+
// Extension tools ride on the disabled snapshot too: they are served without any
632+
// warehouse capability being routed, and the model should hear about them either way.
633+
if (shadowed.size === 0) {
634+
// With extension tools aboard the snapshot also names the bound id, as the
635+
// enabled shape does: the section labels the workspace by it. Without them the
636+
// shape is exactly `EMPTY`'s, as it always was.
637+
return {
638+
...EMPTY("nothing-materialised", workspaceName),
639+
...(extensions.length ? { workspaceId: String(binding.datamateId), extensions } : {}),
640+
}
641+
}
642+
return {
643+
workspaceName,
644+
workspaceId: String(binding.datamateId),
645+
enabled: true,
646+
shadowed,
647+
...(extensions.length ? { extensions } : {}),
648+
}
576649
}
577650

578651
/** Read the session's precedence without recomputing it. */
@@ -684,6 +757,23 @@ export function servedInventory(precedence: Precedence): ServedType[] {
684757
}
685758
return out
686759
}
760+
761+
/**
762+
* The extension-type tools this caller can really call, grouped by integration —
763+
* the awareness section's other list. Same reachability filter as `servedInventory`,
764+
* applied at projection time because the ruleset is attached after derivation; a
765+
* group none of whose tools the caller may call is dropped rather than advertised.
766+
* Deliberately NOT gated on `enabled`: a `nothing-materialised` snapshot carries
767+
* these too (see `derive`).
768+
*/
769+
export function servedExtensions(precedence: Precedence): ServedExtension[] {
770+
const out: ServedExtension[] = []
771+
for (const group of precedence.extensions ?? []) {
772+
const tools = group.tools.filter((t) => reachable(precedence, t.modelKey))
773+
if (tools.length > 0) out.push({ integration: group.integration, tools })
774+
}
775+
return out
776+
}
687777
// altimate_change end
688778

689779
function unreachable(workspaceName: string, modelKey: string): Verdict {

0 commit comments

Comments
 (0)