diff --git a/.changeset/assistant-lifecycle-admission.md b/.changeset/assistant-lifecycle-admission.md new file mode 100644 index 000000000..17f4de1ce --- /dev/null +++ b/.changeset/assistant-lifecycle-admission.md @@ -0,0 +1,5 @@ +--- +"@sapiom/harness": patch +--- + +Release cancelled Assistant preparation without dispatching late results, isolate admission by runtime, and add durable lifecycle and execution-lease coordination. diff --git a/packages/harness/src/core/assistant-lifecycle.test.ts b/packages/harness/src/core/assistant-lifecycle.test.ts new file mode 100644 index 000000000..c94339854 --- /dev/null +++ b/packages/harness/src/core/assistant-lifecycle.test.ts @@ -0,0 +1,282 @@ +import { afterEach, beforeEach, expect, it, vi } from "vitest"; +import { mkdtemp, rm } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { AssistantLifecycleCoordinator } from "./assistant-lifecycle.js"; +import { AssistantSessionStore } from "./assistant-session-store.js"; +import type { HostedOpenCode } from "./opencode-host.js"; + +let root: string; +let store: AssistantSessionStore; +let coordinator: AssistantLifecycleCoordinator; +let current: HostedOpenCode | null; +let abort: AbortController; +let eligible: boolean; +const ensure = vi.fn(), + observe = vi.fn(), + associate = vi.fn(); +const retire = vi.fn(), + assertCurrent = vi.fn(); +const gate = () => { + let release!: () => void; + const promise = new Promise((done) => { + release = done; + }); + return { promise, release }; +}; +const fixture = () => + new AssistantLifecycleCoordinator({ + store, + associations: { ensure: associate }, + host: { + ensure, + observe, + assertCurrent, + current: () => (current?.isCurrent() ? current : null), + retireWithResult: retire, + retireExact: async (hosted) => { + if (current === hosted) await retire(); + }, + beginShutdown: () => { + void retire(); + }, + }, + }); +beforeEach(async () => { + root = await mkdtemp(join(tmpdir(), "assistant-lifecycle-")); + store = new AssistantSessionStore(root); + current = null; + eligible = true; + observe.mockReset(); + ensure.mockReset().mockImplementation(async (id) => { + if (!eligible) throw new Error("access denied"); + if (current?.isCurrent()) return current; + abort = new AbortController(); + const signal = abort.signal; + const hosted: HostedOpenCode = { + harnessSessionId: id, + cwd: root, + stateRoot: join(root, "native"), + contextAuthorityScope: "a".repeat(64), + model: { providerID: "sapiom", modelID: "fixture" }, + signal, + isCurrent: () => eligible && !signal.aborted && current === hosted, + server: { + pid: 123, + exited: new Promise(() => {}), + close: async () => {}, + fetch: vi.fn(), + fetchJson: vi.fn(), + }, + }; + current = hosted; + return hosted; + }); + assertCurrent.mockReset().mockImplementation(async (hosted) => { + if (!hosted.isCurrent()) throw new Error("not current"); + }); + associate.mockReset().mockResolvedValue("ses_history"); + retire.mockReset().mockImplementation(async () => { + const state = current ? "confirmed" : "absent"; + abort?.abort(); + current = null; + return { state }; + }); + coordinator = fixture(); +}); +afterEach(async () => { + abort?.abort(); + await rm(root, { recursive: true, force: true }); +}); + +it("reads metadata without launch and starts each new process paused", async () => { + expect(await coordinator.describe("studio-a")).toMatchObject({ + revision: 0, + execution: "paused", + }); + expect(ensure).not.toHaveBeenCalled(); + const attached = await coordinator.attach("studio-a", 0); + expect(attached).toMatchObject({ + conversationId: "ses_history", + lifecycle: { revision: 1, execution: "paused" }, + }); + await expect( + coordinator.use("studio-a", attached.lease, true), + ).rejects.toMatchObject({ failure: { code: "execution_paused" } }); + await coordinator.enable(current!); + await expect( + coordinator.use("studio-a", attached.lease, true), + ).resolves.toMatchObject({ conversationId: "ses_history" }); + expect(await coordinator.attach("studio-a", 2)).toMatchObject({ + lease: attached.lease, + lifecycle: { execution: "enabled" }, + }); + expect(ensure).toHaveBeenCalledOnce(); + expect(associate).toHaveBeenCalledOnce(); + await retire(); + coordinator = fixture(); + expect(await coordinator.describe("studio-a")).toMatchObject({ + revision: 2, + execution: "paused", + }); + const reopened = await coordinator.attach("studio-a", 2); + expect(reopened.lease).not.toBe(attached.lease); + expect(reopened.lifecycle.execution).toBe("paused"); + await expect(coordinator.use("studio-a", attached.lease)).rejects.toThrow(); + expect(current!.server.fetch).not.toHaveBeenCalled(); +}); + +it("coalesces duplicate attach and rejects stale revisions without native work", async () => { + const held = gate(); + associate.mockImplementationOnce(async () => { + await held.promise; + return "ses_history"; + }); + const first = coordinator.attach("studio-a", 0); + expect(coordinator.attach("studio-a", 0)).toBe(first); + await expect(coordinator.attach("studio-a", 1)).rejects.toThrow(); + held.release(); + await first; + await expect(coordinator.attach("studio-a", 0)).rejects.toThrow(); + expect(ensure).toHaveBeenCalledOnce(); +}); + +it("rejects a late attach after End and retains its durable fence without eligibility", async () => { + const held = gate(); + associate.mockImplementationOnce(async () => { + await held.promise; + return "ses_history"; + }); + const first = coordinator.attach("studio-a", 0); + const rejected = expect(first).rejects.toMatchObject({ + failure: { code: "lifecycle_changed" }, + }); + await vi.waitFor(() => expect(associate).toHaveBeenCalledOnce()); + const ending = coordinator.beginEnd("studio-a"); + expect(abort.signal.aborted).toBe(true); + eligible = false; + await ending.native; + await ending.persistence; + held.release(); + await rejected; + await coordinator.finishEnd(ending.fence); + expect(await store.lifecycle("studio-a")).toMatchObject({ + lifecycle: "ended", + execution: "paused", + }); + eligible = true; + const state = await coordinator.describe("studio-a"); + await expect( + coordinator.attach("studio-a", state.revision), + ).rejects.toMatchObject({ failure: { code: "session_ended" } }); + expect(ensure).toHaveBeenCalledOnce(); +}); + +it("closes admission immediately even if End persistence stalls or fails", async () => { + const attached = await coordinator.attach("studio-a", 0); + const held = gate(); + vi.spyOn(store, "transition").mockImplementationOnce(async () => { + await held.promise; + throw new Error("storage unavailable"); + }); + const ending = coordinator.beginEnd("studio-a"); + const failed = expect(ending.persistence).rejects.toThrow( + "storage unavailable", + ); + expect(abort.signal.aborted).toBe(true); + await expect(coordinator.use("studio-a", attached.lease)).rejects.toThrow(); + expect((await coordinator.describe("studio-a")).lifecycle).toBe("ending"); + held.release(); + await failed; + await ending.native; + expect((await coordinator.describe("studio-a")).lifecycle).toBe("ending"); +}); + +it("a hung old attach cannot block or retire a later authorized runtime", async () => { + const held = gate(); + associate.mockImplementationOnce(async () => { + await held.promise; + return "ses_history"; + }); + const old = coordinator.attach("studio-a", 0); + const rejected = expect(old).rejects.toThrow(); + await vi.waitFor(() => expect(associate).toHaveBeenCalledOnce()); + const ending = coordinator.beginEnd("studio-a"); + await ending.persistence; + await ending.native; + const ended = await coordinator.finishEnd(ending.fence); + // Simulate the later explicit Resume revision claim; never an ordinary SDK read. + const resumed = await store.transition("studio-a", ended.revision, { + lifecycle: "open", + execution: "paused", + }); + const next = await coordinator.attach("studio-a", resumed.revision); + const replacement = current; + held.release(); + await rejected; + expect(current).toBe(replacement); + await expect(coordinator.use("studio-a", next.lease)).resolves.toMatchObject({ + hosted: replacement, + }); + expect(retire).toHaveBeenCalledOnce(); +}); + +it("revalidates the exact lease after asynchronous authorization", async () => { + const attached = await coordinator.attach("studio-a", 0); + const held = gate(); + assertCurrent.mockImplementationOnce(async () => { + await held.promise; + }); + const pending = coordinator.use("studio-a", attached.lease); + const rejected = expect(pending).rejects.toThrow(); + await vi.waitFor(() => expect(assertCurrent).toHaveBeenCalledTimes(3)); + const ending = coordinator.beginEnd("studio-a"); + held.release(); + await rejected; + await ending.persistence; + await ending.native; +}); + +it("invalidates leases on authority loss and durable lifecycle changes", async () => { + const attached = await coordinator.attach("studio-a", 0); + eligible = false; + await expect(coordinator.use("studio-a", attached.lease)).rejects.toThrow(); + eligible = true; + await store.transition("studio-a", 1, { + lifecycle: "ended", + execution: "paused", + }); + await expect(coordinator.use("studio-a", attached.lease)).rejects.toThrow(); + expect(await coordinator.describe("studio-a")).toMatchObject({ + lifecycle: "ended", + execution: "paused", + }); +}); + +it("checks the durable fence after host authorization finishes", async () => { + const attached = await coordinator.attach("studio-a", 0); + assertCurrent.mockImplementationOnce(async () => { + await store.transition("studio-a", 1, { + lifecycle: "ended", + execution: "paused", + }); + }); + await expect( + coordinator.use("studio-a", attached.lease), + ).rejects.toMatchObject({ failure: { code: "lifecycle_changed" } }); +}); + +it("keeps unresolved End durable across coordinator restart and global shutdown paused", async () => { + const ending = coordinator.beginEnd("studio-a"); + await ending.persistence; + await ending.native; + coordinator = fixture(); + expect(await coordinator.describe("studio-a")).toMatchObject({ + lifecycle: "ending", + execution: "paused", + }); + await expect(coordinator.attach("studio-a", 1)).rejects.toThrow(); + coordinator.beginShutdown(); + await expect(coordinator.attach("studio-b", 0)).rejects.toThrow(); + expect(ensure).not.toHaveBeenCalled(); +}); diff --git a/packages/harness/src/core/assistant-lifecycle.ts b/packages/harness/src/core/assistant-lifecycle.ts new file mode 100644 index 000000000..97d8fb70c --- /dev/null +++ b/packages/harness/src/core/assistant-lifecycle.ts @@ -0,0 +1,311 @@ +import { randomUUID } from "node:crypto"; +import type { AssistantLifecycle } from "../shared/assistant-session.js"; +import { openCodeTransportFailure } from "../shared/opencode-errors.js"; +import { + AssistantSessionRevisionError, + type AssistantSessionStore, +} from "./assistant-session-store.js"; +import { + OpenCodeTransportError, + type HostedOpenCode, + type OpenCodeHost, +} from "./opencode-host.js"; +import type { OpenCodeAssociations } from "./opencode-association.js"; + +export interface AssistantAttachment { + conversationId: string; + lease: string; + lifecycle: AssistantLifecycle; +} +interface Lease extends AssistantAttachment { + hosted: HostedOpenCode; +} +export interface AssistantEndFence { + readonly id: string; + readonly generation: number; +} +interface Options { + store: Pick; + host: Pick< + OpenCodeHost, + | "ensure" + | "current" + | "assertCurrent" + | "retireExact" + | "retireWithResult" + | "observe" + | "beginShutdown" + >; + associations: Pick; +} +const failure = ( + code: "lifecycle_changed" | "session_ended" | "execution_paused", +) => new OpenCodeTransportError(openCodeTransportFailure(code)); +const initial = (id: string): AssistantLifecycle => ({ + version: 1, + harnessSessionId: id, + revision: 0, + lifecycle: "open", + execution: "paused", + updatedAt: 0, +}); + +/** Server-owned admission. Reading or detaching a pane never obtains a runtime lease. */ +export class AssistantLifecycleCoordinator { + private readonly leases = new Map(); + private readonly states = new Map(); + private readonly generations = new Map(); + private readonly ending = new Set(); + private readonly pending = new Map< + string, + { revision: number; promise: Promise } + >(); + private readonly listeners = new Set<() => void>(); + private closed = false; + constructor(private readonly options: Options) {} + + subscribe(listener: () => void): () => void { + this.listeners.add(listener); + return () => { + this.listeners.delete(listener); + }; + } + private changed() { + for (const listener of this.listeners) { + try { + listener(); + } catch { + /* Isolate UI subscribers. */ + } + } + } + private remember(state: AssistantLifecycle): void { + const previous = this.states.get(state.harnessSessionId); + if (previous && previous.revision >= state.revision) return; + this.states.set(state.harnessSessionId, state); + this.changed(); + } + private generation(id: string): number { + return this.generations.get(id) ?? 0; + } + private check(fence: AssistantEndFence, ending = false): void { + if ( + this.closed || + this.generation(fence.id) !== fence.generation || + (!ending && this.ending.has(fence.id)) + ) + throw failure("lifecycle_changed"); + } + private view(state: AssistantLifecycle): AssistantLifecycle { + const id = state.harnessSessionId; + const lease = this.leases.get(id); + return { + ...state, + lifecycle: this.ending.has(id) ? "ending" : state.lifecycle, + execution: + state.lifecycle === "open" && + lease?.lifecycle.revision === state.revision && + !this.closed && + !this.ending.has(id) && + lease?.hosted.isCurrent() + ? lease.lifecycle.execution + : "paused", + }; + } + snapshot(): AssistantLifecycle[] { + return [...this.states.values()].map((state) => this.view(state)); + } + async describe(id: string): Promise { + const state = (await this.options.store.lifecycle(id)) ?? initial(id); + this.remember(state); + return this.view(this.states.get(id) ?? state); + } + + attach(id: string, expectedRevision: number): Promise { + if (!Number.isSafeInteger(expectedRevision) || expectedRevision < 0) + return Promise.reject(failure("lifecycle_changed")); + const pending = this.pending.get(id); + if (pending) + return pending.revision === expectedRevision + ? pending.promise + : Promise.reject(failure("lifecycle_changed")); + const fence = { id, generation: this.generation(id) }; + const promise = this.open(fence, expectedRevision).finally(() => { + if (this.pending.get(id)?.promise === promise) this.pending.delete(id); + }); + this.pending.set(id, { revision: expectedRevision, promise }); + return promise; + } + private async open( + fence: AssistantEndFence, + expectedRevision: number, + ): Promise { + const { id } = fence; + this.check(fence); + const state = await this.describe(id); + this.check(fence); + if (state.revision !== expectedRevision) throw failure("lifecycle_changed"); + if (state.lifecycle !== "open") throw failure("session_ended"); + const previous = this.leases.get(id); + if (previous?.hosted === this.options.host.current(id)) { + await this.assertRuntime(previous.hosted); + return this.publicLease(previous); + } + const existing = this.options.host.current(id); + let hosted: HostedOpenCode | undefined; + try { + hosted = await this.options.host.ensure(id); + this.check(fence); + const conversationId = await this.options.associations.ensure(hosted); + this.check(fence); + await this.options.host.assertCurrent(hosted); + this.check(fence); + // Every restarted runtime begins paused, irrespective of the last process's saved admission. + const lifecycle = await this.options.store.transition( + id, + expectedRevision, + { lifecycle: "open", execution: "paused" }, + hosted.signal, + ); + this.check(fence); + await this.options.host.assertCurrent(hosted); + this.check(fence); + const lease: Lease = { + hosted, + conversationId, + lease: randomUUID(), + lifecycle, + }; + this.leases.set(id, lease); + this.remember(lifecycle); + this.options.host.observe(hosted, conversationId); + return this.publicLease(lease); + } catch (error) { + if (hosted && hosted !== existing) + void this.options.host.retireExact(hosted).catch(() => {}); + throw error instanceof AssistantSessionRevisionError + ? failure("lifecycle_changed") + : error; + } + } + private publicLease({ + conversationId, + lease, + lifecycle, + }: Lease): AssistantAttachment { + return { conversationId, lease, lifecycle: { ...lifecycle } }; + } + + /** Exact token + exact host + durable revision; this path never calls ensure(). */ + async use( + id: string, + token: string | undefined, + execution = false, + ): Promise { + const lease = this.leases.get(id); + if (!token || lease?.lease !== token) throw failure("lifecycle_changed"); + await this.assertRuntime(lease.hosted, execution); + return lease; + } + async assertRuntime( + hosted: HostedOpenCode, + execution = false, + ): Promise { + const id = hosted.harnessSessionId; + const lease = this.leases.get(id); + const fence = { id, generation: this.generation(id) }; + const check = () => { + this.check(fence); + if ( + !lease || + this.leases.get(id) !== lease || + lease.hosted !== hosted || + !hosted.isCurrent() + ) + throw failure("lifecycle_changed"); + if (execution && lease.lifecycle.execution !== "enabled") + throw failure("execution_paused"); + }; + check(); + await this.options.host.assertCurrent(hosted); + check(); + const saved = await this.options.store.lifecycle(id); + check(); + if ( + !saved || + saved.revision !== lease!.lifecycle.revision || + saved.lifecycle !== "open" + ) + throw failure("lifecycle_changed"); + } + + /** Called inside the existing dispatch admission, after explicit user context preparation. */ + async enable(hosted: HostedOpenCode): Promise { + await this.assertRuntime(hosted); + const id = hosted.harnessSessionId; + const lease = this.leases.get(id)!; + if (lease.lifecycle.execution === "enabled") return; + const fence = { id, generation: this.generation(id) }; + const state = await this.options.store.transition( + id, + lease.lifecycle.revision, + { lifecycle: "open", execution: "enabled" }, + hosted.signal, + ); + this.check(fence); + if (this.leases.get(id) !== lease) throw failure("lifecycle_changed"); + lease.lifecycle = state; + this.remember(state); + } + + /** Fences execution and requests process termination before any persistence await. + * The End orchestration owner must coalesce callers through its complete + * Terminal/native/persistence operation, including finishEnd or failure reporting. */ + beginEnd(id: string) { + const fence = { id, generation: this.generation(id) + 1 }; + this.generations.set(id, fence.generation); + this.ending.add(id); + this.leases.delete(id); + this.pending.delete(id); + const native = this.options.host.retireWithResult(id); + const persistence = this.persistEnd(fence, "ending"); + this.changed(); + return { fence, native, persistence }; + } + async finishEnd(fence: AssistantEndFence): Promise { + const state = await this.persistEnd(fence, "ended"); + this.check(fence, true); + this.ending.delete(fence.id); + this.changed(); + return state; + } + private async persistEnd( + fence: AssistantEndFence, + lifecycle: "ending" | "ended", + ): Promise { + for (let attempt = 0; attempt < 4; attempt++) { + this.check(fence, true); + const state = await this.options.store.lifecycle(fence.id); + this.check(fence, true); + try { + const next = await this.options.store.transition( + fence.id, + state?.revision ?? 0, + { lifecycle, execution: "paused" }, + ); + this.check(fence, true); + this.remember(next); + return next; + } catch (error) { + if (!(error instanceof AssistantSessionRevisionError)) throw error; + } + } + throw failure("lifecycle_changed"); + } + beginShutdown(): void { + this.closed = true; + this.leases.clear(); + this.options.host.beginShutdown(); + this.changed(); + } +} diff --git a/packages/harness/src/core/assistant-session-store.ts b/packages/harness/src/core/assistant-session-store.ts index 997f11b1b..0490be5c7 100644 --- a/packages/harness/src/core/assistant-session-store.ts +++ b/packages/harness/src/core/assistant-session-store.ts @@ -46,6 +46,12 @@ const associationSchema = z }) .strict(); +export class AssistantSessionRevisionError extends Error { + constructor() { + super("Assistant session changed. Please retry."); + } +} + /** Lifecycle ownership is independent of eligibility; content always requires its exact binding. */ export class AssistantSessionStore { constructor(readonly stateRoot: string) {} @@ -64,15 +70,17 @@ export class AssistantSessionStore { id: string, expected: number, next: Pick, + signal?: AbortSignal, ): Promise { const directory = await assistantDirectory(this.stateRoot, id); const unlock = await new DurableFileLock( join(directory, "lifecycle.json"), ).acquire(); try { + signal?.throwIfAborted(); const current = await this.lifecycle(id); if ((current?.revision ?? 0) !== expected) - throw new Error("Assistant session changed. Please retry."); + throw new AssistantSessionRevisionError(); const updated = lifecycleSchema.parse({ version: 1, harnessSessionId: id, @@ -80,7 +88,7 @@ export class AssistantSessionStore { ...next, updatedAt: Date.now(), }); - await writeAssistantJson(directory, "lifecycle.json", updated); + await writeAssistantJson(directory, "lifecycle.json", updated, signal); return updated; } finally { await unlock(); diff --git a/packages/harness/src/core/opencode-final-response.test.ts b/packages/harness/src/core/opencode-final-response.test.ts index dd3ee4604..2eaaca815 100644 --- a/packages/harness/src/core/opencode-final-response.test.ts +++ b/packages/harness/src/core/opencode-final-response.test.ts @@ -9,6 +9,7 @@ import { assistantContextDigest, } from "./studio-assistant-context.js"; import type { OpenCodeTurnMessage } from "../shared/opencode-turn.js"; +import { DurableFileLock } from "./durable-file-lock.js"; let hosted: HostedOpenCode; let permission: unknown[]; @@ -220,6 +221,75 @@ it("holds admission while resolving context and releases it without dispatch on expect(dispatch).not.toHaveBeenCalled(); }); +it("cancels a hung old Send preparation without blocking or dispatching into its replacement", async () => { + const delivery = new OpenCodeFinalResponse(); + let release!: (value: RequestInit) => void; + const prepare = vi.fn( + () => + new Promise((done) => { + release = done; + }), + ); + const pending = delivery.send(hosted, "ses_test", prepare); + let rejected = false; + void pending.catch(() => { + rejected = true; + }); + await vi.waitFor(() => expect(prepare).toHaveBeenCalledOnce()); + abort.abort(new Error("ended")); + await vi.waitFor(() => expect(rejected).toBe(true)); + const replacement = { ...hosted, signal: new AbortController().signal }; + expect(delivery.isRunning(replacement)).toBe(false); + dispatch.mockImplementationOnce(async () => { + vi.mocked(hosted.server.fetchJson).mockResolvedValue([ + { info: { id: "msg_new", role: "user", time: {} }, parts: [] }, + ]); + return new Response(null, { status: 204 }); + }); + expect((await delivery.send(replacement, "ses_test", {})).status).toBe(204); + release({}); + await expect(pending).rejects.toThrow("ended"); + expect(dispatch).toHaveBeenCalledOnce(); +}); + +it("releases a cancelled recovery preparation lock before its provider settles", async () => { + let release!: (value: { system: string }) => void; + const prepare = vi + .fn() + .mockImplementationOnce( + () => + new Promise((done) => { + release = done; + }), + ) + .mockResolvedValue({ system }); + const delivery = new OpenCodeFinalResponse({ recoverPrompt: prepare }); + const pending = delivery.recover(hosted, "ses_test", "msg_empty"); + let rejected = false; + void pending.catch(() => { + rejected = true; + }); + await vi.waitFor(() => expect(prepare).toHaveBeenCalledOnce()); + abort.abort(new Error("ended")); + await vi.waitFor(() => expect(rejected).toBe(true)); + const releaseProbe = await new DurableFileLock( + join(hosted.stateRoot, "final-response-ses_test-msg_empty.json"), + { timeoutMs: 50 }, + ).acquire(); + await releaseProbe(); + expect(await readdir(hosted.stateRoot)).toEqual([]); + const replacement = { ...hosted, signal: new AbortController().signal }; + await delivery.recover(replacement, "ses_test", "msg_empty"); + release({ system: system! }); + await expect(pending).rejects.toThrow("ended"); + expect(dispatch).toHaveBeenCalledOnce(); + // Its later successful dispatch still owns the original durable replay fence. + await expect( + new OpenCodeFinalResponse().recover(replacement, "ses_test", "msg_empty"), + ).rejects.toThrow(); + expect(dispatch).toHaveBeenCalledOnce(); +}); + it("never dispatches for busy, answered, stale, or already recovered turns", async () => { for (const scenario of [ "busy", @@ -299,7 +369,9 @@ it("fences recovery until an already submitted user message is persisted", async acknowledge = resolve; }), ); - const checkpoint = vi.fn().mockRejectedValue(new Error("archive unavailable")); + const checkpoint = vi + .fn() + .mockRejectedValue(new Error("archive unavailable")); const recovery = new OpenCodeFinalResponse({ onPersisted: checkpoint }); const sending = recovery.send(hosted, "ses_test", { method: "POST", diff --git a/packages/harness/src/core/opencode-final-response.ts b/packages/harness/src/core/opencode-final-response.ts index 2820dac9b..3e760eeab 100644 --- a/packages/harness/src/core/opencode-final-response.ts +++ b/packages/harness/src/core/opencode-final-response.ts @@ -15,7 +15,10 @@ export interface PreparedOpenCodePrompt { readonly expectedSystem: string; } interface DeliveryOptions { - onPersisted?: (hosted: HostedOpenCode, conversationId: string) => Promise; + onPersisted?: ( + hosted: HostedOpenCode, + conversationId: string, + ) => Promise; assertCurrent?: (hosted: HostedOpenCode) => Promise; recoverPrompt?: ( hosted: HostedOpenCode, @@ -45,10 +48,10 @@ const acknowledges = ( /** One continuation from saved results; never resubmit the original prompt. */ export class OpenCodeFinalResponse { - private pending = new Map>(); - private admitting = new Set(); - private uncertain = new Set(); - private awaiting = new Map(); + private pending = new WeakMap>(); + private admitting = new WeakSet(); + private uncertain = new WeakSet(); + private awaiting = new WeakMap(); constructor(private readonly delivery: DeliveryOptions = {}) {} @@ -60,9 +63,7 @@ export class OpenCodeFinalResponse { } isRunning(hosted: HostedOpenCode): boolean { - return ( - this.pending.has(hosted.stateRoot) || this.admitting.has(hosted.stateRoot) - ); + return this.pending.has(hosted) || this.admitting.has(hosted); } async send( @@ -77,7 +78,7 @@ export class OpenCodeFinalResponse { ): Promise { if (this.isRunning(hosted)) throw new Error("Another request is being admitted"); - this.admitting.add(hosted.stateRoot); + this.admitting.add(hosted); const signal = AbortSignal.any([ hosted.signal, AbortSignal.timeout(30_000), @@ -90,7 +91,7 @@ export class OpenCodeFinalResponse { { signal: requestSignal }, ); try { - if (this.uncertain.has(hosted.stateRoot)) { + if (this.uncertain.has(hosted)) { const statuses = await hosted.server.fetchJson< Record >("/session/status", { signal }); @@ -99,15 +100,18 @@ export class OpenCodeFinalResponse { } // Reconcile earlier uncertainty before accepting another context generation. const messages = await history(); - const pending = this.awaiting.get(hosted.stateRoot); + const pending = this.awaiting.get(hosted); if (pending) { if (pending.sessionId !== sessionId || !acknowledges(messages, pending)) throw new Error("Previous request has not been reconciled"); - this.awaiting.delete(hosted.stateRoot); - this.uncertain.delete(hosted.stateRoot); + this.awaiting.delete(hosted); + this.uncertain.delete(hosted); } signal.throwIfAborted(); - const prepared = typeof init === "function" ? await init(signal) : init; + const prepared = + typeof init === "function" + ? await abortablePreparation(init(signal), signal) + : init; const request = "init" in prepared ? prepared.init : prepared; const dispatchSignal = AbortSignal.any([ signal, @@ -121,8 +125,8 @@ export class OpenCodeFinalResponse { "init" in prepared ? { sessionId, before, expectedSystem: prepared.expectedSystem } : undefined; - if (acknowledgement) this.awaiting.set(hosted.stateRoot, acknowledgement); - this.uncertain.add(hosted.stateRoot); + if (acknowledgement) this.awaiting.set(hosted, acknowledgement); + this.uncertain.add(hosted); const response = await hosted.server.fetch( `/session/${sessionId}/prompt_async`, { ...request, signal: dispatchSignal }, @@ -132,8 +136,8 @@ export class OpenCodeFinalResponse { // and missing sessions before forking prompt work. Other HTTP failures // retain uncertainty until native history proves exact acknowledgement. if ([400, 401, 404].includes(response.status)) { - this.awaiting.delete(hosted.stateRoot); - this.uncertain.delete(hosted.stateRoot); + this.awaiting.delete(hosted); + this.uncertain.delete(hosted); } return response; } @@ -152,13 +156,13 @@ export class OpenCodeFinalResponse { if (!acknowledged) await delay(25, undefined, { signal: dispatchSignal }); } - this.awaiting.delete(hosted.stateRoot); - this.uncertain.delete(hosted.stateRoot); + this.awaiting.delete(hosted); + this.uncertain.delete(hosted); // Archive after actual native acknowledgement; storage failure cannot replay a prompt. await this.delivery.onPersisted?.(hosted, sessionId).catch(() => {}); return response; } finally { - this.admitting.delete(hosted.stateRoot); + this.admitting.delete(hosted); } } @@ -167,19 +171,16 @@ export class OpenCodeFinalResponse { sessionId: string, messageId: string, ): Promise { - if ( - this.admitting.has(hosted.stateRoot) || - this.uncertain.has(hosted.stateRoot) - ) + if (this.admitting.has(hosted) || this.uncertain.has(hosted)) return Promise.reject( new Error("A user request has not been reconciled"), ); - const previous = this.pending.get(hosted.stateRoot); + const previous = this.pending.get(hosted); if (previous) return previous; const request = this.finish(hosted, sessionId, messageId).finally(() => { - this.pending.delete(hosted.stateRoot); + this.pending.delete(hosted); }); - this.pending.set(hosted.stateRoot, request); + this.pending.set(hosted, request); return request; } @@ -252,10 +253,13 @@ export class OpenCodeFinalResponse { }; const original = await readOriginal(); const prompt = this.delivery.recoverPrompt - ? await this.delivery.recoverPrompt( - hosted, - sessionId, - original?.info?.system, + ? await abortablePreparation( + this.delivery.recoverPrompt( + hosted, + sessionId, + original?.info?.system, + signal, + ), signal, ) : recoverAssistantPrompt(original?.info?.system); @@ -309,7 +313,7 @@ export class OpenCodeFinalResponse { if (dispatched && !hosted.signal.aborted) { // Cancelling the HTTP waiter does not cancel OpenCode's native fiber. // Fence new prompts until abort is confirmed, even on transport failure. - this.uncertain.add(hosted.stateRoot); + this.uncertain.add(hosted); try { const signal = AbortSignal.any([ hosted.signal, @@ -323,7 +327,7 @@ export class OpenCodeFinalResponse { Record >("/session/status", { signal }); if (!statuses[sessionId] || statuses[sessionId].type === "idle") - this.uncertain.delete(hosted.stateRoot); + this.uncertain.delete(hosted); } catch { /* Keep the fence until native state can be reconciled. */ } @@ -334,3 +338,29 @@ export class OpenCodeFinalResponse { } } } + +/** Only provider preparation is raced. A late result cannot resume dispatch or + * touch its recovery fence; publication and cleanup remain in their original owner. */ +function abortablePreparation( + work: Promise, + signal: AbortSignal, +): Promise { + return new Promise((resolve, reject) => { + const cancel = () => reject(signal.reason); + signal.addEventListener("abort", cancel, { once: true }); + work.then( + (value) => { + signal.removeEventListener("abort", cancel); + resolve(value); + }, + (error) => { + signal.removeEventListener("abort", cancel); + reject(error); + }, + ); + if (signal.aborted) { + signal.removeEventListener("abort", cancel); + cancel(); + } + }); +} diff --git a/packages/harness/src/shared/opencode-errors.ts b/packages/harness/src/shared/opencode-errors.ts index ac5a24670..13e9189d1 100644 --- a/packages/harness/src/shared/opencode-errors.ts +++ b/packages/harness/src/shared/opencode-errors.ts @@ -7,6 +7,10 @@ export const openCodeTransportErrorCodes = [ "native_history_missing", "context_unavailable", "transport_unavailable", + "lifecycle_changed", + "session_ended", + "execution_paused", + "cleanup_unconfirmed", ] as const; export type OpenCodeTransportErrorCode = (typeof openCodeTransportErrorCodes)[number]; @@ -45,6 +49,32 @@ export interface OpenCodeStudioErrorEvent { } const fixedFailures = { + lifecycle_changed: { + code: "lifecycle_changed", + message: + "This Studio session changed. Reopen Assistant before sending another message.", + retryable: true, + action: "reconnect", + }, + session_ended: { + code: "session_ended", + message: "This session has ended. Resume it from its saved history.", + retryable: false, + action: "open_terminal", + }, + execution_paused: { + code: "execution_paused", + message: "Assistant is paused. Send a new message to continue.", + retryable: false, + action: "reconnect", + }, + cleanup_unconfirmed: { + code: "cleanup_unconfirmed", + message: + "Studio could not confirm that all session processes stopped. Try End again.", + retryable: true, + action: "open_terminal", + }, access_denied: { code: "access_denied", message: "Assistant access is not available for this account.",