Skip to content

Commit 1ba3c78

Browse files
TonybynMp4claudecoderabbitai[bot]juliusmarminge
authored
fix(web): Claude task lists stay in the composer between turns (pingdotgg#18392)
Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> Co-authored-by: Julius Marminge <[email protected]>
1 parent 2767012 commit 1ba3c78

10 files changed

Lines changed: 561 additions & 35 deletions

File tree

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

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6380,6 +6380,20 @@ describe("ClaudeAdapterV2 background wake turns", () => {
63806380
harness.sdkMessages,
63816381
toolResults("00000000-0000-4000-8000-000000000505", ["tool-todo-2", "tool-exit-plan-1"]),
63826382
);
6383+
yield* Queue.offer(
6384+
harness.sdkMessages,
6385+
assistantTools("00000000-0000-4000-8000-000000000509", [
6386+
{
6387+
id: "tool-todo-3",
6388+
name: "TodoWrite",
6389+
input: { todos: [{ content: "Implement", status: "pending" }] },
6390+
},
6391+
]),
6392+
);
6393+
yield* Queue.offer(
6394+
harness.sdkMessages,
6395+
toolResults("00000000-0000-4000-8000-000000000510", ["tool-todo-3"]),
6396+
);
63836397
yield* Queue.offer(
63846398
harness.sdkMessages,
63856399
makeResultFrame({
@@ -6401,7 +6415,15 @@ describe("ClaudeAdapterV2 background wake turns", () => {
64016415
);
64026416
const todoItems = [...items.values()].filter((item) => item.type === "todo_list");
64036417
const proposedItems = [...items.values()].filter((item) => item.type === "proposed_plan");
6404-
assert.lengthOf(todoItems, 2);
6418+
assert.lengthOf(todoItems, 3);
6419+
assert.equal(todoItems[1]?.status, "completed");
6420+
assert.deepEqual(todoItems[1]?.steps, [
6421+
{ id: "todo-0", text: "Inspect", status: "completed" },
6422+
]);
6423+
assert.deepEqual(todoItems[2]?.steps, [
6424+
{ id: "todo-0", text: "Implement", status: "pending" },
6425+
]);
6426+
assert.equal(todoItems[1]?.runId, todoItems[2]?.runId);
64056427
assert.lengthOf(proposedItems, 1);
64066428
assert.equal(
64076429
proposedItems[0]?.type === "proposed_plan" && proposedItems[0].markdown,
@@ -6419,15 +6441,15 @@ describe("ClaudeAdapterV2 background wake turns", () => {
64196441
item.type === "dynamic_tool" && item.nativeItemRef?.nativeId === "tool-exit-plan-1",
64206442
),
64216443
);
6444+
const proposedPlan = [...plans.values()].find((plan) => plan.kind === "proposed_plan");
6445+
assert.equal(proposedPlan?.status, "active");
6446+
assert.deepEqual(harness.permissionModeChanges, ["bypassPermissions"]);
64226447
assert.deepEqual(
64236448
[...plans.values()]
64246449
.filter((plan) => plan.kind === "todo_list")
64256450
.map((plan) => plan.status),
6426-
["superseded", "completed"],
6451+
["superseded", "superseded", "active"],
64276452
);
6428-
const proposedPlan = [...plans.values()].find((plan) => plan.kind === "proposed_plan");
6429-
assert.equal(proposedPlan?.status, "active");
6430-
assert.deepEqual(harness.permissionModeChanges, ["bypassPermissions"]);
64316453
}).pipe(
64326454
Effect.provide(
64336455
Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),

‎apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5953,7 +5953,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
59535953
[
59545954
...(previousPlan === undefined ||
59555955
previousPlan.id === plan.id ||
5956-
previousPlan.status === "completed"
5956+
(previousPlan.kind !== "todo_list" && previousPlan.status === "completed")
59575957
? []
59585958
: [
59595959
emitProviderEvent({

‎apps/server/src/orchestration-v2/ProjectionStore.test.ts‎

Lines changed: 180 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1609,6 +1609,8 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
16091609
const now = yield* DateTime.now;
16101610
const nowIso = DateTime.formatIso(now);
16111611
const threadId = ThreadId.make("thread:notification-turn-pages");
1612+
const providerInstanceId = ProviderInstanceId.make("claudeAgent");
1613+
const modelSelection = { instanceId: providerInstanceId, model: "claude-sonnet-4-6" };
16121614
yield* projectionStore.apply({
16131615
id: EventId.make("event:notification-turn-pages:thread"),
16141616
type: "thread.created",
@@ -1639,6 +1641,53 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
16391641
},
16401642
});
16411643

1644+
const providerThreadId = ProviderThreadId.make("provider-thread:notification-turn-pages");
1645+
yield* projectionStore.apply({
1646+
id: EventId.make("event:notification-turn-pages:provider-thread"),
1647+
type: "provider-thread.updated",
1648+
threadId,
1649+
occurredAt: now,
1650+
payload: {
1651+
id: providerThreadId,
1652+
driver: ProviderDriverKind.make("claudeAgent"),
1653+
providerInstanceId,
1654+
providerSessionId: null,
1655+
appThreadId: threadId,
1656+
ownerNodeId: null,
1657+
nativeThreadRef: null,
1658+
nativeConversationHeadRef: null,
1659+
status: "idle",
1660+
firstRunOrdinal: 1,
1661+
lastRunOrdinal: 412,
1662+
handoffIds: [],
1663+
forkedFrom: null,
1664+
createdAt: now,
1665+
updatedAt: now,
1666+
},
1667+
});
1668+
const planId = PlanId.make("plan:notification-turn-pages");
1669+
const olderPlanId = PlanId.make("plan:notification-turn-pages:older");
1670+
for (const [id, run] of [
1671+
[olderPlanId, 1],
1672+
[planId, 2],
1673+
] as const) {
1674+
yield* projectionStore.apply({
1675+
id: EventId.make(`event:notification-turn-pages:plan:${run}`),
1676+
type: "plan.updated",
1677+
threadId,
1678+
occurredAt: now,
1679+
payload: {
1680+
id,
1681+
threadId,
1682+
runId: RunId.make(`run:notification-turn-pages:${run}`),
1683+
nodeId: NodeId.make(`node:notification-turn-pages:${run}`),
1684+
kind: "todo_list",
1685+
status: "active",
1686+
steps: [{ id: "unfinished", text: "Finish the task", status: "pending" }],
1687+
},
1688+
});
1689+
}
1690+
16421691
const startedRuns = 412;
16431692
const allIds: string[] = [];
16441693
const startIds: string[] = [];
@@ -1693,14 +1742,14 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
16931742
run_id, thread_id, ordinal, provider, provider_thread_id, status,
16941743
requested_at, completed_at, payload_json
16951744
) VALUES (
1696-
${runId}, ${threadId}, ${run}, 'codex', NULL, ${status}, ${nowIso}, ${nowIso},
1745+
${runId}, ${threadId}, ${run}, 'claudeAgent', ${providerThreadId}, ${status}, ${nowIso}, ${nowIso},
16971746
${encodeUnknownJsonString({
16981747
id: runId,
16991748
threadId,
17001749
ordinal: run,
17011750
providerInstanceId,
17021751
modelSelection,
1703-
providerThreadId: null,
1752+
providerThreadId,
17041753
userMessageId: `message:notification-turn-pages:${run}`,
17051754
rootNodeId: `node:notification-turn-pages:${run}`,
17061755
activeAttemptId: null,
@@ -1763,6 +1812,22 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
17631812
snapshotSequence: 0,
17641813
});
17651814
assert.strictEqual(bounded.projection.visibleTurnItems[0]?.sourceItemId, startIds[262]);
1815+
assert.deepEqual(
1816+
bounded.projection.plans.map((plan) => plan.id),
1817+
[planId, olderPlanId],
1818+
);
1819+
assert.deepEqual(
1820+
{
1821+
oldRunIds: bounded.projection.runs
1822+
.filter((run) => run.ordinal < 261)
1823+
.map((run) => run.id),
1824+
providerThreadIds: bounded.projection.providerThreads.map((thread) => thread.id),
1825+
},
1826+
{
1827+
oldRunIds: [RunId.make("run:notification-turn-pages:2")],
1828+
providerThreadIds: [providerThreadId],
1829+
},
1830+
);
17661831
const loaded = bounded.projection.visibleTurnItems.map((row) => String(row.sourceItemId));
17671832
let cursor = bounded.historyCursor;
17681833
for (let pages = 0; cursor !== null; pages += 1) {
@@ -1784,6 +1849,119 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
17841849
cursor = page.nextCursor;
17851850
}
17861851
assert.deepEqual(loaded, allIds);
1852+
1853+
const completedPlan = {
1854+
id: planId,
1855+
threadId,
1856+
runId: RunId.make("run:notification-turn-pages:2"),
1857+
nodeId: NodeId.make("node:notification-turn-pages:2"),
1858+
kind: "todo_list" as const,
1859+
status: "completed" as const,
1860+
explanation: "All tasks complete",
1861+
steps: [{ id: "unfinished", text: "Finish the task", status: "completed" as const }],
1862+
};
1863+
yield* projectionStore.apply({
1864+
id: EventId.make("event:notification-turn-pages:plan:completed"),
1865+
type: "plan.updated",
1866+
threadId,
1867+
occurredAt: now,
1868+
payload: completedPlan,
1869+
});
1870+
const afterCompletion = yield* projectionStore.getThreadSnapshotWindow(threadId, {
1871+
rowLimit: 77,
1872+
userTurnLimit: 10,
1873+
});
1874+
const completedBounded = buildBoundedThreadProjection({
1875+
projection: afterCompletion.projection,
1876+
snapshotSequence: 0,
1877+
});
1878+
assert.deepEqual(
1879+
{
1880+
plans: completedBounded.projection.plans,
1881+
oldRunIds: completedBounded.projection.runs
1882+
.filter((run) => run.ordinal < 261)
1883+
.map((run) => run.id),
1884+
providerThreadIds: completedBounded.projection.providerThreads.map((thread) => thread.id),
1885+
},
1886+
{
1887+
plans: [completedPlan, bounded.projection.plans.find((plan) => plan.id === olderPlanId)],
1888+
oldRunIds: [completedPlan.runId],
1889+
providerThreadIds: [providerThreadId],
1890+
},
1891+
);
1892+
1893+
// Older versions left completed predecessors unsuperseded; IDs do not order replacements.
1894+
const newerPlan = {
1895+
...completedPlan,
1896+
id: PlanId.make("plan:legacy-newer"),
1897+
status: "active" as const,
1898+
explanation: "More work remains",
1899+
steps: [{ id: "new", text: "Finish the new task", status: "pending" as const }],
1900+
};
1901+
yield* projectionStore.apply({
1902+
id: EventId.make("event:notification-turn-pages:plan:legacy-newer"),
1903+
type: "plan.updated",
1904+
threadId,
1905+
occurredAt: now,
1906+
payload: newerPlan,
1907+
});
1908+
for (const [plan, itemOrdinal] of [
1909+
[completedPlan, 7],
1910+
[newerPlan, 8],
1911+
] as const) {
1912+
yield* projectionStore.apply({
1913+
id: EventId.make(`event:notification-turn-pages:legacy-item:${itemOrdinal}`),
1914+
type: "turn-item.updated",
1915+
threadId,
1916+
runId: plan.runId,
1917+
nodeId: plan.nodeId,
1918+
occurredAt: now,
1919+
payload: {
1920+
id: TurnItemId.make(`turn-item:notification-turn-pages:${itemOrdinal}`),
1921+
threadId,
1922+
runId: plan.runId,
1923+
nodeId: plan.nodeId,
1924+
providerThreadId,
1925+
providerTurnId: null,
1926+
nativeItemRef: null,
1927+
parentItemId: null,
1928+
ordinal: itemOrdinal,
1929+
status: "completed",
1930+
title: null,
1931+
startedAt: now,
1932+
completedAt: now,
1933+
updatedAt: now,
1934+
type: "todo_list",
1935+
planId: plan.id,
1936+
steps: plan.steps,
1937+
explanation: plan.explanation,
1938+
},
1939+
});
1940+
}
1941+
const legacySnapshot = yield* projectionStore.getThreadSnapshotWindow(threadId, {
1942+
rowLimit: 77,
1943+
userTurnLimit: 10,
1944+
});
1945+
assert.isTrue(legacySnapshot.projection.turnItems.every((item) => item.ordinal > 8));
1946+
assert.deepEqual(
1947+
legacySnapshot.projection.plans.findLast((plan) => plan.runId === newerPlan.runId),
1948+
newerPlan,
1949+
);
1950+
const legacyBounded = buildBoundedThreadProjection({
1951+
projection: legacySnapshot.projection,
1952+
snapshotSequence: 0,
1953+
});
1954+
assert.deepEqual(
1955+
{
1956+
ownerPlans: legacyBounded.projection.plans.filter(
1957+
(plan) => plan.runId === newerPlan.runId,
1958+
),
1959+
oldRunIds: legacyBounded.projection.runs
1960+
.filter((run) => run.ordinal < 261)
1961+
.map((run) => run.id),
1962+
},
1963+
{ ownerPlans: [newerPlan], oldRunIds: [newerPlan.runId] },
1964+
);
17871965
}),
17881966
);
17891967

‎apps/server/src/orchestration-v2/ProjectionStore.ts‎

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3091,6 +3091,54 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
30913091
const cohortPlanIds = cohortJson("planId");
30923092
const cohortCheckpointIds = cohortJson("checkpointId");
30933093
const cohortHandoffIds = cohortJson("contextHandoffId");
3094+
// A newer completion must keep an older active list from resurfacing after paging.
3095+
const [latestTodoList] =
3096+
window === undefined ||
3097+
(fields !== undefined &&
3098+
!fields.some(
3099+
(field) => field === "plans" || field === "runs" || field === "providerThreads",
3100+
))
3101+
? []
3102+
: yield* sql<{
3103+
readonly plan_id: string;
3104+
readonly run_id: string;
3105+
readonly provider_thread_id: string | null;
3106+
readonly last_plan_id: string;
3107+
}>`
3108+
WITH latest_owner AS MATERIALIZED (
3109+
SELECT plan.plan_id, owner.run_id, owner.provider_thread_id
3110+
FROM orchestration_v2_projection_plans AS plan
3111+
INNER JOIN orchestration_v2_projection_runs AS owner
3112+
ON owner.thread_id = plan.thread_id AND owner.run_id = plan.run_id
3113+
WHERE plan.thread_id = ${threadId}
3114+
AND plan.kind = 'todo_list' AND plan.status <> 'superseded'
3115+
ORDER BY owner.ordinal DESC, plan.plan_id DESC
3116+
LIMIT 1
3117+
)
3118+
SELECT COALESCE(CASE WHEN EXISTS (
3119+
SELECT 1 FROM orchestration_v2_projection_plans
3120+
WHERE thread_id = ${threadId} AND run_id = owner.run_id
3121+
AND kind = 'todo_list' AND status <> 'superseded'
3122+
AND plan_id <> owner.plan_id
3123+
) THEN (
3124+
-- Plan UUIDs cannot order replacements within a run.
3125+
SELECT plan.plan_id
3126+
FROM orchestration_v2_projection_turn_items AS item
3127+
CROSS JOIN orchestration_v2_projection_plans AS plan
3128+
ON plan.plan_id = json_extract(item.payload_json, '$.planId')
3129+
WHERE item.run_id = owner.run_id AND item.thread_id = ${threadId}
3130+
AND item.type = 'todo_list'
3131+
AND plan.run_id = owner.run_id AND plan.thread_id = ${threadId}
3132+
AND plan.kind = 'todo_list' AND plan.status <> 'superseded'
3133+
ORDER BY item.ordinal DESC
3134+
LIMIT 1
3135+
) END, owner.plan_id) AS plan_id, owner.run_id, owner.provider_thread_id,
3136+
(
3137+
SELECT MAX(plan_id) FROM orchestration_v2_projection_plans
3138+
WHERE thread_id = ${threadId} AND run_id = owner.run_id
3139+
) AS last_plan_id
3140+
FROM latest_owner AS owner
3141+
`;
30943142

30953143
const [
30963144
thread,
@@ -3126,7 +3174,18 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
31263174
WHERE thread_id = ${threadId}
31273175
AND (status IN ('queued','preparing','starting','running','waiting')
31283176
OR run_id IN (SELECT value FROM json_each(${cohortRunIds}))
3129-
OR run_id = ${window.requiredRunId ?? null})
3177+
OR run_id = ${window.requiredRunId ?? null}
3178+
-- Queued or cancelled turns must not hide the latest actual session.
3179+
OR run_id = ${latestTodoList?.run_id ?? null}
3180+
OR run_id = (
3181+
SELECT run_id FROM orchestration_v2_projection_runs
3182+
WHERE thread_id = ${threadId}
3183+
AND provider_thread_id IS NOT NULL
3184+
AND json_extract(payload_json, '$.startedAt') IS NOT NULL
3185+
AND ${latestTodoList?.plan_id ?? null} IS NOT NULL
3186+
ORDER BY ordinal DESC
3187+
LIMIT 1
3188+
))
31303189
ORDER BY ordinal ASC
31313190
`,
31323191
fields !== undefined && !fields.includes("attempts")
@@ -3246,6 +3305,8 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
32463305
WHERE (thread_id = ${threadId} AND status = 'active')
32473306
OR provider_thread_id IN (SELECT value FROM json_each(${cohortProviderThreadIds}))
32483307
OR owner_node_id IN (SELECT value FROM json_each(${cohortNodeIds}))
3308+
OR (thread_id = ${threadId}
3309+
AND provider_thread_id = ${latestTodoList?.provider_thread_id ?? null})
32493310
ORDER BY COALESCE(first_run_ordinal, 0), provider_thread_id ASC
32503311
`,
32513312
fields !== undefined && !fields.includes("providerTurns")
@@ -3322,11 +3383,15 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
33223383
WHERE plan.thread_id = ${threadId}
33233384
AND (
33243385
plan.status = 'active'
3386+
OR plan.plan_id = ${latestTodoList?.plan_id ?? null}
33253387
OR plan.plan_id IN (
33263388
SELECT value FROM json_each(${cohortPlanIds})
33273389
)
33283390
)
3329-
ORDER BY plan_id ASC
3391+
-- Keep the selected artifact last within its run for equal-run consumers.
3392+
ORDER BY CASE WHEN plan_id = ${latestTodoList?.plan_id ?? null}
3393+
THEN ${latestTodoList?.last_plan_id ?? null} ELSE plan_id END ASC,
3394+
plan_id = ${latestTodoList?.plan_id ?? null} ASC
33303395
`,
33313396
fields !== undefined && !fields.includes("checkpointScopes")
33323397
? Effect.succeed([])

0 commit comments

Comments
 (0)