@qlairoslabs/acp-client 0.12.0 → 0.13.0-alpha.1
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-consumer-fixes.md +17 -3
- package/CHANGELOG-controller-interface.md +258 -0
- package/CHANGELOG-metering.md +29 -0
- package/CHANGELOG-operation-lifecycle.md +15 -1
- package/CONSUMER_GUIDE.md +847 -7
- package/MIGRATION-0.10-to-0.12.md +4 -1
- package/MIGRATION-0.12-to-0.13.md +275 -0
- package/README.md +17 -16
- package/package.json +4 -2
- package/src/acp-client-handlers.ts +48 -0
- package/src/activity.ts +3 -0
- package/src/adapters.ts +30 -0
- package/src/agent/adoption/provider-attachment.ts +33 -0
- package/src/agent/adoption/provider-session-delete.ts +1 -1
- package/src/agent/adoption/provider-sessions.ts +3 -2
- package/src/agent/append-assistant-message.ts +91 -0
- package/src/agent/config.ts +54 -6
- package/src/agent/conversation-manager.ts +94 -158
- package/src/agent/manager-settings.ts +1 -0
- package/src/agent/native-channel-lifecycle.ts +111 -0
- package/src/agent/persisted-lifecycle.ts +44 -4
- package/src/agent/persistence-policy/context.ts +13 -0
- package/src/agent/persistence-policy/resolver.ts +7 -0
- package/src/agent/persistence-policy/types.ts +1 -0
- package/src/agent/record-capture.ts +1 -0
- package/src/agent-adapter-registry.ts +33 -7
- package/src/agent-conversation-contracts.ts +14 -1
- package/src/agent-conversation-errors.ts +8 -0
- package/src/agent-conversation-owner.ts +9 -4
- package/src/agent-session-config.ts +135 -0
- package/src/agent-session.ts +439 -934
- package/src/agent.ts +73 -0
- package/src/capabilities.ts +2 -0
- package/src/checkpointing/recovery.ts +10 -0
- package/src/cli/index.ts +3 -3
- package/src/cli/managed.ts +2 -2
- package/src/client.ts +68 -98
- package/src/connection-attempt.ts +95 -0
- package/src/controller-connection.ts +114 -0
- package/src/controller.ts +156 -148
- package/src/conversation-controller/capabilities.ts +88 -0
- package/src/conversation-controller/contracts.ts +208 -0
- package/src/conversation-controller/factory.ts +39 -0
- package/src/conversation-controller/guards.ts +98 -0
- package/src/conversation-controller/index.ts +20 -0
- package/src/conversation-startup.ts +35 -0
- package/src/conversation-turns/backend-turns.ts +373 -0
- package/src/conversation-turns/clear-operations.ts +37 -0
- package/src/conversation-turns/context.ts +135 -0
- package/src/conversation-turns/index.ts +4 -0
- package/src/conversation-turns/record-helpers.ts +75 -0
- package/src/conversation-turns/recording.ts +235 -0
- package/src/conversation-turns/run-turn.ts +367 -0
- package/src/conversation-turns/turn-run.ts +57 -0
- package/src/errors.ts +41 -0
- package/src/handoff-options.ts +81 -0
- package/src/identity/checkpoint-backed.ts +1 -1
- package/src/index.ts +58 -3
- package/src/internal-inference/coordination/manager.ts +11 -0
- package/src/internal-inference/runtime/harness-runtime.ts +4 -4
- package/src/launcher.ts +35 -2
- package/src/media/parts.ts +5 -0
- package/src/message-status.ts +83 -0
- package/src/metering/engine.ts +39 -64
- package/src/metering/measurement-provenance.ts +36 -0
- package/src/metering/native/durability.ts +115 -0
- package/src/metering/native/ledger.ts +104 -0
- package/src/metering/native/operation.ts +85 -0
- package/src/metering/native/projection.ts +114 -0
- package/src/metering/native/proof.ts +51 -0
- package/src/metering/native/requirements.ts +10 -0
- package/src/metering/native/retirement.ts +92 -0
- package/src/metering/native/source.ts +90 -0
- package/src/metering/record.ts +38 -1
- package/src/metering/state.ts +13 -3
- package/src/native/approvals.ts +169 -0
- package/src/native/capabilities.ts +17 -0
- package/src/native/channel-changes.ts +211 -0
- package/src/native/connection.ts +56 -0
- package/src/native/controller.ts +591 -0
- package/src/native/delegation-sender.ts +26 -0
- package/src/native/errors.ts +35 -0
- package/src/native/functions.ts +156 -0
- package/src/native/host-config.ts +59 -0
- package/src/native/host.ts +909 -0
- package/src/native/item-receipts.ts +15 -0
- package/src/native/metering.ts +12 -0
- package/src/native/records.ts +166 -0
- package/src/native/recovery.ts +186 -0
- package/src/native/reminders/fixtures/schema21.sqlite.gz +0 -0
- package/src/native/reminders/idle.ts +70 -0
- package/src/native/reminders/limits.ts +19 -0
- package/src/native/reminders/notifications.ts +29 -0
- package/src/native/reminders/reminders.test.ts +553 -0
- package/src/native/reminders/routine-config.typecheck.ts +13 -0
- package/src/native/reminders/routine-guard.ts +66 -0
- package/src/native/reminders/routine-schedule.ts +25 -0
- package/src/native/reminders/routine-wire.ts +21 -0
- package/src/native/reminders/schedule.ts +97 -0
- package/src/native/reminders/service.ts +280 -0
- package/src/native/reminders/supervisor.ts +91 -0
- package/src/native/reminders/sync.ts +301 -0
- package/src/native/reminders/wire.ts +197 -0
- package/src/native/reminders/wire.typecheck.ts +13 -0
- package/src/native/submission-withdrawal.ts +65 -0
- package/src/native/usage.ts +15 -0
- package/src/native/worker-delivery.ts +61 -0
- package/src/native-harness/.res/README.md +1 -0
- package/src/native-harness/.res/architecture/README.md +21 -0
- package/src/native-harness/.res/architecture/context-and-data.md +31 -0
- package/src/native-harness/.res/architecture/execution.md +42 -0
- package/src/native-harness/.res/architecture/overview.md +44 -0
- package/src/native-harness/.res/architecture/persistence.md +56 -0
- package/src/native-harness/.res/architecture/suspend-and-resume.md +45 -0
- package/src/native-harness/.res/architecture/technical-architecture.md +50 -0
- package/src/native-harness/.res/decisions/0001-basic-loop.md +11 -0
- package/src/native-harness/.res/decisions/0002-channel-input.md +17 -0
- package/src/native-harness/.res/decisions/0003-inference-inspection.md +14 -0
- package/src/native-harness/.res/decisions/0004-shared-context.md +21 -0
- package/src/native-harness/.res/decisions/0005-notice-inbox.md +20 -0
- package/src/native-harness/.res/decisions/0006-explicit-notices.md +12 -0
- package/src/native-harness/.res/decisions/0007-channel-output.md +12 -0
- package/src/native-harness/.res/decisions/0008-browser-client.md +16 -0
- package/src/native-harness/.res/decisions/0009-scripted-model.md +18 -0
- package/src/native-harness/.res/decisions/0010-inprocess-protocol.md +19 -0
- package/src/native-harness/.res/decisions/0011-stdio-transport.md +21 -0
- package/src/native-harness/.res/decisions/0012-session-storage-contract.md +37 -0
- package/src/native-harness/.res/decisions/0013-durable-delivery-contract.md +64 -0
- package/src/native-harness/.res/decisions/0014-channel-controls-contract.md +73 -0
- package/src/native-harness/.res/decisions/0015-session-file-contract.md +72 -0
- package/src/native-harness/.res/decisions/0016-configuration-permissions.md +159 -0
- package/src/native-harness/.res/decisions/0017-openrouter-managed-config.md +69 -0
- package/src/native-harness/.res/decisions/0018-tool-dispatch-evidence.md +78 -0
- package/src/native-harness/.res/decisions/0019-model-execution.md +188 -0
- package/src/native-harness/.res/decisions/0020-startup-channel-changes.md +71 -0
- package/src/native-harness/.res/decisions/0021-tools-and-host-callbacks.md +100 -0
- package/src/native-harness/.res/decisions/0022-operator-controls.md +115 -0
- package/src/native-harness/.res/decisions/0023-long-running-mock.md +82 -0
- package/src/native-harness/.res/decisions/0024-callback-policy-names.md +46 -0
- package/src/native-harness/.res/decisions/0025-external-notices.md +364 -0
- package/src/native-harness/.res/decisions/0026-bounded-context.md +81 -0
- package/src/native-harness/.res/decisions/0027-development-instructions.md +22 -0
- package/src/native-harness/.res/decisions/0028-debug-tools.md +16 -0
- package/src/native-harness/.res/decisions/0029-bounded-model-call-pages.md +37 -0
- package/src/native-harness/.res/decisions/0030-timeline-retrieval.md +145 -0
- package/src/native-harness/.res/decisions/0031-one-off-reminders.md +173 -0
- package/src/native-harness/.res/docs/11-15.md +148 -0
- package/src/native-harness/.res/docs/BUSINESS_RULES.md +111 -0
- package/src/native-harness/.res/docs/WORK.md +65 -0
- package/src/native-harness/.res/docs/_/11-16-persistence/LOG.md +312 -0
- package/src/native-harness/.res/docs/_/11-16-persistence/PLAN.md +77 -0
- package/src/native-harness/.res/docs/_/11-16-persistence/SPEC.md +313 -0
- package/src/native-harness/.res/docs/_/11-19-channel-controls/LOG.md +160 -0
- package/src/native-harness/.res/docs/_/11-19-channel-controls/PLAN.md +89 -0
- package/src/native-harness/.res/docs/_/11-19-channel-controls/SPEC.md +135 -0
- package/src/native-harness/.res/docs/_/11-19-config-permissions/LOG.md +143 -0
- package/src/native-harness/.res/docs/_/11-19-config-permissions/PLAN.md +93 -0
- package/src/native-harness/.res/docs/_/11-19-config-permissions/SPEC.md +181 -0
- package/src/native-harness/.res/docs/acp-client-integration/0001-DESIGN/11-20-LOG.md +433 -0
- package/src/native-harness/.res/docs/acp-client-integration/0001-DESIGN/PLAN.md +60 -0
- package/src/native-harness/.res/docs/acp-client-integration/0001-DESIGN/PROTOCOL.md +141 -0
- package/src/native-harness/.res/docs/acp-client-integration/0001-DESIGN/SPEC.md +168 -0
- package/src/native-harness/.res/docs/attachments/0001-DESIGN/SPEC.md +111 -0
- package/src/native-harness/.res/docs/bounded-context/0001-DESIGN/PLAN.md +129 -0
- package/src/native-harness/.res/docs/bounded-context/0001-DESIGN/SPEC.md +135 -0
- package/src/native-harness/.res/docs/channel-messaging/0001-DESIGN-external-notices/LOG.md +437 -0
- package/src/native-harness/.res/docs/channel-messaging/0001-DESIGN-external-notices/PLAN.md +127 -0
- package/src/native-harness/.res/docs/channel-messaging/0001-DESIGN-external-notices/SPEC.md +203 -0
- package/src/native-harness/.res/docs/long-running-mock/0001-DESIGN/LOG.md +292 -0
- package/src/native-harness/.res/docs/long-running-mock/0001-DESIGN/PLAN.md +91 -0
- package/src/native-harness/.res/docs/long-running-mock/0001-DESIGN/SPEC.md +187 -0
- package/src/native-harness/.res/docs/model-execution/0001-DESIGN/LOG.md +148 -0
- package/src/native-harness/.res/docs/model-execution/0001-DESIGN/PLAN.md +95 -0
- package/src/native-harness/.res/docs/model-execution/0001-DESIGN/SPEC.md +200 -0
- package/src/native-harness/.res/docs/model-execution/0002-AMENDMENT-accounting/SPEC.md +80 -0
- package/src/native-harness/.res/docs/operator-controls/0001-DESIGN/LOG.md +206 -0
- package/src/native-harness/.res/docs/operator-controls/0001-DESIGN/PLAN.md +106 -0
- package/src/native-harness/.res/docs/operator-controls/0001-DESIGN/SPEC.md +297 -0
- package/src/native-harness/.res/docs/persistence/0002-AMENDMENT-session-file/LOG.md +41 -0
- package/src/native-harness/.res/docs/persistence/0002-AMENDMENT-session-file/PLAN.md +36 -0
- package/src/native-harness/.res/docs/persistence/0002-AMENDMENT-session-file/SPEC.md +84 -0
- package/src/native-harness/.res/docs/shell-execution/0001-DESIGN/SPEC.md +147 -0
- package/src/native-harness/.res/docs/timeline-retrieval/0001-DESIGN/LOG.md +15 -0
- package/src/native-harness/.res/docs/timeline-retrieval/0001-DESIGN/PLAN.md +107 -0
- package/src/native-harness/.res/docs/timeline-retrieval/0001-DESIGN/SPEC.md +114 -0
- package/src/native-harness/.res/docs/todos/11-19-TODO-auto_review-approve.md +24 -0
- package/src/native-harness/.res/docs/todos/11-20-TODO-user-entered-tool-code.md +23 -0
- package/src/native-harness/.res/docs/todos/11-21-TODO-node-resource-coordination.md +103 -0
- package/src/native-harness/.res/docs/todos/11-22-TODO-harness-m1-sequence.md +50 -0
- package/src/native-harness/.res/docs/tools-and-callbacks/0001-DESIGN/LOG.md +337 -0
- package/src/native-harness/.res/docs/tools-and-callbacks/0001-DESIGN/PLAN.md +76 -0
- package/src/native-harness/.res/docs/tools-and-callbacks/0001-DESIGN/SPEC.md +204 -0
- package/src/native-harness/.res/docs/tools-and-callbacks/0002-AMENDMENT-callback-policy/LOG.md +73 -0
- package/src/native-harness/.res/docs/tools-and-callbacks/0002-AMENDMENT-callback-policy/SPEC.md +64 -0
- package/src/native-harness/.res/docs/tools-and-callbacks/0003-DESIGN-attachment-tools/SPEC.md +58 -0
- package/src/native-harness/.res/docs/tools-and-callbacks/0004-DESIGN-reminder-tools/LOG.md +45 -0
- package/src/native-harness/.res/docs/tools-and-callbacks/0004-DESIGN-reminder-tools/PLAN.md +108 -0
- package/src/native-harness/.res/docs/tools-and-callbacks/0004-DESIGN-reminder-tools/SPEC.md +169 -0
- package/src/native-harness/.res/docs/web-tools/0001-DESIGN/SPEC.md +128 -0
- package/src/native-harness/.res/marketing-draft.md +22 -0
- package/src/native-harness/.res/sandbox/1-simple-harness/README.md +285 -0
- package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-1.md +19 -0
- package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-10.md +138 -0
- package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-11.md +148 -0
- package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-12.md +172 -0
- package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-13.md +194 -0
- package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-14.md +233 -0
- package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-15.md +249 -0
- package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-16.md +265 -0
- package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-17.md +279 -0
- package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-18.md +293 -0
- package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-19.md +307 -0
- package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-2.md +29 -0
- package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-20.md +317 -0
- package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-21.md +327 -0
- package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-3.md +40 -0
- package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-4.md +59 -0
- package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-5.md +69 -0
- package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-6.md +79 -0
- package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-7.md +89 -0
- package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-8.md +102 -0
- package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-9.md +116 -0
- package/src/native-harness/.res/sandbox/1-simple-harness/conversation.jsonl +49 -0
- package/src/native-harness/.res/sandbox/1-simple-harness/doc.md +87 -0
- package/src/native-harness/.res/sandbox/1-simple-harness/harness.py +289 -0
- package/src/native-harness/.res/sandbox/1-simple-harness/local/harness copy 2.py +221 -0
- package/src/native-harness/.res/sandbox/1-simple-harness/local/harness copy 3.py +287 -0
- package/src/native-harness/.res/sandbox/1-simple-harness/local/harness copy.py +165 -0
- package/src/native-harness/.res/sandbox/1-simple-harness/state.json +22 -0
- package/src/native-harness/.res/sandbox/1-simple-harness/system-prompt.md +2 -0
- package/src/native-harness/AGENTS.md +70 -0
- package/src/native-harness/ARCHITECTURE.md +249 -0
- package/src/native-harness/README.md +726 -0
- package/src/native-harness/bun.lock +26 -0
- package/src/native-harness/package.json +24 -0
- package/src/native-harness/src/capabilities/context/build.ts +59 -0
- package/src/native-harness/src/capabilities/context/instructions/agent-demo.md +11 -0
- package/src/native-harness/src/capabilities/context/instructions/agent.md +5 -0
- package/src/native-harness/src/capabilities/context/instructions/harness.md +7 -0
- package/src/native-harness/src/capabilities/context/load-instructions.ts +22 -0
- package/src/native-harness/src/capabilities/context/session.ts +92 -0
- package/src/native-harness/src/capabilities/model/attempt.ts +27 -0
- package/src/native-harness/src/capabilities/model/openrouter-stream.ts +72 -0
- package/src/native-harness/src/capabilities/model/openrouter.ts +62 -0
- package/src/native-harness/src/capabilities/model/runner.ts +116 -0
- package/src/native-harness/src/capabilities/model/scenario-attempt.ts +50 -0
- package/src/native-harness/src/capabilities/model/scenario-file.ts +125 -0
- package/src/native-harness/src/capabilities/model/scripted-error.ts +6 -0
- package/src/native-harness/src/capabilities/model/scripted.ts +81 -0
- package/src/native-harness/src/capabilities/model/usage.ts +15 -0
- package/src/native-harness/src/capabilities/tools/builtin/current-time.ts +28 -0
- package/src/native-harness/src/capabilities/tools/builtin/notify-channel.ts +17 -0
- package/src/native-harness/src/capabilities/tools/debug/index.ts +13 -0
- package/src/native-harness/src/capabilities/tools/debug/result.ts +12 -0
- package/src/native-harness/src/capabilities/tools/debug/test-echo.ts +16 -0
- package/src/native-harness/src/capabilities/tools/debug/test-fail.ts +14 -0
- package/src/native-harness/src/capabilities/tools/debug/test-wait.ts +19 -0
- package/src/native-harness/src/capabilities/tools/reminders/index.ts +75 -0
- package/src/native-harness/src/capabilities/tools/runtime/catalog.ts +103 -0
- package/src/native-harness/src/capabilities/tools/runtime/functions.ts +130 -0
- package/src/native-harness/src/capabilities/tools/runtime/invocations.ts +48 -0
- package/src/native-harness/src/capabilities/tools/testing/echo.ts +18 -0
- package/src/native-harness/src/capabilities/tools/testing/record.ts +47 -0
- package/src/native-harness/src/capabilities/tools/timeline/index.ts +41 -0
- package/src/native-harness/src/client.ts +18 -0
- package/src/native-harness/src/contracts/agent.ts +105 -0
- package/src/native-harness/src/contracts/config-permissions.ts +47 -0
- package/src/native-harness/src/contracts/context.ts +112 -0
- package/src/native-harness/src/contracts/events.ts +47 -0
- package/src/native-harness/src/contracts/input-limits.ts +7 -0
- package/src/native-harness/src/contracts/inspection.ts +22 -0
- package/src/native-harness/src/contracts/loop.ts +31 -0
- package/src/native-harness/src/contracts/model-execution.ts +83 -0
- package/src/native-harness/src/contracts/operator-controls.ts +106 -0
- package/src/native-harness/src/contracts/output.ts +13 -0
- package/src/native-harness/src/contracts/protocol.ts +320 -0
- package/src/native-harness/src/contracts/reminders.ts +132 -0
- package/src/native-harness/src/contracts/scenario.ts +42 -0
- package/src/native-harness/src/contracts/session-file.ts +19 -0
- package/src/native-harness/src/contracts/session.ts +184 -0
- package/src/native-harness/src/contracts/storage.ts +596 -0
- package/src/native-harness/src/contracts/timeline-retrieval.ts +40 -0
- package/src/native-harness/src/contracts/tools.ts +133 -0
- package/src/native-harness/src/governance/permissions/gate.ts +129 -0
- package/src/native-harness/src/governance/policy/resolve.ts +32 -0
- package/src/native-harness/src/index.ts +318 -0
- package/src/native-harness/src/interfaces/cli/chat.ts +83 -0
- package/src/native-harness/src/interfaces/cli/development.ts +145 -0
- package/src/native-harness/src/interfaces/cli/format-reply.ts +27 -0
- package/src/native-harness/src/interfaces/cli/listeners.ts +74 -0
- package/src/native-harness/src/interfaces/cli/main.ts +47 -0
- package/src/native-harness/src/interfaces/cli/scenario.ts +37 -0
- package/src/native-harness/src/interfaces/cli/serve.ts +21 -0
- package/src/native-harness/src/interfaces/cli/start.ts +106 -0
- package/src/native-harness/src/interfaces/client.ts +225 -0
- package/src/native-harness/src/interfaces/inspector/index.html +283 -0
- package/src/native-harness/src/interfaces/inspector/observe.ts +35 -0
- package/src/native-harness/src/interfaces/inspector/server.ts +56 -0
- package/src/native-harness/src/interfaces/inspector/state.ts +92 -0
- package/src/native-harness/src/interfaces/protocol/channel-changes.ts +153 -0
- package/src/native-harness/src/interfaces/protocol/framing.ts +47 -0
- package/src/native-harness/src/interfaces/protocol/functions.ts +87 -0
- package/src/native-harness/src/interfaces/protocol/in-process.ts +67 -0
- package/src/native-harness/src/interfaces/protocol/live-queue.ts +55 -0
- package/src/native-harness/src/interfaces/protocol/load.ts +60 -0
- package/src/native-harness/src/interfaces/protocol/local.ts +120 -0
- package/src/native-harness/src/interfaces/protocol/operator.ts +120 -0
- package/src/native-harness/src/interfaces/protocol/output.ts +59 -0
- package/src/native-harness/src/interfaces/protocol/server.ts +517 -0
- package/src/native-harness/src/interfaces/protocol/stdio-client.ts +119 -0
- package/src/native-harness/src/interfaces/protocol/stdio-server.ts +82 -0
- package/src/native-harness/src/interfaces/protocol/subscribe.ts +41 -0
- package/src/native-harness/src/interfaces/protocol/transfers.ts +146 -0
- package/src/native-harness/src/interfaces/web-chat/index.html +94 -0
- package/src/native-harness/src/interfaces/web-chat/server.ts +34 -0
- package/src/native-harness/src/kernel/channels.ts +405 -0
- package/src/native-harness/src/kernel/loop.ts +45 -0
- package/src/native-harness/src/runtime/config/development.ts +23 -0
- package/src/native-harness/src/runtime/config/managed.ts +2 -0
- package/src/native-harness/src/runtime/config/paths.ts +27 -0
- package/src/native-harness/src/runtime/reminder-timer.ts +42 -0
- package/src/native-harness/src/runtime/reminder-validation.ts +104 -0
- package/src/native-harness/src/runtime/reminders.ts +66 -0
- package/src/native-harness/src/state/storage/archive.ts +239 -0
- package/src/native-harness/src/state/storage/engine.ts +1013 -0
- package/src/native-harness/src/state/storage/inference.ts +124 -0
- package/src/native-harness/src/state/storage/memory-reminders.ts +78 -0
- package/src/native-harness/src/state/storage/memory.ts +341 -0
- package/src/native-harness/src/state/storage/policy-metadata.ts +32 -0
- package/src/native-harness/src/state/storage/reminder-archive.ts +63 -0
- package/src/native-harness/src/state/storage/reminder-change.ts +92 -0
- package/src/native-harness/src/state/storage/reminder-tables.ts +27 -0
- package/src/native-harness/src/state/storage/retrieval.ts +122 -0
- package/src/native-harness/src/state/storage/script-identity.ts +7 -0
- package/src/native-harness/src/state/storage/session-file.ts +85 -0
- package/src/native-harness/src/state/storage/sqlite-reminders.ts +134 -0
- package/src/native-harness/src/state/storage/sqlite.ts +625 -0
- package/src/native-harness/src/testing.ts +190 -0
- package/src/native-harness/src/util/config.ts +58 -0
- package/src/native-harness/src/util/json.ts +41 -0
- package/src/native-harness/src/util/message-position.ts +3 -0
- package/src/native-harness/src/util/reminder-schedule.ts +44 -0
- package/src/native-harness/src/util/schema.ts +40 -0
- package/src/native-harness/src/util/timeline-text.ts +72 -0
- package/src/native-harness/src/util/timeline-tool-names.ts +3 -0
- package/src/native-harness/test/agent.test.ts +130 -0
- package/src/native-harness/test/approval-gate.test.ts +255 -0
- package/src/native-harness/test/approval-protocol.test.ts +94 -0
- package/src/native-harness/test/boundaries.test.ts +18 -0
- package/src/native-harness/test/channel-controls.test.ts +322 -0
- package/src/native-harness/test/client-abort.test.ts +136 -0
- package/src/native-harness/test/config-policy.test.ts +301 -0
- package/src/native-harness/test/conformance/storage.ts +1141 -0
- package/src/native-harness/test/context.test.ts +177 -0
- package/src/native-harness/test/crash.test.ts +187 -0
- package/src/native-harness/test/debug-tools.test.ts +67 -0
- package/src/native-harness/test/development-cli.test.ts +67 -0
- package/src/native-harness/test/development.test.ts +171 -0
- package/src/native-harness/test/dispatch-evidence.test.ts +58 -0
- package/src/native-harness/test/e2e.test.ts +186 -0
- package/src/native-harness/test/external-notices.test.ts +101 -0
- package/src/native-harness/test/fixtures/approval-e2e.jsonl +4 -0
- package/src/native-harness/test/fixtures/crash-scenario.jsonl +5 -0
- package/src/native-harness/test/fixtures/development-lifecycle.jsonl +10 -0
- package/src/native-harness/test/fixtures/echo-scenario.jsonl +3 -0
- package/src/native-harness/test/fixtures/format2-schema.ts +27 -0
- package/src/native-harness/test/fixtures/format3-schema.ts +16 -0
- package/src/native-harness/test/fixtures/hold-scenario.jsonl +4 -0
- package/src/native-harness/test/fixtures/model-e2e.jsonl +3 -0
- package/src/native-harness/test/fixtures/storage-owner.ts +20 -0
- package/src/native-harness/test/format-reply.test.ts +10 -0
- package/src/native-harness/test/helpers/operator-view.ts +41 -0
- package/src/native-harness/test/helpers/scenario.ts +9 -0
- package/src/native-harness/test/helpers/session.ts +38 -0
- package/src/native-harness/test/helpers/stdio.ts +46 -0
- package/src/native-harness/test/inspector.test.ts +77 -0
- package/src/native-harness/test/listeners.test.ts +49 -0
- package/src/native-harness/test/loop.test.ts +88 -0
- package/src/native-harness/test/model-channel-double.test.ts +87 -0
- package/src/native-harness/test/model-crash.test.ts +62 -0
- package/src/native-harness/test/model-delivery-regressions.test.ts +55 -0
- package/src/native-harness/test/model-e2e.test.ts +71 -0
- package/src/native-harness/test/model-final-filter.test.ts +30 -0
- package/src/native-harness/test/model-large-reply.test.ts +28 -0
- package/src/native-harness/test/model-live-queue.test.ts +33 -0
- package/src/native-harness/test/model-runner.test.ts +117 -0
- package/src/native-harness/test/model-storage.test.ts +99 -0
- package/src/native-harness/test/model-stream-adapter.test.ts +26 -0
- package/src/native-harness/test/model-stream-public.test.ts +77 -0
- package/src/native-harness/test/notices.test.ts +64 -0
- package/src/native-harness/test/openrouter-config.test.ts +218 -0
- package/src/native-harness/test/operator-controls.test.ts +132 -0
- package/src/native-harness/test/output.test.ts +44 -0
- package/src/native-harness/test/owner-config.test.ts +64 -0
- package/src/native-harness/test/permissions-e2e.test.ts +68 -0
- package/src/native-harness/test/persistence.test.ts +236 -0
- package/src/native-harness/test/portability.test.ts +337 -0
- package/src/native-harness/test/protocol.test.ts +160 -0
- package/src/native-harness/test/reminder-clock.test.ts +61 -0
- package/src/native-harness/test/reminder-stdio.test.ts +140 -0
- package/src/native-harness/test/reminder-storage.test.ts +124 -0
- package/src/native-harness/test/scenario-jsonl.test.ts +112 -0
- package/src/native-harness/test/scripted.test.ts +92 -0
- package/src/native-harness/test/session-file.test.ts +51 -0
- package/src/native-harness/test/startup-channel-changes.test.ts +256 -0
- package/src/native-harness/test/stdio.test.ts +132 -0
- package/src/native-harness/test/storage-memory.test.ts +4 -0
- package/src/native-harness/test/storage-sqlite.test.ts +420 -0
- package/src/native-harness/test/suspension.test.ts +430 -0
- package/src/native-harness/test/timeline-retrieval.test.ts +211 -0
- package/src/native-harness/test/tools-catalog.test.ts +468 -0
- package/src/native-harness/test/tools-protocol.test.ts +418 -0
- package/src/native-harness/test/transfers.test.ts +94 -0
- package/src/native-harness/test/web-chat.test.ts +35 -0
- package/src/native-harness/tsconfig.json +20 -0
- package/src/observation/agent-event-bus.ts +44 -18
- package/src/observation/index.ts +1 -0
- package/src/operations/contracts.ts +11 -3
- package/src/operations/errors.ts +5 -0
- package/src/operations/stale-policy.ts +1 -1
- package/src/operations/supervisor.ts +34 -10
- package/src/orchestrator/acp-delivery.ts +68 -0
- package/src/orchestrator/assignments.ts +27 -0
- package/src/orchestrator/busy.ts +15 -0
- package/src/orchestrator/config.ts +41 -0
- package/src/orchestrator/consumer-api.ts +24 -0
- package/src/orchestrator/contracts.ts +71 -0
- package/src/orchestrator/durable-assignments.ts +111 -0
- package/src/orchestrator/native-delegation.ts +103 -0
- package/src/orchestrator/orchestrator.ts +318 -0
- package/src/orchestrator/permissions.ts +25 -0
- package/src/orchestrator/policy.ts +44 -0
- package/src/orchestrator/pool.ts +123 -0
- package/src/orchestrator/tool-descriptions.ts +11 -0
- package/src/permissions/acp.ts +18 -0
- package/src/permissions/arrival.ts +13 -0
- package/src/permissions/consumer-config.ts +19 -0
- package/src/permissions/conversation.ts +280 -0
- package/src/permissions/matcher.ts +85 -0
- package/src/permissions/outcome.ts +15 -0
- package/src/permissions/policy.ts +81 -0
- package/src/permissions/request.ts +26 -0
- package/src/persistence/activity-projection.ts +1 -0
- package/src/persistence/backend.ts +34 -1
- package/src/persistence/channels.ts +23 -0
- package/src/persistence/index.ts +4 -0
- package/src/persistence/protocol-method.ts +5 -0
- package/src/persistence/reminder-wake.ts +49 -0
- package/src/persistence/reminders.ts +385 -0
- package/src/persistence/sqlite-backend.ts +84 -6
- package/src/persistence/sqlite-channels.ts +65 -0
- package/src/persistence/sqlite-reminders.ts +85 -0
- package/src/persistence/sqlite-schema.ts +108 -3
- package/src/persistence/sqlite-worker-delivery.ts +374 -0
- package/src/persistence/sqlite-workers.ts +59 -0
- package/src/persistence/worker-delivery-contract.ts +104 -0
- package/src/persistence/worker-terminal-reason.ts +29 -0
- package/src/persistence/workers.ts +19 -0
- package/src/provider-notices.ts +137 -0
- package/src/repl/commands.ts +2 -2
- package/src/steering/combine.ts +3 -1
- package/src/steering/contracts.ts +11 -2
- package/src/steering/controller.ts +3 -2
- package/src/transport/acp-transport.ts +24 -14
- package/src/types.ts +15 -12
- package/src/updates.ts +14 -2
- package/src/wire-log.ts +1 -1
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { ConnectionCancelledError } from "./errors.ts";
|
|
2
|
+
|
|
3
|
+
export class ConnectionAttempt<Progress = never> {
|
|
4
|
+
private readonly abort = new AbortController();
|
|
5
|
+
private readonly observers = new Set<(event: Progress) => void>();
|
|
6
|
+
private readonly progress: Progress[] = [];
|
|
7
|
+
private cleanup?: () => Promise<void>;
|
|
8
|
+
private cancellation?: ConnectionCancelledError;
|
|
9
|
+
|
|
10
|
+
get signal(): AbortSignal { return this.abort.signal; }
|
|
11
|
+
|
|
12
|
+
throwIfCancelled(): void {
|
|
13
|
+
if (this.cancellation) throw this.cancellation;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
own(cleanup: () => Promise<void>): void {
|
|
17
|
+
this.throwIfCancelled();
|
|
18
|
+
this.cleanup = cleanup;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
async cancel(): Promise<void> {
|
|
22
|
+
this.cancellation ??= new ConnectionCancelledError();
|
|
23
|
+
this.abort.abort(this.cancellation);
|
|
24
|
+
await this.cleanup?.();
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
observe(observer: (event: Progress) => void): void {
|
|
28
|
+
this.observers.add(observer);
|
|
29
|
+
for (const event of this.progress) {
|
|
30
|
+
if (this.signal.aborted) break;
|
|
31
|
+
observer(event);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
report(event: Progress): void {
|
|
36
|
+
this.progress.push(event);
|
|
37
|
+
for (const observer of [...this.observers]) {
|
|
38
|
+
if (this.signal.aborted) break;
|
|
39
|
+
observer(event);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Equivalent attempts share readiness. An incompatible request cancels and drains the candidate first. */
|
|
45
|
+
export class ConnectionAttempts<T, Progress = never> {
|
|
46
|
+
private active?: { key: string; attempt: ConnectionAttempt<Progress>; result: Promise<T> };
|
|
47
|
+
private teardown = Promise.resolve();
|
|
48
|
+
|
|
49
|
+
run(key: string, work: (attempt: ConnectionAttempt<Progress>) => Promise<T>, observer?: (event: Progress) => void): Promise<T> {
|
|
50
|
+
const active = this.active;
|
|
51
|
+
if (active?.key === key) {
|
|
52
|
+
if (observer) active.attempt.observe(observer);
|
|
53
|
+
return active.result;
|
|
54
|
+
}
|
|
55
|
+
void this.cancel().catch(() => undefined);
|
|
56
|
+
const prior = this.teardown;
|
|
57
|
+
const attempt = new ConnectionAttempt<Progress>();
|
|
58
|
+
if (observer) attempt.observe(observer);
|
|
59
|
+
const result = prior.then(() => {
|
|
60
|
+
attempt.throwIfCancelled();
|
|
61
|
+
return work(attempt);
|
|
62
|
+
}).then(value => {
|
|
63
|
+
attempt.throwIfCancelled();
|
|
64
|
+
return value;
|
|
65
|
+
}, error => {
|
|
66
|
+
// A stopped attempt owns its public outcome, regardless of how the leaf unwinds.
|
|
67
|
+
attempt.throwIfCancelled();
|
|
68
|
+
throw error;
|
|
69
|
+
}).finally(() => {
|
|
70
|
+
if (this.active?.attempt === attempt) this.active = undefined;
|
|
71
|
+
});
|
|
72
|
+
this.active = { key, attempt, result };
|
|
73
|
+
return result;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
cancel(): Promise<void> {
|
|
77
|
+
const active = this.active;
|
|
78
|
+
if (!active) return this.teardown;
|
|
79
|
+
this.active = undefined;
|
|
80
|
+
const teardown = Promise.all([this.teardown, active.attempt.cancel()]).then(() => undefined);
|
|
81
|
+
this.teardown = teardown.catch(() => undefined);
|
|
82
|
+
return teardown;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** Observe the original promise even if its result loses the race. */
|
|
87
|
+
export function untilAborted<T>(work: Promise<T>, signal: AbortSignal): Promise<T> {
|
|
88
|
+
return new Promise((resolve, reject) => {
|
|
89
|
+
const abort = () => reject(signal.reason);
|
|
90
|
+
if (signal.aborted) abort();
|
|
91
|
+
else signal.addEventListener("abort", abort, { once: true });
|
|
92
|
+
const release = () => signal.removeEventListener("abort", abort);
|
|
93
|
+
void work.then(value => { release(); resolve(value); }, error => { release(); reject(error); });
|
|
94
|
+
});
|
|
95
|
+
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { AcpClient, type ConnectResult } from "./client.ts";
|
|
2
|
+
import type { AgentClient } from "./agent-client.ts";
|
|
3
|
+
import type { ConnectionAttempt } from "./connection-attempt.ts";
|
|
4
|
+
import type { AgentControllerConfig, AgentMode, ControllerStartEvent } from "./controller.ts";
|
|
5
|
+
import { ConnectionCancelledError, ProcessCrashError } from "./errors.ts";
|
|
6
|
+
|
|
7
|
+
export interface ReadyConnection {
|
|
8
|
+
client: AgentClient;
|
|
9
|
+
acp: AcpClient | null;
|
|
10
|
+
result: ConnectResult;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Bring up a fresh leaf for the configured adapter in `mode`. normal → AcpClient
|
|
15
|
+
* (connect + new/load session); degraded → PtyClient (spawn interactive CLI). PtyClient
|
|
16
|
+
* is dynamically imported so the normal-mode path never loads the degraded subpath.
|
|
17
|
+
*/
|
|
18
|
+
export async function bringUp(
|
|
19
|
+
config: AgentControllerConfig,
|
|
20
|
+
attempt: ConnectionAttempt<ControllerStartEvent>,
|
|
21
|
+
mode: AgentMode,
|
|
22
|
+
sessionId?: string,
|
|
23
|
+
loadFailure = config.sessionLoadFailure,
|
|
24
|
+
): Promise<ReadyConnection> {
|
|
25
|
+
const adapter = config.adapter;
|
|
26
|
+
if (mode === "degraded") {
|
|
27
|
+
const { PtyClient } = await import("./degraded/index.ts");
|
|
28
|
+
attempt.throwIfCancelled();
|
|
29
|
+
const client = new PtyClient({
|
|
30
|
+
adapter,
|
|
31
|
+
launcher: config.launcher,
|
|
32
|
+
execPrefix: config.execPrefix,
|
|
33
|
+
env: config.env,
|
|
34
|
+
cwd: config.cwd,
|
|
35
|
+
logger: config.logger,
|
|
36
|
+
});
|
|
37
|
+
try {
|
|
38
|
+
await client.start();
|
|
39
|
+
attempt.throwIfCancelled();
|
|
40
|
+
} catch (err) {
|
|
41
|
+
await client.stop();
|
|
42
|
+
throw err;
|
|
43
|
+
}
|
|
44
|
+
return { client, acp: null, result: { sessionId: "", resumed: false } };
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const client = new AcpClient({
|
|
48
|
+
adapter,
|
|
49
|
+
launcher: config.launcher,
|
|
50
|
+
execPrefix: config.execPrefix,
|
|
51
|
+
env: config.env,
|
|
52
|
+
cwd: config.cwd,
|
|
53
|
+
additionalDirectories: config.additionalDirectories,
|
|
54
|
+
sessionId,
|
|
55
|
+
sessionLoadFailure: loadFailure,
|
|
56
|
+
permissions: config.permissions, permissionRule: config.permissionRule,
|
|
57
|
+
clientInfo: config.clientInfo,
|
|
58
|
+
clientCapabilities: config.clientCapabilities,
|
|
59
|
+
clientHandlers: config.clientHandlers,
|
|
60
|
+
logger: config.logger,
|
|
61
|
+
wire: config.wire,
|
|
62
|
+
});
|
|
63
|
+
// connect() is fatal (no connection = nothing to do). startSession() is NOT: an
|
|
64
|
+
// unauthenticated agent may refuse session/new, but we keep the connection up so the
|
|
65
|
+
// user can /login and then create a session. (Auth state is opaque — see SPEC.)
|
|
66
|
+
try {
|
|
67
|
+
attempt.own(() => client.stop());
|
|
68
|
+
const capabilities = await client.connect();
|
|
69
|
+
attempt.throwIfCancelled();
|
|
70
|
+
attempt.report({ stage: "initialized", capabilities });
|
|
71
|
+
} catch (err) {
|
|
72
|
+
await client.stop();
|
|
73
|
+
attempt.throwIfCancelled();
|
|
74
|
+
throw err;
|
|
75
|
+
}
|
|
76
|
+
let result: ConnectResult;
|
|
77
|
+
try {
|
|
78
|
+
result = await client.startSession({
|
|
79
|
+
mcpServers: config.mcpServers,
|
|
80
|
+
additionalDirectories: config.additionalDirectories,
|
|
81
|
+
loadFailure: loadFailure,
|
|
82
|
+
...(config.onSessionReplay ? { onReplayUpdate: config.onSessionReplay } : {}),
|
|
83
|
+
});
|
|
84
|
+
} catch (err) {
|
|
85
|
+
attempt.throwIfCancelled();
|
|
86
|
+
if (err instanceof ConnectionCancelledError || err instanceof ProcessCrashError ||
|
|
87
|
+
config.requireStartupSession || (sessionId && loadFailure === "error")) {
|
|
88
|
+
await client.stop();
|
|
89
|
+
throw err;
|
|
90
|
+
}
|
|
91
|
+
config.logger?.warn("session_start_failed", { error: String(err) });
|
|
92
|
+
result = { sessionId: "", resumed: false };
|
|
93
|
+
}
|
|
94
|
+
attempt.throwIfCancelled();
|
|
95
|
+
if (result.sessionId) {
|
|
96
|
+
attempt.report({
|
|
97
|
+
stage: "internal_session_acquired",
|
|
98
|
+
internalSessionId: result.sessionId,
|
|
99
|
+
disposition: result.resumed ? "resumed" : "created",
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
return { client, acp: client, result };
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export function observeStartProgress(
|
|
106
|
+
config: AgentControllerConfig,
|
|
107
|
+
listener: ((event: ControllerStartEvent) => void) | undefined,
|
|
108
|
+
): ((event: ControllerStartEvent) => void) | undefined {
|
|
109
|
+
if (!listener) return;
|
|
110
|
+
return event => {
|
|
111
|
+
try { listener(event); }
|
|
112
|
+
catch (error) { config.logger?.warn("controller_start_progress_listener_failed", { error: String(error) }); }
|
|
113
|
+
};
|
|
114
|
+
}
|
package/src/controller.ts
CHANGED
|
@@ -1,7 +1,16 @@
|
|
|
1
|
+
import { ownPermissions, type PermissionPolicyConfig } from "./permissions/policy.ts";
|
|
1
2
|
import { ForcedStopError } from "./operations/errors.ts";
|
|
2
|
-
import type { OperationLifecycleConfig, OutputListener } from "./operations/contracts.ts";
|
|
3
|
+
import type { OperationLifecycleConfig, OutputListener, SupervisedProvider } from "./operations/contracts.ts";
|
|
4
|
+
import type {
|
|
5
|
+
ConversationController,
|
|
6
|
+
PreparedTurn,
|
|
7
|
+
TurnContent,
|
|
8
|
+
TurnEvent,
|
|
9
|
+
TurnRun,
|
|
10
|
+
UsageListener,
|
|
11
|
+
} from "./conversation-controller/contracts.ts";
|
|
3
12
|
import {
|
|
4
|
-
AcpClient,
|
|
13
|
+
type AcpClient,
|
|
5
14
|
type AcpClientConfig,
|
|
6
15
|
type ConnectResult,
|
|
7
16
|
type SessionLoadFailure,
|
|
@@ -24,7 +33,10 @@ import type {
|
|
|
24
33
|
WireEntry,
|
|
25
34
|
} from "./types.ts";
|
|
26
35
|
import { NotConnectedError } from "./errors.ts";
|
|
36
|
+
import { conversationCapabilitiesOf, type ConversationCapabilities } from "./conversation-controller/capabilities.ts";
|
|
27
37
|
import type { ClientPrompt } from "./agent-client.ts";
|
|
38
|
+
import { ConnectionAttempts, type ConnectionAttempt } from "./connection-attempt.ts";
|
|
39
|
+
import { bringUp, observeStartProgress, type ReadyConnection } from "./controller-connection.ts";
|
|
28
40
|
|
|
29
41
|
const NO_AVAILABLE_COMMANDS: readonly InternalSessionCommand[] = Object.freeze([]);
|
|
30
42
|
|
|
@@ -42,7 +54,7 @@ type ObservableLeaf = AgentClient & {
|
|
|
42
54
|
subscribeOutput?(listener: OutputListener): () => void;
|
|
43
55
|
};
|
|
44
56
|
|
|
45
|
-
export interface AgentControllerConfig {
|
|
57
|
+
export interface AgentControllerConfig extends PermissionPolicyConfig {
|
|
46
58
|
operationLifecycle?: OperationLifecycleConfig;
|
|
47
59
|
/** The single adapter this controller drives. Switching adapter = a new AgentSession. */
|
|
48
60
|
adapter: Adapter;
|
|
@@ -62,11 +74,12 @@ export interface AgentControllerConfig {
|
|
|
62
74
|
sessionId?: string;
|
|
63
75
|
/** Policy when sessionId cannot be loaded. Default "new" for compatibility. */
|
|
64
76
|
sessionLoadFailure?: SessionLoadFailure;
|
|
77
|
+
/** @internal First use of a deferred Agent conversation must surface session creation errors. */
|
|
78
|
+
requireStartupSession?: boolean;
|
|
65
79
|
/** Receives the history a loaded `sessionId` replays (raw `session/update` notifications). */
|
|
66
80
|
onSessionReplay?: (notification: unknown) => void;
|
|
67
81
|
/** Default MCP servers bound at session creation/switch, unless a call overrides them. */
|
|
68
82
|
mcpServers?: schema.McpServer[];
|
|
69
|
-
defaultPermission?: "approve" | "cancel";
|
|
70
83
|
clientInfo?: AcpClientConfig["clientInfo"];
|
|
71
84
|
/** What we let the agent do through us (terminals, filesystem). All on by default. */
|
|
72
85
|
clientCapabilities?: AcpClientConfig["clientCapabilities"];
|
|
@@ -78,7 +91,7 @@ export interface AgentControllerConfig {
|
|
|
78
91
|
}
|
|
79
92
|
|
|
80
93
|
/** @internal Startup observations used by the managed AgentSession coordinator. */
|
|
81
|
-
export type
|
|
94
|
+
export type ControllerStartEvent =
|
|
82
95
|
| { readonly stage: "initialized"; readonly capabilities: Capabilities }
|
|
83
96
|
| {
|
|
84
97
|
readonly stage: "internal_session_acquired";
|
|
@@ -119,7 +132,7 @@ export interface SetConfigFromStringResult {
|
|
|
119
132
|
* mutable "current client" behavior such as adapter switching and config validation,
|
|
120
133
|
* so CLIs and app UIs do not have to reimplement it.
|
|
121
134
|
*/
|
|
122
|
-
export class
|
|
135
|
+
export class HarnessController implements AgentClient, ConversationController {
|
|
123
136
|
// `current` is the universal leaf (prompt/interrupt/stop) — an AcpClient in normal
|
|
124
137
|
// mode, a PtyClient in degraded mode. `acp` is the SAME object only in normal mode,
|
|
125
138
|
// typed for the rich ACP surface; it is null when degraded (that surface genuinely
|
|
@@ -128,6 +141,7 @@ export class AgentController implements AgentClient {
|
|
|
128
141
|
private readonly connectionListeners = new Set<() => void>();
|
|
129
142
|
private detachDisconnect: (() => void) | null = null;
|
|
130
143
|
private stopping: Promise<void> = Promise.resolve();
|
|
144
|
+
private readonly connections = new ConnectionAttempts<ConnectResult, ControllerStartEvent>();
|
|
131
145
|
private current: AgentClient | null = null;
|
|
132
146
|
private acp: AcpClient | null = null;
|
|
133
147
|
private mode: AgentMode;
|
|
@@ -139,10 +153,13 @@ export class AgentController implements AgentClient {
|
|
|
139
153
|
(update: unknown, sessionId: string | null) => void
|
|
140
154
|
>();
|
|
141
155
|
private readonly outputListeners = new Set<OutputListener>();
|
|
156
|
+
private readonly turnListeners = new Set<(event: TurnEvent) => void>();
|
|
157
|
+
/** Turns reported by `send` that have not been prepared or abandoned yet. */
|
|
158
|
+
private readonly reportedTurns = new Set<string>();
|
|
142
159
|
private detachOutput: (() => void) | null = null;
|
|
143
160
|
private detachUsageUpdates: (() => void) | null = null;
|
|
144
|
-
|
|
145
161
|
constructor(private readonly config: AgentControllerConfig) {
|
|
162
|
+
this.config = { ...config, permissions: ownPermissions(config.permissions) };
|
|
146
163
|
this.mode = config.mode ?? "normal";
|
|
147
164
|
}
|
|
148
165
|
|
|
@@ -165,18 +182,18 @@ export class AgentController implements AgentClient {
|
|
|
165
182
|
}
|
|
166
183
|
}
|
|
167
184
|
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
185
|
+
reconnect(sessionId: string): Promise<ConnectResult> {
|
|
186
|
+
return this.connections.run(`reconnect:${sessionId}`, async attempt => {
|
|
187
|
+
await this.stopCurrent();
|
|
188
|
+
attempt.throwIfCancelled();
|
|
189
|
+
const brought = await bringUp(this.config, attempt, this.mode, sessionId, "error");
|
|
190
|
+
attempt.throwIfCancelled();
|
|
191
|
+
if (!brought.result.resumed || brought.result.sessionId !== sessionId) {
|
|
192
|
+
await brought.client.stop();
|
|
193
|
+
throw new Error("provider did not load the requested session");
|
|
194
|
+
}
|
|
195
|
+
return this.installConnection(attempt, brought);
|
|
196
|
+
});
|
|
180
197
|
}
|
|
181
198
|
|
|
182
199
|
get cancelGraceMs(): number | undefined { return this.config.adapter.cancelGraceMs; }
|
|
@@ -208,6 +225,11 @@ export class AgentController implements AgentClient {
|
|
|
208
225
|
return this.requireAcp().capabilities;
|
|
209
226
|
}
|
|
210
227
|
|
|
228
|
+
/** What the backend offers, in neutral terms; `backend` is null before connecting and in PTY mode. */
|
|
229
|
+
get conversationCapabilities(): ConversationCapabilities {
|
|
230
|
+
return conversationCapabilitiesOf(this.acp);
|
|
231
|
+
}
|
|
232
|
+
|
|
211
233
|
get configOptions(): ConfigOptions {
|
|
212
234
|
return this.acp?.configOptions ?? new Map();
|
|
213
235
|
}
|
|
@@ -235,7 +257,7 @@ export class AgentController implements AgentClient {
|
|
|
235
257
|
*
|
|
236
258
|
* Degraded (pty) mode has no ACP notifications, so this simply never fires there.
|
|
237
259
|
*/
|
|
238
|
-
subscribeUsageUpdate(listener:
|
|
260
|
+
subscribeUsageUpdate(listener: UsageListener): () => void {
|
|
239
261
|
this.usageUpdateListeners.add(listener);
|
|
240
262
|
return () => this.usageUpdateListeners.delete(listener);
|
|
241
263
|
}
|
|
@@ -245,30 +267,42 @@ export class AgentController implements AgentClient {
|
|
|
245
267
|
return () => this.outputListeners.delete(listener);
|
|
246
268
|
}
|
|
247
269
|
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
if (this.current && !this.isConnected) await this.stop();
|
|
251
|
-
if (this.current) {
|
|
252
|
-
return { sessionId: this.currentSessionId ?? "", resumed: this.acp?.hasSession ?? false };
|
|
253
|
-
}
|
|
254
|
-
const brought = await this.bringUp(this.mode, this.config.sessionId, onProgress);
|
|
255
|
-
this.current = brought.client;
|
|
256
|
-
this.acp = brought.acp;
|
|
257
|
-
this.attachAvailableCommandSource();
|
|
258
|
-
return brought.result;
|
|
270
|
+
start(onProgress?: (event: ControllerStartEvent) => void): Promise<ConnectResult> {
|
|
271
|
+
return this.startConnection(false, onProgress);
|
|
259
272
|
}
|
|
260
273
|
|
|
261
274
|
/** Explicitly create a fresh provider thread, ignoring any startup load target. */
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
275
|
+
startFresh(onProgress?: (event: ControllerStartEvent) => void): Promise<ConnectResult> {
|
|
276
|
+
return this.startConnection(true, onProgress);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
private startConnection(fresh: boolean, onProgress?: (event: ControllerStartEvent) => void): Promise<ConnectResult> {
|
|
280
|
+
return this.connections.run(fresh ? "fresh" : "start", async attempt => {
|
|
281
|
+
await this.stopping;
|
|
282
|
+
if (this.current && !this.isConnected) await this.stopCurrent();
|
|
283
|
+
attempt.throwIfCancelled();
|
|
284
|
+
if (this.current && !fresh) return this.existingConnection();
|
|
285
|
+
if (this.current) {
|
|
286
|
+
if (!this.acp) throw new Error("fresh provider sessions require ACP normal mode");
|
|
287
|
+
attempt.own(() => this.stopCurrent());
|
|
288
|
+
const result = await this.newSession();
|
|
289
|
+
attempt.throwIfCancelled();
|
|
290
|
+
return result;
|
|
291
|
+
}
|
|
292
|
+
const brought = await bringUp(this.config, attempt, this.mode, fresh ? undefined : this.config.sessionId);
|
|
293
|
+
return this.installConnection(attempt, brought);
|
|
294
|
+
}, observeStartProgress(this.config, onProgress));
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
private existingConnection(): ConnectResult {
|
|
298
|
+
return { sessionId: this.currentSessionId ?? "", resumed: this.acp?.hasSession ?? false };
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
private installConnection(attempt: ConnectionAttempt<ControllerStartEvent>, brought: ReadyConnection, mode = this.mode): ConnectResult {
|
|
302
|
+
attempt.throwIfCancelled();
|
|
270
303
|
this.current = brought.client;
|
|
271
304
|
this.acp = brought.acp;
|
|
305
|
+
this.mode = mode;
|
|
272
306
|
this.attachAvailableCommandSource();
|
|
273
307
|
return brought.result;
|
|
274
308
|
}
|
|
@@ -289,6 +323,66 @@ export class AgentController implements AgentClient {
|
|
|
289
323
|
.finally(() => this.forceStopListeners.delete(forced));
|
|
290
324
|
}
|
|
291
325
|
|
|
326
|
+
// --- turn path (ConversationController; controller-interface spec) -----------------------------
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* Session-scheduled: makes no provider call. Reports `turn_started` for the run synchronously,
|
|
330
|
+
* inside this call (`sentBySession: true`), and returns an already resolved promise. The turn's
|
|
331
|
+
* provider work starts when its prepared turn's `prompt()` is called.
|
|
332
|
+
*/
|
|
333
|
+
send(run: TurnRun): Promise<void> {
|
|
334
|
+
const turnId = run.turnId;
|
|
335
|
+
if (!turnId) throw new Error("send: a session-scheduled run requires its turnId");
|
|
336
|
+
if (run.messageIds.length === 0) throw new Error("a run carries at least one message id");
|
|
337
|
+
if (new Set(run.messageIds).size !== run.messageIds.length) {
|
|
338
|
+
throw new Error(`a run repeats a message id: ${run.messageIds.join(", ")}`);
|
|
339
|
+
}
|
|
340
|
+
this.reportedTurns.add(turnId);
|
|
341
|
+
const event: TurnEvent = {
|
|
342
|
+
kind: "turn_started",
|
|
343
|
+
turnId,
|
|
344
|
+
cause: { messageIds: Object.freeze([...run.messageIds]) },
|
|
345
|
+
sentBySession: true,
|
|
346
|
+
};
|
|
347
|
+
for (const listener of [...this.turnListeners]) listener(event);
|
|
348
|
+
return Promise.resolve();
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
/** `turn_started` only: this controller never folds items into a running turn (`turn_took`). */
|
|
352
|
+
subscribeTurns(listener: (event: TurnEvent) => void): () => void {
|
|
353
|
+
this.turnListeners.add(listener);
|
|
354
|
+
return () => this.turnListeners.delete(listener);
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
/**
|
|
358
|
+
* Starts nothing. The returned turn's `prompt()` calls `prompt` with the content's input and
|
|
359
|
+
* handlers; its output, cancel and force stop are this controller's own, since it only ever runs
|
|
360
|
+
* one turn. `live` is always true (nothing is held).
|
|
361
|
+
*/
|
|
362
|
+
prepareTurn(turnId: string, content: TurnContent): PreparedTurn {
|
|
363
|
+
this.reportedTurns.delete(turnId);
|
|
364
|
+
return { turnId, live: true, ...this.supervisedContent(content) };
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
/** An internal prompt (summary, recap, seed): the same shape, no turn bookkeeping. */
|
|
368
|
+
prepareInternalPrompt(content: TurnContent): SupervisedProvider {
|
|
369
|
+
return this.supervisedContent(content);
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
/** Releases a reported turn the session won't run. Idempotent; never stops provider work. */
|
|
373
|
+
abandonTurn(turnId: string, _reason: unknown): void {
|
|
374
|
+
this.reportedTurns.delete(turnId);
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
private supervisedContent(content: TurnContent): SupervisedProvider {
|
|
378
|
+
return {
|
|
379
|
+
prompt: () => this.prompt({ ...(content.input ?? {}), handlers: content.handlers }),
|
|
380
|
+
subscribeOutput: (listener) => this.subscribeOutput(listener),
|
|
381
|
+
cancel: () => this.cancel(),
|
|
382
|
+
forceStop: () => this.forceStop(),
|
|
383
|
+
};
|
|
384
|
+
}
|
|
385
|
+
|
|
292
386
|
cancel(): void { this.current?.interrupt(); }
|
|
293
387
|
|
|
294
388
|
forceStop(): Promise<void> {
|
|
@@ -330,6 +424,10 @@ export class AgentController implements AgentClient {
|
|
|
330
424
|
}
|
|
331
425
|
|
|
332
426
|
async stop(): Promise<void> {
|
|
427
|
+
await Promise.all([this.connections.cancel(), this.stopCurrent()]);
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
private async stopCurrent(): Promise<void> {
|
|
333
431
|
const client = this.current;
|
|
334
432
|
this.current = null;
|
|
335
433
|
this.acp = null;
|
|
@@ -357,18 +455,18 @@ export class AgentController implements AgentClient {
|
|
|
357
455
|
* current leaf and brings up a fresh one in the new mode (sessions do NOT carry across —
|
|
358
456
|
* the two transports are unrelated). Returns the new leaf's connect result.
|
|
359
457
|
*/
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
458
|
+
setMode(mode: AgentMode): Promise<ConnectResult> {
|
|
459
|
+
return this.connections.run(`mode:${mode}`, async attempt => {
|
|
460
|
+
await this.stopping;
|
|
461
|
+
attempt.throwIfCancelled();
|
|
462
|
+
if (mode === this.mode && this.current) return this.existingConnection();
|
|
463
|
+
const previous = this.current;
|
|
464
|
+
const brought = await bringUp(this.config, attempt, mode);
|
|
465
|
+
const result = this.installConnection(attempt, brought, mode);
|
|
466
|
+
if (previous) await previous.stop();
|
|
467
|
+
attempt.throwIfCancelled();
|
|
468
|
+
return result;
|
|
469
|
+
});
|
|
372
470
|
}
|
|
373
471
|
|
|
374
472
|
/** Authenticate the connection. Available whenever connected over ACP (with or without a session). */
|
|
@@ -543,103 +641,6 @@ export class AgentController implements AgentClient {
|
|
|
543
641
|
];
|
|
544
642
|
}
|
|
545
643
|
|
|
546
|
-
/**
|
|
547
|
-
* Bring up a fresh leaf for the configured adapter in `mode`. normal → AcpClient
|
|
548
|
-
* (connect + new/load session); degraded → PtyClient (spawn interactive CLI). PtyClient
|
|
549
|
-
* is dynamically imported so the normal-mode path never loads the degraded subpath.
|
|
550
|
-
*/
|
|
551
|
-
private async bringUp(
|
|
552
|
-
mode: AgentMode,
|
|
553
|
-
sessionId?: string,
|
|
554
|
-
onProgress?: (event: AgentControllerStartEvent) => void,
|
|
555
|
-
loadFailure = this.config.sessionLoadFailure,
|
|
556
|
-
): Promise<{ client: AgentClient; acp: AcpClient | null; result: ConnectResult }> {
|
|
557
|
-
const adapter = this.config.adapter;
|
|
558
|
-
if (mode === "degraded") {
|
|
559
|
-
const { PtyClient } = await import("./degraded/index.ts");
|
|
560
|
-
const client = new PtyClient({
|
|
561
|
-
adapter,
|
|
562
|
-
launcher: this.config.launcher,
|
|
563
|
-
execPrefix: this.config.execPrefix,
|
|
564
|
-
env: this.config.env,
|
|
565
|
-
cwd: this.config.cwd,
|
|
566
|
-
logger: this.config.logger,
|
|
567
|
-
});
|
|
568
|
-
try {
|
|
569
|
-
await client.start();
|
|
570
|
-
} catch (err) {
|
|
571
|
-
await client.stop();
|
|
572
|
-
throw err;
|
|
573
|
-
}
|
|
574
|
-
return { client, acp: null, result: { sessionId: "", resumed: false } };
|
|
575
|
-
}
|
|
576
|
-
|
|
577
|
-
const client = new AcpClient({
|
|
578
|
-
adapter,
|
|
579
|
-
launcher: this.config.launcher,
|
|
580
|
-
execPrefix: this.config.execPrefix,
|
|
581
|
-
env: this.config.env,
|
|
582
|
-
cwd: this.config.cwd,
|
|
583
|
-
additionalDirectories: this.config.additionalDirectories,
|
|
584
|
-
sessionId,
|
|
585
|
-
sessionLoadFailure: loadFailure,
|
|
586
|
-
defaultPermission: this.config.defaultPermission,
|
|
587
|
-
clientInfo: this.config.clientInfo,
|
|
588
|
-
clientCapabilities: this.config.clientCapabilities,
|
|
589
|
-
clientHandlers: this.config.clientHandlers,
|
|
590
|
-
logger: this.config.logger,
|
|
591
|
-
wire: this.config.wire,
|
|
592
|
-
});
|
|
593
|
-
// connect() is fatal (no connection = nothing to do). startSession() is NOT: an
|
|
594
|
-
// unauthenticated agent may refuse session/new, but we keep the connection up so the
|
|
595
|
-
// user can /login and then create a session. (Auth state is opaque — see SPEC.)
|
|
596
|
-
try {
|
|
597
|
-
const capabilities = await client.connect();
|
|
598
|
-
this.reportStartProgress(onProgress, { stage: "initialized", capabilities });
|
|
599
|
-
} catch (err) {
|
|
600
|
-
await client.stop();
|
|
601
|
-
throw err;
|
|
602
|
-
}
|
|
603
|
-
let result: ConnectResult;
|
|
604
|
-
try {
|
|
605
|
-
result = await client.startSession({
|
|
606
|
-
mcpServers: this.config.mcpServers,
|
|
607
|
-
additionalDirectories: this.config.additionalDirectories,
|
|
608
|
-
loadFailure: loadFailure,
|
|
609
|
-
...(this.config.onSessionReplay ? { onReplayUpdate: this.config.onSessionReplay } : {}),
|
|
610
|
-
});
|
|
611
|
-
} catch (err) {
|
|
612
|
-
if (sessionId && loadFailure === "error") {
|
|
613
|
-
await client.stop();
|
|
614
|
-
throw err;
|
|
615
|
-
}
|
|
616
|
-
this.config.logger?.warn("session_start_failed", { error: String(err) });
|
|
617
|
-
result = { sessionId: "", resumed: false };
|
|
618
|
-
}
|
|
619
|
-
if (result.sessionId) {
|
|
620
|
-
this.reportStartProgress(onProgress, {
|
|
621
|
-
stage: "internal_session_acquired",
|
|
622
|
-
internalSessionId: result.sessionId,
|
|
623
|
-
disposition: result.resumed ? "resumed" : "created",
|
|
624
|
-
});
|
|
625
|
-
}
|
|
626
|
-
return { client, acp: client, result };
|
|
627
|
-
}
|
|
628
|
-
|
|
629
|
-
private reportStartProgress(
|
|
630
|
-
listener: ((event: AgentControllerStartEvent) => void) | undefined,
|
|
631
|
-
event: AgentControllerStartEvent,
|
|
632
|
-
): void {
|
|
633
|
-
if (!listener) return;
|
|
634
|
-
try {
|
|
635
|
-
listener(event);
|
|
636
|
-
} catch (error) {
|
|
637
|
-
this.config.logger?.warn("controller_start_progress_listener_failed", {
|
|
638
|
-
error: String(error),
|
|
639
|
-
});
|
|
640
|
-
}
|
|
641
|
-
}
|
|
642
|
-
|
|
643
644
|
private requireCurrent(): AgentClient {
|
|
644
645
|
if (!this.current) throw new NotConnectedError("AgentController is not started — call start() first");
|
|
645
646
|
return this.current;
|
|
@@ -700,6 +701,13 @@ export class AgentController implements AgentClient {
|
|
|
700
701
|
}
|
|
701
702
|
}
|
|
702
703
|
|
|
704
|
+
/** @deprecated Renamed `HarnessController`; the old name stays exported. */
|
|
705
|
+
export const AgentController = HarnessController;
|
|
706
|
+
/** @deprecated Renamed `HarnessController`. */
|
|
707
|
+
export type AgentController = HarnessController;
|
|
708
|
+
/** @deprecated Renamed `ControllerStartEvent`. */
|
|
709
|
+
export type AgentControllerStartEvent = ControllerStartEvent;
|
|
710
|
+
|
|
703
711
|
export function parseConfigValue(
|
|
704
712
|
option: ConfigOption,
|
|
705
713
|
rawValue: string,
|