diff --git a/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts index a62977eb17fb2..105565f79ef2f 100644 --- a/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts @@ -7292,13 +7292,9 @@ describe("AcpAdapterV2", () => { const event = yield* Queue.take(events); if (event.type === "turn.terminal" && event.providerTurnId === firstProviderTurnId) break; } - const snapshotBeforeRollback = yield* runtime.readThreadSnapshot({ providerThread }); - // Turns reach orchestration as events; the session keeps no record of them. - assert.deepEqual(snapshotBeforeRollback.providerTurns, []); - const rolledBack = yield* runtime.rollbackThread({ providerThread: { ...providerThread, appThreadId: rollbackThreadId }, - providerThreadTurns: snapshotBeforeRollback.providerTurns, + providerThreadTurns: [], target: { type: "thread_start", checkpointId: CheckpointId.make("checkpoint-acp-rollback-session"), @@ -7306,7 +7302,6 @@ describe("AcpAdapterV2", () => { }, }); assert.isString(rolledBack.providerThread.nativeThreadRef?.nativeId); - assert.deepEqual(rolledBack.providerTurns, []); assert.equal( runtimeInputs[1]?.processEnvironment?.T3_ACP_MCP_AUTHORIZATION, "Bearer rollback-target-token", @@ -7330,7 +7325,6 @@ describe("AcpAdapterV2", () => { const snapshotAfterRollback = yield* runtime.readThreadSnapshot({ providerThread: rolledBack.providerThread, }); - assert.deepEqual(snapshotAfterRollback.providerTurns, []); assert.deepEqual(snapshotAfterRollback.messages, []); const sessionEventsBeforeSecondTurn = sessionEventCount; diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts index c93b840f44e4d..d3c017a54679a 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts @@ -15145,7 +15145,6 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( }), handoffIds: rollbackInput.providerThread.handoffIds, }, - providerTurns: [], messages: [], runtimeRequests: [], }; @@ -15167,7 +15166,6 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( lastRunOrdinal: rollbackInput.target.appRunOrdinal, updatedAt: now, }, - providerTurns: [], messages: [], runtimeRequests: [], }; diff --git a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts index 1c38aaebffc80..a35767f62eecf 100644 --- a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts @@ -9179,7 +9179,6 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* ( nativeConversationHeadRef: threadInput.providerThread.nativeConversationHeadRef, updatedAt: codexTimestamp(response.thread.updatedAt), }, - providerTurns: [], messages: [], runtimeRequests: [], providerPayload: response.thread, @@ -9212,7 +9211,6 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* ( nativeConversationHeadRef, status: "idle" as const, }, - providerTurns: [], messages: [], runtimeRequests: [], }; @@ -9255,7 +9253,6 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* ( status: "idle" as const, updatedAt: codexTimestamp(response.thread.updatedAt), }, - providerTurns: [], messages: [], runtimeRequests: [], providerPayload: response.thread, diff --git a/apps/server/src/orchestration-v2/Adapters/OpenCode2AdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/OpenCode2AdapterV2.test.ts index 0e4bc5d088048..91a00fd60742f 100644 --- a/apps/server/src/orchestration-v2/Adapters/OpenCode2AdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/OpenCode2AdapterV2.test.ts @@ -6873,7 +6873,7 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => { }), ); - it.effect("drops the turns a rollback cut from the snapshot it returns", () => + it.effect("reverts OpenCode at the prompt of the first turn a rollback cuts", () => Effect.gen(function* () { const kept = `msg_t3_turn_${SESSION}:attempt:opencode2-adapter`; const dropped = `msg_t3_turn_${SESSION}:attempt:opencode2-adapter:2`; @@ -6925,7 +6925,7 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => { yield* runtime.startTurn(secondTurn(thread)); yield* Deferred.await(bothEnded); const [first, second] = turns; - const snapshot = yield* runtime.rollbackThread({ + yield* runtime.rollbackThread({ providerThread: thread, target: { type: "provider_turn", @@ -6935,10 +6935,6 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => { }, providerThreadTurns: [first!, second!], }); - assert.deepEqual( - snapshot.providerTurns.map((turn) => turn.id), - [first!.id], - ); }), ); @@ -6994,10 +6990,7 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => { ); it.effect.each([ - [ - "keeps the recorded turns before the target when rolling back a session not loaded yet", - false, - ], + ["reverts a session not loaded yet at the first recorded turn after the target", false], ] as const)("%s", ([, running]) => Effect.gen(function* () { const first = `msg_t3_turn_${SESSION}:attempt:first`; @@ -7069,10 +7062,6 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => { ); } else { assert.isTrue(Exit.isSuccess(rollback)); - assert.deepEqual( - Exit.isSuccess(rollback) ? rollback.value.providerTurns.map((turn) => turn.id) : [], - [kept.id], - ); } }), ); @@ -7360,7 +7349,7 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => { ], "msg_external", ); - const snapshot = yield* runtime.rollbackThread({ + yield* runtime.rollbackThread({ providerThread: thread, target: { type: "provider_turn", @@ -7370,10 +7359,6 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => { }, providerThreadTurns: [first, second], }); - assert.deepEqual( - snapshot.providerTurns.map((turn) => turn.id), - [first.id], - ); }), ); @@ -7872,7 +7857,7 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => { yield* runtime.startTurn(continuationTurn(thread, offers[1]!)); yield* Deferred.await(bothEnded); const [first, continuation] = turns; - const snapshot = yield* runtime.rollbackThread({ + yield* runtime.rollbackThread({ providerThread: thread, target: { type: "provider_turn", @@ -7882,10 +7867,6 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => { }, providerThreadTurns: [first!, continuation!], }); - assert.deepEqual( - snapshot.providerTurns.map((turn) => turn.id), - [first!.id], - ); }), ); @@ -7994,7 +7975,7 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => { yield* runtime.startTurn(continuationTurn(thread, yield* Deferred.await(offered))); yield* Deferred.await(bothEnded); const [first, continuation] = turns; - const snapshot = yield* runtime.rollbackThread({ + yield* runtime.rollbackThread({ providerThread: thread, target: { type: "provider_turn", @@ -8004,10 +7985,6 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => { }, providerThreadTurns: [first!, continuation!], }); - assert.deepEqual( - snapshot.providerTurns.map((turn) => turn.id), - [first!.id], - ); }), ); diff --git a/packages/provider-acp/src/server/adapter.ts b/packages/provider-acp/src/server/adapter.ts index 9fb64218c5b29..d9410e4754f35 100644 --- a/packages/provider-acp/src/server/adapter.ts +++ b/packages/provider-acp/src/server/adapter.ts @@ -10696,7 +10696,6 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( updatedAt: now, }, // Imports read only the transcript; turns reach orchestration as events. - providerTurns: [], messages, reasoningMessageIds: [...state.reasoningIds], runtimeRequests: [], @@ -10755,7 +10754,6 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( status: "idle" as const, updatedAt: now, }, - providerTurns: [], messages: [], runtimeRequests: [], }; diff --git a/packages/provider-core/src/server/ProviderAdapter.ts b/packages/provider-core/src/server/ProviderAdapter.ts index 049198f9a6cb4..4d008a7233900 100644 --- a/packages/provider-core/src/server/ProviderAdapter.ts +++ b/packages/provider-core/src/server/ProviderAdapter.ts @@ -648,7 +648,6 @@ export interface ProviderAdapterV2RuntimeRequestResponseInput { export interface ProviderAdapterV2ThreadSnapshot { readonly providerThread: OrchestrationV2ProviderThread; - readonly providerTurns: ReadonlyArray; readonly messages: ReadonlyArray; /** Ids in `messages` that hold the agent's reasoning, not its reply. */ readonly reasoningMessageIds?: ReadonlyArray; diff --git a/packages/provider-cursor/src/server/adapter.ts b/packages/provider-cursor/src/server/adapter.ts index 426bc8b60a264..c758e46a7b3dc 100644 --- a/packages/provider-cursor/src/server/adapter.ts +++ b/packages/provider-cursor/src/server/adapter.ts @@ -3548,7 +3548,6 @@ export const makeCursorAdapterV2 = Effect.fn("makeCursorAdapterV2")(function* ( status: "idle" as const, updatedAt: now, }, - providerTurns: [], messages: projectedMessages, runtimeRequests: [], providerPayload: { @@ -3629,7 +3628,6 @@ export const makeCursorAdapterV2 = Effect.fn("makeCursorAdapterV2")(function* ( status: "idle" as const, updatedAt: now, }, - providerTurns: [], messages: [], runtimeRequests: [], }; @@ -3659,7 +3657,6 @@ export const makeCursorAdapterV2 = Effect.fn("makeCursorAdapterV2")(function* ( status: "idle" as const, updatedAt: now, }, - providerTurns: [], messages: [], runtimeRequests: [], }; diff --git a/packages/provider-muse/src/server/adapter.ts b/packages/provider-muse/src/server/adapter.ts index 66c1fd1a505ff..825e793876571 100644 --- a/packages/provider-muse/src/server/adapter.ts +++ b/packages/provider-muse/src/server/adapter.ts @@ -2245,7 +2245,6 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* ( const now = yield* DateTime.now; return { providerThread: current, - providerTurns: [], messages: items.flatMap((item): OrchestrationV2ConversationMessage[] => item.kind === "agentMessage" || item.kind === "userMessage" ? [ diff --git a/packages/provider-opencode/src/server/adapter.test.ts b/packages/provider-opencode/src/server/adapter.test.ts index 3cd83ac152f8c..5e0b34c029111 100644 --- a/packages/provider-opencode/src/server/adapter.test.ts +++ b/packages/provider-opencode/src/server/adapter.test.ts @@ -151,6 +151,41 @@ function providerTurn(input: { }; } +// Turns reach orchestration only as `provider_turn.updated` events, so tests read them there. +function trackProviderTurns( + runtime: Pick, +) { + const turns = new Map(); + const events = runtime.events.pipe( + Stream.tap((event) => + Effect.sync(() => { + if (event.type === "provider_turn.updated") { + turns.set(event.providerTurn.id, event.providerTurn); + } + }), + ), + ); + const awaitTurns = Effect.fnUntraced(function* ( + predicate: (turns: ReadonlyArray) => boolean, + ) { + const seen = () => [...turns.values()]; + if (!predicate(seen())) { + yield* events.pipe( + Stream.takeUntil(() => predicate(seen())), + Stream.runDrain, + ); + } + return seen(); + }); + const awaitLatestTurn = ( + predicate: (turn: OrchestrationV2ProviderTurn) => boolean = () => true, + ) => + awaitTurns((seen) => seen.length > 0 && predicate(seen.at(-1)!)).pipe( + Effect.map((seen) => seen.at(-1)!), + ); + return { events, awaitTurns, awaitLatestTurn }; +} + const makeOpenCodeRuntimeHarness = Effect.fn("makeOpenCodeRuntimeHarness")(function* ( suffix: string, nativeSessionId: string, @@ -260,6 +295,7 @@ const makeOpenCodeRuntimeHarness = Effect.fn("makeOpenCodeRuntimeHarness")(funct runtimePolicy: turnPolicy, }); return { + ...trackProviderTurns(runtime), nativeSessionId, now, policy, @@ -466,7 +502,7 @@ describe("OpenCodeAdapterV2", () => { yield* Effect.promise(() => nativeEvents.push({ type: "session.idle", properties: { sessionID: sessionId } }), ); - yield* harness.runtime.events.pipe( + yield* harness.events.pipe( Stream.filter((event) => event.type === "turn.terminal"), Stream.take(1), Stream.runDrain, @@ -538,10 +574,7 @@ describe("OpenCodeAdapterV2", () => { ); const start = yield* harness.startTurn().pipe(Effect.forkScoped); yield* Effect.promise(() => catalogStarted.promise); - const snapshot = yield* harness.runtime.readThreadSnapshot({ - providerThread: harness.providerThread, - }); - const activeTurn = snapshot.providerTurns.at(-1)!; + const activeTurn = yield* harness.awaitLatestTurn(); const steer = (message: string) => harness.runtime .steerTurn({ @@ -616,7 +649,7 @@ describe("OpenCodeAdapterV2", () => { }, }); yield* harness.startTurn(); - const received = yield* harness.runtime.events.pipe( + const received = yield* harness.events.pipe( Stream.takeUntil((event) => event.type === "turn.terminal"), Stream.runCollect, Effect.forkScoped, @@ -648,7 +681,7 @@ describe("OpenCodeAdapterV2", () => { }, }); yield* harness.startTurn(); - const received = yield* harness.runtime.events.pipe( + const received = yield* harness.events.pipe( Stream.takeUntil((event) => event.type === "turn.terminal"), Stream.runCollect, Effect.forkScoped, @@ -864,7 +897,7 @@ describe("OpenCodeAdapterV2", () => { }, }); yield* harness.startTurn(); - const received = yield* harness.runtime.events.pipe( + const received = yield* harness.events.pipe( Stream.takeUntil((event) => event.type === "turn.terminal"), Stream.runCollect, Effect.forkScoped, @@ -999,7 +1032,7 @@ describe("OpenCodeAdapterV2", () => { }, }); yield* harness.startTurn(); - const received = yield* harness.runtime.events.pipe( + const received = yield* harness.events.pipe( Stream.takeUntil((event) => event.type === "turn.terminal"), Stream.runCollect, Effect.forkScoped, @@ -1107,7 +1140,7 @@ describe("OpenCodeAdapterV2", () => { contextUsage: { usedTokens: 150_000, maxTokens: 160_000 }, }, }); - const received = yield* harness.runtime.events.pipe( + const received = yield* harness.events.pipe( Stream.takeUntil((event) => event.type === "turn.terminal"), Stream.runCollect, Effect.forkScoped, @@ -1194,7 +1227,7 @@ describe("OpenCodeAdapterV2", () => { }, }, }); - const received = yield* harness.runtime.events.pipe( + const received = yield* harness.events.pipe( Stream.takeUntil((event) => event.type === "turn.terminal"), Stream.runCollect, Effect.forkScoped, @@ -1236,7 +1269,7 @@ describe("OpenCodeAdapterV2", () => { }, }); yield* harness.startTurn(); - const received = yield* harness.runtime.events.pipe( + const received = yield* harness.events.pipe( Stream.filter( (event) => event.type === "runtime_request.updated" || @@ -1312,7 +1345,7 @@ describe("OpenCodeAdapterV2", () => { }, }); yield* harness.startTurn(); - const received = yield* harness.runtime.events.pipe( + const received = yield* harness.events.pipe( Stream.takeUntil( (event) => event.type === "turn_item.updated" && event.turnItem.type === "compaction", ), @@ -1723,7 +1756,7 @@ describe("OpenCodeAdapterV2", () => { }, }, }); - const awaitTerminal = harness.runtime.events.pipe( + const awaitTerminal = harness.events.pipe( Stream.filter((event) => event.type === "turn.terminal"), Stream.take(1), Stream.runDrain, @@ -1736,14 +1769,9 @@ describe("OpenCodeAdapterV2", () => { yield* awaitTerminal; } assert.equal(offers.length, 0, "foreground admission must not offer a continuation"); - const before = yield* harness.runtime.readThreadSnapshot({ - providerThread: harness.providerThread, - }); - assert.equal(before.providerTurns.length, 1); - assert.equal( - before.providerTurns[0]?.status, - state === "settled" ? "completed" : "running", - ); + const before = yield* harness.awaitTurns((turns) => turns.length > 0); + assert.equal(before.length, 1); + assert.equal(before[0]?.status, state === "settled" ? "completed" : "running"); // Pinned task.ts injects a synthetic root prompt; prompt.ts saves it before starting its loop. yield* push(user("msg_background_result")); @@ -1906,10 +1934,7 @@ describe("OpenCodeAdapterV2", () => { yield* push(status("idle")); yield* Fiber.join(starting); assert.equal(promptIds.length, 1, "failed waiting foreground cannot submit"); - const snapshot = yield* harness.runtime.readThreadSnapshot({ - providerThread: harness.providerThread, - }); - assert.equal(snapshot.providerTurns.at(-1)?.status, "failed"); + yield* harness.awaitLatestTurn((turn) => turn.status === "failed"); return; } if (end === "catalog-live" || end === "catalog-replay") { @@ -1939,15 +1964,13 @@ describe("OpenCodeAdapterV2", () => { yield* TestClock.adjust("10 seconds"); if (idle !== null) yield* Fiber.join(idle); else yield* Fiber.join(starting); - const events = yield* harness.runtime.events.pipe( + const events = yield* harness.events.pipe( Stream.takeUntil((event) => event.type === "turn.terminal"), Stream.runCollect, ); assert.equal(events.find((event) => event.type === "turn.terminal")?.status, "completed"); - const snapshot = yield* harness.runtime.readThreadSnapshot({ - providerThread: harness.providerThread, - }); - assert.equal(snapshot.providerTurns.at(-1)?.status, "completed"); + const turn = yield* harness.awaitLatestTurn((turn) => turn.completedAt !== null); + assert.equal(turn.status, "completed"); return; } assert.equal(offers.length, state === "settled" ? 1 : 0, "only a settled root needs a run"); @@ -1955,10 +1978,8 @@ describe("OpenCodeAdapterV2", () => { yield* offers[0]!.onRunEndedWithoutStarting!(); yield* harness.startTurn("", undefined, undefined, false, "no-wake", {}); yield* awaitTerminal; - const snapshot = yield* harness.runtime.readThreadSnapshot({ - providerThread: harness.providerThread, - }); - assert.equal(snapshot.providerTurns.at(-1)?.status, "completed"); + const turn = yield* harness.awaitLatestTurn((turn) => turn.completedAt !== null); + assert.equal(turn.status, "completed"); assert.equal(promptIds.length, 1); return; } @@ -2122,7 +2143,7 @@ describe("OpenCodeAdapterV2", () => { }); assert.equal(beforeAdmission.runtimeRequests.length, 0); yield* harness.startTurn("", undefined, undefined, false, "held", {}); - const events = yield* harness.runtime.events.pipe( + const events = yield* harness.events.pipe( Stream.takeUntil( (event) => event.type === "runtime_request.updated" && @@ -2215,7 +2236,7 @@ describe("OpenCodeAdapterV2", () => { assert.isTrue(Exit.isFailure(replacement), "uncertain native work requires reconnect"); assert.equal(promptIds.length, 1, "replacement cannot join the surviving native loop"); assert.deepEqual(aborted, [root, "ses_descendant"]); - const events = yield* harness.runtime.events.pipe( + const events = yield* harness.events.pipe( Stream.takeUntil( (event) => event.type === "provider_session.updated" && @@ -2269,19 +2290,13 @@ describe("OpenCodeAdapterV2", () => { yield* Effect.promise(() => routeStarted.promise); yield* TestClock.adjust("10 seconds"); assert.isTrue(Exit.isFailure(yield* Fiber.join(starting))); - const snapshot = yield* harness.runtime.readThreadSnapshot({ - providerThread: harness.providerThread, - }); - assert.equal( - snapshot.providerTurns.at(-1)?.status, - "failed", - "the installed turn must be finalized", - ); + // The installed turn must be finalized. + yield* harness.awaitLatestTurn((turn) => turn.status === "failed"); assert.deepEqual(aborted, [root, "ses_descendant"]); const replacement = yield* harness.startTurn("replacement").pipe(Effect.exit); assert.isTrue(Exit.isFailure(replacement), "incomplete replay requires reconnect"); assert.equal(promptIds.length, 1, "failed replay cannot submit a foreground prompt"); - const events = yield* harness.runtime.events.pipe( + const events = yield* harness.events.pipe( Stream.takeUntil((event) => event.type === "turn.terminal"), Stream.runCollect, ); @@ -2363,7 +2378,7 @@ describe("OpenCodeAdapterV2", () => { if (end === "owned-exit" || end === "failed-stream") { if (end === "owned-exit") yield* Deferred.succeed(nativeExit, 1); else nativeEvents.close(); - yield* harness.runtime.events.pipe( + yield* harness.events.pipe( Stream.filter( (event) => event.type === "provider_session.updated" && @@ -2451,10 +2466,8 @@ describe("OpenCodeAdapterV2", () => { [], "old callbacks cannot abort the installed runtime", ); - const replacementSnapshot = yield* replacement.runtime.readThreadSnapshot({ - providerThread: resumed, - }); - assert.equal(replacementSnapshot.providerTurns.at(-1)?.status, "running"); + const replacementTurn = yield* replacement.awaitLatestTurn(); + assert.equal(replacementTurn.status, "running"); return; } const foreground = @@ -2487,12 +2500,10 @@ describe("OpenCodeAdapterV2", () => { if (end === "foreground-stop" || end === "foreground-eof") { if (end === "foreground-eof") nativeEvents.close(); else { - const snapshot = yield* harness.runtime.readThreadSnapshot({ - providerThread: harness.providerThread, - }); + const turn = yield* harness.awaitLatestTurn((turn) => turn.status === "running"); yield* harness.runtime.interruptTurn({ providerThread: harness.providerThread, - providerTurnId: snapshot.providerTurns.at(-1)!.id, + providerTurnId: turn.id, requestRuntimeRestart: true, }); } @@ -2545,14 +2556,8 @@ describe("OpenCodeAdapterV2", () => { assert.equal(promptIds.length, 2); if (end === "foreground-second-saved") { yield* push({ type: "session.idle", properties: { sessionID: root } }); - const snapshot = yield* harness.runtime.readThreadSnapshot({ - providerThread: harness.providerThread, - }); - assert.equal( - snapshot.providerTurns.at(-1)?.status, - "running", - "an earlier reply cannot admit the foreground prompt", - ); + // An earlier reply cannot admit the foreground prompt. + yield* harness.awaitLatestTurn((turn) => turn.status === "running"); } return; } @@ -2584,7 +2589,7 @@ describe("OpenCodeAdapterV2", () => { if (end === "sequential") yield* secondResult; assert.equal(offers.length, 2, "completed sequential wakes each offer once"); yield* harness.startTurn("", undefined, undefined, false, "wake-two", {}); - const events = yield* harness.runtime.events.pipe( + const events = yield* harness.events.pipe( Stream.takeUntil((event) => event.type === "turn.terminal"), Stream.runCollect, ); @@ -2599,11 +2604,11 @@ describe("OpenCodeAdapterV2", () => { ), ); assert.equal(promptIds.length, 1, "continuations never submit another prompt"); - const snapshot = yield* harness.runtime.readThreadSnapshot({ - providerThread: harness.providerThread, - }); + const turns = yield* harness.awaitTurns( + (turns) => turns.length === 3 && turns.every((turn) => turn.completedAt !== null), + ); assert.deepEqual( - snapshot.providerTurns.map((turn) => turn.status), + turns.map((turn) => turn.status), ["completed", "completed", "completed"], ); return; @@ -2616,10 +2621,7 @@ describe("OpenCodeAdapterV2", () => { if (end === "foreground-buffered" || end === "foreground-buffered-error") yield* harness.startTurn("follow up", undefined, undefined, false, "next"); assert.equal(promptIds.length, 2, "the foreground prompt is submitted"); - const snapshot = yield* harness.runtime.readThreadSnapshot({ - providerThread: harness.providerThread, - }); - assert.equal(snapshot.providerTurns.at(-1)?.status, "running"); + yield* harness.awaitLatestTurn((turn) => turn.status === "running"); yield* push(user(promptIds[1]!)); yield* push({ type: "message.part.updated", @@ -2656,7 +2658,7 @@ describe("OpenCodeAdapterV2", () => { end === "foreground-buffered" || end === "foreground-buffered-error" ) { - const events = yield* harness.runtime.events.pipe( + const events = yield* harness.events.pipe( Stream.takeUntil((event) => event.type === "turn.terminal"), Stream.runCollect, ); @@ -2785,7 +2787,7 @@ describe("OpenCodeAdapterV2", () => { return yield* Effect.die("OpenCode runtime must expose hasPendingBackgroundWork."); } yield* harness.startTurn(); - const received = yield* harness.runtime.events.pipe( + const received = yield* harness.events.pipe( Stream.takeUntil( (event) => event.type === "turn_item.updated" && @@ -2942,7 +2944,7 @@ describe("OpenCodeAdapterV2", () => { }, }); yield* harness.startTurn(); - const childTurns = yield* harness.runtime.events.pipe( + const childTurns = yield* harness.events.pipe( Stream.filter( (event) => event.type === "provider_turn.updated" && @@ -3055,7 +3057,7 @@ describe("OpenCodeAdapterV2", () => { }, }); yield* harness.startTurn(); - const received = yield* harness.runtime.events.pipe( + const received = yield* harness.events.pipe( Stream.takeUntil( (event) => event.type === "turn_item.updated" && event.turnItem.type === "compaction", ), @@ -3174,7 +3176,7 @@ describe("OpenCodeAdapterV2", () => { }, }); yield* harness.startTurn(); - const received = yield* harness.runtime.events.pipe( + const received = yield* harness.events.pipe( Stream.takeUntil((event) => event.type === "plan.updated"), Stream.runCollect, Effect.forkScoped, @@ -3241,7 +3243,7 @@ describe("OpenCodeAdapterV2", () => { }, }); yield* harness.startTurn(); - const received = yield* harness.runtime.events.pipe( + const received = yield* harness.events.pipe( Stream.takeUntil( (event) => event.type === "turn_item.updated" && event.turnItem.type === "compaction", ), @@ -3502,7 +3504,7 @@ describe("OpenCodeAdapterV2", () => { }), ); yield* Fiber.join(start); - const terminal = yield* harness.runtime.events.pipe( + const terminal = yield* harness.events.pipe( Stream.filter((event) => event.type === "turn.terminal"), Stream.take(1), Stream.runCollect, @@ -3564,7 +3566,7 @@ describe("OpenCodeAdapterV2", () => { }, }); yield* harness.startTurn("/compact"); - const events = yield* harness.runtime.events.pipe( + const events = yield* harness.events.pipe( Stream.takeUntil((event) => event.type === "turn.terminal"), Stream.runCollect, ); @@ -3618,7 +3620,7 @@ describe("OpenCodeAdapterV2", () => { id: ProviderThreadId.make("provider-thread:opencode-test:forked-run-row"), }; yield* harness.startTurn("/compact", forkedRow); - const terminal = yield* harness.runtime.events.pipe( + const terminal = yield* harness.events.pipe( Stream.filter( (event): event is Extract => event.type === "turn.terminal", @@ -3739,14 +3741,12 @@ describe("OpenCodeAdapterV2", () => { { options: [{ id: "variant", value: "high" }], interactionMode: "plan" }, ); yield* harness.startTurn(); - const snapshot = yield* harness.runtime.readThreadSnapshot({ - providerThread: harness.providerThread, - }); + const turn = yield* harness.awaitLatestTurn(); yield* harness.runtime.steerTurn({ threadId: harness.threadId, runId: harness.runId, providerThread: harness.providerThread, - providerTurnId: snapshot.providerTurns.at(-1)!.id, + providerTurnId: turn.id, message: { messageId: MessageId.make("message-opencode-steer-agent"), text: "also cover the README", @@ -3865,6 +3865,7 @@ describe("OpenCodeAdapterV2", () => { modelSelection, runtimePolicy: policy, }); + const tracked = trackProviderTurns(runtime); const providerThread = yield* runtime.ensureThread({ threadId, modelSelection, @@ -3972,9 +3973,8 @@ describe("OpenCodeAdapterV2", () => { yield* Effect.promise(() => statusCalled.promise); - const snapshot = yield* runtime.readThreadSnapshot({ providerThread }); - assert.equal(snapshot.providerTurns.at(-1)?.status, "running"); - const activeTurn = snapshot.providerTurns.at(-1)!; + const activeTurn = yield* tracked.awaitLatestTurn(); + assert.equal(activeTurn.status, "running"); const steer = yield* runtime .steerTurn({ threadId, @@ -4080,8 +4080,8 @@ describe("OpenCodeAdapterV2", () => { properties: { sessionID: "native-opencode-race", status: { type: "idle" } }, }), ); - const interrupted = yield* runtime.readThreadSnapshot({ providerThread }); - assert.equal(interrupted.providerTurns.at(-1)?.status, "interrupted"); + const interrupted = yield* tracked.awaitLatestTurn((turn) => turn.completedAt !== null); + assert.equal(interrupted.status, "interrupted"); }).pipe(Effect.provide(Layer.merge(IdAllocator.layer, McpProviderSessions.layer))), ); @@ -4125,10 +4125,7 @@ describe("OpenCodeAdapterV2", () => { }, ); yield* harness.startTurn(); - const snapshot = yield* harness.runtime.readThreadSnapshot({ - providerThread: harness.providerThread, - }); - const turn = snapshot.providerTurns.at(-1)!; + const turn = yield* harness.awaitLatestTurn(); const steer = yield* harness.runtime .steerTurn({ threadId: harness.threadId, @@ -4166,10 +4163,8 @@ describe("OpenCodeAdapterV2", () => { properties: { sessionID: "root", status: { type: "idle" } }, }), ); - const stopped = yield* harness.runtime.readThreadSnapshot({ - providerThread: harness.providerThread, - }); - assert.equal(stopped.providerTurns.at(-1)?.status, "interrupted"); + const stopped = yield* harness.awaitLatestTurn((turn) => turn.completedAt !== null); + assert.equal(stopped.status, "interrupted"); }).pipe(Effect.provide(Layer.merge(IdAllocator.layer, McpProviderSessions.layer))), ); @@ -4252,10 +4247,8 @@ describe("OpenCodeAdapterV2", () => { }, }), ); - const running = yield* harness.runtime.readThreadSnapshot({ - providerThread: harness.providerThread, - }); - yield* harness.runtime.events.pipe( + const running = yield* harness.awaitLatestTurn(); + yield* harness.events.pipe( Stream.tap((event) => event.type === "turn.terminal" ? Deferred.succeed(terminal, undefined) : Effect.void, ), @@ -4267,7 +4260,7 @@ describe("OpenCodeAdapterV2", () => { threadId: harness.threadId, runId: harness.runId, providerThread: harness.providerThread, - providerTurnId: running.providerTurns.at(-1)!.id, + providerTurnId: running.id, message: { messageId: MessageId.make(`message-steer-rejected-${idleTiming}`), text: "also cover the README", @@ -4285,17 +4278,13 @@ describe("OpenCodeAdapterV2", () => { Deferred.await(terminal).pipe(Effect.as("completed")), ); assert.equal(outcome, "rechecked"); - const active = yield* harness.runtime.readThreadSnapshot({ - providerThread: harness.providerThread, - }); - assert.equal(active.providerTurns.at(-1)?.status, "running"); + const active = yield* harness.awaitLatestTurn(); + assert.equal(active.status, "running"); yield* Effect.promise(() => nativeEvents.push(idle)); } if (idleTiming === "after-refused-connection") { - const active = yield* harness.runtime.readThreadSnapshot({ - providerThread: harness.providerThread, - }); - assert.equal(active.providerTurns.at(-1)?.status, "running"); + const active = yield* harness.awaitLatestTurn(); + assert.equal(active.status, "running"); yield* Effect.promise(() => nativeEvents.push(idle)); } yield* Deferred.await(terminal); @@ -4303,10 +4292,8 @@ describe("OpenCodeAdapterV2", () => { assert.equal(statusCalls, idleTiming === "idle-during-status" ? 2 : 1); } - const settled = yield* harness.runtime.readThreadSnapshot({ - providerThread: harness.providerThread, - }); - assert.equal(settled.providerTurns.at(-1)?.status, "completed"); + const settled = yield* harness.awaitLatestTurn((turn) => turn.completedAt !== null); + assert.equal(settled.status, "completed"); }).pipe( Effect.provide(Layer.merge(IdAllocator.layer, McpProviderSessions.layer)), Effect.scoped, @@ -4354,10 +4341,8 @@ describe("OpenCodeAdapterV2", () => { }), ); yield* userMessage(promptIds[0]); - const running = yield* harness.runtime.readThreadSnapshot({ - providerThread: harness.providerThread, - }); - const received = yield* harness.runtime.events.pipe( + const running = yield* harness.awaitLatestTurn(); + const received = yield* harness.events.pipe( Stream.takeUntil((event) => event.type === "turn.terminal"), Stream.runCollect, Effect.forkScoped, @@ -4366,7 +4351,7 @@ describe("OpenCodeAdapterV2", () => { threadId: harness.threadId, runId: harness.runId, providerThread: harness.providerThread, - providerTurnId: running.providerTurns.at(-1)!.id, + providerTurnId: running.id, message: { messageId: MessageId.make("message-steer-reply-lost"), text: "also cover the README", @@ -4510,12 +4495,13 @@ describe("OpenCodeAdapterV2", () => { modelSelection, runtimePolicy: policy, }); + const tracked = trackProviderTurns(runtime); const providerThread = yield* runtime.ensureThread({ threadId, modelSelection, runtimePolicy: policy, }); - const terminalEvents = yield* runtime.events.pipe( + const terminalEvents = yield* tracked.events.pipe( Stream.takeUntil((event) => event.type === "turn.terminal"), Stream.runCollect, Effect.forkScoped, @@ -4566,8 +4552,7 @@ describe("OpenCodeAdapterV2", () => { .pipe(Effect.forkScoped); yield* Effect.promise(() => promptStarted.promise); - const running = yield* runtime.readThreadSnapshot({ providerThread }); - const activeTurn = running.providerTurns.at(-1)!; + const activeTurn = yield* tracked.awaitLatestTurn(); const interrupt = yield* runtime .interruptTurn({ providerThread, providerTurnId: activeTurn.id }) .pipe(Effect.forkScoped); @@ -4635,10 +4620,7 @@ describe("OpenCodeAdapterV2", () => { mcp: { add: async () => ({ data: true }) }, }); yield* harness.startTurn(); - const snapshot = yield* harness.runtime.readThreadSnapshot({ - providerThread: harness.providerThread, - }); - const turn = snapshot.providerTurns.at(-1)!; + const turn = yield* harness.awaitLatestTurn(); const interrupt = yield* harness.runtime .interruptTurn({ providerThread: harness.providerThread, providerTurnId: turn.id }) .pipe(Effect.flip, Effect.forkScoped); @@ -4679,10 +4661,7 @@ describe("OpenCodeAdapterV2", () => { }, ); yield* harness.startTurn(); - const terminalEvents = yield* harness.runtime.events.pipe( - Stream.runCollect, - Effect.forkScoped, - ); + const terminalEvents = yield* harness.events.pipe(Stream.runCollect, Effect.forkScoped); nativeEvents.close(); const received = Array.from(yield* Fiber.join(terminalEvents)); @@ -4736,7 +4715,7 @@ describe("OpenCodeAdapterV2", () => { }, }, ).pipe(Effect.provideService(Clock.Clock, blockingClock)); - const events = yield* harness.runtime.events.pipe(Stream.runCollect, Effect.forkScoped); + const events = yield* harness.events.pipe(Stream.runCollect, Effect.forkScoped); const start = yield* harness.startTurn("/compact").pipe(Effect.forkScoped); yield* Effect.promise(() => summarizeStarted.promise); blockNextClockRead = true; @@ -4809,7 +4788,7 @@ describe("OpenCodeAdapterV2", () => { .pipe(Effect.provideService(Clock.Clock, blockingClock), Effect.exit, Effect.forkScoped); yield* Deferred.await(startClockRead); - const events = yield* harness.runtime.events.pipe(Stream.runCollect, Effect.forkScoped); + const events = yield* harness.events.pipe(Stream.runCollect, Effect.forkScoped); nativeEvents.close(); yield* Fiber.join(events); yield* Deferred.succeed(releaseStartClockRead, undefined); diff --git a/packages/provider-opencode/src/server/adapter.ts b/packages/provider-opencode/src/server/adapter.ts index 6398e8ec74800..39376b09c0b59 100644 --- a/packages/provider-opencode/src/server/adapter.ts +++ b/packages/provider-opencode/src/server/adapter.ts @@ -3496,7 +3496,6 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function status: "idle" as const, updatedAt: snapshotNow, }, - providerTurns: state === undefined ? [] : [...state.providerTurns.values()], messages, runtimeRequests: state === undefined ? [] : [...state.runtimeRequests.values()], providerPayload: nativeMessages, diff --git a/packages/provider-opencode/src/server/v2/adapter.ts b/packages/provider-opencode/src/server/v2/adapter.ts index 9cee26a3fb20d..c1426bb234735 100644 --- a/packages/provider-opencode/src/server/v2/adapter.ts +++ b/packages/provider-opencode/src/server/v2/adapter.ts @@ -349,7 +349,6 @@ interface ThreadState { goalRevision: number; goalFresh: boolean; goalRefreshing: boolean; - readonly providerTurns: Map; active: ActiveTurn | undefined; model: ModelRef | undefined; /** Locally ended but possibly live; consume its next execution end without settling a new turn. */ @@ -934,7 +933,6 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr goalRevision: 0, goalFresh: false, goalRefreshing: false, - providerTurns: new Map(), active: undefined, model: undefined, unsettled: false, @@ -1635,20 +1633,14 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr return ids.length === 0 ? {} : { retainedRuntimeRequestIds: ids }; }; - const emitProviderTurn = ( - state: ThreadState, - turn: ActiveTurn, - providerTurn: OrchestrationV2ProviderTurn, - ) => { - state.providerTurns.set(String(providerTurn.id), providerTurn); - return emit({ + const emitProviderTurn = (turn: ActiveTurn, providerTurn: OrchestrationV2ProviderTurn) => + emit({ type: "provider_turn.updated", driver, threadId: turn.input.threadId, providerTurn, ...(providerTurn.completedAt === null ? {} : retainedRequestsOf(turn)), }); - }; const makeTurn = ( owner: TurnOwner, @@ -2289,7 +2281,7 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr completedAt: null, }, }); - yield* emitProviderTurn(child, turn, providerTurn); + yield* emitProviderTurn(turn, providerTurn); const prompt = subagent.prompt; subagent.prompt = undefined; if (prompt !== undefined && prompt.length > 0) { @@ -2401,7 +2393,7 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr ); const window = windowOf(turn.input.runtimePolicy.cwd, turn.input.modelSelection.model); const lastStep = turn.lastStep; - yield* emitProviderTurn(state, turn, { + yield* emitProviderTurn(turn, { ...turn.providerTurn, status: terminal.status, completedAt, @@ -4321,7 +4313,6 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr ]; }); const lastUser = history.findLast((message) => message.type === "user")?.id; - const state = threads.get(sessionId); return { providerThread: { ...providerThread, @@ -4330,7 +4321,6 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr status: "idle" as const, updatedAt: snapshotAt, }, - providerTurns: state === undefined ? [] : [...state.providerTurns.values()], messages, runtimeRequests: [], }; @@ -4491,7 +4481,7 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr } state.active = turn; state.sessionScopedBackground = false; - yield* emitProviderTurn(state, turn, providerTurn); + yield* emitProviderTurn(turn, providerTurn); state.providerThread = { ...state.providerThread, status: "active", @@ -5459,22 +5449,7 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr if (boundary !== null) { yield* rollBackTo(sessionId, SessionMessage.ID.make(boundary)); } - const keptOrdinal = target.type === "provider_turn" ? target.providerTurn.ordinal : 0; - const loaded = threads.get(sessionId); - for (const [id, turn] of loaded?.providerTurns ?? []) { - if (turn.ordinal > keptOrdinal) loaded?.providerTurns.delete(id); - } - const snapshot = yield* snapshotOf(providerThread, sessionId); - const kept = new Map( - rollbackInput.providerThreadTurns - .filter( - (turn) => - turn.providerThreadId === providerThread.id && turn.ordinal <= keptOrdinal, - ) - .map((turn) => [turn.id, turn] as const), - ); - for (const turn of snapshot.providerTurns) kept.set(turn.id, turn); - return { ...snapshot, providerTurns: [...kept.values()] }; + return yield* snapshotOf(providerThread, sessionId); }, (effect, rollbackInput) => effect.pipe( diff --git a/packages/provider-pi/src/server/adapter.ts b/packages/provider-pi/src/server/adapter.ts index e7803bbe9c5ee..ad1fce56f9da8 100644 --- a/packages/provider-pi/src/server/adapter.ts +++ b/packages/provider-pi/src/server/adapter.ts @@ -3858,7 +3858,6 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* ( ); return { providerThread: state.providerThread, - providerTurns: [], messages, runtimeRequests: [], }; @@ -4370,7 +4369,7 @@ function piExtensionDisplayName(extensionPath: string | undefined): string { function piThreadSnapshot( providerThread: OrchestrationV2ProviderThread, ): ProviderAdapter.ProviderAdapterV2ThreadSnapshot { - return { providerThread, providerTurns: [], messages: [], runtimeRequests: [] }; + return { providerThread, messages: [], runtimeRequests: [] }; } function piQuestion(