YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context
Rust agentsagiaiai-memoryaudio-recordingcomputer-visionhermeshermes-agentllmlocal-ailocal-firstmachine-learningmcpmultimodalopenclawprivacyrewindscreen-recordingspeech-to-textycombinator
chore(ai-gateway): prepare isolated Experiential adapter and ZDR checks (#7338)
Louis Beaumont3d96e77e3fb3
diff --git a/packages/ai-gateway/scripts/EXPERIENTIAL_MIGRATION.md b/packages/ai-gateway/scripts/EXPERIENTIAL_MIGRATION.mdnew file mode 100644index 000000000..99fa9a89f--- /dev/null+++ b/packages/ai-gateway/scripts/EXPERIENTIAL_MIGRATION.md@@ -0,0 +1,217 @@+# Experiential migration preparation++Status: preparation and synthetic probes only. These scripts stay outside the+Worker import graph. They do not change the production provider factory,+Cloudflare routing, secrets, webhooks, deployment configuration or traffic.++```text+Before: customer -> existing gateway/auth/limits -> existing providers+After: customer -> existing gateway/auth/limits -> existing providers+ operator -> read-only preflight -> Experiential account+ operator -> approved synthetic probe + provider.zdr=true -> Experiential+ <- require x-gateway-zdr:true before reading JSON/SSE+```++## Offline inventory++From `packages/ai-gateway`:++```sh+bun scripts/export-migration-inventory.ts > migration-inventory.json+```++Imports the current repository model/plan policy. Destination mappings and live+allowances remain null. Source credit constants are not a live rule export. Run+from a clean reviewed checkout; the report records the current Git revision.+Keep generated reports private when enriched with account or production facts.++## Read-only preflight++Load the test credential into `EXPLABS_API_KEY` from your secret manager inside+the calling process. Never put it in CLI arguments, source, logs or a ticket.+Do not use your Cloudflare or upstream provider credential for this request.++```sh+bun scripts/experiential-preflight.ts > experiential-preflight.json+```++Only GETs the documented API host. Compares exact authenticated model IDs and+reads prompt capture, ZDR entitlement, continuation storage and upstream policy.+Reports exact boolean model ZDR capability separately from catalog membership.+Refuses redirects, redacts upstream+errors, and never tries another key or host. Exit 1 means inspection failed;+exit 2 means a report was produced with migration gates still unresolved. Catalog+access cannot mark the account production ready.++## Optional synthetic probes++Require spending approval, a test-only credential, billing activation, ZDR+entitlement (or an existing grandfathered org ZDR policy), no-training routing,+disabled ZDR continuation storage and a model with an authenticated catalog ZDR+route. Missing or malformed privacy facts block dispatch. Preflight runs again each time. Each+invocation sends exactly one synthetic POST, at most 64 output tokens, with no+retries or fallback. Review the model price before approving: token bounds are+not a dollar guarantee. Reconcile uncertain requests before retrying.++```sh+bun scripts/experiential-smoke.ts --allow-spend gpt-5.4-nano chat+bun scripts/experiential-smoke.ts --allow-spend gpt-5.4-nano stream+bun scripts/experiential-smoke.ts --allow-spend gpt-5.4-nano tools+bun scripts/experiential-smoke.ts --allow-spend gpt-5.4-nano json-schema+bun scripts/experiential-smoke.ts --allow-spend gpt-6-astra responses+bun scripts/experiential-smoke.ts --allow-spend claude-sonnet-5 messages+```++Run only approved cases. These are examples, not a batch command. An unavailable+model fails without substitution. Responses keeps Astra stateless with low+reasoning and Standard service (OpenAI wire value `service_tier: "default"`, checked+against the installed SDK type). Confidential/custom, classifier, Auto and background+rescue IDs are excluded. Streaming requires content, successful finish, usage and+DONE. Plain Chat and schema probes require `finish_reason: "stop"`; filtered,+truncated and unexpected finish reasons fail. Messages requires `end_turn` and+nonempty text. Responses requires overall completion and a completed assistant+message containing nonempty output text. Reasoning alone, refusals and partial+answers do not pass; reasoning alongside a completed answer is allowed.+Tool/schema checks inspect actual arguments or JSON, not just HTTP status.+The output omits prompts, completion content, raw headers and secrets. It records+only the boolean ZDR request/response confirmations and existing usage fields. Inline cost is+optional and never proves settlement or customer allowance accounting.++These probes test the vendor wire contract, not the deployed Screenpipe Worker+or a completed routing adapter. Images, cache accounting, provider-native stream+translation and multi-customer enforcement still need end-to-end proof.++## Per-request privacy and consent boundary++Every probe sends a top-level `"provider": {"zdr": true}` in the JSON body on+Chat Completions, Responses and Messages. This is a request field, not a request+header. With the Anthropic SDK it belongs in `extra_body`. The documented+`x-gateway-zdr: true` response header is required before reading either JSON or+SSE; missing, false or ambiguous values cancel the body and fail without retry.+A response confirmation reports the vendor's verdict, not an independent audit+of its storage. Check live enforcement before sending customer content.++According to the vendor's data-controls contract, per-request ZDR suppresses+prompt/response capture even when `capture_prompt_content` is on, excludes+noncompliant upstream routes including fallbacks, and never loosens org policy.+That is why account capture being on alone no longer blocks a ZDR probe. Pro+entitlement still applies. `403 needs_subscription`, `403 model_not_granted`, and+an older engine's `400` for unsupported `provider` all terminate the probe. Never+remove the field or retry on a retaining route to make it succeed. No-training is+a separate requirement and cannot substitute for ZDR.++These probes have no consent-based exception: all requests use ZDR. Responses+uses `store:false` and no `previous_response_id`; ZDR continuation storage must be+explicitly off. No batch upload or `Idempotency-Key` replay storage is used.+Content-free billing metadata is still retained by the vendor.++The production consent integration is **not implemented here**. Screenpipe's+existing Workflows opt-in covers redacted contributions and training Screenpipe's+own models (`lib/trajectories/collector.ts` and+`components/workflows/workflow-sharing-controls.tsx` in the desktop app). It does+not authorize Experiential to retain raw inference requests. Keep inference ZDR+for both opted-in and opted-out users; consented sharing remains a separate flow.+Before any future retention exception, verify the specific consent purpose and+version against the authenticated account server-side. Missing, stale, revoked,+cross-account or client-supplied consent cannot relax privacy. Exercise these+cases, account switching, all protocol paths and fallbacks in the future runtime+adapter; neither this report nor these synthetic tests prove those customer flows.++## Inactive adapter and local tests++`lib/experiential-adapter.ts` is outside the Worker import graph. Its default+mode refuses all activity; only an explicit `isolated-test` caller can prepare+one request. It does not select a production provider, synchronize entitlements,+or activate a cutover. The existing Bun tests are its consumers:++```sh+bun test src/test/experiential-migration.unit.test.ts src/test/experiential-adapter.unit.test.ts src/test/experiential-sdk-parity.unit.test.ts+```++The SDK tests exercise the installed OpenAI and Anthropic serializers and stream+parsers using injected synthetic transports. They cover tools, images, cache+usage, structured-output serialization, and Astra Responses-to-Chat translation.+Astra stays Low, Standard (`service_tier: "default"` on the wire), and stateless+(`store: false`). Anthropic's existing schema support remains system-prompt+instructions, not a claim of native constrained decoding.++The adapter requires server-verified Screenpipe authentication and a trusted+server registry binding the actor to a customer identity, customer key, source+plan, and expected destination plan key/version/assignment revision. The+provisioning identity and management credential stay separate from inference.+It performs these actual reads before preparing a request:++- Management key: organization, ZDR entitlement, no-training policy, disabled+ ZDR continuation storage, authenticated model ZDR capability, exact catalog+ slug, and `/api/models/{slug}` canonical UUID. Account capture may remain on+ because per-request ZDR suppresses capture under the vendor contract.+- Customer key: `/api/whoami` with both identity fields matching the registry+ and `is_provisioning: false`, customer-visible model listing, and its own+ `customer-plan` assignment matching the pinned tuple with no pending change.++Older servers without identity proof fail closed. Missing models, alternate+model guesses, stale assignments, shared management/customer keys, and privacy+failures never dispatch. Gateway identities are artifact IDs; organization and+canonical model IDs are UUIDs. The adapter sends at most one inference POST per+prepared connection, refuses replay, strips caller/provider/Cloudflare auth+headers, overwrites caller provider preferences with `provider: { zdr: true }`,+and requires exactly `x-gateway-zdr: true` before the SDK consumes JSON or SSE.+Missing or unsafe verdicts cancel the body without retry. Every user requires+ZDR, with no consent-based retention exception. It defaults to 64 output tokens. An operator may explicitly select+128 for an isolated probe; higher or missing request bounds are refused. Neither+bound is a dollar guarantee or permission to spend.++The opt-in HTTP integration test uses a separately provisioned, synthetic-only+local platform stack. It reads a private JSON fixture from the environment and+refuses non-loopback URLs, any model other than `gpt-5.4-mini`, or a fixture not+marked `syntheticUpstream: true`:++```sh+SCREENPIPE_EXPERIENTIAL_LOCAL_FIXTURE=/absolute/private-fixture.json \+ bun test src/test/experiential-adapter.integration.test.ts --timeout=30000+```++The fixture contains `controlBaseUrl`, `gatewayBaseUrl`, `managementKey`, `orgId`,+`provisioningIdentityId`, `canonicalModelUuid`, `model`, `syntheticUpstream`, and+two `customers`. Each customer has `userId`, `identityId`, `customerKey`,+`accountPlan`, and `expectedPlan: { plan_key, plan_version, revision }`. Obtain+these values from the isolated stack's actual control API; do not fabricate+policy readbacks. Keep the fixture outside source control. Without it, the test+is skipped. It exercises one JSON and one streaming request under distinct+customer keys, but neither provisions policy nor proves real-provider behavior.++The provider fixes preserve OpenAI's upstream nonstream `finish_reason` and+normalize Anthropic nonstream tool-call IDs/types and terminal reasons into Chat+format. Regression tests cover a second tool turn. These fixes do not change+routing or imply deployment. Encrypted Tinfoil/EHBP, ordinary custom GLM,+Vertex, classifier, audio, and background rescue routes remain unchanged.++## Gates before implementing production selection++1. Pin deployed behavior and read actual Cloudflare limits, reset windows,+ settled usage, reservations and late/pending exposure.+2. Verify native enforcement across the serving fleet. The documented disjoint+ model groups and UTC-month allowances do not alone preserve shared-total plus+ frontier caps or other windows. Do not invent or approximate commercial limits.+3. Bind customer keys to authenticated identities and approved plans before+ issuance. Keep management credentials separate. Test two synthetic identities,+ rotation, forbidden models, concurrent holds, plan transitions and scoped+ reporting. Request labels authorize nothing.+4. Verify exact model mappings, rates, privacy, cache accounting and retries.+ Keep encrypted Tinfoil/EHBP, custom GLM, Vertex Messages, classifier, audio+ and background rescue separate without their own compatibility proof.+5. Reconcile usage carry-in and rollback across both gateways. A fresh destination+ balance must not reset usage or grant duplicate allowance.+6. Then review a separate routing change and stage with synthetic users.+ Production promotion still requires the owner's explicit signal.++Do not use an unrestricted org key as a temporary customer route. These scripts+never mint keys, assign plans, upload BYOK, change settings, purchase, deploy,+connect subscription webhooks or revoke existing gateway credentials.++Official contracts checked September 28, 2026:++- [Account and identities](https://platform.experientiallabs.ai/docs/account-api)+- [Usage and allowances](https://platform.experientiallabs.ai/docs/cost-api)+- [Privacy](https://platform.experientiallabs.ai/docs/data-controls)+- [Wire reference](https://platform.experientiallabs.ai/llms.txt)diff --git a/packages/ai-gateway/scripts/experiential-preflight.ts b/packages/ai-gateway/scripts/experiential-preflight.tsnew file mode 100644index 000000000..7d86e7372--- /dev/null+++ b/packages/ai-gateway/scripts/experiential-preflight.ts@@ -0,0 +1,17 @@+// screenpipe — AI that knows everything you've seen, said, or heard+// https://screenpipe.com++import { getHostedAiAllowedModels } from '../src/services/hosted-ai-policy';+import { inspectExperientialAccount, MigrationCheckError } from './lib/experiential-migration';++try {+ const report = await inspectExperientialAccount(process.env.EXPLABS_API_KEY ?? '', [+ ...getHostedAiAllowedModels('basic'), ...getHostedAiAllowedModels('business'),+ ]);+ console.log(JSON.stringify(report, null, 2));+ // Deliberately nonzero until migration gates are proven. This is not a deploy gate override.+ process.exitCode = 2;+} catch (error) {+ console.error(error instanceof MigrationCheckError ? error.message : 'Preflight failed; no changes made');+ process.exitCode = 1;+}diff --git a/packages/ai-gateway/scripts/experiential-smoke.ts b/packages/ai-gateway/scripts/experiential-smoke.tsnew file mode 100644index 000000000..ab464c1d8--- /dev/null+++ b/packages/ai-gateway/scripts/experiential-smoke.ts@@ -0,0 +1,19 @@+// screenpipe — AI that knows everything you've seen, said, or heard+// https://screenpipe.com++import { MigrationCheckError, runSyntheticProbe, type ProbeProtocol } from './lib/experiential-migration';++const args = process.argv.slice(2);+if (args.length !== 3 || args[0] !== '--allow-spend') {+ console.error('Usage: bun scripts/experiential-smoke.ts --allow-spend <exact-model-id> <chat|stream|tools|json-schema|responses|messages>');+ console.error('One synthetic inference request, at most 64 output tokens. Requires explicit spending approval and a configured test key in EXPLABS_API_KEY.');+ process.exitCode = 1;+} else {+ try {+ console.log(JSON.stringify(await runSyntheticProbe({ key: process.env.EXPLABS_API_KEY ?? '',+ model: args[1], protocol: args[2] as ProbeProtocol, allowSpend: true }), null, 2));+ } catch (error) {+ console.error(error instanceof MigrationCheckError ? error.message : 'Probe failed; outcome may be uncertain. Do not retry automatically.');+ process.exitCode = 1;+ }+}diff --git a/packages/ai-gateway/scripts/export-migration-inventory.ts b/packages/ai-gateway/scripts/export-migration-inventory.tsnew file mode 100644index 000000000..51db92bf8--- /dev/null+++ b/packages/ai-gateway/scripts/export-migration-inventory.ts@@ -0,0 +1,47 @@+// screenpipe — AI that knows everything you've seen, said, or heard+// https://screenpipe.com++// Offline preparation only. Imports source policy, never reads environment+// variables, credentials, customers or deployed settings, and makes no requests.+// Run from packages/ai-gateway: bun scripts/export-migration-inventory.ts+import { getHostedAiAllowedModels, getHostedAiIncludedCredits } from '../src/services/hosted-ai-policy';+import { gatewayProviderForModel } from '../src/services/cloudflare-ai-gateway';+import { isFrontierModel } from '../src/services/cost-tracker';+import type { AccountPlan } from '../src/types';++const plans: AccountPlan[] = ['free', 'basic', 'business', 'business_max', 'business_ultra', 'team', 'enterprise', 'unknown'];+const source = Bun.spawnSync(['git', 'rev-parse', 'HEAD'], { cwd: import.meta.dir });+if (source.exitCode !== 0) throw new Error('Cannot identify source revision');+const modelIds = [...new Set(plans.flatMap((plan) => [...getHostedAiAllowedModels(plan)]))];++console.log(JSON.stringify({+ schema_version: 1,+ evidence: 'repository source only; not a production or destination configuration',+ source_revision: source.stdout.toString().trim(),+ generated_at: new Date().toISOString(),+ plans: plans.map((plan) => ({+ plan,+ allowed_models: getHostedAiAllowedModels(plan),+ source_advertised_credits: getHostedAiIncludedCredits(plan),+ live_allowance: null,+ })),+ models: modelIds.map((id) => ({+ source_model_id: id,+ frontier: isFrontierModel(id),+ cloudflare_provider: gatewayProviderForModel(id),+ routing_note: id === 'auto' ? 'Resolved by Screenpipe router before provider selection'+ : id === 'screenpipe-event-classifier' ? 'Separate self-hosted route'+ : 'Hosted chat path only; separate encrypted and direct routes also exist',+ experiential_model_slug: null,+ experiential_canonical_model_uuid: null,+ })),+ unresolved: [+ 'Live Cloudflare rule values, windows, enabled state and current usage',+ 'Deployed Worker policy versus source revision',+ 'Destination catalog, plan enforcement activation and per-customer identity mapping',+ 'Equivalent shared-total plus frontier sub-limit enforcement',+ 'Current-period usage and in-flight liability transfer, including rollback',+ 'Provider authentication, privacy and live protocol parity',+ ],+ cutover_authorized: false,+}, null, 2));diff --git a/packages/ai-gateway/scripts/lib/experiential-adapter.ts b/packages/ai-gateway/scripts/lib/experiential-adapter.tsnew file mode 100644index 000000000..931fe67e8--- /dev/null+++ b/packages/ai-gateway/scripts/lib/experiential-adapter.ts@@ -0,0 +1,156 @@+// screenpipe — AI that knows everything you've seen, said, or heard+// https://screenpipe.com++// INACTIVE by default, outside the Worker import graph. This single-request+// adapter is only for an isolated harness with an injected transport. It is not+// a production selector, provisioning client, or substitute for native limits.+import type { AccountPlan, AuthResult } from '../../src/types';+import { hostedChatActorId } from '../../src/services/cloudflare-ai-gateway';+import { isHostedAiModelAllowed } from '../../src/services/hosted-ai-policy';+import { EXPERIENTIAL_ORIGIN, MigrationCheckError, inspectExperientialAccount, verifyExperientialCustomerKey, verifyExperientialCanonicalModel, isExperientialIdentityId, verifyExperientialCustomerPlan, type Fetcher } from './experiential-migration';++export interface ExperientialCustomerBinding {+ actorId: string;+ orgId: string;+ customerIdentityId: string;+ provisioningIdentityId: string;+ accountPlan: AccountPlan;+ expectedPlan: { plan_key: string; plan_version: number; revision: number };+ // Server registry only. Never accept this record from client JSON or headers.+ customerKey: string;+}+export interface ExperientialModelMapping {+ sourceModelId: string;+ destinationModelId: string;+ canonicalModelUuid: string;+ protocol: 'chat' | 'messages' | 'responses';+}+export interface ExperientialAdapterOptions {+ mode?: 'inactive' | 'isolated-test';+ // Explicit operator-selected probe bound, never a customer request override.+ maxOutputTokens?: 64 | 128;+ auth: AuthResult;+ sourceModelId: string;+ mappings: readonly ExperientialModelMapping[];+ // Read-only management preflight requires its own authorized credential.+ // It is never sent to inference or exposed in returned SDK options.+ managementKey: string;+ // Must be a trusted server-owned registry populated by authoritative Clerk /+ // website entitlement synchronization, separately from customer inference.+ lookupBinding: (actorId: string) => Promise<ExperientialCustomerBinding | null>;+ transport: Fetcher;+}+const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;+const KEY = /^xpl_[0-9a-f]{40}$/;++/** Return native SDK connection options only after local and read-only gates.+ * The caller supplies the existing provider implementation; all normalization,+ * tools, images, and Responses -> Chat conversion stay in those providers. */+export async function prepareExperientialProbeConnection(options: ExperientialAdapterOptions) {+ if (options.mode !== 'isolated-test') throw new MigrationCheckError('experiential_adapter_inactive');+ const maxOutputTokens = options.maxOutputTokens ?? 64;+ if (maxOutputTokens !== 64 && maxOutputTokens !== 128) throw new MigrationCheckError('invalid_probe_output_bound');+ const { auth, sourceModelId } = options;+ if (auth.isValid !== true || auth.service === true || !auth.userId?.trim() || auth.accountPlan === 'unknown') {+ throw new MigrationCheckError('verified_customer_entitlement_required');+ }+ if (!isHostedAiModelAllowed(sourceModelId, auth.accountPlan)) throw new MigrationCheckError('model_not_allowed_for_source_plan');+ // Auto must be resolved first. GLM, encrypted EHBP, Vertex, audio, classifier+ // and rescue remain on existing paths even if someone supplies a mapping.+ if (!/^(gpt-|claude-)/.test(sourceModelId)) throw new MigrationCheckError('separate_route_not_migrated');+ const matches = options.mappings.filter(mapping => mapping.sourceModelId === sourceModelId);+ if (matches.length !== 1) throw new MigrationCheckError('exact_model_mapping_required');+ const mapping = { ...matches[0] };+ // Do not guess equivalent or replacement models. Any differing catalog slug+ // needs independently verified mapping support in a later reviewed change.+ if (mapping.destinationModelId !== sourceModelId || !UUID.test(mapping.canonicalModelUuid)) {+ throw new MigrationCheckError('exact_model_mapping_required');+ }+ const protocol = sourceModelId === 'gpt-6-astra' ? 'responses' : sourceModelId.startsWith('claude-') ? 'messages' : 'chat';+ if (mapping.protocol !== protocol) throw new MigrationCheckError('native_protocol_mapping_mismatch');+ const actorId = await hostedChatActorId(auth);+ const found = await options.lookupBinding(actorId);+ if (!found) throw new MigrationCheckError('customer_binding_missing');+ const binding = { ...found };+ if (binding.actorId !== actorId || binding.accountPlan !== auth.accountPlan ||+ !UUID.test(binding.orgId) || !isExperientialIdentityId(binding.customerIdentityId) || !isExperientialIdentityId(binding.provisioningIdentityId) ||+ binding.customerIdentityId === binding.provisioningIdentityId || !KEY.test(binding.customerKey)) {+ throw new MigrationCheckError('customer_binding_invalid_or_stale');+ }+ if (binding.customerKey === options.managementKey || !KEY.test(options.managementKey)) {+ throw new MigrationCheckError('separate_management_credential_required');+ }+ const preflight = await inspectExperientialAccount(options.managementKey, [mapping.destinationModelId], options.transport);+ if (preflight.org_id !== binding.orgId) throw new MigrationCheckError('customer_organization_mismatch');+ if (!preflight.checks.upstream_no_training || !preflight.checks.zdr_request_entitled ||+ !preflight.checks.zdr_continuation_storage_disabled) {+ throw new MigrationCheckError('privacy_preflight_failed');+ }+ if (!preflight.models[0]?.exact_catalog_match) throw new MigrationCheckError('model_not_in_authenticated_catalog');+ if (!preflight.models[0].zdr_route_available) throw new MigrationCheckError('model_has_no_verified_zdr_route');+ await verifyExperientialCanonicalModel(options.managementKey, mapping.destinationModelId, mapping.canonicalModelUuid, options.transport);+ await verifyExperientialCustomerKey(binding.customerKey, binding.orgId, binding.customerIdentityId, mapping.destinationModelId, options.transport);+ await verifyExperientialCustomerPlan(binding.customerKey, binding.orgId, binding.customerIdentityId, binding.expectedPlan, options.transport);+ const path = protocol === 'messages' ? '/v1/messages' : protocol === 'responses' ? '/v1/responses' : '/v1/chat/completions';+ let posted = false;+ const transport = options.transport;+ const sdkFetch = async (url: string | URL | Request, init?: RequestInit): Promise<Response> => {+ // Validate every dispatch, including SDK/provider compatibility retries.+ if (String(url) !== `${EXPERIENTIAL_ORIGIN}${path}` || init?.method?.toUpperCase() !== 'POST' || typeof init.body !== 'string') {+ throw new MigrationCheckError('unexpected_probe_request');+ }+ let body: Record<string, any>;+ try { body = JSON.parse(init.body); } catch { throw new MigrationCheckError('invalid_probe_body'); }+ if (!body || body.model !== mapping.destinationModelId) throw new MigrationCheckError('exact_model_mapping_required');+ const outputLimit = protocol === 'responses' ? body.max_output_tokens : protocol === 'messages' ? body.max_tokens : body.max_completion_tokens ?? body.max_tokens;+ if (!Number.isInteger(outputLimit) || outputLimit < 1 || outputLimit > maxOutputTokens) {+ throw new MigrationCheckError('bounded_synthetic_output_required');+ }+ if (protocol === 'responses' && (body.store !== false || body.previous_response_id !== undefined ||+ body.reasoning?.effort !== 'low' || body.service_tier !== 'default')) {+ throw new MigrationCheckError('astra_stateless_low_standard_required');+ }+ // This harness never permits retention, including for users who opted in.+ // Replace caller provider preferences so they cannot weaken routing policy.+ body.provider = { zdr: true };+ if (posted) throw new MigrationCheckError('probe_replay_forbidden');+ posted = true;+ // Construct auth from the registry, not forwarded headers. Do not leak CF+ // credentials/metadata, org-default auth, or caller-supplied policy labels.+ const incoming = new Headers(init.headers);+ const headers = new Headers({ Authorization: `Bearer ${binding.customerKey}`, 'Content-Type': 'application/json', Accept: 'application/json' });+ if (protocol === 'messages') {+ headers.set('anthropic-version', incoming.get('anthropic-version') ?? '2023-06-01');+ const beta = incoming.get('anthropic-beta');+ if (beta) headers.set('anthropic-beta', beta);+ }+ let response: Response;+ try {+ response = await transport(String(url), { method: 'POST', headers, body: JSON.stringify(body), redirect: 'error',+ signal: init.signal ? AbortSignal.any([init.signal, AbortSignal.timeout(30_000)]) : AbortSignal.timeout(30_000) });+ } catch { throw new MigrationCheckError('transport_failed_no_retry'); }+ if (response.url && new URL(response.url).origin !== EXPERIENTIAL_ORIGIN) {+ await response.body?.cancel(); throw new MigrationCheckError('unexpected_response_origin');+ }+ if (!response.ok) {+ await response.body?.cancel();+ throw new MigrationCheckError('request_rejected_no_retry', response.status);+ }+ // Check before the SDK can consume JSON or any SSE event. Never retry a+ // missing/false verdict: dispatch may already have incurred usage.+ if (response.headers.get('x-gateway-zdr') !== 'true') {+ await response.body?.cancel();+ throw new MigrationCheckError('zdr_response_not_confirmed_no_retry');+ }+ return response;+ };+ return {+ apiKey: binding.customerKey,+ baseURL: protocol === 'messages' ? EXPERIENTIAL_ORIGIN : `${EXPERIENTIAL_ORIGIN}/v1`,+ maxRetries: 0,+ defaultHeaders: { Authorization: `Bearer ${binding.customerKey}`, ...(protocol === 'messages' ? { 'x-api-key': null } : {}) },+ fetch: sdkFetch,+ production_ready: false as const,+ cutover_authorized: false as const,+ };+}diff --git a/packages/ai-gateway/scripts/lib/experiential-migration.ts b/packages/ai-gateway/scripts/lib/experiential-migration.tsnew file mode 100644index 000000000..19019aaef--- /dev/null+++ b/packages/ai-gateway/scripts/lib/experiential-migration.ts@@ -0,0 +1,275 @@+// screenpipe — AI that knows everything you've seen, said, or heard+// https://screenpipe.com++import type { ResponseCreateParamsNonStreaming } from 'openai/resources/responses/responses';+import { getHostedAiAllowedModels } from '../../src/services/hosted-ai-policy';++// Deliberately outside the Worker import graph. These tools prepare and test a+// destination account; they do not select a production route or assign a plan.+export const EXPERIENTIAL_ORIGIN = 'https://api.experientiallabs.ai';+export type Fetcher = (url: string, init: RequestInit) => Promise<Response>;+type Json = Record<string, any>;+export type ProbeProtocol = 'chat' | 'stream' | 'tools' | 'json-schema' | 'responses' | 'messages';+const KEY = /^xpl_[0-9a-f]{40}$/;+const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;+// Gateway identities are artifact IDs, not UUIDs. Org/model IDs remain UUIDs.+export function isExperientialIdentityId(value: string): boolean {+ return value.length <= 128 && /^[a-z][a-z0-9]*([._-][a-z0-9]+)*$/.test(value);+}++export class MigrationCheckError extends Error {+ constructor(public readonly code: string, public readonly status?: number) {+ // Never include upstream error bodies, request headers, or credential values.+ super(status === undefined ? code : `${code} (HTTP ${status})`);+ this.name = 'MigrationCheckError';+ }+}++async function request(key: string, path: string, fetcher: Fetcher, body?: Json): Promise<Response> {+ if (!KEY.test(key)) throw new MigrationCheckError('invalid_credential_format');+ const response = await fetcher(`${EXPERIENTIAL_ORIGIN}${path}`, {+ method: body === undefined ? 'GET' : 'POST',+ redirect: 'error',+ signal: AbortSignal.timeout(30_000),+ headers: { Authorization: `Bearer ${key}`, Accept: 'application/json',+ ...(body === undefined ? {} : { 'Content-Type': 'application/json' }) },+ ...(body === undefined ? {} : { body: JSON.stringify(body) }),+ }).catch(() => { throw new MigrationCheckError('transport_failed_no_retry'); });+ if (!response.ok) {+ await response.body?.cancel();+ throw new MigrationCheckError('request_rejected_no_retry', response.status);+ }+ // Defense in depth for injected transports as well as native redirect:error.+ if (response.url && new URL(response.url).origin !== EXPERIENTIAL_ORIGIN) {+ await response.body?.cancel();+ throw new MigrationCheckError('unexpected_response_origin');+ }+ return response;+}++async function read(key: string, path: string, fetcher: Fetcher): Promise<Json> {+ const response = await request(key, path, fetcher);+ try { return await response.json() as Json; }+ catch { throw new MigrationCheckError('invalid_json_response'); }+}++export async function inspectExperientialAccount(key: string, sourceModels: string[], fetcher: Fetcher = fetch) {+ const who = await read(key, '/api/whoami', fetcher);+ if (typeof who.org_id !== 'string' || !UUID.test(who.org_id)) {+ throw new MigrationCheckError('invalid_organization_response');+ }+ // Read only. No key creation, customer export, card, inference, or settings writes.+ const [catalog, telemetry, providers] = await Promise.all([+ read(key, '/v1/models', fetcher),+ read(key, `/api/orgs/${who.org_id}/telemetry-settings`, fetcher),+ read(key, `/api/orgs/${who.org_id}/provider-policy`, fetcher),+ ]);+ if (!Array.isArray(catalog.data) || catalog.data.some((m: any) => typeof m?.id !== 'string')) {+ throw new MigrationCheckError('invalid_catalog_response');+ }+ const models = new Map<string, Json>(catalog.data.map((m: Json) => [m.id, m]));+ return {+ org_id: who.org_id as string,+ checks: {+ prompt_capture_disabled: telemetry.capture_prompt_content === false,+ upstream_no_training: providers.policy?.require_no_training === true,+ upstream_zero_retention: providers.policy?.require_zdr === true,+ // Existing require_zdr is grandfathered even without a current Pro entitlement.+ zdr_request_entitled: providers.zdr_entitled === true || providers.policy?.require_zdr === true,+ zdr_continuation_storage_disabled: providers.policy?.zdr_continuation_storage === false,+ },+ models: [...new Set(sourceModels)].map((id) => ({ id, exact_catalog_match: models.has(id),+ zdr_route_available: models.get(id)?.data_policy?.zdr === true || models.get(id)?.data_policy?.zdr_on_request === true,+ })),+ // These need live operator/provider evidence; a catalog read cannot pass them.+ unverified: [+ 'native_customer_plan_enforcement', 'per_customer_identity_and_key_isolation',+ 'shared_total_and_frontier_caps', 'reset_window_and_usage_carry_in',+ 'settled_and_pending_usage_reconciliation', 'protocol_and_price_parity',+ 'billing_activation', 'live_cloudflare_rules_and_deployed_baseline',+ 'live_per_request_zdr_enforcement', 'server_owned_customer_consent_and_revocation',+ ],+ production_ready: false as const,+ cutover_authorized: false as const,+ };+}++export async function verifyExperientialCanonicalModel(managementKey: string, model: string, canonicalUuid: string, fetcher: Fetcher) {+ if (!UUID.test(canonicalUuid) || !/^(gpt-|claude-)[a-z0-9.-]+$/.test(model)) throw new MigrationCheckError('exact_model_mapping_required');+ const detail = await read(managementKey, `/api/models/${encodeURIComponent(model)}`, fetcher);+ if (detail.model?.id !== canonicalUuid || detail.model?.slug !== model) {+ throw new MigrationCheckError('canonical_model_mapping_mismatch');+ }+}++// Requires the identity-proof extension to /api/whoami. Older deployments fail+// closed instead of treating organization membership as customer isolation.+export async function verifyExperientialCustomerKey(key: string, orgId: string, identityId: string, model: string, fetcher: Fetcher) {+ if (!UUID.test(orgId) || !isExperientialIdentityId(identityId)) throw new MigrationCheckError('invalid_customer_identity');+ const who = await read(key, '/api/whoami', fetcher);+ if (who.org_id !== orgId || who.identity_id !== identityId || who.customer_plan_identity_id !== identityId || who.is_provisioning !== false) {+ throw new MigrationCheckError('identity_bound_customer_key_required');+ }+ const catalog = await read(key, '/v1/models', fetcher);+ if (!Array.isArray(catalog.data) || !catalog.data.some((entry: Json) => entry?.id === model)) {+ throw new MigrationCheckError('model_not_in_authenticated_catalog');+ }+}++export async function verifyExperientialCustomerPlan(key: string, orgId: string, identityId: string,+ expected: { plan_key: string; plan_version: number; revision: number }, fetcher: Fetcher) {+ if (!UUID.test(orgId) || !isExperientialIdentityId(identityId) ||+ !expected || !/^[a-z][a-z0-9_-]{0,63}$/.test(expected.plan_key) ||+ !Number.isSafeInteger(expected.plan_version) || expected.plan_version < 1 ||+ !Number.isSafeInteger(expected.revision) || expected.revision < 1) {+ throw new MigrationCheckError('expected_customer_plan_required');+ }+ const assignment = await read(key, `/api/orgs/${orgId}/identities/${encodeURIComponent(identityId)}/customer-plan`, fetcher);+ if (assignment.org_id !== orgId || assignment.identity_id !== identityId ||+ assignment.plan_key !== expected.plan_key || assignment.plan_version !== expected.plan_version || assignment.revision !== expected.revision ||+ assignment.pending_plan_key !== null || assignment.pending_plan_version !== null || assignment.pending_effective_at !== null) {+ throw new MigrationCheckError('customer_plan_assignment_stale_or_pending');+ }+}++const PROMPT = 'Reply with OK.';+// OpenAI's wire value for Standard service is "default", not "standard".+const STATELESS_RESPONSES_OPTIONS = { service_tier: 'default', store: false } satisfies+ Pick<ResponseCreateParamsNonStreaming, 'service_tier' | 'store'>;+const tool = { type: 'function', function: { name: 'confirm', description: 'Return confirmation.',+ parameters: { type: 'object', properties: { ok: { type: 'boolean' } }, required: ['ok'], additionalProperties: false } } };++export function syntheticRequest(model: string, protocol: ProbeProtocol): { path: string; body: Json } {+ // Every probe requires ZDR. Screenpipe sharing consent does not authorize+ // vendor retention, and this tool deliberately has no privacy opt-out.+ const provider = { zdr: true };+ // Excludes aliases, auto, confidential/custom, audio and background rescue routes.+ if (!getHostedAiAllowedModels('business').includes(model) || !/^(gpt-|claude-)/.test(model)) {+ throw new MigrationCheckError('unsupported_probe_model');+ }+ if (model === 'gpt-6-astra' && protocol !== 'responses') throw new MigrationCheckError('astra_requires_responses');+ if (protocol === 'messages') {+ if (!model.startsWith('claude-')) throw new MigrationCheckError('messages_requires_claude');+ return { path: '/v1/messages', body: { model, provider, max_tokens: 64, messages: [{ role: 'user', content: PROMPT }] } };+ }+ if (protocol === 'responses') {+ if (model !== 'gpt-6-astra') throw new MigrationCheckError('responses_probe_requires_astra');+ return { path: '/v1/responses', body: { model, provider, input: [{ role: 'user', content: PROMPT }],+ max_output_tokens: 64, reasoning: { effort: 'low' }, ...STATELESS_RESPONSES_OPTIONS } };+ }+ if (!['chat', 'stream', 'tools', 'json-schema'].includes(protocol)) throw new MigrationCheckError('unsupported_protocol');+ const body: Json = { model, provider, messages: [{ role: 'user', content: PROMPT }], max_completion_tokens: 64 };+ if (protocol === 'stream') Object.assign(body, { stream: true, stream_options: { include_usage: true } });+ if (protocol === 'tools') Object.assign(body, { tools: [tool], tool_choice: { type: 'function', function: { name: 'confirm' } } });+ if (protocol === 'json-schema') body.response_format = { type: 'json_schema', json_schema: {+ name: 'confirmation', strict: true, schema: tool.function.parameters,+ } };+ return { path: '/v1/chat/completions', body };+}++function validateCompletion(data: Json, protocol: ProbeProtocol): void {+ if (data.error) throw new MigrationCheckError('upstream_error_in_success_body');+ if (protocol === 'responses') {+ if (data.status !== 'completed' || !Array.isArray(data.output) || !data.output.some((item: Json) =>+ item?.type === 'message' && item.role === 'assistant' && item.status === 'completed' &&+ Array.isArray(item.content) && item.content.some((part: Json) =>+ part?.type === 'output_text' && typeof part.text === 'string' && part.text.trim()))) {+ throw new MigrationCheckError('responses_not_completed');+ }+ } else if (protocol === 'messages') {+ if (data.stop_reason !== 'end_turn' || !Array.isArray(data.content) || !data.content.some((part: Json) =>+ part?.type === 'text' && typeof part.text === 'string' && part.text.trim())) {+ throw new MigrationCheckError('messages_not_completed');+ }+ } else {+ const choice = data.choices?.[0];+ if (protocol !== 'tools' && choice?.finish_reason !== 'stop') throw new MigrationCheckError('chat_not_completed');+ if (protocol === 'tools') {+ const call = choice?.message?.tool_calls?.[0];+ if (choice?.finish_reason !== 'tool_calls' || call?.function?.name !== 'confirm') throw new MigrationCheckError('tool_call_missing');+ try { if (JSON.parse(call.function.arguments)?.ok !== true) throw new Error(); }+ catch { throw new MigrationCheckError('tool_arguments_invalid'); }+ } else if (protocol === 'json-schema') {+ try { const value = JSON.parse(choice.message?.content); if (value?.ok !== true || Object.keys(value).length !== 1) throw new Error(); }+ catch { throw new MigrationCheckError('structured_output_invalid'); }+ } else if (typeof choice.message?.content !== 'string' || !choice.message.content.trim()) {+ throw new MigrationCheckError('chat_content_missing');+ }+ }+}++async function readStream(response: Response): Promise<Json> {+ if (!response.headers.get('content-type')?.includes('text/event-stream')) throw new MigrationCheckError('expected_event_stream');+ if (!response.body) throw new MigrationCheckError('stream_body_missing');+ const reader = response.body.getReader();+ const decoder = new TextDecoder();+ let pending = '', size = 0, done = false, finished = false, content = false;+ let usage: Json | undefined;+ try {+ while (true) {+ const chunk = await reader.read();+ if (chunk.done) break;+ size += chunk.value.byteLength;+ if (size > 1_048_576) throw new MigrationCheckError('probe_response_too_large');+ pending += decoder.decode(chunk.value, { stream: true });+ let end: number;+ while ((end = pending.indexOf('\n')) >= 0) {+ const line = pending.slice(0, end).replace(/\r$/, ''); pending = pending.slice(end + 1);+ if (!line.startsWith('data:')) continue;+ const value = line.slice(5).trim();+ if (!value) continue;+ if (value === '[DONE]') { done = true; continue; }+ if (done) throw new MigrationCheckError('data_after_stream_done');+ let data: Json;+ try { data = JSON.parse(value); } catch { throw new MigrationCheckError('invalid_stream_json'); }+ if (data.error) throw new MigrationCheckError('stream_error');+ if (data.usage) usage = data.usage;+ const choice = data.choices?.[0];+ if (choice?.finish_reason === 'stop') finished = true;+ if (typeof choice?.delta?.content === 'string' && choice.delta.content.length > 0) content = true;+ }+ }+ if (!done || !finished || !content) throw new MigrationCheckError('incomplete_stream_no_retry');+ return { usage };+ } finally { await reader.cancel().catch(() => {}); }+}++export async function runSyntheticProbe(options: {+ key: string; model: string; protocol: ProbeProtocol; allowSpend: boolean;+}, fetcher: Fetcher = fetch) {+ if (options.allowSpend !== true) throw new MigrationCheckError('explicit_spend_authorization_required');+ const spec = syntheticRequest(options.model, options.protocol);+ const before = await inspectExperientialAccount(options.key, [options.model], fetcher);+ // Documented per-request ZDR suppresses capture even when the org switch is+ // on. No-training remains separate; continuation storage must stay disabled.+ if (!before.checks.upstream_no_training || !before.checks.zdr_request_entitled ||+ !before.checks.zdr_continuation_storage_disabled) throw new MigrationCheckError('privacy_preflight_failed');+ if (!before.models[0]?.exact_catalog_match) throw new MigrationCheckError('model_not_in_authenticated_catalog');+ if (!before.models[0].zdr_route_available) throw new MigrationCheckError('model_has_no_verified_zdr_route');+ // Exactly one synthetic POST. No retries, provider fallback, customer data,+ // previous_response_id, BYOK upload or production configuration changes.+ const response = await request(options.key, spec.path, fetcher, spec.body);+ // Confirm before consuming JSON or SSE. A missing/false/ambiguous verdict+ // cannot prove compliance, and must never trigger a retry without provider.zdr.+ if (response.headers.get('x-gateway-zdr') !== 'true') {+ await response.body?.cancel().catch(() => {});+ throw new MigrationCheckError('zdr_response_not_confirmed_no_retry');+ }+ let data: Json;+ try { data = options.protocol === 'stream' ? await readStream(response) : await response.json() as Json; }+ catch (error) { if (error instanceof MigrationCheckError) throw error; throw new MigrationCheckError('invalid_probe_response_no_retry'); }+ if (options.protocol !== 'stream') validateCompletion(data, options.protocol);+ const usage = data.usage;+ if (!usage || typeof usage !== 'object') throw new MigrationCheckError('usage_missing_no_retry');+ const input = usage.input_tokens ?? usage.prompt_tokens;+ const output = usage.output_tokens ?? usage.completion_tokens;+ if (!Number.isInteger(input) || input < 0 || !Number.isInteger(output) || output < 0) throw new MigrationCheckError('usage_invalid');+ const requestId = response.headers.get('x-request-id');+ return { protocol: options.protocol, model: options.model, request_id: requestId,+ zdr_requested: true, zdr_response_confirmed: true,+ input_tokens: input, output_tokens: output,+ // Optional inline cost is not authoritative settlement or allowance proof.+ inline_cost_usd: typeof usage.cost === 'number' && Number.isFinite(usage.cost) && usage.cost >= 0 ? usage.cost : null,+ settlement_verified: false, native_policy_verified: false, production_ready: false,+ };+}diff --git a/packages/ai-gateway/src/providers/anthropic.ts b/packages/ai-gateway/src/providers/anthropic.tsindex 1671eed44..84c7eb8f6 100644--- a/packages/ai-gateway/src/providers/anthropic.ts+++ b/packages/ai-gateway/src/providers/anthropic.ts@@ -587,13 +587,15 @@ export class AnthropicProvider implements AIProvider {return {choices: [{+ finish_reason: anthropicStopReasonToOpenAI(response.stop_reason),message: {content: textContent,role: 'assistant',tool_calls: response.content.filter((block): block is ContentBlock => block.type === 'tool_use').map((block) => ({- type: block.type,+ id: (block as any).id,+ type: 'function',function: {name: (block as any).name,arguments: JSON.stringify((block as any).input),diff --git a/packages/ai-gateway/src/providers/openai.ts b/packages/ai-gateway/src/providers/openai.tsindex a0c971bf2..65c266b04 100644--- a/packages/ai-gateway/src/providers/openai.ts+++ b/packages/ai-gateway/src/providers/openai.ts@@ -741,6 +741,7 @@ export class OpenAIProvider implements AIProvider {return {choices: [{+ finish_reason: response.choices[0].finish_reason,message: {content: response.choices[0].message.content,role: 'assistant',diff --git a/packages/ai-gateway/src/test/experiential-adapter.integration.test.ts b/packages/ai-gateway/src/test/experiential-adapter.integration.test.tsnew file mode 100644index 000000000..a04f722f6--- /dev/null+++ b/packages/ai-gateway/src/test/experiential-adapter.integration.test.ts@@ -0,0 +1,98 @@+// screenpipe — AI that knows everything you've seen, said, or heard+// https://screenpipe.com++// Opt-in actual local control API + gateway HTTP. This never seeds or changes+// plans/settings and refuses non-loopback destinations or real upstreams.+// SCREENPIPE_EXPERIENTIAL_LOCAL_FIXTURE=/private/path.json bun test this-file+import { describe, expect, it } from 'bun:test';+import { prepareExperientialProbeConnection, type ExperientialCustomerBinding } from '../../scripts/lib/experiential-adapter';+import { EXPERIENTIAL_ORIGIN, type Fetcher } from '../../scripts/lib/experiential-migration';+import { hostedChatActorId } from '../services/cloudflare-ai-gateway';+import { OpenAIProvider } from '../providers/openai';+import type { AccountPlan, AuthResult, RequestBody } from '../types';++interface LocalFixture {+ controlBaseUrl: string;+ gatewayBaseUrl: string;+ managementKey: string;+ orgId: string;+ provisioningIdentityId: string;+ canonicalModelUuid: string;+ model: string;+ syntheticUpstream: true;+ customers: Array<{ userId: string; identityId: string; customerKey: string; accountPlan: AccountPlan; expectedPlan: ExperientialCustomerBinding['expectedPlan'] }>;+}+const fixturePath = process.env.SCREENPIPE_EXPERIENTIAL_LOCAL_FIXTURE;+function loopbackBase(value: string) {+ const url = new URL(value);+ if (url.protocol !== 'http:' || url.hostname !== '127.0.0.1' || !url.port || url.username || url.password ||+ url.pathname !== '/' || url.search || url.hash) throw new Error('local fixture must use an explicit IPv4 loopback port');+ return url.origin;+}++describe.skipIf(!fixturePath)('inactive adapter with actual isolated control API and gateway', () => {+ it('authenticates two customers independently and runs native SDK JSON and stream requests', async () => {+ const fixture = await Bun.file(fixturePath!).json() as LocalFixture;+ if (fixture.syntheticUpstream !== true || fixture.model !== 'gpt-5.4-mini' || fixture.customers.length !== 2) {+ throw new Error('requires two synthetic customers and the exact gpt-5.4-mini local fixture');+ }+ const control = loopbackBase(fixture.controlBaseUrl);+ const gateway = loopbackBase(fixture.gatewayBaseUrl);+ const dispatches: Array<{ identityId: string; path: string }> = [];+ const transport: Fetcher = async (requested, init) => {+ const source = new URL(requested);+ if (source.origin !== EXPERIENTIAL_ORIGIN || source.search || source.hash) throw new Error('unexpected local adapter origin');+ const isInference = init.method?.toUpperCase() === 'POST';+ if (isInference && source.pathname !== '/v1/chat/completions') throw new Error('unexpected local inference path');+ if (isInference) {+ const auth = new Headers(init.headers).get('authorization');+ const customer = fixture.customers.find(row => auth === `Bearer ${row.customerKey}`);+ if (!customer) throw new Error('inference did not use a registered synthetic customer');+ dispatches.push({ identityId: customer.identityId, path: source.pathname });+ }+ // Ignore proxy environment variables for this loopback-only connection.+ // Preserve actual API status/body; strip only the response URL caused by+ // the deliberate harness origin rewrite (production rejects redirects).+ const response = await fetch(`${isInference ? gateway : control}${source.pathname}`, { ...init, redirect: 'error', proxy: null } as RequestInit);+ return new Response(response.body, { status: response.status, statusText: response.statusText, headers: response.headers });+ };+ const registry = new Map<string, ExperientialCustomerBinding>();+ const auths: AuthResult[] = [];+ for (const customer of fixture.customers) {+ const auth: AuthResult = { isValid: true, tier: 'subscribed', accountPlan: customer.accountPlan, userId: customer.userId, deviceId: 'synthetic-device' };+ const actorId = await hostedChatActorId(auth);+ if (registry.has(actorId)) throw new Error('synthetic customers must have separate actors');+ registry.set(actorId, { actorId, orgId: fixture.orgId, provisioningIdentityId: fixture.provisioningIdentityId,+ customerIdentityId: customer.identityId, accountPlan: customer.accountPlan, expectedPlan: customer.expectedPlan, customerKey: customer.customerKey });+ auths.push(auth);+ }+ expect(fixture.customers[0].identityId === fixture.customers[1].identityId).toBe(false);+ expect(fixture.customers[0].customerKey === fixture.customers[1].customerKey).toBe(false);+ for (let index = 0; index < auths.length; index++) {+ const connection = await prepareExperientialProbeConnection({ mode: 'isolated-test', auth: auths[index],+ sourceModelId: fixture.model, managementKey: fixture.managementKey, transport,+ mappings: [{ sourceModelId: fixture.model, destinationModelId: fixture.model, canonicalModelUuid: fixture.canonicalModelUuid, protocol: 'chat' }],+ lookupBinding: async actorId => registry.get(actorId) ?? null,+ });+ const provider = new OpenAIProvider(connection.apiKey, connection.baseURL, connection.defaultHeaders, connection.maxRetries);+ (provider as any).client.fetch = connection.fetch;+ const body: RequestBody = { model: fixture.model, max_completion_tokens: 64, messages: [{ role: 'user', content: 'Synthetic fixture: reply OK.' }] };+ if (index === 0) {+ const response: any = await (await provider.createCompletion(body)).json();+ expect(response.choices?.[0]?.finish_reason).toBe('stop');+ expect(typeof response.choices?.[0]?.message?.content).toBe('string');+ expect(response.choices[0].message.content.length).toBeGreaterThan(0);+ expect(response.usage?.prompt_tokens).toBeGreaterThanOrEqual(0);+ } else {+ const text = await new Response(await provider.createStreamingCompletion(body)).text();+ expect(text.endsWith('data: [DONE]\n\n')).toBe(true);+ const events = text.split('\n\n').filter(line => line.startsWith('data: {')).map(line => JSON.parse(line.slice(6)));+ expect(events.some(event => event.choices?.[0]?.delta?.content)).toBe(true);+ expect(events.some(event => event.choices?.[0]?.finish_reason === 'stop')).toBe(true);+ expect(events.some(event => event.usage && Number.isInteger(event.usage.prompt_tokens))).toBe(true);+ }+ expect(connection.production_ready).toBe(false);+ }+ expect(dispatches).toEqual(fixture.customers.map(customer => ({ identityId: customer.identityId, path: '/v1/chat/completions' })));+ }, 30_000);+});diff --git a/packages/ai-gateway/src/test/experiential-adapter.unit.test.ts b/packages/ai-gateway/src/test/experiential-adapter.unit.test.tsnew file mode 100644index 000000000..b9342ab0d--- /dev/null+++ b/packages/ai-gateway/src/test/experiential-adapter.unit.test.ts@@ -0,0 +1,234 @@+// screenpipe — AI that knows everything you've seen, said, or heard+// https://screenpipe.com+import { describe, expect, it } from 'bun:test';+import { prepareExperientialProbeConnection, type ExperientialAdapterOptions } from '../../scripts/lib/experiential-adapter';+import { EXPERIENTIAL_ORIGIN } from '../../scripts/lib/experiential-migration';+import { hostedChatActorId } from '../services/cloudflare-ai-gateway';++const org = '00000000-0000-0000-0000-000000000001';+const identity = 'customer-alice';+const provisioner = 'entitlement-sync';+const canonical = '00000000-0000-0000-0000-000000000004';+const customerKey = `xpl_${'0'.repeat(40)}`;+const managementKey = `xpl_${'1'.repeat(40)}`;+const model = 'gpt-5.4-mini';+const expectedPlan = { plan_key: 'business', plan_version: 1, revision: 1 };+const auth = { isValid: true, tier: 'subscribed', accountPlan: 'business', userId: 'synthetic-user', deviceId: 'synthetic-device' } as const;+async function fixture(overrides: { capture?: boolean; policy?: Record<string, unknown>; entitled?: boolean | null; modelPolicy?: Record<string, unknown>; verdict?: string | null; who?: Record<string, unknown>; status?: number; catalog?: string[]; binding?: Record<string, any> } = {}) {+ const calls: Array<{ url: string; init: RequestInit }> = [];+ const actorId = await hostedChatActorId(auth);+ const options: ExperientialAdapterOptions = {+ mode: 'isolated-test', auth, sourceModelId: model, managementKey,+ mappings: [{ sourceModelId: model, destinationModelId: model, canonicalModelUuid: canonical, protocol: 'chat' }],+ lookupBinding: async () => ({ actorId, orgId: org, customerIdentityId: identity, provisioningIdentityId: provisioner, accountPlan: 'business', expectedPlan, customerKey, ...overrides.binding }),+ transport: async (url, init) => {+ calls.push({ url, init });+ const management = new Headers(init.headers).get('Authorization') === `Bearer ${managementKey}`;+ if (init.method === 'POST') return Response.json({ choices: [{ message: { content: 'OK' } }] }, { status: overrides.status ?? 200, headers: overrides.verdict === null ? {} : { 'x-gateway-zdr': overrides.verdict ?? 'true' } });+ if (url.endsWith('/api/whoami')) return Response.json(management ? { org_id: org } : { org_id: org, identity_id: identity, customer_plan_identity_id: identity, is_provisioning: false, ...overrides.who });+ if (url.endsWith('/v1/models')) return Response.json({ data: (overrides.catalog ?? [model]).map(id => ({ id, data_policy: overrides.modelPolicy ?? { zdr_on_request: true } })) });+ if (url.endsWith(`/identities/${identity}/customer-plan`) && !management) return Response.json({ org_id: org, identity_id: identity, ...expectedPlan, pending_plan_key: null, pending_plan_version: null, pending_effective_at: null });+ if (!management) throw new Error('customer key must not read management privacy settings');+ if (url.endsWith(`/api/models/${model}`)) return Response.json({ model: { id: canonical, slug: model } });+ if (url.endsWith('/telemetry-settings')) return Response.json({ capture_prompt_content: overrides.capture ?? false });+ if (url.endsWith('/provider-policy')) return Response.json({ zdr_entitled: overrides.entitled === undefined ? true : overrides.entitled, policy: { require_no_training: true, zdr_continuation_storage: false, ...overrides.policy } });+ throw new Error('unexpected request');+ },+ };+ return { calls, options };+}+const post = (modelId = model, headers?: Record<string, string>) => ({ method: 'POST', body: JSON.stringify({ model: modelId, max_completion_tokens: 64, messages: [{ role: 'user', content: 'Synthetic.' }] }), headers });+const url = `${EXPERIENTIAL_ORIGIN}/v1/chat/completions`;++describe('inactive customer-isolated adapter', () => {+ it.each([undefined, 'inactive'] as const)('does nothing in mode %s', async mode => {+ const { options, calls } = await fixture();+ await expect(prepareExperientialProbeConnection({ ...options, mode })).rejects.toMatchObject({ code: 'experiential_adapter_inactive' });+ expect(calls).toHaveLength(0);+ });+ it.each([+ { isValid: false }, { accountPlan: 'unknown' }, { userId: '' }, { service: true },+ ])('rejects unverified entitlement locally %j', async change => {+ const { options, calls } = await fixture();+ await expect(prepareExperientialProbeConnection({ ...options, auth: { ...auth, ...change } as any })).rejects.toMatchObject({ code: 'verified_customer_entitlement_required' });+ expect(calls).toHaveLength(0);+ });+ it('rejects source-plan escalation and absent registry entries before any HTTP', async () => {+ const { options, calls } = await fixture();+ await expect(prepareExperientialProbeConnection({ ...options, auth: { ...auth, accountPlan: 'basic' }, sourceModelId: 'claude-opus-5' })).rejects.toMatchObject({ code: 'model_not_allowed_for_source_plan' });+ await expect(prepareExperientialProbeConnection({ ...options, lookupBinding: async () => null })).rejects.toMatchObject({ code: 'customer_binding_missing' });+ expect(calls).toHaveLength(0);+ });+ it.each(['auto', 'glm-5.3-flash-reap50-iq3m', 'screenpipe-event-classifier'])('never migrates the separate route %s', async sourceModelId => {+ const { options, calls } = await fixture();+ await expect(prepareExperientialProbeConnection({ ...options, sourceModelId })).rejects.toMatchObject({ code: 'separate_route_not_migrated' });+ expect(calls).toHaveLength(0);+ });+ it.each(['missing', 'duplicate', 'substitution', 'wrong-protocol', 'invalid-uuid'])('rejects %s model mapping before a request', async failure => {+ const { options, calls } = await fixture();+ const mapping = { ...options.mappings[0] };+ if (failure === 'substitution') mapping.destinationModelId = 'gpt-5.4-nano';+ if (failure === 'wrong-protocol') mapping.protocol = 'messages';+ if (failure === 'invalid-uuid') mapping.canonicalModelUuid = model;+ options.mappings = failure === 'missing' ? [] : failure === 'duplicate' ? [mapping, mapping] : [mapping];+ await expect(prepareExperientialProbeConnection(options)).rejects.toBeDefined(); expect(calls).toHaveLength(0);+ });+ it.each([+ { actorId: 'another-actor' }, { accountPlan: 'business_max' }, { customerIdentityId: provisioner }, { customerKey: managementKey },+ { customerIdentityId: '00000000-0000-0000-0000-000000000002' }, { customerIdentityId: '../customer-alice' }, { customerIdentityId: 'Customer-Alice' },+ ])('rejects stale or shared binding %j', async binding => {+ const { options, calls } = await fixture({ binding });+ await expect(prepareExperientialProbeConnection(options)).rejects.toBeDefined(); expect(calls).toHaveLength(0);+ });+ it('uses entitled per-request ZDR even when account capture is enabled', async () => {+ const { options, calls } = await fixture({ capture: true });+ const c = await prepareExperientialProbeConnection(options);+ await c.fetch(url, { ...post(), body: JSON.stringify({ ...JSON.parse(post().body), provider: { zdr: false, order: ['unsafe'] } }) });+ expect(JSON.parse(calls.find(call => call.init.method === 'POST')!.init.body as string).provider).toEqual({ zdr: true });+ });+ it.each([+ { entitled: false }, { entitled: null }, { policy: { require_no_training: false } },+ { policy: { require_no_training: undefined } }, { policy: { zdr_continuation_storage: true } },+ { policy: { zdr_continuation_storage: undefined } },+ ])('fails closed on missing or unsafe privacy policy %j', async change => {+ const { options, calls } = await fixture(change);+ await expect(prepareExperientialProbeConnection(options)).rejects.toMatchObject({ code: 'privacy_preflight_failed' });+ expect(calls.every(call => call.init.method === 'GET')).toBe(true);+ });+ it.each([{}, { zdr_on_request: false }, { zdr: 'true' }])('requires verified model ZDR capability %j', async modelPolicy => {+ const { options, calls } = await fixture({ modelPolicy });+ await expect(prepareExperientialProbeConnection(options)).rejects.toMatchObject({ code: 'model_has_no_verified_zdr_route' });+ expect(calls.every(call => call.init.method === 'GET')).toBe(true);+ });+ it.each([null, 'false', 'True', '1'])('rejects unconfirmed ZDR verdict %s before reading content and never replays', async verdict => {+ const { options, calls } = await fixture({ verdict });+ const original = options.transport;+ let cancelled = false;+ let reads = 0;+ options.transport = async (url, init) => {+ const response = await original(url, init);+ if (init.method !== 'POST') return response;+ return new Response(new ReadableStream({ pull() { reads++; }, cancel() { cancelled = true; } }, { highWaterMark: 0 }), { headers: response.headers });+ };+ const c = await prepareExperientialProbeConnection(options);+ await expect(c.fetch(url, post())).rejects.toMatchObject({ code: 'zdr_response_not_confirmed_no_retry' });+ expect(cancelled).toBe(true); expect(reads).toBe(0);+ await expect(c.fetch(url, post())).rejects.toMatchObject({ code: 'probe_replay_forbidden' });+ expect(calls.filter(call => call.init.method === 'POST')).toHaveLength(1);+ });+ it.each([+ { identity_id: null }, { identity_id: provisioner }, { customer_plan_identity_id: null },+ { customer_plan_identity_id: provisioner }, { is_provisioning: true }, { is_provisioning: undefined }, { org_id: provisioner },+ ])('fails closed when key identity proof differs %j', async who => {+ const { options, calls } = await fixture({ who });+ await expect(prepareExperientialProbeConnection(options)).rejects.toMatchObject({ code: 'identity_bound_customer_key_required' });+ expect(calls.every(call => call.init.method === 'GET')).toBe(true);+ });+ it.each([+ { plan_key: 'business-max' }, { plan_version: 2 }, { revision: 2 }, { plan_key: null, plan_version: null },+ { org_id: canonical }, { identity_id: provisioner }, { pending_plan_key: 'basic' }, { pending_plan_version: 2 }, { pending_effective_at: '2026-10-01T00:00:00Z' },+ ])('rejects changed, absent or pending actual assignment %j', async change => {+ const { options, calls } = await fixture(); const original = options.transport;+ options.transport = async (url, init) => {+ const response = await original(url, init);+ return url.endsWith('/customer-plan') ? Response.json({ ...await response.json() as any, ...change }) : response;+ };+ await expect(prepareExperientialProbeConnection(options)).rejects.toMatchObject({ code: 'customer_plan_assignment_stale_or_pending' });+ expect(calls.every(call => call.init.method === 'GET')).toBe(true);+ });+ it('requires explicit trusted expected plan, never null for unknown', async () => {+ const { options, calls } = await fixture({ binding: { expectedPlan: undefined } });+ await expect(prepareExperientialProbeConnection(options)).rejects.toMatchObject({ code: 'expected_customer_plan_required' });+ expect(calls.every(call => call.init.method === 'GET')).toBe(true);+ });+ it('checks canonical UUID against management catalog detail, not a label', async () => {+ const { options, calls } = await fixture();+ options.mappings = [{ ...options.mappings[0], canonicalModelUuid: '00000000-0000-0000-0000-000000000005' }];+ await expect(prepareExperientialProbeConnection(options)).rejects.toMatchObject({ code: 'canonical_model_mapping_mismatch' });+ expect(calls.every(call => call.init.method === 'GET')).toBe(true);+ });+ it('keeps unavailable exact models unavailable', async () => {+ const { options, calls } = await fixture({ catalog: ['gpt-5.4-nano'] });+ await expect(prepareExperientialProbeConnection(options)).rejects.toMatchObject({ code: 'model_not_in_authenticated_catalog' });+ expect(calls.every(call => call.init.method === 'GET')).toBe(true);+ });+ it('preflights with management auth but dispatches exactly once with only customer auth', async () => {+ const { options, calls } = await fixture();+ const c = await prepareExperientialProbeConnection(options);+ expect(c.production_ready).toBe(false); expect(c.cutover_authorized).toBe(false); expect(c.maxRetries).toBe(0);+ await c.fetch(url, post(model, { authorization: 'Bearer caller-org-key', 'x-api-key': 'provider-secret', 'cf-aig-metadata': '{"plan":"business_ultra"}', 'x-no-capture': 'true' }));+ const posts = calls.filter(call => call.init.method === 'POST'); expect(posts).toHaveLength(1);+ expect(Object.fromEntries(new Headers(posts[0].init.headers))).toEqual({ authorization: `Bearer ${customerKey}`, 'content-type': 'application/json', accept: 'application/json' });+ expect(posts[0].init.redirect).toBe('error');+ expect(JSON.parse(posts[0].init.body as string).provider).toEqual({ zdr: true });+ await expect(c.fetch(url, post())).rejects.toMatchObject({ code: 'probe_replay_forbidden' });+ expect(calls.filter(call => call.init.method === 'POST')).toHaveLength(1);+ });+ it.each([undefined, 0, 65, -1, 1.5, '64'])('refuses unbounded synthetic output %s', async max_completion_tokens => {+ const { options, calls } = await fixture(); const c = await prepareExperientialProbeConnection(options);+ await expect(c.fetch(url, { ...post(), body: JSON.stringify({ model, max_completion_tokens, messages: [] }) })).rejects.toMatchObject({ code: 'bounded_synthetic_output_required' });+ expect(calls.filter(call => call.init.method === 'POST')).toHaveLength(0);+ });+ it('allows explicitly selected 128-token isolated probes but nothing higher', async () => {+ const { options, calls } = await fixture();+ const c = await prepareExperientialProbeConnection({ ...options, maxOutputTokens: 128 });+ await expect(c.fetch(url, { ...post(), body: JSON.stringify({ model, max_completion_tokens: 129, messages: [] }) })).rejects.toMatchObject({ code: 'bounded_synthetic_output_required' });+ await c.fetch(url, { ...post(), body: JSON.stringify({ model, max_completion_tokens: 128, messages: [] }) });+ expect(calls.filter(call => call.init.method === 'POST')).toHaveLength(1);+ const other = await fixture();+ await expect(prepareExperientialProbeConnection({ ...other.options, maxOutputTokens: 256 as any })).rejects.toMatchObject({ code: 'invalid_probe_output_bound' });+ expect(other.calls).toHaveLength(0);+ });+ it('rejects path, model and method tampering before dispatch', async () => {+ const { options, calls } = await fixture(); const c = await prepareExperientialProbeConnection(options);+ await expect(c.fetch('https://other.invalid/v1/chat/completions', post())).rejects.toMatchObject({ code: 'unexpected_probe_request' });+ await expect(c.fetch(url, post('gpt-5.4-nano'))).rejects.toMatchObject({ code: 'exact_model_mapping_required' });+ await expect(c.fetch(url, { ...post(), method: 'GET' })).rejects.toMatchObject({ code: 'unexpected_probe_request' });+ expect(calls.filter(call => call.init.method === 'POST')).toHaveLength(0);+ });+ it('requires all Astra stateless Low/Standard fields at the transport boundary', async () => {+ const { options, calls } = await fixture();+ options.sourceModelId = 'gpt-6-astra';+ options.mappings = [{ sourceModelId: 'gpt-6-astra', destinationModelId: 'gpt-6-astra', canonicalModelUuid: canonical, protocol: 'responses' }];+ const original = options.transport;+ options.transport = async (url, init) => {+ if (url.endsWith('/v1/models')) return Response.json({ data: [{ id: 'gpt-6-astra', data_policy: { zdr_on_request: true } }] });+ if (url.endsWith('/api/models/gpt-6-astra')) return Response.json({ model: { id: canonical, slug: 'gpt-6-astra' } });+ return original(url, init);+ };+ const c = await prepareExperientialProbeConnection(options);+ const valid = { model: 'gpt-6-astra', max_output_tokens: 64, input: [], reasoning: { effort: 'low' }, service_tier: 'default', store: false };+ for (const change of [{ store: true }, { previous_response_id: 'resp_previous' }, { reasoning: { effort: 'high' } }, { service_tier: 'priority' }]) {+ await expect(c.fetch(`${EXPERIENTIAL_ORIGIN}/v1/responses`, { method: 'POST', body: JSON.stringify({ ...valid, ...change }) })).rejects.toMatchObject({ code: 'astra_stateless_low_standard_required' });+ }+ expect(calls.filter(call => call.init.method === 'POST')).toHaveLength(0);+ });+ it('redacts a disconnect and refuses replay after an uncertain dispatch', async () => {+ const { options, calls } = await fixture(); const original = options.transport;+ options.transport = async (url, init) => {+ if (init.method === 'POST') { calls.push({ url, init }); throw new Error(`upstream disconnected ${customerKey}`); }+ return original(url, init);+ };+ const c = await prepareExperientialProbeConnection(options);+ await expect(c.fetch(url, post())).rejects.toMatchObject({ code: 'transport_failed_no_retry', message: 'transport_failed_no_retry' });+ await expect(c.fetch(url, post())).rejects.toMatchObject({ code: 'probe_replay_forbidden' });+ expect(calls.filter(call => call.init.method === 'POST')).toHaveLength(1);+ });+ it('rejects an off-origin success response even from an injected transport', async () => {+ const { options } = await fixture(); const original = options.transport;+ options.transport = async (url, init) => {+ const response = await original(url, init);+ if (init.method === 'POST') Object.defineProperty(response, 'url', { value: 'https://other.invalid/completion' });+ return response;+ };+ const c = await prepareExperientialProbeConnection(options);+ await expect(c.fetch(url, post())).rejects.toMatchObject({ code: 'unexpected_response_origin' });+ await expect(c.fetch(url, post())).rejects.toMatchObject({ code: 'probe_replay_forbidden' });+ });+ it.each([401, 403, 429, 500])('never retries HTTP %s or exposes upstream content', async status => {+ const { options, calls } = await fixture({ status }); const c = await prepareExperientialProbeConnection(options);+ await expect(c.fetch(url, post())).rejects.toMatchObject({ code: 'request_rejected_no_retry', status });+ await expect(c.fetch(url, post())).rejects.toMatchObject({ code: 'probe_replay_forbidden' });+ expect(calls.filter(call => call.init.method === 'POST')).toHaveLength(1);+ });+});diff --git a/packages/ai-gateway/src/test/experiential-migration.unit.test.ts b/packages/ai-gateway/src/test/experiential-migration.unit.test.tsnew file mode 100644index 000000000..39beb9061--- /dev/null+++ b/packages/ai-gateway/src/test/experiential-migration.unit.test.ts@@ -0,0 +1,231 @@+// screenpipe — AI that knows everything you've seen, said, or heard+// https://screenpipe.com+import { describe, expect, it } from 'bun:test';+import { EXPERIENTIAL_ORIGIN, inspectExperientialAccount, runSyntheticProbe, syntheticRequest, type Fetcher, type ProbeProtocol } from '../../scripts/lib/experiential-migration';+const key = `xpl_${'0'.repeat(40)}`;+const org = '00000000-0000-0000-0000-000000000001';+const json = (body: any, status = 200) => Response.json(body, { status, headers: { 'x-gateway-zdr': 'true' } });+const completion = { choices: [{ finish_reason: 'stop', message: { content: 'OK' } }], usage: { prompt_tokens: 7, completion_tokens: 1, cost: 0.00001 } };+const responseMessage = { type: 'message', role: 'assistant', status: 'completed', content: [{ type: 'output_text', text: 'OK' }] };+function fixture(overrides: { capture?: unknown; training?: unknown; entitled?: unknown; policy?: any; catalog?: any; reply?: () => Response | Promise<Response> } = {}) {+ const calls: Array<{ url: string; init: RequestInit }> = [];+ const transport: Fetcher = async (url, init) => {+ calls.push({ url, init });+ if (init.method === 'POST') return overrides.reply?.() ?? json(completion);+ if (url.endsWith('/api/whoami')) return json({ org_id: org });+ if (url.endsWith('/v1/models')) return json(overrides.catalog ?? { data: [{ id: 'gpt-5.4-nano', data_policy: { zdr_on_request: true } }] });+ if (url.endsWith('/telemetry-settings')) return json({ capture_prompt_content: overrides.capture ?? false });+ if (url.endsWith('/provider-policy')) return json({ zdr_entitled: Object.hasOwn(overrides, 'entitled') ? overrides.entitled : true,+ policy: overrides.policy ?? { require_no_training: overrides.training ?? true, require_zdr: false, zdr_continuation_storage: false } });+ throw new Error('Unexpected test URL');+ };+ return { transport, calls };+}+const probe = (transport: Fetcher, protocol: ProbeProtocol = 'chat') => runSyntheticProbe({ key, model: 'gpt-5.4-nano', protocol, allowSpend: true }, transport);+describe('Experiential migration preparation', () => {+ it.each(['chat', 'stream', 'tools', 'json-schema', 'responses', 'messages'] as ProbeProtocol[])('requires ZDR on every %s probe', protocol => {+ const model = protocol === 'messages' ? 'claude-sonnet-5' : protocol === 'responses' ? 'gpt-6-astra' : 'gpt-5.4-nano';+ const { body } = syntheticRequest(model, protocol);+ expect(body.provider).toEqual({ zdr: true });+ expect(body.previous_response_id).toBeUndefined();+ });+ it('rejects success without ZDR confirmation before consuming content', async () => {+ let cancelled = false;+ const body = new ReadableStream({ cancel() { cancelled = true; } });+ const { transport, calls } = fixture({ reply: () => new Response(body) });+ await expect(probe(transport)).rejects.toMatchObject({ code: 'zdr_response_not_confirmed_no_retry' });+ expect(cancelled).toBe(true);+ expect(calls.filter(c => c.init.method === 'POST')).toHaveLength(1);+ });+ it.each(['false', 'TRUE', 'true, false', ''])('rejects ambiguous ZDR response %j on JSON and SSE', async verdict => {+ for (const protocol of ['chat', 'stream'] as ProbeProtocol[]) {+ let cancelled = false;+ const body = new ReadableStream({ cancel() { cancelled = true; } });+ const { transport, calls } = fixture({ reply: () => new Response(body, { headers: { 'x-gateway-zdr': verdict } }) });+ await expect(probe(transport, protocol)).rejects.toMatchObject({ code: 'zdr_response_not_confirmed_no_retry' });+ expect(cancelled).toBe(true);+ expect(calls.filter(c => c.init.method === 'POST')).toHaveLength(1);+ }+ });+ it('allows entitled per-request ZDR with org capture on and org ZDR off', async () => {+ const { transport, calls } = fixture({ capture: true });+ expect(await probe(transport)).toMatchObject({ zdr_requested: true, zdr_response_confirmed: true, production_ready: false });+ const report = await inspectExperientialAccount(key, ['gpt-5.4-nano'], transport);+ expect(report.checks).toMatchObject({ prompt_capture_disabled: false, upstream_zero_retention: false, zdr_request_entitled: true });+ expect(JSON.parse(calls.find(c => c.init.method === 'POST')!.init.body as string).provider).toEqual({ zdr: true });+ });+ it('preserves the documented grandfathered org ZDR entitlement', async () => {+ const { transport } = fixture({ entitled: false, policy: { require_zdr: true, require_no_training: true, zdr_continuation_storage: false } });+ expect((await probe(transport)).zdr_response_confirmed).toBe(true);+ });+ it.each([undefined, {}, { zdr: false, zdr_on_request: false }, { zdr: 'true', zdr_on_request: 'true' }])('refuses missing or noncompliant model privacy metadata %j before dispatch', async data_policy => {+ const { transport, calls } = fixture({ catalog: { data: [{ id: 'gpt-5.4-nano', data_policy }] } });+ await expect(probe(transport)).rejects.toMatchObject({ code: 'model_has_no_verified_zdr_route' });+ expect(calls.every(c => c.init.method === 'GET')).toBe(true);+ });+ it('accepts a model with ZDR on every route', async () => {+ const { transport } = fixture({ catalog: { data: [{ id: 'gpt-5.4-nano', data_policy: { zdr: true } }] } });+ expect((await probe(transport)).zdr_requested).toBe(true);+ });+ it.each([+ [400, 'unsupported_parameter'], [403, 'needs_subscription'], [403, 'model_not_granted'],+ ])('never strips the ZDR requirement after %s %s', async (status, code) => {+ const { transport, calls } = fixture({ reply: () => json({ error: { code } }, status as number) });+ await expect(probe(transport)).rejects.toMatchObject({ code: 'request_rejected_no_retry', status });+ const posts = calls.filter(c => c.init.method === 'POST');+ expect(posts).toHaveLength(1);+ expect(JSON.parse(posts[0].init.body as string).provider).toEqual({ zdr: true });+ });+ it('only reads the fixed API origin; catalog matches never imply production readiness', async () => {+ const { transport, calls } = fixture();+ const report = await inspectExperientialAccount(key, ['auto', 'gpt-5.4-nano', 'gpt-5.4-nano'], transport);+ expect(report.models).toEqual([{ id: 'auto', exact_catalog_match: false, zdr_route_available: false }, { id: 'gpt-5.4-nano', exact_catalog_match: true, zdr_route_available: true }]);+ expect(report.production_ready).toBe(false); expect(report.cutover_authorized).toBe(false);+ expect(report.unverified).toContain('shared_total_and_frontier_caps'); expect(calls).toHaveLength(4);+ for (const { url, init } of calls) {+ expect(new URL(url).origin).toBe(EXPERIENTIAL_ORIGIN); expect(init.method).toBe('GET');+ expect(init.body).toBeUndefined(); expect(init.redirect).toBe('error');+ expect(init.headers).toEqual({ Authorization: `Bearer ${key}`, Accept: 'application/json' });+ }+ expect(JSON.stringify(report)).not.toContain(key);+ });+ it('requires spending approval before network activity', async () => {+ const { transport, calls } = fixture();+ await expect(runSyntheticProbe({ key, model: 'gpt-5.4-nano', protocol: 'chat', allowSpend: false }, transport)).rejects.toMatchObject({ code: 'explicit_spend_authorization_required' });+ expect(calls).toHaveLength(0);+ });+ it.each(['', 'sk-provider-secret', 'xpl_bad\r\nAuthorization: secret'])('rejects malformed credential %s locally', async invalid => {+ const { transport, calls } = fixture();+ await expect(inspectExperientialAccount(invalid, [], transport)).rejects.toMatchObject({ code: 'invalid_credential_format' });+ expect(calls).toHaveLength(0);+ });+ it('refuses untrusted organization paths', async () => {+ let count = 0;+ await expect(inspectExperientialAccount(key, [], async () => { count++; return json({ org_id: '../../keys' }); })).rejects.toMatchObject({ code: 'invalid_organization_response' });+ expect(count).toBe(1);+ });+ it.each([{ training: false }, { training: 'true' }, { entitled: false }, { entitled: 'true' }, { entitled: undefined }, { policy: {} }, { policy: { require_no_training: true, zdr_continuation_storage: true } }])('fails closed for privacy state %j', async options => {+ const { transport, calls } = fixture(options);+ await expect(probe(transport)).rejects.toMatchObject({ code: 'privacy_preflight_failed' });+ expect(calls.every(c => c.init.method === 'GET')).toBe(true);+ });+ it('does not substitute an unavailable model', async () => {+ const { transport, calls } = fixture({ catalog: { data: [{ id: 'vendor/gpt-5.4-nano' }] } });+ await expect(probe(transport)).rejects.toMatchObject({ code: 'model_not_in_authenticated_catalog' });+ expect(calls).toHaveLength(4);+ });+ it.each(['auto', 'glm-5.3-flash-reap50-iq3m', 'screenpipe-event-classifier', 'argus-trace-1', 'gemma4-e4b', 'gpt-unreviewed', 'claude-unreviewed', 'GPT-5.4-nano'])('excludes %s', async model => {+ const { transport, calls } = fixture();+ await expect(runSyntheticProbe({ key, model, protocol: 'chat', allowSpend: true }, transport)).rejects.toMatchObject({ code: 'unsupported_probe_model' });+ expect(calls).toHaveLength(0);+ });+ it('makes one bounded synthetic POST and does not claim settlement', async () => {+ const { transport, calls } = fixture(); const result = await probe(transport);+ const posts = calls.filter(c => c.init.method === 'POST'); expect(posts).toHaveLength(1);+ expect(posts[0].url).toBe(`${EXPERIENTIAL_ORIGIN}/v1/chat/completions`);+ expect(JSON.parse(posts[0].init.body as string)).toEqual({ model: 'gpt-5.4-nano', provider: { zdr: true }, messages: [{ role: 'user', content: 'Reply with OK.' }], max_completion_tokens: 64 });+ expect(result).toMatchObject({ input_tokens: 7, output_tokens: 1, inline_cost_usd: 0.00001, settlement_verified: false, native_policy_verified: false, production_ready: false });+ expect(JSON.stringify(result)).not.toContain('Reply with');+ });+ it.each([401, 402, 403, 409, 429, 500, 502])('does not retry or leak bodies after HTTP %s', async status => {+ const { transport, calls } = fixture({ reply: () => json({ error: { message: `secret ${key} customer prompt` } }, status) });+ await expect(probe(transport)).rejects.toMatchObject({ code: 'request_rejected_no_retry', status, message: `request_rejected_no_retry (HTTP ${status})` });+ expect(calls.filter(c => c.init.method === 'POST')).toHaveLength(1);+ });+ it('redacts disconnects without retrying an uncertain request', async () => {+ const { transport, calls } = fixture({ reply: () => { throw new Error(`disconnected ${key}`); } });+ await expect(probe(transport)).rejects.toMatchObject({ message: 'transport_failed_no_retry' });+ expect(calls.filter(c => c.init.method === 'POST')).toHaveLength(1);+ });+ it('validates fragmented SSE content, finish, usage and DONE', async () => {+ const text = [JSON.stringify({ choices: [{ delta: { content: 'OK' } }] }), JSON.stringify({ choices: [{ finish_reason: 'stop', delta: {} }] }), JSON.stringify({ choices: [], usage: completion.usage }), '[DONE]'].map(x => `data: ${x}\r\n\r\n`).join('');+ const stream = new ReadableStream({ start(controller) { const bytes = new TextEncoder().encode(text); for (let i = 0; i < bytes.length; i += 7) controller.enqueue(bytes.slice(i, i + 7)); controller.close(); } });+ const { transport } = fixture({ reply: () => new Response(stream, { headers: { 'content-type': 'text/event-stream', 'x-gateway-zdr': 'true' } }) });+ expect(await probe(transport, 'stream')).toMatchObject({ output_tokens: 1 });+ });+ it.each(['data: {"choices":[{"delta":{"content":"partial"}}]}\n\n', 'data: {"error":{"message":"private"}}\n\n', 'data: [DONE]\n\n'])('refuses incomplete or failed SSE without replay', async text => {+ const { transport, calls } = fixture({ reply: () => new Response(text, { headers: { 'content-type': 'text/event-stream', 'x-gateway-zdr': 'true' } }) });+ await expect(probe(transport, 'stream')).rejects.toBeDefined();+ expect(calls.filter(c => c.init.method === 'POST')).toHaveLength(1);+ });+ it('keeps unknown usage and cost unknown', async () => {+ const { transport } = fixture({ reply: () => json({ choices: completion.choices }) });+ await expect(probe(transport)).rejects.toMatchObject({ code: 'usage_missing_no_retry' });+ const other = fixture({ reply: () => json({ choices: completion.choices, usage: { prompt_tokens: 7, completion_tokens: 1 } }) });+ expect((await probe(other.transport)).inline_cost_usd).toBeNull();+ });+ it('validates tool arguments and structured output beyond HTTP 200', async () => {+ const { transport } = fixture();+ await expect(probe(transport, 'tools')).rejects.toMatchObject({ code: 'tool_call_missing' });+ await expect(probe(transport, 'json-schema')).rejects.toMatchObject({ code: 'structured_output_invalid' });+ const toolReply = fixture({ reply: () => json({ choices: [{ finish_reason: 'tool_calls', message: { tool_calls: [{ id: 'call_1', type: 'function', function: { name: 'confirm', arguments: '{"ok":true}' } }] } }], usage: completion.usage }) });+ expect((await probe(toolReply.transport, 'tools')).output_tokens).toBe(1);+ });+ it.each([+ ['messages', 'claude-sonnet-5', { stop_reason: 'max_tokens', content: [{ type: 'text', text: 'partial' }] }],+ ['messages', 'claude-sonnet-5', { stop_reason: 'end_turn', content: [{ type: 'thinking', thinking: 'not an answer' }] }],+ ['responses', 'gpt-6-astra', { status: 'completed', output: [{ type: 'reasoning', summary: [] }] }],+ ['responses', 'gpt-6-astra', { status: 'incomplete', output: [{ type: 'message', content: [{ type: 'output_text', text: 'partial' }] }] }],+ ] as const)('rejects unsuccessful native completion %s', async (protocol, model, reply) => {+ const { transport, calls } = fixture({ catalog: { data: [{ id: model, data_policy: { zdr_on_request: true } }] },+ reply: () => json({ ...reply, usage: { input_tokens: 7, output_tokens: 1 } }) });+ await expect(runSyntheticProbe({ key, model, protocol, allowSpend: true }, transport)).rejects.toMatchObject({ code: `${protocol}_not_completed` });+ expect(calls.filter(c => c.init.method === 'POST')).toHaveLength(1);+ });+ it('pins stateless Astra and keeps native Messages separate', () => {+ const astra = syntheticRequest('gpt-6-astra', 'responses'); expect(astra.path).toBe('/v1/responses');+ expect(astra.body).toMatchObject({ store: false, reasoning: { effort: 'low' }, service_tier: 'default', max_output_tokens: 64 });+ expect(astra.body.previous_response_id).toBeUndefined();+ const anthropic = syntheticRequest('claude-sonnet-5', 'messages'); expect(anthropic.path).toBe('/v1/messages'); expect(anthropic.body.max_tokens).toBe(64);+ expect(() => syntheticRequest('claude-sonnet-5', 'responses')).toThrow(); expect(() => syntheticRequest('gpt-6-astra', 'messages')).toThrow();+ expect(() => syntheticRequest('gpt-6-astra', 'chat')).toThrow('astra_requires_responses');+ });+ it.each(['max_tokens', 'tool_use', 'refusal', 'pause_turn', 'unknown', null])('rejects unfinished Messages stop reason %j without replay', async stop_reason => {+ const model = 'claude-sonnet-5';+ const { transport, calls } = fixture({ catalog: { data: [{ id: model, data_policy: { zdr: true } }] },+ reply: () => json({ content: [{ type: 'text', text: 'partial' }], stop_reason, usage: completion.usage }) });+ await expect(runSyntheticProbe({ key, model, protocol: 'messages', allowSpend: true }, transport)).rejects.toMatchObject({ code: 'messages_not_completed' });+ expect(calls.filter(c => c.init.method === 'POST')).toHaveLength(1);+ });+ it.each([+ { status: 'completed', output: [{ type: 'reasoning', summary: [] }] },+ { status: 'completed', output: [{ ...responseMessage, content: [{ type: 'refusal', refusal: 'no' }] }] },+ { status: 'completed', output: [{ ...responseMessage, content: [{ type: 'output_text', text: ' ' }] }] },+ { status: 'completed', output: [{ ...responseMessage, status: 'incomplete' }] },+ { status: 'completed', output: [{ ...responseMessage, role: 'user' }] },+ { status: 'incomplete', output: [responseMessage] },+ ])('rejects Responses without a completed assistant answer %j', async reply => {+ const model = 'gpt-6-astra';+ const { transport, calls } = fixture({ catalog: { data: [{ id: model, data_policy: { zdr: true } }] },+ reply: () => json({ ...reply, usage: completion.usage }) });+ await expect(runSyntheticProbe({ key, model, protocol: 'responses', allowSpend: true }, transport)).rejects.toMatchObject({ code: 'responses_not_completed' });+ expect(calls.filter(c => c.init.method === 'POST')).toHaveLength(1);+ });+ it.each(['content_filter', 'length', 'tool_calls', 'unknown', null])('rejects unsuccessful Chat finish reason %j for text and schema probes', async finish_reason => {+ for (const protocol of ['chat', 'json-schema'] as ProbeProtocol[]) {+ const { transport, calls } = fixture({ reply: () => json({ choices: [{ finish_reason, message: { content: protocol === 'chat' ? 'partial' : '{"ok":true}' } }], usage: completion.usage }) });+ await expect(probe(transport, protocol)).rejects.toMatchObject({ code: 'chat_not_completed' });+ expect(calls.filter(c => c.init.method === 'POST')).toHaveLength(1);+ }+ });+ it.each([{ content: [] }, { content: [{ type: 'thinking', thinking: 'hidden' }] }, { content: [{ type: 'text', text: ' ' }] }])('rejects Messages without a text answer %j', async ({ content }) => {+ const model = 'claude-sonnet-5';+ const { transport } = fixture({ catalog: { data: [{ id: model, data_policy: { zdr: true } }] },+ reply: () => json({ content, stop_reason: 'end_turn', usage: completion.usage }) });+ await expect(runSyntheticProbe({ key, model, protocol: 'messages', allowSpend: true }, transport)).rejects.toMatchObject({ code: 'messages_not_completed' });+ });+ it('accepts complete Messages and Responses with preceding reasoning', async () => {+ for (const protocol of ['messages', 'responses'] as ProbeProtocol[]) {+ const model = protocol === 'messages' ? 'claude-sonnet-5' : 'gpt-6-astra';+ const reply = protocol === 'messages'+ ? { content: [{ type: 'thinking', thinking: 'hidden' }, { type: 'text', text: 'OK' }], stop_reason: 'end_turn' }+ : { status: 'completed', output: [{ type: 'reasoning', summary: [] }, responseMessage] };+ const { transport, calls } = fixture({ catalog: { data: [{ id: model, data_policy: { zdr: true } }] }, reply: () => json({ ...reply, usage: completion.usage }) });+ const result = await runSyntheticProbe({ key, model, protocol, allowSpend: true }, transport);+ expect(result).toMatchObject({ output_tokens: 1, zdr_requested: true, zdr_response_confirmed: true, production_ready: false });+ const posts = calls.filter(c => c.init.method === 'POST'); expect(posts).toHaveLength(1);+ expect(JSON.parse(posts[0].init.body as string).provider).toEqual({ zdr: true });+ expect(JSON.stringify(result)).not.toContain('hidden');+ }+ });+});diff --git a/packages/ai-gateway/src/test/experiential-sdk-parity.unit.test.ts b/packages/ai-gateway/src/test/experiential-sdk-parity.unit.test.tsnew file mode 100644index 000000000..38292c1da--- /dev/null+++ b/packages/ai-gateway/src/test/experiential-sdk-parity.unit.test.ts@@ -0,0 +1,244 @@+// screenpipe — AI that knows everything you've seen, said, or heard+// https://screenpipe.com++// Offline contract tests: real installed SDK serializers and stream parsers,+// synthetic fetch only. These prove neither deployed route nor provider parity.+import { describe, expect, it } from 'bun:test';+import { OpenAIProvider } from '../providers/openai';+import { AnthropicProvider } from '../providers/anthropic';+import { AstraProvider } from '../providers/astra';+import { ScreenpipeGlmProvider, SCREENPIPE_GLM_MODEL } from '../providers/screenpipe-glm';+import { getHostedChatGatewayConnection, hostedChatActorId, type HostedChatGatewayContext } from '../services/cloudflare-ai-gateway';+import { prepareExperientialProbeConnection } from '../../scripts/lib/experiential-adapter';+import { EXPERIENTIAL_ORIGIN } from '../../scripts/lib/experiential-migration';+import type { Env, RequestBody } from '../types';++const key = `xpl_${'0'.repeat(40)}`;+const context: HostedChatGatewayContext = { user_id: 'synthetic-actor', plan: 'business', lane: 'explicit', workload: 'interactive', trial: false };+const env = { CLOUDFLARE_AI_GATEWAY_ID: 'parity', CLOUDFLARE_AI_GATEWAY_BASE_URL: 'https://gateway.ai.cloudflare.com/v1/synthetic/parity', TINFOIL_GLM_API_KEY: 'synthetic-glm-key' } as Env;+const schema = { type: 'object', properties: { ok: { type: 'boolean' } }, required: ['ok'], additionalProperties: false };+const tools = [{ type: 'function', function: { name: 'confirm', parameters: schema } }];+const usage = { prompt_tokens: 8, completion_tokens: 2, total_tokens: 10, prompt_tokens_details: { cached_tokens: 3 } };+const image = { type: 'image', mimeType: 'image/png', data: 'AAAA' };+function body(model: string): RequestBody {+ return { model, max_tokens: 64, messages: [{ role: 'system', content: 'Synthetic fixture.' }, { role: 'user', content: [{ type: 'text', text: 'Confirm.' }, image] }],+ tools, tool_choice: { type: 'function', function: { name: 'confirm' } }, response_format: { type: 'json_schema', json_schema: { name: 'confirmation', strict: true, schema }, schema } } as RequestBody;+}+function completion() {+ return { id: 'chat_fixture', object: 'chat.completion', model: 'gpt-5.4-mini', created: 1,+ choices: [{ index: 0, message: { role: 'assistant', content: null, tool_calls: [{ id: 'call_1', type: 'function', function: { name: 'confirm', arguments: '{"ok":true}' } }] }, finish_reason: 'tool_calls' }], usage };+}+function sse(events: any[], native = false) {+ const text = events.map(event => `${native ? `event: ${event.type}\n` : ''}data: ${JSON.stringify(event)}\n\n`).join('') + (native ? '' : 'data: [DONE]\n\n');+ const bytes = new TextEncoder().encode(text);+ return new Response(new ReadableStream({ start(controller) {+ for (let index = 0; index < bytes.length; index += 11) controller.enqueue(bytes.slice(index, index + 11));+ controller.close();+ } }), { headers: { 'content-type': 'text/event-stream' } });+}+async function intercept(provider: any, response: () => Response, side?: string, model = 'gpt-5.4-mini', verdict: string | null = 'true') {+ const calls: Array<{ url: string; headers: Headers; body: any }> = [];+ // Replace only SDK fetch, never SDK create() or stream parsing. Destination+ // tests dispatch through the inactive adapter under explicit isolated mode.+ const transport = async (url: any, init: any) => {+ if (init.method === 'GET') {+ const management = new Headers(init.headers).get('authorization') === `Bearer xpl_${'1'.repeat(40)}`;+ if (String(url).endsWith('/api/whoami')) return Response.json({ org_id: '00000000-0000-0000-0000-000000000001',+ ...(management ? {} : { identity_id: 'customer-alice', customer_plan_identity_id: 'customer-alice', is_provisioning: false }) });+ if (String(url).endsWith('/v1/models')) return Response.json({ data: [{ id: model, data_policy: { zdr_on_request: true } }] });+ if (String(url).endsWith('/identities/customer-alice/customer-plan') && !management) return Response.json({ org_id: '00000000-0000-0000-0000-000000000001', identity_id: 'customer-alice', plan_key: 'business', plan_version: 1, revision: 1, pending_plan_key: null, pending_plan_version: null, pending_effective_at: null });+ if (String(url).endsWith('/telemetry-settings') && management) return Response.json({ capture_prompt_content: false });+ if (String(url).endsWith(`/api/models/${model}`) && management) return Response.json({ model: { id: '00000000-0000-0000-0000-000000000004', slug: model } });+ if (String(url).endsWith('/provider-policy') && management) return Response.json({ zdr_entitled: true, policy: { require_no_training: true, zdr_continuation_storage: false } });+ throw new Error('unexpected preflight request');+ }+ calls.push({ url: String(url), headers: new Headers(init.headers), body: JSON.parse(init.body) });+ const result = response();+ if (side === 'experiential-fixture' && verdict !== null) result.headers.set('x-gateway-zdr', verdict);+ return result;+ };+ let sdkFetch: any = transport;+ if (side === 'experiential-fixture') {+ const auth = { isValid: true, tier: 'subscribed', accountPlan: 'business', userId: 'synthetic-user', deviceId: 'synthetic-device' } as const;+ const actorId = await hostedChatActorId(auth);+ const c = await prepareExperientialProbeConnection({ mode: 'isolated-test', auth, sourceModelId: model,+ managementKey: `xpl_${'1'.repeat(40)}`, transport,+ mappings: [{ sourceModelId: model, destinationModelId: model, canonicalModelUuid: '00000000-0000-0000-0000-000000000004', protocol: model === 'gpt-6-astra' ? 'responses' : model.startsWith('claude-') ? 'messages' : 'chat' }],+ lookupBinding: async () => ({ actorId, orgId: '00000000-0000-0000-0000-000000000001', customerIdentityId: 'customer-alice', provisioningIdentityId: 'entitlement-sync', accountPlan: 'business', expectedPlan: { plan_key: 'business', plan_version: 1, revision: 1 }, customerKey: key }),+ });+ sdkFetch = c.fetch;+ expect(c.production_ready).toBe(false);+ }+ provider.client.fetch = sdkFetch;+ if (provider.responsesClient) provider.responsesClient.fetch = sdkFetch;+ return calls;+}+async function connection(side: 'cloudflare' | 'experiential-fixture', provider: 'openai' | 'anthropic') {+ if (side === 'cloudflare') return getHostedChatGatewayConnection(env, provider, context);+ // Documented destination wire shape only, not a production routing selector.+ return { apiKey: key, baseURL: `${EXPERIENTIAL_ORIGIN}${provider === 'openai' ? '/v1' : ''}`, maxRetries: 0,+ defaultHeaders: { Authorization: `Bearer ${key}`, ...(provider === 'anthropic' ? { 'x-api-key': null } : {}) } };+}+function assertWire(call: { url: string; headers: Headers; body: any }, side: string, provider: string, suffix: string) {+ expect(call.url).toBe(side === 'cloudflare'+ ? `https://gateway.ai.cloudflare.com/v1/synthetic/parity/${provider}${suffix}`+ : `${EXPERIENTIAL_ORIGIN}/v1${suffix.replace(/^\/v1/, '')}`);+ expect(call.headers.get('x-api-key')).toBeNull();+ expect(call.headers.get('authorization')).toBe(side === 'cloudflare' ? null : `Bearer ${key}`);+ if (side === 'cloudflare') {+ expect(call.headers.get('cf-aig-collect-log-payload')).toBe('false');+ expect(JSON.parse(call.headers.get('cf-aig-metadata')!)).toEqual(context);+ expect(call.body).not.toHaveProperty('provider');+ } else {+ expect(call.headers.get('cf-aig-metadata')).toBeNull();+ expect(call.body.provider).toEqual({ zdr: true });+ }+}+async function chunks(stream: ReadableStream) {+ const text = await new Response(stream).text();+ expect(text.endsWith('data: [DONE]\n\n')).toBe(true);+ return text.split('\n\n').filter(line => line.startsWith('data: {')).map(line => JSON.parse(line.slice(6)));+}++for (const side of ['cloudflare', 'experiential-fixture'] as const) describe(`${side} installed native SDK wire`, () => {+ it('preserves OpenAI tools, images and structured output without provider auth leakage', async () => {+ const c = await connection(side, 'openai');+ const provider = new OpenAIProvider(c.apiKey, c.baseURL, c.defaultHeaders, c.maxRetries);+ const calls = await intercept(provider, () => Response.json(completion()), side);+ const result: any = await (await provider.createCompletion(body('gpt-5.4-mini'))).json();+ expect(calls).toHaveLength(1); assertWire(calls[0], side, 'openai', '/chat/completions');+ expect(calls[0].body.messages[1].content[1]).toMatchObject({ type: 'image_url', image_url: { url: 'data:image/png;base64,AAAA' } });+ expect(calls[0].body.tools).toEqual(tools);+ expect(calls[0].body.response_format).toMatchObject({ type: 'json_schema', json_schema: { schema } });+ expect(result.choices[0].finish_reason).toBe('tool_calls');+ expect(result.choices[0].message.tool_calls[0].function.arguments).toBe('{"ok":true}');+ expect(result.usage.prompt_tokens_details.cached_tokens).toBe(3);+ });+ it('parses fragmented OpenAI tool streams and final usage', async () => {+ const c = await connection(side, 'openai');+ const provider = new OpenAIProvider(c.apiKey, c.baseURL, c.defaultHeaders, c.maxRetries);+ const calls = await intercept(provider, () => sse([+ { choices: [{ index: 0, delta: { tool_calls: [{ index: 0, id: 'call_1', type: 'function', function: { name: 'confirm', arguments: '{"ok":' } }] } }] },+ { choices: [{ index: 0, delta: { tool_calls: [{ index: 0, function: { arguments: 'true}' } }] } }] },+ { choices: [{ index: 0, delta: {}, finish_reason: 'tool_calls' }] }, { choices: [], usage },+ ]), side);+ const result = await chunks(await provider.createStreamingCompletion(body('gpt-5.4-mini')));+ expect(calls).toHaveLength(1); assertWire(calls[0], side, 'openai', '/chat/completions');+ expect(calls[0].body.stream_options).toEqual({ include_usage: true });+ expect(result.flatMap(c => c.choices[0]?.delta.tool_calls ?? []).map(c => c.function.arguments).join('')).toBe('{"ok":true}');+ expect(result.at(-1).usage).toEqual({ ...usage, cache_creation_input_tokens: 0 });+ });+ it('serializes Anthropic native Messages and translates cache and tool usage', async () => {+ const c = await connection(side, 'anthropic');+ const provider = new AnthropicProvider(c.apiKey, c.baseURL, c.defaultHeaders, c.maxRetries);+ const calls = await intercept(provider, () => Response.json({ id: 'msg_fixture', type: 'message', role: 'assistant', model: 'claude-sonnet-5',+ content: [{ type: 'tool_use', id: 'call_1', name: 'confirm', input: { ok: true } }], stop_reason: 'tool_use',+ usage: { input_tokens: 5, output_tokens: 2, cache_read_input_tokens: 3, cache_creation_input_tokens: 1 } }), side, 'claude-sonnet-5');+ const result: any = await (await provider.createCompletion(body('claude-sonnet-5'))).json();+ expect(calls).toHaveLength(1); assertWire(calls[0], side, 'anthropic', '/v1/messages');+ expect(calls[0].headers.get('anthropic-version')).toBe('2023-06-01');+ expect(calls[0].body.messages[0].content[1]).toEqual({ type: 'image', source: { type: 'base64', media_type: 'image/png', data: 'AAAA' }, cache_control: { type: 'ephemeral' } });+ expect(calls[0].body.tools[0]).toMatchObject({ name: 'confirm', input_schema: schema });+ // Existing provider expresses JSON schema as system instructions, not native+ // constrained decoding. This fixture must not claim stronger guarantees.+ expect(JSON.stringify(calls[0].body.system)).toContain('strictly follows this schema');+ expect(result.choices[0].finish_reason).toBe('tool_calls');+ expect(result.choices[0].message.tool_calls[0]).toEqual({ id: 'call_1', type: 'function', function: { name: 'confirm', arguments: '{"ok":true}' } });+ // Feed the actual first-turn Chat response back into the provider. Dropping+ // its ID would leave a tool_result without the matching Anthropic tool_use.+ const secondCalls = await intercept(provider, () => Response.json({ id: 'msg_second', type: 'message', role: 'assistant', model: 'claude-sonnet-5', content: [{ type: 'text', text: 'OK' }], stop_reason: 'end_turn', usage: { input_tokens: 9, output_tokens: 1 } }), side, 'claude-sonnet-5');+ const second: any = await (await provider.createCompletion({ model: 'claude-sonnet-5', max_tokens: 64, messages: [+ { role: 'user', content: 'Confirm.' }, result.choices[0].message, { role: 'tool', tool_call_id: 'call_1', content: 'confirmed' },+ ] })).json();+ expect(secondCalls[0].body.messages[1].content[0]).toMatchObject({ type: 'tool_use', id: 'call_1', name: 'confirm', input: { ok: true } });+ expect(secondCalls[0].body.messages[2].content[0]).toMatchObject({ type: 'tool_result', tool_use_id: 'call_1', content: 'confirmed' });+ expect(second.choices[0].finish_reason).toBe('stop');+ expect(result.usage).toMatchObject({ prompt_tokens: 9, completion_tokens: 2, prompt_tokens_details: { cached_tokens: 3 }, cache_creation_input_tokens: 1 });+ });+ it('parses native Anthropic tool stream into Chat SSE with cache usage', async () => {+ const c = await connection(side, 'anthropic');+ const provider = new AnthropicProvider(c.apiKey, c.baseURL, c.defaultHeaders, c.maxRetries);+ const calls = await intercept(provider, () => sse([+ { type: 'message_start', message: { id: 'msg_fixture', type: 'message', role: 'assistant', content: [], model: 'claude-sonnet-5', usage: { input_tokens: 5, output_tokens: 0, cache_read_input_tokens: 3, cache_creation_input_tokens: 1 } } },+ { type: 'content_block_start', index: 0, content_block: { type: 'tool_use', id: 'call_1', name: 'confirm', input: {} } },+ { type: 'content_block_delta', index: 0, delta: { type: 'input_json_delta', partial_json: '{"ok":' } },+ { type: 'content_block_delta', index: 0, delta: { type: 'input_json_delta', partial_json: 'true}' } },+ { type: 'content_block_stop', index: 0 }, { type: 'message_delta', delta: { stop_reason: 'tool_use' }, usage: { output_tokens: 2 } }, { type: 'message_stop' },+ ], true), side, 'claude-sonnet-5');+ const result = await chunks(await provider.createStreamingCompletion(body('claude-sonnet-5')));+ expect(calls).toHaveLength(1); assertWire(calls[0], side, 'anthropic', '/v1/messages');+ expect(result.flatMap(c => c.choices[0]?.delta.tool_calls ?? []).map(c => c.function.arguments).join('')).toBe('{"ok":true}');+ expect(result.at(-2).choices[0].finish_reason).toBe('tool_calls');+ expect(result.at(-1).usage).toMatchObject({ prompt_tokens: 9, completion_tokens: 2, cache_creation_input_tokens: 1 });+ });+ it('keeps Astra Low/Standard/stateless and translates native Responses streams', async () => {+ const c = await connection(side, 'openai');+ const provider = new AstraProvider(c.apiKey, c.baseURL, c.defaultHeaders, c.maxRetries);+ const calls = await intercept(provider, () => sse([+ { type: 'response.created', response: { id: 'resp_fixture', created_at: 1 } },+ { type: 'response.output_item.added', item: { type: 'function_call', id: 'fc_1', call_id: 'call_1', name: 'confirm', arguments: '' } },+ { type: 'response.function_call_arguments.delta', item_id: 'fc_1', delta: '{"ok":true}' },+ { type: 'response.completed', response: { id: 'resp_fixture', status: 'completed', usage: { input_tokens: 8, output_tokens: 2, total_tokens: 10, input_tokens_details: { cached_tokens: 3 }, output_tokens_details: { reasoning_tokens: 1 } } } },+ ], true), side, 'gpt-6-astra');+ const result = await chunks(await provider.createStreamingCompletion({ ...body('gpt-6-astra'), reasoning_effort: 'high', store: true, previous_response_id: 'must-not-forward', service_tier: 'priority' } as any));+ expect(calls).toHaveLength(1); assertWire(calls[0], side, 'openai', '/responses');+ expect(calls[0].body).toMatchObject({ model: 'gpt-6-astra', reasoning: { effort: 'low' }, service_tier: 'default', store: false, stream: true });+ expect(calls[0].body).not.toHaveProperty('previous_response_id');+ expect(calls[0].body.input[1].content[1]).toMatchObject({ type: 'input_image', image_url: 'data:image/png;base64,AAAA' });+ expect(calls[0].body.text.format).toMatchObject({ type: 'json_schema', schema });+ expect(result.at(-2).choices[0].finish_reason).toBe('tool_calls');+ expect(result.at(-1).usage).toMatchObject({ prompt_tokens: 8, completion_tokens: 2, prompt_tokens_details: { cached_tokens: 3 } });+ });+});++describe('OpenAI nonstream terminal reasons', () => {+ it.each(['stop', 'tool_calls', 'length', 'content_filter', null, undefined])('preserves upstream %s without manufacturing success', finish_reason => {+ const native = completion();+ native.choices[0].finish_reason = finish_reason as any;+ const result = new OpenAIProvider('synthetic-key').formatResponse(native);+ expect(result.choices[0].finish_reason).toBe(finish_reason);+ });+});++describe('Anthropic nonstream terminal reasons', () => {+ it.each([['end_turn', 'stop'], ['stop_sequence', 'stop'], ['tool_use', 'tool_calls'], ['max_tokens', 'length'], ['refusal', 'content_filter']] as const)('maps %s to %s', (stop_reason, finish_reason) => {+ const result = new AnthropicProvider('synthetic-key').formatResponse({ id: 'msg_fixture', role: 'assistant', type: 'message', model: 'claude-sonnet-5', content: [{ type: 'text', text: 'Synthetic.' }], stop_reason, usage: { input_tokens: 1, output_tokens: 1 } } as any);+ expect(result.choices[0].finish_reason).toBe(finish_reason);+ });+});++describe('ordinary GLM stays on the existing Cloudflare route', () => {+ it('retains container bearer, fixed path and request normalization', async () => {+ const c = await getHostedChatGatewayConnection(env, 'custom-tinfoil', context);+ const provider = new ScreenpipeGlmProvider(c.apiKey, c.baseURL, c.defaultHeaders, c.maxRetries);+ const calls = await intercept(provider, () => Response.json(completion()));+ await provider.createCompletion({ model: SCREENPIPE_GLM_MODEL, messages: [{ role: 'user', content: 'Synthetic text only.' }], tools, reasoning_effort: 'medium' } as RequestBody);+ expect(calls).toHaveLength(1);+ expect(calls[0].url).toBe('https://gateway.ai.cloudflare.com/v1/synthetic/parity/custom-tinfoil/glm/v1/chat/completions');+ expect(calls[0].headers.get('authorization')).toBe('Bearer synthetic-glm-key');+ expect(calls[0].headers.get('cf-aig-byok-alias')).toBeNull();+ expect(calls[0].body.chat_template_kwargs).toEqual({ enable_thinking: false });+ expect(calls[0].body.model).toBe(SCREENPIPE_GLM_MODEL);+ });+});+++describe('installed SDKs cannot bypass an unconfirmed ZDR response', () => {+ it.each(['chat-json', 'chat-stream', 'messages-json', 'messages-stream', 'responses-stream'])('%s cancels before parsing and does not retry', async protocol => {+ const anthropic = protocol.startsWith('messages');+ const model = anthropic ? 'claude-sonnet-5' : protocol.startsWith('responses') ? 'gpt-6-astra' : 'gpt-5.4-mini';+ const c = await connection('experiential-fixture', anthropic ? 'anthropic' : 'openai');+ const Provider = anthropic ? AnthropicProvider : protocol.startsWith('responses') ? AstraProvider : OpenAIProvider;+ const provider = new Provider(c.apiKey, c.baseURL, c.defaultHeaders, c.maxRetries);+ let cancelled = false;+ let reads = 0;+ const calls = await intercept(provider, () => new Response(new ReadableStream({+ pull() { reads++; }, cancel() { cancelled = true; },+ }, { highWaterMark: 0 })), 'experiential-fixture', model, null);+ const result = protocol.endsWith('stream') ? provider.createStreamingCompletion(body(model)) : provider.createCompletion(body(model));+ await expect(result).rejects.toBeDefined();+ expect(calls).toHaveLength(1);+ expect(cancelled).toBe(true); expect(reads).toBe(0);+ expect(calls[0].body.provider).toEqual({ zdr: true });+ });+});