mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-29 23:56:43 +01:00
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work > - Runner, plugin, and Playwright tests verify how those agents run, connect, and appear in the board > - Most package tsconfigs exclude tests, and Vitest strips types, so a type error in those tests fails nothing > - The tests drifted to 803 real type errors, and some of those errors hid assertions that checked nothing > - paperclipai/paperclip#18739 and paperclipai/paperclip#18738 check the server test tree and four test projects, but not these files > - This pull request adds a required check for these test files, fixes every error, and fails CI if a test file is in no program > - The benefit is that a type error in any test file outside `server/src` now fails CI ## Linked Issues or Issue Description Refs #18739 Refs #18738 **What happened?** 517 test files were in no program that required CI checks. A type error in those files did not fail CI. **Expected behavior** CI fails on a type error in any test file. **Steps to reproduce** Add `export const value: number = "text";` to a test file under `packages/paperclip-runner/src` on master. CI stays green, because `tsconfig.json` excludes test files and Vitest strips types. **Measured gap** Each row is a program with its tests included, on one thread. File and error counts are for head `7d4b5e907a`. Memory peaks were measured before the two latest merges of master. | Group | Test files | Real errors | Peak | | --- | ---: | ---: | ---: | | `paperclip-runner` Node source | 290 | 644 | 1.09 GiB | | `paperclip-runner` surfaces | 9 | 12 | 0.42 GiB | | `paperclip-runner` devtools and examples | 14 | 0 | 0.68 GiB | | `sandbox-providers/kubernetes` | 21 | 65 | 0.44 GiB | | 8 other sandbox providers and `bridge-template` | 32 | 4 | 0.34 GiB | | `plugins/sdk` | 22 | 15 | 0.41 GiB | | `plugin-workspace-diff` | 5 | 0 | 0.37 GiB | | `plugin-orchestration-smoke-example` | 1 | 1 | 0.15 GiB | | `kv-demo-mcp-server` | 3 | 0 | 0.13 GiB | | `packages/db` scripts | 1 | 0 | 1.01 GiB | | 8 Playwright suites under `tests/` | 52 | 25 | 1.72 GiB | | `tests/runner-acceptance` | 3 | 0 | 0.58 GiB | | `tests/e2e`, through layer declarations | 63 | 37 | at most 1.59 GiB | | `ui` `AttentionInteractionResolver.test.tsx` | 1 | 0 | n/a | | **Total** | **517** | **803** | | 27 of the 803 errors came from master while this pull request was open. Each merge of master brought new test code that no check had read. Errors that were config errors, not real errors: - One TS2688 for each provider, because the root install does not install provider dependencies. - 9 in the Runner Node program (TS7016 and TS6059). - 2 in `tsconfig.browser.json`. TypeScript 7 removed `baseUrl`, so that config was invalid. - 213 in the Playwright suites (`@/*` aliases and Vite types). - About 60 in `tests/e2e` (import extensions and aliases). `packages/plugins/create-paperclip-plugin` has no gap. Its tsconfig excludes its test, but `cli/tsconfig.json` reaches that test through an import. `server/test-baselines` also has no gap, because `tests/lifecycle-baseline` reads it. ## What Changed - Added `scripts/typecheck-test-programs.mjs` and its tests. The `Typecheck + Release Registry` job runs it. It checks each program in `PROGRAMS` on one thread. - The script fails if a tracked `*.test.ts(x)` or `*.spec.ts(x)` file outside `server/src` is in no program. The compiler file list decides this, because `include` drops `foo.test.tsx` when `foo.test.ts` exists. - Added a `tsconfig.test.json` to each uncovered package, and `tests/tsconfig.playwright.json`. - Added `tests/e2e` to `PROJECTS` in `scripts/typecheck-test-projects.mjs`, with a `tsconfig.json` and a `tsconfig.layers.json`. - Added `scripts/typecheck-sandbox-providers`. It is a small workspace that installs provider SDKs from one lockfile. I did not add a lockfile inside each provider, because `scripts/build-standalone-public-packages.mjs` uses a frozen install when one exists, and that changes what a release resolves. - Corrected `packages/paperclip-runner/tsconfig.browser.json` for TypeScript 7. - Set `rootDir` to `..` in `packages/paperclip-runner/tsconfig.test.json`, because one Runner test imports from `packages/adapter-utils/src`. The program does not emit files, so this changes no build output. - Moved the Kubernetes provider from `vitest ^3.2.4` to `^4.1.8`, the version the other providers use. Vitest 3 requires `tinypool ^1.1.1`, which has two critical advisories (GHSA-85c8-ppgw-ccpr and GHSA-5gmw-xhrv-c9v3). The first patched version is 2.1.2. Its tests have the same result on both versions: 189 passed, and the same 12 `file-sync` tests failed. - Moved the Kubernetes provider from `zod ^3.24.2` to `^4.6.6`, and gave its two `z.record` calls a key schema. This is the only production change, and it stays in this pull request. See "Production change for review". - Renamed `AttentionInteractionResolver.test.ts` to `AttentionInteractionResolver.replace.test.ts`, so TypeScript reads the `.test.tsx` beside it. - Fixed all 803 errors, with changes to test files and test support only. The fix for `contracts/native-execution.test.ts` is the same as the fix in paperclipai/paperclip#18739, so those two changes merge cleanly. - Updated `doc/DEVELOPING.md`. This pull request does not edit `server/scripts/typecheck-layers.mjs`. paperclipai/paperclip#18739 and paperclipai/paperclip#18738 have both merged, and this branch includes them. ### Production change for review The new lockfile made dependency review flag `zod@3.25.76` as a high-severity advisory (GHSA-mrhx-c7xc-m3w5). The first patched version is 4.6.6, so 3.x has no fix. The published Kubernetes plugin uses zod to validate operator config. The zod 3 source does not compile against zod 4, so a typecheck-only override cannot fix this. The existing tests do not prove this change, because they also passed with the invalid one-argument `z.record`. So I parsed 45 inputs with the same schema on zod 3.25.76 and zod 4.6.7: | Result | Inputs | Detail | | --- | ---: | --- | | Identical | 23 | Includes defaults, valid config, and a `__proto__` key | | Same outcome, new error wording | 21 | For example, `Invalid url` is now `Invalid URL`. `validateConfig` shows this text to operators. | | Different outcome | 1 | `jobTtlSecondsAfterFinished: 2 ** 60` was valid and is now rejected. The new limit is `Number.MAX_SAFE_INTEGER`. | I did not test this against a real cluster. The decision is to keep this change in this pull request. Since then, paperclipai/paperclip#17364 has moved the rest of the repository to zod 4.6.x, so the Kubernetes plugin now matches it. ### Behavior change for review `reducedMotion` under `use` is not a Playwright option. It had no effect in 11 configs and 2 `test.use` calls, so those suites ran with `no-preference`. The options are now under `contextOptions`, so reduced motion now takes effect. `app-shell-design.spec.ts` asserts a real transition, so it now asks for `no-preference` itself. The baseline manifest has `snapshotCount: 0`, so this invalidates no published baseline. A frozen trial reference PNG can differ. ### Findings These are product questions. I did not change them, and I did not hide them with casts: 1. **`PrpEvent` and its schema disagree.** The TypeScript type in `src/protocol/replay-contract.ts` requires `normalizedSessionId`. The `required` list in `protocol/schemas/event.schema.json` does not include it. `validatePrpEvent` accepts events that the type forbids. 2. **`requestCheckboxConfirmation` is stricter than its runtime.** It uses the output type, so plugin authors must pass `continuationPolicy` and `allowDeclineReason`, although the schema gives them defaults. `z.input` would fix this. 3. **`RetainedRunnerdMaintenanceInput.backend.kind` is a free string.** The quarantine domain comes from `descriptor.kind`, but the release uses `proof.backend.kind`. If they differ, `completeRetainedNativeSessionCleanup` returns 0 and the quarantine stays. The production caller passes `"runner"` today, so this is not a live bug. 4. **`CodexHarnessSession.close(input?)` is optional, but the contract requires `{ reason }`.** These are tests that verified nothing. I corrected them, and they pass: - `backends/native-instruction-measurement.test.ts` filtered on `placement === "core"`, which is not a valid placement. It measured an empty tool list. It now measures 14 tools. - `drivers/acpx/codex-acpx-driver.test.ts` checked for `usage.updated`, which is not an event type. - The `hostFixture()` helper in `drivers/acpx/runtime-host.test.ts` ignored two overrides, so `expect(prepareSandbox).not.toHaveBeenCalled()` could not fail. - `live/live-session.test.ts` set `agent: "pi"` before a spread that overwrote it. - `native-distribution-integrity.test.ts` passed an AbortSignal where `openCommand` expects `{ signal }`. These are gaps that this pull request does not close: - **The root lockfile still has some old zod versions.** paperclipai/paperclip#17364 moved the direct zod dependencies to 4.6.x. `pnpm-lock.yaml` still resolves `zod@3.25.76`, `4.1.11`, and `4.4.3`, which are below the patched 4.6.6. `packages/paperclip-runner/scripts/pi-distribution/package.json` pins `zod` to `3.25.76`, and some agent SDKs resolve `4.4.3`. Dependency review does not flag them, because this pull request does not add them. - **Dead GitHub coverage.** `chat-adapters-ui-providers.spec.ts` returned early for GitHub, so four later `provider === "github"` branches never ran. I removed them. They included the private key file and paste match, and the reconnect flow. Nothing asserts those behaviors in that spec now. - **Provider tests do not run in CI.** The root `vitest.config.ts` lists only Daytona. The other 8 providers, `bridge-template` included, are typechecked now, but CI does not run their tests. - **One `packages/db` test never runs.** `scripts/clean-poisoned-claude-sessions.test.ts` is outside the `include` of `packages/db/vitest.config.ts`. - **`plugin-orchestration-smoke-example` does not collect.** `@paperclipai/shared` is not linked in its `node_modules`. It passes 3 of 3 with an alias. ## Verification Run these commands: ```sh node scripts/typecheck-test-programs.mjs --low-memory pnpm --filter @paperclipai/server typecheck node scripts/typecheck-test-projects.mjs --low-memory --reuse-declarations node --test scripts/typecheck-test-programs.test.mjs ``` Results on macOS (arm64), Node 26.12. CI uses Node 24, and its `Typecheck + Release Registry` job also passes. At head `7d4b5e907a`, the test programs, `tests/e2e`, and the script tests pass again with 0 errors. The times and peaks below come from the run before the two latest merges of master: | Check | Result | | --- | --- | | Test programs, `--low-memory` | Exit 0, 0 errors, 42 seconds, peak 1.78 GiB | | Server layers, low memory | Exit 0, 0 errors, 38 seconds, peak 3.41 GiB | | Test projects, including `tests/e2e` | Exit 0, 0 errors, 13 seconds, peak 1.59 GiB | | Script tests | 12 passed | | Injected type error in each of 23 places | Each one failed its program with TS2322 | | Coverage check | 0 unchecked files, 10 seconds | I did not run this in a 4 GiB container. The peaks above are `maximum resident set size` for the largest single process. **Runtime check of the fixes.** Types do not prove that a test still works, so I ran the suites before the two latest merges of master: | Suite | Result | | --- | --- | | Runner, full suite | 4,885 passed, 3 failed, 13 skipped | | Sandbox providers, 10 packages | 939 passed, 12 failed, 36 skipped | | Plugin SDK | 200 passed | | `kv-demo-mcp-server` | 12 passed | | Renamed UI test and its sibling | 4 passed | All 15 failures exist without these fixes or come from the local machine: - **3 Cursor Cloud timeouts.** `artifact-delivery.test.ts` has 1 and `cursor-cloud-driver.test.ts` has 2. They time out in a full local run, and all 133 tests in those files with `codex-security-config.test.ts` pass when run alone. They fail in the same way with those two files at their unedited versions. - **12 Kubernetes `file-sync.test.ts` failures.** They fail in the same way before and after the fixes. The first failure is that `/proc/self/fd` does not exist on macOS. After each of the two latest merges of master, I ran only the Runner test files that I changed: | Merge | Result | | --- | --- | | `885c249cb9`, 8 files | 534 passed | | `7d4b5e907a`, 7 files | 672 passed, 16 failed | The 16 failures are in the two `source-reconciliation` test files. They fail in the same way with master's unedited versions of those files. 15 of them fail with `ACPX sidecar exited at initialize`, so I think my local build of the Rust and sidecar binaries is out of date. I did not rebuild them to check. CI builds them from scratch, and all Runner jobs pass in CI at `7d4b5e907a`. **Not run:** - `tests/e2e` and the 8 Playwright suites need a server, so most of them have only compile checks. `playwright test --list` loads 308 tests from the main e2e config. - `packages/plugins/sandbox-providers/kubernetes/test/integration` needs a cluster, so it has only compile checks. - I did not run the full `pnpm test:run` or root `pnpm build`. Required CI runs them. **Reduced motion.** I ran 6 motion-sensitive Storybook specs in system Chrome with a temporary config, once with `reduce` and once with `no-preference`. Both runs had 45 passed and the same 5 failed. So reduced motion did not change those results. The 5 failures do not depend on motion. At least one comes from the browser, because system Chrome prints `oklch(55.6% 0 0)` where the test expects `oklch(0.556 0 0)`. I did not run the other Storybook specs. The `storybook-visual` label runs that workflow. ## Risks - **A new lockfile to maintain.** A change to a provider `package.json` now fails the typecheck job until someone refreshes `scripts/typecheck-sandbox-providers/pnpm-lock.yaml`. The failure message gives the command. - **The job has a new install.** The step downloads provider SDKs, so it depends on the npm registry. It uses `--frozen-lockfile` and `--ignore-scripts`. - **The local install replaces provider `node_modules`.** A contributor with a standalone provider install must reinstall that provider after running the script. - **Reduced motion can change Storybook results.** See "Behavior change for review". That workflow is not part of required CI. - **Kubernetes plugin validation changed.** A release of this plugin ships zod 4. Error wording changes, and very large integers are now rejected. See "Production change for review". - **Master moves fast.** Each merge of master brought new test code with type errors, 27 so far. After this merges, the check reports those errors in the pull request that adds them. ## Model Used - Provider: Anthropic Claude, through Claude Code - Model: Claude Opus 5.5 (`claude-opus-5-5`) - Capabilities: tool use, code execution, and subagents for the type fixes in separate files ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have searched GitHub for duplicate or related PRs and linked them above - [x] I have either (a) linked existing issues with `Fixes: #` / `Closes #` / `Refs #` OR (b) described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip` URLs) - [ ] My branch name describes the change (e.g. `docs/...`, `fix/...`) and contains no internal Paperclip ticket id or instance-derived details. The app generated the branch name. - [ ] I have run tests locally and they pass. See the failures explained in Verification. - [x] I have added or updated tests where applicable - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [x] All Paperclip CI gates are green - [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2002 lines
74 KiB
TypeScript
2002 lines
74 KiB
TypeScript
import { describe, expect, expectTypeOf, it, vi } from "vitest";
|
|
|
|
import type {
|
|
CapabilityFixtureApproval,
|
|
CapabilityFixtureSeed,
|
|
CapabilityJsonValue,
|
|
CapabilitySemanticToolRuntimeSnapshot,
|
|
} from "../mock-core/capability-control-plane-types.js";
|
|
import {
|
|
CapabilityMockControlPlaneAdapter,
|
|
type CapabilitySemanticToolRuntimeStore,
|
|
} from "../mock-core/capability-mock-control-plane-adapter.js";
|
|
import {
|
|
CAPABILITY_OPTIONAL_TOOL_CATALOGS,
|
|
CAPABILITY_SEMANTIC_TOOL_CATALOG,
|
|
capabilitySemanticTool,
|
|
} from "./capability-semantic-tool-catalog.js";
|
|
import { CapabilitySemanticToolRuntime, validateJsonSchema } from "./capability-semantic-tool-runtime.js";
|
|
import { CapabilityCodexToolBinding, CapabilityFakeAgentToolBinding } from "./capability-tool-bindings.js";
|
|
import {
|
|
CAPABILITY_CONTROL_PLANE_OWNED_OPERATION_IDS,
|
|
type CapabilityModelToolSuccess,
|
|
type CapabilityToolSuccess,
|
|
} from "./capability-semantic-tool-types.js";
|
|
|
|
const OPEN = {
|
|
identity: {
|
|
runId: "run-tools",
|
|
sessionId: "session-tools",
|
|
companyId: "company-1",
|
|
issueId: "task-1",
|
|
agentId: "actor-1",
|
|
},
|
|
backendKind: "mock" as const,
|
|
sourceInstanceId: "capability-tools-test",
|
|
};
|
|
|
|
async function runtimeFor(options: {
|
|
workMode?: "standard" | "ask" | "planning" | "skill_test";
|
|
role?: string;
|
|
actorGrants?: string[];
|
|
scenarioGrants?: string[];
|
|
policy?: ConstructorParameters<typeof CapabilitySemanticToolRuntime>[0]["policy"];
|
|
seed?: CapabilityFixtureSeed;
|
|
resolveSecretValue?: (name: string) => string | null;
|
|
semanticToolRuntimeStore?: CapabilitySemanticToolRuntimeStore;
|
|
now?: () => number;
|
|
} = {}) {
|
|
const actor = {
|
|
id: "actor-1",
|
|
companyId: "company-1",
|
|
name: "Fixture Actor",
|
|
role: options.role ?? "engineer",
|
|
status: "active" as const,
|
|
budgetId: "budget-actor-1",
|
|
capabilityGrants: options.actorGrants ?? [],
|
|
};
|
|
const adapter = new CapabilityMockControlPlaneAdapter(
|
|
{
|
|
...options.seed,
|
|
actors: [actor],
|
|
tasks: options.seed?.tasks ?? [{
|
|
id: "task-1",
|
|
companyId: "company-1",
|
|
identifier: "MCK-1",
|
|
title: "Tool boundary fixture",
|
|
description: "Protected task details",
|
|
status: "todo",
|
|
priority: "high",
|
|
workMode: options.workMode ?? "standard",
|
|
parentId: null,
|
|
assigneeActorId: "actor-1",
|
|
checkoutRunId: null,
|
|
executionRunId: null,
|
|
startedAt: null,
|
|
completedAt: null,
|
|
}],
|
|
},
|
|
{ semanticToolRuntimeStore: options.semanticToolRuntimeStore },
|
|
);
|
|
await adapter.start();
|
|
await adapter.openFixtureRun(OPEN);
|
|
return {
|
|
adapter,
|
|
runtime: new CapabilitySemanticToolRuntime({
|
|
adapter,
|
|
runId: OPEN.identity.runId,
|
|
scenarioGrants: options.scenarioGrants,
|
|
policy: options.policy,
|
|
resolveSecretValue: options.resolveSecretValue,
|
|
now: options.now,
|
|
}),
|
|
};
|
|
}
|
|
|
|
function pendingLeaseExpiry(snapshot: CapabilitySemanticToolRuntimeSnapshot | null): number | undefined {
|
|
const extension = snapshot?.extensions[0];
|
|
return extension?.status === "pending" ? extension.leaseExpiresAtMs : undefined;
|
|
}
|
|
|
|
describe("Capability semantic tool catalog", () => {
|
|
it("defines a unique versioned provider-neutral catalog and every optional group", () => {
|
|
const operationIds = CAPABILITY_SEMANTIC_TOOL_CATALOG.map((tool) => tool.operationId);
|
|
expect(new Set(operationIds).size).toBe(operationIds.length);
|
|
expect(CAPABILITY_SEMANTIC_TOOL_CATALOG.every((tool) => tool.version === 1)).toBe(true);
|
|
expect(CAPABILITY_SEMANTIC_TOOL_CATALOG.every((tool) => tool.inputSchema.type === "object")).toBe(true);
|
|
expect(CAPABILITY_SEMANTIC_TOOL_CATALOG.every((tool) => tool.outputSchema.type === "object")).toBe(true);
|
|
expect(CAPABILITY_SEMANTIC_TOOL_CATALOG.every((tool) => tool.mockCommandMapping !== undefined)).toBe(true);
|
|
|
|
expect(Object.keys(CAPABILITY_OPTIONAL_TOOL_CATALOGS).sort()).toEqual([
|
|
"cases",
|
|
"company_skills",
|
|
"delegation_dependencies",
|
|
"discovery",
|
|
"governance",
|
|
"portability_admin",
|
|
"routines",
|
|
"secrets",
|
|
"test_escape_hatch",
|
|
"workspace_runtime",
|
|
]);
|
|
expect(Object.values(CAPABILITY_OPTIONAL_TOOL_CATALOGS).every((tools) => tools.length > 0)).toBe(true);
|
|
expect([
|
|
"get_task_context",
|
|
"get_task_history",
|
|
"list_documents",
|
|
"read_document",
|
|
"list_document_revisions",
|
|
"report_progress",
|
|
"answer_status_question",
|
|
"finish_task",
|
|
"block_task",
|
|
"request_review",
|
|
"write_document",
|
|
"request_human_input",
|
|
"register_deliverable",
|
|
"inspect_operation_result",
|
|
].every((id) => capabilitySemanticTool(id)?.disposition === "always_agent_tool")).toBe(true);
|
|
});
|
|
|
|
it("keeps control-plane operations absent and binds identical fake-agent and Codex surfaces", async () => {
|
|
const { runtime } = await runtimeFor();
|
|
const visible = runtime.visibleTools();
|
|
const fake = new CapabilityFakeAgentToolBinding().bind(visible);
|
|
const codex = new CapabilityCodexToolBinding().bind(visible);
|
|
expect(fake.map((tool) => tool.operationId)).toEqual(codex.map((tool) => tool.name));
|
|
expect(codex.every((tool) => tool.type === "function" && tool.strict)).toBe(true);
|
|
for (const operationId of CAPABILITY_CONTROL_PLANE_OWNED_OPERATION_IDS) {
|
|
expect(visible.tools.some((tool) => tool.operationId === operationId)).toBe(false);
|
|
}
|
|
expect(JSON.stringify({ fake, codex })).not.toContain("Bearer ");
|
|
});
|
|
});
|
|
|
|
describe("Capability exposure and authorization", () => {
|
|
it("computes exposure before invocation from task mode, claims, role, and policy", async () => {
|
|
const baseline = await runtimeFor();
|
|
const baselineIds = baseline.runtime.visibleTools().tools.map((tool) => tool.operationId);
|
|
expect(baselineIds).toContain("finish_task");
|
|
expect(baselineIds).not.toContain("search_tasks");
|
|
|
|
const ask = await runtimeFor({ workMode: "ask" });
|
|
const askIds = ask.runtime.visibleTools().tools.map((tool) => tool.operationId);
|
|
expect(askIds).toContain("answer_status_question");
|
|
expect(askIds).not.toContain("finish_task");
|
|
expect(askIds).not.toContain("write_document");
|
|
|
|
const granted = await runtimeFor({
|
|
scenarioGrants: ["discovery:tasks:read", "governance:approvals:decide"],
|
|
role: "approver",
|
|
policy: { deniedOperationIds: ["search_tasks"] },
|
|
});
|
|
const grantedIds = granted.runtime.visibleTools().tools.map((tool) => tool.operationId);
|
|
expect(grantedIds).not.toContain("search_tasks");
|
|
expect(grantedIds).toContain("decide_approval");
|
|
expect(granted.runtime.authorizationRecords().some(
|
|
(record) => record.operationId === "search_tasks" && record.reason === "operation_denied_by_policy",
|
|
)).toBe(true);
|
|
});
|
|
|
|
it("keeps always tools active-task scoped and returns typed denials without protected state", async () => {
|
|
const protectedValue = "protected-approval-payload";
|
|
const approval: CapabilityFixtureApproval = {
|
|
id: "approval-protected",
|
|
companyId: "company-1",
|
|
taskIds: ["task-1"],
|
|
type: "request_board_approval",
|
|
status: "pending",
|
|
requestedByActorId: null,
|
|
payload: { private: protectedValue },
|
|
decisionNote: null,
|
|
comments: [],
|
|
createdAt: "2026-08-09T00:00:00.000Z",
|
|
decidedAt: null,
|
|
};
|
|
const { adapter, runtime } = await runtimeFor({ seed: { approvals: [approval] } });
|
|
const revision = adapter.snapshot().revision;
|
|
const scoped = await runtime.invoke({
|
|
operationId: "get_task_context",
|
|
input: { taskId: "task-other" },
|
|
});
|
|
expect(scoped).toMatchObject({ ok: false, error: { code: "input_invalid" } });
|
|
expect(adapter.snapshot().revision).toBe(revision);
|
|
|
|
const denied = await runtime.invoke({
|
|
operationId: "decide_approval",
|
|
input: { approvalId: "approval-protected", decision: "approved", note: "approve" },
|
|
idempotencyKey: "decision-1",
|
|
});
|
|
expect(denied).toMatchObject({ ok: false, error: { code: "policy_denied" } });
|
|
expect(JSON.stringify(denied)).not.toContain(protectedValue);
|
|
|
|
const absent = await runtime.invoke({ operationId: "checkout_task", input: {} });
|
|
expect(absent).toMatchObject({
|
|
ok: false,
|
|
error: { code: "operation_absent", reason: "control_plane_owned_operation" },
|
|
});
|
|
expect(JSON.stringify(absent)).not.toContain("Protected task details");
|
|
});
|
|
|
|
it("emits authorization records with decision reasons and resulting state changes", async () => {
|
|
const { adapter, runtime } = await runtimeFor();
|
|
const beforeRevision = adapter.snapshot().revision;
|
|
const result = await runtime.invoke({
|
|
operationId: "report_progress",
|
|
input: { body: "Semantic progress." },
|
|
idempotencyKey: "progress-1",
|
|
});
|
|
expect(result.ok).toBe(true);
|
|
if (!result.ok) throw new Error("expected success");
|
|
expect(result.authorization).toMatchObject({
|
|
outcome: "allowed",
|
|
reason: "task_scoped_always_tool",
|
|
resultingStateChange: {
|
|
beforeRevision,
|
|
afterRevision: adapter.snapshot().revision,
|
|
},
|
|
});
|
|
expect(result.authorization.resultingStateChange?.entityRefs).toEqual(
|
|
expect.arrayContaining(["task:task-1"]),
|
|
);
|
|
expect(adapter.snapshot().comments.at(-1)?.body).toBe("Semantic progress.");
|
|
expect(validateJsonSchema(
|
|
capabilitySemanticTool("report_progress")!.outputSchema,
|
|
structuredClone(result) as unknown as CapabilityJsonValue,
|
|
)).toEqual([]);
|
|
});
|
|
|
|
it("executes the minimum always-tool workflow through the mock command contract", async () => {
|
|
const { adapter, runtime } = await runtimeFor();
|
|
const written = await runtime.invoke({
|
|
operationId: "write_document",
|
|
input: {
|
|
key: "plan",
|
|
title: "Plan",
|
|
body: "# Semantic plan",
|
|
baseRevisionId: null,
|
|
},
|
|
idempotencyKey: "write-plan-1",
|
|
});
|
|
expect(written.ok).toBe(true);
|
|
const revisionId = written.ok
|
|
? written.commandResult?.entityRefs.find((ref) => ref.startsWith("revision:"))?.slice(9)
|
|
: undefined;
|
|
expect(revisionId).toBeTruthy();
|
|
|
|
const read = await runtime.invoke({ operationId: "read_document", input: { key: "plan" } });
|
|
expect(read).toMatchObject({ ok: true, value: { key: "plan", latestRevisionId: revisionId } });
|
|
const revisions = await runtime.invoke({
|
|
operationId: "list_document_revisions",
|
|
input: { key: "plan" },
|
|
});
|
|
expect(revisions).toMatchObject({ ok: true, value: [{ id: revisionId, revision: 1 }] });
|
|
|
|
expect(await runtime.invoke({
|
|
operationId: "register_deliverable",
|
|
input: {
|
|
filename: "report.json",
|
|
contentType: "application/json",
|
|
byteSize: 42,
|
|
sha256: "fixture-sha256",
|
|
contentRef: "fixture://report.json",
|
|
title: "Semantic report",
|
|
},
|
|
idempotencyKey: "deliverable-1",
|
|
})).toMatchObject({ ok: true });
|
|
expect(await runtime.invoke({
|
|
operationId: "request_review",
|
|
input: { summary: "Ready for semantic review." },
|
|
idempotencyKey: "review-1",
|
|
})).toMatchObject({ ok: true });
|
|
expect(await runtime.invoke({
|
|
operationId: "request_human_input",
|
|
input: {
|
|
interactionKind: "confirmation",
|
|
title: "Confirm plan",
|
|
prompt: "Accept this plan?",
|
|
targetRevisionId: revisionId!,
|
|
continuationPolicy: "wake_assignee",
|
|
},
|
|
idempotencyKey: "confirm-plan-1",
|
|
})).toMatchObject({ ok: true });
|
|
|
|
expect(adapter.snapshot()).toMatchObject({
|
|
tasks: [{ status: "in_review" }],
|
|
documents: [{ key: "plan", revisions: [{ body: "# Semantic plan" }] }],
|
|
interactions: [{ kind: "confirmation", status: "pending" }],
|
|
artifacts: [{ filename: "report.json" }],
|
|
workProducts: [{ type: "artifact" }],
|
|
});
|
|
});
|
|
|
|
it("keeps a pending human-input handoff in review instead of weakening the lifecycle", async () => {
|
|
const { adapter, runtime } = await runtimeFor();
|
|
expect(await runtime.invoke({
|
|
operationId: "request_human_input",
|
|
input: {
|
|
interactionKind: "confirmation",
|
|
title: "Confirm plan",
|
|
prompt: "Accept this plan?",
|
|
continuationPolicy: "wake_assignee",
|
|
},
|
|
idempotencyKey: "confirm-plan-first",
|
|
})).toMatchObject({ ok: true });
|
|
|
|
expect(await runtime.invoke({
|
|
operationId: "request_review",
|
|
input: { summary: "A duplicate review transition must fail closed." },
|
|
idempotencyKey: "duplicate-review",
|
|
})).toMatchObject({
|
|
ok: false,
|
|
error: { code: "operation_unsupported", reason: "mock_operation_rejected" },
|
|
});
|
|
expect(adapter.snapshot()).toMatchObject({
|
|
tasks: [{ status: "in_review" }],
|
|
interactions: [{ status: "pending" }],
|
|
comments: [],
|
|
});
|
|
});
|
|
|
|
it.each([
|
|
["sync_company_skills", "company_skills:write", {}, { synced: true }, "engineer"],
|
|
["manage_routine", "routines:write", {}, { managed: true }, "engineer"],
|
|
["upsert_case", "cases:write", { key: "case-1", body: "Case body" }, {
|
|
key: "case-1",
|
|
body: "Case body",
|
|
upserted: true,
|
|
}, "engineer"],
|
|
["administer_company", "company:admin", { action: "refresh" }, {
|
|
action: "refresh",
|
|
applied: true,
|
|
}, "admin"],
|
|
] as const)(
|
|
"executes the advertised %s scenario extension",
|
|
async (operationId, grant, input, value, role) => {
|
|
const { runtime } = await runtimeFor({ scenarioGrants: [grant], role });
|
|
await expect(runtime.invoke({
|
|
operationId,
|
|
input,
|
|
idempotencyKey: `extension-${operationId}`,
|
|
})).resolves.toMatchObject({ ok: true, operationId, value });
|
|
},
|
|
);
|
|
|
|
it("replays required-idempotency extensions without executing a second result", async () => {
|
|
const { adapter, runtime } = await runtimeFor({ scenarioGrants: ["cases:write"] });
|
|
const invocation = {
|
|
operationId: "upsert_case",
|
|
input: { key: "case-1", body: "Case body" },
|
|
idempotencyKey: "upsert-case-1",
|
|
} as const;
|
|
|
|
const first = await runtime.invoke(invocation);
|
|
const restoredAdapter = CapabilityMockControlPlaneAdapter.restore(
|
|
adapter.serialize(),
|
|
);
|
|
const recreated = new CapabilitySemanticToolRuntime({
|
|
adapter: restoredAdapter,
|
|
runId: OPEN.identity.runId,
|
|
scenarioGrants: ["cases:write"],
|
|
});
|
|
const replay = await recreated.invoke(invocation);
|
|
expect(first).toMatchObject({ ok: true, value: { upserted: true } });
|
|
expect(replay).toMatchObject({ ok: true, value: { upserted: true } });
|
|
if (!first.ok || !replay.ok) throw new Error("expected extension success");
|
|
expect(replay.operationResultId).toBe(first.operationResultId);
|
|
|
|
await expect(recreated.invoke({
|
|
operationId: "inspect_operation_result",
|
|
input: { operationResultId: first.operationResultId },
|
|
})).resolves.toMatchObject({
|
|
ok: true,
|
|
value: { key: "case-1", body: "Case body", upserted: true },
|
|
});
|
|
|
|
const next = await recreated.invoke({
|
|
...invocation,
|
|
idempotencyKey: "upsert-case-2",
|
|
});
|
|
expect(next).toMatchObject({ ok: true });
|
|
if (!next.ok) throw new Error("expected extension success");
|
|
expect(next.operationResultId).not.toBe(first.operationResultId);
|
|
|
|
await expect(recreated.invoke({
|
|
...invocation,
|
|
input: { key: "case-1", body: "Different body" },
|
|
})).resolves.toMatchObject({
|
|
ok: false,
|
|
error: { code: "input_invalid", reason: "idempotency_key_conflict" },
|
|
});
|
|
});
|
|
|
|
it("persists extension completion with its inspectable result atomically", async () => {
|
|
const { adapter, runtime } = await runtimeFor({
|
|
scenarioGrants: ["cases:write"],
|
|
});
|
|
const compareAndSwapRuntime = vi.spyOn(
|
|
adapter,
|
|
"compareAndSwapSemanticToolRuntime",
|
|
);
|
|
|
|
const result = await runtime.invoke({
|
|
operationId: "upsert_case",
|
|
input: { key: "case-atomic", body: "Case body" },
|
|
idempotencyKey: "upsert-case-atomic",
|
|
});
|
|
|
|
expect(result).toMatchObject({ ok: true });
|
|
expect(compareAndSwapRuntime).toHaveBeenCalledTimes(3);
|
|
expect(compareAndSwapRuntime.mock.calls[0]![2].extensions).toEqual([
|
|
expect.objectContaining({ status: "pending", phase: "reserved" }),
|
|
]);
|
|
expect(compareAndSwapRuntime.mock.calls[1]![2].extensions).toEqual([
|
|
expect.objectContaining({ status: "pending", phase: "executing" }),
|
|
]);
|
|
const snapshot = compareAndSwapRuntime.mock.calls[2]![2];
|
|
expect(snapshot.extensions).toHaveLength(1);
|
|
const [extension] = snapshot.extensions;
|
|
expect(extension).toMatchObject({ status: "completed" });
|
|
if (extension?.status !== "completed") {
|
|
throw new Error("expected completed extension receipt");
|
|
}
|
|
expect(snapshot.operationResults[extension.resultId]).toEqual(
|
|
extension.execution.value,
|
|
);
|
|
});
|
|
|
|
it("retries the completion merge after a concurrent lease heartbeat", async () => {
|
|
let durableSnapshot = null as CapabilitySemanticToolRuntimeSnapshot | null;
|
|
let injectedHeartbeats = 0;
|
|
let completionAttempts = 0;
|
|
const durableStore: CapabilitySemanticToolRuntimeStore = {
|
|
load: () => durableSnapshot === null ? null : structuredClone(durableSnapshot),
|
|
save: (_runId, snapshot) => {
|
|
durableSnapshot = structuredClone(snapshot);
|
|
},
|
|
compareAndSwap: (_runId, expected, snapshot) => {
|
|
if (JSON.stringify(durableSnapshot) !== JSON.stringify(expected)) return false;
|
|
const completing = snapshot.extensions.some((extension) => extension.status === "completed");
|
|
if (completing) completionAttempts += 1;
|
|
if (completing && injectedHeartbeats < 12 && durableSnapshot !== null) {
|
|
injectedHeartbeats += 1;
|
|
const heartbeat = structuredClone(durableSnapshot) as CapabilitySemanticToolRuntimeSnapshot;
|
|
heartbeat.extensions = heartbeat.extensions.map((extension) =>
|
|
extension.status === "pending"
|
|
? { ...extension, leaseExpiresAtMs: (extension.leaseExpiresAtMs ?? 0) + 1_000 }
|
|
: extension,
|
|
);
|
|
durableSnapshot = heartbeat;
|
|
return false;
|
|
}
|
|
durableSnapshot = structuredClone(snapshot);
|
|
return true;
|
|
},
|
|
};
|
|
const { runtime } = await runtimeFor({
|
|
scenarioGrants: ["cases:write"],
|
|
semanticToolRuntimeStore: durableStore,
|
|
});
|
|
|
|
await expect(runtime.invoke({
|
|
operationId: "upsert_case",
|
|
input: { key: "case-raced", body: "Case body" },
|
|
idempotencyKey: "upsert-case-raced",
|
|
})).resolves.toMatchObject({ ok: true, value: { upserted: true } });
|
|
|
|
expect(injectedHeartbeats).toBe(12);
|
|
expect(completionAttempts).toBe(13);
|
|
expect(durableSnapshot?.extensions).toEqual([
|
|
expect.objectContaining({ status: "completed", resultId: "tool-result-1" }),
|
|
]);
|
|
expect(durableSnapshot?.operationResults["tool-result-1"]).toEqual({
|
|
key: "case-raced",
|
|
body: "Case body",
|
|
upserted: true,
|
|
});
|
|
});
|
|
|
|
it("bounds completion contention and retries the fulfilled execution without rerunning it", async () => {
|
|
vi.useFakeTimers({ now: 0 });
|
|
try {
|
|
let durableSnapshot = null as CapabilitySemanticToolRuntimeSnapshot | null;
|
|
let rejectCompletion = true;
|
|
let completionAttempts = 0;
|
|
const durableStore: CapabilitySemanticToolRuntimeStore = {
|
|
load: () => durableSnapshot === null ? null : structuredClone(durableSnapshot),
|
|
save: (_runId, snapshot) => {
|
|
durableSnapshot = structuredClone(snapshot);
|
|
},
|
|
compareAndSwap: (_runId, expected, snapshot) => {
|
|
if (JSON.stringify(durableSnapshot) !== JSON.stringify(expected)) return false;
|
|
const completing = snapshot.extensions.some(
|
|
(extension) => extension.status === "completed",
|
|
);
|
|
if (completing) completionAttempts += 1;
|
|
if (completing && rejectCompletion) return false;
|
|
durableSnapshot = structuredClone(snapshot);
|
|
return true;
|
|
},
|
|
};
|
|
const { adapter, runtime } = await runtimeFor({
|
|
scenarioGrants: ["cases:write"],
|
|
semanticToolRuntimeStore: durableStore,
|
|
now: Date.now,
|
|
});
|
|
const invocation = {
|
|
operationId: "upsert_case",
|
|
input: { key: "case-bounded-contention", body: "Case body" },
|
|
idempotencyKey: "upsert-case-bounded-contention",
|
|
} as const;
|
|
|
|
const contended = runtime.invoke(invocation);
|
|
for (let yieldIndex = 0; yieldIndex < 8; yieldIndex += 1) {
|
|
await vi.advanceTimersToNextTimerAsync();
|
|
}
|
|
await expect(contended).resolves.toMatchObject({
|
|
ok: false,
|
|
error: { code: "operation_unsupported" },
|
|
});
|
|
expect(completionAttempts).toBe(64);
|
|
expect(durableSnapshot?.extensions).toEqual([
|
|
expect.objectContaining({ status: "pending" }),
|
|
]);
|
|
|
|
await vi.advanceTimersByTimeAsync(30_000);
|
|
const pending = durableSnapshot?.extensions[0];
|
|
expect(pending?.status).toBe("pending");
|
|
if (pending?.status !== "pending") throw new Error("expected a pending extension");
|
|
expect(pending.leaseExpiresAtMs).toBeGreaterThan(Date.now());
|
|
|
|
const followerAdapter = CapabilityMockControlPlaneAdapter.restore(
|
|
adapter.serialize(),
|
|
{ semanticToolRuntimeStore: durableStore },
|
|
);
|
|
const follower = new CapabilitySemanticToolRuntime({
|
|
adapter: followerAdapter,
|
|
runId: OPEN.identity.runId,
|
|
scenarioGrants: ["cases:write"],
|
|
now: Date.now,
|
|
});
|
|
await expect(follower.invoke(invocation)).resolves.toMatchObject({
|
|
ok: false,
|
|
error: { reason: "idempotency_recovery_in_flight" },
|
|
});
|
|
|
|
rejectCompletion = false;
|
|
await expect(runtime.invoke(invocation)).resolves.toMatchObject({
|
|
ok: true,
|
|
operationResultId: "tool-result-1",
|
|
value: { key: "case-bounded-contention", upserted: true },
|
|
});
|
|
expect(completionAttempts).toBe(65);
|
|
} finally {
|
|
vi.useRealTimers();
|
|
}
|
|
});
|
|
|
|
it("reclaims an expired executing marker after its owner terminates", async () => {
|
|
vi.useFakeTimers({ now: 0 });
|
|
try {
|
|
let now = 0;
|
|
let durableSnapshot = null as CapabilitySemanticToolRuntimeSnapshot | null;
|
|
let rejectCompletion = true;
|
|
const durableStore: CapabilitySemanticToolRuntimeStore = {
|
|
load: () => durableSnapshot === null ? null : structuredClone(durableSnapshot),
|
|
save: (_runId, snapshot) => {
|
|
durableSnapshot = structuredClone(snapshot);
|
|
},
|
|
compareAndSwap: (_runId, expected, snapshot) => {
|
|
if (JSON.stringify(durableSnapshot) !== JSON.stringify(expected)) return false;
|
|
if (rejectCompletion &&
|
|
snapshot.extensions.some((extension) => extension.status === "completed")) {
|
|
return false;
|
|
}
|
|
durableSnapshot = structuredClone(snapshot);
|
|
return true;
|
|
},
|
|
};
|
|
const first = await runtimeFor({
|
|
scenarioGrants: ["cases:write"],
|
|
semanticToolRuntimeStore: durableStore,
|
|
now: () => now,
|
|
});
|
|
const invocation = {
|
|
operationId: "upsert_case",
|
|
input: { key: "case-ambiguous-effect", body: "Case body" },
|
|
idempotencyKey: "upsert-case-ambiguous-effect",
|
|
} as const;
|
|
|
|
const contended = first.runtime.invoke(invocation);
|
|
for (let yieldIndex = 0; yieldIndex < 8; yieldIndex += 1) {
|
|
await vi.advanceTimersToNextTimerAsync();
|
|
}
|
|
await expect(contended).resolves.toMatchObject({
|
|
ok: false,
|
|
error: { code: "operation_unsupported" },
|
|
});
|
|
expect(durableSnapshot?.extensions).toEqual([
|
|
expect.objectContaining({ status: "pending", phase: "executing" }),
|
|
]);
|
|
|
|
// Model termination of the first process: its heartbeat no longer owns
|
|
// the lease, and the durable store is available to the replacement.
|
|
vi.clearAllTimers();
|
|
rejectCompletion = false;
|
|
now = 60_000;
|
|
const followerAdapter = CapabilityMockControlPlaneAdapter.restore(
|
|
first.adapter.serialize(),
|
|
{ semanticToolRuntimeStore: durableStore },
|
|
);
|
|
const follower = new CapabilitySemanticToolRuntime({
|
|
adapter: followerAdapter,
|
|
runId: OPEN.identity.runId,
|
|
scenarioGrants: ["cases:write"],
|
|
now: () => now,
|
|
resolveExpiredExtensionReceipt: async () => ({
|
|
value: {
|
|
key: "case-ambiguous-effect",
|
|
body: "Case body",
|
|
upserted: true,
|
|
},
|
|
}),
|
|
});
|
|
await expect(follower.invoke(invocation)).resolves.toMatchObject({
|
|
ok: true,
|
|
operationResultId: "tool-result-1",
|
|
value: { key: "case-ambiguous-effect", upserted: true },
|
|
});
|
|
} finally {
|
|
vi.useRealTimers();
|
|
}
|
|
});
|
|
|
|
it("retries rejected-execution cleanup through snapshot contention", async () => {
|
|
let durableSnapshot: CapabilitySemanticToolRuntimeSnapshot | null = null;
|
|
let rejectExecutionAllocation = true;
|
|
let injectedCleanupContention = false;
|
|
const durableStore: CapabilitySemanticToolRuntimeStore = {
|
|
load: () => {
|
|
if (
|
|
rejectExecutionAllocation &&
|
|
durableSnapshot?.extensions.some((extension) => extension.status === "pending")
|
|
) {
|
|
rejectExecutionAllocation = false;
|
|
throw new Error("injected extension result allocation failure");
|
|
}
|
|
return durableSnapshot === null ? null : structuredClone(durableSnapshot);
|
|
},
|
|
save: (_runId, snapshot) => {
|
|
durableSnapshot = structuredClone(snapshot);
|
|
},
|
|
compareAndSwap: (_runId, expected, snapshot) => {
|
|
if (JSON.stringify(durableSnapshot) !== JSON.stringify(expected)) return false;
|
|
const removingFailedLease =
|
|
durableSnapshot?.extensions.some((extension) => extension.status === "pending") &&
|
|
snapshot.extensions.length === 0;
|
|
if (removingFailedLease && !injectedCleanupContention && durableSnapshot !== null) {
|
|
injectedCleanupContention = true;
|
|
durableSnapshot = {
|
|
...structuredClone(durableSnapshot),
|
|
resultSequence: durableSnapshot.resultSequence + 1,
|
|
};
|
|
return false;
|
|
}
|
|
durableSnapshot = structuredClone(snapshot);
|
|
return true;
|
|
},
|
|
};
|
|
const { runtime } = await runtimeFor({
|
|
scenarioGrants: ["cases:write"],
|
|
semanticToolRuntimeStore: durableStore,
|
|
});
|
|
const invocation = {
|
|
operationId: "upsert_case",
|
|
input: { key: "case-cleanup-raced", body: "Case body" },
|
|
idempotencyKey: "upsert-case-cleanup-raced",
|
|
} as const;
|
|
|
|
await expect(runtime.invoke(invocation)).resolves.toMatchObject({ ok: false });
|
|
await vi.waitFor(() => expect(durableSnapshot?.extensions).toEqual([]));
|
|
expect(injectedCleanupContention).toBe(true);
|
|
await expect(runtime.invoke(invocation)).resolves.toMatchObject({
|
|
ok: true,
|
|
operationResultId: "tool-result-2",
|
|
value: { key: "case-cleanup-raced", upserted: true },
|
|
});
|
|
});
|
|
|
|
it("retains a failed lease until cleanup contention clears", async () => {
|
|
vi.useFakeTimers({ now: 0 });
|
|
try {
|
|
let durableSnapshot = null as CapabilitySemanticToolRuntimeSnapshot | null;
|
|
let failExecutionAllocation = true;
|
|
let cleanupAttempts = 0;
|
|
const durableStore: CapabilitySemanticToolRuntimeStore = {
|
|
load: () => {
|
|
if (
|
|
failExecutionAllocation &&
|
|
durableSnapshot?.extensions.some((extension) => extension.status === "pending")
|
|
) {
|
|
failExecutionAllocation = false;
|
|
throw new Error("injected result allocation failure");
|
|
}
|
|
return durableSnapshot === null ? null : structuredClone(durableSnapshot);
|
|
},
|
|
save: (_runId, snapshot) => {
|
|
durableSnapshot = structuredClone(snapshot);
|
|
},
|
|
compareAndSwap: (_runId, expected, snapshot) => {
|
|
if (JSON.stringify(durableSnapshot) !== JSON.stringify(expected)) return false;
|
|
const removingFailedLease =
|
|
durableSnapshot?.extensions.some((extension) => extension.status === "pending") &&
|
|
snapshot.extensions.length === 0;
|
|
if (removingFailedLease) {
|
|
cleanupAttempts += 1;
|
|
if (cleanupAttempts <= 8) return false;
|
|
}
|
|
durableSnapshot = structuredClone(snapshot);
|
|
return true;
|
|
},
|
|
};
|
|
const { runtime } = await runtimeFor({
|
|
scenarioGrants: ["cases:write"],
|
|
semanticToolRuntimeStore: durableStore,
|
|
now: Date.now,
|
|
});
|
|
const invocation = {
|
|
operationId: "upsert_case",
|
|
input: { key: "case-cleanup-exhausted", body: "Case body" },
|
|
idempotencyKey: "upsert-case-cleanup-exhausted",
|
|
} as const;
|
|
|
|
await expect(runtime.invoke(invocation)).resolves.toMatchObject({ ok: false });
|
|
expect(cleanupAttempts).toBe(8);
|
|
expect(durableSnapshot?.extensions).toEqual([
|
|
expect.objectContaining({ status: "pending" }),
|
|
]);
|
|
|
|
await vi.advanceTimersByTimeAsync(1_000);
|
|
expect(cleanupAttempts).toBe(9);
|
|
expect(durableSnapshot?.extensions).toEqual([]);
|
|
await expect(runtime.invoke(invocation)).resolves.toMatchObject({
|
|
ok: true,
|
|
value: { key: "case-cleanup-exhausted", upserted: true },
|
|
});
|
|
} finally {
|
|
vi.useRealTimers();
|
|
}
|
|
});
|
|
|
|
it("allocates read result ids from the latest shared durable sequence", async () => {
|
|
let durableSnapshot = null as CapabilitySemanticToolRuntimeSnapshot | null;
|
|
const durableStore: CapabilitySemanticToolRuntimeStore = {
|
|
load: () => durableSnapshot === null ? null : structuredClone(durableSnapshot),
|
|
save: (_runId, snapshot) => {
|
|
durableSnapshot = structuredClone(snapshot);
|
|
},
|
|
compareAndSwap: (_runId, expected, snapshot) => {
|
|
if (JSON.stringify(durableSnapshot) !== JSON.stringify(expected)) return false;
|
|
durableSnapshot = structuredClone(snapshot);
|
|
return true;
|
|
},
|
|
};
|
|
const first = await runtimeFor({ semanticToolRuntimeStore: durableStore });
|
|
await expect(first.runtime.invoke({ operationId: "get_task_context", input: {} }))
|
|
.resolves.toMatchObject({ ok: true, operationResultId: "tool-result-1" });
|
|
const followerAdapter = CapabilityMockControlPlaneAdapter.restore(
|
|
first.adapter.serialize(),
|
|
{ semanticToolRuntimeStore: durableStore },
|
|
);
|
|
const follower = new CapabilitySemanticToolRuntime({
|
|
adapter: followerAdapter,
|
|
runId: OPEN.identity.runId,
|
|
});
|
|
|
|
await expect(follower.invoke({ operationId: "get_task_context", input: {} }))
|
|
.resolves.toMatchObject({ ok: true, operationResultId: "tool-result-2" });
|
|
await expect(first.runtime.invoke({ operationId: "get_task_context", input: {} }))
|
|
.resolves.toMatchObject({ ok: true, operationResultId: "tool-result-3" });
|
|
expect(Object.keys(durableSnapshot?.operationResults ?? {})).toEqual([
|
|
"tool-result-1",
|
|
"tool-result-2",
|
|
"tool-result-3",
|
|
]);
|
|
});
|
|
|
|
it("retries extension acquisition after an unrelated snapshot update", async () => {
|
|
let durableSnapshot = null as CapabilitySemanticToolRuntimeSnapshot | null;
|
|
let injectedContention = false;
|
|
const durableStore: CapabilitySemanticToolRuntimeStore = {
|
|
load: () => durableSnapshot === null ? null : structuredClone(durableSnapshot),
|
|
save: (_runId, snapshot) => {
|
|
durableSnapshot = structuredClone(snapshot);
|
|
},
|
|
compareAndSwap: (_runId, expected, snapshot) => {
|
|
if (JSON.stringify(durableSnapshot) !== JSON.stringify(expected)) return false;
|
|
const acquiring = snapshot.extensions.some((extension) => extension.status === "pending");
|
|
if (acquiring && !injectedContention) {
|
|
injectedContention = true;
|
|
durableSnapshot = {
|
|
schema: "paperclip.capability.semantic-tool-runtime.v1",
|
|
resultSequence: 1,
|
|
operationResults: {},
|
|
extensions: [],
|
|
};
|
|
return false;
|
|
}
|
|
durableSnapshot = structuredClone(snapshot);
|
|
return true;
|
|
},
|
|
};
|
|
const { runtime } = await runtimeFor({
|
|
scenarioGrants: ["cases:write"],
|
|
semanticToolRuntimeStore: durableStore,
|
|
});
|
|
|
|
await expect(runtime.invoke({
|
|
operationId: "upsert_case",
|
|
input: { key: "case-acquire-raced", body: "Case body" },
|
|
idempotencyKey: "upsert-case-acquire-raced",
|
|
})).resolves.toMatchObject({ ok: true, value: { upserted: true } });
|
|
expect(injectedContention).toBe(true);
|
|
expect(durableSnapshot?.resultSequence).toBe(2);
|
|
expect(durableSnapshot?.extensions).toEqual([
|
|
expect.objectContaining({ status: "completed" }),
|
|
]);
|
|
});
|
|
|
|
it("fails closed while a restored extension is in flight and reconciles its durable receipt", async () => {
|
|
let durableSnapshot: CapabilitySemanticToolRuntimeSnapshot | null = null;
|
|
const durableStore: CapabilitySemanticToolRuntimeStore = {
|
|
load: () =>
|
|
durableSnapshot === null ? null : structuredClone(durableSnapshot),
|
|
save: (_runId, snapshot) => {
|
|
durableSnapshot = structuredClone(snapshot);
|
|
},
|
|
compareAndSwap: (_runId, expected, snapshot) => {
|
|
if (JSON.stringify(durableSnapshot) !== JSON.stringify(expected)) {
|
|
return false;
|
|
}
|
|
durableSnapshot = structuredClone(snapshot);
|
|
return true;
|
|
},
|
|
};
|
|
const { adapter, runtime } = await runtimeFor({
|
|
scenarioGrants: ["cases:write"],
|
|
semanticToolRuntimeStore: durableStore,
|
|
});
|
|
const invocation = {
|
|
operationId: "upsert_case",
|
|
input: { key: "case-pending", body: "Case body" },
|
|
idempotencyKey: "upsert-case-pending",
|
|
} as const;
|
|
|
|
const inFlight = runtime.invoke(invocation);
|
|
const serializedWhileInFlight = adapter.serialize();
|
|
expect(() =>
|
|
CapabilityMockControlPlaneAdapter.restore(serializedWhileInFlight),
|
|
).toThrow(
|
|
"restoring pending semantic extensions requires a process-independent runtime store",
|
|
);
|
|
const restoredAdapter = CapabilityMockControlPlaneAdapter.restore(
|
|
serializedWhileInFlight,
|
|
{ semanticToolRuntimeStore: durableStore },
|
|
);
|
|
const restored = new CapabilitySemanticToolRuntime({
|
|
adapter: restoredAdapter,
|
|
runId: OPEN.identity.runId,
|
|
scenarioGrants: ["cases:write"],
|
|
});
|
|
|
|
await expect(restored.invoke(invocation)).resolves.toMatchObject({
|
|
ok: false,
|
|
error: {
|
|
code: "operation_unsupported",
|
|
reason: "idempotency_recovery_in_flight",
|
|
},
|
|
});
|
|
const completed = await inFlight;
|
|
expect(completed).toMatchObject({ ok: true });
|
|
|
|
const recovered = await restored.invoke(invocation);
|
|
expect(recovered).toMatchObject({
|
|
ok: true,
|
|
operationResultId: completed.ok ? completed.operationResultId : undefined,
|
|
value: { key: "case-pending", body: "Case body", upserted: true },
|
|
});
|
|
});
|
|
|
|
it("does not revive a serialized pending extension when the durable store is empty", async () => {
|
|
let durableSnapshot = null as CapabilitySemanticToolRuntimeSnapshot | null;
|
|
const durableStore: CapabilitySemanticToolRuntimeStore = {
|
|
load: () => durableSnapshot === null ? null : structuredClone(durableSnapshot),
|
|
save: (_runId, snapshot) => {
|
|
durableSnapshot = structuredClone(snapshot);
|
|
},
|
|
compareAndSwap: (_runId, expected, snapshot) => {
|
|
if (JSON.stringify(durableSnapshot) !== JSON.stringify(expected)) return false;
|
|
durableSnapshot = structuredClone(snapshot);
|
|
return true;
|
|
},
|
|
};
|
|
const first = await runtimeFor({
|
|
scenarioGrants: ["cases:write"],
|
|
semanticToolRuntimeStore: durableStore,
|
|
});
|
|
const invocation = {
|
|
operationId: "upsert_case",
|
|
input: { key: "case-empty-store-pending", body: "Case body" },
|
|
idempotencyKey: "empty-store-pending",
|
|
} as const;
|
|
|
|
const inFlight = first.runtime.invoke(invocation);
|
|
const serializedWhilePending = first.adapter.serialize();
|
|
await expect(inFlight).resolves.toMatchObject({ ok: true });
|
|
durableSnapshot = null as CapabilitySemanticToolRuntimeSnapshot | null;
|
|
|
|
const restoredAdapter = CapabilityMockControlPlaneAdapter.restore(
|
|
serializedWhilePending,
|
|
{ semanticToolRuntimeStore: durableStore },
|
|
);
|
|
const restored = new CapabilitySemanticToolRuntime({
|
|
adapter: restoredAdapter,
|
|
runId: OPEN.identity.runId,
|
|
scenarioGrants: ["cases:write"],
|
|
});
|
|
await expect(restored.invoke(invocation)).resolves.toMatchObject({
|
|
ok: true,
|
|
value: { key: "case-empty-store-pending", upserted: true },
|
|
});
|
|
expect(durableSnapshot?.extensions).toEqual([
|
|
expect.objectContaining({
|
|
key: "run-tools:upsert_case:empty-store-pending",
|
|
status: "completed",
|
|
}),
|
|
]);
|
|
});
|
|
|
|
it("does not revive serialized completed extensions when the durable store is empty", async () => {
|
|
let durableSnapshot = null as CapabilitySemanticToolRuntimeSnapshot | null;
|
|
const durableStore: CapabilitySemanticToolRuntimeStore = {
|
|
load: () => durableSnapshot === null ? null : structuredClone(durableSnapshot),
|
|
save: (_runId, snapshot) => {
|
|
durableSnapshot = structuredClone(snapshot);
|
|
},
|
|
compareAndSwap: (_runId, expected, snapshot) => {
|
|
if (JSON.stringify(durableSnapshot) !== JSON.stringify(expected)) return false;
|
|
durableSnapshot = structuredClone(snapshot);
|
|
return true;
|
|
},
|
|
};
|
|
const first = await runtimeFor({
|
|
scenarioGrants: ["cases:write"],
|
|
semanticToolRuntimeStore: durableStore,
|
|
});
|
|
await expect(first.runtime.invoke({
|
|
operationId: "upsert_case",
|
|
input: { key: "case-stale-completed", body: "Stale body" },
|
|
idempotencyKey: "stale-completed",
|
|
})).resolves.toMatchObject({ ok: true });
|
|
const serializedWithCompleted = first.adapter.serialize();
|
|
durableSnapshot = null as CapabilitySemanticToolRuntimeSnapshot | null;
|
|
|
|
const restoredAdapter = CapabilityMockControlPlaneAdapter.restore(
|
|
serializedWithCompleted,
|
|
{ semanticToolRuntimeStore: durableStore },
|
|
);
|
|
const restored = new CapabilitySemanticToolRuntime({
|
|
adapter: restoredAdapter,
|
|
runId: OPEN.identity.runId,
|
|
scenarioGrants: ["cases:write"],
|
|
});
|
|
await expect(restored.invoke({
|
|
operationId: "upsert_case",
|
|
input: { key: "case-after-empty-store", body: "Fresh body" },
|
|
idempotencyKey: "fresh-after-empty-store",
|
|
})).resolves.toMatchObject({
|
|
ok: true,
|
|
value: { key: "case-after-empty-store", upserted: true },
|
|
});
|
|
expect(durableSnapshot?.extensions).toEqual([
|
|
expect.objectContaining({
|
|
key: "run-tools:upsert_case:fresh-after-empty-store",
|
|
status: "completed",
|
|
}),
|
|
]);
|
|
});
|
|
|
|
it("renews a live extension lease before another runtime can reclaim it", async () => {
|
|
vi.useFakeTimers({ now: 0 });
|
|
try {
|
|
let durableSnapshot = null as CapabilitySemanticToolRuntimeSnapshot | null;
|
|
const durableStore: CapabilitySemanticToolRuntimeStore = {
|
|
load: () =>
|
|
durableSnapshot === null ? null : structuredClone(durableSnapshot),
|
|
save: (_runId, snapshot) => {
|
|
durableSnapshot = structuredClone(snapshot);
|
|
},
|
|
compareAndSwap: (_runId, expected, snapshot) => {
|
|
if (JSON.stringify(durableSnapshot) !== JSON.stringify(expected)) {
|
|
return false;
|
|
}
|
|
durableSnapshot = structuredClone(snapshot);
|
|
return true;
|
|
},
|
|
};
|
|
const { adapter, runtime } = await runtimeFor({
|
|
scenarioGrants: ["cases:write"],
|
|
semanticToolRuntimeStore: durableStore,
|
|
});
|
|
const invocation = {
|
|
operationId: "upsert_case",
|
|
input: { key: "case-live", body: "Case body" },
|
|
idempotencyKey: "live-case",
|
|
} as const;
|
|
|
|
const inFlight = runtime.invoke(invocation);
|
|
expect(durableSnapshot?.extensions[0]).toMatchObject({
|
|
status: "pending",
|
|
leaseExpiresAtMs: 30_000,
|
|
});
|
|
vi.advanceTimersByTime(10_000);
|
|
expect(durableSnapshot?.extensions[0]).toMatchObject({
|
|
status: "pending",
|
|
leaseExpiresAtMs: 40_000,
|
|
});
|
|
|
|
const restoredAdapter = CapabilityMockControlPlaneAdapter.restore(
|
|
adapter.serialize(),
|
|
{ semanticToolRuntimeStore: durableStore },
|
|
);
|
|
const restored = new CapabilitySemanticToolRuntime({
|
|
adapter: restoredAdapter,
|
|
runId: OPEN.identity.runId,
|
|
scenarioGrants: ["cases:write"],
|
|
now: () => 30_001,
|
|
});
|
|
await expect(restored.invoke(invocation)).resolves.toMatchObject({
|
|
ok: false,
|
|
error: {
|
|
code: "operation_unsupported",
|
|
reason: "idempotency_recovery_in_flight",
|
|
},
|
|
});
|
|
await expect(inFlight).resolves.toMatchObject({ ok: true });
|
|
} finally {
|
|
vi.useRealTimers();
|
|
}
|
|
});
|
|
|
|
it("retries a lease heartbeat after a transient durable-store failure", async () => {
|
|
vi.useFakeTimers({ now: 0 });
|
|
try {
|
|
let durableSnapshot = null as CapabilitySemanticToolRuntimeSnapshot | null;
|
|
let failNextLoad = false;
|
|
const durableStore: CapabilitySemanticToolRuntimeStore = {
|
|
load: () => {
|
|
if (failNextLoad) {
|
|
failNextLoad = false;
|
|
throw new Error("transient store read failure");
|
|
}
|
|
return durableSnapshot === null ? null : structuredClone(durableSnapshot);
|
|
},
|
|
save: (_runId, snapshot) => {
|
|
durableSnapshot = structuredClone(snapshot);
|
|
},
|
|
compareAndSwap: (_runId, expected, snapshot) => {
|
|
if (JSON.stringify(durableSnapshot) !== JSON.stringify(expected)) {
|
|
return false;
|
|
}
|
|
durableSnapshot = structuredClone(snapshot);
|
|
return true;
|
|
},
|
|
};
|
|
const { runtime } = await runtimeFor({
|
|
scenarioGrants: ["cases:write"],
|
|
semanticToolRuntimeStore: durableStore,
|
|
});
|
|
const inFlight = runtime.invoke({
|
|
operationId: "upsert_case",
|
|
input: { key: "case-heartbeat-retry", body: "Case body" },
|
|
idempotencyKey: "heartbeat-retry",
|
|
});
|
|
|
|
failNextLoad = true;
|
|
expect(() => vi.advanceTimersByTime(10_000)).not.toThrow();
|
|
expect(pendingLeaseExpiry(durableSnapshot)).toBe(30_000);
|
|
vi.advanceTimersByTime(10_000);
|
|
expect(pendingLeaseExpiry(durableSnapshot)).toBe(50_000);
|
|
await expect(inFlight).resolves.toMatchObject({ ok: true });
|
|
} finally {
|
|
vi.useRealTimers();
|
|
}
|
|
});
|
|
|
|
it("recovers a prepared extension after repeated heartbeat failures", async () => {
|
|
vi.useFakeTimers({ now: 0 });
|
|
try {
|
|
let durableSnapshot = null as CapabilitySemanticToolRuntimeSnapshot | null;
|
|
let failHeartbeatLoads = false;
|
|
const durableStore: CapabilitySemanticToolRuntimeStore = {
|
|
load: () => {
|
|
if (failHeartbeatLoads) throw new Error("persistent store read failure");
|
|
return durableSnapshot === null ? null : structuredClone(durableSnapshot);
|
|
},
|
|
save: (_runId, snapshot) => { durableSnapshot = structuredClone(snapshot); },
|
|
compareAndSwap: (_runId, expected, snapshot) => {
|
|
if (JSON.stringify(durableSnapshot) !== JSON.stringify(expected)) return false;
|
|
durableSnapshot = structuredClone(snapshot);
|
|
return true;
|
|
},
|
|
};
|
|
const { adapter, runtime } = await runtimeFor({
|
|
scenarioGrants: ["cases:write"],
|
|
semanticToolRuntimeStore: durableStore,
|
|
now: () => Date.now(),
|
|
});
|
|
const invocation = {
|
|
operationId: "upsert_case",
|
|
input: { key: "case-heartbeat-loss", body: "Case body" },
|
|
idempotencyKey: "heartbeat-loss",
|
|
} as const;
|
|
|
|
const inFlight = runtime.invoke(invocation);
|
|
failHeartbeatLoads = true;
|
|
vi.advanceTimersByTime(40_000);
|
|
failHeartbeatLoads = false;
|
|
expect(durableSnapshot?.extensions[0]).toMatchObject({
|
|
status: "pending",
|
|
phase: "executing",
|
|
leaseExpiresAtMs: 30_000,
|
|
});
|
|
|
|
const restoredAdapter = CapabilityMockControlPlaneAdapter.restore(
|
|
adapter.serialize(),
|
|
{ semanticToolRuntimeStore: durableStore },
|
|
);
|
|
const resolveExpiredExtensionReceipt = vi.fn(() => ({
|
|
value: {
|
|
key: "case-heartbeat-loss",
|
|
body: "Conflicting recovered body",
|
|
upserted: true,
|
|
},
|
|
}));
|
|
const restored = new CapabilitySemanticToolRuntime({
|
|
adapter: restoredAdapter,
|
|
runId: OPEN.identity.runId,
|
|
scenarioGrants: ["cases:write"],
|
|
now: () => Date.now(),
|
|
resolveExpiredExtensionReceipt,
|
|
});
|
|
const recovered = await restored.invoke(invocation);
|
|
const original = await inFlight;
|
|
expect(recovered).toMatchObject({ ok: true });
|
|
expect(original).toMatchObject({ ok: true });
|
|
if (!recovered.ok || !original.ok) throw new Error("expected recovered execution");
|
|
expect(recovered.operationResultId).toBe(original.operationResultId);
|
|
expect(recovered.value).toEqual({
|
|
key: "case-heartbeat-loss",
|
|
body: "Case body",
|
|
upserted: true,
|
|
});
|
|
expect(original.value).toEqual(recovered.value);
|
|
expect(resolveExpiredExtensionReceipt).not.toHaveBeenCalled();
|
|
} finally {
|
|
vi.useRealTimers();
|
|
}
|
|
});
|
|
|
|
it("retries initial acquisition after a transient durable-store failure", async () => {
|
|
let durableSnapshot: CapabilitySemanticToolRuntimeSnapshot | null = null;
|
|
let failNextLoad = false;
|
|
const durableStore: CapabilitySemanticToolRuntimeStore = {
|
|
load: () => {
|
|
if (failNextLoad) {
|
|
failNextLoad = false;
|
|
throw new Error("transient store read failure");
|
|
}
|
|
return durableSnapshot === null ? null : structuredClone(durableSnapshot);
|
|
},
|
|
save: (_runId, snapshot) => {
|
|
durableSnapshot = structuredClone(snapshot);
|
|
},
|
|
compareAndSwap: (_runId, expected, snapshot) => {
|
|
if (JSON.stringify(durableSnapshot) !== JSON.stringify(expected)) {
|
|
return false;
|
|
}
|
|
durableSnapshot = structuredClone(snapshot);
|
|
return true;
|
|
},
|
|
};
|
|
const { runtime } = await runtimeFor({
|
|
scenarioGrants: ["cases:write"],
|
|
semanticToolRuntimeStore: durableStore,
|
|
});
|
|
const invocation = {
|
|
operationId: "upsert_case",
|
|
input: { key: "case-acquisition-retry", body: "Case body" },
|
|
idempotencyKey: "acquisition-retry",
|
|
};
|
|
|
|
failNextLoad = true;
|
|
await expect(runtime.invoke(invocation)).resolves.toMatchObject({
|
|
ok: false,
|
|
error: { reason: "mock_operation_rejected" },
|
|
});
|
|
await expect(runtime.invoke(invocation)).resolves.toMatchObject({
|
|
ok: true,
|
|
value: { key: "case-acquisition-retry", upserted: true },
|
|
});
|
|
});
|
|
|
|
it("reconstructs a legacy expired built-in receipt after executor loss", async () => {
|
|
let durableSnapshot: CapabilitySemanticToolRuntimeSnapshot = {
|
|
schema: "paperclip.capability.semantic-tool-runtime.v1",
|
|
resultSequence: 0,
|
|
operationResults: {},
|
|
extensions: [{
|
|
key: `${OPEN.identity.runId}:upsert_case:expired-case`,
|
|
input: '{"body":"Case body","key":"case-expired"}',
|
|
status: "pending",
|
|
ownerId: "terminated-executor",
|
|
leaseExpiresAtMs: 1_000,
|
|
phase: "executing",
|
|
}],
|
|
};
|
|
const durableStore: CapabilitySemanticToolRuntimeStore = {
|
|
load: () => structuredClone(durableSnapshot),
|
|
save: (_runId, snapshot) => {
|
|
durableSnapshot = structuredClone(snapshot);
|
|
},
|
|
compareAndSwap: (_runId, expected, snapshot) => {
|
|
if (JSON.stringify(durableSnapshot) !== JSON.stringify(expected)) {
|
|
return false;
|
|
}
|
|
durableSnapshot = structuredClone(snapshot);
|
|
return true;
|
|
},
|
|
};
|
|
const base = await runtimeFor({ scenarioGrants: ["cases:write"] });
|
|
const restoredAdapter = CapabilityMockControlPlaneAdapter.restore(
|
|
base.adapter.serialize(),
|
|
{ semanticToolRuntimeStore: durableStore },
|
|
);
|
|
const restored = new CapabilitySemanticToolRuntime({
|
|
adapter: restoredAdapter,
|
|
runId: OPEN.identity.runId,
|
|
scenarioGrants: ["cases:write"],
|
|
now: () => 1_001,
|
|
});
|
|
|
|
await expect(restored.invoke({
|
|
operationId: "upsert_case",
|
|
input: { key: "case-expired", body: "Case body" },
|
|
idempotencyKey: "expired-case",
|
|
})).resolves.toMatchObject({
|
|
ok: true,
|
|
operationResultId: "tool-result-1",
|
|
value: { key: "case-expired", body: "Case body", upserted: true },
|
|
});
|
|
expect(durableSnapshot.extensions[0]).toMatchObject({
|
|
status: "completed",
|
|
resultId: "tool-result-1",
|
|
execution: {
|
|
value: { key: "case-expired", body: "Case body", upserted: true },
|
|
},
|
|
});
|
|
expect(durableSnapshot.operationResults).toEqual({
|
|
"tool-result-1": { key: "case-expired", body: "Case body", upserted: true },
|
|
});
|
|
});
|
|
|
|
it("recovers an expired built-in extension from its prepared receipt", async () => {
|
|
const observedExport = {
|
|
schema: "paperclip.capability.mock-export.v1",
|
|
company: { id: "company-1", name: "Previously Exported Company" },
|
|
taskCount: 1,
|
|
actorCount: 1,
|
|
};
|
|
let durableSnapshot: CapabilitySemanticToolRuntimeSnapshot = {
|
|
schema: "paperclip.capability.semantic-tool-runtime.v1",
|
|
resultSequence: 0,
|
|
operationResults: {},
|
|
extensions: [{
|
|
key: `${OPEN.identity.runId}:export_company:prepared-export`,
|
|
input: "{}",
|
|
status: "pending",
|
|
ownerId: "terminated-exporter",
|
|
leaseExpiresAtMs: 1_000,
|
|
phase: "executing",
|
|
preparedExecution: {
|
|
value: observedExport,
|
|
commandResult: null,
|
|
entityRefs: [],
|
|
},
|
|
}],
|
|
};
|
|
const durableStore: CapabilitySemanticToolRuntimeStore = {
|
|
load: () => structuredClone(durableSnapshot),
|
|
save: (_runId, snapshot) => { durableSnapshot = structuredClone(snapshot); },
|
|
compareAndSwap: (_runId, expected, snapshot) => {
|
|
if (JSON.stringify(durableSnapshot) !== JSON.stringify(expected)) return false;
|
|
durableSnapshot = structuredClone(snapshot);
|
|
return true;
|
|
},
|
|
};
|
|
const base = await runtimeFor({ scenarioGrants: ["portability:export"] });
|
|
const restoredAdapter = CapabilityMockControlPlaneAdapter.restore(
|
|
base.adapter.serialize(),
|
|
{ semanticToolRuntimeStore: durableStore },
|
|
);
|
|
const resolveExpiredExtensionReceipt = vi.fn(async () => ({
|
|
value: {
|
|
...observedExport,
|
|
company: { id: "company-1", name: "Conflicting Resolver Company" },
|
|
},
|
|
}));
|
|
const restored = new CapabilitySemanticToolRuntime({
|
|
adapter: restoredAdapter,
|
|
runId: OPEN.identity.runId,
|
|
scenarioGrants: ["portability:export"],
|
|
resolveExpiredExtensionReceipt,
|
|
now: () => 1_001,
|
|
});
|
|
|
|
await expect(restored.invoke({
|
|
operationId: "export_company",
|
|
input: {},
|
|
idempotencyKey: "prepared-export",
|
|
})).resolves.toMatchObject({
|
|
ok: true,
|
|
operationResultId: "tool-result-1",
|
|
value: observedExport,
|
|
});
|
|
expect(durableSnapshot.extensions[0]).toMatchObject({
|
|
status: "completed",
|
|
resultId: "tool-result-1",
|
|
execution: { value: observedExport },
|
|
});
|
|
expect(resolveExpiredExtensionReceipt).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("terminally marks a legacy export without an exact receipt", async () => {
|
|
let durableSnapshot: CapabilitySemanticToolRuntimeSnapshot = {
|
|
schema: "paperclip.capability.semantic-tool-runtime.v1",
|
|
resultSequence: 0,
|
|
operationResults: {},
|
|
extensions: [{
|
|
key: `${OPEN.identity.runId}:export_company:expired-export`,
|
|
input: "{}",
|
|
status: "pending",
|
|
ownerId: "terminated-exporter",
|
|
leaseExpiresAtMs: 1_000,
|
|
phase: "executing",
|
|
}],
|
|
};
|
|
const durableStore: CapabilitySemanticToolRuntimeStore = {
|
|
load: () => structuredClone(durableSnapshot),
|
|
save: (_runId, snapshot) => {
|
|
durableSnapshot = structuredClone(snapshot);
|
|
},
|
|
compareAndSwap: (_runId, expected, snapshot) => {
|
|
if (JSON.stringify(durableSnapshot) !== JSON.stringify(expected)) {
|
|
return false;
|
|
}
|
|
durableSnapshot = structuredClone(snapshot);
|
|
return true;
|
|
},
|
|
};
|
|
const base = await runtimeFor({
|
|
scenarioGrants: ["portability:export"],
|
|
seed: { company: { name: "Company State Changed After Execution" } },
|
|
});
|
|
const restoredAdapter = CapabilityMockControlPlaneAdapter.restore(
|
|
base.adapter.serialize(),
|
|
{ semanticToolRuntimeStore: durableStore },
|
|
);
|
|
const restored = new CapabilitySemanticToolRuntime({
|
|
adapter: restoredAdapter,
|
|
runId: OPEN.identity.runId,
|
|
scenarioGrants: ["portability:export"],
|
|
now: () => 1_001,
|
|
});
|
|
|
|
await expect(restored.invoke({
|
|
operationId: "export_company",
|
|
input: {},
|
|
idempotencyKey: "expired-export",
|
|
})).resolves.toMatchObject({
|
|
ok: false,
|
|
error: {
|
|
code: "operation_unsupported",
|
|
reason: "idempotency_receipt_unavailable",
|
|
},
|
|
});
|
|
expect(durableSnapshot.extensions).toEqual([{
|
|
key: `${OPEN.identity.runId}:export_company:expired-export`,
|
|
input: "{}",
|
|
status: "indeterminate",
|
|
reason: "idempotency_receipt_unavailable",
|
|
}]);
|
|
expect(durableSnapshot.operationResults).toEqual({});
|
|
|
|
const retriedAdapter = CapabilityMockControlPlaneAdapter.restore(
|
|
base.adapter.serialize(),
|
|
{ semanticToolRuntimeStore: durableStore },
|
|
);
|
|
const retried = new CapabilitySemanticToolRuntime({
|
|
adapter: retriedAdapter,
|
|
runId: OPEN.identity.runId,
|
|
scenarioGrants: ["portability:export"],
|
|
now: () => 2_001,
|
|
});
|
|
await expect(retried.invoke({
|
|
operationId: "export_company",
|
|
input: {},
|
|
idempotencyKey: "expired-export",
|
|
})).resolves.toMatchObject({
|
|
ok: false,
|
|
error: {
|
|
code: "operation_unsupported",
|
|
reason: "idempotency_receipt_unavailable",
|
|
},
|
|
});
|
|
expect(durableSnapshot.operationResults).toEqual({});
|
|
|
|
const authoritativeExport = {
|
|
schema: "paperclip.capability.mock-export.v1",
|
|
company: { id: "company-1", name: "Originally Exported Company" },
|
|
taskCount: 1,
|
|
actorCount: 1,
|
|
};
|
|
const resolvingAdapter = CapabilityMockControlPlaneAdapter.restore(
|
|
base.adapter.serialize(),
|
|
{ semanticToolRuntimeStore: durableStore },
|
|
);
|
|
const resolving = new CapabilitySemanticToolRuntime({
|
|
adapter: resolvingAdapter,
|
|
runId: OPEN.identity.runId,
|
|
scenarioGrants: ["portability:export"],
|
|
resolveExpiredExtensionReceipt: async () => ({ value: authoritativeExport }),
|
|
now: () => 3_001,
|
|
});
|
|
await expect(resolving.invoke({
|
|
operationId: "export_company",
|
|
input: {},
|
|
idempotencyKey: "expired-export",
|
|
})).resolves.toMatchObject({
|
|
ok: true,
|
|
operationResultId: "tool-result-1",
|
|
value: authoritativeExport,
|
|
});
|
|
expect(durableSnapshot.extensions[0]).toMatchObject({
|
|
status: "completed",
|
|
resultId: "tool-result-1",
|
|
});
|
|
});
|
|
|
|
it("adopts an authoritative completion that wins the recovery publication race", async () => {
|
|
const authoritative = {
|
|
key: "case-publication-race",
|
|
body: "Original executor body",
|
|
upserted: true,
|
|
};
|
|
let durableSnapshot: CapabilitySemanticToolRuntimeSnapshot = {
|
|
schema: "paperclip.capability.semantic-tool-runtime.v1",
|
|
resultSequence: 1,
|
|
operationResults: { "tool-result-1": authoritative },
|
|
extensions: [{
|
|
key: `${OPEN.identity.runId}:upsert_case:publication-race`,
|
|
input: '{"body":"Case body","key":"case-publication-race"}',
|
|
status: "completed",
|
|
resultId: "tool-result-1",
|
|
execution: {
|
|
value: authoritative,
|
|
commandResult: null,
|
|
entityRefs: ["case:case-publication-race"],
|
|
},
|
|
}],
|
|
};
|
|
const durableStore: CapabilitySemanticToolRuntimeStore = {
|
|
load: () => structuredClone(durableSnapshot),
|
|
save: (_runId, snapshot) => { durableSnapshot = structuredClone(snapshot); },
|
|
compareAndSwap: (_runId, expected, snapshot) => {
|
|
if (JSON.stringify(durableSnapshot) !== JSON.stringify(expected)) return false;
|
|
durableSnapshot = structuredClone(snapshot);
|
|
return true;
|
|
},
|
|
};
|
|
const base = await runtimeFor({ scenarioGrants: ["cases:write"] });
|
|
const restoredAdapter = CapabilityMockControlPlaneAdapter.restore(
|
|
base.adapter.serialize(),
|
|
{ semanticToolRuntimeStore: durableStore },
|
|
);
|
|
const restored = new CapabilitySemanticToolRuntime({
|
|
adapter: restoredAdapter,
|
|
runId: OPEN.identity.runId,
|
|
scenarioGrants: ["cases:write"],
|
|
});
|
|
|
|
expect(restored.reconcileExpiredExtensionReceipt({
|
|
operationId: "upsert_case",
|
|
input: { key: "case-publication-race", body: "Case body" },
|
|
idempotencyKey: "publication-race",
|
|
value: {
|
|
key: "case-publication-race",
|
|
body: "Stale recovery observation",
|
|
upserted: true,
|
|
},
|
|
entityRefs: ["case:stale-observation"],
|
|
})).toEqual({ resultId: "tool-result-1" });
|
|
await expect(restored.invoke({
|
|
operationId: "upsert_case",
|
|
input: { key: "case-publication-race", body: "Case body" },
|
|
idempotencyKey: "publication-race",
|
|
})).resolves.toMatchObject({
|
|
ok: true,
|
|
operationResultId: "tool-result-1",
|
|
value: authoritative,
|
|
});
|
|
expect(durableSnapshot.operationResults).toEqual({
|
|
"tool-result-1": authoritative,
|
|
});
|
|
});
|
|
|
|
it("resolves an expired mutable export receipt during a restored retry", async () => {
|
|
const observedExport = {
|
|
schema: "paperclip.capability.mock-export.v1",
|
|
company: { id: "company-1", name: "Previously Exported Company" },
|
|
taskCount: 1,
|
|
actorCount: 1,
|
|
};
|
|
let durableSnapshot: CapabilitySemanticToolRuntimeSnapshot = {
|
|
schema: "paperclip.capability.semantic-tool-runtime.v1",
|
|
resultSequence: 0,
|
|
operationResults: {},
|
|
extensions: [{
|
|
key: `${OPEN.identity.runId}:export_company:recovered-export`,
|
|
input: "{}",
|
|
status: "pending",
|
|
ownerId: "terminated-exporter",
|
|
leaseExpiresAtMs: 1_000,
|
|
phase: "executing",
|
|
}],
|
|
};
|
|
const durableStore: CapabilitySemanticToolRuntimeStore = {
|
|
load: () => structuredClone(durableSnapshot),
|
|
save: (_runId, snapshot) => { durableSnapshot = structuredClone(snapshot); },
|
|
compareAndSwap: (_runId, expected, snapshot) => {
|
|
if (JSON.stringify(durableSnapshot) !== JSON.stringify(expected)) return false;
|
|
durableSnapshot = structuredClone(snapshot);
|
|
return true;
|
|
},
|
|
};
|
|
const base = await runtimeFor({ scenarioGrants: ["portability:export"] });
|
|
const restoredAdapter = CapabilityMockControlPlaneAdapter.restore(
|
|
base.adapter.serialize(),
|
|
{ semanticToolRuntimeStore: durableStore },
|
|
);
|
|
const resolveExpiredExtensionReceipt = vi.fn(async () => ({ value: observedExport }));
|
|
const restored = new CapabilitySemanticToolRuntime({
|
|
adapter: restoredAdapter,
|
|
runId: OPEN.identity.runId,
|
|
scenarioGrants: ["portability:export"],
|
|
now: () => 1_001,
|
|
resolveExpiredExtensionReceipt,
|
|
});
|
|
|
|
await expect(restored.invoke({
|
|
operationId: "export_company",
|
|
input: {},
|
|
idempotencyKey: "recovered-export",
|
|
})).resolves.toMatchObject({
|
|
ok: true,
|
|
operationResultId: "tool-result-1",
|
|
value: observedExport,
|
|
});
|
|
expect(resolveExpiredExtensionReceipt).toHaveBeenCalledOnce();
|
|
expect(durableSnapshot.extensions[0]).toMatchObject({
|
|
status: "completed",
|
|
resultId: "tool-result-1",
|
|
});
|
|
|
|
await restored.invoke({
|
|
operationId: "export_company",
|
|
input: {},
|
|
idempotencyKey: "recovered-export",
|
|
});
|
|
expect(resolveExpiredExtensionReceipt).toHaveBeenCalledOnce();
|
|
});
|
|
|
|
it("allows only one restored runtime to reclaim an expired extension lease", async () => {
|
|
let durableSnapshot: CapabilitySemanticToolRuntimeSnapshot = {
|
|
schema: "paperclip.capability.semantic-tool-runtime.v1",
|
|
resultSequence: 0,
|
|
operationResults: {},
|
|
extensions: [{
|
|
key: `${OPEN.identity.runId}:upsert_case:contended-case`,
|
|
input: '{"body":"Case body","key":"case-contended"}',
|
|
status: "pending",
|
|
ownerId: "terminated-executor",
|
|
leaseExpiresAtMs: 1_000,
|
|
}],
|
|
};
|
|
let successfulClaims = 0;
|
|
const durableStore: CapabilitySemanticToolRuntimeStore = {
|
|
load: () => structuredClone(durableSnapshot),
|
|
save: (_runId, snapshot) => {
|
|
durableSnapshot = structuredClone(snapshot);
|
|
},
|
|
compareAndSwap: (_runId, expected, snapshot) => {
|
|
if (JSON.stringify(durableSnapshot) !== JSON.stringify(expected)) {
|
|
return false;
|
|
}
|
|
if (snapshot.extensions.some((extension) =>
|
|
extension.status === "pending" &&
|
|
extension.ownerId !== "terminated-executor" &&
|
|
extension.phase === "reserved"
|
|
)) successfulClaims += 1;
|
|
durableSnapshot = structuredClone(snapshot);
|
|
return true;
|
|
},
|
|
};
|
|
const base = await runtimeFor({ scenarioGrants: ["cases:write"] });
|
|
const serialized = base.adapter.serialize();
|
|
const runtimes = [0, 1].map(() => {
|
|
const adapter = CapabilityMockControlPlaneAdapter.restore(serialized, {
|
|
semanticToolRuntimeStore: durableStore,
|
|
});
|
|
return new CapabilitySemanticToolRuntime({
|
|
adapter,
|
|
runId: OPEN.identity.runId,
|
|
scenarioGrants: ["cases:write"],
|
|
now: () => 1_001,
|
|
});
|
|
});
|
|
const invocation = {
|
|
operationId: "upsert_case",
|
|
input: { key: "case-contended", body: "Case body" },
|
|
idempotencyKey: "contended-case",
|
|
} as const;
|
|
|
|
const results = await Promise.all(
|
|
runtimes.map((runtime) => runtime.invoke(invocation)),
|
|
);
|
|
|
|
expect(results.filter((result) => result.ok)).toHaveLength(1);
|
|
expect(results.filter((result) => !result.ok)).toEqual([
|
|
expect.objectContaining({
|
|
error: expect.objectContaining({
|
|
code: "operation_unsupported",
|
|
reason: "idempotency_recovery_in_flight",
|
|
}),
|
|
}),
|
|
]);
|
|
expect(successfulClaims).toBe(1);
|
|
});
|
|
|
|
it("preserves a durable extension lease when a stale runtime saves another result", async () => {
|
|
let durableSnapshot: CapabilitySemanticToolRuntimeSnapshot | null = null;
|
|
const durableStore: CapabilitySemanticToolRuntimeStore = {
|
|
load: () =>
|
|
durableSnapshot === null ? null : structuredClone(durableSnapshot),
|
|
save: (_runId, snapshot) => {
|
|
durableSnapshot = structuredClone(snapshot);
|
|
},
|
|
compareAndSwap: (_runId, expected, snapshot) => {
|
|
if (JSON.stringify(durableSnapshot) !== JSON.stringify(expected)) {
|
|
return false;
|
|
}
|
|
durableSnapshot = structuredClone(snapshot);
|
|
return true;
|
|
},
|
|
};
|
|
const base = await runtimeFor({ semanticToolRuntimeStore: durableStore });
|
|
const staleAdapter = CapabilityMockControlPlaneAdapter.restore(
|
|
base.adapter.serialize(),
|
|
{ semanticToolRuntimeStore: durableStore },
|
|
);
|
|
const staleRuntime = new CapabilitySemanticToolRuntime({
|
|
adapter: staleAdapter,
|
|
runId: OPEN.identity.runId,
|
|
});
|
|
const pendingKey = `${OPEN.identity.runId}:upsert_case:owned-elsewhere`;
|
|
durableSnapshot = {
|
|
schema: "paperclip.capability.semantic-tool-runtime.v1",
|
|
resultSequence: 0,
|
|
operationResults: {},
|
|
extensions: [{
|
|
key: pendingKey,
|
|
input: '{"body":"Case body","key":"case-owned"}',
|
|
status: "pending",
|
|
ownerId: "other-runtime",
|
|
leaseExpiresAtMs: 30_000,
|
|
}],
|
|
};
|
|
|
|
await expect(staleRuntime.invoke({
|
|
operationId: "get_task_context",
|
|
input: {},
|
|
})).resolves.toMatchObject({ ok: true });
|
|
|
|
expect(durableSnapshot.extensions).toEqual([
|
|
expect.objectContaining({
|
|
key: pendingKey,
|
|
status: "pending",
|
|
ownerId: "other-runtime",
|
|
}),
|
|
]);
|
|
});
|
|
|
|
it("rejects incomplete or malformed restored extension executions", async () => {
|
|
const { adapter, runtime } = await runtimeFor({
|
|
scenarioGrants: ["cases:write"],
|
|
});
|
|
await runtime.invoke({
|
|
operationId: "upsert_case",
|
|
input: { key: "case-1", body: "Case body" },
|
|
idempotencyKey: "upsert-case-1",
|
|
});
|
|
|
|
for (const mutate of [
|
|
(execution: Record<string, unknown>) => delete execution.value,
|
|
(execution: Record<string, unknown>) => {
|
|
execution.commandResult = { disposition: "applied" };
|
|
},
|
|
]) {
|
|
const snapshot = JSON.parse(adapter.serialize()) as {
|
|
semanticToolRuntimes: Record<
|
|
string,
|
|
{ extensions: Array<{ execution: Record<string, unknown> }> }
|
|
>;
|
|
};
|
|
const execution =
|
|
snapshot.semanticToolRuntimes[OPEN.identity.runId]!.extensions[0]!
|
|
.execution;
|
|
mutate(execution);
|
|
expect(() =>
|
|
CapabilityMockControlPlaneAdapter.restore(JSON.stringify(snapshot)),
|
|
).toThrow("semantic tool runtime for run-tools is invalid");
|
|
}
|
|
});
|
|
|
|
it("rejects restored extension receipts that disagree with inspection", async () => {
|
|
const { adapter, runtime } = await runtimeFor({
|
|
scenarioGrants: ["cases:write"],
|
|
});
|
|
const result = await runtime.invoke({
|
|
operationId: "upsert_case",
|
|
input: { key: "case-1", body: "Case body" },
|
|
idempotencyKey: "upsert-case-1",
|
|
});
|
|
if (!result.ok) throw new Error("expected extension success");
|
|
|
|
const snapshot = JSON.parse(adapter.serialize()) as {
|
|
semanticToolRuntimes: Record<
|
|
string,
|
|
{ operationResults: Record<string, unknown> }
|
|
>;
|
|
};
|
|
snapshot.semanticToolRuntimes[OPEN.identity.runId]!
|
|
.operationResults[result.operationResultId] = { upserted: false };
|
|
|
|
expect(() =>
|
|
CapabilityMockControlPlaneAdapter.restore(JSON.stringify(snapshot)),
|
|
).toThrow("semantic tool runtime for run-tools is invalid");
|
|
});
|
|
|
|
it("rejects a restored result sequence that can reuse a prior result id", async () => {
|
|
const { adapter, runtime } = await runtimeFor({
|
|
scenarioGrants: ["cases:write"],
|
|
});
|
|
const result = await runtime.invoke({
|
|
operationId: "upsert_case",
|
|
input: { key: "case-1", body: "Case body" },
|
|
idempotencyKey: "upsert-case-1",
|
|
});
|
|
expect(result).toMatchObject({ operationResultId: "tool-result-1" });
|
|
|
|
const snapshot = JSON.parse(adapter.serialize()) as {
|
|
semanticToolRuntimes: Record<string, { resultSequence: number }>;
|
|
};
|
|
snapshot.semanticToolRuntimes[OPEN.identity.runId]!.resultSequence = 0;
|
|
expect(() =>
|
|
CapabilityMockControlPlaneAdapter.restore(JSON.stringify(snapshot)),
|
|
).toThrow("semantic tool runtime for run-tools is invalid");
|
|
});
|
|
|
|
it("serializes concurrent retries for required-idempotency extensions", async () => {
|
|
const { runtime } = await runtimeFor({ scenarioGrants: ["cases:write"] });
|
|
const invocation = {
|
|
operationId: "upsert_case",
|
|
input: { key: "case-concurrent", body: "Case body" },
|
|
idempotencyKey: "upsert-case-concurrent",
|
|
} as const;
|
|
|
|
const [first, replay] = await Promise.all([
|
|
runtime.invoke(invocation),
|
|
runtime.invoke(invocation),
|
|
]);
|
|
expect(first).toMatchObject({ ok: true });
|
|
expect(replay).toMatchObject({ ok: true });
|
|
if (!first.ok || !replay.ok) throw new Error("expected extension success");
|
|
expect(replay.operationResultId).toBe(first.operationResultId);
|
|
|
|
const [accepted, conflicting] = await Promise.all([
|
|
runtime.invoke({ ...invocation, idempotencyKey: "shared-key" }),
|
|
runtime.invoke({
|
|
...invocation,
|
|
input: { key: "case-concurrent", body: "Different body" },
|
|
idempotencyKey: "shared-key",
|
|
}),
|
|
]);
|
|
expect(accepted).toMatchObject({ ok: true });
|
|
expect(conflicting).toMatchObject({
|
|
ok: false,
|
|
error: { code: "input_invalid", reason: "idempotency_key_conflict" },
|
|
});
|
|
});
|
|
});
|
|
|
|
describe("Capability security policy", () => {
|
|
it("separates model-only secret delivery from every observable result boundary", async () => {
|
|
expectTypeOf<CapabilityModelToolSuccess>().not.toMatchTypeOf<CapabilityToolSuccess>();
|
|
|
|
const secret = "CAPABILITY_SECRET_SENTINEL_f42d4fbc";
|
|
const { adapter, runtime } = await runtimeFor({
|
|
scenarioGrants: ["secrets:values:read"],
|
|
policy: { allowSecretValueAccess: true },
|
|
resolveSecretValue: (name) => name === "DEPLOY_TOKEN" ? secret : null,
|
|
});
|
|
expect(runtime.visibleTools().tools.map((tool) => tool.operationId)).toContain("read_secret_value");
|
|
const result = await runtime.invoke({
|
|
operationId: "read_secret_value",
|
|
input: { name: "DEPLOY_TOKEN" },
|
|
});
|
|
expect(result).toMatchObject({
|
|
ok: true,
|
|
value: { name: "DEPLOY_TOKEN", value: "[SECRET_VALUE]" },
|
|
});
|
|
if (!result.ok) throw new Error("expected secret read success");
|
|
expect(result.authorization.redactions).toEqual([
|
|
{ path: "$.value", replacement: "[SECRET_VALUE]" },
|
|
]);
|
|
expect(adapter.loadSemanticToolRuntime(OPEN.identity.runId)?.extensions)
|
|
.toEqual([]);
|
|
|
|
const inspected = await runtime.invoke({
|
|
operationId: "inspect_operation_result",
|
|
input: { operationResultId: result.operationResultId },
|
|
});
|
|
expect(inspected).toMatchObject({ ok: true, value: { value: "[SECRET_VALUE]" } });
|
|
|
|
const modelDelivery = await runtime.invokeForModel({
|
|
operationId: "read_secret_value",
|
|
input: { name: "DEPLOY_TOKEN" },
|
|
});
|
|
expect(modelDelivery.readModelResult()).toMatchObject({
|
|
schema: "paperclip.capability.model-tool-result.v1",
|
|
ok: true,
|
|
value: { name: "DEPLOY_TOKEN", value: secret },
|
|
});
|
|
expect(modelDelivery.observableResult).toMatchObject({
|
|
schema: "paperclip.capability.tool-result.v1",
|
|
ok: true,
|
|
value: { name: "DEPLOY_TOKEN", value: "[SECRET_VALUE]" },
|
|
});
|
|
|
|
const observablePayloads = {
|
|
publicInvocation: result,
|
|
trace: { toolResult: modelDelivery },
|
|
snapshot: structuredClone(modelDelivery),
|
|
browser: { toolResult: modelDelivery.observableResult },
|
|
authorization: runtime.authorizationRecords(),
|
|
};
|
|
expect(JSON.stringify(observablePayloads)).not.toContain(secret);
|
|
|
|
const failing = await runtimeFor({
|
|
scenarioGrants: ["secrets:values:read"],
|
|
policy: { allowSecretValueAccess: true },
|
|
resolveSecretValue: () => {
|
|
throw new Error(`resolver rejected ${secret}`);
|
|
},
|
|
});
|
|
const failedDelivery = await failing.runtime.invokeForModel({
|
|
operationId: "read_secret_value",
|
|
input: { name: "DEPLOY_TOKEN" },
|
|
});
|
|
expect(failedDelivery.observableResult).toMatchObject({
|
|
ok: false,
|
|
error: { code: "operation_unsupported", reason: "mock_operation_rejected" },
|
|
});
|
|
expect(JSON.stringify({
|
|
error: failedDelivery,
|
|
authorization: failing.runtime.authorizationRecords(),
|
|
})).not.toContain(secret);
|
|
|
|
let unclaimedResolverCalls = 0;
|
|
const ungranted = await runtimeFor({
|
|
scenarioGrants: ["secrets:values:read"],
|
|
resolveSecretValue: () => {
|
|
unclaimedResolverCalls += 1;
|
|
return secret;
|
|
},
|
|
});
|
|
expect(ungranted.runtime.visibleTools().tools.map((tool) => tool.operationId)).not.toContain("read_secret_value");
|
|
const unclaimedDelivery = await ungranted.runtime.invokeForModel({
|
|
operationId: "read_secret_value",
|
|
input: { name: "DEPLOY_TOKEN" },
|
|
});
|
|
expect(unclaimedDelivery.readModelResult()).toMatchObject({
|
|
ok: false,
|
|
error: { code: "policy_denied", reason: "secret_value_access_disabled" },
|
|
});
|
|
expect(unclaimedResolverCalls).toBe(0);
|
|
expect(JSON.stringify(unclaimedDelivery)).not.toContain(secret);
|
|
});
|
|
|
|
it("prohibits self approval even with the role and explicit decision claim", async () => {
|
|
const approval: CapabilityFixtureApproval = {
|
|
id: "approval-self",
|
|
companyId: "company-1",
|
|
taskIds: ["task-1"],
|
|
type: "request_board_approval",
|
|
status: "pending",
|
|
requestedByActorId: "actor-1",
|
|
payload: { summary: "self requested" },
|
|
decisionNote: null,
|
|
comments: [],
|
|
createdAt: "2026-08-09T00:00:00.000Z",
|
|
decidedAt: null,
|
|
};
|
|
const { adapter, runtime } = await runtimeFor({
|
|
role: "approver",
|
|
scenarioGrants: ["governance:approvals:decide"],
|
|
seed: { approvals: [approval] },
|
|
});
|
|
const result = await runtime.invoke({
|
|
operationId: "decide_approval",
|
|
input: { approvalId: "approval-self", decision: "approved", note: "self approve" },
|
|
idempotencyKey: "self-decision",
|
|
});
|
|
expect(result).toMatchObject({
|
|
ok: false,
|
|
error: { code: "policy_denied", reason: "self_approval_conflict" },
|
|
});
|
|
expect(adapter.snapshot().approvals[0]?.status).toBe("pending");
|
|
});
|
|
|
|
it("keeps the generic escape hatch test-only, explicitly granted, and allowlisted", async () => {
|
|
const denied = await runtimeFor({ workMode: "skill_test", scenarioGrants: ["test:generic_api_request"] });
|
|
expect(denied.runtime.visibleTools().tools.map((tool) => tool.operationId)).not.toContain("generic_api_request");
|
|
|
|
const { runtime } = await runtimeFor({
|
|
workMode: "skill_test",
|
|
scenarioGrants: ["test:generic_api_request"],
|
|
policy: {
|
|
allowGenericEscapeHatch: true,
|
|
genericEscapeHatchAllowlist: [{ method: "GET", path: "/mock/health" }],
|
|
},
|
|
});
|
|
expect(runtime.visibleTools().tools.map((tool) => tool.operationId)).toContain("generic_api_request");
|
|
const allowed = await runtime.invoke({
|
|
operationId: "generic_api_request",
|
|
input: { method: "GET", path: "/mock/health", headers: { authorization: "Bearer not-forwarded" } },
|
|
idempotencyKey: "escape-1",
|
|
});
|
|
expect(allowed).toMatchObject({ ok: true, value: { status: 200, body: null } });
|
|
expect(JSON.stringify(allowed)).not.toContain("not-forwarded");
|
|
|
|
const blocked = await runtime.invoke({
|
|
operationId: "generic_api_request",
|
|
input: { method: "POST", path: "/api/companies" },
|
|
idempotencyKey: "escape-2",
|
|
});
|
|
expect(blocked).toMatchObject({
|
|
ok: false,
|
|
error: { code: "policy_denied", reason: "generic_escape_hatch_target_denied" },
|
|
});
|
|
});
|
|
});
|