@moxt-ai/mobius 0.0.451 → 0.0.453
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-XQBTUYWO.js → chunk-2Q5CQHJI.js} +3 -2
- package/dist/{chunk-XQBTUYWO.js.map → chunk-2Q5CQHJI.js.map} +2 -2
- package/dist/{chunk-5QMXJEVI.js → chunk-PVKEDOVN.js} +3666 -1414
- package/dist/{chunk-5QMXJEVI.js.map → chunk-PVKEDOVN.js.map} +4 -4
- package/dist/{cli-EWBWXBGR.js → cli-TZ354PVI.js} +3 -3
- package/dist/cli.js +2 -2
- package/dist/engine/acp/adapter/antigravity/adapter.d.ts +41 -0
- package/dist/engine/acp/adapter/antigravity/adapter.d.ts.map +1 -0
- package/dist/engine/acp/adapter/antigravity/adapter.js +73 -0
- package/dist/engine/acp/adapter/antigravity/adapter.js.map +1 -0
- package/dist/engine/acp/adapter/antigravity/agent-connection.d.ts +21 -0
- package/dist/engine/acp/adapter/antigravity/agent-connection.d.ts.map +1 -0
- package/dist/engine/acp/adapter/antigravity/agent-connection.js +662 -0
- package/dist/engine/acp/adapter/antigravity/agent-connection.js.map +1 -0
- package/dist/engine/acp/adapter/antigravity/background-wakeup.d.ts +35 -0
- package/dist/engine/acp/adapter/antigravity/background-wakeup.d.ts.map +1 -0
- package/dist/engine/acp/adapter/antigravity/background-wakeup.js +959 -0
- package/dist/engine/acp/adapter/antigravity/background-wakeup.js.map +1 -0
- package/dist/engine/acp/adapter/claude/adapter.d.ts.map +1 -1
- package/dist/engine/acp/adapter/claude/adapter.js +6 -3
- package/dist/engine/acp/adapter/claude/adapter.js.map +1 -1
- 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/acp/adapter/registry.d.ts.map +1 -1
- package/dist/engine/acp/adapter/registry.js +3 -0
- package/dist/engine/acp/adapter/registry.js.map +1 -1
- package/dist/engine/acp/connection/stdio-acp-stream.d.ts +16 -0
- package/dist/engine/acp/connection/stdio-acp-stream.d.ts.map +1 -0
- package/dist/engine/acp/connection/stdio-acp-stream.js +179 -0
- package/dist/engine/acp/connection/stdio-acp-stream.js.map +1 -0
- package/dist/engine/acp/native-history/preview-contract.d.ts +1 -0
- package/dist/engine/acp/native-history/preview-contract.d.ts.map +1 -1
- package/dist/engine/agent/specification/vendors/antigravity-agent.d.ts +20 -0
- package/dist/engine/agent/specification/vendors/antigravity-agent.d.ts.map +1 -0
- package/dist/engine/agent/specification/vendors/antigravity-agent.js +67 -0
- package/dist/engine/agent/specification/vendors/antigravity-agent.js.map +1 -0
- package/dist/engine/agent/specification/vendors/supported-agent-specifications.d.ts.map +1 -1
- package/dist/engine/agent/specification/vendors/supported-agent-specifications.js +2 -0
- package/dist/engine/agent/specification/vendors/supported-agent-specifications.js.map +1 -1
- package/dist/engine/generated/session-title-preferences-migrations.d.ts +5 -0
- package/dist/engine/generated/session-title-preferences-migrations.d.ts.map +1 -0
- package/dist/engine/generated/session-title-preferences-migrations.js +9 -0
- package/dist/engine/generated/session-title-preferences-migrations.js.map +1 -0
- package/dist/engine/runtime/command/registry/runtime.d.ts.map +1 -1
- package/dist/engine/runtime/command/registry/runtime.js +1 -0
- package/dist/engine/runtime/command/registry/runtime.js.map +1 -1
- package/dist/engine/runtime/command/registry/session.d.ts.map +1 -1
- package/dist/engine/runtime/command/registry/session.js +1 -0
- package/dist/engine/runtime/command/registry/session.js.map +1 -1
- package/dist/engine/runtime/observability/environment-diagnostics.d.ts.map +1 -1
- package/dist/engine/runtime/observability/environment-diagnostics.js +2 -0
- package/dist/engine/runtime/observability/environment-diagnostics.js.map +1 -1
- package/dist/engine/schema/session-title-preferences/index.d.ts +2 -0
- package/dist/engine/schema/session-title-preferences/index.d.ts.map +1 -0
- package/dist/engine/schema/session-title-preferences/index.js +2 -0
- package/dist/engine/schema/session-title-preferences/index.js.map +1 -0
- package/dist/engine/schema/session-title-preferences/session-title-preference.d.ts +40 -0
- package/dist/engine/schema/session-title-preferences/session-title-preference.d.ts.map +1 -0
- package/dist/engine/schema/session-title-preferences/session-title-preference.js +6 -0
- package/dist/engine/schema/session-title-preferences/session-title-preference.js.map +1 -0
- package/dist/engine/session/api/session-api.d.ts +2 -0
- package/dist/engine/session/api/session-api.d.ts.map +1 -1
- package/dist/engine/session/api/session-api.js.map +1 -1
- package/dist/engine/session/api/session-layer.d.ts.map +1 -1
- package/dist/engine/session/api/session-layer.js +8 -1
- package/dist/engine/session/api/session-layer.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 +43 -12
- 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/session/record/session-store.d.ts +5 -1
- package/dist/engine/session/record/session-store.d.ts.map +1 -1
- package/dist/engine/session/record/session-store.js +33 -7
- package/dist/engine/session/record/session-store.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/storage/session/acp-session-table.d.ts +1 -0
- package/dist/engine/storage/session/acp-session-table.d.ts.map +1 -1
- package/dist/engine/storage/session/acp-session-table.js +2 -0
- package/dist/engine/storage/session/acp-session-table.js.map +1 -1
- package/dist/engine/storage/session/session-title-preference-table.d.ts +11 -0
- package/dist/engine/storage/session/session-title-preference-table.d.ts.map +1 -0
- package/dist/engine/storage/session/session-title-preference-table.js +30 -0
- package/dist/engine/storage/session/session-title-preference-table.js.map +1 -0
- package/dist/engine/storage/sqlite/local-databases.d.ts +1 -0
- package/dist/engine/storage/sqlite/local-databases.d.ts.map +1 -1
- package/dist/engine/storage/sqlite/local-databases.js +2 -0
- package/dist/engine/storage/sqlite/local-databases.js.map +1 -1
- package/dist/engine/storage/sqlite/local-sqlite-database.d.ts +1 -0
- package/dist/engine/storage/sqlite/local-sqlite-database.d.ts.map +1 -1
- package/dist/engine/storage/sqlite/local-sqlite-database.js +2 -0
- package/dist/engine/storage/sqlite/local-sqlite-database.js.map +1 -1
- package/dist/engine/util/platform/contracts.d.ts +1 -0
- package/dist/engine/util/platform/contracts.d.ts.map +1 -1
- package/dist/engine/util/platform/index.d.ts.map +1 -1
- package/dist/engine/util/platform/index.js +1 -0
- package/dist/engine/util/platform/index.js.map +1 -1
- package/dist/engine/util/platform/linux.d.ts.map +1 -1
- package/dist/engine/util/platform/linux.js +8 -0
- package/dist/engine/util/platform/linux.js.map +1 -1
- package/dist/engine/util/platform/macos.d.ts.map +1 -1
- package/dist/engine/util/platform/macos.js +8 -0
- package/dist/engine/util/platform/macos.js.map +1 -1
- package/dist/engine/util/platform/windows.d.ts.map +1 -1
- package/dist/engine/util/platform/windows.js +12 -0
- package/dist/engine/util/platform/windows.js.map +1 -1
- package/dist/engine.js +2 -2
- package/package.json +3 -2
- /package/dist/{cli-EWBWXBGR.js.map → cli-TZ354PVI.js.map} +0 -0
|
@@ -0,0 +1,959 @@
|
|
|
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_RETRY_DELAY_MS = 150;
|
|
12
|
+
const WAKEUP_STREAM_FALLBACK_MS = 1_500;
|
|
13
|
+
const WAKEUP_AWAITING_OUTPUT_MS = 60_000;
|
|
14
|
+
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;
|
|
15
|
+
const SUBAGENT_IDLE_PATTERN = /\bSubagent [0-9a-f-]+ has gone idle\b/u;
|
|
16
|
+
const MAXIMUM_TOOL_DESCRIPTION_CHARS = 500;
|
|
17
|
+
const transcriptStepSchema = z.object({
|
|
18
|
+
content: z.string().optional(),
|
|
19
|
+
status: z.string().optional(),
|
|
20
|
+
step_index: z.number().int().nonnegative(),
|
|
21
|
+
tool_calls: z.array(z.unknown()).optional(),
|
|
22
|
+
type: z.string(),
|
|
23
|
+
});
|
|
24
|
+
const EMPTY_INCOMING_OUTCOME = {
|
|
25
|
+
afterUpdates: [],
|
|
26
|
+
beforeUpdates: [],
|
|
27
|
+
};
|
|
28
|
+
function readString(record, key) {
|
|
29
|
+
const value = record[key];
|
|
30
|
+
return typeof value === "string" ? value : undefined;
|
|
31
|
+
}
|
|
32
|
+
function requestKeyOf(id) {
|
|
33
|
+
return typeof id === "string" || typeof id === "number" ? id : undefined;
|
|
34
|
+
}
|
|
35
|
+
function isRequestMessage(message) {
|
|
36
|
+
return "method" in message && "id" in message;
|
|
37
|
+
}
|
|
38
|
+
function isNotificationMessage(message) {
|
|
39
|
+
return "method" in message && !("id" in message);
|
|
40
|
+
}
|
|
41
|
+
function isResponseMessage(message) {
|
|
42
|
+
return "id" in message && !("method" in message);
|
|
43
|
+
}
|
|
44
|
+
function resolveGeminiHome(launchEnvironment) {
|
|
45
|
+
const configured = launchEnvironment["GEMINI_HOME"]?.trim();
|
|
46
|
+
if (configured !== undefined && configured.length > 0) {
|
|
47
|
+
return configured;
|
|
48
|
+
}
|
|
49
|
+
const home = launchEnvironment["HOME"]?.trim() || launchEnvironment["USERPROFILE"]?.trim() || homedir();
|
|
50
|
+
return home.length > 0 ? join(home, ".gemini") : undefined;
|
|
51
|
+
}
|
|
52
|
+
async function readTranscriptTailState(launchEnvironment, sessionId, baselineStepIndex) {
|
|
53
|
+
if (!SESSION_UUID_PATTERN.test(sessionId)) {
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
const geminiHome = resolveGeminiHome(launchEnvironment);
|
|
57
|
+
if (geminiHome === undefined) {
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
const tail = await readScopedFileTail(geminiHome, join("antigravity-cli", "brain", sessionId, ".system_generated", "logs", "transcript.jsonl"), TRANSCRIPT_TAIL_BYTES);
|
|
61
|
+
if (tail === null) {
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
if (tail.bytes.byteLength === 0) {
|
|
65
|
+
return { maxStepIndex: -1, settledWithoutToolCalls: false, steps: [] };
|
|
66
|
+
}
|
|
67
|
+
const text = new TextDecoder().decode(tail.bytes);
|
|
68
|
+
const lines = text.split("\n");
|
|
69
|
+
const startIndex = tail.offset > 0 ? 1 : 0;
|
|
70
|
+
const steps = [];
|
|
71
|
+
for (const rawLine of lines.slice(startIndex)) {
|
|
72
|
+
const trimmed = rawLine.trim();
|
|
73
|
+
if (trimmed.length === 0) {
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
let parsedJson;
|
|
77
|
+
try {
|
|
78
|
+
parsedJson = JSON.parse(trimmed);
|
|
79
|
+
}
|
|
80
|
+
catch {
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
const parsed = transcriptStepSchema.safeParse(parsedJson);
|
|
84
|
+
if (parsed.success) {
|
|
85
|
+
steps.push(parsed.data);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
const lastStep = steps.at(-1);
|
|
89
|
+
const maxStepIndex = lastStep?.step_index ?? -1;
|
|
90
|
+
const settledWithoutToolCalls = lastStep !== undefined &&
|
|
91
|
+
lastStep.step_index > baselineStepIndex &&
|
|
92
|
+
lastStep.type === "PLANNER_RESPONSE" &&
|
|
93
|
+
lastStep.status === "DONE" &&
|
|
94
|
+
(lastStep.tool_calls === undefined || lastStep.tool_calls.length === 0);
|
|
95
|
+
return { maxStepIndex, settledWithoutToolCalls, steps };
|
|
96
|
+
}
|
|
97
|
+
function countConsumedSubagentIdles(steps, afterStepIndex, throughStepIndex) {
|
|
98
|
+
const relevant = steps.filter((step) => step.step_index > afterStepIndex && step.step_index <= throughStepIndex);
|
|
99
|
+
let lastPlannerIndex = -1;
|
|
100
|
+
for (let index = relevant.length - 1; index >= 0; index -= 1) {
|
|
101
|
+
const step = relevant[index];
|
|
102
|
+
if (step?.type === "PLANNER_RESPONSE") {
|
|
103
|
+
lastPlannerIndex = index;
|
|
104
|
+
break;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
if (lastPlannerIndex <= 0) {
|
|
108
|
+
return 0;
|
|
109
|
+
}
|
|
110
|
+
let count = 0;
|
|
111
|
+
for (let index = 0; index < lastPlannerIndex; index += 1) {
|
|
112
|
+
const step = relevant[index];
|
|
113
|
+
if (step?.type === "SYSTEM_MESSAGE" && step.content !== undefined && SUBAGENT_IDLE_PATTERN.test(step.content)) {
|
|
114
|
+
count += 1;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return count;
|
|
118
|
+
}
|
|
119
|
+
function extractToolInputRecords(update) {
|
|
120
|
+
const records = [];
|
|
121
|
+
const args = update["args"];
|
|
122
|
+
if (isRecord(args)) {
|
|
123
|
+
records.push(args);
|
|
124
|
+
}
|
|
125
|
+
const rawInput = update["rawInput"];
|
|
126
|
+
if (isRecord(rawInput)) {
|
|
127
|
+
records.push(rawInput);
|
|
128
|
+
}
|
|
129
|
+
return records;
|
|
130
|
+
}
|
|
131
|
+
function readInputString(records, key) {
|
|
132
|
+
for (const record of records) {
|
|
133
|
+
const value = readString(record, key)?.trim();
|
|
134
|
+
if (value !== undefined && value.length > 0) {
|
|
135
|
+
return value;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
return undefined;
|
|
139
|
+
}
|
|
140
|
+
function readToolCategory(update, inputs, previous) {
|
|
141
|
+
const name = readString(update, "name");
|
|
142
|
+
if (name === "run_command") {
|
|
143
|
+
return "command";
|
|
144
|
+
}
|
|
145
|
+
if (name === "schedule") {
|
|
146
|
+
return "schedule";
|
|
147
|
+
}
|
|
148
|
+
if (name === "run_workflow") {
|
|
149
|
+
return "workflow";
|
|
150
|
+
}
|
|
151
|
+
if (name === "invoke_subagent") {
|
|
152
|
+
return "subagent";
|
|
153
|
+
}
|
|
154
|
+
if (name === "manage_subagents") {
|
|
155
|
+
return "manage-subagents";
|
|
156
|
+
}
|
|
157
|
+
const title = readString(update, "title");
|
|
158
|
+
if (title === "Run invoke_subagent" ||
|
|
159
|
+
title === "Running invoke_subagent" ||
|
|
160
|
+
title === "Run invoke_subagent?" ||
|
|
161
|
+
title === "Running start_subagent" ||
|
|
162
|
+
title === "Run start_subagent?") {
|
|
163
|
+
return "subagent";
|
|
164
|
+
}
|
|
165
|
+
if (title === "Run schedule" || title === "Running schedule") {
|
|
166
|
+
return "schedule";
|
|
167
|
+
}
|
|
168
|
+
if (title === "Run manage_subagents") {
|
|
169
|
+
return "manage-subagents";
|
|
170
|
+
}
|
|
171
|
+
if (previous?.category !== undefined && previous.category !== null) {
|
|
172
|
+
return previous.category;
|
|
173
|
+
}
|
|
174
|
+
if (readInputString(inputs, "CommandLine") !== undefined) {
|
|
175
|
+
return "command";
|
|
176
|
+
}
|
|
177
|
+
return null;
|
|
178
|
+
}
|
|
179
|
+
function inspectToolMetadata(update, previous) {
|
|
180
|
+
const inputs = extractToolInputRecords(update);
|
|
181
|
+
const category = readToolCategory(update, inputs, previous);
|
|
182
|
+
let isDaemon = previous?.isDaemon ?? false;
|
|
183
|
+
let subagentKillCount = previous?.subagentKillCount ?? 0;
|
|
184
|
+
let subagentKillAll = previous?.subagentKillAll ?? false;
|
|
185
|
+
for (const record of inputs) {
|
|
186
|
+
if (record["IsDaemon"] === true) {
|
|
187
|
+
isDaemon = true;
|
|
188
|
+
}
|
|
189
|
+
const cron = readString(record, "CronExpression")?.trim();
|
|
190
|
+
if (cron !== undefined && cron.length > 0) {
|
|
191
|
+
isDaemon = true;
|
|
192
|
+
}
|
|
193
|
+
if (category === "manage-subagents") {
|
|
194
|
+
const action = readString(record, "Action");
|
|
195
|
+
if (action === "kill_all") {
|
|
196
|
+
subagentKillAll = true;
|
|
197
|
+
}
|
|
198
|
+
else if (action === "kill" && Array.isArray(record["ConversationIds"])) {
|
|
199
|
+
subagentKillCount = Math.max(subagentKillCount, record["ConversationIds"].length);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
const description = readInputString(inputs, "toolSummary") ??
|
|
204
|
+
readInputString(inputs, "Description") ??
|
|
205
|
+
readInputString(inputs, "Role") ??
|
|
206
|
+
readInputString(inputs, "CommandLine") ??
|
|
207
|
+
readInputString(inputs, "Prompt") ??
|
|
208
|
+
readString(update, "title") ??
|
|
209
|
+
previous?.description;
|
|
210
|
+
return {
|
|
211
|
+
category,
|
|
212
|
+
description: description === undefined ? undefined : truncate(description, MAXIMUM_TOOL_DESCRIPTION_CHARS),
|
|
213
|
+
isDaemon,
|
|
214
|
+
subagentKillAll,
|
|
215
|
+
subagentKillCount,
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
function backgroundKindFor(category) {
|
|
219
|
+
switch (category) {
|
|
220
|
+
case "command":
|
|
221
|
+
return "shell";
|
|
222
|
+
case "subagent":
|
|
223
|
+
return "subagent";
|
|
224
|
+
case "schedule":
|
|
225
|
+
case "workflow":
|
|
226
|
+
return "other";
|
|
227
|
+
case "manage-subagents":
|
|
228
|
+
case null:
|
|
229
|
+
return null;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
function makeMetaNotification(sessionId, metaKey, payload) {
|
|
233
|
+
return {
|
|
234
|
+
sessionId,
|
|
235
|
+
update: {
|
|
236
|
+
_meta: { [metaKey]: payload },
|
|
237
|
+
sessionUpdate: "session_info_update",
|
|
238
|
+
},
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
function isWakeupStartUpdateKind(updateKind) {
|
|
242
|
+
switch (updateKind) {
|
|
243
|
+
case "agent_message_chunk":
|
|
244
|
+
case "agent_thought_chunk":
|
|
245
|
+
case "agent_message":
|
|
246
|
+
case "agent_thought":
|
|
247
|
+
case "tool_call":
|
|
248
|
+
return true;
|
|
249
|
+
default:
|
|
250
|
+
return false;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
function isWakeupChunkUpdateKind(updateKind) {
|
|
254
|
+
return (updateKind === "agent_message_chunk" ||
|
|
255
|
+
updateKind === "agent_thought_chunk" ||
|
|
256
|
+
updateKind === "agent_message" ||
|
|
257
|
+
updateKind === "agent_thought");
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* Bridges `agy acp` background work (`run_command`, one-shot `schedule`, `run_workflow`,
|
|
261
|
+
* and `invoke_subagent`) and autonomous `<SYSTEM_MESSAGE>` continuations onto Mobius's
|
|
262
|
+
* session-level `mobius.backgroundTasks` and `mobius.agentTurn` protocol.
|
|
263
|
+
*/
|
|
264
|
+
export class AntigravityBackgroundWakeup {
|
|
265
|
+
#emitMetaUpdate;
|
|
266
|
+
#launchEnvironment;
|
|
267
|
+
#timer;
|
|
268
|
+
#sessions = new Map();
|
|
269
|
+
#pendingRequests = new Map();
|
|
270
|
+
#disposed = false;
|
|
271
|
+
constructor(options) {
|
|
272
|
+
this.#emitMetaUpdate = options.emitMetaUpdate;
|
|
273
|
+
this.#launchEnvironment = options.launchEnvironment;
|
|
274
|
+
this.#timer = options.timer ?? systemTimer;
|
|
275
|
+
}
|
|
276
|
+
#ensureSession = (sessionId) => {
|
|
277
|
+
const existing = this.#sessions.get(sessionId);
|
|
278
|
+
if (existing !== undefined) {
|
|
279
|
+
return existing;
|
|
280
|
+
}
|
|
281
|
+
const created = {
|
|
282
|
+
activeInteractions: 0,
|
|
283
|
+
backgroundTasks: new Map(),
|
|
284
|
+
baselineStepIndex: -1,
|
|
285
|
+
lastEmittedBackgroundKey: "[]",
|
|
286
|
+
phase: { cancelRequested: false, kind: "idle" },
|
|
287
|
+
toolMetadata: new Map(),
|
|
288
|
+
wakeupGeneration: 0,
|
|
289
|
+
};
|
|
290
|
+
this.#sessions.set(sessionId, created);
|
|
291
|
+
return created;
|
|
292
|
+
};
|
|
293
|
+
#cancelWakeupTimers = (phase) => {
|
|
294
|
+
if (phase.kind !== "wakeup") {
|
|
295
|
+
return;
|
|
296
|
+
}
|
|
297
|
+
switch (phase.stage.kind) {
|
|
298
|
+
case "awaiting-output":
|
|
299
|
+
phase.stage.timeout.cancel();
|
|
300
|
+
return;
|
|
301
|
+
case "blocked-on-work":
|
|
302
|
+
return;
|
|
303
|
+
case "streaming":
|
|
304
|
+
phase.stage.transcriptCheck?.cancel();
|
|
305
|
+
phase.stage.fallbackTimeout.cancel();
|
|
306
|
+
return;
|
|
307
|
+
default: {
|
|
308
|
+
const unreachable = phase.stage;
|
|
309
|
+
throw new Error(`Unexpected wakeup stage: ${String(unreachable)}`);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
};
|
|
313
|
+
#takeBackgroundTasksUpdate = (sessionId, session) => {
|
|
314
|
+
const tasks = [...session.backgroundTasks.values()];
|
|
315
|
+
const serialized = JSON.stringify(tasks);
|
|
316
|
+
if (serialized === session.lastEmittedBackgroundKey) {
|
|
317
|
+
return null;
|
|
318
|
+
}
|
|
319
|
+
session.lastEmittedBackgroundKey = serialized;
|
|
320
|
+
return makeMetaNotification(sessionId, BACKGROUND_TASKS_META, tasks);
|
|
321
|
+
};
|
|
322
|
+
#removeSubagentTasks = (session, count) => {
|
|
323
|
+
if (count <= 0) {
|
|
324
|
+
return;
|
|
325
|
+
}
|
|
326
|
+
let remaining = count;
|
|
327
|
+
for (const [taskId, task] of session.backgroundTasks) {
|
|
328
|
+
if (task.kind !== "subagent") {
|
|
329
|
+
continue;
|
|
330
|
+
}
|
|
331
|
+
session.backgroundTasks.delete(taskId);
|
|
332
|
+
remaining -= 1;
|
|
333
|
+
if (remaining === 0) {
|
|
334
|
+
break;
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
};
|
|
338
|
+
#dropAwaitingResultTasks = (session) => {
|
|
339
|
+
for (const [taskId, task] of session.backgroundTasks) {
|
|
340
|
+
if (task.state === "awaiting-results") {
|
|
341
|
+
session.backgroundTasks.delete(taskId);
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
};
|
|
345
|
+
#hasSubagentTasks = (session) => {
|
|
346
|
+
for (const task of session.backgroundTasks.values()) {
|
|
347
|
+
if (task.kind === "subagent") {
|
|
348
|
+
return true;
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
return false;
|
|
352
|
+
};
|
|
353
|
+
#oldestRunningSubagentId = (session) => {
|
|
354
|
+
for (const task of session.backgroundTasks.values()) {
|
|
355
|
+
if (task.kind === "subagent" && task.state === "running") {
|
|
356
|
+
return task.taskId;
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
return undefined;
|
|
360
|
+
};
|
|
361
|
+
#setWakeupAwaitingOutput = (sessionId, session, generation, openToolIds) => {
|
|
362
|
+
this.#cancelWakeupTimers(session.phase);
|
|
363
|
+
const timeout = this.#timer.schedule(() => {
|
|
364
|
+
this.#finishWakeupFromTimer(sessionId, generation, "end_turn");
|
|
365
|
+
}, WAKEUP_AWAITING_OUTPUT_MS);
|
|
366
|
+
session.phase = {
|
|
367
|
+
generation,
|
|
368
|
+
kind: "wakeup",
|
|
369
|
+
openToolIds,
|
|
370
|
+
stage: { kind: "awaiting-output", timeout },
|
|
371
|
+
};
|
|
372
|
+
};
|
|
373
|
+
#setWakeupBlockedOnWork = (session, generation, openToolIds) => {
|
|
374
|
+
this.#cancelWakeupTimers(session.phase);
|
|
375
|
+
session.phase = {
|
|
376
|
+
generation,
|
|
377
|
+
kind: "wakeup",
|
|
378
|
+
openToolIds,
|
|
379
|
+
stage: { kind: "blocked-on-work" },
|
|
380
|
+
};
|
|
381
|
+
};
|
|
382
|
+
#setWakeupStreaming = (sessionId, session, generation, openToolIds) => {
|
|
383
|
+
this.#cancelWakeupTimers(session.phase);
|
|
384
|
+
const scheduleTranscriptCheck = (delayMs, allowRetry) => this.#timer.schedule(() => {
|
|
385
|
+
void this.#checkWakeupTranscript(sessionId, generation, allowRetry);
|
|
386
|
+
}, delayMs);
|
|
387
|
+
const fallbackTimeout = this.#timer.schedule(() => {
|
|
388
|
+
this.#finishWakeupFromTimer(sessionId, generation, "end_turn");
|
|
389
|
+
}, WAKEUP_STREAM_FALLBACK_MS);
|
|
390
|
+
const transcriptCheck = scheduleTranscriptCheck(TRANSCRIPT_INITIAL_DELAY_MS, true);
|
|
391
|
+
session.phase = {
|
|
392
|
+
generation,
|
|
393
|
+
kind: "wakeup",
|
|
394
|
+
openToolIds,
|
|
395
|
+
stage: { fallbackTimeout, kind: "streaming", transcriptCheck },
|
|
396
|
+
};
|
|
397
|
+
};
|
|
398
|
+
#enterWakeup = (sessionId, session, taskIds) => {
|
|
399
|
+
const notifications = [];
|
|
400
|
+
const bgNotification = this.#takeBackgroundTasksUpdate(sessionId, session);
|
|
401
|
+
if (bgNotification !== null) {
|
|
402
|
+
notifications.push(bgNotification);
|
|
403
|
+
}
|
|
404
|
+
session.wakeupGeneration += 1;
|
|
405
|
+
const generation = session.wakeupGeneration;
|
|
406
|
+
this.#setWakeupAwaitingOutput(sessionId, session, generation, new Set());
|
|
407
|
+
notifications.push(makeMetaNotification(sessionId, AGENT_TURN_META, {
|
|
408
|
+
reason: "background-task",
|
|
409
|
+
stage: "started",
|
|
410
|
+
taskIds: [...taskIds],
|
|
411
|
+
}));
|
|
412
|
+
return notifications;
|
|
413
|
+
};
|
|
414
|
+
#finishWakeup = (sessionId, session, stopReason, cancelRequested) => {
|
|
415
|
+
if (session.phase.kind !== "wakeup") {
|
|
416
|
+
return [];
|
|
417
|
+
}
|
|
418
|
+
this.#cancelWakeupTimers(session.phase);
|
|
419
|
+
session.phase = { cancelRequested, kind: "idle" };
|
|
420
|
+
this.#dropAwaitingResultTasks(session);
|
|
421
|
+
const notifications = [];
|
|
422
|
+
const bgNotification = this.#takeBackgroundTasksUpdate(sessionId, session);
|
|
423
|
+
if (bgNotification !== null) {
|
|
424
|
+
notifications.push(bgNotification);
|
|
425
|
+
}
|
|
426
|
+
notifications.push(makeMetaNotification(sessionId, AGENT_TURN_META, {
|
|
427
|
+
stage: "ended",
|
|
428
|
+
stopReason,
|
|
429
|
+
}));
|
|
430
|
+
return notifications;
|
|
431
|
+
};
|
|
432
|
+
#checkWakeupTranscript = async (sessionId, generation, allowRetry) => {
|
|
433
|
+
if (this.#disposed) {
|
|
434
|
+
return;
|
|
435
|
+
}
|
|
436
|
+
const session = this.#sessions.get(sessionId);
|
|
437
|
+
if (session === undefined || session.phase.kind !== "wakeup" || session.phase.generation !== generation) {
|
|
438
|
+
return;
|
|
439
|
+
}
|
|
440
|
+
if (session.phase.stage.kind !== "streaming") {
|
|
441
|
+
return;
|
|
442
|
+
}
|
|
443
|
+
const tail = await readTranscriptTailState(this.#launchEnvironment, sessionId, session.baselineStepIndex);
|
|
444
|
+
if (this.#disposed) {
|
|
445
|
+
return;
|
|
446
|
+
}
|
|
447
|
+
const current = this.#sessions.get(sessionId);
|
|
448
|
+
if (current === undefined || current.phase.kind !== "wakeup" || current.phase.generation !== generation) {
|
|
449
|
+
return;
|
|
450
|
+
}
|
|
451
|
+
if (current.phase.stage.kind !== "streaming") {
|
|
452
|
+
return;
|
|
453
|
+
}
|
|
454
|
+
if (tail?.settledWithoutToolCalls) {
|
|
455
|
+
current.baselineStepIndex = Math.max(current.baselineStepIndex, tail.maxStepIndex);
|
|
456
|
+
for (const notification of this.#finishWakeup(sessionId, current, "end_turn", false)) {
|
|
457
|
+
this.#emitMetaUpdate(notification);
|
|
458
|
+
}
|
|
459
|
+
return;
|
|
460
|
+
}
|
|
461
|
+
if (allowRetry) {
|
|
462
|
+
const retryTask = this.#timer.schedule(() => {
|
|
463
|
+
void this.#checkWakeupTranscript(sessionId, generation, false);
|
|
464
|
+
}, TRANSCRIPT_RETRY_DELAY_MS);
|
|
465
|
+
current.phase = {
|
|
466
|
+
...current.phase,
|
|
467
|
+
stage: {
|
|
468
|
+
fallbackTimeout: current.phase.stage.fallbackTimeout,
|
|
469
|
+
kind: "streaming",
|
|
470
|
+
transcriptCheck: retryTask,
|
|
471
|
+
},
|
|
472
|
+
};
|
|
473
|
+
}
|
|
474
|
+
};
|
|
475
|
+
#finishWakeupFromTimer = (sessionId, generation, stopReason) => {
|
|
476
|
+
if (this.#disposed) {
|
|
477
|
+
return;
|
|
478
|
+
}
|
|
479
|
+
const session = this.#sessions.get(sessionId);
|
|
480
|
+
if (session === undefined || session.phase.kind !== "wakeup" || session.phase.generation !== generation) {
|
|
481
|
+
return;
|
|
482
|
+
}
|
|
483
|
+
void readTranscriptTailState(this.#launchEnvironment, sessionId, session.baselineStepIndex).then((tail) => {
|
|
484
|
+
if (this.#disposed) {
|
|
485
|
+
return;
|
|
486
|
+
}
|
|
487
|
+
const current = this.#sessions.get(sessionId);
|
|
488
|
+
if (current === undefined || current.phase.kind !== "wakeup" || current.phase.generation !== generation) {
|
|
489
|
+
return;
|
|
490
|
+
}
|
|
491
|
+
if (tail !== null && tail.maxStepIndex > current.baselineStepIndex) {
|
|
492
|
+
current.baselineStepIndex = tail.maxStepIndex;
|
|
493
|
+
}
|
|
494
|
+
for (const notification of this.#finishWakeup(sessionId, current, stopReason, false)) {
|
|
495
|
+
this.#emitMetaUpdate(notification);
|
|
496
|
+
}
|
|
497
|
+
});
|
|
498
|
+
};
|
|
499
|
+
#promoteOpenWakeupToolsToBackground = (session) => {
|
|
500
|
+
if (session.phase.kind !== "wakeup" || session.phase.openToolIds.size === 0) {
|
|
501
|
+
return;
|
|
502
|
+
}
|
|
503
|
+
const remainingOpenToolIds = new Set(session.phase.openToolIds);
|
|
504
|
+
let changed = false;
|
|
505
|
+
for (const toolCallId of session.phase.openToolIds) {
|
|
506
|
+
const meta = session.toolMetadata.get(toolCallId);
|
|
507
|
+
if (meta === undefined || meta.isDaemon) {
|
|
508
|
+
continue;
|
|
509
|
+
}
|
|
510
|
+
const kind = backgroundKindFor(meta.category);
|
|
511
|
+
if (kind === "shell" || kind === "other") {
|
|
512
|
+
remainingOpenToolIds.delete(toolCallId);
|
|
513
|
+
changed = true;
|
|
514
|
+
session.backgroundTasks.set(toolCallId, {
|
|
515
|
+
...(meta.description === undefined ? {} : { description: meta.description }),
|
|
516
|
+
kind,
|
|
517
|
+
state: "running",
|
|
518
|
+
taskId: toolCallId,
|
|
519
|
+
toolUseId: toolCallId,
|
|
520
|
+
});
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
if (changed) {
|
|
524
|
+
session.phase = { ...session.phase, openToolIds: remainingOpenToolIds };
|
|
525
|
+
}
|
|
526
|
+
};
|
|
527
|
+
#pauseOrResumeWakeupTimers = (sessionId, session) => {
|
|
528
|
+
if (session.phase.kind !== "wakeup") {
|
|
529
|
+
return;
|
|
530
|
+
}
|
|
531
|
+
if (session.phase.openToolIds.size > 0 || session.activeInteractions > 0) {
|
|
532
|
+
this.#setWakeupBlockedOnWork(session, session.phase.generation, session.phase.openToolIds);
|
|
533
|
+
}
|
|
534
|
+
else if (session.phase.stage.kind === "blocked-on-work") {
|
|
535
|
+
this.#setWakeupAwaitingOutput(sessionId, session, session.phase.generation, session.phase.openToolIds);
|
|
536
|
+
}
|
|
537
|
+
};
|
|
538
|
+
#applyClientPromptToolUpdate = (session, update, cancelled) => {
|
|
539
|
+
const toolCallId = readString(update, "toolCallId");
|
|
540
|
+
const status = readString(update, "status");
|
|
541
|
+
if (toolCallId === undefined || status === undefined) {
|
|
542
|
+
return;
|
|
543
|
+
}
|
|
544
|
+
const meta = inspectToolMetadata(update, session.toolMetadata.get(toolCallId));
|
|
545
|
+
session.toolMetadata.set(toolCallId, meta);
|
|
546
|
+
if (meta.isDaemon) {
|
|
547
|
+
session.backgroundTasks.delete(toolCallId);
|
|
548
|
+
return;
|
|
549
|
+
}
|
|
550
|
+
if (meta.category === "manage-subagents" && status === "completed") {
|
|
551
|
+
if (meta.subagentKillAll) {
|
|
552
|
+
this.#removeSubagentTasks(session, session.backgroundTasks.size);
|
|
553
|
+
}
|
|
554
|
+
else {
|
|
555
|
+
this.#removeSubagentTasks(session, meta.subagentKillCount);
|
|
556
|
+
}
|
|
557
|
+
return;
|
|
558
|
+
}
|
|
559
|
+
if (meta.category === "subagent") {
|
|
560
|
+
if (!cancelled && status === "completed" && !session.backgroundTasks.has(toolCallId)) {
|
|
561
|
+
session.backgroundTasks.set(toolCallId, {
|
|
562
|
+
...(meta.description === undefined ? {} : { description: meta.description }),
|
|
563
|
+
kind: "subagent",
|
|
564
|
+
state: "running",
|
|
565
|
+
taskId: toolCallId,
|
|
566
|
+
toolUseId: toolCallId,
|
|
567
|
+
});
|
|
568
|
+
}
|
|
569
|
+
return;
|
|
570
|
+
}
|
|
571
|
+
const kind = backgroundKindFor(meta.category);
|
|
572
|
+
if (kind === null) {
|
|
573
|
+
return;
|
|
574
|
+
}
|
|
575
|
+
if (!cancelled && status === "in_progress") {
|
|
576
|
+
session.backgroundTasks.set(toolCallId, {
|
|
577
|
+
...(meta.description === undefined ? {} : { description: meta.description }),
|
|
578
|
+
kind,
|
|
579
|
+
state: "running",
|
|
580
|
+
taskId: toolCallId,
|
|
581
|
+
toolUseId: toolCallId,
|
|
582
|
+
});
|
|
583
|
+
return;
|
|
584
|
+
}
|
|
585
|
+
if (status === "pending" || status === "completed" || status === "failed" || status === "cancelled") {
|
|
586
|
+
session.backgroundTasks.delete(toolCallId);
|
|
587
|
+
}
|
|
588
|
+
};
|
|
589
|
+
#applyWakeupToolUpdate = (sessionId, session, update, notifications) => {
|
|
590
|
+
if (session.phase.kind !== "wakeup") {
|
|
591
|
+
return;
|
|
592
|
+
}
|
|
593
|
+
const toolCallId = readString(update, "toolCallId");
|
|
594
|
+
const status = readString(update, "status");
|
|
595
|
+
const updateKind = readString(update, "sessionUpdate");
|
|
596
|
+
if (toolCallId === undefined || status === undefined || updateKind === undefined) {
|
|
597
|
+
return;
|
|
598
|
+
}
|
|
599
|
+
const meta = inspectToolMetadata(update, session.toolMetadata.get(toolCallId));
|
|
600
|
+
session.toolMetadata.set(toolCallId, meta);
|
|
601
|
+
const trackedExisting = session.backgroundTasks.get(toolCallId);
|
|
602
|
+
if (trackedExisting !== undefined) {
|
|
603
|
+
if (status === "completed") {
|
|
604
|
+
session.backgroundTasks.set(toolCallId, { ...trackedExisting, state: "awaiting-results" });
|
|
605
|
+
const bg = this.#takeBackgroundTasksUpdate(sessionId, session);
|
|
606
|
+
if (bg !== null) {
|
|
607
|
+
notifications.push(bg);
|
|
608
|
+
}
|
|
609
|
+
if (session.phase.stage.kind === "streaming") {
|
|
610
|
+
this.#setWakeupAwaitingOutput(sessionId, session, session.phase.generation, session.phase.openToolIds);
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
else if (status === "failed" || status === "cancelled") {
|
|
614
|
+
session.backgroundTasks.delete(toolCallId);
|
|
615
|
+
const bg = this.#takeBackgroundTasksUpdate(sessionId, session);
|
|
616
|
+
if (bg !== null) {
|
|
617
|
+
notifications.push(bg);
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
return;
|
|
621
|
+
}
|
|
622
|
+
if (meta.isDaemon) {
|
|
623
|
+
if (session.phase.openToolIds.has(toolCallId)) {
|
|
624
|
+
const nextOpenToolIds = new Set(session.phase.openToolIds);
|
|
625
|
+
nextOpenToolIds.delete(toolCallId);
|
|
626
|
+
session.phase = { ...session.phase, openToolIds: nextOpenToolIds };
|
|
627
|
+
}
|
|
628
|
+
return;
|
|
629
|
+
}
|
|
630
|
+
if (updateKind === "tool_call" && (status === "in_progress" || status === "pending")) {
|
|
631
|
+
if (!session.phase.openToolIds.has(toolCallId)) {
|
|
632
|
+
const nextOpenToolIds = new Set(session.phase.openToolIds);
|
|
633
|
+
nextOpenToolIds.add(toolCallId);
|
|
634
|
+
session.phase = { ...session.phase, openToolIds: nextOpenToolIds };
|
|
635
|
+
}
|
|
636
|
+
this.#pauseOrResumeWakeupTimers(sessionId, session);
|
|
637
|
+
return;
|
|
638
|
+
}
|
|
639
|
+
if (status === "completed" || status === "failed" || status === "cancelled") {
|
|
640
|
+
if (session.phase.openToolIds.has(toolCallId)) {
|
|
641
|
+
const nextOpenToolIds = new Set(session.phase.openToolIds);
|
|
642
|
+
nextOpenToolIds.delete(toolCallId);
|
|
643
|
+
session.phase = { ...session.phase, openToolIds: nextOpenToolIds };
|
|
644
|
+
}
|
|
645
|
+
if (meta.category === "subagent" && status === "completed") {
|
|
646
|
+
session.backgroundTasks.set(toolCallId, {
|
|
647
|
+
...(meta.description === undefined ? {} : { description: meta.description }),
|
|
648
|
+
kind: "subagent",
|
|
649
|
+
state: "running",
|
|
650
|
+
taskId: toolCallId,
|
|
651
|
+
toolUseId: toolCallId,
|
|
652
|
+
});
|
|
653
|
+
const bg = this.#takeBackgroundTasksUpdate(sessionId, session);
|
|
654
|
+
if (bg !== null) {
|
|
655
|
+
notifications.push(bg);
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
else if (meta.category === "manage-subagents" && status === "completed") {
|
|
659
|
+
if (meta.subagentKillAll) {
|
|
660
|
+
this.#removeSubagentTasks(session, session.backgroundTasks.size);
|
|
661
|
+
}
|
|
662
|
+
else {
|
|
663
|
+
this.#removeSubagentTasks(session, meta.subagentKillCount);
|
|
664
|
+
}
|
|
665
|
+
const bg = this.#takeBackgroundTasksUpdate(sessionId, session);
|
|
666
|
+
if (bg !== null) {
|
|
667
|
+
notifications.push(bg);
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
this.#pauseOrResumeWakeupTimers(sessionId, session);
|
|
671
|
+
}
|
|
672
|
+
};
|
|
673
|
+
onOutgoingMessage = async (message) => {
|
|
674
|
+
if (this.#disposed || !isRequestMessage(message)) {
|
|
675
|
+
return;
|
|
676
|
+
}
|
|
677
|
+
const key = requestKeyOf(message.id);
|
|
678
|
+
const params = isRecord(message.params) ? message.params : undefined;
|
|
679
|
+
const sessionId = params === undefined ? undefined : readString(params, "sessionId");
|
|
680
|
+
switch (message.method) {
|
|
681
|
+
case methods.agent.session.load: {
|
|
682
|
+
if (sessionId === undefined) {
|
|
683
|
+
return;
|
|
684
|
+
}
|
|
685
|
+
const session = this.#ensureSession(sessionId);
|
|
686
|
+
this.#cancelWakeupTimers(session.phase);
|
|
687
|
+
session.phase = { kind: "loading", requestKey: key };
|
|
688
|
+
if (key !== undefined) {
|
|
689
|
+
this.#pendingRequests.set(key, { kind: "load", sessionId });
|
|
690
|
+
}
|
|
691
|
+
return;
|
|
692
|
+
}
|
|
693
|
+
case methods.agent.session.new:
|
|
694
|
+
case methods.agent.session.resume: {
|
|
695
|
+
if (sessionId !== undefined && key !== undefined) {
|
|
696
|
+
this.#pendingRequests.set(key, { kind: "session-open", sessionId });
|
|
697
|
+
}
|
|
698
|
+
return;
|
|
699
|
+
}
|
|
700
|
+
case methods.agent.session.prompt: {
|
|
701
|
+
if (sessionId === undefined) {
|
|
702
|
+
return;
|
|
703
|
+
}
|
|
704
|
+
const session = this.#ensureSession(sessionId);
|
|
705
|
+
if (session.phase.kind === "wakeup") {
|
|
706
|
+
for (const notification of this.#finishWakeup(sessionId, session, "end_turn", false)) {
|
|
707
|
+
this.#emitMetaUpdate(notification);
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
const tail = await readTranscriptTailState(this.#launchEnvironment, sessionId, session.baselineStepIndex);
|
|
711
|
+
const prePromptStepIndex = Math.max(session.baselineStepIndex, tail?.maxStepIndex ?? -1);
|
|
712
|
+
session.baselineStepIndex = prePromptStepIndex;
|
|
713
|
+
session.phase = {
|
|
714
|
+
cancelled: false,
|
|
715
|
+
kind: "client-prompt",
|
|
716
|
+
prePromptStepIndex,
|
|
717
|
+
requestKey: key,
|
|
718
|
+
};
|
|
719
|
+
if (key !== undefined) {
|
|
720
|
+
this.#pendingRequests.set(key, { kind: "prompt", sessionId });
|
|
721
|
+
}
|
|
722
|
+
return;
|
|
723
|
+
}
|
|
724
|
+
default:
|
|
725
|
+
return;
|
|
726
|
+
}
|
|
727
|
+
};
|
|
728
|
+
onIncomingMessage = async (message) => {
|
|
729
|
+
if (this.#disposed) {
|
|
730
|
+
return EMPTY_INCOMING_OUTCOME;
|
|
731
|
+
}
|
|
732
|
+
if (isResponseMessage(message)) {
|
|
733
|
+
const key = requestKeyOf(message.id);
|
|
734
|
+
const tracked = key === undefined ? undefined : this.#pendingRequests.get(key);
|
|
735
|
+
if (key !== undefined) {
|
|
736
|
+
this.#pendingRequests.delete(key);
|
|
737
|
+
}
|
|
738
|
+
if (tracked === undefined) {
|
|
739
|
+
return EMPTY_INCOMING_OUTCOME;
|
|
740
|
+
}
|
|
741
|
+
const session = this.#ensureSession(tracked.sessionId);
|
|
742
|
+
if (tracked.kind === "load" || tracked.kind === "session-open") {
|
|
743
|
+
const tail = await readTranscriptTailState(this.#launchEnvironment, tracked.sessionId, session.baselineStepIndex);
|
|
744
|
+
if (tail !== null) {
|
|
745
|
+
session.baselineStepIndex = Math.max(session.baselineStepIndex, tail.maxStepIndex);
|
|
746
|
+
}
|
|
747
|
+
if (tracked.kind === "load") {
|
|
748
|
+
session.phase = { cancelRequested: false, kind: "idle" };
|
|
749
|
+
}
|
|
750
|
+
return EMPTY_INCOMING_OUTCOME;
|
|
751
|
+
}
|
|
752
|
+
const prePromptStepIndex = session.phase.kind === "client-prompt" ? session.phase.prePromptStepIndex : session.baselineStepIndex;
|
|
753
|
+
const promptCancelled = (session.phase.kind === "client-prompt" && session.phase.cancelled) ||
|
|
754
|
+
("result" in message &&
|
|
755
|
+
isRecord(message.result) &&
|
|
756
|
+
readString(message.result, "stopReason") === "cancelled");
|
|
757
|
+
const tail = await readTranscriptTailState(this.#launchEnvironment, tracked.sessionId, session.baselineStepIndex);
|
|
758
|
+
if (tail !== null) {
|
|
759
|
+
session.baselineStepIndex = Math.max(session.baselineStepIndex, tail.maxStepIndex);
|
|
760
|
+
if (!promptCancelled) {
|
|
761
|
+
const consumedSubagents = countConsumedSubagentIdles(tail.steps, prePromptStepIndex, session.baselineStepIndex);
|
|
762
|
+
this.#removeSubagentTasks(session, consumedSubagents);
|
|
763
|
+
}
|
|
764
|
+
}
|
|
765
|
+
if (promptCancelled) {
|
|
766
|
+
session.backgroundTasks.clear();
|
|
767
|
+
}
|
|
768
|
+
session.phase = { cancelRequested: promptCancelled, kind: "idle" };
|
|
769
|
+
const bg = this.#takeBackgroundTasksUpdate(tracked.sessionId, session);
|
|
770
|
+
return bg === null ? EMPTY_INCOMING_OUTCOME : { afterUpdates: [], beforeUpdates: [bg] };
|
|
771
|
+
}
|
|
772
|
+
if (!isNotificationMessage(message) || message.method !== methods.client.session.update) {
|
|
773
|
+
return EMPTY_INCOMING_OUTCOME;
|
|
774
|
+
}
|
|
775
|
+
const params = isRecord(message.params) ? message.params : undefined;
|
|
776
|
+
const sessionId = params === undefined ? undefined : readString(params, "sessionId");
|
|
777
|
+
const update = params === undefined ? undefined : params["update"];
|
|
778
|
+
if (sessionId === undefined || !isRecord(update)) {
|
|
779
|
+
return EMPTY_INCOMING_OUTCOME;
|
|
780
|
+
}
|
|
781
|
+
const updateKind = readString(update, "sessionUpdate");
|
|
782
|
+
if (updateKind === undefined) {
|
|
783
|
+
return EMPTY_INCOMING_OUTCOME;
|
|
784
|
+
}
|
|
785
|
+
const session = this.#ensureSession(sessionId);
|
|
786
|
+
switch (session.phase.kind) {
|
|
787
|
+
case "loading":
|
|
788
|
+
return EMPTY_INCOMING_OUTCOME;
|
|
789
|
+
case "client-prompt": {
|
|
790
|
+
if (updateKind === "tool_call" || updateKind === "tool_call_update") {
|
|
791
|
+
this.#applyClientPromptToolUpdate(session, update, session.phase.cancelled);
|
|
792
|
+
}
|
|
793
|
+
return EMPTY_INCOMING_OUTCOME;
|
|
794
|
+
}
|
|
795
|
+
case "idle": {
|
|
796
|
+
if (updateKind === "tool_call_update") {
|
|
797
|
+
const toolCallId = readString(update, "toolCallId");
|
|
798
|
+
const status = readString(update, "status");
|
|
799
|
+
if (toolCallId !== undefined && status !== undefined) {
|
|
800
|
+
const existing = session.backgroundTasks.get(toolCallId);
|
|
801
|
+
if (existing !== undefined) {
|
|
802
|
+
if (status === "failed" || status === "cancelled") {
|
|
803
|
+
session.backgroundTasks.delete(toolCallId);
|
|
804
|
+
const bg = this.#takeBackgroundTasksUpdate(sessionId, session);
|
|
805
|
+
return bg === null ? EMPTY_INCOMING_OUTCOME : { afterUpdates: [bg], beforeUpdates: [] };
|
|
806
|
+
}
|
|
807
|
+
if (status === "completed") {
|
|
808
|
+
if (session.phase.cancelRequested) {
|
|
809
|
+
session.backgroundTasks.delete(toolCallId);
|
|
810
|
+
const bg = this.#takeBackgroundTasksUpdate(sessionId, session);
|
|
811
|
+
return bg === null
|
|
812
|
+
? EMPTY_INCOMING_OUTCOME
|
|
813
|
+
: { afterUpdates: [bg], beforeUpdates: [] };
|
|
814
|
+
}
|
|
815
|
+
session.backgroundTasks.set(toolCallId, {
|
|
816
|
+
...existing,
|
|
817
|
+
state: "awaiting-results",
|
|
818
|
+
});
|
|
819
|
+
return {
|
|
820
|
+
afterUpdates: [],
|
|
821
|
+
beforeUpdates: this.#enterWakeup(sessionId, session, [toolCallId]),
|
|
822
|
+
};
|
|
823
|
+
}
|
|
824
|
+
}
|
|
825
|
+
}
|
|
826
|
+
return EMPTY_INCOMING_OUTCOME;
|
|
827
|
+
}
|
|
828
|
+
if (session.phase.cancelRequested || !isWakeupStartUpdateKind(updateKind)) {
|
|
829
|
+
return EMPTY_INCOMING_OUTCOME;
|
|
830
|
+
}
|
|
831
|
+
if (updateKind === "tool_call") {
|
|
832
|
+
const meta = inspectToolMetadata(update, undefined);
|
|
833
|
+
if (meta.isDaemon) {
|
|
834
|
+
return EMPTY_INCOMING_OUTCOME;
|
|
835
|
+
}
|
|
836
|
+
}
|
|
837
|
+
const subagentTaskId = this.#oldestRunningSubagentId(session);
|
|
838
|
+
if (subagentTaskId !== undefined) {
|
|
839
|
+
const existing = session.backgroundTasks.get(subagentTaskId);
|
|
840
|
+
if (existing !== undefined) {
|
|
841
|
+
session.backgroundTasks.set(subagentTaskId, { ...existing, state: "awaiting-results" });
|
|
842
|
+
}
|
|
843
|
+
}
|
|
844
|
+
const startedNotifications = this.#enterWakeup(sessionId, session, subagentTaskId === undefined ? [] : [subagentTaskId]);
|
|
845
|
+
const extraWakeup = [];
|
|
846
|
+
this.#handleWakeupUpdate(sessionId, session, updateKind, update, extraWakeup);
|
|
847
|
+
return {
|
|
848
|
+
afterUpdates: [],
|
|
849
|
+
beforeUpdates: [...startedNotifications, ...extraWakeup],
|
|
850
|
+
};
|
|
851
|
+
}
|
|
852
|
+
case "wakeup": {
|
|
853
|
+
const beforeUpdates = [];
|
|
854
|
+
this.#handleWakeupUpdate(sessionId, session, updateKind, update, beforeUpdates);
|
|
855
|
+
return beforeUpdates.length === 0 ? EMPTY_INCOMING_OUTCOME : { afterUpdates: [], beforeUpdates };
|
|
856
|
+
}
|
|
857
|
+
default: {
|
|
858
|
+
const unreachable = session.phase;
|
|
859
|
+
throw new Error(`Unexpected Antigravity session phase: ${String(unreachable)}`);
|
|
860
|
+
}
|
|
861
|
+
}
|
|
862
|
+
};
|
|
863
|
+
#handleWakeupUpdate = (sessionId, session, updateKind, update, beforeUpdates) => {
|
|
864
|
+
if (session.phase.kind !== "wakeup") {
|
|
865
|
+
return;
|
|
866
|
+
}
|
|
867
|
+
if (updateKind === "tool_call" || updateKind === "tool_call_update") {
|
|
868
|
+
this.#applyWakeupToolUpdate(sessionId, session, update, beforeUpdates);
|
|
869
|
+
return;
|
|
870
|
+
}
|
|
871
|
+
if (isWakeupChunkUpdateKind(updateKind)) {
|
|
872
|
+
this.#promoteOpenWakeupToolsToBackground(session);
|
|
873
|
+
const bg = this.#takeBackgroundTasksUpdate(sessionId, session);
|
|
874
|
+
if (bg !== null) {
|
|
875
|
+
beforeUpdates.push(bg);
|
|
876
|
+
}
|
|
877
|
+
if (session.phase.openToolIds.size === 0 && session.activeInteractions === 0) {
|
|
878
|
+
this.#setWakeupStreaming(sessionId, session, session.phase.generation, session.phase.openToolIds);
|
|
879
|
+
}
|
|
880
|
+
}
|
|
881
|
+
};
|
|
882
|
+
beginInteraction = (sessionId) => {
|
|
883
|
+
if (this.#disposed) {
|
|
884
|
+
return;
|
|
885
|
+
}
|
|
886
|
+
const session = this.#ensureSession(sessionId);
|
|
887
|
+
session.activeInteractions += 1;
|
|
888
|
+
this.#pauseOrResumeWakeupTimers(sessionId, session);
|
|
889
|
+
};
|
|
890
|
+
endInteraction = (sessionId) => {
|
|
891
|
+
if (this.#disposed) {
|
|
892
|
+
return;
|
|
893
|
+
}
|
|
894
|
+
const session = this.#sessions.get(sessionId);
|
|
895
|
+
if (session === undefined) {
|
|
896
|
+
return;
|
|
897
|
+
}
|
|
898
|
+
session.activeInteractions = Math.max(0, session.activeInteractions - 1);
|
|
899
|
+
this.#pauseOrResumeWakeupTimers(sessionId, session);
|
|
900
|
+
};
|
|
901
|
+
cancelSession = (sessionId) => {
|
|
902
|
+
if (this.#disposed) {
|
|
903
|
+
return "retain";
|
|
904
|
+
}
|
|
905
|
+
const session = this.#ensureSession(sessionId);
|
|
906
|
+
const mustClose = session.phase.kind === "wakeup" || this.#hasSubagentTasks(session);
|
|
907
|
+
if (mustClose) {
|
|
908
|
+
// Process teardown in AgentSession.#endAgentWork owns emitting terminal
|
|
909
|
+
// lifecycle events after SessionManager detaches the closing runtime.
|
|
910
|
+
this.#cancelWakeupTimers(session.phase);
|
|
911
|
+
session.backgroundTasks.clear();
|
|
912
|
+
session.phase = { cancelRequested: true, kind: "idle" };
|
|
913
|
+
return "close";
|
|
914
|
+
}
|
|
915
|
+
if (session.phase.kind === "client-prompt") {
|
|
916
|
+
session.phase = { ...session.phase, cancelled: true };
|
|
917
|
+
}
|
|
918
|
+
else if (session.phase.kind === "idle") {
|
|
919
|
+
session.phase = { cancelRequested: true, kind: "idle" };
|
|
920
|
+
}
|
|
921
|
+
return "retain";
|
|
922
|
+
};
|
|
923
|
+
onUnexpectedProcessExit = (message) => {
|
|
924
|
+
if (this.#disposed) {
|
|
925
|
+
return;
|
|
926
|
+
}
|
|
927
|
+
for (const [sessionId, session] of this.#sessions) {
|
|
928
|
+
const wasWakeup = session.phase.kind === "wakeup";
|
|
929
|
+
this.#cancelWakeupTimers(session.phase);
|
|
930
|
+
session.backgroundTasks.clear();
|
|
931
|
+
if (wasWakeup) {
|
|
932
|
+
session.phase = { cancelRequested: false, kind: "idle" };
|
|
933
|
+
this.#emitMetaUpdate(makeMetaNotification(sessionId, AGENT_TURN_META, {
|
|
934
|
+
message,
|
|
935
|
+
stage: "failed",
|
|
936
|
+
}));
|
|
937
|
+
}
|
|
938
|
+
else if (session.phase.kind === "idle") {
|
|
939
|
+
session.phase = { cancelRequested: false, kind: "idle" };
|
|
940
|
+
}
|
|
941
|
+
const bg = this.#takeBackgroundTasksUpdate(sessionId, session);
|
|
942
|
+
if (bg !== null) {
|
|
943
|
+
this.#emitMetaUpdate(bg);
|
|
944
|
+
}
|
|
945
|
+
}
|
|
946
|
+
};
|
|
947
|
+
dispose = () => {
|
|
948
|
+
if (this.#disposed) {
|
|
949
|
+
return;
|
|
950
|
+
}
|
|
951
|
+
this.#disposed = true;
|
|
952
|
+
for (const session of this.#sessions.values()) {
|
|
953
|
+
this.#cancelWakeupTimers(session.phase);
|
|
954
|
+
}
|
|
955
|
+
this.#sessions.clear();
|
|
956
|
+
this.#pendingRequests.clear();
|
|
957
|
+
};
|
|
958
|
+
}
|
|
959
|
+
//# sourceMappingURL=background-wakeup.js.map
|