Skip to content

Commit a84e7c3

Browse files
fix(workspace): respect a disabled entry, and serialize attaches per project
Codex round 4 on 37714b4. Two findings, both verified, and both new tests were confirmed to fail against the unfixed code before the fix was written. **An explicitly disabled entry is no longer silently re-enabled.** A configured entry with `enabled: false` is reported by `MCP.status()` as `disabled`, which this flow read as "not connected" and retried with `MCP.connect`. That call persists `enabled: true` into whichever config file owns the entry, so for a global `datamate` the first prompt in any bound project quietly re-enabled it for every other project — the same class as the round-1 teardown finding, in the opposite direction. A disabled entry is now left alone and reported as such; only a genuinely `failed` entry is retried. **Attaches are serialized per project, not merely per session.** The previous commit ordered replacement attaches within a session, but the MCP client is instance-wide, `MCP.add` is last-writer-wins, and `SessionRunState` keeps independent runners per session id — so two prompts in the same project overlap for real. A slower attach from one session could land after another's and leave the runtime serving a workspace nobody is bound to, with both memos settled so no later turn would repair it. Attaches now run through a per-project chain, which also subsumes the per-session ordering. `entry-disabled` joins the repairable outcomes, so enabling the entry mid-session is picked up on the next turn rather than requiring a new session. 3 further tests: a disabled entry is neither connected nor persisted, a failed entry is still retried exactly once, and two overlapping sessions in one project never hold the mutating phase at the same time. The last of these asserts the invariant directly (peak concurrency of 1) after two attempts that passed against the unfixed code and so proved nothing.
1 parent 37714b4 commit a84e7c3

2 files changed

Lines changed: 109 additions & 3 deletions

File tree

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

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ export type Outcome =
9797
| { kind: "engine-missing"; declared: number }
9898
| { kind: "engine-too-old"; found: string }
9999
| { kind: "connect-failed"; error: string }
100+
| { kind: "entry-disabled" }
100101

101102
export type LocalMcpConfig = { type: "local"; command: string[]; enabled: boolean }
102103

@@ -446,6 +447,22 @@ async function run(): Promise<Outcome> {
446447
let connected = existing.status === "connected"
447448

448449
if (!connected) {
450+
if (existing.status === "disabled") {
451+
// The user turned this entry off deliberately. Do NOT call `MCP.connect`
452+
// to "retry" it: that persists `enabled: true` into whichever config
453+
// owns the entry, so for a global `datamate` the first prompt in any
454+
// bound project would silently re-enable it for every other project.
455+
// Say what is unavailable and leave their choice alone.
456+
log.info("engine entry is explicitly disabled; leaving it alone", { workspaceId })
457+
await notify({
458+
title: "Workspace engine is disabled",
459+
message:
460+
`The "${DATAMATE_KEY}" MCP entry is disabled, so workspace "${binding.datamateName}" ` +
461+
`integration tools are unavailable. Enable it to use them.`,
462+
variant: "warning",
463+
})
464+
return { kind: "entry-disabled" }
465+
}
449466
if (isUrlEntry(entry)) {
450467
// Dead URL: nothing here can bring that process back — only the IDE can
451468
// restore its port. Fall through to a local spawn and report it below.
@@ -656,7 +673,7 @@ type SessionAttach = { key?: string; task: Promise<Outcome>; waitTimedOut?: bool
656673
* it, fix a broken entry. Caching these for the life of the session means the
657674
* hint we just printed ("install it with …") can be followed and nothing
658675
* 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"])
676+
const REPAIRABLE = new Set<Outcome["kind"]>(["engine-missing", "engine-too-old", "connect-failed", "entry-disabled"])
660677

661678
function isRepairable(outcome: Outcome | undefined): boolean {
662679
return !!outcome && REPAIRABLE.has(outcome.kind)
@@ -724,9 +741,44 @@ export function ensure(sessionID: string): Promise<Outcome> {
724741
return entry.task
725742
}
726743

727-
/** One attach, with the outcome logged exactly once. */
744+
/** In-flight attach chain per project.
745+
*
746+
* Per-session ordering is not enough: the MCP client is instance-wide, not per
747+
* session, `MCP.add` is last-writer-wins, and `SessionRunState` keeps
748+
* independent runners per session id — so two prompts in the same project
749+
* genuinely overlap. Without this, a slower attach from one session can land
750+
* after another session's and leave the runtime serving a workspace nobody is
751+
* bound to, with both memos settled so no later turn repairs it. */
752+
const attachChains = new Map<string, Promise<unknown>>()
753+
754+
function projectKey(): string {
755+
try {
756+
return projectRoot()
757+
} catch {
758+
return "<no-instance>"
759+
}
760+
}
761+
762+
function serializeAttach<T>(fn: () => Promise<T>): Promise<T> {
763+
const key = projectKey()
764+
const previous = attachChains.get(key) ?? Promise.resolve()
765+
// Run regardless of how the previous attach ended — a failure must not wedge
766+
// the chain for the rest of the process.
767+
const next = previous.then(fn, fn)
768+
attachChains.set(
769+
key,
770+
next.then(
771+
() => {},
772+
() => {},
773+
),
774+
)
775+
return next
776+
}
777+
778+
/** One attach, serialized against every other attach in this project, with the
779+
* outcome logged exactly once. */
728780
function attachOnce(sessionID: string): Promise<Outcome> {
729-
return run()
781+
return serializeAttach(() => run())
730782
.catch((err): Outcome => {
731783
log.warn("workspace engine attach failed", { err: String(err) })
732784
return { kind: "connect-failed", error: String(err) }
@@ -783,4 +835,5 @@ export async function whenAttached(sessionID: string, timeoutMs: number = ATTACH
783835
/** Test seam — drop memoised outcomes. */
784836
export function resetForTests(): void {
785837
sessions.clear()
838+
attachChains.clear()
786839
}

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

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -800,3 +800,56 @@ describe("ensure — a superseded attach cannot overwrite the current one", () =
800800
expect(h.added[h.added.length - 1].cfg.command).toEqual(["datamate", "start-stdio", "--datamate", "99"])
801801
})
802802
})
803+
804+
describe("ensure — round 4", () => {
805+
test("an explicitly disabled entry is respected, never silently re-enabled", async () => {
806+
// MCP.connect persists `enabled: true` into whichever config owns the entry,
807+
// so retrying a DISABLED entry would undo a deliberate global disable for
808+
// every other project.
809+
const h = install({
810+
existing: { type: "local", command: ["datamate", "start-stdio"] },
811+
statuses: [{ datamate: { status: "disabled" } }],
812+
})
813+
expect(await ensure("s1")).toEqual({ kind: "entry-disabled" })
814+
expect(h.connects).toHaveLength(0)
815+
expect(h.added).toHaveLength(0)
816+
expect(h.persisted).toHaveLength(0)
817+
})
818+
819+
test("a genuinely FAILED entry is still retried once", async () => {
820+
const h = install({
821+
existing: { type: "local", command: ["datamate", "start-stdio"] },
822+
statuses: [
823+
{ datamate: { status: "failed", error: "exit 1" } },
824+
{ datamate: { status: "failed", error: "exit 1" } },
825+
],
826+
})
827+
expect(await ensure("s1")).toEqual({ kind: "connect-failed", error: "exit 1" })
828+
expect(h.connects).toEqual(["datamate"])
829+
})
830+
831+
test("two overlapping SESSIONS in one project never attach concurrently", async () => {
832+
// MCP state is instance-wide and MCP.add is last-writer-wins, while
833+
// SessionRunState keeps independent runners per session id — so per-session
834+
// ordering is not enough. The invariant is that no two attaches for the same
835+
// project are ever in their mutating phase at the same time.
836+
const h = install({
837+
statuses: [{}, { datamate: { status: "connected" } }, {}, { datamate: { status: "connected" } }],
838+
tools: { datamate_dbt_build_model: 1 },
839+
})
840+
let inFlight = 0
841+
let peak = 0
842+
syncInternals.mcp!.add = async (name, cfg) => {
843+
inFlight += 1
844+
peak = Math.max(peak, inFlight)
845+
await new Promise((r) => setTimeout(r, 40))
846+
h.added.push({ name, cfg })
847+
inFlight -= 1
848+
}
849+
850+
await Promise.all([ensure("sessionA"), ensure("sessionB")])
851+
852+
expect(h.added).toHaveLength(2)
853+
expect(peak).toBe(1) // 2 without project-scoped serialization
854+
})
855+
})

0 commit comments

Comments
 (0)