Repository navigation
Expand file tree
/
Copy pathagentAwareness.ts
More file actions
207 lines (196 loc) · 6.58 KB
/
Copy pathagentAwareness.ts
File metadata and controls
207 lines (196 loc) · 6.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import {
isListedThreadLineage,
isProviderNativeSubagentThread,
type EnvironmentId,
type OrchestrationV2ThreadShell,
type Project,
type ThreadId,
} from "@onicode/contracts";
import * as DateTime from "effect/DateTime";
import {
backgroundWorkHoldsCompletion,
monitorHoldsWakeCompletion,
} from "./orchestrationV2PendingBackgroundWork.ts";
export type AgentAwarenessPhase =
| "starting"
| "running"
| "waiting_for_approval"
| "waiting_for_input"
| "completed"
| "failed"
| "stale";
/**
* Turn items that can start or end a subagent's user-only wait: its secret or link card,
* or a user message that moves past a link card. Filters that drop subagent transcript
* updates must keep these, or the parent's "Input needed" never appears or never clears.
*/
export const turnItemCanChangeUserWait = (item: { readonly type: string }) =>
item.type === "secret_request" || item.type === "link_request" || item.type === "user_message";
export interface AgentAwarenessState {
readonly environmentId: EnvironmentId;
readonly threadId: ThreadId;
readonly projectTitle: string;
readonly threadTitle: string;
readonly phase: AgentAwarenessPhase;
readonly headline: string;
readonly detail?: string;
/** A result someone has opened since it came in: it waits for no one, so nothing shows it. */
readonly seen?: true;
readonly modelTitle: string;
readonly updatedAt: string;
readonly deepLink: string;
}
function buildAgentAwarenessDeepLink(input: {
readonly environmentId: EnvironmentId;
readonly threadId: ThreadId;
}): string {
return `/threads/${encodeURIComponent(input.environmentId)}/${encodeURIComponent(input.threadId)}`;
}
/** What a thread shown under another asks of the person. */
export type ChildThreadRequest = "approval" | "user_input";
/**
* What a subagent or side chat asks that the listed thread above it shows, by the rule the thread
* lists roll requests up with. The agent above answers a subagent's question itself, a provider's
* own subagent asks on its parent, and once a subagent's run ends nothing waits on the answer.
*/
export function childThreadRequest(
thread: Pick<
OrchestrationV2ThreadShell,
"activityRunStatus" | "creationSource" | "lineage" | "pendingRuntimeRequest" | "status"
>,
): ChildThreadRequest | null {
const request = thread.pendingRuntimeRequest;
if (request === null || request.kind === "auth_refresh") return null;
const asks = request.kind === "user_input" ? "user_input" : "approval";
if (thread.lineage.sideChat === true) return asks;
if (
thread.lineage.relationshipToParent !== "subagent" ||
isProviderNativeSubagentThread(thread) ||
(thread.activityRunStatus ?? null) === null ||
(asks === "user_input" && request.userOnly !== true)
) {
return null;
}
return asks;
}
export interface ProjectThreadAwarenessV2Input {
readonly environmentId: EnvironmentId;
readonly project: Pick<Project, "title">;
/** The most pressing thing a thread shown under this one asks. */
readonly childRequest?: ChildThreadRequest | null;
readonly thread: Pick<
OrchestrationV2ThreadShell,
| "activityRunStatus"
| "id"
| "lineage"
| "lastVisitedAt"
| "latestRunBackgroundWake"
| "latestRunCompletedAt"
| "latestRunStopRequested"
| "modelSelection"
| "pendingBackgroundTasks"
| "pendingRuntimeRequest"
| "settledOverride"
| "status"
| "title"
| "updatedAt"
>;
}
export function projectThreadAwarenessV2(
input: ProjectThreadAwarenessV2Input,
): AgentAwarenessState | null {
const { environmentId, project, thread } = input;
if (!isListedThreadLineage(thread.lineage)) return null;
const phase = resolveThreadAwarenessPhaseV2(thread, input.childRequest ?? null);
if (phase === null) {
return null;
}
if (phase === "completed" && thread.latestRunStopRequested === true) return null;
// Resolve the phase first so live or waiting work stays visible after settlement.
const runStatus = thread.activityRunStatus ?? thread.status;
if (
thread.settledOverride === "settled" &&
(phase === "completed" ||
phase === "failed" ||
(phase === "running" && runStatus === "completed")) &&
!backgroundWorkHoldsCompletion(thread.pendingBackgroundTasks ?? [])
) {
return null;
}
// The thread lists call a result read by the same rule.
const seen =
(phase === "completed" || phase === "failed") &&
thread.lastVisitedAt != null &&
thread.latestRunCompletedAt != null &&
DateTime.toEpochMillis(thread.lastVisitedAt) >=
DateTime.toEpochMillis(thread.latestRunCompletedAt);
const detail =
phase === "completed"
? "Review the completed task."
: phase === "failed"
? "The agent run failed."
: undefined;
return {
environmentId,
threadId: thread.id,
projectTitle: project.title,
threadTitle: thread.title,
phase,
headline: headlineForPhase(phase),
...(detail === undefined ? {} : { detail }),
...(seen ? { seen } : {}),
modelTitle: thread.modelSelection.model,
updatedAt: DateTime.formatIso(thread.updatedAt),
deepLink: buildAgentAwarenessDeepLink({ environmentId, threadId: thread.id }),
};
}
function resolveThreadAwarenessPhaseV2(
thread: ProjectThreadAwarenessV2Input["thread"],
childRequest: ChildThreadRequest | null,
): AgentAwarenessPhase | null {
const own = thread.pendingRuntimeRequest;
const asked = own !== null && own.kind !== "auth_refresh";
if ((asked && own.kind !== "user_input") || childRequest === "approval") {
return "waiting_for_approval";
}
if (asked || childRequest === "user_input") return "waiting_for_input";
switch (thread.activityRunStatus ?? thread.status) {
case "preparing":
case "starting":
return "starting";
case "running":
case "waiting":
return "running";
case "completed":
return backgroundWorkHoldsCompletion(thread.pendingBackgroundTasks ?? []) ||
monitorHoldsWakeCompletion(thread)
? "running"
: "completed";
case "failed":
return "failed";
case "idle":
case "queued":
case "interrupted":
case "cancelled":
case "rolled_back":
return null;
}
}
function headlineForPhase(phase: AgentAwarenessPhase): string {
switch (phase) {
case "starting":
return "Starting agent";
case "running":
return "Agent is working";
case "waiting_for_approval":
return "Approval needed";
case "waiting_for_input":
return "Waiting for input";
case "completed":
return "Agent finished";
case "failed":
return "Agent failed";
case "stale":
return "Update delayed";
}
}