Open History to see the last commit
1import type { OrchestrationV2Subagent } from "@t3tools/contracts";
2import * as DateTime from "effect/DateTime";
3
4const ACTIVE_SUBAGENT_STATUSES = new Set<OrchestrationV2Subagent["status"]>([
5 "pending",
6 "running",
7 "waiting",
8]);
9
10/**
11 * Whether a subagent started another run after a notification reported its
12 * final outcome, so that outcome no longer describes it. Claude resumes a
13 * finished subagent under the same id. An active subagent is always on a later
14 * run, even when the notification was delivered late from the queue; a settled
15 * one is on a later run when it started after the notification arrived.
16 */
17export function subagentResumedSince(
18 agent: Pick<OrchestrationV2Subagent, "status" | "startedAt">,
19 notifiedAt: string,
20): boolean {
21 if (ACTIVE_SUBAGENT_STATUSES.has(agent.status)) return true;
22 if (agent.startedAt === null) return false;
23 const notifiedAtMillis = Date.parse(notifiedAt);
24 return (
25 !Number.isNaN(notifiedAtMillis) && DateTime.toEpochMillis(agent.startedAt) > notifiedAtMillis
26 );
27}