@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,591 @@
|
|
|
1
|
+
import type { WireReminderEvent } from "./reminders/wire.ts";
|
|
2
|
+
import { nativeConversationCapabilities } from "./capabilities.ts";
|
|
3
|
+
import { advanceItemReceipts } from "./item-receipts.ts";
|
|
4
|
+
import { terminalNativeTurns } from "./metering.ts";
|
|
5
|
+
import { NativeInferenceSource } from "../metering/native/source.ts";
|
|
6
|
+
import { nativeConversationStopping, nativeProviderTurnEnded, refuseNativeOnClose } from "../permissions/conversation.ts";
|
|
7
|
+
import { runNativeApproval } from "./approvals.ts";
|
|
8
|
+
import { recordingPermissionHandlers } from "../permissions/policy.ts";
|
|
9
|
+
/**
|
|
10
|
+
* The native `ConversationController`: one conversation as one channel of the Agent's harness session
|
|
11
|
+
* (acp-client integration spec, Controller behavior). Translation only: the shared `NativeHarnessHost`
|
|
12
|
+
* owns the process, the open sequence and the saved submissions; the harness owns session and channel
|
|
13
|
+
* state, the take rules, execution and permissions.
|
|
14
|
+
*
|
|
15
|
+
* Backend-scheduled: `send` only hands messages over and resolves on durable acceptance; the harness
|
|
16
|
+
* decides which turn takes which input and reports it (`turn_started`, `turn_took`). A turn can produce
|
|
17
|
+
* output before the session attaches its supervisor, so each reported turn holds its events (and its
|
|
18
|
+
* outcome, if it ends first) until the prepared turn's `prompt()` is called. Preparing starts nothing;
|
|
19
|
+
* the harness's `turn_ended`, or the reconciled outcome after a reopen, settles that prompt once.
|
|
20
|
+
*/
|
|
21
|
+
import type { ChannelEvent, StoredMessage, TurnStatus } from "../native-harness/src/client.ts";
|
|
22
|
+
import type { ConversationCapabilities } from "../conversation-controller/capabilities.ts";
|
|
23
|
+
import type {
|
|
24
|
+
ConversationController,
|
|
25
|
+
PreparedTurn,
|
|
26
|
+
StartResult,
|
|
27
|
+
TurnCause,
|
|
28
|
+
TurnContent,
|
|
29
|
+
TurnEvent,
|
|
30
|
+
TurnRun,
|
|
31
|
+
UsageListener,
|
|
32
|
+
} from "../conversation-controller/contracts.ts";
|
|
33
|
+
import type { ConfigOptions } from "../config-options.ts";
|
|
34
|
+
import { UnsupportedError } from "../errors.ts";
|
|
35
|
+
import { noopLogger, type Logger } from "../logger.ts";
|
|
36
|
+
import { isMediaPart, mediaKind, type MediaKind } from "../media/parts.ts";
|
|
37
|
+
import type { OutputListener, ProviderOutput } from "../operations/contracts.ts";
|
|
38
|
+
import type { PromptHandlers, PromptResult, ResponseMessage } from "../types.ts";
|
|
39
|
+
import type { ChannelPort, ChannelView, NativeHarnessHost } from "./host.ts";
|
|
40
|
+
|
|
41
|
+
/** A native turn that ended failed or interrupted without this conversation cancelling it. */
|
|
42
|
+
export class NativeTurnError extends Error {
|
|
43
|
+
/** Tool calls recorded with no recorded result. The harness never replays them; neither does the host. */
|
|
44
|
+
readonly uncertainCalls: readonly string[];
|
|
45
|
+
constructor(readonly outcome: "failed" | "interrupted", message: string, uncertainCalls: readonly string[] = []) {
|
|
46
|
+
super(message);
|
|
47
|
+
this.name = "NativeTurnError";
|
|
48
|
+
this.uncertainCalls = uncertainCalls;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export interface NativeControllerConfig {
|
|
53
|
+
readonly host: NativeHarnessHost;
|
|
54
|
+
/** The channel id: the conversation's id, stable for its lifetime. */
|
|
55
|
+
readonly channelId: string;
|
|
56
|
+
readonly defaultSender?: string;
|
|
57
|
+
/** True when the conversation has existing history (acp-client's own records); reported as `resumed`. */
|
|
58
|
+
readonly resumed?: boolean;
|
|
59
|
+
readonly logger?: Logger;
|
|
60
|
+
readonly idleStopWindowMs?: number;
|
|
61
|
+
readonly onReminderEvent?: (event: WireReminderEvent) => void;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
type Outcome = { outcome: "completed" | "failed" | "interrupted"; error?: string; uncertainCalls?: readonly string[] };
|
|
65
|
+
type TurnScoped = Extract<ChannelEvent["event"], { turnId: string }>;
|
|
66
|
+
|
|
67
|
+
interface Turn {
|
|
68
|
+
readonly id: string;
|
|
69
|
+
readonly harnessId: string;
|
|
70
|
+
readonly items: Set<string>;
|
|
71
|
+
held: TurnScoped[];
|
|
72
|
+
sink: ((event: TurnScoped) => void) | null;
|
|
73
|
+
readonly completed: Set<string>;
|
|
74
|
+
readonly approvals: Set<string>;
|
|
75
|
+
ended: Outcome | null;
|
|
76
|
+
terminalOnWire?: boolean;
|
|
77
|
+
stoppedBeforeResult?: true;
|
|
78
|
+
/** This conversation asked for the interruption, so an interrupted end is a cancel, not a failure. */
|
|
79
|
+
cancelRequested: boolean;
|
|
80
|
+
/** Given up locally (`forceStop`, `abandonTurn`, `stop`): later events only release bookkeeping. */
|
|
81
|
+
released: boolean;
|
|
82
|
+
fail: ((error: Error, stopping?: boolean) => void) | null;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const sessionTurnId = (harnessId: string) => `native-turn-${harnessId}`;
|
|
86
|
+
const NO_OPTIONS = new Map() as ConfigOptions;
|
|
87
|
+
export class NativeController implements ConversationController, ChannelPort {
|
|
88
|
+
readonly #host: NativeHarnessHost;
|
|
89
|
+
readonly #channel: string;
|
|
90
|
+
readonly #config: NativeControllerConfig;
|
|
91
|
+
readonly #log: Logger;
|
|
92
|
+
readonly #turns = new Map<string, Turn>();
|
|
93
|
+
readonly #turnListeners = new Set<(event: TurnEvent) => void>();
|
|
94
|
+
readonly #outputListeners = new Set<OutputListener>();
|
|
95
|
+
readonly #connectionListeners = new Set<() => void>();
|
|
96
|
+
readonly #receipts = new Map<string, "delivered" | "seen">();
|
|
97
|
+
/** Items of turns the last view listed, so a checkpoint resumed later can name what it took. */
|
|
98
|
+
#viewTurns = new Map<string, readonly string[]>();
|
|
99
|
+
#detach: (() => void) | null = null;
|
|
100
|
+
#unsubscribeHost: (() => void) | null = null;
|
|
101
|
+
#stopped = false;
|
|
102
|
+
#attachment: Promise<void> | null = null;
|
|
103
|
+
#attachmentHandle?: { dispose(): void };
|
|
104
|
+
#attachmentApprovals = new AbortController();
|
|
105
|
+
#usage?: NativeInferenceSource;
|
|
106
|
+
|
|
107
|
+
constructor(config: NativeControllerConfig) {
|
|
108
|
+
this.#config = config;
|
|
109
|
+
this.#host = config.host;
|
|
110
|
+
this.#channel = config.channelId;
|
|
111
|
+
this.#log = config.logger ?? noopLogger;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
get channelId(): string { return this.#channel; }
|
|
115
|
+
get idleStopWindowMs(): number | undefined { return this.#config.idleStopWindowMs; }
|
|
116
|
+
|
|
117
|
+
async start(): Promise<StartResult> {
|
|
118
|
+
this.#stopped = false;
|
|
119
|
+
if (!this.#detach) {
|
|
120
|
+
const detach = this.#host.attach(this.#channel, this, true);
|
|
121
|
+
this.#detach = detach;
|
|
122
|
+
this.#attachmentHandle = { dispose: () => { if (this.#detach === detach) this.disposeAttachment(); } };
|
|
123
|
+
this.#attachment = this.#host.observeAttachment(this.#channel, this).catch(error => {
|
|
124
|
+
if (this.#detach === detach) this.disposeAttachment();
|
|
125
|
+
throw error;
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
await this.#attachment;
|
|
129
|
+
this.#unsubscribeHost ??= this.#host.subscribeState(() => this.#notifyConnection());
|
|
130
|
+
this.#notifyConnection();
|
|
131
|
+
return { sessionId: this.#host.sessionId, resumed: this.#config.resumed ?? false };
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Unsupported: a native conversation shares its agent's session and timeline, and neither an
|
|
136
|
+
* agent-wide nor a channel context reset exists yet (`conversationCapabilities.freshContext` is
|
|
137
|
+
* false). Throws before touching anything: the channel, session and timeline are unchanged.
|
|
138
|
+
*/
|
|
139
|
+
startFresh(): Promise<StartResult> {
|
|
140
|
+
return Promise.reject(new UnsupportedError("a native conversation cannot start a fresh context: the harness has no context reset"));
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
async stop(): Promise<void> {
|
|
144
|
+
if (this.#stopped) return;
|
|
145
|
+
this.#closeAttachmentApprovals(true);
|
|
146
|
+
for (const turn of [...this.#turns.values()].filter(turn => !turn.ended && !turn.released)) {
|
|
147
|
+
turn.cancelRequested = true;
|
|
148
|
+
await this.#host.interrupt(this.#channel, turn.harnessId, "interrupted by the conversation");
|
|
149
|
+
}
|
|
150
|
+
this.#stopped = true;
|
|
151
|
+
this.#release(new Error("the conversation stopped"), false, true);
|
|
152
|
+
this.disposeAttachment();
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
async attachObservation(): Promise<{ dispose(): void }> {
|
|
156
|
+
await this.start();
|
|
157
|
+
if (!this.#attachmentHandle) throw new Error("Attachment was disposed during observation");
|
|
158
|
+
return this.#attachmentHandle;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
disposeAttachment(): void {
|
|
162
|
+
this.#closeAttachmentApprovals();
|
|
163
|
+
this.#attachmentApprovals = new AbortController();
|
|
164
|
+
this.#attachment = null;
|
|
165
|
+
this.#attachmentHandle = undefined;
|
|
166
|
+
this.#detach?.();
|
|
167
|
+
this.#detach = null;
|
|
168
|
+
this.#unsubscribeHost?.();
|
|
169
|
+
this.#unsubscribeHost = null;
|
|
170
|
+
this.#notifyConnection();
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
#closeAttachmentApprovals(stopping = false): void {
|
|
174
|
+
this.#attachmentApprovals.abort(stopping ? nativeConversationStopping : undefined);
|
|
175
|
+
for (const turn of this.#turns.values()) this.#refuseHeldApprovals(turn, stopping);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
#refuseHeldApprovals(turn: Turn, stopping = false): void {
|
|
179
|
+
for (const event of turn.held) if (event.type === "approval_requested") {
|
|
180
|
+
refuseNativeOnClose(this.#host.approvalFor(event), stopping, () => this.#log);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/** True unless this conversation stopped or the host gave up: a stopped process is started by the next work. */
|
|
185
|
+
get isConnected(): boolean { return !this.#stopped && this.#host.state !== "failed"; }
|
|
186
|
+
subscribeConnection(listener: () => void): () => void {
|
|
187
|
+
this.#connectionListeners.add(listener);
|
|
188
|
+
return () => this.#connectionListeners.delete(listener);
|
|
189
|
+
}
|
|
190
|
+
#notifyConnection(): void {
|
|
191
|
+
for (const listener of [...this.#connectionListeners]) { try { listener(); } catch { /* observer */ } }
|
|
192
|
+
}
|
|
193
|
+
readonly cancelGraceMs = undefined;
|
|
194
|
+
|
|
195
|
+
get attachmentConnected(): boolean { return this.#detach !== null; }
|
|
196
|
+
get attachmentStatus(): "disconnected" | "offline" | "suspended" | "online" {
|
|
197
|
+
if (!this.#detach) return "disconnected";
|
|
198
|
+
if (this.#host.state !== "running") return "offline";
|
|
199
|
+
return this.#host.channel(this.#channel)?.lifecycle === "suspended" || this.#host.sessionLifecycle === "suspended"
|
|
200
|
+
? "suspended" : "online";
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
history(limit?: number, cursor?: string) {
|
|
204
|
+
return this.#host.conversationMessages(cursor === undefined ? { channel: this.#channel, limit } : { channel: this.#channel, cursor, limit });
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
catchUp(after: string, commit: Parameters<NativeHarnessHost["catchUp"]>[2], signal?: AbortSignal) {
|
|
208
|
+
return this.#host.catchUp(this.#channel, after, commit, signal);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
readonly configOptions: ConfigOptions = NO_OPTIONS;
|
|
212
|
+
setConfig(id: string): Promise<void> {
|
|
213
|
+
return Promise.reject(new UnsupportedError(`a native conversation has no config option ${id}`));
|
|
214
|
+
}
|
|
215
|
+
subscribeUsageUpdate(_listener: UsageListener): () => void { return () => undefined; }
|
|
216
|
+
get currentSessionId(): string | null { return this.#host.sessionId; }
|
|
217
|
+
get conversationCapabilities(): ConversationCapabilities { return nativeConversationCapabilities(this.#host.reminderSync?.supported, this.#host.reminderSync?.routineAdmission.available); }
|
|
218
|
+
|
|
219
|
+
receiptOf(messageId: string): "delivered" | "seen" | undefined { return this.#receipts.get(messageId); }
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Pushes each message as an input of the channel, in order, under its own id. Resolves once the
|
|
223
|
+
* harness has stored every one durably (a receipt, or the reconciled state after a reopen). Promises
|
|
224
|
+
* no turn and ignores `run.turnId`: the harness reports which turn takes each input.
|
|
225
|
+
*/
|
|
226
|
+
async send(run: TurnRun): Promise<void> {
|
|
227
|
+
if (new Set(run.messageIds).size !== run.messageIds.length) throw new Error("a run repeats a message id");
|
|
228
|
+
if (this.#stopped) throw new Error("the conversation is stopped");
|
|
229
|
+
const media = this.conversationCapabilities.backend?.media;
|
|
230
|
+
const requireMedia = (kind: MediaKind) => {
|
|
231
|
+
if (!media?.[kind]) throw new UnsupportedError(`a native conversation does not support ${kind} input`);
|
|
232
|
+
};
|
|
233
|
+
// Validate the whole run before the host can persist or deliver any of its messages.
|
|
234
|
+
for (const message of run.messages) {
|
|
235
|
+
for (const part of message.messageParts ?? []) {
|
|
236
|
+
if (isMediaPart(part)) requireMedia(part.mimeType === "image/svg+xml" ? "file" : part.kind);
|
|
237
|
+
}
|
|
238
|
+
for (const attachment of message.attachments ?? []) requireMedia(mediaKind(attachment.mimeType));
|
|
239
|
+
}
|
|
240
|
+
// The harness assigns turn ids after acceptance. Use the input's id before any push.
|
|
241
|
+
for (const message of run.messages) {
|
|
242
|
+
if (message.beforeDispatch) await message.beforeDispatch(message.id);
|
|
243
|
+
}
|
|
244
|
+
if (this.#stopped) throw new Error("the conversation is stopped");
|
|
245
|
+
if (!this.#detach) await this.start();
|
|
246
|
+
for (const message of run.messages) {
|
|
247
|
+
if (run.beginInput?.(message.id) === false) continue;
|
|
248
|
+
await this.#host.push({
|
|
249
|
+
id: message.id,
|
|
250
|
+
channel: this.#channel,
|
|
251
|
+
sender: message.participantId ?? this.#config.defaultSender ?? "user",
|
|
252
|
+
text: message.text,
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
withdrawInput(messageId: string, onTaken?: (turnId?: string) => void): Promise<boolean> {
|
|
258
|
+
return this.#host.withdrawSubmission(this.#channel, messageId, onTaken && (id => onTaken(id === undefined ? undefined : sessionTurnId(id))));
|
|
259
|
+
}
|
|
260
|
+
subscribeTurns(listener: (event: TurnEvent) => void): () => void {
|
|
261
|
+
this.#turnListeners.add(listener);
|
|
262
|
+
return () => this.#turnListeners.delete(listener);
|
|
263
|
+
}
|
|
264
|
+
#report(event: TurnEvent): void {
|
|
265
|
+
for (const listener of [...this.#turnListeners]) listener(event);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/** Which of a turn's items are messages this conversation sent; the rest are backend items. */
|
|
269
|
+
#cause(items: readonly string[]): TurnCause {
|
|
270
|
+
const messageIds = items.filter((id) => this.#host.records.submission(id)?.channel === this.#channel);
|
|
271
|
+
const others = items.filter((id) => !messageIds.includes(id)).map((id) => ({ id }));
|
|
272
|
+
if (messageIds.length && others.length) return { messageIds, items: others };
|
|
273
|
+
return messageIds.length ? { messageIds } : { items: others };
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
#begin(harnessId: string, items: readonly string[]): Turn {
|
|
277
|
+
const turn: Turn = {
|
|
278
|
+
id: sessionTurnId(harnessId), harnessId, items: new Set(items), held: [], sink: null,
|
|
279
|
+
completed: new Set(), approvals: new Set(), ended: null, cancelRequested: false, released: false, fail: null,
|
|
280
|
+
};
|
|
281
|
+
this.#turns.set(turn.id, turn);
|
|
282
|
+
this.#report({ kind: "turn_started", turnId: turn.id, cause: this.#cause(items), sentBySession: false });
|
|
283
|
+
return turn;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
reminderEvent(event: WireReminderEvent): void { this.#config.onReminderEvent?.(event); }
|
|
287
|
+
event(event: ChannelEvent): void {
|
|
288
|
+
const payload = event.event;
|
|
289
|
+
if (payload.type === "channel_state") { this.#notifyConnection(); return; }
|
|
290
|
+
if (payload.type === "approval_closed") return; // The host settles the shared approval ticket.
|
|
291
|
+
if (payload.type === "item_withdrawn") { this.#report({ kind: "input_withdrawn", messageId: payload.item }); return; }
|
|
292
|
+
if (payload.type === "turn_started") {
|
|
293
|
+
if (!this.#turns.has(sessionTurnId(payload.turnId))) this.#begin(payload.turnId, payload.items);
|
|
294
|
+
return;
|
|
295
|
+
}
|
|
296
|
+
if (payload.turnId === undefined) return;
|
|
297
|
+
let turn = this.#turns.get(sessionTurnId(payload.turnId));
|
|
298
|
+
// A checkpoint from before this connection continues under its original id: the first this
|
|
299
|
+
// conversation hears of it is the resume.
|
|
300
|
+
if (!turn && payload.type === "turn_resumed") turn = this.#begin(payload.turnId, this.#viewTurns.get(payload.turnId) ?? []);
|
|
301
|
+
if (!turn) { this.#log.warn("native_event_for_unknown_turn", { channel: this.#channel, turnId: payload.turnId, type: payload.type }); return; }
|
|
302
|
+
if (payload.type === "turn_took") {
|
|
303
|
+
// The association holds even when it arrives before the push response: the session matches by id.
|
|
304
|
+
for (const item of payload.items) turn.items.add(item);
|
|
305
|
+
if (!turn.released) this.#report({ kind: "turn_took", turnId: turn.id, cause: this.#cause(payload.items) });
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
this.#accept(turn, payload);
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
#accept(turn: Turn, event: TurnScoped, terminalOnWire = true): void {
|
|
312
|
+
if (turn.ended) return; // terminal once: a second end (live after reconciled, or the reverse) changes nothing
|
|
313
|
+
if (event.type === "items_receipt") {
|
|
314
|
+
const itemIds = advanceItemReceipts(this.#receipts, event.items, event.milestone);
|
|
315
|
+
if (itemIds.length && !turn.released) this.#report({ kind: "items_receipt", turnId: turn.id, itemIds, milestone: event.milestone });
|
|
316
|
+
return;
|
|
317
|
+
}
|
|
318
|
+
if (event.type === "message_completed" && event.messageId !== undefined) {
|
|
319
|
+
if (turn.completed.has(event.messageId)) return;
|
|
320
|
+
turn.completed.add(event.messageId);
|
|
321
|
+
}
|
|
322
|
+
if (event.type === "turn_ended") {
|
|
323
|
+
turn.terminalOnWire = terminalOnWire;
|
|
324
|
+
turn.ended = { outcome: event.outcome, ...(event.error === undefined ? {} : { error: event.error }), ...(event.uncertainCalls ? { uncertainCalls: event.uncertainCalls } : {}) };
|
|
325
|
+
// Terminal for every input the turn took: nothing is left to retry.
|
|
326
|
+
this.#host.settle(turn.items);
|
|
327
|
+
}
|
|
328
|
+
if (turn.released) {
|
|
329
|
+
if (turn.ended) this.#turns.delete(turn.id);
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
if (turn.sink) turn.sink(event);
|
|
333
|
+
else turn.held.push(event);
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* The view after an open. Turns this conversation was following get the outcome they reached while it
|
|
338
|
+
* was not listening, with any committed reply it missed restored by message id; nothing is repeated.
|
|
339
|
+
* A turn it never heard of that took one of its unsettled inputs is reported now, already ended.
|
|
340
|
+
*/
|
|
341
|
+
reconcile(view: ChannelView): void {
|
|
342
|
+
this.#viewTurns = new Map(view.turns.map((turn) => [turn.turnId, turn.items]));
|
|
343
|
+
const byTurn = new Map<string, StoredMessage[]>();
|
|
344
|
+
for (const message of view.messages) byTurn.set(message.turnId, [...(byTurn.get(message.turnId) ?? []), message]);
|
|
345
|
+
const statuses = new Map(view.turns.map((turn) => [turn.turnId, turn]));
|
|
346
|
+
for (const item of view.items) if (item.milestone === "withdrawn") this.#report({ kind: "input_withdrawn", messageId: item.id });
|
|
347
|
+
|
|
348
|
+
const settle = (turn: Turn, status: TurnStatus | undefined) => {
|
|
349
|
+
if (!status) {
|
|
350
|
+
// The harness has no such turn (a session recreated on an empty home): it cannot end by itself.
|
|
351
|
+
this.#accept(turn, { type: "turn_ended", turnId: turn.harnessId, outcome: "failed", error: "the harness has no record of this turn" }, false);
|
|
352
|
+
return;
|
|
353
|
+
}
|
|
354
|
+
for (const message of byTurn.get(status.turnId) ?? []) {
|
|
355
|
+
this.#accept(turn, { type: "message_completed", turnId: status.turnId, messageId: message.messageId,
|
|
356
|
+
...(message.reference ? { reference: true as const } : { parts: message.parts }) });
|
|
357
|
+
}
|
|
358
|
+
// Running or checkpointed: it goes on, and its events will follow this view.
|
|
359
|
+
if (status.state === "running" || status.state === "suspended") return;
|
|
360
|
+
this.#accept(turn, { type: "turn_ended", turnId: status.turnId, outcome: status.state,
|
|
361
|
+
...(status.reason === undefined ? {} : { error: status.reason }),
|
|
362
|
+
...(status.uncertainCalls?.length ? { uncertainCalls: status.uncertainCalls } : {}) });
|
|
363
|
+
};
|
|
364
|
+
|
|
365
|
+
for (const turn of [...this.#turns.values()]) if (!turn.ended) settle(turn, statuses.get(turn.harnessId));
|
|
366
|
+
for (const status of view.turns) {
|
|
367
|
+
if (this.#turns.has(sessionTurnId(status.turnId))) continue;
|
|
368
|
+
// Only a turn that owns one of this conversation's unsettled inputs: older turns were settled before.
|
|
369
|
+
if (status.state !== "running" && status.turnId !== view.status?.activeTurn?.turnId && !status.items.some((id) => this.#host.records.submission(id)?.channel === this.#channel)) continue;
|
|
370
|
+
settle(this.#begin(status.turnId, status.items), status);
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
disconnected(): void { this.#notifyConnection(); }
|
|
375
|
+
|
|
376
|
+
failed(error: Error): void {
|
|
377
|
+
this.#release(error, false);
|
|
378
|
+
this.#notifyConnection();
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
trackedTurns(): string[] {
|
|
382
|
+
return [...this.#turns.values()].filter(turn => !turn.ended && !turn.released).map(turn => turn.harnessId);
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
busy(): boolean {
|
|
386
|
+
return [...this.#turns.values()].some((turn) => !turn.ended && !turn.released);
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
prepareTurn(turnId: string, content: TurnContent): PreparedTurn {
|
|
390
|
+
const turn = this.#turns.get(turnId);
|
|
391
|
+
if (!turn || turn.released) throw new Error(`unknown turn ${turnId}`);
|
|
392
|
+
const listeners = new Set<OutputListener>();
|
|
393
|
+
const signal = (output: ProviderOutput) => {
|
|
394
|
+
for (const listener of [...listeners]) listener(output);
|
|
395
|
+
for (const listener of [...this.#outputListeners]) listener(output);
|
|
396
|
+
};
|
|
397
|
+
const replay = turn.ended !== null;
|
|
398
|
+
return {
|
|
399
|
+
turnId,
|
|
400
|
+
nativeMetering: { turnId: turn.harnessId, isTerminal: () => turn.terminalOnWire === true,
|
|
401
|
+
terminalEarlierTurns: terminalNativeTurns(this.#host, this.#channel), read: position =>
|
|
402
|
+
(this.#usage ??= new NativeInferenceSource(this.#host.sessionId, this.#channel, this.#host.usageLedger))
|
|
403
|
+
.readTurn(turn.harnessId, position) },
|
|
404
|
+
recoverStoppedReplies: async seen => turn.stoppedBeforeResult ? this.#host.recoverStoppedReplies(this.#channel, turn.harnessId, seen) : undefined,
|
|
405
|
+
get live() { return !replay; },
|
|
406
|
+
subscribeOutput: (listener) => { listeners.add(listener); return () => listeners.delete(listener); },
|
|
407
|
+
cancel: () => this.#interrupt(turn),
|
|
408
|
+
forceStop: () => this.forceStop(),
|
|
409
|
+
prompt: () => this.#run(turn, content.handlers, signal),
|
|
410
|
+
};
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
#run(turn: Turn, handlers: PromptHandlers, signal: (output: ProviderOutput) => void): Promise<PromptResult> {
|
|
414
|
+
const sessionId = this.#host.sessionId;
|
|
415
|
+
const messages: ResponseMessage[] = [];
|
|
416
|
+
const drafts = new Map<string, string>();
|
|
417
|
+
const approvals = new AbortController();
|
|
418
|
+
const approvalWork = new Set<Promise<void>>();
|
|
419
|
+
return new Promise<PromptResult>((resolve, reject) => {
|
|
420
|
+
let settled = false;
|
|
421
|
+
const finish = (act: () => void, providerEnded = false) => {
|
|
422
|
+
if (settled) return;
|
|
423
|
+
settled = true;
|
|
424
|
+
turn.sink = null;
|
|
425
|
+
turn.fail = null;
|
|
426
|
+
approvals.abort(providerEnded ? nativeProviderTurnEnded : undefined);
|
|
427
|
+
if (turn.ended) this.#turns.delete(turn.id);
|
|
428
|
+
if (approvalWork.size) void Promise.allSettled(approvalWork).then(act);
|
|
429
|
+
else act();
|
|
430
|
+
};
|
|
431
|
+
const joined = () => messages.map(message => message.text).join("\n\n");
|
|
432
|
+
const cancelled = (terminal: boolean) => finish(() => resolve({
|
|
433
|
+
text: joined(), messages, stopReason: "cancelled", status: "cancelled", usage: null,
|
|
434
|
+
}), terminal);
|
|
435
|
+
turn.fail = (error, stopping) => {
|
|
436
|
+
if (!stopping) return finish(() => reject(error));
|
|
437
|
+
turn.stoppedBeforeResult = true;
|
|
438
|
+
cancelled(false);
|
|
439
|
+
};
|
|
440
|
+
const text = (chunk: string, messageId: string, startsMessage: boolean) => {
|
|
441
|
+
handlers.onChunk?.(chunk);
|
|
442
|
+
handlers.onResponseChunk?.({ text: chunk, messageId, startsMessage });
|
|
443
|
+
};
|
|
444
|
+
const complete = (messageId: string, full: string) => {
|
|
445
|
+
if (settled) return; // A by-reference read can finish after shutdown.
|
|
446
|
+
const draft = drafts.get(messageId);
|
|
447
|
+
// The committed text replaces a draft with the same id. Only the part not streamed is sent
|
|
448
|
+
// on; a draft that diverged (dropped deltas) cannot be retracted, and the result below is
|
|
449
|
+
// the committed text either way.
|
|
450
|
+
if (draft === undefined) text(full, messageId, true);
|
|
451
|
+
else if (full.startsWith(draft) && full.length > draft.length) text(full.slice(draft.length), messageId, false);
|
|
452
|
+
drafts.delete(messageId);
|
|
453
|
+
if (!messages.some((message) => message.messageId === messageId)) messages.push({ messageId, text: full });
|
|
454
|
+
signal({ kind: "update", sessionId });
|
|
455
|
+
};
|
|
456
|
+
// Events are applied one at a time, in order: a message fetched by reference must not be
|
|
457
|
+
// overtaken by the events behind it.
|
|
458
|
+
let chain: Promise<void> = Promise.resolve();
|
|
459
|
+
const apply = async (event: TurnScoped, attachment: AbortSignal): Promise<void> => {
|
|
460
|
+
switch (event.type) {
|
|
461
|
+
case "text_delta":
|
|
462
|
+
text(event.text, event.messageId, !drafts.has(event.messageId));
|
|
463
|
+
drafts.set(event.messageId, (drafts.get(event.messageId) ?? "") + event.text);
|
|
464
|
+
signal({ kind: "update", sessionId });
|
|
465
|
+
return;
|
|
466
|
+
case "text_gap":
|
|
467
|
+
case "text_incomplete":
|
|
468
|
+
return; // a draft is provisional: only a completion with its id makes it a reply
|
|
469
|
+
case "message_completed": {
|
|
470
|
+
const messageId = event.messageId!;
|
|
471
|
+
complete(messageId, event.reference ? await this.#host.readMessage(messageId) : (event.parts ?? []).map((part) => part.text).join(""));
|
|
472
|
+
return;
|
|
473
|
+
}
|
|
474
|
+
case "tool_call": {
|
|
475
|
+
const activity = event.status === "started"
|
|
476
|
+
? { kind: "tool" as const, toolCallId: event.id, title: event.name, toolKind: "other", status: "in_progress", ...(event.input === undefined ? {} : { rawInput: parse(event.input) }) }
|
|
477
|
+
// A call that was skipped, or whose wait ended without an outcome, did not complete.
|
|
478
|
+
: { kind: "tool_update" as const, toolCallId: event.id, status: event.status === "completed" ? "completed" : "failed", ...(event.output === undefined ? {} : { rawOutput: event.output }) };
|
|
479
|
+
handlers.onActivity?.(activity);
|
|
480
|
+
signal({ kind: "update", sessionId, activity });
|
|
481
|
+
return;
|
|
482
|
+
}
|
|
483
|
+
case "approval_requested":
|
|
484
|
+
// Not awaited: the turn can end (interrupted, timed out) while the consumer decides, and
|
|
485
|
+
// that end must not wait behind the question.
|
|
486
|
+
if (turn.approvals.has(event.requestId)) return;
|
|
487
|
+
turn.approvals.add(event.requestId);
|
|
488
|
+
const approval = this.#approve(event, handlers,
|
|
489
|
+
AbortSignal.any([approvals.signal, attachment]), signal);
|
|
490
|
+
if (recordingPermissionHandlers.has(handlers.onPermissionRequest!)) {
|
|
491
|
+
approvalWork.add(approval);
|
|
492
|
+
void approval.then(() => approvalWork.delete(approval));
|
|
493
|
+
}
|
|
494
|
+
return;
|
|
495
|
+
case "turn_resumed":
|
|
496
|
+
signal({ kind: "update", sessionId });
|
|
497
|
+
return;
|
|
498
|
+
case "turn_ended": {
|
|
499
|
+
if (event.outcome === "completed") {
|
|
500
|
+
finish(() => resolve({ text: joined(), messages, stopReason: "end_turn", status: "completed", usage: null }), true);
|
|
501
|
+
} else if (event.outcome === "interrupted" && turn.cancelRequested) {
|
|
502
|
+
cancelled(true);
|
|
503
|
+
} else {
|
|
504
|
+
const reason = event.error ?? event.outcome;
|
|
505
|
+
finish(() => reject(new NativeTurnError(event.outcome as "failed" | "interrupted", `native turn ${turn.harnessId} ${event.outcome}: ${reason}`, event.uncertainCalls)), true);
|
|
506
|
+
}
|
|
507
|
+
return;
|
|
508
|
+
}
|
|
509
|
+
default:
|
|
510
|
+
return; // receipts and checkpoints change nothing the handlers show
|
|
511
|
+
}
|
|
512
|
+
};
|
|
513
|
+
turn.sink = (event) => {
|
|
514
|
+
const attachment = this.#attachmentApprovals.signal;
|
|
515
|
+
chain = chain.then(() => (settled ? undefined : apply(event, attachment))).catch((error) => {
|
|
516
|
+
this.#log.warn("native_turn_event_failed", { turnId: turn.id, type: event.type, error: String(error) });
|
|
517
|
+
// A reply that cannot be read is a failed turn for the host, never a silently shorter one.
|
|
518
|
+
if (event.type === "message_completed") finish(() => reject(error));
|
|
519
|
+
});
|
|
520
|
+
};
|
|
521
|
+
const held = turn.held;
|
|
522
|
+
turn.held = [];
|
|
523
|
+
for (const event of held) turn.sink(event);
|
|
524
|
+
});
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
async #approve(event: Extract<TurnScoped, { type: "approval_requested" }>, handlers: PromptHandlers, cancelled: AbortSignal, signal: (output: ProviderOutput) => void): Promise<void> {
|
|
528
|
+
const ticket = this.#host.approvalFor(event);
|
|
529
|
+
if (!ticket) return;
|
|
530
|
+
const sessionId = this.#host.sessionId;
|
|
531
|
+
signal({ kind: "permission_started", sessionId });
|
|
532
|
+
try { await runNativeApproval(ticket, event, handlers, cancelled, this.#log); }
|
|
533
|
+
finally { signal({ kind: "permission_ended", sessionId }); }
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
#interrupt(turn: Turn): void {
|
|
537
|
+
if (turn.ended) return;
|
|
538
|
+
turn.cancelRequested = true;
|
|
539
|
+
this.#host.interrupt(this.#channel, turn.harnessId, "interrupted by the conversation").catch((error) => {
|
|
540
|
+
// Not running: the reopen's view will report the turn interrupted, and it settles as cancelled.
|
|
541
|
+
this.#log.debug("native_interrupt_not_sent", { turnId: turn.id, error: String(error) });
|
|
542
|
+
});
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
/**
|
|
546
|
+
* Ends a reported turn the session will not run: releases what is held for it. Idempotent, and never
|
|
547
|
+
* stops the harness's work (that is `cancel` / `forceStop`); a turn already executing is left alone.
|
|
548
|
+
*/
|
|
549
|
+
abandonTurn(turnId: string, _reason: unknown): void {
|
|
550
|
+
const turn = this.#turns.get(turnId);
|
|
551
|
+
if (!turn || turn.released || turn.sink) return;
|
|
552
|
+
turn.released = true;
|
|
553
|
+
this.#refuseHeldApprovals(turn);
|
|
554
|
+
turn.held = [];
|
|
555
|
+
if (turn.ended) this.#turns.delete(turnId);
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
subscribeOutput(listener: OutputListener): () => void {
|
|
559
|
+
this.#outputListeners.add(listener);
|
|
560
|
+
return () => this.#outputListeners.delete(listener);
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
/** Cancels the turn executing now on this channel. Other channels are untouched. */
|
|
564
|
+
cancel(): void {
|
|
565
|
+
for (const turn of this.#turns.values()) if (!turn.ended && !turn.released) this.#interrupt(turn);
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
/**
|
|
569
|
+
* Conversation-level force stop: a targeted interrupt of this channel's active turn, and this
|
|
570
|
+
* conversation's own work settled locally (open prepared turns reject). The shared process, its
|
|
571
|
+
* other channels and their subscriptions are untouched: only the host stops the process.
|
|
572
|
+
*/
|
|
573
|
+
async forceStop(): Promise<void> {
|
|
574
|
+
this.#release(new Error("forced stop"), true);
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
#release(error: Error, interrupt: boolean, stopping = false): void {
|
|
578
|
+
for (const turn of [...this.#turns.values()]) {
|
|
579
|
+
turn.released = true;
|
|
580
|
+
this.#refuseHeldApprovals(turn);
|
|
581
|
+
turn.held = [];
|
|
582
|
+
turn.fail?.(error, stopping);
|
|
583
|
+
if (interrupt && !turn.ended) this.#interrupt(turn);
|
|
584
|
+
if (turn.ended) this.#turns.delete(turn.id);
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
function parse(input: string): unknown {
|
|
590
|
+
try { return JSON.parse(input); } catch { return input; }
|
|
591
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { HarnessClient } from "../native-harness/src/client.ts";
|
|
2
|
+
import { FunctionError } from "./functions.ts";
|
|
3
|
+
|
|
4
|
+
/** Use the first taken input as the caller's attribution, even if more inputs arrive during the turn. */
|
|
5
|
+
export async function takenSender(
|
|
6
|
+
client: Pick<HarnessClient, "reconcileChannel">, channel: string, turnId: string, signal: AbortSignal,
|
|
7
|
+
): Promise<string> {
|
|
8
|
+
signal.throwIfAborted();
|
|
9
|
+
const tracked = await client.reconcileChannel({ channel, turnIds: [turnId] });
|
|
10
|
+
signal.throwIfAborted();
|
|
11
|
+
const turn = tracked.turns.find(value => value.turnId === turnId && value.channel === channel);
|
|
12
|
+
const first = turn?.state === "running" ? turn.items[0] : undefined;
|
|
13
|
+
if (!first) throw new FunctionError("delegation_sender_unavailable", "No taken input identifies this delegation call");
|
|
14
|
+
const { items } = await client.reconcileChannel({ channel, itemIds: [first] });
|
|
15
|
+
signal.throwIfAborted();
|
|
16
|
+
const item = items.find(value => value.id === first && value.channel === channel && value.turnId === turnId);
|
|
17
|
+
const id = item?.sender.trim();
|
|
18
|
+
if (!item || !id) throw new FunctionError("delegation_sender_unavailable", "No sender identifies the turn's first taken input");
|
|
19
|
+
if (item.kind !== "participant_message") return `notice:${id}`;
|
|
20
|
+
if (/^[\s\p{Default_Ignorable_Code_Point}]+$/u.test(id)) {
|
|
21
|
+
throw new FunctionError("delegation_sender_unavailable", "Blank participant ID");
|
|
22
|
+
}
|
|
23
|
+
// Escape reserved prefixes so arbitrary participant IDs stay distinct from notice labels.
|
|
24
|
+
if (id.startsWith("notice:") || id.startsWith("participant:")) return `participant:${id}`;
|
|
25
|
+
return id;
|
|
26
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { HarnessClientError } from "../native-harness/src/client.ts";
|
|
2
|
+
|
|
3
|
+
export type HarnessUnavailableCode = "not_running" | "failed" | "stopped" | "open_timeout" | "start_failed" | "reopen_failed";
|
|
4
|
+
|
|
5
|
+
/** The harness cannot currently be used. The code identifies which lifecycle condition prevented it. */
|
|
6
|
+
export class HarnessUnavailableError extends Error {
|
|
7
|
+
readonly code: HarnessUnavailableCode;
|
|
8
|
+
|
|
9
|
+
constructor(message: string, options?: { cause?: unknown; code?: HarnessUnavailableCode }) {
|
|
10
|
+
super(message, options);
|
|
11
|
+
this.name = "HarnessUnavailableError";
|
|
12
|
+
this.code = options?.code ?? "failed";
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** The running harness lacks a capability required by this integration. */
|
|
17
|
+
export class HarnessIncompatibleError extends Error {
|
|
18
|
+
constructor(message: string) {
|
|
19
|
+
super(message);
|
|
20
|
+
this.name = "HarnessIncompatibleError";
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function pendingRetirementError(channel: string): HarnessClientError {
|
|
25
|
+
return new HarnessClientError("channel_retired", `Channel ${channel} is pending retirement`);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export const codeOf = (error: unknown): string | undefined => error instanceof HarnessClientError ? error.code : undefined;
|
|
29
|
+
/** The request's outcome is unknown: it may or may not have been applied. */
|
|
30
|
+
export const ambiguous = (error: unknown) => { const code = codeOf(error); return code === "timeout" || code === "transport_closed" || code === "closed"; };
|
|
31
|
+
|
|
32
|
+
export function terminalStartupError(error: unknown): error is Error {
|
|
33
|
+
return error instanceof HarnessIncompatibleError || (error instanceof Error && "code" in error &&
|
|
34
|
+
(error.code === "invalid_config" || error.code === "reminder_batch_retry_exhausted"));
|
|
35
|
+
}
|