Skip to content

Commit ae1ccb1

Browse files
authored
fix(opencode): forks from compacted threads no longer fail (pingdotgg#29221)
1 parent e051363 commit ae1ccb1

3 files changed

Lines changed: 83 additions & 11 deletions

File tree

‎apps/server/src/orchestration-v2/Adapters/OpenCode2AdapterV2.test.ts‎

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3333,6 +3333,55 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
33333333
}).pipe(Effect.scoped),
33343334
);
33353335

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+
33363385
it.effect("clears a staged revert whose commit failed, before the next prompt too", () =>
33373386
Effect.gen(function* () {
33383387
const kept = "msg_t3_turn_run-attempt:kept:1";
@@ -4120,8 +4169,22 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
41204169
Effect.gen(function* () {
41214170
const FORK = "ses_f1484db83ffeLGtrRCFimo1H0e";
41224171
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: {} }),
41254188
out("session.fork", { sessionID: SESSION, before: "msg_report" }),
41264189
replyData("session.fork", sessionInfo({ id: FORK })),
41274190
out("session.update", { sessionID: FORK, permissions: "<any>" }),

‎apps/server/src/orchestration-v2/testkit/fixtures/opencode2_fork/opencode_transcript.ndjson‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,12 @@
4141
{"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}}}}
4242
{"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}}}}}}
4343
{"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"}}}
4545
{"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"}}}
4747
{"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": {}}}}
4850
{"type": "expect_outbound", "label": "session.fork", "frame": {"type": "session.fork", "input": {"sessionID": "ses_f1484ea8affe5aTo8CcgbGIszj", "before": "msg_0eb7b1f9f001g95Bduh3bi8syS"}}}
4951
{"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"}]}}}}
5052
{"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‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2935,12 +2935,19 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
29352935
);
29362936
});
29372937

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+
),
29442951
Stream.runCollect,
29452952
Effect.map(
29462953
(messages) =>
@@ -4018,7 +4025,7 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
40184025
forkInput.sourceProviderTurns ?? [],
40194026
source.id,
40204027
selected,
4021-
yield* userMessages(sourceId),
4028+
yield* userMessages(sourceId, ["user", "synthetic"]),
40224029
);
40234030
const forked = yield* client.session.fork({
40244031
sessionID: Session.ID.make(sourceId),

0 commit comments

Comments
 (0)