Skip to content

Commit 32795e2

Browse files
fix(server): order retained task lists by their persisted items
1 parent f33c40b commit 32795e2

2 files changed

Lines changed: 111 additions & 9 deletions

File tree

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

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1551,6 +1551,79 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
15511551
providerThreadIds: [providerThreadId],
15521552
},
15531553
);
1554+
1555+
// Older versions left completed predecessors unsuperseded; IDs do not order replacements.
1556+
const newerPlan = {
1557+
...completedPlan,
1558+
id: PlanId.make("plan:legacy-newer"),
1559+
status: "active" as const,
1560+
explanation: "More work remains",
1561+
steps: [{ id: "new", text: "Finish the new task", status: "pending" as const }],
1562+
};
1563+
yield* projectionStore.apply({
1564+
id: EventId.make("event:notification-turn-pages:plan:legacy-newer"),
1565+
type: "plan.updated",
1566+
threadId,
1567+
occurredAt: now,
1568+
payload: newerPlan,
1569+
});
1570+
for (const [plan, itemOrdinal] of [
1571+
[completedPlan, 7],
1572+
[newerPlan, 8],
1573+
] as const) {
1574+
yield* projectionStore.apply({
1575+
id: EventId.make(`event:notification-turn-pages:legacy-item:${itemOrdinal}`),
1576+
type: "turn-item.updated",
1577+
threadId,
1578+
runId: plan.runId,
1579+
nodeId: plan.nodeId,
1580+
occurredAt: now,
1581+
payload: {
1582+
id: TurnItemId.make(`turn-item:notification-turn-pages:${itemOrdinal}`),
1583+
threadId,
1584+
runId: plan.runId,
1585+
nodeId: plan.nodeId,
1586+
providerThreadId,
1587+
providerTurnId: null,
1588+
nativeItemRef: null,
1589+
parentItemId: null,
1590+
ordinal: itemOrdinal,
1591+
status: "completed",
1592+
title: null,
1593+
startedAt: now,
1594+
completedAt: now,
1595+
updatedAt: now,
1596+
type: "todo_list",
1597+
planId: plan.id,
1598+
steps: plan.steps,
1599+
explanation: plan.explanation,
1600+
},
1601+
});
1602+
}
1603+
const legacySnapshot = yield* projectionStore.getThreadSnapshotWindow(threadId, {
1604+
rowLimit: 77,
1605+
userTurnLimit: 10,
1606+
});
1607+
assert.isTrue(legacySnapshot.projection.turnItems.every((item) => item.ordinal > 8));
1608+
assert.deepEqual(
1609+
legacySnapshot.projection.plans.findLast((plan) => plan.runId === newerPlan.runId),
1610+
newerPlan,
1611+
);
1612+
const legacyBounded = buildBoundedThreadProjection({
1613+
projection: legacySnapshot.projection,
1614+
snapshotSequence: 0,
1615+
});
1616+
assert.deepEqual(
1617+
{
1618+
ownerPlans: legacyBounded.projection.plans.filter(
1619+
(plan) => plan.runId === newerPlan.runId,
1620+
),
1621+
oldRunIds: legacyBounded.projection.runs
1622+
.filter((run) => run.ordinal < 261)
1623+
.map((run) => run.id),
1624+
},
1625+
{ ownerPlans: [newerPlan], oldRunIds: [newerPlan.runId] },
1626+
);
15541627
}),
15551628
);
15561629

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

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3023,15 +3023,41 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
30233023
readonly plan_id: string;
30243024
readonly run_id: string;
30253025
readonly provider_thread_id: string | null;
3026+
readonly last_plan_id: string;
30263027
}>`
3027-
SELECT plan.plan_id, owner.run_id, owner.provider_thread_id
3028-
FROM orchestration_v2_projection_plans AS plan
3029-
INNER JOIN orchestration_v2_projection_runs AS owner
3030-
ON owner.thread_id = plan.thread_id AND owner.run_id = plan.run_id
3031-
WHERE plan.thread_id = ${threadId}
3032-
AND plan.kind = 'todo_list' AND plan.status <> 'superseded'
3033-
ORDER BY owner.ordinal DESC, plan.plan_id DESC
3034-
LIMIT 1
3028+
WITH latest_owner AS MATERIALIZED (
3029+
SELECT plan.plan_id, owner.run_id, owner.provider_thread_id
3030+
FROM orchestration_v2_projection_plans AS plan
3031+
INNER JOIN orchestration_v2_projection_runs AS owner
3032+
ON owner.thread_id = plan.thread_id AND owner.run_id = plan.run_id
3033+
WHERE plan.thread_id = ${threadId}
3034+
AND plan.kind = 'todo_list' AND plan.status <> 'superseded'
3035+
ORDER BY owner.ordinal DESC, plan.plan_id DESC
3036+
LIMIT 1
3037+
)
3038+
SELECT COALESCE(CASE WHEN EXISTS (
3039+
SELECT 1 FROM orchestration_v2_projection_plans
3040+
WHERE thread_id = ${threadId} AND run_id = owner.run_id
3041+
AND kind = 'todo_list' AND status <> 'superseded'
3042+
AND plan_id <> owner.plan_id
3043+
) THEN (
3044+
-- Plan UUIDs cannot order replacements within a run.
3045+
SELECT plan.plan_id
3046+
FROM orchestration_v2_projection_turn_items AS item
3047+
CROSS JOIN orchestration_v2_projection_plans AS plan
3048+
ON plan.plan_id = json_extract(item.payload_json, '$.planId')
3049+
WHERE item.run_id = owner.run_id AND item.thread_id = ${threadId}
3050+
AND item.type = 'todo_list'
3051+
AND plan.run_id = owner.run_id AND plan.thread_id = ${threadId}
3052+
AND plan.kind = 'todo_list' AND plan.status <> 'superseded'
3053+
ORDER BY item.ordinal DESC
3054+
LIMIT 1
3055+
) END, owner.plan_id) AS plan_id, owner.run_id, owner.provider_thread_id,
3056+
(
3057+
SELECT MAX(plan_id) FROM orchestration_v2_projection_plans
3058+
WHERE thread_id = ${threadId} AND run_id = owner.run_id
3059+
) AS last_plan_id
3060+
FROM latest_owner AS owner
30353061
`;
30363062

30373063
const [
@@ -3282,7 +3308,10 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
32823308
SELECT value FROM json_each(${cohortPlanIds})
32833309
)
32843310
)
3285-
ORDER BY plan_id ASC
3311+
-- Keep the selected artifact last within its run for equal-run consumers.
3312+
ORDER BY CASE WHEN plan_id = ${latestTodoList?.plan_id ?? null}
3313+
THEN ${latestTodoList?.last_plan_id ?? null} ELSE plan_id END ASC,
3314+
plan_id = ${latestTodoList?.plan_id ?? null} ASC
32863315
`,
32873316
fields !== undefined && !fields.includes("checkpointScopes")
32883317
? Effect.succeed([])

0 commit comments

Comments
 (0)