@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,92 @@
|
|
|
1
|
+
import { reminderDefinitions } from "../src/capabilities/tools/reminders/index.ts";
|
|
2
|
+
import { notifyChannelDefinition } from "../src/capabilities/tools/builtin/notify-channel.ts";
|
|
3
|
+
import { expect, test } from "bun:test";
|
|
4
|
+
import { mkdtemp, rm } from "node:fs/promises";
|
|
5
|
+
import { createScriptedModel } from "../src/capabilities/model/scripted.ts";
|
|
6
|
+
import { bindModelAttempt } from "../src/capabilities/model/attempt.ts";
|
|
7
|
+
import { scenarioAttempt } from "../src/capabilities/model/scenario-attempt.ts";
|
|
8
|
+
import { loadScenario } from "../src/testing.ts";
|
|
9
|
+
import type { ModelRequest } from "../src/contracts/model-execution.ts";
|
|
10
|
+
import { echoDefinition, executeEcho } from "../src/capabilities/tools/testing/echo.ts";
|
|
11
|
+
import { createSession } from "../src/index.ts";
|
|
12
|
+
import { ask } from "./helpers/session.ts";
|
|
13
|
+
import type { Message } from "../src/contracts/loop.ts";
|
|
14
|
+
|
|
15
|
+
test("file scenario executes echo through the real scheduler/loop and supplies its result to the next call", async () => {
|
|
16
|
+
const scenario = await loadScenario(new URL("./fixtures/echo-scenario.jsonl", import.meta.url).pathname);
|
|
17
|
+
const requests: ModelRequest[] = [];
|
|
18
|
+
const model = bindModelAttempt(async () => { throw new Error("Expected a reserved step"); }, {
|
|
19
|
+
scripted: true, create: step => scenarioAttempt(scenario, "test", step!, request => requests.push(request)),
|
|
20
|
+
});
|
|
21
|
+
const session = createSession({ systemPrompt: "Test system", configureTurn: () => ({ model, tools: [echoDefinition], executeTool: executeEcho }) });
|
|
22
|
+
const store = session.store;
|
|
23
|
+
session.start();
|
|
24
|
+
expect(await ask(session, { channelId: "test", participantId: "tester", text: "Echo hello" })).toBe("The tool returned hello.");
|
|
25
|
+
expect(requests).toHaveLength(2);
|
|
26
|
+
expect(requests[0]?.messages.at(-1)).toEqual({ role: "user", content: 'Participant: "tester"\n\nEcho hello' });
|
|
27
|
+
expect(requests[1]?.messages.slice(-2)).toEqual([
|
|
28
|
+
{ role: "assistant", content: "I'll echo that.", calls: [{ id: "echo-1", name: "echo", arguments: '{"text":"hello"}' }] },
|
|
29
|
+
{ role: "tool", callId: "echo-1", content: '{"ok":true,"text":"hello"}' },
|
|
30
|
+
]);
|
|
31
|
+
expect(requests[0]?.tools).toEqual([echoDefinition, notifyChannelDefinition, ...reminderDefinitions]);
|
|
32
|
+
expect(store.snapshot().map(entry => entry.message.role)).toEqual(["user", "assistant", "tool", "assistant"]);
|
|
33
|
+
expect(requests).toHaveLength(scenario.defaultModels.length);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
test("script, returned responses and captured requests do not alias caller data; instances have independent positions", async () => {
|
|
37
|
+
const script = { version: 1, responses: [{ content: "original" }] };
|
|
38
|
+
const first = createScriptedModel(script);
|
|
39
|
+
const second = createScriptedModel(script);
|
|
40
|
+
script.responses[0]!.content = "changed";
|
|
41
|
+
const messages: Message[] = [{ role: "user", content: "prompt" }];
|
|
42
|
+
const tools = [structuredClone(echoDefinition)];
|
|
43
|
+
const answer = await first.model(messages, tools);
|
|
44
|
+
answer.content = "changed answer";
|
|
45
|
+
messages[0]!.content = "changed prompt";
|
|
46
|
+
tools[0]!.name = "changed tool";
|
|
47
|
+
const captured = first.requests();
|
|
48
|
+
expect(captured[0]?.messages[0]?.content).toBe("prompt");
|
|
49
|
+
expect(captured[0]?.tools[0]?.name).toBe("echo");
|
|
50
|
+
captured[0]!.messages.length = 0;
|
|
51
|
+
expect(first.requests()[0]?.messages).toHaveLength(1);
|
|
52
|
+
expect((await second.model([], [])).content).toBe("original");
|
|
53
|
+
// A reopened session skips the responses an earlier process already consumed.
|
|
54
|
+
const continued = createScriptedModel({ version: 1, responses: [{ content: "first" }, { content: "second" }] }, 1);
|
|
55
|
+
expect((await continued.model([], [])).content).toBe("second");
|
|
56
|
+
expect(continued.remaining()).toBe(0);
|
|
57
|
+
await expect(first.model([], [])).rejects.toMatchObject({ code: "script_exhausted" });
|
|
58
|
+
expect(first.requests()).toHaveLength(2);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
test("malformed scripts fail before execution with stable errors", () => {
|
|
62
|
+
for (const script of [
|
|
63
|
+
{}, { version: 2, responses: [] }, { version: 1, responses: [] },
|
|
64
|
+
{ version: 1, responses: [{ content: 42 }] },
|
|
65
|
+
{ version: 1, responses: [{ content: null, calls: null }] },
|
|
66
|
+
{ version: 1, responses: [{ content: "text", typo: true }] },
|
|
67
|
+
{ version: 1, responses: [{ content: null, calls: [{ id: "a", name: "echo", arguments: [] }] }] },
|
|
68
|
+
{ version: 1, responses: [{ content: null, calls: [
|
|
69
|
+
{ id: "a", name: "echo", arguments: {} }, { id: "a", name: "echo", arguments: {} },
|
|
70
|
+
] }] },
|
|
71
|
+
]) expect(() => createScriptedModel(script)).toThrow(expect.objectContaining({ code: "invalid_script" }));
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
test("missing files and invalid JSON have distinct error codes", async () => {
|
|
75
|
+
const dir = await mkdtemp("/tmp/scripted-model-");
|
|
76
|
+
try {
|
|
77
|
+
await expect(loadScenario(`${dir}/missing.jsonl`)).rejects.toMatchObject({ code: "script_load_failed" });
|
|
78
|
+
await Bun.write(`${dir}/invalid.jsonl`, "{");
|
|
79
|
+
await expect(loadScenario(`${dir}/invalid.jsonl`)).rejects.toMatchObject({ code: "invalid_script" });
|
|
80
|
+
} finally { await rm(dir, { recursive: true, force: true }); }
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
test("echo preserves text and returns actionable failures to the loop", async () => {
|
|
84
|
+
const text = "Hello\n\n你好 **literal**";
|
|
85
|
+
expect(JSON.parse(await executeEcho({ id: "a", name: "echo", arguments: JSON.stringify({ text }) })))
|
|
86
|
+
.toEqual({ ok: true, text });
|
|
87
|
+
for (const [name, arguments_] of [["other", "{}"], ["echo", "{"], ["echo", '{"text":4}'], ["echo", '{"text":"hello","extra":true}']]) {
|
|
88
|
+
const result = JSON.parse(await executeEcho({ id: "a", name: name!, arguments: arguments_! }));
|
|
89
|
+
expect(result.ok).toBe(false);
|
|
90
|
+
expect(typeof result.error).toBe("string");
|
|
91
|
+
}
|
|
92
|
+
});
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { expect, test } from "bun:test";
|
|
2
|
+
import { mkdtempSync, rmSync } from "node:fs";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { decodeSessionFile, encodeSessionFile } from "../src/state/storage/session-file.ts";
|
|
6
|
+
import { createMemoryStorageAdapter } from "../src/state/storage/memory.ts";
|
|
7
|
+
import { createSqliteStorageAdapter } from "../src/state/storage/sqlite.ts";
|
|
8
|
+
|
|
9
|
+
const max = 268435456;
|
|
10
|
+
const frame = (hex: string) => { const payload = Buffer.from(hex, "hex"), out = Buffer.alloc(14 + payload.length); out.write("HARNSESS"); out.writeUInt16BE(1, 8); out.writeUInt32BE(payload.length, 10); payload.copy(out, 14); return out; };
|
|
11
|
+
const failure = (fn: () => unknown, code: string) => { try { fn(); throw new Error("expected failure"); } catch (e) { expect(e).toMatchObject({ code }); } };
|
|
12
|
+
|
|
13
|
+
test("CBOR profile reads independent vectors and rejects invalid envelopes and values", () => {
|
|
14
|
+
expect(decodeSessionFile(frame("a3616101616282f5f6616362c3a9"), max)).toEqual({ a: 1, b: [true, null], c: "é" });
|
|
15
|
+
expect(decodeSessionFile(frame("f93e00"), max)).toBe(1.5);
|
|
16
|
+
for (const hex of ["a2616101616102", "5f40ff", "c001", "f7", "fb7ff0000000000000", "a10100", "61ff", "0102", "9f01ff"]) failure(() => decodeSessionFile(frame(hex), max), "invalid_archive");
|
|
17
|
+
const good = frame("a0");
|
|
18
|
+
for (const bad of [Buffer.from("{}"), good.subarray(0, 13), Buffer.concat([good, Buffer.from([0])]), Buffer.from(good)]) {
|
|
19
|
+
if (bad.length === good.length) bad[9] = 2;
|
|
20
|
+
failure(() => decodeSessionFile(bad, max), "invalid_archive");
|
|
21
|
+
}
|
|
22
|
+
const huge = Buffer.from(good); huge.writeUInt32BE(0xffffffff, 10);
|
|
23
|
+
failure(() => decodeSessionFile(huge, 10), "invalid_archive");
|
|
24
|
+
failure(() => decodeSessionFile(good, 14), "transfer_limit");
|
|
25
|
+
failure(() => encodeSessionFile({ n: Infinity }, max), "serialization_error");
|
|
26
|
+
failure(() => encodeSessionFile({ n: 1 }, 14), "transfer_limit");
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
for (const kind of ["memory", "sqlite"] as const) test(`${kind}: imported configuration is atomic, durable and optional for old sessions`, () => {
|
|
30
|
+
const dir = mkdtempSync(join(tmpdir(), "harness-config-"));
|
|
31
|
+
try {
|
|
32
|
+
const make = (name: string) => kind === "memory" ? createMemoryStorageAdapter() : createSqliteStorageAdapter(join(dir, name));
|
|
33
|
+
const original = make("source"), opened = original.open({ mode: "create", identity: { sessionId: "s", compatibilityKey: "k" } });
|
|
34
|
+
if (!opened.ok) throw new Error(opened.error.message);
|
|
35
|
+
expect(opened.value.importedConfig()).toMatchObject({ ok: true, value: undefined });
|
|
36
|
+
const exported = opened.value.exportArchive(); if (!exported.ok) throw new Error(exported.error.message);
|
|
37
|
+
opened.value.close();
|
|
38
|
+
const config = { version: 1 as const, system: "exact", model: { protocol: "scripted" as const, script: "relative.json" },
|
|
39
|
+
defaultPolicy: "__legacy_allow", policies: { __legacy_allow: { fallback: "allow" as const, tools: {} } } };
|
|
40
|
+
const destination = make("dest");
|
|
41
|
+
expect(destination.importSessionFile({ archive: exported.value, config: { ...config, model: { ...config.model, script: " " } } }).ok).toBe(false);
|
|
42
|
+
expect(destination.importSessionFile({ archive: exported.value, config }).ok).toBe(true);
|
|
43
|
+
expect(destination.importSessionFile({ archive: exported.value, config }).ok).toBe(false);
|
|
44
|
+
const restored = destination.open({ mode: "open", identity: { sessionId: "s", compatibilityKey: "k" } });
|
|
45
|
+
if (!restored.ok) throw new Error(restored.error.message);
|
|
46
|
+
const saved = restored.value.importedConfig(); expect(saved).toMatchObject({ ok: true, value: config });
|
|
47
|
+
if (saved.ok && saved.value) saved.value.system = "modified";
|
|
48
|
+
expect(restored.value.importedConfig()).toMatchObject({ ok: true, value: config });
|
|
49
|
+
restored.value.close();
|
|
50
|
+
} finally { rmSync(dir, { recursive: true, force: true }); }
|
|
51
|
+
});
|
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
import { inspectOperatorView } from "./helpers/operator-view.ts";
|
|
2
|
+
import { afterAll, expect, test } from "bun:test";
|
|
3
|
+
import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
4
|
+
import { tmpdir } from "node:os";
|
|
5
|
+
import { join } from "node:path";
|
|
6
|
+
import type { ChannelEvent, PendingChannelChange, ProtocolConfig } from "../src/client.ts";
|
|
7
|
+
import { createInProcessHarness } from "../src/testing.ts";
|
|
8
|
+
import { parseChannelChanges, sameBatch } from "../src/interfaces/protocol/channel-changes.ts";
|
|
9
|
+
import { spawnHarness } from "./helpers/stdio.ts";
|
|
10
|
+
|
|
11
|
+
// Decision 0020: channel changes made while the harness was stopped are applied
|
|
12
|
+
// by the open, before recovered work can run. Durable cases go through the
|
|
13
|
+
// public client to real `serve --stdio` processes with SQLite sessions.
|
|
14
|
+
const root = mkdtempSync(join(tmpdir(), "harness-startup-"));
|
|
15
|
+
afterAll(() => rmSync(root, { recursive: true, force: true }));
|
|
16
|
+
const fixture = (name: string) => new URL(`./fixtures/${name}`, import.meta.url).pathname;
|
|
17
|
+
const config: ProtocolConfig = { version: 1, system: "Startup changes", model: { protocol: "scripted", script: fixture("hold-scenario.jsonl") },
|
|
18
|
+
defaultPolicy: "open", policies: { open: { fallback: "allow", tools: {} }, strict: { fallback: "deny", tools: {} } } };
|
|
19
|
+
const push = (channel: string, id: string, text: string) => ({ channel, item: { id, kind: "user_message" as const, sender: `user-${channel}`, content: [{ type: "text" as const, text }] } });
|
|
20
|
+
const lines = (path: string) => existsSync(path) ? readFileSync(path, "utf8").split("\n").filter(Boolean) : [];
|
|
21
|
+
async function until(check: () => boolean | Promise<boolean>, what: string) {
|
|
22
|
+
for (let i = 0; i < 600; i++) { if (await check()) return; await new Promise(resolve => setTimeout(resolve, 5)); }
|
|
23
|
+
throw new Error(`Timed out waiting for ${what}`);
|
|
24
|
+
}
|
|
25
|
+
function watch(harness: ReturnType<typeof spawnHarness>) {
|
|
26
|
+
const events: ChannelEvent[] = [];
|
|
27
|
+
harness.client.onChannelEvent(event => { events.push(event); });
|
|
28
|
+
return events;
|
|
29
|
+
}
|
|
30
|
+
const started = (events: ChannelEvent[], channel: string) => events.filter(event => event.channel === channel && (event.event.type === "turn_started" || event.event.type === "turn_resumed"));
|
|
31
|
+
|
|
32
|
+
/** A session whose process died with work outstanding: channel `a` has turn 1
|
|
33
|
+
* running in its tool (recovered as interrupted) and `a2` accepted but never
|
|
34
|
+
* taken. Without a startup batch the next open would run `a2`. */
|
|
35
|
+
async function crashed(name: string) {
|
|
36
|
+
const home = mkdtempSync(join(root, `${name}-`)), effects = join(home, "effects.log");
|
|
37
|
+
const first = spawnHarness({ home, effects });
|
|
38
|
+
await first.client.initialize();
|
|
39
|
+
await first.client.createSession({ sessionId: "s", config, participants: [], channels: {} });
|
|
40
|
+
await first.client.push(push("a", "a1", "start"));
|
|
41
|
+
await until(() => lines(effects).includes("start:k"), "tool start");
|
|
42
|
+
await first.client.push(push("a", "a2", "pending"));
|
|
43
|
+
first.child.kill("SIGKILL"); await first.child.exited; first.client.close();
|
|
44
|
+
// Tools of later turns return at once.
|
|
45
|
+
writeFileSync(`${effects}.release-k`, "");
|
|
46
|
+
return { home, effects, open: (fault?: string) => spawnHarness({ home, effects, fault }) };
|
|
47
|
+
}
|
|
48
|
+
const load = (harness: ReturnType<typeof spawnHarness>, channelChanges?: PendingChannelChange[]) =>
|
|
49
|
+
inspectOperatorView(harness.client, { sessionId: "s", config, ...(channelChanges ? { channelChanges } : {}) });
|
|
50
|
+
|
|
51
|
+
test("a suspension made while stopped holds recovered input; other channels run; a live resume takes the input once", async () => {
|
|
52
|
+
const place = await crashed("suspend");
|
|
53
|
+
const second = place.open(), events = watch(second);
|
|
54
|
+
try {
|
|
55
|
+
const init = await second.client.initialize();
|
|
56
|
+
expect(init.description.capabilities.startupChannelChanges).toBe(true);
|
|
57
|
+
const loaded = await load(second, [{ channel: "a", changeId: "v7", lifecycle: "suspended" }, { channel: "fresh", changeId: "v1", displayName: "Fresh", policy: "strict" }]);
|
|
58
|
+
expect(loaded.channelChanges).toEqual([{ channel: "a", changeId: "v7", outcome: "applied" }, { channel: "fresh", changeId: "v1", outcome: "applied" }]);
|
|
59
|
+
// The view already shows the result of the batch.
|
|
60
|
+
expect(loaded.status.channels).toMatchObject([
|
|
61
|
+
{ channel: "a", lifecycle: "suspended", pending: ["a2"], waitingForResume: true },
|
|
62
|
+
{ channel: "fresh", lifecycle: "open", displayName: "Fresh", policy: "strict", pending: [] }]);
|
|
63
|
+
expect(loaded.status.execution).toBe("idle");
|
|
64
|
+
expect(loaded.turns).toMatchObject([{ turnId: "1", state: "interrupted", items: ["a1"] }]);
|
|
65
|
+
// Another channel works while `a` stays parked.
|
|
66
|
+
await second.client.push(push("b", "b1", "hello"));
|
|
67
|
+
await until(() => events.some(event => event.channel === "b" && event.event.type === "turn_ended"), "b's turn");
|
|
68
|
+
expect(started(events, "a")).toEqual([]);
|
|
69
|
+
expect((await second.client.status()).channels.find(channel => channel.channel === "a")).toMatchObject({ lifecycle: "suspended", pending: ["a2"] });
|
|
70
|
+
await expect(second.client.push(push("a", "a3", "refused"))).rejects.toMatchObject({ code: "channel_suspended" });
|
|
71
|
+
// The backlog is intact and is taken exactly once after the live resume.
|
|
72
|
+
await second.client.resumeChannel({ channel: "a" });
|
|
73
|
+
await until(() => events.some(event => event.channel === "a" && event.event.type === "turn_ended"), "a's turn");
|
|
74
|
+
expect(started(events, "a")).toMatchObject([{ event: { type: "turn_started", items: ["a2"] } }]);
|
|
75
|
+
} finally { await second.close(); }
|
|
76
|
+
}, 20000);
|
|
77
|
+
|
|
78
|
+
test("a delete blocked by pending work suspends the channel, keeps its work and lets the rest of the session open; it applies once the work is withdrawn", async () => {
|
|
79
|
+
const place = await crashed("delete");
|
|
80
|
+
const batch: PendingChannelChange[] = [{ channel: "a", changeId: "del-1", lifecycle: "retired", displayName: "ignored when blocked" }, { channel: "b", changeId: "b-1", displayName: "Bob" }];
|
|
81
|
+
const second = place.open(), events = watch(second);
|
|
82
|
+
try {
|
|
83
|
+
await second.client.initialize();
|
|
84
|
+
const loaded = await load(second, batch);
|
|
85
|
+
expect(loaded.channelChanges).toEqual([{ channel: "a", changeId: "del-1", outcome: "blocked", reason: "channel_busy" }, { channel: "b", changeId: "b-1", outcome: "applied" }]);
|
|
86
|
+
const a = loaded.status.channels.find(channel => channel.channel === "a")!;
|
|
87
|
+
expect(a).toMatchObject({ lifecycle: "suspended", pending: ["a2"], waitingForResume: true });
|
|
88
|
+
expect(a.displayName).toBeUndefined();
|
|
89
|
+
expect(loaded.items.find(item => item.id === "a2")).toMatchObject({ milestone: "received" });
|
|
90
|
+
await second.client.push(push("b", "b1", "hello"));
|
|
91
|
+
await until(() => events.some(event => event.channel === "b" && event.event.type === "turn_ended"), "b's turn");
|
|
92
|
+
expect(started(events, "a")).toEqual([]);
|
|
93
|
+
// The host resolves the obstacle through the live API.
|
|
94
|
+
expect(await second.client.withdrawItem({ channel: "a", item: "a2" })).toMatchObject({ outcome: "withdrawn" });
|
|
95
|
+
} finally { await second.close(); }
|
|
96
|
+
|
|
97
|
+
const third = place.open();
|
|
98
|
+
try {
|
|
99
|
+
await third.client.initialize();
|
|
100
|
+
const loaded = await load(third, batch);
|
|
101
|
+
expect(loaded.channelChanges).toEqual([{ channel: "a", changeId: "del-1", outcome: "applied" }, { channel: "b", changeId: "b-1", outcome: "applied" }]);
|
|
102
|
+
expect(loaded.status.channels.find(channel => channel.channel === "a")).toMatchObject({ lifecycle: "retired" });
|
|
103
|
+
} finally { await third.close(); }
|
|
104
|
+
|
|
105
|
+
// A repeated retirement is already applied; a retired ID cannot be reopened.
|
|
106
|
+
const fourth = place.open();
|
|
107
|
+
try {
|
|
108
|
+
await fourth.client.initialize();
|
|
109
|
+
const loaded = await load(fourth, [{ channel: "a", changeId: "del-1", lifecycle: "retired" }]);
|
|
110
|
+
expect(loaded.channelChanges).toEqual([{ channel: "a", changeId: "del-1", outcome: "applied" }]);
|
|
111
|
+
} finally { await fourth.close(); }
|
|
112
|
+
const fifth = place.open();
|
|
113
|
+
try {
|
|
114
|
+
await fifth.client.initialize();
|
|
115
|
+
const loaded = await load(fifth, [{ channel: "a", changeId: "re-1", lifecycle: "open", displayName: "Back" }, { channel: "gone", changeId: "g-1", lifecycle: "retired" }]);
|
|
116
|
+
expect(loaded.channelChanges).toEqual([{ channel: "a", changeId: "re-1", outcome: "blocked", reason: "channel_retired" }, { channel: "gone", changeId: "g-1", outcome: "applied" }]);
|
|
117
|
+
expect(loaded.status.channels.filter(channel => channel.lifecycle === "retired").map(channel => channel.channel)).toEqual(["a", "gone"]);
|
|
118
|
+
await expect(fifth.client.push(push("gone", "g1", "refused"))).rejects.toMatchObject({ code: "channel_retired" });
|
|
119
|
+
} finally { await fifth.close(); }
|
|
120
|
+
}, 30000);
|
|
121
|
+
|
|
122
|
+
test("an invalid batch is rejected whole and leaves every channel as it was", async () => {
|
|
123
|
+
const place = await crashed("invalid");
|
|
124
|
+
const second = place.open(), events = watch(second);
|
|
125
|
+
try {
|
|
126
|
+
await second.client.initialize();
|
|
127
|
+
const valid = { channel: "new", changeId: "n1", displayName: "New" };
|
|
128
|
+
const invalid: [unknown, string][] = [
|
|
129
|
+
[[valid, { channel: "new", changeId: "n2" }], "invalid_params"],
|
|
130
|
+
[[valid, { channel: "a", changeId: "p1", policy: "nope", lifecycle: "suspended" }], "invalid_policy"],
|
|
131
|
+
[[valid, { channel: "a" }], "invalid_params"],
|
|
132
|
+
[[valid, { channel: "a", changeId: "x", lifecycle: "suspending" }], "invalid_params"],
|
|
133
|
+
[[valid, { channel: "a", changeId: "x", extra: true }], "invalid_params"],
|
|
134
|
+
[[valid, { channel: "a", changeId: "x", displayName: "" }], "invalid_params"],
|
|
135
|
+
[{ channel: "a", changeId: "x" }, "invalid_params"],
|
|
136
|
+
];
|
|
137
|
+
for (const [batch, code] of invalid) await expect(load(second, batch as PendingChannelChange[])).rejects.toMatchObject({ code });
|
|
138
|
+
// Nothing was created, renamed or suspended, and nothing ran meanwhile.
|
|
139
|
+
expect(events).toEqual([]);
|
|
140
|
+
const loaded = await load(second);
|
|
141
|
+
expect(loaded.channelChanges).toBeUndefined();
|
|
142
|
+
expect(loaded.status.channels).toMatchObject([{ channel: "a", lifecycle: "open", waitingForResume: false }]);
|
|
143
|
+
// Scheduling releases after the open response. The separate status RPC
|
|
144
|
+
// may already see the recovered item taken, so read its stable identity.
|
|
145
|
+
const recovered = await second.client.reconcileChannel({ channel: "a", itemIds: ["a2"] });
|
|
146
|
+
expect(recovered.items.map(item => item.id)).toEqual(["a2"]);
|
|
147
|
+
expect(loaded.status.channels[0]!.displayName).toBeUndefined();
|
|
148
|
+
await until(() => events.some(event => event.channel === "a" && event.event.type === "turn_ended"), "recovered input to run");
|
|
149
|
+
} finally { await second.close(); }
|
|
150
|
+
}, 20000);
|
|
151
|
+
|
|
152
|
+
test("a lost open response is safe to retry on the same connection: recorded results, nothing applied twice", async () => {
|
|
153
|
+
const place = await crashed("repeat");
|
|
154
|
+
const second = place.open(), events = watch(second);
|
|
155
|
+
const batch: PendingChannelChange[] = [{ channel: "a", changeId: "v1", lifecycle: "suspended", displayName: "Alice" }];
|
|
156
|
+
try {
|
|
157
|
+
await second.client.initialize();
|
|
158
|
+
const first = await load(second, batch);
|
|
159
|
+
const again = await load(second, batch), bare = await load(second);
|
|
160
|
+
expect(again.channelChanges).toEqual(first.channelChanges!);
|
|
161
|
+
expect(bare.channelChanges).toEqual(first.channelChanges!);
|
|
162
|
+
// A fresh view of the same state: no commit happened between the loads.
|
|
163
|
+
expect(again.status).toEqual(first.status);
|
|
164
|
+
expect(events.filter(event => event.event.type === "channel_state").length).toBeLessThanOrEqual(2);
|
|
165
|
+
await expect(load(second, [{ channel: "a", changeId: "v2", lifecycle: "open" }])).rejects.toMatchObject({ code: "invalid_state" });
|
|
166
|
+
expect((await second.client.status()).channels[0]).toMatchObject({ lifecycle: "suspended", displayName: "Alice", pending: ["a2"] });
|
|
167
|
+
expect(started(events, "a")).toEqual([]);
|
|
168
|
+
} finally { await second.close(); }
|
|
169
|
+
}, 20000);
|
|
170
|
+
|
|
171
|
+
test("a process that dies after committing the batch but before answering: resending the desired state on a new connection is safe", async () => {
|
|
172
|
+
const place = await crashed("reconnect");
|
|
173
|
+
const batch: PendingChannelChange[] = [{ channel: "a", changeId: "v1", lifecycle: "suspended" }, { channel: "c", changeId: "c1", displayName: "Carol" }];
|
|
174
|
+
// Dies right after the suspension commits: the host never sees a response.
|
|
175
|
+
const second = place.open("beginChannelSuspend:1");
|
|
176
|
+
await second.client.initialize();
|
|
177
|
+
await expect(load(second, batch)).rejects.toMatchObject({ code: "transport_closed" });
|
|
178
|
+
expect(await second.child.exited).not.toBe(0);
|
|
179
|
+
second.client.close();
|
|
180
|
+
|
|
181
|
+
const third = place.open(), events = watch(third);
|
|
182
|
+
try {
|
|
183
|
+
await third.client.initialize();
|
|
184
|
+
const loaded = await load(third, batch);
|
|
185
|
+
expect(loaded.channelChanges).toEqual([{ channel: "a", changeId: "v1", outcome: "applied" }, { channel: "c", changeId: "c1", outcome: "applied" }]);
|
|
186
|
+
expect(loaded.status.channels).toMatchObject([{ channel: "a", lifecycle: "suspended", pending: ["a2"] }, { channel: "c", displayName: "Carol", lifecycle: "open" }]);
|
|
187
|
+
// No work was duplicated or lost by the retry.
|
|
188
|
+
expect(loaded.turns).toHaveLength(1);
|
|
189
|
+
expect(loaded.items.map(item => `${item.id}:${item.milestone}`)).toEqual(["a1:seen", "a2:received"]);
|
|
190
|
+
await new Promise(resolve => setTimeout(resolve, 50));
|
|
191
|
+
expect(started(events, "a")).toEqual([]);
|
|
192
|
+
} finally { await third.close(); }
|
|
193
|
+
}, 20000);
|
|
194
|
+
|
|
195
|
+
test("a resume in the batch continues a checkpoint only after the response; an incompatible checkpoint blocks and stays suspended", async () => {
|
|
196
|
+
const home = mkdtempSync(join(root, "resume-")), effects = join(home, "effects.log");
|
|
197
|
+
const first = spawnHarness({ home, effects });
|
|
198
|
+
try {
|
|
199
|
+
await first.client.initialize();
|
|
200
|
+
await first.client.createSession({ sessionId: "s", config, participants: [], channels: {} });
|
|
201
|
+
await first.client.push(push("a", "a1", "start"));
|
|
202
|
+
await until(() => lines(effects).includes("start:k"), "tool start");
|
|
203
|
+
const suspending = first.client.suspendChannel({ channel: "a" });
|
|
204
|
+
await until(async () => (await first.client.status()).channels[0]?.lifecycle === "suspending", "suspension to begin");
|
|
205
|
+
writeFileSync(`${effects}.release-k`, "");
|
|
206
|
+
expect(await suspending).toMatchObject({ channel: { lifecycle: "suspended", activeTurn: { state: "suspended" } } });
|
|
207
|
+
} finally { await first.close(); }
|
|
208
|
+
|
|
209
|
+
const resume: PendingChannelChange[] = [{ channel: "a", changeId: "r1", lifecycle: "open" }];
|
|
210
|
+
const changed = spawnHarness({ home, effects });
|
|
211
|
+
try {
|
|
212
|
+
await changed.client.initialize();
|
|
213
|
+
const loaded = await inspectOperatorView(changed.client, { sessionId: "s", config: { ...config, system: "Another system" }, channelChanges: resume });
|
|
214
|
+
expect(loaded.channelChanges).toEqual([{ channel: "a", changeId: "r1", outcome: "blocked", reason: "incompatible" }]);
|
|
215
|
+
expect(loaded.status.channels).toMatchObject([{ channel: "a", lifecycle: "suspended", activeTurn: { turnId: "1", state: "suspended" } }]);
|
|
216
|
+
} finally { await changed.close(); }
|
|
217
|
+
|
|
218
|
+
const second = spawnHarness({ home, effects }), events = watch(second);
|
|
219
|
+
try {
|
|
220
|
+
await second.client.initialize();
|
|
221
|
+
const loaded = await second.client.openSession({ sessionId: "s", config, channelChanges: resume });
|
|
222
|
+
expect(loaded.channelChanges).toEqual([{ channel: "a", changeId: "r1", outcome: "applied" }]);
|
|
223
|
+
await until(() => events.some(event => event.event.type === "turn_ended"), "resumed turn");
|
|
224
|
+
expect(events.at(-1)).toMatchObject({ event: { type: "turn_ended", turnId: "1", outcome: "completed" } });
|
|
225
|
+
// Everything the resumed turn produced came after the open response.
|
|
226
|
+
const at = second.wire.findIndex(message => "result" in message && (message.result as { sessionId?: string })?.sessionId === "s");
|
|
227
|
+
const work = second.wire.findIndex(message => "method" in message && ["message_completed", "tool_call", "turn_ended", "items_receipt"].includes(message.params.event.type));
|
|
228
|
+
expect(at).toBeGreaterThanOrEqual(0);
|
|
229
|
+
expect(work).toBeGreaterThan(at);
|
|
230
|
+
} finally { await second.close(); }
|
|
231
|
+
}, 30000);
|
|
232
|
+
|
|
233
|
+
test("create applies a batch before the new session starts; in-memory servers support it too", async () => {
|
|
234
|
+
const harness = createInProcessHarness();
|
|
235
|
+
try {
|
|
236
|
+
await harness.client.initialize();
|
|
237
|
+
await expect(harness.client.createSession({ sessionId: "bad", config, participants: [], channels: {}, channelChanges: [{ channel: "a", changeId: "1", policy: "nope" }] }))
|
|
238
|
+
.rejects.toMatchObject({ code: "invalid_policy" });
|
|
239
|
+
const created = await harness.client.createSession({ sessionId: "m", config, participants: [], channels: {},
|
|
240
|
+
channelChanges: [{ channel: "a", changeId: "1", displayName: "Alice", lifecycle: "suspended" }, { channel: "b", changeId: "2", policy: "strict", lifecycle: "open" }] });
|
|
241
|
+
expect(created).toMatchObject({ sessionId: "m", lifecycle: "running", revision: expect.any(Number), channelChanges: [{ channel: "a", changeId: "1", outcome: "applied" }, { channel: "b", changeId: "2", outcome: "applied" }] });
|
|
242
|
+
expect((await harness.client.status()).channels).toMatchObject([{ channel: "a", displayName: "Alice", lifecycle: "suspended" }, { channel: "b", policy: "strict", lifecycle: "open" }]);
|
|
243
|
+
await expect(harness.client.push(push("a", "a1", "refused"))).rejects.toMatchObject({ code: "channel_suspended" });
|
|
244
|
+
// Null clears and a missing field leaves the value alone, through the live API's own rules.
|
|
245
|
+
expect(await harness.client.editChannel({ channel: "a", displayName: null })).toMatchObject({ channel: { channel: "a", lifecycle: "suspended" } });
|
|
246
|
+
} finally { harness.close(); }
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
test("batch parsing keeps null distinct from missing and compares batches by content", () => {
|
|
250
|
+
const parsed = parseChannelChanges([{ channel: "a", changeId: "1", displayName: null }, { channel: "b", changeId: "2", policy: null, lifecycle: "open" }]);
|
|
251
|
+
expect(parsed).toEqual([{ channel: "a", changeId: "1", displayName: null }, { channel: "b", changeId: "2", policy: null, lifecycle: "open" }]);
|
|
252
|
+
expect(sameBatch(parsed, [{ changeId: "1", displayName: null, channel: "a" }, { channel: "b", changeId: "2", policy: null, lifecycle: "open" }])).toBe(true);
|
|
253
|
+
expect(sameBatch(parsed, [{ channel: "a", changeId: "1" }, parsed[1]!])).toBe(false);
|
|
254
|
+
expect(sameBatch(parsed, [parsed[1]!, parsed[0]!])).toBe(false);
|
|
255
|
+
expect(sameBatch([], [])).toBe(true);
|
|
256
|
+
});
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { expect, test } from "bun:test";
|
|
2
|
+
import { LineDecoder, LineWriter } from "../src/interfaces/protocol/framing.ts";
|
|
3
|
+
import { HarnessClient, createStdioTransport } from "../src/client.ts";
|
|
4
|
+
import { serveProtocolStdio } from "../src/interfaces/protocol/stdio-server.ts";
|
|
5
|
+
import { spawnHarness } from "./helpers/stdio.ts";
|
|
6
|
+
import { createInProcessTransport } from "../src/client.ts";
|
|
7
|
+
import { tmpdir } from "node:os";
|
|
8
|
+
import { mkdtempSync, rmSync } from "node:fs";
|
|
9
|
+
import { join } from "node:path";
|
|
10
|
+
|
|
11
|
+
test("framing handles byte-fragmented UTF-8, CRLF, multiple lines and bounded incomplete frames", () => {
|
|
12
|
+
const decoder = new LineDecoder(); const lines: string[] = [];
|
|
13
|
+
for (const byte of new TextEncoder().encode('{"text":"你好"}\r\n\n{}\n')) lines.push(...decoder.push(new Uint8Array([byte])));
|
|
14
|
+
expect(lines).toEqual(['{"text":"你好"}', '{}']); decoder.finish();
|
|
15
|
+
const partial = new LineDecoder(); partial.push(new TextEncoder().encode("{"));
|
|
16
|
+
expect(() => partial.finish()).toThrow("Incomplete");
|
|
17
|
+
expect(() => new LineDecoder(3).push(new TextEncoder().encode("1234"))).toThrow("size limit");
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
test("writer serializes asynchronous writes and surfaces backpressure failures", async () => {
|
|
21
|
+
const written: string[] = []; let active = false;
|
|
22
|
+
const writer = new LineWriter(async bytes => {
|
|
23
|
+
expect(active).toBe(false); active = true;
|
|
24
|
+
await Promise.resolve(); written.push(new TextDecoder().decode(bytes)); active = false;
|
|
25
|
+
});
|
|
26
|
+
await Promise.all([writer.send({ n: 1 }), writer.send({ n: 2 })]);
|
|
27
|
+
expect(written).toEqual(['{"n":1}\n', '{"n":2}\n']);
|
|
28
|
+
await expect(new LineWriter(async () => { throw new Error("broken pipe"); }).send({})).rejects.toThrow("broken pipe");
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
test("malformed server output immediately rejects pending and future requests", async () => {
|
|
32
|
+
for (const bad of ['not json\n', '{"jsonrpc":"2.0","id":1}\n', '{"jsonrpc":"2.0","method":"channel/event","params":{}}\n',
|
|
33
|
+
'{"jsonrpc":"2.0","method":"channel/event","params":{"channel":"a","event":{"type":"approval_requested","requestId":"r","channel":"a","toolCallId":"c","tool":"t","arguments":"{}"}}}\n']) {
|
|
34
|
+
let controller!: ReadableStreamDefaultController<Uint8Array>;
|
|
35
|
+
const stream = new ReadableStream<Uint8Array>({ start(value) { controller = value; } });
|
|
36
|
+
const client = new HarnessClient(createStdioTransport({ readable: stream, async write() {}, end() {} }), 1000);
|
|
37
|
+
const request = client.idle(); controller.enqueue(new TextEncoder().encode(bad));
|
|
38
|
+
await expect(request).rejects.toMatchObject({ code: "transport_closed" });
|
|
39
|
+
await expect(client.idle()).rejects.toMatchObject({ code: "transport_closed" });
|
|
40
|
+
client.close();
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
test("child death rejects pending requests without waiting for the request timeout", async () => {
|
|
45
|
+
const harness = spawnHarness();
|
|
46
|
+
// The real child may answer a normal request immediately; queue a request and
|
|
47
|
+
// kill it synchronously before the writer can dispatch to the process.
|
|
48
|
+
const request = harness.client.initialize();
|
|
49
|
+
harness.child.kill("SIGKILL");
|
|
50
|
+
try { await expect(request).rejects.toMatchObject({ code: "transport_closed" }); }
|
|
51
|
+
finally { harness.client.close(); await harness.child.exited; }
|
|
52
|
+
}, 5000);
|
|
53
|
+
|
|
54
|
+
test("server recovers from a malformed JSON line, keeps stdout clean and exits on EOF", async () => {
|
|
55
|
+
const home = mkdtempSync(join(tmpdir(), "harness-stdio-test-"));
|
|
56
|
+
const child = Bun.spawn([process.execPath, new URL("../src/interfaces/cli/main.ts", import.meta.url).pathname, "serve", "--stdio", "--home", home], { stdin: "pipe", stdout: "pipe", stderr: "pipe" });
|
|
57
|
+
const output = new Response(child.stdout).text(), errors = new Response(child.stderr).text();
|
|
58
|
+
const timer = setTimeout(() => child.kill("SIGKILL"), 2000);
|
|
59
|
+
try {
|
|
60
|
+
child.stdin.write('bad json\n{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":1,"host":{"name":"test","version":"0"},"callbacks":[],"subscription":{"text":"final","tools":"full"}}}\n');
|
|
61
|
+
await child.stdin.end();
|
|
62
|
+
expect(await child.exited).toBe(0);
|
|
63
|
+
const messages = (await output).trim().split("\n").map(line => JSON.parse(line));
|
|
64
|
+
expect(messages).toHaveLength(2);
|
|
65
|
+
expect(messages[0]).toMatchObject({ id: null, error: { code: -32700 } });
|
|
66
|
+
expect(messages[1]).toMatchObject({ id: 1, result: { protocolVersion: 1 } });
|
|
67
|
+
expect(await errors).toBe("");
|
|
68
|
+
} finally { clearTimeout(timer); child.kill(); rmSync(home, { recursive: true, force: true }); }
|
|
69
|
+
}, 5000);
|
|
70
|
+
|
|
71
|
+
test("stdio rejects duplicate config fields with a field-specific validation error", async () => {
|
|
72
|
+
const home = mkdtempSync(join(tmpdir(), "harness-stdio-test-"));
|
|
73
|
+
const child = Bun.spawn([process.execPath, new URL("../src/interfaces/cli/main.ts", import.meta.url).pathname, "serve", "--stdio", "--home", home], { stdin: "pipe", stdout: "pipe", stderr: "pipe" });
|
|
74
|
+
const output = new Response(child.stdout).text(), timer = setTimeout(() => child.kill("SIGKILL"), 2000);
|
|
75
|
+
try {
|
|
76
|
+
child.stdin.write('{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":1,"host":{"name":"test","version":"0"},"callbacks":[],"subscription":{"text":"final","tools":"full"}}}\n');
|
|
77
|
+
child.stdin.write('{"jsonrpc":"2.0","id":2,"method":"config/validate","params":{"config":{"version":1,"version":1}}}\n');
|
|
78
|
+
await child.stdin.end(); expect(await child.exited).toBe(0);
|
|
79
|
+
const replies = (await output).trim().split("\n").map(line => JSON.parse(line));
|
|
80
|
+
expect(replies.find(reply => reply.id === 2)).toMatchObject({ error: { data: { code: "invalid_config" }, message: expect.stringContaining("config.version") } });
|
|
81
|
+
} finally { clearTimeout(timer); child.kill(); rmSync(home, { recursive: true, force: true }); }
|
|
82
|
+
}, 5000);
|
|
83
|
+
|
|
84
|
+
test("EOF shutdown fails within its grace period when a request already read never settles", async () => {
|
|
85
|
+
const input = new ReadableStream<Uint8Array>({ start(controller) {
|
|
86
|
+
controller.enqueue(new TextEncoder().encode('{"jsonrpc":"2.0","id":1,"method":"session/suspend","params":{}}\n')); controller.close(); } });
|
|
87
|
+
await expect(serveProtocolStdio({ subscribe: () => () => {},
|
|
88
|
+
request: request => request.method === "session/stop" ? Promise.resolve({ jsonrpc: "2.0" as const, id: request.id, result: {} }) : new Promise(() => {}) },
|
|
89
|
+
input, async () => {}, 20)).rejects.toThrow("shutdown grace expired");
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
test("transport closure and writer failure promptly reject outstanding requests", async () => {
|
|
93
|
+
const local = createInProcessTransport({ subscribe: () => () => {}, request: () => new Promise(() => {}) });
|
|
94
|
+
const client = new HarnessClient(local, 1000);
|
|
95
|
+
const pending = client.idle(); local.close();
|
|
96
|
+
await expect(pending).rejects.toMatchObject({ code: "transport_closed" });
|
|
97
|
+
const broken = new HarnessClient(createStdioTransport({ readable: new ReadableStream(),
|
|
98
|
+
async write() { throw new Error("broken pipe"); }, end() {},
|
|
99
|
+
}), 1000);
|
|
100
|
+
await expect(broken.idle()).rejects.toMatchObject({ code: "transport_closed" });
|
|
101
|
+
client.close(); broken.close();
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
test("EOF stops the session instead of draining it, and flushes the interruption before returning", async () => {
|
|
105
|
+
const frames: string[] = [], methods: string[] = [];
|
|
106
|
+
let publish!: (message: any) => void;
|
|
107
|
+
const endpoint = { subscribe(listener: (message: any) => void) { publish = listener; return () => {}; },
|
|
108
|
+
request: async (request: any) => {
|
|
109
|
+
methods.push(request.method);
|
|
110
|
+
// Stop publishes the interruption of the turn that was still running.
|
|
111
|
+
if (request.method === "session/stop") publish({ jsonrpc: "2.0", method: "channel/event", params: { channel: "a", event: { type: "turn_ended", turnId: "1", outcome: "interrupted" } } });
|
|
112
|
+
return { jsonrpc: "2.0" as const, id: request.id, result: {} };
|
|
113
|
+
} };
|
|
114
|
+
await serveProtocolStdio(endpoint, new ReadableStream({ start(c) { c.close(); } }), async bytes => { frames.push(new TextDecoder().decode(bytes)); }, 1000);
|
|
115
|
+
// No idle polling, and the stop request's own response is not written to the client.
|
|
116
|
+
expect(methods).toEqual(["session/stop"]);
|
|
117
|
+
expect(frames).toHaveLength(1);
|
|
118
|
+
expect(JSON.parse(frames[0]!).params.event).toMatchObject({ type: "turn_ended", outcome: "interrupted" });
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
test("unterminated stdin frame produces a nonzero exit with diagnostics only on stderr", async () => {
|
|
122
|
+
const home = mkdtempSync(join(tmpdir(), "harness-stdio-test-"));
|
|
123
|
+
const child = Bun.spawn([process.execPath, new URL("../src/interfaces/cli/main.ts", import.meta.url).pathname, "serve", "--stdio", "--home", home], { stdin: "pipe", stdout: "pipe", stderr: "pipe" });
|
|
124
|
+
const output = new Response(child.stdout).text(), errors = new Response(child.stderr).text();
|
|
125
|
+
const timer = setTimeout(() => child.kill("SIGKILL"), 2000);
|
|
126
|
+
try {
|
|
127
|
+
child.stdin.write('{"jsonrpc":'); await child.stdin.end();
|
|
128
|
+
expect(await child.exited).toBe(1);
|
|
129
|
+
expect(await output).toBe("");
|
|
130
|
+
expect(await errors).toContain("Incomplete protocol frame");
|
|
131
|
+
} finally { clearTimeout(timer); child.kill(); rmSync(home, { recursive: true, force: true }); }
|
|
132
|
+
}, 5000);
|