Skip to content

Commit 1a5d85a

Browse files
fix(workspace): arm the retry flag synchronously, and report unexpected failures
Codex round 7. Two findings, both verified; each new test was confirmed to fail with only its own fix reverted. **A repair retry is marked non-blocking before the entry is published.** The session entry is registered synchronously, because whenAttached reads it on the very next line — but the flag that makes a retry non-blocking was set after an await, so the timer was already armed by the time it ran. A retry that hung then charged the turn the full 15 seconds, which is exactly what that flag exists to prevent. Whether a turn is a repair retry depends only on the previous outcome, which is known synchronously; the workspace comparison is what needs the await. Deliberately conservative: if the binding also changed, the fresh attach may lose its wait for one turn. Failing to wait costs that turn's tools and is repaired by tools/list_changed; waiting wrongly costs every turn the full cap. The existing test for this passed for the wrong reason. Its retry settled immediately, so whenAttached returned on settle whatever the flag said. It now hangs the retry, which is the only way the flag is under test, and it fails against the old ordering. **An unexpected attach error now tells the user.** Every explicit failure branch emits guidance; a throw from outside them — an unwritable or malformed project config reaching persist — was converted to connect-failed and only logged. The caller discards the outcome and whenAttached returns void, so that single path left the user with neither tools nor an explanation. It now notifies before returning, which is what the rest of the module already promises.
1 parent 18ada3e commit 1a5d85a

2 files changed

Lines changed: 57 additions & 9 deletions

File tree

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

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,22 @@ export function ensure(sessionID: string): Promise<Outcome> {
763763
// turn would sail past without waiting — which is exactly the first-turn gap
764764
// this module exists to close. All the async work happens inside the task.
765765
const previous = sessions.get(sessionID)
766-
const entry = { key: previous?.key, waitTimedOut: previous?.waitTimedOut } as SessionAttach
766+
// Decided SYNCHRONOUSLY, because the entry is published synchronously and
767+
// `whenAttached` reads it on the very next line. Whether this is a repair
768+
// retry depends only on the previous outcome, which is already known — the
769+
// workspace comparison needs an await, and refining the flag after that await
770+
// is too late: the timer is armed by then, so a hung retry charged the turn
771+
// the full cap despite the retry being documented as non-blocking.
772+
//
773+
// Conservative in the right direction: if the binding also changed, the branch
774+
// below resets this to false and that fresh attach may lose its wait for one
775+
// turn. Failing to wait costs a turn's tools, which `tools/list_changed`
776+
// repairs; waiting wrongly costs every turn 15 seconds.
777+
const repairRetry = !!previous && isRepairable(previous.outcome)
778+
const entry = {
779+
key: previous?.key,
780+
waitTimedOut: previous?.waitTimedOut || repairRetry,
781+
} as SessionAttach
767782
entry.task = (async (): Promise<Outcome> => {
768783
const key = await attachKey()
769784
const sameWorkspace = !!previous && previous.key === key
@@ -839,9 +854,20 @@ function serializeAttach<T>(fn: () => Promise<T>): Promise<T> {
839854
* outcome logged exactly once. */
840855
function attachOnce(sessionID: string): Promise<Outcome> {
841856
return serializeAttach(() => run())
842-
.catch((err): Outcome => {
843-
log.warn("workspace engine attach failed", { err: String(err) })
844-
return { kind: "connect-failed", error: String(err) }
857+
.catch(async (err): Promise<Outcome> => {
858+
const error = String(err)
859+
// Every explicit failure branch tells the user what is unavailable and
860+
// why. An unexpected throw — an unwritable project config, a malformed
861+
// one — must not be the single path that leaves them with neither tools
862+
// nor an explanation, since the caller discards this outcome and
863+
// `whenAttached` returns void.
864+
log.warn("workspace engine attach failed", { err: error })
865+
await notify({
866+
title: "Workspace engine attach failed",
867+
message: `Could not attach the workspace engine: ${error}. Integration tools are unavailable for this session.`,
868+
variant: "error",
869+
})
870+
return { kind: "connect-failed", error }
845871
})
846872
.then((outcome) => {
847873
// One line per session, whatever happened — silence is the defect this

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

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -721,15 +721,21 @@ describe("ensure — a repairable failure is re-probed on the next turn", () =>
721721
expect(h.added).toHaveLength(1)
722722
})
723723

724-
test("a repairable retry does not re-arm the turn wait", async () => {
725-
install({ which: null })
724+
test("a repairable retry does not re-arm the turn wait, even when the retry HANGS", async () => {
725+
// The earlier version of this test let the retry settle immediately, so
726+
// whenAttached returned on settle and the test passed whatever the flag
727+
// said. The retry must hang for the flag to be the thing under test.
728+
let onPath: string | null = null
729+
install({})
730+
syncInternals.which = () => onPath
726731
expect(await ensure("s1")).toEqual({ kind: "engine-missing", declared: 2 })
727-
// Next turn re-probes, but whenAttached must return immediately rather than
728-
// charging this turn the full cap.
732+
733+
onPath = "/usr/local/bin/datamate"
734+
syncInternals.versionOf = () => new Promise<string | null>(() => {}) // never settles
729735
void ensure("s1")
730736
const started = performance.now()
731737
await whenAttached("s1", 5_000)
732-
expect(performance.now() - started).toBeLessThan(100)
738+
expect(performance.now() - started).toBeLessThan(150)
733739
})
734740
})
735741

@@ -951,3 +957,19 @@ describe("ensure — round 6: a stale binding must not be installed", () => {
951957
expect(h.added[0].cfg.command).toEqual(["datamate", "start-stdio", "--datamate", "42"])
952958
})
953959
})
960+
961+
describe("ensure — round 7", () => {
962+
test("an unexpected attach error still tells the user", async () => {
963+
// Every explicit failure branch notifies; an unexpected throw must not be
964+
// the one path that leaves the user with neither tools nor an explanation.
965+
const h = install({ statuses: [{}] })
966+
syncInternals.persist = async () => {
967+
throw new Error("EACCES: project config is not writable")
968+
}
969+
const outcome = await ensure("s1")
970+
expect(outcome).toMatchObject({ kind: "connect-failed" })
971+
expect(h.toasts).toHaveLength(1)
972+
expect(h.toasts[0].variant).toBe("error")
973+
expect(h.toasts[0].message).toContain("EACCES")
974+
})
975+
})

0 commit comments

Comments
 (0)