Skip to content

Commit 42d0d52

Browse files
committed
feat(opencode): project native todo updates into checklists
- Emit stable todo plans and checklist items after successful updates - Recover todo state from tool results received during reconnect
1 parent 524a531 commit 42d0d52

2 files changed

Lines changed: 258 additions & 2 deletions

File tree

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

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2891,6 +2891,174 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
28912891
}).pipe(Effect.scoped),
28922892
);
28932893

2894+
it.effect("projects successful native todo updates into one checklist", () =>
2895+
Effect.gen(function* () {
2896+
const updates = [
2897+
[
2898+
{ content: " Inspect code ", status: "completed" },
2899+
{ content: "Implement", status: "in_progress" },
2900+
{ content: "Verify", status: "pending" },
2901+
{ content: "Abandoned", status: "cancelled" },
2902+
],
2903+
[{ content: "Inspect code", status: "completed" }],
2904+
[{ content: "Abandoned", status: "cancelled" }],
2905+
[],
2906+
[{ content: "Follow up", status: "pending" }],
2907+
];
2908+
const tool = (id: string) => ({
2909+
sessionID: SESSION,
2910+
assistantMessageID: "msg_assistant",
2911+
id,
2912+
});
2913+
const call = (id: string, todos: unknown): ReadonlyArray<ProviderReplayEntry> => [
2914+
event("session.tool.input.started", { ...tool(id), name: "todowrite" }),
2915+
event("session.tool.called", { ...tool(id), input: { todos }, executed: false }),
2916+
];
2917+
const { runtime, thread } = yield* resumed([
2918+
out("session.prompt", { sessionID: SESSION, text: "<any>" }),
2919+
promptAccepted,
2920+
...call("call-denied", [{ content: "Not saved", status: "pending" }]),
2921+
event(
2922+
"session.tool.failed",
2923+
{
2924+
...tool("call-denied"),
2925+
error: { type: "unknown", message: "Unable to update todos" },
2926+
executed: false,
2927+
},
2928+
undefined,
2929+
2,
2930+
),
2931+
...call("call-malformed", [{ content: "Not saved", status: "pending" }]),
2932+
event(
2933+
"session.tool.success",
2934+
{
2935+
...tool("call-malformed"),
2936+
content: [{ type: "text", text: "Unrecognized todo result" }],
2937+
metadata: { todos: [{ content: "Unknown", status: "new-status" }] },
2938+
executed: false,
2939+
},
2940+
undefined,
2941+
2,
2942+
),
2943+
...updates.flatMap((todos, index) => [
2944+
...call(`call-todo-${index}`, todos),
2945+
event(
2946+
"session.tool.success",
2947+
{
2948+
...tool(`call-todo-${index}`),
2949+
content: [{ type: "text", text: "Todos updated" }],
2950+
metadata: { todos },
2951+
executed: false,
2952+
},
2953+
undefined,
2954+
2,
2955+
),
2956+
]),
2957+
event("session.execution.succeeded", { sessionID: SESSION }),
2958+
]);
2959+
const events = yield* runtime.events.pipe(
2960+
Stream.takeUntil((event) => event.type === "turn.terminal"),
2961+
Stream.runCollect,
2962+
Effect.forkScoped,
2963+
);
2964+
yield* runtime.startTurn(turnInput(thread));
2965+
const collected = yield* Fiber.join(events);
2966+
const plans = collected.flatMap((event) =>
2967+
event.type === "plan.updated" && event.plan.kind === "todo_list" ? [event.plan] : [],
2968+
);
2969+
assert.deepEqual(
2970+
plans.map((plan) => plan.steps.map((step) => [step.text, step.status])),
2971+
[
2972+
[
2973+
["Inspect code", "completed"],
2974+
["Implement", "running"],
2975+
["Verify", "pending"],
2976+
],
2977+
[["Inspect code", "completed"]],
2978+
[],
2979+
[],
2980+
[["Follow up", "pending"]],
2981+
],
2982+
);
2983+
assert.equal(new Set(plans.map((plan) => plan.id)).size, 1);
2984+
assert.equal(plans[0]?.steps[0]?.id, plans[1]?.steps[0]?.id);
2985+
assert.deepEqual(
2986+
plans.map((plan) => plan.status),
2987+
["active", "completed", "completed", "completed", "active"],
2988+
);
2989+
const items = collected.flatMap((event) =>
2990+
event.type === "turn_item.updated" ? [event.turnItem] : [],
2991+
);
2992+
const checklists = items.filter((item) => item.type === "todo_list");
2993+
assert.equal(new Set(checklists.map((item) => item.id)).size, 1);
2994+
assert.deepEqual(
2995+
checklists.map((item) => item.steps),
2996+
plans.map((plan) => plan.steps),
2997+
);
2998+
assert.deepEqual(
2999+
checklists.map((item) => item.status),
3000+
["completed", "completed", "completed", "completed", "completed"],
3001+
);
3002+
assert.isFalse(
3003+
items.some(
3004+
(item) =>
3005+
item.type === "dynamic_tool" && item.nativeItemRef?.nativeId?.startsWith("call-todo-"),
3006+
),
3007+
);
3008+
assert.deepInclude(
3009+
items.find((item) => item.nativeItemRef?.nativeId === "call-denied"),
3010+
{ type: "dynamic_tool", status: "failed" },
3011+
);
3012+
assert.deepInclude(
3013+
items.find((item) => item.nativeItemRef?.nativeId === "call-malformed"),
3014+
{ type: "dynamic_tool", status: "completed" },
3015+
);
3016+
}).pipe(Effect.scoped),
3017+
);
3018+
3019+
it.effect("recovers native todo updates missed during reconnect", () =>
3020+
Effect.gen(function* () {
3021+
const todos = [{ content: "Saved during disconnect", status: "in_progress" }];
3022+
const { runtime, thread } = yield* resumed([
3023+
out("session.prompt", { sessionID: SESSION, id: PROMPT_ID, text: "<any>" }),
3024+
promptAccepted,
3025+
...reconnected({}, "succeeded", {
3026+
content: [
3027+
{
3028+
type: "tool",
3029+
id: "call-todo-gap",
3030+
name: "todowrite",
3031+
executed: false,
3032+
state: {
3033+
status: "completed",
3034+
input: { todos },
3035+
content: [{ type: "text", text: "Todos updated" }],
3036+
metadata: { todos },
3037+
},
3038+
time: { created: 2, ran: 2, completed: 2 },
3039+
},
3040+
],
3041+
}),
3042+
]);
3043+
const events = yield* runtime.events.pipe(
3044+
Stream.takeUntil((event) => event.type === "turn.terminal"),
3045+
Stream.runCollect,
3046+
Effect.forkScoped,
3047+
);
3048+
yield* runtime.startTurn(turnInput(thread));
3049+
const collected = yield* Fiber.join(events);
3050+
const plans = collected.flatMap((event) =>
3051+
event.type === "plan.updated" && event.plan.kind === "todo_list" ? [event.plan] : [],
3052+
);
3053+
assert.equal(plans.length, 1);
3054+
assert.deepEqual(
3055+
plans[0]?.steps.map((step) => [step.text, step.status]),
3056+
[["Saved during disconnect", "running"]],
3057+
);
3058+
assert.include(turnItems(collected), "todo_list:completed");
3059+
}).pipe(Effect.scoped),
3060+
);
3061+
28943062
it.effect("stores a shell command that exits non-zero as failed", () =>
28953063
Effect.gen(function* () {
28963064
const tool = { sessionID: SESSION, assistantMessageID: "msg_assistant", id: "call-exit" };

‎packages/provider-opencode/src/server/v2/adapter.ts‎

Lines changed: 90 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,14 @@ import {
2727
type OrchestrationV2ProviderSession,
2828
type OrchestrationV2ProviderThread,
2929
type OrchestrationV2ProviderTurn,
30+
type OrchestrationV2PlanStep,
3031
type OrchestrationV2RuntimeRequest,
3132
type OrchestrationV2Subagent,
3233
type OrchestrationV2TurnItem,
3334
type OrchestrationV2UserInputQuestion,
3435
type MessageId,
3536
type ModelSelection,
37+
type PlanId,
3638
type ProviderApprovalDecision,
3739
type ProviderInstanceId,
3840
type RunId,
@@ -101,6 +103,16 @@ const NativeGoal = Schema.NullOr(
101103
);
102104
const decodeGoal = Schema.decodeUnknownEffect(NativeGoal);
103105

106+
const NativeTodoList = Schema.Struct({
107+
todos: Schema.Array(
108+
Schema.Struct({
109+
content: Schema.String,
110+
status: Schema.Literals(["pending", "in_progress", "completed", "cancelled"]),
111+
}),
112+
),
113+
});
114+
const isNativeTodoList = Schema.is(NativeTodoList);
115+
104116
const OpenCode2ProviderCapabilities = {
105117
sessions: {
106118
supportsMultipleProviderThreadsPerSession: true,
@@ -153,8 +165,8 @@ const OpenCode2ProviderCapabilities = {
153165
approvalsCanOriginateFromSubagents: true,
154166
},
155167
planning: {
156-
emitsPlanUpdated: false,
157-
emitsTodoList: false,
168+
emitsPlanUpdated: true,
169+
emitsTodoList: true,
158170
emitsProposedPlan: false,
159171
supportsStructuredQuestions: true,
160172
planDeltasHaveItemIds: false,
@@ -231,6 +243,7 @@ interface ActiveTurn {
231243
before: string | null | undefined;
232244
compaction: { readonly nativeId: string; readonly startedAt: DateTime.Utc } | undefined;
233245
compactions: number;
246+
todoPlanId: PlanId | undefined;
234247
retry:
235248
| {
236249
readonly retry: OrchestrationV2ProviderRetry;
@@ -1061,6 +1074,65 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
10611074
});
10621075
});
10631076

1077+
// Todo updates share the provider event span.
1078+
const emitTodo = Effect.fnUntraced(function* (
1079+
state: ThreadState,
1080+
turn: ActiveTurn,
1081+
todos: typeof NativeTodoList.Type.todos,
1082+
) {
1083+
turn.todoPlanId ??= yield* idAllocator.allocate.plan({
1084+
threadId: turn.input.threadId,
1085+
...(turn.input.runId === null ? {} : { runId: turn.input.runId }),
1086+
driver,
1087+
});
1088+
const nativeId = `${state.sessionId}:todo:${turn.providerTurn.id}`;
1089+
const now = yield* DateTime.now;
1090+
const startedAt = turn.startedAt.get(nativeId) ?? now;
1091+
turn.startedAt.set(nativeId, startedAt);
1092+
const steps: Array<OrchestrationV2PlanStep> = todos.flatMap((todo, index) =>
1093+
todo.status === "cancelled"
1094+
? []
1095+
: [
1096+
{
1097+
id: `${nativeId}:${index + 1}`,
1098+
text: todo.content.trim() || `Todo ${index + 1}`,
1099+
status:
1100+
todo.status === "completed"
1101+
? "completed"
1102+
: todo.status === "in_progress"
1103+
? "running"
1104+
: "pending",
1105+
},
1106+
],
1107+
);
1108+
const base = itemBase(state, turn, nativeId, "completed", startedAt, now, now);
1109+
yield* emitNode(state, turn, nativeId, "todo_list", "completed", startedAt, now);
1110+
yield* emit({
1111+
type: "plan.updated",
1112+
driver,
1113+
plan: {
1114+
id: turn.todoPlanId,
1115+
threadId: turn.input.threadId,
1116+
runId: turn.input.runId,
1117+
nodeId: base.nodeId,
1118+
kind: "todo_list",
1119+
status: steps.every((step) => step.status === "completed") ? "completed" : "active",
1120+
steps,
1121+
},
1122+
});
1123+
yield* emit({
1124+
type: "turn_item.updated",
1125+
driver,
1126+
turnItem: {
1127+
...base,
1128+
type: "todo_list",
1129+
title: "Todo list",
1130+
planId: turn.todoPlanId,
1131+
steps,
1132+
},
1133+
});
1134+
});
1135+
10641136
const emitTool = Effect.fnUntraced(function* (
10651137
state: ThreadState,
10661138
turn: ActiveTurn,
@@ -1074,6 +1146,21 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
10741146
) {
10751147
const tool = turn.tools.get(id);
10761148
if (tool === undefined) return;
1149+
if (tool.name === "todowrite") {
1150+
if (status === "running") return;
1151+
if (status === "completed" && isNativeTodoList(result?.metadata)) {
1152+
const projected = yield* emitTodo(state, turn, result.metadata.todos).pipe(
1153+
Effect.as(true),
1154+
Effect.catchTags({
1155+
IdAllocatorV2AllocationError: (error) =>
1156+
Effect.logWarning("Could not allocate an OpenCode todo list.", error).pipe(
1157+
Effect.as(false),
1158+
),
1159+
}),
1160+
);
1161+
if (projected) return;
1162+
}
1163+
}
10771164
const updatedAt = yield* DateTime.now;
10781165
const startedAt = turn.startedAt.get(id) ?? updatedAt;
10791166
const completedAt = status === "running" ? null : updatedAt;
@@ -1226,6 +1313,7 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
12261313
before: undefined,
12271314
compaction: undefined,
12281315
compactions: 0,
1316+
todoPlanId: undefined,
12291317
retry: undefined,
12301318
interrupted: false,
12311319
unsent: false,

0 commit comments

Comments
 (0)