@zvada/agent-server 0.3.21 → 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 +332 -0
- package/README.md +2 -1
- 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 +66 -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 +4 -3
- 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 +21 -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 +61 -14
- 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 +26 -5
- 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 +45 -20
- 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 +32 -5
- 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 +70 -33
- 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 +37 -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/plan.d.ts +2 -13
- package/dist/types/core/agents/plan.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 +6 -1
- 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 +18 -9
- 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 +14 -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 +15 -1
- package/dist/types/core/utils/errors.d.ts.map +1 -1
- package/dist/types/operator/index.d.ts +11 -0
- package/dist/types/operator/index.d.ts.map +1 -0
- package/dist/types/operator/mcp.d.ts +33 -0
- package/dist/types/operator/mcp.d.ts.map +1 -0
- package/dist/types/operator/operator.d.ts +206 -0
- package/dist/types/operator/operator.d.ts.map +1 -0
- package/dist/types/operator/render.d.ts +89 -0
- package/dist/types/operator/render.d.ts.map +1 -0
- package/dist/types/operator/view.d.ts +44 -0
- package/dist/types/operator/view.d.ts.map +1 -0
- 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/plan.d.ts +20 -0
- package/dist/types/protocol/plan.d.ts.map +1 -0
- 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/dist/types/sync/worker.d.ts.map +1 -1
- package/docs/consuming.md +464 -40
- package/docs/deploy.md +36 -8
- package/docs/durable-sessions.md +35 -5
- package/docs/harnesses.md +396 -30
- package/package.json +11 -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 +113 -22
- package/src/client/index.ts +1 -0
- package/src/client/logins.ts +24 -15
- 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 +69 -10
- package/src/core/agents/codex-app-server/codex-agent.ts +141 -55
- 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 +59 -10
- 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 +142 -26
- package/src/core/agents/codex-app-server/models.ts +9 -8
- package/src/core/agents/codex-app-server/options.ts +79 -11
- 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 +156 -81
- 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 +96 -35
- 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/plan.ts +3 -19
- 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 +12 -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 +70 -34
- 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 +53 -3
- package/src/core/utils/deadline.ts +23 -0
- package/src/core/utils/errors.ts +24 -4
- package/src/operator/index.ts +40 -0
- package/src/operator/mcp.ts +171 -0
- package/src/operator/operator.ts +782 -0
- package/src/operator/render.ts +473 -0
- package/src/operator/view.ts +82 -0
- 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/plan.ts +35 -0
- package/src/protocol/tool-state.ts +14 -1
- package/src/protocol/wire.ts +14 -0
- package/src/server/acp/binding.ts +113 -22
- 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/src/sync/worker.ts +4 -0
- 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
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import type { ToolLocation, ToolResultContent } from "../../../protocol/index.ts";
|
|
2
|
+
import { isRecord } from "../../utils/is-record.ts";
|
|
3
|
+
import { hunkDiff } from "../diffs.ts";
|
|
4
|
+
|
|
5
|
+
/** What a finished Claude tool shows beyond its text: diffs, their places, an exit code. */
|
|
6
|
+
export interface ClaudeResultView {
|
|
7
|
+
content?: ToolResultContent[];
|
|
8
|
+
locations?: ToolLocation[];
|
|
9
|
+
metadata?: Record<string, unknown>;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** How the CLI begins a failed `Bash` result: its only report of a code. */
|
|
13
|
+
const EXIT_CODE = /^Exit code (\d+)(?:\n|$)/;
|
|
14
|
+
|
|
15
|
+
/** An `Edit`'s or an updating `Write`'s `structuredPatch`, and each hunk's start in the new file. */
|
|
16
|
+
function patchView(path: string, patch: unknown[]): ClaudeResultView {
|
|
17
|
+
const content: ToolResultContent[] = [];
|
|
18
|
+
const locations: ToolLocation[] = [];
|
|
19
|
+
for (const hunk of patch) {
|
|
20
|
+
if (!isRecord(hunk) || !Array.isArray(hunk.lines)) continue;
|
|
21
|
+
const entry = hunkDiff(
|
|
22
|
+
path,
|
|
23
|
+
hunk.lines.filter((line): line is string => typeof line === "string"),
|
|
24
|
+
);
|
|
25
|
+
if (!entry) continue;
|
|
26
|
+
content.push(entry);
|
|
27
|
+
locations.push({
|
|
28
|
+
path,
|
|
29
|
+
...(typeof hunk.newStart === "number" && { line: hunk.newStart }),
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
return content.length ? { content, locations } : {};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// @ref LLP 0002#tool-output-has-three-audiences — the CLI's structured result, not its text, makes the diff
|
|
36
|
+
/**
|
|
37
|
+
* What a Claude tool's result shows, read from the CLI's structured result
|
|
38
|
+
* (`tool_use_result` on its `user` message) and, for a failed command, its
|
|
39
|
+
* text. A failure shows no diff. A command that succeeded carries no exit
|
|
40
|
+
* code: the CLI reports none, and counts some non-zero exits as success
|
|
41
|
+
* (`grep` finding nothing).
|
|
42
|
+
*/
|
|
43
|
+
export function claudeResultView(
|
|
44
|
+
toolName: string,
|
|
45
|
+
structured: unknown,
|
|
46
|
+
output: string,
|
|
47
|
+
isError: boolean,
|
|
48
|
+
): ClaudeResultView {
|
|
49
|
+
if (isError) {
|
|
50
|
+
const code = toolName === "Bash" ? EXIT_CODE.exec(output)?.[1] : undefined;
|
|
51
|
+
return code !== undefined ? { metadata: { exitCode: Number(code) } } : {};
|
|
52
|
+
}
|
|
53
|
+
if (!isRecord(structured)) return {};
|
|
54
|
+
if (toolName === "Edit" || toolName === "Write") {
|
|
55
|
+
const path = structured.filePath;
|
|
56
|
+
if (typeof path !== "string") return {};
|
|
57
|
+
if (structured.type === "create" && typeof structured.content === "string")
|
|
58
|
+
return {
|
|
59
|
+
content: [{ type: "diff", path, newText: structured.content }],
|
|
60
|
+
locations: [{ path }],
|
|
61
|
+
};
|
|
62
|
+
return Array.isArray(structured.structuredPatch)
|
|
63
|
+
? patchView(path, structured.structuredPatch)
|
|
64
|
+
: {};
|
|
65
|
+
}
|
|
66
|
+
if (toolName === "NotebookEdit") {
|
|
67
|
+
const { notebook_path: path, old_source: oldText, new_source: newText } = structured;
|
|
68
|
+
if (typeof path !== "string" || typeof oldText !== "string" || typeof newText !== "string")
|
|
69
|
+
return {};
|
|
70
|
+
return { content: [{ type: "diff", path, oldText, newText }] };
|
|
71
|
+
}
|
|
72
|
+
return {};
|
|
73
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { dirname, join } from "node:path";
|
|
3
|
+
import { type DiagnosticHandler, emitDiagnostic } from "../../diagnostics.ts";
|
|
4
|
+
|
|
5
|
+
/** The pairs already said in this OS process: each is said once. */
|
|
6
|
+
const said = new Set<string>();
|
|
7
|
+
|
|
8
|
+
/** A CLI build's platform package: `@anthropic-ai/claude-agent-sdk-<platform>`, the SDK's own numbering. */
|
|
9
|
+
const PLATFORM_PACKAGE = /^@anthropic-ai\/claude-agent-sdk-[a-z0-9-]+$/;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* The release of the CLI build at `cliPath`: the version of the platform
|
|
13
|
+
* package it came in, whose manifest sits beside it (the provisioner's cache,
|
|
14
|
+
* a staged bundle, the package beside an installed SDK). Nothing for a CLI
|
|
15
|
+
* from anywhere else, which names no SDK release.
|
|
16
|
+
*/
|
|
17
|
+
function cliBuildVersion(cliPath: string): string | undefined {
|
|
18
|
+
try {
|
|
19
|
+
const manifest = JSON.parse(readFileSync(join(dirname(cliPath), "package.json"), "utf8")) as {
|
|
20
|
+
name?: unknown;
|
|
21
|
+
version?: unknown;
|
|
22
|
+
};
|
|
23
|
+
return typeof manifest.name === "string" &&
|
|
24
|
+
PLATFORM_PACKAGE.test(manifest.name) &&
|
|
25
|
+
typeof manifest.version === "string"
|
|
26
|
+
? manifest.version
|
|
27
|
+
: undefined;
|
|
28
|
+
} catch {
|
|
29
|
+
return undefined;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Say, once per OS process for each pair, that the Claude SDK that loaded and
|
|
35
|
+
* the CLI build it drives are different releases: a package manager let
|
|
36
|
+
* another SDK in (bun says nothing of an exact peer), or the CLI came from a
|
|
37
|
+
* pin the SDK did not follow. Call it right after `query()`: the SDK names
|
|
38
|
+
* itself there, in `process.env.CLAUDE_AGENT_SDK_VERSION`, from a literal in
|
|
39
|
+
* its own code, so a bundle names it too and no file is read for it. A
|
|
40
|
+
* warning, through `onDiagnostic` and the log; it never fails a turn, and an
|
|
41
|
+
* SDK or a CLI that cannot be named is not compared.
|
|
42
|
+
*/
|
|
43
|
+
export function notePairedReleases(
|
|
44
|
+
cliPath: string | undefined,
|
|
45
|
+
onDiagnostic: DiagnosticHandler | undefined,
|
|
46
|
+
sessionId: string,
|
|
47
|
+
): void {
|
|
48
|
+
try {
|
|
49
|
+
const sdkVersion = process.env.CLAUDE_AGENT_SDK_VERSION;
|
|
50
|
+
const cliVersion = cliPath ? cliBuildVersion(cliPath) : undefined;
|
|
51
|
+
if (!sdkVersion || !cliVersion || sdkVersion === cliVersion) return;
|
|
52
|
+
const pair = `${sdkVersion}\0${cliPath}\0${cliVersion}`;
|
|
53
|
+
if (said.has(pair)) return;
|
|
54
|
+
said.add(pair);
|
|
55
|
+
const message = `Claude SDK ${sdkVersion} drives the CLI build ${cliVersion} (${cliPath}): they are different releases, so the SDK may ask what this CLI does not know. Run the CLI build of the SDK's release (provision.pins.claude) or the SDK of the CLI's.`;
|
|
56
|
+
console.error(`[agent-server] ${message}`);
|
|
57
|
+
emitDiagnostic(onDiagnostic, {
|
|
58
|
+
type: "claudeSdkMismatch",
|
|
59
|
+
sessionId,
|
|
60
|
+
message,
|
|
61
|
+
detail: { sdkVersion, cliVersion, cliPath },
|
|
62
|
+
});
|
|
63
|
+
} catch {
|
|
64
|
+
// A warning: nothing here may fail the turn.
|
|
65
|
+
}
|
|
66
|
+
}
|
|
@@ -11,13 +11,22 @@ import type {
|
|
|
11
11
|
import { AsyncQueue, generateUUIDv7 } from "../../../protocol/index.ts";
|
|
12
12
|
import type { McpServerConfig, PermissionMode } from "../../../protocol/index.ts";
|
|
13
13
|
import type { NativeProcess } from "../../processes/native-processes.ts";
|
|
14
|
-
import { withTimeout } from "../../utils/deadline.ts";
|
|
14
|
+
import { onAbort, untilAborted, withTimeout } from "../../utils/deadline.ts";
|
|
15
15
|
import { SessionResumeError } from "../../utils/errors.ts";
|
|
16
|
-
import {
|
|
16
|
+
import { CANCEL_CONFIRM_MS } from "../base.ts";
|
|
17
|
+
import {
|
|
18
|
+
IDLE_TIMEOUT_MS,
|
|
19
|
+
type PooledClient,
|
|
20
|
+
type PooledProcess,
|
|
21
|
+
type ProcessEndReason,
|
|
22
|
+
ProcessPool,
|
|
23
|
+
} from "../process-pool.ts";
|
|
17
24
|
import { createCanUseTool, createOnElicitation, startQuery } from "./approvals.ts";
|
|
18
25
|
import { commandStarted } from "./events.ts";
|
|
26
|
+
import { claudeLoginStore } from "./login.ts";
|
|
19
27
|
import { ClaudeMcpConnections } from "./mcp.ts";
|
|
20
28
|
import {
|
|
29
|
+
type ClaudeCodeAgentOptions,
|
|
21
30
|
type ClaudeSessionConfig,
|
|
22
31
|
type ClaudeSessionEndReason,
|
|
23
32
|
type ClaudeSessionSettings,
|
|
@@ -27,6 +36,7 @@ import {
|
|
|
27
36
|
toSdkPermissionMode,
|
|
28
37
|
} from "./options.ts";
|
|
29
38
|
import { planUsedUp } from "./outcome.ts";
|
|
39
|
+
import { notePairedReleases } from "./sdk-pair.ts";
|
|
30
40
|
import { withStderr } from "./spawn.ts";
|
|
31
41
|
import { ClaudeSpend } from "./spend.ts";
|
|
32
42
|
import { ClaudeTurn, type ClaudeTurnStart } from "./turn.ts";
|
|
@@ -34,9 +44,6 @@ import { ClaudeTurn, type ClaudeTurnStart } from "./turn.ts";
|
|
|
34
44
|
type SessionState = "starting" | "idle" | "busy" | "terminated";
|
|
35
45
|
type ClaudeContent = SDKUserMessage["message"]["content"];
|
|
36
46
|
|
|
37
|
-
/** How long Claude has to confirm an interrupt before the turn counts as still running. */
|
|
38
|
-
export const INTERRUPT_TIMEOUT_MS = 2_000;
|
|
39
|
-
|
|
40
47
|
/** Claude repeats a message it was sent, where it reads it (`--replay-user-messages`). */
|
|
41
48
|
function isReplay(event: SDKMessage): event is Extract<SDKMessage, { isReplay: true }> {
|
|
42
49
|
return event.type === "user" && "isReplay" in event && event.isReplay === true;
|
|
@@ -47,6 +54,20 @@ function consumed(event: SDKMessage): readonly string[] {
|
|
|
47
54
|
return ("user_message_uuids" in event && event.user_message_uuids) || [];
|
|
48
55
|
}
|
|
49
56
|
|
|
57
|
+
/** Why the pool ended a session, in the reasons `onSessionEnd` has always given. */
|
|
58
|
+
function endReason(reason: ProcessEndReason | undefined): ClaudeSessionEndReason {
|
|
59
|
+
if (reason === "idle" || reason === "shutdown") return reason;
|
|
60
|
+
return reason?.startsWith("replaced:") ? "replaced" : "released";
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** What a session asks of the pool that holds it. */
|
|
64
|
+
interface ClaudeSessionPooling {
|
|
65
|
+
/** A host resume came since its process began to start (`startedBeforeResume`). */
|
|
66
|
+
hostResumed(): boolean;
|
|
67
|
+
/** Claude answered a query of its own between turns: the idle wait starts again. */
|
|
68
|
+
betweenTurns(): void;
|
|
69
|
+
}
|
|
70
|
+
|
|
50
71
|
/**
|
|
51
72
|
* Wraps one long-lived Claude `query()` call. The prompt input is an AsyncQueue
|
|
52
73
|
* that stays open across turns, so the same subprocess (and its context) is
|
|
@@ -54,9 +75,11 @@ function consumed(event: SDKMessage): readonly string[] {
|
|
|
54
75
|
* of the session (`observe`) and hands it to the running turn (`route`); the
|
|
55
76
|
* `result` answering the turn ends it.
|
|
56
77
|
*
|
|
57
|
-
* Lifecycle: starting → idle ⇄ busy → terminated.
|
|
78
|
+
* Lifecycle: starting → idle ⇄ busy → terminated. To the process pool it is
|
|
79
|
+
* both the process and its client: `closed` once its end began, from any
|
|
80
|
+
* state; `close` ends it whatever it is doing, a running turn with it.
|
|
58
81
|
*/
|
|
59
|
-
export class ClaudeSession {
|
|
82
|
+
export class ClaudeSession implements PooledProcess, PooledClient {
|
|
60
83
|
private state: SessionState = "starting";
|
|
61
84
|
/** What the session runs with; its MCP servers are `mcp`'s. */
|
|
62
85
|
private config: Omit<ClaudeSessionConfig, "mcpServers">;
|
|
@@ -85,25 +108,45 @@ export class ClaudeSession {
|
|
|
85
108
|
* changes, so a turn after a used-up window starts from that report.
|
|
86
109
|
*/
|
|
87
110
|
private readonly planWindows = new Map<string, SDKRateLimitEvent>();
|
|
88
|
-
private idleTimer?: ReturnType<typeof setTimeout>;
|
|
89
111
|
/** The `claude` process the engine started for it; none under an operator's spawner. */
|
|
90
112
|
private native?: NativeProcess;
|
|
91
113
|
/** Its end, once it began: settles when its process is stopped. */
|
|
92
114
|
private ending?: Promise<void>;
|
|
115
|
+
private readonly closeHandlers = new Set<() => void>();
|
|
93
116
|
|
|
94
117
|
constructor(
|
|
95
118
|
readonly id: string,
|
|
96
119
|
config: ClaudeSessionConfig,
|
|
97
120
|
private readonly settings: ClaudeSessionSettings,
|
|
98
121
|
private readonly onTerminated?: (reason: ClaudeSessionEndReason) => void,
|
|
122
|
+
private readonly pooling?: ClaudeSessionPooling,
|
|
99
123
|
) {
|
|
100
124
|
const { mcpServers, ...spawn } = config;
|
|
101
125
|
this.config = spawn;
|
|
102
126
|
this.spawnKey = claudeSpawnKey(config);
|
|
103
|
-
this.mcp = new ClaudeMcpConnections(mcpServers);
|
|
127
|
+
this.mcp = new ClaudeMcpConnections(mcpServers, () => pooling?.hostResumed() ?? false);
|
|
104
128
|
this.spend = config.resumeSessionId ? ClaudeSpend.unknown() : ClaudeSpend.fresh();
|
|
105
129
|
}
|
|
106
130
|
|
|
131
|
+
/** The pool's process is its own client. */
|
|
132
|
+
get client(): this {
|
|
133
|
+
return this;
|
|
134
|
+
}
|
|
135
|
+
get closed(): boolean {
|
|
136
|
+
return this.state === "terminated";
|
|
137
|
+
}
|
|
138
|
+
close(reason?: ProcessEndReason): void {
|
|
139
|
+
void this.terminate(endReason(reason));
|
|
140
|
+
}
|
|
141
|
+
onClose(handler: () => void): () => void {
|
|
142
|
+
this.closeHandlers.add(handler);
|
|
143
|
+
return () => this.closeHandlers.delete(handler);
|
|
144
|
+
}
|
|
145
|
+
/** Settles once its process is stopped and its end reported. */
|
|
146
|
+
gone(): Promise<void> {
|
|
147
|
+
return this.ending ?? Promise.resolve();
|
|
148
|
+
}
|
|
149
|
+
|
|
107
150
|
get currentState(): SessionState {
|
|
108
151
|
return this.state;
|
|
109
152
|
}
|
|
@@ -148,9 +191,30 @@ export class ClaudeSession {
|
|
|
148
191
|
);
|
|
149
192
|
}
|
|
150
193
|
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
194
|
+
// @ref LLP 0013#f13-a-live-change-held-a-claude-turn-a-cancel-could-not-end — a cancel ends the change, and the process
|
|
195
|
+
/**
|
|
196
|
+
* Switch what the live process can switch for the turn: its model, its
|
|
197
|
+
* permission mode, its MCP servers. All else is in its spawn key, so this
|
|
198
|
+
* never asks for a new process. The SDK gives these control requests no
|
|
199
|
+
* deadline: the turn's cancel (`signal`) ends the wait for each, and the
|
|
200
|
+
* process, since what it applied is not known; the session's next turn
|
|
201
|
+
* resumes the conversation on a new one.
|
|
202
|
+
*/
|
|
203
|
+
async applyLiveChange(next: ClaudeSessionConfig, signal: AbortSignal): Promise<boolean> {
|
|
204
|
+
try {
|
|
205
|
+
if (next.model !== this.config.model) await untilAborted(this.setModel(next.model), signal);
|
|
206
|
+
if ((next.permissionMode ?? "default") !== (this.config.permissionMode ?? "default")) {
|
|
207
|
+
await untilAborted(this.setPermissionMode(next.permissionMode), signal);
|
|
208
|
+
}
|
|
209
|
+
// Text-only sessions must stay tool-free when the MCP configuration changes.
|
|
210
|
+
if (!next.disableTools && this.mcp.needsUpdate(next.mcpServers)) {
|
|
211
|
+
await untilAborted(this.setMcpServers(next.mcpServers ?? {}), signal);
|
|
212
|
+
}
|
|
213
|
+
} catch (error) {
|
|
214
|
+
if (signal.aborted) void this.terminate("replaced");
|
|
215
|
+
throw error;
|
|
216
|
+
}
|
|
217
|
+
return true;
|
|
154
218
|
}
|
|
155
219
|
|
|
156
220
|
async start(): Promise<void> {
|
|
@@ -183,6 +247,7 @@ export class ClaudeSession {
|
|
|
183
247
|
sessionId: this.id,
|
|
184
248
|
policy: this.settings.toolPolicy,
|
|
185
249
|
turn,
|
|
250
|
+
mcpServers: () => this.mcp.names,
|
|
186
251
|
});
|
|
187
252
|
options.onElicitation = createOnElicitation(turn);
|
|
188
253
|
// Release/shutdown may finish while an async options/tool factory is
|
|
@@ -191,6 +256,8 @@ export class ClaudeSession {
|
|
|
191
256
|
this.query = startQuery(options.permissionMode, () =>
|
|
192
257
|
sdk.query({ prompt: this.promptQueue, options }),
|
|
193
258
|
);
|
|
259
|
+
// Right after `query()`, which names the SDK that ran it.
|
|
260
|
+
notePairedReleases(this.config.cliPath, this.settings.onDiagnostic, this.id);
|
|
194
261
|
const events = this.consumeEvents();
|
|
195
262
|
if (!this.config.disableTools && Object.keys(this.mcp.servers).length) {
|
|
196
263
|
try {
|
|
@@ -213,7 +280,6 @@ export class ClaudeSession {
|
|
|
213
280
|
if ((this.state as SessionState) === "terminated")
|
|
214
281
|
throw new Error("Session terminated during startup");
|
|
215
282
|
this.state = "idle";
|
|
216
|
-
this.resetIdleTimer();
|
|
217
283
|
}
|
|
218
284
|
|
|
219
285
|
/**
|
|
@@ -224,14 +290,13 @@ export class ClaudeSession {
|
|
|
224
290
|
sendMessage(content: ClaudeContent, start: ClaudeTurnStart): ClaudeTurn {
|
|
225
291
|
if (this.state === "terminated") throw this.startupError ?? new Error("Session is terminated");
|
|
226
292
|
if (this.state !== "idle") throw new Error(`Claude session is ${this.state}, not idle`);
|
|
227
|
-
//
|
|
293
|
+
// A host resume can land during preparation. Never enqueue a prompt on a
|
|
228
294
|
// process whose network pool predates suspension.
|
|
229
295
|
if (this.mcp.respawnRequired || (!this.config.disableTools && this.mcp.uncertain)) {
|
|
230
296
|
throw new Error(
|
|
231
297
|
"MCP network connections changed during turn preparation; session is not ready",
|
|
232
298
|
);
|
|
233
299
|
}
|
|
234
|
-
this.clearIdleTimer();
|
|
235
300
|
this.state = "busy";
|
|
236
301
|
const prompt = this.userMessage(content);
|
|
237
302
|
const turn = new ClaudeTurn(start, prompt.uuid);
|
|
@@ -279,6 +344,11 @@ export class ClaudeSession {
|
|
|
279
344
|
};
|
|
280
345
|
}
|
|
281
346
|
|
|
347
|
+
/** How long Claude has to confirm an interrupt before the turn counts as still running. */
|
|
348
|
+
private get confirmMs(): number {
|
|
349
|
+
return this.settings.cancelConfirmMs ?? CANCEL_CONFIRM_MS;
|
|
350
|
+
}
|
|
351
|
+
|
|
282
352
|
/**
|
|
283
353
|
* Stop the in-flight turn without killing the session. Returns whether the
|
|
284
354
|
* interrupt was CONFIRMED: true when there was nothing to interrupt or the
|
|
@@ -290,7 +360,7 @@ export class ClaudeSession {
|
|
|
290
360
|
* instead: it has begun no turn, an interrupt would wait out its startup, and
|
|
291
361
|
* a prompt the CLI has not dequeued survives an interrupt and runs anyway.
|
|
292
362
|
*/
|
|
293
|
-
interruptTurn(timeoutMs =
|
|
363
|
+
interruptTurn(timeoutMs = this.confirmMs): Promise<boolean> {
|
|
294
364
|
const turn = this.turn;
|
|
295
365
|
if (!turn) return this.interrupt(undefined, timeoutMs);
|
|
296
366
|
return turn.interruptOnce(() => this.interrupt(turn, timeoutMs));
|
|
@@ -302,7 +372,7 @@ export class ClaudeSession {
|
|
|
302
372
|
* and end the process when the turn has not ended within the interrupt
|
|
303
373
|
* timeout. The session is idle or terminated when this returns.
|
|
304
374
|
*/
|
|
305
|
-
async stopTurn(turn: ClaudeTurn, timeoutMs =
|
|
375
|
+
async stopTurn(turn: ClaudeTurn, timeoutMs = this.confirmMs): Promise<void> {
|
|
306
376
|
if (this.turn !== turn) return;
|
|
307
377
|
await Promise.all([this.interruptTurn(timeoutMs), withTimeout(turn.ended, timeoutMs)]);
|
|
308
378
|
if (this.turn === turn) await this.terminate("replaced");
|
|
@@ -363,15 +433,19 @@ export class ClaudeSession {
|
|
|
363
433
|
/**
|
|
364
434
|
* End the session: close its query, and stop its process with its tool
|
|
365
435
|
* sessions and MCP servers. Settles once they are gone (at most the grace);
|
|
366
|
-
* every call shares that end.
|
|
436
|
+
* every call shares that end, and the first one's reason is reported.
|
|
367
437
|
*/
|
|
368
438
|
terminate(reason: ClaudeSessionEndReason = "released"): Promise<void> {
|
|
369
|
-
this.ending
|
|
439
|
+
if (!this.ending) {
|
|
440
|
+
this.ending = this.end(reason);
|
|
441
|
+
// Once `gone()` has its answer: the pool waits on it from its handler.
|
|
442
|
+
for (const handler of this.closeHandlers) handler();
|
|
443
|
+
this.closeHandlers.clear();
|
|
444
|
+
}
|
|
370
445
|
return this.ending;
|
|
371
446
|
}
|
|
372
447
|
|
|
373
448
|
private async end(reason: ClaudeSessionEndReason): Promise<void> {
|
|
374
|
-
this.clearIdleTimer();
|
|
375
449
|
this.state = "terminated";
|
|
376
450
|
const query = this.query;
|
|
377
451
|
this.query = undefined;
|
|
@@ -392,7 +466,7 @@ export class ClaudeSession {
|
|
|
392
466
|
this.onTerminated?.(reason);
|
|
393
467
|
} catch {
|
|
394
468
|
// Observer failures (incl. the host's onSessionEnd) must never make
|
|
395
|
-
// terminate() reject
|
|
469
|
+
// terminate() reject: the pool's idle timer ends it unawaited.
|
|
396
470
|
}
|
|
397
471
|
}
|
|
398
472
|
}
|
|
@@ -421,6 +495,9 @@ export class ClaudeSession {
|
|
|
421
495
|
this.queryBegun = true;
|
|
422
496
|
if (event.session_id) this.nativeSessionId = event.session_id;
|
|
423
497
|
this.cancelsQueued = (event.capabilities ?? []).includes("interrupt_cancel_queued_v1");
|
|
498
|
+
// An older CLI may leave the list out.
|
|
499
|
+
if (Array.isArray(event.mcp_servers))
|
|
500
|
+
this.mcp.sawLoaded(event.mcp_servers.map((server) => server.name));
|
|
424
501
|
}
|
|
425
502
|
// The mode Claude runs in now: approving a plan switches it mid-turn,
|
|
426
503
|
// so the next turn compares against this, not the mode it asked for.
|
|
@@ -487,29 +564,81 @@ export class ClaudeSession {
|
|
|
487
564
|
: new Error(reason);
|
|
488
565
|
}
|
|
489
566
|
this.spend.charge(result);
|
|
490
|
-
if (this.state === "idle") this.
|
|
567
|
+
if (this.state === "idle") this.pooling?.betweenTurns();
|
|
491
568
|
}
|
|
492
569
|
|
|
493
570
|
private endTurn(): void {
|
|
494
571
|
this.turn?.end();
|
|
495
572
|
this.turn = null;
|
|
496
573
|
this.state = "idle";
|
|
497
|
-
this.resetIdleTimer();
|
|
498
574
|
}
|
|
575
|
+
}
|
|
499
576
|
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
}
|
|
577
|
+
/** What a turn brings to the pool. `spawn` notes on it whether its process is new. */
|
|
578
|
+
export interface ClaudePoolTurn {
|
|
579
|
+
sessionId: string;
|
|
580
|
+
config: ClaudeSessionConfig;
|
|
581
|
+
/** The turn's process was started for it: a resume it asks for is not confirmed yet. */
|
|
582
|
+
spawned?: boolean;
|
|
583
|
+
}
|
|
508
584
|
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
585
|
+
/** Each logical session's Claude process, on the pool every harness runs on. */
|
|
586
|
+
export type ClaudeSessionPool = ProcessPool<ClaudeSession, ClaudePoolTurn>;
|
|
587
|
+
|
|
588
|
+
/**
|
|
589
|
+
* An agent's Claude sessions. Claude decides of a warm process only whether
|
|
590
|
+
* its spawn key is the turn's and what it switches live; the pool decides
|
|
591
|
+
* the rest (LLP 0001, D10).
|
|
592
|
+
*/
|
|
593
|
+
export function claudeSessionPool(
|
|
594
|
+
settings: ClaudeSessionSettings & Pick<ClaudeCodeAgentOptions, "onSessionEnd">,
|
|
595
|
+
): ClaudeSessionPool {
|
|
596
|
+
const pool: ClaudeSessionPool = new ProcessPool<ClaudeSession, ClaudePoolTurn>(
|
|
597
|
+
{
|
|
598
|
+
// @ref LLP 0011#a-login-change-starts-the-next-turn-on-a-new-cli — only the store it changed in
|
|
599
|
+
store: ({ config }) => claudeLoginStore(config.env),
|
|
600
|
+
resumeOf: ({ config }) => config.resumeSessionId || undefined,
|
|
601
|
+
// Claude may rekey a conversation it resumed: the id it started on still names it.
|
|
602
|
+
conversationsOf: (session) =>
|
|
603
|
+
[session.currentSessionId, session.currentConfig.resumeSessionId].filter(
|
|
604
|
+
(id): id is string => Boolean(id),
|
|
605
|
+
),
|
|
606
|
+
fits: (session, { config }) => session.spawnKey === claudeSpawnKey(config),
|
|
607
|
+
applyLiveChange: (session, { config }, signal) => session.applyLiveChange(config, signal),
|
|
608
|
+
spawn: async (turn, own, signal, replacing) => {
|
|
609
|
+
const { sessionId, config } = turn;
|
|
610
|
+
const session: ClaudeSession = new ClaudeSession(
|
|
611
|
+
sessionId,
|
|
612
|
+
config,
|
|
613
|
+
settings,
|
|
614
|
+
(reason) => settings.onSessionEnd?.(sessionId, reason),
|
|
615
|
+
{
|
|
616
|
+
hostResumed: () => pool.startedBeforeResume(session),
|
|
617
|
+
betweenTurns: () => pool.touch(sessionId, session),
|
|
618
|
+
},
|
|
619
|
+
);
|
|
620
|
+
// The same native conversation continues in a new process, and so
|
|
621
|
+
// does its running cost: the new process restores the total the old one saved.
|
|
622
|
+
if (replacing?.currentSessionId && replacing.currentSessionId === config.resumeSessionId)
|
|
623
|
+
session.spend.continueFrom(replacing.spend);
|
|
624
|
+
own(session);
|
|
625
|
+
turn.spawned = true;
|
|
626
|
+
// A cancel ends a start under way: it has begun no turn, and an
|
|
627
|
+
// interrupt would wait out its startup.
|
|
628
|
+
const offAbort = onAbort(signal, () => void session.terminate());
|
|
629
|
+
try {
|
|
630
|
+
await session.start();
|
|
631
|
+
return session;
|
|
632
|
+
} catch (error) {
|
|
633
|
+
// Never leave a half-started session: it could serve no turn.
|
|
634
|
+
await session.terminate();
|
|
635
|
+
throw error;
|
|
636
|
+
} finally {
|
|
637
|
+
offAbort();
|
|
638
|
+
}
|
|
639
|
+
},
|
|
640
|
+
},
|
|
641
|
+
{ idleMs: settings.idleTimeoutMs ?? IDLE_TIMEOUT_MS },
|
|
642
|
+
);
|
|
643
|
+
return pool;
|
|
515
644
|
}
|
|
@@ -6,7 +6,7 @@ import type { TokenUsage } from "../../../protocol/index.ts";
|
|
|
6
6
|
* in `modelUsage`: every call through its query pipeline, sub-agents and
|
|
7
7
|
* compaction included, where a result's `usage` counts only the main loop.
|
|
8
8
|
*/
|
|
9
|
-
|
|
9
|
+
interface ClaudeUsageTotals {
|
|
10
10
|
input: number;
|
|
11
11
|
output: number;
|
|
12
12
|
cacheRead: number;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ToolKind, ToolLocation, ToolMeta } from "../../../protocol/index.ts";
|
|
1
|
+
import type { McpTool, ToolKind, ToolLocation, ToolMeta } from "../../../protocol/index.ts";
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Maps Claude Code's tool names onto the normalized ToolKind taxonomy and
|
|
@@ -6,32 +6,67 @@ import type { ToolKind, ToolLocation, ToolMeta } from "../../../protocol/index.t
|
|
|
6
6
|
* tools are `other` and produce no locations.
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
// @ref LLP 0002#a-tool-call-says-what-it-acts-on — every tool the pinned CLI offers has a decided kind
|
|
10
|
+
/**
|
|
11
|
+
* Each Claude tool's kind, decided on what the tool does. A map, not an
|
|
12
|
+
* object: a tool named `constructor` is no object member's kind. The free
|
|
13
|
+
* live check (`claude-tool-kinds.live.test.ts`) fails on a tool the pinned
|
|
14
|
+
* CLI offers that is not here, so a pin bump decides it.
|
|
15
|
+
*/
|
|
16
|
+
export const CLAUDE_TOOL_KINDS: ReadonlyMap<string, ToolKind> = new Map<string, ToolKind>([
|
|
17
|
+
// What the pinned CLI (2.1.288) offers, with the engine's options or a product's.
|
|
11
18
|
["Read", "read"],
|
|
12
|
-
["NotebookRead", "read"],
|
|
13
19
|
["Edit", "edit"],
|
|
14
20
|
["Write", "edit"],
|
|
15
|
-
["MultiEdit", "edit"],
|
|
16
21
|
["NotebookEdit", "edit"],
|
|
17
22
|
["Bash", "execute"],
|
|
18
|
-
|
|
19
|
-
["
|
|
20
|
-
|
|
21
|
-
["
|
|
22
|
-
["LS", "search"],
|
|
23
|
+
// Stops a background shell or sub-agent the agent started.
|
|
24
|
+
["TaskStop", "execute"],
|
|
25
|
+
// Runs a script the agent wrote, which starts sub-agents of its own.
|
|
26
|
+
["Workflow", "execute"],
|
|
23
27
|
["WebFetch", "fetch"],
|
|
24
28
|
["WebSearch", "fetch"],
|
|
29
|
+
// Loads a skill's instructions into the conversation.
|
|
30
|
+
["Skill", "read"],
|
|
31
|
+
// The sub-agent tool: `init` names it `Task`, the model `Agent`. A spawn is `task` by its subagent.
|
|
25
32
|
["Task", "think"],
|
|
26
33
|
["Agent", "think"],
|
|
34
|
+
// The agent's own task list, as `TodoWrite` was.
|
|
35
|
+
["TaskCreate", "think"],
|
|
36
|
+
["TaskGet", "think"],
|
|
37
|
+
["TaskList", "think"],
|
|
38
|
+
["TaskUpdate", "think"],
|
|
27
39
|
["TodoWrite", "think"],
|
|
28
40
|
["ExitPlanMode", "switch_mode"],
|
|
29
41
|
["EnterPlanMode", "switch_mode"],
|
|
42
|
+
// Moves the session into a git worktree and back: no mode, no file of the agent's.
|
|
43
|
+
["EnterWorktree", "other"],
|
|
44
|
+
["ExitWorktree", "other"],
|
|
45
|
+
// A question to the person, through the engine's elicitation.
|
|
46
|
+
["AskUserQuestion", "other"],
|
|
47
|
+
// Talk among agents: which there are, a message to one, the findings sent back.
|
|
48
|
+
["ListAgents", "other"],
|
|
49
|
+
["SendMessage", "other"],
|
|
50
|
+
["ReportFindings", "other"],
|
|
51
|
+
// Prompts the session schedules for later, and the agent's own wake-up.
|
|
52
|
+
["CronCreate", "other"],
|
|
53
|
+
["CronDelete", "other"],
|
|
54
|
+
["CronList", "other"],
|
|
55
|
+
["ScheduleWakeup", "other"],
|
|
56
|
+
// Names the pinned CLI no longer offers: a product may pin an older CLI
|
|
57
|
+
// (`provision.pins`), whose calls keep their kinds.
|
|
58
|
+
["NotebookRead", "read"],
|
|
59
|
+
["MultiEdit", "edit"],
|
|
60
|
+
["BashOutput", "execute"],
|
|
61
|
+
["KillShell", "execute"],
|
|
62
|
+
["Grep", "search"],
|
|
63
|
+
["Glob", "search"],
|
|
64
|
+
["LS", "search"],
|
|
30
65
|
]);
|
|
31
66
|
|
|
32
67
|
const PATH_INPUT_KEYS = ["file_path", "filePath", "path", "notebook_path"] as const;
|
|
33
68
|
|
|
34
|
-
|
|
69
|
+
function toolLocationsFromInput(input: Record<string, unknown>): ToolLocation[] | undefined {
|
|
35
70
|
for (const key of PATH_INPUT_KEYS) {
|
|
36
71
|
const v = input[key];
|
|
37
72
|
if (typeof v === "string" && v) return [{ path: v }];
|
|
@@ -39,9 +74,70 @@ export function toolLocationsFromInput(input: Record<string, unknown>): ToolLoca
|
|
|
39
74
|
return undefined;
|
|
40
75
|
}
|
|
41
76
|
|
|
42
|
-
|
|
77
|
+
const MCP_PREFIX = "mcp__";
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* A server's name as Claude Code spells it in a tool name (the pinned CLI
|
|
81
|
+
* 2.1.288, SDK 0.3.288, and 2.1.325 alike):
|
|
82
|
+
* other than letters, digits, `_` and `-` become `_`, and a claude.ai
|
|
83
|
+
* connector's runs of `_` collapse.
|
|
84
|
+
*/
|
|
85
|
+
function spelledInToolName(server: string): string {
|
|
86
|
+
const spelled = server.replace(/[^a-zA-Z0-9_-]/g, "_");
|
|
87
|
+
return server.startsWith("claude.ai ")
|
|
88
|
+
? spelled.replace(/_+/g, "_").replace(/^_|_$/g, "")
|
|
89
|
+
: spelled;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* The server and tool of a Claude MCP tool name, `mcp__<server>__<tool>`. A
|
|
94
|
+
* server's name may hold `__` itself, so the longest name the session
|
|
95
|
+
* attached that fits wins; with none, the name splits at its first `__`.
|
|
96
|
+
* Undefined for a name that is no MCP tool.
|
|
97
|
+
*/
|
|
98
|
+
export function claudeMcpTool(
|
|
99
|
+
toolName: string,
|
|
100
|
+
servers: Iterable<string> = [],
|
|
101
|
+
): McpTool | undefined {
|
|
102
|
+
if (!toolName.startsWith(MCP_PREFIX)) return undefined;
|
|
103
|
+
const rest = toolName.slice(MCP_PREFIX.length);
|
|
104
|
+
let known: { server: string; spelled: string } | undefined;
|
|
105
|
+
for (const server of servers) {
|
|
106
|
+
const spelled = spelledInToolName(server);
|
|
107
|
+
const fits = rest.startsWith(`${spelled}__`) && rest.length > spelled.length + 2;
|
|
108
|
+
if (fits && (!known || spelled.length > known.spelled.length)) known = { server, spelled };
|
|
109
|
+
}
|
|
110
|
+
if (known) return { server: known.server, tool: rest.slice(known.spelled.length + 2) };
|
|
111
|
+
const split = rest.indexOf("__");
|
|
112
|
+
if (split <= 0 || split + 2 >= rest.length) return undefined;
|
|
113
|
+
return { server: rest.slice(0, split), tool: rest.slice(split + 2) };
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** `input[key]` when it is a string with something in it. */
|
|
117
|
+
function textOf(input: Record<string, unknown> | undefined, key: string): string | undefined {
|
|
118
|
+
const value = input?.[key];
|
|
119
|
+
return typeof value === "string" && value ? value : undefined;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// @ref LLP 0002#a-tool-call-says-what-it-acts-on — Claude's own keys, read once here
|
|
123
|
+
/**
|
|
124
|
+
* A Claude tool's kind and touched files, a `Bash` call's command and a
|
|
125
|
+
* `WebFetch` call's address; for an MCP tool, its server and tool among
|
|
126
|
+
* `servers`.
|
|
127
|
+
*/
|
|
128
|
+
export function claudeToolMeta(
|
|
129
|
+
toolName: string,
|
|
130
|
+
input?: Record<string, unknown>,
|
|
131
|
+
servers?: Iterable<string>,
|
|
132
|
+
): ToolMeta {
|
|
133
|
+
const mcp = claudeMcpTool(toolName, servers);
|
|
134
|
+
const command = toolName === "Bash" ? textOf(input, "command") : undefined;
|
|
135
|
+
const url = toolName === "WebFetch" ? textOf(input, "url") : undefined;
|
|
43
136
|
return {
|
|
44
137
|
kind: CLAUDE_TOOL_KINDS.get(toolName) ?? "other",
|
|
138
|
+
...(mcp && { mcp }),
|
|
139
|
+
...(command && { command }),
|
|
140
|
+
...(url && { url }),
|
|
45
141
|
...(input ? { locations: toolLocationsFromInput(input) } : {}),
|
|
46
142
|
};
|
|
47
143
|
}
|