From 1d630cbd9b05370a5612fd99ed03bad7219abbc3 Mon Sep 17 00:00:00 2001 From: Seth Date: Sat, 31 Oct 2026 18:24:58 -0700 Subject: [PATCH 1/2] Delete the unreachable capability-provider subsystem providers/ (bring, managed, maxun, mcp, openapi, fromManifest), admission.ts, provider.ts, mcp.ts, mcpClient.ts and bring.ts were only imported by each other and their own tests; no production, script or e2e path reached them. Drops @modelcontextprotocol/client and @modelcontextprotocol/server with them. Tests that used the providers as fixture builders now seal manifests directly. DESIGN.md updated to match. Co-Authored-By: Claude Opus 5.5 --- package-lock.json | 83 +--- package.json | 2 - src/core/capabilities/DESIGN.md | 77 +-- src/core/capabilities/admission.ts | 396 --------------- src/core/capabilities/asyncExecution.test.ts | 183 +++---- src/core/capabilities/bring.ts | 94 ---- .../capabilities/capability.vertical.test.ts | 155 +++--- .../confirmationIntegrity.test.ts | 23 +- src/core/capabilities/jsonArguments.test.ts | 45 +- src/core/capabilities/mcp.ts | 132 ----- src/core/capabilities/mcpClient.test.ts | 452 ------------------ src/core/capabilities/mcpClient.ts | 240 ---------- src/core/capabilities/provider.test.ts | 320 ------------- src/core/capabilities/provider.ts | 126 ----- .../capabilities/providers/bringProvider.ts | 55 --- .../capabilities/providers/fromManifest.ts | 109 ----- .../providers/managed.decision.ts | 45 -- .../providers/managedProvider.test.ts | 212 -------- .../capabilities/providers/managedProvider.ts | 221 --------- .../providers/maxun.evaluation.ts | 17 - .../providers/maxunProvider.test.ts | 315 ------------ .../capabilities/providers/maxunProvider.ts | 202 -------- .../capabilities/providers/maxunRuntime.ts | 137 ------ .../capabilities/providers/mcpProvider.ts | 90 ---- .../capabilities/providers/openapiProvider.ts | 72 --- src/core/capabilities/redaction.test.ts | 53 +- .../capabilities/untypedOutputFields.test.ts | 13 - src/core/capabilities/urlPolicy.test.ts | 36 -- 28 files changed, 230 insertions(+), 3675 deletions(-) delete mode 100644 src/core/capabilities/admission.ts delete mode 100644 src/core/capabilities/bring.ts delete mode 100644 src/core/capabilities/mcp.ts delete mode 100644 src/core/capabilities/mcpClient.test.ts delete mode 100644 src/core/capabilities/mcpClient.ts delete mode 100644 src/core/capabilities/provider.test.ts delete mode 100644 src/core/capabilities/provider.ts delete mode 100644 src/core/capabilities/providers/bringProvider.ts delete mode 100644 src/core/capabilities/providers/fromManifest.ts delete mode 100644 src/core/capabilities/providers/managed.decision.ts delete mode 100644 src/core/capabilities/providers/managedProvider.test.ts delete mode 100644 src/core/capabilities/providers/managedProvider.ts delete mode 100644 src/core/capabilities/providers/maxun.evaluation.ts delete mode 100644 src/core/capabilities/providers/maxunProvider.test.ts delete mode 100644 src/core/capabilities/providers/maxunProvider.ts delete mode 100644 src/core/capabilities/providers/maxunRuntime.ts delete mode 100644 src/core/capabilities/providers/mcpProvider.ts delete mode 100644 src/core/capabilities/providers/openapiProvider.ts diff --git a/package-lock.json b/package-lock.json index 18e7243..012c4a3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,7 +10,6 @@ "hasInstallScript": true, "dependencies": { "@dnd-kit/core": "^6.3.1", - "@modelcontextprotocol/client": "2.3.1", "clsx": "^2.1.1", "cmdk": "^1.1.1", "framer-motion": "^14.3.0", @@ -31,7 +30,6 @@ "devDependencies": { "@anthropic-ai/sdk": "^0.135.0", "@cloudflare/vite-plugin": "^1.64.0", - "@modelcontextprotocol/server": "2.3.1", "@playwright/test": "^1.64.1", "@tailwindcss/postcss": "^4", "@testing-library/dom": "^10.4.2", @@ -1814,50 +1812,6 @@ "dev": true, "license": "MIT" }, - "node_modules/@modelcontextprotocol/client": { - "version": "2.3.1", - "resolved": "https://registry.npmjs.org/@modelcontextprotocol/client/-/client-2.3.1.tgz", - "integrity": "sha512-mIGZXpHsjnZ6lD+gD/WCMpR5k8yVQQ8nNFH1N0Srf7AvnwTUMYD6pvTY8ng2+He5FfV7YMZLmrjL7cLa/cc3dQ==", - "license": "Apache-2.0", - "dependencies": { - "@modelcontextprotocol/core": "2.3.1", - "cross-spawn": "^7.0.5", - "eventsource": "^3.0.2", - "eventsource-parser": "^3.0.8", - "jose": "^6.1.3", - "pkce-challenge": "^5.0.0", - "zod": "^4.2.0" - }, - "engines": { - "node": ">=20" - } - }, - "node_modules/@modelcontextprotocol/core": { - "version": "2.3.1", - "resolved": "https://registry.npmjs.org/@modelcontextprotocol/core/-/core-2.3.1.tgz", - "integrity": "sha512-laVmIhPGpWi7rG9q+0UigblRN0Hws/Yjlb6qLFtVqryXVLkc4h+yN8z731POD2LnAGQlFe7/tXhUm7Bi0YB91g==", - "license": "Apache-2.0", - "dependencies": { - "zod": "^4.2.0" - }, - "engines": { - "node": ">=20" - } - }, - "node_modules/@modelcontextprotocol/server": { - "version": "2.3.1", - "resolved": "https://registry.npmjs.org/@modelcontextprotocol/server/-/server-2.3.1.tgz", - "integrity": "sha512-e59MfWuSssj6DoQ96JCTqdW1AXT5/XtnTl0WWd6MU1Ka434fFUGGosCLJbGNySDhHGvxQWoRWK137FXYFkYNWw==", - "dev": true, - "license": "Apache-2.0", - "dependencies": { - "@modelcontextprotocol/core": "2.3.1", - "zod": "^4.2.0" - }, - "engines": { - "node": ">=20" - } - }, "node_modules/@napi-rs/wasm-runtime": { "version": "1.2.4", "resolved": "https://registry.npmjs.org/@napi-rs/wasm-runtime/-/wasm-runtime-1.2.4.tgz", @@ -5563,6 +5517,7 @@ "version": "7.0.6", "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.6.tgz", "integrity": "sha512-uV2QOWP2nWzsy2aMp8aRibhi9dlzF5Hgh5SHaB9OiTGEyDTiJJyx0uy51QXdyWbtAHNua4XJzUKca3OzKUd3vA==", + "dev": true, "license": "MIT", "dependencies": { "path-key": "^3.1.0", @@ -6624,27 +6579,6 @@ "node": ">=0.8.x" } }, - "node_modules/eventsource": { - "version": "3.0.7", - "resolved": "https://registry.npmjs.org/eventsource/-/eventsource-3.0.7.tgz", - "integrity": "sha512-CRT1WTyuQoD771GW56XEZFQ/ZoSfWid1alKGDYMmkt2yl8UXrVR4pspqWNEcqKvVIzg6PAltWjxcSSPrboA4iA==", - "license": "MIT", - "dependencies": { - "eventsource-parser": "^3.0.1" - }, - "engines": { - "node": ">=18.0.0" - } - }, - "node_modules/eventsource-parser": { - "version": "3.1.1", - "resolved": "https://registry.npmjs.org/eventsource-parser/-/eventsource-parser-3.1.1.tgz", - "integrity": "sha512-EKN1vKAMcZ8MlYMpaNuxN6R9yakzH6uajHcHVTqWJzvu5pWw9DyhbP35HH8MVBQ+dZjAfDxk+A8NiR9KWaXiyQ==", - "license": "MIT", - "engines": { - "node": ">=18.0.0" - } - }, "node_modules/expect-type": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/expect-type/-/expect-type-1.4.0.tgz", @@ -7846,6 +7780,7 @@ "version": "2.0.0", "resolved": "https://registry.npmjs.org/isexe/-/isexe-2.0.0.tgz", "integrity": "sha512-RHxMLp9lnKHGHRng9QFhRCMbYAcVpn69smSGcq3f36xjgVVWThj4qqLbTLlq7Ssj8B+fIQ1EuCEGI2lKsyQeIw==", + "dev": true, "license": "ISC" }, "node_modules/iterator.prototype": { @@ -9865,6 +9800,7 @@ "version": "3.1.1", "resolved": "https://registry.npmjs.org/path-key/-/path-key-3.1.1.tgz", "integrity": "sha512-ojmeN0qd+y0jszEtoY48r0Peq5dwMEkIlCOu6Q5f41lfkswXuKtYrhgoTpLnyIcHm24Uhqx+5Tqm2InSwLhE6Q==", + "dev": true, "license": "MIT", "engines": { "node": ">=8" @@ -9999,15 +9935,6 @@ "url": "https://github.com/sponsors/jonschlinkert" } }, - "node_modules/pkce-challenge": { - "version": "5.0.1", - "resolved": "https://registry.npmjs.org/pkce-challenge/-/pkce-challenge-5.0.1.tgz", - "integrity": "sha512-wQ0b/W4Fr01qtpHlqSqspcj3EhBvimsdh0KlHhH8HRZnMsEa0ea2fTULOXOS9ccQr3om+GcGRk4e+isrZWV8qQ==", - "license": "MIT", - "engines": { - "node": ">=16.20.0" - } - }, "node_modules/playwright": { "version": "1.64.1", "resolved": "https://registry.npmjs.org/playwright/-/playwright-1.64.1.tgz", @@ -10878,6 +10805,7 @@ "version": "2.0.0", "resolved": "https://registry.npmjs.org/shebang-command/-/shebang-command-2.0.0.tgz", "integrity": "sha512-kHxr2zZpYtdmrN1qDjrrX/Z1rR1kG8Dx+gkpK1G4eXmvXswmcE1hTWBWYUzlraYw1/yZp6YuDY77YtvbN0dmDA==", + "dev": true, "license": "MIT", "dependencies": { "shebang-regex": "^3.0.0" @@ -10890,6 +10818,7 @@ "version": "3.0.0", "resolved": "https://registry.npmjs.org/shebang-regex/-/shebang-regex-3.0.0.tgz", "integrity": "sha512-7++dFhtcx3353uBaq8DDR4NuxBetBzC7ZQOhmTQInHEd6bSrXdiEyzCvG07Z44UYdLShWUyXt5M/yhz8ekcb1A==", + "dev": true, "license": "MIT", "engines": { "node": ">=8" @@ -12609,6 +12538,7 @@ "version": "2.0.2", "resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz", "integrity": "sha512-BLI3Tl1TW3Pvl70l3yq3Y64i+awpwXqsGBYWkkqMtnbXgrMD+yj7rhW0kuEDxzJaYXGjEW5ogapKNMEKNMjibA==", + "dev": true, "license": "ISC", "dependencies": { "isexe": "^2.0.0" @@ -12876,6 +12806,7 @@ "version": "4.6.7", "resolved": "https://registry.npmjs.org/zod/-/zod-4.6.7.tgz", "integrity": "sha512-g8W2D2kU6a8untc053fX1uVBKzM7yqAxW/3zwk83+6RyF2+SKOuV+c0wBDhUwzV1PaGapmZH+rmcUzPf+3CGwg==", + "dev": true, "license": "MIT", "funding": { "url": "https://github.com/sponsors/colinhacks" diff --git a/package.json b/package.json index 0377713..eb96826 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,6 @@ }, "dependencies": { "@dnd-kit/core": "^6.3.1", - "@modelcontextprotocol/client": "2.3.1", "clsx": "^2.1.1", "cmdk": "^1.1.1", "framer-motion": "^14.3.0", @@ -51,7 +50,6 @@ "devDependencies": { "@anthropic-ai/sdk": "^0.135.0", "@cloudflare/vite-plugin": "^1.64.0", - "@modelcontextprotocol/server": "2.3.1", "@playwright/test": "^1.64.1", "@tailwindcss/postcss": "^4", "@testing-library/dom": "^10.4.2", diff --git a/src/core/capabilities/DESIGN.md b/src/core/capabilities/DESIGN.md index 41fac8d..6937852 100644 --- a/src/core/capabilities/DESIGN.md +++ b/src/core/capabilities/DESIGN.md @@ -21,54 +21,23 @@ a secret), a transport, input schemas, and an output schema. The digest is SHA-256 over the canonical body. Approval is not part of the digest, so granting approval does not create a new capability. -Three compilers propose manifests: - -- `compileOpenApi` reads an OpenAPI 3.x document. -- `compileMcpTools` reads MCP tool schemas. Calling them requires a bound - `McpTransport`; the compiler itself does not open a connection. -- `compileBring` reads a structured description. Free text is not a proposal. - -## Providers and admission - -A `CapabilityProvider` (`provider.ts`) discovers candidates in one external -ecosystem and materializes a `CapabilityProposalV1`. `providers/openapiProvider.ts`, -`providers/mcpProvider.ts`, and `providers/bringProvider.ts` wrap the three -compilers above; they do not parse a second way. A proposal carries an effect -*hint*, an auth *placement*, a transport, schemas, and provenance. It has no -id, approval, digest, credential slot, or trust flag, and `admission.ts` -refuses a proposal that includes one. - -`admitProposal` derives what a provider may not claim: the id from the -transport, the effect from the method floor (an MCP read hint counts only for -a server on the host's `trustedEffectHints` list, and the same holds for an -async runtime), the approval from the -effect, and the credential slot from the destination. It seals provenance -(provider, external id, source locator and revision, discovery and admission -time, schema digest) into the digest, then calls `installProposal`. -`providers/maxunProvider.ts` is a `web_data` provider: a scrape robot becomes -`{ markdown: string }`, an extract robot becomes an object of its declared -text fields, and a robot with no declared fields is reported, not admitted as -`any`. Its proposals use the async `maxun` runtime (`providers/maxunRuntime.ts`) -with the API key in the `x-api-key` slot. That runtime's start is fenced: Maxun's -open-source server answers `POST /api/robots/:id/runs` only after the run -finishes, so no run id could be recorded for a run longer than one request. -Until an early-acknowledgement start is verified, start sends nothing and -execution fails `ASYNC_START_REJECTED`. Only the run lookup by id is implemented. -`providers/maxun.evaluation.ts` records that no live Maxun run was performed. -`providers/managedProvider.ts` is a `managed_integration` provider: a -Pipedream- or Composio-style catalog entry becomes an http proposal against the -SaaS API itself, with `oauth` auth. An `oauth` binding carries scopes and a -slot (one slot per origin and scope set); the token enters the slot through the -host connect flow (`connect.ts`, `server/oauthConnect.ts`), and until then -execution is `AUTH_UNBOUND`. `oauth` is http-only and never rides -`server_broker`. `compileOpenApi` binds an OpenAPI oauth2 scheme only for a -provider in `connectProviders.ts` (Google), only when the API is on that -provider's own hosts, and only for a read operation with a read-only scope; -a write that needs a sign-in is refused. -`providers/managed.decision.ts` rejects both vendors as dependencies. -Admission -does not place blocks or wires. The OpenAPI install panel still calls -`compileOpenApi` directly. +`compileOpenApi` reads an OpenAPI 3.x document and proposes manifests. The +registry also accepts manifests on an MCP transport (called through a bound +`McpTransport`) and on a host-bound async runtime; nothing in this tree +compiles those from an external source. + +## OAuth + +An `oauth` binding carries scopes and a slot (one slot per origin and scope +set); the token enters the slot through the host connect flow (`connect.ts`, +`server/oauthConnect.ts`), and until then execution is `AUTH_UNBOUND`. +`oauth` is http-only and never rides `server_broker`. `compileOpenApi` binds +an OpenAPI oauth2 scheme only for a provider in `connectProviders.ts` +(Google), only when the API is on that provider's own hosts, and only for a +read operation with a read-only scope; a write that needs a sign-in is +refused. + +## Installing `validateManifest` rebuilds the canonical object and checks the digest. `installProposal` is the gate that registers anything. A write or destructive @@ -87,15 +56,15 @@ and read and compute entries pass unchanged. A manifest describes a capability. It does not grant itself authority. - Credential slots are `origin + scheme + placement`. Two APIs that both name a scheme `ApiKey` do not share a secret, and a proposal cannot point its `secretRef` at another origin's slot. A run cannot supply a header input in the header the credential travels in (`Authorization`, or the apiKey header name, in any case); that is `INPUT_INVALID`. Neither the credential nor a header input may be placed in a hop-by-hop, framing or transport header (`Connection`, `Content-Length`, `Host`, `Transfer-Encoding`, `Cookie`, ...: `DENIED_HEADER_NAMES` in `manifest.ts`, matched in any case); that fails validation at compile and install, not at execute. A secret value is at most 4096 bytes (`MAX_SECRET_BYTES`), both where it enters the session slot and where the broker reads it from a request body. -- HTTP method is an effect floor. `x-omni-effect` and MCP annotations may raise that floor. They cannot turn POST into auto-running compute. Untrusted MCP `readOnlyHint` is not approval. +- HTTP method is an effect floor. `x-omni-effect` may raise that floor. It cannot turn POST into auto-running compute. - Capability ids are a hash of canonical origin and operation. Speech handlers keep pinned ids. A different origin cannot reuse an existing id. - Wires enter through `admitConnection`. Typed mismatches are refused. A string sink may record `text` or `join_titles` instead of pretending the source was already that string. - Execution is one runtime: `executeCapability`. Each run is a vault record with an idempotency key. The same key and the same request replays; a different request under that key conflicts. The request is compared by its digest (the one a write confirms), and the broker's server ledger compares the method and URL it sends, taken before the credential is added. A write that leaves the process and then throws, or is still `running` after its deadline, is `EFFECT_UNCERTAIN` and is not retryable. Inference runs use the same words in Postgres, including `uncertain` after a stream breaks. - Triggers are `manual`, `on_create` (once per block), `on_input_change`, and `interval`. Write and destructive stay manual. Mounting a view is not a trigger. -- HTTP capabilities are `browser_direct` or `server_broker`. A provider cannot choose: admission sets `browser_direct` unless the host's `AdmissionPolicy.brokerOrigins` names the origin, and then only for read and compute. The broker rebuilds the URL from the manifest, refuses private and metadata addresses, and refuses write and destructive effects. -- A base URL is https, carries no query or fragment, and an IP literal in it must be public. Loopback (http or https) is allowed only when the host itself built the manifest (`validateManifest(..., { hostCreated: true })`). Unsupported OpenAPI constructs fail compilation instead of becoming `any`, with one exception: a response field below the top level that declares no type is typed `any` (`ConvertOptions` in `jsonSchema.ts`), because the API does return it and an `any` source is never assignable to a typed input. A response that is untyped as a whole, or a result list of untyped items, still fails, and so does an untyped input. Admission applies the same line (`untypedResult` in `admission.ts`). -- MCP tools compile from schemas and run through `mcpClient.ts`, a thin adapter over the official TypeScript SDK v2 (`@modelcontextprotocol/client`). The SDK negotiates the protocol era (`auto`: probe 2026-07-28, fall back to the 2025 handshake) and forwards cancellation. An MCP transport carries the server's URL `origin` beside its `serverId` label. Its id hashes `origin`, `serverId` and `toolName`, so two servers that share a label are two capabilities that coexist, and its credential slot is `credentialSlot(origin, auth)`, the http rule, so they never share a secret; execution resolves the slot and passes it as per-call headers, so revoking the slot stops the next call, and refuses a bound transport whose origin is not the manifest's (`TRANSPORT_NOT_BOUND`). `createMcpHttpTransport` applies the base URL rule above (`destinationUrlErrors` in `egress.ts`) to the server URL when it is created, before any session opens: https, no embedded credentials, query or fragment, no private or metadata address, loopback only with `hostCreated: true`. Every request is sent with `redirect: 'manual'`, so a 3xx fails the request and a credentialed request is never replayed to another host. A sync MCP call is bounded by the same 15 s request timeout as HTTP. The capability store is persist version 2: `migrateCapabilityStore` moves an MCP manifest persisted before `origin` existed (it names no server URL, so none can be derived) into `stale`, kept with its stored body and named in the install panel as needing re-install, instead of letting restore reject it and the next write drop it. -- Long work uses the `async_poll` profile, never a longer request timeout. The transport is `{ kind: 'async', runtimeId, operation }`: the host binds the runtime (`asyncRuntime.ts`) and its endpoint, and a proposal cannot name one. `start` returns an external run id, which the vault ledger records before polling. Each start and each poll is still one request of at most 15 s, and the whole run is bounded by the profile's `maxDurationMs` (at most one hour): no sleep or poll request extends past that deadline, except the single observation recovery makes of a run that already outlived it. A run this session is still polling is not expired underneath it by another admission; it settles its own row. Omni never repeats a start. A write that was started and not observed to finish (deadline, repeated poll failures, or cancel) is `uncertain`. `reconcileAsyncExecution` and `recoverAsyncExecutions` read the ledger after a reload and poll the external run id; an uncertain run closes only when the destination reports a terminal status. Async capabilities run manually only. An untrusted async runtime lands at `write`, like an untrusted MCP server. The server ledger carries the same fields (`db/migrations/005_capability_async.sql`). +- HTTP capabilities are `browser_direct` or `server_broker`. `compileOpenApi` sets `server_broker` only when the host asks for it, and then only for read and compute; `validateManifest` refuses a brokered write, destructive or oauth manifest. The broker rebuilds the URL from the manifest, refuses private and metadata addresses, and refuses write and destructive effects. +- A base URL is https, carries no query or fragment, and an IP literal in it must be public. Loopback (http or https) is allowed only when the host itself built the manifest (`validateManifest(..., { hostCreated: true })`). Unsupported OpenAPI constructs fail compilation instead of becoming `any`, with one exception: a response field below the top level that declares no type is typed `any` (`ConvertOptions` in `jsonSchema.ts`), because the API does return it and an `any` source is never assignable to a typed input. A response that is untyped as a whole, or a result list of untyped items, still fails, and so does an untyped input. +- An MCP manifest runs through a host-bound `McpTransport` (`bindMcpTransport` in `execute.ts`). An MCP transport carries the server's URL `origin` beside its `serverId` label. Its id hashes `origin`, `serverId` and `toolName`, so two servers that share a label are two capabilities that coexist, and its credential slot is `credentialSlot(origin, auth)`, the http rule, so they never share a secret; execution resolves the slot and passes it as per-call headers, so revoking the slot stops the next call, and refuses a bound transport whose origin is not the manifest's (`TRANSPORT_NOT_BOUND`). A sync MCP call is bounded by the same 15 s request timeout as HTTP. The capability store is persist version 2: `migrateCapabilityStore` moves an MCP manifest persisted before `origin` existed (it names no server URL, so none can be derived) into `stale`, kept with its stored body and named in the install panel as needing re-install, instead of letting restore reject it and the next write drop it. +- Long work uses the `async_poll` profile, never a longer request timeout. The transport is `{ kind: 'async', runtimeId, operation }`: the host binds the runtime (`asyncRuntime.ts`) and its endpoint, and a manifest cannot name one. `start` returns an external run id, which the vault ledger records before polling. Each start and each poll is still one request of at most 15 s, and the whole run is bounded by the profile's `maxDurationMs` (at most one hour): no sleep or poll request extends past that deadline, except the single observation recovery makes of a run that already outlived it. A run this session is still polling is not expired underneath it by another admission; it settles its own row. Omni never repeats a start. A write that was started and not observed to finish (deadline, repeated poll failures, or cancel) is `uncertain`. `reconcileAsyncExecution` and `recoverAsyncExecutions` read the ledger after a reload and poll the external run id; an uncertain run closes only when the destination reports a terminal status. Async capabilities run manually only. The server ledger carries the same fields (`db/migrations/005_capability_async.sql`). - Speech has a session id, a source (`unknown` until a local adapter proves otherwise), and cancel. A denied microphone is `Permission denied`, not the browser error code. ## Effects @@ -103,7 +72,7 @@ A manifest describes a capability. It does not grant itself authority. | Effect | Default methods | Runs when | | --- | --- | --- | | read | GET, HEAD | the user runs it | -| compute | `x-omni-effect: compute` on a safe method, or an explicit bring/MCP declaration | the user runs it | +| compute | `x-omni-effect: compute` on a safe method, or an explicit declaration on a host-built manifest | the user runs it | | write | POST, PUT, PATCH | approval is `approved`, and the user confirms the shown request for this run | | destructive | DELETE, or `destructiveHint`, or a tightened GET/HEAD | approval is `approved`, and the user confirms the shown request for this run | diff --git a/src/core/capabilities/admission.ts b/src/core/capabilities/admission.ts deleted file mode 100644 index d7a1f21..0000000 --- a/src/core/capabilities/admission.ts +++ /dev/null @@ -1,396 +0,0 @@ -// The host gate between a provider's proposal and an installed manifest. -// Everything a provider could use to grant itself authority is derived here: -// id from the transport, effect from the transport floor plus host trust, -// approval from the effect, and the credential slot from the destination. -// The result still goes through validateManifest and installProposal. - -import { canonicalCapabilityId, transportCredentialSlot } from './identity'; -import { - approvalForEffect, - canonicalInput, - canonicalOutput, - canonicalScopes, - effectAllowedForMethod, - effectForMethod, - isRecord, - PROVIDER_ID_PATTERN, - schemaDigestFor, - sealManifest, - validateManifest, - type AuthBinding, - type CapabilityEffect, - type CapabilityProviderKind, - type CapabilitySourceKind, - type CapabilityTransport, - type EffectSource, - type ManifestDraft, - type ManifestValidation -} from './manifest'; -import { installProposal, type InstallResult } from './registry'; -import type { CapabilityProposalV1, ProposedAuthRequirement, ProposedTransport } from './provider'; - -export interface AdmissionPolicy { - /** - * Host-owned trust list. Only a server named here may have its read hint - * accepted. Nothing a provider returns can add to it. - */ - trustedEffectHints?: { - mcpServers?: readonly string[]; - asyncRuntimes?: readonly string[]; - }; - nowMs?: number; - /** - * Host-owned list of origins (scheme://host[:port]) whose read and - * compute capabilities run through the server broker. Every other http - * capability runs browser_direct, whatever the proposal asked for. - */ - brokerOrigins?: readonly string[]; -} - -const PROPOSAL_KEYS = new Set([ - 'version', 'provider', 'externalIdentity', 'title', 'description', 'effectHint', - 'auth', 'transport', 'inputs', 'output', 'execution', 'provenance' -]); -const PROVIDER_KEYS = new Set(['id', 'kind']); -const IDENTITY_KEYS = new Set(['origin', 'operationId', 'sourceLocator', 'sourceRevision']); -const AUTH_KEYS = new Set(['kind', 'in', 'name', 'prefix', 'scopes']); -const HTTP_TRANSPORT_KEYS = new Set(['kind', 'access', 'baseUrl', 'method', 'path']); -const MCP_TRANSPORT_KEYS = new Set(['kind', 'serverId', 'origin', 'toolName']); -const ASYNC_TRANSPORT_KEYS = new Set(['kind', 'runtimeId', 'operation']); -const SYNC_EXECUTION_KEYS = new Set(['kind']); -const ASYNC_EXECUTION_KEYS = new Set(['kind', 'pollIntervalMs', 'maxDurationMs']); -const PROVENANCE_KEYS = new Set(['providerId', 'sourceLocator', 'discoveredAtMs']); -const INPUT_KEYS = new Set(['name', 'in', 'required', 'schema']); -const OUTPUT_KEYS = new Set(['schema', 'itemsPath', 'titlePath', 'presentation']); -const EFFECTS: CapabilityEffect[] = ['read', 'compute', 'write', 'destructive']; - -const SOURCE_KIND_FOR: Partial> = { - openapi: 'openapi', - mcp: 'mcp', - manual: 'bring', - web_data: 'web_data', - managed_integration: 'managed_integration' -}; - -/** Build the manifest a proposal would become. Nothing is installed. */ -export function manifestFromProposal(proposal: unknown, policy: AdmissionPolicy = {}): ManifestValidation { - const errors = shapeErrors(proposal); - if (errors.length > 0) return { ok: false, errors }; - const typed = proposal as CapabilityProposalV1; - - const sourceKind = SOURCE_KIND_FOR[typed.provider.kind]; - if (!sourceKind) return { ok: false, errors: [`provider kind ${typed.provider.kind} has no admission path`] }; - - const transport = hostTransport(typed.transport); - if ('error' in transport) return { ok: false, errors: [transport.error] }; - - const effect = hostEffect(typed, transport.transport, policy); - if ('error' in effect) return { ok: false, errors: [effect.error] }; - - const auth = hostAuth(typed.auth, transport.transport); - if ('error' in auth) return { ok: false, errors: [auth.error] }; - - if (transport.transport.kind === 'http' - && (effect.effect === 'read' || effect.effect === 'compute') - && auth.auth.kind !== 'oauth' - && policy.brokerOrigins?.includes(new URL(transport.transport.baseUrl).origin) === true) { - transport.transport = { ...transport.transport, access: 'server_broker' }; - } - - const inputs = typed.inputs.map(canonicalInput); - const output = canonicalOutput(typed.output); - const draft: ManifestDraft = { - version: 1, - id: canonicalCapabilityId(transport.transport), - title: typed.title, - ...(typed.description ? { description: typed.description } : {}), - source: { - kind: sourceKind, - locator: typed.externalIdentity.sourceLocator, - operationId: typed.externalIdentity.operationId - }, - provenance: { - providerId: typed.provider.id, - providerKind: typed.provider.kind, - externalId: typed.externalIdentity.operationId, - sourceLocator: typed.externalIdentity.sourceLocator, - ...(typed.externalIdentity.sourceRevision ? { sourceRevision: typed.externalIdentity.sourceRevision } : {}), - discoveredAtMs: typed.provenance.discoveredAtMs, - admittedAtMs: policy.nowMs ?? Date.now(), - schemaDigest: schemaDigestFor({ inputs, output }) - }, - effect: effect.effect, - effectSource: effect.source, - approval: approvalForEffect(effect.effect), - invocation: 'manual', - trigger: { kind: 'manual' }, - auth: auth.auth, - transport: transport.transport, - ...(typed.execution.kind === 'async_poll' - ? { - execution: { - kind: 'async_poll' as const, - pollIntervalMs: typed.execution.pollIntervalMs, - maxDurationMs: typed.execution.maxDurationMs - } - } - : {}), - inputs, - output - }; - return validateManifest(sealManifest(draft)); -} - -/** The only provider-facing install path. Side effects still land as `pending`. */ -export function admitProposal(proposal: unknown, policy: AdmissionPolicy = {}): InstallResult { - const built = manifestFromProposal(proposal, policy); - if (!built.ok || !built.manifest) return { ok: false, errors: built.errors }; - return installProposal(built.manifest); -} - -function hostTransport(transport: ProposedTransport): { transport: CapabilityTransport } | { error: string } { - if (transport.kind === 'http') { - // The proposal's access is ignored: where a credential is sent from - // is the host's choice (AdmissionPolicy.brokerOrigins). - return { - transport: { - kind: 'http', - access: 'browser_direct', - baseUrl: transport.baseUrl, - method: transport.method, - path: transport.path - } - }; - } - if (transport.kind === 'mcp') { - return { transport: { kind: 'mcp', serverId: transport.serverId, origin: transport.origin, toolName: transport.toolName } }; - } - if (transport.kind === 'async') { - return { transport: { kind: 'async', runtimeId: transport.runtimeId, operation: transport.operation } }; - } - return { error: 'providers may propose http, mcp, or async transports only' }; -} - -function hostEffect( - proposal: CapabilityProposalV1, - transport: CapabilityTransport, - policy: AdmissionPolicy -): { effect: CapabilityEffect; source: EffectSource } | { error: string } { - const hint = proposal.effectHint; - if (transport.kind === 'http') { - if (hint === undefined) return { effect: effectForMethod(transport.method), source: 'method' }; - if (!effectAllowedForMethod(transport.method, hint)) { - return { error: `${transport.method} cannot be declared ${hint}` }; - } - return { effect: hint, source: proposal.provider.kind === 'openapi' ? 'extension' : 'declared' }; - } - if (transport.kind === 'mcp' || transport.kind === 'async') { - // No method floor exists here, so an untrusted source lands at write. - const trusted = transport.kind === 'mcp' - ? policy.trustedEffectHints?.mcpServers?.includes(transport.serverId) === true - : policy.trustedEffectHints?.asyncRuntimes?.includes(transport.runtimeId) === true; - const source: EffectSource = trusted ? 'annotation' : 'declared'; - if (hint === 'destructive') return { effect: 'destructive', source }; - if (trusted && hint === 'read') return { effect: 'read', source }; - return { effect: 'write', source }; - } - return { error: 'transport has no effect floor' }; -} - -function hostAuth(auth: ProposedAuthRequirement, transport: CapabilityTransport): { auth: AuthBinding } | { error: string } { - if (auth.kind === 'none') return { auth: { kind: 'none' } }; - const placement = { - kind: auth.kind, - ...(auth.kind === 'apiKey' ? { in: auth.in, name: auth.name } : {}), - ...(auth.kind === 'oauth' ? { scopes: canonicalScopes(auth.scopes) } : {}) - } as Pick; - const secretRef = transportCredentialSlot(transport, placement); - if (!secretRef) return { error: `${transport.kind} transports cannot bind a credential slot` }; - return { - auth: { - ...placement, - ...('prefix' in auth && auth.prefix ? { prefix: auth.prefix } : {}), - secretRef - } - }; -} - -function shapeErrors(input: unknown): string[] { - const errors: string[] = []; - if (!isRecord(input)) return ['proposal must be an object']; - closed(input, PROPOSAL_KEYS, 'proposal', errors, { - approval: 'approval is host-owned', - effect: 'effect is classified by admission; send effectHint', - id: 'capability ids are derived from the transport', - digest: 'the digest is sealed by admission', - trusted: 'trust is a host policy' - }); - if (input.version !== 1) errors.push('proposal.version must be 1'); - - if (!isRecord(input.provider)) { - errors.push('proposal.provider is required'); - } else { - closed(input.provider, PROVIDER_KEYS, 'proposal.provider', errors); - if (typeof input.provider.id !== 'string' || !PROVIDER_ID_PATTERN.test(input.provider.id)) { - errors.push('proposal.provider.id is invalid'); - } - if (typeof input.provider.kind !== 'string') errors.push('proposal.provider.kind is invalid'); - } - - if (!isRecord(input.externalIdentity)) { - errors.push('proposal.externalIdentity is required'); - } else { - const identity = input.externalIdentity; - closed(identity, IDENTITY_KEYS, 'proposal.externalIdentity', errors); - if (typeof identity.operationId !== 'string' || identity.operationId.length === 0 || identity.operationId.length > 120) { - errors.push('proposal.externalIdentity.operationId must be a short non-empty string'); - } - if (typeof identity.sourceLocator !== 'string' || identity.sourceLocator.length === 0 || identity.sourceLocator.length > 200) { - errors.push('proposal.externalIdentity.sourceLocator must be a short non-empty string'); - } - if (identity.sourceRevision !== undefined && (typeof identity.sourceRevision !== 'string' || identity.sourceRevision.length > 120)) { - errors.push('proposal.externalIdentity.sourceRevision must be a short string'); - } - if (identity.origin !== undefined && typeof identity.origin !== 'string') { - errors.push('proposal.externalIdentity.origin must be a string'); - } - } - - if (input.effectHint !== undefined && !EFFECTS.includes(input.effectHint as CapabilityEffect)) { - errors.push('proposal.effectHint is invalid'); - } - - if (!isRecord(input.auth)) { - errors.push('proposal.auth is required'); - } else { - closed(input.auth, AUTH_KEYS, 'proposal.auth', errors, { - secretRef: 'credential slots are derived by admission', - token: 'tokens are bound to a slot by the host, never proposed', - accessToken: 'tokens are bound to a slot by the host, never proposed', - refreshToken: 'tokens are bound to a slot by the host, never proposed' - }); - const kind = input.auth.kind; - if (kind !== 'none' && kind !== 'apiKey' && kind !== 'bearer' && kind !== 'basic' && kind !== 'oauth') { - errors.push('proposal.auth.kind is invalid'); - } - if (kind === 'oauth' && !Array.isArray(input.auth.scopes)) errors.push('proposal.auth.scopes is required for oauth'); - if (kind !== 'oauth' && input.auth.scopes !== undefined) errors.push('proposal.auth.scopes is only for oauth'); - } - - if (!isRecord(input.transport)) { - errors.push('proposal.transport is required'); - } else if (input.transport.kind === 'http') { - closed(input.transport, HTTP_TRANSPORT_KEYS, 'proposal.transport', errors); - // The id and the credential slot are both derived from this URL, and - // both parse it. A proposal that cannot be parsed stops here, as an - // error, before anything is derived from it. - const origin = originOf(input.transport.baseUrl); - if (origin === undefined) { - errors.push('proposal.transport.baseUrl must be an absolute URL'); - } else if (isRecord(input.externalIdentity) && typeof input.externalIdentity.origin === 'string') { - if (origin !== input.externalIdentity.origin) { - errors.push('proposal.externalIdentity.origin does not match the transport'); - } - } - } else if (input.transport.kind === 'mcp') { - closed(input.transport, MCP_TRANSPORT_KEYS, 'proposal.transport', errors); - // The credential slot is derived from this origin, as from an http baseUrl. - const origin = originOf(input.transport.origin); - if (origin === undefined || origin !== input.transport.origin) { - errors.push('proposal.transport.origin must be a URL origin'); - } else if (isRecord(input.externalIdentity) && typeof input.externalIdentity.origin === 'string' - && origin !== input.externalIdentity.origin) { - errors.push('proposal.externalIdentity.origin does not match the transport'); - } - } else if (input.transport.kind === 'async') { - closed(input.transport, ASYNC_TRANSPORT_KEYS, 'proposal.transport', errors, { - baseUrl: 'async endpoints are bound by the host runtime', - url: 'async endpoints are bound by the host runtime' - }); - if (isRecord(input.externalIdentity) && input.externalIdentity.origin !== undefined) { - errors.push('proposal.externalIdentity.origin is not used by async transports'); - } - } else { - errors.push('providers may propose http, mcp, or async transports only'); - } - - if (!Array.isArray(input.inputs)) { - errors.push('proposal.inputs must be a list'); - } else { - for (const entry of input.inputs) { - if (!isRecord(entry)) errors.push('each proposal input must be an object'); - else closed(entry, INPUT_KEYS, `proposal.inputs.${String(entry.name)}`, errors); - } - } - if (!isRecord(input.output)) { - errors.push('proposal.output is required'); - } else { - closed(input.output, OUTPUT_KEYS, 'proposal.output', errors); - if (untypedResult(input.output.schema)) { - errors.push('proposal.output.schema must be typed; an untyped result is not admitted as any'); - } - } - - if (!isRecord(input.execution)) { - errors.push('proposal.execution is required'); - } else { - const kind = input.execution.kind; - if (kind === 'sync') closed(input.execution, SYNC_EXECUTION_KEYS, 'proposal.execution', errors); - else if (kind === 'async_poll') closed(input.execution, ASYNC_EXECUTION_KEYS, 'proposal.execution', errors); - else errors.push('proposal.execution.kind is not supported'); - const asyncTransport = isRecord(input.transport) && input.transport.kind === 'async'; - if (asyncTransport !== (kind === 'async_poll')) { - errors.push('proposal.execution must be async_poll exactly when the transport is async'); - } - } - - if (!isRecord(input.provenance)) { - errors.push('proposal.provenance is required'); - } else { - closed(input.provenance, PROVENANCE_KEYS, 'proposal.provenance', errors); - if (isRecord(input.provider) && input.provenance.providerId !== input.provider.id) { - errors.push('proposal.provenance.providerId must match proposal.provider.id'); - } - if (isRecord(input.externalIdentity) && input.provenance.sourceLocator !== input.externalIdentity.sourceLocator) { - errors.push('proposal.provenance.sourceLocator must match the external identity'); - } - const at = input.provenance.discoveredAtMs; - if (typeof at !== 'number' || !Number.isInteger(at) || at < 0) { - errors.push('proposal.provenance.discoveredAtMs must be a timestamp'); - } - } - return errors; -} - -function closed( - record: Record, - allowed: Set, - path: string, - errors: string[], - reasons: Record = {} -): void { - for (const key of Object.keys(record)) { - if (allowed.has(key)) continue; - errors.push(`${path}.${key} is not a proposal field${reasons[key] ? `; ${reasons[key]}` : ''}`); - } -} - -/** - * The result, or each item of a result list, must be typed. A field inside a - * typed result may be `any`: the API does return it and its type is unknown - * (jsonSchema.ts), and an `any` source is never assignable to a typed input. - */ -function untypedResult(schema: unknown): boolean { - if (!isRecord(schema)) return false; - if (schema.kind === 'any') return true; - return schema.kind === 'array' && isRecord(schema.items) && schema.items.kind === 'any'; -} - -function originOf(baseUrl: unknown): string | undefined { - if (typeof baseUrl !== 'string') return undefined; - try { - return new URL(baseUrl).origin; - } catch { - return undefined; - } -} diff --git a/src/core/capabilities/asyncExecution.test.ts b/src/core/capabilities/asyncExecution.test.ts index ad9111a..15e9c68 100644 --- a/src/core/capabilities/asyncExecution.test.ts +++ b/src/core/capabilities/asyncExecution.test.ts @@ -2,12 +2,12 @@ import 'fake-indexeddb/auto'; import { describe, it, expect, beforeEach, afterEach } from 'vitest'; import { readFileSync } from 'node:fs'; import path from 'node:path'; -import { admitProposal, manifestFromProposal } from './admission'; import { approveCapability, clearCapabilities, installProposal } from './registry'; import { recoverAsyncExecutions, reconcileAsyncExecution } from './execute'; // Write runs need a confirmed preview; these tests exercise the engine after one. import { executeConfirmed as executeCapability } from '../../../test/confirmedExecute'; -import { transportCredentialSlot } from './identity'; +import { canonicalCapabilityId, transportCredentialSlot } from './identity'; +import { canonicalize, sha256 } from './hash'; import { capabilitySecrets } from './secrets'; import { clearExecutionLedger, @@ -24,8 +24,13 @@ import { type AsyncJobRuntime, type AsyncObservation } from './asyncRuntime'; -import { sealManifest, validateManifest, type CapabilityManifest } from './manifest'; -import type { CapabilityProposalV1 } from './provider'; +import { + sealManifest, + validateManifest, + type CapabilityEffect, + type CapabilityExecutionProfile, + type CapabilityManifest +} from './manifest'; import { memoryLedger } from '../services/server/capability.ledger'; import { vaultStorage, __resetVaultConnection } from '../vault/vaultStorage'; @@ -93,9 +98,8 @@ function gatedClock(start = Date.now()): TestClock & { parked: Promise; op // A runtime whose start acknowledges early with a run id, and whose poll // returns the run with `data` in Maxun's field names. It exercises Omni's async // lifecycle. It is not Maxun's start contract: Maxun's open-source server -// answers its run start only when the run finishes, which is why the real -// `maxun` runtime fences start (providers/maxunRuntime.ts). It measures nothing -// about Maxun. +// answers its run start only when the run finishes, so a real Maxun runtime +// would have to fence start. It measures nothing about Maxun. // --------------------------------------------------------------------------- interface MaxunRun { @@ -170,30 +174,56 @@ function maxunFixture(clock: AsyncClock, options: FixtureOptions = {}) { const NOW = 1_790_000_000_000; -function asyncProposal(overrides: Partial = {}): CapabilityProposalV1 { - return { +interface AsyncManifestOptions { + effect?: CapabilityEffect; + execution?: CapabilityExecutionProfile; +} + +/** The async manifest the host installs for the fixture robot. */ +function asyncManifest(options: AsyncManifestOptions = {}): CapabilityManifest { + const effect = options.effect ?? 'write'; + const transport = { kind: 'async' as const, runtimeId: 'maxun', operation: 'robot-42' }; + const inputs: CapabilityManifest['inputs'] = [{ name: 'url', in: 'argument', required: false, schema: { kind: 'string' } }]; + const output: CapabilityManifest['output'] = { + schema: { kind: 'object', properties: { markdown: { kind: 'string' } }, required: ['markdown'] }, + presentation: 'raw' + }; + return sealManifest({ version: 1, - provider: { id: 'fixture.jobs', kind: 'manual' }, - externalIdentity: { operationId: 'robot-42', sourceLocator: 'fixture://maxun/robot-42' }, + id: canonicalCapabilityId(transport), title: 'Scrape pricing page', - auth: { kind: 'apiKey', in: 'header', name: 'x-api-key' }, - transport: { kind: 'async', runtimeId: 'maxun', operation: 'robot-42' }, - inputs: [{ name: 'url', in: 'argument', required: false, schema: { kind: 'string' } }], - output: { - schema: { kind: 'object', properties: { markdown: { kind: 'string' } }, required: ['markdown'] }, - presentation: 'raw' + source: { kind: 'bring', locator: 'fixture://maxun/robot-42', operationId: 'robot-42' }, + provenance: { + providerId: 'fixture.jobs', + providerKind: 'manual', + externalId: 'robot-42', + sourceLocator: 'fixture://maxun/robot-42', + discoveredAtMs: NOW, + admittedAtMs: NOW, + schemaDigest: sha256(canonicalize({ inputs, output })) }, - execution: { kind: 'async_poll', pollIntervalMs: 5_000, maxDurationMs: 120_000 }, - provenance: { providerId: 'fixture.jobs', sourceLocator: 'fixture://maxun/robot-42', discoveredAtMs: NOW }, - ...overrides - }; + effect, + effectSource: effect === 'write' ? 'declared' : 'annotation', + approval: effect === 'read' ? 'auto' : 'pending', + invocation: 'manual', + trigger: { kind: 'manual' }, + auth: { kind: 'apiKey', in: 'header', name: 'x-api-key', secretRef: SLOT }, + transport, + execution: options.execution ?? { kind: 'async_poll', pollIntervalMs: 5_000, maxDurationMs: 120_000 }, + inputs, + output + }); } -/** Admit and approve as the host would. The test never approves from the proposal. */ -function installApproved(overrides: Partial = {}): CapabilityManifest { - const installed = admitProposal(asyncProposal(overrides), { nowMs: NOW }); +function installAsync(options: AsyncManifestOptions = {}): CapabilityManifest { + const installed = installProposal(asyncManifest(options)); expect(installed.ok, installed.errors?.join('; ')).toBe(true); - const manifest = installed.manifest!; + return installed.manifest!; +} + +/** Install and approve as the host would. */ +function installApproved(options: AsyncManifestOptions = {}): CapabilityManifest { + const manifest = installAsync(options); if (manifest.approval === 'pending') { const approved = approveCapability(manifest.id); expect(approved.ok).toBe(true); @@ -219,75 +249,17 @@ afterEach(() => { capabilitySecrets.clear(); }); -describe('async_poll admission', () => { - it('admits an async proposal with the credential slot bound to the runtime and the profile under the digest', () => { - const built = manifestFromProposal(asyncProposal(), { nowMs: NOW }); - expect(built.ok, built.errors.join('; ')).toBe(true); - const manifest = built.manifest!; - expect(manifest.transport).toEqual({ kind: 'async', runtimeId: 'maxun', operation: 'robot-42' }); - expect(manifest.execution).toEqual({ kind: 'async_poll', pollIntervalMs: 5_000, maxDurationMs: 120_000 }); - expect(manifest.auth.secretRef).toBe(SLOT); +describe('async_poll manifest', () => { + it('keeps the profile under the digest', () => { + const manifest = asyncManifest(); + expect(validateManifest(manifest).ok).toBe(true); expect(JSON.stringify(manifest)).not.toContain(KEY); - const stretched = { ...manifest, execution: { ...manifest.execution!, maxDurationMs: 3_000_000 } }; expect(validateManifest(stretched).ok).toBe(false); }); - it('lands at write and pending unless the host trusts the runtime, and never takes approval from the proposal', () => { - const untrusted = manifestFromProposal(asyncProposal({ effectHint: 'read' }), { nowMs: NOW }).manifest!; - expect(untrusted.effect).toBe('write'); - expect(untrusted.approval).toBe('pending'); - - const trusted = manifestFromProposal(asyncProposal({ effectHint: 'read' }), { - nowMs: NOW, - trustedEffectHints: { asyncRuntimes: ['maxun'] } - }).manifest!; - expect(trusted.effect).toBe('read'); - expect(trusted.approval).toBe('auto'); - - const destructive = manifestFromProposal(asyncProposal({ effectHint: 'destructive' }), { - nowMs: NOW, - trustedEffectHints: { asyncRuntimes: ['maxun'] } - }).manifest!; - expect(destructive.effect).toBe('destructive'); - expect(destructive.approval).toBe('pending'); - - const forged = manifestFromProposal({ ...asyncProposal(), approval: 'auto' }, { nowMs: NOW }); - expect(forged.ok).toBe(false); - expect(forged.errors.join(' ')).toMatch(/approval is host-owned/); - }); - - it('refuses an async proposal that names its own endpoint, a mismatched profile, or an untyped result', () => { - const endpoint = manifestFromProposal({ - ...asyncProposal(), - transport: { kind: 'async', runtimeId: 'maxun', operation: 'robot-42', baseUrl: 'https://evil.example.test' } - }, { nowMs: NOW }); - expect(endpoint.ok).toBe(false); - expect(endpoint.errors.join(' ')).toMatch(/bound by the host runtime/); - - const syncProfile = manifestFromProposal(asyncProposal({ execution: { kind: 'sync' } }), { nowMs: NOW }); - expect(syncProfile.ok).toBe(false); - - const tooFast = manifestFromProposal( - asyncProposal({ execution: { kind: 'async_poll', pollIntervalMs: 10, maxDurationMs: 60_000 } }), - { nowMs: NOW } - ); - expect(tooFast.ok).toBe(false); - - const untyped = manifestFromProposal( - asyncProposal({ output: { schema: { kind: 'any' }, presentation: 'raw' } }), - { nowMs: NOW } - ); - expect(untyped.ok).toBe(false); - expect(untyped.errors.join(' ')).toMatch(/must be typed/); - }); - - it('refuses an automatic trigger on an async manifest even outside admission', () => { - const manifest = manifestFromProposal(asyncProposal(), { - nowMs: NOW, - trustedEffectHints: { asyncRuntimes: ['maxun'] } - }).manifest!; - const { digest: _digest, ...draft } = manifest; + it('refuses an automatic trigger on an async manifest', () => { + const { digest: _digest, ...draft } = asyncManifest({ effect: 'read' }); const onInterval = sealManifest({ ...draft, invocation: 'auto', trigger: { kind: 'interval', everyMs: 60_000 } }); const result = installProposal(onInterval); expect(result.ok).toBe(false); @@ -337,16 +309,16 @@ describe('async_poll execution', () => { const clock = virtualClock(NOW); const fixture = maxunFixture(clock); bindAsyncRuntime('maxun', fixture.runtime); - const installed = admitProposal(asyncProposal(), { nowMs: NOW }); - expect(installed.manifest?.approval).toBe('pending'); + const installed = installAsync(); + expect(installed.approval).toBe('pending'); - const refused = await executeCapability(installed.manifest!.id, {}, { clock }); + const refused = await executeCapability(installed.id, {}, { clock }); expect(refused.error?.code).toBe('EFFECT_NOT_APPROVED'); expect(fixture.calls.start).toBe(0); expect(executionRecords()).toHaveLength(0); - approveCapability(installed.manifest!.id); - const ran = await executeCapability(installed.manifest!.id, {}, { clock }); + approveCapability(installed.id); + const ran = await executeCapability(installed.id, {}, { clock }); expect(ran.ok).toBe(true); expect(fixture.calls.start).toBe(1); }); @@ -400,13 +372,10 @@ describe('async_poll execution', () => { expect(failed.error?.code).toBe('ASYNC_RUN_FAILED'); expect(executionRecord(failed.runId!)?.lastObservedStatus).toBe('failed'); - const read = admitProposal(asyncProposal({ effectHint: 'read' }), { - nowMs: NOW, - trustedEffectHints: { asyncRuntimes: ['maxun'] } - }); - expect(read.manifest?.effect).toBe('read'); + const read = installAsync({ effect: 'read' }); + expect(read.effect).toBe('read'); bindAsyncRuntime('maxun', maxunFixture(clock, { outcome: 'malformed' }).runtime); - const malformed = await executeCapability(read.manifest!.id, {}, { clock }); + const malformed = await executeCapability(read.id, {}, { clock }); expect(malformed.error?.code).toBe('TYPED_OUTPUT_MISMATCH'); expect(executionRecord(malformed.runId!)?.status).toBe('failed'); }); @@ -416,12 +385,7 @@ describe('async_poll execution', () => { const clock = virtualClock(NOW); const fixture = maxunFixture(clock, { outcome: 'malformed' }); bindAsyncRuntime('maxun', fixture.runtime); - const manifest = effectHint === 'write' - ? installApproved() - : approveCapability(admitProposal(asyncProposal({ effectHint: 'destructive' }), { - nowMs: NOW, - trustedEffectHints: { asyncRuntimes: ['maxun'] } - }).manifest!.id).manifest!; + const manifest = installApproved({ effect: effectHint }); expect(manifest.effect).toBe(effectHint); const key = `mismatch-${effectHint}-001`; @@ -511,11 +475,8 @@ describe('async_poll execution', () => { it('fails a trusted read at the deadline instead of calling it uncertain', async () => { const clock = virtualClock(NOW); bindAsyncRuntime('maxun', maxunFixture(clock, { finishAfterMs: 10 * 60_000 }).runtime); - const installed = admitProposal(asyncProposal({ effectHint: 'read' }), { - nowMs: NOW, - trustedEffectHints: { asyncRuntimes: ['maxun'] } - }); - const result = await executeCapability(installed.manifest!.id, {}, { clock }); + const installed = installAsync({ effect: 'read' }); + const result = await executeCapability(installed.id, {}, { clock }); expect(result.error?.code).toBe('DEADLINE'); expect(executionRecord(result.runId!)?.status).toBe('failed'); }); diff --git a/src/core/capabilities/bring.ts b/src/core/capabilities/bring.ts deleted file mode 100644 index b5b7a55..0000000 --- a/src/core/capabilities/bring.ts +++ /dev/null @@ -1,94 +0,0 @@ -// "Bring your API" accepts a structured description, never free text. -// An agent may fill this in. The same validator used for OpenAPI decides -// whether the proposal is a manifest. - -import { - approvalForEffect, - effectAllowedForMethod, - effectForMethod, - isRecord, - sealManifest, - validateManifest, - type CapabilityEffect, - type CapabilityInput, - type CapabilityManifest, - type HttpMethod -} from './manifest'; -import type { ValueType } from './valueType'; -import { canonicalCapabilityId, credentialSlot } from './identity'; -import type { CompileResult } from './openapi'; - -export interface BringApiDescription { - id?: string; - title: string; - description?: string; - baseUrl: string; - method: HttpMethod; - path: string; - effect?: CapabilityEffect; - auth?: CapabilityManifest['auth']; - inputs?: CapabilityInput[]; - output: { - schema: ValueType; - itemsPath?: string; - titlePath?: string; - presentation?: CapabilityManifest['output']['presentation']; - }; -} - -export function compileBring(description: BringApiDescription): CompileResult { - if (!isRecord(description)) { - return { manifests: [], errors: [{ message: 'description must be an object' }] }; - } - if (typeof description.title !== 'string' || !description.title.trim()) { - return { manifests: [], errors: [{ message: 'title is required' }] }; - } - const method = description.method; - const effect = description.effect ?? effectForMethod(method); - if (description.effect && !effectAllowedForMethod(method, effect)) { - return { manifests: [], errors: [{ message: `${method} cannot be declared ${effect}` }] }; - } - - const provided = description.auth; - const auth = provided && provided.kind !== 'none' - ? { ...provided, secretRef: credentialSlot(description.baseUrl, provided) } - : { kind: 'none' as const }; - const transport = { - kind: 'http' as const, - access: 'browser_direct' as const, - baseUrl: description.baseUrl, - method, - path: description.path - }; - - const schema = description.output?.schema; - const presentation = description.output?.presentation - ?? (schema?.kind === 'array' ? 'items' : schema?.kind === 'string' ? 'content' : 'raw'); - - const sealed = sealManifest({ - version: 1, - id: canonicalCapabilityId(transport), - title: description.title.trim().slice(0, 120), - ...(description.description ? { description: description.description.slice(0, 2000) } : {}), - source: { kind: 'bring', locator: 'bring' }, - effect, - effectSource: description.effect ? 'declared' : 'method', - approval: approvalForEffect(effect), - invocation: 'manual', - auth, - transport, - inputs: description.inputs ?? [], - output: { - schema, - presentation, - ...(description.output?.itemsPath ? { itemsPath: description.output.itemsPath } : {}), - ...(description.output?.titlePath ? { titlePath: description.output.titlePath } : {}) - } - }); - - const validated = validateManifest(sealed); - if (!validated.ok || !validated.manifest) { - return { manifests: [], errors: [{ message: validated.errors.join('; ') }] }; - } - return { manifests: [validated.manifest], errors: [] }; -} diff --git a/src/core/capabilities/capability.vertical.test.ts b/src/core/capabilities/capability.vertical.test.ts index cec067d..9fcfcd1 100644 --- a/src/core/capabilities/capability.vertical.test.ts +++ b/src/core/capabilities/capability.vertical.test.ts @@ -1,23 +1,27 @@ import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'; import { sha256 } from './hash'; -import { credentialSlot } from './identity'; +import { canonicalCapabilityId, credentialSlot } from './identity'; import { compileOpenApi } from './openapi'; -import { compileMcpTools } from './mcp'; -import { compileBring } from './bring'; -import { validateManifest } from './manifest'; +import { + approvalForEffect, + sealManifest, + validateManifest, + type CapabilityEffect, + type CapabilityInput, + type CapabilityManifest +} from './manifest'; import { dropImportedApprovals } from './importAdmission'; import { type CapabilitySnapshot, approveCapability, clearCapabilities, - exportSnapshot, - getCapability, installProposal, restoreSnapshot, runInstalledCapability, uninstallCapability } from './registry'; import { bindMcpTransport, unbindMcpTransport } from './execute'; +import { allCapabilities, readCapability } from './state'; // Write runs need a confirmed preview; these tests exercise the engine after one. import { executeConfirmed as executeCapability } from '../../../test/confirmedExecute'; import { capabilitySecrets } from './secrets'; @@ -165,6 +169,31 @@ afterEach(() => { vi.restoreAllMocks(); }); +/** A manifest for one tool on an MCP server, as the host would install it. */ +function mcpManifest( + origin: string, + toolName: string, + effect: CapabilityEffect, + inputs: CapabilityInput[], + output: CapabilityManifest['output'] +): CapabilityManifest { + const transport = { kind: 'mcp' as const, serverId: 'board', origin, toolName }; + return sealManifest({ + version: 1, + id: canonicalCapabilityId(transport), + title: toolName, + source: { kind: 'mcp', locator: 'board', operationId: toolName }, + effect, + effectSource: effect === 'write' ? 'declared' : 'annotation', + approval: approvalForEffect(effect), + invocation: 'manual', + auth: { kind: 'none' }, + transport, + inputs, + output + }); +} + describe('capability compiler', () => { it('hashes with sha256', () => { expect(sha256('abc')).toBe('ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad'); @@ -265,25 +294,6 @@ describe('capability compiler', () => { const catalogSource = useBlockStore.getState().addBlock(blockRegistry.get('hackernews_feed')!, { x: 0, y: 200 }); expect(wireService.createWire(catalogSource, personaId)).not.toBe(''); - const count = compileBring({ - title: 'Count posts', - baseUrl: 'https://board.example.test/v1', - method: 'POST', - path: '/count', - inputs: [{ - name: 'count', - in: 'body', - required: true, - schema: { kind: 'integer' } - }], - output: { schema: { kind: 'integer' } } - }); - expect(count.errors).toEqual([]); - expect(installProposal(count.manifests[0]).ok).toBe(true); - const countId = useBlockStore.getState().addBlock(blockRegistry.get(count.manifests[0].id)!, { x: 200, y: 200 }); - expect(wireService.createWire(instanceId, countId)).toBe(''); - expect(useWireStore.getState().wireExists(instanceId, countId)).toBe(false); - const listId = byOp('listPosts').id; expect(uninstallCapability(listId)).toBe(true); expect(blockRegistry.has(listId)).toBe(false); @@ -341,14 +351,14 @@ describe('capability compiler', () => { const create = byOp('createPost'); installProposal(create); approveCapability(create.id); - const snapshot = exportSnapshot(); + const snapshot: CapabilitySnapshot = { version: 1, manifests: allCapabilities() }; expect(JSON.stringify(snapshot)).not.toContain('super-secret'); clearCapabilities(); - expect(getCapability(create.id)).toBeUndefined(); + expect(readCapability(create.id)).toBeUndefined(); const restored = restoreSnapshot(snapshot); expect(restored.installed).toContain(create.id); - expect(getCapability(create.id)?.approval).toBe('approved'); + expect(readCapability(create.id)?.approval).toBe('approved'); const forged = { version: 1 as const, @@ -360,19 +370,14 @@ describe('capability compiler', () => { }); it('gives two MCP servers with the same label and tool at different origins distinct ids that coexist', () => { - const tool = { - serverId: 'board', - name: 'list_board', - inputSchema: { type: 'object', properties: {} }, - outputSchema: { type: 'array', items: { type: 'string' } } - }; - const [first] = compileMcpTools([{ ...tool, origin: 'https://board.example.test' }]).manifests; - const [second] = compileMcpTools([{ ...tool, origin: 'https://other.example.test' }]).manifests; + const output = { schema: { kind: 'array' as const, items: { kind: 'string' as const } }, presentation: 'items' as const }; + const first = mcpManifest('https://board.example.test', 'list_board', 'write', [], output); + const second = mcpManifest('https://other.example.test', 'list_board', 'write', [], output); expect(first.id).not.toBe(second.id); expect(installProposal(first).ok).toBe(true); expect(installProposal(second).ok).toBe(true); - expect(getCapability(first.id)?.transport).toMatchObject({ origin: 'https://board.example.test' }); - expect(getCapability(second.id)?.transport).toMatchObject({ origin: 'https://other.example.test' }); + expect(readCapability(first.id)?.transport).toMatchObject({ origin: 'https://board.example.test' }); + expect(readCapability(second.id)?.transport).toMatchObject({ origin: 'https://other.example.test' }); }); it('an imported capability blob lands writes pending; the same blob through rehydrate keeps approval', () => { @@ -381,7 +386,7 @@ describe('capability compiler', () => { installProposal(create); installProposal(list); approveCapability(create.id); - const snapshot = exportSnapshot(); + const snapshot: CapabilitySnapshot = { version: 1, manifests: allCapabilities() }; // The persisted envelope the capability store writes to the vault. const persisted = JSON.stringify({ state: snapshot, version: 1 }); @@ -397,57 +402,31 @@ describe('capability compiler', () => { const fromImport = restoreSnapshot(admitted.state); expect(fromImport.installed).toEqual(expect.arrayContaining([create.id, list.id])); expect(fromImport.rejected).toEqual([]); - expect(getCapability(create.id)?.approval).toBe('pending'); - expect(getCapability(create.id)?.digest).toBe(create.digest); - expect(getCapability(list.id)?.approval).toBe('auto'); + expect(readCapability(create.id)?.approval).toBe('pending'); + expect(readCapability(create.id)?.digest).toBe(create.digest); + expect(readCapability(list.id)?.approval).toBe('auto'); // Pending is the same state installProposal leaves; approval is the only way out. - expect(getCapability(create.id)).toEqual(installProposal({ ...create, approval: 'approved' }).manifest); + expect(readCapability(create.id)).toEqual(installProposal({ ...create, approval: 'approved' }).manifest); // Rehydrate path (the browser's own blob): the granted approval survives. clearCapabilities(); restoreSnapshot(snapshot); - expect(getCapability(create.id)?.approval).toBe('approved'); + expect(readCapability(create.id)?.approval).toBe('approved'); }); - it('compiles MCP tools and Bring descriptions through the same gate', async () => { - const compiled = compileMcpTools([ - { - serverId: 'board', - origin: 'https://board.example.test', - name: 'list_board', - description: 'Read the board', - annotations: { readOnlyHint: true }, - inputSchema: { - type: 'object', - properties: { q: { type: 'string' } } - }, - outputSchema: { type: 'array', items: { type: 'string' } } - }, - { - serverId: 'board', - origin: 'https://board.example.test', - name: 'wipe_board', - annotations: { readOnlyHint: true, destructiveHint: true }, - outputSchema: { type: 'boolean' } - } - ]); - expect(compiled.errors).toEqual([]); - const read = compiled.manifests.find(manifest => manifest.source.operationId === 'list_board'); - const wipe = compiled.manifests.find(manifest => manifest.source.operationId === 'wipe_board'); - expect(read).toMatchObject({ effect: 'write', approval: 'pending' }); - const trusted = compileMcpTools([{ - serverId: 'board', - origin: 'https://board.example.test', - name: 'list_board', - annotations: { readOnlyHint: true }, - inputSchema: { type: 'object', properties: { q: { type: 'string' } } }, - outputSchema: { type: 'array', items: { type: 'string' } } - }], { trustedAnnotations: true }); - expect(trusted.manifests[0]).toMatchObject({ effect: 'read', approval: 'auto' }); + it('runs an MCP read only through its bound transport and holds a destructive tool for approval', async () => { + const trustedRead = mcpManifest( + 'https://board.example.test', + 'list_board', + 'read', + [{ name: 'q', in: 'argument', required: false, schema: { kind: 'string' } }], + { schema: { kind: 'array', items: { kind: 'string' } }, presentation: 'items' } + ); + const wipe = mcpManifest('https://board.example.test', 'wipe_board', 'destructive', [], { schema: { kind: 'boolean' }, presentation: 'raw' }); + expect(trustedRead).toMatchObject({ effect: 'read', approval: 'auto' }); expect(wipe).toMatchObject({ effect: 'destructive', approval: 'pending' }); - const trustedRead = trusted.manifests[0]; - installProposal(trustedRead); - installProposal(wipe); + expect(installProposal(trustedRead).ok).toBe(true); + expect(installProposal(wipe).ok).toBe(true); const calls: unknown[] = []; bindMcpTransport('board', { @@ -458,7 +437,7 @@ describe('capability compiler', () => { } }); - const wiped = await executeCapability(wipe!.id, {}); + const wiped = await executeCapability(wipe.id, {}); expect(wiped.error?.code).toBe('EFFECT_NOT_APPROVED'); expect(calls).toHaveLength(0); @@ -470,15 +449,5 @@ describe('capability compiler', () => { unbindMcpTransport('board'); const unbound = await executeCapability(trustedRead.id, {}); expect(unbound.error?.code).toBe('TRANSPORT_NOT_BOUND'); - - const brought = compileBring({ - title: 'Ping board', - baseUrl: 'https://board.example.test/v1', - method: 'GET', - path: '/health', - output: { schema: { kind: 'object', properties: { ok: { kind: 'boolean' } }, required: ['ok'] } } - }); - expect(brought.manifests[0]).toMatchObject({ effect: 'read', approval: 'auto', source: { kind: 'bring' } }); - expect(installProposal({ ...brought.manifests[0], token: 'raw-token' }).ok).toBe(false); }); }); diff --git a/src/core/capabilities/confirmationIntegrity.test.ts b/src/core/capabilities/confirmationIntegrity.test.ts index 9fbb887..a78464c 100644 --- a/src/core/capabilities/confirmationIntegrity.test.ts +++ b/src/core/capabilities/confirmationIntegrity.test.ts @@ -4,8 +4,6 @@ import { canonicalCapabilityId, credentialSlot } from './identity'; import { DENIED_HEADER_NAMES, sealManifest, validateManifest, type CapabilityInput, type CapabilityManifest } from './manifest'; import { approveCapability, clearCapabilities, installProposal } from './registry'; import { bindMcpTransport, executeCapability, previewCapabilityRun, unbindMcpTransport } from './execute'; -import { admitProposal } from './admission'; -import type { CapabilityProposalV1 } from './provider'; import { capabilitySecrets } from './secrets'; const SPEC = { @@ -191,22 +189,25 @@ describe('the confirmed request is the dispatched request', () => { }); it('hands a tool exactly the confirmed arguments', async () => { - const proposal: CapabilityProposalV1 = { + const transport = { kind: 'mcp' as const, serverId: 'board', origin: 'https://board.example.test', toolName: 'post' }; + const installed = installProposal(sealManifest({ version: 1, - provider: { id: 'fixture.mcp', kind: 'mcp' }, - externalIdentity: { operationId: 'post', sourceLocator: 'fixture://mcp/board' }, + id: canonicalCapabilityId(transport), title: 'Post', + source: { kind: 'mcp', locator: 'fixture://mcp/board', operationId: 'post' }, + effect: 'write', + effectSource: 'declared', + approval: 'pending', + invocation: 'manual', + trigger: { kind: 'manual' }, auth: { kind: 'none' }, - transport: { kind: 'mcp', serverId: 'board', origin: 'https://board.example.test', toolName: 'post' }, + transport, inputs: [ { name: 'channel', in: 'argument', required: true, schema: { kind: 'string' } }, { name: 'message', in: 'argument', required: true, schema: { kind: 'object' } } ], - output: { schema: { kind: 'null' }, presentation: 'raw' }, - execution: { kind: 'sync' }, - provenance: { providerId: 'fixture.mcp', sourceLocator: 'fixture://mcp/board', discoveredAtMs: 1_790_000_000_000 } - }; - const installed = admitProposal(proposal, { nowMs: 1_790_000_000_000 }); + output: { schema: { kind: 'null' }, presentation: 'raw' } + })); expect(installed.ok, installed.errors.join('; ')).toBe(true); const manifest = approveCapability(installed.manifest!.id).manifest!; const received: Array> = []; diff --git a/src/core/capabilities/jsonArguments.test.ts b/src/core/capabilities/jsonArguments.test.ts index 9741237..7bda45a 100644 --- a/src/core/capabilities/jsonArguments.test.ts +++ b/src/core/capabilities/jsonArguments.test.ts @@ -18,7 +18,6 @@ vi.mock('./hash', async (importOriginal) => { import { compileOpenApi } from './openapi'; import { canonicalCapabilityId } from './identity'; import { sealManifest, type CapabilityInput } from './manifest'; -import { admitProposal } from './admission'; import { approveCapability, clearCapabilities, installProposal } from './registry'; import { bindLocalHandler, @@ -31,7 +30,6 @@ import { import { bindAsyncRuntime, unbindAsyncRuntime, type AsyncClock } from './asyncRuntime'; import { clearExecutionLedger } from './executionLedger'; import { capabilitySecrets } from './secrets'; -import type { CapabilityProposalV1 } from './provider'; import type { CapabilityResult } from './project'; const NOW = 1_790_000_000_000; @@ -95,19 +93,22 @@ function httpFixture(): Fixture { } function mcpFixture(): Fixture { - const proposal: CapabilityProposalV1 = { + const transport = { kind: 'mcp' as const, serverId: 'board', origin: 'https://board.example.test', toolName: 'post' }; + const installed = installProposal(sealManifest({ version: 1, - provider: { id: 'fixture.mcp', kind: 'mcp' }, - externalIdentity: { operationId: 'post', sourceLocator: 'fixture://mcp/board' }, + id: canonicalCapabilityId(transport), title: 'Post', + source: { kind: 'mcp', locator: 'fixture://mcp/board', operationId: 'post' }, + effect: 'write', + effectSource: 'declared', + approval: 'pending', + invocation: 'manual', + trigger: { kind: 'manual' }, auth: { kind: 'none' }, - transport: { kind: 'mcp', serverId: 'board', origin: 'https://board.example.test', toolName: 'post' }, + transport, inputs: [PAYLOAD], - output: { schema: { kind: 'null' }, presentation: 'raw' }, - execution: { kind: 'sync' }, - provenance: { providerId: 'fixture.mcp', sourceLocator: 'fixture://mcp/board', discoveredAtMs: NOW } - }; - const installed = admitProposal(proposal, { nowMs: NOW }); + output: { schema: { kind: 'null' }, presentation: 'raw' } + })); expect(installed.ok, installed.errors.join('; ')).toBe(true); const manifest = approveCapability(installed.manifest!.id).manifest!; expect(manifest.effect).toBe('write'); @@ -123,19 +124,23 @@ function mcpFixture(): Fixture { } function asyncFixture(): Fixture { - const proposal: CapabilityProposalV1 = { + const transport = { kind: 'async' as const, runtimeId: 'jobs', operation: 'job-7' }; + const installed = installProposal(sealManifest({ version: 1, - provider: { id: 'fixture.jobs', kind: 'manual' }, - externalIdentity: { operationId: 'job-7', sourceLocator: 'fixture://jobs/job-7' }, + id: canonicalCapabilityId(transport), title: 'Submit job', + source: { kind: 'bring', locator: 'fixture://jobs/job-7', operationId: 'job-7' }, + effect: 'write', + effectSource: 'declared', + approval: 'pending', + invocation: 'manual', + trigger: { kind: 'manual' }, auth: { kind: 'none' }, - transport: { kind: 'async', runtimeId: 'jobs', operation: 'job-7' }, - inputs: [PAYLOAD], - output: { schema: { kind: 'object' }, presentation: 'raw' }, + transport, execution: { kind: 'async_poll', pollIntervalMs: 5_000, maxDurationMs: 120_000 }, - provenance: { providerId: 'fixture.jobs', sourceLocator: 'fixture://jobs/job-7', discoveredAtMs: NOW } - }; - const installed = admitProposal(proposal, { nowMs: NOW }); + inputs: [PAYLOAD], + output: { schema: { kind: 'object' }, presentation: 'raw' } + })); expect(installed.ok, installed.errors.join('; ')).toBe(true); const manifest = approveCapability(installed.manifest!.id).manifest!; expect(manifest.effect).toBe('write'); diff --git a/src/core/capabilities/mcp.ts b/src/core/capabilities/mcp.ts deleted file mode 100644 index 2794622..0000000 --- a/src/core/capabilities/mcp.ts +++ /dev/null @@ -1,132 +0,0 @@ -// MCP tool schemas compile to the same manifest as OpenAPI. -// Execution stays unbound until a host registers an McpTransport for the -// server id. This module does not open a network connection. - -import { canonicalCapabilityId } from './identity'; -import { fromJsonSchema } from './jsonSchema'; -import { - approvalForEffect, - isRecord, - sealManifest, - validateManifest, - type CapabilityEffect, - type CapabilityInput, - type CapabilityManifest -} from './manifest'; -import type { ValueType } from './valueType'; - -export interface McpToolAnnotations { - readOnlyHint?: boolean; - destructiveHint?: boolean; - idempotentHint?: boolean; -} - -export interface McpToolSchema { - serverId: string; - /** URL origin of the server that listed the tool. `mcpClient.ts` stamps it from the URL it connected to. */ - origin: string; - name: string; - description?: string; - inputSchema?: unknown; - outputSchema?: unknown; - annotations?: McpToolAnnotations; -} - -export interface McpCompileResult { - manifests: CapabilityManifest[]; - errors: Array<{ tool?: string; message: string }>; -} - -export interface CompileMcpOptions { - /** Tool annotations are hints. They classify effect only when the server is trusted. */ - trustedAnnotations?: boolean; -} - -export function compileMcpTools(tools: readonly McpToolSchema[], options: CompileMcpOptions = {}): McpCompileResult { - const manifests: CapabilityManifest[] = []; - const errors: McpCompileResult['errors'] = []; - - for (const tool of tools) { - const compiled = compileOne(tool, options.trustedAnnotations === true); - if ('error' in compiled) errors.push({ tool: tool?.name, message: compiled.error }); - else manifests.push(compiled.manifest); - } - return { manifests, errors }; -} - -function compileOne(tool: McpToolSchema, trustedAnnotations: boolean): { manifest: CapabilityManifest } | { error: string } { - if (!isRecord(tool) || typeof tool.serverId !== 'string' || typeof tool.origin !== 'string' || typeof tool.name !== 'string') { - return { error: 'MCP tool needs a serverId, origin and name' }; - } - if (!/^[A-Za-z0-9_.:-]{1,80}$/.test(tool.serverId)) return { error: 'serverId is invalid' }; - if (!/^[A-Za-z_][A-Za-z0-9_-]{0,64}$/.test(tool.name)) return { error: 'tool name is invalid' }; - - const effect = effectFromAnnotations(tool.annotations, trustedAnnotations); - const inputs = compileArguments(tool.inputSchema); - if ('error' in inputs) return inputs; - const output = compileOutput(tool.outputSchema); - if ('error' in output) return output; - - const transport = { kind: 'mcp' as const, serverId: tool.serverId, origin: tool.origin, toolName: tool.name }; - const sealed = sealManifest({ - version: 1, - id: canonicalCapabilityId(transport), - title: tool.name, - ...(tool.description ? { description: tool.description.slice(0, 2000) } : {}), - source: { kind: 'mcp', locator: tool.serverId, operationId: tool.name }, - effect: effect.effect, - effectSource: trustedAnnotations ? 'annotation' : 'declared', - approval: approvalForEffect(effect.effect), - invocation: 'manual', - auth: { kind: 'none' }, - transport, - inputs: inputs.inputs, - output: output - }); - const validated = validateManifest(sealed); - if (!validated.ok || !validated.manifest) return { error: validated.errors.join('; ') }; - return { manifest: validated.manifest }; -} - -function effectFromAnnotations( - annotations: McpToolAnnotations | undefined, - trustedAnnotations: boolean -): { effect: CapabilityEffect } { - if (annotations?.destructiveHint) return { effect: 'destructive' }; - if (trustedAnnotations && annotations?.readOnlyHint) return { effect: 'read' }; - return { effect: 'write' }; -} - -function compileArguments(schema: unknown): { inputs: CapabilityInput[] } | { error: string } { - if (schema === undefined) return { inputs: [] }; - const converted = fromJsonSchema(schema); - if (!converted.ok) return { error: converted.error }; - if (converted.schema.kind !== 'object') { - return { - inputs: [{ name: 'args', in: 'argument', required: true, schema: converted.schema }] - }; - } - const properties = converted.schema.properties ?? {}; - const required = new Set(converted.schema.required ?? []); - const inputs = Object.entries(properties).map(([name, child]) => ({ - name, - in: 'argument' as const, - required: required.has(name), - schema: child - })); - inputs.sort((a, b) => a.name.localeCompare(b.name)); - return { inputs }; -} - -function compileOutput(schema: unknown): CapabilityManifest['output'] | { error: string } { - if (schema === undefined) return { error: 'tool has no output schema' }; - const converted = fromJsonSchema(schema); - if (!converted.ok) return { error: converted.error }; - return presentationFor(converted.schema); -} - -function presentationFor(schema: ValueType): CapabilityManifest['output'] { - if (schema.kind === 'array') return { schema, presentation: 'items' }; - if (schema.kind === 'string') return { schema, presentation: 'content' }; - return { schema, presentation: 'raw' }; -} diff --git a/src/core/capabilities/mcpClient.test.ts b/src/core/capabilities/mcpClient.test.ts deleted file mode 100644 index 5349e0f..0000000 --- a/src/core/capabilities/mcpClient.test.ts +++ /dev/null @@ -1,452 +0,0 @@ -// @vitest-environment node -// -// Conformance fixtures. Both servers are the official SDK v2 server package -// (@modelcontextprotocol/server), bound to a loopback port in this process: -// -// - modern: createMcpHandler with legacy: 'reject', so only the 2026-07-28 -// era (server/discover) is served; -// - legacy: WebStandardStreamableHTTPServerTransport, the SDK's 2025-era -// entry, which only answers the initialize handshake. -// -// Gap: the 2025-era fixture is the SDK's own legacy server, not an -// independent 2025-03-26 implementation, and no remote production MCP server -// was contacted. These tests show the SDK path works against both eras in -// fixtures; they are not a measurement of any deployed server. - -import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'; -import { readFileSync } from 'node:fs'; -import http from 'node:http'; -import type { AddressInfo } from 'node:net'; -import path from 'node:path'; -import { Client } from '@modelcontextprotocol/client'; -import { createMcpHandler, McpServer, WebStandardStreamableHTTPServerTransport } from '@modelcontextprotocol/server'; -import * as z from 'zod/v4'; -import { createMcpHttpTransport, type McpSdkTransport } from './mcpClient'; -import { mcpProvider } from './providers/mcpProvider'; -import { admitProposal } from './admission'; -import { bindMcpTransport, unbindMcpTransport } from './execute'; -// Write runs need a confirmed preview; these tests exercise the engine after one. -import { executeConfirmed as executeCapability } from '../../../test/confirmedExecute'; -import { approveCapability, clearCapabilities } from './registry'; -import { capabilitySecrets } from './secrets'; -import { credentialSlot, transportCredentialSlot } from './identity'; -import type { CapabilityManifest } from './manifest'; - -interface Seen { - method?: string; - protocol?: string | null; - apiKey?: string | null; -} - -interface Fixture { - url: string; - seen: Seen[]; - close(): Promise; -} - -function registerTools(server: McpServer, era: string): McpServer { - server.registerTool('list', { - description: 'List board items', - inputSchema: z.object({ limit: z.number().int().optional() }), - outputSchema: z.object({ items: z.array(z.string()), era: z.string() }), - annotations: { readOnlyHint: true } - }, async ({ limit }) => { - const items = ['alpha', 'beta', 'gamma'].slice(0, limit ?? 3); - return { content: [{ type: 'text', text: items.join(',') }], structuredContent: { items, era } }; - }); - server.registerTool('slow', { - inputSchema: z.object({}), - outputSchema: z.object({ done: z.boolean() }) - }, async () => { - await new Promise(resolve => setTimeout(resolve, 3_000)); - return { content: [{ type: 'text', text: 'late' }], structuredContent: { done: true } }; - }); - return server; -} - -async function serve(handle: (request: Request) => Promise, requiredKey?: string): Promise { - const seen: Seen[] = []; - const sockets = new Set(); - const server = http.createServer(async (req, res) => { - const chunks: Buffer[] = []; - for await (const chunk of req) chunks.push(chunk as Buffer); - const body = chunks.length > 0 ? Buffer.concat(chunks) : undefined; - const headers = new Headers(); - for (const [key, value] of Object.entries(req.headers)) { - if (typeof value === 'string') headers.set(key, value); - } - let method: string | undefined; - try { - method = body ? (JSON.parse(body.toString()) as { method?: string }).method : undefined; - } catch { - method = undefined; - } - seen.push({ method, protocol: headers.get('mcp-protocol-version'), apiKey: headers.get('x-api-key') }); - if (requiredKey && headers.get('x-api-key') !== requiredKey) { - res.writeHead(401, { 'content-type': 'application/json' }); - res.end(JSON.stringify({ error: 'API key is invalid' })); - return; - } - const controller = new AbortController(); - res.on('close', () => controller.abort()); - const response = await handle(new Request(`http://127.0.0.1${req.url}`, { - method: req.method, - headers, - body: req.method === 'GET' || req.method === 'HEAD' ? undefined : body, - signal: controller.signal - })); - res.writeHead(response.status, Object.fromEntries(response.headers)); - if (response.body) { - const reader = response.body.getReader(); - try { - for (;;) { - const { done, value } = await reader.read(); - if (done) break; - res.write(value); - } - } catch { - // client went away - } - } - res.end(); - }); - server.on('connection', socket => { - sockets.add(socket); - socket.on('close', () => sockets.delete(socket)); - }); - await new Promise(resolve => server.listen(0, '127.0.0.1', resolve)); - const { port } = server.address() as AddressInfo; - return { - url: `http://127.0.0.1:${port}/mcp`, - seen, - close: () => new Promise(resolve => { - for (const socket of sockets) socket.destroy(); - server.close(() => resolve()); - }) - }; -} - -async function modernServer(requiredKey?: string): Promise { - const handler = createMcpHandler(({ era }) => registerTools(new McpServer({ name: 'board', version: '1.0.0' }), era), { - legacy: 'reject' - }); - return serve(request => handler.fetch(request), requiredKey); -} - -async function legacyServer(): Promise { - const transport = new WebStandardStreamableHTTPServerTransport({ sessionIdGenerator: () => 'legacy-session' }); - await registerTools(new McpServer({ name: 'board', version: '1.0.0' }), 'legacy-only').connect(transport); - return serve(request => transport.handleRequest(request)); -} - -const API_KEY_AUTH = { kind: 'apiKey' as const, in: 'header' as const, name: 'x-api-key' }; - -let fixtures: Fixture[] = []; -let transports: McpSdkTransport[] = []; - -async function track(fixture: Promise): Promise { - const ready = await fixture; - fixtures.push(ready); - return ready; -} - -/** This test file is the host that built the loopback fixture URL, so it may say so. */ -function client(config: Parameters[0]): McpSdkTransport { - const transport = createMcpHttpTransport({ hostCreated: true, ...config }); - transports.push(transport); - return transport; -} - -/** Answers every request with a 307 to `target`, which must never see the request. */ -async function redirectingServer(target: string): Promise { - const seen: Seen[] = []; - const server = http.createServer((req, res) => { - seen.push({ method: req.method, apiKey: req.headers['x-api-key'] as string | undefined ?? null }); - req.resume(); - req.on('end', () => { - res.writeHead(307, { location: target }); - res.end(); - }); - }); - await new Promise(resolve => server.listen(0, '127.0.0.1', resolve)); - const { port } = server.address() as AddressInfo; - return { - url: `http://127.0.0.1:${port}/mcp`, - seen, - close: () => new Promise(resolve => { server.closeAllConnections(); server.close(() => resolve()); }) - }; -} - -async function installFrom( - fixture: Fixture, - key: string, - tool: string, - trusted: boolean -): Promise<{ manifest: CapabilityManifest; transport: McpSdkTransport }> { - const transport = client({ url: fixture.url, serverId: 'board' }); - const request = { - serverId: 'board', - auth: API_KEY_AUTH, - lister: { listTools: (signal?: AbortSignal) => transport.listTools(signal, { headers: { 'x-api-key': key } }) } - }; - const discovered = await mcpProvider.discover(request); - const candidate = discovered.candidates.find(entry => entry.externalId === tool)!; - const proposal = await mcpProvider.materialize(candidate, { request }); - const admitted = admitProposal(proposal, trusted ? { trustedEffectHints: { mcpServers: ['board'] } } : {}); - expect(admitted.errors).toEqual([]); - bindMcpTransport('board', transport); - return { manifest: admitted.manifest!, transport }; -} - -beforeEach(() => { - clearCapabilities(); - capabilitySecrets.clear(); -}); - -afterEach(async () => { - unbindMcpTransport('board'); - await Promise.all(transports.map(transport => transport.close())); - await Promise.all(fixtures.map(fixture => fixture.close())); - fixtures = []; - transports = []; - clearCapabilities(); - capabilitySecrets.clear(); -}); - -describe('MCP client over the official SDK', () => { - it('negotiates the 2026-07-28 era with a modern server and sends configured headers', async () => { - const fixture = await track(modernServer()); - const transport = client({ url: fixture.url, headers: { 'x-api-key': 'static-key' } }); - const value = await transport.call('board', 'list', { limit: 2 }); - expect(value).toEqual({ items: ['alpha', 'beta'], era: 'modern' }); - expect(transport.protocolEra()).toBe('modern'); - expect(fixture.seen[0].method).toBe('server/discover'); - expect(fixture.seen.every(entry => entry.apiKey === 'static-key')).toBe(true); - expect(fixture.seen.map(entry => entry.method)).not.toContain('initialize'); - expect(fixture.seen.find(entry => entry.method === 'tools/call')?.protocol).toBe('2026-07-28'); - }); - - it('falls back to the 2025 initialize handshake against a legacy-only server', async () => { - const fixture = await track(legacyServer()); - const transport = client({ url: fixture.url }); - const value = await transport.call('board', 'list', {}); - expect(value).toEqual({ items: ['alpha', 'beta', 'gamma'], era: 'legacy-only' }); - expect(transport.protocolEra()).toBe('legacy'); - const methods = fixture.seen.map(entry => entry.method); - expect(methods.indexOf('server/discover')).toBeLessThan(methods.indexOf('initialize')); - const call = fixture.seen.find(entry => entry.method === 'tools/call'); - expect(call?.protocol).toMatch(/^2025-/); - }); - - it('a pinned 2026 client refuses a legacy-only server instead of downgrading', async () => { - const fixture = await track(legacyServer()); - const transport = client({ url: fixture.url, versionNegotiation: { pin: '2026-07-28' } }); - await expect(transport.call('board', 'list', {})).rejects.toThrow(/negotiation/i); - expect(fixture.seen.map(entry => entry.method)).not.toContain('tools/call'); - }); - - it('opens one session when two calls for the same header key start together', async () => { - const fixture = await track(modernServer()); - const transport = client({ url: fixture.url, headers: { 'x-api-key': 'first' } }); - await transport.call('board', 'list', { limit: 1 }); - - let releaseClose: () => void = () => {}; - const closeGate = new Promise(resolve => { releaseClose = resolve; }); - let markCloseStarted: () => void = () => {}; - const closeStarted = new Promise(resolve => { markCloseStarted = resolve; }); - let connects = 0; - const originalClose = Client.prototype.close; - const originalConnect = Client.prototype.connect; - const closeSpy = vi.spyOn(Client.prototype, 'close').mockImplementation(function (this: Client) { - markCloseStarted(); - return closeGate.then(() => originalClose.call(this)); - }); - const connectSpy = vi.spyOn(Client.prototype, 'connect').mockImplementation(function (this: Client, ...args) { - connects += 1; - return originalConnect.apply(this, args); - }); - - try { - const pending = Promise.all([ - transport.call('board', 'list', { limit: 1 }, undefined, { headers: { 'x-api-key': 'second' } }), - transport.call('board', 'list', { limit: 2 }, undefined, { headers: { 'x-api-key': 'second' } }) - ]); - await closeStarted; - releaseClose(); - const [first, second] = await pending; - expect(first).toEqual({ items: ['alpha'], era: 'modern' }); - expect(second).toEqual({ items: ['alpha', 'beta'], era: 'modern' }); - expect(connects).toBe(1); - } finally { - releaseClose(); - closeSpy.mockRestore(); - connectSpy.mockRestore(); - } - }); - - it('keeps a session open while its call is in flight when a call with another key arrives', async () => { - const fixture = await track(modernServer()); - const transport = client({ url: fixture.url }); - const closeSpy = vi.spyOn(Client.prototype, 'close'); - try { - const slow = transport.call('board', 'slow', {}, undefined, { headers: { 'x-api-key': 'key-a' } }); - const slowSettled = slow.then(() => 'resolved', () => 'rejected'); - // Wait until the slow call has reached the server before switching keys. - for (let i = 0; i < 200 && !fixture.seen.some(entry => entry.method === 'tools/call'); i += 1) { - await new Promise(resolve => setTimeout(resolve, 10)); - } - expect(fixture.seen.some(entry => entry.method === 'tools/call' && entry.apiKey === 'key-a')).toBe(true); - - const other = await transport.call('board', 'list', { limit: 1 }, undefined, { headers: { 'x-api-key': 'key-b' } }); - expect(other).toEqual({ items: ['alpha'], era: 'modern' }); - const tools = await transport.listTools(undefined, { headers: { 'x-api-key': 'key-b' } }); - expect(tools.map(tool => tool.name)).toContain('slow'); - const closedWhileInFlight = closeSpy.mock.calls.length; - - expect(await slowSettled).toBe('resolved'); - expect(await slow).toEqual({ done: true }); - expect(closedWhileInFlight).toBe(0); - // The replaced session closes once its last request settles. - await vi.waitFor(() => expect(closeSpy).toHaveBeenCalledTimes(1)); - } finally { - closeSpy.mockRestore(); - } - }, 15_000); - - it('lists tools with annotations for the provider', async () => { - const fixture = await track(modernServer()); - const tools = await client({ url: fixture.url, serverId: 'board' }).listTools(); - const list = tools.find(tool => tool.name === 'list')!; - expect(list).toMatchObject({ serverId: 'board', annotations: { readOnlyHint: true } }); - expect(list.outputSchema).toBeDefined(); - }); -}); - -describe('MCP capability end to end through the provider, admission, and executor', () => { - it('carries the slot credential per call and fails deterministically after revocation', async () => { - const fixture = await track(modernServer('k-live')); - const { manifest } = await installFrom(fixture, 'k-live', 'list', true); - expect(manifest).toMatchObject({ effect: 'read', approval: 'auto', auth: { kind: 'apiKey', in: 'header', name: 'x-api-key' } }); - const slot = transportCredentialSlot(manifest.transport, manifest.auth); - expect(manifest.auth.secretRef).toBe(slot); - expect(JSON.stringify(manifest)).not.toContain('k-live'); - - capabilitySecrets.set(slot!, 'k-live'); - const ran = await executeCapability(manifest.id, { limit: 1 }); - expect(ran.ok).toBe(true); - expect(ran.typed?.value).toEqual({ items: ['alpha'], era: 'modern' }); - expect(fixture.seen.filter(entry => entry.method === 'tools/call').every(entry => entry.apiKey === 'k-live')).toBe(true); - - const before = fixture.seen.length; - expect(capabilitySecrets.revoke(slot!)).toBe(true); - const revoked = await executeCapability(manifest.id, { limit: 1 }); - expect(revoked.error?.code).toBe('AUTH_UNBOUND'); - expect(fixture.seen.length).toBe(before); - }); - - it('keys the slot on the server origin, so a second server with the same label never receives the first secret', async () => { - const first = await track(modernServer('k-a')); - const second = await track(modernServer('k-b')); - const { manifest } = await installFrom(first, 'k-a', 'list', true); - const slot = transportCredentialSlot(manifest.transport, manifest.auth)!; - expect(slot).toBe(credentialSlot(new URL(first.url).origin, manifest.auth)); - expect(slot).not.toBe(credentialSlot(new URL(second.url).origin, manifest.auth)); - capabilitySecrets.set(slot, 'k-a'); - - // The host rebinds the label "board" to another origin. - bindMcpTransport('board', client({ url: second.url, serverId: 'board' })); - const result = await executeCapability(manifest.id, { limit: 1 }); - expect(result.ok).toBe(false); - expect(result.error?.code).toBe('TRANSPORT_NOT_BOUND'); - expect(second.seen).toHaveLength(0); - }); - - it('does not let an untrusted readOnlyHint grant read/auto', async () => { - const fixture = await track(modernServer('k-live')); - const { manifest } = await installFrom(fixture, 'k-live', 'list', false); - expect(manifest).toMatchObject({ effect: 'write', approval: 'pending' }); - capabilitySecrets.set(manifest.auth.secretRef!, 'k-live'); - const refused = await executeCapability(manifest.id, {}); - expect(refused.error?.code).toBe('EFFECT_NOT_APPROVED'); - expect(fixture.seen.map(entry => entry.method)).not.toContain('tools/call'); - }); - - it('refuses a wrong credential without calling the tool', async () => { - const fixture = await track(modernServer('k-live')); - const { manifest } = await installFrom(fixture, 'k-live', 'list', true); - capabilitySecrets.set(manifest.auth.secretRef!, 'k-wrong'); - const denied = await executeCapability(manifest.id, {}); - expect(denied.ok).toBe(false); - expect(denied.error?.code).toBe('UPSTREAM_ERROR'); - expect(denied.error?.message).toMatch(/401|unauthori[sz]ed|authenticat/i); - expect(fixture.seen.some(entry => entry.apiKey === 'k-wrong')).toBe(true); - expect(fixture.seen.filter(entry => entry.apiKey === 'k-wrong').map(entry => entry.method)).not.toContain('tools/call'); - }); - - it('forwards cancellation into the SDK call', async () => { - const fixture = await track(modernServer('k-live')); - const { manifest } = await installFrom(fixture, 'k-live', 'slow', true); - capabilitySecrets.set(manifest.auth.secretRef!, 'k-live'); - approveCapability(manifest.id); - const controller = new AbortController(); - const started = Date.now(); - setTimeout(() => controller.abort(), 100); - const result = await executeCapability(manifest.id, {}, { signal: controller.signal }); - expect(Date.now() - started).toBeLessThan(2_500); - expect(manifest.effect).toBe('write'); - expect(result.error?.code).toBe('EFFECT_UNCERTAIN'); - expect(fixture.seen.map(entry => entry.method)).toContain('tools/call'); - }); -}); - -describe('MCP server URL follows the http base URL rule before a credential is sent', () => { - it('refuses an http URL that is not loopback', () => { - expect(() => createMcpHttpTransport({ url: 'http://mcp.example.test/mcp', headers: { 'x-api-key': 'k-live' } })) - .toThrow(/must be https/); - }); - - it('refuses loopback http unless the host built the config, so the fixture never sees the key', async () => { - const fixture = await track(modernServer('k-live')); - expect(() => createMcpHttpTransport({ url: fixture.url, headers: { 'x-api-key': 'k-live' } })) - .toThrow(/loopback only in a host-created config/); - expect(fixture.seen).toHaveLength(0); - // The same URL, vouched for by the host, connects. - const allowed = client({ url: fixture.url, headers: { 'x-api-key': 'k-live' } }); - expect(await allowed.call('board', 'list', { limit: 1 })).toEqual({ items: ['alpha'], era: 'modern' }); - }); - - it.each([ - ['https://user:pw@mcp.example.test/mcp', 'embed credentials'], - ['https://mcp.example.test/mcp?token=abc', 'query or fragment'], - ['https://169.254.169.254/mcp', 'metadata service'], - ['https://10.0.0.1/mcp', 'non-public address'], - ['https://localhost/mcp', 'loopback only in a host-created config'] - ])('refuses %s like an http base URL', (url, reason) => { - expect(() => createMcpHttpTransport({ url, headers: { 'x-api-key': 'k-live' } })).toThrow(reason); - }); - - it('accepts a public https URL without opening a connection', () => { - const transport = createMcpHttpTransport({ url: 'https://mcp.example.test/mcp', serverId: 'board' }); - expect(transport.origin).toBe('https://mcp.example.test'); - expect(transport.protocolEra()).toBeUndefined(); - }); - - it('never replays a credentialed request to the host a 3xx names', async () => { - const target = await track(modernServer('k-live')); - const bouncer = await track(redirectingServer(target.url)); - const transport = client({ url: bouncer.url, headers: { 'x-api-key': 'k-live' } }); - await expect(transport.call('board', 'list', { limit: 1 })).rejects.toThrow(); - expect(bouncer.seen.length).toBeGreaterThan(0); - expect(bouncer.seen.every(entry => entry.apiKey === 'k-live')).toBe(true); - expect(target.seen).toHaveLength(0); - }); -}); - -describe('production MCP path', () => { - it('does not hardcode a protocol version or frame JSON-RPC itself', () => { - const source = readFileSync(path.join(process.cwd(), 'src/core/capabilities/mcpClient.ts'), 'utf8'); - expect(source).not.toMatch(/\b20\d\d-\d\d-\d\d\b/); - expect(source).not.toMatch(/jsonrpc|text\/event-stream|mcp-session-id/i); - expect(source).toContain("from '@modelcontextprotocol/client'"); - }); -}); diff --git a/src/core/capabilities/mcpClient.ts b/src/core/capabilities/mcpClient.ts deleted file mode 100644 index 14b8712..0000000 --- a/src/core/capabilities/mcpClient.ts +++ /dev/null @@ -1,240 +0,0 @@ -// MCP client over the official TypeScript SDK v2 (Streamable HTTP). -// The SDK owns version negotiation, framing, SSE, and cancellation. This -// adapter owns only what Omni needs from it: a bound McpTransport for -// executeCapability and a tool lister for the MCP provider. Tool import -// policy, effect classification, and credential slots stay in Omni. - -import { Client, StreamableHTTPClientTransport, type VersionNegotiationMode } from '@modelcontextprotocol/client'; -import { destinationUrlErrors } from './egress'; -import type { McpCallContext, McpTransport } from './execute'; -import { canonicalize, sha256 } from './hash'; -import type { McpToolSchema } from './mcp'; - -export interface McpServerConfig { - /** - * Checked against the http base URL rule (`destinationUrlErrors`) when - * the transport is created, before any session opens: https, no embedded - * credentials, query or fragment, no private or metadata address. - */ - url: string; - /** - * The host itself built this config (not a manifest, provider, model or - * stored snapshot). Only then may `url` target loopback, http or https: - * the same gate as `validateManifest(..., { hostCreated: true })`. - */ - hostCreated?: boolean; - /** Labels tools returned by listTools. The provider re-labels them with its own server id. */ - serverId?: string; - /** - * Headers sent on every request, visible to whoever builds this config. - * A secret belongs in a credential slot instead: execution resolves the - * slot and passes it per call, so revoking the slot stops the next call. - */ - headers?: Record; - clientName?: string; - clientVersion?: string; - /** Defaults to `auto`: probe the modern era, fall back to the 2025 handshake. */ - versionNegotiation?: VersionNegotiationMode; -} - -export type McpProtocolEra = 'legacy' | 'modern'; - -export interface McpSdkTransport extends McpTransport { - listTools(signal?: AbortSignal, context?: McpCallContext): Promise; - protocolEra(): McpProtocolEra | undefined; - close(): Promise; -} - -interface Session { - key: string; - client: Client; - ready: Promise; - /** Requests holding this session that have not settled. */ - active: number; - /** Replaced by a session for another key; it closes when `active` reaches zero. */ - retired: boolean; - closed: boolean; -} - -export function createMcpHttpTransport(config: McpServerConfig): McpSdkTransport { - // The same rule an http transport.baseUrl passes, applied once, before - // a credential can be attached to anything. - const refused = destinationUrlErrors(config.url, { hostCreated: config.hostCreated === true, subject: 'config' }); - if (refused.length > 0) throw new Error(`MCP server URL ${refused.join('; ')}`); - const url = new URL(config.url); - const origin = url.origin; - - let session: Session | undefined; - /** Replaced sessions that still have requests in flight. */ - const draining = new Set(); - // One header key has one in-flight session. Connect used to drop the - // current session and await close() before storing the next one, so two - // overlapping calls could each construct a client. The chain is the slot: - // a second call with the same key waits and reuses the session the first - // call publishes, instead of opening another. - let connectChain: Promise = Promise.resolve(); - - /** - * Take a lease on the session for these headers. The lease is counted - * inside the chain, before the next caller can switch keys, so a switch - * never closes a session whose request is still pending: SDK close() - * rejects every pending response, even when the remote side effect - * completes. The caller releases the lease when its request settles. - */ - function lease(context: McpCallContext | undefined, signal: AbortSignal | undefined): Promise { - const headers = { ...(config.headers ?? {}), ...(context?.headers ?? {}) }; - const key = sha256(canonicalize(headers)); - const opening = connectChain.then(() => openSession(key, headers, signal)); - connectChain = opening.then(() => undefined, () => undefined); - return opening; - } - - function release(held: Session): void { - held.active -= 1; - if (held.retired && held.active === 0) void closeSession(held); - } - - function closeSession(held: Session): Promise { - draining.delete(held); - if (held.closed) return Promise.resolve(); - held.closed = true; - return held.client.close().catch(() => undefined); - } - - async function openSession(key: string, headers: Record, signal: AbortSignal | undefined): Promise { - if (session && session.key === key) { - const current = session; - current.active += 1; - try { - await current.ready; - } catch (error) { - release(current); - throw error; - } - return current; - } - const previous = session; - session = undefined; - if (previous) { - previous.retired = true; - // An idle session closes now, as before. A busy one drains first. - if (previous.active === 0) await closeSession(previous); - else draining.add(previous); - } - - const client = new Client( - { name: config.clientName ?? 'omni', version: config.clientVersion ?? '0.1.0' }, - { versionNegotiation: { mode: config.versionNegotiation ?? 'auto' } } - ); - // The SDK (v2.2.0, src/client/streamableHttp.ts) spreads requestInit - // into every fetch init (GET stream, POST, DELETE) and overrides only - // method, headers, body and signal, so `redirect` reaches fetch. A 3xx - // then comes back as a non-ok response and fails the request; the - // credentialed request is never replayed to the host Location names. - const transport = new StreamableHTTPClientTransport(url, { requestInit: { headers, redirect: 'manual' } }); - const ready = client.connect(transport, signal ? { signal } : undefined).then(() => client); - const current: Session = { key, client, ready, active: 1, retired: false, closed: false }; - session = current; - ready.catch(() => { - if (session === current) session = undefined; - }); - try { - await ready; - } catch (error) { - release(current); - throw error; - } - return current; - } - - return { - origin, - - async call(_serverId, toolName, args, signal, context) { - const held = await lease(context, signal); - try { - const result = await held.client.callTool({ name: toolName, arguments: args }, signal ? { signal } : undefined); - return toolValue(result as Record); - } finally { - release(held); - } - }, - - async listTools(signal, context) { - const held = await lease(context, signal); - try { - return await listAllTools(held.client, signal); - } finally { - release(held); - } - }, - - protocolEra() { - return session?.client.getProtocolEra(); - }, - - close() { - const closing = connectChain.then(async () => { - const current = session; - session = undefined; - const open = [...draining, ...(current ? [current] : [])]; - await Promise.all(open.map(closeSession)); - }); - connectChain = closing.then(() => undefined, () => undefined); - return closing; - } - }; - - async function listAllTools(client: Client, signal: AbortSignal | undefined): Promise { - const tools: McpToolSchema[] = []; - let cursor: string | undefined; - do { - const page = await client.listTools(cursor ? { cursor } : undefined, signal ? { signal } : undefined); - for (const tool of page.tools) { - tools.push({ - serverId: config.serverId ?? '', - origin, - name: tool.name, - ...(tool.description ? { description: tool.description } : {}), - inputSchema: tool.inputSchema, - ...(tool.outputSchema ? { outputSchema: tool.outputSchema } : {}), - ...(tool.annotations ? { - annotations: { - ...(tool.annotations.readOnlyHint !== undefined ? { readOnlyHint: tool.annotations.readOnlyHint } : {}), - ...(tool.annotations.destructiveHint !== undefined ? { destructiveHint: tool.annotations.destructiveHint } : {}), - ...(tool.annotations.idempotentHint !== undefined ? { idempotentHint: tool.annotations.idempotentHint } : {}) - } - } : {}) - }); - } - cursor = page.nextCursor; - } while (cursor); - return tools; - } -} - -function toolValue(result: Record): unknown { - if (result.isError === true) { - throw new Error(textContent(result) || 'MCP tool returned an error'); - } - if ('structuredContent' in result && result.structuredContent !== undefined) { - return result.structuredContent; - } - const text = textContent(result); - if (text) { - try { - return JSON.parse(text) as unknown; - } catch { - return text; - } - } - return result; -} - -function textContent(result: Record): string { - if (!Array.isArray(result.content)) return ''; - return result.content - .map(entry => (typeof entry === 'object' && entry !== null ? (entry as { text?: unknown }).text : undefined)) - .filter((text): text is string => typeof text === 'string') - .join('\n'); -} diff --git a/src/core/capabilities/provider.test.ts b/src/core/capabilities/provider.test.ts deleted file mode 100644 index eda3fc8..0000000 --- a/src/core/capabilities/provider.test.ts +++ /dev/null @@ -1,320 +0,0 @@ -import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'; -import { compileOpenApi } from './openapi'; -import { compileMcpTools, type McpToolSchema } from './mcp'; -import { compileBring, type BringApiDescription } from './bring'; -import { admitProposal, manifestFromProposal } from './admission'; -import { credentialSlot, transportCredentialSlot } from './identity'; -import { clearCapabilities, ensureSpeechCapabilities, getCapability, installProposal, approveCapability } from './registry'; -// Write runs need a confirmed preview; these tests exercise the engine after one. -import { executeConfirmed as executeCapability } from '../../../test/confirmedExecute'; -import { capabilitySecrets } from './secrets'; -import { sealManifest, validateManifest, type CapabilityManifest } from './manifest'; -import type { CapabilityProposalV1, CapabilityProvider } from './provider'; -import { openapiProvider } from './providers/openapiProvider'; -import { mcpProvider } from './providers/mcpProvider'; -import { bringProvider } from './providers/bringProvider'; -import { useBlockStore } from '../stores/blockStore'; -import { useWireStore } from '../stores/wireStore'; - -const NOW = 1_790_000_000_000; - -const BOARD = { - openapi: '3.0.3', - info: { title: 'Board', version: '7' }, - servers: [{ url: 'https://board.example.test/v1' }], - components: { - securitySchemes: { - ApiKey: { type: 'apiKey', in: 'header', name: 'X-Api-Key' } - } - }, - security: [{ ApiKey: [] }], - paths: { - '/items': { - get: { - operationId: 'listItems', - summary: 'List items', - responses: { - '200': { - description: 'items', - content: { 'application/json': { schema: { type: 'array', items: { type: 'string' } } } } - } - } - }, - post: { - operationId: 'createItem', - requestBody: { - required: true, - content: { 'application/json': { schema: { type: 'object', properties: { name: { type: 'string' } } } } } - }, - responses: { '204': { description: 'created' } } - } - }, - '/items/{id}': { - get: { - operationId: 'readItem', - 'x-omni-effect': 'destructive', - parameters: [{ name: 'id', in: 'path', required: true, schema: { type: 'string' } }], - responses: { '204': { description: 'gone' } } - } - } - } -}; - -const TOOLS: McpToolSchema[] = [ - { - serverId: 'board', - origin: 'https://board.example.test', - name: 'list', - annotations: { readOnlyHint: true }, - inputSchema: { type: 'object', properties: { limit: { type: 'integer' } } }, - outputSchema: { type: 'array', items: { type: 'string' } } - }, - { - serverId: 'board', - origin: 'https://board.example.test', - name: 'wipe', - annotations: { destructiveHint: true }, - inputSchema: { type: 'object', properties: {} }, - outputSchema: { type: 'null' } - } -]; - -const BRING: BringApiDescription = { - title: 'Echo', - baseUrl: 'https://echo.example.test', - method: 'GET', - path: '/echo', - effect: 'compute', - auth: { kind: 'bearer', secretRef: 'cred_attacker_supplied' }, - inputs: [{ name: 'q', in: 'query', required: true, schema: { kind: 'string' } }], - output: { schema: { kind: 'string' } } -}; - -async function proposalsOf(provider: CapabilityProvider, request: T): Promise { - const discovered = await provider.discover(request, { nowMs: NOW }); - return Promise.all(discovered.candidates.map(candidate => provider.materialize(candidate, { request }))); -} - -function executionShape(manifest: CapabilityManifest) { - return { - id: manifest.id, - effect: manifest.effect, - effectSource: manifest.effectSource, - approval: manifest.approval, - auth: manifest.auth, - transport: manifest.transport, - inputs: manifest.inputs, - output: manifest.output, - trigger: manifest.trigger - }; -} - -beforeEach(() => { - clearCapabilities(); - ensureSpeechCapabilities(); - capabilitySecrets.clear(); - useBlockStore.setState({ blocks: [], activeShellId: 'root' }); - useWireStore.setState({ wires: [] }); -}); - -afterEach(() => { - clearCapabilities(); - capabilitySecrets.clear(); - vi.unstubAllGlobals(); -}); - -describe('provider seam keeps the compilers as the only parsers', () => { - it('OpenAPI provider proposals admit to the same execution contract as compileOpenApi', async () => { - const compiled = compileOpenApi(BOARD); - const proposals = await proposalsOf(openapiProvider, { document: BOARD }); - expect(proposals).toHaveLength(compiled.manifests.length); - for (const proposal of proposals) { - const admitted = manifestFromProposal(proposal, { nowMs: NOW }); - expect(admitted.errors).toEqual([]); - const direct = compiled.manifests.find(manifest => manifest.id === admitted.manifest!.id)!; - expect(executionShape(admitted.manifest!)).toEqual(executionShape(direct)); - expect(admitted.manifest!.source).toEqual(direct.source); - } - }); - - it('MCP provider proposals admit to the untrusted compileMcpTools contract', async () => { - const compiled = compileMcpTools(TOOLS); - const proposals = await proposalsOf(mcpProvider, { serverId: 'board', tools: TOOLS }); - for (const proposal of proposals) { - const admitted = manifestFromProposal(proposal, { nowMs: NOW }).manifest!; - const direct = compiled.manifests.find(manifest => manifest.id === admitted.id)!; - expect(executionShape(admitted)).toEqual(executionShape(direct)); - } - const list = proposals.find(proposal => proposal.externalIdentity.operationId === 'list')!; - expect(list.effectHint).toBe('read'); - expect(manifestFromProposal(list).manifest).toMatchObject({ effect: 'write', approval: 'pending' }); - }); - - it('Bring provider drops a supplied secretRef and admission derives the slot', async () => { - const [proposal] = await proposalsOf(bringProvider, { description: BRING }); - expect(JSON.stringify(proposal)).not.toContain('cred_attacker_supplied'); - const admitted = manifestFromProposal(proposal, { nowMs: NOW }).manifest!; - const direct = compileBring(BRING).manifests[0]; - expect(executionShape(admitted)).toEqual(executionShape(direct)); - expect(admitted.auth.secretRef).toBe(credentialSlot('https://echo.example.test', { kind: 'bearer' })); - }); - - it('records provenance under the digest', async () => { - const [list] = await proposalsOf(openapiProvider, { document: BOARD }); - const admitted = admitProposal(list, { nowMs: NOW + 5 }); - expect(admitted.ok).toBe(true); - expect(admitted.manifest!.provenance).toMatchObject({ - providerId: 'openapi', - providerKind: 'openapi', - externalId: 'listItems', - sourceLocator: 'Board@7', - sourceRevision: '7', - discoveredAtMs: NOW, - admittedAtMs: NOW + 5 - }); - const tampered = { ...admitted.manifest!, provenance: { ...admitted.manifest!.provenance!, providerId: 'maxun' } }; - expect(validateManifest(tampered).errors).toContain('digest does not match the manifest body'); - const schemaSwap = { - ...admitted.manifest!, - provenance: { ...admitted.manifest!.provenance!, schemaDigest: 'a'.repeat(64) } - }; - expect(validateManifest(schemaSwap).errors).toContain('provenance.schemaDigest does not match inputs and output'); - }); - - it('reports what the source could not represent', async () => { - const discovered = await openapiProvider.discover({ - document: { ...BOARD, paths: { '/x': { get: { operationId: 'x', parameters: [{ name: 'c', in: 'cookie' }], responses: { '204': {} } } } } } - }); - expect(discovered.candidates).toHaveLength(0); - expect(discovered.issues).toEqual([{ externalId: 'x', message: 'cookie parameters are unsupported' }]); - }); -}); - -describe('host admission gate', () => { - async function listProposal(): Promise { - const proposals = await proposalsOf(openapiProvider, { document: BOARD }); - return proposals.find(proposal => proposal.externalIdentity.operationId === 'createItem')!; - } - - it('refuses a forged approval, id, digest, or trust flag', async () => { - const create = await listProposal(); - for (const [key, value] of [['approval', 'approved'], ['id', 'cap_speech_speak'], ['digest', 'f'.repeat(64)], ['trusted', true], ['effect', 'read']] as const) { - const forged = { ...create, [key]: value }; - const result = admitProposal(forged); - expect(result.ok).toBe(false); - expect(result.errors.some(error => error.startsWith(`proposal.${key} is not a proposal field`))).toBe(true); - } - expect(getCapability('cap_speech_speak')?.transport.kind).toBe('local'); - }); - - it('lands a write as pending and execution refuses it until approveCapability', async () => { - const create = await listProposal(); - const result = admitProposal(create); - expect(result.manifest).toMatchObject({ effect: 'write', approval: 'pending' }); - capabilitySecrets.set(result.manifest!.auth.secretRef!, 'key'); - vi.stubGlobal('fetch', vi.fn(async () => new Response(null, { status: 204 }))); - const refused = await executeCapability(result.manifest!.id, { body: { name: 'x' } }); - expect(refused.error?.code).toBe('EFFECT_NOT_APPROVED'); - expect(vi.mocked(fetch)).not.toHaveBeenCalled(); - approveCapability(result.manifest!.id); - const ran = await executeCapability(result.manifest!.id, { body: { name: 'x' } }); - expect(ran.ok).toBe(true); - }); - - it('refuses an effect hint below the method floor', async () => { - const create = await listProposal(); - expect(admitProposal({ ...create, effectHint: 'compute' }).errors).toEqual(['POST cannot be declared compute']); - const del = { ...create, transport: { ...create.transport, method: 'DELETE' as const }, inputs: [], effectHint: 'write' as const }; - expect(admitProposal(del).errors).toEqual(['DELETE cannot be declared write']); - }); - - it('lets a hint raise the floor on a safe method', async () => { - const proposals = await proposalsOf(openapiProvider, { document: BOARD }); - const read = proposals.find(proposal => proposal.externalIdentity.operationId === 'readItem')!; - expect(read.effectHint).toBe('destructive'); - expect(admitProposal(read).manifest).toMatchObject({ effect: 'destructive', approval: 'pending', effectSource: 'extension' }); - }); - - it('accepts an MCP read hint only from a host-trusted server', async () => { - const [list] = await proposalsOf(mcpProvider, { serverId: 'board', tools: TOOLS }); - expect(admitProposal(list).manifest).toMatchObject({ effect: 'write', approval: 'pending', effectSource: 'declared' }); - expect(admitProposal(list, { trustedEffectHints: { mcpServers: ['other'] } }).manifest?.effect).toBe('write'); - expect(admitProposal(list, { trustedEffectHints: { mcpServers: ['board'] } }).manifest) - .toMatchObject({ effect: 'read', approval: 'auto', effectSource: 'annotation' }); - }); - - it('refuses a credential slot named by the provider', async () => { - const create = await listProposal(); - const foreign = credentialSlot('https://other.example.test', { kind: 'apiKey', in: 'header', name: 'X-Api-Key' }); - const result = admitProposal({ ...create, auth: { ...create.auth, secretRef: foreign } }); - expect(result.ok).toBe(false); - expect(result.errors[0]).toMatch(/^proposal\.auth\.secretRef is not a proposal field/); - }); - - it('keys an MCP credential to the server origin, never to its label', async () => { - const auth = { kind: 'apiKey' as const, in: 'header' as const, name: 'x-api-key' }; - const [list] = await proposalsOf(mcpProvider, { serverId: 'board', tools: TOOLS, auth }); - const admitted = admitProposal(list).manifest!; - expect(admitted.auth.secretRef).toBe(transportCredentialSlot(admitted.transport, admitted.auth)); - // The same rule as an http base: origin + scheme + placement. - expect(admitted.auth.secretRef).toBe(credentialSlot('https://board.example.test', auth)); - // The same label at another origin is another slot. - const elsewhere = TOOLS.map(tool => ({ ...tool, origin: 'https://other.example.test' })); - const [moved] = await proposalsOf(mcpProvider, { serverId: 'board', tools: elsewhere, auth }); - const otherSlot = admitProposal(moved).manifest!.auth.secretRef!; - expect(otherSlot).toBe(credentialSlot('https://other.example.test', auth)); - expect(otherSlot).not.toBe(admitted.auth.secretRef); - const { digest: _digest, ...draft } = admitted; - const borrowed = sealManifest({ ...draft, auth: { ...admitted.auth, secretRef: otherSlot } }); - expect(validateManifest(borrowed).errors.some(error => error.startsWith('auth.secretRef must be'))).toBe(true); - const query = await proposalsOf(mcpProvider, { serverId: 'board', tools: TOOLS, auth: { ...auth, in: 'query' } }); - expect(admitProposal(query[0]).errors).toContain('mcp credentials travel in a header'); - }); - - it('refuses a local transport, a mismatched origin, or a provenance that names another provider', async () => { - const create = await listProposal(); - expect(admitProposal({ ...create, transport: { kind: 'local', handler: 'speech.speak' } }).ok).toBe(false); - expect(admitProposal({ ...create, externalIdentity: { ...create.externalIdentity, origin: 'https://evil.example.test' } }).errors) - .toContain('proposal.externalIdentity.origin does not match the transport'); - expect(admitProposal({ ...create, provenance: { ...create.provenance, providerId: 'mcp' } }).errors) - .toContain('proposal.provenance.providerId must match proposal.provider.id'); - }); - - it('returns errors instead of throwing for a malformed HTTP base URL, before deriving an id or a slot', async () => { - const create = await listProposal(); - const { origin: _origin, ...identity } = create.externalIdentity; - expect(create.transport.kind).toBe('http'); - const http = create.transport as Extract; - const { baseUrl: _baseUrl, ...withoutBase } = http; - const malformed: Array<[string, unknown]> = [ - ['not-a-url', { ...http, baseUrl: 'not-a-url' }], - ['omitted', withoutBase], - ['non-string', { ...http, baseUrl: 42 }] - ]; - for (const auth of [{ kind: 'none' }, { kind: 'bearer' }, create.auth]) { - for (const [label, transport] of malformed) { - const proposal = { ...create, externalIdentity: identity, auth, transport }; - let built: ReturnType | undefined; - expect(() => { built = manifestFromProposal(proposal, { nowMs: NOW }); }, `${auth.kind} ${label}`).not.toThrow(); - expect(built!.ok).toBe(false); - expect(built!.errors).toContain('proposal.transport.baseUrl must be an absolute URL'); - let admitted: ReturnType | undefined; - expect(() => { admitted = admitProposal(proposal, { nowMs: NOW }); }).not.toThrow(); - expect(admitted!.ok).toBe(false); - } - } - }); - - it('does not place a block or a wire', async () => { - const proposals = await proposalsOf(openapiProvider, { document: BOARD }); - for (const proposal of proposals) admitProposal(proposal); - expect(useBlockStore.getState().blocks).toHaveLength(0); - expect(useWireStore.getState().wires).toHaveLength(0); - }); - - it('keeps the existing install path for compiler output', () => { - const [list] = compileOpenApi(BOARD).manifests; - expect(installProposal(list).ok).toBe(true); - expect(getCapability(list.id)?.provenance).toBeUndefined(); - }); -}); diff --git a/src/core/capabilities/provider.ts b/src/core/capabilities/provider.ts deleted file mode 100644 index 22ae465..0000000 --- a/src/core/capabilities/provider.ts +++ /dev/null @@ -1,126 +0,0 @@ -// CapabilityProvider contract. A provider knows one external ecosystem and -// can only discover candidates and materialize untrusted proposals. It never -// names an id, an effect, an approval, or a credential slot. Admission -// (`admission.ts`) derives those, and installProposal is still the only way a -// proposal becomes an installed manifest. - -import type { - CapabilityEffect, - CapabilityInput, - CapabilityOutput, - CapabilityProviderKind, - HttpAccess, - HttpMethod -} from './manifest'; - -export type { CapabilityProviderKind } from './manifest'; - -export type LifecycleHint = 'sync' | 'async' | 'event'; - -export interface CapabilityCandidateV1 { - version: 1; - providerId: string; - externalId: string; - title: string; - description?: string; - sourceKind: string; - origin?: string; - effectHint?: CapabilityEffect; - authHint?: string; - lifecycleHint?: LifecycleHint; - provenance: { - providerId: string; - sourceLocator: string; - discoveredAtMs: number; - }; -} - -/** Placement only. The slot name is derived by admission from the transport. */ -export type ProposedAuthRequirement = - | { kind: 'none' } - | { kind: 'apiKey'; in: 'header' | 'query'; name: string; prefix?: string } - | { kind: 'bearer'; prefix?: string } - | { kind: 'basic' } - /** Scopes only. A token enters the slot through a host connect flow, never a proposal. */ - | { kind: 'oauth'; scopes: string[] }; - -export type ProposedTransport = - | { kind: 'http'; access: HttpAccess; baseUrl: string; method: HttpMethod; path: string } - /** `origin` is the server's URL origin; admission keys the credential slot on it, as for an http base. */ - | { kind: 'mcp'; serverId: string; origin: string; toolName: string } - /** Names a runtime the host binds. The provider cannot supply its endpoint. */ - | { kind: 'async'; runtimeId: string; operation: string }; - -export type ProposedInput = CapabilityInput; -export type ProposedOutput = CapabilityOutput; - -export type ProposedExecutionProfile = - | { kind: 'sync' } - | { kind: 'async_poll'; pollIntervalMs: number; maxDurationMs: number }; - -export interface CapabilityProposalProvenance { - providerId: string; - sourceLocator: string; - discoveredAtMs: number; -} - -export interface CapabilityProposalV1 { - version: 1; - provider: { - id: string; - kind: CapabilityProviderKind; - }; - externalIdentity: { - origin?: string; - operationId: string; - sourceLocator: string; - sourceRevision?: string; - }; - title: string; - description?: string; - /** A hint. Admission may raise it to the transport floor and refuses a hint below it. */ - effectHint?: CapabilityEffect; - auth: ProposedAuthRequirement; - transport: ProposedTransport; - inputs: ProposedInput[]; - output: ProposedOutput; - execution: ProposedExecutionProfile; - provenance: CapabilityProposalProvenance; -} - -export interface CapabilityDiscoveryContext { - nowMs?: number; -} - -export interface CapabilityMaterializationContext { - /** The same request discover saw. Providers re-derive; they keep no hidden state. */ - request: TRequest; -} - -export interface ProviderIssue { - externalId?: string; - message: string; -} - -export interface CapabilityDiscovery { - candidates: CapabilityCandidateV1[]; - /** What the source offered that the provider could not represent. */ - issues: ProviderIssue[]; -} - -export interface CapabilityProvider { - readonly id: string; - readonly kind: CapabilityProviderKind; - discover(request: TRequest, context?: CapabilityDiscoveryContext): Promise; - materialize( - candidate: CapabilityCandidateV1, - context: CapabilityMaterializationContext - ): Promise; -} - -export class ProviderMaterializeError extends Error { - constructor(message: string) { - super(message); - this.name = 'ProviderMaterializeError'; - } -} diff --git a/src/core/capabilities/providers/bringProvider.ts b/src/core/capabilities/providers/bringProvider.ts deleted file mode 100644 index 908d1c3..0000000 --- a/src/core/capabilities/providers/bringProvider.ts +++ /dev/null @@ -1,55 +0,0 @@ -// Manual structured acquisition. A description written by a person or a -// model is the same untrusted proposal as any other source. - -import { compileBring, type BringApiDescription } from '../bring'; -import type { CapabilityManifest } from '../manifest'; -import { ProviderMaterializeError, type CapabilityProvider } from '../provider'; -import { candidateFromManifest, proposalFromManifest, type ProposalSource } from './fromManifest'; - -export interface BringDiscoveryRequest { - description: BringApiDescription; - discoveredAtMs?: number; -} - -export const BRING_PROVIDER_ID = 'bring'; - -function externalIdFor(manifest: CapabilityManifest, description: BringApiDescription): string { - if (typeof description.id === 'string' && description.id.trim()) return description.id.trim(); - return manifest.transport.kind === 'http' ? `${manifest.transport.method} ${manifest.transport.path}` : manifest.id; -} - -function sourceFor(manifest: CapabilityManifest, request: BringDiscoveryRequest, now: number): ProposalSource { - return { - providerId: BRING_PROVIDER_ID, - providerKind: 'manual', - externalId: externalIdFor(manifest, request.description), - sourceLocator: manifest.source.locator, - discoveredAtMs: request.discoveredAtMs ?? now, - ...(manifest.effectSource === 'declared' ? { effectHint: manifest.effect } : {}) - }; -} - -export const bringProvider: CapabilityProvider = { - id: BRING_PROVIDER_ID, - kind: 'manual', - - async discover(request, context) { - const now = context?.nowMs ?? Date.now(); - const compiled = compileBring(request.description); - return { - candidates: compiled.manifests.map(manifest => candidateFromManifest(manifest, sourceFor(manifest, request, now))), - issues: compiled.errors.map(issue => ({ message: issue.message })) - }; - }, - - async materialize(candidate, context) { - const compiled = compileBring(context.request.description); - const manifest = compiled.manifests[0]; - if (!manifest) throw new ProviderMaterializeError(compiled.errors[0]?.message ?? 'description did not compile'); - const source = sourceFor(manifest, context.request, candidate.provenance.discoveredAtMs); - if (source.externalId !== candidate.externalId) { - throw new ProviderMaterializeError('candidate does not match the description'); - } - return proposalFromManifest(manifest, source); - } -}; diff --git a/src/core/capabilities/providers/fromManifest.ts b/src/core/capabilities/providers/fromManifest.ts deleted file mode 100644 index 0262f0b..0000000 --- a/src/core/capabilities/providers/fromManifest.ts +++ /dev/null @@ -1,109 +0,0 @@ -// Compilers seal a manifest to prove the source was representable. Providers -// hand admission only what a source may claim: the id, approval, digest, -// credential slot, and classified effect are dropped here. - -import type { CapabilityEffect, CapabilityManifest, CapabilityProviderKind } from '../manifest'; -import type { - CapabilityCandidateV1, - CapabilityProposalV1, - LifecycleHint, - ProposedAuthRequirement, - ProposedTransport -} from '../provider'; -import { ProviderMaterializeError } from '../provider'; - -export interface ProposalSource { - providerId: string; - providerKind: CapabilityProviderKind; - externalId: string; - sourceLocator: string; - sourceRevision?: string; - discoveredAtMs: number; - effectHint?: CapabilityEffect; -} - -export function proposalFromManifest(manifest: CapabilityManifest, source: ProposalSource): CapabilityProposalV1 { - const transport = proposedTransport(manifest); - const origin = originOf(manifest); - return { - version: 1, - provider: { id: source.providerId, kind: source.providerKind }, - externalIdentity: { - ...(origin ? { origin } : {}), - operationId: source.externalId.slice(0, 120), - sourceLocator: source.sourceLocator.slice(0, 200), - ...(source.sourceRevision ? { sourceRevision: source.sourceRevision.slice(0, 120) } : {}) - }, - title: manifest.title, - ...(manifest.description ? { description: manifest.description } : {}), - ...(source.effectHint ? { effectHint: source.effectHint } : {}), - auth: proposedAuth(manifest), - transport, - inputs: manifest.inputs.map(entry => ({ ...entry })), - output: { ...manifest.output }, - execution: manifest.execution?.kind === 'async_poll' ? { ...manifest.execution } : { kind: 'sync' }, - provenance: { - providerId: source.providerId, - sourceLocator: source.sourceLocator.slice(0, 200), - discoveredAtMs: source.discoveredAtMs - } - }; -} - -export function candidateFromManifest( - manifest: CapabilityManifest, - source: ProposalSource, - lifecycleHint: LifecycleHint = 'sync' -): CapabilityCandidateV1 { - const origin = originOf(manifest); - return { - version: 1, - providerId: source.providerId, - externalId: source.externalId, - title: manifest.title, - ...(manifest.description ? { description: manifest.description } : {}), - sourceKind: manifest.source.kind, - ...(origin ? { origin } : {}), - ...(source.effectHint ? { effectHint: source.effectHint } : {}), - authHint: manifest.auth.kind, - lifecycleHint, - provenance: { - providerId: source.providerId, - sourceLocator: source.sourceLocator, - discoveredAtMs: source.discoveredAtMs - } - }; -} - -/** The URL origin a credential would be sent to, when the transport has one. */ -function originOf(manifest: CapabilityManifest): string | undefined { - const transport = manifest.transport; - if (transport.kind === 'http') return new URL(transport.baseUrl).origin; - if (transport.kind === 'mcp') return transport.origin; - return undefined; -} - -function proposedTransport(manifest: CapabilityManifest): ProposedTransport { - const transport = manifest.transport; - if (transport.kind === 'http') { - return { kind: 'http', access: transport.access, baseUrl: transport.baseUrl, method: transport.method, path: transport.path }; - } - if (transport.kind === 'mcp') { - return { kind: 'mcp', serverId: transport.serverId, origin: transport.origin, toolName: transport.toolName }; - } - if (transport.kind === 'async') { - return { kind: 'async', runtimeId: transport.runtimeId, operation: transport.operation }; - } - throw new ProviderMaterializeError('local transports are host builtins, not provider proposals'); -} - -function proposedAuth(manifest: CapabilityManifest): ProposedAuthRequirement { - const auth = manifest.auth; - if (auth.kind === 'apiKey' && auth.in && auth.name) { - return { kind: 'apiKey', in: auth.in, name: auth.name, ...(auth.prefix ? { prefix: auth.prefix } : {}) }; - } - if (auth.kind === 'bearer') return { kind: 'bearer', ...(auth.prefix ? { prefix: auth.prefix } : {}) }; - if (auth.kind === 'basic') return { kind: 'basic' }; - if (auth.kind === 'oauth') return { kind: 'oauth', scopes: [...(auth.scopes ?? [])] }; - return { kind: 'none' }; -} diff --git a/src/core/capabilities/providers/managed.decision.ts b/src/core/capabilities/providers/managed.decision.ts deleted file mode 100644 index f30194b..0000000 --- a/src/core/capabilities/providers/managed.decision.ts +++ /dev/null @@ -1,45 +0,0 @@ -// The explicit decision on managed SaaS vendors as adopted dependencies. -// Omni keeps the managed_integration seam (managedProvider.ts), which can -// describe their catalog entries as candidates. It does not take either SDK. -// Revisit only with a measured OAuth connect and a runtime that keeps -// admission, effect, approval, slots, and receipts in Omni. - -export type ManagedVendorDecisionReason = - | 'no_measured_oauth_connect' - | 'vendor_becomes_integration_runtime'; - -export interface ManagedVendorDecision { - vendor: 'pipedream' | 'composio'; - product: string; - decision: 'adopt' | 'reject'; - reasons: ManagedVendorDecisionReason[]; - detail: string; - /** What was actually run. Nothing here was measured against the vendor. */ - measured: false; - sdkAdded: false; -} - -const DETAIL = 'No OAuth connect was performed in this environment (no vendor credentials were present), ' - + 'so there is no measured connect, token refresh, or revocation to rely on. Adopting the SDK would route ' - + 'execution and credentials through the vendor, making it the integration runtime instead of Omni.'; - -export const managedVendorDecisions: readonly ManagedVendorDecision[] = [ - { - vendor: 'pipedream', - product: 'Pipedream Connect', - decision: 'reject', - reasons: ['no_measured_oauth_connect', 'vendor_becomes_integration_runtime'], - detail: DETAIL, - measured: false, - sdkAdded: false - }, - { - vendor: 'composio', - product: 'Composio', - decision: 'reject', - reasons: ['no_measured_oauth_connect', 'vendor_becomes_integration_runtime'], - detail: DETAIL, - measured: false, - sdkAdded: false - } -]; diff --git a/src/core/capabilities/providers/managedProvider.test.ts b/src/core/capabilities/providers/managedProvider.test.ts deleted file mode 100644 index 4480391..0000000 --- a/src/core/capabilities/providers/managedProvider.test.ts +++ /dev/null @@ -1,212 +0,0 @@ -import { describe, it, expect, beforeEach } from 'vitest'; -import { readFileSync } from 'node:fs'; -import path from 'node:path'; -import { admitProposal, manifestFromProposal } from '../admission'; -import { approveCapability, clearCapabilities, getCapability, installProposal } from '../registry'; -// Write runs need a confirmed preview; these tests exercise the engine after one. -import { executeConfirmed as executeCapability } from '../../../../test/confirmedExecute'; -import { capabilitySecrets } from '../secrets'; -import { clearExecutionLedger } from '../executionLedger'; -import { credentialSlot } from '../identity'; -import { sealManifest, validateManifest, type CapabilityManifest } from '../manifest'; -import type { CapabilityCandidateV1, CapabilityProposalV1, CapabilityProvider } from '../provider'; -import { - MANAGED_FIXTURE_READ, - MANAGED_FIXTURE_WRITE, - managedProvider, - type ManagedActionDescription, - type ManagedProviderRequest -} from './managedProvider'; -import { managedVendorDecisions } from './managed.decision'; - -const NOW = 1_790_000_000_000; -const TOKEN = 'ya29.fixture-token-not-real'; - -const REQUEST: ManagedProviderRequest = { - actions: [MANAGED_FIXTURE_READ, MANAGED_FIXTURE_WRITE], - discoveredAtMs: NOW -}; - -async function proposalFor(action: ManagedActionDescription): Promise { - const discovery = await managedProvider.discover(REQUEST); - const candidate = discovery.candidates.find(c => c.externalId.endsWith(action.actionKey))!; - return managedProvider.materialize(candidate, { request: REQUEST }); -} - -async function admitted(action: ManagedActionDescription): Promise { - const installed = admitProposal(await proposalFor(action), { nowMs: NOW }); - expect(installed.ok, installed.errors?.join('; ')).toBe(true); - return installed.manifest!; -} - -function recordingFetch(body: unknown) { - const calls: Array<{ url: string; method: string; headers: Record; body?: string }> = []; - const fetchImpl: typeof fetch = async (input, init) => { - calls.push({ - url: String(input), - method: init?.method ?? 'GET', - headers: { ...(init?.headers as Record) }, - body: typeof init?.body === 'string' ? init.body : undefined - }); - return new Response(JSON.stringify(body), { status: 200, headers: { 'content-type': 'application/json' } }); - }; - return { fetchImpl, calls }; -} - -beforeEach(() => { - clearCapabilities(); - clearExecutionLedger(); - capabilitySecrets.clear(); -}); - -describe('managed_integration provider', () => { - it('describes catalog entries as candidates with oauth scopes and reports entries it will not carry', async () => { - const discovery = await managedProvider.discover({ - actions: [ - MANAGED_FIXTURE_READ, - MANAGED_FIXTURE_WRITE, - { ...MANAGED_FIXTURE_READ, actionKey: 'plain-http', baseUrl: 'http://sheets.example.test' }, - { ...MANAGED_FIXTURE_READ, actionKey: 'no-scopes', oauth: { scopes: [] } }, - { ...MANAGED_FIXTURE_READ, actionKey: '../escape' } - ], - discoveredAtMs: NOW - }); - expect(discovery.candidates.map(c => c.externalId)).toEqual([ - 'pipedream:google_sheets:get-values-in-range', - 'composio:slack:send-message' - ]); - expect(discovery.candidates.every(c => c.sourceKind === 'managed_integration' && c.authHint === 'oauth')).toBe(true); - expect(discovery.issues.map(i => i.message)).toEqual([ - 'managed actions call the SaaS API over https', - 'managed actions must name their oauth scopes', - 'app and action keys must be plain identifiers' - ]); - }); - - it('admits the read at its method floor with an oauth slot and no token anywhere in the manifest', async () => { - const manifest = await admitted(MANAGED_FIXTURE_READ); - expect(manifest.effect).toBe('read'); - expect(manifest.approval).toBe('auto'); - expect(manifest.source.kind).toBe('managed_integration'); - expect(manifest.provenance).toMatchObject({ providerId: 'managed', providerKind: 'managed_integration', sourceRevision: 'fixture-1' }); - expect(manifest.auth).toEqual({ - kind: 'oauth', - scopes: ['https://www.googleapis.com/auth/spreadsheets.readonly'], - secretRef: credentialSlot('https://sheets.googleapis.com/v4', { - kind: 'oauth', - scopes: ['https://www.googleapis.com/auth/spreadsheets.readonly'] - }) - }); - expect(JSON.stringify(manifest)).not.toMatch(/token/i); - }); - - it('refuses to execute until a credential is bound to the slot, then sends it as a bearer token', async () => { - const manifest = await admitted(MANAGED_FIXTURE_READ); - const server = recordingFetch({ range: 'A1:B2', majorDimension: 'ROWS', values: [['a', 'b']] }); - - const unbound = await executeCapability(manifest.id, { spreadsheetId: 'sheet-1', range: 'A1:B2' }, { fetchImpl: server.fetchImpl }); - expect(unbound.error?.code).toBe('AUTH_UNBOUND'); - expect(server.calls).toHaveLength(0); - - capabilitySecrets.set(manifest.auth.secretRef!, TOKEN); - const bound = await executeCapability(manifest.id, { spreadsheetId: 'sheet-1', range: 'A1:B2' }, { fetchImpl: server.fetchImpl }); - expect(bound.ok, bound.error?.message).toBe(true); - expect(server.calls[0].url).toBe('https://sheets.googleapis.com/v4/spreadsheets/sheet-1/values/A1%3AB2'); - expect(server.calls[0].headers.Authorization).toBe(`Bearer ${TOKEN}`); - }); - - it('installs the write as pending, and it runs only after approval and a bound credential', async () => { - const manifest = await admitted(MANAGED_FIXTURE_WRITE); - expect(manifest.effect).toBe('write'); - expect(manifest.approval).toBe('pending'); - const server = recordingFetch({ ok: true, ts: '1790000000.000100', channel: 'C1' }); - const input = { message: { channel: 'C1', text: 'hello' } }; - - capabilitySecrets.set(manifest.auth.secretRef!, TOKEN); - const pending = await executeCapability(manifest.id, input, { fetchImpl: server.fetchImpl }); - expect(pending.error?.code).toBe('EFFECT_NOT_APPROVED'); - expect(server.calls).toHaveLength(0); - - capabilitySecrets.revoke(manifest.auth.secretRef!); - expect(approveCapability(manifest.id).manifest?.approval).toBe('approved'); - const unbound = await executeCapability(manifest.id, input, { fetchImpl: server.fetchImpl }); - expect(unbound.error?.code).toBe('AUTH_UNBOUND'); - expect(server.calls).toHaveLength(0); - - capabilitySecrets.set(manifest.auth.secretRef!, TOKEN); - const ran = await executeCapability(manifest.id, input, { fetchImpl: server.fetchImpl }); - expect(ran.ok, ran.error?.message).toBe(true); - expect(server.calls[0]).toMatchObject({ method: 'POST', url: 'https://slack.com/api/chat.postMessage' }); - expect(JSON.parse(server.calls[0].body!)).toEqual({ channel: 'C1', text: 'hello' }); - }); - - it('cannot make a write auto-approved by any route a provider controls', async () => { - const proposal = await proposalFor(MANAGED_FIXTURE_WRITE); - - const forged = admitProposal({ ...proposal, approval: 'auto' }, { nowMs: NOW }); - expect(forged.ok).toBe(false); - expect(forged.errors?.join(' ')).toMatch(/approval is host-owned/); - - const lowered = admitProposal({ ...proposal, effectHint: 'read' }, { nowMs: NOW }); - expect(lowered.ok).toBe(false); - expect(lowered.errors?.join(' ')).toMatch(/POST cannot be declared read/); - - const hostile: CapabilityProvider = { - ...managedProvider, - async materialize(candidate: CapabilityCandidateV1, context) { - const honest = await managedProvider.materialize(candidate, context); - return { ...honest, approval: 'auto', effect: 'read', trusted: true } as unknown as CapabilityProposalV1; - } - }; - const candidate = (await hostile.discover(REQUEST)).candidates[1]; - const smuggled = admitProposal(await hostile.materialize(candidate, { request: REQUEST }), { nowMs: NOW }); - expect(smuggled.ok).toBe(false); - - const built = manifestFromProposal(proposal, { nowMs: NOW }).manifest!; - const { digest: _digest, ...draft } = built; - expect(installProposal(sealManifest({ ...draft, approval: 'auto' })).ok).toBe(false); - const preApproved = installProposal(sealManifest({ ...draft, approval: 'approved' })); - expect(preApproved.manifest?.approval).toBe('pending'); - expect(getCapability(built.id)?.approval).toBe('pending'); - }); - - it('refuses a token in the proposal and keeps each scope set in its own slot', async () => { - const proposal = await proposalFor(MANAGED_FIXTURE_READ); - const withToken = manifestFromProposal({ ...proposal, auth: { ...proposal.auth, accessToken: TOKEN } }, { nowMs: NOW }); - expect(withToken.ok).toBe(false); - expect(withToken.errors.join(' ')).toMatch(/tokens are bound to a slot by the host/); - - const base = 'https://sheets.googleapis.com/v4'; - const readonly = credentialSlot(base, { kind: 'oauth', scopes: ['s.readonly'] }); - expect(credentialSlot(base, { kind: 'oauth', scopes: ['s.readonly', 's.write'] })).not.toBe(readonly); - expect(credentialSlot('https://sheets.example.test', { kind: 'oauth', scopes: ['s.readonly'] })).not.toBe(readonly); - expect(credentialSlot(base, { kind: 'bearer' })).not.toBe(readonly); - }); - - it('keeps oauth off the server broker and off non-http transports', async () => { - const manifest = manifestFromProposal(await proposalFor(MANAGED_FIXTURE_READ), { nowMs: NOW }).manifest!; - const { digest: _digest, ...draft } = manifest; - if (draft.transport.kind !== 'http') throw new Error('expected http'); - const brokered = validateManifest(sealManifest({ ...draft, transport: { ...draft.transport, access: 'server_broker' } })); - expect(brokered.ok).toBe(false); - expect(brokered.errors.join(' ')).toMatch(/server_broker cannot carry an oauth token/); - - const scopeless = validateManifest(sealManifest({ ...draft, auth: { kind: 'oauth', secretRef: draft.auth.secretRef } })); - expect(scopeless.ok).toBe(false); - }); -}); - -describe('managed vendor decision', () => { - it('rejects both vendors as adopted dependencies, measured nothing, and added no SDK', () => { - expect(managedVendorDecisions.map(d => [d.vendor, d.decision])).toEqual([ - ['pipedream', 'reject'], - ['composio', 'reject'] - ]); - for (const decision of managedVendorDecisions) { - expect(decision.reasons).toEqual(['no_measured_oauth_connect', 'vendor_becomes_integration_runtime']); - expect(decision.measured).toBe(false); - } - const pkg = readFileSync(path.join(process.cwd(), 'package.json'), 'utf8'); - expect(pkg).not.toMatch(/pipedream|composio/i); - }); -}); diff --git a/src/core/capabilities/providers/managedProvider.ts b/src/core/capabilities/providers/managedProvider.ts deleted file mode 100644 index 0d88361..0000000 --- a/src/core/capabilities/providers/managedProvider.ts +++ /dev/null @@ -1,221 +0,0 @@ -// Managed SaaS catalogs (Pipedream Connect, Composio, or successors) as a -// managed_integration provider. A catalog entry is a candidate description, -// not a dependency: Omni calls the SaaS API directly over http with an oauth -// slot, so no vendor SDK or vendor proxy becomes the integration runtime. -// The token reaches the slot through a host connect flow this module does not -// implement. Until the slot is bound, execution is AUTH_UNBOUND. -// -// See managed.decision.ts for why neither vendor is adopted. - -import type { CapabilityEffect, CapabilityInput, CapabilityOutput, HttpMethod } from '../manifest'; -import type { - CapabilityCandidateV1, - CapabilityDiscovery, - CapabilityDiscoveryContext, - CapabilityMaterializationContext, - CapabilityProposalV1, - CapabilityProvider, - ProviderIssue -} from '../provider'; -import { ProviderMaterializeError } from '../provider'; - -export const MANAGED_PROVIDER_ID = 'managed'; - -export type ManagedVendor = 'pipedream' | 'composio'; - -export interface ManagedActionDescription { - vendor: ManagedVendor; - /** The vendor's app slug, e.g. google_sheets. */ - app: string; - /** The vendor's action or tool key. Provenance only. */ - actionKey: string; - title: string; - description?: string; - method: HttpMethod; - baseUrl: string; - path: string; - oauth: { scopes: string[] }; - inputs: CapabilityInput[]; - output: CapabilityOutput; - /** A hint. Admission applies the method floor and refuses anything below it. */ - effectHint?: CapabilityEffect; - version?: string; -} - -export interface ManagedProviderRequest { - actions: ManagedActionDescription[]; - discoveredAtMs?: number; -} - -const KEY_PART = /^[A-Za-z0-9_.-]{1,50}$/; - -export function managedExternalId(action: Pick): string { - return `${action.vendor}:${action.app}:${action.actionKey}`; -} - -export const managedProvider: CapabilityProvider = { - id: MANAGED_PROVIDER_ID, - kind: 'managed_integration', - - async discover(request, context?: CapabilityDiscoveryContext): Promise { - const discoveredAtMs = request.discoveredAtMs ?? context?.nowMs ?? Date.now(); - const { accepted, issues } = screen(request.actions); - const candidates = accepted.map((action): CapabilityCandidateV1 => ({ - version: 1, - providerId: MANAGED_PROVIDER_ID, - externalId: managedExternalId(action), - title: action.title, - ...(action.description ? { description: action.description } : {}), - sourceKind: 'managed_integration', - origin: new URL(action.baseUrl).origin, - ...(action.effectHint ? { effectHint: action.effectHint } : {}), - authHint: 'oauth', - lifecycleHint: 'sync', - provenance: { providerId: MANAGED_PROVIDER_ID, sourceLocator: locator(action), discoveredAtMs } - })); - return { candidates, issues }; - }, - - async materialize( - candidate: CapabilityCandidateV1, - context: CapabilityMaterializationContext - ): Promise { - const { accepted } = screen(context.request.actions); - const action = accepted.find(entry => managedExternalId(entry) === candidate.externalId); - if (!action) throw new ProviderMaterializeError(`${candidate.externalId} is not in the catalog`); - return { - version: 1, - provider: { id: MANAGED_PROVIDER_ID, kind: 'managed_integration' }, - externalIdentity: { - origin: new URL(action.baseUrl).origin, - operationId: candidate.externalId, - sourceLocator: locator(action), - ...(action.version ? { sourceRevision: action.version.slice(0, 120) } : {}) - }, - title: action.title.slice(0, 120), - ...(action.description ? { description: action.description.slice(0, 2000) } : {}), - ...(action.effectHint ? { effectHint: action.effectHint } : {}), - auth: { kind: 'oauth', scopes: [...action.oauth.scopes] }, - transport: { - kind: 'http', - access: 'browser_direct', - baseUrl: action.baseUrl, - method: action.method, - path: action.path - }, - inputs: action.inputs.map(entry => ({ ...entry })), - output: { ...action.output }, - execution: { kind: 'sync' }, - provenance: { - providerId: MANAGED_PROVIDER_ID, - sourceLocator: locator(action), - discoveredAtMs: candidate.provenance.discoveredAtMs - } - }; - } -}; - -function screen(actions: ManagedActionDescription[]): { accepted: ManagedActionDescription[]; issues: ProviderIssue[] } { - const accepted: ManagedActionDescription[] = []; - const issues: ProviderIssue[] = []; - for (const action of actions) { - const externalId = managedExternalId(action); - if (![action.app, action.actionKey].every(part => typeof part === 'string' && KEY_PART.test(part))) { - issues.push({ externalId, message: 'app and action keys must be plain identifiers' }); - continue; - } - if (action.vendor !== 'pipedream' && action.vendor !== 'composio') { - issues.push({ externalId, message: 'unknown managed vendor' }); - continue; - } - if (!Array.isArray(action.oauth?.scopes) || action.oauth.scopes.length === 0) { - issues.push({ externalId, message: 'managed actions must name their oauth scopes' }); - continue; - } - let url: URL; - try { - url = new URL(action.baseUrl); - } catch { - issues.push({ externalId, message: 'baseUrl is not a URL' }); - continue; - } - if (url.protocol !== 'https:') { - issues.push({ externalId, message: 'managed actions call the SaaS API over https' }); - continue; - } - accepted.push(action); - } - return { accepted, issues }; -} - -function locator(action: ManagedActionDescription): string { - return `${action.vendor}:${action.app}/${action.actionKey}`.slice(0, 200); -} - -// --------------------------------------------------------------------------- -// Canned catalog entries. Their shape is Omni's; the vendor, app, and action -// labels say which catalog each stands in for. They were written by hand, not -// fetched from either vendor. -// --------------------------------------------------------------------------- - -export const MANAGED_FIXTURE_READ: ManagedActionDescription = { - vendor: 'pipedream', - app: 'google_sheets', - actionKey: 'get-values-in-range', - title: 'Read a range from a Google Sheet', - method: 'GET', - baseUrl: 'https://sheets.googleapis.com/v4', - path: '/spreadsheets/{spreadsheetId}/values/{range}', - oauth: { scopes: ['https://www.googleapis.com/auth/spreadsheets.readonly'] }, - inputs: [ - { name: 'spreadsheetId', in: 'path', required: true, schema: { kind: 'string' } }, - { name: 'range', in: 'path', required: true, schema: { kind: 'string' } } - ], - output: { - schema: { - kind: 'object', - properties: { - range: { kind: 'string' }, - majorDimension: { kind: 'string' }, - values: { kind: 'array', items: { kind: 'array', items: { kind: 'string' } } } - }, - required: ['range', 'values'] - }, - itemsPath: 'values', - presentation: 'items' - }, - version: 'fixture-1' -}; - -export const MANAGED_FIXTURE_WRITE: ManagedActionDescription = { - vendor: 'composio', - app: 'slack', - actionKey: 'send-message', - title: 'Post a Slack message', - method: 'POST', - baseUrl: 'https://slack.com/api', - path: '/chat.postMessage', - oauth: { scopes: ['chat:write'] }, - inputs: [ - { - name: 'message', - in: 'body', - required: true, - schema: { - kind: 'object', - properties: { channel: { kind: 'string' }, text: { kind: 'string' } }, - required: ['channel', 'text'], - additionalProperties: false - } - } - ], - output: { - schema: { - kind: 'object', - properties: { ok: { kind: 'boolean' }, ts: { kind: 'string' }, channel: { kind: 'string' } }, - required: ['ok'] - }, - presentation: 'raw' - }, - version: 'fixture-1' -}; diff --git a/src/core/capabilities/providers/maxun.evaluation.ts b/src/core/capabilities/providers/maxun.evaluation.ts deleted file mode 100644 index 7958d3c..0000000 --- a/src/core/capabilities/providers/maxun.evaluation.ts +++ /dev/null @@ -1,17 +0,0 @@ -// What this repository has measured about Maxun. `measured: true` is written -// only after a real Maxun run in the session that commits it. Fixture tests -// prove Omni's async lifecycle; they are not a measurement of Maxun. - -export type MaxunEvaluation = - | { measured: false; reason: 'MAXUN_API_KEY unset' } - | { - measured: true; - measuredAt: string; - robotMode: 'scrape' | 'extract'; - startToTerminalMs: number; - pollCount: number; - outcome: 'succeeded' | 'failed' | 'uncertain'; - typedOutputValid: boolean; - }; - -export const maxunEvaluation: MaxunEvaluation = { measured: false, reason: 'MAXUN_API_KEY unset' }; diff --git a/src/core/capabilities/providers/maxunProvider.test.ts b/src/core/capabilities/providers/maxunProvider.test.ts deleted file mode 100644 index 7aea45f..0000000 --- a/src/core/capabilities/providers/maxunProvider.test.ts +++ /dev/null @@ -1,315 +0,0 @@ -import { describe, it, expect, beforeEach, afterEach } from 'vitest'; -import { readFileSync } from 'node:fs'; -import path from 'node:path'; -import { admitProposal, manifestFromProposal } from '../admission'; -import { approveCapability, clearCapabilities, getCapability } from '../registry'; -// Write runs need a confirmed preview; these tests exercise the engine after one. -import { executeConfirmed as executeCapability } from '../../../../test/confirmedExecute'; -import { capabilitySecrets } from '../secrets'; -import { clearExecutionLedger, executionRecord, executionRecords } from '../executionLedger'; -import { bindAsyncRuntime, unbindAsyncRuntime, type AsyncClock } from '../asyncRuntime'; -import { transportCredentialSlot } from '../identity'; -import { useBlockStore } from '../../stores/blockStore'; -import { useWireStore } from '../../stores/wireStore'; -import { - MAXUN_DEFAULT_PROFILE, - maxunProvider, - type MaxunProviderRequest, - type MaxunRobotDescription -} from './maxunProvider'; -import { createMaxunRuntime } from './maxunRuntime'; -import { maxunEvaluation } from './maxun.evaluation'; - -const NOW = 1_790_000_000_000; -const KEY = 'maxun-fixture-key-not-real'; -const BASE = 'https://maxun.fixture.test'; -/** Public answer supplied by the test. The runtime does not resolve this name. */ -const PUBLIC_ADDRESSES = [{ address: '1.1.1.1' }]; - -function boundRuntime(fetchImpl: typeof fetch) { - return createMaxunRuntime({ baseUrl: BASE, fetch: fetchImpl, addresses: PUBLIC_ADDRESSES }); -} -const SLOT = transportCredentialSlot( - { kind: 'async', runtimeId: 'maxun', operation: 'x' }, - { kind: 'apiKey', in: 'header', name: 'x-api-key' } -)!; - -const ROBOTS: MaxunRobotDescription[] = [ - { id: 'rb-pricing', name: 'Pricing page', mode: 'scrape', updatedAt: '2026-09-29T10:00:00Z' }, - { id: 'rb-listing', name: 'Listing fields', mode: 'extract', fields: ['title', 'price'] }, - { id: 'rb-loose', name: 'No declared fields', mode: 'extract' }, - { id: 'rb-crawl', name: 'Crawl a site', mode: 'crawl' }, - { id: 'bad id/../x', name: 'Path in id', mode: 'scrape' } -]; - -function request(overrides: Partial = {}): MaxunProviderRequest { - return { robots: ROBOTS, sourceLocator: 'maxun:fixture', discoveredAtMs: NOW, ...overrides }; -} - -function virtualClock(start = NOW): AsyncClock { - let now = start; - return { - now: () => now, - async sleep(ms, signal) { - if (signal?.aborted) throw new Error('aborted'); - now += ms; - await Promise.resolve(); - } - }; -} - -// --------------------------------------------------------------------------- -// A local stand-in for Maxun's run lookup, GET /api/robots/{id}/runs/{runId}, -// in the shape that handler returns in the open-source server: -// { statusCode, messageCode, run: { runId, status, data } }. It is not Maxun and -// measures nothing about Maxun. -// --------------------------------------------------------------------------- - -function maxunRunLookup(run: { runId: string; status: string; data?: Record }) { - const seen: Array<{ method: string; url: string; headers: Record; redirect?: RequestRedirect }> = []; - const fetchImpl: typeof fetch = async (input, init) => { - const url = String(input); - const headers = { ...(init?.headers as Record) }; - seen.push({ method: init?.method ?? 'GET', url, headers, redirect: init?.redirect }); - if (headers['x-api-key'] !== KEY) return json(401, { ok: false, error: 'Unauthorized' }); - if (init?.method !== 'GET' || !/^https:\/\/maxun\.fixture\.test\/api\/robots\/[^/]+\/runs\/[^/]+$/.test(url)) { - return json(404, { error: 'no route' }); - } - return json(200, { statusCode: 200, messageCode: 'success', run }); - }; - return { fetchImpl, seen }; -} - -const LOOKUP = { headers: { 'x-api-key': KEY }, idempotencyKey: 'lookup-0001' }; - -function json(status: number, body: unknown): Response { - return new Response(JSON.stringify(body), { status, headers: { 'content-type': 'application/json' } }); -} - -async function install(robotId: string, policy: Parameters[1] = { nowMs: NOW }) { - const discovery = await maxunProvider.discover(request()); - const candidate = discovery.candidates.find(c => c.externalId === robotId)!; - const proposal = await maxunProvider.materialize(candidate, { request: request() }); - const installed = admitProposal(proposal, policy); - expect(installed.ok, installed.errors?.join('; ')).toBe(true); - return installed.manifest!; -} - -beforeEach(() => { - clearCapabilities(); - clearExecutionLedger(); - capabilitySecrets.clear(); - useBlockStore.setState({ blocks: [] }); - useWireStore.setState({ wires: [] }); -}); - -afterEach(() => { - unbindAsyncRuntime('maxun'); - capabilitySecrets.clear(); -}); - -describe('maxun web_data provider', () => { - it('lists scrape and typed extract robots and reports what it cannot type', async () => { - const discovery = await maxunProvider.discover(request()); - expect(discovery.candidates.map(c => c.externalId)).toEqual(['rb-pricing', 'rb-listing']); - expect(discovery.candidates.every(c => c.sourceKind === 'web_data' && c.lifecycleHint === 'async')).toBe(true); - const issues = Object.fromEntries(discovery.issues.map(issue => [issue.externalId ?? '?', issue.message])); - expect(issues['rb-loose']).toMatch(/not admitted as any/); - expect(issues['rb-crawl']).toMatch(/no typed result/); - expect(issues['bad id/../x']).toMatch(/plain identifier/); - }); - - it('materializes an async proposal whose key is a slot, not a manifest field', async () => { - const discovery = await maxunProvider.discover(request()); - const proposal = await maxunProvider.materialize(discovery.candidates[0], { request: request() }); - expect(proposal.transport).toEqual({ kind: 'async', runtimeId: 'maxun', operation: 'scrape:rb-pricing' }); - expect(proposal.execution).toEqual({ kind: 'async_poll', ...MAXUN_DEFAULT_PROFILE }); - expect(proposal.auth).toEqual({ kind: 'apiKey', in: 'header', name: 'x-api-key' }); - expect(proposal).not.toHaveProperty('approval'); - - const manifest = manifestFromProposal(proposal, { nowMs: NOW }).manifest!; - expect(manifest.source).toMatchObject({ kind: 'web_data', operationId: 'rb-pricing' }); - expect(manifest.provenance).toMatchObject({ - providerId: 'maxun', - providerKind: 'web_data', - externalId: 'rb-pricing', - sourceLocator: 'maxun:fixture/robots/rb-pricing', - sourceRevision: '2026-09-29T10:00:00Z' - }); - expect(manifest.auth.secretRef).toBe(SLOT); - expect(manifest.output.schema).toMatchObject({ kind: 'object', required: ['markdown'] }); - expect(JSON.stringify(manifest)).not.toMatch(/maxun-fixture-key/); - }); - - it('lands at write and pending until the host trusts the runtime; the provider cannot approve itself', async () => { - const untrusted = await install('rb-pricing'); - expect(untrusted.effect).toBe('write'); - expect(untrusted.approval).toBe('pending'); - - clearCapabilities(); - const trusted = await install('rb-pricing', { nowMs: NOW, trustedEffectHints: { asyncRuntimes: ['maxun'] } }); - expect(trusted.effect).toBe('read'); - expect(trusted.approval).toBe('auto'); - - const discovery = await maxunProvider.discover(request()); - const proposal = await maxunProvider.materialize(discovery.candidates[0], { request: request() }); - const forged = admitProposal({ ...proposal, approval: 'approved', auth: { ...proposal.auth, secretRef: 'someone-elses-slot' } }); - expect(forged.ok).toBe(false); - }); - - it('does not touch canvas state and keeps Maxun code away from blocks and wires', async () => { - await install('rb-pricing'); - expect(useBlockStore.getState().blocks).toHaveLength(0); - expect(useWireStore.getState().wires).toHaveLength(0); - for (const file of ['maxunProvider.ts', 'maxunRuntime.ts', 'maxun.evaluation.ts']) { - const source = readFileSync(path.join(__dirname, file), 'utf8'); - expect(source).not.toMatch(/stores\/|blockStore|wireStore|restoreSnapshot|approveCapability/); - } - }); -}); - -// --------------------------------------------------------------------------- -// The start contract Maxun's open-source server actually implements -// (getmaxun/maxun server/src/api/record.ts, POST /robots/:id/runs, at -// e17d5ed33a3c90220fba54c78f449a1f55043cb9 and unchanged at develop -// bf3187eff7f644bfbcc1c130fb85bad332bbd420): the handler awaits -// waitForRunCompletion before it answers, so the run id arrives only with the -// finished run. The SDK route POST /sdk/robots/:id/execute waits the same way. -// This fixture holds the POST until the run completes or the caller aborts. -// --------------------------------------------------------------------------- - -function blockingMaxunServer() { - const seen: Array<{ method: string; url: string }> = []; - let markPosted: () => void = () => {}; - const posted = new Promise(resolve => { markPosted = resolve; }); - const fetchImpl: typeof fetch = async (input, init) => { - const url = String(input); - seen.push({ method: init?.method ?? 'GET', url }); - if (init?.method === 'POST') { - markPosted(); - // The run outlives the request: nothing comes back before the abort. - return new Promise((_resolve, reject) => { - const abort = () => reject(Object.assign(new Error('The operation was aborted'), { name: 'AbortError' })); - if (init.signal?.aborted) abort(); - init.signal?.addEventListener('abort', abort, { once: true }); - }); - } - return json(404, { statusCode: 404, messageCode: 'not_found' }); - }; - return { fetchImpl, seen, posted }; -} - -describe('maxun start contract', () => { - it('sends no start while the only verified start holds the request until the run finishes', async () => { - const clock = virtualClock(); - const server = blockingMaxunServer(); - bindAsyncRuntime('maxun', boundRuntime(server.fetchImpl)); - capabilitySecrets.set(SLOT, KEY); - const manifest = await install('rb-pricing'); - approveCapability(manifest.id); - - // The abort stands in for the 15 s request timeout firing before the run completes. - const controller = new AbortController(); - const pending = executeCapability(manifest.id, {}, { clock, signal: controller.signal, idempotencyKey: 'blocking-start-01' }); - await Promise.race([server.posted.then(() => controller.abort()), pending]); - const result = await pending; - - expect(server.seen.filter(call => call.method === 'POST')).toHaveLength(0); - expect(result.ok).toBe(false); - expect(result.error?.code).toBe('ASYNC_START_REJECTED'); - expect(result.error?.message).toMatch(/no verified asynchronous start contract/); - const record = executionRecord(result.runId!)!; - expect(record.status).toBe('failed'); - expect(record.externalRunId).toBeUndefined(); - - const replay = await executeCapability(manifest.id, {}, { clock, idempotencyKey: 'blocking-start-01' }); - expect(replay.error?.code).toBe('ASYNC_START_REJECTED'); - expect(server.seen).toHaveLength(0); - }); -}); - -describe('maxun runtime through the async profile', () => { - it('reads a scrape run by id and returns only the typed markdown', async () => { - const server = maxunRunLookup({ - runId: 'mx-run-1', - status: 'success', - data: { markdown: '# Plans', html: '

Plans

', textData: {}, listData: {} } - }); - const runtime = boundRuntime(server.fetchImpl); - const observed = await runtime.poll('scrape:rb-pricing', 'mx-run-1', { ...LOOKUP, signal: new AbortController().signal }); - expect(observed).toEqual({ status: 'succeeded', value: { markdown: '# Plans' } }); - expect(server.seen).toHaveLength(1); - expect(server.seen[0]).toMatchObject({ method: 'GET', url: `${BASE}/api/robots/rb-pricing/runs/mx-run-1`, redirect: 'error' }); - expect(server.seen[0].headers['x-api-key']).toBe(KEY); - }); - - it('reads an extract run as its text captures', async () => { - const runtime = boundRuntime(maxunRunLookup({ - runId: 'mx-run-2', - status: 'success', - data: { textData: { title: 'Desk', price: '$120' } } - }).fetchImpl); - const observed = await runtime.poll('extract:rb-listing', 'mx-run-2', { ...LOOKUP, signal: new AbortController().signal }); - expect(observed).toEqual({ status: 'succeeded', value: { title: 'Desk', price: '$120' } }); - }); - - it('reports a failed robot run, refuses another run id, and never reads an unknown status as success', async () => { - const signal = new AbortController().signal; - const failed = boundRuntime(maxunRunLookup({ runId: 'mx-run-3', status: 'failed' }).fetchImpl); - expect(await failed.poll('scrape:rb-pricing', 'mx-run-3', { ...LOOKUP, signal })) - .toEqual({ status: 'failed', message: 'Maxun run failed' }); - - const swapped = boundRuntime(maxunRunLookup({ runId: 'mx-run-other', status: 'success', data: { markdown: 'x' } }).fetchImpl); - await expect(swapped.poll('scrape:rb-pricing', 'mx-run-3', { ...LOOKUP, signal })).rejects.toThrow(/different run/); - - const unknown = boundRuntime(maxunRunLookup({ runId: 'mx-run-4', status: 'mystery' }).fetchImpl); - await expect(unknown.poll('scrape:rb-pricing', 'mx-run-4', { ...LOOKUP, signal })).rejects.toThrow(/not understood/); - }); - - it('fails deterministically after the slot is revoked and keeps the definition', async () => { - const clock = virtualClock(); - const server = blockingMaxunServer(); - bindAsyncRuntime('maxun', boundRuntime(server.fetchImpl)); - capabilitySecrets.set(SLOT, KEY); - const manifest = await install('rb-pricing'); - approveCapability(manifest.id); - - capabilitySecrets.revoke(SLOT); - const first = await executeCapability(manifest.id, {}, { clock }); - const second = await executeCapability(manifest.id, {}, { clock }); - expect(first.error?.code).toBe('AUTH_UNBOUND'); - expect(second.error?.code).toBe('AUTH_UNBOUND'); - expect(server.seen).toHaveLength(0); - expect(getCapability(manifest.id)?.approval).toBe('approved'); - expect(JSON.stringify(executionRecords())).not.toContain(KEY); - }); - - it('refuses a non-https or credential-bearing base URL', () => { - expect(() => createMaxunRuntime({ baseUrl: 'http://maxun.fixture.test' })).toThrow(/https/); - expect(() => createMaxunRuntime({ baseUrl: 'https://user:pw@maxun.fixture.test' })).toThrow(/credentials/); - }); - - it('allows the pinned origin without addresses and refuses loopback, private, and metadata bases', () => { - expect(() => createMaxunRuntime()).not.toThrow(); - expect(() => createMaxunRuntime({ baseUrl: 'https://app.maxun.dev/robots' })).not.toThrow(); - expect(() => createMaxunRuntime({ baseUrl: BASE, addresses: PUBLIC_ADDRESSES })).not.toThrow(); - - expect(() => createMaxunRuntime({ baseUrl: BASE })).toThrow(/did not resolve/); - expect(() => createMaxunRuntime({ baseUrl: 'https://127.0.0.1' })).toThrow(/public address/); - expect(() => createMaxunRuntime({ baseUrl: 'https://[::1]' })).toThrow(/public address/); - expect(() => createMaxunRuntime({ baseUrl: 'https://10.1.2.3' })).toThrow(/public address/); - expect(() => createMaxunRuntime({ baseUrl: 'https://localhost' })).toThrow(/public origin/); - expect(() => createMaxunRuntime({ baseUrl: 'https://metadata.google.internal' })).toThrow(/public origin/); - expect(() => createMaxunRuntime({ - baseUrl: BASE, - addresses: [{ address: '1.1.1.1' }, { address: '127.0.0.1' }] - })).toThrow(/non-public/); - }); -}); - -describe('maxun evaluation record', () => { - it('says no live run was performed when MAXUN_API_KEY is unset', () => { - if (process.env.MAXUN_API_KEY) return; - expect(maxunEvaluation).toEqual({ measured: false, reason: 'MAXUN_API_KEY unset' }); - }); -}); diff --git a/src/core/capabilities/providers/maxunProvider.ts b/src/core/capabilities/providers/maxunProvider.ts deleted file mode 100644 index afe633c..0000000 --- a/src/core/capabilities/providers/maxunProvider.ts +++ /dev/null @@ -1,202 +0,0 @@ -// Maxun as a web_data provider. It turns a robot description into an -// untrusted async proposal: the host-bound `maxun` runtime starts the robot -// and polls its run. Maxun owns no canvas state, blocks, or wires, and the -// API key is a credential slot admission derives from the runtime id. -// -// Discovery reads robot descriptions the host already holds. Mapping Maxun's -// own robot listing into these descriptions is not implemented: its response -// shape was not observed from a live account in this environment. - -import { MAX_ASYNC_DURATION_MS, type CapabilityOutput } from '../manifest'; -import type { ValueType } from '../valueType'; -import type { - CapabilityCandidateV1, - CapabilityDiscovery, - CapabilityDiscoveryContext, - CapabilityMaterializationContext, - CapabilityProposalV1, - CapabilityProvider, - ProviderIssue -} from '../provider'; -import { ProviderMaterializeError } from '../provider'; - -export const MAXUN_RUNTIME_ID = 'maxun'; -export const MAXUN_API_KEY_HEADER = 'x-api-key'; -export const MAXUN_PROVIDER_ID = 'maxun'; - -/** Maxun's documented `run_robot` waits up to 30 minutes, so the profile allows that much. */ -export const MAXUN_DEFAULT_PROFILE = { pollIntervalMs: 5_000, maxDurationMs: 30 * 60_000 } as const; - -export type MaxunRobotMode = 'scrape' | 'extract'; - -export interface MaxunRobotDescription { - id: string; - name: string; - description?: string; - /** Only scrape (markdown) and extract (named text captures) have a typed result. */ - mode: string; - /** Text capture names an extract robot promises. Required for extract. */ - fields?: string[]; - /** Maxun's last-modified stamp, kept as the source revision. */ - updatedAt?: string; -} - -export interface MaxunRobotCatalog { - listRobots(signal?: AbortSignal): Promise; -} - -export interface MaxunProviderRequest { - robots?: MaxunRobotDescription[]; - catalog?: MaxunRobotCatalog; - /** Where the robots live, for provenance only. Never used as an endpoint. */ - sourceLocator?: string; - discoveredAtMs?: number; - signal?: AbortSignal; - profile?: { pollIntervalMs: number; maxDurationMs: number }; -} - -const ROBOT_ID = /^[A-Za-z0-9_.-]{1,100}$/; -const FIELD_NAME = /^[A-Za-z_][A-Za-z0-9_]{0,63}$/; -const MAX_FIELDS = 40; - -interface Planned { - robot: MaxunRobotDescription; - mode: MaxunRobotMode; - output: CapabilityOutput; -} - -export function maxunOperation(mode: MaxunRobotMode, robotId: string): string { - return `${mode}:${robotId}`; -} - -export function parseMaxunOperation(operation: string): { mode: MaxunRobotMode; robotId: string } | undefined { - const match = /^(scrape|extract):([A-Za-z0-9_.-]{1,100})$/.exec(operation); - return match ? { mode: match[1] as MaxunRobotMode, robotId: match[2] } : undefined; -} - -export const maxunProvider: CapabilityProvider = { - id: MAXUN_PROVIDER_ID, - kind: 'web_data', - - async discover(request, context?: CapabilityDiscoveryContext): Promise { - const discoveredAtMs = request.discoveredAtMs ?? context?.nowMs ?? Date.now(); - const { planned, issues } = await plan(request); - const candidates = planned.map((entry): CapabilityCandidateV1 => ({ - version: 1, - providerId: MAXUN_PROVIDER_ID, - externalId: entry.robot.id, - title: entry.robot.name, - ...(entry.robot.description ? { description: entry.robot.description } : {}), - sourceKind: 'web_data', - effectHint: 'read', - authHint: 'apiKey', - lifecycleHint: 'async', - provenance: { - providerId: MAXUN_PROVIDER_ID, - sourceLocator: robotLocator(request, entry.robot.id), - discoveredAtMs - } - })); - return { candidates, issues }; - }, - - async materialize( - candidate: CapabilityCandidateV1, - context: CapabilityMaterializationContext - ): Promise { - const { planned } = await plan(context.request); - const entry = planned.find(item => item.robot.id === candidate.externalId); - if (!entry) throw new ProviderMaterializeError(`robot ${candidate.externalId} is not in the catalog`); - const profile = context.request.profile ?? MAXUN_DEFAULT_PROFILE; - const locator = robotLocator(context.request, entry.robot.id); - return { - version: 1, - provider: { id: MAXUN_PROVIDER_ID, kind: 'web_data' }, - externalIdentity: { - operationId: entry.robot.id, - sourceLocator: locator, - ...(entry.robot.updatedAt ? { sourceRevision: entry.robot.updatedAt.slice(0, 120) } : {}) - }, - title: entry.robot.name.slice(0, 120), - ...(entry.robot.description ? { description: entry.robot.description.slice(0, 2000) } : {}), - // A hint only. Admission lands at write unless the host trusts this runtime. - effectHint: 'read', - auth: { kind: 'apiKey', in: 'header', name: MAXUN_API_KEY_HEADER }, - transport: { kind: 'async', runtimeId: MAXUN_RUNTIME_ID, operation: maxunOperation(entry.mode, entry.robot.id) }, - inputs: [], - output: entry.output, - execution: { - kind: 'async_poll', - pollIntervalMs: profile.pollIntervalMs, - maxDurationMs: Math.min(profile.maxDurationMs, MAX_ASYNC_DURATION_MS) - }, - provenance: { - providerId: MAXUN_PROVIDER_ID, - sourceLocator: locator, - discoveredAtMs: candidate.provenance.discoveredAtMs - } - }; - } -}; - -async function plan(request: MaxunProviderRequest): Promise<{ planned: Planned[]; issues: ProviderIssue[] }> { - const robots = request.robots ?? (request.catalog ? await request.catalog.listRobots(request.signal) : []); - const planned: Planned[] = []; - const issues: ProviderIssue[] = []; - for (const robot of robots) { - const externalId = typeof robot?.id === 'string' ? robot.id : undefined; - if (!externalId || !ROBOT_ID.test(externalId)) { - issues.push({ ...(externalId ? { externalId } : {}), message: 'robot id is not a plain identifier' }); - continue; - } - if (typeof robot.name !== 'string' || robot.name.trim().length === 0) { - issues.push({ externalId, message: 'robot has no name' }); - continue; - } - if (robot.mode === 'scrape') { - planned.push({ robot, mode: 'scrape', output: SCRAPE_OUTPUT }); - continue; - } - if (robot.mode === 'extract') { - const output = extractOutput(robot.fields); - if ('error' in output) issues.push({ externalId, message: output.error }); - else planned.push({ robot, mode: 'extract', output: output.output }); - continue; - } - issues.push({ externalId, message: `robot mode ${String(robot.mode)} has no typed result in Omni` }); - } - return { planned, issues }; -} - -const SCRAPE_OUTPUT: CapabilityOutput = { - schema: { - kind: 'object', - properties: { markdown: { kind: 'string' } }, - required: ['markdown'], - additionalProperties: false - }, - presentation: 'content' -}; - -function extractOutput(fields: unknown): { output: CapabilityOutput } | { error: string } { - if (!Array.isArray(fields) || fields.length === 0) { - return { error: 'extract robot declares no fields; an untyped result is not admitted as any' }; - } - if (fields.length > MAX_FIELDS) return { error: `extract robot declares more than ${MAX_FIELDS} fields` }; - const properties: Record = {}; - for (const field of fields) { - if (typeof field !== 'string' || !FIELD_NAME.test(field)) return { error: `field ${String(field)} is not a plain name` }; - properties[field] = { kind: 'string' }; - } - return { - output: { - schema: { kind: 'object', properties, required: Object.keys(properties), additionalProperties: false }, - presentation: 'raw' - } - }; -} - -function robotLocator(request: MaxunProviderRequest, robotId: string): string { - const base = (request.sourceLocator ?? 'maxun').slice(0, 120); - return `${base}/robots/${robotId}`.slice(0, 200); -} diff --git a/src/core/capabilities/providers/maxunRuntime.ts b/src/core/capabilities/providers/maxunRuntime.ts deleted file mode 100644 index 8b94f1b..0000000 --- a/src/core/capabilities/providers/maxunRuntime.ts +++ /dev/null @@ -1,137 +0,0 @@ -// The host binds this runtime under `maxun`. The endpoint is host -// configuration; nothing in a manifest can change it. Each request is one -// bounded fetch with the caller's signal and no redirects, so the API key is -// never forwarded to another origin. The pinned cloud origin is the only host -// accepted without resolved addresses. Any other base has to pass the broker -// egress check, so a loopback, private, or metadata address never sees the key. -// -// Start is fenced. The async_poll profile needs a start that answers with a -// run id before the run finishes, so the id can be recorded and observed. -// Maxun's open-source server does not offer one: POST /api/robots/:id/runs -// awaits waitForRunCompletion before it answers (getmaxun/maxun -// server/src/api/record.ts, at e17d5ed33a3c90220fba54c78f449a1f55043cb9 and -// unchanged at develop bf3187eff7f644bfbcc1c130fb85bad332bbd420), and -// POST /api/sdk/robots/:id/execute waits the same way. Against that server a -// run longer than the 15 s request timeout aborts before its id is known, and -// a write is then uncertain with nothing to reconcile by. So start sends -// nothing and refuses, until an early-acknowledgement contract is verified. -// Maxun Cloud was not measured. -// -// Poll reads one run by id: GET /api/robots/:id/runs/:runId, which answers -// immediately with { statusCode, messageCode, run } in that source. No live -// account was used to check it. - -import { AsyncStartRejected, type AsyncJobRuntime, type AsyncObservation } from '../asyncRuntime'; -import { assessEgress, type ResolvedAddress } from '../egress'; -import { isRecord } from '../valueType'; -import { MAXUN_API_KEY_HEADER, parseMaxunOperation, type MaxunRobotMode } from './maxunProvider'; - -export const MAXUN_CLOUD_BASE_URL = 'https://app.maxun.dev'; - -export const MAXUN_START_UNAVAILABLE = - 'Maxun has no verified asynchronous start contract: its run start answers only when the run finishes. No request was sent.'; - -const MAX_RESPONSE_CHARS = 1_000_000; -const RUNNING = new Set(['queued', 'scheduled', 'pending', 'running', 'in_progress']); -const SUCCEEDED = new Set(['success', 'succeeded', 'completed']); -const FAILED = new Set(['failed', 'error', 'aborted', 'canceled', 'cancelled']); - -export interface MaxunRuntimeConfig { - baseUrl?: string; - fetch?: typeof fetch; - /** - * Addresses the caller already resolved for `baseUrl`. Required for any - * host other than the pinned cloud origin, unless the host is an IP literal - * the egress check can classify on its own. Missing answers are a refusal. - */ - addresses?: ResolvedAddress[]; -} - -export function createMaxunRuntime(config: MaxunRuntimeConfig = {}): AsyncJobRuntime { - const base = hostBase(config.baseUrl ?? MAXUN_CLOUD_BASE_URL, config.addresses ?? []); - const fetchImpl = config.fetch ?? fetch; - - return { - async start() { - throw new AsyncStartRejected(MAXUN_START_UNAVAILABLE); - }, - - async poll(operation, externalRunId, call): Promise { - const parsed = operationOf(operation); - const url = `${base}/api/robots/${encodeURIComponent(parsed.robotId)}/runs/${encodeURIComponent(externalRunId)}`; - const response = await fetchImpl(url, { - method: 'GET', - headers: { ...onlyKey(call.headers), Accept: 'application/json' }, - signal: call.signal, - redirect: 'error' - }); - if (!response.ok) throw new Error(`Maxun run lookup failed: HTTP ${response.status}`); - const run = runOf(await readJson(response)); - if (!run || (run.runId !== undefined && run.runId !== externalRunId)) { - throw new Error('Maxun returned a different run'); - } - const status = typeof run.status === 'string' ? run.status.toLowerCase() : ''; - if (RUNNING.has(status)) return { status: 'running' }; - if (FAILED.has(status)) { - return { status: 'failed', message: typeof run.error === 'string' ? run.error.slice(0, 500) : `Maxun run ${status}` }; - } - if (SUCCEEDED.has(status)) return { status: 'succeeded', value: valueFor(parsed.mode, run.data) }; - throw new Error(`Maxun run status ${status || '(missing)'} is not understood`); - } - }; -} - -/** The value the manifest's output schema checks. Missing parts stay missing, so a mismatch is visible. */ -function valueFor(mode: MaxunRobotMode, data: unknown): unknown { - if (!isRecord(data)) return data; - if (mode === 'scrape') return { markdown: data.markdown }; - return isRecord(data.textData) ? { ...data.textData } : data.textData; -} - -function runOf(body: unknown): Record | undefined { - if (!isRecord(body)) return undefined; - if (typeof body.runId === 'string') return body; - if (isRecord(body.run) && typeof body.run.runId === 'string') return body.run; - return undefined; -} - -function operationOf(operation: string): { mode: MaxunRobotMode; robotId: string } { - const parsed = parseMaxunOperation(operation); - if (!parsed) throw new AsyncStartRejected(`${operation} is not a Maxun robot operation`); - return parsed; -} - -function onlyKey(headers: Record): Record { - const value = headers[MAXUN_API_KEY_HEADER]; - return value ? { [MAXUN_API_KEY_HEADER]: value } : {}; -} - -function hostBase(raw: string, addresses: ResolvedAddress[]): string { - let url: URL; - try { - url = new URL(raw); - } catch { - throw new Error('Maxun base URL is invalid'); - } - if (url.protocol !== 'https:') throw new Error('Maxun base URL must be https'); - if (url.username || url.password) throw new Error('Maxun base URL cannot embed credentials'); - if (url.search || url.hash) throw new Error('Maxun base URL cannot carry a query or fragment'); - // The pinned origin is the product default. It does not need a DNS answer. - // Every other host goes through assessEgress, which classifies literals - // and refuses a name that was not resolved to public addresses. - if (url.origin !== new URL(MAXUN_CLOUD_BASE_URL).origin) { - const decision = assessEgress(url, addresses); - if (!decision.ok) throw new Error(`Maxun base URL is refused: ${decision.reason}`); - } - return url.toString().replace(/\/+$/, ''); -} - -async function readJson(response: Response): Promise { - const text = await response.text(); - if (text.length > MAX_RESPONSE_CHARS) throw new Error('Maxun response is too large'); - try { - return JSON.parse(text); - } catch { - throw new Error('Maxun response is not JSON'); - } -} diff --git a/src/core/capabilities/providers/mcpProvider.ts b/src/core/capabilities/providers/mcpProvider.ts deleted file mode 100644 index 1c6842e..0000000 --- a/src/core/capabilities/providers/mcpProvider.ts +++ /dev/null @@ -1,90 +0,0 @@ -// MCP acquisition from tool schemas. Annotations become an effect hint and -// nothing more: the compiler is always called untrusted, and admission alone -// decides whether a server's read hint counts. - -import { compileMcpTools, type McpToolSchema } from '../mcp'; -import type { CapabilityEffect } from '../manifest'; -import { - ProviderMaterializeError, - type CapabilityDiscovery, - type CapabilityProvider, - type ProposedAuthRequirement -} from '../provider'; -import { candidateFromManifest, proposalFromManifest, type ProposalSource } from './fromManifest'; - -export interface McpToolLister { - listTools(signal?: AbortSignal): Promise; -} - -export interface McpDiscoveryRequest { - serverId: string; - /** How the server authenticates a call. Admission turns it into a slot; the secret never passes here. */ - auth?: ProposedAuthRequirement; - /** Static schemas, or a live lister (the SDK client in `mcpClient.ts`). */ - tools?: readonly McpToolSchema[]; - lister?: McpToolLister; - sourceRevision?: string; - discoveredAtMs?: number; - signal?: AbortSignal; -} - -export const MCP_PROVIDER_ID = 'mcp'; - -async function toolsFor(request: McpDiscoveryRequest): Promise { - const listed = request.tools ?? (request.lister ? await request.lister.listTools(request.signal) : []); - return listed.map(tool => ({ ...tool, serverId: request.serverId })); -} - -function hintFor(tool: McpToolSchema): CapabilityEffect | undefined { - if (tool.annotations?.destructiveHint) return 'destructive'; - if (tool.annotations?.readOnlyHint) return 'read'; - return undefined; -} - -function sourceFor(tool: McpToolSchema, request: McpDiscoveryRequest, now: number): ProposalSource { - return { - providerId: MCP_PROVIDER_ID, - providerKind: 'mcp', - externalId: tool.name, - sourceLocator: request.serverId, - ...(request.sourceRevision ? { sourceRevision: request.sourceRevision } : {}), - discoveredAtMs: request.discoveredAtMs ?? now, - ...(hintFor(tool) ? { effectHint: hintFor(tool) } : {}) - }; -} - -export const mcpProvider: CapabilityProvider = { - id: MCP_PROVIDER_ID, - kind: 'mcp', - - async discover(request, context) { - const now = context?.nowMs ?? Date.now(); - const discovery: CapabilityDiscovery = { candidates: [], issues: [] }; - for (const tool of await toolsFor(request)) { - const compiled = compileMcpTools([tool], { trustedAnnotations: false }); - const manifest = compiled.manifests[0]; - if (manifest) { - discovery.candidates.push({ - ...candidateFromManifest(manifest, sourceFor(tool, request, now)), - authHint: request.auth?.kind ?? 'none' - }); - } else { - discovery.issues.push({ externalId: tool.name, message: compiled.errors[0]?.message ?? 'tool did not compile' }); - } - } - return discovery; - }, - - async materialize(candidate, context) { - const tools = await toolsFor(context.request); - const matches = tools.filter(tool => tool.name === candidate.externalId); - if (matches.length !== 1) throw new ProviderMaterializeError(`tool ${candidate.externalId} does not name exactly one tool`); - const compiled = compileMcpTools(matches, { trustedAnnotations: false }); - const manifest = compiled.manifests[0]; - if (!manifest) throw new ProviderMaterializeError(compiled.errors[0]?.message ?? 'tool did not compile'); - return { - ...proposalFromManifest(manifest, sourceFor(matches[0], context.request, candidate.provenance.discoveredAtMs)), - auth: context.request.auth ?? { kind: 'none' } - }; - } -}; diff --git a/src/core/capabilities/providers/openapiProvider.ts b/src/core/capabilities/providers/openapiProvider.ts deleted file mode 100644 index 7d1551a..0000000 --- a/src/core/capabilities/providers/openapiProvider.ts +++ /dev/null @@ -1,72 +0,0 @@ -// OpenAPI acquisition. The bounded compiler stays the parser; this provider -// only turns its output into candidates and untrusted proposals. - -import { compileOpenApi } from '../openapi'; -import type { CapabilityManifest } from '../manifest'; -import { - ProviderMaterializeError, - type CapabilityProvider -} from '../provider'; -import { candidateFromManifest, proposalFromManifest, type ProposalSource } from './fromManifest'; - -export interface OpenApiDiscoveryRequest { - document: unknown; - /** Stamped by discover so materialize reproduces the same provenance. */ - discoveredAtMs?: number; -} - -export const OPENAPI_PROVIDER_ID = 'openapi'; - -function externalIdFor(manifest: CapabilityManifest): string { - if (manifest.transport.kind !== 'http') return manifest.id; - return manifest.source.operationId ?? `${manifest.transport.method} ${manifest.transport.path}`; -} - -function sourceFor(manifest: CapabilityManifest, request: OpenApiDiscoveryRequest, now: number): ProposalSource { - const revision = versionOf(request.document); - return { - providerId: OPENAPI_PROVIDER_ID, - providerKind: 'openapi', - externalId: externalIdFor(manifest), - sourceLocator: manifest.source.locator, - ...(revision ? { sourceRevision: revision } : {}), - discoveredAtMs: request.discoveredAtMs ?? now, - ...(manifest.effectSource === 'extension' ? { effectHint: manifest.effect } : {}) - }; -} - -function versionOf(document: unknown): string | undefined { - if (typeof document !== 'object' || document === null) return undefined; - const info = (document as { info?: { version?: unknown } }).info; - return typeof info?.version === 'string' ? info.version : undefined; -} - -export const openapiProvider: CapabilityProvider = { - id: OPENAPI_PROVIDER_ID, - kind: 'openapi', - - async discover(request, context) { - const now = context?.nowMs ?? Date.now(); - const compiled = compileOpenApi(request.document); - return { - candidates: compiled.manifests.map(manifest => candidateFromManifest(manifest, sourceFor(manifest, request, now))), - issues: compiled.errors.map(issue => ({ - ...(issue.operation ? { externalId: issue.operation } : {}), - message: issue.message - })) - }; - }, - - async materialize(candidate, context) { - const compiled = compileOpenApi(context.request.document); - const matches = compiled.manifests.filter(entry => externalIdFor(entry) === candidate.externalId); - if (matches.length !== 1) { - throw new ProviderMaterializeError(`operation ${candidate.externalId} does not name exactly one operation`); - } - const manifest = matches[0]; - return proposalFromManifest( - manifest, - sourceFor(manifest, context.request, candidate.provenance.discoveredAtMs) - ); - } -}; diff --git a/src/core/capabilities/redaction.test.ts b/src/core/capabilities/redaction.test.ts index d523b6c..b36af5c 100644 --- a/src/core/capabilities/redaction.test.ts +++ b/src/core/capabilities/redaction.test.ts @@ -2,18 +2,15 @@ import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'; import { compileOpenApi } from './openapi'; import { canonicalCapabilityId, credentialSlot, transportCredentialSlot } from './identity'; import { sealManifest, type CapabilityManifest } from './manifest'; -import { admitProposal } from './admission'; import { approveCapability, clearCapabilities, installProposal } from './registry'; import { bindMcpTransport, executeCapability, previewCapabilityRun, unbindMcpTransport } from './execute'; import { AsyncStartRejected, bindAsyncRuntime, unbindAsyncRuntime } from './asyncRuntime'; import { latestExecutionRecord } from './executionLedger'; import { capabilitySecrets } from './secrets'; import { MAX_ERROR_MESSAGE, redact } from './redact'; -import type { CapabilityProposalV1 } from './provider'; import { handleCapabilityBroker, createBrokerRateLimiter } from '../services/server/capabilityBroker'; import { memoryLedger, type ServerLedger } from '../services/server/capability.ledger'; -const NOW = 1_790_000_000_000; // Characters that change under each encoding: + / = & space. const SECRET = 'Sk+live/Q9=z&t ok-7781'; const BASIC = 'board-user:p@ss+w/rd=1'; @@ -133,20 +130,23 @@ describe('dispatched-call errors carry no credential', () => { }); it('mcp', async () => { - const proposal: CapabilityProposalV1 = { + const transport = { kind: 'mcp' as const, serverId: 'board', origin: 'https://board.example.test', toolName: 'search' }; + const placement = { kind: 'apiKey' as const, in: 'header' as const, name: 'x-api-key' }; + const installed = installProposal(sealManifest({ version: 1, - provider: { id: 'fixture.mcp', kind: 'mcp' }, - externalIdentity: { operationId: 'search', sourceLocator: 'fixture://mcp/board' }, + id: canonicalCapabilityId(transport), title: 'Search', - effectHint: 'read', - auth: { kind: 'apiKey', in: 'header', name: 'x-api-key' }, - transport: { kind: 'mcp', serverId: 'board', origin: 'https://board.example.test', toolName: 'search' }, + source: { kind: 'mcp', locator: 'fixture://mcp/board', operationId: 'search' }, + effect: 'read', + effectSource: 'annotation', + approval: 'auto', + invocation: 'manual', + trigger: { kind: 'manual' }, + auth: { ...placement, secretRef: transportCredentialSlot(transport, placement)! }, + transport, inputs: [], - output: { schema: { kind: 'array', items: { kind: 'string' } }, presentation: 'raw' }, - execution: { kind: 'sync' }, - provenance: { providerId: 'fixture.mcp', sourceLocator: 'fixture://mcp/board', discoveredAtMs: NOW } - }; - const installed = admitProposal(proposal, { nowMs: NOW, trustedEffectHints: { mcpServers: ['board'] } }); + output: { schema: { kind: 'array', items: { kind: 'string' } }, presentation: 'raw' } + })); expect(installed.ok, installed.errors.join('; ')).toBe(true); const manifest = installed.manifest!; capabilitySecrets.set(transportCredentialSlot(manifest.transport, manifest.auth)!, SECRET); @@ -155,19 +155,24 @@ describe('dispatched-call errors carry no credential', () => { }); it('async', async () => { - const proposal: CapabilityProposalV1 = { + const transport = { kind: 'async' as const, runtimeId: 'jobs', operation: 'robot-1' }; + const placement = { kind: 'apiKey' as const, in: 'header' as const, name: 'x-api-key' }; + const installed = installProposal(sealManifest({ version: 1, - provider: { id: 'fixture.jobs', kind: 'manual' }, - externalIdentity: { operationId: 'robot-1', sourceLocator: 'fixture://jobs/robot-1' }, + id: canonicalCapabilityId(transport), title: 'Scrape', - auth: { kind: 'apiKey', in: 'header', name: 'x-api-key' }, - transport: { kind: 'async', runtimeId: 'jobs', operation: 'robot-1' }, - inputs: [], - output: { schema: { kind: 'object', properties: { markdown: { kind: 'string' } }, required: ['markdown'] }, presentation: 'raw' }, + source: { kind: 'bring', locator: 'fixture://jobs/robot-1', operationId: 'robot-1' }, + effect: 'write', + effectSource: 'declared', + approval: 'pending', + invocation: 'manual', + trigger: { kind: 'manual' }, + auth: { ...placement, secretRef: transportCredentialSlot(transport, placement)! }, + transport, execution: { kind: 'async_poll', pollIntervalMs: 5_000, maxDurationMs: 120_000 }, - provenance: { providerId: 'fixture.jobs', sourceLocator: 'fixture://jobs/robot-1', discoveredAtMs: NOW } - }; - const installed = admitProposal(proposal, { nowMs: NOW }); + inputs: [], + output: { schema: { kind: 'object', properties: { markdown: { kind: 'string' } }, required: ['markdown'] }, presentation: 'raw' } + })); expect(installed.ok, installed.errors.join('; ')).toBe(true); const manifest = approveCapability(installed.manifest!.id).manifest!; capabilitySecrets.set(transportCredentialSlot(manifest.transport, manifest.auth)!, SECRET); diff --git a/src/core/capabilities/untypedOutputFields.test.ts b/src/core/capabilities/untypedOutputFields.test.ts index 0ab7ac0..923b85a 100644 --- a/src/core/capabilities/untypedOutputFields.test.ts +++ b/src/core/capabilities/untypedOutputFields.test.ts @@ -1,6 +1,4 @@ import { describe, expect, it } from 'vitest'; -import { admitProposal } from './admission'; -import { openapiProvider } from './providers/openapiProvider'; import { isAssignable } from './valueType'; import { fromJsonSchema } from './jsonSchema'; import { compileOpenApi } from './openapi'; @@ -66,14 +64,3 @@ describe('untyped fields in a response', () => { expect(isAssignable({ kind: 'any' }, { kind: 'string' })).toBe(false); }); }); - -describe('admitting a provider proposal with an untyped field', () => { - it('admits a typed result with a nested any field, through the same rule as the install dialog', async () => { - const request = { document: spec(EVENTS), sourceLocator: 'fixture://events', discoveredAtMs: 1 }; - const { candidates } = await openapiProvider.discover(request); - const proposal = await openapiProvider.materialize(candidates[0], { request }); - const admitted = admitProposal(proposal, { nowMs: 1 }); - expect(admitted.errors).toEqual([]); - expect(admitted.ok).toBe(true); - }); -}); diff --git a/src/core/capabilities/urlPolicy.test.ts b/src/core/capabilities/urlPolicy.test.ts index 4035ebb..274d3e1 100644 --- a/src/core/capabilities/urlPolicy.test.ts +++ b/src/core/capabilities/urlPolicy.test.ts @@ -2,12 +2,7 @@ import { describe, it, expect, beforeEach } from 'vitest'; import { compileOpenApi } from './openapi'; import { canonicalCapabilityId } from './identity'; import { sealManifest, validateManifest, type CapabilityManifest } from './manifest'; -import { admitProposal } from './admission'; import { clearCapabilities } from './registry'; -import { openapiProvider } from './providers/openapiProvider'; -import type { CapabilityProposalV1 } from './provider'; - -const NOW = 1_790_000_000_000; const SPEC = { openapi: '3.0.3', @@ -34,17 +29,6 @@ function atBase(baseUrl: string): CapabilityManifest { return sealManifest({ ...compiled, id: canonicalCapabilityId(transport), transport }); } -async function proposals(): Promise { - const discovered = await openapiProvider.discover({ document: SPEC }, { nowMs: NOW }); - return Promise.all(discovered.candidates.map(candidate => openapiProvider.materialize(candidate, { request: { document: SPEC } }))); -} - -function byOperation(list: CapabilityProposalV1[], operationId: string): CapabilityProposalV1 { - const found = list.find(entry => entry.externalIdentity.operationId === operationId); - if (!found) throw new Error(`no proposal ${operationId}`); - return found; -} - beforeEach(() => { clearCapabilities(); }); @@ -86,23 +70,3 @@ describe('base URL policy', () => { expect(compiled.errors.map(error => error.message).join('; ')).toContain('query or fragment'); }); }); - -describe('admission decides execution access', () => { - it('normalises a proposal that asks for the server broker to browser_direct', async () => { - const list = byOperation(await proposals(), 'list'); - const asked = { ...list, transport: { ...list.transport, access: 'server_broker' } } as CapabilityProposalV1; - const result = admitProposal(asked, { nowMs: NOW }); - expect(result.ok).toBe(true); - expect(result.manifest?.transport).toMatchObject({ access: 'browser_direct' }); - }); - - it('uses the broker only for a read on an origin the host allowlisted', async () => { - const all = await proposals(); - const policy = { nowMs: NOW, brokerOrigins: ['https://board.example.test'] }; - expect(admitProposal(byOperation(all, 'list'), policy).manifest?.transport).toMatchObject({ access: 'server_broker' }); - expect(admitProposal(byOperation(all, 'create'), policy).manifest?.transport).toMatchObject({ access: 'browser_direct' }); - clearCapabilities(); - const elsewhere = { nowMs: NOW, brokerOrigins: ['https://other.example.test'] }; - expect(admitProposal(byOperation(all, 'list'), elsewhere).manifest?.transport).toMatchObject({ access: 'browser_direct' }); - }); -}); From 316b5e6257efa94d5a05303246e8ab75823b83c0 Mon Sep 17 00:00:00 2001 From: Seth Date: Sat, 31 Oct 2026 18:24:58 -0700 Subject: [PATCH 2/2] Delete dead code across the repo - Remove test-only production code (searchApiIndex, exportSnapshot, getCapability/listCapabilities, routeWires, resolveBlockIcon, createLLMService, modelIntentCompiler, validateAllTemplates, ...) - Remove unreachable branches (create-intent destructive gate, polymarket/newsapi focus formatting, summaryOnly/timeWindow wire filtering) - Remove the write-only speech timing module and its plumbing - Remove unused aliases, getters, props, duplicate default exports, two unused index.ts barrels, a stale CSS variable and commented-out code - Make ~90 exports used only within their own file module-private Found per area and adversarially verified; typecheck, lint, tests and production build all pass. Co-Authored-By: Claude Opus 5.5 --- e2e/account-portal.spec.ts | 1 - scripts/lib/drafters.ts | 6 +- scripts/lib/verifySpec.ts | 2 +- src/app/CitadelApp.tsx | 3 +- src/app/globals.css | 1 - src/blocks/capability/CapabilityBlock.tsx | 4 +- src/blocks/capability/CapabilityInputs.tsx | 4 +- .../persona/PersonaBlock.component.test.tsx | 17 +-- src/blocks/persona/RunLineage.tsx | 2 +- src/blocks/persona/index.ts | 5 - src/blocks/truth/CoinGeckoBlock.ts | 4 +- src/blocks/truth/HackerNewsBlock.ts | 3 +- src/blocks/truth/OmniFeedBlock.ts | 6 +- src/blocks/truth/OpenAlexBlock.ts | 8 +- src/blocks/truth/PolymarketBlock.ts | 1 - src/blocks/truth/WorldBankBlock.ts | 8 +- src/canvas/routing/grid.ts | 2 +- src/canvas/routing/index.ts | 9 +- src/canvas/routing/nudge.ts | 2 +- src/canvas/routing/routing.test.ts | 40 ++--- src/canvas/routing/svgPath.ts | 4 +- src/components/ApiSearch.tsx | 2 +- src/components/Sidebar.tsx | 1 - .../apis/AddApiDialog.component.test.tsx | 27 ++-- src/components/apis/AddApiDialog.tsx | 2 +- src/components/apis/ApiReview.tsx | 6 +- src/components/apis/KeyField.tsx | 2 +- src/components/apis/SignIn.tsx | 2 +- src/components/apis/YourApis.tsx | 2 +- src/components/blockIcons.test.ts | 8 +- src/components/blockIcons.tsx | 7 - src/components/blocks/BlockCard.tsx | 3 +- src/components/blocks/BlockSetupCard.tsx | 4 +- src/components/blocks/CryptoView.tsx | 3 +- src/components/blocks/HNView.tsx | 3 +- src/components/blocks/MemoryBlock.tsx | 4 +- src/components/brand/Atmosphere.tsx | 2 +- src/components/mind/MemoryConfirmModal.tsx | 2 - src/components/mind/MindDock.tsx | 2 - src/components/mind/MindPanel.tsx | 3 - src/components/mind/ThinkResultModal.tsx | 2 - src/components/mind/index.ts | 5 - src/components/voice/VoiceControl.tsx | 2 - src/core/account/account.ts | 2 +- src/core/bundleScan.test.ts | 9 +- src/core/bundleScan.ts | 4 +- src/core/capabilities/apiIndex.ts | 6 +- src/core/capabilities/asyncRuntime.ts | 6 +- src/core/capabilities/boundary.test.ts | 2 +- src/core/capabilities/compatibility.ts | 7 +- src/core/capabilities/connectProviders.ts | 2 +- src/core/capabilities/credentialName.ts | 2 +- src/core/capabilities/egress.ts | 10 +- src/core/capabilities/execute.ts | 6 +- src/core/capabilities/executionLedger.ts | 8 +- src/core/capabilities/identity.ts | 4 +- src/core/capabilities/importAdmission.ts | 2 +- src/core/capabilities/jsonSchema.ts | 4 +- src/core/capabilities/jsonValue.ts | 2 +- src/core/capabilities/keyGuide.ts | 2 +- src/core/capabilities/manifest.ts | 64 ++++---- src/core/capabilities/openapi.ts | 4 +- src/core/capabilities/project.ts | 2 +- src/core/capabilities/registry.ts | 18 +-- src/core/capabilities/registryIntents.eval.ts | 2 +- src/core/capabilities/registryRanking.ts | 14 +- src/core/capabilities/registrySearch.eval.ts | 2 +- src/core/capabilities/registrySearch.test.ts | 9 +- src/core/capabilities/registrySearch.ts | 21 +-- src/core/capabilities/speech.test.ts | 10 +- src/core/capabilities/store.test.ts | 7 +- src/core/capabilities/store.ts | 2 +- src/core/capabilities/valueType.ts | 4 +- src/core/cognition/index.ts | 1 - src/core/cognition/kernel.ts | 6 +- src/core/gateway/ApiGateway.ts | 42 ----- src/core/gateway/omnidata.schema.ts | 6 +- src/core/hooks/useShellNavigation.ts | 1 - src/core/interaction/FIELD_REPORT.md | 2 +- src/core/interaction/browserSpeechAdapter.ts | 2 +- src/core/interaction/engine.ts | 3 +- src/core/interaction/fieldHarness.ts | 17 +-- src/core/interaction/intentCompiler.test.ts | 144 +++++------------- src/core/interaction/intentCompiler.ts | 72 +-------- src/core/interaction/mutationInventory.ts | 4 +- src/core/interaction/openaiRealtimeAdapter.ts | 14 +- src/core/interaction/proposal.ts | 14 +- src/core/interaction/pushToTalk.test.ts | 1 - src/core/interaction/pushToTalk.ts | 36 ++--- src/core/interaction/realtimeFakes.ts | 8 +- src/core/interaction/referent.ts | 2 +- src/core/interaction/scriptedSpeechAdapter.ts | 4 +- src/core/interaction/session.test.ts | 2 +- src/core/interaction/speechFeedback.ts | 4 - src/core/interaction/speechInput.ts | 23 +-- .../interaction/speechMeasurement.test.ts | 71 --------- src/core/interaction/speechMeasurement.ts | 124 --------------- src/core/interaction/speechObservation.ts | 8 +- src/core/interaction/types.ts | 1 - src/core/interaction/utterances.ts | 8 +- src/core/models.registry.ts | 2 +- src/core/registry/BlockRegistry.ts | 9 +- src/core/registry/ViewRegistry.tsx | 8 +- src/core/schemas/api.schema.test.ts | 2 +- src/core/schemas/api.schema.ts | 6 +- src/core/schemas/block.schema.ts | 31 +--- src/core/schemas/mind.schema.ts | 16 +- src/core/schemas/shell.schema.ts | 2 +- src/core/schemas/wire.schema.ts | 10 +- src/core/secrets.ts | 11 -- src/core/services/cascade.service.ts | 2 +- src/core/services/cascade.test.ts | 2 +- src/core/services/crystallize.service.ts | 4 +- src/core/services/llm.service.test.ts | 20 +-- src/core/services/llm.service.ts | 8 +- src/core/services/mind.engine.ts | 2 +- src/core/services/persona.engine.test.ts | 14 +- src/core/services/persona.engine.ts | 22 +-- src/core/services/persona.prompts.ts | 2 +- src/core/services/personaTurn.service.test.ts | 5 +- src/core/services/personaTurn.service.ts | 2 +- src/core/services/server/auth.ts | 2 +- src/core/services/server/capability.ledger.ts | 12 +- src/core/services/server/capabilityBroker.ts | 2 +- src/core/services/server/clientKey.ts | 2 +- src/core/services/server/dataPlant.ts | 4 +- src/core/services/server/inference.ledger.ts | 8 +- src/core/services/server/llm.adapters.ts | 4 +- src/core/services/server/oauthConnect.ts | 4 +- src/core/services/server/public.providers.ts | 10 +- src/core/services/server/realtimeSpeech.ts | 6 +- src/core/services/server/registryIntent.ts | 8 +- src/core/services/shell.snapshot.test.ts | 2 +- src/core/services/wire.service.ts | 142 +---------------- src/core/services/wireExtract.test.ts | 16 -- src/core/shells/templates.test.ts | 3 +- src/core/shells/templates.ts | 9 +- src/core/stores/hydrateValidation.test.ts | 41 +++-- src/core/stores/mindStore.ts | 57 +------ src/core/stores/shellStore.ts | 5 - src/core/stores/shellWorkflow.test.ts | 2 +- src/core/stores/toolStore.ts | 4 +- src/core/stores/wireStore.ts | 14 -- src/core/vault/hydration.ts | 28 +--- src/core/vault/index.ts | 3 +- src/core/vault/vaultExport.ts | 4 +- src/core/vault/vaultStorage.ts | 2 +- 147 files changed, 421 insertions(+), 1236 deletions(-) delete mode 100644 src/blocks/persona/index.ts delete mode 100644 src/components/mind/index.ts delete mode 100644 src/core/interaction/speechMeasurement.test.ts delete mode 100644 src/core/interaction/speechMeasurement.ts diff --git a/e2e/account-portal.spec.ts b/e2e/account-portal.spec.ts index 5f88e5e..52d48db 100644 --- a/e2e/account-portal.spec.ts +++ b/e2e/account-portal.spec.ts @@ -66,7 +66,6 @@ test('mobile account list recovers from failure and previews a named restore wit expect(box!.x).toBeGreaterThanOrEqual(0); expect(box!.x + box!.width).toBeLessThanOrEqual(375); } - await page.screenshot({ path: '/tmp/omni-account-ux-mobile.png' }); await page.keyboard.press('Escape'); await expect(preview).toHaveCount(0); await expect(account).toBeFocused(); diff --git a/scripts/lib/drafters.ts b/scripts/lib/drafters.ts index f7df533..78c6119 100644 --- a/scripts/lib/drafters.ts +++ b/scripts/lib/drafters.ts @@ -6,9 +6,9 @@ import Anthropic from '@anthropic-ai/sdk'; -export type DraftProvider = 'anthropic' | 'openai' | 'google'; +type DraftProvider = 'anthropic' | 'openai' | 'google'; -export const DEFAULT_MODELS: Record = { +const DEFAULT_MODELS: Record = { anthropic: 'claude-opus-5-5', openai: 'gpt-5', google: 'gemini-2.5-pro' @@ -22,7 +22,7 @@ const KEY_ENV: Record, string> = { const MAX_OUTPUT_TOKENS = 64_000; const REQUEST_TIMEOUT_MS = 10 * 60_000; -export interface DraftSession { +interface DraftSession { provider: DraftProvider; model: string; /** Send one user turn (the first carries the documents) and return the reply's text. */ diff --git a/scripts/lib/verifySpec.ts b/scripts/lib/verifySpec.ts index c800c41..a636311 100644 --- a/scripts/lib/verifySpec.ts +++ b/scripts/lib/verifySpec.ts @@ -21,7 +21,7 @@ export interface QualityResult { } /** Inputs from the spec itself: every required input's example, plus one-value constants. */ -export function sampleInput(manifest: CapabilityManifest): { ok: true; input: Record } | { ok: false; missing: string[] } { +function sampleInput(manifest: CapabilityManifest): { ok: true; input: Record } | { ok: false; missing: string[] } { const input: Record = {}; const missing: string[] = []; for (const field of manifest.inputs) { diff --git a/src/app/CitadelApp.tsx b/src/app/CitadelApp.tsx index f042ef5..ea160f9 100644 --- a/src/app/CitadelApp.tsx +++ b/src/app/CitadelApp.tsx @@ -10,7 +10,7 @@ import { Sidebar } from '@/components/Sidebar'; import { TopBar } from '@/components/TopBar'; import { CommandPalette } from '@/components/CommandPalette'; import { ShellPanel } from '@/components/ShellPanel'; -import { MindPanel } from '@/components/mind'; +import { MindPanel } from '@/components/mind/MindPanel'; import { MindDock } from '@/components/mind/MindDock'; import { SettingsPanel } from '@/components/settings/SettingsPanel'; import { ContextCaptureModal } from '@/components/mind/ContextCaptureModal'; @@ -59,7 +59,6 @@ export default function CitadelApp() { if (text && text.length > 0) { captureSelection({ text }); - // windowSelection?.removeAllRanges(); // Optional: clear selection } }, [activeTool, captureSelection]); return ( diff --git a/src/app/globals.css b/src/app/globals.css index 2e30856..2f9a684 100644 --- a/src/app/globals.css +++ b/src/app/globals.css @@ -25,7 +25,6 @@ --citadel-primary: #4890f0; --citadel-primary-glow: #9a6bff; --citadel-secondary: #90d8f0; - --citadel-accent: #f59be0; /* Truth Block Colors (Atlas status: success / danger / warning) */ --truth-green: #6ff5a8; diff --git a/src/blocks/capability/CapabilityBlock.tsx b/src/blocks/capability/CapabilityBlock.tsx index 963530c..f78c9b6 100644 --- a/src/blocks/capability/CapabilityBlock.tsx +++ b/src/blocks/capability/CapabilityBlock.tsx @@ -41,7 +41,7 @@ function isPlainParams(value: unknown): value is Record { } /** The executor's error for a keyed run whose slot is empty this session. */ -export function isUnboundKeyError(error: string): boolean { +function isUnboundKeyError(error: string): boolean { return /^Secret slot \S+ is empty$/.test(error); } @@ -54,7 +54,7 @@ function isRefusal(error: string): boolean { } /** What an HTTP status usually means for the person running the block. */ -export function errorHint(error: string): string | null { +function errorHint(error: string): string | null { const status = /\bHTTP (\d{3})\b/.exec(error)?.[1]; if (status === '401' || status === '403') return 'The API refused the request. Check the key, or whether your plan covers this call.'; if (status === '400') return 'The API rejected the request. Check the inputs, and the key if it takes one.'; diff --git a/src/blocks/capability/CapabilityInputs.tsx b/src/blocks/capability/CapabilityInputs.tsx index f0a1e17..a69bd60 100644 --- a/src/blocks/capability/CapabilityInputs.tsx +++ b/src/blocks/capability/CapabilityInputs.tsx @@ -5,8 +5,6 @@ import { looksLikeKey } from '@/core/capabilities/credentialName'; import type { ValueType } from '@/core/capabilities/valueType'; import { isPlainList } from '@/core/capabilities/httpTarget'; -export { looksLikeKey }; - /** What one field holds while being edited: text, or a checkbox's boolean. */ type FieldDraft = string | boolean; /** A body laid out as fields keeps one draft per property. */ @@ -294,7 +292,7 @@ function Field({ input, idPrefix, value, wiredValue, onChange, disabled }: { ); } -export interface CapabilityInputsProps { +interface CapabilityInputsProps { inputs: readonly CapabilityInput[]; params: Record; /** Values arriving on wires. A typed value overrides them. */ diff --git a/src/blocks/persona/PersonaBlock.component.test.tsx b/src/blocks/persona/PersonaBlock.component.test.tsx index f36ca73..dda176e 100644 --- a/src/blocks/persona/PersonaBlock.component.test.tsx +++ b/src/blocks/persona/PersonaBlock.component.test.tsx @@ -62,7 +62,6 @@ describe('PersonaBlockView — UI wiring', () => { mockStream(['Markets look ', 'stable today.'], { success: true, content: 'Markets look stable today.', - sourceIds: [], sources: [] }); render(); @@ -79,7 +78,6 @@ describe('PersonaBlockView — UI wiring', () => { mockStream(['Grounded ', 'answer.'], { success: true, content: 'Grounded answer.', - sourceIds: [], sources: [] }); render(); @@ -99,7 +97,6 @@ describe('PersonaBlockView — UI wiring', () => { it('engine failure surfaces a clear in-chat warning (no fake answer)', async () => { mockStream([], { success: false, - sourceIds: [], sources: [], error: 'No LLM available — make sure Ollama is running (localhost:11434).' }); @@ -124,7 +121,6 @@ describe('PersonaBlockView — provenance', () => { mockStream(['Grounded.'], { success: true, content: 'Grounded.', - sourceIds: ['src-fred'], sources: [{ id: 'src-fred', kind: 'wire', label: 'FRED Series' }] }); render(); @@ -138,7 +134,6 @@ describe('PersonaBlockView — provenance', () => { mockStream(['ok'], { success: true, content: 'ok', - sourceIds: ['src-fred'], sources: [{ id: 'src-fred', kind: 'wire', label: 'FRED Series' }] }); render(); @@ -158,7 +153,6 @@ describe('PersonaBlockView — provenance', () => { mockStream(['from memory'], { success: true, content: 'from memory', - sourceIds: [], sources: [{ id: 'mem-block-1', kind: 'memory', label: 'Long-term memory' }] }); render(); @@ -172,7 +166,6 @@ describe('PersonaBlockView — provenance', () => { mockStream(['general reasoning'], { success: true, content: 'general reasoning', - sourceIds: [], sources: [] }); render(); @@ -194,7 +187,7 @@ describe('PersonaBlockView — answer rendering', () => { const md = '## Outlook' + String.fromCharCode(10) + String.fromCharCode(10) + 'Rates are **steady**.' + String.fromCharCode(10) + String.fromCharCode(10) + '- 10y near 4.2%' + String.fromCharCode(10) + '- CPI cooling'; - mockStream([md], { success: true, content: md, sourceIds: [], sources: [] }); + mockStream([md], { success: true, content: md, sources: [] }); render(); fireEvent.click(screen.getByTitle('Think')); @@ -209,7 +202,7 @@ describe('PersonaBlockView — answer rendering', () => { it('does not execute HTML embedded in an answer', async () => { // Answers can quote text that arrived from an external feed. const hostile = 'Look: '; - mockStream([hostile], { success: true, content: hostile, sourceIds: [], sources: [] }); + mockStream([hostile], { success: true, content: hostile, sources: [] }); const { container } = render(); fireEvent.click(screen.getByTitle('Think')); @@ -218,7 +211,7 @@ describe('PersonaBlockView — answer rendering', () => { }); it('offers lineage only on an answer the ledger recorded', async () => { - mockStream(['Recorded.'], { success: true, content: 'Recorded.', sourceIds: [], sources: [], runId: '42' }); + mockStream(['Recorded.'], { success: true, content: 'Recorded.', sources: [], runId: '42' }); render(); fireEvent.click(screen.getByTitle('Think')); @@ -227,7 +220,7 @@ describe('PersonaBlockView — answer rendering', () => { }); it('offers no lineage when no run was recorded', async () => { - mockStream(['Unrecorded.'], { success: true, content: 'Unrecorded.', sourceIds: [], sources: [] }); + mockStream(['Unrecorded.'], { success: true, content: 'Unrecorded.', sources: [] }); render(); fireEvent.click(screen.getByTitle('Think')); @@ -236,7 +229,7 @@ describe('PersonaBlockView — answer rendering', () => { }); it("leaves the user's own message as literal text", async () => { - mockStream(['ok'], { success: true, content: 'ok', sourceIds: [], sources: [] }); + mockStream(['ok'], { success: true, content: 'ok', sources: [] }); render(); const input = screen.getByPlaceholderText(/Ask Analyst/); diff --git a/src/blocks/persona/RunLineage.tsx b/src/blocks/persona/RunLineage.tsx index 58ee37a..1a7810c 100644 --- a/src/blocks/persona/RunLineage.tsx +++ b/src/blocks/persona/RunLineage.tsx @@ -27,7 +27,7 @@ const NOT_RECORDED = 'The inference ledger is not configured here, so no lineage was recorded for this answer.'; /** Map every non-tree answer to one honest sentence. */ -export async function fetchLineage(runId: string): Promise { +async function fetchLineage(runId: string): Promise { let res: Response; try { res = await fetch(`/api/inference-runs/${encodeURIComponent(runId)}/lineage`, { cache: 'no-store' }); diff --git a/src/blocks/persona/index.ts b/src/blocks/persona/index.ts deleted file mode 100644 index a214406..0000000 --- a/src/blocks/persona/index.ts +++ /dev/null @@ -1,5 +0,0 @@ -// ============================================ -// PROJECT OMNI: PERSONA BLOCKS INDEX -// ============================================ - -export { PersonaBlockView } from './PersonaBlock'; diff --git a/src/blocks/truth/CoinGeckoBlock.ts b/src/blocks/truth/CoinGeckoBlock.ts index ca93957..a77dbff 100644 --- a/src/blocks/truth/CoinGeckoBlock.ts +++ b/src/blocks/truth/CoinGeckoBlock.ts @@ -11,7 +11,7 @@ import { debug } from '@/core/debug'; /** * CoinGecko cryptocurrency data for block display */ -export interface CryptoAsset { +interface CryptoAsset { id: string; symbol: string; name: string; @@ -26,7 +26,6 @@ export interface CryptoAsset { high24h: number; low24h: number; trend: 'up' | 'down' | 'neutral'; - sparkline?: number[]; } /** @@ -122,7 +121,6 @@ export function useCoinGeckoBlock(instanceId: string) { assets: cachedData.length > 0 ? cachedData : assets, status: block?.status || (isLoading ? 'connecting' : 'connected'), lastUpdated: block?.last_updated, - fromCache, refresh, error }; diff --git a/src/blocks/truth/HackerNewsBlock.ts b/src/blocks/truth/HackerNewsBlock.ts index 6096384..3b23b50 100644 --- a/src/blocks/truth/HackerNewsBlock.ts +++ b/src/blocks/truth/HackerNewsBlock.ts @@ -11,7 +11,7 @@ import { debug } from '@/core/debug'; /** * Hacker News story for block display */ -export interface HNStory { +interface HNStory { id: string; title: string; url: string; @@ -111,7 +111,6 @@ export function useHackerNewsBlock(instanceId: string) { stories: cachedData.length > 0 ? cachedData : stories, status: block?.status || (isLoading ? 'connecting' : 'connected'), lastUpdated: block?.last_updated, - fromCache, refresh, error }; diff --git a/src/blocks/truth/OmniFeedBlock.ts b/src/blocks/truth/OmniFeedBlock.ts index a1b149c..9c39f9e 100644 --- a/src/blocks/truth/OmniFeedBlock.ts +++ b/src/blocks/truth/OmniFeedBlock.ts @@ -9,7 +9,7 @@ import { useBlockStore, useSettingsStore } from '@/core/stores'; import { useOmniData } from '@/core/hooks'; import type { OmniItem } from '@/core/gateway'; -export interface OmniFeedBlockData { +interface OmniFeedBlockData { items: OmniItem[]; } @@ -26,8 +26,7 @@ export function useOmniFeedBlock( items, isLoading, error, - refresh, - fromCache + refresh } = useOmniData(apiId, instanceId, { immediate: Boolean(apiId), params, @@ -59,7 +58,6 @@ export function useOmniFeedBlock( items: cached?.length ? cached : items, status: block?.status || (isLoading ? 'connecting' : 'connected'), lastUpdated: block?.last_updated, - fromCache, refresh, error }; diff --git a/src/blocks/truth/OpenAlexBlock.ts b/src/blocks/truth/OpenAlexBlock.ts index c018c90..617e96c 100644 --- a/src/blocks/truth/OpenAlexBlock.ts +++ b/src/blocks/truth/OpenAlexBlock.ts @@ -8,11 +8,11 @@ import { useBlockStore, useSettingsStore } from '@/core/stores'; import { useOmniData } from '@/core/hooks'; import type { OmniItem } from '@/core/gateway'; -export interface OpenAlexBlockData { +interface OpenAlexBlockData { items: OmniItem[]; } -export type OpenAlexBlockParams = Record; +type OpenAlexBlockParams = Record; export function useOpenAlexBlock(instanceId: string, params?: OpenAlexBlockParams) { const { updateData, updateStatus, getBlock } = useBlockStore(); @@ -23,8 +23,7 @@ export function useOpenAlexBlock(instanceId: string, params?: OpenAlexBlockParam items, isLoading, error, - refresh, - fromCache + refresh } = useOmniData('openalex', instanceId, { immediate: true, params, @@ -57,7 +56,6 @@ export function useOpenAlexBlock(instanceId: string, params?: OpenAlexBlockParam items: cachedData?.items?.length ? cachedData.items : items, status: block?.status || (isLoading ? 'connecting' : 'connected'), lastUpdated: block?.last_updated, - fromCache, refresh, error }; diff --git a/src/blocks/truth/PolymarketBlock.ts b/src/blocks/truth/PolymarketBlock.ts index dcef5e9..ddb6871 100644 --- a/src/blocks/truth/PolymarketBlock.ts +++ b/src/blocks/truth/PolymarketBlock.ts @@ -109,7 +109,6 @@ export function usePolymarketBlock(instanceId: string) { markets: cachedData.length > 0 ? cachedData : markets, status: block?.status || (isLoading ? 'connecting' : 'connected'), lastUpdated: block?.last_updated, - fromCache, refresh, pause: () => { }, // Polling handled by useOmniData resume: refresh, diff --git a/src/blocks/truth/WorldBankBlock.ts b/src/blocks/truth/WorldBankBlock.ts index e272eef..431078d 100644 --- a/src/blocks/truth/WorldBankBlock.ts +++ b/src/blocks/truth/WorldBankBlock.ts @@ -8,12 +8,12 @@ import { useBlockStore, useSettingsStore } from '@/core/stores'; import { useOmniData } from '@/core/hooks'; import type { OmniItem, OmniMetrics } from '@/core/gateway'; -export interface WorldBankBlockData { +interface WorldBankBlockData { items: OmniItem[]; metrics: OmniMetrics | null; } -export interface WorldBankBlockParams { +interface WorldBankBlockParams { indicator?: string; country?: string; per_page?: number; @@ -32,8 +32,7 @@ export function useWorldBankBlock(instanceId: string, params?: WorldBankBlockPar items, isLoading, error, - refresh, - fromCache + refresh } = useOmniData('worldbank', instanceId, { immediate: true, params, @@ -69,7 +68,6 @@ export function useWorldBankBlock(instanceId: string, params?: WorldBankBlockPar metrics: cachedData?.metrics || metrics, status: block?.status || (isLoading ? 'connecting' : 'connected'), lastUpdated: block?.last_updated, - fromCache, refresh, error }; diff --git a/src/canvas/routing/grid.ts b/src/canvas/routing/grid.ts index 4e8dcfd..b9bbed7 100644 --- a/src/canvas/routing/grid.ts +++ b/src/canvas/routing/grid.ts @@ -290,7 +290,7 @@ const EAST = 0; const WEST = 1; const SOUTH = 2; -export interface SearchCosts { +interface SearchCosts { bend: number; cross: number; share: number; diff --git a/src/canvas/routing/index.ts b/src/canvas/routing/index.ts index 00c6a1d..685eef1 100644 --- a/src/canvas/routing/index.ts +++ b/src/canvas/routing/index.ts @@ -1,6 +1,6 @@ // Circuit-style wire routing for the canvas. // -// routeWires(blocks, wires) -> one orthogonal polyline per wire +// new WireRouter().route(blocks, wires) -> one orthogonal polyline per wire // // 1. Every block is inflated by `clearance` and becomes an obstacle (a wire's // own source and target blocks too: only the port stub touches them). @@ -32,7 +32,7 @@ import { } from './types'; export * from './types'; -export { polylineToPath, midpoint, DEFAULT_PATH_STYLE } from './svgPath'; +export { polylineToPath, midpoint } from './svgPath'; const EPS = 1e-6; @@ -429,11 +429,6 @@ function routeCore( }; } -/** Route every wire from scratch. Deterministic: the same layout always gives the same wires. */ -export function routeWires(blocks: RouteBlock[], wires: RouteWire[], options: Partial = {}): RouteResult { - return routeCore(blocks, wires, { ...DEFAULT_ROUTE_OPTIONS, ...options }, () => undefined).result; -} - const LIVE_GREED = 2; /** Cost of using another net's port stub: about ten bends, so only a last resort. */ diff --git a/src/canvas/routing/nudge.ts b/src/canvas/routing/nudge.ts index 9208c96..a43bedf 100644 --- a/src/canvas/routing/nudge.ts +++ b/src/canvas/routing/nudge.ts @@ -8,7 +8,7 @@ import type { Point, RouteBlock, RouteOptions } from './types'; -export interface NetPolyline { +interface NetPolyline { points: Point[]; /** Net of each segment (points[k] -> points[k + 1]). Same net = same lane. */ nets: string[]; diff --git a/src/canvas/routing/routing.test.ts b/src/canvas/routing/routing.test.ts index b66b297..3bc3f93 100644 --- a/src/canvas/routing/routing.test.ts +++ b/src/canvas/routing/routing.test.ts @@ -1,5 +1,5 @@ import { describe, it, expect } from 'vitest'; -import { routeWires, WireRouter, segmentHits, splitPoint, polylineToPath, DEFAULT_ROUTE_OPTIONS } from './index'; +import { WireRouter, segmentHits, splitPoint, polylineToPath, DEFAULT_ROUTE_OPTIONS } from './index'; import type { Point, RouteBlock, RouteWire, RoutedWire } from './types'; const EPS = 1e-6; @@ -83,13 +83,13 @@ function blockHits(blocks: RouteBlock[], w: RoutedWire): string[] { return hits; } -describe('routeWires: circuit-style routing', () => { +describe('WireRouter.route: circuit-style routing', () => { it('leaves the output port and enters the input port horizontally, heading east', () => { const blocks: RouteBlock[] = [ { id: 'a', x: 0, y: 0, width: 200, height: 100 }, { id: 'b', x: 400, y: 300, width: 200, height: 100 } ]; - const [w] = routeWires(blocks, [{ id: 'w', source: 'a', target: 'b' }]).wires; + const [w] = new WireRouter().route(blocks, [{ id: 'w', source: 'a', target: 'b' }]).wires; const p = w.points; const o = DEFAULT_ROUTE_OPTIONS; expect(p[0]).toEqual({ x: 200 + o.portOutset, y: 50 }); @@ -109,7 +109,7 @@ describe('routeWires: circuit-style routing', () => { { id: 'wall', x: 320, y: 0, width: 200, height: 300 }, { id: 'b', x: 640, y: 100, width: 200, height: 100 } ]; - const [w] = routeWires(blocks, [{ id: 'w', source: 'a', target: 'b' }]).wires; + const [w] = new WireRouter().route(blocks, [{ id: 'w', source: 'a', target: 'b' }]).wires; expect(blockHits(blocks, w)).toEqual([]); // It keeps the clearance from the wall, not just the wall itself. const wall = blocks[1]; @@ -123,7 +123,7 @@ describe('routeWires: circuit-style routing', () => { { id: 'a', x: 500, y: 0, width: 200, height: 100 }, { id: 'b', x: 0, y: 40, width: 200, height: 100 } ]; - const [w] = routeWires(blocks, [{ id: 'w', source: 'a', target: 'b' }]).wires; + const [w] = new WireRouter().route(blocks, [{ id: 'w', source: 'a', target: 'b' }]).wires; expect(w.routed).toBe(true); expect(blockHits(blocks, w)).toEqual([]); expectOrthogonal(w); @@ -132,7 +132,7 @@ describe('routeWires: circuit-style routing', () => { it('never runs a wire through any block on random layouts (property)', () => { for (let seed = 1; seed <= 120; seed++) { const { blocks, wires } = layout(seed, 12 + (seed % 10), 18 + (seed % 15), 4 + (seed % 3)); - const res = routeWires(blocks, wires); + const res = new WireRouter().route(blocks, wires); expect(res.wires).toHaveLength(wires.length); for (const w of res.wires) { expect(w.routed, `seed ${seed} wire ${w.id} found no path`).toBe(true); @@ -145,12 +145,12 @@ describe('routeWires: circuit-style routing', () => { it('is deterministic: the same layout gives the same wires, whatever the input order', () => { const { blocks, wires } = layout(7, 20, 30); - const a = routeWires(blocks, wires); - const b = routeWires(blocks, wires); + const a = new WireRouter().route(blocks, wires); + const b = new WireRouter().route(blocks, wires); expect(b.wires).toEqual(a.wires); expect(b.junctions).toEqual(a.junctions); - const shuffled = routeWires([...blocks].reverse(), [...wires].reverse()); + const shuffled = new WireRouter().route([...blocks].reverse(), [...wires].reverse()); const byId = new Map(shuffled.wires.map(w => [w.id, w])); for (const w of a.wires) expect(byId.get(w.id)).toEqual(w); }); @@ -158,7 +158,7 @@ describe('routeWires: circuit-style routing', () => { it('spreads parallel runs of different outputs into lanes that never overlap', () => { let nearPort = 0; const check = (blocks: RouteBlock[], wires: RouteWire[], label: string) => { - const res = routeWires(blocks, wires); + const res = new WireRouter().route(blocks, wires); type Seg = { w: RoutedWire; a: Point; b: Point }; // A port jog (the short step a moved lane takes onto its port row, // within `mergeJog` of the port) is where lanes merge; it is the one @@ -213,7 +213,7 @@ describe('routeWires: circuit-style routing', () => { it('fans seven sources into one input along one trunk, with junction dots', () => { const { blocks, wires } = fanIn(); - const res = routeWires(blocks, wires); + const res = new WireRouter().route(blocks, wires); for (const w of res.wires) expect(blockHits(blocks, w)).toEqual([]); // Every wire reaches the port on the same final run: the trunk. const lastVertical = res.wires.map(w => w.points[w.points.length - 3]?.x); @@ -230,7 +230,7 @@ describe('routeWires: circuit-style routing', () => { { id: 'c', x: 200, y: 400, width: 100, height: 100 }, { id: 'b', x: 450, y: -300, width: 100, height: 100 } ]; - const res = routeWires(blocks, [ + const res = new WireRouter().route(blocks, [ { id: 'h', source: 'a', target: 'd' }, { id: 'v', source: 'c', target: 'b' } ]); @@ -246,7 +246,7 @@ describe('routeWires: circuit-style routing', () => { { id: 'over', x: 150, y: -20, width: 200, height: 140 }, { id: 'b', x: 600, y: 0, width: 200, height: 100 } ]; - const [w] = routeWires(blocks, [{ id: 'w', source: 'a', target: 'b' }]).wires; + const [w] = new WireRouter().route(blocks, [{ id: 'w', source: 'a', target: 'b' }]).wires; expectOrthogonal(w); expect(w.points[0].x).toBeCloseTo(216); }); @@ -258,13 +258,13 @@ describe('routeWires: circuit-style routing', () => { { id: 'n', x: 220, y: 0, width: 200, height: 400 }, { id: 'b', x: 700, y: 100, width: 200, height: 100 } ]; - const [w] = routeWires(blocks, [{ id: 'w', source: 'a', target: 'b' }]).wires; + const [w] = new WireRouter().route(blocks, [{ id: 'w', source: 'a', target: 'b' }]).wires; expect(blockHits(blocks, w)).toEqual([]); expectOrthogonal(w); }); it('skips wires whose blocks are missing', () => { - const res = routeWires([{ id: 'a', x: 0, y: 0, width: 10, height: 10 }], [{ id: 'w', source: 'a', target: 'gone' }]); + const res = new WireRouter().route([{ id: 'a', x: 0, y: 0, width: 10, height: 10 }], [{ id: 'w', source: 'a', target: 'gone' }]); expect(res.wires).toEqual([]); }); }); @@ -294,7 +294,7 @@ describe('WireRouter: live drag', () => { router.route(blocks, wires); const moved = blocks.map(b => (b.id === 'hn' ? { ...b, y: b.y + 80 } : b)); router.routeLive(moved, wires, 'hn'); - expect(router.route(moved, wires).wires).toEqual(routeWires(moved, wires).wires); + expect(router.route(moved, wires).wires).toEqual(new WireRouter().route(moved, wires).wires); }); }); @@ -309,11 +309,11 @@ describe('routing performance budget', () => { it('routes the screenshot fan-in layout well inside one frame', () => { const { blocks, wires } = fanIn(); - routeWires(blocks, wires); + new WireRouter().route(blocks, wires); const times: number[] = []; for (let i = 0; i < 20; i++) { const t0 = performance.now(); - routeWires(blocks, wires); + new WireRouter().route(blocks, wires); times.push(performance.now() - t0); } expect(median(times)).toBeLessThan(8); @@ -321,11 +321,11 @@ describe('routing performance budget', () => { it('routes 30 blocks / 60 wires within budget, and a live drag frame faster', () => { const { blocks, wires } = layout(42, 30, 60); - routeWires(blocks, wires); + new WireRouter().route(blocks, wires); const full: number[] = []; for (let i = 0; i < 5; i++) { const t0 = performance.now(); - routeWires(blocks, wires); + new WireRouter().route(blocks, wires); full.push(performance.now() - t0); } const router = new WireRouter(); diff --git a/src/canvas/routing/svgPath.ts b/src/canvas/routing/svgPath.ts index 0f3dfb9..0654cc8 100644 --- a/src/canvas/routing/svgPath.ts +++ b/src/canvas/routing/svgPath.ts @@ -3,12 +3,12 @@ import type { Point } from './types'; -export interface PathStyle { +interface PathStyle { cornerRadius: number; hopRadius: number; } -export const DEFAULT_PATH_STYLE: PathStyle = { cornerRadius: 6, hopRadius: 4 }; +const DEFAULT_PATH_STYLE: PathStyle = { cornerRadius: 6, hopRadius: 4 }; const EPS = 1e-6; diff --git a/src/components/ApiSearch.tsx b/src/components/ApiSearch.tsx index 3152aa9..0575750 100644 --- a/src/components/ApiSearch.tsx +++ b/src/components/ApiSearch.tsx @@ -44,7 +44,7 @@ function shortDate(updated: string): string { return updated ? updated.slice(0, 7) : 'date unknown'; } -export interface ApiSearchProps { +interface ApiSearchProps { /** The entry currently open in the review, if any. */ selectedId?: string | null; /** A supported entry was chosen. The caller fetches and reviews it. */ diff --git a/src/components/Sidebar.tsx b/src/components/Sidebar.tsx index 6b8f501..8786340 100644 --- a/src/components/Sidebar.tsx +++ b/src/components/Sidebar.tsx @@ -167,7 +167,6 @@ export function Sidebar() { {blocks.map(block => ( diff --git a/src/components/apis/AddApiDialog.component.test.tsx b/src/components/apis/AddApiDialog.component.test.tsx index 6f9dd3d..1ea8e6e 100644 --- a/src/components/apis/AddApiDialog.component.test.tsx +++ b/src/components/apis/AddApiDialog.component.test.tsx @@ -5,7 +5,8 @@ import { useState } from 'react'; import { AddApiDialog } from './AddApiDialog'; import type { ApiIndex, ApiIndexEntry } from '@/core/capabilities/apiIndex'; import { compileOpenApi } from '@/core/capabilities/openapi'; -import { clearCapabilities, installProposal, listCapabilities } from '@/core/capabilities/registry'; +import { clearCapabilities, installProposal } from '@/core/capabilities/registry'; +import { allCapabilities } from '@/core/capabilities/state'; import { MAX_SECRET_BYTES, capabilitySecrets } from '@/core/capabilities/secrets'; import { useCapabilityStore } from '@/core/capabilities/store'; import { useBlockStore } from '@/core/stores/blockStore'; @@ -126,7 +127,7 @@ describe('AddApiDialog: a curated API (FRED)', () => { expect(screen.getByText(/does not look like the documented key format \(32 lowercase letters and digits\)/)).toBeTruthy(); fireEvent.click(screen.getByRole('button', { name: 'Install & place' })); - const installed = listCapabilities().filter(m => m.source.locator === 'omni:curated/fred'); + const installed = allCapabilities().filter(m => m.source.locator === 'omni:curated/fred'); expect(installed).toHaveLength(3); for (const manifest of installed) expect(manifest.transport.kind === 'http' && manifest.transport.access).toBe('server_broker'); expect(placedCapabilityBlocks()[0].schema.display_name).toBe('Values of an economic series over time'); @@ -161,7 +162,7 @@ describe('AddApiDialog: from search to the canvas', () => { fireEvent.click(screen.getByRole('button', { name: 'Install & place' })); - expect(listCapabilities().filter(m => m.source.locator === INDEX.entries[0].specUrl)).toHaveLength(2); + expect(allCapabilities().filter(m => m.source.locator === INDEX.entries[0].specUrl)).toHaveLength(2); const placed = placedCapabilityBlocks(); expect(placed).toHaveLength(1); expect(placed[0].schema.display_name).toBe('Forecast timeline'); @@ -184,7 +185,7 @@ describe('AddApiDialog: from search to the canvas', () => { fireEvent.click(await screen.findByLabelText('Install Forecast timeline')); expect(screen.getByText('Installs 1. Nothing is placed on the canvas.')).toBeTruthy(); fireEvent.click(screen.getByRole('button', { name: 'Install' })); - expect(listCapabilities().map(m => m.title)).toEqual(['Weather history']); + expect(allCapabilities().map(m => m.title)).toEqual(['Weather history']); expect(placedCapabilityBlocks()).toHaveLength(0); }); @@ -216,7 +217,7 @@ describe('AddApiDialog: keys and approval', () => { fireEvent.change(screen.getByLabelText('OpenAPI document'), { target: { value: '{not json' } }); fireEvent.click(screen.getByRole('button', { name: 'Review' })); expect(screen.getByText('OpenAPI document must be JSON')).toBeTruthy(); - expect(listCapabilities()).toHaveLength(0); + expect(allCapabilities()).toHaveLength(0); }); it('installs without writing the key into the store; a write lands pending', () => { @@ -225,8 +226,8 @@ describe('AddApiDialog: keys and approval', () => { fireEvent.change(screen.getByLabelText('Key for https://board.example.test'), { target: { value: 'super-secret-value' } }); fireEvent.click(screen.getByRole('button', { name: 'Install & place' })); - const list = listCapabilities().find(m => m.source.operationId === 'listPosts'); - const create = listCapabilities().find(m => m.source.operationId === 'createPost'); + const list = allCapabilities().find(m => m.source.operationId === 'listPosts'); + const create = allCapabilities().find(m => m.source.operationId === 'createPost'); expect(list?.approval).toBe('auto'); expect(create?.approval).toBe('pending'); expect(capabilitySecrets.get(list!.auth.secretRef!)).toBe('super-secret-value'); @@ -239,7 +240,7 @@ describe('AddApiDialog: keys and approval', () => { paste(SPEC); fireEvent.click(screen.getByRole('button', { name: 'Install & place' })); expect(screen.getByRole('alert').textContent).toBe('Enter the key for https://board.example.test'); - expect(listCapabilities()).toHaveLength(0); + expect(allCapabilities()).toHaveLength(0); expect(placedCapabilityBlocks()).toHaveLength(0); }); @@ -249,7 +250,7 @@ describe('AddApiDialog: keys and approval', () => { fireEvent.change(screen.getByLabelText('Key for https://board.example.test'), { target: { value: 'k'.repeat(MAX_SECRET_BYTES + 1) } }); fireEvent.click(screen.getByRole('button', { name: 'Install & place' })); expect(screen.getByRole('alert').textContent).toBe(`A key is at most ${MAX_SECRET_BYTES} bytes`); - expect(listCapabilities()).toHaveLength(0); + expect(allCapabilities()).toHaveLength(0); const ref = compileOpenApi(SPEC).manifests[0].auth.secretRef!; expect(capabilitySecrets.get(ref)).toBeUndefined(); }); @@ -310,8 +311,8 @@ describe('AddApiDialog: keys and approval', () => { fireEvent.click(screen.getByLabelText('Install List posts')); // Unselecting the operation marked for the canvas leaves nothing to place. fireEvent.click(screen.getByRole('button', { name: 'Install' })); - expect(listCapabilities().map(m => m.source.operationId)).toEqual(['createPost']); - expect(listCapabilities()[0].approval).toBe('pending'); + expect(allCapabilities().map(m => m.source.operationId)).toEqual(['createPost']); + expect(allCapabilities()[0].approval).toBe('pending'); }); }); @@ -329,7 +330,7 @@ describe('AddApiDialog: Your APIs', () => { const list = screen.getByRole('list', { name: 'Installed APIs' }); expect(within(list).getByText('Waiting for approval')).toBeTruthy(); fireEvent.click(screen.getByRole('button', { name: 'Approve Create post' })); - expect(listCapabilities().find(m => m.source.operationId === 'createPost')?.approval).toBe('approved'); + expect(allCapabilities().find(m => m.source.operationId === 'createPost')?.approval).toBe('approved'); expect(screen.queryByRole('button', { name: 'Approve Create post' })).toBeNull(); }); @@ -338,7 +339,7 @@ describe('AddApiDialog: Your APIs', () => { act(() => installCreatePost()); fireEvent.click(screen.getByRole('tab', { name: /Your APIs/ })); fireEvent.click(screen.getByRole('button', { name: 'Remove Create post' })); - expect(listCapabilities()).toHaveLength(0); + expect(allCapabilities()).toHaveLength(0); }); it('names a capability the current rules could not restore, and lets you forget it', () => { diff --git a/src/components/apis/AddApiDialog.tsx b/src/components/apis/AddApiDialog.tsx index 27e691e..412f147 100644 --- a/src/components/apis/AddApiDialog.tsx +++ b/src/components/apis/AddApiDialog.tsx @@ -52,7 +52,7 @@ type Review = | { state: 'failed'; message: string } | { state: 'ready'; title: string; subtitle: string; proposals: CapabilityManifest[]; issues: string[] }; -export interface AddApiDialogProps { +interface AddApiDialogProps { open: boolean; onClose: () => void; } diff --git a/src/components/apis/ApiReview.tsx b/src/components/apis/ApiReview.tsx index 5556f24..00df80c 100644 --- a/src/components/apis/ApiReview.tsx +++ b/src/components/apis/ApiReview.tsx @@ -3,7 +3,7 @@ import { credentialPlacement } from '@/core/capabilities/execute'; import type { CapabilityEffect, CapabilityManifest } from '@/core/capabilities/manifest'; -export const EFFECT_STYLE: Record = { +const EFFECT_STYLE: Record = { read: { label: 'read', className: 'text-[var(--truth-green)] border-[var(--truth-green)]/40' }, compute: { label: 'compute', className: 'text-[var(--truth-green)] border-[var(--truth-green)]/40' }, write: { label: 'write · needs approval', className: 'text-[var(--truth-amber)] border-[var(--truth-amber)]/40' }, @@ -33,7 +33,7 @@ export function operationParts(manifest: CapabilityManifest): { method: string; } /** `city, state (optional: units)`: what you will be asked for before a run. */ -export function inputSummary(manifest: CapabilityManifest): string { +function inputSummary(manifest: CapabilityManifest): string { // A one-value input (FRED's file_type) is filled in, not asked for. const asked = manifest.inputs.filter(input => !(input.required && input.schema.enum?.length === 1)); const required = asked.filter(input => input.required).map(input => input.name); @@ -56,7 +56,7 @@ export function destinationOf(manifest: CapabilityManifest): { origin: string; c }; } -export interface ApiReviewProps { +interface ApiReviewProps { title: string; /** `spec updated 2023-03-06` or a paste note. */ subtitle: string; diff --git a/src/components/apis/KeyField.tsx b/src/components/apis/KeyField.tsx index eac2761..0b756dd 100644 --- a/src/components/apis/KeyField.tsx +++ b/src/components/apis/KeyField.tsx @@ -30,7 +30,7 @@ export function useHouseKeyCovers(manifest: CapabilityManifest | undefined): boo return useSyncExternalStore(subscribeHouseKeys, () => houseKeyCovers(manifest), () => false); } -export interface KeyFieldProps { +interface KeyFieldProps { manifest: CapabilityManifest; /** Called after the key is in the slot. */ onSaved?: () => void; diff --git a/src/components/apis/SignIn.tsx b/src/components/apis/SignIn.tsx index 43edd4f..abb0cff 100644 --- a/src/components/apis/SignIn.tsx +++ b/src/components/apis/SignIn.tsx @@ -5,7 +5,7 @@ import { connectSignIn, isConnectConfigured, signInSlotOf, subscribeConnect, typ import type { CapabilityManifest } from '@/core/capabilities/manifest'; import { capabilitySecrets } from '@/core/capabilities/secrets'; -export interface SignInState extends SignInSlot { +interface SignInState extends SignInSlot { signedIn: boolean; /** Whether this server has an OAuth client for the provider. */ configured: boolean; diff --git a/src/components/apis/YourApis.tsx b/src/components/apis/YourApis.tsx index d1dda50..4912d4b 100644 --- a/src/components/apis/YourApis.tsx +++ b/src/components/apis/YourApis.tsx @@ -59,7 +59,7 @@ export function isUserInstalled(manifest: CapabilityManifest): boolean { return manifest.source.locator !== 'speech'; } -export interface YourApisProps { +interface YourApisProps { /** Put an installed operation on the canvas again. */ onPlace: (manifest: CapabilityManifest) => void; } diff --git a/src/components/blockIcons.test.ts b/src/components/blockIcons.test.ts index 6336872..fbb84c3 100644 --- a/src/components/blockIcons.test.ts +++ b/src/components/blockIcons.test.ts @@ -1,8 +1,7 @@ import { describe, it, expect } from 'vitest'; -import { BLOCK_ICON_COMPONENTS, resolveBlockIcon } from './blockIcons'; +import { BLOCK_ICON_COMPONENTS } from './blockIcons'; import { API_CATALOG } from '@/core/schemas/api.schema'; import { blockRegistry } from '@/core/registry/BlockRegistry'; -import { Activity } from 'lucide-react'; describe('block icons — every catalog API has a real UI icon', () => { it('maps every keyless block icon so Armory and the palette do not fall back', () => { @@ -16,9 +15,4 @@ describe('block icons — every catalog API has a real UI icon', () => { expect(schema!.icon && schema!.icon in BLOCK_ICON_COMPONENTS, blockId).toBe(true); } }); - - it('unknown names still resolve to Activity rather than throwing', () => { - expect(resolveBlockIcon('NotARealIcon')).toBe(Activity); - expect(resolveBlockIcon(undefined)).toBe(Activity); - }); }); diff --git a/src/components/blockIcons.tsx b/src/components/blockIcons.tsx index bec3183..b245c0c 100644 --- a/src/components/blockIcons.tsx +++ b/src/components/blockIcons.tsx @@ -67,13 +67,6 @@ export const BLOCK_ICON_COMPONENTS: Record = { Zap }; -export function resolveBlockIcon(iconName?: string): LucideIcon { - if (iconName && BLOCK_ICON_COMPONENTS[iconName]) { - return BLOCK_ICON_COMPONENTS[iconName]; - } - return Activity; -} - /** Look up a Lucide icon by catalog / block name without creating a component in render. */ export function BlockGlyph({ name, className }: { name?: string; className?: string }) { return createElement(BLOCK_ICON_COMPONENTS[name ?? ''] ?? Activity, { className }); diff --git a/src/components/blocks/BlockCard.tsx b/src/components/blocks/BlockCard.tsx index dfc9140..7a203c8 100644 --- a/src/components/blocks/BlockCard.tsx +++ b/src/components/blocks/BlockCard.tsx @@ -29,7 +29,7 @@ import { type DraggableAttributes, type DraggableSyntheticListeners } from '@dnd * the whole card made the card a role="button" that swallowed the names of * the controls inside it (FINDINGS.md). */ -export interface BlockDragHandle { +interface BlockDragHandle { attributes: DraggableAttributes; listeners: DraggableSyntheticListeners; setActivatorNodeRef: (element: HTMLElement | null) => void; @@ -101,7 +101,6 @@ export function BlockCard({ ? '0 0 0 1px rgba(34, 211, 238, 0.35), 0 0 28px rgba(34, 211, 238, 0.25)' : undefined }} - exit={{ opacity: 0, scale: 0.95 }} onMouseEnter={() => setIsHovered(true)} onMouseLeave={() => setIsHovered(false)} data-cited={isCited ? 'true' : 'false'} diff --git a/src/components/blocks/BlockSetupCard.tsx b/src/components/blocks/BlockSetupCard.tsx index fc987d4..7955d37 100644 --- a/src/components/blocks/BlockSetupCard.tsx +++ b/src/components/blocks/BlockSetupCard.tsx @@ -42,21 +42,19 @@ export function BlockBodyState({ isLoading, isEmpty, loadingLabel = 'Loading...', - emptyLabel = 'No results', children }: { error?: string | null; isLoading: boolean; isEmpty: boolean; loadingLabel?: string; - emptyLabel?: string; children: ReactNode; }) { if (error) return ; if (isEmpty) { return (
- {isLoading ? loadingLabel : emptyLabel} + {isLoading ? loadingLabel : 'No results'}
); } diff --git a/src/components/blocks/CryptoView.tsx b/src/components/blocks/CryptoView.tsx index 980163e..e5bbda8 100644 --- a/src/components/blocks/CryptoView.tsx +++ b/src/components/blocks/CryptoView.tsx @@ -12,7 +12,7 @@ import { BlockBodyState } from './BlockSetupCard'; /** * Crypto asset data for display */ -export interface CryptoAsset { +interface CryptoAsset { id: string; symbol: string; name: string; @@ -32,7 +32,6 @@ export interface CryptoAsset { interface CryptoViewProps { assets: CryptoAsset[]; status: string; - lastUpdated?: number | null; onRefresh?: () => void; error?: string | null; } diff --git a/src/components/blocks/HNView.tsx b/src/components/blocks/HNView.tsx index 9274d66..eebf36e 100644 --- a/src/components/blocks/HNView.tsx +++ b/src/components/blocks/HNView.tsx @@ -12,7 +12,7 @@ import { BlockBodyState } from './BlockSetupCard'; /** * HN story data for display */ -export interface HNStory { +interface HNStory { id: string; title: string; url: string; @@ -30,7 +30,6 @@ export interface HNStory { interface HNViewProps { stories: HNStory[]; status: string; - lastUpdated?: number | null; onRefresh?: () => void; error?: string | null; } diff --git a/src/components/blocks/MemoryBlock.tsx b/src/components/blocks/MemoryBlock.tsx index 129df10..414623d 100644 --- a/src/components/blocks/MemoryBlock.tsx +++ b/src/components/blocks/MemoryBlock.tsx @@ -40,7 +40,7 @@ interface MemoryBlockProps { export function MemoryBlockView({ instanceId }: MemoryBlockProps) { const { getBlock, updateData } = useBlockStore(); const contextPools = useMindStore(state => state.contextPools); - const addToPool = useMindStore(state => state.addToPool); + const pushContext = useMindStore(state => state.pushContext); const [draft, setDraft] = useState(''); const block = getBlock(instanceId); @@ -80,7 +80,7 @@ export function MemoryBlockView({ instanceId }: MemoryBlockProps) { const addEntry = () => { const content = draft.trim(); if (!content) return; - addToPool(data.poolId, { type: 'memory', content, importance: 0.6 }); + pushContext(data.poolId, { type: 'memory', content, importance: 0.6 }); setDraft(''); }; diff --git a/src/components/brand/Atmosphere.tsx b/src/components/brand/Atmosphere.tsx index ab1f2c1..48cc2b6 100644 --- a/src/components/brand/Atmosphere.tsx +++ b/src/components/brand/Atmosphere.tsx @@ -22,7 +22,7 @@ const SOFTWARE_GL = /swiftshader|llvmpipe|softpipe|software|basic render/i; * "no WebGL2": the CSS nebula shows instead. */ let hardwareGL: boolean | undefined; -export function hasHardwareWebGL2(): boolean { +function hasHardwareWebGL2(): boolean { if (hardwareGL === undefined) hardwareGL = probeHardwareWebGL2(); return hardwareGL; } diff --git a/src/components/mind/MemoryConfirmModal.tsx b/src/components/mind/MemoryConfirmModal.tsx index b13c2b5..20701df 100644 --- a/src/components/mind/MemoryConfirmModal.tsx +++ b/src/components/mind/MemoryConfirmModal.tsx @@ -165,5 +165,3 @@ export function MemoryConfirmModal({ ); } - -export default MemoryConfirmModal; diff --git a/src/components/mind/MindDock.tsx b/src/components/mind/MindDock.tsx index 3e4c094..1df7733 100644 --- a/src/components/mind/MindDock.tsx +++ b/src/components/mind/MindDock.tsx @@ -256,5 +256,3 @@ export function MindDock({ onExpandPanel }: MindDockProps) { ); } - -export default MindDock; diff --git a/src/components/mind/MindPanel.tsx b/src/components/mind/MindPanel.tsx index 46003b5..7948747 100644 --- a/src/components/mind/MindPanel.tsx +++ b/src/components/mind/MindPanel.tsx @@ -77,7 +77,6 @@ export function MindPanel({ isOpen, onClose }: MindPanelProps) { if (text && text.length > 0) { setSelectedText(text); setSelectionModalOpen(true); - // selection?.removeAllRanges(); // Optional: clear selection } }; @@ -738,5 +737,3 @@ function SettingsView({ llmConfig, onProviderChange }: SettingsViewProps) { ); } - -export default MindPanel; diff --git a/src/components/mind/ThinkResultModal.tsx b/src/components/mind/ThinkResultModal.tsx index b89c313..38b42d3 100644 --- a/src/components/mind/ThinkResultModal.tsx +++ b/src/components/mind/ThinkResultModal.tsx @@ -182,5 +182,3 @@ export function ThinkResultModal({ ); } - -export default ThinkResultModal; diff --git a/src/components/mind/index.ts b/src/components/mind/index.ts deleted file mode 100644 index 0dc980b..0000000 --- a/src/components/mind/index.ts +++ /dev/null @@ -1,5 +0,0 @@ -// ============================================ -// PROJECT OMNI: MIND COMPONENTS -// ============================================ - -export { MindPanel } from './MindPanel'; diff --git a/src/components/voice/VoiceControl.tsx b/src/components/voice/VoiceControl.tsx index 505a9a1..03c0cd6 100644 --- a/src/components/voice/VoiceControl.tsx +++ b/src/components/voice/VoiceControl.tsx @@ -6,7 +6,6 @@ import { fallbackSpeechAdapter, openaiRealtimeAdapter } from '@/core/interaction import { spatialSession } from '@/core/interaction/session'; import { browserSpeechFeedback, emitFeedback, silenceFeedback, type SpeechFeedbackAdapter } from '@/core/interaction/speechFeedback'; import { createSpeechInput, type SpeechOutcome } from '@/core/interaction/speechInput'; -import { liveSpeechTimings } from '@/core/interaction/speechMeasurement'; import type { SpeechAdapter, SpeechCapture } from '@/core/interaction/speechObservation'; import { useUIStore } from '@/core/stores'; @@ -54,7 +53,6 @@ export function VoiceControl({ const [input] = useState(() => createSpeechInput({ adapter: adapter ?? defaultSpeechAdapter(), authority: spatialSession, - recorder: liveSpeechTimings, onListening: capture => setProvider(providerLabel(capture)) })); const feedbackRef = useRef(feedback === undefined ? browserSpeechFeedback() : feedback); diff --git a/src/core/account/account.ts b/src/core/account/account.ts index 50fa228..1429822 100644 --- a/src/core/account/account.ts +++ b/src/core/account/account.ts @@ -1,7 +1,7 @@ import { captureLiveStores, validateStoreEnvelopes } from './snapshot'; import { isVaultExport, prepareVaultImport, type OmniVaultExport } from '../vault/vaultExport'; -export const ACCOUNT_ORIGIN = 'https://syberlabs.io'; +const ACCOUNT_ORIGIN = 'https://syberlabs.io'; export const SIGN_IN_URL = `${ACCOUNT_ORIGIN}/auth/signin?next=${encodeURIComponent('/admin/return?app=omni')}`; export interface Account { user: { id: string; label: string }; portalUrl: string } export interface SavedCanvas { id: string; app: string; name: string; createdAt: number; bytes: number; payload?: unknown } diff --git a/src/core/bundleScan.test.ts b/src/core/bundleScan.test.ts index 6604cf4..1afd7ed 100644 --- a/src/core/bundleScan.test.ts +++ b/src/core/bundleScan.test.ts @@ -18,7 +18,7 @@ import { MIN_CANARY_LENGTH, type Canary } from './bundleScan'; -import { SECRET_ENV_VARS, PUBLIC_ENV_VARS } from './secrets'; +import { SECRET_ENV_VARS } from './secrets'; let root: string; @@ -193,11 +193,4 @@ describe('the secret list itself', () => { expect(name.startsWith('NEXT_PUBLIC_')).toBe(false); } }); - - it('keeps the public and secret lists disjoint', () => { - const secret = new Set(SECRET_ENV_VARS); - for (const name of PUBLIC_ENV_VARS) { - expect(secret.has(name)).toBe(false); - } - }); }); diff --git a/src/core/bundleScan.ts b/src/core/bundleScan.ts index 326771f..3543492 100644 --- a/src/core/bundleScan.ts +++ b/src/core/bundleScan.ts @@ -26,14 +26,14 @@ import { readdirSync, readFileSync, statSync } from 'node:fs'; import path from 'node:path'; -export interface SecretLeak { +interface SecretLeak { /** The env var whose value was found. */ envVar: string; /** Path of the served file, relative to the scanned root. */ file: string; } -export interface BundleScanResult { +interface BundleScanResult { filesScanned: number; bytesScanned: number; leaks: SecretLeak[]; diff --git a/src/core/capabilities/apiIndex.ts b/src/core/capabilities/apiIndex.ts index c44915a..25dba52 100644 --- a/src/core/capabilities/apiIndex.ts +++ b/src/core/capabilities/apiIndex.ts @@ -13,7 +13,7 @@ // ============================================ /** The one host specs are fetched from. Every upstream spec URL is on it today. */ -export const CATALOG_SPEC_HOST = 'api.apis.guru'; +const CATALOG_SPEC_HOST = 'api.apis.guru'; /** Upstream list this index is built from. */ export const CATALOG_SOURCE_URL = 'https://api.apis.guru/v2/list.json'; @@ -59,7 +59,7 @@ export interface ApiIndexEntry { curated?: true; } -export type CredentialNeed = 'none' | 'partial' | 'key'; +type CredentialNeed = 'none' | 'partial' | 'key'; const CREDENTIAL_NEEDS: readonly CredentialNeed[] = ['none', 'partial', 'key']; /** Summarise what a set of compiled operations needs to run, from each one's auth kind. */ @@ -131,7 +131,7 @@ export interface ApiIndex { entries: ApiIndexEntry[]; } -export interface BuildIssue { +interface BuildIssue { id: string; reason: string; } diff --git a/src/core/capabilities/asyncRuntime.ts b/src/core/capabilities/asyncRuntime.ts index 2d675cb..879a0dd 100644 --- a/src/core/capabilities/asyncRuntime.ts +++ b/src/core/capabilities/asyncRuntime.ts @@ -3,7 +3,7 @@ // not decide success, retry, or uncertainty: the executor does, from what was // observed. Each start or poll request is still one bounded request. -export interface AsyncCall { +interface AsyncCall { /** Credential placement resolved from the manifest's slot for this call only. */ headers: Record; signal: AbortSignal; @@ -62,7 +62,7 @@ function abortError(): Error { } export const EXTERNAL_RUN_ID_PATTERN = /^[A-Za-z0-9._:-]{1,128}$/; -export const MAX_CONSECUTIVE_POLL_ERRORS = 5; +const MAX_CONSECUTIVE_POLL_ERRORS = 5; const runtimes = new Map(); @@ -85,7 +85,7 @@ export type PollOutcome = | { kind: 'deadline' } | { kind: 'unobservable'; message: string }; -export interface PollPlan { +interface PollPlan { runtime: AsyncJobRuntime; operation: string; externalRunId: string; diff --git a/src/core/capabilities/boundary.test.ts b/src/core/capabilities/boundary.test.ts index d2cf072..3819c11 100644 --- a/src/core/capabilities/boundary.test.ts +++ b/src/core/capabilities/boundary.test.ts @@ -196,7 +196,7 @@ describe('capability production boundaries', () => { const speakId = useBlockStore.getState().addBlock(blockRegistry.get('cap_speech_speak')!, { x: 40, y: 0 }); const wireId = wireService.createWire(sourceId, speakId); expect(wireId).not.toBe(''); - expect(useWireStore.getState().getWire(wireId)?.projection).toEqual({ kind: 'join_titles' }); + expect(useWireStore.getState().wires.find(w => w.id === wireId)?.projection).toEqual({ kind: 'join_titles' }); const count = compileOpenApi({ openapi: '3.0.3', diff --git a/src/core/capabilities/compatibility.ts b/src/core/capabilities/compatibility.ts index 8d4bd3d..976d9af 100644 --- a/src/core/capabilities/compatibility.ts +++ b/src/core/capabilities/compatibility.ts @@ -16,7 +16,7 @@ function isStringSink(schema: ValueType | undefined): boolean { return schema.properties?.[required[0]]?.kind === 'string'; } -export type WireProjectionKind = 'identity' | 'text' | 'join_titles'; +type WireProjectionKind = 'identity' | 'text' | 'join_titles'; export interface WireProjection { kind: WireProjectionKind; @@ -63,11 +63,6 @@ export function admitConnection(sourceBlockId: string, targetBlockId: string): C return { ok: true, ...ports, projection: { kind: 'identity' } }; } -export function explainPortWire(sourceBlockId: string, targetBlockId: string): string | null { - const admission = admitConnection(sourceBlockId, targetBlockId); - return admission.ok ? null : admission.reason; -} - export function portDataTypeFor(kind: string): PortSchema['dataType'] { if (kind === 'string') return 'text'; if (kind === 'any') return 'any'; diff --git a/src/core/capabilities/connectProviders.ts b/src/core/capabilities/connectProviders.ts index 765f073..c838a0d 100644 --- a/src/core/capabilities/connectProviders.ts +++ b/src/core/capabilities/connectProviders.ts @@ -19,7 +19,7 @@ export interface ConnectProvider { isReadOnlyScope(scope: string): boolean; } -export const CONNECT_PROVIDERS: readonly ConnectProvider[] = [ +const CONNECT_PROVIDERS: readonly ConnectProvider[] = [ { id: 'google', title: 'Google', diff --git a/src/core/capabilities/credentialName.ts b/src/core/capabilities/credentialName.ts index 16f2be5..dc34851 100644 --- a/src/core/capabilities/credentialName.ts +++ b/src/core/capabilities/credentialName.ts @@ -35,7 +35,7 @@ export function operationNeedsCredential(operation: { && ((input.required && looksLikeKey(input.name, input.schema.description)) || KEY_NAME.test(input.name))); } -export interface KeyParameterCandidate { +interface KeyParameterCandidate { name: string; in: string; description?: string; diff --git a/src/core/capabilities/egress.ts b/src/core/capabilities/egress.ts index 31e143f..21e76ec 100644 --- a/src/core/capabilities/egress.ts +++ b/src/core/capabilities/egress.ts @@ -7,11 +7,11 @@ // loopback, link-local, private, or metadata addresses. One private answer // from DNS rejects the name: a mixed result is how rebinding hides. -export interface ResolvedAddress { +interface ResolvedAddress { address: string; } -export type EgressDecision = { ok: true } | { ok: false; reason: string }; +type EgressDecision = { ok: true } | { ok: false; reason: string }; const METADATA_HOSTS = new Set(['metadata.google.internal', 'metadata.google.com']); @@ -20,7 +20,7 @@ const BLOCKED_HOSTS = new Set([ ...METADATA_HOSTS ]); -export interface DestinationUrlOptions { +interface DestinationUrlOptions { /** * The host itself built the thing that names this URL (not a compiler, * provider, model or stored snapshot). Only then may it target loopback. @@ -100,7 +100,7 @@ export function classifyAddress(address: string): EgressDecision { } /** 127.0.0.0/8 or ::1, in any spelling, including an IPv4-mapped one. */ -export function isLoopbackAddress(address: string): boolean { +function isLoopbackAddress(address: string): boolean { if (!address.includes(':')) return /^127(\.\d{1,3}){3}$/.test(address); const hextets = parseIpv6(address); if (!hextets) return false; @@ -109,7 +109,7 @@ export function isLoopbackAddress(address: string): boolean { return hextets.slice(0, 7).every(group => group === 0) && hextets[7] === 1; } -export function isIpLiteral(host: string): boolean { +function isIpLiteral(host: string): boolean { return /^\d{1,3}(\.\d{1,3}){3}$/.test(host) || host.includes(':'); } diff --git a/src/core/capabilities/execute.ts b/src/core/capabilities/execute.ts index 796808e..b5e8608 100644 --- a/src/core/capabilities/execute.ts +++ b/src/core/capabilities/execute.ts @@ -2,7 +2,7 @@ // Side effects stop here until approveCapability has moved them to `approved`. import { canonicalize, sha256 } from './hash'; -import { joinUrl, resolveHttpPath, setQueryParam, stringifyParam } from './httpTarget'; +import { resolveHttpPath, setQueryParam, stringifyParam } from './httpTarget'; import { redact } from './redact'; import { admitExecution, @@ -101,8 +101,6 @@ export interface ExecuteOptions { confirmedRun?: string; } -export { joinUrl }; - /** What a write or destructive run will send, shown to a person before it is sent. */ export interface RunPreview { capabilityId: string; @@ -384,7 +382,7 @@ function settleAsync( return result; } -export interface ReconcileReport { +interface ReconcileReport { runId: string; status: ExecutionPhase; observed: boolean; diff --git a/src/core/capabilities/executionLedger.ts b/src/core/capabilities/executionLedger.ts index ba7d0dd..d68e715 100644 --- a/src/core/capabilities/executionLedger.ts +++ b/src/core/capabilities/executionLedger.ts @@ -18,10 +18,10 @@ export interface ExecutionReceipt { value?: unknown; } -export type ExecutionProfileKind = 'sync' | 'async_poll'; +type ExecutionProfileKind = 'sync' | 'async_poll'; /** What the destination last reported. `accepted` means start returned a run id. */ -export type ObservedStatus = 'accepted' | 'running' | 'succeeded' | 'failed'; +type ObservedStatus = 'accepted' | 'running' | 'succeeded' | 'failed'; export interface ExecutionRecord { runId: string; @@ -93,7 +93,7 @@ function isSideEffect(effect: CapabilityEffect): boolean { * bounded by the same deadline and settles the row itself; closing it * underneath would leave the observer's result and the ledger disagreeing. */ -export function reconcileExecutions(now = Date.now(), observing: ReadonlySet = new Set()): void { +function reconcileExecutions(now = Date.now(), observing: ReadonlySet = new Set()): void { let changed = false; const records = executionRecords().map(record => { if (record.status !== 'admitted' && record.status !== 'running') return record; @@ -122,7 +122,7 @@ export function reconcileExecutions(now = Date.now(), observing: ReadonlySet = { 'local|speech.listen': 'cap_speech_listen' }; -export function canonicalBase(baseUrl: string): string { +function canonicalBase(baseUrl: string): string { const url = new URL(baseUrl); const path = url.pathname.replace(/\/$/, ''); return `${url.origin}${path}`; } -export function capabilityIdentityKey(transport: CapabilityTransport): string { +function capabilityIdentityKey(transport: CapabilityTransport): string { if (transport.kind === 'local') return `local|${transport.handler}`; // The origin is in the key so two servers that share a label are two capabilities. if (transport.kind === 'mcp') return `mcp|${transport.origin}|${transport.serverId}|${transport.toolName}`; diff --git a/src/core/capabilities/importAdmission.ts b/src/core/capabilities/importAdmission.ts index 779d4ea..06652de 100644 --- a/src/core/capabilities/importAdmission.ts +++ b/src/core/capabilities/importAdmission.ts @@ -18,7 +18,7 @@ export interface ImportedCapability { effect: 'write' | 'destructive'; } -export interface ImportedApprovalReport { +interface ImportedApprovalReport { /** The blob to write: identical to the input unless an entry was reset. */ blob: string; /** Every write or destructive entry in the file, all now `pending`. */ diff --git a/src/core/capabilities/jsonSchema.ts b/src/core/capabilities/jsonSchema.ts index 601d44b..40c74ad 100644 --- a/src/core/capabilities/jsonSchema.ts +++ b/src/core/capabilities/jsonSchema.ts @@ -10,11 +10,11 @@ import { isRecord, type ValueType } from './valueType'; const MAX_DEPTH = 24; -export type SchemaConversion = +type SchemaConversion = | { ok: true; schema: ValueType } | { ok: false; error: string }; -export interface ConvertOptions { +interface ConvertOptions { /** 'any': an untyped schema below the top level is `any` (responses). Default: refused. */ untypedBelowTop?: 'any'; /** Internal: whether this schema is a property, item, or map value of another. */ diff --git a/src/core/capabilities/jsonValue.ts b/src/core/capabilities/jsonValue.ts index 0692a05..f24b261 100644 --- a/src/core/capabilities/jsonValue.ts +++ b/src/core/capabilities/jsonValue.ts @@ -7,7 +7,7 @@ const MAX_DEPTH = 64; -export type JsonValue = string | number | boolean | null | readonly JsonValue[] | { readonly [key: string]: JsonValue }; +type JsonValue = string | number | boolean | null | readonly JsonValue[] | { readonly [key: string]: JsonValue }; /** * A deep, frozen copy of `value` made only of strings, finite numbers, diff --git a/src/core/capabilities/keyGuide.ts b/src/core/capabilities/keyGuide.ts index 58012e5..216da53 100644 --- a/src/core/capabilities/keyGuide.ts +++ b/src/core/capabilities/keyGuide.ts @@ -67,7 +67,7 @@ export function checkKeyFormat(guide: KeyGuide | null, key: string): string | nu : `This does not look like the documented key format (${guide.format.describe}). You can still save it.`; } -export type KeyTestResult = { ok: true; message: string } | { ok: false; message: string }; +type KeyTestResult = { ok: true; message: string } | { ok: false; message: string }; /** Fields some APIs use to refuse a key while answering 200 (Alpha Vantage). */ const NOTICE_FIELDS = ['Error Message', 'Information', 'Note', 'error', 'message']; diff --git a/src/core/capabilities/manifest.ts b/src/core/capabilities/manifest.ts index cc2d169..1797fce 100644 --- a/src/core/capabilities/manifest.ts +++ b/src/core/capabilities/manifest.ts @@ -8,22 +8,22 @@ import { canonicalCapabilityId, credentialSlot, transportCredentialSlot } from ' import { destinationUrlErrors } from './egress'; import { validateValueType, type Primitive, type ValueType } from './valueType'; -export const CAPABILITY_MANIFEST_VERSION = 1 as const; +const CAPABILITY_MANIFEST_VERSION = 1 as const; export type CapabilityEffect = 'read' | 'compute' | 'write' | 'destructive'; -export type CapabilityApproval = 'auto' | 'pending' | 'approved' | 'denied'; -export type CapabilitySourceKind = 'openapi' | 'mcp' | 'bring' | 'web_data' | 'managed_integration'; -export type CapabilityInvocation = 'auto' | 'manual'; +type CapabilityApproval = 'auto' | 'pending' | 'approved' | 'denied'; +type CapabilitySourceKind = 'openapi' | 'mcp' | 'bring' | 'web_data' | 'managed_integration'; +type CapabilityInvocation = 'auto' | 'manual'; export type CapabilityTrigger = | { kind: 'manual' } | { kind: 'on_create' } | { kind: 'on_input_change' } | { kind: 'interval'; everyMs: number }; export type HttpMethod = 'GET' | 'HEAD' | 'POST' | 'PUT' | 'PATCH' | 'DELETE'; -export type InputLocation = 'path' | 'query' | 'header' | 'body' | 'argument'; -export type EffectSource = 'method' | 'extension' | 'annotation' | 'declared'; +type InputLocation = 'path' | 'query' | 'header' | 'body' | 'argument'; +type EffectSource = 'method' | 'extension' | 'annotation' | 'declared'; -export interface CapabilitySource { +interface CapabilitySource { kind: CapabilitySourceKind; locator: string; operationId?: string; @@ -59,7 +59,7 @@ export interface CapabilityInput { default?: Primitive; } -export interface CapabilityOutput { +interface CapabilityOutput { schema: ValueType; /** Dot path used only when projecting to the OmniData envelope. */ itemsPath?: string; @@ -67,7 +67,7 @@ export interface CapabilityOutput { presentation: 'items' | 'content' | 'raw'; } -export type HttpAccess = 'browser_direct' | 'server_broker'; +type HttpAccess = 'browser_direct' | 'server_broker'; export type CapabilityTransport = | { kind: 'http'; access: HttpAccess; baseUrl: string; method: HttpMethod; path: string } @@ -77,23 +77,23 @@ export type CapabilityTransport = /** A host-bound job runtime: start returns an external run id, poll observes it. */ | { kind: 'async'; runtimeId: string; operation: string }; -export const MIN_POLL_INTERVAL_MS = 1_000; -export const MAX_POLL_INTERVAL_MS = 300_000; -export const MAX_ASYNC_DURATION_MS = 3_600_000; +const MIN_POLL_INTERVAL_MS = 1_000; +const MAX_POLL_INTERVAL_MS = 300_000; +const MAX_ASYNC_DURATION_MS = 3_600_000; /** How an admitted capability completes. Absent means synchronous. */ export type CapabilityExecutionProfile = | { kind: 'sync' } | { kind: 'async_poll'; pollIntervalMs: number; maxDurationMs: number }; -export type CapabilityProviderKind = 'openapi' | 'mcp' | 'managed_integration' | 'web_data' | 'manual'; +type CapabilityProviderKind = 'openapi' | 'mcp' | 'managed_integration' | 'web_data' | 'manual'; /** * Where an admitted capability came from. Written by admission, never by a * provider. Part of the digest, so a stored manifest cannot swap its origin * story without failing validation. */ -export interface CapabilityProvenance { +interface CapabilityProvenance { providerId: string; providerKind: CapabilityProviderKind; externalId: string; @@ -131,7 +131,7 @@ export interface CapabilityManifest { digest: string; } -export type ManifestDraft = Omit; +type ManifestDraft = Omit; const EFFECTS: CapabilityEffect[] = ['read', 'compute', 'write', 'destructive']; const APPROVALS: CapabilityApproval[] = ['auto', 'pending', 'approved', 'denied']; @@ -150,7 +150,7 @@ const MANIFEST_KEYS = new Set([ const RUNTIME_ID_PATTERN = /^[A-Za-z0-9_.:-]{1,80}$/; const MAX_OAUTH_SCOPES = 20; /** Each input schema is already depth-bounded by validateValueType. */ -export const MAX_MANIFEST_INPUTS = 64; +const MAX_MANIFEST_INPUTS = 64; const OAUTH_SCOPE_PATTERN = /^[\x21\x23-\x5B\x5D-\x7E]{1,200}$/; const OPERATION_PATTERN = /^[A-Za-z0-9_.:-]{1,120}$/; @@ -159,7 +159,7 @@ const PROVENANCE_KEYS = new Set([ 'discoveredAtMs', 'admittedAtMs', 'schemaDigest' ]); -export const PROVIDER_ID_PATTERN = /^[a-z][a-z0-9_.:-]{0,63}$/; +const PROVIDER_ID_PATTERN = /^[a-z][a-z0-9_.:-]{0,63}$/; const ID_PATTERN = /^cap_[a-z0-9_]{1,80}$/; const SECRET_REF_PATTERN = /^[A-Za-z0-9_.:-]{1,64}$/; @@ -177,7 +177,7 @@ export function isInputName(name: string): boolean { * (constructor, toString, ...), which a lookup would find by inheritance. * An input or credential placement may not use one. */ -export function isReservedName(name: string): boolean { +function isReservedName(name: string): boolean { return name === '__proto__' || name === 'prototype' || Object.prototype.hasOwnProperty.call(Object.prototype, name); } @@ -214,7 +214,7 @@ export const DENIED_HEADER_NAMES: ReadonlySet = new Set([ * already outside the token grammar; they are refused here as well so the * rule holds without the grammar. */ -export function isDeniedHeaderName(name: string): boolean { +function isDeniedHeaderName(name: string): boolean { return name.startsWith(':') || DENIED_HEADER_NAMES.has(name.toLowerCase()); } @@ -255,16 +255,16 @@ export function effectAllowedForMethod(method: HttpMethod, effect: CapabilityEff return effect === 'write' || effect === 'destructive'; } -export const MIN_TRIGGER_INTERVAL_MS = 5_000; -export const MAX_TRIGGER_INTERVAL_MS = 86_400_000; +const MIN_TRIGGER_INTERVAL_MS = 5_000; +const MAX_TRIGGER_INTERVAL_MS = 86_400_000; -export function normalizeTrigger(trigger: CapabilityTrigger | undefined, invocation?: CapabilityInvocation): CapabilityTrigger { +function normalizeTrigger(trigger: CapabilityTrigger | undefined, invocation?: CapabilityInvocation): CapabilityTrigger { if (trigger && validTriggerShape(trigger)) return trigger; if (invocation === 'auto') return { kind: 'on_create' }; return { kind: 'manual' }; } -export function triggerInvocation(trigger: CapabilityTrigger): CapabilityInvocation { +function triggerInvocation(trigger: CapabilityTrigger): CapabilityInvocation { return trigger.kind === 'manual' ? 'manual' : 'auto'; } @@ -274,7 +274,7 @@ function validTriggerShape(trigger: CapabilityTrigger): boolean { return false; } -export function triggerProblem(effect: CapabilityEffect, trigger: CapabilityTrigger): string | null { +function triggerProblem(effect: CapabilityEffect, trigger: CapabilityTrigger): string | null { if ((effect === 'write' || effect === 'destructive') && trigger.kind !== 'manual') { return 'write and destructive capabilities only run manually'; } @@ -289,11 +289,11 @@ export function triggerProblem(effect: CapabilityEffect, trigger: CapabilityTrig } /** Digest of the typed contract alone: inputs and output schema. */ -export function schemaDigestFor(draft: Pick): string { +function schemaDigestFor(draft: Pick): string { return sha256(canonicalize({ inputs: draft.inputs, output: draft.output })); } -export function digestPayload(draft: ManifestDraft): unknown { +function digestPayload(draft: ManifestDraft): unknown { return { version: draft.version, id: draft.id, @@ -465,13 +465,13 @@ function pathParamNames(path: string): string[] { return [...path.matchAll(/\{([^}]+)\}/g)].map(match => match[1]); } -export interface ManifestValidation { +interface ManifestValidation { ok: boolean; manifest?: CapabilityManifest; errors: string[]; } -export interface ManifestValidationOptions { +interface ManifestValidationOptions { /** * The host itself built this manifest (not a compiler, provider, model or * stored snapshot). Only then may an http transport target loopback. @@ -773,7 +773,7 @@ function canonicalDraft(input: Record): ManifestDraft { }; } -export function canonicalInput(entry: CapabilityInput): CapabilityInput { +function canonicalInput(entry: CapabilityInput): CapabilityInput { return { name: entry.name, in: entry.in, @@ -785,7 +785,7 @@ export function canonicalInput(entry: CapabilityInput): CapabilityInput { } /** The longest example or default kept from a spec. Longer ones are dropped, not cut. */ -export const MAX_INPUT_HINT = 200; +const MAX_INPUT_HINT = 200; /** An example or default a field can show: a short string, a finite number, or a boolean. */ export function isInputHint(value: unknown): value is Primitive { @@ -820,7 +820,7 @@ function canonicalAuth(auth: AuthBinding): AuthBinding { }; } -export function canonicalScopes(scopes: readonly string[]): string[] { +function canonicalScopes(scopes: readonly string[]): string[] { return [...new Set(scopes)].sort(); } @@ -843,7 +843,7 @@ function canonicalTransport(transport: CapabilityTransport): CapabilityTransport }; } -export function canonicalOutput(output: CapabilityOutput): CapabilityOutput { +function canonicalOutput(output: CapabilityOutput): CapabilityOutput { return { schema: output.schema, presentation: output.presentation, diff --git a/src/core/capabilities/openapi.ts b/src/core/capabilities/openapi.ts index f5e0211..8636914 100644 --- a/src/core/capabilities/openapi.ts +++ b/src/core/capabilities/openapi.ts @@ -27,7 +27,7 @@ import type { ValueType } from './valueType'; const METHODS: HttpMethod[] = ['GET', 'HEAD', 'POST', 'PUT', 'PATCH', 'DELETE']; const MAX_OPERATIONS = 100; -export interface CompileIssue { +interface CompileIssue { operation?: string; message: string; } @@ -37,7 +37,7 @@ export interface CompileResult { errors: CompileIssue[]; } -export interface CompileOptions { +interface CompileOptions { /** * Where the document came from, when the host knows better than the * document does: the catalog spec URL for a registry install. Recorded as diff --git a/src/core/capabilities/project.ts b/src/core/capabilities/project.ts index a2ea265..e81ea7b 100644 --- a/src/core/capabilities/project.ts +++ b/src/core/capabilities/project.ts @@ -11,7 +11,7 @@ export interface TypedValue { value: unknown; } -export interface CapabilityError { +interface CapabilityError { code: string; message: string; retryable: boolean; diff --git a/src/core/capabilities/registry.ts b/src/core/capabilities/registry.ts index 58a4316..46d83e8 100644 --- a/src/core/capabilities/registry.ts +++ b/src/core/capabilities/registry.ts @@ -21,7 +21,7 @@ import { resolveWiredInputs } from './wireInputs'; import { runSpeechHandler, speechManifests } from './speech'; import type { ValueType } from './valueType'; -export interface InstallResult { +interface InstallResult { ok: boolean; manifest?: CapabilityManifest; errors: string[]; @@ -32,7 +32,7 @@ export interface CapabilitySnapshot { manifests: CapabilityManifest[]; } -export interface RestoreReport { +interface RestoreReport { installed: string[]; rejected: Array<{ id?: string; errors: string[] }>; } @@ -101,18 +101,6 @@ export function uninstallCapability(id: string, options?: { removeInstances?: bo return true; } -export function listCapabilities(): CapabilityManifest[] { - return allCapabilities(); -} - -export function getCapability(id: string): CapabilityManifest | undefined { - return readCapability(id); -} - -export function exportSnapshot(): CapabilitySnapshot { - return { version: 1, manifests: allCapabilities() }; -} - export function restoreSnapshot(snapshot: unknown): RestoreReport { if (!isSnapshot(snapshot)) { return { installed: [], rejected: [{ errors: ['snapshot must be { version: 1, manifests }'] }] }; @@ -311,7 +299,7 @@ function isId(value: unknown): value is { id: string } { } /** Called once, after the vault read. Revalidates before anything is rebound. */ -export function acceptRehydrated(manifests: CapabilityManifest[]): void { +function acceptRehydrated(manifests: CapabilityManifest[]): void { if (!claimHydration()) return; // A session that already installed capabilities must not be wiped by an // empty vault read that resolves a moment later. diff --git a/src/core/capabilities/registryIntents.eval.ts b/src/core/capabilities/registryIntents.eval.ts index a17811b..0b7446b 100644 --- a/src/core/capabilities/registryIntents.eval.ts +++ b/src/core/capabilities/registryIntents.eval.ts @@ -16,7 +16,7 @@ // bounds the claim: measured on author-written questions, not on users. // ============================================ -export interface IntentQuestion { +interface IntentQuestion { query: string; /** Any of these as JEV's top intent counts as right. */ accept: string[]; diff --git a/src/core/capabilities/registryRanking.ts b/src/core/capabilities/registryRanking.ts index 7760edb..f4219a2 100644 --- a/src/core/capabilities/registryRanking.ts +++ b/src/core/capabilities/registryRanking.ts @@ -14,11 +14,11 @@ import { DEFAULT_RESULT_LIMIT, type ApiSearcher } from './registrySearch'; import { OTHER_INTENT, REGISTRY_INTENTS, type RegistryIntent } from './registryIntents'; /** An intent counts once JEV gives it at least this probability. */ -export const MIN_INTENT_PROBABILITY = 0.15; +const MIN_INTENT_PROBABILITY = 0.15; /** At most this many intents shape one result list. */ -export const MAX_ROUTED_INTENTS = 2; +const MAX_ROUTED_INTENTS = 2; /** JEV is confident nothing listed fits: keyword search alone. */ -export const OTHER_CUTOFF = 0.5; +const OTHER_CUTOFF = 0.5; /** Keyword results taken per routed intent's terms. */ const TERMS_PER_INTENT = 5; @@ -27,14 +27,14 @@ export interface IntentScore { p: number; } -export interface RankedResult { +interface RankedResult { entry: ApiIndexEntry; /** Why it is here: a routed intent, or the keyword search on the query. */ source: 'intent' | 'keyword'; intent?: string; } -export interface RankOptions { +interface RankOptions { limit?: number; intents?: readonly RegistryIntent[]; } @@ -101,9 +101,9 @@ export function rankResults( // CLIENT CALL // ============================================ -export const REGISTRY_INTENT_ENDPOINT = '/api/registry-intent'; +const REGISTRY_INTENT_ENDPOINT = '/api/registry-intent'; -export interface IntentAnswer { +interface IntentAnswer { scores: IntentScore[]; model: string; } diff --git a/src/core/capabilities/registrySearch.eval.ts b/src/core/capabilities/registrySearch.eval.ts index d8ceb76..5e9f2c9 100644 --- a/src/core/capabilities/registrySearch.eval.ts +++ b/src/core/capabilities/registrySearch.eval.ts @@ -49,7 +49,7 @@ export const SEMANTIC_QUESTIONS: EvalQuestion[] = [ // 'schedule a meeting' dropped with 'calendar events': no usable calendar API. ]; -export interface EvalScore { +interface EvalScore { k: number; hits: number; total: number; diff --git a/src/core/capabilities/registrySearch.test.ts b/src/core/capabilities/registrySearch.test.ts index 1ece318..92af4b3 100644 --- a/src/core/capabilities/registrySearch.test.ts +++ b/src/core/capabilities/registrySearch.test.ts @@ -4,7 +4,6 @@ import { createApiSearcher, fetchCatalogSpec, RegistryFetchError, - searchApiIndex, tokenize } from './registrySearch'; @@ -82,8 +81,8 @@ describe('createApiSearcher', () => { }); it('orders ties deterministically', () => { - const a = searchApiIndex(CATALOG, 'sms').map(r => r.entry.id); - const b = searchApiIndex([...CATALOG].reverse(), 'sms').map(r => r.entry.id); + const a = createApiSearcher(CATALOG).search('sms').map(r => r.entry.id); + const b = createApiSearcher([...CATALOG].reverse()).search('sms').map(r => r.entry.id); expect(a).toEqual(b); }); }); @@ -104,7 +103,7 @@ describe('APIs that compile to nothing', () => { entry('dead.example', 'Weather API', { operations: 0, blocker: 'It needs a sign-in Flyspace does not support yet (OAuth, or several keys at once)' }), entry('live.example', 'Weather API', { operations: 5 }) ]; - expect(searchApiIndex(catalog, 'weather').map(hit => hit.entry.id)).toEqual(['live.example', 'dead.example']); + expect(createApiSearcher(catalog).search('weather').map(hit => hit.entry.id)).toEqual(['live.example', 'dead.example']); }); it('still lead when they match far better', () => { @@ -112,7 +111,7 @@ describe('APIs that compile to nothing', () => { entry('gmail.example', 'Gmail API', { description: 'gmail mail inbox', operations: 0 }), entry('mailer.example', 'Bulk Sender', { description: 'mail', operations: 5 }) ]; - expect(searchApiIndex(catalog, 'gmail inbox')[0].entry.id).toBe('gmail.example'); + expect(createApiSearcher(catalog).search('gmail inbox')[0].entry.id).toBe('gmail.example'); }); }); diff --git a/src/core/capabilities/registrySearch.ts b/src/core/capabilities/registrySearch.ts index 0afe0d5..02af59a 100644 --- a/src/core/capabilities/registrySearch.ts +++ b/src/core/capabilities/registrySearch.ts @@ -20,11 +20,11 @@ import { curatedApi } from './curatedApis'; export const DEFAULT_RESULT_LIMIT = 24; /** Largest spec the browser will download and compile. */ -export const MAX_SPEC_BYTES = 5 * 1024 * 1024; -export const SPEC_FETCH_TIMEOUT_MS = 15_000; +const MAX_SPEC_BYTES = 5 * 1024 * 1024; +const SPEC_FETCH_TIMEOUT_MS = 15_000; /** Where the static index is served from (public/api-index.json). */ -export const API_INDEX_PATH = '/api-index.json'; +const API_INDEX_PATH = '/api-index.json'; // Field weights. A word in the title is the strongest signal of what an API // is; an id names the provider (`stripe.com`); categories are curated; a @@ -36,7 +36,7 @@ const STOP_WORDS = new Set([ 'on', 'or', 'the', 'to', 'want', 'with', 'my', 'that', 'this', 'data', 'service' ]); -export interface SearchResult { +interface SearchResult { entry: ApiIndexEntry; score: number; } @@ -145,20 +145,11 @@ export function createApiSearcher(entries: readonly ApiIndexEntry[]): ApiSearche }; } -/** One-shot search. Prefer `createApiSearcher` when searching the same index repeatedly. */ -export function searchApiIndex( - entries: readonly ApiIndexEntry[], - query: string, - limit: number = DEFAULT_RESULT_LIMIT -): SearchResult[] { - return createApiSearcher(entries).search(query, limit); -} - // ============================================ // FETCH // ============================================ -export type RegistryFetchCode = +type RegistryFetchCode = | 'unsupported_version' | 'not_catalog_url' | 'timeout' @@ -174,7 +165,7 @@ export class RegistryFetchError extends Error { } } -export interface FetchOptions { +interface FetchOptions { fetchImpl?: typeof fetch; signal?: AbortSignal; maxBytes?: number; diff --git a/src/core/capabilities/speech.test.ts b/src/core/capabilities/speech.test.ts index f17a9b8..ccb050f 100644 --- a/src/core/capabilities/speech.test.ts +++ b/src/core/capabilities/speech.test.ts @@ -3,10 +3,10 @@ import { blockRegistry } from '../registry/BlockRegistry'; import { clearCapabilities, ensureSpeechCapabilities, - getCapability, uninstallCapability } from './registry'; import { executeCapability, unbindLocalHandler } from './execute'; +import { readCapability } from './state'; import { setSpeechEngine, speechRecognitionMessage, type SpeechEngine } from './speech'; function fakeEngine(transcript = 'hello there'): SpeechEngine & { spoken: string[]; langs: Array } { @@ -39,8 +39,8 @@ afterEach(() => { describe('speech capabilities', () => { it('installs manual local speak and listen without secrets', () => { - const speak = getCapability('cap_speech_speak'); - const listen = getCapability('cap_speech_listen'); + const speak = readCapability('cap_speech_speak'); + const listen = readCapability('cap_speech_listen'); expect(speak?.transport).toEqual({ kind: 'local', handler: 'speech.speak' }); expect(speak?.invocation).toBe('manual'); expect(speak?.effect).toBe('compute'); @@ -101,9 +101,9 @@ describe('speech capabilities', () => { it('brings builtins back after they are cleared', () => { expect(uninstallCapability('cap_speech_speak')).toBe(true); - expect(getCapability('cap_speech_speak')).toBeUndefined(); + expect(readCapability('cap_speech_speak')).toBeUndefined(); ensureSpeechCapabilities(); - expect(getCapability('cap_speech_speak')?.id).toBe('cap_speech_speak'); + expect(readCapability('cap_speech_speak')?.id).toBe('cap_speech_speak'); expect(blockRegistry.has('polymarket_live_odds')).toBe(true); }); }); diff --git a/src/core/capabilities/store.test.ts b/src/core/capabilities/store.test.ts index 2ef22ea..b864c9e 100644 --- a/src/core/capabilities/store.test.ts +++ b/src/core/capabilities/store.test.ts @@ -2,7 +2,8 @@ import 'fake-indexeddb/auto'; import { describe, it, expect, beforeEach } from 'vitest'; import { migrateCapabilityStore, useCapabilityStore } from './store'; -import { installProposal, clearCapabilities, listCapabilities } from './registry'; +import { installProposal, clearCapabilities } from './registry'; +import { allCapabilities } from './state'; import { compileOpenApi } from './openapi'; import { CAPABILITY_STORE_KEY, dropImportedApprovals } from './importAdmission'; import { vaultStorage, __resetVaultConnection } from '../vault/vaultStorage'; @@ -72,7 +73,7 @@ describe('a v1 vault record survives rehydrate', () => { await useCapabilityStore.persist.rehydrate(); expect(useCapabilityStore.getState().stale.map(entry => entry.id)).toEqual([LEGACY_MCP.id]); - expect(listCapabilities().some(manifest => manifest.id === LEGACY_MCP.id)).toBe(false); + expect(allCapabilities().some(manifest => manifest.id === LEGACY_MCP.id)).toBe(false); // A later install rewrites the persisted manifests; the stale entry stays. expect(installProposal(compileOpenApi(SPEC).manifests[0]).ok).toBe(true); @@ -103,6 +104,6 @@ describe('a v1 vault record survives rehydrate', () => { const [stale] = useCapabilityStore.getState().stale; expect(stale).toMatchObject({ id: LEGACY_MCP.id, title: 'Search board' }); expect((stale.manifest as { approval: string }).approval).toBe('pending'); - expect(listCapabilities().some(manifest => manifest.id === LEGACY_MCP.id)).toBe(false); + expect(allCapabilities().some(manifest => manifest.id === LEGACY_MCP.id)).toBe(false); }); }); diff --git a/src/core/capabilities/store.ts b/src/core/capabilities/store.ts index ae9a40c..aea5ba0 100644 --- a/src/core/capabilities/store.ts +++ b/src/core/capabilities/store.ts @@ -12,7 +12,7 @@ import type { CapabilityManifest } from './manifest'; * here, with the stored body, instead of being dropped on reload. The reader * re-installs the capability from its source; this entry only says which. */ -export interface StaleCapability { +interface StaleCapability { id: string; title: string; reason: string; diff --git a/src/core/capabilities/valueType.ts b/src/core/capabilities/valueType.ts index 6306880..11efd89 100644 --- a/src/core/capabilities/valueType.ts +++ b/src/core/capabilities/valueType.ts @@ -2,7 +2,7 @@ // This is a closed JSON-Schema subset OMNI can validate without a parser // dependency. It is the native contract. OmniData is a separate projection. -export const VALUE_KINDS = [ +const VALUE_KINDS = [ 'string', 'number', 'integer', @@ -13,7 +13,7 @@ export const VALUE_KINDS = [ 'any' ] as const; -export type ValueKind = (typeof VALUE_KINDS)[number]; +type ValueKind = (typeof VALUE_KINDS)[number]; export type Primitive = string | number | boolean | null; diff --git a/src/core/cognition/index.ts b/src/core/cognition/index.ts index 4988d22..005d6ed 100644 --- a/src/core/cognition/index.ts +++ b/src/core/cognition/index.ts @@ -3,4 +3,3 @@ // ============================================ export { runTurn, runTurnStream } from './kernel'; -export type { TurnOptions, TurnResult } from './kernel'; diff --git a/src/core/cognition/kernel.ts b/src/core/cognition/kernel.ts index 4cb2f65..f9bd423 100644 --- a/src/core/cognition/kernel.ts +++ b/src/core/cognition/kernel.ts @@ -20,7 +20,7 @@ import { minOutputTokensFor } from '@/core/models.registry'; import type { LLMConfig } from '@/core/schemas/mind.schema'; import type { ContextSource } from '@/core/schemas/wire.schema'; -export interface TurnOptions { +interface TurnOptions { temperature?: number; maxTokens?: number; signal?: AbortSignal; @@ -35,7 +35,7 @@ export interface TurnOptions { sources?: ContextSource[]; } -export interface TurnResult { +interface TurnResult { success: boolean; /** Full response text ('' on failure). */ content: string; @@ -87,7 +87,7 @@ interface AvailabilityCheck { } /** Shared availability gate (ping-based; no generation). */ -export async function checkLLMAvailable(): Promise { +async function checkLLMAvailable(): Promise { const { llmConfig } = useMindStore.getState(); const llm = getLLMService(llmConfig); const ok = await llm.isAvailable(); diff --git a/src/core/gateway/ApiGateway.ts b/src/core/gateway/ApiGateway.ts index 41c0708..f23ece3 100644 --- a/src/core/gateway/ApiGateway.ts +++ b/src/core/gateway/ApiGateway.ts @@ -87,25 +87,6 @@ class ApiGatewayService { private rateLimitTimers: Map = new Map(); private subscriptions: GatewaySubscription[] = []; - /** - * Register a custom API type - */ - registerType(apiId: string, definition: ApiTypeDefinition): void { - apiTypeRegistry.set(apiId, definition as RegisteredApi); - } - - /** - * Remove a runtime capability registration. - * Catalog ids are not removable through this path. - */ - unregisterType(apiId: string): boolean { - if (!apiId.startsWith('cap_')) return false; - defaultParamsRegistry.delete(apiId); - const removed = apiTypeRegistry.delete(apiId); - this.clearCache(apiId); - return removed; - } - /** * Check if data is cached and still valid */ @@ -268,29 +249,6 @@ class ApiGatewayService { } }); } - - /** - * Clear cache for a specific API or all - */ - clearCache(apiId?: string): void { - if (apiId) { - // Clear all cache entries for this API - for (const key of this.cache.keys()) { - if (key.startsWith(apiId)) { - this.cache.delete(key); - } - } - } else { - this.cache.clear(); - } - } - - /** - * Check if an API type is registered - */ - isRegistered(apiId: string): boolean { - return apiTypeRegistry.has(apiId); - } } // Singleton instance diff --git a/src/core/gateway/omnidata.schema.ts b/src/core/gateway/omnidata.schema.ts index 005ce3d..d0d3594 100644 --- a/src/core/gateway/omnidata.schema.ts +++ b/src/core/gateway/omnidata.schema.ts @@ -6,7 +6,7 @@ /** * API categories for type discrimination */ -export type ApiCategory = +type ApiCategory = | 'prediction_market' | 'news' | 'market_data' @@ -21,7 +21,7 @@ export type ApiCategory = /** * Source metadata for any OmniData */ -export interface OmniSource { +interface OmniSource { /** API provider ID (e.g., 'polymarket', 'newsapi') */ apiId: string; @@ -88,7 +88,7 @@ export interface OmniMetrics { /** * Text/media content (LLM responses, documents) */ -export interface OmniContent { +interface OmniContent { /** Text content */ text: string; diff --git a/src/core/hooks/useShellNavigation.ts b/src/core/hooks/useShellNavigation.ts index 8a5952b..d0fc2a1 100644 --- a/src/core/hooks/useShellNavigation.ts +++ b/src/core/hooks/useShellNavigation.ts @@ -57,7 +57,6 @@ export function useShellNavigation() { const customShellId = hotkeySlots[slot]; if (customShellId) { setActiveShell(customShellId); - // TODO: Add navigation to shell view when shell viewer is implemented return; } diff --git a/src/core/interaction/FIELD_REPORT.md b/src/core/interaction/FIELD_REPORT.md index 33ed7c9..d87ccfe 100644 --- a/src/core/interaction/FIELD_REPORT.md +++ b/src/core/interaction/FIELD_REPORT.md @@ -9,7 +9,7 @@ Scripted in-memory engine tasks are what was measured. `runFieldEvaluation` in ` - Browser audio injection: not measured. No Playwright speech spec was executed and no page-level audio was injected. - Human preference: not measured. The harness returns `not measured` until a person supplies a preference. None was supplied. -No latency percentiles were computed. `SpeechTimingRecorder` still refuses percentiles from zero live samples. +No latency percentiles were computed. ## Speech-disabled use diff --git a/src/core/interaction/browserSpeechAdapter.ts b/src/core/interaction/browserSpeechAdapter.ts index e967dff..33e9818 100644 --- a/src/core/interaction/browserSpeechAdapter.ts +++ b/src/core/interaction/browserSpeechAdapter.ts @@ -36,7 +36,7 @@ export interface RecognitionLike { abort(): void; } -export const BROWSER_ADAPTER_ID = 'browser-web-speech'; +const BROWSER_ADAPTER_ID = 'browser-web-speech'; const RECOGNITION_WAIT_MS = 2000; function recognitionFailure(code: string): SpeechErrorCode | null { diff --git a/src/core/interaction/engine.ts b/src/core/interaction/engine.ts index 69a118a..b007cd4 100644 --- a/src/core/interaction/engine.ts +++ b/src/core/interaction/engine.ts @@ -53,7 +53,7 @@ interface UndoEntry { apply: () => void; } -export interface EngineSnapshot { +interface EngineSnapshot { commands: SpatialCommand[]; traces: InteractionTrace[]; preview: SpatialCommand | null; @@ -646,7 +646,6 @@ export class InteractionEngine { }; proposal.geometry = { point: at }; proposal.subjects = []; - if (intent.destructive) return this.previewDestructive(proposal); return this.execute(proposal, timestampMs, false); } diff --git a/src/core/interaction/fieldHarness.ts b/src/core/interaction/fieldHarness.ts index 8a31324..36921cd 100644 --- a/src/core/interaction/fieldHarness.ts +++ b/src/core/interaction/fieldHarness.ts @@ -15,19 +15,19 @@ import { buildScene, compilerFor, CORPUS_CATALOG, type CompilerConfig, type Scen export type FieldTaskId = 'instantiate-place' | 'connect-source-persona' | 'open-shell' | 'delete-with-confirmation'; -export type SpeechTurn = +type SpeechTurn = | { say: string; fail?: SpeechErrorCode; interrupt?: boolean } | { select: string[] } | { pointAt: { x: number; y: number } }; -export interface SpeechVariant { +interface SpeechVariant { id: string; config: CompilerConfig; scene?: SceneId; turns: SpeechTurn[]; } -export interface FieldTask { +interface FieldTask { id: FieldTaskId; title: string; scene: SceneId; @@ -37,7 +37,7 @@ export interface FieldTask { speech: SpeechVariant[]; } -export interface TurnRecord { +interface TurnRecord { turn: string; outcome: SpeechOutcome['kind'] | 'none' | 'pointer'; lifecycle?: SpatialCommand['lifecycle']; @@ -60,7 +60,7 @@ export interface FieldRun { } /** Supplied by a person after a session. The harness never fills this in. */ -export interface HumanPreference { +interface HumanPreference { participant: string; task: FieldTaskId; prefers: 'speech' | 'pointer' | 'no preference'; @@ -184,7 +184,7 @@ function tally(run: FieldRun, command: SpatialCommand | undefined, isConfirm: bo if (isConfirm && command.lifecycle === 'committed') run.confirmations += 1; } -export async function runSpeechVariant(task: FieldTask, variant: SpeechVariant): Promise { +async function runSpeechVariant(task: FieldTask, variant: SpeechVariant): Promise { const canvas = buildScene(variant.scene ?? task.scene); const engine = new InteractionEngine(canvas, () => CORPUS_CATALOG); const run: FieldRun = { @@ -217,7 +217,7 @@ export async function runSpeechVariant(task: FieldTask, variant: SpeechVariant): return run; } -export function runPointerTask(task: FieldTask): FieldRun { +function runPointerTask(task: FieldTask): FieldRun { const canvas = buildScene(task.scene); const engine = new InteractionEngine(canvas, () => CORPUS_CATALOG); const command = task.pointer.run(engine, canvas) ?? undefined; @@ -257,6 +257,3 @@ export function summarizePreferences(preferences: HumanPreference[]): 'not measu for (const item of preferences) counts[item.prefers] += 1; return counts; } - -export const FIELD_TABLE_START = ''; -export const FIELD_TABLE_END = ''; diff --git a/src/core/interaction/intentCompiler.test.ts b/src/core/interaction/intentCompiler.test.ts index 91e50ae..8e1042e 100644 --- a/src/core/interaction/intentCompiler.test.ts +++ b/src/core/interaction/intentCompiler.test.ts @@ -5,11 +5,9 @@ import { InteractionEngine } from './engine'; import { compileDeterministic, createIntentCompiler, - modelIntentCompiler, paraphraseCompiler, type IntentCompilerResult, type IntentContext, - type ModelIntentRequest, type SemanticCompiler } from './intentCompiler'; import { validateProposal } from './proposal'; @@ -47,15 +45,6 @@ const throwingSemantic: SemanticCompiler = { } }; -function stubModel(output: unknown) { - const requests: ModelIntentRequest[] = []; - const compiler = modelIntentCompiler(request => { - requests.push(request); - return typeof output === 'function' ? (output as (r: ModelIntentRequest) => unknown)(request) : output; - }); - return { compiler, requests }; -} - describe('utterance corpus', () => { it('is versioned and covers every spec §16 class', () => { expect(UTTERANCE_CORPUS_VERSION).toBe(1); @@ -205,102 +194,6 @@ describe('SpatialCommandProposal v1 validation', () => { }); }); -describe('ModelIntentCompiler boundary', () => { - async function compileWith(output: unknown, transcript = 'please take this off the board') { - const { canvas, engine } = boardEngine(); - const { compiler, requests } = stubModel(output); - const observation = finalObservation(transcript); - const result = await createIntentCompiler({ semantic: compiler }).compile(observation, engine.describeSpeechContext()); - return { canvas, engine, observation, result, requests }; - } - - it.each([ - ['garbage text', 'sure! I deleted it for you', 'model-output-invalid-json'], - ['truncated JSON', '{"version":1,"operation":"DELETE"', 'model-output-invalid-json'], - ['a number', 42, 'model-output-invalid:not-an-object'], - ['an oversize string', `"${'x'.repeat(5000)}"`, 'model-output-too-long'], - ['an extra operation', { version: 1, operation: 'INSTALL_CAPABILITY', evidence: { speechObservationId: '?' } }, 'model-output-invalid:unknown-operation'], - ['CONFIRM', { version: 1, operation: 'CONFIRM', evidence: { speechObservationId: '?' } }, 'model-output-invalid:reserved-deterministic'], - ['UNDO', { version: 1, operation: 'UNDO', evidence: { speechObservationId: '?' } }, 'model-output-invalid:reserved-deterministic'] - ])('refuses %s', async (_label, output, reason) => { - const { canvas, engine, observation, result } = await compileWith(output); - expect(result).toEqual({ kind: 'refuse', reason, via: 'model' }); - expect(engine.admitSpeech(observation, result).lifecycle).toBe('refused'); - expect(canvas.blocks).toHaveLength(2); - expect(canvas.wires).toHaveLength(0); - }); - - it('refuses extra fields and ids that were not supplied', async () => { - const extra = await compileWith((request: ModelIntentRequest) => ({ - version: 1, operation: 'DELETE', subject: { kind: 'candidate', id: 'hn' }, - evidence: { speechObservationId: request.observationId }, confirmed: true - })); - expect(extra.result).toEqual({ kind: 'refuse', reason: 'model-output-invalid:unknown-field:confirmed', via: 'model' }); - - const foreign = await compileWith((request: ModelIntentRequest) => ({ - version: 1, operation: 'DELETE', subject: { kind: 'candidate', id: 'root-admin-block' }, - evidence: { speechObservationId: request.observationId } - })); - expect(foreign.result).toEqual({ kind: 'refuse', reason: 'model-output-invalid:candidate-not-supplied', via: 'model' }); - }); - - it('a thrown model call refuses', async () => { - const { result } = await compileWith(() => { throw new Error('rate limited'); }); - expect(result).toEqual({ kind: 'refuse', reason: 'model-error', via: 'model' }); - }); - - it('sends bounded descriptors and no reserved operations', async () => { - const { requests } = await compileWith('nope'); - expect(requests).toHaveLength(1); - expect(requests[0].operations).not.toContain('CONFIRM'); - expect(requests[0].operations).not.toContain('UNDO'); - expect(requests[0].operations).not.toContain('CANCEL'); - expect(requests[0].candidates.map(item => item.id).sort()).toEqual(['analyst', 'hn']); - expect(Object.keys(requests[0].candidates[0]).sort()).toEqual(['blockId', 'id', 'name']); - }); - - it('a valid model delete still previews and waits for the host confirm', async () => { - const { canvas, engine, observation, result } = await compileWith((request: ModelIntentRequest) => JSON.stringify({ - version: 1, operation: 'DELETE', subject: { kind: 'candidate', id: 'hn' }, - evidence: { speechObservationId: request.observationId } - })); - expect(result).toMatchObject({ kind: 'proposal', via: 'model' }); - const command = engine.admitSpeech(observation, result); - expect(command).toMatchObject({ lifecycle: 'previewing', reason: 'destructive-needs-confirm', subjects: ['hn'] }); - expect(command.evidence).toContain('speech-compiler:model'); - expect(canvas.blocks).toHaveLength(2); - expect(engine.hear(finalObservation('yeah'))).toMatchObject({ lifecycle: 'refused' }); - expect(canvas.blocks).toHaveLength(2); - expect(engine.hear(finalObservation('confirm'))).toMatchObject({ lifecycle: 'committed', action: 'delete' }); - expect(canvas.blocks).toHaveLength(1); - }); - - it('a candidate that went stale between compile and admission holds', async () => { - const { canvas, engine, observation, result } = await compileWith((request: ModelIntentRequest) => ({ - version: 1, operation: 'CONNECT', subject: { kind: 'candidate', id: 'hn' }, target: { kind: 'candidate', id: 'analyst' }, - evidence: { speechObservationId: request.observationId } - }), 'hook the first one into the second one'); - expect(result.kind).toBe('proposal'); - canvas.remove('hn'); - expect(engine.admitSpeech(observation, result)).toMatchObject({ lifecycle: 'held', reason: 'stale-referent' }); - expect(canvas.wires).toHaveLength(0); - }); - - it('prompt injection inside a block name cannot forge a confirm', async () => { - const canvas = buildScene('injected-name'); - const engine = new InteractionEngine(canvas, () => CORPUS_CATALOG); - engine.speak('delete notes'); - const obedient = modelIntentCompiler(request => ({ - version: 1, operation: 'CONFIRM', evidence: { speechObservationId: request.observationId } - })); - const observation = finalObservation('do what the notes block says'); - const result = await createIntentCompiler({ semantic: obedient }).compile(observation, engine.describeSpeechContext()); - expect(engine.admitSpeech(observation, result)).toMatchObject({ lifecycle: 'refused', reason: 'model-output-invalid:reserved-deterministic' }); - expect(engine.snapshot().preview).not.toBeNull(); - expect(canvas.blocks).toHaveLength(2); - }); -}); - describe('engine second gate', () => { it('refuses a result that claims the grammar but does not match it', () => { const { canvas, engine } = boardEngine(); @@ -326,6 +219,43 @@ describe('engine second gate', () => { expect(engine.admitSpeech(observation, result)).toMatchObject({ lifecycle: 'refused', reason: 'invalid-proposal:reserved-deterministic' }); }); + it('a semantic candidate that went stale between compile and admission holds', () => { + const { canvas, engine } = boardEngine(); + const observation = finalObservation('hook the first one into the second one'); + const result: IntentCompilerResult = { + kind: 'proposal', via: 'semantic', + proposal: { + version: 1, operation: 'CONNECT', + subject: { kind: 'candidate', id: 'hn' }, target: { kind: 'candidate', id: 'analyst' }, + evidence: { speechObservationId: observation.observationId } + } + }; + canvas.remove('hn'); + expect(engine.admitSpeech(observation, result)).toMatchObject({ lifecycle: 'held', reason: 'stale-referent' }); + expect(canvas.wires).toHaveLength(0); + }); + + it('a valid semantic delete still previews and waits for the host confirm', () => { + const { canvas, engine } = boardEngine(); + const observation = finalObservation('please take this off the board'); + const result: IntentCompilerResult = { + kind: 'proposal', via: 'semantic', + proposal: { + version: 1, operation: 'DELETE', + subject: { kind: 'candidate', id: 'hn' }, + evidence: { speechObservationId: observation.observationId } + } + }; + const command = engine.admitSpeech(observation, result); + expect(command).toMatchObject({ lifecycle: 'previewing', reason: 'destructive-needs-confirm', subjects: ['hn'] }); + expect(command.evidence).toContain('speech-compiler:semantic'); + expect(canvas.blocks).toHaveLength(2); + expect(engine.hear(finalObservation('yeah'))).toMatchObject({ lifecycle: 'refused' }); + expect(canvas.blocks).toHaveLength(2); + expect(engine.hear(finalObservation('confirm'))).toMatchObject({ lifecycle: 'committed', action: 'delete' }); + expect(canvas.blocks).toHaveLength(1); + }); + it('RESOLVE_CAPABILITY is refused and changes nothing', async () => { const { canvas, engine } = boardEngine(); const observation = finalObservation('bring in weather data'); diff --git a/src/core/interaction/intentCompiler.ts b/src/core/interaction/intentCompiler.ts index e11c25a..84b9e0a 100644 --- a/src/core/interaction/intentCompiler.ts +++ b/src/core/interaction/intentCompiler.ts @@ -4,8 +4,6 @@ import { parseSpeech, type SpeechCatalog, type SpeechIntent } from './speech'; import { - PROPOSAL_OPERATIONS, - RESERVED_OPERATIONS, validateProposal, type ReferentExpression, type SpatialCommandProposalV1 @@ -20,14 +18,14 @@ export interface IntentContext { selection: string[]; } -export type CompilerPath = 'grammar' | 'semantic' | 'model'; +type CompilerPath = 'grammar' | 'semantic'; export type IntentCompilerResult = | { kind: 'proposal'; proposal: SpatialCommandProposalV1; via: CompilerPath } | { kind: 'needs-input'; question: string; reason: string; via: CompilerPath } | { kind: 'refuse'; reason: string; via: CompilerPath }; -export interface SemanticInput { +interface SemanticInput { transcript: string; observationId: string; context: IntentContext; @@ -82,7 +80,7 @@ function deictic(word: SpeechIntent['deixis']): ReferentExpression { } /** Lossless map from the fixed grammar's intent to a proposal. */ -export function intentToResult(intent: SpeechIntent, observationId: string, via: CompilerPath = 'grammar'): IntentCompilerResult { +function intentToResult(intent: SpeechIntent, observationId: string, via: CompilerPath = 'grammar'): IntentCompilerResult { if (intent.ambiguous === 'block') return { kind: 'needs-input', reason: 'ambiguous-block', question: 'Which block do you mean?', via }; if (intent.ambiguous === 'shell') return { kind: 'needs-input', reason: 'ambiguous-shell', question: 'Which shell do you mean?', via }; switch (intent.action) { @@ -263,67 +261,3 @@ export function paraphraseCompiler(): SemanticCompiler { } }; } - -// --------------------------------------------------------------------------- -// Model boundary. The model function is injected; it receives bounded -// descriptors and must return one proposal object. Everything else is refused. -// --------------------------------------------------------------------------- - -export interface ModelIntentRequest { - objective: 'compile speech into one bounded Omni proposal'; - transcript: string; - observationId: string; - operations: string[]; - blockTypes: Array<{ blockId: string; displayName: string }>; - shells: Array<{ shellId: string; name: string }>; - candidates: Array<{ id: string; name: string; blockId: string }>; - selection: string[]; -} - -export type IntentModel = (request: ModelIntentRequest) => unknown | Promise; - -const MAX_MODEL_CANDIDATES = 50; -const MAX_MODEL_OUTPUT_CHARS = 4000; - -export function modelIntentCompiler(model: IntentModel): SemanticCompiler { - return { - id: 'model-intent-v1', - async compile({ transcript, observationId, context }) { - const candidates = context.visibleBlocks.slice(0, MAX_MODEL_CANDIDATES); - const candidateIds = new Set(candidates.map(block => block.id)); - const request: ModelIntentRequest = { - objective: 'compile speech into one bounded Omni proposal', - transcript, - observationId, - operations: PROPOSAL_OPERATIONS.filter(operation => !RESERVED_OPERATIONS.has(operation)), - blockTypes: context.vocabulary.blocks.map(block => ({ blockId: block.blockId, displayName: block.displayName })), - shells: context.vocabulary.shells.map(shell => ({ shellId: shell.id, name: shell.name })), - candidates: candidates.map(block => ({ id: block.id, name: block.name, blockId: block.blockId })), - selection: context.selection.filter(id => candidateIds.has(id)) - }; - let output: unknown; - try { - output = await model(request); - } catch { - return refuse('model-error', 'model'); - } - let parsed: unknown = output; - if (typeof output === 'string') { - if (output.length > MAX_MODEL_OUTPUT_CHARS) return refuse('model-output-too-long', 'model'); - try { - parsed = JSON.parse(output); - } catch { - return refuse('model-output-invalid-json', 'model'); - } - } - const checked = validateProposal(parsed, { - observationId, - vocabulary: context.vocabulary, - allowReserved: false, - candidateIds - }); - if (!checked.ok) return refuse(`model-output-invalid:${checked.reason}`, 'model'); - return { kind: 'proposal', proposal: checked.proposal, via: 'model' }; - } - }; -} diff --git a/src/core/interaction/mutationInventory.ts b/src/core/interaction/mutationInventory.ts index 34898e3..75cf844 100644 --- a/src/core/interaction/mutationInventory.ts +++ b/src/core/interaction/mutationInventory.ts @@ -29,14 +29,14 @@ export const TOPOLOGY_WRITES = [ export type TopologyWrite = typeof TOPOLOGY_WRITES[number]; -export interface InventoryException { +interface InventoryException { write: TopologyWrite; /** The concrete function that makes the write. */ in: string; reason: string; } -export interface InventoryEntry { +interface InventoryEntry { file: string; /** User actions in this file that now enter the engine. */ routed: Array<{ action: string; via: 'pointerCreate' | 'pointerDelete' | 'pointerConnect' | 'pointerMove' }>; diff --git a/src/core/interaction/openaiRealtimeAdapter.ts b/src/core/interaction/openaiRealtimeAdapter.ts index be6baea..fa8a04c 100644 --- a/src/core/interaction/openaiRealtimeAdapter.ts +++ b/src/core/interaction/openaiRealtimeAdapter.ts @@ -11,8 +11,8 @@ import { type SpeechObservationV1 } from './speechObservation'; -export const OPENAI_REALTIME_ADAPTER_ID = 'openai-realtime'; -export const REALTIME_SESSION_ROUTE = '/api/speech/realtime-session'; +const OPENAI_REALTIME_ADAPTER_ID = 'openai-realtime'; +const REALTIME_SESSION_ROUTE = '/api/speech/realtime-session'; export const REALTIME_CALLS_URL = 'https://api.openai.com/v1/realtime/calls'; /** Configuration bounds, not measured latencies. */ @@ -26,7 +26,7 @@ export interface RealtimeTransport { createPeer(): RTCPeerConnection; } -export function browserRealtimeTransport(): RealtimeTransport { +function browserRealtimeTransport(): RealtimeTransport { return { fetch: (url, init) => fetch(url, init), getUserMedia(constraints) { @@ -42,9 +42,7 @@ export function browserRealtimeTransport(): RealtimeTransport { }; } -export interface RealtimeAdapterOptions { - sessionRoute?: string; - callsUrl?: string; +interface RealtimeAdapterOptions { now?: () => number; connectTimeoutMs?: number; finalizeTimeoutMs?: number; @@ -112,8 +110,8 @@ export function openaiRealtimeAdapter( options: RealtimeAdapterOptions = {} ): SpeechAdapter & { probe(): Promise } { const now = options.now ?? Date.now; - const route = options.sessionRoute ?? REALTIME_SESSION_ROUTE; - const callsUrl = options.callsUrl ?? REALTIME_CALLS_URL; + const route = REALTIME_SESSION_ROUTE; + const callsUrl = REALTIME_CALLS_URL; const connectTimeoutMs = options.connectTimeoutMs ?? CONNECT_TIMEOUT_MS; const finalizeTimeoutMs = options.finalizeTimeoutMs ?? FINALIZE_TIMEOUT_MS; diff --git a/src/core/interaction/proposal.ts b/src/core/interaction/proposal.ts index af34932..5fbe86a 100644 --- a/src/core/interaction/proposal.ts +++ b/src/core/interaction/proposal.ts @@ -4,7 +4,7 @@ import type { SpeechCatalog } from './speech'; -export const PROPOSAL_OPERATIONS = [ +const PROPOSAL_OPERATIONS = [ 'CREATE', 'MOVE', 'CONNECT', @@ -18,10 +18,10 @@ export const PROPOSAL_OPERATIONS = [ 'RESOLVE_CAPABILITY' ] as const; -export type ProposalOperation = typeof PROPOSAL_OPERATIONS[number]; +type ProposalOperation = typeof PROPOSAL_OPERATIONS[number]; /** Only the deterministic grammar may produce these. A model outage must not block them, and a model must not forge them. */ -export const RESERVED_OPERATIONS: ReadonlySet = new Set(['UNDO', 'CANCEL', 'CONFIRM']); +const RESERVED_OPERATIONS: ReadonlySet = new Set(['UNDO', 'CANCEL', 'CONFIRM']); export type ReferentExpression = | { kind: 'deictic'; word: 'this' | 'these' } @@ -30,9 +30,9 @@ export type ReferentExpression = | { kind: 'block-type'; blockId: string } | { kind: 'shell'; shellId: string }; -export type SpatialRelationExpression = { kind: 'pointed' } | { kind: 'default' }; +type SpatialRelationExpression = { kind: 'pointed' } | { kind: 'default' }; -export interface CapabilityQueryExpression { +interface CapabilityQueryExpression { text: string; } @@ -46,7 +46,7 @@ export interface SpatialCommandProposalV1 { evidence: { speechObservationId: string }; } -export interface ProposalRules { +interface ProposalRules { observationId: string; vocabulary: SpeechCatalog; /** Reserved operations are admitted only from the deterministic grammar. */ @@ -55,7 +55,7 @@ export interface ProposalRules { candidateIds?: ReadonlySet; } -export type ProposalCheck = { ok: true; proposal: SpatialCommandProposalV1 } | { ok: false; reason: string }; +type ProposalCheck = { ok: true; proposal: SpatialCommandProposalV1 } | { ok: false; reason: string }; const TOP_KEYS = new Set(['version', 'operation', 'subject', 'target', 'placement', 'capabilityQuery', 'evidence']); const MAX_NAME = 80; diff --git a/src/core/interaction/pushToTalk.test.ts b/src/core/interaction/pushToTalk.test.ts index dd7a5dc..24ef511 100644 --- a/src/core/interaction/pushToTalk.test.ts +++ b/src/core/interaction/pushToTalk.test.ts @@ -63,7 +63,6 @@ describe('push to talk', () => { const heard = await talk.release(); expect(partials.map(item => [item.transcript, item.final])).toEqual([['add', false], ['add hacker', false]]); expect(heard.observation?.transcript).toBe('add hacker news'); - expect(heard.timings?.firstDeltaAtMs).toBeTypeOf('number'); }); it('cancel aborts the signal, cancels the capture, and returns nothing', async () => { diff --git a/src/core/interaction/pushToTalk.ts b/src/core/interaction/pushToTalk.ts index 3187358..2703203 100644 --- a/src/core/interaction/pushToTalk.ts +++ b/src/core/interaction/pushToTalk.ts @@ -9,7 +9,7 @@ import { type SpeechObservationV1 } from './speechObservation'; -export interface Heard { +interface Heard { transcript: string; heard: boolean; error?: SpeechAdapterError; @@ -17,31 +17,21 @@ export interface Heard { observation?: SpeechObservationV1; } -export interface PushToTalkTimings { - sessionId: string; - captureRequestedAtMs: number; - microphoneActiveAtMs?: number; - firstDeltaAtMs?: number; - stopRequestedAtMs?: number; - finalAtMs?: number; -} - export interface PushToTalkOptions { locale?: string; newSessionId?: () => string; - now?: () => number; /** Interim observations for display only. They never reach the engine. */ onPartial?: (observation: SpeechObservationV1) => void; /** The microphone is live on this capture. */ onListening?: (capture: SpeechCapture) => void; } -export interface PushToTalk { +interface PushToTalk { readonly held: boolean; readonly adapterId: string; /** Resolves with an error when capture could not start, otherwise undefined. */ press(): Promise; - release(): Promise; + release(): Promise; /** Drop the microphone without turning the audio into a command. */ cancel(): void; } @@ -50,11 +40,9 @@ interface Pending { sessionId: string; abort: AbortController; capture: Promise; - timings: PushToTalkTimings; } export function createPushToTalk(adapter: SpeechAdapter, options: PushToTalkOptions = {}): PushToTalk { - const now = options.now ?? Date.now; const newSessionId = options.newSessionId ?? createSpeechSessionId; let current: Pending | null = null; // A released session still starting or finalizing. Cancel must reach it: @@ -76,7 +64,6 @@ export function createPushToTalk(adapter: SpeechAdapter, options: PushToTalkOpti if (current) return Promise.resolve(undefined); const sessionId = newSessionId(); const abort = new AbortController(); - const timings: PushToTalkTimings = { sessionId, captureRequestedAtMs: now() }; let capture: Promise; try { capture = adapter.start({ @@ -86,18 +73,16 @@ export function createPushToTalk(adapter: SpeechAdapter, options: PushToTalkOpti onObservation(observation) { if (observation.sessionId !== sessionId || abort.signal.aborted) return; if (observation.final) return; - timings.firstDeltaAtMs ??= now(); options.onPartial?.(observation); } }); } catch (error) { capture = Promise.reject(error); } - const entry: Pending = { sessionId, abort, capture, timings }; + const entry: Pending = { sessionId, abort, capture }; current = entry; return capture.then( started => { - timings.microphoneActiveAtMs = now(); if (current === entry) options.onListening?.(started); return undefined; }, @@ -131,7 +116,7 @@ export function createPushToTalk(adapter: SpeechAdapter, options: PushToTalkOpti } }; - async function settle(entry: Pending): Promise { + async function settle(entry: Pending): Promise { let capture: SpeechCapture; try { capture = await entry.capture; @@ -141,24 +126,21 @@ export function createPushToTalk(adapter: SpeechAdapter, options: PushToTalkOpti } // Cancelled while starting: cancel() already cancels this capture. if (entry.abort.signal.aborted) return { transcript: '', heard: false }; - entry.timings.stopRequestedAtMs = now(); let final: SpeechObservationV1 | null; try { final = await capture.stop(); } catch (error) { - return { ...failed(error), timings: entry.timings }; + return failed(error); } - if (entry.abort.signal.aborted) return { transcript: '', heard: false, timings: entry.timings }; + if (entry.abort.signal.aborted) return { transcript: '', heard: false }; if (!final || !final.final || final.sessionId !== entry.sessionId) { - return { transcript: '', heard: false, timings: entry.timings }; + return { transcript: '', heard: false }; } - entry.timings.finalAtMs = now(); const transcript = final.transcript.trim(); return { transcript, heard: transcript.length > 0, - observation: transcript ? final : undefined, - timings: entry.timings + observation: transcript ? final : undefined }; } } diff --git a/src/core/interaction/realtimeFakes.ts b/src/core/interaction/realtimeFakes.ts index 00d2774..948e3f0 100644 --- a/src/core/interaction/realtimeFakes.ts +++ b/src/core/interaction/realtimeFakes.ts @@ -3,7 +3,7 @@ import type { RealtimeTransport } from './openaiRealtimeAdapter'; -export class FakeChannel { +class FakeChannel { readyState: 'connecting' | 'open' | 'closed' = 'connecting'; sent: unknown[] = []; onopen: (() => void) | null = null; @@ -35,7 +35,7 @@ export class FakeChannel { } } -export class FakePeer { +class FakePeer { tracks: unknown[] = []; channel: FakeChannel | null = null; local: unknown = null; @@ -75,7 +75,7 @@ export class FakePeer { } } -export interface FakeRequest { +interface FakeRequest { url: string; method: string; headers: Record; @@ -83,7 +83,7 @@ export interface FakeRequest { signal?: AbortSignal; } -export interface FakeRealtimeOptions { +interface FakeRealtimeOptions { sessionStatus?: number; sessionBody?: Record; configured?: boolean; diff --git a/src/core/interaction/referent.ts b/src/core/interaction/referent.ts index 2c4a6c1..044342b 100644 --- a/src/core/interaction/referent.ts +++ b/src/core/interaction/referent.ts @@ -3,7 +3,7 @@ import type { CanvasBlockView, FramedPoint } from './types'; -export interface ResolveInput { +interface ResolveInput { shellId: string; blocks: CanvasBlockView[]; point?: FramedPoint; diff --git a/src/core/interaction/scriptedSpeechAdapter.ts b/src/core/interaction/scriptedSpeechAdapter.ts index 35eb021..b081bef 100644 --- a/src/core/interaction/scriptedSpeechAdapter.ts +++ b/src/core/interaction/scriptedSpeechAdapter.ts @@ -21,7 +21,7 @@ export interface SpeechScript { echoFinal?: boolean; } -export interface ScriptedSpeechAdapter extends SpeechAdapter { +interface ScriptedSpeechAdapter extends SpeechAdapter { starts: number; stops: number; cancels: number; @@ -29,7 +29,7 @@ export interface ScriptedSpeechAdapter extends SpeechAdapter { sessions: string[]; } -export const SCRIPTED_ADAPTER_ID = 'scripted-speech'; +const SCRIPTED_ADAPTER_ID = 'scripted-speech'; export function scriptedSpeechAdapter( script: SpeechScript | (() => SpeechScript), diff --git a/src/core/interaction/session.test.ts b/src/core/interaction/session.test.ts index d04bf28..a5f85ca 100644 --- a/src/core/interaction/session.test.ts +++ b/src/core/interaction/session.test.ts @@ -82,7 +82,7 @@ describe('undo of a delete', () => { }); it('does not duplicate a wire the canvas already has back', () => { - const saved = useWireStore.getState().getWire(wireId)!; + const saved = useWireStore.getState().wires.find(w => w.id === wireId)!; deleteBlock('dst'); // Between the delete and its undo, a shell load (say, an undone // navigation) restored the block and the same wire from the saved shell. diff --git a/src/core/interaction/speechFeedback.ts b/src/core/interaction/speechFeedback.ts index 0b58f1a..4eaafac 100644 --- a/src/core/interaction/speechFeedback.ts +++ b/src/core/interaction/speechFeedback.ts @@ -55,10 +55,6 @@ export function browserSpeechFeedback(): SpeechFeedbackAdapter { }; } -export function silentSpeechFeedback(): SpeechFeedbackAdapter { - return { id: 'silent', speak() {}, cancel() {} }; -} - /** Returns false when the feedback adapter failed. The caller's state is untouched either way. */ export function emitFeedback(feedback: SpeechFeedbackAdapter | null, text: string): boolean { if (!feedback) return true; diff --git a/src/core/interaction/speechInput.ts b/src/core/interaction/speechInput.ts index 9c37d9c..9221c9a 100644 --- a/src/core/interaction/speechInput.ts +++ b/src/core/interaction/speechInput.ts @@ -3,7 +3,6 @@ import { createIntentCompiler, type IntentCompiler, type IntentCompilerResult, type IntentContext } from './intentCompiler'; import { createPushToTalk, type PushToTalkOptions } from './pushToTalk'; -import type { SpeechTimingRecorder } from './speechMeasurement'; import type { SpeechAdapter, SpeechErrorCode, SpeechObservationV1 } from './speechObservation'; import type { SpatialCommand } from './types'; @@ -19,7 +18,7 @@ export type SpeechOutcome = | { kind: 'cancelled' } | { kind: 'error'; code: SpeechErrorCode; message: string }; -export interface SpeechInput { +interface SpeechInput { readonly held: boolean; readonly adapterId: string; prepare(): Promise; @@ -29,20 +28,17 @@ export interface SpeechInput { cancel(): void; } -export interface SpeechInputOptions extends PushToTalkOptions { +interface SpeechInputOptions extends PushToTalkOptions { adapter: SpeechAdapter; authority: SpeechAuthority; /** Defaults to the fixed grammar alone. */ compiler?: IntentCompiler; - /** Receives timings from live captures only; synthetic adapters are refused by the recorder. */ - recorder?: SpeechTimingRecorder; } const STOP_LISTENING = /^stop listening[.!]?$/i; export function createSpeechInput(options: SpeechInputOptions): SpeechInput { - const now = options.now ?? Date.now; - const talk = createPushToTalk(options.adapter, { ...options, now }); + const talk = createPushToTalk(options.adapter, options); const compiler = options.compiler ?? createIntentCompiler(); let generation = 0; @@ -74,20 +70,7 @@ export function createSpeechInput(options: SpeechInputOptions): SpeechInput { if (STOP_LISTENING.test(heard.transcript)) return { kind: 'stopped' }; const result = await compiler.compile(heard.observation, options.authority.describeSpeechContext()); if (turn !== generation) return { kind: 'cancelled' }; - const decidedAtMs = now(); const command = options.authority.admitSpeech(heard.observation, result); - const admittedAtMs = now(); - if (heard.timings) { - const { sessionId: _sessionId, ...timings } = heard.timings; - options.recorder?.record({ - ...timings, - adapterId: heard.observation.provider.adapterId, - captureKind: options.adapter.captureKind, - decidedAtMs, - ...(command.lifecycle === 'committed' ? { committedAtMs: admittedAtMs } : {}), - lifecycle: command.lifecycle - }); - } return { kind: 'command', command, observation: heard.observation }; }, cancel() { diff --git a/src/core/interaction/speechMeasurement.test.ts b/src/core/interaction/speechMeasurement.test.ts deleted file mode 100644 index 684ae20..0000000 --- a/src/core/interaction/speechMeasurement.test.ts +++ /dev/null @@ -1,71 +0,0 @@ -import { describe, expect, it } from 'vitest'; -import { InteractionEngine } from './engine'; -import { MemoryCanvas } from './memoryCanvas'; -import { scriptedSpeechAdapter } from './scriptedSpeechAdapter'; -import { createSpeechInput } from './speechInput'; -import { MIN_SAMPLES_FOR_PERCENTILES, SPEECH_MEASUREMENTS, SpeechTimingRecorder, type SpeechTimingSample } from './speechMeasurement'; - -function live(offset: number): SpeechTimingSample { - return { - adapterId: 'browser-web-speech', - captureKind: 'live', - captureRequestedAtMs: 0, - microphoneActiveAtMs: 10, - stopRequestedAtMs: 1000, - finalAtMs: 1000 + offset, - decidedAtMs: 1000 + offset, - committedAtMs: 1000 + offset - }; -} - -describe('speech measurement', () => { - it('the committed state says nothing was measured, and why', () => { - expect(SPEECH_MEASUREMENTS.measured).toBe(false); - expect(SPEECH_MEASUREMENTS.reason).toMatch(/no live microphone/i); - expect(Object.values(SPEECH_MEASUREMENTS.metrics).every(value => value === 'not measured')).toBe(true); - expect(Object.values(SPEECH_MEASUREMENTS.adapters).every(entry => entry.measured === false)).toBe(true); - }); - - it('refuses to emit percentiles from zero samples', () => { - const recorder = new SpeechTimingRecorder(); - const summary = recorder.summarize('speechEndToFinalTranscript'); - expect(summary).toEqual({ metric: 'speechEndToFinalTranscript', measured: false, samples: 0, reason: 'no live samples' }); - expect('p50Ms' in summary).toBe(false); - expect('p95Ms' in summary).toBe(false); - }); - - it('refuses percentiles below the sample floor', () => { - const recorder = new SpeechTimingRecorder(); - for (let index = 0; index < MIN_SAMPLES_FOR_PERCENTILES - 1; index += 1) recorder.record(live(index)); - expect(recorder.summarize('speechEndToFinalTranscript').measured).toBe(false); - }); - - it('stores only live captures with ordered timestamps', () => { - const recorder = new SpeechTimingRecorder(); - expect(recorder.record({ ...live(5), captureKind: 'synthetic' })).toBe(false); - expect(recorder.record({ ...live(5), microphoneActiveAtMs: undefined })).toBe(false); - expect(recorder.record({ ...live(5), finalAtMs: 500 })).toBe(false); - expect(recorder.record({ ...live(5), committedAtMs: 1 })).toBe(false); - expect(recorder.record(live(5))).toBe(true); - expect(recorder.samples()).toHaveLength(1); - }); - - it('computes nearest-rank percentiles once enough live samples exist', () => { - const recorder = new SpeechTimingRecorder(); - for (let index = 1; index <= 20; index += 1) recorder.record(live(index * 10)); - expect(recorder.summarize('speechEndToFinalTranscript')).toEqual({ - metric: 'speechEndToFinalTranscript', measured: true, samples: 20, p50Ms: 100, p95Ms: 190 - }); - expect(recorder.summarize('speechEndToFinalTranscript', 'openai-realtime').measured).toBe(false); - }); - - it('a scripted capture never becomes a sample', async () => { - const recorder = new SpeechTimingRecorder(); - const engine = new InteractionEngine(new MemoryCanvas()); - const input = createSpeechInput({ adapter: scriptedSpeechAdapter({ final: 'create a researcher' }), authority: engine, recorder }); - await input.press(); - const outcome = await input.release(); - expect(outcome?.kind).toBe('command'); - expect(recorder.samples()).toEqual([]); - }); -}); diff --git a/src/core/interaction/speechMeasurement.ts b/src/core/interaction/speechMeasurement.ts deleted file mode 100644 index 5b592bf..0000000 --- a/src/core/interaction/speechMeasurement.ts +++ /dev/null @@ -1,124 +0,0 @@ -// Speech timing measurement. No number here is a measurement until a live -// capture on real hardware produced it. - -export type SpeechMetric = - | 'pressToMicrophoneReady' - | 'speechEndToFinalTranscript' - | 'finalTranscriptToDecision' - | 'decisionToCommit' - | 'speechEndToCommit'; - -export interface SpeechTimingSample { - adapterId: string; - captureKind: 'live' | 'synthetic'; - captureRequestedAtMs: number; - microphoneActiveAtMs?: number; - firstDeltaAtMs?: number; - stopRequestedAtMs?: number; - finalAtMs?: number; - decidedAtMs?: number; - committedAtMs?: number; - lifecycle?: string; -} - -export type MetricSummary = - | { metric: SpeechMetric; measured: false; samples: number; reason: string } - | { metric: SpeechMetric; measured: true; samples: number; p50Ms: number; p95Ms: number }; - -/** - * Committed state of speech measurement for this repository. - * Flip an entry only with a recorder export from a real microphone session, - * and say which hardware, browser, and provider produced it. - */ -export const SPEECH_MEASUREMENTS = { - measured: false, - reason: 'No live microphone capture has been run. The build environment has no microphone and no OPENAI_API_KEY.', - adapters: { - 'browser-web-speech': { measured: false, reason: 'not measured: no live microphone session recorded' }, - 'openai-realtime': { measured: false, reason: 'not measured: no live microphone session or OPENAI_API_KEY available' } - }, - metrics: { - pressToMicrophoneReady: 'not measured', - speechEndToFinalTranscript: 'not measured', - finalTranscriptToDecision: 'not measured', - decisionToCommit: 'not measured', - speechEndToCommit: 'not measured', - transcriptionFailureRate: 'not measured', - cancellationSuccessRate: 'not measured' - } -} as const; - -/** A policy floor, not a claim about variance: below it the recorder reports no percentiles. */ -export const MIN_SAMPLES_FOR_PERCENTILES = 20; - -function span(from: number | undefined, to: number | undefined): number | null { - if (from === undefined || to === undefined) return null; - if (!Number.isFinite(from) || !Number.isFinite(to) || to < from) return null; - return to - from; -} - -function metricValue(sample: SpeechTimingSample, metric: SpeechMetric): number | null { - switch (metric) { - case 'pressToMicrophoneReady': return span(sample.captureRequestedAtMs, sample.microphoneActiveAtMs); - case 'speechEndToFinalTranscript': return span(sample.stopRequestedAtMs, sample.finalAtMs); - case 'finalTranscriptToDecision': return span(sample.finalAtMs, sample.decidedAtMs); - case 'decisionToCommit': return span(sample.decidedAtMs, sample.committedAtMs); - case 'speechEndToCommit': return span(sample.stopRequestedAtMs, sample.committedAtMs); - } -} - -function nearestRank(sorted: number[], percentile: number): number { - const rank = Math.ceil((percentile / 100) * sorted.length); - return sorted[Math.min(sorted.length, Math.max(1, rank)) - 1]; -} - -export class SpeechTimingRecorder { - private readonly entries: SpeechTimingSample[] = []; - - constructor(private readonly limit = 1000) {} - - /** Stores a sample only from a live capture with ordered, finite timestamps. */ - record(sample: SpeechTimingSample): boolean { - if (sample.captureKind !== 'live') return false; - if (!sample.adapterId) return false; - const stamps = [ - sample.captureRequestedAtMs, - sample.microphoneActiveAtMs, - sample.stopRequestedAtMs, - sample.finalAtMs, - sample.decidedAtMs - ]; - if (stamps.some(value => value === undefined || !Number.isFinite(value))) return false; - for (let index = 1; index < stamps.length; index += 1) { - if ((stamps[index] as number) < (stamps[index - 1] as number)) return false; - } - if (sample.committedAtMs !== undefined && (!Number.isFinite(sample.committedAtMs) || sample.committedAtMs < (sample.decidedAtMs as number))) { - return false; - } - this.entries.push({ ...sample }); - if (this.entries.length > this.limit) this.entries.shift(); - return true; - } - - samples(): SpeechTimingSample[] { - return this.entries.map(entry => ({ ...entry })); - } - - summarize(metric: SpeechMetric, adapterId?: string): MetricSummary { - const values = this.entries - .filter(entry => !adapterId || entry.adapterId === adapterId) - .map(entry => metricValue(entry, metric)) - .filter((value): value is number => value !== null) - .sort((left, right) => left - right); - if (values.length === 0) { - return { metric, measured: false, samples: 0, reason: 'no live samples' }; - } - if (values.length < MIN_SAMPLES_FOR_PERCENTILES) { - return { metric, measured: false, samples: values.length, reason: `fewer than ${MIN_SAMPLES_FOR_PERCENTILES} live samples` }; - } - return { metric, measured: true, samples: values.length, p50Ms: nearestRank(values, 50), p95Ms: nearestRank(values, 95) }; - } -} - -/** In-memory only. Holds timestamps and adapter ids: no audio, no transcripts. */ -export const liveSpeechTimings = new SpeechTimingRecorder(); diff --git a/src/core/interaction/speechObservation.ts b/src/core/interaction/speechObservation.ts index 377c998..f411fc4 100644 --- a/src/core/interaction/speechObservation.ts +++ b/src/core/interaction/speechObservation.ts @@ -1,7 +1,7 @@ // SpeechObservation v1 and the SpeechAdapter seam. // A provider produces evidence. OmniOS owns the session, the ids, and every effect. -export interface SpeechProvenance { +interface SpeechProvenance { adapterId: string; providerName?: string; model?: string; @@ -30,7 +30,7 @@ export interface SpeechObservationV1 { source: 'microphone'; } -export interface SpeechAdapterStartInput { +interface SpeechAdapterStartInput { sessionId: string; locale?: string; signal: AbortSignal; @@ -75,7 +75,7 @@ export class SpeechAdapterError extends Error { } } -export const SPEECH_ERROR_MESSAGES: Record = { +const SPEECH_ERROR_MESSAGES: Record = { unsupported: 'This browser has no speech recognition.', unconfigured: 'Remote transcription is not configured.', 'permission-denied': 'Microphone permission was refused.', @@ -107,7 +107,7 @@ export function createSpeechSessionId(): string { return `omni-speech-${sessionSequence}-${random}`; } -export const MAX_TRANSCRIPT_CHARS = 2000; +const MAX_TRANSCRIPT_CHARS = 2000; const OBSERVATION_KEYS = new Set([ 'version', 'sessionId', 'observationId', 'provider', 'locale', 'startedAtMs', 'endedAtMs', diff --git a/src/core/interaction/types.ts b/src/core/interaction/types.ts index 8d2851f..9074732 100644 --- a/src/core/interaction/types.ts +++ b/src/core/interaction/types.ts @@ -86,7 +86,6 @@ export interface SpatialCommand { export interface InteractionTrace { command: string; subject?: string; - subjects?: string[]; target?: string; from?: { x: number; y: number }; to?: { x: number; y: number }; diff --git a/src/core/interaction/utterances.ts b/src/core/interaction/utterances.ts index 7dc810a..95e21ad 100644 --- a/src/core/interaction/utterances.ts +++ b/src/core/interaction/utterances.ts @@ -26,7 +26,7 @@ export type UtteranceClass = export type CompilerConfig = 'deterministic-only' | 'paraphrase'; export const COMPILER_CONFIGS: CompilerConfig[] = ['deterministic-only', 'paraphrase']; -export interface UtteranceExpectation { +interface UtteranceExpectation { outcome: 'command' | 'error' | 'silence' | 'none'; lifecycle?: CommandLifecycle; action?: SpatialAction | 'open-shell'; @@ -42,7 +42,7 @@ export interface UtteranceExpectation { export type SceneId = 'board' | 'two-news' | 'similar-personas' | 'injected-name'; -export interface UtteranceCase { +interface UtteranceCase { id: string; class: UtteranceClass; note: string; @@ -323,7 +323,7 @@ export function expectationFor(item: UtteranceCase, config: CompilerConfig): Utt return item.expect[config] ?? item.expect['deterministic-only']; } -export interface CountingCompiler extends IntentCompiler { +interface CountingCompiler extends IntentCompiler { semanticCalls: number; } @@ -346,7 +346,7 @@ export function compilerFor(config: CompilerConfig, semantic: SemanticCompiler = return counted; } -export interface PlayedUtterance { +interface PlayedUtterance { canvas: MemoryCanvas; engine: InteractionEngine; outcome: SpeechOutcome | undefined; diff --git a/src/core/models.registry.ts b/src/core/models.registry.ts index 50e032f..68318d2 100644 --- a/src/core/models.registry.ts +++ b/src/core/models.registry.ts @@ -11,7 +11,7 @@ import type { LLMProvider } from './schemas/mind.schema'; -export interface ModelInfo { +interface ModelInfo { id: string; label: string; /** diff --git a/src/core/registry/BlockRegistry.ts b/src/core/registry/BlockRegistry.ts index e462e86..0a3de12 100644 --- a/src/core/registry/BlockRegistry.ts +++ b/src/core/registry/BlockRegistry.ts @@ -2,7 +2,7 @@ // PROJECT OMNI: BLOCK REGISTRY // ============================================ -import { OmniBlockSchema, BlockCategory } from '../schemas/block.schema'; +import { OmniBlockSchema } from '../schemas/block.schema'; import { createJsonOutputPort, createAnyInputPort, createTextOutputPort } from '../services/port.service'; /** @@ -36,13 +36,6 @@ class BlockRegistry { return Array.from(this.blocks.values()); } - /** - * Get blocks by category - */ - getByCategory(category: BlockCategory): OmniBlockSchema[] { - return this.getAll().filter(block => block.category === category); - } - /** * Search blocks by semantic tags or name */ diff --git a/src/core/registry/ViewRegistry.tsx b/src/core/registry/ViewRegistry.tsx index 66d2dd2..31840f2 100644 --- a/src/core/registry/ViewRegistry.tsx +++ b/src/core/registry/ViewRegistry.tsx @@ -13,7 +13,7 @@ import { useWorldBankBlock } from '@/blocks/truth/WorldBankBlock'; import { useOmniFeedBlock } from '@/blocks/truth/OmniFeedBlock'; import { API_CATALOG } from '@/core/schemas/api.schema'; import { TextBlockView } from '@/blocks/workspace/TextBlock'; -import { PersonaBlockView } from '@/blocks/persona'; +import { PersonaBlockView } from '@/blocks/persona/PersonaBlock'; import { MemoryBlockView } from '@/components/blocks/MemoryBlock'; import { CapabilityBlockView } from '@/blocks/capability/CapabilityBlock'; import { SpeechBlockView } from '@/blocks/capability/SpeechBlock'; @@ -57,13 +57,12 @@ function PolymarketBlockContent({ instanceId }: { instanceId: string }) { } function CryptoBlockContent({ instanceId }: { instanceId: string }) { - const { assets, status, lastUpdated, refresh, error } = useCoinGeckoBlock(instanceId); + const { assets, status, refresh, error } = useCoinGeckoBlock(instanceId); return ( @@ -71,13 +70,12 @@ function CryptoBlockContent({ instanceId }: { instanceId: string }) { } function HNBlockContent({ instanceId }: { instanceId: string }) { - const { stories, status, lastUpdated, refresh, error } = useHackerNewsBlock(instanceId); + const { stories, status, refresh, error } = useHackerNewsBlock(instanceId); return ( diff --git a/src/core/schemas/api.schema.test.ts b/src/core/schemas/api.schema.test.ts index d2595db..dd5a654 100644 --- a/src/core/schemas/api.schema.test.ts +++ b/src/core/schemas/api.schema.test.ts @@ -4,7 +4,7 @@ import { blockRegistry } from '@/core/registry/BlockRegistry'; describe('catalog blockIds — the map shells use to know what they need', () => { it('every registered truth block maps to exactly one provider', () => { - for (const block of blockRegistry.getByCategory('truth')) { + for (const block of blockRegistry.getAll().filter(b => b.category === 'truth')) { const owners = API_CATALOG.filter(p => p.blockIds?.includes(block.block_id)); expect(owners, block.block_id).toHaveLength(1); } diff --git a/src/core/schemas/api.schema.ts b/src/core/schemas/api.schema.ts index 8cd9e82..6b32d54 100644 --- a/src/core/schemas/api.schema.ts +++ b/src/core/schemas/api.schema.ts @@ -6,7 +6,7 @@ /** * OmniData gateway categories (used by the API gateway layer) */ -export type GatewayCategory = +type GatewayCategory = | 'prediction_market' | 'news' | 'market_data' @@ -69,7 +69,7 @@ export interface RestListAdapterConfig { /** * Gateway integration config */ -export type ApiGatewayAdapter = +type ApiGatewayAdapter = | { type: 'normalizer'; normalizerId: string; @@ -83,7 +83,7 @@ export type ApiGatewayAdapter = /** * API integration metadata */ -export interface ApiIntegration { +interface ApiIntegration { gateway?: ApiGatewayAdapter; } diff --git a/src/core/schemas/block.schema.ts b/src/core/schemas/block.schema.ts index 765aa8f..93c4af4 100644 --- a/src/core/schemas/block.schema.ts +++ b/src/core/schemas/block.schema.ts @@ -27,7 +27,7 @@ export type ConnectionStatus = /** * Expand behavior for blocks */ -export type BlockExpandMode = 'resize' | 'portal' | 'fullscreen'; +type BlockExpandMode = 'resize' | 'portal' | 'fullscreen'; /** Data types shown as visual hints on block wire handles. */ export type PortDataType = 'json' | 'text' | 'media' | 'any'; @@ -36,7 +36,7 @@ export type PortDataType = 'json' | 'text' | 'media' | 'any'; * Schema-bearing port contract. Optional so existing catalog ports stay * visual hints. When both ends of a wire declare one, the wire is checked. */ -export interface PortValueSchema { +interface PortValueSchema { kind: 'string' | 'number' | 'integer' | 'boolean' | 'null' | 'object' | 'array' | 'any'; description?: string; enum?: Array; @@ -51,7 +51,7 @@ export interface PortValueSchema { /** * Port direction */ -export type PortDirection = 'input' | 'output'; +type PortDirection = 'input' | 'output'; /** * Port definition for block schema @@ -162,36 +162,13 @@ export interface PolymarketMarket { tags: string[]; } -export interface PolymarketOutcome { +interface PolymarketOutcome { id: string; name: string; probability: number; priceHistory?: { timestamp: number; price: number }[]; } -/** - * NewsAPI-specific data types - */ -export interface NewsArticle { - id: string; - title: string; - description: string; - source: string; - author?: string; - url: string; - imageUrl?: string; - publishedAt: string; - content?: string; - sentiment?: 'positive' | 'negative' | 'neutral'; -} - -export interface NewsFeed { - articles: NewsArticle[]; - totalResults: number; - query?: string; - category?: string; -} - /** * Legacy shell connection shape. Port IDs remain here only so saved shells * from the old connection store can be converted to block-ID DataWires. diff --git a/src/core/schemas/mind.schema.ts b/src/core/schemas/mind.schema.ts index 997cc87..dcc414c 100644 --- a/src/core/schemas/mind.schema.ts +++ b/src/core/schemas/mind.schema.ts @@ -66,12 +66,12 @@ export const LLM_DEFAULTS: Record = { /** * Node types in the knowledge graph */ -export type KnowledgeNodeType = 'entity' | 'concept' | 'event' | 'insight' | 'prediction'; +type KnowledgeNodeType = 'entity' | 'concept' | 'event' | 'insight' | 'prediction'; /** * A node in the knowledge graph */ -export interface KnowledgeNode { +interface KnowledgeNode { id: string; type: KnowledgeNodeType; label: string; @@ -86,7 +86,7 @@ export interface KnowledgeNode { /** * Relationship types between nodes */ -export type RelationType = +type RelationType = | 'related_to' | 'causes' | 'correlates_with' @@ -101,7 +101,7 @@ export type RelationType = /** * An edge/relationship in the knowledge graph */ -export interface KnowledgeEdge { +interface KnowledgeEdge { id: string; sourceId: string; targetId: string; @@ -128,7 +128,7 @@ export interface KnowledgeGraph { /** * Persona trait definition */ -export interface PersonaTrait { +interface PersonaTrait { id: string; name: string; description: string; @@ -138,7 +138,7 @@ export interface PersonaTrait { /** * Built-in trait presets */ -export const PERSONA_TRAITS: PersonaTrait[] = [ +const PERSONA_TRAITS: PersonaTrait[] = [ { id: 'analytical', name: 'Analytical', description: 'Depth of logical analysis', value: 0.5 }, { id: 'creative', name: 'Creative', description: 'Novelty in connections and ideas', value: 0.5 }, { id: 'cautious', name: 'Cautious', description: 'Risk awareness and hedging', value: 0.5 }, @@ -308,7 +308,7 @@ export function createMemoryBlockData(poolId: string = 'memory'): MemoryBlockDat /** * Pruning strategies for context pools */ -export type PruneStrategy = 'fifo' | 'importance' | 'recency' | 'hybrid'; +type PruneStrategy = 'fifo' | 'importance' | 'recency' | 'hybrid'; /** * A context pool configuration @@ -329,7 +329,7 @@ export interface ContextPool { /** * Built-in context pool definitions */ -export const BUILTIN_POOLS: Omit[] = [ +const BUILTIN_POOLS: Omit[] = [ { id: 'observations', name: 'Observations', diff --git a/src/core/schemas/shell.schema.ts b/src/core/schemas/shell.schema.ts index 81f28ea..a60bd6d 100644 --- a/src/core/schemas/shell.schema.ts +++ b/src/core/schemas/shell.schema.ts @@ -32,7 +32,7 @@ export type AestheticTheme = /** * Shell types for universal shell architecture */ -export type ShellType = +type ShellType = | 'root' // Default home shell | 'system' // One of the 7 life system shells | 'custom' // User-created shell diff --git a/src/core/schemas/wire.schema.ts b/src/core/schemas/wire.schema.ts index c99ef9f..bd933d4 100644 --- a/src/core/schemas/wire.schema.ts +++ b/src/core/schemas/wire.schema.ts @@ -17,9 +17,6 @@ export interface WireFilters { /** Auto-refresh when source updates */ autoRefresh: boolean; - - /** Specific fields to include (empty = all) */ - fields?: string[]; } /** @@ -32,7 +29,7 @@ export interface WireFilters { * can still carry them; such a wire is drawn in its status colour and is * excluded from persona context. */ -export type WireStatus = 'active' | 'stale' | 'error' | 'disconnected'; +type WireStatus = 'active' | 'stale' | 'error' | 'disconnected'; /** * Wire type - defines data flow semantics @@ -95,7 +92,7 @@ export interface DataWire { * compounds their errors, and the reader should see when an * answer rests on another answer. */ -export type ContextSourceKind = 'wire' | 'memory' | 'inference'; +type ContextSourceKind = 'wire' | 'memory' | 'inference'; export interface ContextSource { /** Block instance id for `wire`; pool id for `ambient`. */ @@ -169,8 +166,7 @@ export interface PersonaBlockData { export const DEFAULT_WIRE_FILTERS: WireFilters = { summaryOnly: false, timeWindow: 'all', - autoRefresh: true, - fields: [] + autoRefresh: true }; /** diff --git a/src/core/secrets.ts b/src/core/secrets.ts index c0bac49..9f51598 100644 --- a/src/core/secrets.ts +++ b/src/core/secrets.ts @@ -45,14 +45,3 @@ export const SECRET_ENV_VARS = [ * would corrupt more than it protects. A real key is far longer. */ export const MIN_SECRET_LENGTH = 8; - -/** - * Env vars that are public BY DESIGN. Listed so the distinction is explicit - * rather than implied by absence: `NEXT_PUBLIC_*` is inlined into the client - * bundle by Next, which is the whole point of the prefix. - */ -export const PUBLIC_ENV_VARS = [ - 'NEXT_PUBLIC_OMNI_DEBUG', - // Not a credential: a localhost URL the user configures. - 'OLLAMA_BASE_URL' -] as const; diff --git a/src/core/services/cascade.service.ts b/src/core/services/cascade.service.ts index 7413fa9..3f66f24 100644 --- a/src/core/services/cascade.service.ts +++ b/src/core/services/cascade.service.ts @@ -28,7 +28,7 @@ function personaSourcesOf(blockId: string): string[] { .filter(isPersona); } -export interface CascadePlan { +interface CascadePlan { /** Personas to run, upstream first; the target is last. */ order: string[]; /** True when a cycle was found and broken to keep the plan finite. */ diff --git a/src/core/services/cascade.test.ts b/src/core/services/cascade.test.ts index aec7e5d..cbcc3ae 100644 --- a/src/core/services/cascade.test.ts +++ b/src/core/services/cascade.test.ts @@ -137,7 +137,7 @@ describe('crystallize', () => { expect(created?.schema.block_id).toBe('memory_pool'); expect((created?.data as MemoryBlockData).poolId).toBe(CRYSTAL_POOL); - const entries = useMindStore.getState().getPoolEntries(CRYSTAL_POOL); + const entries = useMindStore.getState().contextPools.find(p => p.id === CRYSTAL_POOL)?.entries ?? []; expect(entries.some(e => e.content === 'Rates are steady near 4.2%.')).toBe(true); }); diff --git a/src/core/services/crystallize.service.ts b/src/core/services/crystallize.service.ts index d85dbde..078434a 100644 --- a/src/core/services/crystallize.service.ts +++ b/src/core/services/crystallize.service.ts @@ -19,7 +19,7 @@ import { createMemoryBlockData, type MemoryBlockData } from '@/core/schemas/mind export const CRYSTAL_POOL = 'memory'; -export interface CrystallizeResult { +interface CrystallizeResult { ok: boolean; /** The Memory block the insight is now visible in. */ memoryBlockId?: string; @@ -81,7 +81,7 @@ export function crystallize(content: string, sourceBlockId: string): Crystallize createdBlock = true; } - useMindStore.getState().addToPool(CRYSTAL_POOL, { + useMindStore.getState().pushContext(CRYSTAL_POOL, { type: 'memory', content: text, importance: 0.8, diff --git a/src/core/services/llm.service.test.ts b/src/core/services/llm.service.test.ts index 2f0e164..fa696af 100644 --- a/src/core/services/llm.service.test.ts +++ b/src/core/services/llm.service.test.ts @@ -7,7 +7,7 @@ // ============================================ import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'; -import { createLLMService, getLLMService } from './llm.service'; +import { getLLMService } from './llm.service'; import { LLM_DEFAULTS } from '@/core/schemas/mind.schema'; const originalFetch = globalThis.fetch; @@ -35,7 +35,7 @@ function postedBody(): Record { describe('LLMService.isAvailable', () => { it('pings /api/llm with the provider and no key', async () => { vi.mocked(globalThis.fetch).mockResolvedValue(jsonResponse({ available: true })); - const llm = createLLMService(LLM_DEFAULTS.local); + const llm = getLLMService(LLM_DEFAULTS.local); await expect(llm.isAvailable()).resolves.toBe(true); @@ -51,7 +51,7 @@ describe('LLMService.isAvailable', () => { it('returns false when the probe cannot reach the server', async () => { vi.mocked(globalThis.fetch).mockRejectedValue(new Error('network down')); - await expect(createLLMService(LLM_DEFAULTS.local).isAvailable()).resolves.toBe(false); + await expect(getLLMService(LLM_DEFAULTS.local).isAvailable()).resolves.toBe(false); }); }); @@ -60,7 +60,7 @@ describe('LLMService.complete', () => { vi.mocked(globalThis.fetch).mockResolvedValue( jsonResponse({ content: 'hello', tokensUsed: 3, finishReason: 'stop' }) ); - const llm = createLLMService(LLM_DEFAULTS.anthropic); + const llm = getLLMService(LLM_DEFAULTS.anthropic); const result = await llm.complete([{ role: 'user', content: 'Hi' }]); expect(result.content).toBe('hello'); @@ -77,14 +77,14 @@ describe('LLMService.complete', () => { jsonResponse({ error: 'anthropic is not configured on the server.' }, 503) ); await expect( - createLLMService(LLM_DEFAULTS.anthropic).complete([{ role: 'user', content: 'Hi' }]) + getLLMService(LLM_DEFAULTS.anthropic).complete([{ role: 'user', content: 'Hi' }]) ).rejects.toThrow('anthropic is not configured on the server.'); }); it('passes the caller cancellation signal to complete fetch', async () => { const controller = new AbortController(); vi.mocked(globalThis.fetch).mockResolvedValue(jsonResponse({ content: 'ok' })); - await createLLMService(LLM_DEFAULTS.local).complete( + await getLLMService(LLM_DEFAULTS.local).complete( [{ role: 'user', content: 'Hi' }], { signal: controller.signal } ); expect((vi.mocked(globalThis.fetch).mock.calls[0][1] as RequestInit).signal) @@ -93,7 +93,7 @@ describe('LLMService.complete', () => { it('reuses the caller key for retries of one logical operation', async () => { vi.mocked(globalThis.fetch).mockImplementation(async () => jsonResponse({ content: 'ok' })); - const llm = createLLMService(LLM_DEFAULTS.local); + const llm = getLLMService(LLM_DEFAULTS.local); const operation = { idempotencyKey: 'stable-operation-key' }; await llm.complete([{ role: 'user', content: 'Hi' }], operation); await llm.complete([{ role: 'user', content: 'Hi' }], operation); @@ -125,7 +125,7 @@ describe('LLMService.stream', () => { ) ); - const llm = createLLMService(LLM_DEFAULTS.local); + const llm = getLLMService(LLM_DEFAULTS.local); const got: string[] = []; for await (const chunk of llm.stream([{ role: 'user', content: 'Hi' }])) { got.push(chunk); @@ -141,7 +141,7 @@ describe('LLMService.stream', () => { vi.mocked(globalThis.fetch).mockResolvedValue( new Response('x', { status: 200 }) ); - const llm = createLLMService(LLM_DEFAULTS.local); + const llm = getLLMService(LLM_DEFAULTS.local); const gen = llm.stream([{ role: 'user', content: 'Hi' }], { signal: controller.signal }); await gen.next(); @@ -156,7 +156,7 @@ describe('LLMService.stream', () => { pull(controller) { controller.enqueue(new TextEncoder().encode('first')); }, cancel() { canceled = true; } }), { status: 200 })); - const gen = createLLMService(LLM_DEFAULTS.local).stream([{ role: 'user', content: 'Hi' }]); + const gen = getLLMService(LLM_DEFAULTS.local).stream([{ role: 'user', content: 'Hi' }]); await gen.next(); await gen.return(undefined); expect(canceled).toBe(true); diff --git a/src/core/services/llm.service.ts b/src/core/services/llm.service.ts index ee31658..986d798 100644 --- a/src/core/services/llm.service.ts +++ b/src/core/services/llm.service.ts @@ -41,7 +41,7 @@ export interface LLMOptions { onRunId?: (runId: string) => void; } -export interface LLMResponse { +interface LLMResponse { content: string; tokensUsed?: number; finishReason?: string; @@ -85,7 +85,7 @@ function abortError(): Error { // LLM SERVICE // ============================================ -export class LLMService { +class LLMService { private config: LLMConfig; constructor(config: LLMConfig) { @@ -219,7 +219,3 @@ export function getLLMService(config: LLMConfig): LLMService { } return currentService; } - -export function createLLMService(config: LLMConfig): LLMService { - return new LLMService(config); -} diff --git a/src/core/services/mind.engine.ts b/src/core/services/mind.engine.ts index 56ce182..c4edb47 100644 --- a/src/core/services/mind.engine.ts +++ b/src/core/services/mind.engine.ts @@ -158,7 +158,7 @@ ${taskDescription} // TYPES // ============================================ -export interface ThinkResult { +interface ThinkResult { success: boolean; response?: string; insights?: ExtractedInsight[]; diff --git a/src/core/services/persona.engine.test.ts b/src/core/services/persona.engine.test.ts index 53fe224..ed2437c 100644 --- a/src/core/services/persona.engine.test.ts +++ b/src/core/services/persona.engine.test.ts @@ -64,14 +64,13 @@ describe('preparePersonaTurn — message assembly', () => { }); useWireStore.getState().addWire('news_1', personaId); - const { messages, hasContext, sourceIds } = preparePersonaTurn({ + const { messages, sources } = preparePersonaTurn({ instanceId: personaId, personaType: 'analyst', userMessage: 'What stands out?' }); - expect(hasContext).toBe(true); - expect(sourceIds).toContain('news_1'); + expect(sources.filter(s => s.kind === 'wire').map(s => s.id)).toEqual(['news_1']); // system + user (no history) expect(messages[0].role).toBe('system'); const userMsg = messages[messages.length - 1]; @@ -88,13 +87,13 @@ describe('preparePersonaTurn — message assembly', () => { activeShellId: 'root' }); - const { messages, hasContext } = preparePersonaTurn({ + const { messages, sources } = preparePersonaTurn({ instanceId: personaId, personaType: 'analyst', userMessage: 'Hello' }); - expect(hasContext).toBe(false); + expect(sources.filter(s => s.kind === 'wire')).toHaveLength(0); expect(messages[messages.length - 1].content).toMatch(/No active data sources connected/i); }); @@ -108,14 +107,13 @@ describe('preparePersonaTurn — message assembly', () => { }); useWireStore.getState().addWire('feed_1', personaId); - const { messages, hasContext, sourceIds } = preparePersonaTurn({ + const { messages, sources } = preparePersonaTurn({ instanceId: personaId, personaType: 'analyst', userMessage: 'Hello' }); - expect(hasContext).toBe(false); - expect(sourceIds).toEqual([]); + expect(sources.filter(s => s.kind === 'wire')).toHaveLength(0); const prompt = messages[messages.length - 1].content; expect(prompt).toMatch(/Connected sources have no data available yet/i); expect(prompt).not.toMatch(/No active data sources connected/i); diff --git a/src/core/services/persona.engine.ts b/src/core/services/persona.engine.ts index f4ba5b3..cc8e1d0 100644 --- a/src/core/services/persona.engine.ts +++ b/src/core/services/persona.engine.ts @@ -51,13 +51,10 @@ export interface PersonaTurnInput { abortSignal?: AbortSignal; } -export interface PersonaTurnPrepared { +interface PersonaTurnPrepared { messages: LLMMessage[]; - sourceIds: string[]; /** Labelled provenance for the UI: wired blocks and ambient pools. */ sources: ContextSource[]; - /** Whether any wired data context was found. */ - hasContext: boolean; } const DEFAULT_THINK_TASK = @@ -69,11 +66,7 @@ const DEFAULT_THINK_TASK = * context + recent history + the user task. Pure (no network) for easy testing. */ export function preparePersonaTurn(input: PersonaTurnInput): PersonaTurnPrepared { - const { context, sourceIds, sources } = aggregateWireContext(input.instanceId); - // aggregateWireContext returns a human sentinel string when nothing is - // wired and another when every wired source is empty, so presence of real - // context is keyed off sourceIds, not the text. - const hasContext = sourceIds.length > 0; + const { context, sources } = aggregateWireContext(input.instanceId); const system = buildPersonaSystemPrompt(input.personaType, input.customName); @@ -94,13 +87,12 @@ export function preparePersonaTurn(input: PersonaTurnInput): PersonaTurnPrepared { role: 'user', content: `${contextBlock}\n\n---\n\n${task}` } ]; - return { messages, sourceIds, sources, hasContext }; + return { messages, sources }; } export interface PersonaTurnResult { success: boolean; content?: string; - sourceIds: string[]; /** What actually fed this turn — record this, not the block's wires. */ sources: ContextSource[]; error?: string; @@ -117,7 +109,7 @@ export interface PersonaTurnResult { export async function* streamPersonaTurn( input: PersonaTurnInput ): AsyncGenerator { - const { messages, sourceIds, sources } = preparePersonaTurn(input); + const { messages, sources } = preparePersonaTurn(input); input.onPrepared?.(sources); // The Cognition Kernel owns the turn lifecycle (availability, registry @@ -139,10 +131,10 @@ export async function* streamPersonaTurn( const { runId } = result; if (result.stopped) { - return { success: true, content: result.content, sourceIds, sources, stopped: true, runId }; + return { success: true, content: result.content, sources, stopped: true, runId }; } return result.success - ? { success: true, content: result.content, sourceIds, sources, runId } - : { success: false, sourceIds, sources, error: result.error, runId }; + ? { success: true, content: result.content, sources, runId } + : { success: false, sources, error: result.error, runId }; } diff --git a/src/core/services/persona.prompts.ts b/src/core/services/persona.prompts.ts index 67ffd0e..0c1f41b 100644 --- a/src/core/services/persona.prompts.ts +++ b/src/core/services/persona.prompts.ts @@ -9,7 +9,7 @@ import { PersonaConfig } from '@/core/schemas/mind.schema'; // BASE CONTEXT // ============================================ -export const MIND_CONTEXT = `You are an AI cognitive layer embedded within "The Citadel" — a command center for truth-seeking and strategic intelligence. +const MIND_CONTEXT = `You are an AI cognitive layer embedded within "The Citadel" — a command center for truth-seeking and strategic intelligence. You have access to various data streams: - Polymarket: Prediction market data showing crowd-sourced probability estimates diff --git a/src/core/services/personaTurn.service.test.ts b/src/core/services/personaTurn.service.test.ts index 748c15a..4cc3432 100644 --- a/src/core/services/personaTurn.service.test.ts +++ b/src/core/services/personaTurn.service.test.ts @@ -81,7 +81,6 @@ function mockStream(opts: { return { success: true, content: (opts.chunks ?? ['ok']).join(''), - sourceIds: opts.sources.map(s => s.id), sources: opts.sources, ...opts.result }; @@ -203,7 +202,6 @@ describe('runPersonaTurn — streaming commits', () => { return { success: true, content: 'ABCD', - sourceIds: [], sources: [] }; }); @@ -266,7 +264,7 @@ describe('runPersonaTurn — persisted writes during a stream (O4)', () => { now += STREAM_COMMIT_MS; yield `token${i} `; } - return { success: true, content: 'answer', sourceIds: [], sources: [] }; + return { success: true, content: 'answer', sources: [] }; }); await settle(); // beforeEach's setState lands before counting starts const put = vi.spyOn(IDBObjectStore.prototype, 'put'); @@ -358,7 +356,6 @@ describe('runPersonaTurn — stop keeps the partial', () => { return { success: true, content: 'Partial insight', - sourceIds: [], sources: [], stopped: true }; diff --git a/src/core/services/personaTurn.service.ts b/src/core/services/personaTurn.service.ts index a005a46..37b386f 100644 --- a/src/core/services/personaTurn.service.ts +++ b/src/core/services/personaTurn.service.ts @@ -26,7 +26,7 @@ import type { PersonaBlockData, ContextSource, PersonaChatMessage } from '@/core */ export const STREAM_COMMIT_MS = 80; -export interface PersonaTurnOutcome { +interface PersonaTurnOutcome { ran: boolean; success: boolean; error?: string; diff --git a/src/core/services/server/auth.ts b/src/core/services/server/auth.ts index 627293e..334b43c 100644 --- a/src/core/services/server/auth.ts +++ b/src/core/services/server/auth.ts @@ -3,7 +3,7 @@ import 'server-only'; import { createRemoteJWKSet, errors, jwtVerify } from 'jose'; import { NextResponse } from 'next/server'; -export interface AuthenticatedIdentity { +interface AuthenticatedIdentity { ownerId: string | null; } diff --git a/src/core/services/server/capability.ledger.ts b/src/core/services/server/capability.ledger.ts index 84b7ff5..b3ebacf 100644 --- a/src/core/services/server/capability.ledger.ts +++ b/src/core/services/server/capability.ledger.ts @@ -5,9 +5,9 @@ import { query, isDatabaseConfigured } from '@/core/db/client'; -export type ServerPhase = 'admitted' | 'running' | 'succeeded' | 'failed' | 'canceled' | 'uncertain'; -export type ServerExecutionProfile = 'sync' | 'async_poll'; -export type ServerObservedStatus = 'accepted' | 'running' | 'succeeded' | 'failed'; +type ServerPhase = 'admitted' | 'running' | 'succeeded' | 'failed' | 'canceled' | 'uncertain'; +type ServerExecutionProfile = 'sync' | 'async_poll'; +type ServerObservedStatus = 'accepted' | 'running' | 'succeeded' | 'failed'; export interface ServerExecution { runId: string; @@ -27,7 +27,7 @@ export interface ServerExecution { lastObservedAt?: number; } -export interface ServerAdmit { +interface ServerAdmit { kind: 'new' | 'replay' | 'conflict' | 'in_flight' | 'uncertain'; row: ServerExecution; } @@ -43,7 +43,7 @@ export interface ServerLedger { } /** The no-database ledger keeps this many rows; settled rows are evicted first, oldest first. */ -export const MEMORY_LEDGER_MAX_ROWS = 1_000; +const MEMORY_LEDGER_MAX_ROWS = 1_000; export function memoryLedger(maxRows = MEMORY_LEDGER_MAX_ROWS): ServerLedger { const rows = new Map(); @@ -167,7 +167,7 @@ function fromRow(found: Row): ServerExecution { }; } -export function postgresLedger(): ServerLedger { +function postgresLedger(): ServerLedger { return { async admit(row) { const inserted = await query<{ id: string }>(INSERT, [ diff --git a/src/core/services/server/capabilityBroker.ts b/src/core/services/server/capabilityBroker.ts index 626083a..6abad06 100644 --- a/src/core/services/server/capabilityBroker.ts +++ b/src/core/services/server/capabilityBroker.ts @@ -57,7 +57,7 @@ export function createBrokerRateLimiter(perCaller = MAX_PER_CALLER, global = MAX const processLimiter = createBrokerRateLimiter(); /** Proof that one request was already counted against the broker budget. Spent once. */ -export interface BrokerAdmission { +interface BrokerAdmission { readonly caller: string; } diff --git a/src/core/services/server/clientKey.ts b/src/core/services/server/clientKey.ts index f3fdd8c..493f609 100644 --- a/src/core/services/server/clientKey.ts +++ b/src/core/services/server/clientKey.ts @@ -11,7 +11,7 @@ export type ClientKeyEnv = Readonly>; /** Every caller shares this bucket when no ingress is declared. */ export const DIRECT_CLIENT = 'direct'; -export type Ingress = 'cloudflare' | 'proxy' | 'direct'; +type Ingress = 'cloudflare' | 'proxy' | 'direct'; /** * `OMNI_TRUSTED_PROXY` names what sits in front of this server: diff --git a/src/core/services/server/dataPlant.ts b/src/core/services/server/dataPlant.ts index 2ebcc26..d25ac8b 100644 --- a/src/core/services/server/dataPlant.ts +++ b/src/core/services/server/dataPlant.ts @@ -25,9 +25,9 @@ export const CACHE_TTL_MS = 5 * 60_000; const MAX_CACHE_ENTRIES = 500; const DAY_MS = 24 * 60 * 60_000; /** House-key answers one caller may receive per day, cached or not. */ -export const MAX_CALLER_PER_DAY = 200; +const MAX_CALLER_PER_DAY = 200; /** Upstream calls one provider may receive on a house key per day. */ -export const MAX_PROVIDER_PER_DAY = 500; +const MAX_PROVIDER_PER_DAY = 500; export interface HouseKey { apiId: string; diff --git a/src/core/services/server/inference.ledger.ts b/src/core/services/server/inference.ledger.ts index e388d6e..daf95fa 100644 --- a/src/core/services/server/inference.ledger.ts +++ b/src/core/services/server/inference.ledger.ts @@ -42,7 +42,7 @@ export interface RunSource { parentRunId?: string; } -export interface OpenRunInput { +interface OpenRunInput { provider: RunProvider; model: string; streamed: boolean; @@ -58,7 +58,7 @@ export interface OpenRunInput { requestDigest?: string; } -export interface RunOutcome { +interface RunOutcome { output?: string; outputChars?: number; tokensUsed?: number; @@ -70,7 +70,7 @@ export interface RunOutcome { * opening INSERT failed — then every method is a no-op, so the route reads * the same either way and carries no `if (ledgerEnabled)` branches. */ -export interface LedgerRun { +interface LedgerRun { /** Row id, or null when nothing was recorded. */ readonly id: string | null; readonly replay?: boolean; @@ -515,7 +515,7 @@ export interface LedgerRunRow { sources: RunSource[]; } -export interface RecentRunsFilter { +interface RecentRunsFilter { limit?: number; provider?: RunProvider; status?: RunStatus; diff --git a/src/core/services/server/llm.adapters.ts b/src/core/services/server/llm.adapters.ts index 37e8bad..f5670d5 100644 --- a/src/core/services/server/llm.adapters.ts +++ b/src/core/services/server/llm.adapters.ts @@ -12,12 +12,12 @@ export interface LLMMessage { content: string; } -export interface LLMOptions { +interface LLMOptions { temperature?: number; maxTokens?: number; } -export interface LLMResponse { +interface LLMResponse { content: string; tokensUsed?: number; finishReason?: string; diff --git a/src/core/services/server/oauthConnect.ts b/src/core/services/server/oauthConnect.ts index d1f8ff8..8efd314 100644 --- a/src/core/services/server/oauthConnect.ts +++ b/src/core/services/server/oauthConnect.ts @@ -96,7 +96,7 @@ function unseal(value: string | undefined, client: Client): FlowCookie | null { } } -export type StartResult = +type StartResult = | { ok: true; redirect: string; cookie: string } | { ok: false; status: number; error: string }; @@ -141,7 +141,7 @@ export function startConnect(args: { return { ok: true, redirect: authorize.toString(), cookie }; } -export type CallbackResult = +type CallbackResult = | { ok: true; providerId: ConnectProviderId; nonce: string; scopes: string[]; accessToken: string; expiresIn: number } | { ok: false; nonce?: string; error: string }; diff --git a/src/core/services/server/public.providers.ts b/src/core/services/server/public.providers.ts index 1376cad..d6dcc76 100644 --- a/src/core/services/server/public.providers.ts +++ b/src/core/services/server/public.providers.ts @@ -22,7 +22,7 @@ export const PUBLIC_PROXY_IDS = [ 'frankfurter' ] as const; -export type PublicProviderId = (typeof PUBLIC_PROXY_IDS)[number]; +type PublicProviderId = (typeof PUBLIC_PROXY_IDS)[number]; export function isPublicProvider(id: string): id is PublicProviderId { return (PUBLIC_PROXY_IDS as readonly string[]).includes(id); @@ -91,11 +91,11 @@ function headersFor(id: PublicProviderId): Record { * these are single small GETs and a canvas block is waiting on them, so the * broker's 15 s (which covers a whole capability run) would be too generous. */ -export const PUBLIC_UPSTREAM_DEADLINE_MS = 8000; +const PUBLIC_UPSTREAM_DEADLINE_MS = 8000; /** Same cap the capability broker puts on an upstream body. */ -export const PUBLIC_MAX_RESPONSE_BYTES = 1_000_000; +const PUBLIC_MAX_RESPONSE_BYTES = 1_000_000; -export type PublicFailureCode = +type PublicFailureCode = | 'upstream_timeout' | 'upstream_redirect' | 'upstream_status' @@ -103,7 +103,7 @@ export type PublicFailureCode = | 'upstream_shape' | 'upstream_unreachable'; -export interface PublicFailure { +interface PublicFailure { error: { code: PublicFailureCode; message: string }; } diff --git a/src/core/services/server/realtimeSpeech.ts b/src/core/services/server/realtimeSpeech.ts index 8ee0903..e6eaf89 100644 --- a/src/core/services/server/realtimeSpeech.ts +++ b/src/core/services/server/realtimeSpeech.ts @@ -4,7 +4,7 @@ import 'server-only'; export const REALTIME_CLIENT_SECRETS_URL = 'https://api.openai.com/v1/realtime/client_secrets'; -export const REALTIME_TRANSCRIPTION_MODEL = 'gpt-live-transcribe'; +const REALTIME_TRANSCRIPTION_MODEL = 'gpt-live-transcribe'; const MINT_DEADLINE_MS = 10_000; type Env = Record; @@ -14,13 +14,13 @@ export function realtimeSpeechConfigured(env: Env = process.env): boolean { return env.OMNI_SPEECH_REALTIME_ENABLED === '1' && !!env.OPENAI_API_KEY?.trim(); } -export function transcriptionLanguage(locale: unknown): string | undefined { +function transcriptionLanguage(locale: unknown): string | undefined { if (typeof locale !== 'string') return undefined; const match = /^([a-z]{2})(?:-[A-Za-z]{2})?$/.exec(locale.trim()); return match?.[1]; } -export type MintResult = +type MintResult = | { ok: true; clientSecret: string; model: string; expiresAt?: number } | { ok: false; status: number; error: 'unconfigured' | 'provider-error' | 'timeout' }; diff --git a/src/core/services/server/registryIntent.ts b/src/core/services/server/registryIntent.ts index e9a9b79..bfbd81a 100644 --- a/src/core/services/server/registryIntent.ts +++ b/src/core/services/server/registryIntent.ts @@ -22,13 +22,13 @@ export const TYPESAFE_MODEL = 'jev-latest'; const RESOLVED_MODEL = /^jev-\d+\.\d+(\.\d+)?$/; export const MAX_QUERY_CHARS = 200; -export const INTENT_DEADLINE_MS = 8000; +const INTENT_DEADLINE_MS = 8000; const INSTRUCTIONS = 'Which kind of API is this person looking for? Choose the closest option. ' + 'This only routes a search; it does not answer the request.'; -export type IntentFailure = +type IntentFailure = | 'not_configured' | 'query_invalid' | 'timeout' @@ -36,11 +36,11 @@ export type IntentFailure = | 'upstream_status' | 'upstream_shape'; -export type IntentOutcome = +type IntentOutcome = | { ok: true; scores: IntentScore[]; model: string } | { ok: false; failure: IntentFailure }; -export interface ClassifyOptions { +interface ClassifyOptions { apiKey?: string; fetchImpl?: typeof fetch; signal?: AbortSignal; diff --git a/src/core/services/shell.snapshot.test.ts b/src/core/services/shell.snapshot.test.ts index c9b710e..29120bf 100644 --- a/src/core/services/shell.snapshot.test.ts +++ b/src/core/services/shell.snapshot.test.ts @@ -176,7 +176,7 @@ describe('captureShellSnapshot', () => { }); it('does not send the observations pool (earlier answers) to the model', () => { - useMindStore.getState().addToPool('observations', { + useMindStore.getState().pushContext('observations', { type: 'analysis', content: 'a prior Think answer', importance: 0.8 }); diff --git a/src/core/services/wire.service.ts b/src/core/services/wire.service.ts index 994c4dc..2d61afc 100644 --- a/src/core/services/wire.service.ts +++ b/src/core/services/wire.service.ts @@ -7,7 +7,7 @@ import { useBlockStore } from '../stores'; import { useWireStore } from '../stores/wireStore'; import { WireFilters, ContextSource } from '../schemas/wire.schema'; -import { PolymarketMarket, NewsArticle } from '../schemas/block.schema'; +import { PolymarketMarket } from '../schemas/block.schema'; function isRecord(value: unknown): value is Record { return typeof value === 'object' && value !== null && !Array.isArray(value); @@ -65,7 +65,7 @@ function asMemoryEntries(value: unknown): Array<{ content: string }> { */ export function extractBlockData( blockId: string, - filters: WireFilters + _filters: WireFilters ): string | null { const block = useBlockStore.getState().getBlock(blockId); if (!block?.data) return null; @@ -75,24 +75,7 @@ export function extractBlockData( try { // Handle different data types - if (isRecord(data) && Array.isArray(data.markets)) { - // Polymarket data - extracted = formatFilteredMarkets(data.markets as PolymarketMarket[], filters); - } else if (isRecord(data) && Array.isArray(data.articles)) { - // News data - const articles = data.articles as NewsArticle[]; - const filtered = filters.timeWindow && filters.timeWindow !== 'all' - ? filterArticlesByTimeWindow(articles, filters.timeWindow) - : articles; - - if (filtered.length === 0) { - extracted = ''; - } else if (filters.summaryOnly) { - extracted = formatNewsSummary(filtered); - } else { - extracted = formatNewsDetailed(filtered); - } - } else if (isRecord(data) && 'personaType' in data && Array.isArray(data.messages)) { + if (isRecord(data) && 'personaType' in data && Array.isArray(data.messages)) { // A persona used as a SOURCE: another mind's conclusion, not its // internals. This used to fall through to the generic JSON dump, // which fed the downstream persona ids, timestamps and isCollapsed @@ -100,9 +83,7 @@ export function extractBlockData( const lastAnswer = lastPersonaAnswer(data); if (!lastAnswer) return null; - extracted = filters.summaryOnly - ? lastAnswer.content.slice(0, 500) + (lastAnswer.content.length > 500 ? '…' : '') - : lastAnswer.content; + extracted = lastAnswer.content; } else if (isRecord(data) && 'poolId' in data && Array.isArray(data.entries)) { // Memory block — a Mind pool wired in like any other source. extracted = asMemoryEntries(data.entries) @@ -111,15 +92,13 @@ export function extractBlockData( .join('\n'); } else if (isRecord(data) && typeof data.content === 'string') { // Text block - extracted = filters.summaryOnly - ? data.content.slice(0, 500) + (data.content.length > 500 ? '...' : '') - : data.content; + extracted = data.content; } else if (isRecord(data) && Array.isArray(data.items)) { // OmniItem[] from the gateway (polymarket, coingecko, fred, metaculus, // hackernews, …). The useful signal lives in each item's `metadata` // (probability, volume, price, value, …) — include it, don't drop it. const items = data.items; - const limit = filters.summaryOnly ? 8 : 25; + const limit = 25; extracted = items.slice(0, limit) .map(item => formatOmniItem(item)) .join('\n'); @@ -129,10 +108,10 @@ export function extractBlockData( if (isRecord(first) && 'question' in first && 'outcomes' in first) { // PolymarketMarket[] (how the Polymarket block stores its data): // use the dedicated formatter so outcomes + volume are included. - extracted = formatFilteredMarkets(data as PolymarketMarket[], filters); + extracted = formatMarketsDetailed(data as PolymarketMarket[]); } else if (isRecord(first) && 'metadata' in first) { // OmniItem[] stored directly. - const limit = filters.summaryOnly ? 8 : 25; + const limit = 25; extracted = data.slice(0, limit).map(item => formatOmniItem(item)).join('\n'); if (data.length > limit) extracted += `\n… ${data.length - limit} more`; } else { @@ -150,12 +129,6 @@ export function extractBlockData( extracted = carriesNothing(data) ? '' : JSON.stringify(data, null, 2).slice(0, 2000); } - // Apply field filtering if specified - if (filters.fields && filters.fields.length > 0) { - // This would require more sophisticated field extraction - // For now, we'll just include everything - } - // A source that carried nothing is not grounding. Return null, not a // placeholder like '(No data)': aggregateWireContext cites every // source that returns text, so a placeholder would be cited as @@ -216,76 +189,6 @@ function formatOmniItem(item: unknown): string { return `- ${title}${metaStr}${descStr}`; } -/** - * Apply the wire's time window and format what is left. '' when nothing is - * left, so no formatter emits a header for zero rows. - */ -function formatFilteredMarkets(markets: PolymarketMarket[], filters: WireFilters): string { - const filtered = filters.timeWindow && filters.timeWindow !== 'all' - ? filterByTimeWindow(markets, filters.timeWindow) - : markets; - if (filtered.length === 0) return ''; - return filters.summaryOnly ? formatMarketsSummary(filtered) : formatMarketsDetailed(filtered); -} - -/** - * Filter markets by time window - */ -function filterByTimeWindow( - markets: PolymarketMarket[], - timeWindow: 'hour' | 'day' | 'week' -): PolymarketMarket[] { - const now = Date.now(); - const cutoffs = { - hour: now - 60 * 60 * 1000, - day: now - 24 * 60 * 60 * 1000, - week: now - 7 * 24 * 60 * 60 * 1000 - }; - - const cutoff = cutoffs[timeWindow]; - - return markets.filter(market => { - const endDate = new Date(market.endDate).getTime(); - return endDate >= cutoff; - }); -} - -/** - * Filter articles by time window - */ -function filterArticlesByTimeWindow( - articles: NewsArticle[], - timeWindow: 'hour' | 'day' | 'week' -): NewsArticle[] { - const now = Date.now(); - const cutoffs = { - hour: now - 60 * 60 * 1000, - day: now - 24 * 60 * 60 * 1000, - week: now - 7 * 24 * 60 * 60 * 1000 - }; - - const cutoff = cutoffs[timeWindow]; - - return articles.filter(article => { - const publishDate = new Date(article.publishedAt).getTime(); - return publishDate >= cutoff; - }); -} - -/** - * Format markets as summary - */ -function formatMarketsSummary(markets: PolymarketMarket[]): string { - const lines = markets.slice(0, 5).map(market => { - const topOutcome = market.outcomes.reduce((max, outcome) => - outcome.probability > max.probability ? outcome : max - ); - return `• ${market.question.slice(0, 80)}${market.question.length > 80 ? '...' : ''}\n → ${topOutcome.name}: ${(topOutcome.probability * 100).toFixed(1)}%`; - }); - - return `**Prediction Markets** (${markets.length} total)\n\n${lines.join('\n\n')}`; -} - /** * Format markets with full details */ @@ -302,35 +205,6 @@ function formatMarketsDetailed(markets: PolymarketMarket[]): string { return lines.join('\n\n---\n\n'); } -/** - * Format news as summary - */ -function formatNewsSummary(articles: NewsArticle[]): string { - const lines = articles.slice(0, 10).map(article => { - const sentiment = article.sentiment - ? ` [${article.sentiment === 'positive' ? '📈' : article.sentiment === 'negative' ? '📉' : '➖'}]` - : ''; - return `• ${article.title}${sentiment}\n ${article.source} • ${new Date(article.publishedAt).toLocaleDateString()}`; - }); - - return `**News Feed** (${articles.length} articles)\n\n${lines.join('\n\n')}`; -} - -/** - * Format news with full details - */ -function formatNewsDetailed(articles: NewsArticle[]): string { - const lines = articles.map(article => { - const sentiment = article.sentiment - ? ` [Sentiment: ${article.sentiment === 'positive' ? 'Positive' : article.sentiment === 'negative' ? 'Negative' : 'Neutral'}]` - : ''; - - return `**${article.title}**${sentiment}\n${article.description || ''}\n*${article.source}* • ${new Date(article.publishedAt).toLocaleString()}\n[Read more](${article.url})`; - }); - - return lines.join('\n\n---\n\n'); -} - /** What kind of evidence a source block contributes. */ function sourceKindFor(blockId: string): ContextSource['kind'] { if (blockId === 'memory_pool') return 'memory'; diff --git a/src/core/services/wireExtract.test.ts b/src/core/services/wireExtract.test.ts index 63c2c35..4bba442 100644 --- a/src/core/services/wireExtract.test.ts +++ b/src/core/services/wireExtract.test.ts @@ -89,22 +89,6 @@ describe('extractBlockData — empty content carries nothing', () => { it.each(empties)('returns null for %s (no placeholder to cite)', (_label, data) => { seed('empty', data); expect(extractBlockData('empty', DEFAULT_WIRE_FILTERS)).toBeNull(); - expect(extractBlockData('empty', { ...DEFAULT_WIRE_FILTERS, summaryOnly: true })).toBeNull(); - }); - - const expiredMarket = { - id: 'm', question: 'Old?', outcomes: [{ id: 'y', name: 'Yes', probability: 0.5 }], - volume: 1, liquidity: 0, endDate: '2001-01-01', category: 'x', tags: [] - }; - - it.each([ - ['wrapped { markets }', { markets: [expiredMarket] }], - ['direct PolymarketMarket[]', [expiredMarket]] - ])('returns null when a time window filters every market of a %s away', (_shape, data) => { - seed('old', data); - for (const summaryOnly of [true, false]) { - expect(extractBlockData('old', { ...DEFAULT_WIRE_FILTERS, timeWindow: 'day', summaryOnly })).toBeNull(); - } }); it('still returns JSON for an object that carries a value (0 and false are data)', () => { diff --git a/src/core/shells/templates.test.ts b/src/core/shells/templates.test.ts index 4765b31..9092acd 100644 --- a/src/core/shells/templates.test.ts +++ b/src/core/shells/templates.test.ts @@ -3,7 +3,6 @@ import { SHELL_TEMPLATES, getShellTemplate, validateTemplate, - validateAllTemplates, keyedProvidersForTemplate, type ShellTemplate } from './templates'; @@ -24,7 +23,7 @@ describe('SHELL_TEMPLATES integrity (against the real registry)', () => { }); it('every template references only registered block_ids and valid refs', () => { - const problems = validateAllTemplates(isRegistered); + const problems = SHELL_TEMPLATES.flatMap(t => validateTemplate(t, isRegistered)); // If this fails, a template names a block that does not exist in the // registry, or a connection points at a missing block. expect(problems).toEqual([]); diff --git a/src/core/shells/templates.ts b/src/core/shells/templates.ts index 220b533..b86c984 100644 --- a/src/core/shells/templates.ts +++ b/src/core/shells/templates.ts @@ -10,7 +10,7 @@ import type { PersonaType, AestheticTheme } from '@/core/schemas/shell.schema'; import { API_CATALOG, type ApiProvider } from '@/core/schemas/api.schema'; /** A block placement inside a template (placeholder instanceId, remapped on use). */ -export interface TemplateBlock { +interface TemplateBlock { /** Local placeholder id, unique within the template; remapped on instantiate. */ ref: string; /** Registry block_id this placement instantiates. */ @@ -22,7 +22,7 @@ export interface TemplateBlock { } /** A wire between two template blocks, by their local `ref`s. */ -export interface TemplateConnection { +interface TemplateConnection { sourceRef: string; targetRef: string; } @@ -201,8 +201,3 @@ export function validateTemplate( return problems; } - -/** Validate every built-in template. Returns all problems across templates. */ -export function validateAllTemplates(isRegistered: (blockId: string) => boolean): string[] { - return SHELL_TEMPLATES.flatMap(t => validateTemplate(t, isRegistered)); -} diff --git a/src/core/stores/hydrateValidation.test.ts b/src/core/stores/hydrateValidation.test.ts index 1508f82..7ecc984 100644 --- a/src/core/stores/hydrateValidation.test.ts +++ b/src/core/stores/hydrateValidation.test.ts @@ -5,14 +5,13 @@ import { useWireStore } from './wireStore'; import { useShellStore } from './shellStore'; import { useMindStore } from './mindStore'; import { vaultStorage } from '../vault/vaultStorage'; -import { getHydrationRejections, __resetHydrationRejections } from '../vault/hydration'; import { DEFAULT_WIRE_FILTERS, type DataWire } from '../schemas/wire.schema'; import type { BlockInstance, OmniBlockSchema } from '../schemas/block.schema'; import type { ShellConfig } from '../schemas/shell.schema'; import { LLM_DEFAULTS, createInitialMindState } from '../schemas/mind.schema'; // A record read back from the vault is checked against its schema before it -// becomes state. A record that fails is dropped, recorded, and warned about; +// becomes state. A record that fails is dropped and warned about; // the records next to it survive. Nothing is cast. const schema: OmniBlockSchema = { @@ -77,9 +76,23 @@ async function readVault(key: string): Promise<{ state: Record; let warn: MockInstance; +interface Rejection { + store: string; + id: string | null; + reason: string; +} + +/** The records the hydrate refused, in order, read from its warnings. */ +function rejections(store?: string): Rejection[] { + return warn.mock.calls + .map(([message]) => /^\[vault\] ([^:\s]+): dropped persisted record(?: ([^:\s]+))?: ([\s\S]*)$/.exec(String(message))) + .filter((match): match is RegExpExecArray => match !== null) + .map(([, name, id, reason]) => ({ store: name, id: id ?? null, reason })) + .filter(rejection => store === undefined || rejection.store === store); +} + beforeEach(async () => { warn = vi.spyOn(console, 'warn').mockImplementation(() => {}); - __resetHydrationRejections(); for (const key of ['omni-blocks', 'omni-wires', 'omni-shells', 'omni-mind']) { await vaultStorage.removeItem(key); } @@ -110,7 +123,7 @@ describe('omni-blocks hydrate', () => { await useBlockStore.persist.rehydrate(); expect(useBlockStore.getState().blocks.map(b => b.instance_id)).toEqual(['b_ok']); - const rejected = getHydrationRejections().filter(r => r.store === 'omni-blocks'); + const rejected = rejections('omni-blocks'); expect(rejected.map(r => r.id)).toEqual(['b_missing', 'b_wrong_type', 'b_no_type', null]); expect(rejected[0].reason).toContain('position'); expect(rejected[1].reason).toContain('schema.block_id'); @@ -122,22 +135,22 @@ describe('omni-blocks hydrate', () => { await writeVault('omni-blocks', { blocks: [block('b_ok')], activeShellId: 7 }, 2); await useBlockStore.persist.rehydrate(); expect(useBlockStore.getState().activeShellId).toBe('root'); - expect(getHydrationRejections().map(r => r.reason).join()).toContain('activeShellId'); + expect(rejections('omni-blocks').map(r => r.reason).join()).toContain('activeShellId'); }); it('rejects a blocks field that is not an array and keeps an empty canvas', async () => { await writeVault('omni-blocks', { blocks: { b_ok: block('b_ok') }, activeShellId: 'root' }, 2); await useBlockStore.persist.rehydrate(); expect(useBlockStore.getState().blocks).toEqual([]); - expect(getHydrationRejections()).toHaveLength(1); - expect(getHydrationRejections()[0].reason).toContain('array'); + expect(warn).toHaveBeenCalledTimes(1); + expect(rejections('omni-blocks')[0].reason).toContain('array'); }); it('hydrates nothing and records nothing when the vault is empty', async () => { await useBlockStore.persist.rehydrate(); expect(useBlockStore.getState().blocks).toEqual([]); expect(useBlockStore.getState().activeShellId).toBe('root'); - expect(getHydrationRejections()).toEqual([]); + expect(warn).not.toHaveBeenCalled(); }); }); @@ -157,7 +170,7 @@ describe('omni-wires hydrate', () => { await useWireStore.persist.rehydrate(); expect(useWireStore.getState().wires.map(w => w.id)).toEqual(['w_ok']); - const rejected = getHydrationRejections().filter(r => r.store === 'omni-wires'); + const rejected = rejections('omni-wires'); expect(rejected.map(r => r.id)).toEqual(['w_missing', 'w_filters']); expect(rejected[0].reason).toContain('targetBlockId'); expect(rejected[1].reason).toContain('filters'); @@ -188,7 +201,7 @@ describe('omni-shells hydrate', () => { expect(state.activeShellId).toBeNull(); expect(state.hotkeySlots).toEqual({ 1: 's_ok' }); - const rejected = getHydrationRejections().filter(r => r.store === 'omni-shells'); + const rejected = rejections('omni-shells'); expect(rejected.map(r => r.id)).toEqual(['s_missing', 's_partly_bad', 's_partly_w', null]); expect(rejected[0].reason).toContain('name'); expect(rejected[1].reason).toContain('position'); @@ -220,13 +233,13 @@ describe('omni-mind hydrate', () => { await useMindStore.persist.rehydrate(); const state = useMindStore.getState(); - expect(state.getPoolEntries('memory')).toEqual([entry]); + expect(state.contextPools.find(p => p.id === 'memory')?.entries ?? []).toEqual([entry]); expect(state.activePersonaId).toBe('strategist'); expect(state.llmConfig.provider).toBe('anthropic'); expect(state.personas.map(p => p.id)).toEqual(fresh.personas.map(p => p.id)); // Built-in pools the blob lacked are still added. expect(state.contextPools.map(p => p.id).sort()).toEqual(fresh.contextPools.map(p => p.id).sort()); - expect(getHydrationRejections()).toEqual([]); + expect(warn).not.toHaveBeenCalled(); // Written back at the new version, so the migration runs once. await new Promise(r => setTimeout(r, 30)); @@ -250,13 +263,13 @@ describe('omni-mind hydrate', () => { await useMindStore.persist.rehydrate(); const state = useMindStore.getState(); - expect(state.getPoolEntries('memory')).toEqual([good]); + expect(state.contextPools.find(p => p.id === 'memory')?.entries ?? []).toEqual([good]); expect(state.contextPools.some(p => p.id === 'custom_pool')).toBe(false); expect(state.personas.map(p => p.id)).toEqual([fresh.personas[0].id]); expect(state.llmConfig).toEqual(fresh.llmConfig); expect(state.graph).toEqual({ nodes: [], edges: [], lastUpdated: state.graph.lastUpdated }); - const rejected = getHydrationRejections().filter(r => r.store === 'omni-mind'); + const rejected = rejections('omni-mind'); expect(rejected.map(r => r.id)).toEqual([null, null, 'p_bad', 'custom_pool', 'ctx_bad']); expect(rejected[0].reason).toContain('llmConfig'); expect(rejected[1].reason).toContain('graph'); diff --git a/src/core/stores/mindStore.ts b/src/core/stores/mindStore.ts index 45fb9d5..4df6fb9 100644 --- a/src/core/stores/mindStore.ts +++ b/src/core/stores/mindStore.ts @@ -93,8 +93,6 @@ interface MindStore extends MindState { // Context Pools // ================== pushContext: (poolId: string, entry: Omit) => string; - addToPool: (poolId: string, entry: Omit) => string; // Alias for pushContext - getPoolEntries: (poolId: string) => ContextEntry[]; clearPool: (poolId: string) => void; // ================== @@ -264,14 +262,6 @@ export const useMindStore = create()( return id; }, - // Alias for pushContext - used by MindEngine - addToPool: (poolId, entry) => get().pushContext(poolId, entry), - - getPoolEntries: (poolId) => { - const pool = get().contextPools.find(p => p.id === poolId); - return pool?.entries || []; - }, - clearEphemeralContext: () => { const EPHEMERAL_POOLS = ['observations', 'predictions', 'directives', 'inferences']; set(state => ({ @@ -460,48 +450,9 @@ export const useMindStore = create()( */ function formatBlockDataForFocus(blockType: string, data: unknown): string { if (!data) return `[${blockType}] No data available`; - - switch (blockType) { - case 'polymarket': { - const markets = data as Array<{ - question: string; - outcomes: Array<{ name: string; probability: number }>; - volume?: number; - endDate?: string; - }>; - if (!markets.length) return '[Polymarket] No markets loaded'; - - const formatted = markets.slice(0, 10).map(m => { - const outcomes = m.outcomes?.map(o => - ` - ${o.name}: ${(o.probability * 100).toFixed(1)}%` - ).join('\n') || ' No outcomes'; - return `📊 ${m.question}\n${outcomes}${m.volume ? `\n Volume: $${m.volume.toLocaleString()}` : ''}`; - }).join('\n\n'); - - return `[POLYMARKET FOCUS - ${markets.length} markets]\n\n${formatted}`; - } - - case 'newsapi': { - const articles = data as Array<{ - title: string; - source: { name: string }; - description?: string; - publishedAt?: string; - }>; - if (!articles.length) return '[News] No articles loaded'; - - const formatted = articles.slice(0, 10).map(a => - `📰 ${a.title}\n Source: ${a.source?.name || 'Unknown'}${a.description ? `\n ${a.description.slice(0, 200)}...` : ''}` - ).join('\n\n'); - - return `[NEWS FOCUS - ${articles.length} articles]\n\n${formatted}`; - } - - default: - try { - return `[${blockType.toUpperCase()} FOCUS]\n${JSON.stringify(data, null, 2).slice(0, 2000)}`; - } catch { - return `[${blockType}] Data format unknown`; - } + try { + return `[${blockType.toUpperCase()} FOCUS]\n${JSON.stringify(data, null, 2).slice(0, 2000)}`; + } catch { + return `[${blockType}] Data format unknown`; } } diff --git a/src/core/stores/shellStore.ts b/src/core/stores/shellStore.ts index ab7ea96..caf73ac 100644 --- a/src/core/stores/shellStore.ts +++ b/src/core/stores/shellStore.ts @@ -101,9 +101,6 @@ interface ShellState { /** Set current persona */ setPersona: (persona: PersonaType) => void; - /** Set current aesthetic */ - setAesthetic: (aesthetic: AestheticTheme) => void; - /** Get active shell */ getActiveShell: () => ShellConfig | undefined; } @@ -171,8 +168,6 @@ export const useShellStore = create()( setPersona: (persona) => set({ currentPersona: persona }), - setAesthetic: (aesthetic) => set({ currentAesthetic: aesthetic }), - getActiveShell: () => { const state = get(); return state.shells.find(s => s.id === state.activeShellId); diff --git a/src/core/stores/shellWorkflow.test.ts b/src/core/stores/shellWorkflow.test.ts index d67b72f..aad9552 100644 --- a/src/core/stores/shellWorkflow.test.ts +++ b/src/core/stores/shellWorkflow.test.ts @@ -104,7 +104,7 @@ describe('shell workflow — wire cleanup (A1 orphan-wire regressions)', () => { useBlockStore.getState().removeBlock(analyst.instance_id); expect(useWireStore.getState().getWiresToBlock(analyst.instance_id)).toHaveLength(0); - expect(useWireStore.getState().getWiresFromBlock(analyst.instance_id)).toHaveLength(0); + expect(useWireStore.getState().wires.filter(w => w.sourceBlockId === analyst.instance_id)).toHaveLength(0); }); it('legacy shells (BlockConnection[]) convert to live wires on load', () => { diff --git a/src/core/stores/toolStore.ts b/src/core/stores/toolStore.ts index c57d468..11d1d0d 100644 --- a/src/core/stores/toolStore.ts +++ b/src/core/stores/toolStore.ts @@ -9,9 +9,9 @@ import { create } from 'zustand'; // TYPES // ============================================ -export type ToolType = 'navigate' | 'highlighter'; +type ToolType = 'navigate' | 'highlighter'; -export interface SelectionData { +interface SelectionData { text: string; sourceBlockId?: string; sourceBlockType?: string; diff --git a/src/core/stores/wireStore.ts b/src/core/stores/wireStore.ts index d02cd6e..163843d 100644 --- a/src/core/stores/wireStore.ts +++ b/src/core/stores/wireStore.ts @@ -113,18 +113,12 @@ interface WireStoreState { /** Re-admit every wire in the store; refused wires are dropped (storage hydrate) */ revalidatePersistedWires: () => void; - /** Get wires where block is source */ - getWiresFromBlock: (blockId: string) => DataWire[]; - /** Get wires where block is target (persona) */ getWiresToBlock: (blockId: string) => DataWire[]; /** Check if wire exists between two blocks */ wireExists: (sourceBlockId: string, targetBlockId: string) => boolean; - /** Get a specific wire */ - getWire: (wireId: string) => DataWire | undefined; - /** Get all wires for a specific shell */ getWiresByShell: (shellId: string) => DataWire[]; } @@ -206,10 +200,6 @@ export const useWireStore = create()( set(state => ({ wires: readmitWires(state.wires) })); }, - getWiresFromBlock: (blockId) => { - return get().wires.filter(w => w.sourceBlockId === blockId); - }, - getWiresToBlock: (blockId) => { return get().wires.filter(w => w.targetBlockId === blockId); }, @@ -220,10 +210,6 @@ export const useWireStore = create()( ); }, - getWire: (wireId) => { - return get().wires.find(w => w.id === wireId); - }, - getWiresByShell: (shellId) => { return get().wires.filter(w => w.shellId === shellId); } diff --git a/src/core/vault/hydration.ts b/src/core/vault/hydration.ts index 0f23421..ebbbac4 100644 --- a/src/core/vault/hydration.ts +++ b/src/core/vault/hydration.ts @@ -4,7 +4,7 @@ // becomes state. Until this existed every store hydrated by casting: a blob // that lost a field, or carried a value of the wrong type, became live state // and failed later, far from the cause. Now a record that does not fit is -// dropped and recorded here, and the records next to it survive. +// dropped with a console warning, and the records next to it survive. // // The schema source of truth is the TypeScript interface. `Shape` makes // a spec list every required key of `T`, so adding a required field to an @@ -13,31 +13,7 @@ // unions have no runtime form, and a cosmetic value is not worth a record. // ============================================ -/** One persisted record the hydrate refused, and why. */ -export interface HydrationRejection { - /** The persist name of the store the record came from (`omni-blocks`...). */ - store: string; - /** The record's own id when it had one. */ - id: string | null; - /** The first field that failed, and how. */ - reason: string; - at: number; -} - -const rejections: HydrationRejection[] = []; - -/** Every record refused since the page loaded. */ -export function getHydrationRejections(): readonly HydrationRejection[] { - return rejections; -} - -/** Test hook. */ -export function __resetHydrationRejections(): void { - rejections.length = 0; -} - function recordRejection(store: string, id: string | null, reason: string): void { - rejections.push({ store, id, reason, at: Date.now() }); console.warn(`[vault] ${store}: dropped persisted record${id ? ` ${id}` : ''}: ${reason}`); } @@ -47,7 +23,7 @@ function recordRejection(store: string, id: string | null, reason: string): void * a nested shape. */ export type FieldSpec = string | readonly string[] | ShapeSpec; -export interface ShapeSpec { +interface ShapeSpec { readonly [field: string]: FieldSpec; } diff --git a/src/core/vault/index.ts b/src/core/vault/index.ts index 312af2d..d5c55c9 100644 --- a/src/core/vault/index.ts +++ b/src/core/vault/index.ts @@ -3,6 +3,5 @@ // ============================================ export { vaultStorage, getVaultHealth, getInitialVaultHealth, subscribeVaultHealth } from './vaultStorage'; -export type { VaultHealth, VaultFailure } from './vaultStorage'; +export type { VaultHealth } from './vaultStorage'; export { exportVault, importVault, isVaultExport, prepareVaultImport } from './vaultExport'; -export type { OmniVaultExport, VaultImportPlan, VaultImportReport } from './vaultExport'; diff --git a/src/core/vault/vaultExport.ts b/src/core/vault/vaultExport.ts index fb9c1bc..076bb03 100644 --- a/src/core/vault/vaultExport.ts +++ b/src/core/vault/vaultExport.ts @@ -69,14 +69,14 @@ export function isVaultExport(obj: unknown): obj is OmniVaultExport { .every(([k, v]) => k.startsWith(OMNI_KEY_PREFIX) && typeof v === 'string'); } -export interface VaultImportPlan { +interface VaultImportPlan { /** What the import will write, keyed like `OmniVaultExport.data`. */ data: Record; /** Write and destructive capabilities in the file; each lands `pending`. */ needsApproval: ImportedCapability[]; } -export interface VaultImportReport { +interface VaultImportReport { /** Number of restored keys. */ restored: number; needsApproval: ImportedCapability[]; diff --git a/src/core/vault/vaultStorage.ts b/src/core/vault/vaultStorage.ts index 9071bc6..56f70ac 100644 --- a/src/core/vault/vaultStorage.ts +++ b/src/core/vault/vaultStorage.ts @@ -31,7 +31,7 @@ function idbAvailable(): boolean { } /** The last vault operation that failed, by DOMException name (or a vault code). */ -export interface VaultFailure { +interface VaultFailure { code: string; at: number; }