Skip to content

Commit 5de0acf

Browse files
fix(workspace): an answer names an engine that is there, and is true when it is given
Two holes in the questions every named answer asks. A client that vanished is not a client that was replaced. `confirmServing` compared the runtime's launch record against the engine that was judged, but treated NO record as agreement — so a client removed or disconnected during the tool listing or the allowlist lookup was still answered `attached` or `reused`. The record is the only witness to "still serving"; its absence is now the verdict `gone`, handled at both callers like a replacement: the install is undone, the reuse answer is `superseded`, and nothing is detached because nothing is there. A harness that does not model the record is not asked. The attached answer was fixed before the success announcements and given after them, and the announcements are three awaits. A re-link, a disable or a replacement landing inside them left this turn holding `attached` for an engine that no longer served the bound workspace — the one residual the install region still named. Every await after a guard belongs to the guard: the world is asked once more after the last announcement, and the install is undone and answered `superseded` if it moved. The toast was true when it was shown; the answer is true when it is given; no second toast. The test that asserted the residual now asserts its closure; a read-count fixture gains the fourth intent read of the first attach. Removing the `gone` verdict fails two named tests; removing the post-announcement check fails three.
1 parent 37cabe5 commit 5de0acf

4 files changed

Lines changed: 105 additions & 21 deletions

File tree

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

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -680,11 +680,19 @@ async function run(sessionID: string): Promise<Outcome> {
680680
* what it judged), and it must run BEFORE the refusal is announced. */
681681
const confirmServing = async (
682682
judged: ExistingEntry | null,
683-
): Promise<"ok" | "disabled" | "unreadable" | "moved" | "replaced"> => {
683+
): Promise<"ok" | "disabled" | "unreadable" | "moved" | "replaced" | "gone"> => {
684684
const world = await worldUnchanged()
685685
if (world !== "ok") return world === "replaced" ? "moved" : world
686-
const servingNow = client.spawned ? await client.spawned(DATAMATE_KEY).catch(() => undefined) : undefined
687-
if (servingNow && judged && !sameEntry(servingNow, judged)) return "replaced"
686+
// The runtime's own record of what it launched is the only witness to
687+
// "still serving". A record that names a different launch is a replacement;
688+
// NO record means the client was removed or disconnected while we waited,
689+
// and an answer naming an engine that is not there is as wrong as one naming
690+
// the wrong engine. A harness that does not model the record is not asked.
691+
if (client.spawned) {
692+
const servingNow = await client.spawned(DATAMATE_KEY).catch(() => undefined)
693+
if (!servingNow) return "gone"
694+
if (judged && !sameEntry(servingNow, judged)) return "replaced"
695+
}
688696
return "ok"
689697
}
690698

@@ -1095,11 +1103,13 @@ async function run(sessionID: string): Promise<Outcome> {
10951103
// The tool and allowlist reads above are two awaits; `confirmServing`
10961104
// asks the two questions every named answer asks after them.
10971105
const verdict = await confirmServing(runningEngine(inspection))
1098-
if (verdict === "replaced") {
1099-
// The replacement is someone else's; it is not ours to detach, and the
1100-
// next decision judges it on its own merits.
1101-
log.info("the engine we judged was replaced during the reuse lookup; not answering for it", {
1106+
if (verdict === "replaced" || verdict === "gone") {
1107+
// A replacement is someone else's and not ours to detach; a client that
1108+
// is gone has nothing to detach. Either way the next decision judges
1109+
// what is there on its own merits.
1110+
log.info("the engine we judged is no longer the one serving; not answering for it", {
11021111
workspaceId,
1112+
verdict,
11031113
})
11041114
return { kind: "superseded" }
11051115
}
@@ -1457,6 +1467,22 @@ async function run(sessionID: string): Promise<Outcome> {
14571467
})
14581468
}
14591469
await noteHostedNeighbours(outcome)
1470+
// Three more awaits sit between fixing the answer and giving it, and every
1471+
// await after a guard belongs to the guard's problem: a re-link, a disable
1472+
// or a replacement landing inside the announcements would otherwise leave
1473+
// this turn holding `attached` for an engine that no longer serves the
1474+
// bound workspace. The toast was true when it was shown; the answer must be
1475+
// true when it is given. Superseded is repairable, so the next turn
1476+
// re-decides for whatever is bound then.
1477+
const afterAnnouncing = await confirmServing(cfg)
1478+
if (afterAnnouncing !== "ok") {
1479+
log.info("the world changed while the attach was being announced; undoing rather than answering for it", {
1480+
workspaceId,
1481+
why: afterAnnouncing,
1482+
})
1483+
await undoNow()
1484+
return { kind: "superseded" }
1485+
}
14601486
return outcome
14611487
} catch (err) {
14621488
// Undo first, then decide how to report. A throw that lands after a re-link

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1314,6 +1314,62 @@ describe("an answer is revalidated before it is given", () => {
13141314
expect(h.persisted, "wrote config while honouring a disable").toHaveLength(0)
13151315
expect(h.toasts.map((t) => t.title)).toEqual(["Workspace engine is disabled"])
13161316
})
1317+
1318+
test("a client that vanished during the reuse lookup is not answered as serving", async () => {
1319+
// Someone disconnects or removes the entry while the tool listing is in
1320+
// flight. There is nothing to detach and nothing serving; answering
1321+
// `reused` would name an engine that is not there.
1322+
const h = install({
1323+
existing: { type: "local", command: ["datamate", "start-stdio", "--datamate", "42"] },
1324+
statuses: [{ datamate: { status: "connected" } }],
1325+
tools: { datamate_dbt_build_model: 1 },
1326+
})
1327+
const prevTools = syncInternals.mcp!.tools!
1328+
syncInternals.mcp!.tools = async () => {
1329+
h.spawnedNow = undefined
1330+
return prevTools()
1331+
}
1332+
expect(await ensure("s1")).toEqual({ kind: "superseded" })
1333+
expect(h.added).toHaveLength(0)
1334+
expect(h.removes).toHaveLength(0)
1335+
})
1336+
1337+
test("a client that vanished during the post-install awaits is not reported as attached", async () => {
1338+
const h = install({
1339+
statuses: [{}, { datamate: { status: "connected" } }, { datamate: { status: "connected" } }],
1340+
tools: { datamate_dbt_build_model: 1 },
1341+
})
1342+
const prevTools = syncInternals.mcp!.tools!
1343+
syncInternals.mcp!.tools = async () => {
1344+
h.spawnedNow = undefined
1345+
return prevTools()
1346+
}
1347+
expect((await ensure("s1")).kind, "reported an engine that is no longer there").toBe("superseded")
1348+
expect(h.restores, "left our pin on disk for a client that is gone").toHaveLength(1)
1349+
})
1350+
1351+
test("a re-link during the success announcements is not answered with the old workspace", async () => {
1352+
// The answer was fixed before the announcements, but it is GIVEN after
1353+
// them, and they are awaits. The toast was true when shown; the answer must
1354+
// be true when returned — so the world is asked once more after the last
1355+
// announcement, and the install is undone if it moved. No second toast.
1356+
let current: CachedBinding | null = binding // 42
1357+
const h = install({
1358+
statuses: [{}, { datamate: { status: "connected" } }, { datamate: { status: "connected" } }],
1359+
tools: { datamate_dbt_build_model: 1 },
1360+
})
1361+
syncInternals.resolveBinding = async () => current
1362+
const prevNotify = syncInternals.notify!
1363+
syncInternals.notify = async (toast) => {
1364+
await prevNotify(toast)
1365+
if (toast.title.endsWith("connected")) current = { ...binding, datamateId: 99, datamateName: "other" } as CachedBinding
1366+
}
1367+
expect((await ensure("s1")).kind).toBe("superseded")
1368+
expect(h.removes, "left the old workspace's engine serving under the new binding").toEqual(["datamate"])
1369+
expect(h.restores).toHaveLength(1)
1370+
expect(h.toasts.filter((t) => t.title.endsWith("connected"))).toHaveLength(1)
1371+
expect(h.toasts.filter((t) => t.variant === "error")).toHaveLength(0)
1372+
})
13171373
})
13181374

13191375
describe("a cached success is re-probed against the floor", () => {

‎packages/opencode/test/altimate/workspace/mutation-guards.test.ts‎

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -293,26 +293,25 @@ describe("the world check sits adjacent to every mutation", () => {
293293
// ---------------------------------------------------------------------------
294294
// T4 — answered after awaits that follow the final guard (announce, notify).
295295
// ---------------------------------------------------------------------------
296-
describe("the attached answer is fixed before it is announced", () => {
297-
test("a re-link during announceToolsChanged is answered `attached` for the old workspace", async () => {
296+
describe("the attached answer is true when it is given, not only when it was fixed", () => {
297+
test("a re-link during announceToolsChanged is not answered `attached` for the old workspace", async () => {
298298
const h = install({ statuses: [{}, { datamate: { status: "connected" } }], tools: { datamate_dbt_build_model: 1 } })
299299
syncInternals.toolsChanged = async () => {
300300
h.trace.push("toolsChanged")
301301
h.current = B
302302
}
303303
const outcome = await ensure("s1")
304-
// The answer is now fixed BEFORE the announcements rather than after them,
305-
// so the decision no longer straddles those awaits — but a re-link landing
306-
// inside the toast still leaves this turn holding `attached` for 42. It
307-
// cannot be guarded without either un-saying a toast already shown or
308-
// announcing a success we then retract.
309-
//
310-
// What must hold is that it does not OUTLIVE the turn: the memo is keyed to
311-
// the workspace it was taken for, so the next turn re-decides for 99 rather
312-
// than riding it.
313-
expect(outcome.kind).toBe("attached")
304+
// The answer is fixed before the announcements and GIVEN after them, and
305+
// the announcements are awaits. The world is asked once more after the
306+
// last of them: a re-link landing inside is undone and answered
307+
// `superseded`, so this turn never holds `attached` for a workspace the
308+
// project has left. The toast was true when it was shown; no second one.
309+
expect(outcome.kind).toBe("superseded")
310+
expect(h.removes, "left the old workspace's engine serving under the new binding").toEqual(["datamate"])
311+
expect(h.restores).toHaveLength(1)
312+
// Superseded is repairable: the next turn attaches for the new binding.
314313
const second = await ensure("s1")
315-
expect(second.kind, "rode a memo taken for the workspace the project had left").not.toBe("reused")
314+
expect(second.kind).toBe("attached")
316315
expect(h.added.at(-1)?.cfg.command, "did not re-attach for the new binding").toEqual([
317316
"datamate",
318317
"start-stdio",

‎packages/opencode/test/altimate/workspace/undo-and-teardown.test.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,10 @@ describe("the restore refuses on the text it edits", () => {
639639
})
640640

641641
test("memo validation read throws (transient) → not served, re-decided → reused; no toast", async () => {
642-
const { h } = realReader((n) => n === 4, () => (h.added.length ? PINNED42 : null))
642+
// The first attach makes four intent reads (inspection, pre-install
643+
// guard, post-install guard, post-announcement guard); the memo
644+
// validation on the next turn is the fifth.
645+
const { h } = realReader((n) => n === 5, () => (h.added.length ? PINNED42 : null))
643646
const first = await ensure("s1")
644647
expect(first.kind).toBe("attached")
645648
h.statusQueue = [{ datamate: { status: "connected" } }]

0 commit comments

Comments
 (0)