Open History to see the last commit
1import {
2 latestProviderTurnForAttempt,
3 type MessageId,
4 type OrchestrationV2ThreadProjection,
5} from "@t3tools/contracts";
6
7/**
8 * A persisted steer without an acceptance receipt can be retried as a continuation.
9 * Delivery is at least once: provider acceptance and our receipt cannot commit
10 * atomically. Reusing the message ID keeps recovery from duplicating timeline items.
11 */
12export function isUndeliveredMailboxSteer(
13 projection: Pick<OrchestrationV2ThreadProjection, "messages" | "runs" | "providerTurns">,
14 messageId: MessageId,
15): boolean {
16 const message = projection.messages.find((candidate) => candidate.id === messageId);
17 if (message?.delegatedCompletion === undefined) return false;
18 const run = projection.runs.find((candidate) => candidate.id === message.runId);
19 return (
20 run !== undefined &&
21 run.userMessageId !== message.id &&
22 (["completed", "failed", "interrupted", "cancelled", "rolled_back"].includes(run.status) ||
23 latestProviderTurnForAttempt(projection.providerTurns, run.activeAttemptId)?.status ===
24 "completed")
25 );
26}