@zvada/agent-server 0.3.22 → 0.3.23
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/CHANGELOG.md +261 -0
- package/dist/types/accounts/chatgpt.d.ts +1 -1
- package/dist/types/accounts/chatgpt.d.ts.map +1 -1
- package/dist/types/accounts/http.d.ts +2 -4
- package/dist/types/accounts/http.d.ts.map +1 -1
- package/dist/types/client/client.d.ts +37 -11
- package/dist/types/client/client.d.ts.map +1 -1
- package/dist/types/client/connection.d.ts +64 -13
- package/dist/types/client/connection.d.ts.map +1 -1
- package/dist/types/client/index.d.ts +1 -1
- package/dist/types/client/index.d.ts.map +1 -1
- package/dist/types/client/logins.d.ts +2 -1
- package/dist/types/client/logins.d.ts.map +1 -1
- package/dist/types/client/streams.d.ts +9 -3
- package/dist/types/client/streams.d.ts.map +1 -1
- package/dist/types/client/turns.d.ts +1 -1
- package/dist/types/client/turns.d.ts.map +1 -1
- package/dist/types/core/agents/acp/acp-agent.d.ts +34 -1
- package/dist/types/core/agents/acp/acp-agent.d.ts.map +1 -1
- package/dist/types/core/agents/acp/adapter.d.ts.map +1 -1
- package/dist/types/core/agents/acp/client.d.ts +12 -3
- package/dist/types/core/agents/acp/client.d.ts.map +1 -1
- package/dist/types/core/agents/acp/mappings.d.ts +8 -2
- package/dist/types/core/agents/acp/mappings.d.ts.map +1 -1
- package/dist/types/core/agents/base.d.ts +22 -8
- package/dist/types/core/agents/base.d.ts.map +1 -1
- package/dist/types/core/agents/claude-code/adapter.d.ts +7 -0
- package/dist/types/core/agents/claude-code/adapter.d.ts.map +1 -1
- package/dist/types/core/agents/claude-code/approvals.d.ts +4 -1
- package/dist/types/core/agents/claude-code/approvals.d.ts.map +1 -1
- package/dist/types/core/agents/claude-code/claude-agent.d.ts +16 -5
- package/dist/types/core/agents/claude-code/claude-agent.d.ts.map +1 -1
- package/dist/types/core/agents/claude-code/events.d.ts +9 -1
- package/dist/types/core/agents/claude-code/events.d.ts.map +1 -1
- package/dist/types/core/agents/claude-code/login.d.ts +4 -1
- package/dist/types/core/agents/claude-code/login.d.ts.map +1 -1
- package/dist/types/core/agents/claude-code/mcp.d.ts +21 -7
- package/dist/types/core/agents/claude-code/mcp.d.ts.map +1 -1
- package/dist/types/core/agents/claude-code/models.d.ts +5 -1
- package/dist/types/core/agents/claude-code/models.d.ts.map +1 -1
- package/dist/types/core/agents/claude-code/options.d.ts +10 -4
- package/dist/types/core/agents/claude-code/options.d.ts.map +1 -1
- package/dist/types/core/agents/claude-code/outcome.d.ts +3 -5
- package/dist/types/core/agents/claude-code/outcome.d.ts.map +1 -1
- package/dist/types/core/agents/claude-code/results.d.ts +16 -0
- package/dist/types/core/agents/claude-code/results.d.ts.map +1 -0
- package/dist/types/core/agents/claude-code/sdk-pair.d.ts +13 -0
- package/dist/types/core/agents/claude-code/sdk-pair.d.ts.map +1 -0
- package/dist/types/core/agents/claude-code/session.d.ts +50 -12
- package/dist/types/core/agents/claude-code/session.d.ts.map +1 -1
- package/dist/types/core/agents/claude-code/spend.d.ts +2 -1
- package/dist/types/core/agents/claude-code/spend.d.ts.map +1 -1
- package/dist/types/core/agents/claude-code/tools.d.ts +26 -3
- package/dist/types/core/agents/claude-code/tools.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/adapter.d.ts +2 -2
- package/dist/types/core/agents/codex-app-server/adapter.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/client.d.ts +12 -2
- package/dist/types/core/agents/codex-app-server/client.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/codex-agent.d.ts +58 -13
- package/dist/types/core/agents/codex-app-server/codex-agent.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/commands.d.ts +2 -2
- package/dist/types/core/agents/codex-app-server/commands.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/credentials.d.ts +5 -1
- package/dist/types/core/agents/codex-app-server/credentials.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/effort.d.ts +2 -1
- package/dist/types/core/agents/codex-app-server/effort.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/homes.d.ts +19 -0
- package/dist/types/core/agents/codex-app-server/homes.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/items.d.ts +27 -2
- package/dist/types/core/agents/codex-app-server/items.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/login.d.ts +4 -1
- package/dist/types/core/agents/codex-app-server/login.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/mcp.d.ts +15 -30
- package/dist/types/core/agents/codex-app-server/mcp.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/models.d.ts +3 -10
- package/dist/types/core/agents/codex-app-server/models.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/options.d.ts +27 -4
- package/dist/types/core/agents/codex-app-server/options.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/plan-approval.d.ts +2 -1
- package/dist/types/core/agents/codex-app-server/plan-approval.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/protocol.d.ts +16 -3
- package/dist/types/core/agents/codex-app-server/protocol.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/requests.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/session.d.ts +68 -40
- package/dist/types/core/agents/codex-app-server/session.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/subagents.d.ts +9 -4
- package/dist/types/core/agents/codex-app-server/subagents.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/thread-settings.d.ts +3 -2
- package/dist/types/core/agents/codex-app-server/thread-settings.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/turn-items.d.ts +2 -1
- package/dist/types/core/agents/codex-app-server/turn-items.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/turn.d.ts +31 -8
- package/dist/types/core/agents/codex-app-server/turn.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/user-login.d.ts +28 -0
- package/dist/types/core/agents/codex-app-server/user-login.d.ts.map +1 -0
- package/dist/types/core/agents/credentials.d.ts +3 -1
- package/dist/types/core/agents/credentials.d.ts.map +1 -1
- package/dist/types/core/agents/diffs.d.ts +9 -0
- package/dist/types/core/agents/diffs.d.ts.map +1 -0
- package/dist/types/core/agents/elicitation.d.ts +3 -2
- package/dist/types/core/agents/elicitation.d.ts.map +1 -1
- package/dist/types/core/agents/login.d.ts +6 -0
- package/dist/types/core/agents/login.d.ts.map +1 -1
- package/dist/types/core/agents/mcp-config.d.ts +0 -4
- package/dist/types/core/agents/mcp-config.d.ts.map +1 -1
- package/dist/types/core/agents/process-pool.d.ts +193 -0
- package/dist/types/core/agents/process-pool.d.ts.map +1 -0
- package/dist/types/core/agents/scripted/adapter.d.ts +68 -0
- package/dist/types/core/agents/scripted/adapter.d.ts.map +1 -0
- package/dist/types/core/agents/scripted/mcp-call.d.ts +56 -0
- package/dist/types/core/agents/scripted/mcp-call.d.ts.map +1 -0
- package/dist/types/core/agents/scripted/scripted-agent.d.ts +73 -0
- package/dist/types/core/agents/scripted/scripted-agent.d.ts.map +1 -0
- package/dist/types/core/agents/scripted/scripts.d.ts +29 -0
- package/dist/types/core/agents/scripted/scripts.d.ts.map +1 -0
- package/dist/types/core/agents/scripted/steps.d.ts +132 -0
- package/dist/types/core/agents/scripted/steps.d.ts.map +1 -0
- package/dist/types/core/agents/session-agent.d.ts +49 -6
- package/dist/types/core/agents/session-agent.d.ts.map +1 -1
- package/dist/types/core/diagnostics.d.ts +4 -1
- package/dist/types/core/diagnostics.d.ts.map +1 -1
- package/dist/types/core/index.d.ts +5 -0
- package/dist/types/core/index.d.ts.map +1 -1
- package/dist/types/core/presets.d.ts +32 -6
- package/dist/types/core/presets.d.ts.map +1 -1
- package/dist/types/core/processes/lifeline.d.ts +0 -20
- package/dist/types/core/processes/lifeline.d.ts.map +1 -1
- package/dist/types/core/provision/index.d.ts +2 -2
- package/dist/types/core/provision/index.d.ts.map +1 -1
- package/dist/types/core/provision/install-build.d.ts +2 -1
- package/dist/types/core/provision/install-build.d.ts.map +1 -1
- package/dist/types/core/provision/npm.d.ts +20 -8
- package/dist/types/core/provision/npm.d.ts.map +1 -1
- package/dist/types/core/provision/provisioner.d.ts +4 -1
- package/dist/types/core/provision/provisioner.d.ts.map +1 -1
- package/dist/types/core/provision/tools.d.ts +2 -1
- package/dist/types/core/provision/tools.d.ts.map +1 -1
- package/dist/types/core/runtime/agent-runtime.d.ts +16 -8
- package/dist/types/core/runtime/agent-runtime.d.ts.map +1 -1
- package/dist/types/core/runtime/event-processor.d.ts +5 -1
- package/dist/types/core/runtime/event-processor.d.ts.map +1 -1
- package/dist/types/core/runtime/harness-clis.d.ts +9 -0
- package/dist/types/core/runtime/harness-clis.d.ts.map +1 -1
- package/dist/types/core/runtime/harness-logins.d.ts +33 -5
- package/dist/types/core/runtime/harness-logins.d.ts.map +1 -1
- package/dist/types/core/runtime/model-lists.d.ts +2 -0
- package/dist/types/core/runtime/model-lists.d.ts.map +1 -1
- package/dist/types/core/runtime/permission-policy.d.ts +5 -3
- package/dist/types/core/runtime/permission-policy.d.ts.map +1 -1
- package/dist/types/core/runtime/request-broker.d.ts +2 -1
- package/dist/types/core/runtime/request-broker.d.ts.map +1 -1
- package/dist/types/core/runtime/turn.d.ts +12 -1
- package/dist/types/core/runtime/turn.d.ts.map +1 -1
- package/dist/types/core/utils/deadline.d.ts +6 -0
- package/dist/types/core/utils/deadline.d.ts.map +1 -1
- package/dist/types/core/utils/errors.d.ts +7 -1
- package/dist/types/core/utils/errors.d.ts.map +1 -1
- package/dist/types/operator/operator.d.ts.map +1 -1
- package/dist/types/operator/render.d.ts +6 -4
- package/dist/types/operator/render.d.ts.map +1 -1
- package/dist/types/protocol/auth.d.ts +54 -24
- package/dist/types/protocol/auth.d.ts.map +1 -1
- package/dist/types/protocol/clis.d.ts +14 -7
- package/dist/types/protocol/clis.d.ts.map +1 -1
- package/dist/types/protocol/config.d.ts +2 -0
- package/dist/types/protocol/config.d.ts.map +1 -1
- package/dist/types/protocol/conversation.d.ts +40 -3
- package/dist/types/protocol/conversation.d.ts.map +1 -1
- package/dist/types/protocol/credentials.d.ts +7 -0
- package/dist/types/protocol/credentials.d.ts.map +1 -1
- package/dist/types/protocol/execution.d.ts +2 -1
- package/dist/types/protocol/execution.d.ts.map +1 -1
- package/dist/types/protocol/factories.d.ts +12 -2
- package/dist/types/protocol/factories.d.ts.map +1 -1
- package/dist/types/protocol/harness.d.ts +6 -2
- package/dist/types/protocol/harness.d.ts.map +1 -1
- package/dist/types/protocol/history.d.ts +19 -0
- package/dist/types/protocol/history.d.ts.map +1 -1
- package/dist/types/protocol/lifecycle.d.ts +47 -8
- package/dist/types/protocol/lifecycle.d.ts.map +1 -1
- package/dist/types/protocol/model-catalog.d.ts.map +1 -1
- package/dist/types/protocol/models.d.ts +3 -0
- package/dist/types/protocol/models.d.ts.map +1 -1
- package/dist/types/protocol/parts.d.ts +14 -0
- package/dist/types/protocol/parts.d.ts.map +1 -1
- package/dist/types/protocol/tool-state.d.ts +12 -0
- package/dist/types/protocol/tool-state.d.ts.map +1 -1
- package/dist/types/protocol/wire-api.d.ts +129 -23
- package/dist/types/protocol/wire-api.d.ts.map +1 -1
- package/dist/types/protocol/wire.d.ts +70 -10
- package/dist/types/protocol/wire.d.ts.map +1 -1
- package/dist/types/server/acp/binding.d.ts +6 -1
- package/dist/types/server/acp/binding.d.ts.map +1 -1
- package/dist/types/server/acp/translate.d.ts +33 -7
- package/dist/types/server/acp/translate.d.ts.map +1 -1
- package/dist/types/server/agent-server.d.ts +6 -0
- package/dist/types/server/agent-server.d.ts.map +1 -1
- package/dist/types/server/feedback.d.ts +2 -1
- package/dist/types/server/feedback.d.ts.map +1 -1
- package/dist/types/server/install.d.ts +11 -0
- package/dist/types/server/install.d.ts.map +1 -1
- package/dist/types/server/session-gate.d.ts +2 -1
- package/dist/types/server/session-gate.d.ts.map +1 -1
- package/dist/types/server/session-store.d.ts +1 -0
- package/dist/types/server/session-store.d.ts.map +1 -1
- package/dist/types/server/subscriptions.d.ts +2 -1
- package/dist/types/server/subscriptions.d.ts.map +1 -1
- package/dist/types/server/transports.d.ts.map +1 -1
- package/dist/types/sync/fit.d.ts +8 -7
- package/dist/types/sync/fit.d.ts.map +1 -1
- package/dist/types/sync/history-sqlite.d.ts.map +1 -1
- package/dist/types/sync/index.d.ts +1 -0
- package/dist/types/sync/index.d.ts.map +1 -1
- package/dist/types/sync/journal-sqlite.d.ts +2 -1
- package/dist/types/sync/journal-sqlite.d.ts.map +1 -1
- package/dist/types/sync/journal.d.ts +11 -3
- package/dist/types/sync/journal.d.ts.map +1 -1
- package/dist/types/sync/projection.d.ts +6 -0
- package/dist/types/sync/projection.d.ts.map +1 -1
- package/dist/types/sync/recovery.d.ts +2 -1
- package/dist/types/sync/recovery.d.ts.map +1 -1
- package/dist/types/sync/schema.d.ts +10 -0
- package/dist/types/sync/schema.d.ts.map +1 -1
- package/dist/types/sync/sql.d.ts +2 -1
- package/dist/types/sync/sql.d.ts.map +1 -1
- package/docs/consuming.md +278 -28
- package/docs/deploy.md +36 -8
- package/docs/durable-sessions.md +35 -5
- package/docs/harnesses.md +385 -24
- package/package.json +6 -1
- package/src/accounts/chatgpt.ts +2 -2
- package/src/accounts/http.ts +2 -16
- package/src/client/client.ts +92 -31
- package/src/client/connection.ts +103 -22
- package/src/client/index.ts +1 -0
- package/src/client/logins.ts +1 -1
- package/src/client/streams.ts +12 -4
- package/src/client/turns.ts +1 -1
- package/src/core/agents/acp/acp-agent.ts +181 -52
- package/src/core/agents/acp/adapter.ts +9 -1
- package/src/core/agents/acp/client.ts +16 -4
- package/src/core/agents/acp/mappings.ts +27 -0
- package/src/core/agents/base.ts +25 -10
- package/src/core/agents/claude-code/adapter.ts +47 -17
- package/src/core/agents/claude-code/approvals.ts +6 -5
- package/src/core/agents/claude-code/claude-agent.ts +60 -30
- package/src/core/agents/claude-code/events.ts +11 -1
- package/src/core/agents/claude-code/login.ts +14 -2
- package/src/core/agents/claude-code/mcp.ts +31 -15
- package/src/core/agents/claude-code/models.ts +7 -2
- package/src/core/agents/claude-code/options.ts +11 -3
- package/src/core/agents/claude-code/outcome.ts +10 -2
- package/src/core/agents/claude-code/results.ts +73 -0
- package/src/core/agents/claude-code/sdk-pair.ts +66 -0
- package/src/core/agents/claude-code/session.ts +166 -37
- package/src/core/agents/claude-code/spend.ts +1 -1
- package/src/core/agents/claude-code/tools.ts +108 -12
- package/src/core/agents/codex-app-server/adapter.ts +37 -12
- package/src/core/agents/codex-app-server/client.ts +37 -8
- package/src/core/agents/codex-app-server/codex-agent.ts +115 -50
- package/src/core/agents/codex-app-server/commands.ts +2 -2
- package/src/core/agents/codex-app-server/credentials.ts +10 -5
- package/src/core/agents/codex-app-server/effort.ts +1 -1
- package/src/core/agents/codex-app-server/homes.ts +43 -1
- package/src/core/agents/codex-app-server/items.ts +219 -31
- package/src/core/agents/codex-app-server/login.ts +14 -2
- package/src/core/agents/codex-app-server/mcp.ts +47 -32
- package/src/core/agents/codex-app-server/models.ts +9 -8
- package/src/core/agents/codex-app-server/options.ts +69 -10
- package/src/core/agents/codex-app-server/plan-approval.ts +1 -1
- package/src/core/agents/codex-app-server/protocol.ts +14 -4
- package/src/core/agents/codex-app-server/requests.ts +14 -6
- package/src/core/agents/codex-app-server/session.ts +151 -113
- package/src/core/agents/codex-app-server/subagents.ts +21 -14
- package/src/core/agents/codex-app-server/thread-settings.ts +18 -8
- package/src/core/agents/codex-app-server/turn-items.ts +1 -1
- package/src/core/agents/codex-app-server/turn.ts +79 -32
- package/src/core/agents/codex-app-server/user-login.ts +107 -0
- package/src/core/agents/credentials.ts +5 -2
- package/src/core/agents/diffs.ts +25 -0
- package/src/core/agents/elicitation.ts +2 -2
- package/src/core/agents/login.ts +6 -0
- package/src/core/agents/mcp-config.ts +2 -2
- package/src/core/agents/process-pool.ts +432 -0
- package/src/core/agents/scripted/adapter.ts +142 -0
- package/src/core/agents/scripted/mcp-call.ts +134 -0
- package/src/core/agents/scripted/scripted-agent.ts +418 -0
- package/src/core/agents/scripted/scripts.ts +171 -0
- package/src/core/agents/scripted/steps.ts +127 -0
- package/src/core/agents/session-agent.ts +105 -8
- package/src/core/diagnostics.ts +9 -1
- package/src/core/index.ts +11 -0
- package/src/core/presets.ts +99 -14
- package/src/core/processes/lifeline.ts +99 -25
- package/src/core/provision/index.ts +1 -2
- package/src/core/provision/install-build.ts +4 -1
- package/src/core/provision/npm.ts +309 -97
- package/src/core/provision/provisioner.ts +22 -8
- package/src/core/provision/tools.ts +44 -19
- package/src/core/runtime/agent-runtime.ts +48 -18
- package/src/core/runtime/event-processor.ts +18 -2
- package/src/core/runtime/harness-clis.ts +36 -1
- package/src/core/runtime/harness-logins.ts +82 -31
- package/src/core/runtime/model-lists.ts +1 -1
- package/src/core/runtime/permission-policy.ts +4 -3
- package/src/core/runtime/request-broker.ts +1 -1
- package/src/core/runtime/turn.ts +51 -3
- package/src/core/utils/deadline.ts +23 -0
- package/src/core/utils/errors.ts +11 -4
- package/src/operator/operator.ts +8 -4
- package/src/operator/render.ts +46 -8
- package/src/protocol/auth.ts +30 -23
- package/src/protocol/clis.ts +12 -4
- package/src/protocol/credentials.ts +19 -0
- package/src/protocol/execution.ts +5 -0
- package/src/protocol/factories.ts +25 -3
- package/src/protocol/harness.ts +11 -1
- package/src/protocol/history.ts +20 -0
- package/src/protocol/lifecycle.ts +10 -1
- package/src/protocol/model-catalog.ts +19 -8
- package/src/protocol/parts.ts +12 -1
- package/src/protocol/tool-state.ts +14 -1
- package/src/protocol/wire.ts +14 -0
- package/src/server/acp/binding.ts +113 -23
- package/src/server/acp/translate.ts +161 -8
- package/src/server/agent-server.ts +10 -1
- package/src/server/bin.ts +83 -9
- package/src/server/feedback.ts +1 -1
- package/src/server/install.ts +82 -4
- package/src/server/session-gate.ts +1 -1
- package/src/server/session-store.ts +5 -1
- package/src/server/subscriptions.ts +1 -1
- package/src/server/transports.ts +14 -0
- package/src/sync/fit.ts +115 -28
- package/src/sync/history-sqlite.ts +6 -2
- package/src/sync/index.ts +1 -0
- package/src/sync/journal-sqlite.ts +20 -2
- package/src/sync/journal.ts +10 -2
- package/src/sync/projection.ts +10 -0
- package/src/sync/recovery.ts +11 -4
- package/src/sync/schema.ts +7 -5
- package/src/sync/sql.ts +1 -1
- package/dist/types/core/agents/claude-code/session-pool.d.ts +0 -36
- package/dist/types/core/agents/claude-code/session-pool.d.ts.map +0 -1
- package/dist/types/core/agents/session-pool.d.ts +0 -41
- package/dist/types/core/agents/session-pool.d.ts.map +0 -1
- package/src/core/agents/claude-code/session-pool.ts +0 -136
- package/src/core/agents/session-pool.ts +0 -111
package/src/sync/journal.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import {
|
|
2
2
|
type AgentHarness,
|
|
3
3
|
type DurableSession,
|
|
4
|
+
type LastTurn,
|
|
4
5
|
type LifecycleEvent,
|
|
5
6
|
type MessagePartDeltaEvent,
|
|
6
7
|
type RunRequest,
|
|
@@ -19,7 +20,7 @@ import {
|
|
|
19
20
|
export class JournalError extends WireRefusal<JournalRefusal, Record<string, unknown>> {
|
|
20
21
|
override readonly name = "JournalError";
|
|
21
22
|
}
|
|
22
|
-
|
|
23
|
+
type JournalRefusal = Extract<
|
|
23
24
|
WireErrorName,
|
|
24
25
|
| "turnConflict"
|
|
25
26
|
| "turnActive"
|
|
@@ -84,7 +85,13 @@ export interface SessionJournal {
|
|
|
84
85
|
* `not_verified`. Either way until `confirmStopped`. Returns the release.
|
|
85
86
|
*/
|
|
86
87
|
claim(options?: ClaimOptions): () => void;
|
|
88
|
+
/** What it knows of the session, less `lastTurn`: every admission and append reads it. */
|
|
87
89
|
session(sessionId: string): DurableSession | undefined;
|
|
90
|
+
/**
|
|
91
|
+
* The session's admitted turn, or else the ended one whose `turn.ended` is
|
|
92
|
+
* latest, for `session/get`. Undefined before its first turn.
|
|
93
|
+
*/
|
|
94
|
+
lastTurn(sessionId: string): LastTurn | undefined;
|
|
88
95
|
turn(sessionId: string, turnId: string): TurnReceipt | undefined;
|
|
89
96
|
/** Whether this journal already admitted the turn. Throws `turnConflict` for other input. */
|
|
90
97
|
admission(request: RunRequest): "new" | "duplicate";
|
|
@@ -96,7 +103,8 @@ export interface SessionJournal {
|
|
|
96
103
|
*/
|
|
97
104
|
admit(request: RunRequest): "new" | "duplicate";
|
|
98
105
|
/** Commits `event`, or, when its content cannot be stored, a copy cut down to
|
|
99
|
-
* fit
|
|
106
|
+
* fit (a binary that does not fit is replaced whole by a marker, never cut)
|
|
107
|
+
* or a notice in its place. Deliver what it returns. Throws only when
|
|
100
108
|
* storage itself fails. */
|
|
101
109
|
append(event: LifecycleEvent): WireEventEnvelope;
|
|
102
110
|
/**
|
package/src/sync/projection.ts
CHANGED
|
@@ -7,6 +7,7 @@ import {
|
|
|
7
7
|
jsonBytes,
|
|
8
8
|
reduceConversation,
|
|
9
9
|
} from "../protocol/index.ts";
|
|
10
|
+
import { omitBinaries } from "./fit.ts";
|
|
10
11
|
|
|
11
12
|
/** The most events a projection folds unless its caller says otherwise: it is built in memory. */
|
|
12
13
|
const MAX_PROJECTION_RECORDS = 100_000;
|
|
@@ -28,6 +29,15 @@ export function projectionFold(
|
|
|
28
29
|
};
|
|
29
30
|
}
|
|
30
31
|
|
|
32
|
+
/**
|
|
33
|
+
* `checkpoint` with its largest images and files replaced by text saying what
|
|
34
|
+
* they were until it fits a history page (`omitBinaries`): binaries alone never
|
|
35
|
+
* make a checkpoint too large to keep. The events keep what they kept.
|
|
36
|
+
*/
|
|
37
|
+
export function fitCheckpoint(checkpoint: HistoryCheckpoint): HistoryCheckpoint {
|
|
38
|
+
return omitBinaries(checkpoint, HISTORY_MAX_BYTES, "this checkpoint");
|
|
39
|
+
}
|
|
40
|
+
|
|
31
41
|
/** A checkpoint fits a history page, as all history does: no store keeps, and no reader takes, a larger one. */
|
|
32
42
|
export function checkCheckpointBytes(checkpoint: HistoryCheckpoint): void {
|
|
33
43
|
if (jsonBytes(checkpoint) > HISTORY_MAX_BYTES)
|
package/src/sync/recovery.ts
CHANGED
|
@@ -12,7 +12,7 @@ import {
|
|
|
12
12
|
import type { SessionEvent } from "./fit.ts";
|
|
13
13
|
|
|
14
14
|
/** A turn a server restart cut off, as the journal holds it. */
|
|
15
|
-
|
|
15
|
+
interface CutOffTurn {
|
|
16
16
|
sessionId: string;
|
|
17
17
|
turnId: string;
|
|
18
18
|
/** The session's harness, which a turn cut off before its first event never named. */
|
|
@@ -76,6 +76,8 @@ export function recoveryFacts(history: Iterable<unknown>, turn: CutOffTurn): Ses
|
|
|
76
76
|
outcome: { action: "cancel" },
|
|
77
77
|
timestamp: at,
|
|
78
78
|
});
|
|
79
|
+
// Whether a tool that may have run was open at the cut: only then is its outcome unknown.
|
|
80
|
+
let toolRan = false;
|
|
79
81
|
for (const entry of state.timeline) {
|
|
80
82
|
if (entry.kind !== "message" || entry.turnId !== turnId) continue;
|
|
81
83
|
// A tool keeps running after the model message that called it ends, and
|
|
@@ -84,6 +86,8 @@ export function recoveryFacts(history: Iterable<unknown>, turn: CutOffTurn): Ses
|
|
|
84
86
|
if ("raw" in part) continue;
|
|
85
87
|
const where = address.get(part.id);
|
|
86
88
|
const closed = where && closePart(state, turnId, part, at, turn.tails.get(part.id));
|
|
89
|
+
if (closed?.type === "tool" && closed.state.status === "failed")
|
|
90
|
+
toolRan ||= closed.state.closure === "interrupted";
|
|
87
91
|
if (where && closed)
|
|
88
92
|
facts.push({
|
|
89
93
|
type: "message.part",
|
|
@@ -103,6 +107,11 @@ export function recoveryFacts(history: Iterable<unknown>, turn: CutOffTurn): Ses
|
|
|
103
107
|
timestamp: at,
|
|
104
108
|
});
|
|
105
109
|
}
|
|
110
|
+
// @ref LLP 0003#commit-before-acknowledging — what the restart found, never what to do:
|
|
111
|
+
// the history outlives a fence, which `recoveryRequired` alone says is still up.
|
|
112
|
+
const agent = turn.fence
|
|
113
|
+
? `Its agent could not be confirmed stopped at restart (${turn.fence})`
|
|
114
|
+
: "Its agent was stopped at restart";
|
|
106
115
|
facts.push({
|
|
107
116
|
type: "turn.ended",
|
|
108
117
|
sessionId,
|
|
@@ -110,9 +119,7 @@ export function recoveryFacts(history: Iterable<unknown>, turn: CutOffTurn): Ses
|
|
|
110
119
|
stopReason: "error",
|
|
111
120
|
error: {
|
|
112
121
|
category: "server_restart",
|
|
113
|
-
message: turn.
|
|
114
|
-
? `Server restarted before the turn settled and could not confirm its agent stopped (${turn.fence}); check it before continuing.`
|
|
115
|
-
: "Server restarted before the turn settled. Its agent was stopped at restart; what its running tool did is unknown.",
|
|
122
|
+
message: `Server restarted before the turn settled. ${agent}${toolRan ? "; what its running tool did is unknown" : ""}.`,
|
|
116
123
|
},
|
|
117
124
|
timestamp: at,
|
|
118
125
|
});
|
package/src/sync/schema.ts
CHANGED
|
@@ -6,8 +6,10 @@ import type { Db } from "./sql.ts";
|
|
|
6
6
|
* tail is the whole part; version 5 keeps the native processes its owners
|
|
7
7
|
* started, and why a fence could not be lifted; version 4 keeps each
|
|
8
8
|
* streaming part's unsnapshotted tail for recovery; version 3 stored no token
|
|
9
|
-
* deltas (wire v3); version 2 journals kept them at their own seq.
|
|
10
|
-
|
|
9
|
+
* deltas (wire v3); version 2 journals kept them at their own seq. Exported
|
|
10
|
+
* from `./sync`, which loads no SQLite driver: a product that starts a new
|
|
11
|
+
* journal file per schema names the file from it. */
|
|
12
|
+
export const JOURNAL_SCHEMA_VERSION = 6;
|
|
11
13
|
|
|
12
14
|
const DDL = `
|
|
13
15
|
CREATE TABLE identity (id TEXT PRIMARY KEY);
|
|
@@ -57,15 +59,15 @@ CREATE TABLE checkpoints (
|
|
|
57
59
|
source_id TEXT NOT NULL, session_id TEXT NOT NULL, format TEXT NOT NULL,
|
|
58
60
|
seq INTEGER NOT NULL, body TEXT NOT NULL,
|
|
59
61
|
PRIMARY KEY (source_id, session_id, format));
|
|
60
|
-
PRAGMA user_version = ${
|
|
62
|
+
PRAGMA user_version = ${JOURNAL_SCHEMA_VERSION};
|
|
61
63
|
`;
|
|
62
64
|
|
|
63
65
|
/** Create or check the schema; returns the store's identity, minting it once. */
|
|
64
66
|
export function openSchema(db: Db, newId: () => string): string {
|
|
65
67
|
const { user_version: version } = db.one<{ user_version: number }>("PRAGMA user_version")!;
|
|
66
|
-
if (version !== 0 && version !==
|
|
68
|
+
if (version !== 0 && version !== JOURNAL_SCHEMA_VERSION)
|
|
67
69
|
throw new Error(
|
|
68
|
-
`Unsupported journal schema ${version}; this build reads schema ${
|
|
70
|
+
`Unsupported journal schema ${version}; this build reads schema ${JOURNAL_SCHEMA_VERSION}. Open it with the agent-server that wrote it, or start a new journal file`,
|
|
69
71
|
);
|
|
70
72
|
return db.atomic(() => {
|
|
71
73
|
if (version === 0) {
|
package/src/sync/sql.ts
CHANGED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import type { McpSetServersResult } from "@anthropic-ai/claude-agent-sdk";
|
|
2
|
-
import type { McpServerConfig } from "../../../protocol/index.ts";
|
|
3
|
-
import { type ClaudeCodeAgentOptions, type ClaudeSessionConfig, type ClaudeSessionSettings } from "./options.ts";
|
|
4
|
-
import { ClaudeSession } from "./session.ts";
|
|
5
|
-
/** The selected session and whether this invocation owns its new startup. */
|
|
6
|
-
export interface ClaudeSessionSpawn {
|
|
7
|
-
session: ClaudeSession;
|
|
8
|
-
spawned: boolean;
|
|
9
|
-
}
|
|
10
|
-
/**
|
|
11
|
-
* One live `ClaudeSession` per logical session id. Decides, per turn, whether
|
|
12
|
-
* the existing session can be reused (hot-swapping model, mode and MCP) or
|
|
13
|
-
* must be replaced by a new process that resumes the conversation.
|
|
14
|
-
*/
|
|
15
|
-
export declare class ClaudeSessionPool {
|
|
16
|
-
private readonly settings;
|
|
17
|
-
private readonly sessions;
|
|
18
|
-
/** Started before the login last changed: the next turn replaces the process. */
|
|
19
|
-
private readonly priorLogin;
|
|
20
|
-
constructor(settings: ClaudeSessionSettings & Pick<ClaudeCodeAgentOptions, "onSessionEnd">);
|
|
21
|
-
getOrCreate(sessionId: string, config: ClaudeSessionConfig): Promise<ClaudeSessionSpawn>;
|
|
22
|
-
get(sessionId: string): ClaudeSession | undefined;
|
|
23
|
-
terminate(sessionId: string): Promise<void>;
|
|
24
|
-
terminateAll(): Promise<void>;
|
|
25
|
-
invalidateMcpConnections(): void;
|
|
26
|
-
/** A running CLI keeps the credentials it started with: each session's next turn starts a new one. */
|
|
27
|
-
loginChanged(): void;
|
|
28
|
-
/**
|
|
29
|
-
* Explicit live MCP swap (e.g. mid-conversation app registration). NOTE:
|
|
30
|
-
* this syncs the session's servers, so the NEXT turn's wire config (if it
|
|
31
|
-
* still carries the old server set) will swap back — explicit swappers
|
|
32
|
-
* must keep their per-turn `mcpServers` in step.
|
|
33
|
-
*/
|
|
34
|
-
setMcpServers(sessionId: string, servers: Record<string, McpServerConfig>): Promise<McpSetServersResult | undefined>;
|
|
35
|
-
}
|
|
36
|
-
//# sourceMappingURL=session-pool.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"session-pool.d.ts","sourceRoot":"","sources":["../../../../../src/core/agents/claude-code/session-pool.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,gCAAgC,CAAC;AAC1E,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,4BAA4B,CAAC;AAClE,OAAO,EACN,KAAK,sBAAsB,EAC3B,KAAK,mBAAmB,EACxB,KAAK,qBAAqB,EAE1B,MAAM,cAAc,CAAC;AACtB,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAE7C,6EAA6E;AAC7E,MAAM,WAAW,kBAAkB;IAClC,OAAO,EAAE,aAAa,CAAC;IACvB,OAAO,EAAE,OAAO,CAAC;CACjB;AA6BD;;;;GAIG;AACH,qBAAa,iBAAiB;IAM5B,OAAO,CAAC,QAAQ,CAAC,QAAQ;IAL1B,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAoC;IAC7D,iFAAiF;IACjF,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAgC;gBAGzC,QAAQ,EAAE,qBAAqB,GAAG,IAAI,CAAC,sBAAsB,EAAE,cAAc,CAAC;IAG1F,WAAW,CAAC,SAAS,EAAE,MAAM,EAAE,MAAM,EAAE,mBAAmB,GAAG,OAAO,CAAC,kBAAkB,CAAC;IAuC9F,GAAG,CAAC,SAAS,EAAE,MAAM,GAAG,aAAa,GAAG,SAAS;IAI3C,SAAS,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAO3C,YAAY,IAAI,OAAO,CAAC,IAAI,CAAC;IAKnC,wBAAwB,IAAI,IAAI;IAIhC,sGAAsG;IACtG,YAAY,IAAI,IAAI;IAIpB;;;;;OAKG;IACG,aAAa,CAClB,SAAS,EAAE,MAAM,EACjB,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,eAAe,CAAC,GACtC,OAAO,CAAC,mBAAmB,GAAG,SAAS,CAAC;CAK3C"}
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Idle time before an unused session's subprocess is shut down, unless its
|
|
3
|
-
* harness's `idleTimeoutMs` says otherwise.
|
|
4
|
-
*/
|
|
5
|
-
export declare const IDLE_TIMEOUT_MS: number;
|
|
6
|
-
/** The slice of a session a SessionPool manages: a closable client + idle timer. */
|
|
7
|
-
export interface PooledSession {
|
|
8
|
-
client: {
|
|
9
|
-
closed: boolean;
|
|
10
|
-
close(): void;
|
|
11
|
-
};
|
|
12
|
-
idleTimer?: ReturnType<typeof setTimeout>;
|
|
13
|
-
}
|
|
14
|
-
/**
|
|
15
|
-
* Per-logical-session bookkeeping shared by subprocess-backed harnesses
|
|
16
|
-
* (codex-app-server, acp): the sessionId → session map, idle-eviction timers,
|
|
17
|
-
* self-healing when a client dies underneath us, and whole-pool teardown.
|
|
18
|
-
* Harnesses own what a session *is*; this owns when one goes away.
|
|
19
|
-
*/
|
|
20
|
-
export declare class SessionPool<S extends PooledSession> {
|
|
21
|
-
private readonly sessions;
|
|
22
|
-
private readonly pending;
|
|
23
|
-
private closed;
|
|
24
|
-
/** Own a client before its initialization RPCs have completed. */
|
|
25
|
-
trackPending(sessionId: string, client: S["client"]): () => void;
|
|
26
|
-
get(sessionId: string): S | undefined;
|
|
27
|
-
set(sessionId: string, session: S): void;
|
|
28
|
-
values(): IterableIterator<S>;
|
|
29
|
-
/** Native clients include both retained sessions and unfinished initialization. */
|
|
30
|
-
clients(): IterableIterator<S["client"]>;
|
|
31
|
-
/** Self-heal: drop the mapping when this exact client closed underneath us. */
|
|
32
|
-
dropIfCurrent(sessionId: string, client: S["client"]): void;
|
|
33
|
-
/** Tear down one session: cancel its idle timer, unmap it, close its client. */
|
|
34
|
-
close(sessionId: string): void;
|
|
35
|
-
/** Tear down ready and initializing clients; refuse subsequent registrations. */
|
|
36
|
-
closeAll(): void;
|
|
37
|
-
/** Shut the session down after `idleMs` unless another turn claims it first. */
|
|
38
|
-
armIdle(sessionId: string, session: S, idleMs: number): void;
|
|
39
|
-
clearIdle(session: S): void;
|
|
40
|
-
}
|
|
41
|
-
//# sourceMappingURL=session-pool.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"session-pool.d.ts","sourceRoot":"","sources":["../../../../src/core/agents/session-pool.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,eAAO,MAAM,eAAe,QAAa,CAAC;AAE1C,oFAAoF;AACpF,MAAM,WAAW,aAAa;IAC7B,MAAM,EAAE;QAAE,MAAM,EAAE,OAAO,CAAC;QAAC,KAAK,IAAI,IAAI,CAAA;KAAE,CAAC;IAC3C,SAAS,CAAC,EAAE,UAAU,CAAC,OAAO,UAAU,CAAC,CAAC;CAC1C;AAED;;;;;GAKG;AACH,qBAAa,WAAW,CAAC,CAAC,SAAS,aAAa;IAC/C,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAwB;IACjD,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAuC;IAC/D,OAAO,CAAC,MAAM,CAAS;IAEvB,kEAAkE;IAClE,YAAY,CAAC,SAAS,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC,CAAC,QAAQ,CAAC,GAAG,MAAM,IAAI;IAmBhE,GAAG,CAAC,SAAS,EAAE,MAAM,GAAG,CAAC,GAAG,SAAS;IAIrC,GAAG,CAAC,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC,GAAG,IAAI;IAQxC,MAAM,IAAI,gBAAgB,CAAC,CAAC,CAAC;IAI7B,mFAAmF;IAClF,OAAO,IAAI,gBAAgB,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC;IAKzC,+EAA+E;IAC/E,aAAa,CAAC,SAAS,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC,CAAC,QAAQ,CAAC,GAAG,IAAI;IAQ3D,gFAAgF;IAChF,KAAK,CAAC,SAAS,EAAE,MAAM,GAAG,IAAI;IAU9B,iFAAiF;IACjF,QAAQ,IAAI,IAAI;IAOhB,gFAAgF;IAChF,OAAO,CAAC,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC,EAAE,MAAM,EAAE,MAAM,GAAG,IAAI;IAU5D,SAAS,CAAC,OAAO,EAAE,CAAC,GAAG,IAAI;CAM3B"}
|
|
@@ -1,136 +0,0 @@
|
|
|
1
|
-
import type { McpSetServersResult } from "@anthropic-ai/claude-agent-sdk";
|
|
2
|
-
import type { McpServerConfig } from "../../../protocol/index.ts";
|
|
3
|
-
import {
|
|
4
|
-
type ClaudeCodeAgentOptions,
|
|
5
|
-
type ClaudeSessionConfig,
|
|
6
|
-
type ClaudeSessionSettings,
|
|
7
|
-
claudeSpawnKey,
|
|
8
|
-
} from "./options.ts";
|
|
9
|
-
import { ClaudeSession } from "./session.ts";
|
|
10
|
-
|
|
11
|
-
/** The selected session and whether this invocation owns its new startup. */
|
|
12
|
-
export interface ClaudeSessionSpawn {
|
|
13
|
-
session: ClaudeSession;
|
|
14
|
-
spawned: boolean;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Whether the live `session` cannot serve a turn asking for `config`: what its
|
|
19
|
-
* process was started with changed, the turn names another conversation (a
|
|
20
|
-
* warm process cannot switch), or host suspension left it a stale network pool.
|
|
21
|
-
*/
|
|
22
|
-
function mustReplace(session: ClaudeSession, config: ClaudeSessionConfig): boolean {
|
|
23
|
-
const otherConversation =
|
|
24
|
-
Boolean(config.resumeSessionId) &&
|
|
25
|
-
config.resumeSessionId !== session.currentSessionId &&
|
|
26
|
-
config.resumeSessionId !== session.currentConfig.resumeSessionId;
|
|
27
|
-
return (
|
|
28
|
-
otherConversation || session.mcp.respawnRequired || session.spawnKey !== claudeSpawnKey(config)
|
|
29
|
-
);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/** Switch what the live process can switch: its model, its permission mode, its MCP servers. */
|
|
33
|
-
async function hotSwap(session: ClaudeSession, next: ClaudeSessionConfig): Promise<void> {
|
|
34
|
-
if (next.model !== session.currentConfig.model) await session.setModel(next.model);
|
|
35
|
-
if ((next.permissionMode ?? "default") !== (session.currentConfig.permissionMode ?? "default")) {
|
|
36
|
-
await session.setPermissionMode(next.permissionMode);
|
|
37
|
-
}
|
|
38
|
-
// Text-only sessions must stay tool-free when the MCP configuration changes.
|
|
39
|
-
if (!next.disableTools && session.mcp.needsUpdate(next.mcpServers)) {
|
|
40
|
-
await session.setMcpServers(next.mcpServers ?? {});
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* One live `ClaudeSession` per logical session id. Decides, per turn, whether
|
|
46
|
-
* the existing session can be reused (hot-swapping model, mode and MCP) or
|
|
47
|
-
* must be replaced by a new process that resumes the conversation.
|
|
48
|
-
*/
|
|
49
|
-
export class ClaudeSessionPool {
|
|
50
|
-
private readonly sessions = new Map<string, ClaudeSession>();
|
|
51
|
-
/** Started before the login last changed: the next turn replaces the process. */
|
|
52
|
-
private readonly priorLogin = new WeakSet<ClaudeSession>();
|
|
53
|
-
|
|
54
|
-
constructor(
|
|
55
|
-
private readonly settings: ClaudeSessionSettings & Pick<ClaudeCodeAgentOptions, "onSessionEnd">,
|
|
56
|
-
) {}
|
|
57
|
-
|
|
58
|
-
async getOrCreate(sessionId: string, config: ClaudeSessionConfig): Promise<ClaudeSessionSpawn> {
|
|
59
|
-
const existing = this.sessions.get(sessionId);
|
|
60
|
-
let replaced: ClaudeSession | undefined;
|
|
61
|
-
// One ending meanwhile (idle eviction) is stopped before another
|
|
62
|
-
// process resumes its conversation (LLP 0001, D10).
|
|
63
|
-
if (existing?.currentState === "terminated") await existing.terminate();
|
|
64
|
-
else if (existing) {
|
|
65
|
-
if (!mustReplace(existing, config) && !this.priorLogin.has(existing)) {
|
|
66
|
-
await hotSwap(existing, config);
|
|
67
|
-
return { session: existing, spawned: false };
|
|
68
|
-
}
|
|
69
|
-
// Host suspension invalidates the process-wide HTTP pool, including
|
|
70
|
-
// sockets for removed MCP servers. Use the same strict saved-history
|
|
71
|
-
// resume as idle eviction, with the next turn's prepared config.
|
|
72
|
-
await existing.terminate("replaced");
|
|
73
|
-
this.sessions.delete(sessionId);
|
|
74
|
-
replaced = existing;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
const session = new ClaudeSession(sessionId, config, this.settings, (reason) => {
|
|
78
|
-
if (this.sessions.get(sessionId) === session) this.sessions.delete(sessionId);
|
|
79
|
-
this.settings.onSessionEnd?.(sessionId, reason);
|
|
80
|
-
});
|
|
81
|
-
// The same native conversation continues in a new process, and so does
|
|
82
|
-
// its running cost: the new process restores the total the old one saved.
|
|
83
|
-
if (replaced?.currentSessionId && replaced.currentSessionId === config.resumeSessionId)
|
|
84
|
-
session.spend.continueFrom(replaced.spend);
|
|
85
|
-
this.sessions.set(sessionId, session);
|
|
86
|
-
try {
|
|
87
|
-
await session.start();
|
|
88
|
-
} catch (err) {
|
|
89
|
-
// Don't leave a half-started session in the map — it could never serve a turn.
|
|
90
|
-
await session.terminate();
|
|
91
|
-
if (this.sessions.get(sessionId) === session) this.sessions.delete(sessionId);
|
|
92
|
-
throw err;
|
|
93
|
-
}
|
|
94
|
-
return { session, spawned: true };
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
get(sessionId: string): ClaudeSession | undefined {
|
|
98
|
-
return this.sessions.get(sessionId);
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
async terminate(sessionId: string): Promise<void> {
|
|
102
|
-
const session = this.sessions.get(sessionId);
|
|
103
|
-
if (!session) return;
|
|
104
|
-
await session.terminate();
|
|
105
|
-
if (this.sessions.get(sessionId) === session) this.sessions.delete(sessionId);
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
async terminateAll(): Promise<void> {
|
|
109
|
-
await Promise.all([...this.sessions.values()].map((s) => s.terminate("shutdown")));
|
|
110
|
-
this.sessions.clear();
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
invalidateMcpConnections(): void {
|
|
114
|
-
for (const session of this.sessions.values()) session.invalidateMcpConnections();
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
/** A running CLI keeps the credentials it started with: each session's next turn starts a new one. */
|
|
118
|
-
loginChanged(): void {
|
|
119
|
-
for (const session of this.sessions.values()) this.priorLogin.add(session);
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
/**
|
|
123
|
-
* Explicit live MCP swap (e.g. mid-conversation app registration). NOTE:
|
|
124
|
-
* this syncs the session's servers, so the NEXT turn's wire config (if it
|
|
125
|
-
* still carries the old server set) will swap back — explicit swappers
|
|
126
|
-
* must keep their per-turn `mcpServers` in step.
|
|
127
|
-
*/
|
|
128
|
-
async setMcpServers(
|
|
129
|
-
sessionId: string,
|
|
130
|
-
servers: Record<string, McpServerConfig>,
|
|
131
|
-
): Promise<McpSetServersResult | undefined> {
|
|
132
|
-
const session = this.sessions.get(sessionId);
|
|
133
|
-
if (!session || session.currentState === "terminated") return undefined;
|
|
134
|
-
return session.setMcpServers(servers);
|
|
135
|
-
}
|
|
136
|
-
}
|
|
@@ -1,111 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Idle time before an unused session's subprocess is shut down, unless its
|
|
3
|
-
* harness's `idleTimeoutMs` says otherwise.
|
|
4
|
-
*/
|
|
5
|
-
export const IDLE_TIMEOUT_MS = 5 * 60_000;
|
|
6
|
-
|
|
7
|
-
/** The slice of a session a SessionPool manages: a closable client + idle timer. */
|
|
8
|
-
export interface PooledSession {
|
|
9
|
-
client: { closed: boolean; close(): void };
|
|
10
|
-
idleTimer?: ReturnType<typeof setTimeout>;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Per-logical-session bookkeeping shared by subprocess-backed harnesses
|
|
15
|
-
* (codex-app-server, acp): the sessionId → session map, idle-eviction timers,
|
|
16
|
-
* self-healing when a client dies underneath us, and whole-pool teardown.
|
|
17
|
-
* Harnesses own what a session *is*; this owns when one goes away.
|
|
18
|
-
*/
|
|
19
|
-
export class SessionPool<S extends PooledSession> {
|
|
20
|
-
private readonly sessions = new Map<string, S>();
|
|
21
|
-
private readonly pending = new Map<string, Set<S["client"]>>();
|
|
22
|
-
private closed = false;
|
|
23
|
-
|
|
24
|
-
/** Own a client before its initialization RPCs have completed. */
|
|
25
|
-
trackPending(sessionId: string, client: S["client"]): () => void {
|
|
26
|
-
if (this.closed) {
|
|
27
|
-
client.close();
|
|
28
|
-
throw new Error("session pool is closed");
|
|
29
|
-
}
|
|
30
|
-
let clients = this.pending.get(sessionId);
|
|
31
|
-
if (!clients) {
|
|
32
|
-
clients = new Set();
|
|
33
|
-
this.pending.set(sessionId, clients);
|
|
34
|
-
}
|
|
35
|
-
clients.add(client);
|
|
36
|
-
return () => {
|
|
37
|
-
clients.delete(client);
|
|
38
|
-
if (!clients.size && this.pending.get(sessionId) === clients) {
|
|
39
|
-
this.pending.delete(sessionId);
|
|
40
|
-
}
|
|
41
|
-
};
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
get(sessionId: string): S | undefined {
|
|
45
|
-
return this.sessions.get(sessionId);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
set(sessionId: string, session: S): void {
|
|
49
|
-
if (this.closed || session.client.closed) {
|
|
50
|
-
session.client.close();
|
|
51
|
-
throw new Error("cannot register a closed session");
|
|
52
|
-
}
|
|
53
|
-
this.sessions.set(sessionId, session);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
values(): IterableIterator<S> {
|
|
57
|
-
return this.sessions.values();
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/** Native clients include both retained sessions and unfinished initialization. */
|
|
61
|
-
*clients(): IterableIterator<S["client"]> {
|
|
62
|
-
for (const session of this.sessions.values()) yield session.client;
|
|
63
|
-
for (const clients of this.pending.values()) yield* clients;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/** Self-heal: drop the mapping when this exact client closed underneath us. */
|
|
67
|
-
dropIfCurrent(sessionId: string, client: S["client"]): void {
|
|
68
|
-
const current = this.sessions.get(sessionId);
|
|
69
|
-
if (current && current.client === client) {
|
|
70
|
-
this.clearIdle(current);
|
|
71
|
-
this.sessions.delete(sessionId);
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
/** Tear down one session: cancel its idle timer, unmap it, close its client. */
|
|
76
|
-
close(sessionId: string): void {
|
|
77
|
-
const pending = this.pending.get(sessionId);
|
|
78
|
-
this.pending.delete(sessionId);
|
|
79
|
-
const session = this.sessions.get(sessionId);
|
|
80
|
-
this.sessions.delete(sessionId);
|
|
81
|
-
if (session) this.clearIdle(session);
|
|
82
|
-
for (const client of pending ?? []) client.close();
|
|
83
|
-
session?.client.close();
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/** Tear down ready and initializing clients; refuse subsequent registrations. */
|
|
87
|
-
closeAll(): void {
|
|
88
|
-
this.closed = true;
|
|
89
|
-
for (const id of new Set([...this.sessions.keys(), ...this.pending.keys()])) {
|
|
90
|
-
this.close(id);
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
/** Shut the session down after `idleMs` unless another turn claims it first. */
|
|
95
|
-
armIdle(sessionId: string, session: S, idleMs: number): void {
|
|
96
|
-
this.clearIdle(session);
|
|
97
|
-
session.idleTimer = setTimeout(() => {
|
|
98
|
-
if (this.sessions.get(sessionId) !== session) return;
|
|
99
|
-
this.sessions.delete(sessionId);
|
|
100
|
-
session.client.close();
|
|
101
|
-
}, idleMs);
|
|
102
|
-
session.idleTimer.unref?.();
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
clearIdle(session: S): void {
|
|
106
|
-
if (session.idleTimer) {
|
|
107
|
-
clearTimeout(session.idleTimer);
|
|
108
|
-
session.idleTimer = undefined;
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
}
|