feat(server): OpenCode 2 native steering, fork and rollback #14468
Mergedjuliusmarmingemerged 7 commits into
t3code/codex-turn-mapping fromrefs/pull/14468/headmerged by juliusmarminge View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XXL
- apps/server/src/orchestration-v2/Adapters/OpenCode2AdapterV2.ts lines 104 to 104Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -97,6 +101,4 @@97101 threads: {98102 canCreateEmptyThread: true,99103 canReadThreadSnapshot: true,100- canRollbackThread: false,101- canForkThread: false,102- canForkFromTurn: false,104+ canRollbackThread: true,apps/server/src/orchestration-v2/Adapters/OpenCode2AdapterV2.ts lines 1557 to 1565OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -1426,8 +1545,21 @@14261545 rules: native.permissions,14271546 policy: input.runtimePolicy,14281547 grants: [],1548+ strandedSteers: new Set(),14291549 };14301550 threads.set(native.id, state);14311551 return state;14321552 };143315531554+ /** Takes back steers a finished turn left in OpenCode's inbox. */1555+ const cancelStrandedSteers = Effect.fnUntraced(function* (state: ThreadState) {1556+ const sessionID = Session.ID.make(state.sessionId);1557+ yield* Effect.forEach(1558+ state.strandedSteers,1559+ (inboxID) =>1560+ client.session.inbox1561+ .cancel({ sessionID, inboxID: SessionMessage.ID.make(inboxID) })1562+ .pipe(Effect.timeout(REQUEST_REPLY_TIMEOUT), Effect.ignore({ log: true })),1563+ { discard: true },1564+ );1565+ state.strandedSteers.clear();
- apps/server/src/orchestration-v2/Adapters/OpenCode2AdapterV2.ts line 3259OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -2957,0 +3239,21 @@3239+ const state = threads.get(sessionId);3240+ if (state?.active !== undefined || (state !== undefined && hasBackground(state))) {3241+ return yield* new ProviderAdapter.ProviderAdapterProtocolError({3242+ driver: OPENCODE_PROVIDER,3243+ detail: `Cannot roll back OpenCode session ${sessionId} while it is still working`,3244+ });3245+ }3246+ // A session this runtime has not loaded yet is loaded the way a turn3247+ // would load it, which stops anything it still waits on.3248+ if (state === undefined) yield* runtime.resumeThread({ providerThread });3249+ const boundary = yield* boundaryAfter(3250+ rollbackInput.providerThreadTurns,3251+ providerThread.id,3252+ target.type === "provider_turn" ? target.providerTurn : undefined,3253+ yield* userMessages(sessionId),3254+ );3255+ if (boundary !== null) {3256+ yield* rollBackTo(sessionId, SessionMessage.ID.make(boundary));3257+ }3258+ // The snapshot's head is the last user message OpenCode kept.3259+ return yield* snapshotOf(providerThread, sessionId);
juliusmarminge added labelsmacroscope-review
- apps/server/src/orchestration-v2/Adapters/OpenCode2AdapterV2.ts line 3398OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -3006,0 +3378,21 @@3378+ providerSessionId: input.providerSessionId,3379+ appThreadId: forkInput.targetThreadId,3380+ ownerNodeId: forkInput.ownerNodeId ?? null,3381+ nativeThreadRef: ref(forked.id),3382+ nativeConversationHeadRef: null,3383+ status: "idle",3384+ firstRunOrdinal: null,3385+ lastRunOrdinal: null,3386+ handoffIds: [],3387+ forkedFrom: {3388+ providerThreadId: source.id,3389+ ...(forkInput.providerTurnId === undefined3390+ ? {}3391+ : { providerTurnId: forkInput.providerTurnId }),3392+ },3393+ createdAt,3394+ updatedAt: createdAt,3395+ };3396+ // A fork keeps the source's location, which is the target thread's3397+ // worktree too: a fork inherits it. A later move reaches it on resume.3398+ const state = register(providerThread, forked, forked.location.directory);
- apps/server/src/orchestration-v2/Adapters/OpenCode2AdapterV2.ts line 3431Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -3024,0 +3328,21 @@3328+ (yield* client.session.active().pipe(Effect.timeout(ACTIVE_CHECK_TIMEOUT)));3329+ if (3330+ state !== undefined &&3331+ (state.active !== undefined || stillStopping || hasBackground(state))3332+ ) {3333+ return yield* new ProviderAdapter.ProviderAdapterProtocolError({3334+ driver: OPENCODE_PROVIDER,3335+ detail: `Cannot roll back OpenCode session ${sessionId} while it is still working`,3336+ });3337+ }3338+ // A session this runtime has not loaded yet is loaded the way a turn3339+ // would load it, which stops anything it still waits on.3340+ if (state === undefined) yield* runtime.resumeThread({ providerThread });3341+ const boundary = yield* boundaryAfter(3342+ rollbackInput.providerThreadTurns,3343+ providerThread.id,3344+ target.type === "provider_turn" ? target.providerTurn : undefined,3345+ yield* userMessages(sessionId),3346+ );3347+ if (boundary !== null) {3348+ yield* rollBackTo(sessionId, SessionMessage.ID.make(boundary));
- apps/server/src/orchestration-v2/Adapters/OpenCode2AdapterV2.ts line 3467Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -3025,4 +3370,19 @@3370+ if (turn.ordinal > keptOrdinal) loaded?.providerTurns.delete(id);3371+ }3372+ // The snapshot's head is the last user message OpenCode kept.3373+ return yield* snapshotOf(providerThread, sessionId);3374+ }).pipe(3375+ exclusive(rollbackInput.providerThread),3376+ Effect.mapError((cause) =>3377+ isProviderAdapterError(cause)3378+ ? cause3379+ : new ProviderAdapter.ProviderAdapterRollbackThreadError({3380+ driver,3381+ providerThreadId: rollbackInput.providerThread.id,3382+ checkpointId: rollbackInput.target.checkpointId,3383+ cause,3384+ }),3385+ ),30253386 ),30263387 forkThread: (forkInput) =>3027- Effect.fail(3028- new ProviderAdapter.ProviderAdapterForkThreadError({3388+ Effect.gen(function* () {apps/server/src/orchestration-v2/Adapters/OpenCode2AdapterV2.ts line 2591Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -2350,0 +2520,21 @@2520+ Effect.tapCause((cause) =>2521+ Effect.logWarning("Could not take back an OpenCode steer.", cause),2522+ ),2523+ Effect.exit,2524+ Effect.map(Exit.isSuccess),2525+ );2526+ if (cancelled) state.strandedSteers.delete(inboxID);2527+ }2528+ });2529+2530+ /**2531+ * Clears a staged revert and waits out the empty execution `clear` runs,2532+ * which is no turn of T3's. Clearing with nothing staged is a no-op.2533+ */2534+ const clearRevert = Effect.fnUntraced(function* (sessionId: string) {2535+ const settled = yield* Deferred.make<void>();2536+ clearing.set(sessionId, settled);2537+ yield* client.session.revert2538+ .clear({ sessionID: Session.ID.make(sessionId) })2539+ .pipe(2540+ Effect.andThen(Deferred.await(settled)),
- apps/server/src/orchestration-v2/Adapters/OpenCode2AdapterV2.ts lines 3437 to 3440Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -3024,0 +3352,21 @@3352+ detail: `Cannot roll back OpenCode session ${sessionId} while it is still working`,3353+ });3354+ }3355+ // A session this runtime has not loaded yet is loaded the way a turn3356+ // would load it, which stops anything it still waits on.3357+ if (state === undefined) yield* runtime.resumeThread({ providerThread });3358+ const boundary = yield* boundaryAfter(3359+ rollbackInput.providerThreadTurns,3360+ providerThread.id,3361+ target.type === "provider_turn" ? target.providerTurn : undefined,3362+ yield* userMessages(sessionId),3363+ );3364+ if (boundary !== null) {3365+ yield* rollBackTo(sessionId, SessionMessage.ID.make(boundary));3366+ }3367+ // The turns after the target left the history with their messages.3368+ const keptOrdinal = target.type === "provider_turn" ? target.providerTurn.ordinal : 0;3369+ const loaded = threads.get(sessionId);3370+ for (const [id, turn] of loaded?.providerTurns ?? []) {3371+ if (turn.ordinal > keptOrdinal) loaded?.providerTurns.delete(id);3372+ }apps/server/src/orchestration-v2/Adapters/OpenCode2AdapterV2.ts line 3357OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -3024,0 +3337,21 @@3337+ const state = threads.get(sessionId);3338+ // A run a timed-out Stop or an unanswered request left behind may3339+ // still be going, and a cut made meanwhile would race it, so the3340+ // server is asked first. Its end may still be on the stream either3341+ // way, so the flag stays for the next turn to skip it.3342+ const stillStopping =3343+ state?.unsettled === true &&3344+ sessionId in3345+ (yield* client.session.active().pipe(Effect.timeout(ACTIVE_CHECK_TIMEOUT)));3346+ if (3347+ state !== undefined &&3348+ (state.active !== undefined || stillStopping || hasBackground(state))3349+ ) {3350+ return yield* new ProviderAdapter.ProviderAdapterProtocolError({3351+ driver: OPENCODE_PROVIDER,3352+ detail: `Cannot roll back OpenCode session ${sessionId} while it is still working`,3353+ });3354+ }3355+ // A session this runtime has not loaded yet is loaded the way a turn3356+ // would load it, which stops anything it still waits on.3357+ if (state === undefined) yield* runtime.resumeThread({ providerThread });
- @@ -891,3 +927,18 @@891927 },892928 ],893929 },930+ {931+ name: "opencode2_nested_background",932+ buildInput: openCode2NestedBackgroundInput,933+ providers: [934+ {935+ driver: ProviderDriverKind.make("opencode"),936+ transcriptFile: new URL(937+ "./opencode2_nested_background/opencode_transcript.ndjson",938+ import.meta.url,939+ ),940+ modelSelection: {941+ instanceId: OPENCODE2_MODEL_SELECTION.instanceId,942+ model: "openrouter/deepseek/deepseek-v4-flash",943+ },944+ assertOutput: assertOpenCode2NestedBackgroundOutput,
juliusmarminge merged this40920d9
juliusmarminge closed this
Sign in to join this conversation.
macroscopeapp[bot](GitHub profile, opens in a new tab) reviewed