@moxt-ai/mobius 0.0.452 → 0.0.454
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{chunk-DVSY3FDV.js → chunk-7VYQEJW7.js} +1642 -160
- package/dist/{chunk-DVSY3FDV.js.map → chunk-7VYQEJW7.js.map} +4 -4
- package/dist/{chunk-TZJRGEU4.js → chunk-TOQQNYQU.js} +2 -2
- package/dist/{chunk-TZJRGEU4.js.map → chunk-TOQQNYQU.js.map} +1 -1
- package/dist/{cli-NQKQR32P.js → cli-CCIMF4WB.js} +3 -3
- package/dist/cli.js +2 -2
- package/dist/engine/acp/adapter/antigravity/adapter.d.ts.map +1 -1
- package/dist/engine/acp/adapter/antigravity/adapter.js +12 -1
- package/dist/engine/acp/adapter/antigravity/adapter.js.map +1 -1
- package/dist/engine/acp/adapter/antigravity/agent-connection.d.ts +10 -3
- package/dist/engine/acp/adapter/antigravity/agent-connection.d.ts.map +1 -1
- package/dist/engine/acp/adapter/antigravity/agent-connection.js +190 -24
- package/dist/engine/acp/adapter/antigravity/agent-connection.js.map +1 -1
- package/dist/engine/acp/adapter/antigravity/background-wakeup.d.ts +37 -0
- package/dist/engine/acp/adapter/antigravity/background-wakeup.d.ts.map +1 -0
- package/dist/engine/acp/adapter/antigravity/background-wakeup.js +1251 -0
- package/dist/engine/acp/adapter/antigravity/background-wakeup.js.map +1 -0
- package/dist/engine/acp/adapter/execution-connections.d.ts.map +1 -1
- package/dist/engine/acp/adapter/execution-connections.js +4 -1
- package/dist/engine/acp/adapter/execution-connections.js.map +1 -1
- package/dist/engine/agent/specification/vendors/antigravity-agent.d.ts +5 -1
- package/dist/engine/agent/specification/vendors/antigravity-agent.d.ts.map +1 -1
- package/dist/engine/agent/specification/vendors/antigravity-agent.js +7 -2
- package/dist/engine/agent/specification/vendors/antigravity-agent.js.map +1 -1
- package/dist/engine/session/live/agent-session.d.ts +1 -1
- package/dist/engine/session/live/agent-session.d.ts.map +1 -1
- package/dist/engine/session/live/agent-session.js +34 -8
- package/dist/engine/session/live/agent-session.js.map +1 -1
- package/dist/engine/session/manager/session-manager.d.ts.map +1 -1
- package/dist/engine/session/manager/session-manager.js +16 -1
- package/dist/engine/session/manager/session-manager.js.map +1 -1
- package/dist/engine/storage/files/local-files.d.ts +5 -0
- package/dist/engine/storage/files/local-files.d.ts.map +1 -1
- package/dist/engine/storage/files/local-files.js +38 -0
- package/dist/engine/storage/files/local-files.js.map +1 -1
- package/dist/engine.js +2 -2
- package/package.json +1 -1
- /package/dist/{cli-NQKQR32P.js.map → cli-CCIMF4WB.js.map} +0 -0
|
@@ -0,0 +1,1251 @@
|
|
|
1
|
+
import { homedir } from "node:os";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { methods, } from "@agentclientprotocol/sdk";
|
|
4
|
+
import { z } from "zod";
|
|
5
|
+
import { readScopedFileTail } from "../../../storage/files/local-files.js";
|
|
6
|
+
import { systemTimer } from "../../../util/timer.js";
|
|
7
|
+
import { isRecord, truncate } from "../../../util/value-utils.js";
|
|
8
|
+
import { AGENT_TURN_META, BACKGROUND_TASKS_META, } from "../../agent/in-process-endpoint.js";
|
|
9
|
+
const TRANSCRIPT_TAIL_BYTES = 65_536;
|
|
10
|
+
const TRANSCRIPT_INITIAL_DELAY_MS = 40;
|
|
11
|
+
const TRANSCRIPT_POLL_INTERVAL_MS = 150;
|
|
12
|
+
export const WAKEUP_INACTIVITY_TIMEOUT_MS = 300_000;
|
|
13
|
+
const SESSION_UUID_PATTERN = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/iu;
|
|
14
|
+
const SUBAGENT_CONVERSATION_ID_PATTERN = /"conversationId"\s*:\s*"([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})"/giu;
|
|
15
|
+
const SUBAGENT_IDLE_ID_PATTERN = /\bSubagent ([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}) (?:has gone|went) idle\b/giu;
|
|
16
|
+
const AGENT_EXECUTOR_ERROR_PATTERN = /^\s*⚠ agent executor error:\s*(.+?)\s*$/su;
|
|
17
|
+
const SUBAGENT_CREATED_MARKER = "Created the following subagent";
|
|
18
|
+
const MAXIMUM_TOOL_DESCRIPTION_CHARS = 500;
|
|
19
|
+
const transcriptStepSchema = z.object({
|
|
20
|
+
content: z.string().optional(),
|
|
21
|
+
status: z.string().optional(),
|
|
22
|
+
step_index: z.number().int().nonnegative(),
|
|
23
|
+
tool_calls: z.array(z.unknown()).optional(),
|
|
24
|
+
type: z.string(),
|
|
25
|
+
});
|
|
26
|
+
const EMPTY_INCOMING_OUTCOME = {
|
|
27
|
+
afterUpdates: [],
|
|
28
|
+
beforeUpdates: [],
|
|
29
|
+
};
|
|
30
|
+
function readString(record, key) {
|
|
31
|
+
const value = record[key];
|
|
32
|
+
return typeof value === "string" ? value : undefined;
|
|
33
|
+
}
|
|
34
|
+
function requestKeyOf(id) {
|
|
35
|
+
return typeof id === "string" || typeof id === "number" ? id : undefined;
|
|
36
|
+
}
|
|
37
|
+
function isRequestMessage(message) {
|
|
38
|
+
return "method" in message && "id" in message;
|
|
39
|
+
}
|
|
40
|
+
function isNotificationMessage(message) {
|
|
41
|
+
return "method" in message && !("id" in message);
|
|
42
|
+
}
|
|
43
|
+
function isResponseMessage(message) {
|
|
44
|
+
return "id" in message && !("method" in message);
|
|
45
|
+
}
|
|
46
|
+
function resolveGeminiHome(launchEnvironment) {
|
|
47
|
+
const configured = launchEnvironment["GEMINI_HOME"]?.trim();
|
|
48
|
+
if (configured !== undefined && configured.length > 0) {
|
|
49
|
+
return configured;
|
|
50
|
+
}
|
|
51
|
+
const home = launchEnvironment["HOME"]?.trim() || launchEnvironment["USERPROFILE"]?.trim() || homedir();
|
|
52
|
+
return home.length > 0 ? join(home, ".gemini") : undefined;
|
|
53
|
+
}
|
|
54
|
+
async function readTranscriptTailState(launchEnvironment, sessionId, baselineStepIndex) {
|
|
55
|
+
if (!SESSION_UUID_PATTERN.test(sessionId)) {
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
const geminiHome = resolveGeminiHome(launchEnvironment);
|
|
59
|
+
if (geminiHome === undefined) {
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
const tail = await readScopedFileTail(geminiHome, join("antigravity-cli", "brain", sessionId, ".system_generated", "logs", "transcript.jsonl"), TRANSCRIPT_TAIL_BYTES);
|
|
63
|
+
if (tail === null) {
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
if (tail.bytes.byteLength === 0) {
|
|
67
|
+
return { maxStepIndex: -1, settledStepIndex: null, steps: [] };
|
|
68
|
+
}
|
|
69
|
+
const text = new TextDecoder().decode(tail.bytes);
|
|
70
|
+
const lines = text.split("\n");
|
|
71
|
+
const startIndex = tail.offset > 0 ? 1 : 0;
|
|
72
|
+
const steps = [];
|
|
73
|
+
for (const rawLine of lines.slice(startIndex)) {
|
|
74
|
+
const trimmed = rawLine.trim();
|
|
75
|
+
if (trimmed.length === 0) {
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
let parsedJson;
|
|
79
|
+
try {
|
|
80
|
+
parsedJson = JSON.parse(trimmed);
|
|
81
|
+
}
|
|
82
|
+
catch {
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
const parsed = transcriptStepSchema.safeParse(parsedJson);
|
|
86
|
+
if (parsed.success) {
|
|
87
|
+
steps.push(parsed.data);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
steps.sort((left, right) => left.step_index - right.step_index);
|
|
91
|
+
const maxStepIndex = steps.at(-1)?.step_index ?? -1;
|
|
92
|
+
const settledStep = steps.find((step) => step.step_index > baselineStepIndex &&
|
|
93
|
+
step.type === "PLANNER_RESPONSE" &&
|
|
94
|
+
step.status === "DONE" &&
|
|
95
|
+
(step.tool_calls === undefined || step.tool_calls.length === 0));
|
|
96
|
+
return {
|
|
97
|
+
maxStepIndex,
|
|
98
|
+
settledStepIndex: settledStep?.step_index ?? null,
|
|
99
|
+
steps,
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
function extractUuidMatches(text, pattern) {
|
|
103
|
+
pattern.lastIndex = 0;
|
|
104
|
+
const matches = [];
|
|
105
|
+
for (const match of text.matchAll(pattern)) {
|
|
106
|
+
const captured = match[1]?.toLowerCase();
|
|
107
|
+
if (captured !== undefined) {
|
|
108
|
+
matches.push(captured);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return matches;
|
|
112
|
+
}
|
|
113
|
+
function extractExecutorError(updateKind, update) {
|
|
114
|
+
if (updateKind !== "agent_message_chunk" && updateKind !== "agent_message") {
|
|
115
|
+
return undefined;
|
|
116
|
+
}
|
|
117
|
+
const content = update["content"];
|
|
118
|
+
if (!isRecord(content) || content["type"] !== "text") {
|
|
119
|
+
return undefined;
|
|
120
|
+
}
|
|
121
|
+
const text = readString(content, "text");
|
|
122
|
+
if (text === undefined) {
|
|
123
|
+
return undefined;
|
|
124
|
+
}
|
|
125
|
+
const match = AGENT_EXECUTOR_ERROR_PATTERN.exec(text);
|
|
126
|
+
const message = match?.[1];
|
|
127
|
+
return message === undefined ? undefined : truncate(message, MAXIMUM_TOOL_DESCRIPTION_CHARS);
|
|
128
|
+
}
|
|
129
|
+
function extractToolInputRecords(update) {
|
|
130
|
+
const records = [];
|
|
131
|
+
const args = update["args"];
|
|
132
|
+
if (isRecord(args)) {
|
|
133
|
+
records.push(args);
|
|
134
|
+
}
|
|
135
|
+
const rawInput = update["rawInput"];
|
|
136
|
+
if (isRecord(rawInput)) {
|
|
137
|
+
records.push(rawInput);
|
|
138
|
+
}
|
|
139
|
+
return records;
|
|
140
|
+
}
|
|
141
|
+
function readInputString(records, key) {
|
|
142
|
+
for (const record of records) {
|
|
143
|
+
const value = readString(record, key)?.trim();
|
|
144
|
+
if (value !== undefined && value.length > 0) {
|
|
145
|
+
return value;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
return undefined;
|
|
149
|
+
}
|
|
150
|
+
function readToolCategory(update, inputs, previous) {
|
|
151
|
+
const name = readString(update, "name");
|
|
152
|
+
if (name === "run_command") {
|
|
153
|
+
return "command";
|
|
154
|
+
}
|
|
155
|
+
if (name === "schedule") {
|
|
156
|
+
return "schedule";
|
|
157
|
+
}
|
|
158
|
+
if (name === "run_workflow") {
|
|
159
|
+
return "workflow";
|
|
160
|
+
}
|
|
161
|
+
if (name === "invoke_subagent") {
|
|
162
|
+
return "subagent";
|
|
163
|
+
}
|
|
164
|
+
if (name === "manage_subagents") {
|
|
165
|
+
return "manage-subagents";
|
|
166
|
+
}
|
|
167
|
+
const title = readString(update, "title");
|
|
168
|
+
if (title === "Run invoke_subagent" ||
|
|
169
|
+
title === "Running invoke_subagent" ||
|
|
170
|
+
title === "Run invoke_subagent?" ||
|
|
171
|
+
title === "Running start_subagent" ||
|
|
172
|
+
title === "Run start_subagent?") {
|
|
173
|
+
return "subagent";
|
|
174
|
+
}
|
|
175
|
+
if (title === "Run schedule" || title === "Running schedule") {
|
|
176
|
+
return "schedule";
|
|
177
|
+
}
|
|
178
|
+
if (title === "Run manage_subagents") {
|
|
179
|
+
return "manage-subagents";
|
|
180
|
+
}
|
|
181
|
+
if (previous?.category !== undefined && previous.category !== null) {
|
|
182
|
+
return previous.category;
|
|
183
|
+
}
|
|
184
|
+
if (readInputString(inputs, "CommandLine") !== undefined) {
|
|
185
|
+
return "command";
|
|
186
|
+
}
|
|
187
|
+
return null;
|
|
188
|
+
}
|
|
189
|
+
function inspectToolMetadata(update, previous) {
|
|
190
|
+
const inputs = extractToolInputRecords(update);
|
|
191
|
+
const category = readToolCategory(update, inputs, previous);
|
|
192
|
+
let isDaemon = previous?.isDaemon ?? false;
|
|
193
|
+
const subagentKillIds = new Set(previous?.subagentKillIds ?? []);
|
|
194
|
+
let subagentKillAll = previous?.subagentKillAll ?? false;
|
|
195
|
+
for (const record of inputs) {
|
|
196
|
+
if (record["IsDaemon"] === true) {
|
|
197
|
+
isDaemon = true;
|
|
198
|
+
}
|
|
199
|
+
const cron = readString(record, "CronExpression")?.trim();
|
|
200
|
+
if (cron !== undefined && cron.length > 0) {
|
|
201
|
+
isDaemon = true;
|
|
202
|
+
}
|
|
203
|
+
if (category === "manage-subagents") {
|
|
204
|
+
const action = readString(record, "Action");
|
|
205
|
+
if (action === "kill_all") {
|
|
206
|
+
subagentKillAll = true;
|
|
207
|
+
}
|
|
208
|
+
else if (action === "kill" && Array.isArray(record["ConversationIds"])) {
|
|
209
|
+
for (const rawId of record["ConversationIds"]) {
|
|
210
|
+
if (typeof rawId === "string") {
|
|
211
|
+
const normalized = rawId.trim().toLowerCase();
|
|
212
|
+
if (normalized.length > 0) {
|
|
213
|
+
subagentKillIds.add(normalized);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
const description = readInputString(inputs, "toolSummary") ??
|
|
221
|
+
readInputString(inputs, "Description") ??
|
|
222
|
+
readInputString(inputs, "Role") ??
|
|
223
|
+
readInputString(inputs, "CommandLine") ??
|
|
224
|
+
readInputString(inputs, "Prompt") ??
|
|
225
|
+
readString(update, "title") ??
|
|
226
|
+
previous?.description;
|
|
227
|
+
return {
|
|
228
|
+
category,
|
|
229
|
+
description: description === undefined ? undefined : truncate(description, MAXIMUM_TOOL_DESCRIPTION_CHARS),
|
|
230
|
+
isDaemon,
|
|
231
|
+
subagentKillAll,
|
|
232
|
+
subagentKillIds: [...subagentKillIds],
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
function backgroundKindFor(category) {
|
|
236
|
+
switch (category) {
|
|
237
|
+
case "command":
|
|
238
|
+
return "shell";
|
|
239
|
+
case "subagent":
|
|
240
|
+
return "subagent";
|
|
241
|
+
case "schedule":
|
|
242
|
+
case "workflow":
|
|
243
|
+
return "other";
|
|
244
|
+
case "manage-subagents":
|
|
245
|
+
case null:
|
|
246
|
+
return null;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
function makeMetaNotification(sessionId, metaKey, payload) {
|
|
250
|
+
return {
|
|
251
|
+
sessionId,
|
|
252
|
+
update: {
|
|
253
|
+
_meta: { [metaKey]: payload },
|
|
254
|
+
sessionUpdate: "session_info_update",
|
|
255
|
+
},
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
function isWakeupStartUpdateKind(updateKind) {
|
|
259
|
+
switch (updateKind) {
|
|
260
|
+
case "agent_message_chunk":
|
|
261
|
+
case "agent_thought_chunk":
|
|
262
|
+
case "agent_message":
|
|
263
|
+
case "agent_thought":
|
|
264
|
+
case "tool_call":
|
|
265
|
+
return true;
|
|
266
|
+
default:
|
|
267
|
+
return false;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
function isWakeupChunkUpdateKind(updateKind) {
|
|
271
|
+
return (updateKind === "agent_message_chunk" ||
|
|
272
|
+
updateKind === "agent_thought_chunk" ||
|
|
273
|
+
updateKind === "agent_message" ||
|
|
274
|
+
updateKind === "agent_thought");
|
|
275
|
+
}
|
|
276
|
+
/**
|
|
277
|
+
* Bridges `agy acp` background work (`run_command`, one-shot `schedule`, `run_workflow`,
|
|
278
|
+
* and `invoke_subagent`) and autonomous `<SYSTEM_MESSAGE>` continuations onto Mobius's
|
|
279
|
+
* session-level `mobius.backgroundTasks` and `mobius.agentTurn` protocol.
|
|
280
|
+
*/
|
|
281
|
+
export class AntigravityBackgroundWakeup {
|
|
282
|
+
#emitMetaUpdate;
|
|
283
|
+
#launchEnvironment;
|
|
284
|
+
#timer;
|
|
285
|
+
#sessions = new Map();
|
|
286
|
+
#pendingRequests = new Map();
|
|
287
|
+
#disposed = false;
|
|
288
|
+
constructor(options) {
|
|
289
|
+
this.#emitMetaUpdate = options.emitMetaUpdate;
|
|
290
|
+
this.#launchEnvironment = options.launchEnvironment;
|
|
291
|
+
this.#timer = options.timer ?? systemTimer;
|
|
292
|
+
}
|
|
293
|
+
#ensureSession = (sessionId) => {
|
|
294
|
+
const existing = this.#sessions.get(sessionId);
|
|
295
|
+
if (existing !== undefined) {
|
|
296
|
+
return existing;
|
|
297
|
+
}
|
|
298
|
+
const created = {
|
|
299
|
+
activeInteractions: 0,
|
|
300
|
+
backgroundTasks: new Map(),
|
|
301
|
+
baselineStepIndex: -1,
|
|
302
|
+
lastEmittedBackgroundKey: "[]",
|
|
303
|
+
phase: { cancelRequested: false, kind: "idle" },
|
|
304
|
+
sessionOpenStepIndex: -1,
|
|
305
|
+
subagents: {
|
|
306
|
+
boundCreationSteps: new Set(),
|
|
307
|
+
consumedIdleSteps: new Set(),
|
|
308
|
+
conversationIdByToolCallId: new Map(),
|
|
309
|
+
pendingToolCallIds: [],
|
|
310
|
+
},
|
|
311
|
+
toolMetadata: new Map(),
|
|
312
|
+
wakeupGeneration: 0,
|
|
313
|
+
wakeupStageSeq: 0,
|
|
314
|
+
};
|
|
315
|
+
this.#sessions.set(sessionId, created);
|
|
316
|
+
return created;
|
|
317
|
+
};
|
|
318
|
+
#cancelWakeupTimers = (phase) => {
|
|
319
|
+
if (phase.kind !== "wakeup") {
|
|
320
|
+
return;
|
|
321
|
+
}
|
|
322
|
+
switch (phase.stage.kind) {
|
|
323
|
+
case "awaiting-output":
|
|
324
|
+
phase.stage.timeout.cancel();
|
|
325
|
+
phase.stage.transcriptCheck.cancel();
|
|
326
|
+
return;
|
|
327
|
+
case "blocked-on-work":
|
|
328
|
+
case "verifying-timeout":
|
|
329
|
+
return;
|
|
330
|
+
case "streaming":
|
|
331
|
+
phase.stage.transcriptCheck.cancel();
|
|
332
|
+
phase.stage.inactivityTimeout.cancel();
|
|
333
|
+
return;
|
|
334
|
+
default: {
|
|
335
|
+
const unreachable = phase.stage;
|
|
336
|
+
throw new Error(`Unexpected wakeup stage: ${String(unreachable)}`);
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
};
|
|
340
|
+
#emitNotifications = (notifications) => {
|
|
341
|
+
for (const notification of notifications) {
|
|
342
|
+
this.#emitMetaUpdate(notification);
|
|
343
|
+
}
|
|
344
|
+
};
|
|
345
|
+
#takeBackgroundTasksUpdate = (sessionId, session) => {
|
|
346
|
+
const tasks = [...session.backgroundTasks.values()];
|
|
347
|
+
const serialized = JSON.stringify(tasks);
|
|
348
|
+
if (serialized === session.lastEmittedBackgroundKey) {
|
|
349
|
+
return null;
|
|
350
|
+
}
|
|
351
|
+
session.lastEmittedBackgroundKey = serialized;
|
|
352
|
+
return makeMetaNotification(sessionId, BACKGROUND_TASKS_META, tasks);
|
|
353
|
+
};
|
|
354
|
+
#registerSubagentTask = (session, toolCallId, description) => {
|
|
355
|
+
if (!session.backgroundTasks.has(toolCallId)) {
|
|
356
|
+
session.backgroundTasks.set(toolCallId, {
|
|
357
|
+
...(description === undefined ? {} : { description }),
|
|
358
|
+
kind: "subagent",
|
|
359
|
+
state: "running",
|
|
360
|
+
taskId: toolCallId,
|
|
361
|
+
toolUseId: toolCallId,
|
|
362
|
+
});
|
|
363
|
+
}
|
|
364
|
+
if (!session.subagents.conversationIdByToolCallId.has(toolCallId) &&
|
|
365
|
+
!session.subagents.pendingToolCallIds.includes(toolCallId)) {
|
|
366
|
+
session.subagents.pendingToolCallIds.push(toolCallId);
|
|
367
|
+
}
|
|
368
|
+
};
|
|
369
|
+
#deleteSubagentTask = (session, toolCallId) => {
|
|
370
|
+
session.backgroundTasks.delete(toolCallId);
|
|
371
|
+
session.subagents.conversationIdByToolCallId.delete(toolCallId);
|
|
372
|
+
const pendingIndex = session.subagents.pendingToolCallIds.indexOf(toolCallId);
|
|
373
|
+
if (pendingIndex !== -1) {
|
|
374
|
+
session.subagents.pendingToolCallIds.splice(pendingIndex, 1);
|
|
375
|
+
}
|
|
376
|
+
};
|
|
377
|
+
#deleteAllSubagentTasks = (session) => {
|
|
378
|
+
for (const [taskId, task] of [...session.backgroundTasks.entries()]) {
|
|
379
|
+
if (task.kind === "subagent") {
|
|
380
|
+
this.#deleteSubagentTask(session, taskId);
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
};
|
|
384
|
+
#clearAllBackgroundTasks = (session) => {
|
|
385
|
+
session.backgroundTasks.clear();
|
|
386
|
+
session.subagents.conversationIdByToolCallId.clear();
|
|
387
|
+
session.subagents.pendingToolCallIds.length = 0;
|
|
388
|
+
};
|
|
389
|
+
#dropAwaitingResultTasks = (session) => {
|
|
390
|
+
for (const [taskId, task] of [...session.backgroundTasks.entries()]) {
|
|
391
|
+
if (task.state === "awaiting-results") {
|
|
392
|
+
if (task.kind === "subagent") {
|
|
393
|
+
this.#deleteSubagentTask(session, taskId);
|
|
394
|
+
}
|
|
395
|
+
else {
|
|
396
|
+
session.backgroundTasks.delete(taskId);
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
};
|
|
401
|
+
#hasSubagentTasks = (session) => {
|
|
402
|
+
for (const task of session.backgroundTasks.values()) {
|
|
403
|
+
if (task.kind === "subagent") {
|
|
404
|
+
return true;
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
return false;
|
|
408
|
+
};
|
|
409
|
+
#syncSubagentCreations = (session, steps) => {
|
|
410
|
+
if (session.subagents.pendingToolCallIds.length === 0) {
|
|
411
|
+
return;
|
|
412
|
+
}
|
|
413
|
+
for (const step of steps) {
|
|
414
|
+
if (step.step_index <= session.sessionOpenStepIndex ||
|
|
415
|
+
step.type !== "GENERIC" ||
|
|
416
|
+
step.status !== "DONE" ||
|
|
417
|
+
session.subagents.boundCreationSteps.has(step.step_index) ||
|
|
418
|
+
step.content === undefined ||
|
|
419
|
+
!step.content.includes(SUBAGENT_CREATED_MARKER)) {
|
|
420
|
+
continue;
|
|
421
|
+
}
|
|
422
|
+
const createdIds = extractUuidMatches(step.content, SUBAGENT_CONVERSATION_ID_PATTERN);
|
|
423
|
+
if (createdIds.length === 0) {
|
|
424
|
+
continue;
|
|
425
|
+
}
|
|
426
|
+
let boundAny = false;
|
|
427
|
+
for (const conversationId of createdIds) {
|
|
428
|
+
let nextToolCallId;
|
|
429
|
+
while (session.subagents.pendingToolCallIds.length > 0) {
|
|
430
|
+
const candidate = session.subagents.pendingToolCallIds.shift();
|
|
431
|
+
if (candidate !== undefined && session.backgroundTasks.has(candidate)) {
|
|
432
|
+
nextToolCallId = candidate;
|
|
433
|
+
break;
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
if (nextToolCallId === undefined) {
|
|
437
|
+
break;
|
|
438
|
+
}
|
|
439
|
+
session.subagents.conversationIdByToolCallId.set(nextToolCallId, conversationId);
|
|
440
|
+
boundAny = true;
|
|
441
|
+
}
|
|
442
|
+
if (boundAny) {
|
|
443
|
+
session.subagents.boundCreationSteps.add(step.step_index);
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
};
|
|
447
|
+
#findSubagentToolCallId = (session, conversationId) => {
|
|
448
|
+
const targetId = conversationId.toLowerCase();
|
|
449
|
+
for (const [toolCallId, mappedId] of session.subagents.conversationIdByToolCallId) {
|
|
450
|
+
if (mappedId === targetId && session.backgroundTasks.has(toolCallId)) {
|
|
451
|
+
return toolCallId;
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
return undefined;
|
|
455
|
+
};
|
|
456
|
+
#processIdleSubagentMessages = (session, steps, maxStepExclusive, onMatchedToolCall) => {
|
|
457
|
+
for (const step of steps) {
|
|
458
|
+
if (step.step_index <= session.sessionOpenStepIndex ||
|
|
459
|
+
(maxStepExclusive !== undefined && step.step_index >= maxStepExclusive) ||
|
|
460
|
+
step.type !== "SYSTEM_MESSAGE" ||
|
|
461
|
+
session.subagents.consumedIdleSteps.has(step.step_index) ||
|
|
462
|
+
step.content === undefined) {
|
|
463
|
+
continue;
|
|
464
|
+
}
|
|
465
|
+
const idleIds = extractUuidMatches(step.content, SUBAGENT_IDLE_ID_PATTERN);
|
|
466
|
+
if (idleIds.length === 0) {
|
|
467
|
+
continue;
|
|
468
|
+
}
|
|
469
|
+
session.subagents.consumedIdleSteps.add(step.step_index);
|
|
470
|
+
for (const conversationId of idleIds) {
|
|
471
|
+
const toolCallId = this.#findSubagentToolCallId(session, conversationId);
|
|
472
|
+
if (toolCallId !== undefined) {
|
|
473
|
+
onMatchedToolCall(toolCallId);
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
};
|
|
478
|
+
#markTriggeredSubagentIdles = (session, steps) => {
|
|
479
|
+
const matched = new Set();
|
|
480
|
+
this.#processIdleSubagentMessages(session, steps, undefined, (toolCallId) => {
|
|
481
|
+
const existing = session.backgroundTasks.get(toolCallId);
|
|
482
|
+
if (existing !== undefined) {
|
|
483
|
+
session.backgroundTasks.set(toolCallId, { ...existing, state: "awaiting-results" });
|
|
484
|
+
matched.add(toolCallId);
|
|
485
|
+
}
|
|
486
|
+
});
|
|
487
|
+
return [...matched];
|
|
488
|
+
};
|
|
489
|
+
#consumeSubagentIdlesThrough = (session, steps, plannerStepIndex) => {
|
|
490
|
+
this.#processIdleSubagentMessages(session, steps, plannerStepIndex, (toolCallId) => {
|
|
491
|
+
this.#deleteSubagentTask(session, toolCallId);
|
|
492
|
+
});
|
|
493
|
+
};
|
|
494
|
+
#killSubagentsByMetadata = async (sessionId, session, meta) => {
|
|
495
|
+
if (meta.subagentKillAll) {
|
|
496
|
+
this.#deleteAllSubagentTasks(session);
|
|
497
|
+
return true;
|
|
498
|
+
}
|
|
499
|
+
if (meta.subagentKillIds.length === 0) {
|
|
500
|
+
return true;
|
|
501
|
+
}
|
|
502
|
+
await this.#syncPendingSubagentCreations(sessionId, session);
|
|
503
|
+
if (this.#disposed || this.#sessions.get(sessionId) !== session) {
|
|
504
|
+
return false;
|
|
505
|
+
}
|
|
506
|
+
for (const conversationId of meta.subagentKillIds) {
|
|
507
|
+
const targetToolCallId = this.#findSubagentToolCallId(session, conversationId);
|
|
508
|
+
if (targetToolCallId !== undefined) {
|
|
509
|
+
this.#deleteSubagentTask(session, targetToolCallId);
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
return true;
|
|
513
|
+
};
|
|
514
|
+
#syncPendingSubagentCreations = async (sessionId, session) => {
|
|
515
|
+
if (session.subagents.pendingToolCallIds.length === 0) {
|
|
516
|
+
return;
|
|
517
|
+
}
|
|
518
|
+
const tail = await readTranscriptTailState(this.#launchEnvironment, sessionId, session.baselineStepIndex);
|
|
519
|
+
if (this.#disposed || this.#sessions.get(sessionId) !== session || tail === null) {
|
|
520
|
+
return;
|
|
521
|
+
}
|
|
522
|
+
this.#syncSubagentCreations(session, tail.steps);
|
|
523
|
+
};
|
|
524
|
+
#scheduleTranscriptPoll = (sessionId, generation, stageEpoch, delayMs) => this.#timer.schedule(() => {
|
|
525
|
+
void this.#checkWakeupTranscript(sessionId, generation, stageEpoch);
|
|
526
|
+
}, delayMs);
|
|
527
|
+
#setWakeupAwaitingOutput = (sessionId, session, generation, openToolIds) => {
|
|
528
|
+
this.#cancelWakeupTimers(session.phase);
|
|
529
|
+
session.wakeupStageSeq += 1;
|
|
530
|
+
const stageEpoch = session.wakeupStageSeq;
|
|
531
|
+
const transcriptCheck = this.#scheduleTranscriptPoll(sessionId, generation, stageEpoch, TRANSCRIPT_INITIAL_DELAY_MS);
|
|
532
|
+
const timeout = this.#timer.schedule(() => {
|
|
533
|
+
void this.#handleWakeupTimeout(sessionId, generation, stageEpoch, "Antigravity continuation timed out waiting for output");
|
|
534
|
+
}, WAKEUP_INACTIVITY_TIMEOUT_MS);
|
|
535
|
+
session.phase = {
|
|
536
|
+
generation,
|
|
537
|
+
kind: "wakeup",
|
|
538
|
+
openToolIds,
|
|
539
|
+
stage: { kind: "awaiting-output", stageEpoch, timeout, transcriptCheck },
|
|
540
|
+
};
|
|
541
|
+
};
|
|
542
|
+
#setWakeupBlockedOnWork = (session, generation, openToolIds) => {
|
|
543
|
+
this.#cancelWakeupTimers(session.phase);
|
|
544
|
+
session.wakeupStageSeq += 1;
|
|
545
|
+
const stageEpoch = session.wakeupStageSeq;
|
|
546
|
+
session.phase = {
|
|
547
|
+
generation,
|
|
548
|
+
kind: "wakeup",
|
|
549
|
+
openToolIds,
|
|
550
|
+
stage: { kind: "blocked-on-work", stageEpoch },
|
|
551
|
+
};
|
|
552
|
+
};
|
|
553
|
+
#setWakeupStreaming = (sessionId, session, generation, openToolIds, incomingExecutorError) => {
|
|
554
|
+
const previousExecutorError = session.phase.kind === "wakeup" &&
|
|
555
|
+
session.phase.generation === generation &&
|
|
556
|
+
session.phase.stage.kind === "streaming"
|
|
557
|
+
? session.phase.stage.executorError
|
|
558
|
+
: undefined;
|
|
559
|
+
const executorError = incomingExecutorError ?? previousExecutorError;
|
|
560
|
+
this.#cancelWakeupTimers(session.phase);
|
|
561
|
+
session.wakeupStageSeq += 1;
|
|
562
|
+
const stageEpoch = session.wakeupStageSeq;
|
|
563
|
+
const transcriptCheck = this.#scheduleTranscriptPoll(sessionId, generation, stageEpoch, TRANSCRIPT_INITIAL_DELAY_MS);
|
|
564
|
+
const inactivityTimeout = this.#timer.schedule(() => {
|
|
565
|
+
void this.#handleWakeupTimeout(sessionId, generation, stageEpoch, "Antigravity continuation timed out while streaming");
|
|
566
|
+
}, WAKEUP_INACTIVITY_TIMEOUT_MS);
|
|
567
|
+
session.phase = {
|
|
568
|
+
generation,
|
|
569
|
+
kind: "wakeup",
|
|
570
|
+
openToolIds,
|
|
571
|
+
stage: {
|
|
572
|
+
executorError,
|
|
573
|
+
executorErrorPolls: 0,
|
|
574
|
+
inactivityTimeout,
|
|
575
|
+
kind: "streaming",
|
|
576
|
+
stageEpoch,
|
|
577
|
+
transcriptCheck,
|
|
578
|
+
},
|
|
579
|
+
};
|
|
580
|
+
};
|
|
581
|
+
#enterWakeup = (sessionId, session, taskIds) => {
|
|
582
|
+
const notifications = [];
|
|
583
|
+
const bgNotification = this.#takeBackgroundTasksUpdate(sessionId, session);
|
|
584
|
+
if (bgNotification !== null) {
|
|
585
|
+
notifications.push(bgNotification);
|
|
586
|
+
}
|
|
587
|
+
session.wakeupGeneration += 1;
|
|
588
|
+
const generation = session.wakeupGeneration;
|
|
589
|
+
this.#setWakeupAwaitingOutput(sessionId, session, generation, new Set());
|
|
590
|
+
notifications.push(makeMetaNotification(sessionId, AGENT_TURN_META, {
|
|
591
|
+
reason: "background-task",
|
|
592
|
+
stage: "started",
|
|
593
|
+
taskIds: [...taskIds],
|
|
594
|
+
}));
|
|
595
|
+
return notifications;
|
|
596
|
+
};
|
|
597
|
+
#finishWakeup = (sessionId, session, stopReason, cancelRequested) => {
|
|
598
|
+
if (session.phase.kind !== "wakeup") {
|
|
599
|
+
return [];
|
|
600
|
+
}
|
|
601
|
+
this.#cancelWakeupTimers(session.phase);
|
|
602
|
+
session.phase = { cancelRequested, kind: "idle" };
|
|
603
|
+
this.#dropAwaitingResultTasks(session);
|
|
604
|
+
const notifications = [];
|
|
605
|
+
const bgNotification = this.#takeBackgroundTasksUpdate(sessionId, session);
|
|
606
|
+
if (bgNotification !== null) {
|
|
607
|
+
notifications.push(bgNotification);
|
|
608
|
+
}
|
|
609
|
+
notifications.push(makeMetaNotification(sessionId, AGENT_TURN_META, {
|
|
610
|
+
stage: "ended",
|
|
611
|
+
stopReason,
|
|
612
|
+
}));
|
|
613
|
+
return notifications;
|
|
614
|
+
};
|
|
615
|
+
#failWakeup = (sessionId, session, message) => {
|
|
616
|
+
if (session.phase.kind !== "wakeup") {
|
|
617
|
+
return [];
|
|
618
|
+
}
|
|
619
|
+
this.#cancelWakeupTimers(session.phase);
|
|
620
|
+
session.phase = { cancelRequested: false, kind: "idle" };
|
|
621
|
+
this.#dropAwaitingResultTasks(session);
|
|
622
|
+
const notifications = [];
|
|
623
|
+
const bgNotification = this.#takeBackgroundTasksUpdate(sessionId, session);
|
|
624
|
+
if (bgNotification !== null) {
|
|
625
|
+
notifications.push(bgNotification);
|
|
626
|
+
}
|
|
627
|
+
notifications.push(makeMetaNotification(sessionId, AGENT_TURN_META, {
|
|
628
|
+
message,
|
|
629
|
+
stage: "failed",
|
|
630
|
+
}));
|
|
631
|
+
return notifications;
|
|
632
|
+
};
|
|
633
|
+
#rescheduleTranscriptCheck = (sessionId, session, generation, stageEpoch, stage) => {
|
|
634
|
+
if (session.phase.kind !== "wakeup") {
|
|
635
|
+
return;
|
|
636
|
+
}
|
|
637
|
+
const nextCheck = this.#scheduleTranscriptPoll(sessionId, generation, stageEpoch, TRANSCRIPT_POLL_INTERVAL_MS);
|
|
638
|
+
switch (stage.kind) {
|
|
639
|
+
case "awaiting-output":
|
|
640
|
+
session.phase = {
|
|
641
|
+
...session.phase,
|
|
642
|
+
stage: { ...stage, transcriptCheck: nextCheck },
|
|
643
|
+
};
|
|
644
|
+
return;
|
|
645
|
+
case "streaming":
|
|
646
|
+
session.phase = {
|
|
647
|
+
...session.phase,
|
|
648
|
+
stage: {
|
|
649
|
+
...stage,
|
|
650
|
+
executorErrorPolls: stage.executorError === undefined ? 0 : stage.executorErrorPolls + 1,
|
|
651
|
+
transcriptCheck: nextCheck,
|
|
652
|
+
},
|
|
653
|
+
};
|
|
654
|
+
return;
|
|
655
|
+
}
|
|
656
|
+
};
|
|
657
|
+
#checkWakeupTranscript = async (sessionId, generation, stageEpoch) => {
|
|
658
|
+
if (this.#disposed) {
|
|
659
|
+
return;
|
|
660
|
+
}
|
|
661
|
+
const session = this.#sessions.get(sessionId);
|
|
662
|
+
if (session === undefined || session.phase.kind !== "wakeup" || session.phase.generation !== generation) {
|
|
663
|
+
return;
|
|
664
|
+
}
|
|
665
|
+
if (session.phase.stage.stageEpoch !== stageEpoch ||
|
|
666
|
+
(session.phase.stage.kind !== "streaming" && session.phase.stage.kind !== "awaiting-output")) {
|
|
667
|
+
return;
|
|
668
|
+
}
|
|
669
|
+
const tail = await readTranscriptTailState(this.#launchEnvironment, sessionId, session.baselineStepIndex);
|
|
670
|
+
if (this.#disposed) {
|
|
671
|
+
return;
|
|
672
|
+
}
|
|
673
|
+
const current = this.#sessions.get(sessionId);
|
|
674
|
+
if (current === undefined || current.phase.kind !== "wakeup" || current.phase.generation !== generation) {
|
|
675
|
+
return;
|
|
676
|
+
}
|
|
677
|
+
const currentStage = current.phase.stage;
|
|
678
|
+
if (currentStage.stageEpoch !== stageEpoch ||
|
|
679
|
+
(currentStage.kind !== "streaming" && currentStage.kind !== "awaiting-output")) {
|
|
680
|
+
return;
|
|
681
|
+
}
|
|
682
|
+
if (tail !== null) {
|
|
683
|
+
this.#syncSubagentCreations(current, tail.steps);
|
|
684
|
+
}
|
|
685
|
+
if (tail?.settledStepIndex !== null && tail?.settledStepIndex !== undefined) {
|
|
686
|
+
current.baselineStepIndex = Math.max(current.baselineStepIndex, tail.settledStepIndex);
|
|
687
|
+
this.#consumeSubagentIdlesThrough(current, tail.steps, tail.settledStepIndex);
|
|
688
|
+
this.#emitNotifications(this.#finishWakeup(sessionId, current, "end_turn", false));
|
|
689
|
+
return;
|
|
690
|
+
}
|
|
691
|
+
if (currentStage.kind === "streaming" &&
|
|
692
|
+
currentStage.executorError !== undefined &&
|
|
693
|
+
currentStage.executorErrorPolls >= 1) {
|
|
694
|
+
if (tail !== null && tail.maxStepIndex > current.baselineStepIndex) {
|
|
695
|
+
current.baselineStepIndex = tail.maxStepIndex;
|
|
696
|
+
}
|
|
697
|
+
this.#emitNotifications(this.#failWakeup(sessionId, current, currentStage.executorError));
|
|
698
|
+
return;
|
|
699
|
+
}
|
|
700
|
+
this.#rescheduleTranscriptCheck(sessionId, current, generation, stageEpoch, currentStage);
|
|
701
|
+
};
|
|
702
|
+
#handleWakeupTimeout = async (sessionId, generation, stageEpoch, message) => {
|
|
703
|
+
if (this.#disposed) {
|
|
704
|
+
return;
|
|
705
|
+
}
|
|
706
|
+
const session = this.#sessions.get(sessionId);
|
|
707
|
+
if (session === undefined || session.phase.kind !== "wakeup" || session.phase.generation !== generation) {
|
|
708
|
+
return;
|
|
709
|
+
}
|
|
710
|
+
if (session.phase.stage.stageEpoch !== stageEpoch ||
|
|
711
|
+
(session.phase.stage.kind !== "awaiting-output" && session.phase.stage.kind !== "streaming")) {
|
|
712
|
+
return;
|
|
713
|
+
}
|
|
714
|
+
this.#cancelWakeupTimers(session.phase);
|
|
715
|
+
session.wakeupStageSeq += 1;
|
|
716
|
+
const timeoutEpoch = session.wakeupStageSeq;
|
|
717
|
+
session.phase = {
|
|
718
|
+
...session.phase,
|
|
719
|
+
stage: { kind: "verifying-timeout", message, stageEpoch: timeoutEpoch },
|
|
720
|
+
};
|
|
721
|
+
const tail = await readTranscriptTailState(this.#launchEnvironment, sessionId, session.baselineStepIndex);
|
|
722
|
+
if (this.#disposed) {
|
|
723
|
+
return;
|
|
724
|
+
}
|
|
725
|
+
const current = this.#sessions.get(sessionId);
|
|
726
|
+
if (current === undefined || current.phase.kind !== "wakeup" || current.phase.generation !== generation) {
|
|
727
|
+
return;
|
|
728
|
+
}
|
|
729
|
+
if (current.phase.stage.kind !== "verifying-timeout" || current.phase.stage.stageEpoch !== timeoutEpoch) {
|
|
730
|
+
return;
|
|
731
|
+
}
|
|
732
|
+
if (tail !== null) {
|
|
733
|
+
this.#syncSubagentCreations(current, tail.steps);
|
|
734
|
+
}
|
|
735
|
+
if (tail?.settledStepIndex !== null && tail?.settledStepIndex !== undefined) {
|
|
736
|
+
current.baselineStepIndex = Math.max(current.baselineStepIndex, tail.settledStepIndex);
|
|
737
|
+
this.#consumeSubagentIdlesThrough(current, tail.steps, tail.settledStepIndex);
|
|
738
|
+
this.#emitNotifications(this.#finishWakeup(sessionId, current, "end_turn", false));
|
|
739
|
+
return;
|
|
740
|
+
}
|
|
741
|
+
if (tail !== null && tail.maxStepIndex > current.baselineStepIndex) {
|
|
742
|
+
current.baselineStepIndex = tail.maxStepIndex;
|
|
743
|
+
}
|
|
744
|
+
this.#emitNotifications(this.#failWakeup(sessionId, current, message));
|
|
745
|
+
};
|
|
746
|
+
#promoteOpenWakeupToolsToBackground = (session) => {
|
|
747
|
+
if (session.phase.kind !== "wakeup" || session.phase.openToolIds.size === 0) {
|
|
748
|
+
return;
|
|
749
|
+
}
|
|
750
|
+
const remainingOpenToolIds = new Set(session.phase.openToolIds);
|
|
751
|
+
let changed = false;
|
|
752
|
+
for (const toolCallId of session.phase.openToolIds) {
|
|
753
|
+
const meta = session.toolMetadata.get(toolCallId);
|
|
754
|
+
if (meta === undefined || meta.isDaemon) {
|
|
755
|
+
continue;
|
|
756
|
+
}
|
|
757
|
+
const kind = backgroundKindFor(meta.category);
|
|
758
|
+
if (kind === "shell" || kind === "other") {
|
|
759
|
+
remainingOpenToolIds.delete(toolCallId);
|
|
760
|
+
changed = true;
|
|
761
|
+
session.backgroundTasks.set(toolCallId, {
|
|
762
|
+
...(meta.description === undefined ? {} : { description: meta.description }),
|
|
763
|
+
kind,
|
|
764
|
+
state: "running",
|
|
765
|
+
taskId: toolCallId,
|
|
766
|
+
toolUseId: toolCallId,
|
|
767
|
+
});
|
|
768
|
+
}
|
|
769
|
+
}
|
|
770
|
+
if (changed) {
|
|
771
|
+
session.phase = { ...session.phase, openToolIds: remainingOpenToolIds };
|
|
772
|
+
}
|
|
773
|
+
};
|
|
774
|
+
#pauseOrResumeWakeupTimers = (sessionId, session) => {
|
|
775
|
+
if (session.phase.kind !== "wakeup") {
|
|
776
|
+
return;
|
|
777
|
+
}
|
|
778
|
+
if (session.phase.openToolIds.size > 0 || session.activeInteractions > 0) {
|
|
779
|
+
if (session.phase.stage.kind !== "blocked-on-work") {
|
|
780
|
+
this.#setWakeupBlockedOnWork(session, session.phase.generation, session.phase.openToolIds);
|
|
781
|
+
}
|
|
782
|
+
}
|
|
783
|
+
else if (session.phase.stage.kind === "blocked-on-work") {
|
|
784
|
+
this.#setWakeupAwaitingOutput(sessionId, session, session.phase.generation, session.phase.openToolIds);
|
|
785
|
+
}
|
|
786
|
+
};
|
|
787
|
+
#applyClientPromptToolUpdate = async (sessionId, session, update, cancelled) => {
|
|
788
|
+
const toolCallId = readString(update, "toolCallId");
|
|
789
|
+
const status = readString(update, "status");
|
|
790
|
+
if (toolCallId === undefined || status === undefined) {
|
|
791
|
+
return;
|
|
792
|
+
}
|
|
793
|
+
const meta = inspectToolMetadata(update, session.toolMetadata.get(toolCallId));
|
|
794
|
+
session.toolMetadata.set(toolCallId, meta);
|
|
795
|
+
if (meta.isDaemon) {
|
|
796
|
+
session.backgroundTasks.delete(toolCallId);
|
|
797
|
+
return;
|
|
798
|
+
}
|
|
799
|
+
if (meta.category === "manage-subagents" && status === "completed") {
|
|
800
|
+
await this.#killSubagentsByMetadata(sessionId, session, meta);
|
|
801
|
+
return;
|
|
802
|
+
}
|
|
803
|
+
if (meta.category === "subagent") {
|
|
804
|
+
if (!cancelled && status === "completed") {
|
|
805
|
+
this.#registerSubagentTask(session, toolCallId, meta.description);
|
|
806
|
+
await this.#syncPendingSubagentCreations(sessionId, session);
|
|
807
|
+
}
|
|
808
|
+
return;
|
|
809
|
+
}
|
|
810
|
+
const kind = backgroundKindFor(meta.category);
|
|
811
|
+
if (kind === null) {
|
|
812
|
+
return;
|
|
813
|
+
}
|
|
814
|
+
if (!cancelled && status === "in_progress") {
|
|
815
|
+
session.backgroundTasks.set(toolCallId, {
|
|
816
|
+
...(meta.description === undefined ? {} : { description: meta.description }),
|
|
817
|
+
kind,
|
|
818
|
+
state: "running",
|
|
819
|
+
taskId: toolCallId,
|
|
820
|
+
toolUseId: toolCallId,
|
|
821
|
+
});
|
|
822
|
+
return;
|
|
823
|
+
}
|
|
824
|
+
if (status === "pending" || status === "completed" || status === "failed" || status === "cancelled") {
|
|
825
|
+
session.backgroundTasks.delete(toolCallId);
|
|
826
|
+
}
|
|
827
|
+
};
|
|
828
|
+
#applyWakeupToolUpdate = async (sessionId, session, update, notifications) => {
|
|
829
|
+
if (session.phase.kind !== "wakeup") {
|
|
830
|
+
return;
|
|
831
|
+
}
|
|
832
|
+
const toolCallId = readString(update, "toolCallId");
|
|
833
|
+
const status = readString(update, "status");
|
|
834
|
+
const updateKind = readString(update, "sessionUpdate");
|
|
835
|
+
if (toolCallId === undefined || status === undefined || updateKind === undefined) {
|
|
836
|
+
return;
|
|
837
|
+
}
|
|
838
|
+
const meta = inspectToolMetadata(update, session.toolMetadata.get(toolCallId));
|
|
839
|
+
session.toolMetadata.set(toolCallId, meta);
|
|
840
|
+
const trackedExisting = session.backgroundTasks.get(toolCallId);
|
|
841
|
+
if (trackedExisting !== undefined) {
|
|
842
|
+
if (status === "completed") {
|
|
843
|
+
session.backgroundTasks.set(toolCallId, { ...trackedExisting, state: "awaiting-results" });
|
|
844
|
+
const bg = this.#takeBackgroundTasksUpdate(sessionId, session);
|
|
845
|
+
if (bg !== null) {
|
|
846
|
+
notifications.push(bg);
|
|
847
|
+
}
|
|
848
|
+
if (session.phase.stage.kind === "streaming") {
|
|
849
|
+
this.#setWakeupAwaitingOutput(sessionId, session, session.phase.generation, session.phase.openToolIds);
|
|
850
|
+
}
|
|
851
|
+
}
|
|
852
|
+
else if (status === "failed" || status === "cancelled") {
|
|
853
|
+
if (trackedExisting.kind === "subagent") {
|
|
854
|
+
this.#deleteSubagentTask(session, toolCallId);
|
|
855
|
+
}
|
|
856
|
+
else {
|
|
857
|
+
session.backgroundTasks.delete(toolCallId);
|
|
858
|
+
}
|
|
859
|
+
const bg = this.#takeBackgroundTasksUpdate(sessionId, session);
|
|
860
|
+
if (bg !== null) {
|
|
861
|
+
notifications.push(bg);
|
|
862
|
+
}
|
|
863
|
+
}
|
|
864
|
+
return;
|
|
865
|
+
}
|
|
866
|
+
if (meta.isDaemon) {
|
|
867
|
+
if (session.phase.openToolIds.has(toolCallId)) {
|
|
868
|
+
const nextOpenToolIds = new Set(session.phase.openToolIds);
|
|
869
|
+
nextOpenToolIds.delete(toolCallId);
|
|
870
|
+
session.phase = { ...session.phase, openToolIds: nextOpenToolIds };
|
|
871
|
+
}
|
|
872
|
+
return;
|
|
873
|
+
}
|
|
874
|
+
if (updateKind === "tool_call" && (status === "in_progress" || status === "pending")) {
|
|
875
|
+
if (!session.phase.openToolIds.has(toolCallId)) {
|
|
876
|
+
const nextOpenToolIds = new Set(session.phase.openToolIds);
|
|
877
|
+
nextOpenToolIds.add(toolCallId);
|
|
878
|
+
session.phase = { ...session.phase, openToolIds: nextOpenToolIds };
|
|
879
|
+
}
|
|
880
|
+
this.#pauseOrResumeWakeupTimers(sessionId, session);
|
|
881
|
+
return;
|
|
882
|
+
}
|
|
883
|
+
if (status === "completed" || status === "failed" || status === "cancelled") {
|
|
884
|
+
if (session.phase.openToolIds.has(toolCallId)) {
|
|
885
|
+
const nextOpenToolIds = new Set(session.phase.openToolIds);
|
|
886
|
+
nextOpenToolIds.delete(toolCallId);
|
|
887
|
+
session.phase = { ...session.phase, openToolIds: nextOpenToolIds };
|
|
888
|
+
}
|
|
889
|
+
if (meta.category === "subagent" && status === "completed") {
|
|
890
|
+
this.#registerSubagentTask(session, toolCallId, meta.description);
|
|
891
|
+
await this.#syncPendingSubagentCreations(sessionId, session);
|
|
892
|
+
if (this.#disposed || this.#sessions.get(sessionId) !== session) {
|
|
893
|
+
return;
|
|
894
|
+
}
|
|
895
|
+
const bg = this.#takeBackgroundTasksUpdate(sessionId, session);
|
|
896
|
+
if (bg !== null) {
|
|
897
|
+
notifications.push(bg);
|
|
898
|
+
}
|
|
899
|
+
}
|
|
900
|
+
else if (meta.category === "manage-subagents" && status === "completed") {
|
|
901
|
+
const active = await this.#killSubagentsByMetadata(sessionId, session, meta);
|
|
902
|
+
if (!active) {
|
|
903
|
+
return;
|
|
904
|
+
}
|
|
905
|
+
const bg = this.#takeBackgroundTasksUpdate(sessionId, session);
|
|
906
|
+
if (bg !== null) {
|
|
907
|
+
notifications.push(bg);
|
|
908
|
+
}
|
|
909
|
+
}
|
|
910
|
+
this.#pauseOrResumeWakeupTimers(sessionId, session);
|
|
911
|
+
}
|
|
912
|
+
};
|
|
913
|
+
onOutgoingMessage = async (message) => {
|
|
914
|
+
if (this.#disposed || !isRequestMessage(message)) {
|
|
915
|
+
return;
|
|
916
|
+
}
|
|
917
|
+
const key = requestKeyOf(message.id);
|
|
918
|
+
const params = isRecord(message.params) ? message.params : undefined;
|
|
919
|
+
const sessionId = params === undefined ? undefined : readString(params, "sessionId");
|
|
920
|
+
switch (message.method) {
|
|
921
|
+
case methods.agent.session.load: {
|
|
922
|
+
if (sessionId === undefined) {
|
|
923
|
+
return;
|
|
924
|
+
}
|
|
925
|
+
const session = this.#ensureSession(sessionId);
|
|
926
|
+
this.#cancelWakeupTimers(session.phase);
|
|
927
|
+
session.phase = { kind: "loading", requestKey: key };
|
|
928
|
+
if (key !== undefined) {
|
|
929
|
+
this.#pendingRequests.set(key, { kind: "load", sessionId });
|
|
930
|
+
}
|
|
931
|
+
return;
|
|
932
|
+
}
|
|
933
|
+
case methods.agent.session.new:
|
|
934
|
+
case methods.agent.session.resume: {
|
|
935
|
+
if (sessionId !== undefined && key !== undefined) {
|
|
936
|
+
this.#pendingRequests.set(key, { kind: "session-open", sessionId });
|
|
937
|
+
}
|
|
938
|
+
return;
|
|
939
|
+
}
|
|
940
|
+
case methods.agent.session.prompt: {
|
|
941
|
+
if (sessionId === undefined) {
|
|
942
|
+
return;
|
|
943
|
+
}
|
|
944
|
+
const session = this.#ensureSession(sessionId);
|
|
945
|
+
if (session.phase.kind === "wakeup") {
|
|
946
|
+
this.#emitNotifications(this.#finishWakeup(sessionId, session, "end_turn", false));
|
|
947
|
+
}
|
|
948
|
+
const tail = await readTranscriptTailState(this.#launchEnvironment, sessionId, session.baselineStepIndex);
|
|
949
|
+
const prePromptStepIndex = Math.max(session.baselineStepIndex, tail?.maxStepIndex ?? -1);
|
|
950
|
+
session.baselineStepIndex = prePromptStepIndex;
|
|
951
|
+
session.phase = {
|
|
952
|
+
cancelled: false,
|
|
953
|
+
kind: "client-prompt",
|
|
954
|
+
prePromptStepIndex,
|
|
955
|
+
requestKey: key,
|
|
956
|
+
};
|
|
957
|
+
if (key !== undefined) {
|
|
958
|
+
this.#pendingRequests.set(key, { kind: "prompt", sessionId });
|
|
959
|
+
}
|
|
960
|
+
return;
|
|
961
|
+
}
|
|
962
|
+
default:
|
|
963
|
+
return;
|
|
964
|
+
}
|
|
965
|
+
};
|
|
966
|
+
onIncomingMessage = async (message) => {
|
|
967
|
+
if (this.#disposed) {
|
|
968
|
+
if (isResponseMessage(message) &&
|
|
969
|
+
"result" in message &&
|
|
970
|
+
isRecord(message.result) &&
|
|
971
|
+
readString(message.result, "stopReason") !== "cancelled") {
|
|
972
|
+
return { afterUpdates: [], beforeUpdates: [], rewritePromptCancelled: true };
|
|
973
|
+
}
|
|
974
|
+
return EMPTY_INCOMING_OUTCOME;
|
|
975
|
+
}
|
|
976
|
+
if (isResponseMessage(message)) {
|
|
977
|
+
const key = requestKeyOf(message.id);
|
|
978
|
+
const tracked = key === undefined ? undefined : this.#pendingRequests.get(key);
|
|
979
|
+
if (key !== undefined) {
|
|
980
|
+
this.#pendingRequests.delete(key);
|
|
981
|
+
}
|
|
982
|
+
if (tracked === undefined) {
|
|
983
|
+
return EMPTY_INCOMING_OUTCOME;
|
|
984
|
+
}
|
|
985
|
+
const session = this.#ensureSession(tracked.sessionId);
|
|
986
|
+
if (tracked.kind === "load" || tracked.kind === "session-open") {
|
|
987
|
+
const tail = await readTranscriptTailState(this.#launchEnvironment, tracked.sessionId, session.baselineStepIndex);
|
|
988
|
+
if (tail !== null) {
|
|
989
|
+
session.baselineStepIndex = Math.max(session.baselineStepIndex, tail.maxStepIndex);
|
|
990
|
+
session.sessionOpenStepIndex = Math.max(session.sessionOpenStepIndex, tail.maxStepIndex);
|
|
991
|
+
}
|
|
992
|
+
if (tracked.kind === "load") {
|
|
993
|
+
session.phase = { cancelRequested: false, kind: "idle" };
|
|
994
|
+
}
|
|
995
|
+
return EMPTY_INCOMING_OUTCOME;
|
|
996
|
+
}
|
|
997
|
+
const prePromptStepIndex = session.phase.kind === "client-prompt" ? session.phase.prePromptStepIndex : session.baselineStepIndex;
|
|
998
|
+
const responseStopReason = "result" in message && isRecord(message.result) ? readString(message.result, "stopReason") : undefined;
|
|
999
|
+
const tail = await readTranscriptTailState(this.#launchEnvironment, tracked.sessionId, session.baselineStepIndex);
|
|
1000
|
+
const promptCancelled = this.#disposed ||
|
|
1001
|
+
(session.phase.kind === "client-prompt" && session.phase.cancelled) ||
|
|
1002
|
+
(session.phase.kind === "idle" && session.phase.cancelRequested) ||
|
|
1003
|
+
responseStopReason === "cancelled";
|
|
1004
|
+
if (tail !== null) {
|
|
1005
|
+
session.baselineStepIndex = Math.max(session.baselineStepIndex, tail.maxStepIndex);
|
|
1006
|
+
this.#syncSubagentCreations(session, tail.steps);
|
|
1007
|
+
if (!promptCancelled) {
|
|
1008
|
+
let lastPlannerStepIndex;
|
|
1009
|
+
for (let index = tail.steps.length - 1; index >= 0; index -= 1) {
|
|
1010
|
+
const step = tail.steps[index];
|
|
1011
|
+
if (step !== undefined &&
|
|
1012
|
+
step.step_index > prePromptStepIndex &&
|
|
1013
|
+
step.step_index <= session.baselineStepIndex &&
|
|
1014
|
+
step.type === "PLANNER_RESPONSE") {
|
|
1015
|
+
lastPlannerStepIndex = step.step_index;
|
|
1016
|
+
break;
|
|
1017
|
+
}
|
|
1018
|
+
}
|
|
1019
|
+
if (lastPlannerStepIndex !== undefined) {
|
|
1020
|
+
this.#consumeSubagentIdlesThrough(session, tail.steps, lastPlannerStepIndex);
|
|
1021
|
+
}
|
|
1022
|
+
}
|
|
1023
|
+
}
|
|
1024
|
+
if (promptCancelled) {
|
|
1025
|
+
this.#clearAllBackgroundTasks(session);
|
|
1026
|
+
}
|
|
1027
|
+
session.phase = { cancelRequested: promptCancelled, kind: "idle" };
|
|
1028
|
+
const bg = this.#takeBackgroundTasksUpdate(tracked.sessionId, session);
|
|
1029
|
+
const rewritePromptCancelled = promptCancelled &&
|
|
1030
|
+
"result" in message &&
|
|
1031
|
+
isRecord(message.result) &&
|
|
1032
|
+
responseStopReason !== "cancelled";
|
|
1033
|
+
const beforeUpdates = bg === null ? [] : [bg];
|
|
1034
|
+
if (beforeUpdates.length === 0 && !rewritePromptCancelled) {
|
|
1035
|
+
return EMPTY_INCOMING_OUTCOME;
|
|
1036
|
+
}
|
|
1037
|
+
return {
|
|
1038
|
+
afterUpdates: [],
|
|
1039
|
+
beforeUpdates,
|
|
1040
|
+
...(rewritePromptCancelled ? { rewritePromptCancelled: true } : {}),
|
|
1041
|
+
};
|
|
1042
|
+
}
|
|
1043
|
+
if (!isNotificationMessage(message) || message.method !== methods.client.session.update) {
|
|
1044
|
+
return EMPTY_INCOMING_OUTCOME;
|
|
1045
|
+
}
|
|
1046
|
+
const params = isRecord(message.params) ? message.params : undefined;
|
|
1047
|
+
const sessionId = params === undefined ? undefined : readString(params, "sessionId");
|
|
1048
|
+
const update = params === undefined ? undefined : params["update"];
|
|
1049
|
+
if (sessionId === undefined || !isRecord(update)) {
|
|
1050
|
+
return EMPTY_INCOMING_OUTCOME;
|
|
1051
|
+
}
|
|
1052
|
+
const updateKind = readString(update, "sessionUpdate");
|
|
1053
|
+
if (updateKind === undefined) {
|
|
1054
|
+
return EMPTY_INCOMING_OUTCOME;
|
|
1055
|
+
}
|
|
1056
|
+
const session = this.#ensureSession(sessionId);
|
|
1057
|
+
switch (session.phase.kind) {
|
|
1058
|
+
case "loading":
|
|
1059
|
+
return EMPTY_INCOMING_OUTCOME;
|
|
1060
|
+
case "client-prompt": {
|
|
1061
|
+
if (updateKind === "tool_call" || updateKind === "tool_call_update") {
|
|
1062
|
+
await this.#applyClientPromptToolUpdate(sessionId, session, update, session.phase.cancelled);
|
|
1063
|
+
}
|
|
1064
|
+
return EMPTY_INCOMING_OUTCOME;
|
|
1065
|
+
}
|
|
1066
|
+
case "idle": {
|
|
1067
|
+
if (updateKind === "tool_call_update") {
|
|
1068
|
+
const toolCallId = readString(update, "toolCallId");
|
|
1069
|
+
const status = readString(update, "status");
|
|
1070
|
+
if (toolCallId !== undefined && status !== undefined) {
|
|
1071
|
+
const existing = session.backgroundTasks.get(toolCallId);
|
|
1072
|
+
if (existing !== undefined) {
|
|
1073
|
+
if (status === "failed" || status === "cancelled") {
|
|
1074
|
+
if (existing.kind === "subagent") {
|
|
1075
|
+
this.#deleteSubagentTask(session, toolCallId);
|
|
1076
|
+
}
|
|
1077
|
+
else {
|
|
1078
|
+
session.backgroundTasks.delete(toolCallId);
|
|
1079
|
+
}
|
|
1080
|
+
const bg = this.#takeBackgroundTasksUpdate(sessionId, session);
|
|
1081
|
+
return bg === null ? EMPTY_INCOMING_OUTCOME : { afterUpdates: [bg], beforeUpdates: [] };
|
|
1082
|
+
}
|
|
1083
|
+
if (status === "completed") {
|
|
1084
|
+
if (session.phase.cancelRequested) {
|
|
1085
|
+
if (existing.kind === "subagent") {
|
|
1086
|
+
this.#deleteSubagentTask(session, toolCallId);
|
|
1087
|
+
}
|
|
1088
|
+
else {
|
|
1089
|
+
session.backgroundTasks.delete(toolCallId);
|
|
1090
|
+
}
|
|
1091
|
+
const bg = this.#takeBackgroundTasksUpdate(sessionId, session);
|
|
1092
|
+
return bg === null
|
|
1093
|
+
? EMPTY_INCOMING_OUTCOME
|
|
1094
|
+
: { afterUpdates: [bg], beforeUpdates: [] };
|
|
1095
|
+
}
|
|
1096
|
+
session.backgroundTasks.set(toolCallId, {
|
|
1097
|
+
...existing,
|
|
1098
|
+
state: "awaiting-results",
|
|
1099
|
+
});
|
|
1100
|
+
return {
|
|
1101
|
+
afterUpdates: [],
|
|
1102
|
+
beforeUpdates: this.#enterWakeup(sessionId, session, [toolCallId]),
|
|
1103
|
+
};
|
|
1104
|
+
}
|
|
1105
|
+
}
|
|
1106
|
+
}
|
|
1107
|
+
return EMPTY_INCOMING_OUTCOME;
|
|
1108
|
+
}
|
|
1109
|
+
if (session.phase.cancelRequested || !isWakeupStartUpdateKind(updateKind)) {
|
|
1110
|
+
return EMPTY_INCOMING_OUTCOME;
|
|
1111
|
+
}
|
|
1112
|
+
if (updateKind === "tool_call") {
|
|
1113
|
+
const meta = inspectToolMetadata(update, undefined);
|
|
1114
|
+
if (meta.isDaemon) {
|
|
1115
|
+
return EMPTY_INCOMING_OUTCOME;
|
|
1116
|
+
}
|
|
1117
|
+
}
|
|
1118
|
+
let matchedTaskIds = [];
|
|
1119
|
+
if (this.#hasSubagentTasks(session)) {
|
|
1120
|
+
const tail = await readTranscriptTailState(this.#launchEnvironment, sessionId, session.baselineStepIndex);
|
|
1121
|
+
if (this.#disposed || this.#sessions.get(sessionId) !== session) {
|
|
1122
|
+
return EMPTY_INCOMING_OUTCOME;
|
|
1123
|
+
}
|
|
1124
|
+
if (session.phase.kind !== "idle" || session.phase.cancelRequested) {
|
|
1125
|
+
return EMPTY_INCOMING_OUTCOME;
|
|
1126
|
+
}
|
|
1127
|
+
if (tail !== null) {
|
|
1128
|
+
this.#syncSubagentCreations(session, tail.steps);
|
|
1129
|
+
matchedTaskIds = this.#markTriggeredSubagentIdles(session, tail.steps);
|
|
1130
|
+
}
|
|
1131
|
+
}
|
|
1132
|
+
const startedNotifications = this.#enterWakeup(sessionId, session, matchedTaskIds);
|
|
1133
|
+
const extraWakeup = [];
|
|
1134
|
+
await this.#handleWakeupUpdate(sessionId, session, updateKind, update, extraWakeup);
|
|
1135
|
+
return {
|
|
1136
|
+
afterUpdates: [],
|
|
1137
|
+
beforeUpdates: [...startedNotifications, ...extraWakeup],
|
|
1138
|
+
};
|
|
1139
|
+
}
|
|
1140
|
+
case "wakeup": {
|
|
1141
|
+
const beforeUpdates = [];
|
|
1142
|
+
await this.#handleWakeupUpdate(sessionId, session, updateKind, update, beforeUpdates);
|
|
1143
|
+
return beforeUpdates.length === 0 ? EMPTY_INCOMING_OUTCOME : { afterUpdates: [], beforeUpdates };
|
|
1144
|
+
}
|
|
1145
|
+
default: {
|
|
1146
|
+
const unreachable = session.phase;
|
|
1147
|
+
throw new Error(`Unexpected Antigravity session phase: ${String(unreachable)}`);
|
|
1148
|
+
}
|
|
1149
|
+
}
|
|
1150
|
+
};
|
|
1151
|
+
#handleWakeupUpdate = async (sessionId, session, updateKind, update, beforeUpdates) => {
|
|
1152
|
+
if (session.phase.kind !== "wakeup") {
|
|
1153
|
+
return;
|
|
1154
|
+
}
|
|
1155
|
+
if (updateKind === "tool_call" || updateKind === "tool_call_update") {
|
|
1156
|
+
await this.#applyWakeupToolUpdate(sessionId, session, update, beforeUpdates);
|
|
1157
|
+
return;
|
|
1158
|
+
}
|
|
1159
|
+
if (isWakeupChunkUpdateKind(updateKind)) {
|
|
1160
|
+
const executorError = extractExecutorError(updateKind, update);
|
|
1161
|
+
this.#promoteOpenWakeupToolsToBackground(session);
|
|
1162
|
+
const bg = this.#takeBackgroundTasksUpdate(sessionId, session);
|
|
1163
|
+
if (bg !== null) {
|
|
1164
|
+
beforeUpdates.push(bg);
|
|
1165
|
+
}
|
|
1166
|
+
if (session.phase.openToolIds.size === 0 && session.activeInteractions === 0) {
|
|
1167
|
+
this.#setWakeupStreaming(sessionId, session, session.phase.generation, session.phase.openToolIds, executorError);
|
|
1168
|
+
}
|
|
1169
|
+
}
|
|
1170
|
+
};
|
|
1171
|
+
beginInteraction = (sessionId) => {
|
|
1172
|
+
if (this.#disposed) {
|
|
1173
|
+
return;
|
|
1174
|
+
}
|
|
1175
|
+
const session = this.#ensureSession(sessionId);
|
|
1176
|
+
session.activeInteractions += 1;
|
|
1177
|
+
this.#pauseOrResumeWakeupTimers(sessionId, session);
|
|
1178
|
+
};
|
|
1179
|
+
endInteraction = (sessionId) => {
|
|
1180
|
+
if (this.#disposed) {
|
|
1181
|
+
return;
|
|
1182
|
+
}
|
|
1183
|
+
const session = this.#sessions.get(sessionId);
|
|
1184
|
+
if (session === undefined) {
|
|
1185
|
+
return;
|
|
1186
|
+
}
|
|
1187
|
+
session.activeInteractions = Math.max(0, session.activeInteractions - 1);
|
|
1188
|
+
this.#pauseOrResumeWakeupTimers(sessionId, session);
|
|
1189
|
+
};
|
|
1190
|
+
cancelSession = (sessionId) => {
|
|
1191
|
+
if (this.#disposed) {
|
|
1192
|
+
return "retain";
|
|
1193
|
+
}
|
|
1194
|
+
const session = this.#ensureSession(sessionId);
|
|
1195
|
+
const mustClose = session.phase.kind === "wakeup" || this.#hasSubagentTasks(session);
|
|
1196
|
+
if (mustClose) {
|
|
1197
|
+
// Process teardown in AgentSession.#endAgentWork owns emitting terminal
|
|
1198
|
+
// lifecycle events after SessionManager detaches the closing runtime.
|
|
1199
|
+
this.#cancelWakeupTimers(session.phase);
|
|
1200
|
+
this.#clearAllBackgroundTasks(session);
|
|
1201
|
+
session.phase =
|
|
1202
|
+
session.phase.kind === "client-prompt"
|
|
1203
|
+
? { ...session.phase, cancelled: true }
|
|
1204
|
+
: { cancelRequested: true, kind: "idle" };
|
|
1205
|
+
return "close";
|
|
1206
|
+
}
|
|
1207
|
+
if (session.phase.kind === "client-prompt") {
|
|
1208
|
+
session.phase = { ...session.phase, cancelled: true };
|
|
1209
|
+
}
|
|
1210
|
+
else if (session.phase.kind === "idle") {
|
|
1211
|
+
session.phase = { cancelRequested: true, kind: "idle" };
|
|
1212
|
+
}
|
|
1213
|
+
return "retain";
|
|
1214
|
+
};
|
|
1215
|
+
onUnexpectedProcessExit = (message) => {
|
|
1216
|
+
if (this.#disposed) {
|
|
1217
|
+
return;
|
|
1218
|
+
}
|
|
1219
|
+
for (const [sessionId, session] of this.#sessions) {
|
|
1220
|
+
const wasWakeup = session.phase.kind === "wakeup";
|
|
1221
|
+
this.#cancelWakeupTimers(session.phase);
|
|
1222
|
+
this.#clearAllBackgroundTasks(session);
|
|
1223
|
+
if (wasWakeup) {
|
|
1224
|
+
session.phase = { cancelRequested: false, kind: "idle" };
|
|
1225
|
+
this.#emitMetaUpdate(makeMetaNotification(sessionId, AGENT_TURN_META, {
|
|
1226
|
+
message,
|
|
1227
|
+
stage: "failed",
|
|
1228
|
+
}));
|
|
1229
|
+
}
|
|
1230
|
+
else if (session.phase.kind === "idle") {
|
|
1231
|
+
session.phase = { cancelRequested: false, kind: "idle" };
|
|
1232
|
+
}
|
|
1233
|
+
const bg = this.#takeBackgroundTasksUpdate(sessionId, session);
|
|
1234
|
+
if (bg !== null) {
|
|
1235
|
+
this.#emitMetaUpdate(bg);
|
|
1236
|
+
}
|
|
1237
|
+
}
|
|
1238
|
+
};
|
|
1239
|
+
dispose = () => {
|
|
1240
|
+
if (this.#disposed) {
|
|
1241
|
+
return;
|
|
1242
|
+
}
|
|
1243
|
+
this.#disposed = true;
|
|
1244
|
+
for (const session of this.#sessions.values()) {
|
|
1245
|
+
this.#cancelWakeupTimers(session.phase);
|
|
1246
|
+
}
|
|
1247
|
+
this.#sessions.clear();
|
|
1248
|
+
this.#pendingRequests.clear();
|
|
1249
|
+
};
|
|
1250
|
+
}
|
|
1251
|
+
//# sourceMappingURL=background-wakeup.js.map
|