Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

agentAwareness.ts

187 lines5.6 KB

1import {
2 isListedThreadLineage,
3 type EnvironmentId,
4 isProviderNativeSubagentThread,
5 type OrchestrationV2ThreadShell,
6 type Project,
7 type RuntimeRequestId,
8 type ThreadId,
9} from "@t3tools/contracts";
10import * as DateTime from "effect/DateTime";
11​
12import { backgroundWorkHoldsCompletion } from "./orchestrationV2PendingBackgroundWork.ts";
13​
14export type AgentAwarenessPhase =
15 | "starting"
16 | "running"
17 | "waiting_for_approval"
18 | "waiting_for_input"
19 | "completed"
20 | "failed"
21 | "stale";
22​
23export interface SubagentRequestAwareness {
24 readonly requestId: RuntimeRequestId;
25 /** The child's own update time, which moves whenever its request changes. */
26 readonly updatedAt: DateTime.Utc;
28​
29/** Provider-native subagents ask on their parent; the coordinator answers child questions. */
30export function subagentAwaitsApproval(
31 thread: Pick<
32 OrchestrationV2ThreadShell,
33 "activityRunStatus" | "creationSource" | "lineage" | "pendingRuntimeRequest"
34 >,
35): boolean {
36 const kind = thread.pendingRuntimeRequest?.kind;
37 return (
38 kind !== undefined &&
39 kind !== "auth_refresh" &&
40 kind !== "user_input" &&
41 thread.lineage.relationshipToParent === "subagent" &&
42 !isProviderNativeSubagentThread(thread) &&
43 // The activity run stays set when a follow-up is queued behind a blocked run.
44 thread.activityRunStatus != null
45 );
47​
48export interface AgentAwarenessState {
49 readonly environmentId: EnvironmentId;
50 readonly threadId: ThreadId;
51 readonly projectTitle: string;
52 readonly threadTitle: string;
53 readonly phase: AgentAwarenessPhase;
54 readonly headline: string;
55 readonly detail?: string;
56 readonly modelTitle: string;
57 readonly updatedAt: string;
58 readonly deepLink: string;
60​
61function buildAgentAwarenessDeepLink(input: {
62 readonly environmentId: EnvironmentId;
63 readonly threadId: ThreadId;
64}): string {
65 return `/threads/${encodeURIComponent(input.environmentId)}/${encodeURIComponent(input.threadId)}`;
67​
68export interface ProjectThreadAwarenessV2Input {
69 readonly environmentId: EnvironmentId;
70 readonly project: Pick<Project, "title">;
71 readonly thread: Pick<
72 OrchestrationV2ThreadShell,
73 | "activityRunStatus"
74 | "id"
75 | "lineage"
76 | "latestRunStopRequested"
77 | "modelSelection"
78 | "pendingBackgroundTasks"
79 | "pendingRuntimeRequest"
80 | "settledOverride"
81 | "status"
82 | "title"
83 | "updatedAt"
84 >;
85 /** The approval a live subagent below this thread is waiting on. */
86 readonly pendingSubagentRequest?: SubagentRequestAwareness | null;
88​
89export function projectThreadAwarenessV2(
90 input: ProjectThreadAwarenessV2Input,
91): AgentAwarenessState | null {
92 const { environmentId, project, thread } = input;
93 if (!isListedThreadLineage(thread.lineage)) return null;
94 const subagentRequest = input.pendingSubagentRequest ?? null;
95 const phase = resolveThreadAwarenessPhaseV2(thread, subagentRequest !== null);
96 if (phase === null) {
97 return null;
98 }
99 if (phase === "completed" && thread.latestRunStopRequested === true) return null;
100 // Resolve the phase first so live or waiting work stays visible after settlement.
101 if (
102 thread.settledOverride === "settled" &&
103 (phase === "completed" || phase === "failed") &&
104 !backgroundWorkHoldsCompletion(thread.pendingBackgroundTasks ?? [])
105 ) {
106 return null;
107 }
108 const detail =
109 phase === "completed"
110 ? "Review the completed task."
111 : phase === "failed"
112 ? "The agent run failed."
113 : undefined;
114 return {
115 environmentId,
116 threadId: thread.id,
117 projectTitle: project.title,
118 threadTitle: thread.title,
119 phase,
120 headline: headlineForPhase(phase),
121 ...(detail === undefined ? {} : { detail }),
122 modelTitle: thread.modelSelection.model,
123 // A child's request leaves the parent's own timestamp alone. Alerts are
124 // keyed by phase and time, so a second wait needs the child's time.
125 updatedAt: DateTime.formatIso(
126 subagentRequest === null
127 ? thread.updatedAt
128 : DateTime.max(thread.updatedAt, subagentRequest.updatedAt),
129 ),
130 deepLink: buildAgentAwarenessDeepLink({ environmentId, threadId: thread.id }),
131 };
133​
134function resolveThreadAwarenessPhaseV2(
135 thread: ProjectThreadAwarenessV2Input["thread"],
136 childAwaitsApproval: boolean,
137): AgentAwarenessPhase | null {
138 if (thread.pendingRuntimeRequest?.kind === "user_input") return "waiting_for_input";
139 if (
140 thread.pendingRuntimeRequest !== null &&
141 thread.pendingRuntimeRequest.kind !== "auth_refresh"
142 ) {
143 return "waiting_for_approval";
144 }
145 // The parent can finish its own turn while a delegated child keeps working,
146 // so a child's request outranks the parent's run status.
147 if (childAwaitsApproval) return "waiting_for_approval";
148 switch (thread.activityRunStatus ?? thread.status) {
149 case "preparing":
150 case "starting":
151 return "starting";
152 case "running":
153 case "waiting":
154 return "running";
155 case "completed":
156 return backgroundWorkHoldsCompletion(thread.pendingBackgroundTasks ?? [])
157 ? "running"
158 : "completed";
159 case "failed":
160 return "failed";
161 case "idle":
162 case "queued":
163 case "interrupted":
164 case "cancelled":
165 case "rolled_back":
166 return null;
167 }
169​
170function headlineForPhase(phase: AgentAwarenessPhase): string {
171 switch (phase) {
172 case "starting":
173 return "Starting agent";
174 case "running":
175 return "Agent is working";
176 case "waiting_for_approval":
177 return "Approval needed";
178 case "waiting_for_input":
179 return "Waiting for input";
180 case "completed":
181 return "Agent finished";
182 case "failed":
183 return "Agent failed";
184 case "stale":
185 return "Update delayed";
186 }