Open History to see the last commit
1import type {
2 EnvironmentId,
3 OrchestrationProjectShell,
4 OrchestrationThreadShell,
5 ThreadId,
6} from "@t3tools/contracts";
7
8export type AgentAwarenessPhase =
9 | "starting"
10 | "running"
11 | "waiting_for_approval"
12 | "waiting_for_input"
13 | "completed"
14 | "failed"
15 | "stale";
16
17export interface AgentAwarenessState {
18 readonly environmentId: EnvironmentId;
19 readonly threadId: ThreadId;
20 readonly projectTitle: string;
21 readonly threadTitle: string;
22 readonly phase: AgentAwarenessPhase;
23 readonly headline: string;
24 readonly detail?: string;
25 readonly modelTitle: string;
26 readonly updatedAt: string;
27 readonly deepLink: string;
28}
29
30export interface ProjectThreadAwarenessInput {
31 readonly environmentId: EnvironmentId;
32 readonly project: Pick<OrchestrationProjectShell, "title">;
33 readonly thread: Pick<
34 OrchestrationThreadShell,
35 | "id"
36 | "title"
37 | "modelSelection"
38 | "session"
39 | "latestTurn"
40 | "updatedAt"
41 | "hasPendingApprovals"
42 | "hasPendingUserInput"
43 >;
44}
45
46export function buildAgentAwarenessDeepLink(input: {
47 readonly environmentId: EnvironmentId;
48 readonly threadId: ThreadId;
49}): string {
50 return `/threads/${encodeURIComponent(input.environmentId)}/${encodeURIComponent(input.threadId)}`;
51}
52
53export function projectThreadAwareness(
54 input: ProjectThreadAwarenessInput,
55): AgentAwarenessState | null {
56 const { environmentId, project, thread } = input;
57 const phase = resolveThreadAwarenessPhase(thread);
58 if (!phase) {
59 return null;
60 }
61
62 const detail = detailForPhase(phase, thread);
63 return {
64 environmentId,
65 threadId: thread.id,
66 projectTitle: project.title,
67 threadTitle: thread.title,
68 phase,
69 headline: headlineForPhase(phase),
70 ...(detail === undefined ? {} : { detail }),
71 modelTitle: thread.modelSelection.model,
72 updatedAt: thread.updatedAt,
73 deepLink: buildAgentAwarenessDeepLink({ environmentId, threadId: thread.id }),
74 };
75}
76
77function resolveThreadAwarenessPhase(
78 thread: ProjectThreadAwarenessInput["thread"],
79): AgentAwarenessPhase | null {
80 if (thread.hasPendingApprovals) {
81 return "waiting_for_approval";
82 }
83 if (thread.hasPendingUserInput) {
84 return "waiting_for_input";
85 }
86 if (thread.session?.status === "error" || thread.latestTurn?.state === "error") {
87 return "failed";
88 }
89 if (thread.session?.status === "starting") {
90 return "starting";
91 }
92 if (thread.session?.status === "running" || thread.latestTurn?.state === "running") {
93 return "running";
94 }
95 if (thread.latestTurn?.state === "completed") {
96 return "completed";
97 }
98 // A turn that finished can still read as "interrupted" here: session
99 // teardown settles still-running turns by session status, and that write
100 // can race the turn.completed one. completedAt survives the race — a turn
101 // that has a completion timestamp finished, whatever the state column says.
102 // Without this, quick finish-then-teardown threads resolve to null
103 // persistently and get tombstoned instead of published as completed.
104 if (thread.latestTurn?.state === "interrupted" && thread.latestTurn.completedAt !== null) {
105 return "completed";
106 }
107 // Threads whose turns never produce a checkpoint (no code changes) have no
108 // materialized latestTurn in the shell at all, and the session-set
109 // projection clears latest_turn_id the moment the session settles. The
110 // session status is then the only surviving completion signal: a live
111 // session at "ready"/"idle" with nothing pending and nothing running means
112 // the agent finished and is waiting for the next prompt — Done.
113 if (thread.session?.status === "ready" || thread.session?.status === "idle") {
114 return "completed";
115 }
116 return null;
117}
118
119function headlineForPhase(phase: AgentAwarenessPhase): string {
120 switch (phase) {
121 case "starting":
122 return "Starting agent";
123 case "running":
124 return "Agent is working";
125 case "waiting_for_approval":
126 return "Approval needed";
127 case "waiting_for_input":
128 return "Waiting for input";
129 case "completed":
130 return "Agent finished";
131 case "failed":
132 return "Agent failed";
133 case "stale":
134 return "Update delayed";
135 }
136}
137
138function detailForPhase(
139 phase: AgentAwarenessPhase,
140 thread: ProjectThreadAwarenessInput["thread"],
141): string | undefined {
142 if (phase === "failed") {
143 return thread.session?.lastError ?? undefined;
144 }
145 if (phase === "completed") {
146 return "Review the completed task.";
147 }
148 if (phase === "running" && thread.session?.providerName) {
149 return `${thread.session.providerName} is active.`;
150 }
151 return undefined;
152}