@moxt-ai/mobius 0.0.104 → 0.0.107
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/acp/process-session.d.ts.map +1 -1
- package/dist/acp/process-session.js +11 -6
- package/dist/acp/process-session.js.map +1 -1
- package/dist/acp/session-manager.d.ts +2 -0
- package/dist/acp/session-manager.d.ts.map +1 -1
- package/dist/acp/session-manager.js +5 -0
- package/dist/acp/session-manager.js.map +1 -1
- package/dist/acp/session-store.d.ts +5 -1
- package/dist/acp/session-store.d.ts.map +1 -1
- package/dist/acp/session-store.js +6 -1
- package/dist/acp/session-store.js.map +1 -1
- package/dist/agent-catalog/handle-agent-catalog-refresh.d.ts +21 -0
- package/dist/agent-catalog/handle-agent-catalog-refresh.d.ts.map +1 -0
- package/dist/agent-catalog/handle-agent-catalog-refresh.js +119 -0
- package/dist/agent-catalog/handle-agent-catalog-refresh.js.map +1 -0
- package/dist/agent-catalog/handle-agent-settings-discovery.d.ts +24 -0
- package/dist/agent-catalog/handle-agent-settings-discovery.d.ts.map +1 -0
- package/dist/agent-catalog/handle-agent-settings-discovery.js +129 -0
- package/dist/agent-catalog/handle-agent-settings-discovery.js.map +1 -0
- package/dist/cli.js +2810 -3760
- package/dist/cli.js.map +4 -4
- package/dist/daemon-manager.d.ts.map +1 -1
- package/dist/daemon-manager.js +2 -9
- package/dist/daemon-manager.js.map +1 -1
- package/dist/daemon-session-commands.d.ts +6 -6
- package/dist/daemon-session-commands.d.ts.map +1 -1
- package/dist/daemon-session-commands.js +43 -30
- package/dist/daemon-session-commands.js.map +1 -1
- package/dist/daemon-turn-reconciliation.d.ts +4 -2
- package/dist/daemon-turn-reconciliation.d.ts.map +1 -1
- package/dist/daemon-turn-reconciliation.js +24 -3
- package/dist/daemon-turn-reconciliation.js.map +1 -1
- package/dist/index.js +2794 -3735
- package/dist/index.js.map +4 -4
- package/dist/native-sessions/handle-native-requests.d.ts +22 -0
- package/dist/native-sessions/handle-native-requests.d.ts.map +1 -0
- package/dist/native-sessions/handle-native-requests.js +91 -0
- package/dist/native-sessions/handle-native-requests.js.map +1 -0
- package/dist/native-sessions/service.d.ts +2 -1
- package/dist/native-sessions/service.d.ts.map +1 -1
- package/dist/native-sessions/service.js +27 -0
- package/dist/native-sessions/service.js.map +1 -1
- package/dist/relay-connection/coalescing-narration-publisher.d.ts +3 -3
- package/dist/relay-connection/coalescing-narration-publisher.js +3 -3
- package/dist/relay-connection/coalescing-text-publisher.d.ts +3 -4
- package/dist/relay-connection/coalescing-text-publisher.d.ts.map +1 -1
- package/dist/relay-connection/coalescing-text-publisher.js +3 -4
- package/dist/relay-connection/coalescing-text-publisher.js.map +1 -1
- package/dist/relay-connection/daemon-operation-state.d.ts +18 -0
- package/dist/relay-connection/daemon-operation-state.d.ts.map +1 -0
- package/dist/relay-connection/daemon-operation-state.js +69 -0
- package/dist/relay-connection/daemon-operation-state.js.map +1 -0
- package/dist/relay-connection/session-event-publisher.d.ts +3 -9
- package/dist/relay-connection/session-event-publisher.d.ts.map +1 -1
- package/dist/relay-connection/session-event-publisher.js +30 -224
- package/dist/relay-connection/session-event-publisher.js.map +1 -1
- package/dist/run-daemon.d.ts +1 -1
- package/dist/run-daemon.d.ts.map +1 -1
- package/dist/run-daemon.js +96 -618
- package/dist/run-daemon.js.map +1 -1
- package/dist/runtime.js +2821 -3764
- package/dist/runtime.js.map +4 -4
- package/package.json +1 -1
- package/dist/relay-connection/session-event-outbox.d.ts +0 -61
- package/dist/relay-connection/session-event-outbox.d.ts.map +0 -1
- package/dist/relay-connection/session-event-outbox.js +0 -546
- package/dist/relay-connection/session-event-outbox.js.map +0 -1
package/dist/run-daemon.js
CHANGED
|
@@ -1,30 +1,30 @@
|
|
|
1
1
|
import { randomUUID } from "node:crypto";
|
|
2
2
|
import { styleText } from "node:util";
|
|
3
|
-
import {
|
|
3
|
+
import { AGENT_CATALOG_REFRESH_KIND, AGENT_INSTALL_CANCEL_KIND, AGENT_INSTALL_REQUEST_KIND, AGENT_SETTINGS_DISCOVER_KIND, decodeServiceToDaemonMessage, decodeSessionLiveHistoryLoad, decodeSessionLiveHistoryPageLoad, FILE_FAILED_KIND, FILE_READ_KIND, NATIVE_SESSION_CANCEL_KIND, NATIVE_SESSION_REQUEST_KIND, NATIVE_SESSION_TURN_FORK_KIND, RUNTIME_HEARTBEAT_ACK_KIND, RUNTIME_LIVENESS_READY_KIND, SESSION_CANCEL_KIND, SESSION_CONFIGURE_KIND, SESSION_ELICITATION_RESPOND_KIND, SESSION_LIVE_HISTORY_LOAD_KIND, SESSION_LIVE_HISTORY_PAGE_LOAD_KIND, SESSION_PERMISSION_RESPOND_KIND, SESSION_PREPARE_KIND, SESSION_PROMPT_KIND, SessionPromptDisposition, WORKSPACE_LIST_KIND, } from "@moxt-ai/mobius-protocol";
|
|
4
4
|
import { WebSocket } from "undici";
|
|
5
|
-
import {
|
|
5
|
+
import { AcpSessionManager } from "./acp/session-manager.js";
|
|
6
6
|
import { MemoryAcpSessionStore } from "./acp/session-store.js";
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
7
|
+
import { supportedLocalAgentSpecifications } from "./agent-catalog/discover-local-agents.js";
|
|
8
|
+
import { createAgentCatalogRefreshHandler } from "./agent-catalog/handle-agent-catalog-refresh.js";
|
|
9
|
+
import { createAgentSettingsDiscoveryHandler } from "./agent-catalog/handle-agent-settings-discovery.js";
|
|
9
10
|
import { AgentIntegrationDefinition } from "./agent-catalog/local-agent.js";
|
|
10
11
|
import { publishAgentCatalog } from "./agent-catalog/publish-agent-catalog.js";
|
|
11
12
|
import { AgentInstallationManager } from "./agent-installation/agent-installation-manager.js";
|
|
12
13
|
import { FixedInstallationExecutor } from "./agent-installation/fixed-installation-executor.js";
|
|
13
14
|
import { streamLocalFile } from "./artifacts/local-file-streamer.js";
|
|
14
15
|
import { WorkspaceDirectoryService } from "./authorization/workspace-directory.js";
|
|
15
|
-
import { DaemonLocalContext, withDaemonEnvironment } from "./config.js";
|
|
16
16
|
import { DaemonDiagnosticLog } from "./daemon-diagnostic-log.js";
|
|
17
17
|
import { AuditInstallationEvents, DaemonAcpObserver, DaemonInstallationObserver } from "./daemon-observers.js";
|
|
18
|
-
import {
|
|
18
|
+
import { executePrompt, executeSessionConfigure, executeSessionLiveHistoryLoad, executeSessionLiveHistoryPageLoad, executeSessionPrepare, executeWorkspaceList, reportOperationFailure, send, settleAbandonedTurn, } from "./daemon-session-commands.js";
|
|
19
19
|
import { ActiveDaemonTurn, ActiveTurnReconciliationError, reconcileActiveTurns } from "./daemon-turn-reconciliation.js";
|
|
20
20
|
import { readCodexFullHistory } from "./native-sessions/codex-history.js";
|
|
21
|
+
import { createNativeSessionRequestHandler } from "./native-sessions/handle-native-requests.js";
|
|
21
22
|
import { NativeSessionService } from "./native-sessions/service.js";
|
|
22
|
-
import { revalidateExecutable } from "./process/executable.js";
|
|
23
23
|
import { connectRelay } from "./relay-connection/connect.js";
|
|
24
|
+
import { DaemonOperationState } from "./relay-connection/daemon-operation-state.js";
|
|
24
25
|
import { RelayConnectionDiagnostics } from "./relay-connection/diagnostics.js";
|
|
25
26
|
import { RelayConnectionLiveness } from "./relay-connection/liveness.js";
|
|
26
27
|
import { isRuntimeRelayRevocation, RuntimeRelayRevokedError } from "./relay-connection/runtime-revocation.js";
|
|
27
|
-
import { RuntimeDeliveryInboxCapacityError, SessionEventOutbox } from "./relay-connection/session-event-outbox.js";
|
|
28
28
|
import { DaemonSessionEventPublisher } from "./relay-connection/session-event-publisher.js";
|
|
29
29
|
import { systemDaemonTimer } from "./relay-connection/timer.js";
|
|
30
30
|
const RELAY_RECONNECT_DELAY_MILLISECONDS = 500;
|
|
@@ -94,60 +94,40 @@ async function installDetectedAgentIntegrations(config, installations, signal) {
|
|
|
94
94
|
}, signal);
|
|
95
95
|
}
|
|
96
96
|
}
|
|
97
|
-
|
|
98
|
-
// as PATH and the managed state directory. An Agent that was verified a moment ago is therefore not
|
|
99
|
-
// necessarily uninstalled just because one discovery attempt failed to re-verify it. Keep the last
|
|
100
|
-
// verified definition while the exact executable it was verified against still exists unchanged on
|
|
101
|
-
// disk, so a transient probe failure cannot silently drop a working Agent. A genuine removal, or a
|
|
102
|
-
// replaced executable, stops satisfying that check and is still reflected in the catalog.
|
|
103
|
-
async function retainStillInstalledAgents(previous, refreshed, signal) {
|
|
104
|
-
const refreshedIdentifiers = new Set(refreshed.map((agent) => agent.id));
|
|
105
|
-
const retained = [];
|
|
106
|
-
for (const agent of previous) {
|
|
107
|
-
if (refreshedIdentifiers.has(agent.id)) {
|
|
108
|
-
continue;
|
|
109
|
-
}
|
|
110
|
-
signal.throwIfAborted();
|
|
111
|
-
if (await revalidateExecutable(agent.agentIdentity)) {
|
|
112
|
-
retained.push(agent);
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
return Object.freeze([...refreshed, ...retained]);
|
|
116
|
-
}
|
|
117
|
-
async function refreshAgentCatalog(config, resolveEnvironment, signal) {
|
|
118
|
-
signal.throwIfAborted();
|
|
119
|
-
const shellEnvironment = await resolveEnvironment(signal);
|
|
120
|
-
const environment = new DaemonLocalContext(config.localContext.stateDirectory, shellEnvironment).environment;
|
|
121
|
-
signal.throwIfAborted();
|
|
122
|
-
const inventory = await discoverLocalAgentInventory({
|
|
123
|
-
defaultWorkspace: config.defaultWorkspace,
|
|
124
|
-
environment,
|
|
125
|
-
signal,
|
|
126
|
-
});
|
|
127
|
-
const previousCatalog = config.catalog.read();
|
|
128
|
-
const refreshedAgents = inventory.readyAgents().map((agent) => withDaemonEnvironment(agent, environment));
|
|
129
|
-
const agents = await retainStillInstalledAgents(previousCatalog.agents, refreshedAgents, signal);
|
|
130
|
-
const verifiedIdentifiers = new Set(agents.map((agent) => agent.id));
|
|
131
|
-
config.catalog.transition(agents, inventory.installable.filter((agent) => !verifiedIdentifiers.has(agent.id)));
|
|
132
|
-
try {
|
|
133
|
-
await publishAgentCatalog(config, signal);
|
|
134
|
-
}
|
|
135
|
-
catch (error) {
|
|
136
|
-
config.catalog.transition(previousCatalog.agents, previousCatalog.installableAgents);
|
|
137
|
-
throw error;
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
async function runRelayConnection(config, directories, audit, sessions, installations, installationObserver, nativeSessions, outbox, publisher, activeTurns, activeAttemptIds, connectionEpoch, diagnostics, onConnected, signal, timer, resolveAgentCatalogEnvironment) {
|
|
97
|
+
async function runRelayConnection(config, directories, audit, sessions, installations, installationObserver, nativeSessions, operations, publisher, activeTurns, connectionEpoch, diagnostics, onConnected, signal, timer, resolveAgentCatalogEnvironment) {
|
|
141
98
|
const relayUrl = new URL(config.relayUrl);
|
|
142
|
-
relayUrl.searchParams.set("deliveryReceipts", "1");
|
|
143
99
|
relayUrl.searchParams.set("liveness", "1");
|
|
144
100
|
const socket = await connectRelay(relayUrl, signal);
|
|
145
101
|
const reconnectingTurns = [...activeTurns.values()];
|
|
146
102
|
diagnostics.connectionOpened(connectionEpoch);
|
|
147
103
|
const connectionController = new AbortController();
|
|
148
|
-
const
|
|
149
|
-
|
|
150
|
-
|
|
104
|
+
const handleSettingsDiscovery = createAgentSettingsDiscoveryHandler({
|
|
105
|
+
config,
|
|
106
|
+
directories,
|
|
107
|
+
audit,
|
|
108
|
+
sessions,
|
|
109
|
+
diagnostics,
|
|
110
|
+
socket,
|
|
111
|
+
signal: connectionController.signal,
|
|
112
|
+
timer,
|
|
113
|
+
});
|
|
114
|
+
const handleCatalogRefresh = createAgentCatalogRefreshHandler({
|
|
115
|
+
config,
|
|
116
|
+
audit,
|
|
117
|
+
diagnostics,
|
|
118
|
+
socket,
|
|
119
|
+
signal: connectionController.signal,
|
|
120
|
+
timer,
|
|
121
|
+
resolveEnvironment: resolveAgentCatalogEnvironment,
|
|
122
|
+
});
|
|
123
|
+
const nativeRequestHandler = createNativeSessionRequestHandler({
|
|
124
|
+
audit,
|
|
125
|
+
diagnostics,
|
|
126
|
+
nativeSessions,
|
|
127
|
+
signal: connectionController.signal,
|
|
128
|
+
socket,
|
|
129
|
+
timer,
|
|
130
|
+
});
|
|
151
131
|
installationObserver.connect(socket);
|
|
152
132
|
onConnected();
|
|
153
133
|
try {
|
|
@@ -190,197 +170,48 @@ async function runRelayConnection(config, directories, audit, sessions, installa
|
|
|
190
170
|
closeRelaySocket(socket, code, reason);
|
|
191
171
|
rejectConnection(failure);
|
|
192
172
|
};
|
|
193
|
-
const
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
reportFailure(publisher, command, "agent_execution_interrupted", "The local Agent execution was interrupted before it completed", receipt.attemptId);
|
|
215
|
-
outbox.transitionExecution(command.commandId, "failed");
|
|
216
|
-
return;
|
|
217
|
-
}
|
|
218
|
-
if (receipt.state !== "dispatching") {
|
|
219
|
-
return;
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
const turn = new ActiveDaemonTurn(command, receipt.attemptId);
|
|
223
|
-
activeTurns.set(command.commandId, turn);
|
|
224
|
-
const activateAttempt = () => {
|
|
225
|
-
const stream = turn.accept();
|
|
226
|
-
if (trackActiveAttempt) {
|
|
227
|
-
activeAttemptIds.set(command.sessionId, receipt.attemptId);
|
|
228
|
-
}
|
|
229
|
-
return stream;
|
|
230
|
-
};
|
|
231
|
-
try {
|
|
232
|
-
await execute(receipt.attemptId, activateAttempt, turn.signal, turn);
|
|
233
|
-
}
|
|
234
|
-
finally {
|
|
235
|
-
activeTurns.delete(command.commandId);
|
|
236
|
-
if (activeAttemptIds.get(command.sessionId) === receipt.attemptId) {
|
|
237
|
-
activeAttemptIds.delete(command.sessionId);
|
|
238
|
-
}
|
|
239
|
-
turn.finish();
|
|
240
|
-
}
|
|
173
|
+
const waitForTurnCompletion = (turn, waitSignal) => {
|
|
174
|
+
waitSignal.throwIfAborted();
|
|
175
|
+
return new Promise((resolve, reject) => {
|
|
176
|
+
let settled = false;
|
|
177
|
+
const finish = (result) => {
|
|
178
|
+
if (settled) {
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
settled = true;
|
|
182
|
+
waitSignal.removeEventListener("abort", onAbort);
|
|
183
|
+
if (result.kind === "completed") {
|
|
184
|
+
resolve();
|
|
185
|
+
}
|
|
186
|
+
else {
|
|
187
|
+
reject(result.error);
|
|
188
|
+
}
|
|
189
|
+
};
|
|
190
|
+
const onAbort = () => finish({ error: waitSignal.reason, kind: "failed" });
|
|
191
|
+
waitSignal.addEventListener("abort", onAbort, { once: true });
|
|
192
|
+
void turn.completion.then(() => finish({ kind: "completed" }), (error) => finish({ error, kind: "failed" }));
|
|
193
|
+
});
|
|
241
194
|
};
|
|
242
|
-
const
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
if (kind === SESSION_CANCEL_KIND) {
|
|
246
|
-
const cancellation = message;
|
|
247
|
-
if (cancellation.sessionId !== delivery.sessionId) {
|
|
248
|
-
throw new Error("Runtime delivery Session identity does not match its payload");
|
|
249
|
-
}
|
|
250
|
-
const turn = activeTurns.get(cancellation.commandId);
|
|
251
|
-
if (turn === undefined || !turn.matches(cancellation)) {
|
|
252
|
-
settleAbandonedTurn(publisher, outbox, audit, cancellation);
|
|
253
|
-
return;
|
|
254
|
-
}
|
|
255
|
-
if (!turn.requestCancellation()) {
|
|
256
|
-
return;
|
|
257
|
-
}
|
|
258
|
-
audit.record({
|
|
259
|
-
commandId: cancellation.commandId,
|
|
260
|
-
event: "session_cancellation_requested",
|
|
261
|
-
sessionId: cancellation.sessionId,
|
|
262
|
-
});
|
|
263
|
-
await sessions.cancelPrompt(cancellation.sessionId);
|
|
264
|
-
return;
|
|
265
|
-
}
|
|
266
|
-
if (kind === SESSION_CONFIGURE_KIND) {
|
|
267
|
-
const command = message;
|
|
268
|
-
if (command.sessionId !== delivery.sessionId) {
|
|
269
|
-
throw new Error("Runtime delivery Session identity does not match its payload");
|
|
270
|
-
}
|
|
271
|
-
await executeSessionConfigure(publisher, socket, command, config, directories, audit, sessions, connectionController.signal);
|
|
272
|
-
return;
|
|
273
|
-
}
|
|
274
|
-
if (kind === SESSION_PERMISSION_RESPOND_KIND) {
|
|
275
|
-
const command = message;
|
|
276
|
-
if (command.sessionId !== delivery.sessionId) {
|
|
277
|
-
throw new Error("Runtime delivery Session identity does not match its payload");
|
|
278
|
-
}
|
|
279
|
-
try {
|
|
280
|
-
await sessions.respondPermission(command);
|
|
281
|
-
audit.record({
|
|
282
|
-
commandId: command.interactionId,
|
|
283
|
-
event: "session_permission_resolved",
|
|
284
|
-
sessionId: command.sessionId,
|
|
285
|
-
});
|
|
286
|
-
}
|
|
287
|
-
catch (error) {
|
|
288
|
-
reportOperationFailure(socket, command.interactionId, command.sessionId, error);
|
|
289
|
-
}
|
|
290
|
-
return;
|
|
195
|
+
const waitForSessionAvailability = async (command, waitSignal) => {
|
|
196
|
+
if (command.disposition === SessionPromptDisposition.Steer) {
|
|
197
|
+
return "available";
|
|
291
198
|
}
|
|
292
|
-
|
|
293
|
-
const
|
|
294
|
-
|
|
295
|
-
|
|
199
|
+
for (;;) {
|
|
200
|
+
const first = [...activeTurns.values()].find((turn) => turn.command.sessionId === command.sessionId &&
|
|
201
|
+
turn.command.disposition !== SessionPromptDisposition.Steer);
|
|
202
|
+
if (first === undefined || first.command.commandId === command.commandId) {
|
|
203
|
+
return command.disposition !== SessionPromptDisposition.Queue ||
|
|
204
|
+
operations.isTerminalExecution(command.targetCommandId, command.sessionId)
|
|
205
|
+
? "available"
|
|
206
|
+
: "queue-target-unavailable";
|
|
296
207
|
}
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
audit.record({
|
|
300
|
-
commandId: command.interactionId,
|
|
301
|
-
event: "session_elicitation_resolved",
|
|
302
|
-
sessionId: command.sessionId,
|
|
303
|
-
});
|
|
208
|
+
if (command.disposition !== SessionPromptDisposition.Queue) {
|
|
209
|
+
return "busy";
|
|
304
210
|
}
|
|
305
|
-
|
|
306
|
-
|
|
211
|
+
if (first.command.commandId !== command.targetCommandId) {
|
|
212
|
+
return "queue-target-unavailable";
|
|
307
213
|
}
|
|
308
|
-
|
|
309
|
-
}
|
|
310
|
-
if (kind === SESSION_PROMPT_KIND) {
|
|
311
|
-
const command = message;
|
|
312
|
-
await processDeliveredPrompt(delivery, command, command.disposition !== SessionPromptDisposition.Steer, (attemptId, activateAttempt, executionSignal, turn) => executePrompt(outbox, publisher, command, config, directories, audit, sessions, attemptId, activateAttempt, () => sessionAvailable(command), timer, executionSignal, turn.recordText));
|
|
313
|
-
return;
|
|
314
|
-
}
|
|
315
|
-
throw new Error("Runtime delivery contains an unsupported protocol message");
|
|
316
|
-
};
|
|
317
|
-
const processRuntimeDelivery = (delivery, state) => {
|
|
318
|
-
if (state === "processed" || processingDeliveryIds.has(delivery.deliveryId)) {
|
|
319
|
-
return;
|
|
320
|
-
}
|
|
321
|
-
processingDeliveryIds.add(delivery.deliveryId);
|
|
322
|
-
void processDeliveredPayload(delivery)
|
|
323
|
-
.then(() => {
|
|
324
|
-
outbox.markRuntimeDeliveryProcessed(delivery.deliveryId, delivery.sessionId);
|
|
325
|
-
})
|
|
326
|
-
.catch((error) => {
|
|
327
|
-
if (connectionController.signal.aborted) {
|
|
328
|
-
return;
|
|
329
|
-
}
|
|
330
|
-
console.error("Mobius daemon could not process a in-memory Runtime delivery", error);
|
|
331
|
-
failAndClose(new Error("Runtime delivery processing failed", { cause: error }), 4008, "Invalid Runtime delivery", "delivery");
|
|
332
|
-
})
|
|
333
|
-
.finally(() => processingDeliveryIds.delete(delivery.deliveryId));
|
|
334
|
-
};
|
|
335
|
-
const receiveRuntimeDelivery = (delivery) => {
|
|
336
|
-
const payload = decodeServiceToDaemonMessage(delivery.payload);
|
|
337
|
-
const payloadKind = payload.kind;
|
|
338
|
-
let payloadSessionId = "";
|
|
339
|
-
if (payloadKind === SESSION_CANCEL_KIND) {
|
|
340
|
-
payloadSessionId = payload.sessionId;
|
|
341
|
-
}
|
|
342
|
-
else if (payloadKind === SESSION_CONFIGURE_KIND) {
|
|
343
|
-
payloadSessionId = payload.sessionId;
|
|
344
|
-
}
|
|
345
|
-
else if (payloadKind === SESSION_PERMISSION_RESPOND_KIND) {
|
|
346
|
-
payloadSessionId = payload.sessionId;
|
|
347
|
-
}
|
|
348
|
-
else if (payloadKind === SESSION_ELICITATION_RESPOND_KIND) {
|
|
349
|
-
payloadSessionId = payload.sessionId;
|
|
350
|
-
}
|
|
351
|
-
else if (payloadKind === SESSION_PROMPT_KIND) {
|
|
352
|
-
payloadSessionId = payload.sessionId;
|
|
353
|
-
}
|
|
354
|
-
else {
|
|
355
|
-
throw new Error("Runtime delivery contains an unsupported protocol message");
|
|
356
|
-
}
|
|
357
|
-
if (payloadSessionId !== delivery.sessionId) {
|
|
358
|
-
throw new Error("Runtime delivery Session identity does not match its payload");
|
|
359
|
-
}
|
|
360
|
-
const stored = outbox.claimRuntimeDelivery(delivery);
|
|
361
|
-
send(socket, {
|
|
362
|
-
deliveryId: delivery.deliveryId,
|
|
363
|
-
kind: RUNTIME_DELIVERY_RECEIVED_KIND,
|
|
364
|
-
sessionId: delivery.sessionId,
|
|
365
|
-
});
|
|
366
|
-
processRuntimeDelivery(delivery, stored.state);
|
|
367
|
-
};
|
|
368
|
-
const resumeRuntimeDeliveries = () => {
|
|
369
|
-
for (const stored of outbox.runtimeDeliveries()) {
|
|
370
|
-
const delivery = {
|
|
371
|
-
deliveryId: stored.deliveryId,
|
|
372
|
-
kind: RUNTIME_DELIVERY_KIND,
|
|
373
|
-
payload: stored.payload,
|
|
374
|
-
sessionId: stored.sessionId,
|
|
375
|
-
};
|
|
376
|
-
if (stored.state !== "relay_committed") {
|
|
377
|
-
send(socket, {
|
|
378
|
-
deliveryId: stored.deliveryId,
|
|
379
|
-
kind: RUNTIME_DELIVERY_RECEIVED_KIND,
|
|
380
|
-
sessionId: stored.sessionId,
|
|
381
|
-
});
|
|
382
|
-
}
|
|
383
|
-
processRuntimeDelivery(delivery, stored.state);
|
|
214
|
+
await waitForTurnCompletion(first, waitSignal);
|
|
384
215
|
}
|
|
385
216
|
};
|
|
386
217
|
const connectionLiveness = new RelayConnectionLiveness(timer, {
|
|
@@ -435,40 +266,6 @@ async function runRelayConnection(config, directories, audit, sessions, installa
|
|
|
435
266
|
try {
|
|
436
267
|
const message = decodeServiceToDaemonMessage(encoded);
|
|
437
268
|
const kind = message.kind;
|
|
438
|
-
if (kind === RUNTIME_DELIVERY_COMMITTED_KIND) {
|
|
439
|
-
const committed = message;
|
|
440
|
-
if (!outbox.markRuntimeDeliveryCommitted(committed.deliveryId, committed.sessionId)) {
|
|
441
|
-
console.warn("Mobius daemon ignored confirmation for an unknown Runtime delivery");
|
|
442
|
-
}
|
|
443
|
-
return;
|
|
444
|
-
}
|
|
445
|
-
if (kind === RUNTIME_DELIVERY_KIND) {
|
|
446
|
-
receiveRuntimeDelivery(message);
|
|
447
|
-
return;
|
|
448
|
-
}
|
|
449
|
-
if (kind === SESSION_EVENT_ACK_KIND) {
|
|
450
|
-
publisher.acknowledge(message);
|
|
451
|
-
return;
|
|
452
|
-
}
|
|
453
|
-
if (kind === SESSION_EVENT_DEFERRED_KIND) {
|
|
454
|
-
publisher.defer(message);
|
|
455
|
-
return;
|
|
456
|
-
}
|
|
457
|
-
if (kind === SESSION_EVENT_REJECTED_KIND) {
|
|
458
|
-
const rejection = message;
|
|
459
|
-
if (publisher.quarantine(rejection)) {
|
|
460
|
-
audit.record({
|
|
461
|
-
commandId: rejection.eventId,
|
|
462
|
-
details: { code: rejection.code },
|
|
463
|
-
event: "session_event_rejected",
|
|
464
|
-
sessionId: rejection.sessionId,
|
|
465
|
-
});
|
|
466
|
-
}
|
|
467
|
-
else {
|
|
468
|
-
console.warn("Mobius daemon ignored a rejection for an unknown session event");
|
|
469
|
-
}
|
|
470
|
-
return;
|
|
471
|
-
}
|
|
472
269
|
if (kind === SESSION_LIVE_HISTORY_LOAD_KIND) {
|
|
473
270
|
const command = decodeSessionLiveHistoryLoad(encoded);
|
|
474
271
|
void executeSessionLiveHistoryLoad(socket, command, config, directories, audit, sessions, connectionController.signal);
|
|
@@ -480,324 +277,19 @@ async function runRelayConnection(config, directories, audit, sessions, installa
|
|
|
480
277
|
return;
|
|
481
278
|
}
|
|
482
279
|
if (kind === NATIVE_SESSION_CANCEL_KIND) {
|
|
483
|
-
|
|
484
|
-
const pending = nativeRequests.get(command.requestId);
|
|
485
|
-
if (pending?.sessionId === command.sessionId) {
|
|
486
|
-
pending.controller.abort(new AcpExecutionError("Native session operation cancelled", {}, "native_session_cancelled"));
|
|
487
|
-
}
|
|
280
|
+
nativeRequestHandler.cancel(message);
|
|
488
281
|
return;
|
|
489
282
|
}
|
|
490
283
|
if (kind === AGENT_SETTINGS_DISCOVER_KIND) {
|
|
491
|
-
|
|
492
|
-
const existing = [...agentSettingsDiscoveries.values()].find((request) => request.agentId === command.agentId && !request.controller.signal.aborted);
|
|
493
|
-
if ([...agentSettingsDiscoveries.values()].some((request) => request.requestIds.has(command.requestId))) {
|
|
494
|
-
if (socket.readyState === WebSocket.OPEN) {
|
|
495
|
-
send(socket, {
|
|
496
|
-
code: "agent_settings_discovery_busy",
|
|
497
|
-
kind: AGENT_SETTINGS_DISCOVERY_FAILED_KIND,
|
|
498
|
-
message: "This settings discovery request is already running",
|
|
499
|
-
requestId: command.requestId,
|
|
500
|
-
});
|
|
501
|
-
}
|
|
502
|
-
return;
|
|
503
|
-
}
|
|
504
|
-
if (existing !== undefined &&
|
|
505
|
-
existing.model === command.model &&
|
|
506
|
-
existing.requestIds.size < 16) {
|
|
507
|
-
existing.requestIds.add(command.requestId);
|
|
508
|
-
return;
|
|
509
|
-
}
|
|
510
|
-
if (existing !== undefined) {
|
|
511
|
-
if (socket.readyState === WebSocket.OPEN) {
|
|
512
|
-
send(socket, {
|
|
513
|
-
code: "agent_settings_discovery_busy",
|
|
514
|
-
kind: AGENT_SETTINGS_DISCOVERY_FAILED_KIND,
|
|
515
|
-
message: "Another settings discovery is already running for this Agent",
|
|
516
|
-
requestId: command.requestId,
|
|
517
|
-
});
|
|
518
|
-
}
|
|
519
|
-
return;
|
|
520
|
-
}
|
|
521
|
-
if (agentSettingsDiscoveries.size >= 4) {
|
|
522
|
-
if (socket.readyState === WebSocket.OPEN) {
|
|
523
|
-
send(socket, {
|
|
524
|
-
code: "agent_settings_discovery_busy",
|
|
525
|
-
kind: AGENT_SETTINGS_DISCOVERY_FAILED_KIND,
|
|
526
|
-
message: "Too many Agent settings discoveries are running",
|
|
527
|
-
requestId: command.requestId,
|
|
528
|
-
});
|
|
529
|
-
}
|
|
530
|
-
return;
|
|
531
|
-
}
|
|
532
|
-
const controller = new AbortController();
|
|
533
|
-
const requestIds = new Set([command.requestId]);
|
|
534
|
-
const operationSignal = AbortSignal.any([connectionController.signal, controller.signal]);
|
|
535
|
-
const timeout = timer.schedule(() => controller.abort(new AcpExecutionError("Agent settings discovery timed out", {}, "agent_settings_discovery_timeout")), 90_000);
|
|
536
|
-
const completion = (async () => {
|
|
537
|
-
try {
|
|
538
|
-
const result = await discoverAgentSettings(command, config, directories, sessions, operationSignal);
|
|
539
|
-
for (const diagnostic of result.unsupportedModels) {
|
|
540
|
-
const details = agentSettingsFailureDetails(command.agentId, diagnostic);
|
|
541
|
-
audit.record({
|
|
542
|
-
commandId: command.requestId,
|
|
543
|
-
details,
|
|
544
|
-
event: "agent_settings_model_unsupported",
|
|
545
|
-
sessionId: `agent-settings:${command.agentId}`,
|
|
546
|
-
});
|
|
547
|
-
diagnostics.daemonDiagnostic("agent_settings_model_unsupported", command.requestId, details);
|
|
548
|
-
process.stderr.write(`Mobius daemon diagnostic ${JSON.stringify({ event: "agent_settings_model_unsupported", requestId: command.requestId, ...details })}\n`);
|
|
549
|
-
}
|
|
550
|
-
for (const requestId of requestIds) {
|
|
551
|
-
audit.record({
|
|
552
|
-
commandId: requestId,
|
|
553
|
-
details: {
|
|
554
|
-
agentId: command.agentId,
|
|
555
|
-
unsupportedModelCount: result.unsupportedModels.length.toString(),
|
|
556
|
-
},
|
|
557
|
-
event: "agent_settings_discovered",
|
|
558
|
-
sessionId: `agent-settings:${command.agentId}`,
|
|
559
|
-
});
|
|
560
|
-
if (socket.readyState === WebSocket.OPEN) {
|
|
561
|
-
send(socket, { ...result.event, requestId });
|
|
562
|
-
}
|
|
563
|
-
}
|
|
564
|
-
}
|
|
565
|
-
catch (error) {
|
|
566
|
-
const failure = operationSignal.aborted
|
|
567
|
-
? (controller.signal.reason ??
|
|
568
|
-
new AcpExecutionError("Agent settings discovery cancelled", {}, "agent_settings_discovery_cancelled"))
|
|
569
|
-
: error;
|
|
570
|
-
const details = agentSettingsFailureDetails(command.agentId, failure);
|
|
571
|
-
for (const requestId of requestIds) {
|
|
572
|
-
audit.record({
|
|
573
|
-
commandId: requestId,
|
|
574
|
-
details,
|
|
575
|
-
event: "agent_settings_discovery_failed",
|
|
576
|
-
sessionId: `agent-settings:${command.agentId}`,
|
|
577
|
-
});
|
|
578
|
-
}
|
|
579
|
-
diagnostics.daemonDiagnostic("agent_settings_discovery_failed", command.requestId, details, failure);
|
|
580
|
-
if (failure instanceof AcpConfigurationValueUnavailableError) {
|
|
581
|
-
process.stderr.write(`Mobius daemon diagnostic ${JSON.stringify({ event: "agent_settings_discovery_failed", requestId: command.requestId, ...details })}\n`);
|
|
582
|
-
}
|
|
583
|
-
if (socket.readyState === WebSocket.OPEN) {
|
|
584
|
-
for (const requestId of requestIds) {
|
|
585
|
-
send(socket, {
|
|
586
|
-
code: operationErrorCode(failure),
|
|
587
|
-
kind: AGENT_SETTINGS_DISCOVERY_FAILED_KIND,
|
|
588
|
-
message: operationErrorMessage(failure),
|
|
589
|
-
requestId,
|
|
590
|
-
});
|
|
591
|
-
}
|
|
592
|
-
}
|
|
593
|
-
}
|
|
594
|
-
finally {
|
|
595
|
-
timeout.cancel();
|
|
596
|
-
agentSettingsDiscoveries.delete(command.requestId);
|
|
597
|
-
}
|
|
598
|
-
})();
|
|
599
|
-
agentSettingsDiscoveries.set(command.requestId, {
|
|
600
|
-
agentId: command.agentId,
|
|
601
|
-
model: command.model,
|
|
602
|
-
requestIds,
|
|
603
|
-
completion,
|
|
604
|
-
controller,
|
|
605
|
-
});
|
|
284
|
+
handleSettingsDiscovery.handle(message);
|
|
606
285
|
return;
|
|
607
286
|
}
|
|
608
287
|
if (kind === AGENT_CATALOG_REFRESH_KIND) {
|
|
609
|
-
|
|
610
|
-
if (agentCatalogRefreshes.has(command.requestId) || agentCatalogRefreshes.size > 0) {
|
|
611
|
-
if (socket.readyState === WebSocket.OPEN) {
|
|
612
|
-
send(socket, {
|
|
613
|
-
code: "agent_catalog_refresh_busy",
|
|
614
|
-
kind: AGENT_CATALOG_REFRESH_FAILED_KIND,
|
|
615
|
-
message: "Another local Agent catalog refresh is already running",
|
|
616
|
-
requestId: command.requestId,
|
|
617
|
-
});
|
|
618
|
-
}
|
|
619
|
-
return;
|
|
620
|
-
}
|
|
621
|
-
const controller = new AbortController();
|
|
622
|
-
const operationSignal = AbortSignal.any([connectionController.signal, controller.signal]);
|
|
623
|
-
const timeout = timer.schedule(() => controller.abort(new AcpExecutionError("The local Agent catalog refresh timed out", {}, "agent_catalog_refresh_timeout")), 90_000);
|
|
624
|
-
const completion = (async () => {
|
|
625
|
-
try {
|
|
626
|
-
await refreshAgentCatalog(config, resolveAgentCatalogEnvironment, operationSignal);
|
|
627
|
-
audit.record({
|
|
628
|
-
commandId: command.requestId,
|
|
629
|
-
event: "agent_catalog_refreshed",
|
|
630
|
-
sessionId: "agent-catalog",
|
|
631
|
-
});
|
|
632
|
-
if (socket.readyState === WebSocket.OPEN) {
|
|
633
|
-
send(socket, {
|
|
634
|
-
kind: AGENT_CATALOG_REFRESHED_KIND,
|
|
635
|
-
requestId: command.requestId,
|
|
636
|
-
});
|
|
637
|
-
}
|
|
638
|
-
}
|
|
639
|
-
catch (error) {
|
|
640
|
-
const failure = operationSignal.aborted
|
|
641
|
-
? (controller.signal.reason ??
|
|
642
|
-
new AcpExecutionError("The local Agent catalog refresh was cancelled", {}, "agent_catalog_refresh_cancelled"))
|
|
643
|
-
: error;
|
|
644
|
-
audit.record({
|
|
645
|
-
commandId: command.requestId,
|
|
646
|
-
details: { code: operationErrorCode(failure) },
|
|
647
|
-
event: "agent_catalog_refresh_failed",
|
|
648
|
-
sessionId: "agent-catalog",
|
|
649
|
-
});
|
|
650
|
-
diagnostics.daemonDiagnostic("agent_catalog_refresh_failed", command.requestId, { code: operationErrorCode(failure) }, failure);
|
|
651
|
-
if (socket.readyState === WebSocket.OPEN) {
|
|
652
|
-
send(socket, {
|
|
653
|
-
code: operationErrorCode(failure),
|
|
654
|
-
kind: AGENT_CATALOG_REFRESH_FAILED_KIND,
|
|
655
|
-
message: operationErrorMessage(failure),
|
|
656
|
-
requestId: command.requestId,
|
|
657
|
-
});
|
|
658
|
-
}
|
|
659
|
-
}
|
|
660
|
-
finally {
|
|
661
|
-
timeout.cancel();
|
|
662
|
-
agentCatalogRefreshes.delete(command.requestId);
|
|
663
|
-
}
|
|
664
|
-
})();
|
|
665
|
-
agentCatalogRefreshes.set(command.requestId, { completion, controller });
|
|
666
|
-
return;
|
|
667
|
-
}
|
|
668
|
-
if (kind === NATIVE_SESSION_REQUEST_KIND) {
|
|
669
|
-
const command = message;
|
|
670
|
-
const previous = [...nativeRequests.values()].filter((request) => request.agentId === command.agentId);
|
|
671
|
-
if (nativeRequests.has(command.requestId) ||
|
|
672
|
-
nativeRequests.size >= 4 ||
|
|
673
|
-
(command.action !== "linked-history" &&
|
|
674
|
-
command.action !== "active-turn" &&
|
|
675
|
-
previous.some((request) => !request.controller.signal.aborted))) {
|
|
676
|
-
reportOperationFailure(socket, command.requestId, command.sessionId, new AcpExecutionError("Another native session operation is running", {}, "native_session_busy"));
|
|
677
|
-
return;
|
|
678
|
-
}
|
|
679
|
-
const controller = new AbortController();
|
|
680
|
-
const operationSignal = AbortSignal.any([connectionController.signal, controller.signal]);
|
|
681
|
-
const timeout = timer.schedule(() => controller.abort(new AcpExecutionError("Native session operation timed out", {}, "native_session_timeout")), 90_000);
|
|
682
|
-
const completion = (async () => {
|
|
683
|
-
try {
|
|
684
|
-
operationSignal.throwIfAborted();
|
|
685
|
-
if (previous.length > 0 && command.action !== "active-turn") {
|
|
686
|
-
await new Promise((resolve, reject) => {
|
|
687
|
-
const cancel = () => reject(operationSignal.reason);
|
|
688
|
-
operationSignal.addEventListener("abort", cancel, {
|
|
689
|
-
once: true,
|
|
690
|
-
});
|
|
691
|
-
void Promise.all(previous.map((request) => request.completion)).then(() => {
|
|
692
|
-
operationSignal.removeEventListener("abort", cancel);
|
|
693
|
-
resolve();
|
|
694
|
-
}, (error) => {
|
|
695
|
-
operationSignal.removeEventListener("abort", cancel);
|
|
696
|
-
reject(error);
|
|
697
|
-
});
|
|
698
|
-
});
|
|
699
|
-
}
|
|
700
|
-
operationSignal.throwIfAborted();
|
|
701
|
-
const result = await nativeSessions.execute(command, operationSignal);
|
|
702
|
-
if (socket.readyState === WebSocket.OPEN) {
|
|
703
|
-
send(socket, result);
|
|
704
|
-
}
|
|
705
|
-
}
|
|
706
|
-
catch (error) {
|
|
707
|
-
const failure = operationSignal.aborted
|
|
708
|
-
? (controller.signal.reason ??
|
|
709
|
-
new AcpExecutionError("Native session operation cancelled", {}, "native_session_cancelled"))
|
|
710
|
-
: error;
|
|
711
|
-
audit.record({
|
|
712
|
-
commandId: command.requestId,
|
|
713
|
-
sessionId: command.sessionId,
|
|
714
|
-
event: "native_session_operation_failed",
|
|
715
|
-
details: { code: operationErrorCode(failure) },
|
|
716
|
-
});
|
|
717
|
-
diagnostics.daemonDiagnostic("native_session_operation_failed", command.requestId, {
|
|
718
|
-
action: command.action,
|
|
719
|
-
agentId: command.agentId,
|
|
720
|
-
code: operationErrorCode(failure),
|
|
721
|
-
sessionId: command.sessionId,
|
|
722
|
-
}, failure);
|
|
723
|
-
reportOperationFailure(socket, command.requestId, command.sessionId, failure);
|
|
724
|
-
}
|
|
725
|
-
finally {
|
|
726
|
-
timeout.cancel();
|
|
727
|
-
nativeRequests.delete(command.requestId);
|
|
728
|
-
}
|
|
729
|
-
})();
|
|
730
|
-
nativeRequests.set(command.requestId, {
|
|
731
|
-
agentId: command.agentId,
|
|
732
|
-
sessionId: command.sessionId,
|
|
733
|
-
controller,
|
|
734
|
-
completion,
|
|
735
|
-
});
|
|
288
|
+
handleCatalogRefresh.handle(message);
|
|
736
289
|
return;
|
|
737
290
|
}
|
|
738
|
-
if (kind === NATIVE_SESSION_TURN_FORK_KIND) {
|
|
739
|
-
|
|
740
|
-
const previous = [...nativeRequests.values()].filter((request) => request.agentId === command.agentId);
|
|
741
|
-
if (nativeRequests.has(command.requestId) ||
|
|
742
|
-
nativeRequests.size >= 4 ||
|
|
743
|
-
previous.some((request) => !request.controller.signal.aborted)) {
|
|
744
|
-
reportOperationFailure(socket, command.requestId, command.sessionId, new AcpExecutionError("Another native session operation is running", {}, "native_session_busy"));
|
|
745
|
-
return;
|
|
746
|
-
}
|
|
747
|
-
const controller = new AbortController();
|
|
748
|
-
const operationSignal = AbortSignal.any([connectionController.signal, controller.signal]);
|
|
749
|
-
const timeout = timer.schedule(() => controller.abort(new AcpExecutionError("Native session operation timed out", {}, "native_session_timeout")), 90_000);
|
|
750
|
-
const completion = (async () => {
|
|
751
|
-
try {
|
|
752
|
-
operationSignal.throwIfAborted();
|
|
753
|
-
if (previous.length > 0) {
|
|
754
|
-
await new Promise((resolve, reject) => {
|
|
755
|
-
const cancel = () => reject(operationSignal.reason);
|
|
756
|
-
operationSignal.addEventListener("abort", cancel, { once: true });
|
|
757
|
-
void Promise.all(previous.map((request) => request.completion)).then(() => {
|
|
758
|
-
operationSignal.removeEventListener("abort", cancel);
|
|
759
|
-
resolve();
|
|
760
|
-
}, (error) => {
|
|
761
|
-
operationSignal.removeEventListener("abort", cancel);
|
|
762
|
-
reject(error);
|
|
763
|
-
});
|
|
764
|
-
});
|
|
765
|
-
}
|
|
766
|
-
operationSignal.throwIfAborted();
|
|
767
|
-
const result = await nativeSessions.executeTurnFork(command, operationSignal);
|
|
768
|
-
if (socket.readyState === WebSocket.OPEN) {
|
|
769
|
-
send(socket, result);
|
|
770
|
-
}
|
|
771
|
-
}
|
|
772
|
-
catch (error) {
|
|
773
|
-
const failure = operationSignal.aborted
|
|
774
|
-
? (controller.signal.reason ??
|
|
775
|
-
new AcpExecutionError("Native session operation cancelled", {}, "native_session_cancelled"))
|
|
776
|
-
: error;
|
|
777
|
-
audit.record({
|
|
778
|
-
commandId: command.requestId,
|
|
779
|
-
details: { code: operationErrorCode(failure) },
|
|
780
|
-
event: "native_session_turn_fork_failed",
|
|
781
|
-
sessionId: command.sessionId,
|
|
782
|
-
});
|
|
783
|
-
diagnostics.daemonDiagnostic("native_session_turn_fork_failed", command.requestId, {
|
|
784
|
-
agentId: command.agentId,
|
|
785
|
-
code: operationErrorCode(failure),
|
|
786
|
-
sessionId: command.sessionId,
|
|
787
|
-
}, failure);
|
|
788
|
-
reportOperationFailure(socket, command.requestId, command.sessionId, failure);
|
|
789
|
-
}
|
|
790
|
-
finally {
|
|
791
|
-
timeout.cancel();
|
|
792
|
-
nativeRequests.delete(command.requestId);
|
|
793
|
-
}
|
|
794
|
-
})();
|
|
795
|
-
nativeRequests.set(command.requestId, {
|
|
796
|
-
agentId: command.agentId,
|
|
797
|
-
completion,
|
|
798
|
-
controller,
|
|
799
|
-
sessionId: command.sessionId,
|
|
800
|
-
});
|
|
291
|
+
if (kind === NATIVE_SESSION_REQUEST_KIND || kind === NATIVE_SESSION_TURN_FORK_KIND) {
|
|
292
|
+
nativeRequestHandler.handle(message);
|
|
801
293
|
return;
|
|
802
294
|
}
|
|
803
295
|
if (kind === RUNTIME_LIVENESS_READY_KIND) {
|
|
@@ -828,10 +320,11 @@ async function runRelayConnection(config, directories, audit, sessions, installa
|
|
|
828
320
|
const cancellation = message;
|
|
829
321
|
const turn = activeTurns.get(cancellation.commandId);
|
|
830
322
|
if (turn === undefined || !turn.matches(cancellation)) {
|
|
831
|
-
settleAbandonedTurn(publisher,
|
|
323
|
+
settleAbandonedTurn(publisher, operations, audit, cancellation);
|
|
832
324
|
return;
|
|
833
325
|
}
|
|
834
|
-
|
|
326
|
+
const cancellationState = turn.requestCancellation();
|
|
327
|
+
if (cancellationState === "already-requested") {
|
|
835
328
|
return;
|
|
836
329
|
}
|
|
837
330
|
audit.record({
|
|
@@ -839,6 +332,9 @@ async function runRelayConnection(config, directories, audit, sessions, installa
|
|
|
839
332
|
event: "session_cancellation_requested",
|
|
840
333
|
sessionId: cancellation.sessionId,
|
|
841
334
|
});
|
|
335
|
+
if (cancellationState === "pending") {
|
|
336
|
+
return;
|
|
337
|
+
}
|
|
842
338
|
void sessions.cancelPrompt(cancellation.sessionId).catch((error) => {
|
|
843
339
|
console.error("Mobius daemon could not cancel the agent turn", error);
|
|
844
340
|
});
|
|
@@ -912,36 +408,23 @@ async function runRelayConnection(config, directories, audit, sessions, installa
|
|
|
912
408
|
if (activeTurns.has(command.commandId)) {
|
|
913
409
|
return;
|
|
914
410
|
}
|
|
915
|
-
const receipt =
|
|
411
|
+
const receipt = operations.claimExecution(command.commandId, command.sessionId, encoded);
|
|
916
412
|
if (receipt.kind === "duplicate") {
|
|
917
413
|
return;
|
|
918
414
|
}
|
|
919
415
|
const turn = new ActiveDaemonTurn(command, receipt.attemptId);
|
|
920
416
|
activeTurns.set(command.commandId, turn);
|
|
921
|
-
void executePrompt(
|
|
922
|
-
const stream = turn.accept();
|
|
923
|
-
if (command.disposition !== SessionPromptDisposition.Steer) {
|
|
924
|
-
activeAttemptIds.set(command.sessionId, receipt.attemptId);
|
|
925
|
-
}
|
|
926
|
-
return stream;
|
|
927
|
-
}, () => sessionAvailable(command), timer, turn.signal, turn.recordText)
|
|
417
|
+
void executePrompt(operations, publisher, command, config, directories, audit, sessions, receipt.attemptId, turn.accept, (waitSignal) => waitForSessionAvailability(command, waitSignal), timer, turn.signal, turn.recordText, turn.cancellationRequested)
|
|
928
418
|
.catch((error) => {
|
|
929
419
|
console.error("Mobius daemon turn failed", error);
|
|
930
420
|
})
|
|
931
421
|
.finally(() => {
|
|
932
422
|
activeTurns.delete(command.commandId);
|
|
933
|
-
if (activeAttemptIds.get(command.sessionId) === receipt.attemptId) {
|
|
934
|
-
activeAttemptIds.delete(command.sessionId);
|
|
935
|
-
}
|
|
936
423
|
turn.finish();
|
|
937
424
|
});
|
|
938
425
|
}
|
|
939
426
|
catch (error) {
|
|
940
427
|
console.error("Mobius daemon rejected a message", error);
|
|
941
|
-
if (error instanceof RuntimeDeliveryInboxCapacityError) {
|
|
942
|
-
failAndClose(error, 4013, "Runtime delivery queue full", "delivery_backpressure");
|
|
943
|
-
return;
|
|
944
|
-
}
|
|
945
428
|
failAndClose(new Error("Relay sent an invalid protocol message", { cause: error }), 4008, "Invalid protocol message", "protocol");
|
|
946
429
|
}
|
|
947
430
|
};
|
|
@@ -953,7 +436,6 @@ async function runRelayConnection(config, directories, audit, sessions, installa
|
|
|
953
436
|
return;
|
|
954
437
|
}
|
|
955
438
|
publisher.connect(socket);
|
|
956
|
-
resumeRuntimeDeliveries();
|
|
957
439
|
}, (error) => {
|
|
958
440
|
if (finished) {
|
|
959
441
|
return;
|
|
@@ -969,7 +451,6 @@ async function runRelayConnection(config, directories, audit, sessions, installa
|
|
|
969
451
|
turn.abortDaemon();
|
|
970
452
|
}
|
|
971
453
|
publisher.connect(socket);
|
|
972
|
-
resumeRuntimeDeliveries();
|
|
973
454
|
return;
|
|
974
455
|
}
|
|
975
456
|
failAndClose(new Error("Active Agent turn reconciliation failed", { cause: error }), ACTIVE_TURN_RECONCILIATION_CLOSE_CODE, "Active Agent turn reconciliation failed", "reconciliation");
|
|
@@ -982,9 +463,9 @@ async function runRelayConnection(config, directories, audit, sessions, installa
|
|
|
982
463
|
finally {
|
|
983
464
|
connectionController.abort();
|
|
984
465
|
await Promise.all([
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
466
|
+
nativeRequestHandler.settle(),
|
|
467
|
+
handleSettingsDiscovery.settle(),
|
|
468
|
+
handleCatalogRefresh.settle(),
|
|
988
469
|
]);
|
|
989
470
|
}
|
|
990
471
|
}
|
|
@@ -1001,17 +482,14 @@ export async function runDaemon(config, audit, signal, timer = systemDaemonTimer
|
|
|
1001
482
|
stateDirectory,
|
|
1002
483
|
});
|
|
1003
484
|
const sessionStore = new MemoryAcpSessionStore();
|
|
1004
|
-
const
|
|
1005
|
-
const publisher = new DaemonSessionEventPublisher(
|
|
485
|
+
const operations = new DaemonOperationState();
|
|
486
|
+
const publisher = new DaemonSessionEventPublisher();
|
|
1006
487
|
const activeTurns = new Map();
|
|
1007
|
-
const activeAttemptIds = new Map();
|
|
1008
488
|
const recordSessionEvent = (sessionId, event) => {
|
|
1009
|
-
const
|
|
1010
|
-
|
|
1011
|
-
publisher.recordRuntime(sessionId, event);
|
|
1012
|
-
return;
|
|
489
|
+
for (const turn of activeTurns.values()) {
|
|
490
|
+
turn.recordInteraction(event);
|
|
1013
491
|
}
|
|
1014
|
-
publisher.
|
|
492
|
+
publisher.publish(sessionId, event);
|
|
1015
493
|
};
|
|
1016
494
|
const sessions = new AcpSessionManager(new DaemonAcpObserver(recordSessionEvent), sessionStore, {
|
|
1017
495
|
read: async (options, nativeSessionId) => options.agent.id === "codex"
|
|
@@ -1089,7 +567,7 @@ export async function runDaemon(config, audit, signal, timer = systemDaemonTimer
|
|
|
1089
567
|
while (!signal.aborted) {
|
|
1090
568
|
connectionEpoch += 1;
|
|
1091
569
|
try {
|
|
1092
|
-
await runRelayConnection(config, directories, audit, sessions, installations, installationObserver, nativeSessions,
|
|
570
|
+
await runRelayConnection(config, directories, audit, sessions, installations, installationObserver, nativeSessions, operations, publisher, activeTurns, connectionEpoch, diagnostics, () => {
|
|
1093
571
|
if (catalogPublished) {
|
|
1094
572
|
options.onConnectionStatus?.("online");
|
|
1095
573
|
}
|
|
@@ -1135,7 +613,7 @@ export async function runDaemon(config, audit, signal, timer = systemDaemonTimer
|
|
|
1135
613
|
publisher.close();
|
|
1136
614
|
}
|
|
1137
615
|
finally {
|
|
1138
|
-
|
|
616
|
+
operations.close();
|
|
1139
617
|
}
|
|
1140
618
|
}
|
|
1141
619
|
}
|