Skip to content

Commit 37714b4

Browse files
fix(workspace): close four attach races found by codex round 3
All four verified against the branch before fixing. **A superseded attach can no longer overwrite the current one.** Re-linking a live session started a second attach without ordering it against the first. Both end in `MCP.add`, and whichever completes last owns the runtime client, so a slower attach for the workspace just left could land after the new one and restore its tools — with the new memo already settled, so no later turn would repair it. Replacement attaches are now serialized per session: the superseded task is awaited before the next one starts, which makes the final `MCP.add` the current workspace's by construction. **Persisting the entry now invalidates the config cache.** `Config.get()` is cached per instance and `addMcpToConfig` is a raw file write that does not touch that cache, so every later `existingEntry()` in the process still saw the pre-write config. A managed entry then became unrecognisable to `isManagedEntry`, which is what leaves a stale engine attached in a project whose binding stopped resolving — the failure mode the previous commit's unbound teardown was supposed to prevent. The local-config write path in `config.ts` already invalidates for this exact reason; the engine entry now does too. **A repairable failure is re-probed on the next turn.** `engine-missing`, `engine-too-old` and `connect-failed` were memoised for the life of the session, so a user who followed the install hint we had just printed saw nothing happen until they started a new session. Those three outcomes are now retried; success stays memoised, so this does not mean re-attaching every turn. The retry deliberately does NOT re-arm the bounded wait: it runs on every turn, and a `connect-failed` retry sitting in MCP's 30s connect budget would otherwise charge each turn the full cap. Repaired tools arrive over `tools/list_changed`. **The version probe targets the engine, not its wrapper.** For a pinned entry like `npx @altimateai/[email protected] start-stdio --datamate 42`, the probe ran `npx --version`, so a modern wrapper vouched for a pre-floor engine that does not provide the locked workspace pin attribution relies on. Only a directly identifiable `datamate` executable is probed now; anything else yields no version and falls through to the below-floor handling — replaced from PATH, or reported. Asking the running server instead is not an option: `serverInfo.version` is a hard-coded placeholder on precisely the engines this floor excludes. 7 further tests: retry after install and after update, success still memoised, the retry not re-arming the wait, npx not probed and replaced, an absolute datamate path probed and reused, and a slow superseded attach losing to the re-linked workspace.
1 parent 2480439 commit 37714b4

2 files changed

Lines changed: 186 additions & 8 deletions

File tree

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

Lines changed: 57 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,15 @@ async function persist(name: string, cfg: LocalMcpConfig): Promise<void> {
263263
if (syncInternals.persist) return syncInternals.persist(name, cfg)
264264
const configPath = await resolveConfigPath(projectRoot())
265265
await addMcpToConfig(name, cfg, configPath)
266+
// `Config.get()` is cached per instance, and `addMcpToConfig` is a raw file
267+
// write that does not touch that cache — so without this, every later
268+
// `existingEntry()` in this process still sees the pre-write config. That is
269+
// how a managed entry becomes unrecognisable to `isManagedEntry` later in the
270+
// same server process, leaving a stale engine attached in an unbound project.
271+
// The local-config write path in `config.ts` invalidates for the same reason.
272+
await Config.invalidate().catch((err) => {
273+
log.warn("could not invalidate the config cache after persisting the engine entry", { err: String(err) })
274+
})
266275
}
267276

268277
async function existingEntry(name: string): Promise<ExistingEntry | null> {
@@ -486,8 +495,16 @@ async function run(): Promise<Outcome> {
486495
})
487496
await detachRejected({ workspaceId, reason: "not-attributable", pinnedTo: pin })
488497
} else {
498+
// Probe the ENGINE, not whatever wraps it. `npx @altimateai/[email protected]
499+
// start-stdio --datamate 42` would otherwise have us run `npx --version`
500+
// and let a pre-floor engine clear the floor on the wrapper's version.
501+
// Asking the running server instead is not an option: `serverInfo.version`
502+
// is a hard-coded placeholder on the very engines this floor excludes.
503+
// An unidentifiable command yields no version, which falls through to the
504+
// below-floor handling — replace it from PATH, or report it.
489505
const entryBin = commandArgv(entry)[0]
490-
const found = entryBin ? await versionOf(entryBin) : null
506+
const directBin = entryBin && /(^|[\\/])datamate(\.[a-z]+)?$/i.test(entryBin) ? entryBin : null
507+
const found = directBin ? await versionOf(directBin) : null
491508
if (found && compareVersions(found, MIN_ENGINE_VERSION) >= 0) {
492509
// Rule 5 applies to a reused engine too. A running engine that lost an
493510
// integration — a connection deleted, a restart that dropped it —
@@ -633,7 +650,17 @@ async function run(): Promise<Outcome> {
633650
* answers costs the first turn a pause rather than the turn itself. */
634651
export const ATTACH_WAIT_MS = 15_000
635652

636-
type SessionAttach = { key?: string; task: Promise<Outcome>; waitTimedOut?: boolean }
653+
type SessionAttach = { key?: string; task: Promise<Outcome>; waitTimedOut?: boolean; outcome?: Outcome }
654+
655+
/** Outcomes the user can repair without restarting: install the engine, update
656+
* it, fix a broken entry. Caching these for the life of the session means the
657+
* hint we just printed ("install it with …") can be followed and nothing
658+
* happens until a new session — so they are re-probed on the next turn. */
659+
const REPAIRABLE = new Set<Outcome["kind"]>(["engine-missing", "engine-too-old", "connect-failed"])
660+
661+
function isRepairable(outcome: Outcome | undefined): boolean {
662+
return !!outcome && REPAIRABLE.has(outcome.kind)
663+
}
637664

638665
const sessions = new Map<string, SessionAttach>()
639666

@@ -662,15 +689,37 @@ export function ensure(sessionID: string): Promise<Outcome> {
662689
const entry = { key: previous?.key, waitTimedOut: previous?.waitTimedOut } as SessionAttach
663690
entry.task = (async (): Promise<Outcome> => {
664691
const key = await attachKey()
665-
// Same workspace as the attach we already did for this session: reuse it.
666-
if (previous && previous.key === key) return previous.task
667-
// First attach for this session, or the binding changed under it. A changed
668-
// binding gets a fresh attach AND a fresh wait budget: the previous budget
669-
// was spent on a different workspace's engine.
692+
const sameWorkspace = !!previous && previous.key === key
693+
// Same workspace and the attach either succeeded or is still in flight:
694+
// reuse it. A settled FAILURE is not reused — the user may have acted on
695+
// the hint it produced.
696+
if (sameWorkspace && !isRepairable(previous!.outcome)) return previous!.task
670697
entry.key = key
671-
entry.waitTimedOut = false
698+
if (sameWorkspace) {
699+
// Re-probing a repairable failure. Do NOT re-arm the wait: this runs on
700+
// every turn, and a retry that blocks would charge each one the full cap
701+
// (a `connect-failed` retry can sit in MCP's 30s connect budget). The
702+
// repaired engine's tools arrive over `tools/list_changed` instead.
703+
entry.waitTimedOut = true
704+
} else {
705+
// The binding changed under this session. A fresh attach gets a fresh wait
706+
// budget — the previous one was spent on a different workspace's engine.
707+
entry.waitTimedOut = false
708+
// Serialize against the attach being superseded. Both tasks end in
709+
// `MCP.add`, and whichever completes LAST owns the runtime client, so a
710+
// slower attach for the workspace we just left could otherwise land after
711+
// this one and restore its tools — with this session's memo already
712+
// settled, so no later turn would repair it.
713+
if (previous) await previous.task.catch(() => {})
714+
}
672715
return attachOnce(sessionID)
673716
})()
717+
entry.task.then(
718+
(outcome) => {
719+
entry.outcome = outcome
720+
},
721+
() => {},
722+
)
674723
sessions.set(sessionID, entry)
675724
return entry.task
676725
}

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

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,3 +671,132 @@ describe("ensure — an unbound project does not keep a stale MANAGED entry", ()
671671
expect(h.toasts).toHaveLength(0)
672672
})
673673
})
674+
675+
describe("ensure — a repairable failure is re-probed on the next turn", () => {
676+
test("engine-missing is retried once the engine appears, without a new session", async () => {
677+
let onPath: string | null = null
678+
const h = install({
679+
statuses: [{}, { datamate: { status: "connected" } }],
680+
tools: { datamate_dbt_build_model: 1, datamate_dbt_compile_model: 1 },
681+
})
682+
syncInternals.which = () => onPath
683+
684+
// Turn 1: no engine. We print the install hint.
685+
expect(await ensure("s1")).toEqual({ kind: "engine-missing", declared: 2 })
686+
expect(h.added).toHaveLength(0)
687+
688+
// The user follows that hint mid-session.
689+
onPath = "/usr/local/bin/datamate"
690+
expect(await ensure("s1")).toMatchObject({ kind: "attached" })
691+
expect(h.added).toHaveLength(1)
692+
})
693+
694+
test("engine-too-old is retried after an update", async () => {
695+
let version = "0.5.9"
696+
const h = install({
697+
statuses: [{}, { datamate: { status: "connected" } }],
698+
tools: { datamate_dbt_build_model: 1, datamate_dbt_compile_model: 1 },
699+
})
700+
syncInternals.versionOf = async () => version
701+
702+
expect(await ensure("s1")).toEqual({ kind: "engine-too-old", found: "0.5.9" })
703+
version = "0.7.0"
704+
expect(await ensure("s1")).toMatchObject({ kind: "attached" })
705+
expect(h.added).toHaveLength(1)
706+
})
707+
708+
test("a SUCCESSFUL outcome is still memoised — retry must not mean re-attach every turn", async () => {
709+
const h = install({
710+
statuses: [{}, { datamate: { status: "connected" } }],
711+
tools: { datamate_dbt_build_model: 1, datamate_dbt_compile_model: 1 },
712+
})
713+
const first = await ensure("s1")
714+
expect(first).toMatchObject({ kind: "attached" })
715+
expect(await ensure("s1")).toBe(first)
716+
expect(await ensure("s1")).toBe(first)
717+
expect(h.added).toHaveLength(1)
718+
})
719+
720+
test("a repairable retry does not re-arm the turn wait", async () => {
721+
install({ which: null })
722+
expect(await ensure("s1")).toEqual({ kind: "engine-missing", declared: 2 })
723+
// Next turn re-probes, but whenAttached must return immediately rather than
724+
// charging this turn the full cap.
725+
void ensure("s1")
726+
const started = performance.now()
727+
await whenAttached("s1", 5_000)
728+
expect(performance.now() - started).toBeLessThan(100)
729+
})
730+
})
731+
732+
describe("ensure — the version probe targets the engine, not its wrapper", () => {
733+
test("an npx-wrapped entry is not trusted on the wrapper's version", async () => {
734+
const probed: string[] = []
735+
const h = install({
736+
existing: { type: "local", command: ["npx", "@altimateai/[email protected]", "start-stdio", "--datamate", "42"] },
737+
statuses: [{ datamate: { status: "connected" } }, { datamate: { status: "connected" } }],
738+
tools: { datamate_dbt_build_model: 1, datamate_dbt_compile_model: 1 },
739+
})
740+
syncInternals.versionOf = async (bin) => {
741+
probed.push(bin)
742+
return "0.7.0"
743+
}
744+
const outcome = await ensure("s1")
745+
// npx is never probed — a modern wrapper must not vouch for an old engine.
746+
expect(probed).not.toContain("npx")
747+
// Unverifiable, so it is replaced by a pinned spawn we can vouch for.
748+
expect(outcome).toMatchObject({ kind: "attached" })
749+
expect(h.removes).toEqual(["datamate"])
750+
expect(h.added[0].cfg.command).toEqual(["datamate", "start-stdio", "--datamate", "42"])
751+
})
752+
753+
test("an absolute path to a real datamate IS probed and reused", async () => {
754+
const probed: string[] = []
755+
const h = install({
756+
existing: { type: "local", command: ["/opt/bin/datamate", "start-stdio", "--datamate", "42"] },
757+
statuses: [{ datamate: { status: "connected" } }],
758+
tools: { datamate_dbt_build_model: 1, datamate_dbt_compile_model: 1 },
759+
})
760+
syncInternals.versionOf = async (bin) => {
761+
probed.push(bin)
762+
return "0.7.0"
763+
}
764+
expect(await ensure("s1")).toMatchObject({ kind: "reused" })
765+
expect(probed).toContain("/opt/bin/datamate")
766+
expect(h.added).toHaveLength(0)
767+
})
768+
})
769+
770+
describe("ensure — a superseded attach cannot overwrite the current one", () => {
771+
test("the re-linked workspace wins even when the old attach is slower", async () => {
772+
let current: CachedBinding | null = binding // 42
773+
const h = install({
774+
statuses: [
775+
{},
776+
{ datamate: { status: "connected" } },
777+
{},
778+
{ datamate: { status: "connected" } },
779+
],
780+
tools: { datamate_dbt_build_model: 1 },
781+
})
782+
syncInternals.resolveBinding = async () => current
783+
// Make the FIRST attach slow, so without serialization it would land last.
784+
let firstAdd = true
785+
syncInternals.mcp!.add = async (name, cfg) => {
786+
if (firstAdd) {
787+
firstAdd = false
788+
await new Promise((r) => setTimeout(r, 60))
789+
}
790+
h.added.push({ name, cfg })
791+
}
792+
793+
const a = ensure("s1") // workspace 42
794+
current = { ...binding, datamateId: 99, datamateName: "other" } as CachedBinding
795+
const b = ensure("s1") // re-link to 99
796+
await Promise.all([a, b])
797+
798+
// Both ran, but in order: the LAST add must be the workspace we re-linked to.
799+
expect(h.added).toHaveLength(2)
800+
expect(h.added[h.added.length - 1].cfg.command).toEqual(["datamate", "start-stdio", "--datamate", "99"])
801+
})
802+
})

0 commit comments

Comments
 (0)