@qlairoslabs/acp-client 0.13.0-alpha.1 → 0.13.2-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/CONSUMER_GUIDE.md +181 -8
- package/MIGRATION-0.12-to-0.13.md +20 -7
- package/package.json +13 -2
- package/src/agent/config.ts +2 -0
- package/src/agent/conversation-manager.ts +1 -0
- package/src/agent/native-operations.ts +40 -0
- package/src/agent-session-config.ts +2 -0
- package/src/agent-session.ts +12 -2
- package/src/agent.ts +100 -2
- package/src/client.ts +5 -3
- package/src/controller-connection.ts +1 -0
- package/src/controller.ts +2 -0
- package/src/index.ts +3 -0
- package/src/launcher.ts +2 -0
- package/src/native/approvals.ts +20 -3
- package/src/native/capabilities.ts +2 -2
- package/src/native/connection.ts +1 -1
- package/src/native/controller.ts +5 -3
- package/src/native/host-config.ts +2 -0
- package/src/native/host.ts +83 -4
- package/src/native/mcp.ts +47 -0
- package/src/native/recovery.ts +8 -7
- package/src/native/reminders/routine-guard.ts +4 -3
- package/src/native/reminders/routine-wire.ts +1 -1
- package/src/native-harness/README.md +58 -1
- package/src/native-harness/package.json +4 -0
- package/src/native-harness/scripts/build-routine-zones.py +47 -0
- package/src/native-harness/src/capabilities/attachments/admission.ts +42 -0
- package/src/native-harness/src/capabilities/attachments/image-validation.ts +98 -0
- package/src/native-harness/src/capabilities/attachments/input.ts +1 -0
- package/src/native-harness/src/capabilities/attachments/read.ts +76 -0
- package/src/native-harness/src/capabilities/attachments/recovery.ts +26 -0
- package/src/native-harness/src/capabilities/attachments/selection.ts +37 -0
- package/src/native-harness/src/capabilities/attachments/service.ts +75 -0
- package/src/native-harness/src/capabilities/attachments/upload.ts +164 -0
- package/src/native-harness/src/capabilities/attachments/vision.ts +45 -0
- package/src/native-harness/src/capabilities/context/build.ts +5 -0
- package/src/native-harness/src/capabilities/context/session.ts +2 -1
- package/src/native-harness/src/capabilities/model/openrouter-stream.ts +80 -17
- package/src/native-harness/src/capabilities/model/runner.ts +6 -3
- package/src/native-harness/src/capabilities/reminders/cron-calendar.ts +50 -0
- package/src/native-harness/src/capabilities/reminders/cron-fields.ts +46 -0
- package/src/native-harness/src/capabilities/reminders/cron-proof.ts +32 -0
- package/src/native-harness/src/capabilities/reminders/cron-zone.ts +53 -0
- package/src/native-harness/src/capabilities/reminders/iana-zones.json.gz +0 -0
- package/src/native-harness/src/capabilities/reminders/iana-zones.ts +34 -0
- package/src/native-harness/src/capabilities/reminders/interval.ts +30 -0
- package/src/native-harness/src/capabilities/reminders/routine-advance.ts +47 -0
- package/src/native-harness/src/capabilities/reminders/routine-backlog.ts +47 -0
- package/src/native-harness/src/capabilities/reminders/routine-expiry.ts +15 -0
- package/src/native-harness/src/capabilities/reminders/routine-occurrence.ts +65 -0
- package/src/native-harness/src/capabilities/reminders/routine-schedule.ts +70 -0
- package/src/native-harness/src/capabilities/reminders/unattended.ts +8 -0
- package/src/native-harness/src/capabilities/tools/mcp/client.ts +97 -0
- package/src/native-harness/src/capabilities/tools/mcp/http.ts +183 -0
- package/src/native-harness/src/capabilities/tools/mcp/registry.ts +62 -0
- package/src/native-harness/src/capabilities/tools/mcp/result.ts +33 -0
- package/src/native-harness/src/capabilities/tools/mcp/schema.ts +77 -0
- package/src/native-harness/src/capabilities/tools/mcp/secrets.ts +28 -0
- package/src/native-harness/src/capabilities/tools/mcp/stdio.ts +105 -0
- package/src/native-harness/src/capabilities/tools/reminders/index.ts +27 -5
- package/src/native-harness/src/capabilities/tools/runtime/invocations.ts +4 -1
- package/src/native-harness/src/capabilities/tools/web/config.ts +1 -0
- package/src/native-harness/src/capabilities/tools/web/definitions.ts +18 -0
- package/src/native-harness/src/capabilities/tools/web/direct-address.ts +56 -0
- package/src/native-harness/src/capabilities/tools/web/direct-transport.ts +235 -0
- package/src/native-harness/src/capabilities/tools/web/direct.ts +149 -0
- package/src/native-harness/src/capabilities/tools/web/exa-transport.ts +41 -0
- package/src/native-harness/src/capabilities/tools/web/exa.ts +71 -0
- package/src/native-harness/src/capabilities/tools/web/preview.ts +44 -0
- package/src/native-harness/src/capabilities/tools/web/registry.ts +47 -0
- package/src/native-harness/src/capabilities/tools/web/usage.ts +1 -0
- package/src/native-harness/src/contracts/agent.ts +3 -1
- package/src/native-harness/src/contracts/attachments.ts +62 -0
- package/src/native-harness/src/contracts/config-permissions.ts +14 -3
- package/src/native-harness/src/contracts/context.ts +1 -1
- package/src/native-harness/src/contracts/events.ts +2 -2
- package/src/native-harness/src/contracts/loop.ts +3 -3
- package/src/native-harness/src/contracts/mcp.ts +22 -0
- package/src/native-harness/src/contracts/model-execution.ts +2 -1
- package/src/native-harness/src/contracts/operator-controls.ts +8 -1
- package/src/native-harness/src/contracts/protocol.ts +21 -5
- package/src/native-harness/src/contracts/reminders.ts +4 -2
- package/src/native-harness/src/contracts/routines.ts +20 -0
- package/src/native-harness/src/contracts/session-file.ts +2 -0
- package/src/native-harness/src/contracts/session.ts +8 -3
- package/src/native-harness/src/contracts/storage.ts +32 -13
- package/src/native-harness/src/contracts/tools.ts +2 -0
- package/src/native-harness/src/contracts/web.ts +16 -0
- package/src/native-harness/src/governance/permissions/gate.ts +9 -2
- package/src/native-harness/src/governance/policy/resolve.ts +6 -1
- package/src/native-harness/src/index.ts +48 -12
- package/src/native-harness/src/interfaces/cli/development.ts +108 -53
- package/src/native-harness/src/interfaces/cli/serve.ts +2 -2
- package/src/native-harness/src/interfaces/cli/start.ts +22 -13
- package/src/native-harness/src/interfaces/client.ts +8 -2
- package/src/native-harness/src/interfaces/inspector/index.html +7 -1
- package/src/native-harness/src/interfaces/inspector/state.ts +6 -2
- package/src/native-harness/src/interfaces/protocol/load.ts +3 -1
- package/src/native-harness/src/interfaces/protocol/local.ts +13 -4
- package/src/native-harness/src/interfaces/protocol/operator.ts +8 -2
- package/src/native-harness/src/interfaces/protocol/server.ts +102 -36
- package/src/native-harness/src/interfaces/protocol/stdio-client.ts +8 -0
- package/src/native-harness/src/interfaces/web-chat/index.html +65 -12
- package/src/native-harness/src/interfaces/web-chat/server.ts +30 -3
- package/src/native-harness/src/kernel/channels.ts +27 -15
- package/src/native-harness/src/kernel/loop.ts +1 -1
- package/src/native-harness/src/runtime/config/inference-key.ts +7 -0
- package/src/native-harness/src/runtime/config/managed.ts +1 -1
- package/src/native-harness/src/runtime/config/paths.ts +8 -1
- package/src/native-harness/src/runtime/execution/mcp-stdio.ts +31 -0
- package/src/native-harness/src/runtime/reminder-validation.ts +31 -10
- package/src/native-harness/src/runtime/reminders.ts +2 -2
- package/src/native-harness/src/state/storage/archive.ts +113 -12
- package/src/native-harness/src/state/storage/attachment-files.ts +58 -0
- package/src/native-harness/src/state/storage/attachment-import.ts +106 -0
- package/src/native-harness/src/state/storage/attachment-tables.ts +123 -0
- package/src/native-harness/src/state/storage/engine.ts +396 -45
- package/src/native-harness/src/state/storage/inference.ts +30 -2
- package/src/native-harness/src/state/storage/memory-attachments.ts +56 -0
- package/src/native-harness/src/state/storage/memory-reminders.ts +31 -6
- package/src/native-harness/src/state/storage/memory.ts +32 -4
- package/src/native-harness/src/state/storage/reminder-archive.ts +53 -12
- package/src/native-harness/src/state/storage/reminder-change.ts +60 -14
- package/src/native-harness/src/state/storage/reminder-tables.ts +11 -3
- package/src/native-harness/src/state/storage/routine-row.ts +25 -0
- package/src/native-harness/src/state/storage/session-file.ts +22 -9
- package/src/native-harness/src/state/storage/sqlite-reminders.ts +48 -8
- package/src/native-harness/src/state/storage/sqlite.ts +59 -11
- package/src/native-harness/src/state/storage/web-tables.ts +47 -0
- package/src/native-harness/src/testing.ts +151 -56
- package/src/native-harness/src/util/attachment-input.ts +42 -0
- package/src/native-harness/src/util/config.ts +64 -9
- package/src/native-harness/src/util/intake.ts +7 -0
- package/src/native-harness/src/util/mcp-config.ts +102 -0
- package/src/native-harness/src/util/web-config.ts +34 -0
- package/src/native-harness/src/util/web-usage.ts +33 -0
- package/src/orchestrator/permissions.ts +6 -3
- package/src/permissions/conversation.ts +13 -5
- package/src/permissions/matcher.ts +2 -0
- package/src/permissions/outcome.ts +12 -1
- package/src/permissions/policy.ts +13 -8
- package/src/steering/combine.ts +1 -0
- package/src/steering/contracts.ts +3 -0
- package/src/terminals.ts +9 -6
- package/src/native/reminders/reminders.test.ts +0 -553
- package/src/native/reminders/routine-config.typecheck.ts +0 -13
- package/src/native/reminders/wire.typecheck.ts +0 -13
- package/src/native-harness/.res/README.md +0 -1
- package/src/native-harness/.res/architecture/README.md +0 -21
- package/src/native-harness/.res/architecture/context-and-data.md +0 -31
- package/src/native-harness/.res/architecture/execution.md +0 -42
- package/src/native-harness/.res/architecture/overview.md +0 -44
- package/src/native-harness/.res/architecture/persistence.md +0 -56
- package/src/native-harness/.res/architecture/suspend-and-resume.md +0 -45
- package/src/native-harness/.res/architecture/technical-architecture.md +0 -50
- package/src/native-harness/.res/decisions/0001-basic-loop.md +0 -11
- package/src/native-harness/.res/decisions/0002-channel-input.md +0 -17
- package/src/native-harness/.res/decisions/0003-inference-inspection.md +0 -14
- package/src/native-harness/.res/decisions/0004-shared-context.md +0 -21
- package/src/native-harness/.res/decisions/0005-notice-inbox.md +0 -20
- package/src/native-harness/.res/decisions/0006-explicit-notices.md +0 -12
- package/src/native-harness/.res/decisions/0007-channel-output.md +0 -12
- package/src/native-harness/.res/decisions/0008-browser-client.md +0 -16
- package/src/native-harness/.res/decisions/0009-scripted-model.md +0 -18
- package/src/native-harness/.res/decisions/0010-inprocess-protocol.md +0 -19
- package/src/native-harness/.res/decisions/0011-stdio-transport.md +0 -21
- package/src/native-harness/.res/decisions/0012-session-storage-contract.md +0 -37
- package/src/native-harness/.res/decisions/0013-durable-delivery-contract.md +0 -64
- package/src/native-harness/.res/decisions/0014-channel-controls-contract.md +0 -73
- package/src/native-harness/.res/decisions/0015-session-file-contract.md +0 -72
- package/src/native-harness/.res/decisions/0016-configuration-permissions.md +0 -159
- package/src/native-harness/.res/decisions/0017-openrouter-managed-config.md +0 -69
- package/src/native-harness/.res/decisions/0018-tool-dispatch-evidence.md +0 -78
- package/src/native-harness/.res/decisions/0019-model-execution.md +0 -188
- package/src/native-harness/.res/decisions/0020-startup-channel-changes.md +0 -71
- package/src/native-harness/.res/decisions/0021-tools-and-host-callbacks.md +0 -100
- package/src/native-harness/.res/decisions/0022-operator-controls.md +0 -115
- package/src/native-harness/.res/decisions/0023-long-running-mock.md +0 -82
- package/src/native-harness/.res/decisions/0024-callback-policy-names.md +0 -46
- package/src/native-harness/.res/decisions/0025-external-notices.md +0 -364
- package/src/native-harness/.res/decisions/0026-bounded-context.md +0 -81
- package/src/native-harness/.res/decisions/0027-development-instructions.md +0 -22
- package/src/native-harness/.res/decisions/0028-debug-tools.md +0 -16
- package/src/native-harness/.res/decisions/0029-bounded-model-call-pages.md +0 -37
- package/src/native-harness/.res/decisions/0030-timeline-retrieval.md +0 -145
- package/src/native-harness/.res/decisions/0031-one-off-reminders.md +0 -173
- package/src/native-harness/.res/docs/11-15.md +0 -148
- package/src/native-harness/.res/docs/BUSINESS_RULES.md +0 -111
- package/src/native-harness/.res/docs/WORK.md +0 -65
- package/src/native-harness/.res/docs/_/11-16-persistence/LOG.md +0 -312
- package/src/native-harness/.res/docs/_/11-16-persistence/PLAN.md +0 -77
- package/src/native-harness/.res/docs/_/11-16-persistence/SPEC.md +0 -313
- package/src/native-harness/.res/docs/_/11-19-channel-controls/LOG.md +0 -160
- package/src/native-harness/.res/docs/_/11-19-channel-controls/PLAN.md +0 -89
- package/src/native-harness/.res/docs/_/11-19-channel-controls/SPEC.md +0 -135
- package/src/native-harness/.res/docs/_/11-19-config-permissions/LOG.md +0 -143
- package/src/native-harness/.res/docs/_/11-19-config-permissions/PLAN.md +0 -93
- package/src/native-harness/.res/docs/_/11-19-config-permissions/SPEC.md +0 -181
- package/src/native-harness/.res/docs/acp-client-integration/0001-DESIGN/11-20-LOG.md +0 -433
- package/src/native-harness/.res/docs/acp-client-integration/0001-DESIGN/PLAN.md +0 -60
- package/src/native-harness/.res/docs/acp-client-integration/0001-DESIGN/PROTOCOL.md +0 -141
- package/src/native-harness/.res/docs/acp-client-integration/0001-DESIGN/SPEC.md +0 -168
- package/src/native-harness/.res/docs/attachments/0001-DESIGN/SPEC.md +0 -111
- package/src/native-harness/.res/docs/bounded-context/0001-DESIGN/PLAN.md +0 -129
- package/src/native-harness/.res/docs/bounded-context/0001-DESIGN/SPEC.md +0 -135
- package/src/native-harness/.res/docs/channel-messaging/0001-DESIGN-external-notices/LOG.md +0 -437
- package/src/native-harness/.res/docs/channel-messaging/0001-DESIGN-external-notices/PLAN.md +0 -127
- package/src/native-harness/.res/docs/channel-messaging/0001-DESIGN-external-notices/SPEC.md +0 -203
- package/src/native-harness/.res/docs/long-running-mock/0001-DESIGN/LOG.md +0 -292
- package/src/native-harness/.res/docs/long-running-mock/0001-DESIGN/PLAN.md +0 -91
- package/src/native-harness/.res/docs/long-running-mock/0001-DESIGN/SPEC.md +0 -187
- package/src/native-harness/.res/docs/model-execution/0001-DESIGN/LOG.md +0 -148
- package/src/native-harness/.res/docs/model-execution/0001-DESIGN/PLAN.md +0 -95
- package/src/native-harness/.res/docs/model-execution/0001-DESIGN/SPEC.md +0 -200
- package/src/native-harness/.res/docs/model-execution/0002-AMENDMENT-accounting/SPEC.md +0 -80
- package/src/native-harness/.res/docs/operator-controls/0001-DESIGN/LOG.md +0 -206
- package/src/native-harness/.res/docs/operator-controls/0001-DESIGN/PLAN.md +0 -106
- package/src/native-harness/.res/docs/operator-controls/0001-DESIGN/SPEC.md +0 -297
- package/src/native-harness/.res/docs/persistence/0002-AMENDMENT-session-file/LOG.md +0 -41
- package/src/native-harness/.res/docs/persistence/0002-AMENDMENT-session-file/PLAN.md +0 -36
- package/src/native-harness/.res/docs/persistence/0002-AMENDMENT-session-file/SPEC.md +0 -84
- package/src/native-harness/.res/docs/shell-execution/0001-DESIGN/SPEC.md +0 -147
- package/src/native-harness/.res/docs/timeline-retrieval/0001-DESIGN/LOG.md +0 -15
- package/src/native-harness/.res/docs/timeline-retrieval/0001-DESIGN/PLAN.md +0 -107
- package/src/native-harness/.res/docs/timeline-retrieval/0001-DESIGN/SPEC.md +0 -114
- package/src/native-harness/.res/docs/todos/11-19-TODO-auto_review-approve.md +0 -24
- package/src/native-harness/.res/docs/todos/11-20-TODO-user-entered-tool-code.md +0 -23
- package/src/native-harness/.res/docs/todos/11-21-TODO-node-resource-coordination.md +0 -103
- package/src/native-harness/.res/docs/todos/11-22-TODO-harness-m1-sequence.md +0 -50
- package/src/native-harness/.res/docs/tools-and-callbacks/0001-DESIGN/LOG.md +0 -337
- package/src/native-harness/.res/docs/tools-and-callbacks/0001-DESIGN/PLAN.md +0 -76
- package/src/native-harness/.res/docs/tools-and-callbacks/0001-DESIGN/SPEC.md +0 -204
- package/src/native-harness/.res/docs/tools-and-callbacks/0002-AMENDMENT-callback-policy/LOG.md +0 -73
- package/src/native-harness/.res/docs/tools-and-callbacks/0002-AMENDMENT-callback-policy/SPEC.md +0 -64
- package/src/native-harness/.res/docs/tools-and-callbacks/0003-DESIGN-attachment-tools/SPEC.md +0 -58
- package/src/native-harness/.res/docs/tools-and-callbacks/0004-DESIGN-reminder-tools/LOG.md +0 -45
- package/src/native-harness/.res/docs/tools-and-callbacks/0004-DESIGN-reminder-tools/PLAN.md +0 -108
- package/src/native-harness/.res/docs/tools-and-callbacks/0004-DESIGN-reminder-tools/SPEC.md +0 -169
- package/src/native-harness/.res/docs/web-tools/0001-DESIGN/SPEC.md +0 -128
- package/src/native-harness/.res/marketing-draft.md +0 -22
- package/src/native-harness/.res/sandbox/1-simple-harness/README.md +0 -285
- package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-1.md +0 -19
- package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-10.md +0 -138
- package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-11.md +0 -148
- package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-12.md +0 -172
- package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-13.md +0 -194
- package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-14.md +0 -233
- package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-15.md +0 -249
- package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-16.md +0 -265
- package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-17.md +0 -279
- package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-18.md +0 -293
- package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-19.md +0 -307
- package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-2.md +0 -29
- package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-20.md +0 -317
- package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-21.md +0 -327
- package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-3.md +0 -40
- package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-4.md +0 -59
- package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-5.md +0 -69
- package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-6.md +0 -79
- package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-7.md +0 -89
- package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-8.md +0 -102
- package/src/native-harness/.res/sandbox/1-simple-harness/conversation/context-9.md +0 -116
- package/src/native-harness/.res/sandbox/1-simple-harness/conversation.jsonl +0 -49
- package/src/native-harness/.res/sandbox/1-simple-harness/doc.md +0 -87
- package/src/native-harness/.res/sandbox/1-simple-harness/harness.py +0 -289
- package/src/native-harness/.res/sandbox/1-simple-harness/local/harness copy 2.py +0 -221
- package/src/native-harness/.res/sandbox/1-simple-harness/local/harness copy 3.py +0 -287
- package/src/native-harness/.res/sandbox/1-simple-harness/local/harness copy.py +0 -165
- package/src/native-harness/.res/sandbox/1-simple-harness/state.json +0 -22
- package/src/native-harness/.res/sandbox/1-simple-harness/system-prompt.md +0 -2
- package/src/native-harness/AGENTS.md +0 -70
- package/src/native-harness/ARCHITECTURE.md +0 -249
- package/src/native-harness/bun.lock +0 -26
- package/src/native-harness/test/agent.test.ts +0 -130
- package/src/native-harness/test/approval-gate.test.ts +0 -255
- package/src/native-harness/test/approval-protocol.test.ts +0 -94
- package/src/native-harness/test/boundaries.test.ts +0 -18
- package/src/native-harness/test/channel-controls.test.ts +0 -322
- package/src/native-harness/test/client-abort.test.ts +0 -136
- package/src/native-harness/test/config-policy.test.ts +0 -301
- package/src/native-harness/test/conformance/storage.ts +0 -1141
- package/src/native-harness/test/context.test.ts +0 -177
- package/src/native-harness/test/crash.test.ts +0 -187
- package/src/native-harness/test/debug-tools.test.ts +0 -67
- package/src/native-harness/test/development-cli.test.ts +0 -67
- package/src/native-harness/test/development.test.ts +0 -171
- package/src/native-harness/test/dispatch-evidence.test.ts +0 -58
- package/src/native-harness/test/e2e.test.ts +0 -186
- package/src/native-harness/test/external-notices.test.ts +0 -101
- package/src/native-harness/test/fixtures/approval-e2e.jsonl +0 -4
- package/src/native-harness/test/fixtures/crash-scenario.jsonl +0 -5
- package/src/native-harness/test/fixtures/development-lifecycle.jsonl +0 -10
- package/src/native-harness/test/fixtures/echo-scenario.jsonl +0 -3
- package/src/native-harness/test/fixtures/format2-schema.ts +0 -27
- package/src/native-harness/test/fixtures/format3-schema.ts +0 -16
- package/src/native-harness/test/fixtures/hold-scenario.jsonl +0 -4
- package/src/native-harness/test/fixtures/model-e2e.jsonl +0 -3
- package/src/native-harness/test/fixtures/storage-owner.ts +0 -20
- package/src/native-harness/test/format-reply.test.ts +0 -10
- package/src/native-harness/test/helpers/operator-view.ts +0 -41
- package/src/native-harness/test/helpers/scenario.ts +0 -9
- package/src/native-harness/test/helpers/session.ts +0 -38
- package/src/native-harness/test/helpers/stdio.ts +0 -46
- package/src/native-harness/test/inspector.test.ts +0 -77
- package/src/native-harness/test/listeners.test.ts +0 -49
- package/src/native-harness/test/loop.test.ts +0 -88
- package/src/native-harness/test/model-channel-double.test.ts +0 -87
- package/src/native-harness/test/model-crash.test.ts +0 -62
- package/src/native-harness/test/model-delivery-regressions.test.ts +0 -55
- package/src/native-harness/test/model-e2e.test.ts +0 -71
- package/src/native-harness/test/model-final-filter.test.ts +0 -30
- package/src/native-harness/test/model-large-reply.test.ts +0 -28
- package/src/native-harness/test/model-live-queue.test.ts +0 -33
- package/src/native-harness/test/model-runner.test.ts +0 -117
- package/src/native-harness/test/model-storage.test.ts +0 -99
- package/src/native-harness/test/model-stream-adapter.test.ts +0 -26
- package/src/native-harness/test/model-stream-public.test.ts +0 -77
- package/src/native-harness/test/notices.test.ts +0 -64
- package/src/native-harness/test/openrouter-config.test.ts +0 -218
- package/src/native-harness/test/operator-controls.test.ts +0 -132
- package/src/native-harness/test/output.test.ts +0 -44
- package/src/native-harness/test/owner-config.test.ts +0 -64
- package/src/native-harness/test/permissions-e2e.test.ts +0 -68
- package/src/native-harness/test/persistence.test.ts +0 -236
- package/src/native-harness/test/portability.test.ts +0 -337
- package/src/native-harness/test/protocol.test.ts +0 -160
- package/src/native-harness/test/reminder-clock.test.ts +0 -61
- package/src/native-harness/test/reminder-stdio.test.ts +0 -140
- package/src/native-harness/test/reminder-storage.test.ts +0 -124
- package/src/native-harness/test/scenario-jsonl.test.ts +0 -112
- package/src/native-harness/test/scripted.test.ts +0 -92
- package/src/native-harness/test/session-file.test.ts +0 -51
- package/src/native-harness/test/startup-channel-changes.test.ts +0 -256
- package/src/native-harness/test/stdio.test.ts +0 -132
- package/src/native-harness/test/storage-memory.test.ts +0 -4
- package/src/native-harness/test/storage-sqlite.test.ts +0 -420
- package/src/native-harness/test/suspension.test.ts +0 -430
- package/src/native-harness/test/timeline-retrieval.test.ts +0 -211
- package/src/native-harness/test/tools-catalog.test.ts +0 -468
- package/src/native-harness/test/tools-protocol.test.ts +0 -418
- package/src/native-harness/test/transfers.test.ts +0 -94
- package/src/native-harness/test/web-chat.test.ts +0 -35
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { cronCivilCalendar } from "./cron-calendar.ts";
|
|
2
|
+
import { parseRoutineCron } from "./cron-fields.ts";
|
|
3
|
+
import { zonedCalendar } from "./cron-zone.ts";
|
|
4
|
+
import type { RoutineCalendar, RoutineSchedule } from "../../contracts/routines.ts";
|
|
5
|
+
import { reminderInstant, REMINDER_LIMITS } from "../../util/reminder-schedule.ts";
|
|
6
|
+
import { proveRoutineCron } from "./cron-proof.ts";
|
|
7
|
+
import { resolveRoutineZone } from "./iana-zones.ts";
|
|
8
|
+
import { intervalCalendar } from "./interval.ts";
|
|
9
|
+
|
|
10
|
+
const invalid = { kind: "invalid_schedule" } as const;
|
|
11
|
+
|
|
12
|
+
function limitCalendar(calendar: RoutineCalendar, first: number, count: number | undefined, until: number | undefined): RoutineCalendar | undefined {
|
|
13
|
+
const end = until === undefined ? Infinity : calendar.rank(until);
|
|
14
|
+
const size = Math.min(end - first, count ?? Infinity);
|
|
15
|
+
if (size <= 0 || calendar.at(first) === undefined) return;
|
|
16
|
+
return {
|
|
17
|
+
at: ordinal => Number.isSafeInteger(ordinal) && ordinal >= 0 && ordinal < size ? calendar.at(first + ordinal) : undefined,
|
|
18
|
+
rank: through => Math.max(0, Math.min(size, calendar.rank(through) - first)),
|
|
19
|
+
nextCalendarMatch: instant => calendar.nextCalendarMatch(instant),
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function prepareRoutineSchedule(schedule: RoutineSchedule, acceptedAt: string, now: number, processedThrough?: number) {
|
|
24
|
+
const anchor = reminderInstant(acceptedAt);
|
|
25
|
+
if (anchor === undefined || anchor > now + 30_000) return invalid;
|
|
26
|
+
if (schedule.count !== undefined && (schedule.until !== undefined || !Number.isSafeInteger(schedule.count) || schedule.count < 1)) return invalid;
|
|
27
|
+
const until = schedule.until === undefined ? undefined : reminderInstant(schedule.until);
|
|
28
|
+
if (schedule.until !== undefined && until === undefined) return invalid;
|
|
29
|
+
|
|
30
|
+
if ("every" in schedule) {
|
|
31
|
+
const calendar = intervalCalendar(schedule, anchor, processedThrough);
|
|
32
|
+
if (!calendar) return invalid;
|
|
33
|
+
const start = schedule.startAt === undefined ? anchor + schedule.every : reminderInstant(schedule.startAt)!;
|
|
34
|
+
return { kind: "valid", calendar, schedule: { ...schedule, startAt: new Date(start).toISOString() } } as const;
|
|
35
|
+
}
|
|
36
|
+
const zone = resolveRoutineZone(schedule.timezone);
|
|
37
|
+
if (!zone) return invalid;
|
|
38
|
+
const proof = proveRoutineCron(schedule.cron, zone);
|
|
39
|
+
if (proof.kind !== "valid") return proof;
|
|
40
|
+
const { calendar } = proof;
|
|
41
|
+
const first = calendar.at(calendar.rank(anchor));
|
|
42
|
+
if (first === undefined || first - anchor < REMINDER_LIMITS.minIntervalMs) return invalid;
|
|
43
|
+
const bounded = limitCalendar(calendar, calendar.rank(Math.max(anchor, processedThrough ?? anchor)), schedule.count, until);
|
|
44
|
+
return bounded
|
|
45
|
+
? { kind: "valid", calendar: bounded, schedule: { ...schedule, timezone: zone.timezone }, timezoneVersion: zone.timezoneVersion } as const
|
|
46
|
+
: invalid;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** An owner transaction can advance many rows or steps using one index per
|
|
50
|
+
* admitted cron. Existing schedules need evaluation, not a timed proof. */
|
|
51
|
+
export function createRoutineEvaluator() {
|
|
52
|
+
const calendars = new Map<string, RoutineCalendar | undefined>();
|
|
53
|
+
return (schedule: RoutineSchedule, acceptedAt: string, timezoneVersion: string | undefined, processedThrough?: number) => {
|
|
54
|
+
const anchor = reminderInstant(acceptedAt);
|
|
55
|
+
if (anchor === undefined) return;
|
|
56
|
+
if ("every" in schedule) return intervalCalendar(schedule, anchor, processedThrough);
|
|
57
|
+
const key = JSON.stringify([timezoneVersion, schedule.timezone, schedule.cron]);
|
|
58
|
+
if (!calendars.has(key)) {
|
|
59
|
+
const zone = resolveRoutineZone(schedule.timezone);
|
|
60
|
+
const fields = zone?.timezoneVersion === timezoneVersion ? parseRoutineCron(schedule.cron) : undefined;
|
|
61
|
+
const civil = fields && cronCivilCalendar(fields);
|
|
62
|
+
calendars.set(key, zone && civil ? zonedCalendar(civil, zone.start, zone.end, zone.initialOffset, zone.transitions) : undefined);
|
|
63
|
+
}
|
|
64
|
+
const calendar = calendars.get(key);
|
|
65
|
+
if (!calendar) return;
|
|
66
|
+
const until = schedule.until === undefined ? undefined : reminderInstant(schedule.until);
|
|
67
|
+
if (schedule.until !== undefined && until === undefined) return;
|
|
68
|
+
return limitCalendar(calendar, calendar.rank(Math.max(anchor, processedThrough ?? anchor)), schedule.count, until);
|
|
69
|
+
};
|
|
70
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** Use the same initial take when checking eligibility and charging a turn.
|
|
2
|
+
* Kept outside storage so memory and SQLite apply identical policy. */
|
|
3
|
+
export function unattendedAdmission(limit: number | undefined, spent: number, participantInInitialTake: boolean) {
|
|
4
|
+
if (limit === undefined) return { eligible: true, nextSpent: spent };
|
|
5
|
+
if (participantInInitialTake) return { eligible: true, nextSpent: 0 };
|
|
6
|
+
if (spent >= limit) return { eligible: false, nextSpent: spent };
|
|
7
|
+
return { eligible: true, nextSpent: spent + 1 };
|
|
8
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import type { JSONRPCMessage } from "@modelcontextprotocol/sdk/types.js";
|
|
2
|
+
import type { McpTool } from "../../../contracts/mcp.ts";
|
|
3
|
+
import { acceptsMcpSchema, matchesMcpArguments, mcpToolName } from "./schema.ts";
|
|
4
|
+
import { mcpSecrets } from "./secrets.ts";
|
|
5
|
+
import { projectMcpResult } from "./result.ts";
|
|
6
|
+
import { McpStdioTransport, type McpStdioProcess } from "./stdio.ts";
|
|
7
|
+
|
|
8
|
+
const encoder = new TextEncoder();
|
|
9
|
+
|
|
10
|
+
type Transport = import("@modelcontextprotocol/sdk/shared/transport.js").Transport;
|
|
11
|
+
export const supportedMcpVersions = new Set(["2025-03-26", "2025-06-18", "2025-11-25"]);
|
|
12
|
+
|
|
13
|
+
export function connectMcpStdio(name: string, child: McpStdioProcess, deadlineMs?: number, secrets?: readonly string[]) {
|
|
14
|
+
return connectMcp(name, async () => {
|
|
15
|
+
const { JSONRPCMessageSchema } = await import("@modelcontextprotocol/sdk/types.js");
|
|
16
|
+
const transport = new McpStdioTransport<JSONRPCMessage>(child, supportedMcpVersions, value => JSONRPCMessageSchema.parse(value));
|
|
17
|
+
return { transport, drain: () => transport.drain() };
|
|
18
|
+
}, deadlineMs, secrets, () => child.close());
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export async function connectMcp(name: string,
|
|
22
|
+
assemble: () => Promise<{ transport: Transport; drain(): Promise<void> }>,
|
|
23
|
+
deadlineMs = 1_800_000, secrets: readonly string[] = [], rollback?: () => Promise<void>) {
|
|
24
|
+
let transport: Transport | undefined;
|
|
25
|
+
const startup = AbortSignal.timeout(10_000);
|
|
26
|
+
try {
|
|
27
|
+
const redact = mcpSecrets(secrets);
|
|
28
|
+
const { Client } = await import("@modelcontextprotocol/sdk/client/index.js");
|
|
29
|
+
const { ErrorCode, McpError } = await import("@modelcontextprotocol/sdk/types.js");
|
|
30
|
+
const live = await assemble();
|
|
31
|
+
transport = live.transport;
|
|
32
|
+
const client = new Client({ name: "native-harness", version: "1" }, { capabilities: {} });
|
|
33
|
+
let ready = true;
|
|
34
|
+
const shutdown = new AbortController();
|
|
35
|
+
client.onclose = () => { ready = false; };
|
|
36
|
+
await client.connect(transport, { signal: startup, timeout: 10_000 });
|
|
37
|
+
if (!client.getServerCapabilities()?.tools) throw new Error("MCP tools unavailable");
|
|
38
|
+
const bindings = new Map<string, McpTool>();
|
|
39
|
+
const cursors = new Set<string>();
|
|
40
|
+
let bytes = 0;
|
|
41
|
+
let cursor: string | undefined;
|
|
42
|
+
for (let page = 0; ; page++) {
|
|
43
|
+
if (page === 4) throw new Error("MCP catalog exceeds page limit");
|
|
44
|
+
const list = await client.listTools(cursor === undefined ? undefined : { cursor }, { signal: startup, timeout: 10_000 });
|
|
45
|
+
for (const remote of list.tools) {
|
|
46
|
+
if (redact.contains(remote.name)) throw new Error("Unsafe MCP tool name");
|
|
47
|
+
redact.tree(remote.inputSchema, true);
|
|
48
|
+
const key = mcpToolName(name, remote.name);
|
|
49
|
+
const schema = remote.inputSchema;
|
|
50
|
+
if (!key || bindings.has(key) || bindings.size === 32 || !acceptsMcpSchema(schema)) throw new Error("Invalid MCP tool catalog");
|
|
51
|
+
const definition = { name: key, description: redact.text(remote.description ?? ""), parameters: schema };
|
|
52
|
+
bytes += encoder.encode(JSON.stringify(definition)).length;
|
|
53
|
+
if (bytes > 128 * 1024) throw new Error("MCP catalog exceeds byte limit");
|
|
54
|
+
bindings.set(key, {
|
|
55
|
+
definition,
|
|
56
|
+
available: () => ready,
|
|
57
|
+
accepts: args => encoder.encode(JSON.stringify(args)).length <= 16 * 1024 && matchesMcpArguments(schema, args),
|
|
58
|
+
async call(args, signal) {
|
|
59
|
+
const fail = (code: string) => ({ content: JSON.stringify({ ok: false, code }), code });
|
|
60
|
+
if (!ready) return fail("tool_unavailable");
|
|
61
|
+
if (signal.aborted) return fail("tool_interrupted");
|
|
62
|
+
try {
|
|
63
|
+
const result = await client.callTool({ name: remote.name, arguments: args }, undefined, {
|
|
64
|
+
timeout: deadlineMs, signal: AbortSignal.any([signal, shutdown.signal]), resetTimeoutOnProgress: false, onprogress() {},
|
|
65
|
+
});
|
|
66
|
+
if (result.isError) return fail("mcp_tool_error");
|
|
67
|
+
if (!Array.isArray(result.content)) return fail("mcp_protocol_error");
|
|
68
|
+
try {
|
|
69
|
+
return { content: projectMcpResult({
|
|
70
|
+
content: result.content,
|
|
71
|
+
structuredContent: redact.tree(result.structuredContent),
|
|
72
|
+
}, redact.text) };
|
|
73
|
+
} catch { return fail("mcp_protocol_error"); }
|
|
74
|
+
} catch (error) {
|
|
75
|
+
if (signal.aborted) return fail("tool_interrupted");
|
|
76
|
+
if (error instanceof McpError && error.code === ErrorCode.RequestTimeout) return fail("tool_timeout");
|
|
77
|
+
return fail(ready && error instanceof McpError && error.code !== ErrorCode.ConnectionClosed
|
|
78
|
+
? "mcp_protocol_error" : "mcp_uncertain");
|
|
79
|
+
}
|
|
80
|
+
},
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
cursor = list.nextCursor;
|
|
84
|
+
if (cursor === undefined) break;
|
|
85
|
+
if (cursors.has(cursor)) throw new Error("MCP catalog cursor repeated");
|
|
86
|
+
cursors.add(cursor);
|
|
87
|
+
}
|
|
88
|
+
return { name, bindings, bytes, available: () => ready, async close() {
|
|
89
|
+
shutdown.abort();
|
|
90
|
+
await live.drain();
|
|
91
|
+
await client.close();
|
|
92
|
+
} };
|
|
93
|
+
} catch {
|
|
94
|
+
await (transport?.close() ?? rollback?.());
|
|
95
|
+
throw new Error("MCP initialization failed");
|
|
96
|
+
}
|
|
97
|
+
}
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import { connectMcp, supportedMcpVersions } from "./client.ts";
|
|
2
|
+
|
|
3
|
+
const receiveLimit = 256 * 1024;
|
|
4
|
+
type Fetch = (...args: Parameters<typeof fetch>) => Promise<Response>;
|
|
5
|
+
|
|
6
|
+
export function connectMcpHttps(name: string, endpoint: string, headers: Record<string, string>,
|
|
7
|
+
deadlineMs?: number, secrets?: readonly string[], fixtureCa?: string) {
|
|
8
|
+
return connectMcp(name, async () => {
|
|
9
|
+
const { StreamableHTTPClientTransport } = await import("@modelcontextprotocol/sdk/client/streamableHttp.js");
|
|
10
|
+
// The Bun package-name shim delegates back to proxy-aware Bun fetch.
|
|
11
|
+
const undici: typeof import("undici") = await import(new URL("index.js", import.meta.resolve("undici/package.json")).href);
|
|
12
|
+
const dispatcher = new undici.Agent({ headersTimeout: 0, bodyTimeout: 0,
|
|
13
|
+
connect: { rejectUnauthorized: true, ...(fixtureCa === undefined ? {} : { ca: fixtureCa }) } });
|
|
14
|
+
const send: Fetch = async (input, init) => {
|
|
15
|
+
const response = await dispatcher.request({
|
|
16
|
+
origin: new URL(String(input)).origin,
|
|
17
|
+
path: new URL(String(input)).pathname,
|
|
18
|
+
method: "POST", body: String(init?.body), signal: init?.signal ?? undefined,
|
|
19
|
+
headers: { ...Object.fromEntries(new Headers(init?.headers)), "accept-encoding": "identity" },
|
|
20
|
+
});
|
|
21
|
+
// Undici reports deliberate destruction through the Node error event.
|
|
22
|
+
// The iterator still observes read errors; discarded bodies have no reader.
|
|
23
|
+
response.body.on("error", () => {});
|
|
24
|
+
const discard = async () => {
|
|
25
|
+
if (response.body.closed) return;
|
|
26
|
+
await new Promise<void>(resolve => {
|
|
27
|
+
response.body.once("close", resolve);
|
|
28
|
+
response.body.destroy();
|
|
29
|
+
});
|
|
30
|
+
};
|
|
31
|
+
const headers = new Headers();
|
|
32
|
+
for (const [name, value] of Object.entries(response.headers)) {
|
|
33
|
+
if (typeof value === "string") headers.set(name, value);
|
|
34
|
+
else for (const item of value ?? []) headers.append(name, item);
|
|
35
|
+
}
|
|
36
|
+
const encoding = headers.get("content-encoding");
|
|
37
|
+
if ((response.statusCode >= 300 && response.statusCode < 400) || (encoding && encoding.toLowerCase() !== "identity")) {
|
|
38
|
+
await discard();
|
|
39
|
+
throw new Error("MCP HTTPS response refused");
|
|
40
|
+
}
|
|
41
|
+
if (response.statusCode >= 400 || [204, 205].includes(response.statusCode)) {
|
|
42
|
+
await discard();
|
|
43
|
+
return new Response(null, { status: response.statusCode, headers });
|
|
44
|
+
}
|
|
45
|
+
// Pull from Undici directly. Bun's Readable.toWeb adapter can enqueue
|
|
46
|
+
// after cancel and throw outside the request's error path.
|
|
47
|
+
const source = response.body.iterator();
|
|
48
|
+
let cancelled = false;
|
|
49
|
+
const body = new ReadableStream<Uint8Array>({
|
|
50
|
+
async pull(controller) {
|
|
51
|
+
try {
|
|
52
|
+
const next = await source.next();
|
|
53
|
+
if (cancelled) return;
|
|
54
|
+
if (next.done) controller.close();
|
|
55
|
+
else controller.enqueue(next.value);
|
|
56
|
+
} catch (error) {
|
|
57
|
+
if (!cancelled) controller.error(error);
|
|
58
|
+
}
|
|
59
|
+
},
|
|
60
|
+
async cancel() {
|
|
61
|
+
cancelled = true;
|
|
62
|
+
await discard();
|
|
63
|
+
await source.return?.().catch(() => {});
|
|
64
|
+
},
|
|
65
|
+
});
|
|
66
|
+
return new Response(body, { status: response.statusCode, headers });
|
|
67
|
+
};
|
|
68
|
+
const requests = new Map<number | string, AbortController>();
|
|
69
|
+
const finished = new Set<number | string>();
|
|
70
|
+
const receiveFailure = (id: number | string, lifetime: AbortController) => {
|
|
71
|
+
setTimeout(() => {
|
|
72
|
+
if (requests.get(id) === lifetime && !lifetime.signal.aborted && !finished.has(id)) void transport.close().catch(() => {});
|
|
73
|
+
release(id);
|
|
74
|
+
}, 0);
|
|
75
|
+
};
|
|
76
|
+
function release(id: number | string) {
|
|
77
|
+
requests.delete(id);
|
|
78
|
+
finished.delete(id);
|
|
79
|
+
}
|
|
80
|
+
const cancellations = new Set<Promise<void>>();
|
|
81
|
+
const fencedFetch: Fetch = async (input, init) => {
|
|
82
|
+
if (String(input) !== endpoint) throw new Error("MCP endpoint refused");
|
|
83
|
+
if (init?.method === "GET") return new Response(null, { status: 405 });
|
|
84
|
+
if (init?.method !== "POST") throw new Error("MCP method refused");
|
|
85
|
+
const message = JSON.parse(String(init.body));
|
|
86
|
+
const id: unknown = message.id;
|
|
87
|
+
const requestId = typeof id === "number" || typeof id === "string" ? id : undefined;
|
|
88
|
+
const cancelId = message.method === "notifications/cancelled" ? message.params?.requestId : undefined;
|
|
89
|
+
const lifetime = requestId === undefined ? undefined : new AbortController();
|
|
90
|
+
const signals = [init.signal, lifetime?.signal, requestId === undefined ? AbortSignal.timeout(250) : undefined]
|
|
91
|
+
.filter((signal): signal is AbortSignal => !!signal);
|
|
92
|
+
if (lifetime && requestId !== undefined) requests.set(requestId, lifetime);
|
|
93
|
+
let done: (() => void) | undefined;
|
|
94
|
+
if (cancelId !== undefined) {
|
|
95
|
+
let finish!: () => void;
|
|
96
|
+
const pending = new Promise<void>(resolve => { finish = resolve; });
|
|
97
|
+
cancellations.add(pending);
|
|
98
|
+
done = () => { requests.get(cancelId)?.abort(); cancellations.delete(pending); finish(); };
|
|
99
|
+
}
|
|
100
|
+
const releaseRequest = () => { if (requestId !== undefined) release(requestId); };
|
|
101
|
+
try {
|
|
102
|
+
const response = await send(endpoint, { ...init, redirect: "error", signal: AbortSignal.any(signals) });
|
|
103
|
+
if (!response.ok || requestId === undefined) {
|
|
104
|
+
await response.body?.cancel();
|
|
105
|
+
releaseRequest();
|
|
106
|
+
return new Response(null, { status: response.status, headers: response.headers });
|
|
107
|
+
}
|
|
108
|
+
if (!response.body) { releaseRequest(); return response; }
|
|
109
|
+
const reader = response.body.getReader();
|
|
110
|
+
const streamed = response.headers.get("content-type")?.split(";")[0]?.trim().toLowerCase() === "text/event-stream";
|
|
111
|
+
let count = 0;
|
|
112
|
+
let jsonStart = !streamed;
|
|
113
|
+
const body = new ReadableStream<Uint8Array>({
|
|
114
|
+
async pull(controller) {
|
|
115
|
+
try {
|
|
116
|
+
const chunk = await reader.read();
|
|
117
|
+
if (chunk.done) {
|
|
118
|
+
controller.close();
|
|
119
|
+
if (streamed) receiveFailure(requestId, lifetime!);
|
|
120
|
+
else releaseRequest();
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
count += chunk.value.byteLength;
|
|
124
|
+
if (count > receiveLimit) {
|
|
125
|
+
await reader.cancel();
|
|
126
|
+
throw new Error("MCP response limit exceeded");
|
|
127
|
+
}
|
|
128
|
+
if (jsonStart) {
|
|
129
|
+
for (const byte of chunk.value) {
|
|
130
|
+
if (byte === 32 || byte === 9 || byte === 10 || byte === 13) continue;
|
|
131
|
+
if (byte === 91) { await reader.cancel(); throw new Error("MCP response batch refused"); }
|
|
132
|
+
jsonStart = false;
|
|
133
|
+
break;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
controller.enqueue(chunk.value);
|
|
137
|
+
} catch (error) {
|
|
138
|
+
if (streamed) receiveFailure(requestId, lifetime!);
|
|
139
|
+
else releaseRequest();
|
|
140
|
+
controller.error(error);
|
|
141
|
+
}
|
|
142
|
+
},
|
|
143
|
+
async cancel() { releaseRequest(); await reader.cancel(); },
|
|
144
|
+
});
|
|
145
|
+
return new Response(body, { status: response.status, headers: response.headers });
|
|
146
|
+
} catch {
|
|
147
|
+
releaseRequest();
|
|
148
|
+
throw new Error("MCP HTTPS request failed");
|
|
149
|
+
} finally { done?.(); }
|
|
150
|
+
};
|
|
151
|
+
const transport = new StreamableHTTPClientTransport(new URL(endpoint), {
|
|
152
|
+
requestInit: { headers },
|
|
153
|
+
fetch: fencedFetch,
|
|
154
|
+
reconnectionOptions: { maxRetries: 0, initialReconnectionDelay: 0, maxReconnectionDelay: 0, reconnectionDelayGrowFactor: 1 },
|
|
155
|
+
});
|
|
156
|
+
const close = transport.close.bind(transport);
|
|
157
|
+
let closing: Promise<void> | undefined;
|
|
158
|
+
transport.close = () => closing ??= (async () => {
|
|
159
|
+
try { await close(); } finally { await dispatcher.destroy(); }
|
|
160
|
+
})();
|
|
161
|
+
const start = transport.start.bind(transport);
|
|
162
|
+
transport.start = async () => {
|
|
163
|
+
const onmessage = transport.onmessage;
|
|
164
|
+
transport.onmessage = message => {
|
|
165
|
+
if ("id" in message && ("result" in message || "error" in message) && message.id !== undefined && requests.has(message.id)) {
|
|
166
|
+
finished.add(message.id);
|
|
167
|
+
}
|
|
168
|
+
onmessage?.(message);
|
|
169
|
+
};
|
|
170
|
+
await start();
|
|
171
|
+
};
|
|
172
|
+
const setVersion = transport.setProtocolVersion.bind(transport);
|
|
173
|
+
transport.setProtocolVersion = version => {
|
|
174
|
+
if (!supportedMcpVersions.has(version)) throw new Error("Unsupported MCP protocol revision");
|
|
175
|
+
setVersion(version);
|
|
176
|
+
};
|
|
177
|
+
return { transport, async drain() {
|
|
178
|
+
// SDK cancellation enters async send before its POST reaches fencedFetch.
|
|
179
|
+
await Bun.sleep(0);
|
|
180
|
+
await Promise.allSettled(cancellations);
|
|
181
|
+
} };
|
|
182
|
+
}, deadlineMs, secrets);
|
|
183
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import type { ManagedConfig } from "../../../contracts/config-permissions.ts";
|
|
2
|
+
import { spawnMcpStdio } from "../../../runtime/execution/mcp-stdio.ts";
|
|
3
|
+
import type { McpServerStatus, McpTool } from "../../../contracts/mcp.ts";
|
|
4
|
+
import { connectMcpHttps } from "./http.ts";
|
|
5
|
+
import { resolveMcpHttpsHeaders } from "../../../util/mcp-config.ts";
|
|
6
|
+
import { connectMcpStdio } from "./client.ts";
|
|
7
|
+
|
|
8
|
+
export async function openMcpRegistry(config: ManagedConfig, environment: Record<string, string | undefined>,
|
|
9
|
+
runtimeHeaders?: Record<string, Record<string, string>>, fixtureCa?: string) {
|
|
10
|
+
const entries = Object.entries(config.mcpServers ?? {}).sort(([a], [b]) => a.localeCompare(b));
|
|
11
|
+
type Connection = Awaited<ReturnType<typeof connectMcpStdio>>;
|
|
12
|
+
const connections: Connection[] = [];
|
|
13
|
+
const bindings = new Map<string, McpTool>();
|
|
14
|
+
const status = Object.create(null) as Record<string, McpServerStatus>;
|
|
15
|
+
let bytes = 0;
|
|
16
|
+
for (let offset = 0; offset < entries.length; offset += 4) {
|
|
17
|
+
const batch = await Promise.all(entries.slice(offset, offset + 4).map(async ([name, entry]) => {
|
|
18
|
+
const transport = entry.transport;
|
|
19
|
+
if (entry.enabled === false) { status[name] = { transport, state: "disabled" }; return; }
|
|
20
|
+
if (transport === "http") {
|
|
21
|
+
const resolved = resolveMcpHttpsHeaders(entry, environment, runtimeHeaders?.[name]);
|
|
22
|
+
if (!resolved) { status[name] = { transport, state: "unavailable", reason: "config" }; return; }
|
|
23
|
+
try {
|
|
24
|
+
const connection = await connectMcpHttps(name, entry.endpoint, resolved.headers, config.limits?.mcpCallTimeoutMs, resolved.secrets, fixtureCa);
|
|
25
|
+
return { name, connection, transport };
|
|
26
|
+
} catch { status[name] = { transport, state: "unavailable", reason: "connect" }; return; }
|
|
27
|
+
}
|
|
28
|
+
const env: Record<string, string> = Object.create(null);
|
|
29
|
+
for (const [key, value] of Object.entries(entry.env ?? {})) {
|
|
30
|
+
const resolved = environment[value.env];
|
|
31
|
+
if (!resolved?.trim() || resolved.includes("\0")) {
|
|
32
|
+
status[name] = { transport, state: "unavailable", reason: "config" }; return;
|
|
33
|
+
}
|
|
34
|
+
env[key] = resolved;
|
|
35
|
+
}
|
|
36
|
+
let child;
|
|
37
|
+
try { child = spawnMcpStdio({ ...entry, env }); }
|
|
38
|
+
catch { status[name] = { transport, state: "unavailable", reason: "spawn" }; return; }
|
|
39
|
+
try { return { name, connection: await connectMcpStdio(name, child, config.limits?.mcpCallTimeoutMs, Object.values(env)), transport }; }
|
|
40
|
+
catch { status[name] = { transport, state: "unavailable", reason: "discovery" }; return; }
|
|
41
|
+
}));
|
|
42
|
+
for (const result of batch) {
|
|
43
|
+
if (!result) continue;
|
|
44
|
+
const { name, connection, transport } = result;
|
|
45
|
+
if (bytes + connection.bytes > 128 * 1024 || bindings.size + connection.bindings.size > 64) {
|
|
46
|
+
status[name] = { transport, state: "unavailable", reason: "limit" };
|
|
47
|
+
await connection.close();
|
|
48
|
+
} else {
|
|
49
|
+
status[name] = { transport, state: "ready" };
|
|
50
|
+
bytes += connection.bytes;
|
|
51
|
+
connections.push(connection);
|
|
52
|
+
for (const [id, tool] of connection.bindings) bindings.set(id, tool);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
let closing: Promise<void> | undefined;
|
|
57
|
+
return { bindings, status() {
|
|
58
|
+
return Object.fromEntries(Object.entries(status).map(([name, state]) => [name,
|
|
59
|
+
state.state === "ready" && connections.some(connection => connection.name === name && !connection.available())
|
|
60
|
+
? { ...state, state: "unavailable" as const, reason: "closed" as const } : state]));
|
|
61
|
+
}, close() { return closing ??= Promise.allSettled(connections.map(connection => connection.close())).then(() => {}); } };
|
|
62
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/** SDK-validated result, projected without exposing unsupported block payloads. */
|
|
2
|
+
export function projectMcpResult(result: {
|
|
3
|
+
content: readonly { type: string; text?: string }[];
|
|
4
|
+
structuredContent?: unknown;
|
|
5
|
+
}, redactText: (text: string) => string = text => text): string {
|
|
6
|
+
const textBlocks = result.content.filter(block => block.type === "text" && typeof block.text === "string");
|
|
7
|
+
const output = {
|
|
8
|
+
text: redactText(textBlocks.map(block => block.text).join("\n")),
|
|
9
|
+
structuredContent: result.structuredContent ?? null,
|
|
10
|
+
omittedContentBlocks: result.content.length - textBlocks.length,
|
|
11
|
+
truncated: false,
|
|
12
|
+
};
|
|
13
|
+
const serialize = () => JSON.stringify(output);
|
|
14
|
+
const fits = () => Buffer.byteLength(serialize()) <= 2048;
|
|
15
|
+
if (fits()) return serialize();
|
|
16
|
+
output.truncated = true;
|
|
17
|
+
if (output.structuredContent !== null) output.structuredContent = null;
|
|
18
|
+
if (fits()) return serialize();
|
|
19
|
+
const units: string[] = [];
|
|
20
|
+
for (const unit of output.text) {
|
|
21
|
+
if (units.length === 2048) break;
|
|
22
|
+
units.push(unit);
|
|
23
|
+
}
|
|
24
|
+
let low = 0, high = units.length;
|
|
25
|
+
while (low < high) {
|
|
26
|
+
const mid = Math.ceil((low + high) / 2);
|
|
27
|
+
output.text = units.slice(0, mid).join("");
|
|
28
|
+
if (fits()) low = mid;
|
|
29
|
+
else high = mid - 1;
|
|
30
|
+
}
|
|
31
|
+
output.text = units.slice(0, low).join("");
|
|
32
|
+
return serialize();
|
|
33
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
const types = new Set(["object", "array", "string", "boolean", "integer", "number", "null"]);
|
|
2
|
+
|
|
3
|
+
interface Schema {
|
|
4
|
+
type: string;
|
|
5
|
+
properties?: Record<string, Schema>;
|
|
6
|
+
required?: string[];
|
|
7
|
+
additionalProperties?: boolean;
|
|
8
|
+
items?: Schema;
|
|
9
|
+
enum?: unknown[];
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function record(value: unknown): value is Record<string, unknown> {
|
|
13
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function matches(type: string, value: unknown): boolean {
|
|
17
|
+
switch (type) {
|
|
18
|
+
case "object": return record(value);
|
|
19
|
+
case "array": return Array.isArray(value);
|
|
20
|
+
case "integer": return Number.isInteger(value);
|
|
21
|
+
case "number": return typeof value === "number" && Number.isFinite(value);
|
|
22
|
+
case "null": return value === null;
|
|
23
|
+
default: return typeof value === type;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function acceptsMcpSchema(input: unknown): input is Schema {
|
|
28
|
+
let nodes = 0;
|
|
29
|
+
function visit(node: unknown, depth: number): boolean {
|
|
30
|
+
if (++nodes > 128 || depth > 8 || !record(node) || typeof node.type !== "string" || !types.has(node.type)) return false;
|
|
31
|
+
for (const [key, value] of Object.entries(node)) {
|
|
32
|
+
switch (key) {
|
|
33
|
+
case "type": break;
|
|
34
|
+
case "description": case "title": if (typeof value !== "string") return false; break;
|
|
35
|
+
case "properties":
|
|
36
|
+
if (node.type !== "object" || !record(value) || !Object.values(value).every(child => visit(child, depth + 1))) return false;
|
|
37
|
+
break;
|
|
38
|
+
case "required":
|
|
39
|
+
if (node.type !== "object" || !Array.isArray(value) || new Set(value).size !== value.length ||
|
|
40
|
+
!value.every(name => typeof name === "string" && record(node.properties) && Object.hasOwn(node.properties, name))) return false;
|
|
41
|
+
break;
|
|
42
|
+
case "additionalProperties":
|
|
43
|
+
if (node.type !== "object" || typeof value !== "boolean") return false;
|
|
44
|
+
break;
|
|
45
|
+
case "items":
|
|
46
|
+
if (node.type !== "array" || !visit(value, depth + 1)) return false;
|
|
47
|
+
break;
|
|
48
|
+
case "enum":
|
|
49
|
+
if (!Array.isArray(value) || !value.length || new Set(value).size !== value.length ||
|
|
50
|
+
node.type === "object" || node.type === "array" || !value.every(member => matches(node.type as string, member))) return false;
|
|
51
|
+
break;
|
|
52
|
+
default: return false;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
57
|
+
return record(input) && input.type === "object" && visit(input, 1) &&
|
|
58
|
+
Buffer.byteLength(JSON.stringify(input)) <= 8192;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function matchesMcpArguments(schema: Schema, value: unknown): boolean {
|
|
62
|
+
if (!matches(schema.type, value) || (schema.enum && !schema.enum.includes(value))) return false;
|
|
63
|
+
if (schema.items && Array.isArray(value)) return value.every(item => matchesMcpArguments(schema.items!, item));
|
|
64
|
+
if (!record(value)) return true;
|
|
65
|
+
if (schema.required?.some(key => !Object.hasOwn(value, key))) return false;
|
|
66
|
+
return Object.entries(value).every(([key, entry]) => {
|
|
67
|
+
const properties = schema.properties;
|
|
68
|
+
return properties && Object.hasOwn(properties, key)
|
|
69
|
+
? matchesMcpArguments(properties[key]!, entry)
|
|
70
|
+
: schema.additionalProperties !== false;
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function mcpToolName(server: string, remote: string): string | undefined {
|
|
75
|
+
if (!/^[a-z][a-z0-9]{0,11}$/.test(server) || !remote || !remote.isWellFormed() || Buffer.byteLength(remote) > 22) return;
|
|
76
|
+
return `mcp__${server}__${Buffer.from(remote).toString("hex")}`;
|
|
77
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/** Lifetime is one connection. Values never leave this closure. */
|
|
2
|
+
export function mcpSecrets(values: readonly string[]) {
|
|
3
|
+
const secrets = [...new Set(values.filter(Boolean))].sort((a, b) => b.length - a.length);
|
|
4
|
+
const contains = (text: string) => secrets.some(secret => text.includes(secret));
|
|
5
|
+
const pattern = secrets.length ? new RegExp(secrets.map(secret => secret.replace(/[.*+?^$\{\}()|[\]\\]/g, "\\$&")).join("|"), "g") : undefined;
|
|
6
|
+
function text(value: string): string {
|
|
7
|
+
if (!pattern) return value;
|
|
8
|
+
const projected = value.replace(pattern, "[redacted]");
|
|
9
|
+
// Overlapping matches or short credentials can make the marker unsafe.
|
|
10
|
+
if (contains(projected)) throw new Error("MCP output could not be safely redacted");
|
|
11
|
+
return projected;
|
|
12
|
+
}
|
|
13
|
+
function tree(value: unknown, checkOnly = false, depth = 0): unknown {
|
|
14
|
+
if (depth > 64) throw new Error("MCP output nesting exceeds limit");
|
|
15
|
+
if (typeof value === "string") {
|
|
16
|
+
if (!checkOnly) return text(value);
|
|
17
|
+
if (contains(value)) throw new Error("MCP definition contains credential material");
|
|
18
|
+
return value;
|
|
19
|
+
}
|
|
20
|
+
if (Array.isArray(value)) return value.map(item => tree(item, checkOnly, depth + 1));
|
|
21
|
+
if (value && typeof value === "object") return Object.fromEntries(Object.entries(value).map(([key, entry]) => {
|
|
22
|
+
if (contains(key)) throw new Error("MCP output key contains credential material");
|
|
23
|
+
return [key, tree(entry, checkOnly, depth + 1)];
|
|
24
|
+
}));
|
|
25
|
+
return value;
|
|
26
|
+
}
|
|
27
|
+
return { contains, text, tree };
|
|
28
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
// Runner-owned streams implement the SDK transport interface without giving
|
|
2
|
+
// the SDK authority to launch or inherit the harness environment.
|
|
3
|
+
export interface McpStdioProcess {
|
|
4
|
+
stdout: ReadableStream<Uint8Array>;
|
|
5
|
+
write(frame: Uint8Array): Promise<void>;
|
|
6
|
+
close(): Promise<void>;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
const frameLimit = 256 * 1024;
|
|
10
|
+
|
|
11
|
+
export class McpStdioTransport<Message> {
|
|
12
|
+
onmessage?: (message: Message) => void;
|
|
13
|
+
onerror?: (error: Error) => void;
|
|
14
|
+
onclose?: () => void;
|
|
15
|
+
#started = false;
|
|
16
|
+
#closing?: Promise<void>;
|
|
17
|
+
#sends = Promise.resolve();
|
|
18
|
+
readonly #process: McpStdioProcess;
|
|
19
|
+
|
|
20
|
+
constructor(process: McpStdioProcess, private readonly revisions?: ReadonlySet<string>,
|
|
21
|
+
private readonly parse?: (value: unknown) => Message) {
|
|
22
|
+
this.#process = process;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
setProtocolVersion(version: string) {
|
|
26
|
+
if (this.revisions && !this.revisions.has(version)) throw new Error("Unsupported MCP protocol revision");
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
async start(): Promise<void> {
|
|
30
|
+
if (this.#started || this.#closing) throw new Error("MCP transport already started or closed");
|
|
31
|
+
this.#started = true;
|
|
32
|
+
void this.#receive();
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
async #receive() {
|
|
36
|
+
const reader = this.#process.stdout.getReader();
|
|
37
|
+
const frame = new Uint8Array(frameLimit);
|
|
38
|
+
const decoder = new TextDecoder("utf-8", { fatal: true });
|
|
39
|
+
let size = 0;
|
|
40
|
+
try {
|
|
41
|
+
while (!this.#closing) {
|
|
42
|
+
const read = await reader.read();
|
|
43
|
+
if (read.done) {
|
|
44
|
+
if (size) throw new Error("Incomplete MCP frame");
|
|
45
|
+
break;
|
|
46
|
+
}
|
|
47
|
+
const chunk = read.value;
|
|
48
|
+
for (let start = 0; start < chunk.length;) {
|
|
49
|
+
const newline = chunk.indexOf(10, start);
|
|
50
|
+
const end = newline < 0 ? chunk.length : newline;
|
|
51
|
+
const length = end - start;
|
|
52
|
+
if (size + length > frameLimit) throw new Error("MCP frame too large");
|
|
53
|
+
frame.set(chunk.subarray(start, end), size);
|
|
54
|
+
size += length;
|
|
55
|
+
if (newline >= 0) {
|
|
56
|
+
const message: unknown = JSON.parse(decoder.decode(frame.subarray(0, size)));
|
|
57
|
+
if (!message || typeof message !== "object" || Array.isArray(message)) {
|
|
58
|
+
throw new Error("Invalid MCP message");
|
|
59
|
+
}
|
|
60
|
+
this.onmessage?.(this.parse ? this.parse(message) : message as Message);
|
|
61
|
+
size = 0;
|
|
62
|
+
}
|
|
63
|
+
start = end + 1;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
} catch {
|
|
67
|
+
if (!this.#closing) this.onerror?.(new Error("Invalid MCP stdio stream"));
|
|
68
|
+
} finally {
|
|
69
|
+
reader.releaseLock();
|
|
70
|
+
await this.close().catch(() => this.onerror?.(new Error("MCP stdio cleanup failed")));
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
send(message: Message): Promise<void> {
|
|
75
|
+
const frame = new TextEncoder().encode(JSON.stringify(message) + "\n");
|
|
76
|
+
if (frame.length > frameLimit + 1) return Promise.reject(new Error("MCP frame too large"));
|
|
77
|
+
const write = this.#sends.then(async () => {
|
|
78
|
+
if (!this.#started || this.#closing) throw new Error("MCP transport is closed");
|
|
79
|
+
const stop = AbortSignal.timeout(250);
|
|
80
|
+
const writeDeadline = new Promise<never>((_, reject) => {
|
|
81
|
+
stop.addEventListener("abort", () => reject(new Error("MCP stdio write timed out")), { once: true });
|
|
82
|
+
});
|
|
83
|
+
try {
|
|
84
|
+
await Promise.race([this.#process.write(frame), writeDeadline]);
|
|
85
|
+
} catch (error) {
|
|
86
|
+
void this.close().catch(() => {});
|
|
87
|
+
throw error;
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
this.#sends = write.catch(() => {});
|
|
91
|
+
return write;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
async drain(): Promise<void> { await this.#sends; }
|
|
95
|
+
|
|
96
|
+
close(): Promise<void> {
|
|
97
|
+
return this.#closing ??= Promise.resolve().then(async () => {
|
|
98
|
+
try {
|
|
99
|
+
await this.#process.close();
|
|
100
|
+
} finally {
|
|
101
|
+
this.onclose?.();
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
}
|