From 4f46eac453965efde3bc782aebfeec198551afc4 Mon Sep 17 00:00:00 2001 From: Kaoru0822 <208281322+kaoru0822-kitauji@users.noreply.github.com> Date: Mon, 12 Oct 2026 06:07:22 -0700 Subject: [PATCH 01/25] ci: narrow native-host lane selection and reuse build caches (#2131) * ci: narrow native-host lane selection and reuse build caches Unowned src/ and bridge/ files no longer fall back to the full baseline. They run the Linux suite plus the Linux x64 installed-package/Inspector baseline. New apple and package scopes own the Apple artifact lane, the package matrix and the Windows curated lane, including the test files those lanes invoke. Manual-only verifier entrypoints run static checks and the Linux suite. CHANGELOG, SECURITY, GitHub templates and the native Windows README are documentation-only. Pull requests omit the macOS x64 package host unless labelled ci:full. The Windows and Apple lanes restore the shared Turbo build cache, the Windows lane drops its duplicate typecheck, and the Linux suite uses six shards. Main pushes keep the full baseline. Co-Authored-By: Claude Opus 5.5 * ci: match package script entrypoints against scope globs Script-only package.json edits resolved their entrypoint with literal equality, so glob-owned verifiers such as scripts/verify/apple/** and scripts/verify/inspector/** fell back to the full baseline instead of selecting their owning lane. Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Claude Opus 5.5 --- .github/workflows/ci.yml | 15 +- docs/testing.md | 66 ++++-- scripts/ci/plan.mjs | 58 +++-- scripts/ci/scopes.mjs | 106 ++++++++- .../boundary/filesystem/ciChangeScope.test.ts | 213 +++++++++++++++++- 5 files changed, 413 insertions(+), 45 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a8dc167e4..20824bae3 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -105,7 +105,7 @@ jobs: strategy: fail-fast: false matrix: - shard: [1/4, 2/4, 3/4, 4/4] + shard: [1/6, 2/6, 3/6, 4/6, 5/6, 6/6] steps: - uses: actions/checkout@v4 - uses: actions/setup-node@v4 @@ -372,7 +372,7 @@ jobs: apple-artifacts: name: Real Apple artifact CLI/MCP E2E - needs: changes + needs: [changes, build] if: needs.changes.outputs.apple == 'true' runs-on: macos-15 timeout-minutes: 15 @@ -383,6 +383,10 @@ jobs: node-version-file: .nvmrc cache: npm - run: npm ci + - uses: actions/download-artifact@v4 + with: + name: rea-build-cache + path: .turbo/cache/ - name: Verify Apple artifact formats and metadata env: REA_ARTIFACT_NODE_COMPILE_CACHE: ${{ runner.temp }}/rea-artifact-node-cache @@ -445,7 +449,7 @@ jobs: windows-curated: name: Windows capability and package lane - needs: [changes, windows-native-build] + needs: [changes, build, windows-native-build] if: needs.changes.outputs.package == 'true' runs-on: windows-latest timeout-minutes: 25 @@ -456,8 +460,11 @@ jobs: node-version-file: .nvmrc cache: npm - run: npm ci + - uses: actions/download-artifact@v4 + with: + name: rea-build-cache + path: .turbo/cache/ - run: npm run build:cached - - run: npm run typecheck - uses: actions/download-artifact@v4 with: name: windows-native-controls diff --git a/docs/testing.md b/docs/testing.md index 92cf8dc44..9aaeb8464 100644 --- a/docs/testing.md +++ b/docs/testing.md @@ -837,25 +837,42 @@ not make native host acceptance unavailable. ## Native platform baseline in CI Pull requests that change only root `README*.md` files, `docs/`, `AGENTS.md`, -or `CONTRIBUTING.md` run formatting and generated-document validation without -the source-test shards or native package lanes. Classification compares the PR -head with its merge base, so later base-branch changes do not expand that scope. -Static and coverage aggregate jobs remain present and fail if classification -fails. Every code change retains the complete four-shard Linux suite; individual -test files are not selected by imports or filenames. +`CONTRIBUTING.md`, `CHANGELOG.md`, `SECURITY.md`, `native/windows/README.md`, +or the GitHub issue and pull-request templates run formatting and +generated-document validation without the source-test shards or native package +lanes. Classification compares the PR head with its merge base, so later +base-branch changes do not expand that scope. Static and coverage aggregate jobs +remain present and fail if classification fails. Every code change retains the +complete six-shard Linux suite; individual test files are not selected by +imports or filenames. `scripts/ci/plan.mjs` selects lanes using the ownership table in `scripts/ci/scopes.mjs`. Provider implementation changes run their real-provider -verifiers and the Linux x64 installed-package/Inspector baseline. Test-only -changes run the Linux suite without native package verification. Website changes -run website checks. Provider workflow changes validate the workflow and run that provider's verifier. -Manual-provider/Android workflow changes run workflow validation only. -Shared process/contract/CLI boundaries, dependencies, packaging inputs, unknown -paths, and changes to the planner select the full baseline. Package script-only -edits select their owning verifier when its entrypoint is identifiable; unknown -or shared script changes select everything. Android build-script edits retain -the Linux suite and workflow validation; real Android verification stays manual. -Renames account for both paths. +verifiers and the Linux x64 installed-package/Inspector baseline. The `apple` +scope owns the Apple artifact lane; the `package` scope owns the multi-host +package matrix and the Windows curated lane, including Windows native controls, +Inspector, setup, configuration and packaging entrypoints. Runtime sources under +`src/` or `bridge/` without an owner, and the authored skill, run the Linux suite +and the Linux x64 installed-package/Inspector baseline only. Test-only changes +run the Linux suite without native package verification, except test files that +the Apple or Windows curated lanes invoke explicitly, which also select that +lane. Edits to verifier entrypoints run only by manual, licensed or self-hosted +workflows run static checks and the Linux suite. Website changes run website +checks. Provider workflow changes validate the workflow and run that provider's +verifier. Manual-provider/Android workflow changes run workflow validation only. +Shared process/contract/CLI boundaries, dependencies, other packaging inputs, +unknown paths, and changes to the planner select the full baseline. Package +script-only edits select their owning verifier when its entrypoint is +identifiable; unknown or shared script changes select everything. Android +build-script edits retain the Linux suite and workflow validation; real Android +verification stays manual. Renames account for both paths. + +This narrower selection trades PR-time coverage for runner capacity. A change to +unowned shared runtime code that fails only on arm64, macOS, Windows, the Apple +artifact lane, or a provider lane that does not own the file is not caught on +the PR; the main-push full baseline reports it after merge. Ownership globs do +not follow imports. Use the `ci:full` label when a change may have native-host +or provider effects outside its owners. Main pushes and manual dispatch retain the full baseline, including all portable provider lanes and the native matrix below. A `ci:full` PR label selects the full @@ -879,12 +896,16 @@ CI exercises the pinned Node.js runtime on native hosted runners: | Linux x64 | `ubuntu-latest` | Installed package and real Node Inspector CLI/MCP | | Linux arm64 (aarch64) | `ubuntu-24.04-arm` | Installed package and real Node Inspector CLI/MCP | | macOS 15 arm64 | `macos-15` | Installed package and real Node Inspector CLI/MCP | -| macOS 15 x64 | `macos-15-intel` | Installed package and real Node Inspector CLI/MCP | +| macOS 15 x64 | `macos-15-intel` | Installed package and real Node Inspector CLI/MCP (main, dispatch and `ci:full` only) | | Windows x64 | `windows-latest` | Curated capabilities, native controls, installed package and real Node Inspector CLI/MCP | +macOS x64 is the slowest package host. Pull requests without the `ci:full` label +omit it, so darwin x64 installed-package and Inspector regressions surface on +the main-push full baseline rather than on the PR. + Package lanes assert the actual Node platform/architecture before verification -and record those values with the Node version. The four-host package matrix -runs up to four jobs concurrently on independent hosted runners with explicit +and record those values with the Node version. The package matrix runs up to +four jobs concurrently on independent hosted runners with explicit timeouts and per-runner Node heap/thread limits; Windows retains its curated capability lane. Package checks cover installation, CLI/MCP discovery, target-free analysis, configuration backups/recovery, Evidence and owned @@ -1077,7 +1098,7 @@ CI owns coverage; aggregate and domain/contract thresholds are maintained in `vitest.config.ts`. They are glob-specific and never updated automatically. Coverage does not replace named boundary or real-provider scenarios. -CI runs four native Vitest shards without retries. Each shard emits a blob +CI runs six native Vitest shards without retries. Each shard emits a blob report; the merge job produces aggregate coverage plus JUnit and JSON timing reports, uploads them together, and writes the slowest files to the workflow summary. Static checks, documentation, build, package verification, and @@ -1091,8 +1112,9 @@ architecture, and `.npmrc`; test-only changes can reuse compiled outputs. Every build and verification command still runs, so Turbo checks task inputs and rebuilds misses. There is no prefix fallback to accumulate older build outputs. -Native package jobs reuse the same workflow's Turbo cache for portable compiled -JavaScript, skills, and catalogs. They retain the normal build and prepack +Native package jobs, the Windows curated lane, and the Apple artifact lane reuse +the same workflow's Turbo cache for portable compiled JavaScript, skills, and +catalogs. The Windows lane relies on the Linux static job for typechecking. They retain the normal build and prepack commands, which restore matching outputs or rebuild on a cache miss. Native artifacts, dependency installs, package archives, and runtime verification remain host-local. Keep platform-dependent outputs out of this shared cache. diff --git a/scripts/ci/plan.mjs b/scripts/ci/plan.mjs index a970cc57e..bea640f55 100644 --- a/scripts/ci/plan.mjs +++ b/scripts/ci/plan.mjs @@ -1,19 +1,24 @@ import { execFileSync } from "node:child_process"; import { appendFileSync } from "node:fs"; import { matchesGlob } from "node:path"; -import { SCOPES } from "./scopes.mjs"; +import { MANUAL_VERIFIERS, SCOPES } from "./scopes.mjs"; const flags = [ - "code", - "runtime", - "package", - "apple", - "fixtures", - "readiness", - "docs", - "workflows", - ...Object.keys(SCOPES), + ...new Set([ + "code", + "runtime", + "package", + "apple", + "fixtures", + "readiness", + "docs", + "workflows", + ...Object.keys(SCOPES), + ]), ]; +// Test files select the native-host lanes that run them explicitly. Provider +// scopes remain implementation-owned, so test-only edits stay off those lanes. +const NATIVE_TEST_OWNERS = ["apple", "package"]; // Actionlint validates workflow syntax only. The semantic suite asserts these // workflows' release, publication and self-hosted runner safeguards. const GUARDED_WORKFLOWS = new Set([ @@ -87,11 +92,11 @@ function classifyPackage(base, head) { const scriptOwners = simple && entries.every((entry) => - Object.values(SCOPES).some((patterns) => patterns.includes(entry)), + Object.values(SCOPES).some((patterns) => matchesAny(entry, patterns)), ) ? Object.entries(SCOPES) .filter(([, patterns]) => - entries.some((entry) => patterns.includes(entry)), + entries.some((entry) => matchesAny(entry, patterns)), ) .map(([owner]) => owner) : []; @@ -119,14 +124,23 @@ if ( .filter(Boolean); for (const file of files) { if ( - /^(README(?:_[^/]+)?\.md|AGENTS\.md|CONTRIBUTING\.md)$/.test(file) || - file.startsWith("docs/") + /^(README(?:_[^/]+)?\.md|AGENTS\.md|CONTRIBUTING\.md|CHANGELOG\.md|SECURITY\.md|native\/windows\/README\.md|\.github\/pull_request_template\.md)$/.test( + file, + ) || + file.startsWith("docs/") || + file.startsWith(".github/ISSUE_TEMPLATE/") ) { selected.docs = true; selected.website ||= matchesAny(file, SCOPES.website); continue; } if (/^(src|tests)\/.*\.(?:test|spec)\.[cm]?[jt]sx?$/.test(file)) { + selected.code = true; + for (const owner of NATIVE_TEST_OWNERS) + selected[owner] ||= matchesAny(file, SCOPES[owner]); + continue; + } + if (matchesAny(file, MANUAL_VERIFIERS)) { selected.code = true; continue; } @@ -166,6 +180,14 @@ if ( } else if (owners.length) { if (file.startsWith("src/") || file.startsWith("bridge/")) selected.runtime = true; + } else if (/^(src|bridge)\//.test(file)) { + // Unowned runtime sources keep the Linux x64 installed-package baseline; + // ci:full, main pushes and manual dispatch retain every native host. + selected.runtime = true; + reasons.push(`Unowned runtime source: ${file}`); + } else if (file.startsWith(".agents/skills/")) { + selected.runtime = true; + selected.docs = true; } else { full(`Unclassified input: ${file}`); } @@ -173,13 +195,19 @@ if ( } else throw new Error(`Unsupported CI event: ${event}`); selected.runtime ||= selected.package; const linux = { os: "ubuntu-latest", platform: "linux", architecture: "x64" }; +// macOS x64 is the slowest package host. Pull requests run it only with ci:full; +// main pushes, schedules and manual dispatch always include it. +const intel = + event !== "pull_request" || process.env.CI_FULL === "true" + ? [{ os: "macos-15-intel", platform: "darwin", architecture: "x64" }] + : []; const matrix = { include: selected.package ? [ linux, { os: "ubuntu-24.04-arm", platform: "linux", architecture: "arm64" }, { os: "macos-15", platform: "darwin", architecture: "arm64" }, - { os: "macos-15-intel", platform: "darwin", architecture: "x64" }, + ...intel, ] : [linux], }; diff --git a/scripts/ci/scopes.mjs b/scripts/ci/scopes.mjs index 93c34c4a6..728cedc57 100644 --- a/scripts/ci/scopes.mjs +++ b/scripts/ci/scopes.mjs @@ -1,6 +1,88 @@ -// Ownership of the real-provider and website verification lanes. Shared build -// inputs are classified once in plan.mjs rather than repeated in every workflow. +// Ownership of the native-host, real-provider and website verification lanes. +// Shared build inputs are classified once in plan.mjs rather than repeated in +// every workflow. export const SCOPES = { + // Apple artifact CLI/MCP lane on macOS, including its focused regressions. + apple: [ + "src/native/Apple*", + "src/native/NativeMacOS*", + "src/native/NativeUi*", + "src/native/Lldb*", + "bridge/native/**", + "bridge/process/**", + "scripts/verify/apple/**", + "tests/boundary/filesystem/installScript.test.ts", + "tests/boundary/filesystem/nibNativeHierarchy.test.ts", + "tests/boundary/filesystem/interfaceBuilderBundle.test.ts", + "tests/boundary/cli/nativePlistCli.test.ts", + "tests/boundary/cli/nativePlistPrototypeKeyCli.test.ts", + "tests/boundary/cli/nativePlistSelectionFailureCli.test.ts", + "tests/boundary/cli/nativeDemangleCli.test.ts", + "tests/boundary/mcp/nativeDemangleMcp.test.ts", + "tests/boundary/providers/native/nativeSignatureTargetBinding.test.ts", + "tests/boundary/cli/nativeSignatureCli.test.ts", + "tests/boundary/mcp/nativeSignatureTargetBindingMcp.test.ts", + "src/process/capture/FilesystemSnapshot.test.ts", + "tests/boundary/process/darwinProcessRunTokenReader.test.ts", + "tests/boundary/process/directAnalysisSignals.test.ts", + "tests/boundary/process/processCaptureLifecycle.test.ts", + "tests/boundary/process/processCli.test.ts", + "tests/boundary/mcp/sessionMcp.test.ts", + ], + // Four-host installed-package matrix and the Windows curated lane. + package: [ + "src/windows/**", + "native/windows/**", + "src/inspector/**", + "scripts/verify/inspector/**", + "scripts/verify-package.mjs", + "scripts/verify-package-*", + "scripts/verify-windows-*.mjs", + "scripts/build-windows-native.mjs", + "scripts/windows-capabilities.mjs", + "scripts/prepack.mjs", + "scripts/prepare.mjs", + "scripts/rea.mjs", + "install.sh", + "src/config/**", + "src/application/Setup*.ts", + "src/application/Uninstall*.ts", + "src/cli/interactiveSetup.ts", + "src/cli/setupCommands.ts", + "tests/boundary/filesystem/binaryTarget.test.ts", + "tests/boundary/filesystem/safeOutputTree.test.ts", + "tests/boundary/providers/native/artifactProvider.archiveSafety.test.ts", + "tests/boundary/providers/native/artifactProvider.extractionCompleteness.test.ts", + "tests/boundary/filesystem/setupUnavailableProvider.test.ts", + "tests/boundary/filesystem/windowsNpxRegistration.test.ts", + "tests/boundary/filesystem/webScriptManifestPaths.test.ts", + "tests/boundary/process/ProcessEvidence.host.test.ts", + "tests/acceptance/webModuleCli.test.ts", + "tests/acceptance/webSourceLocationCli.test.ts", + "tests/boundary/cli/referenceSourceImportStatus.test.ts", + "tests/boundary/filesystem/asarInventoryFreshness.test.ts", + "tests/boundary/filesystem/javascript-applications/javascriptArtifactReconstruction.test.ts", + "tests/conformance/providers/ghidra/windowsCapabilities.test.ts", + "src/ghidra/GhidraInstallation.test.ts", + "tests/boundary/providers/ghidra/ghidraTransport.test.ts", + "tests/boundary/providers/ghidra/ghidraTargetSnapshot.test.ts", + "tests/boundary/providers/ghidra/ghidraLauncher.test.ts", + "tests/boundary/providers/hopper/hopperClient.cleanupRetry.test.ts", + "src/windows/WindowsNativeLoader.test.ts", + "src/windows/WindowsOwnedProcess.test.ts", + "src/ghidra/GhidraLauncherEnvironment.test.ts", + "src/process/ProcessOwnership.test.ts", + "src/process/ProcessOwnership.part2.test.ts", + "src/process/ProcessOwnership.part3.test.ts", + "src/process/ProcessOwnership.validation.test.ts", + "src/application/Update.test.ts", + "tests/process-global/updateMaintenance.test.ts", + "tests/conformance/providers/ghidra/realGhidraWindowsWorkflow.test.ts", + "tests/boundary/filesystem/directoryArtifactReaderOpen.test.ts", + "tests/boundary/filesystem/javascriptPackagePrecedence.test.ts", + "tests/boundary/filesystem/javascriptArtifactContentIdentity.test.ts", + "tests/boundary/process/sourceMapUncHosts.test.ts", + ], browser: [ ".github/workflows/real-browser.yml", "scripts/verify-real-browser.mjs", @@ -227,3 +309,23 @@ export const SCOPES = { ".github/workflows/website-pages.yml", ], }; + +// Verifier entrypoints that only manual, licensed, or self-hosted workflows +// run. No selected CI lane executes them, so their edits need only the static +// checks and the source-test suite. Shared helpers under scripts/lib remain +// broad inputs. +export const MANUAL_VERIFIERS = [ + "scripts/verify-real-electron.mjs", + "scripts/verify-real-ghidra*.mjs", + "scripts/verify-real-hopper*.mjs", + "scripts/verify-real-ida.mjs", + "scripts/verify-*-client.mjs", + "scripts/verify-native-agent-client.mjs", + "scripts/verify-jeb.mjs", + "scripts/verify-apktool.mjs", + "scripts/verify-adb.mjs", + "scripts/verify-agent-experience.mjs", + "scripts/verify-packaged-ghidra-windows.mjs", + "scripts/verify-wasm-artifact.mjs", + "scripts/verify-packaged-wasm-artifact.mjs", +]; diff --git a/tests/boundary/filesystem/ciChangeScope.test.ts b/tests/boundary/filesystem/ciChangeScope.test.ts index 32db97e1d..e65575496 100644 --- a/tests/boundary/filesystem/ciChangeScope.test.ts +++ b/tests/boundary/filesystem/ciChangeScope.test.ts @@ -6,7 +6,7 @@ import { rename, writeFile, } from "node:fs/promises"; -import { dirname, join } from "node:path"; +import { dirname, join, matchesGlob } from "node:path"; import { afterAll, beforeAll, expect, it } from "vitest"; import { parse } from "yaml"; @@ -157,7 +157,7 @@ it("retains complete checks when implementation is renamed into documentation", const head = await commit(directory); expect(await classify(directory, base, head)).toMatchObject({ code: true, - package: true, + runtime: true, }); }); @@ -268,6 +268,54 @@ it.each([ { code: false, evm: true, browser: false, package: false }, ], ["unknown-input", { code: true, package: true, evm: true, browser: true }], + [ + "src/application/Example.ts", + { + code: true, + runtime: true, + package: false, + apple: false, + evm: false, + browser: false, + }, + ], + [ + "bridge/ghidra/Example.java", + { code: true, runtime: true, package: false, apple: false }, + ], + [ + "src/native/AppleExample.ts", + { code: true, runtime: true, apple: true, package: false, evm: false }, + ], + [ + "scripts/verify/apple/example.mjs", + { code: true, apple: true, package: false, runtime: false }, + ], + [ + "src/windows/Example.ts", + { code: true, runtime: true, package: true, apple: false, evm: false }, + ], + ["install.sh", { code: true, runtime: true, package: true, evm: false }], + [ + ".agents/skills/reverse-engineer-anything/SKILL.md", + { code: true, runtime: true, docs: true, package: false, evm: false }, + ], + [ + "scripts/verify-real-ghidra.mjs", + { code: true, runtime: false, package: false, apple: false, evm: false }, + ], + [ + "scripts/verify-claude-client.mjs", + { code: true, runtime: false, package: false, browser: false }, + ], + [ + "tests/boundary/process/darwinProcessRunTokenReader.test.ts", + { code: true, apple: true, package: false, runtime: false }, + ], + [ + "src/process/ProcessOwnership.test.ts", + { code: true, package: true, apple: false, evm: false, browser: false }, + ], ])("selects the appropriate lanes for %s", async (path, expected) => { const { directory, base } = await fixture(); const head = await change(directory, [path]); @@ -404,6 +452,14 @@ it.each([ { code: true, package: true, browser: true, evm: true }, ], ["docs/.vitepress/config.ts", { code: false, docs: true, package: false }], + ["CHANGELOG.md", { code: false, docs: true, package: false }], + ["SECURITY.md", { code: false, docs: true, package: false }], + [".github/pull_request_template.md", { code: false, docs: true }], + [ + ".github/ISSUE_TEMPLATE/bug.yml", + { code: false, docs: true, workflows: false }, + ], + ["native/windows/README.md", { code: false, docs: true, package: false }], [ "src/contracts/evm/tools.ts", { code: true, docs: true, evm: true, package: false }, @@ -462,6 +518,43 @@ it("fails without emitting a plan when package metadata is malformed", async () expect(await readFile(join(directory, "scope-output"), "utf8")).toBe(""); }); +it.each([ + [ + "verify:keyed-archive", + "npm run build:cached && node scripts/verify/apple/keyed-archive.mjs", + { apple: true, package: false, evm: false, browser: false }, + ], + [ + "verify:inspector", + "npm run build:cached && node scripts/verify/inspector/runtime-observation.mjs", + { package: true, apple: false, evm: false, browser: false }, + ], + [ + "verify:windows-native", + "node scripts/verify-windows-native.mjs", + { package: true, apple: false, evm: false, browser: false }, + ], +])( + "routes the %s script to its glob-owned lane", + async (script, command, expected) => { + const { directory } = await fixture(); + const base = await change( + directory, + ["package.json"], + JSON.stringify({ scripts: { [script]: command } }), + ); + const head = await change( + directory, + ["package.json"], + JSON.stringify({ scripts: { [script]: `${command} --verbose` } }), + ); + expect(await classify(directory, base, head)).toMatchObject({ + code: true, + ...expected, + }); + }, +); + it("retains a broad fallback when a provider script adds shared shell work", async () => { const { directory } = await fixture(); const before = { @@ -538,3 +631,119 @@ it.each(["compile:termux", "build:termux", "build:termux:run"])( }); }, ); + +const INTEL_HOST = { + os: "macos-15-intel", + platform: "darwin", + architecture: "x64", +}; + +it("keeps macOS x64 package verification for ci:full and non-PR events", async () => { + const { directory, base } = await fixture(); + const head = await change(directory, ["src/windows/Example.ts"]); + const matrix = z.object({ include: z.array(z.unknown()) }); + const hosts = async (event: string, full: boolean) => + matrix.parse((await classify(directory, base, head, event, full)).matrix) + .include; + const pullRequest = await hosts("pull_request", false); + expect(pullRequest).toHaveLength(3); + expect(pullRequest).not.toContainEqual(INTEL_HOST); + expect(await hosts("pull_request", true)).toContainEqual(INTEL_HOST); + for (const event of ["push", "schedule", "workflow_dispatch"]) + expect(await hosts(event, false)).toContainEqual(INTEL_HOST); +}); + +const workflowFile = async (name: string): Promise => + parse( + await readFile( + new URL(`../../../.github/workflows/${name}`, import.meta.url), + "utf8", + ), + ); + +const scopesSchema = z.object({ + SCOPES: z.record(z.string(), z.array(z.string())), + MANUAL_VERIFIERS: z.array(z.string()), +}); + +const loadScopes = async () => + scopesSchema.parse( + await import( + new URL("../../../scripts/ci/scopes.mjs", import.meta.url).href + ), + ); + +const stepRuns = z.object({ + on: z.unknown(), + jobs: z.record( + z.string(), + z.object({ + steps: z.array(z.object({ run: z.string().optional() })).optional(), + }), + ), +}); + +it("owns every explicit native-host test path with the lane that runs it", async () => { + const { SCOPES } = await loadScopes(); + const ci = stepRuns.parse(await workflowFile("ci.yml")); + const listed = (job: string) => + (ci.jobs[job]?.steps ?? []) + .flatMap((step) => step.run?.split(/\s+/) ?? []) + .filter((token) => token.endsWith(".test.ts")); + const unowned = (job: string, scope: string) => + listed(job).filter( + (path) => + !(SCOPES[scope] ?? []).some((pattern) => matchesGlob(path, pattern)), + ); + expect(listed("apple-artifacts").length).toBeGreaterThan(0); + expect(listed("windows-curated").length).toBeGreaterThan(0); + expect(unowned("apple-artifacts", "apple")).toEqual([]); + expect(unowned("windows-curated", "package")).toEqual([]); +}); + +it("never runs a manual-only verifier from an automatic workflow", async () => { + const { MANUAL_VERIFIERS } = await loadScopes(); + const scripts = z + .object({ scripts: z.record(z.string(), z.string()) }) + .parse( + JSON.parse( + await readFile( + new URL("../../../package.json", import.meta.url), + "utf8", + ), + ), + ).scripts; + const directory = new URL("../../../.github/workflows/", import.meta.url); + const commands: string[] = []; + for (const name of await readdir(directory)) { + const workflow = stepRuns.parse(await workflowFile(name)); + const triggers = JSON.stringify(workflow.on); + if ( + name !== "ci.yml" && + !triggers.includes("workflow_call") && + !triggers.includes("pull_request") + ) + continue; + for (const job of Object.values(workflow.jobs)) + for (const step of job.steps ?? []) if (step.run) commands.push(step.run); + } + const files = new Set(); + const seen = new Set(); + while (commands.length) { + const command = commands.pop() ?? ""; + for (const [, file] of command.matchAll(/\b(scripts\/[\w./-]+\.mjs)\b/g)) + if (file) files.add(file); + for (const [, script] of command.matchAll(/npm run ([\w:.-]+)/g)) { + if (!script || seen.has(script)) continue; + seen.add(script); + const body = scripts[script]; + if (body) commands.push(body); + } + } + expect(files.size).toBeGreaterThan(0); + expect( + [...files].filter((file) => + MANUAL_VERIFIERS.some((pattern) => matchesGlob(file, pattern)), + ), + ).toEqual([]); +}); From 315973259732544a597b8b17d25c69735910e72e Mon Sep 17 00:00:00 2001 From: Kaoru0822 <208281322+kaoru0822-kitauji@users.noreply.github.com> Date: Mon, 12 Oct 2026 06:07:37 -0700 Subject: [PATCH 02/25] fix(cli): report an invalid REA_LOG_LEVEL as configuration (#2140) createCli parsed REA_LOG_LEVEL with a throwing schema, so an invalid or uppercase value crashed every command, including doctor and --help, with a ZodError stack trace. Parse it into a ConfigurationError result and have the executable print the same configuration message as the MCP adapter and exit 1 before building the CLI. Fixes #2135 Co-authored-by: Claude Opus 5.5 --- scripts/rea.mjs | 9 ++++++-- src/cli.ts | 20 +++++++++++++++--- src/logger.ts | 20 ++++++++++++++++-- .../cli/cliLogLevelConfiguration.test.ts | 21 +++++++++++++++++++ tests/boundary/cli/dispatcher.test.ts | 2 +- 5 files changed, 64 insertions(+), 8 deletions(-) create mode 100644 tests/boundary/cli/cliLogLevelConfiguration.test.ts diff --git a/scripts/rea.mjs b/scripts/rea.mjs index 3b1ef6c4a..d3380c9c2 100755 --- a/scripts/rea.mjs +++ b/scripts/rea.mjs @@ -58,7 +58,8 @@ if (!(await compiledRuntimeExists(runtimeFiles))) { process.stdout.write(result.output); process.exitCode = result.exitCode; } else { - const { createCli } = await import("../dist/cli-runtime/cli.js"); + const { cliConfigurationError, createCli } = + await import("../dist/cli-runtime/cli.js"); const { createStreamedCliJsonOutput } = await import("../dist/cli-runtime/streamedJsonOutput.js"); const { @@ -67,7 +68,11 @@ if (!(await compiledRuntimeExists(runtimeFiles))) { validateCliOutputArguments, } = await import("../dist/cli-runtime/cliOutput.js"); const outputArguments = validateCliOutputArguments(args); - if (!outputArguments.ok) { + const configurationError = cliConfigurationError(process.env); + if (configurationError !== undefined) { + process.stderr.write(`${configurationError}\n`); + process.exitCode = 1; + } else if (!outputArguments.ok) { process.stdout.write(renderCliOutputArgumentError(outputArguments)); process.exitCode = 1; } else { diff --git a/src/cli.ts b/src/cli.ts index 1cbe3b896..5438fd472 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -8,6 +8,7 @@ import { registerBinaryDiagnosticsCommands } from "./cli/binaryDiagnosticsComman import { Cli } from "incur"; import { createLogger, parseLogLevel } from "./logger.js"; +import { projectAnalysisError } from "./domain/analysisErrorProjection.js"; import { PRODUCT_IDENTITY } from "./identity.js"; import { npxRegistrationCommand } from "./application/ClientRegistrationIdentity.js"; import { registerSetupCommands } from "./cli/setupCommands.js"; @@ -41,6 +42,16 @@ import { type CliResultOutput, } from "./cli/streamedJsonOutput.js"; +/** Describe CLI settings that are invalid before any command runs, as MCP does. */ +export const cliConfigurationError = ( + selectedEnvironment: Readonly>, +): string | undefined => { + const value = selectedEnvironment.REA_LOG_LEVEL; + if (value === undefined) return undefined; + const level = parseLogLevel(value); + return level.ok ? undefined : projectAnalysisError(level.error).message; +}; + /** * Build the one-shot Incur CLI without starting Hopper at import time. * Analysis commands acquire and close their own sessions; bare `mcp` and @@ -54,11 +65,14 @@ export const createCli = ( ): CliInstance => { const environment = snapshotEnvironment(selectedEnvironment); const analysis = createDirectAnalysis(environment); + // The executable reports an invalid level through cliConfigurationError. + const level = + environment.REA_LOG_LEVEL === undefined + ? undefined + : parseLogLevel(environment.REA_LOG_LEVEL); const logger = createLogger( "cli", - environment.REA_LOG_LEVEL === undefined - ? "silent" - : parseLogLevel(environment.REA_LOG_LEVEL), + level?.ok === true ? level.value : "silent", ); const cli = Cli.create(PRODUCT_IDENTITY.cliBinary, { version: PRODUCT_IDENTITY.packageVersion, diff --git a/src/logger.ts b/src/logger.ts index e43c7f97c..05fc3822f 100644 --- a/src/logger.ts +++ b/src/logger.ts @@ -1,6 +1,9 @@ import pino, { type Logger } from "pino"; import { z } from "zod"; +import { ConfigurationError } from "./domain/configurationErrors.js"; +import { err, ok, type Result } from "./domain/result.js"; + /** Supported structured log levels at the process boundary. */ export type LogLevel = z.infer; @@ -9,8 +12,21 @@ const logLevelSchema = z .default("info"); /** Parse the optional REA_LOG_LEVEL environment variable. */ -export const parseLogLevel = (value: unknown): LogLevel => - logLevelSchema.parse(value); +export const parseLogLevel = ( + value: unknown, +): Result => { + const parsed = logLevelSchema.safeParse(value); + if (parsed.success) return ok(parsed.data); + return err( + new ConfigurationError("Invalid REA environment configuration", { + cause: parsed.error, + settings: parsed.error.issues.map(({ message }) => ({ + setting: "REA_LOG_LEVEL", + constraint: message, + })), + }), + ); +}; /** Create a JSON logger on stderr so protocol and CLI result stdout stay valid. */ export const createLogger = (mode: "mcp" | "cli", level: LogLevel): Logger => diff --git a/tests/boundary/cli/cliLogLevelConfiguration.test.ts b/tests/boundary/cli/cliLogLevelConfiguration.test.ts new file mode 100644 index 000000000..cc09a3b94 --- /dev/null +++ b/tests/boundary/cli/cliLogLevelConfiguration.test.ts @@ -0,0 +1,21 @@ +import { expect } from "vitest"; + +import { cliTest } from "../../support/cli/cliFixture.js"; + +for (const level of ["loud", "INFO"]) + cliTest( + `reports REA_LOG_LEVEL=${level} as invalid configuration`, + async ({ cli }) => { + for (const arguments_ of [["doctor", "--json"], ["--help"]]) { + const result = await cli.run({ + arguments: arguments_, + environment: { REA_LOG_LEVEL: level }, + }); + expect(result.exitCode).toBe(1); + expect(result.stdout).toBe(""); + expect(result.stderr).toBe( + 'REA configuration is invalid: REA_LOG_LEVEL: Invalid option: expected one of "trace"|"debug"|"info"|"warn"|"error"|"fatal"|"silent".\n', + ); + } + }, + ); diff --git a/tests/boundary/cli/dispatcher.test.ts b/tests/boundary/cli/dispatcher.test.ts index 583bd601d..efd6ca414 100644 --- a/tests/boundary/cli/dispatcher.test.ts +++ b/tests/boundary/cli/dispatcher.test.ts @@ -25,7 +25,7 @@ describe("executable dispatcher", () => { ), workspace.write( "dist/cli-runtime/cli.js", - 'export const createCli = () => ({ serve: (args) => process.stdout.write(JSON.stringify({ adapter: "cli", args })) });\n', + 'export const cliConfigurationError = () => undefined; export const createCli = () => ({ serve: (args) => process.stdout.write(JSON.stringify({ adapter: "cli", args })) });\n', ), workspace.write( "dist/cli-runtime/cliOutput.js", From 58fb372071e56da9c5df0e37593ee3f0da6b64bc Mon Sep 17 00:00:00 2001 From: Ysopwnable <161784452+N0zoM1z0@users.noreply.github.com> Date: Mon, 12 Oct 2026 21:17:52 +0800 Subject: [PATCH 03/25] feat(analysis): add opt-in retained inventory and WASM views (#2122) Add opt-in retained inventory and WASM Evidence views with exact nested output contracts and a small static WASM descriptor. Preserve old complete CLI/MCP workflows, producer choices, original authenticated Evidence and cleanup ownership. Validated with real public applications, genuine old Evidence migration, 123 focused assertions, 11 synchronized description checks and 42 installed-package WABT cases. Detailed generated experiment JSON stays outside the final tree; concise findings and reproduction inputs remain. Main-existing completion/package CI failures are separately attributed and left unchanged. Refs #2016 Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com> --- .../references/evidence-workflows.md | 13 + .../references/native-and-artifacts.md | 10 + .gitignore | 1 + docs/architecture.mermaid | 6 +- docs/mcp-contracts.md | 37 +- experiments/analysis-efficiency/README.md | 540 +++++++++++++ .../analysis-efficiency/agent-trial.py | 278 +++++++ experiments/analysis-efficiency/compare.mjs | 157 ++++ experiments/analysis-efficiency/corpus.json | 120 +++ experiments/analysis-efficiency/measure.mjs | 754 ++++++++++++++++++ .../analysis-efficiency/probe-analyzers.mjs | 116 +++ .../analysis-efficiency/probe-browser.mjs | 85 ++ .../provider-environment.json | 10 + .../analysis-efficiency/questions/android.txt | 3 + .../analysis-efficiency/questions/browser.txt | 1 + .../questions/electron-inventory.txt | 1 + .../analysis-efficiency/questions/evm.txt | 1 + .../questions/firmware.txt | 1 + .../questions/javascript-selected.txt | 1 + .../questions/managed-nonempty.txt | 1 + .../analysis-efficiency/questions/managed.txt | 1 + .../analysis-efficiency/questions/native.txt | 1 + .../analysis-efficiency/questions/wasm.txt | 1 + .../analysis-efficiency/resource-run.py | 158 ++++ .../analysis-efficiency/review-model.py | 265 ++++++ .../analysis-efficiency/verify-migration.mjs | 163 ++++ .../analysisView/AnalysisViewService.ts | 1 + src/application/wasm/WasmArtifactService.ts | 7 +- src/cli/wasmCommands.ts | 9 +- src/composition/wasm.ts | 22 + src/contracts/analysisViewToolContracts.ts | 22 +- src/contracts/artifactToolContracts.ts | 16 +- src/contracts/toolEffects.test.ts | 2 + src/contracts/toolOutputSchemaGroups.ts | 10 +- src/contracts/wasm/wasmToolContracts.ts | 41 +- src/domain/analysisView/analysisView.ts | 174 +++- src/domain/analysisView/analysisViewText.ts | 55 ++ .../analysisView/artifactInventoryView.ts | 203 +++++ src/domain/analysisView/binaryLayoutView.ts | 8 + .../analysisView/javascriptApplicationView.ts | 6 + src/domain/analysisView/nativeFunctionView.ts | 45 +- src/domain/analysisView/wasmArtifactView.ts | 207 +++++ src/domain/artifactGraph.ts | 8 +- src/domain/artifactInspection.ts | 18 + src/domain/artifactInventoryEvidence.ts | 129 +-- src/domain/wasm/wasmArtifact.ts | 20 + src/server/createServer.ts | 13 +- src/server/registerArtifactTools.ts | 27 +- src/server/registerEvidenceTools.ts | 31 +- src/server/registerWasmTools.ts | 6 +- tests/boundary/cli/analysisViewCli.test.ts | 51 +- .../mcp/artifactMcp.inlineInventory.test.ts | 454 ++++++++++- tests/boundary/mcp/sessionMcp.test.ts | 2 +- tests/boundary/mcp/wasmArtifactMcp.test.ts | 218 ++++- tests/fixtures/wasm/artifact.ts | 27 +- 55 files changed, 4322 insertions(+), 235 deletions(-) create mode 100644 experiments/analysis-efficiency/README.md create mode 100644 experiments/analysis-efficiency/agent-trial.py create mode 100644 experiments/analysis-efficiency/compare.mjs create mode 100644 experiments/analysis-efficiency/corpus.json create mode 100644 experiments/analysis-efficiency/measure.mjs create mode 100644 experiments/analysis-efficiency/probe-analyzers.mjs create mode 100644 experiments/analysis-efficiency/probe-browser.mjs create mode 100644 experiments/analysis-efficiency/provider-environment.json create mode 100644 experiments/analysis-efficiency/questions/android.txt create mode 100644 experiments/analysis-efficiency/questions/browser.txt create mode 100644 experiments/analysis-efficiency/questions/electron-inventory.txt create mode 100644 experiments/analysis-efficiency/questions/evm.txt create mode 100644 experiments/analysis-efficiency/questions/firmware.txt create mode 100644 experiments/analysis-efficiency/questions/javascript-selected.txt create mode 100644 experiments/analysis-efficiency/questions/managed-nonempty.txt create mode 100644 experiments/analysis-efficiency/questions/managed.txt create mode 100644 experiments/analysis-efficiency/questions/native.txt create mode 100644 experiments/analysis-efficiency/questions/wasm.txt create mode 100644 experiments/analysis-efficiency/resource-run.py create mode 100644 experiments/analysis-efficiency/review-model.py create mode 100644 experiments/analysis-efficiency/verify-migration.mjs create mode 100644 src/domain/analysisView/analysisViewText.ts create mode 100644 src/domain/analysisView/artifactInventoryView.ts create mode 100644 src/domain/analysisView/wasmArtifactView.ts diff --git a/.agents/skills/reverse-engineer-anything/references/evidence-workflows.md b/.agents/skills/reverse-engineer-anything/references/evidence-workflows.md index 5ccd021b9..c651287f3 100644 --- a/.agents/skills/reverse-engineer-anything/references/evidence-workflows.md +++ b/.agents/skills/reverse-engineer-anything/references/evidence-workflows.md @@ -11,6 +11,19 @@ returned inline. When complete `inspect_binary_layout` or `inspect_analysis_view` for a summary, one section/symbol/module, or a stable page instead of repeating the producer. +Completed `inspect_artifact` inventory also supports a summary and +`view: {kind: "inventory", collection: "occurrences", logical_paths: ["package.json", "main.js"]}`. +Omit `logical_paths` to page all available occurrences; `[]` selects none. +Other inventory collections are `nodes`, `edges` and +`integrity_contradictions`. Keep `source_complete` separate from page exhaustion: +a missing match in partial source Evidence does not establish absence. Use the +returned parent Evidence ID for subsequent views and export before closing the +session when the source must survive it. +When `tools/list` advertises `inspect_artifact.detail`, request `"summary"` to +retain its complete source and receive that view immediately. Without that +advertised choice, keep the existing complete call. CLI inspection returns +portable complete Evidence; save it before projecting a selected inline view. + Use `record_unknown` to track unresolved questions in the user's investigation and name the authority or environment still required. Supply supporting and contradicting evidence IDs. It updates the session registry directly. diff --git a/.agents/skills/reverse-engineer-anything/references/native-and-artifacts.md b/.agents/skills/reverse-engineer-anything/references/native-and-artifacts.md index 2b0def8e4..1b02a7ed2 100644 --- a/.agents/skills/reverse-engineer-anything/references/native-and-artifacts.md +++ b/.agents/skills/reverse-engineer-anything/references/native-and-artifacts.md @@ -36,6 +36,16 @@ retained, call `inspect_analysis_view` for a summary, mitigations or linkage facet, one section or symbol, or a stable page. Do not re-run the decoder to read a single object. +## Offline WASM + +Use `inspect_wasm_artifact` with caller-supplied WABT for selected local bytes. +For a completed result, use `inspect_analysis_view` with its reported retained +reference for a summary or `view: {kind: "wasm", facet: "exports", offset: 0, limit: 32}`. +Facets also include sections, imports, candidates, glue, WAT, headers, details +and tool profile. Text offsets count UTF-16 code units; follow `next_offset`. +The complete source remains retained after an oversized MCP reply. No target +execution or network acquisition is implied, and WAT is decoded representation. + ## Native targets After `open_binary`, use focused search, procedure, or function tools directly. diff --git a/.gitignore b/.gitignore index e1b8d81eb..90d431ec0 100644 --- a/.gitignore +++ b/.gitignore @@ -47,6 +47,7 @@ Thumbs.db # Local reference sources and analysis artifacts /_reference/ +/experiments/analysis-efficiency/results/ # Generated website download /website/public/examples/notes-example.zip diff --git a/docs/architecture.mermaid b/docs/architecture.mermaid index 83c6e5cb2..33725582d 100644 --- a/docs/architecture.mermaid +++ b/docs/architecture.mermaid @@ -6,7 +6,7 @@ graph TD end subgraph "Production Composition" - COMPOSITION["src/composition/
fresh typed provider and session factories
one-shot binding and independent optional loading"] + COMPOSITION["src/composition/
fresh typed provider and session factories
one-shot binding and independent optional loading
static WASM descriptor: named contract, factory, retryable lifetime"] end subgraph "Adapters" @@ -27,6 +27,7 @@ graph TD subgraph "Provider Layers" PWNTOOLS["src/native/pwntools/ + src/native/pwndbg/
BYO offline ELF/core adapters
owned snapshots/Python and optional core-only GDB
unchanged upstream parsers, ABI structures and mapping API"] + WASM["src/wasm/
caller-supplied WABT; immutable artifact inspection
owned process supervision and retryable workspace cleanup"] EVM["src/evm/
unchanged EVMole/WASM in bounded owned worker
explicit carrier parsing and inferred interface adapter"] HOPPER["src/hopper/
Hopper launch & Unix-socket protocol
BridgeLauncher, HopperClient, protocol"] GHIDRA["src/ghidra/
Ghidra discovery, profile & function analysis
snapshot, Unix/Windows transport, bounded lifecycle"] @@ -94,6 +95,9 @@ graph TD APP -->|record callbacks| RECORDS SERVER -->|narrow record ports| RECORDS RECORDS --> DOMAIN + COMPOSITION --> WASM + WASM --> PROCESS + WASM --> DOMAIN COMPOSITION --> PWNTOOLS PWNTOOLS --> PROCESS PWNTOOLS --> DOMAIN diff --git a/docs/mcp-contracts.md b/docs/mcp-contracts.md index 7ac0bf1f2..8e5755caf 100644 --- a/docs/mcp-contracts.md +++ b/docs/mcp-contracts.md @@ -412,7 +412,7 @@ Ordinary responses keep their existing complete result contract. ## Retained application Evidence inputs -`analyze_javascript_application` and `inspect_binary_layout` accept `detail`: +`analyze_javascript_application`, `inspect_binary_layout`, and `inspect_artifact` accept `detail`: `complete` (default) returns the complete analysis Evidence; `summary` records that complete Evidence in the session and returns the derived `inspect_analysis_view` summary Evidence instead. Its @@ -420,6 +420,9 @@ that complete Evidence in the session and returns the derived analysis, so later views and application workflows read it without repeating analysis or moving the complete record through the response. A server without session retention refuses summary detail with `capability_unavailable`. +For `inspect_artifact`, omission also keeps the original request parameters and +complete output shape. Delivery selection never reaches the inventory engine; +the complete inspection and its atomic inventory Evidence remain retained. `inspect_analysis_view` also accepts `api-surface` for retained HAR capture Evidence, grouping request lines by method, origin and format-rule path template. Templated @@ -446,6 +449,34 @@ application and semantic coverage. The result carries a digest of the selected facts. Actual serialized size determines MCP transport admission: reduce the page size or export retained Evidence if the view is too large. +Completed `inspect_artifact` and atomic `inventory_artifact` Evidence also +support summary and inventory views. A summary reports the manifest's expected +counts separately from available record counts, observed entry kinds, inventory +Evidence IDs and inspection coverage. An inventory view selects `nodes`, +`occurrences`, `edges`, or `integrity_contradictions`, with `offset` and `limit` +defaulting to 0 and 64. For occurrences, optional `logical_paths` matches exact +recorded strings: omission selects all available records; `[]` selects none. +These selectors do not reopen the artifact or resolve filesystem paths. +`source_complete` records whether the available inventory passes the complete +graph checks. Exhausting a page from a partial source does not establish that +unreturned paths are absent. Views carry their own Evidence IDs and preserve +links to the original parent; complete producer output and historical inline +Evidence input parsing retain their existing shapes. + +Completed `inspect_wasm_artifact` Evidence supports a summary and +`view: {kind: "wasm", facet: "exports", offset: 0, limit: 64}`. Other facets +are `sections`, `imports`, `candidates`, `glue`, `wat`, `headers`, `details`, and +the singleton `tool_profile` (offset 0). Arrays retain original WABT forms and +records. Text facets page by UTF-16 code units and preserve complete Unicode +characters. Summaries report the selected artifact's identity, validation, +available counts, decoded WAT digest/length and exact tool profile. Runtime +execution and network acquisition remain `not-performed`. A valid WAT digest +is checked at the owning model boundary for current output and portable input. +The original producer still returns complete Evidence by default. When its +complete MCP reply exceeds the frame, use the exact reported retained +reference with these views, or export the complete source. Views do not run +WABT again and do not turn decoded WAT into original source. + `trace_application_feature`, `trace_javascript_semantics`, `compare_application_versions`, `compare_source_to_bundle`, and `compare_javascript_export_shapes` accept complete inline application Evidence @@ -484,6 +515,10 @@ CLI application workflows continue to read portable inline Evidence from files and run the same analysis workflows; a standalone CLI invocation cannot resolve another MCP connection's retained records. No additional lookup call, provider selection, or approval step is required for a same-session follow-up. +CLI `inspect-artifact` continues to return complete portable Evidence. Save that +JSON to a file and use `inspect-analysis-view` with inline source Evidence for +later inventory summaries or pages; a one-shot CLI session cannot retain a +parent for another invocation. ## Aggregate native context diff --git a/experiments/analysis-efficiency/README.md b/experiments/analysis-efficiency/README.md new file mode 100644 index 000000000..8c1708009 --- /dev/null +++ b/experiments/analysis-efficiency/README.md @@ -0,0 +1,540 @@ +# Analysis efficiency measurements + +Tracks the experiments for [#2016](https://github.com/morluto/rea/issues/2016). +The production CLI/MCP contracts and defaults remain the compatibility baseline. +Downloaded artifacts, runtime copies, exported Evidence, transcripts and detailed +JSON measurements belong outside the checkout. This document keeps the measured +findings, verification scope and revision references; the scripts and `corpus.json` +keep the reproduction inputs. Generated `results/` is ignored. + +The 12 detailed checkpoint receipts were archived byte-for-byte before removal +from the final PR tree. The archive `review-receipts-00d51e0fc-20261012` is in the +private experiment lab outside the checkout; its `archive-index.json` records +source revision, filenames, sizes and SHA-256. +Raw producer replies, model transcripts and exported Evidence remain in the same +private lab. These local files are not required to build or use REA. Historical +checkpoint commits retain their original receipts; no history was rewritten. + +## Optimization priorities and gradual migration + +Agent token consumption and end-to-end task time are the primary objectives. +Memory and CPU consumption are also measured and controlled. Correctness, +Evidence provenance, caller choices and existing-user compatibility are +acceptance conditions for every optimization. + +- Tokens: minimize unnecessary model-visible data while returning the requested + facts and useful provenance inline. Count discovery, repeated calls, errors, + retries and follow-up turns; report total/cached/uncached input and output + separately. Compact JSON bytes are a delivery measurement, not a token count. +- Time: measure completion of the same analyst question, including startup, + provider work, transport, client processing and model corrections. Separate + measured stages and cold/warm state; use repeated comparable trials before + generalizing an observed latency improvement. +- Memory: record aggregate process-tree peaks and limit/cleanup events, account + for retained Evidence and representation expansion, and keep host safeguards. +- CPU: record aggregate CPU time for the completed task. A one-CPU quota is an + experiment control; it does not itself establish reduced CPU consumption. + +Migrate domain by domain with focused real-artifact checks and relevant shared +contract checks. The domain matrix is a growing roadmap; native PE and DOS, for +example, remain unverified in this study. A passed lane does not imply all +advertised platforms/providers have passed. Newly discovered REA bugs remain +in the separate local handoff document for later team repair. + +Old clients using existing CLI/MCP requests against the candidate retain the +complete default path. New summary/view options require explicit selection. +Actual old Evidence import/view/export checks preserve original authenticated +records and identities. These checks establish the recorded baseline/workflow +scope, not every historical release or third-party client. A new client targeting +an older server must discover the actual schema and use a supported equivalent +where available; this study does not implement a universal automatic fallback. +The catalog intentionally gains more precise/additive schemas, so its bytes and +digest are not frozen: measured runtime `259e99dd9` has about 2.6% larger complete +catalog JSON than the frozen baseline. The separate post-integration receipt +records the newer runtime, including upstream changes. Optional views can reduce +model-visible result data; old complete requests do not silently switch delivery +or acquire that reduction automatically. + +## Reproduce a measurement + +Use the repository's pinned Node/npm and locked dependencies. Build each selected +revision, then copy its `dist/`, `bridge/`, `scripts/`, `skills/`, `package.json` +and `package-lock.json` to a separate runtime directory before editing production +sources. That copy needs the matching locked dependencies. The initial frozen +baseline is `42b0ed126df7d5b908ac369715378d5e519051b7`; this study does not +silently substitute a newer `main` or the previous Code Mode experiment. + +The public artifacts and acquisition digests are in `corpus.json`. Supply their +actual local paths and an optional JSON object of explicit provider environment +settings. Do not add credentials to that file. Provider settings are inherited +by the child, but the report persists only the selected settings' digest. Runs +reject undeclared inherited REA/Hopper and Ghidra/Java configuration keys before +starting a measured server or creating a model profile; include those settings +explicitly in `--providers`. Both drivers use `provider-environment.json` and +record its digest. The system/runtime environment still needs comparable +controlled conditions; its contents are not serialized. Both drivers also +fingerprint the effective provider `PATH` without recording its value, and +the SDK comparator rejects different or unrecorded search paths. This detects different +search configuration, not replacement of executable bytes at the same path. +Historical receipts +predate this guard, so their selected-settings digest alone does not certify +that no undeclared provider setting was inherited. + +```sh +export NODE_OPTIONS=--max-old-space-size=512 +export UV_THREADPOOL_SIZE=1 +export GOMAXPROCS=1 +export GOMEMLIMIT=2GiB +python3 experiments/analysis-efficiency/resource-run.py -- \ + node experiments/analysis-efficiency/measure.mjs \ + --runtime-root /absolute/frozen-runtime \ + --revision 42b0ed126df7d5b908ac369715378d5e519051b7 \ + --artifact obsidian.asar --path /absolute/obsidian-1.14.4.asar \ + --providers /absolute/selected-providers.json \ + --output /absolute/new-measurement-directory +``` + +The wrapper requires Linux cgroup v2 and a working user systemd manager. It owns +a unique transient scope, restricts the whole process tree to one CPU, caps +memory at 4 GiB with no swap and 192 tasks, and samples memory/tasks/CPU every +100 ms. It stops its own scope above 3 GiB or below 4 GiB host available memory; +startup requires 8 GiB available. Cleanup failures retain the owned scope's +identity. Run heavy builds, providers and model trials serially. Sampled peaks +can miss short spikes; cgroup limit events are separately reported. + +An actual Codex + Chromium trial exceeded the initial 192-task allowance and +was stopped automatically below 1 GiB sampled memory, with no OOM. The isolated +browser model retry uses `resource-run.py --task-ceiling 256 -- COMMAND...` to +account for both process trees; memory, swap and CPU controls are unchanged. +Keep the original aborted run and cleanup receipt. This is experiment capacity, +not a REA defect or a successful browser analysis. + +## What the numbers mean + +`summary.json` records the selected artifact's before/after SHA-256, runtime +file-table digest, dispatcher and lockfile digests, runner source digest, +Node/SDK versions, catalog sizes, caller arguments and producer Evidence IDs. +`runtime-files.json` identifies the exact compiled files measured. Actual +provider settings and runtime files must stay fixed during a comparison. + +Every tool call measures three intervals at the public SDK Transport boundary: + +- Client work before `send`, including first-use schema preparation. +- Transport/server roundtrip, including framing, I/O, server work and client + JSON parsing. This interval is **not** a Ghidra-only or provider-only timer. +- Client work after the parsed response reaches `onmessage`. + +The SDK call's total excludes the runner's subsequent encoding and disk writes. +Result byte counts measure compact JSON of the parsed client result, not raw +wire bytes or tokens. Progress notifications retain their actual arrival times; +they do not imply more precise internal engine timings than the producer emits. +Scope CPU time and aggregate process-tree memory come from the wrapper's final +JSON line. Fresh processes have cold application caches; host page caches are +not globally flushed. + +`--delivery summary` is an explicit candidate workflow, initially for artifact +inventory. It must be supported by the selected server's advertised schema. +The default `complete` runs the existing calls unchanged. Compare the same +analyst facts and complete source Evidence before crediting a smaller response. +Projection can reduce delivery bytes without reducing upstream analysis time. +For SQLite WASM, summary delivery reads the completed retained result through +the summary and exports views after preserving the original transport error. +`--javascript-question selected` analyzes only the exact Obsidian sources +`package.json` and `main.js`, keeps inventory identity, and reads each observed +`main.js` module by its exact node ID. Package metadata is not a module selector. + +Compare paired runs with the same runner source and provider settings: + +```sh +node experiments/analysis-efficiency/compare.mjs \ + --left /absolute/complete-run --right /absolute/summary-run \ + --output /absolute/new-comparison.json +``` + +This checks all selected analyst facts, including inventory source IDs, exact +path records, manifest, counts, completeness, contradictions, coverage, +unexplored branches and limitations. It excludes only delivery observation IDs +and the named recovery-path description. Different settings, changed facts or +failed workflows make the comparator fail. Binwalk's per-invocation region UUIDs +are separately preserved with both original values and JSON paths; only those +opaque lookup values are excluded from physical-region fact equality. Original +producer replies are never rewritten. Selected JavaScript comparisons likewise +preserve both values of the two named parent-heap-dependent byte budgets before +excluding their values from fact equality; fixed limits and coverage rows remain +checked. Counts include extra view calls and +observed tool errors. Use repeated runs with alternating order before claiming +a distribution or a speedup; one pair is only one observation. + +For a completed WASM result exceeding the MCP response budget, the runner keeps +the original error, exports retained Evidence through the public tool, and +authenticates the exact reported record using the selected runtime's parser. +The report names this recovery; it does not relabel the oversized inline reply +as successful delivery. + +## Initial observations and exclusions + +The frozen baseline has 2,326,609 bytes of complete catalog JSON, of which +494,207 bytes describe names, descriptions and input schemas. One Obsidian +trial spent about 21.9 s before the first request was sent, then about 0.77 s +in that request's transport/server roundtrip. Other cold runs differed +substantially. These single observations identify a measurement boundary; +they do not establish a speedup or a distribution. + +The initial matrix exercised shipped Obsidian, NewPipe, ShareX, OpenWrt, SQLite +WASM and jq artifacts. SQLite produced a complete result larger than the +transport frame and requires an explicit recovery run. A first jq attempt hit +the SDK's default 60 s timeout because the experimental runner used a superseded +three-argument call signature. SDK 2.3.1 accepts request options as argument two; +the corrected jq run completed real Ghidra analysis in about 265 s. That earlier +driver timeout is excluded from REA regression claims. Provisional runs with +different runner source digests are not one paired statistical cohort. + +No cross-session engine database cache, default analyzer changes, external +plugin loading, or broad plugin SDK is enabled by this experiment. + +## Obsidian delivery checkpoint + +The two recorded pairs compare frozen baseline +`42b0ed12` and candidate `f1106181` in complete → summary and summary → complete +order using the same runner source, bytes and provider settings. Both preserve +every selected fact and the atomic inventory Evidence ID. + +| Measurement | Complete | Summary + exact path view | +| --------------------------------------------------- | --------------: | ------------------------: | +| Tool-result compact JSON, including lifecycle calls | 937,404 bytes | 16,322 bytes | +| Tool calls per question | 3 | 4 | +| Complete catalog compact JSON | 2,326,609 bytes | 2,370,505 bytes | +| Workflow elapsed, first pair | 11.04 s | 10.23 s | +| Workflow elapsed, second pair | 10.57 s | 10.18 s | + +Reply JSON falls by about 98.3%; catalog JSON grows by about 1.9%. The two elapsed +observations do not establish a statistical speedup. Inventory coverage and +upstream byte acquisition are unchanged. The candidate remains opt-in; existing +requests continue to deliver the complete inspection. Each run verifies the +selected artifact's before/after digest and reports no observed OOM or task +limit events. Source typing and projection improve selected delivery without +introducing an engine database cache or an external plugin protocol. + +## Task-scoped analyzer experiment + +The recorded jq pair explicitly disables +`Decompiler Switch Analysis` through the existing `REA_GHIDRA_DISABLED_ANALYZERS` +setting and compares it with the unchanged Ghidra default. Both use the same +frozen runtime and real jq bytes, one CPU, a fresh ephemeral database and the +same entry/main question. Completed main dossiers exceed the MCP frame in both +runs; existing retained native views recover the selected facts, preserving the +observed transport errors in the reports. + +One disabled/default pair takes 174.77/276.23 seconds. Entry and main pseudocode, +main strings and its block count agree. However, callee `0x40daa0` has 604 +recovered body bytes in eight ranges by default, and 164 bytes in four ranges +with the same 646-byte span after disabling switch analysis. Equal top-level +counts do not establish call-graph equivalence. **Keep the production default.** +Use the existing control only for a caller-selected task whose required facts +have been checked; no general fast preset is introduced. A separate real +coreutils/Ghidra probe rejects a nonexistent analyzer before bridge readiness, +preserves the named failure and closes the session. Source bytes remain unchanged. + +`--schemas selected` is a separate SDK 2.3.1 client experiment. It passes the exact +live named `tools/list` definition to the SDK's public `callTool` option, retaining +output validation. It does not change REA or the default runner mode. Selected +definitions compile per call rather than preparing the entire cached catalog; +report this client effect separately from engine analysis or model behavior. + +## Real Codex CLI trials + +`agent-trial.py` accepts an explicit frozen runtime and revision, the same +question templates in `questions/`, and optional explicit provider settings. +It fingerprints the actual runtime, runs pinned local Codex CLI with a private +temporary profile, copies only the authentication file with mode 0600, disables +shell/web/multi-agent tools, and removes copied profile state afterward. Raw +events and answers remain outside Git. A zero exit code is not a factual grade. +Review the original tool calls, failed requests, result limitations and answer. + +The optional `--enabled-tools FILE` supplies a list of exact names to the +temporary Codex MCP configuration's `enabled_tools`, as described in the +[official MCP configuration](https://learn.chatgpt.com/docs/extend/mcp?surface=cli). +It leaves the REA catalog and other clients intact. Initial Obsidian trials +showed parameter-correction attempts and different delivery choices even with +the same question; filtering alone has not established a cost or quality win. +Requested model aliases are recorded without inventing a resolved model identity. + +The browser question uses `--authority browser` to permit only its explicitly +selected public scenario in a fresh, owned headless profile. Browser observations +and cleanup are graded separately from static artifact analysis. + +## Real artifact compatibility checkpoint + +Six frozen baseline/pilot pairs exercise +Obsidian, NewPipe, ShareX, OpenWrt, SQLite WASM and published historical WETH code +through public MCP tools. All selected facts agree under the stated comparison +rules. Obsidian complete delivery and atomic inventory Evidence identity remain +unchanged. ShareX native-boundary claims remain scoped to the selected DLL; +NewPipe retains a null DEX descriptor rather than inventing one. + +SQLite's complete 15,382,128-code-unit WAT exceeds the MCP response budget in both +revisions. The control recovers complete Evidence through file export; the pilot +reads summary and four exact export forms inline, without running WABT again. +Inline byte totals exclude file exports, so they do not establish a size win. +OpenWrt comparison preserves both sets of upstream-generated region UUIDs before +comparing offsets, extents, signatures, coverage and limitations. WETH is a +published historical example; a live RPC request returned HTTP 403, so this is +not live-chain verification. Every pair records unchanged target bytes and zero +observed OOM/task-limit events. The Linux x64 results do not establish behavior +for IDA, Hopper, macOS, Windows or Harmony. + +## Same-question model and selected-source follow-up + +Two actual SQLite model trials preserve all +12 requested facts. The question, developer instruction, provider settings and +tool allowlist hashes match. A description-only follow-up supplies the exact +summary kind and its fields. Calls change from 42 to 3, workflow time from +292.64 to 63.79 seconds, and reported input tokens from 304,698 to 74,387. The +follow-up reads no unnecessary WAT pages. Both keep the original completed +analysis's transport error and authenticate the retained source/view identities. +This is one stochastic model pair, not a statistical or general causal claim. + +The selected JavaScript pair preserves +all 24 observed `main.js` module identities and the exact two-source selection. +The strict comparison initially differs on parent budget values derived from +observed heap availability; both values and paths remain recorded separately. +Other coverage facts, per-family rows, fixed worker limits, omitted/unknown +counts, statistics and Electron observations agree. Coverage remains partial, +with 36 unselected relevant files and 384 semantic unknowns. These static facts +do not establish runtime behavior. The comparator does not rewrite Evidence. + +Ten real CLI trial reviews cover native, +managed, Android, firmware, Electron/JavaScript, WASM, EVM and browser tasks. +The selected JavaScript model preserves core facts and partial coverage but +omits full budget/family provenance. The browser producer captures complete +DOM/accessibility/URL evidence and terminates its owned process; direct model +answers lose two accessibility metadata fields to client context clipping and +report them unknown. A requested under-development Codex code-mode configuration +still produces a direct MCP call in the actual trace, so no executor/composition +win is credited. Profile cleanup, original errors, actual calls, usage, facts and +scope limits remain independently reviewed. Enabling a configuration flag does +not prove that a capability was used. + +Discovered REA defects are recorded separately for team repair at the user's +request. This follow-up performs no additional REA bug fixes. + +## Nonempty managed boundaries and final verification + +A second shipped managed library checks +11 ModuleRefs, 147 PInvoke declarations and 225 native implementations from +ShareX HelpersLib 19.0.2. All complete normalized artifact and native-boundary +fields match between baseline and pilot, including source Evidence identities. +This covers nonempty declarations beyond the main ShareX DLL's empty import +tables. It does not verify native exports, execute the assembly or resolve CLR +dependencies. The curated receipt keeps identities, coverage, all ModuleRefs, +the first three PInvoke records and digests of the complete boundary payloads. +Full producer tables and comparison inputs stay in the private lab outputs. + +The earlier verification includes 42 real +WABT CLI/MCP cases and the complete real SQLite CLI JSON: the unchanged default +returns the same original source Evidence ID, normalized result and raw result +as the baseline exported MCP record. Source bytes are unchanged. The seven-file +contract/composition run passes 51/52 assertions and hits the existing 20-second +presentation-test deadline while sharing one CPU. That same unmodified assertion +passes alone in 10.835 seconds. The original combined failure and isolated pass +remain recorded; no test deadline or REA bug was patched. Build, type/lint/module +checks and generated-document checks pass. No full `check:pr` or unverified +provider/platform coverage is implied. + +## Actual old Evidence migration + +The migration experiment imports the original +baseline SQLite Evidence bundle into the pilot runtime, reads a typed summary +and four export forms, and exports the retained bundle again. The complete old +WASM record is deeply equal after import/export, with its original authenticated +Evidence ID. The original baseline Obsidian complete inspection also works as +inline input to the new inventory summary, retaining the old parent and atomic +inventory identities and manifest. Original input files remain byte-identical. + +The runtime explicitly selects an absent WABT directory and requests no producer +operation. These views consume existing Evidence without another provider run; +this is portable-result reuse through existing import/export, not an engine +database cache. The five-call workflow completes in 5.458 seconds with a sampled +aggregate peak of 700,542,976 bytes and no observed limit or OOM events. + +To repeat it with genuine old public outputs: + +```sh +python3 experiments/analysis-efficiency/resource-run.py -- \ + node experiments/analysis-efficiency/verify-migration.mjs \ + --runtime-root /absolute/frozen-pilot-runtime \ + --revision 259e99dd906290189217cf0fb1a19769bcf87101 \ + --bundle /absolute/baseline-exported-evidence.json \ + --inspection-reply /absolute/baseline-complete-inspection-reply.json \ + --output /absolute/new-migration-directory +``` + +## PR integration verification + +The separate post-integration verification pins runtime +`0eb42a1eb8ab17aed04240174cb77612e733a1d8`, integrated with main +`c22111eca2c4bc84a084f12711f405b3e7422a40`. Conflict resolution preserves main's +HAR API-surface views, portable capture admission, exact layout/JavaScript +summary carriers and existing CLI tests alongside inventory/WASM views. +Earlier real-model observations keep their original runtime fingerprints. + +Build, the complete `check` static gate and generated documentation checks pass. +Twelve focused files pass 123 assertions, including the presentation test run +separately with its original deadline. Actual package creation, private-prefix +installation and 42 real WABT CLI/MCP cases pass. No package is published. +The complete generic package verifier and full local `check:pr` were not run. + +Genuine old SQLite/Obsidian Evidence migration passes again with WABT unavailable. +A fresh public SDK cohort preserves the original Obsidian default result size +(937,404 compact JSON bytes), atomic inventory identity and every selected fact; +explicit summary plus exact paths still returns 16,322 bytes. Complete catalog +JSON is 2,359,082 bytes versus the original baseline's 2,326,609 (about 1.4% +larger). That difference includes intervening main changes and does not isolate +this PR's catalog contribution. One cohort is not a latency distribution. + +Recorded integration scopes have no OOM/OOM-kill or hard-limit abort. The full +source-check scope reports three `memory.high` events near its 3 GiB soft +threshold. The subsequent cold pre-push type/lint scope reports 37 such events, +with 3,221,155,840 sampled peak bytes and no hard-limit/OOM event. Heavy lanes stay +serial with the existing CPU, memory and no-swap controls. No further REA bug repairs +are included; separate local findings remain for team follow-up. + +## CI attribution + +Review of the actual main and PR job logs separates two PR-related +description/test mismatches from pre-existing main failures. The complete-graph +wording check now accepts capitalization changes; the existing effects audit +records why returning WASM execution/network metadata is not foreign execution. +Those two files pass 11 focused assertions without production changes. Main's +own CI already fails the same live prompt-completion assertion and Darwin +packaged-completion checks; the affected implementation and fixture files are +byte-identical here. Those failures remain unchanged for team review at the +maintainer's request. CI failures are not all infrastructure faults, and the +failed CI gate is not presented as passed. + +| Failure | Main `c22111eca` | Reviewed PR `8d6f7eb1c` | +| ---------------------------------------- | ---------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------- | +| Empty prompt completion after open | [main shard](https://github.com/morluto/rea/actions/runs/38224038323/job/114727940506) | [PR shard](https://github.com/morluto/rea/actions/runs/38228724531/job/114742955092) | +| Packaged prompt completion, Darwin arm64 | [main package](https://github.com/morluto/rea/actions/runs/38224038323/job/114728030045) | [PR package](https://github.com/morluto/rea/actions/runs/38228724531/job/114743092314) | +| Packaged prompt completion, Darwin x64 | [main package](https://github.com/morluto/rea/actions/runs/38224038323/job/114728029923) | [PR package](https://github.com/morluto/rea/actions/runs/38228724531/job/114743092079) | + +`src/server/promptCompletion.ts`, `src/application/binary/BinarySession.ts`, +`tests/boundary/mcp/promptCompletion.test.ts` and +`scripts/verify-package-prompts.mjs` are byte-identical to that main revision. +Main commit `07b36c148946f46b99a4ce04e55726e97d0ee03a` introduces ready-provider-only +completion; the failing fixtures expect populated completion immediately after +opening. Whether those expectations or the readiness behavior should change is +left for team review. Commit `00d51e0fcd1d1bba60bf56281725e82d525a27ae` +synchronizes only the two PR-related description checks. + +## Frozen runtime references + +### Review follow-up + +Two substantive bot findings were reproduced and addressed before merge. Atomic +inventory projections now reuse the owning subject/digest/format validation. +Actual public SDK calls with original Obsidian data demonstrated the old +acceptance of null subjects and inconsistent formats; the corrected runtime +rejects both with `invalid_request`. Six boundary assertions also cover missing +subject, wrong format and wrong digest across summary and node views. + +The related three-file run passes 37 assertions, and full static/docs gates +pass. Genuine baseline SQLite/Obsidian import/view/export still preserves whole +original records and source/view identities with WABT unavailable. A new real +Obsidian summary workflow preserves every selected fact, atomic source identity +and 16,322 delivered JSON bytes; catalog bytes remain 2,359,082. + +Both experimental drivers reject undeclared inherited provider controls before +server/profile creation and log no values. Explicit settings still override +the corresponding inherited values. This prevents claiming equal declared +settings while silently inheriting a different analysis option; it does not +certify every aspect of the inherited system/runtime environment. These are +research-driver controls, with no production configuration changes. + +The source/static scope records 98 `memory.high` events, no hard limit/OOM event, +and a sampled peak of 3,221,209,088 bytes. The real migration/Obsidian scope peaks +at 677,367,808 bytes with no limit/OOM events. All raw receipts remain outside Git. + +A later WASM finding was also reproduced: portable sources with absent subject +or a different subject path could report another artifact path. The producer and +view now share an exact path/digest identity rule. Actual WABT inspection of the +published VS Code Tree-sitter INI grammar (0.3.1, 4,716 bytes, SHA-256 +`1e8cdf3027209100fa54a43d0faed69721ae1116428164c8ad6dcc496a9aa6af`) +supplied the unchanged report for public SDK before/after checks. The old runtime +accepts both inconsistencies; the corrected runtime rejects both with +`invalid_request` and WABT unavailable. Its 26 focused assertions and type/lint +checks pass, and genuine old records still round-trip unchanged. The verification +scope peaks at 3,204,509,696 bytes with no observed limit/OOM event. The initial +CSS producer exceeded the MCP reply budget as expected; that failed probe is +preserved separately and is not credited as a completed identity reproduction. + +These revisions identify different experiment stages; model measurements were +not rerun or relabeled as post-integration measurements. + +| Stage | Source revision | +| ---------------------------------------------- | ------------------------------------------ | +| Original baseline | `42b0ed126df7d5b908ac369715378d5e519051b7` | +| Inventory delivery pairs | `f110618184e99f89fa776bc5e59703a61baa7c57` | +| Initial WASM pilot | `0002caa07ffd7319672a21ce901dbfbc55e21aac` | +| Exact WASM summary guidance | `259e99dd906290189217cf0fb1a19769bcf87101` | +| Main-integrated package/migration verification | `0eb42a1eb8ab17aed04240174cb77612e733a1d8` | + +The baseline runtime file-table SHA-256 is +`3a1765a6fec0031db0b288112d4db9bc0dd503c9c3aa0946f07ce57f7673ab4b`; +the integrated runtime file-table SHA-256 is +`e99d3bdc851822b77e7d5bfaa9b60c57e7b99bd66e27a739bdea2e9300560d5f`. +The integrated lockfile SHA-256 is +`0a55042141899a442a39dce3d49755c65a861f86d1930bd4f9c6d89cd53f5eba`. +Actual old migration inputs have SHA-256 +`e1ff3d28801436bcc11693f79cc847a4f31234f49d88449f9e0d77215c3765fc` +(SQLite bundle) and +`7cecd07012bb79f3afad2ea9e70357e056e9eab48c1aa13bbbd310cdcb43eb9f` +(Obsidian complete inspection reply). The source WASM identity +`ev_16b9f6e0fcefcab9603f14fd5e63ea00c4749accd94fef4a4e9fbb260d2e0140` +and atomic inventory identity +`ev_22c17e52489f3ec0e7298a62da37bd985dbbec202d7208a0bb1c20d863790aed` +remain unchanged after the recorded import/view/export workflows. + +### Complete carrier identity and provider search configuration + +The outer `inspect_artifact` carrier now uses the same owning subject/digest/format +validator as its atomic inventory. Invalid imported/inline carriers are rejected +before selected-view construction; the generic historical Evidence parser and +valid complete records are unchanged. Six additional boundary assertions cover +missing subject, wrong format and wrong digest across summary/node projections. +The negative checks use the small keyed-archive workflow so the original large +ZIP retention/capacity workflow keeps its existing deadline and distinct coverage. +An initial large-workflow placement timed out and is retained as a failed attempt. +The three public MCP workflows, build, fast static gate and generated docs pass. +The final static/test scope observed one soft `memory.high` event, with no hard +memory, task-limit, OOM or OOM-kill event; it is not a performance trial. + +Both research drivers fingerprint the effective provider `PATH`, including an +explicit caller override, without serializing its value or the ambient environment. +The SDK comparator rejects absent or unequal search-path fingerprints. This +records search configuration, not executable replacement at an unchanged path; +provider/runtime identities and controlled host conditions still matter. + +Actual public SDK calls using the original shipped Obsidian inspection reproduce +accepted null/wrong-format outer subjects on frozen `2ef0467bb` and `invalid_request` +on the corrected compiled runtime. Two fresh Obsidian replays with different +inherited search paths preserve selected facts and source identities, but the +comparator rejects matching settings with only `provider_search_path_sha256` +different. The original records and failed/accepted replies stay in private lab +receipts; no environment value is copied into them. + +### Resource-counter closeout qualification + +The experimental wrapper now attempts a final cgroup event read before waiting +or cleanup. Disappeared/incomplete counters or still-owned descendants make +`resource_counters_verified` false; last observations remain available separately. +The model grader treats missing final counters as unverified, never as zero, +and any recorded soft/hard/task-limit event prevents an unrestricted resource +pass. Lightweight parser probes cover missing/incomplete and nonzero counters; +an actual short command confirms disappearance is reported as unknown. Regrading +one already-correct SQLite model answer preserves its facts but rejects the old +receipt's absent final resource counters. Earlier receipts record last-sampled +observations, not verified final counters. Their bytes, facts and token reports +remain original observations; resource-qualified performance conclusions need +this additional closeout evidence. No analyzer/build/schema/package behavior is +changed, so unrelated verification is not repeated for this research-driver fix. diff --git a/experiments/analysis-efficiency/agent-trial.py b/experiments/analysis-efficiency/agent-trial.py new file mode 100644 index 000000000..8392000ca --- /dev/null +++ b/experiments/analysis-efficiency/agent-trial.py @@ -0,0 +1,278 @@ +"""Evaluate one real Codex CLI task with an isolated REA MCP registration.""" + +import argparse +import hashlib +import json +import os +from pathlib import Path +import shutil +import signal +import subprocess +import tempfile +import time + + +def file_digest(path): + digest = hashlib.sha256() + with path.open('rb') as data: + for chunk in iter(lambda: data.read(1024 * 1024), b''): + digest.update(chunk) + return digest.hexdigest() + + +def runtime_fingerprint(root): + files = [] + for name in ['dist', 'bridge']: + for path in sorted((root / name).rglob('*')): + if path.is_symlink(): + raise ValueError('Runtime fingerprint does not follow symlinks') + if path.is_file(): + files.append({'path': path.relative_to(root).as_posix(), 'sha256': file_digest(path)}) + files.sort(key=lambda row: row['path']) + text = json.dumps(files, separators=(',', ':'), ensure_ascii=False) + return files, hashlib.sha256(text.encode()).hexdigest() + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument('--mode', choices=['direct', 'client-code'], default='direct') + parser.add_argument('--runtime-root', type=Path, required=True) + parser.add_argument('--revision', required=True) + parser.add_argument('--prompt', type=Path, required=True) + parser.add_argument('--artifact-path', help='Replace {{artifact_path}} in a question template') + parser.add_argument('--output', type=Path, required=True) + parser.add_argument('--model', default='gpt-5.6-terra') + parser.add_argument('--reasoning', default='high') + parser.add_argument('--timeout', type=int, default=900) + parser.add_argument('--codex', default='/usr/bin/codex') + parser.add_argument('--providers', type=Path) + parser.add_argument('--enabled-tools', type=Path) + parser.add_argument('--authority', choices=['static', 'browser'], default='static') + args = parser.parse_args() + def terminate(signum, _frame): + # The outer resource scope terminates descendants together. Ensure the + # runner's copied credentials are also removed on a resource abort. + raise SystemExit(128 + signum) + signal.signal(signal.SIGTERM, terminate) + if not args.runtime_root.is_absolute() or not args.output.is_absolute(): + raise ValueError('Runtime and output paths must be absolute') + repository = args.runtime_root + driver_repository = Path(__file__).resolve().parents[2] + experiment = Path(__file__).resolve().parent + node = shutil.which('node') + if subprocess.check_output([node, '--version'], text=True).strip() != 'v24.18.0': + raise SystemExit('Use pinned Node 24.18.0 on PATH') + provider_env = {'REA_LOG_LEVEL': 'silent', 'NODE_OPTIONS': '--max-old-space-size=512'} + if args.providers: + declared = json.loads(args.providers.read_text()) + if not isinstance(declared, dict) or not all( + isinstance(value, str) for value in declared.values() + ): + raise ValueError('--providers must contain an object of explicit string settings') + provider_env.update(declared) + provider_policy_text = (experiment / 'provider-environment.json').read_text() + provider_policy = json.loads(provider_policy_text) + undeclared = sorted(key for key in os.environ if ( + any(key.startswith(prefix) for prefix in provider_policy['namespace_prefixes']) + or key in provider_policy['additional_keys'] + ) and key not in provider_env) + if undeclared: + raise ValueError('Declare inherited provider settings explicitly in --providers: ' + + ', '.join(undeclared) + '. Values are not logged.') + args.output.mkdir(parents=True, exist_ok=False, mode=0o700) + runtime_files, runtime_digest = runtime_fingerprint(repository) + (args.output / 'runtime-files.json').write_text(json.dumps(runtime_files)) + profile = Path(tempfile.mkdtemp(prefix='profile-', dir=args.output)) + trial_codex_home = profile / 'codex' + account = profile / 'account' + workspace = profile / 'workspace' + for directory in [trial_codex_home, account, workspace]: + directory.mkdir(mode=0o700) + source_auth = Path(os.environ.get('CODEX_HOME', str(Path.home() / '.codex'))) / 'auth.json' + auth = trial_codex_home / 'auth.json' + started = time.monotonic() + timeout = False + exit_code = None + environment = dict(os.environ) + environment.update({ + 'CODEX_HOME': str(trial_codex_home), 'HOME': str(account), + 'XDG_CONFIG_HOME': str(account / '.config'), + 'XDG_CACHE_HOME': str(account / '.cache'), + 'XDG_DATA_HOME': str(account / '.local/share'), + 'NODE_OPTIONS': '--max-old-space-size=512', + }) + target_digest = file_digest(Path(args.artifact_path)) if args.artifact_path else None + command_args = [str(repository / 'scripts/rea.mjs'), 'mcp'] + instruction = ( + 'Use the registered REA MCP server for this investigation. Static analysis ' + 'only: do not execute target code, fetch URLs, launch a GUI, or modify targets. ' + 'Preserve observations, derivations, inferences, limitations, and real Evidence IDs. ' + 'An Evidence value has evidence_id, normalized_result and limitations; ' + 'non-Evidence session values have result. ' + 'The analyzed target digest is Evidence.subject.digest.sha256. ' + 'normalized_result.engine.artifact_sha256 identifies the analysis engine ' + 'artifact and must never be substituted for the target digest. ' + 'Prefer summary/focused views where appropriate. Do not invent successful facts ' + 'after a tool error. No other agents. Stop once the requested facts are supported.' + ) + if args.authority == 'browser': + instruction = ( + 'Use the registered REA tools for the explicitly selected public browser scenario. ' + 'Launching the selected headless browser with a fresh empty profile and observing ' + 'the supplied public URL is authorized. Keep all actions within that scenario. ' + 'No injected code, credentials, unrelated targets, shell/file tools or other agents. ' + 'Preserve real Evidence IDs, observation completeness, failed captures and limitations. ' + 'Do not infer original source or global behavior from one observed session.' + ) + if args.mode == 'client-code': + instruction += ( + ' Use the client JavaScript executor to compose registered MCP calls when ' + 'possible and select the requested normalized_result fields before returning ' + 'intermediate output to model context. Keep real Evidence IDs and limitations.' + ) + enabled_tools = None + if args.enabled_tools: + enabled_tools = json.loads(args.enabled_tools.read_text()) + if not isinstance(enabled_tools, list) or not all(isinstance(name, str) and name for name in enabled_tools) or len(enabled_tools) != len(set(enabled_tools)): + raise ValueError('enabled-tools must be a unique list of exact tool names') + quote = json.dumps + config = '\n'.join([ + 'approval_policy = "never"', 'sandbox_mode = "read-only"', + 'web_search = "disabled"', 'model = ' + quote(args.model), + 'model_reasoning_effort = ' + quote(args.reasoning), + 'developer_instructions = ' + quote(instruction), + '[features]', 'multi_agent = false', 'shell_snapshot = false', + 'shell_tool = false', 'unified_exec = false', + 'code_mode = ' + ('true' if args.mode == 'client-code' else 'false'), + 'code_mode_only = ' + ('true' if args.mode == 'client-code' else 'false'), + '[history]', 'persistence = "none"', + '[mcp_servers.rea]', 'command = ' + quote(node), + 'args = ' + quote(command_args), 'startup_timeout_sec = 60', + 'tool_timeout_sec = 600', + *([] if enabled_tools is None else ['enabled_tools = ' + quote(enabled_tools)]), + '[mcp_servers.rea.env]', + *[key + ' = ' + quote(value) for key, value in provider_env.items()], + ]) + '\n' + # MCP read-only hints cover normal static tools. Explicitly authorize only + # the session-opening primitive needed by the requested native tasks. + config += '[mcp_servers.rea.tools.open_binary]\napproval_mode = "approve"\n' + for tool in ['analyze_javascript_application', 'inspect_analysis_view', + 'inspect_android_package', 'search_android_classes', + 'inspect_android_class', 'inspect_android_method', + 'inspect_firmware_regions', 'inspect_managed_artifact', + 'inspect_managed_members', 'inspect_managed_native_boundaries', + 'inspect_artifact', 'inspect_wasm_artifact', + 'inspect_evm_interface', 'get_evidence_bundle', 'export_evidence_bundle', + 'capture_browser_scenario', + 'resolve_containing_procedure', 'analyze_function', 'close_binary']: + config += '[mcp_servers.rea.tools.' + tool + ']\napproval_mode = "approve"\n' + try: + if source_auth.exists(): + shutil.copyfile(source_auth, auth) + auth.chmod(0o600) + (trial_codex_home / 'config.toml').write_text(config) + (trial_codex_home / 'config.toml').chmod(0o600) + prompt = args.prompt.read_text() + template_digest = hashlib.sha256(prompt.encode()).hexdigest() + if '{{artifact_path}}' in prompt: + if not args.artifact_path or not os.path.isabs(args.artifact_path): + raise ValueError('A question template requires an absolute --artifact-path') + prompt = prompt.replace('{{artifact_path}}', args.artifact_path) + elif args.artifact_path: + raise ValueError('--artifact-path requires the literal template marker') + (args.output / 'prompt.txt').write_text(prompt) + (args.output / 'trial-settings.json').write_text(json.dumps({ + 'mode': args.mode, 'model': args.model, 'reasoning': args.reasoning, + 'driver_commit': subprocess.check_output( + ['git', 'rev-parse', 'HEAD'], cwd=driver_repository, text=True).strip(), + 'runtime_revision': args.revision, + 'runtime_root': str(repository), + 'selected_artifact_path': args.artifact_path, + 'selected_artifact_sha256_before': target_digest, + 'enabled_tools': enabled_tools, + 'authority': args.authority, + 'runtime_files_sha256': runtime_digest, + 'runtime_dispatcher_sha256': file_digest(repository / 'scripts/rea.mjs'), + 'package_lock_sha256': file_digest(repository / 'package-lock.json'), + 'experiment_sources_sha256': { + name: hashlib.sha256((experiment / name).read_bytes()).hexdigest() + for name in ['agent-trial.py', 'provider-environment.json'] + }, + 'developer_instructions': instruction, + 'developer_instructions_sha256': hashlib.sha256(instruction.encode()).hexdigest(), + 'prompt_sha256': hashlib.sha256(prompt.encode()).hexdigest(), + 'question_template_sha256': template_digest, + 'provider_settings_sha256': hashlib.sha256( + json.dumps(provider_env, sort_keys=True).encode()).hexdigest(), + 'provider_search_path_sha256': hashlib.sha256( + provider_env.get('PATH', os.environ.get('PATH', '')).encode()).hexdigest(), + 'provider_environment_policy_sha256': hashlib.sha256( + provider_policy_text.encode()).hexdigest(), + 'features': {'shell_tool': False, 'unified_exec': False, + 'multi_agent': False, 'code_mode': args.mode == 'client-code', + 'code_mode_only': args.mode == 'client-code'}, + }, indent=2) + '\n') + invocation = [args.codex, '--no-daemon', 'exec', '--strict-config', '--ephemeral', + '--skip-git-repo-check', '--ignore-rules', '--json', + '--color', 'never', '-C', str(workspace), + '--output-last-message', str(args.output.resolve() / 'answer.txt'), '-'] + with (args.output / 'events.jsonl').open('w') as events, (args.output / 'stderr.log').open('w') as errors: + child = subprocess.Popen(invocation, env=environment, cwd=workspace, + stdin=subprocess.PIPE, stdout=events, stderr=errors, text=True) + child.stdin.write(prompt) + child.stdin.close() + try: + exit_code = child.wait(timeout=args.timeout) + except subprocess.TimeoutExpired: + timeout = True + child.terminate() + try: + exit_code = child.wait(timeout=10) + except subprocess.TimeoutExpired: + child.kill() + exit_code = child.wait() + usage = [] + tools = [] + event_types = {} + for line in (args.output / 'events.jsonl').open(): + if not line.strip(): + continue + event = json.loads(line) + kind = event.get('type', 'unknown') + event_types[kind] = event_types.get(kind, 0) + 1 + if 'usage' in event: + usage.append(event['usage']) + item = event.get('item', {}) + if item.get('type') == 'mcp_tool_call' and kind == 'item.completed': + tools.append({ + **{k: item.get(k) for k in ['server', 'tool', 'arguments', 'status', 'error']}, + 'codex_event_result_json_utf8_bytes': len(json.dumps( + item.get('result'), separators=(',', ':'), ensure_ascii=False + ).encode()), + }) + final_target_digest = file_digest(Path(args.artifact_path)) if args.artifact_path else None + summary = { + 'mode': args.mode, 'requested_model': args.model, 'reasoning': args.reasoning, + 'runtime_revision': args.revision, + 'selected_artifact_sha256_before': target_digest, + 'selected_artifact_sha256_after': final_target_digest, + 'selected_artifact_bytes_unchanged': target_digest == final_target_digest if target_digest else None, + 'model_identity_scope': 'requested alias; resolved server-side model identity is not inferred', + 'codex_version': subprocess.check_output([args.codex, '--version'], text=True).strip(), + 'exit_code': exit_code, 'timeout': timeout, + 'wall_seconds': round(time.monotonic() - started, 3), + 'usage': usage, 'completed_mcp_calls': tools, 'event_types': event_types, + 'factual_correctness': 'not_assessed', + 'scope': 'real_cli_trial_requires_transcript_and_fact_review', + } + (args.output / 'summary.json').write_text(json.dumps(summary, indent=2) + '\n') + print(json.dumps(summary)) + finally: + # Always remove copied credentials, config, caches and profile state. + shutil.rmtree(profile) + (args.output / 'cleanup.json').write_text(json.dumps({'profile_removed': not profile.exists()}) + '\n') + raise SystemExit(124 if timeout else exit_code) + + +if __name__ == '__main__': + main() diff --git a/experiments/analysis-efficiency/compare.mjs b/experiments/analysis-efficiency/compare.mjs new file mode 100644 index 000000000..8c386a4d9 --- /dev/null +++ b/experiments/analysis-efficiency/compare.mjs @@ -0,0 +1,157 @@ +import { isDeepStrictEqual } from "node:util"; +import { readFile, writeFile } from "node:fs/promises"; +import { resolve } from "node:path"; +import { parseArgs } from "node:util"; + +const { values } = parseArgs({ + options: { + left: { type: "string" }, + right: { type: "string" }, + output: { type: "string" }, + }, +}); +if (!values.left || !values.right) + throw new Error( + "Use --left MEASUREMENT_DIR --right MEASUREMENT_DIR [--output JSON]", + ); +const read = async (directory, name) => + JSON.parse(await readFile(resolve(directory, name), "utf8")); +const left = await read(values.left, "summary.json"); +const right = await read(values.right, "summary.json"); +const leftAnswer = await read(values.left, "answer.json"); +const rightAnswer = await read(values.right, "answer.json"); +const sameSettings = [ + "artifact", + "artifact_sha256", + "artifact_sha256_after", + "native_question", + "javascript_question", + "provider_settings_sha256", + "provider_search_path_sha256", + "provider_environment_policy_sha256", + "measure_source_sha256", + "package_lock_sha256", + "node_version", + "sdk_client_version", + "sdk_server_version", + "platform", + "architecture", +]; +const settingDifferences = sameSettings.filter( + (key) => + !isDeepStrictEqual(left[key], right[key]) || + (key === "provider_search_path_sha256" && + !/^[a-f0-9]{64}$/.test(left[key] ?? "")), +); +// Producer/view IDs identify different delivery observations; a named recovery +// describes the delivery path. All selected analyst facts still must agree. +const facts = (answer) => + Object.fromEntries( + Object.entries(answer).filter( + ([key]) => key !== "evidence_ids" && key !== "recovery", + ), + ); +const a = facts(leftAnswer); +const b = facts(rightAnswer); +const runSpecific = []; +// Binwalk assigns a new opaque region UUID in each invocation. Preserve both +// original lookup values in this report; compare the observed region facts +// separately. The product and original replies retain those IDs unchanged. +if (left.artifact === "openwrt.bin") { + for (const [side, answer] of [ + ["left", a], + ["right", b], + ]) { + for (const [index, region] of answer.result.regions.entries()) { + runSpecific.push({ + side, + path: ["result", "regions", index, "provider_id"], + value: region.provider_id, + }); + delete region.provider_id; + } + } +} +// JavaScript result budgets depend on observed parent heap availability. +// Preserve their exact values on both sides, then compare the coverage facts +// and fixed worker limits. Never exclude status, omitted counts or families. +if ( + left.artifact === "obsidian.asar" && + left.javascript_question === "selected" +) { + const dynamicBudgets = new Set([ + "javascript_analysis_parent_available_result_budget_bytes", + "javascript_analysis_parent_result_budget_bytes", + ]); + for (const [side, answer] of [ + ["left", a], + ["right", b], + ]) { + for (const layer of ["application", "semantic"]) { + for (const [index, limit] of answer.coverage[layer].limits.entries()) { + if (!dynamicBudgets.has(limit.name) || limit.unit !== "bytes") continue; + runSpecific.push({ + side, + path: ["coverage", layer, "limits", index, "value"], + name: limit.name, + unit: limit.unit, + value: limit.value, + }); + delete limit.value; + } + } + } +} +const factDifferences = [ + ...new Set([...Object.keys(a), ...Object.keys(b)]), +].filter((key) => !isDeepStrictEqual(a[key], b[key])); +const cost = (report) => ({ + revision: report.revision, + runtime_files_sha256: report.runtime_files_sha256, + delivery: report.delivery, + sdk_schema_mode: report.sdk_schema_mode, + elapsed_ms: report.elapsed_ms, + catalog_json_utf8_bytes: report.catalog_json_utf8_bytes, + calls: report.calls.length, + observed_tool_errors: report.calls.filter((call) => call.is_error).length, + client_result_json_utf8_bytes: report.calls.reduce( + (sum, call) => sum + (call.client_result_json_utf8_bytes ?? 0), + 0, + ), + client_before_send_ms: report.calls.reduce( + (sum, call) => sum + (call.client_before_send_ms ?? 0), + 0, + ), + transport_roundtrip_ms: report.calls.reduce( + (sum, call) => sum + (call.transport_roundtrip_ms ?? 0), + 0, + ), + client_after_receive_ms: report.calls.reduce( + (sum, call) => sum + (call.client_after_receive_ms ?? 0), + 0, + ), +}); +const comparison = { + artifact: left.artifact, + artifact_sha256: left.artifact_sha256, + settings_match: settingDifferences.length === 0, + setting_differences: settingDifferences, + facts_match: factDifferences.length === 0, + fact_differences: factDifferences, + preserved_run_specific_values: runSpecific, + workflow_failures: [left.failure, right.failure], + left: cost(left), + right: cost(right), + scope: + "Same selected analyst facts; includes extra view calls and preserves observed tool errors. Result JSON bytes exclude file exports, catalog responses and raw wire framing. This is one paired observation, not a statistical speedup or independent ground-truth proof.", +}; +const text = JSON.stringify(comparison, null, 2) + "\n"; +if (values.output) await writeFile(values.output, text, { flag: "wx" }); +process.stdout.write(text); +if ( + settingDifferences.length || + factDifferences.length || + left.failure || + right.failure +) + process.exitCode = 1; diff --git a/experiments/analysis-efficiency/corpus.json b/experiments/analysis-efficiency/corpus.json new file mode 100644 index 000000000..c85180568 --- /dev/null +++ b/experiments/analysis-efficiency/corpus.json @@ -0,0 +1,120 @@ +{ + "schema_version": 1, + "artifacts": [ + { + "id": "sharex.dll", + "file": "ShareX.dll", + "bytes": 2162688, + "sha256": "191a191be964e434daf0f9d76d1e0d8ddce90d7e248cba2a6b5ac9577f288d53", + "provenance": { + "project": "ShareX", + "version": "19.0.2", + "upstreamFile": "ShareX-19.0.2-portable.zip", + "url": "https://github.com/ShareX/ShareX/releases/download/v19.0.2/ShareX-19.0.2-portable.zip", + "transport_sha256": "967b5d838df5ce25fb5fd7cbd16f1bd797427e2df16907b022b74a4bc0fb9d54", + "transport_bytes": 168796487, + "derivation": "selected regular ZIP member ShareX.dll" + } + }, + { + "id": "sqlite.wasm", + "file": "sqlite3.wasm", + "bytes": 868907, + "sha256": "2ee8f3dab694532afc8840e07703127287662d08b74e6ff50491ce63f00d5752", + "provenance": { + "project": "SQLite Wasm", + "version": "3.53.4-build1", + "upstreamFile": "sqlite-wasm-3.53.4-build1.tgz", + "url": "https://registry.npmjs.org/@sqlite.org/sqlite-wasm/-/sqlite-wasm-3.53.4-build1.tgz", + "transport_sha256": "24f46db4ce5248fa8f8f24b67e81909ab1a9c152f42a4796a32c6d385a6fb62b", + "transport_bytes": 977616, + "transport_integrity": "sha512-U8pvL0patYHMgieD8JxxYJTlh9m1LUX8+BDYb7MiKQzBI89f2qbdmdcG/PvnPaLm9wsBbv4VlVG453ixnxBOnA==", + "derivation": "selected regular tar member package/dist/sqlite3.wasm" + } + }, + { + "id": "obsidian.asar", + "file": "obsidian-1.14.4.asar", + "bytes": 27477966, + "sha256": "146ef8470d8cbb4a6db209bdc19bf2bad13350444186c0fd93c31087b142b92f", + "provenance": { + "project": "Obsidian", + "version": "1.14.4", + "upstreamFile": "obsidian-1.14.4.asar", + "transport": { + "file": "obsidian-1.14.4.asar.gz", + "url": "https://github.com/obsidianmd/obsidian-releases/releases/download/v1.14.4/obsidian-1.14.4.asar.gz", + "sha256": "d1ed428c363968774f0f3906e67d8a53a058ece0b6cdd924b07865977b2ada21", + "bytes": 9200670 + }, + "derivation": "gzip decompression" + } + }, + { + "id": "newpipe.apk", + "file": "NewPipe_v0.29.1.apk", + "bytes": 11507122, + "sha256": "18447bfb1e06d113edc88df93f471827280de06f6f3d4dc42f56f28b9c1bab79", + "provenance": { + "project": "NewPipe", + "version": "0.29.1", + "upstreamFile": "NewPipe_v0.29.1.apk", + "url": "https://github.com/TeamNewPipe/NewPipe/releases/download/v0.29.1/NewPipe_v0.29.1.apk" + } + }, + { + "id": "jq", + "file": "jq-linux-amd64", + "bytes": 2267912, + "sha256": "b1c22172dd303f3be49e935aa56aa48a8b7a46e0bc838b4997d3bb451495870f", + "provenance": { + "project": "jq", + "version": "1.8.2", + "upstreamFile": "jq-linux-amd64", + "url": "https://github.com/jqlang/jq/releases/download/jq-1.8.2/jq-linux-amd64" + } + }, + { + "id": "openwrt.bin", + "file": "openwrt-25.12.5-ath79-generic-tplink_archer-c7-v5-squashfs-sysupgrade.bin", + "bytes": 7405862, + "sha256": "f15b16b36df7fce8a5cf1302320fdb428c3bb6fbc2949a4858afa257f4f364a2", + "provenance": { + "project": "OpenWrt Archer C7 v5", + "version": "25.12.5", + "upstreamFile": "openwrt-25.12.5-ath79-generic-tplink_archer-c7-v5-squashfs-sysupgrade.bin", + "url": "https://downloads.openwrt.org/releases/25.12.5/targets/ath79/generic/openwrt-25.12.5-ath79-generic-tplink_archer-c7-v5-squashfs-sysupgrade.bin" + } + }, + { + "id": "weth.hex", + "file": "weth-published.hex", + "bytes": 6251, + "sha256": "eb67ea4da28b88358df0385358bad3a58b131317835b45b3ede85c61851335e4", + "provenance": { + "project": "Published historical WETH runtime code example", + "url": "https://ethereum.org/developers/docs/apis/json-rpc/", + "contract_address": "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", + "published_block": "0x5daf3b", + "transport_sha256": "98c131a1bd99a0c2051dd6d6716fd306c36cdd45995d5ffd559bd9c690569c0f", + "derivation": "Parse the named eth_getCode JSON result from the official published example and preserve the hex carrier with one trailing LF", + "authority": "published historical RPC example; live retrieval returned HTTP 403; deployment authenticity remains unverified" + } + }, + { + "id": "sharex.helpers.dll", + "file": "ShareX.HelpersLib.dll", + "bytes": 1076224, + "sha256": "e91bd1ec904e2bb8a1add4bb6187443b878bf33d24fe9e27f0b799bfee9de33d", + "provenance": { + "project": "ShareX HelpersLib", + "version": "19.0.2", + "upstreamFile": "ShareX-19.0.2-portable.zip", + "url": "https://github.com/ShareX/ShareX/releases/download/v19.0.2/ShareX-19.0.2-portable.zip", + "transport_sha256": "967b5d838df5ce25fb5fd7cbd16f1bd797427e2df16907b022b74a4bc0fb9d54", + "transport_bytes": 168796487, + "derivation": "selected regular ZIP member ShareX.HelpersLib.dll; no target execution" + } + } + ] +} diff --git a/experiments/analysis-efficiency/measure.mjs b/experiments/analysis-efficiency/measure.mjs new file mode 100644 index 000000000..a10eb23d8 --- /dev/null +++ b/experiments/analysis-efficiency/measure.mjs @@ -0,0 +1,754 @@ +import { createHash } from "node:crypto"; +import { createReadStream } from "node:fs"; +import { + mkdir, + open, + readFile, + readdir, + stat, + writeFile, +} from "node:fs/promises"; +import { dirname, isAbsolute, resolve } from "node:path"; +import { fileURLToPath, pathToFileURL } from "node:url"; +import { parseArgs } from "node:util"; +import { Client } from "@modelcontextprotocol/client"; +import { StdioClientTransport } from "@modelcontextprotocol/client/stdio"; + +const root = resolve(dirname(fileURLToPath(import.meta.url)), "../.."); +const { values } = parseArgs({ + options: { + "runtime-root": { type: "string", default: root }, + revision: { type: "string" }, + artifact: { type: "string" }, + path: { type: "string" }, + providers: { type: "string" }, + output: { type: "string" }, + delivery: { type: "string", default: "complete" }, + "native-question": { type: "string", default: "entry" }, + "javascript-question": { type: "string", default: "inventory" }, + schemas: { type: "string", default: "default" }, + }, +}); +if ( + !values.revision || + !values.artifact || + !values.path || + !values.output || + !isAbsolute(values.path) || + !isAbsolute(values.output) || + !["complete", "summary"].includes(values.delivery) || + !["entry", "entry-and-main"].includes(values["native-question"]) || + !["inventory", "selected"].includes(values["javascript-question"]) || + !["default", "selected"].includes(values.schemas) || + (values.artifact !== "jq" && values["native-question"] !== "entry") || + (values.artifact !== "obsidian.asar" && + values["javascript-question"] !== "inventory") +) + throw new Error( + "Use --revision SHA --artifact ID --path ABS --output NEW_ABS_DIR [--runtime-root ABS] [--providers JSON] [--delivery complete|summary]", + ); + +const sha = (text) => createHash("sha256").update(text).digest("hex"); +const fileSha = async (path) => { + const hash = createHash("sha256"); + for await (const chunk of createReadStream(path)) hash.update(chunk); + return hash.digest("hex"); +}; +const corpus = JSON.parse( + await readFile(new URL("./corpus.json", import.meta.url), "utf8"), +); +const artifact = corpus.artifacts.find(({ id }) => id === values.artifact); +if (!artifact) throw new Error("Select an artifact from the pinned corpus"); +const inputSha = await fileSha(values.path); +if ( + artifact.sha256 !== inputSha || + artifact.bytes !== (await stat(values.path)).size +) + throw new Error("Selected bytes differ from the pinned public corpus"); +const selectedProviders = values.providers + ? JSON.parse(await readFile(values.providers, "utf8")) + : {}; +if ( + selectedProviders === null || + Array.isArray(selectedProviders) || + typeof selectedProviders !== "object" || + Object.values(selectedProviders).some((value) => typeof value !== "string") +) + throw new Error( + "--providers must contain an object of explicit string settings", + ); +const providerPolicyText = await readFile( + new URL("./provider-environment.json", import.meta.url), + "utf8", +); +const providerPolicy = JSON.parse(providerPolicyText); +const undeclaredProviderKeys = Object.keys(process.env).filter( + (key) => + (providerPolicy.namespace_prefixes.some((prefix) => + key.startsWith(prefix), + ) || + providerPolicy.additional_keys.includes(key)) && + !Object.hasOwn(selectedProviders, key), +); +if (undeclaredProviderKeys.length > 0) + throw new Error( + `Declare inherited provider settings explicitly in --providers: ${undeclaredProviderKeys.sort().join(", ")}. Values are not logged.`, + ); +await mkdir(values.output, { recursive: false, mode: 0o700 }); +const runtimeFiles = []; +const fingerprintTree = async (directory) => { + for (const entry of await readdir(directory, { withFileTypes: true })) { + const path = resolve(directory, entry.name); + if (entry.isDirectory()) await fingerprintTree(path); + else if (entry.isFile()) + runtimeFiles.push({ + path: path.slice(resolve(values["runtime-root"]).length + 1), + sha256: await fileSha(path), + }); + else throw new Error("Runtime fingerprint does not follow symlinks"); + } +}; +await fingerprintTree(resolve(values["runtime-root"], "dist")); +await fingerprintTree(resolve(values["runtime-root"], "bridge")); +runtimeFiles.sort((a, b) => (a.path < b.path ? -1 : a.path > b.path ? 1 : 0)); +await writeFile( + resolve(values.output, "runtime-files.json"), + JSON.stringify(runtimeFiles), +); +const fingerprints = { + measure_source_sha256: await fileSha(fileURLToPath(import.meta.url)), + runtime_files_sha256: sha(JSON.stringify(runtimeFiles)), + runtime_dispatcher_sha256: await fileSha( + resolve(values["runtime-root"], "scripts/rea.mjs"), + ), + package_lock_sha256: await fileSha( + resolve(values["runtime-root"], "package-lock.json"), + ), +}; +const clientVersion = JSON.parse( + await readFile( + resolve(root, "node_modules/@modelcontextprotocol/client/package.json"), + "utf8", + ), +).version; +const serverVersion = JSON.parse( + await readFile( + resolve( + values["runtime-root"], + "node_modules/@modelcontextprotocol/server/package.json", + ), + "utf8", + ), +).version; + +class ToolFailure extends Error { + constructor(value) { + super("REA returned a tool error"); + this.value = value; + } +} + +const client = new Client({ name: "rea-analysis-efficiency", version: "1" }); +const transport = new StdioClientTransport({ + command: process.execPath, + args: [resolve(values["runtime-root"], "scripts/rea.mjs"), "mcp"], + env: { ...process.env, ...selectedProviders }, + stderr: "pipe", +}); +const diagnostics = []; +const calls = []; +const catalogDefinitions = new Map(); +const pendingRpc = new Map(); +let activeCall; +const started = performance.now(); +let connectedMs, catalogMs, catalogBytes, catalogInputBytes, result, failure; +const decode = (reply) => + reply.structuredContent ?? + JSON.parse( + reply.content.find((item) => item.type === "text")?.text ?? "null", + ); +const call = async (name, arguments_) => { + const toolDefinition = + values.schemas === "selected" ? catalogDefinitions.get(name) : undefined; + if (values.schemas === "selected" && toolDefinition === undefined) + throw new Error(`No live advertised definition for ${name}`); + const began = performance.now(); + const progress = []; + const rpc = {}; + activeCall = { began, rpc }; + try { + const reply = await client.callTool( + { name, arguments: arguments_ }, + { + ...(toolDefinition === undefined ? {} : { toolDefinition }), + timeout: 600_000, + onprogress: (update) => + progress.push({ elapsed_ms: performance.now() - began, ...update }), + }, + ); + const elapsedMs = performance.now() - began; + const encodingBegan = performance.now(); + const text = JSON.stringify(reply); + const value = decode(reply); + calls.push({ + name, + arguments: arguments_, + elapsed_ms: elapsedMs, + client_before_send_ms: rpc.sent === undefined ? null : rpc.sent - began, + transport_roundtrip_ms: + rpc.received === undefined || rpc.sent === undefined + ? null + : rpc.received - rpc.sent, + client_after_receive_ms: + rpc.received === undefined ? null : began + elapsedMs - rpc.received, + client_result_json_utf8_bytes: Buffer.byteLength(text), + runner_encoding_ms: performance.now() - encodingBegan, + producer_evidence_id: value?.evidence_id ?? null, + is_error: reply.isError === true, + progress, + }); + await writeFile(resolve(values.output, `call-${calls.length}.json`), text); + if (reply.isError) throw new ToolFailure(value); + return value; + } catch (error) { + if (calls.at(-1)?.name !== name || calls.at(-1)?.arguments !== arguments_) + calls.push({ + name, + arguments: arguments_, + elapsed_ms: performance.now() - began, + failure: String(error), + progress, + }); + throw error; + } finally { + activeCall = undefined; + for (const [id, pending] of pendingRpc) + if (pending.rpc === rpc) pendingRpc.delete(id); + } +}; +const normalized = (value) => value.normalized_result; +const targetSha = (value) => value.subject?.digest.sha256; +const inventoryPaths = ["package.json", "main.js", "preload.js"]; +const unique = (values) => [...new Set(values)].sort(); +const inspectManagedTarget = async () => { + const info = await call("inspect_managed_artifact", { path: values.path }); + const boundaries = await call("inspect_managed_native_boundaries", { + path: values.path, + }); + return { + artifact_sha256: targetSha(info), + artifact: normalized(info), + boundaries: normalized(boundaries), + evidence_ids: [info.evidence_id, boundaries.evidence_id], + }; +}; + +const workflows = { + "obsidian.asar": async () => { + if (values["javascript-question"] === "selected") { + const inspected = await call("analyze_javascript_application", { + input_path: values.path, + source_paths: ["package.json", "main.js"], + detail: "summary", + }); + const receipt = normalized(inspected); + const parent = receipt.parent_evidence_id; + const evidenceIds = [parent, inspected.evidence_id]; + const modules = []; + let offset = 0; + do { + const page = await call("inspect_analysis_view", { + source: { kind: "retained-evidence", evidence_id: parent }, + view: { kind: "page", collection: "modules", offset, limit: 64 }, + }); + evidenceIds.push(page.evidence_id); + const view = normalized(page); + for (const module of view.items.filter( + (row) => row.path === "main.js", + )) { + const selected = await call("inspect_analysis_view", { + source: { kind: "retained-evidence", evidence_id: parent }, + view: { + kind: "item", + collection: "modules", + selector: { node_id: module.node_id }, + }, + }); + evidenceIds.push(selected.evidence_id); + modules.push(normalized(selected).item); + } + if (view.coverage.exhausted) break; + if ( + view.coverage.next_offset === null || + view.coverage.next_offset <= offset + ) + throw new Error("Module page did not advance"); + offset = view.coverage.next_offset; + } while (true); + return { + artifact_sha256: targetSha(inspected), + ...receipt.summary, + modules, + limitations: receipt.limitations, + evidence_ids: evidenceIds, + }; + } + await call("open_binary", { path: values.path }); + try { + const inspected = await call( + "inspect_artifact", + values.delivery === "summary" ? { detail: "summary" } : {}, + ); + if (values.delivery === "summary") { + const view = normalized(inspected); + const summary = view.summary; + if (!summary.source_complete || !summary.inspection) + throw new Error( + "Expected a complete inventory with inspection coverage", + ); + const parent = view.parent_evidence_id; + const selected = await call("inspect_analysis_view", { + source: { kind: "retained-evidence", evidence_id: parent }, + view: { + kind: "inventory", + collection: "occurrences", + logical_paths: inventoryPaths, + offset: 0, + limit: 64, + }, + }); + const contradictions = []; + let offset = 0; + while (offset < summary.available_counts.integrity_contradictions) { + const page = normalized( + await call("inspect_analysis_view", { + source: { kind: "retained-evidence", evidence_id: parent }, + view: { + kind: "inventory", + collection: "integrity_contradictions", + offset, + limit: 64, + }, + }), + ); + contradictions.push(...page.items); + if (page.coverage.exhausted) break; + if ( + page.coverage.next_offset === null || + page.coverage.next_offset <= offset + ) + throw new Error("Inventory page did not advance"); + offset = page.coverage.next_offset; + } + return { + artifact_sha256: targetSha(inspected), + manifest: summary.manifest, + source_complete: summary.source_complete, + entry_kind_counts: summary.observed_entry_kind_counts, + requested_paths: normalized(selected).items, + inventory_evidence_ids: summary.inventory_evidence_ids, + integrity_contradictions: contradictions, + coverage: summary.inspection.coverage, + unexplored_branches: summary.inspection.unexplored_branches, + limitations: view.limitations, + evidence_ids: [inspected.evidence_id, selected.evidence_id], + }; + } + const inspection = normalized(inspected); + const inventory = inspection.substeps[0].evidence; + const graph = normalized(inventory); + const counts = { file: 0, directory: 0, symlink: 0, slice: 0 }; + for (const row of graph.occurrences) counts[row.entry_kind]++; + const { parseArtifactInventoryEvidence } = await import( + pathToFileURL( + resolve( + values["runtime-root"], + "dist/domain/artifactInventoryEvidence.js", + ), + ).href + ); + const checked = parseArtifactInventoryEvidence(inventory); + return { + artifact_sha256: targetSha(inspected), + manifest: graph.manifest, + source_complete: checked.inventory.complete, + entry_kind_counts: counts, + requested_paths: graph.occurrences.filter((row) => + inventoryPaths.includes(row.logical_path), + ), + inventory_evidence_ids: [inventory.evidence_id], + integrity_contradictions: graph.integrity_contradictions, + coverage: inspection.coverage, + unexplored_branches: inspection.unexplored_branches, + evidence_ids: [inspected.evidence_id, inventory.evidence_id], + limitations: unique([ + ...inspected.limitations, + ...inspection.limitations, + ...inventory.limitations, + ]), + }; + } finally { + await call("close_binary", {}); + } + }, + "newpipe.apk": async () => { + const pkg = await call("inspect_android_package", { path: values.path }); + const found = await call("search_android_classes", { + path: values.path, + query: "MainActivity", + }); + const cls = await call("inspect_android_class", { + path: values.path, + class_name: "org.schabi.newpipe.MainActivity", + }); + return { + artifact_sha256: targetSha(pkg), + package_name: normalized(pkg).package_name, + version_name: normalized(pkg).version_name, + version_code: normalized(pkg).version_code, + matches: normalized(found).matches, + coverage: normalized(found).coverage, + onCreate: normalized(cls).methods.filter( + (row) => row.name === "onCreate", + ), + evidence_ids: [pkg.evidence_id, found.evidence_id, cls.evidence_id], + }; + }, + "sharex.dll": inspectManagedTarget, + "sharex.helpers.dll": inspectManagedTarget, + "openwrt.bin": async () => { + const info = await call("inspect_firmware_regions", { path: values.path }); + return { + artifact_sha256: targetSha(info), + result: normalized(info), + evidence_ids: [info.evidence_id], + }; + }, + "sqlite.wasm": async () => { + let info; + let recovery = null; + try { + info = await call("inspect_wasm_artifact", { + path: values.path, + glue_paths: [], + candidate_paths: [], + }); + } catch (error) { + const reference = + error instanceof ToolFailure && + error.value?.error?.code === "resource_constraint" + ? error.value.error.details?.reported_limits?.evidence_reference + : undefined; + if (reference?.kind !== "retained-evidence") throw error; + if (values.delivery === "summary") { + const summaryEvidence = await call("inspect_analysis_view", { + source: reference, + view: { kind: "summary" }, + }); + const summary = normalized(summaryEvidence).summary; + const exportsEvidence = await call("inspect_analysis_view", { + source: reference, + view: { kind: "wasm", facet: "exports", offset: 0, limit: 4 }, + }); + return { + artifact_sha256: targetSha(summaryEvidence), + validation: summary.validation, + section_count: summary.available_counts.sections, + import_count: summary.available_counts.imports, + export_count: summary.available_counts.exports, + exports_first4: normalized(exportsEvidence).item, + wat_sha256: summary.wat.sha256, + wat_code_units: summary.wat.code_units, + tool_profile: summary.tool_profile, + runtime_execution: summary.runtime_execution, + network_fetch: summary.network_fetch, + evidence_ids: [ + reference.evidence_id, + summaryEvidence.evidence_id, + exportsEvidence.evidence_id, + ], + recovery: + "existing completed producer reference read through typed WASM views", + }; + } + const path = resolve(values.output, "retained-evidence.json"); + await call("export_evidence_bundle", { path }); + const bundle = JSON.parse(await readFile(path, "utf8")); + const record = bundle.records.find( + (row) => row.evidence_id === reference.evidence_id, + ); + const { parseEvidence } = await import( + pathToFileURL( + resolve(values["runtime-root"], "dist/domain/evidence.js"), + ).href + ); + info = parseEvidence(record); + recovery = + "complete producer Evidence exported after an observed transport constraint"; + } + const n = normalized(info); + return { + artifact_sha256: targetSha(info), + validation: n.validation, + section_count: n.sections.length, + import_count: n.imports.length, + export_count: n.exports.length, + exports_first4: n.exports.slice(0, 4), + wat_sha256: n.wat.sha256, + wat_code_units: n.wat.text.length, + tool_profile: n.tool_profile, + runtime_execution: n.runtime_execution, + network_fetch: n.network_fetch, + evidence_ids: [info.evidence_id], + recovery, + }; + }, + "weth.hex": async () => { + const info = await call("inspect_evm_interface", { + path: values.path, + encoding: "hex", + }); + const n = normalized(info); + return { + artifact_sha256: targetSha(info), + artifact: n.artifact, + bytecode: n.bytecode, + functions: n.functions, + discovery_completeness: n.discovery_completeness, + runtime_execution: n.runtime_execution, + limitations: n.limitations, + evidence_ids: [info.evidence_id], + }; + }, + jq: async () => { + const file = await open(values.path, "r"); + let header; + try { + header = (await file.read(Buffer.alloc(32), 0, 32, 0)).buffer; + } finally { + await file.close(); + } + if (header.subarray(0, 6).toString("hex") !== "7f454c460201") + throw new Error("Expected ELF64 little endian entry seed"); + const address = `0x${header.readBigUInt64LE(24).toString(16)}`; + await call("open_binary", { path: values.path, provider_id: "ghidra" }); + try { + const containing = await call("resolve_containing_procedure", { + address, + }); + const info = await call("analyze_function", { procedure: address }); + const n = normalized(info); + // This exact application entry is materialized in the pinned jq entry's + // observed MOV RDI, 0x401f00 instruction, not an original-source claim. + let main; + const mainEvidenceIds = []; + if (values["native-question"] === "entry-and-main") { + let sourceId; + try { + const completed = await call("analyze_function", { + procedure: "0x401f00", + }); + sourceId = completed.evidence_id; + } catch (error) { + const reference = + error instanceof ToolFailure && + error.value?.error?.code === "resource_constraint" + ? error.value.error.details?.reported_limits?.evidence_reference + : undefined; + if (reference?.kind !== "retained-evidence") throw error; + sourceId = reference.evidence_id; + } + mainEvidenceIds.push(sourceId); + main = {}; + for (const facet of [ + "procedure", + "pseudocode", + "referenced_strings", + "callees", + "callers", + "basic_blocks", + ]) { + let offset = 0; + const items = []; + const pages = []; + do { + const selected = await call("inspect_analysis_view", { + source: { kind: "retained-evidence", evidence_id: sourceId }, + view: { + kind: "native", + facet, + offset, + limit: + facet === "pseudocode" + ? 65536 + : facet === "basic_blocks" + ? 1 + : 64, + }, + }); + mainEvidenceIds.push(selected.evidence_id); + const v = normalized(selected); + items.push(v.item); + pages.push({ + coverage: v.coverage, + limitations: v.limitations, + unknowns: v.unknowns, + }); + if (v.coverage.exhausted || facet === "basic_blocks") break; + if ( + v.coverage.next_offset === null || + v.coverage.next_offset <= offset + ) + throw new Error("Native view did not advance"); + offset = v.coverage.next_offset; + } while (true); + main[facet] = { items, pages }; + } + } + return { + artifact_sha256: targetSha(info), + entry_address: address, + containing_procedure: normalized(containing), + procedure: n.procedure, + pseudocode_prefix: n.pseudocode.slice(0, 512), + caller_count: n.callers.length, + callee_count: n.callees.length, + ...(main === undefined ? {} : { main }), + analysis_profile: info.analysis_profile, + evidence_ids: [ + containing.evidence_id, + info.evidence_id, + ...mainEvidenceIds, + ], + }; + } finally { + await call("close_binary", {}); + } + }, +}; + +try { + await client.connect(transport); + connectedMs = performance.now() - started; + // Observe the public Transport seam without replacing its codec or lifecycle. + // These intervals separate SDK work from the transport/server roundtrip; + // the roundtrip still includes server work, framing, IO and client parsing. + const send = transport.send.bind(transport); + transport.send = (message, options) => { + if (message.method === "tools/call" && activeCall !== undefined) { + activeCall.rpc.sent = performance.now(); + pendingRpc.set(message.id, activeCall); + } + return send(message, options); + }; + const receive = transport.onmessage; + transport.onmessage = (message, extra) => { + if ("result" in message || "error" in message) { + const pending = pendingRpc.get(message.id); + if (pending !== undefined) { + pending.rpc.received = performance.now(); + pendingRpc.delete(message.id); + } + } + receive?.(message, extra); + }; + transport.stderr?.on("data", (chunk) => diagnostics.push(chunk.toString())); + const catalogStarted = performance.now(); + const tools = []; + let cursor; + do { + const page = await client.listTools(cursor === undefined ? {} : { cursor }); + tools.push(...page.tools); + cursor = page.nextCursor; + } while (cursor !== undefined); + catalogMs = performance.now() - catalogStarted; + for (const tool of tools) { + if (catalogDefinitions.has(tool.name)) + throw new Error("Duplicate advertised tool name"); + catalogDefinitions.set(tool.name, tool); + } + catalogBytes = Buffer.byteLength(JSON.stringify(tools)); + catalogInputBytes = Buffer.byteLength( + JSON.stringify( + tools.map(({ name, description, inputSchema }) => ({ + name, + description, + inputSchema, + })), + ), + ); + await writeFile( + resolve(values.output, "catalog.json"), + JSON.stringify(tools), + ); + const workflow = workflows[values.artifact]; + if (!workflow) throw new Error("No pinned question for selected artifact"); + result = await workflow(); + if (result.artifact_sha256 !== inputSha) + throw new Error("Producer target digest differs from selected bytes"); +} catch (error) { + failure = String(error); +} finally { + try { + await client.close(); + } catch (error) { + failure = `${failure ?? ""} cleanup: ${String(error)}`; + } +} +await writeFile( + resolve(values.output, "diagnostics.log"), + diagnostics.join(""), +); +const finalInputSha = await fileSha(values.path); +if (finalInputSha !== inputSha) + failure = `${failure ?? ""} Selected artifact bytes changed during the workflow`; +if (result) + await writeFile( + resolve(values.output, "answer.json"), + JSON.stringify(result), + ); +const report = { + revision: values.revision, + runtime_root: values["runtime-root"], + artifact: values.artifact, + artifact_sha256: inputSha, + artifact_sha256_after: finalInputSha, + delivery: values.delivery, + sdk_schema_mode: values.schemas, + native_question: values.artifact === "jq" ? values["native-question"] : null, + javascript_question: + values.artifact === "obsidian.asar" ? values["javascript-question"] : null, + provider_settings_sha256: sha(JSON.stringify(selectedProviders)), + provider_search_path_sha256: sha( + selectedProviders.PATH ?? process.env.PATH ?? "", + ), + provider_environment_policy_sha256: sha(providerPolicyText), + ...fingerprints, + node_version: process.version, + sdk_client_version: clientVersion, + sdk_server_version: serverVersion, + platform: process.platform, + architecture: process.arch, + connected_ms: connectedMs, + catalog_ms: catalogMs, + catalog_json_utf8_bytes: catalogBytes, + catalog_input_definition_utf8_bytes: catalogInputBytes, + elapsed_ms: performance.now() - started, + calls, + failure: failure ?? null, + measurement_scope: + "Public SDK Transport timestamps split client work from the transport/server roundtrip; parsed result JSON bytes are not raw wire bytes; runner encoding and writes are outside each call; engine-stage durations and independent producer truth are not inferred", +}; +await writeFile( + resolve(values.output, "summary.json"), + JSON.stringify(report, null, 2), +); +process.stdout.write( + JSON.stringify({ + artifact: values.artifact, + delivery: values.delivery, + elapsed_ms: report.elapsed_ms, + failure: failure?.slice(0, 800) ?? null, + }) + "\n", +); +if (failure) process.exitCode = 1; diff --git a/experiments/analysis-efficiency/probe-analyzers.mjs b/experiments/analysis-efficiency/probe-analyzers.mjs new file mode 100644 index 000000000..6ae961c4b --- /dev/null +++ b/experiments/analysis-efficiency/probe-analyzers.mjs @@ -0,0 +1,116 @@ +import { createHash } from "node:crypto"; +import { createReadStream } from "node:fs"; +import { mkdir, open, readFile, writeFile } from "node:fs/promises"; +import { resolve } from "node:path"; +import { parseArgs } from "node:util"; +import { Client } from "@modelcontextprotocol/client"; +import { StdioClientTransport } from "@modelcontextprotocol/client/stdio"; + +const { values } = parseArgs({ + options: { + "runtime-root": { type: "string" }, + path: { type: "string" }, + providers: { type: "string" }, + output: { type: "string" }, + }, +}); +if ( + !values["runtime-root"] || + !values.path || + !values.providers || + !values.output +) + throw new Error( + "Use --runtime-root ABS --path ABS --providers JSON --output NEW_ABS_DIR", + ); +const selected = JSON.parse(await readFile(values.providers, "utf8")); +const expectedName = selected.REA_GHIDRA_DISABLED_ANALYZERS; +if (!expectedName) + throw new Error( + "The probe requires an explicitly selected invalid analyzer name", + ); +const digest = async () => { + const hash = createHash("sha256"); + for await (const chunk of createReadStream(values.path)) hash.update(chunk); + return hash.digest("hex"); +}; +const before = await digest(); +await mkdir(values.output, { mode: 0o700 }); +const file = await open(values.path, "r"); +let address; +try { + const header = Buffer.alloc(32); + const { bytesRead } = await file.read(header, 0, header.length, 0); + if ( + bytesRead !== 32 || + header.subarray(0, 6).toString("hex") !== "7f454c460201" + ) + throw new Error( + "This counterexample uses a real ELF64 little-endian application", + ); + address = `0x${header.readBigUInt64LE(24).toString(16)}`; +} finally { + await file.close(); +} +const client = new Client({ name: "rea-analyzer-verification", version: "1" }); +const transport = new StdioClientTransport({ + command: process.execPath, + args: [resolve(values["runtime-root"], "scripts/rea.mjs"), "mcp"], + env: { ...process.env, ...selected }, + stderr: "pipe", +}); +const calls = []; +const call = async (name, arguments_) => { + const reply = await client.callTool( + { name, arguments: arguments_ }, + { timeout: 600_000 }, + ); + await writeFile( + resolve(values.output, `call-${calls.length + 1}.json`), + JSON.stringify(reply), + ); + calls.push({ name, is_error: reply.isError === true }); + return reply; +}; +let rejected = false; +let named = false; +let failure = null; +try { + await client.connect(transport); + const opened = await call("open_binary", { + path: values.path, + provider_id: "ghidra", + }); + const reply = opened.isError + ? opened + : await call("resolve_containing_procedure", { address }); + rejected = reply.isError === true; + named = rejected && JSON.stringify(reply).includes(expectedName); +} catch (error) { + failure = String(error); +} finally { + try { + await call("close_binary", {}); + } catch (error) { + failure = `${failure ?? ""} close: ${String(error)}`; + } + await client.close(); +} +const after = await digest(); +const report = { + target_sha256: before, + target_sha256_after: after, + expected_analyzer: expectedName, + rejected, + rejection_names_requested_analyzer: named, + failure, + calls, + oracle: + "A syntactically valid but nonexistent analyzer must produce a meaningful public refusal, not successful Evidence claiming that preset. A not-found procedure still means Ghidra reached readiness.", +}; +await writeFile( + resolve(values.output, "summary.json"), + JSON.stringify(report, null, 2), +); +process.stdout.write(JSON.stringify(report) + "\n"); +if (!rejected || !named || failure || before !== after) process.exitCode = 1; diff --git a/experiments/analysis-efficiency/probe-browser.mjs b/experiments/analysis-efficiency/probe-browser.mjs new file mode 100644 index 000000000..3ec1eb816 --- /dev/null +++ b/experiments/analysis-efficiency/probe-browser.mjs @@ -0,0 +1,85 @@ +import { mkdir, readFile, writeFile } from "node:fs/promises"; +import { resolve } from "node:path"; +import { parseArgs } from "node:util"; +import { Client } from "@modelcontextprotocol/client"; +import { StdioClientTransport } from "@modelcontextprotocol/client/stdio"; + +const { values } = parseArgs({ + options: { + "runtime-root": { type: "string" }, + scenario: { type: "string" }, + output: { type: "string" }, + }, +}); +if (!values["runtime-root"] || !values.scenario || !values.output) + throw new Error( + "Use --runtime-root ABS --scenario JSON --output NEW_ABS_DIR", + ); +await mkdir(values.output, { mode: 0o700 }); +const scenario = JSON.parse(await readFile(values.scenario, "utf8")); +const client = new Client({ name: "rea-real-browser-app", version: "1" }); +const transport = new StdioClientTransport({ + command: process.execPath, + args: [resolve(values["runtime-root"], "scripts/rea.mjs"), "mcp"], + env: { + ...process.env, + REA_LOG_LEVEL: "silent", + NODE_OPTIONS: "--max-old-space-size=512", + }, + stderr: "pipe", +}); +let failure; +try { + await client.connect(transport); + transport.stderr?.resume(); + const reply = await client.callTool( + { name: "capture_browser_scenario", arguments: scenario }, + { timeout: 120_000 }, + ); + await writeFile(resolve(values.output, "reply.json"), JSON.stringify(reply)); + const value = + reply.structuredContent ?? + JSON.parse( + reply.content.find((item) => item.type === "text")?.text ?? "null", + ); + if (reply.isError) { + failure = value.error; + } else { + const capture = value.normalized_result; + await writeFile( + resolve(values.output, "summary.json"), + JSON.stringify( + { + evidence_id: value.evidence_id, + browser: capture.browser, + scenario: capture.scenario, + completeness: capture.completeness, + step_count: capture.steps.length, + event_count: capture.events.retained, + limitations: value.limitations, + }, + null, + 2, + ), + ); + process.stdout.write( + JSON.stringify({ + evidence_id: value.evidence_id, + browser: capture.browser, + step_count: capture.steps.length, + }) + "\n", + ); + } +} finally { + await client.close(); +} +if (failure) { + await writeFile( + resolve(values.output, "failure.json"), + JSON.stringify(failure, null, 2), + ); + process.stdout.write( + JSON.stringify({ code: failure.code, message: failure.message }) + "\n", + ); + process.exitCode = 1; +} diff --git a/experiments/analysis-efficiency/provider-environment.json b/experiments/analysis-efficiency/provider-environment.json new file mode 100644 index 000000000..de145d10e --- /dev/null +++ b/experiments/analysis-efficiency/provider-environment.json @@ -0,0 +1,10 @@ +{ + "namespace_prefixes": ["REA_", "HOPPER_"], + "additional_keys": [ + "GHIDRA_INSTALL_DIR", + "JAVA_HOME", + "JAVA_TOOL_OPTIONS", + "JDK_JAVA_OPTIONS", + "_JAVA_OPTIONS" + ] +} diff --git a/experiments/analysis-efficiency/questions/android.txt b/experiments/analysis-efficiency/questions/android.txt new file mode 100644 index 000000000..e303575f7 --- /dev/null +++ b/experiments/analysis-efficiency/questions/android.txt @@ -0,0 +1,3 @@ +Statically investigate the actual shipped APK at {{artifact_path}} using only the registered REA tools. Report the exact artifact SHA-256, package name, version name/code, every class-name match for the case-sensitive substring MainActivity, and all onCreate method descriptors reported for org.schabi.newpipe.MainActivity. Preserve reported signatures and explicit unknowns. For each onCreate entry include reported_signature, overload_index and dex_descriptor exactly as reported, preserving null. Final answer must be JSON with artifact_sha256, package_name, version_name, version_code, matches, onCreate, evidence_ids, limitations. Include real Evidence IDs supporting each result. Do not execute the APK or use shell, host file tools, network or other agents. Stop once these facts are supported. + +When a client JavaScript executor is available, compose the required REA calls in one execution and return only selected facts from normalized_result, real Evidence IDs, target identity from subject.digest.sha256 and limitations. Parse original MCP text content if necessary. Do not return raw_result, whole Evidence or unrelated inventories. All matching classes and onCreate overloads must still be included. diff --git a/experiments/analysis-efficiency/questions/browser.txt b/experiments/analysis-efficiency/questions/browser.txt new file mode 100644 index 000000000..c66f39422 --- /dev/null +++ b/experiments/analysis-efficiency/questions/browser.txt @@ -0,0 +1 @@ +Observe the real public Excalidraw application through REA capture_browser_scenario using exactly this authorized fresh-profile headless scenario: {"browser": {"mode": "launch", "executable_path": "/home/yann/.cache/ms-playwright/chromium-1234/chrome-linux64/chrome", "headless": true}, "start_url": {"url": "https://excalidraw.com/", "query": []}, "actions": [{"step_id": "settled", "action": "wait_for_timeout", "duration_ms": 2000}], "capture": {"at_end": ["url", "dom", "accessibility"], "events": ["network", "page-errors"]}}. Report actual browser product/version, start origin, final sanitized URL, DOM/accessibility artifact SHA-256 and byte counts when captured, step count, network/page-error observations, completeness, provider-owned process cleanup, real Evidence ID and limitations. Preserve unavailable and partial capture states. This is one runtime observation, not a claim about original source or all behavior. Do not supply credentials, interact with other targets or inject JavaScript. Final JSON fields: browser, start_origin, final_url, dom, accessibility, step_count, network, page_errors, completeness, evidence_ids, limitations. diff --git a/experiments/analysis-efficiency/questions/electron-inventory.txt b/experiments/analysis-efficiency/questions/electron-inventory.txt new file mode 100644 index 000000000..bb3e70cf1 --- /dev/null +++ b/experiments/analysis-efficiency/questions/electron-inventory.txt @@ -0,0 +1 @@ +Statically inventory {{artifact_path}} using only the registered REA tools. Open this ASAR as an archive, inspect its artifact inventory, and close the session. Report exact target SHA-256, the complete inventory manifest, counts of every entry_kind across all occurrences, the original occurrence records for root paths package.json, main.js and preload.js if present, all integrity contradictions, inspection coverage, unexplored branches, real Evidence IDs and limitations. This question asks about package bytes and inventory, not AST interpretation or runtime behavior. Do not execute target code, analyze all JavaScript, extract files, launch a GUI, use shell/file tools/network or other agents. When a client JavaScript executor is available, compose original calls in dependency order and return only requested fields from normalized_result; the full inventory is the Evidence nested in the inspect_artifact inventory_artifact substep. Use Evidence.subject.digest.sha256 for the target identity and retain both inspection and inventory Evidence IDs. Final JSON fields: artifact_sha256, manifest, entry_kind_counts, requested_paths, integrity_contradictions, coverage, unexplored_branches, evidence_ids, limitations. Stop when these facts and cleanup are supported. diff --git a/experiments/analysis-efficiency/questions/evm.txt b/experiments/analysis-efficiency/questions/evm.txt new file mode 100644 index 000000000..a5ba2f6a6 --- /dev/null +++ b/experiments/analysis-efficiency/questions/evm.txt @@ -0,0 +1 @@ +Statically inspect the published historical WETH hex carrier at {{artifact_path}} using only REA inspect_evm_interface with encoding hex. Report original carrier SHA-256/byte count, decoded bytecode SHA-256/byte count, every inferred function selector with bytecode_offset, dispatch, inferred_arguments and inferred_state_mutability exactly including null. Preserve evidence_kind, discovery_completeness, bytecode kind/hardfork/deployment_authenticity, runtime_execution, real Evidence IDs and limitations. This file comes from the official Ethereum published historical eth_getCode example; do not infer live deployment authenticity, original source signatures or runtime behavior. No RPC, transactions, target execution, shell/file tools or other agents. Final JSON fields: artifact_sha256, artifact_bytes, bytecode_sha256, bytecode_bytes, functions, evidence_kind, discovery_completeness, bytecode_kind, hardfork, deployment_authenticity, runtime_execution, evidence_ids, limitations. diff --git a/experiments/analysis-efficiency/questions/firmware.txt b/experiments/analysis-efficiency/questions/firmware.txt new file mode 100644 index 000000000..de03fe9ff --- /dev/null +++ b/experiments/analysis-efficiency/questions/firmware.txt @@ -0,0 +1 @@ +Statically inspect {{artifact_path}} using only the registered REA tools. Report exact target SHA-256 and every reported firmware region with offsets, signatures, reported extents, provider provenance and examined coverage. Preserve unknowns and limitations; a signature scan does not prove partition boundaries or boot/runtime validity. Do not execute target code, extract files, launch a GUI, use shell/file tools/network or other agents. When a client JavaScript executor is available, select the requested normalized_result fields, Evidence.subject.digest.sha256 and real Evidence IDs before returning output. Final JSON fields: artifact_sha256, result, evidence_ids, limitations. Stop when the requested facts are supported. diff --git a/experiments/analysis-efficiency/questions/javascript-selected.txt b/experiments/analysis-efficiency/questions/javascript-selected.txt new file mode 100644 index 000000000..e4a7520da --- /dev/null +++ b/experiments/analysis-efficiency/questions/javascript-selected.txt @@ -0,0 +1 @@ +Investigate the actual shipped Obsidian ASAR at {{artifact_path}} using REA analyze_javascript_application with source_paths ["package.json","main.js"] and detail summary. Keep complete inventory identity while analyzing only those exact sources. Read retained module views for each main.js module identity; module pages may contain several identities for a path, so preserve their exact node_id values. package.json is package metadata and need not be a module. Report selected input path/root artifact SHA-256, requested_paths and unselected_files, graph/statistics, Electron observations, exact selected main.js module identities/types/digests, analysis and semantic coverage, retained source Evidence ID, view Evidence IDs and limitations. Do not execute target JavaScript, analyze other sources, extract files, use network/shell/file tools or other agents. Static AST results do not prove actual runtime behavior or original source. Final JSON fields: input_path, artifact_sha256, source_selection, statistics, electron, modules, coverage, evidence_ids, limitations. diff --git a/experiments/analysis-efficiency/questions/managed-nonempty.txt b/experiments/analysis-efficiency/questions/managed-nonempty.txt new file mode 100644 index 000000000..cfa47b5c5 --- /dev/null +++ b/experiments/analysis-efficiency/questions/managed-nonempty.txt @@ -0,0 +1 @@ +Statically inspect the shipped ShareX HelpersLib assembly at {{artifact_path}} using only registered REA tools. Report exact target SHA-256 and module MVID, native ModuleRef/PInvoke/native-implementation counts, every original ModuleRef record, the first three complete PInvoke declaration records in producer order, coverage and limitations. Preserve tokens, import scope/name, flags, nulls and verification states exactly; declarations do not prove native-library/export availability or runtime behavior. No assembly load, native symbol lookup, target execution, GUI, shell/file tools, network or other agents. Final JSON fields: artifact_sha256, module_mvid, summary, module_refs, pinvoke_first3, coverage, evidence_ids, limitations. Stop once those selected facts are supported. Use compact selected evidence when the client supports it, but do not invent fields hidden by client output clipping. diff --git a/experiments/analysis-efficiency/questions/managed.txt b/experiments/analysis-efficiency/questions/managed.txt new file mode 100644 index 000000000..57125c432 --- /dev/null +++ b/experiments/analysis-efficiency/questions/managed.txt @@ -0,0 +1 @@ +Statically inspect {{artifact_path}} using only the registered REA tools. Report exact target SHA-256, assembly name/version, module name/MVID, managed classification and architecture with its evidence, every reported target framework, and all declared native ModuleRef/PInvoke boundaries with coverage and limitations. Do not load or execute the assembly, launch a GUI, use shell/file tools/network or other agents. When a client JavaScript executor is available, compose the required original MCP calls and return only requested normalized_result fields, target identity from subject.digest.sha256 and real Evidence IDs. Do not return whole Evidence, raw_result or unrelated resources/custom-attribute inventories. Preserve dependency order for operations sharing the selected managed target. Final JSON fields: artifact_sha256, assembly, module, classification, target_frameworks, native_boundaries, coverage, evidence_ids, limitations. Stop when these facts are supported. diff --git a/experiments/analysis-efficiency/questions/native.txt b/experiments/analysis-efficiency/questions/native.txt new file mode 100644 index 000000000..2a790b1e6 --- /dev/null +++ b/experiments/analysis-efficiency/questions/native.txt @@ -0,0 +1 @@ +Statically inspect {{artifact_path}} using only the registered REA tools and Ghidra. The pinned ELF header was independently observed to report entry address 0x403a50; treat that address as the analyst's seed, not as a function-name assumption. Keep default auto-analysis enabled and allow at least 600 seconds for native analysis (including the script deadline if using a bridge). Open the target with provider_id ghidra, resolve the containing procedure for that address, analyze it, and close the ephemeral session in a finally block or a subsequent cleanup call. Report exact target SHA-256, the entry_address, containing_procedure, procedure metadata, the first 512 UTF-16 units of pseudocode, caller_count, callee_count, real Evidence IDs and limitations. Do not execute target code, launch a GUI, use shell/file tools/network or other agents. When a client JavaScript executor is available, compose the original calls in dependency order and select requested fields before returning output; use Evidence.subject.digest.sha256 for the target identity. Final JSON fields: artifact_sha256, entry_address, containing_procedure, procedure, pseudocode_prefix, caller_count, callee_count, evidence_ids, limitations. The address seed applies only to the pinned jq artifact in corpus.json. Stop when these facts and cleanup are supported. diff --git a/experiments/analysis-efficiency/questions/wasm.txt b/experiments/analysis-efficiency/questions/wasm.txt new file mode 100644 index 000000000..555c0e3ca --- /dev/null +++ b/experiments/analysis-efficiency/questions/wasm.txt @@ -0,0 +1 @@ +Statically inspect the real shipped SQLite WASM at {{artifact_path}} using only REA tools. Report exact selected artifact SHA-256, valid/invalid status, section count, import count, export count, first four complete export WAT forms in producer order, decoded WAT SHA-256 and code-unit length, WABT release/source commit, and exact runtime_execution/network_fetch states. If complete delivery exceeds the MCP frame, use the exact retained reference for summary and wasm exports views. Preserve original strings, uncertainties and real Evidence IDs. Do not execute WASM, fetch URLs or use shell/file tools. Final JSON fields: artifact_sha256, validation, section_count, import_count, export_count, exports_first4, wat_sha256, wat_code_units, wabt_release, wabt_source_commit, runtime_execution, network_fetch, evidence_ids, limitations. diff --git a/experiments/analysis-efficiency/resource-run.py b/experiments/analysis-efficiency/resource-run.py new file mode 100644 index 000000000..4417b390a --- /dev/null +++ b/experiments/analysis-efficiency/resource-run.py @@ -0,0 +1,158 @@ +"""Run one owned experiment in a bounded, transient Linux systemd scope.""" + +import argparse +import json +import os +from pathlib import Path +import subprocess +import sys +import time +import uuid + + +def available_bytes(): + for line in Path('/proc/meminfo').read_text().splitlines(): + if line.startswith('MemAvailable:'): + return int(line.split()[1]) * 1024 + raise RuntimeError('MemAvailable unavailable') + + +def scope_has_processes(group): + if group is None: + return False + try: + return bool((group / 'cgroup.procs').read_text().strip()) + except FileNotFoundError: + return False + + +def final_resource_events(group): + """An exited transient cgroup may disappear; missing counters are unknown.""" + if group is None: + return None + try: + result = {name: dict((key, int(value)) for key, value in + (line.split() for line in (group / name).read_text().splitlines())) + for name in ['memory.events', 'pids.events']} + except (OSError, ValueError): + return None + required = {'memory.events': ['high', 'max', 'oom', 'oom_kill'], + 'pids.events': ['max']} + if not all(all(key in result[name] for key in keys) + for name, keys in required.items()): + return None + return result + + +def stop_owned_scope(unit, process, group): + subprocess.run(['systemctl', '--user', 'stop', '--no-block', unit], + check=False, timeout=5) + for attempt in range(2): + deadline = time.monotonic() + 10 + while process.poll() is None or scope_has_processes(group): + if time.monotonic() >= deadline: + break + time.sleep(0.1) + else: + return + if attempt == 1: + raise RuntimeError('Cleanup unverified; retain owned scope ' + unit) + subprocess.run(['systemctl', '--user', 'kill', '--kill-who=all', + '--signal=SIGKILL', unit], check=False, timeout=5) + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument('--task-ceiling', type=int, default=192) + parser.add_argument('command', nargs=argparse.REMAINDER) + options = parser.parse_args() + if options.task_ceiling <= 0: + parser.error('--task-ceiling must be positive') + args = options.command + if args and args[0] == '--': + args = args[1:] + if not args: + raise SystemExit('Usage: resource-run.py -- COMMAND ARGS...') + gib = 1024 ** 3 + if available_bytes() < 8 * gib: + raise SystemExit('Refusing experiment: less than 8 GiB available') + unit = 'rea-efficiency-' + uuid.uuid4().hex + '.scope' + cpu = max(os.sched_getaffinity(0)) + command = [ + 'systemd-run', '--user', '--scope', '--unit=' + unit, + '-p', 'MemoryMax=4G', '-p', 'MemoryHigh=3G', '-p', 'MemorySwapMax=0', + '-p', 'CPUQuota=100%', '-p', 'TasksMax=' + str(options.task_ceiling), + 'taskset', '-c', str(cpu), *args, + ] + process = subprocess.Popen(command) + peak = 0 + peak_tasks = 0 + events = {} + cpu_stats = {} + abort = None + final_events = None + started = time.monotonic() + try: + group = None + while process.poll() is None: + if group is None: + reply = subprocess.run( + ['systemctl', '--user', 'show', unit, '--value', + '--property=ControlGroup'], + capture_output=True, text=True, check=False, + ) + path = reply.stdout.strip() + if path: + group = Path('/sys/fs/cgroup') / path.lstrip('/') + current = group / 'memory.current' if group else None + if current and current.exists(): + try: + peak = max(peak, int(current.read_text())) + peak_tasks = max(peak_tasks, int((group / 'pids.current').read_text())) + cpu_stats = dict((key, int(value)) for key, value in + (line.split() for line in (group / 'cpu.stat').read_text().splitlines())) + for event_file in ['memory.events', 'pids.events']: + events[event_file] = dict( + (key, int(value)) for key, value in + (line.split() for line in (group / event_file).read_text().splitlines()) + ) + except FileNotFoundError: + # A completed transient scope can disappear between reads. + pass + if peak > 3 * gib: + abort = 'scope_memory_early_stop' + elif available_bytes() < 4 * gib: + abort = 'host_available_memory_early_stop' + elif events.get('pids.events', {}).get('max', 0): + abort = 'scope_task_limit_observed' + if abort: + stop_owned_scope(unit, process, group) + break + time.sleep(0.1) + # Read while the owned group still exists, before wait/cleanup can remove it. + final_events = final_resource_events(group) + code = process.wait() + except BaseException: + stop_owned_scope(unit, process, group) + raise + finally: + if scope_has_processes(group): + final_events = None + stop_owned_scope(unit, process, group) + print(json.dumps({ + 'resource_scope': unit, 'peak_cgroup_bytes_sampled': peak, + 'elapsed_seconds': round(time.monotonic() - started, 3), + 'abort': abort, 'hard_memory_bytes': 4 * gib, + 'early_stop_bytes': 3 * gib, 'cpu_quota_percent': 100, + 'cpu_affinity': [cpu], + 'task_ceiling': options.task_ceiling, 'peak_tasks_sampled': peak_tasks, + 'resource_events_last_observed': events, + 'resource_events_final': final_events, + 'resource_counters_verified': final_events is not None, + 'cpu_stat_last_observed': cpu_stats, + }), file=sys.stderr) + raise SystemExit(125 if abort else code) + + +if __name__ == '__main__': + main() diff --git a/experiments/analysis-efficiency/review-model.py b/experiments/analysis-efficiency/review-model.py new file mode 100644 index 000000000..22d6dd766 --- /dev/null +++ b/experiments/analysis-efficiency/review-model.py @@ -0,0 +1,265 @@ +"""Check selected model facts against public producer observations, not exit status.""" + +import argparse +import hashlib +import json +from pathlib import Path +import re + + +def read(path): + return json.loads(path.read_text()) + + +def strings(value): + if isinstance(value, str): + yield value + elif isinstance(value, list): + for entry in value: + yield from strings(entry) + elif isinstance(value, dict): + for entry in value.values(): + yield from strings(entry) + + +def evidence_ids(value): + return {s for s in strings(value) if re.fullmatch(r'ev_[a-f0-9]{64}', s)} + + +def decode(result): + value = result.get('structured_content', result.get('structuredContent')) + if value is not None: + return value + for entry in result.get('content', []): + if entry.get('type') == 'text': + try: + return json.loads(entry['text']) + except json.JSONDecodeError: + pass + return None + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument('--domain', choices=['wasm', 'evm', 'android', 'managed', 'managed-nonempty', 'firmware', 'inventory', 'javascript', 'native', 'browser'], required=True) + parser.add_argument('--trial', type=Path, required=True) + parser.add_argument('--reference', type=Path) + parser.add_argument('--resource-log', type=Path, required=True) + parser.add_argument('--output', type=Path, required=True) + args = parser.parse_args() + summary = read(args.trial / 'summary.json') + settings = read(args.trial / 'trial-settings.json') + answer_text = (args.trial / 'answer.txt').read_text().strip() + fenced = answer_text.startswith('```json\n') and answer_text.endswith('\n```') + answer = json.loads(answer_text[8:-4] if fenced else answer_text) + reference = read(args.reference / 'answer.json') if args.reference else None + observed_ids = set() + producer_results = {} + tool_errors = [] + for line in (args.trial / 'events.jsonl').open(): + event = json.loads(line) + item = event.get('item', {}) + if event.get('type') != 'item.completed' or item.get('type') != 'mcp_tool_call': + continue + result = item.get('result') or {} + parsed = decode(result) + if item['status'] == 'completed': + # Only response text contributes observed IDs, never model arguments. + for text in strings(result): + observed_ids.update(re.findall(r'ev_[a-f0-9]{64}', text)) + if parsed is not None: + producer_results.setdefault(item['tool'], []).append(parsed) + else: + tool_errors.append({'tool': item['tool'], 'arguments': item['arguments'], 'response': parsed if parsed is not None else result}) + if parsed is not None and parsed.get('error', {}).get('code') == 'resource_constraint': + reference_value = parsed['error'].get('details', {}).get('reported_limits', {}).get('evidence_reference') + observed_ids.update(evidence_ids(reference_value)) + reported_ids = evidence_ids(answer.get('evidence_ids')) + checks = { + 'reported_evidence_ids_observed_in_responses': bool(reported_ids) and reported_ids <= observed_ids, + 'temporary_profile_removed': read(args.trial / 'cleanup.json')['profile_removed'], + } + if summary.get('selected_artifact_sha256_before') is not None: + checks['selected_artifact_bytes_unchanged'] = summary['selected_artifact_bytes_unchanged'] + if reference is not None: + checks['target_digest'] = answer.get('artifact_sha256') == reference['artifact_sha256'] + differences = {} + + def check(name, actual, expected): + checks[name] = actual == expected + if not checks[name]: + differences[name] = {'answer': actual, 'producer': expected} + + domain = args.domain + if domain == 'wasm': + for key in ['validation', 'section_count', 'import_count', 'export_count', 'exports_first4', 'wat_sha256', 'wat_code_units', 'runtime_execution', 'network_fetch']: + check(key, answer.get(key), reference[key]) + for key in ['release', 'source_commit']: + check('wabt_' + key, answer.get('wabt_' + key), reference['tool_profile'][key]) + elif domain == 'evm': + n = producer_results['inspect_evm_interface'][-1]['normalized_result'] + for key, expected in {'artifact_bytes': n['artifact']['bytes'], 'bytecode_sha256': n['bytecode']['sha256'], 'bytecode_bytes': n['bytecode']['bytes'], 'functions': n['functions'], 'evidence_kind': n['evidence_kind'], 'discovery_completeness': n['discovery_completeness'], 'runtime_execution': n['runtime_execution'], 'bytecode_kind': n['bytecode']['kind'], 'hardfork': n['bytecode']['hardfork'], 'deployment_authenticity': n['bytecode']['deployment_authenticity']}.items(): + check(key, answer.get(key), expected) + elif domain == 'android': + for key in ['package_name', 'version_name', 'version_code', 'matches']: + check(key, answer.get(key), reference[key]) + fields = ['reported_signature', 'overload_index', 'dex_descriptor'] + checks['onCreate_fields_present_including_explicit_null'] = all(all(k in row for k in fields) for row in answer.get('onCreate', [])) + check('all_onCreate_descriptors', [{k: row.get(k) for k in fields} for row in answer.get('onCreate', [])], [{k: row[k] for k in fields} for row in reference['onCreate']]) + elif domain == 'managed': + n = reference['artifact'] + for group, fields in [('assembly', ['name', 'version']), ('module', ['name', 'mvid'])]: + for field in fields: + check(group + '.' + field, answer.get(group, {}).get(field), n[group][field]) + for key in ['classification', 'target_frameworks']: + check(key, answer.get(key), n[key]) + for key in ['module_refs', 'pinvoke_imports']: + check(key, answer.get('native_boundaries', {}).get(key), reference['boundaries'][key]) + check('artifact_coverage', answer.get('coverage', {}).get('artifact'), n['coverage']) + check('native_boundaries_coverage', answer.get('coverage', {}).get('native_boundaries'), reference['boundaries']['coverage']) + elif domain == 'managed-nonempty': + n = reference['boundaries'] + check('module_mvid', answer.get('module_mvid'), n['module']['mvid']) + check('summary', {key: answer.get('summary', {}).get(key) for key in n['summary']}, n['summary']) + check('all_original_module_refs', answer.get('module_refs'), n['module_refs']) + check('first3_complete_pinvoke_records', answer.get('pinvoke_first3'), n['pinvoke_imports'][:3]) + check('coverage', answer.get('coverage'), n['coverage']) + elif domain == 'firmware': + source = producer_results['inspect_firmware_regions'][-1] + n = source['normalized_result'] + result = answer['result'] + check('input_size', result.get('input_size'), n['input_size']) + check('examined_coverage', result.get('coverage', result.get('examined_coverage')), n['coverage']) + fields = ['provider_id', 'offset', 'reported_size', 'signature', 'description', 'reported_confidence', 'size_basis'] + check('all_original_region_records', [{k: row.get(k) for k in fields} for row in result.get('regions', [])], [{k: row[k] for k in fields} for row in n['regions']]) + engine = result.get('engine', result.get('provider_provenance', {}).get('engine', {})) + for key in ['name', 'version']: + check('engine.' + key, engine.get(key), n['engine'][key]) + check('engine_executable_digest', engine.get('executable_sha256', engine.get('artifact_sha256')), n['engine']['executable_sha256']) + elif domain == 'inventory': + for key in ['manifest', 'entry_kind_counts', 'integrity_contradictions']: + check(key, answer.get(key), reference[key]) + original = producer_results['inspect_artifact'][-1]['normalized_result'] + if original.get('kind') == 'summary': + branches = original['summary']['inspection']['unexplored_branches'] + else: + branches = original['unexplored_branches'] + # Caller-selected integrity policy participates in atomic Evidence and + # branch identity. Match those IDs to this original response, not a + # reference invocation that may use a different policy. + check('unexplored_branches_with_original_ids', answer.get('unexplored_branches'), branches) + coverage = answer.get('coverage', {}) + check('inspection_coverage', coverage.get('inspection', coverage), reference['coverage']) + paths = answer.get('requested_paths') + if isinstance(paths, dict): + paths = [row for value in paths.values() for row in (value if isinstance(value, list) else [value]) if row is not None] + canonical = lambda row: json.dumps(row, sort_keys=True, separators=(',', ':')) + check('original_selected_path_records', sorted(map(canonical, paths or [])), sorted(map(canonical, reference['requested_paths']))) + elif domain == 'javascript': + for key in ['input_path', 'source_selection', 'statistics', 'electron']: + check(key, answer.get(key), reference[key]) + coverage = answer['coverage'] + expected = reference['coverage'] + for key in ['application_unknowns', 'semantic_unknowns']: + check('coverage.' + key, coverage.get(key), expected[key]) + for layer in ['application', 'semantic']: + for key, value in expected[layer].items(): + if key in ['limits', 'families']: continue + check('coverage.' + layer + '.' + key, coverage[layer].get(key), value) + # Budgets vary with parent heap and belong to this actual invocation. + # Exact optional provenance remains a separate strict check; omissions + # must not be hidden by accepting the coarse coverage summary. + original = producer_results['analyze_javascript_application'][-1]['normalized_result']['summary']['coverage'] + check('full_coverage_provenance', coverage, original) + # The question names types/digests, not literal answer property names. + # Exact node IDs select the producer's main.js records. No source or + # Evidence values are rewritten to accept these answer labels. + select = lambda rows: sorted([{ + 'node_id': row.get('node_id'), + 'kind': row.get('kind', row.get('type')), + 'identity': row.get('identity'), + 'hashes': row.get('hashes', row.get('digests')), + } for row in rows], key=lambda row: row['node_id']) + check('all_selected_module_identities', select(answer.get('modules', [])), select(reference['modules'])) + elif domain == 'native': + for key in ['entry_address', 'pseudocode_prefix', 'caller_count', 'callee_count']: + check(key, answer.get(key), reference[key]) + containing = answer['containing_procedure'] + expected_containing = reference['containing_procedure']['procedure'] + for key in ['address', 'name']: + check('containing_procedure.' + key, containing.get(key, containing.get('procedure', {}).get(key)), expected_containing[key]) + procedure = answer['procedure'] + expected = reference['procedure'] + for key in ['address', 'name', 'classification', 'signature', 'locals']: + check('procedure.' + key, procedure.get(key), expected[key]) + check('procedure.body_ranges', procedure.get('body_ranges', procedure.get('body', {}).get('ranges')), expected['body']['ranges']) + check('procedure.total_bytes', procedure.get('total_bytes', procedure.get('body', {}).get('total_bytes')), expected['body']['total_bytes']) + calls = summary['completed_mcp_calls'] + checks['default_analysis_not_disabled'] = all(not call['arguments'].get('skip_auto_analysis', False) for call in calls if call['tool'] == 'open_binary') + checks['session_close_completed'] = any(call['tool'] == 'close_binary' and call['status'] == 'completed' for call in calls) + elif domain == 'browser': + source = producer_results['capture_browser_scenario'][-1] + n = source['normalized_result'] + for key in ['browser', 'completeness']: + check(key, answer.get(key), n[key]) + check('start_origin', answer.get('start_origin'), n['scenario']['start_origin']) + step_count = answer.get('step_count') + check('step_count', step_count.get('captured_steps') if isinstance(step_count, dict) else step_count, len(n['steps'])) + final = n['steps'][-1] + expected_url = final['after_url'] if isinstance(answer.get('final_url'), dict) else final['after_url']['url'] + check('final_url', answer.get('final_url'), expected_url) + for key in ['dom', 'accessibility']: + expected = final['artifacts'][key] + actual = answer.get(key, {}) + if expected['state'] == 'captured': + actual = actual.get('value', actual) + for field in ['sha256', 'bytes']: + check(key + '.' + field, actual.get(field), expected['value'][field]) + else: + check(key + '.state', actual.get('state'), expected['state']) + checks['owned_browser_terminated'] = n['browser']['cleanup'] == 'terminated-owned-process' + events = n['events']['items'] + page_errors = [event for event in events if event['kind'] == 'page-error'] + if isinstance(answer.get('page_errors'), dict): + check('page_error_count', answer['page_errors'].get('count'), len(page_errors)) + network = answer.get('network', {}) + if 'observed_transactions' in network: + check('network_transaction_count', network['observed_transactions'], len({event['transaction_id'] for event in events if event.get('transaction_id') is not None})) + if 'observed_response_statuses' in network: + statuses = {} + for event in events: + if event['kind'] == 'response': + status = str(event['status']) + statuses[status] = statuses.get(status, 0) + 1 + check('network_response_statuses', network['observed_response_statuses'], statuses) + scopes = [json.loads(line) for line in args.resource_log.read_text().splitlines() if line.startswith('{')] + scope = next(value for value in reversed(scopes) if 'resource_scope' in value) + checks['no_resource_abort'] = scope['abort'] is None + events = scope.get('resource_events_final') + checks['resource_counters_verified'] = scope.get('resource_counters_verified') is True + checks['no_observed_oom_or_task_limit'] = ( + isinstance(events, dict) + and all(events.get('memory.events', {}).get(k) == 0 for k in ['max', 'oom', 'oom_kill']) + and events.get('pids.events', {}).get('max') == 0 + ) + checks['no_resource_limit_event'] = ( + checks['resource_counters_verified'] + and checks['no_observed_oom_or_task_limit'] + and events['memory.events'].get('high') == 0 + ) + report = { + 'domain': domain, 'checks': checks, 'all_selected_checks_pass': all(checks.values()), + 'differences': differences, 'tool_errors': tool_errors, 'summary': summary, + 'fingerprints': {key: value for key, value in settings.items() if 'sha256' in key or key in ['runtime_revision', 'enabled_tools', 'authority']}, + 'answer_file_sha256': hashlib.sha256((args.trial / 'answer.txt').read_bytes()).hexdigest(), + 'answer_had_json_fence': fenced, 'reported_evidence_ids': sorted(reported_ids), 'resource': scope, + 'scope': 'Fixed selected-fact review against actual public producer observations. Equivalent explicitly named answer representations are accepted; unknowns/nulls remain checked. Limitations, unsupported claims and broader behavior still require transcript review. Token usage and failed calls are retained; zero exit status is not a factual grade.', + } + with args.output.open('x') as output: + output.write(json.dumps(report, indent=2, ensure_ascii=False) + '\n') + print(json.dumps({'domain': domain, 'passed': report['all_selected_checks_pass'], 'failed_checks': [key for key, value in checks.items() if not value]})) + raise SystemExit(0 if report['all_selected_checks_pass'] else 1) + + +if __name__ == '__main__': + main() diff --git a/experiments/analysis-efficiency/verify-migration.mjs b/experiments/analysis-efficiency/verify-migration.mjs new file mode 100644 index 000000000..c5f0244ae --- /dev/null +++ b/experiments/analysis-efficiency/verify-migration.mjs @@ -0,0 +1,163 @@ +import assert from "node:assert/strict"; +import { createHash } from "node:crypto"; +import { readFile, writeFile, mkdir } from "node:fs/promises"; +import { isAbsolute, resolve } from "node:path"; +import { parseArgs } from "node:util"; +import { Client } from "@modelcontextprotocol/client"; +import { StdioClientTransport } from "@modelcontextprotocol/client/stdio"; + +const { values } = parseArgs({ + options: { + "runtime-root": { type: "string" }, + revision: { type: "string" }, + bundle: { type: "string" }, + "inspection-reply": { type: "string" }, + output: { type: "string" }, + }, +}); +for (const key of ["runtime-root", "bundle", "inspection-reply", "output"]) + assert.ok(values[key] && isAbsolute(values[key]), `Supply --${key} ABS`); +assert.ok(values.revision); +await mkdir(values.output, { mode: 0o700 }); +const sha = (value) => createHash("sha256").update(value).digest("hex"); +const bundleBytes = await readFile(values.bundle); +const oldBundle = JSON.parse(bundleBytes.toString("utf8")); +const oldWasm = oldBundle.records.find( + (record) => record.operation === "inspect_wasm_artifact", +); +assert.ok(oldWasm); +const inspectionBytes = await readFile(values["inspection-reply"]); +const oldInspection = JSON.parse( + inspectionBytes.toString("utf8"), +).structuredContent; +assert.equal(oldInspection.operation, "inspect_artifact"); +const definitions = new Map(); +const client = new Client({ + name: "rea-real-evidence-migration", + version: "1", +}); +// An absent explicitly selected WABT directory makes accidental re-analysis +// fail. Imported/portable views must operate on existing Evidence alone. +const transport = new StdioClientTransport({ + command: process.execPath, + args: [resolve(values["runtime-root"], "scripts/rea.mjs"), "mcp"], + env: { + ...process.env, + REA_LOG_LEVEL: "silent", + NODE_OPTIONS: "--max-old-space-size=512", + REA_WABT_BIN_DIRECTORY: resolve(values.output, "absent-wabt"), + }, + stderr: "pipe", +}); +const calls = []; +const call = async (name, arguments_) => { + const reply = await client.callTool( + { name, arguments: arguments_ }, + { toolDefinition: definitions.get(name), timeout: 120_000 }, + ); + await writeFile( + resolve(values.output, `call-${calls.length + 1}.json`), + JSON.stringify(reply), + ); + assert.notEqual(reply.isError, true, JSON.stringify(reply)); + const result = reply.structuredContent ?? JSON.parse(reply.content[0].text); + calls.push({ + name, + arguments: + name === "inspect_analysis_view" && arguments_.source.kind === "inline" + ? { + source: { + kind: "inline", + original_evidence_id: arguments_.source.evidence.evidence_id, + }, + view: arguments_.view, + } + : arguments_, + evidence_id: result.evidence_id ?? null, + }); + return result; +}; +let report; +try { + await client.connect(transport); + transport.stderr?.resume(); + for (const definition of (await client.listTools()).tools) + definitions.set(definition.name, definition); + await call("import_evidence_bundle", { path: values.bundle }); + const source = { + kind: "retained-evidence", + evidence_id: oldWasm.evidence_id, + }; + const summary = await call("inspect_analysis_view", { + source, + view: { kind: "summary" }, + }); + const exports = await call("inspect_analysis_view", { + source, + view: { kind: "wasm", facet: "exports", offset: 0, limit: 4 }, + }); + assert.equal( + summary.normalized_result.parent_evidence_id, + oldWasm.evidence_id, + ); + assert.equal(summary.subject.digest.sha256, oldWasm.subject.digest.sha256); + assert.equal( + summary.normalized_result.summary.wat.sha256, + oldWasm.normalized_result.wat.sha256, + ); + assert.deepEqual( + exports.normalized_result.item, + oldWasm.normalized_result.exports.slice(0, 4), + ); + const inventory = await call("inspect_analysis_view", { + source: { kind: "inline", evidence: oldInspection }, + view: { kind: "summary" }, + }); + assert.equal( + inventory.normalized_result.parent_evidence_id, + oldInspection.evidence_id, + ); + const oldAtomic = oldInspection.normalized_result.substeps[0].evidence; + assert.deepEqual( + inventory.normalized_result.summary.manifest, + oldAtomic.normalized_result.manifest, + ); + assert.deepEqual(inventory.normalized_result.summary.inventory_evidence_ids, [ + oldAtomic.evidence_id, + ]); + const exportedPath = resolve(values.output, "exported-after-import.json"); + await call("export_evidence_bundle", { path: exportedPath }); + const exported = JSON.parse(await readFile(exportedPath, "utf8")); + assert.deepEqual( + exported.records.find( + (record) => record.evidence_id === oldWasm.evidence_id, + ), + oldWasm, + ); + assert.equal(sha(await readFile(values.bundle)), sha(bundleBytes)); + assert.equal( + sha(await readFile(values["inspection-reply"])), + sha(inspectionBytes), + ); + report = { + runtime_revision: values.revision, + original_bundle_sha256: sha(bundleBytes), + original_inspection_reply_sha256: sha(inspectionBytes), + source_files_unchanged: true, + imported_wasm_source_evidence_id: oldWasm.evidence_id, + original_wasm_record_preserved_exactly: true, + typed_summary_and_first4_exports_match: true, + portable_old_inspection_and_atomic_inventory_identity_preserved: true, + original_inventory_evidence_id: oldAtomic.evidence_id, + calls, + scope: + "Actual old complete Evidence imported or passed inline to the new runtime; original authentication, IDs, records, source bytes and selected facts preserved. WABT is unavailable and no producer analysis is requested. Existing file import/export is used, not an engine database cache.", + }; +} finally { + await client.close(); +} +await writeFile( + resolve(values.output, "summary.json"), + JSON.stringify(report, null, 2), +); +process.stdout.write(JSON.stringify(report) + "\n"); diff --git a/src/application/analysisView/AnalysisViewService.ts b/src/application/analysisView/AnalysisViewService.ts index bf7e52ff6..4af70eb4e 100644 --- a/src/application/analysisView/AnalysisViewService.ts +++ b/src/application/analysisView/AnalysisViewService.ts @@ -69,6 +69,7 @@ export const inspectAnalysisViewValidated = ( evidenceId: parent.evidence_id, operation: parent.operation, normalizedResult: parent.normalized_result, + subject: parent.subject, limitations: parent.limitations, parameters: parent.parameters, artifact: diff --git a/src/application/wasm/WasmArtifactService.ts b/src/application/wasm/WasmArtifactService.ts index a59b66f6b..25b844e54 100644 --- a/src/application/wasm/WasmArtifactService.ts +++ b/src/application/wasm/WasmArtifactService.ts @@ -1,4 +1,3 @@ -import { createHash } from "node:crypto"; import { isAbsolute } from "node:path"; import type { ExecutionOptions } from "../AnalysisProvider.js"; import type { WasmArtifactPort } from "./WasmArtifactPort.js"; @@ -12,6 +11,7 @@ import { createEvidence, type Evidence } from "../../domain/evidence.js"; import { inspectWasmArtifactInputSchema, wasmArtifactSchema, + wasmArtifactMatchesSubject, } from "../../domain/wasm/wasmArtifact.js"; import { analysisInputErrorFromIssues } from "../../domain/inputIssueProjection.js"; import { err, ok, type Result } from "../../domain/result.js"; @@ -66,10 +66,7 @@ export class WasmArtifactService { if ( !report.success || report.data.artifact.path !== input.data.path || - execution.subject?.path !== input.data.path || - execution.subject.sha256 !== report.data.artifact.sha256 || - createHash("sha256").update(report.data.wat.text).digest("hex") !== - report.data.wat.sha256 || + !wasmArtifactMatchesSubject(report.data.artifact, execution.subject) || JSON.stringify(report.data.glue.map((item) => item.artifact.path)) !== JSON.stringify(input.data.glue_paths) || JSON.stringify(report.data.candidates.map((item) => item.path)) !== diff --git a/src/cli/wasmCommands.ts b/src/cli/wasmCommands.ts index a6b32b18f..b91bda033 100644 --- a/src/cli/wasmCommands.ts +++ b/src/cli/wasmCommands.ts @@ -1,7 +1,6 @@ import { z } from "incur"; import { resolve } from "node:path"; -import { createWasmArtifactService } from "../composition/wasm.js"; -import { CLI_COMMANDS } from "../cliCommandNames.js"; +import { wasmArtifactModule } from "../composition/wasm.js"; import { logCliCommand } from "../cliLogging.js"; import { projectAnalysisError } from "../domain/analysisErrorProjection.js"; import type { Logger } from "pino"; @@ -14,8 +13,8 @@ export const registerWasmCommands = ( logger: Logger, environment: Readonly, ): void => { - const service = createWasmArtifactService(environment); - cli.command(CLI_COMMANDS.inspectWasmArtifact, { + const service = wasmArtifactModule.create(environment); + cli.command(wasmArtifactModule.cliCommand, { description: "Inspect an explicit local WASM artifact with WABT", args: z.object({ path: z.string().describe("Local WASM artifact"), @@ -35,7 +34,7 @@ export const registerWasmCommands = ( try { return await logCliCommand( logger, - CLI_COMMANDS.inspectWasmArtifact, + wasmArtifactModule.cliCommand, async () => { const result = await service.inspect( { diff --git a/src/composition/wasm.ts b/src/composition/wasm.ts index c8a67b388..ddf308074 100644 --- a/src/composition/wasm.ts +++ b/src/composition/wasm.ts @@ -1,7 +1,29 @@ import { WasmArtifactService } from "../application/wasm/WasmArtifactService.js"; import { WabtArtifactProvider } from "../wasm/WabtArtifactProvider.js"; +import { INSPECT_WASM_ARTIFACT_CONTRACT } from "../contracts/wasm/wasmToolContracts.js"; +import { CLI_COMMANDS } from "../cliCommandNames.js"; /** Compose the caller-supplied offline toolchain without acquisition or import-time I/O. */ export const createWasmArtifactService = ( environment: Readonly = process.env, ): WasmArtifactService => new WasmArtifactService(new WabtArtifactProvider(environment)); + +/** + * Internal static descriptor for one named domain operation and its lifetime. + * CLI/MCP share this binding; the service keeps failed resource ownership so + * close can be retried. Construction performs no acquisition or target I/O. + */ +export const wasmArtifactModule = { + contract: INSPECT_WASM_ARTIFACT_CONTRACT, + cliCommand: CLI_COMMANDS.inspectWasmArtifact, + create( + environment: Readonly, + injected?: WasmArtifactService, + ): Pick { + const service = injected ?? createWasmArtifactService(environment); + return { + inspect: service.inspect.bind(service), + close: service.close.bind(service), + }; + }, +} as const; diff --git a/src/contracts/analysisViewToolContracts.ts b/src/contracts/analysisViewToolContracts.ts index 9346ef256..f55aafcf7 100644 --- a/src/contracts/analysisViewToolContracts.ts +++ b/src/contracts/analysisViewToolContracts.ts @@ -9,23 +9,39 @@ import { evidenceResultOf } from "./toolOutputSchemaPrimitives.js"; const RETAINED_EXAMPLE_ID = `ev_${"a".repeat(64)}`; -/** Selected views of already completed layout, JavaScript and native function Evidence. */ +/** Selected views of completed layout, JavaScript, native, inventory, WASM and web network Evidence. */ export const ANALYSIS_VIEW_TOOL_CONTRACTS = [ { name: "inspect_analysis_view", ...toolContractMetadata("inspect_analysis_view"), kind: "application", description: - "Inspect selected views of completed binary-layout, JavaScript application or native analyze_function Evidence without repeating analysis. The api-surface view groups retained HAR requests by method, origin and format-rule path template; templated segments are inferences. It reads no headers or bodies and reports no query values. Use an exact same-session retained evidence_id or portable inline Evidence. Select a summary, layout mitigations/linkage facet, one section/symbol/module, or a stable page. Module pages include assets, bundled and source modules; use node_id for ambiguous or unavailable paths. Native views select procedure, pseudocode, assembly, basic blocks, comments, callers/callees, references, unresolved calls, referenced strings/names, native API and high-pcode. Use nonnegative offset and a positive caller-selected limit (defaults 0 and 64); pseudocode uses UTF-16 code units, other native pages use rows. Returns artifact identity, parent Evidence ID, a distinct view digest, coverage, limitations and unknowns. Actual serialized size determines MCP transport admission; choose a smaller page or export the complete retained Evidence if needed.", + 'Inspect selected views of completed binary-layout, JavaScript application, native analyze_function, inspect_artifact, inventory_artifact, inspect_wasm_artifact or inspect_web_network_capture Evidence without repeating analysis. The api-surface view groups retained HAR requests by method, origin and format-rule path template; templated segments are inferences. It reads no headers or bodies and reports no query values. For a retained record use source: {kind: "retained-evidence", evidence_id: ""}. Use normalized_result.parent_evidence_id from a summary receipt. Source kind is required. Portable source uses {kind: "inline", evidence: }. For layout, JavaScript, native, inventory and WASM sources, request a summary with view: {kind: "summary"}; summary is a view kind and has no facet. The WASM summary gives validation, counts, WAT digest/length, tool profile and execution/network states without reading WAT text. Select a layout mitigations/linkage facet, one section/symbol/module, or a stable page. Module pages include assets, bundled and source modules; use node_id for ambiguous or unavailable paths. Inventory uses view: {kind: "inventory", collection: "occurrences", logical_paths: ["package.json"], offset: 0, limit: 64}. Inventory views page nodes, occurrences, edges or integrity contradictions; occurrence logical_paths matches exact recorded strings (omitted means all available, [] means none). source_complete distinguishes complete inventories from partial source records; page exhaustion does not prove absence in a partial source. WASM views use kind wasm and facet sections/imports/exports/candidates/glue/wat/headers/details/tool_profile; text facets use UTF-16 code units and source forms remain exact. Native views select procedure, pseudocode, assembly, basic blocks, comments, callers/callees, references, unresolved calls, referenced strings/names, native API and high-pcode. Use nonnegative offset and a positive caller-selected limit (defaults 0 and 64); pseudocode uses UTF-16 code units, other native pages use rows. Returns artifact identity, parent Evidence ID, a distinct view digest, coverage, limitations and unknowns. Actual serialized size determines MCP transport admission; choose a smaller page or export the complete retained Evidence if needed.', inputSchema: inspectAnalysisViewInputSchema, outputSchema: evidenceResultOf(analysisViewResultSchema), examples: [ + { + title: "Read exact inventory paths from retained inspection Evidence", + input: { + source: { + kind: "retained-evidence", + evidence_id: RETAINED_EXAMPLE_ID, + }, + view: { + kind: "inventory", + collection: "occurrences", + logical_paths: ["package.json", "main.js"], + offset: 0, + limit: 32, + }, + }, + }, { title: "Group requests from retained HAR Evidence", input: API_SURFACE_EXAMPLE.input, }, { - title: "Summarize retained JavaScript application Evidence", + title: "Summarize retained analysis Evidence, including WASM", input: { source: { kind: "retained-evidence", diff --git a/src/contracts/artifactToolContracts.ts b/src/contracts/artifactToolContracts.ts index f1935e288..287b41214 100644 --- a/src/contracts/artifactToolContracts.ts +++ b/src/contracts/artifactToolContracts.ts @@ -9,11 +9,23 @@ import { requireOutputSchema } from "./toolOutputSchemaPrimitives.js"; import { appleAssetCatalogInputSchema } from "../domain/apple/appleAssetCatalog.js"; import { dylibResolutionInputSchema } from "../domain/apple/dylibResolution.js"; import { keyedArchiveInputSchema } from "../domain/apple/keyedArchive.js"; +import { analysisDetailSchema } from "./analysisDetail.js"; /** Exact caller boundary for deterministic artifact inventory. */ export const artifactInventoryInputSchema = z.strictObject({ integrity_policy: artifactIntegrityPolicySchema, }); +/** Delivery selection belongs to the adapter, not the inventory producer. */ +export const artifactInspectionInputSchema = + artifactInventoryInputSchema.extend({ + detail: analysisDetailSchema + .unwrap() + .optional() + .describe( + "Omitted or complete returns the complete inspection Evidence. summary retains that complete Evidence in this session and returns its inspect_analysis_view summary; use normalized_result.parent_evidence_id for inventory views.", + ), + }); + /** Extraction needs no selector: it materializes every regular child file. */ export const artifactExtractionInputSchema = z.strictObject({ integrity_policy: artifactIntegrityPolicySchema, @@ -67,8 +79,8 @@ const artifact = < export const ARTIFACT_TOOL_CONTRACTS = [ artifact( "inspect_artifact", - "Inspect an active archive or application package in one call. Returns the complete content-addressed artifact graph once, as the nested inventory Evidence, with root and integrity observations, integrity contradictions, hypotheses, unexplored branches, limitations, and next probes inline. Read-only DMG mounting is used automatically on supported macOS hosts. Integrity mismatches fail by default; record-and-continue keeps mismatches explicitly untrusted. It does not extract files; use extract_artifact to materialize its regular contents.", - artifactInventoryInputSchema, + "Inspect an active archive or application package in one call. By default returns the complete content-addressed artifact graph once, as the typed nested inventory Evidence, with root and integrity observations, integrity contradictions, hypotheses, unexplored branches, limitations, and next probes inline. Optional detail summary retains the complete inspection and returns its derived summary view; read exact paths or inventory pages with inspect_analysis_view using parent_evidence_id. This changes delivery, not inventory coverage. Read-only DMG mounting is used automatically on supported macOS hosts. Integrity mismatches fail by default; record-and-continue keeps mismatches explicitly untrusted. It does not extract files; use extract_artifact to materialize its regular contents.", + artifactInspectionInputSchema, ), artifact( "extract_artifact", diff --git a/src/contracts/toolEffects.test.ts b/src/contracts/toolEffects.test.ts index 135d96ef8..0ae7cf819 100644 --- a/src/contracts/toolEffects.test.ts +++ b/src/contracts/toolEffects.test.ts @@ -25,6 +25,8 @@ const EXECUTION_WORDING_EXCEPTIONS: Readonly> = { "decodes bytecode in REA's bundled worker without executing it", inspect_wasm_artifact: "WABT validates and decompiles the module without executing it", + inspect_analysis_view: + "reports retained WASM execution/network states without performing execution", inspect_managed_artifact: "reads metadata without executing target code", inspect_managed_members: "reads metadata without executing target code", inspect_managed_native_boundaries: diff --git a/src/contracts/toolOutputSchemaGroups.ts b/src/contracts/toolOutputSchemaGroups.ts index 1f5db4cc3..8487929e6 100644 --- a/src/contracts/toolOutputSchemaGroups.ts +++ b/src/contracts/toolOutputSchemaGroups.ts @@ -1,4 +1,5 @@ import { z } from "zod"; +import { artifactInventorySummaryViewResultSchema } from "../domain/analysisView/analysisView.js"; import { evidenceBundleSummarySchema } from "../domain/evidenceRecordSummary.js"; import { BINARY_ARCHITECTURES } from "../domain/binaryTargetTypes.js"; import { @@ -37,7 +38,7 @@ import { listArchitecturesSchema, } from "../domain/native/nativeInspection.js"; import { artifactExtractionResultSchema } from "../domain/artifactGraph.js"; -import { artifactInspectionResultSchema } from "../domain/artifactInspection.js"; +import { artifactInspectionOutputSchema } from "../domain/artifactInspection.js"; import { interfaceBuilderAnalysisSchema } from "../domain/apple/interfaceBuilderGraph.js"; import { dylibResolutionResultSchema } from "../domain/apple/dylibResolution.js"; import { keyedArchiveResultSchema } from "../domain/apple/keyedArchive.js"; @@ -385,7 +386,12 @@ export const nativeOutputSchemas: Readonly> = { /** Exact Evidence schemas for provider-neutral artifact graph operations. */ export const artifactOutputSchemas: Readonly> = { - inspect_artifact: evidenceResultOf(artifactInspectionResultSchema), + inspect_artifact: evidenceResultOf( + z.union([ + artifactInspectionOutputSchema, + artifactInventorySummaryViewResultSchema, + ]), + ), extract_artifact: evidenceResultOf(artifactExtractionResultSchema), decode_interface_builder: evidenceResultOf(interfaceBuilderAnalysisSchema), inspect_keyed_archive: evidenceResultOf(keyedArchiveResultSchema), diff --git a/src/contracts/wasm/wasmToolContracts.ts b/src/contracts/wasm/wasmToolContracts.ts index 53934c643..901dd7ded 100644 --- a/src/contracts/wasm/wasmToolContracts.ts +++ b/src/contracts/wasm/wasmToolContracts.ts @@ -6,24 +6,25 @@ import type { ToolContract } from "../toolContractTypes.js"; import { toolContractMetadata } from "../toolEffects.js"; import { evidenceResultOf } from "../toolOutputSchemaPrimitives.js"; /** Optional caller-supplied WABT capability with target-free CLI/MCP parity. */ -export const WASM_TOOL_CONTRACTS = [ - { - name: "inspect_wasm_artifact", - ...toolContractMetadata("inspect_wasm_artifact"), - kind: "artifact-provider", - description: - "Validate an explicit local WASM artifact with caller-supplied WABT 1.0.42, retain exact byte SHA-256, sections, upstream import/export WAT forms and decoded WAT inline as Evidence. Optionally associate selected JavaScript glue literals with explicit local candidates while preserving ambiguous URL/basename matches. Configure absolute REA_WABT_BIN_DIRECTORY. No installation, implicit fetch, WASM execution or original-source claim.", - inputSchema: inspectWasmArtifactInputSchema, - outputSchema: evidenceResultOf(wasmArtifactSchema), - examples: [ - { - title: "Inspect a selected local WASM module", - input: { - path: "/artifacts/module.wasm", - glue_paths: [], - candidate_paths: [], - }, +export const INSPECT_WASM_ARTIFACT_CONTRACT = { + name: "inspect_wasm_artifact", + ...toolContractMetadata("inspect_wasm_artifact"), + kind: "artifact-provider", + description: + "Validate an explicit local WASM artifact with caller-supplied WABT 1.0.42, retain exact byte SHA-256, sections, upstream import/export WAT forms and decoded WAT inline as Evidence. Optionally associate selected JavaScript glue literals with explicit local candidates while preserving ambiguous URL/basename matches. Configure absolute REA_WABT_BIN_DIRECTORY. No installation, implicit fetch, WASM execution or original-source claim.", + inputSchema: inspectWasmArtifactInputSchema, + outputSchema: evidenceResultOf(wasmArtifactSchema), + examples: [ + { + title: "Inspect a selected local WASM module", + input: { + path: "/artifacts/module.wasm", + glue_paths: [], + candidate_paths: [], }, - ], - }, -] as const satisfies readonly ToolContract[]; + }, + ], +} as const satisfies ToolContract; + +/** Keep the public catalog in its existing order, using its named contract. */ +export const WASM_TOOL_CONTRACTS = [INSPECT_WASM_ARTIFACT_CONTRACT] as const; diff --git a/src/domain/analysisView/analysisView.ts b/src/domain/analysisView/analysisView.ts index fe2d408c1..ce0b55946 100644 --- a/src/domain/analysisView/analysisView.ts +++ b/src/domain/analysisView/analysisView.ts @@ -8,12 +8,20 @@ import { } from "../analysisErrorCore.js"; import { digestCanonicalValue } from "../canonicalDigest.js"; import { digestSchema, prefixedDigestSchema } from "../digests.js"; -import { evidenceSchema } from "../evidence.js"; +import { evidenceSchema, type Evidence } from "../evidence.js"; import { jsonObjectSchema, jsonValueSchema } from "../jsonValue.js"; import { err, ok, type Result } from "../result.js"; import type { AnalysisError } from "../analysisErrorBase.js"; import { binaryLayoutSchema } from "../native/binaryLayout.js"; import { functionDossierSchema } from "../hopperValues.js"; +import { + artifactGraphManifestSchema, + artifactNodeSchema, + artifactOccurrenceSchema, + artifactEdgeSchema, + integrityContradictionSchema, +} from "../artifactGraph.js"; +import { artifactInspectionResultSchema } from "../artifactInspection.js"; import { parseOwnedJavaScriptApplicationAnalysisSteps, type JavaScriptApplicationAnalysisResult, @@ -27,6 +35,13 @@ import { apiSurfaceViewSchema, } from "./webNetworkApiSurfaceView.js"; import { projectNativeFunctionView } from "./nativeFunctionView.js"; +import { projectArtifactInventoryView } from "./artifactInventoryView.js"; +import { + projectWasmArtifactView, + wasmViewRequestSchema, + wasmViewItemSchema, + wasmSummarySchema, +} from "./wasmArtifactView.js"; import type { WebNetworkApiSurfaceAccumulator } from "./WebNetworkApiSurfaceAccumulator.js"; /** Public name of the selected-view workflow. */ @@ -108,6 +123,28 @@ const nativeViewSchema = z.strictObject({ limit: z.number().int().positive().default(64), }); +/** Original inventory records in stable order, optionally matching exact paths. */ +const inventoryViewSchema = z.discriminatedUnion("collection", [ + z.strictObject({ + kind: z.literal("inventory"), + collection: z.literal("occurrences"), + logical_paths: z + .array(z.string()) + .optional() + .describe( + "Exact recorded logical paths. Omitted selects all available occurrences; an empty array selects none. This does not resolve filesystem paths.", + ), + offset: z.number().int().nonnegative().default(0), + limit: z.number().int().positive().default(64), + }), + z.strictObject({ + kind: z.literal("inventory"), + collection: z.enum(["nodes", "edges", "integrity_contradictions"]), + offset: z.number().int().nonnegative().default(0), + limit: z.number().int().positive().default(64), + }), +]); + /** Caller-selected projection of already completed analysis Evidence. */ export const analysisViewRequestSchema = z.discriminatedUnion("kind", [ summaryViewSchema, @@ -115,6 +152,8 @@ export const analysisViewRequestSchema = z.discriminatedUnion("kind", [ itemViewSchema, pageViewSchema, nativeViewSchema, + inventoryViewSchema, + wasmViewRequestSchema, apiSurfaceViewSchema, ]); @@ -192,30 +231,30 @@ const javascriptSummarySchema = z.strictObject({ limitation_count: z.number().int().nonnegative(), }); -const summaryViewResultFields = { - kind: z.literal("summary"), - view: summaryViewSchema, - parent_evidence_id: prefixedDigestSchema("ev"), - parent_digest: digestSchema, - view_digest: digestSchema, - artifact: artifactSchema, - coverage: coverageSchema, - limitations: z.array(z.string()), - unknowns: z.array(z.string()), -} as const; - -/** Summary `inspect_binary_layout` detail returns; no other view kind. */ -export const binaryLayoutSummaryViewResultSchema = z.strictObject({ - ...summaryViewResultFields, - summary: layoutSummarySchema, - parent_operation: z.literal("inspect_binary_layout"), -}); - -/** Summary `analyze_javascript_application` detail returns; no other view kind. */ -export const javascriptApplicationSummaryViewResultSchema = z.strictObject({ - ...summaryViewResultFields, - summary: javascriptSummarySchema, - parent_operation: z.literal("analyze_javascript_application"), +const inventorySummarySchema = z.strictObject({ + manifest: artifactGraphManifestSchema, + available_counts: z.strictObject({ + nodes: z.number().int().nonnegative(), + occurrences: z.number().int().nonnegative(), + edges: z.number().int().nonnegative(), + integrity_contradictions: z.number().int().nonnegative(), + }), + observed_entry_kind_counts: z.strictObject({ + file: z.number().int().nonnegative(), + directory: z.number().int().nonnegative(), + symlink: z.number().int().nonnegative(), + slice: z.number().int().nonnegative(), + }), + source_complete: z.boolean(), + inventory_evidence_ids: z.array(prefixedDigestSchema("ev")).min(1), + inspection: z + .strictObject({ + coverage: artifactInspectionResultSchema.shape.coverage, + unexplored_branches: + artifactInspectionResultSchema.shape.unexplored_branches, + }) + .nullable(), + limitation_count: z.number().int().nonnegative(), }); const viewResultBase = { @@ -224,6 +263,9 @@ const viewResultBase = { "inspect_binary_layout", "analyze_javascript_application", "analyze_function", + "inspect_artifact", + "inventory_artifact", + "inspect_wasm_artifact", ]), parent_digest: digestSchema, view_digest: digestSchema, @@ -233,6 +275,33 @@ const viewResultBase = { unknowns: z.array(z.string()), } as const; +const summaryViewResultBase = { + kind: z.literal("summary"), + view: summaryViewSchema, + ...viewResultBase, +} as const; + +/** Summary `inspect_binary_layout` detail returns; no other view kind. */ +export const binaryLayoutSummaryViewResultSchema = z.strictObject({ + ...summaryViewResultBase, + summary: layoutSummarySchema, + parent_operation: z.literal("inspect_binary_layout"), +}); + +/** Summary `analyze_javascript_application` detail returns; no other view kind. */ +export const javascriptApplicationSummaryViewResultSchema = z.strictObject({ + ...summaryViewResultBase, + summary: javascriptSummarySchema, + parent_operation: z.literal("analyze_javascript_application"), +}); + +/** Exact summary carrier for artifact inspection's optional delivery contract. */ +export const artifactInventorySummaryViewResultSchema = z.strictObject({ + ...summaryViewResultBase, + parent_operation: z.enum(["inspect_artifact", "inventory_artifact"]), + summary: inventorySummarySchema, +}); + /** Projected facts for one selected view of retained analysis Evidence. */ export const analysisViewResultSchema = z.discriminatedUnion("kind", [ z.strictObject({ @@ -247,10 +316,13 @@ export const analysisViewResultSchema = z.discriminatedUnion("kind", [ }), }), z.strictObject({ - kind: z.literal("summary"), - view: summaryViewSchema, - summary: z.union([layoutSummarySchema, javascriptSummarySchema]), - ...viewResultBase, + ...summaryViewResultBase, + summary: z.union([ + layoutSummarySchema, + javascriptSummarySchema, + inventorySummarySchema, + wasmSummarySchema, + ]), }), z.strictObject({ kind: z.literal("facet"), @@ -277,6 +349,28 @@ export const analysisViewResultSchema = z.discriminatedUnion("kind", [ item: jsonValueSchema, ...viewResultBase, }), + z.strictObject({ + kind: z.literal("wasm"), + view: wasmViewRequestSchema, + item: wasmViewItemSchema, + ...viewResultBase, + }), + z.strictObject({ + kind: z.literal("inventory"), + view: inventoryViewSchema, + manifest: artifactGraphManifestSchema, + inventory_evidence_ids: z.array(prefixedDigestSchema("ev")).min(1), + source_complete: z.boolean(), + items: z.array( + z.union([ + artifactNodeSchema, + artifactOccurrenceSchema, + artifactEdgeSchema, + integrityContradictionSchema, + ]), + ), + ...viewResultBase, + }), ]); export type AnalysisViewRequest = z.output; @@ -291,6 +385,8 @@ export interface AnalysisViewParent { readonly evidenceId: string; readonly operation: string; readonly normalizedResult: unknown; + /** Preserve the authenticated source identity, including explicit absence. */ + readonly subject?: Evidence["subject"]; readonly limitations: readonly string[]; readonly artifact?: { readonly path: string; readonly sha256: string } | null; /** Parent Evidence parameters, when the parent operation records them. */ @@ -319,7 +415,12 @@ export type UnsignedAnalysisView = | Omit< Extract, "view_digest" - >; + > + | Omit< + Extract, + "view_digest" + > + | Omit, "view_digest">; /** Attach the content digest that must change when projected bytes change. */ export const sealAnalysisView = ( @@ -364,7 +465,7 @@ const unsupportedParent = ( new AnalysisUnsupportedTargetError( INSPECT_ANALYSIS_VIEW_OPERATION, parentArtifactPath(parent), - `Parent Evidence operation ${JSON.stringify(parent.operation)} is not inspect_binary_layout, analyze_javascript_application or analyze_function.`, + `Parent Evidence operation ${JSON.stringify(parent.operation)} does not support analysis views. Use completed binary layout, JavaScript application, native function, artifact inventory/inspection, WASM, or web network capture Evidence.`, ); const parentArtifactPath = (parent: AnalysisViewParent): string => { @@ -459,5 +560,16 @@ export const projectAnalysisView = ( const projected = projectNativeFunctionView(parent, native.data, view); return projected.ok ? ok(sealAnalysisView(projected.value)) : projected; } + if (parent.operation === "inspect_wasm_artifact") { + const projected = projectWasmArtifactView(parent, view); + return projected.ok ? ok(sealAnalysisView(projected.value)) : projected; + } + if ( + parent.operation === "inspect_artifact" || + parent.operation === "inventory_artifact" + ) { + const projected = projectArtifactInventoryView(parent, view); + return projected.ok ? ok(sealAnalysisView(projected.value)) : projected; + } return err(unsupportedParent(parent)); }; diff --git a/src/domain/analysisView/analysisViewText.ts b/src/domain/analysisView/analysisViewText.ts new file mode 100644 index 000000000..e7c2bc1d4 --- /dev/null +++ b/src/domain/analysisView/analysisViewText.ts @@ -0,0 +1,55 @@ +import type { AnalysisError } from "../analysisErrorBase.js"; +import { err, ok, type Result } from "../result.js"; +import { + analysisViewInputError, + type AnalysisViewCoverage, +} from "./analysisView.js"; +import { pageViewCoverage } from "./analysisViewCoverage.js"; + +const viewError = (message: string): AnalysisError => + analysisViewInputError([ + { path: ["view"], reason: "invalid_value", message }, + ]); + +/** Slice text by UTF-16 code units without losing a partial Unicode character. */ +export const analysisTextPage = ( + body: string, + offset: number, + limit: number, + label = "Pseudocode", +): Result< + { + readonly item: { readonly text: string; readonly unit: "utf16-code-units" }; + readonly coverage: AnalysisViewCoverage; + }, + AnalysisError +> => { + const start = Math.min(offset, body.length); + const high = (n: number) => n >= 0xd800 && n <= 0xdbff; + const low = (n: number) => n >= 0xdc00 && n <= 0xdfff; + if ( + start > 0 && + start < body.length && + high(body.charCodeAt(start - 1)) && + low(body.charCodeAt(start)) + ) + return err(viewError(`${label} offset splits a UTF-16 surrogate pair.`)); + let end = Math.min(start + limit, body.length); + if ( + end > start && + end < body.length && + high(body.charCodeAt(end - 1)) && + low(body.charCodeAt(end)) + ) + end--; + if (end === start && start < body.length) + return err( + viewError( + `Increase ${label.toLowerCase()} limit to include the next complete Unicode character.`, + ), + ); + return ok({ + item: { text: body.slice(start, end), unit: "utf16-code-units" }, + coverage: pageViewCoverage(start, end - start, body.length), + }); +}; diff --git a/src/domain/analysisView/artifactInventoryView.ts b/src/domain/analysisView/artifactInventoryView.ts new file mode 100644 index 000000000..84b198dfd --- /dev/null +++ b/src/domain/analysisView/artifactInventoryView.ts @@ -0,0 +1,203 @@ +import { z } from "zod"; +import { AnalysisInputError } from "../analysisErrorCore.js"; +import type { AnalysisError } from "../analysisErrorBase.js"; +import { uniqueSorted } from "../canonicalOrdering.js"; +import { err, ok, type Result } from "../result.js"; +import { + parseArtifactInventoryEvidence, + parseArtifactInventoryResult, + validateArtifactInventorySubject, + type InventorySet, +} from "../artifactInventoryEvidence.js"; +import { + artifactInspectionResultSchema, + type ArtifactInspectionResult, +} from "../artifactInspection.js"; +import type { + AnalysisViewParent, + AnalysisViewRequest, + UnsignedAnalysisView, +} from "./analysisView.js"; +import { + completeWithinViewCoverage, + pageViewCoverage, +} from "./analysisViewCoverage.js"; + +const inputError = (message: string): AnalysisInputError => + new AnalysisInputError("inspect_analysis_view", undefined, [ + { + path: ["source"], + reason: "invalid_value", + message, + }, + ]); + +interface InventorySource { + readonly inventory: InventorySet; + readonly evidenceIds: readonly string[]; + readonly inspection: ArtifactInspectionResult | null; +} + +/** Reuse the owning inventory integrity/completeness parser for both carriers. */ +const parseSource = ( + parent: AnalysisViewParent, +): Result => { + try { + if (parent.operation === "inventory_artifact") + return ok({ + inventory: parseArtifactInventoryResult( + parent.normalizedResult, + parent.subject ?? null, + ), + evidenceIds: [parent.evidenceId], + inspection: null, + }); + const inspection = artifactInspectionResultSchema.parse( + parent.normalizedResult, + ); + for (const step of inspection.substeps) { + if ( + step.evidence_id !== step.evidence.evidence_id || + step.evidence.operation !== step.operation + ) + throw new TypeError( + "Inspection substep identity differs from its source Evidence", + ); + } + const parsed = parseArtifactInventoryEvidence( + inspection.substeps.map(({ evidence }) => evidence), + ); + const manifest = parsed.inventory.manifest; + validateArtifactInventorySubject(parent.subject ?? null, manifest); + if ( + inspection.subject.manifest_id !== manifest.manifest_id || + inspection.subject.root_artifact_id !== manifest.root_artifact_id || + inspection.subject.root_sha256 !== manifest.root_sha256 || + inspection.subject.root_format !== manifest.root_format + ) + throw new TypeError( + "Inspection subject differs from its inventory manifest", + ); + return ok({ + inventory: parsed.inventory, + evidenceIds: parsed.evidence.map(({ evidence_id }) => evidence_id), + inspection, + }); + } catch (cause: unknown) { + if (!(cause instanceof TypeError) && !(cause instanceof z.ZodError)) + throw cause; + return err(inputError(cause.message)); + } +}; + +/** Project exact source records; exhausted pages cover available records, not runtime behavior. */ +export const projectArtifactInventoryView = ( + parent: AnalysisViewParent, + view: AnalysisViewRequest, +): Result => { + const operation = parent.operation; + if (operation !== "inventory_artifact" && operation !== "inspect_artifact") + return err( + inputError( + "Inventory views require inventory_artifact or inspect_artifact Evidence.", + ), + ); + if (view.kind !== "summary" && view.kind !== "inventory") + return err( + inputError("Artifact Evidence supports summary and inventory views."), + ); + const parsed = parseSource(parent); + if (!parsed.ok) return parsed; + const { inventory, evidenceIds, inspection } = parsed.value; + const limitations = uniqueSorted([ + ...parent.limitations, + ...inventory.limitations, + ...(inspection?.limitations ?? []), + ]); + const shared = { + parent_evidence_id: parent.evidenceId, + parent_operation: operation, + parent_digest: parent.evidenceId.slice(3), + artifact: { + path: parent.artifact?.path ?? "", + sha256: inventory.manifest.root_sha256, + }, + limitations, + unknowns: [ + "Inventory does not establish application runtime behavior or complete format-specific analysis.", + ...(inventory.complete + ? [] + : [ + "Parent inventory is partial; counts and exhausted pages cover only available records. Missing matches do not establish absence.", + ]), + ], + } satisfies Pick< + UnsignedAnalysisView, + | "parent_evidence_id" + | "parent_operation" + | "parent_digest" + | "artifact" + | "limitations" + | "unknowns" + >; + if (view.kind === "summary") { + const counts = { file: 0, directory: 0, symlink: 0, slice: 0 }; + for (const occurrence of inventory.occurrences) + counts[occurrence.entry_kind]++; + const examined = + inventory.nodes.length + + inventory.occurrences.length + + inventory.edges.length; + return ok({ + kind: "summary", + view, + summary: { + manifest: inventory.manifest, + available_counts: { + nodes: inventory.nodes.length, + occurrences: inventory.occurrences.length, + edges: inventory.edges.length, + integrity_contradictions: inventory.integrityContradictions.length, + }, + observed_entry_kind_counts: counts, + source_complete: inventory.complete, + inventory_evidence_ids: [...evidenceIds], + inspection: + inspection === null + ? null + : { + coverage: inspection.coverage, + unexplored_branches: inspection.unexplored_branches, + }, + limitation_count: limitations.length, + }, + coverage: completeWithinViewCoverage(examined, examined), + ...shared, + }); + } + const paths = + view.collection === "occurrences" && view.logical_paths !== undefined + ? new Set(view.logical_paths) + : undefined; + const rows = + view.collection === "occurrences" && paths !== undefined + ? inventory.occurrences.filter((row) => paths.has(row.logical_path)) + : view.collection === "occurrences" + ? inventory.occurrences + : view.collection === "nodes" + ? inventory.nodes + : view.collection === "edges" + ? inventory.edges + : inventory.integrityContradictions; + const items = rows.slice(view.offset, view.offset + view.limit); + return ok({ + kind: "inventory", + view, + manifest: inventory.manifest, + inventory_evidence_ids: [...evidenceIds], + source_complete: inventory.complete, + items, + coverage: pageViewCoverage(view.offset, items.length, rows.length), + ...shared, + }); +}; diff --git a/src/domain/analysisView/binaryLayoutView.ts b/src/domain/analysisView/binaryLayoutView.ts index 485bfbac6..2fa2551bb 100644 --- a/src/domain/analysisView/binaryLayoutView.ts +++ b/src/domain/analysisView/binaryLayoutView.ts @@ -156,6 +156,14 @@ export const projectBinaryLayoutView = ( return err( layoutIncompatible("Native views require analyze_function Evidence."), ); + if (view.kind === "inventory") + return err( + layoutIncompatible("Inventory views require artifact Evidence."), + ); + if (view.kind === "wasm") + return err( + layoutIncompatible("WASM views require inspect_wasm_artifact Evidence."), + ); if (view.kind === "summary") return ok({ kind: "summary", diff --git a/src/domain/analysisView/javascriptApplicationView.ts b/src/domain/analysisView/javascriptApplicationView.ts index fdb0666c4..140a30f9e 100644 --- a/src/domain/analysisView/javascriptApplicationView.ts +++ b/src/domain/analysisView/javascriptApplicationView.ts @@ -247,6 +247,12 @@ export const projectJavaScriptApplicationView = ( return err( jsIncompatible("Native views require analyze_function Evidence."), ); + if (view.kind === "inventory") + return err(jsIncompatible("Inventory views require artifact Evidence.")); + if (view.kind === "wasm") + return err( + jsIncompatible("WASM views require inspect_wasm_artifact Evidence."), + ); if (view.kind === "summary") return ok({ kind: "summary", diff --git a/src/domain/analysisView/nativeFunctionView.ts b/src/domain/analysisView/nativeFunctionView.ts index 511453763..f5a29e0e1 100644 --- a/src/domain/analysisView/nativeFunctionView.ts +++ b/src/domain/analysisView/nativeFunctionView.ts @@ -1,3 +1,4 @@ +import { analysisTextPage } from "./analysisViewText.js"; import type { AnalysisError } from "../analysisErrorBase.js"; import { uniqueSorted } from "../canonicalOrdering.js"; import type { FunctionDossier } from "../hopperValues.js"; @@ -5,7 +6,6 @@ import { jsonValueSchema } from "../jsonValue.js"; import { err, ok, type Result } from "../result.js"; import { analysisViewInputError, - type AnalysisViewCoverage, type AnalysisViewParent, type AnalysisViewRequest, type UnsignedAnalysisView, @@ -59,47 +59,6 @@ const parentFields = ( }; }; -const pseudocodePage = ( - body: string, - offset: number, - limit: number, -): Result< - { - readonly item: { readonly text: string; readonly unit: "utf16-code-units" }; - readonly coverage: AnalysisViewCoverage; - }, - AnalysisError -> => { - const start = Math.min(offset, body.length); - const high = (n: number) => n >= 0xd800 && n <= 0xdbff; - const low = (n: number) => n >= 0xdc00 && n <= 0xdfff; - if ( - start > 0 && - start < body.length && - high(body.charCodeAt(start - 1)) && - low(body.charCodeAt(start)) - ) - return err(viewError("Pseudocode offset splits a UTF-16 surrogate pair.")); - let end = Math.min(start + limit, body.length); - if ( - end > start && - end < body.length && - high(body.charCodeAt(end - 1)) && - low(body.charCodeAt(end)) - ) - end--; - if (end === start && start < body.length) - return err( - viewError( - "Increase pseudocode limit to include the next complete Unicode character.", - ), - ); - return ok({ - item: { text: body.slice(start, end), unit: "utf16-code-units" }, - coverage: pageViewCoverage(start, end - start, body.length), - }); -}; - const nativeRows = ( dossier: FunctionDossier, facet: Exclude< @@ -193,7 +152,7 @@ export const projectNativeFunctionView = ( return ok(singleton(status)); } if (view.facet === "pseudocode") { - const page = pseudocodePage(dossier.pseudocode, view.offset, view.limit); + const page = analysisTextPage(dossier.pseudocode, view.offset, view.limit); return page.ok ? ok({ kind: "native", view, ...page.value, ...common }) : page; diff --git a/src/domain/analysisView/wasmArtifactView.ts b/src/domain/analysisView/wasmArtifactView.ts new file mode 100644 index 000000000..111078600 --- /dev/null +++ b/src/domain/analysisView/wasmArtifactView.ts @@ -0,0 +1,207 @@ +import { z } from "zod"; +import type { AnalysisError } from "../analysisErrorBase.js"; +import { uniqueSorted } from "../canonicalOrdering.js"; +import { err, ok, type Result } from "../result.js"; +import { + wasmArtifactSchema, + wasmArtifactMatchesSubject, +} from "../wasm/wasmArtifact.js"; +import { analysisInputErrorFromIssues } from "../inputIssueProjection.js"; +import { + analysisViewInputError, + type AnalysisViewParent, + type AnalysisViewRequest, + type UnsignedAnalysisView, +} from "./analysisView.js"; +import { + completeWithinViewCoverage, + pageViewCoverage, +} from "./analysisViewCoverage.js"; +import { analysisTextPage } from "./analysisViewText.js"; + +/** Source forms and text from completed WASM Evidence; this never executes a VM. */ +export const wasmViewRequestSchema = z.strictObject({ + kind: z.literal("wasm"), + facet: z.enum([ + "sections", + "imports", + "exports", + "candidates", + "glue", + "wat", + "headers", + "details", + "tool_profile", + ]), + offset: z.number().int().nonnegative().default(0), + limit: z.number().int().positive().default(64), +}); + +export const wasmViewItemSchema = z.union([ + wasmArtifactSchema.shape.sections, + wasmArtifactSchema.shape.imports, + wasmArtifactSchema.shape.candidates, + wasmArtifactSchema.shape.glue, + wasmArtifactSchema.shape.tool_profile, + z.strictObject({ text: z.string(), unit: z.literal("utf16-code-units") }), +]); + +/** Counts are observations of the completed producer, independent of runtime semantics. */ +export const wasmSummarySchema = z.strictObject({ + bytes: wasmArtifactSchema.shape.artifact.shape.bytes, + validation: wasmArtifactSchema.shape.validation, + available_counts: z.strictObject({ + sections: z.number().int().nonnegative(), + imports: z.number().int().nonnegative(), + exports: z.number().int().nonnegative(), + candidates: z.number().int().nonnegative(), + glue: z.number().int().nonnegative(), + }), + wat: wasmArtifactSchema.shape.wat + .omit({ text: true }) + .extend({ code_units: z.number().int().nonnegative() }), + tool_profile: wasmArtifactSchema.shape.tool_profile, + runtime_execution: wasmArtifactSchema.shape.runtime_execution, + network_fetch: wasmArtifactSchema.shape.network_fetch, + limitation_count: z.number().int().nonnegative(), +}); + +/** Read the exact retained WABT forms with source identity and explicit uncertainty. */ +export const projectWasmArtifactView = ( + parent: AnalysisViewParent, + view: AnalysisViewRequest, +): Result => { + if (view.kind !== "summary" && view.kind !== "wasm") + return err( + analysisViewInputError([ + { + path: ["view"], + reason: "invalid_value", + message: "WASM Evidence supports summary and wasm views.", + }, + ]), + ); + const parsed = wasmArtifactSchema.safeParse(parent.normalizedResult); + if (!parsed.success) + return err( + analysisInputErrorFromIssues( + "inspect_analysis_view", + parsed.error.issues.map((issue) => ({ + ...issue, + path: ["source", "normalized_result", ...issue.path], + })), + { source: { normalized_result: parent.normalizedResult } }, + { cause: parsed.error }, + ), + ); + const report = parsed.data; + if ( + !wasmArtifactMatchesSubject( + report.artifact, + parent.subject == null + ? null + : { + path: parent.subject.local_path, + sha256: parent.subject.digest.sha256, + }, + ) + ) + return err( + analysisViewInputError([ + { + path: ["source", "subject"], + reason: "invalid_value", + message: + "WASM parent Evidence subject must identify the report's exact path and SHA-256.", + }, + ]), + ); + const limitations = uniqueSorted([ + ...parent.limitations, + ...report.limitations, + ]); + const shared = { + parent_evidence_id: parent.evidenceId, + parent_operation: "inspect_wasm_artifact" as const, + parent_digest: parent.evidenceId.slice(3), + artifact: { path: report.artifact.path, sha256: report.artifact.sha256 }, + limitations, + unknowns: [ + "Decoded WAT is not original source. Runtime behavior and network acquisition were not performed.", + ...(report.glue.some((glue) => glue.parse_status !== "complete") + ? [ + "Selected glue parsing is incomplete; unmatched references do not establish absence.", + ] + : []), + ], + }; + if (view.kind === "summary") + return ok({ + kind: "summary", + view, + ...shared, + summary: { + bytes: report.artifact.bytes, + validation: report.validation, + available_counts: { + sections: report.sections.length, + imports: report.imports.length, + exports: report.exports.length, + candidates: report.candidates.length, + glue: report.glue.length, + }, + wat: { + sha256: report.wat.sha256, + representation: report.wat.representation, + code_units: report.wat.text.length, + }, + tool_profile: report.tool_profile, + runtime_execution: report.runtime_execution, + network_fetch: report.network_fetch, + limitation_count: limitations.length, + }, + coverage: completeWithinViewCoverage( + report.sections.length + report.imports.length + report.exports.length, + report.sections.length + report.imports.length + report.exports.length, + ), + }); + if (view.facet === "tool_profile") { + if (view.offset !== 0) + return err( + analysisViewInputError([ + { + path: ["view", "offset"], + reason: "invalid_value", + message: "Singleton tool_profile requires offset 0.", + }, + ]), + ); + return ok({ + kind: "wasm", + view, + item: report.tool_profile, + coverage: completeWithinViewCoverage(1, 1), + ...shared, + }); + } + if ( + view.facet === "wat" || + view.facet === "headers" || + view.facet === "details" + ) { + const text = view.facet === "wat" ? report.wat.text : report[view.facet]; + const page = analysisTextPage(text, view.offset, view.limit, view.facet); + return page.ok + ? ok({ kind: "wasm", view, ...page.value, ...shared }) + : page; + } + const rows = report[view.facet]; + const item = rows.slice(view.offset, view.offset + view.limit); + return ok({ + kind: "wasm", + view, + item, + coverage: pageViewCoverage(view.offset, item.length, rows.length), + ...shared, + }); +}; diff --git a/src/domain/artifactGraph.ts b/src/domain/artifactGraph.ts index fd5d0177d..4860fd209 100644 --- a/src/domain/artifactGraph.ts +++ b/src/domain/artifactGraph.ts @@ -101,7 +101,7 @@ const artifactCommandSchema = z.object({ export type ArtifactCommand = z.infer; /** One content-addressed application artifact in deterministic manifest order. */ -const artifactNodeSchema = z.object({ +export const artifactNodeSchema = z.object({ artifact_id: artifactIdSchema, format: artifactContentFormatSchema, sha256: sha256Schema, @@ -113,7 +113,7 @@ const artifactNodeSchema = z.object({ }); /** One path or virtual location where an artifact occurs in its parent. */ -const artifactOccurrenceSchema = z.object({ +export const artifactOccurrenceSchema = z.object({ occurrence_id: occurrenceIdSchema, artifact_id: artifactIdSchema.nullable(), parent_occurrence_id: occurrenceIdSchema.nullable(), @@ -159,7 +159,7 @@ export const integrityContradictionSchema = z.object({ * Parent-child derivation relation between two content-addressed artifacts. * The child artifact and its logical path are those of the edge's occurrence. */ -const artifactEdgeSchema = z.object({ +export const artifactEdgeSchema = z.object({ edge_id: edgeIdSchema, ordinal: z.number().int().min(0), parent_artifact_id: artifactIdSchema, @@ -177,7 +177,7 @@ const artifactEdgeSchema = z.object({ }); /** Stable cryptographic commitment and counts for a complete artifact graph. */ -const artifactGraphManifestSchema = z.object({ +export const artifactGraphManifestSchema = z.object({ manifest_id: manifestIdSchema, root_artifact_id: artifactIdSchema, root_sha256: sha256Schema, diff --git a/src/domain/artifactInspection.ts b/src/domain/artifactInspection.ts index 19ccb9b8f..a5bb99d16 100644 --- a/src/domain/artifactInspection.ts +++ b/src/domain/artifactInspection.ts @@ -103,6 +103,24 @@ export type ArtifactInspectionResult = z.infer< typeof artifactInspectionResultSchema >; +/** + * The current producer guarantees a concrete inventory payload. Specialize + * its output without narrowing the historical inspection/Evidence input + * parser or applying inventory defaults inside an authenticated imported record. + */ +export const artifactInspectionOutputSchema = + artifactInspectionResultSchema.safeExtend({ + substeps: z + .array( + artifactInspectionResultSchema.shape.substeps.element.safeExtend({ + evidence: evidenceSchema.safeExtend({ + normalized_result: artifactInventoryResultSchema, + }), + }), + ) + .min(1), + }); + /** Project one successful inventory Evidence record into an inspection report. */ export const createArtifactInspection = ( inventoryInput: unknown, diff --git a/src/domain/artifactInventoryEvidence.ts b/src/domain/artifactInventoryEvidence.ts index 33491c7a7..a44902ac3 100644 --- a/src/domain/artifactInventoryEvidence.ts +++ b/src/domain/artifactInventoryEvidence.ts @@ -37,37 +37,63 @@ export interface ParsedInventorySet { /** Parse, merge, and verify inventory Evidence pages. */ export const parseArtifactInventoryEvidence = ( input: unknown, -): ParsedInventorySet => - assembleInventorySet( - (Array.isArray(input) ? input : [input]).map((value) => { - const evidence = parseEvidence(value); - const inventoryEvidence = - evidence.operation === "inventory_artifact" - ? evidence - : evidence.operation === "inspect_artifact" - ? artifactInspectionResultSchema.parse(evidence.normalized_result) - .substeps[0]?.evidence - : undefined; - if (inventoryEvidence === undefined) - throw new TypeError( - "Artifact comparison requires inspect_artifact Evidence", - ); - const inventory = artifactInventoryResultSchema.parse( - inventoryEvidence.normalized_result, +): ParsedInventorySet => { + const pages = (Array.isArray(input) ? input : [input]).map((value) => { + const evidence = parseEvidence(value); + const inventoryEvidence = + evidence.operation === "inventory_artifact" + ? evidence + : evidence.operation === "inspect_artifact" + ? artifactInspectionResultSchema.parse(evidence.normalized_result) + .substeps[0]?.evidence + : undefined; + if (inventoryEvidence === undefined) + throw new TypeError( + "Artifact comparison requires inspect_artifact Evidence", ); - if ( - inventoryEvidence.subject === null || - inventoryEvidence.subject.digest.sha256 !== - inventory.manifest.root_sha256 || - inventoryEvidence.subject.format !== inventory.manifest.root_format - ) - throw new TypeError( - "Artifact inventory Evidence subject does not match its root digest", - ); - validateInventoryPage(inventory); - return { evidence, inventory }; - }), - ); + const inventory = artifactInventoryResultSchema.parse( + inventoryEvidence.normalized_result, + ); + validateArtifactInventorySubject( + inventoryEvidence.subject, + inventory.manifest, + ); + validateInventoryPage(inventory); + return { evidence, inventory }; + }); + return { + evidence: pages.map(({ evidence }) => evidence), + inventory: assembleInventoryResults( + pages.map(({ inventory }) => inventory), + ), + }; +}; + +/** Validate one already authenticated parent payload with the same graph proofs. */ +export const parseArtifactInventoryResult = ( + input: unknown, + subject: Evidence["subject"], +): InventorySet => { + const inventory = artifactInventoryResultSchema.parse(input); + validateArtifactInventorySubject(subject, inventory.manifest); + validateInventoryPage(inventory); + return assembleInventoryResults([inventory]); +}; + +/** Both inventory carriers require the same authenticated root identity. */ +export const validateArtifactInventorySubject = ( + subject: Evidence["subject"], + manifest: ArtifactInventoryResult["manifest"], +): void => { + if ( + subject === null || + subject.digest.sha256 !== manifest.root_sha256 || + subject.format !== manifest.root_format + ) + throw new TypeError( + "Artifact inventory Evidence subject does not match its root digest", + ); +}; const validateInventoryPage = (inventory: ArtifactInventoryResult): void => { assertUnique( @@ -91,45 +117,30 @@ const validateInventoryPage = (inventory: ArtifactInventoryResult): void => { throw new TypeError("Artifact node ID is not content-addressed"); }; -const assembleInventorySet = ( - pages: readonly { - readonly evidence: Evidence; - readonly inventory: ArtifactInventoryResult; - }[], -): ParsedInventorySet => { +const assembleInventoryResults = ( + pages: readonly ArtifactInventoryResult[], +): InventorySet => { const first = pages[0]; if (first === undefined) throw new TypeError("Artifact inventory requires Evidence pages"); - const manifestJson = canonicalJson( - first.inventory.manifest, - "Artifact inventory", - ); + const manifestJson = canonicalJson(first.manifest, "Artifact inventory"); const nodes = new Map(); const occurrences = new Map(); const paths = new Map(); const edges = new Map(); for (const page of pages) { - if ( - canonicalJson(page.inventory.manifest, "Artifact inventory") !== - manifestJson - ) + if (canonicalJson(page.manifest, "Artifact inventory") !== manifestJson) throw new TypeError("Artifact inventory pages do not share one manifest"); if ( - canonicalJson( - page.inventory.integrity_contradictions, - "Artifact inventory", - ) !== - canonicalJson( - first.inventory.integrity_contradictions, - "Artifact inventory", - ) + canonicalJson(page.integrity_contradictions, "Artifact inventory") !== + canonicalJson(first.integrity_contradictions, "Artifact inventory") ) throw new TypeError( "Artifact inventory pages do not share integrity contradictions", ); - for (const node of page.inventory.nodes) + for (const node of page.nodes) mergeExact(nodes, node.artifact_id, node, "node"); - for (const occurrence of page.inventory.occurrences) { + for (const occurrence of page.occurrences) { mergeExact( occurrences, occurrence.occurrence_id, @@ -138,21 +149,21 @@ const assembleInventorySet = ( ); mergeExact(paths, occurrence.logical_path, occurrence, "logical path"); } - for (const edge of page.inventory.edges) + for (const edge of page.edges) mergeExact(edges, edge.edge_id, edge, "edge"); } - const inventory = orderedInventory(first.inventory, pages, { + const inventory = orderedInventory(first, pages, { nodes, occurrences, edges, }); if (inventory.complete) validateCompleteInventory(inventory); - return { evidence: pages.map(({ evidence }) => evidence), inventory }; + return inventory; }; const orderedInventory = ( first: ArtifactInventoryResult, - pages: readonly { readonly inventory: ArtifactInventoryResult }[], + pages: readonly ArtifactInventoryResult[], values: { readonly nodes: ReadonlyMap; readonly occurrences: ReadonlyMap; @@ -178,7 +189,7 @@ const orderedInventory = ( edges, integrityContradictions: first.integrity_contradictions, limitations: [ - ...new Set(pages.flatMap(({ inventory }) => inventory.limitations)), + ...new Set(pages.flatMap((inventory) => inventory.limitations)), ].sort(compareUnicodeCodePoints), complete: nodes.length === first.manifest.node_count && diff --git a/src/domain/wasm/wasmArtifact.ts b/src/domain/wasm/wasmArtifact.ts index ed2a120e0..254a55a3a 100644 --- a/src/domain/wasm/wasmArtifact.ts +++ b/src/domain/wasm/wasmArtifact.ts @@ -1,4 +1,5 @@ import { z } from "zod"; +import { createHash } from "node:crypto"; import { localPathStringSchema } from "../localPath.js"; @@ -83,6 +84,15 @@ export const wasmArtifactSchema = z limitations: z.array(z.string()), }) .superRefine((value, context) => { + if ( + createHash("sha256").update(value.wat.text, "utf8").digest("hex") !== + value.wat.sha256 + ) + context.addIssue({ + code: "custom", + path: ["wat", "sha256"], + message: "Decoded WAT digest does not match its UTF-8 text.", + }); const selected = value.candidates[0]; if ( selected?.path !== value.artifact.path || @@ -125,3 +135,13 @@ export const wasmArtifactSchema = z export type InspectWasmArtifactInput = z.infer< typeof inspectWasmArtifactInputSchema >; +export type WasmArtifact = z.infer; + +/** A WASM observation must identify the exact selected path and byte digest. */ +export const wasmArtifactMatchesSubject = ( + artifact: WasmArtifact["artifact"], + subject: Pick | null | undefined, +): subject is Pick => + subject != null && + subject.path === artifact.path && + subject.sha256 === artifact.sha256; diff --git a/src/server/createServer.ts b/src/server/createServer.ts index 5c738f4bc..0e5c69cba 100644 --- a/src/server/createServer.ts +++ b/src/server/createServer.ts @@ -1,5 +1,5 @@ import type { WasmArtifactService } from "../application/wasm/WasmArtifactService.js"; -import { createWasmArtifactService } from "../composition/wasm.js"; +import { wasmArtifactModule } from "../composition/wasm.js"; import { registerWasmTools } from "./registerWasmTools.js"; import { snapshotEnvironment } from "../process/snapshotEnvironment.js"; import { createPeResourcesService } from "../composition/binaryDiagnostics.js"; @@ -246,8 +246,10 @@ export const createServer = ( ); const android = options.androidAnalysis ?? createAndroidAnalysisProvider(environment); - const wasmArtifact = - options.wasmArtifact ?? createWasmArtifactService(environment); + const wasmArtifact = wasmArtifactModule.create( + environment, + options.wasmArtifact, + ); const apktool = options.apktoolAnalysis ?? createApktoolResourceAnalysisProvider(environment); @@ -399,7 +401,7 @@ const registerConfiguredAnalysisTools = ( withAdmittedAnalysis, }: ServerToolContext, android: AndroidAnalysisPort, - wasmArtifact: WasmArtifactService, + wasmArtifact: Pick, jeb: JebAnalysisPort, firmwareAnalysis: FirmwareAnalysisService, ): void => { @@ -557,6 +559,7 @@ const registerBinaryAnalysisTools = ({ activeTarget, recordEvidence, recordEvidenceWithUnknown, + evidenceById, withAdmittedAnalysis, }: ServerToolContext): void => { const recordUnknown = @@ -592,7 +595,7 @@ const registerBinaryAnalysisTools = ({ withAdmittedAnalysis, }; registerEvidenceTools(server, NATIVE_TOOL_CONTRACTS, evidenceOptions); - registerArtifactTools(server, evidenceOptions); + registerArtifactTools(server, { ...evidenceOptions, evidenceById }); registerManagedTools(server, evidenceOptions); if (session !== undefined) registerManagedWorkflowTools(server, { diff --git a/src/server/registerArtifactTools.ts b/src/server/registerArtifactTools.ts index c3509bae6..cf95fea41 100644 --- a/src/server/registerArtifactTools.ts +++ b/src/server/registerArtifactTools.ts @@ -1,7 +1,14 @@ import type { EvidenceMcpServer } from "./EvidenceMcpServer.js"; import type { EvidenceWriter } from "../application/investigation/InvestigationRecordPort.js"; -import { ARTIFACT_TOOL_CONTRACTS } from "../contracts/artifactToolContracts.js"; +import { + ARTIFACT_TOOL_CONTRACTS, + artifactInspectionInputSchema, +} from "../contracts/artifactToolContracts.js"; +import type { EvidenceLookup } from "../application/EvidenceInputResolver.js"; +import { summarizeRetainedAnalysis } from "../application/analysisView/AnalysisViewService.js"; +import { createArtifactExtractionDestination } from "../application/artifacts/ArtifactExtractionDestination.js"; +import { ok } from "../domain/result.js"; import type { BinaryTarget } from "../domain/binaryTargetTypes.js"; import type { Logger } from "pino"; import { registerEvidenceTools } from "./registerEvidenceTools.js"; @@ -14,11 +21,29 @@ export const registerArtifactTools = ( readonly logger: Logger; readonly activeTarget: (() => BinaryTarget | undefined) | undefined; readonly recordEvidence: EvidenceWriter["recordEvidence"] | undefined; + readonly evidenceById?: EvidenceLookup | undefined; readonly withAdmittedAnalysis: import("./analysisAdmission.js").WithAdmittedAnalysis; }, ): void => { registerEvidenceTools(server, ARTIFACT_TOOL_CONTRACTS, { ...options, + parametersForExecution: (operation, parameters) => { + if (operation === "inspect_artifact") { + const { detail: _detail, ...execution } = + artifactInspectionInputSchema.parse(parameters); + return execution; + } + return operation === "extract_artifact" + ? { ...parameters, output_root: createArtifactExtractionDestination() } + : parameters; + }, + projectEvidence: (operation, parameters, evidence) => + operation === "inspect_artifact" && parameters.detail === "summary" + ? summarizeRetainedAnalysis(evidence, { + recordEvidence: options.recordEvidence, + evidenceById: options.evidenceById, + }) + : ok(evidence), sourceEvidence: (operation, result) => operation === "inspect_artifact" ? artifactInspectionResultSchema diff --git a/src/server/registerEvidenceTools.ts b/src/server/registerEvidenceTools.ts index c03079c9c..a8efac6de 100644 --- a/src/server/registerEvidenceTools.ts +++ b/src/server/registerEvidenceTools.ts @@ -8,12 +8,13 @@ import type { import type { ToolContract } from "../contracts/toolContractTypes.js"; import type { BinaryTarget } from "../domain/binaryTargetTypes.js"; import { createEvidence, type Evidence } from "../domain/evidence.js"; -import { jsonObjectSchema } from "../domain/jsonValue.js"; +import { jsonObjectSchema, type JsonValue } from "../domain/jsonValue.js"; +import type { AnalysisError } from "../domain/analysisErrorBase.js"; +import { ok, type Result } from "../domain/result.js"; import type { Logger } from "pino"; import { mcpProgressReporter } from "./mcpProgress.js"; import { logToolExecution } from "./toolLogging.js"; import { toolRegistrationOptions } from "./toolRegistrationOptions.js"; -import { createArtifactExtractionDestination } from "../application/artifacts/ArtifactExtractionDestination.js"; import type { WithAdmittedAnalysis } from "./analysisAdmission.js"; interface EvidenceToolRegistration { @@ -28,6 +29,15 @@ interface EvidenceToolRegistration { operation: Exclude, result: import("../domain/jsonValue.js").JsonValue, ) => readonly Evidence[]; + readonly parametersForExecution?: ( + operation: Exclude, + parameters: Readonly>, + ) => Readonly>; + readonly projectEvidence?: ( + operation: Exclude, + parameters: Readonly>, + evidence: Evidence, + ) => Result; } /** Register provider-backed contracts that return atomic Evidence observations. */ @@ -57,12 +67,8 @@ export const registerEvidenceTools = ( }); const parameters = jsonObjectSchema.parse(input); const executionParameters = - contract.name === "extract_artifact" - ? { - ...parameters, - output_root: createArtifactExtractionDestination(), - } - : parameters; + options.parametersForExecution?.(contract.name, parameters) ?? + parameters; const execution = await logToolExecution( options.logger, contract.name, @@ -108,9 +114,14 @@ export const registerEvidenceTools = ( contract, ); } - const recorded = options.recordEvidence?.(evidence); + const projected = + options.projectEvidence?.(contract.name, parameters, evidence) ?? + ok(evidence); + if (!projected.ok) + return server.delivery.toCallToolResult(projected, contract); + const recorded = options.recordEvidence?.(projected.value); return server.delivery.toEvidenceToolResult( - evidence, + projected.value, contract, recorded, ); diff --git a/src/server/registerWasmTools.ts b/src/server/registerWasmTools.ts index 7aebed5b2..9c0bd7699 100644 --- a/src/server/registerWasmTools.ts +++ b/src/server/registerWasmTools.ts @@ -2,7 +2,7 @@ import type { EvidenceMcpServer } from "./EvidenceMcpServer.js"; import type { WasmArtifactService } from "../application/wasm/WasmArtifactService.js"; import type { EvidenceWriter } from "../application/investigation/InvestigationRecordPort.js"; -import { toolContract } from "../contracts/toolContracts.js"; +import { wasmArtifactModule } from "../composition/wasm.js"; import type { Logger } from "pino"; import { createEvidenceToolHandler } from "./contractToolHandler.js"; import type { WithAdmittedAnalysis } from "./analysisAdmission.js"; @@ -11,12 +11,12 @@ import { toolRegistrationOptions } from "./toolRegistrationOptions.js"; /** Bind the named WASM contract to shared validation and session Evidence recording. */ export const registerWasmTools = ( server: EvidenceMcpServer, - service: WasmArtifactService, + service: Pick, logger: Logger, recordEvidence?: EvidenceWriter["recordEvidence"], withAdmittedAnalysis?: WithAdmittedAnalysis, ): void => { - const contract = toolContract("inspect_wasm_artifact"); + const contract = wasmArtifactModule.contract; const handler = createEvidenceToolHandler( server, (input, options) => service.inspect(input, options), diff --git a/tests/boundary/cli/analysisViewCli.test.ts b/tests/boundary/cli/analysisViewCli.test.ts index 0ba1a3ecf..4e63358d7 100644 --- a/tests/boundary/cli/analysisViewCli.test.ts +++ b/tests/boundary/cli/analysisViewCli.test.ts @@ -1,10 +1,10 @@ import { apiSurfaceHar } from "../../fixtures/historicalHar.js"; -import { parseEvidence } from "../../../src/domain/evidence.js"; import { createHash } from "node:crypto"; -import { mkdir, writeFile } from "node:fs/promises"; +import { mkdir, unlink, writeFile } from "node:fs/promises"; import { join } from "node:path"; import { expect } from "vitest"; +import { TextReader, Uint8ArrayWriter, ZipWriter } from "@zip.js/zip.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; import { cliTest } from "../../support/cli/cliFixture.js"; @@ -15,11 +15,56 @@ import { analysisViewCaptureEvidence, analysisViewObjectDenseCapture, } from "../../fixtures/analysisView.js"; -import { createEvidence } from "../../../src/domain/evidence.js"; +import { createEvidence, parseEvidence } from "../../../src/domain/evidence.js"; import { ghidraFunctionDossier } from "../../../src/domain/ghidraValues.fixture.js"; import { createEvidenceBundle } from "../../../src/domain/evidenceBundle.js"; import { inspectEvidenceBundle } from "../../../src/application/investigation/InspectEvidenceBundle.js"; +cliTest( + "projects portable artifact inventory after the original ZIP is removed", + async ({ cli }) => { + const root = await createTestTempDirectory("rea-inventory-view-cli-"); + const archive = join(root, "fixture.zip"); + const writer = new ZipWriter(new Uint8ArrayWriter()); + await writer.add("main.js", new TextReader("console.log('portable');\n")); + await writeFile(archive, await writer.close()); + const inspected = await cli.run({ + arguments: ["inspect-artifact", archive, "--json"], + }); + expect(inspected.exitCode).toBe(0); + const parent = parseEvidence(inspected.json); + await unlink(archive); + const request = join(root, "view.json"); + await writeFile( + request, + JSON.stringify({ + source: { kind: "inline", evidence: parent }, + view: { + kind: "inventory", + collection: "occurrences", + logical_paths: ["main.js"], + }, + }), + ); + const projected = await cli.run({ + arguments: ["inspect-analysis-view", request, "--json"], + }); + expect(projected.exitCode).toBe(0); + expect(projected.json).toMatchObject({ + operation: "inspect_analysis_view", + evidence_links: [parent.evidence_id], + normalized_result: { + parent_evidence_id: parent.evidence_id, + kind: "inventory", + source_complete: true, + artifact: { path: archive, sha256: parent.subject?.digest.sha256 }, + items: [expect.objectContaining({ logical_path: "main.js" })], + coverage: { total: 1, exhausted: true, next_offset: null }, + }, + }); + }, +); + cliTest( "admits portable captures before parsing and preserves capture caveats", async ({ cli }) => { diff --git a/tests/boundary/mcp/artifactMcp.inlineInventory.test.ts b/tests/boundary/mcp/artifactMcp.inlineInventory.test.ts index 9241bf471..4cb3133d5 100644 --- a/tests/boundary/mcp/artifactMcp.inlineInventory.test.ts +++ b/tests/boundary/mcp/artifactMcp.inlineInventory.test.ts @@ -1,8 +1,9 @@ import { parseMcpToolError } from "../../fixtures/mcpToolError.js"; -import { readFile, writeFile } from "node:fs/promises"; +import { readFile, unlink, writeFile } from "node:fs/promises"; import { join } from "node:path"; import { Client, InMemoryTransport } from "@modelcontextprotocol/client"; +import { Ajv2020 } from "ajv/dist/2020.js"; import { TextReader, Uint8ArrayWriter, ZipWriter } from "@zip.js/zip.js"; import { expect, it } from "vitest"; import { z } from "zod"; @@ -12,8 +13,81 @@ import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; import { createTestBinarySession } from "../../fixtures/binarySession.js"; import { ArtifactProvider } from "../../../src/artifacts/ArtifactProvider.js"; import { createServer } from "../../../src/server/createServer.js"; +import { + createEvidence, + parseEvidence, + type Evidence, +} from "../../../src/domain/evidence.js"; +import { + artifactInspectionResultSchema, + createArtifactInspection, +} from "../../../src/domain/artifactInspection.js"; +import { + artifactInventoryResultSchema, + type ArtifactInventoryResult, +} from "../../../src/domain/artifactGraph.js"; +import { + analysisViewResultSchema, + type AnalysisViewResult, +} from "../../../src/domain/analysisView/analysisView.js"; -it("inspects a standalone keyed archive through MCP with original object identities", async () => { +const assertInventorySubjectValidation = async ( + client: Client, + source: Evidence, +): Promise => { + const result = + source.operation === "inspect_artifact" + ? artifactInspectionResultSchema.parse(source.normalized_result) + .substeps[0]?.evidence.normalized_result + : source.normalized_result; + const inventory = artifactInventoryResultSchema.parse(result); + if (source.subject === null) + throw new Error("Fixture requires a known subject"); + for (const target of [ + undefined, + { + path: source.subject.local_path, + sha256: inventory.manifest.root_sha256, + format: "file" as const, + }, + { + path: source.subject.local_path, + sha256: "f".repeat(64), + format: inventory.manifest.root_format, + }, + ]) { + const inconsistent = createEvidence(target, source.provider, { + operation: source.operation, + parameters: source.parameters, + result: source.normalized_result, + }); + for (const view of [ + { kind: "summary" }, + { kind: "inventory", collection: "nodes", offset: 0, limit: 1 }, + ]) { + const reply = await client.callTool({ + name: "inspect_analysis_view", + arguments: { source: { kind: "inline", evidence: inconsistent }, view }, + }); + expect(parseMcpToolError(reply)).toMatchObject({ + error: { + code: "invalid_request", + details: { + issues: [ + { + path: ["source"], + reason: "invalid_value", + message: expect.stringContaining("subject does not match"), + }, + ], + }, + }, + }); + } + } +}; + +it("preserves keyed archive identities and rejects inconsistent inventory carrier subjects", async () => { const directory = await createTestTempDirectory("rea-keyed-mcp-"); const path = join(directory, "archive.plist"); await writeFile( @@ -99,6 +173,19 @@ it("inspects a standalone keyed archive through MCP with original object identit }, }, }); + const inspected = await client.callTool({ + name: "inspect_artifact", + arguments: {}, + }); + expect(inspected.isError).not.toBe(true); + const parent = parseEvidence(inspected.structuredContent); + const inspection = artifactInspectionResultSchema.parse( + parent.normalized_result, + ); + const inventory = inspection.substeps[0]?.evidence; + if (inventory === undefined) throw new Error("Missing inventory Evidence"); + await assertInventorySubjectValidation(client, inventory); + await assertInventorySubjectValidation(client, parent); } finally { await client.close(); await server.close(); @@ -106,7 +193,201 @@ it("inspects a standalone keyed archive through MCP with original object identit } }); -it("returns all 520 ZIP file occurrences in one inspect_artifact MCP result", async () => { +const createInventoryViewReader = async (client: Client, parent: Evidence) => { + const advertised = (await client.listTools()).tools.find( + (tool) => tool.name === "inspect_analysis_view", + ); + if (advertised?.outputSchema === undefined) + throw new Error("Missing advertised view schemas"); + const ajv = new Ajv2020({ strict: false, validateFormats: false }); + const inputSchema: Record = advertised.inputSchema; + const outputSchema: Record = advertised.outputSchema; + const validateInput = ajv.compile(inputSchema); + const validateOutput = ajv.compile(outputSchema); + expect( + validateInput({ + source: { kind: "retained-evidence", evidence_id: parent.evidence_id }, + view: { kind: "inventory", collection: "nodes", logical_paths: [] }, + }), + ).toBe(false); + const readView = async ( + view: Record, + source = { + kind: "retained-evidence", + evidence_id: parent.evidence_id, + }, + ) => { + const arguments_ = { source, view }; + expect( + validateInput(arguments_), + JSON.stringify(validateInput.errors), + ).toBe(true); + const reply = await client.callTool({ + name: "inspect_analysis_view", + arguments: arguments_, + }); + if (reply.isError) + throw new Error(JSON.stringify(parseMcpToolError(reply))); + expect( + validateOutput(reply.structuredContent), + JSON.stringify(validateOutput.errors), + ).toBe(true); + const evidence = parseEvidence(reply.structuredContent); + return { + evidence, + result: analysisViewResultSchema.parse(evidence.normalized_result), + }; + }; + return readView; +}; + +const assertHistoricalInventory = async ( + client: Client, + input: { + archive: string; + parent: Evidence; + inventoryEvidence: Evidence; + graph: ArtifactInventoryResult; + }, +) => { + const { archive, parent, inventoryEvidence, graph } = input; + // Historical records may omit a later inventory field. Input parsing must + // not insert defaults inside that authenticated child Evidence. + const { integrity_contradictions: _contradictions, ...legacyInventory } = + graph; + const legacyTarget = { + path: archive, + sha256: graph.manifest.root_sha256, + format: "zip" as const, + }; + const legacySource = createEvidence( + legacyTarget, + inventoryEvidence.provider, + { + operation: "inventory_artifact", + parameters: {}, + result: legacyInventory, + }, + ); + const legacyInspection = createArtifactInspection(legacySource); + expect(legacyInspection.substeps[0]?.evidence).toEqual(legacySource); + expect( + legacyInspection.substeps[0]?.evidence.normalized_result, + ).not.toHaveProperty("integrity_contradictions"); + const legacyParent = createEvidence(legacyTarget, parent.provider, { + operation: "inspect_artifact", + parameters: {}, + result: legacyInspection, + }); + const legacyReply = await client.callTool({ + name: "inspect_analysis_view", + arguments: { + source: { kind: "inline", evidence: legacyParent }, + view: { kind: "summary" }, + }, + }); + expect( + legacyReply.isError, + JSON.stringify(legacyReply.structuredContent), + ).not.toBe(true); + expect( + parseEvidence(legacyReply.structuredContent).normalized_result, + ).toMatchObject({ + parent_evidence_id: legacyParent.evidence_id, + summary: { + source_complete: true, + inventory_evidence_ids: [legacySource.evidence_id], + }, + }); +}; + +const assertPortableRoundtrip = async ( + client: Client, + directory: string, + parent: Evidence, + originalView: AnalysisViewResult, +) => { + const saved = join(directory, "evidence.json"); + const exported = await client.callTool({ + name: "export_evidence_bundle", + arguments: { path: saved }, + }); + if (exported.isError) + throw new Error(JSON.stringify(parseMcpToolError(exported))); + const closed = await client.callTool({ + name: "close_binary", + arguments: {}, + }); + if (closed.isError) + throw new Error(JSON.stringify(parseMcpToolError(closed))); + const missing = await client.callTool({ + name: "inspect_analysis_view", + arguments: { + source: { kind: "retained-evidence", evidence_id: parent.evidence_id }, + view: { kind: "summary" }, + }, + }); + expect(parseMcpToolError(missing)).toMatchObject({ + error: { details: { reason: "missing" } }, + }); + const imported = await client.callTool({ + name: "import_evidence_bundle", + arguments: { path: saved }, + }); + if (imported.isError) + throw new Error(JSON.stringify(parseMcpToolError(imported))); + const restored = await client.callTool({ + name: "inspect_analysis_view", + arguments: { + source: { kind: "retained-evidence", evidence_id: parent.evidence_id }, + view: { kind: "summary" }, + }, + }); + if (restored.isError) + throw new Error(JSON.stringify(parseMcpToolError(restored))); + expect(parseEvidence(restored.structuredContent).normalized_result).toEqual( + originalView, + ); +}; + +const assertSummaryDelivery = async ( + client: Client, + session: ReturnType, + complete: Evidence, +) => { + expect(complete.parameters).not.toHaveProperty("detail"); + const reply = await client.callTool({ + name: "inspect_artifact", + arguments: { detail: "summary" }, + }); + expect(reply.isError, JSON.stringify(reply.structuredContent)).not.toBe(true); + const projected = parseEvidence(reply.structuredContent); + const view = analysisViewResultSchema.parse(projected.normalized_result); + expect(projected.operation).toBe("inspect_analysis_view"); + expect(view.kind).toBe("summary"); + const retained = session.evidenceById(view.parent_evidence_id); + if (retained === undefined) throw new Error("Missing complete inspection"); + expect(retained.operation).toBe("inspect_artifact"); + expect(retained.parameters).toMatchObject({ detail: "summary" }); + expect(retained.normalized_result).toEqual(complete.normalized_result); + expect(retained.evidence_id).not.toBe(projected.evidence_id); + const inspection = artifactInspectionResultSchema.parse( + retained.normalized_result, + ); + expect( + session.evidenceById(inspection.substeps[0]?.evidence_id ?? ""), + ).toEqual(inspection.substeps[0]?.evidence); + const explicit = await client.callTool({ + name: "inspect_artifact", + arguments: { detail: "complete" }, + }); + expect(explicit.isError).not.toBe(true); + expect(parseEvidence(explicit.structuredContent).normalized_result).toEqual( + complete.normalized_result, + ); +}; + +it("keeps complete ZIP output and reads retained inventory views after the source is removed", async () => { const directory = await createTestTempDirectory("rea-artifact-inline-"); const archive = join(directory, "many.zip"); const writer = new ZipWriter(new Uint8ArrayWriter()); @@ -138,27 +419,154 @@ it("returns all 520 ZIP file occurrences in one inspect_artifact MCP result", as expect(result.isError, JSON.stringify(result.structuredContent)).not.toBe( true, ); - const inspection = z - .object({ - substeps: z.array( - z.object({ - evidence: z.object({ - normalized_result: z.object({ - occurrences: z.array(z.object({ logical_path: z.string() })), - }), - }), - }), - ), - }) - .parse( - z - .object({ normalized_result: z.unknown() }) - .parse(result.structuredContent).normalized_result, - ); + const parent = parseEvidence(result.structuredContent); + await assertSummaryDelivery(client, session, parent); + const parsedInspection = artifactInspectionResultSchema.parse( + parent.normalized_result, + ); + const inventoryEvidence = parsedInspection.substeps[0]?.evidence; + if (inventoryEvidence === undefined) + throw new Error("Missing inventory Evidence"); + const graph = artifactInventoryResultSchema.parse( + inventoryEvidence.normalized_result, + ); + expect(graph.occurrences).toHaveLength(fileCount + 1); + // Follow-ups must use retained source facts, not reopen this archive. + await unlink(archive); + const readView = await createInventoryViewReader(client, parent); + const summary = await readView({ kind: "summary" }); + expect(summary.result).toMatchObject({ + kind: "summary", + parent_evidence_id: parent.evidence_id, + artifact: { path: archive, sha256: graph.manifest.root_sha256 }, + summary: { + manifest: graph.manifest, + source_complete: true, + available_counts: { occurrences: fileCount + 1 }, + observed_entry_kind_counts: { file: fileCount + 1 }, + inventory_evidence_ids: [inventoryEvidence.evidence_id], + }, + }); + expect(summary.evidence.evidence_links).toEqual([parent.evidence_id]); + const paths = ["files/0.txt", "files/519.txt", "missing.txt"]; + const selected = await readView({ + kind: "inventory", + collection: "occurrences", + logical_paths: paths, + offset: 0, + limit: 1, + }); + expect(selected.result).toMatchObject({ + kind: "inventory", + source_complete: true, + items: graph.occurrences + .filter((row) => paths.includes(row.logical_path)) + .slice(0, 1), + coverage: { examined: 1, total: 2, next_offset: 1, exhausted: false }, + }); + const next = await readView({ + kind: "inventory", + collection: "occurrences", + logical_paths: paths, + offset: 1, + limit: 1, + }); + expect(next.result).toMatchObject({ + coverage: { examined: 1, next_offset: null, exhausted: true }, + }); + const none = await readView({ + kind: "inventory", + collection: "occurrences", + logical_paths: [], + }); + expect(none.result).toMatchObject({ + items: [], + coverage: { total: 0, exhausted: true }, + }); + + const partial = createEvidence( + { path: archive, sha256: graph.manifest.root_sha256, format: "zip" }, + inventoryEvidence.provider, + { + operation: "inventory_artifact", + parameters: {}, + result: { ...graph, occurrences: graph.occurrences.slice(0, 1) }, + }, + ); + const partialReply = await client.callTool({ + name: "inspect_analysis_view", + arguments: { + source: { kind: "inline", evidence: partial }, + view: { + kind: "inventory", + collection: "occurrences", + logical_paths: ["missing.txt"], + }, + }, + }); expect( - inspection.substeps[0]?.evidence.normalized_result.occurrences, - ).toHaveLength(fileCount + 1); + parseEvidence(partialReply.structuredContent).normalized_result, + ).toMatchObject({ + source_complete: false, + items: [], + unknowns: expect.arrayContaining([ + expect.stringContaining("Missing matches do not establish absence"), + ]), + }); + + await assertHistoricalInventory(client, { + archive, + parent, + inventoryEvidence, + graph, + }); + await assertPortableRoundtrip(client, directory, parent, summary.result); } finally { - await Promise.allSettled([client.close(), server.close(), session.close()]); + await client.close(); + await server.close(); + await session.close(); + } +}); + +it("refuses summary delivery without a retained Evidence owner and preserves complete delivery", async () => { + const directory = await createTestTempDirectory("rea-inventory-fixed-"); + const archive = join(directory, "fixture.zip"); + const writer = new ZipWriter(new Uint8ArrayWriter()); + await writer.add("main.js", new TextReader("console.log('fixed');\n")); + await writeFile(archive, await writer.close()); + const session = createTestBinarySession(new ArtifactProvider(process.env)); + const opened = await session.open(archive); + if (!opened.ok) throw opened.error; + const server = createServer({ kind: "fixed", analysis: session }); + const client = new Client({ name: "artifact-fixed-test", version: "1" }); + const [clientTransport, serverTransport] = + InMemoryTransport.createLinkedPair(); + try { + await server.connect(serverTransport); + await client.connect(clientTransport); + const summary = await client.callTool({ + name: "inspect_artifact", + arguments: { detail: "summary" }, + }); + expect(parseMcpToolError(summary)).toMatchObject({ + error: { + code: "capability_unavailable", + message: expect.stringContaining("does not retain Evidence"), + }, + }); + const complete = await client.callTool({ + name: "inspect_artifact", + arguments: {}, + }); + expect(complete.isError).not.toBe(true); + expect( + parseEvidence(complete.structuredContent).normalized_result, + ).toMatchObject({ + substeps: [expect.objectContaining({ operation: "inventory_artifact" })], + }); + } finally { + await client.close(); + await server.close(); + await session.close(); } }); diff --git a/tests/boundary/mcp/sessionMcp.test.ts b/tests/boundary/mcp/sessionMcp.test.ts index 42e92e501..5b8c7de00 100644 --- a/tests/boundary/mcp/sessionMcp.test.ts +++ b/tests/boundary/mcp/sessionMcp.test.ts @@ -281,7 +281,7 @@ describe("target-free MCP workflow", () => { ); expect( beforeTools.find(({ name }) => name === "inspect_artifact")?.description, - ).toContain("Returns the complete content-addressed artifact graph"); + ).toMatch(/\breturns the complete content-addressed artifact graph\b/iu); expect(beforeNames).toContain("open_binary"); expect(beforeNames).toContain("binary_session"); expect(beforeNames).toContain("current_document"); diff --git a/tests/boundary/mcp/wasmArtifactMcp.test.ts b/tests/boundary/mcp/wasmArtifactMcp.test.ts index 0a4a2610c..ce42e9b9c 100644 --- a/tests/boundary/mcp/wasmArtifactMcp.test.ts +++ b/tests/boundary/mcp/wasmArtifactMcp.test.ts @@ -1,11 +1,14 @@ -import type { Client } from "@modelcontextprotocol/client"; +import { Client, InMemoryTransport } from "@modelcontextprotocol/client"; import { Ajv2020 } from "ajv/dist/2020.js"; import { expect, it, onTestFinished, vi } from "vitest"; import { readFile, realpath, writeFile } from "node:fs/promises"; import { join } from "node:path"; import { WasmArtifactService } from "../../../src/application/wasm/WasmArtifactService.js"; import { toolContract } from "../../../src/contracts/toolContracts.js"; -import { parseEvidence } from "../../../src/domain/evidence.js"; +import { createEvidence, parseEvidence } from "../../../src/domain/evidence.js"; +import { createHash } from "node:crypto"; +import { z } from "zod"; +import { createServer } from "../../../src/server/createServer.js"; import { ok } from "../../../src/domain/result.js"; import { createApplicationMcpHarness } from "../../fixtures/applicationMcpHarness.js"; import { createTestBinarySession } from "../../fixtures/binarySession.js"; @@ -141,3 +144,214 @@ it("advertises exact valid schemas and records inline WASM interface evidence wi await harness.close(); expect(close).toHaveBeenCalled(); }); + +it("reads an oversized completed WASM result through its reported retained reference", async () => { + const text = "(module)\n" + ";;😀\n" + " ".repeat(6 * 1024 * 1024); + const value = { + ...wasmArtifactFixture(), + imports: ['(import "env" "memory.with punctuation" (memory 1))'], + exports: ['(export "first" (func 0))', '(export "second" (func 1))'], + wat: { + text, + sha256: createHash("sha256").update(text, "utf8").digest("hex"), + representation: "decoded-wat" as const, + }, + }; + const service = new WasmArtifactService({ + close: () => Promise.resolve(), + inspect: () => Promise.resolve(ok(wasmArtifactExecution(value))), + }); + const harness = await createApplicationMcpHarness({ + serverOptions: { wasmArtifact: service }, + }); + onTestFinished(harness.close); + const reply = await harness.client.callTool({ + name: "inspect_wasm_artifact", + arguments: { path: value.artifact.path }, + }); + const reference = z + .object({ + details: z.object({ + reported_limits: z.object({ + evidence_reference: z.object({ + kind: z.literal("retained-evidence"), + evidence_id: z.string(), + }), + }), + }), + }) + .parse(parseMcpToolError(reply).error).details + .reported_limits.evidence_reference; + const catalog = await harness.client.listTools(); + const advertised = catalog.tools.find( + (tool) => tool.name === "inspect_analysis_view", + ); + if (advertised?.outputSchema === undefined) + throw new Error("Missing WASM view schemas"); + const ajv = new Ajv2020({ strict: false, validateFormats: false }); + const input: Record = advertised.inputSchema; + const output: Record = advertised.outputSchema; + for (const [view, expected] of [ + [ + { kind: "summary" }, + { + summary: { + validation: "valid", + available_counts: { imports: 1, exports: 2 }, + runtime_execution: "not-performed", + network_fetch: "not-performed", + wat: { sha256: value.wat.sha256, code_units: text.length }, + }, + }, + ], + [ + { kind: "wasm", facet: "exports", offset: 1, limit: 1 }, + { + item: [value.exports[1]], + coverage: { total: 2, next_offset: null, exhausted: true }, + }, + ], + [{ kind: "wasm", facet: "imports" }, { item: value.imports }], + [ + { kind: "wasm", facet: "wat", offset: 0, limit: 16 }, + { + item: { text: text.slice(0, 16), unit: "utf16-code-units" }, + coverage: { total: text.length, next_offset: 16, exhausted: false }, + }, + ], + ] as const) { + const args = { source: reference, view }; + expect(ajv.validate(input, args), JSON.stringify(ajv.errors)).toBe(true); + const selected = await harness.client.callTool({ + name: "inspect_analysis_view", + arguments: args, + }); + expect( + selected.isError, + JSON.stringify(selected.structuredContent), + ).not.toBe(true); + expect( + ajv.validate(output, selected.structuredContent), + JSON.stringify(ajv.errors), + ).toBe(true); + const evidence = parseEvidence(selected.structuredContent); + expect(evidence.confidence).toBe("derived"); + expect(evidence.evidence_links).toEqual([reference.evidence_id]); + expect(evidence.normalized_result).toMatchObject({ + parent_evidence_id: reference.evidence_id, + ...expected, + }); + } + const original = harness.session.evidenceById(reference.evidence_id); + expect(original?.normalized_result).toEqual(value); + const inconsistent = createEvidence( + { + path: value.artifact.path, + sha256: value.artifact.sha256, + format: "file", + }, + { id: "historical-wabt", name: "Historical WABT", version: "1" }, + { + operation: "inspect_wasm_artifact", + parameters: {}, + result: { ...value, wat: { ...value.wat, sha256: "f".repeat(64) } }, + }, + ); + const invalid = await harness.client.callTool({ + name: "inspect_analysis_view", + arguments: { + source: { kind: "inline", evidence: inconsistent }, + view: { kind: "summary" }, + }, + }); + expect(parseMcpToolError(invalid)).toMatchObject({ + error: { + code: "invalid_request", + details: { + issues: [ + { + path: ["source", "normalized_result", "wat", "sha256"], + reason: "invalid_value", + }, + ], + }, + }, + }); + await harness.client.ping(); +}); + +it("rejects absent or inconsistent WASM source identity before deriving portable views", async () => { + const value = wasmArtifactFixture(); + const harness = await createApplicationMcpHarness({ + serverOptions: { environment: {} }, + }); + onTestFinished(harness.close); + for (const target of [ + undefined, + { + path: `${value.artifact.path}.other`, + sha256: value.artifact.sha256, + format: "file" as const, + }, + { + path: value.artifact.path, + sha256: "f".repeat(64), + format: "file" as const, + }, + ]) { + const source = createEvidence( + target, + { id: "historical-wabt", name: "Historical WABT", version: "1" }, + { operation: "inspect_wasm_artifact", parameters: {}, result: value }, + ); + for (const view of [ + { kind: "summary" }, + { kind: "wasm", facet: "exports", offset: 0, limit: 1 }, + ]) { + const reply = await harness.client.callTool({ + name: "inspect_analysis_view", + arguments: { source: { kind: "inline", evidence: source }, view }, + }); + expect(parseMcpToolError(reply)).toMatchObject({ + error: { + code: "invalid_request", + details: { + issues: [{ path: ["source", "subject"], reason: "invalid_value" }], + }, + }, + }); + } + } + await harness.client.ping(); +}); + +it("retries cleanup of the injected WASM module after server shutdown fails", async () => { + const close = vi + .fn() + .mockRejectedValueOnce(new Error("retained WASM workspace")) + .mockResolvedValue(undefined); + const service = new WasmArtifactService({ + close, + inspect: () => Promise.resolve(ok(wasmArtifactExecution())), + }); + const session = createTestBinarySession(() => { + throw new Error("No deep provider"); + }); + const server = createServer( + { kind: "session", session }, + { wasmArtifact: service }, + ); + const client = new Client({ name: "wasm-cleanup-retry", version: "1" }); + const [a, b] = InMemoryTransport.createLinkedPair(); + try { + await server.connect(b); + await client.connect(a); + await expect(server.close()).rejects.toThrow("REA session cleanup failed"); + await server.close(); + expect(close).toHaveBeenCalledTimes(2); + } finally { + await client.close(); + await server.close(); + await session.close(); + } +}); diff --git a/tests/fixtures/wasm/artifact.ts b/tests/fixtures/wasm/artifact.ts index 9119d612b..d4cc803b4 100644 --- a/tests/fixtures/wasm/artifact.ts +++ b/tests/fixtures/wasm/artifact.ts @@ -1,7 +1,10 @@ import { createHash } from "node:crypto"; import { createAnalysisExecution } from "../../../src/application/AnalysisProvider.js"; +import type { WasmArtifact } from "../../../src/domain/wasm/wasmArtifact.js"; /** Source-owned transport seam; upstream binaries are verified in a separate lane. */ -export const wasmArtifactFixture = (path = "/artifacts/module.wasm") => ({ +export const wasmArtifactFixture = ( + path = "/artifacts/module.wasm", +): WasmArtifact => ({ artifact: { path, sha256: "a".repeat(64), bytes: 8 }, validation: "valid" as const, sections: [], @@ -18,16 +21,18 @@ export const wasmArtifactFixture = (path = "/artifacts/module.wasm") => ({ source: "https://github.com/WebAssembly/wabt" as const, release: "1.0.42" as const, source_commit: "ff0ef7e0009402740c805a9744c09b05be063e48" as const, - commands: ["wasm-validate", "wasm-objdump", "wasm2wat"].map((tool) => ({ - tool, - path: `/tools/${tool}`, - sha256: "b".repeat(64), - version_banner: "1.0.42\n", - arguments: - tool === "wasm-objdump" - ? ["-h", "-x", "module.wasm"] - : ["--enable-all", "module.wasm"], - })), + commands: (["wasm-validate", "wasm-objdump", "wasm2wat"] as const).map( + (tool) => ({ + tool, + path: `/tools/${tool}`, + sha256: "b".repeat(64), + version_banner: "1.0.42\n", + arguments: + tool === "wasm-objdump" + ? ["-h", "-x", "module.wasm"] + : ["--enable-all", "module.wasm"], + }), + ), timeout_ms: 30_000, output_budget_bytes: 32 * 1024 * 1024, }, From df2c08b546d4f40103e11b8b31dbfc3d41ee687b Mon Sep 17 00:00:00 2001 From: Kaoru0822 <208281322+kaoru0822-kitauji@users.noreply.github.com> Date: Mon, 12 Oct 2026 06:49:15 -0700 Subject: [PATCH 04/25] fix(jeb): confine an invalid REA_JEB_MCP_URL to the JEB tools (#2145) JebProvider throws when REA_JEB_MCP_URL fails endpoint validation, and the composition built it eagerly for the CLI and every MCP connection. A malformed value, or http://localhost:8425/mcp, therefore crashed every CLI command with a stack trace and failed every MCP connection with an internal server error. The composition now parses the endpoint first and, when it is unusable, returns a JEB port that reports not_configured availability and a ConfigurationError naming REA_JEB_MCP_URL. Fixes #2143 Co-authored-by: Claude Opus 5.5 --- src/composition/jeb.ts | 41 +++++++++++++++++++++++++++--- tests/boundary/cli/jebCli.test.ts | 11 ++++++++ tests/boundary/mcp/jebMcp.test.ts | 42 +++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+), 3 deletions(-) diff --git a/src/composition/jeb.ts b/src/composition/jeb.ts index 4abc33b9d..38f5dae5d 100644 --- a/src/composition/jeb.ts +++ b/src/composition/jeb.ts @@ -1,13 +1,48 @@ import type { JebAnalysisPort } from "../application/jeb/JebAnalysisPort.js"; +import { ConfigurationError } from "../domain/configurationErrors.js"; +import { err } from "../domain/result.js"; import type { JebMcpConnectionFactory } from "../jeb/JebMcpConnection.js"; +import { parseJebMcpEndpoint } from "../jeb/JebMcpEndpoint.js"; import { JebProvider } from "../jeb/JebProvider.js"; -/** Construct one JEB provider bound to the caller-selected MCP endpoint. */ +/** + * Construct one JEB provider bound to the caller-selected MCP endpoint. An + * unusable endpoint makes only JEB unavailable; every other CLI command and + * MCP tool is composed alongside it. + */ export const createJebAnalysisProvider = ( environment: Readonly> = process.env, connectionFactory?: JebMcpConnectionFactory, -): JebAnalysisPort => - new JebProvider({ +): JebAnalysisPort => { + const endpoint = parseJebMcpEndpoint(environment.REA_JEB_MCP_URL); + if (!endpoint.ok) { + const constraint = + typeof endpoint.error.cause === "string" + ? endpoint.error.cause + : endpoint.error.message; + return unconfiguredJebProvider( + new ConfigurationError("Invalid REA environment configuration", { + cause: endpoint.error, + settings: [{ setting: "REA_JEB_MCP_URL", constraint }], + }), + ); + } + return new JebProvider({ environment, ...(connectionFactory === undefined ? {} : { connectionFactory }), }); +}; + +const unconfiguredJebProvider = ( + error: ConfigurationError, +): JebAnalysisPort => ({ + inspectAvailability: () => + Promise.resolve({ + status: "unavailable", + code: "not_configured", + reason: `REA_JEB_MCP_URL is invalid: ${error.settings[0]?.constraint ?? error.message}`, + diagnostics: { setting: "REA_JEB_MCP_URL" }, + }), + close: () => Promise.resolve(), + execute: () => Promise.resolve(err(error)), +}); diff --git a/tests/boundary/cli/jebCli.test.ts b/tests/boundary/cli/jebCli.test.ts index c188b0e31..6e2325c7f 100644 --- a/tests/boundary/cli/jebCli.test.ts +++ b/tests/boundary/cli/jebCli.test.ts @@ -30,3 +30,14 @@ it("validates decompile item input before contacting the engine", async () => { ], }); }); + +it("reports an invalid endpoint without breaking other commands", async () => { + const environment = { REA_JEB_MCP_URL: "http://localhost:8425/mcp" }; + const jeb = await serveCli(["inspect-jeb-client", "--json"], environment); + expect(JSON.parse(jeb.stdout)).toMatchObject({ + code: "configuration_invalid", + details: { settings: [{ setting: "REA_JEB_MCP_URL" }] }, + }); + const capabilities = await serveCli(["capabilities", "--json"], environment); + expect(capabilities.exitCode).toBe(0); +}); diff --git a/tests/boundary/mcp/jebMcp.test.ts b/tests/boundary/mcp/jebMcp.test.ts index 7ccc0b579..44b482714 100644 --- a/tests/boundary/mcp/jebMcp.test.ts +++ b/tests/boundary/mcp/jebMcp.test.ts @@ -141,3 +141,45 @@ it("preserves JEB provider failure reasons through the MCP boundary", async () = expect(text).toContain("ECONNREFUSED"); expect(text).toContain("REA_JEB_MCP_URL"); }); + +it("keeps the MCP server usable when REA_JEB_MCP_URL is invalid", async () => { + const session = createTestBinarySession(() => { + throw new Error("deep provider must not start"); + }); + const server = createServer( + { kind: "session", session }, + { environment: { REA_JEB_MCP_URL: "http://localhost:8425/mcp" } }, + ); + const client = new Client({ name: "jeb-invalid-endpoint", version: "1" }); + onTestFinished(async () => { + await client.close(); + await server.close(); + await session.close(); + }); + const [clientTransport, serverTransport] = + InMemoryTransport.createLinkedPair(); + await server.connect(serverTransport); + await client.connect(clientTransport); + expect((await client.listTools()).tools.length).toBeGreaterThan(0); + const failed = await client.callTool({ + name: "inspect_jeb_client", + arguments: {}, + }); + expect(failed.isError).toBe(true); + const text = (failed.content ?? []) + .map((block) => (block.type === "text" ? block.text : "")) + .join(""); + expect(JSON.parse(text)).toMatchObject({ + error: { + code: "configuration_invalid", + details: { + settings: [ + { + setting: "REA_JEB_MCP_URL", + constraint: expect.stringContaining("loopback"), + }, + ], + }, + }, + }); +}); From 90e37f4152990be721784006a820a3794920213c Mon Sep 17 00:00:00 2001 From: Kaoru0822 <208281322+kaoru0822-kitauji@users.noreply.github.com> Date: Mon, 12 Oct 2026 06:53:26 -0700 Subject: [PATCH 05/25] fix(managed): report malformed PE layouts as invalid input (#2194) * fix(managed): report malformed PE layouts as invalid input readManagedPeLayout raises ManagedReaderFailure for DOS, COFF and section header failures such as section data past EOF. Those escaped the managed inspectors, so ManagedStaticProvider wrapped them as ProviderAdapterError and the CLI and MCP reported execution_failure with advice to run rea doctor. Map format failures to an invalid_format input issue carrying the reader's detail, as inspect-pe-resources and inspect-go-binary do. Fixes #2191 Co-Authored-By: Claude Opus 5.5 * fix(managed): route unmappable PE layouts through one input error inspect-managed-native-boundaries caught a layout-level ManagedReaderFailure itself and returned an empty malformed inspection with exit 0, while the other managed operations now report the same bytes as invalid input. Let the layout failure reach the provider's invalid_format mapping; metadata failures after a readable layout still return malformed coverage. Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Claude Opus 5.5 --- src/dotnet/ManagedNativeBoundaryInspector.ts | 11 ++----- src/dotnet/ManagedStaticProvider.ts | 16 ++++++++++ tests/acceptance/managedCliOutput.test.ts | 33 ++++++++++++++++++++ 3 files changed, 51 insertions(+), 9 deletions(-) diff --git a/src/dotnet/ManagedNativeBoundaryInspector.ts b/src/dotnet/ManagedNativeBoundaryInspector.ts index 773a95c07..d2b84be04 100644 --- a/src/dotnet/ManagedNativeBoundaryInspector.ts +++ b/src/dotnet/ManagedNativeBoundaryInspector.ts @@ -121,15 +121,8 @@ export const inspectManagedNativeBoundariesBytes = ( bytes: Buffer, target: BinaryTarget, ): ManagedNativeBoundaryInspection => { - let pe: ManagedPeLayout; - try { - pe = readManagedPeLayout(bytes); - } catch (cause: unknown) { - if (!(cause instanceof ManagedReaderFailure)) throw cause; - return emptyInspection(target, bytes, "malformed", { - issues: [cause.issue], - }); - } + // An unmappable PE layout is invalid input for every managed operation. + const pe = readManagedPeLayout(bytes); if (pe.cli === null) return emptyInspection( target, diff --git a/src/dotnet/ManagedStaticProvider.ts b/src/dotnet/ManagedStaticProvider.ts index ba09a1939..d456455a2 100644 --- a/src/dotnet/ManagedStaticProvider.ts +++ b/src/dotnet/ManagedStaticProvider.ts @@ -22,6 +22,7 @@ import type { BinaryTarget } from "../domain/binaryTargetTypes.js"; import { AnalysisCancelledError, AnalysisCapabilityUnavailableError, + AnalysisInputError, } from "../domain/analysisErrorCore.js"; import { EvidenceIntegrityError } from "../domain/evidenceErrors.js"; import { ProviderAdapterError } from "../domain/providerAdapterError.js"; @@ -38,6 +39,7 @@ import { inspectManagedArtifactBytes } from "./ManagedArtifactInspector.js"; import { inspectManagedMembersBytes } from "./ManagedMemberInspector.js"; import { inspectManagedNativeBoundariesBytes } from "./ManagedNativeBoundaryInspector.js"; import { RegularFileCleanupFailure } from "../application/RegularFileRead.js"; +import { ManagedReaderFailure } from "./ManagedReaderFailure.js"; import { hashManagedSource, managedCleanupError, @@ -135,6 +137,20 @@ class ManagedStaticClient implements AnalysisClient { if (cause instanceof AnalysisError) return err(cause); if (options?.signal?.aborted === true) return err(new AnalysisCancelledError(operation)); + // A PE header or section table the reader cannot map is malformed input. + if ( + cause instanceof ManagedReaderFailure && + cause.issue.code !== "resource-limit" + ) + return err( + new AnalysisInputError(operation, { cause }, [ + { + path: ["path"], + reason: "invalid_format", + message: `Selected artifact ${this.target.path}: ${cause.issue.detail}`, + }, + ]), + ); return err( new ProviderAdapterError(MANAGED_STATIC_PROVIDER.id, operation, { cause, diff --git a/tests/acceptance/managedCliOutput.test.ts b/tests/acceptance/managedCliOutput.test.ts index a24020d60..26ef7b85d 100644 --- a/tests/acceptance/managedCliOutput.test.ts +++ b/tests/acceptance/managedCliOutput.test.ts @@ -307,3 +307,36 @@ cliTest( } }, ); + +cliTest( + "managed CLI reports a section table past EOF as invalid input", + async ({ cli }) => { + const root = await createTestTempDirectory("rea-managed-cli-sections-"); + const path = join(root, "fixture.dll"); + const bytes = buildManagedPeFixture(); + const pe = bytes.readUInt32LE(0x3c); + const sections = pe + 24 + bytes.readUInt16LE(pe + 20); + bytes.writeUInt32LE(bytes.length + 0x1000, sections + 20); + await writeFile(path, bytes); + for (const command of [ + "inspect-managed-artifact", + "inspect-managed-members", + "inspect-managed-native-boundaries", + ]) { + const result = await cli.run({ arguments: [command, path, "--json"] }); + expect(result.exitCode).toBe(1); + expect(result.json).toMatchObject({ + code: "invalid_request", + details: { + issues: [ + { + path: ["path"], + reason: "invalid_format", + message: expect.stringContaining("pe.section-data"), + }, + ], + }, + }); + } + }, +); From 51367cae6f4e6ef9b9dcb28dc21fe6c92d1dd6f8 Mon Sep 17 00:00:00 2001 From: Kaoru0822 <208281322+kaoru0822-kitauji@users.noreply.github.com> Date: Mon, 12 Oct 2026 06:53:55 -0700 Subject: [PATCH 06/25] fix(setup): name structural problems in client configurations (#2154) A client configuration that parses but whose root or MCP server table is not an object (for example {"mcpServers": null} or []) made setup print a raw Zod issue dump and made uninstall claim the file was not valid JSON. parseClientConfiguration now raises a ClientConfigurationShapeError naming the root or the servers key, which setup reports as the malformed detail and uninstall reports instead of a syntax error. Fixes #2152 Co-authored-by: Claude Opus 5.5 --- .../ClientConfigurationDocument.ts | 31 +++++++++++++++++-- src/application/Uninstall.ts | 6 ++-- .../filesystem/clientLifecycle.test.ts | 29 +++++++++++++++++ 3 files changed, 61 insertions(+), 5 deletions(-) diff --git a/src/application/ClientConfigurationDocument.ts b/src/application/ClientConfigurationDocument.ts index 65b4ba8de..27b5393d2 100644 --- a/src/application/ClientConfigurationDocument.ts +++ b/src/application/ClientConfigurationDocument.ts @@ -128,6 +128,20 @@ const parseDocument = ( return objectSchema.parse(document); }; +/** A parsed configuration whose structure, not its syntax, cannot hold MCP servers. */ +export class ClientConfigurationShapeError extends TypeError {} + +const requireObject = ( + value: unknown, + description: string, +): Record => { + const parsed = objectSchema.safeParse(value); + if (parsed.success) return parsed.data; + throw new ClientConfigurationShapeError(`${description} must be an object`, { + cause: parsed.error, + }); +}; + /** Members of OpenCode V2's `mcp` object that are not server names. */ const OPENCODE_V2_MCP_SETTINGS = new Set(["servers", "timeout"]); @@ -140,10 +154,21 @@ export const parseClientConfiguration = ( throw new TypeError("client does not have a supported MCP config format"); // DeepSeek Harness stores a Cordis patch list, not a server table. if (format === "dsh") return parseDshCordisDocument(text); - const document = parseDocument(text, format); + let document: Record; + try { + document = parseDocument(text, format); + } catch (cause: unknown) { + if (cause instanceof z.ZodError) + throw new ClientConfigurationShapeError( + "The configuration root must be an object", + { cause }, + ); + throw cause; + } const serversKey = clientConfigurationServersKey(format); const value = document[serversKey]; - const servers = value === undefined ? {} : objectSchema.parse(value); + const servers = + value === undefined ? {} : requireObject(value, `"${serversKey}"`); const native = servers.servers; // A V1 server named `servers` has a string `type` discriminator; the V2 // table may instead hold a server object named `type`. @@ -164,7 +189,7 @@ export const parseClientConfiguration = ( }; return { document, - servers: objectSchema.parse(native), + servers: requireObject(native, `"${serversKey}.servers"`), serversPath: [serversKey, "servers"], dialect: "opencode_v2", legacyServers: Object.fromEntries( diff --git a/src/application/Uninstall.ts b/src/application/Uninstall.ts index 6822460bb..24ff88ea6 100644 --- a/src/application/Uninstall.ts +++ b/src/application/Uninstall.ts @@ -1,4 +1,5 @@ import { + ClientConfigurationShapeError, parseClientConfiguration, serializeClientConfiguration, clientServerPath, @@ -279,12 +280,13 @@ const readClientConfiguration = async ( const parsed = parseClientConfiguration(original, client.format); return { kind: "configuration", transactionPath, original, parsed }; } catch (cause: unknown) { - void cause; return itemRead( item( client.name, "failed", - `Configuration is not valid ${client.format === "toml" || client.format === "grok" ? "TOML" : client.format === "opencode" ? "JSONC" : "JSON"} and was not changed. Repair it, then rerun uninstall.`, + cause instanceof ClientConfigurationShapeError + ? `Configuration was not changed: ${cause.message}. Repair it, then rerun uninstall.` + : `Configuration is not valid ${client.format === "toml" || client.format === "grok" ? "TOML" : client.format === "opencode" ? "JSONC" : "JSON"} and was not changed. Repair it, then rerun uninstall.`, ), ); } diff --git a/tests/boundary/filesystem/clientLifecycle.test.ts b/tests/boundary/filesystem/clientLifecycle.test.ts index db0b517e7..6ee7543e9 100644 --- a/tests/boundary/filesystem/clientLifecycle.test.ts +++ b/tests/boundary/filesystem/clientLifecycle.test.ts @@ -21,6 +21,7 @@ import { PRODUCT_IDENTITY } from "../../../src/identity.js"; import { readClientRegistrationStatuses } from "../../../src/application/ClientRegistrationStatus.js"; import { configureClientConfiguration } from "../../../src/application/SetupClientConfiguration.js"; +import { readConfigurationFile } from "../../../src/application/ClientConfigurationFiles.js"; import { detectClients, systemSetupHost, @@ -1593,6 +1594,34 @@ describe("uninstall configuration preflight", () => { expect(await readFile(config, "utf8")).toBe("not-json"); }); + it.each([ + ['{"mcpServers": null}', '"mcpServers" must be an object'], + ["[]", "The configuration root must be an object"], + ])( + "names the structural problem in valid JSON %s", + async (content, problem) => { + const home = await createTestTempDirectory("rea-uninstall-shape-"); + roots.push(home); + const config = join(home, ".cursor/mcp.json"); + await mkdir(dirname(config), { recursive: true }); + await writeFile(config, content); + expect(await readConfigurationFile(config, "json")).toMatchObject({ + ok: false, + error: { + kind: "malformed", + detail: `Configuration ${config} is malformed: ${problem}`, + }, + }); + const result = await runUninstall(false, systemUninstallHost(home)); + expect(result.items).toContainEqual({ + name: "cursor", + status: "failed", + detail: `Configuration was not changed: ${problem}. Repair it, then rerun uninstall.`, + }); + expect(await readFile(config, "utf8")).toBe(content); + }, + ); + it("stops before any removal when one client configuration is malformed", async () => { const home = await createTestTempDirectory("rea-uninstall-stop-"); roots.push(home); From d35035780a643e38301b8eaea03f01bb506e9df9 Mon Sep 17 00:00:00 2001 From: Kaoru0822 <208281322+kaoru0822-kitauji@users.noreply.github.com> Date: Mon, 12 Oct 2026 06:54:38 -0700 Subject: [PATCH 07/25] fix(setup): remove emptied skill directories on uninstall (#2182) * fix(setup): remove emptied skill directories on uninstall Uninstall removes only the managed skill files so user files survive, but it left every emptied reverse-engineer-anything directory and its references/ subdirectory behind. After the ownership manifest is removed, remove the managed directories deepest first with a non-recursive rmdir, which keeps any directory that still holds user files. Fixes #2159 Co-Authored-By: Claude Opus 5.5 * fix(setup): report emptied skill directories that cannot be removed Only a directory that still holds user files, is already absent, or is no longer a directory is kept silently. Any other rmdir failure, such as a parent that denies deletion, now fails the skill item with the directory path instead of reporting the skill as removed. Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Claude Opus 5.5 --- src/application/Uninstall.ts | 61 ++++++++++++++++-- .../filesystem/clientLifecycle.test.ts | 63 +++++++++++++++++++ 2 files changed, 120 insertions(+), 4 deletions(-) diff --git a/src/application/Uninstall.ts b/src/application/Uninstall.ts index 24ff88ea6..570b5589e 100644 --- a/src/application/Uninstall.ts +++ b/src/application/Uninstall.ts @@ -7,7 +7,7 @@ import { withClientServers, type ClientConfigurationDocument, } from "./ClientConfigurationDocument.js"; -import { copyFile, lstat, realpath, rm } from "node:fs/promises"; +import { copyFile, lstat, realpath, rm, rmdir } from "node:fs/promises"; import { constants as fsConstants } from "node:fs"; import { homeDirectoryFromEnvironment } from "../config/homeDirectory.js"; import { join } from "node:path"; @@ -51,6 +51,8 @@ export interface UninstallFileSystem { stat(path: string): Promise; realpath?(path: string): Promise; remove(path: string, recursive?: boolean): Promise; + /** Remove a directory only if it is empty; a non-empty one is kept. */ + removeEmptyDirectory?(path: string): Promise; } const systemFileSystem = ( @@ -65,6 +67,7 @@ const systemFileSystem = ( stat: (path) => lstat(path), realpath: (path) => realpath(path), remove: (path, recursive = true) => rm(path, { recursive }), + removeEmptyDirectory: (path) => rmdir(path), }); /** One explicitly classified uninstall action. */ @@ -602,10 +605,20 @@ const removeManagedSkill = async ( "REA ownership manifest was retained because a managed file could not be safely removed.", ), ); - else - results.push( - await removeManagedSkillFile(path, manifestName, name, fileSystem), + else { + const manifest = await removeManagedSkillFile( + path, + manifestName, + name, + fileSystem, ); + results.push(manifest); + const directories = + manifest.status === "removed" + ? await removeEmptyManagedDirectories(path, name, fileSystem) + : undefined; + if (directories !== undefined) results.push(directories); + } const failed = results.find(({ status }) => status === "failed"); if (failed !== undefined) return item(name, "failed", results.map(({ detail }) => detail).join(" ")); @@ -617,6 +630,46 @@ const removeManagedSkill = async ( ); }; +/** Directories setup creates for managed files, deepest first, then the root. */ +const MANAGED_SKILL_DIRECTORIES = [ + ...new Set( + MANAGED_SKILL_FILES.map((relativePath) => + relativePath.split("/").slice(0, -1).join("/"), + ).filter((directory) => directory.length > 0), + ), +].sort((left, right) => right.split("/").length - left.split("/").length); + +/** Remove managed directories left empty; any remaining user file keeps them. */ +const removeEmptyManagedDirectories = async ( + root: string, + name: string, + fileSystem: UninstallFileSystem, +): Promise => { + if (fileSystem.removeEmptyDirectory === undefined) return undefined; + for (const directory of [ + ...MANAGED_SKILL_DIRECTORIES.map((relative) => join(root, relative)), + root, + ]) { + try { + await fileSystem.removeEmptyDirectory(directory); + } catch (cause: unknown) { + // User files, an absent directory, or a non-directory keep it as is. + if (!isKeptDirectory(cause)) + return item( + name, + "failed", + `Managed skill directory could not be removed: ${directory}.`, + ); + } + } + return undefined; +}; + +const isKeptDirectory = (cause: unknown): boolean => + cause instanceof Error && + "code" in cause && + ["ENOTEMPTY", "EEXIST", "ENOENT", "ENOTDIR"].includes(String(cause.code)); + const removeManagedSkillFile = async ( root: string, relativePath: string, diff --git a/tests/boundary/filesystem/clientLifecycle.test.ts b/tests/boundary/filesystem/clientLifecycle.test.ts index 6ee7543e9..89c4dc95a 100644 --- a/tests/boundary/filesystem/clientLifecycle.test.ts +++ b/tests/boundary/filesystem/clientLifecycle.test.ts @@ -1,5 +1,6 @@ import { constants as fsConstants } from "node:fs"; import { + chmod, copyFile, lstat, mkdir, @@ -1527,6 +1528,68 @@ describe("client configuration filesystem removal", () => { ); }); + it("removes emptied skill directories and keeps ones with user files", async () => { + const home = await createTestTempDirectory("rea-uninstall-skill-dirs-"); + roots.push(home); + const shared = join(home, ".agents/skills/reverse-engineer-anything"); + const claude = join(home, ".claude/skills/reverse-engineer-anything"); + for (const root of [shared, claude]) { + await mkdir(join(root, "references"), { recursive: true }); + await writeFile(join(root, "SKILL.md"), ownedSkillManifest); + await writeFile( + join(root, "references/evidence-workflows.md"), + "managed", + ); + } + await writeFile(join(claude, "notes.md"), "user"); + + const result = await runUninstall( + false, + systemUninstallHost(home, undefined, { + HOME: home, + CLAUDE_CONFIG_DIR: join(home, ".claude"), + }), + ); + + expect(result.status).toBe("complete"); + await expect(lstat(shared)).rejects.toMatchObject({ code: "ENOENT" }); + await expect(lstat(join(claude, "references"))).rejects.toMatchObject({ + code: "ENOENT", + }); + expect(await readFile(join(claude, "notes.md"), "utf8")).toBe("user"); + }); + + it.skipIf(process.platform === "win32" || process.getuid?.() === 0)( + "reports an emptied skill directory that cannot be removed", + async () => { + const home = await createTestTempDirectory("rea-uninstall-skill-perm-"); + roots.push(home); + const parent = join(home, ".agents/skills"); + const root = join(parent, "reverse-engineer-anything"); + await mkdir(join(root, "references"), { recursive: true }); + await writeFile(join(root, "SKILL.md"), ownedSkillManifest); + await chmod(parent, 0o555); + try { + const result = await runUninstall( + false, + systemUninstallHost(home, undefined, { HOME: home }), + ); + expect(result.status).toBe("failed"); + expect(result.items).toContainEqual( + expect.objectContaining({ + name: "skill", + status: "failed", + detail: expect.stringContaining( + `Managed skill directory could not be removed: ${root}.`, + ), + }), + ); + } finally { + await chmod(parent, 0o755); + } + }, + ); + it("removes an owned entry through a symlink without replacing it", async () => { const home = await createTestTempDirectory("rea-uninstall-symlink-"); roots.push(home); From 7c9c1ebdb77662f6276ef2f7250e7df414bd03ca Mon Sep 17 00:00:00 2001 From: Kaoru0822 <208281322+kaoru0822-kitauji@users.noreply.github.com> Date: Mon, 12 Oct 2026 06:56:45 -0700 Subject: [PATCH 08/25] fix(mcp): keep shared optional providers open across discarded probes (#2205) startMcpTransport loads optional observation providers once per process and passes them to every server instance the SDK constructs. Each instance's closeProviders also closed the shared browser scenario provider, so when the SDK discarded a server/discover probe instance for a legacy initialize, every later capture_browser_scenario call failed with "Browser scenario provider is closing." Server instances no longer close the caller-supplied provider; the transport returns closeOptionalProviders and process shutdown closes it once. Fixes #2195 Co-authored-by: Claude Opus 5.5 --- src/main.ts | 1 + src/main/shutdown.ts | 4 ++ src/main/transport.ts | 8 ++- src/server/createServer.ts | 1 - .../browserScenarioProbeLifecycle.test.ts | 61 +++++++++++++++++++ 5 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 tests/boundary/process/browserScenarioProbeLifecycle.test.ts diff --git a/src/main.ts b/src/main.ts index 40c241b47..3d32798b7 100644 --- a/src/main.ts +++ b/src/main.ts @@ -82,6 +82,7 @@ export const run = async ( createShutdown({ handle: transport.handle, closeAndroid: transport.closeAndroid, + closeOptionalProviders: transport.closeOptionalProviders, session, dependencies, serverLogger, diff --git a/src/main/shutdown.ts b/src/main/shutdown.ts index f6722eb97..90d30c575 100644 --- a/src/main/shutdown.ts +++ b/src/main/shutdown.ts @@ -16,6 +16,7 @@ interface ShutdownCleanupOwner { export const createShutdown = (input: { readonly handle: StdioServerHandle; readonly closeAndroid?: () => Promise; + readonly closeOptionalProviders?: () => Promise; readonly session: BinarySession; readonly dependencies: RuntimeDependencies; readonly serverLogger: Logger; @@ -30,6 +31,9 @@ export const createShutdown = (input: { ...(input.closeAndroid === undefined ? [] : [{ close: input.closeAndroid, closed: false }]), + ...(input.closeOptionalProviders === undefined + ? [] + : [{ close: input.closeOptionalProviders, closed: false }]), { close: async () => { const closed = await session.close({ retainProviderDocuments: true }); diff --git a/src/main/transport.ts b/src/main/transport.ts index f8d6014f3..f9a5cd0ea 100644 --- a/src/main/transport.ts +++ b/src/main/transport.ts @@ -31,6 +31,7 @@ export const startMcpTransport = async ( readonly ok: true; readonly handle: StdioServerHandle; readonly closeAndroid: () => Promise; + readonly closeOptionalProviders: () => Promise; } | { readonly ok: false } > => { @@ -62,6 +63,11 @@ export const startMcpTransport = async ( for (const result of results) if (result.status === "rejected") throw result.reason; }; + // Optional providers are shared by every server instance the SDK creates, + // including a discarded discovery probe, so only process shutdown closes them. + const closeOptionalProviders = async (): Promise => { + await optionalProviders.browserScenarioCapture?.close?.(); + }; let handle: StdioServerHandle; try { handle = dependencies.serve( @@ -124,5 +130,5 @@ export const startMcpTransport = async ( } return { ok: false }; } - return { ok: true, handle, closeAndroid }; + return { ok: true, handle, closeAndroid, closeOptionalProviders }; }; diff --git a/src/server/createServer.ts b/src/server/createServer.ts index 0e5c69cba..3220fe8e7 100644 --- a/src/server/createServer.ts +++ b/src/server/createServer.ts @@ -270,7 +270,6 @@ export const createServer = ( javascriptRecovery.close?.() ?? Promise.resolve(), artifactResources.close(), firmwareAnalysis.close(), - options.browserScenarioCapture?.close?.() ?? Promise.resolve(), ]) .then((results) => { const failures = results.flatMap((result) => diff --git a/tests/boundary/process/browserScenarioProbeLifecycle.test.ts b/tests/boundary/process/browserScenarioProbeLifecycle.test.ts new file mode 100644 index 000000000..42871d13a --- /dev/null +++ b/tests/boundary/process/browserScenarioProbeLifecycle.test.ts @@ -0,0 +1,61 @@ +import { STDIO_DEFAULT_MAX_BUFFER_SIZE } from "@modelcontextprotocol/server"; +import { expect, it, vi } from "vitest"; + +import type { BrowserScenarioCapturePort } from "../../../src/application/BrowserScenarioCapturePort.js"; +import { silentLogger } from "../../../src/logger.js"; +import { startMcpTransport } from "../../../src/main/transport.js"; +import type { RuntimeDependencies } from "../../../src/main/types.js"; +import { ToolResultDelivery } from "../../../src/server/toolResult.js"; +import { createTestBinarySession } from "../../fixtures/binarySession.js"; + +it("keeps the shared browser scenario provider open when the SDK discards its discovery probe", async () => { + const session = createTestBinarySession(() => { + throw new Error("Unexpected deep provider"); + }); + const close = vi.fn(async () => undefined); + const browserScenarioCapture: BrowserScenarioCapturePort = { + identity: () => ({ + id: "fake-browser", + name: "Fake browser", + version: "1", + }), + captureScenario: async () => { + throw new Error("Unexpected scenario capture"); + }, + close, + }; + const factories: Parameters[0][] = []; + const runtime: RuntimeDependencies = { + env: {}, + serve: (factory) => { + factories.push(factory); + return { close: async () => undefined }; + }, + writeStderr: () => undefined, + setExitCode: () => undefined, + registerShutdown: () => () => undefined, + }; + const transport = await startMcpTransport(runtime, session, { + logger: silentLogger, + serverLogger: silentLogger, + environment: runtime.env, + delivery: new ToolResultDelivery(STDIO_DEFAULT_MAX_BUFFER_SIZE), + loadOptionalProviders: async () => ({ browserScenarioCapture }), + }); + if (!transport.ok) throw new Error("Transport failed"); + const factory = factories[0]; + if (factory === undefined) throw new Error("Missing SDK server factory"); + try { + const probe = await factory({ era: "modern" }); + await probe.close(); + const replacement = await factory({ era: "legacy" }); + await replacement.close(); + expect(close).not.toHaveBeenCalled(); + + await transport.closeOptionalProviders(); + expect(close).toHaveBeenCalledTimes(1); + } finally { + await transport.closeAndroid(); + await session.close(); + } +}); From 0b7145361ec02d27260eb1f5bc0be955c9374a96 Mon Sep 17 00:00:00 2001 From: Ysopwnable <161784452+N0zoM1z0@users.noreply.github.com> Date: Mon, 12 Oct 2026 22:01:47 +0800 Subject: [PATCH 09/25] feat(wasm): add opt-in first-call summary delivery (#2193) Add explicit first-call WASM summary delivery while preserving omitted/complete MCP requests, CLI output, original Evidence identity and provider inputs. Reuse exact retained export views and schema-based capability discovery. Nine paired public SDK comparisons and ten actual Codex CLI answers preserve selected facts and source references. Record token reductions alongside mixed latency and unverified final resource-counter conditions. Current-main integration passes seven relevant WASM MCP workflows, a fresh real-WABT SQLite workflow and required static gates. Refs #2016. Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com> --- .../references/native-and-artifacts.md | 4 + docs/mcp-contracts.md | 8 +- experiments/analysis-efficiency/README.md | 9 +- experiments/analysis-efficiency/corpus.json | 34 ++++ experiments/analysis-efficiency/measure.mjs | 183 ++++++++++-------- .../questions/wasm-interface.txt | 1 + .../analysis-efficiency/wasm-first-call.md | 149 ++++++++++++++ src/contracts/wasm/wasmToolContracts.ts | 21 +- src/domain/analysisView/analysisView.ts | 7 + src/server/createServer.ts | 1 + src/server/registerWasmTools.ts | 26 ++- tests/boundary/mcp/wasmArtifactMcp.test.ts | 144 +++++++++++++- 12 files changed, 499 insertions(+), 88 deletions(-) create mode 100644 experiments/analysis-efficiency/questions/wasm-interface.txt create mode 100644 experiments/analysis-efficiency/wasm-first-call.md diff --git a/.agents/skills/reverse-engineer-anything/references/native-and-artifacts.md b/.agents/skills/reverse-engineer-anything/references/native-and-artifacts.md index 1b02a7ed2..dfb701760 100644 --- a/.agents/skills/reverse-engineer-anything/references/native-and-artifacts.md +++ b/.agents/skills/reverse-engineer-anything/references/native-and-artifacts.md @@ -39,6 +39,10 @@ read a single object. ## Offline WASM Use `inspect_wasm_artifact` with caller-supplied WABT for selected local bytes. +For counts, validation and WAT identity, select `detail: "summary"` on the first +MCP call. The complete original Evidence stays retained; use +`normalized_result.parent_evidence_id` for exact exports or text pages. Omitted +or complete detail keeps the existing complete result. For a completed result, use `inspect_analysis_view` with its reported retained reference for a summary or `view: {kind: "wasm", facet: "exports", offset: 0, limit: 32}`. Facets also include sections, imports, candidates, glue, WAT, headers, details diff --git a/docs/mcp-contracts.md b/docs/mcp-contracts.md index 8e5755caf..1fb3b5e5d 100644 --- a/docs/mcp-contracts.md +++ b/docs/mcp-contracts.md @@ -412,7 +412,8 @@ Ordinary responses keep their existing complete result contract. ## Retained application Evidence inputs -`analyze_javascript_application`, `inspect_binary_layout`, and `inspect_artifact` accept `detail`: +`analyze_javascript_application`, `inspect_binary_layout`, `inspect_artifact`, and +`inspect_wasm_artifact` accept `detail`: `complete` (default) returns the complete analysis Evidence; `summary` records that complete Evidence in the session and returns the derived `inspect_analysis_view` summary Evidence instead. Its @@ -476,6 +477,11 @@ The original producer still returns complete Evidence by default. When its complete MCP reply exceeds the frame, use the exact reported retained reference with these views, or export the complete source. Views do not run WABT again and do not turn decoded WAT into original source. +For counts, validation and WAT identity, select `detail: "summary"` on the first +`inspect_wasm_artifact` call. It retains the original complete source without +attempting oversized delivery, and returns the same summary with an exact +`parent_evidence_id` for later export or facet reads. Delivery detail does not +reach WABT or change the original source Evidence ID. `trace_application_feature`, `trace_javascript_semantics`, `compare_application_versions`, `compare_source_to_bundle`, and diff --git a/experiments/analysis-efficiency/README.md b/experiments/analysis-efficiency/README.md index 8c1708009..9a09e9d36 100644 --- a/experiments/analysis-efficiency/README.md +++ b/experiments/analysis-efficiency/README.md @@ -137,8 +137,11 @@ inventory. It must be supported by the selected server's advertised schema. The default `complete` runs the existing calls unchanged. Compare the same analyst facts and complete source Evidence before crediting a smaller response. Projection can reduce delivery bytes without reducing upstream analysis time. -For SQLite WASM, summary delivery reads the completed retained result through -the summary and exports views after preserving the original transport error. +For WASM, the runner validates a summary request against the actual advertised +2020-12 input schema, including SDK-generated references. Supporting servers +return the first-call summary; older servers retain their complete path and use +summary/export views after an observed transport constraint. Original errors +remain in the receipts, and the complete source Evidence ID is a compared fact. `--javascript-question selected` analyzes only the exact Obsidian sources `package.json` and `main.js`, keeps inventory identity, and reads each observed `main.js` module by its exact node ID. Package metadata is not a module selector. @@ -538,3 +541,5 @@ observations, not verified final counters. Their bytes, facts and token reports remain original observations; resource-qualified performance conclusions need this additional closeout evidence. No analyzer/build/schema/package behavior is changed, so unrelated verification is not repeated for this research-driver fix. + +The next bounded WASM delivery cohort is documented in [first-call summary measurements](wasm-first-call.md). diff --git a/experiments/analysis-efficiency/corpus.json b/experiments/analysis-efficiency/corpus.json index c85180568..911fa88f5 100644 --- a/experiments/analysis-efficiency/corpus.json +++ b/experiments/analysis-efficiency/corpus.json @@ -115,6 +115,40 @@ "transport_bytes": 168796487, "derivation": "selected regular ZIP member ShareX.HelpersLib.dll; no target execution" } + }, + { + "id": "vscode.tree-sitter.wasm", + "file": "tree-sitter.wasm", + "bytes": 206218, + "sha256": "3a31af706ffdf4a7116b064cdd4988df6791823298615009fc5b6bccbd42909b", + "provenance": { + "project": "VS Code Tree-sitter published WASM package", + "version": "0.3.1", + "project_url": "https://github.com/microsoft/vscode-tree-sitter-wasm", + "upstreamFile": "tree-sitter-wasm-0.3.1.tgz", + "url": "https://registry.npmjs.org/@vscode/tree-sitter-wasm/-/tree-sitter-wasm-0.3.1.tgz", + "transport_sha256": "985684b2cfbe5e8f90499a9f36e37519887c7d4e2697d0457d0f1e923ce5184c", + "transport_bytes": 2053205, + "transport_integrity": "sha512-RJFoomET6FajjG511fmQxeBQfU6M24a0aFZPqpid+ttIxanWf1VGytBG0UmsGjt07qmIPJS8U31D+aecuCucsQ==", + "derivation": "selected regular tar member package/wasm/tree-sitter.wasm; no target execution" + } + }, + { + "id": "vscode.tree-sitter-css.wasm", + "file": "tree-sitter-css.wasm", + "bytes": 128668, + "sha256": "8a23977fe271357cce6f254ef88c9bebf3854602d8046605aef6a45c02135c59", + "provenance": { + "project": "VS Code Tree-sitter published WASM package", + "version": "0.3.1", + "project_url": "https://github.com/microsoft/vscode-tree-sitter-wasm", + "upstreamFile": "tree-sitter-wasm-0.3.1.tgz", + "url": "https://registry.npmjs.org/@vscode/tree-sitter-wasm/-/tree-sitter-wasm-0.3.1.tgz", + "transport_sha256": "985684b2cfbe5e8f90499a9f36e37519887c7d4e2697d0457d0f1e923ce5184c", + "transport_bytes": 2053205, + "transport_integrity": "sha512-RJFoomET6FajjG511fmQxeBQfU6M24a0aFZPqpid+ttIxanWf1VGytBG0UmsGjt07qmIPJS8U31D+aecuCucsQ==", + "derivation": "selected regular tar member package/wasm/tree-sitter-css.wasm; no target execution" + } } ] } diff --git a/experiments/analysis-efficiency/measure.mjs b/experiments/analysis-efficiency/measure.mjs index a10eb23d8..348bf02c2 100644 --- a/experiments/analysis-efficiency/measure.mjs +++ b/experiments/analysis-efficiency/measure.mjs @@ -13,6 +13,7 @@ import { fileURLToPath, pathToFileURL } from "node:url"; import { parseArgs } from "node:util"; import { Client } from "@modelcontextprotocol/client"; import { StdioClientTransport } from "@modelcontextprotocol/client/stdio"; +import { Ajv2020 } from "ajv/dist/2020.js"; const root = resolve(dirname(fileURLToPath(import.meta.url)), "../.."); const { values } = parseArgs({ @@ -243,6 +244,106 @@ const inspectManagedTarget = async () => { }; }; +/** The same counts/export question for each pinned shipped WASM module. */ +const inspectWasmTarget = async () => { + let info; + let recovery = null; + const arguments_ = { + path: values.path, + glue_paths: [], + candidate_paths: [], + }; + const definition = catalogDefinitions.get("inspect_wasm_artifact"); + if (!definition) throw new Error("Missing advertised WASM definition"); + // Validate the real advertised dialect, including SDK-generated local refs. + const supportsSummary = new Ajv2020({ + strict: false, + validateFormats: false, + }).compile(definition.inputSchema)({ ...arguments_, detail: "summary" }); + try { + info = await call("inspect_wasm_artifact", { + ...arguments_, + ...(values.delivery === "summary" && supportsSummary + ? { detail: "summary" } + : {}), + }); + } catch (error) { + const reference = + error instanceof ToolFailure && + error.value?.error?.code === "resource_constraint" + ? error.value.error.details?.reported_limits?.evidence_reference + : undefined; + if (reference?.kind !== "retained-evidence") throw error; + if (values.delivery === "summary") { + info = await call("inspect_analysis_view", { + source: reference, + view: { kind: "summary" }, + }); + recovery = + "existing completed producer reference read through typed WASM views"; + } else { + const path = resolve(values.output, "retained-evidence.json"); + await call("export_evidence_bundle", { path }); + const bundle = JSON.parse(await readFile(path, "utf8")); + const record = bundle.records.find( + (row) => row.evidence_id === reference.evidence_id, + ); + const { parseEvidence } = await import( + pathToFileURL( + resolve(values["runtime-root"], "dist/domain/evidence.js"), + ).href + ); + info = parseEvidence(record); + recovery = + "complete producer Evidence exported after an observed transport constraint"; + } + } + const n = normalized(info); + if (n.kind === "summary") { + const summary = n.summary; + const exportsEvidence = await call("inspect_analysis_view", { + source: { kind: "retained-evidence", evidence_id: n.parent_evidence_id }, + view: { kind: "wasm", facet: "exports", offset: 0, limit: 4 }, + }); + return { + artifact_sha256: targetSha(info), + validation: summary.validation, + section_count: summary.available_counts.sections, + import_count: summary.available_counts.imports, + export_count: summary.available_counts.exports, + exports_first4: normalized(exportsEvidence).item, + wat_sha256: summary.wat.sha256, + wat_code_units: summary.wat.code_units, + tool_profile: summary.tool_profile, + runtime_execution: summary.runtime_execution, + network_fetch: summary.network_fetch, + evidence_ids: [ + n.parent_evidence_id, + info.evidence_id, + exportsEvidence.evidence_id, + ], + source_evidence_id: n.parent_evidence_id, + recovery, + }; + } + return { + artifact_sha256: targetSha(info), + validation: n.validation, + section_count: n.sections.length, + import_count: n.imports.length, + export_count: n.exports.length, + exports_first4: n.exports.slice(0, 4), + wat_sha256: n.wat.sha256, + wat_code_units: n.wat.text.length, + tool_profile: n.tool_profile, + runtime_execution: n.runtime_execution, + network_fetch: n.network_fetch, + evidence_ids: [info.evidence_id], + source_evidence_id: info.evidence_id, + recovery, + }; +}; + const workflows = { "obsidian.asar": async () => { if (values["javascript-question"] === "selected") { @@ -424,85 +525,9 @@ const workflows = { evidence_ids: [info.evidence_id], }; }, - "sqlite.wasm": async () => { - let info; - let recovery = null; - try { - info = await call("inspect_wasm_artifact", { - path: values.path, - glue_paths: [], - candidate_paths: [], - }); - } catch (error) { - const reference = - error instanceof ToolFailure && - error.value?.error?.code === "resource_constraint" - ? error.value.error.details?.reported_limits?.evidence_reference - : undefined; - if (reference?.kind !== "retained-evidence") throw error; - if (values.delivery === "summary") { - const summaryEvidence = await call("inspect_analysis_view", { - source: reference, - view: { kind: "summary" }, - }); - const summary = normalized(summaryEvidence).summary; - const exportsEvidence = await call("inspect_analysis_view", { - source: reference, - view: { kind: "wasm", facet: "exports", offset: 0, limit: 4 }, - }); - return { - artifact_sha256: targetSha(summaryEvidence), - validation: summary.validation, - section_count: summary.available_counts.sections, - import_count: summary.available_counts.imports, - export_count: summary.available_counts.exports, - exports_first4: normalized(exportsEvidence).item, - wat_sha256: summary.wat.sha256, - wat_code_units: summary.wat.code_units, - tool_profile: summary.tool_profile, - runtime_execution: summary.runtime_execution, - network_fetch: summary.network_fetch, - evidence_ids: [ - reference.evidence_id, - summaryEvidence.evidence_id, - exportsEvidence.evidence_id, - ], - recovery: - "existing completed producer reference read through typed WASM views", - }; - } - const path = resolve(values.output, "retained-evidence.json"); - await call("export_evidence_bundle", { path }); - const bundle = JSON.parse(await readFile(path, "utf8")); - const record = bundle.records.find( - (row) => row.evidence_id === reference.evidence_id, - ); - const { parseEvidence } = await import( - pathToFileURL( - resolve(values["runtime-root"], "dist/domain/evidence.js"), - ).href - ); - info = parseEvidence(record); - recovery = - "complete producer Evidence exported after an observed transport constraint"; - } - const n = normalized(info); - return { - artifact_sha256: targetSha(info), - validation: n.validation, - section_count: n.sections.length, - import_count: n.imports.length, - export_count: n.exports.length, - exports_first4: n.exports.slice(0, 4), - wat_sha256: n.wat.sha256, - wat_code_units: n.wat.text.length, - tool_profile: n.tool_profile, - runtime_execution: n.runtime_execution, - network_fetch: n.network_fetch, - evidence_ids: [info.evidence_id], - recovery, - }; - }, + "sqlite.wasm": inspectWasmTarget, + "vscode.tree-sitter.wasm": inspectWasmTarget, + "vscode.tree-sitter-css.wasm": inspectWasmTarget, "weth.hex": async () => { const info = await call("inspect_evm_interface", { path: values.path, diff --git a/experiments/analysis-efficiency/questions/wasm-interface.txt b/experiments/analysis-efficiency/questions/wasm-interface.txt new file mode 100644 index 000000000..fd09040cd --- /dev/null +++ b/experiments/analysis-efficiency/questions/wasm-interface.txt @@ -0,0 +1 @@ +Statically inspect the real shipped WASM module at {{artifact_path}} using only REA tools. Report exact selected artifact SHA-256, valid/invalid status, section count, import count, export count, first four complete export WAT forms in producer order, decoded WAT SHA-256 and code-unit length, WABT release/source commit, and exact runtime_execution/network_fetch states. If complete delivery exceeds the MCP frame, use the exact retained reference for summary and wasm exports views. Preserve original strings, uncertainties and real Evidence IDs. Do not execute WASM, fetch URLs or use shell/file tools. Final JSON fields: artifact_sha256, validation, section_count, import_count, export_count, exports_first4, wat_sha256, wat_code_units, wabt_release, wabt_source_commit, runtime_execution, network_fetch, evidence_ids, limitations. diff --git a/experiments/analysis-efficiency/wasm-first-call.md b/experiments/analysis-efficiency/wasm-first-call.md new file mode 100644 index 000000000..fb554507a --- /dev/null +++ b/experiments/analysis-efficiency/wasm-first-call.md @@ -0,0 +1,149 @@ +# WASM first-call summary delivery + +Refs [#2016](https://github.com/morluto/rea/issues/2016). This bounded follow-up +adds optional MCP `inspect_wasm_artifact detail: summary`. Omitted/complete MCP +requests and CLI output remain complete. Full original Evidence is retained; +delivery selection is stripped before provider inputs, and exact export views +read the original parent without another WABT invocation. + +## Pinned repeated public SDK cohort + +- Control: `18e5d6275965c5f1d3ef9fe373d1792822098003`. +- Candidate: `45831c622a13368494d2a8f3a9718effa91c937c`. +- Runtime file-table SHA-256: control `ef65527c2d41b621f71059b7553050660d801ad905a61a71b81aba266f70e1ec`; + candidate `353f5513ea09b0a627fa99814cebe0a1231c6c8f7e0e12a28205521133478b1a`. +- Actual WABT 1.0.42, identical explicitly selected provider configuration and + effective search-path fingerprint, pinned Node 24.18.0 and locked SDK. +- Three paired fresh-process trials per digest-pinned shipped module, serial + on one CPU: control/candidate, candidate/control, control/candidate. +- Same validation/counts/first-four complete export forms/WAT identity/provider + identity/execution and fetch states; complete original source Evidence ID is + also compared. All nine pairs preserve these selected facts and source IDs. + +| Shipped module | Result JSON bytes, control → summary | Calls / errors | Median seconds, control → summary (ranges) | +| ----------------------------- | -----------------------------------: | -------------- | ----------------------------------------------- | +| `sqlite.wasm` | 20,118 → 18,533 | 3/1 → 2/0 | 15.962 (15.719–21.138) → 18.171 (10.242–18.649) | +| `vscode.tree-sitter-css.wasm` | 20,366 → 18,781 | 3/1 → 2/0 | 4.920 (4.885–4.936) → 5.191 (4.971–5.232) | +| `vscode.tree-sitter.wasm` | 6,433,872 → 18,891 | 1/0 → 2/0 | 5.225 (5.172–5.519) → 4.763 (4.729–4.830) | + +Byte counts include all investigation call results, including failed delivery +and extra views. They exclude catalog responses, raw wire framing and file +exports; they are not actual model tokens. The complete catalog is 2,359,082 +versus 2,362,451 JSON bytes (+3,369 bytes, about 0.14%). An extra export-view +call is included when summary replaces an otherwise fitting complete reply. + +| Shipped module | Median owned CPU seconds, control → summary | Median sampled family MiB, control → summary (maxima) | +| ----------------------------- | ------------------------------------------- | ----------------------------------------------------- | +| `sqlite.wasm` | 15.376 → 15.954 | 660.84 (671.27) → 633.07 (671.51) | +| `vscode.tree-sitter-css.wasm` | 5.760 → 6.027 | 692.91 (694.93) → 678.98 (693.88) | +| `vscode.tree-sitter.wasm` | 6.134 → 5.663 | 674.04 (695.66) → 550.66 (553.47) | + +All 18 scopes have no sampled abort and their last observations contain no +memory-high/hard/OOM/OOM-kill or task-limit event. Final counters are unverified +under the subsequent closeout audit below. Sampling can miss brief peaks. Application caches are cold per process; +host page caches are not flushed. SDK elapsed time includes startup, discovery, +schema work and persistence, not just provider analysis. Three observations +give descriptive medians/ranges, not a statistical or cross-domain speed claim. + +SQLite and the CSS grammar already recovered an oversized complete result +through retained views in the control. The new path removes the initial error +and one call; result-byte savings are correspondingly modest. The Tree-sitter +runtime fits the MCP frame but delivers megabytes of unneeded WAT; its summary +plus exact exports greatly reduces delivery. Full provider analysis and complete +Evidence retention still occur, so this does not establish an engine shortcut. + +An earlier 18-run cohort did not activate the treatment: the driver looked +for an inline enum and missed SDK-generated `$ref` definitions. Those original +runs and their source are archived separately and excluded from benefit claims. +The corrected driver validates the actual advertised 2020-12 request schema. + +## Real model verification + +Ten actual Codex CLI 0.161.0 trials request `gpt-5.6-terra` high: three paired +SQLite repetitions and one pair for each published VS Code component. The +resolved server-side model identity is not inferred from the requested alias. +Same question, fixed developer instructions, four allowlisted REA tools and +explicit WABT settings; private profiles, shell/web/other-agent restrictions +and target-byte preservation remain verified. + +All ten answers match the 12 requested producer facts, including exact export +forms and explicit execution/fetch states. Reported Evidence IDs occur in real +responses; all copied profiles are removed. Manual review retains static-analysis +limitations and makes no original-source/runtime claim. Every candidate chooses +`detail: summary` on its first inspection and then reads exports without WABT +rerun. Original control errors and client-context clipping remain in transcripts. + +| Module / observations per side | Total input, control → summary | Cached input | Uncached input | Output | +| --------------------------------- | -----------------------------: | ----------------: | --------------: | ------------: | +| `sqlite.wasm` / 3 | 96,832 → 79,264 | 70,656 → 51,456 | 26,215 → 28,004 | 1,470 → 1,512 | +| `vscode.tree-sitter-css.wasm` / 1 | 247,889 → 79,389 | 207,872 → 61,440 | 40,017 → 17,949 | 1,931 → 1,327 | +| `vscode.tree-sitter.wasm` / 1 | 556,424 → 282,165 | 478,208 → 239,360 | 78,216 → 42,805 | 2,560 → 2,110 | + +SQLite rows show medians of three observations; VS Code rows are single pairs. +Usage is the CLI-reported total for the turn, with cached/uncached input listed +separately. Lower total input does not imply a proportional monetary saving, +uncached-input reduction or faster answer. + +| Module | Calls / observed errors, control → summary | Seconds, control → summary | +| ----------------------------- | ------------------------------------------ | ----------------------------------------------- | +| `sqlite.wasm` | 3/1 → 2/0 | 61.521 (54.301–80.579) → 55.656 (42.960–62.145) | +| `vscode.tree-sitter-css.wasm` | 3/1 → 2/0 | 87.126 → 106.580 | +| `vscode.tree-sitter.wasm` | 4/0 → 2/0 | 56.952 → 104.999 | + +SQLite total input falls about 18% and median answer time about 10%, while +its median uncached input rises about 7%. The two VS Code pairs reduce total +input by about 49% and 68%, but observed answer time rises about 84% and 22%. +These data support optional smaller delivery; they do not establish uniform +model speedup. The runtime control also makes one protocol resource-discovery +call, included in the four-call count. Cache state, model reasoning and service +latency are not separated by this small sample. + +The model and SDK cohorts ran with the original resource wrapper. Their final +receipts retain last-sampled cgroup counters, not verified final counters. +The subsequent closeout correction regrades all ten existing answers: selected +facts, real IDs, unchanged target bytes and profile cleanup still pass, while +the final resource conditions are **unverified**. No model/build/provider run +was repeated for that review-only correction. Last-sampled peaks are 656–944 +MiB for model scopes with no observed limit events; one CPU, 4 GiB hard ceiling, +3 GiB early-stop and no-swap controls were active. This is not a proof of absent +late resource events or a fully resource-qualified performance conclusion. +The original transcripts/usage and regrades remain archived separately. + +## Reproduction and verification + +Use [the common measurement/model instructions](README.md) with the public +digest-pinned inputs in [corpus.json](corpus.json), `--schemas selected` and +`--delivery summary`. For each artifact run the above three paired orders +against separately frozen runtimes, then `compare.mjs`. Use the same +[WASM interface question](questions/wasm-interface.txt), four exact enabled +tools (`inspect_wasm_artifact`, `inspect_analysis_view`, `get_evidence_bundle`, +`export_evidence_bundle`), explicit WABT settings and resource scope for models. + +The VS Code artifacts are two published components from +[@vscode/tree-sitter-wasm 0.3.1](https://github.com/microsoft/vscode-tree-sitter-wasm). +The selected regular archive members have verified digests and package SHA-512 +integrity. This is component analysis, not proof of the whole VS Code runtime. + +The prototype passes 54 focused assertions and full static/docs gates. After +rebasing, 29 focused assertions and 42 actual installed-package WABT CLI/MCP +cases pass; the final rebased snapshot passes build/fast static and seven WASM +MCP workflows again. Omitted/explicit-complete behavior, retained identity, +fixed-server absence of retention, oversized continuation and invalid WAT +digest remain checked. No version publication or target execution occurs. +The final static/build scope has 51 soft memory-high events and no hard/OOM +events; this is not credited as a performance trial. + +Detailed JSON, transcripts, target bytes, profiles and acquisition receipts +stay outside Git. Source inputs, scripts and this concise measured scope are +reviewable in the repository. + +## Main integration checkpoint + +Prerequisite #2122 is merged at `58fb372071e56da9c5df0e37593ee3f0da6b64bc`. +The follow-up is rebased directly onto latest fetched main. Intervening changes +outside the prerequisite concern CLI/logging/CI planning rather than WASM +production/delivery. Seven focused WASM MCP workflows and one fresh real-WABT +SQLite public-SDK request pass against the new build: selected facts and original +source ID match the pinned candidate, summary uses two calls with no tool error. +Only the relevant integration checks and required pre-push gate are repeated; +earlier measurements retain their original runtimes, not the new main label. diff --git a/src/contracts/wasm/wasmToolContracts.ts b/src/contracts/wasm/wasmToolContracts.ts index 901dd7ded..34f26f11d 100644 --- a/src/contracts/wasm/wasmToolContracts.ts +++ b/src/contracts/wasm/wasmToolContracts.ts @@ -4,16 +4,29 @@ import { } from "../../domain/wasm/wasmArtifact.js"; import type { ToolContract } from "../toolContractTypes.js"; import { toolContractMetadata } from "../toolEffects.js"; -import { evidenceResultOf } from "../toolOutputSchemaPrimitives.js"; +import { + analysisDetailSchema, + analysisDetailOutputSchemaOf, +} from "../analysisDetail.js"; +import { wasmArtifactSummaryViewResultSchema } from "../../domain/analysisView/analysisView.js"; + +/** Delivery selection does not change the provider input or original Evidence. */ +export const wasmArtifactInspectionInputSchema = + inspectWasmArtifactInputSchema.extend({ + detail: analysisDetailSchema.unwrap().optional(), + }); /** Optional caller-supplied WABT capability with target-free CLI/MCP parity. */ export const INSPECT_WASM_ARTIFACT_CONTRACT = { name: "inspect_wasm_artifact", ...toolContractMetadata("inspect_wasm_artifact"), kind: "artifact-provider", description: - "Validate an explicit local WASM artifact with caller-supplied WABT 1.0.42, retain exact byte SHA-256, sections, upstream import/export WAT forms and decoded WAT inline as Evidence. Optionally associate selected JavaScript glue literals with explicit local candidates while preserving ambiguous URL/basename matches. Configure absolute REA_WABT_BIN_DIRECTORY. No installation, implicit fetch, WASM execution or original-source claim.", - inputSchema: inspectWasmArtifactInputSchema, - outputSchema: evidenceResultOf(wasmArtifactSchema), + 'Validate an explicit local WASM artifact with caller-supplied WABT 1.0.42. Omitted or complete detail returns exact byte SHA-256, sections, upstream import/export WAT forms and decoded WAT inline as Evidence. For counts, validation and WAT identity without WAT text, pass detail: "summary" on this first call; it retains the complete original Evidence and returns the same typed summary as inspect_analysis_view, avoiding oversized complete delivery. Read exact imports/exports or WAT pages with inspect_analysis_view using normalized_result.parent_evidence_id. Delivery selection does not change analysis, provider inputs or source identity. Optionally associate selected JavaScript glue literals with explicit local candidates while preserving ambiguous URL/basename matches. Configure absolute REA_WABT_BIN_DIRECTORY. No installation, implicit fetch, WASM execution or original-source claim.', + inputSchema: wasmArtifactInspectionInputSchema, + outputSchema: analysisDetailOutputSchemaOf( + wasmArtifactSchema, + wasmArtifactSummaryViewResultSchema, + ), examples: [ { title: "Inspect a selected local WASM module", diff --git a/src/domain/analysisView/analysisView.ts b/src/domain/analysisView/analysisView.ts index ce0b55946..539557877 100644 --- a/src/domain/analysisView/analysisView.ts +++ b/src/domain/analysisView/analysisView.ts @@ -302,6 +302,13 @@ export const artifactInventorySummaryViewResultSchema = z.strictObject({ summary: inventorySummarySchema, }); +/** Exact summary carrier for WASM inspection's optional delivery contract. */ +export const wasmArtifactSummaryViewResultSchema = z.strictObject({ + ...summaryViewResultBase, + parent_operation: z.literal("inspect_wasm_artifact"), + summary: wasmSummarySchema, +}); + /** Projected facts for one selected view of retained analysis Evidence. */ export const analysisViewResultSchema = z.discriminatedUnion("kind", [ z.strictObject({ diff --git a/src/server/createServer.ts b/src/server/createServer.ts index 3220fe8e7..742511ffc 100644 --- a/src/server/createServer.ts +++ b/src/server/createServer.ts @@ -453,6 +453,7 @@ const registerConfiguredAnalysisTools = ( toolLogger, recordEvidence, withAdmittedAnalysis, + evidenceById, ); registerEvmTools( server, diff --git a/src/server/registerWasmTools.ts b/src/server/registerWasmTools.ts index 9c0bd7699..494972b2f 100644 --- a/src/server/registerWasmTools.ts +++ b/src/server/registerWasmTools.ts @@ -2,6 +2,10 @@ import type { EvidenceMcpServer } from "./EvidenceMcpServer.js"; import type { WasmArtifactService } from "../application/wasm/WasmArtifactService.js"; import type { EvidenceWriter } from "../application/investigation/InvestigationRecordPort.js"; +import type { EvidenceLookup } from "../application/EvidenceInputResolver.js"; +import { summarizeRetainedAnalysis } from "../application/analysisView/AnalysisViewService.js"; +import { analysisInputErrorFromIssues } from "../domain/inputIssueProjection.js"; +import { err } from "../domain/result.js"; import { wasmArtifactModule } from "../composition/wasm.js"; import type { Logger } from "pino"; import { createEvidenceToolHandler } from "./contractToolHandler.js"; @@ -15,11 +19,31 @@ export const registerWasmTools = ( logger: Logger, recordEvidence?: EvidenceWriter["recordEvidence"], withAdmittedAnalysis?: WithAdmittedAnalysis, + evidenceById?: EvidenceLookup, ): void => { const contract = wasmArtifactModule.contract; const handler = createEvidenceToolHandler( server, - (input, options) => service.inspect(input, options), + async (rawInput, options) => { + const parsed = contract.inputSchema.safeParse(rawInput); + if (!parsed.success) + return err( + analysisInputErrorFromIssues( + contract.name, + parsed.error.issues, + rawInput, + { cause: parsed.error }, + ), + ); + const { detail, ...input } = parsed.data; + const inspected = await service.inspect(input, options); + return detail === "summary" && inspected.ok + ? summarizeRetainedAnalysis(inspected.value, { + recordEvidence, + evidenceById, + }) + : inspected; + }, logger, recordEvidence, withAdmittedAnalysis, diff --git a/tests/boundary/mcp/wasmArtifactMcp.test.ts b/tests/boundary/mcp/wasmArtifactMcp.test.ts index ce42e9b9c..49c7ad762 100644 --- a/tests/boundary/mcp/wasmArtifactMcp.test.ts +++ b/tests/boundary/mcp/wasmArtifactMcp.test.ts @@ -6,6 +6,7 @@ import { join } from "node:path"; import { WasmArtifactService } from "../../../src/application/wasm/WasmArtifactService.js"; import { toolContract } from "../../../src/contracts/toolContracts.js"; import { createEvidence, parseEvidence } from "../../../src/domain/evidence.js"; +import { wasmArtifactSummaryViewResultSchema } from "../../../src/domain/analysisView/analysisView.js"; import { createHash } from "node:crypto"; import { z } from "zod"; import { createServer } from "../../../src/server/createServer.js"; @@ -33,6 +34,63 @@ const wasmToolSchemas = async (client: Client) => { return { name: advertised.name, ajv, inputSchema, outputSchema }; }; +const assertFirstSummaryDelivery = async ( + harness: Awaited>, + value: ReturnType, + inspect: WasmArtifactService["provider"]["inspect"], +) => { + const { + ajv: wasmAjv, + inputSchema, + outputSchema, + } = await wasmToolSchemas(harness.client); + const summaryInput = { path: value.artifact.path, detail: "summary" }; + expect(wasmAjv.validate(inputSchema, summaryInput)).toBe(true); + expect( + wasmAjv.validate(inputSchema, { ...summaryInput, detail: "brief" }), + ).toBe(false); + const directSummary = await harness.client.callTool({ + name: "inspect_wasm_artifact", + arguments: summaryInput, + }); + expect(directSummary.isError).not.toBe(true); + expect( + wasmAjv.validate(outputSchema, directSummary.structuredContent), + JSON.stringify(wasmAjv.errors), + ).toBe(true); + const summaryEvidence = parseEvidence(directSummary.structuredContent); + const summary = wasmArtifactSummaryViewResultSchema.parse( + summaryEvidence.normalized_result, + ); + expect(summaryEvidence.confidence).toBe("derived"); + expect(summaryEvidence.evidence_links).toEqual([summary.parent_evidence_id]); + expect(summary.summary.wat).toMatchObject({ + sha256: value.wat.sha256, + code_units: value.wat.text.length, + }); + expect(summary.summary.wat).not.toHaveProperty("text"); + const retained = harness.session.evidenceById(summary.parent_evidence_id); + if (retained === undefined) throw new Error("Summary lost original Evidence"); + expect(retained.normalized_result).toEqual(value); + expect(retained.parameters).toEqual({ + path: value.artifact.path, + glue_paths: [], + candidate_paths: [], + }); + expect(inspect).toHaveBeenCalledTimes(1); + expect(inspect).toHaveBeenLastCalledWith( + retained.parameters, + expect.objectContaining({ signal: expect.any(AbortSignal) }), + ); + expect( + wasmAjv.validate(outputSchema, { + ...summaryEvidence, + normalized_result: { ...summary, parent_operation: "inventory_artifact" }, + }), + ).toBe(false); + return { summary, retained }; +}; + it("directs rejected native WASM opening to the advertised static tool while preserving missing-WABT diagnostics", async () => { const directory = await createTestTempDirectory("rea-wasm-recovery-"); const path = join( @@ -84,6 +142,16 @@ it("directs rejected native WASM opening to the advertised static tool while pre }, }, }); + const summary = await client.callTool({ + name, + arguments: { ...input, detail: "summary" }, + }); + expect(parseMcpToolError(summary)).toMatchObject({ + error: { + code: "capability_unavailable", + details: { reason: expect.stringContaining("REA_WABT_BIN_DIRECTORY") }, + }, + }); await client.ping(); expect(createClient).not.toHaveBeenCalled(); expect(await readFile(path)).toEqual( @@ -157,14 +225,22 @@ it("reads an oversized completed WASM result through its reported retained refer representation: "decoded-wat" as const, }, }; + const inspect = vi.fn(() => + Promise.resolve(ok(wasmArtifactExecution(value))), + ); const service = new WasmArtifactService({ close: () => Promise.resolve(), - inspect: () => Promise.resolve(ok(wasmArtifactExecution(value))), + inspect, }); const harness = await createApplicationMcpHarness({ serverOptions: { wasmArtifact: service }, }); onTestFinished(harness.close); + const { summary, retained } = await assertFirstSummaryDelivery( + harness, + value, + inspect, + ); const reply = await harness.client.callTool({ name: "inspect_wasm_artifact", arguments: { path: value.artifact.path }, @@ -182,6 +258,17 @@ it("reads an oversized completed WASM result through its reported retained refer }) .parse(parseMcpToolError(reply).error).details .reported_limits.evidence_reference; + expect(reference.evidence_id).toBe(summary.parent_evidence_id); + const explicitComplete = await harness.client.callTool({ + name: "inspect_wasm_artifact", + arguments: { path: value.artifact.path, detail: "complete" }, + }); + expect(parseMcpToolError(explicitComplete)).toMatchObject({ + error: { + code: "resource_constraint", + details: { reported_limits: { evidence_reference: reference } }, + }, + }); const catalog = await harness.client.listTools(); const advertised = catalog.tools.find( (tool) => tool.name === "inspect_analysis_view", @@ -244,6 +331,17 @@ it("reads an oversized completed WASM result through its reported retained refer } const original = harness.session.evidenceById(reference.evidence_id); expect(original?.normalized_result).toEqual(value); + expect(original).toEqual(retained); + expect(inspect).toHaveBeenCalledTimes(3); + await harness.client.ping(); +}); + +it("rejects inconsistent decoded-WAT digests in portable Evidence without a provider run", async () => { + const value = wasmArtifactFixture(); + const harness = await createApplicationMcpHarness({ + serverOptions: { environment: {} }, + }); + onTestFinished(harness.close); const inconsistent = createEvidence( { path: value.artifact.path, @@ -325,6 +423,50 @@ it("rejects absent or inconsistent WASM source identity before deriving portable await harness.client.ping(); }); +it("requires retention for WASM summary delivery while keeping fixed-server complete delivery", async () => { + const value = wasmArtifactFixture(); + const service = new WasmArtifactService({ + close: () => Promise.resolve(), + inspect: () => Promise.resolve(ok(wasmArtifactExecution(value))), + }); + const session = createTestBinarySession(() => { + throw new Error("No deep provider"); + }); + const server = createServer( + { kind: "fixed", analysis: session }, + { wasmArtifact: service }, + ); + const client = new Client({ name: "wasm-fixed-delivery", version: "1" }); + const [a, b] = InMemoryTransport.createLinkedPair(); + try { + await server.connect(b); + await client.connect(a); + const summary = await client.callTool({ + name: "inspect_wasm_artifact", + arguments: { path: value.artifact.path, detail: "summary" }, + }); + expect(parseMcpToolError(summary)).toMatchObject({ + error: { + code: "capability_unavailable", + message: expect.stringContaining("does not retain Evidence"), + }, + }); + const complete = await client.callTool({ + name: "inspect_wasm_artifact", + arguments: { path: value.artifact.path }, + }); + expect(complete.isError).not.toBe(true); + expect(parseEvidence(complete.structuredContent).normalized_result).toEqual( + value, + ); + await client.ping(); + } finally { + await client.close(); + await server.close(); + await session.close(); + } +}); + it("retries cleanup of the injected WASM module after server shutdown fails", async () => { const close = vi .fn() From 4a4ae9b2b9712f7ac16f2d23c71b5c29b0c23d2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Gon=C3=A7alves=20Araujo?= <47118053+dandgabr@users.noreply.github.com> Date: Mon, 12 Oct 2026 11:01:58 -0300 Subject: [PATCH 10/25] feat: add Frida instrumentation support (#1359) * feat: add Frida instrumentation support * fix: resolve Frida CI checks * test: cover Frida contract validation branches * docs: link Frida instrumentation guide * fix(frida): handle cancellation cleanup safely * fix(frida): address PR review findings * fix(frida): preserve resume and message observations * fix(ci): use canonical registry for rolldown * fix(frida): declare foreign code effects * fix(frida): retain failed startup resources for cleanup retries Keep failed startup session and spawn handles owned until detach, resume, and remote lease cleanup complete. Serialize retries and release each remote reference once. Await CLI provider cleanup while preserving the primary failure and any completed observations. Add regressions for retained startup resources, concurrent cleanup, shared remote leases, and CLI cleanup outcomes. Validation: 41 focused tests pass on the PR head and its latest-main integration; serial static and docs checks pass. Real Linux Frida 17.23.2 workflows and the native spawn/controlled-failure cleanup probe pass. Assisted-by: Kogasa (gensokyo-skills:kogasa-surprise-testing) --------- Co-authored-by: N0zoM1z0 <1705004398@qq.com> --- docs/frida-instrumentation.md | 13 + docs/index.md | 2 + docs/testing.md | 12 + knip.json | 1 + package-lock.json | 450 ++++-- package.json | 8 + scripts/lib/catalog-builders.mjs | 9 + scripts/lib/catalog-core.mjs | 2 + scripts/verify-frida.mjs | 198 +++ scripts/verify/fridaBindingsCompatibility.ts | 28 + src/application/CapabilityInventory.ts | 6 + .../OptionalObservationProviders.ts | 5 +- .../frida/FridaInstrumentationManager.test.ts | 988 +++++++++++++ .../frida/FridaInstrumentationPort.ts | 117 ++ .../frida/FridaInstrumentationService.test.ts | 508 +++++++ .../frida/FridaInstrumentationService.ts | 400 ++++++ src/cli.ts | 4 + src/cli/fridaCommands.ts | 275 ++++ src/cliCommandNames.ts | 12 + src/contracts/fridaRemoteAddress.ts | 21 + src/contracts/fridaToolContracts.test.ts | 170 +++ src/contracts/fridaToolContracts.ts | 327 +++++ src/contracts/toolContractExamples.ts | 23 + src/contracts/toolContracts.ts | 2 + src/contracts/toolEffects.test.ts | 5 + src/contracts/toolEffects.ts | 51 + src/frida/FridaInstrumentationManager.ts | 1230 +++++++++++++++++ src/frida/frida-bindings.d.ts | 78 ++ src/server/createServer.ts | 35 +- src/server/registerFridaTools.ts | 342 +++++ tests/boundary/cli/fridaCli.test.ts | 236 ++++ .../filesystem/productCatalog.test.ts | 9 + tests/boundary/mcp/fridaMcp.test.ts | 415 ++++++ tsconfig.frida-verification.json | 11 + 34 files changed, 5845 insertions(+), 148 deletions(-) create mode 100644 docs/frida-instrumentation.md create mode 100644 scripts/verify-frida.mjs create mode 100644 scripts/verify/fridaBindingsCompatibility.ts create mode 100644 src/application/frida/FridaInstrumentationManager.test.ts create mode 100644 src/application/frida/FridaInstrumentationPort.ts create mode 100644 src/application/frida/FridaInstrumentationService.test.ts create mode 100644 src/application/frida/FridaInstrumentationService.ts create mode 100644 src/cli/fridaCommands.ts create mode 100644 src/contracts/fridaRemoteAddress.ts create mode 100644 src/contracts/fridaToolContracts.test.ts create mode 100644 src/contracts/fridaToolContracts.ts create mode 100644 src/frida/FridaInstrumentationManager.ts create mode 100644 src/frida/frida-bindings.d.ts create mode 100644 src/server/registerFridaTools.ts create mode 100644 tests/boundary/cli/fridaCli.test.ts create mode 100644 tests/boundary/mcp/fridaMcp.test.ts create mode 100644 tsconfig.frida-verification.json diff --git a/docs/frida-instrumentation.md b/docs/frida-instrumentation.md new file mode 100644 index 000000000..c93c76465 --- /dev/null +++ b/docs/frida-instrumentation.md @@ -0,0 +1,13 @@ +# Frida instrumentation + +REA exposes Frida device and process discovery, attach or spawn sessions, and caller-authored JavaScript instrumentation through the CLI and MCP. The Node bindings are an optional package dependency and load only when a Frida operation runs. REA does not install Frida server on a target. + +For remote devices, pass a host with an optional port (`host[:port]`; bracket IPv6 addresses) and any token, certificate, origin, or keepalive options on that operation. The keepalive interval is measured in seconds, matching Frida Core 17.23.2. Authentication values are held only for the active call or session and are excluded from Evidence. Do not embed credentials in the endpoint address. + +MCP exposes `list_frida_devices`, `list_frida_processes`, `start_frida_session`, `load_frida_script`, `resume_frida_session`, `unload_frida_script`, `frida_session_status`, and `close_frida_session`. A spawned process starts paused so scripts can load first. Closing unloads session scripts and detaches. If a spawn remains paused, REA resumes it only after script cleanup or session detach confirms the instrumentation is released; if both cleanup paths fail, the target stays paused and the cleanup error is retained. REA never kills the target. MCP sessions end with the server process. The one-shot CLI command `instrument-with-frida` accepts one inline script or script file, observes messages for a bounded duration, and returns Evidence with the source hash and captured messages. + +Failed startup cleanup retains acquired Frida handles, including a paused spawn that has not attached, for later cleanup retries. Its remote connection stays leased until session detach and target resume complete. CLI commands await provider cleanup before returning; MCP retains these resources for cleanup when its server closes. A failed cleanup remains reported rather than treating an unregistered session as fully released. + +Frida scripts execute inside the selected target with that process's memory, filesystem, and network authority. Message capture is bounded to 16 MiB and reports truncation. Script source is not stored in Evidence; its selected path and SHA-256 digest are. Evidence retains non-authentication target selection such as device or endpoint, mode, program or PID, and spawn arguments. Authentication values remain excluded. Results report the observed session and messages, while unsupported platform metadata remains unknown. A request cancelled before a Frida operation starts has no provider effects. If cancellation arrives during a native Frida call, REA waits for that call to return, then closes any session it created and returns the observed session as a partial observation. Cancellation Evidence separately reports whether a target resume was observed, whether instrumentation cleanup completed, and that final target liveness remains unknown. REA does not kill the target. Remote discovery retains its observations and reports a separate cleanup error if the temporary connection cannot be removed. + +The [Frida Core API](https://frida.re/docs/frida-core-api/) documents device management, sessions, and script loading; the [JavaScript API](https://frida.re/docs/javascript-api/) documents target-side instrumentation. [Luma](https://luma.frida.re/) is Frida's standalone GUI; REA does not automate or control it. `npm run verify:frida` requires a Linux host with the optional Frida binding and uses a short-lived local Node.js fixture created by the verifier. Add `-- --address HOST:PORT --pid PID` to run the separate remote attach lane against a caller-selected target. Mock tests do not establish real device/server compatibility. diff --git a/docs/index.md b/docs/index.md index 42ae8f680..17fed2a95 100644 --- a/docs/index.md +++ b/docs/index.md @@ -43,4 +43,6 @@ npx -y rea-agents@latest analyze-javascript-application /absolute/path/to/app -- See [installation and setup](./installation.md) for requirements and configuration. +For runtime instrumentation, see the [Frida guide](./frida-instrumentation.md). + ![REA inspecting a native binary in Hopper](./assets/rea-hopper-analysis.png) diff --git a/docs/testing.md b/docs/testing.md index 9aaeb8464..5df9c4d9e 100644 --- a/docs/testing.md +++ b/docs/testing.md @@ -1414,6 +1414,18 @@ npm run evidence:check The records preserve unsupported and unverified coverage as explicit unknowns. Run the matching real-tool prerequisites described in this guide. +## Frida instrumentation + +`npm run verify:frida` builds REA, spawns a short-lived local Node.js fixture, +loads a harmless marker script before resuming it, captures the message, and +detaches. This lane requires Linux on the REA host and the optional Frida +binding; it does not need a Frida server or a caller-selected process. To run +the separate remote attach lane, pass `-- --address HOST:PORT --pid PID` for an +explicitly selected process. Optional remote flags are `--token`, +`--certificate`, `--origin`, and `--keepalive-interval` (seconds); authentication values +are not written to files or included in the report. Mock tests do not establish +real device/server compatibility. + ### Offline WASM artifacts `npm run verify:wasm:artifact -- --require-tools` uses an absolute diff --git a/knip.json b/knip.json index 8d38b4ef1..72d093397 100644 --- a/knip.json +++ b/knip.json @@ -7,6 +7,7 @@ "src/contracts/errorSchemas.ts", "src/evaluation/AgentEvaluationFixtures.ts", "src/application/RealHopperAssertions.ts", + "scripts/verify/fridaBindingsCompatibility.ts", "scripts/verify/termux/browser.mjs", "scripts/verify-real-ghidra-noreturn.mjs", "tests/processOwnershipRunner.ts" diff --git a/package-lock.json b/package-lock.json index de9c141f5..749f4614f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -73,6 +73,9 @@ }, "engines": { "node": "^22.19.0 || ^24.11.0 || >=26.0.0" + }, + "optionalDependencies": { + "frida": "17.23.2" } }, "node_modules/@algolia/abtesting": { @@ -742,9 +745,6 @@ "x64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -903,9 +903,6 @@ "arm64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -923,9 +920,6 @@ "arm64" ], "dev": true, - "libc": [ - "musl" - ], "license": "MIT", "optional": true, "os": [ @@ -943,9 +937,6 @@ "ppc64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -963,9 +954,6 @@ "riscv64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -983,9 +971,6 @@ "riscv64" ], "dev": true, - "libc": [ - "musl" - ], "license": "MIT", "optional": true, "os": [ @@ -1003,9 +988,6 @@ "s390x" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -1023,9 +1005,6 @@ "x64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -1043,9 +1022,6 @@ "x64" ], "dev": true, - "libc": [ - "musl" - ], "license": "MIT", "optional": true, "os": [ @@ -1239,9 +1215,6 @@ "arm64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -1256,9 +1229,6 @@ "arm64" ], "dev": true, - "libc": [ - "musl" - ], "license": "MIT", "optional": true, "os": [ @@ -1273,9 +1243,6 @@ "ppc64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -1290,9 +1257,6 @@ "riscv64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -1307,9 +1271,6 @@ "riscv64" ], "dev": true, - "libc": [ - "musl" - ], "license": "MIT", "optional": true, "os": [ @@ -1324,9 +1285,6 @@ "s390x" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -1341,9 +1299,6 @@ "x64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -1358,9 +1313,6 @@ "x64" ], "dev": true, - "libc": [ - "musl" - ], "license": "MIT", "optional": true, "os": [ @@ -1878,9 +1830,6 @@ "arm64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -1898,9 +1847,6 @@ "arm64" ], "dev": true, - "libc": [ - "musl" - ], "license": "MIT", "optional": true, "os": [ @@ -1918,9 +1864,6 @@ "ppc64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -1938,9 +1881,6 @@ "riscv64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -1958,9 +1898,6 @@ "riscv64" ], "dev": true, - "libc": [ - "musl" - ], "license": "MIT", "optional": true, "os": [ @@ -1978,9 +1915,6 @@ "s390x" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -1998,9 +1932,6 @@ "x64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -2018,9 +1949,6 @@ "x64" ], "dev": true, - "libc": [ - "musl" - ], "license": "MIT", "optional": true, "os": [ @@ -2214,9 +2142,6 @@ "arm64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -2234,9 +2159,6 @@ "arm64" ], "dev": true, - "libc": [ - "musl" - ], "license": "MIT", "optional": true, "os": [ @@ -2254,9 +2176,6 @@ "ppc64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -2274,9 +2193,6 @@ "s390x" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -2294,9 +2210,6 @@ "x64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -2314,9 +2227,6 @@ "x64" ], "dev": true, - "libc": [ - "musl" - ], "license": "MIT", "optional": true, "os": [ @@ -2476,9 +2386,6 @@ "arm" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -2493,9 +2400,6 @@ "arm" ], "dev": true, - "libc": [ - "musl" - ], "license": "MIT", "optional": true, "os": [ @@ -2510,9 +2414,6 @@ "arm64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -2527,9 +2428,6 @@ "arm64" ], "dev": true, - "libc": [ - "musl" - ], "license": "MIT", "optional": true, "os": [ @@ -2544,9 +2442,6 @@ "loong64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -2561,9 +2456,6 @@ "loong64" ], "dev": true, - "libc": [ - "musl" - ], "license": "MIT", "optional": true, "os": [ @@ -2578,9 +2470,6 @@ "ppc64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -2595,9 +2484,6 @@ "ppc64" ], "dev": true, - "libc": [ - "musl" - ], "license": "MIT", "optional": true, "os": [ @@ -2612,9 +2498,6 @@ "riscv64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -2629,9 +2512,6 @@ "riscv64" ], "dev": true, - "libc": [ - "musl" - ], "license": "MIT", "optional": true, "os": [ @@ -2646,9 +2526,6 @@ "s390x" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -2663,9 +2540,6 @@ "x64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -2680,9 +2554,6 @@ "x64" ], "dev": true, - "libc": [ - "musl" - ], "license": "MIT", "optional": true, "os": [ @@ -4142,6 +4013,16 @@ ], "license": "MIT" }, + "node_modules/bindings": { + "version": "1.5.0", + "resolved": "https://registry.npmjs.org/bindings/-/bindings-1.5.0.tgz", + "integrity": "sha512-p2q/t/mhvuOj/UeLlV6566GD/guowlr0hHxClI0W9m7MWYkL1F0hLo+0Aexs9HSPCtR1SXQ0TD3MMKrXZajbiQ==", + "license": "MIT", + "optional": true, + "dependencies": { + "file-uri-to-path": "1.0.0" + } + }, "node_modules/birpc": { "version": "2.9.0", "resolved": "https://registry.npmjs.org/birpc/-/birpc-2.9.0.tgz", @@ -4152,6 +4033,58 @@ "url": "https://github.com/sponsors/antfu" } }, + "node_modules/bl": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/bl/-/bl-4.1.0.tgz", + "integrity": "sha512-1W07cM9gS6DcLperZfFSj+bWLtaPGSOHWhPiGzXmvVJbRLdG82sH/Kn8EtW1VqWVA54AKf2h5k5BbnIbwF3h6w==", + "license": "MIT", + "optional": true, + "dependencies": { + "buffer": "^5.5.0", + "inherits": "^2.0.4", + "readable-stream": "^3.4.0" + } + }, + "node_modules/bl/node_modules/buffer": { + "version": "5.7.1", + "resolved": "https://registry.npmjs.org/buffer/-/buffer-5.7.1.tgz", + "integrity": "sha512-EHcyIPBQ4BSGlvjB16k5KgAJ27CIsHY/2JBmCRReo48y9rQ3MaUzWX3KVlBa4U7MyX02HdVj0K7C3WaB3ju7FQ==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ], + "license": "MIT", + "optional": true, + "dependencies": { + "base64-js": "^1.3.1", + "ieee754": "^1.1.13" + } + }, + "node_modules/bl/node_modules/readable-stream": { + "version": "3.6.2", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-3.6.2.tgz", + "integrity": "sha512-9u/sniCrY3D5WdsERHzHE4G2YCXqoG5FTHUiCC4SIbr6XcLZBY05ya9EKjYek9O5xOAwjGq+1JdGBAS7Q9ScoA==", + "license": "MIT", + "optional": true, + "dependencies": { + "inherits": "^2.0.3", + "string_decoder": "^1.1.1", + "util-deprecate": "^1.0.1" + }, + "engines": { + "node": ">= 6" + } + }, "node_modules/brace-expansion": { "version": "5.0.12", "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-5.0.12.tgz", @@ -4290,6 +4223,13 @@ "url": "https://github.com/sponsors/wooorm" } }, + "node_modules/chownr": { + "version": "1.1.4", + "resolved": "https://registry.npmjs.org/chownr/-/chownr-1.1.4.tgz", + "integrity": "sha512-jJ0bqzaylmJtVnNgzTeSOs8DPavpbYgEr/b0YL8/2GO3xJEhInFmhKMUnEJQjZumK7KXGFhUy89PrsJWlakBVg==", + "license": "ISC", + "optional": true + }, "node_modules/clean-git-ref": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/clean-git-ref/-/clean-git-ref-2.0.1.tgz", @@ -4418,6 +4358,16 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/deep-extend": { + "version": "0.6.0", + "resolved": "https://registry.npmjs.org/deep-extend/-/deep-extend-0.6.0.tgz", + "integrity": "sha512-LOHxIOaPYdHlJRtCQfDIVZtfw/ufM8+rVj649RIHzcm/vGwQRXFt6OPqIFWsm2XEMrNIEtWR64sY1LEKD2vAOA==", + "license": "MIT", + "optional": true, + "engines": { + "node": ">=4.0.0" + } + }, "node_modules/define-data-property": { "version": "1.1.4", "resolved": "https://registry.npmjs.org/define-data-property/-/define-data-property-1.1.4.tgz", @@ -4449,9 +4399,8 @@ "version": "2.1.2", "resolved": "https://registry.npmjs.org/detect-libc/-/detect-libc-2.1.2.tgz", "integrity": "sha512-Btj2BOOO83o3WyH59e8MgXsxEQVcarkUOpEYrubB0urwnN10yQ364rsiByU11nZlqWYZm05i/of7io4mzihBtQ==", - "dev": true, + "devOptional": true, "license": "Apache-2.0", - "peer": true, "engines": { "node": ">=8" } @@ -4513,6 +4462,16 @@ "dev": true, "license": "MIT" }, + "node_modules/end-of-stream": { + "version": "1.4.5", + "resolved": "https://registry.npmjs.org/end-of-stream/-/end-of-stream-1.4.5.tgz", + "integrity": "sha512-ooEGc6HP26xXq/N+GCGOT0JKCLDGrq2bQUZrQ7gyrJiZANJ/8YDTxTpQBXGMn+WbIQXNVpyWymm7KYVICQnyOg==", + "license": "MIT", + "optional": true, + "dependencies": { + "once": "^1.4.0" + } + }, "node_modules/entities": { "version": "7.0.1", "resolved": "https://registry.npmjs.org/entities/-/entities-7.0.1.tgz", @@ -4644,6 +4603,16 @@ "node": ">=22" } }, + "node_modules/expand-template": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/expand-template/-/expand-template-2.0.3.tgz", + "integrity": "sha512-XYfuKMvj4O35f/pOXLObndIRvyQ+/+6AhODh+OKWj9S9498pHHn/IMszH+gt0fBCRWMNfk1ZSp5x3AifmnI2vg==", + "license": "(MIT OR WTFPL)", + "optional": true, + "engines": { + "node": ">=6" + } + }, "node_modules/expect-type": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/expect-type/-/expect-type-1.4.0.tgz", @@ -4751,6 +4720,13 @@ } } }, + "node_modules/file-uri-to-path": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/file-uri-to-path/-/file-uri-to-path-1.0.0.tgz", + "integrity": "sha512-0Zt+s3L7Vf1biwWZ29aARiVYLx7iMGnEUl9x33fbB/j3jR81u/O2LbqK+Bm1CDSNDKVtJ/YjwY7TUd5SkeLQLw==", + "license": "MIT", + "optional": true + }, "node_modules/focus-trap": { "version": "7.8.0", "resolved": "https://registry.npmjs.org/focus-trap/-/focus-trap-7.8.0.tgz", @@ -4793,6 +4769,29 @@ "node": ">=18.3.0" } }, + "node_modules/frida": { + "version": "17.23.2", + "resolved": "https://registry.npmjs.org/frida/-/frida-17.23.2.tgz", + "integrity": "sha512-gRCmmTUFdEILyHRN1b66erRoXafwsfWTqXyoBBqkSCFX0Ue7Jhvs/KhA3JO0XrgITNlPd91QAGsFvWzh0mhVgg==", + "hasInstallScript": true, + "license": "LGPL-2.0 WITH WxWindows-exception-3.1", + "optional": true, + "dependencies": { + "bindings": "^1.5.0", + "minimatch": "^10.0.1", + "prebuild-install": "^7.1.3" + }, + "engines": { + "node": ">=16" + } + }, + "node_modules/fs-constants": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/fs-constants/-/fs-constants-1.0.0.tgz", + "integrity": "sha512-y6OAwoSIf7FyjMIv94u+b5rdheZEjzR63GTyZJm5qh4Bi+2YgwLCcI/fPFZkL5PSixOt6ZNKm+w+Hfp/Bciwow==", + "license": "MIT", + "optional": true + }, "node_modules/fsevents": { "version": "2.3.3", "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.3.tgz", @@ -4880,6 +4879,13 @@ "url": "https://github.com/privatenumber/get-tsconfig?sponsor=1" } }, + "node_modules/github-from-package": { + "version": "0.0.0", + "resolved": "https://registry.npmjs.org/github-from-package/-/github-from-package-0.0.0.tgz", + "integrity": "sha512-SyHy3T1v2NUXn29OsWdxmK6RwHD+vkj3v8en8AOBZ1wBQ/hCAQ5bAQTD02kW4W9tUp/3Qh6J8r9EvntiyCmOOw==", + "license": "MIT", + "optional": true + }, "node_modules/glob": { "version": "13.0.6", "resolved": "https://registry.npmjs.org/glob/-/glob-13.0.6.tgz", @@ -5119,6 +5125,13 @@ "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==", "license": "ISC" }, + "node_modules/ini": { + "version": "1.3.8", + "resolved": "https://registry.npmjs.org/ini/-/ini-1.3.8.tgz", + "integrity": "sha512-JV/yugV2uzW5iMRSiZAyDtQd+nxtUnjeLt0acNdw98kKLrvuRVyB80tsREOE7yvGVgalhZ6RNXCmEHkUKBKxew==", + "license": "ISC", + "optional": true + }, "node_modules/is-callable": { "version": "1.2.7", "resolved": "https://registry.npmjs.org/is-callable/-/is-callable-1.2.7.tgz", @@ -5555,9 +5568,6 @@ "arm64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MPL-2.0", "optional": true, "os": [ @@ -5580,9 +5590,6 @@ "arm64" ], "dev": true, - "libc": [ - "musl" - ], "license": "MPL-2.0", "optional": true, "os": [ @@ -5605,9 +5612,6 @@ "x64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MPL-2.0", "optional": true, "os": [ @@ -5630,9 +5634,6 @@ "x64" ], "dev": true, - "libc": [ - "musl" - ], "license": "MPL-2.0", "optional": true, "os": [ @@ -6070,6 +6071,13 @@ "dev": true, "license": "MIT" }, + "node_modules/mkdirp-classic": { + "version": "0.5.3", + "resolved": "https://registry.npmjs.org/mkdirp-classic/-/mkdirp-classic-0.5.3.tgz", + "integrity": "sha512-gKLcREMhtuZRwRAfqP3RFW+TK4JqApVBtOIftVgjuABpAtpxhPGaDcfvbhNvD0B8iD1oUr/txX35NjcaY6Ns/A==", + "license": "MIT", + "optional": true + }, "node_modules/nanoid": { "version": "3.3.20", "resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.3.20.tgz", @@ -6089,6 +6097,26 @@ "node": "^10 || ^12 || ^13.7 || ^14 || >=15.0.1" } }, + "node_modules/napi-build-utils": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/napi-build-utils/-/napi-build-utils-2.0.0.tgz", + "integrity": "sha512-GEbrYkbfF7MoNaoh2iGG84Mnf/WZfB0GdGEsM8wz7Expx/LlWf5U8t9nvJKXSp3qr5IsEbK04cBGhol/KwOsWA==", + "license": "MIT", + "optional": true + }, + "node_modules/node-abi": { + "version": "3.98.0", + "resolved": "https://registry.npmjs.org/node-abi/-/node-abi-3.98.0.tgz", + "integrity": "sha512-Llh8noNw7s15m+rmIrZnZQbe4gLyasL9loT74rC92PkmwWKeEedKvc7o6pjp8B3MhrYT/Cl846NOYZbz1wmddw==", + "license": "MIT", + "optional": true, + "dependencies": { + "semver": "^7.3.5" + }, + "engines": { + "node": ">=10" + } + }, "node_modules/obug": { "version": "2.2.1", "resolved": "https://registry.npmjs.org/obug/-/obug-2.2.1.tgz", @@ -6554,6 +6582,34 @@ } } }, + "node_modules/prebuild-install": { + "version": "7.1.3", + "resolved": "https://registry.npmjs.org/prebuild-install/-/prebuild-install-7.1.3.tgz", + "integrity": "sha512-8Mf2cbV7x1cXPUILADGI3wuhfqWvtiLA1iclTDbFRZkgRQS0NqsPZphna9V+HyTEadheuPmjaJMsbzKQFOzLug==", + "deprecated": "No longer maintained. Please contact the author of the relevant native addon; alternatives are available.", + "license": "MIT", + "optional": true, + "dependencies": { + "detect-libc": "^2.0.0", + "expand-template": "^2.0.3", + "github-from-package": "0.0.0", + "minimist": "^1.2.3", + "mkdirp-classic": "^0.5.3", + "napi-build-utils": "^2.0.0", + "node-abi": "^3.3.0", + "pump": "^3.0.0", + "rc": "^1.2.7", + "simple-get": "^4.0.0", + "tar-fs": "^2.0.0", + "tunnel-agent": "^0.6.0" + }, + "bin": { + "prebuild-install": "bin.js" + }, + "engines": { + "node": ">=10" + } + }, "node_modules/process": { "version": "0.11.10", "resolved": "https://registry.npmjs.org/process/-/process-0.11.10.tgz", @@ -6590,6 +6646,17 @@ "url": "https://github.com/sponsors/wooorm" } }, + "node_modules/pump": { + "version": "3.0.4", + "resolved": "https://registry.npmjs.org/pump/-/pump-3.0.4.tgz", + "integrity": "sha512-VS7sjc6KR7e1ukRFhQSY5LM2uBWAUPiOPa/A3mkKmiMwSmRFUITt0xuj+/lesgnCv+dPIEYlkzrcyXgquIHMcA==", + "license": "MIT", + "optional": true, + "dependencies": { + "end-of-stream": "^1.1.0", + "once": "^1.3.1" + } + }, "node_modules/pure-rand": { "version": "8.4.2", "resolved": "https://registry.npmjs.org/pure-rand/-/pure-rand-8.4.2.tgz", @@ -6613,6 +6680,32 @@ "integrity": "sha512-tYC1Q1hgyRuHgloV/YXs2w15unPVh8qfu/qCTfhTYamaw7fyhumKa2yGpdSo87vY32rIclj+4fWYQXUMs9EHvg==", "license": "MIT" }, + "node_modules/rc": { + "version": "1.2.8", + "resolved": "https://registry.npmjs.org/rc/-/rc-1.2.8.tgz", + "integrity": "sha512-y3bGgqKj3QBdxLbLkomlohkvsA8gdAiUQlSBJnBhfn+BPxg4bc62d8TcBW15wavDfgexCgccckhcZvywyQYPOw==", + "license": "(BSD-2-Clause OR MIT OR Apache-2.0)", + "optional": true, + "dependencies": { + "deep-extend": "^0.6.0", + "ini": "~1.3.0", + "minimist": "^1.2.0", + "strip-json-comments": "~2.0.1" + }, + "bin": { + "rc": "cli.js" + } + }, + "node_modules/rc/node_modules/strip-json-comments": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/strip-json-comments/-/strip-json-comments-2.0.1.tgz", + "integrity": "sha512-4gB8na07fecVVkOI6Rs4e7T6NOTki5EmL7TUduTs6bu3EdnSycntVJ4re8kgZA+wx9IueI2Y11bfbgwtzuE0KQ==", + "license": "MIT", + "optional": true, + "engines": { + "node": ">=0.10.0" + } + }, "node_modules/readable-stream": { "version": "4.7.0", "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-4.7.0.tgz", @@ -7178,6 +7271,51 @@ "dev": true, "license": "MIT" }, + "node_modules/tar-fs": { + "version": "2.1.5", + "resolved": "https://registry.npmjs.org/tar-fs/-/tar-fs-2.1.5.tgz", + "integrity": "sha512-OboTd8mmMhZDNPV+UjQcK9yKAatXu2aJ+r1w4im1Otd4M4fl2hwvdoXUxIYHFTHWK/3y3FarBP70v3vwmGlOxw==", + "license": "MIT", + "optional": true, + "dependencies": { + "chownr": "^1.1.1", + "mkdirp-classic": "^0.5.2", + "pump": "^3.0.0", + "tar-stream": "^2.1.4" + } + }, + "node_modules/tar-stream": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/tar-stream/-/tar-stream-2.2.0.tgz", + "integrity": "sha512-ujeqbceABgwMZxEJnk2HDY2DlnUZ+9oEcb1KzTVfYHio0UE6dG71n60d8D2I4qNvleWrrXpmjpt7vZeF1LnMZQ==", + "license": "MIT", + "optional": true, + "dependencies": { + "bl": "^4.0.3", + "end-of-stream": "^1.4.1", + "fs-constants": "^1.0.0", + "inherits": "^2.0.3", + "readable-stream": "^3.1.1" + }, + "engines": { + "node": ">=6" + } + }, + "node_modules/tar-stream/node_modules/readable-stream": { + "version": "3.6.2", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-3.6.2.tgz", + "integrity": "sha512-9u/sniCrY3D5WdsERHzHE4G2YCXqoG5FTHUiCC4SIbr6XcLZBY05ya9EKjYek9O5xOAwjGq+1JdGBAS7Q9ScoA==", + "license": "MIT", + "optional": true, + "dependencies": { + "inherits": "^2.0.3", + "string_decoder": "^1.1.1", + "util-deprecate": "^1.0.1" + }, + "engines": { + "node": ">= 6" + } + }, "node_modules/thread-stream": { "version": "4.2.0", "resolved": "https://registry.npmjs.org/thread-stream/-/thread-stream-4.2.0.tgz", @@ -7286,6 +7424,19 @@ "license": "0BSD", "optional": true }, + "node_modules/tunnel-agent": { + "version": "0.6.0", + "resolved": "https://registry.npmjs.org/tunnel-agent/-/tunnel-agent-0.6.0.tgz", + "integrity": "sha512-McnNiV1l8RYeY8tBgEpuodCC1mLUdbSN+CYBL7kJsJNInOP8UjDDEwdk6Mw60vdLLrr5NHKZhMAOSrR2NZuQ+w==", + "license": "Apache-2.0", + "optional": true, + "dependencies": { + "safe-buffer": "^5.0.1" + }, + "engines": { + "node": "*" + } + }, "node_modules/turbo": { "version": "2.11.7", "resolved": "https://registry.npmjs.org/turbo/-/turbo-2.11.7.tgz", @@ -7458,6 +7609,13 @@ "url": "https://opencollective.com/unified" } }, + "node_modules/util-deprecate": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", + "integrity": "sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==", + "license": "MIT", + "optional": true + }, "node_modules/vfile": { "version": "6.0.3", "resolved": "https://registry.npmjs.org/vfile/-/vfile-6.0.3.tgz", diff --git a/package.json b/package.json index da611da56..0393f84c3 100644 --- a/package.json +++ b/package.json @@ -181,6 +181,8 @@ "verify:ghidra:cross-format": "npm run build:cached && node scripts/verify-real-ghidra.mjs --cross-format", "verify:ghidra:windows": "npm run build:cached && npm run build:ghidra-windows-fixture && node scripts/verify-real-ghidra-windows.mjs", "verify:browser": "npm run build:cached && node scripts/verify-real-browser.mjs", + "verify:frida": "npm run build:cached && node scripts/verify-frida.mjs", + "verify:frida:bindings": "tsc --noEmit -p tsconfig.frida-verification.json", "verify:browser:dom": "node scripts/verify-browser-dom-destinations.mjs", "verify:browser:network": "npm run build:cached && node scripts/verify-browser-network.mjs", "verify:electron": "npm run build:cached && node scripts/verify-real-electron.mjs", @@ -341,5 +343,11 @@ "typescript-termux": "npm:typescript@5.9.3", "vitepress": "1.6.4", "vitest": "5.0.3" + }, + "optionalDependencies": { + "frida": "17.23.2" + }, + "allowScripts": { + "frida@17.23.2": true } } diff --git a/scripts/lib/catalog-builders.mjs b/scripts/lib/catalog-builders.mjs index f9e0c0cef..787d6255d 100644 --- a/scripts/lib/catalog-builders.mjs +++ b/scripts/lib/catalog-builders.mjs @@ -49,6 +49,11 @@ export const toolFamilyCatalog = (sources) => { surface: "android-provider", contracts: sources.androidContracts.ANDROID_TOOL_CONTRACTS, }, + { + id: "frida", + surface: "instrumentation-provider", + contracts: sources.fridaContracts.FRIDA_TOOL_CONTRACTS, + }, { id: "apktool", surface: "apktool-provider", @@ -145,6 +150,10 @@ export const providerCatalog = (sources) => { activeContracts, } = electronContractSlices(sources); return [ + { + identity: sources.fridaProvider.FRIDA_PROVIDER_IDENTITY, + contracts: sources.fridaContracts.FRIDA_TOOL_CONTRACTS, + }, { identity: sources.hopperProvider.HOPPER_PROVIDER_IDENTITY, contracts: declaredContracts( diff --git a/scripts/lib/catalog-core.mjs b/scripts/lib/catalog-core.mjs index 56ffd544c..546b7359c 100644 --- a/scripts/lib/catalog-core.mjs +++ b/scripts/lib/catalog-core.mjs @@ -43,6 +43,8 @@ const SOURCE_PATHS = { sessionContracts: "dist/contracts/sessionToolContracts.js", binaryDiagnosticsContracts: "dist/contracts/native/binaryDiagnosticsToolContracts.js", + fridaContracts: "dist/contracts/fridaToolContracts.js", + fridaProvider: "dist/frida/FridaInstrumentationManager.js", pwntoolsProvider: "dist/native/pwntools/PwntoolsRelease.js", nativeContracts: "dist/contracts/native/nativeToolContracts.js", wasmContracts: "dist/contracts/wasm/wasmToolContracts.js", diff --git a/scripts/verify-frida.mjs b/scripts/verify-frida.mjs new file mode 100644 index 000000000..ece121a14 --- /dev/null +++ b/scripts/verify-frida.mjs @@ -0,0 +1,198 @@ +import { basename } from "node:path"; + +import { FridaInstrumentationService } from "../dist/application/frida/FridaInstrumentationService.js"; +import { FridaInstrumentationManager } from "../dist/frida/FridaInstrumentationManager.js"; +import { isValidFridaRemoteAddress } from "../dist/contracts/fridaRemoteAddress.js"; + +const options = parseArguments(process.argv.slice(2)); +if (options.help) { + process.stdout.write( + "Usage: npm run verify:frida [-- --address HOST:PORT --pid PID [remote auth flags]]\n" + + "Without flags, verifies a local Node.js process created by this script.\n", + ); + process.exit(0); +} + +const manager = new FridaInstrumentationManager(); +const service = new FridaInstrumentationService(manager); +const remote = + options.address === undefined + ? undefined + : { + address: options.address, + ...(options.token === undefined ? {} : { token: options.token }), + ...(options.certificate === undefined + ? {} + : { certificate: options.certificate }), + ...(options.origin === undefined ? {} : { origin: options.origin }), + ...(options.keepaliveInterval === undefined + ? {} + : { keepaliveInterval: options.keepaliveInterval }), + }; + +try { + let targetProcess; + let input; + if (remote === undefined) { + input = { + mode: "spawn", + deviceId: "local", + program: process.execPath, + argv: ["-e", "setTimeout(() => {}, 1500)"], + source: { + sourceKind: "inline", + source: "send({ rea_frida_verification: 'ok', pid: Process.id });", + }, + durationMs: 500, + }; + targetProcess = { pid: undefined, name: basename(process.execPath) }; + } else { + const inventory = await service.listProcesses({ remote }); + if (!inventory.ok) throw new Error(inventory.error.message); + targetProcess = inventory.value.processes.find( + ({ pid }) => pid === options.pid, + ); + if (targetProcess === undefined) + throw new Error( + `Selected PID ${String(options.pid)} was not observed remotely`, + ); + input = { + mode: "attach", + remote, + pid: options.pid, + source: { + sourceKind: "inline", + source: "send({ rea_frida_verification: 'ok', pid: Process.id });", + }, + durationMs: 500, + }; + } + + const result = await service.instrument(input); + if (!result.ok) throw new Error(result.error.message); + const observed = result.value.evidence.normalized_result; + if ( + typeof observed !== "object" || + observed === null || + Array.isArray(observed) || + !Array.isArray(observed.messages) + ) + throw new Error("Frida returned no message observation"); + const messageObserved = observed.messages.some( + (message) => + typeof message === "object" && + message !== null && + !Array.isArray(message) && + message.type === "send" && + typeof message.payload === "object" && + message.payload !== null && + !Array.isArray(message.payload) && + message.payload.rea_frida_verification === "ok" && + message.payload.pid === observed.pid, + ); + if (!messageObserved) + throw new Error("Selected target did not return the Frida marker"); + const report = { + ok: result.value.cleanupError === null, + provider: "frida", + host_platform: process.platform, + target_mode: remote === undefined ? "local_spawn" : "remote_attach", + target_pid: observed.pid, + target_name: targetProcess.name, + ...(remote === undefined ? {} : { remote_target_observed: true }), + script_message_observed: true, + cleanup_error: result.value.cleanupError, + }; + process.stdout.write(`${JSON.stringify(report)}\n`); + if (!report.ok) process.exitCode = 1; +} catch (cause) { + process.stderr.write( + `Frida real-provider verification failed: ${cause instanceof Error ? cause.message : String(cause)}\n`, + ); + process.exitCode = 1; +} finally { + await manager.closeAll().catch(() => undefined); +} + +function parseArguments(args) { + if (args.length === 1 && args[0] === "--help") return { help: true }; + const values = parseOptionValues(args); + const address = values.get("--address"); + const pidValue = values.get("--pid"); + const pid = parseRemotePid(address, pidValue); + if (address === undefined) assertNoRemoteOptions(values); + const keepaliveInterval = parseKeepalive(values.get("--keepalive-interval")); + return { + help: false, + ...(address === undefined ? {} : { address }), + ...(pid === undefined ? {} : { pid }), + ...(values.has("--token") ? { token: values.get("--token") } : {}), + ...(values.has("--certificate") + ? { certificate: values.get("--certificate") } + : {}), + ...(values.has("--origin") ? { origin: values.get("--origin") } : {}), + ...(keepaliveInterval === undefined ? {} : { keepaliveInterval }), + }; +} + +function parseOptionValues(args) { + const values = new Map(); + const valueFlags = new Set([ + "--address", + "--pid", + "--token", + "--certificate", + "--origin", + "--keepalive-interval", + ]); + for (let index = 0; index < args.length; index += 1) { + const flag = args[index]; + const value = args[index + 1]; + if ( + typeof flag !== "string" || + !valueFlags.has(flag) || + value === undefined || + values.has(flag) + ) + throw new Error( + "Expected no arguments for local-fixture verification, or --address HOST:PORT --pid PID and optional remote auth flags", + ); + values.set(flag, value); + index += 1; + } + return values; +} + +function parseRemotePid(address, pidValue) { + if ((address === undefined) !== (pidValue === undefined)) + throw new Error("Remote verification requires both --address and --pid"); + if (address !== undefined && !isValidFridaRemoteAddress(address)) + throw new Error( + "--address must be a host[:port] without credentials, path, query, or fragment", + ); + const pid = pidValue === undefined ? undefined : Number(pidValue); + if (pid !== undefined && (!Number.isSafeInteger(pid) || pid <= 0)) + throw new Error("--pid must be a positive process identifier"); + return pid; +} + +function assertNoRemoteOptions(values) { + for (const flag of [ + "--token", + "--certificate", + "--origin", + "--keepalive-interval", + ]) + if (values.has(flag)) + throw new Error("Remote connection options require --address and --pid"); +} + +function parseKeepalive(value) { + const interval = value === undefined ? undefined : Number(value); + if ( + interval !== undefined && + (!Number.isSafeInteger(interval) || interval <= 0) + ) + throw new Error("--keepalive-interval must be a positive integer"); + return interval; +} diff --git a/scripts/verify/fridaBindingsCompatibility.ts b/scripts/verify/fridaBindingsCompatibility.ts new file mode 100644 index 000000000..cb156efb6 --- /dev/null +++ b/scripts/verify/fridaBindingsCompatibility.ts @@ -0,0 +1,28 @@ +import type { + Device as VendorDevice, + DeviceManager as VendorDeviceManager, + Script as VendorScript, + Session as VendorSession, +} from "../../node_modules/frida/build/src/frida_binding.js"; +import type { Device, DeviceManager, Script, Session } from "frida"; + +type Assert = Condition; + +type VendorDeviceManagerFitsShim = Assert< + VendorDeviceManager extends DeviceManager ? true : false +>; +type VendorDeviceFitsShim = Assert; +type VendorSessionFitsShim = Assert< + VendorSession extends Session ? true : false +>; +type VendorScriptFitsShim = Assert; + +// These aliases intentionally fail when the pinned Frida API narrows incompatibly. +const compatibility: readonly [ + VendorDeviceManagerFitsShim, + VendorDeviceFitsShim, + VendorSessionFitsShim, + VendorScriptFitsShim, +] = [true, true, true, true]; + +void compatibility; diff --git a/src/application/CapabilityInventory.ts b/src/application/CapabilityInventory.ts index 7c9d3910c..76c5cafbb 100644 --- a/src/application/CapabilityInventory.ts +++ b/src/application/CapabilityInventory.ts @@ -22,6 +22,7 @@ import type { JsonValue } from "../domain/jsonValue.js"; import { isMachOFormat } from "../domain/binaryTargetTypes.js"; import type { ProviderAvailability } from "./AnalysisProvider.js"; import { TOOL_CONTRACTS } from "../contracts/toolContracts.js"; +import { FRIDA_TOOL_CONTRACTS } from "../contracts/fridaToolContracts.js"; import { clientRequirementsFor, NO_CLIENT_FEATURES, @@ -131,6 +132,9 @@ const ACTIVE_TARGET_KINDS: ReadonlySet = new Set([ "native-provider", "artifact-provider", ]); +const FRIDA_OPERATION_NAMES: ReadonlySet = new Set( + FRIDA_TOOL_CONTRACTS.map(({ name }) => name), +); /** Native contracts defined only for an active Mach-O executable. */ const MACHO_TARGET_OPERATIONS: ReadonlySet = new Set([ @@ -316,6 +320,7 @@ const optionalProviderFailureFor = ({ if (kind === "electron-provider") return failures?.electronObservation; if (kind === "runtime-provider") return failures?.javascriptRuntimeObservation; + if (FRIDA_OPERATION_NAMES.has(name)) return failures?.fridaInstrumentation; return undefined; }; @@ -533,6 +538,7 @@ const targetAvailability = ({ targetFormat, targetOpen, }: AvailabilityContext): Availability | null => { + if (FRIDA_OPERATION_NAMES.has(name)) return null; if (!targetOpen && ACTIVE_TARGET_KINDS.has(kind)) return { reason: "target_required", diff --git a/src/application/OptionalObservationProviders.ts b/src/application/OptionalObservationProviders.ts index d66554966..499925a7c 100644 --- a/src/application/OptionalObservationProviders.ts +++ b/src/application/OptionalObservationProviders.ts @@ -22,7 +22,10 @@ export interface OptionalProviderLoadFailure { export type OptionalProviderLoadFailures = Readonly< Partial< - Record + Record< + keyof OptionalObservationProviders | "fridaInstrumentation", + OptionalProviderLoadFailure + > > >; diff --git a/src/application/frida/FridaInstrumentationManager.test.ts b/src/application/frida/FridaInstrumentationManager.test.ts new file mode 100644 index 000000000..8c315c0d5 --- /dev/null +++ b/src/application/frida/FridaInstrumentationManager.test.ts @@ -0,0 +1,988 @@ +import { describe, expect, it } from "vitest"; + +import type { + Device, + DeviceManager, + FridaMessage, + Process, + Script, + Session, + Signal, +} from "frida"; +import { FridaInstrumentationManager } from "../../frida/FridaInstrumentationManager.js"; + +const signal = void>() => { + let connected: Handler | undefined; + const value: Signal & { + emit: (...args: Parameters) => void; + } = { + connect: (handler) => { + connected = handler; + }, + disconnect: (handler) => { + if (connected === handler) connected = undefined; + }, + emit: (...args) => connected?.(...args), + }; + return value; +}; + +class FakeScript implements Script { + readonly message = + signal<(message: FridaMessage, data: Buffer | null) => void>(); + isDestroyed = false; + loadFailure: Error | undefined; + unloadFailureCount = 0; + unloadCalls = 0; + + constructor( + readonly payload: unknown = { event: "loaded" }, + readonly beforeLoad: () => Promise = async () => {}, + readonly description = "script loaded", + readonly stack = "", + ) {} + + async load(): Promise { + await this.beforeLoad(); + if (this.loadFailure !== undefined) throw this.loadFailure; + this.message.emit( + { + type: "send", + payload: this.payload, + description: this.description, + stack: this.stack, + }, + null, + ); + } + + async unload(): Promise { + this.unloadCalls += 1; + if (this.unloadFailureCount > 0) { + this.unloadFailureCount -= 1; + throw new Error("script unload failed"); + } + if (this.isDestroyed) throw new Error("Script is destroyed"); + this.isDestroyed = true; + } +} + +class FakeSession implements Session { + readonly detached = signal<() => void>(); + readonly scripts: FakeScript[] = []; + nextPayload: unknown = { event: "loaded" }; + nextDescription = "script loaded"; + nextStack = ""; + nextLoadFailure: Error | undefined; + nextUnloadFailureCount = 0; + detachFailureCount = 0; + scriptLoadGate: Promise | undefined; + onScriptLoadStarted: (() => void) | undefined; + private detachedState = false; + readonly pid = 512; + + isDetached(): boolean { + return this.detachedState; + } + + async createScript(): Promise