Skip to content

Commit 7632549

Browse files
fix(workspace): refuse the managed key in /mcps; fail closed on a relink whose probe fails
`/mcps enable|disable datamate` reached MCP directly and bypassed both guarded routes; it now answers with the managed-key refusal. When a relink to another workspace is followed by a failed probe, the previous workspace's engine is released rather than retained; a bound directory whose overlay could not be derived settles connect-failed and says so once instead of a silent unbound.
1 parent b2e5c6a commit 7632549

3 files changed

Lines changed: 57 additions & 4 deletions

File tree

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

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -407,12 +407,27 @@ async function reconcile(sessionID: string, directory: string, state: DirectoryS
407407
}
408408

409409
// A transient overlay failure (its retry is throttled above) keeps what was
410-
// last applied: a running engine is not released over a fault in the probe.
411-
const overlayNow = state.current ?? (state.failedAt !== undefined ? (state.applied ?? null) : null)
410+
// last applied for this same workspace: a running engine is not released
411+
// over a fault in the probe. After a relink nothing is kept — workspace A's
412+
// engine must not serve a directory now bound to B.
413+
const retained = state.failedAt !== undefined && state.applied?.workspace.id === workspaceId ? state.applied : null
414+
const overlayNow = state.current ?? retained
412415
if (!overlayNow) {
413416
if (state.applied) await releaseKey(loaded, !!state.applied.entry)
414417
state.applied = null
415-
record(sessionID, { kind: "unbound" })
418+
if (state.failedAt === undefined) {
419+
record(sessionID, { kind: "unbound" })
420+
return
421+
}
422+
// Bound, but the overlay could not be derived: say so, once, rather than
423+
// settling a bound directory as unbound in silence.
424+
const outcome: Outcome = { kind: "connect-failed", error: "the workspace engine could not be checked" }
425+
record(sessionID, outcome)
426+
await announceRefusal(sessionID, outcome, {
427+
title: `Workspace "${binding.datamateName}": engine unavailable`,
428+
message: `${outcome.error}; it is checked again shortly.`,
429+
variant: "warning",
430+
})
416431
return
417432
}
418433
const workspace = overlayNow.workspace

‎packages/opencode/src/session/prompt.ts‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ import { MemoryPrompt } from "../memory/prompt"
2525
import { UNIFIED_INJECTION_BUDGET } from "../memory/types"
2626
// altimate_change - workspace memory read path
2727
import * as WorkspaceMemory from "../altimate/workspace/memory-sync"
28-
// altimate_change - workspace engine turn boundary
28+
// altimate_change start — workspace engine turn boundary and managed-key refusal
2929
import * as WorkspaceEngine from "../altimate/workspace/engine-overlay"
30+
import { DATAMATE_KEY } from "../altimate/datamate-transport"
31+
// altimate_change end
3032
import { Plugin } from "../plugin"
3133
import PROMPT_PLAN from "../session/prompt/plan.txt"
3234
import BUILD_SWITCH from "../session/prompt/build-switch.txt"
@@ -2985,6 +2987,18 @@ NOTE: At any point in time through this workflow you should feel free to ask the
29852987
)
29862988
}
29872989

2990+
// The workspace-managed `datamate` key is derived per process: this
2991+
// command must not close or restart that engine, nor persist `enabled`
2992+
// for it. Same refusal as the HTTP routes, in the command's own reply.
2993+
const managed = name === DATAMATE_KEY ? await WorkspaceEngine.managedWorkspaceLoaded() : null
2994+
if (managed) {
2995+
return respond(
2996+
userMsg.info.id,
2997+
`MCP server **${name}** is managed by workspace **${managed.name}** in this project and cannot be ${subCmd}d here. Unlink the project, or run without ALTIMATE_WORKSPACE, to manage it by hand.`,
2998+
model,
2999+
)
3000+
}
3001+
29883002
let responseText: string
29893003

29903004
if (isEnable) {

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,14 +513,18 @@ describe("beforeTurn — what a turn boundary does", () => {
513513
expect(h.added).toHaveLength(0)
514514
expect(settledOutcome("s1")?.kind).toBe("attached")
515515
// Something else invalidates config, and the overlay's probe now throws.
516+
// The usable-engine memo would mask the throw; forget it so the fault fires.
516517
syncInternals.which = () => {
517518
throw new Error("PATH unreadable")
518519
}
520+
invalidateProbe()
519521
await syncInternals.config!.invalidate()
520522
await beforeTurn("s1")
521523
expect(h.removes).toBe(0)
522524
expect(h.added).toHaveLength(0)
523525
expect(settledOutcome("s1")?.kind).toBe("attached")
526+
// The fault was real: the overlay is gone until its retry, the engine is not.
527+
expect(overlayForTests()).toBeNull()
524528
// The fault clears and the TTL passes: still the same engine, not a second one.
525529
h.which = "/usr/local/bin/datamate"
526530
syncInternals.which = () => h.which
@@ -531,6 +535,26 @@ describe("beforeTurn — what a turn boundary does", () => {
531535
expect(settledOutcome("s1")?.kind).toBe("attached")
532536
})
533537

538+
test("a relink whose overlay then fails releases the old workspace's engine and says so", async () => {
539+
// Workspace A's engine may not go on serving a directory now bound to B.
540+
const h = install({})
541+
await beforeTurn("s1")
542+
expect(settledOutcome("s1")?.kind).toBe("attached")
543+
h.binding = bound(43, "ops")
544+
syncInternals.which = () => {
545+
throw new Error("PATH unreadable")
546+
}
547+
invalidateProbe()
548+
await beforeTurn("s1")
549+
expect(h.removes).toBe(1)
550+
expect(h.added).toHaveLength(0)
551+
expect(settledOutcome("s1")?.kind).toBe("connect-failed")
552+
// The attach toast, then the failure's — naming the workspace now bound.
553+
expect(h.toasts).toHaveLength(2)
554+
expect(h.toasts[1].title).toContain("ops")
555+
expect(h.toasts[1].title).toContain("unavailable")
556+
})
557+
534558
test("an unlink hands the key back to the entry the reloaded config restores", async () => {
535559
// The overlay had shadowed the user's own hosted entry; once unbound, config
536560
// reloads with that entry and MCP must be told to start it, because MCP only

0 commit comments

Comments
 (0)