From dde5d8e12b9ab71bffae1cc4267ef08c7fcd0bf1 Mon Sep 17 00:00:00 2001 From: Yash Date: Mon, 14 Sep 2026 21:37:17 +0000 Subject: [PATCH] feat(harness): verify one paused native continuation seed --- .changeset/verified-assistant-seed.md | 5 + .../harness/docs/assistant-continuation.md | 7 + .../assistant-continuation-native.test.ts | 537 ++++++++++++++++++ .../src/core/assistant-continuation-native.ts | 347 +++++++++++ 4 files changed, 896 insertions(+) create mode 100644 .changeset/verified-assistant-seed.md create mode 100644 packages/harness/src/core/assistant-continuation-native.test.ts create mode 100644 packages/harness/src/core/assistant-continuation-native.ts diff --git a/.changeset/verified-assistant-seed.md b/.changeset/verified-assistant-seed.md new file mode 100644 index 000000000..817697172 --- /dev/null +++ b/.changeset/verified-assistant-seed.md @@ -0,0 +1,5 @@ +--- +"@sapiom/harness": patch +--- + +Verify one paused native Assistant continuation seed across interrupted creation and retry. diff --git a/packages/harness/docs/assistant-continuation.md b/packages/harness/docs/assistant-continuation.md index 40a372f45..cb0e5aebf 100644 --- a/packages/harness/docs/assistant-continuation.md +++ b/packages/harness/docs/assistant-continuation.md @@ -37,3 +37,10 @@ End preempts preparation; ordinary attachment follows verified preparation. If a previous acceptance already committed, a retry validates its original manifest and material before any writes. Pending receipt input cannot repair lost committed material, including after a lost acceptance acknowledgement. + +Native preparation writes intent before creation or seeding. A retry reconciles +the exact operation marker and native identity, then verifies one synthetic +no-reply message against both native read APIs, including its accepted system, +receipt IDs, text/hash and flags. Missing or ambiguous outcomes remain uncertain; +stable native message IDs are mutable and are never blindly resubmitted. +Preparing or reopening the child does not invoke a model or replay tools. diff --git a/packages/harness/src/core/assistant-continuation-native.test.ts b/packages/harness/src/core/assistant-continuation-native.test.ts new file mode 100644 index 000000000..2e5dc59a5 --- /dev/null +++ b/packages/harness/src/core/assistant-continuation-native.test.ts @@ -0,0 +1,537 @@ +import { + assistantContentHash, + serializeAcceptedAssistantSystem, + studioAssistantCompletionSystem, +} from "@sapiom/opencode"; +import { describe, expect, it, vi } from "vitest"; +import { + AssistantContinuationNative, + AssistantContinuationUnconfirmedError, +} from "./assistant-continuation-native.js"; +import type { AssistantContinuationReceipt } from "./assistant-continuation-store.js"; +import type { HostedOpenCode } from "./opencode-host.js"; +import { sourceContext } from "./test-fixtures/assistant-context.js"; +import { + acceptedAssistantRecord, + createAssistantContextCandidate, +} from "./assistant-sources.js"; + +const token = "11111111-1111-4111-8111-111111111111"; +function seedSystem({ + id = "child-studio", + cwd = "/work", + scope = "c".repeat(64), + conversationId = "ses_saved", + attemptToken = token, + brief = "Saved public context. Wait for the next message.", +} = {}) { + const context = sourceContext(); + Object.assign(context.session, { id, cwd }); + context.guidance.push({ + id: "continuation", + kind: "continuation", + source: "studio:record", + required: true, + status: "available", + revision: null, + text: brief, + }); + const candidate = createAssistantContextCandidate(context, scope); + const { + context: facts, + instructionSet, + ...ref + } = acceptedAssistantRecord(candidate, scope, conversationId, token); + const sources = instructionSet.sources.filter( + (source) => source.status === "available", + ); + const content = (source: (typeof sources)[number]) => ({ + sourceId: source.id, + text: Buffer.from( + candidate.materials.find((item) => item.sourceId === source.id)!.bytes, + ).toString("utf8"), + }); + return { + ref, + system: serializeAcceptedAssistantSystem( + studioAssistantCompletionSystem(attemptToken), + { + schemaVersion: 2, + accepted: ref, + attemptToken, + context: facts, + stable: { + policy: content(sources.find((source) => source.kind === "policy")!), + guidance: sources + .filter( + (source) => source.kind !== "policy" && source.format === "utf8", + ) + .map(content), + manifests: sources + .filter((source) => source.format === "json") + .map(content), + sourceManifest: instructionSet, + }, + }, + ), + }; +} + +function fixture() { + const controller = new AbortController(), + hostController = new AbortController(); + let receipt = { + childStudioId: "child-studio", + operationId: "operation", + nativeCreationMarker: "continuation-operation-child", + phase: "allocated", + brief: { + text: "Saved public context. Wait for the next message.", + sha256: "", + }, + seedMessageId: "msg_seed", + seedPartId: "prt_seed", + childBinding: null, + acceptedRef: null, + acceptanceId: token, + attemptToken: token, + } as unknown as AssistantContinuationReceipt; + receipt.brief.sha256 = assistantContentHash(receipt.brief.text); + const acceptedSeed = seedSystem(); + const sessions: Record[] = [], + messages: Record[] = []; + const posts: { path: string; body: Record }[] = []; + let loseCreate = false, + loseSeed = false; + const fetch = vi.fn( + async (path: string, init?: RequestInit): Promise => { + if (init?.method === "POST") { + const body = JSON.parse(init.body as string) as Record; + posts.push({ path, body }); + if (path === "/session") { + sessions.push({ ...body, id: "ses_saved", directory: "/work" }); + if (loseCreate) throw new Error("lost create response"); + return Response.json(sessions[0]); + } + const row = { + info: { + id: body.messageID, + sessionID: "ses_saved", + role: "user", + system: body.system, + }, + parts: (body.parts as Record[]).map((part) => ({ + ...part, + messageID: body.messageID, + sessionID: "ses_saved", + })), + }; + messages.push(row); + if (loseSeed) throw new Error("lost seed response"); + return Response.json(row); // Native synchronous noReply returns a USER, not SDK's assistant type. + } + if (path.startsWith("/session?")) return Response.json(sessions); + if (path === "/session/ses_saved") + return Response.json(sessions[0] ?? {}, { + status: sessions.length ? 200 : 404, + }); + if (path.endsWith("/message/msg_seed")) + return Response.json(messages[0] ?? {}, { + status: messages.length ? 200 : 404, + }); + if (path.endsWith("/message")) return Response.json(messages); + throw new Error(`unexpected native path ${path}`); + }, + ); + const hosted = { + harnessSessionId: "child-studio", + cwd: "/work", + contextAuthorityScope: "c".repeat(64), + signal: hostController.signal, + model: { providerID: "sapiom", modelID: "test" }, + server: { fetch }, + } as unknown as HostedOpenCode; + const current = vi.fn(async () => {}); + const native = new AssistantContinuationNative(current); + const advance = vi.fn(async (patch) => (receipt = { ...receipt, ...patch })); + const conversation = () => + native.conversation(hosted, receipt, advance, controller.signal); + const seed = () => + native.seed( + hosted, + receipt, + acceptedSeed.system, + advance, + controller.signal, + ); + const associate = () => { + receipt = { + ...receipt, + phase: "accepting", + childBinding: { + harnessSessionId: hosted.harnessSessionId, + cwd: hosted.cwd, + contextAuthorityScope: hosted.contextAuthorityScope, + conversationId: "ses_saved", + }, + acceptedRef: acceptedSeed.ref, + }; + }; + return { + native, + hosted, + current, + controller, + hostController, + sessions, + messages, + posts, + fetch, + advance, + conversation, + seed, + system: acceptedSeed.system, + associate, + get receipt() { + return receipt; + }, + setReceipt(value: Partial) { + receipt = { ...receipt, ...value }; + }, + loseCreate() { + loseCreate = true; + }, + loseSeed() { + loseSeed = true; + }, + }; +} + +describe("trusted Assistant continuation native protocol", () => { + it("persists intent before one create and reconciles the same marked conversation after lost response/restart", async () => { + const f = fixture(); + f.loseCreate(); + expect(await f.conversation()).toBe("ses_saved"); + expect(f.advance).toHaveBeenCalledWith({ phase: "creating" }); + expect(f.advance.mock.invocationCallOrder[0]).toBeLessThan( + f.fetch.mock.invocationCallOrder[0]!, + ); + expect( + await new AssistantContinuationNative(f.current).conversation( + f.hosted, + f.receipt, + f.advance, + f.controller.signal, + ), + ).toBe("ses_saved"); + expect(f.posts).toHaveLength(1); + }); + it("does not create after a crash between durable intent and the original request", async () => { + const f = fixture(); + f.setReceipt({ phase: "creating" }); + await expect(f.conversation()).rejects.toBeInstanceOf( + AssistantContinuationUnconfirmedError, + ); + expect(f.posts).toHaveLength(0); + }); + it("rejects absent or ambiguous markers and truncated discovery", async () => { + for (const count of [0, 2, 100]) { + const f = fixture(); + await f.conversation(); + const original = f.sessions[0]!; + f.sessions.splice( + 0, + f.sessions.length, + ...Array.from({ length: count }, () => ({ ...original })), + ); + await expect(f.conversation()).rejects.toBeInstanceOf( + AssistantContinuationUnconfirmedError, + ); + expect(f.posts).toHaveLength(1); + } + }); + it("requires both operation metadata and title, then exact native workspace", async () => { + for (const patch of [ + { title: "different" }, + { metadata: {} }, + { directory: "/foreign" }, + { id: "child-studio" }, + ]) { + const f = fixture(); + await f.conversation(); + Object.assign(f.sessions[0]!, patch); + await expect(f.conversation()).rejects.toBeInstanceOf( + AssistantContinuationUnconfirmedError, + ); + expect(f.posts).toHaveLength(1); + } + }); + it("prepares exactly one synthetic no-reply message, verifies USER readback, and never dispatches work", async () => { + const f = fixture(); + await f.conversation(); + f.associate(); + f.loseSeed(); + await f.seed(); + const post = f.posts[1]!; + expect(post.path).toBe("/session/ses_saved/message"); + expect(post.body).toMatchObject({ + noReply: true, + system: f.system, + messageID: "msg_seed", + agent: "build", + model: { providerID: "sapiom", modelID: "test" }, + parts: [ + { + id: "prt_seed", + synthetic: true, + ignored: false, + text: f.receipt.brief.text, + }, + ], + }); + expect(post.body).not.toHaveProperty("tools"); + await new AssistantContinuationNative(f.current).seed( + f.hosted, + f.receipt, + f.system, + f.advance, + f.controller.signal, + ); + expect(f.posts).toHaveLength(2); // one conversation + one seed, including lost-response retry + expect( + f.fetch.mock.calls.some(([path]) => + /prompt_async|\/recover|\/tool/.test(path), + ), + ).toBe(false); + }); + it("never resends when persisted seed intent has no verified native message", async () => { + const f = fixture(); + await f.conversation(); + f.associate(); + f.setReceipt({ phase: "seeding" }); + await expect(f.seed()).rejects.toBeInstanceOf( + AssistantContinuationUnconfirmedError, + ); + expect(f.posts).toHaveLength(1); + }); + it("does not overwrite changed seed bytes, flags, identities, or system", async () => { + const changes = [ + (row: any) => { + row.parts[0].text = "changed"; + }, + (row: any) => { + row.parts[0].synthetic = false; + }, + (row: any) => { + row.parts[0].ignored = true; + }, + (row: any) => { + row.parts[0].metadata.sapiomContinuation.briefHash = "other"; + }, + (row: any) => { + row.parts[0].messageID = "msg_other"; + }, + (row: any) => { + row.info.system = "other accepted context"; + }, + (row: any) => { + row.info.role = "assistant"; + }, + ]; + for (const change of changes) { + const f = fixture(); + await f.conversation(); + f.associate(); + await f.seed(); + change(f.messages[0]); + await expect(f.seed()).rejects.toBeInstanceOf( + AssistantContinuationUnconfirmedError, + ); + expect(f.posts).toHaveLength(2); + } + }); + it("rejects duplicate operation seeds, including a different native message ID", async () => { + const f = fixture(); + await f.conversation(); + f.associate(); + await f.seed(); + f.messages.push(JSON.parse(JSON.stringify(f.messages[0]))); + await expect(f.seed()).rejects.toBeInstanceOf( + AssistantContinuationUnconfirmedError, + ); + expect(f.posts).toHaveLength(2); + }); + it("rejects foreign child identity before native IO", async () => { + const f = fixture(); + f.associate(); + Object.assign(f.hosted, { contextAuthorityScope: "d".repeat(64) }); + await expect(f.seed()).rejects.toBeInstanceOf( + AssistantContinuationUnconfirmedError, + ); + expect(f.fetch).not.toHaveBeenCalled(); + }); + it("End after durable creation intent prevents the native POST", async () => { + const f = fixture(); + f.advance.mockImplementationOnce(async (patch) => { + f.setReceipt(patch); + f.hostController.abort(new Error("ended")); + return f.receipt; + }); + await expect(f.conversation()).rejects.toThrow("ended"); + expect(f.receipt.phase).toBe("creating"); + expect(f.posts).toHaveLength(0); + }); + it("cancels a hung native request when the exact host ends", async () => { + const f = fixture(); + f.setReceipt({ phase: "creating" }); + f.fetch.mockImplementationOnce(async () => new Promise(() => {})); + const request = f.conversation(); + const rejected = expect(request).rejects.toThrow("ended"); + await vi.waitFor(() => expect(f.fetch).toHaveBeenCalled()); + f.hostController.abort(new Error("ended")); + await rejected; + expect(f.posts).toHaveLength(0); + }); + it("rejects non-receipt context, parent facts, changed brief and attempt before native IO", async () => { + for (const change of [ + { id: "parent-studio" }, + { cwd: "/foreign" }, + { scope: "d".repeat(64) }, + { conversationId: "ses_parent" }, + { attemptToken: "22222222-2222-4222-8222-222222222222" }, + { brief: "Changed continuation" }, + ]) { + const f = fixture(); + f.associate(); + const wrong = seedSystem(change); + f.setReceipt({ acceptedRef: wrong.ref }); + await expect( + f.native.seed( + f.hosted, + f.receipt, + wrong.system, + f.advance, + f.controller.signal, + ), + ).rejects.toBeInstanceOf(AssistantContinuationUnconfirmedError); + expect(f.fetch).not.toHaveBeenCalled(); + expect(f.advance).not.toHaveBeenCalled(); + } + const f = fixture(); + f.associate(); + for (const system of [ + "", + "generic system", + f.system.replace( + "StudioAssistantContext/v2", + "StudioAssistantContext/v3", + ), + ]) { + await expect( + f.native.verifySeed(f.hosted, f.receipt, system, f.controller.signal), + ).rejects.toBeInstanceOf(AssistantContinuationUnconfirmedError); + } + f.setReceipt({ + acceptedRef: { ...f.receipt.acceptedRef!, revision: "f".repeat(64) }, + }); + await expect(f.seed()).rejects.toBeInstanceOf( + AssistantContinuationUnconfirmedError, + ); + expect(f.fetch).not.toHaveBeenCalled(); + }); + it("End settles a held authority check before either native mutation", async () => { + for (const kind of ["create", "seed"] as const) { + const f = fixture(); + if (kind === "seed") f.associate(); + f.current.mockImplementationOnce(async () => new Promise(() => {})); + let result: unknown; + const request = (kind === "create" ? f.conversation() : f.seed()).catch( + (error) => { + result = error; + }, + ); + await vi.waitFor(() => expect(f.current).toHaveBeenCalled()); + f.hostController.abort(new Error("ended during authority")); + await vi.waitFor(() => + expect(result).toMatchObject({ message: "ended during authority" }), + ); + await request; + expect(f.fetch).not.toHaveBeenCalled(); + expect(f.advance).not.toHaveBeenCalled(); + } + }); + it("cancels a stalled response body on either caller or runtime End", async () => { + for (const owner of ["controller", "hostController"] as const) { + const f = fixture(), + cancel = vi.fn(), + pull = vi.fn(); + f.setReceipt({ phase: "creating" }); + f.fetch.mockResolvedValueOnce( + new Response(new ReadableStream({ pull, cancel })), + ); + const request = f.conversation(); + const rejected = expect(request).rejects.toThrow("ended stream"); + await vi.waitFor(() => expect(pull).toHaveBeenCalled()); + f[owner].abort(new Error("ended stream")); + await rejected; + expect(cancel).toHaveBeenCalled(); + expect(f.posts).toHaveLength(0); + } + }); + it("discards a late HTTP response after cancellation even if fetch ignored the signal", async () => { + const f = fixture(), + cancel = vi.fn(); + let resolve!: (response: Response) => void; + f.setReceipt({ phase: "creating" }); + f.fetch.mockImplementationOnce( + () => + new Promise((done) => { + resolve = done; + }), + ); + const request = f.conversation(); + const rejected = expect(request).rejects.toThrow("ended late"); + await vi.waitFor(() => expect(f.fetch).toHaveBeenCalled()); + f.hostController.abort(new Error("ended late")); + await rejected; + resolve(new Response(new ReadableStream({ cancel }))); + await vi.waitFor(() => expect(cancel).toHaveBeenCalled()); + }); + it("rejects malformed and oversized discovery bodies without creating again", async () => { + for (const body of ["{broken", new Uint8Array(16 * 1024 * 1024 + 1)]) { + const f = fixture(); + f.setReceipt({ phase: "creating" }); + f.fetch.mockResolvedValueOnce(new Response(body)); + await expect(f.conversation()).rejects.toBeInstanceOf( + AssistantContinuationUnconfirmedError, + ); + expect(f.posts).toHaveLength(0); + } + }); + it("requires exact seed identity and content in history as well as direct readback", async () => { + for (const change of [ + (row: any) => { + row.info.id = "msg_other"; + }, + (row: any) => { + row.info.role = "assistant"; + }, + (row: any) => { + row.parts[0].text = "Changed in later history read"; + }, + ]) { + const f = fixture(); + await f.conversation(); + f.associate(); + await f.seed(); + const history = JSON.parse(JSON.stringify(f.messages)); + change(history[0]); + f.fetch.mockResolvedValueOnce(Response.json(f.messages[0])); + f.fetch.mockResolvedValueOnce(Response.json(history)); + await expect(f.seed()).rejects.toBeInstanceOf( + AssistantContinuationUnconfirmedError, + ); + expect(f.posts).toHaveLength(2); + } + }); +}); diff --git a/packages/harness/src/core/assistant-continuation-native.ts b/packages/harness/src/core/assistant-continuation-native.ts new file mode 100644 index 000000000..d42166c87 --- /dev/null +++ b/packages/harness/src/core/assistant-continuation-native.ts @@ -0,0 +1,347 @@ +import { + assistantContentHash, + encodeAssistantContext, + parseStudioAssistantSystem, +} from "@sapiom/opencode"; +import { isConversationId } from "../shared/assistant-state.js"; +import { awaitAssistantInspection } from "./assistant-lifecycle.js"; +import type { + AssistantContinuationPatch, + AssistantContinuationReceipt, +} from "./assistant-continuation-store.js"; +import type { HostedOpenCode } from "./opencode-host.js"; + +/** Native mutations may have committed even when their HTTP response was lost. */ +export class AssistantContinuationUnconfirmedError extends Error { + readonly code = "CONTINUATION_UNCONFIRMED"; + constructor() { + super( + "Studio could not verify the prepared continuation. Retry this operation to check its saved result.", + ); + } +} +const uncertain = () => new AssistantContinuationUnconfirmedError(); +type Row = Record; +const object = (value: unknown): Row => { + if (!value || typeof value !== "object" || Array.isArray(value)) + throw uncertain(); + return value as Row; +}; +type Advance = ( + patch: AssistantContinuationPatch, +) => Promise; + +/** Trusted no-reply protocol. Stable native IDs are mutable, so retries only read. */ +export class AssistantContinuationNative { + constructor( + private readonly assertCurrent: (hosted: HostedOpenCode) => Promise, + ) {} + + private async current(hosted: HostedOpenCode, signal: AbortSignal) { + signal = AbortSignal.any([signal, hosted.signal]); + signal.throwIfAborted(); + hosted.signal.throwIfAborted(); + await awaitAssistantInspection(this.assertCurrent(hosted), signal); + signal.throwIfAborted(); + hosted.signal.throwIfAborted(); + } + + private async request( + hosted: HostedOpenCode, + path: string, + signal: AbortSignal, + body?: unknown, + ): Promise { + signal = AbortSignal.any([signal, hosted.signal]); + await this.current(hosted, signal); + const pending = hosted.server + .fetch(path, { + signal, + ...(body === undefined + ? { method: "GET" } + : { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify(body), + }), + }) + .then((response) => { + if (signal.aborted) { + void response.body?.cancel().catch(() => {}); + signal.throwIfAborted(); + } + return response; + }); + const response = await awaitAssistantInspection(pending, signal); + if (!response.ok || !response.body) { + void response.body?.cancel().catch(() => {}); + throw uncertain(); + } + const reader = response.body.getReader(), + chunks: Uint8Array[] = []; + let size = 0; + try { + for (;;) { + const { done, value } = await awaitAssistantInspection( + reader.read(), + signal, + ); + if (done) break; + size += value.byteLength; + if (size > 16 * 1024 * 1024) throw uncertain(); + chunks.push(value); + } + await this.current(hosted, signal); + try { + return JSON.parse(Buffer.concat(chunks).toString("utf8")) as unknown; + } catch { + throw uncertain(); + } + } finally { + void reader.cancel().catch(() => {}); + reader.releaseLock(); + } + } + + private marker( + value: unknown, + receipt: AssistantContinuationReceipt, + ): boolean { + if (!value || typeof value !== "object" || Array.isArray(value)) + return false; + const row = value as Row; + if (row.title !== receipt.nativeCreationMarker) return false; + const metadata = row.metadata as Row | undefined; + const continuation = metadata?.sapiomContinuation as Row | undefined; + return continuation?.operationId === receipt.operationId; + } + + async conversation( + hosted: HostedOpenCode, + input: AssistantContinuationReceipt, + advance: Advance, + signal: AbortSignal, + ): Promise { + let receipt = input; + if ( + hosted.harnessSessionId !== receipt.childStudioId || + !["allocated", "creating"].includes(receipt.phase) + ) + throw uncertain(); + if (receipt.phase === "allocated") { + await this.current(hosted, signal); + receipt = await advance({ phase: "creating" }); + await this.current(hosted, signal); + try { + await this.request(hosted, "/session", signal, { + title: receipt.nativeCreationMarker, + metadata: { + sapiomContinuation: { operationId: receipt.operationId }, + }, + }); + } catch { + // Inspect even after a lost HTTP acknowledgement. Never send creation twice. + await this.current(hosted, signal); + } + } + const rows = await this.request( + hosted, + `/session?search=${encodeURIComponent(receipt.nativeCreationMarker)}&limit=100`, + signal, + ); + if (!Array.isArray(rows) || rows.length >= 100) throw uncertain(); + const matches = rows.filter((row) => this.marker(row, receipt)); + if (matches.length !== 1) throw uncertain(); + const match = object(matches[0]); + if (!isConversationId(match.id) || match.id === hosted.harnessSessionId) + throw uncertain(); + const saved = object( + await this.request(hosted, `/session/${match.id}`, signal), + ); + if ( + saved.id !== match.id || + saved.directory !== hosted.cwd || + !this.marker(saved, receipt) + ) + throw uncertain(); + return match.id; + } + + private child( + hosted: HostedOpenCode, + receipt: AssistantContinuationReceipt, + ): string { + const binding = receipt.childBinding; + if ( + !binding || + binding.harnessSessionId !== hosted.harnessSessionId || + binding.harnessSessionId !== receipt.childStudioId || + !isConversationId(binding.conversationId) || + binding.cwd !== hosted.cwd || + binding.contextAuthorityScope !== hosted.contextAuthorityScope + ) + throw uncertain(); + return binding.conversationId; + } + + private acceptedSystem( + hosted: HostedOpenCode, + receipt: AssistantContinuationReceipt, + system: string, + ): void { + try { + const parsed = parseStudioAssistantSystem(system, { + conversationId: this.child(hosted, receipt), + authorityScope: hosted.contextAuthorityScope, + attemptToken: receipt.attemptToken, + }); + if ( + parsed.kind !== "accepted-v2" || + !receipt.acceptedRef || + receipt.acceptedRef.acceptanceId !== receipt.acceptanceId || + encodeAssistantContext(parsed.wire.accepted) !== + encodeAssistantContext(receipt.acceptedRef) || + parsed.wire.context.session.id !== receipt.childStudioId || + parsed.wire.context.session.cwd !== hosted.cwd + ) + throw uncertain(); + const sources = parsed.wire.stable.sourceManifest.sources.filter( + (source) => + source.kind === "continuation" && source.status === "available", + ); + if ( + sources.length !== 1 || + sources[0]!.contentHash !== receipt.brief.sha256 + ) + throw uncertain(); + } catch { + throw uncertain(); + } + } + + async seed( + hosted: HostedOpenCode, + input: AssistantContinuationReceipt, + system: string, + advance: Advance, + signal: AbortSignal, + ): Promise { + let receipt = input; + const conversationId = this.child(hosted, receipt); + this.acceptedSystem(hosted, receipt, system); + if ( + !receipt.acceptedRef || + !system || + !["accepting", "seeding", "prepared"].includes(receipt.phase) + ) + throw uncertain(); + if (receipt.phase === "accepting") { + await this.current(hosted, signal); + receipt = await advance({ phase: "seeding" }); + await this.current(hosted, signal); + try { + await this.request( + hosted, + `/session/${conversationId}/message`, + signal, + { + messageID: receipt.seedMessageId, + model: { ...hosted.model }, + agent: "build", + noReply: true, + system, + parts: [ + { + id: receipt.seedPartId, + type: "text", + text: receipt.brief.text, + synthetic: true, + ignored: false, + metadata: { + sapiomContinuation: { + operationId: receipt.operationId, + briefHash: receipt.brief.sha256, + }, + }, + }, + ], + }, + ); + } catch { + await this.current(hosted, signal); + } + } + await this.verifySeed(hosted, receipt, system, signal); + } + + /** The exact proof used for prepared status and server-owned seed attestation. */ + async verifySeed( + hosted: HostedOpenCode, + receipt: AssistantContinuationReceipt, + system: string, + signal: AbortSignal, + ): Promise { + const conversationId = this.child(hosted, receipt); + this.acceptedSystem(hosted, receipt, system); + const row = object( + await this.request( + hosted, + `/session/${conversationId}/message/${receipt.seedMessageId}`, + signal, + ), + ); + const info = object(row.info); + if ( + info.id !== receipt.seedMessageId || + info.sessionID !== conversationId || + info.role !== "user" || + info.system !== system || + !Array.isArray(row.parts) || + row.parts.length !== 1 + ) + throw uncertain(); + const part = object(row.parts[0]); + const marker = object(object(part.metadata).sapiomContinuation); + if ( + part.id !== receipt.seedPartId || + part.messageID !== info.id || + part.sessionID !== conversationId || + part.type !== "text" || + part.synthetic !== true || + part.ignored !== false || + part.text !== receipt.brief.text || + assistantContentHash(part.text as string) !== receipt.brief.sha256 || + marker.operationId !== receipt.operationId || + marker.briefHash !== receipt.brief.sha256 + ) + throw uncertain(); + const history = await this.request( + hosted, + `/session/${conversationId}/message`, + signal, + ); + if (!Array.isArray(history) || history.length > 10_000) throw uncertain(); + const seeds = history.flatMap((message) => { + const value = object(message); + if (!Array.isArray(value.parts)) throw uncertain(); + return value.parts.flatMap((raw) => { + const p = object(raw), + metadata = p.metadata as Row | undefined; + return (metadata?.sapiomContinuation as Row | undefined) + ?.operationId === receipt.operationId + ? [{ info: object(value.info), part: p }] + : []; + }); + }); + if ( + seeds.length !== 1 || + seeds[0]!.info.id !== receipt.seedMessageId || + seeds[0]!.info.sessionID !== conversationId || + seeds[0]!.info.role !== "user" || + seeds[0]!.info.system !== system || + encodeAssistantContext(seeds[0]!.part) !== encodeAssistantContext(part) + ) + throw uncertain(); + await this.current(hosted, signal); + } +}