Skip to content

Commit 791a286

Browse files
feat(workspace): expose a read-only settledOutcome for other modules
Workspace precedence needs to attest which engine is actually serving a session, and had been awaiting ensure() to get it. That is unsafe in two ways: ensure() builds a fresh task per call and awaits the binding before resolving, so it never returns an already-settled promise and re-registers the session entry once per turn — mutating attach bookkeeping a caller only meant to read — and awaiting it is unbounded, reintroducing the prompt hang the bounded whenAttached exists to prevent. settledOutcome(sessionID) is a pure read of state already held: no task, no registration, no await. It returns undefined while an attach is in flight and for a session that never attached, so callers must read undefined as "not known yet" rather than "no engine". 3 tests: undefined before and settled after, undefined while in flight, and repeated reads leaving the session memo, the project chain and the attach count untouched.
1 parent 9295086 commit 791a286

2 files changed

Lines changed: 54 additions & 0 deletions

File tree

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -939,6 +939,23 @@ function attachOnce(sessionID: string): Promise<Outcome> {
939939
})
940940
}
941941

942+
/** The memoised outcome for a session, if its attach has already settled.
943+
*
944+
* A pure read: it creates no task, registers nothing, awaits nothing, and does
945+
* not touch the memo or the project chain. `ensure()` is deliberately unsuitable
946+
* for this — it builds a fresh task per call and awaits the binding before
947+
* resolving, so a caller polling it would never see an already-settled promise
948+
* AND would re-register the session entry once per turn, mutating bookkeeping it
949+
* only meant to read. Worse, awaiting it is unbounded, which reintroduces the
950+
* prompt hang the bounded `whenAttached` exists to prevent.
951+
*
952+
* Returns undefined while an attach is still in flight, and for a session that
953+
* has never attached. Callers must treat undefined as "not known yet", never as
954+
* "no engine". */
955+
export function settledOutcome(sessionID: string): Outcome | undefined {
956+
return sessions.get(sessionID)?.outcome
957+
}
958+
942959
/** Wait for a session's in-flight attach, capped.
943960
*
944961
* A turn resolves its tool list up front, before the per-turn block that starts

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
MAX_TRACKED_SESSIONS,
1919
trackedSessionsForTests,
2020
trackedChainsForTests,
21+
settledOutcome,
2122
type LocalMcpConfig,
2223
} from "../../../src/altimate/workspace/engine-sync"
2324
import type { CachedBinding } from "../../../src/altimate/workspace/state"
@@ -1037,3 +1038,39 @@ describe("ensure — round 8", () => {
10371038
expect(trackedChainsForTests()).toBe(0)
10381039
})
10391040
})
1041+
1042+
describe("settledOutcome — a read-only view for other modules", () => {
1043+
test("undefined before an attach exists, the outcome after it settles", async () => {
1044+
const h = install({
1045+
statuses: [{}, { datamate: { status: "connected" } }],
1046+
tools: { datamate_dbt_build_model: 1 },
1047+
})
1048+
expect(settledOutcome("s1")).toBeUndefined() // never attached
1049+
const outcome = await ensure("s1")
1050+
expect(settledOutcome("s1")).toEqual(outcome)
1051+
expect(h.added).toHaveLength(1)
1052+
})
1053+
1054+
test("undefined while the attach is still in flight — never a premature answer", async () => {
1055+
install({})
1056+
syncInternals.versionOf = () => new Promise<string | null>(() => {}) // never settles
1057+
void ensure("s1")
1058+
expect(settledOutcome("s1")).toBeUndefined()
1059+
await new Promise((r) => setTimeout(r, 20))
1060+
expect(settledOutcome("s1")).toBeUndefined()
1061+
})
1062+
1063+
test("reading never mutates the memo or the project chain", async () => {
1064+
const h = install({
1065+
statuses: [{}, { datamate: { status: "connected" } }],
1066+
tools: { datamate_dbt_build_model: 1 },
1067+
})
1068+
await ensure("s1")
1069+
const sessionsBefore = trackedSessionsForTests()
1070+
const chainsBefore = trackedChainsForTests()
1071+
for (let i = 0; i < 5; i++) settledOutcome("s1")
1072+
expect(trackedSessionsForTests()).toBe(sessionsBefore)
1073+
expect(trackedChainsForTests()).toBe(chainsBefore)
1074+
expect(h.added).toHaveLength(1) // no attach was triggered by reading
1075+
})
1076+
})

0 commit comments

Comments
 (0)