Skip to content

Commit 88fe3c3

Browse files
committed
fix(server): exclude automatic restart chains from result recovery
1 parent a1eda74 commit 88fe3c3

2 files changed

Lines changed: 105 additions & 1 deletion

File tree

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

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,73 @@ const followUpResultRecovery = Effect.gen(function* () {
568568
});
569569
assert.notInclude(yield* store.getRecoveryThreadIds("subagent-results"), childId);
570570
}
571+
let ordinal = 11;
572+
for (const [scenario, sourceRunId, recoverable] of [
573+
["requested", original.id, true],
574+
["notification", RunId.make("run:automatic-follow-up:0"), false],
575+
["scheduled", RunId.make("run:automatic-follow-up:3"), false],
576+
["notification-intermediate", original.id, false],
577+
["scheduled-intermediate", original.id, false],
578+
["missing", RunId.make("run:missing-source"), false],
579+
] as const) {
580+
let previousRunId = sourceRunId;
581+
for (let depth = 1; depth <= 3; depth++) {
582+
const continuation = {
583+
...monitor,
584+
id: RunId.make(`run:continuation:${scenario}:${depth}`),
585+
userMessageId: MessageId.make(`message:continuation:${scenario}:${depth}`),
586+
ordinal: ordinal++,
587+
restartContinuationOfRunId: previousRunId,
588+
};
589+
yield* store.apply({
590+
id: EventId.make(`continuation:message:${scenario}:${depth}`),
591+
type: "message.updated",
592+
threadId: childId,
593+
runId: continuation.id,
594+
occurredAt: now,
595+
payload: {
596+
...(yield* store.getThreadProjection(childId)).messages[0]!,
597+
id: continuation.userMessageId,
598+
runId: continuation.id,
599+
creationSource: "server",
600+
createdBy: "agent",
601+
notification:
602+
scenario === "notification-intermediate" && depth === 1
603+
? { source: { kind: "command" }, outcome: "completed", summary: "Automatic update" }
604+
: undefined,
605+
scheduledTaskId:
606+
scenario === "scheduled-intermediate" && depth === 1
607+
? ScheduledTaskId.make("continuation-schedule")
608+
: undefined,
609+
},
610+
});
611+
yield* store.apply({
612+
id: EventId.make(`continuation:run:${scenario}:${depth}`),
613+
type: "run.updated",
614+
threadId: childId,
615+
runId: continuation.id,
616+
occurredAt: now,
617+
payload: continuation,
618+
});
619+
const candidates = yield* store.getRecoveryThreadIds("subagent-results");
620+
assert.equal(candidates.includes(childId), recoverable, `${scenario} depth ${depth}`);
621+
if (recoverable) {
622+
yield* store.apply({
623+
id: EventId.make(`continuation:transfer:${scenario}:${depth}`),
624+
type: "context-transfer.created",
625+
threadId: childId,
626+
occurredAt: now,
627+
payload: {
628+
...transfer,
629+
id: ContextTransferId.make(`transfer:continuation:${scenario}:${depth}`),
630+
sourcePoint: { threadId: childId, runId: continuation.id },
631+
},
632+
});
633+
assert.notInclude(yield* store.getRecoveryThreadIds("subagent-results"), childId);
634+
}
635+
previousRunId = continuation.id;
636+
}
637+
}
571638
});
572639

573640
it.effect("recovers only undelivered follow-up results in memory", () =>

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

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3523,7 +3523,44 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
35233523
AND json_extract(message.payload_json, '$.createdBy') IS NOT 'user'
35243524
AND NOT (
35253525
(terminal.ordinal = 1 AND json_extract(message.payload_json, '$.notification') IS NULL)
3526-
OR json_extract(terminal.payload_json, '$.restartContinuationOfRunId') IS NOT NULL
3526+
OR EXISTS (
3527+
-- Follow only this candidate's earlier runs. Automatic wakes
3528+
-- cannot become requested work through a restart chain.
3529+
WITH RECURSIVE continuation_sources(run_id, ordinal, payload_json) AS (
3530+
SELECT terminal.run_id, terminal.ordinal, terminal.payload_json
3531+
UNION ALL
3532+
SELECT source.run_id, source.ordinal, source.payload_json
3533+
FROM continuation_sources AS continuation
3534+
JOIN orchestration_v2_projection_runs AS source
3535+
ON source.thread_id = child.thread_id
3536+
AND source.run_id = CASE WHEN json_valid(continuation.payload_json)
3537+
THEN json_extract(continuation.payload_json, '$.restartContinuationOfRunId')
3538+
ELSE NULL END
3539+
AND source.ordinal < continuation.ordinal
3540+
WHERE NOT EXISTS (
3541+
SELECT 1 FROM orchestration_v2_projection_messages AS source_message
3542+
WHERE source_message.thread_id = child.thread_id
3543+
AND source_message.message_id = json_extract(continuation.payload_json, '$.userMessageId')
3544+
AND CASE WHEN json_valid(source_message.payload_json) THEN
3545+
json_extract(source_message.payload_json, '$.notification') IS NOT NULL
3546+
OR json_extract(source_message.payload_json, '$.scheduledTaskId') IS NOT NULL
3547+
ELSE 0 END
3548+
)
3549+
)
3550+
SELECT 1 FROM continuation_sources AS source
3551+
WHERE NOT EXISTS (
3552+
SELECT 1 FROM orchestration_v2_projection_messages AS source_message
3553+
WHERE source_message.thread_id = child.thread_id
3554+
AND source_message.message_id = json_extract(source.payload_json, '$.userMessageId')
3555+
AND CASE WHEN json_valid(source_message.payload_json) THEN
3556+
json_extract(source_message.payload_json, '$.notification') IS NOT NULL
3557+
OR json_extract(source_message.payload_json, '$.scheduledTaskId') IS NOT NULL
3558+
OR (json_extract(source_message.payload_json, '$.creationSource') = 'server'
3559+
AND json_extract(source_message.payload_json, '$.createdBy') IS NOT 'user'
3560+
AND source.ordinal <> 1)
3561+
ELSE 0 END
3562+
)
3563+
)
35273564
))
35283565
ELSE 0 END
35293566
)

0 commit comments

Comments
 (0)