@zvada/agent-server 0.3.17 → 0.3.19
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/AGENTS.md +11 -26
- package/CHANGELOG.md +1060 -0
- package/README.md +50 -26
- package/dist/types/accounts/chatgpt.d.ts +34 -0
- package/dist/types/accounts/chatgpt.d.ts.map +1 -0
- package/dist/types/accounts/check.d.ts +24 -0
- package/dist/types/accounts/check.d.ts.map +1 -0
- package/dist/types/accounts/claude.d.ts +21 -0
- package/dist/types/accounts/claude.d.ts.map +1 -0
- package/dist/types/accounts/endpoint.d.ts +50 -0
- package/dist/types/accounts/endpoint.d.ts.map +1 -0
- package/dist/types/accounts/http.d.ts +38 -0
- package/dist/types/accounts/http.d.ts.map +1 -0
- package/dist/types/accounts/index.d.ts +6 -0
- package/dist/types/accounts/index.d.ts.map +1 -0
- package/dist/types/client/client.d.ts +272 -0
- package/dist/types/client/client.d.ts.map +1 -0
- package/dist/types/client/connection.d.ts +160 -0
- package/dist/types/client/connection.d.ts.map +1 -0
- package/dist/types/client/history.d.ts +8 -0
- package/dist/types/client/history.d.ts.map +1 -0
- package/dist/types/client/index.d.ts +5 -0
- package/dist/types/client/index.d.ts.map +1 -0
- package/dist/types/client/listeners.d.ts +8 -0
- package/dist/types/client/listeners.d.ts.map +1 -0
- package/dist/types/client/logins.d.ts +35 -0
- package/dist/types/client/logins.d.ts.map +1 -0
- package/dist/types/client/rpc.d.ts +46 -0
- package/dist/types/client/rpc.d.ts.map +1 -0
- package/dist/types/client/streams.d.ts +150 -0
- package/dist/types/client/streams.d.ts.map +1 -0
- package/dist/types/client/transports.d.ts +35 -0
- package/dist/types/client/transports.d.ts.map +1 -0
- package/dist/types/client/turns.d.ts +81 -0
- package/dist/types/client/turns.d.ts.map +1 -0
- package/dist/types/core/agents/acp/acp-agent.d.ts +68 -0
- package/dist/types/core/agents/acp/acp-agent.d.ts.map +1 -0
- package/dist/types/core/agents/acp/adapter.d.ts +39 -0
- package/dist/types/core/agents/acp/adapter.d.ts.map +1 -0
- package/dist/types/core/agents/acp/client.d.ts +90 -0
- package/dist/types/core/agents/acp/client.d.ts.map +1 -0
- package/dist/types/core/agents/acp/known-agents.d.ts +35 -0
- package/dist/types/core/agents/acp/known-agents.d.ts.map +1 -0
- package/dist/types/core/agents/acp/mappings.d.ts +46 -0
- package/dist/types/core/agents/acp/mappings.d.ts.map +1 -0
- package/dist/types/core/agents/acp/requests.d.ts +26 -0
- package/dist/types/core/agents/acp/requests.d.ts.map +1 -0
- package/dist/types/core/agents/attachments.d.ts +11 -0
- package/dist/types/core/agents/attachments.d.ts.map +1 -0
- package/dist/types/core/agents/base.d.ts +227 -0
- package/dist/types/core/agents/base.d.ts.map +1 -0
- package/dist/types/core/agents/claude-code/adapter.d.ts +58 -0
- package/dist/types/core/agents/claude-code/adapter.d.ts.map +1 -0
- package/dist/types/core/agents/claude-code/approvals.d.ts +26 -0
- package/dist/types/core/agents/claude-code/approvals.d.ts.map +1 -0
- package/dist/types/core/agents/claude-code/claude-agent.d.ts +70 -0
- package/dist/types/core/agents/claude-code/claude-agent.d.ts.map +1 -0
- package/dist/types/core/agents/claude-code/credentials.d.ts +54 -0
- package/dist/types/core/agents/claude-code/credentials.d.ts.map +1 -0
- package/dist/types/core/agents/claude-code/events.d.ts +42 -0
- package/dist/types/core/agents/claude-code/events.d.ts.map +1 -0
- package/dist/types/core/agents/claude-code/login.d.ts +61 -0
- package/dist/types/core/agents/claude-code/login.d.ts.map +1 -0
- package/dist/types/core/agents/claude-code/mcp.d.ts +52 -0
- package/dist/types/core/agents/claude-code/mcp.d.ts.map +1 -0
- package/dist/types/core/agents/claude-code/models.d.ts +30 -0
- package/dist/types/core/agents/claude-code/models.d.ts.map +1 -0
- package/dist/types/core/agents/claude-code/options.d.ts +183 -0
- package/dist/types/core/agents/claude-code/options.d.ts.map +1 -0
- package/dist/types/core/agents/claude-code/outcome.d.ts +50 -0
- package/dist/types/core/agents/claude-code/outcome.d.ts.map +1 -0
- package/dist/types/core/agents/claude-code/session-pool.d.ts +36 -0
- package/dist/types/core/agents/claude-code/session-pool.d.ts.map +1 -0
- package/dist/types/core/agents/claude-code/session.d.ts +148 -0
- package/dist/types/core/agents/claude-code/session.d.ts.map +1 -0
- package/dist/types/core/agents/claude-code/spawn.d.ts +29 -0
- package/dist/types/core/agents/claude-code/spawn.d.ts.map +1 -0
- package/dist/types/core/agents/claude-code/spend.d.ts +58 -0
- package/dist/types/core/agents/claude-code/spend.d.ts.map +1 -0
- package/dist/types/core/agents/claude-code/tools.d.ts +4 -0
- package/dist/types/core/agents/claude-code/tools.d.ts.map +1 -0
- package/dist/types/core/agents/claude-code/turn-usage.d.ts +52 -0
- package/dist/types/core/agents/claude-code/turn-usage.d.ts.map +1 -0
- package/dist/types/core/agents/claude-code/turn.d.ts +121 -0
- package/dist/types/core/agents/claude-code/turn.d.ts.map +1 -0
- package/dist/types/core/agents/codex-app-server/adapter.d.ts +59 -0
- package/dist/types/core/agents/codex-app-server/adapter.d.ts.map +1 -0
- package/dist/types/core/agents/codex-app-server/client.d.ts +116 -0
- package/dist/types/core/agents/codex-app-server/client.d.ts.map +1 -0
- package/dist/types/core/agents/codex-app-server/codex-agent.d.ts +113 -0
- package/dist/types/core/agents/codex-app-server/codex-agent.d.ts.map +1 -0
- package/dist/types/core/agents/codex-app-server/commands.d.ts +66 -0
- package/dist/types/core/agents/codex-app-server/commands.d.ts.map +1 -0
- package/dist/types/core/agents/codex-app-server/credentials.d.ts +49 -0
- package/dist/types/core/agents/codex-app-server/credentials.d.ts.map +1 -0
- package/dist/types/core/agents/codex-app-server/effort.d.ts +12 -0
- package/dist/types/core/agents/codex-app-server/effort.d.ts.map +1 -0
- package/dist/types/core/agents/codex-app-server/events.d.ts +45 -0
- package/dist/types/core/agents/codex-app-server/events.d.ts.map +1 -0
- package/dist/types/core/agents/codex-app-server/homes.d.ts +20 -0
- package/dist/types/core/agents/codex-app-server/homes.d.ts.map +1 -0
- package/dist/types/core/agents/codex-app-server/input.d.ts +26 -0
- package/dist/types/core/agents/codex-app-server/input.d.ts.map +1 -0
- package/dist/types/core/agents/codex-app-server/items.d.ts +31 -0
- package/dist/types/core/agents/codex-app-server/items.d.ts.map +1 -0
- package/dist/types/core/agents/codex-app-server/login.d.ts +38 -0
- package/dist/types/core/agents/codex-app-server/login.d.ts.map +1 -0
- package/dist/types/core/agents/codex-app-server/mcp.d.ts +62 -0
- package/dist/types/core/agents/codex-app-server/mcp.d.ts.map +1 -0
- package/dist/types/core/agents/codex-app-server/models.d.ts +67 -0
- package/dist/types/core/agents/codex-app-server/models.d.ts.map +1 -0
- package/dist/types/core/agents/codex-app-server/options.d.ts +40 -0
- package/dist/types/core/agents/codex-app-server/options.d.ts.map +1 -0
- package/dist/types/core/agents/codex-app-server/plan-approval.d.ts +19 -0
- package/dist/types/core/agents/codex-app-server/plan-approval.d.ts.map +1 -0
- package/dist/types/core/agents/codex-app-server/protocol.d.ts +99 -0
- package/dist/types/core/agents/codex-app-server/protocol.d.ts.map +1 -0
- package/dist/types/core/agents/codex-app-server/requests.d.ts +42 -0
- package/dist/types/core/agents/codex-app-server/requests.d.ts.map +1 -0
- package/dist/types/core/agents/codex-app-server/session.d.ts +96 -0
- package/dist/types/core/agents/codex-app-server/session.d.ts.map +1 -0
- package/dist/types/core/agents/codex-app-server/subagents.d.ts +53 -0
- package/dist/types/core/agents/codex-app-server/subagents.d.ts.map +1 -0
- package/dist/types/core/agents/codex-app-server/thread-settings.d.ts +87 -0
- package/dist/types/core/agents/codex-app-server/thread-settings.d.ts.map +1 -0
- package/dist/types/core/agents/codex-app-server/turn-items.d.ts +33 -0
- package/dist/types/core/agents/codex-app-server/turn-items.d.ts.map +1 -0
- package/dist/types/core/agents/codex-app-server/turn.d.ts +163 -0
- package/dist/types/core/agents/codex-app-server/turn.d.ts.map +1 -0
- package/dist/types/core/agents/config-fingerprint.d.ts +5 -0
- package/dist/types/core/agents/config-fingerprint.d.ts.map +1 -0
- package/dist/types/core/agents/credentials.d.ts +28 -0
- package/dist/types/core/agents/credentials.d.ts.map +1 -0
- package/dist/types/core/agents/elicitation.d.ts +37 -0
- package/dist/types/core/agents/elicitation.d.ts.map +1 -0
- package/dist/types/core/agents/inherit-config.d.ts +18 -0
- package/dist/types/core/agents/inherit-config.d.ts.map +1 -0
- package/dist/types/core/agents/login.d.ts +67 -0
- package/dist/types/core/agents/login.d.ts.map +1 -0
- package/dist/types/core/agents/mcp-config.d.ts +57 -0
- package/dist/types/core/agents/mcp-config.d.ts.map +1 -0
- package/dist/types/core/agents/plan.d.ts +16 -0
- package/dist/types/core/agents/plan.d.ts.map +1 -0
- package/dist/types/core/agents/registry.d.ts +30 -0
- package/dist/types/core/agents/registry.d.ts.map +1 -0
- package/dist/types/core/agents/session-agent.d.ts +48 -0
- package/dist/types/core/agents/session-agent.d.ts.map +1 -0
- package/dist/types/core/agents/session-pool.d.ts +41 -0
- package/dist/types/core/agents/session-pool.d.ts.map +1 -0
- package/dist/types/core/agents/stderr-tail.d.ts +28 -0
- package/dist/types/core/agents/stderr-tail.d.ts.map +1 -0
- package/dist/types/core/agents/thinking-levels.d.ts +4 -0
- package/dist/types/core/agents/thinking-levels.d.ts.map +1 -0
- package/dist/types/core/agents/types.d.ts +124 -0
- package/dist/types/core/agents/types.d.ts.map +1 -0
- package/dist/types/core/diagnostics.d.ts +36 -0
- package/dist/types/core/diagnostics.d.ts.map +1 -0
- package/dist/types/core/index.d.ts +29 -0
- package/dist/types/core/index.d.ts.map +1 -0
- package/dist/types/core/presets.d.ts +80 -0
- package/dist/types/core/presets.d.ts.map +1 -0
- package/dist/types/core/processes/identity.d.ts +31 -0
- package/dist/types/core/processes/identity.d.ts.map +1 -0
- package/dist/types/core/processes/lifeline.d.ts +47 -0
- package/dist/types/core/processes/lifeline.d.ts.map +1 -0
- package/dist/types/core/processes/native-processes.d.ts +130 -0
- package/dist/types/core/processes/native-processes.d.ts.map +1 -0
- package/dist/types/core/processes/process-table.d.ts +43 -0
- package/dist/types/core/processes/process-table.d.ts.map +1 -0
- package/dist/types/core/provision/cache-trust.d.ts +13 -0
- package/dist/types/core/provision/cache-trust.d.ts.map +1 -0
- package/dist/types/core/provision/cache-usage.d.ts +14 -0
- package/dist/types/core/provision/cache-usage.d.ts.map +1 -0
- package/dist/types/core/provision/checksums.d.ts +46 -0
- package/dist/types/core/provision/checksums.d.ts.map +1 -0
- package/dist/types/core/provision/extract.d.ts +7 -0
- package/dist/types/core/provision/extract.d.ts.map +1 -0
- package/dist/types/core/provision/index.d.ts +5 -0
- package/dist/types/core/provision/index.d.ts.map +1 -0
- package/dist/types/core/provision/install-build.d.ts +36 -0
- package/dist/types/core/provision/install-build.d.ts.map +1 -0
- package/dist/types/core/provision/npm.d.ts +40 -0
- package/dist/types/core/provision/npm.d.ts.map +1 -0
- package/dist/types/core/provision/pins.d.ts +24 -0
- package/dist/types/core/provision/pins.d.ts.map +1 -0
- package/dist/types/core/provision/platform.d.ts +28 -0
- package/dist/types/core/provision/platform.d.ts.map +1 -0
- package/dist/types/core/provision/provisioner.d.ts +189 -0
- package/dist/types/core/provision/provisioner.d.ts.map +1 -0
- package/dist/types/core/provision/releases.d.ts +8 -0
- package/dist/types/core/provision/releases.d.ts.map +1 -0
- package/dist/types/core/provision/tools.d.ts +81 -0
- package/dist/types/core/provision/tools.d.ts.map +1 -0
- package/dist/types/core/proxy/anthropic-proxy.d.ts +52 -0
- package/dist/types/core/proxy/anthropic-proxy.d.ts.map +1 -0
- package/dist/types/core/proxy/api-key-store.d.ts +37 -0
- package/dist/types/core/proxy/api-key-store.d.ts.map +1 -0
- package/dist/types/core/proxy/index.d.ts +3 -0
- package/dist/types/core/proxy/index.d.ts.map +1 -0
- package/dist/types/core/proxy/loopback.d.ts +14 -0
- package/dist/types/core/proxy/loopback.d.ts.map +1 -0
- package/dist/types/core/runtime/agent-runtime.d.ts +181 -0
- package/dist/types/core/runtime/agent-runtime.d.ts.map +1 -0
- package/dist/types/core/runtime/event-processor.d.ts +87 -0
- package/dist/types/core/runtime/event-processor.d.ts.map +1 -0
- package/dist/types/core/runtime/event-sink.d.ts +44 -0
- package/dist/types/core/runtime/event-sink.d.ts.map +1 -0
- package/dist/types/core/runtime/harness-clis.d.ts +10 -0
- package/dist/types/core/runtime/harness-clis.d.ts.map +1 -0
- package/dist/types/core/runtime/harness-logins.d.ts +68 -0
- package/dist/types/core/runtime/harness-logins.d.ts.map +1 -0
- package/dist/types/core/runtime/login-reads.d.ts +28 -0
- package/dist/types/core/runtime/login-reads.d.ts.map +1 -0
- package/dist/types/core/runtime/model-lists.d.ts +37 -0
- package/dist/types/core/runtime/model-lists.d.ts.map +1 -0
- package/dist/types/core/runtime/permission-policy.d.ts +61 -0
- package/dist/types/core/runtime/permission-policy.d.ts.map +1 -0
- package/dist/types/core/runtime/request-broker.d.ts +49 -0
- package/dist/types/core/runtime/request-broker.d.ts.map +1 -0
- package/dist/types/core/runtime/steering.d.ts +38 -0
- package/dist/types/core/runtime/steering.d.ts.map +1 -0
- package/dist/types/core/runtime/turn-outcome.d.ts +21 -0
- package/dist/types/core/runtime/turn-outcome.d.ts.map +1 -0
- package/dist/types/core/runtime/turn.d.ts +121 -0
- package/dist/types/core/runtime/turn.d.ts.map +1 -0
- package/dist/types/core/utils/bounded.d.ts +6 -0
- package/dist/types/core/utils/bounded.d.ts.map +1 -0
- package/dist/types/core/utils/deadline.d.ts +14 -0
- package/dist/types/core/utils/deadline.d.ts.map +1 -0
- package/dist/types/core/utils/errors.d.ts +104 -0
- package/dist/types/core/utils/errors.d.ts.map +1 -0
- package/dist/types/core/utils/is-record.d.ts +3 -0
- package/dist/types/core/utils/is-record.d.ts.map +1 -0
- package/dist/types/core/utils/loopback.d.ts +6 -0
- package/dist/types/core/utils/loopback.d.ts.map +1 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/protocol/account.d.ts +26 -0
- package/dist/types/protocol/account.d.ts.map +1 -0
- package/dist/types/protocol/async-queue.d.ts +21 -0
- package/dist/types/protocol/async-queue.d.ts.map +1 -0
- package/dist/types/protocol/auth.d.ts +228 -0
- package/dist/types/protocol/auth.d.ts.map +1 -0
- package/dist/types/protocol/canonical.d.ts +9 -0
- package/dist/types/protocol/canonical.d.ts.map +1 -0
- package/dist/types/protocol/clis.d.ts +74 -0
- package/dist/types/protocol/clis.d.ts.map +1 -0
- package/dist/types/protocol/config.d.ts +200 -0
- package/dist/types/protocol/config.d.ts.map +1 -0
- package/dist/types/protocol/conversation.d.ts +942 -0
- package/dist/types/protocol/conversation.d.ts.map +1 -0
- package/dist/types/protocol/credentials.d.ts +145 -0
- package/dist/types/protocol/credentials.d.ts.map +1 -0
- package/dist/types/protocol/errors.d.ts +36 -0
- package/dist/types/protocol/errors.d.ts.map +1 -0
- package/dist/types/protocol/execution.d.ts +28 -0
- package/dist/types/protocol/execution.d.ts.map +1 -0
- package/dist/types/protocol/factories.d.ts +94 -0
- package/dist/types/protocol/factories.d.ts.map +1 -0
- package/dist/types/protocol/guards.d.ts +55 -0
- package/dist/types/protocol/guards.d.ts.map +1 -0
- package/dist/types/protocol/harness.d.ts +70 -0
- package/dist/types/protocol/harness.d.ts.map +1 -0
- package/dist/types/protocol/history.d.ts +318 -0
- package/dist/types/protocol/history.d.ts.map +1 -0
- package/dist/types/protocol/ids.d.ts +9 -0
- package/dist/types/protocol/ids.d.ts.map +1 -0
- package/dist/types/protocol/index.d.ts +35 -0
- package/dist/types/protocol/index.d.ts.map +1 -0
- package/dist/types/protocol/lifecycle.d.ts +1190 -0
- package/dist/types/protocol/lifecycle.d.ts.map +1 -0
- package/dist/types/protocol/meta-keys.d.ts +17 -0
- package/dist/types/protocol/meta-keys.d.ts.map +1 -0
- package/dist/types/protocol/meta.d.ts +13 -0
- package/dist/types/protocol/meta.d.ts.map +1 -0
- package/dist/types/protocol/model-catalog.d.ts +11 -0
- package/dist/types/protocol/model-catalog.d.ts.map +1 -0
- package/dist/types/protocol/models.d.ts +107 -0
- package/dist/types/protocol/models.d.ts.map +1 -0
- package/dist/types/protocol/part-input.d.ts +101 -0
- package/dist/types/protocol/part-input.d.ts.map +1 -0
- package/dist/types/protocol/parts.d.ts +357 -0
- package/dist/types/protocol/parts.d.ts.map +1 -0
- package/dist/types/protocol/reduce.d.ts +26 -0
- package/dist/types/protocol/reduce.d.ts.map +1 -0
- package/dist/types/protocol/selectors.d.ts +64 -0
- package/dist/types/protocol/selectors.d.ts.map +1 -0
- package/dist/types/protocol/seq-cursor.d.ts +66 -0
- package/dist/types/protocol/seq-cursor.d.ts.map +1 -0
- package/dist/types/protocol/stop-reasons.d.ts +33 -0
- package/dist/types/protocol/stop-reasons.d.ts.map +1 -0
- package/dist/types/protocol/thinking.d.ts +28 -0
- package/dist/types/protocol/thinking.d.ts.map +1 -0
- package/dist/types/protocol/time.d.ts +21 -0
- package/dist/types/protocol/time.d.ts.map +1 -0
- package/dist/types/protocol/tokens.d.ts +29 -0
- package/dist/types/protocol/tokens.d.ts.map +1 -0
- package/dist/types/protocol/tool-closure.d.ts +25 -0
- package/dist/types/protocol/tool-closure.d.ts.map +1 -0
- package/dist/types/protocol/tool-state.d.ts +209 -0
- package/dist/types/protocol/tool-state.d.ts.map +1 -0
- package/dist/types/protocol/verify.d.ts +52 -0
- package/dist/types/protocol/verify.d.ts.map +1 -0
- package/dist/types/protocol/vocabulary.d.ts +19 -0
- package/dist/types/protocol/vocabulary.d.ts.map +1 -0
- package/dist/types/protocol/wire-api.d.ts +1983 -0
- package/dist/types/protocol/wire-api.d.ts.map +1 -0
- package/dist/types/protocol/wire.d.ts +1758 -0
- package/dist/types/protocol/wire.d.ts.map +1 -0
- package/dist/types/server/acp/binding.d.ts +33 -0
- package/dist/types/server/acp/binding.d.ts.map +1 -0
- package/dist/types/server/acp/session-records.d.ts +17 -0
- package/dist/types/server/acp/session-records.d.ts.map +1 -0
- package/dist/types/server/acp/translate.d.ts +48 -0
- package/dist/types/server/acp/translate.d.ts.map +1 -0
- package/dist/types/server/agent-server.d.ts +121 -0
- package/dist/types/server/agent-server.d.ts.map +1 -0
- package/dist/types/server/feedback.d.ts +37 -0
- package/dist/types/server/feedback.d.ts.map +1 -0
- package/dist/types/server/history.d.ts +15 -0
- package/dist/types/server/history.d.ts.map +1 -0
- package/dist/types/server/index.d.ts +7 -0
- package/dist/types/server/index.d.ts.map +1 -0
- package/dist/types/server/install.d.ts +42 -0
- package/dist/types/server/install.d.ts.map +1 -0
- package/dist/types/server/open-parts.d.ts +27 -0
- package/dist/types/server/open-parts.d.ts.map +1 -0
- package/dist/types/server/routes.d.ts +61 -0
- package/dist/types/server/routes.d.ts.map +1 -0
- package/dist/types/server/secrets.d.ts +11 -0
- package/dist/types/server/secrets.d.ts.map +1 -0
- package/dist/types/server/session-gate.d.ts +32 -0
- package/dist/types/server/session-gate.d.ts.map +1 -0
- package/dist/types/server/session-log.d.ts +39 -0
- package/dist/types/server/session-log.d.ts.map +1 -0
- package/dist/types/server/session-store.d.ts +119 -0
- package/dist/types/server/session-store.d.ts.map +1 -0
- package/dist/types/server/subscriptions.d.ts +74 -0
- package/dist/types/server/subscriptions.d.ts.map +1 -0
- package/dist/types/server/transports.d.ts +74 -0
- package/dist/types/server/transports.d.ts.map +1 -0
- package/dist/types/server/wire-error.d.ts +9 -0
- package/dist/types/server/wire-error.d.ts.map +1 -0
- package/dist/types/sync/event-log.d.ts +45 -0
- package/dist/types/sync/event-log.d.ts.map +1 -0
- package/dist/types/sync/fit.d.ts +40 -0
- package/dist/types/sync/fit.d.ts.map +1 -0
- package/dist/types/sync/history-reader.d.ts +8 -0
- package/dist/types/sync/history-reader.d.ts.map +1 -0
- package/dist/types/sync/history-sqlite.d.ts +37 -0
- package/dist/types/sync/history-sqlite.d.ts.map +1 -0
- package/dist/types/sync/history.d.ts +70 -0
- package/dist/types/sync/history.d.ts.map +1 -0
- package/dist/types/sync/index.d.ts +4 -0
- package/dist/types/sync/index.d.ts.map +1 -0
- package/dist/types/sync/journal-sqlite.d.ts +51 -0
- package/dist/types/sync/journal-sqlite.d.ts.map +1 -0
- package/dist/types/sync/journal.d.ts +107 -0
- package/dist/types/sync/journal.d.ts.map +1 -0
- package/dist/types/sync/live-tails.d.ts +30 -0
- package/dist/types/sync/live-tails.d.ts.map +1 -0
- package/dist/types/sync/node-sqlite.d.ts +7 -0
- package/dist/types/sync/node-sqlite.d.ts.map +1 -0
- package/dist/types/sync/projection.d.ts +10 -0
- package/dist/types/sync/projection.d.ts.map +1 -0
- package/dist/types/sync/recovery.d.ts +25 -0
- package/dist/types/sync/recovery.d.ts.map +1 -0
- package/dist/types/sync/retry.d.ts +25 -0
- package/dist/types/sync/retry.d.ts.map +1 -0
- package/dist/types/sync/schema.d.ts +4 -0
- package/dist/types/sync/schema.d.ts.map +1 -0
- package/dist/types/sync/sql.d.ts +38 -0
- package/dist/types/sync/sql.d.ts.map +1 -0
- package/dist/types/sync/sqlite-file.d.ts +9 -0
- package/dist/types/sync/sqlite-file.d.ts.map +1 -0
- package/dist/types/sync/sqlite.d.ts +6 -0
- package/dist/types/sync/sqlite.d.ts.map +1 -0
- package/dist/types/sync/worker.d.ts +63 -0
- package/dist/types/sync/worker.d.ts.map +1 -0
- package/docs/consuming.md +460 -54
- package/docs/deploy.md +218 -47
- package/docs/durable-sessions.md +195 -0
- package/docs/feedback.md +116 -0
- package/docs/harnesses.md +219 -87
- package/docs/sync-history.md +197 -0
- package/docs/sync-integration.md +119 -0
- package/package.json +53 -23
- package/src/accounts/chatgpt.ts +194 -0
- package/src/accounts/check.ts +112 -0
- package/src/accounts/claude.ts +111 -0
- package/src/accounts/endpoint.ts +151 -0
- package/src/accounts/http.ts +97 -0
- package/src/accounts/index.ts +19 -0
- package/src/client/client.ts +641 -602
- package/src/client/connection.ts +371 -0
- package/src/client/history.ts +65 -0
- package/src/client/index.ts +11 -2
- package/src/client/listeners.ts +20 -0
- package/src/client/logins.ts +105 -0
- package/src/client/rpc.ts +144 -0
- package/src/client/streams.ts +488 -0
- package/src/client/transports.ts +62 -8
- package/src/client/turns.ts +171 -0
- package/src/core/agents/acp/acp-agent.ts +94 -118
- package/src/core/agents/acp/adapter.ts +20 -9
- package/src/core/agents/acp/client.ts +125 -47
- package/src/core/agents/acp/known-agents.ts +36 -4
- package/src/core/agents/acp/mappings.ts +64 -18
- package/src/core/agents/acp/requests.ts +125 -0
- package/src/core/agents/attachments.ts +18 -0
- package/src/core/agents/base.ts +120 -42
- package/src/core/agents/claude-code/adapter.ts +177 -238
- package/src/core/agents/claude-code/approvals.ts +270 -0
- package/src/core/agents/claude-code/claude-agent.ts +175 -158
- package/src/core/agents/claude-code/credentials.ts +171 -0
- package/src/core/agents/claude-code/events.ts +55 -0
- package/src/core/agents/claude-code/login.ts +269 -0
- package/src/core/agents/claude-code/mcp.ts +206 -0
- package/src/core/agents/claude-code/models.ts +15 -9
- package/src/core/agents/claude-code/options.ts +326 -39
- package/src/core/agents/claude-code/outcome.ts +209 -0
- package/src/core/agents/claude-code/session-pool.ts +136 -0
- package/src/core/agents/claude-code/session.ts +513 -0
- package/src/core/agents/claude-code/spawn.ts +144 -0
- package/src/core/agents/claude-code/spend.ts +160 -0
- package/src/core/agents/claude-code/tools.ts +47 -0
- package/src/core/agents/claude-code/turn-usage.ts +135 -0
- package/src/core/agents/claude-code/turn.ts +232 -0
- package/src/core/agents/codex-app-server/adapter.ts +314 -117
- package/src/core/agents/codex-app-server/client.ts +186 -84
- package/src/core/agents/codex-app-server/codex-agent.ts +343 -0
- package/src/core/agents/codex-app-server/commands.ts +228 -0
- package/src/core/agents/codex-app-server/credentials.ts +126 -0
- package/src/core/agents/codex-app-server/effort.ts +29 -0
- package/src/core/agents/codex-app-server/events.ts +47 -0
- package/src/core/agents/codex-app-server/homes.ts +64 -0
- package/src/core/agents/codex-app-server/input.ts +60 -0
- package/src/core/agents/codex-app-server/items.ts +149 -0
- package/src/core/agents/codex-app-server/login.ts +180 -0
- package/src/core/agents/codex-app-server/mcp.ts +182 -37
- package/src/core/agents/codex-app-server/models.ts +99 -46
- package/src/core/agents/codex-app-server/options.ts +146 -0
- package/src/core/agents/codex-app-server/plan-approval.ts +135 -0
- package/src/core/agents/codex-app-server/protocol.ts +117 -0
- package/src/core/agents/codex-app-server/requests.ts +332 -0
- package/src/core/agents/codex-app-server/session.ts +285 -0
- package/src/core/agents/codex-app-server/subagents.ts +108 -42
- package/src/core/agents/codex-app-server/thread-settings.ts +279 -0
- package/src/core/agents/codex-app-server/turn-items.ts +88 -0
- package/src/core/agents/codex-app-server/turn.ts +463 -0
- package/src/core/agents/config-fingerprint.ts +7 -14
- package/src/core/agents/credentials.ts +98 -0
- package/src/core/agents/elicitation.ts +75 -0
- package/src/core/agents/inherit-config.ts +21 -0
- package/src/core/agents/login.ts +116 -0
- package/src/core/agents/mcp-config.ts +112 -8
- package/src/core/agents/plan.ts +25 -0
- package/src/core/agents/registry.ts +21 -4
- package/src/core/agents/session-agent.ts +96 -4
- package/src/core/agents/{session-store.ts → session-pool.ts} +11 -5
- package/src/core/agents/stderr-tail.ts +75 -0
- package/src/core/agents/types.ts +40 -5
- package/src/core/index.ts +60 -48
- package/src/core/presets.ts +100 -39
- package/src/core/processes/identity.ts +113 -0
- package/src/core/processes/lifeline.ts +170 -0
- package/src/core/processes/native-processes.ts +379 -0
- package/src/core/processes/process-table.ts +174 -0
- package/src/core/provision/cache-trust.ts +121 -0
- package/src/core/provision/cache-usage.ts +69 -0
- package/src/core/provision/checksums.ts +64 -0
- package/src/core/provision/extract.ts +11 -2
- package/src/core/provision/index.ts +4 -1
- package/src/core/provision/install-build.ts +136 -0
- package/src/core/provision/npm.ts +112 -17
- package/src/core/provision/pins.ts +11 -34
- package/src/core/provision/platform.ts +12 -1
- package/src/core/provision/provisioner.ts +329 -397
- package/src/core/provision/releases.ts +86 -0
- package/src/core/provision/tools.ts +329 -0
- package/src/core/proxy/anthropic-proxy.ts +55 -25
- package/src/core/proxy/api-key-store.ts +2 -12
- package/src/core/proxy/loopback.ts +106 -0
- package/src/core/runtime/agent-runtime.ts +340 -481
- package/src/core/runtime/event-processor.ts +207 -75
- package/src/core/runtime/event-sink.ts +45 -2
- package/src/core/runtime/harness-clis.ts +61 -0
- package/src/core/runtime/harness-logins.ts +328 -0
- package/src/core/runtime/login-reads.ts +78 -0
- package/src/core/runtime/model-lists.ts +57 -57
- package/src/core/runtime/permission-policy.ts +113 -0
- package/src/core/runtime/request-broker.ts +204 -0
- package/src/core/runtime/steering.ts +112 -0
- package/src/core/runtime/turn-outcome.ts +30 -0
- package/src/core/runtime/turn.ts +360 -0
- package/src/core/utils/bounded.ts +12 -0
- package/src/core/utils/deadline.ts +38 -0
- package/src/core/utils/errors.ts +113 -21
- package/src/core/utils/is-record.ts +4 -0
- package/src/core/utils/loopback.ts +10 -0
- package/src/protocol/account.ts +104 -0
- package/src/protocol/auth.ts +199 -0
- package/src/protocol/canonical.ts +16 -0
- package/src/protocol/clis.ts +69 -0
- package/src/protocol/config.ts +60 -6
- package/src/protocol/conversation.ts +272 -0
- package/src/protocol/credentials.ts +145 -0
- package/src/protocol/errors.ts +78 -20
- package/src/protocol/execution.ts +5 -2
- package/src/protocol/factories.ts +55 -7
- package/src/protocol/guards.ts +55 -2
- package/src/protocol/harness.ts +56 -6
- package/src/protocol/history.ts +275 -0
- package/src/protocol/ids.ts +4 -17
- package/src/protocol/index.ts +10 -0
- package/src/protocol/lifecycle.ts +250 -79
- package/src/protocol/meta-keys.ts +17 -0
- package/src/protocol/meta.ts +3 -3
- package/src/protocol/models.ts +2 -43
- package/src/protocol/part-input.ts +2 -1
- package/src/protocol/parts.ts +12 -3
- package/src/protocol/reduce.ts +302 -413
- package/src/protocol/selectors.ts +1 -20
- package/src/protocol/seq-cursor.ts +14 -0
- package/src/protocol/stop-reasons.ts +1 -1
- package/src/protocol/thinking.ts +1 -30
- package/src/protocol/time.ts +2 -2
- package/src/protocol/tool-closure.ts +93 -0
- package/src/protocol/tool-state.ts +19 -4
- package/src/protocol/verify.ts +222 -92
- package/src/protocol/vocabulary.ts +21 -5
- package/src/protocol/wire-api.ts +232 -0
- package/src/protocol/wire.ts +425 -57
- package/src/server/acp/binding.ts +63 -103
- package/src/server/acp/session-records.ts +53 -0
- package/src/server/acp/translate.ts +129 -20
- package/src/server/agent-server.ts +275 -366
- package/src/server/bin.ts +295 -72
- package/src/server/feedback.ts +204 -14
- package/src/server/history.ts +160 -0
- package/src/server/index.ts +4 -8
- package/src/server/install.ts +107 -8
- package/src/server/open-parts.ts +95 -0
- package/src/server/routes.ts +391 -0
- package/src/server/secrets.ts +24 -0
- package/src/server/session-gate.ts +69 -0
- package/src/server/session-log.ts +14 -2
- package/src/server/session-store.ts +289 -0
- package/src/server/subscriptions.ts +232 -0
- package/src/server/transports.ts +125 -25
- package/src/server/wire-error.ts +38 -0
- package/src/sync/event-log.ts +160 -0
- package/src/sync/fit.ts +140 -0
- package/src/sync/history-reader.ts +57 -0
- package/src/sync/history-sqlite.ts +339 -0
- package/src/sync/history.ts +220 -0
- package/src/sync/index.ts +18 -0
- package/src/sync/journal-sqlite.ts +439 -0
- package/src/sync/journal.ts +132 -0
- package/src/sync/live-tails.ts +107 -0
- package/src/sync/node-sqlite.ts +20 -0
- package/src/sync/projection.ts +35 -0
- package/src/sync/recovery.ts +141 -0
- package/src/sync/retry.ts +49 -0
- package/src/sync/schema.ts +77 -0
- package/src/sync/sql.ts +106 -0
- package/src/sync/sqlite-file.ts +38 -0
- package/src/sync/sqlite.ts +21 -0
- package/src/sync/worker.ts +259 -0
- package/docs/rfds/0001-deterministic-echo-ids.md +0 -44
- package/docs/rfds/0002-native-subagents.md +0 -197
- package/src/core/agents/claude-code/generator-session.ts +0 -572
- package/src/core/agents/claude-code/session-manager.ts +0 -166
- package/src/core/agents/codex-app-server/codex-app-server-agent.ts +0 -962
- package/src/core/agents/codex-items.ts +0 -149
- package/src/core/agents/codex-sdk/adapter.ts +0 -237
- package/src/core/agents/codex-sdk/codex-sdk-agent.ts +0 -330
- package/src/core/agents/tool-meta.ts +0 -68
package/src/client/client.ts
CHANGED
|
@@ -1,37 +1,52 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
2
|
+
type AgentHarness,
|
|
3
|
+
type AgentInput,
|
|
4
|
+
type AsyncHistoryApi,
|
|
5
|
+
type AuthUpdated,
|
|
6
|
+
AuthUpdatedSchema,
|
|
3
7
|
type DecodedEventsReplayResult,
|
|
4
|
-
type DecodedLifecycleEvent,
|
|
5
8
|
type DecodedWireEventEnvelope,
|
|
9
|
+
type DurableSession,
|
|
10
|
+
type ElicitationOutcome,
|
|
11
|
+
type HarnessAuth,
|
|
12
|
+
type HarnessCli,
|
|
13
|
+
type HarnessCliProgress,
|
|
14
|
+
HarnessCliProgressSchema,
|
|
6
15
|
type InitializeResult,
|
|
7
|
-
InitializeResultSchema,
|
|
8
|
-
type JsonRpcId,
|
|
9
16
|
type ModelList,
|
|
10
17
|
type ModelsListParams,
|
|
11
18
|
type PermissionOutcome,
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
19
|
+
type RunningTurn,
|
|
20
|
+
type SessionConfirmStoppedParams,
|
|
21
|
+
SessionOverflowParamsSchema,
|
|
22
|
+
type SessionSubscribeResult,
|
|
15
23
|
type TurnCancelResult,
|
|
16
|
-
|
|
17
|
-
type TurnEndedEvent,
|
|
24
|
+
type TurnReceipt,
|
|
18
25
|
type TurnStartParams,
|
|
19
|
-
|
|
20
|
-
type
|
|
26
|
+
type TurnStartResult,
|
|
27
|
+
type TurnSteerParams,
|
|
28
|
+
type TurnSteerResult,
|
|
21
29
|
WIRE_ERROR_CODES,
|
|
22
30
|
WIRE_METHODS,
|
|
23
|
-
WIRE_PROTOCOL_VERSION,
|
|
24
|
-
type WireImplementationInfo,
|
|
25
31
|
type WireTransport,
|
|
26
|
-
createSeqCursor,
|
|
27
|
-
decodeEventsReplayResult,
|
|
28
|
-
decodeModelList,
|
|
29
32
|
decodeWireEventEnvelope,
|
|
30
33
|
decodeWireMessage,
|
|
31
|
-
encodeRequest,
|
|
32
34
|
generateUUIDv7,
|
|
33
|
-
isUnknownEvent,
|
|
34
35
|
} from "../protocol/index.ts";
|
|
36
|
+
import {
|
|
37
|
+
type AgentServerClientOptions,
|
|
38
|
+
Connection,
|
|
39
|
+
ConnectionClosedError,
|
|
40
|
+
type ConnectionEvent,
|
|
41
|
+
type ConnectionRefusal,
|
|
42
|
+
ConnectionRefusedError,
|
|
43
|
+
type TransportFactory,
|
|
44
|
+
} from "./connection.ts";
|
|
45
|
+
import { createHistoryClient } from "./history.ts";
|
|
46
|
+
import { Listeners } from "./listeners.ts";
|
|
47
|
+
import { type LoginHandle, startLogin } from "./logins.ts";
|
|
48
|
+
import { WireRequestError } from "./rpc.ts";
|
|
49
|
+
import { EventGapError, type Follow, type SessionEventGap, Streams } from "./streams.ts";
|
|
35
50
|
import {
|
|
36
51
|
type SpawnServerOptions,
|
|
37
52
|
type WebSocketLike,
|
|
@@ -39,153 +54,125 @@ import {
|
|
|
39
54
|
socketTransport,
|
|
40
55
|
spawnTransport,
|
|
41
56
|
} from "./transports.ts";
|
|
57
|
+
import { type Handle, type TurnHandle, Turns } from "./turns.ts";
|
|
58
|
+
|
|
59
|
+
export {
|
|
60
|
+
type AgentServerClientOptions,
|
|
61
|
+
ConnectionClosedError,
|
|
62
|
+
type ConnectionEvent,
|
|
63
|
+
type ConnectionRefusal,
|
|
64
|
+
ConnectionRefusedError,
|
|
65
|
+
EventGapError,
|
|
66
|
+
type SessionEventGap,
|
|
67
|
+
type TurnHandle,
|
|
68
|
+
};
|
|
42
69
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
) {
|
|
51
|
-
super(message);
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/** The transport dropped (and reconnection, if enabled, gave up). */
|
|
56
|
-
export class ConnectionClosedError extends Error {
|
|
57
|
-
override readonly name = "ConnectionClosedError";
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* Events were lost irrecoverably: the replay window was evicted or the server
|
|
62
|
-
* restarted. The consumer's local view of the session is stale.
|
|
63
|
-
*/
|
|
64
|
-
export class EventGapError extends Error {
|
|
65
|
-
override readonly name = "EventGapError";
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
/** A delivery failure, reported before surviving events. Execution may continue. */
|
|
69
|
-
export interface SessionEventGap {
|
|
70
|
-
sessionId: string;
|
|
71
|
-
reason: "evicted" | "replay_failed" | "session_missing" | "server_restarted" | "log_reset";
|
|
72
|
-
error: EventGapError;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* A running turn: consume `events` (ends after `turn.ended`) or await `result`.
|
|
77
|
-
*
|
|
78
|
-
* `events` yields `UnknownEvent`s too (Law 6): a newer server's event type is
|
|
79
|
-
* delivered in order rather than dropped, so consumers persist and forward it.
|
|
80
|
-
* Narrow with `isUnknownEvent` before switching on a known `type`;
|
|
81
|
-
* `reduceConversation` already accepts the union as-is.
|
|
82
|
-
*/
|
|
83
|
-
export interface TurnHandle {
|
|
84
|
-
sessionId: string;
|
|
85
|
-
turnId: string;
|
|
86
|
-
events: AsyncIterableIterator<DecodedLifecycleEvent | UnknownEvent>;
|
|
87
|
-
result: Promise<TurnEndedEvent>;
|
|
70
|
+
export interface SubscribeOptions {
|
|
71
|
+
/**
|
|
72
|
+
* Catch up after this seq: the session's kept events after it reach
|
|
73
|
+
* `onEvent` first. Default: none, the head (live only). A session this
|
|
74
|
+
* client follows already keeps its place.
|
|
75
|
+
*/
|
|
76
|
+
afterSeq?: number;
|
|
88
77
|
}
|
|
89
78
|
|
|
90
|
-
|
|
79
|
+
/** How `connect` dials. */
|
|
80
|
+
export interface ConnectOptions extends AgentServerClientOptions {
|
|
91
81
|
/**
|
|
92
|
-
*
|
|
93
|
-
*
|
|
94
|
-
*
|
|
82
|
+
* Sent as `Authorization: Bearer <token>` with every dial, redials
|
|
83
|
+
* included: what a listener with a token requires. Bun and Node only; a
|
|
84
|
+
* browser's WebSocket sends no headers.
|
|
95
85
|
*/
|
|
96
|
-
|
|
97
|
-
maxReconnectAttempts?: number;
|
|
98
|
-
/** Base reconnect delay; grows exponentially, capped at 10s. */
|
|
99
|
-
reconnectDelayMs?: number;
|
|
100
|
-
requestTimeoutMs?: number;
|
|
101
|
-
/** Attempts to heal one sequence gap before failing its turn. Default 3. */
|
|
102
|
-
maxReplayAttempts?: number;
|
|
103
|
-
/** Delay between failed replay requests. Defaults to reconnectDelayMs. */
|
|
104
|
-
replayRetryDelayMs?: number;
|
|
105
|
-
/** Sent in `initialize`. */
|
|
106
|
-
clientInfo?: WireImplementationInfo;
|
|
86
|
+
token?: string;
|
|
107
87
|
}
|
|
108
88
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
89
|
+
/** Where a subscription started. */
|
|
90
|
+
export interface SessionSubscription {
|
|
91
|
+
sessionId: string;
|
|
92
|
+
/** The session's last kept seq at the subscription point. */
|
|
93
|
+
headSeq: number;
|
|
94
|
+
/** The turn running at the point. */
|
|
95
|
+
activeTurn?: RunningTurn;
|
|
113
96
|
}
|
|
114
97
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
rejectResult: (error: unknown) => void;
|
|
98
|
+
/** How a turn handle follows its turn (`runTurn`, `joinTurn`). */
|
|
99
|
+
export interface FollowOptions {
|
|
100
|
+
/** Stop following: `events` and `result` fail with the signal's reason, an `AbortError`. */
|
|
101
|
+
signal?: AbortSignal;
|
|
120
102
|
}
|
|
121
103
|
|
|
122
|
-
interface
|
|
123
|
-
|
|
104
|
+
export interface JoinTurnOptions extends FollowOptions {
|
|
105
|
+
/** The first kept event to yield. Default: the running turn's `turn.started`. */
|
|
106
|
+
fromSeq?: number;
|
|
124
107
|
}
|
|
125
108
|
|
|
126
|
-
/**
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
}
|
|
139
|
-
},
|
|
140
|
-
};
|
|
141
|
-
|
|
142
|
-
/** A model list from a server that may know thinking levels this build does not. */
|
|
143
|
-
const MODEL_LIST: ResultSchema<ModelList> = {
|
|
144
|
-
safeParse(raw) {
|
|
145
|
-
try {
|
|
146
|
-
return { success: true, data: decodeModelList(raw) };
|
|
147
|
-
} catch {
|
|
148
|
-
return { success: false };
|
|
149
|
-
}
|
|
150
|
-
},
|
|
151
|
-
};
|
|
109
|
+
/** `signal` stops the handle following its turn. */
|
|
110
|
+
function stopOn(handle: Handle, signal: AbortSignal | undefined): void {
|
|
111
|
+
if (!signal) return;
|
|
112
|
+
const stop = () => handle.fail(signal.reason ?? new Error("stopped following the turn"));
|
|
113
|
+
if (signal.aborted) {
|
|
114
|
+
stop();
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
signal.addEventListener("abort", stop, { once: true });
|
|
118
|
+
const off = () => signal.removeEventListener("abort", stop);
|
|
119
|
+
void handle.result.then(off, off);
|
|
120
|
+
}
|
|
152
121
|
|
|
153
|
-
|
|
122
|
+
/** How often `prepareClis` asks again while an install runs, besides when one ends. */
|
|
123
|
+
const PREPARE_POLL_MS = 2_000;
|
|
154
124
|
|
|
155
125
|
/**
|
|
156
126
|
* Typed client for the agent-server wire. Turns are quick-ack: `runTurn`
|
|
157
127
|
* resolves once the server accepts the turn and hands back an async iterable
|
|
158
|
-
*
|
|
159
|
-
*
|
|
160
|
-
*
|
|
161
|
-
*
|
|
128
|
+
* of its events, from its `turn.started` to its `turn.ended`. The client
|
|
129
|
+
* follows the sessions it subscribed to (and its turns subscribe): each
|
|
130
|
+
* session's `seq` is tracked so duplicates are dropped and gaps healed via
|
|
131
|
+
* `events/replay`, and — in `connect` mode — a dropped socket is redialed and
|
|
132
|
+
* its sessions subscribed again.
|
|
162
133
|
*/
|
|
163
134
|
export class AgentServerClient {
|
|
164
|
-
private
|
|
165
|
-
private
|
|
166
|
-
private readonly
|
|
167
|
-
|
|
168
|
-
private readonly
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
Array<{ envelope: DecodedWireEventEnvelope; transport: WireTransport | null }>
|
|
172
|
-
>();
|
|
173
|
-
/** The server process behind the transport, learned at `initialize`. */
|
|
174
|
-
private serverInstanceId: string | undefined;
|
|
175
|
-
private readonly syncing = new Map<string, WireTransport>();
|
|
176
|
-
private readonly eventHandlers = new Set<(envelope: DecodedWireEventEnvelope) => void>();
|
|
177
|
-
private readonly gapHandlers = new Set<(gap: SessionEventGap) => void>();
|
|
178
|
-
private awaitingHandshake = false;
|
|
179
|
-
private readonly turnBySession = new Map<string, InternalTurn>();
|
|
180
|
-
private initializePromise: Promise<InitializeResult> | null = null;
|
|
181
|
-
private closed = false;
|
|
182
|
-
private reconnecting = false;
|
|
135
|
+
private readonly connection: Connection;
|
|
136
|
+
private readonly turns = new Turns();
|
|
137
|
+
private readonly progress = new Listeners<HarnessCliProgress>();
|
|
138
|
+
private readonly authUpdates = new Listeners<AuthUpdated>();
|
|
139
|
+
private readonly streams: Streams;
|
|
140
|
+
/** Portable history operations (`history/*`), independent of execution subscriptions. */
|
|
141
|
+
readonly history: AsyncHistoryApi;
|
|
183
142
|
|
|
184
143
|
private constructor(
|
|
185
|
-
|
|
144
|
+
factory: TransportFactory,
|
|
186
145
|
private readonly options: AgentServerClientOptions,
|
|
187
|
-
|
|
188
|
-
) {
|
|
146
|
+
redials: boolean,
|
|
147
|
+
) {
|
|
148
|
+
this.connection = new Connection(factory, options, redials, {
|
|
149
|
+
line: (line, transport) => this.handleLine(line, transport),
|
|
150
|
+
opened: (redialed) => this.streams.reconnected(redialed),
|
|
151
|
+
serverChanged: () => this.streams.adoptFreshLogs(),
|
|
152
|
+
reconnected: () => {
|
|
153
|
+
this.streams.resume();
|
|
154
|
+
this.subscribeAgain();
|
|
155
|
+
},
|
|
156
|
+
closed: (error) => this.turns.failAll(error),
|
|
157
|
+
});
|
|
158
|
+
this.streams = new Streams(
|
|
159
|
+
{
|
|
160
|
+
transport: () => this.connection.transport,
|
|
161
|
+
open: () => !this.connection.closed,
|
|
162
|
+
replay: (sessionId, fromSeq) => this.replay(sessionId, fromSeq),
|
|
163
|
+
resubscribe: (sessionId, turnId) => void this.resubscribe(sessionId, turnId),
|
|
164
|
+
resubscribeAll: () => void this.resubscribeAll(),
|
|
165
|
+
maxReplayAttempts: options.maxReplayAttempts ?? 3,
|
|
166
|
+
replayRetryDelayMs: options.replayRetryDelayMs ?? options.reconnectDelayMs ?? 250,
|
|
167
|
+
},
|
|
168
|
+
this.turns,
|
|
169
|
+
);
|
|
170
|
+
this.history = createHistoryClient(
|
|
171
|
+
(method, params) => this.connection.request(method, params),
|
|
172
|
+
() => this.initialize(),
|
|
173
|
+
options,
|
|
174
|
+
);
|
|
175
|
+
}
|
|
189
176
|
|
|
190
177
|
/**
|
|
191
178
|
* Bring your own transport: the factory is invoked for the initial
|
|
@@ -196,7 +183,7 @@ export class AgentServerClient {
|
|
|
196
183
|
options: AgentServerClientOptions = {},
|
|
197
184
|
): Promise<AgentServerClient> {
|
|
198
185
|
const client = new AgentServerClient(factory, options, options.reconnect ?? true);
|
|
199
|
-
await client.
|
|
186
|
+
await client.connection.open();
|
|
200
187
|
return client;
|
|
201
188
|
}
|
|
202
189
|
|
|
@@ -210,21 +197,24 @@ export class AgentServerClient {
|
|
|
210
197
|
options,
|
|
211
198
|
options.reconnect ?? false,
|
|
212
199
|
);
|
|
213
|
-
await client.
|
|
200
|
+
await client.connection.open();
|
|
214
201
|
return client;
|
|
215
202
|
}
|
|
216
203
|
|
|
217
|
-
/**
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
204
|
+
/**
|
|
205
|
+
* Dial a listening agent-server (reconnects by default). One that refuses
|
|
206
|
+
* the client (401: the token it sent, or that it sent none) fails it with
|
|
207
|
+
* `ConnectionRefusedError`, and is not redialed.
|
|
208
|
+
*/
|
|
209
|
+
static async connect(url: string, options: ConnectOptions = {}): Promise<AgentServerClient> {
|
|
210
|
+
const headers =
|
|
211
|
+
options.token === undefined ? undefined : { authorization: `Bearer ${options.token}` };
|
|
222
212
|
const client = new AgentServerClient(
|
|
223
|
-
() => connectTransport(url),
|
|
213
|
+
() => connectTransport(url, headers),
|
|
224
214
|
options,
|
|
225
215
|
options.reconnect ?? true,
|
|
226
216
|
);
|
|
227
|
-
await client.
|
|
217
|
+
await client.connection.open();
|
|
228
218
|
return client;
|
|
229
219
|
}
|
|
230
220
|
|
|
@@ -244,137 +234,137 @@ export class AgentServerClient {
|
|
|
244
234
|
options,
|
|
245
235
|
false,
|
|
246
236
|
);
|
|
247
|
-
await client.
|
|
237
|
+
await client.connection.open();
|
|
248
238
|
return client;
|
|
249
239
|
}
|
|
250
240
|
|
|
251
|
-
// ----
|
|
241
|
+
// ---- turns ---------------------------------------------------------------
|
|
252
242
|
|
|
253
243
|
/** Handshake (memoized). Called lazily by `runTurn`/`startTurn`. */
|
|
254
244
|
initialize(): Promise<InitializeResult> {
|
|
255
|
-
|
|
256
|
-
WIRE_METHODS.initialize,
|
|
257
|
-
{ protocolVersion: WIRE_PROTOCOL_VERSION, client: this.options.clientInfo },
|
|
258
|
-
InitializeResultSchema,
|
|
259
|
-
)
|
|
260
|
-
.then((result) => {
|
|
261
|
-
if (result.protocolVersion !== WIRE_PROTOCOL_VERSION) {
|
|
262
|
-
throw new WireRequestError(
|
|
263
|
-
WIRE_ERROR_CODES.protocolVersionMismatch,
|
|
264
|
-
`server selected unsupported protocol version: ${result.protocolVersion}`,
|
|
265
|
-
{ supported: [WIRE_PROTOCOL_VERSION] },
|
|
266
|
-
);
|
|
267
|
-
}
|
|
268
|
-
// The restart question, answered by the handshake instead of
|
|
269
|
-
// inferred from seq patterns: a changed instanceId means every
|
|
270
|
-
// session log we were tracking belongs to a dead process.
|
|
271
|
-
if (this.serverInstanceId !== undefined && this.serverInstanceId !== result.instanceId) {
|
|
272
|
-
this.adoptFreshLogs();
|
|
273
|
-
}
|
|
274
|
-
this.serverInstanceId = result.instanceId;
|
|
275
|
-
return result;
|
|
276
|
-
})
|
|
277
|
-
.catch((err) => {
|
|
278
|
-
this.initializePromise = null;
|
|
279
|
-
throw err;
|
|
280
|
-
});
|
|
281
|
-
return this.initializePromise;
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
/**
|
|
285
|
-
* The server process we were talking to is gone; its logs died with it.
|
|
286
|
-
* Drop every per-session cursor and queue so the reconnect resync replays
|
|
287
|
-
* each fresh log from seq 1. Local views may be stale relative to the fresh
|
|
288
|
-
* logs; consumers with durable state resnapshot on their side.
|
|
289
|
-
*/
|
|
290
|
-
private adoptFreshLogs(): void {
|
|
291
|
-
// Active turns first: their logs died with the old process, and no fresh
|
|
292
|
-
// log will ever emit THEIR turn.ended — an un-failed handle hangs forever
|
|
293
|
-
// (a session id the fresh process happens to reuse would even feed it
|
|
294
|
-
// someone else's events).
|
|
295
|
-
const sessionIds = new Set([...this.cursor.sessions(), ...this.turnBySession.keys()]);
|
|
296
|
-
for (const sessionId of sessionIds) {
|
|
297
|
-
this.reportEventGap(
|
|
298
|
-
sessionId,
|
|
299
|
-
"server_restarted",
|
|
300
|
-
new EventGapError(
|
|
301
|
-
`server instance changed; the turn's session log died with the old process`,
|
|
302
|
-
),
|
|
303
|
-
);
|
|
304
|
-
}
|
|
305
|
-
for (const sessionId of this.cursor.sessions()) this.cursor.reset(sessionId);
|
|
306
|
-
// Keep frames actually received from the fresh process: they may already
|
|
307
|
-
// have left its bounded replay window. Only old transport entries die.
|
|
308
|
-
for (const [sessionId, held] of this.heldBack) {
|
|
309
|
-
const fresh = held.filter((entry) => entry.transport === this.transport);
|
|
310
|
-
if (fresh.length) {
|
|
311
|
-
this.heldBack.set(sessionId, fresh);
|
|
312
|
-
this.cursor.seek(sessionId, 0);
|
|
313
|
-
} else this.heldBack.delete(sessionId);
|
|
314
|
-
}
|
|
245
|
+
return this.connection.initialize();
|
|
315
246
|
}
|
|
316
247
|
|
|
317
248
|
/**
|
|
318
|
-
* Start a turn and stream it
|
|
319
|
-
*
|
|
249
|
+
* Start a turn and stream it, from its `turn.started` to its `turn.ended`.
|
|
250
|
+
* Ids are minted client-side when omitted. The start subscribes this client
|
|
251
|
+
* to the session from its head until the turn ends, so it downloads none
|
|
252
|
+
* of its history; a start re-sent after a redial converges on the turn the
|
|
253
|
+
* server admitted and catches up on it from its `turn.started`. `signal`
|
|
254
|
+
* stops the handle, as on `joinTurn`: the turn runs on (`cancelTurn` stops
|
|
255
|
+
* it), and `onEvent` hears it to its end.
|
|
320
256
|
*/
|
|
321
|
-
async runTurn(params: TurnStartParams): Promise<TurnHandle> {
|
|
257
|
+
async runTurn(params: TurnStartParams, options: FollowOptions = {}): Promise<TurnHandle> {
|
|
322
258
|
await this.initialize();
|
|
323
259
|
const sessionId = params.sessionId ?? generateUUIDv7();
|
|
324
260
|
const turnId = params.turnId ?? generateUUIDv7();
|
|
325
|
-
if (this.
|
|
261
|
+
if (this.turns.running(sessionId))
|
|
326
262
|
throw new Error(`session ${sessionId} already has an active turn`);
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
let resolveResult!: (event: TurnEndedEvent) => void;
|
|
330
|
-
let rejectResult!: (error: unknown) => void;
|
|
331
|
-
const result = new Promise<TurnEndedEvent>((resolve, reject) => {
|
|
332
|
-
resolveResult = resolve;
|
|
333
|
-
rejectResult = reject;
|
|
334
|
-
});
|
|
335
|
-
// Consumers may only iterate `events`; don't surface an unhandled rejection.
|
|
336
|
-
result.catch(() => undefined);
|
|
337
|
-
this.turnBySession.set(sessionId, { turnId, queue, resolveResult, rejectResult });
|
|
263
|
+
const handle = this.turns.open(sessionId, turnId, true);
|
|
264
|
+
stopOn(handle, options.signal);
|
|
338
265
|
try {
|
|
339
|
-
await this.
|
|
266
|
+
const accepted = await this.connection.resend(
|
|
340
267
|
WIRE_METHODS.turnStart,
|
|
341
|
-
{ ...params, sessionId, turnId },
|
|
342
|
-
|
|
268
|
+
{ ...params, sessionId, turnId, subscribe: true },
|
|
269
|
+
// A turn that already ended streams nothing: its end is in the ack.
|
|
270
|
+
(ack) => {
|
|
271
|
+
if (!ack.terminal) this.joinAt(handle, ack.headSeq, { turnId });
|
|
272
|
+
},
|
|
343
273
|
);
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
274
|
+
const { terminal } = accepted;
|
|
275
|
+
if (terminal && (terminal.sessionId !== sessionId || terminal.turnId !== turnId))
|
|
276
|
+
throw new Error("Turn receipt belongs to a different request");
|
|
277
|
+
// A retried start of a turn that already ended: its end, without its events
|
|
278
|
+
// (`replay` or `history` serve its transcript).
|
|
279
|
+
if (terminal) handle.push(terminal);
|
|
280
|
+
else
|
|
281
|
+
await this.catchUp(
|
|
282
|
+
handle,
|
|
283
|
+
accepted.deduplicated
|
|
284
|
+
? (accepted.startedSeq ?? accepted.headSeq + 1)
|
|
285
|
+
: accepted.headSeq + 1,
|
|
286
|
+
accepted.headSeq,
|
|
287
|
+
);
|
|
288
|
+
} catch (error) {
|
|
289
|
+
handle.fail(error);
|
|
290
|
+
throw error;
|
|
349
291
|
}
|
|
350
|
-
return
|
|
292
|
+
return this.handleOf(handle);
|
|
351
293
|
}
|
|
352
294
|
|
|
353
|
-
/**
|
|
354
|
-
|
|
295
|
+
/**
|
|
296
|
+
* Quick-ack only: the turn's events reach `onEvent` through its
|
|
297
|
+
* `turn.ended` (unless `subscribe: false`), and `replay` serves what came
|
|
298
|
+
* before.
|
|
299
|
+
*/
|
|
300
|
+
async startTurn(params: TurnStartParams): Promise<TurnStartResult> {
|
|
355
301
|
await this.initialize();
|
|
356
302
|
const sessionId = params.sessionId ?? generateUUIDv7();
|
|
357
303
|
const turnId = params.turnId ?? generateUUIDv7();
|
|
358
|
-
|
|
359
|
-
return this.request(
|
|
304
|
+
return this.connection.request(
|
|
360
305
|
WIRE_METHODS.turnStart,
|
|
361
306
|
{ ...params, sessionId, turnId },
|
|
362
|
-
|
|
307
|
+
(ack) => {
|
|
308
|
+
if (params.subscribe !== false && !ack.terminal)
|
|
309
|
+
this.streams.joined(sessionId, ack.headSeq, { turnId });
|
|
310
|
+
},
|
|
363
311
|
);
|
|
364
312
|
}
|
|
365
313
|
|
|
366
|
-
/**
|
|
367
|
-
|
|
314
|
+
/**
|
|
315
|
+
* Join the turn running in `sessionId`. Yields, each once and in seq
|
|
316
|
+
* order: the session's kept events from `fromSeq` (default: the turn's
|
|
317
|
+
* `turn.started`), the turn's open parts as of the join, then live frames
|
|
318
|
+
* through the turn's `turn.ended` — so even when this client already
|
|
319
|
+
* follows the session or another handle joined earlier. Resolves once the
|
|
320
|
+
* catch-up is replayed (`catchUp` counts it); `undefined` when no turn is
|
|
321
|
+
* running. `steer` and `result` work as on `runTurn`'s handle. Joining
|
|
322
|
+
* watches the session (`subscribe`).
|
|
323
|
+
*/
|
|
324
|
+
async joinTurn(
|
|
368
325
|
sessionId: string,
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
326
|
+
options: JoinTurnOptions = {},
|
|
327
|
+
): Promise<TurnHandle | undefined> {
|
|
328
|
+
await this.initialize();
|
|
329
|
+
let handle: Handle | undefined;
|
|
330
|
+
const answer = await this.subscribeTo({ sessionId }, (ack) => {
|
|
331
|
+
if (!ack.activeTurn) return this.streams.joined(sessionId, ack.headSeq, "watch");
|
|
332
|
+
handle = this.turns.open(sessionId, ack.activeTurn.turnId, false);
|
|
333
|
+
this.joinAt(handle, ack.headSeq, "watch");
|
|
334
|
+
});
|
|
335
|
+
if (!handle) return undefined;
|
|
336
|
+
stopOn(handle, options.signal);
|
|
337
|
+
const fromSeq = options.fromSeq ?? answer.activeTurn?.startedSeq ?? answer.headSeq + 1;
|
|
338
|
+
await this.catchUp(handle, fromSeq, answer.headSeq);
|
|
339
|
+
return this.handleOf(handle);
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
// @ref LLP 0001#d13--input-while-a-turn-runs-steers-it — the client half
|
|
343
|
+
/**
|
|
344
|
+
* Add input to a running turn. The agent reads it at its next step, where
|
|
345
|
+
* the turn's stream shows it as a user message (`steerMessageId(steerId)`);
|
|
346
|
+
* a turn that ends first lists it in `turn.ended.undeliveredSteers`.
|
|
347
|
+
* `no_active_turn` means the turn takes no more input — send it as a new
|
|
348
|
+
* turn; `unsupported` means the harness cannot steer. `steerId` is minted
|
|
349
|
+
* when omitted; a re-send with the same one converges, so a steer whose
|
|
350
|
+
* answer was lost to a reconnect is re-sent, never doubled. A session this
|
|
351
|
+
* client does not follow is followed first, until the turn ends, so the
|
|
352
|
+
* steer's user message, its receipt, reaches it.
|
|
353
|
+
*/
|
|
354
|
+
async steerTurn(params: TurnSteerParams): Promise<TurnSteerResult> {
|
|
355
|
+
await this.initialize();
|
|
356
|
+
const { sessionId, turnId } = params;
|
|
357
|
+
// One it cannot subscribe to still takes the steer; only its receipt is lost.
|
|
358
|
+
if (!this.streams.follows(sessionId))
|
|
359
|
+
await this.subscribeTo({ sessionId, turnId }, (ack) => {
|
|
360
|
+
// A turn that is not running subscribes nothing, and is not followed.
|
|
361
|
+
if (ack.activeTurn?.turnId === turnId)
|
|
362
|
+
this.streams.joined(sessionId, ack.headSeq, { turnId });
|
|
363
|
+
}).catch(() => undefined);
|
|
364
|
+
return this.connection.resend(WIRE_METHODS.turnSteer, {
|
|
365
|
+
...params,
|
|
366
|
+
steerId: params.steerId ?? generateUUIDv7(),
|
|
367
|
+
});
|
|
378
368
|
}
|
|
379
369
|
|
|
380
370
|
/**
|
|
@@ -386,418 +376,467 @@ export class AgentServerClient {
|
|
|
386
376
|
* be running; `turn.ended` remains the source of truth.
|
|
387
377
|
*/
|
|
388
378
|
cancelTurn(sessionId: string, turnId?: string): Promise<TurnCancelResult> {
|
|
389
|
-
return this.request(
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
379
|
+
return this.connection.request(WIRE_METHODS.turnCancel, {
|
|
380
|
+
sessionId,
|
|
381
|
+
...(turnId !== undefined ? { turnId } : {}),
|
|
382
|
+
});
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
/** Answer a `permission.requested` event. */
|
|
386
|
+
async respondPermission(
|
|
387
|
+
sessionId: string,
|
|
388
|
+
requestId: string,
|
|
389
|
+
outcome: PermissionOutcome,
|
|
390
|
+
): Promise<boolean> {
|
|
391
|
+
const { accepted } = await this.connection.request(WIRE_METHODS.permissionRespond, {
|
|
392
|
+
sessionId,
|
|
393
|
+
requestId,
|
|
394
|
+
outcome,
|
|
395
|
+
});
|
|
396
|
+
return accepted;
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
/** Answer an `elicitation.requested` event. */
|
|
400
|
+
async respondElicitation(
|
|
401
|
+
sessionId: string,
|
|
402
|
+
requestId: string,
|
|
403
|
+
outcome: ElicitationOutcome,
|
|
404
|
+
): Promise<boolean> {
|
|
405
|
+
const { accepted } = await this.connection.request(WIRE_METHODS.elicitationRespond, {
|
|
406
|
+
sessionId,
|
|
407
|
+
requestId,
|
|
408
|
+
outcome,
|
|
409
|
+
});
|
|
410
|
+
return accepted;
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
// ---- sessions ------------------------------------------------------------
|
|
414
|
+
|
|
415
|
+
/**
|
|
416
|
+
* Watch a session until `unsubscribe` (or its end): its kept events after
|
|
417
|
+
* `afterSeq` reach `onEvent` (and the session's handles), each once and in
|
|
418
|
+
* order, then its open parts, then its frames live; after a redial, what a
|
|
419
|
+
* dropped connection missed. Refused `unknownSession` when the server has
|
|
420
|
+
* no log for it, and `subscriptionLimit` past the sessions a connection may
|
|
421
|
+
* watch.
|
|
422
|
+
*/
|
|
423
|
+
async subscribe(sessionId: string, options: SubscribeOptions = {}): Promise<SessionSubscription> {
|
|
424
|
+
await this.initialize();
|
|
425
|
+
// What it holds, or `afterSeq`, it catches up from with `events/replay`.
|
|
426
|
+
const answer = await this.subscribeTo({ sessionId }, (ack) =>
|
|
427
|
+
this.streams.joined(sessionId, ack.headSeq, "watch", options.afterSeq),
|
|
393
428
|
);
|
|
429
|
+
return {
|
|
430
|
+
sessionId,
|
|
431
|
+
headSeq: answer.headSeq,
|
|
432
|
+
...(answer.activeTurn && { activeTurn: answer.activeTurn }),
|
|
433
|
+
};
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
/**
|
|
437
|
+
* Stop watching a session; `false` when this client did not. A turn of
|
|
438
|
+
* this client's that runs there is still followed to its end.
|
|
439
|
+
*/
|
|
440
|
+
async unsubscribe(sessionId: string): Promise<boolean> {
|
|
441
|
+
const watched = this.streams.watches(sessionId);
|
|
442
|
+
this.streams.unwatch(sessionId);
|
|
443
|
+
const { unsubscribed } = await this.connection.request(WIRE_METHODS.sessionUnsubscribe, {
|
|
444
|
+
sessionId,
|
|
445
|
+
});
|
|
446
|
+
return unsubscribed || watched;
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
/**
|
|
450
|
+
* Every session's frames, live from now, to `onEvent` (observers): no
|
|
451
|
+
* history, the open parts of running turns, and after a redial, the gaps a
|
|
452
|
+
* dropped connection left healed.
|
|
453
|
+
*/
|
|
454
|
+
async subscribeAll(): Promise<void> {
|
|
455
|
+
await this.initialize();
|
|
456
|
+
await this.requestAll();
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
/** Stop following every session (`subscribeAll`); those subscribed by name stay. */
|
|
460
|
+
async unsubscribeAll(): Promise<boolean> {
|
|
461
|
+
this.streams.unfollowAll();
|
|
462
|
+
const { unsubscribed } = await this.connection.request(WIRE_METHODS.sessionUnsubscribeAll, {});
|
|
463
|
+
return unsubscribed;
|
|
394
464
|
}
|
|
395
465
|
|
|
396
466
|
/** Release an idle session's replay history and harness-native resources. */
|
|
397
467
|
async closeSession(sessionId: string): Promise<boolean> {
|
|
398
|
-
if (this.
|
|
468
|
+
if (this.turns.running(sessionId))
|
|
399
469
|
throw new Error(`cannot close session ${sessionId} while its turn is active`);
|
|
400
|
-
}
|
|
401
|
-
|
|
402
|
-
WIRE_METHODS.sessionClose,
|
|
403
|
-
{ sessionId },
|
|
404
|
-
SessionCloseResultSchema,
|
|
405
|
-
);
|
|
406
|
-
// Keep the seq watermark: the server continues a reused id's seq where the
|
|
407
|
-
// closed incarnation stopped, so post-reuse events stay contiguous for this
|
|
408
|
-
// client too. A genuine server restart surfaces as a transport drop, and the
|
|
409
|
-
// resync's unknownSession path resets the watermark then.
|
|
410
|
-
this.heldBack.delete(sessionId);
|
|
411
|
-
this.syncing.delete(sessionId);
|
|
470
|
+
const { closed } = await this.connection.request(WIRE_METHODS.sessionClose, { sessionId });
|
|
471
|
+
this.streams.forget(sessionId);
|
|
412
472
|
return closed;
|
|
413
473
|
}
|
|
414
474
|
|
|
475
|
+
/**
|
|
476
|
+
* Durable servers: let an idle session's agent go (its process, a Codex
|
|
477
|
+
* home it holds) while the session stays open, as after a restart. Its
|
|
478
|
+
* next turn names its native conversation (`config.resumeSessionId`; a turn
|
|
479
|
+
* that does not is refused `resumeRequired`, whose data names it), also a
|
|
480
|
+
* turn another client of the session sends: nothing tells it of the
|
|
481
|
+
* release. `false` when the server holds no open session by that id.
|
|
482
|
+
*/
|
|
483
|
+
async releaseSession(sessionId: string): Promise<boolean> {
|
|
484
|
+
await this.initialize();
|
|
485
|
+
const { released } = await this.connection.request(WIRE_METHODS.sessionRelease, {
|
|
486
|
+
sessionId,
|
|
487
|
+
});
|
|
488
|
+
return released;
|
|
489
|
+
}
|
|
490
|
+
|
|
415
491
|
/**
|
|
416
492
|
* The models a harness offers for the login and folder a turn with the same
|
|
417
493
|
* `cwd`, `env` and `apiKey` would use. `source: "catalog"` (with `error`)
|
|
418
494
|
* means the CLI did not answer and the list is agent-server's built-in one.
|
|
419
495
|
*/
|
|
420
496
|
listModels(params: ModelsListParams): Promise<ModelList> {
|
|
421
|
-
return this.request(WIRE_METHODS.modelsList, params
|
|
497
|
+
return this.connection.request(WIRE_METHODS.modelsList, params);
|
|
422
498
|
}
|
|
423
499
|
|
|
424
|
-
/**
|
|
500
|
+
/**
|
|
501
|
+
* Where each harness's CLI is on the server (every harness when absent):
|
|
502
|
+
* `ready`, `missing` (what its first turn installs), `installing`,
|
|
503
|
+
* `failed` (with `error`) or `unmanaged`. Installs nothing.
|
|
504
|
+
*/
|
|
505
|
+
async listClis(harnesses?: AgentHarness[]): Promise<HarnessCli[]> {
|
|
506
|
+
await this.initialize();
|
|
507
|
+
const { clis } = await this.connection.request(WIRE_METHODS.clisList, {
|
|
508
|
+
...(harnesses && { harnesses }),
|
|
509
|
+
});
|
|
510
|
+
return clis;
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
/**
|
|
514
|
+
* Install each harness's CLI on the server now, so a first turn does not
|
|
515
|
+
* wait for it, and answer once each is ready or failed. The server answers
|
|
516
|
+
* `clis/prepare` at once, since an install outlasts a request's timeout;
|
|
517
|
+
* this waits for `clis/progress` to say an install ended, asking
|
|
518
|
+
* `clis/list` then and every few seconds, across a redial to the same
|
|
519
|
+
* server. Safe to call again: a running install is joined. `signal` stops
|
|
520
|
+
* the waiting, not the install.
|
|
521
|
+
*/
|
|
522
|
+
async prepareClis(
|
|
523
|
+
harnesses?: AgentHarness[],
|
|
524
|
+
options: { signal?: AbortSignal } = {},
|
|
525
|
+
): Promise<HarnessCli[]> {
|
|
526
|
+
const { signal } = options;
|
|
527
|
+
signal?.throwIfAborted();
|
|
528
|
+
// Before the first request: a redial asks again only of the server it knows.
|
|
529
|
+
await this.initialize();
|
|
530
|
+
const which = { ...(harnesses && { harnesses }) };
|
|
531
|
+
let wake = () => {};
|
|
532
|
+
const off = this.progress.add(({ phase }) => {
|
|
533
|
+
if (phase === "ready" || phase === "failed") wake();
|
|
534
|
+
});
|
|
535
|
+
/** `work`, or the signal's reason once it aborts. */
|
|
536
|
+
const unlessAborted = <T>(work: (aborted: Promise<never>) => Promise<T>): Promise<T> => {
|
|
537
|
+
if (!signal) return work(new Promise<never>(() => {}));
|
|
538
|
+
let stop = () => {};
|
|
539
|
+
const aborted = new Promise<never>((_, reject) => {
|
|
540
|
+
stop = () => reject(signal.reason);
|
|
541
|
+
signal.addEventListener("abort", stop, { once: true });
|
|
542
|
+
});
|
|
543
|
+
return work(aborted).finally(() => signal.removeEventListener("abort", stop));
|
|
544
|
+
};
|
|
545
|
+
try {
|
|
546
|
+
let { clis } = await unlessAborted((aborted) =>
|
|
547
|
+
Promise.race([this.connection.resend(WIRE_METHODS.clisPrepare, which), aborted]),
|
|
548
|
+
);
|
|
549
|
+
while (clis.some((cli) => cli.state === "installing")) {
|
|
550
|
+
({ clis } = await unlessAborted(async (aborted) => {
|
|
551
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
552
|
+
try {
|
|
553
|
+
await Promise.race([
|
|
554
|
+
new Promise<void>((resolve) => {
|
|
555
|
+
timer = setTimeout(resolve, PREPARE_POLL_MS);
|
|
556
|
+
wake = resolve;
|
|
557
|
+
}),
|
|
558
|
+
aborted,
|
|
559
|
+
]);
|
|
560
|
+
} finally {
|
|
561
|
+
clearTimeout(timer);
|
|
562
|
+
}
|
|
563
|
+
return Promise.race([this.connection.resend(WIRE_METHODS.clisList, which), aborted]);
|
|
564
|
+
}));
|
|
565
|
+
}
|
|
566
|
+
return clis;
|
|
567
|
+
} finally {
|
|
568
|
+
off();
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
/** Fetch kept events (`seq >= fromSeq`) without touching what the client follows.
|
|
573
|
+
* A durable server answers a page at a time: `hasMore` asks for the next. */
|
|
425
574
|
replay(sessionId: string, fromSeq: number): Promise<DecodedEventsReplayResult> {
|
|
426
|
-
return this.request(WIRE_METHODS.eventsReplay, { sessionId, fromSeq }
|
|
575
|
+
return this.connection.request(WIRE_METHODS.eventsReplay, { sessionId, fromSeq });
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
/**
|
|
579
|
+
* The session's kept events from `fromSeq` through `throughSeq` (default:
|
|
580
|
+
* the last), oldest first: every page `replay` answers. Fails
|
|
581
|
+
* `EventGapError` when some of them were evicted.
|
|
582
|
+
*/
|
|
583
|
+
async replayAll(
|
|
584
|
+
sessionId: string,
|
|
585
|
+
fromSeq: number,
|
|
586
|
+
throughSeq = Number.POSITIVE_INFINITY,
|
|
587
|
+
): Promise<DecodedWireEventEnvelope[]> {
|
|
588
|
+
const kept: DecodedWireEventEnvelope[] = [];
|
|
589
|
+
for (let from = fromSeq; from <= throughSeq; ) {
|
|
590
|
+
const page = await this.replay(sessionId, from);
|
|
591
|
+
if (page.firstAvailableSeq !== null && page.firstAvailableSeq > from)
|
|
592
|
+
throw new EventGapError(
|
|
593
|
+
`events ${from}..${page.firstAvailableSeq - 1} for session ${sessionId} were evicted`,
|
|
594
|
+
);
|
|
595
|
+
for (const envelope of page.events)
|
|
596
|
+
if (envelope.seq >= from && envelope.seq <= throughSeq) kept.push(envelope);
|
|
597
|
+
const last = page.events.at(-1)?.seq;
|
|
598
|
+
if (!page.hasMore || last === undefined || last >= throughSeq) break;
|
|
599
|
+
from = last + 1;
|
|
600
|
+
}
|
|
601
|
+
return kept;
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
/** Durable servers: a turn's receipt, even for a turn a restart cut off. */
|
|
605
|
+
async getTurn(sessionId: string, turnId: string): Promise<TurnReceipt> {
|
|
606
|
+
await this.initialize();
|
|
607
|
+
return this.connection.request(WIRE_METHODS.turnGet, { sessionId, turnId });
|
|
427
608
|
}
|
|
428
609
|
|
|
429
|
-
/**
|
|
610
|
+
/** Durable servers: the session's head, native id, and whether a restart cut off its last turn. */
|
|
611
|
+
async getSession(sessionId: string): Promise<DurableSession> {
|
|
612
|
+
await this.initialize();
|
|
613
|
+
return this.connection.request(WIRE_METHODS.sessionGet, { sessionId });
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
/** Durable servers: lift the fence a restart put on a session. Check first
|
|
617
|
+
* that its native execution stopped: history alone cannot prove that a
|
|
618
|
+
* detached agent or tool process is dead. */
|
|
619
|
+
async confirmSessionStopped(params: SessionConfirmStoppedParams): Promise<boolean> {
|
|
620
|
+
await this.initialize();
|
|
621
|
+
const { recovered } = await this.connection.request(WIRE_METHODS.sessionConfirmStopped, params);
|
|
622
|
+
return recovered;
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
// ---- logins ----------------------------------------------------------------
|
|
626
|
+
|
|
627
|
+
/**
|
|
628
|
+
* Each harness's login on the server (every harness when `harnesses` is
|
|
629
|
+
* absent), for the store and environment a turn with `env` would use:
|
|
630
|
+
* signed in, with what and as whom, signed out, or unknown and why; and
|
|
631
|
+
* the logins it offers.
|
|
632
|
+
*/
|
|
633
|
+
async authStatus(
|
|
634
|
+
options: { harnesses?: AgentHarness[]; env?: Record<string, string> } = {},
|
|
635
|
+
): Promise<HarnessAuth[]> {
|
|
636
|
+
await this.initialize();
|
|
637
|
+
const { auth } = await this.connection.request(WIRE_METHODS.authStatus, options);
|
|
638
|
+
return auth;
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
/**
|
|
642
|
+
* Start one of a harness's logins through its CLI's own flow: answered
|
|
643
|
+
* once the CLI says where the user signs in (`url`, `userCode`, or from
|
|
644
|
+
* another device `codeUrl`, then `complete` with the code it shows).
|
|
645
|
+
* `done` resolves with the harness's login once it succeeded.
|
|
646
|
+
*/
|
|
647
|
+
async login(
|
|
648
|
+
harness: AgentHarness,
|
|
649
|
+
method: string,
|
|
650
|
+
options: { apiKey?: string; env?: Record<string, string> } = {},
|
|
651
|
+
): Promise<LoginHandle> {
|
|
652
|
+
await this.initialize();
|
|
653
|
+
return startLogin(
|
|
654
|
+
{
|
|
655
|
+
updates: this.authUpdates,
|
|
656
|
+
connection: this.connection.events,
|
|
657
|
+
complete: (loginId, code) =>
|
|
658
|
+
this.connection.request(WIRE_METHODS.authComplete, { loginId, code }),
|
|
659
|
+
cancel: (loginId) => this.connection.request(WIRE_METHODS.authCancel, { loginId }),
|
|
660
|
+
},
|
|
661
|
+
() => this.connection.request(WIRE_METHODS.authLogin, { harness, method, ...options }),
|
|
662
|
+
);
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
/** Remove a harness's CLI login on the server; answers its login after. */
|
|
666
|
+
async logout(
|
|
667
|
+
harness: AgentHarness,
|
|
668
|
+
options: { env?: Record<string, string> } = {},
|
|
669
|
+
): Promise<HarnessAuth> {
|
|
670
|
+
await this.initialize();
|
|
671
|
+
return this.connection.request(WIRE_METHODS.authLogout, { harness, ...options });
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
// ---- observing -----------------------------------------------------------
|
|
675
|
+
|
|
676
|
+
/** The frames of the sessions this client follows, each once and in order (post-dedupe). */
|
|
430
677
|
onEvent(handler: (envelope: DecodedWireEventEnvelope) => void): () => void {
|
|
431
|
-
this.
|
|
432
|
-
|
|
678
|
+
return this.streams.events.add(handler);
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
/** A harness's CLI being installed on the server, whatever caused it (`clis/progress`). */
|
|
682
|
+
onCliProgress(handler: (progress: HarnessCliProgress) => void): () => void {
|
|
683
|
+
return this.progress.add(handler);
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
/** A harness's login changed on the server, or a login ended, whoever caused it (`auth/updated`). */
|
|
687
|
+
onAuthUpdated(handler: (update: AuthUpdated) => void): () => void {
|
|
688
|
+
return this.authUpdates.add(handler);
|
|
433
689
|
}
|
|
434
690
|
|
|
435
691
|
/** Observe unrecoverable delivery gaps even when using startTurn without a handle. */
|
|
436
692
|
onEventGap(handler: (gap: SessionEventGap) => void): () => void {
|
|
437
|
-
this.
|
|
438
|
-
|
|
693
|
+
return this.streams.gaps.add(handler);
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
/**
|
|
697
|
+
* Observe the connection: a drop, each redial attempt, the reconnect (and
|
|
698
|
+
* whether the server is still the one whose logs this client followed),
|
|
699
|
+
* and the end. Handlers run synchronously, before requests waiting on the
|
|
700
|
+
* redial are sent again; one that throws is ignored.
|
|
701
|
+
*/
|
|
702
|
+
onConnection(handler: (event: ConnectionEvent) => void): () => void {
|
|
703
|
+
return this.connection.events.add(handler);
|
|
439
704
|
}
|
|
440
705
|
|
|
441
706
|
/** Ask the server to shut down, then close this client. */
|
|
442
707
|
async shutdownServer(): Promise<void> {
|
|
443
|
-
await this.request(WIRE_METHODS.shutdown, {}
|
|
708
|
+
await this.connection.request(WIRE_METHODS.shutdown, {}).catch(() => undefined);
|
|
444
709
|
await this.close();
|
|
445
710
|
}
|
|
446
711
|
|
|
447
712
|
async close(): Promise<void> {
|
|
448
|
-
|
|
449
|
-
this.closed = true;
|
|
450
|
-
const reason = new ConnectionClosedError("client closed");
|
|
451
|
-
this.failPending(reason);
|
|
452
|
-
this.failAllTurns(reason);
|
|
453
|
-
this.transport?.close();
|
|
454
|
-
this.transport = null;
|
|
455
|
-
}
|
|
456
|
-
|
|
457
|
-
// ---- transport lifecycle -----------------------------------------------
|
|
458
|
-
|
|
459
|
-
private async openTransport(): Promise<void> {
|
|
460
|
-
const transport = await this.factory();
|
|
461
|
-
if (this.closed) {
|
|
462
|
-
transport.close();
|
|
463
|
-
throw new ConnectionClosedError("client closed while connecting");
|
|
464
|
-
}
|
|
465
|
-
if (transport.closed) throw new ConnectionClosedError("transport opened already closed");
|
|
466
|
-
this.transport = transport;
|
|
467
|
-
this.awaitingHandshake = this.reconnecting;
|
|
468
|
-
transport.onLine((line) => {
|
|
469
|
-
if (this.transport === transport) this.handleLine(line);
|
|
470
|
-
});
|
|
471
|
-
transport.onClose((reason) => this.handleTransportClose(transport, reason));
|
|
472
|
-
}
|
|
473
|
-
|
|
474
|
-
private handleTransportClose(transport: WireTransport, reason?: string): void {
|
|
475
|
-
// A late close from a superseded transport must not tear down the current one.
|
|
476
|
-
if (this.transport !== transport) return;
|
|
477
|
-
this.transport = null;
|
|
478
|
-
this.failPending(new ConnectionClosedError(reason ?? "connection closed"));
|
|
479
|
-
if (this.closed) return;
|
|
480
|
-
if (!this.reconnectEnabled) {
|
|
481
|
-
this.closed = true;
|
|
482
|
-
this.failAllTurns(new ConnectionClosedError(reason ?? "connection closed"));
|
|
483
|
-
return;
|
|
484
|
-
}
|
|
485
|
-
void this.reconnectLoop();
|
|
713
|
+
this.connection.close();
|
|
486
714
|
}
|
|
487
715
|
|
|
488
|
-
|
|
489
|
-
if (this.reconnecting) return;
|
|
490
|
-
this.reconnecting = true;
|
|
491
|
-
const maxAttempts = this.options.maxReconnectAttempts ?? 8;
|
|
492
|
-
const base = this.options.reconnectDelayMs ?? 250;
|
|
493
|
-
try {
|
|
494
|
-
for (let attempt = 0; attempt < maxAttempts && !this.closed; attempt++) {
|
|
495
|
-
await sleep(Math.min(10_000, base * 2 ** Math.min(attempt, 5)));
|
|
496
|
-
if (this.closed) break;
|
|
497
|
-
try {
|
|
498
|
-
await this.openTransport();
|
|
499
|
-
if (this.closed || !this.transport) continue;
|
|
500
|
-
// Re-handshake: the transport may have reconnected to a DIFFERENT
|
|
501
|
-
// process (or a different build). A version mismatch fails loudly
|
|
502
|
-
// here instead of silently mixing dialects, and a changed
|
|
503
|
-
// instanceId adopts the fresh logs before any resync runs.
|
|
504
|
-
this.initializePromise = null;
|
|
505
|
-
await this.initialize();
|
|
506
|
-
if (this.closed || !this.transport) continue;
|
|
507
|
-
this.awaitingHandshake = false;
|
|
508
|
-
// Heal every tracked session — including turns that saw no event
|
|
509
|
-
// before the drop (or none after it): without a resync they would
|
|
510
|
-
// never receive their turn.ended and would hang forever.
|
|
511
|
-
const sessionIds = new Set([
|
|
512
|
-
...this.cursor.sessions(),
|
|
513
|
-
...this.turnBySession.keys(),
|
|
514
|
-
...this.heldBack.keys(),
|
|
515
|
-
]);
|
|
516
|
-
for (const sessionId of sessionIds) void this.syncSession(sessionId);
|
|
517
|
-
if (!this.transport) continue;
|
|
518
|
-
return;
|
|
519
|
-
} catch {
|
|
520
|
-
// next attempt
|
|
521
|
-
}
|
|
522
|
-
}
|
|
523
|
-
if (!this.closed) {
|
|
524
|
-
this.closed = true;
|
|
525
|
-
this.failAllTurns(new ConnectionClosedError("reconnect failed"));
|
|
526
|
-
}
|
|
527
|
-
} finally {
|
|
528
|
-
this.reconnecting = false;
|
|
529
|
-
}
|
|
530
|
-
}
|
|
531
|
-
|
|
532
|
-
// ---- inbound dispatch ----------------------------------------------------
|
|
716
|
+
// ---- internals -------------------------------------------------------------
|
|
533
717
|
|
|
534
|
-
private handleLine(line: string): void {
|
|
535
|
-
const
|
|
536
|
-
if (
|
|
537
|
-
|
|
538
|
-
const pending = this.pending.get(msg.id);
|
|
539
|
-
if (!pending) return;
|
|
540
|
-
this.pending.delete(msg.id);
|
|
541
|
-
clearTimeout(pending.timer);
|
|
542
|
-
if (msg.error)
|
|
543
|
-
pending.reject(new WireRequestError(msg.error.code, msg.error.message, msg.error.data));
|
|
544
|
-
else pending.resolve(msg.result);
|
|
718
|
+
private handleLine(line: string, transport: WireTransport): void {
|
|
719
|
+
const message = decodeWireMessage(line);
|
|
720
|
+
if (message.kind === "response") {
|
|
721
|
+
this.connection.rpc.settle(message);
|
|
545
722
|
return;
|
|
546
723
|
}
|
|
547
|
-
if (
|
|
724
|
+
if (message.kind !== "notification") return;
|
|
725
|
+
if (message.method === WIRE_METHODS.event) {
|
|
548
726
|
try {
|
|
549
|
-
//
|
|
550
|
-
//
|
|
727
|
+
// An unknown event TYPE is preserved and still advances seq. Only a
|
|
728
|
+
// malformed envelope or a malformed KNOWN event lands in the catch —
|
|
551
729
|
// a server bug, which must not be papered over by advancing past it.
|
|
552
|
-
this.
|
|
730
|
+
this.streams.accept(decodeWireEventEnvelope(message.params), transport);
|
|
553
731
|
} catch {
|
|
554
732
|
// undeliverable; the next event's seq gap raises it honestly
|
|
555
733
|
}
|
|
734
|
+
} else if (message.method === WIRE_METHODS.sessionOverflow) {
|
|
735
|
+
const notice = SessionOverflowParamsSchema.safeParse(message.params);
|
|
736
|
+
if (notice.success) this.streams.overflowed(notice.data);
|
|
737
|
+
} else if (message.method === WIRE_METHODS.clisProgress) {
|
|
738
|
+
const progress = HarnessCliProgressSchema.safeParse(message.params);
|
|
739
|
+
if (progress.success) this.progress.emit(progress.data);
|
|
740
|
+
} else if (message.method === WIRE_METHODS.authUpdated) {
|
|
741
|
+
const update = AuthUpdatedSchema.safeParse(message.params);
|
|
742
|
+
if (update.success) this.authUpdates.emit(update.data);
|
|
556
743
|
}
|
|
557
744
|
}
|
|
558
745
|
|
|
559
746
|
/**
|
|
560
|
-
*
|
|
561
|
-
*
|
|
562
|
-
* The verdicts come from the shared `SeqCursor`; this method owns only what
|
|
563
|
-
* a client does about them (queue, replay, deliver).
|
|
747
|
+
* A start or join's answer, the moment it arrives: the handle's point
|
|
748
|
+
* among the frames. What follows the point waits for the catch-up before it.
|
|
564
749
|
*/
|
|
565
|
-
private
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
if (this.awaitingHandshake || this.syncing.get(sessionId) === this.transport) {
|
|
570
|
-
this.holdBack(sessionId, envelope);
|
|
571
|
-
return;
|
|
572
|
-
}
|
|
573
|
-
switch (this.cursor.advance(sessionId, envelope.seq)) {
|
|
574
|
-
case "duplicate":
|
|
575
|
-
return; // e.g. a replay overlap
|
|
576
|
-
case "reset":
|
|
577
|
-
// Defense-in-depth. Real restarts are detected by the handshake
|
|
578
|
-
// (`instanceId` at re-initialize) before any envelope flows — this
|
|
579
|
-
// arm fires only if a fresh log appears with NO transport drop,
|
|
580
|
-
// which no current topology produces. If it does: the queue is
|
|
581
|
-
// dead-log state, the fresh frame is authoritative.
|
|
582
|
-
this.reportEventGap(
|
|
583
|
-
sessionId,
|
|
584
|
-
"log_reset",
|
|
585
|
-
new EventGapError(`session ${sessionId} log reset`),
|
|
586
|
-
);
|
|
587
|
-
this.heldBack.delete(sessionId);
|
|
588
|
-
this.deliver(envelope);
|
|
589
|
-
return;
|
|
590
|
-
case "deliver":
|
|
591
|
-
this.deliver(envelope);
|
|
592
|
-
this.drainHeld(sessionId);
|
|
593
|
-
return;
|
|
594
|
-
case "gap":
|
|
595
|
-
this.holdBack(sessionId, envelope);
|
|
596
|
-
void this.syncSession(sessionId);
|
|
597
|
-
return;
|
|
598
|
-
}
|
|
599
|
-
}
|
|
600
|
-
|
|
601
|
-
private holdBack(sessionId: string, envelope: DecodedWireEventEnvelope): void {
|
|
602
|
-
const held = this.heldBack.get(sessionId) ?? [];
|
|
603
|
-
held.push({ envelope, transport: this.transport });
|
|
604
|
-
this.heldBack.set(sessionId, held);
|
|
605
|
-
}
|
|
606
|
-
|
|
607
|
-
private drainHeld(sessionId: string): void {
|
|
608
|
-
if (this.awaitingHandshake) return;
|
|
609
|
-
const held = this.heldBack.get(sessionId);
|
|
610
|
-
if (!held?.length) return;
|
|
611
|
-
held.sort((a, b) => a.envelope.seq - b.envelope.seq);
|
|
612
|
-
while (held.length) {
|
|
613
|
-
const next = held[0]!.envelope;
|
|
614
|
-
// `gap` leaves the cursor untouched, so breaking here is safe: the
|
|
615
|
-
// envelope stays queued for the replay that fills the hole.
|
|
616
|
-
const verdict = this.cursor.advance(sessionId, next.seq);
|
|
617
|
-
if (verdict === "gap") break;
|
|
618
|
-
held.shift();
|
|
619
|
-
if (verdict === "reset") {
|
|
620
|
-
// Defense-in-depth only (see the reset arm in handleEnvelope):
|
|
621
|
-
// drop the queue, deliver the fresh frame, resync for its tail.
|
|
622
|
-
this.reportEventGap(
|
|
623
|
-
sessionId,
|
|
624
|
-
"log_reset",
|
|
625
|
-
new EventGapError(`session ${sessionId} log reset`),
|
|
626
|
-
);
|
|
627
|
-
held.length = 0;
|
|
628
|
-
this.deliver(next);
|
|
629
|
-
// Let any replay owning this drain release its transport first.
|
|
630
|
-
queueMicrotask(() => void this.syncSession(sessionId));
|
|
631
|
-
break;
|
|
632
|
-
}
|
|
633
|
-
if (verdict !== "duplicate") this.deliver(next);
|
|
634
|
-
}
|
|
635
|
-
if (!held.length) this.heldBack.delete(sessionId);
|
|
750
|
+
private joinAt(handle: Handle, headSeq: number, follow: Follow): void {
|
|
751
|
+
this.streams.joined(handle.sessionId, headSeq, follow);
|
|
752
|
+
handle.point = { headSeq, position: this.streams.position };
|
|
753
|
+
handle.catchingUp();
|
|
636
754
|
}
|
|
637
755
|
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
!transport ||
|
|
642
|
-
this.awaitingHandshake ||
|
|
643
|
-
this.closed ||
|
|
644
|
-
this.syncing.get(sessionId) === transport
|
|
645
|
-
)
|
|
646
|
-
return;
|
|
647
|
-
this.syncing.set(sessionId, transport);
|
|
648
|
-
const isCurrent = () => !this.closed && !this.awaitingHandshake && this.transport === transport;
|
|
756
|
+
/** The kept events from `fromSeq` through `headSeq`, replayed into the handle before what followed its point. */
|
|
757
|
+
private async catchUp(handle: Handle, fromSeq: number, headSeq: number): Promise<void> {
|
|
758
|
+
let replayed: DecodedWireEventEnvelope[];
|
|
649
759
|
try {
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
if (!isCurrent()) return;
|
|
659
|
-
for (const envelope of replayed.events) this.holdBack(sessionId, envelope);
|
|
660
|
-
this.drainHeld(sessionId);
|
|
661
|
-
const missingFrom = this.cursor.last(sessionId) + 1;
|
|
662
|
-
if (replayed.firstAvailableSeq !== null && replayed.firstAvailableSeq > missingFrom) {
|
|
663
|
-
// The prefix was evicted — the gap is unfillable. Fail the turn
|
|
664
|
-
// handle (its part reconstruction is broken) and jump forward.
|
|
665
|
-
this.reportEventGap(
|
|
666
|
-
sessionId,
|
|
667
|
-
"evicted",
|
|
668
|
-
new EventGapError(
|
|
669
|
-
`events ${missingFrom}..${replayed.firstAvailableSeq - 1} for session ${sessionId} were evicted`,
|
|
670
|
-
),
|
|
671
|
-
);
|
|
672
|
-
this.cursor.seek(sessionId, replayed.firstAvailableSeq - 1);
|
|
673
|
-
}
|
|
674
|
-
break;
|
|
675
|
-
} catch (err) {
|
|
676
|
-
if (!isCurrent()) return;
|
|
677
|
-
if (err instanceof WireRequestError && err.code === WIRE_ERROR_CODES.unknownSession) {
|
|
678
|
-
this.reportEventGap(
|
|
679
|
-
sessionId,
|
|
680
|
-
"session_missing",
|
|
681
|
-
new EventGapError(`session ${sessionId} is unknown to the server (restarted?)`),
|
|
682
|
-
);
|
|
683
|
-
this.cursor.reset(sessionId);
|
|
684
|
-
this.heldBack.delete(sessionId);
|
|
685
|
-
break;
|
|
686
|
-
}
|
|
687
|
-
if (!this.transport || this.closed) break; // reconnect performs a fresh resync
|
|
688
|
-
if (attempt === maxAttempts) {
|
|
689
|
-
this.reportEventGap(
|
|
690
|
-
sessionId,
|
|
691
|
-
"replay_failed",
|
|
692
|
-
new EventGapError(
|
|
693
|
-
`failed to replay events from ${fromSeq} for session ${sessionId} after ${maxAttempts} attempts`,
|
|
694
|
-
),
|
|
695
|
-
);
|
|
696
|
-
this.heldBack.delete(sessionId);
|
|
697
|
-
break;
|
|
698
|
-
}
|
|
699
|
-
await sleep(retryDelay);
|
|
700
|
-
}
|
|
701
|
-
}
|
|
702
|
-
} finally {
|
|
703
|
-
if (this.syncing.get(sessionId) === transport) this.syncing.delete(sessionId);
|
|
760
|
+
replayed = await this.replayAll(handle.sessionId, fromSeq, headSeq);
|
|
761
|
+
} catch (error) {
|
|
762
|
+
handle.fail(
|
|
763
|
+
error instanceof WireRequestError && error.code === WIRE_ERROR_CODES.unknownSession
|
|
764
|
+
? new EventGapError(`session ${handle.sessionId} is unknown to the server (restarted?)`)
|
|
765
|
+
: error,
|
|
766
|
+
);
|
|
767
|
+
throw error;
|
|
704
768
|
}
|
|
705
|
-
|
|
769
|
+
handle.caughtUp(replayed.map((envelope) => envelope.event));
|
|
770
|
+
}
|
|
771
|
+
|
|
772
|
+
private handleOf(handle: Handle): TurnHandle {
|
|
773
|
+
const { sessionId, turnId } = handle;
|
|
774
|
+
return {
|
|
775
|
+
sessionId,
|
|
776
|
+
turnId,
|
|
777
|
+
events: handle.queue,
|
|
778
|
+
result: handle.result,
|
|
779
|
+
get catchUp() {
|
|
780
|
+
return handle.catchUp;
|
|
781
|
+
},
|
|
782
|
+
steer: (input: AgentInput, steerId?: string) =>
|
|
783
|
+
this.steerTurn({ sessionId, turnId, input, ...(steerId !== undefined && { steerId }) }),
|
|
784
|
+
};
|
|
706
785
|
}
|
|
707
786
|
|
|
708
|
-
/**
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
787
|
+
/**
|
|
788
|
+
* Subscribe to the session again (a redial, an overflow), for the turn of
|
|
789
|
+
* this client's it runs or, without one, watching it; what it missed heals
|
|
790
|
+
* by replay. A turn the answer says runs no more ended by its `headSeq`:
|
|
791
|
+
* the session is followed through there, then let go. A refusal is the
|
|
792
|
+
* server's answer: the session is followed no more.
|
|
793
|
+
*/
|
|
794
|
+
private async resubscribe(sessionId: string, turnId?: string): Promise<void> {
|
|
795
|
+
try {
|
|
796
|
+
await this.subscribeTo({ sessionId, ...(turnId !== undefined && { turnId }) }, (ack) => {
|
|
797
|
+
if (turnId === undefined) this.streams.joined(sessionId, ack.headSeq, "watch");
|
|
798
|
+
else if (ack.activeTurn?.turnId === turnId)
|
|
799
|
+
this.streams.joined(sessionId, ack.headSeq, { turnId });
|
|
800
|
+
// @ref LLP 0008#client-api — followed through the head, then let go
|
|
801
|
+
else this.streams.turnEnded(sessionId, turnId, ack.headSeq);
|
|
802
|
+
});
|
|
803
|
+
} catch (error) {
|
|
804
|
+
// Any other failure: a dropped connection subscribes again once redialed.
|
|
805
|
+
if (!(error instanceof WireRequestError)) return;
|
|
806
|
+
if (error.code === WIRE_ERROR_CODES.unknownSession) this.streams.missing(sessionId);
|
|
807
|
+
else this.streams.refused(sessionId, error.message);
|
|
725
808
|
}
|
|
726
809
|
}
|
|
727
810
|
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
for (const handler of [...this.gapHandlers]) {
|
|
735
|
-
try {
|
|
736
|
-
handler({ sessionId, reason, error });
|
|
737
|
-
} catch {
|
|
738
|
-
// A consumer callback cannot interrupt replay or other subscribers.
|
|
739
|
-
}
|
|
811
|
+
/** Every session again; refused, the client follows every session no more. */
|
|
812
|
+
private async resubscribeAll(): Promise<void> {
|
|
813
|
+
try {
|
|
814
|
+
await this.requestAll();
|
|
815
|
+
} catch (error) {
|
|
816
|
+
if (error instanceof WireRequestError) this.streams.refusedAll(error.message);
|
|
740
817
|
}
|
|
741
818
|
}
|
|
742
819
|
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
this.
|
|
747
|
-
|
|
748
|
-
|
|
820
|
+
// @ref LLP 0008#what-requests-do-to-subscriptions — a redial subscribes the running turns first
|
|
821
|
+
/** After a redial: every session followed, from what the client holds, pipelined. */
|
|
822
|
+
private subscribeAgain(): void {
|
|
823
|
+
for (const { sessionId, turnId } of this.streams.resubscriptions())
|
|
824
|
+
void this.resubscribe(sessionId, turnId);
|
|
825
|
+
if (this.streams.followsAll) void this.resubscribeAll();
|
|
749
826
|
}
|
|
750
827
|
|
|
751
|
-
|
|
752
|
-
|
|
828
|
+
/** `session/subscribeAll`, sent now. */
|
|
829
|
+
private requestAll(): Promise<unknown> {
|
|
830
|
+
return this.connection.request(WIRE_METHODS.sessionSubscribeAll, {}, ({ sessions }) =>
|
|
831
|
+
this.streams.joinedAll(sessions),
|
|
832
|
+
);
|
|
753
833
|
}
|
|
754
834
|
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
this.
|
|
761
|
-
}
|
|
762
|
-
|
|
763
|
-
// ---- outbound ------------------------------------------------------------
|
|
764
|
-
|
|
765
|
-
private request<T>(method: string, params: unknown, schema: ResultSchema<T>): Promise<T> {
|
|
766
|
-
if (this.closed) return Promise.reject(new ConnectionClosedError("client closed"));
|
|
767
|
-
const transport = this.transport;
|
|
768
|
-
if (!transport) return Promise.reject(new ConnectionClosedError("not connected"));
|
|
769
|
-
const id = this.nextId++;
|
|
770
|
-
const timeoutMs = this.options.requestTimeoutMs ?? 30_000;
|
|
771
|
-
return new Promise<T>((resolve, reject) => {
|
|
772
|
-
const timer = setTimeout(() => {
|
|
773
|
-
this.pending.delete(id);
|
|
774
|
-
reject(
|
|
775
|
-
new WireRequestError(WIRE_ERROR_CODES.internalError, `request timed out: ${method}`),
|
|
776
|
-
);
|
|
777
|
-
}, timeoutMs);
|
|
778
|
-
this.pending.set(id, {
|
|
779
|
-
resolve: (raw) => {
|
|
780
|
-
const parsed = schema.safeParse(raw);
|
|
781
|
-
if (parsed.success) resolve(parsed.data);
|
|
782
|
-
else
|
|
783
|
-
reject(
|
|
784
|
-
new WireRequestError(WIRE_ERROR_CODES.internalError, `malformed ${method} result`),
|
|
785
|
-
);
|
|
786
|
-
},
|
|
787
|
-
reject,
|
|
788
|
-
timer,
|
|
789
|
-
});
|
|
790
|
-
try {
|
|
791
|
-
transport.send(encodeRequest(id, method, params));
|
|
792
|
-
} catch (error) {
|
|
793
|
-
clearTimeout(timer);
|
|
794
|
-
this.pending.delete(id);
|
|
795
|
-
reject(error);
|
|
796
|
-
}
|
|
797
|
-
});
|
|
835
|
+
/** `session/subscribe` for one session, `onAnswer` given its point the moment it arrives. */
|
|
836
|
+
private subscribeTo(
|
|
837
|
+
params: { sessionId: string; turnId?: string },
|
|
838
|
+
onAnswer: (point: SessionSubscribeResult) => void,
|
|
839
|
+
): Promise<SessionSubscribeResult> {
|
|
840
|
+
return this.connection.request(WIRE_METHODS.sessionSubscribe, params, onAnswer);
|
|
798
841
|
}
|
|
799
842
|
}
|
|
800
|
-
|
|
801
|
-
function sleep(ms: number): Promise<void> {
|
|
802
|
-
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
803
|
-
}
|