Skip to content

Commit 2480439

Browse files
fix(workspace): detach rejected engines, and report the gap on reuse
Codex round 2 on ec21cc0. Three findings, all verified against the branch. **A rejected engine is now detached at the moment of rejection.** Teardown ran just before the replacement spawn, so every exit that failed to produce a replacement — `engine-missing`, and both `engine-too-old` returns — came back with the rejected engine still connected. The turn's `resolveTools` then handed the model exactly the tools the attribution check had just decided it must not have: an unpinned engine serving whichever workspace its owner had active, or one below the floor whose `--datamate` pin the engine does not lock. Worse than the pre-attribution behaviour, because the client had explicitly judged it untrustworthy and served it anyway. `detachRejected` now runs at each rejection site, so "we will not use this engine" and "this engine is no longer serving this session" are the same event. It stays runtime-only (`MCP.remove`): the config file is never touched. **Reuse reports declared-versus-delivered, like the fresh attach.** A running engine that lost an integration — a deleted connection, a restart that dropped one — serves fewer tools than the workspace declares, and only the fresh-attach path said so. Reuse is the common path, so silence there is exactly where the gap goes unnoticed; it was visible in this branch's own testing, where a workspace declaring 52 keys delivered 12 and then 7 across a connection change. `reused` now carries `declared` and `missing` and warns when the gap is non-empty. An unreadable allowlist degrades quietly rather than inventing a gap. **An unbound project no longer keeps a stale managed entry.** MCP bootstrap starts every enabled config entry before the prompt (`src/mcp/index.ts:762-781`), while `run()` returned for an unbound project without touching it — so unlinking a project left the previously pinned workspace's tools still attached. Only an entry matching the exact command we persist is torn down; an IDE-written or hand-edited entry is the user's and is left alone, and the config is not modified either way. The related half of that finding — a project attached under the pilot flag keeps its tools when the flag is later off — is NOT fixed here. Acting on it means doing MCP work while the gate is closed, which is the opposite of what the gate is for, and deciding whether a pilot flag should retroactively disable an entry the user now has in their project config is a product call. Recorded in the PR body under "Held for Ralph" instead of guessed at. 9 further tests: detach on each irreplaceable-rejection exit, the reuse gap and its toast, an unreadable allowlist, and unbound detaching our managed entry while leaving an IDE entry alone.
1 parent ec21cc0 commit 2480439

2 files changed

Lines changed: 197 additions & 32 deletions

File tree

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

Lines changed: 85 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ const TOOL_PREFIX = `${DATAMATE_KEY}_`
9292
export type Outcome =
9393
| { kind: "disabled" }
9494
| { kind: "unbound" }
95-
| { kind: "reused"; available: number }
95+
| { kind: "reused"; available: number; declared?: number; missing?: string[] }
9696
| { kind: "attached"; available: number; declared: number; missing: string[]; replaced?: string }
9797
| { kind: "engine-missing"; declared: number }
9898
| { kind: "engine-too-old"; found: string }
@@ -312,6 +312,14 @@ export function pinnedWorkspace(entry: ExistingEntry | null): string | null {
312312
return null
313313
}
314314

315+
/** Did WE write this entry? Only an entry matching the exact command we persist
316+
* is ours to tear down; an IDE-written or hand-edited entry belongs to the user
317+
* and is left alone even when it is the wrong one for this project. */
318+
function isManagedEntry(entry: ExistingEntry | null): boolean {
319+
const argv = commandArgv(entry)
320+
return argv.length === 4 && argv[0] === ENGINE_BINARY && argv[1] === "start-stdio" && argv[2] === PIN_FLAG && !!argv[3]
321+
}
322+
315323
/** Short, printable identity of an entry, for saying what was replaced. */
316324
function describeEntry(entry: ExistingEntry | null): string {
317325
if (isUrlEntry(entry)) return entry.url
@@ -366,10 +374,28 @@ function describeMissing(missing: string[]): string {
366374
async function run(): Promise<Outcome> {
367375
if (!isEnabled()) return { kind: "disabled" }
368376

377+
const client = mcp()
378+
369379
const binding = await resolveBinding()
370-
if (!binding) return { kind: "unbound" }
380+
if (!binding) {
381+
// An entry WE persisted for a binding that no longer exists is still started
382+
// by MCP bootstrap on every launch, and would serve the OLD workspace's tools
383+
// in a project that is no longer linked to it. Detach it — runtime only, the
384+
// config entry is left in place — so an unlinked project does not silently
385+
// keep another workspace's tools. Only our own managed entry qualifies.
386+
const present = (await client.status())[DATAMATE_KEY]
387+
if (present) {
388+
const stale = await existingEntry(DATAMATE_KEY)
389+
if (isManagedEntry(stale)) {
390+
log.info("detaching a managed engine entry in an unbound project", { entry: describeEntry(stale) })
391+
await client.remove(DATAMATE_KEY).catch((err) => {
392+
log.warn("could not detach the managed engine entry", { err: String(err) })
393+
})
394+
}
395+
}
396+
return { kind: "unbound" }
397+
}
371398
const workspaceId = String(binding.datamateId)
372-
const client = mcp()
373399

374400
// Rule 1 — reuse what already serves this session, but only if it can be shown
375401
// to serve THIS workspace, on an engine that still clears the floor.
@@ -385,11 +411,25 @@ async function run(): Promise<Outcome> {
385411
// locked, which is the drift this attribution is meant to exclude.
386412
let replaced: string | undefined
387413
let replacedNote = ""
388-
/** Was the entry we are replacing still RUNNING? Then it must be closed before
389-
* we spawn, or `MCP.add` leaves its child process orphaned beside the new one —
390-
* the same duplicate-engine hazard this module refuses for a failing entry, and
391-
* in practice it wedges the session. */
392-
let replacedLive = false
414+
415+
/** Stop serving an entry we have judged untrustworthy for this workspace.
416+
*
417+
* Runtime-only (`MCP.remove`): closes the client and drops it from the tool
418+
* catalogue without touching any config file — `MCP.disconnect` would persist
419+
* `enabled: false` into whichever config owns the entry, which for a global
420+
* one disables the user's engine everywhere.
421+
*
422+
* This must run at the moment of REJECTION, not merely before a replacement
423+
* spawn. Every exit that fails to produce a replacement — `engine-missing`,
424+
* `engine-too-old` — would otherwise return with the rejected engine still
425+
* connected, and the turn's `resolveTools` would hand the model exactly the
426+
* tools we just decided it must not have. It also closes the client `MCP.add`
427+
* would otherwise overwrite without closing, which orphans a second engine. */
428+
const detachRejected = async (why: Record<string, unknown>): Promise<void> => {
429+
await client.remove(DATAMATE_KEY).catch((err) => {
430+
log.warn("could not detach the rejected engine entry", { err: String(err), ...why })
431+
})
432+
}
393433
const before = await client.status()
394434
const existing = before[DATAMATE_KEY]
395435
if (existing) {
@@ -436,22 +476,49 @@ async function run(): Promise<Outcome> {
436476
// connected URL entry lands here too, which is the point — the hosted
437477
// endpoint serves a different tool set, and rule 4 forbids adopting it.
438478
replaced = describeEntry(entry)
439-
replacedLive = true
440479
replacedNote = pin
441480
? ` Replaced an engine entry pinned to workspace ${pin} for this session.`
442481
: ` Replaced an engine entry that is not pinned to this workspace (${replaced}) for this session; it serves whichever workspace its owner has active.`
443-
log.info("existing engine entry is not attributable to this workspace; will spawn locally", {
482+
log.info("existing engine entry is not attributable to this workspace; detaching", {
444483
workspaceId,
445484
pinnedTo: pin,
446485
entry: replaced,
447486
})
487+
await detachRejected({ workspaceId, reason: "not-attributable", pinnedTo: pin })
448488
} else {
449489
const entryBin = commandArgv(entry)[0]
450490
const found = entryBin ? await versionOf(entryBin) : null
451491
if (found && compareVersions(found, MIN_ENGINE_VERSION) >= 0) {
452-
const available = engineToolKeys(await client.tools()).size
453-
log.info("reusing existing engine entry", { workspaceId, available, version: found })
454-
return { kind: "reused", available }
492+
// Rule 5 applies to a reused engine too. A running engine that lost an
493+
// integration — a connection deleted, a restart that dropped it —
494+
// serves fewer tools than the workspace declares, and only the fresh
495+
// attach used to say so. Reuse is the COMMON path, so staying silent
496+
// here is where the gap would actually go unnoticed.
497+
const present = engineToolKeys(await client.tools())
498+
const declaredKeys = await declared(workspaceId)
499+
const missing = declaredKeys ? declaredKeys.keys.filter((k) => !present.has(k)) : []
500+
const available = present.size
501+
if (declaredKeys && missing.length > 0) {
502+
await notify({
503+
title: `Workspace "${binding.datamateName}" is missing declared tools`,
504+
message:
505+
`The running engine serves ${available} of ${declaredKeys.keys.length} declared integration tools.` +
506+
describeMissing(missing),
507+
variant: "warning",
508+
})
509+
}
510+
log.info("reusing existing engine entry", {
511+
workspaceId,
512+
available,
513+
version: found,
514+
declared: declaredKeys?.keys.length,
515+
missing,
516+
})
517+
return {
518+
kind: "reused",
519+
available,
520+
...(declaredKeys ? { declared: declaredKeys.keys.length, missing } : {}),
521+
}
455522
}
456523
// Pinned to us, but below the floor or unreadable. Prefer a newer engine
457524
// on PATH over keeping one whose pin the engine does not lock; if PATH
@@ -460,6 +527,9 @@ async function run(): Promise<Outcome> {
460527
const pathVersion = onPath ? await versionOf(onPath) : null
461528
if (!pathVersion || compareVersions(pathVersion, MIN_ENGINE_VERSION) < 0) {
462529
const label = found ?? "unknown"
530+
// Rejected and irreplaceable: detach anyway. Leaving it connected would
531+
// return "too old" while still serving the too-old engine's tools.
532+
await detachRejected({ workspaceId, reason: "below-floor", found: label })
463533
await notify({
464534
title: "Workspace engine is too old",
465535
message:
@@ -470,13 +540,13 @@ async function run(): Promise<Outcome> {
470540
return { kind: "engine-too-old", found: label }
471541
}
472542
replaced = describeEntry(entry)
473-
replacedLive = true
474543
replacedNote = ` Replaced an engine entry running ${found ?? "an unreadable version"}, below the ${MIN_ENGINE_VERSION} floor, for this session.`
475-
log.info("existing engine entry is below the version floor; will spawn locally", {
544+
log.info("existing engine entry is below the version floor; detaching", {
476545
workspaceId,
477546
found,
478547
pathVersion,
479548
})
549+
await detachRejected({ workspaceId, reason: "below-floor-replaceable", found })
480550
}
481551
}
482552
}
@@ -515,21 +585,6 @@ async function run(): Promise<Outcome> {
515585
command: [ENGINE_BINARY, "start-stdio", "--datamate", workspaceId],
516586
enabled: true,
517587
}
518-
if (replacedLive) {
519-
// Close the live registration first: `MCP.add` does not close the client it
520-
// overwrites, so adding over a running stdio server starts a second engine
521-
// and abandons the first with its pipes still open.
522-
//
523-
// `remove`, NOT `disconnect`. `MCP.disconnect` persists `enabled: false` to
524-
// whichever config file actually holds the entry — which for an IDE-written
525-
// or user-global `datamate` is the GLOBAL config. Our replacement is written
526-
// project-locally, so disconnecting would leave the user's engine disabled in
527-
// every OTHER project. `remove` is runtime-only teardown: it closes the
528-
// client, drops it from state, and publishes ToolsChanged, touching no file.
529-
await client.remove(DATAMATE_KEY).catch((err) => {
530-
log.warn("could not close the engine entry being replaced", { err: String(err) })
531-
})
532-
}
533588
await persist(DATAMATE_KEY, cfg)
534589
await client.add(DATAMATE_KEY, cfg)
535590

‎packages/opencode/test/altimate/workspace/engine-sync.test.ts‎

Lines changed: 112 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ describe("ensure", () => {
140140
statuses: [{ datamate: { status: "connected" } }],
141141
tools: { datamate_dbt_build_model: 1, datamate_dbt_compile_model: 1 },
142142
})
143-
expect(await ensure("s1")).toEqual({ kind: "reused", available: 2 })
143+
expect(await ensure("s1")).toEqual({ kind: "reused", available: 2, declared: 2, missing: [] })
144144
expect(h.added).toHaveLength(0)
145145
expect(h.persisted).toHaveLength(0)
146146
})
@@ -400,7 +400,7 @@ describe("ensure — attribution of a CONNECTED entry", () => {
400400
statuses: [{ datamate: { status: "connected" } }],
401401
tools: twoTools,
402402
})
403-
expect(await ensure("s1")).toEqual({ kind: "reused", available: 2 })
403+
expect(await ensure("s1")).toEqual({ kind: "reused", available: 2, declared: 2, missing: [] })
404404
expect(h.added).toHaveLength(0)
405405
expect(h.persisted).toHaveLength(0)
406406
expect(h.removes).toHaveLength(0) // reuse must never tear down what it reuses
@@ -561,3 +561,113 @@ describe("ensure — the memo follows the BINDING, not just the session id", ()
561561
expect(h).toBeDefined()
562562
})
563563
})
564+
565+
describe("ensure — a REJECTED engine is detached even when it cannot be replaced", () => {
566+
const liveUnpinned = { type: "local", command: ["datamate", "start-stdio"] }
567+
568+
test("no engine on PATH: still detaches, so resolveTools cannot serve the wrong workspace", async () => {
569+
const h = install({
570+
existing: liveUnpinned,
571+
statuses: [{ datamate: { status: "connected" } }],
572+
which: null,
573+
tools: { datamate_dbt_build_model: 1 },
574+
})
575+
expect(await ensure("s1")).toEqual({ kind: "engine-missing", declared: 2 })
576+
// The whole point: we judged it untrustworthy, so it must not still be serving.
577+
expect(h.removes).toEqual(["datamate"])
578+
expect(h.added).toHaveLength(0)
579+
})
580+
581+
test("PATH engine below the floor: still detaches before reporting too-old", async () => {
582+
const h = install({
583+
existing: liveUnpinned,
584+
statuses: [{ datamate: { status: "connected" } }],
585+
version: "0.5.9",
586+
tools: { datamate_dbt_build_model: 1 },
587+
})
588+
expect(await ensure("s1")).toEqual({ kind: "engine-too-old", found: "0.5.9" })
589+
expect(h.removes).toEqual(["datamate"])
590+
expect(h.added).toHaveLength(0)
591+
})
592+
593+
test("a pinned-but-below-floor engine with nothing better is detached, not left serving", async () => {
594+
const h = install({
595+
existing: { type: "local", command: ["datamate", "start-stdio", "--datamate", "42"] },
596+
statuses: [{ datamate: { status: "connected" } }],
597+
version: () => "0.6.3",
598+
tools: { datamate_dbt_build_model: 1 },
599+
})
600+
expect(await ensure("s1")).toEqual({ kind: "engine-too-old", found: "0.6.3" })
601+
expect(h.removes).toEqual(["datamate"])
602+
})
603+
})
604+
605+
describe("ensure — reuse reports the declared-vs-delivered gap (rule 5)", () => {
606+
test("a reused engine missing a declared tool warns, and the outcome carries the gap", async () => {
607+
const h = install({
608+
existing: { type: "local", command: ["datamate", "start-stdio", "--datamate", "42"] },
609+
statuses: [{ datamate: { status: "connected" } }],
610+
tools: { datamate_dbt_build_model: 1 }, // declared has two keys
611+
})
612+
expect(await ensure("s1")).toEqual({
613+
kind: "reused",
614+
available: 1,
615+
declared: 2,
616+
missing: ["dbt_compile_model"],
617+
})
618+
expect(h.toasts).toHaveLength(1)
619+
expect(h.toasts[0].variant).toBe("warning")
620+
expect(h.toasts[0].message).toContain("1 of 2 declared integration tools")
621+
expect(h.toasts[0].message).toContain("dbt_compile_model")
622+
})
623+
624+
test("no gap means no toast", async () => {
625+
const h = install({
626+
existing: { type: "local", command: ["datamate", "start-stdio", "--datamate", "42"] },
627+
statuses: [{ datamate: { status: "connected" } }],
628+
tools: { datamate_dbt_build_model: 1, datamate_dbt_compile_model: 1 },
629+
})
630+
expect(await ensure("s1")).toMatchObject({ kind: "reused", missing: [] })
631+
expect(h.toasts).toHaveLength(0)
632+
})
633+
634+
test("an unreadable allowlist degrades quietly rather than inventing a gap", async () => {
635+
const h = install({
636+
existing: { type: "local", command: ["datamate", "start-stdio", "--datamate", "42"] },
637+
statuses: [{ datamate: { status: "connected" } }],
638+
declared: null,
639+
tools: { datamate_dbt_build_model: 1 },
640+
})
641+
expect(await ensure("s1")).toEqual({ kind: "reused", available: 1 })
642+
expect(h.toasts).toHaveLength(0)
643+
})
644+
})
645+
646+
describe("ensure — an unbound project does not keep a stale MANAGED entry", () => {
647+
test("our own pinned entry is detached when the binding is gone", async () => {
648+
const h = install({
649+
binding: null,
650+
existing: { type: "local", command: ["datamate", "start-stdio", "--datamate", "5"] },
651+
statuses: [{ datamate: { status: "connected" } }],
652+
})
653+
expect(await ensure("s1")).toEqual({ kind: "unbound" })
654+
expect(h.removes).toEqual(["datamate"])
655+
})
656+
657+
test("an IDE-written entry is LEFT ALONE — it is the user's, not ours", async () => {
658+
const h = install({
659+
binding: null,
660+
existing: { command: "datamate", args: ["start-stdio"] },
661+
statuses: [{ datamate: { status: "connected" } }],
662+
})
663+
expect(await ensure("s1")).toEqual({ kind: "unbound" })
664+
expect(h.removes).toHaveLength(0)
665+
})
666+
667+
test("nothing registered means nothing to detach", async () => {
668+
const h = install({ binding: null, statuses: [{}] })
669+
expect(await ensure("s1")).toEqual({ kind: "unbound" })
670+
expect(h.removes).toHaveLength(0)
671+
expect(h.toasts).toHaveLength(0)
672+
})
673+
})

0 commit comments

Comments
 (0)