@platless/axom-cli 0.3.13 → 0.5.9
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/axom-patch/cli/README.md +30 -8
- package/dist/axom-patch/cli/account-limits/README.md +27 -0
- package/dist/axom-patch/cli/account-limits/antigravity-proxy.mjs +47 -0
- package/dist/axom-patch/cli/account-limits/antigravity-read.mjs +90 -0
- package/dist/axom-patch/cli/account-limits/antigravity.mjs +46 -0
- package/dist/axom-patch/cli/account-limits/generate.py +120 -0
- package/dist/axom-patch/cli/account-limits/kimi-proxy.mjs +64 -0
- package/dist/axom-patch/cli/account-limits/normalize.mjs +151 -0
- package/dist/axom-patch/cli/claude-account-limits.json +37 -0
- package/dist/axom-patch/cli/codex-account-limits.json +37 -0
- package/dist/axom-patch/cli/commandcode-account-limits.json +30 -0
- package/dist/axom-patch/cli/nightly-task.md +152 -0
- package/dist/axom-patch/cli/pi-subagent-subwork.json +23 -0
- package/dist/axom-patch/cli/probes/claude-late-async-terminal.mjs +5 -1
- package/dist/axom-patch/cli/probes/pi-subagent-subwork.mjs +211 -0
- package/dist/axom-patch/cli/probes/qoder-steering.mjs +1 -1
- package/dist/axom-patch/cli/probes/qoder-subagent-origin.mjs +20 -8
- package/dist/axom-patch/cli/probes/zcode-subagent-subwork.mjs +257 -0
- package/dist/axom-patch/cli/qoder-background-terminal.md +2 -1
- package/dist/axom-patch/cli/qoder-foreground-output.md +4 -3
- package/dist/axom-patch/cli/qoder-steering.json +15 -15
- package/dist/axom-patch/cli/qoder-subagent-origin.json +21 -21
- package/dist/axom-patch/cli/runtime.json +34 -8
- package/dist/axom-patch/cli/zcode-account-limits.json +31 -0
- package/dist/axom-patch/cli/zcode-models.json +38 -0
- package/dist/axom-patch/cli/zcode-subagent-subwork.json +73 -0
- package/dist/cli.js +33 -6
- package/dist/keepers/acp/keeper.mjs +3083 -0
- package/dist/keepers/acp/manifest.json +9 -0
- package/dist/keepers/terminal/keeper.mjs +8469 -0
- package/dist/keepers/terminal/manifest.json +9 -0
- package/dist/keepers/terminal/node_modules/node-pty/lib/conpty_console_list_agent.js +25 -0
- package/dist/keepers/terminal/node_modules/node-pty/lib/eventEmitter2.js +47 -0
- package/dist/keepers/terminal/node_modules/node-pty/lib/index.js +52 -0
- package/dist/keepers/terminal/node_modules/node-pty/lib/interfaces.js +7 -0
- package/dist/keepers/terminal/node_modules/node-pty/lib/shared/conout.js +10 -0
- package/dist/keepers/terminal/node_modules/node-pty/lib/terminal.js +190 -0
- package/dist/keepers/terminal/node_modules/node-pty/lib/types.js +7 -0
- package/dist/keepers/terminal/node_modules/node-pty/lib/unixTerminal.js +351 -0
- package/dist/keepers/terminal/node_modules/node-pty/lib/utils.js +38 -0
- package/dist/keepers/terminal/node_modules/node-pty/lib/windowsConoutConnection.js +131 -0
- package/dist/keepers/terminal/node_modules/node-pty/lib/windowsPtyAgent.js +344 -0
- package/dist/keepers/terminal/node_modules/node-pty/lib/windowsTerminal.js +209 -0
- package/dist/keepers/terminal/node_modules/node-pty/lib/worker/conoutSocketWorker.js +22 -0
- package/dist/keepers/terminal/node_modules/node-pty/package.json +66 -0
- package/dist/keepers/terminal/node_modules/node-pty/prebuilds/darwin-arm64/pty.node +0 -0
- package/dist/keepers/terminal/node_modules/node-pty/prebuilds/darwin-arm64/spawn-helper +0 -0
- package/dist/keepers/terminal/node_modules/node-pty/prebuilds/darwin-x64/pty.node +0 -0
- package/dist/keepers/terminal/node_modules/node-pty/prebuilds/darwin-x64/spawn-helper +0 -0
- package/dist/keepers/terminal/node_modules/node-pty/prebuilds/linux-arm64/pty.node +0 -0
- package/dist/keepers/terminal/node_modules/node-pty/prebuilds/linux-x64/pty.node +0 -0
- package/dist/runtime-deps/qoder-sdk/package-lock.json +1189 -0
- package/dist/runtime-deps/qoder-sdk/package.json +8 -0
- package/dist/test/keeperKit.js +67 -0
- package/dist/workstation/acp/acpAgents.js +2163 -611
- package/dist/workstation/acp/acpClient.js +60 -497
- package/dist/workstation/acp/adoption.js +10 -396
- package/dist/workstation/acp/binaryInstall.js +100 -27
- package/dist/workstation/acp/builds/gc.js +245 -0
- package/dist/workstation/acp/builds/install/binary.js +28 -0
- package/dist/workstation/acp/builds/install/npm.js +235 -0
- package/dist/workstation/acp/builds/install/uv.js +254 -0
- package/dist/workstation/acp/builds/migrate.js +195 -0
- package/dist/workstation/acp/builds/model.js +128 -0
- package/dist/workstation/acp/builds/nodeGate.js +100 -0
- package/dist/workstation/acp/builds/store.js +352 -0
- package/dist/workstation/acp/commandTableProbe.js +68 -0
- package/dist/workstation/acp/configOptions.js +103 -1
- package/dist/workstation/acp/envSecrets.js +27 -0
- package/dist/workstation/acp/initializeResponse.js +285 -0
- package/dist/workstation/acp/installErrors.js +12 -2
- package/dist/workstation/acp/installProbe.js +1 -1
- package/dist/workstation/acp/managedPatches.js +76 -160
- package/dist/workstation/acp/outbox.js +330 -0
- package/dist/workstation/acp/patchAssets.js +20 -3
- package/dist/workstation/acp/permission.js +42 -10
- package/dist/workstation/acp/processor/checkpoint.js +87 -0
- package/dist/workstation/acp/processor/commands.js +35 -0
- package/dist/workstation/acp/processor/control.js +355 -0
- package/dist/workstation/acp/processor/emit.js +26 -0
- package/dist/workstation/acp/processor/jsonrpc.js +172 -0
- package/dist/workstation/acp/processor/memoryLog.js +49 -0
- package/dist/workstation/acp/processor/origins.js +48 -0
- package/dist/workstation/acp/processor/output.js +67 -0
- package/dist/workstation/acp/processor/projection.js +42 -0
- package/dist/workstation/acp/processor/record.js +128 -0
- package/dist/workstation/acp/processor/runtime.js +181 -0
- package/dist/workstation/acp/processor/sdkSchemas.js +61 -0
- package/dist/workstation/acp/processor/session.js +525 -0
- package/dist/workstation/acp/processor/sideChannel.js +80 -0
- package/dist/workstation/acp/processor/state.js +74 -0
- package/dist/workstation/acp/processor/step.js +459 -0
- package/dist/workstation/acp/processor/test/fixtureLog.js +65 -0
- package/dist/workstation/acp/processor/test/harness.js +120 -0
- package/dist/workstation/acp/processor/test/largeIdEcho.js +50 -0
- package/dist/workstation/acp/processor/test/rawAgent.js +70 -0
- package/dist/workstation/acp/processor/transport/generations.js +76 -0
- package/dist/workstation/acp/processor/transport/keeper.js +130 -0
- package/dist/workstation/acp/processor/transport/local.js +269 -0
- package/dist/workstation/acp/processor/transport/types.js +78 -0
- package/dist/workstation/acp/processor/updates.js +364 -0
- package/dist/workstation/acp/processorSession.js +1512 -0
- package/dist/workstation/acp/qoderStartup.js +1 -1
- package/dist/workstation/acp/registry/catalog.js +154 -32
- package/dist/workstation/acp/registry/launch.js +61 -119
- package/dist/workstation/acp/sessionUpdateMapper.js +18 -7
- package/dist/workstation/acp/steering.js +58 -0
- package/dist/workstation/acp/switching.js +114 -0
- package/dist/workstation/acp/terminalLogin.js +16 -42
- package/dist/workstation/acp/terminalLoginSpec.js +41 -0
- package/dist/workstation/acp/test/adapterFixtures.js +256 -0
- package/dist/workstation/acp/test/daemonLife.js +106 -0
- package/dist/workstation/acp/test/keeperWorld.js +267 -0
- package/dist/workstation/acp/test/memoryOutbox.js +55 -0
- package/dist/workstation/acp/test/raw/fixtures.js +196 -0
- package/dist/workstation/acp/test/raw/qoderCaptures.js +125 -0
- package/dist/workstation/acp/test/raw/sanitize.js +344 -0
- package/dist/workstation/acp/test/raw/sources.js +249 -0
- package/dist/workstation/acp/test/realAdapter.js +377 -0
- package/dist/workstation/acp/test/scriptAgent.js +206 -0
- package/dist/workstation/acp/test/sessionFrames.js +42 -0
- package/dist/workstation/acp/timedCommand.js +4 -2
- package/dist/workstation/acp/uvxInstall.js +72 -122
- package/dist/workstation/agentFlow.js +103 -0
- package/dist/workstation/agentSessions.js +1324 -474
- package/dist/workstation/channels/agent.js +7 -5
- package/dist/workstation/channels/computer.js +69 -21
- package/dist/workstation/channels/computerExecs.js +200 -0
- package/dist/workstation/channels/management.js +84 -4
- package/dist/workstation/daemon.js +288 -7
- package/dist/workstation/daemonctl.js +28 -2
- package/dist/workstation/exec/attach.js +126 -0
- package/dist/workstation/exec/command.js +40 -0
- package/dist/workstation/exec/durable.js +517 -0
- package/dist/workstation/exec/fallback.js +106 -0
- package/dist/workstation/exec/policy.js +837 -0
- package/dist/workstation/exec/shaping.js +211 -0
- package/dist/workstation/exec.js +3 -112
- package/dist/workstation/files/adapters/computerFileFrames.js +51 -0
- package/dist/workstation/files/adapters/toolRequests.js +121 -0
- package/dist/workstation/files/client.js +455 -0
- package/dist/workstation/files/errors.js +28 -0
- package/dist/workstation/files/fence.js +350 -0
- package/dist/workstation/files/find/fileIndex.js +153 -0
- package/dist/workstation/files/find/fuzzy.js +155 -0
- package/dist/workstation/files/host.js +122 -0
- package/dist/workstation/files/hostProtocol.js +2 -0
- package/dist/workstation/files/media/cache.js +620 -0
- package/dist/workstation/files/media/encoding.js +308 -0
- package/dist/workstation/files/media/estimator.js +100 -0
- package/dist/workstation/files/media/media.js +325 -0
- package/dist/workstation/files/media/packedAudio.js +23 -0
- package/dist/workstation/files/media/scheduler.js +551 -0
- package/dist/workstation/files/media/types.js +5 -0
- package/dist/workstation/files/scope.js +45 -0
- package/dist/workstation/files/structured.js +284 -0
- package/dist/workstation/{fileOps.js → files/text.js} +117 -38
- package/dist/workstation/files/uploads.js +351 -0
- package/dist/workstation/journal.js +65 -236
- package/dist/workstation/keepers/acp/client.js +101 -0
- package/dist/workstation/keepers/acp/envelope.js +62 -0
- package/dist/workstation/keepers/acp/host.js +419 -0
- package/dist/workstation/keepers/acp/main.js +18 -0
- package/dist/workstation/keepers/acp/memoryStream.js +121 -0
- package/dist/workstation/keepers/acp/params.js +137 -0
- package/dist/workstation/keepers/acp/protocol.js +66 -0
- package/dist/workstation/keepers/acp/recover.js +120 -0
- package/dist/workstation/keepers/acp/session.js +584 -0
- package/dist/workstation/keepers/acp/stderr.js +154 -0
- package/dist/workstation/keepers/kit/activity.js +103 -0
- package/dist/workstation/keepers/kit/bootstrap.js +164 -0
- package/dist/workstation/keepers/kit/buildVersion.js +51 -0
- package/dist/workstation/keepers/kit/bundle.js +120 -0
- package/dist/workstation/keepers/kit/client.js +432 -0
- package/dist/workstation/keepers/kit/identity.js +21 -0
- package/dist/workstation/keepers/kit/ipc.js +112 -0
- package/dist/workstation/keepers/kit/launchConfig.js +8 -0
- package/dist/workstation/keepers/kit/launcher.js +249 -0
- package/dist/workstation/keepers/kit/lock.js +123 -0
- package/dist/workstation/keepers/kit/orphanSweep.js +86 -0
- package/dist/workstation/keepers/kit/paths.js +75 -0
- package/dist/workstation/keepers/kit/protocol.js +43 -0
- package/dist/workstation/keepers/kit/server.js +518 -0
- package/dist/workstation/keepers/kit/streamLog.js +697 -0
- package/dist/workstation/keepers/legacy.js +296 -0
- package/dist/workstation/keepers/policy.js +576 -0
- package/dist/workstation/keepers/stop.js +34 -0
- package/dist/workstation/keepers/terminal/client.js +213 -0
- package/dist/workstation/keepers/terminal/exec.js +981 -0
- package/dist/workstation/keepers/terminal/generations.js +129 -0
- package/dist/workstation/keepers/terminal/keeper.js +62 -0
- package/dist/workstation/keepers/terminal/main.js +43 -0
- package/dist/workstation/keepers/terminal/orphanSweep.js +3 -0
- package/dist/workstation/keepers/terminal/params.js +27 -0
- package/dist/workstation/keepers/terminal/protocol.js +56 -0
- package/dist/workstation/keepers/terminal/pty.js +330 -0
- package/dist/workstation/protocol/accountLimits.js +47 -0
- package/dist/workstation/protocol/agent.js +48 -4
- package/dist/workstation/protocol/common.js +30 -0
- package/dist/workstation/protocol/computer.js +15 -4
- package/dist/workstation/protocol/management.js +50 -2
- package/dist/workstation/qoderAdapter/backgroundProcess.js +18 -6
- package/dist/workstation/qoderAdapter/backgroundTerminal.js +78 -147
- package/dist/workstation/qoderAdapter/backgroundTerminalFold.js +372 -0
- package/dist/workstation/qoderAdapter/callbackContext.js +76 -0
- package/dist/workstation/qoderAdapter/foregroundOutput.js +15 -9
- package/dist/workstation/qoderAdapter/history.js +1 -3
- package/dist/workstation/qoderAdapter/main.js +4 -2
- package/dist/workstation/qoderAdapter/sdkDriver.js +1 -2
- package/dist/workstation/qoderAdapter/sdkRuntime.js +93 -0
- package/dist/workstation/qoderAdapter/sdkRuntimeInstall.js +145 -0
- package/dist/workstation/qoderAdapter/session.js +1 -1
- package/dist/workstation/qoderAdapter/subwork.js +15 -7
- package/dist/workstation/systemMetrics.js +110 -1
- package/dist/workstation/tools/client.js +9 -1
- package/dist/workstation/tools/info.js +50 -0
- package/dist/workstation/tools/keeperProcess.js +15 -5
- package/dist/workstation/tools/router.js +55 -0
- package/dist/workstation/tools/terminalRouter.js +222 -0
- package/dist/workstation/tools/validation.js +37 -1
- package/dist/workstation/update/coordinator.js +3 -0
- package/dist/workstation/update/ptyPreflight.js +21 -0
- package/dist/workstation/update/taskRegistry.js +105 -11
- package/package.json +10 -7
- package/dist/workstation/acp/acpSession.js +0 -1475
- package/dist/workstation/acp/contract.js +0 -499
- package/dist/workstation/acp/draftInterceptor.js +0 -163
- package/dist/workstation/acp/sessionScope.js +0 -19
- package/dist/workstation/filePaths.js +0 -174
- package/dist/workstation/fileWorker.js +0 -81
- package/dist/workstation/fileWorkerClient.js +0 -121
- package/dist/workstation/qoderAdapter/taskEvent.js +0 -26
- package/dist/workstation/tools/core.js +0 -50
- package/dist/workstation/tools/files.js +0 -525
- package/dist/workstation/tools/terminal.js +0 -214
|
@@ -1,13 +1,16 @@
|
|
|
1
|
-
import fs from 'node:fs/promises';
|
|
2
1
|
import path from 'node:path';
|
|
3
2
|
import { createHash, randomUUID } from 'node:crypto';
|
|
4
|
-
import { AGENT_WIRE_AUTH_URL_VERSION, AGENT_WIRE_HEARTBEAT_INVENTORY_VERSION, AGENT_WIRE_SCOPED_ORIGIN_VERSION, AGENT_WIRE_ORPHAN_OFFER_VERSION, AGENT_WIRE_SESSION_AUTH_METHODS_VERSION, AGENT_WIRE_SESSION_LOST_VERSION,
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
3
|
+
import { AGENT_WIRE_AUTH_URL_VERSION, AGENT_WIRE_HEARTBEAT_INVENTORY_VERSION, AGENT_WIRE_SCOPED_ORIGIN_VERSION, AGENT_WIRE_ORPHAN_OFFER_VERSION, AGENT_WIRE_SESSION_AUTH_METHODS_VERSION, AGENT_WIRE_SESSION_LOST_VERSION, AGENT_WIRE_VERSION, AGENT_WIRE_DELEGATION_MCP_VERSION, AGENT_WIRE_DELEGATION_RENEW_VERSION, AGENT_WIRE_PROVIDER_TASK_VERSION, AGENT_WIRE_REOPEN_REQUEST_VERSION, AGENT_WIRE_RELATION_ACK_VERSION, outboundAgentFrameRejection, outboundFailureReply, } from './protocol/agent.js';
|
|
4
|
+
import { listLegacyJournalRefs, readLegacyJournal, removeLegacyJournal } from './journal.js';
|
|
5
|
+
import { SESSION_WINDOW_BYTES, SESSION_WINDOW_EVENTS } from './acp/outbox.js';
|
|
6
|
+
import { SocketGate } from './agentFlow.js';
|
|
7
|
+
import { AGENT_PROBE_TIMEOUT_MS, AgentRefusedSessionError, LOCAL_SESSION_UNMOVABLE } from './acp/acpAgents.js';
|
|
8
|
+
import { AdapterStillInstallingError } from './acp/builds/model.js';
|
|
9
|
+
import { projectFrame } from './acp/processor/projection.js';
|
|
8
10
|
import { reportsActivity, supportsAsyncTaskStop } from './acp/activityReporting.js';
|
|
9
11
|
import { activityIsBusy } from './acp/activityBusy.js';
|
|
10
12
|
import { buildSessionMcpServers } from './acp/delegationMcp.js';
|
|
13
|
+
import { ADAPTER_LOST_REOPEN_ATTEMPTS, plainSwitchBlocker, REOPEN_REQUEST_RETRY_MS, switchBlockers, SWITCH_QUIET_MS } from './acp/switching.js';
|
|
11
14
|
function delegationLeaseId(lease) {
|
|
12
15
|
return createHash('sha256').update(JSON.stringify([
|
|
13
16
|
lease.url, lease.headers, lease.bindingId,
|
|
@@ -109,14 +112,87 @@ function resumeFromSeqOf(frame) {
|
|
|
109
112
|
const seq = Math.trunc(frame.resumeFromSeq ?? 0);
|
|
110
113
|
return Number.isFinite(seq) ? Math.max(0, seq) : 0;
|
|
111
114
|
}
|
|
115
|
+
/**
|
|
116
|
+
* What `agent_open` asks of {@link AcpAgents.open} (and of a switch, which needs the same fresh spawn environment — spec §6.3).
|
|
117
|
+
*
|
|
118
|
+
* `env` is an OVERLAY merged onto process.env when the agent is spawned (see AcpOpenSpec.env), so it is built from frame-supplied
|
|
119
|
+
* values only. The single exception is PATH: when pathPrepend is set the whole variable is resolved here — prepends first, then the
|
|
120
|
+
* frame's PATH or, failing that, the daemon's — so the launcher uses spec.env.PATH verbatim instead of re-deriving it.
|
|
121
|
+
*/
|
|
122
|
+
function openSpecOf(frame) {
|
|
123
|
+
const env = { ...(frame.env ?? {}) };
|
|
124
|
+
const pathPrepend = frame.pathPrepend ?? [];
|
|
125
|
+
if (pathPrepend.length > 0) {
|
|
126
|
+
env.PATH = [...pathPrepend, env.PATH ?? process.env.PATH ?? ''].filter(Boolean).join(path.delimiter);
|
|
127
|
+
}
|
|
128
|
+
return {
|
|
129
|
+
openRequestId: frame.id,
|
|
130
|
+
sessionRef: frame.sessionRef,
|
|
131
|
+
agentKey: frame.agentKey,
|
|
132
|
+
...(frame.delegationMcp ? { delegation: {
|
|
133
|
+
leaseId: delegationLeaseId(frame.delegationMcp),
|
|
134
|
+
...(frame.delegationMcp.bindingId ? { bindingId: frame.delegationMcp.bindingId, instanceId: randomUUID() } : {}),
|
|
135
|
+
} } : {}),
|
|
136
|
+
// `frame.bridge` is the registry agent id now — the wire field keeps its name because Plan A
|
|
137
|
+
// renames nothing (Global Constraint 3); ruling R2 replaced what it *means*.
|
|
138
|
+
agentId: frame.bridge,
|
|
139
|
+
cwd: frame.cwd,
|
|
140
|
+
env,
|
|
141
|
+
agentEnv: frame.agentEnv ?? {},
|
|
142
|
+
...(frame.command ? { command: frame.command } : {}),
|
|
143
|
+
args: frame.args ?? [],
|
|
144
|
+
...(frame.nativeSessionId ? { nativeSessionId: frame.nativeSessionId } : {}),
|
|
145
|
+
...(frame.requireSessionLoad ? { requireSessionLoad: true } : {}),
|
|
146
|
+
...(frame.delegationMcp ? { delegationMcp: frame.delegationMcp } : {}),
|
|
147
|
+
// Retaining a whole adopted history costs memory for the session's lifetime, so it is only
|
|
148
|
+
// kept when this open is the one that will stream it back.
|
|
149
|
+
backfill: frame.backfill === true,
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
/** A recorded frame as the processor makes it: the projection adds `sessionRef` (right after `t`) when it is sent. */
|
|
153
|
+
function withoutSessionRef(frame) {
|
|
154
|
+
const { sessionRef: _ref, ...rest } = frame;
|
|
155
|
+
return rest;
|
|
156
|
+
}
|
|
157
|
+
/** How an adapter exited, for the daemon log and the crash-limit refusal: code or signal, and the last stderr line. */
|
|
158
|
+
function exitWords(exit) {
|
|
159
|
+
const status = exit.signal ? `signal ${exit.signal}` : exit.code !== null ? `exit code ${exit.code}` : 'no exit status';
|
|
160
|
+
const lastLine = exit.stderrTail.trim().split('\n').at(-1)?.trim().slice(0, 200);
|
|
161
|
+
return lastLine ? `${status}; stderr: ${lastLine}` : status;
|
|
162
|
+
}
|
|
112
163
|
function errorMessage(err) {
|
|
113
164
|
return err instanceof Error ? err.message : String(err);
|
|
114
165
|
}
|
|
166
|
+
/**
|
|
167
|
+
* The reply text of an open that failed. A managed (delegation) open hides the cause behind one fixed sentence — except when
|
|
168
|
+
* the adapter is still being installed: that one is actionable ("try again shortly") and carries nothing about the lease (15c).
|
|
169
|
+
*/
|
|
170
|
+
function openFailureText(frame, err) {
|
|
171
|
+
return frame.delegationMcp && !(err instanceof AdapterStillInstallingError) ? 'Managed delegation session could not open' : errorMessage(err);
|
|
172
|
+
}
|
|
115
173
|
/**
|
|
116
174
|
* Batch size for `agent_backfill_events`. Kept modest so a session with a long history does not
|
|
117
175
|
* arrive as one giant frame; the last batch of a stream is whatever remains below this size.
|
|
118
176
|
*/
|
|
119
177
|
export const BACKFILL_BATCH_SIZE = 64;
|
|
178
|
+
/**
|
|
179
|
+
* How long the reconnect offer waits for the start-time adoption (Task 21, spec §5.5.2: rebuild before the agent channel is
|
|
180
|
+
* answered). Sessions still being rebuilt after it are offered without `turnRunning` — never as missing, which would end their turns.
|
|
181
|
+
*/
|
|
182
|
+
export const ADOPTION_OFFER_WAIT_MS = 10_000;
|
|
183
|
+
/**
|
|
184
|
+
* Own exits of a ref's adapter (no keeper-acp reason: it crashed, or was killed outside Axom) in a row, with no turn completing in
|
|
185
|
+
* between, past which a respawn is no longer made at once (spec §11 "single adapter crash": `session/load` recovers it — but an
|
|
186
|
+
* adapter that dies on every respawn must not be respawned for ever). From the limit on, an `agent_open` that would respawn it is
|
|
187
|
+
* refused with the reason until {@link ADAPTER_CRASH_BACKOFF_MS} has passed since the last exit, doubling per exit past the limit up
|
|
188
|
+
* to {@link ADAPTER_CRASH_BACKOFF_MAX_MS}. A completed turn, or {@link ADAPTER_CRASH_FORGET_MS} without an exit, starts the count over.
|
|
189
|
+
*/
|
|
190
|
+
export const ADAPTER_CRASH_LIMIT = 3;
|
|
191
|
+
export const ADAPTER_CRASH_BACKOFF_MS = 30_000;
|
|
192
|
+
export const ADAPTER_CRASH_BACKOFF_MAX_MS = 10 * 60_000;
|
|
193
|
+
export const ADAPTER_CRASH_FORGET_MS = 30 * 60_000;
|
|
194
|
+
/** At most one daemon-log line per ref per this period for refused respawns. */
|
|
195
|
+
const ADAPTER_CRASH_WARN_EVERY_MS = 60_000;
|
|
120
196
|
/**
|
|
121
197
|
* 一条会话自报 `running` 之后多久没有任何新推送,就值得在日志里点名。
|
|
122
198
|
*
|
|
@@ -154,19 +230,6 @@ function sanitizeNativeSessionQuery(agentId, query) {
|
|
|
154
230
|
args: query.args ?? [],
|
|
155
231
|
};
|
|
156
232
|
}
|
|
157
|
-
/**
|
|
158
|
-
* Name an event for a diagnostic without trusting it: on a payload hostile enough that even
|
|
159
|
-
* `eventName` throws (the validator's 'event not inspectable' case), the log line itself must
|
|
160
|
-
* still come out.
|
|
161
|
-
*/
|
|
162
|
-
function wireEventName(e) {
|
|
163
|
-
try {
|
|
164
|
-
return String(e?.eventName);
|
|
165
|
-
}
|
|
166
|
-
catch {
|
|
167
|
-
return '<uninspectable>';
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
233
|
/**
|
|
171
234
|
* Owns the workstation's live external-agent sessions and translates between the tunnel's
|
|
172
235
|
* `agent_*` frames and the ACP client.
|
|
@@ -175,14 +238,17 @@ function wireEventName(e) {
|
|
|
175
238
|
* (and the subprocesses behind them) must survive a dropped websocket. The send binding is swapped
|
|
176
239
|
* per connection via {@link bindSend} / {@link closeAll}.
|
|
177
240
|
*
|
|
178
|
-
* Delivery is at-least-once, not best-effort: every outbound event is
|
|
179
|
-
*
|
|
180
|
-
* entirely — is
|
|
241
|
+
* Delivery is at-least-once, not best-effort: every outbound event is recorded in the session's outbox (the transport's outbox
|
|
242
|
+
* stream, Task 20 — it replaced the daemon journal) before it is sent, so a send that goes nowhere — socket down, or the daemon
|
|
243
|
+
* restarted entirely — is resent when the server re-opens the ref with its `resumeFromSeq` watermark.
|
|
244
|
+
*
|
|
245
|
+
* Sending is flow-controlled (spec §5.6): a session's numbered frames go out from its outbox while fewer than
|
|
246
|
+
* {@link SESSION_WINDOW_EVENTS} events / {@link SESSION_WINDOW_BYTES} bytes are unacknowledged on the connection and the socket
|
|
247
|
+
* gate is open ({@link SocketGate}); sessions take turns. Control frames are written at once ({@link emit}).
|
|
181
248
|
*/
|
|
182
249
|
export class AgentSessionManager {
|
|
183
250
|
agents;
|
|
184
251
|
journalDir;
|
|
185
|
-
maxJournalBytes;
|
|
186
252
|
tasks;
|
|
187
253
|
sessions = new Map();
|
|
188
254
|
/** In-flight opens, keyed by sessionRef, so two racing agent_open frames cannot spawn two agents. */
|
|
@@ -193,21 +259,19 @@ export class AgentSessionManager {
|
|
|
193
259
|
* fails its pending open with ok:false and will never adopt the result). Without this, the native
|
|
194
260
|
* agent that finishes spawning afterwards is orphaned: alive, registered, and unknown to the
|
|
195
261
|
* server. {@link openSession} closes and discards any session whose ref is tombstoned, and uses
|
|
196
|
-
* the recorded {@link AbandonReason} to decide whether
|
|
262
|
+
* the recorded {@link AbandonReason} to decide whether its outbox dies with it.
|
|
197
263
|
*/
|
|
198
264
|
closedDuringOpen = new Map();
|
|
199
265
|
/**
|
|
200
|
-
* Refs whose
|
|
266
|
+
* Refs whose session is still being torn down (adapter stopped, outbox flushed and kept or released), and the promise that says
|
|
267
|
+
* when it is done.
|
|
201
268
|
*
|
|
202
269
|
* A close and the reopen of the same ref are two independent inbound frames — the daemon does not
|
|
203
270
|
* serialize `handleFrame` (see daemon.ts's `ws.on('message', async …)`) — and `onClose` returns the
|
|
204
|
-
* ref to the "openable" state the moment it drops out of `sessions`, while the actual
|
|
205
|
-
*
|
|
206
|
-
*
|
|
207
|
-
*
|
|
208
|
-
* `init()` can read a file the old `destroy()` then removes, leaving the new journal with a stale
|
|
209
|
-
* size and a seq counter recovered from bytes that no longer exist. The two instances also share a
|
|
210
|
-
* `.tmp` path, so a rewrite in one can be pulled out from under the other.
|
|
271
|
+
* ref to the "openable" state the moment it drops out of `sessions`, while the actual teardown is
|
|
272
|
+
* still running. The reopen spawns under the same transport key: letting it start in that window
|
|
273
|
+
* would race the old release against the new session's outbox (deleting it under it) or read the
|
|
274
|
+
* outbox back while the old session's last appends are still landing.
|
|
211
275
|
*
|
|
212
276
|
* {@link openSession} waits on this before constructing anything.
|
|
213
277
|
*/
|
|
@@ -250,33 +314,110 @@ export class AgentSessionManager {
|
|
|
250
314
|
* running for a card nobody will ever see.
|
|
251
315
|
*/
|
|
252
316
|
loginsInFlight = new Set();
|
|
317
|
+
/** The socket gate of the bound connection (spec §5.6). */
|
|
318
|
+
gate = new SocketGate();
|
|
319
|
+
/** Bumped per bound connection: a session's window counts what was sent on this one. */
|
|
320
|
+
epoch = 0;
|
|
321
|
+
/** Round-robin start for the sender. */
|
|
322
|
+
turn = 0;
|
|
323
|
+
pumping = false;
|
|
324
|
+
pumpAgain = false;
|
|
325
|
+
/** The one-time migration of the legacy daemon journal ({@link migrateLegacyJournals}). */
|
|
326
|
+
legacyMigration;
|
|
327
|
+
/** The start-time adoption of keeper-acp sessions ({@link adoptSessions}): resolves once they are all registered (or given up). */
|
|
328
|
+
adoption;
|
|
329
|
+
/** A retry of the adoption in flight ({@link adoptLate}): resolves once what it found is tracked in {@link adopting}. */
|
|
330
|
+
lateAdoption;
|
|
331
|
+
/** Refs found running in keeper-acp and still being rebuilt: what the offer reports for them, and when they are done. */
|
|
332
|
+
adopting = new Map();
|
|
333
|
+
/**
|
|
334
|
+
* Refs this daemon detached (spec §5.5.5): the session now belongs to the next daemon. A frame for one is dropped quietly — telling
|
|
335
|
+
* the server the session is lost would end the turn the next daemon continues.
|
|
336
|
+
*/
|
|
337
|
+
detached = new Set();
|
|
338
|
+
/**
|
|
339
|
+
* Refs being switched (spec §6.3, Task 24) and the promise that says when the switch (and its `agent_opened`) is done: every other
|
|
340
|
+
* inbound frame of the ref is parked behind it and delivered in order afterwards — to the session that then holds the ref.
|
|
341
|
+
*/
|
|
342
|
+
switching = new Map();
|
|
343
|
+
/** When a reopen was last requested for a ref, and for what (a switch, or an adapter keeper-acp lost): not asked again too soon. */
|
|
344
|
+
reopenRequests = new Map();
|
|
345
|
+
/** The quiet period a session must keep before it is switched (spec §14; a test seam). */
|
|
346
|
+
switchQuietMs = SWITCH_QUIET_MS;
|
|
347
|
+
/** Own adapter exits per ref ({@link ADAPTER_CRASH_LIMIT}): how many in a row, the last one's time and words. */
|
|
348
|
+
adapterCrashes = new Map();
|
|
349
|
+
/** {@link ADAPTER_CRASH_BACKOFF_MS} (a test seam). */
|
|
350
|
+
adapterCrashBackoffMs = ADAPTER_CRASH_BACKOFF_MS;
|
|
351
|
+
/** `agent_open`s being handled per ref, from admission to their reply: a lost notice waits for them ({@link flushAdapterLost}). */
|
|
352
|
+
openHandling = new Map();
|
|
253
353
|
constructor(
|
|
254
354
|
/** The one ACP client every external agent runs through; keyed per call by registry agent id. */
|
|
255
355
|
agents, send,
|
|
256
|
-
/**
|
|
356
|
+
/** `<configDir>/agents`: the legacy journals (`sessions/<ref>.jsonl`) an older CLI left are migrated from here, once. */
|
|
257
357
|
journalDir,
|
|
258
|
-
/** Per-session journal cap. Beyond it the oldest half is dropped and the server told about the hole. */
|
|
259
|
-
maxJournalBytes = DEFAULT_JOURNAL_MAX_BYTES,
|
|
260
358
|
/** Shared update admission and force-cancellation authority, when this daemon supports updates. */
|
|
261
359
|
tasks) {
|
|
262
360
|
this.agents = agents;
|
|
263
361
|
this.journalDir = journalDir;
|
|
264
|
-
this.maxJournalBytes = maxJournalBytes;
|
|
265
362
|
this.tasks = tasks;
|
|
266
363
|
this.send = send;
|
|
267
364
|
}
|
|
268
|
-
/** Where journals live: `<journalDir>/sessions/<sessionRef>.jsonl
|
|
365
|
+
/** Where an older CLI's journals live: `<journalDir>/sessions/<sessionRef>.jsonl` (read-only legacy, `journal.ts`). */
|
|
269
366
|
get sessionsDir() {
|
|
270
367
|
return path.join(this.journalDir, 'sessions');
|
|
271
368
|
}
|
|
369
|
+
/**
|
|
370
|
+
* The daemon journal an older CLI kept (`agents/sessions/<ref>.jsonl`) becomes, once, a local outbox under the same ref
|
|
371
|
+
* (`AcpAgents.importLegacyOutbox`): from then on it is offered as an orphan and resent on a reopen exactly as the journal was,
|
|
372
|
+
* and released like any other outbox. Each file is deleted once its outbox exists; a file that fails stays for the next start.
|
|
373
|
+
* Runs once per process (the resume offer and every open wait for it).
|
|
374
|
+
*/
|
|
375
|
+
migrateLegacyJournals() {
|
|
376
|
+
this.legacyMigration ??= (async () => {
|
|
377
|
+
const importOutbox = this.agents.importLegacyOutbox?.bind(this.agents);
|
|
378
|
+
if (!importOutbox)
|
|
379
|
+
return;
|
|
380
|
+
let refs;
|
|
381
|
+
try {
|
|
382
|
+
refs = await listLegacyJournalRefs(this.sessionsDir);
|
|
383
|
+
}
|
|
384
|
+
catch (error) {
|
|
385
|
+
console.warn(`[agent] listing legacy journals failed: ${errorMessage(error)}`);
|
|
386
|
+
return;
|
|
387
|
+
}
|
|
388
|
+
for (const sessionRef of refs) {
|
|
389
|
+
try {
|
|
390
|
+
const entries = await readLegacyJournal(this.sessionsDir, sessionRef);
|
|
391
|
+
const imported = await importOutbox(sessionRef, entries.map((entry) => ({
|
|
392
|
+
seq: entry.seq,
|
|
393
|
+
...(entry.src ? { src: entry.src } : {}),
|
|
394
|
+
// The outbox keeps the processor's frame; the projection adds `sessionRef` when it is sent.
|
|
395
|
+
frame: withoutSessionRef(entry.frame),
|
|
396
|
+
})));
|
|
397
|
+
if (imported === false) {
|
|
398
|
+
// A local outbox already holds this ref (an import that already happened, or a fallback session's own): it wins, and
|
|
399
|
+
// the file's frames are not merged into it.
|
|
400
|
+
console.warn(`[agent] legacy journal of ${sessionRef} not imported: a local outbox for it already exists; deleting the journal`);
|
|
401
|
+
}
|
|
402
|
+
await removeLegacyJournal(this.sessionsDir, sessionRef);
|
|
403
|
+
if (imported !== false)
|
|
404
|
+
console.info(`[agent] migrated the legacy journal of ${sessionRef} (${entries.length} unacknowledged frame(s)) to its outbox`);
|
|
405
|
+
}
|
|
406
|
+
catch (error) {
|
|
407
|
+
console.warn(`[agent] migrating the legacy journal of ${sessionRef} failed (kept for the next start): ${errorMessage(error)}`);
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
})();
|
|
411
|
+
return this.legacyMigration;
|
|
412
|
+
}
|
|
272
413
|
/** Number of daemon-owned external-agent sessions, for the management heartbeat projection. */
|
|
273
414
|
activeSessionCount() {
|
|
274
415
|
return [...this.sessions.values()].filter((session) => !session.closed).length;
|
|
275
416
|
}
|
|
276
|
-
/** Snapshot advertised once after an agent-channel reconnect; the server remains replay authority. */
|
|
277
417
|
/**
|
|
278
|
-
* The reconnect inventory: the sessions this daemon holds, plus the
|
|
279
|
-
*
|
|
418
|
+
* The reconnect inventory: the sessions this daemon holds, plus the outboxes earlier lives left in the transports (Task 20: what
|
|
419
|
+
* `acp.list` and the local fallback list that no live session holds — a previous daemon's keeper-acp sessions, swept or degraded
|
|
420
|
+
* sessions, local outboxes, migrated legacy journals).
|
|
280
421
|
*
|
|
281
422
|
* **The orphan half is what makes recovery automatic.** A journal whose agent is gone -- the
|
|
282
423
|
* daemon restarted, or the machine rebooted -- is not in {@link sessions}, so before this it
|
|
@@ -286,9 +427,7 @@ export class AgentSessionManager {
|
|
|
286
427
|
*
|
|
287
428
|
* **Orphans carry `journalMaxSeq: 0`, and that is not a placeholder for a number worth reading.**
|
|
288
429
|
* The server drives an orphan off `agentAlive` and its own live-turn ledger, never off a watermark
|
|
289
|
-
* comparison, so nothing reads the field.
|
|
290
|
-
* per file -- each one reads the whole file, up to 64 MB -- on every agent-channel reconnect, to
|
|
291
|
-
* produce a number that is then discarded.
|
|
430
|
+
* comparison, so nothing reads the field.
|
|
292
431
|
*
|
|
293
432
|
* **Gated on {@link AGENT_WIRE_ORPHAN_OFFER_VERSION}, and that gate is load-bearing** in a way the
|
|
294
433
|
* heartbeat-inventory floor is not: this widens the meaning of an existing list whose absence the
|
|
@@ -300,14 +439,19 @@ export class AgentSessionManager {
|
|
|
300
439
|
// `true`: this is the once-per-connection offer, the one place `activityRaw` is allowed to
|
|
301
440
|
// ride — see {@link liveInventory}'s parameter doc for why {@link heartbeatInventory} must pass
|
|
302
441
|
// `false` at its own call site instead.
|
|
303
|
-
|
|
442
|
+
// Spec §5.5.2: every session keeper-acp kept is in the offer (a missing one has its turn ended) — rebuilt ones with their
|
|
443
|
+
// liveness, ones still rebuilding without `turnRunning`.
|
|
444
|
+
await this.adoptionSettled(ADOPTION_OFFER_WAIT_MS);
|
|
445
|
+
await this.adoptLate();
|
|
446
|
+
const live = [...this.liveInventory(true), ...this.adoptingInventory()];
|
|
304
447
|
if (this.serverAgentWire < AGENT_WIRE_ORPHAN_OFFER_VERSION)
|
|
305
448
|
return live;
|
|
306
449
|
// Every ref this daemon still has a handle on, not merely the ones it would report as live:
|
|
307
450
|
// a ref that is mid-open, mid-retire, or closed-but-not-yet-forgotten is one we are still the
|
|
308
451
|
// owner of, and announcing it as abandoned invites a reopen against a session we hold.
|
|
309
|
-
|
|
310
|
-
const
|
|
452
|
+
await this.migrateLegacyJournals();
|
|
453
|
+
const held = new Set([...this.sessions.keys(), ...this.opening.keys(), ...this.retiring.keys(), ...this.adopting.keys()]);
|
|
454
|
+
const orphans = await this.orphanedRefs(held);
|
|
311
455
|
// `turnRunning: false` is an observation here, not a default: an orphan has no agent process at
|
|
312
456
|
// all, so there is nothing that could be running on it.
|
|
313
457
|
return [
|
|
@@ -316,42 +460,20 @@ export class AgentSessionManager {
|
|
|
316
460
|
];
|
|
317
461
|
}
|
|
318
462
|
/**
|
|
319
|
-
*
|
|
320
|
-
*
|
|
321
|
-
*
|
|
322
|
-
* daemon that has never opened a session, so it is not a warning; anything else is, but it still
|
|
323
|
-
* degrades to "offer what we hold" rather than failing the whole offer, because losing the live
|
|
324
|
-
* half of the inventory would be a far bigger regression than missing the orphan half.
|
|
463
|
+
* Transport sessions no live session holds (`AcpAgents.leftoverSessions`). A failure degrades to "offer what we hold" rather
|
|
464
|
+
* than failing the whole offer: losing the live half of the inventory would be a far bigger regression than missing the orphan
|
|
465
|
+
* half.
|
|
325
466
|
*/
|
|
326
|
-
async
|
|
327
|
-
let
|
|
467
|
+
async orphanedRefs(held) {
|
|
468
|
+
let keys;
|
|
328
469
|
try {
|
|
329
|
-
|
|
470
|
+
keys = (await this.agents.leftoverSessions?.()) ?? [];
|
|
330
471
|
}
|
|
331
472
|
catch (error) {
|
|
332
|
-
|
|
333
|
-
console.warn(`[agent] listing journals for the resume offer failed: ${errorMessage(error)}`);
|
|
334
|
-
}
|
|
473
|
+
console.warn(`[agent] listing leftover sessions for the resume offer failed: ${errorMessage(error)}`);
|
|
335
474
|
return [];
|
|
336
475
|
}
|
|
337
|
-
|
|
338
|
-
for (const name of names) {
|
|
339
|
-
if (!name.endsWith('.jsonl'))
|
|
340
|
-
continue;
|
|
341
|
-
let ref;
|
|
342
|
-
try {
|
|
343
|
-
ref = decodeURIComponent(name.slice(0, -'.jsonl'.length));
|
|
344
|
-
}
|
|
345
|
-
catch {
|
|
346
|
-
// Not a name this daemon wrote (`encodeURIComponent` never emits a malformed escape), so
|
|
347
|
-
// there is no ref behind it to offer.
|
|
348
|
-
continue;
|
|
349
|
-
}
|
|
350
|
-
if (!held.has(ref))
|
|
351
|
-
refs.push(ref);
|
|
352
|
-
}
|
|
353
|
-
// readdir order is filesystem-defined; the offer should not be.
|
|
354
|
-
return refs.sort();
|
|
476
|
+
return keys.filter((ref) => !held.has(ref)).sort();
|
|
355
477
|
}
|
|
356
478
|
/**
|
|
357
479
|
* The live half, shared with the heartbeat -- which must stay synchronous and touch no disk.
|
|
@@ -363,7 +485,7 @@ export class AgentSessionManager {
|
|
|
363
485
|
* life -- which is the point: "the agent is gone" is one fact with one handling, not a special
|
|
364
486
|
* case per way of getting there.
|
|
365
487
|
*
|
|
366
|
-
* @param includeActivity Whether to carry each session's {@link
|
|
488
|
+
* @param includeActivity Whether to carry each session's {@link AcpAgentSession.activityRaw}
|
|
367
489
|
* along as `activityRaw`. **Must be `false` from {@link heartbeatInventory} and `true` from
|
|
368
490
|
* {@link resumeOfferSessions}, never the other way round** — `activityRaw` is an unbounded vendor
|
|
369
491
|
* blob, and `AGENT_WIRE_HEARTBEAT_INVENTORY_VERSION`'s own doc comment already ruled that kind of
|
|
@@ -379,15 +501,42 @@ export class AgentSessionManager {
|
|
|
379
501
|
const liveness = session.session.liveness;
|
|
380
502
|
return {
|
|
381
503
|
sessionRef: session.sessionRef,
|
|
382
|
-
|
|
383
|
-
|
|
504
|
+
// The processor's head seq (spec §5.5.2): everything this session numbered, or its outbox held. A server below
|
|
505
|
+
// AGENT_WIRE_SCOPED_ORIGIN_VERSION never gets the scoped frames the processor numbered (they are withheld, a seq gap),
|
|
506
|
+
// so its watermark can never reach the head seq: it is told what the outbox recorded instead, or it would reopen the
|
|
507
|
+
// ref on every inventory.
|
|
508
|
+
journalMaxSeq: this.serverAgentWire < AGENT_WIRE_SCOPED_ORIGIN_VERSION ? session.session.outbox.lastSeq : session.session.headSeq,
|
|
509
|
+
// A ref being switched (Task 24) is idle by construction and alive on one process or the other.
|
|
510
|
+
agentAlive: liveness.agentAlive || this.switching.has(session.sessionRef),
|
|
384
511
|
turnRunning: liveness.turnRunning,
|
|
385
|
-
...(includeActivity && session.
|
|
386
|
-
? { activityRaw: session.
|
|
512
|
+
...(includeActivity && session.session.activityRaw !== undefined
|
|
513
|
+
? { activityRaw: session.session.activityRaw }
|
|
514
|
+
: {}),
|
|
515
|
+
// Task 22 (spec §5.5.4): a durable session's agent blocked on cards — the server reopens the ref when it already
|
|
516
|
+
// answered one of them (the answer this daemon may have lost is resent off that open). Only to a server that reads it.
|
|
517
|
+
...(this.serverAgentWire >= AGENT_WIRE_REOPEN_REQUEST_VERSION && session.session.durable &&
|
|
518
|
+
(session.session.pendingRequestIds?.length ?? 0) > 0
|
|
519
|
+
? { pendingRequestIds: [...session.session.pendingRequestIds] }
|
|
387
520
|
: {}),
|
|
388
521
|
};
|
|
389
522
|
});
|
|
390
523
|
}
|
|
524
|
+
/**
|
|
525
|
+
* Ask the server for `agent_open{requireSessionLoad:true}` on a live session (spec §6.3, `agent_reopen_request`): an idle
|
|
526
|
+
* session with a pending switch needs the spawn secrets only an open carries. Sent only to a server at
|
|
527
|
+
* {@link AGENT_WIRE_REOPEN_REQUEST_VERSION} — an older one cannot decode the frame, and eight undecodable frames in a row close
|
|
528
|
+
* the agent channel (spec §10: there the switch waits for the session's natural reopen). Returns whether it was sent: `false`
|
|
529
|
+
* for an older or unbound server and for a ref this daemon does not hold. The caller (Task 24) decides when.
|
|
530
|
+
*/
|
|
531
|
+
requestReopen(sessionRef, reason) {
|
|
532
|
+
if (this.send === null || this.serverAgentWire < AGENT_WIRE_REOPEN_REQUEST_VERSION)
|
|
533
|
+
return false;
|
|
534
|
+
const live = this.sessions.get(sessionRef);
|
|
535
|
+
if (!live || live.closed)
|
|
536
|
+
return false;
|
|
537
|
+
this.emit({ t: 'agent_reopen_request', sessionRef, reason });
|
|
538
|
+
return true;
|
|
539
|
+
}
|
|
391
540
|
/**
|
|
392
541
|
* The same inventory, for the heartbeat that goes out every ten seconds.
|
|
393
542
|
*
|
|
@@ -411,7 +560,7 @@ export class AgentSessionManager {
|
|
|
411
560
|
if (this.serverAgentWire < AGENT_WIRE_HEARTBEAT_INVENTORY_VERSION)
|
|
412
561
|
return undefined;
|
|
413
562
|
// `false`: activityRaw must stay absent on every heartbeat — see `liveInventory`'s own doc.
|
|
414
|
-
const sessions = this.liveInventory(false);
|
|
563
|
+
const sessions = [...this.liveInventory(false), ...this.adoptingInventory()];
|
|
415
564
|
return sessions.length > 0 ? sessions : undefined;
|
|
416
565
|
}
|
|
417
566
|
/**
|
|
@@ -419,7 +568,7 @@ export class AgentSessionManager {
|
|
|
419
568
|
* the server advertised on this connection — the version is per-connection, so it is rebound with
|
|
420
569
|
* the sender rather than set once.
|
|
421
570
|
*/
|
|
422
|
-
bindSend(send, serverAgentWire = AGENT_WIRE_VERSION) {
|
|
571
|
+
bindSend(send, serverAgentWire = AGENT_WIRE_VERSION, bufferedAmount) {
|
|
423
572
|
if (this.mutedDrops > 0) {
|
|
424
573
|
const seconds = this.mutedSinceMs === null ? '?' : ((Date.now() - this.mutedSinceMs) / 1000).toFixed(1);
|
|
425
574
|
console.warn(`[agent] sender rebound after ${seconds}s unbound; ${this.mutedDrops} frame(s) were dropped while muted`);
|
|
@@ -428,21 +577,37 @@ export class AgentSessionManager {
|
|
|
428
577
|
this.mutedSinceMs = null;
|
|
429
578
|
this.send = send;
|
|
430
579
|
this.serverAgentWire = serverAgentWire;
|
|
580
|
+
// A new connection: its own window and socket. What was sent on the previous one waits for the server's reopen to say how far
|
|
581
|
+
// it got; what was never sent goes out now.
|
|
582
|
+
this.epoch += 1;
|
|
583
|
+
for (const live of this.sessions.values())
|
|
584
|
+
live.session.outbox.connectionChanged();
|
|
585
|
+
this.gate.rebind(bufferedAmount);
|
|
431
586
|
// Before any inbound frame on this connection can be answered: a sweep notice that arrived here
|
|
432
587
|
// after the server had already re-opened its ref would invalidate a healthy generation.
|
|
433
588
|
this.flushSwept();
|
|
589
|
+
this.pump();
|
|
590
|
+
// Account observations are outside the session journal. Replay the cached state on reconnect
|
|
591
|
+
// so a server restart does not depend on the provider sending another quota notification.
|
|
592
|
+
if (this.serverAgentWire >= 27)
|
|
593
|
+
for (const live of this.sessions.values()) {
|
|
594
|
+
if (!live.closed && live.session.accountLimits)
|
|
595
|
+
this.emit({ t: 'agent_account_update', agentKey: live.agentKey, snapshot: live.session.accountLimits });
|
|
596
|
+
}
|
|
597
|
+
for (const ref of [...this.sessions.keys()])
|
|
598
|
+
this.flushAdapterLost(ref);
|
|
434
599
|
}
|
|
435
600
|
/**
|
|
436
601
|
* Called when the websocket drops. Despite the name, this does **not** close agent sessions — the
|
|
437
602
|
* agent subprocesses keep running so a reconnect can resume them. What it does is unbind the
|
|
438
603
|
* sender, tombstone in-flight opens, and end in-flight *logins*.
|
|
439
604
|
*
|
|
440
|
-
* Events produced while unbound are
|
|
441
|
-
*
|
|
605
|
+
* Events produced while unbound are recorded in their session's outbox and wait there; the server gets them when the sender is
|
|
606
|
+
* rebound, and again when it re-opens the ref with a `resumeFromSeq`.
|
|
442
607
|
*
|
|
443
608
|
* A login is the one piece of work here that cannot survive the drop, which is why it is the one
|
|
444
609
|
* thing this really does end: its URL reaches the reader only as `agent_auth_url`, and that frame
|
|
445
|
-
* is not
|
|
610
|
+
* is not recorded the way session events are, so one emitted while unbound is gone rather than
|
|
446
611
|
* replayed — leaving an agent subprocess held open, for up to `AGENT_LOGIN_CEILING_MS`, for a card
|
|
447
612
|
* nobody will ever see. See {@link loginsInFlight}, and the comment at the abort itself for why it
|
|
448
613
|
* is scoped to the teardown that actually unbinds.
|
|
@@ -450,7 +615,7 @@ export class AgentSessionManager {
|
|
|
450
615
|
* In-flight opens are the other exception: the server fails its pending open with ok:false on
|
|
451
616
|
* disconnect, so a session that finishes spawning after this point would be orphaned. Those refs
|
|
452
617
|
* are tombstoned and the resulting bridge session is closed on arrival — but as `disconnected`,
|
|
453
|
-
* which keeps the
|
|
618
|
+
* which keeps the outbox: the server is coming back for it.
|
|
454
619
|
*/
|
|
455
620
|
closeAll(boundSender) {
|
|
456
621
|
// Unbinding is scoped to the connection that did the binding. The sender is one shared slot on
|
|
@@ -471,6 +636,8 @@ export class AgentSessionManager {
|
|
|
471
636
|
// registered, and unknown to the server.
|
|
472
637
|
if (boundSender === undefined || this.send === boundSender) {
|
|
473
638
|
this.send = null;
|
|
639
|
+
this.epoch += 1;
|
|
640
|
+
this.gate.rebind(undefined);
|
|
474
641
|
// Logins are the exception to "sessions keep running" — see {@link loginsInFlight}. Scoped to
|
|
475
642
|
// the teardown that actually unbinds, unlike the tombstoning below: if a newer connection has
|
|
476
643
|
// already rebound, this login's frames still have somewhere to go and killing it would be
|
|
@@ -481,13 +648,13 @@ export class AgentSessionManager {
|
|
|
481
648
|
login.abort();
|
|
482
649
|
}
|
|
483
650
|
for (const sessionRef of this.opening.keys()) {
|
|
484
|
-
// Never downgrade an explicit close into a disconnect: that would strand the
|
|
651
|
+
// Never downgrade an explicit close into a disconnect: that would strand the outbox.
|
|
485
652
|
if (!this.closedDuringOpen.has(sessionRef))
|
|
486
653
|
this.closedDuringOpen.set(sessionRef, 'disconnected');
|
|
487
654
|
}
|
|
488
655
|
}
|
|
489
656
|
/**
|
|
490
|
-
* Awaits every queued
|
|
657
|
+
* Awaits every queued outbox write and the send behind it. Used by tests, and available to a
|
|
491
658
|
* shutdown path that wants events on disk before the process exits.
|
|
492
659
|
*
|
|
493
660
|
* Bounded on purpose. A session that is actively streaming keeps appending to its own chain, so
|
|
@@ -500,51 +667,200 @@ export class AgentSessionManager {
|
|
|
500
667
|
for (let round = 0; round < maxRounds; round += 1) {
|
|
501
668
|
let progressed = false;
|
|
502
669
|
for (const live of [...this.sessions.values()]) {
|
|
503
|
-
const
|
|
504
|
-
await
|
|
505
|
-
if (live.
|
|
670
|
+
const before = live.session.outbox.pending.length + live.session.outbox.lastSeq;
|
|
671
|
+
await live.session.outbox.flush();
|
|
672
|
+
if (live.session.outbox.pending.length + live.session.outbox.lastSeq !== before)
|
|
506
673
|
progressed = true;
|
|
507
674
|
}
|
|
675
|
+
await new Promise((resolve) => setImmediate(resolve));
|
|
508
676
|
if (!progressed)
|
|
509
677
|
return;
|
|
510
678
|
}
|
|
511
679
|
}
|
|
680
|
+
/**
|
|
681
|
+
* The daemon's start (Task 21, spec §5.5): adopt the sessions keeper-acp still runs from a previous daemon. Each is registered as
|
|
682
|
+
* a live session once rebuilt (frames it produces from then on are recorded and sent as any session's); until then the offer and
|
|
683
|
+
* the heartbeat report it without `turnRunning`, and frames for its ref wait for it. A session that cannot be continued is dropped
|
|
684
|
+
* (its adapter stopped, its outbox offered as an orphan). Once per manager; never rejects.
|
|
685
|
+
*/
|
|
686
|
+
adoptSessions() {
|
|
687
|
+
this.adoption ??= (async () => {
|
|
688
|
+
const adopt = this.agents.adoptKeeperSessions?.bind(this.agents);
|
|
689
|
+
if (!adopt)
|
|
690
|
+
return;
|
|
691
|
+
let found;
|
|
692
|
+
try {
|
|
693
|
+
found = await adopt();
|
|
694
|
+
}
|
|
695
|
+
catch (error) {
|
|
696
|
+
console.warn(`[agent] adopting keeper-acp sessions failed: ${errorMessage(error)}`);
|
|
697
|
+
return;
|
|
698
|
+
}
|
|
699
|
+
await Promise.all(this.trackAdoptions(found));
|
|
700
|
+
})();
|
|
701
|
+
return this.adoption;
|
|
702
|
+
}
|
|
703
|
+
/** Registers each found session once rebuilt; until then it is `adopting` (offered without `turnRunning`, its frames wait). */
|
|
704
|
+
trackAdoptions(found) {
|
|
705
|
+
const done = [];
|
|
706
|
+
for (const candidate of found) {
|
|
707
|
+
if (this.sessions.has(candidate.sessionRef) || this.adopting.has(candidate.sessionRef))
|
|
708
|
+
continue;
|
|
709
|
+
const settled = candidate.session.then((session) => {
|
|
710
|
+
if (session)
|
|
711
|
+
this.registerAdopted(candidate, session);
|
|
712
|
+
}, (error) => {
|
|
713
|
+
console.warn(`[agent] adopting ${candidate.sessionRef} failed: ${errorMessage(error)}`);
|
|
714
|
+
}).finally(() => { this.adopting.delete(candidate.sessionRef); });
|
|
715
|
+
this.adopting.set(candidate.sessionRef, { journalMaxSeq: candidate.journalMaxSeq, agentAlive: candidate.agentAlive !== false, done: settled });
|
|
716
|
+
done.push(settled);
|
|
717
|
+
}
|
|
718
|
+
return done;
|
|
719
|
+
}
|
|
720
|
+
/**
|
|
721
|
+
* Final review finding 3: a keeper-acp generation the start-time adoption could not list (it did not answer in time) is listed
|
|
722
|
+
* again — before a frame is handled, before the reconnect offer and on every switch poll — and what it holds is adopted like
|
|
723
|
+
* any start-time adoption, so its sessions are never offered as orphans nor reopened into a new spawn. Resolves once what the
|
|
724
|
+
* retry found is tracked (not rebuilt). Never rejects.
|
|
725
|
+
*/
|
|
726
|
+
adoptLate() {
|
|
727
|
+
const retry = this.agents.retryKeeperAdoption?.bind(this.agents);
|
|
728
|
+
if (!retry || !this.adoption || !this.agents.keeperAdoptionPending?.())
|
|
729
|
+
return Promise.resolve();
|
|
730
|
+
this.lateAdoption ??= (async () => {
|
|
731
|
+
await this.adoption?.catch(() => undefined);
|
|
732
|
+
let found;
|
|
733
|
+
try {
|
|
734
|
+
found = await retry();
|
|
735
|
+
}
|
|
736
|
+
catch (error) {
|
|
737
|
+
console.warn(`[agent] retrying the keeper-acp adoption failed: ${errorMessage(error)}`);
|
|
738
|
+
return;
|
|
739
|
+
}
|
|
740
|
+
if (found.length > 0)
|
|
741
|
+
console.info(`[agent] adopted ${found.length} keeper-acp session(s) late: ${found.map((f) => f.sessionRef).join(', ')}`);
|
|
742
|
+
void Promise.all(this.trackAdoptions(found));
|
|
743
|
+
})().finally(() => { this.lateAdoption = undefined; });
|
|
744
|
+
return this.lateAdoption;
|
|
745
|
+
}
|
|
746
|
+
/** An adopted session joins like an opened one; the server reopens it (or not) from the offer. */
|
|
747
|
+
registerAdopted(candidate, session) {
|
|
748
|
+
if (this.sessions.has(candidate.sessionRef)) {
|
|
749
|
+
// Cannot happen (opens of an adopting ref wait for it); never hold two sessions under one ref.
|
|
750
|
+
void session.detach?.();
|
|
751
|
+
return;
|
|
752
|
+
}
|
|
753
|
+
let highestScopedJournalSeq = 0;
|
|
754
|
+
for (const item of session.outbox.pending) {
|
|
755
|
+
if (item.frame.t === 'agent_event' && item.frame.acpSessionId != null)
|
|
756
|
+
highestScopedJournalSeq = Math.max(highestScopedJournalSeq, item.seq);
|
|
757
|
+
}
|
|
758
|
+
const live = {
|
|
759
|
+
sessionRef: candidate.sessionRef, adopted: true, agentId: candidate.agentId, agentKey: candidate.agentKey, session,
|
|
760
|
+
nativeSessionId: session.nativeSessionId ?? null,
|
|
761
|
+
scopedReplayBlocked: false, highestScopedJournalSeq, unwatch: () => { }, replays: Promise.resolve(), closed: false,
|
|
762
|
+
lastActivityAt: Date.now(),
|
|
763
|
+
};
|
|
764
|
+
this.register(live);
|
|
765
|
+
console.info(`[acp:${live.agentId} agent:${live.agentKey}] adopted session ${live.sessionRef} from keeper-acp`);
|
|
766
|
+
}
|
|
767
|
+
/** Waits (bounded) for the start-time adoption; what is still rebuilding afterwards is reported as such. */
|
|
768
|
+
async adoptionSettled(timeoutMs) {
|
|
769
|
+
if (!this.adoption)
|
|
770
|
+
return;
|
|
771
|
+
let timer;
|
|
772
|
+
await Promise.race([this.adoption, new Promise((resolve) => { timer = setTimeout(resolve, timeoutMs); })]);
|
|
773
|
+
clearTimeout(timer);
|
|
774
|
+
}
|
|
775
|
+
/** Refs still being rebuilt, as inventory entries: alive, their outbox's head seq, and no `turnRunning` (not observed yet). */
|
|
776
|
+
adoptingInventory() {
|
|
777
|
+
return [...this.adopting.entries()].filter(([ref]) => !this.sessions.has(ref))
|
|
778
|
+
.map(([sessionRef, { journalMaxSeq, agentAlive }]) => ({ sessionRef, journalMaxSeq, agentAlive }));
|
|
779
|
+
}
|
|
780
|
+
/**
|
|
781
|
+
* The daemon is going away (SIGTERM, spec §5.5.5): every durable (keeper-hosted) session is detached — head checkpoint, adapter
|
|
782
|
+
* untouched, no failed turn — and the others cut a head checkpoint (their adapters end with the daemon, their outboxes are kept).
|
|
783
|
+
* Bounded by `timeoutMs`.
|
|
784
|
+
*/
|
|
785
|
+
async detachAll(timeoutMs = 2_000) {
|
|
786
|
+
// Sessions still being adopted are not in `sessions` and are skipped. Harmless: an adoption writes no checkpoint of its own and
|
|
787
|
+
// holds nothing the next daemon cannot rebuild the same way (the adapter runs on in keeper-acp, the logs are durable); if one
|
|
788
|
+
// registers after this, the transports are closed right behind it and its loop stops quietly (`KeeperClosedError`).
|
|
789
|
+
const work = [...this.sessions.values()].map((live) => (live.session.durable && live.session.detach
|
|
790
|
+
? this.retire(live, 'detach')
|
|
791
|
+
: live.session.checkpoint?.())?.catch((err) => {
|
|
792
|
+
console.warn(`[agent] leaving ${live.sessionRef} to the next daemon failed: ${errorMessage(err)}`);
|
|
793
|
+
}));
|
|
794
|
+
let timer;
|
|
795
|
+
await Promise.race([Promise.all(work), new Promise((resolve) => { timer = setTimeout(resolve, timeoutMs); })]);
|
|
796
|
+
clearTimeout(timer);
|
|
797
|
+
}
|
|
512
798
|
async handleFrame(frame) {
|
|
799
|
+
// A frame for a ref keeper-acp still runs from a previous daemon waits until that session is rebuilt (Task 21): answering
|
|
800
|
+
// it "unknown session" would end a turn that is going on.
|
|
801
|
+
const frameRef = 'sessionRef' in frame && typeof frame.sessionRef === 'string' ? frame.sessionRef : undefined;
|
|
802
|
+
// A generation the adoption could not list yet may hold this ref (final review finding 3): listed again first (synchronous check).
|
|
803
|
+
if (frameRef !== undefined && !this.liveSession(frameRef) && this.agents.keeperAdoptionPending?.())
|
|
804
|
+
await this.adoptLate();
|
|
805
|
+
const adoptingRef = frameRef !== undefined ? this.adopting.get(frameRef) : undefined;
|
|
806
|
+
if (adoptingRef)
|
|
807
|
+
await adoptingRef.done;
|
|
808
|
+
// Parked while the ref is switched (spec §6.3 step 1): delivered, in arrival order, to whichever session holds it afterwards.
|
|
809
|
+
for (let parked = frameRef !== undefined ? this.switching.get(frameRef) : undefined; parked; parked = this.switching.get(frameRef))
|
|
810
|
+
await parked;
|
|
513
811
|
switch (frame.t) {
|
|
514
812
|
case 'agent_delegation_renew': {
|
|
515
813
|
if (this.serverAgentWire < AGENT_WIRE_DELEGATION_RENEW_VERSION)
|
|
516
814
|
return;
|
|
517
815
|
const live = this.liveSession(frame.sessionRef);
|
|
518
816
|
const now = Date.now();
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
live.
|
|
817
|
+
// Delegation's non-secret fields are processor state (Task 19): the checks read it, an accepted renewal is a note.
|
|
818
|
+
const delegation = live?.session.delegation ?? undefined;
|
|
819
|
+
if (!live || !delegation || live.closed || !live.session.liveness.agentAlive || live.session.liveness.turnRunning ||
|
|
820
|
+
activityIsBusy(live.session.activityRaw) || delegation.refreshPending ||
|
|
821
|
+
live.nativeSessionId !== frame.nativeSessionId || delegation.instanceId !== frame.instanceId ||
|
|
822
|
+
delegation.bindingId !== frame.bindingId || !frame.bindingId ||
|
|
523
823
|
!Number.isSafeInteger(frame.renewalRevision) || frame.renewalRevision < 1 ||
|
|
524
824
|
!Number.isSafeInteger(frame.expiresAtEpochMs) || frame.expiresAtEpochMs <= now ||
|
|
525
|
-
frame.expiresAtEpochMs > now + 330_000 || frame.renewalRevision < (
|
|
526
|
-
frame.expiresAtEpochMs <
|
|
527
|
-
(frame.renewalRevision ===
|
|
825
|
+
frame.expiresAtEpochMs > now + 330_000 || frame.renewalRevision < (delegation.renewalRevision ?? 0) ||
|
|
826
|
+
frame.expiresAtEpochMs < delegation.expiresAtEpochMs ||
|
|
827
|
+
(frame.renewalRevision === delegation.renewalRevision && frame.expiresAtEpochMs !== delegation.expiresAtEpochMs)) {
|
|
528
828
|
this.emit({ t: 'agent_delegation_renewed', id: frame.id, ok: false, error: 'The original managed instance cannot renew' });
|
|
529
829
|
return;
|
|
530
830
|
}
|
|
531
|
-
|
|
532
|
-
|
|
831
|
+
try {
|
|
832
|
+
await live.session.note({ type: 'agent_delegation_renewed', expiresAtEpochMs: frame.expiresAtEpochMs, renewalRevision: frame.renewalRevision });
|
|
833
|
+
}
|
|
834
|
+
catch (err) {
|
|
835
|
+
this.emit({ t: 'agent_delegation_renewed', id: frame.id, ok: false, error: errorMessage(err) });
|
|
836
|
+
return;
|
|
837
|
+
}
|
|
838
|
+
const renewed = live.session.delegation ?? delegation;
|
|
533
839
|
this.emit({ t: 'agent_delegation_renewed', id: frame.id, ok: true,
|
|
534
|
-
nativeSessionId: live.nativeSessionId, instanceId:
|
|
535
|
-
renewalRevision:
|
|
840
|
+
nativeSessionId: live.nativeSessionId, instanceId: renewed.instanceId, bindingId: renewed.bindingId,
|
|
841
|
+
renewalRevision: renewed.renewalRevision, expiresAtEpochMs: renewed.expiresAtEpochMs });
|
|
536
842
|
return;
|
|
537
843
|
}
|
|
538
844
|
case 'agent_open': {
|
|
539
845
|
const admission = this.reserveOpen(frame.id, frame.sessionRef);
|
|
540
846
|
if (admission === 'rejected')
|
|
541
847
|
return;
|
|
848
|
+
const ref = frame.sessionRef;
|
|
849
|
+
this.openHandling.set(ref, (this.openHandling.get(ref) ?? 0) + 1);
|
|
542
850
|
try {
|
|
543
851
|
await this.onOpen(frame);
|
|
544
852
|
}
|
|
545
853
|
finally {
|
|
546
854
|
if (admission === 'reserved')
|
|
547
|
-
this.tasks?.agentOpenFinished(
|
|
855
|
+
this.tasks?.agentOpenFinished(ref);
|
|
856
|
+
const left = (this.openHandling.get(ref) ?? 1) - 1;
|
|
857
|
+
if (left > 0)
|
|
858
|
+
this.openHandling.set(ref, left);
|
|
859
|
+
else
|
|
860
|
+
this.openHandling.delete(ref);
|
|
861
|
+
// An adapter that exited while this open was handled (a respawn that died right after its load) is reported after the
|
|
862
|
+
// reply: reported before it, the server would void the generation it is establishing.
|
|
863
|
+
this.flushAdapterLost(ref);
|
|
548
864
|
}
|
|
549
865
|
return;
|
|
550
866
|
}
|
|
@@ -553,27 +869,14 @@ export class AgentSessionManager {
|
|
|
553
869
|
if (!live)
|
|
554
870
|
return;
|
|
555
871
|
if (this.tasks && !this.tasks.tryAdmitTurn(frame.sessionRef)) {
|
|
556
|
-
//
|
|
557
|
-
//
|
|
558
|
-
//
|
|
559
|
-
//
|
|
560
|
-
|
|
561
|
-
this.record(live, (seq) => ({
|
|
562
|
-
t: 'agent_turn_done', sessionRef: frame.sessionRef, seq, status: 'failed',
|
|
563
|
-
error: 'Workstation is not accepting new work',
|
|
564
|
-
platformTurnId: frame.platformTurnId,
|
|
565
|
-
}));
|
|
872
|
+
// The refusal is the daemon's decision, so it is a note (`agent_prompt{admitted:false}`) and the processor ends the
|
|
873
|
+
// turn `failed` with the same words (`ADMISSION_REFUSED`), numbered in the session's own sequence (spec §5.4). No
|
|
874
|
+
// `authMethods`: the processor's short frame carries no `agentRefused`, so the projection adds none — the prompt
|
|
875
|
+
// never reached the agent.
|
|
876
|
+
live.session.refusePrompt(frame.platformTurnId);
|
|
566
877
|
return;
|
|
567
878
|
}
|
|
568
|
-
|
|
569
|
-
live.session.prompt(promptBlocks(frame), frame.platformTurnId);
|
|
570
|
-
}
|
|
571
|
-
catch (error) {
|
|
572
|
-
this.record(live, (seq) => ({
|
|
573
|
-
t: 'agent_turn_done', sessionRef: frame.sessionRef, seq, status: 'failed',
|
|
574
|
-
error: error instanceof Error ? error.message : String(error), platformTurnId: frame.platformTurnId,
|
|
575
|
-
}));
|
|
576
|
-
}
|
|
879
|
+
live.session.prompt(promptBlocks(frame), frame.platformTurnId);
|
|
577
880
|
return;
|
|
578
881
|
}
|
|
579
882
|
case 'agent_answer': {
|
|
@@ -678,20 +981,11 @@ export class AgentSessionManager {
|
|
|
678
981
|
this.emit({ t: 'agent_provider_task_stopped', id: frame.id, state: 'rejected', error: 'Provider task control requires a newer server' });
|
|
679
982
|
return;
|
|
680
983
|
}
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
return;
|
|
687
|
-
}
|
|
688
|
-
try {
|
|
689
|
-
const state = await live.session.providerTaskStop(frame.taskId, frame.requestId);
|
|
690
|
-
this.emit({ t: 'agent_provider_task_stopped', id: frame.id, state });
|
|
691
|
-
}
|
|
692
|
-
catch (err) {
|
|
693
|
-
this.emit({ t: 'agent_provider_task_stopped', id: frame.id, state: 'unknown', error: errorMessage(err) });
|
|
694
|
-
}
|
|
984
|
+
// No session selects a provider-task adapter (none ever did: the old path's `providerTaskAdapterSelected` had no
|
|
985
|
+
// producer that set it, and it went with that path in Task 29), so no task identity is ever live and trusted. The
|
|
986
|
+
// wire answer is unchanged, and so is the routing (an unknown ref is still reported lost).
|
|
987
|
+
this.routableSession(frame.sessionRef, 'provider_task_stop');
|
|
988
|
+
this.emit({ t: 'agent_provider_task_stopped', id: frame.id, state: 'rejected', error: 'Provider task identity is not live and trusted' });
|
|
695
989
|
return;
|
|
696
990
|
}
|
|
697
991
|
/**
|
|
@@ -733,7 +1027,7 @@ export class AgentSessionManager {
|
|
|
733
1027
|
// — see {@link promptBlocks}. `frame.priority` is passed through untouched, `undefined`
|
|
734
1028
|
// included: absent means the server named no timing, and inventing one here would be
|
|
735
1029
|
// this daemon deciding something the server deliberately left unsaid.
|
|
736
|
-
outcome: await live.session.steer(promptBlocks(frame), frame.priority),
|
|
1030
|
+
outcome: await live.session.steer(promptBlocks(frame), frame.priority, frame.id),
|
|
737
1031
|
});
|
|
738
1032
|
}
|
|
739
1033
|
catch (err) {
|
|
@@ -769,7 +1063,7 @@ export class AgentSessionManager {
|
|
|
769
1063
|
const operation = `request=${frame.id} session=${frame.sessionRef} config=${frame.configId}`;
|
|
770
1064
|
console.info(`${new Date().toISOString()} [agent/config] applying ${operation}`);
|
|
771
1065
|
try {
|
|
772
|
-
const configOptions = await live.session.setConfigOption(frame.configId, frame.value);
|
|
1066
|
+
const configOptions = await live.session.setConfigOption(frame.configId, frame.value, frame.id);
|
|
773
1067
|
console.info(`${new Date().toISOString()} [agent/config] confirmed ${operation} elapsedMs=${Math.round(performance.now() - startedAt)}`);
|
|
774
1068
|
this.emit({ t: 'agent_config_option_set', id: frame.id, ok: true, configOptions });
|
|
775
1069
|
}
|
|
@@ -791,13 +1085,7 @@ export class AgentSessionManager {
|
|
|
791
1085
|
if (!live)
|
|
792
1086
|
return;
|
|
793
1087
|
this.touch(live);
|
|
794
|
-
await this.
|
|
795
|
-
if (live.scopedReplayBlocked || this.blockIncompatibleJournal(live))
|
|
796
|
-
return;
|
|
797
|
-
await live.journal.truncateThrough(frame.throughSeq);
|
|
798
|
-
if (frame.throughSeq >= live.highestScopedJournalSeq)
|
|
799
|
-
live.highestScopedJournalSeq = 0;
|
|
800
|
-
});
|
|
1088
|
+
await this.acknowledge(live, frame.throughSeq);
|
|
801
1089
|
return;
|
|
802
1090
|
}
|
|
803
1091
|
case 'agent_list_native':
|
|
@@ -816,6 +1104,28 @@ export class AgentSessionManager {
|
|
|
816
1104
|
return;
|
|
817
1105
|
// Both of these spawn an agent subprocess, exactly as `agent_list_native` does, so they answer
|
|
818
1106
|
// a draining workstation the same way rather than starting work it is trying to finish.
|
|
1107
|
+
case 'agent_account_read': {
|
|
1108
|
+
if (this.tasks && !this.tasks.accepting) {
|
|
1109
|
+
this.emit({ t: 'agent_account_read_result', id: frame.id, ok: false, error: 'Workstation is not accepting new work' });
|
|
1110
|
+
return;
|
|
1111
|
+
}
|
|
1112
|
+
try {
|
|
1113
|
+
// A keeper session may predate the extension even after its current adapter build was upgraded.
|
|
1114
|
+
const spec = sanitizeNativeSessionQuery(frame.bridge, frame.query);
|
|
1115
|
+
if (!spec)
|
|
1116
|
+
throw new Error('No workspace is available');
|
|
1117
|
+
const minimumVersion = await this.agents.minimumAccountLimitsVersion?.(spec) ?? 2;
|
|
1118
|
+
const live = [...this.sessions.values()].find(live => live.agentKey === frame.agentKey && !live.closed && live.session.liveness.agentAlive && live.session.supportsAccountLimits === true && (live.session.accountLimitsVersion ?? 2) >= minimumVersion);
|
|
1119
|
+
if (!live?.session.readAccountLimits && !this.agents.readAccountLimits)
|
|
1120
|
+
throw new Error('Account usage is unsupported');
|
|
1121
|
+
const snapshot = live?.session.readAccountLimits ? await live.session.readAccountLimits(frame.id) : await this.agents.readAccountLimits(spec);
|
|
1122
|
+
this.emit({ t: 'agent_account_read_result', id: frame.id, ok: true, snapshot });
|
|
1123
|
+
}
|
|
1124
|
+
catch {
|
|
1125
|
+
this.emit({ t: 'agent_account_read_result', id: frame.id, ok: false, error: 'Failed to fetch usage' });
|
|
1126
|
+
}
|
|
1127
|
+
return;
|
|
1128
|
+
}
|
|
819
1129
|
case 'agent_verify':
|
|
820
1130
|
if (this.tasks && !this.tasks.accepting) {
|
|
821
1131
|
this.emit({ t: 'agent_verified', id: frame.id, ok: false, error: 'Workstation is not accepting new work' });
|
|
@@ -839,6 +1149,14 @@ export class AgentSessionManager {
|
|
|
839
1149
|
}
|
|
840
1150
|
await this.onInstall(frame);
|
|
841
1151
|
return;
|
|
1152
|
+
// "Move idle sessions" (Task 25): a move spawns adapters, so a draining workstation refuses it like an install.
|
|
1153
|
+
case 'agent_switch_idle':
|
|
1154
|
+
if (this.tasks && !this.tasks.accepting) {
|
|
1155
|
+
this.emit({ t: 'agent_switch_idle_result', id: frame.id, ok: false, error: 'Workstation is not accepting new work' });
|
|
1156
|
+
return;
|
|
1157
|
+
}
|
|
1158
|
+
await this.onSwitchIdle(frame);
|
|
1159
|
+
return;
|
|
842
1160
|
default:
|
|
843
1161
|
this.warnUnhandledFrame(frame);
|
|
844
1162
|
return;
|
|
@@ -877,23 +1195,104 @@ export class AgentSessionManager {
|
|
|
877
1195
|
* Classifies an open before its first await. A live or already-opening ref is continuation
|
|
878
1196
|
* traffic, so graceful drain lets it replay/piggyback without acquiring (and later releasing)
|
|
879
1197
|
* another reservation. Force-stop is different: it is destructive shutdown and rejects every
|
|
880
|
-
* open, including reconnects
|
|
1198
|
+
* open, including reconnects — except a reconnect of a live keeper-hosted session, which the force
|
|
1199
|
+
* stop leaves attached. A ref absent from both maps is genuinely new work and still needs
|
|
881
1200
|
* the ordinary TaskRegistry admission reservation.
|
|
882
1201
|
*/
|
|
883
1202
|
reserveOpen(openId, sessionRef) {
|
|
884
1203
|
if (!this.tasks)
|
|
885
1204
|
return 'continuation';
|
|
1205
|
+
const live = this.liveSession(sessionRef);
|
|
1206
|
+
// A keeper-hosted session whose adapter is gone is respawned by this open: new work, not a continuation — it takes a reservation,
|
|
1207
|
+
// and a draining workstation refuses it like any open (spec §8.2; fix round 1, minor 9).
|
|
1208
|
+
const respawn = live !== undefined && this.needsRespawn(live);
|
|
886
1209
|
if (this.tasks.isForceStopping()) {
|
|
1210
|
+
// A keeper-hosted session a force update left attached (TaskRegistry.stopAll) is still reopened as a continuation — a reconnect
|
|
1211
|
+
// during the download must not cut it off; anything else is refused.
|
|
1212
|
+
if (live && !respawn && this.tasks.isDurableSession(sessionRef))
|
|
1213
|
+
return 'continuation';
|
|
887
1214
|
this.emit({ t: 'agent_opened', id: openId, ok: false, error: 'Workstation is not accepting new work' });
|
|
888
1215
|
return 'rejected';
|
|
889
1216
|
}
|
|
890
|
-
if (this.
|
|
1217
|
+
if ((live && !respawn) || this.opening.has(sessionRef) || this.adopting.has(sessionRef))
|
|
891
1218
|
return 'continuation';
|
|
892
1219
|
if (this.tasks.tryReserveAgentOpen(sessionRef))
|
|
893
1220
|
return 'reserved';
|
|
894
1221
|
this.emit({ t: 'agent_opened', id: openId, ok: false, error: 'Workstation is not accepting new work' });
|
|
895
1222
|
return 'rejected';
|
|
896
1223
|
}
|
|
1224
|
+
/**
|
|
1225
|
+
* A session whose adapter is gone: an `agent_open` of it spawns and loads anew (Task 21) — keeper-hosted or adopted, and also a
|
|
1226
|
+
* local-fallback session whose adapter exited (it would otherwise be "reused" dead).
|
|
1227
|
+
*/
|
|
1228
|
+
needsRespawn(live) {
|
|
1229
|
+
const exited = live.session.adapterExit !== undefined;
|
|
1230
|
+
return (live.adopted === true || live.session.durable === true || exited) && !live.session.liveness.agentAlive;
|
|
1231
|
+
}
|
|
1232
|
+
/**
|
|
1233
|
+
* The adapter of `live` exited under its loop, and its frames (a failed `turn_done` for an open turn) are recorded. Unless this
|
|
1234
|
+
* daemon stopped it (`terminated`: a close, a switch, a retire), the session cannot take another prompt: the server is told it is
|
|
1235
|
+
* lost (`agent_session_lost{sweep}` — the poison contract's recovery, spec §11 "single adapter crash"), so its next prompt opens
|
|
1236
|
+
* first, and that `agent_open` respawns the adapter and loads the native session ({@link needsRespawn}). The session stays held
|
|
1237
|
+
* (reported `agentAlive:false`) until then. An exit of its own also counts towards {@link ADAPTER_CRASH_LIMIT}.
|
|
1238
|
+
*/
|
|
1239
|
+
onAdapterExited(live, exit) {
|
|
1240
|
+
const ref = live.sessionRef;
|
|
1241
|
+
if (exit.reason === 'terminated')
|
|
1242
|
+
return;
|
|
1243
|
+
if (live.closed || this.sessions.get(ref) !== live || this.detached.has(ref))
|
|
1244
|
+
return;
|
|
1245
|
+
const how = exitWords(exit);
|
|
1246
|
+
if (exit.reason === undefined) {
|
|
1247
|
+
const now = Date.now();
|
|
1248
|
+
const last = this.adapterCrashes.get(ref);
|
|
1249
|
+
const count = last && now - last.lastAt < ADAPTER_CRASH_FORGET_MS ? last.count + 1 : 1;
|
|
1250
|
+
this.adapterCrashes.set(ref, { count, lastAt: now, detail: how });
|
|
1251
|
+
}
|
|
1252
|
+
console.warn(`[acp:${live.agentId} agent:${live.agentKey}] the adapter of session ${ref} exited ` +
|
|
1253
|
+
`(${exit.reason ?? 'on its own'}; ${how})${exit.busy ? ' while a turn or card was open' : ''}; its next open respawns it`);
|
|
1254
|
+
// Everything the exit produced (the failed `turn_done`) is in the outbox by now: the notice waits until it has been sent.
|
|
1255
|
+
live.adapterLost = { spawnId: exit.spawnId, reason: exit.reason, busy: exit.busy, notified: false, throughSeq: live.session.outbox.lastSeq };
|
|
1256
|
+
this.flushAdapterLost(ref);
|
|
1257
|
+
}
|
|
1258
|
+
/**
|
|
1259
|
+
* Sends the lost notice of the session holding `ref` ({@link onAdapterExited}) once it cannot overtake anything: no open, switch
|
|
1260
|
+
* or retire of the ref under way (the open's own reply goes first), and every frame the exit produced (`throughSeq`: its failed
|
|
1261
|
+
* `turn_done`) already sent **on this connection** — the outbox pump holds frames behind the session window and the socket gate,
|
|
1262
|
+
* while this notice is sent directly. Overtaken, the server would end the turn itself, drop the `turn_done` unacknowledged, and
|
|
1263
|
+
* the reopen's replay of it would end the NEXT turn. Re-checked whenever the pump sends for the ref, after an open, and after a
|
|
1264
|
+
* rebind. Not queued while unbound ({@link sweptWhileUnsent}): a new connection leaves the server's delivery unwired anyway, and
|
|
1265
|
+
* entries sent on an earlier one are held until its reopen — which respawns the adapter and retires this session.
|
|
1266
|
+
*/
|
|
1267
|
+
flushAdapterLost(ref) {
|
|
1268
|
+
const live = this.liveSession(ref);
|
|
1269
|
+
const lost = live?.adapterLost;
|
|
1270
|
+
if (!live || !lost || lost.notified || this.send === null)
|
|
1271
|
+
return;
|
|
1272
|
+
if (this.opening.has(ref) || this.openHandling.has(ref) || this.switching.has(ref) || this.retiring.has(ref))
|
|
1273
|
+
return;
|
|
1274
|
+
if (live.session.liveness.agentAlive)
|
|
1275
|
+
return;
|
|
1276
|
+
if (live.session.outbox.pending.some((item) => item.seq <= lost.throughSeq && item.sentOn !== this.epoch))
|
|
1277
|
+
return;
|
|
1278
|
+
lost.notified = true;
|
|
1279
|
+
this.reportSessionLost(ref, AgentSessionManager.SWEEP_LOST_ACTION);
|
|
1280
|
+
}
|
|
1281
|
+
/**
|
|
1282
|
+
* Why an open of `ref` may not respawn its adapter now ({@link ADAPTER_CRASH_LIMIT}), or `undefined`. The text is what the user's
|
|
1283
|
+
* failed message shows.
|
|
1284
|
+
*/
|
|
1285
|
+
adapterCrashHold(ref, now) {
|
|
1286
|
+
const crashes = this.adapterCrashes.get(ref);
|
|
1287
|
+
if (!crashes || crashes.count < ADAPTER_CRASH_LIMIT || now - crashes.lastAt >= ADAPTER_CRASH_FORGET_MS)
|
|
1288
|
+
return undefined;
|
|
1289
|
+
const backoff = Math.min(this.adapterCrashBackoffMs * 2 ** (crashes.count - ADAPTER_CRASH_LIMIT), ADAPTER_CRASH_BACKOFF_MAX_MS);
|
|
1290
|
+
const wait = crashes.lastAt + backoff - now;
|
|
1291
|
+
if (wait <= 0)
|
|
1292
|
+
return undefined;
|
|
1293
|
+
return `The agent's adapter process exited on its own ${crashes.count} times in a row (last: ${crashes.detail}); ` +
|
|
1294
|
+
`it is not started again for another ${Math.ceil(wait / 1000)} s.`;
|
|
1295
|
+
}
|
|
897
1296
|
/**
|
|
898
1297
|
* Everything a failure frame says about a refusal **the agent itself** produced: the sign-in table
|
|
899
1298
|
* it advertised, and the JSON-RPC code it answered with.
|
|
@@ -952,7 +1351,12 @@ export class AgentSessionManager {
|
|
|
952
1351
|
*/
|
|
953
1352
|
routableSession(sessionRef, action) {
|
|
954
1353
|
const live = this.liveSession(sessionRef);
|
|
955
|
-
if (!live) {
|
|
1354
|
+
if (!live && this.detached.has(sessionRef)) {
|
|
1355
|
+
// Left to the next daemon (spec §5.5.5): it continues the session, so it is not lost. An answer dropped here is resent by the
|
|
1356
|
+
// server off the next `agent_opened.pendingRequestIds`.
|
|
1357
|
+
console.warn(`[agent] dropped ${action} for '${sessionRef}': the session was handed to the next daemon`);
|
|
1358
|
+
}
|
|
1359
|
+
else if (!live) {
|
|
956
1360
|
console.warn(`[agent] dropped ${action} for unknown session '${sessionRef}'`);
|
|
957
1361
|
this.reportSessionLost(sessionRef, action);
|
|
958
1362
|
}
|
|
@@ -1042,18 +1446,17 @@ export class AgentSessionManager {
|
|
|
1042
1446
|
* `now` is a parameter rather than a clock read so the sweep is deterministic under test.
|
|
1043
1447
|
* The teardown is deliberately the same one `agent_close` performs ({@link retire}) — a swept
|
|
1044
1448
|
* session must leave nothing behind that a later frame could resurrect — with one deliberate
|
|
1045
|
-
* exception: **the
|
|
1449
|
+
* exception: **the outbox survives**.
|
|
1046
1450
|
*
|
|
1047
1451
|
* That exception is the difference between `agent_close` and this sweep. `agent_close` is the
|
|
1048
|
-
* server saying "I am done with this ref", so its
|
|
1452
|
+
* server saying "I am done with this ref", so its outbox can never be asked for again. A sweep is
|
|
1049
1453
|
* unilateral: the server has not asked for anything and may well re-open the ref with a
|
|
1050
|
-
* `resumeFromSeq` the moment it next hears from the user. The sweep also fires precisely when
|
|
1051
|
-
*
|
|
1052
|
-
* TTL (a laptop shut overnight), so a turn that finished while the socket was down is
|
|
1053
|
-
* and unsent. Destroying it there loses that turn outright
|
|
1054
|
-
* not even a transport-gap card would mark the hole. The same distinction already exists one layer
|
|
1454
|
+
* `resumeFromSeq` the moment it next hears from the user. The sweep also fires precisely when an
|
|
1455
|
+
* outbox is most likely to be holding undelivered output — the workstation was offline past the
|
|
1456
|
+
* TTL (a laptop shut overnight), so a turn that finished while the socket was down is recorded
|
|
1457
|
+
* and unsent. Destroying it there loses that turn outright. The same distinction already exists one layer
|
|
1055
1458
|
* up as {@link AbandonReason}; a sweep is `disconnected`-shaped, not `closed`-shaped. Kill the
|
|
1056
|
-
* process, keep the
|
|
1459
|
+
* process, keep the outbox (the transport session; its next open reuses it).
|
|
1057
1460
|
*
|
|
1058
1461
|
* Each sweep also reports itself to the server ({@link notifySwept}) so its stale session state is
|
|
1059
1462
|
* retired before the next user message runs into it, rather than after — the sweep heals the
|
|
@@ -1066,7 +1469,7 @@ export class AgentSessionManager {
|
|
|
1066
1469
|
// debugging a stuck workstation can see it instead of just trusting a RUNNING that never moves.
|
|
1067
1470
|
for (const live of this.sessions.values()) {
|
|
1068
1471
|
if (!live.staleActivityWarned &&
|
|
1069
|
-
activityIsBusy(live.
|
|
1472
|
+
activityIsBusy(live.session.activityRaw) &&
|
|
1070
1473
|
live.lastActivityAtMs !== undefined &&
|
|
1071
1474
|
now - live.lastActivityAtMs > STALE_ACTIVITY_WARN_MS) {
|
|
1072
1475
|
live.staleActivityWarned = true;
|
|
@@ -1101,12 +1504,37 @@ export class AgentSessionManager {
|
|
|
1101
1504
|
return;
|
|
1102
1505
|
}
|
|
1103
1506
|
}
|
|
1104
|
-
// Rotation is server-driven. A busy reconnect
|
|
1105
|
-
|
|
1507
|
+
// Rotation is server-driven. A busy reconnect resends the original process's outbox.
|
|
1508
|
+
let existing = this.liveSession(frame.sessionRef);
|
|
1509
|
+
// An open that would spawn the ref's adapter anew after it kept exiting on its own waits out the backoff (no crash loop).
|
|
1510
|
+
if (existing && this.needsRespawn(existing) && !this.opening.has(frame.sessionRef)) {
|
|
1511
|
+
const held = this.adapterCrashHold(frame.sessionRef, Date.now());
|
|
1512
|
+
if (held) {
|
|
1513
|
+
// Rate-limited: the server's heartbeat may retry the open every ten seconds for the whole backoff.
|
|
1514
|
+
const now = Date.now();
|
|
1515
|
+
const crashes = this.adapterCrashes.get(frame.sessionRef);
|
|
1516
|
+
if (crashes && now - (crashes.warnedAt ?? -Infinity) >= ADAPTER_CRASH_WARN_EVERY_MS) {
|
|
1517
|
+
crashes.warnedAt = now;
|
|
1518
|
+
console.warn(`[agent] ${frame.sessionRef}: not respawning its adapter: ${held}`);
|
|
1519
|
+
}
|
|
1520
|
+
this.emit({ t: 'agent_opened', id: frame.id, ok: false, error: held });
|
|
1521
|
+
if (frame.backfill)
|
|
1522
|
+
this.emit({ t: 'agent_backfill_done', id: frame.id, ok: false, count: 0, error: held });
|
|
1523
|
+
return;
|
|
1524
|
+
}
|
|
1525
|
+
}
|
|
1106
1526
|
let openFrame = frame;
|
|
1107
1527
|
let retireForRefresh;
|
|
1528
|
+
if (existing && this.needsRespawn(existing)) {
|
|
1529
|
+
// A keeper-hosted session whose adapter is gone (it exited, or keeper-acp restarted and recovered it as `keeper_lost` — its
|
|
1530
|
+
// tail was folded and recorded): the reopen starts a new spawn in the same transport session and loads the native one, as
|
|
1531
|
+
// for any session left behind (Task 21).
|
|
1532
|
+
retireForRefresh = existing;
|
|
1533
|
+
existing = undefined;
|
|
1534
|
+
}
|
|
1108
1535
|
if (existing) {
|
|
1109
|
-
|
|
1536
|
+
const delegation = existing.session.delegation ?? undefined;
|
|
1537
|
+
if (delegation && !frame.delegationMcp) {
|
|
1110
1538
|
this.emit({ t: 'agent_opened', id: frame.id, ok: false, error: 'Managed delegation lease is required' });
|
|
1111
1539
|
return;
|
|
1112
1540
|
}
|
|
@@ -1115,14 +1543,22 @@ export class AgentSessionManager {
|
|
|
1115
1543
|
return;
|
|
1116
1544
|
}
|
|
1117
1545
|
const refresh = !!frame.delegationMcp &&
|
|
1118
|
-
(
|
|
1546
|
+
(delegation?.leaseId !== delegationLeaseId(frame.delegationMcp) || delegation?.refreshPending === true);
|
|
1119
1547
|
if (refresh) {
|
|
1120
1548
|
if (!existing.nativeSessionId || (frame.nativeSessionId && frame.nativeSessionId !== existing.nativeSessionId)) {
|
|
1121
1549
|
this.emit({ t: 'agent_opened', id: frame.id, ok: false, error: 'Managed delegation cannot reload the original session' });
|
|
1122
1550
|
return;
|
|
1123
1551
|
}
|
|
1124
|
-
if (existing.session.liveness.turnRunning || activityIsBusy(existing.
|
|
1125
|
-
|
|
1552
|
+
if (existing.session.liveness.turnRunning || activityIsBusy(existing.session.activityRaw)) {
|
|
1553
|
+
if (!delegation?.refreshPending) {
|
|
1554
|
+
try {
|
|
1555
|
+
await existing.session.note({ type: 'agent_delegation_refresh_pending' });
|
|
1556
|
+
}
|
|
1557
|
+
catch (err) {
|
|
1558
|
+
this.emit({ t: 'agent_opened', id: frame.id, ok: false, error: errorMessage(err) });
|
|
1559
|
+
return;
|
|
1560
|
+
}
|
|
1561
|
+
}
|
|
1126
1562
|
if (!await this.resumeAndReply(existing, frame, false))
|
|
1127
1563
|
return;
|
|
1128
1564
|
this.replyNoReplayNeeded(frame);
|
|
@@ -1132,6 +1568,10 @@ export class AgentSessionManager {
|
|
|
1132
1568
|
retireForRefresh = existing;
|
|
1133
1569
|
}
|
|
1134
1570
|
else {
|
|
1571
|
+
// Spec §6.3: a session with a switch pending (a persistent intent: another adapter build, or off an older keeper-acp
|
|
1572
|
+
// generation) moves at this open — the only moment its spawn secrets are at hand — when it is strictly idle.
|
|
1573
|
+
if (await this.switchAtOpen(existing, frame))
|
|
1574
|
+
return;
|
|
1135
1575
|
// Reusing a live session: neither the process nor the native session moved.
|
|
1136
1576
|
if (!await this.resumeAndReply(existing, frame, false))
|
|
1137
1577
|
return;
|
|
@@ -1149,8 +1589,9 @@ export class AgentSessionManager {
|
|
|
1149
1589
|
// Piggyback on the attempt already running, and report its real failure — not a generic one.
|
|
1150
1590
|
const { live, error, agentRefused } = await inFlight;
|
|
1151
1591
|
if (live) {
|
|
1152
|
-
|
|
1153
|
-
|
|
1592
|
+
const delegation = live.session.delegation ?? undefined;
|
|
1593
|
+
if (!!frame.delegationMcp !== !!delegation ||
|
|
1594
|
+
(frame.delegationMcp && delegation?.leaseId !== delegationLeaseId(frame.delegationMcp))) {
|
|
1154
1595
|
this.emit({ t: 'agent_opened', id: frame.id, ok: false, error: 'Managed delegation lease does not match the live session' });
|
|
1155
1596
|
return;
|
|
1156
1597
|
}
|
|
@@ -1177,14 +1618,15 @@ export class AgentSessionManager {
|
|
|
1177
1618
|
}
|
|
1178
1619
|
return;
|
|
1179
1620
|
}
|
|
1621
|
+
// A respawn of a dead keeper session keeps its (older) generation held across the retire and the spawn (Task 21 fix round 2).
|
|
1622
|
+
const releaseHold = retireForRefresh && this.needsRespawn(retireForRefresh) ? this.agents.holdForReopen?.(frame.sessionRef) : undefined;
|
|
1180
1623
|
const attempt = (async () => {
|
|
1181
1624
|
if (retireForRefresh)
|
|
1182
1625
|
await this.retire(retireForRefresh, 'keep');
|
|
1183
1626
|
return this.openSession(openFrame);
|
|
1184
1627
|
})()
|
|
1185
|
-
.catch((err) => ({
|
|
1186
|
-
|
|
1187
|
-
}));
|
|
1628
|
+
.catch((err) => ({ live: null, error: openFailureText(openFrame, err) }))
|
|
1629
|
+
.finally(() => releaseHold?.());
|
|
1188
1630
|
this.opening.set(frame.sessionRef, attempt);
|
|
1189
1631
|
let outcome;
|
|
1190
1632
|
try {
|
|
@@ -1228,6 +1670,332 @@ export class AgentSessionManager {
|
|
|
1228
1670
|
});
|
|
1229
1671
|
}
|
|
1230
1672
|
}
|
|
1673
|
+
// ---- switching (Task 24, spec §6.3) ---------------------------------------------------------------------------------------
|
|
1674
|
+
/** Why `live` may not be switched now (the strict idle criterion, `switching.ts`); empty = strictly idle. */
|
|
1675
|
+
switchBlockersOf(live, now) {
|
|
1676
|
+
const ref = live.sessionRef;
|
|
1677
|
+
return switchBlockers({
|
|
1678
|
+
session: live.session,
|
|
1679
|
+
lastActivityAt: live.lastActivityAt,
|
|
1680
|
+
busyRef: this.opening.has(ref) || this.retiring.has(ref) || this.adopting.has(ref) || this.switching.has(ref),
|
|
1681
|
+
now, quietMs: this.switchQuietMs,
|
|
1682
|
+
});
|
|
1683
|
+
}
|
|
1684
|
+
/**
|
|
1685
|
+
* An `agent_open` of a held session with a switch pending (spec §6.3): when the session is strictly idle, it is moved here — the
|
|
1686
|
+
* open carries the spawn environment only the server has — and this open is answered with the session that holds the ref
|
|
1687
|
+
* afterwards (`reloaded`; the server takes its runtime diagnostics whole). Every other frame of the ref is parked meanwhile.
|
|
1688
|
+
* `false`: nothing to switch now (no intent, not idle, draining) — the caller reuses the session as usual.
|
|
1689
|
+
*/
|
|
1690
|
+
async switchAtOpen(live, frame) {
|
|
1691
|
+
const agents = this.agents;
|
|
1692
|
+
if (!agents.switchSession || !agents.pendingSwitchFor || frame.delegationMcp)
|
|
1693
|
+
return false;
|
|
1694
|
+
// A switch spawns an adapter: new work, refused while the workstation drains (spec §8.2).
|
|
1695
|
+
if (this.tasks && !this.tasks.accepting)
|
|
1696
|
+
return false;
|
|
1697
|
+
let target;
|
|
1698
|
+
try {
|
|
1699
|
+
target = await agents.pendingSwitchFor(live.sessionRef, live.session);
|
|
1700
|
+
}
|
|
1701
|
+
catch {
|
|
1702
|
+
return false;
|
|
1703
|
+
}
|
|
1704
|
+
if (!target || live.closed || this.sessions.get(live.sessionRef) !== live)
|
|
1705
|
+
return false;
|
|
1706
|
+
const blockers = this.switchBlockersOf(live, Date.now());
|
|
1707
|
+
if (blockers.length > 0) {
|
|
1708
|
+
console.info(`[agent] ${live.sessionRef}: switch (${target.reason}) waits: ${blockers.join('; ')}`);
|
|
1709
|
+
return false;
|
|
1710
|
+
}
|
|
1711
|
+
const ref = live.sessionRef;
|
|
1712
|
+
// Like an open, the switch is counted while it runs (spec §8.2: a half-made keeper session would be stopped by a restart) — and
|
|
1713
|
+
// a workstation that stopped admitting refuses it.
|
|
1714
|
+
if (this.tasks && !this.tasks.tryReserveAgentOpen(ref))
|
|
1715
|
+
return false;
|
|
1716
|
+
let finished;
|
|
1717
|
+
this.switching.set(ref, new Promise((resolve) => { finished = resolve; }));
|
|
1718
|
+
try {
|
|
1719
|
+
const resumeFrom = resumeFromSeqOf(frame);
|
|
1720
|
+
const outcome = await agents.switchSession(live.session, {
|
|
1721
|
+
...openSpecOf(frame), seqBase: () => resumeFrom,
|
|
1722
|
+
...(live.nativeSessionId ? { nativeSessionId: live.nativeSessionId } : {}),
|
|
1723
|
+
// Forced whatever the frame says (spec §5.5.4): a concurrent open can win the server's reopen without the flag.
|
|
1724
|
+
requireSessionLoad: true,
|
|
1725
|
+
}, { ...target, proceed: () => !this.tasks?.isForceStopping() && !live.closed });
|
|
1726
|
+
await this.afterSwitch(live, frame, outcome);
|
|
1727
|
+
return true;
|
|
1728
|
+
}
|
|
1729
|
+
finally {
|
|
1730
|
+
this.tasks?.agentOpenFinished(ref);
|
|
1731
|
+
this.switching.delete(ref);
|
|
1732
|
+
finished();
|
|
1733
|
+
}
|
|
1734
|
+
}
|
|
1735
|
+
async afterSwitch(live, frame, outcome) {
|
|
1736
|
+
switch (outcome.kind) {
|
|
1737
|
+
case 'unmovable':
|
|
1738
|
+
// Nothing was torn down: the session goes on in its process, as for any reopen of a live session.
|
|
1739
|
+
if (!await this.resumeAndReply(live, frame, false))
|
|
1740
|
+
return;
|
|
1741
|
+
this.replyNoReplayNeeded(frame);
|
|
1742
|
+
return;
|
|
1743
|
+
case 'switched':
|
|
1744
|
+
case 'rolledBack': {
|
|
1745
|
+
const next = this.replaceSession(live, outcome.session, outcome.nativeSessionId);
|
|
1746
|
+
if (!await this.resumeAndReply(next, frame, true))
|
|
1747
|
+
return;
|
|
1748
|
+
this.replyNoReplayNeeded(frame);
|
|
1749
|
+
return;
|
|
1750
|
+
}
|
|
1751
|
+
case 'lost': {
|
|
1752
|
+
// Its adapter is gone and none could load it: retired like a degraded session (outbox kept), the server told. Its next
|
|
1753
|
+
// `agent_open` reopens it the ordinary way.
|
|
1754
|
+
this.unhold(live);
|
|
1755
|
+
this.notifySwept(live.sessionRef);
|
|
1756
|
+
const error = `The agent session could not be moved to another adapter process: ${outcome.error}`;
|
|
1757
|
+
this.emit({ t: 'agent_opened', id: frame.id, ok: false, error });
|
|
1758
|
+
if (frame.backfill)
|
|
1759
|
+
this.emit({ t: 'agent_backfill_done', id: frame.id, ok: false, count: 0, error });
|
|
1760
|
+
}
|
|
1761
|
+
}
|
|
1762
|
+
}
|
|
1763
|
+
/** Stop holding `live` (its session is already closed by whoever ended it). */
|
|
1764
|
+
unhold(live) {
|
|
1765
|
+
// Replaced by a switch's successor or rollback, or lost by a switch: a different process from now on.
|
|
1766
|
+
this.adapterCrashes.delete(live.sessionRef);
|
|
1767
|
+
live.closed = true;
|
|
1768
|
+
live.unwatch();
|
|
1769
|
+
if (this.sessions.get(live.sessionRef) === live)
|
|
1770
|
+
this.sessions.delete(live.sessionRef);
|
|
1771
|
+
this.tasks?.unregisterAgentSession(live.sessionRef);
|
|
1772
|
+
}
|
|
1773
|
+
/** The ref is now held by `session` (a switch's successor or its rollback): registered exactly like an opened session. */
|
|
1774
|
+
replaceSession(live, session, nativeSessionId) {
|
|
1775
|
+
this.unhold(live);
|
|
1776
|
+
let highestScopedJournalSeq = 0;
|
|
1777
|
+
for (const item of session.outbox.pending) {
|
|
1778
|
+
if (item.frame.t === 'agent_event' && item.frame.acpSessionId != null)
|
|
1779
|
+
highestScopedJournalSeq = Math.max(highestScopedJournalSeq, item.seq);
|
|
1780
|
+
}
|
|
1781
|
+
const next = {
|
|
1782
|
+
sessionRef: live.sessionRef, agentId: live.agentId, agentKey: live.agentKey, session, nativeSessionId,
|
|
1783
|
+
scopedReplayBlocked: false, highestScopedJournalSeq, unwatch: () => { }, replays: Promise.resolve(), closed: false,
|
|
1784
|
+
lastActivityAt: Date.now(),
|
|
1785
|
+
};
|
|
1786
|
+
this.register(next);
|
|
1787
|
+
return next;
|
|
1788
|
+
}
|
|
1789
|
+
/**
|
|
1790
|
+
* The daemon's switch scheduler (spec §6.3 step 7; the daemon calls it every few seconds): for each held session with a switch
|
|
1791
|
+
* pending that is strictly idle, ask the server for an `agent_open` (`agent_reopen_request`, wire ≥ 25 only) — the switch happens
|
|
1792
|
+
* at that open. Also (Task 23's carry): a keeper-hosted session whose adapter keeper-acp lost (`keeper_lost`/`keeper_stopped` —
|
|
1793
|
+
* not an exit of the adapter's own, which would loop) gets its reopen asked for, so it is respawned and loaded without waiting for
|
|
1794
|
+
* the user's next message — at most {@link ADAPTER_LOST_REOPEN_ATTEMPTS} times per lost spawn, backing off from
|
|
1795
|
+
* {@link REOPEN_REQUEST_RETRY_MS}, never while draining. A switch request the server did not answer is asked again after
|
|
1796
|
+
* {@link REOPEN_REQUEST_RETRY_MS}. Returns the refs a request went out for. Never rejects.
|
|
1797
|
+
*/
|
|
1798
|
+
async pollSwitches(now = Date.now(), only) {
|
|
1799
|
+
await this.adoptLate(); // the periodic tick also retries a keeper-acp listing that failed (final review finding 3)
|
|
1800
|
+
const requested = [];
|
|
1801
|
+
for (const live of [...this.sessions.values()]) {
|
|
1802
|
+
const ref = live.sessionRef;
|
|
1803
|
+
if (live.closed || (only && !only.has(ref)))
|
|
1804
|
+
continue;
|
|
1805
|
+
const last = this.reopenRequests.get(ref);
|
|
1806
|
+
const recently = (why) => last !== undefined && last.why === why && now - last.at < REOPEN_REQUEST_RETRY_MS * 2 ** Math.max(0, last.attempts - 1);
|
|
1807
|
+
const busyRef = this.opening.has(ref) || this.retiring.has(ref) || this.adopting.has(ref) || this.switching.has(ref);
|
|
1808
|
+
const session = live.session;
|
|
1809
|
+
const exit = session.adapterExit;
|
|
1810
|
+
if (session.durable && exit && !session.liveness.agentAlive && !session.isDegraded) {
|
|
1811
|
+
// Only a session that had a turn or a card open when keeper-acp lost its adapter (what this daemon saw fold): an idle one
|
|
1812
|
+
// was reported lost and waits for its next message — a keeper-acp restart must not respawn every idle session at once.
|
|
1813
|
+
const lostByKeeper = (exit.reason === 'keeper_lost' || exit.reason === 'keeper_stopped') &&
|
|
1814
|
+
live.adapterLost?.spawnId === exit.spawnId && live.adapterLost?.busy === true;
|
|
1815
|
+
const why = `adapter-lost:${exit.spawnId ?? '?'}`;
|
|
1816
|
+
// Bounded per lost spawn (fix round 1): 60 s, 120 s, 240 s apart, then the user's next message reopens it. Not while
|
|
1817
|
+
// draining: a respawn is new work (spec §8.2).
|
|
1818
|
+
const attempts = last?.why === why ? last.attempts : 0;
|
|
1819
|
+
if (lostByKeeper && live.nativeSessionId && !busyRef && !session.liveness.turnRunning && !recently(why) &&
|
|
1820
|
+
attempts < ADAPTER_LOST_REOPEN_ATTEMPTS && (!this.tasks || this.tasks.accepting) && this.requestReopen(ref, 'agent_exited')) {
|
|
1821
|
+
this.reopenRequests.set(ref, { at: now, why, attempts: attempts + 1 });
|
|
1822
|
+
requested.push(ref);
|
|
1823
|
+
}
|
|
1824
|
+
continue;
|
|
1825
|
+
}
|
|
1826
|
+
if (!this.agents.pendingSwitchFor)
|
|
1827
|
+
continue;
|
|
1828
|
+
let target;
|
|
1829
|
+
try {
|
|
1830
|
+
target = await this.agents.pendingSwitchFor(ref, live.session);
|
|
1831
|
+
}
|
|
1832
|
+
catch {
|
|
1833
|
+
continue;
|
|
1834
|
+
}
|
|
1835
|
+
if (!target || live.closed || (this.tasks && !this.tasks.accepting))
|
|
1836
|
+
continue;
|
|
1837
|
+
if (this.switchBlockersOf(live, now).length > 0 || recently('switch'))
|
|
1838
|
+
continue;
|
|
1839
|
+
if (this.requestReopen(ref, target.reason)) {
|
|
1840
|
+
this.reopenRequests.set(ref, { at: now, why: 'switch', attempts: 1 });
|
|
1841
|
+
requested.push(ref);
|
|
1842
|
+
}
|
|
1843
|
+
}
|
|
1844
|
+
return requested;
|
|
1845
|
+
}
|
|
1846
|
+
/**
|
|
1847
|
+
* "Move idle sessions" (Task 25's `agent_switch_idle`, spec §6.4): every held session of `agentId` that runs another build than
|
|
1848
|
+
* `toBuild` (else the agent's current build) gets a persistent intent to move there; those already strictly idle get their
|
|
1849
|
+
* `agent_reopen_request` at once, the others when they become idle. A session that can never be moved this way
|
|
1850
|
+
* ({@link unmovableWhy}: a local-fallback session, a managed delegation, no native session, a target that already refused it) gets
|
|
1851
|
+
* no intent — it would only be asked to reopen again and again — and is listed in `unmovable` with the reason. A `toBuild` that is
|
|
1852
|
+
* not installed for the agent is refused (throws). Returns the refs registered, the ones a request went out for, and the unmovable.
|
|
1853
|
+
*/
|
|
1854
|
+
async switchIdle(agentId, options = {}) {
|
|
1855
|
+
const { requestSwitch, placementOf } = this.agents;
|
|
1856
|
+
if (!requestSwitch || !placementOf)
|
|
1857
|
+
return { registered: [], requested: [], unmovable: [] };
|
|
1858
|
+
let target = options.toBuild;
|
|
1859
|
+
if (this.agents.buildIdsOf) {
|
|
1860
|
+
const ids = await this.agents.buildIdsOf(agentId);
|
|
1861
|
+
if (target !== undefined && !ids.installed.includes(target))
|
|
1862
|
+
throw new Error(`Adapter build ${target} of '${agentId}' is not installed on this workstation`);
|
|
1863
|
+
target ??= ids.current;
|
|
1864
|
+
}
|
|
1865
|
+
const reason = options.reason ?? (options.toBuild ? `move to adapter build ${options.toBuild}` : 'move to the current adapter build');
|
|
1866
|
+
const registered = [];
|
|
1867
|
+
const unmovable = [];
|
|
1868
|
+
for (const live of [...this.sessions.values()]) {
|
|
1869
|
+
if (live.closed || live.agentId !== agentId)
|
|
1870
|
+
continue;
|
|
1871
|
+
const placement = placementOf.call(this.agents, live.session);
|
|
1872
|
+
// A command override (a custom agent) has no build to move — and the scan does not count it either.
|
|
1873
|
+
if (placement && placement.buildId === undefined)
|
|
1874
|
+
continue;
|
|
1875
|
+
// Already there: nothing to move, and nothing to report as unmovable either.
|
|
1876
|
+
if (target !== undefined && placement?.buildId === target)
|
|
1877
|
+
continue;
|
|
1878
|
+
const why = this.unmovableWhy(live, target);
|
|
1879
|
+
if (why !== undefined) {
|
|
1880
|
+
console.info(`[agent] ${live.sessionRef}: not moved to ${target ?? 'the current build'}: ${why.detail ?? why.plain}`);
|
|
1881
|
+
unmovable.push({ sessionRef: live.sessionRef, reason: why.plain });
|
|
1882
|
+
continue;
|
|
1883
|
+
}
|
|
1884
|
+
await requestSwitch.call(this.agents, agentId, live.sessionRef, { ...(options.toBuild ? { toBuild: options.toBuild } : {}), reason, requestedBy: 'user' });
|
|
1885
|
+
registered.push(live.sessionRef);
|
|
1886
|
+
}
|
|
1887
|
+
const requested = await this.pollSwitches(Date.now(), new Set(registered));
|
|
1888
|
+
return { registered, requested, unmovable };
|
|
1889
|
+
}
|
|
1890
|
+
/**
|
|
1891
|
+
* Why `live` can never be moved to `target` by a switch (spec §6.3 "当前限制"), or `undefined`: a session keeper-acp does not host
|
|
1892
|
+
* (the local fallback), a managed delegation, a session with no native session to load, or one a switch to the same build already
|
|
1893
|
+
* found unmovable. `plain` is what the scan card and the result frame say (Task 25); `detail` is the switch's own words, for the
|
|
1894
|
+
* log, when there are any.
|
|
1895
|
+
*/
|
|
1896
|
+
unmovableWhy(live, target) {
|
|
1897
|
+
const placement = this.agents.placementOf?.call(this.agents, live.session);
|
|
1898
|
+
if (!placement?.keeper)
|
|
1899
|
+
return { plain: LOCAL_SESSION_UNMOVABLE };
|
|
1900
|
+
if (live.session.delegation)
|
|
1901
|
+
return { plain: 'it is a delegated session, which can\'t be moved to another build yet' };
|
|
1902
|
+
if (!live.nativeSessionId)
|
|
1903
|
+
return { plain: 'it has no saved conversation the new build could reload' };
|
|
1904
|
+
const refused = this.agents.unmovableFor?.call(this.agents, live.sessionRef);
|
|
1905
|
+
if (refused && (refused.toBuild === undefined || refused.toBuild === target)) {
|
|
1906
|
+
return { plain: `the ${refused.toBuild ?? 'new'} adapter could not take it over when this was last tried`, detail: refused.reason };
|
|
1907
|
+
}
|
|
1908
|
+
return undefined;
|
|
1909
|
+
}
|
|
1910
|
+
/**
|
|
1911
|
+
* How the external-agents scan counts one running session (spec §6.4, Task 25; `AcpAgents.roster({sessions})`): `unmovable` with
|
|
1912
|
+
* the reason ({@link unmovableWhy}; also a session this daemon does not hold), else `busy` with the strict idle criterion's blockers
|
|
1913
|
+
* ({@link switchBlockersOf}), else `idle`. `pendingTo`: the build a pending switch intent still owes it. Never rejects.
|
|
1914
|
+
*/
|
|
1915
|
+
async rosterSessionState(running) {
|
|
1916
|
+
const live = this.sessions.get(running.sessionRef);
|
|
1917
|
+
if (!live || live.closed)
|
|
1918
|
+
return { state: 'unmovable', reasons: ['it is not connected to a conversation right now'] };
|
|
1919
|
+
const current = this.agents.buildIdsOf ? (await this.agents.buildIdsOf(running.agentId).catch(() => undefined))?.current : undefined;
|
|
1920
|
+
let pendingTo;
|
|
1921
|
+
try {
|
|
1922
|
+
pendingTo = (await this.agents.pendingSwitchFor?.(running.sessionRef, live.session))?.toBuild;
|
|
1923
|
+
}
|
|
1924
|
+
catch { /* an unreadable builds.json: nothing pending to report */ }
|
|
1925
|
+
const pending = pendingTo !== undefined ? { pendingTo } : {};
|
|
1926
|
+
const why = this.unmovableWhy(live, current);
|
|
1927
|
+
if (why !== undefined)
|
|
1928
|
+
return { state: 'unmovable', reasons: [why.plain], ...pending };
|
|
1929
|
+
// The card's words, not the log's (`plainSwitchBlocker`); `switchAtOpen` still logs the internal sentences.
|
|
1930
|
+
const blockers = this.switchBlockersOf(live, Date.now()).map(plainSwitchBlocker);
|
|
1931
|
+
return blockers.length > 0 ? { state: 'busy', reasons: blockers, ...pending } : { state: 'idle', ...pending };
|
|
1932
|
+
}
|
|
1933
|
+
/** Answers `agent_switch_idle` (Task 25): {@link switchIdle}, counted. */
|
|
1934
|
+
async onSwitchIdle(frame) {
|
|
1935
|
+
try {
|
|
1936
|
+
const result = await this.switchIdle(frame.bridge, frame.toBuild !== undefined ? { toBuild: frame.toBuild } : {});
|
|
1937
|
+
this.emit({
|
|
1938
|
+
t: 'agent_switch_idle_result', id: frame.id, ok: true,
|
|
1939
|
+
registered: result.registered.length, immediate: result.requested.length, unmovable: result.unmovable,
|
|
1940
|
+
});
|
|
1941
|
+
}
|
|
1942
|
+
catch (error) {
|
|
1943
|
+
this.emit({ t: 'agent_switch_idle_result', id: frame.id, ok: false, error: errorMessage(error) });
|
|
1944
|
+
}
|
|
1945
|
+
}
|
|
1946
|
+
/**
|
|
1947
|
+
* The Workstations page's view of a held session (Task 26, spec §8.1): which registry agent it runs and whether it is busy right
|
|
1948
|
+
* now — a turn in flight or the agent reporting itself active (the same sample `activeAgentTasks` once used). `undefined` when this
|
|
1949
|
+
* daemon does not hold the ref.
|
|
1950
|
+
*/
|
|
1951
|
+
sessionView(sessionRef) {
|
|
1952
|
+
const live = this.sessions.get(sessionRef);
|
|
1953
|
+
if (!live || live.closed)
|
|
1954
|
+
return undefined;
|
|
1955
|
+
let busy = false;
|
|
1956
|
+
try {
|
|
1957
|
+
busy = live.session.liveness.turnRunning || activityIsBusy(live.session.activityRaw);
|
|
1958
|
+
}
|
|
1959
|
+
catch {
|
|
1960
|
+
busy = false;
|
|
1961
|
+
}
|
|
1962
|
+
return { agentId: live.agentId, busy };
|
|
1963
|
+
}
|
|
1964
|
+
/**
|
|
1965
|
+
* keeper-acp replacement (spec §8.3, for Task 26): every held session on an older keeper-acp generation — only on `gens` when given
|
|
1966
|
+
* (the generations a replacement was decided for; another older one may merely be `available`) — gets a persistent intent to move
|
|
1967
|
+
* to the current generation when idle (same build). Returns the refs registered.
|
|
1968
|
+
*/
|
|
1969
|
+
async moveOffOlderKeeperGenerations(reason = 'keeper-acp was upgraded', gens) {
|
|
1970
|
+
const { requestSwitch, placementOf } = this.agents;
|
|
1971
|
+
if (!requestSwitch || !placementOf)
|
|
1972
|
+
return [];
|
|
1973
|
+
const registered = [];
|
|
1974
|
+
for (const live of [...this.sessions.values()]) {
|
|
1975
|
+
const placement = placementOf.call(this.agents, live.session);
|
|
1976
|
+
if (live.closed || !placement?.keeper || placement.gen === undefined)
|
|
1977
|
+
continue;
|
|
1978
|
+
if (gens && !gens.includes(placement.gen))
|
|
1979
|
+
continue;
|
|
1980
|
+
await requestSwitch.call(this.agents, live.agentId, live.sessionRef, { keeperGen: 'current', reason, requestedBy: 'axom' });
|
|
1981
|
+
registered.push(live.sessionRef);
|
|
1982
|
+
}
|
|
1983
|
+
await this.pollSwitches(Date.now(), new Set(registered));
|
|
1984
|
+
return registered;
|
|
1985
|
+
}
|
|
1986
|
+
/** Cancel a pending switch: one ref's, or every one of an agent's. `true` when one existed. */
|
|
1987
|
+
async cancelSwitch(target) {
|
|
1988
|
+
if (!this.agents.cancelSwitch)
|
|
1989
|
+
return false;
|
|
1990
|
+
const cancelled = await this.agents.cancelSwitch(target);
|
|
1991
|
+
if ('sessionRef' in target)
|
|
1992
|
+
this.reopenRequests.delete(target.sessionRef);
|
|
1993
|
+
else
|
|
1994
|
+
for (const live of this.sessions.values())
|
|
1995
|
+
if (live.agentId === target.agentId)
|
|
1996
|
+
this.reopenRequests.delete(live.sessionRef);
|
|
1997
|
+
return cancelled;
|
|
1998
|
+
}
|
|
1231
1999
|
/**
|
|
1232
2000
|
* Answers a backfill-flagged open that landed on an already-live (or already-being-opened)
|
|
1233
2001
|
* session, without replaying anything.
|
|
@@ -1254,8 +2022,8 @@ export class AgentSessionManager {
|
|
|
1254
2022
|
* and `agent_open` already has the whole open path (spec construction, ok/error reply) that a
|
|
1255
2023
|
* standalone frame would force the daemon to rebuild a second copy of.
|
|
1256
2024
|
*
|
|
1257
|
-
* Deliberately outside the live event/
|
|
1258
|
-
*
|
|
2025
|
+
* Deliberately outside the live event/outbox machinery — no `bridge_seq`, no
|
|
2026
|
+
* outbox entry, no `agent_event_ack` round trip. `bridge_seq` means one specific
|
|
1259
2027
|
* thing: live-consumption progress, the only basis for de-duplication across reconnects. Backfill
|
|
1260
2028
|
* is a one-shot replay correlated purely by this open's `id`; folding it into that watermark would
|
|
1261
2029
|
* give it two meanings, which is exactly what produced two watermark bugs in an earlier phase.
|
|
@@ -1289,6 +2057,8 @@ export class AgentSessionManager {
|
|
|
1289
2057
|
batch.push(event);
|
|
1290
2058
|
count += 1;
|
|
1291
2059
|
if (batch.length >= BACKFILL_BATCH_SIZE) {
|
|
2060
|
+
// Bulk and never acknowledged: no session window, but it waits for the socket gate like session frames (agentFlow.ts).
|
|
2061
|
+
await this.gate.opened();
|
|
1292
2062
|
this.emit({ t: 'agent_backfill_events', id, events: batch });
|
|
1293
2063
|
batch = [];
|
|
1294
2064
|
}
|
|
@@ -1324,36 +2094,29 @@ export class AgentSessionManager {
|
|
|
1324
2094
|
`but the live one is '${live.nativeSessionId}'`);
|
|
1325
2095
|
}
|
|
1326
2096
|
const resumeFrom = resumeFromSeqOf(frame);
|
|
1327
|
-
//
|
|
1328
|
-
// the
|
|
2097
|
+
// The server applied through `resumeFrom` (its durable watermark): that is an ack. Everything after it is resent in order —
|
|
2098
|
+
// verbatim, carrying the original seq, so the server's de-duplication does the deciding. The outbox never drops its oldest
|
|
2099
|
+
// entries (the backlog stays in the raw log instead, §5.6), so there is no gap to announce any more.
|
|
1329
2100
|
let incompatible = false;
|
|
1330
|
-
|
|
2101
|
+
// One replay at a time per session: two racing reopens resend the tail once, the second only what the first did not cover.
|
|
2102
|
+
const replay = live.replays.then(async () => {
|
|
1331
2103
|
if (live.closed)
|
|
1332
2104
|
return;
|
|
1333
2105
|
incompatible = this.blockIncompatibleJournal(live);
|
|
1334
2106
|
if (incompatible)
|
|
1335
2107
|
return;
|
|
1336
|
-
|
|
2108
|
+
await this.acknowledge(live, resumeFrom);
|
|
1337
2109
|
if (live.closed)
|
|
1338
2110
|
return;
|
|
1339
|
-
if (this.hasGap(live, entries, resumeFrom)) {
|
|
1340
|
-
this.emit({
|
|
1341
|
-
t: 'agent_event',
|
|
1342
|
-
sessionRef: live.sessionRef,
|
|
1343
|
-
// The hole left by the overflow drop starts exactly here. Reusing that seq (rather than
|
|
1344
|
-
// taking a fresh one) is what makes the notice survive the server's `seq > bridgeSeq`
|
|
1345
|
-
// filter *without* pushing the watermark past the replayed entries behind it.
|
|
1346
|
-
seq: resumeFrom + 1,
|
|
1347
|
-
eventName: 'step',
|
|
1348
|
-
payload: { message: 'transport gap: oldest streamed output dropped', tone: 'neutral' },
|
|
1349
|
-
});
|
|
1350
|
-
}
|
|
1351
|
-
// Already-formed frames, carrying their original seq — resent verbatim so the server's
|
|
1352
|
-
// de-duplication does the deciding.
|
|
1353
|
-
for (const entry of entries)
|
|
1354
|
-
this.emit(entry.frame);
|
|
1355
2111
|
live.scopedReplayBlocked = false;
|
|
2112
|
+
// Sends synchronously while the window and the socket allow, so on an idle link the tail precedes `agent_opened`; on a
|
|
2113
|
+
// busy one `agent_opened` may overtake it — the session's frames keep their own order (analysis B §4.3), and a large tail
|
|
2114
|
+
// cannot push the reply past the server's open timeout.
|
|
2115
|
+
live.session.outbox.resendAll();
|
|
2116
|
+
this.pump();
|
|
1356
2117
|
});
|
|
2118
|
+
live.replays = replay.catch((err) => console.error(`[agent] replay failed for ${live.sessionRef}: ${errorMessage(err)}`));
|
|
2119
|
+
await live.replays;
|
|
1357
2120
|
if (incompatible) {
|
|
1358
2121
|
const error = `Scoped journal replay requires agent wire ${AGENT_WIRE_SCOPED_ORIGIN_VERSION}; retained history was not delivered or acknowledged`;
|
|
1359
2122
|
this.emit({ t: 'agent_opened', id: frame.id, ok: false, error });
|
|
@@ -1365,6 +2128,8 @@ export class AgentSessionManager {
|
|
|
1365
2128
|
this.emit({ t: 'agent_opened', id: frame.id, ok: false, error: 'session was closed while opening' });
|
|
1366
2129
|
return false;
|
|
1367
2130
|
}
|
|
2131
|
+
const delegation = live.session.delegation ?? undefined;
|
|
2132
|
+
const activityRaw = live.session.activityRaw;
|
|
1368
2133
|
this.emit({
|
|
1369
2134
|
t: 'agent_opened',
|
|
1370
2135
|
id: frame.id,
|
|
@@ -1375,11 +2140,12 @@ export class AgentSessionManager {
|
|
|
1375
2140
|
// Emitted only when true, the same `supportsSteering` convention as below: absent is the
|
|
1376
2141
|
// field's only "no", and it is also what an older peer sends.
|
|
1377
2142
|
...(reloaded ? { reloaded: true } : {}),
|
|
1378
|
-
|
|
1379
|
-
...(
|
|
1380
|
-
...(
|
|
1381
|
-
|
|
1382
|
-
|
|
2143
|
+
// Delegation's non-secret fields, from processor state (Task 19: `agent_opened` is assembled from state).
|
|
2144
|
+
...(delegation ? { delegationExpiresAtEpochMs: delegation.expiresAtEpochMs } : {}),
|
|
2145
|
+
...(delegation?.refreshPending ? { delegationRefreshPending: true } : {}),
|
|
2146
|
+
...(this.serverAgentWire >= AGENT_WIRE_DELEGATION_RENEW_VERSION && delegation?.instanceId ? {
|
|
2147
|
+
delegationInstanceId: delegation.instanceId, delegationBindingId: delegation.bindingId,
|
|
2148
|
+
delegationRenewalRevision: delegation.renewalRevision,
|
|
1383
2149
|
} : {}),
|
|
1384
2150
|
// The server learns this here and nowhere else. It used to be published only on the roster in
|
|
1385
2151
|
// the `agent_hello` handshake, which is a snapshot taken *before* any agent was connected to —
|
|
@@ -1441,91 +2207,72 @@ export class AgentSessionManager {
|
|
|
1441
2207
|
...(reportsActivity(live.session.agentInfo?.name) ? { reportsActivity: true } : {}),
|
|
1442
2208
|
...(live.nativeSessionId && supportsAsyncTaskStop(live.session.agentInfo?.name)
|
|
1443
2209
|
? { supportsAsyncTaskStop: true } : {}),
|
|
1444
|
-
//
|
|
2210
|
+
// A keeper-hosted session outlives this daemon (spec §5.5.4): the server resends answers it recorded for the cards still
|
|
2211
|
+
// pending (a daemon that died between receiving an answer and writing it would leave the adapter blocked), and decides an
|
|
2212
|
+
// unconfirmed steer by whether it was steered. Additive keys: a server that does not read them ignores them.
|
|
2213
|
+
...(live.session.durable ? {
|
|
2214
|
+
durable: true,
|
|
2215
|
+
pendingRequestIds: [...(live.session.pendingRequestIds ?? [])],
|
|
2216
|
+
steeredIds: [...(live.session.steeredIds ?? [])],
|
|
2217
|
+
} : {}),
|
|
2218
|
+
// This session's activity-state snapshot as of right now (processor state), resent here because
|
|
1445
2219
|
// `agent_session_state` is never journaled and a reopen would otherwise have nothing to
|
|
1446
|
-
// recover it from
|
|
1447
|
-
//
|
|
1448
|
-
...(
|
|
2220
|
+
// recover it from. Absent when nothing has ever arrived (including "no collector for this
|
|
2221
|
+
// agent"), never a synthesized default.
|
|
2222
|
+
...(activityRaw !== undefined ? { activityRaw } : {}),
|
|
1449
2223
|
});
|
|
1450
2224
|
return true;
|
|
1451
2225
|
}
|
|
1452
|
-
/**
|
|
1453
|
-
* Whether the overflow drop actually took something the server still needs. `droppedOldest` alone
|
|
1454
|
-
* is not enough: by the time of this reconnect the server may have acked past the hole, and
|
|
1455
|
-
* announcing a gap that no longer exists would both mislead the user and collide with the seq of
|
|
1456
|
-
* the first replayed entry.
|
|
1457
|
-
*/
|
|
1458
|
-
hasGap(live, entries, resumeFrom) {
|
|
1459
|
-
if (!live.journal.droppedOldest)
|
|
1460
|
-
return false;
|
|
1461
|
-
const first = entries[0];
|
|
1462
|
-
return first ? first.seq > resumeFrom + 1 : live.journal.lastSeq > resumeFrom;
|
|
1463
|
-
}
|
|
1464
2226
|
async openSession(frame) {
|
|
1465
|
-
// A previous incarnation of this ref may still be
|
|
1466
|
-
//
|
|
1467
|
-
//
|
|
2227
|
+
// A previous incarnation of this ref may still be closing: its adapter stopped and its transport session (raw, outbox) kept or
|
|
2228
|
+
// released — the new spawn goes under the same key (see {@link retiring}). This is the first statement on purpose — before the
|
|
2229
|
+
// agent spawns, before anything touches the transport. The one-time legacy journal migration lands before any open too.
|
|
1468
2230
|
await this.retiring.get(frame.sessionRef);
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
//
|
|
1472
|
-
//
|
|
1473
|
-
//
|
|
1474
|
-
const
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
env.PATH = [...pathPrepend, env.PATH ?? process.env.PATH ?? ''].filter(Boolean).join(path.delimiter);
|
|
1478
|
-
}
|
|
1479
|
-
// Recovered before the agent spawns, because nextSeq() is synchronous and the first event may
|
|
1480
|
-
// arrive the instant onEvent is registered: the high-water mark has to be known by then or a
|
|
1481
|
-
// restarted daemon would restart seq at 1 and the server would swallow the next turn as
|
|
1482
|
-
// already-applied. The frame's watermark is passed in as a floor under whatever the file
|
|
1483
|
-
// yields, because "the file is empty" is the *normal* state between turns — everything in it
|
|
1484
|
-
// was acked and truncated — so the file alone would say 0 while the server sits at N.
|
|
1485
|
-
// init() only reads — the file appears on the first append — so an open that fails here leaves
|
|
1486
|
-
// nothing behind.
|
|
1487
|
-
const journal = new SessionJournal(this.sessionsDir, frame.sessionRef, this.maxJournalBytes);
|
|
1488
|
-
let highestScopedJournalSeq = 0;
|
|
1489
|
-
try {
|
|
1490
|
-
await journal.init(resumeFromSeqOf(frame));
|
|
1491
|
-
// Recovery is the only full scan needed for compatibility. Appends and ACKs below maintain
|
|
1492
|
-
// the high watermark incrementally, including while a v17 connection is offline.
|
|
1493
|
-
for (const entry of await journal.entriesAfter(0)) {
|
|
1494
|
-
if (entry.frame.t === 'agent_event' && entry.frame.acpSessionId != null) {
|
|
1495
|
-
highestScopedJournalSeq = Math.max(highestScopedJournalSeq, entry.seq);
|
|
1496
|
-
}
|
|
1497
|
-
}
|
|
1498
|
-
}
|
|
1499
|
-
catch (err) {
|
|
1500
|
-
return { live: null, error: `journal init failed: ${errorMessage(err)}` };
|
|
1501
|
-
}
|
|
1502
|
-
// Both journal recovery and a retiring prior incarnation can await before this point. Only a
|
|
1503
|
-
// force-stop may cancel an open admitted before that await; graceful drain owns its reservation
|
|
1504
|
-
// and must let it settle without starting a second admission decision halfway through.
|
|
2231
|
+
await this.adopting.get(frame.sessionRef)?.done;
|
|
2232
|
+
await this.migrateLegacyJournals();
|
|
2233
|
+
// The server's watermark is the floor the processor numbers from (AcpOpenSpec.seqBase); the outbox an earlier life left under
|
|
2234
|
+
// the ref raises it (`max(outbox.maxSeq, committedTail.seq, resumeFromSeq)`, §5.5) — a restarted daemon never restarts seq at 1,
|
|
2235
|
+
// or the server would swallow the next turn as already applied.
|
|
2236
|
+
const resumeFrom = resumeFromSeqOf(frame);
|
|
2237
|
+
// Only a force-stop may cancel an open admitted before the awaits above; graceful drain owns its reservation and must let it
|
|
2238
|
+
// settle without starting a second admission decision halfway through.
|
|
1505
2239
|
if (this.tasks?.isForceStopping()) {
|
|
1506
|
-
await journal.destroy();
|
|
1507
2240
|
return { live: null, error: 'Workstation is not accepting new work' };
|
|
1508
2241
|
}
|
|
1509
2242
|
let session;
|
|
1510
2243
|
let nativeSessionId;
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
2244
|
+
// Spec 2026-10-11 §6.2: an agent with nothing installed yet is installed once, by this open. A success becomes the session's
|
|
2245
|
+
// own `agent_install_outcome` note (AcpAgents → the processor numbers one past-tense `step`, recorded in the outbox like any
|
|
2246
|
+
// event). A failure has no session to go on: its `step` is sent here, numbered right after the server's watermark — not
|
|
2247
|
+
// recorded anywhere (the open fails with the same reason, and a later open numbers from the server's watermark again).
|
|
2248
|
+
// Nothing is written while the install runs: a durable "Installing…" card would never be updated and read as stuck.
|
|
2249
|
+
let failedInstall;
|
|
2250
|
+
const onInstallOutcome = (outcome) => {
|
|
2251
|
+
if (!outcome.ok)
|
|
2252
|
+
failedInstall = outcome;
|
|
2253
|
+
};
|
|
2254
|
+
// Sent before the failed open's reply — unless an earlier life left frames under the ref: one of them may hold
|
|
2255
|
+
// `resumeFrom + 1`, and the server would keep the step and drop that frame as a duplicate. `agent_opened{ok:false}` carries
|
|
2256
|
+
// the same error then.
|
|
2257
|
+
const reportFailedInstall = async () => {
|
|
2258
|
+
if (!failedInstall)
|
|
2259
|
+
return;
|
|
2260
|
+
let leftover = false;
|
|
2261
|
+
try {
|
|
2262
|
+
leftover = ((await this.agents.leftoverSessions?.()) ?? []).includes(frame.sessionRef);
|
|
2263
|
+
}
|
|
2264
|
+
catch {
|
|
2265
|
+
leftover = true;
|
|
2266
|
+
}
|
|
2267
|
+
if (leftover)
|
|
2268
|
+
return;
|
|
2269
|
+
this.emit({
|
|
2270
|
+
t: 'agent_event', sessionRef: frame.sessionRef, seq: resumeFrom + 1, eventName: 'step',
|
|
2271
|
+
payload: { title: 'Agent adapter', message: `Could not install ${failedInstall.agentName} adapter ${failedInstall.version}`, detail: failedInstall.error, tone: 'error' },
|
|
1528
2272
|
});
|
|
2273
|
+
};
|
|
2274
|
+
try {
|
|
2275
|
+
const result = await this.agents.open({ ...openSpecOf(frame), onInstallOutcome, seqBase: () => resumeFrom });
|
|
1529
2276
|
session = result.session;
|
|
1530
2277
|
nativeSessionId = result.nativeSessionId;
|
|
1531
2278
|
if (frame.delegationMcp && !nativeSessionId) {
|
|
@@ -1534,13 +2281,14 @@ export class AgentSessionManager {
|
|
|
1534
2281
|
}
|
|
1535
2282
|
}
|
|
1536
2283
|
catch (err) {
|
|
2284
|
+
await reportFailedInstall();
|
|
1537
2285
|
// `open` throws on both sides of the agent's `initialize` — nothing to launch and a spawn
|
|
1538
2286
|
// that failed look identical from here — so the marker is asked for rather than assumed. Only
|
|
1539
2287
|
// an {@link AgentRefusedSessionError} is the agent's own no, which is what an `auth_required`
|
|
1540
2288
|
// on `session/new` arrives as; see {@link OpenOutcome.agentRefused}.
|
|
1541
2289
|
return {
|
|
1542
2290
|
live: null,
|
|
1543
|
-
error: frame
|
|
2291
|
+
error: openFailureText(frame, err),
|
|
1544
2292
|
// The code comes off the marker, not off `errorMessage(err)` above: the wrapper captured it
|
|
1545
2293
|
// from the original rejection at the moment that object still existed.
|
|
1546
2294
|
...(err instanceof AgentRefusedSessionError
|
|
@@ -1557,7 +2305,6 @@ export class AgentSessionManager {
|
|
|
1557
2305
|
catch (err) {
|
|
1558
2306
|
console.error(`[agent] close of force-stopped open failed for ${frame.sessionRef}: ${errorMessage(err)}`);
|
|
1559
2307
|
}
|
|
1560
|
-
await journal.destroy();
|
|
1561
2308
|
return { live: null, error: 'Workstation is not accepting new work' };
|
|
1562
2309
|
}
|
|
1563
2310
|
// The session became pointless while it was spawning (agent_close, or the socket dropped and the
|
|
@@ -1567,57 +2314,58 @@ export class AgentSessionManager {
|
|
|
1567
2314
|
if (abandonReason) {
|
|
1568
2315
|
this.closedDuringOpen.delete(frame.sessionRef);
|
|
1569
2316
|
try {
|
|
1570
|
-
|
|
2317
|
+
// Only an explicit agent_close retires the ref, and only then does its outbox go. A socket
|
|
2318
|
+
// drop must leave it alone: it may hold frames an earlier life left, and the server will
|
|
2319
|
+
// re-open the ref and ask for exactly those.
|
|
2320
|
+
await session.close(abandonReason === 'closed' ? 'release' : 'keep');
|
|
1571
2321
|
}
|
|
1572
2322
|
catch (err) {
|
|
1573
2323
|
console.error(`[agent] close of abandoned open failed for ${frame.sessionRef}: ${errorMessage(err)}`);
|
|
1574
2324
|
}
|
|
1575
|
-
// Only an explicit agent_close retires the ref, and only then does its journal go. A socket
|
|
1576
|
-
// drop must leave the file alone: it may hold entries this daemon recovered from a previous
|
|
1577
|
-
// life, and the server will re-open the ref and ask for exactly those.
|
|
1578
|
-
if (abandonReason === 'closed')
|
|
1579
|
-
await journal.destroy();
|
|
1580
2325
|
return { live: null, error: 'session closed during open' };
|
|
1581
2326
|
}
|
|
2327
|
+
// Frames an earlier life left (read back into the outbox at start) keep the scoped high-water mark they had.
|
|
2328
|
+
let highestScopedJournalSeq = 0;
|
|
2329
|
+
for (const item of session.outbox.pending) {
|
|
2330
|
+
if (item.frame.t === 'agent_event' && item.frame.acpSessionId != null)
|
|
2331
|
+
highestScopedJournalSeq = Math.max(highestScopedJournalSeq, item.seq);
|
|
2332
|
+
}
|
|
1582
2333
|
const live = {
|
|
1583
2334
|
sessionRef: frame.sessionRef,
|
|
1584
2335
|
agentId: frame.bridge,
|
|
1585
2336
|
agentKey: frame.agentKey,
|
|
1586
2337
|
session,
|
|
1587
2338
|
nativeSessionId,
|
|
1588
|
-
...(frame.delegationMcp ? { delegationExpiresAtEpochMs: frame.delegationMcp.expiresAtEpochMs } : {}),
|
|
1589
|
-
...(frame.delegationMcp ? { delegationLeaseId: delegationLeaseId(frame.delegationMcp) } : {}),
|
|
1590
|
-
...(frame.delegationMcp?.bindingId ? { delegationBindingId: frame.delegationMcp.bindingId,
|
|
1591
|
-
delegationInstanceId: randomUUID(), delegationRenewalRevision: 0 } : {}),
|
|
1592
|
-
journal,
|
|
1593
|
-
writes: Promise.resolve(),
|
|
1594
2339
|
scopedReplayBlocked: false,
|
|
1595
2340
|
highestScopedJournalSeq,
|
|
2341
|
+
unwatch: () => { },
|
|
2342
|
+
replays: Promise.resolve(),
|
|
1596
2343
|
closed: false,
|
|
1597
2344
|
lastActivityAt: Date.now(),
|
|
1598
2345
|
};
|
|
1599
|
-
this.
|
|
1600
|
-
|
|
2346
|
+
this.register(live);
|
|
2347
|
+
return { live };
|
|
2348
|
+
}
|
|
2349
|
+
/** A session joins: held, registered with the TaskRegistry, its frames and its outbox wired to the sender. */
|
|
2350
|
+
register(live) {
|
|
2351
|
+
const session = live.session;
|
|
2352
|
+
this.detached.delete(live.sessionRef);
|
|
2353
|
+
this.sessions.set(live.sessionRef, live);
|
|
2354
|
+
const durable = session.durable === true && session.detach !== undefined;
|
|
2355
|
+
this.tasks?.registerAgentSession(live.sessionRef,
|
|
2356
|
+
// A keeper-hosted session is detached, never stopped, by the update paths (spec §5.5.5): its turn goes on in keeper-acp.
|
|
2357
|
+
() => this.retire(live, durable ? 'detach' : 'destroy'),
|
|
1601
2358
|
// 两个层级量取或:本进程发出的提示词是否在途(agent 自己也许在续跑一个我们没发起的回合,
|
|
1602
2359
|
// 那一种由第二项回答),以及 agent 自报的状态。两者都不累积,所以没有可泄漏的状态。
|
|
1603
|
-
() => live.session.liveness.turnRunning || activityIsBusy(live.
|
|
1604
|
-
session
|
|
1605
|
-
|
|
1606
|
-
session.
|
|
1607
|
-
session.
|
|
1608
|
-
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
|
|
1612
|
-
});
|
|
1613
|
-
session.onProviderTaskEvent?.((event) => {
|
|
1614
|
-
if (live.closed || this.sessions.get(live.sessionRef) !== live || this.serverAgentWire < AGENT_WIRE_PROVIDER_TASK_VERSION ||
|
|
1615
|
-
!live.session.providerTaskAdapterSelected || event.rootSessionId !== live.nativeSessionId ||
|
|
1616
|
-
event.runtimeGeneration !== live.session.runtimeDiagnostics?.profile?.runtimeGeneration)
|
|
1617
|
-
return;
|
|
1618
|
-
this.emit({ t: 'agent_provider_task_event', sessionRef: live.sessionRef, ...event });
|
|
1619
|
-
});
|
|
1620
|
-
return { live };
|
|
2360
|
+
() => live.session.liveness.turnRunning || activityIsBusy(live.session.activityRaw), { durable });
|
|
2361
|
+
// Everything the session produces arrives as processor frames (Task 19): numbered by the processor, projected for the
|
|
2362
|
+
// connected server here, recorded in the outbox (the numbered ones) and sent from it (Task 20).
|
|
2363
|
+
live.unwatch = session.outbox.onChange(() => this.pump());
|
|
2364
|
+
session.onFrames((frames) => this.onSessionFrames(live, frames));
|
|
2365
|
+
if (session.accountLimits && this.serverAgentWire >= 27)
|
|
2366
|
+
this.emit({ t: 'agent_account_update', agentKey: live.agentKey, snapshot: session.accountLimits });
|
|
2367
|
+
session.onDegraded?.((reason) => { void this.onSessionDegraded(live, reason); });
|
|
2368
|
+
session.onAdapterExited?.((exit) => { this.onAdapterExited(live, exit); });
|
|
1621
2369
|
}
|
|
1622
2370
|
/**
|
|
1623
2371
|
* Answers `agent_list_native` by asking the agent itself, over a throwaway connection this manager
|
|
@@ -1850,180 +2598,251 @@ export class AgentSessionManager {
|
|
|
1850
2598
|
this.emit({ t: 'agent_installed', id: frame.id, ok: false, error: errorMessage(err) });
|
|
1851
2599
|
}
|
|
1852
2600
|
}
|
|
1853
|
-
onAgentEvent(live, e) {
|
|
1854
|
-
if (live.closed)
|
|
1855
|
-
return;
|
|
1856
|
-
// 这里曾经有一行:`tool_call` / `permission_request` 就登记一次「这条会话在忙」。它是在
|
|
1857
|
-
// 没有 vendor 状态信号的年代用来猜「agent 是不是在续跑一个本进程没发起的回合」的启发式,
|
|
1858
|
-
// 而它开的口子只有 `turn_done` 能关——任何一条没有配套 `turn_done` 的事件都会把这个 ref
|
|
1859
|
-
// 永久钉成忙碌。那个猜测现在有了真信号(`lastActivityRaw`,见本类的注册点),所以这一行
|
|
1860
|
-
// 不是被删掉了事,是被它当初代理的那个事实取代了。
|
|
1861
|
-
// Before the contract check: an agent emitting even non-conforming events is a live process,
|
|
1862
|
-
// and the idle sweep is about liveness, not correctness.
|
|
1863
|
-
this.touch(live);
|
|
1864
|
-
// Contract check BEFORE record(): record() takes the next seq synchronously, so a violating
|
|
1865
|
-
// event that got numbered and then dropped would leave a hole in the sequence — the server's
|
|
1866
|
-
// watermark reads a hole as lost data. Drop, don't throw: one bad event must not poison the
|
|
1867
|
-
// session, and the diagnostic (agent, session, event) is what makes the violation debuggable.
|
|
1868
|
-
const rejection = validateWireEvent(e);
|
|
1869
|
-
if (rejection) {
|
|
1870
|
-
console.warn(`[acp:${live.agentId} agent:${live.agentKey}] dropped event '${wireEventName(e)}' for ${live.sessionRef}: ` +
|
|
1871
|
-
`${rejection.reason}${rejection.detail ? ` (${rejection.detail})` : ''}`);
|
|
1872
|
-
return;
|
|
1873
|
-
}
|
|
1874
|
-
if (e.acpSessionId !== undefined && this.serverAgentWire < AGENT_WIRE_SCOPED_ORIGIN_VERSION) {
|
|
1875
|
-
if (e.eventName === 'permission_request' && typeof e.payload.requestId === 'string') {
|
|
1876
|
-
const options = e.payload.options;
|
|
1877
|
-
let cancelled = '__axom_undeliverable_permission__';
|
|
1878
|
-
while (options.some((option) => option.optionId === cancelled))
|
|
1879
|
-
cancelled += '_';
|
|
1880
|
-
live.session.answer(e.payload.requestId, cancelled);
|
|
1881
|
-
}
|
|
1882
|
-
console.warn(`[acp:${live.agentId}] scoped live event '${e.eventName}' for '${e.acpSessionId}' disabled: requires agent wire ${AGENT_WIRE_SCOPED_ORIGIN_VERSION}`);
|
|
1883
|
-
return;
|
|
1884
|
-
}
|
|
1885
|
-
this.record(live, (seq) => ({
|
|
1886
|
-
t: 'agent_event',
|
|
1887
|
-
sessionRef: live.sessionRef,
|
|
1888
|
-
seq,
|
|
1889
|
-
eventName: e.eventName,
|
|
1890
|
-
payload: e.payload,
|
|
1891
|
-
// Present only for a subagent's own output. The frame still rides this session's ref, journal
|
|
1892
|
-
// and seq — one watermark per ref (see `WireEvent.acpSessionId`); this field only says which
|
|
1893
|
-
// session on the far side the event belongs to.
|
|
1894
|
-
...(e.acpSessionId ? { acpSessionId: e.acpSessionId } : {}),
|
|
1895
|
-
}));
|
|
1896
|
-
}
|
|
1897
2601
|
/**
|
|
1898
|
-
*
|
|
1899
|
-
*
|
|
1900
|
-
*
|
|
1901
|
-
* journaled and must never advance {@link SessionJournal}'s seq the way `onAgentEvent`'s
|
|
1902
|
-
* `agent_event`s do.
|
|
2602
|
+
* The session's processor frames (Task 19), in processor order: projected for the connected server
|
|
2603
|
+
* (`processor/projection.ts`), the numbered ones journaled under the processor's own `seq` and then sent, the levels and
|
|
2604
|
+
* RPC replies sent as they are.
|
|
1903
2605
|
*
|
|
1904
|
-
*
|
|
1905
|
-
* `agent_opened.activityRaw` and `AgentResumeSessionWire.activityRaw`, neither of which is gated
|
|
1906
|
-
* on {@link AGENT_WIRE_SESSION_STATE_VERSION} (they are additive keys on frames the peer already
|
|
1907
|
-
* decodes, the `supportsSteering`/`reloaded` shape — see those fields' own doc comments), so the
|
|
1908
|
-
* cache has to be current even for a peer too old to receive the push itself.
|
|
2606
|
+
* What used to be decided here moved to the processor, and is no longer re-decided:
|
|
1909
2607
|
*
|
|
1910
|
-
*
|
|
1911
|
-
*
|
|
1912
|
-
*
|
|
1913
|
-
*
|
|
1914
|
-
*
|
|
1915
|
-
*
|
|
1916
|
-
* the
|
|
1917
|
-
*
|
|
2608
|
+
* - **Numbering.** `seq` is processor state; this method never allocates one. The contract check that used to run before
|
|
2609
|
+
* `record()` (`validateWireEvent`) runs in the processor before it numbers (`emit.ts`), so a refused event still leaves no hole.
|
|
2610
|
+
* - **Scoped events for an old server** (spec §5.4, "与连接状态无关"): the processor numbers them whatever the server; the
|
|
2611
|
+
* projection withholds them below {@link AGENT_WIRE_SCOPED_ORIGIN_VERSION} — leaving a `seq` gap the server does not detect —
|
|
2612
|
+
* and a withheld permission card is answered by an `agent_answer_undeliverable` note the processor turns into a logged
|
|
2613
|
+
* `cancelled` answer (the auto-answer used to be written straight to the agent).
|
|
2614
|
+
* - **`refusalFields`**: the projection adds the agent's observed sign-in methods and code to an `agent_turn_done` the processor
|
|
2615
|
+
* marked `agentRefused` — exactly the turns {@link refusalFields} was gated on.
|
|
2616
|
+
*
|
|
2617
|
+
* A frame the projection withholds is not recorded either: the outbox (Task 20, the record-before-send buffer that replaced the
|
|
2618
|
+
* journal) only ever holds frames this server can be sent, which is what keeps {@link blockIncompatibleJournal} meaningful.
|
|
2619
|
+
*
|
|
2620
|
+
* Returns the outbox append of the numbered frames: the session's record loop awaits it before folding another record, so the
|
|
2621
|
+
* outbox holding a frame proves its record's commands are in raw (and a checkpoint never precedes a record's frames).
|
|
1918
2622
|
*/
|
|
1919
|
-
|
|
1920
|
-
|
|
1921
|
-
|
|
1922
|
-
|
|
1923
|
-
|
|
1924
|
-
|
|
1925
|
-
|
|
1926
|
-
|
|
1927
|
-
|
|
1928
|
-
|
|
1929
|
-
|
|
1930
|
-
|
|
1931
|
-
|
|
1932
|
-
|
|
1933
|
-
|
|
1934
|
-
|
|
1935
|
-
|
|
2623
|
+
onSessionFrames(live, frames) {
|
|
2624
|
+
const numbered = [];
|
|
2625
|
+
for (const output of frames) {
|
|
2626
|
+
if (live.closed) {
|
|
2627
|
+
// Closed or detached while the session was finishing a record (Task 21 fix round 1, I-1): its numbered frames are still
|
|
2628
|
+
// recorded — a detached session's next daemon resends them; dropping them would skip their seqs behind the head checkpoint.
|
|
2629
|
+
// Nothing is sent or noted for it any more.
|
|
2630
|
+
if (output.journaled && projectFrame(output, { sessionRef: live.sessionRef, serverAgentWire: this.serverAgentWire }).frame) {
|
|
2631
|
+
numbered.push({ seq: output.frame.seq, src: output.src, frame: output.frame });
|
|
2632
|
+
}
|
|
2633
|
+
continue;
|
|
2634
|
+
}
|
|
2635
|
+
const frame = output.frame;
|
|
2636
|
+
if (frame.t === 'agent_account_update') {
|
|
2637
|
+
if (this.serverAgentWire >= 27)
|
|
2638
|
+
this.emit({ t: 'agent_account_update', agentKey: live.agentKey, snapshot: frame.snapshot });
|
|
2639
|
+
continue;
|
|
2640
|
+
}
|
|
2641
|
+
switch (frame.t) {
|
|
2642
|
+
case 'agent_event':
|
|
2643
|
+
// A live process, whatever the event: the idle sweep is about liveness.
|
|
2644
|
+
this.touch(live);
|
|
2645
|
+
break;
|
|
2646
|
+
case 'agent_turn_done':
|
|
2647
|
+
// Busy is sampled (`liveness.turnRunning`, already false by now); a session that was the registry's only busy one
|
|
2648
|
+
// may just have gone idle, and sampling has no event of its own to announce that.
|
|
2649
|
+
this.tasks?.sessionBusyChanged();
|
|
2650
|
+
this.touch(live);
|
|
2651
|
+
if (typeof frame.nativeSessionId === 'string' && frame.nativeSessionId)
|
|
2652
|
+
live.nativeSessionId = frame.nativeSessionId;
|
|
2653
|
+
// The adapter works: its earlier own exits no longer count towards the crash limit.
|
|
2654
|
+
if (frame.status !== 'failed')
|
|
2655
|
+
this.adapterCrashes.delete(live.sessionRef);
|
|
2656
|
+
break;
|
|
2657
|
+
case 'agent_session_state':
|
|
2658
|
+
// A level, never journaled; the session keeps the latest (`activityRaw`) for `agent_opened`/the resume offer, even for a
|
|
2659
|
+
// server too old to be sent the push itself. A push is when sampled busy may flip, so the registry looks again.
|
|
2660
|
+
live.lastActivityAtMs = Date.now();
|
|
2661
|
+
live.staleActivityWarned = false;
|
|
2662
|
+
this.tasks?.sessionBusyChanged();
|
|
2663
|
+
break;
|
|
2664
|
+
case 'agent_runtime_profile':
|
|
2665
|
+
if (this.sessions.get(live.sessionRef) !== live)
|
|
2666
|
+
continue;
|
|
2667
|
+
break;
|
|
2668
|
+
}
|
|
2669
|
+
const projection = projectFrame(output, {
|
|
2670
|
+
sessionRef: live.sessionRef,
|
|
2671
|
+
serverAgentWire: this.serverAgentWire,
|
|
2672
|
+
...(frame.t === 'agent_turn_done' && frame.agentRefused === true
|
|
2673
|
+
? { authMethods: this.agents.observedAuthMethods(live.agentId) } : {}),
|
|
2674
|
+
});
|
|
2675
|
+
if (projection.note) {
|
|
2676
|
+
void live.session.note(projection.note).catch((err) => {
|
|
2677
|
+
console.error(`[agent] auto-answer for an undeliverable card failed for ${live.sessionRef}: ${errorMessage(err)}`);
|
|
2678
|
+
});
|
|
2679
|
+
}
|
|
2680
|
+
if (!projection.frame) {
|
|
2681
|
+
if (frame.t === 'agent_event' && frame.acpSessionId !== undefined) {
|
|
2682
|
+
console.warn(`[acp:${live.agentId}] scoped live event '${String(frame.eventName)}' for '${String(frame.acpSessionId)}' disabled: ` +
|
|
2683
|
+
`requires agent wire ${AGENT_WIRE_SCOPED_ORIGIN_VERSION}`);
|
|
2684
|
+
}
|
|
2685
|
+
continue;
|
|
2686
|
+
}
|
|
2687
|
+
if (output.journaled)
|
|
2688
|
+
numbered.push({ seq: frame.seq, src: output.src, frame });
|
|
2689
|
+
else
|
|
2690
|
+
this.emit(projection.frame);
|
|
2691
|
+
}
|
|
2692
|
+
return numbered.length > 0 ? this.record(live, numbered) : undefined;
|
|
1936
2693
|
}
|
|
1937
|
-
|
|
1938
|
-
|
|
2694
|
+
/**
|
|
2695
|
+
* The session degraded under its own loop (a poisoned processor, or a transport that failed): its open turns were already ended
|
|
2696
|
+
* `failed` through {@link onSessionFrames} and its adapter is being stopped. It is retired like a swept session — the journal
|
|
2697
|
+
* kept, the server told — so the next `agent_open` spawns anew and loads the native session (interface-contracts: the poison
|
|
2698
|
+
* recovery contract; never retried from a checkpoint).
|
|
2699
|
+
*/
|
|
2700
|
+
async onSessionDegraded(live, reason) {
|
|
2701
|
+
if (live.closed || this.sessions.get(live.sessionRef) !== live)
|
|
1939
2702
|
return;
|
|
1940
|
-
|
|
1941
|
-
|
|
1942
|
-
|
|
1943
|
-
//
|
|
1944
|
-
//
|
|
1945
|
-
|
|
1946
|
-
|
|
1947
|
-
if (r.nativeSessionId)
|
|
1948
|
-
live.nativeSessionId = r.nativeSessionId;
|
|
1949
|
-
this.record(live, (seq) => ({
|
|
1950
|
-
t: 'agent_turn_done',
|
|
1951
|
-
sessionRef: live.sessionRef,
|
|
1952
|
-
seq,
|
|
1953
|
-
status: r.status,
|
|
1954
|
-
platformTurnId: r.platformTurnId,
|
|
1955
|
-
executionEnded: r.executionEnded,
|
|
1956
|
-
stopReason: r.stopReason,
|
|
1957
|
-
nativeSessionId: r.nativeSessionId ?? null,
|
|
1958
|
-
error: r.error ?? null,
|
|
1959
|
-
// Only on a failure the agent itself answered. This is the frame credentials that expired
|
|
1960
|
-
// mid-conversation arrive on — the agent answers the next `session/prompt` with
|
|
1961
|
-
// `auth_required` rather than ending the session — and it is also the busiest frame on this
|
|
1962
|
-
// channel, one per turn for the life of every session, which is why a completed turn carries
|
|
1963
|
-
// no table nothing reads.
|
|
1964
|
-
//
|
|
1965
|
-
// `status === 'failed'` on its own would be wrong, and wrong in the direction that hurts:
|
|
1966
|
-
// `AcpSession` synthesises that status on three paths the agent never answered (a prompt
|
|
1967
|
-
// after the read loop died, a connection that aborted mid-turn, an unmappable update), all of
|
|
1968
|
-
// them on a *live* session whose method table this daemon certainly holds. An agent process
|
|
1969
|
-
// crashing mid-turn is plausibly more common than a token expiring mid-turn, so pairing those
|
|
1970
|
-
// with `authMethods` would put more false login cards on screen than real ones. See
|
|
1971
|
-
// {@link TurnResult.agentRefused}.
|
|
1972
|
-
//
|
|
1973
|
-
// And `agentRefused` on its own is not enough either, which is what `errorCode` is here for:
|
|
1974
|
-
// an exceeded context window is a refusal the agent genuinely answered, and it is common.
|
|
1975
|
-
...(r.status === 'failed' && r.agentRefused ? this.refusalFields(live.agentId, r.errorCode) : {}),
|
|
1976
|
-
}));
|
|
2703
|
+
console.error(`[acp:${live.agentId} agent:${live.agentKey}] session ${live.sessionRef} degraded and was stopped: ${reason}`);
|
|
2704
|
+
const alreadyLost = live.adapterLost?.notified === true;
|
|
2705
|
+
await this.retire(live, 'keep');
|
|
2706
|
+
// Its adapter's exit already told the server: a second notice could arrive after the server reopened the ref, and void the new
|
|
2707
|
+
// generation.
|
|
2708
|
+
if (!alreadyLost)
|
|
2709
|
+
this.notifySwept(live.sessionRef);
|
|
1977
2710
|
}
|
|
1978
2711
|
/**
|
|
1979
|
-
*
|
|
1980
|
-
*
|
|
1981
|
-
*
|
|
2712
|
+
* Records processor-numbered frames in the session's outbox (the processor's frame, without `sessionRef`). They are sent from
|
|
2713
|
+
* there by {@link pump} — an append announces itself — under the session window and the socket gate. A failed append rejects:
|
|
2714
|
+
* the session degrades (a frame that could not be recorded is not sent either).
|
|
1982
2715
|
*/
|
|
1983
|
-
record(live,
|
|
1984
|
-
const seq
|
|
1985
|
-
const frame = build(seq);
|
|
1986
|
-
void this.enqueue(live, async () => {
|
|
2716
|
+
record(live, frames) {
|
|
2717
|
+
for (const { seq, frame } of frames) {
|
|
1987
2718
|
if (frame.t === 'agent_event' && frame.acpSessionId != null) {
|
|
1988
2719
|
live.highestScopedJournalSeq = Math.max(live.highestScopedJournalSeq, seq);
|
|
1989
2720
|
}
|
|
1990
|
-
|
|
1991
|
-
|
|
1992
|
-
|
|
1993
|
-
|
|
1994
|
-
|
|
1995
|
-
|
|
1996
|
-
|
|
1997
|
-
|
|
1998
|
-
|
|
1999
|
-
|
|
2000
|
-
|
|
2001
|
-
|
|
2002
|
-
|
|
2721
|
+
}
|
|
2722
|
+
return live.session.outbox.append(frames);
|
|
2723
|
+
}
|
|
2724
|
+
/**
|
|
2725
|
+
* The server applied through `throughSeq` (`agent_event_ack`, or the `resumeFromSeq` of a reopen): the outbox forgets those
|
|
2726
|
+
* entries and the transport truncates its stream (with the `committedTail` watermark). Held back, like delivery, while an
|
|
2727
|
+
* incompatible server still owes this session a scoped frame it cannot interpret.
|
|
2728
|
+
*/
|
|
2729
|
+
async acknowledge(live, throughSeq) {
|
|
2730
|
+
if (live.scopedReplayBlocked || this.blockIncompatibleJournal(live))
|
|
2731
|
+
return;
|
|
2732
|
+
await live.session.outbox.ack(throughSeq, (error) => {
|
|
2733
|
+
console.error(`[agent] outbox commit failed for ${live.sessionRef}: ${errorMessage(error)}`);
|
|
2003
2734
|
});
|
|
2735
|
+
if (throughSeq >= live.highestScopedJournalSeq)
|
|
2736
|
+
live.highestScopedJournalSeq = 0;
|
|
2004
2737
|
}
|
|
2005
|
-
/**
|
|
2738
|
+
/**
|
|
2739
|
+
* Must run before replay, delivery or ACK truncation: a server below {@link AGENT_WIRE_SCOPED_ORIGIN_VERSION} cannot deliver or
|
|
2740
|
+
* acknowledge a scoped frame the outbox still holds, so nothing of this session goes to it (sending a higher seq would let it
|
|
2741
|
+
* acknowledge past the child event it could not interpret).
|
|
2742
|
+
*/
|
|
2006
2743
|
blockIncompatibleJournal(live) {
|
|
2007
2744
|
if (this.serverAgentWire >= AGENT_WIRE_SCOPED_ORIGIN_VERSION)
|
|
2008
2745
|
return false;
|
|
2009
2746
|
if (live.scopedReplayBlocked)
|
|
2010
2747
|
return true;
|
|
2011
|
-
//
|
|
2012
|
-
// proves the peer has applied (or explicitly accepted a gap through) that scoped sequence.
|
|
2748
|
+
// Only a compatible ACK proves the peer has applied (or explicitly accepted a gap through) that scoped sequence.
|
|
2013
2749
|
const incompatible = live.highestScopedJournalSeq > 0;
|
|
2014
2750
|
if (incompatible)
|
|
2015
2751
|
live.scopedReplayBlocked = true;
|
|
2016
2752
|
return incompatible;
|
|
2017
2753
|
}
|
|
2018
|
-
/**
|
|
2019
|
-
|
|
2020
|
-
|
|
2021
|
-
|
|
2754
|
+
/**
|
|
2755
|
+
* The sender (spec §5.6): sessions take turns, one frame each per round, while the socket gate is open. A session sends its
|
|
2756
|
+
* next unsent outbox entry only while fewer than {@link SESSION_WINDOW_EVENTS} events and {@link SESSION_WINDOW_BYTES} bytes it
|
|
2757
|
+
* sent on this connection are unacknowledged (one frame always fits an empty window). Each frame is projected for the
|
|
2758
|
+
* connected server as it leaves. Re-entrant calls fold into the running one.
|
|
2759
|
+
*/
|
|
2760
|
+
pump() {
|
|
2761
|
+
if (this.pumping) {
|
|
2762
|
+
this.pumpAgain = true;
|
|
2763
|
+
return;
|
|
2764
|
+
}
|
|
2765
|
+
this.pumping = true;
|
|
2766
|
+
try {
|
|
2767
|
+
do {
|
|
2768
|
+
this.pumpAgain = false;
|
|
2769
|
+
if (this.send === null)
|
|
2770
|
+
return;
|
|
2771
|
+
const lives = [...this.sessions.values()];
|
|
2772
|
+
if (lives.length === 0)
|
|
2773
|
+
return;
|
|
2774
|
+
let progressed = true;
|
|
2775
|
+
while (progressed) {
|
|
2776
|
+
progressed = false;
|
|
2777
|
+
const start = this.turn;
|
|
2778
|
+
this.turn = (start + 1) % lives.length;
|
|
2779
|
+
for (let k = 0; k < lives.length; k++) {
|
|
2780
|
+
const live = lives[(start + k) % lives.length];
|
|
2781
|
+
if (live.closed || this.sessions.get(live.sessionRef) !== live)
|
|
2782
|
+
continue;
|
|
2783
|
+
if (live.scopedReplayBlocked || this.blockIncompatibleJournal(live))
|
|
2784
|
+
continue;
|
|
2785
|
+
const outbox = live.session.outbox;
|
|
2786
|
+
const item = outbox.nextUnsent();
|
|
2787
|
+
if (!item)
|
|
2788
|
+
continue;
|
|
2789
|
+
// A resolving subagent relation and everything before it go out past the window, in seq order — the server cannot
|
|
2790
|
+
// acknowledge anything after a child frame without it (spec §5.6). Once sent they count again for a server that
|
|
2791
|
+
// acknowledges as soon as the relation releases its cap (wire ≥ 25); an older one wastes its acks while capped, so
|
|
2792
|
+
// there they keep not counting (`SessionOutbox.inflight`).
|
|
2793
|
+
const pinnedCountsWhenSent = this.serverAgentWire >= AGENT_WIRE_RELATION_ACK_VERSION;
|
|
2794
|
+
const inflight = outbox.inflight(this.epoch, pinnedCountsWhenSent);
|
|
2795
|
+
const bypass = item.seq <= outbox.pinnedThrough;
|
|
2796
|
+
if (!bypass && (inflight.events >= SESSION_WINDOW_EVENTS || (inflight.events > 0 && inflight.bytes + item.bytes > SESSION_WINDOW_BYTES)))
|
|
2797
|
+
continue;
|
|
2798
|
+
if (!this.gate.isOpen()) {
|
|
2799
|
+
this.gate.whenOpen(() => this.pump());
|
|
2800
|
+
return;
|
|
2801
|
+
}
|
|
2802
|
+
item.sentOn = this.epoch;
|
|
2803
|
+
this.sendRecorded(live, item.frame, item.src);
|
|
2804
|
+
progressed = true;
|
|
2805
|
+
if (this.send === null)
|
|
2806
|
+
return;
|
|
2807
|
+
if (live.adapterLost && !live.adapterLost.notified && item.seq >= live.adapterLost.throughSeq)
|
|
2808
|
+
this.flushAdapterLost(live.sessionRef);
|
|
2809
|
+
}
|
|
2810
|
+
}
|
|
2811
|
+
} while (this.pumpAgain);
|
|
2812
|
+
}
|
|
2813
|
+
finally {
|
|
2814
|
+
this.pumping = false;
|
|
2815
|
+
}
|
|
2816
|
+
}
|
|
2817
|
+
/** A retiring session's last unsent frames, in order (outside the rotation; bounded by its window). */
|
|
2818
|
+
sendUnsent(live) {
|
|
2819
|
+
if (this.send === null || live.scopedReplayBlocked || this.blockIncompatibleJournal(live))
|
|
2820
|
+
return;
|
|
2821
|
+
for (let item = live.session.outbox.nextUnsent(); item; item = live.session.outbox.nextUnsent()) {
|
|
2822
|
+
item.sentOn = this.epoch;
|
|
2823
|
+
this.sendRecorded(live, item.frame, item.src);
|
|
2824
|
+
}
|
|
2825
|
+
}
|
|
2826
|
+
/** One recorded frame, projected for the connected server as it leaves. */
|
|
2827
|
+
sendRecorded(live, frame, src) {
|
|
2828
|
+
const projection = projectFrame({ journaled: true, frame, src }, {
|
|
2829
|
+
sessionRef: live.sessionRef,
|
|
2830
|
+
serverAgentWire: this.serverAgentWire,
|
|
2831
|
+
...(frame.t === 'agent_turn_done' && frame.agentRefused === true ? { authMethods: this.agents.observedAuthMethods(live.agentId) } : {}),
|
|
2022
2832
|
});
|
|
2023
|
-
|
|
2024
|
-
|
|
2833
|
+
if (projection.note) {
|
|
2834
|
+
void live.session.note(projection.note).catch((err) => {
|
|
2835
|
+
console.error(`[agent] auto-answer for an undeliverable card failed for ${live.sessionRef}: ${errorMessage(err)}`);
|
|
2836
|
+
});
|
|
2837
|
+
}
|
|
2838
|
+
if (projection.frame)
|
|
2839
|
+
this.emit(projection.frame);
|
|
2025
2840
|
}
|
|
2026
2841
|
async onClose(sessionRef) {
|
|
2842
|
+
// The server is done with the ref: a switch it was waiting for is moot (Task 24), and so is its adapter's crash count.
|
|
2843
|
+
void this.agents.cancelSwitch?.({ sessionRef }).catch(() => false);
|
|
2844
|
+
this.reopenRequests.delete(sessionRef);
|
|
2845
|
+
this.adapterCrashes.delete(sessionRef);
|
|
2027
2846
|
// Closing a ref that is still spawning: nothing to close yet, so tombstone it — openSession
|
|
2028
2847
|
// closes the session (and destroys its journal) the moment the bridge hands it over.
|
|
2029
2848
|
//
|
|
@@ -2036,47 +2855,78 @@ export class AgentSessionManager {
|
|
|
2036
2855
|
return;
|
|
2037
2856
|
}
|
|
2038
2857
|
const live = this.sessions.get(sessionRef);
|
|
2039
|
-
if (
|
|
2858
|
+
if (live) {
|
|
2859
|
+
await this.retire(live, 'destroy');
|
|
2040
2860
|
return;
|
|
2041
|
-
|
|
2861
|
+
}
|
|
2862
|
+
// Not held: whatever an earlier life left under the ref (a swept or degraded session's outbox, a previous daemon's, a migrated
|
|
2863
|
+
// legacy journal) can never be asked for again — it goes, as the journal did on `agent_close`.
|
|
2864
|
+
//
|
|
2865
|
+
// The cleanup publishes on `retiring` — synchronously, before this handler's first await — because the server's close→reopen
|
|
2866
|
+
// pair (a stale recycle, a config reload) can land its reopen while the cleanup is still listing the keeper. `openSession`
|
|
2867
|
+
// waits on this map as its very first statement, so without the publication that open spawns under the cleanup, and the
|
|
2868
|
+
// cleanup's list sees the fresh spawn as a running leftover and stops it mid-`initialize` ("exited before responding to
|
|
2869
|
+
// initialize (code=null, signal=SIGTERM)"). The recheck stays inside: an open that appeared while a previous teardown was
|
|
2870
|
+
// settling owns the ref — its reopen reads the leftover outbox back instead of dropping it. A cleanup that fails must not
|
|
2871
|
+
// fail whatever waits on it (the published promise never rejects), and a second close chains behind this one as today.
|
|
2872
|
+
const previous = this.retiring.get(sessionRef);
|
|
2873
|
+
const cleanup = (async () => {
|
|
2874
|
+
await previous;
|
|
2875
|
+
if (this.sessions.has(sessionRef) || this.opening.has(sessionRef))
|
|
2876
|
+
return;
|
|
2877
|
+
await this.agents.releaseLeftover?.(sessionRef);
|
|
2878
|
+
})().catch((error) => {
|
|
2879
|
+
console.warn(`[agent] leftover cleanup for ${sessionRef} failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
2880
|
+
});
|
|
2881
|
+
this.retiring.set(sessionRef, cleanup);
|
|
2882
|
+
try {
|
|
2883
|
+
await cleanup;
|
|
2884
|
+
}
|
|
2885
|
+
finally {
|
|
2886
|
+
if (this.retiring.get(sessionRef) === cleanup)
|
|
2887
|
+
this.retiring.delete(sessionRef);
|
|
2888
|
+
}
|
|
2042
2889
|
}
|
|
2043
2890
|
/**
|
|
2044
2891
|
* The teardown behind `agent_close`: stop emitting, unregister, kill the agent subprocess, and —
|
|
2045
|
-
* when
|
|
2046
|
-
*
|
|
2047
|
-
*
|
|
2048
|
-
* itself (see {@link sweepIdle} for why that one differs).
|
|
2892
|
+
* when `outbox` says so — drop the session's transport session with its outbox. Shared with {@link sweepIdle} so an orphan swept
|
|
2893
|
+
* on the timer leaves exactly as little behind as one the server closed properly, except for the outbox itself (see
|
|
2894
|
+
* {@link sweepIdle} for why that one differs).
|
|
2049
2895
|
*
|
|
2050
|
-
*
|
|
2051
|
-
* is
|
|
2052
|
-
* before a reopen constructs a second {@link SessionJournal} on the same path, or the new
|
|
2053
|
-
* instance recovers its seq counter from a file that is still being written behind it.
|
|
2896
|
+
* Published on {@link retiring} either way: a reopen spawns under the same transport key, so it must wait until the old adapter
|
|
2897
|
+
* is stopped and the session's pending appends (and its head checkpoint) have landed — or its outbox is gone.
|
|
2054
2898
|
*/
|
|
2055
|
-
async retire(live,
|
|
2899
|
+
async retire(live, outbox) {
|
|
2056
2900
|
const sessionRef = live.sessionRef;
|
|
2901
|
+
// Closed for good (an `agent_close`): its crash count goes with it. A `keep` retire is a respawn, a sweep or a degrade — the count
|
|
2902
|
+
// must survive it, or a crash-looping adapter would be respawned for ever.
|
|
2903
|
+
if (outbox === 'destroy')
|
|
2904
|
+
this.adapterCrashes.delete(sessionRef);
|
|
2057
2905
|
live.closed = true;
|
|
2906
|
+
live.unwatch();
|
|
2058
2907
|
this.sessions.delete(sessionRef);
|
|
2059
2908
|
this.tasks?.unregisterAgentSession(sessionRef);
|
|
2060
|
-
|
|
2061
|
-
|
|
2062
|
-
|
|
2063
|
-
|
|
2064
|
-
|
|
2065
|
-
|
|
2909
|
+
if (outbox === 'detach')
|
|
2910
|
+
this.detached.add(sessionRef);
|
|
2911
|
+
const closed = (async () => {
|
|
2912
|
+
// Frames produced before the close still go out (as the journal's queued sends did): appends in flight land, then whatever
|
|
2913
|
+
// is unsent is written — the session left the sender's rotation with the `sessions` entry.
|
|
2914
|
+
await live.session.outbox.flush();
|
|
2915
|
+
this.sendUnsent(live);
|
|
2916
|
+
if (outbox === 'detach' && live.session.detach)
|
|
2917
|
+
await live.session.detach();
|
|
2918
|
+
else
|
|
2919
|
+
await live.session.close(outbox === 'destroy' ? 'release' : 'keep');
|
|
2920
|
+
})().catch((err) => {
|
|
2921
|
+
console.error(`[agent] close failed for ${sessionRef}: ${errorMessage(err)}`);
|
|
2066
2922
|
});
|
|
2067
|
-
const retired =
|
|
2923
|
+
const retired = closed.then(() => undefined).finally(() => {
|
|
2068
2924
|
// Only clear our own entry: a later close of the same ref may already have replaced it.
|
|
2069
2925
|
if (this.retiring.get(sessionRef) === retired)
|
|
2070
2926
|
this.retiring.delete(sessionRef);
|
|
2071
2927
|
});
|
|
2072
2928
|
this.retiring.set(sessionRef, retired);
|
|
2073
|
-
|
|
2074
|
-
await live.session.close();
|
|
2075
|
-
}
|
|
2076
|
-
catch (err) {
|
|
2077
|
-
console.error(`[agent] close failed for ${sessionRef}: ${errorMessage(err)}`);
|
|
2078
|
-
}
|
|
2079
|
-
await journalDone;
|
|
2929
|
+
await retired;
|
|
2080
2930
|
}
|
|
2081
2931
|
/**
|
|
2082
2932
|
* The one place every outbound agent frame passes, and so the one place the wire contract can be
|