@devries-cameron20 / t3code-1 / commits / ae1ccb171f
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(opencode): forks from compacted threads no longer fail (#29221)

Lakshmi Tanmay committed Nov 3, 2026 at 07:12 UTC ae1ccb171fd80dd6f2261687882279d79c55e106
3 files changed +83 -11
apps/server/src/orchestration-v2/Adapters/OpenCode2AdapterV2.test.ts
+65 -2
@@ -3333,6 +3333,55 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
3333 }).pipe(Effect.scoped),
3334 );
3335
3336 + it.effect("rejects unreadable rollback history before cutting the native session", () =>
3337 + Effect.gen(function* () {
3338 + const { runtime, thread } = yield* resumed([
3339 + out("message.list", { sessionID: SESSION, limit: "100", type: undefined }),
3340 + reply("message.list", {
3341 + data: [
3342 + { id: "msg_kept", time: { created: 1 }, text: "first", type: "user" },
3343 + {
3344 + id: "msg_compaction",
3345 + time: { created: 2 },
3346 + type: "compaction",
3347 + status: "completed",
3348 + reason: "auto",
3349 + summary: "First turn summary",
3350 + },
3351 + { id: "msg_dropped", time: { created: 3 }, text: "second", type: "user" },
3352 + ],
3353 + cursor: {},
3354 + }),
3355 + ]);
3356 + const now = yield* DateTime.now;
3357 + const turns = ["msg_kept", "msg_dropped"].map((nativeId, index) => ({
3358 + id: ProviderTurnId.make(`provider-turn:rollback-history:${index}`),
3359 + providerThreadId: thread.id,
3360 + nodeId: NodeId.make(`node:rollback-history:${index}`),
3361 + runAttemptId: RunAttemptId.make(`attempt:rollback-history:${index}`),
3362 + nativeTurnRef: { driver: OPENCODE_PROVIDER, nativeId, strength: "weak" as const },
3363 + ordinal: index + 1,
3364 + status: "completed" as const,
3365 + startedAt: now,
3366 + completedAt: now,
3367 + }));
3368 + const failure = yield* runtime
3369 + .rollbackThread({
3370 + providerThread: thread,
3371 + target: {
3372 + type: "provider_turn",
3373 + checkpointId: CheckpointId.make("checkpoint:rollback-history"),
3374 + appRunOrdinal: 1,
3375 + providerTurn: turns[0]!,
3376 + },
3377 + providerThreadTurns: turns,
3378 + })
3379 + .pipe(Effect.flip);
3380 + assert.equal(failure._tag, "ProviderAdapterRollbackThreadError");
3381 + assert.include(Cause.pretty(Cause.fail(failure)), '["recent"]');
3382 + }).pipe(Effect.scoped),
3383 + );
3384 +
3385 it.effect("clears a staged revert whose commit failed, before the next prompt too", () =>
3386 Effect.gen(function* () {
3387 const kept = "msg_t3_turn_run-attempt:kept:1";
@@ -4120,8 +4169,22 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
4169 Effect.gen(function* () {
4170 const FORK = "ses_f1484db83ffeLGtrRCFimo1H0e";
4171 const { runtime, thread, first, second } = yield* continued([
4123 - out("message.list", "<any>"),
4124 - reply("message.list", continuedHistory),
4172 + out("message.list", { sessionID: SESSION, limit: "100", type: "user" }),
4173 + reply("message.list", { data: [continuedHistory.data[1]], cursor: {} }),
4174 + out("message.list", { sessionID: SESSION, limit: "100", type: "synthetic" }),
4175 + reply("message.list", {
4176 + data: [
4177 + { id: "msg_later_report", time: { created: 3 }, text: "later", type: "synthetic" },
4178 + ],
4179 + cursor: { next: "synthetic-page-2" },
4180 + }),
4181 + out("message.list", {
4182 + sessionID: SESSION,
4183 + limit: "100",
4184 + type: "synthetic",
4185 + cursor: "synthetic-page-2",
4186 + }),
4187 + reply("message.list", { data: [continuedHistory.data[0]], cursor: {} }),
4188 out("session.fork", { sessionID: SESSION, before: "msg_report" }),
4189 replyData("session.fork", sessionInfo({ id: FORK })),
4190 out("session.update", { sessionID: FORK, permissions: "<any>" }),
apps/server/src/orchestration-v2/testkit/fixtures/opencode2_fork/opencode_transcript.ndjson
+4 -2
@@ -41,10 +41,12 @@
41 {"type": "emit_inbound", "label": "session.step.ended.2", "frame": {"type": "sdk.event", "event": {"id": "evt_0eb7b2461001qEeIO2dKdf1y1Y", "created": 1790657111137, "type": "session.step.ended", "location": {"directory": "<work>"}, "data": {"sessionID": "ses_f1484ea8affe5aTo8CcgbGIszj", "assistantMessageID": "msg_0eb7b1fb30011jHuDOQ5YiY6eH", "finish": "stop", "rawFinish": "stop", "cost": 0, "tokens": {"input": 16, "output": 3, "reasoning": 0, "cache": {"read": 9327, "write": 0}}, "snapshot": "99482b54df6361365fa0ed7363ccf07edaa7fc93", "files": []}, "durable": {"aggregateID": "ses_f1484ea8affe5aTo8CcgbGIszj", "seq": 20, "version": 1}}}}
42 {"type": "emit_inbound", "label": "session.usage.updated.2", "frame": {"type": "sdk.event", "event": {"id": "evt_0eb7b2465001TtO12Ofsv2KZTq", "created": 1790657111141, "type": "session.usage.updated", "data": {"sessionID": "ses_f1484ea8affe5aTo8CcgbGIszj", "cost": 0, "tokens": {"input": 8759, "output": 4, "reasoning": 99, "cache": {"read": 9813, "write": 0}}}}}}
43 {"type": "emit_inbound", "label": "session.execution.succeeded.2", "frame": {"type": "sdk.event", "event": {"id": "evt_0eb7b24660011TVz6Q64I8aAQ7", "created": 1790657111142, "type": "session.execution.succeeded", "data": {"sessionID": "ses_f1484ea8affe5aTo8CcgbGIszj"}, "durable": {"aggregateID": "ses_f1484ea8affe5aTo8CcgbGIszj", "seq": 21, "version": 1}}}}
44 -{"type": "expect_outbound", "label": "message.list", "frame": {"type": "message.list", "input": "<any>"}}
44 +{"type": "expect_outbound", "label": "message.list", "frame": {"type": "message.list", "input": {"sessionID": "ses_f1484ea8affe5aTo8CcgbGIszj", "limit": "100", "type": "user"}}}
45 {"type": "emit_inbound", "label": "message.list.response", "frame": {"type": "sdk.response", "operation": "message.list", "data": {"data": [{"id": "msg_0eb7b1f9f001g95Bduh3bi8syS", "time": {"created": 1790657109932}, "text": "Reply exactly TWO.", "type": "user"}, {"id": "msg_0eb7b1597001e14VBV3nmQYdFS", "time": {"created": 1790657107378}, "text": "Reply exactly ONE.", "type": "user"}], "cursor": {"next": "cursor-after-first-page"}}}}
46 -{"type": "expect_outbound", "label": "message.list.2", "frame": {"type": "message.list", "input": "<any>"}}
46 +{"type": "expect_outbound", "label": "message.list.2", "frame": {"type": "message.list", "input": {"sessionID": "ses_f1484ea8affe5aTo8CcgbGIszj", "limit": "100", "type": "user", "cursor": "cursor-after-first-page"}}}
47 {"type": "emit_inbound", "label": "message.list.response.2", "frame": {"type": "sdk.response", "operation": "message.list", "data": {"data": [], "cursor": {}}}}
48 +{"type": "expect_outbound", "label": "message.list.synthetic", "frame": {"type": "message.list", "input": {"sessionID": "ses_f1484ea8affe5aTo8CcgbGIszj", "limit": "100", "type": "synthetic"}}}
49 +{"type": "emit_inbound", "label": "message.list.synthetic.response", "frame": {"type": "sdk.response", "operation": "message.list", "data": {"data": [], "cursor": {}}}}
50 {"type": "expect_outbound", "label": "session.fork", "frame": {"type": "session.fork", "input": {"sessionID": "ses_f1484ea8affe5aTo8CcgbGIszj", "before": "msg_0eb7b1f9f001g95Bduh3bi8syS"}}}
51 {"type": "emit_inbound", "label": "session.fork.response", "frame": {"type": "sdk.response", "operation": "session.fork", "data": {"data": {"id": "ses_f1484db83ffeLGtrRCFimo1H0e", "fork": {"sessionID": "ses_f1484ea8affe5aTo8CcgbGIszj", "boundary": {"type": "before", "messageID": "msg_0eb7b1f9f001g95Bduh3bi8syS"}}, "projectID": "global", "model": {"id": "big-pickle", "providerID": "opencode", "variant": "default"}, "cost": 0, "tokens": {"input": 0, "output": 0, "reasoning": 0, "cache": {"read": 0, "write": 0}}, "time": {"created": 1790657111165, "updated": 1790657111165}, "title": "spike fork (fork #1)", "location": {"directory": "<work>"}, "permissions": [{"action": "*", "resource": "*", "effect": "allow"}, {"action": "subagent", "resource": "*", "effect": "deny"}]}}}}
52 {"type": "expect_outbound", "label": "session.update", "frame": {"type": "session.update", "input": {"sessionID": "ses_f1484db83ffeLGtrRCFimo1H0e", "permissions": [{"action": "*", "resource": "*", "effect": "allow"}, {"action": "t3-code-*", "resource": "*", "effect": "deny"}, {"action": "t3-code-thread_opencode2-fork_target_*", "resource": "*", "effect": "allow"}]}}}
packages/provider-opencode/src/server/v2/adapter.ts
+14 -7
@@ -2935,12 +2935,19 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
2935 );
2936 });
2937
2938 - // The API accepts one message type; boundaries require both user and synthetic messages.
2939 - const userMessages = (sessionId: string) =>
2940 - OpenCode2Client.paginate(
2941 - { sessionID: Session.ID.make(sessionId), limit: 100 },
2942 - client.message.list,
2943 - ).pipe(
2938 + // Rollback must validate full history before native mutation; forks only need prompts.
2939 + const userMessages = (sessionId: string, types?: ReadonlyArray<"user" | "synthetic">) =>
2940 + Stream.fromIterable(types ?? [undefined]).pipe(
2941 + Stream.flatMap((type) =>
2942 + OpenCode2Client.paginate(
2943 + {
2944 + sessionID: Session.ID.make(sessionId),
2945 + limit: 100,
2946 + ...(type === undefined ? {} : { type }),
2947 + },
2948 + client.message.list,
2949 + ),
2950 + ),
2951 Stream.runCollect,
2952 Effect.map(
2953 (messages) =>
@@ -4018,7 +4025,7 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
4025 forkInput.sourceProviderTurns ?? [],
4026 source.id,
4027 selected,
4021 - yield* userMessages(sourceId),
4028 + yield* userMessages(sourceId, ["user", "synthetic"]),
4029 );
4030 const forked = yield* client.session.fork({
4031 sessionID: Session.ID.make(sourceId),