@zvada/agent-server 0.3.22 → 0.3.24
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 +295 -0
- package/README.md +14 -5
- package/dist/accounts/chatgpt.js +144 -0
- package/dist/accounts/check.js +71 -0
- package/dist/accounts/claude.js +80 -0
- package/dist/accounts/endpoint.js +104 -0
- package/dist/accounts/http.js +58 -0
- package/dist/accounts/index.js +9 -0
- package/dist/client/client.js +686 -0
- package/dist/client/connection.js +338 -0
- package/dist/client/history.js +49 -0
- package/dist/client/index.js +6 -0
- package/dist/client/listeners.js +19 -0
- package/dist/client/logins.js +92 -0
- package/dist/client/rpc.js +105 -0
- package/dist/client/streams.js +432 -0
- package/dist/client/transports.js +118 -0
- package/dist/client/turns.js +122 -0
- package/dist/core/agents/acp/acp-agent.js +425 -0
- package/dist/core/agents/acp/adapter.js +231 -0
- package/dist/core/agents/acp/client.js +222 -0
- package/dist/core/agents/acp/known-agents.js +57 -0
- package/dist/core/agents/acp/mappings.js +164 -0
- package/dist/core/agents/acp/requests.js +79 -0
- package/dist/core/agents/attachments.js +14 -0
- package/dist/core/agents/base.js +64 -0
- package/dist/core/agents/claude-code/adapter.js +474 -0
- package/dist/core/agents/claude-code/approvals.js +245 -0
- package/dist/core/agents/claude-code/claude-agent.js +319 -0
- package/dist/core/agents/claude-code/credentials.js +142 -0
- package/dist/core/agents/claude-code/events.js +11 -0
- package/dist/core/agents/claude-code/login.js +217 -0
- package/dist/core/agents/claude-code/mcp.js +181 -0
- package/dist/core/agents/claude-code/models.js +74 -0
- package/dist/core/agents/claude-code/options.js +240 -0
- package/dist/core/agents/claude-code/outcome.js +204 -0
- package/dist/core/agents/claude-code/results.js +58 -0
- package/dist/core/agents/claude-code/sdk-pair.js +59 -0
- package/dist/core/agents/claude-code/session.js +593 -0
- package/dist/core/agents/claude-code/spawn.js +125 -0
- package/dist/core/agents/claude-code/spend.js +118 -0
- package/dist/core/agents/claude-code/tools.js +131 -0
- package/dist/core/agents/claude-code/turn-usage.js +121 -0
- package/dist/core/agents/claude-code/turn.js +199 -0
- package/dist/core/agents/codex-app-server/adapter.js +463 -0
- package/dist/core/agents/codex-app-server/client.js +338 -0
- package/dist/core/agents/codex-app-server/codex-agent.js +294 -0
- package/dist/core/agents/codex-app-server/commands.js +184 -0
- package/dist/core/agents/codex-app-server/credentials.js +92 -0
- package/dist/core/agents/codex-app-server/effort.js +13 -0
- package/dist/core/agents/codex-app-server/events.js +18 -0
- package/dist/core/agents/codex-app-server/homes.js +118 -0
- package/dist/core/agents/codex-app-server/input.js +52 -0
- package/dist/core/agents/codex-app-server/items.js +307 -0
- package/dist/core/agents/codex-app-server/login.js +183 -0
- package/dist/core/agents/codex-app-server/mcp.js +323 -0
- package/dist/core/agents/codex-app-server/models.js +160 -0
- package/dist/core/agents/codex-app-server/options.js +155 -0
- package/dist/core/agents/codex-app-server/plan-approval.js +124 -0
- package/dist/core/agents/codex-app-server/protocol.js +38 -0
- package/dist/core/agents/codex-app-server/requests.js +250 -0
- package/dist/core/agents/codex-app-server/session.js +278 -0
- package/dist/core/agents/codex-app-server/subagents.js +260 -0
- package/dist/core/agents/codex-app-server/thread-settings.js +214 -0
- package/dist/core/agents/codex-app-server/turn-items.js +69 -0
- package/dist/core/agents/codex-app-server/turn.js +448 -0
- package/dist/core/agents/codex-app-server/user-login.js +78 -0
- package/dist/core/agents/config-fingerprint.js +10 -0
- package/dist/core/agents/credentials.js +71 -0
- package/dist/core/agents/diffs.js +27 -0
- package/dist/core/agents/elicitation.js +42 -0
- package/dist/core/agents/inherit-config.js +5 -0
- package/dist/core/agents/login.js +56 -0
- package/dist/core/agents/mcp-config.js +109 -0
- package/dist/core/agents/plan.js +6 -0
- package/dist/core/agents/process-pool.js +307 -0
- package/dist/core/agents/registry.js +49 -0
- package/dist/core/agents/scripted/adapter.js +86 -0
- package/dist/core/agents/scripted/mcp-call.js +74 -0
- package/dist/core/agents/scripted/scripted-agent.js +330 -0
- package/dist/core/agents/scripted/scripts.js +154 -0
- package/dist/core/agents/scripted/steps.js +87 -0
- package/dist/core/agents/session-agent.js +209 -0
- package/dist/core/agents/stderr-tail.js +71 -0
- package/dist/core/agents/thinking-levels.js +9 -0
- package/dist/core/agents/types.js +1 -0
- package/dist/core/diagnostics.js +23 -0
- package/dist/core/index.js +31 -0
- package/dist/core/presets.js +114 -0
- package/dist/core/processes/identity.js +91 -0
- package/dist/core/processes/lifeline.js +210 -0
- package/dist/core/processes/native-processes.js +299 -0
- package/dist/core/processes/process-table.js +163 -0
- package/dist/core/provision/cache-trust.js +121 -0
- package/dist/core/provision/cache-usage.js +61 -0
- package/dist/core/provision/checksums.js +22 -0
- package/dist/core/provision/extract.js +28 -0
- package/dist/core/provision/index.js +4 -0
- package/dist/core/provision/install-build.js +99 -0
- package/dist/core/provision/npm.js +316 -0
- package/dist/core/provision/pins.js +35 -0
- package/dist/core/provision/platform.js +65 -0
- package/dist/core/provision/provisioner.js +372 -0
- package/dist/core/provision/releases.js +68 -0
- package/dist/core/provision/tools.js +279 -0
- package/dist/core/proxy/anthropic-proxy.js +163 -0
- package/dist/core/proxy/api-key-store.js +30 -0
- package/dist/core/proxy/index.js +3 -0
- package/dist/core/proxy/loopback.js +86 -0
- package/dist/core/runtime/agent-runtime.js +434 -0
- package/dist/core/runtime/event-processor.js +379 -0
- package/dist/core/runtime/event-sink.js +65 -0
- package/dist/core/runtime/harness-clis.js +83 -0
- package/dist/core/runtime/harness-logins.js +313 -0
- package/dist/core/runtime/login-reads.js +71 -0
- package/dist/core/runtime/model-lists.js +160 -0
- package/dist/core/runtime/permission-policy.js +84 -0
- package/dist/core/runtime/request-broker.js +139 -0
- package/dist/core/runtime/steering.js +79 -0
- package/dist/core/runtime/turn-outcome.js +16 -0
- package/dist/core/runtime/turn.js +329 -0
- package/dist/core/utils/bounded.js +13 -0
- package/dist/core/utils/deadline.js +57 -0
- package/dist/core/utils/errors.js +176 -0
- package/dist/core/utils/is-record.js +4 -0
- package/dist/core/utils/loopback.js +11 -0
- package/dist/index.js +4 -0
- package/dist/operator/index.js +10 -0
- package/dist/operator/mcp.js +139 -0
- package/dist/operator/operator.js +499 -0
- package/dist/operator/render.js +408 -0
- package/dist/operator/view.js +35 -0
- package/dist/protocol/account.js +96 -0
- package/dist/protocol/async-queue.js +65 -0
- package/dist/protocol/auth.js +169 -0
- package/dist/protocol/canonical.js +18 -0
- package/dist/protocol/clis.js +63 -0
- package/dist/protocol/config.js +137 -0
- package/dist/protocol/conversation.js +231 -0
- package/dist/protocol/credentials.js +141 -0
- package/dist/protocol/errors.js +181 -0
- package/dist/protocol/execution.js +28 -0
- package/dist/protocol/factories.js +231 -0
- package/dist/protocol/guards.js +71 -0
- package/dist/protocol/harness.js +93 -0
- package/dist/protocol/history.js +201 -0
- package/dist/protocol/ids.js +37 -0
- package/dist/protocol/index.js +36 -0
- package/dist/protocol/lifecycle.js +562 -0
- package/dist/protocol/meta-keys.js +17 -0
- package/dist/protocol/meta.js +13 -0
- package/dist/protocol/model-catalog.js +223 -0
- package/dist/protocol/models.js +84 -0
- package/dist/protocol/part-input.js +94 -0
- package/dist/protocol/parts.js +166 -0
- package/dist/protocol/plan.js +31 -0
- package/dist/protocol/reduce.js +484 -0
- package/dist/protocol/selectors.js +136 -0
- package/dist/protocol/seq-cursor.js +37 -0
- package/dist/protocol/stop-reasons.js +41 -0
- package/dist/protocol/thinking.js +31 -0
- package/dist/protocol/time.js +21 -0
- package/dist/protocol/tokens.js +57 -0
- package/dist/protocol/tool-closure.js +70 -0
- package/dist/protocol/tool-state.js +153 -0
- package/dist/protocol/verify.js +467 -0
- package/dist/protocol/vocabulary.js +27 -0
- package/dist/protocol/wire-api.js +94 -0
- package/dist/protocol/wire.js +669 -0
- package/dist/server/acp/binding.js +251 -0
- package/dist/server/acp/session-records.js +53 -0
- package/dist/server/acp/translate.js +401 -0
- package/dist/server/agent-server.js +338 -0
- package/dist/server/bin.js +492 -0
- package/dist/server/feedback.js +216 -0
- package/dist/server/history.js +123 -0
- package/dist/server/index.js +8 -0
- package/dist/server/install.js +171 -0
- package/dist/server/open-parts.js +87 -0
- package/dist/server/routes.js +291 -0
- package/dist/server/secrets.js +19 -0
- package/dist/server/session-gate.js +54 -0
- package/dist/server/session-log.js +66 -0
- package/dist/server/session-store.js +243 -0
- package/dist/server/subscriptions.js +189 -0
- package/dist/server/transports.js +214 -0
- package/dist/server/wire-error.js +26 -0
- package/dist/sync/event-log.js +89 -0
- package/dist/sync/fit.js +202 -0
- package/dist/sync/history-reader.js +33 -0
- package/dist/sync/history-sqlite.js +281 -0
- package/dist/sync/history.js +163 -0
- package/dist/sync/index.js +5 -0
- package/dist/sync/journal-sqlite.js +316 -0
- package/dist/sync/journal.js +10 -0
- package/dist/sync/live-tails.js +85 -0
- package/dist/sync/node-sqlite.js +27 -0
- package/dist/sync/projection.js +32 -0
- package/dist/sync/recovery.js +117 -0
- package/dist/sync/retry.js +45 -0
- package/dist/sync/schema.js +73 -0
- package/dist/sync/sql.js +94 -0
- package/dist/sync/sqlite-file.js +31 -0
- package/dist/sync/sqlite.js +18 -0
- package/dist/sync/worker.js +212 -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 +19 -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 +29 -10
- 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 +58 -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 +25 -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 +16 -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 +71 -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 +201 -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/bin.d.ts +3 -0
- package/dist/types/server/bin.d.ts.map +1 -0
- 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 +2 -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/node-sqlite.d.ts +4 -1
- package/dist/types/sync/node-sqlite.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 +304 -28
- package/docs/deploy.md +64 -8
- package/docs/durable-sessions.md +35 -5
- package/docs/harnesses.md +398 -25
- package/package.json +48 -18
- 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 +11 -6
- package/src/core/agents/claude-code/claude-agent.ts +89 -32
- 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 +30 -9
- 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 +179 -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 +54 -3
- package/src/core/agents/codex-app-server/items.ts +219 -31
- package/src/core/agents/codex-app-server/login.ts +44 -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 +17 -8
- package/src/core/agents/codex-app-server/session.ts +160 -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 +458 -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 +100 -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 +108 -19
- 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 +20 -1
- 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/node-sqlite.ts +19 -3
- 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
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import { POSIX } from "./process-table.js";
|
|
3
|
+
/**
|
|
4
|
+
* The lifeline: a `/bin/sh` that holds a pipe from this process and, when the
|
|
5
|
+
* pipe closes (this process died, SIGKILL included), stops every process group
|
|
6
|
+
* registered on it. Kept as a string, not a file: nothing to ship, and it runs
|
|
7
|
+
* from a `bun build --compile` binary.
|
|
8
|
+
*
|
|
9
|
+
* Its input, one line each: `+g<pgid>` registers a group, `-g<pgid>` forgets
|
|
10
|
+
* it. It ignores the signals a terminal or Ctrl-C sends a whole group: only
|
|
11
|
+
* the pipe decides. At EOF it walks the table (`ps`, or `/proc` on Linux) for
|
|
12
|
+
* what runs under each group, sends SIGTERM to the groups, to every tool
|
|
13
|
+
* session it finds (both CLIs start their commands with `setsid`) and to
|
|
14
|
+
* anything else under them alone, waits up to its grace, walks again and
|
|
15
|
+
* sends SIGKILL to what is left. `$1` is the grace in tenths of a second.
|
|
16
|
+
* The walk is `treeTargets`'s, in awk: a root that leads its group is reached
|
|
17
|
+
* as it, a descendant leading its own as that group, any other descendant in
|
|
18
|
+
* a group the tree does not lead alone.
|
|
19
|
+
*/
|
|
20
|
+
const LIFELINE_SCRIPT = String.raw `
|
|
21
|
+
trap '' HUP INT TERM QUIT
|
|
22
|
+
grace=${"$"}{1:-30}
|
|
23
|
+
groups=" "
|
|
24
|
+
while IFS= read -r line; do
|
|
25
|
+
case "$line" in
|
|
26
|
+
+g[0-9]*) groups="$groups${"$"}{line#+g} " ;;
|
|
27
|
+
-g[0-9]*) groups=$(printf '%s' "$groups" | sed "s/ ${"$"}{line#-g} / /") ;;
|
|
28
|
+
esac
|
|
29
|
+
done
|
|
30
|
+
[ "$groups" = " " ] && exit 0
|
|
31
|
+
table() {
|
|
32
|
+
if [ -r /proc/self/stat ]; then
|
|
33
|
+
cat /proc/[0-9]*/stat 2>/dev/null | sed 's/^\([0-9]*\) (.*) \(.\) \([0-9]*\) \([0-9]*\) .*/\1 \3 \4 \2/'
|
|
34
|
+
else
|
|
35
|
+
/bin/ps -axo pid=,ppid=,pgid=,stat= 2>/dev/null
|
|
36
|
+
fi
|
|
37
|
+
}
|
|
38
|
+
targets() {
|
|
39
|
+
table | awk -v roots="$*" -v self="$$" '
|
|
40
|
+
{ pp[$1] = $2; pg[$1] = $3; z[$1] = ($4 ~ /^Z/) }
|
|
41
|
+
END {
|
|
42
|
+
n = split(roots, r, " ")
|
|
43
|
+
for (i = 1; i <= n; i++) { root[r[i]] = 1; keep[r[i]] = 1 }
|
|
44
|
+
for (p in pg) if ((pg[p] in root) && (!(pg[p] in pg) || pg[pg[p]] == pg[p])) keep[p] = 1
|
|
45
|
+
do { changed = 0
|
|
46
|
+
for (p in pp) if (!(p in keep) && (pp[p] in keep)) { keep[p] = 1; changed = 1 }
|
|
47
|
+
} while (changed)
|
|
48
|
+
for (g in root) {
|
|
49
|
+
if ((g in pg) && pg[g] != g) { if (!z[g]) out[g] = 1; continue }
|
|
50
|
+
for (p in pg) if (pg[p] == g && !z[p]) { out["-" g] = 1; break }
|
|
51
|
+
}
|
|
52
|
+
for (p in keep) {
|
|
53
|
+
if ((p in root) || !(p in pg) || z[p]) continue
|
|
54
|
+
if ((pg[p] in root) && (!(pg[p] in pg) || pg[pg[p]] == pg[p])) continue
|
|
55
|
+
if (pg[p] == p) out["-" p] = 1
|
|
56
|
+
else if (!((pg[p] in keep) && pg[pg[p]] == pg[p])) out[p] = 1
|
|
57
|
+
}
|
|
58
|
+
for (t in out) { a = t; sub(/^-/, "", a); if (a + 0 > 1 && a != self) print t }
|
|
59
|
+
}' | tr '\n' ' '
|
|
60
|
+
}
|
|
61
|
+
alive() {
|
|
62
|
+
table | awk -v targets="$*" '
|
|
63
|
+
!($4 ~ /^Z/) { live[$1] = 1; group[$3] = 1 }
|
|
64
|
+
END {
|
|
65
|
+
n = split(targets, t, " ")
|
|
66
|
+
for (i = 1; i <= n; i++) { x = t[i]; g = x; sub(/^-/, "", g); if ((x ~ /^-/) ? (g in group) : (x in live)) print x }
|
|
67
|
+
}' | tr '\n' ' '
|
|
68
|
+
}
|
|
69
|
+
first=$(targets $groups)
|
|
70
|
+
for t in $first; do kill -TERM "$t" 2>/dev/null; done
|
|
71
|
+
ticks=$grace
|
|
72
|
+
while [ "$ticks" -gt 0 ] && [ -n "$(alive $first)" ]; do sleep 0.1; ticks=$((ticks - 1)); done
|
|
73
|
+
roots="$groups"
|
|
74
|
+
for t in $first; do roots="$roots ${"$"}{t#-}"; done
|
|
75
|
+
for t in $(alive $first) $(targets $roots); do kill -KILL "$t" 2>/dev/null; done
|
|
76
|
+
`;
|
|
77
|
+
/** How the lifeline names itself (`$0`), so a process listing shows whose it is. */
|
|
78
|
+
const LIFELINE_NAME = "agent-server-lifeline";
|
|
79
|
+
/** Respawns within this long of the last one count as a lifeline that cannot run. */
|
|
80
|
+
const RESPAWN_WINDOW_MS = 1_000;
|
|
81
|
+
/**
|
|
82
|
+
* Start the shell in a session of its own, so no signal aimed at this
|
|
83
|
+
* process's group or terminal reaches it; its pipe is all it reads. Neither it
|
|
84
|
+
* nor its pipe keeps this process alive.
|
|
85
|
+
*/
|
|
86
|
+
function startShell(ticks, exited, failed) {
|
|
87
|
+
const args = ["-c", LIFELINE_SCRIPT, LIFELINE_NAME, ticks];
|
|
88
|
+
const bun = globalThis.Bun;
|
|
89
|
+
if (bun) {
|
|
90
|
+
// @ref LLP 0007#the-lifeline — registered before the CLI gets input
|
|
91
|
+
// Bun's `node:child_process` writes a child's stdin only once its event
|
|
92
|
+
// loop turns: a SIGKILL before that lost the registration, and the CLI
|
|
93
|
+
// outlived its owner. A `Bun.spawn` sink's `flush` writes it now.
|
|
94
|
+
const child = bun.spawn(["/bin/sh", ...args], {
|
|
95
|
+
stdin: "pipe",
|
|
96
|
+
stdout: "ignore",
|
|
97
|
+
stderr: "ignore",
|
|
98
|
+
detached: true,
|
|
99
|
+
onExit: exited,
|
|
100
|
+
});
|
|
101
|
+
child.unref();
|
|
102
|
+
child.stdin.unref();
|
|
103
|
+
return {
|
|
104
|
+
pid: child.pid,
|
|
105
|
+
write(lines) {
|
|
106
|
+
child.stdin.write(lines);
|
|
107
|
+
// Gone already: its exit starts the next one.
|
|
108
|
+
void Promise.resolve(child.stdin.flush()).catch(() => { });
|
|
109
|
+
},
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
// Node writes a pipe at once when it can (libuv's `uv_try_write`).
|
|
113
|
+
const child = spawn("/bin/sh", args, { detached: true, stdio: ["pipe", "ignore", "ignore"] });
|
|
114
|
+
child.unref();
|
|
115
|
+
child.stdin?.unref?.();
|
|
116
|
+
child.stdin?.on("error", () => { });
|
|
117
|
+
child.once("error", failed);
|
|
118
|
+
child.once("exit", exited);
|
|
119
|
+
return {
|
|
120
|
+
pid: child.pid,
|
|
121
|
+
write: (lines) => void child.stdin?.write(lines),
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* The lifeline of this OS process: one per process, shared by every runtime
|
|
126
|
+
* in it, started at the first registration. If it dies while this process
|
|
127
|
+
* lives, it is started again with the groups still registered.
|
|
128
|
+
*/
|
|
129
|
+
class Lifeline {
|
|
130
|
+
graceMs;
|
|
131
|
+
child;
|
|
132
|
+
groups = new Set();
|
|
133
|
+
lastSpawn = 0;
|
|
134
|
+
/** No `/bin/sh` here, or it keeps dying: the restart sweep is all that is left. */
|
|
135
|
+
broken = false;
|
|
136
|
+
constructor(graceMs) {
|
|
137
|
+
this.graceMs = graceMs;
|
|
138
|
+
}
|
|
139
|
+
/** The lifeline's pid while one runs (a drill loses it on purpose). */
|
|
140
|
+
get pid() {
|
|
141
|
+
return this.child?.pid;
|
|
142
|
+
}
|
|
143
|
+
register(pgid) {
|
|
144
|
+
if (this.groups.has(pgid))
|
|
145
|
+
return;
|
|
146
|
+
this.groups.add(pgid);
|
|
147
|
+
if (!this.ensure())
|
|
148
|
+
this.write(`+g${pgid}\n`);
|
|
149
|
+
}
|
|
150
|
+
forget(pgid) {
|
|
151
|
+
if (this.groups.delete(pgid))
|
|
152
|
+
this.write(`-g${pgid}\n`);
|
|
153
|
+
}
|
|
154
|
+
/** Start one when none runs; true when it was started now, with every group sent. */
|
|
155
|
+
ensure() {
|
|
156
|
+
if (this.child || this.broken || !POSIX)
|
|
157
|
+
return false;
|
|
158
|
+
const now = Date.now();
|
|
159
|
+
if (now - this.lastSpawn < RESPAWN_WINDOW_MS) {
|
|
160
|
+
this.broken = true;
|
|
161
|
+
return false;
|
|
162
|
+
}
|
|
163
|
+
this.lastSpawn = now;
|
|
164
|
+
const ticks = String(Math.max(1, Math.ceil(this.graceMs / 100)));
|
|
165
|
+
let child;
|
|
166
|
+
const gone = () => {
|
|
167
|
+
if (this.child !== child)
|
|
168
|
+
return false;
|
|
169
|
+
this.child = undefined;
|
|
170
|
+
return true;
|
|
171
|
+
};
|
|
172
|
+
try {
|
|
173
|
+
child = startShell(ticks, () => {
|
|
174
|
+
if (gone() && this.groups.size)
|
|
175
|
+
this.ensure();
|
|
176
|
+
}, () => {
|
|
177
|
+
// No shell to run it: nothing to respawn.
|
|
178
|
+
this.broken = true;
|
|
179
|
+
gone();
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
catch {
|
|
183
|
+
this.broken = true;
|
|
184
|
+
return false;
|
|
185
|
+
}
|
|
186
|
+
this.child = child;
|
|
187
|
+
this.write([...this.groups].map((pgid) => `+g${pgid}\n`).join(""));
|
|
188
|
+
return true;
|
|
189
|
+
}
|
|
190
|
+
write(lines) {
|
|
191
|
+
if (!lines)
|
|
192
|
+
return;
|
|
193
|
+
try {
|
|
194
|
+
this.child?.write(lines);
|
|
195
|
+
}
|
|
196
|
+
catch {
|
|
197
|
+
// It is gone; its exit starts the next one.
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
let shared;
|
|
202
|
+
/** This process's lifeline, made on first use with the grace of the runtime that needed it first. */
|
|
203
|
+
export function lifeline(graceMs) {
|
|
204
|
+
shared ??= new Lifeline(graceMs);
|
|
205
|
+
return shared;
|
|
206
|
+
}
|
|
207
|
+
/** The pid of this process's lifeline while one runs. */
|
|
208
|
+
export function lifelinePid() {
|
|
209
|
+
return shared?.pid;
|
|
210
|
+
}
|
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
import { spawn as nodeSpawn } from "node:child_process";
|
|
2
|
+
import { StderrTail } from "../agents/stderr-tail.js";
|
|
3
|
+
import { onAbort } from "../utils/deadline.js";
|
|
4
|
+
import { bootId, startToken, verifyRecord } from "./identity.js";
|
|
5
|
+
import { lifeline } from "./lifeline.js";
|
|
6
|
+
import { POSIX, aliveTargets, readProcessTable, signalTargets, treeTargets, } from "./process-table.js";
|
|
7
|
+
/** How long a stopped process has between SIGTERM and SIGKILL: Claude's own shutdown took up to 2.7 s. */
|
|
8
|
+
export const DEFAULT_GRACE_MS = 3_000;
|
|
9
|
+
/** The stderr kept for an exit error, as the Claude SDK keeps it. */
|
|
10
|
+
const STDERR_TAIL_MAX = 2048;
|
|
11
|
+
/** How long an exit waits for its stdout's and stderr's last words, as the Claude SDK's own spawner waits for stderr. */
|
|
12
|
+
const OUTPUT_CLOSE_WAIT_MS = 200;
|
|
13
|
+
const POLL_MS = 50;
|
|
14
|
+
/** How long a SIGKILLed tree may take to be gone before it counts as not stopped. */
|
|
15
|
+
const KILL_WAIT_MS = 1_000;
|
|
16
|
+
/** One native process the engine started: the child itself, its stderr tail, its record and its stop. */
|
|
17
|
+
export class NativeProcess {
|
|
18
|
+
child;
|
|
19
|
+
record;
|
|
20
|
+
tail;
|
|
21
|
+
stopper;
|
|
22
|
+
/**
|
|
23
|
+
* Settles once it exited and its stdout and stderr closed, at most 200 ms
|
|
24
|
+
* after the exit: what it wrote just before its exit is read first (Codex's
|
|
25
|
+
* last response), and its stderr's last words are in the tail.
|
|
26
|
+
*/
|
|
27
|
+
exited;
|
|
28
|
+
/** The stop under way, once one began: whether everything under it is gone. */
|
|
29
|
+
stopping;
|
|
30
|
+
constructor(child, record, tail, stopper) {
|
|
31
|
+
this.child = child;
|
|
32
|
+
this.record = record;
|
|
33
|
+
this.tail = tail;
|
|
34
|
+
this.stopper = stopper;
|
|
35
|
+
this.exited = new Promise((resolve) => {
|
|
36
|
+
let exit;
|
|
37
|
+
let open = (child.stdout ? 1 : 0) + (child.stderr ? 1 : 0);
|
|
38
|
+
let timer;
|
|
39
|
+
const settle = () => {
|
|
40
|
+
if (!exit)
|
|
41
|
+
return;
|
|
42
|
+
clearTimeout(timer);
|
|
43
|
+
resolve(exit);
|
|
44
|
+
};
|
|
45
|
+
const closed = () => {
|
|
46
|
+
if (--open === 0)
|
|
47
|
+
settle();
|
|
48
|
+
};
|
|
49
|
+
child.stderr?.on("data", (chunk) => tail.write(chunk));
|
|
50
|
+
child.stderr?.once("close", () => {
|
|
51
|
+
tail.end();
|
|
52
|
+
closed();
|
|
53
|
+
});
|
|
54
|
+
child.stdout?.once("close", closed);
|
|
55
|
+
child.once("exit", (code, signal) => {
|
|
56
|
+
exit = { code, signal };
|
|
57
|
+
if (open === 0)
|
|
58
|
+
settle();
|
|
59
|
+
else
|
|
60
|
+
timer = setTimeout(settle, OUTPUT_CLOSE_WAIT_MS);
|
|
61
|
+
});
|
|
62
|
+
// A spawn that failed never exits.
|
|
63
|
+
child.once("error", () => {
|
|
64
|
+
if (child.pid === undefined)
|
|
65
|
+
resolve({ code: null, signal: null });
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
get pid() {
|
|
70
|
+
return this.child.pid;
|
|
71
|
+
}
|
|
72
|
+
/** The end of its stderr (2 KB): complete once `exited` settles. */
|
|
73
|
+
get stderrTail() {
|
|
74
|
+
return this.tail.text;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Stop it and everything under it: SIGTERM to its group and its tool
|
|
78
|
+
* sessions, SIGKILL to what is left after the grace. Whether all of it is
|
|
79
|
+
* gone. Every call shares one stop.
|
|
80
|
+
*/
|
|
81
|
+
stop(options = {}) {
|
|
82
|
+
this.stopping ??= this.stopper(this, options.graceMs).catch(() => false);
|
|
83
|
+
return this.stopping;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
/** A stop holds its process open until it is done (at most the grace and a second). */
|
|
87
|
+
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
88
|
+
// @ref LLP 0007#ownership — the process that spawns a CLI owns it, and takes it along
|
|
89
|
+
/**
|
|
90
|
+
* The native processes a runtime starts, and how it stops them. Each CLI
|
|
91
|
+
* starts in a session and process group of its own, registered with this OS
|
|
92
|
+
* process's lifeline before it is sent any input, and its identity is read at
|
|
93
|
+
* once, so a crash cannot leave a CLI with work and no record. Stopping one
|
|
94
|
+
* reaches its whole tree: its group, the tool sessions under it, and anything
|
|
95
|
+
* else under it alone, SIGTERM first (both CLIs stop their own tools on it),
|
|
96
|
+
* SIGKILL after the grace.
|
|
97
|
+
*/
|
|
98
|
+
export class NativeProcesses {
|
|
99
|
+
live = new Set();
|
|
100
|
+
observers = new Set();
|
|
101
|
+
graceMs;
|
|
102
|
+
lifeline;
|
|
103
|
+
spawnChild;
|
|
104
|
+
constructor(options = {}) {
|
|
105
|
+
this.graceMs = options.graceMs ?? DEFAULT_GRACE_MS;
|
|
106
|
+
this.lifeline = options.lifeline !== false;
|
|
107
|
+
this.spawnChild = options.spawn ?? nodeSpawn;
|
|
108
|
+
if (options.onRecord)
|
|
109
|
+
this.observers.add(options.onRecord);
|
|
110
|
+
}
|
|
111
|
+
/** Watch records start and stop (what a journal writes); returns the unsubscribe. */
|
|
112
|
+
observe(observer) {
|
|
113
|
+
this.observers.add(observer);
|
|
114
|
+
return () => this.observers.delete(observer);
|
|
115
|
+
}
|
|
116
|
+
/** Start a native CLI the engine owns. Throws when its record cannot be kept. */
|
|
117
|
+
spawn(spec) {
|
|
118
|
+
const child = this.spawnChild(spec.command, spec.args, {
|
|
119
|
+
cwd: spec.cwd,
|
|
120
|
+
env: spec.env ?? process.env,
|
|
121
|
+
stdio: ["pipe", "pipe", "pipe"],
|
|
122
|
+
// setsid: a new session and group, which nothing aimed at this
|
|
123
|
+
// process's group or terminal reaches unannounced.
|
|
124
|
+
detached: POSIX,
|
|
125
|
+
windowsHide: true,
|
|
126
|
+
});
|
|
127
|
+
const pid = child.pid;
|
|
128
|
+
const record = pid !== undefined && spec.sessionId !== undefined
|
|
129
|
+
? {
|
|
130
|
+
sessionId: spec.sessionId,
|
|
131
|
+
harness: spec.harness,
|
|
132
|
+
pid,
|
|
133
|
+
boot: bootId(),
|
|
134
|
+
started: startToken(pid),
|
|
135
|
+
executable: spec.command,
|
|
136
|
+
}
|
|
137
|
+
: undefined;
|
|
138
|
+
const native = new NativeProcess(child, record, new StderrTail(STDERR_TAIL_MAX), (stopped, graceMs = this.graceMs) => this.stopNative(stopped, graceMs));
|
|
139
|
+
// A spawn that failed says why in its `error`.
|
|
140
|
+
if (pid === undefined)
|
|
141
|
+
return native;
|
|
142
|
+
if (POSIX && this.lifeline)
|
|
143
|
+
lifeline(this.graceMs).register(pid);
|
|
144
|
+
this.live.add(native);
|
|
145
|
+
if (record) {
|
|
146
|
+
try {
|
|
147
|
+
this.report({ type: "started", record }, true);
|
|
148
|
+
}
|
|
149
|
+
catch (error) {
|
|
150
|
+
void native.stop();
|
|
151
|
+
throw error;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
// One that exits by itself is stopped all the same: what it left in its
|
|
155
|
+
// group is collected, once its output has had its last words, and its
|
|
156
|
+
// record is reported gone.
|
|
157
|
+
void native.exited.then(() => native.stop());
|
|
158
|
+
onAbort(spec.signal, () => void native.stop());
|
|
159
|
+
return native;
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* A session's process a host started with its own spawner: recorded with no
|
|
163
|
+
* pid, so a restart leaves its session to the host (`not_owned`). Call what
|
|
164
|
+
* this returns once it exited.
|
|
165
|
+
*/
|
|
166
|
+
hosted(spec) {
|
|
167
|
+
const record = { ...spec, pid: null, boot: null, started: null };
|
|
168
|
+
this.report({ type: "started", record }, true);
|
|
169
|
+
let reported = false;
|
|
170
|
+
return () => {
|
|
171
|
+
if (reported)
|
|
172
|
+
return;
|
|
173
|
+
reported = true;
|
|
174
|
+
this.report({ type: "stopped", record }, false);
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
/** Stop every process this runtime started and has not seen gone. */
|
|
178
|
+
async stopAll() {
|
|
179
|
+
await Promise.all([...this.live].map((native) => native.stop()));
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* The restart sweep: for each record a previous owner left, verify that it
|
|
183
|
+
* still names a running process (boot, start time, executable), and stop it
|
|
184
|
+
* when it does. A record that names another process leaves it untouched.
|
|
185
|
+
*/
|
|
186
|
+
async stopOrphans(records) {
|
|
187
|
+
const outcomes = new Map();
|
|
188
|
+
await Promise.all(records.map(async (record) => {
|
|
189
|
+
if (record.pid === null) {
|
|
190
|
+
outcomes.set(record, "not_owned");
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
const identity = verifyRecord(record);
|
|
194
|
+
if (identity !== "ours") {
|
|
195
|
+
outcomes.set(record, identity);
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
const stopped = await this.stopTree([record.pid], this.graceMs);
|
|
199
|
+
outcomes.set(record, stopped === "refused"
|
|
200
|
+
? "not_verified"
|
|
201
|
+
: stopped === "stopped" && verifyRecord(record) === "gone"
|
|
202
|
+
? "stopped"
|
|
203
|
+
: "not_stopped");
|
|
204
|
+
}));
|
|
205
|
+
return outcomes;
|
|
206
|
+
}
|
|
207
|
+
async stopNative(native, graceMs) {
|
|
208
|
+
const pid = native.pid;
|
|
209
|
+
const stopped = POSIX && pid !== undefined
|
|
210
|
+
? await this.stopTree([pid], graceMs, native)
|
|
211
|
+
: await stopChild(native, graceMs);
|
|
212
|
+
if (stopped !== "stopped")
|
|
213
|
+
return false;
|
|
214
|
+
if (pid !== undefined && this.lifeline)
|
|
215
|
+
lifeline(this.graceMs).forget(pid);
|
|
216
|
+
this.live.delete(native);
|
|
217
|
+
if (native.record)
|
|
218
|
+
this.report({ type: "stopped", record: native.record }, false);
|
|
219
|
+
return true;
|
|
220
|
+
}
|
|
221
|
+
/** SIGTERM the tree under `roots`, SIGKILL what is left after the grace, walking it again. */
|
|
222
|
+
async stopTree(roots, graceMs, native) {
|
|
223
|
+
const table = readProcessTable();
|
|
224
|
+
// No table to walk: the process alone, as before.
|
|
225
|
+
if (!table)
|
|
226
|
+
return native ? stopChild(native, graceMs) : "refused";
|
|
227
|
+
const first = treeTargets(table, roots);
|
|
228
|
+
if (!first.length)
|
|
229
|
+
return "stopped";
|
|
230
|
+
const refused = signalTargets(first, "SIGTERM");
|
|
231
|
+
if (refused.length === first.length)
|
|
232
|
+
return "refused";
|
|
233
|
+
const deadline = Date.now() + graceMs;
|
|
234
|
+
let last = table;
|
|
235
|
+
do {
|
|
236
|
+
await sleep(POLL_MS);
|
|
237
|
+
last = readProcessTable() ?? last;
|
|
238
|
+
} while (aliveTargets(last, first).length && Date.now() < deadline);
|
|
239
|
+
// What its tool sessions started since, and what never stopped.
|
|
240
|
+
const rest = new Set([
|
|
241
|
+
...aliveTargets(last, first),
|
|
242
|
+
...treeTargets(last, [...roots, ...first.map(Math.abs)]),
|
|
243
|
+
]);
|
|
244
|
+
if (!rest.size)
|
|
245
|
+
return "stopped";
|
|
246
|
+
signalTargets([...rest], "SIGKILL");
|
|
247
|
+
for (const end = Date.now() + KILL_WAIT_MS; Date.now() < end;) {
|
|
248
|
+
await sleep(POLL_MS);
|
|
249
|
+
const now = readProcessTable();
|
|
250
|
+
if (now && !aliveTargets(now, [...rest]).length)
|
|
251
|
+
return "stopped";
|
|
252
|
+
}
|
|
253
|
+
return "notStopped";
|
|
254
|
+
}
|
|
255
|
+
report(event, strict) {
|
|
256
|
+
let failure;
|
|
257
|
+
for (const observer of [...this.observers]) {
|
|
258
|
+
try {
|
|
259
|
+
observer(event);
|
|
260
|
+
}
|
|
261
|
+
catch (error) {
|
|
262
|
+
failure ??= { error };
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
// A record that could not be kept fails its spawn; a gone one is tried again at the next start.
|
|
266
|
+
if (strict && failure)
|
|
267
|
+
throw failure.error;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* Where there are no process groups (Windows), no table or no pid (a spawn
|
|
272
|
+
* that failed, a stand-in): SIGTERM the child, SIGKILL it after the grace.
|
|
273
|
+
*/
|
|
274
|
+
async function stopChild(native, graceMs) {
|
|
275
|
+
const { child } = native;
|
|
276
|
+
if (child.exitCode !== null || child.signalCode !== null)
|
|
277
|
+
return "stopped";
|
|
278
|
+
child.kill("SIGTERM");
|
|
279
|
+
if (await exitWithin(native, graceMs))
|
|
280
|
+
return "stopped";
|
|
281
|
+
child.kill("SIGKILL");
|
|
282
|
+
return (await exitWithin(native, KILL_WAIT_MS)) ? "stopped" : "notStopped";
|
|
283
|
+
}
|
|
284
|
+
/** Whether `native` exits within `ms`; the wait ends at its exit. */
|
|
285
|
+
function exitWithin(native, ms) {
|
|
286
|
+
return new Promise((resolve) => {
|
|
287
|
+
const timer = setTimeout(() => resolve(false), ms);
|
|
288
|
+
void native.exited.then(() => {
|
|
289
|
+
clearTimeout(timer);
|
|
290
|
+
resolve(true);
|
|
291
|
+
});
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
let shared;
|
|
295
|
+
/** The native processes of a harness made without a runtime's (a test, a custom registry). */
|
|
296
|
+
export function sharedNativeProcesses() {
|
|
297
|
+
shared ??= new NativeProcesses();
|
|
298
|
+
return shared;
|
|
299
|
+
}
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import { execFileSync } from "node:child_process";
|
|
2
|
+
import { readFileSync, readdirSync } from "node:fs";
|
|
3
|
+
/** Whether this platform has process groups, sessions and a table to walk. */
|
|
4
|
+
export const POSIX = process.platform !== "win32";
|
|
5
|
+
/** On Linux the table is read from `/proc` without spawning anything; busybox `ps` lacks `pgid`. */
|
|
6
|
+
const LINUX = process.platform === "linux";
|
|
7
|
+
/**
|
|
8
|
+
* The fields of `/proc/<pid>/stat` after its command, which may hold spaces
|
|
9
|
+
* and parentheses: state first, then ppid, pgrp, session, … starttime (the
|
|
10
|
+
* 22nd field overall) at index 19.
|
|
11
|
+
*/
|
|
12
|
+
export function statFields(pid) {
|
|
13
|
+
try {
|
|
14
|
+
const stat = readFileSync(`/proc/${pid}/stat`, "utf8");
|
|
15
|
+
return stat.slice(stat.lastIndexOf(")") + 2).split(" ");
|
|
16
|
+
}
|
|
17
|
+
catch {
|
|
18
|
+
return undefined; // gone, or not ours to read
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
function linuxTable() {
|
|
22
|
+
const rows = [];
|
|
23
|
+
for (const name of readdirSync("/proc")) {
|
|
24
|
+
const pid = Number(name);
|
|
25
|
+
if (!Number.isInteger(pid) || pid <= 0)
|
|
26
|
+
continue;
|
|
27
|
+
const fields = statFields(pid);
|
|
28
|
+
if (!fields)
|
|
29
|
+
continue;
|
|
30
|
+
rows.push({
|
|
31
|
+
pid,
|
|
32
|
+
ppid: Number(fields[1]),
|
|
33
|
+
pgid: Number(fields[2]),
|
|
34
|
+
zombie: fields[0] === "Z",
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
return rows;
|
|
38
|
+
}
|
|
39
|
+
function psTable() {
|
|
40
|
+
const out = execFileSync("/bin/ps", ["-axo", "pid=,ppid=,pgid=,stat="], {
|
|
41
|
+
encoding: "utf8",
|
|
42
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
43
|
+
});
|
|
44
|
+
const rows = [];
|
|
45
|
+
for (const line of out.split("\n")) {
|
|
46
|
+
const [pid, ppid, pgid, stat] = line.trim().split(/\s+/);
|
|
47
|
+
if (!pid || !stat)
|
|
48
|
+
continue;
|
|
49
|
+
rows.push({
|
|
50
|
+
pid: Number(pid),
|
|
51
|
+
ppid: Number(ppid),
|
|
52
|
+
pgid: Number(pgid),
|
|
53
|
+
zombie: stat.startsWith("Z"),
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
return rows;
|
|
57
|
+
}
|
|
58
|
+
/** Every process on the machine; undefined where none can be read (Windows, no `ps`). */
|
|
59
|
+
export function readProcessTable() {
|
|
60
|
+
if (!POSIX)
|
|
61
|
+
return undefined;
|
|
62
|
+
try {
|
|
63
|
+
return LINUX ? linuxTable() : psTable();
|
|
64
|
+
}
|
|
65
|
+
catch {
|
|
66
|
+
return undefined;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
/** A target a stop may send a signal to: nothing that names the caller, its group, init or everyone. */
|
|
70
|
+
function signallable(target, own) {
|
|
71
|
+
if (!Number.isInteger(target) || Math.abs(target) <= 1)
|
|
72
|
+
return false;
|
|
73
|
+
if (target === process.pid)
|
|
74
|
+
return false;
|
|
75
|
+
return !(own && target === -own.pgid);
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* What to signal to reach each root and everything under it.
|
|
79
|
+
*
|
|
80
|
+
* A root that leads its own group (a CLI spawned detached leads a new
|
|
81
|
+
* session) is reached as that group, which also holds what it started
|
|
82
|
+
* without a session of its own (Claude's MCP servers); a root that does not
|
|
83
|
+
* is reached alone. Below them, a process that leads its own group (a tool:
|
|
84
|
+
* both CLIs start each command with `setsid`) is reached as that group, one
|
|
85
|
+
* in a group the tree leads is covered by it, and one in anyone else's group
|
|
86
|
+
* is reached alone. Never by a group the tree does not lead: with the SDK's
|
|
87
|
+
* own spawner Claude's MCP servers share their owner's group, which can hold
|
|
88
|
+
* the owner's parent.
|
|
89
|
+
*/
|
|
90
|
+
export function treeTargets(table, roots) {
|
|
91
|
+
const rows = new Map(table.map((row) => [row.pid, row]));
|
|
92
|
+
const rootSet = new Set(roots);
|
|
93
|
+
const leads = (pid) => {
|
|
94
|
+
const row = rows.get(pid);
|
|
95
|
+
// A leader that already exited leaves its group to its members.
|
|
96
|
+
return !row || row.pgid === pid;
|
|
97
|
+
};
|
|
98
|
+
const tree = new Set(rootSet);
|
|
99
|
+
for (const row of table)
|
|
100
|
+
if (rootSet.has(row.pgid) && leads(row.pgid))
|
|
101
|
+
tree.add(row.pid);
|
|
102
|
+
for (let grew = true; grew;) {
|
|
103
|
+
grew = false;
|
|
104
|
+
for (const row of table)
|
|
105
|
+
if (!tree.has(row.pid) && tree.has(row.ppid)) {
|
|
106
|
+
tree.add(row.pid);
|
|
107
|
+
grew = true;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
const own = rows.get(process.pid);
|
|
111
|
+
const targets = new Set();
|
|
112
|
+
const live = (row) => Boolean(row && !row.zombie);
|
|
113
|
+
for (const root of rootSet) {
|
|
114
|
+
const row = rows.get(root);
|
|
115
|
+
if (leads(root)) {
|
|
116
|
+
if (table.some((member) => member.pgid === root && !member.zombie))
|
|
117
|
+
targets.add(-root);
|
|
118
|
+
}
|
|
119
|
+
else if (live(row))
|
|
120
|
+
targets.add(root);
|
|
121
|
+
}
|
|
122
|
+
for (const pid of tree) {
|
|
123
|
+
const row = rows.get(pid);
|
|
124
|
+
if (rootSet.has(pid) || !live(row))
|
|
125
|
+
continue;
|
|
126
|
+
if (rootSet.has(row.pgid) && leads(row.pgid))
|
|
127
|
+
continue;
|
|
128
|
+
if (row.pgid === pid)
|
|
129
|
+
targets.add(-pid);
|
|
130
|
+
else if (tree.has(row.pgid) && rows.get(row.pgid)?.pgid === row.pgid)
|
|
131
|
+
continue;
|
|
132
|
+
else
|
|
133
|
+
targets.add(pid);
|
|
134
|
+
}
|
|
135
|
+
return [...targets].filter((target) => signallable(target, own));
|
|
136
|
+
}
|
|
137
|
+
/** The targets something still answers for: a live process, or a group with a live member. */
|
|
138
|
+
export function aliveTargets(table, targets) {
|
|
139
|
+
const live = new Set();
|
|
140
|
+
const groups = new Set();
|
|
141
|
+
for (const row of table) {
|
|
142
|
+
if (row.zombie)
|
|
143
|
+
continue;
|
|
144
|
+
live.add(row.pid);
|
|
145
|
+
groups.add(row.pgid);
|
|
146
|
+
}
|
|
147
|
+
return targets.filter((target) => (target < 0 ? groups.has(-target) : live.has(target)));
|
|
148
|
+
}
|
|
149
|
+
/** Send `signal` to each target. Returns those the system would not let this process signal. */
|
|
150
|
+
export function signalTargets(targets, signal) {
|
|
151
|
+
const refused = [];
|
|
152
|
+
for (const target of targets) {
|
|
153
|
+
try {
|
|
154
|
+
process.kill(target, signal);
|
|
155
|
+
}
|
|
156
|
+
catch (error) {
|
|
157
|
+
// ESRCH: gone already. EPERM: someone else's, which a stop never touches.
|
|
158
|
+
if (error.code === "EPERM")
|
|
159
|
+
refused.push(target);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
return refused;
|
|
163
|
+
}
|