Skip to content

Commit ce5331d

Browse files
fix(workspace): tear down a disabled engine, and close the seam the restore fix opened
Three defects, each proven by individual revert. A disabled entry stopped at the config. `MCP.status()` reports live client state and `MCP.tools()` gates on exactly that status — it reads the config only for a timeout — so an entry disabled AFTER it connected kept exporting its tools and credentials to `resolveTools`. The branch already documented this in a comment and still returned without touching the runtime. It now detaches through the existing rejection path, which is runtime-only and writes no config: respecting the edit, not re-applying it. The same check was unreachable from the memoised-success path. Validation covered connectivity, pin and version but never `enabled`, so a session that had already attached rode its memo past a disable for the rest of its life. The check goes in ahead of the command-unchanged shortcut, and returns false rather than detaching directly — routing the session back through `run()`, where the reporting and the teardown already live. The third was introduced by the previous commit: snapshotting the project entry for a restore put an awaited disk read between the final binding check and the install it guards. The late guard would undo the stale attach, but only after spawning an engine and taking the per-project lock — long enough for the replacement's first-turn wait to expire, which is the failure that guard exists to prevent. The snapshot moves above the check, so nothing awaits between the check and the mutations. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01Y9X4vNjkwQ3vTiJ8w1kKR6
1 parent 37dd23d commit ce5331d

2 files changed

Lines changed: 96 additions & 3 deletions

File tree

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

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,15 @@ async function run(): Promise<Outcome> {
663663
// bound project would silently re-enable it for every other project.
664664
// Say what is unavailable and leave their choice alone.
665665
log.info("engine entry is explicitly disabled; leaving it alone", { workspaceId })
666+
// Leaving the CONFIG alone is the whole point; leaving the RUNTIME alone is
667+
// not. `MCP.status()` reports live client state, and `MCP.tools()` gates on
668+
// exactly that status without consulting `enabled` — so an entry disabled
669+
// after it connected keeps exporting its tools to `resolveTools`, and the
670+
// user's "off" is honoured on disk while the model still holds the
671+
// workspace's tools and credentials. `remove` is runtime-only: it closes
672+
// the client and publishes ToolsChanged without writing config, which is
673+
// precisely "respect the edit", not "re-apply" it.
674+
await detachRejected({ reason: "the entry is disabled" })
666675
await notify({
667676
title: "Workspace engine is disabled",
668677
message:
@@ -852,15 +861,23 @@ async function run(): Promise<Outcome> {
852861
command: [ENGINE_BINARY, "start-stdio", "--datamate", workspaceId],
853862
enabled: true,
854863
}
864+
// Snapshot what persist() is about to overwrite — the project file's own
865+
// entry, not the merged view — so a supersede can put back exactly that.
866+
//
867+
// Read BEFORE the guard rather than between it and the writes. Every await
868+
// after the last check reopens the window that check exists to close, and a
869+
// disk read is a wide one. The post-install guard would undo the stale attach,
870+
// but only after it had spawned an engine and held the per-project lock — long
871+
// enough for the replacement's first-turn wait to expire, which is the failure
872+
// the guard was added to prevent. Nothing may await between the guard and the
873+
// mutations it guards.
874+
const projectBefore = await projectEntry()
855875
if (!(await stillCurrent())) {
856876
// Re-linked while we were probing. Installing now would attach the workspace
857877
// this session has already left, and would win by arriving first.
858878
log.info("abandoning attach; the binding changed before the engine was installed", { workspaceId })
859879
return { kind: "superseded" }
860880
}
861-
// Snapshot what persist() is about to overwrite — the project file's own
862-
// entry, not the merged view — so a supersede can put back exactly that.
863-
const projectBefore = await projectEntry()
864881
await persist(DATAMATE_KEY, cfg)
865882
await client.add(DATAMATE_KEY, cfg)
866883

@@ -979,6 +996,16 @@ async function engineStillOurs(workspaceId: string, record?: SessionAttach): Pro
979996
// instance-wide client is serving B — so the cached success would expose B's
980997
// tools under binding A. The pin is what makes it ours.
981998
const entry = await existingEntry(DATAMATE_KEY)
999+
// Intent outranks every other check, and it is checked FIRST because the
1000+
// command-unchanged shortcut below returns early: a session that already
1001+
// attached would otherwise ride its memo straight past the disable for the
1002+
// rest of its life, never re-entering `run()` where the check lives.
1003+
// Returning false here does not itself detach — it routes this session back
1004+
// through `run()`, which reports `entry-disabled` and tears the client down.
1005+
if (entry?.enabled === false) {
1006+
log.info("engine entry was disabled since the cached attach; re-deciding", { workspaceId })
1007+
return false
1008+
}
9821009
if (pinnedWorkspace(entry) !== workspaceId) return false
9831010

9841011
// The pin is not the whole contract: the FLOOR is what makes the pin

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

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1523,3 +1523,69 @@ describe("ensure — round 18", () => {
15231523
expect(h.restores, "restored something into the project file instead of removing the override").toEqual([null])
15241524
})
15251525
})
1526+
1527+
describe("INVARIANT — a disabled entry serves nothing", () => {
1528+
// "Disabled" is a claim about what the model can reach, not about what the
1529+
// config file says. The config is where the user expresses it; the runtime is
1530+
// where it either holds or doesn't.
1531+
test("an entry disabled AFTER it connected is torn down, not merely reported", async () => {
1532+
const h = install({
1533+
existing: { type: "local", command: ["datamate", "start-stdio", "--datamate", "42"], enabled: false },
1534+
// The status a live disable actually produces. `MCP.status()` returns live
1535+
// client state and `MCP.tools()` gates on exactly that, consulting the
1536+
// config only for a timeout — so reporting `entry-disabled` while the
1537+
// client stays registered hands that turn the tools and the credentials
1538+
// of the workspace the user just switched off.
1539+
statuses: [{ datamate: { status: "connected" } }],
1540+
})
1541+
expect(await ensure("s1")).toEqual({ kind: "entry-disabled" })
1542+
expect(h.removes, "reported the entry disabled but left its client serving tools").toContain("datamate")
1543+
// Respecting the edit must not turn into rewriting it.
1544+
expect(h.persisted, "wrote to the config while honouring a disable").toHaveLength(0)
1545+
expect(h.connects, "retried an entry the user disabled").toHaveLength(0)
1546+
})
1547+
1548+
test("a memoised success does not outlive the entry being disabled", async () => {
1549+
// The disable check lives in `run()`, and a settled success never re-enters
1550+
// it. Every later turn of that session is decided by the memo alone, so the
1551+
// check has to be reachable from the validation path too.
1552+
let enabled = true
1553+
const h = install({
1554+
statuses: [
1555+
{},
1556+
{ datamate: { status: "connected" } },
1557+
{ datamate: { status: "connected" } },
1558+
{ datamate: { status: "connected" } },
1559+
],
1560+
tools: { datamate_dbt_build_model: 1, datamate_dbt_compile_model: 1 },
1561+
})
1562+
syncInternals.existingEntry = async () =>
1563+
({ type: "local", command: ["datamate", "start-stdio", "--datamate", "42"], enabled }) as ExistingEntry
1564+
expect(await ensure("s1")).toMatchObject({ kind: "attached" })
1565+
1566+
enabled = false
1567+
expect(await ensure("s1"), "rode the memo straight past the user's disable").toEqual({ kind: "entry-disabled" })
1568+
expect(h.removes, "kept serving the disabled entry's tools for the rest of the session").toContain("datamate")
1569+
})
1570+
1571+
test("nothing awaits between the final binding check and the install", async () => {
1572+
// The guard is only worth what the gap after it is: any await between the
1573+
// check and the mutations reopens the window the check exists to close. The
1574+
// late guard would undo this attach — but only after it had spawned an
1575+
// engine and taken the per-project lock, which is long enough for the
1576+
// replacement attach's first-turn wait to expire.
1577+
let current: CachedBinding | null = binding
1578+
const h = install({
1579+
statuses: [{}, { datamate: { status: "connected" } }],
1580+
tools: { datamate_dbt_build_model: 1 },
1581+
})
1582+
syncInternals.resolveBinding = async () => current
1583+
syncInternals.projectEntry = async () => {
1584+
current = { ...binding, datamateId: 99, datamateName: "other" } as CachedBinding
1585+
return null
1586+
}
1587+
expect(await ensure("s1")).toEqual({ kind: "superseded" })
1588+
expect(h.added, "installed an engine for a workspace the project had already left").toHaveLength(0)
1589+
expect(h.persisted, "pinned a workspace the project had already left").toHaveLength(0)
1590+
})
1591+
})

0 commit comments

Comments
 (0)