diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d27d2b24cd..a7b982f530 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -330,11 +330,14 @@ jobs: # every shard's diagnostics and coverage. Passing PRs run the full suite. fail-fast: ${{ github.event_name == 'pull_request' && needs.changes.outputs.tier == 'fast' }} matrix: - # Fast feedback uses one source group and eight built groups so the - # slowest built shard job stays inside a ten-minute execution budget. - # Classification, build, and report merge sit outside that job. - # Full qualification retains six complete shards with coverage. - shard: ${{ fromJSON(needs.changes.outputs.tier == 'fast' && '["1/8","2/8","3/8","4/8","5/8","6/8","7/8","8/8"]' || '["1/6","2/6","3/6","4/6","5/6","6/6"]') }} + # Fast feedback uses one source group and twelve built groups. Shards + # are balanced by recorded duration, but hosted runner speed varies, + # so the slowest shard sets the critical path: at eight shards it ran + # 24% above the mean shard. Smaller shards bound that excess at a few + # percent more runner time. Classification, build, and report merge + # sit outside these jobs. Full qualification retains six complete + # shards with coverage. + shard: ${{ fromJSON(needs.changes.outputs.tier == 'fast' && '["1/12","2/12","3/12","4/12","5/12","6/12","7/12","8/12","9/12","10/12","11/12","12/12"]' || '["1/6","2/6","3/6","4/6","5/6","6/6"]') }} steps: - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0 - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0 @@ -755,7 +758,9 @@ jobs: needs: [changes, static, build] if: needs.changes.outputs.apple == 'true' runs-on: macos-15 - timeout-minutes: 15 + # Passing runs take 10-15 minutes on macOS runners of varying speed, so a + # 15-minute budget cancelled about a third of runs. Keep a hang bounded. + timeout-minutes: 25 steps: - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0 - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0 diff --git a/bridge/ghidra/ReaGhidraBridge.java b/bridge/ghidra/ReaGhidraBridge.java index 82d75c5cc2..12329c1a5d 100644 --- a/bridge/ghidra/ReaGhidraBridge.java +++ b/bridge/ghidra/ReaGhidraBridge.java @@ -77,6 +77,8 @@ import ghidra.program.model.data.StringDataInstance; import ghidra.program.model.data.DataType; import ghidra.program.model.lang.Register; import ghidra.program.model.scalar.Scalar; +import ghidra.program.model.listing.Bookmark; +import ghidra.program.model.listing.BookmarkType; import ghidra.program.model.listing.CommentType; import ghidra.program.model.listing.Data; import ghidra.program.model.listing.DataIterator; @@ -3987,6 +3989,7 @@ public final class ReaGhidraBridge extends HeadlessScript { private final class FunctionBoundaryLimitations { private final Function function; private final List limitations = new ArrayList<>(); + private boolean inferredNoReturnCall; private Instruction beforePrevious; private Instruction previous; @@ -3996,8 +3999,11 @@ public final class ReaGhidraBridge extends HeadlessScript { private void observe(Instruction instruction) throws Exception { monitor.checkCancelled(); - String limitation = terminalCallLimitation(instruction); - if (limitation != null) limitations.add(limitation); + TerminalCall terminal = terminalCall(instruction); + if (terminal != null) { + limitations.add(terminal.limitation()); + if (terminal.inferredNoReturn()) inferredNoReturnCall = true; + } if (beforePrevious != null && previous != null) { String seh = borlandSehHandlerLimitation( function, beforePrevious, previous, instruction); @@ -4008,10 +4014,42 @@ public final class ReaGhidraBridge extends HeadlessScript { } private List values() { - return List.copyOf(limitations); + if (!inferredNoReturnCall) return List.copyOf(limitations); + List result = new ArrayList<>(); + // One consequence and recovery per function, ahead of the call-specific facts so a + // bounded summary that admits limitations in order keeps it. The text names no + // address, so those summaries also deduplicate it across functions. + result.add("A terminal call in this function targets a callee that " + + "Ghidra's Non-Returning Functions - Discovered analyzer inferred cannot return, so Ghidra " + + "excluded that call's fall-through from the analyzed body and from the pseudocode and facts " + + "derived from it. If the callee returns, code reachable only through that fall-through is " + + "missing from this result. To compare, analyze a fresh import with " + + "REA_GHIDRA_DISABLED_ANALYZERS='Non-Returning Functions - Discovered'."); + result.addAll(limitations); + return result; } } + /** A terminal call's limitation and whether its no-return flag was analyzer-inferred. */ + private record TerminalCall(String limitation, boolean inferredNoReturn) {} + + /** + * Ghidra's Non-Returning Functions - Discovered analyzer bookmarks each entry it marks with + * this Analysis comment; the name-list Known analyzer uses "Non-Returning Function Identified". + * A missing bookmark leaves the flag's origin unknown, never declared. + */ + private boolean discoveredNoReturn(Function target) { + Function thunked = target.isThunk() ? target.getThunkedFunction(true) : null; + return hasDiscoveredNoReturnBookmark(target) || + (thunked != null && hasDiscoveredNoReturnBookmark(thunked)); + } + + private boolean hasDiscoveredNoReturnBookmark(Function function) { + Bookmark bookmark = currentProgram.getBookmarkManager().getBookmark( + function.getEntryPoint(), BookmarkType.ANALYSIS, "Non-Returning Function"); + return bookmark != null && "Non-Returning Function Found".equals(bookmark.getComment()); + } + private String borlandSehHandlerLimitation( Function function, Instruction handlerPush, Instruction framePush, Instruction frameLink) { @@ -4104,9 +4142,10 @@ public final class ReaGhidraBridge extends HeadlessScript { } } - private String terminalCallLimitation(Instruction instruction) { + private TerminalCall terminalCall(Instruction instruction) { if (!instruction.getFlowType().isCall() || !instruction.getFlowType().isTerminal()) return null; + boolean inferred = false; Address[] destinations = instruction.getFlows(); StringBuilder limitation = new StringBuilder(); limitation.append("Ghidra Listing reports a terminal call at ") @@ -4135,14 +4174,18 @@ public final class ReaGhidraBridge extends HeadlessScript { Function target = destinations.length == 1 ? currentProgram.getFunctionManager().getFunctionAt(destinations[0]) : null; - if (target != null && target.hasNoReturn()) { + if (target != null && target.hasNoReturn() && discoveredNoReturn(target)) { + inferred = true; + limitation.append(" Ghidra's Non-Returning Functions - Discovered analyzer bookmarked this callee as a non-returning function it found; the flag is an analysis inference, not an independent verification that the callee cannot return."); + } + else if (target != null && target.hasNoReturn()) { limitation.append(" This records Ghidra's FunctionManager flag, not an independent verification that the callee cannot return."); } else { limitation.append(" The reason for the terminal flow is not established by this observation."); } } - return limitation.toString(); + return new TerminalCall(limitation.toString(), inferred); } private static String renderAssembly(List instructions) { diff --git a/docs/development/testing.md b/docs/development/testing.md index c473e01c33..e7ead9ae85 100644 --- a/docs/development/testing.md +++ b/docs/development/testing.md @@ -238,7 +238,9 @@ broad changes or diagnosing CI, rather than after every edit. Package/install changes additionally need package verification; provider changes need actual provider evidence. -Vitest projects use up to two workers, bounded by host parallelism. +Vitest projects use up to two workers, bounded by host parallelism. GitHub +Actions jobs own their runner and use up to four; see +[test deadlines](#developer-commands) for how deadlines scale there. `process-boundary` runs later with serial files because process-tree sampling shares host resources. Acceptance and process-global cases use isolated forks without serial scheduling. Pure domain/service, composition, conformance, @@ -283,7 +285,7 @@ coverage. Ordinary CI also exhausted the former 384 MiB budget after earlier files loaded the shared source/contract graph, while later command registration and schema/help rendering still needed transient allocation space. This is a measured capacity control, not a suite-wide speedup claim. The worker count -remains bounded at two, and spawned REA processes retain their own heap settings. +remains bounded, and spawned REA processes retain their own heap settings. Coverage enablement still controls instrumentation, test deadlines and coverage thresholds; explicitly disabling it or selecting only a provider does not enable instrumentation. @@ -299,6 +301,21 @@ work from fixture generation, schema compilation, subprocess startup and cleanup Choose elapsed, CPU and process-family memory limits before the run; a worker count or test timeout alone does not enforce those limits. +A test deadline turns a hang into a named failure; it is not a performance +assertion. `vitest.config.ts` applies the deadlines in +`tests/support/testDeadlines.ts` per project: 30 seconds for source projects +and 120 seconds for built projects, with hooks sharing their project's +deadline. Coverage doubles every deadline, and so does hosted CI, where +CPU-bound cases share every core with other workers and their subprocesses. +Do not pass a per-test timeout to fit a slow host. A child run through a +test process registry (`processTest`, `cli.run`, or `createTestProcessRegistry`) +gets the running test's remaining deadline less a reporting reserve, so a hung +child fails with its captured output. Pass `timeoutMs` only when the child's deadline is itself under test. Tag a bounded workload that needs longer with +`{ tags: ["long-running"] }`, or a whole file with +`// @module-tag long-running`. Assert elapsed time explicitly when speed is the +behavior under test. Derive an explicit deadline only when a test must outlive +an operation deadline whose diagnostics it checks. + Keep capacity fixtures intact when diagnosing a test failure. For byte equality of large buffers, use `Buffer.equals` or `Buffer.compare`; a generic structural diff can allocate far more than the production operation. @@ -343,11 +360,17 @@ The process restarts when the environment changes, after 32 commands, or when the worker reports a resident set of at least 1 GiB. A fatal worker failure or a broken session frame retires that process and waits for it to exit before the next command starts. -`cli.run` still starts a fresh process, including stdin and explicit deadlines. -`runFileCli` uses the file session for commands that have neither. Go binary, -IL2CPP metadata, and dylib-resolution commands use that session. File teardown -stops the shared CLI process, including startup still in flight, and does not -open another after close. +In a `cliTest` file, `cli.run` uses that shared process when it reports the +command exactly as a fresh `rea` process would. Both run the same one-shot +owner (`src/cliOneShot.ts`), and the shared process presents the launcher's +entrypoint. A command starts a fresh process instead when it supplies stdin or +an explicit deadline, enters MCP or the MCP doctor (which the launcher routes +first), sets `NODE_OPTIONS`, or enables logging, which writes outside the +per-command capture. Pass `freshProcess: true` for process-level facts such as +startup, signals, or exit, and keep one fresh check beside reused cases where +dispatch itself is under test. `createTestCli` drivers always start a fresh +process. File teardown stops the shared CLI process, including startup still in +flight, and does not open another after close. `exportedFunctionBindingsCli.test.ts` uses the shared compiled CLI session for its file and compares its JSON command results with the MCP session. It selects 20-second startup and command deadlines; other callers retain their existing @@ -1000,8 +1023,8 @@ partial diagnostics and preflights do not establish complete mutation outcomes or qualify the release revision for mutation coverage. Fast CI starts the source-only projects after classification, independently of -build and static checks, and runs eight built-dependent shards after the build. -Those nine test jobs still select every portable test. Full qualification keeps six +build and static checks, and runs twelve built-dependent shards after the build. +Those thirteen test jobs still select every portable test. Full qualification keeps six complete Vitest shards with coverage. Neither tier retries tests. Canonical project ownership lives in `scripts/lib/test-projects.mjs`, shared by Vitest, CI group execution, and development-test preparation. A test importing compiled @@ -1077,32 +1100,12 @@ existing four-minute timing collection plus the bounded diagnostic stages. Fast built shards target under ten minutes of uncontended shard-job execution. That figure is the `test-shard` job. The job waits for classification and the -build, and the portable-test merge runs after the slowest shard. On 2026-10-20, -run `39451548945` measured those surrounding jobs with runner queue excluded: -classification 15 seconds, build 49 seconds, and the portable-test merge 29 -seconds. Shard 1/5 of that run spent 22 seconds before built Vitest and 3 -seconds after it, including `npm ci` at 9 seconds. `node_modules` is about 674 -MiB, so publishing it as a shared artifact would transfer more data than that -install saves. The npm download cache stays the setup cache. - -File timings from that run put the built suite at 7944 seconds of module time. -The sequencer keeps each file's full duration on one worker. Extra workers -overlap other files in the same sequence group, and `process-boundary` runs -after that group. Scheduling the unsplit inventory onto five shards peaks at -820 seconds of tests. That run's slowest built shard job lasted 925 seconds -(shard 4/5), and shard 1/5 spent 874 seconds in built Vitest, so hosted job -time sits above the file schedule. Six shards of the same inventory peak at -684 seconds of tests. Eight shards, after the 578-second loader file is split -by case count (54 cases stay in that file and 46 move into each of three -files), peak at 513 seconds of tests. With the measured 25-second shard setup, -that shard job is about 538 seconds. Adding the measured classification, build, -and merge makes an uncontended portable critical path of about 631 seconds, -still before the gap between this file schedule and hosted Vitest. Runner queue -is separate and grows when more shard jobs compete for runners. +build, and the portable-test merge runs after the slowest shard. Runner queue is +separate and grows when more shard jobs compete for runners. The npm download +cache stays the setup cache rather than a shared `node_modules` artifact. `process-boundary` stays serial because process-tree sampling shares the host. -Raising the worker cap from two to four on this four-core host cut a six-file -acceptance sample from 96–101 seconds to 71 seconds and did not remove the -single-file floor, so the cap stays two. Full qualification keeps six covered +Hosted CI jobs use four workers; other hosts stay at two +because they are shared with other checks. Full qualification keeps six covered shards; its longer instrumentation is outside this fast budget. The separate `CI timings` workflow batches recent completed CI and Release runs diff --git a/docs/getting-started/cli.md b/docs/getting-started/cli.md index e493ed5ee7..730dccc211 100644 --- a/docs/getting-started/cli.md +++ b/docs/getting-started/cli.md @@ -1339,7 +1339,15 @@ bytes as text, omitting a final UTF-8 code point if it crosses that byte limit. `stderr_bytes` counts all observed bytes, including omitted output; `stderr_truncated` indicates that output exceeded the retention limit. The same diagnostics appear inside the report when `--full-output` is selected. Log -truncation alone does not change the diagnostic health or exit status. +truncation alone does not change the diagnostic health or exit status. More than +32 MiB of combined child stdout and stderr stops the child and fails the check +in progress. + +Doctor owns the server's process group on POSIX hosts, or its Job Object on +Windows. Before reporting, it stops that process family, including POSIX +descendants in other process groups that inherited its run token. If that +cleanup cannot be confirmed, the report keeps its earlier checks and adds a +failed `cleanup` check with the reason and retained run identifier. The default terminal format is TOON. Use `--json` when saving results for a JSON consumer. Valued output flags (`--format`, `--filter-output`, diff --git a/docs/getting-started/client-configuration.md b/docs/getting-started/client-configuration.md index 9e2a4b707c..8761dd2d84 100644 --- a/docs/getting-started/client-configuration.md +++ b/docs/getting-started/client-configuration.md @@ -515,7 +515,11 @@ Setup installs its selected skill copy there. Gemini CLI uses `$GEMINI_CLI_HOME/.gemini/settings.json` when that home override is nonempty, including its shared personal skills at -`$GEMINI_CLI_HOME/.agents/skills`. Setup and doctor check MCP allow/exclude +`$GEMINI_CLI_HOME/.agents/skills`. Automatic detection uses Gemini CLI's own +settings, trusted-folder records or `.gemini/tmp` session state; the `.gemini` +directory alone does not identify it, because Antigravity keeps its +configuration there too. Select `--client gemini_cli` explicitly before those +files exist. Setup and doctor check MCP allow/exclude policies from system defaults, user settings, trusted workspace settings, and system overrides. Workspace trust follows `GEMINI_CLI_TRUST_WORKSPACE`, `GEMINI_RESTRICTED_MODE`, the effective `security.folderTrust.enabled` setting, diff --git a/docs/guides/native/native-investigation.md b/docs/guides/native/native-investigation.md index e129ac089b..f44c98cf10 100644 --- a/docs/guides/native/native-investigation.md +++ b/docs/guides/native/native-investigation.md @@ -321,7 +321,12 @@ the others. A function that `procedure_info` classifies as external, such as an import, is counted as `external` and recorded with that status and the body fact as observed. It gets no file, no pseudocode is requested, and a resume skips it. A function whose classification is unknown is decompiled as usual; -a provider that returns null pseudocode for it is reported as `no_pseudocode`. Inventory rows without an address are counted in the limitations. `--layout dwarf` +a provider that returns null pseudocode for it is reported as `no_pseudocode`. Inventory rows without an address are counted in the limitations. +The top-level `limitations` of the manifest and index count every retained +non-external function record whose body is unavailable, including records an +earlier run wrote, and repeat the reason when all of them share one. A resume +that skips every file therefore keeps the caveat until a later attempt records +a known body. `--layout dwarf` reports that compilation-unit paths are unavailable and still names files by address. Filters are a case-sensitive name substring, a case-sensitive name prefix, an inclusive address range (including uppercase hexadecimal input), and a positive safe-integer attempt limit. The limit does not allocate space in proportion to its value. Cancellation leaves diff --git a/docs/guides/runtime/process-capture.md b/docs/guides/runtime/process-capture.md index 3f2f0b7207..56c3ab00ff 100644 --- a/docs/guides/runtime/process-capture.md +++ b/docs/guides/runtime/process-capture.md @@ -639,6 +639,31 @@ current token proof is still required before signaling. Swept cleanup verifies t remaining family after its group signals. Coverage is bounded by the native snapshots and generations inspected; sampled group IDs select candidates. +Settlement records the observation before final owned-process cleanup. Even a +quiesced sampled group can have token-owned descendants discovered during that +cleanup. If the ownership layer reports that cleanup sent termination signals, +`settlement.cleanup_outcome` is `cleaned` on success, and process residual +unknowns explain that the intervention may have ended further terminal or +filesystem work. Failed resource cleanup remains `failed` and retains the same +intervention fact in partial observations. This does not establish how an +uncaptured run would have continued or change which processes REA may signal. + +An observed signal is retained even if a later group signal or final ownership +verification fails. It does not establish complete cleanup: the failed resource, +original diagnostic, and retry authority remain visible. A successful retry can +clear the live cleanup status without rewriting the earlier partial observations. + +If capture completion fails before a capture exists, the intervention is retained +in `partial_observation.observations.residual_unknowns`, alongside the available +observations and explicit unavailable fields. Older records may omit this optional +array. No intervention entry is added without an observed signaling fact; absence +does not prove that cleanup sent no signals. + +Readers that previously accepted only `not_required` for a quiesced settlement +must refresh the advertised MCP schema or update their saved-capture reader to +accept `cleaned` as well. Existing saved captures remain valid and keep their +recorded outcome; missing historical signal facts are not reconstructed. + When the host withholds an unrelated process's ownership token, REA leaves that process untouched and records its PID and reason in `cleanup.unverified_processes` and process residual unknowns. Successful cleanup verifies the owned group; diff --git a/docs/providers/ghidra-provider.md b/docs/providers/ghidra-provider.md index 069afe2bd9..0eb12989e0 100644 --- a/docs/providers/ghidra-provider.md +++ b/docs/providers/ghidra-provider.md @@ -338,7 +338,18 @@ handler execution or change the body. Instruction windows and dossiers also retain these warnings in their result limitations. An unresolved callee or unestablished terminal-flow basis remains explicit. These observations qualify the analysis; they do not prove that a no-return flag is incorrect or recover -excluded code. Analysis profiles version this observation contract so older +excluded code. When the callee's entry, or the function a thunk forwards to, +carries the `Non-Returning Function Found` analysis bookmark that Ghidra's +`Non-Returning Functions - Discovered` analyzer writes, the call's limitation +says the flag was inferred, and the function's limitations add one statement +that Ghidra excluded that call's fall-through from the body and pseudocode, so +code reachable only through it is missing if the callee returns. That analyzer can mark returning callees, such as an +imported `operator new` thunk, so a constructor may decompile to a few lines. To +compare, open a fresh import with +`REA_GHIDRA_DISABLED_ANALYZERS='Non-Returning Functions - Discovered'`; an +existing project (`REA_GHIDRA_PROJECT_PATH`) keeps the flags its analysis +already stored. A flag without that bookmark keeps an unknown origin; REA does +not treat it as declared. Analysis profiles version this observation contract so older snapshots cannot replay unqualified results. On non-Windows hosts, the ephemeral import repair clears no-return flags on the diff --git a/docs/reference/mcp-contracts.md b/docs/reference/mcp-contracts.md index 221991344b..19f87b0db4 100644 --- a/docs/reference/mcp-contracts.md +++ b/docs/reference/mcp-contracts.md @@ -1065,6 +1065,17 @@ from an older request does not override a newer outcome. Request deadlines and cancellations neither mark nor clear availability. A new provider client starts without these outcomes. +A `provider_missing` group may also list `alternatives`: advertised tools the +current provider makes available for part of that operation's question. When +the provider does not declare `next_address` or `prev_address`, they list +whichever of `inspect_native_instruction`, `read_function_instructions` and +`procedure_assembly` qualify. These answer instruction-level questions only: +none navigates arbitrary analyzed objects such as data or addresses outside a +procedure, and neither listing order nor address plus length proves executed +control flow. Calling the missing operation returns `capability_unavailable` +with the same tools in `details.alternatives` and a remediation stating each +one's scope. Without a qualifying alternative, both keep their generic advice. + Standalone command operations (such as `inspect_with_objdump` and `execute_rizin_command`) similarly reflect observed executable launch failures: an unprobed executable is reported available; once a launch failure is observed, diff --git a/docs/reverse-engineering-tools.md b/docs/reverse-engineering-tools.md index 50f317848c..2fa2441ea3 100644 --- a/docs/reverse-engineering-tools.md +++ b/docs/reverse-engineering-tools.md @@ -96,6 +96,10 @@ Persistent command Evidence retains the caller-selected and resolved startup pat Session status retains up to 64 recent protocol frames. Each history entry keeps a complete-character UTF-8 prefix of at most 16 KiB, plus a truncation marker when needed; `recent_output_truncated` reports omitted history. This history budget also accounts for replacement characters from invalid UTF-8. It does not shorten command Evidence, whose output budget defaults to 16 MiB. Rizin debug startup waits 30000 ms with no runtime override. Each command frame defaults to 120000 ms; its deadline, command-output budget, session capacity and pending-command queue use the [shared debugger launch settings](#debugger-launch-settings). The 16777216-byte frame cap and 4096-frame command cap remain fixed. Evidence names the cap that stopped the command. The one-shot `command_timeout_ms` setting does not change a debug session. +### Stopped debugger sessions + +A Rizin or GDB session stops accepting commands when REA stops it, for example after an output or frame limit, a command deadline, cancellation or a protocol failure, or when the debugger process exits. REA keeps the stopped session and the first reason it observed until `close_rizin_debug_session` or `close_gdb_session` releases it, including after a retried cleanup succeeds. `rizin_debug_session_status` and `gdb_session_status` then report `state: "closed"` with that reason in `closed_reason`. A later `rizin_debug_command` or `gdb_console` on the session fails with `capability_unavailable`; `details.reason` carries the same reason, and the remediation is to close the session and start a new one. A `session_id` that this server never returned, or that close already released, fails with `invalid_request` instead. + ### Debugger launch settings GDB and Rizin share these settings in the environment that launches REA: @@ -136,6 +140,8 @@ rea debug-with-rizin --inferior-tty /dev/pts/7 ./program dr rea execute-rizin-command ./program iI ``` +Rizin exits with status 0 even when its shell refuses a statement, so the exit code does not show whether the command ran. Rizin 0.6 through 0.9.1 log one of four stderr lines for such a refusal: `ERROR: Command '…' does not exist.`, ``ERROR: Wrong number of arguments passed to `…`…``, `ERROR: core: Error while executing command: …` (an invalid statement), or `ERROR: core: Error while parsing command: …`. Rizin then skips the refused statement and every later one in the command. When the completed process's stderr contains one of these lines, REA reports `completion_status: "rejected"` and adds a limitation naming the refusal. stdout then holds only the output of earlier statements, so empty stdout is not an empty result. Other `ERROR:` lines, such as `ERROR: Cannot find function at …`, come from commands that ran; REA keeps them in stderr and reports `complete`. A command that lowers `log.level` or redirects stderr hides refusals from REA. + Captured stdout and stderr use the same `max_output_bytes` (CLI: `--max-output-bytes`) admission and recovery described above, default 16 MiB. One-shot commands independently use `command_timeout_ms`. Raising output admission cannot recover an earlier capture; command effects and cleanup must be checked before another invocation. ### Decoder and map inputs diff --git a/docs/testing-lanes.md b/docs/testing-lanes.md index a3db828239..a7846af0ec 100644 --- a/docs/testing-lanes.md +++ b/docs/testing-lanes.md @@ -842,30 +842,16 @@ branches are not producers. A tier with no qualifying artifact leaves history unset, and Vitest uses its own assignment. A later schedule that reuses qualification without uploading timings leaves the previous artifact in place. -On 2026-10-21 the newest full history was run -`39545047294`, recorded before compiled-CLI session reuse. Fast run -`39552964873` packed its eight built shards from that history, and those shard -jobs executed for 319–591 seconds. Replaying that run's file durations through -the sequencer's two-worker schedule balances every shard at about 404 seconds -of file time. The same replay with the stale history weights leaves the slowest -shard near 547 seconds and the fastest near 293. That spread is file-schedule -time, not runner queue, which is why each tier keeps its own history. -CI assigns discovered files by historical duration, with a deterministic -fallback weight for new files, rather than dividing only by file count. -Each file keeps its full duration on one worker. Extra workers overlap other -files in the same sequence group, and `process-boundary` runs after that -group. History published before the competing-loader split still names only -`javascriptLiteralPercentPaths.test.ts`. Until an artifact records -`javascriptLiteralLoaderOwnership.test.ts`, the sequencer assigns that -recorded duration by the pre-split case counts: 54/192 stays with the -percent-path file and 138/192 goes to the consolidated loader file. History -that names the retired Early, Middle, and Late shards sums those durations -onto the consolidated file instead, and scales a partial set as three equal -shares. That weight is the old dual-surface cost. The demoted file runs every -loader once through the shared stdio MCP session, so the first schedule -overestimates it. Every discovered file still belongs to exactly one shard; +Each tier keeps its own history because stale weights from another tier, or +from before a large test change, unbalance the shards. +CI assigns discovered files by historical duration rather than dividing only +by file count. A file with no recorded duration, such as a newly added or +renamed test, is weighted at the median recorded duration until a published +artifact records it. Each file keeps its full duration on one worker. Extra +workers overlap other files in the same sequence group, and `process-boundary` +runs after that group. Every discovered file still belongs to exactly one shard; worker limits and project ordering remain unchanged. Fast qualification keeps -eight built shards without coverage. The merge job rejects successful reports +twelve built shards without coverage. The merge job rejects successful reports with missing, unexpected, or duplicate portable files. Coverage instrumentation and aggregate thresholds remain enforced in full qualification. Missing or unusable history falls back to Vitest's default assignment. Only diff --git a/knip.json b/knip.json index 2734540923..375339a4e9 100644 --- a/knip.json +++ b/knip.json @@ -3,6 +3,7 @@ "entry": [ "src/main.ts", "src/cli.ts", + "src/cliOneShot.ts", "src/cliOutput.ts", "src/cliBuiltinArguments.ts", "src/cli/cliResultOutput.ts", diff --git a/knip.production.json b/knip.production.json index 5ccf4fd2ad..f45bc681b7 100644 --- a/knip.production.json +++ b/knip.production.json @@ -3,6 +3,7 @@ "entry": [ "src/main.ts!", "src/cli.ts!", + "src/cliOneShot.ts!", "src/cliOutput.ts!", "src/cliBuiltinArguments.ts!", "src/cli/cliResultOutput.ts!", diff --git a/scripts/bundle-runtime.mjs b/scripts/bundle-runtime.mjs index 54c6ddc82b..c135343c59 100644 --- a/scripts/bundle-runtime.mjs +++ b/scripts/bundle-runtime.mjs @@ -5,7 +5,8 @@ import { fileURLToPath, pathToFileURL } from "node:url"; /** * Reduce filesystem resolution during CLI and MCP startup. Keep dependencies - * external, dynamic imports lazy, and original compiled worker entrypoints. + * external and original compiled worker entrypoints. Dynamic imports can split + * into lazy chunks unless the same module is also statically reachable. */ export const bundleRuntime = async (root) => { const compiledRoot = resolve(root, "dist"); @@ -23,7 +24,7 @@ export const bundleRuntime = async (root) => { "cli-runtime", { cli: "cli.js", - cliOutput: "cliOutput.js", + cliOneShot: "cliOneShot.js", cliBuiltinArguments: "cliBuiltinArguments.js", cliResultOutput: "cli/cliResultOutput.js", mcpDoctor: "mcpDoctor.js", diff --git a/scripts/ci/job-timings.mjs b/scripts/ci/job-timings.mjs index 4d96f91b0f..60c6f78582 100644 --- a/scripts/ci/job-timings.mjs +++ b/scripts/ci/job-timings.mjs @@ -1,5 +1,5 @@ import { execFile } from "node:child_process"; -import { mkdir, readFile, writeFile } from "node:fs/promises"; +import { mkdir, writeFile } from "node:fs/promises"; import { dirname, join, resolve } from "node:path"; import { promisify } from "node:util"; import { fileURLToPath } from "node:url"; diff --git a/scripts/ci/test-group.mjs b/scripts/ci/test-group.mjs index 7d99175b4e..d69ea008f3 100644 --- a/scripts/ci/test-group.mjs +++ b/scripts/ci/test-group.mjs @@ -19,6 +19,11 @@ if ( const child = spawn( process.execPath, [ + // Reuse the development runner's POSIX process-group owner. Windows + // source tests must remain usable without the built native Job Object. + ...(process.platform === "win32" + ? [] + : [resolve("scripts/run-exclusive.mjs"), "--unlocked", process.execPath]), resolve("node_modules/vitest/vitest.mjs"), "run", ...projects.flatMap(({ name }) => ["--project", name]), diff --git a/scripts/lib/artifact-e2e.d.mts b/scripts/lib/artifact-e2e.d.mts index ee8f8503e9..6aad209dfc 100644 --- a/scripts/lib/artifact-e2e.d.mts +++ b/scripts/lib/artifact-e2e.d.mts @@ -2,6 +2,17 @@ import type { Client, CallToolRequestOptions, } from "@modelcontextprotocol/client"; +import type { VerifierCommands } from "./verify-commands.mjs"; + +/** Keep the opened target and its owned stdio server alive through verification. */ +export function withArtifactMcp( + target: string, + verify: (client: Client) => Promise, + options?: { + readonly commands?: VerifierCommands; + readonly signal?: AbortSignal; + }, +): Promise; /** Call with the server's advertised schema; callers cannot replace it. */ export function artifactMcpCall( diff --git a/scripts/lib/development-tests.d.mts b/scripts/lib/development-tests.d.mts index 89402e863d..e13e9cfdba 100644 --- a/scripts/lib/development-tests.d.mts +++ b/scripts/lib/development-tests.d.mts @@ -15,6 +15,7 @@ export function parseDevelopmentTestRequest( export function developmentTestPlan( request: DevelopmentTestRequest, baseCommit?: string, + resolvedPaths?: readonly string[], ): { readonly needsBuild: boolean; readonly artifactTasks: readonly string[]; diff --git a/scripts/lib/development-tests.mjs b/scripts/lib/development-tests.mjs index 84d094043b..22fcc23559 100644 --- a/scripts/lib/development-tests.mjs +++ b/scripts/lib/development-tests.mjs @@ -58,7 +58,11 @@ const artifactConsumers = { }; /** Plan source feedback or explicit tests with their runtime/artifact prerequisites. */ -export const developmentTestPlan = (request, baseCommit) => { +export const developmentTestPlan = ( + request, + baseCommit, + resolvedPaths = request.paths, +) => { const explicit = request.paths.length > 0; if ( request.mode === "changed" && @@ -70,12 +74,12 @@ export const developmentTestPlan = (request, baseCommit) => { ? [] : SOURCE_TEST_PROJECTS.map(({ name }) => name); return { - needsBuild: request.paths.some( + needsBuild: resolvedPaths.some( (path) => projectForTest(path).group === "built", ), artifactTasks: Object.entries(artifactConsumers).flatMap( ([task, consumers]) => - request.paths.some((path) => consumers.includes(path)) ? [task] : [], + resolvedPaths.some((path) => consumers.includes(path)) ? [task] : [], ), vitestArguments: [ "run", diff --git a/scripts/lib/verifier-command-owner.mts b/scripts/lib/verifier-command-owner.mts index 636f508cc5..b549976b4d 100644 --- a/scripts/lib/verifier-command-owner.mts +++ b/scripts/lib/verifier-command-owner.mts @@ -2,10 +2,7 @@ import { randomUUID } from "node:crypto"; import type { RetainedResourceScope } from "../../src/application/AnalysisProvider.js"; import { AnalysisCleanupLease } from "../../src/artifacts/AnalysisCleanupLease.js"; -import { - ArtifactResourceScope, - type ArtifactResourceOwner, -} from "../../src/artifacts/ArtifactResourceScope.js"; +import { ArtifactResourceScope } from "../../src/artifacts/ArtifactResourceScope.js"; import { prepareOwnedCommand, runOwnedCommand, @@ -61,6 +58,7 @@ export interface VerifierCommands { arguments_: readonly string[], options?: VerifierCommandOptions, ): Promise; + /** Retain a live resource until close, independently of ordinary command execution. */ adopt(owner: VerifierCleanupOwner, resource: string): void; readonly cleanupResources: readonly string[]; close(): Promise; @@ -152,13 +150,17 @@ const commandLimits = (options: VerifierCommandOptions) => { }; }; +type AdoptedVerifierOwner = { + readonly kind: "lease"; + readonly resource: string; + readonly lease: AnalysisCleanupLease; +}; + class VerifierCommandOwner implements VerifierCommands { readonly #resources = new ArtifactResourceScope(); readonly #controller = new AbortController(); - readonly #adopted = new WeakMap< - VerifierCleanupOwner, - ArtifactResourceOwner - >(); + readonly #adopted = new WeakMap(); + readonly #liveAdopted = new Set(); readonly #resourceOwner: RetainedResourceScope; #closed = false; #released = false; @@ -170,13 +172,21 @@ class VerifierCommandOwner implements VerifierCommands { ) => Promise, ) { this.#resourceOwner = { - retain: (owner, resource) => this.adopt(owner, resource), + retain: (owner, resource) => + this.#resources.retain(this.#adoptedOwner(owner, resource)), run: (work) => this.#resources.run(work), }; } get cleanupResources(): readonly string[] { - return this.#resources.cleanupResources; + return [ + ...new Set([ + ...this.#resources.cleanupResources, + ...[...this.#liveAdopted].flatMap(({ lease, resource }) => + lease.released ? [] : (lease.cleanupResources ?? [resource]), + ), + ]), + ]; } exec( @@ -233,6 +243,15 @@ class VerifierCommandOwner implements VerifierCommands { } adopt(owner: VerifierCleanupOwner, resource: string): void { + const retained = this.#adoptedOwner(owner, resource); + if (this.#closed) this.#resources.retain(retained); + else this.#liveAdopted.add(retained); + } + + #adoptedOwner( + owner: VerifierCleanupOwner, + resource: string, + ): AdoptedVerifierOwner { if (this.#released) throw new Error("Verifier command resource release has completed"); let retained = this.#adopted.get(owner); @@ -244,15 +263,18 @@ class VerifierCommandOwner implements VerifierCommands { }; this.#adopted.set(owner, retained); } - // An admitted command can transfer a late preparation/cleanup capability - // while close is joining it. Acquisition must precede this scope's release. - this.#resources.retain(retained); + return retained; } close(): Promise { this.#closed = true; this.#controller.abort(new Error("Verifier command owner closed")); if (this.#closing !== undefined) return this.#closing; + // Live adoption owns a lifetime, not a failed cleanup. Only closure moves + // these leases into the retry scope; command failures enter it immediately. + // Late transfers during admitted work still join that same close below. + for (const owner of this.#liveAdopted) this.#resources.retain(owner); + this.#liveAdopted.clear(); let attempt: Promise; attempt = this.#resources .close() diff --git a/scripts/prepack.mjs b/scripts/prepack.mjs index d5ee4f21c7..ccd2d91eb1 100644 --- a/scripts/prepack.mjs +++ b/scripts/prepack.mjs @@ -54,7 +54,7 @@ if (await pathExists(join(root, "src"))) { "dist/main.js", "dist/mcp/main.js", "dist/cli-runtime/cli.js", - "dist/cli-runtime/cliOutput.js", + "dist/cli-runtime/cliOneShot.js", "dist/cli-runtime/cliBuiltinArguments.js", "dist/cli-runtime/cliResultOutput.js", "dist/cli-runtime/mcpDoctor.js", diff --git a/scripts/rea.mjs b/scripts/rea.mjs index f5a25da273..e6050e03a1 100755 --- a/scripts/rea.mjs +++ b/scripts/rea.mjs @@ -64,7 +64,7 @@ const runtimeFiles = [ ? ["dist/mcp/main.js", "dist/cli-runtime/mcpDoctor.js"] : [ "dist/cli-runtime/cli.js", - "dist/cli-runtime/cliOutput.js", + "dist/cli-runtime/cliOneShot.js", "dist/cli-runtime/cliResultOutput.js", ]), ]; @@ -104,154 +104,66 @@ if (!(await compiledRuntimeExists(runtimeFiles))) { ); } } else { - const { - cliConfigurationError, - renderCliOutputArgumentError, - renderEmptyFilteredCliOutput, - renderVersionReport, - parseCliOutputArguments, - } = await import("../dist/cli-runtime/cliOutput.js"); + const { runOneShotCli } = await import("../dist/cli-runtime/cliOneShot.js"); guardClosedStdout(); - const cliArgs = isShellCompletion - ? args - : builtinArguments.normalizeCliArguments(args); - // Completion words are transport data, including partial output flags. - const outputOptions = parseCliOutputArguments( - isShellCompletion ? [] : cliArgs, - ); - const outputArguments = outputOptions.validation; - // `--version` with an explicit format reports the runtime as a document; - // plain `--version` stays Incur's bare version string. - const versionOutput = renderVersionReport( - outputOptions, - { - packageName: packageJson.name, - packageVersion: packageJson.version, - versions: process.versions, - platform: process.platform, - architecture: process.arch, - }, - process.stdout.isTTY === true, - ); - const configurationError = cliConfigurationError(process.env); let dispatchFailure; let closeCliResources; // Incur can exit from the callback below before this function returns. // That callback owns the status, including a closed-pipe exit of 141. let exiting = false; try { - // Doctor is the readiness report for this failure. Other commands still stop - // here so they do not run with an unusable log level. - if ( - configurationError !== undefined && - (isShellCompletion || - builtinArguments.splitIncurBuiltinArguments(cliArgs).command[0] !== - "doctor") - ) { - process.stderr.write(`${configurationError}\n`); - process.exitCode = 1; - } else if (!outputArguments.ok) { - writeStdout(renderCliOutputArgumentError(outputArguments)); - process.exitCode = 1; - } else if (versionOutput !== undefined) { - writeStdout(versionOutput); - } else if ( - !isShellCompletion && - builtinArguments.isProjectScopedAmpRegistration(cliArgs) - ) { - // Amp has global settings only. Share the parsed scope fact with the - // registration workflow and refuse before loading the command runtime. - // - // A caller that asked for a structured document gets the refusal in - // that document, through the same pre-execution error renderer the - // output-argument branch uses; anything else keeps the plain message. - const refusalMessage = - "Amp does not support project-scoped registration. Run without --no-global to register Amp globally, or select a client with project settings. No client settings were changed."; - const refusalFormat = outputOptions.format; - if ( - refusalFormat === "json" || - refusalFormat === "jsonl" || - refusalFormat === "yaml" - ) { - writeStdout( - renderCliOutputArgumentError({ - ok: false, - format: refusalFormat, - code: "AMP_PROJECT_SCOPE_UNSUPPORTED", - message: refusalMessage, - }), - ); - } else { - process.stderr.write(`${refusalMessage}\n`); - } - process.exitCode = 1; - } else { - const cliRuntime = await import("../dist/cli-runtime/cli.js"); - closeCliResources = cliRuntime.closeCliResources; - const { createCli } = cliRuntime; - const { createCliResultOutput } = - await import("../dist/cli-runtime/cliResultOutput.js"); - let wroteOutput = false; - const resultOutput = createCliResultOutput(outputOptions, process.stdout); - try { - await createCli(process.env, resultOutput).serve(cliArgs, { - stdout: (output) => { - if (resultOutput?.handled) { - if (resultOutput.failed && !stdoutClosed) - process.stderr.write(output); - return; - } - if (output.length > 0) wroteOutput = true; - if (resultOutput?.captureSdkOutput(output)) return; - writeStdout(output); - }, - // A command failure caused by the closed pipe keeps the pipe's status. - // Worker shutdown is shared with the dispatch finally below. A failed - // shutdown is reported here because this callback is what exits. - exit: (code) => { - exiting = true; - void (async () => { - let status = typeof code === "number" ? code : 0; - try { - await closeCliResources(); - } catch (cleanup) { - process.stderr.write( - `Failed to close CLI resources: ${cleanupFailureText(cleanup)}\n`, - ); - if (status === 0) status = 1; - } - try { - await resultOutput?.finishSdkOutput(status); - } catch (cause) { - if (!stdoutClosed) - process.stderr.write(`${cleanupFailureText(cause)}\n`); - if (status === 0) status = 1; - } - process.exit(stdoutClosed ? SIGPIPE_EXIT_STATUS : status); - })(); - }, - }); - } catch (cause) { - if (wroteOutput) throw cause; - const recovered = await resultOutput - ?.recoverFormattingFailure(cause) - .catch((writeFailure) => { - // Recovery writes outside Incur's command boundary. A reader that - // declined this document keeps the same quiet SIGPIPE outcome. - if (!stdoutClosed) throw writeFailure; - return true; - }); - if (!recovered) throw cause; - process.exitCode = stdoutClosed ? SIGPIPE_EXIT_STATUS : 1; - } - if (!wroteOutput && !resultOutput?.handled) - writeStdout(renderEmptyFilteredCliOutput(outputOptions) ?? ""); - // An SDK failure exits through the callback, which owns its pending write. - if (!exiting) - await resultOutput?.finishSdkOutput( - typeof process.exitCode === "number" ? process.exitCode : 0, - ); - } + await runOneShotCli(args, { + environment: process.env, + runtime: { + packageName: packageJson.name, + packageVersion: packageJson.version, + versions: process.versions, + platform: process.platform, + architecture: process.arch, + }, + stdoutIsTerminal: process.stdout.isTTY === true, + resultDestination: process.stdout, + closedStdoutStatus: SIGPIPE_EXIT_STATUS, + writeStdout, + writeStderr: (text) => { + process.stderr.write(text); + }, + stdoutClosed: () => stdoutClosed, + exitCode: () => + typeof process.exitCode === "number" ? process.exitCode : 0, + setExitCode: (code) => { + process.exitCode = code; + }, + ownCliResources: (close) => { + closeCliResources = close; + }, + exitRequested: () => exiting, + // A command failure caused by the closed pipe keeps the pipe's status. + // Worker shutdown is shared with the dispatch finally below. A failed + // shutdown is reported here because this callback is what exits. + exit: (code, finishOutput) => { + exiting = true; + void (async () => { + let status = code; + try { + await closeCliResources(); + } catch (cleanup) { + process.stderr.write( + `Failed to close CLI resources: ${cleanupFailureText(cleanup)}\n`, + ); + if (status === 0) status = 1; + } + try { + await finishOutput(status); + } catch (cause) { + if (!stdoutClosed) + process.stderr.write(`${cleanupFailureText(cause)}\n`); + if (status === 0) status = 1; + } + process.exit(stdoutClosed ? SIGPIPE_EXIT_STATUS : status); + })(); + }, + }); } catch (cause) { if (cause?.code === "EPIPE") { stdoutClosed = true; diff --git a/scripts/run-development-tests.mjs b/scripts/run-development-tests.mjs index e182b14658..56c2939071 100644 --- a/scripts/run-development-tests.mjs +++ b/scripts/run-development-tests.mjs @@ -1,12 +1,13 @@ import { spawn } from "node:child_process"; -import { stat } from "node:fs/promises"; -import { resolve } from "node:path"; +import { realpath, stat } from "node:fs/promises"; +import { relative, resolve, sep } from "node:path"; import { fileURLToPath } from "node:url"; import { developmentTestPlan, parseDevelopmentTestRequest, } from "./lib/development-tests.mjs"; +import { projectForTest } from "./lib/test-projects.mjs"; let child; let forceTimer; @@ -29,9 +30,22 @@ for (const signal of Object.keys(signalExitCodes)) try { const [mode, ...arguments_] = process.argv.slice(2); const request = parseDevelopmentTestRequest(mode, arguments_); + const resolvedPaths = []; + const repository = await realpath("."); for (const path of request.paths) { if (!(await stat(path)).isFile()) throw new Error(`Test path is not a file: ${path}`); + // Ownership follows the filesystem's canonical spelling. Execution keeps + // the caller path so exact discovery still rejects excluded symlink aliases. + const resolvedPath = relative(repository, await realpath(path)) + .split(sep) + .join("/"); + if ( + request.mode !== "focused" && + projectForTest(resolvedPath).group !== "source" + ) + throw new Error(`Use test:focused for built test projects: ${path}`); + resolvedPaths.push(resolvedPath); } let baseCommit; if (request.mode === "changed") { @@ -42,7 +56,7 @@ try { ); baseCommit = base.output.trim(); } - const plan = developmentTestPlan(request, baseCommit); + const plan = developmentTestPlan(request, baseCommit, resolvedPaths); if (request.dryRun) { console.log(JSON.stringify({ ...request, baseCommit, ...plan }, null, 2)); } else { diff --git a/scripts/verify-go-binary.mjs b/scripts/verify-go-binary.mjs index ddacb8b8be..cf9bc1327d 100644 --- a/scripts/verify-go-binary.mjs +++ b/scripts/verify-go-binary.mjs @@ -1,22 +1,23 @@ #!/usr/bin/env node import assert from "node:assert/strict"; import { isUtf8 } from "node:buffer"; -import { execFile } from "node:child_process"; import { createHash } from "node:crypto"; import { lstat, mkdir, readFile, writeFile } from "node:fs/promises"; import { isAbsolute, join } from "node:path"; import { fileURLToPath } from "node:url"; -import { promisify } from "node:util"; import { Client } from "@modelcontextprotocol/client"; -import { StdioClientTransport } from "@modelcontextprotocol/client/stdio"; import { parseEvidence } from "../dist/domain/evidence.js"; import { goBinarySchema } from "../dist/domain/go/goBinary.js"; import { PrivateRuntimeRoot } from "../dist/process/PrivateRuntimeRoot.js"; +import { AnalysisCleanupLease } from "../dist/artifacts/AnalysisCleanupLease.js"; import { artifactMcpCall } from "./lib/artifact-e2e.mjs"; import { requireMcpOutput } from "./lib/mcp-verifier-results.mjs"; import { createVerifierRun, completeVerifierRun } from "./lib/verifier-run.mjs"; +import { createVerifierCommands } from "./lib/verify-commands.mjs"; +import { createOwnedMcpStdioTransport } from "./lib/owned-mcp-stdio.mjs"; +import { closeVerifierResources } from "./lib/verifier-cleanup.mjs"; -const execute = promisify(execFile); +const commands = await createVerifierCommands(); const compiler = process.env.REA_VERIFY_GO_COMMAND ?? "go"; if (process.env.REA_VERIFY_GO_COMMAND !== undefined && !isAbsolute(compiler)) throw new Error( @@ -51,13 +52,16 @@ const goEnvironment = { // Go's own verifier seam isolates telemetry; no global configuration is changed. TEST_TELEMETRY_DIR: join(root.path, "telemetry"), }; -const client = new Client({ name: "go-binary-verifier", version: "1" }); -const transport = new StdioClientTransport({ - command: process.execPath, - args: [entrypoint, "mcp"], - env: environment, - stderr: "pipe", -}); +let client; +let transport; +const cleanup = new AnalysisCleanupLease(async () => { + await closeVerifierResources([ + () => client?.close(), + () => transport?.close(), + () => commands.close(), + ]); + await root.close(); +}, [root.path]); const targets = [ ["linux", "amd64", "elf", 64, "little"], ["linux", "386", "elf", 32, "little"], @@ -73,10 +77,19 @@ const verified = []; const omittedCoverage = []; const failures = []; try { + client = new Client({ name: "go-binary-verifier", version: "1" }); + transport = await createOwnedMcpStdioTransport({ + command: process.execPath, + arguments: [entrypoint, "mcp"], + hostEnvironment: environment, + cwd: process.cwd(), + expectedCommand: null, + sweepTokenOwnedProcesses: true, + }); await prepareFixture(); try { compilerVersion = ( - await execute(compiler, ["version"], { + await commands.exec(compiler, ["version"], { cwd: root.path, env: goEnvironment, timeout: 10_000, @@ -185,7 +198,7 @@ try { const path = join(root.path, "linux-amd64-byte-strings"); const expected = JSON.parse( ( - await execute(oracle, ["--build-byte-fixture", compiler, path], { + await commands.exec(oracle, ["--build-byte-fixture", compiler, path], { cwd: sourceRoot, env: { ...goEnvironment, GOOS: "linux", GOARCH: "amd64" }, timeout: 120_000, @@ -198,21 +211,16 @@ try { } catch (cause) { failures.push(cause); } finally { - for (const close of [ - () => client.close(), - () => transport.close(), - () => root.close(), - ]) { - try { - await close(); - } catch (cause) { - failures.push(cause); - } + try { + await cleanup.release(); + } catch (cause) { + failures.push(cause); } } const verifier = await completeVerifierRun(run); try { - await assert.rejects(lstat(root.path), { code: "ENOENT" }); + if (cleanup.released) + await assert.rejects(lstat(root.path), { code: "ENOENT" }); if (verifier.process_lineage.status === "verified") assert.deepEqual(verifier.process_lineage.descendants, []); } catch (cause) { @@ -226,15 +234,21 @@ if (failures.length > 0) { compiler: compilerVersion, verified, omitted_coverage: omittedCoverage, + retained_root: cleanup.released ? null : root.path, verifier, }, null, 2, ), ); - throw new AggregateError( - failures, - "Real Go metadata verification failed; cleanup failures are retained.", + throw Object.assign( + new AggregateError( + failures, + cleanup.released + ? "Real Go metadata verification failed; cleanup failures are retained." + : `Real Go metadata verification failed; cleanup is incomplete at ${root.path}.`, + ), + { cleanupLeases: cleanup.released ? [] : [cleanup] }, ); } console.log( @@ -256,7 +270,7 @@ console.log( ); async function go(args, overrides = {}) { - return execute(compiler, args, { + return commands.exec(compiler, args, { cwd: sourceRoot, env: { ...goEnvironment, ...overrides }, timeout: 120_000, @@ -356,7 +370,7 @@ async function inspect(mode, path, includeStringLiterals = false) { ...(includeStringLiterals ? ["--include-string-literals"] : []), "--json", ]; - const { stdout } = await execute(process.execPath, args, { + const { stdout } = await commands.exec(process.execPath, args, { env: environment, timeout: 30_000, maxBuffer: 16 * 1024 * 1024, @@ -424,7 +438,7 @@ async function verify( byteExpected ?? JSON.parse( ( - await execute(oracle, [oraclePath], { + await commands.exec(oracle, [oraclePath], { timeout: 10_000, maxBuffer: 1024 * 1024, }) diff --git a/scripts/verify-package-setup.mjs b/scripts/verify-package-setup.mjs index 5723280266..4222ca5e27 100644 --- a/scripts/verify-package-setup.mjs +++ b/scripts/verify-package-setup.mjs @@ -1,4 +1,3 @@ -import { deepStrictEqual } from "node:assert"; import { lstat, mkdir, @@ -13,7 +12,6 @@ import { dirname, isAbsolute, join } from "node:path"; import { parse as parseToml } from "smol-toml"; import { TOOL_CONTRACTS } from "../dist/contracts/toolContracts.js"; -import { PRODUCT_IDENTITY } from "../dist/identity.js"; import { pathExists } from "./lib/verify-package-core.mjs"; import { parse as parseJsonc } from "jsonc-parser"; import { skillReferenceIssues } from "./lib/docs-facts.mjs"; diff --git a/scripts/verify-real-ghidra-dol.mjs b/scripts/verify-real-ghidra-dol.mjs index eaa355351d..d92d6ea452 100644 --- a/scripts/verify-real-ghidra-dol.mjs +++ b/scripts/verify-real-ghidra-dol.mjs @@ -1,7 +1,6 @@ #!/usr/bin/env node import assert from "node:assert/strict"; import { execFile } from "node:child_process"; -import { createHash } from "node:crypto"; import { readFile, mkdtemp, mkdir, rm } from "node:fs/promises"; import { tmpdir } from "node:os"; import { join } from "node:path"; @@ -13,7 +12,6 @@ import { parseBinaryTarget } from "../dist/application/BinaryTargetResolver.js"; import { ArtifactResourceScope } from "../dist/artifacts/ArtifactResourceScope.js"; import { readGhidraLanguageCatalog } from "../dist/ghidra/GhidraLanguageCatalog.js"; import { parseEvidence } from "../dist/domain/evidence.js"; -import { parseAnalysisSnapshot } from "../dist/domain/analysisSnapshot.js"; import { functionDossierSchema } from "../dist/domain/native/functionDossier.js"; import { inspectVerifierGhidraInstallation } from "./lib/ghidra-installation-inspection.mjs"; import { buildGameCubeDolFixture } from "../tests/conformance/ghidra/dol-fixture.mjs"; @@ -96,8 +94,6 @@ async function languageInstalled(languageId) { } } -const sha256 = (bytes) => createHash("sha256").update(bytes).digest("hex"); - /** One MCP session plus CLI check. `languageOverride` is set only for the Gekko pass. */ async function runPass({ fixture, languageOverride, label, layout }) { const env = { ...baseEnv }; diff --git a/scripts/verify/android/failure-recovery.mjs b/scripts/verify/android/failure-recovery.mjs index 713520e088..f9c454adfd 100644 --- a/scripts/verify/android/failure-recovery.mjs +++ b/scripts/verify/android/failure-recovery.mjs @@ -151,7 +151,7 @@ export async function verifyAndroidFailureRecovery({ failure_code: "runtime_unavailable", }, availabilityReason: "provider_unavailable", - availabilityRemediation: /selected Java runtime/u, + availabilityRemediation: /selected Java launcher/u, message: /selected Java runtime/u, remediation: /selected launcher/u, }); @@ -194,12 +194,7 @@ export async function verifyAndroidFailureRecovery({ repository, available: false, unavailableReason: scenario.availabilityReason ?? "provider_missing", - remediation: - scenario.availabilityRemediation ?? - new RegExp( - scenario.observation.replace(/[.*+?^${}()|[\]\\]/gu, "\\$&"), - "u", - ), + remediation: scenario.availabilityRemediation ?? scenario.remediation, }); console.log(`PASS CLI applicable recovery: ${scenario.label}`); } diff --git a/scripts/verify/rea-numeric-limits.mjs b/scripts/verify/rea-numeric-limits.mjs index 8b53ab76fe..60cfdf21af 100644 --- a/scripts/verify/rea-numeric-limits.mjs +++ b/scripts/verify/rea-numeric-limits.mjs @@ -94,29 +94,36 @@ const stringBindings = (sourceFile, symbols, unresolved) => { ts.forEachChild(node, collect); }; collect(sourceFile); - const potential = (node) => { + const potential = (node, matchesCandidate) => { const expression = unwrapExpression(node); if (exactLimit(literalText(expression)) !== undefined) return true; - if (ts.isIdentifier(expression)) return candidates.has(expression.text); + if (ts.isIdentifier(expression)) return matchesCandidate(expression); if (ts.isPropertyAccessExpression(expression)) return ( exactLimit(expression.name.text) !== undefined || - potential(expression.expression) + potential(expression.expression, matchesCandidate) ); if (ts.isElementAccessExpression(expression)) return ( expression.argumentExpression !== undefined && - potential(expression.argumentExpression) + potential(expression.argumentExpression, matchesCandidate) ); if (ts.isObjectLiteralExpression(expression)) return expression.properties.some( (property) => - ts.isPropertyAssignment(property) && potential(property.initializer), + ts.isPropertyAssignment(property) && + potential(property.initializer, matchesCandidate), ); if (ts.isConditionalExpression(expression)) - return potential(expression.whenTrue) || potential(expression.whenFalse); + return ( + potential(expression.whenTrue, matchesCandidate) || + potential(expression.whenFalse, matchesCandidate) + ); if (ts.isBinaryExpression(expression)) - return potential(expression.left) || potential(expression.right); + return ( + potential(expression.left, matchesCandidate) || + potential(expression.right, matchesCandidate) + ); return false; }; let changed = true; @@ -125,13 +132,34 @@ const stringBindings = (sourceFile, symbols, unresolved) => { for (const declaration of declarations) { if ( !candidates.has(declaration.name.text) && - potential(declaration.initializer) + potential(declaration.initializer, (identifier) => + candidates.has(identifier.text), + ) ) { candidates.add(declaration.name.text); changed = true; } } } + // Names narrow the search cheaply, but only the lexical declaration can + // establish that a particular use is an alias of a limit. + const lexicalCandidates = new Set(); + changed = true; + while (changed) { + changed = false; + for (const declaration of declarations) { + if ( + candidates.has(declaration.name.text) && + !lexicalCandidates.has(declaration) && + potential(declaration.initializer, (identifier) => + lexicalCandidates.has(symbols(identifier)?.valueDeclaration), + ) + ) { + lexicalCandidates.add(declaration); + changed = true; + } + } + } const unknown = (node, reason) => { const path = sourceFile.fileName; const offset = node.getStart(); @@ -152,11 +180,12 @@ const stringBindings = (sourceFile, symbols, unresolved) => { declaration === undefined || !ts.isVariableDeclaration(declaration) || !ts.isVariableDeclarationList(declaration.parent) || - declaration.initializer === undefined + declaration.initializer === undefined || + !lexicalCandidates.has(declaration) ) return undefined; if ((declaration.parent.flags & ts.NodeFlags.Const) === 0) { - unknown(identifier, "mutable setting-key or value alias"); + unknown(declaration.name, "mutable setting-key or value alias"); return undefined; } if (declaration.pos >= identifier.pos) { @@ -890,7 +919,7 @@ if (direct) { const entries = parseNumericLimitRegistry(registry); const listed = new Map(); for (const entry of entries) { - const { name, policy, read, reason } = entry; + const { name, read, reason } = entry; if (listed.has(name)) failures.push(`${name} is listed twice`); listed.set(name, entry); if (reason.trim().length < 40) diff --git a/src/adb/AdbCommand.capture.test.ts b/src/adb/AdbCommand.capture.test.ts index 41b87d2cbb..2b2c8a7312 100644 --- a/src/adb/AdbCommand.capture.test.ts +++ b/src/adb/AdbCommand.capture.test.ts @@ -20,6 +20,7 @@ it.each([ source: 'process.stdout.write("partial-雪"); process.stderr.write("waiting\\n"); setInterval(() => {}, 1000)', code: "provider_timeout", + timeoutMs: 500, stdout: "partial-雪", stderr: "waiting\n", }, @@ -28,6 +29,7 @@ it.each([ source: 'process.stdout.write("partial-雪"); process.stderr.write("adb: error: device offline\\n"); process.exitCode = 1', code: "unsupported_target", + expectedPath: "caller-device", stdout: "partial-雪", stderr: "adb: error: device offline\n", }, @@ -36,6 +38,7 @@ it.each([ source: 'process.stdout.write("partial-雪"); process.stderr.write("adb: error: cannot stat \'/host/token file\': Permission denied\\n"); process.exitCode = 1', code: "unsupported_target", + expectedPath: "/host/token file", stdout: "partial-雪", stderr: "adb: error: cannot stat '/host/token file': Permission denied\n", }, @@ -54,7 +57,7 @@ it.each([ }, ["-e", testCase.source], { - timeoutMs: testCase.name === "timeout" ? 500 : 5_000, + timeoutMs: testCase.timeoutMs ?? 5_000, maxOutputBytes: testCase.maxOutputBytes ?? 1024, }, undefined, @@ -75,12 +78,34 @@ it.each([ }, }); if (testCase.code === "unsupported_target") - expect(projected.details?.path).toBe( - testCase.name === "local path refusal" - ? "/host/token file" - : "caller-device", - ); + expect(projected.details?.path).toBe(testCase.expectedPath); + if (testCase.code === "provider_timeout") { + expect(projected.details?.timeout_ms).toBe(testCase.timeoutMs); + expect(projected.details?.limit).toBeUndefined(); + } expect(JSON.stringify(projected)).not.toContain( "ambient-value-must-not-be-persisted", ); }); + +it.each([true, false])( + "projects a truncated adb capture as retryable only for volatile output (%s)", + async (volatileOutput) => { + const result = await runAdbCapture( + volatileOutput ? "read_adb_logcat" : "install_adb_package", + { device: "device" }, + { binary: process.execPath, pathSource: "environment", environment: {} }, + ["-e", 'process.stdout.write("X".repeat(4096))'], + { timeoutMs: 5_000, maxOutputBytes: 1024, volatileOutput }, + undefined, + ); + if (result.ok) throw new Error("Expected a capture allowance refusal"); + const projected = projectAnalysisError(result.error); + expect(projected.retryable).toBe(volatileOutput); + expect(projected).toMatchObject({ + code: "resource_constraint", + details: { reported_limits: { max_output_bytes: 1024 } }, + }); + expect(projected.details?.limit).toBeUndefined(); + }, +); diff --git a/src/adb/AdbCommand.test.ts b/src/adb/AdbCommand.test.ts index 3664389ded..e82b247bd5 100644 --- a/src/adb/AdbCommand.test.ts +++ b/src/adb/AdbCommand.test.ts @@ -1,5 +1,4 @@ import { expect, it, vi } from "vitest"; -import { projectAnalysisError } from "../domain/analysisErrorProjection.js"; import { ExecFileOutputCancelledError, execFileBytes, @@ -212,65 +211,6 @@ it("does not infer cancellation from a foreign ABORT_ERR code", async () => { }); }); -it.each([true, false])( - "projects a truncated adb capture as retryable only for volatile output (%s)", - async (volatileOutput) => { - vi.mocked(execFileBytes).mockRejectedValueOnce( - Object.assign(new Error("too much"), { - code: 1, - stdout: "", - stderr: "", - signal: null, - killed: false, - outputTruncated: true, - }), - ); - const result = await runAdbCapture( - volatileOutput ? "read_adb_logcat" : "install_adb_package", - { device: "device" }, - { binary: "/host/adb", pathSource: "environment", environment: {} }, - volatileOutput ? ["logcat", "-d"] : ["install", "app.apk"], - { timeoutMs: 100, maxOutputBytes: 1024, volatileOutput }, - undefined, - ); - if (result.ok) throw new Error("Expected a capture allowance refusal"); - const projected = projectAnalysisError(result.error); - expect(projected.retryable).toBe(volatileOutput); - expect(projected).toMatchObject({ - code: "resource_constraint", - details: { reported_limits: { max_output_bytes: 1024 } }, - }); - expect(projected.details?.limit).toBeUndefined(); - }, -); - -it("reports the effective fixed adb deadline", async () => { - vi.mocked(execFileBytes).mockRejectedValueOnce( - Object.assign(new Error("deadline"), { - code: "ETIMEDOUT", - stdout: "", - stderr: "", - signal: null, - killed: true, - }), - ); - const result = await runAdbCapture( - "pull_adb_file", - { device: "device" }, - { binary: "/host/adb", pathSource: "environment", environment: {} }, - ["pull", "/remote"], - { timeoutMs: 600_000, maxOutputBytes: 1024 }, - undefined, - ); - if (result.ok) throw new Error("Expected an adb deadline"); - const projected = projectAnalysisError(result.error); - expect(projected).toMatchObject({ - code: "provider_timeout", - details: { timeout_ms: 600_000 }, - }); - expect(projected.details?.limit).toBeUndefined(); -}); - it("keeps a physical failure even when the later abort reason is that same error", async () => { const controller = new AbortController(); const failure = new Error("physical command failure"); diff --git a/src/adb/AdbProvider.test.ts b/src/adb/AdbProvider.test.ts index 239baa8875..5d950e0490 100644 --- a/src/adb/AdbProvider.test.ts +++ b/src/adb/AdbProvider.test.ts @@ -191,7 +191,7 @@ fi`, await Promise.allSettled([inspection, closing]); await provider.close(); } -}, 15_000); +}); it("fails closed when adb exits successfully without a recognized version", async () => { const stdout = `unexpected adb output ${"x".repeat(700)}\n`; diff --git a/src/adb/AdbProvider.ts b/src/adb/AdbProvider.ts index 50677b3e1a..5b2a1c2e43 100644 --- a/src/adb/AdbProvider.ts +++ b/src/adb/AdbProvider.ts @@ -84,6 +84,7 @@ import { import { RegularFileOperationCancelled } from "../filesystem/RegularFile.js"; import { OwnedFileHandle } from "../filesystem/OwnedFileHandle.js"; import { createEvidence } from "../domain/evidence.js"; +import { createAnalysisExecutionEvidence } from "../domain/analysisObservation.js"; import { PrivateRuntimeRoot, type PrivateRuntimeRootOptions, @@ -993,15 +994,12 @@ export class AdbProvider { const partialObservation = execution === undefined ? undefined - : createEvidence(execution.subject ?? undefined, execution.provider, { + : createAnalysisExecutionEvidence( operation, parameters, - result: execution.result, - rawResult: execution.rawResult, - limitations: execution.limitations, - locations: execution.locations, - confidence: "observed", - }); + execution, + "observed", + ); return new ProviderCleanupError( ADB_PROVIDER_IDENTITY.id, resources, diff --git a/src/adb/AdbVersionProbe.test.ts b/src/adb/AdbVersionProbe.test.ts index d45b3bafca..6b1c70c263 100644 --- a/src/adb/AdbVersionProbe.test.ts +++ b/src/adb/AdbVersionProbe.test.ts @@ -47,19 +47,32 @@ it("reports the fixed adb version-probe deadline", async () => { }); it("reports the fixed adb version-output cap", async () => { - vi.mocked(execFileOutput).mockRejectedValueOnce( - Object.assign(new Error("too much"), { - code: "ERR_CHILD_PROCESS_STDIO_MAXBUFFER", - stdout: "", - stderr: "", - signal: null, - killed: false, - outputTruncated: true, - }), - ); - const failure = await inspectAdbClient(selection).catch( - (cause: unknown) => cause, - ); + let selected: string = selection.binary; + if (process.platform === "win32") { + vi.mocked(execFileOutput).mockRejectedValueOnce( + Object.assign(new Error("too much"), { + code: "ERR_CHILD_PROCESS_STDIO_MAXBUFFER", + stdout: "", + stderr: "", + signal: null, + killed: false, + outputTruncated: true, + }), + ); + } else { + const root = await mkdtemp(join(tmpdir(), "rea-adb-version-cap-")); + onTestFinished(() => rm(root, { recursive: true, force: true })); + selected = join(root, "adb"); + await writeFile( + selected, + `#!${process.execPath}\nprocess.stdout.write("x".repeat(100_000));\n`, + { mode: 0o755 }, + ); + } + const failure = await inspectAdbClient({ + ...selection, + binary: selected, + }).catch((cause: unknown) => cause); const projected = projectAnalysisError( failure as Parameters[0], ); @@ -74,25 +87,28 @@ it("reports the fixed adb version-output cap", async () => { expect(projected.details?.limit).toBeUndefined(); }); -it("distinguishes execute denial from missing or unreadable version output", async () => { - vi.mocked(execFileOutput).mockRejectedValueOnce( - Object.assign(new Error("denied"), { - code: "EACCES", - spawnFailed: true, - stdout: "", - stderr: "", - signal: null, - killed: false, - }), - ); - await expect(inspectAdbClient(selection)).rejects.toMatchObject({ - code: "binary_not_executable", - diagnostics: { - selected_binary: selection.binary, - filesystem_error: "EACCES", - }, - }); -}); +it.skipIf(process.platform !== "win32")( + "classifies an EACCES execute refusal on Windows", + async () => { + vi.mocked(execFileOutput).mockRejectedValueOnce( + Object.assign(new Error("denied"), { + code: "EACCES", + spawnFailed: true, + stdout: "", + stderr: "", + signal: null, + killed: false, + }), + ); + await expect(inspectAdbClient(selection)).rejects.toMatchObject({ + code: "binary_not_executable", + diagnostics: { + selected_binary: selection.binary, + filesystem_error: "EACCES", + }, + }); + }, +); it("names an EPERM execute refusal separately from missing permission", async () => { vi.mocked(execFileOutput).mockRejectedValueOnce( diff --git a/src/android/JadxProvider.ts b/src/android/JadxProvider.ts index b17e5a9d3d..3de0a13bbf 100644 --- a/src/android/JadxProvider.ts +++ b/src/android/JadxProvider.ts @@ -313,10 +313,10 @@ export class JadxProvider implements AndroidAnalysisPort { : AbortSignal.any([signal, this.#shutdown.signal]), }); this.#inspections.add(pending); - void pending.then( - () => this.#inspections.delete(pending), - () => this.#inspections.delete(pending), - ); + const remove = (): void => { + this.#inspections.delete(pending); + }; + void pending.then(remove, remove); return pending; } diff --git a/src/android/JadxRelease.ts b/src/android/JadxRelease.ts index 3e48adf558..4123d6f1ae 100644 --- a/src/android/JadxRelease.ts +++ b/src/android/JadxRelease.ts @@ -14,7 +14,7 @@ export const JADX_BRIDGE_IDENTITY = Object.freeze({ }); /** Remediation when `REA_JADX_MCP_JAR` is missing or not an absolute path. */ -export const JADX_JAR_CONFIGURATION_REMEDIATION = `Set REA_JADX_MCP_JAR to the absolute path of a caller-supplied jadx-headless-mcp ${JADX_RELEASE.version} JAR. REA does not download or install it.`; +export const JADX_JAR_CONFIGURATION_REMEDIATION = `Download the jadx-headless-mcp ${JADX_RELEASE.version} release JAR from ${JADX_RELEASE.url}, verify SHA-256 ${JADX_RELEASE.sha256}, and set REA_JADX_MCP_JAR to its absolute path. REA does not download or install it.`; /** Remediation when the selected Java runtime cannot satisfy the JADX bridge. */ export const JADX_JAVA_RUNTIME_REMEDIATION = diff --git a/src/apkSignature/ApkSignatureProvider.ts b/src/apkSignature/ApkSignatureProvider.ts index 66863f93c3..eb30ffcf5d 100644 --- a/src/apkSignature/ApkSignatureProvider.ts +++ b/src/apkSignature/ApkSignatureProvider.ts @@ -415,11 +415,11 @@ export class ApkSignatureProvider implements ApkSignaturePort { const { v1, decoded: v1Decoded, duplicates } = v1Read.value; const v2 = decodeScheme(block.value, APK_BLOCK_ID.signatureV2, "v2"); - if (!v2.ok) return err(v2.error); + if (!v2.ok) return v2; const v3 = decodeScheme(block.value, APK_BLOCK_ID.signatureV3, "v3"); - if (!v3.ok) return err(v3.error); + if (!v3.ok) return v3; const v31 = decodeScheme(block.value, APK_BLOCK_ID.signatureV31, "v3.1"); - if (!v31.ok) return err(v31.error); + if (!v31.ok) return v31; // Account for representation expansion before allocating the report. const projected = projectedOutputBytes( diff --git a/src/apktool/ApktoolConfiguration.test.ts b/src/apktool/ApktoolConfiguration.test.ts index b9730251c8..a3c4f49c1b 100644 --- a/src/apktool/ApktoolConfiguration.test.ts +++ b/src/apktool/ApktoolConfiguration.test.ts @@ -1,5 +1,6 @@ import * as commandOwner from "../process/OwnedCommand.js"; import { failedInspectionDescriptor } from "../../tests/fixtures/processInspectionCleanup.js"; +import { awaitAcknowledgement } from "../../tests/fixtures/lifecycleAcknowledgement.js"; import { AnalysisError, primaryAnalysisError, @@ -486,6 +487,7 @@ it("cancels a pending filesystem lookup and owns its eventual rejection", async const pendingStat = new Promise((_resolve, reject) => { rejectLookup = reject; }); + void pendingStat.catch(() => undefined); const observed = new Promise((resolve) => { started = resolve; }); @@ -502,14 +504,20 @@ it("cancels a pending filesystem lookup and owns its eventual rejection", async "win32", controller.signal, ); - await observed; - controller.abort(); - await expect(pending).rejects.toMatchObject({ - _tag: "AnalysisCancelledError", - }); - rejectLookup(new Error("filesystem completed after cancellation")); - await Promise.resolve(); - expect(lookup.stat).toHaveBeenCalledTimes(1); + try { + await awaitAcknowledgement(observed, pending, "apktool filesystem lookup"); + controller.abort(); + await expect(pending).rejects.toMatchObject({ + _tag: "AnalysisCancelledError", + }); + rejectLookup(new Error("filesystem completed after cancellation")); + await Promise.resolve(); + expect(lookup.stat).toHaveBeenCalledTimes(1); + } finally { + controller.abort(); + rejectLookup(new Error("fixture cleanup")); + await pending.catch(() => undefined); + } }); it("bounds the entire pending PATH lookup by the selection deadline", async () => { diff --git a/src/apktool/ApktoolVersionProbe.lifecycle.test.ts b/src/apktool/ApktoolVersionProbe.lifecycle.test.ts index 5b645a101a..f88f37bce7 100644 --- a/src/apktool/ApktoolVersionProbe.lifecycle.test.ts +++ b/src/apktool/ApktoolVersionProbe.lifecycle.test.ts @@ -58,7 +58,6 @@ it.skipIf(process.platform === "win32")( await fixture.cleanup([probe]); } }, - 30_000, ); let commands = new ApktoolCommandCleanup(); diff --git a/src/application/AnalysisContextQueries.test.ts b/src/application/AnalysisContextQueries.test.ts index f39e884da8..49fd81ec6b 100644 --- a/src/application/AnalysisContextQueries.test.ts +++ b/src/application/AnalysisContextQueries.test.ts @@ -1,5 +1,6 @@ -import { describe, expect, it } from "vitest"; +import { describe, expect, it, onTestFinished } from "vitest"; import { createDeferred } from "../../tests/fixtures/binarySession.js"; +import { awaitAcknowledgement } from "../../tests/fixtures/lifecycleAcknowledgement.js"; import { getNavigationContext, @@ -500,7 +501,16 @@ it("waits for late facet cleanup after cancellation and retains its owner", asyn settled = true; return result; }); - await started.promise; + onTestFinished(async () => { + controller.abort(); + late.resolve(err(failure)); + try { + await pending; + } finally { + await lease.release(); + } + }); + await awaitAcknowledgement(started.promise, pending, "facet execution"); controller.abort(); await Promise.resolve(); expect(settled).toBe(false); @@ -530,7 +540,12 @@ it("reports cancellation after a late successful facet that ignored its signal", { address: "0x401000", document: "fixture" }, controller.signal, ); - await started.promise; + onTestFinished(async () => { + controller.abort(); + late.resolve(ok(createAnalysisExecution("finished", provider))); + await pending; + }); + await awaitAcknowledgement(started.promise, pending, "facet execution"); controller.abort(); late.resolve(ok(createAnalysisExecution("finished", provider))); const result = await pending; @@ -586,7 +601,11 @@ it("preserves a provider's cancellation for the same signal", async () => { { address: "0x401000", document: "fixture" }, controller.signal, ); - await started.promise; + onTestFinished(async () => { + controller.abort(reason); + await pending; + }); + await awaitAcknowledgement(started.promise, pending, "provider execution"); controller.abort(reason); const result = await pending; if (result.ok) throw new Error("expected cancellation"); diff --git a/src/application/AnalysisProvider.ts b/src/application/AnalysisProvider.ts index 2aa8cd54a1..6f7786df23 100644 --- a/src/application/AnalysisProvider.ts +++ b/src/application/AnalysisProvider.ts @@ -53,6 +53,11 @@ export interface ExecutionOptions { * supplied it. */ readonly selectorOrigin?: "provider"; + /** + * Whether the caller's catalog advertises an operation; refusal recovery + * names only advertised operations. Absent means every operation is. + */ + readonly isOperationAdvertised?: (operation: AnalysisOperation) => boolean; } /** Closed operations accepted by provider-backed analysis sessions. */ diff --git a/src/application/AppBundleExecutable.openStep.test.ts b/src/application/AppBundleExecutable.openStep.test.ts index cf8e8011a1..4c3f1fa4f2 100644 --- a/src/application/AppBundleExecutable.openStep.test.ts +++ b/src/application/AppBundleExecutable.openStep.test.ts @@ -4,7 +4,7 @@ import { join } from "node:path"; import { expect, it } from "vitest"; import { createTestTempDirectory } from "../../tests/fixtures/temporaryDirectory.js"; -import { resolveAppBundleExecutable } from "./AppBundleExecutable.js"; +import { resolveAppleBundleExecutable } from "./AppBundleExecutable.js"; it.each([ ["no comment", ""], @@ -22,7 +22,7 @@ it.each([ await writeFile(infoPlist, source); await writeFile(executable, "fixture - never executed"); - expect(await resolveAppBundleExecutable(bundle)).toEqual({ + expect(await resolveAppleBundleExecutable(bundle)).toEqual({ ok: true, value: { executable, infoPlist }, }); diff --git a/src/application/AppBundleExecutable.test.ts b/src/application/AppBundleExecutable.test.ts index 2e3a016bf1..eaaff23396 100644 --- a/src/application/AppBundleExecutable.test.ts +++ b/src/application/AppBundleExecutable.test.ts @@ -6,7 +6,7 @@ import { describe, expect, it } from "vitest"; import { createTestTempDirectory } from "../../tests/fixtures/temporaryDirectory.js"; import { binaryPlistObjectTable } from "../../tests/fixtures/binaryPlistObjectTable.js"; -import { resolveAppBundleExecutable } from "./AppBundleExecutable.js"; +import { resolveAppleBundleExecutable } from "./AppBundleExecutable.js"; const binaryPlistExecutableWithThreeByteUid = (): Buffer => { const ascii = (value: string): Buffer => { @@ -53,7 +53,7 @@ it.each([ ); await writeFile(executable, "inert fixture; never executed"); - const result = await resolveAppBundleExecutable(bundle); + const result = await resolveAppleBundleExecutable(bundle); expect(result).toEqual({ ok: true, value: { executable, infoPlist: plistPath }, @@ -71,7 +71,7 @@ describe("app bundle Info.plist magic", () => { await writeFile(plistPath, binaryPlistExecutableWithThreeByteUid()); await writeFile(executable, "inert fixture; never executed"); - expect(await resolveAppBundleExecutable(bundle)).toEqual({ + expect(await resolveAppleBundleExecutable(bundle)).toEqual({ ok: true, value: { executable, infoPlist: plistPath }, }); @@ -86,7 +86,7 @@ describe("app bundle Info.plist magic", () => { const plist = Buffer.from("bplist00"); plist[0] = (plist[0] ?? 0) | 0x80; await writeFile(plistPath, plist); - const result = await resolveAppBundleExecutable(bundle); + const result = await resolveAppleBundleExecutable(bundle); expect(result).toMatchObject({ ok: false, error: { @@ -114,7 +114,7 @@ describe("app bundle program directory containment", () => { await writeFile(join(bundle, "Contents", "Info.plist"), plist); let childResolved = false; - const result = await resolveAppBundleExecutable(bundle, { + const result = await resolveAppleBundleExecutable(bundle, { readFile: async () => Buffer.from(plist), realpath: async (path) => { if (path === bundle) return canonicalBundle; diff --git a/src/application/AppBundleExecutable.ts b/src/application/AppBundleExecutable.ts index 815debc13d..9c737fd822 100644 --- a/src/application/AppBundleExecutable.ts +++ b/src/application/AppBundleExecutable.ts @@ -88,7 +88,7 @@ export interface ResolvedAppBundleResourceRoot { } /** Filesystem operations used by the resolver, injectable for boundary tests. */ -export interface AppBundleFileSystem { +interface AppBundleFileSystem { readonly readFile: (path: string) => Promise; readonly realpath: (path: string) => Promise; } @@ -101,7 +101,7 @@ const defaultFileSystem: AppBundleFileSystem = { readFile, realpath }; * a wrapper whose `Wrapper` directory holds the iOS bundle; that bundle is * found by listing `Wrapper`, never by following the `WrappedBundle` link. */ -export const resolveAppBundleExecutable = async ( +const resolveAppBundleExecutable = async ( path: string, fileSystem: AppBundleFileSystem = defaultFileSystem, ): Promise> => { diff --git a/src/application/AppBundleOpenStepDepth.test.ts b/src/application/AppBundleOpenStepDepth.test.ts index 38ff38862c..7775c76d59 100644 --- a/src/application/AppBundleOpenStepDepth.test.ts +++ b/src/application/AppBundleOpenStepDepth.test.ts @@ -3,7 +3,7 @@ import { join } from "node:path"; import { describe, expect, it } from "vitest"; -import { resolveAppBundleExecutable } from "./AppBundleExecutable.js"; +import { resolveAppleBundleExecutable } from "./AppBundleExecutable.js"; import { createTestTempDirectory } from "../../tests/fixtures/temporaryDirectory.js"; describe("OpenStep Info.plist nesting during app target resolution", () => { @@ -20,7 +20,7 @@ describe("OpenStep Info.plist nesting during app target resolution", () => { `{ CFBundleExecutable = App; payload = ${"(".repeat(depth)}x${")".repeat(depth)}; }`, ); - const result = await resolveAppBundleExecutable(app); + const result = await resolveAppleBundleExecutable(app); if (depth === 127) { expect(result).toMatchObject({ @@ -52,7 +52,7 @@ describe("OpenStep Info.plist nesting during app target resolution", () => { `{ CFBundleExecutable = App; }${"(".repeat(3000)}x${")".repeat(3000)}`, ); - expect(await resolveAppBundleExecutable(app)).toMatchObject({ + expect(await resolveAppleBundleExecutable(app)).toMatchObject({ ok: true, value: { executable: join(programs, "App") }, }); diff --git a/src/application/BinaryTargetResolver.ts b/src/application/BinaryTargetResolver.ts index 3267f9635c..522adcca87 100644 --- a/src/application/BinaryTargetResolver.ts +++ b/src/application/BinaryTargetResolver.ts @@ -208,7 +208,7 @@ export const parseBinaryTarget = async ( throwIfTargetResolutionCancelled(signal); const resolved = await resolveAppBundle(canonical); throwIfTargetResolutionCancelled(signal); - if (!resolved.ok) return err(resolved.error); + if (!resolved.ok) return resolved; const { executable: path, infoPlist } = resolved.value; const bundle = infoPlist === undefined ? {} : { bundleInfoPlist: infoPlist }; diff --git a/src/application/CapabilityInventory.ts b/src/application/CapabilityInventory.ts index 5886a117ce..1ca915e89a 100644 --- a/src/application/CapabilityInventory.ts +++ b/src/application/CapabilityInventory.ts @@ -56,6 +56,7 @@ import { type AnalysisCleanupRetry, } from "../domain/analysisErrorRecovery.js"; import { isBundleResourceOperation } from "./binary/BundleResourceSession.js"; +import { offeredOperationAlternatives } from "./OperationAlternatives.js"; const FRIDA_OPERATION_NAMES: ReadonlySet = new Set( FRIDA_TOOL_CONTRACTS.map(({ name }) => name), @@ -126,6 +127,8 @@ export type AvailabilityPolicy = { type AvailabilityFacts = { readonly defaultModeAvailable?: boolean; readonly modes?: readonly ToolAvailabilityMode[]; + /** Advertised operations the current provider offers for part of the question. */ + readonly alternatives?: readonly AnalysisOperation[]; }; type Availability = AvailabilityFacts & ( @@ -162,6 +165,7 @@ type AvailabilityContext = { readonly targetFormat: string | undefined; readonly descriptors: ReadonlyMap; readonly policy: AvailabilityPolicy; + readonly isToolAdvertised: (name: string) => boolean; }; type EnhancedContractName = Extract< @@ -322,10 +326,15 @@ const ADDRESS_CONTEXT_EFFECT_COMPOSITION: ModeComposition = { ], }; -/** Build stable per-operation availability for discovery and tool visibility. */ +/** + * Build stable per-operation availability for the caller's advertised tools. + * Each tool's availability still derives from the complete provider facts; + * advertisement narrows only the reported tools and the alternatives named. + */ export const buildCapabilityInventory = ( sessionStatus: JsonValue, policy: AvailabilityPolicy, + isToolAdvertised: (name: string) => boolean = () => true, ): readonly ToolAvailability[] => { const status = statusSchema.parse(sessionStatus); const descriptors = new Map( @@ -345,6 +354,7 @@ export const buildCapabilityInventory = ( targetFormat: status.format, descriptors, policy, + isToolAdvertised, }); const effects = composedEffects( contract.name, @@ -398,8 +408,13 @@ export const buildCapabilityInventory = ( available: false, reason: availability.reason, remediation: availability.remediation, + ...(availability.alternatives === undefined + ? {} + : { alternatives: [...availability.alternatives] }), }; - }).sort((left, right) => compareUnicodeCodePoints(left.name, right.name)); + }) + .filter(({ name }) => isToolAdvertised(name)) + .sort((left, right) => compareUnicodeCodePoints(left.name, right.name)); }; type ClientRequirements = ToolAvailability["client_requirements"]; @@ -445,6 +460,9 @@ export function compactToolAvailability( ? {} : { default_mode_available: entry.default_mode_available }), ...(entry.modes === undefined ? {} : { modes: [...entry.modes] }), + ...(entry.available || entry.alternatives === undefined + ? {} + : { alternatives: [...entry.alternatives] }), ...(JSON.stringify(entry.client_requirements) === JSON.stringify(clientRequirements) ? {} @@ -730,7 +748,7 @@ const workflowAvailabilityFor = ( : "provider_missing", remediation: policy.androidAnalysisAvailability?.remediation ?? - "Set REA_JADX_MCP_JAR to a caller-supplied jadx-headless-mcp 0.7.1 JAR and provide a full JDK on Linux, macOS, or Windows x64 with its matching bundled native controls.", + "Set REA_JADX_MCP_JAR to the jadx-headless-mcp 0.7.1 release JAR (rea doctor --provider jadx names its download URL and SHA-256) and provide a full JDK on Linux, macOS, or Windows x64 with its matching bundled native controls.", }; if (kind === "adb-provider") return ownerProbeAvailability( @@ -959,6 +977,7 @@ const providerAvailability = ({ kind, name, policy, + isToolAdvertised, }: AvailabilityContext): Availability => { if (kind === "enhanced") return composedAvailability(name, descriptors); const launchFailure = @@ -976,12 +995,20 @@ const providerAvailability = ({ kind === "native-provider") ) return { reason: "available", remediation: null }; - if (descriptor === undefined) + if (descriptor === undefined) { + const offered = offeredOperationAlternatives( + name, + (alternative) => + descriptors.get(alternative)?.available === true && + isToolAdvertised(alternative), + ); return { reason: "provider_missing", remediation: "Install or configure a provider that declares this operation.", + ...(offered === undefined ? {} : { alternatives: offered.operations }), }; + } return descriptor.available ? { reason: "available", remediation: null } : { diff --git a/src/application/CapabilityScope.test.ts b/src/application/CapabilityScope.test.ts index 4842d8fafe..2ee0da3f22 100644 --- a/src/application/CapabilityScope.test.ts +++ b/src/application/CapabilityScope.test.ts @@ -93,7 +93,12 @@ it("joins concurrent shutdowns and seals ownership before asynchronous cleanup s try { await awaitAcknowledgement(entered, first, "capability cleanup"); second = scope.close(); - expect(second).toBe(first); + let secondSettled = false; + void second.then(() => { + secondSettled = true; + }); + await Promise.resolve(); + expect(secondSettled).toBe(false); expect(close).toHaveBeenCalledTimes(1); expect(() => scope.own("later-owner", {}, async () => undefined)).toThrow( "after host shutdown", diff --git a/src/application/ClaudeDesktopConfigLocation.test.ts b/src/application/ClaudeDesktopConfigLocation.test.ts index 662b3ebef2..d0529a5308 100644 --- a/src/application/ClaudeDesktopConfigLocation.test.ts +++ b/src/application/ClaudeDesktopConfigLocation.test.ts @@ -1,15 +1,14 @@ +import { CLAUDE_DESKTOP_PACKAGE_FAMILY } from "../../tests/fixtures/claudeDesktop.js"; import { basename, dirname, join, sep } from "node:path"; import { describe, expect, it } from "vitest"; import { - CLAUDE_DESKTOP_PACKAGE_FAMILY, resolveWindowsClaudeDesktopLocation, type ClaudeDesktopLocationFileSystem, } from "./ClaudeDesktopConfigLocation.js"; import { packagePrivateLauncherBlocker, - packagePrivateStorageFamily, redirectedWriteBlocker, type MsixPackageView, windowsAppDataRoots, @@ -642,15 +641,19 @@ describe("package-private REA launchers", () => { "rea.mjs", ); - it("names the package that privately stores a path", () => { - expect(packagePrivateStorageFamily(stored, packages)).toBe( - CLAUDE_DESKTOP_PACKAGE_FAMILY, - ); - expect(packagePrivateStorageFamily(launcher, packages)).toBeUndefined(); + it("blocks private cache paths without treating other package paths as private", () => { + const identity = (path: string) => path; expect( - packagePrivateStorageFamily( - join(packages, CLAUDE_DESKTOP_PACKAGE_FAMILY, "LocalState", "x"), + packagePrivateLauncherBlocker([stored], packages, identity), + ).toContain(CLAUDE_DESKTOP_PACKAGE_FAMILY); + expect( + packagePrivateLauncherBlocker([launcher], packages, identity), + ).toBeUndefined(); + expect( + packagePrivateLauncherBlocker( + [join(packages, CLAUDE_DESKTOP_PACKAGE_FAMILY, "LocalState", "x")], packages, + identity, ), ).toBeUndefined(); }); diff --git a/src/application/ClaudeDesktopConfigLocation.ts b/src/application/ClaudeDesktopConfigLocation.ts index fb10e496ff..bbee87fe29 100644 --- a/src/application/ClaudeDesktopConfigLocation.ts +++ b/src/application/ClaudeDesktopConfigLocation.ts @@ -29,7 +29,7 @@ const PROBES_PER_DIRECTORY = 2; * publisher ID Windows derives from `CN="Anthropic, PBC", O="Anthropic, PBC", * L=San Francisco, S=California, C=US, …`. */ -export const CLAUDE_DESKTOP_PACKAGE_FAMILY = "Claude_pzs8sxrjxfjjc"; +const CLAUDE_DESKTOP_PACKAGE_FAMILY = "Claude_pzs8sxrjxfjjc"; /** Observed Claude package names plus Windows' 13-character publisher ID. */ const CLAUDE_PACKAGE_FAMILY = diff --git a/src/application/DesktopBuildDoctor.test.ts b/src/application/DesktopBuildDoctor.test.ts index 04e7ee13c7..adefe24d5c 100644 --- a/src/application/DesktopBuildDoctor.test.ts +++ b/src/application/DesktopBuildDoctor.test.ts @@ -36,7 +36,7 @@ const buildHost = ( createDoctorHostFixture({ platform: "linux", architecture: "x64", - installationPaths: () => Promise.resolve([]), + installationLaunchers: () => Promise.resolve([]), clientRegistrations: () => Promise.resolve([]), desktopBuildFacts: () => Promise.resolve({ ...ready, ...facts }), ...overrides, diff --git a/src/application/DirectAnalysis.ts b/src/application/DirectAnalysis.ts index 589d76b9f3..08ccf5126b 100644 --- a/src/application/DirectAnalysis.ts +++ b/src/application/DirectAnalysis.ts @@ -67,10 +67,7 @@ import type { AnalysisProfileCommitment } from "../domain/analysisProfile.js"; import { profilelessNativeDispatchProfile } from "./binary/ProfilelessNativeWorkflowSnapshot.js"; import { err, ok, type Result } from "../domain/result.js"; import type { AnalysisSnapshot } from "../domain/analysisSnapshot.js"; -import type { - AnalysisExecution, - ProviderIdentity, -} from "./AnalysisProvider.js"; +import type { AnalysisExecution } from "./AnalysisProvider.js"; import { createAnalysisExecutionEvidence } from "../domain/analysisObservation.js"; import { REA_WORKFLOW_PROVIDER } from "../domain/investigationProviders.js"; import { workflowAnalysisProfile } from "./WorkflowAnalysisProfile.js"; @@ -296,7 +293,7 @@ export const runProviderAnalysis = async ( delivery.detail, operationSignal, ); - return result.ok ? ok(result.value) : err(cliError(result.error, path)); + return result.ok ? result : err(cliError(result.error, path)); } const admitted = admitOneShotOperation(tool, arguments_, operationSignal); if (!admitted.ok) return err(cliError(admitted.error, path)); @@ -445,14 +442,14 @@ const runAnalysis = async ( let isDirectory = false; if (BUNDLE_RESOURCE_OPERATIONS.has(tool)) { const inspected = await inspectBundleResourceInput(path); - if (!inspected.ok) return err(inspected.error); + if (!inspected.ok) return inspected; isDirectory = inspected.value; } if (BUNDLE_RESOURCE_OPERATIONS.has(tool) && isDirectory) { const opened = await session.openBundleResources(path, { signal }); - if (!opened.ok) return err(opened.error); + if (!opened.ok) return opened; const execution = await session.execute(tool, arguments_, { signal }); - if (!execution.ok) return err(execution.error); + if (!execution.ok) return execution; return ok( withEvidenceCallerInputPath( createAnalysisExecutionEvidence( @@ -472,7 +469,7 @@ const runAnalysis = async ( signal, ...targetSelection(options), }); - if (!prepared.ok) return err(prepared.error); + if (!prepared.ok) return prepared; const { snapshot } = prepared.value; let resolvedTarget: ResolvedSessionOpen | undefined; if (snapshot !== undefined && prepared.value.target !== undefined) { @@ -484,7 +481,7 @@ const runAnalysis = async ( ? {} : { providerId: options.providerId }), }); - if (!preview.ok) return err(preview.error); + if (!preview.ok) return preview; resolvedTarget = preview.value; const route = preview.value.route; const bindingProfile = @@ -548,7 +545,7 @@ const runAnalysis = async ( resolvedTarget === undefined ? await session.open(path, openOptions) : await session.openResolvedTarget(resolvedTarget, openOptions); - if (!opened.ok) return err(opened.error); + if (!opened.ok) return opened; const evidenceProfile = analysisProfileForEvidence(session, tool); const executed = await executeAnalysisTool({ session, diff --git a/src/application/Doctor.test.ts b/src/application/Doctor.test.ts index 5d5b65f9f7..1a4b97830e 100644 --- a/src/application/Doctor.test.ts +++ b/src/application/Doctor.test.ts @@ -17,7 +17,7 @@ const configurationHost = ( ): DoctorHost => ({ ...systemDoctorHost({ environment: { ...environment } }), ...host({ - installationPaths: () => Promise.resolve([]), + installationLaunchers: () => Promise.resolve([]), clientRegistrations: () => Promise.resolve([]), }), }); @@ -32,7 +32,7 @@ describe("doctor Hopper startup timeout", () => { value === undefined ? {} : { REA_HOPPER_STARTUP_TIMEOUT_MS: value }, }), ...host({ - installationPaths: () => Promise.resolve([]), + installationLaunchers: () => Promise.resolve([]), clientRegistrations: () => Promise.resolve([]), ...overrides, }), @@ -725,8 +725,13 @@ describe("doctor installation identity", () => { const result = await runDoctor( undefined, host({ - installationPaths: () => - Promise.resolve(["/usr/local/bin/rea", "/home/user/bin/rea"]), + installationLaunchers: () => + Promise.resolve( + ["/usr/local/bin", "/home/user/bin"].map((installation) => ({ + path: `${installation}/rea`, + installation, + })), + ), installedSkillIdentity: () => Promise.resolve({ version: "10", @@ -1390,3 +1395,24 @@ describe("doctor target admission diagnostics", () => { expect(result.checks).toContainEqual(targetCheck); }); }); + +describe("doctor installation count", () => { + it("reports one installation when several launchers resolve to it", async () => { + const result = await runDoctor( + undefined, + host({ + installationLaunchers: () => + Promise.resolve( + ["rea", "rea.cmd"].map((name) => ({ + path: `C:\\npm\\${name}`, + installation: "c:\\npm", + })), + ), + }), + ); + expect(result.identity?.installations).toEqual({ + paths: ["C:\\npm\\rea", "C:\\npm\\rea.cmd"], + state: "single", + }); + }); +}); diff --git a/src/application/Doctor.ts b/src/application/Doctor.ts index aa6b08eb66..f4c4b7bde7 100644 --- a/src/application/Doctor.ts +++ b/src/application/Doctor.ts @@ -8,7 +8,8 @@ import { ArtifactReaderFailure } from "../artifacts/ArtifactReader.js"; import { AnalysisCleanupLease } from "../artifacts/AnalysisCleanupLease.js"; import { STARTUP_OWNER_CLOSED } from "../process/SharedStartupAttempt.js"; import { constants } from "node:fs"; -import { access, stat } from "node:fs/promises"; +import { access, realpath, stat } from "node:fs/promises"; +import { posix, win32 } from "node:path"; import { ArtifactResourceScope } from "../artifacts/ArtifactResourceScope.js"; import type { RetainedResourceScope } from "./AnalysisProvider.js"; import { @@ -151,7 +152,8 @@ export interface DoctorHost { ): Promise; /** Resolve a parsed upstream request budget from the effective client environment. */ resolveProviderRequestBudget?: ProviderRequestBudgetResolver; - installationPaths?(): Promise; + /** `rea` launchers in PATH order, each with the installation it resolves to. */ + installationLaunchers?(): Promise; /** Undefined checks installed skill roots; explicit selections stay scoped. */ installedSkillIdentity?( clientIds: readonly string[] | undefined, @@ -486,7 +488,7 @@ const collectDoctorIdentity = async ( readonly checks: readonly DoctorCheck[]; readonly value: DoctorIdentity; }> => { - const installationPaths = (await host.installationPaths?.()) ?? []; + const launchers = (await host.installationLaunchers?.()) ?? []; const registrations = (await host.clientRegistrations?.( scope.clients, @@ -528,8 +530,8 @@ const collectDoctorIdentity = async ( "Call binary_session in the active client to inspect the running server identity; a registration whose saved scope omits the session family cannot report it, so include session through rea setup --tool-families and restart first.", }, installations: { - paths: installationPaths, - state: installationState(installationPaths), + paths: launchers.map(({ path }) => path), + state: installationState(launchers), }, skill: { installed_version: installedSkillIdentity?.version ?? null, @@ -626,10 +628,41 @@ const registrationCheck = ( remediation: registration.remediation, }); +/** A PATH launcher and the installation identity its host resolved it to. */ +export interface DoctorInstallationLauncher { + readonly path: string; + readonly installation: string; +} + const installationState = ( - paths: readonly string[], -): DoctorIdentity["installations"]["state"] => - paths.length === 0 ? "unknown" : paths.length === 1 ? "single" : "multiple"; + launchers: readonly DoctorInstallationLauncher[], +): DoctorIdentity["installations"]["state"] => { + const installations = new Set( + launchers.map(({ installation }) => installation), + ); + return installations.size === 0 + ? "unknown" + : installations.size === 1 + ? "single" + : "multiple"; +}; + +/** + * One installation is one launcher directory after symlink resolution. npm + * writes `rea`, `rea.cmd` and `rea.ps1` side by side on Windows, where `where` + * lists each and directory spelling is case-insensitive; PATH can also reach + * one launcher through a symlinked directory. An unresolvable launcher keeps + * its listed path rather than joining another installation. + */ +const launcherInstallation = async ( + path: string, + platform: NodeJS.Platform, +): Promise => { + const resolved = await realpath(path).catch(() => path); + return platform === "win32" + ? win32.dirname(resolved).toLowerCase() + : posix.dirname(resolved); +}; /** Optional outer-adapter diagnostics composed without reversing dependencies. */ export interface SystemDoctorHostOptions { @@ -870,14 +903,20 @@ export const systemDoctorHost = ( return undefined; } }, - async installationPaths() { + async installationLaunchers() { try { const command = platform === "win32" ? "where" : "which"; const arguments_ = platform === "win32" ? ["rea"] : ["-a", "rea"]; - return uniqueLines( + const paths = uniqueLines( (await hostExecFileOutput(command, arguments_, commandEnvironment)) .stdout, ); + return await Promise.all( + paths.map(async (path) => ({ + path, + installation: await launcherInstallation(path, platform), + })), + ); } catch (cause: unknown) { if (cause instanceof AnalysisCancelledError) throw cause; // best-effort cleanup: optional path probing; failure means none found. diff --git a/src/application/EvidenceBundleCommands.ts b/src/application/EvidenceBundleCommands.ts index 3f4cc35192..02b2ea2e81 100644 --- a/src/application/EvidenceBundleCommands.ts +++ b/src/application/EvidenceBundleCommands.ts @@ -307,9 +307,7 @@ const projectWrite = ( bundle: EvidenceBundle, written: Result, ): Result => - written.ok - ? ok(evidenceBundleExportReceipt(bundle, written.value)) - : err(written.error); + written.ok ? ok(evidenceBundleExportReceipt(bundle, written.value)) : written; /** Standalone bundle discovery shares the same read owner as import and comparison. */ export const inspectEvidenceBundleCommand = async ( diff --git a/src/application/EvidenceBundleFiles.ts b/src/application/EvidenceBundleFiles.ts index ccb3f350c3..2753b4e689 100644 --- a/src/application/EvidenceBundleFiles.ts +++ b/src/application/EvidenceBundleFiles.ts @@ -31,16 +31,11 @@ import { import { readJsonFile, writeTextParts } from "./JsonFiles.js"; import type { ArtifactResourceScope } from "../artifacts/ArtifactResourceScope.js"; -type EvidenceReadFailure = +type EvidenceBundleFileFailure = | EvidenceFileError | EvidenceIntegrityError | AnalysisResourceConstraintError | AnalysisCancelledError; -type EvidenceWriteFailure = - | EvidenceFileError - | EvidenceIntegrityError - | AnalysisCancelledError - | AnalysisResourceConstraintError; /** Project a published bundle with record and residual-unknown revision counts. */ export const evidenceBundleExportReceipt = ( @@ -59,7 +54,7 @@ export const readEvidenceBundle = ( signal?: AbortSignal, resources?: ArtifactResourceScope, operation = "import_evidence_bundle", -): Promise> => +): Promise> => readBundle(path, false, signal, resources, operation); /** Read an authenticated immutable snapshot for a subsequent session import. */ @@ -68,7 +63,7 @@ export const readImmutableEvidenceBundle = ( signal?: AbortSignal, resources?: ArtifactResourceScope, operation = "import_evidence_bundle", -): Promise> => +): Promise> => readBundle(path, true, signal, resources, operation); /** Join both comparison reads and preserve cleanup owned by either failed input. */ @@ -109,7 +104,7 @@ const readBundle = async ( signal?: AbortSignal, resources?: ArtifactResourceScope, operation = "import_evidence_bundle", -): Promise> => { +): Promise> => { if (signal?.aborted) return err( new AnalysisCancelledError(operation, { @@ -135,7 +130,7 @@ export const authenticateEvidenceBundleValue = ( value: unknown, path: string, options: { readonly operation: string; readonly immutable?: boolean }, -): Result => { +): Result => { const { operation } = options; const headroom = requireAuthenticationHeadroom(value, { operation, @@ -226,7 +221,7 @@ export const writeEvidenceBundle = async ( ): Promise< Result< { readonly path: string; readonly bytes: number }, - EvidenceWriteFailure + EvidenceBundleFileFailure > > => { if (signal?.aborted === true) diff --git a/src/application/GeminiClientSettings.ts b/src/application/GeminiClientSettings.ts index a9398d1624..60b47cf169 100644 --- a/src/application/GeminiClientSettings.ts +++ b/src/application/GeminiClientSettings.ts @@ -2,6 +2,7 @@ import { realpath, stat } from "node:fs/promises"; import { dirname, resolve } from "node:path"; import { stripComments } from "jsonc-parser"; import { z } from "zod"; +import { jsonRecordSchema } from "../domain/jsonRecord.js"; import { err, ok, type Result } from "../domain/result.js"; import { PRODUCT_IDENTITY } from "../identity.js"; import type { ArtifactResourceScope } from "../artifacts/ArtifactResourceScope.js"; @@ -34,8 +35,7 @@ const folderTrustSchema = z .optional(), }) .passthrough(); -const trustRulesSchema = z.record( - z.string(), +const trustRulesSchema = jsonRecordSchema( z.enum(["TRUST_FOLDER", "TRUST_PARENT", "DO_NOT_TRUST"]), ); const sourceFailure = ( @@ -47,18 +47,18 @@ const sourceFailure = ( detail: `Gemini settings in ${path} are malformed: ${cause instanceof Error ? cause.message : String(cause)}`, }); -// Gemini resolves symlinks when possible and compares macOS/Windows paths without case. -const normalizedPath = async ( - path: string, - platform: NodeJS.Platform, -): Promise => { - const physical = await realpath(path).catch(() => path); - const absolute = resolve(physical).replaceAll("\\", "/"); +// Gemini normalizes trust-file keys before replacement and rule precedence. +const normalizedPath = (path: string, platform: NodeJS.Platform): string => { + const absolute = resolve(path).replaceAll("\\", "/"); return platform === "win32" || platform === "darwin" ? absolute.toLowerCase() : absolute; }; +// Applicability additionally resolves symlinks, without changing rule identity. +const physicalPath = async (path: string, platform: NodeJS.Platform) => + normalizedPath(await realpath(path).catch(() => path), platform); + const workspaceTrusted = async ( settings: NonNullable, sources: readonly SettingsSource[], @@ -78,21 +78,29 @@ const workspaceTrusted = async ( ); if (!text.ok) return text; if (text.value === undefined) return ok(false); - let rules: z.output; + let rules: ReadonlyMap[string]>; try { // Gemini strips comments, but rejects trailing commas, BOMs and invalid trust levels. - rules = trustRulesSchema.parse(JSON.parse(stripComments(text.value))); + const parsed = trustRulesSchema.parse( + JSON.parse(stripComments(text.value)), + ); + rules = new Map( + Object.entries(parsed).map(([path, level]) => [ + normalizedPath(path, settings.platform), + level, + ]), + ); } catch (cause: unknown) { return err(sourceFailure(settings.trustedFoldersPath, cause)); } - const workspace = await normalizedPath( + const workspace = await physicalPath( settings.workspaceDirectory, settings.platform, ); let longest = -1; let trusted = false; - for (const [path, level] of Object.entries(rules)) { - const effective = await normalizedPath( + for (const [path, level] of rules) { + const effective = await physicalPath( level === "TRUST_PARENT" ? dirname(path) : path, settings.platform, ); @@ -138,11 +146,11 @@ export const readClientPolicyBlock = async ( if (parsed.value !== undefined) sources.push({ path, document: parsed.value }); } - const workspace = await normalizedPath( + const workspace = await physicalPath( settings.workspaceDirectory, settings.platform, ); - const home = await normalizedPath( + const home = await physicalPath( dirname(dirname(client.configPath)), settings.platform, ); diff --git a/src/application/GooseConfigurationYaml.test.ts b/src/application/GooseConfigurationYaml.test.ts index 96a9356acc..c12ceb116b 100644 --- a/src/application/GooseConfigurationYaml.test.ts +++ b/src/application/GooseConfigurationYaml.test.ts @@ -4,7 +4,6 @@ import { parseDocument } from "yaml"; import { ConfigurationError } from "../domain/configurationErrors.js"; import { editGooseConfigurationYaml, - GooseConfigurationYamlError, parseGooseConfigurationYaml, stringifyGooseConfigurationYaml, } from "./GooseConfigurationYaml.js"; @@ -111,11 +110,15 @@ describe("Goose YAML 1.2 source facts", () => { } catch (cause: unknown) { failure = cause; } - expect(failure).toBeInstanceOf(GooseConfigurationYamlError); + expect(failure).toBeInstanceOf(ConfigurationError); expect(failure).toMatchObject({ kind: "shadowed_member", path }); + expect(failure).toHaveProperty( + "message", + `Goose YAML contains a duplicate mapping member at ${path.join(".")}`, + ); expect(() => editGooseConfigurationYaml(source, ["extensions", "rea"], extension), - ).toThrow(GooseConfigurationYamlError); + ).toThrow(`duplicate mapping member at ${path.join(".")}`); }, ); diff --git a/src/application/GooseConfigurationYaml.ts b/src/application/GooseConfigurationYaml.ts index 005d5df9e6..8d1ae5cd78 100644 --- a/src/application/GooseConfigurationYaml.ts +++ b/src/application/GooseConfigurationYaml.ts @@ -143,7 +143,7 @@ const gooseYamlOptions = { } as const; /** A duplicate YAML member cannot establish one selected configuration value. */ -export class GooseConfigurationYamlError extends ConfigurationError { +class GooseConfigurationYamlError extends ConfigurationError { readonly kind = "shadowed_member"; constructor( readonly path: readonly string[], diff --git a/src/application/GuidanceAccess.test.ts b/src/application/GuidanceAccess.test.ts index cbdf5168a7..17b2c88ce2 100644 --- a/src/application/GuidanceAccess.test.ts +++ b/src/application/GuidanceAccess.test.ts @@ -1,7 +1,6 @@ import { expect, it } from "vitest"; import { ArtifactResourceScope } from "../artifacts/ArtifactResourceScope.js"; import { RegularFileCleanupFailure } from "../filesystem/OwnedRegularFile.js"; -import { ArtifactReaderFailure } from "../artifacts/ArtifactReader.js"; import { MANAGED_SKILL_FILES } from "./SetupSkill.js"; import { searchGuidanceOutputSchema } from "../contracts/guidanceContracts.js"; import { RetainedRegularFileCleanupFailure } from "./RegularFileRead.js"; diff --git a/src/application/HopperDownload.ts b/src/application/HopperDownload.ts index f92d143a83..f7094c80ba 100644 --- a/src/application/HopperDownload.ts +++ b/src/application/HopperDownload.ts @@ -49,13 +49,13 @@ export type HopperRequestFailure = HopperDownloadFailure & { export const HOPPER_DOWNLOAD_BYTES = 100_000_000; /** Longest wait for the response or its next bytes before a download stalls. */ -export const HOPPER_DOWNLOAD_STALL_MS = 30_000; +const HOPPER_DOWNLOAD_STALL_MS = 30_000; /** * Overall bound for one download. A slow connection that keeps delivering * bytes is bounded here rather than by a deadline sized for fast networks. */ -export const HOPPER_DOWNLOAD_TIMEOUT_MS = 15 * 60_000; +const HOPPER_DOWNLOAD_TIMEOUT_MS = 15 * 60_000; /** Small release feed and key requests must not use the package bounds. */ export const HOPPER_METADATA_TIMEOUT_MS = 10_000; diff --git a/src/application/HopperReleaseFeed.ts b/src/application/HopperReleaseFeed.ts index e6da3d70d5..e4c9635005 100644 --- a/src/application/HopperReleaseFeed.ts +++ b/src/application/HopperReleaseFeed.ts @@ -123,6 +123,8 @@ export class HopperReleaseError extends Error { | "release_metadata" | "download" | "integrity" + /** The authenticated package's launcher differs from the feed's main_exe_hash. */ + | "launcher_digest_mismatch" | "publisher_verification_failed" | "verification_unavailable" | "unsupported_hopper_build" diff --git a/src/application/LinuxHopper.test.ts b/src/application/LinuxHopper.test.ts index 9c00fbad3d..a7b56f99a2 100644 --- a/src/application/LinuxHopper.test.ts +++ b/src/application/LinuxHopper.test.ts @@ -4,9 +4,8 @@ import { join } from "node:path"; import { describe, expect, it, vi } from "vitest"; import { - formatCommandForDisplay, installLinuxHopper, - linuxPackageManagerCommands, + linuxHopperInstallDisclosure, linuxSharedLibrariesAvailable, parseLinuxDistribution, supportsReaHost, @@ -31,19 +30,6 @@ import * as nativeCommands from "../process/ExecFileOutput.js"; import * as pythonRuntime from "./LinuxHopperPython.js"; import { createTestTempDirectory } from "../../tests/fixtures/temporaryDirectory.js"; -describe("Linux package-manager command display", () => { - it("preserves argument boundaries for paths with spaces and quotes", () => { - expect( - formatCommandForDisplay("pkexec", [ - "pacman", - "-U", - "/tmp/rea test/hopper.arch", - "apostrophe'path", - ]), - ).toBe('pkexec pacman -U "/tmp/rea test/hopper.arch" "apostrophe\'path"'); - }); -}); - class RecordingLinuxHost implements LinuxHopperInstallHost { distributionValue: LinuxDistribution | undefined = { id: "ubuntu", @@ -263,47 +249,34 @@ describe("Linux Hopper installation", () => { ] as const)( "selects the %s native package manager and dependencies", (family, executable, packages) => { - expect( - linuxPackageManagerCommands(family, "/tmp/Hopper package", true), - ).toContainEqual( - expect.objectContaining({ - executable, - args: expect.arrayContaining([...packages]), - }), + const root = linuxHopperInstallDisclosure(family, true).commands; + const nonRoot = linuxHopperInstallDisclosure(family, false).commands; + expect(root.some((command) => command.startsWith(`${executable} `))).toBe( + true, ); + for (const dependency of packages) + expect( + root.some((command) => command.split(" ").includes(dependency)), + ).toBe(true); expect( - linuxPackageManagerCommands(family, "/tmp/Hopper package", false), - ).toContainEqual( - expect.objectContaining({ - executable: "pkexec", - args: expect.arrayContaining([executable, "/tmp/Hopper package"]), - }), - ); + nonRoot.some( + (command) => + command.startsWith(`pkexec ${executable} `) && + command.includes(`""`), + ), + ).toBe(true); }, ); it.each([true, false])( "keeps installed apt dependencies at their versions while Hopper may upgrade (root: %s)", (isRoot) => { - const prefix = isRoot ? [] : ["pkexec"]; + const prefix = isRoot ? "" : "pkexec "; expect( - linuxPackageManagerCommands("deb", "/tmp/Hopper package", isRoot).map( - ({ executable, args }) => [executable, ...args], - ), + linuxHopperInstallDisclosure("deb", isRoot).commands.slice(0, 2), ).toEqual([ - [ - ...prefix, - "apt-get", - "install", - "-y", - "--no-upgrade", - "xvfb", - "xauth", - "python3", - "libx11-6", - "libxtst6", - ], - [...prefix, "apt-get", "install", "-y", "/tmp/Hopper package"], + `${prefix}apt-get install -y --no-upgrade xvfb xauth python3 libx11-6 libxtst6`, + `${prefix}apt-get install -y ""`, ]); }, ); diff --git a/src/application/LinuxHopper.ts b/src/application/LinuxHopper.ts index 34d2007c89..3aa683c58e 100644 --- a/src/application/LinuxHopper.ts +++ b/src/application/LinuxHopper.ts @@ -226,6 +226,7 @@ export type LinuxHopperInstallResult = | "archive_write" | "launcher_inspection" | "integrity" + | "launcher_digest_mismatch" | "authorization_or_package_manager" | "launcher_missing" | "runtime_dependencies" @@ -511,7 +512,7 @@ export const installLinuxHopper = async ( }; /** Canonical launcher path for a legacy user-local Linux Hopper installation. */ -export const linuxHopperLauncherPath = (home: string): string => +const linuxHopperLauncherPath = (home: string): string => appendCallerPath(home, ".local/share/rea/hopper/bin/Hopper", "linux"); /** REA's managed Linux install path, preferred when no explicit launcher is set. */ @@ -796,7 +797,7 @@ const runPackageManagerCommand = async ( }; /** Preserve argument boundaries in a command shown to the user. */ -export const formatCommandForDisplay = ( +const formatCommandForDisplay = ( executable: string, args: readonly string[], ): string => @@ -807,7 +808,7 @@ export const formatCommandForDisplay = ( .join(" "); /** Select the native package manager and authorization boundary without shell evaluation. */ -export const linuxPackageManagerCommands = ( +const linuxPackageManagerCommands = ( family: LinuxPackageFamily, archive: string, isRoot: boolean, diff --git a/src/application/LinuxHopperPackageRuntime.test.ts b/src/application/LinuxHopperPackageRuntime.test.ts index b3cda7929f..992c821df8 100644 --- a/src/application/LinuxHopperPackageRuntime.test.ts +++ b/src/application/LinuxHopperPackageRuntime.test.ts @@ -6,16 +6,11 @@ import { err, ok } from "../domain/result.js"; import * as pythonRuntime from "./LinuxHopperPython.js"; import { linuxHopperInstallDisclosure, - linuxPackageManagerCommands, systemLinuxHopperInstallHost, } from "./LinuxHopper.js"; /** Package transactions that install Hopper before any runtime library repair. */ -const debCommands = linuxPackageManagerCommands( - "deb", - "/tmp/verified-hopper.deb", - false, -).length; +const debCommands = 2; const missingPython = { interpreter: "/usr/bin/python3", @@ -41,6 +36,34 @@ const commandSuccess = { }; describe("Linux Hopper package runtime", () => { + it("preserves package path boundaries in the reported failed command", async () => { + const directory = await createTestTempDirectory("rea-hopper-command-"); + const host = systemLinuxHopperInstallHost( + { XDG_CACHE_HOME: directory }, + async () => undefined, + ); + const archive = "/tmp/rea test/hopper's package.rpm"; + const command = vi + .spyOn(boundedCommands, "execFileBoundedOutput") + .mockResolvedValue({ + exitCode: 1, + stdout: "", + stderr: "fixture failure", + outputTruncated: false, + }); + try { + const result = await host.installPackage("rpm", archive); + expect(command.mock.calls[0]?.[1]).toContain(archive); + expect(result).toMatchObject({ + status: "failed", + failure: { command: expect.stringContaining(`"${archive}"`) }, + }); + } finally { + command.mockRestore(); + await host.close?.(); + } + }); + it.each([ { name: "installs and verifies the matching future Python library", diff --git a/src/application/LinuxHopperPublisher.ts b/src/application/LinuxHopperPublisher.ts index aae258f471..a204fec15b 100644 --- a/src/application/LinuxHopperPublisher.ts +++ b/src/application/LinuxHopperPublisher.ts @@ -261,7 +261,7 @@ export class LinuxHopperPublisher { if (mainExeHash !== release.main_exe_hash) return err( new HopperReleaseError( - "integrity", + "launcher_digest_mismatch", `The signed ${family} package launcher SHA-256 ${mainExeHash} differs from the release main_exe_hash ${release.main_exe_hash}.`, ), ); diff --git a/src/application/MacHopperPaths.test.ts b/src/application/MacHopperPaths.test.ts index 15a32dc0aa..83be4a56b6 100644 --- a/src/application/MacHopperPaths.test.ts +++ b/src/application/MacHopperPaths.test.ts @@ -11,7 +11,7 @@ import { import { tmpdir } from "node:os"; import { join, posix, relative } from "node:path"; import { installMacHopperBundle } from "./MacHopper.js"; -import { linuxHopperLauncherPath } from "./LinuxHopper.js"; +import { linuxHopperLauncherCandidates } from "./LinuxHopper.js"; import { homeDirectoryFromEnvironment } from "../config/homeDirectory.js"; import { describe, expect, it } from "vitest"; @@ -19,34 +19,17 @@ import { localMacHopperRenamedLauncherCandidates, macHopperCanonicalLauncherCandidates, macHopperUserApplicationPath, - macHopperRenamedLauncherCandidates, readMacHopperRenamedLauncherCandidates, } from "./MacHopperPaths.js"; describe("macOS Hopper launcher paths", () => { const home = "/fixture/home"; - it("keeps canonical system and user candidates ahead of renamed bundles", () => { + it("keeps canonical system and user candidates in order", () => { expect(macHopperCanonicalLauncherCandidates(home)).toEqual([ "/Applications/Hopper Disassembler.app/Contents/MacOS/hopper", "/fixture/home/Applications/Hopper Disassembler.app/Contents/MacOS/hopper", ]); - expect( - macHopperRenamedLauncherCandidates( - home, - new Map([ - [ - "/Applications", - ["Hopper z.app", "Hopper a.app", "Hopper Disassembler.app"], - ], - ["/fixture/home/Applications", ["Hopper User.app"]], - ]), - ), - ).toEqual([ - "/Applications/Hopper a.app/Contents/MacOS/hopper", - "/Applications/Hopper z.app/Contents/MacOS/hopper", - "/fixture/home/Applications/Hopper User.app/Contents/MacOS/hopper", - ]); }); it.skipIf(process.platform === "win32")( @@ -84,7 +67,7 @@ describe("macOS Hopper launcher paths", () => { expect(macHopperCanonicalLauncherCandidates(home)[1]).toBe( `${home}/Applications/Hopper Disassembler.app/Contents/MacOS/hopper`, ); - expect(linuxHopperLauncherPath(home)).toBe( + expect(linuxHopperLauncherCandidates(home)[1]).toBe( `${home}/.local/share/rea/hopper/bin/Hopper`, ); const source = join(temporary, "source.app"); @@ -154,13 +137,17 @@ describe("macOS Hopper launcher paths", () => { recursive: true, }); await mkdir(join(applications, "Hopper Disassembler.app")); + await mkdir(join(applications, "Hopper Alpha.app")); + await mkdir(join(applications, "Hopper Zeta.app")); await writeFile(join(applications, "Hopper File.app"), ""); const runtime = [...localMacHopperRenamedLauncherCandidates(home)]; expect(runtime.filter((path) => path.startsWith(home))).toEqual([ + posix.join(home, "Applications/Hopper Alpha.app/Contents/MacOS/hopper"), posix.join( home, "Applications/Hopper Preview.app/Contents/MacOS/hopper", ), + posix.join(home, "Applications/Hopper Zeta.app/Contents/MacOS/hopper"), ]); expect(await readMacHopperRenamedLauncherCandidates(home)).toEqual( runtime, diff --git a/src/application/MacHopperPaths.ts b/src/application/MacHopperPaths.ts index e30705cf89..643051812d 100644 --- a/src/application/MacHopperPaths.ts +++ b/src/application/MacHopperPaths.ts @@ -43,7 +43,7 @@ export const macHopperCanonicalLauncherCandidates = ( ]; /** Build deterministic candidates from the discovered app bundle names. */ -export const macHopperRenamedLauncherCandidates = ( +const macHopperRenamedLauncherCandidates = ( homeDirectory: string, bundleNamesByRoot: ReadonlyMap, ): readonly string[] => diff --git a/src/application/McpClientRegistration.test.ts b/src/application/McpClientRegistration.test.ts index 5074fcae42..e916e55c6d 100644 --- a/src/application/McpClientRegistration.test.ts +++ b/src/application/McpClientRegistration.test.ts @@ -12,6 +12,7 @@ import { join, sep } from "node:path"; import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import { createTestTempDirectory } from "../../tests/fixtures/temporaryDirectory.js"; +import { awaitAcknowledgement } from "../../tests/fixtures/lifecycleAcknowledgement.js"; import { ok } from "../domain/result.js"; import { ArtifactResourceScope } from "../artifacts/ArtifactResourceScope.js"; let resources: ArtifactResourceScope; @@ -612,17 +613,22 @@ describe("Registration cancellation and sibling outcomes", () => { entered(); await blocked; }); - await admitted; - const controller = new AbortController(); - const queued = register(path, launcher, controller.signal); - controller.abort("queued cancellation"); - unblock(); - await first; - expect(await queued).toMatchObject({ - ok: false, - error: { _tag: "AnalysisCancelledError" }, - }); - expect(await readFile(path, "utf8")).toBe("{}"); + try { + await awaitAcknowledgement(admitted, first, "registration transaction"); + const controller = new AbortController(); + const queued = register(path, launcher, controller.signal); + controller.abort("queued cancellation"); + unblock(); + await first; + expect(await queued).toMatchObject({ + ok: false, + error: { _tag: "AnalysisCancelledError" }, + }); + expect(await readFile(path, "utf8")).toBe("{}"); + } finally { + unblock(); + await first; + } }); it("keeps successful siblings after ordinary failures and marks work not started after cancellation", async () => { diff --git a/src/application/McpRegistrationCatalog.test.ts b/src/application/McpRegistrationCatalog.test.ts index 28cc45f7a3..8f1d2f000c 100644 --- a/src/application/McpRegistrationCatalog.test.ts +++ b/src/application/McpRegistrationCatalog.test.ts @@ -5,7 +5,28 @@ import { z } from "zod"; import { createTestTempDirectory } from "../../tests/fixtures/temporaryDirectory.js"; import { createTestProcessRegistry } from "../../tests/support/process/processFixture.js"; -import { resolveMcpRegistrationClientId } from "./McpRegistrationCatalog.js"; +import { resolveMcpRegistrationClients } from "./McpRegistrationCatalog.js"; +import { ArtifactResourceScope } from "../artifacts/ArtifactResourceScope.js"; + +const selectAgent = async (agent: string) => { + const root = await createTestTempDirectory("rea-mcp-alias-"); + const resources = new ArtifactResourceScope(); + try { + return await resolveMcpRegistrationClients( + { + agents: [agent], + global: true, + cwd: root, + home: root, + platform: process.platform, + environment: {}, + }, + resources, + ); + } finally { + await resources.close(); + } +}; it.each([ ["cline-vscode", "cline"], @@ -23,15 +44,22 @@ it.each([ ["kiro", "kiro-cli"], ["mastra", "mastracode"], ["pi-agent", "pi"], -])("retains helper alias %s as %s", (alias, expected) => { - expect(resolveMcpRegistrationClientId(alias.toUpperCase())).toBe(expected); +])("resolves registration alias %s as %s", async (alias, expected) => { + const result = await selectAgent(alias.toUpperCase()); + if (!result.ok) throw result.error; + expect(result.value.map(({ id }) => id)).toEqual([expected]); }); -it("does not treat prototype names or unknown strings as client identities", () => { - expect(resolveMcpRegistrationClientId("__proto__")).toBeUndefined(); - expect(resolveMcpRegistrationClientId("constructor")).toBeUndefined(); - expect(resolveMcpRegistrationClientId("unknown-client")).toBeUndefined(); -}); +it.each(["__proto__", "constructor", "unknown-client"])( + "rejects unknown registration identity %s", + async (agent) => { + const result = await selectAgent(agent); + expect(result).toMatchObject({ + ok: false, + error: { _tag: "ConfigurationError" }, + }); + }, +); const clientSchema = z.object({ id: z.string(), @@ -256,133 +284,136 @@ const REVIEWED_CLIENT_IDS = [ "amp", ]; -it("resolves selected-environment paths and scopes without external catalog readers", async () => { - const home = await createTestTempDirectory("rea-registration-catalog-"); - const processes = createTestProcessRegistry(); - try { - await mkdir(join(home, "config")); - const child = await processes.run( - process.execPath, - ["--import", "tsx", "--input-type=module", "--eval", CATALOG_PROGRAM], - { - timeoutMs: 50_000, - env: { - PATH: process.env.PATH, - SystemRoot: process.env.SystemRoot, - WINDIR: process.env.WINDIR, - TMPDIR: process.env.TMPDIR, - TMP: process.env.TMP, - TEMP: process.env.TEMP, - HOME: home, - USERPROFILE: home, - XDG_CONFIG_HOME: join(home, "config"), - XDG_DATA_HOME: join(home, "data"), - XDG_CACHE_HOME: join(home, "cache"), - XDG_STATE_HOME: join(home, "state"), - APPDATA: join(home, "appdata"), - LOCALAPPDATA: join(home, "localappdata"), +it( + "resolves selected-environment paths and scopes without external catalog readers", + { tags: ["long-running"] }, + async () => { + const home = await createTestTempDirectory("rea-registration-catalog-"); + const processes = createTestProcessRegistry(); + try { + await mkdir(join(home, "config")); + const child = await processes.run( + process.execPath, + ["--import", "tsx", "--input-type=module", "--eval", CATALOG_PROGRAM], + { + env: { + PATH: process.env.PATH, + SystemRoot: process.env.SystemRoot, + WINDIR: process.env.WINDIR, + TMPDIR: process.env.TMPDIR, + TMP: process.env.TMP, + TEMP: process.env.TEMP, + HOME: home, + USERPROFILE: home, + XDG_CONFIG_HOME: join(home, "config"), + XDG_DATA_HOME: join(home, "data"), + XDG_CACHE_HOME: join(home, "cache"), + XDG_STATE_HOME: join(home, "state"), + APPDATA: join(home, "appdata"), + LOCALAPPDATA: join(home, "localappdata"), + }, }, - }, - ); - expect(child.exitCode, child.stderr).toBe(0); - const result = catalogObservationsSchema.parse(JSON.parse(child.stdout)); - assertKiloNativeLayers(result, home); - expect(result.empty.ok).toBe(true); - if (!result.empty.ok) throw new Error(result.empty.error.message); - expect(result.empty.value.map((client) => client.id)).toEqual( - REVIEWED_CLIENT_IDS, - ); - expect(result.layeredKilo.ok).toBe(true); - if (!result.layeredKilo.ok) - throw new Error(result.layeredKilo.error.message); - const kiloLayers = [ - "config.json", - "kilo.json", - "kilo.jsonc", - "opencode.json", - "opencode.jsonc", - ].map((name) => join(home, "layered", "kilo", name)); - expect(result.layeredKilo.value[0]).toMatchObject({ - configPath: kiloLayers.at(-1), - configPaths: kiloLayers, - }); - expect(result.layeredOpenCode.ok).toBe(true); - if (!result.layeredOpenCode.ok) - throw new Error(result.layeredOpenCode.error.message); - expect(result.layeredOpenCode.value[0]?.configPaths).toEqual( - ["config.json", "opencode.json", "opencode.jsonc"].map((name) => - join(home, "config", "opencode", name), - ), - ); - expect(result.explicit.ok).toBe(true); - if (!result.explicit.ok) throw new Error(result.explicit.error.message); - expect(result.explicit.value.map((client) => client.id)).toEqual([ - "codex", - "amp", - "gemini-cli", - "fx", - "windsurf", - ]); - expect(result.explicit.value[0]?.configPath).toBe( - join(home, "selected-codex", "config.toml"), - ); - expect( - result.explicit.value.find((client) => client.id === "fx"), - ).toMatchObject({ format: "fx", privateProfile: true }); - expect(result.detected.ok).toBe(true); - if (!result.detected.ok) throw new Error(result.detected.error.message); - expect(result.detected.value.map((client) => client.id)).toEqual([ - "claude-code", - "cursor", - "amp", - ]); - expect(result.project.ok, JSON.stringify(result.project)).toBe(true); - if (!result.project.ok) throw new Error(result.project.error.message); - expect(result.project.value[0]?.format).toBe("copilot_cli_project"); - expect( - result.project.value.map(({ configPath, scope }) => ({ - configPath, - scope, - })), - ).toEqual([ - { - configPath: join(home, "project", ".github", "mcp.json"), - scope: "project", - }, - ...(process.platform === "win32" - ? [] - : [ - { - configPath: join(home, "project", ".kilocode", "kilo.jsonc"), - scope: "project", - }, - ]), - { - configPath: join(home, "project", "opencode.jsonc"), - scope: "project", - }, - ]); - for (const refused of [ - result.onlyGlobal, - result.defaultProject, - result.projectAmp, - result.unknown, - ...(result.blockedCandidate === null ? [] : [result.blockedCandidate]), - result.unknownDetection, - ]) { - expect(refused.ok).toBe(false); - if (refused.ok) throw new Error("Expected preflight refusal."); - expect(refused.error.tag).toBe("ConfigurationError"); + ); + expect(child.exitCode, child.stderr).toBe(0); + const result = catalogObservationsSchema.parse(JSON.parse(child.stdout)); + assertKiloNativeLayers(result, home); + expect(result.empty.ok).toBe(true); + if (!result.empty.ok) throw new Error(result.empty.error.message); + expect(result.empty.value.map((client) => client.id)).toEqual( + REVIEWED_CLIENT_IDS, + ); + expect(result.layeredKilo.ok).toBe(true); + if (!result.layeredKilo.ok) + throw new Error(result.layeredKilo.error.message); + const kiloLayers = [ + "config.json", + "kilo.json", + "kilo.jsonc", + "opencode.json", + "opencode.jsonc", + ].map((name) => join(home, "layered", "kilo", name)); + expect(result.layeredKilo.value[0]).toMatchObject({ + configPath: kiloLayers.at(-1), + configPaths: kiloLayers, + }); + expect(result.layeredOpenCode.ok).toBe(true); + if (!result.layeredOpenCode.ok) + throw new Error(result.layeredOpenCode.error.message); + expect(result.layeredOpenCode.value[0]?.configPaths).toEqual( + ["config.json", "opencode.json", "opencode.jsonc"].map((name) => + join(home, "config", "opencode", name), + ), + ); + expect(result.explicit.ok).toBe(true); + if (!result.explicit.ok) throw new Error(result.explicit.error.message); + expect(result.explicit.value.map((client) => client.id)).toEqual([ + "codex", + "amp", + "gemini-cli", + "fx", + "windsurf", + ]); + expect(result.explicit.value[0]?.configPath).toBe( + join(home, "selected-codex", "config.toml"), + ); + expect( + result.explicit.value.find((client) => client.id === "fx"), + ).toMatchObject({ format: "fx", privateProfile: true }); + expect(result.detected.ok).toBe(true); + if (!result.detected.ok) throw new Error(result.detected.error.message); + expect(result.detected.value.map((client) => client.id)).toEqual([ + "claude-code", + "cursor", + "amp", + ]); + expect(result.project.ok, JSON.stringify(result.project)).toBe(true); + if (!result.project.ok) throw new Error(result.project.error.message); + expect(result.project.value[0]?.format).toBe("copilot_cli_project"); + expect( + result.project.value.map(({ configPath, scope }) => ({ + configPath, + scope, + })), + ).toEqual([ + { + configPath: join(home, "project", ".github", "mcp.json"), + scope: "project", + }, + ...(process.platform === "win32" + ? [] + : [ + { + configPath: join(home, "project", ".kilocode", "kilo.jsonc"), + scope: "project", + }, + ]), + { + configPath: join(home, "project", "opencode.jsonc"), + scope: "project", + }, + ]); + for (const refused of [ + result.onlyGlobal, + result.defaultProject, + result.projectAmp, + result.unknown, + ...(result.blockedCandidate === null ? [] : [result.blockedCandidate]), + result.unknownDetection, + ]) { + expect(refused.ok).toBe(false); + if (refused.ok) throw new Error("Expected preflight refusal."); + expect(refused.error.tag).toBe("ConfigurationError"); + } + expect(result.cancelled).toMatchObject({ + ok: false, + error: { tag: "AnalysisCancelledError" }, + }); + } finally { + await processes.shutdown(); + await rm(home, { recursive: true, force: true }); } - expect(result.cancelled).toMatchObject({ - ok: false, - error: { tag: "AnalysisCancelledError" }, - }); - } finally { - await processes.shutdown(); - await rm(home, { recursive: true, force: true }); - } -}, 60_000); + }, +); const assertKiloNativeLayers = ( result: z.infer, diff --git a/src/application/McpRegistrationCatalog.ts b/src/application/McpRegistrationCatalog.ts index 4ddd82cd27..d1bb301684 100644 --- a/src/application/McpRegistrationCatalog.ts +++ b/src/application/McpRegistrationCatalog.ts @@ -356,7 +356,7 @@ const ALIASES = { const isClientId = (value: string): value is McpRegistrationClientId => Object.hasOwn(MCP_REGISTRATION_CLIENTS, value); -export const resolveMcpRegistrationClientId = ( +const resolveMcpRegistrationClientId = ( input: string, ): McpRegistrationClientId | undefined => { const lower = input.toLowerCase(); diff --git a/src/application/MsixPackageStorage.ts b/src/application/MsixPackageStorage.ts index 9eb3dede08..13dbefba8a 100644 --- a/src/application/MsixPackageStorage.ts +++ b/src/application/MsixPackageStorage.ts @@ -54,7 +54,7 @@ const pathWithin = (path: string, root: string): boolean => { * processes see those files at virtual AppData locations; other processes do * not, and Windows removes them with the package. */ -export const packagePrivateStorageFamily = ( +const packagePrivateStorageFamily = ( canonicalPath: string, packagesDirectory: string, ): string | undefined => { diff --git a/src/application/OperationAlternatives.ts b/src/application/OperationAlternatives.ts new file mode 100644 index 0000000000..d98cc78d76 --- /dev/null +++ b/src/application/OperationAlternatives.ts @@ -0,0 +1,84 @@ +import type { AnalysisOperation } from "./AnalysisProvider.js"; + +/** One operation that observes part of what a missing operation answers. */ +interface OperationAlternative { + readonly operation: AnalysisOperation; + /** What it returns, as a noun phrase. */ + readonly observes: string; +} + +/** + * Follow-ups for one question shape that an undeclared operation would have + * answered. They are conditional rather than substitutes: `limitation` + * states what none of them establishes. + */ +interface OperationAlternativeRelation { + readonly question: string; + readonly alternatives: readonly OperationAlternative[]; + readonly limitation: string; +} + +const INSTRUCTION_OBSERVATIONS: OperationAlternativeRelation = { + question: "instruction-level questions", + alternatives: [ + { + operation: "inspect_native_instruction", + observes: + "the decoded instruction or data at one exact address, with its length and static flow", + }, + { + operation: "read_function_instructions", + observes: "one analyzed procedure's instruction listing in address order", + }, + { + operation: "procedure_assembly", + observes: "one analyzed procedure's assembly text", + }, + ], + limitation: + "None navigates arbitrary analyzed objects such as data or addresses outside a procedure, and neither listing order nor address plus length proves executed control flow.", +}; + +/** Operations absent from this relation keep their consumer's generic advice. */ +const OPERATION_ALTERNATIVES: ReadonlyMap< + string, + OperationAlternativeRelation +> = new Map([ + ["next_address", INSTRUCTION_OBSERVATIONS], + ["prev_address", INSTRUCTION_OBSERVATIONS], +]); + +/** Declared alternatives the caller can run now, in declaration order. */ +export interface OfferedOperationAlternatives { + readonly operations: readonly AnalysisOperation[]; + /** Each offer's scope, what none establishes, and the provider choice. */ + readonly remediation: string; +} + +/** + * Select the declared alternatives for an operation the current provider does + * not declare. `isOffered` holds only for operations that provider makes + * available and the caller advertises. Undefined means none qualifies. + */ +export const offeredOperationAlternatives = ( + operation: string, + isOffered: (alternative: AnalysisOperation) => boolean, +): OfferedOperationAlternatives | undefined => { + const relation = OPERATION_ALTERNATIVES.get(operation); + if (relation === undefined) return undefined; + const offered = relation.alternatives.filter((alternative) => + isOffered(alternative.operation), + ); + if (offered.length === 0) return undefined; + const described = offered.map( + ({ operation: alternative, observes }) => `${alternative} (${observes})`, + ); + const listed = + described.length === 1 + ? described[0] + : `${described.slice(0, -1).join(", ")} and ${described.at(-1)}`; + return { + operations: offered.map((alternative) => alternative.operation), + remediation: `For ${relation.question}, the current provider offers ${listed}. ${relation.limitation} Running ${operation} itself requires a provider that declares it.`, + }; +}; diff --git a/src/application/Setup.fixture.ts b/src/application/Setup.fixture.ts index aac93940c3..54922c686a 100644 --- a/src/application/Setup.fixture.ts +++ b/src/application/Setup.fixture.ts @@ -23,7 +23,6 @@ import { PRODUCT_IDENTITY, SDK_IDENTITY } from "../identity.js"; import { DEFAULT_CATALOG_IDENTITY } from "../catalogIdentity.js"; import type { ClientRegistrationStatus } from "./ClientRegistrationStatus.js"; import { currentClientRegistrationCommand } from "./ClientRegistrationIdentity.js"; -import { CURRENT_ENTRYPOINT_PATH } from "../process/CurrentEntrypointPath.js"; /** Recording setup host for service-level planning and recovery tests. */ export class FakeSetupHost implements SetupHost { diff --git a/src/application/SetupCleanupOwner.test.ts b/src/application/SetupCleanupOwner.test.ts index 7c665b1f6d..0804406aba 100644 --- a/src/application/SetupCleanupOwner.test.ts +++ b/src/application/SetupCleanupOwner.test.ts @@ -101,9 +101,16 @@ it("starts child finalization synchronously and shares concurrent closes", async ]); const first = owner.close(); expect(calls).toBe(1); - expect(owner.close()).toBe(first); + const second = owner.close(); + let secondSettled = false; + void second.then(() => { + secondSettled = true; + }); + await Promise.resolve(); + expect(secondSettled).toBe(false); complete?.(); expect(await first).toBeUndefined(); + expect(await second).toBeUndefined(); expect(await owner.close()).toBeUndefined(); expect(calls).toBe(1); }); diff --git a/src/application/SetupClientConfiguration.nativeSyntax.test.ts b/src/application/SetupClientConfiguration.nativeSyntax.test.ts index ed4b7aeee1..3043a1955a 100644 --- a/src/application/SetupClientConfiguration.nativeSyntax.test.ts +++ b/src/application/SetupClientConfiguration.nativeSyntax.test.ts @@ -102,7 +102,6 @@ it("repairs equal-launcher Copilot JSONC syntax while strict JSON and standard J process.execPath, ["--import", "tsx", entrypoint], { - timeoutMs: 20_000, env: { PATH: process.env.PATH, SystemRoot: process.env.SystemRoot, diff --git a/src/application/SetupInstallFailure.test.ts b/src/application/SetupInstallFailure.test.ts index bb5c89a4dd..3ed59dc8e8 100644 --- a/src/application/SetupInstallFailure.test.ts +++ b/src/application/SetupInstallFailure.test.ts @@ -41,6 +41,20 @@ describe("macOS Hopper setup failure projection", () => { }); }); +describe("Linux Hopper launcher digest projection", () => { + it("keeps the public integrity code without advising a retry", () => { + const failure = setupInstallFailure("launcher_digest_mismatch", { + platform: "linux", + }); + expect(failure.code).toBe("integrity_mismatch"); + expect(failure.remediation).toMatch(/signature verified/u); + expect(failure.remediation).not.toMatch(/^Retry setup/u); + expect(failure.remediation).not.toBe( + setupInstallFailure("integrity", { platform: "linux" }).remediation, + ); + }); +}); + describe("Linux package-manager setup failure projection", () => { it("quotes the failing command and a sanitized bounded diagnostic", () => { const result = setupInstallFailure("authorization_or_package_manager", { diff --git a/src/application/SetupInstallFailure.ts b/src/application/SetupInstallFailure.ts index c815f0223a..3b73c57158 100644 --- a/src/application/SetupInstallFailure.ts +++ b/src/application/SetupInstallFailure.ts @@ -54,6 +54,7 @@ export type HopperInstallFailureReason = | "release_metadata" | "download" | "integrity" + | "launcher_digest_mismatch" | "authorization_or_package_manager" | "launcher_missing" | "runtime_dependencies" @@ -166,6 +167,13 @@ const stageFailure = ( remediation: "The Hopper download failed integrity verification. Retry setup; if it repeats, update REA before installing.", }; + case "launcher_digest_mismatch": + return { + status: "failed", + code: "integrity_mismatch", + remediation: + "The package signature verified, so the download is authentic, but the vendor release feed describes a different launcher build, so REA cannot identify this Hopper build and did not install it. Retrying setup or updating REA does not change the feed; report the mismatch to the Hopper vendor and rerun setup after the feed is corrected.", + }; case "authorization_or_package_manager": return { status: "failed", diff --git a/src/application/SupportedClients.ts b/src/application/SupportedClients.ts index 429dfe89d7..edbf23e17d 100644 --- a/src/application/SupportedClients.ts +++ b/src/application/SupportedClients.ts @@ -689,6 +689,17 @@ export const SUPPORTED_CLIENT_DEFINITIONS = [ join(env.GEMINI_CLI_HOME || home, ".gemini", "settings.json"), markerPath: ({ home, env }: ClientPathContext) => join(env.GEMINI_CLI_HOME || home, ".gemini"), + // Antigravity keeps its configuration under the same .gemini directory, + // so the directory alone does not show Gemini CLI. Its own settings, + // trusted-folder records and session state do. + discoveryPaths: (context: ClientPathContext) => { + const root = join(context.env.GEMINI_CLI_HOME || context.home, ".gemini"); + return [ + join(root, "settings.json"), + geminiSettings(context).trustedFoldersPath, + join(root, "tmp"), + ]; + }, skillPath: ({ home, env }: ClientPathContext) => join(env.GEMINI_CLI_HOME || home, ".agents", "skills"), format: "json", diff --git a/src/application/UpdateCommandRunner.ts b/src/application/UpdateCommandRunner.ts index a273696801..9081b9b834 100644 --- a/src/application/UpdateCommandRunner.ts +++ b/src/application/UpdateCommandRunner.ts @@ -18,7 +18,7 @@ import { UpdateCommandProgress } from "./UpdateCommandProgress.js"; import type { UpdateOutput } from "./Update.js"; import { withoutPackageRunnerIdentity } from "./ClientRegistrationIdentity.js"; -export const UPDATE_COMMAND_OUTPUT_BYTES = 4 * 1024 * 1024; +const UPDATE_COMMAND_OUTPUT_BYTES = 4 * 1024 * 1024; const UPDATE_PROBE_TIMEOUT_MS = 10_000; /** The runner owns whether the process started; callers need not infer it from text. */ diff --git a/src/application/UpdateRuntime.ts b/src/application/UpdateRuntime.ts index 4e04bed385..0bf27d1355 100644 --- a/src/application/UpdateRuntime.ts +++ b/src/application/UpdateRuntime.ts @@ -144,7 +144,7 @@ const INVALID_NPM_RELEASE_METADATA = "Invalid npm release metadata: expected a version string or a single-element version array."; /** bun info arguments for dist-tags.latest; Bun resolves its configured registry. */ -export const BUN_LATEST_VERSION_COMMAND: readonly string[] = [ +const BUN_LATEST_VERSION_COMMAND: readonly string[] = [ "bun", "info", PRODUCT_IDENTITY.packageName, @@ -173,7 +173,7 @@ const npmLatestVersionCommand = ( */ const parseNpmLatestVersion = (metadata: string): Result => { const decoded = safeParseJson(metadata); - if (!decoded.ok) return err(decoded.error); + if (!decoded.ok) return decoded; const version = readNpmLatestVersion(decoded.value); return version === undefined ? err(INVALID_NPM_RELEASE_METADATA) diff --git a/src/application/WorkflowEvidence.ts b/src/application/WorkflowEvidence.ts index 0928d904aa..6d6127f0e8 100644 --- a/src/application/WorkflowEvidence.ts +++ b/src/application/WorkflowEvidence.ts @@ -217,7 +217,7 @@ export const executeComposedWorkflow = async ( }; /** Scope of the exact-address reference lookups composed by trace_feature. */ -export const TRACE_FEATURE_REFERENCE_LIMITATION = +const TRACE_FEATURE_REFERENCE_LIMITATION = "Reference queries cover only each matched start address, not interior bytes of strings and procedures. Empty references and truncated: false do not establish complete reference coverage."; /** Build the shared Evidence record for a successful composed binary workflow. */ diff --git a/src/application/binary/AnalysisSnapshotCache.test.ts b/src/application/binary/AnalysisSnapshotCache.test.ts index 70b196b7ed..97566e5f0f 100644 --- a/src/application/binary/AnalysisSnapshotCache.test.ts +++ b/src/application/binary/AnalysisSnapshotCache.test.ts @@ -31,7 +31,6 @@ import type { } from "../AnalysisProvider.js"; import { AnalysisSnapshotCache, - SNAPSHOT_CACHE_ENTRY_CEILING, isSnapshotCacheable, isSourceEvidenceRetainable, } from "./AnalysisSnapshotCache.js"; @@ -595,7 +594,7 @@ describe("analysis snapshot cache capacity", () => { parameters: { query: "existing" }, execution, }; - for (let index = 0; index < SNAPSHOT_CACHE_ENTRY_CEILING - 1; index += 1) + for (let index = 0; index < 9_999; index += 1) cache.recordWorkflow({ ...workflowInput, parameters: { query: String(index) }, @@ -620,9 +619,7 @@ describe("analysis snapshot cache capacity", () => { }); cache.recordWorkflow(workflowInput); expect(cache.entries()).toHaveLength(1); - expect(cache.workflowEntries()).toHaveLength( - SNAPSHOT_CACHE_ENTRY_CEILING - 1, - ); + expect(cache.workflowEntries()).toHaveLength(9_999); cache.record({ ...directInput, @@ -669,9 +666,7 @@ describe("analysis snapshot cache capacity", () => { ), "a capacity-refused workflow binding is not counted", ).toBe(0); - expect(cache.workflowEntries()).toHaveLength( - SNAPSHOT_CACHE_ENTRY_CEILING - 1, - ); + expect(cache.workflowEntries()).toHaveLength(9_999); const otherTarget = { ...target, sha256: "b".repeat(64) }; cache.recordWorkflow({ diff --git a/src/application/binary/AnalysisSnapshotCache.ts b/src/application/binary/AnalysisSnapshotCache.ts index 560f34d1d5..88156bffc1 100644 --- a/src/application/binary/AnalysisSnapshotCache.ts +++ b/src/application/binary/AnalysisSnapshotCache.ts @@ -87,7 +87,7 @@ const DOCUMENT_SCOPED_OPERATIONS: ReadonlySet = new Set( ); /** Maximum duplicated result bindings retained across direct and workflow queries. */ -export const SNAPSHOT_CACHE_ENTRY_CEILING = 10_000; +const SNAPSHOT_CACHE_ENTRY_CEILING = 10_000; const replayDocumentSelected = ( operation: AnalysisOperation, @@ -173,7 +173,7 @@ export const requiresNativeSourceAdmissionBeforeReplay = ( ): boolean => process.platform === "win32" && providerId === "ghidra"; /** Limitation that marks every observation replayed from a snapshot. */ -export const SNAPSHOT_REPLAY_LIMITATION = +const SNAPSHOT_REPLAY_LIMITATION = "Loaded from a local REA analysis snapshot; this call did not re-run the provider."; /** Bounded in-memory cache for one immutable binary identity. */ diff --git a/src/application/binary/BinarySession.ts b/src/application/binary/BinarySession.ts index aa0487b8d2..05199df91e 100644 --- a/src/application/binary/BinarySession.ts +++ b/src/application/binary/BinarySession.ts @@ -54,7 +54,10 @@ import { InvestigationRecords } from "../investigation/InvestigationRecords.js"; import { recordEvidenceSources } from "../investigation/recordEvidenceSources.js"; import { recoverProviderSourceFailure } from "../investigation/sourcePublicationRecovery.js"; import { BinarySessionRecords } from "./BinarySessionRecords.js"; -import { binarySessionStatus } from "./BinarySessionStatus.js"; +import { + binarySessionStatus, + currentRouteCapability, +} from "./BinarySessionStatus.js"; import { SessionRuntimeAvailability } from "./SessionRuntimeAvailability.js"; import { isSourceEvidenceRetainable } from "./AnalysisSnapshotCache.js"; import type { BinarySessionDetail } from "../../contracts/sessionStatusContract.js"; @@ -1396,11 +1399,6 @@ export class BinarySession : structuredClone(this.#active.target); } - activeBundleResourceRoot(): ResolvedAppBundleResourceRoot | undefined { - const root = this.#activeBundleResources?.root; - return root === undefined ? undefined : structuredClone(root); - } - override exportAnalysisSnapshot() { if (this.#activeBundleResources !== undefined) return err(new ResourceSnapshotMismatchError("export")); @@ -1567,6 +1565,8 @@ export class BinarySession parameters: arguments_, snapshotContext: options?.snapshotContext, nativeSourceAdmitted: this.#active?.nativeSourceAdmitted === true, + isOperationAvailable: (operation) => this.#currentlyAvailable(operation), + isOperationAdvertised: options?.isOperationAdvertised, unboundOperationError: (operation, route) => this.#providerRouter.unboundOperationError(operation, route), lookupSnapshot: (target, profile, operation, parameters) => @@ -1705,6 +1705,20 @@ export class BinarySession return execution; } + /** Whether the active route can run an operation now, as status reports it. */ + #currentlyAvailable(operation: string): boolean { + const active = this.#active; + const descriptor = active?.route.capabilities.get(operation); + return ( + descriptor !== undefined && + currentRouteCapability( + descriptor, + active?.target, + this.#runtimeAvailability.entries, + ).available + ); + } + #clearRuntimeAvailability(): void { if (this.#runtimeAvailability.clear()) this.#emitAvailabilityChanged(); } @@ -1740,22 +1754,16 @@ export class BinarySession this.#transitionPending += 1; // Later calls wait for this transition and must not be included in its drain. const admittedThrough = this.#transitionGeneration++; - const result = this.#transition.then( - () => operation(admittedThrough), - () => operation(admittedThrough), - ); + const perform = (): Promise => operation(admittedThrough); + const result = this.#transition.then(perform, perform); this.#transition = result.then( () => undefined, () => undefined, ); - void result.then( - () => { - this.#transitionPending -= 1; - }, - () => { - this.#transitionPending -= 1; - }, - ); + const finish = (): void => { + this.#transitionPending -= 1; + }; + void result.then(finish, finish); return result; } @@ -1850,32 +1858,19 @@ export class BinarySession ); }; signal.addEventListener("abort", onAbort, { once: true }); - transition.then( - () => { - signal.removeEventListener("abort", onAbort); - resolve( - signal.aborted - ? err( - new AnalysisCancelledError(operation, { - cause: signal.reason, - }), - ) - : ok(undefined), - ); - }, - () => { - signal.removeEventListener("abort", onAbort); - resolve( - signal.aborted - ? err( - new AnalysisCancelledError(operation, { - cause: signal.reason, - }), - ) - : ok(undefined), - ); - }, - ); + const finish = (): void => { + signal.removeEventListener("abort", onAbort); + resolve( + signal.aborted + ? err( + new AnalysisCancelledError(operation, { + cause: signal.reason, + }), + ) + : ok(undefined), + ); + }; + transition.then(finish, finish); }); } } diff --git a/src/application/binary/BinarySessionBundleResources.test.ts b/src/application/binary/BinarySessionBundleResources.test.ts index d1d89dd7a4..9e21bb8c80 100644 --- a/src/application/binary/BinarySessionBundleResources.test.ts +++ b/src/application/binary/BinarySessionBundleResources.test.ts @@ -145,9 +145,11 @@ it("opens a resource-only bundle without executable identity and keeps snapshot path: opened.value.canonicalPath, caller_path: opened.value.callerPath, }); - expect(session.activeBundleResourceRoot()?.canonicalPath).toBe( - opened.value.canonicalPath, - ); + expect(await session.status()).toMatchObject({ + open: true, + session_kind: "bundle_resources", + resource_root: { canonical_path: opened.value.canonicalPath }, + }); expect( bundleResourceRootSchema.parse({ caller_path: opened.value.callerPath, diff --git a/src/application/binary/BinarySessionExecution.ts b/src/application/binary/BinarySessionExecution.ts index b4d346d2fd..97fc8d83b2 100644 --- a/src/application/binary/BinarySessionExecution.ts +++ b/src/application/binary/BinarySessionExecution.ts @@ -17,6 +17,7 @@ import type { CapabilityDescriptor, ExecutionOptions, } from "../AnalysisProvider.js"; +import { offeredOperationAlternatives } from "../OperationAlternatives.js"; import { isSnapshotCacheable, requestsFreshIndependentDecompile, @@ -45,6 +46,11 @@ interface PrepareSessionExecutionInput { readonly parameters: Readonly>; readonly snapshotContext?: ExecutionOptions["snapshotContext"]; readonly nativeSourceAdmitted: boolean; + /** Current availability as binary_session reports it, not the declaration. */ + readonly isOperationAvailable: (operation: AnalysisOperation) => boolean; + readonly isOperationAdvertised: + | ((operation: AnalysisOperation) => boolean) + | undefined; readonly unboundOperationError: ( operation: AnalysisOperation, route: SessionProviderRoute, @@ -79,6 +85,8 @@ export const prepareSessionExecution = ( operation, parameters, nativeSourceAdmitted, + isOperationAvailable, + isOperationAdvertised, unboundOperationError, lookupSnapshot, } = input; @@ -97,6 +105,15 @@ export const prepareSessionExecution = ( capability === undefined ? undeclaredOperationGuidance(operation, entryCommentAvailable) : undefined; + const offered = + capability === undefined + ? offeredOperationAlternatives( + operation, + (alternative) => + isOperationAvailable(alternative) && + (isOperationAdvertised?.(alternative) ?? true), + ) + : undefined; return err( new AnalysisCapabilityUnavailableError( active.route.binding?.identity.id ?? active.route.identity.id, @@ -114,6 +131,12 @@ export const prepareSessionExecution = ( : { userMessage: `The selected provider does not declare ${operation}.`, fallbackRemediationAction: undeclaredGuidance, + ...(offered === undefined + ? {} + : { + alternatives: offered.operations, + remediationAction: offered.remediation, + }), }, ), ); diff --git a/src/application/binary/BinarySessionRefusalAlternatives.test.ts b/src/application/binary/BinarySessionRefusalAlternatives.test.ts new file mode 100644 index 0000000000..8f0ca826ac --- /dev/null +++ b/src/application/binary/BinarySessionRefusalAlternatives.test.ts @@ -0,0 +1,200 @@ +import { describe, expect, it, onTestFinished } from "vitest"; + +import { observed } from "../../../tests/fixtures/analysisExecution.js"; +import { + createBinarySessionTargets, + createTestBinarySession, +} from "../../../tests/fixtures/binarySession.js"; +import { createAnalysisProfile } from "../../domain/analysisProfile.js"; +import { projectAnalysisError } from "../../domain/analysisErrorProjection.js"; +import { ok } from "../../domain/result.js"; +import type { + AnalysisOperation, + AnalysisProvider, + CapabilityDescriptor, + ExecutionOptions, +} from "../AnalysisProvider.js"; +import { buildCapabilityInventory } from "../CapabilityInventory.js"; +import { offeredOperationAlternatives } from "../OperationAlternatives.js"; + +const IDENTITY = { + id: "fixture", + name: "Fixture analysis provider", + version: "1", +} as const; +const GENERIC = + "This analysis is unavailable for the current target. Choose another analysis or target."; + +/** The owning formatter's remediation for exactly these offered tools. */ +const remediationFor = (operation: string, offered: readonly string[]) => + offeredOperationAlternatives(operation, (alternative) => + offered.includes(alternative), + )?.remediation; + +const capability = ( + operation: Exclude, + available = true, +): CapabilityDescriptor => ({ + provider: IDENTITY, + operation, + ...(available + ? ({ available: true, reason: null } as const) + : ({ + available: false, + reason: `${operation} needs a loaded listing`, + } as const)), + cachePolicy: "live", + effects: { + mutatesArtifact: false, + launchesProcess: false, + mayShowUi: false, + mayAccessNetwork: false, + mayWriteFilesystem: false, + changesPermissions: false, + requiresRoot: false, + }, + limitations: [], +}); + +/** A deep provider whose declared operations alone decide the offers. */ +const openSession = async (capabilities: readonly CapabilityDescriptor[]) => { + const provider: AnalysisProvider = { + identity: () => IDENTITY, + resolveAnalysisProfile: () => + Promise.resolve( + ok({ profile: createAnalysisProfile(IDENTITY, { fixture: true }) }), + ), + capabilities: () => capabilities, + createClient: () => ({ + execute: (operation) => Promise.resolve(observed(operation)), + close: () => Promise.resolve(ok(null)), + }), + }; + const session = createTestBinarySession(provider); + onTestFinished(async () => { + expect(await session.close()).toEqual({ ok: true, value: null }); + }); + const [target] = await createBinarySessionTargets(); + expect((await session.open(target)).ok).toBe(true); + return session; +}; + +const refusal = async ( + session: Awaited>, + operation: "next_address" | "prev_address", + options?: ExecutionOptions, +) => { + const result = await session.execute(operation, {}, options); + if (result.ok) throw new Error(`${operation} unexpectedly succeeded`); + return projectAnalysisError(result.error); +}; + +const offeredInDiscovery = async ( + session: Awaited>, + isToolAdvertised?: (name: string) => boolean, +) => + buildCapabilityInventory( + await session.status(), + { processCaptureEnabled: false }, + isToolAdvertised, + ) + .filter(({ name }) => name === "next_address" || name === "prev_address") + .map((tool) => (tool.available ? undefined : tool.alternatives)); + +describe("undeclared navigation refusals", () => { + it("describe only offered tools, what none establishes and the provider choice", () => { + const offered = offeredOperationAlternatives( + "prev_address", + (alternative) => alternative === "procedure_assembly", + ); + expect(offered?.operations).toEqual(["procedure_assembly"]); + expect(offered?.remediation).toContain("procedure_assembly ("); + expect(offered?.remediation).not.toMatch( + /inspect_native_instruction|read_function_instructions/u, + ); + expect(offered?.remediation).toContain("executed control flow"); + expect(offered?.remediation).toContain("prev_address itself"); + expect( + offeredOperationAlternatives("list_procedures", () => true), + ).toBeUndefined(); + }); + + it("offer only alternatives the provider makes available", async () => { + const session = await openSession([ + capability("inspect_native_instruction", false), + capability("read_function_instructions"), + capability("procedure_assembly"), + ]); + for (const operation of ["next_address", "prev_address"] as const) + expect(await refusal(session, operation)).toMatchObject({ + code: "capability_unavailable", + remediation: { + action: remediationFor(operation, [ + "read_function_instructions", + "procedure_assembly", + ]), + }, + details: { + provider_id: "fixture", + operation, + alternatives: ["read_function_instructions", "procedure_assembly"], + }, + }); + expect(await offeredInDiscovery(session)).toEqual([ + ["read_function_instructions", "procedure_assembly"], + ["read_function_instructions", "procedure_assembly"], + ]); + }); + + it("offer only alternatives the caller advertises", async () => { + const session = await openSession([ + capability("read_function_instructions"), + capability("procedure_assembly"), + ]); + const withoutListing = (operation: string) => + operation !== "read_function_instructions"; + const narrowed = await refusal(session, "next_address", { + isOperationAdvertised: withoutListing, + }); + expect(narrowed.details?.alternatives).toEqual(["procedure_assembly"]); + expect(narrowed.remediation.action).toBe( + remediationFor("next_address", ["procedure_assembly"]), + ); + expect(await offeredInDiscovery(session, withoutListing)).toEqual([ + ["procedure_assembly"], + ["procedure_assembly"], + ]); + + const hidden = (operation: string) => + operation !== "read_function_instructions" && + operation !== "procedure_assembly"; + const generic = await refusal(session, "prev_address", { + isOperationAdvertised: hidden, + }); + expect(generic.remediation.action).toEqual( + expect.stringContaining("binary_session"), + ); + expect(generic.details).not.toHaveProperty("alternatives"); + expect(await offeredInDiscovery(session, hidden)).toEqual([ + undefined, + undefined, + ]); + }); + + it("keep a declared operation's own unavailability reason", async () => { + const session = await openSession([ + capability("next_address", false), + capability("read_function_instructions"), + ]); + const declared = await refusal(session, "next_address"); + expect(declared).toMatchObject({ + code: "capability_unavailable", + remediation: { action: GENERIC }, + details: { reason: "next_address needs a loaded listing" }, + }); + expect(declared.details).not.toHaveProperty("alternatives"); + const [next, previous] = await offeredInDiscovery(session); + expect(next).toBeUndefined(); + expect(previous).toEqual(["read_function_instructions"]); + }); +}); diff --git a/src/application/binary/BinarySessionStatus.ts b/src/application/binary/BinarySessionStatus.ts index afbb9b396f..6abb6561ba 100644 --- a/src/application/binary/BinarySessionStatus.ts +++ b/src/application/binary/BinarySessionStatus.ts @@ -51,6 +51,34 @@ interface BinarySessionStatusInput { readonly refreshCandidates?: boolean; } +/** + * One route capability as the session currently reports it: target + * applicability and observed runtime unavailability override the declaration. + */ +export const currentRouteCapability = ( + descriptor: CapabilityDescriptor, + target: BinaryTarget | undefined, + runtimeUnavailability: ReadonlyMap, +) => { + const targetRefusal = + target === undefined + ? undefined + : descriptor.targetApplicability === undefined + ? artifactTargetRefusal(descriptor.operation, target) + : targetApplicabilityRefusal(descriptor.targetApplicability, target); + if (targetRefusal !== undefined && descriptor.available) + return { + ...descriptor, + available: false as const, + availabilityCode: targetRefusal.code, + reason: targetRefusal.reason, + }; + const unavailable = runtimeUnavailability.get(descriptor.operation); + return unavailable === undefined + ? descriptor + : runtimeUnavailable(descriptor, unavailable.reason); +}; + /** Project internal provider routing state into the caller-visible session status. */ export const binarySessionStatus = async ({ investigation, @@ -79,28 +107,9 @@ export const binarySessionStatus = async ({ .sort((left, right) => compareUnicodeCodePoints(left.operation, right.operation), ) - .map((descriptor) => { - const targetRefusal = - target === undefined - ? undefined - : descriptor.targetApplicability === undefined - ? artifactTargetRefusal(descriptor.operation, target) - : targetApplicabilityRefusal( - descriptor.targetApplicability, - target, - ); - if (targetRefusal !== undefined && descriptor.available) - return { - ...descriptor, - available: false as const, - availabilityCode: targetRefusal.code, - reason: targetRefusal.reason, - }; - const unavailable = runtimeUnavailability.get(descriptor.operation); - return unavailable === undefined - ? descriptor - : runtimeUnavailable(descriptor, unavailable.reason); - }); + .map((descriptor) => + currentRouteCapability(descriptor, target, runtimeUnavailability), + ); const capabilities = providerCapabilitySets(routeCapabilities); const analysisProviderBinding = bundleResourceRoot !== undefined diff --git a/src/application/binaryDiagnostics/AppleIpsService.test.ts b/src/application/binaryDiagnostics/AppleIpsService.test.ts index 1784b7dc70..72829a6442 100644 --- a/src/application/binaryDiagnostics/AppleIpsService.test.ts +++ b/src/application/binaryDiagnostics/AppleIpsService.test.ts @@ -1,3 +1,6 @@ +import { createHash } from "node:crypto"; +import { parseAppleIps } from "../../native/apple/AppleIpsParser.js"; +import { inspectAppleIpsInputSchema } from "../../domain/native/appleIps.js"; import { parseEvidence } from "../../domain/evidence.js"; import { expect, it } from "vitest"; import { AppleIpsService } from "./AppleIpsService.js"; @@ -10,52 +13,19 @@ import { } from "../../domain/analysisErrorCore.js"; const identity = APPLE_IPS_PROVIDER; -const report: AppleIps = { - artifact: { - path: "/artifacts/report.ips", - sha256: "a".repeat(64), - bytes: 128, +const bytes = Buffer.from( + '{"bug_type":"309"}\n{"procName":"rea-crash-fixture","threads":[],"usedImages":[]}', +); +const path = "/artifacts/report.ips"; +const report = parseAppleIps( + bytes, + { + path, + sha256: createHash("sha256").update(bytes).digest("hex"), + bytes: bytes.length, }, - metadata: { - bug_type: "309", - timestamp: null, - os_version: null, - bundle_id: null, - incident_id: null, - name: null, - build_version: null, - platform: null, - unlisted_keys: [], - }, - process: { - name: "rea-crash-fixture", - path: null, - pid: null, - parent: null, - parent_pid: null, - cpu_type: null, - translated: null, - os_version: null, - }, - exception: null, - termination: null, - faulting_thread: null, - threads: [], - images: [], - classification: { status: "unclassified", matches: [] }, - coverage: { - status: "complete", - reason: null, - threads: 0, - frames: 0, - images: 0, - registers: 0, - type_mismatches: [], - }, - unlisted_body_keys: [], - join: null, - limitations: ["Opened Mach-O and dSYM UUID joins remain unobserved."], -}; + inspectAppleIpsInputSchema.parse({ path }), +); it.each([0, 5])( "rejects an unresolved faulting thread %s without its mismatch", diff --git a/src/application/binaryDiagnostics/BinaryLayoutPort.ts b/src/application/binaryDiagnostics/BinaryLayoutPort.ts index 55fe16c8ed..54c9c3066e 100644 --- a/src/application/binaryDiagnostics/BinaryLayoutPort.ts +++ b/src/application/binaryDiagnostics/BinaryLayoutPort.ts @@ -1,11 +1,13 @@ -import type { ExecutionOptions } from "../AnalysisProvider.js"; +import type { + ExecutionOptions, + ProviderIdentity, +} from "../AnalysisProvider.js"; import type { AnalysisError } from "../../domain/analysisErrorBase.js"; import type { BinaryLayout, InspectBinaryLayoutInput, } from "../../domain/native/binaryLayout.js"; import type { Result } from "../../domain/result.js"; -import type { ProviderIdentity } from "../AnalysisProvider.js"; /** Replaceable offline binary-layout provider; owns its parser and process protocol. */ export interface BinaryLayoutPort { diff --git a/src/application/binaryDiagnostics/ByteEntropyService.test.ts b/src/application/binaryDiagnostics/ByteEntropyService.test.ts index 0fa02b0660..0d0ed6d371 100644 --- a/src/application/binaryDiagnostics/ByteEntropyService.test.ts +++ b/src/application/binaryDiagnostics/ByteEntropyService.test.ts @@ -5,10 +5,7 @@ import { expect, it, vi } from "vitest"; import { createTestTempDirectory } from "../../../tests/fixtures/temporaryDirectory.js"; import { byteEntropySchema } from "../../domain/native/byteEntropy.js"; import { projectAnalysisError } from "../../domain/analysisErrorProjection.js"; -import { - ByteEntropyService, - MAX_BYTE_ENTROPY_BLOCKS, -} from "./ByteEntropyService.js"; +import { ByteEntropyService } from "./ByteEntropyService.js"; it("keeps whole-file identity and entropy while selecting unaligned blocks and a partial tail", async () => { const root = await createTestTempDirectory("rea-entropy-"); @@ -421,7 +418,7 @@ it("reports a changed artifact when its parent directory moves during streaming" it("refuses a block series above the output-row limit before scanning", async () => { const root = await createTestTempDirectory("rea-entropy-block-limit-"); const path = join(root, "bytes"); - await writeFile(path, Buffer.alloc(MAX_BYTE_ENTROPY_BLOCKS * 256 + 1)); + await writeFile(path, Buffer.alloc(32_768 * 256 + 1)); let reads = 0; const service = new ByteEntropyService({ stat, @@ -443,8 +440,8 @@ it("refuses a block series above the output-row limit before scanning", async () _tag: "AnalysisResourceConstraintError", resource: "capacity", reportedLimits: { - max_blocks: MAX_BYTE_ENTROPY_BLOCKS, - requested_blocks: MAX_BYTE_ENTROPY_BLOCKS + 1, + max_blocks: 32_768, + requested_blocks: 32_769, }, }, }); diff --git a/src/application/binaryDiagnostics/ByteEntropyService.ts b/src/application/binaryDiagnostics/ByteEntropyService.ts index ae5cc1f564..55e0fe1c99 100644 --- a/src/application/binaryDiagnostics/ByteEntropyService.ts +++ b/src/application/binaryDiagnostics/ByteEntropyService.ts @@ -44,7 +44,7 @@ import { completeProviderEvidence } from "../ProviderOperationExecution.js"; import { parseAnalysisInput } from "../AnalysisInput.js"; /** Bound retained result rows before constructing the Evidence representation. */ -export const MAX_BYTE_ENTROPY_BLOCKS = 32_768; +const MAX_BYTE_ENTROPY_BLOCKS = 32_768; /** Generated files such as Linux /proc entries report size 0 but still hold bytes. */ const ZERO_SIZE_LIMITATION = diff --git a/src/application/binaryDiagnostics/ElfNotesService.test.ts b/src/application/binaryDiagnostics/ElfNotesService.test.ts index f71883719e..a682610ab6 100644 --- a/src/application/binaryDiagnostics/ElfNotesService.test.ts +++ b/src/application/binaryDiagnostics/ElfNotesService.test.ts @@ -2,7 +2,10 @@ import { parseEvidence } from "../../domain/evidence.js"; import { expect, it } from "vitest"; import { ElfNotesService } from "./ElfNotesService.js"; import { ELF_NOTES_PROVIDER } from "../../domain/investigationProviders.js"; -import type { ElfNotes } from "../../domain/native/elfNotes.js"; +import { createHash } from "node:crypto"; +import { inspectElfNotesInputSchema } from "../../domain/native/elfNotes.js"; +import { elfNoteFixture } from "../../native/elf/ElfNotes.fixture.js"; +import { parseElfNotes } from "../../native/elf/ElfNoteParser.js"; import { ok } from "../../domain/result.js"; import { AnalysisCancelledError, @@ -11,22 +14,17 @@ import { } from "../../domain/analysisErrorCore.js"; const identity = ELF_NOTES_PROVIDER; -const report: ElfNotes = { - artifact: { - path: "/artifacts/app", - sha256: "a".repeat(64), - bytes: 512, +const path = "/artifacts/app"; +const bytes = elfNoteFixture({ ownership: "none" }); +const report = parseElfNotes( + bytes, + { + path, + sha256: createHash("sha256").update(bytes).digest("hex"), + bytes: bytes.length, }, - format: "elf64", - byte_order: "little", - machine: 62, - type: 3, - type_name: "shared", - tables: [], - entries: [], - coverage: { status: "complete", examined_tables: 0, examined_entries: 0 }, - limitations: ["No PT_NOTE program header or SHT_NOTE section was declared."], -}; + inspectElfNotesInputSchema.parse({ path }), +); it("rejects a relative path and extra arguments before reading", async () => { let calls = 0; diff --git a/src/application/binaryDiagnostics/ElfNotesService.ts b/src/application/binaryDiagnostics/ElfNotesService.ts index fae180099f..6ba2fe0705 100644 --- a/src/application/binaryDiagnostics/ElfNotesService.ts +++ b/src/application/binaryDiagnostics/ElfNotesService.ts @@ -1,3 +1,4 @@ +import { insideArtifact } from "./artifactRange.js"; import type { ExecutionOptions } from "../AnalysisProvider.js"; import type { ElfNotesPort } from "./ElfNotesPort.js"; import type { AnalysisError } from "../../domain/analysisErrorBase.js"; @@ -8,7 +9,6 @@ import { elfNotesSchema, type ElfNotes, } from "../../domain/native/elfNotes.js"; -import { BINARY_ARCHITECTURES } from "../../domain/binaryTargetTypes.js"; import { elfArchitecture } from "../../domain/elfFixedHeader.js"; import { err, type Result } from "../../domain/result.js"; import { completeProviderEvidence } from "../ProviderOperationExecution.js"; @@ -54,9 +54,7 @@ export class ElfNotesService { ), ); const value = report.data; - const architecture = BINARY_ARCHITECTURES.find( - (supported) => supported === elfArchitecture(value.machine), - ); + const architecture = elfArchitecture(value.machine); const locations = [ { kind: "artifact-path" as const, path: value.artifact.path }, ...value.tables.map((table) => ({ @@ -93,8 +91,7 @@ const validReport = ( maxEntries: number, maxBytes: number, ): boolean => { - const inside = ({ offset, bytes }: { offset: number; bytes: number }) => - offset <= report.artifact.bytes - bytes; + const inside = insideArtifact(report.artifact.bytes); if ( report.artifact.bytes > maxBytes || report.coverage.examined_entries > maxEntries || diff --git a/src/application/binaryDiagnostics/ElfUnwindRangesService.test.ts b/src/application/binaryDiagnostics/ElfUnwindRangesService.test.ts index 422233a1a4..b3b0cacb45 100644 --- a/src/application/binaryDiagnostics/ElfUnwindRangesService.test.ts +++ b/src/application/binaryDiagnostics/ElfUnwindRangesService.test.ts @@ -27,57 +27,6 @@ const undecodableHeaderPointer = (): Buffer => { bytes[Number(bytes.readBigUInt64LE(64 + 56 + 8)) + 1] = 0x0f; return bytes; }; -const report: ElfUnwindRanges = { - artifact: { - path: "/artifacts/app", - sha256: "a".repeat(64), - bytes: 512, - }, - format: "elf64", - byte_order: "little", - machine: 62, - type: 3, - type_name: "shared", - eh_frame: { status: "absent", source: null, location: null }, - eh_frame_hdr: { - status: "absent", - location: null, - version: null, - eh_frame_pointer_encoding: null, - fde_count_encoding: null, - table_encoding: null, - eh_frame_pointer: null, - fde_count: null, - table_entries: 0, - }, - debug_frame: { status: "absent", location: null }, - cies: [], - ranges: [], - page: { - offset: 0, - limit: 256, - returned: 0, - total: 0, - next_offset: null, - }, - coverage: { - status: "complete", - total_ranges: 0, - total_cies: 0, - ranges_with_symbols: 0, - ranges_without_symbols: 0, - eh_frame_hdr_agreement: "unavailable", - examined_records: 0, - }, - limitations: [ - "No .eh_frame section or PT_GNU_EH_FRAME segment was declared.", - "No .eh_frame_hdr section or PT_GNU_EH_FRAME segment was declared.", - "CFA instructions are counted but not evaluated.", - ".debug_frame is absent; only .eh_frame was examined.", - "Symbol names, section names, and augmentation strings are target-derived and untrusted.", - ], -}; - const parsed = ( bytes: Buffer, raw: Readonly> = {}, @@ -97,6 +46,8 @@ const parsed = ( ); }; +const report = parsed(elfUnwindAbsentFixture()); + const accept = async ( value: ElfUnwindRanges, raw: Readonly> = {}, diff --git a/src/application/binaryDiagnostics/ElfUnwindRangesService.ts b/src/application/binaryDiagnostics/ElfUnwindRangesService.ts index 21d6eccbd6..588c27975e 100644 --- a/src/application/binaryDiagnostics/ElfUnwindRangesService.ts +++ b/src/application/binaryDiagnostics/ElfUnwindRangesService.ts @@ -1,3 +1,4 @@ +import { insideArtifact } from "./artifactRange.js"; import type { ExecutionOptions } from "../AnalysisProvider.js"; import type { ElfUnwindRangesPort } from "./ElfUnwindRangesPort.js"; import type { AnalysisError } from "../../domain/analysisErrorBase.js"; @@ -12,7 +13,6 @@ import { MAX_INSPECT_ELF_UNWIND_RANGES, type ElfUnwindRanges, } from "../../domain/native/elfUnwindRanges.js"; -import { BINARY_ARCHITECTURES } from "../../domain/binaryTargetTypes.js"; import { elfArchitecture } from "../../domain/elfFixedHeader.js"; import { err, type Result } from "../../domain/result.js"; import { completeProviderEvidence } from "../ProviderOperationExecution.js"; @@ -75,9 +75,7 @@ export class ElfUnwindRangesService { ), ); const value = report.data; - const architecture = BINARY_ARCHITECTURES.find( - (supported) => supported === elfArchitecture(value.machine), - ); + const architecture = elfArchitecture(value.machine); const locations = [ { kind: "artifact-path" as const, path: value.artifact.path }, ...rangesOf(value).map((range) => ({ @@ -109,11 +107,6 @@ export class ElfUnwindRangesService { } } -const inside = - (fileBytes: number) => - (range: Range): boolean => - range.bytes <= fileBytes && range.offset <= fileBytes - range.bytes; - const rangesOf = (report: ElfUnwindRanges): Range[] => { const found: Range[] = []; const add = (range: Range | null): void => { @@ -381,7 +374,7 @@ const validReport = ( report.type_name !== elfObjectTypeName(report.type) || report.page.offset !== offset || report.page.limit !== limit || - !rangesOf(report).every(inside(report.artifact.bytes)) || + !rangesOf(report).every(insideArtifact(report.artifact.bytes)) || !has(report, CFA_LIMITATION) || !has(report, UNTRUSTED_LIMITATION) || (indirectVisible(report) && !has(report, INDIRECT_LIMITATION)) diff --git a/src/application/binaryDiagnostics/KernelModuleService.ts b/src/application/binaryDiagnostics/KernelModuleService.ts index 6c80f7d801..0c822628a3 100644 --- a/src/application/binaryDiagnostics/KernelModuleService.ts +++ b/src/application/binaryDiagnostics/KernelModuleService.ts @@ -1,3 +1,4 @@ +import { insideArtifact } from "./artifactRange.js"; import type { ExecutionOptions } from "../AnalysisProvider.js"; import type { KernelModulePort } from "./KernelModulePort.js"; import type { AnalysisError } from "../../domain/analysisErrorBase.js"; @@ -15,7 +16,6 @@ import { type KernelModule, type InspectKernelModuleInput, } from "../../domain/native/kernelModule.js"; -import { BINARY_ARCHITECTURES } from "../../domain/binaryTargetTypes.js"; import { elfArchitecture } from "../../domain/elfFixedHeader.js"; import { err, type Result } from "../../domain/result.js"; import { completeProviderEvidence } from "../ProviderOperationExecution.js"; @@ -59,11 +59,8 @@ export class KernelModuleService { ), ); const value = report.data; - const machine = value.machine; - const mapped = machine === null ? undefined : elfArchitecture(machine); - const architecture = BINARY_ARCHITECTURES.find( - (supported) => supported === mapped, - ); + const architecture = + value.machine === null ? undefined : elfArchitecture(value.machine); const locations = [ { kind: "artifact-path" as const, path: value.artifact.path }, ...rangesOf(value).map((range) => ({ @@ -95,11 +92,6 @@ export class KernelModuleService { } } -const inside = - (fileBytes: number) => - (range: Range): boolean => - range.bytes <= fileBytes && range.offset <= fileBytes - range.bytes; - const rangesOf = (report: KernelModule): Range[] => { const found: Range[] = []; const add = (range: Range | null | undefined): void => { @@ -290,7 +282,7 @@ const validReport = ( if ( report.artifact.bytes > input.max_file_bytes || report.coverage.examined_entries > input.max_entries || - !rangesOf(report).every(inside(report.artifact.bytes)) + !rangesOf(report).every(insideArtifact(report.artifact.bytes)) ) return false; if (report.applicability === "unsupported") diff --git a/src/application/binaryDiagnostics/NativeInitializersService.ts b/src/application/binaryDiagnostics/NativeInitializersService.ts index e61db3ac0d..af234d3332 100644 --- a/src/application/binaryDiagnostics/NativeInitializersService.ts +++ b/src/application/binaryDiagnostics/NativeInitializersService.ts @@ -1,3 +1,4 @@ +import { insideArtifact } from "./artifactRange.js"; import type { ExecutionOptions } from "../AnalysisProvider.js"; import type { NativeInitializersPort } from "./NativeInitializersPort.js"; import type { AnalysisError } from "../../domain/analysisErrorBase.js"; @@ -106,8 +107,7 @@ const validReport = ( report.architecture !== input.architecture ) return false; - const insideArtifact = (offset: number, bytes: number): boolean => - offset <= report.artifact.bytes - bytes; + const inside = insideArtifact(report.artifact.bytes); // Entry file offsets name the selected image, not another slice in the container. const imageStart = slice?.file_offset ?? 0; const imageEnd = @@ -115,7 +115,8 @@ const validReport = ( const insideImage = (offset: number | null, bytes: number): boolean => offset === null || (offset >= imageStart && bytes <= imageEnd - offset); if ( - (slice !== null && !insideArtifact(slice.file_offset, slice.size)) || + (slice !== null && + !inside({ offset: slice.file_offset, bytes: slice.size })) || report.artifact.bytes > input.max_file_bytes || coverage.offset !== input.offset || coverage.limit !== input.limit || diff --git a/src/application/binaryDiagnostics/PeCertificatesService.test.ts b/src/application/binaryDiagnostics/PeCertificatesService.test.ts index fa73b22f77..e1264077a1 100644 --- a/src/application/binaryDiagnostics/PeCertificatesService.test.ts +++ b/src/application/binaryDiagnostics/PeCertificatesService.test.ts @@ -1,7 +1,10 @@ import { parseEvidence } from "../../domain/evidence.js"; import { createHash } from "node:crypto"; import { inspectPeCertificatesInputSchema } from "../../domain/native/peCertificates.js"; -import { peCertificateFixture } from "../../native/pe/PeCertificates.fixture.js"; +import { + peCertificateFixture, + peWithoutCertificateTableFixture, +} from "../../native/pe/PeCertificates.fixture.js"; import { parsePeCertificates } from "../../native/pe/PeCertificateParser.js"; import { expect, it } from "vitest"; import { PeCertificatesService } from "./PeCertificatesService.js"; @@ -15,19 +18,17 @@ import { } from "../../domain/analysisErrorCore.js"; const identity = PE_CERTIFICATES_PROVIDER; -const report: PeCertificates = { - artifact: { - path: "/artifacts/app.exe", - sha256: "a".repeat(64), - bytes: 512, +const path = "/artifacts/app.exe"; +const bytes = peWithoutCertificateTableFixture(); +const report = parsePeCertificates( + bytes, + { + path, + sha256: createHash("sha256").update(bytes).digest("hex"), + bytes: bytes.length, }, - format: "pe32", - machine: 0x14c, - table: null, - entries: [], - coverage: { status: "complete", examined_entries: 0, certificates: 0 }, - limitations: ["IMAGE_DIRECTORY_ENTRY_SECURITY is absent."], -}; + inspectPeCertificatesInputSchema.parse({ path }), +); it("rejects a relative path and extra arguments before reading", async () => { let calls = 0; diff --git a/src/application/binaryDiagnostics/PeCertificatesService.ts b/src/application/binaryDiagnostics/PeCertificatesService.ts index 8c0351c3b6..2f3752a559 100644 --- a/src/application/binaryDiagnostics/PeCertificatesService.ts +++ b/src/application/binaryDiagnostics/PeCertificatesService.ts @@ -1,3 +1,4 @@ +import { insideArtifact } from "./artifactRange.js"; import type { ExecutionOptions } from "../AnalysisProvider.js"; import type { PeCertificatesPort } from "./PeCertificatesPort.js"; import type { AnalysisError } from "../../domain/analysisErrorBase.js"; @@ -94,8 +95,7 @@ const validReport = ( maxEntries: number, maxBytes: number, ): boolean => { - const inside = ({ offset, bytes }: { offset: number; bytes: number }) => - offset <= report.artifact.bytes - bytes; + const inside = insideArtifact(report.artifact.bytes); if ( report.artifact.bytes > maxBytes || report.coverage.examined_entries > maxEntries || diff --git a/src/application/binaryDiagnostics/PeImportsService.test.ts b/src/application/binaryDiagnostics/PeImportsService.test.ts index c641bf5385..9810201edf 100644 --- a/src/application/binaryDiagnostics/PeImportsService.test.ts +++ b/src/application/binaryDiagnostics/PeImportsService.test.ts @@ -1,9 +1,15 @@ +import { createHash } from "node:crypto"; +import { peWithoutImportTableFixture } from "../../../tests/fixtures/binaryDiagnostics/peImports.js"; +import { parsePeImports } from "../../native/pe/PeImportParser.js"; +import { + inspectPeImportsInputSchema, + peImportsSchema, + type PeImports, +} from "../../domain/native/peImports.js"; import { parseEvidence } from "../../domain/evidence.js"; import { expect, it } from "vitest"; import { PeImportsService } from "./PeImportsService.js"; import { PE_IMPORTS_PROVIDER } from "../../domain/investigationProviders.js"; -import type { PeImports } from "../../domain/native/peImports.js"; -import { peImportsSchema } from "../../domain/native/peImports.js"; import { ok } from "../../domain/result.js"; import { AnalysisCancelledError, @@ -12,35 +18,17 @@ import { } from "../../domain/analysisErrorCore.js"; const identity = PE_IMPORTS_PROVIDER; -const report: PeImports = { - artifact: { - path: "/artifacts/app.exe", - sha256: "a".repeat(64), - bytes: 512, +const path = "/artifacts/app.exe"; +const bytes = peWithoutImportTableFixture(); +const report = await parsePeImports( + bytes, + { + path, + sha256: createHash("sha256").update(bytes).digest("hex"), + bytes: bytes.length, }, - format: "pe32-plus", - machine: 0x8664, - bits: 64, - architecture: "amd64", - import_directory: null, - delay_import_directory: null, - imports: [], - delay_imports: [], - imphash: { - algorithm: "pefile", - md5: null, - reason: "IMAGE_DIRECTORY_ENTRY_IMPORT is absent.", - }, - coverage: { - status: "complete", - reason: null, - examined_dlls: 0, - imports: 0, - delay_imports: 0, - }, - issues: [], - limitations: ["IMAGE_DIRECTORY_ENTRY_IMPORT is absent."], -}; + inspectPeImportsInputSchema.parse({ path }), +); it("rejects a relative path and extra arguments before reading", async () => { let calls = 0; @@ -189,7 +177,7 @@ it("publishes every consumed directory span as its own Evidence location", async size: 40, locations: [ { offset: 0x100, bytes: 20 }, - { offset: 0x1f8, bytes: 20 }, + { offset: report.artifact.bytes - 8, bytes: 20 }, ], }, }), diff --git a/src/application/binaryDiagnostics/PeImportsService.ts b/src/application/binaryDiagnostics/PeImportsService.ts index 60bf227c6c..c84e188b5d 100644 --- a/src/application/binaryDiagnostics/PeImportsService.ts +++ b/src/application/binaryDiagnostics/PeImportsService.ts @@ -1,3 +1,4 @@ +import { insideArtifact } from "./artifactRange.js"; import type { ExecutionOptions } from "../AnalysisProvider.js"; import type { PeImportsPort } from "./PeImportsPort.js"; import type { AnalysisError } from "../../domain/analysisErrorBase.js"; @@ -101,8 +102,7 @@ const validReport = ( readonly max_file_bytes: number; }, ): boolean => { - const inside = ({ offset, bytes }: { offset: number; bytes: number }) => - offset <= report.artifact.bytes - bytes; + const inside = insideArtifact(report.artifact.bytes); const dllCount = report.imports.length + report.delay_imports.length; if ( report.artifact.bytes > input.max_file_bytes || diff --git a/src/application/binaryDiagnostics/PeLoadConfigService.ts b/src/application/binaryDiagnostics/PeLoadConfigService.ts index 0e18362d5d..3f56621fef 100644 --- a/src/application/binaryDiagnostics/PeLoadConfigService.ts +++ b/src/application/binaryDiagnostics/PeLoadConfigService.ts @@ -1,3 +1,4 @@ +import { insideArtifact } from "./artifactRange.js"; import type { ExecutionOptions } from "../AnalysisProvider.js"; import type { PeLoadConfigPort } from "./PeLoadConfigPort.js"; import type { AnalysisError } from "../../domain/analysisErrorBase.js"; @@ -132,17 +133,16 @@ const validReport = ( ...fileRange(report.load_config?.directory ?? null), ]; const structure = report.load_config?.structure_location; + const inside = insideArtifact(report.artifact.bytes); return ( (structure === undefined || (structure.offset === report.load_config?.directory.file_offset && structure.bytes === report.load_config.size && - structure.offset <= report.artifact.bytes - structure.bytes)) && + inside(structure))) && ranges.every( ({ start, end }) => start <= end && end <= report.artifact.bytes, ) && - (report.tls?.callback_array_locations ?? []).every( - ({ offset, bytes }) => offset <= report.artifact.bytes - bytes, - ) && + (report.tls?.callback_array_locations ?? []).every(inside) && (report.tls?.callbacks ?? []).every( ({ file_offset }) => file_offset === null || file_offset < report.artifact.bytes, diff --git a/src/application/binaryDiagnostics/PeResourcesService.ts b/src/application/binaryDiagnostics/PeResourcesService.ts index 78b58216ca..e559ee9a50 100644 --- a/src/application/binaryDiagnostics/PeResourcesService.ts +++ b/src/application/binaryDiagnostics/PeResourcesService.ts @@ -1,3 +1,4 @@ +import { insideArtifact } from "./artifactRange.js"; import type { ExecutionOptions } from "../AnalysisProvider.js"; import type { PeResourcesPort } from "./PeResourcesPort.js"; import type { AnalysisError } from "../../domain/analysisErrorBase.js"; @@ -10,7 +11,7 @@ import { type PeResourceIdentity, } from "../../domain/native/peResources.js"; import { peSubjectArchitecture } from "../../domain/peMachine.js"; -import { err, ok, type Result } from "../../domain/result.js"; +import { err, type Result } from "../../domain/result.js"; import { completeProviderEvidence } from "../ProviderOperationExecution.js"; import { parseAnalysisInput } from "../AnalysisInput.js"; @@ -83,8 +84,7 @@ const validReport = ( maxEntries: number, maxBytes: number, ): boolean => { - const inside = ({ offset, bytes }: { offset: number; bytes: number }) => - offset <= report.artifact.bytes - bytes; + const inside = insideArtifact(report.artifact.bytes); const identityInside = (identity: PeResourceIdentity): boolean => identity.kind === "id" || inside(identity.location); if ( diff --git a/src/application/binaryDiagnostics/WindowsShortcutService.test.ts b/src/application/binaryDiagnostics/WindowsShortcutService.test.ts index 4aa8be43bd..ba9b98b000 100644 --- a/src/application/binaryDiagnostics/WindowsShortcutService.test.ts +++ b/src/application/binaryDiagnostics/WindowsShortcutService.test.ts @@ -2,7 +2,9 @@ import { parseEvidence } from "../../domain/evidence.js"; import { expect, it } from "vitest"; import { WindowsShortcutService } from "./WindowsShortcutService.js"; import { WINDOWS_SHORTCUT_PROVIDER } from "../../domain/investigationProviders.js"; -import type { WindowsShortcut } from "../../domain/native/windowsShortcut.js"; +import { createHash } from "node:crypto"; +import { inspectWindowsShortcutInputSchema } from "../../domain/native/windowsShortcut.js"; +import { parseWindowsShortcut } from "../../native/windows/WindowsShortcutParser.js"; import { ok } from "../../domain/result.js"; import { AnalysisCancelledError, @@ -12,64 +14,20 @@ import { const identity = WINDOWS_SHORTCUT_PROVIDER; -const report: WindowsShortcut = { - artifact: { - path: "/artifacts/shortcut.lnk", - sha256: "a".repeat(64), - bytes: 128, +const path = "/artifacts/shortcut.lnk"; +const bytes = Buffer.alloc(80); +bytes.writeUInt32LE(76, 0); +Buffer.from("0114020000000000c000000000000046", "hex").copy(bytes, 4); +bytes.writeUInt32LE(1, 60); +const report = await parseWindowsShortcut( + bytes, + { + path, + sha256: createHash("sha256").update(bytes).digest("hex"), + bytes: bytes.length, }, - header: { - location: { offset: 0, bytes: 76 }, - header_size: 76, - clsid: "00021401-0000-0000-C000-000000000046", - link_flags: { raw: 0, decoded: [] }, - file_attributes: { raw: 0, decoded: [] }, - creation_time: { raw: "0", iso8601: null }, - access_time: { raw: "0", iso8601: null }, - write_time: { raw: "0", iso8601: null }, - file_size: 0, - icon_index: 0, - show_command: { raw: 1, decoded: "normal" }, - hotkey: { - raw: 0, - modifiers: [], - virtual_key: null, - virtual_key_name: null, - }, - }, - id_list: null, - link_info: null, - string_data: { - name: null, - relative_path: null, - working_dir: null, - arguments: null, - icon_location: null, - }, - extra_data: { blocks: [], terminal_offset: null }, - observations: [ - { kind: "arguments_present", evidence_kind: "derived", value: false }, - { kind: "argument_length", evidence_kind: "derived", characters: 0 }, - { kind: "target_extension", evidence_kind: "derived", extension: null }, - { kind: "show_command_minimized", evidence_kind: "derived", value: false }, - { - kind: "icon_location_differs_from_target", - evidence_kind: "derived", - value: null, - }, - ], - coverage: { - status: "complete", - reason: null, - examined_id_list_items: 0, - examined_extra_data_blocks: 0, - failing_offset: null, - }, - issues: [], - limitations: [ - "Rows reproduce the selected Shell Link bytes; they do not resolve, open, or execute the link target.", - ], -}; + inspectWindowsShortcutInputSchema.parse({ path }), +); it("rejects a relative path and extra arguments before reading", async () => { let calls = 0; diff --git a/src/application/binaryDiagnostics/WindowsShortcutService.ts b/src/application/binaryDiagnostics/WindowsShortcutService.ts index 44dcd2f620..49a4c31ee2 100644 --- a/src/application/binaryDiagnostics/WindowsShortcutService.ts +++ b/src/application/binaryDiagnostics/WindowsShortcutService.ts @@ -1,3 +1,4 @@ +import { insideArtifact } from "./artifactRange.js"; import type { ExecutionOptions } from "../AnalysisProvider.js"; import type { WindowsShortcutPort } from "./WindowsShortcutPort.js"; import type { AnalysisError } from "../../domain/analysisErrorBase.js"; @@ -121,8 +122,7 @@ const validReport = ( if (complete !== (report.issues.length === 0)) return false; if (complete && report.coverage.failing_offset !== null) return false; if (!complete && report.coverage.reason === null) return false; - const inside = ({ offset, bytes }: { offset: number; bytes: number }) => - offset <= report.artifact.bytes - bytes; + const inside = insideArtifact(report.artifact.bytes); if (!inside(report.header.location)) return false; if (report.id_list !== null && !inside(report.id_list.location)) return false; if (report.link_info !== null && !inside(report.link_info.location)) diff --git a/src/application/binaryDiagnostics/artifactRange.ts b/src/application/binaryDiagnostics/artifactRange.ts new file mode 100644 index 0000000000..1349934b60 --- /dev/null +++ b/src/application/binaryDiagnostics/artifactRange.ts @@ -0,0 +1,11 @@ +/** Check a schema-validated nonnegative byte range against its carrier size. */ +export const insideArtifact = + (fileBytes: number) => + ({ + offset, + bytes, + }: { + readonly offset: number; + readonly bytes: number; + }): boolean => + offset <= fileBytes - bytes; diff --git a/src/application/decompileDirectory/decompileAll.test.ts b/src/application/decompileDirectory/decompileAll.test.ts index 6ef999f519..39fc809e88 100644 --- a/src/application/decompileDirectory/decompileAll.test.ts +++ b/src/application/decompileDirectory/decompileAll.test.ts @@ -735,6 +735,97 @@ describe("decompile_all resume", () => { }); }); +describe("decompile_all retained body coverage", () => { + const directory = useOutputDirectory(); + const limitationsOf = (value: JsonValue): readonly string[] => + (value as { limitations: string[] }).limitations; + const unavailableReasons = (index: { + readonly functions: readonly { + readonly body: { readonly available: boolean; readonly reason?: string }; + }[]; + }): string[] => + index.functions.flatMap(({ body }) => + !body.available && body.reason !== undefined ? [body.reason] : [], + ); + const indexOf = async () => + decompileDirectoryIndexSchema.parse( + JSON.parse( + await readFile( + join(directory(), DECOMPILE_DIRECTORY_INDEX_NAME), + "utf8", + ), + ), + ); + + it("keeps the caveat of retained records through no-op and empty resumes", async () => { + const created = await run( + harness({ info: "unavailable" }).tools, + directory(), + ); + expect(created.ok).toBe(true); + if (!created.ok) return; + const createdLimitations = limitationsOf(created.value); + // One caveat counts the retained records and repeats their shared reason. + expect(createdLimitations).toHaveLength(1); + const createdReason = unavailableReasons(await indexOf())[0]; + expect(createdReason).toBeDefined(); + expect(createdLimitations[0]).toMatch(/^3 exported function record/u); + expect(createdLimitations[0]).toContain(createdReason); + const file = join(directory(), "functions/0x1000.c"); + const before = await readFile(file, "utf8"); + + for (const filter of [{}, { name: "NoSuchFunction" }]) { + const resumed = harness(); + const result = await run(resumed.tools, directory(), { + resume: true, + ...filter, + }); + expect(result.ok).toBe(true); + if (!result.ok) return; + expect(result.value).toMatchObject({ counts: { requested: 0 } }); + expect(limitationsOf(result.value)).toEqual(createdLimitations); + expect((await indexOf()).limitations).toEqual(createdLimitations); + expect(of(resumed.calls, "procedure_info")).toHaveLength(0); + } + expect(await readFile(file, "utf8")).toBe(before); + expect( + (await indexOf()).functions.map((record) => record.body.available), + ).toEqual([false, false, false]); + }); + + it("counts retained and new unknown bodies and drops the caveat once every body is known", async () => { + expect( + (await run(harness({ info: "unavailable" }).tools, directory())).ok, + ).toBe(true); + await rm(join(directory(), "functions/0x1000.c")); + const mixed = await run(harness({ info: "error" }).tools, directory(), { + resume: true, + }); + expect(mixed.ok).toBe(true); + if (!mixed.ok) return; + expect(mixed.value).toMatchObject({ + counts: { written: 1, skipped: 2 }, + }); + // Differing reasons are counted together without naming either one. + const reasons = new Set(unavailableReasons(await indexOf())); + expect(reasons.size).toBe(2); + const mixedCaveats = limitationsOf(mixed.value).filter((limitation) => + limitation.startsWith("3 exported function record"), + ); + expect(mixedCaveats).toHaveLength(1); + for (const reason of reasons) expect(mixedCaveats[0]).not.toContain(reason); + + for (const address of ["0x1000", "0x2000", "0x3000"]) + await rm(join(directory(), `functions/${address}.c`)); + const known = await run(harness().tools, directory(), { resume: true }); + expect(known.ok).toBe(true); + if (!known.ok) return; + expect(known.value).toMatchObject({ counts: { written: 3 } }); + expect(limitationsOf(known.value).join("\n")).not.toContain("body"); + expect((await indexOf()).limitations).toEqual([]); + }); +}); + describe("decompile_all filters and layout", () => { const directory = useOutputDirectory(); diff --git a/src/application/decompileDirectory/decompileAll.ts b/src/application/decompileDirectory/decompileAll.ts index 37a070770e..78b97fffb6 100644 --- a/src/application/decompileDirectory/decompileAll.ts +++ b/src/application/decompileDirectory/decompileAll.ts @@ -38,6 +38,7 @@ import { DecompileExport, exportOutputFailure, type ExportIdentity, + type UnknownBodies, } from "./exportTree.js"; import { bodyRecord, @@ -64,6 +65,13 @@ const DUPLICATE_ADDRESSES = export const OUTPUT_IO_STREAK_LIMIT = 5; const unaddressedRows = (count: number): string => `${count.toString()} inventory row(s) had no address and were not exported.`; +/** Summarizes every retained record, including those earlier runs wrote. */ +const unknownBodyRecords = (unknown: UnknownBodies): string => + `${unknown.count.toString()} exported function record(s) report an unavailable body${ + unknown.sharedReason === undefined + ? ` for differing reasons; see each record's body.reason in ${DECOMPILE_DIRECTORY_INDEX_NAME}.` + : `: ${unknown.sharedReason}` + }`; interface FailureReason { readonly code: string; @@ -84,7 +92,6 @@ interface PublishState { readonly failures: InlineFailure[]; inlineFailureBytes: number; readonly recentFailures: PublishState["failures"]; - bodyUnavailable: boolean; /** Failure code of the function being attempted, if it failed. */ lastFailureCode: string | undefined; /** @@ -137,7 +144,6 @@ export const decompileAll = async ( failures: [], inlineFailureBytes: 2, recentFailures: [], - bodyUnavailable: false, lastFailureCode: undefined, outputIoStreak: 0, }; @@ -161,7 +167,7 @@ export const decompileAll = async ( input.layout, loaded.value.identity, selection.collapsedDuplicates, - state.bodyUnavailable, + session.unknownBodies(), loaded.value.unaddressed, ); indexed = await session.writeIndex({ @@ -520,7 +526,6 @@ const publishOne = async (input: { ); if (body.kind === "error") return { result: err(body.error), bodySupport: input.bodySupport }; - if (body.kind === "unavailable") input.state.bodyUnavailable = true; // Caveats of the observations this attempt used. The journal record keeps // the complete list, written or failed, so the bounded Evidence summary and // the length-capped file header never hold the only copy. @@ -1084,7 +1089,7 @@ const exportLimitations = ( layout: DecompileDirectoryInput["layout"], identity: ExportIdentity, collapsedDuplicates: boolean, - bodyUnavailable: boolean, + unknownBodies: UnknownBodies, unaddressed: number, ): readonly string[] => [ ...(layout === "dwarf" ? [DWARF_LAYOUT_UNAVAILABLE] : []), @@ -1092,7 +1097,7 @@ const exportLimitations = ( ...(identity.analysisProfileDigest === null ? [PROFILE_UNAVAILABLE] : []), ...(collapsedDuplicates ? [DUPLICATE_ADDRESSES] : []), ...(unaddressed > 0 ? [unaddressedRows(unaddressed)] : []), - ...(bodyUnavailable ? [BODY_UNAVAILABLE] : []), + ...(unknownBodies.count > 0 ? [unknownBodyRecords(unknownBodies)] : []), ]; const layoutStatus = ( diff --git a/src/application/decompileDirectory/exportTree.ts b/src/application/decompileDirectory/exportTree.ts index 0f96fe795c..93e6a4548c 100644 --- a/src/application/decompileDirectory/exportTree.ts +++ b/src/application/decompileDirectory/exportTree.ts @@ -105,6 +105,13 @@ export const classifyOutputPath = async ( ); }; +/** Retained records whose body ranges are unknown. */ +export interface UnknownBodies { + readonly count: number; + /** The reason every such record gives, or undefined when they differ. */ + readonly sharedReason: string | undefined; +} + /** Owns one export directory and its append-only journal. */ export class DecompileExport { private constructor( @@ -148,6 +155,27 @@ export class DecompileExport { return this.written.get(address); } + /** + * Retained non-external records whose body ranges are unknown, with their + * reason when every such record shares one. The index lists every retained + * record, so its summary must come from these records rather than from the + * functions the latest call attempted. External records have no file for a + * body caveat to qualify. + */ + unknownBodies(): UnknownBodies { + let count = 0; + let sharedReason: string | undefined; + for (const record of this.records.values()) { + if (record.status === "external" || record.body.available) continue; + sharedReason = + count === 0 || sharedReason === record.body.reason + ? record.body.reason + : undefined; + count += 1; + } + return { count, sharedReason }; + } + /** * Stable relative path for an address. The first path the journal gave this * address wins, even when a later failure record names no path. diff --git a/src/application/desktop/DesktopApplicationCapture.test.ts b/src/application/desktop/DesktopApplicationCapture.test.ts index 272d952f01..1740804ae8 100644 --- a/src/application/desktop/DesktopApplicationCapture.test.ts +++ b/src/application/desktop/DesktopApplicationCapture.test.ts @@ -353,7 +353,6 @@ it.skipIf(process.platform !== "linux")( spy.mockRestore(); } }, - 30_000, ); describe.skipIf(process.platform !== "linux")( @@ -416,7 +415,7 @@ describe.skipIf(process.platform !== "linux")( expect( JSON.parse(await readFile(join(output, "capture.json"), "utf8")), ).toEqual(JSON.parse(JSON.stringify(evidence))); - }, 30_000); + }); it("keeps a baseline-only capture when cancelled before any checkpoint", async () => { const { root, home, environment } = await fixture(); @@ -517,7 +516,7 @@ describe.skipIf(process.platform !== "linux")( after: expect.objectContaining({ sha256: sha256("count=2\n") }), }), ]); - }, 30_000); + }); }, ); @@ -589,7 +588,7 @@ describe.skipIf(process.platform !== "linux")( { name: "stop", trigger: "stop" }, { name: "exit", trigger: "exit" }, ]); - }, 30_000); + }); }, ); @@ -644,7 +643,6 @@ describe.skipIf(process.platform !== "linux")( details: { cleanup: "incomplete", resources: [statePath] }, }); }, - 30_000, ); }, ); @@ -678,7 +676,7 @@ describe.skipIf(process.platform !== "linux")( expect(await readdir(root)).toEqual( expect.arrayContaining(["group-term", "setsid-term"]), ); - }, 30_000); + }); it("stops the application gracefully and removes retained content when the session fails", async () => { const { root, home, environment } = await fixture(); @@ -703,7 +701,7 @@ describe.skipIf(process.platform !== "linux")( expect(await readdir(root)).toEqual( expect.arrayContaining(["group-term", "setsid-term"]), ); - }, 30_000); + }); }, ); @@ -774,7 +772,7 @@ describe.skipIf(process.platform !== "linux")( expect(capture.limitations).toContainEqual( expect.stringMatching(/^The window list could not be read in [34] /u), ); - }, 30_000); + }); }, ); diff --git a/src/application/desktop/DesktopBuildExport.ts b/src/application/desktop/DesktopBuildExport.ts index 5975916209..78f025a10c 100644 --- a/src/application/desktop/DesktopBuildExport.ts +++ b/src/application/desktop/DesktopBuildExport.ts @@ -7,7 +7,6 @@ import { READER_RUNNER_SLACK_S, readerTimeout, } from "../../domain/desktop/appTaskReaders.js"; -import { BUILD_REA_TIMEOUT_S } from "../../desktop/BuildComputerController.js"; import { BuildComputerLongRuns } from "../../desktop/BuildComputerLongRuns.js"; import { AnalysisError } from "../../domain/analysisErrorBase.js"; import { AnalysisCancelledError } from "../../domain/analysisErrorCore.js"; @@ -32,6 +31,7 @@ import { jsonValueSchema, type JsonValue } from "../../domain/jsonValue.js"; import { err, ok, type Result } from "../../domain/result.js"; import { AGENTENV_PROVIDER } from "../../desktop/AgentEnvComputerHelper.js"; import { + BUILD_REA_TIMEOUT_S, fetchHomeFolder, type ControlledComputer, } from "../../desktop/BuildComputerController.js"; diff --git a/src/application/desktop/DesktopBuildOutputRead.test.ts b/src/application/desktop/DesktopBuildOutputRead.test.ts index 273f0a60bd..8819f9ad72 100644 --- a/src/application/desktop/DesktopBuildOutputRead.test.ts +++ b/src/application/desktop/DesktopBuildOutputRead.test.ts @@ -3,10 +3,7 @@ import { describe, expect, it } from "vitest"; import { AnalysisResourceConstraintError } from "../../domain/analysisErrorCore.js"; import { redactSecrets } from "../../domain/desktop/buildSecrets.js"; import { createEvidence } from "../../domain/evidence.js"; -import { - GUEST_FILE_ANSWER_SOURCE, - guestFileAnswerCeiling, -} from "../../desktop/AgentEnvComputer.js"; +import { guestFileAnswerCeiling } from "../../desktop/AgentEnvComputer.js"; import { readOutput } from "./DesktopBuildCommands.js"; import { endedCapture } from "./DesktopBuildCaptureFailure.js"; import { @@ -38,7 +35,7 @@ describe("readOutput", () => { guest_file_bytes: Number.MAX_SAFE_INTEGER, file_count: 1, answer_ceiling_bytes: guestFileAnswerCeiling(), - ceiling_source: GUEST_FILE_ANSWER_SOURCE, + ceiling_source: "v8.heap_size_limit/4", }); }); @@ -101,7 +98,7 @@ describe("endedCapture", () => { guest_file_bytes: Number.MAX_SAFE_INTEGER, file_count: 1, answer_ceiling_bytes: guestFileAnswerCeiling(), - ceiling_source: GUEST_FILE_ANSWER_SOURCE, + ceiling_source: "v8.heap_size_limit/4", }); }); diff --git a/src/application/desktop/DesktopDconfDiagnostics.test.ts b/src/application/desktop/DesktopDconfDiagnostics.test.ts new file mode 100644 index 0000000000..9835d47a57 --- /dev/null +++ b/src/application/desktop/DesktopDconfDiagnostics.test.ts @@ -0,0 +1,64 @@ +import { writeFile } from "node:fs/promises"; +import { join } from "node:path"; +import { expect, it } from "vitest"; + +import { createTestTempDirectory } from "../../../tests/fixtures/temporaryDirectory.js"; +import { dumpDconfDatabase } from "../../desktop/DconfDatabaseReader.js"; +import { desktopApplicationStateSchema } from "../../domain/desktop/desktopState.js"; +import { inspectDesktopApplicationState } from "./DesktopApplicationState.js"; +import { + HOME, + capture, + checkpoint, + readers, + retained, + writeCaptureFolder, +} from "./desktopCapture.fixture.js"; + +it.skipIf(process.platform === "win32")( + "retains a dconf command failure in state Evidence", + async () => { + const tools = await createTestTempDirectory("dconf-state-diagnostics-"); + const command = join(tools, "dconf"); + await writeFile(command, "#!/bin/sh\nexit 17\n", { mode: 0o600 }); + const base = capture(); + const path = `${HOME}/.config/dconf/user`; + const bytes = Buffer.from("GVariant\0\0\0\0"); + const folder = await writeCaptureFolder( + { + ...base, + state: { + ...base.state, + content_roots: [path], + checkpoints: [ + checkpoint("baseline", "baseline", [], [retained(path, bytes)]), + ], + }, + }, + undefined, + [bytes], + ); + const evidence = await inspectDesktopApplicationState(folder, { + ...readers, + dconf: (database, signal) => + dumpDconfDatabase(database, { REA_DCONF_COMMAND: command }, signal), + }); + if (!("normalized_result" in evidence)) throw evidence; + const state = desktopApplicationStateSchema.parse( + evidence.normalized_result, + ); + expect(state.paths).toEqual([ + expect.objectContaining({ + home_path: ".config/dconf/user", + versions: [ + expect.objectContaining({ + format: "dconf", + view: null, + view_error: expect.stringContaining("EACCES"), + }), + ], + }), + ]); + expect(state.paths[0]?.versions[0]?.view_error).toContain(command); + }, +); diff --git a/src/application/desktop/DesktopTaskExport.test.ts b/src/application/desktop/DesktopTaskExport.test.ts index 03cb266ca3..4cda72255e 100644 --- a/src/application/desktop/DesktopTaskExport.test.ts +++ b/src/application/desktop/DesktopTaskExport.test.ts @@ -6,7 +6,7 @@ import { describe, expect, it } from "vitest"; import { createTestTempDirectory } from "../../../tests/fixtures/temporaryDirectory.js"; import { AnalysisInputError } from "../../domain/analysisErrorCore.js"; import { exportDesktopApplication } from "./DesktopApplicationExport.js"; -import { namesApp, shippedSeedCheckView } from "./DesktopTaskExport.js"; +import { shippedSeedCheckView } from "./DesktopTaskExport.js"; import { draftDesktopApplicationTask } from "./DesktopTaskDraft.js"; import { HOME, @@ -802,17 +802,20 @@ describe("exportDesktopApplication with a task's prompt", () => { expect(naming(named.result.limitations)).toEqual([]); }); - it("does not treat a word that only contains the app's name as naming it", () => { - const dia = { title: "Dia", name: "dia", aptPackage: "dia" }; - expect(namesApp("Update the diagram and save it.", dia)).toBe(false); - expect(namesApp("In Dia, add a box.", dia)).toBe(true); - expect( - namesApp("In Notes, write down milk.", { - title: "Notes", - name: "notes", - aptPackage: "notes", - }), - ).toBe(true); + it("does not treat a word that only contains the app's name as naming it", async () => { + const checks = [ + { id: "milk", kind: "lines_present", file: NOTE, lines: ["milk"] }, + ]; + const exported = await exportWithTasks({ + ...task(checks), + prompt: "Update the notesheet and save it.", + }); + if (exported.result instanceof Error) throw exported.result; + expect(exported.result.limitations).toEqual( + expect.arrayContaining([ + expect.stringMatching(/prompt.*does not name the app/iu), + ]), + ); }); }); diff --git a/src/application/desktop/DesktopTaskExport.ts b/src/application/desktop/DesktopTaskExport.ts index 5014d398df..0c64f6441c 100644 --- a/src/application/desktop/DesktopTaskExport.ts +++ b/src/application/desktop/DesktopTaskExport.ts @@ -490,7 +490,7 @@ const admitScreenTask = async ( }; /** Whether a prompt names the app by its title, environment or package, in any letter case. */ -export const namesApp = ( +const namesApp = ( prompt: string, { title, diff --git a/src/application/electron/ElectronFuseService.ts b/src/application/electron/ElectronFuseService.ts index 7a23c862fb..5862b903a3 100644 --- a/src/application/electron/ElectronFuseService.ts +++ b/src/application/electron/ElectronFuseService.ts @@ -8,7 +8,7 @@ import { } from "../../domain/electron/electronFuses.js"; import type { Evidence } from "../../domain/evidence.js"; import { createElectronFuseEvidence } from "../../domain/electron/electronFuseEvidence.js"; -import { err, ok, type Result } from "../../domain/result.js"; +import { err, type Result } from "../../domain/result.js"; import { completeProviderEvidence } from "../ProviderOperationExecution.js"; import { parseAnalysisInput } from "../AnalysisInput.js"; diff --git a/src/application/environment/environmentBuild.test.ts b/src/application/environment/environmentBuild.test.ts index 6229505826..5cabed56d2 100644 --- a/src/application/environment/environmentBuild.test.ts +++ b/src/application/environment/environmentBuild.test.ts @@ -33,7 +33,6 @@ import { inspectAnalysisViewValidated } from "../analysisView/AnalysisViewServic import { DEFAULT_BUILD_TEMPLATE_RULES, buildEnvironment, - canonicalTemplateRules, classifyBuildCapture, type EnvironmentBuildInput, } from "./environmentBuild.js"; @@ -497,12 +496,20 @@ describe("classifyBuildCapture", () => { }); }); -describe("canonicalTemplateRules", () => { - it("orders rules as the views apply them", () => { - expect( - canonicalTemplateRules(["prefix-variance", "after-uid-literal"]), - ).toEqual(["after-uid-literal", "prefix-variance"]); - }); +it("reports template rules in the order the views apply them", async () => { + const root = await createTestTempDirectory("rea-environment-rule-order-"); + const built = await buildEnvironment( + await buildInput(root, undefined, { + dryRun: true, + directory: undefined, + templateRules: ["prefix-variance", "after-uid-literal"], + }), + ); + if (!built.ok) throw built.error; + expect(built.value.manifest.template_rules).toEqual([ + "after-uid-literal", + "prefix-variance", + ]); }); describe("buildEnvironment credential names", () => { diff --git a/src/application/environment/environmentBuild.ts b/src/application/environment/environmentBuild.ts index 6c53935d4f..2bf05f09ad 100644 --- a/src/application/environment/environmentBuild.ts +++ b/src/application/environment/environmentBuild.ts @@ -14,7 +14,6 @@ import { TEMPLATE_RULES, type TemplateRule, } from "../../domain/analysisView/environmentManifestRules.js"; -import type { Evidence } from "../../domain/evidence.js"; import type { EnvironmentBuildReport } from "../../domain/environmentBundleExport.js"; import { err, ok, type Result } from "../../domain/result.js"; import { inspectAnalysisView } from "../analysisView/AnalysisViewService.js"; @@ -86,7 +85,7 @@ export const classifyBuildCapture = ( }; /** Rules in the views' fixed order, so the report and manifest list them canonically. */ -export const canonicalTemplateRules = ( +const canonicalTemplateRules = ( rules: readonly TemplateRule[], ): TemplateRule[] => TEMPLATE_RULES.filter((rule) => rules.includes(rule)); diff --git a/src/application/environment/environmentBuildReport.ts b/src/application/environment/environmentBuildReport.ts index c183968231..4ea6ec4054 100644 --- a/src/application/environment/environmentBuildReport.ts +++ b/src/application/environment/environmentBuildReport.ts @@ -1,3 +1,4 @@ +import { escapeJsonPointerToken } from "../../domain/jsonPointer.js"; import { z } from "zod"; import { basename } from "node:path"; import { isIP } from "node:net"; @@ -83,9 +84,6 @@ const SCAN_BODY_LIMIT_BYTES = 4 * 1024 * 1024; /** Pointers the report lists; the count covers every match. */ const REPORTED_POINTERS = 20; -const pointerToken = (name: string): string => - name.replaceAll("~", "~0").replaceAll("/", "~1"); - /** A held value: a non-empty string, or an array holding one. */ const holdsValue = (value: unknown): boolean => (typeof value === "string" && value !== "") || @@ -198,7 +196,7 @@ const credentialTally = () => { const entries = Object.entries(value); for (let index = entries.length - 1; index >= 0; index--) { const [name, child] = entries[index]!; - const path = `${pointer}/${pointerToken(name)}`; + const path = `${pointer}/${escapeJsonPointerToken(name)}`; field(`${label} ${path}`, name, child); stack.push([child, path]); } @@ -209,7 +207,7 @@ const credentialTally = () => { else if (scanned?.kind === "json") walk(scanned.value, label); else if (scanned?.kind === "form") for (const [name, value] of scanned.fields) - field(`${label} /${pointerToken(name)}`, name, value); + field(`${label} /${escapeJsonPointerToken(name)}`, name, value); }; return { field, diff --git a/src/application/environment/environmentBundle.test.ts b/src/application/environment/environmentBundle.test.ts index 0da059f5a1..7a4b4773f6 100644 --- a/src/application/environment/environmentBundle.test.ts +++ b/src/application/environment/environmentBundle.test.ts @@ -14,8 +14,6 @@ import { import { projectAnalysisError } from "../../domain/analysisErrorProjection.js"; import { PACKAGE_METADATA } from "../../generatedPackageMetadata.js"; import { - BUNDLE_BASE_IMAGE, - bundleDockerfile, ENVIRONMENT_EXPORT_OPERATION, planEnvironmentBundle, registryPackageSource, @@ -115,7 +113,7 @@ describe("planEnvironmentBundle", () => { it("pins the registry package to this build's exact version in the Dockerfile", () => { const dockerfile = file(plan(), "envs/grafana-example/Dockerfile"); const lines = dockerfile.trimEnd().split("\n"); - expect(lines[0]).toBe(`FROM ${BUNDLE_BASE_IMAGE}`); + expect(lines[0]).toMatch(/^FROM node:\d+\.\d+\.\d+-bookworm-slim$/u); expect(lines).toContain("ENV MCP_HOST=0.0.0.0"); expect(lines).toContain("ENV MCP_PORT=18765"); expect(lines).toContain( @@ -149,7 +147,9 @@ describe("planEnvironmentBundle", () => { "RUN npm install -g --ignore-scripts --no-fund --no-audit /tmp/rea-agents.tgz && rm /tmp/rea-agents.tgz\n", ); expect(dockerfile).not.toContain(`rea-agents@${PACKAGE_METADATA.version}`); - expect(bundleDockerfile(registryPackageSource())).not.toBe(dockerfile); + expect(file(plan(), "envs/grafana-example/Dockerfile")).not.toBe( + dockerfile, + ); }); it("writes a verifier that checks the served tool count and the status the server answers the first parameterless GET tool with", () => { diff --git a/src/application/environment/environmentBundle.ts b/src/application/environment/environmentBundle.ts index f381c2e47c..5bdc028cb8 100644 --- a/src/application/environment/environmentBundle.ts +++ b/src/application/environment/environmentBundle.ts @@ -21,15 +21,15 @@ import type { EnvironmentToolDefinition } from "./environmentTools.js"; export const ENVIRONMENT_EXPORT_OPERATION = "environment-export"; /** The image the exported Dockerfile builds from; the agentenv runner builds it with Docker. */ -export const BUNDLE_BASE_IMAGE = `node:${PACKAGE_METADATA.pinnedNodeVersion}-bookworm-slim`; +const BUNDLE_BASE_IMAGE = `node:${PACKAGE_METADATA.pinnedNodeVersion}-bookworm-slim`; /** The port the agentenv framework maps for an MCP server env; it sets the host but not the port. */ -export const BUNDLE_MCP_PORT = 18765; +const BUNDLE_MCP_PORT = 18765; /** Host names the exported env container is addressed by: the published loopback port and the compose service name. */ -export const BUNDLE_ALLOWED_HOSTS = "127.0.0.1,localhost"; +const BUNDLE_ALLOWED_HOSTS = "127.0.0.1,localhost"; /** Name of the packed CLI inside the env folder when a tarball is exported. */ -export const BUNDLE_TARBALL_NAME = "rea-agents.tgz"; +const BUNDLE_TARBALL_NAME = "rea-agents.tgz"; /** Where the Dockerfile installs the CLI from. */ export type EnvironmentPackageSource = @@ -152,7 +152,7 @@ const READINESS_PROBE_LAYER = "RUN apt-get update && apt-get install -y --no-install-recommends python3 && rm -rf /var/lib/apt/lists/*"; /** The Dockerfile text: pinned base image, readiness probe interpreter, pinned CLI, listener defaults, non-root user. */ -export const bundleDockerfile = ( +const bundleDockerfile = ( source: EnvironmentPackageSource, name?: string, ): string => @@ -196,7 +196,7 @@ const pythonLiteral = (value: string | number | null): string => value === null ? "None" : JSON.stringify(value); /** The verifier script in the shape of the agentenv echo example's `verify(mcp_url)`. */ -export const bundleVerifier = ( +const bundleVerifier = ( plan: Pick, ): string => [ @@ -241,7 +241,7 @@ export const bundleVerifier = ( ].join("\n"); /** The smoke task: deploy the env locally, then run the verifier against it. */ -export const bundleSmokeTask = (name: string): string => +const bundleSmokeTask = (name: string): string => `${JSON.stringify( [ { id: "env", type: "deploy_env", env_id: name, sandbox_type: "local" }, @@ -280,10 +280,7 @@ from their recordings because the state is unavailable }; /** The bundle README: provenance, what the environment serves and does not establish, how to run it, and the rights note. */ -export const bundleReadme = ( - source: EnvironmentSource, - plan: ReadmePlan, -): string => { +const bundleReadme = (source: EnvironmentSource, plan: ReadmePlan): string => { const { manifest } = source; const check = plan.check === null diff --git a/src/application/environment/environmentCompareDiff.ts b/src/application/environment/environmentCompareDiff.ts index 020db4ced3..a596f133eb 100644 --- a/src/application/environment/environmentCompareDiff.ts +++ b/src/application/environment/environmentCompareDiff.ts @@ -1,3 +1,4 @@ +import { escapeJsonPointerToken } from "../../domain/jsonPointer.js"; import type { JsonObject } from "../../domain/analysisView/environmentStateRules.js"; import type { FieldRule } from "../../domain/analysisView/environmentStateWriteRules.js"; import { matchesClockFormat } from "../../domain/analysisView/environmentStateWriteClockFormats.js"; @@ -74,9 +75,6 @@ const shown = (value: JsonValue | undefined): string => { return text.length > VALUE_CHARS ? `${text.slice(0, VALUE_CHARS)}…` : text; }; -const escapePointer = (key: string): string => - key.replaceAll("~", "~0").replaceAll("/", "~1"); - const formatKey = (format: ClockFormat): string => format.kind === "epoch" ? `epoch-${format.unit}` @@ -427,7 +425,7 @@ class Differ { ? { agreed: new Set(), fields: new Map() } : undefined; for (const [key, value] of Object.entries(live)) { - const child = `${pointer}/${escapePointer(key)}`; + const child = `${pointer}/${escapeJsonPointerToken(key)}`; const other = envKey(key); // A translated key and a verbatim key may name the same entry; an // environment member can account for only one live member. @@ -460,7 +458,7 @@ class Differ { ? undefined : { ...generation, - pointer: `${generation.pointer}/${escapePointer(key)}`, + pointer: `${generation.pointer}/${escapeJsonPointerToken(key)}`, }, ); if (this.differences.length === before) composed?.agreed.add(key); @@ -489,7 +487,7 @@ class Differ { } else this.add({ category: "value", - pointer: `${pointer}/${escapePointer(rule.field)}`, + pointer: `${pointer}/${escapeJsonPointerToken(rule.field)}`, live: shown(live[rule.field]), env: shown(env[other]), }); @@ -499,7 +497,7 @@ class Differ { if (!matched.has(key)) this.add({ category: "extra-field", - pointer: `${pointer}/${escapePointer(key)}`, + pointer: `${pointer}/${escapeJsonPointerToken(key)}`, env: shown(value), }); } diff --git a/src/application/environment/environmentCompareIds.ts b/src/application/environment/environmentCompareIds.ts index ecab1767ac..8862fdb512 100644 --- a/src/application/environment/environmentCompareIds.ts +++ b/src/application/environment/environmentCompareIds.ts @@ -1,3 +1,4 @@ +import { escapeJsonPointerToken } from "../../domain/jsonPointer.js"; import type { JsonObject } from "../../domain/analysisView/environmentStateRules.js"; import type { BodyAddress } from "../../domain/analysisView/environmentStateWriteRules.js"; import { isUuid } from "../../domain/analysisView/environmentStateWriteValues.js"; @@ -315,7 +316,7 @@ const idPair = ( : undefined; const childPointer = (at: string, key: string | number): string => - `${at}/${String(key).replaceAll("~", "~0").replaceAll("/", "~1")}`; + `${at}/${escapeJsonPointerToken(String(key))}`; const learnAt = ( live: JsonValue | undefined, diff --git a/src/application/environment/environmentQueryText.test.ts b/src/application/environment/environmentQueryText.test.ts index 1e52df3078..6f123857ce 100644 --- a/src/application/environment/environmentQueryText.test.ts +++ b/src/application/environment/environmentQueryText.test.ts @@ -28,7 +28,7 @@ describe("query values given as numbers or booleans", () => { body: { maxSize: 3 }, }); const strings = { maxSize: "2", offset: ["0"] }; - expect(withQueryText(tool, strings)).toBe(strings); + expect(withQueryText(tool, strings)).toEqual(strings); }); it("answers a list paged by a number as it answers the same page size given as text", () => { diff --git a/src/application/environment/environmentState.test.ts b/src/application/environment/environmentState.test.ts index 51f47a55cc..44479e8d40 100644 --- a/src/application/environment/environmentState.test.ts +++ b/src/application/environment/environmentState.test.ts @@ -33,10 +33,7 @@ import { STATE_INTAKE_LIMIT_BYTES, parseStateDocument, } from "./environmentStateDocument.js"; -import { - EnvironmentStateStore, - ENVIRONMENT_COLLECTION_HISTORY_LIMIT_BYTES, -} from "./environmentStateStore.js"; +import { EnvironmentStateStore } from "./environmentStateStore.js"; const fixture = environmentStateServeFixture(); const { source, state } = environmentStateServeInputs(fixture); @@ -189,12 +186,12 @@ describe("served collection history", () => { if (refused === null) throw new Error("Expected reported history budget details"); expect(refused).toEqual(run()); - expect(refused).toMatchObject({ - max_collection_history_bytes: ENVIRONMENT_COLLECTION_HISTORY_LIMIT_BYTES, - }); - expect(refused.collection_history_bytes).toBeGreaterThan( - ENVIRONMENT_COLLECTION_HISTORY_LIMIT_BYTES, - ); + const maximum = refused.max_collection_history_bytes; + expect(maximum).toEqual(expect.any(Number)); + if (typeof maximum !== "number") + throw new Error("Expected a history limit"); + expect(maximum).toBeGreaterThan(0); + expect(refused.collection_history_bytes).toBeGreaterThan(maximum); }); it("does not charge a deleted loaded collection again at the history limit", () => { diff --git a/src/application/environment/environmentStateDocument.ts b/src/application/environment/environmentStateDocument.ts index 85febafb2e..6877f97b8a 100644 --- a/src/application/environment/environmentStateDocument.ts +++ b/src/application/environment/environmentStateDocument.ts @@ -1,3 +1,4 @@ +import { environmentEntityKey } from "../../domain/analysisView/environmentStateIdentity.js"; import { types } from "node:util"; import { z } from "zod"; @@ -28,7 +29,6 @@ import { type JsonValue, } from "../../domain/jsonValue.js"; import { err, ok, type Result } from "../../domain/result.js"; -import { compositeKey } from "../../domain/unicodeCodePointOrder.js"; import type { EnvironmentState } from "./environmentState.js"; import { ENVIRONMENT_SERVE_OPERATION } from "./environmentSource.js"; import { REA_STATE_FORMAT } from "./environmentTools.js"; @@ -216,7 +216,7 @@ interface EntityCheck { } const entityKey = (entity: Pick) => - compositeKey([entity.type, entity.collection, entity.id]); + environmentEntityKey(entity.type, entity.collection, entity.id); /** The first problem with an entity's view values: each names an item or list view of its type once and keeps its identity. */ const viewFieldsProblem = ( diff --git a/src/application/environment/environmentStateStore.ts b/src/application/environment/environmentStateStore.ts index b6e9d99a5e..45818277a2 100644 --- a/src/application/environment/environmentStateStore.ts +++ b/src/application/environment/environmentStateStore.ts @@ -1,3 +1,7 @@ +import { + environmentCollectionKey, + environmentEntityKey, +} from "../../domain/analysisView/environmentStateIdentity.js"; import { markedDeleted, type StateEntity, @@ -11,7 +15,6 @@ import { import type { JsonValue } from "../../domain/jsonValue.js"; import { jsonByteLength } from "../../domain/jsonSerialization.js"; import { err, ok, type Result } from "../../domain/result.js"; -import { compositeKey } from "../../domain/unicodeCodePointOrder.js"; import { ENVIRONMENT_SERVE_OPERATION } from "./environmentSource.js"; import type { EnvironmentState } from "./environmentState.js"; import { @@ -22,7 +25,7 @@ import { } from "./environmentStateDocument.js"; /** Maximum logical budget for collection spellings added by served creates since reset/load. */ -export const ENVIRONMENT_COLLECTION_HISTORY_LIMIT_BYTES = 1024 * 1024; +const ENVIRONMENT_COLLECTION_HISTORY_LIMIT_BYTES = 1024 * 1024; const collectionHistoryCost = ( state: EnvironmentState, @@ -259,7 +262,7 @@ export class EnvironmentStateStore { entity: StateEntity, index: number, ): Result { - const key = compositeKey([entity.type, entity.collection, entity.id]); + const key = environmentEntityKey(entity.type, entity.collection, entity.id); const replaced = this.#storedDeleted.get(key); const entities = replaced === undefined @@ -339,7 +342,7 @@ export class EnvironmentStateStore { const replaced = new Map(); for (const entity of entities) { const held = this.#entities.get( - compositeKey([entity.type, entity.collection, entity.id]), + environmentEntityKey(entity.type, entity.collection, entity.id), ); if (held !== undefined) replaced.set(held, entity); } @@ -351,7 +354,7 @@ export class EnvironmentStateStore { const bytes = this.#grown(delta); if (!bytes.ok) return bytes; const key = (entity: StateEntity) => - compositeKey([entity.type, entity.collection, entity.id]); + environmentEntityKey(entity.type, entity.collection, entity.id); const placed = new Set(order.map(key)); let next = 0; this.#install( @@ -377,7 +380,7 @@ export class EnvironmentStateStore { const held = new Set( entities.flatMap((entity) => { const found = this.#entities.get( - compositeKey([entity.type, entity.collection, entity.id]), + environmentEntityKey(entity.type, entity.collection, entity.id), ); return found === undefined ? [] : [found]; }), @@ -400,14 +403,14 @@ export class EnvironmentStateStore { collection: string, id: string, ): StateEntity | undefined { - const key = compositeKey([type, collection, id]); + const key = environmentEntityKey(type, collection, id); return this.#entities.has(key) ? undefined : (this.#storedDeleted.get(key) ?? this.#deleted.get(key)); } #keepDeleted(entity: StateEntity): void { - const key = compositeKey([entity.type, entity.collection, entity.id]); + const key = environmentEntityKey(entity.type, entity.collection, entity.id); const earlier = this.#deleted.get(key); if (earlier !== undefined) { this.#deleted.delete(key); @@ -472,12 +475,14 @@ export class EnvironmentStateStore { collection: string, id: string, ): StateEntity | undefined { - return this.#entities.get(compositeKey([type, collection, id])); + return this.#entities.get(environmentEntityKey(type, collection, id)); } /** One collection's entities in state order. */ inCollection(type: string, collection: string): readonly StateEntity[] { - return this.#collections.get(compositeKey([type, collection])) ?? []; + return ( + this.#collections.get(environmentCollectionKey(type, collection)) ?? [] + ); } /** The collections holding entities of one type, in state order of their first entity. */ @@ -529,7 +534,7 @@ export class EnvironmentStateStore { for (const entity of content.entities) { if (markedDeleted(entity.fields)) { storedDeleted.set( - compositeKey([entity.type, entity.collection, entity.id]), + environmentEntityKey(entity.type, entity.collection, entity.id), entity, ); const named = typeCollections.get(entity.type) ?? []; @@ -538,10 +543,10 @@ export class EnvironmentStateStore { continue; } entities.set( - compositeKey([entity.type, entity.collection, entity.id]), + environmentEntityKey(entity.type, entity.collection, entity.id), entity, ); - const key = compositeKey([entity.type, entity.collection]); + const key = environmentCollectionKey(entity.type, entity.collection); const members = collections.get(key); if (members !== undefined) { members.push(entity); diff --git a/src/application/environment/environmentStateWriteAnswer.ts b/src/application/environment/environmentStateWriteAnswer.ts index 1e235795a3..8c1b1b6f68 100644 --- a/src/application/environment/environmentStateWriteAnswer.ts +++ b/src/application/environment/environmentStateWriteAnswer.ts @@ -1,3 +1,4 @@ +import { jsonPointerTokens } from "../../domain/jsonPointer.js"; import { createdFields, documentBodyProblem, @@ -53,7 +54,6 @@ import { collectionMatch, decodedSegment, isObject, - pointerTokens, scalarText, type JsonObject, type StateEntity, @@ -435,7 +435,7 @@ export const writeAnswer = ( // A member the call sent and the plan stores as sent is answered as sent; any other keeps the recorded answer. unloaded: (plan.observation?.read?.unloaded ?? []).filter( (path) => { - const [field] = pointerTokens(path); + const [field] = jsonPointerTokens(path); return !( rendered.sent?.includes(field!) === true && plan.rules.some( diff --git a/src/application/environment/environmentTools.test.ts b/src/application/environment/environmentTools.test.ts index c1662f9521..344dd80fc5 100644 --- a/src/application/environment/environmentTools.test.ts +++ b/src/application/environment/environmentTools.test.ts @@ -6,7 +6,6 @@ import type { RecordedExchange, } from "./environmentSource.js"; import { - ENVIRONMENT_CATALOG_LIMIT_BYTES, environmentCard, environmentToolArguments, environmentToolDefinitions as prepareDefinitions, @@ -352,7 +351,7 @@ describe("environment tool response summaries", () => { error: { _tag: "AnalysisResourceConstraintError", resource: "capacity", - reportedLimits: { max_catalog_bytes: ENVIRONMENT_CATALOG_LIMIT_BYTES }, + reportedLimits: { max_catalog_bytes: expect.any(Number) }, }, }); const [single] = environmentToolDefinitions( diff --git a/src/application/environment/environmentTools.ts b/src/application/environment/environmentTools.ts index f0446bdf4f..41f61f1841 100644 --- a/src/application/environment/environmentTools.ts +++ b/src/application/environment/environmentTools.ts @@ -1,3 +1,4 @@ +import { jsonPointerTokens } from "../../domain/jsonPointer.js"; import { ownMember, isObject, @@ -5,7 +6,6 @@ import { isSafeHttpMethod, listItems, locateList, - pointerTokens, type JsonObject, } from "../../domain/analysisView/environmentStateRules.js"; import { AnalysisResourceConstraintError } from "../../domain/analysisErrorCore.js"; @@ -170,7 +170,7 @@ const describeJson = (value: JsonValue): string => { const pointer = locateList(value, undefined); const items = pointer === undefined ? [] : (listItems(value, pointer) ?? []); if (pointer !== undefined && items.length > 0) { - const [member] = pointerTokens(pointer); + const [member] = jsonPointerTokens(pointer); return member === undefined ? `a JSON array of objects ${withFields(items)}` : `a JSON object whose ${describeText(member, "member", "capture Evidence", JSON.stringify)} lists objects ${withFields(items)}`; @@ -231,7 +231,7 @@ const toolDescription = ( `${REPLAY_CATALOG_INSTRUCTIONS} ${describeText(tool.method, "method")} ${describeText(tool.path_template, "path")}; ${describeResponses(tool, source.exchanges)}; ${describe?.(tool) ?? answers} [${describeList(tool.record_ordinals, tool.record_ordinals.length)}]; evidence ${source.evidenceId}`; /** Captured argument names longer than this are advertised under a short alias; the manifest keeps the name. */ -export const MAX_ARGUMENT_NAME_CHARS = 64; +const MAX_ARGUMENT_NAME_CHARS = 64; const bodyDescription = (tool: EnvironmentManifestTool): string => { const properties = tool.body_shape?.properties; @@ -377,7 +377,7 @@ function* environmentToolProperties( } /** Serialization reservation calibrated with 1500 fields and eight concurrent SDK discovery requests. */ -export const ENVIRONMENT_CATALOG_LIMIT_BYTES = 1024 * 1024; +const ENVIRONMENT_CATALOG_LIMIT_BYTES = 1024 * 1024; const EMPTY_PROPERTY_BYTES = JSON.stringify({ type: "string", description: "" }).length + 4; const EMPTY_TOOL_BYTES = JSON.stringify({ @@ -530,10 +530,10 @@ export const environmentToolDefinitions = ( export const REA_STATE_FORMAT = "rea-state/v1"; /** The data-plane methods an environment served from a state answers. */ -export const STATE_OPERATIONS = ["data/reset", "data/add", "data/get"] as const; +const STATE_OPERATIONS = ["data/reset", "data/add", "data/get"] as const; /** The intake declaration a state-served card carries: what `data/add` loads and `data/get` returns. */ -export const STATE_INTAKE_EXTENSION = { +const STATE_INTAKE_EXTENSION = { uri: "urn:agentenv:intake/v1", description: `data/add replaces the whole environment state with one ${REA_STATE_FORMAT} document; data/get returns the current state in the same format.`, params: { diff --git a/src/application/javascript/ElectronBoundaryAnalysis.ts b/src/application/javascript/ElectronBoundaryAnalysis.ts index b98bdbaaa9..a60c1f32c9 100644 --- a/src/application/javascript/ElectronBoundaryAnalysis.ts +++ b/src/application/javascript/ElectronBoundaryAnalysis.ts @@ -172,7 +172,7 @@ const electronModuleReferencedBy = ({ }; /** Whether any analyzed source loads an Electron module. */ -export const electronModuleReferenced = ( +const electronModuleReferenced = ( analysis: JavaScriptArtifactAnalysis, ): boolean => analysis.files.some(electronModuleReferencedBy); diff --git a/src/application/javascript/JavaScriptApplicationService.ts b/src/application/javascript/JavaScriptApplicationService.ts index fad0217392..d7cee78469 100644 --- a/src/application/javascript/JavaScriptApplicationService.ts +++ b/src/application/javascript/JavaScriptApplicationService.ts @@ -1,3 +1,4 @@ +import { escapeJsonPointerToken } from "../../domain/jsonPointer.js"; import type { AnalysisSettingsReader } from "../../domain/analysisSettings.js"; import { ASAR_HEADER_ADMISSION_SETTING, @@ -145,7 +146,8 @@ export const analyzeJavaScriptApplicationValidated = async ( }); assertJavaScriptAnalysisActive(options.signal); return ok(evidence); - } catch (cause: unknown) { + } catch (caught: unknown) { + let cause = caught; if ( cause instanceof ArtifactReaderFailure && cause.reason === "cancelled" && @@ -246,13 +248,10 @@ const describeResultSchemaFailure = (cause: z.ZodError): string => { const pointer = path.length === 0 ? "/" - : `/${path.map((part) => escapePointerPart(String(part))).join("/")}`; + : `/${path.map((part) => escapeJsonPointerToken(String(part))).join("/")}`; return `Result schema rejected ${String(cause.issues.length)} ${cause.issues.length === 1 ? "issue" : "issues"} at ${pointer} (${issue.code})`; }; -const escapePointerPart = (part: string): string => - part.replaceAll("~", "~0").replaceAll("/", "~1"); - const FILESYSTEM_ERROR_CODES = new Set([ "ENOENT", "EACCES", diff --git a/src/application/javascript/JavaScriptArtifactAnalysis.order.test.ts b/src/application/javascript/JavaScriptArtifactAnalysis.order.test.ts index 3cea7a14ca..d384e44131 100644 --- a/src/application/javascript/JavaScriptArtifactAnalysis.order.test.ts +++ b/src/application/javascript/JavaScriptArtifactAnalysis.order.test.ts @@ -1,8 +1,5 @@ import { expect, it } from "vitest"; -import { - analyzeAndAdoptJavaScriptArtifactFiles, - javaScriptAnalysisOrder, -} from "./JavaScriptArtifactAnalysis.js"; +import { analyzeAndAdoptJavaScriptArtifactFiles } from "./JavaScriptArtifactAnalysis.js"; import { createJavaScriptSemanticGraphProjection } from "./JavaScriptSemanticGraphBuilder.js"; import { AnalysisResourceConstraintError } from "../../domain/analysisErrorCore.js"; import { analyzeJavaScriptStaticSource } from "../../../tests/fixtures/javascriptStaticAnalysis.js"; @@ -37,24 +34,6 @@ const inventory = [ manifest("package.json"), ]; -it("offers application JavaScript before dependency JavaScript", () => { - expect(javaScriptAnalysisOrder(inventory).map(({ path }) => path)).toEqual([ - "node_modules/a/package.json", - "out-global-online/main/index.js", - "out-global-online/preload/index.js", - "package.json", - "node_modules/a/index.js", - "node_modules/b/lib/yaml.js", - ]); - // A package merely named like a dependency directory is not one. - expect( - javaScriptAnalysisOrder([ - source("node_modules.js"), - source("src/node_modules_shim.js"), - ]).map(({ path }) => path), - ).toEqual(["node_modules.js", "src/node_modules_shim.js"]); -}); - it("spends a limited result budget on first-party sources and keeps inventory order", async () => { const javascript = analyzeJavaScriptStaticSource("export const x = 1;"); const analyzed: string[] = []; @@ -107,6 +86,38 @@ it("spends a limited result budget on first-party sources and keeps inventory or expect(outcome.interruption).toBeNull(); }); +it("does not treat files merely named like dependency directories as dependencies", async () => { + const files = [ + source("node_modules/dep/index.js"), + source("node_modules.js"), + source("src/node_modules_shim.js"), + ]; + const analyzed: string[] = []; + const javascript = analyzeJavaScriptStaticSource("export const x = 1;"); + await analyzeAndAdoptJavaScriptArtifactFiles( + { + files, + unavailable_relevant_files: [], + containers: [], + text_bytes_read: 60, + invalid_utf8_files: 0, + }, + { + analyze: async (file) => { + analyzed.push(file.path); + return ok({ javascript, module: null, projection: null }); + }, + }, + createJavaScriptSemanticGraphProjection(), + async () => {}, + ); + expect(analyzed).toEqual([ + "node_modules.js", + "src/node_modules_shim.js", + "node_modules/dep/index.js", + ]); +}); + it("spends capacity in the caller's requested order for explicitly selected sources", async () => { const files = [ source("node_modules/selected/index.js"), diff --git a/src/application/javascript/JavaScriptArtifactAnalysis.ts b/src/application/javascript/JavaScriptArtifactAnalysis.ts index bbed58a4d3..142587a98a 100644 --- a/src/application/javascript/JavaScriptArtifactAnalysis.ts +++ b/src/application/javascript/JavaScriptArtifactAnalysis.ts @@ -329,7 +329,7 @@ const dependencySourcePath = (path: string): boolean => * preloads, and Electron windows are examined. Offer application JavaScript * first and dependency JavaScript last; metadata files keep their position. */ -export const javaScriptAnalysisOrder = ( +const javaScriptAnalysisOrder = ( files: readonly JavaScriptArtifactFile[], ): JavaScriptArtifactFile[] => { const application: JavaScriptArtifactFile[] = []; diff --git a/src/application/javascript/JavaScriptArtifactGraphElectronCoverage.test.ts b/src/application/javascript/JavaScriptArtifactGraphElectronCoverage.test.ts index c3c5f1677c..bfa873624b 100644 --- a/src/application/javascript/JavaScriptArtifactGraphElectronCoverage.test.ts +++ b/src/application/javascript/JavaScriptArtifactGraphElectronCoverage.test.ts @@ -17,7 +17,6 @@ import { javascriptArtifactAnalysisScopes } from "./JavaScriptArtifactAnalysisTy import { completeJavaScriptAnalysisSteps } from "./JavaScriptAnalysisControl.js"; import { buildImmutableJavaScriptArtifactGraphSteps as buildGraph } from "./JavaScriptArtifactGraphBuilder.js"; import { JavaScriptJsonDocuments } from "./JavaScriptJsonDocuments.js"; -import { electronModuleReferenced } from "./ElectronBoundaryAnalysis.js"; const SHA256 = "a".repeat(64); const PATH = "main.js"; @@ -178,25 +177,14 @@ describe("Electron semantic coverage", () => { expect(electronSemantics).toBeUndefined(); }); - it("does not count a type-only Electron import as boundary coverage", () => { - // A `.js` fixture cannot carry `import type`, so the module predicate is - // exercised directly here. - const analysisWith = ( - kind: "require" | "type-import", - ): JavaScriptArtifactAnalysis => - ({ - files: [ - { - file: fileFor(""), - javascript: null, - semantic: { - ir: { moduleLinks: [{ kind, specifier: "electron" }] }, - }, - }, - ], - }) as unknown as JavaScriptArtifactAnalysis; - expect(electronModuleReferenced(analysisWith("require"))).toBe(true); - expect(electronModuleReferenced(analysisWith("type-import"))).toBe(false); + it("does not count a type-only Electron import as boundary coverage", async () => { + const result = await graphFor( + 'import type { BrowserWindow } from "electron";\nexport type Window = BrowserWindow;\n', + undefined, + "main.ts", + ); + expect(result.electronSemantics).toBeUndefined(); + expect(result.limitations).not.toContain(EMPTY_COVERAGE); }); it("matches a supported Electron subpath and ignores an unrelated package", async () => { diff --git a/src/application/javascript/JavaScriptArtifactReconstruction.ts b/src/application/javascript/JavaScriptArtifactReconstruction.ts index 21e68c15e1..ba833987be 100644 --- a/src/application/javascript/JavaScriptArtifactReconstruction.ts +++ b/src/application/javascript/JavaScriptArtifactReconstruction.ts @@ -214,8 +214,11 @@ export const reconstructJavaScriptArtifact = async ( ).length, { scope }, ); - let outcome; - let cleanupFailure: AnalysisError | undefined; + let outcome!: Awaited< + ReturnType + >; + let analysisFailure: { readonly cause: unknown } | undefined; + let cleanupAttempt: Awaited>; try { outcome = await analyzeAndAdoptJavaScriptArtifactFiles( files, @@ -250,18 +253,28 @@ export const reconstructJavaScriptArtifact = async ( preloads.paths, preloads.limitations, ); + } catch (cause: unknown) { + analysisFailure = { cause }; } finally { - try { - const cleanup = await scope.release({ - kind: "javascript-source-analysis", - analysis: worker, - resource: `JavaScript source analyzer for ${path}`, - }); - if (cleanup.kind === "failed") throw cleanup.cause; - } catch (cause: unknown) { - if (!(cause instanceof AnalysisError)) throw cause; - cleanupFailure = cause; - } + cleanupAttempt = await scope.release({ + kind: "javascript-source-analysis", + analysis: worker, + resource: `JavaScript source analyzer for ${path}`, + }); + } + if (analysisFailure !== undefined) { + if (cleanupAttempt.kind === "failed") + throw scope.cleanupFailure( + { kind: "failed", cause: analysisFailure.cause }, + [cleanupAttempt], + ); + throw analysisFailure.cause; + } + let cleanupFailure: AnalysisError | undefined; + if (cleanupAttempt.kind === "failed") { + if (!(cleanupAttempt.cause instanceof AnalysisError)) + throw cleanupAttempt.cause; + cleanupFailure = cleanupAttempt.cause; } const analysis = outcome.analysis; let interruption = outcome.interruption; diff --git a/src/application/javascript/JavaScriptNextAppContext.ts b/src/application/javascript/JavaScriptNextAppContext.ts index 70c7528361..7630dc3772 100644 --- a/src/application/javascript/JavaScriptNextAppContext.ts +++ b/src/application/javascript/JavaScriptNextAppContext.ts @@ -9,6 +9,10 @@ import type { NextAppSourceContext } from "../../domain/javascript/javascriptRou import { compositeKey } from "../../domain/unicodeCodePointOrder.js"; import type { JavaScriptJsonDocuments } from "./JavaScriptJsonDocuments.js"; +/** Identify a package directory within its owning container. */ +const packageDirectoryKey = (container: string, directory: string): string => + compositeKey([container, directory]); + /** Package declarations and inventoried directories own source-router applicability. */ export const createJavaScriptNextAppContext = ( fileSet: Pick< @@ -25,7 +29,7 @@ export const createJavaScriptNextAppContext = ( fileSet.files .filter(({ kind }) => kind === "package-json") .map((file) => [ - compositeKey([file.container_sha256, posix.dirname(file.path)]), + packageDirectoryKey(file.container_sha256, posix.dirname(file.path)), file, ]), ); @@ -33,7 +37,7 @@ export const createJavaScriptNextAppContext = ( fileSet.unavailable_relevant_files .filter(({ kind }) => kind === "package-json") .map((file) => [ - compositeKey([file.container_sha256, posix.dirname(file.path)]), + packageDirectoryKey(file.container_sha256, posix.dirname(file.path)), file, ]), ); @@ -44,7 +48,7 @@ export const createJavaScriptNextAppContext = ( ; directory = posix.dirname(directory) ) { - const key = compositeKey([file.container_sha256, directory]); + const key = packageDirectoryKey(file.container_sha256, directory); const unavailable = unavailablePackages.get(key); if (unavailable !== undefined) return { diff --git a/src/application/javascript/JavaScriptRuntimeObservationService.test.ts b/src/application/javascript/JavaScriptRuntimeObservationService.test.ts index 6e92841437..6227f907da 100644 --- a/src/application/javascript/JavaScriptRuntimeObservationService.test.ts +++ b/src/application/javascript/JavaScriptRuntimeObservationService.test.ts @@ -1,10 +1,7 @@ import { expect, it } from "vitest"; import { BrowserObservationError } from "../../domain/browserObservationError.js"; -import { - AnalysisCancelledError, - AnalysisOutputError, -} from "../../domain/analysisErrorCore.js"; +import { AnalysisCancelledError } from "../../domain/analysisErrorCore.js"; import { err, ok } from "../../domain/result.js"; import { parseEvidence } from "../../domain/evidence.js"; import type { JavaScriptRuntimeTargetList } from "../../domain/javascript/javascriptRuntimeObservation.js"; diff --git a/src/application/javascript/JavaScriptSemanticGraphBuilder.test.ts b/src/application/javascript/JavaScriptSemanticGraphBuilder.test.ts index 1ff7f2932e..2dda35da5d 100644 --- a/src/application/javascript/JavaScriptSemanticGraphBuilder.test.ts +++ b/src/application/javascript/JavaScriptSemanticGraphBuilder.test.ts @@ -1,3 +1,4 @@ +import { JAVASCRIPT_ANALYSIS_RESOURCE_DEFAULTS } from "../../domain/javascript/javascriptAnalysisResourceControls.js"; import { javaScriptSemanticQueryInputSchema } from "../../domain/javascript/javascriptSemanticQuerySchemas.js"; import { primitiveByteExpansionSource, @@ -9,8 +10,6 @@ import { expect, it } from "vitest"; import { aliasSelectionBudgetSource } from "../../../tests/fixtures/javascriptAliasSelectionBudget.js"; import { - SEMANTIC_GRAPH_FILE_NODE_CEILING, - SEMANTIC_GRAPH_NODE_CEILING, createJavaScriptSemanticGraphProjection, projectJavaScriptSemanticFileSteps, } from "./JavaScriptSemanticGraphBuilder.js"; @@ -972,7 +971,9 @@ it("bounds semantic node projection and reports the ceiling in coverage", () => ).join("\n"); const graph = graphFor(statements); - expect(graph.nodes.length).toBeLessThanOrEqual(SEMANTIC_GRAPH_NODE_CEILING); + expect(graph.nodes.length).toBeLessThanOrEqual( + JAVASCRIPT_ANALYSIS_RESOURCE_DEFAULTS.semanticNodes, + ); expect(graph.coverage.truncated).toBe(true); expect(graph.coverage.status).toBe("partial"); expect(graph.coverage.omitted_nodes).toBeNull(); @@ -1136,9 +1137,13 @@ it("leaves a small application complete and untruncated", () => { it("does not mark a file truncated when it exactly fills its budget", () => { // Each unbound reference projects one expression, plus the module root. const graph = graphFor( - "external;\n".repeat(SEMANTIC_GRAPH_FILE_NODE_CEILING - 1), + "external;\n".repeat( + JAVASCRIPT_ANALYSIS_RESOURCE_DEFAULTS.semanticNodesPerFile - 1, + ), + ); + expect(graph.nodes).toHaveLength( + JAVASCRIPT_ANALYSIS_RESOURCE_DEFAULTS.semanticNodesPerFile, ); - expect(graph.nodes).toHaveLength(SEMANTIC_GRAPH_FILE_NODE_CEILING); expect(graph.coverage).toMatchObject({ truncated: false, omitted_nodes: 0, @@ -1160,7 +1165,9 @@ it("only records node loss when the budget rejects a new identity", () => { expect(state.fileNodeBudget).toBe(0); expect(state.fileNodesDropped).toBe(false); - expect(addSemanticGraphNode(state, semanticNodeFor("first"))).toBe(first); + expect(addSemanticGraphNode(state, semanticNodeFor("first"))).toEqual(first); + expect(state.nodes.size).toBe(2); + expect(state.fileNodeBudget).toBe(0); expect(state.fileNodesDropped).toBe(false); const blocked = addSemanticGraphNode(state, semanticNodeFor("third")); diff --git a/src/application/javascript/JavaScriptSemanticGraphBuilder.ts b/src/application/javascript/JavaScriptSemanticGraphBuilder.ts index c1e4ade32c..37880b30c6 100644 --- a/src/application/javascript/JavaScriptSemanticGraphBuilder.ts +++ b/src/application/javascript/JavaScriptSemanticGraphBuilder.ts @@ -111,36 +111,6 @@ interface FileContext extends SemanticFlowProjectionContext { >; } -/** - * Resource-safety ceiling on retained semantic nodes. - * - * The semantic projection emits one node per AST expression, call site, binding - * and property slot. That is unbounded in input size. A small tree of five files - * already yields on the order of 170,000 nodes, and larger trees yield millions, - * exhausting the heap before the result can be serialized. - * - * This is a real resource-safety constraint, not a presentation preference, so - * the ceiling is applied and reported through the graph's own `coverage` fields - * (`truncated`, `omitted_nodes`, `omitted_relations`, `limits`) rather than - * silently dropping data. - * - * Defaults bound work even when ample heap is available. Callers may raise the - * capacities explicitly; isolated worker heap, bounded record transport and - * parent result admission independently constrain actual retained memory. The - * result uses incremental serialization rather than a single JSON string. - */ -export const SEMANTIC_GRAPH_NODE_CEILING = - JAVASCRIPT_ANALYSIS_RESOURCE_DEFAULTS.semanticNodes; - -/** - * Per-file share of the semantic node budget. - * - * Bound each source file independently so one large bundled library cannot - * consume the whole tree's budget and starve the application's own modules. - */ -export const SEMANTIC_GRAPH_FILE_NODE_CEILING = - JAVASCRIPT_ANALYSIS_RESOURCE_DEFAULTS.semanticNodesPerFile; - /** File-local semantic projection that does not retain consumed source IR. */ export interface JavaScriptSemanticGraphProjection { /** Node capacity offered to the next independently analyzed source file. */ @@ -216,8 +186,12 @@ export const createJavaScriptSemanticGraphProjection = ( readonly fileUnknowns?: number; } = {}, ): JavaScriptSemanticGraphProjection => { - const nodeCeiling = limits.totalNodes ?? SEMANTIC_GRAPH_NODE_CEILING; - const fileNodeCeiling = limits.fileNodes ?? SEMANTIC_GRAPH_FILE_NODE_CEILING; + // Bound total and per-file work so a large dependency cannot starve the graph. + const nodeCeiling = + limits.totalNodes ?? JAVASCRIPT_ANALYSIS_RESOURCE_DEFAULTS.semanticNodes; + const fileNodeCeiling = + limits.fileNodes ?? + JAVASCRIPT_ANALYSIS_RESOURCE_DEFAULTS.semanticNodesPerFile; const unknownCeiling = limits.totalUnknowns ?? JAVASCRIPT_ANALYSIS_RESOURCE_DEFAULTS.semanticUnknowns; @@ -276,7 +250,8 @@ export const createJavaScriptSemanticGraphProjection = ( unknownCeiling, unknownsBefore + fileUnknownCeiling, ); - fingerprints.push(...(yield* projectFileSteps(file, ir, state, paths))); + for (const fingerprint of yield* projectFileSteps(file, ir, state, paths)) + fingerprints.push(fingerprint); // Nodes were dropped only if the budget actually blocked creation. A file // that exactly fills its share ends with a zero budget without dropping // anything, so the remaining budget alone cannot decide truncation. @@ -453,7 +428,8 @@ export const createJavaScriptSemanticGraphProjection = ( for (const unknown of newUnknowns) state.unknowns.set(unknown.unknown_id, unknown); for (const root of projection.roots) state.roots.add(root); - fingerprints.push(...projection.fingerprints); + for (const fingerprint of projection.fingerprints) + fingerprints.push(fingerprint); if (projection.nodesTruncated) { truncatedFiles += 1; noteSemanticCeilingReached( diff --git a/src/application/managed/ManagedMemberComparisonService.ts b/src/application/managed/ManagedMemberComparisonService.ts index c337097c20..48c7651332 100644 --- a/src/application/managed/ManagedMemberComparisonService.ts +++ b/src/application/managed/ManagedMemberComparisonService.ts @@ -138,9 +138,9 @@ export const compareManagedMemberPaths = async ( operation, ).bindCleanupDependencies([leftTarget.error, rightTarget.error]), ); - return err(leftTarget.error); + return leftTarget; } - if (!rightTarget.ok) return err(rightTarget.error); + if (!rightTarget.ok) return rightTarget; const inspectTarget = async ( target: BinaryTarget, inputField: "leftPath" | "rightPath", diff --git a/src/application/native/NativeDispatchMetadataInspection.ts b/src/application/native/NativeDispatchMetadataInspection.ts index 42a4cb45d0..b7f38cad61 100644 --- a/src/application/native/NativeDispatchMetadataInspection.ts +++ b/src/application/native/NativeDispatchMetadataInspection.ts @@ -83,7 +83,7 @@ export const inspectNativeDispatch = async ( {}, signal === undefined ? {} : { signal }, ); - if (!execution.ok) return err(execution.error); + if (!execution.ok) return execution; const names = dispatchNameInventorySchema.safeParse(execution.value.result); if (!names.success) return err( diff --git a/src/application/native/NativeUiActionTrace.ts b/src/application/native/NativeUiActionTrace.ts index 0113c26aaa..905686c0b8 100644 --- a/src/application/native/NativeUiActionTrace.ts +++ b/src/application/native/NativeUiActionTrace.ts @@ -77,8 +77,8 @@ export const traceNativeUiAction = async ( (uiExecution.error._tag === "AnalysisCancelledError" || uiExecution.error.cleanupIncomplete) ) - return err(uiExecution.error); - if (!metadataExecution.ok) return err(metadataExecution.error); + return uiExecution; + if (!metadataExecution.ok) return metadataExecution; const metadata = nativeDispatchMetadataResultSchema.parse( metadataExecution.value, ); diff --git a/src/application/ntfs/NtfsJournalPaging.test.ts b/src/application/ntfs/NtfsJournalPaging.test.ts index 6981e6dda0..e377937f30 100644 --- a/src/application/ntfs/NtfsJournalPaging.test.ts +++ b/src/application/ntfs/NtfsJournalPaging.test.ts @@ -16,6 +16,7 @@ import { parseEvidence } from "../../domain/evidence.js"; import { ntfsJournalResultSchema } from "../../domain/windows/ntfsJournalQuery.js"; import { inspectNtfsJournalEvidence } from "./NtfsJournalService.js"; import { createDeferred } from "../../../tests/fixtures/binarySession.js"; +import { awaitAcknowledgement } from "../../../tests/fixtures/lifecycleAcknowledgement.js"; import { createTestTempDirectory } from "../../../tests/fixtures/temporaryDirectory.js"; const acquisitions = async () => { @@ -170,7 +171,11 @@ it("keeps an active acquisition independent of a cancelled peer, bounds concurre }); try { const active = inspectNtfsJournalEvidence(fixture.input, resources); - await entered.promise; + await awaitAcknowledgement( + entered.promise, + active, + "provider lifecycle boundary", + ); const cancelled = new AbortController(); cancelled.abort("peer cancelled"); const peer = await inspectNtfsJournalEvidence( diff --git a/src/application/ownedProcessAvailability.test.ts b/src/application/ownedProcessAvailability.test.ts index 13a97d8dfb..4ead728557 100644 --- a/src/application/ownedProcessAvailability.test.ts +++ b/src/application/ownedProcessAvailability.test.ts @@ -1,4 +1,5 @@ -import { describe, expect, it } from "vitest"; +import { describe, expect, it, onTestFinished, vi } from "vitest"; +import * as processOutput from "../process/ExecFileOutput.js"; import { AnalysisCancelledError, @@ -20,15 +21,11 @@ describe("Linux process table prerequisite discovery", () => { "reports %s with its own recovery", async (code) => { const cause = Object.assign(new Error(`ps failed: ${code}`), { code }); - const host = createSystemProcessOwnershipHost( - "linux", - {}, - { - execFileOutput: async () => { - throw cause; - }, - }, - ); + const command = vi + .spyOn(processOutput, "execFileOutput") + .mockRejectedValue(cause); + onTestFinished(() => command.mockRestore()); + const host = createSystemProcessOwnershipHost("linux", {}); let failure: unknown; try { await host.prepare?.(); @@ -58,15 +55,11 @@ describe("Linux process table prerequisite discovery", () => { it("reports a ps probe failure with no error code without blaming missing procps", async () => { const cause = new Error("ps failed without error code"); - const host = createSystemProcessOwnershipHost( - "linux", - {}, - { - execFileOutput: async () => { - throw cause; - }, - }, - ); + const command = vi + .spyOn(processOutput, "execFileOutput") + .mockRejectedValue(cause); + onTestFinished(() => command.mockRestore()); + const host = createSystemProcessOwnershipHost("linux", {}); let failure: unknown; try { await host.prepare?.(); @@ -103,15 +96,11 @@ describe("Linux process table prerequisite discovery", () => { it("does not convert host spawn capacity exhaustion to unavailability", async () => { const cause = Object.assign(new Error("capacity"), { code: "EAGAIN" }); - const host = createSystemProcessOwnershipHost( - "linux", - {}, - { - execFileOutput: async () => { - throw cause; - }, - }, - ); + const command = vi + .spyOn(processOutput, "execFileOutput") + .mockRejectedValue(cause); + onTestFinished(() => command.mockRestore()); + const host = createSystemProcessOwnershipHost("linux", {}); await expect(host.prepare?.()).rejects.toBe(cause); }); }); diff --git a/src/application/process/ProcessCli.ts b/src/application/process/ProcessCli.ts index a50b470b1d..da4a0e7775 100644 --- a/src/application/process/ProcessCli.ts +++ b/src/application/process/ProcessCli.ts @@ -152,7 +152,7 @@ export const captureProcessScenarioFile = async ( ); captureOutcome = captured.ok ? ok(createProcessCaptureEvidence(parsed.data, captured.value)) - : err(captured.error); + : captured; } catch (cause: unknown) { captureOutcome = err(cause); } diff --git a/src/application/process/ProcessScenarioPreparation.test.ts b/src/application/process/ProcessScenarioPreparation.test.ts index 05d1ec3740..9f850c8a63 100644 --- a/src/application/process/ProcessScenarioPreparation.test.ts +++ b/src/application/process/ProcessScenarioPreparation.test.ts @@ -13,29 +13,40 @@ import { prepareProcessScenarioPlan } from "./ProcessScenarioPreparation.js"; afterEach(() => vi.restoreAllMocks()); -it("rejects observation roots that capture admission rejects", async () => { - const root = await createTestTempDirectory("rea-process-plan-"); - const file = join(root, "selected"); - await writeFile(file, "not a directory"); - const scenario = processScenarioSchema.parse({ - executable: process.execPath, - working_directory: root, - filesystem_observation_paths: [join(file, "child")], - }); +/** + * Windows refuses capture before root admission runs. The cancellation cases + * replace admission, so they plan for a platform that reaches it on any host. + */ +const capturePlatform = "linux"; - const prepared = await prepareProcessScenarioPlan( - scenario, - {}, - process.platform, - ); +// A path below a regular file is ENOTDIR on POSIX. Windows reports ENOENT, +// which admission accepts as a root the scenario may create. +it.skipIf(process.platform === "win32")( + "rejects observation roots that capture admission rejects", + async () => { + const root = await createTestTempDirectory("rea-process-plan-"); + const file = join(root, "selected"); + await writeFile(file, "not a directory"); + const scenario = processScenarioSchema.parse({ + executable: process.execPath, + working_directory: root, + filesystem_observation_paths: [join(file, "child")], + }); - expect(prepared.ok).toBe(false); - if (prepared.ok) return; - expect(prepared.error).toMatchObject({ - _tag: "ProcessCaptureError", - userMessage: "filesystem observation admission failed", - }); -}); + const prepared = await prepareProcessScenarioPlan( + scenario, + {}, + process.platform, + ); + + expect(prepared.ok).toBe(false); + if (prepared.ok) return; + expect(prepared.error).toMatchObject({ + _tag: "ProcessCaptureError", + userMessage: "filesystem observation admission failed", + }); + }, +); it("reports cancellation normalized inside admission as planning cancellation", async () => { const scenario = processScenarioSchema.parse({ @@ -59,7 +70,7 @@ it("reports cancellation normalized inside admission as planning cancellation", const prepared = await prepareProcessScenarioPlan( scenario, {}, - process.platform, + capturePlatform, controller.signal, ); @@ -100,7 +111,7 @@ it("reports cancellation after the last awaited admission step", async () => { const planning = prepareProcessScenarioPlan( scenario, {}, - process.platform, + capturePlatform, controller.signal, ); diff --git a/src/application/process/ProcessScenarioPreparation.ts b/src/application/process/ProcessScenarioPreparation.ts index d2bdb6f9ca..4010896982 100644 --- a/src/application/process/ProcessScenarioPreparation.ts +++ b/src/application/process/ProcessScenarioPreparation.ts @@ -53,7 +53,7 @@ export const prepareProcessScenarioPlan = async ( await resolveProcessScenarioRuntimePaths(scenario, environment, platform); if (aborted()) return cancelled(); const privacy = createProcessSensitiveOutput(scenario); - if (!privacy.ok) return err(privacy.error); + if (!privacy.ok) return privacy; const admitted = await admitFilesystemObservationRoots(resolved, signal); if (!admitted.ok) return aborted() ? cancelled() : admitted; if (aborted()) return cancelled(); diff --git a/src/application/releaseExposure/ReleaseExposureService.test.ts b/src/application/releaseExposure/ReleaseExposureService.test.ts index ac1bbba2dc..4a7b073e1b 100644 --- a/src/application/releaseExposure/ReleaseExposureService.test.ts +++ b/src/application/releaseExposure/ReleaseExposureService.test.ts @@ -19,7 +19,6 @@ import { createEvidence, type Evidence } from "../../domain/evidence.js"; import { RELEASE_EXPOSURE_PROVIDER } from "../../domain/investigationProviders.js"; import { PACKAGED_NATIVE_IMAGES, - RELEASE_EXPOSURE_CHECK_IDS, releaseExposureReportSchema, type ReleaseExposureScan, } from "../../domain/releaseExposure.js"; @@ -189,9 +188,6 @@ describe("ReleaseExposureService", () => { observation: { samples: string[] }; }[]; }; - expect(report.checks.map((check) => check.id)).toEqual([ - ...RELEASE_EXPOSURE_CHECK_IDS, - ]); expect( report.checks.find((check) => check.id === "go.build_paths")?.status, ).toBe("not_applicable"); diff --git a/src/application/reverse/ReverseEngineeringCommandPort.ts b/src/application/reverse/ReverseEngineeringCommandPort.ts index 7228526c86..d5680bf58a 100644 --- a/src/application/reverse/ReverseEngineeringCommandPort.ts +++ b/src/application/reverse/ReverseEngineeringCommandPort.ts @@ -11,6 +11,11 @@ export interface ReverseEngineeringProviderCommand { readonly identity: EvidenceProvider; /** What the provider cannot attest about the arguments it built. */ readonly limitations?: readonly string[]; + /** + * Explain a provider refusal of the command that a completed process + * reported only in its diagnostics, or return undefined. + */ + readonly rejection?: (stderr: string) => string | undefined; readonly environmentOverrides?: Readonly; } diff --git a/src/application/reverse/ReverseEngineeringService.ts b/src/application/reverse/ReverseEngineeringService.ts index 8f3c0efdd9..c3aa241a19 100644 --- a/src/application/reverse/ReverseEngineeringService.ts +++ b/src/application/reverse/ReverseEngineeringService.ts @@ -599,7 +599,7 @@ export class ReverseEngineeringService { let stderrInvalidUtf8 = false; let cleanupIncomplete = false; let cleanupOnlyFailure = false; - let completionStatus: "complete" | "unknown" = "complete"; + let completionStatus: "complete" | "rejected" | "unknown" = "complete"; let outputLimitReached = false; let failure: AnalysisError | undefined; let cleanupOwner: ProviderProcessSupervisor | undefined; @@ -1076,6 +1076,15 @@ export class ReverseEngineeringService { : digestAfter !== digest; const outputBudgetSource = parameters.max_output_bytes === undefined ? "default" : "request"; + // Only a completed process's diagnostics can establish a refusal; partial + // or failed captures keep their unknown completion. + const rejection = + completionStatus === "complete" && + (failure === undefined || + (failure instanceof AnalysisOutputError && failure.cleanupOnly)) + ? profile.rejection?.(stderr) + : undefined; + if (rejection !== undefined) completionStatus = "rejected"; const result = { stdout, stderr, @@ -1116,6 +1125,7 @@ export class ReverseEngineeringService { ] : []), ...(profile.limitations ?? []), + ...(rejection === undefined ? [] : [rejection]), ...(stdoutInvalidUtf8 || stderrInvalidUtf8 ? [ `Captured ${[ diff --git a/src/artifacts/ArtifactFileSource.test.ts b/src/artifacts/ArtifactFileSource.test.ts index 2850354ea9..d472c5b32e 100644 --- a/src/artifacts/ArtifactFileSource.test.ts +++ b/src/artifacts/ArtifactFileSource.test.ts @@ -2,6 +2,7 @@ import { open, writeFile, type FileHandle } from "node:fs/promises"; import { join } from "node:path"; import { expect, it, vi } from "vitest"; import { createTestTempDirectory } from "../../tests/fixtures/temporaryDirectory.js"; +import { awaitAcknowledgement } from "../../tests/fixtures/lifecycleAcknowledgement.js"; import { ArtifactFileSource } from "./ArtifactFileSource.js"; it("retries an admission cancelled after opening and closing the selected descriptor", async () => { @@ -132,7 +133,7 @@ it("lets another caller continue when shared admission is cancelled", async () = }, (cause: unknown) => cause, ); - await started; + await awaitAcknowledgement(started, first, "artifact source admission"); const second = source.read(); await Promise.resolve(); controller.abort(reason); diff --git a/src/artifacts/ArtifactOperationFailure.test.ts b/src/artifacts/ArtifactOperationFailure.test.ts index d05af1481c..0834c54523 100644 --- a/src/artifacts/ArtifactOperationFailure.test.ts +++ b/src/artifacts/ArtifactOperationFailure.test.ts @@ -5,6 +5,17 @@ import { translateArtifactFailure } from "./ArtifactOperationFailure.js"; import { projectAnalysisError } from "../domain/analysisErrorProjection.js"; import { artifactReaderLimitFailure } from "./ArtifactReaderLimits.js"; import { constants as bufferConstants } from "node:buffer"; +import { imageFileSizeRemediation } from "./StableArtifactFailure.js"; + +// The provider ceiling test uses a sparse file and is skipped on Windows. +it.skipIf(process.platform !== "win32")( + "names the fixed image ceiling when the selected budget is already maximal", + () => { + expect(imageFileSizeRemediation(1024, 1024)).toContain( + "no runtime override", + ); + }, +); it.each([ { effective: 1, required: bufferConstants.MAX_STRING_LENGTH + 1 }, @@ -94,6 +105,18 @@ it.each(["capacity", "file-size", "memory"] as const)( message: failure.message, details: { boundary, detail: failure.message }, }); + const wrapped = ArtifactReaderFailure.withCleanup(failure, { + reason: "close failed", + resources: ["handle"], + }); + expect( + projectAnalysisError( + translateArtifactFailure("inventory_artifact", wrapped), + ), + ).toMatchObject({ + code: "cleanup_incomplete", + details: { boundary, cleanup: "incomplete", resources: ["handle"] }, + }); }, ); diff --git a/src/artifacts/ArtifactResourceScope.test.ts b/src/artifacts/ArtifactResourceScope.test.ts index 821c91cafb..5f6678c2b6 100644 --- a/src/artifacts/ArtifactResourceScope.test.ts +++ b/src/artifacts/ArtifactResourceScope.test.ts @@ -1,6 +1,7 @@ import { open, writeFile } from "node:fs/promises"; import { join } from "node:path"; import { createTestTempDirectory } from "../../tests/fixtures/temporaryDirectory.js"; +import { awaitAcknowledgement } from "../../tests/fixtures/lifecycleAcknowledgement.js"; import { withArtifactFileHandle, bindArtifactFileCleanup, @@ -36,25 +37,33 @@ it("waits for admitted operations and shares concurrent close", async () => { started.resolve(); await finish.promise; }); - await started.promise; + try { + await awaitAcknowledgement(started.promise, operation, "scope admission"); + const close = scope.close(); + expect(scope.close()).toBe(close); + await expect(scope.run(async () => undefined)).rejects.toMatchObject({ + reason: "unavailable", + message: expect.stringContaining("closed"), + }); + let closed = false; + void close.then(() => { + closed = true; + }); + await Promise.resolve(); + expect(closed).toBe(false); - const close = scope.close(); - expect(scope.close()).toBe(close); - await expect(scope.run(async () => undefined)).rejects.toMatchObject({ - reason: "unavailable", - message: expect.stringContaining("closed"), - }); - let closed = false; - void close.then(() => { - closed = true; - }); - await Promise.resolve(); - expect(closed).toBe(false); - - finish.resolve(); - await operation; - await close; - expect(closed).toBe(true); + finish.resolve(); + await operation; + await close; + expect(closed).toBe(true); + } finally { + finish.resolve(); + try { + await operation; + } finally { + await scope.close(); + } + } }); it("stops a close hook even when retained cleanup is still failing", async () => { diff --git a/src/artifacts/AsarArtifactReader.ts b/src/artifacts/AsarArtifactReader.ts index 4dd8bf328c..fed845c12e 100644 --- a/src/artifacts/AsarArtifactReader.ts +++ b/src/artifacts/AsarArtifactReader.ts @@ -57,6 +57,7 @@ export class AsarArtifactReader implements ArtifactReader { #snapshot: { readonly path: string; readonly sha256: string } | undefined; #snapshotRoot: string | undefined; #snapshotOperation: Promise = Promise.resolve(); + #snapshotGeneration = 0; constructor( private readonly path: string, @@ -64,6 +65,7 @@ export class AsarArtifactReader implements ArtifactReader { ) {} async *entries(signal?: AbortSignal): AsyncIterable { + const generation = this.#snapshotGeneration; let paths: readonly AsarListedEntry[]; try { await this.prepareContainer(undefined, signal); @@ -102,7 +104,7 @@ export class AsarArtifactReader implements ArtifactReader { throw asarFailure(this.path, "inventory", cause); } for (const facts of paths) { - assertAsarReadActive(signal); + this.#assertInventoryActive(generation, signal); const { path, kind } = facts; const providerPath = path.split("/").join(sep); const entry: ArtifactEntry = { @@ -130,6 +132,16 @@ export class AsarArtifactReader implements ArtifactReader { }); yield entry; } + this.#assertInventoryActive(generation, signal); + } + + #assertInventoryActive(generation: number, signal?: AbortSignal): void { + assertAsarReadActive(signal); + if (generation !== this.#snapshotGeneration) + throw new ArtifactReaderFailure( + "unavailable", + "ASAR inventory snapshot was closed", + ); } async #admitHeader(snapshotPath: string, signal?: AbortSignal) { @@ -341,6 +353,7 @@ export class AsarArtifactReader implements ArtifactReader { } async close(): Promise { + this.#snapshotGeneration += 1; await this.#withSnapshotOperation(() => this.#closeSnapshot()); } diff --git a/src/artifacts/AsarHeaderIdentity.test.ts b/src/artifacts/AsarHeaderIdentity.test.ts index 3ef42c1fe6..9f28ea159f 100644 --- a/src/artifacts/AsarHeaderIdentity.test.ts +++ b/src/artifacts/AsarHeaderIdentity.test.ts @@ -7,6 +7,32 @@ import { expect, it } from "vitest"; import { AsarArtifactReader } from "./AsarArtifactReader.js"; import { asarHeaderBytes } from "../../tests/fixtures/asarHeader.js"; +it.each(["close", "cancel"] as const)( + "does not complete an ASAR inventory after %s while paused", + async (interruption) => { + const root = await mkdtemp(join(tmpdir(), "rea-asar-paused-")); + const path = join(root, "app.asar"); + await writeFile( + path, + asarHeaderBytes('{"files":{"x":{"size":0,"offset":"0"}}}'), + ); + const reader = new AsarArtifactReader(path); + const controller = new AbortController(); + try { + const entries = reader.entries(controller.signal)[Symbol.asyncIterator](); + expect((await entries.next()).done).toBe(false); + if (interruption === "close") await reader.close(); + else controller.abort(); + await expect(entries.next()).rejects.toMatchObject({ + reason: interruption === "close" ? "unavailable" : "cancelled", + }); + } finally { + await reader.close(); + await rm(root, { recursive: true, force: true }); + } + }, +); + it("rejects malformed UTF-8 instead of inventing a replacement-character member identity", async () => { const root = await mkdtemp(join(tmpdir(), "rea-asar-identity-")); const path = join(root, "app.asar"); diff --git a/src/artifacts/BunStandaloneArtifactReader.ts b/src/artifacts/BunStandaloneArtifactReader.ts index 9754854ee9..776c856f4c 100644 --- a/src/artifacts/BunStandaloneArtifactReader.ts +++ b/src/artifacts/BunStandaloneArtifactReader.ts @@ -7,8 +7,7 @@ import { artifactReaderLimitFailure } from "./ArtifactReaderLimits.js"; import type { BunStandaloneAdmission } from "./bun/BunStandaloneAdmission.js"; import type { StableRegularFileDescriptor } from "../filesystem/RegularFile.js"; import { setImmediate } from "node:timers/promises"; -import { Readable, Transform, pipeline } from "node:stream"; -import { finished } from "node:stream/promises"; +import { Readable, Transform } from "node:stream"; import { artifactExpansionBudgetBytes } from "../domain/artifactExpansionBudget.js"; import type { BunStandaloneEntryMetadata, @@ -32,6 +31,7 @@ import { } from "./ArtifactReader.js"; import { ArtifactFileSource } from "./ArtifactFileSource.js"; import { ArtifactResourceScope } from "./ArtifactResourceScope.js"; +import { ownArtifactStream } from "./OwnedArtifactStream.js"; import { bunFormatFailure, bunMetadataBytesLimit, @@ -108,12 +108,14 @@ export class BunStandaloneArtifactReader implements ArtifactReader { ); await this.#source.readSelectedPath(signal); }); + this.#assertOpen(signal); let index = 0; for (const entry of this.#entries.values()) { if (index++ % 256 === 0) await setImmediate(); this.#assertOpen(signal); yield copyArtifactEntry(entry); } + this.#assertOpen(signal); } async open(entry: ArtifactEntry, signal?: AbortSignal): Promise { @@ -129,6 +131,14 @@ export class BunStandaloneArtifactReader implements ArtifactReader { this.#assertOpen(signal); if (stored.byteOffset === null || stored.declaredSize === null) throw bunFormatFailure("member has no physical byte range"); + const cancellationFailure = (reason: unknown): ArtifactReaderFailure => + new ArtifactReaderFailure( + "cancelled", + "Bun standalone read cancelled", + { + cause: reason, + }, + ); const source = stored.declaredSize === 0 ? Readable.from([]) @@ -136,33 +146,16 @@ export class BunStandaloneArtifactReader implements ArtifactReader { start: stored.byteOffset, end: stored.byteOffset + stored.declaredSize - 1, signal, - cancellationFailure: (reason) => - new ArtifactReaderFailure( - "cancelled", - "Bun standalone read cancelled", - { cause: reason }, - ), + cancellationFailure, }); const output = memberStream(stored); - output.on("error", () => undefined); - this.#streams.add(output); - output.once("close", () => this.#streams.delete(output)); - void this.#scope - .run( - () => - new Promise((resolve) => { - pipeline(source, output, () => { - resolve(); - }); - }), - ) - .catch((cause: unknown) => - output.destroy( - cause instanceof Error - ? cause - : new Error("Bun standalone stream ownership failed", { cause }), - ), - ); + ownArtifactStream(source, output, { + scope: this.#scope, + streams: this.#streams, + signal, + cancellationFailure, + label: "Bun standalone", + }); return output; }); } @@ -177,11 +170,6 @@ export class BunStandaloneArtifactReader implements ArtifactReader { ), ); await this.#scope.close(); - await Promise.all( - [...this.#streams].map((stream) => - finished(stream).catch(() => undefined), - ), - ); await this.#source.close(); this.#entries.clear(); this.#header = undefined; diff --git a/src/artifacts/DecodedRegionArtifactReader.ts b/src/artifacts/DecodedRegionArtifactReader.ts index fd600cab19..f3406908c5 100644 --- a/src/artifacts/DecodedRegionArtifactReader.ts +++ b/src/artifacts/DecodedRegionArtifactReader.ts @@ -118,6 +118,7 @@ export class DecodedRegionArtifactReader implements ArtifactReader { ); await this.#source.read(signal); }); + this.#assertOpen(signal); const coverage = this.#described?.coverage; if (coverage !== undefined) options?.onCoverage?.({ @@ -131,6 +132,7 @@ export class DecodedRegionArtifactReader implements ArtifactReader { this.#assertOpen(signal); yield copyArtifactEntry(entry); } + this.#assertOpen(signal); } async open(entry: ArtifactEntry, signal?: AbortSignal): Promise { diff --git a/src/artifacts/DenoStandaloneArtifactReader.ts b/src/artifacts/DenoStandaloneArtifactReader.ts index 2e40a76abe..8f7a79b5b5 100644 --- a/src/artifacts/DenoStandaloneArtifactReader.ts +++ b/src/artifacts/DenoStandaloneArtifactReader.ts @@ -1,5 +1,5 @@ import { ArtifactPreparation } from "./ArtifactPreparation.js"; -import { Readable, Transform, pipeline } from "node:stream"; +import { Readable, Transform } from "node:stream"; import { setImmediate } from "node:timers/promises"; import type { StableRegularFileDescriptor } from "../filesystem/RegularFile.js"; @@ -27,6 +27,7 @@ import { type ArtifactTraversalOptions, } from "./ArtifactReader.js"; import { ArtifactResourceScope } from "./ArtifactResourceScope.js"; +import { ownArtifactStream } from "./OwnedArtifactStream.js"; import type { DenoStandaloneAdmission } from "./deno/DenoStandaloneAdmission.js"; import { denoFormatFailure, @@ -146,6 +147,10 @@ export class DenoStandaloneArtifactReader implements ArtifactReader { this.#assertOpen(signal); if (stored.byteOffset === null || stored.declaredSize === null) throw denoFormatFailure("member has no physical byte range"); + const cancellationFailure = (reason: unknown): ArtifactReaderFailure => + new ArtifactReaderFailure("cancelled", "Deno compile read cancelled", { + cause: reason, + }); const source = stored.declaredSize === 0 ? Readable.from([]) @@ -153,64 +158,16 @@ export class DenoStandaloneArtifactReader implements ArtifactReader { start: stored.byteOffset, end: stored.byteOffset + stored.declaredSize - 1, signal, - cancellationFailure: (reason) => - new ArtifactReaderFailure( - "cancelled", - "Deno compile read cancelled", - { - cause: reason, - }, - ), + cancellationFailure, }); const output = this.#memberStream(stored, signal); - output.on("error", () => undefined); - this.#streams.add(output); - let producerComplete = false; - const onAbort = (): void => { - // Active I/O and final validation own failure precedence. Buffered - // readable bytes remain owned after production finishes. - if (!producerComplete || output.destroyed) return; - output.destroy( - new ArtifactReaderFailure( - "cancelled", - "Deno compile read cancelled", - { - cause: signal?.reason, - }, - ), - ); - }; - const retired = new Promise((resolve) => { - output.once("close", () => { - this.#streams.delete(output); - signal?.removeEventListener("abort", onAbort); - resolve(); - }); + ownArtifactStream(source, output, { + scope: this.#scope, + streams: this.#streams, + signal, + cancellationFailure, + label: "Deno compile", }); - signal?.addEventListener("abort", onAbort, { once: true }); - if (signal?.aborted) onAbort(); - void this.#scope - .run(async () => { - // Writable completion can leave unread bytes buffered. Join both - // producer work and retirement before releasing the carrier. - await Promise.all([ - new Promise((resolve) => { - pipeline(source, output, () => { - producerComplete = true; - if (signal?.aborted) onAbort(); - resolve(); - }); - }), - retired, - ]); - }) - .catch((cause: unknown) => - output.destroy( - cause instanceof Error - ? cause - : new Error("Deno compile stream ownership failed", { cause }), - ), - ); return output; }); } diff --git a/src/artifacts/DirectoryArtifactReader.ts b/src/artifacts/DirectoryArtifactReader.ts index ac7af3c347..00e14ce33d 100644 --- a/src/artifacts/DirectoryArtifactReader.ts +++ b/src/artifacts/DirectoryArtifactReader.ts @@ -169,6 +169,7 @@ export class DirectoryArtifactReader implements ArtifactReader { assertContainedDirectory(root, directory, admittedRoot.identity), ); } + abortIfNeeded(signal); if (unrepresentableNameCount > 0) options?.onCoverage?.({ status: "unknown", @@ -254,6 +255,7 @@ export class DirectoryArtifactReader implements ArtifactReader { }; try { for (;;) { + abortIfNeeded(signal); await withDirectoryIoContext("inspect directory", directory, () => assertContainedDirectory(root, directory, admittedRoot.identity), ); @@ -297,8 +299,7 @@ export class DirectoryArtifactReader implements ArtifactReader { await this.#finishDirectory(handle, directory, traversal); } if (traversal.kind === "failed") throw traversal.cause; - childDirectories.reverse(); - pending.push(...childDirectories); + for (const child of childDirectories.reverse()) pending.push(child); } async #finishDirectory( diff --git a/src/artifacts/FitArtifactReader.ts b/src/artifacts/FitArtifactReader.ts index 2130ad7e61..37cd34b15f 100644 --- a/src/artifacts/FitArtifactReader.ts +++ b/src/artifacts/FitArtifactReader.ts @@ -1,5 +1,5 @@ import { ArtifactPreparation } from "./ArtifactPreparation.js"; -import { Readable, Transform, pipeline } from "node:stream"; +import { Readable, Transform } from "node:stream"; import { setImmediate } from "node:timers/promises"; import type { FitMetadata } from "../domain/artifactFormatMetadata.js"; @@ -14,6 +14,7 @@ import { type ArtifactTraversalOptions, } from "./ArtifactReader.js"; import { ArtifactResourceScope } from "./ArtifactResourceScope.js"; +import { ownArtifactStream } from "./OwnedArtifactStream.js"; import { assertArtifactReadActive } from "./ArtifactFileOperation.js"; import { ArtifactFileSource } from "./ArtifactFileSource.js"; import { @@ -115,45 +116,13 @@ export class FitArtifactReader implements ArtifactReader { cancellationFailure, }); const output = this.#imageStream(stored, signal); - output.on("error", () => undefined); - this.#streams.add(output); - let producerComplete = false; - const onAbort = (): void => { - // Active I/O and final validation own failure precedence. Buffered - // readable bytes still belong to the caller after they finish. - if (!producerComplete || output.destroyed) return; - output.destroy(cancellationFailure(signal?.reason)); - }; - const retired = new Promise((resolve) => { - output.once("close", () => { - this.#streams.delete(output); - signal?.removeEventListener("abort", onAbort); - resolve(); - }); + ownArtifactStream(source, output, { + scope: this.#scope, + streams: this.#streams, + signal, + cancellationFailure, + label: "FIT", }); - signal?.addEventListener("abort", onAbort, { once: true }); - if (signal?.aborted) onAbort(); - void this.#scope - .run(async () => { - // Retain both producer work and the unconsumed readable stream. - await Promise.all([ - new Promise((resolve) => - pipeline(source, output, () => { - producerComplete = true; - if (signal?.aborted) onAbort(); - resolve(); - }), - ), - retired, - ]); - }) - .catch((cause: unknown) => - output.destroy( - cause instanceof Error - ? cause - : new Error("FIT stream ownership failed", { cause }), - ), - ); return output; }); } diff --git a/src/artifacts/HexRecordArtifactReader.test.ts b/src/artifacts/HexRecordArtifactReader.test.ts index 037c296547..de2202b077 100644 --- a/src/artifacts/HexRecordArtifactReader.test.ts +++ b/src/artifacts/HexRecordArtifactReader.test.ts @@ -27,6 +27,29 @@ const writeImage = async (name: string, text: string): Promise => { return path; }; +it.each(["close", "cancel"] as const)( + "does not complete a decoded-region inventory after %s while paused", + async (interruption) => { + const path = await writeImage( + "image.hex", + `${intelHexRecord(0, 0, [1])}\n:00000001FF\n`, + ); + const reader = new HexRecordArtifactReader(path, "intel-hex"); + const controller = new AbortController(); + try { + const entries = reader.entries(controller.signal)[Symbol.asyncIterator](); + expect((await entries.next()).done).toBe(false); + if (interruption === "close") await reader.close(); + else controller.abort(); + await expect(entries.next()).rejects.toMatchObject({ + reason: interruption === "close" ? "unavailable" : "cancelled", + }); + } finally { + await reader.close(); + } + }, +); + it("reports unknown coverage and termination facts as limitations", async () => { const truncated = await scanCanonicalArtifactInventory( await writeImage("truncated.s19", SREC_S19.replace("S9030000FC\n", "")), @@ -66,7 +89,13 @@ it("fails closed on over-limit records", async () => { scanCanonicalArtifactInventory( await writeImage("runs.hex", `${runs.join("\n")}\n`), ), - ).rejects.toMatchObject({ reason: "limit", boundary: "capacity" }); + ).rejects.toMatchObject({ + reason: "limit", + boundary: "capacity", + message: expect.stringContaining( + "more than 65536 address-discontiguous record runs", + ), + }); }); it("leaves an invalid first record to plain file classification", async () => { diff --git a/src/artifacts/InesArtifactReader.ts b/src/artifacts/InesArtifactReader.ts index 1be8e79cdf..3ff48f8562 100644 --- a/src/artifacts/InesArtifactReader.ts +++ b/src/artifacts/InesArtifactReader.ts @@ -57,7 +57,7 @@ export class InesArtifactReader implements ArtifactReader { this.#layout = parsed.value; this.#entries.clear(); for (const region of parsed.value.regions) { - abortIfNeeded(signal); + this.#assertOpen(signal); const entry: ArtifactEntry = { path: region.path, kind: "file", @@ -74,6 +74,7 @@ export class InesArtifactReader implements ArtifactReader { this.#entries.set(entry.adapterKey, copyArtifactEntry(entry)); yield entry; } + this.#assertOpen(signal); } async open(entry: ArtifactEntry, signal?: AbortSignal): Promise { @@ -166,9 +167,7 @@ export class InesArtifactReader implements ArtifactReader { } async #verifySource(signal?: AbortSignal): Promise { - abortIfNeeded(signal); - if (this.#closed) - throw new ArtifactReaderFailure("unavailable", "iNES reader is closed"); + this.#assertOpen(signal); try { await assertStableRegularFileIdentity( this.source.handle, @@ -188,4 +187,10 @@ export class InesArtifactReader implements ArtifactReader { // State validation is asynchronous even after the final byte was consumed. abortIfNeeded(signal); } + + #assertOpen(signal?: AbortSignal): void { + abortIfNeeded(signal); + if (this.#closed) + throw new ArtifactReaderFailure("unavailable", "iNES reader is closed"); + } } diff --git a/src/artifacts/MachOSliceArtifactReader.test.ts b/src/artifacts/MachOSliceArtifactReader.test.ts index 43eea98dd8..9852872836 100644 --- a/src/artifacts/MachOSliceArtifactReader.test.ts +++ b/src/artifacts/MachOSliceArtifactReader.test.ts @@ -1,5 +1,6 @@ import { EventEmitter, once } from "node:events"; import { PassThrough } from "node:stream"; +import { buffer } from "node:stream/consumers"; import { truncate, writeFile } from "node:fs/promises"; import { join } from "node:path"; @@ -14,6 +15,7 @@ import { ProviderProcessSupervisor } from "../process/ProviderProcess.js"; import type { NativeCommandRunner } from "../native/CommandRunner.js"; import { NativeCommandFailure } from "../native/CommandRunner.js"; import { createDeferred } from "../../tests/fixtures/binarySession.js"; +import { awaitAcknowledgement } from "../../tests/fixtures/lifecycleAcknowledgement.js"; import { OwnedFileHandle } from "../filesystem/OwnedFileHandle.js"; import type { ArtifactEntry } from "./ArtifactReader.js"; @@ -157,56 +159,60 @@ describe("Mach-O command cleanup lifecycle", () => { const reader = new MachOSliceArtifactReader(binary, {}, runner); const iteration = reader.entries(); const firstEntry = iteration[Symbol.asyncIterator]().next(); - await started; - const closing = reader.close(); - await Promise.resolve(); - expect(cleanupRetries).toBe(0); - finishCommand( - err( - new NativeCommandFailure( - "lipo", - "timeout", - null, - undefined, - { - stdout: "partial lipo output", - stderr: "partial lipo diagnostic", - stdoutBytes: 19, - stderrBytes: 23, - exitCode: null, - signal: null, - truncated: false, - }, - "capture process group remains unverified", - ["capture-group:9999"], - ), + const commandResult = err( + new NativeCommandFailure( + "lipo", + "timeout", + null, + undefined, + { + stdout: "partial lipo output", + stderr: "partial lipo diagnostic", + stdoutBytes: 19, + stderrBytes: 23, + exitCode: null, + signal: null, + truncated: false, + }, + "capture process group remains unverified", + ["capture-group:9999"], ), ); - - await expect(firstEntry).rejects.toMatchObject({ - reason: "unavailable", - cause: expect.objectContaining({ - cleanupFailure: "capture process group remains unverified", - }), - cleanup: { - reason: "capture process group remains unverified", - resources: ["capture-group:9999"], - }, - }); - await expect(closing).rejects.toMatchObject({ - cleanup: { - reason: - "run:first, process-group:1234: first process group remains live; run:second, process-group:5678: second process group remains live", - resources: [ - "run:first", - "process-group:1234", - "run:second", - "process-group:5678", - ], - }, - }); - await reader.close(); - expect(cleanupRetries).toBe(2); + try { + await awaitAcknowledgement(started, firstEntry, "lipo capture start"); + const closing = reader.close(); + await Promise.resolve(); + expect(cleanupRetries).toBe(0); + finishCommand(commandResult); + await expect(firstEntry).rejects.toMatchObject({ + reason: "unavailable", + cause: expect.objectContaining({ + cleanupFailure: "capture process group remains unverified", + }), + cleanup: { + reason: "capture process group remains unverified", + resources: ["capture-group:9999"], + }, + }); + await expect(closing).rejects.toMatchObject({ + cleanup: { + reason: + "run:first, process-group:1234: first process group remains live; run:second, process-group:5678: second process group remains live", + resources: [ + "run:first", + "process-group:1234", + "run:second", + "process-group:5678", + ], + }, + }); + await reader.close(); + expect(cleanupRetries).toBe(2); + } finally { + finishCommand(commandResult); + await firstEntry.catch(() => undefined); + await reader.close().catch(() => undefined); + } }); }); @@ -633,6 +639,7 @@ describe("Mach-O slice stream ownership", () => { const release = createDeferred(); const nativeRead = source.handle.read.bind(source.handle); let reads: { mockRestore(): void } | undefined; + let consumed: Promise | undefined; try { const [entry] = await sliceEntries(reader); reads = vi.spyOn(source.handle, "read").mockImplementation((async ( @@ -643,8 +650,9 @@ describe("Mach-O slice stream ownership", () => { return Reflect.apply(nativeRead, source.handle, args); }) as typeof source.handle.read); const stream = await reader.open(entry!); - stream.read(0); - await reached.promise; + consumed = buffer(stream); + void consumed.catch(() => undefined); + await awaitAcknowledgement(reached.promise, consumed, "slice read"); let closed = false; const closing = reader.close().then(() => { closed = true; @@ -656,9 +664,13 @@ describe("Mach-O slice stream ownership", () => { expect(stream.closed).toBe(true); } finally { release.resolve(); - await reader.close(); - reads?.mockRestore(); - await source.handle.close(); + try { + await reader.close(); + } finally { + await consumed?.catch(() => undefined); + reads?.mockRestore(); + await source.handle.close(); + } } }); @@ -729,7 +741,7 @@ it("joins Mach-O structural enumeration reads admitted before close", async () = const traversal = reader.entries()[Symbol.asyncIterator](); const next = traversal.next(); try { - await reached.promise; + await awaitAcknowledgement(reached.promise, next, "slice enumeration read"); let closed = false; const closing = reader.close().then(() => { closed = true; diff --git a/src/artifacts/NativeDmgArtifactReader.ts b/src/artifacts/NativeDmgArtifactReader.ts index 90a1d01305..235b5a795b 100644 --- a/src/artifacts/NativeDmgArtifactReader.ts +++ b/src/artifacts/NativeDmgArtifactReader.ts @@ -213,9 +213,17 @@ export class NativeDmgArtifactReader implements ArtifactReader { [Symbol.asyncIterator](); try { for (;;) { - if (this.#closed) return; + if (this.#closed) + throw new ArtifactReaderFailure( + "unavailable", + "DMG reader is closed", + ); const next = await this.#trackDirectoryOperation(() => entries.next()); - if (this.#closed) return; + if (this.#closed) + throw new ArtifactReaderFailure( + "unavailable", + "DMG reader is closed", + ); if (next.done) return; yield { ...next.value, path: `${prefix}/${next.value.path}` }; } diff --git a/src/artifacts/NativeDmgProtocol.test.ts b/src/artifacts/NativeDmgProtocol.test.ts index f7bbb9e1e5..7e9106a734 100644 --- a/src/artifacts/NativeDmgProtocol.test.ts +++ b/src/artifacts/NativeDmgProtocol.test.ts @@ -81,6 +81,12 @@ it.skipIf(process.platform === "win32").each([false, true])( paths.push(entry.path); expect(paths).toContain("image.dmg/Volume/readable.txt"); expect(coverage).toEqual(unknownCoverage ? [producerCoverage] : []); + const paused = reader.entries()[Symbol.asyncIterator](); + expect((await paused.next()).done).toBe(false); + await reader.close(); + await expect(paused.next()).rejects.toMatchObject({ + reason: "unavailable", + }); } finally { try { await reader.close(); diff --git a/src/artifacts/NtfsMftArtifactReader.test.ts b/src/artifacts/NtfsMftArtifactReader.test.ts index 3fec45e728..5f7be93dba 100644 --- a/src/artifacts/NtfsMftArtifactReader.test.ts +++ b/src/artifacts/NtfsMftArtifactReader.test.ts @@ -7,8 +7,11 @@ import { ArtifactFileSource } from "./ArtifactFileSource.js"; import { assertArtifactReadActive } from "./ArtifactFileOperation.js"; import { NtfsMftArtifactReader } from "./NtfsMftArtifactReader.js"; -import { MFT_DEFAULT_RECORD_BYTES } from "../domain/windows/ntfsMft.js"; import { createTestTempDirectory } from "../../tests/fixtures/temporaryDirectory.js"; +import { awaitAcknowledgement } from "../../tests/fixtures/lifecycleAcknowledgement.js"; + +// The constructed FILE records intentionally use the 1024-byte default. +const MFT_DEFAULT_RECORD_BYTES = 1024; import { linkedMftFixture } from "../../tests/fixtures/ntfsMftExtension.js"; import { scanCanonicalArtifactInventory } from "../../tests/fixtures/artifactInventory.js"; @@ -212,7 +215,7 @@ it("retries a shared preparation cancelled by another traversal", async () => { const cancelledResult = expect(cancelled).rejects.toMatchObject({ reason: "cancelled", }); - await started; + await awaitAcknowledgement(started, cancelled, "NTFS traversal read"); const other = reader.entries()[Symbol.asyncIterator]().next(); controller.abort("cancelled first traversal"); await cancelledResult; @@ -225,6 +228,22 @@ it("retries a shared preparation cancelled by another traversal", async () => { } }); +it("reports cancellation during MFT coverage", async () => { + const path = await writeMft("$MFT", recordBytes({})); + const reader = new NtfsMftArtifactReader(path); + const controller = new AbortController(); + try { + const entries = reader + .entries(controller.signal, { + onCoverage: () => controller.abort(), + }) + [Symbol.asyncIterator](); + await expect(entries.next()).rejects.toMatchObject({ reason: "cancelled" }); + } finally { + await reader.close(); + } +}); + it("revalidates the source before reusing a completed header inventory", async () => { const path = await writeMft("$MFT", recordBytes({})); const reader = new NtfsMftArtifactReader(path); @@ -303,7 +322,7 @@ it("does not finish a traversal when close begins during final source verificati const rejected = expect(traversal).rejects.toMatchObject({ reason: "unavailable", }); - await started; + await awaitAcknowledgement(started, traversal, "NTFS close read"); const closing = reader.close(); resume(); await rejected; diff --git a/src/artifacts/NtfsMftArtifactReader.ts b/src/artifacts/NtfsMftArtifactReader.ts index 4f54fa3982..0989f9e421 100644 --- a/src/artifacts/NtfsMftArtifactReader.ts +++ b/src/artifacts/NtfsMftArtifactReader.ts @@ -74,6 +74,7 @@ export class NtfsMftArtifactReader implements ArtifactReader { // An MFT declares no members, so the root occurrence is the whole // inventory; the coverage callback reports what its parse did not establish. options?.onCoverage?.(coverageOf(this.#limitations)); + this.#assertOpen(signal); } async open(entry: ArtifactEntry, _signal?: AbortSignal): Promise { diff --git a/src/artifacts/OwnedArtifactStream.ts b/src/artifacts/OwnedArtifactStream.ts new file mode 100644 index 0000000000..1f1ebce415 --- /dev/null +++ b/src/artifacts/OwnedArtifactStream.ts @@ -0,0 +1,67 @@ +import { pipeline, type Readable, type Transform } from "node:stream"; + +import type { ArtifactResourceScope } from "./ArtifactResourceScope.js"; + +/** Keep the carrier and buffered output owned through production and retirement. */ +export const ownArtifactStream = ( + source: Readable, + output: Transform, + { + scope, + streams, + signal, + cancellationFailure, + label, + }: { + readonly scope: ArtifactResourceScope; + readonly streams: Set; + readonly signal: AbortSignal | undefined; + readonly cancellationFailure: (reason: unknown) => Error; + readonly label: string; + }, +): void => { + output.on("error", () => undefined); + streams.add(output); + let producerComplete = false; + const onAbort = (): void => { + // Active I/O and validation own failure precedence; cancellation also + // retires buffered output after production completes. + if (!producerComplete || output.destroyed) return; + output.destroy(cancellationFailure(signal?.reason)); + }; + const retired = new Promise((resolve) => { + output.once("close", () => { + streams.delete(output); + signal?.removeEventListener("abort", onAbort); + resolve(); + }); + }); + signal?.addEventListener("abort", onAbort, { once: true }); + if (signal?.aborted) onAbort(); + output.once("finish", () => { + // A successful writable finish follows the transform's final validation. + // Pipeline retirement may follow later while readable bytes remain buffered. + producerComplete = true; + if (signal?.aborted) onAbort(); + }); + void scope + .run(async () => { + await Promise.all([ + new Promise((resolve) => { + pipeline(source, output, () => { + producerComplete = true; + if (signal?.aborted) onAbort(); + resolve(); + }); + }), + retired, + ]); + }) + .catch((cause: unknown) => + output.destroy( + cause instanceof Error + ? cause + : new Error(`${label} stream ownership failed`, { cause }), + ), + ); +}; diff --git a/src/artifacts/OwnedAsyncArtifactStream.ts b/src/artifacts/OwnedAsyncArtifactStream.ts new file mode 100644 index 0000000000..16f0b3675e --- /dev/null +++ b/src/artifacts/OwnedAsyncArtifactStream.ts @@ -0,0 +1,81 @@ +import { Readable } from "node:stream"; + +import { ArtifactReaderFailure } from "./ArtifactReader.js"; +import { artifactReadCancelledBy } from "./ArtifactFileOperation.js"; +import type { ArtifactResourceScope } from "./ArtifactResourceScope.js"; + +/** Retain a member iterator, including pending reads, until its stream closes. */ +export const ownAsyncArtifactStream = ( + chunks: AsyncIterable, + controller: AbortController, + { + scope, + streams, + signal, + label, + highWaterMark, + }: { + readonly scope: ArtifactResourceScope; + readonly streams: Map; + readonly signal: AbortSignal | undefined; + readonly label: string; + readonly highWaterMark: number; + }, +): Readable => { + let pending = false; + const cancelled = (): ArtifactReaderFailure => + new ArtifactReaderFailure("cancelled", `${label} member read cancelled`, { + cause: signal?.reason, + }); + const tracked = async function* (): AsyncGenerator { + const iterator = chunks[Symbol.asyncIterator](); + try { + while (true) { + pending = true; + let next: IteratorResult; + try { + next = await iterator.next(); + } catch (cause) { + if (artifactReadCancelledBy(cause, signal)) throw cancelled(); + throw cause; + } finally { + pending = false; + } + if (signal?.aborted) throw cancelled(); + if (next.done) return; + yield next.value; + } + } finally { + await iterator.return?.(); + } + }; + const output = Readable.from(tracked(), { objectMode: false, highWaterMark }); + const onAbort = (): void => { + controller.abort(signal?.reason); + // An outstanding iterator read owns its failure; a successful read will + // observe cancellation before yielding its next chunk. + if (!pending) output.destroy(cancelled()); + }; + output.on("error", () => undefined); + streams.set(output, controller); + const completion = new Promise((resolve) => { + output.once("close", () => { + signal?.removeEventListener("abort", onAbort); + streams.delete(output); + resolve(); + }); + }); + void scope + .run(() => completion) + .catch((cause: unknown) => { + controller.abort(cause); + output.destroy( + cause instanceof Error + ? cause + : new Error(`${label} stream ownership failed`, { cause }), + ); + }); + signal?.addEventListener("abort", onAbort, { once: true }); + if (signal?.aborted === true) onAbort(); + return output; +}; diff --git a/src/artifacts/PositionalArtifactReader.ts b/src/artifacts/PositionalArtifactReader.ts index 72b0931095..2e01b8ab29 100644 --- a/src/artifacts/PositionalArtifactReader.ts +++ b/src/artifacts/PositionalArtifactReader.ts @@ -1,5 +1,5 @@ import { ArtifactPreparation } from "./ArtifactPreparation.js"; -import { Readable, Transform, pipeline } from "node:stream"; +import { Readable, Transform } from "node:stream"; import { setImmediate } from "node:timers/promises"; import { @@ -12,6 +12,7 @@ import { type ArtifactTraversalOptions, } from "./ArtifactReader.js"; import { ArtifactResourceScope } from "./ArtifactResourceScope.js"; +import { ownArtifactStream } from "./OwnedArtifactStream.js"; import { assertArtifactReadActive } from "./ArtifactFileOperation.js"; import { ArtifactFileSource } from "./ArtifactFileSource.js"; import { readFileHandleChunks } from "../filesystem/readFileHandleChunks.js"; @@ -116,6 +117,14 @@ export abstract class PositionalArtifactReader implements ArtifactReader { ); const { handle } = await this.#source.read(signal); this.assertOpen(signal); + const cancellationFailure = (reason: unknown): ArtifactReaderFailure => + new ArtifactReaderFailure( + "cancelled", + `${this.#label} read cancelled`, + { + cause: reason, + }, + ); const source = stored.declaredSize === 0 ? Readable.from([]) @@ -123,63 +132,17 @@ export abstract class PositionalArtifactReader implements ArtifactReader { start: stored.byteOffset, end: stored.byteOffset + stored.declaredSize - 1, signal, - cancellationFailure: (reason) => - new ArtifactReaderFailure( - "cancelled", - `${this.#label} read cancelled`, - { cause: reason }, - ), + cancellationFailure, }); const output = this.#entryStream(stored, signal); // Retirement can destroy the stream before its consumer listens. - output.on("error", () => undefined); - this.#streams.add(output); - let producerComplete = false; - const onAbort = (): void => { - // Preserve active read/validation errors; after production, the caller - // still owns buffered bytes and cancellation must retire that result. - if (!producerComplete || output.destroyed) return; - output.destroy( - new ArtifactReaderFailure( - "cancelled", - `${this.#label} read cancelled`, - { cause: signal?.reason }, - ), - ); - }; - // Writable completion can leave unread bytes buffered on the readable - // side, so the stream stays owned until that side closes too. - const retired = new Promise((resolve) => { - output.once("close", () => { - this.#streams.delete(output); - signal?.removeEventListener("abort", onAbort); - resolve(); - }); + ownArtifactStream(source, output, { + scope: this.#scope, + streams: this.#streams, + signal, + cancellationFailure, + label: this.#label, }); - signal?.addEventListener("abort", onAbort, { once: true }); - if (signal?.aborted) onAbort(); - void this.#scope - .run(async () => { - await Promise.all([ - new Promise((resolve) => { - pipeline(source, output, () => { - producerComplete = true; - if (signal?.aborted) onAbort(); - resolve(); - }); - }), - retired, - ]); - }) - .catch((cause: unknown) => - output.destroy( - cause instanceof Error - ? cause - : new Error(`${this.#label} stream ownership failed`, { - cause, - }), - ), - ); return output; }); } diff --git a/src/artifacts/PyInstallerArtifactReader.ts b/src/artifacts/PyInstallerArtifactReader.ts index f6eab57113..3298164dcd 100644 --- a/src/artifacts/PyInstallerArtifactReader.ts +++ b/src/artifacts/PyInstallerArtifactReader.ts @@ -180,6 +180,7 @@ export class PyInstallerArtifactReader implements ArtifactReader { this.#members.clear(); return value; }); + this.#signal(signal); options?.onCoverage?.(loaded.traversal); for (const member of loaded.members) { abortIfNeeded(active); @@ -187,6 +188,7 @@ export class PyInstallerArtifactReader implements ArtifactReader { this.#members.set(sealed.entry.adapterKey, sealed); yield sealed.entry; } + this.#signal(signal); } async open(entry: ArtifactEntry, signal?: AbortSignal): Promise { diff --git a/src/artifacts/RegularFileAdmission.test.ts b/src/artifacts/RegularFileAdmission.test.ts index eb1ce843b3..d9319462cc 100644 --- a/src/artifacts/RegularFileAdmission.test.ts +++ b/src/artifacts/RegularFileAdmission.test.ts @@ -44,7 +44,12 @@ it("returns the original validated metadata snapshot with its actual descriptor" }); return handle; }); - const descriptor = await openRegularFile(path, { symlinks: "reject" }); + // The test just wrote this file. Settling its fresh Windows ChangeTime would + // replace the first observation, so admit it as written on every host. + const descriptor = await openRegularFile(path, { + symlinks: "reject", + changeTime: "as-written", + }); try { expect(descriptor.handle).toBe(opened); expect(descriptor.initial).toBe(validated); diff --git a/src/artifacts/ReleaseExposureReader.test.ts b/src/artifacts/ReleaseExposureReader.test.ts index e89c00e080..32361f42a9 100644 --- a/src/artifacts/ReleaseExposureReader.test.ts +++ b/src/artifacts/ReleaseExposureReader.test.ts @@ -201,6 +201,8 @@ vi.mock("node:fs/promises", async () => { const PLANTED_SOURCE = "release-exposure-planted-secret-7c2e9a"; const PLANTED_KEY = "AKIAPLANT9EXPOSUREXX"; const ignoreText = (): void => undefined; +// Keep the marker separate so Vite does not treat fixture text as this file's source map. +const INLINE_MAP_PREFIX = "# sourceMappingURL=data:application/json;base64,"; describe("ReleaseExposureReader review regressions", () => { it("keeps inline CSS source-map coverage unknown in files and packages", async () => { @@ -691,25 +693,31 @@ describe("ReleaseExposureReader limits", () => { } }); - it("counts members past the package entry cap and marks coverage unknown", async () => { - const root = await createTestTempDirectory("rea-release-exposure-entries-"); - const files = [ - ...Array.from({ length: 4096 }, (_, index) => ({ - path: `f${String(index)}.bin`, - bytes: Buffer.from("x"), - })), - { path: "hidden.js.map", bytes: Buffer.from("{not json") }, - { path: "secret.txt", bytes: Buffer.from("hello\n") }, - ]; - await writeFile(join(root, "app.dll"), dotnetBundleFixture(files).bytes); - const scanned = await inspect(root); - expect(scanned.coverage).toBe("unknown"); - expect(scanned.source_maps.skipped).toBeGreaterThan(0); - expect(scanned.text_members.skipped).toBeGreaterThan(0); - expect(scanned.omitted.map((item) => item.subject)).toEqual( - expect.arrayContaining(["source maps", "text members"]), - ); - }, 60_000); + it( + "counts members past the package entry cap and marks coverage unknown", + { tags: ["long-running"] }, + async () => { + const root = await createTestTempDirectory( + "rea-release-exposure-entries-", + ); + const files = [ + ...Array.from({ length: 4096 }, (_, index) => ({ + path: `f${String(index)}.bin`, + bytes: Buffer.from("x"), + })), + { path: "hidden.js.map", bytes: Buffer.from("{not json") }, + { path: "secret.txt", bytes: Buffer.from("hello\n") }, + ]; + await writeFile(join(root, "app.dll"), dotnetBundleFixture(files).bytes); + const scanned = await inspect(root); + expect(scanned.coverage).toBe("unknown"); + expect(scanned.source_maps.skipped).toBeGreaterThan(0); + expect(scanned.text_members.skipped).toBeGreaterThan(0); + expect(scanned.omitted.map((item) => item.subject)).toEqual( + expect.arrayContaining(["source maps", "text members"]), + ); + }, + ); it("does not drop native images before the service samples them", async () => { const root = await createTestTempDirectory("rea-release-exposure-natives-"); @@ -1498,28 +1506,34 @@ describe("ReleaseExposureReader member allowances", () => { }, ); - it("keeps a later source map readable after many large binary members", async () => { - const root = await createTestTempDirectory("rea-release-exposure-budget-"); - const binary = Buffer.alloc(MAX_RELEASE_EXPOSURE_TEXT_MEMBER_BYTES); - const oversizedScript = Buffer.alloc( - MAX_RELEASE_EXPOSURE_TEXT_MEMBER_BYTES + 1, - 0x20, - ); - await writeFile( - join(root, "app.asar"), - asarArchive([ - ...Array.from({ length: 160 }, (_, index) => ({ - name: `a${String(index)}.bin`, - bytes: binary, - })), - { name: "b.js", bytes: oversizedScript }, - { name: "z.js.map", bytes: embeddedSourceMap() }, - ]), - ); - const scanned = await inspect(join(root, "app.asar")); - expect(scanned.source_maps).toMatchObject({ embedded: 1, skipped: 1 }); - expect(scanned.extractable.readable_members).toBe(162); - }, 60_000); + it( + "keeps a later source map readable after many large binary members", + { tags: ["long-running"] }, + async () => { + const root = await createTestTempDirectory( + "rea-release-exposure-budget-", + ); + const binary = Buffer.alloc(MAX_RELEASE_EXPOSURE_TEXT_MEMBER_BYTES); + const oversizedScript = Buffer.alloc( + MAX_RELEASE_EXPOSURE_TEXT_MEMBER_BYTES + 1, + 0x20, + ); + await writeFile( + join(root, "app.asar"), + asarArchive([ + ...Array.from({ length: 160 }, (_, index) => ({ + name: `a${String(index)}.bin`, + bytes: binary, + })), + { name: "b.js", bytes: oversizedScript }, + { name: "z.js.map", bytes: embeddedSourceMap() }, + ]), + ); + const scanned = await inspect(join(root, "app.asar")); + expect(scanned.source_maps).toMatchObject({ embedded: 1, skipped: 1 }); + expect(scanned.extractable.readable_members).toBe(162); + }, + ); it("reports an oversized linker map as a debug companion wherever it ships", async () => { const root = await createTestTempDirectory("rea-release-exposure-link-"); @@ -2067,7 +2081,7 @@ const plistObject = (type: number, length: number, body: Buffer): Buffer => describe("ReleaseExposureReader text encodings", () => { const marker = "-----BEGIN PRIVATE KEY-----\n"; - const inlineMap = `//# sourceMappingURL=data:application/json;base64,${Buffer.from( + const inlineMap = `//${INLINE_MAP_PREFIX}${Buffer.from( JSON.stringify({ version: 3, sources: ["src/app.ts"], @@ -4328,7 +4342,7 @@ describe("ReleaseExposureReader script roles over a .map name", () => { }); describe("ReleaseExposureReader HTML script references", () => { - const inlineMap = `//# sourceMappingURL=data:application/json;base64,${Buffer.from( + const inlineMap = `//${INLINE_MAP_PREFIX}${Buffer.from( JSON.stringify({ version: 3, sources: ["src/app.ts"], @@ -4620,7 +4634,7 @@ describe("ReleaseExposureReader cumulative text allowance", () => { describe("ReleaseExposureReader unfollowed map links", () => { const SCRIPT = "const value = 1;\n//# sourceMappingURL=app.js.map\n"; - const EMBEDDED_PAYLOAD = `const value = 1;\n//# sourceMappingURL=data:application/json;base64,${Buffer.from( + const EMBEDDED_PAYLOAD = `const value = 1;\n//${INLINE_MAP_PREFIX}${Buffer.from( JSON.stringify({ version: 3, sources: ["a.ts"], diff --git a/src/artifacts/StableArtifactFailure.ts b/src/artifacts/StableArtifactFailure.ts index 17bd02e64f..a5b9615443 100644 --- a/src/artifacts/StableArtifactFailure.ts +++ b/src/artifacts/StableArtifactFailure.ts @@ -5,6 +5,44 @@ import { ProviderCleanupError } from "../domain/providerCleanupError.js"; import { ArtifactReaderFailure } from "./ArtifactReader.js"; import { bindArtifactFileCleanup } from "./ArtifactFileOperation.js"; import { completedStableArtifactObservation } from "./StableArtifactObservation.js"; +import { isSelectedRegularFileError } from "../filesystem/RegularFile.js"; +import { + AnalysisAccessDeniedError, + AnalysisInputError, +} from "../domain/analysisErrorCore.js"; + +/** Project filesystem errors that identify a refused or unreadable selected file. */ +export const projectSelectedArtifactFileFailure = ( + cause: unknown, + operation: string, + path: string, + description: string, + field = "path", +): AnalysisError | undefined => { + if (!(cause instanceof Error) || !("code" in cause)) return undefined; + if (cause.code === "EACCES" || cause.code === "EPERM") + return new AnalysisAccessDeniedError(operation, path, cause.code, { + cause, + }); + if (isSelectedRegularFileError(cause)) + return new AnalysisInputError(operation, { cause }, [ + { + path: [field], + reason: "invalid_value", + message: `Selected ${description} could not be read (${String(cause.code)}): ${path}.`, + }, + ]); + return undefined; +}; + +/** Explain the selected image read budget and its fixed upper bound. */ +export const imageFileSizeRemediation = ( + selectedBytes: number, + maximumBytes: number, +): string => + selectedBytes >= maximumBytes + ? `The image exceeds the ${String(maximumBytes)}-byte input ceiling this operation accepts. This release has no runtime override. Select a smaller artifact.` + : `Raise max_file_bytes above ${String(selectedBytes)} bytes or select a smaller artifact; the limit is the caller-selected input read budget.`; /** Name an observed regular-file retry that admission will validate again. */ export const regularFileSymlinkRecovery = (resolvedPath: string): string => diff --git a/src/artifacts/UImageArtifactReader.boundaries.test.ts b/src/artifacts/UImageArtifactReader.boundaries.test.ts index 5e594c94d1..3248cb380d 100644 --- a/src/artifacts/UImageArtifactReader.boundaries.test.ts +++ b/src/artifacts/UImageArtifactReader.boundaries.test.ts @@ -103,9 +103,12 @@ it("preserves cancellation during payload CRC admission", async () => { ), ).toBe(true); } finally { - await reader.close(); - reads.mockRestore(); - await admitted.handle.close(); + try { + await reader.close(); + } finally { + reads.mockRestore(); + await admitted.handle.close(); + } } }); @@ -149,9 +152,12 @@ it("admits a multi-image count before scanning the remaining payload", async () results.reduce((sum, result) => sum + result.bytesRead, 0), ).toBeLessThanOrEqual(UIMAGE_HEADER_SIZE + UIMAGE_MULTI_TABLE_WINDOW); } finally { - await reader.close(); - reads.mockRestore(); - await admitted.handle.close(); + try { + await reader.close(); + } finally { + reads.mockRestore(); + await admitted.handle.close(); + } } }); diff --git a/src/artifacts/UImageArtifactReader.ts b/src/artifacts/UImageArtifactReader.ts index 05c9af9347..a8fc6a38a7 100644 --- a/src/artifacts/UImageArtifactReader.ts +++ b/src/artifacts/UImageArtifactReader.ts @@ -1,5 +1,5 @@ import { ArtifactPreparation } from "./ArtifactPreparation.js"; -import { Readable, Transform, pipeline } from "node:stream"; +import { Readable, Transform } from "node:stream"; import { crc32 } from "node:zlib"; import { readArtifactFileBytes } from "./ArtifactFileRange.js"; import { setImmediate } from "node:timers/promises"; @@ -19,6 +19,7 @@ import { type ArtifactTraversalOptions, } from "./ArtifactReader.js"; import { ArtifactResourceScope } from "./ArtifactResourceScope.js"; +import { ownArtifactStream } from "./OwnedArtifactStream.js"; import { readFileHandleChunks } from "../filesystem/readFileHandleChunks.js"; import type { StableRegularFileDescriptor } from "../filesystem/RegularFile.js"; import { @@ -114,6 +115,10 @@ export class UImageArtifactReader implements ArtifactReader { ); const { handle } = await this.#source.read(signal); this.#assertOpen(signal); + const cancellationFailure = (reason: unknown): ArtifactReaderFailure => + new ArtifactReaderFailure("cancelled", "uImage read cancelled", { + cause: reason, + }); const source = stored.declaredSize === 0 ? Readable.from([]) @@ -121,60 +126,16 @@ export class UImageArtifactReader implements ArtifactReader { start: stored.byteOffset, end: stored.byteOffset + stored.declaredSize - 1, signal, - cancellationFailure: (reason) => - new ArtifactReaderFailure( - "cancelled", - "uImage read cancelled", - { - cause: reason, - }, - ), + cancellationFailure, }); const output = this.#memberStream(stored, signal); - output.on("error", () => undefined); - this.#streams.add(output); - let producerComplete = false; - const onAbort = (): void => { - // Active reads/validation own failure precedence. After they complete, - // cancellation must still retire any unconsumed readable bytes. - if (!producerComplete || output.destroyed) return; - output.destroy( - new ArtifactReaderFailure("cancelled", "uImage read cancelled", { - cause: signal?.reason, - }), - ); - }; - const retired = new Promise((resolve) => { - output.once("close", () => { - this.#streams.delete(output); - signal?.removeEventListener("abort", onAbort); - resolve(); - }); + ownArtifactStream(source, output, { + scope: this.#scope, + streams: this.#streams, + signal, + cancellationFailure, + label: "uImage", }); - signal?.addEventListener("abort", onAbort, { once: true }); - if (signal?.aborted) onAbort(); - void this.#scope - .run(async () => { - // Writable completion can leave readable bytes buffered. Retain both - // producer I/O and the caller's stream until consumption or closure. - await Promise.all([ - new Promise((resolve) => - pipeline(source, output, () => { - producerComplete = true; - if (signal?.aborted) onAbort(); - resolve(); - }), - ), - retired, - ]); - }) - .catch((cause: unknown) => - output.destroy( - cause instanceof Error - ? cause - : new Error("uImage stream ownership failed", { cause }), - ), - ); return output; }); } diff --git a/src/artifacts/UnityFsArtifactReader.boundaries.test.ts b/src/artifacts/UnityFsArtifactReader.boundaries.test.ts index 672d7f3336..047b915783 100644 --- a/src/artifacts/UnityFsArtifactReader.boundaries.test.ts +++ b/src/artifacts/UnityFsArtifactReader.boundaries.test.ts @@ -7,6 +7,7 @@ import { setImmediate } from "node:timers/promises"; import { describe, expect, it, vi } from "vitest"; import { createTestTempDirectory } from "../../tests/fixtures/temporaryDirectory.js"; import { createDeferred } from "../../tests/fixtures/binarySession.js"; +import { awaitAcknowledgement } from "../../tests/fixtures/lifecycleAcknowledgement.js"; import { buildUnityFs } from "../../tests/fixtures/unityfs.js"; import { openRegularFile } from "../filesystem/RegularFile.js"; import { OwnedFileHandle } from "../filesystem/OwnedFileHandle.js"; @@ -185,9 +186,12 @@ describe("UnityFS allocation and malformed-input boundaries", () => { }); expect(reads).toHaveBeenCalledTimes(1); // bounded header only } finally { - await reader.close(); - reads.mockRestore(); - await admitted.handle.close(); + try { + await reader.close(); + } finally { + reads.mockRestore(); + await admitted.handle.close(); + } } }, ); @@ -223,6 +227,19 @@ describe("UnityFS allocation and malformed-input boundaries", () => { size, data: index === 0 ? Buffer.alloc(size, 0x61) : Buffer.alloc(0), })); + const control = new UnityFsArtifactReader( + await fixture( + buildUnityFs(members.slice(0, 256), { + blockSize: size, + encodeBlock: repeatedBlock, + }), + ), + ); + try { + expect(await entries(control)).toHaveLength(256); + } finally { + await control.close(); + } const reader = new UnityFsArtifactReader( await fixture( buildUnityFs(members, { @@ -232,7 +249,9 @@ describe("UnityFS allocation and malformed-input boundaries", () => { ), ); try { - await expect(entries(reader)).rejects.toMatchObject({ reason: "limit" }); + await expect(entries(reader)).rejects.toMatchObject({ + reason: "limit", + }); } finally { await reader.close(); } @@ -335,6 +354,38 @@ it.each([1, 2])( ); describe("UnityFS stream ownership and immutable cache", () => { + it("preserves a pending read failure when cancellation overlaps it", async () => { + const path = await fixture(buildUnityFs([{ path: "x", data: "bytes" }])); + const admitted = await openRegularFile(path, { symlinks: "reject" }); + const reader = new UnityFsArtifactReader(path, admitted); + const reached = createDeferred(); + const release = createDeferred(); + const failure = new Error("member read failed"); + const controller = new AbortController(); + const [entry] = await entries(reader); + const reads = vi + .spyOn(admitted.handle, "read") + .mockImplementation((async () => { + reached.resolve(); + await release.promise; + throw failure; + }) as typeof admitted.handle.read); + let consumed: Promise | undefined; + try { + consumed = buffer(await reader.open(entry!, controller.signal)); + await awaitAcknowledgement(reached.promise, consumed, "member read"); + controller.abort(new Error("caller cancelled")); + release.resolve(); + await expect(consumed).rejects.toBe(failure); + } finally { + release.resolve(); + await consumed?.catch(() => undefined); + reads.mockRestore(); + await reader.close(); + await admitted.handle.close(); + } + }); + it("retains descriptor ownership after cleanup fails and permits retry", async () => { const reader = new UnityFsArtifactReader( await fixture(buildUnityFs([{ path: "x", data: "bytes" }])), @@ -373,10 +424,12 @@ describe("UnityFS stream ownership and immutable cache", () => { await release.promise; return Reflect.apply(nativeRead, admitted.handle, args); }) as typeof admitted.handle.read); + let consumed: Promise | undefined; try { const stream = await reader.open(entry!); - stream.read(0); - await reached.promise; + consumed = buffer(stream); + void consumed.catch(() => undefined); + await awaitAcknowledgement(reached.promise, consumed, "member read"); let closed = false; const closing = reader.close().then(() => { closed = true; @@ -388,9 +441,13 @@ describe("UnityFS stream ownership and immutable cache", () => { expect(stream.closed).toBe(true); } finally { release.resolve(); - await reader.close(); - reads.mockRestore(); - await admitted.handle.close(); + try { + await reader.close(); + } finally { + await consumed?.catch(() => undefined); + reads.mockRestore(); + await admitted.handle.close(); + } } }); @@ -422,9 +479,12 @@ describe("UnityFS stream ownership and immutable cache", () => { buffer(await reader.open(entry!, controller.signal)), ).rejects.toMatchObject({ reason: "cancelled", cause: reason }); } finally { - await reader.close(); - reads.mockRestore(); - await admitted.handle.close(); + try { + await reader.close(); + } finally { + reads.mockRestore(); + await admitted.handle.close(); + } } }); }); @@ -442,12 +502,19 @@ describe("UnityFS stream capacity and cache ownership", () => { await fixture(buildUnityFs(members, { blockSize: 1 })), ); try { + let completed = 0; await expect( (async () => { - for (const entry of await entries(reader)) + for (const entry of await entries(reader)) { await buffer(await reader.open(entry)); + completed += 1; + } })(), - ).rejects.toMatchObject({ reason: "limit", boundary: "capacity" }); + ).rejects.toMatchObject({ + reason: "limit", + boundary: "capacity", + }); + expect(completed).toBe(128); } finally { await reader.close(); } diff --git a/src/artifacts/UnityFsArtifactReader.test.ts b/src/artifacts/UnityFsArtifactReader.test.ts index 4f992c7f47..55dfee3037 100644 --- a/src/artifacts/UnityFsArtifactReader.test.ts +++ b/src/artifacts/UnityFsArtifactReader.test.ts @@ -198,6 +198,23 @@ describe("UnityFS payloads and malformed declarations", () => { }); describe("UnityFS unavailable and malformed directories", () => { + it("reports cancellation during unavailable-directory coverage", async () => { + await withReader( + buildUnityFs(members, { flags: 0x1400 | 0x43 }), + async (reader) => { + const controller = new AbortController(); + const entries = reader + .entries(controller.signal, { + onCoverage: () => controller.abort(), + }) + [Symbol.asyncIterator](); + await expect(entries.next()).rejects.toMatchObject({ + reason: "cancelled", + }); + }, + ); + }); + it("reports an encrypted directory as unknown coverage without entries", async () => { await withReader( buildUnityFs(members, { flags: 0x1400 | 0x43 }), diff --git a/src/artifacts/UnityFsArtifactReader.ts b/src/artifacts/UnityFsArtifactReader.ts index e25069ab45..5d6ef1c023 100644 --- a/src/artifacts/UnityFsArtifactReader.ts +++ b/src/artifacts/UnityFsArtifactReader.ts @@ -13,6 +13,7 @@ import { type ArtifactTraversalOptions, } from "./ArtifactReader.js"; import { ArtifactResourceScope } from "./ArtifactResourceScope.js"; +import { ownAsyncArtifactStream } from "./OwnedAsyncArtifactStream.js"; import { assertArtifactReadActive } from "./ArtifactFileOperation.js"; import { readArtifactFileBytes } from "./ArtifactFileRange.js"; import type { @@ -108,6 +109,7 @@ export class UnityFsArtifactReader implements ArtifactReader { : "UnityFS blocks-and-directory table uses an unsupported compression; member identities remain unknown.", ], }); + this.#assertOpen(signal); return; } let index = 0; @@ -153,46 +155,17 @@ export class UnityFsArtifactReader implements ArtifactReader { { boundary: "capacity" }, ); const controller = new AbortController(); - const output = Readable.from( + const output = ownAsyncArtifactStream( this.#memberChunks(handle, state, controller.signal), + controller, { - objectMode: false, + scope: this.#scope, + streams: this.#streams, + signal, + label: "UnityFS", highWaterMark: CHUNK_BYTES, }, ); - const onAbort = (): void => { - controller.abort(signal?.reason); - output.destroy( - new ArtifactReaderFailure( - "cancelled", - "UnityFS member read cancelled", - { cause: signal?.reason }, - ), - ); - }; - output.on("error", () => undefined); - this.#streams.set(output, controller); - const completion = new Promise((resolve) => { - output.once("close", () => { - signal?.removeEventListener("abort", onAbort); - this.#streams.delete(output); - resolve(); - }); - }); - // Readable.from waits for its async iterator to retire before close. Keep - // that whole lifetime owned, including a suspended positional read/decoder. - void this.#scope - .run(() => completion) - .catch((cause: unknown) => { - controller.abort(cause); - output.destroy( - cause instanceof Error - ? cause - : new Error("UnityFS stream ownership failed", { cause }), - ); - }); - signal?.addEventListener("abort", onAbort, { once: true }); - if (signal?.aborted === true) onAbort(); return output; }); } diff --git a/src/artifacts/WxapkgArtifactReader.boundaries.test.ts b/src/artifacts/WxapkgArtifactReader.boundaries.test.ts index 62750f463c..f8f82e6ab8 100644 --- a/src/artifacts/WxapkgArtifactReader.boundaries.test.ts +++ b/src/artifacts/WxapkgArtifactReader.boundaries.test.ts @@ -156,7 +156,10 @@ describe("wxapkg allocation and malformed-input boundaries", () => { await fixture(buildWxapkg(members, { aliasOffsets: true })), ); try { - await expect(entries(reader)).rejects.toMatchObject({ reason: "limit" }); + await expect(entries(reader)).rejects.toMatchObject({ + reason: "limit", + message: expect.stringContaining("wxapkg members declare"), + }); } finally { await reader.close(); } @@ -187,6 +190,117 @@ describe("wxapkg allocation and malformed-input boundaries", () => { }); }); +describe("wxapkg pending read cancellation", () => { + it("retains final source verification failure during cancellation", async () => { + const reader = new WxapkgArtifactReader( + await fixture(buildWxapkg([{ path: "x", data: "bytes" }])), + ); + const [entry] = await entries(reader); + const reached = createDeferred(); + const release = createDeferred(); + const failure = new Error("final source verification failed"); + const controller = new AbortController(); + const read = ArtifactFileSource.prototype.read; + let count = 0; + const observer = vi + .spyOn(ArtifactFileSource.prototype, "read") + .mockImplementation(async function (this: ArtifactFileSource, signal) { + if (++count === 2) { + reached.resolve(); + await release.promise; + throw failure; + } + return read.call(this, signal); + }); + let consumed: Promise | undefined; + try { + consumed = buffer(await reader.open(entry!, controller.signal)); + await awaitAcknowledgement( + reached.promise, + consumed, + "final verification", + ); + controller.abort(new Error("caller cancelled")); + release.resolve(); + await expect(consumed).rejects.toBe(failure); + } finally { + release.resolve(); + await consumed?.catch(() => undefined); + observer.mockRestore(); + await reader.close(); + } + }); + + it("retains the member cancellation outcome after a pending read succeeds", async () => { + const path = await fixture(buildWxapkg([{ path: "x", data: "bytes" }])); + const admitted = await openRegularFile(path, { symlinks: "reject" }); + const reader = new WxapkgArtifactReader(path, admitted); + const reached = createDeferred(); + const release = createDeferred(); + const controller = new AbortController(); + const reason = new Error("caller cancelled"); + const nativeRead = admitted.handle.read.bind(admitted.handle); + const [entry] = await entries(reader); + const reads = vi.spyOn(admitted.handle, "read").mockImplementation((async ( + ...args: unknown[] + ) => { + reached.resolve(); + await release.promise; + return Reflect.apply(nativeRead, admitted.handle, args); + }) as typeof admitted.handle.read); + let consumed: Promise | undefined; + try { + consumed = buffer(await reader.open(entry!, controller.signal)); + await awaitAcknowledgement(reached.promise, consumed, "member read"); + controller.abort(reason); + release.resolve(); + await expect(consumed).rejects.toMatchObject({ + reason: "cancelled", + message: "wxapkg member read cancelled", + cause: reason, + }); + } finally { + release.resolve(); + await consumed?.catch(() => undefined); + reads.mockRestore(); + await reader.close(); + await admitted.handle.close(); + } + }); + + it("preserves a pending read failure when cancellation overlaps it", async () => { + const path = await fixture(buildWxapkg([{ path: "x", data: "bytes" }])); + const admitted = await openRegularFile(path, { symlinks: "reject" }); + const reader = new WxapkgArtifactReader(path, admitted); + const reached = createDeferred(); + const release = createDeferred(); + const failure = new Error("member read failed"); + const controller = new AbortController(); + const [entry] = await entries(reader); + const reads = vi + .spyOn(admitted.handle, "read") + .mockImplementation((async () => { + reached.resolve(); + await release.promise; + throw failure; + }) as typeof admitted.handle.read); + let consumed: Promise | undefined; + try { + consumed = buffer(await reader.open(entry!, controller.signal)); + await awaitAcknowledgement(reached.promise, consumed, "member read"); + controller.abort(new Error("caller cancelled")); + release.resolve(); + await expect(consumed).rejects.toBe(failure); + } finally { + release.resolve(); + await consumed?.catch(() => undefined); + reads.mockRestore(); + await reader.close(); + await admitted.handle.close(); + } + }); +}); + describe("wxapkg stream ownership", () => { it("retains descriptor ownership after cleanup fails and permits retry", async () => { const reader = new WxapkgArtifactReader( @@ -307,29 +421,25 @@ describe("wxapkg stream capacity and freshness", () => { } }); - it("bounds repeated member I/O operations, including empty members", async () => { + it.each([ + { path: "x", data: "b" }, + { path: "empty.wxss", data: "" }, + ])("bounds repeated I/O operations for $path", async (member) => { const reader = new WxapkgArtifactReader( - await fixture( - buildWxapkg([ - { path: "x", data: "b" }, - { path: "empty.wxss", data: "" }, - ]), - ), + await fixture(buildWxapkg([member])), ); try { - const [entry, emptyEntry] = await entries(reader); + const [entry] = await entries(reader); await expect( (async () => { for (let index = 0; index < 5000; index++) await buffer(await reader.open(entry!)); })(), - ).rejects.toMatchObject({ reason: "limit", boundary: "capacity" }); - await expect( - (async () => { - for (let index = 0; index < 5000; index++) - await buffer(await reader.open(emptyEntry!)); - })(), - ).rejects.toMatchObject({ reason: "limit", boundary: "capacity" }); + ).rejects.toMatchObject({ + reason: "limit", + boundary: "capacity", + message: expect.stringContaining("wxapkg directory-and-chunk I/O work"), + }); } finally { await reader.close(); } diff --git a/src/artifacts/WxapkgArtifactReader.test.ts b/src/artifacts/WxapkgArtifactReader.test.ts index 55dc883f73..0f50222f08 100644 --- a/src/artifacts/WxapkgArtifactReader.test.ts +++ b/src/artifacts/WxapkgArtifactReader.test.ts @@ -176,6 +176,20 @@ describe("WxapkgArtifactReader", () => { }); }); }); + + it("reports cancellation during encrypted-directory coverage", async () => { + await withReader(wxapkgEncryptedBytes(), async (reader) => { + const controller = new AbortController(); + const entries = reader + .entries(controller.signal, { + onCoverage: () => controller.abort(), + }) + [Symbol.asyncIterator](); + await expect(entries.next()).rejects.toMatchObject({ + reason: "cancelled", + }); + }); + }); }); describe("WxapkgArtifactReader malformed inputs", () => { diff --git a/src/artifacts/WxapkgArtifactReader.ts b/src/artifacts/WxapkgArtifactReader.ts index 4bc11a737f..7aeb15c8d5 100644 --- a/src/artifacts/WxapkgArtifactReader.ts +++ b/src/artifacts/WxapkgArtifactReader.ts @@ -13,6 +13,7 @@ import { type ArtifactTraversalOptions, } from "./ArtifactReader.js"; import { ArtifactResourceScope } from "./ArtifactResourceScope.js"; +import { ownAsyncArtifactStream } from "./OwnedAsyncArtifactStream.js"; import { assertArtifactReadActive } from "./ArtifactFileOperation.js"; import { readArtifactFileBytes } from "./ArtifactFileRange.js"; import type { @@ -97,6 +98,7 @@ export class WxapkgArtifactReader implements ArtifactReader { "wxapkg package is V1MMWX-encrypted; member identities and contents remain unknown.", ], }); + this.#assertOpen(signal); return; } let index = 0; @@ -131,46 +133,17 @@ export class WxapkgArtifactReader implements ArtifactReader { { boundary: "capacity" }, ); const controller = new AbortController(); - const output = Readable.from( + const output = ownAsyncArtifactStream( this.#memberChunks(handle, state, controller.signal), + controller, { - objectMode: false, + scope: this.#scope, + streams: this.#streams, + signal, + label: "wxapkg", highWaterMark: CHUNK_BYTES, }, ); - const onAbort = (): void => { - controller.abort(signal?.reason); - output.destroy( - new ArtifactReaderFailure( - "cancelled", - "wxapkg member read cancelled", - { cause: signal?.reason }, - ), - ); - }; - output.on("error", () => undefined); - this.#streams.set(output, controller); - const completion = new Promise((resolve) => { - output.once("close", () => { - signal?.removeEventListener("abort", onAbort); - this.#streams.delete(output); - resolve(); - }); - }); - // Readable.from waits for its async iterator to retire before close. Keep - // that whole lifetime owned, including a suspended positional read. - void this.#scope - .run(() => completion) - .catch((cause: unknown) => { - controller.abort(cause); - output.destroy( - cause instanceof Error - ? cause - : new Error("wxapkg stream ownership failed", { cause }), - ); - }); - signal?.addEventListener("abort", onAbort, { once: true }); - if (signal?.aborted === true) onAbort(); return output; }); } diff --git a/src/artifacts/ZipArtifactReader.test.ts b/src/artifacts/ZipArtifactReader.test.ts index 523edb25ad..aeefc7def7 100644 --- a/src/artifacts/ZipArtifactReader.test.ts +++ b/src/artifacts/ZipArtifactReader.test.ts @@ -6,6 +6,7 @@ import { } from "../../tests/fixtures/zipZstandard.js"; import { ArtifactReaderFailure, type ArtifactEntry } from "./ArtifactReader.js"; import { createDeferred } from "../../tests/fixtures/binarySession.js"; +import { awaitAcknowledgement } from "../../tests/fixtures/lifecycleAcknowledgement.js"; import { once } from "node:events"; import { randomBytes } from "node:crypto"; import { constants, zstdCompressSync } from "node:zlib"; @@ -26,12 +27,14 @@ import { createTestTempDirectory } from "../../tests/fixtures/temporaryDirectory import { ZipArtifactReader } from "./ZipArtifactReader.js"; import { openRegularFile } from "../filesystem/RegularFile.js"; -import { MINIMUM_ARTIFACT_EXPANDED_BYTES } from "../domain/artifactExpansionBudget.js"; import { OwnedFileHandle } from "../filesystem/OwnedFileHandle.js"; import { hashReadable } from "./ArtifactHash.js"; import { translateArtifactFailure } from "./ArtifactOperationFailure.js"; + import { analysisErrorUserMessage } from "../domain/analysisErrorPresentation.js"; +const MINIMUM_ARTIFACT_EXPANDED_BYTES = 256 * 1024 * 1024; + /** Small real ZIP carriers with forged sizes; metadata-only controls, never decompressed. */ const writeZipWithDeclaredSize = async ( directory: string, @@ -754,7 +757,11 @@ describe("ZipArtifactReader producer ownership", () => { for await (const _chunk of stream) break; } const closing = reader.close(); - expect(reader.close()).toBe(closing); + const secondClose = reader.close(); + let secondClosed = false; + void secondClose.then(() => { + secondClosed = true; + }); await held.reached; expect(stream.destroyed).toBe(true); expect(fileClose).not.toHaveBeenCalled(); @@ -764,8 +771,9 @@ describe("ZipArtifactReader producer ownership", () => { }); await Promise.resolve(); expect(closed).toBe(false); + expect(secondClosed).toBe(false); held.release(); - await closing; + await Promise.all([closing, secondClose]); expect(fileClose).toHaveBeenCalledTimes(1); await reader.close(); expect(fileClose).toHaveBeenCalledTimes(1); @@ -829,7 +837,7 @@ it("joins an admitted ZIP enumeration before reader close completes", async () = const traversal = reader.entries()[Symbol.asyncIterator](); const next = traversal.next(); try { - await reached.promise; + await awaitAcknowledgement(reached.promise, next, "ZIP enumeration read"); let closed = false; const closing = reader.close().then(() => { closed = true; diff --git a/src/artifacts/ZipArtifactReader.ts b/src/artifacts/ZipArtifactReader.ts index 791da08573..b786adbb94 100644 --- a/src/artifacts/ZipArtifactReader.ts +++ b/src/artifacts/ZipArtifactReader.ts @@ -246,7 +246,9 @@ export class ZipArtifactReader implements ArtifactReader { }); yield metadata; } + this.#assertOpen(signal); await this.#source.verify(); + this.#assertOpen(signal); } finally { this.#enumerations.delete(enumeration); } diff --git a/src/artifacts/apple/AppleAssetCatalogAnalysis.test.ts b/src/artifacts/apple/AppleAssetCatalogAnalysis.test.ts index 8e64174828..d1643aaac3 100644 --- a/src/artifacts/apple/AppleAssetCatalogAnalysis.test.ts +++ b/src/artifacts/apple/AppleAssetCatalogAnalysis.test.ts @@ -366,7 +366,7 @@ describe("Apple asset catalog and Interface Builder concurrency", () => { }, }); expect(result.catalogs).toHaveLength(1); - }, 5_000); + }); it("cancels the Interface Builder analysis and reports a catalog failure", async () => { let cancelled = false; diff --git a/src/artifacts/apple/KeyedArchiveRawRefusal.test.ts b/src/artifacts/apple/KeyedArchiveRawRefusal.test.ts index 460c2734eb..8ab88e3942 100644 --- a/src/artifacts/apple/KeyedArchiveRawRefusal.test.ts +++ b/src/artifacts/apple/KeyedArchiveRawRefusal.test.ts @@ -9,7 +9,6 @@ import { AnalysisAccessDeniedError } from "../../domain/analysisErrorCore.js"; import { projectAnalysisError } from "../../domain/analysisErrorProjection.js"; import { ArtifactReaderFailure } from "../ArtifactReader.js"; import { translateArtifactFailure } from "../ArtifactOperationFailure.js"; -import { DirectoryArtifactEntryAccessDenied } from "../DirectoryArtifactReader.js"; import { inspectBundleKeyedArchive } from "./KeyedArchiveReader.js"; import { createTestTempDirectory } from "../../../tests/fixtures/temporaryDirectory.js"; diff --git a/src/artifacts/apple/MachoLoadCommandReader.test.ts b/src/artifacts/apple/MachoLoadCommandReader.test.ts index 1b08333f78..d18cbdd47e 100644 --- a/src/artifacts/apple/MachoLoadCommandReader.test.ts +++ b/src/artifacts/apple/MachoLoadCommandReader.test.ts @@ -24,8 +24,6 @@ import { versionMinCommand, } from "./MachoImage.fixture.js"; import { - MAX_LAZY_INFO_DEPENDENCIES, - MAX_LAZY_INFO_SYMBOL_REFERENCES, MAX_LOAD_COMMAND_BYTES, readMachoImage, } from "./MachoLoadCommandReader.js"; @@ -35,6 +33,10 @@ import { DirectoryArtifactReader } from "../DirectoryArtifactReader.js"; import { createTestTempDirectory } from "../../../tests/fixtures/temporaryDirectory.js"; import { javaClassFile } from "../../../tests/fixtures/javaClassFile.js"; +// Independently exercise the dependency and reference budgets. +const MAX_LAZY_INFO_DEPENDENCIES = 1024; +const MAX_LAZY_INFO_SYMBOL_REFERENCES = 64 * 1024; + const read = (bytes: Uint8Array) => readMachoImage(readerOf(bytes), bytes.length); diff --git a/src/artifacts/apple/MachoLoadCommandReader.ts b/src/artifacts/apple/MachoLoadCommandReader.ts index e36ebd4030..1d0ee2c744 100644 --- a/src/artifacts/apple/MachoLoadCommandReader.ts +++ b/src/artifacts/apple/MachoLoadCommandReader.ts @@ -19,10 +19,7 @@ import type { MachoImageFacts, MachoSlice, } from "../../domain/apple/dylibResolution.js"; -import { - machoArchitectureName, - machoCpuSubtypeBase, -} from "../../domain/apple/machoCpuIdentity.js"; +import { machoArchitectureName } from "../../domain/apple/machoCpuIdentity.js"; import { legacyMachoPlatform, type MachoVersionMinPlatform, @@ -52,13 +49,13 @@ export const MAX_LOAD_COMMAND_BYTES = 16 * 1024 * 1024; * byte cap alone would retain about a million dependency results. Stop before * those results are kept or traced. */ -export const MAX_LAZY_INFO_DEPENDENCIES = 1024; +const MAX_LAZY_INFO_DEPENDENCIES = 1024; /** * Symbol names are validated and then discarded. Distinct one-byte strings * stay under the 16 MiB byte budgets while retaining one cache entry per * offset, so stop before that map and those decodes grow. */ -export const MAX_LAZY_INFO_SYMBOL_REFERENCES = 64 * 1024; +const MAX_LAZY_INFO_SYMBOL_REFERENCES = 64 * 1024; const LC_SEGMENT = 0x1; const LC_SEGMENT_64 = 0x19; diff --git a/src/artifacts/bun/BunStandaloneArtifactReader.test.ts b/src/artifacts/bun/BunStandaloneArtifactReader.test.ts index 1914901cd0..12fdd878ee 100644 --- a/src/artifacts/bun/BunStandaloneArtifactReader.test.ts +++ b/src/artifacts/bun/BunStandaloneArtifactReader.test.ts @@ -1,9 +1,11 @@ import { open, writeFile, type FileHandle } from "node:fs/promises"; +import { once } from "node:events"; import { createHash } from "node:crypto"; import { join } from "node:path"; import { buffer } from "node:stream/consumers"; import { expect, it, vi } from "vitest"; import { createTestTempDirectory } from "../../../tests/fixtures/temporaryDirectory.js"; +import { awaitAcknowledgement } from "../../../tests/fixtures/lifecycleAcknowledgement.js"; import * as artifactPaths from "../ArtifactPaths.js"; import { BunStandaloneArtifactReader } from "../BunStandaloneArtifactReader.js"; import { inventoryArtifact } from "../inventory/ArtifactInventory.js"; @@ -17,6 +19,34 @@ import { elfAppendedBunPayload, } from "./BunStandalone.fixture.js"; +it.each(["close", "cancel"] as const)( + "does not report a complete inventory after %s while iteration is paused", + async (interruption) => { + const path = join(await createTestTempDirectory("rea-bun-paused-"), "app"); + await writeFile( + path, + elfAppendedBunPayload( + bunModuleGraphPayload({ + modules: [{ name: "main.js", contents: Buffer.from("body") }], + }), + ), + ); + const reader = new BunStandaloneArtifactReader(path); + const controller = new AbortController(); + try { + const entries = reader.entries(controller.signal)[Symbol.asyncIterator](); + expect((await entries.next()).done).toBe(false); + if (interruption === "close") await reader.close(); + else controller.abort(); + await expect(entries.next()).rejects.toMatchObject({ + reason: interruption === "close" ? "unavailable" : "cancelled", + }); + } finally { + await reader.close(); + } + }, +); + it("keeps duplicate and unsafe names distinct while extracting exact bytes", async () => { const path = join(await createTestTempDirectory("rea-bun-reader-"), "app"); await writeFile( @@ -130,6 +160,35 @@ async function collect(values: AsyncIterable): Promise { return result; } +it("cancels buffered member bytes after production finishes", async () => { + const path = join(await createTestTempDirectory("rea-bun-buffered-"), "app"); + await writeFile( + path, + elfAppendedBunPayload( + bunModuleGraphPayload({ + modules: [{ name: "main.js", contents: Buffer.from("buffered") }], + }), + ), + ); + const reader = new BunStandaloneArtifactReader(path); + const controller = new AbortController(); + const reason = new Error("cancel buffered Bun member"); + try { + const [entry] = await collect(reader.entries()); + if (entry === undefined) throw new Error("Expected Bun member"); + const stream = await reader.open(entry, controller.signal); + await once(stream, "finish"); + expect(stream.readableLength).toBeGreaterThan(0); + controller.abort(reason); + await expect(buffer(stream)).rejects.toMatchObject({ + reason: "cancelled", + cause: reason, + }); + } finally { + await reader.close(); + } +}); + it("allows an uncancelled inventory after a cancelled graph preparation", async () => { const path = join( await createTestTempDirectory("rea-bun-cancel-retry-"), @@ -226,7 +285,7 @@ it("keeps another caller's in-flight inventory independent of cancelled preparat }, (cause: unknown) => cause, ); - await started; + await awaitAcknowledgement(started, first, "Bun artifact read"); const second = collect(reader.entries()); controller.abort(reason); resume(); diff --git a/src/artifacts/deno/DenoSectionLayout.test.ts b/src/artifacts/deno/DenoSectionLayout.test.ts index 8c7d4614b8..eb8a49ee63 100644 --- a/src/artifacts/deno/DenoSectionLayout.test.ts +++ b/src/artifacts/deno/DenoSectionLayout.test.ts @@ -1,10 +1,6 @@ import { expect, it } from "vitest"; -import { - MAX_DENO_METADATA_JSON_BYTES, - MAX_DENO_TABLE_COUNT, - parseDenoSection, -} from "./DenoSectionLayout.js"; +import { parseDenoSection } from "./DenoSectionLayout.js"; import { buildDenoSection, defaultDenoMetadata, @@ -130,7 +126,7 @@ it("treats a missing opening marker as an unsupported layout", async () => { }); it("refuses metadata JSON above the named allowance before reading it as JSON", async () => { - const claimed = MAX_DENO_METADATA_JSON_BYTES + 1; + const claimed = 8 * 1024 * 1024 + 1; const bytes = Buffer.alloc(DENO_FIXTURE_SECTION_MAGIC.length + 8 + claimed); DENO_FIXTURE_SECTION_MAGIC.copy(bytes, 0); bytes.writeBigUInt64LE(BigInt(claimed), DENO_FIXTURE_SECTION_MAGIC.length); @@ -145,7 +141,7 @@ it("refuses a specifier table above the named record allowance", async () => { parseDenoSectionBytes( buildDenoSection({ specifiers: [], - specifierCount: MAX_DENO_TABLE_COUNT + 1, + specifierCount: 100_001, }), ), ).rejects.toMatchObject({ diff --git a/src/artifacts/deno/DenoSectionLayout.ts b/src/artifacts/deno/DenoSectionLayout.ts index 93d2a7b89c..6bd659091c 100644 --- a/src/artifacts/deno/DenoSectionLayout.ts +++ b/src/artifacts/deno/DenoSectionLayout.ts @@ -9,9 +9,9 @@ import { walkDenoVirtualFs, type DenoVfsWalk } from "./DenoVirtualFs.js"; /** Opening and closing marker of a Deno compile data section. */ export const DENO_SECTION_MAGIC = Buffer.from("d3n0l4nd", "latin1"); -export const MAX_DENO_METADATA_JSON_BYTES = 8 * 1024 * 1024; +const MAX_DENO_METADATA_JSON_BYTES = 8 * 1024 * 1024; export const MAX_DENO_VFS_HEADER_JSON_BYTES = 32 * 1024 * 1024; -export const MAX_DENO_TABLE_COUNT = 100_000; +const MAX_DENO_TABLE_COUNT = 100_000; export const MAX_DENO_SPECIFIER_TEXT_BYTES = 8 * 1024 * 1024; export const MAX_DENO_REMOTE_BODY_BYTES = 256 * 1024 * 1024; diff --git a/src/artifacts/deno/DenoStandaloneArtifactReader.test.ts b/src/artifacts/deno/DenoStandaloneArtifactReader.test.ts index b363fac09d..eb79d760b6 100644 --- a/src/artifacts/deno/DenoStandaloneArtifactReader.test.ts +++ b/src/artifacts/deno/DenoStandaloneArtifactReader.test.ts @@ -5,6 +5,7 @@ import { open, writeFile } from "node:fs/promises"; import { join } from "node:path"; import { expect, it, vi } from "vitest"; +import { awaitAcknowledgement } from "../../../tests/fixtures/lifecycleAcknowledgement.js"; import { elfNote, elfNoteFixture } from "../../native/elf/ElfNotes.fixture.js"; import { @@ -19,10 +20,7 @@ import { artifactFormatDescriptor } from "../artifactFormats.js"; import { ArtifactPathRegistry } from "../ArtifactPaths.js"; import { DenoStandaloneArtifactReader } from "../DenoStandaloneArtifactReader.js"; import { cArchiveExecutable } from "../pyinstaller/CArchive.fixture.js"; -import { - locateDenoStandalone, - MAX_DENO_INTEL_TRAILER_SENTINELS, -} from "./DenoStandaloneLocation.js"; +import { locateDenoStandalone } from "./DenoStandaloneLocation.js"; import { buildDenoSection, DENO_FIXTURE_SECTION_MAGIC, @@ -34,9 +32,11 @@ import { denoUniversal, machOWithSegment, } from "./DenoStandalone.fixture.js"; -import { MAX_DENO_PATH_COMPONENT_BYTES } from "./DenoVirtualFs.js"; + import { resolveArtifactReaderLimits } from "../../domain/artifactReaderLimits.js"; +const MAX_DENO_PATH_COMPONENT_BYTES = 255; + it("keeps a confirmed Deno inventory when another pack recognizer is limited", async () => { const original = denoElf(); // Add a valid empty section table without changing the existing PT_NOTE. @@ -1071,7 +1071,7 @@ it("rejects packed Intel sentinels without rereading a chunk per sentinel", asyn it("leaves an x64 Mach-O absent when sentinels pass the allowance", async () => { const packed = Buffer.concat([ denoMachO([], { architecture: "x64" }), - rejectedIntelSentinels(MAX_DENO_INTEL_TRAILER_SENTINELS + 1), + rejectedIntelSentinels(65), ]); let readBytes = 0; const probe = await locateDenoStandalone( @@ -1307,7 +1307,7 @@ it("accepts side-by-side Intel trailers with identical bytes", async () => { it("refuses a confirmed Intel trailer when the allowance ends the outer search", async () => { const bytes = Buffer.concat([ denoMachO([], { architecture: "x64" }), - rejectedIntelSentinels(MAX_DENO_INTEL_TRAILER_SENTINELS), + rejectedIntelSentinels(64), intelTrailerRecord(buildDenoSection()), ]); await expect( @@ -1876,7 +1876,7 @@ it.each(["retry", "independent caller", "physical failure"] as const)( (cause: unknown) => cause, ); pending.push(failure); - await entered; + await awaitAcknowledgement(entered, first, "Deno artifact read"); const concurrent = scenario === "independent caller" ? collect(reader.entries()) diff --git a/src/artifacts/deno/DenoStandaloneLocation.ts b/src/artifacts/deno/DenoStandaloneLocation.ts index f68f409e94..372b5dba20 100644 --- a/src/artifacts/deno/DenoStandaloneLocation.ts +++ b/src/artifacts/deno/DenoStandaloneLocation.ts @@ -64,7 +64,7 @@ const ELF_NOTE_TYPE = 0x53554901; const PE_HEADER_PROBE_BYTES = 64 * 1024; const INTEL_CHUNK_BYTES = 1024 * 1024; /** Trailer candidates examined before the search stops. */ -export const MAX_DENO_INTEL_TRAILER_SENTINELS = 64; +const MAX_DENO_INTEL_TRAILER_SENTINELS = 64; const INTEL_SENTINEL = Buffer.concat([ Buffer.from("<~sui-data~>", "latin1"), Buffer.from([0xef, 0xbe, 0xad, 0xde]), diff --git a/src/artifacts/deno/DenoVirtualFs.test.ts b/src/artifacts/deno/DenoVirtualFs.test.ts index e6668bcd14..a5d4d6edec 100644 --- a/src/artifacts/deno/DenoVirtualFs.test.ts +++ b/src/artifacts/deno/DenoVirtualFs.test.ts @@ -2,15 +2,17 @@ import { expect, it } from "vitest"; import { ArtifactReaderFailure } from "../ArtifactReader.js"; import { - MAX_DENO_LOGICAL_PATH_BYTES, MAX_DENO_LOGICAL_PATH_PARTS, - MAX_DENO_PATH_COMPONENT_BYTES, - MAX_DENO_VFS_DEPTH, - MAX_DENO_VFS_ENTRIES, - MAX_DENO_VFS_NAME_UNITS, walkDenoVirtualFs, } from "./DenoVirtualFs.js"; +// Independent format and admission examples. +const MAX_DENO_VFS_ENTRIES = 100_000; +const MAX_DENO_VFS_NAME_UNITS = 4_096; +const MAX_DENO_VFS_DEPTH = 512; +const MAX_DENO_PATH_COMPONENT_BYTES = 255; +const MAX_DENO_LOGICAL_PATH_BYTES = 512; + const file = ( name: string, offset: number, diff --git a/src/artifacts/deno/DenoVirtualFs.ts b/src/artifacts/deno/DenoVirtualFs.ts index d0d26e23e8..f678f30c7a 100644 --- a/src/artifacts/deno/DenoVirtualFs.ts +++ b/src/artifacts/deno/DenoVirtualFs.ts @@ -4,32 +4,32 @@ import { ArtifactReaderFailure } from "../ArtifactReader.js"; import { normalizeArtifactPath } from "../ArtifactPaths.js"; /** Directory, file, and symlink records admitted from one virtual filesystem. */ -export const MAX_DENO_VFS_ENTRIES = 100_000; +const MAX_DENO_VFS_ENTRIES = 100_000; /** Logical path parts admitted before an indexed fallback. */ export const MAX_DENO_LOGICAL_PATH_PARTS = 256; /** * UTF-16 code units retained for one virtual name. Longer names are indexed * and the original spelling is not copied into members. */ -export const MAX_DENO_VFS_NAME_UNITS = 4_096; +const MAX_DENO_VFS_NAME_UNITS = 4_096; /** * Nested `walkEntries` calls. This is the recursion budget, separate from * the logical-path part fallback. */ -export const MAX_DENO_VFS_DEPTH = 512; +const MAX_DENO_VFS_DEPTH = 512; /** * UTF-16 code units of the source path built for each entry, directories * included, plus retained symlink parts. Each path is rebuilt and normalized * once, so this also bounds the synchronous walk. */ -export const MAX_DENO_VFS_RETAINED_UNITS = 32 * 1024 * 1024; +const MAX_DENO_VFS_RETAINED_UNITS = 32 * 1024 * 1024; /** POSIX NAME_MAX. A longer component makes extraction fail with ENAMETOOLONG. */ -export const MAX_DENO_PATH_COMPONENT_BYTES = 255; +const MAX_DENO_PATH_COMPONENT_BYTES = 255; /** * Natural logical path bytes. Half of the 1,024-byte Darwin PATH_MAX, leaving * the rest for the output root, so a long path does not abort extraction. */ -export const MAX_DENO_LOGICAL_PATH_BYTES = 512; +const MAX_DENO_LOGICAL_PATH_BYTES = 512; /** Logical path suffixes of the regions stored beside an original file. */ export const DENO_CHILD_SUFFIXES = { transpiled: ".deno-transpiled", diff --git a/src/artifacts/extraction/EmbeddedArtifactExtraction.test.ts b/src/artifacts/extraction/EmbeddedArtifactExtraction.test.ts index 8742984684..291b86f788 100644 --- a/src/artifacts/extraction/EmbeddedArtifactExtraction.test.ts +++ b/src/artifacts/extraction/EmbeddedArtifactExtraction.test.ts @@ -106,10 +106,13 @@ it.each(cases)( ).toHaveLength(2); expect(scope.hasPending).toBe(false); } finally { - await scope.close(); - observation.mockRestore(); - close.mockRestore(); - release.mockRestore(); + try { + await scope.close(); + } finally { + observation.mockRestore(); + close.mockRestore(); + release.mockRestore(); + } } }, ); @@ -146,8 +149,11 @@ it.each(cases)( await expect(stat(output)).rejects.toMatchObject({ code: "ENOENT" }); expect(scope.hasPending).toBe(false); } finally { - await scope.close(); - close.mockRestore(); + try { + await scope.close(); + } finally { + close.mockRestore(); + } } }, ); @@ -295,8 +301,11 @@ it.each(cases)( expect(closes).toBe(2); expect(scope.hasPending).toBe(false); } finally { - await scope.close(); - close.mockRestore(); + try { + await scope.close(); + } finally { + close.mockRestore(); + } } }, ); @@ -378,8 +387,11 @@ it.each( } expect(scope.hasPending).toBe(false); } finally { - await scope.close(); - close.mockRestore(); + try { + await scope.close(); + } finally { + close.mockRestore(); + } } }, ); diff --git a/src/artifacts/fit/FitStructure.test.ts b/src/artifacts/fit/FitStructure.test.ts index 9024f3b111..8792258e03 100644 --- a/src/artifacts/fit/FitStructure.test.ts +++ b/src/artifacts/fit/FitStructure.test.ts @@ -12,13 +12,7 @@ import { fitFixture, type FitFixtureImage, } from "./FitImage.fixture.js"; -import { - FIT_MAX_CONFIGURATION_REFERENCES, - FIT_MAX_CONFIGURATIONS, - FIT_MAX_HASH_NODES_PER_IMAGE, - FIT_MAX_IMAGES, - readFitStructure, -} from "./FitStructure.js"; +import { readFitStructure } from "./FitStructure.js"; const read = (file: Buffer) => readFitStructure( @@ -487,75 +481,74 @@ describe("FIT configurations", () => { describe("FIT fixed limits", () => { it("fails more images than the fixed allowance", () => { - expect( - failureOf( - fitFixture({ - images: Array.from({ length: FIT_MAX_IMAGES + 1 }, (_, index) => ({ - name: `image-${String(index)}`, - data: Buffer.from([index & 0xff]), - })), - }), - ), - ).toMatchObject({ reason: "limit", boundary: "capacity" }); + const make = (count: number) => + fitFixture({ + images: Array.from({ length: count }, (_, index) => ({ + name: `image-${String(index)}`, + data: Buffer.from([index & 0xff]), + })), + }); + expect(read(make(1024)).images).toHaveLength(1024); + expect(failureOf(make(1025))).toMatchObject({ + reason: "limit", + boundary: "capacity", + }); }); it("fails more hash nodes than the fixed allowance", () => { - expect( - failureOf( - fitFixture({ - images: [ - { - ...kernel(), - hashes: Array.from( - { length: FIT_MAX_HASH_NODES_PER_IMAGE + 1 }, - () => "crc32", - ), - }, - ], - }), - ), - ).toMatchObject({ reason: "limit", boundary: "capacity" }); + const make = (count: number) => + fitFixture({ + images: [ + { + ...kernel(), + hashes: Array.from({ length: count }, () => "crc32"), + }, + ], + }); + expect(read(make(8)).images[0]?.hashes).toHaveLength(8); + expect(failureOf(make(9))).toMatchObject({ + reason: "limit", + boundary: "capacity", + }); }); it("fails more configurations than the fixed allowance", () => { - expect( - failureOf( - fitFixture({ - images: [kernel()], - configurations: { - name: "configurations", - children: Array.from( - { length: FIT_MAX_CONFIGURATIONS + 1 }, - (_, index) => ({ name: `conf-${String(index)}` }), - ), - }, - }), - ), - ).toMatchObject({ reason: "limit", boundary: "capacity" }); + const make = (count: number) => + fitFixture({ + images: [kernel()], + configurations: { + name: "configurations", + children: Array.from({ length: count }, (_, index) => ({ + name: `conf-${String(index)}`, + })), + }, + }); + expect(read(make(1024)).metadata.configurations).toHaveLength(1024); + expect(failureOf(make(1025))).toMatchObject({ + reason: "limit", + boundary: "capacity", + }); }); it("bounds configuration references before decoding them", () => { - expect( - failureOf( - fitFixture({ - images: [kernel()], - configurations: { - name: "configurations", - children: [ - { - name: "conf-1", - properties: { - loadables: Buffer.alloc(FIT_MAX_CONFIGURATION_REFERENCES + 1), - }, - }, - ], - }, - }), - ), - ).toMatchObject({ reason: "limit", boundary: "capacity" }); + const make = (count: number) => + fitFixture({ + images: [kernel()], + configurations: { + name: "configurations", + children: [ + { name: "conf-1", properties: { loadables: Buffer.alloc(count) } }, + ], + }, + }); + expect(read(make(16_384)).metadata.configurations).toHaveLength(1); + expect(failureOf(make(16_385))).toMatchObject({ + reason: "limit", + boundary: "capacity", + }); }); - it("maps a parser limit to a fixed allowance without a caller limit", () => { + it("does not attach caller-adjustable admission to a fixed parser limit", () => { let nested: FixtureNode = { name: "leaf" }; for (let depth = 0; depth < 64; depth += 1) nested = { name: `n${String(depth)}`, children: [nested] }; @@ -565,9 +558,12 @@ describe("FIT fixed limits", () => { children: [{ name: "images" }, nested], }), ); - expect(failure).toMatchObject({ reason: "limit", boundary: "capacity" }); - // The parser's caller-limit recovery does not apply to fixed FIT reads. - expect(String((failure as Error).message)).not.toContain("caller-selected"); + expect(failure).toMatchObject({ + reason: "limit", + boundary: "capacity", + readerLimit: undefined, + admission: undefined, + }); }); it("fails a structure without /images", () => { @@ -580,10 +576,7 @@ describe("FIT fixed limits", () => { it("bounds dangling-reference diagnostics while preserving every declared reference", () => { const name = "c".repeat(4096); - const references = Array.from( - { length: FIT_MAX_CONFIGURATION_REFERENCES }, - () => "missing", - ); + const references = Array.from({ length: 16_384 }, () => "missing"); const file = fitFixture({ images: [{ name: "kernel", data: Buffer.from("K") }], configurations: { diff --git a/src/artifacts/fit/FitStructure.ts b/src/artifacts/fit/FitStructure.ts index c8b18f8e1f..06f7e44d20 100644 --- a/src/artifacts/fit/FitStructure.ts +++ b/src/artifacts/fit/FitStructure.ts @@ -18,11 +18,11 @@ import { BinaryFormatFailure } from "../../native/metadata/BinaryContainer.js"; import { ArtifactReaderFailure } from "../ArtifactReader.js"; // Fixed resource bounds; configurable ceilings are tracked in #6176. -export const FIT_MAX_IMAGES = 1024; -export const FIT_MAX_HASH_NODES_PER_IMAGE = 8; -export const FIT_MAX_CONFIGURATIONS = 1024; +const FIT_MAX_IMAGES = 1024; +const FIT_MAX_HASH_NODES_PER_IMAGE = 8; +const FIT_MAX_CONFIGURATIONS = 1024; /** Bounds configuration references and their limitations across the FIT. */ -export const FIT_MAX_CONFIGURATION_REFERENCES = 16_384; +const FIT_MAX_CONFIGURATION_REFERENCES = 16_384; type FitNode = Devicetree["nodes"][number]; type FitProperty = FitNode["properties"][number]; diff --git a/src/artifacts/inventory/ArtifactInventoryFinalization.test.ts b/src/artifacts/inventory/ArtifactInventoryFinalization.test.ts index 6f5da4a287..4a33afa234 100644 --- a/src/artifacts/inventory/ArtifactInventoryFinalization.test.ts +++ b/src/artifacts/inventory/ArtifactInventoryFinalization.test.ts @@ -8,6 +8,7 @@ import { ArtifactProvider } from "../ArtifactProvider.js"; import { ArtifactReaderFailure } from "../ArtifactReader.js"; import { ArtifactResourceScope } from "../ArtifactResourceScope.js"; import { ZipArtifactReader } from "../ZipArtifactReader.js"; +import { DirectoryArtifactReader } from "../DirectoryArtifactReader.js"; import { OwnedFileHandle } from "../../filesystem/OwnedFileHandle.js"; import { projectAnalysisError } from "../../domain/analysisErrorProjection.js"; import { @@ -51,6 +52,41 @@ const zipFixture = async () => { return { path, sha256: createHash("sha256").update(bytes).digest("hex") }; }; +it("does not complete a directory inventory after cancellation at the last entry", async () => { + const root = await createTestTempDirectory("rea-directory-paused-"); + await writeFile(join(root, "member"), "data"); + const reader = new DirectoryArtifactReader(root); + const controller = new AbortController(); + try { + const entries = reader.entries(controller.signal)[Symbol.asyncIterator](); + expect((await entries.next()).done).toBe(false); + controller.abort(); + await expect(entries.next()).rejects.toMatchObject({ reason: "cancelled" }); + } finally { + await reader.close(); + } +}); + +it.each(["close", "cancel"] as const)( + "does not complete a ZIP inventory after %s at the last entry", + async (interruption) => { + const { path } = await zipFixture(); + const reader = new ZipArtifactReader(path, "zip"); + const controller = new AbortController(); + try { + const entries = reader.entries(controller.signal)[Symbol.asyncIterator](); + expect((await entries.next()).done).toBe(false); + if (interruption === "close") await reader.close(); + else controller.abort(); + await expect(entries.next()).rejects.toMatchObject({ + reason: interruption === "close" ? "unavailable" : "cancelled", + }); + } finally { + await reader.close(); + } + }, +); + it.each([ { transfer: false, readerFails: false, rootFails: false, cancelAt: "reader" }, { transfer: false, readerFails: false, rootFails: false, cancelAt: "root" }, diff --git a/src/artifacts/inventory/classify.ts b/src/artifacts/inventory/classify.ts index 35af41535a..15bfa22513 100644 --- a/src/artifacts/inventory/classify.ts +++ b/src/artifacts/inventory/classify.ts @@ -3,12 +3,10 @@ import { nesContainerFormat, parseInesLayout, } from "../../domain/inesContainer.js"; -import type { GodotPckAdmission } from "../GodotPckLocation.js"; import { resolveArtifactReaderLimits, type ArtifactReaderLimits, } from "../../domain/artifactReaderLimits.js"; -import type { PyInstallerAdmission } from "../pyinstaller/PyInstallerAdmission.js"; import type { ArtifactReaderAdmission, ArtifactFormatAdmission, diff --git a/src/artifacts/nodeSea/NodeSea.fixture.ts b/src/artifacts/nodeSea/NodeSea.fixture.ts index d21990d99b..79300bb06f 100644 --- a/src/artifacts/nodeSea/NodeSea.fixture.ts +++ b/src/artifacts/nodeSea/NodeSea.fixture.ts @@ -81,9 +81,7 @@ export const buildNodeSeaBlob = ( return Buffer.concat(parts); }; -export const nodeSeaFuseBytes = ( - state: NodeSeaFuseFixture = "absent", -): Buffer => { +const nodeSeaFuseBytes = (state: NodeSeaFuseFixture = "absent"): Buffer => { if (state === "absent") return Buffer.alloc(0); const sentinel = (digit: string, hash = NODE_SEA_FUSE_HASH): Buffer => Buffer.from(`\0NODE_SEA_FUSE_${hash}:${digit}\0`, "latin1"); diff --git a/src/artifacts/pyinstaller/MarshalSubset.ts b/src/artifacts/pyinstaller/MarshalSubset.ts index ad3584eef1..864ae7799f 100644 --- a/src/artifacts/pyinstaller/MarshalSubset.ts +++ b/src/artifacts/pyinstaller/MarshalSubset.ts @@ -192,9 +192,7 @@ const readByteString = ( ): Result => { const length = readInt32(cursor); if (!length.ok) return length; - const bytes = readBytes(cursor, length.value); - if (!bytes.ok) return bytes; - return ok(bytes.value); + return readBytes(cursor, length.value); }; const readLong = ( diff --git a/src/artifacts/pyinstaller/PyInstallerAdmissionBoundaries.test.ts b/src/artifacts/pyinstaller/PyInstallerAdmissionBoundaries.test.ts index 4242b390f8..2fc7a90a6e 100644 --- a/src/artifacts/pyinstaller/PyInstallerAdmissionBoundaries.test.ts +++ b/src/artifacts/pyinstaller/PyInstallerAdmissionBoundaries.test.ts @@ -1,6 +1,7 @@ import type { FileHandle } from "node:fs/promises"; import { expect, it, vi } from "vitest"; import { withPyInstallerReader } from "../../../tests/fixtures/pyinstallerReader.js"; +import { awaitAcknowledgement } from "../../../tests/fixtures/lifecycleAcknowledgement.js"; import { OwnedFileHandle } from "../../filesystem/OwnedFileHandle.js"; import type { ArtifactEntry } from "../ArtifactReader.js"; import { PyInstallerPyzStore } from "./PyInstallerPyzStore.js"; @@ -114,7 +115,11 @@ it.each([false, true])( const opening = reader.open(selected, signal.signal); void opening.catch(() => undefined); try { - await started; + await awaitAcknowledgement( + started, + opening, + "PyInstaller image admission", + ); signal.abort(cancelled); release(); await expect(opening).rejects.toMatchObject({ diff --git a/src/artifacts/pyinstaller/PyInstallerArtifactReader.test.ts b/src/artifacts/pyinstaller/PyInstallerArtifactReader.test.ts index f73b3584c5..796e8f465d 100644 --- a/src/artifacts/pyinstaller/PyInstallerArtifactReader.test.ts +++ b/src/artifacts/pyinstaller/PyInstallerArtifactReader.test.ts @@ -30,6 +30,41 @@ const sha256 = (bytes: Buffer): string => const PYTHON_313_MAGIC = Buffer.from("f30d0d0a", "hex"); +it.each(["close", "cancel"] as const)( + "does not complete a PyInstaller inventory after %s while paused", + async (interruption) => { + const path = join(await createTestTempDirectory("rea-pyi-paused-"), "app"); + await writeFile( + path, + cArchiveExecutable({ + layout: "2.1", + pythonVersion: 311, + members: [ + { + name: "data", + typeCode: "x", + payload: Buffer.from("x"), + compress: false, + }, + ], + }), + ); + const reader = new PyInstallerArtifactReader(path); + const controller = new AbortController(); + try { + const entries = reader.entries(controller.signal)[Symbol.asyncIterator](); + expect((await entries.next()).done).toBe(false); + if (interruption === "close") await reader.close(); + else controller.abort(); + await expect(entries.next()).rejects.toMatchObject({ + reason: interruption === "close" ? "unavailable" : "cancelled", + }); + } finally { + await reader.close(); + } + }, +); + const derived = (payload: Buffer): Buffer => Buffer.concat([reconstructedPycHeader(PYTHON_39_MAGIC), payload]); diff --git a/src/artifacts/pyinstaller/PyInstallerBoundaries.test.ts b/src/artifacts/pyinstaller/PyInstallerBoundaries.test.ts index 524ca9bcb5..c807d3d294 100644 --- a/src/artifacts/pyinstaller/PyInstallerBoundaries.test.ts +++ b/src/artifacts/pyinstaller/PyInstallerBoundaries.test.ts @@ -1,4 +1,5 @@ import { withPyInstallerReader } from "../../../tests/fixtures/pyinstallerReader.js"; +import { awaitAcknowledgement } from "../../../tests/fixtures/lifecycleAcknowledgement.js"; import { dirname, join } from "node:path"; import { createTestTempDirectory } from "../../../tests/fixtures/temporaryDirectory.js"; import { createHash, randomBytes } from "node:crypto"; @@ -392,7 +393,11 @@ it.each([false, true])( void consumed.catch(() => undefined); let closing: Promise | undefined; try { - await entered.promise; + await awaitAcknowledgement( + entered.promise, + consumed, + "PyInstaller operation", + ); controller.abort(caller); let closed = false; closing = reader.close().then(() => { @@ -616,7 +621,11 @@ it("joins a late private-file close and retries its failed owner after cancellat .next() .catch((cause: unknown) => cause); try { - await entered.promise; + await awaitAcknowledgement( + entered.promise, + listing, + "PyInstaller operation", + ); let settled = false; const closing = reader.close().then(() => { settled = true; @@ -690,7 +699,11 @@ for (const cancel of [false, true]) { (cause: unknown) => ({ ok: false as const, cause }), ); try { - await entered.promise; + await awaitAcknowledgement( + entered.promise, + reading, + "PyInstaller operation", + ); expect(output.readableEnded).toBe(false); expect(owner?.handle.fd).toBeGreaterThanOrEqual(0); if (cancel) { @@ -825,7 +838,11 @@ it.each([false, true])( .catch((cause: unknown) => cause); let closing: Promise | undefined; try { - await entered.promise; + await awaitAcknowledgement( + entered.promise, + first, + "PyInstaller operation", + ); await setImmediate(); expect(handles.size).toBe(1); caller.abort(reason); @@ -928,7 +945,11 @@ it.each( ); let second: Promise | undefined; try { - await entered.promise; + await awaitAcknowledgement( + entered.promise, + first, + "PyInstaller operation", + ); expect(reader.formatMetadata()).toBeUndefined(); if (peer) { second = members(reader); @@ -1031,7 +1052,11 @@ it("keeps a physical parse failure primary despite simultaneous caller cancellat ); let second: Promise | undefined; try { - await entered.promise; + await awaitAcknowledgement( + entered.promise, + first, + "PyInstaller operation", + ); second = members(reader).catch((cause: unknown) => cause); release.resolve(); const original = await first; @@ -1042,8 +1067,7 @@ it("keeps a physical parse failure primary despite simultaneous caller cancellat expect(reader.formatMetadata()).toBeUndefined(); } finally { release.resolve(); - await first; - await second; + await Promise.allSettled([first, ...(second ? [second] : [])]); reads.mockRestore(); } }, diff --git a/src/artifacts/pyinstaller/PyzArchive.test.ts b/src/artifacts/pyinstaller/PyzArchive.test.ts index 1adc66e7eb..45f84d44fd 100644 --- a/src/artifacts/pyinstaller/PyzArchive.test.ts +++ b/src/artifacts/pyinstaller/PyzArchive.test.ts @@ -6,7 +6,7 @@ import { open, writeFile } from "node:fs/promises"; import { join } from "node:path"; import { expect, it } from "vitest"; -import { readPyzArchive, MAX_PYZ_TABLE_BYTES } from "./PyzArchive.js"; +import { readPyzArchive } from "./PyzArchive.js"; import { createTestTempDirectory } from "../../../tests/fixtures/temporaryDirectory.js"; import { PYTHON_39_MAGIC, pyzArchive } from "./CArchive.fixture.js"; @@ -82,7 +82,7 @@ it.each(["PYZ\0", "BAD\0"])( await writeFile(path, header); const handle = await open(path, "r+"); try { - const size = 12 + MAX_PYZ_TABLE_BYTES + 1; + const size = 12 + 16 * 1024 * 1024 + 1; await handle.truncate(size); expect(await readPyzArchive(handle, 0, size)).toMatchObject({ ok: false, diff --git a/src/artifacts/pyinstaller/PyzArchive.ts b/src/artifacts/pyinstaller/PyzArchive.ts index f0408e1e54..9f88a04967 100644 --- a/src/artifacts/pyinstaller/PyzArchive.ts +++ b/src/artifacts/pyinstaller/PyzArchive.ts @@ -44,7 +44,7 @@ export type PyzArchive = }; /** Bound the transient marshal table allocation independently of image expansion. */ -export const MAX_PYZ_TABLE_BYTES = 16 * 1024 * 1024; +const MAX_PYZ_TABLE_BYTES = 16 * 1024 * 1024; interface PyzHeaderFacts { readonly signatureMatches: boolean; diff --git a/src/browser/BrowserScenarioResponseValues.ts b/src/browser/BrowserScenarioResponseValues.ts index bf7ed27d30..7ad607e838 100644 --- a/src/browser/BrowserScenarioResponseValues.ts +++ b/src/browser/BrowserScenarioResponseValues.ts @@ -1,3 +1,4 @@ +import { jsonPointerTokens } from "../domain/jsonPointer.js"; import { visit, type JSONPath } from "jsonc-parser"; import { isExactJsonNumber } from "../domain/exactJsonNumber.js"; import { safeParseJson } from "../domain/safeJson.js"; @@ -22,14 +23,6 @@ export interface ResponseValue { readonly value: string | number; } -const pointerTokens = (pointer: string): readonly string[] => - pointer === "" - ? [] - : pointer - .slice(1) - .split("/") - .map((token) => token.replaceAll("~1", "/").replaceAll("~0", "~")); - /** Keep unrelated fields usable while marking numbers that cannot be selected exactly. */ export const parseScenarioValueJson = (text: string) => safeParseJson(text, (_key, value, context) => @@ -44,7 +37,7 @@ export const responsePointerValue = ( pointer: string, ): unknown => { let current = value; - for (const key of pointerTokens(pointer)) { + for (const key of jsonPointerTokens(pointer)) { if (Array.isArray(current)) { if (!/^(?:0|[1-9][0-9]*)(?![\s\S])/u.test(key)) return undefined; current = current[Number(key)]; @@ -131,10 +124,11 @@ export const decodedBodyBound = ( headers: Readonly>, ): number | undefined => { const declared = headers["content-length"]?.trim(); + // Content-Length allows leading zeros; only significant digits bound precision. const bytes = received !== undefined && Number.isSafeInteger(received) && received > 0 ? received - : declared !== undefined && /^[1-9]\d{0,14}$/u.test(declared) + : declared !== undefined && /^0*[1-9]\d{0,14}$/u.test(declared) ? Number(declared) : undefined; if (bytes === undefined) return undefined; @@ -190,7 +184,7 @@ export const redactResponseSource = ( ): Result => { const markers = new Map( values.map(({ entry, value }) => [ - JSON.stringify(pointerTokens(entry.json_pointer)), + JSON.stringify(jsonPointerTokens(entry.json_pointer)), redact(String(value)), ]), ); diff --git a/src/browser/CdpAgentHints.integration.test.ts b/src/browser/CdpAgentHints.integration.test.ts index f6da762eee..d3963ed6e0 100644 --- a/src/browser/CdpAgentHints.integration.test.ts +++ b/src/browser/CdpAgentHints.integration.test.ts @@ -90,7 +90,6 @@ it.each(["", "h:"])( await browser.close(); } }, - 30_000, ); it.each(["progress", "enable", "terminal", "complete"] as const)( diff --git a/src/browser/CdpCaptureDocuments.baseUrl.test.ts b/src/browser/CdpCaptureDocuments.baseUrl.test.ts index 5836c2d20e..71adb9f679 100644 --- a/src/browser/CdpCaptureDocuments.baseUrl.test.ts +++ b/src/browser/CdpCaptureDocuments.baseUrl.test.ts @@ -1367,5 +1367,5 @@ describe("XML namespace resolution ordering and capacity", () => { expect(result.urls).toEqual([ approvedHref(elements.length, `${origin}/deep`), ]); - }, 30_000); + }); }); diff --git a/src/browser/CdpCaptureDocuments.ts b/src/browser/CdpCaptureDocuments.ts index 47b31f50a8..bb04f8cd9d 100644 --- a/src/browser/CdpCaptureDocuments.ts +++ b/src/browser/CdpCaptureDocuments.ts @@ -777,11 +777,15 @@ const domMetadata = ( destination_scope: destination.scope, }); } - const rel = (pairs.get("rel") ?? "") - .toLowerCase() - // HTML link type tokens are separated by ASCII whitespace only. - .split(/[\t\n\f\r ]+/u) - .filter((value) => agentRelValues.includes(value)); + // HTML link types form a set per element, separated by ASCII whitespace. + const rel = [ + ...new Set( + (pairs.get("rel") ?? "") + .toLowerCase() + .split(/[\t\n\f\r ]+/u) + .filter((value) => agentRelValues.includes(value)), + ), + ]; const href = urls.find(({ attribute }) => attribute === "href")?.url ?? null; const agentHints = tagName === "link" && rel.length > 0 diff --git a/src/browser/CdpElectronInspection.ts b/src/browser/CdpElectronInspection.ts index d4e3bb7223..168bc83dd1 100644 --- a/src/browser/CdpElectronInspection.ts +++ b/src/browser/CdpElectronInspection.ts @@ -244,6 +244,10 @@ const frameInventoryIdentity = ( .sort(compareUnicodeCodePoints), ); +/** Key a frame generation consistently for both authorization and capture. */ +const frameGenerationKey = (frameId: string, generation: number): string => + compositeKey([frameId, generation]); + const authorizeFrameGenerations = ( read: ElectronFrameRead, state: ElectronInspectionState, @@ -262,13 +266,13 @@ const authorizeFrameGenerations = ( const generation = read.generations.get(frame.frame_id) ?? 0; if (unproven.has(frame.frame_id)) continue; state.authorizedFrameGenerations.add( - compositeKey([frame.frame_id, generation]), + frameGenerationKey(frame.frame_id, generation), ); } for (const id of read.excludedFrameIds) if (!unproven.has(id)) state.excludedFrameGenerations.add( - compositeKey([id, read.generations.get(id) ?? 0]), + frameGenerationKey(id, read.generations.get(id) ?? 0), ); }; @@ -583,7 +587,7 @@ const captureElectronContent = async ( frameScopeForScript: (script) => { const generation = state.scriptGenerations.get(script); if (script.frameId !== null && generation !== undefined) { - const key = compositeKey([script.frameId, generation]); + const key = frameGenerationKey(script.frameId, generation); if (state.excludedFrameGenerations.has(key)) return { state: "excluded" }; if (state.authorizedFrameGenerations.has(key)) diff --git a/src/browser/CdpElectronProvider.ts b/src/browser/CdpElectronProvider.ts index 1dc918975d..bd9d96737a 100644 --- a/src/browser/CdpElectronProvider.ts +++ b/src/browser/CdpElectronProvider.ts @@ -214,24 +214,25 @@ export class CdpElectronProvider implements ElectronObservationPort { }), ); } catch (cause: unknown) { + const completedPrefix = { + browser_version: discovery?.version ?? null, + examined_targets: examined, + discovered_targets: discovery?.targets.length ?? null, + allowed_targets: allowed, + excluded: { + unsupported_url: unsupportedUrl, + non_page: nonPage, + }, + unconnectable_targets: unconnectable, + coverage: + "partial: target authorization stopped; remaining targets are unknown", + }; const fileFailure = mapElectronFileAuthorizationFailure( cause, CDP_ELECTRON_PROVIDER_IDENTITY.id, "list_electron_targets", { - completed_prefix: { - browser_version: discovery?.version ?? null, - examined_targets: examined, - discovered_targets: discovery?.targets.length ?? null, - allowed_targets: allowed, - excluded: { - unsupported_url: unsupportedUrl, - non_page: nonPage, - }, - unconnectable_targets: unconnectable, - coverage: - "partial: target authorization stopped; remaining targets are unknown", - }, + completed_prefix: completedPrefix, }, ); if (fileFailure !== undefined) return err(fileFailure); @@ -240,19 +241,7 @@ export class CdpElectronProvider implements ElectronObservationPort { ? new AnalysisCancelledError("list_electron_targets", { cause: options.signal.reason, diagnostics: { - completed_prefix: { - browser_version: discovery?.version ?? null, - examined_targets: examined, - discovered_targets: discovery?.targets.length ?? null, - allowed_targets: allowed, - excluded: { - unsupported_url: unsupportedUrl, - non_page: nonPage, - }, - unconnectable_targets: unconnectable, - coverage: - "partial: target authorization stopped; remaining targets are unknown", - }, + completed_prefix: completedPrefix, }, }) : providerError(cause, "list_electron_targets"), diff --git a/src/browser/ElectronProcessCleanup.ts b/src/browser/ElectronProcessCleanup.ts index 45238703ac..ac12a1857d 100644 --- a/src/browser/ElectronProcessCleanup.ts +++ b/src/browser/ElectronProcessCleanup.ts @@ -2,11 +2,9 @@ import { cleanupOwnedProcessGroup, cleanupWindowsProcessTree, verifyNoTokenOwnedProcesses, - type OwnedProcessGroup, type PersistentOwnedProcessGroup, type ProcessCleanupResult, type ProcessLineageObservation, - type OwnershipSweepUnverifiedProcess, type ProcessOwnershipHost, } from "../process/ProcessOwnership.js"; import { systemProcessOwnershipHost } from "../process/ProcessOwnershipObservation.js"; diff --git a/src/browser/IsolatedWebBundleAnalyzer.test.ts b/src/browser/IsolatedWebBundleAnalyzer.test.ts index 23a701c95e..bfc2624cb3 100644 --- a/src/browser/IsolatedWebBundleAnalyzer.test.ts +++ b/src/browser/IsolatedWebBundleAnalyzer.test.ts @@ -1,3 +1,4 @@ +import { awaitAcknowledgement } from "../../tests/fixtures/lifecycleAcknowledgement.js"; import { expect, it } from "vitest"; import { createWebPageInspection } from "../../tests/support/browser/webPageInspection.js"; @@ -233,10 +234,19 @@ it("carries the exact caller cancellation reason and completed prefix facts", as input, controller.signal, ); - await started.promise; - controller.abort(exactReason); - const result = await pending; - analyzer.releaseReservation(); + let result: Awaited; + try { + await awaitAcknowledgement(started.promise, pending, "script analysis"); + controller.abort(exactReason); + result = await pending; + } finally { + controller.abort(exactReason); + try { + await pending; + } finally { + analyzer.releaseReservation(); + } + } if (result.ok) throw new Error("Expected cancellation"); expect(result.error._tag).toBe("AnalysisCancelledError"); diff --git a/src/browser/PlaywrightElectronActiveActions.test.ts b/src/browser/PlaywrightElectronActiveActions.test.ts index e171badb94..65b34439c7 100644 --- a/src/browser/PlaywrightElectronActiveActions.test.ts +++ b/src/browser/PlaywrightElectronActiveActions.test.ts @@ -5,6 +5,7 @@ import { afterEach, expect, it, vi } from "vitest"; import type { ElectronApplication } from "playwright-core"; import { ArtifactResourceScope } from "../artifacts/ArtifactResourceScope.js"; import { createDeferred } from "../../tests/fixtures/binarySession.js"; +import { awaitAcknowledgement } from "../../tests/fixtures/lifecycleAcknowledgement.js"; import { electronActiveObservationInputSchema, @@ -791,20 +792,33 @@ it.each(["acquisition", "identity"] as const)( { signal: controller.signal }, resources, ); - await started.promise; - controller.abort(new Error("stop during selected window read")); - await Promise.resolve(); - finish.resolve(); - const outcome = await running; - expect(outcome.actions[0]).toMatchObject({ - status: "cancelled", - target: null, - }); - expect(page.locator).not.toHaveBeenCalled(); - expect(dispose).toHaveBeenCalledOnce(); - expect(resources.hasPending).toBe(false); - await resources.close(); - expect(dispose).toHaveBeenCalledOnce(); + try { + await awaitAcknowledgement( + started.promise, + running, + "selected window read", + ); + controller.abort(new Error("stop during selected window read")); + await Promise.resolve(); + finish.resolve(); + const outcome = await running; + expect(outcome.actions[0]).toMatchObject({ + status: "cancelled", + target: null, + }); + expect(page.locator).not.toHaveBeenCalled(); + expect(dispose).toHaveBeenCalledOnce(); + expect(resources.hasPending).toBe(false); + await resources.close(); + expect(dispose).toHaveBeenCalledOnce(); + } finally { + finish.resolve(); + try { + await running; + } finally { + await resources.close(); + } + } }, ); diff --git a/src/browser/PlaywrightElectronActiveProvider.test.ts b/src/browser/PlaywrightElectronActiveProvider.test.ts index 4d576cdc4d..90e4a07f5a 100644 --- a/src/browser/PlaywrightElectronActiveProvider.test.ts +++ b/src/browser/PlaywrightElectronActiveProvider.test.ts @@ -8,6 +8,7 @@ import { AnalysisOutputError } from "../domain/analysisErrorCore.js"; import { ProviderCleanupError } from "../domain/providerCleanupError.js"; import { DarwinProcessOwnershipInspectionError } from "../process/DarwinProcessRunTokenReader.js"; import { ExecFileOutputCancelledError } from "../process/ExecFileOutput.js"; +import * as processOutput from "../process/ExecFileOutput.js"; import { cleanupElectronProcesses } from "./ElectronProcessCleanup.js"; import { afterEach, beforeEach, expect, it, vi } from "vitest"; import { writeFile } from "node:fs/promises"; @@ -183,15 +184,13 @@ it("preserves an independent AbortError from actual ownership baseline inspectio "independent inspector failure", "AbortError", ); + vi.spyOn(processOutput, "execFileOutput").mockImplementation(async () => { + controller.abort(reason); + throw physical; + }); const host = ownershipObservation.createSystemProcessOwnershipHost( "linux", {}, - { - execFileOutput: async () => { - controller.abort(reason); - throw physical; - }, - }, ); const prepare = vi .spyOn(ownershipObservation, "prepareProcessOwnershipInspection") diff --git a/src/browser/PlaywrightScenarioBrowser.test.ts b/src/browser/PlaywrightScenarioBrowser.test.ts index 20854534dc..eb5738f707 100644 --- a/src/browser/PlaywrightScenarioBrowser.test.ts +++ b/src/browser/PlaywrightScenarioBrowser.test.ts @@ -6,6 +6,7 @@ import { afterEach, expect, it, vi } from "vitest"; import { mkdir, writeFile, rm, access, symlink } from "node:fs/promises"; import { join } from "node:path"; import { createTestTempDirectory } from "../../tests/fixtures/temporaryDirectory.js"; +import { awaitAcknowledgement } from "../../tests/fixtures/lifecycleAcknowledgement.js"; import type { Browser, BrowserContext, @@ -242,34 +243,42 @@ it.each([false, true])( (value) => ({ ok: true as const, value }), (cause: unknown) => ({ ok: false as const, cause }), ); - await started.promise; - if (cancelled) controller.abort(reason); - launched.resolve({ + const context = { close, browser: () => ({}) as Browser, pages: () => [], newPage, - } as unknown as BrowserContext); - const result = await opening; - if (result.ok) { - try { - expect(cancelled).toBe(false); - expect(newPage).toHaveBeenCalledOnce(); - expect(close).not.toHaveBeenCalled(); - expect(result.value.profilePath).toBe(profile); - } finally { - await result.value.cleanup.close(); + } as unknown as BrowserContext; + try { + await awaitAcknowledgement(started.promise, opening, "browser launch"); + if (cancelled) controller.abort(reason); + launched.resolve(context); + const result = await opening; + if (result.ok) { + try { + expect(cancelled).toBe(false); + expect(newPage).toHaveBeenCalledOnce(); + expect(close).not.toHaveBeenCalled(); + expect(result.value.profilePath).toBe(profile); + } finally { + await result.value.cleanup.close(); + } + } else { + expect(cancelled).toBe(true); + expect(result.cause).toMatchObject({ reason: "cancelled" }); + if (!(result.cause instanceof AnalysisError)) + throw new Error("Expected typed cancellation"); + expect(result.cause.cause).toBe(reason); + expect(newPage).not.toHaveBeenCalled(); } - } else { - expect(cancelled).toBe(true); - expect(result.cause).toMatchObject({ reason: "cancelled" }); - if (!(result.cause instanceof AnalysisError)) - throw new Error("Expected typed cancellation"); - expect(result.cause.cause).toBe(reason); - expect(newPage).not.toHaveBeenCalled(); + expect(close).toHaveBeenCalledOnce(); + await expect(access(profile)).rejects.toMatchObject({ code: "ENOENT" }); + } finally { + controller.abort(reason); + launched.resolve(context); + const outcome = await opening; + if (outcome.ok) await outcome.value.cleanup.close(); } - expect(close).toHaveBeenCalledOnce(); - await expect(access(profile)).rejects.toMatchObject({ code: "ENOENT" }); }, ); @@ -428,16 +437,27 @@ it("does not configure a late attached connection after cancellation", async () signal: controller.signal, }, ); - await connectionStarted.promise; - controller.abort(); - connection.resolve(connectedBrowser); + try { + await awaitAcknowledgement( + connectionStarted.promise, + opening, + "CDP connection", + ); + controller.abort(); + connection.resolve(connectedBrowser); - await expect(opening).rejects.toMatchObject({ - _tag: "BrowserObservationError", - reason: "cancelled", - }); - expect(connectedBrowser.contexts).not.toHaveBeenCalled(); - expect(connectedBrowser.close).toHaveBeenCalledOnce(); + await expect(opening).rejects.toMatchObject({ + _tag: "BrowserObservationError", + reason: "cancelled", + }); + expect(connectedBrowser.contexts).not.toHaveBeenCalled(); + expect(connectedBrowser.close).toHaveBeenCalledOnce(); + } finally { + controller.abort(); + connection.resolve(connectedBrowser); + const opened = await opening.catch(() => undefined); + await opened?.cleanup.close(); + } }); it("closes an attached connection when cancellation starts target discovery", async () => { @@ -651,10 +671,19 @@ it("checks a late stop after attached restoration and before transport disconnec }, }); - await restorationStarted.promise; - revision += 1; - finishRestoration.resolve(undefined); - await release; + try { + await awaitAcknowledgement( + restorationStarted.promise, + release, + "browser restoration", + ); + revision += 1; + finishRestoration.resolve(undefined); + await release; + } finally { + finishRestoration.resolve(undefined); + await release.catch(() => undefined); + } expect(order).toEqual([ "restore-started", @@ -738,25 +767,36 @@ it("waits for a cancelled environment mutation before restoring it", async () => }); const controller = new AbortController(); const opening = fixture.open(controller.signal); - await localeStarted.promise; - const cancelled = expect(opening).rejects.toMatchObject({ - _tag: "AnalysisCancelledError", - }); - controller.abort(); - await new Promise((resolve) => setImmediate(resolve)); - expect(fixture.environmentSession.send).not.toHaveBeenCalledWith( - "Emulation.setLocaleOverride", - { locale: "" }, - ); + try { + await awaitAcknowledgement( + localeStarted.promise, + opening, + "locale mutation", + ); + const cancelled = expect(opening).rejects.toMatchObject({ + _tag: "AnalysisCancelledError", + }); + controller.abort(); + await new Promise((resolve) => setImmediate(resolve)); + expect(fixture.environmentSession.send).not.toHaveBeenCalledWith( + "Emulation.setLocaleOverride", + { locale: "" }, + ); - localeResult.resolve(undefined); - await cancelled; - expect(fixture.environmentSession.send).toHaveBeenCalledWith( - "Emulation.setLocaleOverride", - { locale: "" }, - ); - expect(fixture.environmentSession.detach).toHaveBeenCalledOnce(); - expect(fixture.browser.close).toHaveBeenCalledOnce(); + localeResult.resolve(undefined); + await cancelled; + expect(fixture.environmentSession.send).toHaveBeenCalledWith( + "Emulation.setLocaleOverride", + { locale: "" }, + ); + expect(fixture.environmentSession.detach).toHaveBeenCalledOnce(); + expect(fixture.browser.close).toHaveBeenCalledOnce(); + } finally { + controller.abort(); + localeResult.resolve(undefined); + const opened = await opening.catch(() => undefined); + await opened?.cleanup.close(); + } }); it("releases cleanup ownership when the attached target is destroyed", async () => { @@ -1055,12 +1095,26 @@ it("rechecks the browser release gate after finalization changes its revision", }, ); - await eventsStarted.promise; - finishEvents.resolve(undefined); - await gateStarted.promise; - revision += 1; - finishGate.resolve(undefined); - await release; + try { + await awaitAcknowledgement( + eventsStarted.promise, + release, + "browser event finalization", + ); + finishEvents.resolve(undefined); + await awaitAcknowledgement( + gateStarted.promise, + release, + "browser release gate", + ); + revision += 1; + finishGate.resolve(undefined); + await release; + } finally { + finishEvents.resolve(undefined); + finishGate.resolve(undefined); + await release.catch(() => undefined); + } expect(gateRuns).toEqual([0, 1]); expect(closeBrowser).toHaveBeenCalledTimes(1); @@ -1092,10 +1146,19 @@ it("runs the latest stop gate after attached restoration and before disconnect", }, }); - await restorationStarted.promise; - revision += 1; - finishRestoration.resolve(undefined); - await release; + try { + await awaitAcknowledgement( + restorationStarted.promise, + release, + "browser restoration", + ); + revision += 1; + finishRestoration.resolve(undefined); + await release; + } finally { + finishRestoration.resolve(undefined); + await release.catch(() => undefined); + } expect(gateRevisions).toEqual([1]); expect(disconnected).toBe(true); @@ -1143,19 +1206,32 @@ it.each([false, true])( () => ({ ok: true as const }), (cause: unknown) => ({ ok: false as const, cause }), ); - await gateStarted.promise; - if (changeRevision) revision += 1; - finishGate.resolve(undefined); - await expect(release).resolves.toEqual({ ok: false, cause: failure }); - expect(closeBrowser).toHaveBeenCalledTimes(1); - expect(transportClosed).toBe(false); - expect(owner.browserClosed).toBe(false); - expect(owner.cleanupResources).toContain("browser_transport"); + try { + await awaitAcknowledgement( + gateStarted.promise, + release, + "browser release gate", + ); + if (changeRevision) revision += 1; + finishGate.resolve(undefined); + await expect(release).resolves.toEqual({ ok: false, cause: failure }); + expect(closeBrowser).toHaveBeenCalledTimes(1); + expect(transportClosed).toBe(false); + expect(owner.browserClosed).toBe(false); + expect(owner.cleanupResources).toContain("browser_transport"); - await owner.release(undefined, gate); - expect(closeBrowser).toHaveBeenCalledTimes(2); - expect(transportClosed).toBe(true); - expect(owner.browserClosed).toBe(true); + await owner.release(undefined, gate); + expect(closeBrowser).toHaveBeenCalledTimes(2); + expect(transportClosed).toBe(true); + expect(owner.browserClosed).toBe(true); + } finally { + finishGate.resolve(undefined); + try { + await release; + } finally { + await owner.close(); + } + } }, ); diff --git a/src/browser/PlaywrightScenarioRequest.test.ts b/src/browser/PlaywrightScenarioRequest.test.ts index 962b7b840a..3f683f183f 100644 --- a/src/browser/PlaywrightScenarioRequest.test.ts +++ b/src/browser/PlaywrightScenarioRequest.test.ts @@ -1076,6 +1076,12 @@ describe("saved response JSON pointers", () => { vi.unstubAllGlobals(); }); + it("selects the root and decodes escaped response keys once", () => { + const value = { "a/b": { "~1": "literal", "/": "slash" } }; + expect(responsePointerValue(value, "")).toBe(value); + expect(responsePointerValue(value, "/a~1b/~01")).toBe("literal"); + }); + it("does not coerce a terminal newline in an array index", () => { expect(responsePointerValue({ items: ["record-7"] }, "/items/0\n")).toBe( undefined, @@ -1699,6 +1705,14 @@ describe("saving from a UI step's response", () => { { encoding: "identity", size: 0 }, { encoding: "gzip", size: 0 }, { size: 0, length: "0" }, + { size: 0, length: "0000" }, + { size: 0, length: "+17" }, + { size: 0, length: "17.0" }, + { size: 0, length: "1.7e1" }, + { size: 0, length: "17, 17" }, + { size: 0, length: "0001000000000000000" }, + { size: 0, length: "9007199254740993" }, + { size: 0, length: "0009007199254740993" }, { size: -1 }, { size: Number.NaN }, ])( @@ -1831,7 +1845,10 @@ describe("saving from a compressed or cached UI response", () => { { encoding: "deflate", size: 32 }, { encoding: "x-gzip", size: 32 }, { size: 0, length: "17" }, + { size: 0, length: "0017" }, + { size: 0, length: "00000000000000000017" }, { encoding: "gzip", size: 0, length: "32" }, + { encoding: "gzip", size: 0, length: "0032" }, ])( "saves from a response whose received size or declared length bounds its decoded size: %j", async (bounds) => { @@ -1852,23 +1869,29 @@ describe("saving from a compressed or cached UI response", () => { }, ); - it("refuses a compressed response that could decode past the limit without reading it", async () => { - const parsed = clickSaving("/api/items"); - const { page, reads } = uiPage([ - { - method: "POST", - url: `${ORIGIN}/api/items`, - status: 201, - body: '{"id":"x"}', - encoding: "gzip", - size: 12_000, - }, - ]); - await expect( - perform(page, parsed.actions[0]!, resolved(parsed)), - ).rejects.toThrow("could decode past the size limit"); - expect(reads.count).toBe(0); - }); + it.each([ + { encoding: "gzip", size: 12_000 }, + { size: 0, length: "00012000001" }, + { encoding: "gzip", size: 0, length: "00012000" }, + ])( + "refuses a response that could decode past the limit without reading it: %j", + async (bounds) => { + const parsed = clickSaving("/api/items"); + const { page, reads } = uiPage([ + { + method: "POST", + url: `${ORIGIN}/api/items`, + status: 201, + body: '{"id":"x"}', + ...bounds, + }, + ]); + await expect( + perform(page, parsed.actions[0]!, resolved(parsed)), + ).rejects.toThrow("could decode past the size limit"); + expect(reads.count).toBe(0); + }, + ); }); describe("saved response limits and deadlines", () => { diff --git a/src/browser/PlaywrightScenarioRequest.ts b/src/browser/PlaywrightScenarioRequest.ts index 6a2c909e31..8c53335b1e 100644 --- a/src/browser/PlaywrightScenarioRequest.ts +++ b/src/browser/PlaywrightScenarioRequest.ts @@ -38,7 +38,7 @@ type PageResponse = | { readonly error: string }; /** A request step that could not be sent or did not answer as expected. */ -export class ScenarioRequestError extends Error { +class ScenarioRequestError extends Error { override readonly name = "ScenarioRequestError"; } diff --git a/src/browser/PlaywrightScenarioServiceWorkers.ts b/src/browser/PlaywrightScenarioServiceWorkers.ts index 43aa200834..9c01e3290d 100644 --- a/src/browser/PlaywrightScenarioServiceWorkers.ts +++ b/src/browser/PlaywrightScenarioServiceWorkers.ts @@ -389,14 +389,10 @@ export class PlaywrightScenarioServiceWorkers { // Abort discovery/handshakes and reads, never an already-sent page mutation. this.#setup.abort(); const restoration = (this.#restoration ??= this.restore()); - void restoration.then( - () => { - if (this.#restoration === restoration) this.#restoration = undefined; - }, - () => { - if (this.#restoration === restoration) this.#restoration = undefined; - }, - ); + const clearRestoration = (): void => { + if (this.#restoration === restoration) this.#restoration = undefined; + }; + void restoration.then(clearRestoration, clearRestoration); // Restoration completion does not discharge a stop request that arrived // after that restoration began. Both paths share the owner's stop latch. const completion = restoration.then(() => this.fulfillStopRequest()); @@ -427,14 +423,10 @@ export class PlaywrightScenarioServiceWorkers { this.#loadingStopped = true; })(); this.#stopPromise = stopping; - void stopping.then( - () => { - if (this.#stopPromise === stopping) this.#stopPromise = undefined; - }, - () => { - if (this.#stopPromise === stopping) this.#stopPromise = undefined; - }, - ); + const clearStop = (): void => { + if (this.#stopPromise === stopping) this.#stopPromise = undefined; + }; + void stopping.then(clearStop, clearStop); return stopping; } diff --git a/src/browser/PlaywrightScenarioSession.test.ts b/src/browser/PlaywrightScenarioSession.test.ts index 49d3447475..8b66b90819 100644 --- a/src/browser/PlaywrightScenarioSession.test.ts +++ b/src/browser/PlaywrightScenarioSession.test.ts @@ -1,3 +1,4 @@ +import { awaitAcknowledgement } from "../../tests/fixtures/lifecycleAcknowledgement.js"; import { AnalysisCleanupLeaseGroup, AnalysisCleanupLease, @@ -550,24 +551,34 @@ it("transfers cancellation to a no-stop cleanup already in progress", async () = }); const firstClose = session.close(false); - await started; - cancellation.abort(); - const retry = session.close(); - finishRestoration(); - await expect(Promise.all([firstClose, retry])).resolves.toEqual([ - "disconnected-external", - "disconnected-external", - ]); + try { + await awaitAcknowledgement(started, firstClose, "policy restoration"); + cancellation.abort(); + const retry = session.close(); + finishRestoration(); + await expect(Promise.all([firstClose, retry])).resolves.toEqual([ + "disconnected-external", + "disconnected-external", + ]); - const policyStops = mocks.policySend.mock.calls.filter( - ([method]) => method === "Page.stopLoading", - ).length; - const frameStops = page.evaluate.mock.calls.filter( - ([script]) => script === "globalThis.stop()", - ).length; - expect(policyStops + frameStops).toBe(1); - expect(cleanup.close).toHaveBeenCalledTimes(1); - expect(browserWasOpenAtStop).toBe(true); + const policyStops = mocks.policySend.mock.calls.filter( + ([method]) => method === "Page.stopLoading", + ).length; + const frameStops = page.evaluate.mock.calls.filter( + ([script]) => script === "globalThis.stop()", + ).length; + expect(policyStops + frameStops).toBe(1); + expect(cleanup.close).toHaveBeenCalledTimes(1); + expect(browserWasOpenAtStop).toBe(true); + } finally { + cancellation.abort(); + finishRestoration(); + try { + await firstClose; + } finally { + await session.close(); + } + } }); it("does not repeat a stop confirmed by storage during a late retry", async () => { @@ -591,27 +602,37 @@ it("does not repeat a stop confirmed by storage during a late retry", async () = }); const firstClose = session.close(false); - await started; - cancellation.abort(); - const retry = session.close(); - finishRestoration(); - await expect(Promise.all([firstClose, retry])).resolves.toEqual([ - "disconnected-external", - "disconnected-external", - ]); + try { + await awaitAcknowledgement(started, firstClose, "policy restoration"); + cancellation.abort(); + const retry = session.close(); + finishRestoration(); + await expect(Promise.all([firstClose, retry])).resolves.toEqual([ + "disconnected-external", + "disconnected-external", + ]); - expect(storage?.close).toHaveBeenCalledWith(false); - expect( - mocks.policySend.mock.calls.filter( - ([method]) => method === "Page.stopLoading", - ), - ).toHaveLength(0); - expect( - page.evaluate.mock.calls.filter( - ([script]) => script === "globalThis.stop()", - ), - ).toHaveLength(0); - expect(cleanup.close).toHaveBeenCalledTimes(1); + expect(storage?.close).toHaveBeenCalledWith(false); + expect( + mocks.policySend.mock.calls.filter( + ([method]) => method === "Page.stopLoading", + ), + ).toHaveLength(0); + expect( + page.evaluate.mock.calls.filter( + ([script]) => script === "globalThis.stop()", + ), + ).toHaveLength(0); + expect(cleanup.close).toHaveBeenCalledTimes(1); + } finally { + cancellation.abort(); + finishRestoration(); + try { + await firstClose; + } finally { + await session.close(); + } + } }); it.each(["block", "allow"] as const)( @@ -938,18 +959,19 @@ it("does not await policy initialization after its child target detaches", async }, sessionId: "policy-session", }); - await started; - const closing = session.close(); - mocks.policyEvent?.({ - method: "Target.detachedFromTarget", - params: { sessionId: "child-session" }, - }); try { + await awaitAcknowledgement(started, held, "child policy admission"); + const closing = session.close(); + mocks.policyEvent?.({ + method: "Target.detachedFromTarget", + params: { sessionId: "child-session" }, + }); await expect(closing).resolves.toBe("disconnected-external"); + expect(cleanup.close).toHaveBeenCalledTimes(1); } finally { releaseChild(); + await session.close(); } - expect(cleanup.close).toHaveBeenCalledTimes(1); }); it("skips policy restoration for a child without a committed document", async () => { @@ -1222,7 +1244,11 @@ it.each([false, true])( signal: controller.signal, }); try { - await started.promise; + await awaitAcknowledgement( + started.promise, + result, + "cookie initialization", + ); controller.abort(reason); await new Promise((resolve) => setImmediate(resolve)); expect(cleanup.close).not.toHaveBeenCalled(); @@ -1396,14 +1422,21 @@ it("cancels a stalled policy preflight without allowing late setup", async () => const outcome = expect(opening).rejects.toMatchObject({ _tag: "AnalysisCancelledError", }); - await started; - cancellation.abort(); - await outcome; - expect(cleanup.close).toHaveBeenCalledTimes(1); - expect(cleanup.close.mock.calls[0]).toHaveLength(1); - release(); - await new Promise((resolve) => setImmediate(resolve)); - expect(mocks.policySend).not.toHaveBeenCalled(); + try { + await awaitAcknowledgement(started, opening, "policy preflight"); + cancellation.abort(); + await outcome; + expect(cleanup.close).toHaveBeenCalledTimes(1); + expect(cleanup.close.mock.calls[0]).toHaveLength(1); + release(); + await new Promise((resolve) => setImmediate(resolve)); + expect(mocks.policySend).not.toHaveBeenCalled(); + } finally { + cancellation.abort(); + release(); + await opening.catch(() => undefined); + await outcome.catch(() => undefined); + } }); it("waits for a pending registration installation before cancelled cleanup", async () => { @@ -1427,25 +1460,32 @@ it("waits for a pending registration installation before cancelled cleanup", asy const outcome = expect(opening).rejects.toMatchObject({ _tag: "AnalysisCancelledError", }); - await started; - cancellation.abort(); - await new Promise((resolve) => setImmediate(resolve)); - expect(cleanup.close).not.toHaveBeenCalled(); - expect(page.evaluate).toHaveBeenCalledTimes(1); - releaseInstallation(); - await outcome; - const restoration = mocks.policySend.mock.calls.find( - ([method]) => method === "Runtime.evaluate", - )?.[1]; - expect(restoration).toMatchObject({ - expression: expect.stringContaining("policy_owned"), - }); - expect(mocks.policySend).toHaveBeenCalledWith( - "Page.stopLoading", - {}, - "policy-session", - ); - expect(cleanup.close).toHaveBeenCalledTimes(1); + try { + await awaitAcknowledgement(started, opening, "policy installation"); + cancellation.abort(); + await new Promise((resolve) => setImmediate(resolve)); + expect(cleanup.close).not.toHaveBeenCalled(); + expect(page.evaluate).toHaveBeenCalledTimes(1); + releaseInstallation(); + await outcome; + const restoration = mocks.policySend.mock.calls.find( + ([method]) => method === "Runtime.evaluate", + )?.[1]; + expect(restoration).toMatchObject({ + expression: expect.stringContaining("policy_owned"), + }); + expect(mocks.policySend).toHaveBeenCalledWith( + "Page.stopLoading", + {}, + "policy-session", + ); + expect(cleanup.close).toHaveBeenCalledTimes(1); + } finally { + cancellation.abort(); + releaseInstallation(); + await opening.catch(() => undefined); + await outcome.catch(() => undefined); + } }); it("rejects attached block when caller-owned registrations already exist", async () => { @@ -1578,7 +1618,7 @@ it("retains late opening failure cleanup after cancellation until the exact prof const pending = provider.captureScenario(scenario, { signal: caller.signal, }); - await started; + await awaitAcknowledgement(started, pending, "browser launch start"); caller.abort(reason); const result = await pending; if (result.ok) throw new Error("Expected cancelled opening"); @@ -1600,6 +1640,7 @@ it("retains late opening failure cleanup after cancellation until the exact prof }); expect(failure.cleanupIncomplete).toBe(false); } finally { + caller.abort(reason); denied = false; finishOpening(); await provider.close(); diff --git a/src/browser/WebBundleAnalysisWorkerStartup.test.ts b/src/browser/WebBundleAnalysisWorkerStartup.test.ts index 4261c014df..dc0542d6da 100644 --- a/src/browser/WebBundleAnalysisWorkerStartup.test.ts +++ b/src/browser/WebBundleAnalysisWorkerStartup.test.ts @@ -120,11 +120,14 @@ it.for([ } } } finally { - await worker.close(); - preparation.mockRestore(); - child.stdin.destroy(); - child.stdout.destroy(); - child.stderr.destroy(); + try { + await worker.close(); + } finally { + preparation.mockRestore(); + child.stdin.destroy(); + child.stdout.destroy(); + child.stderr.destroy(); + } } }, ); diff --git a/src/browser/WebSourceMapFetcher.test.ts b/src/browser/WebSourceMapFetcher.test.ts index 5a92142691..8f15c7e5d9 100644 --- a/src/browser/WebSourceMapFetcher.test.ts +++ b/src/browser/WebSourceMapFetcher.test.ts @@ -601,84 +601,51 @@ it("does not mistake an independent body AbortError for a falsy caller cancellat ).toBe(true); }); -it("keeps an actual stalled HTTP header deadline local to its map", async () => { - const server = createServer(() => {}); - server.listen(0, "127.0.0.1"); - await once(server, "listening"); - const address = server.address(); - if (address === null || typeof address === "string") - throw new Error("Expected TCP address"); - const url = `http://127.0.0.1:${address.port}/map`; - try { - const result = await fetchWebSourceMaps( - [{ ...request, declaredUrl: url, fetchUrl: url }], - { - ...input, - allowed_origins: [new URL(url).origin], - source_map_fetch_timeout_ms: 1000, - }, - undefined, - { fetch }, - ); - expect(result).toMatchObject({ - ok: true, - value: { - items: [ - { - status: "fetch_failed", - limitation: expect.stringContaining("deadline"), - }, - ], - }, +it.each(["headers", "body"] as const)( + "keeps an actual stalled HTTP %s deadline local to its map", + async (stage) => { + const server = createServer((_request, response) => { + if (stage === "body") { + response.writeHead(200, { "content-type": "application/json" }); + response.write('{"version":3,'); + } }); - } finally { - server.closeAllConnections(); - await new Promise((resolve, reject) => - server.close((error) => (error ? reject(error) : resolve())), - ); - } -}); - -it("keeps an actual stalled HTTP response deadline local to its map", async () => { - const server = createServer((_request, response) => { - response.writeHead(200, { "content-type": "application/json" }); - response.write('{"version":3,'); - }); - server.listen(0, "127.0.0.1"); - await once(server, "listening"); - const address = server.address(); - if (address === null || typeof address === "string") - throw new Error("Expected TCP address"); - const url = `http://127.0.0.1:${address.port}/map`; - try { - const result = await fetchWebSourceMaps( - [{ ...request, declaredUrl: url, fetchUrl: url }], - { - ...input, - allowed_origins: [new URL(url).origin], - source_map_fetch_timeout_ms: 1000, - }, - undefined, - { fetch }, - ); - expect(result).toMatchObject({ - ok: true, - value: { - items: [ - { - status: "fetch_failed", - limitation: expect.stringContaining("deadline"), - }, - ], - }, - }); - } finally { - server.closeAllConnections(); - await new Promise((resolve, reject) => - server.close((error) => (error ? reject(error) : resolve())), - ); - } -}); + server.listen(0, "127.0.0.1"); + await once(server, "listening"); + const address = server.address(); + if (address === null || typeof address === "string") + throw new Error("Expected TCP address"); + const url = `http://127.0.0.1:${address.port}/map`; + try { + const result = await fetchWebSourceMaps( + [{ ...request, declaredUrl: url, fetchUrl: url }], + { + ...input, + allowed_origins: [new URL(url).origin], + source_map_fetch_timeout_ms: 1000, + }, + undefined, + { fetch }, + ); + expect(result).toMatchObject({ + ok: true, + value: { + items: [ + { + status: "fetch_failed", + limitation: expect.stringContaining("deadline"), + }, + ], + }, + }); + } finally { + server.closeAllConnections(); + await new Promise((resolve, reject) => + server.close((error) => (error ? reject(error) : resolve())), + ); + } + }, +); it.each([302, 503])( "retains HTTP %s response cleanup without hanging on an independent tee branch", diff --git a/src/browser/WebSourceMapFetcher.ts b/src/browser/WebSourceMapFetcher.ts index c4e855dadb..b333429f07 100644 --- a/src/browser/WebSourceMapFetcher.ts +++ b/src/browser/WebSourceMapFetcher.ts @@ -706,7 +706,8 @@ const fetchOne = async ( budget: budget.decodedRecords, }, ); - } catch (caught: unknown) { + } catch (failure: unknown) { + let caught = failure; if (caught instanceof SourceMapPendingFetch) { if (caught.cause instanceof SourceMapDeadlineError) { // Native fetch reports abort completion asynchronously. Check the next @@ -870,7 +871,8 @@ const readBoundedText = async ( if (value !== undefined) chunks.push(value); bytes += nextBytes; } - } catch (cause: unknown) { + } catch (caught: unknown) { + let cause = caught; // Fetch can error its body with the abort reason before read cancellation. // A confirmed errored stream is already terminal and needs only its lock released. await Promise.resolve(); diff --git a/src/browser/history/CaptureFailures.test.ts b/src/browser/history/CaptureFailures.test.ts index 4d85134462..bdd0fa7d66 100644 --- a/src/browser/history/CaptureFailures.test.ts +++ b/src/browser/history/CaptureFailures.test.ts @@ -2,7 +2,6 @@ import { expect, it } from "vitest"; import { ArtifactReaderFailure } from "../../artifacts/ArtifactReader.js"; import { captureDecoderHeapContinuingRecovery, - captureDecoderHeapRecovery, DEFAULT_CAPTURE_DECODER_HEAP, } from "../../config/captureDecoderHeap.js"; import { projectAnalysisError } from "../../domain/analysisErrorProjection.js"; diff --git a/src/browser/history/CaptureFailures.ts b/src/browser/history/CaptureFailures.ts index 73b895418d..6e5f3060f6 100644 --- a/src/browser/history/CaptureFailures.ts +++ b/src/browser/history/CaptureFailures.ts @@ -1,3 +1,4 @@ +import { javaScriptWorkerMemoryLimitBytes } from "../../process/JavaScriptWorkerMemoryLimit.js"; import { captureBudgets } from "./CaptureBudgets.js"; import { analysisSettingLimit } from "../../domain/analysisSettingDefinitions.js"; import type { ExecutionOptions } from "../../application/AnalysisProvider.js"; @@ -36,11 +37,8 @@ import { type CaptureDecoderHeap, } from "../../config/captureDecoderHeap.js"; import { OwnedCommandFailure } from "../../process/OwnedCommand.js"; -import { - NODE_CAPTURE_HEAP_ENFORCED, - NODE_CAPTURE_HEAP_LIMITS, - captureDecoderProcessMemoryLimitBytes, -} from "./CaptureRelease.js"; +import { NODE_CAPTURE_HEAP_ENFORCED } from "./CaptureRelease.js"; +import { CAPTURE_DECODER_SEMI_SPACE_MIB } from "../../domain/analysisResourceLimits.js"; const OPERATION = "inspect_web_network_capture"; /** Reason for a decoder whose owned process tree reached its Job Object commit limit. */ @@ -296,7 +294,7 @@ export const captureMemoryFailure = ( cause?: unknown, heap: CaptureDecoderHeap = DEFAULT_CAPTURE_DECODER_HEAP, ): AnalysisResourceConstraintError => { - const processMemoryLimitBytes = captureDecoderProcessMemoryLimitBytes( + const processMemoryLimitBytes = javaScriptWorkerMemoryLimitBytes( heap.oldGenerationMiB, ); const recovery = captureDecoderHeapRecovery(heap); @@ -315,7 +313,7 @@ export const captureMemoryFailure = ( ? { old_generation_heap_mib: heap.oldGenerationMiB, old_generation_heap_source: heap.source, - semi_space_heap_mib: NODE_CAPTURE_HEAP_LIMITS.semiSpaceMiB, + semi_space_heap_mib: CAPTURE_DECODER_SEMI_SPACE_MIB, } : {}), ...(input.format !== "mitmproxy" && processMemoryLimitBytes !== null diff --git a/src/browser/history/CaptureJson.test.ts b/src/browser/history/CaptureJson.test.ts index 239ba86fc9..a42e494dbb 100644 --- a/src/browser/history/CaptureJson.test.ts +++ b/src/browser/history/CaptureJson.test.ts @@ -21,19 +21,15 @@ it("retains escape-heavy strings within a heap near their decoded size", async ( if (!Array.isArray(parsed) || parsed.length !== 463 || parsed.some((value) => value !== body)) process.exit(3); `; - const child = await processes.run( - process.execPath, - [ - "--max-old-space-size=48", - "--max-semi-space-size=8", - "--import", - "tsx", - "--input-type=module", - "--eval", - program, - ], - { timeoutMs: 30_000 }, - ); + const child = await processes.run(process.execPath, [ + "--max-old-space-size=48", + "--max-semi-space-size=8", + "--import", + "tsx", + "--input-type=module", + "--eval", + program, + ]); expect(child.exitCode, child.stderr).toBe(0); }); diff --git a/src/browser/history/CaptureJson.ts b/src/browser/history/CaptureJson.ts index b1703b76f5..b6333843b1 100644 --- a/src/browser/history/CaptureJson.ts +++ b/src/browser/history/CaptureJson.ts @@ -1,3 +1,4 @@ +import { escapeJsonPointerToken } from "../../domain/jsonPointer.js"; import { captureBudgets } from "./CaptureBudgets.js"; import { visit } from "jsonc-parser"; import { LosslessNumber } from "lossless-json"; @@ -58,7 +59,7 @@ export const parseCaptureJson = ( return `${parent.pointer}/${parent.items.length}`; return parent.pendingKey === undefined ? parent.pointer - : `${parent.pointer}/${parent.pendingKey.replaceAll("~", "~0").replaceAll("/", "~1")}`; + : `${parent.pointer}/${escapeJsonPointerToken(parent.pendingKey)}`; }; // The document root is container level 1. Admit a container only while // its own level stays within the budget; values inside an admitted diff --git a/src/browser/history/CaptureRedaction.ts b/src/browser/history/CaptureRedaction.ts index 9a505fce04..5a405ce81b 100644 --- a/src/browser/history/CaptureRedaction.ts +++ b/src/browser/history/CaptureRedaction.ts @@ -1,3 +1,4 @@ +import { escapeJsonPointerToken } from "../../domain/jsonPointer.js"; import { isSafeNumber, type LosslessNumber } from "lossless-json"; import type { WebNetworkCaptureRecord } from "../../domain/webNetworkCapture.js"; import { parseRecordedTransportUrl } from "../../domain/recordedTransportUrl.js"; @@ -149,7 +150,7 @@ export class CaptureRedaction { /** Escape a producer property for an RFC 6901 location. */ export const capturePointer = (parent: string, key: string): string => - `${parent}/${key.replaceAll("~", "~0").replaceAll("/", "~1")}`; + `${parent}/${escapeJsonPointerToken(key)}`; /** Structural record guard does not duck-type number/byte sidecar markers. */ export const captureObject = ( diff --git a/src/browser/history/CaptureRelease.ts b/src/browser/history/CaptureRelease.ts index dbb0c5fff6..96fa825ad5 100644 --- a/src/browser/history/CaptureRelease.ts +++ b/src/browser/history/CaptureRelease.ts @@ -1,27 +1,5 @@ -import { - CAPTURE_DECODER_SEMI_SPACE_MIB, - DEFAULT_CAPTURE_DECODER_OLD_GENERATION_MIB, -} from "../../domain/analysisResourceLimits.js"; import { javaScriptRuntime } from "../../domain/runtimeVersion.js"; -import { javaScriptWorkerMemoryLimitBytes } from "../../process/JavaScriptWorkerMemoryLimit.js"; - -/** - * Default V8 heap settings for the owned Node capture decoder process. - * The old generation is `REA_CAPTURE_DECODER_OLD_GENERATION_MIB`; the - * semi-space stays fixed. - */ -export const NODE_CAPTURE_HEAP_LIMITS = { - oldGenerationMiB: DEFAULT_CAPTURE_DECODER_OLD_GENERATION_MIB, - semiSpaceMiB: CAPTURE_DECODER_SEMI_SPACE_MIB, -} as const; - -/** Windows Job Object commit limit for one resolved old-generation budget. */ -export const captureDecoderProcessMemoryLimitBytes = ( - oldGenerationMiB: number, -): number | null => javaScriptWorkerMemoryLimitBytes(oldGenerationMiB); /** The Node capture decoder runs on this executable; Bun accepts but ignores V8 heap flags. */ export const NODE_CAPTURE_HEAP_ENFORCED = javaScriptRuntime(process.versions).id === "node"; - -/** Fixed offline interpretation profile, not an authenticated producer claim. */ diff --git a/src/browser/history/HistoricalCaptureBudgets.test.ts b/src/browser/history/HistoricalCaptureBudgets.test.ts index b5c382176b..f0ca1b87ef 100644 --- a/src/browser/history/HistoricalCaptureBudgets.test.ts +++ b/src/browser/history/HistoricalCaptureBudgets.test.ts @@ -10,9 +10,9 @@ import { historicalHar } from "../../../tests/fixtures/historicalHar.js"; import { createTestTempDirectory } from "../../../tests/fixtures/temporaryDirectory.js"; import { projectAnalysisError } from "../../domain/analysisErrorProjection.js"; import { jsonValueValidationIssue } from "../../domain/jsonValue.js"; -import { MAX_WEB_NETWORK_CAPTURE_NESTING_DEPTH } from "../../domain/webNetworkCapture.js"; const require = createRequire(import.meta.url); +const MAX_WEB_NETWORK_CAPTURE_NESTING_DEPTH = 996; /** Exercise the real private decoder protocol and parsers from the source checkout. */ const service = () => diff --git a/src/browser/history/HistoricalCaptureDecoder.ts b/src/browser/history/HistoricalCaptureDecoder.ts index 98e13c8a8f..f8ff159a87 100644 --- a/src/browser/history/HistoricalCaptureDecoder.ts +++ b/src/browser/history/HistoricalCaptureDecoder.ts @@ -1,3 +1,4 @@ +import { javaScriptWorkerMemoryLimitBytes } from "../../process/JavaScriptWorkerMemoryLimit.js"; import { captureBudgets, withCaptureBudgets } from "./CaptureBudgets.js"; import type { FileSnapshotPartialObservation } from "../../domain/fileSnapshotObservation.js"; import { @@ -16,10 +17,7 @@ import { resolveCaptureDecoderHeap, type CaptureDecoderHeap, } from "../../config/captureDecoderHeap.js"; -import { - NODE_CAPTURE_HEAP_ENFORCED, - captureDecoderProcessMemoryLimitBytes, -} from "./CaptureRelease.js"; +import { NODE_CAPTURE_HEAP_ENFORCED } from "./CaptureRelease.js"; import type { HistoricalCaptureFormatAdapter } from "./HistoricalCaptureFormatAdapter.js"; import { randomUUID } from "node:crypto"; import { writeFile } from "node:fs/promises"; @@ -613,9 +611,7 @@ const nodeMemoryLimitation = (heap: CaptureDecoderHeap): string => { const recovery = captureDecoderHeapRecovery(heap); if (NODE_CAPTURE_HEAP_ENFORCED) return `HAR/SAZ/qlog use a ${String(heap.oldGenerationMiB)} MiB Node old-generation heap (source ${heap.source}). ${recovery}`; - const commitLimit = captureDecoderProcessMemoryLimitBytes( - heap.oldGenerationMiB, - ); + const commitLimit = javaScriptWorkerMemoryLimitBytes(heap.oldGenerationMiB); return commitLimit === null ? `HAR/SAZ/qlog run on Bun, which does not enforce REA's V8 heap flags. ${recovery}` : `HAR/SAZ/qlog run on Bun, which does not enforce REA's V8 heap flags, under a ${String(commitLimit)}-byte Windows Job Object commit limit. ${recovery}`; diff --git a/src/browser/history/HistoricalCaptureReply.ts b/src/browser/history/HistoricalCaptureReply.ts index 1adad77f90..b2f93710c9 100644 --- a/src/browser/history/HistoricalCaptureReply.ts +++ b/src/browser/history/HistoricalCaptureReply.ts @@ -158,5 +158,6 @@ const excludeNormalizedNumbers = ( projection.numeric_literals = projection.numeric_literals.filter( (number) => !excluded.has(number.pointer), ); - projection.redactions.push(...redaction.redactions); + for (const excludedValue of redaction.redactions) + projection.redactions.push(excludedValue); }; diff --git a/src/browser/history/NodeCaptureAdapter.test.ts b/src/browser/history/NodeCaptureAdapter.test.ts index 1e224a2ef8..e0857eaac6 100644 --- a/src/browser/history/NodeCaptureAdapter.test.ts +++ b/src/browser/history/NodeCaptureAdapter.test.ts @@ -1,4 +1,4 @@ -import { expect, it, vi } from "vitest"; +import { expect, it } from "vitest"; import { AnalysisCapabilityUnavailableError } from "../../domain/analysisErrorCore.js"; import { NodeCaptureAdapter } from "./NodeCaptureAdapter.js"; @@ -9,17 +9,20 @@ it.each(["har", "qlog"] as const)( }, ); -vi.mock("./CaptureRelease.js", async (original) => ({ - ...(await original()), - NODE_CAPTURE_HEAP_ENFORCED: false, - captureDecoderProcessMemoryLimitBytes: () => null, -})); - it.each(["saz", "qlog"] as const)( - "refuses a %s worker when the runtime has no enforceable memory bound", + "uses an enforceable memory bound for %s on the current runtime", async (format) => { - await expect( - new NodeCaptureAdapter(format).command("/owned/request.json"), - ).rejects.toBeInstanceOf(AnalysisCapabilityUnavailableError); + const adapter = new NodeCaptureAdapter(format); + const bun = process.versions.bun !== undefined; + if (bun && process.platform !== "win32") { + await expect( + adapter.command("/owned/request.json"), + ).rejects.toBeInstanceOf(AnalysisCapabilityUnavailableError); + } else { + const command = await adapter.command("/owned/request.json"); + expect(command.command).toBe(process.execPath); + if (bun) expect(command.memoryLimitBytes).toBeGreaterThan(0); + else expect(command.memoryLimitBytes).toBeUndefined(); + } }, ); diff --git a/src/browser/history/NodeCaptureAdapter.ts b/src/browser/history/NodeCaptureAdapter.ts index 6b0a7abd74..490be0c523 100644 --- a/src/browser/history/NodeCaptureAdapter.ts +++ b/src/browser/history/NodeCaptureAdapter.ts @@ -1,3 +1,4 @@ +import { javaScriptWorkerMemoryLimitBytes } from "../../process/JavaScriptWorkerMemoryLimit.js"; import { fileURLToPath } from "node:url"; import type { HistoricalCaptureFormatAdapter } from "./HistoricalCaptureFormatAdapter.js"; import { @@ -6,10 +7,7 @@ import { type CaptureDecoderHeap, } from "../../config/captureDecoderHeap.js"; import { CAPTURE_DECODER_SEMI_SPACE_MIB } from "../../domain/analysisResourceLimits.js"; -import { - NODE_CAPTURE_HEAP_ENFORCED, - captureDecoderProcessMemoryLimitBytes, -} from "./CaptureRelease.js"; +import { NODE_CAPTURE_HEAP_ENFORCED } from "./CaptureRelease.js"; import { QLOG_CAPTURE_PROVIDER_IDENTITY, HAR_CAPTURE_PROVIDER_IDENTITY, @@ -54,7 +52,7 @@ export class NodeCaptureAdapter implements HistoricalCaptureFormatAdapter { : []; } async command(requestPath: string) { - const memoryLimitBytes = captureDecoderProcessMemoryLimitBytes( + const memoryLimitBytes = javaScriptWorkerMemoryLimitBytes( this.heap.oldGenerationMiB, ); if ( diff --git a/src/browser/history/playwrightTrace/PlaywrightTraceLifecycle.test.ts b/src/browser/history/playwrightTrace/PlaywrightTraceLifecycle.test.ts index cd24739c3d..5e9bc74c2e 100644 --- a/src/browser/history/playwrightTrace/PlaywrightTraceLifecycle.test.ts +++ b/src/browser/history/playwrightTrace/PlaywrightTraceLifecycle.test.ts @@ -1,4 +1,5 @@ import { createDeferred } from "../../../../tests/fixtures/binarySession.js"; +import { awaitAcknowledgement } from "../../../../tests/fixtures/lifecycleAcknowledgement.js"; import { readFile, rename, copyFile } from "node:fs/promises"; import { expect, it, vi } from "vitest"; import { ArtifactResourceScope } from "../../../artifacts/ArtifactResourceScope.js"; @@ -240,7 +241,7 @@ it("bounds active imports without an unbounded queue and releases admission afte ); const first = port.inspect(fixture.path); try { - await reached.promise; + await awaitAcknowledgement(reached.promise, first, "trace import"); const second = await port.inspect(fixture.path); if (second.ok) throw new Error("Expected capacity refusal"); expect(second.error._tag).toBe("AnalysisResourceConstraintError"); diff --git a/src/browser/history/protobuf/ProtobufCapture.ts b/src/browser/history/protobuf/ProtobufCapture.ts index 86cb3c2574..2bdd7f658e 100644 --- a/src/browser/history/protobuf/ProtobufCapture.ts +++ b/src/browser/history/protobuf/ProtobufCapture.ts @@ -1,3 +1,4 @@ +import { unescapeJsonPointerToken } from "../../../domain/jsonPointer.js"; import { captureBudgets } from "../CaptureBudgets.js"; import { isAbsolute } from "node:path"; import { CaptureFormatError } from "../CaptureFormatError.js"; @@ -63,10 +64,7 @@ const excludeBody = ( sha256: null, }, ); - const parts = pointer - .split("/") - .slice(1) - .map((part) => part.replaceAll("~1", "/").replaceAll("~0", "~")); + const parts = pointer.split("/").slice(1).map(unescapeJsonPointerToken); let parent: JsonValue | undefined = record.reported; for (const part of parts.slice(0, -1)) { if ( diff --git a/src/browser/network/PlaywrightScenarioNetwork.test.ts b/src/browser/network/PlaywrightScenarioNetwork.test.ts index d570e43fe9..fedc66d8bb 100644 --- a/src/browser/network/PlaywrightScenarioNetwork.test.ts +++ b/src/browser/network/PlaywrightScenarioNetwork.test.ts @@ -256,35 +256,48 @@ it.each([ }, ); -it("reads and redacts marked gzip content its received size bounds", async () => { - const { collector, events, secrets } = harness({ - request_body: false, - response_body: true, - header_values: false, - }); - const request = new RequestFixture("marked"); - const body = '{"token":"secret-token"}'; - request.responseSize = 20; - const response = new ResponseFixture(request, body); - vi.spyOn(response, "headers").mockReturnValue({ - "content-type": "application/json", - "content-encoding": "gzip", - }); - secrets.responseProtector("login", [ - { name: "token", json_pointer: "/token", secret: true }, - ])?.(request); - collector.request(request); - collector.response(response); - collector.finished(request); - await collector.finish(); - const content = events.find( - (event) => event.kind === "network-content" && event.phase === "response", - ); - expect(content).toBeDefined(); - const text = bodyText(content!); - expect(text).not.toContain("secret-token"); - expect(text).toContain("[REDACTED:page:login.token]"); -}); +it.each([ + { size: 20, headers: { "content-encoding": "gzip" } }, + { size: 0, headers: { "content-length": "24" } }, + { size: 0, headers: { "content-length": "0024" } }, + { size: 0, headers: { "content-length": "00000000000000000024" } }, + { + size: 0, + headers: { "content-encoding": "gzip", "content-length": "0024" }, + }, +])( + "reads and redacts marked content within its decoded bound: %j", + async (bounds) => { + const { collector, events, secrets } = harness({ + request_body: false, + response_body: true, + header_values: false, + }); + const request = new RequestFixture("marked"); + const body = '{"token":"secret-token"}'; + request.responseSize = bounds.size; + const response = new ResponseFixture(request, body); + vi.spyOn(response, "headers").mockReturnValue({ + "content-type": "application/json", + ...bounds.headers, + }); + secrets.responseProtector("login", [ + { name: "token", json_pointer: "/token", secret: true }, + ])?.(request); + collector.request(request); + collector.response(response); + collector.finished(request); + await collector.finish(); + const content = events.find( + (event) => event.kind === "network-content" && event.phase === "response", + ); + expect(content).toBeDefined(); + const text = bodyText(content!); + expect(text).not.toContain("secret-token"); + expect(text).toContain("[REDACTED:page:login.token]"); + expect(response.bodyReads).toBe(1); + }, +); it("preserves unselected source spellings while redacting escaped aliases", async () => { const { collector, events, secrets } = harness({ @@ -346,22 +359,28 @@ it("associates sensitive fields by request identity while keeping same-URL unmar ); }); -it.each(["br", "cache"])( - "withholds marked response content when decoded bounds are unknown: %s", - async (kind) => { +it.each([ + { size: 64, headers: { "content-encoding": "br" } }, + { size: 0, headers: {} }, + { size: 0, headers: { "content-length": "0000" } }, + { size: 0, headers: { "content-length": "24, 24" } }, + { size: 0, headers: { "content-length": "0009007199254740993" } }, + { size: 0, headers: { "content-length": "00012000001" } }, +])( + "withholds marked response content when decoded bounds are unknown or over budget: %j", + async (bounds) => { const { collector, events, secrets } = harness(); const request = new RequestFixture("marked"); - if (kind === "cache") request.responseSize = 0; + request.responseSize = bounds.size; const secret = "private-source-token"; const response = new ResponseFixture( request, JSON.stringify({ token: secret }), ); - if (kind === "br") - vi.spyOn(response, "headers").mockReturnValue({ - "content-type": "application/json", - "content-encoding": "br", - }); + vi.spyOn(response, "headers").mockReturnValue({ + "content-type": "application/json", + ...bounds.headers, + }); secrets.responseProtector("login", [ { name: "token", json_pointer: "/token", secret: true }, ])?.(request); diff --git a/src/captures/TsharkCaptureProvider.ts b/src/captures/TsharkCaptureProvider.ts index 7fc4f98232..cde6285ba7 100644 --- a/src/captures/TsharkCaptureProvider.ts +++ b/src/captures/TsharkCaptureProvider.ts @@ -31,6 +31,7 @@ import { import type { PacketCapturePort } from "../application/captures/PacketCaptureService.js"; import { ArtifactReaderFailure } from "../artifacts/ArtifactReader.js"; import { + ARTIFACT_PRODUCER_LIMITATION, ArtifactAnalysisResources, artifactCommandOutput, type ArtifactAnalysisRuntime, @@ -51,7 +52,7 @@ import { AnalysisOutputError, AnalysisResourceConstraintError, } from "../domain/analysisErrorCore.js"; -import { createEvidence } from "../domain/evidence.js"; +import { createAnalysisExecutionEvidence } from "../domain/analysisObservation.js"; import { fixedAllowanceOptions } from "../domain/fixedAllowanceRemediation.js"; import { ProviderAdapterError } from "../domain/providerAdapterError.js"; import { @@ -427,7 +428,7 @@ export class TsharkCaptureProvider implements PacketCapturePort { "Packet limits count input packets before display filtering. Filtered results do not establish whole-capture or device traffic absence. Two-pass reassembly cannot recover bytes missing from the recording or beyond the read limit.", "Repeated field values remain ordered string arrays; raw packet bytes are included only when explicitly selected as a supported field. Export index/score metadata are omitted. All stderr, including warnings on exit zero, is retained.", "A private empty personal configuration is used. Installed libraries, global configuration and dissector plugins are trusted and not fully fingerprinted. File-only analysis and disabled name resolution do not constitute an OS sandbox.", - "Linux prlimit enforces the reported per-process address-space, per-file and CPU limits before execution with no core dumps, preserving tighter inherited soft and hard limits. Input, command deadline and retained output budgets are reported in limits; producer_limits can change their requested defaults on the next call without restart. Output overflow fails. Parser and transport bounds remain independent. These are per-process/per-file limits, not aggregate memory or filesystem quotas; caller-supplied tools and the limiter are trusted.", + ARTIFACT_PRODUCER_LIMITATION, ], }), ); @@ -455,14 +456,12 @@ export class TsharkCaptureProvider implements PacketCapturePort { return this.#resources.finish( result, (execution) => - createEvidence(execution.subject ?? undefined, execution.provider, { + createAnalysisExecutionEvidence( operation, - parameters: input, - result: execution.result, - confidence: "observed", - locations: execution.locations, - limitations: execution.limitations, - }), + input, + execution, + "observed", + ), operation, options?.signal, ); diff --git a/src/cli/androidCommands.ts b/src/cli/androidCommands.ts index f495804a70..03bf927e82 100644 --- a/src/cli/androidCommands.ts +++ b/src/cli/androidCommands.ts @@ -22,7 +22,7 @@ import { parseAnalysisInput } from "../application/AnalysisInput.js"; import { androidJniSelectionSchema } from "../domain/android/androidJniBindings.js"; import { androidJniDeliverySchema } from "../domain/android/androidJniQuery.js"; import { androidInputSchemas } from "../domain/android/androidAnalysis.js"; -import { cliIntegerArgument } from "./options.js"; +import { cliDecimalField } from "./options.js"; /** Expose the same explicit APK requests through one-shot CLI commands. */ export const registerAndroidCommands = ( @@ -223,11 +223,11 @@ export const registerAndroidCommands = ( ), }), options: z.object({ - overloadIndex: cliIntegerArgument(0) - .optional() - .describe( + overloadIndex: cliDecimalField( + androidInputSchemas.inspect_android_method.shape.overload_index.describe( "Explicit zero-based index among same-name methods; required when ambiguous", ), + ), mappingPath, ...analysisEvidenceDeliveryOptions, }), @@ -255,11 +255,11 @@ export const registerAndroidCommands = ( options: z.object({ methodName: androidInputSchemas.trace_android_references.shape.method_name, - overloadIndex: cliIntegerArgument(0) - .optional() - .describe( + overloadIndex: cliDecimalField( + androidInputSchemas.trace_android_references.shape.overload_index.describe( "Index among defined methods or observed external prototypes listed by an ambiguous query", ), + ), }), run: ({ args, options }) => execute( diff --git a/src/cli/cutterCommands.ts b/src/cli/cutterCommands.ts index 258175c4dd..d2eac140bc 100644 --- a/src/cli/cutterCommands.ts +++ b/src/cli/cutterCommands.ts @@ -8,7 +8,8 @@ import { logCliCommand } from "../cliLogging.js"; import { finishAnalysisCommand } from "./finishAnalysisCommand.js"; import { withCommandCancellation } from "./commandCancellation.js"; import type { CliInstance } from "./types.js"; -import { cliIntegerArgument } from "./options.js"; +import { cliDecimalField } from "./options.js"; +import { cutterCommandInputSchema } from "../contracts/cutterToolContracts.js"; /** Register commands that control an already-running Cutter instance. */ export const registerCutterCommands = ( @@ -43,8 +44,10 @@ export const registerCutterCommands = ( .string() .uuid() .describe("Session ID copied from list-cutter-sessions."), - expectedGeneration: cliIntegerArgument(0).describe( - "Session generation returned by list-cutter-sessions.", + expectedGeneration: cliDecimalField( + cutterCommandInputSchema.shape.expected_generation.describe( + "Session generation returned by list-cutter-sessions.", + ), ), command: z .string() diff --git a/src/cli/environmentCommands.ts b/src/cli/environmentCommands.ts index 68159b4e45..080b08d418 100644 --- a/src/cli/environmentCommands.ts +++ b/src/cli/environmentCommands.ts @@ -11,7 +11,10 @@ import type { ServedState } from "../application/environment/environmentState.js import { z } from "incur"; import type { Logger } from "pino"; -import { CLI_COMMANDS } from "../cliCommandNames.js"; +import { + CLI_COMMANDS, + DEFAULT_INFO_LOGGING_COMMANDS, +} from "../cliCommandNames.js"; import { parseCliJsonInput } from "../cliJsonInput.js"; import { logCliCommand } from "../cliLogging.js"; import { parseEnvironmentName } from "../config/environmentName.js"; @@ -38,7 +41,7 @@ import { inspectWebNetworkCaptureInputSchema } from "../domain/webNetworkCapture import { withCommandCancellation } from "./commandCancellation.js"; import { finishAnalysisCommand } from "./finishAnalysisCommand.js"; import type { CliInstance } from "./types.js"; -import { cliDecimalField } from "./options.js"; +import { cliDecimalField, publishArgvInput } from "./options.js"; /** Same envelope label as the other JSON-input commands and `parseCliJsonInput`. */ const cliError = (error: AnalysisError): JsonValue => ({ @@ -48,10 +51,12 @@ const cliError = (error: AnalysisError): JsonValue => ({ /** The listener announces its URL, and a build its stages, at info like `rea mcp --http`; an explicit REA_LOG_LEVEL still wins. */ const infoLogger = ( + command: string, environment: Readonly, logger: Logger, ): Logger => - environment.REA_LOG_LEVEL === undefined + environment.REA_LOG_LEVEL === undefined && + DEFAULT_INFO_LOGGING_COMMANDS.has(command) ? createLogger("cli", "info") : logger; @@ -179,7 +184,11 @@ const serveEnvironmentCommand = async ( source, served: { state, report }, } = inputs.value; - const commandLogger = infoLogger(environment, logger); + const commandLogger = infoLogger( + CLI_COMMANDS.environmentServe, + environment, + logger, + ); if (!report.served && report.reason === "unavailable") commandLogger.warn( { code: report.code, error: report.message }, @@ -376,7 +385,7 @@ const inspectBuildHar = async ( inspected.value, ENVIRONMENT_BUILD_OPERATION, ); - return file.ok ? ok(file.value) : err(cliError(file.error)); + return file.ok ? file : err(cliError(file.error)); }; /** Read the capture once, then build or plan the bundle from it. */ @@ -411,7 +420,11 @@ const buildEnvironmentCommand = async ( ], ), ); - const commandLogger = infoLogger(environment, logger); + const commandLogger = infoLogger( + CLI_COMMANDS.environmentBuild, + environment, + logger, + ); const progress = (stage: string): void => { commandLogger.info({ stage }, "Building environment"); }; @@ -689,11 +702,13 @@ export const registerEnvironmentCommands = ( host: mcpHttpHostSchema .optional() .describe("Listener address; default MCP_HOST, else 127.0.0.1"), - port: mcpHttpPortSchema - .optional() - .describe( - "Listener port; default MCP_PORT, else 0 for an ephemeral port", - ), + port: publishArgvInput( + mcpHttpPortSchema + .optional() + .describe( + "Listener port; default MCP_PORT, else 0 for an ephemeral port", + ), + ), }), run: ({ args, options }) => withCommandCancellation((signal) => diff --git a/src/cli/options.ts b/src/cli/options.ts index 5e4d1773c8..2f5645cae9 100644 --- a/src/cli/options.ts +++ b/src/cli/options.ts @@ -24,13 +24,6 @@ type DecimalWrapper = readonly kind: "optional" | "exactOptional" | "nullable" | "readonly"; }; -/** Parse decimal integer tokens before Number coercion can erase their syntax. */ -export const cliIntegerArgument = (min: number) => - z - .string() - .regex(DECIMAL_INTEGER_TOKEN, "Expected a decimal integer") - .pipe(z.coerce.number().safe().min(min)); - const decimalToken = (domain: z.ZodNumber) => z .string() @@ -42,10 +35,12 @@ const unwrapSchema = (field: { unwrap: () => unknown }): z.ZodType => field.unwrap() as z.ZodType; /** - * Incur advertises Zod's output view. Publish the input view so the decimal - * token guard survives `--schema` and other SDK conversions. + * Incur advertises Zod's output view. Publish the input view so an argv token + * grammar that transforms text survives `--schema` and other SDK conversions. */ -const publishDecimalInput = (field: z.ZodType): z.ZodType => { +export const publishArgvInput = ( + field: Field, +): Field => { const { $schema: _dialect, ...inputSchema } = z.toJSONSchema(field, { io: "input", }); @@ -149,7 +144,7 @@ export const cliDecimalField = ( numeric.description === undefined ? admitted : admitted.describe(numeric.description); - return publishDecimalInput(described) as z.ZodType>; + return publishArgvInput(described) as z.ZodType>; }; /** Adapt each numeric property of a shared shape; other properties stay unchanged. */ diff --git a/src/cli/reverseEngineeringCommands.ts b/src/cli/reverseEngineeringCommands.ts index 186660ab45..936b2e2295 100644 --- a/src/cli/reverseEngineeringCommands.ts +++ b/src/cli/reverseEngineeringCommands.ts @@ -13,7 +13,7 @@ import { import { evidenceSchema, type Evidence } from "../domain/evidence.js"; import type { JsonValue } from "../domain/jsonValue.js"; import type { Result } from "../domain/result.js"; -import { err, ok } from "../domain/result.js"; +import { ok } from "../domain/result.js"; import { projectAnalysisError } from "../domain/analysisErrorProjection.js"; import { OBJDUMP_ARTIFACT_OPERATIONS, @@ -80,7 +80,7 @@ const cleanupOnlyCompleteEvidence = ( return typeof normalized === "object" && normalized !== null && !Array.isArray(normalized) && - normalized.completion_status === "complete" + normalized.completion_status !== "unknown" ? parsed.data : undefined; }; @@ -373,16 +373,14 @@ export const registerReverseEngineeringCommands = ( const started = await manager.start(signal, { inferiorTty: options.inferiorTty, }); - if (!started.ok) return err(started.error); + if (!started.ok) return started; sessionId = started.value.session_id; const executed = await manager.execute( sessionId, args.command, signal, ); - return executed.ok - ? ok(executed.value.evidence) - : err(executed.error); + return executed.ok ? ok(executed.value.evidence) : executed; }, service: { close: () => @@ -445,16 +443,14 @@ export const registerReverseEngineeringCommands = ( }, signal, ); - if (!started.ok) return err(started.error); + if (!started.ok) return started; sessionId = started.value.session_id; const executed = await manager.execute( sessionId, args.command, signal, ); - return executed.ok - ? ok(executed.value.evidence) - : err(executed.error); + return executed.ok ? ok(executed.value.evidence) : executed; }, { close: () => diff --git a/src/cli/setupCommands.ts b/src/cli/setupCommands.ts index 17692c3bc5..7aef3d4007 100644 --- a/src/cli/setupCommands.ts +++ b/src/cli/setupCommands.ts @@ -12,6 +12,7 @@ import { } from "../application/SetupTypes.js"; import { isUninstallFailure } from "../application/UninstallFailure.js"; import { homeDirectoryFromEnvironment } from "../config/homeDirectory.js"; +import { localPathStringSchema } from "../domain/localPath.js"; import { mcpToolFamiliesSchema, toolFamiliesConstraint, @@ -144,7 +145,9 @@ const registerDoctorCommand = ( cli.command(CLI_COMMANDS.doctor, { description: "Check whether REA is ready", options: z.object({ - target: z.string().optional().describe("Optional app path to check"), + target: localPathStringSchema + .optional() + .describe("Optional app path to check"), client: z .array(supportedClientSchema) .default([]) diff --git a/src/cli/utilityCommands.ts b/src/cli/utilityCommands.ts index 45680f2962..c431b3cf4e 100644 --- a/src/cli/utilityCommands.ts +++ b/src/cli/utilityCommands.ts @@ -1,6 +1,6 @@ import { projectCliAnalysisResult } from "./projectCliAnalysisResult.js"; import { z } from "incur"; -import { cliDecimalField, cliIntegerArgument } from "./options.js"; +import { cliDecimalField } from "./options.js"; import type { DirectAnalysis } from "../composition/directAnalysis.js"; import { createHistoricalSourceSummary } from "../domain/referenceSourceSummary.js"; @@ -21,7 +21,10 @@ import { } from "../domain/analysisErrorCore.js"; import { projectInputIssues } from "../domain/inputIssueProjection.js"; import { safeParseJson } from "../domain/safeJson.js"; -import { nativeUiScenarioInputSchema } from "../domain/native/nativeUiObservation.js"; +import { + nativeUiObservationInputSchema, + nativeUiScenarioInputSchema, +} from "../domain/native/nativeUiObservation.js"; import { nativeCommandTimeoutField, nativeUiDeadlineField, @@ -49,15 +52,16 @@ export const registerUtilityCommands = ( registerReferenceSourceCommand(cli, logger, environment); }; -/** Decimal argv text; the shared deadline field owns the bounds and help. */ -const cliNativeDeadline = (field: typeof nativeUiDeadlineField) => - cliDecimalField(field); - -// Decimal tokens only, for the same reason as cliNativeDeadline. +// Decimal tokens only; shared fields own their bounds and defaults. +const nativeUiScopeFields = nativeUiObservationInputSchema.shape; const nativeUiScopeOptions = { - pid: cliIntegerArgument(1).describe("Existing application PID"), - windowId: cliIntegerArgument(1).describe( - "Exact selected application window ID", + pid: cliDecimalField( + nativeUiScopeFields.pid.describe("Existing application PID"), + ), + windowId: cliDecimalField( + nativeUiScopeFields.window_id.describe( + "Exact selected application window ID", + ), ), screenshot: z .boolean() @@ -67,10 +71,12 @@ const nativeUiScopeOptions = { .boolean() .default(true) .describe("Read the selected window accessibility tree"), - maxNodes: cliIntegerArgument(1) - .default(500) - .describe("Maximum accessibility nodes per capture"), - nativeUiDeadlineMs: cliNativeDeadline(nativeUiDeadlineField), + maxNodes: cliDecimalField( + nativeUiScopeFields.max_nodes.describe( + "Maximum accessibility nodes per capture", + ), + ), + nativeUiDeadlineMs: cliDecimalField(nativeUiDeadlineField), }; const nativeUiScope = (options: { @@ -208,7 +214,7 @@ const registerNativeCommands = ( path: localPathStringSchema.describe("Mach-O or app path"), }), options: z.object({ - nativeCommandTimeoutMs: cliNativeDeadline(nativeCommandTimeoutField), + nativeCommandTimeoutMs: cliDecimalField(nativeCommandTimeoutField), }), run: ({ args, options }) => logCliCommand(logger, command, () => @@ -235,7 +241,7 @@ const registerNativeCommands = ( .describe( "Plist path relative to the app root, or an absolute path (default: the bundle's Info.plist)", ), - nativeCommandTimeoutMs: cliNativeDeadline(nativeCommandTimeoutField), + nativeCommandTimeoutMs: cliDecimalField(nativeCommandTimeoutField), }), run: ({ args, options }) => logCliCommand(logger, "inspect-plist", () => @@ -263,7 +269,7 @@ const registerNativeCommands = ( symbols: swiftSymbolsSchema.describe("Swift mangled symbols to demangle"), }), options: z.object({ - nativeCommandTimeoutMs: cliNativeDeadline(nativeCommandTimeoutField), + nativeCommandTimeoutMs: cliDecimalField(nativeCommandTimeoutField), }), run: ({ args, options }) => logCliCommand(logger, "demangle-swift", () => @@ -368,11 +374,15 @@ const registerReferenceSourceCommand = ( .describe( "Complete committed graph or compact historical-source summary", ), - outlineDepth: cliIntegerArgument(0) - .optional() - .describe( - "Summary outline path depth (default: 2; 0 omits entries); requires --detail summary", - ), + outlineDepth: cliDecimalField( + z + .int() + .min(0) + .optional() + .describe( + "Summary outline path depth (default: 2; 0 omits entries); requires --detail summary", + ), + ), }) .superRefine((options, context) => { if (options.outlineDepth !== undefined && options.detail !== "summary") diff --git a/src/cli/webModuleCommands.ts b/src/cli/webModuleCommands.ts index cef3f31a3a..2611fd926b 100644 --- a/src/cli/webModuleCommands.ts +++ b/src/cli/webModuleCommands.ts @@ -6,7 +6,7 @@ import { logCliCommand } from "../cliLogging.js"; import { webModuleTraceInputSchema } from "../domain/webModuleTrace.js"; import type { Logger } from "pino"; import type { CliInstance } from "./types.js"; -import { cliIntegerArgument } from "./options.js"; +import { cliDecimalField } from "./options.js"; import { withCommandCancellation } from "./commandCancellation.js"; import { finishAnalysisCommand } from "./finishAnalysisCommand.js"; @@ -25,8 +25,10 @@ export const registerWebModuleCommands = ( "Trace one captured script's native imports to URLs and captured source candidates", args: z.object({ manifestPath: webModuleTraceInputSchema.shape.manifest_path, - scriptIndex: cliIntegerArgument(0).describe( - "Zero-based manifest script index", + scriptIndex: cliDecimalField( + webModuleTraceInputSchema.shape.script_index.describe( + "Zero-based manifest script index", + ), ), }), options: z.object({ diff --git a/src/cli/webNetworkCaptureCommands.ts b/src/cli/webNetworkCaptureCommands.ts index 5a33258feb..b90494eed3 100644 --- a/src/cli/webNetworkCaptureCommands.ts +++ b/src/cli/webNetworkCaptureCommands.ts @@ -8,7 +8,7 @@ import { CLI_COMMANDS } from "../cliCommandNames.js"; import { logCliCommand } from "../cliLogging.js"; import type { Logger } from "pino"; import type { CliInstance } from "./types.js"; -import { cliDecimalField, cliIntegerArgument } from "./options.js"; +import { cliDecimalField } from "./options.js"; import { withCommandCancellation } from "./commandCancellation.js"; import { analysisEvidenceDeliveryOptions, @@ -18,6 +18,9 @@ import { localPathStringSchema } from "../domain/localPath.js"; import { absoluteCallerPath } from "../filesystem/callerPath.js"; import { explicitSensitiveValueOptions } from "./explicitSensitiveArguments.js"; +const recordOrdinal = + inspectWebNetworkCaptureInputSchema.shape.record_ordinals.unwrap().element; + /** Expose offline historical capture inspection with the same selection contract as MCP. */ export const registerWebNetworkCaptureCommands = ( cli: CliInstance, @@ -51,7 +54,7 @@ export const registerWebNetworkCaptureCommands = ( .optional() .describe("Fully qualified message type in the descriptor set"), record: z - .array(cliIntegerArgument(0)) + .array(cliDecimalField(recordOrdinal)) .optional() .describe("Zero-based producer ordinals; repeat to select several"), ...explicitSensitiveValueOptions, diff --git a/src/cli/webRuntimeCommands.ts b/src/cli/webRuntimeCommands.ts index ef4a2020c7..d40a70d8da 100644 --- a/src/cli/webRuntimeCommands.ts +++ b/src/cli/webRuntimeCommands.ts @@ -4,7 +4,6 @@ import { createWebRuntimeService } from "../composition/webRuntime.js"; import { cliDomainInput } from "./domainInput.js"; import { observeWebExecutionInputSchema } from "../domain/webExecution.js"; import { inspectWebEventListenersInputSchema } from "../domain/webEventListeners.js"; -import { browserCliError } from "./browserCliError.js"; import { browserScopeOptions, selectedCommandTimeout, diff --git a/src/cli/webSourceLocationCommands.ts b/src/cli/webSourceLocationCommands.ts index 3f894fccf7..a5612c6220 100644 --- a/src/cli/webSourceLocationCommands.ts +++ b/src/cli/webSourceLocationCommands.ts @@ -16,10 +16,11 @@ import { CLI_COMMANDS } from "../cliCommandNames.js"; import { logCliCommand } from "../cliLogging.js"; import type { Logger } from "pino"; import type { CliInstance } from "./types.js"; -import { cliDecimalField, cliIntegerArgument } from "./options.js"; +import { cliDecimalField } from "./options.js"; import { withCommandCancellation } from "./commandCancellation.js"; const sourceMapSelection = webSourceLocationInputSchema.shape.source_map; +const generatedPosition = webSourceLocationInputSchema.shape.generated_position; /** Expose the same explicit selected map/point contract through the CLI. */ export const registerWebSourceLocationCommands = ( @@ -33,14 +34,20 @@ export const registerWebSourceLocationCommands = ( "Trace one retained script position through an explicitly selected local source map", args: z.object({ manifestPath: webSourceLocationInputSchema.shape.manifest_path, - scriptIndex: cliIntegerArgument(0).describe( - "Zero-based manifest script index", + scriptIndex: cliDecimalField( + webSourceLocationInputSchema.shape.script_index.describe( + "Zero-based manifest script index", + ), ), sourceMapPath: sourceMapSelection.shape.path, sourceMapUrl: sourceMapSelection.shape.url, - line: cliIntegerArgument(1).describe("One-based generated line"), - column: cliIntegerArgument(0).describe( - "Zero-based UTF-16 generated column", + line: cliDecimalField( + generatedPosition.shape.line.describe("One-based generated line"), + ), + column: cliDecimalField( + generatedPosition.shape.column.describe( + "Zero-based UTF-16 generated column", + ), ), }), options: z.object({ diff --git a/src/cliCommandNames.ts b/src/cliCommandNames.ts index 892978fa7b..34602b4962 100644 --- a/src/cliCommandNames.ts +++ b/src/cliCommandNames.ts @@ -201,3 +201,12 @@ export const CLI_COMMANDS = Object.freeze({ /** Ordered primary CLI inventory; aliases are intentionally excluded. */ export const CLI_COMMAND_NAMES = Object.freeze(Object.values(CLI_COMMANDS)); + +/** + * Commands that announce their progress at info level when REA_LOG_LEVEL is + * unset, like `rea mcp --http`. Their logger writes to stderr directly. + */ +export const DEFAULT_INFO_LOGGING_COMMANDS: ReadonlySet = new Set([ + CLI_COMMANDS.environmentServe, + CLI_COMMANDS.environmentBuild, +]); diff --git a/src/cliJsonInput.ts b/src/cliJsonInput.ts index fcdc101f47..e90c8b07a6 100644 --- a/src/cliJsonInput.ts +++ b/src/cliJsonInput.ts @@ -35,7 +35,6 @@ export const parseCliJsonInput = async ( options?: { readonly snapshot?: boolean; readonly stdin?: Readable; - readonly maxStdinBytes?: number; }, ): Promise< | { readonly ok: true; readonly value: unknown } @@ -132,13 +131,12 @@ const readCliJsonStdin = async ( options?: { readonly snapshot?: boolean; readonly stdin?: Readable; - readonly maxStdinBytes?: number; }, ): Promise< Result > => { const stream = options?.stdin ?? process.stdin; - const maxBytes = options?.maxStdinBytes ?? DEFAULT_MAX_STDIN_BYTES; + const maxBytes = DEFAULT_MAX_STDIN_BYTES; const chunks: Buffer[] = []; let totalBytes = 0; const requireHeadroom = (): void => { diff --git a/src/cliOneShot.ts b/src/cliOneShot.ts new file mode 100644 index 0000000000..49c6148f9d --- /dev/null +++ b/src/cliOneShot.ts @@ -0,0 +1,169 @@ +import type { Writable } from "node:stream"; + +import { + cliCompletionShell, + isProjectScopedAmpRegistration, + normalizeCliArguments, + splitIncurBuiltinArguments, +} from "./cliBuiltinArguments.js"; +import { + cliConfigurationError, + parseCliOutputArguments, + renderCliOutputArgumentError, + renderEmptyFilteredCliOutput, + renderVersionReport, +} from "./cliOutput.js"; +import type { VersionReportHost } from "./domain/versionReport.js"; + +/** + * Process facilities one command needs. The executable binds them to its own + * streams and exit; a driver that serves several commands from one process + * binds them to per-command capture. + */ +export interface OneShotCliHost { + readonly environment: NodeJS.ProcessEnv; + readonly runtime: VersionReportHost; + readonly stdoutIsTerminal: boolean; + /** Destination for result documents the command streams itself. */ + readonly resultDestination: Writable; + /** Status reported after a recovered failure when the reader has closed stdout. */ + readonly closedStdoutStatus: number; + writeStdout(text: string): void; + writeStderr(text: string): void; + stdoutClosed(): boolean; + /** Status the process would report if it ended now. */ + exitCode(): number; + setExitCode(code: number): void; + /** Called once when the command runtime loads, before any command runs. */ + ownCliResources(close: () => Promise): void; + /** + * The command asked to end the process with `code`. The host owns that + * exit and must call `finishOutput` with the status it reports. + */ + exit(code: number, finishOutput: (status: number) => Promise): void; + /** Whether `exit` has run. Its host then owns the pending result write. */ + exitRequested(): boolean; +} + +const AMP_PROJECT_SCOPE_REFUSAL = + "Amp does not support project-scoped registration. Run without --no-global to register Amp globally, or select a client with project settings. No client settings were changed."; + +/** + * Run one CLI command: admit its output arguments and configuration, answer + * the documents that need no command runtime, then dispatch. Failures the + * command did not report propagate to the host. + */ +export const runOneShotCli = async ( + arguments_: readonly string[], + host: OneShotCliHost, +): Promise => { + const isShellCompletion = cliCompletionShell(host.environment) !== undefined; + const cliArguments = isShellCompletion + ? [...arguments_] + : normalizeCliArguments([...arguments_]); + // Completion words are transport data, including partial output flags. + const outputOptions = parseCliOutputArguments( + isShellCompletion ? [] : cliArguments, + ); + const outputArguments = outputOptions.validation; + // `--version` with an explicit format reports the runtime as a document; + // plain `--version` stays Incur's bare version string. + const versionOutput = renderVersionReport( + outputOptions, + host.runtime, + host.stdoutIsTerminal, + ); + const configurationError = cliConfigurationError(host.environment); + // Doctor is the readiness report for this failure. Other commands still stop + // here so they do not run with an unusable log level. + if ( + configurationError !== undefined && + (isShellCompletion || + splitIncurBuiltinArguments(cliArguments).command[0] !== "doctor") + ) { + host.writeStderr(`${configurationError}\n`); + host.setExitCode(1); + return; + } + if (!outputArguments.ok) { + host.writeStdout(renderCliOutputArgumentError(outputArguments)); + host.setExitCode(1); + return; + } + if (versionOutput !== undefined) { + host.writeStdout(versionOutput); + return; + } + if (!isShellCompletion && isProjectScopedAmpRegistration(cliArguments)) { + // Amp has global settings only. Share the parsed scope fact with the + // registration workflow and refuse before loading the command runtime. + // + // A caller that asked for a structured document gets the refusal in + // that document, through the same pre-execution error renderer the + // output-argument branch uses; anything else keeps the plain message. + const refusalFormat = outputOptions.format; + if ( + refusalFormat === "json" || + refusalFormat === "jsonl" || + refusalFormat === "yaml" + ) { + host.writeStdout( + renderCliOutputArgumentError({ + format: refusalFormat, + code: "AMP_PROJECT_SCOPE_UNSUPPORTED", + message: AMP_PROJECT_SCOPE_REFUSAL, + }), + ); + } else { + host.writeStderr(`${AMP_PROJECT_SCOPE_REFUSAL}\n`); + } + host.setExitCode(1); + return; + } + + // Loaded only now: the documents above answer without the command catalog. + const { closeCliResources, createCli } = await import("./cli.js"); + host.ownCliResources(closeCliResources); + const { createCliResultOutput } = await import("./cli/cliResultOutput.js"); + let wroteOutput = false; + const resultOutput = createCliResultOutput( + outputOptions, + host.resultDestination, + ); + try { + await createCli(host.environment, resultOutput).serve(cliArguments, { + stdout: (output) => { + if (resultOutput?.handled) { + if (resultOutput.failed && !host.stdoutClosed()) + host.writeStderr(output); + return; + } + if (output.length > 0) wroteOutput = true; + if (resultOutput?.captureSdkOutput(output)) return; + host.writeStdout(output); + }, + exit: (code) => { + host.exit(typeof code === "number" ? code : 0, async (status) => + resultOutput?.finishSdkOutput(status), + ); + }, + }); + } catch (cause) { + if (wroteOutput) throw cause; + const recovered = await resultOutput + ?.recoverFormattingFailure(cause) + .catch((writeFailure: unknown) => { + // Recovery writes outside Incur's command boundary. A reader that + // declined this document keeps the same quiet closed-pipe outcome. + if (!host.stdoutClosed()) throw writeFailure; + return true; + }); + if (!recovered) throw cause; + host.setExitCode(host.stdoutClosed() ? host.closedStdoutStatus : 1); + } + if (!wroteOutput && !resultOutput?.handled) + host.writeStdout(renderEmptyFilteredCliOutput(outputOptions) ?? ""); + // A requested exit is finished by the host, which owns its pending write. + if (!host.exitRequested()) + await resultOutput?.finishSdkOutput(host.exitCode()); +}; diff --git a/src/cliOutput.ts b/src/cliOutput.ts index 045e9cbc22..928961a39b 100644 --- a/src/cliOutput.ts +++ b/src/cliOutput.ts @@ -366,9 +366,18 @@ export const renderEmptyFilteredCliOutput = ({ return undefined; }; +/** A refusal reported as a structured document before any command runs. */ +export interface CliPreExecutionError { + readonly format: StructuredOutputFormat; + readonly code: + | Exclude["code"] + | "AMP_PROJECT_SCOPE_UNSUPPORTED"; + readonly message: string; +} + /** Render one complete structured error before command execution. */ export const renderCliOutputArgumentError = ( - error: Exclude, + error: CliPreExecutionError, ): string => { const value = { ok: false, diff --git a/src/composition/resolveWindowsProviderExecutable.ts b/src/composition/resolveWindowsProviderExecutable.ts index bb5f175882..dc255939ae 100644 --- a/src/composition/resolveWindowsProviderExecutable.ts +++ b/src/composition/resolveWindowsProviderExecutable.ts @@ -1,4 +1,4 @@ -import { existsSync, statSync } from "node:fs"; +import { statSync } from "node:fs"; import { win32 } from "node:path"; import { windowsPathEntries } from "../process/WindowsExecutable.js"; @@ -11,7 +11,7 @@ export interface WindowsExecutableLookup { const isRegularFile = (path: string): boolean => { try { - return existsSync(path) && statSync(path).isFile(); + return statSync(path).isFile(); } catch { return false; } diff --git a/src/composition/reverseEngineering.ts b/src/composition/reverseEngineering.ts index 6394dc1550..301c65f1aa 100644 --- a/src/composition/reverseEngineering.ts +++ b/src/composition/reverseEngineering.ts @@ -6,6 +6,7 @@ import { OBJDUMP_PROVIDER_IDENTITY } from "../objdump/ObjdumpProviderIdentity.js import { rizinCommand, rizinCommandLimitations, + rizinCommandRejection, } from "../rizin/RizinCommand.js"; import { RIZIN_PROVIDER_IDENTITY } from "../rizin/RizinProviderIdentity.js"; import { resolveWindowsProviderExecutable } from "./resolveWindowsProviderExecutable.js"; @@ -32,6 +33,7 @@ export const createReverseEngineeringCommandPort = ( ), arguments: rizinCommand(input), limitations: rizinCommandLimitations(input), + rejection: rizinCommandRejection, identity: RIZIN_PROVIDER_IDENTITY, }), }); diff --git a/src/composition/webBundleAnalysisWorkerProcess.ts b/src/composition/webBundleAnalysisWorkerProcess.ts index c10b8203fd..ae0f9b6737 100644 --- a/src/composition/webBundleAnalysisWorkerProcess.ts +++ b/src/composition/webBundleAnalysisWorkerProcess.ts @@ -1,14 +1,8 @@ import { analyzeCapturedWebScript } from "../domain/webBundleAnalyzer.js"; -import { - serveWebBundleAnalysisWorker, - type WebBundleScriptAnalyzer, -} from "../browser/WebBundleAnalysisWorkerServer.js"; - -const analyze: WebBundleScriptAnalyzer = (script, precision) => - analyzeCapturedWebScript(script, precision); +import { serveWebBundleAnalysisWorker } from "../browser/WebBundleAnalysisWorkerServer.js"; try { - await serveWebBundleAnalysisWorker(analyze); + await serveWebBundleAnalysisWorker(analyzeCapturedWebScript); } catch (cause: unknown) { process.stderr.write( `${cause instanceof Error ? cause.message : String(cause)}\n`, diff --git a/src/config/regexWorkerHeapRequest.test.ts b/src/config/regexWorkerHeapRequest.test.ts index ad245270b5..e5fa9c411b 100644 --- a/src/config/regexWorkerHeapRequest.test.ts +++ b/src/config/regexWorkerHeapRequest.test.ts @@ -16,7 +16,6 @@ import { resolveRegexWorkerHeapRequest, REGEX_WORKER_HEAP_REQUEST_SETTING, regexWorkerHeapRequestContinuingRecovery, - regexWorkerHeapRequestRecovery, } from "./regexWorkerHeapRequest.js"; import { MAXIMUM_REGEX_WORKER_HEAP_REQUEST_MIB, diff --git a/src/contracts/erlangBeamDeliveryContracts.test.ts b/src/contracts/bytecodeDeliveryContracts.test.ts similarity index 61% rename from src/contracts/erlangBeamDeliveryContracts.test.ts rename to src/contracts/bytecodeDeliveryContracts.test.ts index 4a8f4a670e..6b91f4e753 100644 --- a/src/contracts/erlangBeamDeliveryContracts.test.ts +++ b/src/contracts/bytecodeDeliveryContracts.test.ts @@ -4,16 +4,73 @@ import { expect, it } from "vitest"; import { toolContract } from "./toolContracts.js"; import { toolInputSchemaWithMetadata } from "./toolSchemaMetadata.js"; -const view = { - kind: "beam", - collection: "exports", - fields: ["function"], +const beamView = { kind: "beam", collection: "exports", fields: ["function"] }; +const pycView = { + kind: "python-pyc", + collection: "code_objects", + fields: ["name"], }; -it.each(["draft-2020-12", "draft-07"] as const)( - "advertises BEAM delivery combinations in %s", - (target) => { - const contract = toolContract("inspect_erlang_beam"); +const cases = [ + { + name: "inspect_erlang_beam", + path: "/tmp/module.beam", + view: beamView, + extra: [ + [{ detail: "summary", view: { ...beamView, fields: ["name"] } }, false], + [ + { detail: "summary", view: { ...beamView, modules: ["erlang"] } }, + false, + ], + [ + { + detail: "summary", + view: { ...beamView, collection: "imports", modules: ["erlang"] }, + }, + true, + ], + [ + { + detail: "summary", + view: { ...beamView, collection: "chunks", fields: ["payload"] }, + }, + true, + ], + [ + { + detail: "summary", + view: { ...beamView, collection: "atoms", fields: ["index", "name"] }, + }, + true, + ], + [ + { + detail: "summary", + view: { ...beamView, collection: "lambdas", fields: ["free_vars"] }, + }, + true, + ], + ], + }, + { + name: "inspect_python_pyc", + path: "/tmp/module.pyc", + view: pycView, + extra: [], + }, +] as const; + +it.each( + cases.flatMap((testCase) => + (["draft-2020-12", "draft-07"] as const).map((target) => ({ + ...testCase, + target, + })), + ), +)( + "advertises $name delivery combinations in $target", + ({ name, path, view, extra, target }) => { + const contract = toolContract(name); const projected = toolInputSchemaWithMetadata(contract)[ "~standard" ].jsonSchema.input({ target }); @@ -43,38 +100,9 @@ it.each(["draft-2020-12", "draft-07"] as const)( [{ detail: "summary", view: { ...view, fields: ["missing"] } }, false], [{ detail: "summary", view: { ...view, kind: "native-exports" } }, false], [{ detail: "summary", view, unexpected: true }, false], - [{ detail: "summary", view: { ...view, fields: ["name"] } }, false], - [{ detail: "summary", view: { ...view, modules: ["erlang"] } }, false], - [ - { - detail: "summary", - view: { ...view, collection: "imports", modules: ["erlang"] }, - }, - true, - ], - [ - { - detail: "summary", - view: { ...view, collection: "chunks", fields: ["payload"] }, - }, - true, - ], - [ - { - detail: "summary", - view: { ...view, collection: "atoms", fields: ["index", "name"] }, - }, - true, - ], - [ - { - detail: "summary", - view: { ...view, collection: "lambdas", fields: ["free_vars"] }, - }, - true, - ], + ...extra, ] as const) { - const input = { path: "/tmp/module.beam", ...delivery }; + const input = { path, ...delivery }; expect( contract.inputSchema.safeParse(input).success, JSON.stringify(input), diff --git a/src/contracts/decompileDirectory.ts b/src/contracts/decompileDirectory.ts index 0750a1be85..376d8de71b 100644 --- a/src/contracts/decompileDirectory.ts +++ b/src/contracts/decompileDirectory.ts @@ -215,7 +215,9 @@ export const decompileDirectoryManifestSchema = z ), limitations: z .array(z.string()) - .describe("Limits of this export. Pseudocode is not included."), + .describe( + "Limits of this export directory. The body-coverage caveat counts every retained function record, including records earlier runs wrote. Pseudocode is not included.", + ), resume: z.boolean().describe("Whether this call continued a journal."), }) .superRefine((manifest, context) => { @@ -366,7 +368,11 @@ export const decompileDirectoryIndexSchema = z.strictObject({ .describe( "Selection and inventory limitations owned by the run that last rewrote this index. Absent in indexes written before this field existed.", ), - limitations: z.array(z.string()), + limitations: z + .array(z.string()) + .describe( + "Limits of this export directory. The body-coverage caveat counts every retained function record, including records earlier runs wrote.", + ), functions: z.array(decompileDirectoryFunctionSchema), }); diff --git a/src/contracts/desktopBuildToolContracts.ts b/src/contracts/desktopBuildToolContracts.ts index 813b9cef55..6701c64565 100644 --- a/src/contracts/desktopBuildToolContracts.ts +++ b/src/contracts/desktopBuildToolContracts.ts @@ -10,8 +10,6 @@ import { MAX_BUILD_STATE_PATHS, SEED_CHECKPOINT, } from "../domain/desktop/buildCapture.js"; -import { draftNoteSchema } from "../domain/desktop/appTaskDraftNotes.js"; -import { draftValueSchema } from "../domain/desktop/appTaskDraftValues.js"; import { WITNESS_STAGES } from "../domain/desktop/buildWitness.js"; import { MEASURE_BUILD_TASK_CONTRACT } from "./desktopBuildMeasureContract.js"; import { diff --git a/src/contracts/gdbToolContracts.ts b/src/contracts/gdbToolContracts.ts index 418abc4e9a..ee15fc43ec 100644 --- a/src/contracts/gdbToolContracts.ts +++ b/src/contracts/gdbToolContracts.ts @@ -63,7 +63,7 @@ export const GDB_TOOL_CONTRACTS = [ ...toolContractMetadata("gdb_session_status"), kind: "native-provider", description: - "Return the observed lifecycle state and bounded recent MI records and process diagnostics for one persistent REA GDB session; truncation and invalid-UTF-8 fields report discarded or lossy data.", + "Return the observed lifecycle state and bounded recent MI records and process diagnostics for one persistent REA GDB session; closed_reason says why a closed session stopped, and truncation and invalid-UTF-8 fields report discarded or lossy data.", inputSchema: z.strictObject({ session_id: sessionId }), outputSchema: z.strictObject({ session_id: sessionId, @@ -79,6 +79,12 @@ export const GDB_TOOL_CONTRACTS = [ diagnostics_truncated: z.boolean(), inferior_tty: z.string().nullable(), inferior_tty_verified: z.boolean(), + closed_reason: z + .string() + .exactOptional() + .describe( + "Present when state is closed: why the session stopped accepting commands. Close it and start a new session to continue.", + ), }), examples: [ { diff --git a/src/contracts/jsonSchemaReferences.ts b/src/contracts/jsonSchemaReferences.ts index b72267e387..86c2630625 100644 --- a/src/contracts/jsonSchemaReferences.ts +++ b/src/contracts/jsonSchemaReferences.ts @@ -1,3 +1,4 @@ +import { unescapeJsonPointerToken } from "../domain/jsonPointer.js"; import { schemaArrays, schemaChildren, @@ -231,7 +232,7 @@ export class JsonSchemaReferences { for (const token of fragment.slice(1).split("/")) { if (/~(?:[^01]|$)/u.test(token)) return { kind: "unresolved", reason: "pointer" }; - const key = token.replaceAll("~1", "/").replaceAll("~0", "~"); + const key = unescapeJsonPointerToken(token); if ( value === null || typeof value !== "object" || diff --git a/src/contracts/pythonPycDeliveryContracts.test.ts b/src/contracts/pythonPycDeliveryContracts.test.ts deleted file mode 100644 index 3d8074de97..0000000000 --- a/src/contracts/pythonPycDeliveryContracts.test.ts +++ /dev/null @@ -1,58 +0,0 @@ -import { Ajv2020 } from "ajv/dist/2020.js"; -import { Ajv } from "ajv"; -import { expect, it } from "vitest"; -import { toolContract } from "./toolContracts.js"; -import { toolInputSchemaWithMetadata } from "./toolSchemaMetadata.js"; - -const view = { - kind: "python-pyc", - collection: "code_objects", - fields: ["name"], -}; - -it.each(["draft-2020-12", "draft-07"] as const)( - "advertises pyc delivery combinations in %s", - (target) => { - const contract = toolContract("inspect_python_pyc"); - const projected = toolInputSchemaWithMetadata(contract)[ - "~standard" - ].jsonSchema.input({ target }); - const ajv = - target === "draft-2020-12" - ? new Ajv2020({ strict: false, validateFormats: false }) - : new Ajv({ strict: false, validateFormats: false }); - expect(ajv.validateSchema(projected)).toBe(true); - expect(projected).toMatchObject({ type: "object" }); - for (const key of ["anyOf", "oneOf", "allOf", "x-rea-root-discriminator"]) - expect(projected).not.toHaveProperty(key); - const validate = ajv.compile(projected); - for (const [delivery, valid] of [ - [{}, true], - [{ detail: "complete" }, true], - [{ detail: "summary" }, true], - [{ detail: "summary", view }, true], - [ - { - detail: "summary", - view: { ...view, names: [], fields: [], indices: [] }, - }, - true, - ], - [{ view }, false], - [{ detail: "complete", view }, false], - [{ detail: "summary", view: { ...view, fields: ["missing"] } }, false], - [{ detail: "summary", view: { ...view, kind: "native-exports" } }, false], - [{ detail: "summary", view, unexpected: true }, false], - ] as const) { - const input = { path: "/tmp/module.pyc", ...delivery }; - expect( - contract.inputSchema.safeParse(input).success, - JSON.stringify(input), - ).toBe(valid); - expect( - validate(input), - JSON.stringify({ input, errors: validate.errors }), - ).toBe(valid); - } - }, -); diff --git a/src/contracts/reverseEngineeringToolContracts.ts b/src/contracts/reverseEngineeringToolContracts.ts index 39d45fb899..73394aca40 100644 --- a/src/contracts/reverseEngineeringToolContracts.ts +++ b/src/contracts/reverseEngineeringToolContracts.ts @@ -39,7 +39,7 @@ export const REVERSE_ENGINEERING_TOOL_CONTRACTS = [ ...toolContractMetadata("execute_rizin_command"), kind: "native-provider", description: - "Run one caller-selected Rizin command on a local artifact. Commands/plugins can execute code, mutate files, access network and read outside the artifact; those external inputs are not Evidence-bound. Suppresses user settings/scripts but retains plugins. Raw stdout/stderr is authoritative, JSON is command-specific, and UTF-8 loss/truncation is explicit; output-limit termination leaves completion unknown. For raw images, asm_arch/asm_bits/asm_cpu select decoding and base_address maps offset 0; base_address also passes -n to skip loaders even on ELF, without REA confirming either. An admitted command retires shared binary analysis: close Ghidra explicitly first, then open_binary after effects settle. command_timeout_ms is per-phase and max_output_bytes bounds combined output (16 MiB default); both apply next call without restart and do not raise client receive limits or persistent debugger deadlines.", + "Run one caller-selected Rizin command on a local artifact. Commands/plugins can execute code, mutate files, access network and read outside the artifact; those external inputs are not Evidence-bound. Suppresses user settings/scripts but retains plugins. Raw stdout/stderr is authoritative, JSON is command-specific, and UTF-8 loss/truncation is explicit; output-limit termination leaves completion unknown. Rizin exits 0 even when its shell refuses a statement; completion_status rejected marks that refusal, and later statements did not run. Other command failures can still exit 0, so inspect stderr before treating empty output as absence. For raw images, asm_arch/asm_bits/asm_cpu select decoding and base_address maps offset 0; base_address also passes -n to skip loaders even on ELF, without REA confirming either. An admitted command retires shared binary analysis: close Ghidra explicitly first, then open_binary after effects settle. command_timeout_ms is per-phase and max_output_bytes bounds combined output (16 MiB default); both apply next call without restart and do not raise client receive limits or persistent debugger deadlines.", inputSchema: rizinInputSchema, outputSchema: evidenceResultOf(commandObservationSchema), examples: [ diff --git a/src/contracts/rizinDebugToolContracts.ts b/src/contracts/rizinDebugToolContracts.ts index 7266834ebd..0c9ac55860 100644 --- a/src/contracts/rizinDebugToolContracts.ts +++ b/src/contracts/rizinDebugToolContracts.ts @@ -72,7 +72,7 @@ export const RIZIN_DEBUG_TOOL_CONTRACTS = [ ...toolContractMetadata("rizin_debug_session_status"), kind: "native-provider", description: - "Return observed lifecycle state and bounded recent NUL-framed output and process diagnostics for one Rizin debugger session; truncation fields report discarded data, and output_invalid_utf8 reports whether invalid UTF-8 has been observed in session frame bytes; recent output may contain replacement characters.", + "Return observed lifecycle state and bounded recent NUL-framed output and process diagnostics for one Rizin debugger session; closed_reason says why a closed session stopped, truncation fields report discarded data, and output_invalid_utf8 reports whether invalid UTF-8 has been observed in session frame bytes; recent output may contain replacement characters.", inputSchema: z.strictObject({ session_id: sessionId }), outputSchema: z.strictObject({ session_id: sessionId, @@ -82,6 +82,12 @@ export const RIZIN_DEBUG_TOOL_CONTRACTS = [ recent_output_truncated: z.boolean(), diagnostics_truncated: z.boolean(), output_invalid_utf8: z.boolean(), + closed_reason: z + .string() + .exactOptional() + .describe( + "Present when state is closed: why the session stopped accepting commands. Close it and start a new session to continue.", + ), }), examples: [ { diff --git a/src/contracts/toolOutputSchemaGroups.ts b/src/contracts/toolOutputSchemaGroups.ts index 087ad12293..8100e805ae 100644 --- a/src/contracts/toolOutputSchemaGroups.ts +++ b/src/contracts/toolOutputSchemaGroups.ts @@ -106,7 +106,6 @@ import { nativeDispatchMetadataResultSchema } from "../domain/native/objcSwiftMe import { nativeInvestigationTraceSchema } from "../domain/native/nativeInvestigationGraph.js"; import { addressedEntry, - functionDossierOutput, lifecycleResultOf, nullableText, procedureInfoOutput, diff --git a/src/contracts/toolOutputSchemaPrimitives.ts b/src/contracts/toolOutputSchemaPrimitives.ts index 069dfffc3f..f482e533c9 100644 --- a/src/contracts/toolOutputSchemaPrimitives.ts +++ b/src/contracts/toolOutputSchemaPrimitives.ts @@ -5,7 +5,6 @@ import { investigationStatisticsSchema } from "../domain/investigationLifecycle. import { utcDateTimeSchema } from "../domain/offsetDateTime.js"; import { analysisCleanupRetrySchema } from "../domain/analysisErrorRecovery.js"; -import { functionDossierSchema } from "../domain/native/functionDossier.js"; import { analysisProfileSchema } from "../domain/analysisProfile.js"; import { evidenceSchema, @@ -467,6 +466,12 @@ const toolAvailabilityFacts = { }), }; +/** + * Advertised operations the current provider offers for part of an + * unavailable operation's question. They do not replace that operation. + */ +const toolAvailabilityAlternatives = z.array(z.string().min(1)).min(1); + export const toolUnavailabilityReason = z.enum([ "client_capability_missing", "target_required", @@ -488,6 +493,7 @@ export const toolAvailability = z.discriminatedUnion("available", [ available: z.literal(false), reason: toolUnavailabilityReason, remediation: z.string().nullable(), + alternatives: toolAvailabilityAlternatives.optional(), }), ]); export type ToolAvailability = z.output; @@ -535,6 +541,7 @@ const toolAvailabilityGroupOf = (facts: Facts) => available: z.literal(false), reason: toolUnavailabilityReason, remediation: z.string().nullable(), + alternatives: toolAvailabilityAlternatives.optional(), }), ]); @@ -775,5 +782,3 @@ export const objcClassDiscoveryOutput = evidenceResultOf( identity_coverage: objcClassDiscoveryCoverageSchema, }), ); - -export const functionDossierOutput = evidenceResultOf(functionDossierSchema); diff --git a/src/contracts/toolSchemaMetadata.test.ts b/src/contracts/toolSchemaMetadata.test.ts new file mode 100644 index 0000000000..58864f282c --- /dev/null +++ b/src/contracts/toolSchemaMetadata.test.ts @@ -0,0 +1,32 @@ +import { expect, it } from "vitest"; + +import { ELECTRON_TOOL_CONTRACTS } from "./javascript/electronToolContracts.js"; +import { + toolInputSchemaWithMetadata, + toolOutputSchemaWithMetadata, +} from "./toolSchemaMetadata.js"; + +// Each MCP server registers every contract. The SDK reprojects registered +// schemas on every tools/list, so a wrapper rebuilt per server would repeat +// the whole catalog's JSON Schema conversion for each new server. +it("shares one advertised wrapper and projection per contract across registrations", () => { + const contract = ELECTRON_TOOL_CONTRACTS.find( + ({ name }) => name === "analyze_javascript_application", + ); + if (contract === undefined) throw new Error("Missing contract fixture"); + const options = { target: "draft-2020-12" } as const; + + const firstInput = toolInputSchemaWithMetadata(contract); + const secondInput = toolInputSchemaWithMetadata(contract); + expect(secondInput).toBe(firstInput); + expect(secondInput["~standard"].jsonSchema.input(options)).toBe( + firstInput["~standard"].jsonSchema.input(options), + ); + + const firstOutput = toolOutputSchemaWithMetadata(contract); + const secondOutput = toolOutputSchemaWithMetadata(contract); + expect(secondOutput).toBe(firstOutput); + expect(secondOutput["~standard"].jsonSchema.output(options)).toBe( + firstOutput["~standard"].jsonSchema.output(options), + ); +}); diff --git a/src/contracts/toolSchemaMetadata.ts b/src/contracts/toolSchemaMetadata.ts index 021bc9c4a8..02330d0c49 100644 --- a/src/contracts/toolSchemaMetadata.ts +++ b/src/contracts/toolSchemaMetadata.ts @@ -63,42 +63,65 @@ const PROPERTY_DESCRIPTIONS: Readonly> = { type JsonSchemaProjection = z.ZodType["~standard"]["jsonSchema"]["input"]; +// Contracts are immutable module values. Build each advertised wrapper once so +// its memoized projection serves every server in the process, not only the +// server that registered it. +const advertisedInputSchemas = new WeakMap(); +const advertisedOutputSchemas = new WeakMap(); + +const oncePerContract = < + Contract extends ToolContract, + Schema extends z.ZodType, +>( + cache: WeakMap, + contract: Contract, + build: () => Schema, +): Schema => { + const cached = cache.get(contract); + if (cached !== undefined) return cached as Schema; + const built = build(); + cache.set(contract, built); + return built; +}; + /** Attach caller guidance to a canonical schema for the SDK wire projection. */ export const toolInputSchemaWithMetadata = ( contract: Contract, ): Contract["inputSchema"] => - withAdvertisedJsonSchema( - contract.inputSchema, - "input", - (project) => (options) => { - const inline = project({ - ...options, - libraryOptions: { ...options.libraryOptions, reused: "inline" }, - }); - const examples = advertisedInputExamples(contract); - const presented = presentInputJsonSchema( - inline, - fallbackPropertyDescription, - ); - const sourceGuidance = getEvidenceInputContract(contract.inputSchema) - .hasEvidence - ? 'Declared Evidence fields accept complete records or {"kind":"retained-evidence","evidence_id":"ev_..."}. For a complete source, use the summary\'s parent_evidence_id.' - : undefined; - return { - // Keep advertised parameters self-contained for clients that do not - // resolve local references. The registered canonical schema remains - // responsible for runtime validation. - ...presented, - ...(sourceGuidance === undefined - ? {} - : { - description: [presented.description, sourceGuidance] - .filter((text) => typeof text === "string") - .join(" "), - }), - ...(examples.length === 0 ? {} : { examples }), - }; - }, + oncePerContract(advertisedInputSchemas, contract, () => + withAdvertisedJsonSchema( + contract.inputSchema, + "input", + (project) => (options) => { + const inline = project({ + ...options, + libraryOptions: { ...options.libraryOptions, reused: "inline" }, + }); + const examples = advertisedInputExamples(contract); + const presented = presentInputJsonSchema( + inline, + fallbackPropertyDescription, + ); + const sourceGuidance = getEvidenceInputContract(contract.inputSchema) + .hasEvidence + ? 'Declared Evidence fields accept complete records or {"kind":"retained-evidence","evidence_id":"ev_..."}. For a complete source, use the summary\'s parent_evidence_id.' + : undefined; + return { + // Keep advertised parameters self-contained for clients that do not + // resolve local references. The registered canonical schema remains + // responsible for runtime validation. + ...presented, + ...(sourceGuidance === undefined + ? {} + : { + description: [presented.description, sourceGuidance] + .filter((text) => typeof text === "string") + .join(" "), + }), + ...(examples.length === 0 ? {} : { examples }), + }; + }, + ), ); /** Return the full-profile advertised input JSON Schema registered on MCP. */ @@ -113,25 +136,28 @@ export const advertisedInputJsonSchema = ( export const toolOutputSchemaWithMetadata = ( contract: Contract, ): Contract["outputSchema"] => - withAdvertisedJsonSchema( - contract.outputSchema, - "output", - (project) => (options) => { - const shared = project({ - ...options, - libraryOptions: { reused: "ref", ...options.libraryOptions }, - }); - // An explicit ID owns the reference base. Relative external references - // also depend on that base, so leave those projections unchanged. - if ("$id" in shared || hasRelativeSchemaReference(shared)) return shared; - return { - ...shared, - $id: `urn:rea:tool-output:sha256:${digestCanonicalValue( - { target: options.target, schema: shared }, - "Tool output schema", - )}`, - }; - }, + oncePerContract(advertisedOutputSchemas, contract, () => + withAdvertisedJsonSchema( + contract.outputSchema, + "output", + (project) => (options) => { + const shared = project({ + ...options, + libraryOptions: { reused: "ref", ...options.libraryOptions }, + }); + // An explicit ID owns the reference base. Relative external references + // also depend on that base, so leave those projections unchanged. + if ("$id" in shared || hasRelativeSchemaReference(shared)) + return shared; + return { + ...shared, + $id: `urn:rea:tool-output:sha256:${digestCanonicalValue( + { target: options.target, schema: shared }, + "Tool output schema", + )}`, + }; + }, + ), ); /** diff --git a/src/cutter/CutterBridgeClient.ts b/src/cutter/CutterBridgeClient.ts index d2e0dc5451..cef5949892 100644 --- a/src/cutter/CutterBridgeClient.ts +++ b/src/cutter/CutterBridgeClient.ts @@ -222,7 +222,7 @@ const mapDiscovery = async ( export interface CutterDiscoveryOptions { readonly signal?: AbortSignal; } -export type CutterBridgeExecutionState = +type CutterBridgeExecutionState = | "not_started" | "complete" | "failed" @@ -252,7 +252,7 @@ class CutterBridgeRequestFailure extends Error { } } -export interface WindowsCutterDescriptorReader { +interface WindowsCutterDescriptorReader { verifyDirectory(directory: string): boolean; readDescriptor(directory: string, name: string, maxBytes: number): Buffer; } @@ -795,13 +795,6 @@ export class CutterBridgeClient { return descriptor; } - async listDescriptors(): Promise< - readonly z.infer[] - > { - const loaded = await this.#readDescriptors("list_cutter_sessions"); - return loaded.descriptors; - } - async #readDescriptors( operation: "list_cutter_sessions" | "cutter_command", signal?: AbortSignal, diff --git a/src/cutter/CutterBridgeService.ts b/src/cutter/CutterBridgeService.ts index 987d4b4b16..bde3c2d74f 100644 --- a/src/cutter/CutterBridgeService.ts +++ b/src/cutter/CutterBridgeService.ts @@ -163,7 +163,7 @@ export class CutterBridgeService { ); const input = parsed.data; const cleanup = await this.retryCleanup(); - if (!cleanup.ok) return err(cleanup.error); + if (!cleanup.ok) return cleanup; try { const output = await this.client.execute({ sessionId: input.session_id, diff --git a/src/cutter/cutterAuthenticatedResponse.ts b/src/cutter/cutterAuthenticatedResponse.ts index ce57b9c503..c8b6dc8483 100644 --- a/src/cutter/cutterAuthenticatedResponse.ts +++ b/src/cutter/cutterAuthenticatedResponse.ts @@ -40,7 +40,7 @@ type Escape = "none" | "slash" | "hex" | "low-slash" | "low-u" | "low-hex"; const invalid = (): Error => new Error("invalid authenticated response"); -export interface DecodedCutterResponse { +interface DecodedCutterResponse { readonly response: string; readonly proof: string; } @@ -262,7 +262,7 @@ export class CutterAuthenticatedResponseDecoder { } } -export type CutterInboundFrame = +type CutterInboundFrame = | { readonly kind: "challenge"; readonly value: unknown } | { readonly kind: "response"; diff --git a/src/desktop/AgentEnvComputer.test.ts b/src/desktop/AgentEnvComputer.test.ts index 9d79fcef21..a828e3a877 100644 --- a/src/desktop/AgentEnvComputer.test.ts +++ b/src/desktop/AgentEnvComputer.test.ts @@ -6,6 +6,62 @@ import { StructuredContentMcpClient } from "../providers/StructuredContentMcpCli import { AgentEnvComputer } from "./AgentEnvComputer.js"; import type { ComputerHelper } from "./AgentEnvComputerHelper.js"; +it("refuses a home-file answer larger than the reported size", async () => { + const connect = vi + .spyOn(StructuredContentMcpClient.prototype, "connect") + .mockResolvedValue(undefined); + const close = vi + .spyOn(StructuredContentMcpClient.prototype, "close") + .mockResolvedValue(undefined); + const call = vi + .spyOn(StructuredContentMcpClient.prototype, "callTool") + .mockResolvedValue({ content: [] }); + const fetch = vi + .spyOn(globalThis, "fetch") + .mockResolvedValueOnce(Response.json({ ready: true })) + .mockResolvedValueOnce( + Response.json({ + result: { + parts: [ + { + kind: "file", + file: { bytes: Buffer.from("larger").toString("base64") }, + metadata: { + "urn:agentenv:data-objects/v1": { path: "out.json" }, + }, + }, + ], + }, + }), + ); + try { + const { computer } = await AgentEnvComputer.connect( + { + handshake: async () => ({ + sandbox_id: "controlled-sandbox", + base_url: "http://127.0.0.1:1", + mcp_url: "http://127.0.0.1:1/mcp", + control_token: null, + }), + release: async () => ({ verified: true, teardown: "agentenv" }), + } as unknown as ComputerHelper, + new AbortController().signal, + ); + try { + await expect(computer.homeFiles(["out.json"], 1)).rejects.toMatchObject({ + reportedLimits: { guest_file_bytes: 1, decoded_file_bytes: 6 }, + }); + } finally { + await computer.close(); + } + } finally { + fetch.mockRestore(); + call.mockRestore(); + close.mockRestore(); + connect.mockRestore(); + } +}); + it.each(["http", "invalid_json", "readiness"] as const)( "redacts the complete control token in %s failure diagnostics", async (failure) => { diff --git a/src/desktop/AgentEnvComputer.ts b/src/desktop/AgentEnvComputer.ts index ccf5231eff..af747b679b 100644 --- a/src/desktop/AgentEnvComputer.ts +++ b/src/desktop/AgentEnvComputer.ts @@ -38,13 +38,13 @@ export const guestFileAnswerBytes = (bytes: number, files: number): number => /** The most REA reads into memory for one answer of unknown size: a quarter of the V8 heap, as for recordings. */ const heapQuarter = () => Math.floor(getHeapStatistics().heap_size_limit / 4); /** Where the data/get answer ceiling comes from. The guest's reported size does not change it. */ -export const GUEST_FILE_ANSWER_SOURCE = "v8.heap_size_limit/4"; +const GUEST_FILE_ANSWER_SOURCE = "v8.heap_size_limit/4"; /** * Copies of one answer held together while it is parsed: the raw chunks, the * concatenated buffer, and the text. The decoded files replace the text * afterward and are smaller than the base64 body. */ -export const GUEST_FILE_ANSWER_COPIES = 3; +const GUEST_FILE_ANSWER_COPIES = 3; /** The host ceiling for one data/get answer, including base64 expansion and each file's envelope. */ export const guestFileAnswerCeiling = (): number => heapQuarter(); @@ -105,7 +105,7 @@ export interface GuestFilePart { * The decoded files, or a refusal when they contain more bytes than the * computer reported. An underreported size must not admit the larger body. */ -export const retainGuestFiles = ( +const retainGuestFiles = ( parts: readonly GuestFilePart[], reportedBytes: number, files: number, diff --git a/src/desktop/AgentEnvSolveHelper.test.ts b/src/desktop/AgentEnvSolveHelper.test.ts index 4a24e714d8..f9645279b5 100644 --- a/src/desktop/AgentEnvSolveHelper.test.ts +++ b/src/desktop/AgentEnvSolveHelper.test.ts @@ -7,11 +7,13 @@ import { writeFile, } from "node:fs/promises"; import { join } from "node:path"; +import { setTimeout as delay } from "node:timers/promises"; import { describe, expect, it } from "vitest"; import { fakeAgentEnvSolves } from "../../tests/fixtures/fakeAgentEnvSolves.js"; import { createTestTempDirectory } from "../../tests/fixtures/temporaryDirectory.js"; +import { awaitAcknowledgement } from "../../tests/fixtures/lifecycleAcknowledgement.js"; import { ProviderCleanupError } from "../domain/providerCleanupError.js"; import { cleanupOwnedProcessGroup } from "../process/ProcessOwnership.js"; import { spawnOwnedProviderProcess } from "../process/ProviderProcess.js"; @@ -22,7 +24,7 @@ import { const SOLVE = { outcome: "passed" as const, checks: {}, turns: 3, usd: 0.1 }; -const until = async (path: string) => { +const until = async (path: string, signal: AbortSignal) => { for (;;) { if ( await access(path).then( @@ -31,7 +33,7 @@ const until = async (path: string) => { ) ) return; - await new Promise((resolve) => setTimeout(resolve, 50)); + await delay(50, undefined, { signal }); } }; @@ -49,10 +51,13 @@ describe("runSolveHelper", () => { const runner = await fakeAgentEnvSolves([SOLVE]); const environment = { ...runner.environment, FAKE_SOLVE_HANGS: "1" }; const stop = new AbortController(); + const polling = new AbortController(); const retained: SolveCleanupOwner[] = []; const started = until( join(runner.environment.FAKE_STATE ?? "", "solves.jsonl"), - ).then(() => stop.abort(new Error("cancelled by the test"))); + polling.signal, + ); + void started.catch(() => undefined); const begun = Date.now(); const solved = runSolveHelper({ @@ -64,17 +69,24 @@ describe("runSolveHelper", () => { // agentenv keeps a sandbox's folder until its teardown is confirmed, so the // emptied private root shows nothing is left up: only the cancellation remains. - const failure: unknown = await solved.then( - () => undefined, - (error: unknown) => error, - ); - expect(failure).toBeInstanceOf(Error); - expect((failure as Error).message).toBe("cancelled by the test"); - expect(failure).not.toHaveProperty("cleanupIncomplete", true); - await started; - expect(retained).toHaveLength(0); - expect(Date.now() - begun).toBeLessThan(15_000); - }, 30_000); + try { + await awaitAcknowledgement(started, solved, "solve helper start", 10_000); + stop.abort(new Error("cancelled by the test")); + const failure: unknown = await solved.then( + () => undefined, + (error: unknown) => error, + ); + expect(failure).toBeInstanceOf(Error); + expect((failure as Error).message).toBe("cancelled by the test"); + expect(failure).not.toHaveProperty("cleanupIncomplete", true); + expect(retained).toHaveLength(0); + expect(Date.now() - begun).toBeLessThan(15_000); + } finally { + polling.abort(); + stop.abort(); + await Promise.allSettled([started, solved]); + } + }); it("records a helper stopped at its deadline without a report as a failed solve of unknown cost, its cleanup known", async () => { const runner = await fakeAgentEnvSolves([SOLVE]); @@ -97,7 +109,7 @@ describe("runSolveHelper", () => { }); expect(report.error).toMatch(/stopped \(SIG[A-Z]+\) without a report/u); expect(retained).toHaveLength(0); - }, 30_000); + }); it("keeps a helper whose cleanup failed, so closing can retry it, and reports what is left", async () => { const runner = await fakeAgentEnvSolves([SOLVE]); @@ -134,7 +146,7 @@ describe("runSolveHelper", () => { status: "verified-cleanup", }); retained[0]?.dispose(); - }, 30_000); + }); }); it("retains a reported sandbox on failed removal, then retries that exact sandbox", async () => { @@ -172,7 +184,7 @@ it("retains a reported sandbox on failed removal, then retries that exact sandbo expect(failure.cleanupIncomplete).toBe(false); expect(failure.cleanupResources).toEqual([]); retained[0]?.dispose(); -}, 30_000); +}); it("retains a completed report if cancellation arrives during final cleanup", async () => { const runner = await fakeAgentEnvSolves([SOLVE]); @@ -199,7 +211,7 @@ it("retains a completed report if cancellation arrives during final cleanup", as cause: reason, report: { outcome: "passed", turns: 3, usd: 0.1 }, }); -}, 30_000); +}); it("retains an unrecognized sandbox root entry and retries after it is repaired", async () => { const runner = await fakeAgentEnvSolves([SOLVE]); @@ -228,7 +240,7 @@ it("retains an unrecognized sandbox root entry and retries after it is repaired" if (!(failure instanceof ProviderCleanupError)) throw failure; expect(failure.cleanupIncomplete).toBe(false); retained[0]?.dispose(); -}, 30_000); +}); it("retains root cleanup and the spawn failure when admission fails", async () => { const runner = await fakeAgentEnvSolves([SOLVE]); @@ -262,4 +274,4 @@ it("retains root cleanup and the spawn failure when admission fails", async () = expect(failure.cleanupIncomplete).toBe(false); expect(failure.cleanupResources).toEqual([]); retained[0]?.dispose(); -}, 30_000); +}); diff --git a/src/desktop/BuildComputerController.test.ts b/src/desktop/BuildComputerController.test.ts index 017a297e83..024536de23 100644 --- a/src/desktop/BuildComputerController.test.ts +++ b/src/desktop/BuildComputerController.test.ts @@ -4,21 +4,20 @@ import { mkdtemp, rm } from "node:fs/promises"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { fileURLToPath } from "node:url"; +import { getHeapStatistics } from "node:v8"; import { describe, expect, it } from "vitest"; import { AnalysisResourceConstraintError } from "../domain/analysisErrorCore.js"; import { guestFileAnswerBytes } from "./AgentEnvComputer.js"; import { - batchListedFiles, - controllerArgumentPieces, fetchHomeFolder, LINUX_ARGUMENT_VECTOR_BYTES, - LINUX_SINGLE_ARGUMENT_BYTES, runBuildController, } from "./BuildComputerController.js"; const MIB = 1024 * 1024; +const EXPECTED_LINUX_SINGLE_ARGUMENT_BYTES = 32 * 4096 - 1; const sha256 = (bytes: Buffer) => createHash("sha256").update(bytes).digest("hex"); @@ -58,6 +57,38 @@ const computer = (files: ReadonlyMap, steps: string[]) => { }; describe("fetchHomeFolder", () => { + it("splits tiny files before their answer envelopes exceed the live budget", async () => { + const workingBytes = Math.floor(getHeapStatistics().heap_size_limit / 12); + const count = Math.floor(workingBytes / (64 * 1024)) + 2; + const files = new Map( + Array.from({ length: count }, (_, index) => [ + `.rea-build/b/file-${String(index)}`, + Buffer.from("x"), + ]), + ); + const base = computer(files, []); + const batches: { count: number; bytes: number }[] = []; + const observed = { + ...base, + homeFiles: (paths: readonly string[], bytes: number) => { + if (paths[0]?.endsWith(".listing.json") !== true) + batches.push({ count: paths.length, bytes }); + return base.homeFiles(paths, bytes); + }, + }; + const result = await fetchHomeFolder(observed, ".rea-build/b", { + file: () => Promise.resolve(), + }); + expect(result).toEqual({ files: count, bytes: count }); + expect(batches.length).toBeGreaterThan(1); + expect( + batches.every( + ({ count, bytes }) => + guestFileAnswerBytes(bytes, count) <= workingBytes, + ), + ).toBe(true); + }); + it("hands over each checked batch before asking for the next, so one batch at a time is held", async () => { const files = new Map([ [".rea-build/b/a", Buffer.alloc(5 * MIB, 1)], @@ -103,22 +134,6 @@ describe("fetchHomeFolder", () => { }); }); -describe("batchListedFiles", () => { - it("starts another batch before each file's envelope pushes the answer over the budget", () => { - const files = [1, 2, 3].map((index) => ({ - path: `f${String(index)}`, - size: 10, - })); - const budget = guestFileAnswerBytes(20, 2); - expect( - batchListedFiles(files, { - rawBytes: 8 * 1024 * 1024, - answerBytes: budget, - }).map((batch) => batch.files.length), - ).toEqual([2, 1]); - }); -}); - describe("controller arguments", () => { it("splits a full start request so each argument fits Linux and the pieces join back", async () => { const launchArguments = Array.from({ length: 32 }, () => "a".repeat(4096)); @@ -160,10 +175,10 @@ describe("controller arguments", () => { ]); for (const argument of argv) expect(Buffer.byteLength(argument)).toBeLessThanOrEqual( - LINUX_SINGLE_ARGUMENT_BYTES, + EXPECTED_LINUX_SINGLE_ARGUMENT_BYTES, ); expect(Buffer.byteLength(JSON.stringify(payload))).toBeGreaterThan( - LINUX_SINGLE_ARGUMENT_BYTES, + EXPECTED_LINUX_SINGLE_ARGUMENT_BYTES, ); expect(JSON.parse(argv.slice(5).join(""))).toEqual(payload); }); @@ -204,13 +219,37 @@ describe("controller arguments", () => { }); it("keeps a character split on a UTF-16 pair whole and joins the controller's real entry point", async () => { - const text = `${"a".repeat(LINUX_SINGLE_ARGUMENT_BYTES - 1)}\u{1F600}rest`; - const pieces = controllerArgumentPieces(text); - expect(pieces.join("")).toBe(text); - for (const piece of pieces) + const text = `${"a".repeat(EXPECTED_LINUX_SINGLE_ARGUMENT_BYTES - 12)}\u{1F600}rest`; + const input = { text }; + let argv: readonly string[] = []; + await runBuildController( + { + exec: (next) => { + argv = next; + return Promise.resolve({ + exit_code: 0, + stdout: "{}\n", + stderr: "", + truncated: false, + timed_out: false, + }); + }, + homeFiles: () => Promise.resolve(new Map()), + redacted: (value) => value, + }, + "start", + input, + { timeoutS: 1 }, + ); + const pieces = argv.slice(5); + expect(JSON.parse(pieces.join(""))).toEqual(input); + expect(pieces.length).toBeGreaterThan(1); + for (const piece of pieces) { expect(Buffer.byteLength(piece)).toBeLessThanOrEqual( - LINUX_SINGLE_ARGUMENT_BYTES, + EXPECTED_LINUX_SINGLE_ARGUMENT_BYTES, ); + expect(Buffer.from(piece, "utf8").toString("utf8") === piece).toBe(true); + } const root = await mkdtemp(join(tmpdir(), "rea-controller-args-")); try { const output = join(root, "listing.json"); @@ -235,25 +274,3 @@ describe("controller arguments", () => { } }); }); - -describe("batchListedFiles", () => { - it("splits many tiny files before their envelopes pass the answer budget", () => { - const files = Array.from({ length: 3000 }, (_, index) => ({ - path: `home/file-${String(index)}`, - size: 1, - })); - const answerBytes = guestFileAnswerBytes(1, 1); - const batches = batchListedFiles(files, { - rawBytes: 8 * MIB, - answerBytes, - }); - - expect(batches.length).toBe(files.length); - expect( - batches.every( - (batch) => - guestFileAnswerBytes(batch.bytes, batch.files.length) <= answerBytes, - ), - ).toBe(true); - }); -}); diff --git a/src/desktop/BuildComputerController.ts b/src/desktop/BuildComputerController.ts index 7ab7f9ae00..ca538ba2fe 100644 --- a/src/desktop/BuildComputerController.ts +++ b/src/desktop/BuildComputerController.ts @@ -25,7 +25,7 @@ export const BUILD_REA_TIMEOUT_S = 1800; * Linux `MAX_ARG_STRLEN` on a 4 KiB page, excluding the NUL the kernel counts. * One argument past this fails with E2BIG before the process starts. */ -export const LINUX_SINGLE_ARGUMENT_BYTES = 32 * 4096 - 1; +const LINUX_SINGLE_ARGUMENT_BYTES = 32 * 4096 - 1; /** * A usual Linux `ARG_MAX`, leaving room for the environment the kernel counts * with the arguments. A larger vector fails with E2BIG before the process starts. @@ -43,7 +43,7 @@ const isLowSurrogate = (text: string, index: number): boolean => { }; /** Pieces of `text` that each fit in one Linux argument and join back to it. */ -export const controllerArgumentPieces = (text: string): readonly string[] => { +const controllerArgumentPieces = (text: string): readonly string[] => { const pieces: string[] = []; let start = 0; while (start < text.length) { @@ -277,7 +277,7 @@ const listFolder = async ( }; /** One listed file and the answer budget a batch must fit. */ -export interface ListedBatchFile { +interface ListedBatchFile { readonly path: string; readonly size: number; } @@ -287,7 +287,7 @@ export interface ListedBatchFile { * `rawBytes` or its base64 answer, including each file's envelope, passes * `answerBytes`. */ -export const batchListedFiles = ( +const batchListedFiles = ( files: readonly File[], limits: { readonly rawBytes: number; readonly answerBytes: number }, ): { files: File[]; bytes: number }[] => { diff --git a/src/desktop/BuildSecretFiles.cleanup.test.ts b/src/desktop/BuildSecretFiles.cleanup.test.ts index 24560149f9..abb9a11615 100644 --- a/src/desktop/BuildSecretFiles.cleanup.test.ts +++ b/src/desktop/BuildSecretFiles.cleanup.test.ts @@ -1,7 +1,7 @@ import { chmod, mkdir, readdir, writeFile } from "node:fs/promises"; import { basename, dirname, join } from "node:path"; -import { afterEach, describe, expect, it, vi } from "vitest"; +import { afterEach, expect, it, vi } from "vitest"; import { createTestTempDirectory } from "../../tests/fixtures/temporaryDirectory.js"; import { AnalysisError } from "../domain/analysisErrorBase.js"; @@ -10,7 +10,6 @@ import { DesktopBuildSecrets } from "../application/desktop/DesktopBuildSecrets. // Made-up values, never real credentials. const FIRST = "e-Plum7-Kite-Throwaway-42"; -const SECOND = "e-Quince3-Lantern-Throwaway-77"; const injection = vi.hoisted(() => ({ path: "", diff --git a/src/desktop/DconfDatabaseReader.test.ts b/src/desktop/DconfDatabaseReader.test.ts index d9b19ecfbc..8a20d40074 100644 --- a/src/desktop/DconfDatabaseReader.test.ts +++ b/src/desktop/DconfDatabaseReader.test.ts @@ -99,3 +99,63 @@ describe.skipIf(process.platform === "win32")("dumpDconfDatabase", () => { }, ); }); + +describe.skipIf(process.platform === "win32")( + "dconf command diagnostics", + () => { + it("retains the selected command and errno when execution is denied", async () => { + const root = await createTestTempDirectory("dconf-denied-command-"); + const command = join(root, "dconf"); + await writeFile(command, "#!/bin/sh\nexit 17\n", { mode: 0o600 }); + + const dump = await dumpDconfDatabase(join(root, "user"), { + REA_DCONF_COMMAND: command, + }); + + expect(dump).toEqual({ + ok: false, + error: expect.stringContaining("EACCES"), + }); + if (dump.ok) throw new Error("Expected a command failure"); + expect(dump.error).toContain(command); + }); + + it.each(["", " \t\n", "reader refused the database\n"])( + "retains a nonzero command's reason with stderr %j", + async (stderr) => { + const root = await createTestTempDirectory("dconf-command-exit-"); + const command = join(root, "dconf"); + await writeFile( + command, + `#!/bin/sh\nprintf '%s' '${stderr}' >&2\nexit 17\n`, + { + mode: 0o755, + }, + ); + + const dump = await dumpDconfDatabase(join(root, "user"), { + REA_DCONF_COMMAND: command, + }); + + expect(dump.ok).toBe(false); + if (dump.ok) throw new Error("Expected a command failure"); + if (stderr.trim() === "") { + expect(dump.error).toContain("Command failed:"); + expect(dump.error).toContain(command); + } else expect(dump.error).toBe(stderr.trim()); + }, + ); + + it("preserves the missing-command recovery", async () => { + const root = await createTestTempDirectory("dconf-missing-command-"); + expect( + await dumpDconfDatabase(join(root, "user"), { + REA_DCONF_COMMAND: join(root, "absent"), + }), + ).toEqual({ + ok: false, + error: "dconf is not installed (dconf-cli); set REA_DCONF_COMMAND", + }); + }); + }, +); diff --git a/src/desktop/DconfDatabaseReader.ts b/src/desktop/DconfDatabaseReader.ts index aa54f06a36..89c970cb7a 100644 --- a/src/desktop/DconfDatabaseReader.ts +++ b/src/desktop/DconfDatabaseReader.ts @@ -57,7 +57,7 @@ export const dumpDconfDatabase = async ( }; return { ok: false, - error: failure === undefined ? String(cause) : failure.stderr.trim(), + error: failure?.stderr.trim() || String(cause), }; } }; diff --git a/src/desktop/DesktopApplicationSession.test.ts b/src/desktop/DesktopApplicationSession.test.ts index 6f34e5c770..3f0105b4e4 100644 --- a/src/desktop/DesktopApplicationSession.test.ts +++ b/src/desktop/DesktopApplicationSession.test.ts @@ -157,7 +157,7 @@ describe.skipIf(process.platform !== "linux")("runDesktopSession", () => { await expect(runDesktopSession(plan)).rejects.toBe(failure); expect(checkpoints).toEqual(["stop", "exit"]); expect(vi.mocked(terminateTokenOwnedProcesses)).toHaveBeenCalledTimes(1); - }, 30_000); + }); it("launches the application without the agent environment's variables", async () => { const root = await createTestTempDirectory("desktop-session-env-"); @@ -184,7 +184,7 @@ describe.skipIf(process.platform !== "linux")("runDesktopSession", () => { "REA_PROCESS_RUN_ID", ]), ); - }, 30_000); + }); }); describe.skipIf(process.platform !== "linux")( @@ -209,7 +209,7 @@ describe.skipIf(process.platform !== "linux")( entry.pid !== outcome.session.launch_pid, ).length, ).toBeGreaterThan(0); - }, 30_000); + }); it("does not take samples whose window list was unreadable as the application having exited", async () => { const root = await createTestTempDirectory("desktop-session-unread-"); @@ -230,7 +230,7 @@ describe.skipIf(process.platform !== "linux")( expect(outcome.session.end_reason).toBe("application_exited"); expect(outcome.unreadableWindowSamples).toBe(4); expect(samples).toBeGreaterThanOrEqual(6); - }, 30_000); + }); }, ); @@ -385,7 +385,7 @@ describe.skipIf(process.platform === "win32")( expect(stderr).not.toContain("runtime 0 of 400"); expect(Buffer.byteLength(stderr)).toBeLessThanOrEqual(8 * 1024); expect(stderr_truncated).toBe(true); - }, 30_000); + }); it("gives each checkpoint the titles the app's own windows have shown so far", async () => { const root = await createTestTempDirectory("desktop-session-titles-"); @@ -422,7 +422,7 @@ describe.skipIf(process.platform === "win32")( }); expect(seen.at(-1)).toEqual(["today.txt - Notes"]); - }, 30_000); + }); it("launches the app with glibc's fatal errors sent to its stderr, not its terminal", async () => { const root = await createTestTempDirectory("desktop-session-libc-"); @@ -435,6 +435,6 @@ describe.skipIf(process.platform === "win32")( ); expect(outcome.session.stderr).toContain("LIBC_FATAL_STDERR_=1"); - }, 30_000); + }); }, ); diff --git a/src/desktop/DesktopBuildOwnedCommand.test.ts b/src/desktop/DesktopBuildOwnedCommand.test.ts index 0dbc2e2c6c..1c810577a6 100644 --- a/src/desktop/DesktopBuildOwnedCommand.test.ts +++ b/src/desktop/DesktopBuildOwnedCommand.test.ts @@ -65,9 +65,15 @@ it expect(stop).toHaveBeenCalledTimes(processRetained ? 1 : 0); await expect(stat(workspace)).rejects.toMatchObject({ code: "ENOENT" }); } finally { - await chmod(parent, 0o700); - await resources.close(); - command.mockRestore(); + try { + try { + await chmod(parent, 0o700); + } finally { + await resources.close(); + } + } finally { + command.mockRestore(); + } } }, ); diff --git a/src/desktop/DesktopBuildSetupHost.test.ts b/src/desktop/DesktopBuildSetupHost.test.ts index ee0a480c38..7a09af62d6 100644 --- a/src/desktop/DesktopBuildSetupHost.test.ts +++ b/src/desktop/DesktopBuildSetupHost.test.ts @@ -5,6 +5,7 @@ import { expect, it, vi } from "vitest"; import { createTestTempDirectory } from "../../tests/fixtures/temporaryDirectory.js"; import { createDeferred } from "../../tests/fixtures/binarySession.js"; +import { awaitAcknowledgement } from "../../tests/fixtures/lifecycleAcknowledgement.js"; import { providerSnapshot } from "../../tests/support/reverseEngineeringCompositionFixtures.js"; import * as owned from "../process/OwnedCommand.js"; import { systemDoctorHost } from "../application/Doctor.js"; @@ -89,7 +90,11 @@ it("retains installer cancellation, command outcome and output", async () => { ).installAgentenv(uv, controller.signal); void pending.catch(() => undefined); try { - await entered.promise; + await awaitAcknowledgement( + entered.promise, + pending, + "provider lifecycle boundary", + ); controller.abort(reason); const failure: unknown = await pending.catch((cause: unknown) => cause); if (!(failure instanceof AnalysisCancelledError)) throw failure; @@ -103,8 +108,11 @@ it("retains installer cancellation, command outcome and output", async () => { } finally { controller.abort(); await Promise.allSettled([pending]); - await resources.close(); - command.mockRestore(); + try { + await resources.close(); + } finally { + command.mockRestore(); + } } }); diff --git a/src/desktop/DesktopInspectionSampling.test.ts b/src/desktop/DesktopInspectionSampling.test.ts index 63ef2dc7d3..ce1e83607b 100644 --- a/src/desktop/DesktopInspectionSampling.test.ts +++ b/src/desktop/DesktopInspectionSampling.test.ts @@ -3,6 +3,7 @@ import { writeFile } from "node:fs/promises"; import * as files from "node:fs/promises"; import { join } from "node:path"; import { expect, it, onTestFinished, vi } from "vitest"; +import * as processOutput from "../process/ExecFileOutput.js"; vi.mock("node:fs/promises", async (original) => { const actual = await original(); @@ -260,30 +261,25 @@ const samplingSession = async (route: Route) => { sourceSettled = true; sampling = false; }); - const native = createSystemProcessOwnershipHost( - "linux", - {}, - { - execFileOutput: async () => { - if (sourceSettled && retained.pending().length > 0) { - freshBeforeRetry += 1; - throw new Error("fresh lookup before sampling owner retry"); - } - if (sampling && route !== "runId") { - if (route === "cancelledListing" || retained.pending().length > 0) { - nativeCancellations += 1; - if (route === "cancelledListing") sampling = false; - controller.abort(reason); - throw new ExecFileOutputCancelledError(reason); - } - return commandOutput( - "42 1 42 1000 S recording-child\n43 1 42 1000 S recording-peer\n", - ); - } - return commandOutput(""); - }, - }, - ); + vi.spyOn(processOutput, "execFileOutput").mockImplementation(async () => { + if (sourceSettled && retained.pending().length > 0) { + freshBeforeRetry += 1; + throw new Error("fresh lookup before sampling owner retry"); + } + if (sampling && route !== "runId") { + if (route === "cancelledListing" || retained.pending().length > 0) { + nativeCancellations += 1; + if (route === "cancelledListing") sampling = false; + controller.abort(reason); + throw new ExecFileOutputCancelledError(reason); + } + return commandOutput( + "42 1 42 1000 S recording-child\n43 1 42 1000 S recording-peer\n", + ); + } + return commandOutput(""); + }); + const native = createSystemProcessOwnershipHost("linux", {}); rejectUnexpectedNativeOperations(); vi.spyOn(systemProcessOwnershipHost, "listProcesses").mockImplementation( async (signal) => { diff --git a/src/desktop/GuestFileAnswer.test.ts b/src/desktop/GuestFileAnswer.test.ts index 6b4b0b2ba2..0f34db72c5 100644 --- a/src/desktop/GuestFileAnswer.test.ts +++ b/src/desktop/GuestFileAnswer.test.ts @@ -4,12 +4,9 @@ import { AnalysisResourceConstraintError } from "../domain/analysisErrorCore.js" import { readHomeFiles } from "./ComputerHomeFiles.js"; import { AgentEnvComputer, - GUEST_FILE_ANSWER_COPIES, - GUEST_FILE_ANSWER_SOURCE, guestFileAnswerBytes, guestFileAnswerRefusal, guestFileAnswerWorkingBytes, - retainGuestFiles, } from "./AgentEnvComputer.js"; describe("guest file encoding", () => { @@ -119,20 +116,14 @@ describe("guest file answers", () => { ); it("counts base64, the envelope, and the copies held while the answer is parsed", () => { - expect(guestFileAnswerBytes(3, 1)).toBeGreaterThan(3); - expect(guestFileAnswerBytes(3, 2)).toBeGreaterThan( - guestFileAnswerBytes(3, 1), - ); - const wire = guestFileAnswerBytes(3, 1); + expect(guestFileAnswerBytes(3, 1)).toBe(65_540); + expect(guestFileAnswerBytes(3, 2)).toBe(131_076); + const wire = 65_540; expect(guestFileAnswerRefusal(3, 1, wire)).toBeInstanceOf( AnalysisResourceConstraintError, ); - expect( - guestFileAnswerRefusal(3, 1, wire * GUEST_FILE_ANSWER_COPIES), - ).toBeUndefined(); - expect(guestFileAnswerWorkingBytes(wire * GUEST_FILE_ANSWER_COPIES)).toBe( - wire, - ); + expect(guestFileAnswerRefusal(3, 1, wire * 3)).toBeUndefined(); + expect(guestFileAnswerWorkingBytes(wire * 3)).toBe(wire); }); it("refuses a reported size that expands past the working budget before naming a fetch", () => { @@ -144,13 +135,13 @@ describe("guest file answers", () => { expect(refusal.reportedLimits).toEqual({ guest_file_bytes: reported, file_count: 2, - answer_bytes: guestFileAnswerBytes(reported, 2), + answer_bytes: 135_072, answer_ceiling_bytes: ceiling, - answer_working_bytes: guestFileAnswerWorkingBytes(ceiling), - answer_copies: GUEST_FILE_ANSWER_COPIES, - ceiling_source: GUEST_FILE_ANSWER_SOURCE, + answer_working_bytes: 333, + answer_copies: 3, + ceiling_source: "v8.heap_size_limit/4", }); - expect(refusal.remediationAction).toContain(GUEST_FILE_ANSWER_SOURCE); + expect(refusal.remediationAction).toContain("v8.heap_size_limit/4"); expect(refusal.remediationAction).toContain("--max-old-space-size"); expect(refusal.remediationAction).toContain("Restart REA"); expect(guestFileAnswerBytes(reported, 2)).toBeGreaterThan( @@ -158,21 +149,6 @@ describe("guest file answers", () => { ); }); - it("does not keep file bytes the computer reported as smaller", () => { - const retained = retainGuestFiles( - [{ path: "out.json", bytes: Buffer.from("larger") }], - 1, - 1, - 1_000_000, - ); - expect(retained.ok).toBe(false); - if (retained.ok) return; - expect(retained.error.reportedLimits).toMatchObject({ - guest_file_bytes: 1, - decoded_file_bytes: Buffer.from("larger").length, - }); - }); - it("does not ask the computer for a home file past that same ceiling", async () => { const failure = await AgentEnvComputer.prototype.homeFiles .call({}, ["out.json"], Number.MAX_SAFE_INTEGER) diff --git a/src/desktop/LinuxApplicationInventory.ts b/src/desktop/LinuxApplicationInventory.ts index 7439eaea0c..8d3a8d73eb 100644 --- a/src/desktop/LinuxApplicationInventory.ts +++ b/src/desktop/LinuxApplicationInventory.ts @@ -92,7 +92,7 @@ const LINUX_INVENTORY_HOST: LinuxInventoryHost = { type Package = DesktopApplicationInventory["package"]; /** Directories searched for desktop entries, most specific first. */ -export const desktopEntryDirectories = ( +const desktopEntryDirectories = ( environment: LinuxApplicationRequest["environment"], home: string, ): readonly string[] => { diff --git a/src/domain/analysisErrorCore.ts b/src/domain/analysisErrorCore.ts index 09714bf3e9..a69243fc1b 100644 --- a/src/domain/analysisErrorCore.ts +++ b/src/domain/analysisErrorCore.ts @@ -254,6 +254,11 @@ export class AnalysisCapabilityUnavailableError extends AnalysisError { readonly siblingReasons: Readonly> | undefined; /** Incomplete cleanup must settle before the operation can be retried. */ readonly retryable: boolean; + /** + * Operations the caller can run now that observe part of what this one + * would answer; the remediation states what they do not establish. + */ + readonly alternatives: readonly string[]; constructor( readonly providerId: string, @@ -266,6 +271,7 @@ export class AnalysisCapabilityUnavailableError extends AnalysisError { readonly siblingOperations?: readonly string[]; readonly siblingReasons?: Readonly>; readonly retryable?: boolean; + readonly alternatives?: readonly string[]; }, ) { super( @@ -278,6 +284,7 @@ export class AnalysisCapabilityUnavailableError extends AnalysisError { this.siblingOperations = options?.siblingOperations; this.siblingReasons = options?.siblingReasons; this.retryable = options?.retryable === true && !this.cleanupIncomplete; + this.alternatives = Object.freeze([...(options?.alternatives ?? [])]); } } diff --git a/src/domain/analysisErrorProjection.test.ts b/src/domain/analysisErrorProjection.test.ts index 1c80094466..c9f45c78fa 100644 --- a/src/domain/analysisErrorProjection.test.ts +++ b/src/domain/analysisErrorProjection.test.ts @@ -16,7 +16,6 @@ import { import { BrowserObservationError } from "./browserObservationError.js"; import { EvidenceFileError, EvidenceIntegrityError } from "./evidenceErrors.js"; import { - HOPPER_STARTUP_TIMEOUT_CONTINUING_REMEDIATION, HOPPER_STARTUP_TIMEOUT_REMEDIATION, HopperProcessError, HopperRemoteError, @@ -1570,8 +1569,8 @@ describe("analysis error projection: caller lifetime", () => { ), }, ); - expect(projectAnalysisError(error).remediation.action).toBe( - HOPPER_STARTUP_TIMEOUT_CONTINUING_REMEDIATION, + expect(projectAnalysisError(error).remediation.action).toMatch( + /update_analysis_settings.*hopper\.startup_timeout_ms.*without restarting REA/u, ); const invocation = projectAnalysisError(error, "invocation"); expect(invocation.remediation.action).toBe( diff --git a/src/domain/analysisErrorProjection.ts b/src/domain/analysisErrorProjection.ts index c7244177c5..02c07cabd5 100644 --- a/src/domain/analysisErrorProjection.ts +++ b/src/domain/analysisErrorProjection.ts @@ -867,6 +867,9 @@ const providerErrorDetails = ( provider_id: error.providerId, operation: error.operation, reason: error.reason, + ...(error.alternatives.length === 0 + ? {} + : { alternatives: [...error.alternatives] }), }; if (error instanceof ProviderSelectionError) return { diff --git a/src/domain/analysisSnapshot.test.ts b/src/domain/analysisSnapshot.test.ts index 624c3e3a36..74a22f0536 100644 --- a/src/domain/analysisSnapshot.test.ts +++ b/src/domain/analysisSnapshot.test.ts @@ -1,7 +1,6 @@ import { describe, expect, it } from "vitest"; import { - type AnalysisSnapshot, analysisSnapshotSchema, analysisQueryId, parseAnalysisSnapshot, diff --git a/src/domain/analysisView/EnvironmentStateExtraction.ts b/src/domain/analysisView/EnvironmentStateExtraction.ts index b95be24419..2bfc300e05 100644 --- a/src/domain/analysisView/EnvironmentStateExtraction.ts +++ b/src/domain/analysisView/EnvironmentStateExtraction.ts @@ -1,10 +1,8 @@ import { StateRecordChronology } from "./StateRecordChronology.js"; import { jsonByteLength } from "../jsonSerialization.js"; import type { JsonValue } from "../jsonValue.js"; -import { - compareUnicodeCodePoints as compare, - compositeKey, -} from "../unicodeCodePointOrder.js"; +import { compareUnicodeCodePoints as compare } from "../unicodeCodePointOrder.js"; +import { environmentEntityKey } from "./environmentStateIdentity.js"; import { ownMember, apiBaseLengths, @@ -95,9 +93,6 @@ interface ObservedMembership { capacity: number; } -const entityKey = (type: string, collection: string, id: string): string => - compositeKey([type, collection, id]); - const requestLine = (record: StateRecordFacts): string => record.path + (record.query ?? ""); @@ -772,7 +767,7 @@ export class EnvironmentStateExtraction { const id = scalarText(ownMember(object, field)); if (id === undefined) return; const collection = collectionPathOf(record, plan.item); - const key = entityKey(plan.type, collection, id); + const key = environmentEntityKey(plan.type, collection, id); const position = this.#chronology.positions.get(record.ordinal)!; let entity = this.#entities.get(key); if (entity === undefined) { @@ -823,7 +818,7 @@ export class EnvironmentStateExtraction { compareObservedEntities, )) this.#merged.set( - entityKey(entity.type, entity.collection, entity.id), + environmentEntityKey(entity.type, entity.collection, entity.id), mergeObservedEntity(entity), ); } @@ -841,7 +836,7 @@ export class EnvironmentStateExtraction { withBody: (ordinal, use) => this.#withBody(ordinal, use), retain: (bytes) => this.#retain(bytes), entity: (type, collection, id) => - this.#merged.get(entityKey(type, collection, id)), + this.#merged.get(environmentEntityKey(type, collection, id)), inCollection: (type, collection) => this.#inCollection(type, collection), heldFields: (type) => { const fields = new Set(); diff --git a/src/domain/analysisView/analysisViewCycles.test.ts b/src/domain/analysisView/analysisViewCycles.test.ts index 494e8814fb..41e08788b5 100644 --- a/src/domain/analysisView/analysisViewCycles.test.ts +++ b/src/domain/analysisView/analysisViewCycles.test.ts @@ -1,17 +1,11 @@ -import { readdirSync, readFileSync, statSync } from "node:fs"; -import { dirname, join, relative, resolve } from "node:path"; +import { dirname } from "node:path"; import { fileURLToPath } from "node:url"; -import { describe, expect, it } from "vitest"; +import { describe, it } from "vitest"; -import { readImportSpecifiers } from "../../../scripts/lib/import-specifiers.mjs"; +import { expectImportCycles } from "../../../tests/fixtures/importCycles.js"; const root = dirname(fileURLToPath(import.meta.url)); -const relativeSpecifiers = (source: string): readonly string[] => - readImportSpecifiers(source) - .map(({ specifier }) => specifier) - .filter((specifier) => specifier.startsWith(".")); - /** * Cycles that already existed outside the analysis-view hub. Membership is * exact so a new edge cannot hide inside the allow-list. @@ -40,97 +34,8 @@ const allowedCycleEdges = [ "environmentStateWriteEffects.ts -> environmentStateWriteAddress.ts", ].sort(); -const productionFiles = (directory: string): string[] => { - const files: string[] = []; - for (const name of readdirSync(directory)) { - const path = join(directory, name); - if (statSync(path).isDirectory()) files.push(...productionFiles(path)); - else if (name.endsWith(".ts") && !name.includes(".test.")) files.push(path); - } - return files; -}; - -const importGraph = (): Map> => { - const files = new Set( - productionFiles(root).map((path) => relative(root, path)), - ); - const graph = new Map>(); - for (const file of files) { - const source = readFileSync(join(root, file), "utf8"); - const edges = new Set(); - for (const spec of relativeSpecifiers(source)) { - const resolved = resolve(dirname(join(root, file)), spec).replace( - /\.js$/, - ".ts", - ); - const target = relative(root, resolved); - if (files.has(target)) edges.add(target); - } - graph.set(file, edges); - } - return graph; -}; - -const stronglyConnected = ( - graph: Map>, -): readonly (readonly string[])[] => { - let index = 0; - const stack: string[] = []; - const onStack = new Set(); - const indices = new Map(); - const low = new Map(); - const components: string[][] = []; - const visit = (node: string): void => { - indices.set(node, index); - low.set(node, index); - index += 1; - stack.push(node); - onStack.add(node); - for (const next of graph.get(node) ?? []) { - if (!indices.has(next)) { - visit(next); - low.set(node, Math.min(low.get(node) ?? index, low.get(next) ?? index)); - } else if (onStack.has(next)) { - low.set( - node, - Math.min(low.get(node) ?? index, indices.get(next) ?? index), - ); - } - } - if (low.get(node) === indices.get(node)) { - const component: string[] = []; - let member = ""; - while (member !== node) { - member = stack.pop() ?? node; - onStack.delete(member); - component.push(member); - } - if (component.length > 1) components.push(component.sort()); - } - }; - for (const node of [...graph.keys()].sort()) { - if (!indices.has(node)) visit(node); - } - return components.sort( - (left, right) => - right.length - left.length || left.join().localeCompare(right.join()), - ); -}; - describe("analysis view import cycles", () => { it("keeps projectors from importing the request and result hub", () => { - const graph = importGraph(); - const cycles = stronglyConnected(graph).map((cycle) => cycle.join("\n")); - expect(cycles).toEqual( - allowedCycles.map((cycle) => [...cycle].sort().join("\n")), - ); - const internalEdges = allowedCycles.flatMap((cycle) => - cycle.flatMap((source) => - [...(graph.get(source) ?? [])] - .filter((target) => cycle.includes(target)) - .map((target) => `${source} -> ${target}`), - ), - ); - expect(internalEdges.sort()).toEqual(allowedCycleEdges); + expectImportCycles(root, allowedCycles, allowedCycleEdges); }); }); diff --git a/src/domain/analysisView/apkSignatureView.ts b/src/domain/analysisView/apkSignatureView.ts index 812c3cf2c7..1d1a2cdf3d 100644 --- a/src/domain/analysisView/apkSignatureView.ts +++ b/src/domain/analysisView/apkSignatureView.ts @@ -194,7 +194,7 @@ const read = (parent: AnalysisViewParent): Result => { function* records( report: ApkSignatureResult, selection: Selection, -): Generator> { +): Generator & { id: string; signer_id?: string }> { const wanted = selection.schemes === undefined ? undefined : new Set(selection.schemes); for (const scheme of schemes) { @@ -317,8 +317,8 @@ export const projectApkSignatureView = ( const rows = ids?.size === 0 || signers?.size === 0 ? [] : records(report, view); for (const row of rows) { - if (ids !== undefined && !ids.has(String(row.id))) continue; - if (signers !== undefined && !signers.has(String(row.signer_id ?? row.id))) + if (ids !== undefined && !ids.has(row.id)) continue; + if (signers !== undefined && !signers.has(row.signer_id ?? row.id)) continue; if (total++ < view.offset || items.length >= view.limit) continue; const item: Record = {}; diff --git a/src/domain/analysisView/environmentManifestRules.ts b/src/domain/analysisView/environmentManifestRules.ts index 1bdf605df2..2ded89e506 100644 --- a/src/domain/analysisView/environmentManifestRules.ts +++ b/src/domain/analysisView/environmentManifestRules.ts @@ -118,7 +118,7 @@ const earliest = (groups: readonly ToolGroup[]): ToolGroup => /** Merge groups around `shape`; literal siblings of its templated positions become observed values. */ const mergeGroups = ( groups: readonly ToolGroup[], - shape = earliest(groups), + shape: ToolGroup, ): ToolGroup => { if (groups.length === 1) return groups[0]!; const primary = earliest(groups); diff --git a/src/domain/analysisView/environmentStateCounters.ts b/src/domain/analysisView/environmentStateCounters.ts index 2a9fdef81b..bd2764c7fc 100644 --- a/src/domain/analysisView/environmentStateCounters.ts +++ b/src/domain/analysisView/environmentStateCounters.ts @@ -1,3 +1,4 @@ +import { jsonPointerTokens } from "../jsonPointer.js"; import type { JsonValue } from "../jsonValue.js"; type JsonObject = { [key: string]: JsonValue }; @@ -7,12 +8,7 @@ const isObject = (value: JsonValue | undefined): value is JsonObject => /** A list counter's member names: a top-level member's name not starting with `/`, else a pointer (`/pageInfo/totalRows`, `/~1total`). */ export const counterPath = (counter: string): string[] => - counter.startsWith("/") - ? counter - .slice(1) - .split("/") - .map((token) => token.replaceAll("~1", "/").replaceAll("~0", "~")) - : [counter]; + counter.startsWith("/") ? jsonPointerTokens(counter) : [counter]; /** A list counter's recorded value, or undefined when the body lacks it. */ export const counterAt = ( diff --git a/src/domain/analysisView/environmentStateIdentity.ts b/src/domain/analysisView/environmentStateIdentity.ts new file mode 100644 index 0000000000..4d04c0d07f --- /dev/null +++ b/src/domain/analysisView/environmentStateIdentity.ts @@ -0,0 +1,14 @@ +import { compositeKey } from "../unicodeCodePointOrder.js"; + +/** Key an entity by its type, collection, and caller-supplied identifier. */ +export const environmentEntityKey = ( + type: string, + collection: string, + id: string, +): string => compositeKey([type, collection, id]); + +/** Key a collection by its type and caller-supplied collection path. */ +export const environmentCollectionKey = ( + type: string, + collection: string, +): string => compositeKey([type, collection]); diff --git a/src/domain/analysisView/environmentStateListFacts.ts b/src/domain/analysisView/environmentStateListFacts.ts index 22c1df8b7c..e158665d30 100644 --- a/src/domain/analysisView/environmentStateListFacts.ts +++ b/src/domain/analysisView/environmentStateListFacts.ts @@ -1,3 +1,4 @@ +import { jsonPointerTokens } from "../jsonPointer.js"; import { identifierWords } from "./environmentStateWords.js"; import type { JsonValue } from "../jsonValue.js"; import { compareUnicodeCodePoints as compare } from "../unicodeCodePointOrder.js"; @@ -14,7 +15,6 @@ import { listItems, listsIds, pointerTo, - pointerTokens, recordedItemCount, same, scalarText, @@ -52,7 +52,7 @@ export const listFacts = ( if (items.length >= 2) for (const [name, value] of Object.entries(parent)) if ( - name !== pointerTokens(plan.pointer)[0] && + name !== jsonPointerTokens(plan.pointer)[0] && listsIds(value, items, field) ) listing.add(name); @@ -101,7 +101,7 @@ export const itemTotals = ( parent: JsonObject, listPointer: string, ): [counter: string, value: JsonValue | undefined][] => { - const root = pointerTokens(listPointer)[0]; + const root = jsonPointerTokens(listPointer)[0]; const totals: [string, JsonValue | undefined][] = []; for (const [name, value] of Object.entries(parent)) if (isItemTotal(name)) @@ -153,7 +153,7 @@ const continues = (name: string, value: JsonValue | undefined): boolean => /** Continuation facts in the envelope or its sibling paging objects, never inside the list payload. */ const hasContinuation = (parent: JsonObject, listPointer: string): boolean => { - const root = pointerTokens(listPointer)[0]; + const root = jsonPointerTokens(listPointer)[0]; return Object.entries(parent).some( ([name, value]) => continues(name, value) || diff --git a/src/domain/analysisView/environmentStateResponseMembers.ts b/src/domain/analysisView/environmentStateResponseMembers.ts index 51d42ef820..93173334dd 100644 --- a/src/domain/analysisView/environmentStateResponseMembers.ts +++ b/src/domain/analysisView/environmentStateResponseMembers.ts @@ -1,3 +1,4 @@ +import { unescapeJsonPointerToken } from "../jsonPointer.js"; import { canonicalJson } from "../comparisonSemantics.js"; import type { JsonValue } from "../jsonValue.js"; import { compareUnicodeCodePoints as compare } from "../unicodeCodePointOrder.js"; @@ -183,7 +184,7 @@ export const mintedFor = ( /** The first member a pointer names, which holds the entity rather than the envelope. */ const pointerRoot = (pointer: string): string => - pointer.slice(1).split("/")[0]!.replaceAll("~1", "/").replaceAll("~0", "~"); + unescapeJsonPointerToken(pointer.slice(1).split("/")[0]!); /** Of these names, the top-level members a body carries outside the entity at `pointer`, or anywhere in a document (no pointer); none when the entity is the whole body. */ export const envelopeMembers = ( diff --git a/src/domain/analysisView/environmentStateRules.ts b/src/domain/analysisView/environmentStateRules.ts index 48a49dacf3..fc979af021 100644 --- a/src/domain/analysisView/environmentStateRules.ts +++ b/src/domain/analysisView/environmentStateRules.ts @@ -1,3 +1,4 @@ +import { escapeJsonPointerToken, jsonPointerTokens } from "../jsonPointer.js"; import { iterateUrlQueryParameters, parseUrlQueryParameterNames, @@ -575,15 +576,7 @@ export const deeperThan = (value: JsonValue, limit: number): boolean => { }; export const pointerTo = (name: string): string => - `/${name.replaceAll("~", "~0").replaceAll("/", "~1")}`; -/** The unescaped reference tokens of a pointer. */ -export const pointerTokens = (pointer: string): string[] => - pointer === "" - ? [] - : pointer - .slice(1) - .split("/") - .map((token) => token.replaceAll("~1", "/").replaceAll("~0", "~")); + `/${escapeJsonPointerToken(name)}`; const memberOf = ( body: JsonValue | undefined, @@ -597,7 +590,7 @@ const soleElement = (value: JsonValue | undefined): JsonValue | undefined => /** The value at a pointer: the root, one member, or element 0 of a one-element array at the root or in one member. */ export const at = (body: JsonValue, pointer: string): JsonValue | undefined => { - const [first, second, ...rest] = pointerTokens(pointer); + const [first, second, ...rest] = jsonPointerTokens(pointer); if (first === undefined) return body; if (second !== undefined) return second === "0" && rest.length === 0 @@ -616,7 +609,7 @@ export const replaceAt = ( pointer: string, value: JsonValue, ): JsonValue => { - const [first, second] = pointerTokens(pointer); + const [first, second] = jsonPointerTokens(pointer); if (first === undefined) return value; if (at(body, pointer) === undefined) return body; if (!isObject(body)) return [value]; diff --git a/src/domain/analysisView/environmentStateVariedCalls.test.ts b/src/domain/analysisView/environmentStateVariedCalls.test.ts index f157859c3b..94c8a2424a 100644 --- a/src/domain/analysisView/environmentStateVariedCalls.test.ts +++ b/src/domain/analysisView/environmentStateVariedCalls.test.ts @@ -9,7 +9,6 @@ import { DEFAULT_THEN_NUMBERED_ROWS, DONE_CLOCK_ROWS, DONE_MICRO_ROWS, - NUMBERED_THEN_BLANK_ROWS, OWNER_CLEARED_THEN_NAMED_ROWS, SENT_DELETED_THEN_NUMBERED_ROWS, SENT_IN_ANOTHER_PROJECT_ROWS, diff --git a/src/domain/analysisView/environmentStateWriteApply.test.ts b/src/domain/analysisView/environmentStateWriteApply.test.ts index e2e84fd59f..89c9af7505 100644 --- a/src/domain/analysisView/environmentStateWriteApply.test.ts +++ b/src/domain/analysisView/environmentStateWriteApply.test.ts @@ -136,10 +136,19 @@ it.each<[string, JsonObject | undefined, string]>([ }); it.each<[string, JsonValue | undefined]>([ - ["", { a: [{ b: 1 }, { b: [2, 3] }], "x/y": { "m~n": true }, "": "blank" }], + [ + "", + { + a: [{ b: 1 }, { b: [2, 3] }], + "x/y": { "m~n": true }, + "~1": "literal tilde-one", + "": "blank", + }, + ], ["/a/0/b", 1], ["/a/1/b/1", 3], ["/x~1y/m~0n", true], + ["/~01", "literal tilde-one"], ["/", "blank"], ["/a/2/b", undefined], ["/a/01/b", undefined], @@ -151,6 +160,7 @@ it.each<[string, JsonValue | undefined]>([ const body = { a: [{ b: 1 }, { b: [2, 3] }], "x/y": { "m~n": true }, + "~1": "literal tilde-one", "": "blank", }; expect(valueAt(body, pointer)).toEqual(expected); diff --git a/src/domain/analysisView/environmentStateWriteApply.ts b/src/domain/analysisView/environmentStateWriteApply.ts index 3386f1a3d0..e64aba21e1 100644 --- a/src/domain/analysisView/environmentStateWriteApply.ts +++ b/src/domain/analysisView/environmentStateWriteApply.ts @@ -1,3 +1,4 @@ +import { jsonPointerTokens } from "../jsonPointer.js"; import type { JsonValue } from "../jsonValue.js"; import { err, ok, type Result } from "../result.js"; import { @@ -6,7 +7,6 @@ import { at, isObject, pointerTo, - pointerTokens, replaceAt, same, scalarText, @@ -179,7 +179,7 @@ export const valueAt = ( pointer: string, ): JsonValue | undefined => { let value: JsonValue | undefined = body; - for (const token of pointerTokens(pointer)) { + for (const token of jsonPointerTokens(pointer)) { if (Array.isArray(value)) { // JSON Pointer array indexes are canonical decimal tokens; coercing // arbitrary text would make /01 alias /1 and can turn huge indexes into @@ -343,7 +343,7 @@ const copiedShapes = ( rules.flatMap((rule): [string, JsonValue][] => { if (rule.rule !== "copy" || !Object.hasOwn(observed, rule.field)) return []; - const tokens = pointerTokens(rule.from); + const tokens = jsonPointerTokens(rule.from); return tokens.length === 1 ? [[tokens[0]!, observed[rule.field]!]] : []; }), ); @@ -708,7 +708,7 @@ export const writtenResponse = ( ); const answer = (written.unloaded ?? []).reduce( (rendered, path) => { - const tokens = pointerTokens(path); + const tokens = jsonPointerTokens(path); const kept = memberAt(shown, tokens); if (kept !== undefined) return withMemberAt(rendered, tokens, kept); // A member the answer left out of an object it showed stays out. diff --git a/src/domain/analysisView/environmentStateWriteChecks.ts b/src/domain/analysisView/environmentStateWriteChecks.ts index bb01c1b589..b3d304790d 100644 --- a/src/domain/analysisView/environmentStateWriteChecks.ts +++ b/src/domain/analysisView/environmentStateWriteChecks.ts @@ -1,6 +1,10 @@ import { ServedOrder } from "./environmentStateWriteOrder.js"; import type { JsonValue } from "../jsonValue.js"; import { compositeKey } from "../unicodeCodePointOrder.js"; +import { + environmentCollectionKey, + environmentEntityKey, +} from "./environmentStateIdentity.js"; import { ownMember, at, @@ -35,9 +39,9 @@ import type { InsertRule } from "./environmentStateWriteRules.js"; import { withoutMembers } from "./environmentStateResponseMembers.js"; export const entityTarget = (type: string, collection: string, id: string) => - `e${compositeKey([type, collection, id])}`; + `e${environmentEntityKey(type, collection, id)}`; export const collectionTarget = (type: string, collection: string) => - `c${compositeKey([type, collection])}`; + `c${environmentCollectionKey(type, collection)}`; export const documentTarget = (tool: string) => `d${compositeKey([tool])}`; /** A target `remove` dropped, so a later absence can still name the collection and id serving resolved. */ diff --git a/src/domain/analysisView/environmentStateWriteComposed.test.ts b/src/domain/analysisView/environmentStateWriteComposed.test.ts index c34caebb1a..c8d7c6d2c7 100644 --- a/src/domain/analysisView/environmentStateWriteComposed.test.ts +++ b/src/domain/analysisView/environmentStateWriteComposed.test.ts @@ -272,34 +272,30 @@ describe("compositions serving could not keep", () => { ); }); -it( - "indexes a wide body's repeated values once, and only when a string is left to compose", - { timeout: 5_000 }, - () => { - const n = 8_000; - const shared = `https://shared.example.test/${"x".repeat(64)}`; - const body: JsonObject = Object.fromEntries( - Array.from({ length: n }, (_, index) => [`e${String(index)}`, shared]), - ); - const rules: FieldRule[] = Array.from({ length: n }, (_, index) => ({ - field: `e${String(index)}`, - rule: "echo", - })); - let reads = 0; - const counted = new Proxy(body, { - get: (target, key) => { - reads += 1; - return Reflect.get(target, key); - }, - }); - const composer = createdComposer(counted, rules, []); - expect(reads).toBe(0); - expect(composer("e0")).toBeUndefined(); - expect(composer("e1")).toBeUndefined(); - // One read per source to index them, once, and one per composed field. - expect(reads).toBe(n + 2); - }, -); +it("indexes a wide body's repeated values once, and only when a string is left to compose", () => { + const n = 8_000; + const shared = `https://shared.example.test/${"x".repeat(64)}`; + const body: JsonObject = Object.fromEntries( + Array.from({ length: n }, (_, index) => [`e${String(index)}`, shared]), + ); + const rules: FieldRule[] = Array.from({ length: n }, (_, index) => ({ + field: `e${String(index)}`, + rule: "echo", + })); + let reads = 0; + const counted = new Proxy(body, { + get: (target, key) => { + reads += 1; + return Reflect.get(target, key); + }, + }); + const composer = createdComposer(counted, rules, []); + expect(reads).toBe(0); + expect(composer("e0")).toBeUndefined(); + expect(composer("e1")).toBeUndefined(); + // One read per source to index them, once, and one per composed field. + expect(reads).toBe(n + 2); +}); describe("slug spellings", () => { it("spells & in a slug part as the word only when the part says so", () => { diff --git a/src/domain/analysisView/environmentStateWriteCopies.test.ts b/src/domain/analysisView/environmentStateWriteCopies.test.ts index 1ad8fe9585..6720c41f1f 100644 --- a/src/domain/analysisView/environmentStateWriteCopies.test.ts +++ b/src/domain/analysisView/environmentStateWriteCopies.test.ts @@ -85,38 +85,37 @@ describe("members a created field derives from", () => { }); }); -it( - "finds the echoed members a wide body's constants hold without searching each text for each", - { timeout: 5_000 }, - () => { - const n = 8_000; - const body = Object.fromEntries( - Array.from({ length: n }, (_, index) => [ - `e${String(index)}`, - `echoed value ${String(index).padStart(6, "0")}`, - ]), - ); - const constants = Object.fromEntries( - Array.from({ length: n }, (_, index) => [ - `k${String(index)}`, - `constant text ${String(index)}`, - ]), - ); - const result = create( - { - id: 1, - ...body, - ...constants, - last: `see ${body[`e${String(n - 1)}`]!}`, - }, - body, - () => [], - ); - expect(result.fixed).toEqual([ - { member: `e${String(n - 1)}`, sent: body[`e${String(n - 1)}`] }, - ]); - }, -); +it("finds the echoed members a wide body's constants hold without searching each text for each", () => { + const n = 8_000; + const body = Object.fromEntries( + Array.from({ length: n }, (_, index) => [ + `e${String(index)}`, + `echoed value ${String(index).padStart(6, "0")}`, + ]), + ); + const constants = Object.fromEntries( + Array.from({ length: n }, (_, index) => [ + `k${String(index)}`, + `constant text ${String(index)}`, + ]), + ); + const started = performance.now(); + const result = create( + { + id: 1, + ...body, + ...constants, + last: `see ${body[`e${String(n - 1)}`]!}`, + }, + body, + () => [], + ); + // Searching every text for every echoed member is quadratic in the body. + expect(performance.now() - started).toBeLessThan(5_000); + expect(result.fixed).toEqual([ + { member: `e${String(n - 1)}`, sent: body[`e${String(n - 1)}`] }, + ]); +}); describe("coincidences no member derives a created field from", () => { it.each([ diff --git a/src/domain/analysisView/environmentStateWriteCopies.ts b/src/domain/analysisView/environmentStateWriteCopies.ts index 44f2dec475..aa162e9d54 100644 --- a/src/domain/analysisView/environmentStateWriteCopies.ts +++ b/src/domain/analysisView/environmentStateWriteCopies.ts @@ -1,7 +1,7 @@ +import { jsonPointerTokens } from "../jsonPointer.js"; import type { JsonValue } from "../jsonValue.js"; import { ownMember, - pointerTokens, scalarText, type JsonObject, type StateEntity, @@ -49,7 +49,7 @@ export const storedMembers = ( rules.flatMap((rule) => { if (rule.rule === "echo") return [rule.field]; if (rule.rule === "reference") return [rule.member]; - const tokens = rule.rule === "copy" ? pointerTokens(rule.from) : []; + const tokens = rule.rule === "copy" ? jsonPointerTokens(rule.from) : []; // A member also stored otherwise under its own name is not stored as sent. return tokens.length === 1 && !Object.hasOwn(entity, tokens[0]!) ? tokens diff --git a/src/domain/analysisView/environmentStateWriteExplain.test.ts b/src/domain/analysisView/environmentStateWriteExplain.test.ts index 2624c33e2d..ea6289b6b3 100644 --- a/src/domain/analysisView/environmentStateWriteExplain.test.ts +++ b/src/domain/analysisView/environmentStateWriteExplain.test.ts @@ -922,66 +922,63 @@ it.each<[string, string, boolean]>([ expect(prefixRelated(right, left)).toBe(related); }); -it( - "explains many copied and slugged fields of a wide body without searching it again for each", - { timeout: 5_000 }, - () => { - const n = 8_000; - const value = (index: number) => `value-${String(index).padStart(6, "0")}`; - const nested = Object.fromEntries( - Array.from({ length: n }, (_, index) => [ - `g${String(index)}`, - { s: value(index) }, - ]), - ); - const copies = Object.fromEntries( - Array.from({ length: n }, (_, index) => [ - `c${String(index)}`, - value(index), - ]), - ); - const copied = explainCreate(created({ id: 1, ...copies }, nested)); - expect(ruleFor(copied.rules, `c${String(n - 1)}`)).toEqual({ - field: `c${String(n - 1)}`, - rule: "copy", - from: `/g${String(n - 1)}/s`, - }); - // Strings no member slugs to, in a body of many members, each tried as a slug. - const flat = Object.fromEntries( - Array.from({ length: n }, (_, index) => [ - `m${String(index)}`, - `Member ${String(index)}`, - ]), - ); - const others = Object.fromEntries( - Array.from({ length: n }, (_, index) => [ - `o${String(index)}`, - `x${String(index)}`, - ]), - ); - const slugged = explainCreate( - created({ id: 1, ...others, s: "member-7" }, flat), - ); - expect(ruleFor(slugged.rules, "s")).toEqual({ - field: "s", - rule: "slug", - from: "m7", - }); - }, -); +it("explains many copied and slugged fields of a wide body without searching it again for each", () => { + const n = 8_000; + const value = (index: number) => `value-${String(index).padStart(6, "0")}`; + const nested = Object.fromEntries( + Array.from({ length: n }, (_, index) => [ + `g${String(index)}`, + { s: value(index) }, + ]), + ); + const copies = Object.fromEntries( + Array.from({ length: n }, (_, index) => [ + `c${String(index)}`, + value(index), + ]), + ); + const started = performance.now(); + const copied = explainCreate(created({ id: 1, ...copies }, nested)); + expect(ruleFor(copied.rules, `c${String(n - 1)}`)).toEqual({ + field: `c${String(n - 1)}`, + rule: "copy", + from: `/g${String(n - 1)}/s`, + }); + // Strings no member slugs to, in a body of many members, each tried as a slug. + const flat = Object.fromEntries( + Array.from({ length: n }, (_, index) => [ + `m${String(index)}`, + `Member ${String(index)}`, + ]), + ); + const others = Object.fromEntries( + Array.from({ length: n }, (_, index) => [ + `o${String(index)}`, + `x${String(index)}`, + ]), + ); + const slugged = explainCreate( + created({ id: 1, ...others, s: "member-7" }, flat), + ); + expect(ruleFor(slugged.rules, "s")).toEqual({ + field: "s", + rule: "slug", + from: "m7", + }); + // Searching the body again for each field is quadratic in its members. + expect(performance.now() - started).toBeLessThan(5_000); +}); -it( - "searches a wide request body for a copied value in linear time", - { timeout: 5_000 }, - () => { - // Shifting each value off the queue made one fruitless search over 500,000 zeros take about a minute. - const wide = Array.from({ length: 500_000 }, () => 0); - const result = explainCreate( - created({ id: 1, note: "not anywhere in the body" }, { items: wide }), - ); - expect(ruleFor(result.rules, "note")).toBeUndefined(); - }, -); +it("searches a wide request body for a copied value in linear time", () => { + // Shifting each value off the queue made one fruitless search over 500,000 zeros take about a minute. + const wide = Array.from({ length: 500_000 }, () => 0); + const started = performance.now(); + const result = explainCreate( + created({ id: 1, note: "not anywhere in the body" }, { items: wide }), + ); + expect(performance.now() - started).toBeLessThan(5_000); + expect(ruleFor(result.rules, "note")).toBeUndefined(); +}); const ID = (n: number): string => `00000000-0000-4000-8000-${String(n).padStart(12, "0")}`; diff --git a/src/domain/analysisView/nativeObservationView.ts b/src/domain/analysisView/nativeObservationView.ts index 226ea044ff..95549d7d7f 100644 --- a/src/domain/analysisView/nativeObservationView.ts +++ b/src/domain/analysisView/nativeObservationView.ts @@ -31,7 +31,7 @@ import { import { projectNativeGraphRows } from "./nativeGraphView.js"; /** What an address page leaves in the complete parent, when recorded. */ -export const XREFS_ADDRESS_PAGE_LIMITATION = +const XREFS_ADDRESS_PAGE_LIMITATION = "This page contains source addresses only, not reference kinds or provenance. Retrieve or export the complete parent Evidence for any additional provider details it recorded."; const readAddresses = createAnalysisViewResultReader(nativeAddressListSchema); diff --git a/src/domain/analysisView/webBundleView.test.ts b/src/domain/analysisView/webBundleView.test.ts index 3c6ed6133e..25639df204 100644 --- a/src/domain/analysisView/webBundleView.test.ts +++ b/src/domain/analysisView/webBundleView.test.ts @@ -1,5 +1,6 @@ import { expect, it } from "vitest"; import type { WebBundleAnalysis } from "../webBundleAnalysis.js"; +import { sampleWebBundleAnalysis } from "../../../tests/fixtures/webBundleAnalysis.js"; import { AnalysisInputError } from "../analysisErrorCore.js"; import { projectAnalysisError } from "../analysisErrorProjection.js"; import { @@ -8,104 +9,6 @@ import { } from "./analysisView.js"; import { analysisViewRequestSchema } from "./analysisViewRequest.js"; -const sampleAnalysis = (): WebBundleAnalysis => ({ - capture: { - target_url: "https://example.test/app", - target_origin: "https://example.test", - scripts_observed: 3, - scripts_analyzed: 2, - source_artifacts: [], - }, - observations: { - chunks: { - nodes: [ - { - script_key: "app.js", - url: "https://example.test/app.js", - artifact_sha256: "a".repeat(64), - bytes: 1200, - }, - { - script_key: "vendor.js", - url: "https://example.test/vendor.js", - artifact_sha256: "b".repeat(64), - bytes: 4500, - }, - ], - edges: [], - }, - routes: [], - endpoints: [ - { - value: "/api/users", - mechanism: "call:fetch", - location: { script_key: "app.js", line: 10, column: 2 }, - }, - { - value: "/api/users", - mechanism: "call:fetch", - location: { script_key: "vendor.js", line: 42, column: 8 }, - }, - { - value: "/api/orders", - mechanism: "call:sendBeacon", - location: { script_key: "app.js", line: 85, column: 4 }, - }, - ], - webmcp_declarations: [], - source_maps: { - status: "not_requested", - requested: 0, - processed: 0, - items: [], - }, - }, - inferences: [ - { - kind: "endpoint_candidate", - value: "/api/dynamic-user", - confidence: "low", - basis: [ - { - script_key: "app.js", - artifact_sha256: "a".repeat(64), - line: 25, - column: 4, - detector: "call:client.get:unverified-network-receiver", - }, - ], - }, - { - kind: "vendor_fingerprint", - value: "React", - confidence: "high", - basis: [ - { - script_key: "vendor.js", - artifact_sha256: "b".repeat(64), - line: 1, - column: 0, - detector: "vendor:react", - }, - ], - }, - ], - unknowns: [ - { - dimension: "network", - reason: "Unverified client receiver", - affected_script_keys: ["app.js"], - }, - ], - completeness: { - status: "partial", - parsed_scripts: 2, - parse_failures: 1, - visited_ast_nodes: 150, - }, - limitations: ["Script evaluation was omitted"], -}); - const makeParent = ( overrides?: Partial, ): AnalysisViewParent => ({ @@ -114,7 +17,7 @@ const makeParent = ( operation: "analyze_web_bundle", limitations: ["Parent run was bounded"], normalizedResult: { - ...sampleAnalysis(), + ...sampleWebBundleAnalysis(), ...overrides, }, }); @@ -134,7 +37,7 @@ const query = ( it.each([undefined, null, "https://example.test"])( "preserves historical omitted and explicit target origins: %s", (origin) => { - const report = sampleAnalysis(); + const report = sampleWebBundleAnalysis(); if (origin === undefined) delete report.capture.target_origin; else report.capture.target_origin = origin; const parent = makeParent(report); @@ -247,7 +150,7 @@ it("pages endpoints and concatenated pages match original records exactly, prese }); const concatenated = [...page1.items, ...page2.items]; - const original = sampleAnalysis().observations.endpoints; + const original = sampleWebBundleAnalysis().observations.endpoints; expect(concatenated).toEqual( original.map((row, source_index) => ({ source_index, ...row })), ); @@ -399,7 +302,7 @@ it("returns typed AnalysisResourceConstraintError when a single record exceeds 1 }; const parent = makeParent({ observations: { - ...sampleAnalysis().observations, + ...sampleWebBundleAnalysis().observations, endpoints: [hugeEndpoint], }, }); @@ -430,7 +333,7 @@ it.each([undefined, ["call:fetch"]])( }); const parent = makeParent({ observations: { - ...sampleAnalysis().observations, + ...sampleWebBundleAnalysis().observations, endpoints: [ largeRecord(1), largeRecord(2), @@ -480,7 +383,7 @@ it("rejects non-web-bundle parent operation with invalid_value", () => { evidenceId: `ev_${"d".repeat(64)}`, operation: "analyze_javascript_application", limitations: [], - normalizedResult: sampleAnalysis(), + normalizedResult: sampleWebBundleAnalysis(), }; const result = projectAnalysisView( @@ -500,7 +403,7 @@ it("rejects non-web-bundle parent operation with invalid_value", () => { }); it("continues filtered pages by matching ordinal while preserving original indices", () => { - const report = sampleAnalysis(); + const report = sampleWebBundleAnalysis(); const row = report.observations.endpoints[0]!; const endpoints = [ { ...row, mechanism: "call:sendBeacon" }, diff --git a/src/domain/android/androidJniBindings.test.ts b/src/domain/android/androidJniBindings.test.ts index 49a034955e..9e82a6188b 100644 --- a/src/domain/android/androidJniBindings.test.ts +++ b/src/domain/android/androidJniBindings.test.ts @@ -6,12 +6,11 @@ import { type AndroidJniLibraryObservation, type AndroidJniSelection, } from "./androidJniBindings.js"; -import type { - AndroidNativeDeclaration, - AndroidNativeDeclarations, -} from "./androidNativeDeclarations.js"; +import type { AndroidNativeDeclarations } from "./androidNativeDeclarations.js"; import type { AndroidJniLibraryFacts } from "./androidJniLibraryFacts.js"; +type AndroidNativeDeclaration = AndroidNativeDeclarations["methods"][number]; + const declaration = ( changes: Partial = {}, ): AndroidNativeDeclaration => ({ diff --git a/src/domain/android/androidMapping.ts b/src/domain/android/androidMapping.ts index d49c924905..6f24529a85 100644 --- a/src/domain/android/androidMapping.ts +++ b/src/domain/android/androidMapping.ts @@ -6,7 +6,6 @@ import { PROGUARD_MAPPING_LIMITS, proguardResolutionBytes, type ProguardMapping, - type ProguardResolution, resolveProguardClass, resolveProguardField, resolveProguardMethod, diff --git a/src/domain/android/androidNativeDeclarations.ts b/src/domain/android/androidNativeDeclarations.ts index 7d1b5d4a45..b634781e9d 100644 --- a/src/domain/android/androidNativeDeclarations.ts +++ b/src/domain/android/androidNativeDeclarations.ts @@ -32,9 +32,6 @@ export const androidNativeDeclarationsSchema = z.strictObject({ unknowns: z.array(z.string()), }); -export type AndroidNativeDeclaration = z.infer< - typeof androidNativeDeclarationSchema ->; export type AndroidNativeDeclarations = z.infer< typeof androidNativeDeclarationsSchema >; diff --git a/src/domain/apiContract/parseApiContract.ts b/src/domain/apiContract/parseApiContract.ts index f08178f2bb..e98446a42d 100644 --- a/src/domain/apiContract/parseApiContract.ts +++ b/src/domain/apiContract/parseApiContract.ts @@ -1,3 +1,4 @@ +import { escapeJsonPointerToken } from "../jsonPointer.js"; import { AnalysisCancelledError, AnalysisInputError, @@ -44,8 +45,6 @@ const record = (value: unknown): Record | undefined => typeof value === "object" && value !== null && !Array.isArray(value) ? (value as Record) : undefined; -const pointerPart = (value: string): string => - value.replaceAll("~", "~0").replaceAll("/", "~1"); const identity = (prefix: string, facts: unknown) => `${prefix}_${digestCanonicalValue(facts)}`; @@ -240,7 +239,7 @@ function* additionalOperations( if (examined++ % 128 === 0) yield; const admitted = inventory.reserve(512 + method.length * 24); if (!admitted.ok) return admitted; - const source = `${pointer}/${pointerPart(method)}`; + const source = `${pointer}/${escapeJsonPointerToken(method)}`; const reserved = reservedAdditionalMethods.has(method); const added = !httpToken.test(method) || reserved @@ -308,7 +307,7 @@ function* pathItem( if (!admitted.ok) return admitted; const diagnosed = inventory.diagnostic( "unknown-path-item-field", - `${pointer}/${pointerPart(key)}`, + `${pointer}/${escapeJsonPointerToken(key)}`, "This field is not an operation or a recognized Path Item field in the selected OpenAPI minor version.", ); if (!diagnosed.ok) return diagnosed; @@ -391,7 +390,7 @@ function* collectPaths( // Reserve expanded pointer/path strings before escaping or building a row. const admitted = inventory.reserve(512 + path.length * 16); if (!admitted.ok) return admitted; - const pointer = `/paths/${pointerPart(path)}`; + const pointer = `/paths/${escapeJsonPointerToken(path)}`; const read = path.startsWith("/") ? yield* pathItem(sourcePaths[path], { inventory, path, pointer, modern }) : inventory.diagnostic( diff --git a/src/domain/apkSignature/apkSignatureSchemes.test.ts b/src/domain/apkSignature/apkSignatureSchemes.test.ts index b7419eab2f..3c297c3c3e 100644 --- a/src/domain/apkSignature/apkSignatureSchemes.test.ts +++ b/src/domain/apkSignature/apkSignatureSchemes.test.ts @@ -5,10 +5,12 @@ import { contentDigestAlgorithm, decodeRotationLineage, decodeSigners, - MAX_LINEAGE_NODES, - MAX_SIGNERS, } from "./apkSignatureSchemes.js"; +// Fixed input boundaries exercise the decoder without importing its internal limits. +const MAX_SIGNERS = 32; +const MAX_LINEAGE_NODES = 32; + const ATTR_ID = 0xbeeff00d; const u32 = (n: number): number[] => [ diff --git a/src/domain/apkSignature/apkSignatureSchemes.ts b/src/domain/apkSignature/apkSignatureSchemes.ts index 8e7714a101..e4032a0851 100644 --- a/src/domain/apkSignature/apkSignatureSchemes.ts +++ b/src/domain/apkSignature/apkSignatureSchemes.ts @@ -66,12 +66,12 @@ const MAX_ELEMENTS = 4096; * Resource admission for signer fan-out, including per-signer X.509 work. * This is not an APK format maximum; excess records return a capacity error. */ -export const MAX_SIGNERS = 32; +const MAX_SIGNERS = 32; /** * Resource admission for lineage fan-out before certificate materialisation. * This is not a format maximum; longer declared chains are refused explicitly. */ -export const MAX_LINEAGE_NODES = 32; +const MAX_LINEAGE_NODES = 32; /** The only proof-of-rotation lineage encoding version apksig accepts. */ const LINEAGE_VERSION = 1; diff --git a/src/domain/apkSignature/apkV1Signature.test.ts b/src/domain/apkSignature/apkV1Signature.test.ts index e8518230f9..46f7553466 100644 --- a/src/domain/apkSignature/apkV1Signature.test.ts +++ b/src/domain/apkSignature/apkV1Signature.test.ts @@ -5,11 +5,13 @@ import { centralDirectoryV1SignatureEntries, detectV1Signature, localEntryData, - MAX_DUPLICATE_NAME_SAMPLE, - MAX_V1_SIGNATURE_ENTRIES, type ApkV1Entry, } from "./apkV1Signature.js"; +// Fixed examples keep the entry and output-sample limits independently checked. +const MAX_V1_SIGNATURE_ENTRIES = 4096; +const MAX_DUPLICATE_NAME_SAMPLE = 5; + /** A central-directory entry with no stored data. */ const entry = (name: string): ApkV1Entry => ({ name, diff --git a/src/domain/apkSignature/apkV1Signature.ts b/src/domain/apkSignature/apkV1Signature.ts index 554ee06211..662dbc5d24 100644 --- a/src/domain/apkSignature/apkV1Signature.ts +++ b/src/domain/apkSignature/apkV1Signature.ts @@ -54,7 +54,7 @@ export type ApkCentralDirectoryError = * Signing-relevant `META-INF/` entries retained. A signed APK holds a manifest * plus one `.SF` and one block per v1 signer; thousands is already pathological. */ -export const MAX_V1_SIGNATURE_ENTRIES = 4096; +const MAX_V1_SIGNATURE_ENTRIES = 4096; /** * v1 signers (signature block + `.SF` pairs) decoded. apksig accepts at most @@ -63,7 +63,7 @@ export const MAX_V1_SIGNATURE_ENTRIES = 4096; export const MAX_V1_SIGNERS = 32; /** Duplicated central-directory names decoded for the report; the rest are counted. */ -export const MAX_DUPLICATE_NAME_SAMPLE = 5; +const MAX_DUPLICATE_NAME_SAMPLE = 5; /** A signing-relevant central-directory entry and where its data is stored. */ export interface ApkV1Entry { diff --git a/src/domain/apkSignature/apkV1SignatureBlock.test.ts b/src/domain/apkSignature/apkV1SignatureBlock.test.ts index 4f60aea58f..00e41a5c83 100644 --- a/src/domain/apkSignature/apkV1SignatureBlock.test.ts +++ b/src/domain/apkSignature/apkV1SignatureBlock.test.ts @@ -19,12 +19,12 @@ import { signerInfo, tlv, } from "../../../tests/fixtures/apkSignature/v1SignatureBlock.js"; -import { - decodeV1SignatureBlock, - MAX_ASN1_ELEMENTS, - MAX_V1_BLOCK_CERTIFICATES, - MAX_V1_SIGNER_INFOS, -} from "./apkV1SignatureBlock.js"; +import { decodeV1SignatureBlock } from "./apkV1SignatureBlock.js"; + +// Fixed input boundaries exercise the decoder without importing its internal limits. +const MAX_V1_BLOCK_CERTIFICATES = 32; +const MAX_V1_SIGNER_INFOS = 32; +const MAX_ASN1_ELEMENTS = 65_536; const sha256 = (bytes: Uint8Array): string => createHash("sha256").update(bytes).digest("hex"); diff --git a/src/domain/apkSignature/apkV1SignatureBlock.ts b/src/domain/apkSignature/apkV1SignatureBlock.ts index 29e259815a..5946e8303f 100644 --- a/src/domain/apkSignature/apkV1SignatureBlock.ts +++ b/src/domain/apkSignature/apkV1SignatureBlock.ts @@ -35,13 +35,13 @@ export type ApkV1BlockError = }; /** Certificates in one block: a signer leaf plus a short chain is the norm. */ -export const MAX_V1_BLOCK_CERTIFICATES = 32; +const MAX_V1_BLOCK_CERTIFICATES = 32; /** SignerInfos in one block; apksig verifies one, more is pathological. */ -export const MAX_V1_SIGNER_INFOS = 32; +const MAX_V1_SIGNER_INFOS = 32; /** Nesting depth; SignedData → X.509 extensions is under 12 levels deep. */ const MAX_ASN1_DEPTH = 32; /** Elements read while decoding one block; bounds work for any byte length. */ -export const MAX_ASN1_ELEMENTS = 65_536; +const MAX_ASN1_ELEMENTS = 65_536; export interface ApkV1SignerInfo { readonly identifiedBy: "issuer-and-serial" | "subject-key-identifier"; diff --git a/src/domain/apple/interfaceBuilderGraph.ts b/src/domain/apple/interfaceBuilderGraph.ts index 61f1dbab49..82993bb040 100644 --- a/src/domain/apple/interfaceBuilderGraph.ts +++ b/src/domain/apple/interfaceBuilderGraph.ts @@ -1,6 +1,5 @@ import { z } from "zod"; import { - bundleResourceRootSchema, bundleResourceBoundResultSchema, type BundleResourceRoot, } from "./bundleResourceRoot.js"; diff --git a/src/domain/artifactComparison.test.ts b/src/domain/artifactComparison.test.ts index 60b0267574..220087afcd 100644 --- a/src/domain/artifactComparison.test.ts +++ b/src/domain/artifactComparison.test.ts @@ -1,9 +1,6 @@ import { expect, it } from "vitest"; import { ARTIFACT_COMPARISON_EXAMPLE } from "../contracts/artifactComparisonExample.js"; -import { - compareArtifacts, - FORMAT_DECLARATION_UNKNOWN_LIMITATION, -} from "./artifactComparison.js"; +import { compareArtifacts } from "./artifactComparison.js"; import { artifactInventoryResultSchema, type ArtifactInventoryResult, @@ -684,7 +681,11 @@ it.each([ status: "unknown", changes: [{ classification: "unknown", dimensions: ["availability"] }], }); - expect(result.limitations).toEqual([FORMAT_DECLARATION_UNKNOWN_LIMITATION]); + expect(result.limitations).toEqual([ + expect.stringMatching( + /metadata declarations are missing.*complete declaration unavailable/u, + ), + ]); }); it("does not compare ESP signature block metadata", () => { @@ -815,7 +816,11 @@ it("compares a node-less record's occurrence fields but not its limitations", () dimensions: ["metadata", "availability"], }), ]); - expect(result.limitations).toEqual([FORMAT_DECLARATION_UNKNOWN_LIMITATION]); + expect(result.limitations).toEqual([ + expect.stringMatching( + /metadata declarations are missing.*complete declaration unavailable/u, + ), + ]); // A record's `executable` marking is a declared field like `encrypted`. expect( compareArtifacts( diff --git a/src/domain/artifactComparison.ts b/src/domain/artifactComparison.ts index 2cfd99de6e..f8395898ec 100644 --- a/src/domain/artifactComparison.ts +++ b/src/domain/artifactComparison.ts @@ -74,23 +74,42 @@ const artifactChangeSchema = z.object({ }); /** Deterministic artifact comparison with every change returned inline. */ -export const artifactComparisonResultSchema = z.object({ - status: comparisonStatusSchema, - left_manifest_id: prefixedDigestSchema("agm"), - right_manifest_id: prefixedDigestSchema("agm"), - summary: z.object({ - unchanged: z.int().min(0), - added: z.int().min(0), - removed: z.int().min(0), - changed: z.int().min(0), - unknown: z.int().min(0), - contradiction: z.int().min(0).default(0), - }), - changes: z.array(artifactChangeSchema), - // Absent on legacy records; absence does not identify a contradicting side. - integrity_mismatch_evidence_ids: z.array(evidenceIdSchema).optional(), - limitations: z.array(z.string()), -}); +export const artifactComparisonResultSchema = z + .object({ + status: comparisonStatusSchema, + left_manifest_id: prefixedDigestSchema("agm"), + right_manifest_id: prefixedDigestSchema("agm"), + summary: z.object({ + unchanged: z.int().min(0), + added: z.int().min(0), + removed: z.int().min(0), + changed: z.int().min(0), + unknown: z.int().min(0), + contradiction: z.int().min(0).default(0), + }), + changes: z.array(artifactChangeSchema), + // Absent on legacy records; absence does not identify a contradicting side. + integrity_mismatch_evidence_ids: z.array(evidenceIdSchema).optional(), + limitations: z.array(z.string()), + }) + .superRefine((result, context) => { + if ( + result.status === "unchanged" && + (result.changes.length > 0 || + result.summary.added > 0 || + result.summary.removed > 0 || + result.summary.changed > 0 || + result.summary.unknown > 0 || + result.summary.contradiction > 0 || + (result.integrity_mismatch_evidence_ids?.length ?? 0) > 0) + ) + context.addIssue({ + code: "custom", + path: ["status"], + message: + "An unchanged artifact comparison cannot retain changes, non-zero change counts, or integrity mismatches.", + }); + }); export type ArtifactComparisonResult = z.infer< typeof artifactComparisonResultSchema @@ -98,7 +117,7 @@ export type ArtifactComparisonResult = z.infer< type ArtifactChange = z.infer; /** Comparison limitation when a path's format declarations cannot be compared. */ -export const FORMAT_DECLARATION_UNKNOWN_LIMITATION = +const FORMAT_DECLARATION_UNKNOWN_LIMITATION = "Format metadata declarations are missing on one side or differ in kind for matching paths; omitted metadata may be a legacy or non-container observation and leaves the complete declaration unavailable."; const comparisonInventory = (input: unknown, side: "left" | "right") => { diff --git a/src/domain/artifactExpansionBudget.test.ts b/src/domain/artifactExpansionBudget.test.ts index 38b67b54c8..f95c9930b8 100644 --- a/src/domain/artifactExpansionBudget.test.ts +++ b/src/domain/artifactExpansionBudget.test.ts @@ -1,21 +1,10 @@ import { describe, expect, it } from "vitest"; -import { - ARTIFACT_EXPANSION_RATIO, - artifactExpansionBudgetBytes, - MINIMUM_ARTIFACT_EXPANDED_BYTES, -} from "./artifactExpansionBudget.js"; +import { artifactExpansionBudgetBytes } from "./artifactExpansionBudget.js"; describe("artifactExpansionBudgetBytes", () => { it("grants at least the floor and grows with the encoded archive size", () => { - expect(artifactExpansionBudgetBytes(0)).toBe( - MINIMUM_ARTIFACT_EXPANDED_BYTES, - ); - expect(artifactExpansionBudgetBytes(1024)).toBe( - MINIMUM_ARTIFACT_EXPANDED_BYTES, - ); - const large = MINIMUM_ARTIFACT_EXPANDED_BYTES * 4; - expect(artifactExpansionBudgetBytes(large)).toBe( - large * ARTIFACT_EXPANSION_RATIO, - ); + expect(artifactExpansionBudgetBytes(0)).toBe(256 * 1024 * 1024); + expect(artifactExpansionBudgetBytes(1024)).toBe(256 * 1024 * 1024); + expect(artifactExpansionBudgetBytes(1024 * 1024)).toBe(500 * 1024 * 1024); }); it("keeps cumulative byte accounting within the exact number domain", () => { expect(artifactExpansionBudgetBytes(Number.MAX_SAFE_INTEGER)).toBe( diff --git a/src/domain/artifactExpansionBudget.ts b/src/domain/artifactExpansionBudget.ts index 07ca135dd7..e567ed2255 100644 --- a/src/domain/artifactExpansionBudget.ts +++ b/src/domain/artifactExpansionBudget.ts @@ -9,8 +9,8 @@ * an embedded executable carrier. The safe-integer ceiling preserves exact byte * accounting at the JSON/number boundary. */ -export const ARTIFACT_EXPANSION_RATIO = 500; -export const MINIMUM_ARTIFACT_EXPANDED_BYTES = 256 * 1024 * 1024; +const ARTIFACT_EXPANSION_RATIO = 500; +const MINIMUM_ARTIFACT_EXPANDED_BYTES = 256 * 1024 * 1024; /** Largest total readable plaintext bytes for this encoded archive span. */ export const artifactExpansionBudgetBytes = (archiveBytes: number): number => diff --git a/src/domain/browserScenario.ts b/src/domain/browserScenario.ts index cca415dcfd..656a036390 100644 --- a/src/domain/browserScenario.ts +++ b/src/domain/browserScenario.ts @@ -223,23 +223,20 @@ const validateRequestSteps = ( }); }; -const validateStorage = (scenario: ScenarioShape): readonly string[] => { - const references: string[] = []; - scenario.storage.cookies.forEach((cookie) => { - references.push(...secretReferencesInValue(cookie.value)); - references.push(...destinationSecretReferences(cookie.destination)); - }); +function* storageSecretReferences(scenario: ScenarioShape): Generator { + for (const cookie of scenario.storage.cookies) { + yield* secretReferencesInValue(cookie.value); + yield* destinationSecretReferences(cookie.destination); + } for (const collection of ["local_storage", "session_storage"] as const) - scenario.storage[collection].forEach((storage) => { + for (const storage of scenario.storage[collection]) for (const entry of storage.entries) - references.push(...secretReferencesInValue(entry.value)); - }); - return references; -}; + yield* secretReferencesInValue(entry.value); +} const validateSecretReferences = ( scenario: ScenarioShape, - references: readonly string[], + references: ReadonlySet, context: z.RefinementCtx, ): void => { const declared = new Set(scenario.secrets.map(({ secret_id: id }) => id)); @@ -286,9 +283,12 @@ const validateBrowserScenario = ( validateRequestSteps(scenario, context); validateSavedValues(scenario, context); - const references = scenario.actions.flatMap(actionSecretReferences); - references.push(...destinationSecretReferences(scenario.start_url)); - references.push(...validateStorage(scenario)); + // Reference identity is global; source uses remain in the parsed scenario. + const references = new Set(scenario.actions.flatMap(actionSecretReferences)); + for (const reference of destinationSecretReferences(scenario.start_url)) + references.add(reference); + for (const reference of storageSecretReferences(scenario)) + references.add(reference); validateSecretReferences(scenario, references, context); }; diff --git a/src/domain/browserScenarioDiff.ts b/src/domain/browserScenarioDiff.ts index 86c1811be4..e74255567f 100644 --- a/src/domain/browserScenarioDiff.ts +++ b/src/domain/browserScenarioDiff.ts @@ -81,7 +81,7 @@ export const compareBrowserScenarios = ( input.before_scenario, input.after_scenario, ); - const incomparableReason = eventCoverageReason( + const incomparableReason = joinIncomparableReasons( familyIncomparableReason, networkContentCoverageReason(input.before_scenario, input.after_scenario), beforeEvents.coverageReason, @@ -499,20 +499,6 @@ const digestScenarioEvents = ( ); }; -const eventCoverageReason = ( - familyReason: string | undefined, - networkContentReason: string | undefined, - beforeNetworkReason: string | undefined, - afterNetworkReason: string | undefined, -): string | undefined => { - return joinIncomparableReasons( - familyReason, - networkContentReason, - beforeNetworkReason, - afterNetworkReason, - ); -}; - interface CompareCapturedValuesInput { readonly artifact: BrowserScenarioArtifactKind; readonly before: CaptureState; diff --git a/src/domain/callPath.test.ts b/src/domain/callPath.test.ts index 4186bf419f..a49d83e15b 100644 --- a/src/domain/callPath.test.ts +++ b/src/domain/callPath.test.ts @@ -275,7 +275,7 @@ describe("call path topology and capacity", () => { expect(result.status).toBe("unknown"); expect(result.search_scope.exhaustive).toBe(false); expect(result.limitations).toEqual(limitations); - }, 30_000); + }); it("retains every shortest path through merging branches and cycles", () => { const result = run([ diff --git a/src/domain/commandObservation.ts b/src/domain/commandObservation.ts index ff383e64be..cbe75404b6 100644 --- a/src/domain/commandObservation.ts +++ b/src/domain/commandObservation.ts @@ -9,7 +9,11 @@ export const commandObservationSchema = z.strictObject({ output_budget_bytes: z.int().positive().exactOptional(), output_budget_source: z.enum(["default", "request"]).exactOptional(), output_truncated: z.boolean(), - completion_status: z.enum(["complete", "unknown"]), + completion_status: z + .enum(["complete", "rejected", "unknown"]) + .describe( + "complete: the provider process completed. rejected: it completed, but the provider reported that it refused the command; limitations say what ran. unknown: the capture or process ended early.", + ), artifact_sha256_after: z .string() .regex(/^[a-f0-9]{64}$/u) diff --git a/src/domain/comparisonEvidenceResult.ts b/src/domain/comparisonEvidenceResult.ts index 0027acf47c..a671156482 100644 --- a/src/domain/comparisonEvidenceResult.ts +++ b/src/domain/comparisonEvidenceResult.ts @@ -104,10 +104,10 @@ export function readComparisonResult( evidence.normalized_result, ); if (!parsed.ok) return parsed; - const links = validateFindingLinks( - evidence, - parsed.value.changes.flatMap(({ evidence_links }) => evidence_links), - ); + const links = validateFindingLinks(evidence, [ + ...parsed.value.changes.flatMap(({ evidence_links }) => evidence_links), + ...(parsed.value.integrity_mismatch_evidence_ids ?? []), + ]); if (!links.ok) return links; return ok({ operation, evidence, result: parsed.value }); } diff --git a/src/domain/debuggerSessionErrors.ts b/src/domain/debuggerSessionErrors.ts new file mode 100644 index 0000000000..99ec96a5e7 --- /dev/null +++ b/src/domain/debuggerSessionErrors.ts @@ -0,0 +1,58 @@ +import { + AnalysisCapabilityUnavailableError, + AnalysisInputError, +} from "./analysisErrorCore.js"; + +/** Tool names and wording for one persistent debugger's session errors. */ +export interface DebuggerSessionTools { + readonly providerId: string; + readonly name: string; + readonly start: string; + readonly close: string; +} + +/** A session_id this server does not hold, unlike a retained stopped session. */ +export const unknownDebuggerSessionError = ( + tools: DebuggerSessionTools, + operation: string, +): AnalysisInputError => + new AnalysisInputError( + operation, + { + conformsToInputContract: true, + userMessage: `No ${tools.name} debug session has this session_id in this REA server.`, + remediationAction: `Use a session_id that ${tools.start} returned in this server process; ${tools.close} releases a session's ID. Call ${tools.start} to begin a new session.`, + }, + [ + { + path: ["session_id"], + reason: "invalid_value", + message: `No ${tools.name} debug session has this session_id in this REA server.`, + }, + ], + ); + +/** Refuse a command on a retained session that stopped, naming why it stopped. */ +export const stoppedDebuggerSessionError = ( + tools: DebuggerSessionTools, + operation: string, + reason: string, +): AnalysisCapabilityUnavailableError => + new AnalysisCapabilityUnavailableError(tools.providerId, operation, reason, { + userMessage: `The ${tools.name} debug session stopped and accepts no more commands. ${reason}`, + remediationAction: `Call ${tools.close} with this session_id to release the stopped session, then call ${tools.start} to begin a new one. Earlier commands may have changed the target, so confirm their effects before repeating them.`, + }); + +export const RIZIN_DEBUG_SESSION_TOOLS = { + providerId: "rizin", + name: "Rizin", + start: "start_rizin_debug_session", + close: "close_rizin_debug_session", +} as const satisfies DebuggerSessionTools; + +export const GDB_SESSION_TOOLS = { + providerId: "gdb", + name: "GDB", + start: "start_gdb_session", + close: "close_gdb_session", +} as const satisfies DebuggerSessionTools; diff --git a/src/domain/desktop/appTask.ts b/src/domain/desktop/appTask.ts index 64da7ef204..a3d069de9e 100644 --- a/src/domain/desktop/appTask.ts +++ b/src/domain/desktop/appTask.ts @@ -1,6 +1,7 @@ import { isPortableBundleName } from "../portableBundleName.js"; import { z } from "zod"; +import { jsonRecordSchema } from "../jsonRecord.js"; import { jsonValueSchema, type JsonValue } from "../jsonValue.js"; import { err, ok, type Result } from "../result.js"; import { describeValidationFailure } from "../validationFailure.js"; @@ -15,12 +16,10 @@ const line = z .refine((value) => !value.includes("\n"), "a line cannot hold a line break"); const lines = z.array(line).min(1).max(100); const count = z.int().min(0); -const where = z - .record(z.string().min(1), jsonValueSchema) - .refine( - (columns) => Object.keys(columns).length > 0, - "name at least one column", - ); +const where = jsonRecordSchema(jsonValueSchema, z.string().min(1)).refine( + (columns) => Object.keys(columns).length > 0, + "name at least one column", +); const comparison = { equals: count.optional(), at_least: count.optional(), diff --git a/src/domain/desktop/appTaskDraft.ts b/src/domain/desktop/appTaskDraft.ts index 4e83eed0cf..7f105c55d3 100644 --- a/src/domain/desktop/appTaskDraft.ts +++ b/src/domain/desktop/appTaskDraft.ts @@ -1,3 +1,4 @@ +import { escapeJsonPointerToken } from "../jsonPointer.js"; import type { JsonValue } from "../jsonValue.js"; import { isTimestamp, type DesktopTaskCheckInput } from "./appTask.js"; import { @@ -33,7 +34,7 @@ import { } from "./desktopState.js"; import { diffNamedMembers, diffStateViews } from "./stateChanges.js"; import { DRAFT_VIEW_CHARS } from "./valueCuts.js"; -import { escapeToken, resolvePointer } from "./viewPointer.js"; +import { resolvePointer } from "./viewPointer.js"; type Version = DesktopStatePath["versions"][number]; type Suggestion = DesktopTaskCheckInput; @@ -178,8 +179,8 @@ const keyPointers = ( isRecord(view) && isRecord(view.groups) ? view.groups : {}, ).flatMap(([group, keys]) => Object.keys(isRecord(keys) ? keys : {}).map((key): [string, string] => [ - `${group}/${escapeToken(key)}`, - `/groups/${escapeToken(group)}/${escapeToken(key)}`, + `${group}/${escapeJsonPointerToken(key)}`, + `/groups/${escapeJsonPointerToken(group)}/${escapeJsonPointerToken(key)}`, ]), ), ), diff --git a/src/domain/desktop/appTaskDraftAdded.ts b/src/domain/desktop/appTaskDraftAdded.ts index 1fd48ff69f..8539a89afe 100644 --- a/src/domain/desktop/appTaskDraftAdded.ts +++ b/src/domain/desktop/appTaskDraftAdded.ts @@ -1,3 +1,7 @@ +import { + escapeJsonPointerToken, + unescapeJsonPointerToken, +} from "../jsonPointer.js"; import type { JsonValue } from "../jsonValue.js"; import { collapseWhitespace, type DesktopTaskCheckInput } from "./appTask.js"; import { @@ -16,12 +20,7 @@ import { rankAttributes, } from "./attributeRank.js"; import { pairMembers } from "./memberPairing.js"; -import { - elementText, - escapeToken, - isXmlElement, - resolvePointer, -} from "./viewPointer.js"; +import { elementText, isXmlElement, resolvePointer } from "./viewPointer.js"; import { selectorOf } from "./viewSelector.js"; type Suggestion = DesktopTaskCheckInput; @@ -136,9 +135,7 @@ const idsIn = (roots: readonly JsonValue[]): ReadonlySet => { /** The attribute keys the last segment of a pointer selects by. */ const selectorKeys = (pointer: string): ReadonlySet => { - const segment = (pointer.split("/").at(-1) ?? "") - .replaceAll("~1", "/") - .replaceAll("~0", "~"); + const segment = unescapeJsonPointerToken(pointer.split("/").at(-1) ?? ""); return new Set( [...segment.matchAll(/\[@(?[^=\]]+)=/gu)].map( (match) => match.groups?.key ?? "", @@ -214,7 +211,11 @@ export const attributeChecks = ( ); return rankAttributes( eligible.filter(({ key, value }) => - checkFits(context, `${pointer}/attributes/${escapeToken(key)}`, value), + checkFits( + context, + `${pointer}/attributes/${escapeJsonPointerToken(key)}`, + value, + ), ), ) .toSorted( @@ -225,7 +226,7 @@ export const attributeChecks = ( .map(({ key, value }) => valueEqualsCheck( file, - `${pointer}/attributes/${escapeToken(key)}`, + `${pointer}/attributes/${escapeJsonPointerToken(key)}`, value, ), ); @@ -329,7 +330,7 @@ export const fieldChecks = ( element.children.forEach((field, index) => { if (!isXmlElement(field)) return; const key = keys[index] ?? String(index); - const fieldAt = `${pointer}/children/${key.startsWith("[") ? escapeToken(key) : key}`; + const fieldAt = `${pointer}/children/${key.startsWith("[") ? escapeJsonPointerToken(key) : key}`; if ( ID_FIELD.test(field.name) || inSplitDate(ends.final, fieldAt) || @@ -420,11 +421,8 @@ const sharedFields = ( const selectorFields = (pointer: string): ReadonlySet => new Set( ( - selectorOf( - (pointer.split("/").at(-1) ?? "") - .replaceAll("~1", "/") - .replaceAll("~0", "~"), - ) ?? [] + selectorOf(unescapeJsonPointerToken(pointer.split("/").at(-1) ?? "")) ?? + [] ) .filter(({ prefix }) => prefix === ">") .map(({ key, value }) => `${key}\u0000${value}`), @@ -495,7 +493,7 @@ const fieldsOf = ( ]); return element.children.flatMap((field, index): Suggestion[] => { const key = keys[index] ?? ""; - const at = `${pointer}/children/${escapeToken(key)}`; + const at = `${pointer}/children/${escapeJsonPointerToken(key)}`; if ( !isXmlElement(field) || !key.startsWith("[") || @@ -515,7 +513,7 @@ const fieldsOf = ( isXmlElement(record) ? fieldsOf( context, - `${at}/children/${escapeToken(records[place] ?? "")}`, + `${at}/children/${escapeJsonPointerToken(records[place] ?? "")}`, record, { nested: false }, ) diff --git a/src/domain/desktop/appTaskDraftAlternatives.ts b/src/domain/desktop/appTaskDraftAlternatives.ts index 5f194fb6c2..c0eee64aec 100644 --- a/src/domain/desktop/appTaskDraftAlternatives.ts +++ b/src/domain/desktop/appTaskDraftAlternatives.ts @@ -1,3 +1,7 @@ +import { + escapeJsonPointerToken, + unescapeJsonPointerToken, +} from "../jsonPointer.js"; import type { JsonValue } from "../jsonValue.js"; import { isStamp } from "./appTaskDraftNoise.js"; import { @@ -6,15 +10,12 @@ import { isPlaceholder, isSaveNumberedId, } from "./attributeRank.js"; -import { escapeToken, isXmlElement, resolvePointer } from "./viewPointer.js"; +import { isXmlElement, resolvePointer } from "./viewPointer.js"; import { selectorOf, spellCondition } from "./viewSelector.js"; /** Other pointers a value's entry lists. */ const MAX_ALTERNATIVES = 3; -const unescape = (token: string) => - token.replaceAll("~1", "/").replaceAll("~0", "~"); - /** * The value's pointer naming its element another way, for an author to pick * a key without another export: the innermost member a pointer selects by a @@ -32,7 +33,7 @@ export const alternativePointers = ( for (let index = segments.length - 1; index >= 2; index -= 1) { const conditions = segments[index - 1] === "children" - ? selectorOf(unescape(segments[index] ?? "")) + ? selectorOf(unescapeJsonPointerToken(segments[index] ?? "")) : undefined; if (conditions === undefined) continue; // A step its tag alone names, as an XHTML `h1` holding `id="chapter-two"`, gives way outward when nothing else names it. @@ -56,7 +57,7 @@ export const alternativePointers = ( ); const reads = segments[index + 1] === "attributes" && index + 3 === segments.length - ? unescape(segments[index + 2] ?? "") + ? unescapeJsonPointerToken(segments[index + 2] ?? "") : undefined; const alone = (key: string, value: string) => list.filter( @@ -81,7 +82,7 @@ export const alternativePointers = ( ? [ [ ...segments.slice(0, index), - escapeToken( + escapeJsonPointerToken( `[name=${element.name}]${spellCondition({ prefix: "@", key, value })}`, ), ...segments.slice(index + 1), diff --git a/src/domain/desktop/appTaskDraftAttributeText.ts b/src/domain/desktop/appTaskDraftAttributeText.ts index bc98b46f8f..35fe1a39af 100644 --- a/src/domain/desktop/appTaskDraftAttributeText.ts +++ b/src/domain/desktop/appTaskDraftAttributeText.ts @@ -1,3 +1,4 @@ +import { escapeJsonPointerToken } from "../jsonPointer.js"; import type { JsonValue } from "../jsonValue.js"; import type { DesktopTaskCheckInput } from "./appTask.js"; import { @@ -7,12 +8,7 @@ import { } from "./appTaskDraftNoise.js"; import { isIdentifyingAttribute, isNameLikeKey } from "./attributeRank.js"; import type { DesktopStateDifference } from "./stateChanges.js"; -import { - elementText, - escapeToken, - isXmlElement, - resolvePointer, -} from "./viewPointer.js"; +import { elementText, isXmlElement, resolvePointer } from "./viewPointer.js"; /** How far a text check climbs from a run to the element an attribute names. */ const MAX_CLIMB = 3; @@ -185,7 +181,7 @@ export const textAttributes = ( ...change.removed.flatMap((pointer) => own(ends.baseline, pointer)), ].filter( ({ owner, key }) => - noise(`${owner}/attributes/${escapeToken(key)}`) === undefined, + noise(`${owner}/attributes/${escapeJsonPointerToken(key)}`) === undefined, ); }; diff --git a/src/domain/desktop/appTaskDraftGuards.ts b/src/domain/desktop/appTaskDraftGuards.ts index 6df054c302..8f8d65791b 100644 --- a/src/domain/desktop/appTaskDraftGuards.ts +++ b/src/domain/desktop/appTaskDraftGuards.ts @@ -1,3 +1,7 @@ +import { + escapeJsonPointerToken, + unescapeJsonPointerToken, +} from "../jsonPointer.js"; import type { JsonValue } from "../jsonValue.js"; import { collapseWhitespace, type DesktopTaskCheckInput } from "./appTask.js"; import { sameJson } from "./appTaskChecks.js"; @@ -18,12 +22,7 @@ import { type ListStep, } from "./appTaskDraftSteps.js"; import { numberedInOrder } from "./positionalKeys.js"; -import { - elementText, - escapeToken, - isXmlElement, - resolvePointer, -} from "./viewPointer.js"; +import { elementText, isXmlElement, resolvePointer } from "./viewPointer.js"; import { selectorOf, spellCondition, type Condition } from "./viewSelector.js"; import { childConditions } from "./selectorCandidates.js"; @@ -142,7 +141,7 @@ const sharing = ( ) => { const own = attributesOf(member); const selected = new Set( - (selectorOf(key.replaceAll("~1", "/").replaceAll("~0", "~")) ?? []).map( + (selectorOf(unescapeJsonPointerToken(key)) ?? []).map( ({ key: name }) => name, ), ); @@ -209,7 +208,7 @@ const siblingsOf = ( pairing.older.keys[partner] !== key || !isXmlElement(element) ? [] - : [{ member: escapeToken(key), element }]; + : [{ member: escapeJsonPointerToken(key), element }]; }), all, }; @@ -224,12 +223,12 @@ const attributesOf = ({ attributes }: Record) => /** The conditions a sibling's segment selects it by, `@` attributes and `>` children's text. */ const conditionsOf = (member: string) => - selectorOf(member.replaceAll("~1", "/").replaceAll("~0", "~")) ?? []; + selectorOf(unescapeJsonPointerToken(member)) ?? []; /** The attribute a path into a member reads, as `name` for `attributes/name`. */ const attributeRead = (rest: readonly string[]) => rest.length === 2 && rest[0] === "attributes" - ? (rest[1] ?? "").replaceAll("~1", "/").replaceAll("~0", "~") + ? unescapeJsonPointerToken(rest[1] ?? "") : undefined; /** What a guard at a path into a sibling reads at the seed: an attribute's value, or an element's whole text, trimmed. */ @@ -311,7 +310,9 @@ const rekeyed = ( resolvePointer(view, `${step.list}/${member}`, "xml"); const end = at(sibling.member, context.ends.final); for (const condition of candidates) { - const member = escapeToken(`[name=${tag}]${spellCondition(condition)}`); + const member = escapeJsonPointerToken( + `[name=${tag}]${spellCondition(condition)}`, + ); if ( at(member, context.ends.baseline) === element && at(member, context.ends.final) === end @@ -373,12 +374,13 @@ const guardField = ( !isIdentifyingAttribute(key) && !isStamp(value) && !numberedInOrder(all, String(sibling.element.name), key) && - varies(["attributes", escapeToken(key)]) + varies(["attributes", escapeJsonPointerToken(key)]) ? [{ key, value }] : [], ), ); - if (best !== undefined) return ["attributes", escapeToken(best.key)]; + if (best !== undefined) + return ["attributes", escapeJsonPointerToken(best.key)]; return own ? rest : undefined; }; diff --git a/src/domain/desktop/appTaskDraftJson.ts b/src/domain/desktop/appTaskDraftJson.ts index 1a45d92410..d032d6bc89 100644 --- a/src/domain/desktop/appTaskDraftJson.ts +++ b/src/domain/desktop/appTaskDraftJson.ts @@ -1,3 +1,8 @@ +import { + escapeJsonPointerToken, + jsonPointerTokens, + unescapeJsonPointerToken, +} from "../jsonPointer.js"; import type { JsonValue } from "../jsonValue.js"; import type { DesktopTaskCheckInput } from "./appTask.js"; import type { FileNote } from "./appTaskDraftFileNotes.js"; @@ -17,7 +22,6 @@ import { import { pairMembers } from "./memberPairing.js"; import type { DesktopStateDifference } from "./stateChanges.js"; import { - escapeToken, locatePointer as locateView, resolvePointer as resolveView, } from "./viewPointer.js"; @@ -46,9 +50,9 @@ export const generatedRecordId = ( return (pointer: string): boolean => { const segments = pointer.split("/"); if (segments.length < 3) return false; - const field = segments.at(-1)?.replaceAll("~1", "/").replaceAll("~0", "~"); - if (field === undefined || !isRecordIdField(field)) return false; - const member = segments.at(-2)?.replaceAll("~1", "/").replaceAll("~0", "~"); + const field = unescapeJsonPointerToken(segments.at(-1)!); + if (!isRecordIdField(field)) return false; + const member = unescapeJsonPointerToken(segments.at(-2)!); const list = segments.slice(0, -2).join("/"); if (!lists.has(list)) { const after = resolveView(ends.final, list, format); @@ -104,14 +108,14 @@ export const generatedIdsInAdditions = ( const { keys } = pairMembers(value, [], undefined, "json").newer; value.forEach((child, index) => pending.push({ - pointer: `${pointer}/${escapeToken(keys[index] ?? String(index))}`, + pointer: `${pointer}/${escapeJsonPointerToken(keys[index] ?? String(index))}`, value: child, }), ); } else if (value !== null && typeof value === "object") { for (const [key, child] of Object.entries(value)) pending.push({ - pointer: `${pointer}/${escapeToken(key)}`, + pointer: `${pointer}/${escapeJsonPointerToken(key)}`, value: child, }); } @@ -133,9 +137,7 @@ const MAX_LISTED = 5; */ const ownNameRecord = (pointer: string): string | undefined => { const spelled = pointer.split("/"); - const segments = spelled.map((segment) => - segment.replaceAll("~1", "/").replaceAll("~0", "~"), - ); + const segments = spelled.map(unescapeJsonPointerToken); const at = segments.findIndex((segment, index) => { const conditions = selectorOf(segment) ?? []; const rest = segments.slice(index + 1).join("/"); @@ -396,11 +398,7 @@ export const isLikeDefault = ( /** Whether a member's selector reads a field, as `[format=BOOK][>data~1Title=SPQR]` reads `/data/Title`: a guard of it proves only that such a member exists. */ const selectsBy = (member: string, field: string) => { - const path = field - .slice(1) - .split("/") - .map((segment) => segment.replaceAll("~1", "/").replaceAll("~0", "~")) - .join("/"); + const path = jsonPointerTokens(field).join("/"); return (selectorOf(member) ?? []).some( ({ prefix, key }) => prefix !== "@" && key === path, ); @@ -493,7 +491,7 @@ export const recordGuards = ( pairing.older.keys[partner] !== key || selectsBy(key, record.field) ? [] - : [`${record.list}/${escapeToken(key)}`]; + : [`${record.list}/${escapeJsonPointerToken(key)}`]; }) .slice(0, MAX_SIBLINGS_READ); let found = 0; diff --git a/src/domain/desktop/appTaskDraftMirrors.ts b/src/domain/desktop/appTaskDraftMirrors.ts index 035f48c58f..e95481009d 100644 --- a/src/domain/desktop/appTaskDraftMirrors.ts +++ b/src/domain/desktop/appTaskDraftMirrors.ts @@ -1,3 +1,4 @@ +import { unescapeJsonPointerToken } from "../jsonPointer.js"; import type { JsonValue } from "../jsonValue.js"; import { isNameLikeKey } from "./attributeRank.js"; import type { DesktopStateDifference } from "./stateChanges.js"; @@ -19,11 +20,9 @@ const documentAt = (view: JsonValue | null, pointer: string) => { /** The value of the name-like attribute a pointer's last step names its element by, as `label` for `[name=elementInformation][@name=label]`. */ const fieldKeyOf = (element: string) => - selectorOf( - (element.split("/").at(-1) ?? "") - .replaceAll("~1", "/") - .replaceAll("~0", "~"), - )?.find(({ prefix, key }) => prefix === "@" && isNameLikeKey(key))?.value; + selectorOf(unescapeJsonPointerToken(element.split("/").at(-1) ?? ""))?.find( + ({ prefix, key }) => prefix === "@" && isNameLikeKey(key), + )?.value; /** * Text an app shows of another field it keeps, which changed alike: a text diff --git a/src/domain/desktop/appTaskDraftNamed.ts b/src/domain/desktop/appTaskDraftNamed.ts index a479866b95..fac9b92826 100644 --- a/src/domain/desktop/appTaskDraftNamed.ts +++ b/src/domain/desktop/appTaskDraftNamed.ts @@ -1,3 +1,4 @@ +import { escapeJsonPointerToken } from "../jsonPointer.js"; import type { JsonValue } from "../jsonValue.js"; import { collapseWhitespace, type DesktopTaskCheckInput } from "./appTask.js"; import { @@ -41,12 +42,7 @@ import { type DesktopStateDifference, } from "./stateChanges.js"; import { pairMembers, pairedAtSeed } from "./memberPairing.js"; -import { - escapeToken, - isXmlElement, - locatePointer, - resolvePointer, -} from "./viewPointer.js"; +import { isXmlElement, locatePointer, resolvePointer } from "./viewPointer.js"; import { ARCHIVE, referenceRoots, @@ -388,13 +384,13 @@ const addedChecks = ( value.forEach((item, index) => addedChecks( collected, - `${pointer}/${escapeToken(keys[index] ?? String(index))}`, + `${pointer}/${escapeJsonPointerToken(keys[index] ?? String(index))}`, item, ), ); } else if (isRecord(value) && Object.keys(value).length > 0) for (const [key, item] of Object.entries(value)) - addedChecks(collected, `${pointer}/${escapeToken(key)}`, item); + addedChecks(collected, `${pointer}/${escapeJsonPointerToken(key)}`, item); // A scalar, or an empty array or object, is checked as it is. else equals(collected, pointer, value); }; @@ -452,7 +448,7 @@ const renamedFrom = (collected: Collected, pointer: string) => { old.includes("][") ) return; - const at = `${list}/${escapeToken(`[name=${before.name}][@${key}=${old}]`)}`; + const at = `${list}/${escapeJsonPointerToken(`[name=${before.name}][@${key}=${old}]`)}`; const seed = locatePointer(ends.baseline, at, "xml"); if ( seed.kind === "value" && diff --git a/src/domain/desktop/appTaskDraftNested.ts b/src/domain/desktop/appTaskDraftNested.ts index a1fe5a542b..308e1cca6e 100644 --- a/src/domain/desktop/appTaskDraftNested.ts +++ b/src/domain/desktop/appTaskDraftNested.ts @@ -1,3 +1,4 @@ +import { escapeJsonPointerToken } from "../jsonPointer.js"; import type { JsonValue } from "../jsonValue.js"; import type { DesktopTaskCheckInput } from "./appTask.js"; import { @@ -13,7 +14,7 @@ import { isViewStateName, rankAttributes, } from "./attributeRank.js"; -import { escapeToken, isXmlElement, resolvePointer } from "./viewPointer.js"; +import { isXmlElement, resolvePointer } from "./viewPointer.js"; import { spellCondition } from "./viewSelector.js"; import { checkFits, type AddedContext } from "./appTaskDraftAdded.js"; @@ -71,7 +72,12 @@ export const recordListsOf = (element: JsonValue): RecordList[] => { return records.length > 0 && tags.get(child.name) === 1 && !/[=[\]]/u.test(child.name) - ? [{ steps: ["children", escapeToken(`[name=${child.name}]`)], records }] + ? [ + { + steps: ["children", escapeJsonPointerToken(`[name=${child.name}]`)], + records, + }, + ] : []; }); }; @@ -138,7 +144,7 @@ const recordKey = ( ? undefined : { key: best.key, - segment: escapeToken( + segment: escapeJsonPointerToken( `[name=${record.name}]${spellCondition({ prefix: "@", ...best })}`, ), }; @@ -225,12 +231,16 @@ export const nestedRecordChecks = ( const value = record.attributes[name]; return ( typeof value === "string" && - checkFits(context, `${at}/attributes/${escapeToken(name)}`, value) + checkFits( + context, + `${at}/attributes/${escapeJsonPointerToken(name)}`, + value, + ) ); }) .slice(0, MAX_PER_RECORD) .flatMap((name) => { - const field = `${at}/attributes/${escapeToken(name)}`; + const field = `${at}/attributes/${escapeJsonPointerToken(name)}`; const value = record.attributes[name]; return context.named(field) && typeof value === "string" ? [valueEqualsCheck(context.file, field, value)] diff --git a/src/domain/desktop/appTaskDraftNoise.ts b/src/domain/desktop/appTaskDraftNoise.ts index 4a8f134b65..c5e83a8a9c 100644 --- a/src/domain/desktop/appTaskDraftNoise.ts +++ b/src/domain/desktop/appTaskDraftNoise.ts @@ -1,3 +1,4 @@ +import { unescapeJsonPointerToken } from "../jsonPointer.js"; import type { JsonValue } from "../jsonValue.js"; import { isTimestamp } from "./appTask.js"; import { @@ -124,7 +125,7 @@ const COUNTER = /count|cycles?|revision/iu; export const attributeKeyOf = (pointer: string): string | undefined => { const segments = pointer.split("/"); return segments.at(-2) === "attributes" - ? segments.at(-1)?.replaceAll("~1", "/").replaceAll("~0", "~") + ? unescapeJsonPointerToken(segments.at(-1)!) : undefined; }; @@ -506,7 +507,7 @@ const COORDINATES = /^-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?(?:[,;] ?-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?)*$/u; /** Whether a value is a coordinate or a list of them, which an app may derive rather than its user enter. */ -export const isCoordinates = (value: JsonValue | undefined): boolean => +export const isCoordinates = (value: JsonValue | undefined): value is string => typeof value === "string" && COORDINATES.test(value.trim()); /** How far above an attribute's element the element whose text changed may sit for the attribute to be its layout. */ @@ -568,7 +569,7 @@ const derivedLayout = ( // A lone number is layout only where a name on its way says so; a list of them always is. return ( at >= 0 && - (/[,;]/u.test(String(value)) || + (/[,;]/u.test(value) || namesLayout(ends.final, pointer, [owner, ...above.slice(0, at)])) ); }), @@ -622,7 +623,7 @@ const drawingLayout = ( attributeKeyOf(pointer) !== undefined && isCoordinates(value) && drawn(pointer) && - (/[,;]/u.test(String(value)) || + (/[,;]/u.test(value) || namesLayout(viewOf(pointer), pointer, [ownerOfAttribute(pointer)])) ); }; diff --git a/src/domain/desktop/appTaskDraftSteps.ts b/src/domain/desktop/appTaskDraftSteps.ts index 80cd69a3b9..ef175418c2 100644 --- a/src/domain/desktop/appTaskDraftSteps.ts +++ b/src/domain/desktop/appTaskDraftSteps.ts @@ -1,3 +1,4 @@ +import { unescapeJsonPointerToken } from "../jsonPointer.js"; import type { JsonValue } from "../jsonValue.js"; import { isNameLikeKey } from "./attributeRank.js"; import { isXmlElement, resolvePointer } from "./viewPointer.js"; @@ -32,7 +33,7 @@ export const listSteps = (pointer: string, positional = false): ListStep[] => { /** Whether a step's member is named as a field is, by a name-like attribute alone, as QElectroTech's `elementInformation[@name=label]`. */ const namedAsField = ({ member }: ListStep) => { const [only, ...more] = ( - selectorOf(member.replaceAll("~1", "/").replaceAll("~0", "~")) ?? [] + selectorOf(unescapeJsonPointerToken(member)) ?? [] ).filter(({ prefix }) => prefix !== ""); return ( only !== undefined && diff --git a/src/domain/desktop/appTaskDraftText.ts b/src/domain/desktop/appTaskDraftText.ts index bc800df724..19d250d5bf 100644 --- a/src/domain/desktop/appTaskDraftText.ts +++ b/src/domain/desktop/appTaskDraftText.ts @@ -1,3 +1,4 @@ +import { escapeJsonPointerToken } from "../jsonPointer.js"; import type { JsonValue } from "../jsonValue.js"; import { collapseWhitespace, type DesktopTaskCheckInput } from "./appTask.js"; import { @@ -161,7 +162,7 @@ const byOwnAttribute = (element: string, ends: Ends): Candidate | undefined => { ) continue; const selector = `[name=${after.name}][@${key}=${value}]`; - const pointer = `${parent}/children/${selector.replaceAll("~", "~0").replaceAll("/", "~1")}`; + const pointer = `${parent}/children/${escapeJsonPointerToken(selector)}`; if ( resolvePointer(ends.baseline, pointer, "xml") === before && resolvePointer(ends.final, pointer, "xml") === after diff --git a/src/domain/desktop/appTaskDraftValues.ts b/src/domain/desktop/appTaskDraftValues.ts index f7019440e3..e761b0f873 100644 --- a/src/domain/desktop/appTaskDraftValues.ts +++ b/src/domain/desktop/appTaskDraftValues.ts @@ -1,3 +1,4 @@ +import { escapeJsonPointerToken } from "../jsonPointer.js"; import { z } from "zod"; import { jsonValueSchema, type JsonValue } from "../jsonValue.js"; @@ -14,12 +15,7 @@ import { recordListsOf } from "./appTaskDraftNested.js"; import { holdsFields, isRecordAt } from "./appTaskDraftText.js"; import { attributesOf, compactValue, previewWriter } from "./compactValue.js"; import { pairMembers, pairedAtSeed } from "./memberPairing.js"; -import { - escapeToken, - isXmlElement, - locatePointer, - resolvePointer, -} from "./viewPointer.js"; +import { isXmlElement, locatePointer, resolvePointer } from "./viewPointer.js"; /** Why a value's pointer cannot go into a check as it is, or why the value is noise. */ export const VALUE_NOTES = ["by_position", ...NOISE_REASONS] as const; @@ -476,7 +472,12 @@ const fieldsOf = (view: JsonValue | null, pointer: string): string[] => { return (isXmlElement(element) ? element.children : []).flatMap( (child, index) => isXmlElement(child) - ? [{ child, key: escapeToken(keys[index] ?? String(index)) }] + ? [ + { + child, + key: escapeJsonPointerToken(keys[index] ?? String(index)), + }, + ] : [], ); }; diff --git a/src/domain/desktop/appTaskOwnKeys.test.ts b/src/domain/desktop/appTaskOwnKeys.test.ts new file mode 100644 index 0000000000..a580256f7b --- /dev/null +++ b/src/domain/desktop/appTaskOwnKeys.test.ts @@ -0,0 +1,66 @@ +import { expect, it } from "vitest"; +import { parseDesktopTask } from "./appTask.js"; +import { evaluateTaskChecks } from "./appTaskChecks.js"; + +it.each(["__proto__", "constructor", "ordinary"])( + "retains the own row condition %s while grading every selected column", + (column) => { + const where = Object.fromEntries([ + ["id", 1], + [column, "wanted"], + ]); + const common = { file: "db.sqlite", table: "rows", where }; + const parsed = parseDesktopTask({ + name: "find-row", + prompt: "Find the row matching every selected column.", + checks: [ + { kind: "row_present", ...common }, + { kind: "row_absent", ...common }, + { kind: "row_count", ...common, equals: 0 }, + ], + }); + if (!parsed.ok) throw new Error(parsed.error); + const first = parsed.value.checks[0]; + if (first?.kind !== "row_present") throw new Error("Expected a row check"); + expect(Object.keys(first.where)).toEqual(["id", column]); + const views = new Map([ + [ + "db.sqlite", + { + format: "sqlite", + view: { + tables: { + rows: { + columns: ["id", column], + primary_key: ["id"], + rows: [[1, "other"]], + truncated: false, + }, + }, + tables_truncated: false, + }, + }, + ], + ]); + expect( + evaluateTaskChecks(parsed.value.checks, views).map( + ({ passed }) => passed, + ), + ).toEqual([false, true, true]); + expect(Object.keys(where)).toEqual(["id", column]); + }, +); + +it("accepts an own __proto__ condition as a nonempty row predicate", () => { + const where = JSON.parse('{"__proto__":"wanted"}'); + const parsed = parseDesktopTask({ + name: "find-row", + prompt: "Find the selected row.", + checks: [{ kind: "row_present", file: "db.sqlite", table: "rows", where }], + }); + if (!parsed.ok) throw new Error(parsed.error); + const check = parsed.value.checks[0]; + if (check?.kind !== "row_present") throw new Error("Expected a row check"); + expect(Object.keys(check.where)).toEqual(["__proto__"]); + expect(check.where["__proto__"]).toBe("wanted"); +}); diff --git a/src/domain/desktop/buildWitness.test.ts b/src/domain/desktop/buildWitness.test.ts index 6fa138ce3d..e1b32477e5 100644 --- a/src/domain/desktop/buildWitness.test.ts +++ b/src/domain/desktop/buildWitness.test.ts @@ -8,6 +8,8 @@ import { type WitnessStageRecord, } from "./buildWitness.js"; import type { ComputerAction } from "./computerActions.js"; +import { parseDesktopTask } from "./appTask.js"; +import { evaluateTaskChecks } from "./appTaskChecks.js"; type Logged = { readonly action: ComputerAction; @@ -351,6 +353,60 @@ describe("build witness over a long session", () => { }); describe("build witness state views", () => { + it.each(["__proto__", "constructor", "ordinary"])( + "grades the exact own state path %s instead of treating it as absent", + (path) => { + const view = JSON.parse('{"count":7,"__proto__":"retained value"}'); + const parts = [ + { + kind: "data", + data: { + state: Object.fromEntries([[path, { format: "json", view }]]), + }, + }, + ]; + const task = parseDesktopTask({ + name: "check-state", + prompt: "Check the selected file and its value.", + checks: [ + { kind: "file_present", file: path }, + { kind: "file_absent", file: path }, + { kind: "value_equals", file: path, pointer: "/count", value: 7 }, + ], + }); + const parsed = appStateViews(parts); + if (!task.ok) throw new Error(task.error); + if (!parsed.ok) throw new Error(parsed.error); + expect([...parsed.value.keys()]).toEqual([path]); + expect(parsed.value.get(path)).toEqual({ + format: "json", + view, + error: null, + }); + expect( + evaluateTaskChecks(task.value.checks, parsed.value).map( + ({ passed }) => passed, + ), + ).toEqual([true, false, true]); + expect(Object.hasOwn(parts[0]!.data.state, path)).toBe(true); + expect(Object.hasOwn(view, "__proto__")).toBe(true); + }, + ); + + it.each( + ["__proto__", "ordinary"].flatMap((path) => [ + { path, entry: { format: 7, view: null } }, + { path, entry: { format: "json" } }, + { path, entry: { format: null, view: null, error: 7 } }, + ]), + )("rejects a malformed view under $path: $entry", ({ path, entry }) => { + expect( + appStateViews([ + { kind: "data", data: { state: Object.fromEntries([[path, entry]]) } }, + ]), + ).toMatchObject({ ok: false }); + }); + it("reads the app's data/get state into the views the checks read, keeping each file's error", () => { expect( appStateViews([ diff --git a/src/domain/desktop/buildWitness.ts b/src/domain/desktop/buildWitness.ts index 9efbc5ac90..f729ec4db6 100644 --- a/src/domain/desktop/buildWitness.ts +++ b/src/domain/desktop/buildWitness.ts @@ -1,6 +1,7 @@ import { z } from "zod"; import { jsonValueSchema } from "../jsonValue.js"; +import { jsonRecordSchema } from "../jsonRecord.js"; import { err, ok, type Result } from "../result.js"; import type { TaskCheckOutcome, TaskFileView } from "./appTaskChecks.js"; import type { ComputerAction } from "./computerActions.js"; @@ -221,14 +222,17 @@ export const admitWitnessStage = ( return ok(log.total); }; -const fileViewSchema = z.looseObject({ +const fileViewSchema = z.object({ format: z.string().nullable(), view: jsonValueSchema.nullable(), - error: z.string().nullish(), + error: z + .string() + .nullish() + .transform((error) => error ?? null), }); const statePartSchema = z.looseObject({ kind: z.literal("data"), - data: z.looseObject({ state: z.record(z.string(), fileViewSchema) }), + data: z.looseObject({ state: jsonRecordSchema(fileViewSchema) }), }); /** @@ -241,18 +245,7 @@ export const appStateViews = ( for (const part of parts) { const parsed = statePartSchema.safeParse(part); if (parsed.success) - return ok( - new Map( - Object.entries(parsed.data.data.state).map(([path, entry]) => [ - path, - { - format: entry.format, - view: entry.view, - error: entry.error ?? null, - }, - ]), - ), - ); + return ok(new Map(Object.entries(parsed.data.data.state))); } return err( "The app's data/get returned no state views; witness a bundle exported by REA, whose app serves them.", diff --git a/src/domain/desktop/memberPairing.ts b/src/domain/desktop/memberPairing.ts index 718e5f226d..3535975e5f 100644 --- a/src/domain/desktop/memberPairing.ts +++ b/src/domain/desktop/memberPairing.ts @@ -1,3 +1,4 @@ +import { unescapeJsonPointerToken } from "../jsonPointer.js"; import type { JsonValue } from "../jsonValue.js"; import { isTimestamp } from "./appTask.js"; import { @@ -362,7 +363,7 @@ export interface ListPlace { /** A list's place, read from its pointer, as `/root/children/[name=gnm:Sheets]/children` sits in `gnm:Sheets`. */ export const holdsTagOnly = (list: string): ListPlace => { const owner = selectorOf( - (list.split("/").at(-2) ?? "").replaceAll("~1", "/").replaceAll("~0", "~"), + unescapeJsonPointerToken(list.split("/").at(-2) ?? ""), )?.find(({ prefix, key }) => prefix === "" && key === "name")?.value; return { root: /(?:^|\/)root\/children$/u.test(list), diff --git a/src/domain/desktop/stateChanges.ts b/src/domain/desktop/stateChanges.ts index ff863ef91a..446aee3670 100644 --- a/src/domain/desktop/stateChanges.ts +++ b/src/domain/desktop/stateChanges.ts @@ -1,3 +1,4 @@ +import { escapeJsonPointerToken } from "../jsonPointer.js"; import type { JsonValue } from "../jsonValue.js"; import type { DesktopStateFormat } from "./stateFormats.js"; import { holdsTagOnly, pairMembers } from "./memberPairing.js"; @@ -24,9 +25,6 @@ const isRecord = ( ): value is Record => typeof value === "object" && value !== null && !Array.isArray(value); -const escapePointer = (segment: string) => - segment.replaceAll("~", "~0").replaceAll("/", "~1"); - /** Longest JSON pointer listed; a longer one is only counted, so deep paths never fill the lists. */ const MAX_LISTED_POINTER = 4096; @@ -137,7 +135,7 @@ const walk = ( } else if (into.changed !== undefined && ownEntry(from) !== ownEntry(other)) note(into.changed, path); const visit = (key: string, item: JsonValue, counterpart?: JsonValue) => { - const segment = escapePointer(key); + const segment = escapeJsonPointerToken(key); path.segments.push(segment); path.length += segment.length + 1; walk(item, counterpart, path, into); @@ -283,7 +281,10 @@ const keyEntries = (view: JsonValue): Map => { for (const [group, keys] of Object.entries(groups)) if (isRecord(keys)) for (const [key, value] of Object.entries(keys)) - entries.set(`${group}/${escapePointer(key)}`, JSON.stringify(value)); + entries.set( + `${group}/${escapeJsonPointerToken(key)}`, + JSON.stringify(value), + ); return entries; }; diff --git a/src/domain/desktop/stateFormats.ts b/src/domain/desktop/stateFormats.ts index f32ff051eb..19ff4af6da 100644 --- a/src/domain/desktop/stateFormats.ts +++ b/src/domain/desktop/stateFormats.ts @@ -392,7 +392,7 @@ const jsonView = ( rounded = true; return value; }); - if (!parsed.ok) return err(parsed.error); + if (!parsed.ok) return parsed; if (rounded) return err( "a number in it cannot be read exactly, such as an integer above 2^53", diff --git a/src/domain/desktop/viewPointer.test.ts b/src/domain/desktop/viewPointer.test.ts index f5d5e0092d..d48a2194d3 100644 --- a/src/domain/desktop/viewPointer.test.ts +++ b/src/domain/desktop/viewPointer.test.ts @@ -26,6 +26,12 @@ const VIEW: JsonValue = { }; describe("locatePointer", () => { + it("decodes an escaped tilde once rather than treating the result as another escape", () => { + expect( + locatePointer({ "~1": "literal", "/": "slash" }, "/~01", "json"), + ).toMatchObject({ kind: "value", value: "literal" }); + }); + it("selects the one member a selector names, by field, by attribute, or both", () => { expect( locatePointer( diff --git a/src/domain/desktop/viewPointer.ts b/src/domain/desktop/viewPointer.ts index ca090d26e2..e3b38e9890 100644 --- a/src/domain/desktop/viewPointer.ts +++ b/src/domain/desktop/viewPointer.ts @@ -1,3 +1,4 @@ +import { unescapeJsonPointerToken } from "../jsonPointer.js"; import type { JsonValue } from "../jsonValue.js"; import { collapseWhitespace, isTimestamp } from "./appTask.js"; import { @@ -32,10 +33,6 @@ type Stepped = const INDEX = /^(?:0|[1-9][0-9]*)$/u; -/** A pointer segment for a key: `~` and `/` escaped as RFC 6901 writes them. */ -export const escapeToken = (token: string) => - token.replaceAll("~", "~0").replaceAll("/", "~1"); - const isRecord = ( value: JsonValue | null | undefined, ): value is Record => @@ -226,7 +223,7 @@ export const locatePointer = ( const segments = pointer.slice(1).split("/"); const steps = { searched: [] as string[], byPosition: [] as string[] }; for (const [position, segment] of segments.entries()) { - const token = segment.replaceAll("~1", "/").replaceAll("~0", "~"); + const token = unescapeJsonPointerToken(segment); if (Array.isArray(current)) { const array = segments .slice(0, position) diff --git a/src/domain/evidenceBundle.test.ts b/src/domain/evidenceBundle.test.ts index 2b809fc64b..a46289a9bf 100644 --- a/src/domain/evidenceBundle.test.ts +++ b/src/domain/evidenceBundle.test.ts @@ -5,7 +5,6 @@ import { ANALYSIS_SNAPSHOT_TARGET } from "./analysisSnapshot.fixture.js"; import { createEvidenceBundle, createImmutableEvidenceBundle, - NonCanonicalEvidenceBundleError, evidenceBundleForTarget, parseEvidenceBundle, safeParseEvidenceBundle, @@ -67,7 +66,10 @@ describe("evidence bundle failure classification", () => { expect(parsed.ok).toBe(false); if (parsed.ok) return; expect(parsed.error.kind).toBe("invalid-bundle"); - expect(parsed.error.cause).toBeInstanceOf(NonCanonicalEvidenceBundleError); + expect(parsed.error.cause).toMatchObject({ + name: "NonCanonicalEvidenceBundleError", + message: "Evidence bundle manifests are not canonical", + }); }); it("treats only authenticated standalone Evidence as the wrong input kind", () => { diff --git a/src/domain/evidenceBundle.ts b/src/domain/evidenceBundle.ts index 951449a45e..b423212e80 100644 --- a/src/domain/evidenceBundle.ts +++ b/src/domain/evidenceBundle.ts @@ -62,7 +62,7 @@ export const isImmutableEvidenceBundle = ( typeof input === "object" && input !== null && immutableBundles.has(input); /** Caller input has valid shape but is not in authenticated canonical form. */ -export class NonCanonicalEvidenceBundleError extends TypeError { +class NonCanonicalEvidenceBundleError extends TypeError { constructor() { super("Evidence bundle manifests are not canonical"); this.name = "NonCanonicalEvidenceBundleError"; @@ -70,7 +70,7 @@ export class NonCanonicalEvidenceBundleError extends TypeError { } /** Caller input lists the same Evidence record more than once. */ -export class DuplicateEvidenceRecordError extends TypeError { +class DuplicateEvidenceRecordError extends TypeError { constructor() { super("Evidence bundle contains duplicate record IDs"); this.name = "DuplicateEvidenceRecordError"; diff --git a/src/domain/evidenceCompletionLedger.ts b/src/domain/evidenceCompletionLedger.ts index 49ef503b43..d2f9ca6397 100644 --- a/src/domain/evidenceCompletionLedger.ts +++ b/src/domain/evidenceCompletionLedger.ts @@ -2,7 +2,7 @@ import { z } from "zod"; import { digestCanonicalValue } from "./canonicalDigest.js"; import { IDENTIFIER_PATTERN } from "./stringPatterns.js"; -import { prefixedDigestSchema } from "./../domain/digests.js"; +import { prefixedDigestSchema } from "./digests.js"; import { compareAsciiStrings, isSortedUniqueAscii, diff --git a/src/domain/evidenceRecordSummary.ts b/src/domain/evidenceRecordSummary.ts index 373d028fdd..caa2438448 100644 --- a/src/domain/evidenceRecordSummary.ts +++ b/src/domain/evidenceRecordSummary.ts @@ -202,7 +202,7 @@ export const summarizeEvidenceBundle = ( export const EVIDENCE_RECORD_SELECTION_LIMIT = 8; /** Labels every record selection as historical and as distinct from an importable bundle. */ -export const EVIDENCE_RECORD_SELECTION_LIMITATIONS = [ +const EVIDENCE_RECORD_SELECTION_LIMITATIONS = [ "Selected records are retained historical observations; they do not establish current live provider or database state.", "A record selection is not an Evidence bundle: it omits manifests and residual unknowns and cannot be imported. Use export_evidence_bundle for a portable bundle.", ] as const; @@ -280,7 +280,7 @@ export const evidenceLinkStatusFilterSchema = z.enum([ export const EVIDENCE_LINK_INSPECTION_LIMIT = 64; /** Bounds what link membership establishes, in every inspection. */ -export const EVIDENCE_LINK_INSPECTION_LIMITATIONS = [ +const EVIDENCE_LINK_INSPECTION_LIMITATIONS = [ "Link status is membership in this session or bundle: missing means absent here, not nonexistent, and neither status describes analysis coverage or a record's meaning.", ] as const; diff --git a/src/domain/explicitSensitiveFailure.test.ts b/src/domain/explicitSensitiveFailure.test.ts index 82dc095870..eb923f5102 100644 --- a/src/domain/explicitSensitiveFailure.test.ts +++ b/src/domain/explicitSensitiveFailure.test.ts @@ -292,6 +292,25 @@ it("preserves a retryable capability failure through explicit-value redaction", expect(JSON.stringify(projectAnalysisError(result))).not.toContain("private"); }); +it.each([ + ["file-size", false], + ["memory", undefined], +] as const)( + "preserves %s resource refusal retryability through redaction", + (resource, retryable) => { + const source = new AnalysisResourceConstraintError( + "inspect_pe_resources", + resource, + "Selected artifact exceeds 64 bytes", + { max_file_bytes: 64 }, + retryable === undefined ? {} : { retryable }, + ); + expect(redactExplicitFailure(source, ["secret"])).toMatchObject({ + retryable, + }); + }, +); + it("coarsens format issue pointers without creating replacement coordinates", () => { const error = new AnalysisInputError( "inspect_web_network_capture", diff --git a/src/domain/explicitSensitiveFailure.ts b/src/domain/explicitSensitiveFailure.ts index 878ff431e6..1a1c05017b 100644 --- a/src/domain/explicitSensitiveFailure.ts +++ b/src/domain/explicitSensitiveFailure.ts @@ -1,3 +1,4 @@ +import { unescapeJsonPointerToken } from "./jsonPointer.js"; import { BrowserObservationError } from "./browserObservationError.js"; import { EvidenceFileError } from "./evidenceErrors.js"; import type { AnalysisError } from "./analysisErrorBase.js"; @@ -54,7 +55,7 @@ const redactSensitivePointer = ( ): string => { let parent = ""; for (const segment of pointer.split("/").slice(1)) { - const decoded = segment.replaceAll("~1", "/").replaceAll("~0", "~"); + const decoded = unescapeJsonPointerToken(segment); const child = `${parent}/${segment}`; if (text(decoded) !== decoded || text(child) !== child) return parent; parent = child; diff --git a/src/domain/firmware/hexRecordSyntax.ts b/src/domain/firmware/hexRecordSyntax.ts index a3f3d7e369..c84e957f31 100644 --- a/src/domain/firmware/hexRecordSyntax.ts +++ b/src/domain/firmware/hexRecordSyntax.ts @@ -1,9 +1,9 @@ import { err, ok, type Result } from "../result.js"; /** ":" + count + address + type + 255 data bytes + checksum, excluding EOL. */ -export const INTEL_HEX_MAX_LINE_CHARS = 521; +const INTEL_HEX_MAX_LINE_CHARS = 521; /** "S" + type + count + 255 counted bytes, excluding EOL. */ -export const SREC_MAX_LINE_CHARS = 514; +const SREC_MAX_LINE_CHARS = 514; export type HexRecordEncoding = "intel-hex" | "srec"; diff --git a/src/domain/firmware/hexRecords.test.ts b/src/domain/firmware/hexRecords.test.ts index ec327092e9..9855dcdfda 100644 --- a/src/domain/firmware/hexRecords.test.ts +++ b/src/domain/firmware/hexRecords.test.ts @@ -6,11 +6,7 @@ import { createHexRecordDecoder, detectHexRecordEncoding, } from "./hexRecords.js"; -import { - INTEL_HEX_MAX_LINE_CHARS, - SREC_MAX_LINE_CHARS, - type HexRecordEncoding, -} from "./hexRecordSyntax.js"; +import type { HexRecordEncoding } from "./hexRecordSyntax.js"; import { INTEL_HEX_I16, INTEL_HEX_I32_GAP, @@ -233,7 +229,7 @@ describe("Intel HEX lines, termination and limits", () => { 0, Array.from({ length: 255 }, () => 1), ); - expect(longest).toHaveLength(INTEL_HEX_MAX_LINE_CHARS); + expect(longest).toHaveLength(521); expect(image(`${longest}\r\n:00000001FF\n`).metadata.data_bytes).toBe(255); const decoded = decode(`${longest}0\n`); expect(decoded).toMatchObject({ @@ -376,7 +372,7 @@ describe("S-record decoding", () => { 0, Array.from({ length: 252 }, () => 1), ); - expect(longest).toHaveLength(SREC_MAX_LINE_CHARS); + expect(longest).toHaveLength(514); expect(image(`${longest}\n`, "srec").metadata.data_bytes).toBe(252); expect(decode(`${longest} 0\n`, "srec").ok).toBe(false); }); diff --git a/src/domain/functionComparisonDimensions.ts b/src/domain/functionComparisonDimensions.ts index 6c2f1d2b62..64c1c96d2c 100644 --- a/src/domain/functionComparisonDimensions.ts +++ b/src/domain/functionComparisonDimensions.ts @@ -16,7 +16,6 @@ import { nameAndSwiftProjection, normalizeCfg, referencedStringProjection, - sorted, swiftLiteralCoverage, swiftLiteralSitesConflict, } from "./functionComparisonNormalization.js"; diff --git a/src/domain/hopperErrors.ts b/src/domain/hopperErrors.ts index fd00aaedce..914049c33c 100644 --- a/src/domain/hopperErrors.ts +++ b/src/domain/hopperErrors.ts @@ -41,7 +41,7 @@ export const HOPPER_STARTUP_TIMEOUT_REMEDIATION = "Review details.launcher and inspect Hopper for ongoing initial analysis or a pending loader, demo, or license dialog. For a large target, set REA_HOPPER_STARTUP_TIMEOUT_MS higher in the environment of a new REA invocation, then rerun the command; that invocation starts Hopper again. Resolve retained cleanup before rerunning."; /** Startup-timeout recovery for a caller whose REA runtime keeps running. */ -export const HOPPER_STARTUP_TIMEOUT_CONTINUING_REMEDIATION = +const HOPPER_STARTUP_TIMEOUT_CONTINUING_REMEDIATION = "Review details.launcher and inspect Hopper for ongoing initial analysis or a pending loader, demo, or license dialog. For a large target, use update_analysis_settings with setting hopper.startup_timeout_ms and action set for the next startup attempt without restarting REA, or increase REA_HOPPER_STARTUP_TIMEOUT_MS for future REA processes. Allow at least that long in the MCP client. Resolve retained cleanup before reopening."; /** Hopper did not respond within the configured operation deadline. */ diff --git a/src/domain/javascript/javascriptApplicationVersionRelationshipUncertainty.test.ts b/src/domain/javascript/javascriptApplicationVersionRelationshipUncertainty.test.ts index 6988b4f3d5..951d073434 100644 --- a/src/domain/javascript/javascriptApplicationVersionRelationshipUncertainty.test.ts +++ b/src/domain/javascript/javascriptApplicationVersionRelationshipUncertainty.test.ts @@ -7,7 +7,6 @@ import { import { createJavaScriptApplicationGraph, createJavaScriptApplicationEdge, - type JavaScriptApplicationGraph, } from "./javascriptApplicationGraph.js"; import { compareJavaScriptApplicationVersions } from "./javascriptApplicationVersionComparison.js"; diff --git a/src/domain/javascript/javascriptGraphqlOperations.ts b/src/domain/javascript/javascriptGraphqlOperations.ts index f95d0b0768..8bdf8798df 100644 --- a/src/domain/javascript/javascriptGraphqlOperations.ts +++ b/src/domain/javascript/javascriptGraphqlOperations.ts @@ -24,50 +24,40 @@ import type { * earlier strings still belong to this object and must not become standalone * operation findings during the string scan. */ -const graphqlSourceBodyLiterals = ( +const graphqlSourceOwnership = ( node: t.ObjectExpression, -): readonly t.StringLiteral[] | undefined => { - const hasLocationOffset = node.properties.some( - (property) => - t.isObjectProperty(property) && - semanticStaticPropertyKey(property.key, property.computed) === - "locationOffset", - ); - if (!hasLocationOffset) return undefined; - - const bodyStrings = (object: t.ObjectExpression) => - object.properties.flatMap((property) => - t.isObjectProperty(property) && - semanticStaticPropertyKey(property.key, property.computed) === "body" && - t.isStringLiteral(property.value) - ? [property.value] - : [], - ); - const bodies = bodyStrings(node); - - // Inline object spreads can own additional body values which the surrounding - // Source object may select. Include only body-key string literals from those - // spread values; ordinary strings elsewhere in the object remain scannable. - const visited = new WeakSet(); - visited.add(node); - const pending = node.properties.flatMap((property) => { - if (!t.isSpreadElement(property)) return []; - const spread = unwrapJavaScriptExpression(property.argument).node; - return t.isObjectExpression(spread) ? [spread] : []; - }); +): + | { + readonly bodies: readonly t.StringLiteral[]; + readonly objects: ReadonlySet; + } + | undefined => { + const bodies: t.StringLiteral[] = []; + let hasLocationOffset = false; + // Own marker presence and body literals through the same inline-spread walk. + // Unknown spreads cannot remove a known own marker. Ordinary nested objects + // and non-body strings remain independently scannable. + const visited = new Set(); + const pending = [node]; while (pending.length > 0) { const current = pending.pop(); if (current === undefined || visited.has(current)) continue; visited.add(current); - bodies.push(...bodyStrings(current)); for (const property of current.properties) { - if (t.isSpreadElement(property)) { + if (t.isObjectProperty(property)) { + const key = semanticStaticPropertyKey(property.key, property.computed); + if (key === "locationOffset") hasLocationOffset = true; + else if (key === "body" && t.isStringLiteral(property.value)) + bodies.push(property.value); + } else if (t.isSpreadElement(property)) { const spread = unwrapJavaScriptExpression(property.argument).node; if (t.isObjectExpression(spread)) pending.push(spread); } } } - return bodies.length === 0 ? undefined : bodies; + return hasLocationOffset && bodies.length > 0 + ? { bodies, objects: visited } + : undefined; }; const GRAPHQL_OPERATION_TYPES: ReadonlySet = new Set([ @@ -442,6 +432,7 @@ export const createJavaScriptGraphqlScan = ( index: JavaScriptBindingIndex, ): JavaScriptRpcScan => { const bodies = new WeakSet(); + const ownedSources = new WeakSet(); const copied = new WeakMap>(); const copies: GraphqlSourceCopy[] = []; const sources: { @@ -474,9 +465,14 @@ export const createJavaScriptGraphqlScan = ( copies.push({ ...source, operations }); } } - const owned = graphqlSourceBodyLiterals(node); + const owned = ownedSources.has(node) + ? undefined + : graphqlSourceOwnership(node); if (owned !== undefined) { - for (const body of owned) bodies.add(body); + for (const body of owned.bodies) bodies.add(body); + // The outer Source owns its inline spread objects too: they cannot + // later emit overwritten or unknown bodies as independent Sources. + for (const object of owned.objects) ownedSources.add(object); const body = semanticFinalObjectProperty(node, "body"); const recovered = t.isStringLiteral(body) ? graphqlOperationFromText(body.value) diff --git a/src/domain/javascript/javascriptGraphqlSourceOwnership.test.ts b/src/domain/javascript/javascriptGraphqlSourceOwnership.test.ts new file mode 100644 index 0000000000..a98174ae34 --- /dev/null +++ b/src/domain/javascript/javascriptGraphqlSourceOwnership.test.ts @@ -0,0 +1,60 @@ +import { expect, it } from "vitest"; +import { analyzeJavaScriptStaticSource } from "../../../tests/fixtures/javascriptStaticAnalysis.js"; + +it.each([ + [ + "direct marker", + 'locationOffset:{line:1,column:1},body:"query Old { x }",body:"query Current { x }"', + ["Current"], + ], + [ + "marker in an inline spread", + '...{locationOffset:{line:1,column:1}},body:"query Old { x }",body:"query Current { x }"', + ["Current"], + ], + [ + "marker in nested inline spreads", + '...{...{locationOffset:{line:1,column:1}}},body:"query Old { x }",body:"query Current { x }"', + ["Current"], + ], + [ + "overwritten Source in an inline spread", + '...{locationOffset:{line:1,column:1},body:"query Old { x }"},body:"query Current { x }"', + ["Current"], + ], + [ + "unknown final body after an inline Source", + '...{locationOffset:{line:1,column:1},body:"query Old { x }"},...runtimeOptions', + [], + ], + [ + "nested inline Sources with an unrelated nested Source", + '...{...{locationOffset:{line:1,column:1},body:"query Old { x }"},body:"query Middle { x }"},body:"query Current { x }",metadata:{locationOffset:{line:1,column:1},body:"query Independent { x }"}', + ["Current", "Independent"], + ], + [ + "unknown later body with an owned marker", + 'locationOffset:{line:1,column:1},body:"query Old { x }",...runtimeOptions', + [], + ], + [ + "ordinary nested metadata", + 'metadata:{locationOffset:{line:1,column:1}},body:"query Plain { x }"', + ["Plain"], + ], + [ + "unknown computed property after the body", + 'locationOffset:{line:1,column:1},body:"query Old { x }",[key]:runtimeValue', + [], + ], +] as const)( + "preserves Source body ownership for %s", + (_name, fields, expected) => { + const result = analyzeJavaScriptStaticSource(`const source={${fields}};`); + expect( + result.endpoints + .filter(({ mechanism }) => mechanism === "graphql:query") + .map(({ value }) => value), + ).toEqual(expected); + }, +); diff --git a/src/domain/javascript/javascriptModuleDeclaration.ts b/src/domain/javascript/javascriptModuleDeclaration.ts index 4a158fb41d..6548fd3ae5 100644 --- a/src/domain/javascript/javascriptModuleDeclaration.ts +++ b/src/domain/javascript/javascriptModuleDeclaration.ts @@ -83,7 +83,8 @@ export const semanticRuntimeExportValueSources = ( const values: t.Node[] = []; if (t.isVariableDeclaration(node.declaration)) for (const declaration of node.declaration.declarations) - values.push(...semanticWriteTargets(declaration.id)); + for (const target of semanticWriteTargets(declaration.id)) + values.push(target); else if ( (t.isFunctionDeclaration(node.declaration) || t.isClassDeclaration(node.declaration)) && diff --git a/src/domain/javascript/javascriptRuntimeLoadState.ts b/src/domain/javascript/javascriptRuntimeLoadState.ts index 853a3012f0..82bcfd1361 100644 --- a/src/domain/javascript/javascriptRuntimeLoadState.ts +++ b/src/domain/javascript/javascriptRuntimeLoadState.ts @@ -40,7 +40,7 @@ export const classifyStaticLoadStates = ( reconciliationComplete: options.reconciliationComplete, }, ); - states.push(...projection.states); + for (const state of projection) states.push(state); } return { states, omittedStates: 0 }; }; @@ -53,7 +53,7 @@ const classifyLayer = ( options: { readonly reconciliationComplete: boolean; }, -): { readonly states: JavaScriptStaticLoadState[] } => { +): readonly JavaScriptStaticLoadState[] => { const nodes = layer.graph.nodes.filter(isLoadStateNode); const direct = new Map(); const ambiguous = new Set(); @@ -89,7 +89,7 @@ const classifyLayer = ( ({ codeInventoryCompleteWithinScope }) => codeInventoryCompleteWithinScope, ); - const states = nodes.map((node) => { + return nodes.map((node) => { const runtimeNodeIds = uniqueSorted(direct.get(node.node_id) ?? []); if (runtimeNodeIds.length > 0) return state(layer, node, { @@ -127,7 +127,6 @@ const classifyLayer = ( : "static-or-runtime-coverage-incomplete", }); }); - return { states }; }; const residentNodes = ( diff --git a/src/domain/javascript/javascriptSemanticAmbientWrites.ts b/src/domain/javascript/javascriptSemanticAmbientWrites.ts index e8c5e87b56..4482b9f2f3 100644 --- a/src/domain/javascript/javascriptSemanticAmbientWrites.ts +++ b/src/domain/javascript/javascriptSemanticAmbientWrites.ts @@ -34,8 +34,6 @@ import { GLOBAL_OBJECT_NAMES, globalObjectName, globalReferenceNamespace, - isUnmodifiedGlobalReference, - isUnmodifiedImportMetaUrl, } from "./javascriptSemanticGlobalReference.js"; import { readSemanticRequireLoader } from "./javascriptSemanticRequireOriginSlot.js"; import { @@ -963,22 +961,15 @@ const createGlobalPathRead = ( }; byNamespace.set(namespace, effects); } - // The call that escapes a receiver contains the callee being identified. - // That escape cannot have replaced the callee before the call selects it. - const escapePrecedesRead = effects.escapes.some((effect) => { - const escape = effect.node; + const escapePrecedesRead = effects.escapes.some(({ node }) => { + // Selecting this callee precedes exposing its own receiver. Other reads, + // including arguments, need activation-aware ordering across repetitions. if ( - escape !== undefined && - escape !== site && - typeof escape.start === "number" && - typeof escape.end === "number" && - typeof site.start === "number" && - typeof site.end === "number" && - site.start >= escape.start && - site.end <= escape.end + (t.isCallExpression(node) || t.isOptionalCallExpression(node)) && + node.callee === site ) return false; - return semanticEffectTiming(site, escape, state) !== "after"; + return semanticEffectTiming(site, node, state) !== "after"; }); if ( (escapesCovered && effects.escapes.length === 0) || diff --git a/src/domain/javascript/javascriptSemanticAnalysis.child_process_callbacks.test.ts b/src/domain/javascript/javascriptSemanticAnalysis.child_process_callbacks.test.ts new file mode 100644 index 0000000000..8684465805 --- /dev/null +++ b/src/domain/javascript/javascriptSemanticAnalysis.child_process_callbacks.test.ts @@ -0,0 +1,67 @@ +import { expect, it } from "vitest"; +import { analyzeJavaScriptSemantics } from "./javascriptSemanticAnalysis.js"; + +const spawns = (source: string) => + analyzeJavaScriptSemantics(source).childProcessSpawns; + +it.each(["spawn", "fork"])("rejects function-valued %s options", (method) => { + expect( + spawns(` + import { ${method} } from "node:child_process"; + ${method}("worker.js", () => {}); + ${method}("worker.js", [], function options() {}); + ${method}("worker.js", [], class Options {}); + `), + ).toEqual([]); +}); + +it("rejects a literal exec callback after explicit options", () => { + expect( + spawns(` + import { exec } from "node:child_process"; + exec("node --version", {}, false); + exec("node --version", null, {}); + `), + ).toEqual([]); +}); + +it("selects execFile callbacks with and without an argv array", () => { + expect( + spawns(` + import { execFile } from "node:child_process"; + execFile("node", {}, { cwd: "." }); + execFile("node", [], {}, false); + execFile("node", null, {}, "callback"); + `), + ).toEqual([]); +}); + +it("keeps callback overloads and nullish callbacks", () => { + expect( + spawns(` + import { exec, execFile } from "node:child_process"; + exec("node --version", () => {}, false); + exec("node --version", {}, null); + execFile("node", () => {}, { cwd: false }); + execFile("node", [], () => {}, false); + execFile("node", {}, null); + execFile("node", [], {}, undefined); + `), + ).toHaveLength(6); +}); + +it("leaves uncertain callback slots and spread positions undecided", () => { + expect( + spawns(` + import { exec, execFile, spawn } from "node:child_process"; + function run(options, callback, args) { + exec("node --version", options, false); + exec("node --version", {}, callback); + execFile("node", options, false); + execFile("node", [], options, false); + execFile("node", ...args, {}, false); + spawn("node", ...args, () => {}); + } + `), + ).toHaveLength(6); +}); diff --git a/src/domain/javascript/javascriptSemanticAnalysis.fs_function_arguments.test.ts b/src/domain/javascript/javascriptSemanticAnalysis.fs_function_arguments.test.ts new file mode 100644 index 0000000000..53a3602f3c --- /dev/null +++ b/src/domain/javascript/javascriptSemanticAnalysis.fs_function_arguments.test.ts @@ -0,0 +1,94 @@ +import { expect, it } from "vitest"; +import { analyzeJavaScriptSemantics } from "./javascriptSemanticAnalysis.js"; +import { analyzeCommonJsSemanticFixture } from "../../../tests/fixtures/javascriptCommonJsAnalysis.js"; + +const functionValues = [ + ["arrow", "() => {}"], + ["function", "function () {}"], + ["async function", "async function () {}"], + ["generator", "function* () {}"], + ["class", "class {}"], +] as const; + +const resourcesFor = (call: string) => + analyzeJavaScriptSemantics(` + import {open, openSync} from "node:fs"; + import {open as openPromise} from "node:fs/promises"; + function f(p, cb, ...args) { ${call}; } + `).resourceOperations; + +it.each([ + "open(p)", + 'open(p, "r")', + 'open(p, "r", 0o644)', + 'open(p, "r", 0o644, 17)', + 'open(p, "r", 0o644, null)', + 'open(p, "r", 0o644, {})', + 'open(p, "r", 0o644, void sideEffect())', +])( + "omits an open whose normalized callback is missing or nonfunction: %s", + (call) => { + expect(resourcesFor(call)).toEqual([]); + }, +); + +it.each([ + 'open(p, "r", cb, 17)', + 'open(p, "r", 0o644, cb)', + 'open(p, "r", 0o644, ...args)', +])("preserves an unknown callback or overload: %s", (call) => { + expect(resourcesFor(call)).toHaveLength(1); +}); + +it.each(functionValues)( + "rejects a %s in fs flag/mode value slots", + (_kind, value) => { + for (const call of [ + `openSync(p, ${value})`, + `openSync(p, "r", ${value})`, + `openPromise(p, ${value})`, + `openPromise(p, "r", ${value})`, + `open(p, ${value}, cb)`, + `open(p, ${value}, ...args, cb)`, + ]) + expect(resourcesFor(call), call).toEqual([]); + }, +); + +it.each(functionValues)( + "preserves %s callback overloads and an empty trailing spread", + (_kind, value) => { + for (const call of [ + `open(p, ${value})`, + `open(p, "r", ${value})`, + `open(p, "r", ${value}, 17)`, + `open(p, ${value}, ...args)`, + ]) + expect(resourcesFor(call), call).toMatchObject([ + { kind: "acquire", method: "open" }, + ]); + }, +); + +it.each([ + "openSync(...args, () => {})", + "openPromise(p, ...args, () => {})", + "open(...args, () => {}, cb)", + "openSync(p, flags)", + 'openPromise(p, "r", mode)', +])("leaves unresolved argument positions and values undecided: %s", (call) => { + expect(resourcesFor(call)).toHaveLength(1); +}); + +it.each(["openSync(p, () => {})", 'openSync(p, "r", () => {})'])( + "applies Node's rejection only to an established builtin: %s", + (call) => { + const source = `const {openSync}=require("node:fs"); function f(p) { ${call}; }`; + expect(analyzeJavaScriptSemantics(source).resourceOperations).toMatchObject( + [{ kind: "acquire", method: "openSync", builtinAuthority: "candidate" }], + ); + expect(analyzeCommonJsSemanticFixture(source).resourceOperations).toEqual( + [], + ); + }, +); diff --git a/src/domain/javascript/javascriptSemanticAnalysis.getter_aliases.test.ts b/src/domain/javascript/javascriptSemanticAnalysis.getter_aliases.test.ts index 1eeaec3503..a4fe7ad5a2 100644 --- a/src/domain/javascript/javascriptSemanticAnalysis.getter_aliases.test.ts +++ b/src/domain/javascript/javascriptSemanticAnalysis.getter_aliases.test.ts @@ -109,6 +109,7 @@ describe("projected private getter receivers", () => { describe("private receiver field ownership", () => { it("bounds growing receiver projections under cyclic class inheritance", () => { + const started = performance.now(); const ir = analyzeJavaScriptSemantics(`export function result() { const shared = { x: 1 }; class A extends B { @@ -120,11 +121,13 @@ describe("private receiver field ownership", () => { new A().v.x = 2; return shared.x; }`); + // Unbounded projection growth would end only at a resource ceiling. + expect(performance.now() - started).toBeLessThan(1_000); expect(onlyCallable(ir, "result").returnSites[0]?.value).toMatchObject({ status: "literal", value: 1, }); - }, 1000); + }); it.each([ [ diff --git a/src/domain/javascript/javascriptSemanticAnalysis.member_mutations.test.ts b/src/domain/javascript/javascriptSemanticAnalysis.member_mutations.test.ts index d3b3f9c136..4ede75e190 100644 --- a/src/domain/javascript/javascriptSemanticAnalysis.member_mutations.test.ts +++ b/src/domain/javascript/javascriptSemanticAnalysis.member_mutations.test.ts @@ -1076,7 +1076,7 @@ describe("property mutation collection on minified bundles", () => { { value: { status: "unknown" } }, ], }); - }, 30000); + }); }); describe("results of methods on an escaped receiver", () => { @@ -1112,7 +1112,7 @@ describe("results of methods on an escaped receiver", () => { status: "array", items: [{ value: { status: "unknown" } }], }); - }, 30000); + }); }); it.each([ diff --git a/src/domain/javascript/javascriptSemanticAnalysis.reference_lifetimes.test.ts b/src/domain/javascript/javascriptSemanticAnalysis.reference_lifetimes.test.ts index ed98d2626f..c327b5b785 100644 --- a/src/domain/javascript/javascriptSemanticAnalysis.reference_lifetimes.test.ts +++ b/src/domain/javascript/javascriptSemanticAnalysis.reference_lifetimes.test.ts @@ -26,7 +26,7 @@ describe("bounded paths through dynamic selectors and class origins", () => { value: 42, }); expect(performance.now() - start).toBeLessThan(2000); - }, 30000); + }); it("retains effects through class getter aliases while bounding loop paths", () => { expect( @@ -65,7 +65,7 @@ describe("bounded paths through dynamic selectors and class origins", () => { "unknown", ); expect(performance.now() - start).toBeLessThan(2000); - }, 30000); + }); it("retains shared-child mutations reached by dynamic-key alias loops", () => { expect( @@ -103,7 +103,7 @@ describe("bounded member path propagation", () => { ); expect(callable.returnSites[0]?.value.status).toBe("unknown"); expect(performance.now() - start).toBeLessThan(2000); - }, 30000); + }); it("preserves Array prototype effects through an otherwise unknown alias", () => { expect( @@ -125,7 +125,7 @@ describe("bounded member path propagation", () => { ), ).toEqual({ status: "literal", value: 3 }); expect(performance.now() - start).toBeLessThan(2000); - }, 30000); + }); // A dynamic self alias escapes its repeated prefix, which conservatively // covers every child; a static key without that slot keeps the literal. @@ -144,7 +144,6 @@ describe("bounded member path propagation", () => { expect(performance.now() - start).toBeLessThan(2000); expect(value?.status).toBe(status); }, - 30000, ); it.each([ @@ -196,7 +195,6 @@ describe("bounded member path propagation", () => { expect(performance.now() - start).toBeLessThan(2000); expect(value?.status).toBe(status); }, - 30000, ); it.each([ @@ -282,7 +280,7 @@ describe("reference lifetimes across calls and shallow copies", () => { `const alias0 = [1]; ${declarations} const ${pattern} = alias9; return alias0[0];`, ); expect(performance.now() - start).toBeLessThan(2000); - }, 30000); + }); it("bounds conditional shallow-copy traversal while retaining shared children", () => { const declarations = Array.from( @@ -297,7 +295,7 @@ describe("reference lifetimes across calls and shallow copies", () => { )?.status, ).toBe("unknown"); expect(performance.now() - start).toBeLessThan(2000); - }, 30000); + }); it.each([ ["child: {}", "child: {}", "literal"], diff --git a/src/domain/javascript/javascriptSemanticAnalysis.timer_delays.test.ts b/src/domain/javascript/javascriptSemanticAnalysis.timer_delays.test.ts new file mode 100644 index 0000000000..b4864ba683 --- /dev/null +++ b/src/domain/javascript/javascriptSemanticAnalysis.timer_delays.test.ts @@ -0,0 +1,189 @@ +import { expect, it } from "vitest"; +import { analyzeJavaScriptSemantics } from "./javascriptSemanticAnalysis.js"; + +const operations = (source: string) => + analyzeJavaScriptSemantics(source).timerOperations; + +it.each(["setTimeout", "setInterval"])( + "does not create a %s handle when delay conversion must throw", + (method) => { + const result = operations(` + import { ${method}, clearTimeout } from "node:timers"; + const positive = ${method}(() => {}, 1n); + const negative = ${method}(() => {}, -1n); + const zero = ${method}(() => {}, 0n); + const symbol = ${method}(() => {}, Symbol("delay")); + const converted = ${method}(() => {}, BigInt("1")); + clearTimeout(positive); + `); + expect(result.filter(({ kind }) => kind === "schedule")).toEqual([]); + expect(result).toMatchObject([ + { kind: "cancel", resolution: "unresolved", linkedTimerId: null }, + ]); + }, +); + +it("keeps supported delays and the immediate callback's BigInt argument", () => { + const result = operations(` + import { setTimeout, setInterval, setImmediate } from "node:timers"; + setTimeout(() => {}, "2"); + setInterval(() => {}, null); + setTimeout(() => {}, !1n); + setTimeout(() => {}, void 1n); + setImmediate(() => {}, 1n); + `); + expect(result.map(({ method }) => method)).toEqual([ + "setTimeout", + "setInterval", + "setTimeout", + "setTimeout", + "setImmediate", + ]); +}); + +it("retains uncertain delays, shifted arguments and shadowed constructors", () => { + const result = operations(` + import { setTimeout } from "node:timers"; + function schedule(delay, args, Symbol, BigInt) { + setTimeout(() => {}, delay); + setTimeout(...args, 1n); + setTimeout(() => {}, ...args); + setTimeout(() => {}, Symbol("delay")); + setTimeout(() => {}, BigInt("1")); + } + `); + expect(result).toHaveLength(5); +}); + +it.each([ + 'eval("Symbol = () => 1; BigInt = () => 1");', + '(0, eval)("Symbol = () => 1; BigInt = () => 1");', + 'Function("Symbol = () => 1; BigInt = () => 1")();', +])( + "retains delays after global code may replace constructors: %s", + (effect) => { + expect( + operations(` + import { setTimeout, setInterval } from "node:timers"; + ${effect} + setTimeout(() => {}, Symbol("delay")); + setInterval(() => {}, BigInt("1")); + `).map(({ method }) => method), + ).toEqual(["setTimeout", "setInterval"]); + }, +); + +it("keeps literal rejection independent of global code effects", () => { + expect( + operations(` + import { setTimeout } from "node:timers"; + eval(code); + setTimeout(() => {}, 1n); + `), + ).toEqual([]); +}); + +it("ignores later global code and shadowed eval for constructor identity", () => { + expect( + operations(` + import { setTimeout } from "node:timers"; + setTimeout(() => {}, Symbol("delay")); + eval("Symbol = () => 1"); + `), + ).toEqual([]); + expect( + operations(` + import { setTimeout } from "node:timers"; + function run(eval) { + eval("Symbol = () => 1"); + setTimeout(() => {}, Symbol("delay")); + } + `), + ).toEqual([]); +}); + +it.each([ + ["Symbol", "configure(globalThis);"], + ["BigInt", "configure(globalThis);"], + ["Symbol", "const host = globalThis; configure(host);"], + ["BigInt", "configure({ host: globalThis });"], +])("retains %s delays and cancellation after %s", (constructor, effect) => { + const result = operations(` + import configure from "./configure.js"; + import { setTimeout, clearTimeout } from "node:timers"; + ${effect} + const handle = setTimeout(() => {}, ${constructor}("1")); + clearTimeout(handle); + `); + expect(result).toMatchObject([ + { kind: "schedule", resolution: "complete" }, + { kind: "cancel", resolution: "complete" }, + ]); + expect(result[1]?.linkedTimerId).toBe(result[0]?.timerId); +}); + +it("still rejects literal BigInt delays after the global object escapes", () => { + expect( + operations(` + import configure from "./configure.js"; + import { setTimeout, setInterval } from "node:timers"; + configure(globalThis); + setTimeout(() => {}, 1n); + setInterval(() => {}, -1n); + `), + ).toEqual([]); +}); + +it("does not let later or unrelated escapes change constructor identity", () => { + expect( + operations(` + import configure from "./configure.js"; + import { setTimeout, setInterval } from "node:timers"; + configure({}); + setTimeout(() => {}, Symbol("1")); + setInterval(() => {}, BigInt("1")); + configure(globalThis); + `), + ).toEqual([]); +}); + +it("selects a constructor before an argument can escape the global object", () => { + expect( + operations(` + import configure from "./configure.js"; + import { setTimeout } from "node:timers"; + setTimeout(() => {}, Symbol(configure(globalThis))); + `), + ).toEqual([]); +}); + +it.each(["Symbol", "BigInt"])( + "retains %s identity uncertainty inside an escaped callback", + (constructor) => { + const result = operations(` + import configure from "./configure.js"; + import { setTimeout, clearTimeout } from "node:timers"; + configure(globalThis, () => { + const handle = setTimeout(() => {}, ${constructor}("1")); + clearTimeout(handle); + }); + `); + expect(result).toMatchObject([ + { kind: "schedule", resolution: "complete" }, + { kind: "cancel", resolution: "complete" }, + ]); + expect(result[1]?.linkedTimerId).toBe(result[0]?.timerId); + }, +); + +it.each([ + 'for (let i=0;i<2;i++) configure(globalThis, i ? setTimeout(()=>{}, Symbol("1")) : null);', + 'function run(first) { configure(globalThis, first ? null : setTimeout(()=>{}, BigInt("1"))); } run(true); run(false);', + 'let first=true; while (next()) { configure(globalThis, first ? null : setTimeout(()=>{}, Symbol("1"))); first=false; }', +])("retains constructor uncertainty across repeated escapes: %s", (source) => { + expect( + operations( + `import configure from "./configure.js"; import {setTimeout} from "node:timers"; ${source}`, + ), + ).toMatchObject([{ kind: "schedule" }]); +}); diff --git a/src/domain/javascript/javascriptSemanticAnalysis.ts b/src/domain/javascript/javascriptSemanticAnalysis.ts index 3234457612..c29f75c0a3 100644 --- a/src/domain/javascript/javascriptSemanticAnalysis.ts +++ b/src/domain/javascript/javascriptSemanticAnalysis.ts @@ -119,7 +119,6 @@ import { } from "./javascriptSemanticCoverage.js"; import { semanticResourceLimitReason } from "./javascriptSemanticResourceLimits.js"; import { - javaScriptRuntimeContext, javaScriptProgramStrictness, parseJavaScriptSource, type ParsedJavaScriptSource, diff --git a/src/domain/javascript/javascriptSemanticAsyncEffects.ts b/src/domain/javascript/javascriptSemanticAsyncEffects.ts index 4e5802e8c4..6fb46d96e5 100644 --- a/src/domain/javascript/javascriptSemanticAsyncEffects.ts +++ b/src/domain/javascript/javascriptSemanticAsyncEffects.ts @@ -19,8 +19,14 @@ import { } from "./javascriptSemanticState.js"; import { semanticBindingAtRead } from "./javascriptSemanticMutationInitializers.js"; import { traverseJavaScriptValueAst } from "./javascriptSemanticTraversal.js"; -import { isUnmodifiedGlobalReference } from "./javascriptSemanticAmbientWrites.js"; -import { semanticOpaqueNameMayHaveChanged } from "./javascriptSemanticOpaqueEffects.js"; +import { + createGlobalReferenceReader, + isUnmodifiedGlobalReference, +} from "./javascriptSemanticAmbientWrites.js"; +import { + semanticOpaqueGlobalMayHaveChanged, + semanticOpaqueNameMayHaveChanged, +} from "./javascriptSemanticOpaqueEffects.js"; import { range } from "./javascriptStaticAnalysisHelpers.js"; import { builtinDataEffectIdentity, @@ -109,6 +115,10 @@ interface TimerCollectionInput { readonly ownerCallableId: string | null; readonly state: JavaScriptSemanticAnalysisState; readonly output: TimerCandidate[]; + readonly delayConstructors: ReadonlyMap< + string, + ReturnType + >; readonly ancestors: readonly t.Node[]; } @@ -120,6 +130,10 @@ export const collectJavaScriptSemanticAsyncEffects = ( ): AsyncEffectAnalysis => { const events: JavaScriptSemanticEventOperation[] = []; const timers: TimerCandidate[] = []; + const delayConstructors = new Map([ + ["Symbol", createGlobalReferenceReader(state, ["Symbol"])], + ["BigInt", createGlobalReferenceReader(state, ["BigInt"])], + ]); const callableIds = new Set(callables.map(({ callableId }) => callableId)); const callableStack = createSemanticCallableIdStack(callableIds); const ancestors: t.Node[] = []; @@ -134,6 +148,7 @@ export const collectJavaScriptSemanticAsyncEffects = ( ownerCallableId, state, output: timers, + delayConstructors, ancestors, }); } @@ -376,6 +391,38 @@ const schedulesWithNonFunctionCallback = ( return t.isUnaryExpression(value); }; +// Node converts timeout and interval delays to numbers before creating a handle. +// BigInt and Symbol values throw there. A spread can shift the delay slot, and +// shadowed or replaced constructors cannot establish the argument's type. +const schedulesWithInvalidDelay = ( + node: t.CallExpression | t.OptionalCallExpression, + state: JavaScriptSemanticAnalysisState, + delayConstructors: TimerCollectionInput["delayConstructors"], +): boolean => { + if ( + node.arguments.slice(0, 2).some((argument) => t.isSpreadElement(argument)) + ) + return false; + const argument = node.arguments[1]; + if (argument === undefined) return false; + const value = unwrapJavaScriptExpression(argument).node; + if (t.isBigIntLiteral(value)) return true; + if ( + t.isUnaryExpression(value) && + ["+", "-", "~"].includes(value.operator) && + t.isBigIntLiteral(unwrapJavaScriptExpression(value.argument).node) + ) + return true; + return ( + t.isCallExpression(value) && + t.isIdentifier(value.callee) && + delayConstructors.get(value.callee.name)?.(value.callee).status === + "unmodified" && + !semanticOpaqueNameMayHaveChanged(value.callee, state) && + !semanticOpaqueGlobalMayHaveChanged(value.callee, state) + ); +}; + const collectTimer = (input: TimerCollectionInput): void => { const { node, ownerCallableId, state, output, ancestors } = input; const identity = timerMethod(node.callee, state); @@ -384,7 +431,9 @@ const collectTimer = (input: TimerCollectionInput): void => { if ( TIMER_SCHEDULE_METHODS.some((candidate) => candidate === method) && nodeBuiltinTimerMethod(node.callee, state) !== null && - schedulesWithNonFunctionCallback(node, input.state) + (schedulesWithNonFunctionCallback(node, state) || + ((method === "setTimeout" || method === "setInterval") && + schedulesWithInvalidDelay(node, state, input.delayConstructors))) ) return; const kind = TIMER_SCHEDULE_METHODS.some((candidate) => candidate === method) diff --git a/src/domain/javascript/javascriptSemanticChildProcesses.ts b/src/domain/javascript/javascriptSemanticChildProcesses.ts index 38d0f04bc2..c85ec82b56 100644 --- a/src/domain/javascript/javascriptSemanticChildProcesses.ts +++ b/src/domain/javascript/javascriptSemanticChildProcesses.ts @@ -79,7 +79,8 @@ export const collectJavaScriptSemanticChildProcesses = ( !forkRejectsStdioArray(candidate, context) && !rejectsStdioLiteral(candidate, context) && !rejectsProcessOptionLiteral(candidate, context) && - !rejectsOptionsShape(candidate, context, state), + !rejectsOptionsShape(candidate, context, state) && + !rejectsCallback(candidate, context), ); const childProcessSpawns = candidates.map((candidate) => immutableSpawn(candidate, context, state), @@ -647,6 +648,13 @@ const rejectsOptionsShape = ( if (options === undefined) return false; const value = unwrapJavaScriptExpression(options).node; if (context.isArray(value)) return true; + if ( + candidate.method !== "execFile" && + (t.isFunctionExpression(value) || + t.isArrowFunctionExpression(value) || + t.isClassExpression(value)) + ) + return true; const literal = readExactJavaScriptLiteral(value); if (literal.found) return !(candidate.method !== "spawn" && literal.value === null); @@ -662,6 +670,52 @@ const rejectsOptionsShape = ( return isUnpricedPrimitiveLiteral(value); }; +/** Select only callback slots whose overload is established by source syntax. */ +const rejectsCallback = ( + candidate: SpawnCandidate, + context: ChildSpawnContext, +): boolean => { + if (candidate.method !== "exec" && candidate.method !== "execFile") + return false; + const args = candidate.node.arguments; + if (args.slice(0, 4).some((argument) => t.isSpreadElement(argument))) + return false; + const second = args[1] && unwrapJavaScriptExpression(args[1]).node; + const third = args[2] && unwrapJavaScriptExpression(args[2]).node; + let callback: t.Node | undefined; + if (candidate.method === "exec") { + // A function in slot 1 replaces slot 2, even when slot 2 is not callable. + if ( + second !== undefined && + !context.isNullish(second) && + !rejectsSemanticEventListener(second) + ) + return false; + callback = third; + } else if (t.isObjectExpression(second) || t.isRegExpLiteral(second)) { + callback = third; + } else if ( + second === undefined || + context.isArray(second) || + context.isNullish(second) + ) { + // With argv present, a third-slot function replaces the fourth callback. + if ( + third !== undefined && + !context.isNullish(third) && + !rejectsSemanticEventListener(third) + ) + return false; + callback = args[3]; + } else { + return false; + } + return ( + callback !== undefined && + !context.isNullish(callback) && + rejectsSemanticEventListener(callback) + ); +}; /** * The exact-value reader deliberately refuses to price a BigInt, a non-finite * numeric literal such as `1e400`, and a negated numeric literal such as `-0`. diff --git a/src/domain/javascript/javascriptSemanticCycles.test.ts b/src/domain/javascript/javascriptSemanticCycles.test.ts index 0019aae4ee..35f5c7db3b 100644 --- a/src/domain/javascript/javascriptSemanticCycles.test.ts +++ b/src/domain/javascript/javascriptSemanticCycles.test.ts @@ -1,17 +1,11 @@ -import { readdirSync, readFileSync, statSync } from "node:fs"; -import { dirname, join, relative, resolve } from "node:path"; +import { dirname } from "node:path"; import { fileURLToPath } from "node:url"; -import { describe, expect, it } from "vitest"; +import { describe, it } from "vitest"; -import { readImportSpecifiers } from "../../../scripts/lib/import-specifiers.mjs"; +import { expectImportCycles } from "../../../tests/fixtures/importCycles.js"; const root = dirname(fileURLToPath(import.meta.url)); -const relativeSpecifiers = (source: string): readonly string[] => - readImportSpecifiers(source) - .map(({ specifier }) => specifier) - .filter((specifier) => specifier.startsWith(".")); - /** * Cycles that already existed outside the semantic analysis hub. Membership is * exact so a new edge cannot hide inside the allow-list. @@ -42,97 +36,8 @@ const allowedCycleEdges = [ "javascriptStaticAnalysisState.ts -> javascriptStaticAnalysisFindings.ts", ].sort(); -const productionFiles = (directory: string): string[] => { - const files: string[] = []; - for (const name of readdirSync(directory)) { - const path = join(directory, name); - if (statSync(path).isDirectory()) files.push(...productionFiles(path)); - else if (name.endsWith(".ts") && !name.includes(".test.")) files.push(path); - } - return files; -}; - -const importGraph = (): Map> => { - const files = new Set( - productionFiles(root).map((path) => relative(root, path)), - ); - const graph = new Map>(); - for (const file of files) { - const source = readFileSync(join(root, file), "utf8"); - const edges = new Set(); - for (const spec of relativeSpecifiers(source)) { - const resolved = resolve(dirname(join(root, file)), spec).replace( - /\.js$/, - ".ts", - ); - const target = relative(root, resolved); - if (files.has(target)) edges.add(target); - } - graph.set(file, edges); - } - return graph; -}; - -const stronglyConnected = ( - graph: Map>, -): readonly (readonly string[])[] => { - let index = 0; - const stack: string[] = []; - const onStack = new Set(); - const indices = new Map(); - const low = new Map(); - const components: string[][] = []; - const visit = (node: string): void => { - indices.set(node, index); - low.set(node, index); - index += 1; - stack.push(node); - onStack.add(node); - for (const next of graph.get(node) ?? []) { - if (!indices.has(next)) { - visit(next); - low.set(node, Math.min(low.get(node) ?? index, low.get(next) ?? index)); - } else if (onStack.has(next)) { - low.set( - node, - Math.min(low.get(node) ?? index, indices.get(next) ?? index), - ); - } - } - if (low.get(node) === indices.get(node)) { - const component: string[] = []; - let member = ""; - while (member !== node) { - member = stack.pop() ?? node; - onStack.delete(member); - component.push(member); - } - if (component.length > 1) components.push(component.sort()); - } - }; - for (const node of [...graph.keys()].sort()) { - if (!indices.has(node)) visit(node); - } - return components.sort( - (left, right) => - right.length - left.length || left.join().localeCompare(right.join()), - ); -}; - describe("javascript semantic import cycles", () => { it("keeps the semantic modules out of one import cycle", () => { - const graph = importGraph(); - const cycles = stronglyConnected(graph).map((cycle) => cycle.join("\n")); - expect(cycles).toEqual( - allowedCycles.map((cycle) => [...cycle].sort().join("\n")), - ); - const internalEdges = allowedCycles.flatMap((cycle) => - cycle.flatMap((source) => - [...(graph.get(source) ?? [])] - .filter((target) => cycle.includes(target)) - .map((target) => `${source} -> ${target}`), - ), - ); - expect(internalEdges.sort()).toEqual(allowedCycleEdges); + expectImportCycles(root, allowedCycles, allowedCycleEdges); }); }); diff --git a/src/domain/javascript/javascriptSemanticGetterResultWalker.ts b/src/domain/javascript/javascriptSemanticGetterResultWalker.ts index 3144b0d1ab..f9049b12f2 100644 --- a/src/domain/javascript/javascriptSemanticGetterResultWalker.ts +++ b/src/domain/javascript/javascriptSemanticGetterResultWalker.ts @@ -9,7 +9,6 @@ import { semanticReferenceSourcesInactive } from "./javascriptSemanticReferenceS import { resolveSemanticBindingState, type JavaScriptSemanticBindingState, - type JavaScriptSemanticAnalysisState, } from "./javascriptSemanticState.js"; import type { JavaScriptSemanticObjectReference as Reference } from "./javascriptSemanticObjectReference.js"; import type { JavaScriptSemanticPropertyPath as PropertyPath } from "./javascriptSemanticPropertyPaths.js"; diff --git a/src/domain/javascript/javascriptSemanticResources.ts b/src/domain/javascript/javascriptSemanticResources.ts index 0bff28ed2b..358d54c601 100644 --- a/src/domain/javascript/javascriptSemanticResources.ts +++ b/src/domain/javascript/javascriptSemanticResources.ts @@ -1,4 +1,5 @@ import * as t from "@babel/types"; +import { rejectsSemanticEventListener } from "./javascriptSemanticAsyncEffects.js"; import { BUILTIN_RESOURCE_SPECIFIERS, @@ -95,9 +96,16 @@ export const collectJavaScriptSemanticResources = ( ]; }; +/** These expressions have typeof "function", including a class constructor. */ +const isFunctionValuedSyntax = (value: t.Node): boolean => + t.isFunctionExpression(value) || + t.isArrowFunctionExpression(value) || + t.isClassExpression(value); + /** Literal kinds Node rejects for both open flags and modes. */ const rejectsFlagOrModeLiteral = (value: t.Node): boolean => { return ( + isFunctionValuedSyntax(value) || t.isBigIntLiteral(value) || t.isBooleanLiteral(value) || t.isRegExpLiteral(value) || @@ -118,6 +126,7 @@ const rejectsFlagOrModeLiteral = (value: t.Node): boolean => { const rejectsOpenFlagLiteral = ( node: t.CallExpression | t.OptionalCallExpression, context: DataEffectTraversalContext, + callbackForm: boolean, ): boolean => { // A spread before the flag slot shifts the runtime position, so argument 1 is // not provably the flag. `openSync(...args, "0600")` with @@ -126,6 +135,16 @@ const rejectsOpenFlagLiteral = ( const flag = node.arguments[1]; if (flag === undefined || t.isSpreadElement(flag)) return false; const value = unwrapJavaScriptExpression(flag).node; + // fs.open(path, callback) defaults the flags. Later spreads may all be + // empty; a fixed third argument is needed to exclude that overload. + if ( + callbackForm && + isFunctionValuedSyntax(value) && + node.arguments.every( + (argument, index) => index < 2 || t.isSpreadElement(argument), + ) + ) + return false; // A no-expression template literal is exactly readable, so it is the same flag // string Node receives. const flagText = t.isStringLiteral(value) @@ -289,6 +308,7 @@ const isNonFiniteNumber = ( const rejectsOpenModeLiteral = ( node: t.CallExpression | t.OptionalCallExpression, context: DataEffectTraversalContext, + callbackForm: boolean, ): boolean => { // A spread before the mode slot shifts the runtime position. if ( @@ -299,6 +319,8 @@ const rejectsOpenModeLiteral = ( const mode = node.arguments[2]; if (mode === undefined || t.isSpreadElement(mode)) return false; const value = unwrapJavaScriptExpression(mode).node; + // Only callback-form fs.open treats a function in this slot as its callback. + if (callbackForm && isFunctionValuedSyntax(value)) return false; // `null`, `undefined` and `void ` all default the mode. if (t.isNullLiteral(value)) return false; // A no-expression template literal is exactly readable, so it is the same @@ -339,6 +361,21 @@ const openFlagInFirstArgument = (reference: AcquisitionReference): boolean => ? reference.specifier === "fs" || reference.specifier === "fs/promises" : false; +/** Node normalizes fs.open's callback before validating its function type. */ +const rejectsOpenCallbackLiteral = ( + node: t.CallExpression | t.OptionalCallExpression, +): boolean => { + // A spread can change both arity and which operand is the callback. + if (node.arguments.some((argument) => t.isSpreadElement(argument))) + return false; + if (node.arguments.length < 3) + return rejectsSemanticEventListener(node.arguments[1]); + // A function-valued mode is the callback; an unknown mode may be one too. + // Both can supersede a missing or invalid fourth operand. + if (!rejectsSemanticEventListener(node.arguments[2])) return false; + return rejectsSemanticEventListener(node.arguments[3]); +}; + const collectAcquisitions = ( program: t.Program, context: DataEffectTraversalContext, @@ -348,9 +385,15 @@ const collectAcquisitions = ( if (!t.isCallExpression(node) && !t.isOptionalCallExpression(node)) return; const reference = acquisitionMethod(node.callee, context.state); if (reference === null) return; - if (openFlagInFirstArgument(reference)) { - if (rejectsOpenFlagLiteral(node, context)) return; - if (rejectsOpenModeLiteral(node, context)) return; + if ( + openFlagInFirstArgument(reference) && + reference.builtinAuthority !== "candidate" + ) { + const callbackForm = + reference.specifier === "fs" && reference.method === "open"; + if (callbackForm && rejectsOpenCallbackLiteral(node)) return; + if (rejectsOpenFlagLiteral(node, context, callbackForm)) return; + if (rejectsOpenModeLiteral(node, context, callbackForm)) return; } output.push({ node, diff --git a/src/domain/javascript/javascriptSemanticSlots.ts b/src/domain/javascript/javascriptSemanticSlots.ts index 4150231e40..352dc3dff1 100644 --- a/src/domain/javascript/javascriptSemanticSlots.ts +++ b/src/domain/javascript/javascriptSemanticSlots.ts @@ -1,3 +1,4 @@ +import { escapeJsonPointerToken } from "../jsonPointer.js"; import type { JavaScriptSemanticProperty, JavaScriptSemanticValue, @@ -100,6 +101,4 @@ export const semanticSlotAtPath = ( /** Encode path segments as an unambiguous JSON Pointer, preserving empty keys. */ export const semanticPropertyPointer = (path: readonly string[]): string => - path - .map((key) => `/${key.replaceAll("~", "~0").replaceAll("/", "~1")}`) - .join(""); + path.map((key) => `/${escapeJsonPointerToken(key)}`).join(""); diff --git a/src/domain/javascript/javascriptStaticAnalysisBundler.ts b/src/domain/javascript/javascriptStaticAnalysisBundler.ts index 429654af56..8c207d5e24 100644 --- a/src/domain/javascript/javascriptStaticAnalysisBundler.ts +++ b/src/domain/javascript/javascriptStaticAnalysisBundler.ts @@ -230,7 +230,7 @@ const recoverBundlerModules = ( ); if (recovered === null) continue; modules.push(recovered.module); - asyncChunkKeys.push(...recovered.asyncChunkKeys); + for (const key of recovered.asyncChunkKeys) asyncChunkKeys.push(key); unknownAsyncChunkKeys += recovered.unknownAsyncChunkKeys; } return { diff --git a/src/domain/javascript/javascriptStaticAnalysisHelpers.ts b/src/domain/javascript/javascriptStaticAnalysisHelpers.ts index 25862ab5a8..49bd4961f0 100644 --- a/src/domain/javascript/javascriptStaticAnalysisHelpers.ts +++ b/src/domain/javascript/javascriptStaticAnalysisHelpers.ts @@ -507,20 +507,6 @@ const isRequestUrlOrPathLiteral = (value: string): boolean => value.startsWith("./") || value.startsWith("../"); -/** Explicit request references, classified after receiver-specific exclusions. */ -const isRequestUrlReference = (value: string): boolean => { - if ( - isRequestUrlOrPathLiteral(value) || - value.startsWith("?") || - value.startsWith("#") - ) - return true; - // WHATWG accepts HTTP/WS scheme forms without authority slashes. Parse - // without a base so bare keys stay ambiguous, and keep the source spelling. - if (!/^(?:https?|wss?):/iu.test(value)) return false; - return URL.parse(value) !== null; -}; - /** * Browsing-context and document `open` take a URL or type first and a target * name second. The name is only a spelling, since `parent` or `self` may be a diff --git a/src/domain/jsonPointer.ts b/src/domain/jsonPointer.ts new file mode 100644 index 0000000000..ed8949c43b --- /dev/null +++ b/src/domain/jsonPointer.ts @@ -0,0 +1,13 @@ +/** Encode one RFC 6901 reference token. */ +export const escapeJsonPointerToken = (token: string): string => + token.replaceAll("~", "~0").replaceAll("/", "~1"); + +/** Decode a reference token without validating the enclosing pointer. */ +export const unescapeJsonPointerToken = (token: string): string => + token.replaceAll("~1", "/").replaceAll("~0", "~"); + +/** Split an already-admitted JSON pointer into decoded reference tokens. */ +export const jsonPointerTokens = (pointer: string): string[] => + pointer === "" + ? [] + : pointer.slice(1).split("/").map(unescapeJsonPointerToken); diff --git a/src/domain/localPath.ts b/src/domain/localPath.ts index 24f1f60e3b..2761e98719 100644 --- a/src/domain/localPath.ts +++ b/src/domain/localPath.ts @@ -1,6 +1,8 @@ import { isAbsolute, relative, sep } from "node:path"; import { z } from "zod"; +import { rejectUnpairedSurrogates } from "./stringPatterns.js"; + /** * Reject OS-invalid NUL bytes in a local filesystem name. This is not a path * shape check: an empty or relative value is admitted here and narrowed by the @@ -16,13 +18,15 @@ export const nulFreeStringSchema = z .string() .regex(/^[^\x00]*$/u, "Local filesystem paths cannot contain NUL"); -/** A selected local filesystem path: nonempty, NUL-free, and representable without replacement. */ -export const localPathStringSchema = nulFreeStringSchema - .min(1) - .regex( - /^[^\uD800-\uDFFF]*$/u, - "Local filesystem paths must contain well-formed Unicode", - ); +/** + * A selected local filesystem path: nonempty, NUL-free, and representable + * without replacement. The well-formed Unicode rule is enforced but not + * advertised; see `rejectUnpairedSurrogates`. + */ +export const localPathStringSchema = rejectUnpairedSurrogates( + nulFreeStringSchema.min(1), + "Local filesystem paths must contain well-formed Unicode", +); /** * A Windows absolute path: rooted, UNC, or drive-qualified. The classes spell diff --git a/src/domain/managed/managedMemberSignatureIdentity.test.ts b/src/domain/managed/managedMemberSignatureIdentity.test.ts index a07c2c1c1c..f14391e6e6 100644 --- a/src/domain/managed/managedMemberSignatureIdentity.test.ts +++ b/src/domain/managed/managedMemberSignatureIdentity.test.ts @@ -16,12 +16,15 @@ describe("managed signature identity", () => { ); }); - it("matches resolved digests and raw digests among themselves", () => { + it("keeps different raw digests distinct when resolution is unavailable", () => { + expect(signatureIdentity(signature(digest, null))).not.toBe( + signatureIdentity(signature("cd".repeat(32), null)), + ); + }); + + it("matches resolved digests despite different raw digests", () => { expect(signatureIdentity(signature("01".repeat(32), digest))).toBe( signatureIdentity(signature("02".repeat(32), digest)), ); - expect(signatureIdentity(signature(digest, null))).toBe( - signatureIdentity(signature(digest, null)), - ); }); }); diff --git a/src/domain/managed/managedNativeVerificationMatch.ts b/src/domain/managed/managedNativeVerificationMatch.ts index 1e5dd83724..063c5a067e 100644 --- a/src/domain/managed/managedNativeVerificationMatch.ts +++ b/src/domain/managed/managedNativeVerificationMatch.ts @@ -45,7 +45,7 @@ export const collectNativeSymbols = ( unsupported += 1; } else { accepted += 1; - symbols.push(...observation.symbols); + for (const symbol of observation.symbols) symbols.push(symbol); if (observation.exportCoverage !== undefined) { if (!observation.exportCoverage.exhaustive) partialExports = true; for (const limitation of observation.exportCoverage.limitations) diff --git a/src/domain/native/binaryLayout.test.ts b/src/domain/native/binaryLayout.test.ts index dbd086c7ad..c5a030c66e 100644 --- a/src/domain/native/binaryLayout.test.ts +++ b/src/domain/native/binaryLayout.test.ts @@ -91,7 +91,7 @@ it.each(["mutable", "shallow-frozen"])( }, ); -it("never treats sealing as format validity or caches a refused layout", () => { +it("does not treat an invalid sealed layout as valid on repeated reads", () => { const value = report(); value.sections[0]!.index = 10; freezeJsonSnapshot(value); diff --git a/src/domain/native/nativeDeadlines.ts b/src/domain/native/nativeDeadlines.ts index 856b524b51..472396b1a1 100644 --- a/src/domain/native/nativeDeadlines.ts +++ b/src/domain/native/nativeDeadlines.ts @@ -51,7 +51,7 @@ export const pwntoolsTimeoutField = deadlineField( "Deadline in milliseconds for each owned pwntools command in this call. Omitted means 30000, range 1..2147483647; applies on the next call. Independent CPU, output, diagnostic, file and address-space allowances are selected in producer_limits. input_bytes overrides REA_PWNTOOLS_MAX_INPUT_BYTES; otherwise that launch default applies.", ); -export type NativeDeadlineRead = +type NativeDeadlineRead = | { readonly ok: true; readonly timeoutMs: number } | { readonly ok: false; diff --git a/src/domain/native/nativeInitializers.test.ts b/src/domain/native/nativeInitializers.test.ts index 873f3189a8..c3645760b8 100644 --- a/src/domain/native/nativeInitializers.test.ts +++ b/src/domain/native/nativeInitializers.test.ts @@ -1,6 +1,5 @@ import { expect, it } from "vitest"; import { - DEFAULT_NATIVE_INITIALIZER_PAGE, inspectNativeInitializersInputSchema, nativeInitializersSchema, NATIVE_INITIALIZER_LIMITATIONS, @@ -11,7 +10,7 @@ it("applies paging defaults and rejects relative paths and extra arguments", () inspectNativeInitializersInputSchema.parse({ path: "/artifacts/app" }), ).toMatchObject({ offset: 0, - limit: DEFAULT_NATIVE_INITIALIZER_PAGE, + limit: 64, max_file_bytes: 67108864, }); for (const input of [ diff --git a/src/domain/native/nativeInitializers.ts b/src/domain/native/nativeInitializers.ts index d6c5be306c..9813b10459 100644 --- a/src/domain/native/nativeInitializers.ts +++ b/src/domain/native/nativeInitializers.ts @@ -13,7 +13,7 @@ export const MAX_INSPECT_NATIVE_INITIALIZERS_FILE_BYTES = 536870912; /** Slots collected before the inventory is reported incomplete. */ export const MAX_NATIVE_INITIALIZER_ENTRIES = 65536; /** Default page size for returned entries. */ -export const DEFAULT_NATIVE_INITIALIZER_PAGE = 64; +const DEFAULT_NATIVE_INITIALIZER_PAGE = 64; /** Upper bound on one returned page. */ export const MAX_NATIVE_INITIALIZER_PAGE = 4096; /** Bytes admitted for one symbol or section name. */ @@ -24,7 +24,7 @@ export const MAX_NATIVE_INITIALIZER_NOTES = 24; * Upper bound on reported limitations: the standing ones, the note budget, a * line per tallied reason, an omission count and a withheld-name count. */ -export const MAX_NATIVE_INITIALIZER_LIMITATIONS = 40; +const MAX_NATIVE_INITIALIZER_LIMITATIONS = 40; const uint = z.number().int().min(0).max(0xffffffff); const safeNonNegative = z.number().int().min(0).max(Number.MAX_SAFE_INTEGER); diff --git a/src/domain/process/processCapture.ts b/src/domain/process/processCapture.ts index 642ec3a006..4d7b1478a5 100644 --- a/src/domain/process/processCapture.ts +++ b/src/domain/process/processCapture.ts @@ -191,7 +191,7 @@ export type RecordProcessCaptureEvent = ( index: number, ) => void; -/** Observed process-tree settlement and the cleanup required by that state. */ +/** Observed process-tree settlement and subsequent owned-resource cleanup. */ export type ProcessSettlement = | VerifiedProcessSettlement | { @@ -204,7 +204,7 @@ export type VerifiedProcessSettlement = | { readonly state: "quiesced"; readonly elapsed_ms: number; - readonly cleanup_outcome: "not_required"; + readonly cleanup_outcome: "not_required" | "cleaned"; } | { readonly state: "alive_at_deadline" | "unverifiable"; @@ -353,6 +353,10 @@ export interface IncompleteProcessCaptureObservations { readonly manifest: PartialProcessObservationField< UnverifiedProcessCapture["manifest"] >; + /** Facts retained after completion failed; historical records may omit them. */ + readonly residual_unknowns?: + | UnverifiedProcessCapture["residual_unknowns"] + | undefined; } const fileStateSchema = z.discriminatedUnion("type", [ @@ -429,7 +433,7 @@ const processSettlementSchema = z.discriminatedUnion("state", [ z.object({ state: z.literal("quiesced"), elapsed_ms: z.int().nonnegative(), - cleanup_outcome: z.enum(["not_required", "failed"]), + cleanup_outcome: z.enum(["not_required", "cleaned", "failed"]), }), z.object({ state: z.enum(["alive_at_deadline", "unverifiable"]), @@ -540,7 +544,7 @@ const processCaptureShapeSchema = z.strictObject({ z.object({ state: z.literal("quiesced"), elapsed_ms: z.int().nonnegative(), - cleanup_outcome: z.literal("not_required"), + cleanup_outcome: z.enum(["not_required", "cleaned"]), }), z.object({ state: z.enum(["alive_at_deadline", "unverifiable"]), @@ -655,6 +659,8 @@ const incompleteProcessCaptureObservationsSchema = z.strictObject({ manifest: partialObservationFieldSchema( processCaptureShapeSchema.shape.manifest, ), + residual_unknowns: + processCaptureShapeSchema.shape.residual_unknowns.optional(), }); const partialCleanupReportSchema = z.strictObject({ @@ -799,7 +805,10 @@ export const partialProcessCaptureObservationSchema = z capture.settlement.state === "quiesced" ? { ...capture.settlement, - cleanup_outcome: "not_required" as const, + cleanup_outcome: + capture.settlement.cleanup_outcome === "cleaned" + ? ("cleaned" as const) + : ("not_required" as const), } : { ...capture.settlement, cleanup_outcome: "cleaned" as const }; const validationCapture: UnverifiedProcessCapture = { diff --git a/src/domain/process/processCapture.validation.test.ts b/src/domain/process/processCapture.validation.test.ts index 1c815899cd..5a21323b1e 100644 --- a/src/domain/process/processCapture.validation.test.ts +++ b/src/domain/process/processCapture.validation.test.ts @@ -514,9 +514,9 @@ it("does not infer normalized retention budget from missing scenario defaults", ).toContain("manifest.scenario.limits.output_bytes"); }); -it("rejects settlement and cleanup combinations that cannot occur", () => { +it("accepts post-quiescence cleanup and rejects unverified settlement combinations", () => { const capture = emptyCapture(); - expect(() => + expect( parseProcessCapture({ ...capture, settlement: { @@ -525,7 +525,9 @@ it("rejects settlement and cleanup combinations that cannot occur", () => { cleanup_outcome: "cleaned", }, }), - ).toThrow("cleanup_outcome"); + ).toMatchObject({ + settlement: { state: "quiesced", cleanup_outcome: "cleaned" }, + }); expect(() => parseProcessCapture({ ...capture, @@ -1248,7 +1250,7 @@ it("rejects old executable identity representations without migration", () => { ).toThrow("legacy_executable_sha256"); }); -it("requires compatible contracts and enforces capture age through a clock seam", () => { +it("requires compatible contracts and enforces capture age", () => { const capture = emptyCapture(); expect(() => compareProcessCaptures(capture, { @@ -1274,11 +1276,17 @@ it("requires compatible contracts and enforces capture age through a clock seam" ], }), ); + const completedAt = new Date(Date.now() - 86_400_000).toISOString(); + const stale = { + ...capture, + manifest: { + ...capture.manifest, + started_at: completedAt, + completed_at: completedAt, + }, + }; expect(() => - compareProcessCaptures(capture, capture, { - maxCaptureAgeMs: 1, - now: () => Date.parse("2026-01-01T00:00:01.000Z"), - }), + compareProcessCaptures(stale, stale, { maxCaptureAgeMs: 1_000 }), ).toThrow( expect.objectContaining({ issues: [ diff --git a/src/domain/process/processComparison.ts b/src/domain/process/processComparison.ts index 3547a33190..3a4cf8295c 100644 --- a/src/domain/process/processComparison.ts +++ b/src/domain/process/processComparison.ts @@ -46,10 +46,9 @@ export const PROCESS_COMPARISON_DIMENSIONS = [ export type ProcessComparisonDimension = (typeof PROCESS_COMPARISON_DIMENSIONS)[number]; -/** Caller-selected comparison policy; the clock is not persisted as policy. */ +/** Caller-selected comparison policy. */ export interface ProcessCaptureComparisonOptions { readonly maxCaptureAgeMs?: number; - readonly now?: () => number; readonly traceSpecification?: ProcessTraceSpecification; } @@ -479,7 +478,7 @@ const explicitlyTimedSources = ( const assertComparable = ( left: ProcessCapture, right: ProcessCapture, - options: { readonly maxCaptureAgeMs?: number; readonly now?: () => number }, + options: { readonly maxCaptureAgeMs?: number }, ): void => { if ( left.manifest.comparison_contract_sha256 !== @@ -500,7 +499,7 @@ const assertComparable = ( } if (options.maxCaptureAgeMs === undefined) return; const maxCaptureAgeMs = options.maxCaptureAgeMs; - const now = (options.now ?? Date.now)(); + const now = Date.now(); const stale = ( [ ["left", left], diff --git a/src/domain/propertyListOpenStep.test.ts b/src/domain/propertyListOpenStep.test.ts index 6bf1066c24..6bf80f4721 100644 --- a/src/domain/propertyListOpenStep.test.ts +++ b/src/domain/propertyListOpenStep.test.ts @@ -3,8 +3,8 @@ import { parseOpenStep } from "plist"; import { decodeOpenStepPropertyList, - OpenStepPropertyListError, startsOpenStepPropertyList, + type OpenStepPropertyListCode, } from "./propertyListOpenStep.js"; it.each([ @@ -30,7 +30,7 @@ const syntaxFailure = (read: () => unknown): Error => { }; const containerRefusal = ( - code: OpenStepPropertyListError["code"], + code: OpenStepPropertyListCode, position: number, ) => ({ code, position }); @@ -75,7 +75,7 @@ it.each([ for (const text of [prefix, prefix + "(".repeat(130)]) { const pinned = syntaxFailure(() => parseOpenStep(text)); const owned = syntaxFailure(() => decodeOpenStepPropertyList(text)); - expect(owned).not.toBeInstanceOf(OpenStepPropertyListError); + expect(owned).not.toHaveProperty("_tag", "OpenStepPropertyListError"); expect(owned.constructor).toBe(pinned.constructor); expect(owned.message).toBe(pinned.message); } @@ -156,8 +156,10 @@ it("does not treat nonterminating whitespace or quoted comment text as line ends const error = syntaxFailure(() => decodeOpenStepPropertyList("// comment without a line end"), ); - expect(error).toBeInstanceOf(OpenStepPropertyListError); - expect(error).toMatchObject({ code: "unexpected_end" }); + expect(error).toMatchObject({ + _tag: "OpenStepPropertyListError", + code: "unexpected_end", + }); }); it.each([ diff --git a/src/domain/propertyListOpenStep.ts b/src/domain/propertyListOpenStep.ts index eabf0943ef..535975fae5 100644 --- a/src/domain/propertyListOpenStep.ts +++ b/src/domain/propertyListOpenStep.ts @@ -169,7 +169,7 @@ export const decodeOpenStepQuotedString = (token: string): string => { export type OpenStepPropertyListCode = (typeof OPEN_STEP_SYNTAX_CODES)[number]; /** OpenStep syntax refusal. `code` is the contract; `position` is the source offset. */ -export class OpenStepPropertyListError extends Error { +class OpenStepPropertyListError extends Error { readonly _tag = "OpenStepPropertyListError"; constructor( diff --git a/src/domain/propertyListXmlSource.ts b/src/domain/propertyListXmlSource.ts index db98ba434c..0ed1550649 100644 --- a/src/domain/propertyListXmlSource.ts +++ b/src/domain/propertyListXmlSource.ts @@ -13,7 +13,7 @@ interface ScalarSpan { readonly selfClosing: boolean; } -export class XmlPlistSourceError extends TypeError { +class XmlPlistSourceError extends TypeError { readonly _tag = "XmlPlistSourceError"; constructor() { super("Cannot locate XML plist scalar in its original source"); diff --git a/src/domain/reconstructionVerification.test.ts b/src/domain/reconstructionVerification.test.ts index 0103b7bf1d..cdef927ba9 100644 --- a/src/domain/reconstructionVerification.test.ts +++ b/src/domain/reconstructionVerification.test.ts @@ -2,13 +2,21 @@ import { describe, expect, it } from "vitest"; import { createEvidence, type Evidence } from "./evidence.js"; import { AnalysisInputError } from "./analysisErrorCore.js"; +import { compareArtifacts } from "./artifactComparison.js"; +import { readComparisonResult } from "./comparisonEvidenceResult.js"; +import { artifactInventoryResultSchema } from "./artifactGraph.js"; +import { artifactGraphDigest, artifactManifestId } from "./artifactIdentity.js"; import { createEvidenceBundle } from "./evidenceBundle.js"; import { EvidenceIntegrityError } from "./evidenceErrors.js"; import { reconstructionVerificationResultSchema, reconstructionSpecificationSchema, } from "./reconstructionVerificationSchemas.js"; -import type { JsonValue } from "./jsonValue.js"; +import { + jsonObjectSchema, + jsonValueSchema, + type JsonValue, +} from "./jsonValue.js"; import { EMPTY_PROCESS_CAPTURE_EXAMPLE } from "./process/processCaptureExample.js"; import { ARTIFACT_COMPARISON_EXAMPLE } from "../contracts/artifactComparisonExample.js"; import { createResidualUnknown } from "./residualUnknown.js"; @@ -196,6 +204,7 @@ const artifactComparison = ( left: Evidence, right: Evidence, partial = false, + result?: JsonValue, ): Evidence => createEvidence( undefined, @@ -211,13 +220,43 @@ const artifactComparison = ( left_evidence_ids: [left.evidence_id], right_evidence_ids: [right.evidence_id], }, - result: artifactResult(partial, [left.evidence_id, right.evidence_id]), + result: result ?? { + ...(artifactResult(partial, [ + left.evidence_id, + right.evidence_id, + ]) as Record), + left_manifest_id: compareArtifacts(left, right).left_manifest_id, + right_manifest_id: compareArtifacts(left, right).right_manifest_id, + }, confidence: "derived", authority: "analyst-inference", evidenceLinks: [left.evidence_id, right.evidence_id], }, ); +const verifyArtifactComparison = ( + left: Evidence, + right: Evidence, + result: JsonValue, +) => { + const comparison = artifactComparison(left, right, false, result); + return verifyReconstruction( + { + name: "Artifact structure", + claims: [ + { + kind: "structural-artifact", + claim_id: "artifact-graph", + title: "Artifact graph remains equal", + comparison_evidence_id: comparison.evidence_id, + dimension: "overall", + }, + ], + }, + createEvidenceBundle([left, right, comparison]), + ); +}; + describe("reconstruction verification", () => { it("accepts real process dimensions and rejects removed protocol dimensions", () => { const left = source("1", "controlled-replay"); @@ -423,6 +462,45 @@ describe("reconstruction verification outcomes", () => { }); }); + it("rejects an unchanged artifact aggregate that disagrees with retained changes", () => { + const left = ARTIFACT_COMPARISON_EXAMPLE.left; + const right = ARTIFACT_COMPARISON_EXAMPLE.right; + const changed = compareArtifacts(left, right); + expect(changed.status).toBe("changed"); + expect(changed.changes).toHaveLength(1); + expect(changed.changes[0]?.classification).toBe("changed"); + const inconsistentResults = [ + jsonValueSchema.parse({ ...changed, status: "unchanged" }), + jsonValueSchema.parse({ ...changed, status: "unchanged", changes: [] }), + jsonValueSchema.parse({ + ...jsonObjectSchema.parse(artifactResult(false)), + integrity_mismatch_evidence_ids: [left.evidence_id], + }), + ]; + + for (const result of inconsistentResults) + expect(() => verifyArtifactComparison(left, right, result)).toThrow( + EvidenceIntegrityError, + ); + }); + + it("preserves unknown artifact rows with known dimensions and validates integrity citations", () => { + const left = ARTIFACT_COMPARISON_EXAMPLE.left; + const right = ARTIFACT_COMPARISON_EXAMPLE.right; + const result = jsonObjectSchema.parse( + artifactResult(true, [left.evidence_id, right.evidence_id]), + ); + result.status = "unknown"; + const rows = result.changes as Array>; + rows[0]!.dimensions = ["availability", "metadata", "executable"]; + const valid = artifactComparison(left, right, false, result); + expect(readComparisonResult(valid, "compare_artifacts").ok).toBe(true); + result.status = "contradiction"; + result.integrity_mismatch_evidence_ids = [`ev_${"f".repeat(64)}`]; + const unlinked = artifactComparison(left, right, false, result); + expect(readComparisonResult(unlinked, "compare_artifacts").ok).toBe(false); + }); + it("keeps insufficient authority and partial structural evidence unknown", () => { const weakLeft = source("1", "controlled-replay", "derived"); const replayRight = source("2", "controlled-replay"); @@ -720,3 +798,181 @@ describe("retained reconstruction source coverage", () => { }, ); }); + +it("withholds unchanged when source inventory coverage is incomplete", () => { + const original = ARTIFACT_COMPARISON_EXAMPLE.left; + const data = { + ...(original.normalized_result as Record), + coverage: { scope: "root", status: "unknown" }, + }; + const left = createEvidence( + { path: "fixture-left", sha256: "0".repeat(64), format: "file" }, + original.provider, + { + operation: "inventory_artifact", + parameters: {}, + result: data, + confidence: "observed", + authority: "shipped-artifact", + }, + ); + const right = ARTIFACT_COMPARISON_EXAMPLE.right; + const real = compareArtifacts(left, right); + expect(real.status).toBe("unknown"); + expect( + verifyArtifactComparison(left, right, jsonValueSchema.parse(real)).status, + ).toBe("unknown"); + expect(real.changes.some((row) => row.classification === "changed")).toBe( + true, + ); + expect( + verifyArtifactComparison( + left, + right, + jsonValueSchema.parse({ ...real, status: "changed" }), + ).status, + ).toBe("unknown"); + const fake = jsonValueSchema.parse({ + ...real, + status: "unchanged", + changes: [], + summary: { + unchanged: 1, + added: 0, + removed: 0, + changed: 0, + unknown: 0, + contradiction: 0, + }, + }); + const result = verifyArtifactComparison(left, right, fake); + expect(result.status).toBe("unknown"); +}); + +it.each(["unavailable", "mismatched"] as const)( + "withholds unchanged when source content identity is %s", + (hashStatus) => { + const original = ARTIFACT_COMPARISON_EXAMPLE.left; + const value = artifactInventoryResultSchema.parse( + original.normalized_result, + ); + value.occurrences = value.occurrences.map((row) => ({ + ...row, + hash_status: hashStatus, + })); + const digest = artifactGraphDigest({ + nodes: value.nodes, + occurrences: value.occurrences, + edges: value.edges, + contradictions: value.integrity_contradictions, + }); + value.manifest.graph_sha256 = digest; + value.manifest.manifest_id = artifactManifestId( + value.manifest.root_artifact_id, + digest, + ); + const observe = (path: string) => + createEvidence( + { path, sha256: "0".repeat(64), format: "file" }, + original.provider, + { + operation: "inventory_artifact", + parameters: { path }, + result: value, + confidence: "observed", + authority: "shipped-artifact", + }, + ); + const left = observe("fixture-left"), + right = observe("fixture-right"); + const actual = compareArtifacts(left, right); + expect(actual.status).not.toBe("unchanged"); + const claimed = jsonValueSchema.parse({ + ...actual, + status: "unchanged", + changes: [], + integrity_mismatch_evidence_ids: [], + summary: { + unchanged: 1, + added: 0, + removed: 0, + changed: 0, + unknown: 0, + contradiction: 0, + }, + }); + expect(verifyArtifactComparison(left, right, claimed).status).toBe( + "unknown", + ); + }, +); + +it("passes independently sourced, fully corroborated unchanged inventories", () => { + const left = ARTIFACT_COMPARISON_EXAMPLE.left; + const right = createEvidence( + { path: "independent-copy", sha256: "0".repeat(64), format: "file" }, + left.provider, + { + operation: "inventory_artifact", + parameters: { path: "independent-copy" }, + result: left.normalized_result, + confidence: "observed", + authority: "shipped-artifact", + }, + ); + const actual = compareArtifacts(left, right); + expect(actual.status).toBe("unchanged"); + expect( + verifyArtifactComparison(left, right, jsonValueSchema.parse(actual)).status, + ).toBe("pass"); + const changed = compareArtifacts(left, ARTIFACT_COMPARISON_EXAMPLE.right); + const inconsistent = jsonValueSchema.parse({ + ...changed, + left_manifest_id: actual.left_manifest_id, + right_manifest_id: actual.right_manifest_id, + changes: changed.changes.map((row) => ({ + ...row, + evidence_links: [left.evidence_id, right.evidence_id], + })), + }); + expect(verifyArtifactComparison(left, right, inconsistent).status).toBe( + "unknown", + ); + expect( + verifyArtifactComparison( + left, + ARTIFACT_COMPARISON_EXAMPLE.right, + jsonValueSchema.parse(changed), + ).status, + ).toBe("fail"); +}); + +it.each(["unchanged", "unknown", "changed"] as const)( + "rejects incorrect source manifest identity on %s claims", + (status) => { + const left = ARTIFACT_COMPARISON_EXAMPLE.left, + right = ARTIFACT_COMPARISON_EXAMPLE.right; + const actual = compareArtifacts(left, right); + const claimed = jsonValueSchema.parse({ + ...actual, + status, + left_manifest_id: `agm_${"f".repeat(64)}`, + ...(status === "unchanged" + ? { + changes: [], + summary: { + unchanged: 1, + added: 0, + removed: 0, + changed: 0, + unknown: 0, + contradiction: 0, + }, + } + : {}), + }); + expect(() => verifyArtifactComparison(left, right, claimed)).toThrow( + EvidenceIntegrityError, + ); + }, +); diff --git a/src/domain/reconstructionVerification.ts b/src/domain/reconstructionVerification.ts index 85fe3a4503..6833adc419 100644 --- a/src/domain/reconstructionVerification.ts +++ b/src/domain/reconstructionVerification.ts @@ -2,6 +2,7 @@ import { ArtifactInventoryCarrierError, parseArtifactInventoryEvidence, } from "./artifactInventoryEvidence.js"; +import { compareArtifacts } from "./artifactComparison.js"; import { uniqueSorted } from "./canonicalOrdering.js"; import { compareUnicodeCodePoints } from "./unicodeCodePointOrder.js"; import { comparisonSourceEvidenceSides } from "./comparisonSourceEvidence.js"; @@ -62,6 +63,11 @@ interface ValidatedSourceFacts { }[]; } +type ArtifactReplay = Pick< + ReturnType, + "status" | "left_manifest_id" | "right_manifest_id" +>; + /** Verify only explicitly declared claims; no global equivalence is inferred. */ export const verifyReconstruction = ( specificationInput: unknown, @@ -87,8 +93,11 @@ export const verifyReconstruction = ( bundle.records.map((item) => [item.evidence_id, item]), ); const heads = reconstructionUnknownHeads(bundle.unknowns); + const artifactReplays = new Map(); const results = specification.claims - .map((claim, index) => evaluateClaim(claim, index, records, heads)) + .map((claim, index) => + evaluateClaim(claim, index, records, heads, artifactReplays), + ) .sort((left, right) => compareUnicodeCodePoints(left.claim_id, right.claim_id), ); @@ -134,6 +143,7 @@ const evaluateClaim = ( claimIndex: number, records: ReadonlyMap, heads: ReadonlyMap, + artifactReplays: Map, ): ClaimResult => { const comparison = records.get(claim.comparison_evidence_id); if (comparison === undefined) @@ -173,6 +183,15 @@ const evaluateClaim = ( ...authorityLimitations(claim, sources, sides), ...callCoverage.blockers, ...sourceProcessCoverage(claim, sourceFacts.processes, observed), + ...(claim.kind === "structural-artifact" + ? assessArtifactSourceComparison( + sides, + records, + comparison, + observed, + artifactReplays, + ) + : []), ]; const relevantUnknowns = reconstructionClaimUnknowns( heads, @@ -204,6 +223,76 @@ const evaluateClaim = ( }); }; +const assessArtifactSourceComparison = ( + sides: { + readonly left: readonly string[]; + readonly right: readonly string[]; + }, + records: ReadonlyMap, + comparison: Evidence, + observed: ObservedStatus, + replays: Map, +): string[] => { + const parsed = readComparisonResult(comparison, "compare_artifacts"); + if (!parsed.ok) throw parsed.error; + let leftManifest: string; + let rightManifest: string; + let corroborated = true; + try { + const left = sides.left.map((id) => records.get(id)!); + const right = sides.right.map((id) => records.get(id)!); + if ( + observed === "unchanged" || + observed === "changed" || + observed === "added" || + observed === "removed" || + observed === "contradiction" + ) { + const key = JSON.stringify([sides.left, sides.right]); + let actual = replays.get(key); + if (actual === undefined) { + const result = compareArtifacts(left, right); + actual = { + status: result.status, + left_manifest_id: result.left_manifest_id, + right_manifest_id: result.right_manifest_id, + }; + replays.set(key, actual); + } + leftManifest = actual.left_manifest_id; + rightManifest = actual.right_manifest_id; + corroborated = actual.status === observed; + } else { + leftManifest = + parseArtifactInventoryEvidence(left).inventory.manifest.manifest_id; + rightManifest = + parseArtifactInventoryEvidence(right).inventory.manifest.manifest_id; + } + } catch (cause: unknown) { + if ( + !(cause instanceof AnalysisInputError) && + !(cause instanceof ArtifactInventoryCarrierError) + ) + throw cause; + throw new EvidenceIntegrityError(cause.message, { + cause, + userMessage: cause.message, + }); + } + if ( + parsed.value.result.left_manifest_id !== leftManifest || + parsed.value.result.right_manifest_id !== rightManifest + ) + throw new EvidenceIntegrityError( + "Artifact comparison manifest identities disagree with its sources", + ); + return corroborated + ? [] + : [ + "Artifact source inventories do not corroborate the claimed comparison status.", + ]; +}; + const readValidatedSourceFacts = ( claim: Claim, sources: readonly Evidence[], diff --git a/src/domain/referenceSourceGraph.test.ts b/src/domain/referenceSourceGraph.test.ts index 39cc38dd5e..80a3c7dc61 100644 --- a/src/domain/referenceSourceGraph.test.ts +++ b/src/domain/referenceSourceGraph.test.ts @@ -8,7 +8,6 @@ import { createHistoricalSourceManifest, historicalSourceGraphSchema, historicalSourceParseFailureKey, - parseHistoricalSourceGraph, type HistoricalSourceGraphInput, } from "./referenceSourceGraph.js"; @@ -129,7 +128,7 @@ const registerSourceCommitmentTests = (): void => { manifest_id: `hsm_${createHash("sha256").update(semanticJson).digest("hex")}`, }); expect(first).toEqual(second); - expect(parseHistoricalSourceGraph(graph)).toEqual(graph); + expect(historicalSourceGraphSchema.parse(graph)).toEqual(graph); expect(() => createHistoricalSourceGraph({ ...graphInput(), @@ -145,7 +144,7 @@ const registerSourceCommitmentTests = (): void => { it("rejects stale roots and commits file, directory, and symlink semantics", () => { const original = createHistoricalSourceGraph(graphInput()); expect(() => - parseHistoricalSourceGraph({ ...original, root_sha256: HASH_A }), + historicalSourceGraphSchema.parse({ ...original, root_sha256: HASH_A }), ).toThrow(/root commitment/u); for (const mutate of [ @@ -214,7 +213,7 @@ describe("historical source graph", () => { manifests: paths, }; const graph = createHistoricalSourceGraph(input); - expect(parseHistoricalSourceGraph(graph)).toEqual(graph); + expect(historicalSourceGraphSchema.parse(graph)).toEqual(graph); expect(() => createHistoricalSourceGraph({ ...input, diff --git a/src/domain/referenceSourceGraph.ts b/src/domain/referenceSourceGraph.ts index 4a901da252..6a3495e147 100644 --- a/src/domain/referenceSourceGraph.ts +++ b/src/domain/referenceSourceGraph.ts @@ -9,7 +9,7 @@ import { isPortableAbsoluteReferenceTarget, } from "./referenceSourcePathIdentity.js"; import { compareUnicodeCodePoints } from "./unicodeCodePointOrder.js"; -import { digestSchema, prefixedDigestSchema } from "./../domain/digests.js"; +import { digestSchema, prefixedDigestSchema } from "./digests.js"; const boundedTextSchema = z.string().min(1); const relativePathSchema = z .string() @@ -566,11 +566,6 @@ export const createHistoricalSourceGraph = ( }); }; -/** Parse a graph and reject stale or caller-invented root commitments. */ -export const parseHistoricalSourceGraph = ( - input: unknown, -): HistoricalSourceGraph => historicalSourceGraphSchema.parse(input); - /** Commit semantic graph facts while retaining host-local diagnostic metadata separately. */ const computeHistoricalSourceGraphSha256 = ( graph: HistoricalSourceGraph, @@ -589,7 +584,7 @@ const computeHistoricalSourceGraphSha256 = ( export const createHistoricalSourceManifest = ( input: unknown, ): HistoricalSourceManifest => { - const graph = parseHistoricalSourceGraph(input); + const graph = historicalSourceGraphSchema.parse(input); const semantic = { schema: "HistoricalSourceManifest" as const, authority: "historical-reference" as const, diff --git a/src/domain/releaseExposure.test.ts b/src/domain/releaseExposure.test.ts index f4cfbf56e0..53aa16f93a 100644 --- a/src/domain/releaseExposure.test.ts +++ b/src/domain/releaseExposure.test.ts @@ -4,8 +4,7 @@ import { assembleReleaseExposureReport, exposureMeetsFailOn, PACKAGED_NATIVE_IMAGES, - RELEASE_EXPOSURE_CHECK_IDS, - RELEASE_EXPOSURE_LIMITATION, + type ReleaseExposureCheckId, type ReleaseExposureOwnerFacts, type ReleaseExposureScan, } from "./releaseExposure.js"; @@ -64,16 +63,32 @@ const reportFor = ( const statusOf = ( report: ReturnType, - id: (typeof RELEASE_EXPOSURE_CHECK_IDS)[number], + id: ReleaseExposureCheckId, ) => report.checks.find((check) => check.id === id)?.status; describe("release exposure checks", () => { it("keeps the fixed check order and the security limitation", () => { const report = reportFor(); expect(report.checks.map((check) => check.id)).toEqual([ - ...RELEASE_EXPOSURE_CHECK_IDS, + "javascript.source_map_sources", + "javascript.source_maps", + "electron.run_as_node", + "electron.asar_integrity", + "electron.only_load_from_asar", + "electron.node_cli_inspect", + "packaging.extractable_payload", + "native.debug_companions", + "native.pdb_path", + "go.build_paths", + "rust.dependency_metadata", + "text.credential_markers", + "native.hardening", + "macho.hardened_runtime", + "android.manifest", ]); - expect(report.limitations[0]).toBe(RELEASE_EXPOSURE_LIMITATION); + expect(report.limitations[0]).toMatch( + /static analysis.*does not establish.*secure/u, + ); expect(report.summary.no_exposure_observed_in_assessed_checks).toBe(true); expect(report.summary.by_status.not_assessed).toBe(3); expect(report.summary.highest_exposed_severity).toBeNull(); diff --git a/src/domain/releaseExposure.ts b/src/domain/releaseExposure.ts index 160e26022e..bf80520679 100644 --- a/src/domain/releaseExposure.ts +++ b/src/domain/releaseExposure.ts @@ -38,7 +38,7 @@ export const MAX_RELEASE_EXPOSURE_SAMPLES = 8; export const RELEASE_EXPOSURE_OPERATION = "audit_release_exposure"; -export const RELEASE_EXPOSURE_LIMITATION = +const RELEASE_EXPOSURE_LIMITATION = "This report states what static analysis of the selected artifact recovered. It does not establish that the application is secure."; export const RELEASE_EXPOSURE_ALLOWANCES = [ @@ -67,7 +67,7 @@ export const RELEASE_EXPOSURE_SEVERITIES = [ ] as const; export const RELEASE_EXPOSURE_FAIL_LEVELS = ["high", "medium", "low"] as const; -export const RELEASE_EXPOSURE_CHECK_IDS = [ +const RELEASE_EXPOSURE_CHECK_IDS = [ "javascript.source_map_sources", "javascript.source_maps", "electron.run_as_node", @@ -733,10 +733,7 @@ const metadataStatus = ( images.some((image) => image.kind === "failed" || image.kind === "absent") ) return "unknown"; - if ( - images.length === 0 || - images.every((image) => image.kind === "unsupported") - ) + if (images.every((image) => image.kind === "unsupported")) return "not_applicable"; return "not_observed"; }; diff --git a/src/domain/reverseEngineering.ts b/src/domain/reverseEngineering.ts index db0420d4a3..f21d0c8558 100644 --- a/src/domain/reverseEngineering.ts +++ b/src/domain/reverseEngineering.ts @@ -3,6 +3,7 @@ import { constants as bufferConstants } from "node:buffer"; import { commandEvidenceDetailSchema } from "./commandObservation.js"; import { localPathStringSchema } from "./localPath.js"; import { RAW_BINARY_ENTRY_POINT } from "./rawBinary.js"; +import { rejectUnpairedSurrogates } from "./stringPatterns.js"; /** Omitted one-shot objdump/rizin wall clock. Preflight shares it; the command gets it again. */ export const DEFAULT_REVERSE_COMMAND_TIMEOUT_MS = 120_000; @@ -62,15 +63,13 @@ const objdumpArtifactInputSchema = z const objdumpDisassemblyInputSchema = objdumpArtifactInputSchema.extend({ operation: z.literal("disassemble"), - symbol: z - .string() - .min(1) - .regex(/^[^\x00]*$/u) - // Keep UTF-16 checks out of patterns consumed by scalar Unicode engines. - .refine( - (symbol) => !/[\uD800-\uDFFF]/u.test(symbol), - "Symbol must be well-formed Unicode.", - ) + symbol: rejectUnpairedSurrogates( + z + .string() + .min(1) + .regex(/^[^\x00]*$/u), + "Symbol must be well-formed Unicode.", + ) .describe( "Native symbol to disassemble, expressed as well-formed Unicode. Matching and symbol boundaries follow the selected executable; omitted means all code sections.", ) diff --git a/src/domain/staticRuntimeCorrelation.test.ts b/src/domain/staticRuntimeCorrelation.test.ts index 75c35ab3bb..ba307e120d 100644 --- a/src/domain/staticRuntimeCorrelation.test.ts +++ b/src/domain/staticRuntimeCorrelation.test.ts @@ -1,9 +1,6 @@ import { describe, expect, it } from "vitest"; import { ARTIFACT_COMPARISON_EXAMPLE } from "../contracts/artifactComparisonExample.js"; -import { - INVESTIGATION_EXAMPLES, - FUNCTION_COMPARISON_EVIDENCE, -} from "../contracts/investigationExamples.js"; +import { FUNCTION_COMPARISON_EVIDENCE } from "../contracts/investigationExamples.js"; import { artifactComparisonResultSchema, compareArtifacts, diff --git a/src/domain/stringPatterns.test.ts b/src/domain/stringPatterns.test.ts index 4dd634d7a7..e96d87dd29 100644 --- a/src/domain/stringPatterns.test.ts +++ b/src/domain/stringPatterns.test.ts @@ -12,6 +12,7 @@ import { canonicalBase64Schema, isCanonicalBase64, isPaddedBase64, + rejectUnpairedSurrogates, } from "./stringPatterns.js"; it("preserves identifier grammars under Unicode and Unicode-set compilation", () => { @@ -154,3 +155,22 @@ it("validates complete projected canonical schemas at the encoded-input budget", expect(validate(value)).toBe(expected); } }); + +it("rejects unpaired surrogates at runtime without advertising a pattern", () => { + const schema = rejectUnpairedSurrogates(z.string(), "Must be well-formed"); + for (const value of ["\ud800", "a\udc00", "\udc00\ud800", "\ud83d"]) + expect( + schema.safeParse(value).error?.issues, + JSON.stringify(value), + ).toEqual([ + { + code: "invalid_format", + format: "regex", + message: "Must be well-formed", + path: [], + }, + ]); + for (const value of ["", "plain", "\ud83d\ude00", "\ufffd", "\uffff"]) + expect(schema.safeParse(value).success, JSON.stringify(value)).toBe(true); + expect(z.toJSONSchema(schema, { io: "input" })).not.toHaveProperty("pattern"); +}); diff --git a/src/domain/stringPatterns.ts b/src/domain/stringPatterns.ts index b97d08e45c..71424570f0 100644 --- a/src/domain/stringPatterns.ts +++ b/src/domain/stringPatterns.ts @@ -17,6 +17,34 @@ export const INVALID_BASE64_PADDING_PATTERN = /(?:=[^=]|===)/; export const INVALID_CANONICAL_BASE64_PAD_BITS_PATTERN = /(?:[^AQgw]==$|[BCDFGHJKLNOPRSTVWXZabdefhijlmnpqrtuvxyz1235679+\x2f]=$)/; +// In `u` mode a well-formed surrogate pair is one code point outside this +// range, so the class matches only unpaired surrogates. +const UNPAIRED_SURROGATE_PATTERN = /[\uD800-\uDFFF]/u; + +/** + * Reject unpaired UTF-16 surrogates without advertising a pattern. + * + * ECMA-262 spells the surrogate block only as `\uD800-\uDFFF`. RE2 has no `\u` + * escape and Rust's `regex` and `jsonschema` crates refuse non-scalar code + * points, and a client that cannot compile one advertised pattern refuses the + * complete tools request rather than the affected call. The issue keeps the + * `invalid_format` regex shape an advertised `.regex` raises, so input issue + * projection still reports `message`. + */ +export const rejectUnpairedSurrogates = ( + schema: Schema, + message: string, +): Schema => + schema.superRefine((value, context) => { + if (UNPAIRED_SURROGATE_PATTERN.test(value)) + context.addIssue({ + code: "invalid_format", + format: "regex", + message, + input: value, + }); + }); + export const isPaddedBase64 = (value: string): boolean => value.length % 4 === 0 && !INVALID_BASE64_CHARACTER_PATTERN.test(value) && diff --git a/src/domain/unicodeCodePointOrder.ts b/src/domain/unicodeCodePointOrder.ts index 4f635048d1..d0d2280ae7 100644 --- a/src/domain/unicodeCodePointOrder.ts +++ b/src/domain/unicodeCodePointOrder.ts @@ -18,9 +18,9 @@ export const compareUnicodeCodePoints = ( }; /** - * Collision-free composite key for deduplication/sorting. `\0`-joins - * collide when any component contains NUL (valid in JS property names); - * a JSON tuple escapes every component. Single owner for composite keys. + * JSON tuple key for deduplication/sorting. Unlike `\0`-joins, it preserves + * boundaries when string components contain NUL. Callers must account for JSON + * serialization semantics, such as undefined array elements becoming null. */ export const compositeKey = (parts: readonly unknown[]): string => JSON.stringify(parts); diff --git a/src/domain/webBundleAnalyzerInspection.ts b/src/domain/webBundleAnalyzerInspection.ts index af373a5cbc..30565524a9 100644 --- a/src/domain/webBundleAnalyzerInspection.ts +++ b/src/domain/webBundleAnalyzerInspection.ts @@ -24,7 +24,6 @@ import type { JavaScriptOpenReceiverFact } from "./javascript/javascriptSemantic import { isIntrinsicMapLookup } from "./javascript/javascriptSemanticMapOperations.js"; import { semanticStaticPropertyKey, - semanticStaticPropertyName, isNullishJavaScriptSyntax, staticObjectProperty, staticObjectPropertyNames, diff --git a/src/domain/webNetworkCapture.ts b/src/domain/webNetworkCapture.ts index ed4ed5cecc..1a7e9c5a28 100644 --- a/src/domain/webNetworkCapture.ts +++ b/src/domain/webNetworkCapture.ts @@ -33,7 +33,7 @@ const CAPTURE_REPORTED_RECORD_DEPTH = [ 0, "reported", ].length; -export const MAX_WEB_NETWORK_CAPTURE_NESTING_DEPTH = +const MAX_WEB_NETWORK_CAPTURE_NESTING_DEPTH = MAX_JSON_DEPTH - CAPTURE_REPORTED_RECORD_DEPTH; /** One admitted capture's complete-input, complete-reply and parser policy. */ diff --git a/src/domain/windows/ntfsMft.test.ts b/src/domain/windows/ntfsMft.test.ts index e5966a34a8..3e9695b970 100644 --- a/src/domain/windows/ntfsMft.test.ts +++ b/src/domain/windows/ntfsMft.test.ts @@ -1,13 +1,14 @@ import { describe, expect, it } from "vitest"; import { - MFT_DEFAULT_RECORD_BYTES, - MFT_MAX_FILE_BYTES, createMftDecoder, parseMftIndex, selectMftMetadata, } from "./ntfsMft.js"; +// The fixtures use the documented 1024-byte default, independently of the reader. +const MFT_DEFAULT_RECORD_BYTES = 1024; + interface RecordOptions { readonly sequenceNumber?: number; readonly hardLinkCount?: number; @@ -526,7 +527,7 @@ describe("parseMftIndex refusals and limits", () => { }); it("refuses a file above the fixed ceiling", () => { - const decoder = createMftDecoder(MFT_MAX_FILE_BYTES + 1); + const decoder = createMftDecoder(512 * 1024 * 1024 + 1); expect(decoder.ok).toBe(false); if (decoder.ok) return; expect(decoder.error).toMatchObject({ diff --git a/src/domain/windows/ntfsMft.ts b/src/domain/windows/ntfsMft.ts index 4e7306ff76..2cec504f92 100644 --- a/src/domain/windows/ntfsMft.ts +++ b/src/domain/windows/ntfsMft.ts @@ -27,7 +27,7 @@ const hasMftFileRecordSignature = (prefix: Uint8Array): boolean => MFT_FILE_RECORD_MAGIC; /** A record's declared size is a power of two from 512 upward; 1024 is NTFS's default. */ -export const MFT_DEFAULT_RECORD_BYTES = 1024; +const MFT_DEFAULT_RECORD_BYTES = 1024; const MFT_MIN_RECORD_BYTES = 512; const MFT_MAX_RECORD_BYTES = 65536; /** An update sequence array holds at least the sequence number and one fixup word. */ @@ -39,7 +39,7 @@ export const FIXUP_SECTOR_BYTES = 512; * is fetched, so this is deliberately * smaller than a volume's MFT can be. */ -export const MFT_MAX_FILE_BYTES = 512 * 1024 * 1024; +const MFT_MAX_FILE_BYTES = 512 * 1024 * 1024; /** Limit the inline header sample independently of full stream coverage. */ export const MFT_INLINE_HEADER_LIMIT = 32; diff --git a/src/domain/windowsNativeArtifact.ts b/src/domain/windowsNativeArtifact.ts index cdb943cbec..b1d301e8fe 100644 --- a/src/domain/windowsNativeArtifact.ts +++ b/src/domain/windowsNativeArtifact.ts @@ -18,7 +18,7 @@ export type WindowsNativeManifest = typeof WINDOWS_NATIVE_ARTIFACT & { readonly artifactSha256: string; }; -export class WindowsNativeArtifactError extends Error { +class WindowsNativeArtifactError extends Error { readonly _tag = "WindowsNativeArtifactError" as const; } diff --git a/src/domain/xmlDocument.ts b/src/domain/xmlDocument.ts index 44bb465dec..6958e6ef37 100644 --- a/src/domain/xmlDocument.ts +++ b/src/domain/xmlDocument.ts @@ -5,7 +5,7 @@ import { err, ok, type Result } from "./result.js"; /** XML 1.0 literal line endings; Unicode separators remain source characters. */ export const XML_LINE_ENDING = /\r\n?|\n/gu; -export class XmlDocumentParseError extends Error { +class XmlDocumentParseError extends Error { readonly _tag = "XmlDocumentParseError"; constructor( readonly reason: "malformed" | "doctype" | "internal-subset", diff --git a/src/dotnet/ManagedArtifactInspector.test.ts b/src/dotnet/ManagedArtifactInspector.test.ts index f3a7fd8fd9..5c877a1786 100644 --- a/src/dotnet/ManagedArtifactInspector.test.ts +++ b/src/dotnet/ManagedArtifactInspector.test.ts @@ -580,7 +580,7 @@ describe("managed artifact coded indexes", () => { (attribute) => attribute.type_name === "Fixture.LookupAttribute", ), ).toBe(true); - }, 30_000); + }); }); it.each([false, true])( diff --git a/src/electron/ElectronFuseCleanup.test.ts b/src/electron/ElectronFuseCleanup.test.ts index 5ef6b2811e..a9deb69f8c 100644 --- a/src/electron/ElectronFuseCleanup.test.ts +++ b/src/electron/ElectronFuseCleanup.test.ts @@ -514,12 +514,13 @@ it("joins shutdown during preflight and refuses any later launch or capture", as application_root: root, }; const running = provider.capture(input); - await entered.promise; let closed = false; - const closing = provider.close().then(() => { - closed = true; - }); + let closing: Promise | undefined; try { + await awaitAcknowledgement(entered.promise, running, "preflight read"); + closing = provider.close().then(() => { + closed = true; + }); await new Promise((resolve) => setImmediate(resolve)); expect(closed).toBe(false); release.resolve(); @@ -531,7 +532,8 @@ it("joins shutdown during preflight and refuses any later launch or capture", as } finally { release.resolve(); await running; - await closing; + if (closing !== undefined) await closing; + else await provider.close(); } }); diff --git a/src/electron/ElectronFuseProvider.test.ts b/src/electron/ElectronFuseProvider.test.ts index af7a2bfbda..acc2e625b3 100644 --- a/src/electron/ElectronFuseProvider.test.ts +++ b/src/electron/ElectronFuseProvider.test.ts @@ -28,7 +28,10 @@ import { import { ELECTRON_FUSE_STATE_BYTES } from "../domain/electron/electronFuses.js"; import { ElectronFuseProvider } from "./ElectronFuseProvider.js"; import { projectAnalysisError } from "../domain/analysisErrorProjection.js"; -import { readElectronFuseImage } from "./ElectronFuseReader.js"; +import { + ELECTRON_FUSE_LIMITS, + readElectronFuseImage, +} from "./ElectronFuseReader.js"; const provider = new ElectronFuseProvider(); @@ -292,18 +295,6 @@ describe("ELF embedded ASAR integrity applicability", () => { }); describe("Electron fuse reader resource boundaries", () => { - it("rejects invalid scan window sizes before allocating or scanning", async () => { - const root = await createTestTempDirectory( - "rea-electron-fuses-window-limit-", - ); - const path = join(root, "window"); - await writeFile(path, thinFuseImage(OBSIDIAN_FUSE_BYTES).bytes); - for (const chunkBytes of [0, -1, 1.5, 256 * 1024 + 1]) - await expect(readElectronFuseImage(path, { chunkBytes })).rejects.toThrow( - /chunkBytes must be an integer between/, - ); - }); - it("rejects overlapping universal Mach-O slices before repeated searching", async () => { const root = await createTestTempDirectory("rea-electron-fuses-overlap-"); const image = universalFuseImage().bytes; @@ -336,11 +327,15 @@ describe("Electron fuse path and container admission", () => { const root = await createTestTempDirectory("rea-electron-fuses-window-"); const image = thinFuseImage(OBSIDIAN_FUSE_BYTES); const path = join(root, "window"); - await writeFile(path, image.bytes); - const read = await readElectronFuseImage(path, { chunkBytes: 20 }); - expect(read.slices[0]?.sentinels[0]?.sentinel_offset).toBe( - image.sentinelOffset, + const sentinelOffset = ELECTRON_FUSE_LIMITS.chunkBytes - 8; + const bytes = Buffer.alloc( + sentinelOffset + image.bytes.length - image.sentinelOffset, ); + image.bytes.copy(bytes, 0, 0, image.sentinelOffset); + image.bytes.copy(bytes, sentinelOffset, image.sentinelOffset); + await writeFile(path, bytes); + const read = await readElectronFuseImage(path); + expect(read.slices[0]?.sentinels[0]?.sentinel_offset).toBe(sentinelOffset); expect(read.slices[0]?.sentinels[0]?.fuses).toHaveLength(8); }); diff --git a/src/electron/ElectronFuseReader.ts b/src/electron/ElectronFuseReader.ts index 8b719a39b6..a5eadc6062 100644 --- a/src/electron/ElectronFuseReader.ts +++ b/src/electron/ElectronFuseReader.ts @@ -89,7 +89,6 @@ export const readElectronFuseImage = async ( path: string, options?: { readonly signal?: AbortSignal; - readonly chunkBytes?: number; readonly selection?: ResolvedElectronFuseFile; readonly resources?: ArtifactResourceScope; }, @@ -141,15 +140,6 @@ export const readElectronFuseImage = async ( `Selected artifact exceeds ${ELECTRON_FUSE_LIMITS.maximumFileBytes} bytes: ${path}`, ); const size = Number(before.size); - const chunkBytes = options?.chunkBytes ?? ELECTRON_FUSE_LIMITS.chunkBytes; - if ( - !Number.isSafeInteger(chunkBytes) || - chunkBytes < 1 || - chunkBytes > ELECTRON_FUSE_LIMITS.chunkBytes - ) - throw new TypeError( - `chunkBytes must be an integer between 1 and ${ELECTRON_FUSE_LIMITS.chunkBytes}.`, - ); return withArtifactRegularFile( path, options?.resources ?? new ArtifactResourceScope(), @@ -166,7 +156,6 @@ export const readElectronFuseImage = async ( path, size, container.slices, - chunkBytes, signal, ); const current = await recheckPath(path); @@ -287,7 +276,6 @@ const scanSlices = async ( path: string, size: number, ranges: readonly SliceRange[], - chunkBytes: number, signal: AbortSignal | undefined, ): Promise<{ readonly sha256: string; @@ -299,11 +287,11 @@ const scanSlices = async ( hits: [] as number[], })); const hash = createHash("sha256"); - const chunk = Buffer.allocUnsafe(chunkBytes); + const chunk = Buffer.allocUnsafe(ELECTRON_FUSE_LIMITS.chunkBytes); let position = 0; while (position < size) { assertArtifactReadActive(signal); - const length = Math.min(chunkBytes, size - position); + const length = Math.min(chunk.length, size - position); const read = await handle.read(chunk, 0, length, position); if (read.bytesRead === 0) throw new RegularFileChangedError(path); assertArtifactReadActive(signal); diff --git a/src/evm/EvmAdmissionError.ts b/src/evm/EvmAdmissionError.ts index 7f0993c979..f74c38b72a 100644 --- a/src/evm/EvmAdmissionError.ts +++ b/src/evm/EvmAdmissionError.ts @@ -14,7 +14,7 @@ const EVM_ADMISSION_CODES = [ "root_not_map", ] as const; -export type EvmAdmissionCode = (typeof EVM_ADMISSION_CODES)[number]; +type EvmAdmissionCode = (typeof EVM_ADMISSION_CODES)[number]; /** EVM limiter, worker, or metadata admission failure. `code` is the stable identity. */ export class EvmAdmissionError extends Error { diff --git a/src/evm/EvmBytecodeCarrier.test.ts b/src/evm/EvmBytecodeCarrier.test.ts index 69ae9b388a..bd291cbf6e 100644 --- a/src/evm/EvmBytecodeCarrier.test.ts +++ b/src/evm/EvmBytecodeCarrier.test.ts @@ -40,7 +40,7 @@ it("rejects a long interior separator run without pathological backtracking", as killSignal: "SIGKILL", }, ); -}, 20_000); +}); it("distinguishes raw bytes, empty code and malformed UTF-8", () => { const bytes = Uint8Array.from([0xff, 0x00]); const decoded = decodeEvmBytecodeCarrier(bytes, "raw"); diff --git a/src/evm/EvmoleFailures.ts b/src/evm/EvmoleFailures.ts index fb92d36bd9..d755bbece0 100644 --- a/src/evm/EvmoleFailures.ts +++ b/src/evm/EvmoleFailures.ts @@ -69,7 +69,7 @@ export interface EvmFileSizeFailureEvidence { } /** Keep carrier read, engine and owned lifecycle failures distinct. */ -export interface EvmFailureContext { +interface EvmFailureContext { readonly phase: "configuration" | "artifact-read" | "worker"; readonly path: string; readonly workerLimits?: EvmWorkerLimits | undefined; diff --git a/src/evm/EvmoleInterfaceProvider.ts b/src/evm/EvmoleInterfaceProvider.ts index 049f28487c..adb7911d28 100644 --- a/src/evm/EvmoleInterfaceProvider.ts +++ b/src/evm/EvmoleInterfaceProvider.ts @@ -97,14 +97,14 @@ export const evmHostSupported = ( platform: NodeJS.Platform, arch: string, ): boolean => platform === "linux" && arch === "x64"; -export const EVM_PRLIMIT_COMMAND_DEFAULT = "/usr/bin/prlimit"; +const EVM_PRLIMIT_COMMAND_DEFAULT = "/usr/bin/prlimit"; -export interface EvmLimiterProbeFileSystem { +interface EvmLimiterProbeFileSystem { access(path: string, mode: number): Promise; stat(path: string): Promise<{ isFile(): boolean }>; } -export interface EvmLimiterProbeOptions { +interface EvmLimiterProbeOptions { readonly platform?: NodeJS.Platform; readonly arch?: string; readonly fs?: EvmLimiterProbeFileSystem; @@ -254,7 +254,7 @@ type PendingEvmCleanup = { root: | { readonly owner: EvmRuntimeRoot; readonly lease: AnalysisCleanupLease } | undefined; - process: { readonly lease: AnalysisCleanupLease } | undefined; + process: AnalysisCleanupLease | undefined; }; const ownerCleanupMarker = ( @@ -468,7 +468,6 @@ export class EvmoleInterfaceProvider implements EvmInterfacePort { let root: Pick | undefined; let rootLease: AnalysisCleanupLease | undefined; let processOwner: ProviderProcessSupervisor | undefined; - let processLease: AnalysisCleanupLease | undefined; let processCleanup: | { readonly reason: string; @@ -643,7 +642,7 @@ export class EvmoleInterfaceProvider implements EvmInterfacePort { ...(this.launcher === undefined ? {} : { launcher: this.launcher }), retainCleanup: (owner, cleanup) => { processOwner = owner; - processLease = processLeaseFor(owner); + processLeaseFor(owner); processCleanup = { reason: cleanup.reason, resources: @@ -830,7 +829,7 @@ export class EvmoleInterfaceProvider implements EvmInterfacePort { cause.cleanupOwner !== undefined ) { processOwner = cause.cleanupOwner; - processLease = processLeaseFor(cause.cleanupOwner); + processLeaseFor(cause.cleanupOwner); } let fileFailure: EvmFileSizeFailureEvidence | undefined; let markerCleanupFailure: ArtifactReaderFailure | undefined; @@ -968,7 +967,7 @@ export class EvmoleInterfaceProvider implements EvmInterfacePort { root === undefined || rootOwnerLease === undefined ? undefined : { owner: root, lease: rootOwnerLease }, - process: { lease }, + process: lease, }; if (processCleanup === undefined) { const owners = [lease, rootOwnerLease].filter( @@ -1118,7 +1117,7 @@ export class EvmoleInterfaceProvider implements EvmInterfacePort { if (pending === undefined) return undefined; if (pending.process !== undefined) { try { - await pending.process.lease.release(); + await pending.process.release(); } catch (cause: unknown) { const failure = evmCleanupFailure( pending.input, @@ -1126,7 +1125,7 @@ export class EvmoleInterfaceProvider implements EvmInterfacePort { [ ...new Set([ ...(pending.root === undefined ? [] : [pending.root.owner.path]), - ...(pending.process.lease.cleanupResources ?? []), + ...(pending.process.cleanupResources ?? []), ]), ], cause instanceof Error ? cause.message : String(cause), @@ -1136,7 +1135,7 @@ export class EvmoleInterfaceProvider implements EvmInterfacePort { bindEvmCleanupOwners( failure, [ - pending.process.lease, + pending.process, ...(pending.root === undefined ? [] : [pending.root.lease]), ], pending.result.ok ? undefined : pending.result.error, diff --git a/src/filesystem/OwnedFileHandle.ts b/src/filesystem/OwnedFileHandle.ts index 3f54ffeb08..184a9de2b8 100644 --- a/src/filesystem/OwnedFileHandle.ts +++ b/src/filesystem/OwnedFileHandle.ts @@ -1,7 +1,7 @@ import type { FileHandle } from "node:fs/promises"; /** Close admission for one owned file handle. `code` is the stable identity. */ -export class OwnedFileHandleError extends Error { +class OwnedFileHandleError extends Error { readonly _tag = "OwnedFileHandleError"; constructor(readonly code: "handle_invalidated") { diff --git a/src/firmware/FirmwareFiles.ts b/src/firmware/FirmwareFiles.ts index fc3cb4c6b8..07e742f752 100644 --- a/src/firmware/FirmwareFiles.ts +++ b/src/firmware/FirmwareFiles.ts @@ -31,7 +31,6 @@ import { mapArtifactFileFailure, artifactSelectedPathErrno, } from "../artifacts/ArtifactFileOperation.js"; -import { ArtifactReaderFailure } from "../artifacts/ArtifactReader.js"; import { readArtifactFileChunks } from "../artifacts/readArtifactFileChunks.js"; import { decodeDirectoryEntryName } from "../filesystem/decodeDirectoryEntryName.js"; import { NonRegularFileReadError } from "../filesystem/RegularFile.js"; diff --git a/src/firmware/devicetree/DevicetreeProvider.ts b/src/firmware/devicetree/DevicetreeProvider.ts index 1756ff4cb7..385fe4820c 100644 --- a/src/firmware/devicetree/DevicetreeProvider.ts +++ b/src/firmware/devicetree/DevicetreeProvider.ts @@ -1,9 +1,9 @@ import { + projectSelectedArtifactFileFailure, projectStableArtifactFailure, stableArtifactInputRecovery, projectArtifactCloseFailure, } from "../../artifacts/StableArtifactFailure.js"; -import { isSelectedRegularFileError } from "../../filesystem/RegularFile.js"; import { ArtifactReaderFailure } from "../../artifacts/ArtifactReader.js"; import { type StableArtifactSnapshot, @@ -19,7 +19,6 @@ import type { DevicetreePort } from "../../application/binaryDiagnostics/Devicet import type { ExecutionOptions } from "../../application/AnalysisProvider.js"; import { ArtifactOperationError } from "../../domain/artifactOperationError.js"; import { - AnalysisAccessDeniedError, AnalysisArtifactChangedError, AnalysisCancelledError, AnalysisInputError, @@ -170,20 +169,13 @@ const devicetreeFailurePrimary = ( ], ); } - if (cause instanceof Error && "code" in cause) { - if (cause.code === "EACCES" || cause.code === "EPERM") - return new AnalysisAccessDeniedError(OPERATION, input.path, cause.code, { - cause, - }); - if (isSelectedRegularFileError(cause)) - return new AnalysisInputError(OPERATION, { cause }, [ - { - path: ["path"], - reason: "invalid_value", - message: `Selected devicetree blob could not be read (${String(cause.code)}): ${input.path}.`, - }, - ]); - } + const fileFailure = projectSelectedArtifactFileFailure( + cause, + OPERATION, + input.path, + "devicetree blob", + ); + if (fileFailure !== undefined) return fileFailure; return new ArtifactOperationError( OPERATION, origin === "owned-or-classified" && cause instanceof ArtifactReaderFailure diff --git a/src/flutter/FlutterBuildProvider.ts b/src/flutter/FlutterBuildProvider.ts index fe5259201c..70d7cb322f 100644 --- a/src/flutter/FlutterBuildProvider.ts +++ b/src/flutter/FlutterBuildProvider.ts @@ -369,11 +369,6 @@ const checkApkSize = (operation: string, bytes: number): void => { ); }; -export interface FlutterProviderOptions { - /** Deterministic root removal for tests; production removes recursively. */ - readonly removeRoot?: (path: string) => Promise; -} - /** * Flutter build identification by pure parsing: reads `lib//libapp.so` * and `libflutter.so` through the hardened ZIP reader and reports the Dart @@ -385,16 +380,12 @@ export class FlutterBuildProvider { string, Set<{ close(): Promise }> >(); - private readonly removeRoot: (path: string) => Promise; private readonly cleanupFailures = new Map(); private readonly rootLeases = new Map(); private readonly operations = new ArtifactResourceScope(); - constructor(options: FlutterProviderOptions = {}) { + constructor() { this.operations.addCloseHook(() => this.closeRoots()); - this.removeRoot = - options.removeRoot ?? - ((path) => rm(path, { recursive: true, force: true })); } /** @@ -578,7 +569,7 @@ export class FlutterBuildProvider { return false; } try { - await this.removeRoot(root); + await rm(root, { recursive: true, force: true }); this.ownedRoots.delete(root); this.cleanupFailures.delete(root); return true; diff --git a/src/frida/FridaInstrumentationManager.test.ts b/src/frida/FridaInstrumentationManager.test.ts index 71af2b800a..34a16cb850 100644 --- a/src/frida/FridaInstrumentationManager.test.ts +++ b/src/frida/FridaInstrumentationManager.test.ts @@ -1,5 +1,6 @@ import { describe, expect, it, onTestFinished, vi } from "vitest"; import { createDeferred } from "../../tests/fixtures/binarySession.js"; +import { awaitAcknowledgement } from "../../tests/fixtures/lifecycleAcknowledgement.js"; import type { Device, @@ -1774,12 +1775,18 @@ it("serializes script loading against concurrent close and unloads the loaded sc void closing.then(() => { closed = true; }); - expect(manager.closeSession(started.value.sessionId)).toBe(closing); + const secondClose = manager.closeSession(started.value.sessionId); + let secondClosed = false; + void secondClose.then(() => { + secondClosed = true; + }); await Promise.resolve(); expect(closed).toBe(false); + expect(secondClosed).toBe(false); finishScriptLoad?.(); expect((await loading).ok).toBe(true); expect((await closing).ok).toBe(true); + expect((await secondClose).ok).toBe(true); expect(bindings.device.session.scripts[0]?.isDestroyed).toBe(true); expect(bindings.device.session.isDetached()).toBe(true); }); @@ -1863,7 +1870,11 @@ it.each([false, true])( { sourceKind: "inline", source: "send('marker')" }, controller.signal, ); - await entered.promise; + await awaitAcknowledgement( + entered.promise, + loading, + "provider lifecycle boundary", + ); const script = session.scripts[0]; if (script === undefined) throw new Error("Missing admitted script"); const unload = script.unload.bind(script); @@ -1927,7 +1938,11 @@ it("preserves a native rejection observed before cancellation during script clea { sourceKind: "inline", source: "void 0" }, controller.signal, ); - await entered.promise; + await awaitAcknowledgement( + entered.promise, + loading, + "provider lifecycle boundary", + ); controller.abort(new Error("late abort during cleanup")); held.resolve(); const result = await loading; diff --git a/src/gdb/GdbSessionManager.ts b/src/gdb/GdbSessionManager.ts index 2c254aa985..8c515f4979 100644 --- a/src/gdb/GdbSessionManager.ts +++ b/src/gdb/GdbSessionManager.ts @@ -13,6 +13,11 @@ import { AnalysisInputError, AnalysisTimeoutError, } from "../domain/analysisErrorCore.js"; +import { + GDB_SESSION_TOOLS, + stoppedDebuggerSessionError, + unknownDebuggerSessionError, +} from "../domain/debuggerSessionErrors.js"; import { AnalysisError, type AnalysisErrorOptions, @@ -22,7 +27,6 @@ import type { GdbCommandResult, GdbOutputLimit, } from "../domain/commandObservation.js"; -import { DEBUGGER_CONTROL_REPLY_BYTES } from "../domain/analysisResourceLimits.js"; import { currentAnalysisOperationSnapshot } from "../domain/analysisOperationSnapshot.js"; import { err, ok, type Result } from "../domain/result.js"; import { ConfigurationError } from "../domain/configurationErrors.js"; @@ -381,7 +385,7 @@ const gdbMiGroupsListSpan = ( return groupSpan; }; -/** @internal Pure parser for live PIDs in recursive GDB MI thread groups. */ +/** Parse the complete GDB MI inferior inventory without guessing missing PIDs. */ export const gdbThreadGroupPids = ( mi: string, ): readonly string[] | undefined => { @@ -467,12 +471,6 @@ export const gdbThreadGroupPids = ( return undefined; }; -/** @internal Pure count of recursive GDB MI groups with live PIDs. */ -export const gdbThreadGroupCount = (mi: string): number | undefined => { - const pids = gdbThreadGroupPids(mi); - return pids?.length; -}; - const verifyGdbInferiorOutputSink = async ( pid: string, selectedTty: string, @@ -543,14 +541,14 @@ interface GdbSession { readonly verifyInferiorsEndedAfterExit: () => Promise; readonly knownInferiorIdentities: Map; readonly inferiorTty: string | undefined; - /** Startup and detach MI. Separate from the caller command-output cap. */ - readonly controlReplyBytes: number; readonly stdoutSettled: Promise; readonly settleStdout: () => void; inferiorTtyVerified: boolean; noInferiorVerified: boolean; /** Sticky admission seal once destructive cleanup starts, including failed stops. */ cleanupStarted: boolean; + /** close_gdb_session started cleanup, so a successful retry releases the ID. */ + closeRequested: boolean; stopAttempt: ReturnType | undefined; readonly pending: Map; readonly cleanupFailures: Set; @@ -575,17 +573,8 @@ interface GdbSession { recentRecordsTruncated: boolean; } -const gdbConsoleUnavailable = (): AnalysisInputError => - new AnalysisInputError("gdb_console", undefined, [ - { - path: ["session_id"], - reason: "invalid_value", - message: "GDB session is unavailable or closed.", - }, - ]); - const gdbCloseUnavailable = (): AnalysisInputError => - new AnalysisInputError("close_gdb_session"); + unknownDebuggerSessionError(GDB_SESSION_TOOLS, "close_gdb_session"); /** Owns persistent GDB/MI sessions independently from the active binary target. */ export class GdbSessionManager { @@ -615,7 +604,6 @@ export class GdbSessionManager { readonly #maxSessions: DebuggerAdmissionLimit; readonly #maxPendingCommands: DebuggerAdmissionLimit; readonly #launchCommandOutput: DebuggerAdmissionLimit; - readonly #controlReplyBytes: number; get #commandDeadline(): DebuggerAdmissionLimit { const override = currentAnalysisOperationSnapshot()?.overrides[ @@ -668,11 +656,6 @@ export class GdbSessionManager { ) => Promise; /** @internal Read-only process identity observer for lifecycle tests. */ readonly readInferiorProcessStartTime?: ProcessStartTimeReader; - /** - * @internal Test seam for startup and detach MI truncation. - * Production uses the fixed control-reply budget. - */ - readonly controlReplyBytes?: number; } = {}, ) { this.#environment = { ...(options.environment ?? process.env) }; @@ -689,8 +672,6 @@ export class GdbSessionManager { this.#maxSessions = limits.maxSessions; this.#maxPendingCommands = limits.maxPendingCommands; this.#launchCommandOutput = limits.maxCommandOutputBytes; - this.#controlReplyBytes = - options.controlReplyBytes ?? DEBUGGER_CONTROL_REPLY_BYTES; } async start( @@ -786,7 +767,7 @@ export class GdbSessionManager { "start_gdb_session", "memory", `GDB session capacity reached (${String(this.#maxSessions.value)} sessions).`, - "Close a GDB session before starting another.", + "Close a GDB session before starting another; a stopped session that gdb_session_status reports closed still holds its slot until close_gdb_session releases it.", this.#maxSessions, { max_sessions: this.#maxSessions.value, @@ -913,7 +894,6 @@ export class GdbSessionManager { }, knownInferiorIdentities: new Map(), inferiorTty, - controlReplyBytes: this.#controlReplyBytes, stdoutSettled, settleStdout, inferiorTtyVerified: false, @@ -921,6 +901,7 @@ export class GdbSessionManager { // during startup, so no inferior can exist until execute() is called. noInferiorVerified: true, cleanupStarted: false, + closeRequested: false, stopAttempt: undefined, pending: new Map(), cleanupFailures: new Set(), @@ -1274,15 +1255,13 @@ export class GdbSessionManager { > > { const session = this.#sessions.get(sessionId); - if ( - this.#closing || - session === undefined || - !session.ready || - session.cleanupStarted || - session.exited || - session.protocolError !== undefined - ) - return err(gdbConsoleUnavailable()); + if (session === undefined) + return err(unknownDebuggerSessionError(GDB_SESSION_TOOLS, "gdb_console")); + const stopped = this.#stopReason(session); + if (stopped !== undefined) + return err( + stoppedDebuggerSessionError(GDB_SESSION_TOOLS, "gdb_console", stopped), + ); if (signal?.aborted) return err(cancelledWithCallerReason("gdb_console", signal)); try { @@ -1291,14 +1270,16 @@ export class GdbSessionManager { async () => { // Retained cleanup ownership and a delayed exit event do not imply // command readiness. Recheck admission when this queued turn starts. - if ( - this.#closing || - this.#sessions.get(sessionId) !== session || - session.cleanupStarted || - session.exited || - session.protocolError !== undefined - ) - return err(gdbConsoleUnavailable()); + // Every path that releases a session first records why it stopped. + const stoppedWhileQueued = this.#stopReason(session); + if (stoppedWhileQueued !== undefined) + return err( + stoppedDebuggerSessionError( + GDB_SESSION_TOOLS, + "gdb_console", + stoppedWhileQueued, + ), + ); session.noInferiorVerified = false; session.knownInferiorIdentities.clear(); if (commandMayChangeInferiorOutput(command)) @@ -1473,21 +1454,17 @@ export class GdbSessionManager { readonly diagnostics_truncated: boolean; readonly inferior_tty: string | null; readonly inferior_tty_verified: boolean; + readonly closed_reason?: string; } | undefined { const session = this.#sessions.get(sessionId); if (session === undefined) return undefined; const diagnostics = session.supervisor.snapshot(); + const stopped = this.#stopReason(session); return { session_id: session.id, mi_version: session.miVersion, - state: - session.cleanupStarted || - session.exited || - session.protocolError !== undefined || - !session.ready - ? "closed" - : "ready", + state: stopped === undefined ? "ready" : "closed", recent_mi_records: [...session.lines], recent_mi_records_truncated: session.recentRecordsTruncated, mi_invalid_utf8: session.invalidUtf8Observed, @@ -1499,9 +1476,24 @@ export class GdbSessionManager { diagnostics_truncated: diagnostics.diagnosticTruncated === true, inferior_tty: session.inferiorTty ?? null, inferior_tty_verified: session.inferiorTtyVerified, + ...(stopped === undefined ? {} : { closed_reason: stopped }), }; } + /** Why a retained session accepts no commands, or undefined while it is ready. */ + #stopReason(session: GdbSession): string | undefined { + if (session.protocolError !== undefined) return session.protocolError; + if (this.#closing) + return "REA is shutting down and closing every GDB session."; + if (session.closeRequested) + return "close_gdb_session started releasing this session, and its owned cleanup has not completed."; + if (session.exited) + return `The GDB process exited (exit code ${String(session.exitCode)}, signal ${String(session.exitSignal)}); a command such as quit, or a crash, can end it.`; + if (session.cleanupStarted || !session.ready) + return "REA stopped this GDB session."; + return undefined; + } + async close( sessionId: string, ): Promise< @@ -1538,11 +1530,12 @@ export class GdbSessionManager { throw new Error( "GDB could not confirm its complete, untruncated inferior list before shutdown.", ); - const groupCount = gdbThreadGroupCount(groups.mi); - if (groupCount === undefined) + const pids = gdbThreadGroupPids(groups.mi); + if (pids === undefined) throw new Error( "GDB returned an unknown inferior list before shutdown.", ); + const groupCount = pids.length; if ( groupCount > 1 || (groupCount > 0 && !session.inferiorTtyVerified) @@ -1566,6 +1559,7 @@ export class GdbSessionManager { // Refusing inferior disposition above leaves console remediation // available; from here onward only owned cleanup may be retried. session.cleanupStarted = true; + session.closeRequested = true; if (groupCount === 0) { session.noInferiorVerified = true; targetState = "none_observed"; @@ -1605,6 +1599,7 @@ export class GdbSessionManager { async #finishSessionCleanup( session: GdbSession, ): Promise> { + session.closeRequested = true; const stopped = await this.#stopDebuggerOnly(session); if (stopped.status === "incomplete") return err( @@ -1631,7 +1626,10 @@ export class GdbSessionManager { stopped.reason, ), ); - this.#sessions.delete(session.id); + // The caller still holds a published session's ID; keep it addressable + // so commands and status report why it stopped until it is closed. + if (!session.ready || session.closeRequested) + this.#sessions.delete(session.id); } return ok(null); } @@ -2172,7 +2170,7 @@ export class GdbSessionManager { const acceptedDeadline = this.#commandDeadline; const acceptedOutput = command.startsWith("-interpreter-exec console ") ? this.#commandOutput - : { ...debuggerControlReplyOutput, value: session.controlReplyBytes }; + : debuggerControlReplyOutput; return new Promise((resolve, reject) => { let abortListener!: () => void; const cleanup = (): void => { @@ -2226,6 +2224,7 @@ export class GdbSessionManager { "The unrestricted GDB console can access local shell, filesystem, network, and target controls.", ], }); + session.protocolError ??= `A GDB command exceeded its ${String(acceptedDeadline.value)} ms deadline; owned-session shutdown was requested because output correlation can no longer be guaranteed.`; void this.#stopDebuggerOnly(session).then((stopped) => { if ( stopped.status === "incomplete" && diff --git a/src/ghidra/GhidraAnalysisProfile.ts b/src/ghidra/GhidraAnalysisProfile.ts index 83fa0320f2..f56aa7adbb 100644 --- a/src/ghidra/GhidraAnalysisProfile.ts +++ b/src/ghidra/GhidraAnalysisProfile.ts @@ -74,7 +74,7 @@ const sharedAnalysisSemantics = { instruction_status: "unlisted-and-containing-code-data-v3", instruction_references: "exact-address-incoming-without-synthetic-entry-v3", byte_search_coverage: "separate-loaded-overlays-v1", - function_boundary_observations: "ghidra-function-boundary-limitations-v4", + function_boundary_observations: "ghidra-function-boundary-limitations-v5", } as const; /** diff --git a/src/ghidra/GhidraDoctor.test.ts b/src/ghidra/GhidraDoctor.test.ts index 2f801f448f..64dbcf8268 100644 --- a/src/ghidra/GhidraDoctor.test.ts +++ b/src/ghidra/GhidraDoctor.test.ts @@ -517,15 +517,11 @@ describe("Windows Ghidra temporary directory check", () => { ); }); - it("does not probe when no Windows installation was selected", async () => { - const probe = vi.fn(() => Promise.reject(new Error("unexpected probe"))); + it("omits the temporary directory check when no Windows installation was selected", async () => { const projected = await inspectSystemGhidraProvider( {}, inspectionResources.owner, - undefined, - probe, ); - expect(probe).not.toHaveBeenCalled(); expect(projected.checks.map(({ name }) => name)).not.toContain( "temp_directory", ); diff --git a/src/ghidra/GhidraDoctor.ts b/src/ghidra/GhidraDoctor.ts index ee6b172f39..335d0a1be8 100644 --- a/src/ghidra/GhidraDoctor.ts +++ b/src/ghidra/GhidraDoctor.ts @@ -120,9 +120,6 @@ export const inspectSystemGhidraProvider = async ( environment: Readonly, resourceOwner: RetainedResourceScope, signal?: AbortSignal, - probeTemporaryDirectory: ( - resourceOwner: RetainedResourceScope, - ) => Promise = probeGhidraTemporaryDirectory, ): Promise => { try { const installation = await inspectGhidraInstallation({ @@ -139,7 +136,7 @@ export const inspectSystemGhidraProvider = async ( // The restricted-host denial reported for #7075 is Windows-specific. const temporaryDirectory = installation.status === "available" && installation.platform === "win32" - ? await probeTemporaryDirectory(resourceOwner) + ? await probeGhidraTemporaryDirectory(resourceOwner) : undefined; return projectGhidraDoctorInspection(installation, temporaryDirectory); } catch (cause: unknown) { diff --git a/src/ghidra/GhidraDolLoadImage.ts b/src/ghidra/GhidraDolLoadImage.ts index 353ac70f94..e993b41ec7 100644 --- a/src/ghidra/GhidraDolLoadImage.ts +++ b/src/ghidra/GhidraDolLoadImage.ts @@ -1,12 +1,11 @@ +import { createLoadImageChecks } from "./GhidraLoadImageChecks.js"; import type { GhidraLanguageOverride } from "../config/ghidraLanguageOverride.js"; import type { BinaryTarget } from "../domain/binaryTargetTypes.js"; import type { DolHeader } from "../domain/gamecubeDol.js"; -import type { JsonValue } from "../domain/jsonValue.js"; import { nativeAddressCoordinateInputSchema } from "../domain/native/nativeAddress.js"; import { nativeLoadImageSchema, type NativeLoadImage, - type NativeLoadImageCheck, type NativeLoadImageObservation, } from "../domain/native/nativeLoadImage.js"; import { err, ok, type Result } from "../domain/result.js"; @@ -89,23 +88,7 @@ export const attestGhidraDolLoadImage = ( if (snapshot.sectionSha256.length !== header.sections.length) return err("Ghidra DOL snapshot section digests do not match the header"); const language = ghidraDolLanguage(languageOverride); - const checks: NativeLoadImageCheck[] = []; - const check = ( - name: string, - expected: JsonValue, - observed: JsonValue, - fileOffset: number | null = null, - location: string | null = null, - ): void => { - checks.push({ - name, - matched: JSON.stringify(expected) === JSON.stringify(observed), - expected, - observed, - file_offset: fileOffset, - address: location, - }); - }; + const { checks, check } = createLoadImageChecks(); // Field order is fixed: checks compare serialized values. const fileFacts = (mapping: FileFacts) => ({ diff --git a/src/ghidra/GhidraDolProfile.test.ts b/src/ghidra/GhidraDolProfile.test.ts index 0de31e0799..971147516e 100644 --- a/src/ghidra/GhidraDolProfile.test.ts +++ b/src/ghidra/GhidraDolProfile.test.ts @@ -19,7 +19,10 @@ import { ghidraDolSectionFile, } from "./GhidraDolProfile.js"; import { GhidraProvider } from "./GhidraProvider.js"; -import { ghidraProfileInstallationHost as host } from "./GhidraInstallation.fixture.js"; +import { + ghidraProfileInstallationHost as host, + simulatedInstallDirectory, +} from "./GhidraInstallation.fixture.js"; // Abbreviated from Ghidra 12.1.4 ppc.ldefs and x86.ldefs: the stock PowerPC // default, the optional Broadway variant, and one little-endian language to refuse. @@ -47,7 +50,7 @@ const configFor = (environment: NodeJS.ProcessEnv) => { }; const installedConfig = async (environment: NodeJS.ProcessEnv) => { - const installDir = await createTestTempDirectory("rea-ghidra-dol-"); + const installDir = await simulatedInstallDirectory("rea-ghidra-dol-"); const languages = join( installDir, "Ghidra/Processors/PowerPC/data/languages", diff --git a/src/ghidra/GhidraExecutableHeader.ts b/src/ghidra/GhidraExecutableHeader.ts index aee59a2259..bd27c3e62b 100644 --- a/src/ghidra/GhidraExecutableHeader.ts +++ b/src/ghidra/GhidraExecutableHeader.ts @@ -9,7 +9,7 @@ import { readStableFilePrefix } from "../filesystem/readStableFileBytes.js"; import { ghidraFileFailure } from "./GhidraFileArtifacts.js"; /** Only this prefix is examined; the remainder is never retained or read. */ -export const GHIDRA_EXECUTABLE_HEADER_BYTES = 4096; +const GHIDRA_EXECUTABLE_HEADER_BYTES = 4096; /** Retain complete prefix observations independently of final descriptor cleanup. */ export const readGhidraExecutableHeader = async ( diff --git a/src/ghidra/GhidraInstallation.fixture.ts b/src/ghidra/GhidraInstallation.fixture.ts index ff57aa7fe3..e04403a469 100644 --- a/src/ghidra/GhidraInstallation.fixture.ts +++ b/src/ghidra/GhidraInstallation.fixture.ts @@ -1,3 +1,9 @@ +import { mkdir, mkdtemp, realpath } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { join, parse, relative } from "node:path"; +import { onTestFinished } from "vitest"; +import { normalizeTestTemporaryPrefix } from "../../scripts/lib/test-temporary-ownership.mjs"; +import { removeTestWorkspace } from "../../tests/support/workspace/workspaceFixture.js"; import { ok as javaProbeOk, err } from "../domain/result.js"; import type { GhidraJavaObservation, @@ -23,6 +29,44 @@ export const ghidraProfileInstallationHost: GhidraInstallationHost = { }), }; +/** + * A temporary installation directory the simulated Linux host accepts as + * absolute. On Windows it is made under the working directory and spelled as a + * POSIX path on that drive; other hosts get an ordinary test directory. The + * caller owns and disposes the native directory. + */ +export const createSimulatedInstallDirectory = async ( + prefix: string, +): Promise<{ + readonly path: string; + readonly dispose: () => Promise; +}> => { + const root = + process.platform === "win32" + ? join(process.cwd(), "build") + : await realpath(tmpdir()); + await mkdir(root, { recursive: true }); + const owned = await realpath( + await mkdtemp(join(root, normalizeTestTemporaryPrefix(prefix))), + ); + return { + path: + process.platform === "win32" + ? `/${relative(parse(owned).root, owned).replaceAll("\\", "/")}` + : owned, + dispose: () => removeTestWorkspace(owned), + }; +}; + +/** Own the simulated directory for a single test. */ +export const simulatedInstallDirectory = async ( + prefix: string, +): Promise => { + const directory = await createSimulatedInstallDirectory(prefix); + onTestFinished(directory.dispose); + return directory.path; +}; + /** A confirmed absent Java command for installation admission fixtures. */ export const missingJavaProbe = err({ _tag: "GhidraJavaProbeFailure", diff --git a/src/ghidra/GhidraInstallationProperties.ts b/src/ghidra/GhidraInstallationProperties.ts index c459802969..3f1220a183 100644 --- a/src/ghidra/GhidraInstallationProperties.ts +++ b/src/ghidra/GhidraInstallationProperties.ts @@ -19,7 +19,7 @@ import { ghidraApplicationProperties } from "./GhidraInstallationPolicy.js"; import { ghidraFileFailure } from "./GhidraFileArtifacts.js"; /** Metadata admission budget, not a Java Properties format limit. */ -export const MAX_GHIDRA_INSTALLATION_PROPERTIES_BYTES = 1024 * 1024; +const MAX_GHIDRA_INSTALLATION_PROPERTIES_BYTES = 1024 * 1024; export interface GhidraInstallationFileOptions { readonly resourceOwner: RetainedResourceScope; readonly signal?: AbortSignal; diff --git a/src/ghidra/GhidraLanguageOverride.test.ts b/src/ghidra/GhidraLanguageOverride.test.ts index 2657b23b9e..c9df41a86e 100644 --- a/src/ghidra/GhidraLanguageOverride.test.ts +++ b/src/ghidra/GhidraLanguageOverride.test.ts @@ -1,13 +1,5 @@ -import { - chmod, - mkdir, - mkdtemp, - readdir, - rm, - writeFile, -} from "node:fs/promises"; -import { tmpdir } from "node:os"; -import { join, parse, relative } from "node:path"; +import { chmod, mkdir, readdir, rm, writeFile } from "node:fs/promises"; +import { join } from "node:path"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; import { createTestTempDirectory } from "../../tests/fixtures/temporaryDirectory.js"; import { parseConfig } from "../config/parseConfig.js"; @@ -18,7 +10,10 @@ import { ghidraProfileLanguageOverride, sameGhidraLanguageOverride, } from "./GhidraAnalysisProfile.js"; -import { ghidraProfileInstallationHost as host } from "./GhidraInstallation.fixture.js"; +import { + createSimulatedInstallDirectory, + ghidraProfileInstallationHost as host, +} from "./GhidraInstallation.fixture.js"; import { admitGhidraLanguageOverride, parseGhidraLanguageDefinitions, @@ -106,25 +101,19 @@ const legacyMips: BinaryTarget = { }; let installDir = ""; -let ownedInstallDir = ""; +let disposeInstallDir: (() => Promise) | undefined; beforeAll(async () => { - const root = - process.platform === "win32" ? join(process.cwd(), "build") : tmpdir(); - await mkdir(root, { recursive: true }); - ownedInstallDir = await mkdtemp(join(root, "rea-ghidra-languages-")); - // The simulated Linux host needs a POSIX absolute path. Keep the owned - // native path for cleanup and derive the simulated path on the same drive. - installDir = - process.platform === "win32" - ? `/${relative(parse(ownedInstallDir).root, ownedInstallDir).replaceAll("\\", "/")}` - : ownedInstallDir; + const directory = await createSimulatedInstallDirectory( + "rea-ghidra-languages-", + ); + installDir = directory.path; + disposeInstallDir = directory.dispose; const languages = join(installDir, "Ghidra/Processors/x86/data/languages"); await mkdir(languages, { recursive: true }); await writeFile(join(languages, "x86.ldefs"), X86_LDEFS); }); afterAll(async () => { - if (ownedInstallDir !== "") - await rm(ownedInstallDir, { recursive: true, force: true }); + await disposeInstallDir?.(); }); const provider = (environment: Record) => { diff --git a/src/ghidra/GhidraLauncher.ts b/src/ghidra/GhidraLauncher.ts index e41f55dac6..cbc2456018 100644 --- a/src/ghidra/GhidraLauncher.ts +++ b/src/ghidra/GhidraLauncher.ts @@ -16,7 +16,7 @@ import { import type { GhidraLanguageOverride } from "../config/ghidraLanguageOverride.js"; import type { GhidraRiscvCallingConvention } from "./GhidraRiscvProfile.js"; import { snapshotEnvironment } from "../process/snapshotEnvironment.js"; -import { copyFile, mkdir, readFile, realpath } from "node:fs/promises"; +import { copyFile, mkdir, realpath } from "node:fs/promises"; import { basename, dirname, join, posix, win32 } from "node:path"; import { writeGhidraRuntimeFile } from "./GhidraRuntimeFile.js"; diff --git a/src/ghidra/GhidraLoadImageChecks.ts b/src/ghidra/GhidraLoadImageChecks.ts new file mode 100644 index 0000000000..9ce31958b2 --- /dev/null +++ b/src/ghidra/GhidraLoadImageChecks.ts @@ -0,0 +1,24 @@ +import type { JsonValue } from "../domain/jsonValue.js"; +import type { NativeLoadImageCheck } from "../domain/native/nativeLoadImage.js"; + +/** Record one attestation's checks, preserving compared values and source locations. */ +export const createLoadImageChecks = () => { + const checks: NativeLoadImageCheck[] = []; + const check = ( + name: string, + expected: JsonValue, + observed: JsonValue, + fileOffset: number | null = null, + location: string | null = null, + ): void => { + checks.push({ + name, + matched: JSON.stringify(expected) === JSON.stringify(observed), + expected, + observed, + file_offset: fileOffset, + address: location, + }); + }; + return { checks, check }; +}; diff --git a/src/ghidra/GhidraLoadImageValues.test.ts b/src/ghidra/GhidraLoadImageValues.test.ts index e3e5ad1c07..04d8bc37b9 100644 --- a/src/ghidra/GhidraLoadImageValues.test.ts +++ b/src/ghidra/GhidraLoadImageValues.test.ts @@ -113,13 +113,13 @@ const damage: ReadonlyArray< [ "modified source", (x) => { - x.source_files[0] && (x.source_files[0].modified_sha256 = "f".repeat(64)); + x.source_files[0]!.modified_sha256 = "f".repeat(64); }, ], [ "original source", (x) => { - x.source_files[0] && (x.source_files[0].original_sha256 = "f".repeat(64)); + x.source_files[0]!.original_sha256 = "f".repeat(64); }, ], [ diff --git a/src/ghidra/GhidraLoadImageValues.ts b/src/ghidra/GhidraLoadImageValues.ts index 5e0679d411..198b5deed2 100644 --- a/src/ghidra/GhidraLoadImageValues.ts +++ b/src/ghidra/GhidraLoadImageValues.ts @@ -1,12 +1,11 @@ +import { createLoadImageChecks } from "./GhidraLoadImageChecks.js"; import { createHash } from "node:crypto"; import { nativeLoadImageSchema, type NativeLoadImage, - type NativeLoadImageCheck, type NativeLoadImageObservation, } from "../domain/native/nativeLoadImage.js"; import type { DosMzHeader, MzHeaderEvidence } from "../domain/dosMz.js"; -import type { JsonValue } from "../domain/jsonValue.js"; import type { BinaryTarget } from "../domain/binaryTargetTypes.js"; import { validateDosComLength } from "../domain/dosCom.js"; import { err, ok, type Result } from "../domain/result.js"; @@ -24,28 +23,6 @@ const limitations = [ "A mismatching memory digest identifies the reported source range; it does not locate the first differing byte inside that range.", ]; -/** Record one attestation's checks, preserving compared values and source locations. */ -const createLoadImageChecks = () => { - const checks: NativeLoadImageCheck[] = []; - const check = ( - name: string, - expected: JsonValue, - observed: JsonValue, - fileOffset: number | null = null, - location: string | null = null, - ): void => { - checks.push({ - name, - matched: JSON.stringify(expected) === JSON.stringify(observed), - expected, - observed, - file_offset: fileOffset, - address: location, - }); - }; - return { checks, check }; -}; - export type GhidraDosImage = { readonly header: DosMzHeader; readonly loadSegment: number; diff --git a/src/ghidra/GhidraMipsProfile.test.ts b/src/ghidra/GhidraMipsProfile.test.ts index 44e2105b5e..dcb4ee734d 100644 --- a/src/ghidra/GhidraMipsProfile.test.ts +++ b/src/ghidra/GhidraMipsProfile.test.ts @@ -1,7 +1,6 @@ import { describe, expect, it } from "vitest"; import { mkdir, writeFile } from "node:fs/promises"; import { join } from "node:path"; -import { createTestTempDirectory } from "../../tests/fixtures/temporaryDirectory.js"; import { ok } from "../domain/result.js"; import { parseConfig } from "../config/parseConfig.js"; import type { @@ -17,7 +16,10 @@ import { ghidraMipsProfileMismatchReason, ghidraMipsProfileParameters, } from "./GhidraMipsProfile.js"; -import { ghidraProfileInstallationHost as host } from "./GhidraInstallation.fixture.js"; +import { + ghidraProfileInstallationHost as host, + simulatedInstallDirectory, +} from "./GhidraInstallation.fixture.js"; import { GHIDRA_SESSION_CAPABILITIES } from "./GhidraSessionValues.js"; import type { GhidraMipsSoftFloatReport } from "./GhidraMipsProfile.js"; import { @@ -45,7 +47,7 @@ const MIPS_LANGUAGE_DEFINITIONS = ` `; const installedMipsConfig = async () => { - const installDir = await createTestTempDirectory("rea-ghidra-mips-"); + const installDir = await simulatedInstallDirectory("rea-ghidra-mips-"); const languages = join(installDir, "Ghidra/Processors/MIPS/data/languages"); await mkdir(languages, { recursive: true }); await writeFile(join(languages, "mips.ldefs"), MIPS_LANGUAGE_DEFINITIONS); diff --git a/src/ghidra/GhidraPowerPcProfile.test.ts b/src/ghidra/GhidraPowerPcProfile.test.ts index 67877e22c2..15cdadbd4e 100644 --- a/src/ghidra/GhidraPowerPcProfile.test.ts +++ b/src/ghidra/GhidraPowerPcProfile.test.ts @@ -1,7 +1,6 @@ -import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises"; -import { tmpdir } from "node:os"; -import { join, parse, relative } from "node:path"; -import { describe, expect, it, onTestFinished } from "vitest"; +import { mkdir, writeFile } from "node:fs/promises"; +import { join } from "node:path"; +import { describe, expect, it } from "vitest"; import { parseConfig } from "../config/parseConfig.js"; import type { BinaryTarget, @@ -16,7 +15,10 @@ import { ghidraProfileLanguageOverride, } from "./GhidraAnalysisProfile.js"; import { GhidraProvider } from "./GhidraProvider.js"; -import { ghidraProfileInstallationHost as host } from "./GhidraInstallation.fixture.js"; +import { + ghidraProfileInstallationHost as host, + simulatedInstallDirectory, +} from "./GhidraInstallation.fixture.js"; // Abbreviated from Ghidra 12.1.4 Processors/PowerPC/data/languages/ppc.ldefs. const PPC_LDEFS = ` @@ -76,19 +78,7 @@ describe("bounded Ghidra PowerPC admission", () => { }); it("lets a configured language replace the lane default", async () => { - const root = - process.platform === "win32" ? join(process.cwd(), "build") : tmpdir(); - await mkdir(root, { recursive: true }); - const ownedInstallDir = await mkdtemp(join(root, "rea-ghidra-ppc-")); - onTestFinished(async () => { - await rm(ownedInstallDir, { recursive: true, force: true }); - }); - // The simulated Linux host needs a POSIX absolute path. Keep the owned - // native path for cleanup and derive the simulated path on the same drive. - const installDir = - process.platform === "win32" - ? `/${relative(parse(ownedInstallDir).root, ownedInstallDir).replaceAll("\\", "/")}` - : ownedInstallDir; + const installDir = await simulatedInstallDirectory("rea-ghidra-ppc-"); const languages = join( installDir, "Ghidra/Processors/PowerPC/data/languages", diff --git a/src/ghidra/GhidraProvider.ts b/src/ghidra/GhidraProvider.ts index 15d25ef2b4..63c6e8783e 100644 --- a/src/ghidra/GhidraProvider.ts +++ b/src/ghidra/GhidraProvider.ts @@ -671,16 +671,11 @@ export class GhidraProvider implements AnalysisProviderCandidate { ); const owned = attempt; this.#inspectionAttempt = owned; - void owned.promise.then( - () => { - if (this.#inspectionAttempt === owned) - this.#inspectionAttempt = undefined; - }, - () => { - if (this.#inspectionAttempt === owned) - this.#inspectionAttempt = undefined; - }, - ); + const clearInspection = (): void => { + if (this.#inspectionAttempt === owned) + this.#inspectionAttempt = undefined; + }; + void owned.promise.then(clearInspection, clearInspection); } const waited = await attempt.wait(callerSignal); if (waited.status === "cancelled") diff --git a/src/ghidra/GhidraProviderClient.ts b/src/ghidra/GhidraProviderClient.ts index ba12ccf820..dad8e75752 100644 --- a/src/ghidra/GhidraProviderClient.ts +++ b/src/ghidra/GhidraProviderClient.ts @@ -300,7 +300,7 @@ export const createGhidraProviderClient = (input: { const { config, logger, clientFactory, context, installation } = input; const { target, profile } = input.admission; const prerequisites = ghidraClientPrerequisites(profile, installation); - if (!prerequisites.ok) return err(prerequisites.error); + if (!prerequisites.ok) return prerequisites; const committedProfile = prerequisites.value.profile; const checkpoint = ghidraCheckpointSelectionSchema .optional() diff --git a/src/ghidra/GhidraRawProfileDigest.test.ts b/src/ghidra/GhidraRawProfileDigest.test.ts index 682576a8ab..6621f5e142 100644 --- a/src/ghidra/GhidraRawProfileDigest.test.ts +++ b/src/ghidra/GhidraRawProfileDigest.test.ts @@ -8,12 +8,12 @@ import { GhidraProvider } from "./GhidraProvider.js"; // Digests commit to the current shared policy, including the instruction // status, load-image relocation-symbol, procedure search, byte-search and -// bit-field base-type, runtime value-flow budgets and inventory-search revisions that prevent reuse of snapshots with earlier +// bit-field base-type, runtime value-flow budgets, function-boundary and inventory-search revisions that prevent reuse of snapshots with earlier // results. const RAW_DIGESTS = { - "6502": "51160ff6e94cb32eaeb87ac248915afb3bc17e33b11a1a101222344dd3907c6f", + "6502": "6c4e88435526e8f0907b072600e3b121577962a36554e023dde4cf85113dd9f6", "x86-disabled-analyzer": - "94e93961a699cf9250844d53fe95802c72063c08656fc308b3fed523dd86ea1c", + "b2dc33d1932cbe71d72819428634927f3a2d8a2bbd54b6893f823ccb6b795d0b", } as const; const configFor = (environment: NodeJS.ProcessEnv) => { diff --git a/src/ghidra/GhidraRequestQueue.ts b/src/ghidra/GhidraRequestQueue.ts index 94c5940a05..9746be3b15 100644 --- a/src/ghidra/GhidraRequestQueue.ts +++ b/src/ghidra/GhidraRequestQueue.ts @@ -9,15 +9,15 @@ import { } from "./GhidraSessionError.js"; /** Result settled by one queued Ghidra Program request. */ -export type GhidraQueuedRequestResult = Result; +type GhidraQueuedRequestResult = Result; /** Socket execution seam used after a request reaches the queue head. */ -export type GhidraRequestExecutor = ( +type GhidraRequestExecutor = ( method: string, parameters: JsonValue, options: GhidraRequestOptions, ) => Promise; /** Stop the provider session when an admitted operation is cancelled. */ -export type GhidraActiveCancellationHandler = () => Promise< +type GhidraActiveCancellationHandler = () => Promise< Result >; diff --git a/src/ghidra/GhidraRiscvProfile.test.ts b/src/ghidra/GhidraRiscvProfile.test.ts index 3851a99ecd..768b719fa5 100644 --- a/src/ghidra/GhidraRiscvProfile.test.ts +++ b/src/ghidra/GhidraRiscvProfile.test.ts @@ -1,6 +1,5 @@ -import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; -import { tmpdir } from "node:os"; -import { join, parse, relative } from "node:path"; +import { mkdir, readFile, writeFile } from "node:fs/promises"; +import { join } from "node:path"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; import { parseConfig } from "../config/parseConfig.js"; import type { @@ -31,7 +30,10 @@ import { GHIDRA_SESSION_CAPABILITIES, type GhidraSessionInfo, } from "./GhidraSessionValues.js"; -import { ghidraProfileInstallationHost as host } from "./GhidraInstallation.fixture.js"; +import { + createSimulatedInstallDirectory, + ghidraProfileInstallationHost as host, +} from "./GhidraInstallation.fixture.js"; import { ghidraPinnedLanguage, ghidraProfileLanguageOverride, @@ -107,21 +109,19 @@ const ilp32 = ( }); let installDir = ""; +let disposeInstallDir: (() => Promise) | undefined; beforeAll(async () => { - const root = - process.platform === "win32" ? join(process.cwd(), "build") : tmpdir(); - await mkdir(root, { recursive: true }); - installDir = await mkdtemp(join(root, "rea-ghidra-riscv-languages-")); - // The simulated Linux host needs a POSIX absolute path. A current-drive root - // path also reaches this real fixture on Windows without changing admission. - if (process.platform === "win32") - installDir = `/${relative(parse(installDir).root, installDir).replaceAll("\\", "/")}`; + const directory = await createSimulatedInstallDirectory( + "rea-ghidra-riscv-languages-", + ); + installDir = directory.path; + disposeInstallDir = directory.dispose; const languages = join(installDir, "Ghidra/Processors/RISCV/data/languages"); await mkdir(languages, { recursive: true }); await writeFile(join(languages, "riscv.ldefs"), RISCV_LDEFS); }); afterAll(async () => { - await rm(installDir, { recursive: true, force: true }); + await disposeInstallDir?.(); }); const ghidra = (environment: Record = {}) => diff --git a/src/ghidra/GhidraSnapshotCache.test.ts b/src/ghidra/GhidraSnapshotCache.test.ts index d396e4e504..31e4733d73 100644 --- a/src/ghidra/GhidraSnapshotCache.test.ts +++ b/src/ghidra/GhidraSnapshotCache.test.ts @@ -351,8 +351,8 @@ describe("Ghidra snapshot profile admission", () => { }, { parameter: "function_boundary_observations", - current: "ghidra-function-boundary-limitations-v4", - legacy: "ghidra-function-boundary-limitations-v3", + current: "ghidra-function-boundary-limitations-v5", + legacy: "ghidra-function-boundary-limitations-v4", }, { parameter: "instruction_references", diff --git a/src/ghidra/GhidraSocketConnection.ts b/src/ghidra/GhidraSocketConnection.ts index 16ebf9d279..e8274c8362 100644 --- a/src/ghidra/GhidraSocketConnection.ts +++ b/src/ghidra/GhidraSocketConnection.ts @@ -1,15 +1,15 @@ import type { ArtifactResourceScope } from "../artifacts/ArtifactResourceScope.js"; -import { createConnection, type Socket } from "node:net"; +import type { Socket } from "node:net"; +import { connectLocalSocketOnce } from "../process/LocalSocketConnection.js"; import { err, ok, type Result } from "../domain/result.js"; import type { ProviderStartupDeadline } from "../process/ProviderDeadline.js"; import { GHIDRA_STARTUP_DEADLINE_ELAPSED, - GhidraSessionError, + type GhidraSessionError, type GhidraSessionFailureFactory, } from "./GhidraSessionError.js"; import { - type GhidraConnectTarget, type GhidraEndpoint, observeGhidraEndpoint, } from "./GhidraTransport.js"; @@ -86,7 +86,7 @@ export const connectGhidraSocket = async ( return err(interruptionFailure(options)); continue; } - const connected = await connectGhidraSocketOnce( + const connected = await connectLocalSocketOnce( observed.value, options.deadline.signal, ); @@ -104,57 +104,6 @@ export const connectGhidraSocket = async ( ); }; -/** Open one abort-aware connection to an observed local Ghidra endpoint. */ -const connectGhidraSocketOnce = ( - target: GhidraConnectTarget, - signal: AbortSignal, -): Promise> => - new Promise((resolve) => { - const socket = - "path" in target - ? createConnection(target.path) - : createConnection({ host: target.host, port: target.port }); - const detach = (): void => { - socket.off("connect", onConnect); - socket.off("error", onError); - signal.removeEventListener("abort", onAbort); - }; - const onConnect = (): void => { - detach(); - resolve(ok(socket)); - }; - const onError = (cause: Error): void => { - detach(); - socket.destroy(); - resolve( - err( - new GhidraSessionError("start", "Ghidra bridge connection failed", { - endpoint: target, - error: cause.message, - }), - ), - ); - }; - const onAbort = (): void => { - detach(); - socket.destroy(); - resolve( - err( - new GhidraSessionError( - "cancelled", - "Ghidra bridge connection was cancelled", - { endpoint: target }, - { cause: signal.reason, cancellation: { reason: signal.reason } }, - ), - ), - ); - }; - socket.once("connect", onConnect); - socket.once("error", onError); - signal.addEventListener("abort", onAbort, { once: true }); - if (signal.aborted) onAbort(); - }); - const interruptionFailure = ( options: GhidraSocketConnectOptions, cause?: GhidraSessionError, diff --git a/src/ghidra/GhidraTemporaryDirectory.test.ts b/src/ghidra/GhidraTemporaryDirectory.test.ts index f30f078ea4..31442dc58d 100644 --- a/src/ghidra/GhidraTemporaryDirectory.test.ts +++ b/src/ghidra/GhidraTemporaryDirectory.test.ts @@ -1,66 +1,60 @@ -import { afterEach, describe, expect, it, vi } from "vitest"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import { tmpdir } from "node:os"; import { dirname, resolve } from "node:path"; import * as fs from "node:fs/promises"; import { createTestTempDirectory } from "../../tests/fixtures/temporaryDirectory.js"; -import type { RetainedResourceScope } from "../application/AnalysisProvider.js"; - import { createInspectionResources } from "../../tests/fixtures/inspectionResources.js"; import { ghidraJavaHomeSaveDenial, probeGhidraTemporaryDirectory, - type GhidraTemporaryDirectoryHost, } from "./GhidraTemporaryDirectory.js"; import { ghidraSessionRoot } from "./GhidraSessionRoot.js"; vi.mock("node:fs/promises", async (importOriginal) => { const actual = await importOriginal(); - return { ...actual, open: vi.fn(actual.open) }; + return { ...actual, open: vi.fn(actual.open), unlink: vi.fn(actual.unlink) }; }); const denied = (code: string): NodeJS.ErrnoException => Object.assign(new Error(`${code}: operation not permitted`), { code }); let resources = createInspectionResources(); +let directory: string; +beforeEach(async () => { + directory = await createTestTempDirectory("rea-ghidra-probe-"); + for (const name of ["TMPDIR", "TMP", "TEMP"]) vi.stubEnv(name, directory); +}); afterEach(async () => { await resources.close().catch(() => undefined); resources = createInspectionResources(); -}); - -const host = ( - overrides: Partial = {}, -): GhidraTemporaryDirectoryHost => ({ - sessionRoot: () => "C:\\sandbox\\tmp", - writeProbe: (_path: string, _resourceOwner: RetainedResourceScope) => - Promise.resolve(), - removeProbe: () => Promise.resolve(), - ...overrides, + vi.mocked(fs.open).mockReset(); + vi.mocked(fs.unlink).mockReset(); + vi.unstubAllEnvs(); }); describe("Ghidra temporary directory probe", () => { - it("reports success only after the probe file is written and removed", async () => { - const removeProbe = vi.fn(() => Promise.resolve()); + it("writes and removes a probe in the session root", async () => { await expect( - probeGhidraTemporaryDirectory(resources.owner, host({ removeProbe })), - ).resolves.toEqual({ ok: true, directory: "C:\\sandbox\\tmp" }); - expect(removeProbe).toHaveBeenCalledTimes(1); + probeGhidraTemporaryDirectory(resources.owner), + ).resolves.toEqual({ + ok: true, + directory, + }); + const path = vi.mocked(fs.unlink).mock.calls[0]?.[0]; + expect(path).toBeDefined(); + await expect(fs.access(path!)).rejects.toMatchObject({ code: "ENOENT" }); + await resources.close(); + expect(fs.unlink).toHaveBeenCalledTimes(1); }); it("fails closed with the system code when the write is denied", async () => { - const removeProbe = vi.fn(() => Promise.reject(denied("ENOENT"))); - const result = await probeGhidraTemporaryDirectory( - resources.owner, - host({ - writeProbe: (_path: string, _resourceOwner: RetainedResourceScope) => - Promise.reject(denied("EPERM")), - removeProbe, - }), - ); + vi.mocked(fs.open).mockRejectedValueOnce(denied("EPERM")); + const result = await probeGhidraTemporaryDirectory(resources.owner); expect(result).toMatchObject({ ok: false, - directory: "C:\\sandbox\\tmp", + directory, stage: "write", code: "EPERM", }); @@ -68,83 +62,49 @@ describe("Ghidra temporary directory probe", () => { }); it("probes the Ghidra session root rather than an unusable inherited temp", async () => { - const writeProbe = vi.fn( - (_path: string, _resourceOwner: RetainedResourceScope) => - Promise.resolve(), + const dotted = resolve(directory, ".cache", "tmp"); + for (const name of ["TMPDIR", "TMP", "TEMP"]) vi.stubEnv(name, dotted); + const expected = ghidraSessionRoot(); + expect(resolve(tmpdir())).toBe(dotted); + expect(expected).not.toBe(dotted); + await expect( + probeGhidraTemporaryDirectory(resources.owner), + ).resolves.toEqual({ + ok: true, + directory: expected, + }); + expect(dirname(String(vi.mocked(fs.open).mock.calls[0]?.[0]))).toBe( + expected, ); - const names = ["TMPDIR", "TMP", "TEMP"] as const; - const previous = names.map((name) => process.env[name]); - const dotted = resolve("work", ".cache", "tmp"); - for (const name of names) process.env[name] = dotted; - let expected: string; - try { - expected = ghidraSessionRoot(); - expect(resolve(tmpdir())).toBe(dotted); - expect(expected).not.toBe(dotted); - await expect( - probeGhidraTemporaryDirectory(resources.owner, { - writeProbe, - removeProbe: () => Promise.resolve(), - }), - ).resolves.toEqual({ ok: true, directory: expected }); - } finally { - names.forEach((name, index) => { - const value = previous[index]; - if (value === undefined) delete process.env[name]; - else process.env[name] = value; - }); - } - expect(dirname(writeProbe.mock.calls[0]?.[0] ?? "")).toBe(expected); - expect(writeProbe.mock.calls[0]?.[1]).toBe(resources.owner); }); it.each([ - [ - "a failed write", - "write", - (_path: string, _resourceOwner: RetainedResourceScope) => - Promise.reject(denied("EIO")), - ], - [ - "a written probe", - "cleanup", - (_path: string, _resourceOwner: RetainedResourceScope) => - Promise.resolve(), - ], + ["a failed write", "write", true], + ["a written probe", "cleanup", false], ] as const)( "retains cleanup ownership of %s whose removal fails and retries it on close", - async (_label, stage, writeProbe) => { - const removeProbe = vi - .fn<(path: string) => Promise>() + async (_label, stage, writeFails) => { + if (writeFails) vi.mocked(fs.open).mockRejectedValueOnce(denied("EIO")); + vi.mocked(fs.unlink) .mockRejectedValueOnce(denied("EPERM")) - .mockRejectedValueOnce(denied("EPERM")) - .mockResolvedValue(undefined); - const result = await probeGhidraTemporaryDirectory( - resources.owner, - host({ writeProbe, removeProbe }), - ); + .mockRejectedValueOnce(denied("EPERM")); + const result = await probeGhidraTemporaryDirectory(resources.owner); expect(result).toMatchObject({ ok: false, stage, retainedPath: expect.stringContaining("rea-doctor-probe-"), }); const retainedPath = result.ok ? undefined : result.retainedPath; - const callsBeforeClose = removeProbe.mock.calls.length; await expect(resources.close()).rejects.toThrow(); await expect(resources.close()).resolves.toBeUndefined(); - expect(removeProbe.mock.calls.slice(callsBeforeClose)).toEqual([ - [retainedPath], - [retainedPath], - ]); + expect(fs.unlink).toHaveBeenCalledTimes(3); + expect(fs.unlink).toHaveBeenLastCalledWith(retainedPath); + if (retainedPath !== undefined) + await expect(fs.access(retainedPath)).rejects.toMatchObject({ + code: "ENOENT", + }); }, ); - - it("does not retain cleanup when the probe was removed", async () => { - const removeProbe = vi.fn(() => Promise.resolve()); - await probeGhidraTemporaryDirectory(resources.owner, host({ removeProbe })); - await resources.close(); - expect(removeProbe).toHaveBeenCalledTimes(1); - }); }); describe("Ghidra java_home.save denial", () => { @@ -202,7 +162,6 @@ it.each([ ])( "retains the default probe descriptor through $label", async ({ writeFails, closeFails, removeFails }) => { - const directory = await createTestTempDirectory("rea-ghidra-probe-owner-"); const { open } = await vi.importActual( "node:fs/promises", @@ -226,16 +185,10 @@ it.each([ vi.spyOn(file, "writeFile").mockRejectedValueOnce(writeFailure); return file; }); - let removalAttempts = 0; + if (removeFails) + vi.mocked(fs.unlink).mockRejectedValueOnce(denied("EPERM")); try { - const result = await probeGhidraTemporaryDirectory(resources.owner, { - sessionRoot: () => directory, - removeProbe: async (path) => { - removalAttempts += 1; - if (removeFails && removalAttempts === 1) throw denied("EPERM"); - await fs.unlink(path); - }, - }); + const result = await probeGhidraTemporaryDirectory(resources.owner); if (writeFails || closeFails) { expect(result).toMatchObject({ ok: false, @@ -248,7 +201,7 @@ it.each([ await resources.close(); expect(descriptor?.fd).toBe(-1); expect(closeAttempts).toBe(closeFails ? 2 : 1); - expect(removalAttempts).toBe(removeFails ? 2 : 1); + expect(fs.unlink).toHaveBeenCalledTimes(removeFails ? 2 : 1); } finally { opening.mockRestore(); if (descriptor !== undefined && descriptor.fd >= 0) diff --git a/src/ghidra/GhidraTemporaryDirectory.ts b/src/ghidra/GhidraTemporaryDirectory.ts index 68b6f33c1f..310e66362d 100644 --- a/src/ghidra/GhidraTemporaryDirectory.ts +++ b/src/ghidra/GhidraTemporaryDirectory.ts @@ -34,37 +34,26 @@ export type GhidraTemporaryDirectoryProbe = readonly retainedPath?: string; }; -/** Filesystem seam so classification is testable without a restricted host. */ -export interface GhidraTemporaryDirectoryHost { - readonly sessionRoot: () => string; - readonly writeProbe: ( - path: string, - resourceOwner: RetainedResourceScope, - ) => Promise; - readonly removeProbe: (path: string) => Promise; -} - -const systemHost: GhidraTemporaryDirectoryHost = { - sessionRoot: () => ghidraSessionRoot(), - writeProbe: async (path, resourceOwner) => { - const resources = new ArtifactResourceScope(); - try { - await withArtifactFileHandle( +const writeProbe = async ( + path: string, + resourceOwner: RetainedResourceScope, +) => { + const resources = new ArtifactResourceScope(); + try { + await withArtifactFileHandle( + path, + await open(path, "wx", 0o600), + resources, + undefined, + (handle) => handle.writeFile("rea doctor probe\n"), + ); + } finally { + if (resources.hasPending) + resourceOwner.retain( + { close: () => resources.close(), cleanupResources: [path] }, path, - await open(path, "wx", 0o600), - resources, - undefined, - (handle) => handle.writeFile("rea doctor probe\n"), ); - } finally { - if (resources.hasPending) - resourceOwner.retain( - { close: () => resources.close(), cleanupResources: [path] }, - path, - ); - } - }, - removeProbe: unlink, + } }; const errorCode = (cause: unknown): string | undefined => @@ -84,19 +73,17 @@ const errorMessage = (cause: unknown): string => */ export const probeGhidraTemporaryDirectory = async ( resourceOwner: RetainedResourceScope, - overrides: Partial = {}, ): Promise => { - const host = { ...systemHost, ...overrides }; - const directory = host.sessionRoot(); + const directory = ghidraSessionRoot(); const path = join(directory, `rea-doctor-probe-${randomUUID()}`); const remove = () => - host.removeProbe(path).catch((cause: unknown) => { + unlink(path).catch((cause: unknown) => { if (errorCode(cause) !== "ENOENT") throw cause; }); const retain = () => resourceOwner.retain({ close: remove, cleanupResources: [path] }, path); try { - await host.writeProbe(path, resourceOwner); + await writeProbe(path, resourceOwner); } catch (cause: unknown) { const cleanupOnly = cause instanceof ArtifactFileCleanupFailure && @@ -121,7 +108,7 @@ export const probeGhidraTemporaryDirectory = async ( }; } try { - await host.removeProbe(path); + await unlink(path); } catch (cause: unknown) { retain(); return { diff --git a/src/ghidra/extensions/nativeaot/NativeAotProducer.test.ts b/src/ghidra/extensions/nativeaot/NativeAotProducer.test.ts index 32cf250260..c2789c525e 100644 --- a/src/ghidra/extensions/nativeaot/NativeAotProducer.test.ts +++ b/src/ghidra/extensions/nativeaot/NativeAotProducer.test.ts @@ -5,6 +5,7 @@ import { validateNativeAotReport } from "./NativeAotReport.js"; it.skipIf(!process.env.JAVA_HOME || !process.env.GSON_JAR)( "accepts transaction reports from the Java producer with controlled APIs", + { tags: ["long-running"] }, async () => { const output = await runOwnedCommand( { @@ -37,5 +38,4 @@ it.skipIf(!process.env.JAVA_HOME || !process.env.GSON_JAR)( }), ).toBeNull(); }, - 90_000, ); diff --git a/src/go/GoBinaryImage.ts b/src/go/GoBinaryImage.ts index 9f86ee3581..e89856b93b 100644 --- a/src/go/GoBinaryImage.ts +++ b/src/go/GoBinaryImage.ts @@ -360,6 +360,11 @@ const readInlineExtent = ( ); length |= BigInt(byte & 127) << BigInt(index * 7); if ((byte & 128) === 0) { + if (length > BigInt(remaining - index - 1)) + throw new BinaryFormatFailure( + "malformed", + "Go build-info string range is truncated", + ); if (length > BigInt(maximumBytes)) throw new BinaryResourceFailure( "build-info", @@ -367,11 +372,6 @@ const readInlineExtent = ( `Go build-info string length exceeds max_build_info_bytes=${String(maximumBytes)}`, ); const size = Number(length); - if (size > remaining - index - 1) - throw new BinaryFormatFailure( - "malformed", - "Go build-info string range is truncated", - ); const start = offset + index + 1; return { location: { offset: start, bytes: size }, @@ -401,11 +401,10 @@ const readPointerString = ( const header = mappedFileOffset(mappings, address, width * 2, zeroFills); const valueAddress = reader.word(header, pointerBits); const length = reader.word(header + width, pointerBits); - if (length > BigInt(maximumBytes)) - throw new BinaryResourceFailure( - "build-info", - maximumBytes, - `Go build-info pointer string length exceeds max_build_info_bytes=${String(maximumBytes)}`, + if (length > BigInt(reader.bytes.length)) + throw new BinaryFormatFailure( + "malformed", + "Go build-info pointer string length exceeds the file extent", ); const size = Number(length); // Zero-length Go strings may carry a zero data pointer. @@ -413,6 +412,12 @@ const readPointerString = ( size === 0 && valueAddress === 0n ? header : mappedFileOffset(mappings, valueAddress, size, zeroFills); + if (length > BigInt(maximumBytes)) + throw new BinaryResourceFailure( + "build-info", + maximumBytes, + `Go build-info pointer string length exceeds max_build_info_bytes=${String(maximumBytes)}`, + ); return { bytes: reader.range(offset, size, "Go build-info pointer string"), location: { offset, bytes: size }, diff --git a/src/go/GoBinaryProvider.ts b/src/go/GoBinaryProvider.ts index 9f241cb446..b658d3635b 100644 --- a/src/go/GoBinaryProvider.ts +++ b/src/go/GoBinaryProvider.ts @@ -1,11 +1,11 @@ import { jsonValueByteLength } from "../domain/jsonValueByteLength.js"; import { GO_BINARY_PROVIDER_IDENTITY } from "./GoBinaryProviderIdentity.js"; import { + projectSelectedArtifactFileFailure, projectStableArtifactFailure, projectArtifactCloseFailure, stableArtifactInputRecovery, } from "../artifacts/StableArtifactFailure.js"; -import { isSelectedRegularFileError } from "../filesystem/RegularFile.js"; import { type StableArtifactSnapshot, readStableArtifact, @@ -22,7 +22,6 @@ import type { ExecutionOptions } from "../application/AnalysisProvider.js"; import type { GoBinaryPort } from "../application/go/GoBinaryPort.js"; import { AnalysisError } from "../domain/analysisErrorBase.js"; import { - AnalysisAccessDeniedError, AnalysisArtifactChangedError, AnalysisCancelledError, AnalysisInputError, @@ -153,20 +152,13 @@ const failureForPrimary = ( [{ path: ["path"], reason: "invalid_format", message: cause.message }], ); } - if (cause instanceof Error && "code" in cause) { - if (cause.code === "EACCES" || cause.code === "EPERM") - return new AnalysisAccessDeniedError(OPERATION, path, cause.code, { - cause, - }); - if (isSelectedRegularFileError(cause)) - return new AnalysisInputError(OPERATION, { cause }, [ - { - path: ["path"], - reason: "invalid_value", - message: `Selected artifact could not be read (${String(cause.code)}): ${path}.`, - }, - ]); - } + const fileFailure = projectSelectedArtifactFileFailure( + cause, + OPERATION, + path, + "artifact", + ); + if (fileFailure !== undefined) return fileFailure; return new ProviderAdapterError(GO_BINARY_PROVIDER_IDENTITY.id, OPERATION, { cause, diagnostics: { diff --git a/src/gpu/CudaInfo.ts b/src/gpu/CudaInfo.ts index 12e5e08b9f..cfe7b36d93 100644 --- a/src/gpu/CudaInfo.ts +++ b/src/gpu/CudaInfo.ts @@ -39,7 +39,7 @@ export class CudaMetadataBudget { this.#indexBytes += size; } } -export interface CudaInfoRecord { +interface CudaInfoRecord { readonly format: number; readonly attribute: number; readonly value: number; diff --git a/src/gpu/GpuElfKernels.ts b/src/gpu/GpuElfKernels.ts index e122d8bf65..116e6f3871 100644 --- a/src/gpu/GpuElfKernels.ts +++ b/src/gpu/GpuElfKernels.ts @@ -23,7 +23,7 @@ import { type NativeSection, type NativeSectionDirectory, } from "../native/metadata/NativeSectionDirectory.js"; -import { readElfString } from "../native/metadata/ElfStringTable.js"; +import { readElfStringBytes } from "../native/metadata/ElfStringTable.js"; import { readElfSymbol } from "../domain/elfSymbol.js"; import { gpuCount, @@ -321,7 +321,7 @@ const cudaKernels = async ( const kernel = blankKernel( source, `sym:${section.index}:${offset / width}`, - readElfString(names, symbol.name_offset), + readElfStringBytes(names, symbol.name_offset).toString("latin1"), site(source, section, offset, width), ); if ( diff --git a/src/gpu/GpuInventory.test.ts b/src/gpu/GpuInventory.test.ts index 269b14eb22..97efe073c2 100644 --- a/src/gpu/GpuInventory.test.ts +++ b/src/gpu/GpuInventory.test.ts @@ -150,3 +150,15 @@ it.each(["zlib", "zstd"] as const)( expect(observedReadBytes).toBeLessThan(2 * bytes.length); }, ); + +it("classifies an over-limit kernel symbol name as capacity", async () => { + const bytes = cubinFixture({ name: "x".repeat(65_536) }); + const inventory = new GpuInventory({ path: "/fixture", analysis: "kernels" }); + await inventory.inspect(new GpuBufferSource(bytes, "fixture")); + expect(inventory.objects[0]).toMatchObject({ + inspection: "capacity", + limitations: expect.arrayContaining([ + expect.stringContaining("65536-byte read allowance"), + ]), + }); +}); diff --git a/src/gpu/GpuInventory.ts b/src/gpu/GpuInventory.ts index b28d7cb833..2c48691fe3 100644 --- a/src/gpu/GpuInventory.ts +++ b/src/gpu/GpuInventory.ts @@ -289,7 +289,8 @@ export class GpuInventory { if (cause instanceof GpuReportCapacityFailure) throw cause; if ( !(cause instanceof BinaryFormatFailure) && - !(cause instanceof BinaryResourceFailure) + !(cause instanceof BinaryResourceFailure) && + !(cause instanceof BinaryStringReadLimitFailure) ) throw cause; row.inspection = diff --git a/src/gpu/PtxKernels.ts b/src/gpu/PtxKernels.ts index 4c7d2d270b..9de263787c 100644 --- a/src/gpu/PtxKernels.ts +++ b/src/gpu/PtxKernels.ts @@ -527,7 +527,7 @@ export function* readPtxKernels( if (outerDepth !== 0) rejectGpu("Unbalanced PTX body"); } -export interface PtxTargetFacts { +interface PtxTargetFacts { readonly recognized: boolean; readonly target: string | null; readonly limitation: string | null; diff --git a/src/hopper/BridgeLauncher.ts b/src/hopper/BridgeLauncher.ts index ce9dd6a050..485c7f2687 100644 --- a/src/hopper/BridgeLauncher.ts +++ b/src/hopper/BridgeLauncher.ts @@ -457,7 +457,7 @@ export class HopperApplicationLauncher implements BridgeLauncher { expectedLinuxSha256 = inspected.value.sha256; } const display = await this.#privateDisplay(signal); - if (!display.ok) return err(display.error); + if (!display.ok) return display; const nativeHelperVerified = (this.dependencies.platform ?? process.platform) === "darwin" && this.options.launchMode === "native" && diff --git a/src/hopper/HopperClient.ts b/src/hopper/HopperClient.ts index 7b8e61dbdc..fcc2af03ea 100644 --- a/src/hopper/HopperClient.ts +++ b/src/hopper/HopperClient.ts @@ -95,7 +95,7 @@ import { } from "./protocol.js"; /** Aggregate in-memory retention budget for this provider's launcher output. */ -export const HOPPER_PROCESS_DIAGNOSTIC_BYTES = 8 * 1024 * 1024; +const HOPPER_PROCESS_DIAGNOSTIC_BYTES = 8 * 1024 * 1024; const SESSION_ROOT = process.platform === "darwin" ? "/tmp" : tmpdir(); diff --git a/src/hopper/HopperProvider.test.ts b/src/hopper/HopperProvider.test.ts index a0ce805223..be44411491 100644 --- a/src/hopper/HopperProvider.test.ts +++ b/src/hopper/HopperProvider.test.ts @@ -313,8 +313,11 @@ describe("Hopper relationship coverage", () => { ), }); } finally { - await client.close(); - callTool.mockRestore(); + try { + await client.close(); + } finally { + callTool.mockRestore(); + } } }, ); @@ -406,8 +409,11 @@ describe("Hopper caller reconciliation failures", () => { }, }); } finally { - await client.close(); - callTool.mockRestore(); + try { + await client.close(); + } finally { + callTool.mockRestore(); + } } }); }); diff --git a/src/hopper/HopperRegexSearch.ts b/src/hopper/HopperRegexSearch.ts index f4828eb6e3..47fa60b5f4 100644 --- a/src/hopper/HopperRegexSearch.ts +++ b/src/hopper/HopperRegexSearch.ts @@ -55,10 +55,10 @@ export class HopperRegexSearch { signal, }); this.#active.set(controller, pending); - void pending.then( - () => this.#active.delete(controller), - () => this.#active.delete(controller), - ); + const remove = (): void => { + this.#active.delete(controller); + }; + void pending.then(remove, remove); return pending; } diff --git a/src/hopper/HopperRequestQueue.test.ts b/src/hopper/HopperRequestQueue.test.ts index 657ecc30c2..9ffff13110 100644 --- a/src/hopper/HopperRequestQueue.test.ts +++ b/src/hopper/HopperRequestQueue.test.ts @@ -163,7 +163,7 @@ describe("HopperRequestQueue deadlines", () => { await expect(queued).resolves.toEqual({ ok: false, error: failure }); expect(result).toHaveBeenCalledTimes(1); expect(queue.activity()).toBeNull(); - expect(queue.queuedCount()).toBe(0); + expect(queue.hasQueued(2)).toBe(false); expect(vi.getTimerCount()).toBe(0); }); }); @@ -197,7 +197,9 @@ describe("HopperRequestQueue deadline admission and races", () => { ok: false, error: { _tag: "HopperCancelledError" }, }); - expect(queue.queuedCount()).toBe(0); + expect(queue.hasQueued(2)).toBe(false); + expect(queue.hasQueued(3)).toBe(false); + expect(queue.activity()?.queuedRequests).toBe(0); queue.accept(1, ok(null)); await active; expect(sent).toEqual([1]); @@ -356,7 +358,7 @@ it("does not hand a waiting request to the sender until it reaches the wire", as const active = queue.run(1, "active", { value: "A" }, {}); const queued = queue.run(2, "queued", { value: "B" }, {}); expect(sent).toEqual([1]); - expect(queue.queuedCount()).toBe(1); + expect(queue.activity()?.queuedRequests).toBe(1); queue.accept(1, ok(null)); expect(sent).toEqual([1, 2]); queue.accept(2, ok(null)); diff --git a/src/hopper/HopperRequestQueue.ts b/src/hopper/HopperRequestQueue.ts index f5474fc142..2ba4b4bce3 100644 --- a/src/hopper/HopperRequestQueue.ts +++ b/src/hopper/HopperRequestQueue.ts @@ -15,12 +15,12 @@ import { import type { JsonValue } from "../domain/jsonValue.js"; import { err, type Result } from "../domain/result.js"; -export type HopperRequestResult = Result; +type HopperRequestResult = Result; /** Node timer ceiling; larger deadlines would be clamped instead of honored. */ const MAX_TIMEOUT_MS = 2_147_483_647; -export interface HopperRequestQueueOptions { +interface HopperRequestQueueOptions { readonly signal?: AbortSignal; /** Caller deadline from admission, including time waiting in the FIFO. */ readonly timeoutMs?: number; @@ -221,11 +221,6 @@ export class HopperRequestQueue { return this.#queue.some((entry) => entry.id === id); } - /** Number of caller requests waiting behind the active bridge operation. */ - queuedCount(): number { - return this.#queue.length; - } - #size(): number { return this.#queue.length + (this.#active === undefined ? 0 : 1); } diff --git a/src/hopper/HopperResponseStream.ts b/src/hopper/HopperResponseStream.ts index 6ec6f58b34..1cbb041719 100644 --- a/src/hopper/HopperResponseStream.ts +++ b/src/hopper/HopperResponseStream.ts @@ -4,7 +4,7 @@ import { } from "./protocol.js"; import { Utf8NdjsonLineBuffer } from "../process/Utf8NdjsonLineBuffer.js"; -export interface HopperResponseStreamOptions { +interface HopperResponseStreamOptions { readonly accept: (message: HopperBridgeMessage) => boolean; readonly hasQueued: (id: number) => boolean; readonly nextRequestId: () => number; diff --git a/src/hopper/HopperSocketConnection.ts b/src/hopper/HopperSocketConnection.ts index 77fe8ef300..c6cdd03508 100644 --- a/src/hopper/HopperSocketConnection.ts +++ b/src/hopper/HopperSocketConnection.ts @@ -1,40 +1,23 @@ -import { createConnection, type Socket } from "node:net"; +import type { Socket } from "node:net"; +import { connectLocalSocketOnce } from "../process/LocalSocketConnection.js"; import { hopperCancelledBySignal, type HopperCancelledError, HopperStartError, } from "../domain/hopperErrors.js"; -import { err, ok, type Result } from "../domain/result.js"; +import { err, type Result } from "../domain/result.js"; /** Open one abort-aware Hopper Unix-socket connection attempt. */ -export const connectHopperSocketOnce = ( +export const connectHopperSocketOnce = async ( socketPath: string, signal: AbortSignal, -): Promise> => - new Promise((resolve) => { - const socket = createConnection(socketPath); - const detach = (): void => { - socket.off("connect", onConnect); - socket.off("error", onError); - signal.removeEventListener("abort", onAbort); - }; - const onConnect = (): void => { - detach(); - resolve(ok(socket)); - }; - const onError = (cause: Error): void => { - detach(); - socket.destroy(); - resolve(err(new HopperStartError({ cause }))); - }; - const onAbort = (): void => { - detach(); - socket.destroy(); - resolve(err(hopperCancelledBySignal(signal))); - }; - socket.once("connect", onConnect); - socket.once("error", onError); - signal.addEventListener("abort", onAbort, { once: true }); - if (signal.aborted) onAbort(); - }); +): Promise> => { + const connected = await connectLocalSocketOnce({ path: socketPath }, signal); + if (connected.ok) return connected; + return err( + connected.error.kind === "cancelled" + ? hopperCancelledBySignal(signal) + : new HopperStartError({ cause: connected.error.cause }), + ); +}; diff --git a/src/ida/IdaConfiguration.test.ts b/src/ida/IdaConfiguration.test.ts index 9e05556f44..0b537f08c2 100644 --- a/src/ida/IdaConfiguration.test.ts +++ b/src/ida/IdaConfiguration.test.ts @@ -212,7 +212,6 @@ describe("IDA registration descriptor ownership", () => { retain: (resource) => retained.push(resource), run: (operation) => operation(), }; - const close = OwnedFileHandle.prototype.close; const reason = new Error("cancel while closing IDA registration"); const controller = new AbortController(); let entered!: () => void; diff --git a/src/ida/IdaMcpConnection.test.ts b/src/ida/IdaMcpConnection.test.ts index 7c2291f56c..d82a86970b 100644 --- a/src/ida/IdaMcpConnection.test.ts +++ b/src/ida/IdaMcpConnection.test.ts @@ -242,8 +242,8 @@ const mockedHttpBoundary = ({ it("preserves the SDK request timeout as a typed IDA timeout", async () => { const timeoutMs = 1000; - const requestTimeoutMs = 40; const pendingCall = vi.fn(); + const requestTimeoutMs = 40; mockedHttpBoundary({ serverName: "ida-timeout-fixture", tools: [], @@ -288,7 +288,6 @@ it("preserves the SDK request timeout as a typed IDA timeout", async () => { it("bounds a stalled health poll by the remaining readiness deadline", async () => { const timeoutMs = 800; - const pendingCall = vi.fn(); let clockNow = 0; const clock = { now: () => clockNow, diff --git a/src/ida/IdaSessionClient.test.ts b/src/ida/IdaSessionClient.test.ts index 5c90566c59..d0a15d47a6 100644 --- a/src/ida/IdaSessionClient.test.ts +++ b/src/ida/IdaSessionClient.test.ts @@ -11,6 +11,7 @@ import { import { basename, dirname, join, sep } from "node:path"; import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import { z } from "zod"; +import { awaitAcknowledgement } from "../../tests/fixtures/lifecycleAcknowledgement.js"; import { createIdaTarget, RecordingIdaMcp, @@ -780,7 +781,6 @@ describe("attached IDA regex supervision", () => { await client.close(); } }, - 15_000, ); it("bounds regex matching without caller cancellation and preserves subsequent searches", async () => { const { producer, client } = await fixture(); @@ -826,7 +826,7 @@ describe("attached IDA regex supervision", () => { } finally { await client.close(); } - }, 15_000); + }); }); describe("attached IDA function failure handling", () => { @@ -915,16 +915,26 @@ describe("attached IDA function failure handling", () => { { procedure: "main" }, { signal: controller.signal }, ); - await started; - controller.abort(); - const close = client.close(); - expect(producer.closes).toBe(0); - release?.(); - const result = await call; - expect(result.ok).toBe(false); - if (!result.ok) expect(result.error._tag).toBe("AnalysisCancelledError"); - expect((await close).ok).toBe(true); - expect(producer.closes).toBe(1); + try { + await awaitAcknowledgement(started, call, "IDA read-only call"); + controller.abort(); + const close = client.close(); + expect(producer.closes).toBe(0); + release?.(); + const result = await call; + expect(result.ok).toBe(false); + if (!result.ok) expect(result.error._tag).toBe("AnalysisCancelledError"); + expect((await close).ok).toBe(true); + expect(producer.closes).toBe(1); + } finally { + controller.abort(); + release?.(); + try { + await call; + } finally { + await client.close(); + } + } }); }); @@ -2322,7 +2332,7 @@ it("keeps the session cleanup owner until an ignored-abort HTTP acquisition and ); const executing = client.execute("health", {}); try { - await started; + await awaitAcknowledgement(started, executing, "IDA HTTP acquisition"); const result = await executing; expect(result).toMatchObject({ ok: false, diff --git a/src/il2cpp/Il2cppMetadataProvider.ts b/src/il2cpp/Il2cppMetadataProvider.ts index 1f89e6354d..8ee9b18a73 100644 --- a/src/il2cpp/Il2cppMetadataProvider.ts +++ b/src/il2cpp/Il2cppMetadataProvider.ts @@ -1,5 +1,4 @@ import { IL2CPP_METADATA_PROVIDER_IDENTITY } from "./Il2CppMetadataProviderIdentity.js"; -import { isSelectedRegularFileError } from "../filesystem/RegularFile.js"; import { Il2cppBinaryFormatFailure, IL2CPP_BINARY_BYTES, @@ -15,7 +14,10 @@ import { } from "../artifacts/readStableArtifact.js"; import { ArtifactResourceScope } from "../artifacts/ArtifactResourceScope.js"; import { ArtifactReaderFailure } from "../artifacts/ArtifactReader.js"; -import { stableArtifactInputRecovery } from "../artifacts/StableArtifactFailure.js"; +import { + projectSelectedArtifactFileFailure, + stableArtifactInputRecovery, +} from "../artifacts/StableArtifactFailure.js"; import { artifactFilePrimaryFacts, type ArtifactFilePrimaryFacts, @@ -33,7 +35,6 @@ import { createIl2cppMetadataAdmission } from "./Il2cppMetadataAdmission.js"; import { hexAddress as hex } from "./Il2cppBinaryImage.js"; import { jsonValueByteLength } from "../domain/jsonValueByteLength.js"; import { - AnalysisAccessDeniedError, AnalysisArtifactChangedError, AnalysisCancelledError, AnalysisCapabilityUnavailableError, @@ -154,20 +155,14 @@ const failureForPrimary = ( [{ path: [field], reason: "invalid_format", message: cause.message }], ); } - if (cause instanceof Error && "code" in cause) { - if (cause.code === "EACCES" || cause.code === "EPERM") - return new AnalysisAccessDeniedError(OPERATION, path, cause.code, { - cause, - }); - if (isSelectedRegularFileError(cause)) - return new AnalysisInputError(OPERATION, { cause }, [ - { - path: [field], - reason: "invalid_value", - message: `Selected artifact could not be read (${String(cause.code)}): ${path}.`, - }, - ]); - } + const fileFailure = projectSelectedArtifactFileFailure( + cause, + OPERATION, + path, + "artifact", + field, + ); + if (fileFailure !== undefined) return fileFailure; return new ProviderAdapterError( IL2CPP_METADATA_PROVIDER_IDENTITY.id, OPERATION, diff --git a/src/inspector/V8InspectorEndpoint.ts b/src/inspector/V8InspectorEndpoint.ts index 6a63745667..d9e19c3bed 100644 --- a/src/inspector/V8InspectorEndpoint.ts +++ b/src/inspector/V8InspectorEndpoint.ts @@ -4,7 +4,6 @@ import type { JavaScriptRuntimeTargetList, JavaScriptRuntimeTargetLocation, } from "../domain/javascript/javascriptRuntimeObservation.js"; -import { BrowserObservationError } from "../domain/browserObservationError.js"; import type { BrowserObservationOperation } from "../domain/browserObservationErrors.js"; import { urlDeclaresQueryOrFragment } from "../domain/urlComponents.js"; import { parseCdpEndpointValue, readCdpJson } from "../browser/CdpEndpoint.js"; diff --git a/src/javascript/analysis/JavaScriptAnalysisTransfer.worker.test.ts b/src/javascript/analysis/JavaScriptAnalysisTransfer.worker.test.ts index e0548f3d45..44a3caf812 100644 --- a/src/javascript/analysis/JavaScriptAnalysisTransfer.worker.test.ts +++ b/src/javascript/analysis/JavaScriptAnalysisTransfer.worker.test.ts @@ -45,6 +45,7 @@ it.each([ "utf8-cleanup-retry", ] as const)( "authenticates the physical worker transfer at the source-analysis port: %s", + { tags: ["long-running"] }, async (mode) => { const controller = new AbortController(); const cancellation = { reason: "cancel after physical transfer failure" }; @@ -153,5 +154,4 @@ it.each([ if (root === undefined) throw new Error("Worker root was not acquired"); await expect(lstat(root)).rejects.toMatchObject({ code: "ENOENT" }); }, - 40_000, ); diff --git a/src/javascript/sourceMaps/SourceMapCommand.test.ts b/src/javascript/sourceMaps/SourceMapCommand.test.ts index a0184a30a5..02cdc5aeab 100644 --- a/src/javascript/sourceMaps/SourceMapCommand.test.ts +++ b/src/javascript/sourceMaps/SourceMapCommand.test.ts @@ -1,6 +1,7 @@ import { ProcessAdmissionError } from "../../process/ProcessAdmissionError.js"; import { access } from "node:fs/promises"; -import { afterEach, expect, it, vi } from "vitest"; +import { afterEach, expect, it, onTestFinished, vi } from "vitest"; +import * as processOutput from "../../process/ExecFileOutput.js"; import { AnalysisCancelledError, AnalysisOutputError, @@ -95,15 +96,11 @@ it("preserves Linux process inspection failure and recovery before launching the const cause = Object.assign(new Error("ps permission denied"), { code: "EACCES", }); - const host = createSystemProcessOwnershipHost( - "linux", - {}, - { - execFileOutput: async () => { - throw cause; - }, - }, - ); + const command = vi + .spyOn(processOutput, "execFileOutput") + .mockRejectedValue(cause); + onTestFinished(() => command.mockRestore()); + const host = createSystemProcessOwnershipHost("linux", {}); const result = await runSourceMapCommand( "{}", "/probe/source.map", @@ -131,16 +128,14 @@ it("retains a physical process inspection failure when the caller cancels concur const physical = Object.assign(new Error("ps permission denied"), { code: "EACCES", }); - const host = createSystemProcessOwnershipHost( - "linux", - {}, - { - execFileOutput: async () => { - controller.abort(reason); - throw physical; - }, - }, - ); + const command = vi + .spyOn(processOutput, "execFileOutput") + .mockImplementation(async () => { + controller.abort(reason); + throw physical; + }); + onTestFinished(() => command.mockRestore()); + const host = createSystemProcessOwnershipHost("linux", {}); const result = await runSourceMapCommand( "{}", "/probe/source.map", diff --git a/src/jeb/JebMcpConnection.ts b/src/jeb/JebMcpConnection.ts index cf3a079a6f..50630fb3d8 100644 --- a/src/jeb/JebMcpConnection.ts +++ b/src/jeb/JebMcpConnection.ts @@ -246,14 +246,10 @@ export const createStreamableHttpJebMcpConnection: JebMcpConnectionFactory = ( } }, owner.signal.signal); owner.acquisition = acquisition; - void acquisition.promise.then( - () => { - if (owner.acquisition === acquisition) owner.acquisition = undefined; - }, - () => { - if (owner.acquisition === acquisition) owner.acquisition = undefined; - }, - ); + const clearAcquisition = (): void => { + if (owner.acquisition === acquisition) owner.acquisition = undefined; + }; + void acquisition.promise.then(clearAcquisition, clearAcquisition); return waitForAcquisition(owner, acquisition, signal); }; diff --git a/src/jeb/JebProvider.test.ts b/src/jeb/JebProvider.test.ts index 96885f50b6..d5e6e9498a 100644 --- a/src/jeb/JebProvider.test.ts +++ b/src/jeb/JebProvider.test.ts @@ -1,4 +1,5 @@ import { describe, expect, it, vi } from "vitest"; +import { awaitAcknowledgement } from "../../tests/fixtures/lifecycleAcknowledgement.js"; import { createJebAnalysisProvider } from "../composition/jeb.js"; import { runWithAnalysisOperationSnapshot } from "../domain/analysisOperationSnapshot.js"; @@ -551,6 +552,8 @@ it("forwards in-flight cancellation to the MCP call", async () => { async close() {}, async call(_name, _args, signal) { entered(); + if (signal!.aborted) + throw new AnalysisCancelledError(_name, { cause: signal!.reason }); await new Promise((_resolve, reject) => signal!.addEventListener( "abort", @@ -568,13 +571,22 @@ it("forwards in-flight cancellation to the MCP call", async () => { { operation: "inspect_jeb_client", input: {} }, { signal: controller.signal }, ); - await started; - controller.abort(); - const result = await pending; - expect(result.ok).toBe(false); - if (!result.ok) { - expect(result.error).toBeInstanceOf(AnalysisCancelledError); - expect(result.error.cause).toBe(controller.signal.reason); + try { + await awaitAcknowledgement(started, pending, "JEB MCP call"); + controller.abort(); + const result = await pending; + expect(result.ok).toBe(false); + if (!result.ok) { + expect(result.error).toBeInstanceOf(AnalysisCancelledError); + expect(result.error.cause).toBe(controller.signal.reason); + } + } finally { + controller.abort(); + try { + await pending; + } finally { + await provider.close(); + } } }); diff --git a/src/kuna/KunaCommand.ts b/src/kuna/KunaCommand.ts index dd32e1a3c2..da1a15a10a 100644 --- a/src/kuna/KunaCommand.ts +++ b/src/kuna/KunaCommand.ts @@ -62,7 +62,7 @@ export type KunaProcessLauncher = ( ) => Promise; /** One bounded Kuna run inside a private workspace. */ -export interface KunaCommandRequest { +interface KunaCommandRequest { readonly installation: KunaInstallation; readonly arguments: readonly string[]; /** Private directory used as the working and temporary directory. */ @@ -90,7 +90,7 @@ export interface KunaCommandRequest { * stopped, so it may still write into the workspace, which therefore stays * with the caller's cleanup owner. */ -export type KunaCommandOutcome = +type KunaCommandOutcome = | { readonly kind: "ok"; readonly snapshot: ProviderProcessSnapshot } | { readonly kind: "failed"; @@ -309,7 +309,7 @@ export interface KunaInputPrefix { } /** A copy's SHA-256 and size, and the leading bytes kept from the same stream. */ -export interface KunaCopiedInput { +interface KunaCopiedInput { readonly sha256: string; readonly bytes: number; /** Empty unless a prefix was requested; the digest covers these exact bytes. */ diff --git a/src/kuna/KunaInstallation.ts b/src/kuna/KunaInstallation.ts index 8da9bc60cd..91a5cae4a8 100644 --- a/src/kuna/KunaInstallation.ts +++ b/src/kuna/KunaInstallation.ts @@ -170,7 +170,7 @@ export const kunaInstallationCancellation = ( }; /** One installation inspection's retained owners and originating errors. */ -export interface KunaResourceScope { +interface KunaResourceScope { readonly owner: RetainedResourceScope; readonly cleanupResources: readonly string[] | undefined; readonly hasPending: boolean; @@ -188,12 +188,10 @@ export const createKunaResourceScope = ( ): KunaResourceScope => { const leases = new Map< Parameters[0], - { readonly lease: AnalysisCleanupLease } + AnalysisCleanupLease >(); const pendingLeases = () => - [...leases.values()] - .map(({ lease }) => lease) - .filter((lease) => !lease.released); + [...leases.values()].filter((lease) => !lease.released); const bindPendingFailure = (error: AnalysisError) => { const retained = pendingLeases(); if (retained.length > 0) new AnalysisCleanupLeaseGroup(error, retained); @@ -236,7 +234,7 @@ export const createKunaResourceScope = ( owner: { retain: (owner, resource) => { const lease = AnalysisCleanupLease.forCloseOwner(owner, resource); - leases.set(owner, { lease }); + leases.set(owner, lease); parent.retain( { close: () => lease.release(), diff --git a/src/kuna/KunaProvider.ts b/src/kuna/KunaProvider.ts index 5978870dec..01ac21e993 100644 --- a/src/kuna/KunaProvider.ts +++ b/src/kuna/KunaProvider.ts @@ -44,7 +44,6 @@ import { kunaCapabilities } from "./KunaProviderCapabilities.js"; import { KUNA_PROVIDER_IDENTITY } from "./KunaProviderMetadata.js"; import { KUNA_COMMAND_REMEDIATION, - KUNA_PROVIDER_ID, KUNA_RELEASE_VERIFICATION_EXPLANATION, KUNA_UNVERIFIED_VERSION_REMEDIATION, KUNA_VERIFIED_VERSION, @@ -70,7 +69,6 @@ export class KunaProvider implements AnalysisProviderCandidate { readonly #heapRequest: RegexWorkerHeapRequest; readonly #platform: NodeJS.Platform; readonly #launcher: KunaProcessLauncher | undefined; - readonly #probeTimeoutMs: number | undefined; readonly #singleFunctionRuns: number | undefined; #cache: KunaProjectCache | undefined; #runSlots: KunaRunSlots | undefined; @@ -80,8 +78,6 @@ export class KunaProvider implements AnalysisProviderCandidate { options: { readonly platform?: NodeJS.Platform; readonly launcher?: KunaProcessLauncher; - /** Test seam for bounded installation-probe lifecycle cases. */ - readonly probeTimeoutMs?: number; /** Test seam for foreground native runs; production derives it from the host. */ readonly singleFunctionRuns?: number; /** @@ -105,7 +101,6 @@ export class KunaProvider implements AnalysisProviderCandidate { options.regexWorkerHeapRequest ?? resolveRegexWorkerHeapRequest(this.#environment); this.#launcher = options.launcher; - this.#probeTimeoutMs = options.probeTimeoutMs; this.#singleFunctionRuns = options.singleFunctionRuns; } @@ -225,9 +220,6 @@ export class KunaProvider implements AnalysisProviderCandidate { ...(this.#launcher === undefined ? {} : { processLauncher: this.#launcher }), - ...(this.#probeTimeoutMs === undefined - ? {} - : { probeTimeoutMs: this.#probeTimeoutMs }), }); } catch (cause: unknown) { const retainedCleanup = kunaRetainedResourceCleanupError( diff --git a/src/main/messages.ts b/src/main/messages.ts index 7a37a10d8b..cce3f16ef8 100644 --- a/src/main/messages.ts +++ b/src/main/messages.ts @@ -10,5 +10,6 @@ export const MCP_SHUTDOWN_FAILED = "REA could not close cleanly. End the REA process before starting it again."; export const SERVER_START_FAILED = "Do not repeat this startup. Tell the user REA could not start and ask them to restart the MCP client, then run `rea doctor`."; -export const MCP_HTTP_LISTEN_FAILED = - "REA could not listen for MCP HTTP connections on the selected host and port. Choose another --port or stop the process using it."; +/** The listener error owns recovery for its parsed failure; never its native cause text. */ +export const mcpHttpListenFailed = (remediation: string): string => + `REA could not listen for MCP HTTP connections. ${remediation}`; diff --git a/src/main/transport.ts b/src/main/transport.ts index 8090ae613a..536faeffa2 100644 --- a/src/main/transport.ts +++ b/src/main/transport.ts @@ -12,7 +12,7 @@ import type { OptionalProviderLoadResult } from "../application/OptionalObservat import { OwnedMcpServers } from "./OwnedMcpServers.js"; import { MCP_CONNECTION_START_FAILED, - MCP_HTTP_LISTEN_FAILED, + mcpHttpListenFailed, } from "./messages.js"; import { reportMcpTransportError } from "./reportMcpTransportError.js"; @@ -181,10 +181,7 @@ export const startMcpTransport = async ( if (!cancelled) { const startFailure = cause instanceof StreamableHttpListenError - ? cause.classification.ok && - cause.classification.value.kind === "host_allowlist" - ? cause.remediationAction - : MCP_HTTP_LISTEN_FAILED + ? mcpHttpListenFailed(cause.remediationAction) : MCP_CONNECTION_START_FAILED; serverLogger.error( { diff --git a/src/mcpDoctor.cleanup.test.ts b/src/mcpDoctor.cleanup.test.ts new file mode 100644 index 0000000000..412901098d --- /dev/null +++ b/src/mcpDoctor.cleanup.test.ts @@ -0,0 +1,59 @@ +import { resolve } from "node:path"; + +import { expect, it, vi } from "vitest"; + +const cleanupDenial = vi.hoisted(() => ({ + reason: undefined as string | undefined, +})); + +// Keep real process-group cleanup, then report the selected denial so the +// doctor's late cleanup failure is observed without leaking the fixture. +vi.mock("./process/ProcessOwnership.js", async (importOriginal) => { + const original = + await importOriginal(); + return { + ...original, + cleanupOwnedProcessGroup: async ( + ...parameters: Parameters + ) => { + const released = await original.cleanupOwnedProcessGroup(...parameters); + return cleanupDenial.reason === undefined + ? released + : { cleaned: false as const, reason: cleanupDenial.reason }; + }, + }; +}); + +import { runProductionMcpDoctor } from "./mcpDoctor.js"; + +it("keeps the startup diagnostic and reports retained resources when cleanup fails", async () => { + cleanupDenial.reason = "fixture process group remained alive"; + try { + const result = await runProductionMcpDoctor({ + command: process.execPath, + args: [resolve("tests/fixtures/mcpDoctorExit.mjs")], + cwd: process.cwd(), + environment: { ...process.env, REA_MCP_DOCTOR_DEADLINE_MS: "5000" }, + }); + + expect(result).toMatchObject({ + healthy: false, + checks: [ + { name: "transport", ok: false }, + { + name: "cleanup", + ok: false, + detail: expect.stringMatching( + /^MCP stdio cleanup failed: Provider mcp-doctor adapter failed during mcp_doctor \(fixture process group remained alive\); retained resources: [0-9a-f-]{36}$/u, + ), + }, + ], + diagnostics: { + stderr: "fixture MCP startup exit\n", + stderr_truncated: false, + }, + }); + } finally { + cleanupDenial.reason = undefined; + } +}); diff --git a/src/mcpDoctor.ts b/src/mcpDoctor.ts index 6b1652bc38..4113e4577d 100644 --- a/src/mcpDoctor.ts +++ b/src/mcpDoctor.ts @@ -1,5 +1,4 @@ import { Client, SdkError, SdkErrorCode } from "@modelcontextprotocol/client"; -import { StdioClientTransport } from "@modelcontextprotocol/client/stdio"; import { Help } from "incur"; import { performance } from "node:perf_hooks"; import { resolve } from "node:path"; @@ -27,6 +26,7 @@ import { import { contractsForToolFamilies } from "./contracts/toolFamilies.js"; import { promptsForToolFamilies } from "./contracts/promptToolFamilyScope.js"; import { err, ok } from "./domain/result.js"; +import { AnalysisError } from "./domain/analysisErrorBase.js"; import { PRODUCT_IDENTITY } from "./identity.js"; import { parseMcpStartupBudgets, @@ -37,6 +37,7 @@ import { mcpDoctorDeadlineRecovery, } from "./mcpStartupPolicy.js"; import { MCP_DOCTOR_ALIAS, MCP_DOCTOR_OPTIONS } from "./mcpDoctorOptions.js"; +import { OwnedMcpStdioTransport } from "./process/OwnedMcpStdioTransport.js"; const OUTPUT_FORMATS = CLI_DOCUMENT_OUTPUT_FORMATS; type OutputFormat = (typeof OUTPUT_FORMATS)[number]; @@ -120,14 +121,20 @@ export const runProductionMcpDoctor = async (options: McpDoctorOptions) => { Math.max(1, Math.ceil(deadline - performance.now())), ); const stderr = captureStderr(); - const transport = new OwnedStdioClientTransport({ - command: options.command, - args: [...options.args], - cwd: options.cwd, - env: targetFreeEnvironment(options.environment), - stderr: "pipe", - }); - transport.stderr?.on("data", stderr.append); + const transport = new DoctorStdioTransport( + { + command: options.command, + arguments: [...options.args], + ...(options.cwd === undefined ? {} : { cwd: options.cwd }), + // The caller's environment is the child's complete base. The launcher + // would otherwise merge the ambient process environment, including the + // target selection the doctor removes. + hostEnvironment: targetFreeEnvironment(options.environment), + // A server can start its own providers in separate process groups. + sweepTokenOwnedProcesses: true, + }, + stderr.append, + ); const client = new Client({ name: "rea-mcp-doctor", version: "1" }); let result: ProductionMcpDoctorResult; let cleanupFailure: { readonly cause: unknown } | undefined; @@ -161,25 +168,13 @@ export const runProductionMcpDoctor = async (options: McpDoctorOptions) => { }; } finally { clearTimeout(timer); + // The owned transport is the only cleanup owner; Client.close would only + // delegate to it. An interrupted connect can also clear the client's + // transport reference before its startup child is closed. try { - await client.close(); - } catch (cause: unknown) { - cleanupFailure = { cause }; - } - try { - // An interrupted connect can clear the client's transport reference - // before its startup child is closed. The transport retains that child. await transport.close(); } catch (cause: unknown) { - cleanupFailure = { - cause: - cleanupFailure === undefined || cleanupFailure.cause === cause - ? cause - : new AggregateError( - [cleanupFailure.cause, cause], - `MCP client and transport cleanup failed: ${errorMessage(cleanupFailure.cause)}; ${errorMessage(cause)}`, - ), - }; + cleanupFailure = { cause }; } } if (cleanupFailure !== undefined) @@ -191,10 +186,12 @@ export const runProductionMcpDoctor = async (options: McpDoctorOptions) => { { name: "cleanup", ok: false, - detail: `MCP stdio cleanup failed: ${errorMessage(cleanupFailure.cause)}`, + detail: cleanupFailureDetail(cleanupFailure.cause), }, ], }; + // This reporter keeps the documented stderr prefix, including a withheld + // partial character; the transport's own retention serves cleanup errors. return { ...result, diagnostics: stderr.value() }; }; @@ -213,13 +210,25 @@ type ProductionMcpDoctorResult = readonly checks: readonly McpDoctorCheck[]; }; -/** Join SDK-triggered and caller-triggered close calls for the same child. */ -class OwnedStdioClientTransport extends StdioClientTransport { - #closePromise: Promise | undefined; - - override close(): Promise { - this.#closePromise ??= super.close(); - return this.#closePromise; +/** The doctor's bounded stdio session, owning the process family it starts. */ +class DoctorStdioTransport extends OwnedMcpStdioTransport { + constructor( + options: ConstructorParameters[0], + onStderr: (chunk: unknown) => void, + ) { + super(options, { + provider: "mcp-doctor", + operation: "mcp_doctor", + label: "MCP doctor", + maxFrameBytes: 8 * 1024 * 1024, + maxOperationBytes: 32 * 1024 * 1024, + // Retain only stderr, with the byte budget the report promises. + maxDiagnosticBytes: { + stdout: 0, + stderr: MCP_STARTUP_POLICY.doctorStderrBytes, + }, + onStderr, + }); } } @@ -315,7 +324,10 @@ const inspectProductionMcpSession = async ( ok: observedIdentity.packageVersion === PRODUCT_IDENTITY.packageVersion && observedIdentity.catalogDigest === catalog.digests.combined_sha256, - detail: `package=${observedIdentity.packageVersion ?? "missing"}; catalog=${observedIdentity.catalogDigest ?? "missing"}`, + // Identity is carried by the request-flow reply; without one it is unobserved, not missing. + detail: requestFlow.ok + ? `package=${observedIdentity.packageVersion ?? "missing"}; catalog=${observedIdentity.catalogDigest ?? "missing"}` + : `identity not observed: ${requestFlow.error.detail}`, }, { name: "request-flow", @@ -665,6 +677,20 @@ const isRecord = (value: unknown): value is Readonly> => const errorMessage = (cause: unknown): string => cause instanceof Error ? cause.message : String(cause); +/** Report the transport's typed cleanup reason and the resources it retains. */ +const cleanupFailureDetail = (cause: unknown): string => { + const detail = `MCP stdio cleanup failed: ${errorMessage(cause)}`; + if (!(cause instanceof AnalysisError) || !cause.cleanupIncomplete) + return detail; + const reason = cause.diagnostics?.reason; + return [ + typeof reason === "string" ? `${detail} (${reason})` : detail, + ...(cause.cleanupResources.length === 0 + ? [] + : [`retained resources: ${cause.cleanupResources.join(", ")}`]), + ].join("; "); +}; + /** * Name the applied doctor deadline when an owned abort or SDK request timeout * fires. Remote failure text is not a deadline. Codex's 30 second startup diff --git a/src/model/SafetensorsHeader.ts b/src/model/SafetensorsHeader.ts index 7593b16003..3a463423c2 100644 --- a/src/model/SafetensorsHeader.ts +++ b/src/model/SafetensorsHeader.ts @@ -1,3 +1,4 @@ +import { escapeJsonPointerToken } from "../domain/jsonPointer.js"; import { setImmediate } from "node:timers/promises"; import { assertArtifactReadActive } from "../artifacts/ArtifactFileOperation.js"; import { TextDecoder } from "node:util"; @@ -196,8 +197,6 @@ const exactOffset = (value: bigint): number => { ); return Number(value); }; -const pointer = (value: string): string => - value.replaceAll("~", "~0").replaceAll("/", "~1"); export const readSafetensorsHeader = async ( reader: ModelHeaderReader, @@ -271,7 +270,7 @@ export const readSafetensorsHeader = async ( element_count: null, preview_complete: true, ...source, - json_pointer: `/__metadata__/${pointer(key)}`, + json_pointer: `/__metadata__/${escapeJsonPointerToken(key)}`, }); } } @@ -336,7 +335,7 @@ export const readSafetensorsHeader = async ( byte_length_state: "declared", storage_validation: bits === undefined ? "unknown" : "consistent", ...source, - json_pointer: `/${pointer(name)}`, + json_pointer: `/${escapeJsonPointerToken(name)}`, }); } const ordered = [...tensors].sort( diff --git a/src/native/CommandRunner.ts b/src/native/CommandRunner.ts index b79878b1c8..a9a46c3a59 100644 --- a/src/native/CommandRunner.ts +++ b/src/native/CommandRunner.ts @@ -91,7 +91,7 @@ export class NativeCommandFailure extends Error { } /** Named cleanup ownership carried alongside the ordinary error cause. */ -export interface NativeCommandFailureOptions extends ErrorOptions { +interface NativeCommandFailureOptions extends ErrorOptions { /** Exact opaque reason recorded by the cancellation-owning boundary. */ readonly cancellation?: { readonly reason: unknown }; /** Typed owner retained when command process cleanup was incomplete. */ @@ -113,7 +113,7 @@ export interface NativeCommandCleanupFailure { } /** Partial output and lifecycle details retained when a native command fails. */ -export interface NativeCommandFailureCapture { +interface NativeCommandFailureCapture { readonly stdout: string; readonly stderr: string; readonly stdoutBytes: number; @@ -147,7 +147,7 @@ export type NativeCommandLauncher = ( ) => Promise; /** Resolve one native tool's selected invocation and prelaunch file identity. */ -export type NativeToolResolver = ( +type NativeToolResolver = ( tool: string, signal?: AbortSignal, launcher?: NativeCommandLauncher, diff --git a/src/native/NativeCallObservation.ts b/src/native/NativeCallObservation.ts index 9ac33a2a1b..f10c5dd7af 100644 --- a/src/native/NativeCallObservation.ts +++ b/src/native/NativeCallObservation.ts @@ -32,7 +32,7 @@ import type { NativeCallTrace, NativeCallTracer } from "./LldbCallTracer.js"; const OPERATION = "observe_native_calls"; /** What every call observation leaves unobserved or approximated. */ -export const NATIVE_CALL_OBSERVATION_LIMITATIONS = [ +const NATIVE_CALL_OBSERVATION_LIMITATIONS = [ "Only entries are observed; return values, floating-point and stack-passed arguments are not captured.", "Registers are the raw integer argument registers at entry. For Objective-C methods the first two hold the receiver and the selector.", "Receiver classes come from LLDB's Objective-C runtime reader without running target code; they are null for class methods and for receivers LLDB cannot classify.", @@ -42,7 +42,7 @@ export const NATIVE_CALL_OBSERVATION_LIMITATIONS = [ "The observation window is checked about once a second, and recording pauses the process at every entry, which slows it.", ] as const; -export const NATIVE_CALL_BREAKPOINT_LOCATION_LIST_INCOMPLETE_LIMITATION = +const NATIVE_CALL_BREAKPOINT_LOCATION_LIST_INCOMPLETE_LIMITATION = "The breakpoint location list is incomplete; location_count gives the full matching count."; const breakpointLocationBudgetLimitation = (bytes: number): string => `Resolved breakpoint locations exceeded capture_limits.breakpoint_location_bytes (${String(bytes)} bytes); omitted locations are counted in location_count. This metadata limit does not change the process outcome. Select a larger budget before a new authorized observation; earlier omitted facts cannot be recovered.`; diff --git a/src/native/NativeCommandDeadline.test.ts b/src/native/NativeCommandDeadline.test.ts index 4224b1e7a4..a26e3026e1 100644 --- a/src/native/NativeCommandDeadline.test.ts +++ b/src/native/NativeCommandDeadline.test.ts @@ -19,7 +19,6 @@ import type { NativeCommandRunner, } from "./CommandRunner.js"; import { XcrunCommandRunner } from "./CommandRunner.js"; -import { DEFAULT_NATIVE_COMMAND_TIMEOUT_MS } from "../domain/native/nativeDeadlines.js"; import { NativeMacOSProvider } from "./NativeMacOSProvider.js"; vi.mock("../process/OwnedCommand.js", async (original) => ({ @@ -32,7 +31,7 @@ afterEach(() => { }); class RecordingRunner implements NativeCommandRunner { - readonly timeouts: number[] = []; + readonly calls: { tool: string; timeoutMs: number | undefined }[] = []; readonly #inner = new NativeFixtureRunner(); run( @@ -40,7 +39,7 @@ class RecordingRunner implements NativeCommandRunner { arguments_: readonly string[], options: NativeCommandOptions, ) { - if (options.timeoutMs !== undefined) this.timeouts.push(options.timeoutMs); + this.calls.push({ tool, timeoutMs: options.timeoutMs }); return this.#inner.run(tool, arguments_, options); } } @@ -48,6 +47,7 @@ class RecordingRunner implements NativeCommandRunner { const execute = async ( runner: NativeCommandRunner, parameters: Readonly>, + operation: "list_architectures" | "inspect_signature" = "list_architectures", ) => { const root = await createTestTempDirectory("rea-native-deadline-"); const path = join(root, "app"); @@ -56,29 +56,50 @@ const execute = async ( await nativeMachoTargetForFile(path), ); try { - return await client.execute("list_architectures", parameters); + return await client.execute(operation, parameters); } finally { expect((await client.close()).ok).toBe(true); } }; -describe("native_command_timeout_ms", () => { - it("uses 60000 ms when the caller omits the setting", async () => { - const runner = new RecordingRunner(); - const result = await execute(runner, {}); - expect(result.ok, JSON.stringify(result)).toBe(true); - expect(runner.timeouts.every((timeout) => timeout === 60_000)).toBe(true); - expect(DEFAULT_NATIVE_COMMAND_TIMEOUT_MS).toBe(60_000); - expect(runner.timeouts.length).toBeGreaterThan(0); - }); +// Signature inspection reads display, requirements and entitlements, then both +// architecture states in the authored universal Mach-O fixture. +const commandCases = [ + { operation: "list_architectures", tools: ["lipo"] }, + { + operation: "inspect_signature", + tools: ["codesign", "codesign", "codesign", "codesign", "codesign"], + }, +] as const; - it("passes a selected deadline through to each native command", async () => { - const runner = new RecordingRunner(); - const result = await execute(runner, { native_command_timeout_ms: 1_500 }); - expect(result.ok, JSON.stringify(result)).toBe(true); - expect(runner.timeouts.length).toBeGreaterThan(0); - expect(runner.timeouts.every((timeout) => timeout === 1_500)).toBe(true); - }); +describe("native_command_timeout_ms", () => { + it.each(commandCases)( + "uses 60000 ms for every $operation command when the caller omits the setting", + async ({ operation, tools }) => { + const runner = new RecordingRunner(); + const result = await execute(runner, {}, operation); + expect(result.ok, JSON.stringify(result)).toBe(true); + expect(runner.calls).toEqual( + tools.map((tool) => ({ tool, timeoutMs: 60_000 })), + ); + }, + ); + + it.each(commandCases)( + "passes a selected deadline through every $operation command", + async ({ operation, tools }) => { + const runner = new RecordingRunner(); + const result = await execute( + runner, + { native_command_timeout_ms: 1_500 }, + operation, + ); + expect(result.ok, JSON.stringify(result)).toBe(true); + expect(runner.calls).toEqual( + tools.map((tool) => ({ tool, timeoutMs: 1_500 })), + ); + }, + ); it("passes the selected deadline through production xcrun resolution", async () => { const calls: { @@ -125,7 +146,7 @@ describe("native_command_timeout_ms", () => { const runner = new RecordingRunner(); const result = await execute(runner, { native_command_timeout_ms: 0 }); if (result.ok) throw new Error("expected an invalid deadline"); - expect(runner.timeouts).toEqual([]); + expect(runner.calls).toEqual([]); expect(projectAnalysisError(result.error)).toMatchObject({ code: "invalid_request", details: { diff --git a/src/native/NativeCommandPreparation.test.ts b/src/native/NativeCommandPreparation.test.ts index 242881a87e..b59a5a01b0 100644 --- a/src/native/NativeCommandPreparation.test.ts +++ b/src/native/NativeCommandPreparation.test.ts @@ -3,7 +3,10 @@ import { writeFile } from "node:fs/promises"; import { join } from "node:path"; import { failedInspectionDescriptor } from "../../tests/fixtures/processInspectionCleanup.js"; import { createTestTempDirectory } from "../../tests/fixtures/temporaryDirectory.js"; -import { nativeMachoTargetForFile } from "../../tests/fixtures/nativeCommands.js"; +import { + nativeMachoTargetForFile, + NativeFixtureRunner, +} from "../../tests/fixtures/nativeCommands.js"; import { fat } from "../domain/binaryTarget.fixture.js"; import { primaryAnalysisError } from "../domain/analysisErrorBase.js"; import { ProviderAdapterError } from "../domain/providerAdapterError.js"; @@ -18,7 +21,6 @@ import { import { NativeMacOSProvider } from "./NativeMacOSProvider.js"; import { XcrunCommandRunner, NativeCommandFailure } from "./CommandRunner.js"; import { LldbCallTracer } from "./LldbCallTracer.js"; -import { NativeFixtureRunner } from "../../tests/fixtures/nativeCommands.js"; import { err } from "../domain/result.js"; vi.mock("../process/OwnedCommand.js", async (original) => ({ diff --git a/src/native/NativeMachoInspection.ts b/src/native/NativeMachoInspection.ts index 764598f3b1..c97f3bed9a 100644 --- a/src/native/NativeMachoInspection.ts +++ b/src/native/NativeMachoInspection.ts @@ -109,7 +109,7 @@ const OPTIONAL_VTOOL_COMMAND = ["vtool", ["-show-build"]] as const; const VTOOL_UNAVAILABLE_LIMITATION = "vtool is unavailable; build metadata is normalized from binary load commands."; /** Recorded when vtool refuses the universal container and slice facts remain. */ -export const VTOOL_CONTAINER_REFUSAL_LIMITATION = +const VTOOL_CONTAINER_REFUSAL_LIMITATION = "vtool declined the universal container as not Mach-O; build metadata is normalized from binary load commands."; /** Inspect one Mach-O with the required native commands and normalized output. */ export const inspectNativeMacho = async ( @@ -122,7 +122,7 @@ export const inspectNativeMacho = async ( [...prefix, context.target.path], context.signal, ); - if (!captured.ok) return err(captured.error); + if (!captured.ok) return captured; captures.push(captured.value); } const lipo = captures.find(({ tool }) => tool === "lipo"); @@ -186,7 +186,7 @@ export const inspectNativeMacho = async ( vtoolSelectsSlice ? runSliceCommand(OPTIONAL_VTOOL_COMMAND) : null, ]); for (const captured of required) { - if (!captured.ok) return err(captured.error); + if (!captured.ok) return captured; if ( (captured.value.exitCode !== 0 || captured.value.signal !== null) && dyldSliceRefusal(captured.value) === null && @@ -296,7 +296,7 @@ export const inspectNativeMacho = async ( !namesEveryExport(captures[nmIndex]!.stdout, dyldExports) ) { const withDyldInfo = await runSliceCommand(DYLD_INFO_EXPORTS); - if (!withDyldInfo.ok) return err(withDyldInfo.error); + if (!withDyldInfo.ok) return withDyldInfo; captures[nmIndex] = withDyldInfo.value; } const dyldImports = captures.find( @@ -314,7 +314,7 @@ export const inspectNativeMacho = async ( ); if (parsedImports.needsNameInventory) { const importNames = await runSliceCommand(SYMBOL_TABLE_IMPORTS); - if (!importNames.ok) return err(importNames.error); + if (!importNames.ok) return importNames; captures.push(importNames.value); parsedImports = parseDyldImports( sliceOutput(dyldImports), diff --git a/src/native/NativeMachoOperand.test.ts b/src/native/NativeMachoOperand.test.ts index 5ac7a6b85c..46bb9fbb34 100644 --- a/src/native/NativeMachoOperand.test.ts +++ b/src/native/NativeMachoOperand.test.ts @@ -11,7 +11,6 @@ import type { NativeCallTracer } from "./LldbCallTracer.js"; import { NativeCommandFailure } from "./CommandRunner.js"; import { inspectMachoSchema } from "../domain/native/nativeInspection.js"; import { err, ok } from "../domain/result.js"; -import { VTOOL_CONTAINER_REFUSAL_LIMITATION } from "./NativeMachoInspection.js"; const targetDirectories: string[] = []; afterEach(async () => { @@ -158,7 +157,13 @@ describe("private otool operand lifecycle", () => { exit: { code: 1, signal: null }, }); expect(vtool?.command.at(-1)).toBe(alias); - expect(report.limitations).toContain(VTOOL_CONTAINER_REFUSAL_LIMITATION); + expect(report.limitations).toEqual( + expect.arrayContaining([ + expect.stringMatching( + /vtool.*universal container.*build metadata.*binary load commands/iu, + ), + ]), + ); expect( report.provenance.find((entry) => entry.tool === "lipo"), ).toMatchObject({ exit: { code: 0, signal: null } }); diff --git a/src/native/NativeMachoSelectedSlice.test.ts b/src/native/NativeMachoSelectedSlice.test.ts index ebea4487f5..9b5c06061d 100644 --- a/src/native/NativeMachoSelectedSlice.test.ts +++ b/src/native/NativeMachoSelectedSlice.test.ts @@ -3,7 +3,7 @@ import { assertSelectedMachoSlice, resolveSelectedMachoSlice, } from "./NativeMachoSelectedSlice.js"; -import { parseLipoOutput } from "./parsers/lipo.js"; +import { parseLipoOutputResult } from "./parsers/lipo.js"; import type { MachoCommandNames } from "./MachoCommandNames.js"; it("binds the lipo row to one FAT entry and its selected thin header", async () => { @@ -21,10 +21,12 @@ it("binds the lipo row to one FAT entry and its selected thin header", async () bytes.writeUInt32BE(64, at + 12); bytes.writeUInt32BE(4, at + 16); } - const lipo = parseLipoOutput( + const parsed = parseLipoOutputResult( `Fat header in: /fixture\nfat_magic 0xcafebabe\nnfat_arch 2\narchitecture arm64v8\ncputype CPU_TYPE_ARM64\ncpusubtype CPU_SUBTYPE_ARM64_V8\noffset 64\nsize 64\nalign 2^4\narchitecture arm64\ncputype CPU_TYPE_ARM64\ncpusubtype CPU_SUBTYPE_ARM64_ALL\noffset 128\nsize 64\nalign 2^4\n`, "/fixture", ); + if (!parsed.ok) throw parsed.error; + const lipo = parsed.value; const source = { bytes: bytes.length, readSlice: async ( diff --git a/src/native/NativeMachoVtoolRefusal.test.ts b/src/native/NativeMachoVtoolRefusal.test.ts index 5c5895979e..bfa5749d2e 100644 --- a/src/native/NativeMachoVtoolRefusal.test.ts +++ b/src/native/NativeMachoVtoolRefusal.test.ts @@ -6,10 +6,7 @@ import { NativeFixtureRunner, nativeMachoTarget, } from "../../tests/fixtures/nativeCommands.js"; -import { - inspectNativeMacho, - VTOOL_CONTAINER_REFUSAL_LIMITATION, -} from "./NativeMachoInspection.js"; +import { inspectNativeMacho } from "./NativeMachoInspection.js"; import { inspectionFatContext, inspectionReader, @@ -69,8 +66,12 @@ const refusal = "/usr/bin/vtool error: /fixture/universal file is not mach-o\n"; it("retains required facts and failed vtool provenance for its universal-container refusal", async () => { const result = await inspect({ diagnostic: refusal }); if (!result.ok) throw result.error; - expect(result.value.limitations).toContain( - VTOOL_CONTAINER_REFUSAL_LIMITATION, + expect(result.value.limitations).toEqual( + expect.arrayContaining([ + expect.stringMatching( + /vtool.*universal container.*build metadata.*binary load commands/iu, + ), + ]), ); expect(result.value.result).toMatchObject({ format: "mach-o-universal", diff --git a/src/native/apple/AppleIpsProvider.ts b/src/native/apple/AppleIpsProvider.ts index 9e26899ec0..d917fc52b5 100644 --- a/src/native/apple/AppleIpsProvider.ts +++ b/src/native/apple/AppleIpsProvider.ts @@ -14,11 +14,11 @@ import { } from "../../domain/native/appleIps.js"; import type { StableArtifactSnapshot } from "../../artifacts/readStableArtifact.js"; import { + projectSelectedArtifactFileFailure, projectStableArtifactFailure, stableArtifactInputRecovery, projectArtifactCloseFailure, } from "../../artifacts/StableArtifactFailure.js"; -import { isSelectedRegularFileError } from "../../filesystem/RegularFile.js"; import { ArtifactReaderFailure } from "../../artifacts/ArtifactReader.js"; import { readStableArtifact } from "../../artifacts/readStableArtifact.js"; import { @@ -32,7 +32,6 @@ import type { AppleIpsPort } from "../../application/binaryDiagnostics/AppleIpsP import type { ExecutionOptions } from "../../application/AnalysisProvider.js"; import { AnalysisError } from "../../domain/analysisErrorBase.js"; import { - AnalysisAccessDeniedError, AnalysisArtifactChangedError, AnalysisCancelledError, AnalysisInputError, @@ -332,20 +331,14 @@ const appleIpsFailurePrimary = ( [{ path: [field], reason: "invalid_format", message: cause.message }], ); } - if (cause instanceof Error && "code" in cause) { - if (cause.code === "EACCES" || cause.code === "EPERM") - return new AnalysisAccessDeniedError(OPERATION, input.path, cause.code, { - cause, - }); - if (isSelectedRegularFileError(cause)) - return new AnalysisInputError(OPERATION, { cause }, [ - { - path: [field], - reason: "invalid_value", - message: `Selected report could not be read (${String(cause.code)}): ${input.path}.`, - }, - ]); - } + const fileFailure = projectSelectedArtifactFileFailure( + cause, + OPERATION, + input.path, + "report", + field, + ); + if (fileFailure !== undefined) return fileFailure; return new ProviderAdapterError(APPLE_IPS_PROVIDER.id, OPERATION, { cause, diagnostics: { diff --git a/src/native/elf/ElfNotesProvider.ts b/src/native/elf/ElfNotesProvider.ts index 1aeaeed590..dbc875b738 100644 --- a/src/native/elf/ElfNotesProvider.ts +++ b/src/native/elf/ElfNotesProvider.ts @@ -1,9 +1,9 @@ import { + projectSelectedArtifactFileFailure, projectStableArtifactFailure, stableArtifactInputRecovery, projectArtifactCloseFailure, } from "../../artifacts/StableArtifactFailure.js"; -import { isSelectedRegularFileError } from "../../filesystem/RegularFile.js"; import { ArtifactReaderFailure } from "../../artifacts/ArtifactReader.js"; import { type StableArtifactSnapshot, @@ -19,7 +19,6 @@ import type { ElfNotesPort } from "../../application/binaryDiagnostics/ElfNotesP import type { ExecutionOptions } from "../../application/AnalysisProvider.js"; import { ArtifactOperationError } from "../../domain/artifactOperationError.js"; import { - AnalysisAccessDeniedError, AnalysisArtifactChangedError, AnalysisCancelledError, AnalysisInputError, @@ -212,20 +211,13 @@ const elfNotesFailurePrimary = ( ], ); } - if (cause instanceof Error && "code" in cause) { - if (cause.code === "EACCES" || cause.code === "EPERM") - return new AnalysisAccessDeniedError(OPERATION, input.path, cause.code, { - cause, - }); - if (isSelectedRegularFileError(cause)) - return new AnalysisInputError(OPERATION, { cause }, [ - { - path: ["path"], - reason: "invalid_value", - message: `Selected ELF image could not be read (${String(cause.code)}): ${input.path}.`, - }, - ]); - } + const fileFailure = projectSelectedArtifactFileFailure( + cause, + OPERATION, + input.path, + "ELF image", + ); + if (fileFailure !== undefined) return fileFailure; return new ArtifactOperationError( OPERATION, origin === "owned-or-classified" && cause instanceof ArtifactReaderFailure diff --git a/src/native/elf/ElfUnwindRangesProvider.ts b/src/native/elf/ElfUnwindRangesProvider.ts index 5245830c92..86044d8dad 100644 --- a/src/native/elf/ElfUnwindRangesProvider.ts +++ b/src/native/elf/ElfUnwindRangesProvider.ts @@ -1,9 +1,9 @@ import { + projectSelectedArtifactFileFailure, projectStableArtifactFailure, stableArtifactInputRecovery, projectArtifactCloseFailure, } from "../../artifacts/StableArtifactFailure.js"; -import { isSelectedRegularFileError } from "../../filesystem/RegularFile.js"; import { ArtifactReaderFailure } from "../../artifacts/ArtifactReader.js"; import { readStableArtifact } from "../../artifacts/readStableArtifact.js"; import { @@ -16,7 +16,6 @@ import type { ElfUnwindRangesPort } from "../../application/binaryDiagnostics/El import type { ExecutionOptions } from "../../application/AnalysisProvider.js"; import { ArtifactOperationError } from "../../domain/artifactOperationError.js"; import { - AnalysisAccessDeniedError, AnalysisArtifactChangedError, AnalysisCancelledError, AnalysisInputError, @@ -215,20 +214,13 @@ const elfUnwindFailurePrimary = ( ], ); } - if (cause instanceof Error && "code" in cause) { - if (cause.code === "EACCES" || cause.code === "EPERM") - return new AnalysisAccessDeniedError(OPERATION, input.path, cause.code, { - cause, - }); - if (isSelectedRegularFileError(cause)) - return new AnalysisInputError(OPERATION, { cause }, [ - { - path: ["path"], - reason: "invalid_value", - message: `Selected ELF image could not be read (${String(cause.code)}): ${input.path}.`, - }, - ]); - } + const fileFailure = projectSelectedArtifactFileFailure( + cause, + OPERATION, + input.path, + "ELF image", + ); + if (fileFailure !== undefined) return fileFailure; return new ArtifactOperationError( OPERATION, origin === "owned-or-classified" && cause instanceof ArtifactReaderFailure diff --git a/src/native/elf/KernelBuildConfigProvider.ts b/src/native/elf/KernelBuildConfigProvider.ts index 0075332321..0181da8151 100644 --- a/src/native/elf/KernelBuildConfigProvider.ts +++ b/src/native/elf/KernelBuildConfigProvider.ts @@ -1,9 +1,9 @@ import { + projectSelectedArtifactFileFailure, projectStableArtifactFailure, stableArtifactInputRecovery, projectArtifactCloseFailure, } from "../../artifacts/StableArtifactFailure.js"; -import { isSelectedRegularFileError } from "../../filesystem/RegularFile.js"; import { ArtifactReaderFailure } from "../../artifacts/ArtifactReader.js"; import { readStableArtifact } from "../../artifacts/readStableArtifact.js"; import { @@ -16,7 +16,6 @@ import type { KernelBuildConfigPort } from "../../application/binaryDiagnostics/ import type { ExecutionOptions } from "../../application/AnalysisProvider.js"; import { ArtifactOperationError } from "../../domain/artifactOperationError.js"; import { - AnalysisAccessDeniedError, AnalysisArtifactChangedError, AnalysisCancelledError, AnalysisInputError, @@ -199,20 +198,13 @@ const kernelBuildConfigFailurePrimary = ( ], ); } - if (cause instanceof Error && "code" in cause) { - if (cause.code === "EACCES" || cause.code === "EPERM") - return new AnalysisAccessDeniedError(OPERATION, input.path, cause.code, { - cause, - }); - if (isSelectedRegularFileError(cause)) - return new AnalysisInputError(OPERATION, { cause }, [ - { - path: ["path"], - reason: "invalid_value", - message: `Selected kernel image could not be read (${String(cause.code)}): ${input.path}.`, - }, - ]); - } + const fileFailure = projectSelectedArtifactFileFailure( + cause, + OPERATION, + input.path, + "kernel image", + ); + if (fileFailure !== undefined) return fileFailure; return new ArtifactOperationError( OPERATION, origin === "owned-or-classified" && cause instanceof ArtifactReaderFailure diff --git a/src/native/elf/KernelModuleProvider.ts b/src/native/elf/KernelModuleProvider.ts index 89a1ce53f7..4902614fdf 100644 --- a/src/native/elf/KernelModuleProvider.ts +++ b/src/native/elf/KernelModuleProvider.ts @@ -1,9 +1,9 @@ import { + projectSelectedArtifactFileFailure, projectStableArtifactFailure, stableArtifactInputRecovery, projectArtifactCloseFailure, } from "../../artifacts/StableArtifactFailure.js"; -import { isSelectedRegularFileError } from "../../filesystem/RegularFile.js"; import { ArtifactReaderFailure } from "../../artifacts/ArtifactReader.js"; import { type StableArtifactSnapshot, @@ -19,7 +19,6 @@ import type { KernelModulePort } from "../../application/binaryDiagnostics/Kerne import type { ExecutionOptions } from "../../application/AnalysisProvider.js"; import { ArtifactOperationError } from "../../domain/artifactOperationError.js"; import { - AnalysisAccessDeniedError, AnalysisArtifactChangedError, AnalysisCancelledError, AnalysisInputError, @@ -204,20 +203,13 @@ const kernelModuleFailurePrimary = ( ], ); } - if (cause instanceof Error && "code" in cause) { - if (cause.code === "EACCES" || cause.code === "EPERM") - return new AnalysisAccessDeniedError(OPERATION, input.path, cause.code, { - cause, - }); - if (isSelectedRegularFileError(cause)) - return new AnalysisInputError(OPERATION, { cause }, [ - { - path: ["path"], - reason: "invalid_value", - message: `Selected kernel module could not be read (${String(cause.code)}): ${input.path}.`, - }, - ]); - } + const fileFailure = projectSelectedArtifactFileFailure( + cause, + OPERATION, + input.path, + "kernel module", + ); + if (fileFailure !== undefined) return fileFailure; return new ArtifactOperationError( OPERATION, origin === "owned-or-classified" && cause instanceof ArtifactReaderFailure diff --git a/src/native/exports/NativeExportParser.test.ts b/src/native/exports/NativeExportParser.test.ts index 93e026b5be..2e3dedf364 100644 --- a/src/native/exports/NativeExportParser.test.ts +++ b/src/native/exports/NativeExportParser.test.ts @@ -10,7 +10,6 @@ import * as nativeExportContracts from "../../domain/native/nativeExports.js"; import { BinaryFormatFailure, BinaryResourceFailure, - BinaryStringReadLimitFailure, } from "../metadata/BinaryContainer.js"; import { parseNativeExports, @@ -873,6 +872,33 @@ describe("ELF export version and name coverage", () => { }); }); +it.each(["definition", "requirement"] as const)( + "retains an export with an over-limit version %s name as partial", + (kind) => { + const longName = "v".repeat(65_536); + const result = parse( + elfExportImage({ + symbols: [{ name: "kept", version: 2 }], + ...(kind === "definition" + ? { versionDefinitions: ["base", longName] } + : { versionRequirements: [{ index: 2, name: longName }] }), + }), + ); + expect(result.exports).toMatchObject([ + { name: "kept", version: { index: 2, name: null } }, + ]); + expect(result.coverage.status).toBe("partial"); + expect(result.issues).toEqual( + expect.arrayContaining([ + expect.objectContaining({ + code: "version_unresolved", + message: expect.stringContaining("65536-byte read allowance"), + }), + ]), + ); + }, +); + describe("ELF version requirements", () => { it("resolves defined copy-relocated symbols through version requirements", () => { const result = parse( @@ -1146,14 +1172,15 @@ describe("ELF symbol-name text", () => { expect(result.exports.map((entry) => entry.name)).toEqual( length === 65535 ? [name] : [], ); - if (length === 65536) + if (length === 65536) { + expect(result.coverage.status).toBe("partial"); expect(result.issues).toContainEqual( expect.objectContaining({ - message: expect.stringContaining( - new BinaryStringReadLimitFailure(65_536).message, - ), + code: "name_unreadable", + message: expect.stringContaining("65536-byte read allowance"), }), ); + } }, ); diff --git a/src/native/exports/NativeExportParser.ts b/src/native/exports/NativeExportParser.ts index 771716ffa0..e3de7aa47c 100644 --- a/src/native/exports/NativeExportParser.ts +++ b/src/native/exports/NativeExportParser.ts @@ -2,6 +2,8 @@ import { BinaryFormatFailure, BinaryReader, BinaryResourceFailure, + isBinaryFormatOrStringLimitFailure, + type BinaryStringReadLimitFailure, mappedFileOffset, imageFileMappings, readMappedCString, @@ -96,7 +98,7 @@ const coverage = ( }; const failureMessage = (cause: unknown): string => { - if (cause instanceof BinaryFormatFailure) return cause.message; + if (isBinaryFormatOrStringLimitFailure(cause)) return cause.message; throw cause; }; @@ -350,7 +352,7 @@ const createVersionNameReader = () => { const decodedNames = new Map< string, | { readonly value: DecodedElfString | null } - | { readonly failure: BinaryFormatFailure } + | { readonly failure: BinaryFormatFailure | BinaryStringReadLimitFailure } >(); let cacheBytes = 0; const chargeCache = (bytes: number): void => { @@ -373,7 +375,7 @@ const createVersionNameReader = () => { try { raw = readElfStringBytes(names, offset); } catch (cause) { - if (!(cause instanceof BinaryFormatFailure)) throw cause; + if (!isBinaryFormatOrStringLimitFailure(cause)) throw cause; // Invalid offsets/unterminated aliases must not repeatedly scan strings // either. Account for the cached error and its stack conservatively. chargeCache(4096); diff --git a/src/native/exports/NativeExportsProvider.ts b/src/native/exports/NativeExportsProvider.ts index 9e47ec9f21..7f6b0d2762 100644 --- a/src/native/exports/NativeExportsProvider.ts +++ b/src/native/exports/NativeExportsProvider.ts @@ -1,9 +1,10 @@ import { + imageFileSizeRemediation, + projectSelectedArtifactFileFailure, projectStableArtifactFailure, stableArtifactInputRecovery, projectArtifactCloseFailure, } from "../../artifacts/StableArtifactFailure.js"; -import { isSelectedRegularFileError } from "../../filesystem/RegularFile.js"; import { ArtifactReaderFailure } from "../../artifacts/ArtifactReader.js"; import { type StableArtifactSnapshot, @@ -20,7 +21,6 @@ import type { NativeExportsPort } from "../../application/binaryDiagnostics/Nati import type { ExecutionOptions } from "../../application/AnalysisProvider.js"; import { AnalysisError } from "../../domain/analysisErrorBase.js"; import { - AnalysisAccessDeniedError, AnalysisArtifactChangedError, AnalysisCancelledError, AnalysisInputError, @@ -86,11 +86,6 @@ export class NativeExportsProvider implements NativeExportsPort { } } -const fileSizeRemediation = (input: InspectNativeExportsInput): string => - input.max_file_bytes >= MAX_INSPECT_NATIVE_EXPORTS_FILE_BYTES - ? `The image exceeds the ${String(MAX_INSPECT_NATIVE_EXPORTS_FILE_BYTES)}-byte input ceiling this operation accepts. This release has no runtime override. Select a smaller artifact.` - : `Raise max_file_bytes above ${String(input.max_file_bytes)} bytes or select a smaller artifact; the limit is the caller-selected input read budget.`; - const nativeExportsFailurePrimary = ( facts: ArtifactFilePrimaryFacts, input: InspectNativeExportsInput, @@ -161,7 +156,10 @@ const nativeExportsFailurePrimary = ( { cause, retryable: false, - remediationAction: fileSizeRemediation(input), + remediationAction: imageFileSizeRemediation( + input.max_file_bytes, + MAX_INSPECT_NATIVE_EXPORTS_FILE_BYTES, + ), }, ); if (cause.reason === "path" || cause.reason === "format") @@ -171,20 +169,13 @@ const nativeExportsFailurePrimary = ( [{ path: ["path"], reason: "invalid_format", message: cause.message }], ); } - if (cause instanceof Error && "code" in cause) { - if (cause.code === "EACCES" || cause.code === "EPERM") - return new AnalysisAccessDeniedError(OPERATION, input.path, cause.code, { - cause, - }); - if (isSelectedRegularFileError(cause)) - return new AnalysisInputError(OPERATION, { cause }, [ - { - path: ["path"], - reason: "invalid_value", - message: `Selected image could not be read (${String(cause.code)}): ${input.path}.`, - }, - ]); - } + const fileFailure = projectSelectedArtifactFileFailure( + cause, + OPERATION, + input.path, + "image", + ); + if (fileFailure !== undefined) return fileFailure; return new ProviderAdapterError(NATIVE_EXPORTS_PROVIDER.id, OPERATION, { cause, diagnostics: { diff --git a/src/native/hardening/BinaryHardeningParsers.test.ts b/src/native/hardening/BinaryHardeningParsers.test.ts index d92673743a..3f7db3d8e1 100644 --- a/src/native/hardening/BinaryHardeningParsers.test.ts +++ b/src/native/hardening/BinaryHardeningParsers.test.ts @@ -1089,14 +1089,18 @@ it("leaves GNU property rows unknown behind a compressed note section", () => { }); }); -it("admits a hostile section table without rescanning every span", () => { - const started = performance.now(); - const image = readElfHardening( - elfHardeningFixture({ disjointNoteSections: 65000 }), - ); - expect(performance.now() - started).toBeLessThan(5000); - expect(findRow(image.rows, "cet_ibt_shstk").state).toBe("unknown"); -}, 60000); +it( + "admits a hostile section table without rescanning every span", + { tags: ["long-running"] }, + () => { + const started = performance.now(); + const image = readElfHardening( + elfHardeningFixture({ disjointNoteSections: 65000 }), + ); + expect(performance.now() - started).toBeLessThan(5000); + expect(findRow(image.rows, "cet_ibt_shstk").state).toBe("unknown"); + }, +); it("cites a non-ASCII import name by its source bytes", () => { const name = "__\u00ff_chk"; diff --git a/src/native/hardening/BinaryHardeningProvider.ts b/src/native/hardening/BinaryHardeningProvider.ts index 63acc5a161..9e798c3932 100644 --- a/src/native/hardening/BinaryHardeningProvider.ts +++ b/src/native/hardening/BinaryHardeningProvider.ts @@ -1,8 +1,10 @@ import { + imageFileSizeRemediation, + projectSelectedArtifactFileFailure, + stableArtifactInputRecovery, projectStableArtifactFailure, projectArtifactCloseFailure, } from "../../artifacts/StableArtifactFailure.js"; -import { isSelectedRegularFileError } from "../../filesystem/RegularFile.js"; import { ArtifactReaderFailure } from "../../artifacts/ArtifactReader.js"; import { type StableArtifactSnapshot, @@ -19,7 +21,6 @@ import type { BinaryHardeningPort } from "../../application/binaryDiagnostics/Bi import type { ExecutionOptions } from "../../application/AnalysisProvider.js"; import { AnalysisError } from "../../domain/analysisErrorBase.js"; import { - AnalysisAccessDeniedError, AnalysisArtifactChangedError, AnalysisCancelledError, AnalysisInputError, @@ -131,11 +132,6 @@ export const readBinaryHardening = async ( }; }; -const fileSizeRemediation = (input: InspectBinaryHardeningInput): string => - input.max_file_bytes >= MAX_INSPECT_BINARY_HARDENING_FILE_BYTES - ? `The image exceeds the ${String(MAX_INSPECT_BINARY_HARDENING_FILE_BYTES)}-byte input ceiling this operation accepts. This release has no runtime override. Select a smaller artifact.` - : `Raise max_file_bytes above ${String(input.max_file_bytes)} bytes or select a smaller artifact; the limit is the caller-selected input read budget.`; - const binaryHardeningFailurePrimary = ( facts: ArtifactFilePrimaryFacts, input: InspectBinaryHardeningInput, @@ -204,28 +200,26 @@ const binaryHardeningFailurePrimary = ( { cause, retryable: false, - remediationAction: fileSizeRemediation(input), + remediationAction: imageFileSizeRemediation( + input.max_file_bytes, + MAX_INSPECT_BINARY_HARDENING_FILE_BYTES, + ), }, ); if (cause.reason === "path" || cause.reason === "format") - return new AnalysisInputError(OPERATION, { cause }, [ - { path: ["path"], reason: "invalid_format", message: cause.message }, - ]); - } - if (cause instanceof Error && "code" in cause) { - if (cause.code === "EACCES" || cause.code === "EPERM") - return new AnalysisAccessDeniedError(OPERATION, input.path, cause.code, { - cause, - }); - if (isSelectedRegularFileError(cause)) - return new AnalysisInputError(OPERATION, { cause }, [ - { - path: ["path"], - reason: "invalid_value", - message: `Selected image could not be read (${String(cause.code)}): ${input.path}.`, - }, - ]); + return new AnalysisInputError( + OPERATION, + { cause, ...stableArtifactInputRecovery(cause) }, + [{ path: ["path"], reason: "invalid_format", message: cause.message }], + ); } + const fileFailure = projectSelectedArtifactFileFailure( + cause, + OPERATION, + input.path, + "image", + ); + if (fileFailure !== undefined) return fileFailure; return new ProviderAdapterError(BINARY_HARDENING_PROVIDER.id, OPERATION, { cause, diagnostics: { diff --git a/src/native/initializers/NativeInitializersParsers.test.ts b/src/native/initializers/NativeInitializersParsers.test.ts index 791396ce5f..2ed1730533 100644 --- a/src/native/initializers/NativeInitializersParsers.test.ts +++ b/src/native/initializers/NativeInitializersParsers.test.ts @@ -41,7 +41,6 @@ import { } from "./NativeInitializers.fixture.js"; import { MAX_NATIVE_INITIALIZER_ENTRIES, - MAX_NATIVE_INITIALIZER_LIMITATIONS, MAX_NATIVE_INITIALIZER_NAME_BYTES, MAX_NATIVE_INITIALIZER_NOTES, NATIVE_INITIALIZER_LIMITATIONS, @@ -516,106 +515,6 @@ describe("ELF section content admission", () => { ); }); -describe("ELF section content admission", () => { - it("keeps symbol relocations unresolved for a conflicting symbol entry width", async () => { - const bytes = elfRelocatableCtors(); - bytes.writeBigUInt64LE(16n, 0x500 + 4 * 64 + 56); - const report = await read(bytes); - expect( - report.entries.find((entry) => entry.kind === "init_array"), - ).toMatchObject({ - resolution: "unresolved", - value_va: null, - symbol: null, - }); - expect(report.status).toBe("incomplete"); - expect(report.limitations.join("\n")).toContain("entry size"); - }); - - it.each([ - { section: 7, label: "initializer array" }, - { section: 4, label: "symbol table" }, - { section: 5, label: "symbol string table" }, - { section: 8, label: "relocation table" }, - ])("preserves uncertainty for a compressed $label", async ({ section }) => { - const base = elfRelocatableCtors(); - const at = 0x500 + section * 64; - const offset = Number(base.readBigUInt64LE(at + 24)); - const size = Number(base.readBigUInt64LE(at + 32)); - const header = Buffer.alloc(24); - header.writeUInt32LE(1, 0); - header.writeBigUInt64LE(BigInt(size), 8); - header.writeBigUInt64LE(1n, 16); - const compressed = Buffer.concat([ - header, - deflateSync(base.subarray(offset, offset + size)), - ]); - const bytes = Buffer.concat([base, compressed]); - bytes.writeBigUInt64LE(base.readBigUInt64LE(at + 8) | 0x800n, at + 8); - bytes.writeBigUInt64LE(BigInt(base.length), at + 24); - bytes.writeBigUInt64LE(BigInt(compressed.length), at + 32); - const report = await read(bytes); - expect(report.status).toBe("incomplete"); - expect(report.coverage.total_entries).toBeNull(); - expect(report.limitations.join("\n")).toContain("compressed"); - if (section === 7) - expect(report.entries.some((entry) => entry.kind === "init_array")).toBe( - false, - ); - else { - const init = report.entries.find((entry) => entry.kind === "init_array"); - expect(init).toBeDefined(); - if (section === 5) expect(init?.symbol).toBeNull(); - else - expect(init).toMatchObject({ - resolution: "unresolved", - value_va: null, - }); - } - // The uncompressed fixture remains the working comparison. - expect((await read(base)).status).toBe("parsed"); - }); - - it("does not substitute an unrelated dynamic symbol table for DT_SYMTAB", async () => { - const bytes = elf64PieInitializers(); - bytes.writeBigUInt64LE(0x777n, 0x300 + 14 * 16 + 8); - const report = await read(bytes); - expect( - report.entries.find((entry) => entry.slot_va === "0x210"), - ).toMatchObject({ - resolution: "unresolved", - symbol: null, - value_va: null, - }); - expect(report.status).toBe("incomplete"); - }); - - it.each(["compressed", "not a string table"])( - "retains later symbol tables when an earlier name source is %s", - async (source) => { - const bytes = elf64PieInitializers(); - // The first (dynamic) symbol table links section 8; its bytes still contain imp_init. - if (source === "compressed") - bytes.writeBigUInt64LE(0x802n, 0x800 + 8 * 64 + 8); - else bytes.writeUInt32LE(1, 0x800 + 8 * 64 + 4); - const report = await read(bytes); - expect(report.status).toBe("incomplete"); - expect( - report.entries.find((entry) => entry.kind === "init"), - ).toMatchObject({ - symbol: { name: "ctor_a" }, - value_va: "0x100", - }); - expect( - report.entries.find((entry) => entry.slot_va === "0x210"), - ).toMatchObject({ - resolution: "unresolved_import", - symbol: null, - }); - }, - ); -}); - describe("ELF incomplete inputs", () => { it("reports an unsupported relocation machine as incomplete, not as raw zeros", async () => { const bytes = elf64PieInitializers(); @@ -931,9 +830,7 @@ describe("ELF repeated observations", () => { ).toEqual([ `${String(rows)} symbol name(s) exceeded the 1024-byte name budget and were not used.`, ]); - expect(report.limitations.length).toBeLessThanOrEqual( - MAX_NATIVE_INITIALIZER_LIMITATIONS, - ); + expect(report.limitations.length).toBeLessThanOrEqual(40); }); it("tallies relocation sections with an unexpected entry size", async () => { @@ -950,9 +847,7 @@ describe("ELF repeated observations", () => { ).toEqual([ `${String(sections)} relocation section(s) had an unexpected entry size and were not applied.`, ]); - expect(report.limitations.length).toBeLessThanOrEqual( - MAX_NATIVE_INITIALIZER_LIMITATIONS, - ); + expect(report.limitations.length).toBeLessThanOrEqual(40); }); it("keeps distinct notes to the budget and counts the rest", async () => { @@ -979,7 +874,7 @@ describe("ELF repeated observations", () => { // Standing limitations, the full note list and the withheld-name line fit the schema. expect( NATIVE_INITIALIZER_LIMITATIONS.length + lines.length + 1, - ).toBeLessThanOrEqual(MAX_NATIVE_INITIALIZER_LIMITATIONS); + ).toBeLessThanOrEqual(40); expect(notes.recorded).toBe(400); }); }); @@ -1026,45 +921,49 @@ describe("ELF section lookups", () => { ]); }); - it("resolves a full slot budget against a near-maximal section table in one pass", async () => { - const base = elf64PieInitializers(); - const slots = MAX_NATIVE_INITIALIZER_ENTRIES; - const payload = Buffer.alloc(slots * 8); - for (let slot = 0; slot < slots; slot += 1) - payload.writeBigUInt64LE(0x100n, slot * 8); - // Allocated, unnamed sections that cover every slot and target address. - const extra = Buffer.alloc((0xffff - 12) * 64); - for (let at = 0; at < extra.length; at += 64) { - extra.writeUInt32LE(1, at + 4); - extra.writeBigUInt64LE(2n, at + 8); - extra.writeBigUInt64LE(0x100000n, at + 32); - } - const bytes = Buffer.concat([ - base, - payload, - base.subarray(0x800, 0x800 + 12 * 64), - extra, - ]); - bytes.writeBigUInt64LE(BigInt(base.length + payload.length), 40); - bytes.writeUInt16LE(0xffff, 60); - // The single PT_LOAD maps the whole file; DT_INIT_ARRAY moves to the payload. - bytes.writeBigUInt64LE(BigInt(bytes.length), 64 + 32); - bytes.writeBigUInt64LE(BigInt(bytes.length), 64 + 40); - bytes.writeBigUInt64LE(BigInt(base.length), 0x300 + 2 * 16 + 8); - bytes.writeBigUInt64LE(BigInt(slots * 8), 0x300 + 3 * 16 + 8); - const started = performance.now(); - const report = await read(bytes); - // A per-lookup header walk is about 4e9 comparisons here; the index is a few hundred ms. - expect(performance.now() - started).toBeLessThan(10_000); - expect(report.coverage.examined_entries).toBe(slots); - const slot = report.entries.find((entry) => entry.kind === "init_array"); - expect(slot).toMatchObject({ - value_va: "0x100", - slot_section: null, - section: { name: ".text" }, - symbol: { name: "ctor_a" }, - }); - }, 180_000); + it( + "resolves a full slot budget against a near-maximal section table in one pass", + { tags: ["long-running"] }, + async () => { + const base = elf64PieInitializers(); + const slots = MAX_NATIVE_INITIALIZER_ENTRIES; + const payload = Buffer.alloc(slots * 8); + for (let slot = 0; slot < slots; slot += 1) + payload.writeBigUInt64LE(0x100n, slot * 8); + // Allocated, unnamed sections that cover every slot and target address. + const extra = Buffer.alloc((0xffff - 12) * 64); + for (let at = 0; at < extra.length; at += 64) { + extra.writeUInt32LE(1, at + 4); + extra.writeBigUInt64LE(2n, at + 8); + extra.writeBigUInt64LE(0x100000n, at + 32); + } + const bytes = Buffer.concat([ + base, + payload, + base.subarray(0x800, 0x800 + 12 * 64), + extra, + ]); + bytes.writeBigUInt64LE(BigInt(base.length + payload.length), 40); + bytes.writeUInt16LE(0xffff, 60); + // The single PT_LOAD maps the whole file; DT_INIT_ARRAY moves to the payload. + bytes.writeBigUInt64LE(BigInt(bytes.length), 64 + 32); + bytes.writeBigUInt64LE(BigInt(bytes.length), 64 + 40); + bytes.writeBigUInt64LE(BigInt(base.length), 0x300 + 2 * 16 + 8); + bytes.writeBigUInt64LE(BigInt(slots * 8), 0x300 + 3 * 16 + 8); + const started = performance.now(); + const report = await read(bytes); + // A per-lookup header walk is about 4e9 comparisons here; the index is a few hundred ms. + expect(performance.now() - started).toBeLessThan(10_000); + expect(report.coverage.examined_entries).toBe(slots); + const slot = report.entries.find((entry) => entry.kind === "init_array"); + expect(slot).toMatchObject({ + value_va: "0x100", + slot_section: null, + section: { name: ".text" }, + symbol: { name: "ctor_a" }, + }); + }, + ); }); describe("Mach-O symbol tables", () => { diff --git a/src/native/initializers/NativeInitializersProvider.ts b/src/native/initializers/NativeInitializersProvider.ts index 9386f327b2..db1e4c06c5 100644 --- a/src/native/initializers/NativeInitializersProvider.ts +++ b/src/native/initializers/NativeInitializersProvider.ts @@ -1,8 +1,10 @@ import { + imageFileSizeRemediation, + projectSelectedArtifactFileFailure, + stableArtifactInputRecovery, projectArtifactCloseFailure, projectStableArtifactFailure, } from "../../artifacts/StableArtifactFailure.js"; -import { isSelectedRegularFileError } from "../../filesystem/RegularFile.js"; import { ArtifactReaderFailure } from "../../artifacts/ArtifactReader.js"; import { readStableArtifact } from "../../artifacts/readStableArtifact.js"; import { @@ -16,7 +18,6 @@ import type { NativeInitializersPort } from "../../application/binaryDiagnostics import type { ExecutionOptions } from "../../application/AnalysisProvider.js"; import { AnalysisError } from "../../domain/analysisErrorBase.js"; import { - AnalysisAccessDeniedError, AnalysisArtifactChangedError, AnalysisCancelledError, AnalysisInputError, @@ -184,11 +185,6 @@ const architectureName = (cpuType: number): string => { } }; -const fileSizeRemediation = (input: InspectNativeInitializersInput): string => - input.max_file_bytes >= MAX_INSPECT_NATIVE_INITIALIZERS_FILE_BYTES - ? `The image exceeds the ${String(MAX_INSPECT_NATIVE_INITIALIZERS_FILE_BYTES)}-byte input ceiling this operation accepts. This release has no runtime override. Select a smaller artifact.` - : `Raise max_file_bytes above ${String(input.max_file_bytes)} bytes or select a smaller artifact; the limit is the caller-selected input read budget.`; - const failurePrimary = ( facts: ArtifactFilePrimaryFacts, input: InspectNativeInitializersInput, @@ -274,28 +270,26 @@ const failurePrimary = ( { cause, retryable: false, - remediationAction: fileSizeRemediation(input), + remediationAction: imageFileSizeRemediation( + input.max_file_bytes, + MAX_INSPECT_NATIVE_INITIALIZERS_FILE_BYTES, + ), }, ); if (cause.reason === "path" || cause.reason === "format") - return new AnalysisInputError(OPERATION, { cause }, [ - { path: ["path"], reason: "invalid_format", message: cause.message }, - ]); - } - if (cause instanceof Error && "code" in cause) { - if (cause.code === "EACCES" || cause.code === "EPERM") - return new AnalysisAccessDeniedError(OPERATION, input.path, cause.code, { - cause, - }); - if (isSelectedRegularFileError(cause)) - return new AnalysisInputError(OPERATION, { cause }, [ - { - path: ["path"], - reason: "invalid_value", - message: `Selected image could not be read (${String(cause.code)}): ${input.path}.`, - }, - ]); + return new AnalysisInputError( + OPERATION, + { cause, ...stableArtifactInputRecovery(cause) }, + [{ path: ["path"], reason: "invalid_format", message: cause.message }], + ); } + const fileFailure = projectSelectedArtifactFileFailure( + cause, + OPERATION, + input.path, + "image", + ); + if (fileFailure !== undefined) return fileFailure; return new ProviderAdapterError(NATIVE_INITIALIZERS_PROVIDER.id, OPERATION, { cause, diagnostics: { diff --git a/src/native/metadata/BinaryContainer.ts b/src/native/metadata/BinaryContainer.ts index a1ac3b8331..a587637ad0 100644 --- a/src/native/metadata/BinaryContainer.ts +++ b/src/native/metadata/BinaryContainer.ts @@ -41,17 +41,22 @@ export class BinaryResourceFailure extends Error { } /** A bounded read could not establish termination; it does not prove invalid text. */ -export class BinaryStringReadLimitFailure extends BinaryFormatFailure { +export class BinaryStringReadLimitFailure extends Error { constructor(readonly maximumBytes: number) { - // Preserve the existing broad failure contract for legacy callers while - // allowing coverage-aware consumers to distinguish a limit from corruption. super( - "malformed", `String termination was not established within its ${String(maximumBytes)}-byte read allowance`, ); + this.name = "BinaryStringReadLimitFailure"; } } +/** Recognized format failures and bounded string-read failures. */ +export const isBinaryFormatOrStringLimitFailure = ( + cause: unknown, +): cause is BinaryFormatFailure | BinaryStringReadLimitFailure => + cause instanceof BinaryFormatFailure || + cause instanceof BinaryStringReadLimitFailure; + /** Native loader region facts; language-specific fallback selection belongs to consumers. */ export interface LoadRegion { readonly mapping: FileMapping; diff --git a/src/native/metadata/ElfStringTable.test.ts b/src/native/metadata/ElfStringTable.test.ts index c26b8b184d..11a25d70ad 100644 --- a/src/native/metadata/ElfStringTable.test.ts +++ b/src/native/metadata/ElfStringTable.test.ts @@ -3,7 +3,7 @@ import { BinaryFormatFailure, BinaryStringReadLimitFailure, } from "./BinaryContainer.js"; -import { readElfString, readElfStringBytes } from "./ElfStringTable.js"; +import { readElfStringBytes } from "./ElfStringTable.js"; it("preserves empty and non-UTF8 string-table bytes", () => { const table = Buffer.from([0, 0xff, 0x61, 0]); @@ -11,7 +11,6 @@ it("preserves empty and non-UTF8 string-table bytes", () => { expect(readElfStringBytes(table, 1).equals(Buffer.from([0xff, 0x61]))).toBe( true, ); - expect(readElfString(table, 1)).toBe("\xffa"); }); it("reads a terminator inside the scan allowance at a nonzero table offset", () => { @@ -38,7 +37,7 @@ it.each([65_536, 70_000])( failure = cause; } expect(failure).toBeInstanceOf(BinaryStringReadLimitFailure); - expect(failure).toBeInstanceOf(BinaryFormatFailure); + expect(failure).not.toBeInstanceOf(BinaryFormatFailure); expect(failure).toMatchObject({ maximumBytes: 65_536 }); }, ); diff --git a/src/native/metadata/ElfStringTable.ts b/src/native/metadata/ElfStringTable.ts index 9f02f84a38..5386bd84cd 100644 --- a/src/native/metadata/ElfStringTable.ts +++ b/src/native/metadata/ElfStringTable.ts @@ -4,10 +4,6 @@ import { BinaryStringReadLimitFailure, } from "./BinaryContainer.js"; -export const readElfString = (bytes: Buffer, offset: number): string => { - return readElfStringBytes(bytes, offset).toString("latin1"); -}; - /** Read exact string-table bytes; ELF does not prescribe a character encoding. */ export const readElfStringBytes = (bytes: Buffer, offset: number): Buffer => { const observed = observeElfStringTableEntry(bytes, offset, 65536); diff --git a/src/native/parsers/lipo.test.ts b/src/native/parsers/lipo.test.ts index 0282b49018..8e09e321c3 100644 --- a/src/native/parsers/lipo.test.ts +++ b/src/native/parsers/lipo.test.ts @@ -1,6 +1,7 @@ import { describe, expect, it } from "vitest"; -import { parseLipoCapture, parseLipoOutput } from "./lipo.js"; +import { parseLipoCapture } from "./lipo.js"; +import { parseLipoFixture } from "../../../tests/fixtures/lipo.js"; const fatRecords = `fat_magic 0xcafebabe nfat_arch 2 @@ -64,7 +65,7 @@ describe("lipo numeric field observations", () => { "retains an explicit uninterpretable $field source: $source", ({ field, source }) => { const sourceLine = `\t${field} ${source} `; - const architecture = parseLipoOutput( + const architecture = parseLipoFixture( `architecture arm64\n cputype 16777228\n${sourceLine}\n`, ).architectures[0]; expect(architecture?.cpu_type_code).toBe(16777228); @@ -82,7 +83,7 @@ describe("lipo numeric field observations", () => { ); it("parses each valid numeric field once while preserving its exact source line", () => { - const architecture = parseLipoOutput( + const architecture = parseLipoFixture( "architecture arm64\n offset 4096 \n size 48\n align 2^12 (4096)\n", ).architectures[0]; expect(architecture).toMatchObject({ @@ -113,7 +114,7 @@ describe("lipo numeric field observations", () => { const conflicting = field === "align" ? "2^13 (8192)" : "8192"; for (const second of [first, conflicting, "unsupported"]) { const sourceLines = [` ${field} ${first}`, `\t${field} ${second} `]; - const row = parseLipoOutput( + const row = parseLipoFixture( `architecture arm64\n${sourceLines.join("\n")}\n`, ).architectures[0]; expect(row?.fields[field]).toEqual( @@ -150,7 +151,7 @@ it.each([ ])( "preserves unknown CPU names without inventing codes", ({ type, subtype, typeCode, subtypeCode, subtypeBase }) => { - const { architectures } = parseLipoOutput( + const { architectures } = parseLipoFixture( fatRecords .replace("CPU_TYPE_ARM64", type) .replace("CPU_SUBTYPE_ARM64_ALL", subtype), @@ -167,7 +168,7 @@ it.each([ describe("lipo architecture facts", () => { it("preserves Apple's declared and hidden ARM64 architecture scopes", () => { - const output = parseLipoOutput(`fat_magic 0xcafebabe + const output = parseLipoFixture(`fat_magic 0xcafebabe nfat_arch 1 (+1 hidden) architecture armv7 cputype CPU_TYPE_ARM @@ -193,7 +194,7 @@ architecture arm64 (hidden) }); it("normalizes symbolic CPU fields and arm64e capability bits", () => { - const { format, architectures } = parseLipoOutput(`Fat header in: /bin/ls + const { format, architectures } = parseLipoFixture(`Fat header in: /bin/ls fat_magic 0xcafebabe nfat_arch 2 architecture x86_64 @@ -241,7 +242,7 @@ architecture arm64e it("normalizes the CPU names lipo prints for i386 slices", () => { // cctools misc/lipo.c print_cputype() spells i386 with I386 names. expect( - parseLipoOutput(`architecture i386 + parseLipoFixture(`architecture i386 cputype CPU_TYPE_I386 cpusubtype CPU_SUBTYPE_I386_ALL capabilities 0x0 @@ -261,7 +262,7 @@ architecture arm64e it("keeps unknown symbolic CPU values as source text without a code", () => { expect( - parseLipoOutput( + parseLipoFixture( "architecture future\n cputype CPU_TYPE_FUTURE\n cpusubtype CPU_SUBTYPE_FUTURE\n offset 4096\n size 64\n align 2^12\n", ).architectures, ).toMatchObject([ @@ -281,7 +282,7 @@ architecture arm64e ["0x0", 3], ])("applies printed x86_64 capabilities %s", (capabilities, code) => { expect( - parseLipoOutput( + parseLipoFixture( `architecture x86_64\n cpusubtype CPU_SUBTYPE_X86_64_ALL\n capabilities ${capabilities}\n`, ).architectures[0]?.cpu_subtype_code, ).toBe(code); @@ -290,7 +291,7 @@ architecture arm64e it("leaves the subtype code unknown when lipo prints no capabilities", () => { // macOS 26 lipo prints this blank line for LIB64 and plain x86_64 slices. expect( - parseLipoOutput( + parseLipoFixture( "architecture x86_64\n cputype CPU_TYPE_X86_64\n cpusubtype CPU_SUBTYPE_X86_ALL\n capabilities \n offset 16384\n", ).architectures, ).toMatchObject([ @@ -310,7 +311,7 @@ architecture arm64e "retains an arm64e base subtype with capabilities '$capabilities'", ({ capabilities, code }) => { expect( - parseLipoOutput( + parseLipoFixture( `architecture arm64e.v1\n cputype CPU_TYPE_ARM64\n cpusubtype CPU_SUBTYPE_ARM64E\n capabilities ${capabilities}\n`, ).architectures, ).toMatchObject([ @@ -337,8 +338,8 @@ describe("lipo FAT operand framing", () => { (path) => { const output = `Fat header in: ${path}\n${fatRecords}`; - const parsed = parseLipoOutput(output, path); - expect(parsed).toEqual(parseLipoOutput(fatRecords)); + const parsed = parseLipoFixture(output, path); + expect(parsed).toEqual(parseLipoFixture(fatRecords)); expect(parsed.architectures.map(({ name }) => name)).toEqual([ "arm64", "x86_64", @@ -350,8 +351,8 @@ describe("lipo FAT operand framing", () => { const path = "/fixture/first\r\narchitecture forged\r\n size 64"; const output = `Fat header in: ${path}\r\n${fatRecords.replaceAll("\n", "\r\n")}`; - const parsed = parseLipoOutput(output, path); - expect(parsed).toEqual(parseLipoOutput(fatRecords)); + const parsed = parseLipoFixture(output, path); + expect(parsed).toEqual(parseLipoFixture(fatRecords)); expect(parsed.architectures).toMatchObject([ { name: "arm64", file_offset: 4096, size: 32 }, { name: "x86_64", file_offset: 8192, size: 32 }, @@ -362,12 +363,12 @@ describe("lipo FAT operand framing", () => { const path = "/fixture/app"; const output = `Fat header in: ${path}\n${fatRecords}`; - expect(parseLipoOutput(output, path)).toEqual(parseLipoOutput(output)); + expect(parseLipoFixture(output, path)).toEqual(parseLipoFixture(output)); }); it("does not require a FAT path header in captured fixtures", () => { - expect(parseLipoOutput(fatRecords, "/fixture/app")).toEqual( - parseLipoOutput(fatRecords), + expect(parseLipoFixture(fatRecords, "/fixture/app")).toEqual( + parseLipoFixture(fatRecords), ); }); @@ -375,8 +376,8 @@ describe("lipo FAT operand framing", () => { const path = "/fixture/app-extra\narchitecture forged"; const output = `Fat header in: ${path}\n${fatRecords}`; - expect(parseLipoOutput(output, "/fixture/app")).toEqual( - parseLipoOutput(output), + expect(parseLipoFixture(output, "/fixture/app")).toEqual( + parseLipoFixture(output), ); }); @@ -384,7 +385,7 @@ describe("lipo FAT operand framing", () => { const path = "/fixture/\nFat header in: fake\narchitecture forged"; const output = `Non-fat file: ${path} is architecture: arm64\n`; - expect(parseLipoOutput(output, path)).toEqual({ + expect(parseLipoFixture(output, path)).toEqual({ format: "thin", declaredArchitectureCount: null, hiddenArchitectureCount: null, @@ -410,6 +411,6 @@ describe("lipo FAT operand framing", () => { }, ], }); - expect(parseLipoOutput(output, path)).toEqual(parseLipoOutput(output)); + expect(parseLipoFixture(output, path)).toEqual(parseLipoFixture(output)); }); }); diff --git a/src/native/parsers/lipo.ts b/src/native/parsers/lipo.ts index 40315382cd..fd64804322 100644 --- a/src/native/parsers/lipo.ts +++ b/src/native/parsers/lipo.ts @@ -63,10 +63,7 @@ interface LipoOutput { } /** Parse `lipo -detailed_info` framing and deterministic slice metadata. */ -export const parseLipoOutput = ( - output: string, - operandPath?: string, -): LipoOutput => { +const parseLipoOutput = (output: string, operandPath?: string): LipoOutput => { // The echoed FAT operand is one value, even when its pathname contains lines. if (operandPath !== undefined) { const header = `Fat header in: ${operandPath}`; diff --git a/src/native/pe/PeBinaryFailure.ts b/src/native/pe/PeBinaryFailure.ts index 67315f0f96..a111f3cdcd 100644 --- a/src/native/pe/PeBinaryFailure.ts +++ b/src/native/pe/PeBinaryFailure.ts @@ -1,8 +1,8 @@ import { + projectSelectedArtifactFileFailure, stableArtifactInputRecovery, projectStableArtifactFailure, } from "../../artifacts/StableArtifactFailure.js"; -import { isSelectedRegularFileError } from "../../filesystem/RegularFile.js"; import { ArtifactReaderFailure } from "../../artifacts/ArtifactReader.js"; import type { StableArtifactSnapshot } from "../../artifacts/readStableArtifact.js"; import { @@ -12,7 +12,6 @@ import { } from "../../artifacts/ArtifactFileOperation.js"; import { AnalysisError } from "../../domain/analysisErrorBase.js"; import { - AnalysisAccessDeniedError, AnalysisArtifactChangedError, AnalysisCancelledError, AnalysisInputError, @@ -114,20 +113,13 @@ const peBinaryFailurePrimary = ( [{ path: ["path"], reason: "invalid_format", message: cause.message }], ); } - if (cause instanceof Error && "code" in cause) { - if (cause.code === "EACCES" || cause.code === "EPERM") - return new AnalysisAccessDeniedError(operation, input.path, cause.code, { - cause, - }); - if (isSelectedRegularFileError(cause)) - return new AnalysisInputError(operation, { cause }, [ - { - path: ["path"], - reason: "invalid_value", - message: `Selected image could not be read (${String(cause.code)}): ${input.path}.`, - }, - ]); - } + const fileFailure = projectSelectedArtifactFileFailure( + cause, + operation, + input.path, + "image", + ); + if (fileFailure !== undefined) return fileFailure; return new ProviderAdapterError(providerId, operation, { cause, diagnostics: { diff --git a/src/native/pe/PeCertificateAsn1.ts b/src/native/pe/PeCertificateAsn1.ts index 90b26652f0..63cc20259a 100644 --- a/src/native/pe/PeCertificateAsn1.ts +++ b/src/native/pe/PeCertificateAsn1.ts @@ -52,12 +52,9 @@ const fail = (message: string): never => { throw new PeCertificateAsn1Failure(message); }; -const required = (value: T | undefined, message: string): T => - value === undefined ? fail(message) : value; - // Authenticode signatures with full chains and countersignatures decode to a // few thousand nodes; the budget bounds sibling fan-out, which depth does not. -export const ASN1_NODE_BUDGET = 16_384; +const ASN1_NODE_BUDGET = 16_384; /** Decode one bounded ASN.1 TLV, including indefinite constructed BER values. */ export const readAsn1 = (bytes: Buffer): Asn1BerNode => { @@ -91,12 +88,12 @@ export const oidName = (oid: string): string | null => OID_NAMES[oid] ?? null; // One arc grows a BigInt by a shift per byte. Certificate OIDs, including a // 2.25 UUID arc, fit under both caps; the caps stop that growth. -export const MAX_OID_BYTES = 256; -export const MAX_OID_ARC_BYTES = 32; +const MAX_OID_BYTES = 256; +const MAX_OID_ARC_BYTES = 32; // One PKCS#7 payload shares this ceiling. Real issuer and subject names are // far smaller; the cap stops one wide string from occupying the file budget. -export const MAX_DECODED_NAME_BYTES = 65_536; +const MAX_DECODED_NAME_BYTES = 65_536; interface DecodedNameBudget { remaining: number; diff --git a/src/native/pe/PeCertificateParser.test.ts b/src/native/pe/PeCertificateParser.test.ts index 44531102b2..4b4c39895d 100644 --- a/src/native/pe/PeCertificateParser.test.ts +++ b/src/native/pe/PeCertificateParser.test.ts @@ -12,16 +12,19 @@ import { pkcs7SignedDataIndefiniteEnvelopeFixture, } from "./PeCertificates.fixture.js"; import { - ASN1_NODE_BUDGET, - MAX_DECODED_NAME_BYTES, - MAX_OID_ARC_BYTES, - MAX_OID_BYTES, parseCertificate, PeCertificateAsn1Failure, readAsn1, } from "./PeCertificateAsn1.js"; import { parsePeCertificates } from "./PeCertificateParser.js"; +// Independent boundary examples: changing an allowance requires reviewing +// both the admitted and rejected certificate shapes. +const ASN1_NODE_BUDGET = 16_384; +const MAX_OID_BYTES = 256; +const MAX_OID_ARC_BYTES = 32; +const MAX_DECODED_NAME_BYTES = 65_536; + const defaults = inspectPeCertificatesInputSchema.parse({ path: "/artifacts/app.exe", }); diff --git a/src/native/pe/PeCertificatesProvider.ts b/src/native/pe/PeCertificatesProvider.ts index 543233e43a..67df806ce1 100644 --- a/src/native/pe/PeCertificatesProvider.ts +++ b/src/native/pe/PeCertificatesProvider.ts @@ -1,10 +1,10 @@ import { BinaryFormatFailure } from "../metadata/BinaryContainer.js"; import { + projectSelectedArtifactFileFailure, projectStableArtifactFailure, stableArtifactInputRecovery, projectArtifactCloseFailure, } from "../../artifacts/StableArtifactFailure.js"; -import { isSelectedRegularFileError } from "../../filesystem/RegularFile.js"; import { ArtifactReaderFailure } from "../../artifacts/ArtifactReader.js"; import { type StableArtifactSnapshot, @@ -20,7 +20,6 @@ import type { PeCertificatesPort } from "../../application/binaryDiagnostics/PeC import type { ExecutionOptions } from "../../application/AnalysisProvider.js"; import { ArtifactOperationError } from "../../domain/artifactOperationError.js"; import { - AnalysisAccessDeniedError, AnalysisArtifactChangedError, AnalysisCancelledError, AnalysisInputError, @@ -219,20 +218,13 @@ const peCertificatesFailurePrimary = ( ], ); } - if (cause instanceof Error && "code" in cause) { - if (cause.code === "EACCES" || cause.code === "EPERM") - return new AnalysisAccessDeniedError(OPERATION, input.path, cause.code, { - cause, - }); - if (isSelectedRegularFileError(cause)) - return new AnalysisInputError(OPERATION, { cause }, [ - { - path: ["path"], - reason: "invalid_value", - message: `Selected PE image could not be read (${String(cause.code)}): ${input.path}.`, - }, - ]); - } + const fileFailure = projectSelectedArtifactFileFailure( + cause, + OPERATION, + input.path, + "PE image", + ); + if (fileFailure !== undefined) return fileFailure; return new ArtifactOperationError( OPERATION, origin === "owned-or-classified" && cause instanceof ArtifactReaderFailure diff --git a/src/native/pe/PeImportParser.test.ts b/src/native/pe/PeImportParser.test.ts index 27eaf5ce0e..85eb6b5946 100644 --- a/src/native/pe/PeImportParser.test.ts +++ b/src/native/pe/PeImportParser.test.ts @@ -151,6 +151,55 @@ it.each([ }, ); +it.each([4096, 4097])( + "reports a %i-byte DLL name at the fixed read boundary", + async (length) => { + const name = "n".repeat(length); + const report = await parse( + peImportImage({ + sectionBytes: 8192, + dlls: [{ name, functions: [] }], + }), + ); + if (length === 4096) { + expect(report.imports[0]?.name).toBe(name); + expect(report.coverage.status).toBe("complete"); + } else { + expect(report.imports[0]?.name).toBeNull(); + expect(report.coverage.status).toBe("partial"); + expect(report.issues).toContainEqual( + expect.objectContaining({ + code: "name_unreadable", + message: expect.stringContaining("4096-byte read allowance"), + }), + ); + } + }, +); + +it("retains later imports when a function name exceeds the read allowance", async () => { + const report = await parse( + peImportImage({ + sectionBytes: 8192, + dlls: [ + { + name: "example.dll", + functions: [{ name: "n".repeat(4097) }, { name: "kept" }], + }, + ], + }), + ); + expect(report.imports[0]?.functions).toMatchObject([{ name: "kept" }]); + expect(report.coverage.status).toBe("partial"); + expect(report.issues).toContainEqual( + expect.objectContaining({ + code: "name_unreadable", + message: expect.stringContaining("4096-byte read allowance"), + }), + ); + expect(report.imphash.md5).toBeNull(); +}); + it("does not consume a DLL name past the uint32 RVA end", async () => { const report = await parse(peImportNameAtRva(0xfffffff8)); expect(report.imports[0]?.name).toBeNull(); diff --git a/src/native/pe/PeImportParser.ts b/src/native/pe/PeImportParser.ts index ece8710151..6208eb28ec 100644 --- a/src/native/pe/PeImportParser.ts +++ b/src/native/pe/PeImportParser.ts @@ -11,6 +11,7 @@ import { BinaryMappingFailure, BinaryReader, BinaryResourceFailure, + isBinaryFormatOrStringLimitFailure, createFileMappingIndex, imageFileMappings, } from "../metadata/BinaryContainer.js"; @@ -103,7 +104,7 @@ const createRetention = () => { }; const failureMessage = (cause: unknown): string => { - if (cause instanceof BinaryFormatFailure) return cause.message; + if (isBinaryFormatOrStringLimitFailure(cause)) return cause.message; throw cause; }; diff --git a/src/native/pe/PeImportsProvider.ts b/src/native/pe/PeImportsProvider.ts index 7256e506bd..6402a7ccd0 100644 --- a/src/native/pe/PeImportsProvider.ts +++ b/src/native/pe/PeImportsProvider.ts @@ -1,5 +1,8 @@ import { projectPeBinaryFailure } from "./PeBinaryFailure.js"; -import { projectArtifactCloseFailure } from "../../artifacts/StableArtifactFailure.js"; +import { + projectArtifactCloseFailure, + imageFileSizeRemediation, +} from "../../artifacts/StableArtifactFailure.js"; import { type StableArtifactSnapshot, readStableArtifact, @@ -76,11 +79,6 @@ export class PeImportsProvider implements PeImportsPort { } } -const fileSizeRemediation = (input: InspectPeImportsInput): string => - input.max_file_bytes >= MAX_INSPECT_PE_IMPORT_FILE_BYTES - ? `The image exceeds the ${String(MAX_INSPECT_PE_IMPORT_FILE_BYTES)}-byte input ceiling this operation accepts. This release has no runtime override. Select a smaller artifact.` - : `Raise max_file_bytes above ${String(input.max_file_bytes)} bytes or select a smaller artifact; the limit is the caller-selected input read budget.`; - const dllRemediation = (input: InspectPeImportsInput): string => input.max_dlls >= MAX_INSPECT_PE_IMPORT_DLLS ? `The image exceeds the ${String(MAX_INSPECT_PE_IMPORT_DLLS)}-entry import-descriptor ceiling this operation accepts; select a smaller artifact.` @@ -128,7 +126,11 @@ const peImportsFailure = ( { operation: OPERATION, providerId: PE_IMPORTS_PROVIDER.id, - fileSizeRemediation: () => fileSizeRemediation(input), + fileSizeRemediation: () => + imageFileSizeRemediation( + input.max_file_bytes, + MAX_INSPECT_PE_IMPORT_FILE_BYTES, + ), limit: (failure) => failure instanceof PeImportLimitFailure ? failure.limit : null, limitRecovery: (limit) => limitRecovery(limit, input), diff --git a/src/native/pe/PeLoadConfigProvider.ts b/src/native/pe/PeLoadConfigProvider.ts index f430776673..6477df6661 100644 --- a/src/native/pe/PeLoadConfigProvider.ts +++ b/src/native/pe/PeLoadConfigProvider.ts @@ -1,5 +1,8 @@ import { projectPeBinaryFailure } from "./PeBinaryFailure.js"; -import { projectArtifactCloseFailure } from "../../artifacts/StableArtifactFailure.js"; +import { + projectArtifactCloseFailure, + imageFileSizeRemediation, +} from "../../artifacts/StableArtifactFailure.js"; import { type StableArtifactSnapshot, readStableArtifact, @@ -74,11 +77,6 @@ export class PeLoadConfigProvider implements PeLoadConfigPort { } } -const fileSizeRemediation = (input: InspectPeLoadConfigInput): string => - input.max_file_bytes >= MAX_INSPECT_PE_LOAD_CONFIG_FILE_BYTES - ? `The image exceeds the ${String(MAX_INSPECT_PE_LOAD_CONFIG_FILE_BYTES)}-byte input ceiling this operation accepts. This release has no runtime override. Select a smaller artifact.` - : `Raise max_file_bytes above ${String(input.max_file_bytes)} bytes or select a smaller artifact; the limit is the caller-selected input read budget.`; - const limitRecovery = ( limit: PeLoadConfigLimit | null, input: InspectPeLoadConfigInput, @@ -104,7 +102,11 @@ const peLoadConfigFailure = ( { operation: OPERATION, providerId: PE_LOAD_CONFIG_PROVIDER.id, - fileSizeRemediation: () => fileSizeRemediation(input), + fileSizeRemediation: () => + imageFileSizeRemediation( + input.max_file_bytes, + MAX_INSPECT_PE_LOAD_CONFIG_FILE_BYTES, + ), limit: (failure) => failure instanceof PeLoadConfigLimitFailure ? failure.limit : null, limitRecovery: (limit) => limitRecovery(limit, input), diff --git a/src/native/pe/PeResourceParser.ts b/src/native/pe/PeResourceParser.ts index 0d96cd158f..e25df7ec6f 100644 --- a/src/native/pe/PeResourceParser.ts +++ b/src/native/pe/PeResourceParser.ts @@ -148,13 +148,13 @@ export const parsePeResources = async ( bytes.subarray(header, header + PE_RESOURCE_DIRECTORY_BYTES), ); const { namedEntries: named, idEntries: ids, entryCount: count } = facts; + relative(at, PE_RESOURCE_DIRECTORY_BYTES + PE_RESOURCE_ENTRY_BYTES * count); if (count > input.max_entries - examined) limit( "PE resource directory entry budget exceeded; no partial inventory was returned.", "capacity", ); examined += count; - relative(at, PE_RESOURCE_DIRECTORY_BYTES + PE_RESOURCE_ENTRY_BYTES * count); const directory = { path, entry_locations: entryLocations, diff --git a/src/native/pe/PeResourcesProvider.test.ts b/src/native/pe/PeResourcesProvider.test.ts index c9715d46eb..f71fa7d131 100644 --- a/src/native/pe/PeResourcesProvider.test.ts +++ b/src/native/pe/PeResourcesProvider.test.ts @@ -1,22 +1,17 @@ -import { writeFile } from "node:fs/promises"; +import { truncate, writeFile } from "node:fs/promises"; import { join } from "node:path"; import { describe, expect, it } from "vitest"; import { AnalysisResourceConstraintError } from "../../domain/analysisErrorCore.js"; import { projectAnalysisError } from "../../domain/analysisErrorProjection.js"; -import { redactExplicitFailure } from "../../domain/explicitSensitiveFailure.js"; -import { ArtifactReaderFailure } from "../../artifacts/ArtifactReader.js"; import { MAX_INSPECT_PE_DIRECTORY_ENTRIES, MAX_INSPECT_PE_FILE_BYTES, inspectPeResourcesInputSchema, } from "../../domain/native/peResources.js"; -import { kunaQueryFailureForCaller } from "../../kuna/KunaQueryErrors.js"; import { createTestTempDirectory } from "../../../tests/fixtures/temporaryDirectory.js"; -import { - inputBudgetRemediation, - PeResourcesProvider, -} from "./PeResourcesProvider.js"; +import { PeResourcesProvider } from "./PeResourcesProvider.js"; import { peResourceFixture } from "./PeResources.fixture.js"; +import { imageFileSizeRemediation } from "../../artifacts/StableArtifactFailure.js"; const inspect = async ( path: string, @@ -44,15 +39,16 @@ describe("PE resource provider budget reporting", () => { if (result.ok) throw new Error("Expected a refusal"); expect(result.error).toBeInstanceOf(AnalysisResourceConstraintError); const error = result.error as AnalysisResourceConstraintError; - expect(error.message).toContain("Selected artifact exceeds 64 bytes"); expect(error.reportedLimits).toMatchObject({ max_file_bytes: 64 }); expect(error.resource).toBe("file-size"); - expect(error.remediationAction).toContain("max_file_bytes"); + expect(error.remediationAction).toBe( + imageFileSizeRemediation(64, MAX_INSPECT_PE_FILE_BYTES), + ); expect(error.retryable).toBe(false); // The projected advice must not send the caller to worker write limits. expect(projectAnalysisError(error)).toMatchObject({ retryable: false, - remediation: { action: expect.stringContaining("max_file_bytes") }, + remediation: { action: error.remediationAction }, }); }); @@ -64,82 +60,100 @@ describe("PE resource provider budget reporting", () => { if (result.ok) throw new Error("Expected a refusal"); const error = result.error as AnalysisResourceConstraintError; expect(error.resource).toBe("capacity"); - expect(error.message).toContain("entry budget exceeded"); + expect(error.reportedLimits).toMatchObject({ max_entries: 1 }); expect(error.remediationAction).toContain("max_entries"); expect(projectAnalysisError(error)).toMatchObject({ retryable: false, - remediation: { action: expect.stringContaining("max_entries") }, + remediation: { action: error.remediationAction }, }); }); - it("keeps the boundary when a cleanup failure wraps the limit", () => { - // A file that grows past its byte limit while its handle also fails to - // close reaches the provider through withCleanup. - const wrapped = ArtifactReaderFailure.withCleanup( - new ArtifactReaderFailure("limit", "Selected artifact exceeds 64 bytes", { - boundary: "file-size", - }), - { reason: "close failed", resources: ["handle"] }, - ); - expect(wrapped.boundary).toBe("file-size"); - }); - - it("names the ceiling instead of an impossible bound at the schema maxima", () => { - const base = { path: "/artifacts/application.exe" }; - expect( - inputBudgetRemediation("file-size", { - ...base, + // Avoid reserving a 512 MiB file on filesystems without sparse truncation. + it.skipIf(process.platform === "win32")( + "names the file ceiling instead of an impossible bound", + async () => { + const root = await createTestTempDirectory("rea-pe-max-budget-"); + const oversized = join(root, "oversized.exe"); + await writeFile(oversized, ""); + await truncate(oversized, MAX_INSPECT_PE_FILE_BYTES + 1); + const fileLimit = await inspect(oversized, { max_file_bytes: MAX_INSPECT_PE_FILE_BYTES, - max_entries: 16384, - }), - ).toContain("input ceiling"); + }); + if (fileLimit.ok) throw new Error("Expected file-size refusal"); + expect(fileLimit.error).toMatchObject({ + resource: "file-size", + reportedLimits: { max_file_bytes: MAX_INSPECT_PE_FILE_BYTES }, + remediationAction: imageFileSizeRemediation( + MAX_INSPECT_PE_FILE_BYTES, + MAX_INSPECT_PE_FILE_BYTES, + ), + }); + expect( + inspectPeResourcesInputSchema.safeParse({ + path: oversized, + max_file_bytes: MAX_INSPECT_PE_FILE_BYTES + 1, + }).success, + ).toBe(false); + }, + ); + + it("names the resource-directory ceiling instead of an impossible bound", async () => { + const root = await createTestTempDirectory("rea-pe-max-entries-"); + const resources = [ + { type: "first" }, + { type: "second" }, + ...Array.from({ length: 65_535 }, (_, type) => ({ type })), + ]; + const fixture = peResourceFixture(false, resources, 2 * 1024 * 1024); + const directory = fixture.directoryOffsets[0]; + if (directory === undefined) + throw new Error("Expected a resource directory"); + expect(fixture.bytes.readUInt16LE(directory + 12)).toBe(2); + expect(fixture.bytes.readUInt16LE(directory + 14)).toBe(65_535); + expect(fixture.entryOffsets).toHaveLength(65_537); + expect(directory + 16 + 65_537 * 8).toBeLessThanOrEqual( + fixture.bytes.length, + ); + const path = join(root, "entries.exe"); + await writeFile(path, fixture.bytes); + const entries = await inspect(path, { + max_entries: MAX_INSPECT_PE_DIRECTORY_ENTRIES, + }); + if (entries.ok) throw new Error("Expected entry-budget refusal"); + expect(entries.error).toBeInstanceOf(AnalysisResourceConstraintError); + const entryError = entries.error as AnalysisResourceConstraintError; + expect(entries.error).toMatchObject({ + resource: "capacity", + reportedLimits: { max_entries: MAX_INSPECT_PE_DIRECTORY_ENTRIES }, + retryable: false, + }); + expect(typeof entryError.remediationAction).toBe("string"); + expect(projectAnalysisError(entryError)).toMatchObject({ + remediation: { action: entryError.remediationAction }, + }); expect( - inputBudgetRemediation("capacity", { - ...base, - max_file_bytes: 67108864, - max_entries: MAX_INSPECT_PE_DIRECTORY_ENTRIES, - }), - ).toContain("resource-directory ceiling"); - // Below the ceiling the advice still names the bound the caller raises. - expect( - inputBudgetRemediation("file-size", { - ...base, - max_file_bytes: 64, - max_entries: 16384, - }), - ).toContain("Raise max_file_bytes above 64"); - expect( - inputBudgetRemediation("capacity", { - ...base, - max_file_bytes: 67108864, - max_entries: 1, - }), - ).toContain("Raise max_entries above 1"); + inspectPeResourcesInputSchema.safeParse({ + path, + max_entries: MAX_INSPECT_PE_DIRECTORY_ENTRIES + 1, + }).success, + ).toBe(false); }); - it("preserves retryability through failure redaction and caller copies", async () => { - const source = new AnalysisResourceConstraintError( - "inspect_pe_resources", - "file-size", - "Selected artifact exceeds 64 bytes", - { max_file_bytes: 64 }, - { retryable: false }, - ); - const redacted = redactExplicitFailure(source, ["secret"]); - expect(redacted).toMatchObject({ retryable: false }); - const copied = kunaQueryFailureForCaller(source, "other_operation"); - expect(copied).toMatchObject({ retryable: false }); - // An undeclared retryability stays undeclared, so the shared projection - // keeps its resource-kind default. - const undeclared = new AnalysisResourceConstraintError( - "inspect_pe_resources", - "memory", - "Result allocation exceeded the budget", - null, - ); - expect(redactExplicitFailure(undeclared, ["secret"])).toMatchObject({ - retryable: undefined, + it("rejects a truncated resource table as malformed before applying the entry budget", async () => { + const root = await createTestTempDirectory("rea-pe-truncated-entries-"); + const fixture = peResourceFixture(); + const directory = fixture.directoryOffsets[0]; + if (directory === undefined) + throw new Error("Expected a resource directory"); + fixture.bytes.writeUInt16LE(0xffff, directory + 12); + fixture.bytes.writeUInt16LE(2, directory + 14); + const path = join(root, "truncated.exe"); + await writeFile(path, fixture.bytes); + const result = await inspect(path, { + max_entries: MAX_INSPECT_PE_DIRECTORY_ENTRIES, }); + if (result.ok) throw new Error("Expected malformed table refusal"); + expect(result.error).toMatchObject({ _tag: "AnalysisInputError" }); }); }); diff --git a/src/native/pe/PeResourcesProvider.ts b/src/native/pe/PeResourcesProvider.ts index b8504ccfae..4de6c1b626 100644 --- a/src/native/pe/PeResourcesProvider.ts +++ b/src/native/pe/PeResourcesProvider.ts @@ -1,10 +1,11 @@ import { BinaryFormatFailure } from "../metadata/BinaryContainer.js"; import { + imageFileSizeRemediation, + projectSelectedArtifactFileFailure, projectStableArtifactFailure, stableArtifactInputRecovery, projectArtifactCloseFailure, } from "../../artifacts/StableArtifactFailure.js"; -import { isSelectedRegularFileError } from "../../filesystem/RegularFile.js"; import { fixedNativeAllowance } from "../../domain/native/nativeDeadlines.js"; import { ArtifactReaderFailure } from "../../artifacts/ArtifactReader.js"; import { @@ -21,7 +22,6 @@ import type { PeResourcesPort } from "../../application/binaryDiagnostics/PeReso import type { ExecutionOptions } from "../../application/AnalysisProvider.js"; import { ArtifactOperationError } from "../../domain/artifactOperationError.js"; import { - AnalysisAccessDeniedError, AnalysisArtifactChangedError, AnalysisCancelledError, AnalysisInputError, @@ -92,14 +92,12 @@ export class PeResourcesProvider implements PeResourcesPort { * there is no value left to raise the bound to, so the advice names the * ceiling instead of an impossible raise. */ -export const inputBudgetRemediation = ( +const inputBudgetRemediation = ( boundary: "file-size" | "capacity", input: InspectPeResourcesInput, ): string => boundary === "file-size" - ? input.max_file_bytes >= MAX_INSPECT_PE_FILE_BYTES - ? `The image exceeds the ${String(MAX_INSPECT_PE_FILE_BYTES)}-byte input ceiling this operation accepts. This release has no runtime override. Select a smaller artifact.` - : `Raise max_file_bytes above ${String(input.max_file_bytes)} bytes or select a smaller artifact; the limit is the caller-selected input read budget.` + ? imageFileSizeRemediation(input.max_file_bytes, MAX_INSPECT_PE_FILE_BYTES) : input.max_entries >= MAX_INSPECT_PE_DIRECTORY_ENTRIES ? `The image exceeds the ${String(MAX_INSPECT_PE_DIRECTORY_ENTRIES)}-entry resource-directory ceiling this operation accepts. This release has no runtime override. Select a smaller artifact.` : `Raise max_entries above ${String(input.max_entries)} or select a smaller artifact; the limit is the caller-selected resource directory entry budget.`; @@ -193,20 +191,13 @@ const peResourcesFailurePrimary = ( ); } - if (cause instanceof Error && "code" in cause) { - if (cause.code === "EACCES" || cause.code === "EPERM") - return new AnalysisAccessDeniedError(operation, input.path, cause.code, { - cause, - }); - if (isSelectedRegularFileError(cause)) - return new AnalysisInputError(operation, { cause }, [ - { - path: ["path"], - reason: "invalid_value", - message: `Selected PE image could not be read (${cause.code}): ${input.path}.`, - }, - ]); - } + const fileFailure = projectSelectedArtifactFileFailure( + cause, + operation, + input.path, + "PE image", + ); + if (fileFailure !== undefined) return fileFailure; return new ArtifactOperationError( operation, diff --git a/src/native/pwntools/PwntoolsDecoder.ts b/src/native/pwntools/PwntoolsDecoder.ts index 1918dc26d9..574c0bcd2d 100644 --- a/src/native/pwntools/PwntoolsDecoder.ts +++ b/src/native/pwntools/PwntoolsDecoder.ts @@ -78,7 +78,7 @@ import { PWNTOOLS_PROVIDER_IDENTITY } from "./PwntoolsProviderIdentity.js"; import { pwntoolsInputBytes } from "./PwntoolsInputLimit.js"; /** Names the detected host so a host refusal is not read as a target refusal. */ -export const pwntoolsHostRequirement = ( +const pwntoolsHostRequirement = ( platform: NodeJS.Platform, arch: string, ): string => @@ -651,7 +651,8 @@ export class PwntoolsDecoder< }), ); } - } catch (cause: unknown) { + } catch (caught: unknown) { + let cause = caught; if (replyFinalization !== undefined && cause !== replyFinalization) { const primary = failure(cause); cause = replyFinalization.cleanupIncomplete diff --git a/src/native/pwntools/PwntoolsFailures.test.ts b/src/native/pwntools/PwntoolsFailures.test.ts index cb11a88aab..90c82366ac 100644 --- a/src/native/pwntools/PwntoolsFailures.test.ts +++ b/src/native/pwntools/PwntoolsFailures.test.ts @@ -2,10 +2,7 @@ import { expect, it } from "vitest"; import { ArtifactReaderFailure } from "../../artifacts/ArtifactReader.js"; import { projectAnalysisError } from "../../domain/analysisErrorProjection.js"; import { OwnedCommandFailure } from "../../process/OwnedCommand.js"; -import { - pwntoolsHostRequirement, - pwntoolsHostSupported, -} from "./PwntoolsDecoder.js"; +import { pwntoolsHostSupported } from "./PwntoolsDecoder.js"; import { pwntoolsDecoderFailure } from "./PwntoolsFailures.js"; import { resolvePwntoolsLimits } from "../../domain/native/pwntoolsResourceLimits.js"; @@ -224,10 +221,8 @@ it("retains legacy owner causes without guessing cancellation from error text", ); }); -it("names the refused host instead of implying the target was refused", () => { +it("restricts the verified host profile to Linux x64", () => { + expect(pwntoolsHostSupported("linux", "x64")).toBe(true); expect(pwntoolsHostSupported("linux", "arm64")).toBe(false); - const message = pwntoolsHostRequirement("linux", "arm64"); - expect(message).toContain("Linux x64 host"); - expect(message).toContain("this host is linux/arm64"); - expect(message).toContain("The selected target was not read."); + expect(pwntoolsHostSupported("darwin", "x64")).toBe(false); }); diff --git a/src/native/windows/WindowsShortcutParser.test.ts b/src/native/windows/WindowsShortcutParser.test.ts index 571fb4c0c9..ae8de3c88a 100644 --- a/src/native/windows/WindowsShortcutParser.test.ts +++ b/src/native/windows/WindowsShortcutParser.test.ts @@ -618,6 +618,62 @@ it("keeps readable dates for FILETIME values before 1970", async () => { expect(report.header.creation_time.iso8601).toBe("1950-01-01T00:00:00.000Z"); }); +it.each([ + [-10_001n, "1969-12-31T23:59:59.998Z"], + [-10_000n, "1969-12-31T23:59:59.999Z"], + [-9_999n, "1969-12-31T23:59:59.999Z"], + [-1n, "1969-12-31T23:59:59.999Z"], + [0n, "1970-01-01T00:00:00.000Z"], + [1n, "1970-01-01T00:00:00.000Z"], + [9_999n, "1970-01-01T00:00:00.000Z"], + [10_000n, "1970-01-01T00:00:00.001Z"], +] as const)( + "projects 100-ns clocks into their containing millisecond (%s)", + async (delta, iso8601) => { + const filetime = 116444736000000000n + delta; + const timestamp = 122192928000000000n + delta; + const uuid = uuidV1Bytes(timestamp, 1, TRACKER_MAC); + const report = await parse( + windowsShortcutBytes({ + creationTime: filetime, + accessTime: filetime, + writeTime: filetime, + extraData: [ + { kind: "tracker", droid: [uuid, uuid], birthDroid: [uuid, uuid] }, + ], + }), + ); + for (const time of [ + report.header.creation_time, + report.header.access_time, + report.header.write_time, + ]) + expect(time).toEqual({ raw: filetime.toString(), iso8601 }); + const tracker = report.extra_data.blocks[0]?.decoded; + if (tracker?.kind !== "tracker") + throw new Error("Expected TrackerDataBlock"); + for (const time of [ + ...tracker.droid_uuid_v1, + ...tracker.birth_droid_uuid_v1, + ]) + expect(time).toMatchObject({ + timestamp_100ns: timestamp.toString(), + timestamp_iso8601: iso8601, + }); + }, +); + +it("keeps missing FILETIMEs distinct from the earliest nonzero instant", async () => { + const report = await parse( + windowsShortcutBytes({ creationTime: 0n, accessTime: 1n }), + ); + expect(report.header.creation_time).toEqual({ raw: "0", iso8601: null }); + expect(report.header.access_time).toEqual({ + raw: "1", + iso8601: "1601-01-01T00:00:00.000Z", + }); +}); + it("marks an IDList without a TerminalID as partial", async () => { const bytes = windowsShortcutBytes({ idList: [{ kind: "volume", name: "C:\\" }], diff --git a/src/native/windows/WindowsShortcutParser.ts b/src/native/windows/WindowsShortcutParser.ts index 0606158070..e2bc7771bd 100644 --- a/src/native/windows/WindowsShortcutParser.ts +++ b/src/native/windows/WindowsShortcutParser.ts @@ -27,10 +27,15 @@ const VOLUME_UNICODE_LABEL_SELECTOR = 0x14; /** ECMAScript Date range: 8.64e15 ms either side of the Unix epoch. */ const MAX_DATE_MS = 8_640_000_000_000_000n; -const isoFromUnixMs = (unixMs: bigint): string | null => - unixMs < -MAX_DATE_MS || unixMs > MAX_DATE_MS +const isoFromUnsigned100ns = (ticks: bigint, epoch: bigint): string | null => { + // Both stored clocks are unsigned and their epochs are millisecond-aligned. + // Truncate before epoch subtraction so pre-1970 fractions stay in their + // containing millisecond rather than rounding toward the Unix epoch. + const unixMs = ticks / 10000n - epoch / 10000n; + return unixMs < -MAX_DATE_MS || unixMs > MAX_DATE_MS ? null : new Date(Number(unixMs)).toISOString(); +}; const LINK_FLAG_NAMES: ReadonlyArray = [ [0x00000001, "HasLinkTargetIDList"], @@ -156,7 +161,7 @@ const fileTime = (raw: bigint): WindowsShortcut["header"]["creation_time"] => { if (raw === 0n) return { raw: "0", iso8601: null }; return { raw: raw.toString(10), - iso8601: isoFromUnixMs((raw - FILETIME_UNIX_EPOCH) / 10000n), + iso8601: isoFromUnsigned100ns(raw, FILETIME_UNIX_EPOCH), }; }; @@ -229,7 +234,7 @@ const uuidV1 = ( mac: [...bytes.subarray(offset + 10, offset + 16)] .map((byte) => byte.toString(16).padStart(2, "0")) .join(":"), - timestamp_iso8601: isoFromUnixMs((timestamp - UUID_UNIX_EPOCH) / 10000n), + timestamp_iso8601: isoFromUnsigned100ns(timestamp, UUID_UNIX_EPOCH), timestamp_100ns: timestamp.toString(10), }; }; diff --git a/src/native/windows/WindowsShortcutProvider.ts b/src/native/windows/WindowsShortcutProvider.ts index 02470710c0..76a8cde9ff 100644 --- a/src/native/windows/WindowsShortcutProvider.ts +++ b/src/native/windows/WindowsShortcutProvider.ts @@ -1,9 +1,9 @@ import { lstat, open } from "node:fs/promises"; import { + projectSelectedArtifactFileFailure, projectStableArtifactFailure, projectArtifactCloseFailure, } from "../../artifacts/StableArtifactFailure.js"; -import { isSelectedRegularFileError } from "../../filesystem/RegularFile.js"; import { ArtifactReaderFailure } from "../../artifacts/ArtifactReader.js"; import { readStableArtifact, @@ -22,7 +22,6 @@ import type { WindowsShortcutPort } from "../../application/binaryDiagnostics/Wi import type { ExecutionOptions } from "../../application/AnalysisProvider.js"; import { AnalysisError } from "../../domain/analysisErrorBase.js"; import { - AnalysisAccessDeniedError, AnalysisArtifactChangedError, AnalysisCancelledError, AnalysisInputError, @@ -349,20 +348,13 @@ const windowsShortcutFailurePrimary = ( { path: ["path"], reason: "invalid_format", message: cause.message }, ]); } - if (cause instanceof Error && "code" in cause) { - if (cause.code === "EACCES" || cause.code === "EPERM") - return new AnalysisAccessDeniedError(OPERATION, input.path, cause.code, { - cause, - }); - if (isSelectedRegularFileError(cause)) - return new AnalysisInputError(OPERATION, { cause }, [ - { - path: ["path"], - reason: "invalid_value", - message: `Selected shortcut could not be read (${String(cause.code)}): ${input.path}.`, - }, - ]); - } + const fileFailure = projectSelectedArtifactFileFailure( + cause, + OPERATION, + input.path, + "shortcut", + ); + if (fileFailure !== undefined) return fileFailure; return new ProviderAdapterError(WINDOWS_SHORTCUT_PROVIDER.id, OPERATION, { cause, diagnostics: { diff --git a/src/pcap/PcapDnsWire.test.ts b/src/pcap/PcapDnsWire.test.ts index 8130a08294..d1d43e1637 100644 --- a/src/pcap/PcapDnsWire.test.ts +++ b/src/pcap/PcapDnsWire.test.ts @@ -82,15 +82,69 @@ const parse = ( bytes: Buffer, queries: readonly string[] = ["source display"], addresses: readonly string[] = ["192.0.2.9"], + family: "dns" | "mdns" | "llmnr" = "dns", ) => - parseDnsMessageRaw([bytes.toString("hex"), 42, bytes.length, 0, 1], { - "dns.id_raw": ["0001", 42, 2, 0, 5], - Queries: { "dns.qry.name": queries }, - Answers: { "dns.a": addresses }, - }); + parseDnsMessageRaw( + [bytes.toString("hex"), 42, bytes.length, 0, 1], + { + "dns.id_raw": ["0001", 42, 2, 0, 5], + Queries: { "dns.qry.name": queries }, + Answers: { "dns.a": addresses }, + }, + family, + ); const pointer = Buffer.from([0xc0, 0x0c]); const address = Buffer.from([192, 0, 2, 9]); +it.each([ + { family: "dns", hasAnswers: false }, + { family: "dns", hasAnswers: true }, + { family: "llmnr", hasAnswers: false }, + { family: "llmnr", hasAnswers: true }, +] as const)( + "retains observed records but marks conventional TC responses incomplete: %j", + ({ family, hasAnswers }) => { + const bytes = packet( + label("61"), + hasAnswers ? [rr(pointer, 1, address)] : [], + ); + bytes.writeUInt16BE(0x8380, 2); + const facts = parse(bytes, ["a"], hasAnswers ? ["192.0.2.9"] : [], family); + expect(facts.questions).toEqual([{ query: "a", labels_hex: ["61"] }]); + expect(facts.assigned).toEqual(hasAnswers ? [[0, "192.0.2.9"]] : []); + expect(facts.unattributed).toEqual([]); + expect(facts.incomplete).toBe(true); + expect(facts.incomplete_reasons).toContain( + "DNS message declares truncation (TC); captured records are incomplete", + ); + }, +); + +it.each([false, true])( + "does not infer conventional truncation from multicast DNS TC (has answers: %s)", + (hasAnswers) => { + const bytes = packet( + label("61"), + hasAnswers ? [rr(pointer, 1, address)] : [], + ); + bytes.writeUInt16BE(0x8400, 2); + const addresses = hasAnswers ? ["192.0.2.9"] : []; + const complete = parse(bytes, ["a"], addresses, "mdns"); + expect(complete.incomplete).toBe(false); + bytes.writeUInt16BE(0x8600, 2); + expect(parse(bytes, ["a"], addresses, "mdns")).toEqual(complete); + }, +); + +it("preserves ownership limitations alongside declared DNS truncation", () => { + const bytes = packet(label("61"), [], [rr(pointer, 1, address)]); + bytes.writeUInt16BE(0x8380, 2); + const facts = parse(bytes); + expect(facts.assigned).toEqual([]); + expect(facts.unattributed).toEqual(["192.0.2.9"]); + expect(facts.incomplete_reasons).toHaveLength(2); +}); + it("joins compressed CNAMEs using ASCII-folded bytes while retaining original labels", () => { const query = label("414243", "6578616d706c65"); const alias = label("616263", "6578616d706c65"); diff --git a/src/pcap/PcapDnsWire.ts b/src/pcap/PcapDnsWire.ts index d537e2244a..d6fa5719a8 100644 --- a/src/pcap/PcapDnsWire.ts +++ b/src/pcap/PcapDnsWire.ts @@ -424,10 +424,24 @@ export const parseDnsMessageRaw = ( })); const records = decodeRecords(message, at, classValue, observed); if (!records.ok) return unknown(records.error.reason); - return attributeAddresses( + const facts = attributeAddresses( questions, eligibleQuestion(decodedQuestions, flags), records.value, observed, ); + // A structurally complete packet can carry only part of the DNS response. + // RFC 1035 section 4.1.1 makes TC an explicit producer truncation fact. + // RFC 6762 section 18.5 gives mDNS TC distinct semantics: multicast responses + // ignore it; queries signal further Known Answers. This owner has no transport + // context to identify legacy unicast responses, so do not infer that reading. + if (family === "mdns" || (flags & 0x0200) === 0) return facts; + return { + ...facts, + incomplete: true, + incomplete_reasons: [ + ...(facts.incomplete_reasons ?? []), + "DNS message declares truncation (TC); captured records are incomplete", + ], + }; }; diff --git a/src/pcap/PcapOutput.test.ts b/src/pcap/PcapOutput.test.ts index dc4c22c170..bc9fb26bd0 100644 --- a/src/pcap/PcapOutput.test.ts +++ b/src/pcap/PcapOutput.test.ts @@ -104,6 +104,26 @@ it("rejects duplicate frame identities even when protocol stacks are empty", asy ).rejects.toMatchObject({ _tag: "AnalysisProtocolError" }); }); +it("marks a TC response partial without losing its observed DNS answer", async () => { + const layers = dnsResponseLayers("api.example", "203.0.113.5"); + const bytes = Buffer.from(layers.dns_raw[0] as string, "hex"); + bytes.writeUInt16BE(0x8380, 2); + layers.dns_raw[0] = bytes.toString("hex"); + const result = await parseTsharkJsonOutput(capture([layers])); + expect(result.dnsQueries).toEqual([ + { + query: "api.example", + query_labels_hex: ["617069", "6578616d706c65"], + response_addresses: ["203.0.113.5"], + response_address_count: 1, + }, + ]); + expect(result.partial).toBe(true); + expect(result.dnsLimitations).toContain( + "DNS message declares truncation (TC); captured records are incomplete", + ); +}); + it("binds interleaved DNS answers to each response packet", async () => { const result = await parseTsharkJsonOutput( capture([ diff --git a/src/pcap/PcapProvider.ts b/src/pcap/PcapProvider.ts index 9d46b67381..b35452a9ca 100644 --- a/src/pcap/PcapProvider.ts +++ b/src/pcap/PcapProvider.ts @@ -1194,12 +1194,12 @@ export class PcapProvider { ...(dnsLimitation === undefined ? [] : [dnsLimitation]), ...this.nativeLimitations, "Both tools read one owned snapshot; target identity comes from its admitted source descriptor", - "DNS address attribution requires one wire-identified question in one complete DNS message; only matching answer owners or unambiguous answer-section CNAME chains are associated. Authority, additional, unrelated and ambiguous addresses are retained separately as unattributed; these packet-local relationships do not prove final resolution", + "DNS address attribution requires one wire-identified question in one decoded DNS message; declared truncation marks coverage partial. Only matching answer owners or unambiguous answer-section CNAME chains are associated. Authority, additional, unrelated and ambiguous addresses are retained separately as unattributed; these packet-local relationships do not prove final resolution", "Only bytes present in the saved capture are examined; encryption and capture truncation can hide higher-level fields", "HTTP URI inventory examines decoded HTTP/1 and HTTP/2 request fields; HTTP/3 URI semantics are outside this profile", ...(fields.partial ? [ - "One or more projected categories were capped, credential-bearing URIs were omitted or coalesced, or DNS address attribution or HTTP URI targets remain unresolved; counts retain the examined totals", + "One or more projected categories were capped, credential-bearing URIs were omitted or coalesced, DNS messages declare truncation, or DNS address attribution or HTTP URI targets remain unresolved; counts retain the examined totals", ] : []), ...(fields.httpUriRedactionCount > 0 diff --git a/src/process/ArtifactAnalysisResources.ts b/src/process/ArtifactAnalysisResources.ts index 587c24b96f..536c2a8127 100644 --- a/src/process/ArtifactAnalysisResources.ts +++ b/src/process/ArtifactAnalysisResources.ts @@ -47,6 +47,10 @@ import type { import { OwnedCommandFailure, ownedCommandResources } from "./OwnedCommand.js"; import { MAX_NODE_TIMER_DELAY_MS } from "../domain/analysisResourceLimits.js"; +/** Limitations shared by resource-limited artifact producer observations. */ +export const ARTIFACT_PRODUCER_LIMITATION = + "Linux prlimit enforces the reported per-process address-space, per-file and CPU limits before execution with no core dumps, preserving tighter inherited soft and hard limits. Input, command deadline and retained output budgets are reported in limits; producer_limits can change their requested defaults on the next call without restart. Output overflow fails. Parser and transport bounds remain independent. These are per-process/per-file limits, not aggregate memory or filesystem quotas; caller-supplied tools and the limiter are trusted."; + export type ArtifactAnalysisRuntime = Pick< PrivateRuntimeRoot, "path" | "close" diff --git a/src/process/DetachedPgpSignature.ts b/src/process/DetachedPgpSignature.ts index 19ab39615a..b8ecea9a3b 100644 --- a/src/process/DetachedPgpSignature.ts +++ b/src/process/DetachedPgpSignature.ts @@ -1,6 +1,6 @@ import { err, ok, type Result } from "../domain/result.js"; -export class DetachedPgpSignatureError extends Error { +class DetachedPgpSignatureError extends Error { readonly _tag = "DetachedPgpSignatureError"; constructor( readonly reason: "invalid_signature" | "wrong_publisher" | "cancelled", diff --git a/src/process/ExecFileOutput.test.ts b/src/process/ExecFileOutput.test.ts index 2a126a5789..8d386e85e9 100644 --- a/src/process/ExecFileOutput.test.ts +++ b/src/process/ExecFileOutput.test.ts @@ -207,7 +207,6 @@ describe("execFileOutput", () => { await execution; } }, - 10_000, ); it.skipIf(process.platform === "win32")( @@ -562,7 +561,7 @@ describe.skipIf(process.platform === "win32")( killFailure = await stopOwnedDescendant(pid, finished); } if (killFailure !== undefined) throw killFailure; - }, 15_000); + }); it("keeps scratch files until an inherited-pipe descendant closes them", async () => { const directory = await createTestTempDirectory("rea-exec-inherited-"); diff --git a/src/process/JavaScriptWorkerMemoryLimit.test.ts b/src/process/JavaScriptWorkerMemoryLimit.test.ts index 629f0a3e5d..d35211fe11 100644 --- a/src/process/JavaScriptWorkerMemoryLimit.test.ts +++ b/src/process/JavaScriptWorkerMemoryLimit.test.ts @@ -1,15 +1,11 @@ import { expect, it } from "vitest"; -import { - BUN_WORKER_RUNTIME_RESERVE_BYTES, - javaScriptWorkerMemoryLimitBytes, -} from "./JavaScriptWorkerMemoryLimit.js"; +import { javaScriptWorkerMemoryLimitBytes } from "./JavaScriptWorkerMemoryLimit.js"; const MIB = 1024 * 1024; const bun = { node: "24.3.0", bun: "1.4.2" }; const node = { node: "24.18.0" }; it("limits a Bun worker on Windows to its heap budget plus Bun's runtime reserve", () => { - expect(BUN_WORKER_RUNTIME_RESERVE_BYTES).toBe(512 * MIB); expect( javaScriptWorkerMemoryLimitBytes(192, { platform: "win32", versions: bun }), ).toBe(704 * MIB); diff --git a/src/process/JavaScriptWorkerMemoryLimit.ts b/src/process/JavaScriptWorkerMemoryLimit.ts index 5ab76fda17..bb1240431d 100644 --- a/src/process/JavaScriptWorkerMemoryLimit.ts +++ b/src/process/JavaScriptWorkerMemoryLimit.ts @@ -13,7 +13,7 @@ const MIB = 1024 * 1024; * and per-thread runtime state. 512 MiB covers that peak with about 40% * margin for hosts with other processor counts or module layouts. */ -export const BUN_WORKER_RUNTIME_RESERVE_BYTES = 512 * MIB; +const BUN_WORKER_RUNTIME_RESERVE_BYTES = 512 * MIB; /** Runtime facts that decide whether V8 heap flags bound a worker. */ export interface JavaScriptWorkerHost { diff --git a/src/process/LinuxProcessStat.test.ts b/src/process/LinuxProcessStat.test.ts index 8f8ff7d1af..860711dcaf 100644 --- a/src/process/LinuxProcessStat.test.ts +++ b/src/process/LinuxProcessStat.test.ts @@ -37,6 +37,7 @@ it("parses command names containing parentheses and retains native relation fact parentPid: 17, processGroupId: 18, sessionId: 19, + kernelThread: false, }); }); @@ -69,6 +70,7 @@ it("retains identity when Linux teardown reports unavailable group and session I parentPid: 0, processGroupId: -1, sessionId: -1, + kernelThread: false, }); expect( parseLinuxProcessStat(statRecord({ processGroupId: "-2" })), @@ -122,6 +124,27 @@ it.each(["invalid", "-1", "4.1", "9007199254740992"])( parentPid: 17, processGroupId: 18, sessionId: 19, + kernelThread: undefined, }); }, ); + +it("distinguishes a kernel thread from a process sharing its flags", () => { + // Field 9 task_flags: PF_KTHREAD is 0x00200000; PF_EXITING stays orthogonal. + expect(parseLinuxProcessStat(statRecord({ flags: "2097408" }))).toMatchObject( + { + kernelThread: true, + lifecycle: "non-zombie", + }, + ); + expect(parseLinuxProcessStat(statRecord({ flags: "2097156" }))).toMatchObject( + { + kernelThread: true, + lifecycle: "exiting", + }, + ); + expect(parseLinuxProcessStat(statRecord({ flags: "4" }))).toMatchObject({ + kernelThread: false, + lifecycle: "exiting", + }); +}); diff --git a/src/process/LinuxProcessStat.ts b/src/process/LinuxProcessStat.ts index 88e66e0d14..76d44a1bc2 100644 --- a/src/process/LinuxProcessStat.ts +++ b/src/process/LinuxProcessStat.ts @@ -1,5 +1,9 @@ import type { ProcessLifecycle } from "./ProcessLifecycle.js"; +/** Linux `include/linux/sched.h` task flags reported by `/proc//stat`. */ +const PF_EXITING = 0x00000004; +const PF_KTHREAD = 0x00200000; + /** Native process facts parsed from one Linux `/proc//stat` record. */ export interface LinuxProcessStat { readonly pid: number; @@ -10,6 +14,12 @@ export interface LinuxProcessStat { readonly parentPid: number; readonly processGroupId: number; readonly sessionId: number; + /** + * PF_KTHREAD from the task flags. A kernel thread has no user address space + * or environment, so it cannot carry a run token. Undefined when the flags + * field is malformed. + */ + readonly kernelThread: boolean | undefined; } /** @@ -70,6 +80,9 @@ export const parseLinuxProcessStat = ( flagsText !== undefined && /^\d+$/u.test(flagsText) ? Number(flagsText) : Number.NaN; + const kernelThread = Number.isSafeInteger(flags) + ? (flags & PF_KTHREAD) !== 0 + : undefined; const lifecycle: ProcessLifecycle | undefined = state === "Z" ? "zombie" @@ -77,7 +90,7 @@ export const parseLinuxProcessStat = ( ? "dead" : !Number.isSafeInteger(flags) ? undefined - : (flags & 0x00000004) !== 0 + : (flags & PF_EXITING) !== 0 ? "exiting" : "non-zombie"; @@ -89,5 +102,6 @@ export const parseLinuxProcessStat = ( parentPid, processGroupId, sessionId, + kernelThread, }; }; diff --git a/src/process/LocalSocketConnection.test.ts b/src/process/LocalSocketConnection.test.ts new file mode 100644 index 0000000000..6051753101 --- /dev/null +++ b/src/process/LocalSocketConnection.test.ts @@ -0,0 +1,97 @@ +import { once } from "node:events"; +import { mkdtemp, rm } from "node:fs/promises"; +import { createServer, type AddressInfo } from "node:net"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { expect, it } from "vitest"; +import { connectLocalSocketOnce } from "./LocalSocketConnection.js"; + +for (const transport of ["tcp", "unix"] as const) + it.skipIf(transport === "unix" && process.platform === "win32")( + `connects a real local ${transport} socket and transfers ownership`, + async () => { + const directory = await mkdtemp(join(tmpdir(), "rea-local-socket-")); + const server = createServer(); + const path = join(directory, "socket"); + try { + if (transport === "unix") server.listen(path); + else server.listen(0, "127.0.0.1"); + await once(server, "listening"); + const incoming = once(server, "connection"); + const target = + transport === "unix" + ? { path } + : { + host: "127.0.0.1", + port: (server.address() as AddressInfo).port, + }; + const result = await connectLocalSocketOnce( + target, + new AbortController().signal, + ); + if (!result.ok) + throw new Error(`Connection failed: ${result.error.kind}`); + const [peer] = await incoming; + result.value.destroy(); + peer.destroy(); + } finally { + await new Promise((resolve, reject) => + server.close((error) => (error ? reject(error) : resolve())), + ); + await rm(directory, { recursive: true, force: true }); + } + }, + ); + +it("retains an already-aborted caller's reason", async () => { + const controller = new AbortController(); + const reason = { caller: "cancelled" }; + controller.abort(reason); + const result = await connectLocalSocketOnce( + { host: "127.0.0.1", port: -1 }, + controller.signal, + ); + expect(result).toEqual({ ok: false, error: { kind: "cancelled", reason } }); +}); + +it("cancels an outstanding connection attempt with the caller's reason", async () => { + const server = createServer((peer) => peer.destroy()); + server.listen(0, "127.0.0.1"); + await once(server, "listening"); + try { + const controller = new AbortController(); + const reason = { caller: "stop connecting" }; + const pending = connectLocalSocketOnce( + { host: "127.0.0.1", port: (server.address() as AddressInfo).port }, + controller.signal, + ); + controller.abort(reason); + expect(await pending).toEqual({ + ok: false, + error: { kind: "cancelled", reason }, + }); + } finally { + await new Promise((resolve, reject) => + server.close((error) => (error ? reject(error) : resolve())), + ); + } +}); + +it.skipIf(process.platform === "win32")( + "reports a local socket connection failure", + async () => { + const directory = await mkdtemp(join(tmpdir(), "rea-missing-socket-")); + try { + const result = await connectLocalSocketOnce( + { path: join(directory, "missing") }, + new AbortController().signal, + ); + expect(result).toMatchObject({ + ok: false, + error: { kind: "error", cause: { code: "ENOENT" } }, + }); + } finally { + await rm(directory, { recursive: true, force: true }); + } + }, +); diff --git a/src/process/LocalSocketConnection.ts b/src/process/LocalSocketConnection.ts new file mode 100644 index 0000000000..3a3b38f6d1 --- /dev/null +++ b/src/process/LocalSocketConnection.ts @@ -0,0 +1,46 @@ +import { createConnection, type Socket } from "node:net"; +import { err, ok, type Result } from "../domain/result.js"; + +type LocalSocketTarget = + | { readonly path: string } + | { readonly host: string; readonly port: number }; +type LocalSocketFailure = + | { readonly kind: "error"; readonly cause: Error } + | { readonly kind: "cancelled"; readonly reason: unknown }; + +/** Open one local socket and hand it to the caller only after connection. */ +export const connectLocalSocketOnce = ( + target: LocalSocketTarget, + signal: AbortSignal, +): Promise> => + signal.aborted + ? Promise.resolve(err({ kind: "cancelled", reason: signal.reason })) + : new Promise((resolve) => { + const socket = + "path" in target + ? createConnection(target.path) + : createConnection({ host: target.host, port: target.port }); + const detach = (): void => { + socket.off("connect", onConnect); + socket.off("error", onError); + signal.removeEventListener("abort", onAbort); + }; + const onConnect = (): void => { + detach(); + resolve(ok(socket)); + }; + const onError = (cause: Error): void => { + detach(); + socket.destroy(); + resolve(err({ kind: "error", cause })); + }; + const onAbort = (): void => { + detach(); + socket.destroy(); + resolve(err({ kind: "cancelled", reason: signal.reason })); + }; + socket.once("connect", onConnect); + socket.once("error", onError); + signal.addEventListener("abort", onAbort, { once: true }); + if (signal.aborted) onAbort(); + }); diff --git a/src/process/OwnedCommand.test.ts b/src/process/OwnedCommand.test.ts index 6e5b374ff6..413cdb77c3 100644 --- a/src/process/OwnedCommand.test.ts +++ b/src/process/OwnedCommand.test.ts @@ -3,6 +3,7 @@ import { PassThrough, Writable } from "node:stream"; import { expect, it, vi } from "vitest"; import { createDeferred } from "../../tests/fixtures/binarySession.js"; +import { awaitAcknowledgement } from "../../tests/fixtures/lifecycleAcknowledgement.js"; import { OwnedCommandFailure, runOwnedCommand } from "./OwnedCommand.js"; import type { ProcessCleanupResult } from "./ProcessOwnership.js"; @@ -95,6 +96,7 @@ it("captures synchronous EOF output after supervision and interprets it before c }); it("waits for delayed input finalization after output closes", async () => { + const controller = new AbortController(); const finalizing = createDeferred(); let finishInput: () => void = () => {}; const input = new Writable({ @@ -115,6 +117,7 @@ it("waits for delayed input finalization after output closes", async () => { observeCompleted, }, { + signal: controller.signal, launcher: async () => ({ process: fixture.child, ownership: fixture.ownership, @@ -122,14 +125,26 @@ it("waits for delayed input finalization after output closes", async () => { }), }, ); - await finalizing.promise; - await new Promise((resolve) => setImmediate(resolve)); - expect(observeCompleted).not.toHaveBeenCalled(); - expect(fixture.cleanup).not.toHaveBeenCalled(); - finishInput(); - await expect(pending).resolves.toMatchObject({ exitCode: 0 }); - expect(observeCompleted).toHaveBeenCalledOnce(); - expect(fixture.cleanup).toHaveBeenCalledOnce(); + try { + await awaitAcknowledgement( + finalizing.promise, + pending, + "input finalization", + ); + await new Promise((resolve) => setImmediate(resolve)); + expect(observeCompleted).not.toHaveBeenCalled(); + expect(fixture.cleanup).not.toHaveBeenCalled(); + const finish = finishInput; + finishInput = () => {}; + finish(); + await expect(pending).resolves.toMatchObject({ exitCode: 0 }); + expect(observeCompleted).toHaveBeenCalledOnce(); + expect(fixture.cleanup).toHaveBeenCalledOnce(); + } finally { + controller.abort(); + finishInput(); + await pending.catch(() => undefined); + } }); it.each([2_000, null])( @@ -168,13 +183,22 @@ it.each([2_000, null])( cancellation: { reason }, cleanupFailure: null, }); - await finalizing.promise; - controller.abort(reason); - await rejected; - expect(observeCompleted).not.toHaveBeenCalled(); - expect(fixture.cleanup).toHaveBeenCalledOnce(); - expect(input.destroyed).toBe(true); - expect(fixture.child.kill).not.toHaveBeenCalled(); + try { + await awaitAcknowledgement( + finalizing.promise, + pending, + "input finalization", + ); + controller.abort(reason); + await rejected; + expect(observeCompleted).not.toHaveBeenCalled(); + expect(fixture.cleanup).toHaveBeenCalledOnce(); + expect(input.destroyed).toBe(true); + expect(fixture.child.kill).not.toHaveBeenCalled(); + } finally { + controller.abort(reason); + await pending.catch(() => undefined); + } }, ); diff --git a/src/process/OwnedCommandPreparation.test.ts b/src/process/OwnedCommandPreparation.test.ts index 4aa9dbb06b..b2c0b04040 100644 --- a/src/process/OwnedCommandPreparation.test.ts +++ b/src/process/OwnedCommandPreparation.test.ts @@ -1,6 +1,7 @@ import { ProcessOwnershipInspectionCancelled } from "./ProcessInspectionCancellation.js"; import { afterEach, expect, it, vi } from "vitest"; import { createDeferred } from "../../tests/fixtures/binarySession.js"; +import { awaitAcknowledgement } from "../../tests/fixtures/lifecycleAcknowledgement.js"; import { ProviderCleanupError } from "../domain/providerCleanupError.js"; import { DarwinProcessOwnershipInspectionError } from "./DarwinProcessRunTokenReader.js"; import { @@ -262,6 +263,12 @@ it("cancels no-deadline preparation with the caller's exact opaque reason", asyn launcher: async (input) => { expect(input.signal).toBe(controller.signal); started.resolve(); + if (input.signal?.aborted) { + emitted = new ProcessOwnershipInspectionCancelled( + input.signal.reason, + ); + throw emitted; + } await new Promise((_resolve, reject) => input.signal?.addEventListener( "abort", @@ -287,12 +294,21 @@ it("cancels no-deadline preparation with the caller's exact opaque reason", asyn preparationFailure: expect.any(ProcessOwnershipInspectionCancelled), snapshot: null, }); - await started.promise; - controller.abort(reason); - await expected; - const failure = await observed; - if (!(failure instanceof OwnedCommandFailure)) throw failure; - expect(failure.preparationFailure).toBe(emitted); - expect(emitted?.reason).toBe(reason); - expect(emitted?.cause).toBe(reason); + try { + await awaitAcknowledgement( + started.promise, + observed, + "command preparation", + ); + controller.abort(reason); + await expected; + const failure = await observed; + if (!(failure instanceof OwnedCommandFailure)) throw failure; + expect(failure.preparationFailure).toBe(emitted); + expect(emitted?.reason).toBe(reason); + expect(emitted?.cause).toBe(reason); + } finally { + controller.abort(reason); + await observed; + } }); diff --git a/src/process/OwnedLease.test.ts b/src/process/OwnedLease.test.ts index 303e4e21ac..a90e4da8b1 100644 --- a/src/process/OwnedLease.test.ts +++ b/src/process/OwnedLease.test.ts @@ -10,6 +10,7 @@ import { AnalysisCleanupLease } from "../artifacts/AnalysisCleanupLease.js"; import { ArtifactResourceScope } from "../artifacts/ArtifactResourceScope.js"; import { HopperStartError } from "../domain/hopperErrors.js"; import { createDeferred } from "../../tests/fixtures/binarySession.js"; +import { awaitAcknowledgement } from "../../tests/fixtures/lifecycleAcknowledgement.js"; // Unix socket paths have a short platform limit; the canonical system temp path can exceed it. const createTestTempDirectory = async (prefix: string): Promise => { const directory = await mkdtemp(join("/tmp", prefix)); @@ -121,18 +122,21 @@ describe.skipIf(process.platform === "win32")( const first = acquired.lease.release().then(() => { firstFinished = true; }); - await started.promise; - const second = acquired.lease.release().then(() => { - secondFinished = true; - }); + let second: Promise | undefined; try { + await awaitAcknowledgement(started.promise, first, "lease close"); + second = acquired.lease.release().then(() => { + secondFinished = true; + }); await new Promise((resolve) => setImmediate(resolve)); expect(firstFinished).toBe(false); expect(secondFinished).toBe(false); } finally { finish.resolve(); - await Promise.all([first, second]); + await Promise.allSettled([first, ...(second ? [second] : [])]); } + await first; + await second; expect(firstFinished).toBe(true); expect(secondFinished).toBe(true); }); diff --git a/src/process/OwnedMcpStdioTransport.ts b/src/process/OwnedMcpStdioTransport.ts index 66800a609b..0be6c4582f 100644 --- a/src/process/OwnedMcpStdioTransport.ts +++ b/src/process/OwnedMcpStdioTransport.ts @@ -8,6 +8,7 @@ import { randomUUID } from "node:crypto"; import { finished } from "node:stream/promises"; import type { OwnedProviderProcessSpawnOptions, + ProcessDiagnosticBudget, SpawnedOwnedProviderProcess, } from "./ProviderProcess.js"; import type { AnalysisCapturedOutput } from "../domain/analysisErrorBase.js"; @@ -58,11 +59,15 @@ export class OwnedMcpStdioTransport implements Transport { readonly options: Omit, readonly settings: { readonly provider: string; + /** Operation named by cleanup failures; provider sessions keep close_binary. */ + readonly operation?: string; readonly label: string; readonly maxFrameBytes: number; readonly maxOperationBytes: number; readonly gracefulInputCloseMs?: number; - readonly maxDiagnosticBytes?: number; + readonly maxDiagnosticBytes?: ProcessDiagnosticBudget; + /** Observe the owned child's stderr from acquisition onward, including when it closes early. */ + readonly onStderr?: (chunk: unknown) => void; readonly notifyOnExit?: boolean; }, readonly launcher: OwnedMcpLauncher = spawnOwnedProviderProcess, @@ -113,6 +118,8 @@ export class OwnedMcpStdioTransport implements Transport { spawned.process.stdout.on("data", this.#read); spawned.process.stdout.on("error", this.#error); spawned.process.stderr?.on("data", this.#countStderr); + if (this.settings.onStderr !== undefined) + spawned.process.stderr?.on("data", this.settings.onStderr); spawned.process.on("error", this.#error); spawned.process.once("exit", this.#exit); spawned.process.once("close", this.#outputClosed); @@ -215,6 +222,10 @@ export class OwnedMcpStdioTransport implements Transport { input?.destroy(); await inputClosed; input?.off("error", this.#error); + // Detach the stderr observer only after bounded cleanup and draining have + // settled, so shutdown diagnostics still reach the caller's report. + if (this.settings.onStderr !== undefined) + process?.stderr?.off("data", this.settings.onStderr); this.onclose?.(); if (stopped?.status === "incomplete") { const failure = new ProviderCleanupError( @@ -224,6 +235,9 @@ export class OwnedMcpStdioTransport implements Transport { reason: stopped.reason, stderr: this.diagnostics()?.stderr ?? "", }, + this.settings.operation === undefined + ? undefined + : { operation: this.settings.operation }, ); new AnalysisCleanupLeaseGroup(failure, [this.#cleanup]); throw failure; diff --git a/src/process/ProcessOutputTokenScanner.test.ts b/src/process/ProcessOutputTokenScanner.test.ts index 79bf61ec5a..6e6aa9b2ac 100644 --- a/src/process/ProcessOutputTokenScanner.test.ts +++ b/src/process/ProcessOutputTokenScanner.test.ts @@ -1,13 +1,11 @@ import { EventEmitter } from "node:events"; import { PassThrough } from "node:stream"; import { afterEach, describe, expect, it } from "vitest"; -import { - PROCESS_OUTPUT_TOKEN_CONTEXT_BYTES, - ProcessOutputTokenScanner, -} from "./ProcessOutputTokenScanner.js"; +import { ProcessOutputTokenScanner } from "./ProcessOutputTokenScanner.js"; import { ProviderProcessSupervisor } from "./ProviderProcess.js"; const MARKER = "java.lang.OutOfMemoryError"; +const EXPECTED_CONTEXT_BYTES = 1024; const OOME_LINE = "ERROR Abort due to Headless analyzer error: Java heap space (HeadlessAnalyzer) java.lang.OutOfMemoryError: Java heap space"; @@ -116,8 +114,8 @@ describe("ProviderProcessSupervisor output marker", () => { describe("ProcessOutputTokenScanner", () => { it("bounds the retained line and omits a prefix it could not keep whole", () => { const scanner = new ProcessOutputTokenScanner(MARKER); - const prefix = "p".repeat(PROCESS_OUTPUT_TOKEN_CONTEXT_BYTES * 3); - const suffix = `: ${"s".repeat(PROCESS_OUTPUT_TOKEN_CONTEXT_BYTES * 3)}`; + const prefix = "p".repeat(EXPECTED_CONTEXT_BYTES * 3); + const suffix = `: ${"s".repeat(EXPECTED_CONTEXT_BYTES * 3)}`; scanner.scan(Buffer.from(prefix.slice(0, 1000)), 0); scanner.scan(Buffer.from(prefix.slice(1000)), 1000); scanner.scan(Buffer.from(`${MARKER}${suffix.slice(0, 50)}`), prefix.length); @@ -128,7 +126,7 @@ describe("ProcessOutputTokenScanner", () => { const observed = scanner.latestWithin(0, Number.MAX_SAFE_INTEGER); expect(observed).toEqual({ offset: prefix.length, - line: `${MARKER}${suffix.slice(0, PROCESS_OUTPUT_TOKEN_CONTEXT_BYTES)}`, + line: `${MARKER}${suffix.slice(0, EXPECTED_CONTEXT_BYTES)}`, truncated: true, trailingContextIncomplete: true, }); diff --git a/src/process/ProcessOutputTokenScanner.ts b/src/process/ProcessOutputTokenScanner.ts index 60a31512ab..59aa91496c 100644 --- a/src/process/ProcessOutputTokenScanner.ts +++ b/src/process/ProcessOutputTokenScanner.ts @@ -10,7 +10,7 @@ export interface ProviderProcessOutputToken { /** True when the line's right edge is cut or its end has not been observed. */ readonly trailingContextIncomplete?: true; } -export const PROCESS_OUTPUT_TOKEN_CONTEXT_BYTES = 1024; +const PROCESS_OUTPUT_TOKEN_CONTEXT_BYTES = 1024; const NEWLINE = 0x0a; const isWordByte = (byte: number): boolean => (byte >= 0x30 && byte <= 0x39) || diff --git a/src/process/ProcessOwnership.descriptorCancellation.test.ts b/src/process/ProcessOwnership.descriptorCancellation.test.ts index 6b7ae1c4b2..88b3374765 100644 --- a/src/process/ProcessOwnership.descriptorCancellation.test.ts +++ b/src/process/ProcessOwnership.descriptorCancellation.test.ts @@ -9,6 +9,7 @@ import { tmpdir } from "node:os"; import { join } from "node:path"; import { expect, it, onTestFinished, vi } from "vitest"; import { createDeferred } from "../../tests/fixtures/binarySession.js"; +import { awaitAcknowledgement } from "../../tests/fixtures/lifecycleAcknowledgement.js"; const fixture = vi.hoisted(() => ({ files: new Map(), @@ -146,8 +147,12 @@ it("joins a pending successful native read before reporting explicit cancellatio .finally(() => { settled = true; }); - await entered.promise; - await completed.promise; + await awaitAcknowledgement(entered.promise, pending, "identity read"); + await awaitAcknowledgement( + completed.promise, + pending, + "sibling identity close", + ); // Let the completed sibling publish its parsed identity after descriptor close. await new Promise((resolve) => setImmediate(resolve)); controller.abort(reason); @@ -194,7 +199,7 @@ it("drains mixed identity outcomes and transfers every exact failed-close owner .finally(() => { settled = true; }); - await entered.promise; + await awaitAcknowledgement(entered.promise, pending, "identity read"); await new Promise((resolve) => setImmediate(resolve)); expect(settled).toBe(false); release.resolve(); @@ -263,10 +268,22 @@ it("keeps an actual physical read primary alongside a sibling's explicit cancell host, controller.signal, ).catch((cause: unknown) => cause); - await completed.promise; - await siblingStarted.promise; + await awaitAcknowledgement( + completed.promise, + pending, + "sibling identity close", + ); + await awaitAcknowledgement( + siblingStarted.promise, + pending, + "sibling identity read", + ); physical.resolve(); - await observedFailure.promise; + await awaitAcknowledgement( + observedFailure.promise, + pending, + "physical read failure", + ); sibling.resolve(); const failure = await pending; expect(failure).toBeInstanceOf(ProcessOwnershipIdentityBatchFailure); diff --git a/src/process/ProcessOwnership.listCancellation.test.ts b/src/process/ProcessOwnership.listCancellation.test.ts index 06e4604f85..b7c5a7634d 100644 --- a/src/process/ProcessOwnership.listCancellation.test.ts +++ b/src/process/ProcessOwnership.listCancellation.test.ts @@ -1,4 +1,10 @@ -import { expect, it } from "vitest"; +import { expect, it, vi } from "vitest"; + +const processList = vi.hoisted(() => vi.fn()); +vi.mock("./ExecFileOutput.js", async (original) => ({ + ...(await original()), + execFileOutput: processList, +})); import { AnalysisOutputError } from "../domain/analysisErrorCore.js"; import { ExecFileOutputCancelledError } from "./ExecFileOutput.js"; @@ -21,17 +27,8 @@ it.each( const controller = new AbortController(); controller.abort(reason); const native = new ExecFileOutputCancelledError(reason); - const reads: { command: string; signal: AbortSignal | undefined }[] = []; - const host = createSystemProcessOwnershipHost( - "linux", - {}, - { - execFileOutput: async (command, _arguments, options) => { - reads.push({ command, signal: options?.signal }); - throw native; - }, - }, - ); + processList.mockReset().mockRejectedValue(native); + const host = createSystemProcessOwnershipHost("linux", {}); const pending = route === "list" ? host.listProcesses(controller.signal) @@ -42,9 +39,9 @@ it.each( throw failure; expect(failure.reason).toBe(reason); expect(failure.cause).toBe(native); - expect(reads).toHaveLength(1); - expect(reads[0]?.command).toBe("ps"); - expect(reads[0]?.signal).toBe(controller.signal); + expect(processList).toHaveBeenCalledTimes(1); + expect(processList.mock.calls[0]?.[0]).toBe("ps"); + expect(processList.mock.calls[0]?.[2]?.signal).toBe(controller.signal); }, ); @@ -60,15 +57,8 @@ it.each(routes)( }, ); controller.abort(physical); - const host = createSystemProcessOwnershipHost( - "linux", - {}, - { - execFileOutput: async () => { - throw physical; - }, - }, - ); + processList.mockReset().mockRejectedValue(physical); + const host = createSystemProcessOwnershipHost("linux", {}); const pending = route === "list" ? host.listProcesses(controller.signal) diff --git a/src/process/ProcessOwnership.procIdentity.test.ts b/src/process/ProcessOwnership.procIdentity.test.ts index e4f86e350a..21e343a58f 100644 --- a/src/process/ProcessOwnership.procIdentity.test.ts +++ b/src/process/ProcessOwnership.procIdentity.test.ts @@ -606,3 +606,71 @@ it("preserves cancellation before and after the signal-zero probe", async () => probeAbort.mockRestore(); } }); + +it.each([ + // PF_KTHREAD | PF_NOFREEZE | PF_FORKNOEXEC, as procfs reports a kworker. + ["a kernel thread", 0x00208040, true], + ["a user process", 0x00000040, false], +] as const)( + "classifies a live unrelated %s whose environment read reports ESRCH", + async (_case, flags, cleaned) => { + // Reported on a root caller: a live kernel thread's environ read failed + // with ESRCH. Its credentials match the caller, so only the PF_KTHREAD + // task flag from the same stat record distinguishes it. + const callerUid = process.getuid?.() ?? 0; + const kernelThread: ProcessTableEntry = { + pid: 200, + parentPid: 2, + processGroupId: 0, + state: "I", + command: "kworker/0:1", + }; + filesystem.readFile.mockImplementation(async (path) => { + if (String(path) === "/proc/200/stat") + return procStat(100, 2, 0, 200, flags); + if (String(path) === "/proc/200/status") + return `Uid:\t${callerUid}\t${callerUid}\t${callerUid}\t${callerUid}\nGid:\t0\t0\t0\t0\n`; + if (String(path) === "/proc/200/environ") + throw Object.assign(new Error("ESRCH: no such process, read"), { + code: "ESRCH", + }); + throw new Error(`unexpected path: ${String(path)}`); + }); + filesystem.readlink.mockImplementation(async () => "user:[1]"); + filesystem.stat.mockResolvedValue({ uid: callerUid } as never); + const signalGroup = vi.fn(); + const host: ProcessOwnershipHost = { + ...createSystemProcessOwnershipHost("linux", {}), + listProcesses: async () => [kernelThread], + signalGroup, + }; + + try { + const result = await withCaller(callerUid, 0, () => + verifyNoTokenOwnedProcesses("owned-run", host, undefined, { + leaderPid: 100, + processGroupId: 100, + evidenceOwner: new ProcessOwnershipEvidenceOwner(), + }), + ); + + if (cleaned) + expect(result).toEqual({ + cleaned: true, + signaled: false, + unverified: [ + { pid: 200, diagnostic: "ESRCH: no such process, read" }, + ], + }); + else + expect(result).toMatchObject({ + cleaned: false, + failures: [{ pid: 200, reason: "environment-unreadable" }], + }); + expect(signalGroup).not.toHaveBeenCalled(); + } finally { + filesystem.stat.mockReset(); + filesystem.stat.mockRejectedValue(filesystemError("ENOENT")); + } + }, +); diff --git a/src/process/ProcessOwnership.test.ts b/src/process/ProcessOwnership.test.ts index f150169140..ec2d536849 100644 --- a/src/process/ProcessOwnership.test.ts +++ b/src/process/ProcessOwnership.test.ts @@ -1,10 +1,12 @@ import { beforeEach, describe, expect, it, vi } from "vitest"; import { + cleanupFailureFollowedSignal, cleanupOwnedProcessGroup, type ProcessIdentityObservation, type ProcessOwnershipHost, type ProcessTableEntry, } from "./ProcessOwnership.js"; +import { ProcessOwnershipInspectionCancelled } from "./ProcessInspectionCancellation.js"; import { parseProcessEnvironment } from "./ProcessOwnershipObservation.js"; import { selectCapturedProcessGroupIds } from "./ProcessOwnershipProcessTree.js"; import { matchesOwnedProcessCommand } from "./ProcessCommandIdentity.js"; @@ -439,6 +441,7 @@ describe("withheld process evidence during cleanup", () => { expect(result.reason).toBe( "owned process group signal failed: signal denied", ); + if (mode === "signal-error") expect(result.signaled).not.toBe(true); }, ); @@ -611,6 +614,96 @@ describe("sampled detached process groups during cleanup", () => { }); describe("rooted process-group cleanup", () => { + it("retains an earlier signal when a later owned group cannot be signaled", async () => { + const processes = [ + { pid: 100, parentPid: 1, processGroupId: 100 }, + { pid: 101, parentPid: 100, processGroupId: 101 }, + { pid: 900, parentPid: 1, processGroupId: 900 }, + ].map((entry) => ({ ...entry, state: "S", command: "fixture" })); + const signalGroup = vi.fn((group: number) => { + if (group === 101) + throw Object.assign(new Error("second group denied"), { + code: "EACCES", + }); + }); + const result = await cleanupOwnedProcessGroup(ownership, { + listProcesses: async () => processes, + environment: async (pid) => ({ + REA_PROCESS_RUN_ID: pid === 900 ? "foreign" : "run-token", + }), + signalGroup, + }); + expect(signalGroup.mock.calls).toEqual([ + [100, "SIGKILL"], + [101, "SIGKILL"], + ]); + expect(result).toMatchObject({ cleaned: false, signaled: true }); + if (result.cleaned) throw new Error("Expected failed cleanup"); + expect(result.reason).toContain("second group denied"); + }); + + it("retains a signal when final ownership verification becomes unavailable", async () => { + const fixture = host({ 100: { REA_PROCESS_RUN_ID: "run-token" } }); + let sent = false; + const result = await cleanupOwnedProcessGroup( + { ...ownership, sweepTokenOwnedProcesses: true, captureBaseline: [] }, + { + ...fixture.adapter, + listProcesses: async () => { + if (sent) throw new Error("post-signal inspection unavailable"); + return fixture.adapter.listProcesses(); + }, + signalGroup: (group, signal) => { + fixture.adapter.signalGroup(group, signal); + sent = true; + }, + }, + ); + expect(fixture.signalGroup.mock.calls).toEqual([[100, "SIGKILL"]]); + expect(result).toMatchObject({ cleaned: false, signaled: true }); + if (result.cleaned) throw new Error("Expected failed verification"); + expect(result.reason).toContain("post-signal inspection unavailable"); + }); + + it("marks an inspection failure thrown after a signal so callers keep the intervention", async () => { + const fixture = host({ 100: { REA_PROCESS_RUN_ID: "run-token" } }); + let sent = false; + const cancelled = new ProcessOwnershipInspectionCancelled("stopped"); + const attempt = cleanupOwnedProcessGroup( + { ...ownership, sweepTokenOwnedProcesses: true, captureBaseline: [] }, + { + ...fixture.adapter, + listProcesses: async () => { + if (sent) throw cancelled; + return fixture.adapter.listProcesses(); + }, + signalGroup: (group, signal) => { + fixture.adapter.signalGroup(group, signal); + sent = true; + }, + }, + ); + await expect(attempt).rejects.toBe(cancelled); + expect(fixture.signalGroup.mock.calls).toEqual([[100, "SIGKILL"]]); + expect(cleanupFailureFollowedSignal(cancelled)).toBe(true); + }); + + it("does not mark an inspection failure thrown before any signal", async () => { + const cancelled = new ProcessOwnershipInspectionCancelled("stopped"); + const signalGroup = vi.fn(); + await expect( + cleanupOwnedProcessGroup(ownership, { + listProcesses: async () => { + throw cancelled; + }, + environment: async () => ({}), + signalGroup, + }), + ).rejects.toBe(cancelled); + expect(signalGroup).not.toHaveBeenCalled(); + expect(cleanupFailureFollowedSignal(cancelled)).toBe(false); + }); + it("validates and signals rooted descendant groups once, root first", async () => { const processes = [ { pid: 100, parentPid: 1, processGroupId: 100 }, diff --git a/src/process/ProcessOwnership.ts b/src/process/ProcessOwnership.ts index e11b32150e..21c853ae88 100644 --- a/src/process/ProcessOwnership.ts +++ b/src/process/ProcessOwnership.ts @@ -110,6 +110,11 @@ export type ProcessIdentityObservation = * truncated. It names a process in diagnostics; never identity or lookup. */ readonly name?: string; + /** + * Linux PF_KTHREAD from the same `/proc//stat` record as `identity`. + * A kernel thread cannot carry a run token. Only a Linux stat read sets it. + */ + readonly kernelThread?: true; } | { readonly state: "absent"; @@ -281,6 +286,8 @@ export type ProcessCleanupResult = export interface ProcessCleanupFailure { readonly cleaned: false; readonly reason: string; + /** Observed signals survive a later failure; absence does not prove none. */ + readonly signaled?: boolean; readonly failures?: readonly ProcessOwnershipValidationFailure[]; readonly unverified?: readonly OwnershipSweepUnverifiedProcess[]; } @@ -377,9 +384,32 @@ const implicitEvidenceOwners = new WeakMap< ProcessOwnershipEvidenceOwner >(); +// Failures thrown after this module signaled an owned group, so callers can +// keep the observed intervention without changing the failure's identity. +const failuresAfterSignal = new WeakSet(); + +/** Whether a thrown cleanup failure followed an observed owned-group signal. */ +export const cleanupFailureFollowedSignal = (cause: unknown): boolean => + typeof cause === "object" && cause !== null && failuresAfterSignal.has(cause); + export const cleanupOwnedProcessGroup = async ( ownership: OwnedProcessGroup, host: ProcessOwnershipHost = systemHost, +): Promise => { + const attempt = { signaled: false }; + try { + return await cleanupOwnedProcessGroupAttempt(ownership, host, attempt); + } catch (cause: unknown) { + if (attempt.signaled && typeof cause === "object" && cause !== null) + failuresAfterSignal.add(cause); + throw cause; + } +}; + +const cleanupOwnedProcessGroupAttempt = async ( + ownership: OwnedProcessGroup, + host: ProcessOwnershipHost, + attempt: { signaled: boolean }, ): Promise => { if (ownership.evidenceOwner === undefined) { let evidenceOwner = implicitEvidenceOwners.get(ownership); @@ -403,7 +433,15 @@ export const cleanupOwnedProcessGroup = async ( exitSettlement, ); if ("cleaned" in plan) return plan; - let signaled = false; + // A later verification or signal failure cannot erase earlier intervention. + const finish = ( + result: ProcessCleanupResult, + unverified: readonly OwnershipSweepUnverifiedProcess[] = plan.unverified, + ): ProcessCleanupResult => + mergeUnverifiedObservations( + attempt.signaled ? { ...result, signaled: true } : result, + unverified, + ); const pendingGroups = [...plan.signalOrder]; const pending = new Set(pendingGroups); const passes = new Map(); @@ -413,13 +451,10 @@ export const cleanupOwnedProcessGroup = async ( pending.delete(processGroupId); const seen = passes.get(processGroupId) ?? 0; if (seen >= OWNED_GROUP_REVALIDATION_LIMIT) - return mergeUnverifiedObservations( - { - cleaned: false, - reason: `owned process group ${String(processGroupId)} reappeared after ${String(OWNED_GROUP_REVALIDATION_LIMIT)} revalidation passes. This release has no runtime override.`, - }, - plan.unverified, - ); + return finish({ + cleaned: false, + reason: `owned process group ${String(processGroupId)} reappeared after ${String(OWNED_GROUP_REVALIDATION_LIMIT)} revalidation passes. This release has no runtime override.`, + }); passes.set(processGroupId, seen + 1); const revalidation = await revalidateOwnedProcessGroup( processGroupId, @@ -428,40 +463,33 @@ export const cleanupOwnedProcessGroup = async ( plan.tokenOwnedIdentities, exitSettlement, ); - if ("cleaned" in revalidation) - return mergeUnverifiedObservations(revalidation, plan.unverified); + if ("cleaned" in revalidation) return finish(revalidation); for (const groupId of revalidation.escapedProcessGroupIds) { if (groupId === processGroupId || pending.has(groupId)) continue; if ((passes.get(groupId) ?? 0) >= OWNED_GROUP_REVALIDATION_LIMIT) - return mergeUnverifiedObservations( - { - cleaned: false, - reason: `owned process group ${String(groupId)} reappeared after ${String(OWNED_GROUP_REVALIDATION_LIMIT)} revalidation passes. This release has no runtime override.`, - }, - plan.unverified, - ); + return finish({ + cleaned: false, + reason: `owned process group ${String(groupId)} reappeared after ${String(OWNED_GROUP_REVALIDATION_LIMIT)} revalidation passes. This release has no runtime override.`, + }); pending.add(groupId); pendingGroups.push(groupId); } if (revalidation.empty) continue; try { host.signalGroup(processGroupId, "SIGKILL"); - signaled = true; + attempt.signaled = true; } catch (cause: unknown) { const code = cause instanceof Error && "code" in cause ? cause.code : undefined; if (code !== "ESRCH") - return mergeUnverifiedObservations( - { - cleaned: false, - reason: `owned process group signal failed: ${errorMessage(cause)}`, - }, - plan.unverified, - ); + return finish({ + cleaned: false, + reason: `owned process group signal failed: ${errorMessage(cause)}`, + }); } } if (plan.unresolved.length > 0) - return cleanupValidationFailure(plan.unresolved, plan.unverified); + return finish(cleanupValidationFailure(plan.unresolved)); if (ownership.sweepTokenOwnedProcesses === true) { let verification: ProcessCleanupResult; try { @@ -498,26 +526,18 @@ export const cleanupOwnedProcessGroup = async ( } } catch (cause: unknown) { rethrowProcessInspectionFailure(cause); - return mergeUnverifiedObservations( - { - cleaned: false, - reason: `final process ownership verification failed: ${errorMessage(cause)}`, - }, - plan.unverified, - ); + return finish({ + cleaned: false, + reason: `final process ownership verification failed: ${errorMessage(cause)}`, + }); } - if (!verification.cleaned) - return mergeUnverifiedObservations(verification, plan.unverified); - return mergeUnverifiedObservations({ cleaned: true, signaled }, [ + if (!verification.cleaned) return finish(verification); + return finish({ cleaned: true, signaled: attempt.signaled }, [ ...plan.unverified, ...(verification.unverified ?? []), ]); } - return { - cleaned: true, - signaled, - ...(plan.unverified.length === 0 ? {} : { unverified: plan.unverified }), - }; + return finish({ cleaned: true, signaled: attempt.signaled }); }; /** Verify that no live process still carries an owned capture run token. */ @@ -1911,13 +1931,17 @@ const scanWithOwnershipEvidence = async ( fact.identityAfter.state === "readable" ? fact.identityAfter : undefined; + // A kernel thread has no user environment, so it cannot carry this run's + // token. Procfs can fail its environment read (for example with ESRCH) + // while it stays live, and root callers see no credential restriction. if ( knownUnrelated && - environmentWithheldFromInspection( - fact.token.reason, - observed?.effectiveUid, - observed?.environmentRestriction, - ) + (observed?.kernelThread === true || + environmentWithheldFromInspection( + fact.token.reason, + observed?.effectiveUid, + observed?.environmentRestriction, + )) ) { unverified.push({ pid: fact.process.pid, diff --git a/src/process/ProcessOwnershipObservation.ts b/src/process/ProcessOwnershipObservation.ts index 14410cf1c5..2b5f09c276 100644 --- a/src/process/ProcessOwnershipObservation.ts +++ b/src/process/ProcessOwnershipObservation.ts @@ -1183,11 +1183,8 @@ export const createSystemProcessOwnershipHost = ( readonly darwinModuleCachePath?: string; /** Deadline for Darwin ownership-helper compilation. */ readonly compileTimeoutMs?: number; - /** Subprocess output execution seam for testing process listing failures. */ - readonly execFileOutput?: typeof execFileOutput; } = {}, ): ProcessOwnershipHost => { - const runExecFileOutput = options.execFileOutput ?? execFileOutput; const darwinModuleCachePath = options.darwinModuleCachePath ?? hostEnvironment.REA_DARWIN_MODULE_CACHE_PATH; @@ -1226,7 +1223,7 @@ export const createSystemProcessOwnershipHost = ( if (platform === "win32") return []; let stdout: string; try { - ({ stdout } = await runExecFileOutput( + ({ stdout } = await execFileOutput( "ps", ["-axo", "pid=,ppid=,pgid=,uid=,stat=,command="], { @@ -1307,6 +1304,9 @@ export const createSystemProcessOwnershipHost = ( state: "readable" as const, identity: `linux-starttime:${facts.startTime}`, ...(facts.name === "" ? {} : { name: facts.name }), + ...(facts.kernelThread === true + ? { kernelThread: true as const } + : {}), ...(effectiveUid === undefined ? {} : { effectiveUid }), ...(environmentRestriction === undefined ? {} @@ -1634,7 +1634,7 @@ export const createSystemProcessOwnershipHost = ( environment.REA_PROCESS_RUN_ID = observation.runId; return environment; } - const { stdout } = await runExecFileOutput( + const { stdout } = await execFileOutput( "ps", ["eww", "-p", String(pid)], { diff --git a/src/process/ProcessOwnershipProcessTree.test.ts b/src/process/ProcessOwnershipProcessTree.test.ts new file mode 100644 index 0000000000..6a2b254bc1 --- /dev/null +++ b/src/process/ProcessOwnershipProcessTree.test.ts @@ -0,0 +1,70 @@ +import { expect, it } from "vitest"; +import type { ProcessTableEntry } from "./ProcessOwnership.js"; +import { descendantsOf } from "./ProcessOwnershipProcessTree.js"; + +const row = (pid: number, parentPid: number): ProcessTableEntry => ({ + pid, + parentPid, + processGroupId: 1000, + state: "S", + command: "modeled process", +}); + +it.each([false, true])( + "retains a wide family with an intermediate parent: %s", + (nested) => { + // Synthetic table rows, not launched processes. This adapter project forks + // with the production main-thread stack, exceeding its argument capacity. + const children = Array.from({ length: 130_000 }, (_, index) => + row(1002 + index, nested ? 1001 : 1000), + ); + const intermediate = row(1001, 1000); + const expected = nested ? [intermediate, ...children] : children; + const table = [row(99, 0), row(1000, 0), ...expected]; + const found = descendantsOf([1000], table); + expect(found).toHaveLength(expected.length); + expect(found.every((process, index) => process === expected[index])).toBe( + true, + ); + expect(new Set(found.map(({ pid }) => pid)).size).toBe(expected.length); + expect(found.at(-1)?.pid).toBe(131001); + }, +); + +it("keeps breadth-first table order across several parents", () => { + const table = [ + row(10, 1), + row(1, 0), + row(4, 3), + row(2, 1), + row(6, 2), + row(3, 1), + row(5, 3), + row(99, 0), + ]; + expect(descendantsOf([1], table).map(({ pid }) => pid)).toEqual([ + 10, 2, 3, 6, 4, 5, + ]); +}); + +it("excludes all selected roots and visits repeated or cyclic PIDs once", () => { + const first = row(3, 2); + const leaf = row(4, 3); + const table = Object.freeze([ + row(1, 3), + row(2, 1), + first, + { ...first, command: "later duplicate" }, + leaf, + ]); + const found = descendantsOf([1, 2, 1], table); + expect(found).toEqual([first, leaf]); + expect(found[0]).toBe(first); + expect(found[1]).toBe(leaf); +}); + +it("returns no descendants for empty or unobserved roots", () => { + const table = [row(1, 0), row(2, 1)]; + expect(descendantsOf([], table)).toEqual([]); + expect(descendantsOf([99], table)).toEqual([]); +}); diff --git a/src/process/ProcessOwnershipProcessTree.ts b/src/process/ProcessOwnershipProcessTree.ts index 01b65447cb..a7cd53533c 100644 --- a/src/process/ProcessOwnershipProcessTree.ts +++ b/src/process/ProcessOwnershipProcessTree.ts @@ -24,7 +24,8 @@ export const descendantsOf = ( if (visited.has(process.pid)) continue; visited.add(process.pid); descendants.push(process); - pending.push(...(childrenByParent.get(process.pid) ?? [])); + for (const child of childrenByParent.get(process.pid) ?? []) + pending.push(child); } return descendants; }; diff --git a/src/process/RegexMatcher.test.ts b/src/process/RegexMatcher.test.ts index b2f25b2dbb..a38174dc87 100644 --- a/src/process/RegexMatcher.test.ts +++ b/src/process/RegexMatcher.test.ts @@ -1,7 +1,6 @@ import { AnalysisSettings } from "../application/AnalysisSettings.js"; import { parseConfig } from "../config/parseConfig.js"; import { runtimeAnalysisConfig } from "../config/runtimeAnalysisSettings.js"; -import { matchRegexValues } from "./RegexMatcher.js"; import { expect, it } from "vitest"; import { @@ -15,10 +14,10 @@ import { } from "../config/regexWorkerHeapRequest.js"; import { projectAnalysisError } from "../domain/analysisErrorProjection.js"; import { + matchRegexValues, execArgvFromCommandLine, readLaunchExecArgv, readLaunchNodeOptions, - regexMatchingDeadlineError, regexMatchingLimitation, regexWorkerHeapError, resolveLaunchRegexWorkerHeap, diff --git a/src/process/capture/ProcessCaptureCleanupOutcome.test.ts b/src/process/capture/ProcessCaptureCleanupOutcome.test.ts new file mode 100644 index 0000000000..1f55f7ab16 --- /dev/null +++ b/src/process/capture/ProcessCaptureCleanupOutcome.test.ts @@ -0,0 +1,313 @@ +import { expect, it } from "vitest"; +import { emptyProcessCapture } from "../../domain/process/processCapture.fixture.js"; +import { projectAnalysisError } from "../../domain/analysisErrorProjection.js"; +import { analysisErrorProjectionSchema } from "../../contracts/errorSchemas.js"; +import { ProcessCaptureError } from "./ProcessCaptureError.js"; +import { + PROCESS_CAPTURE_CLEANUP_INTERVENTION_UNKNOWN, + ProcessCaptureResourceScope, + resolveProcessResult, + createProcessCaptureObservationBuffer, + type ProcessCaptureCleanupOutcome, + type PendingProcessCapture, +} from "./ProcessCaptureLifecycle.js"; + +const pendingCapture = (): PendingProcessCapture => { + const { cleanup, ...capture } = emptyProcessCapture(); + void cleanup; + return capture; +}; + +// An injected ownership result exercises finalization without claiming host +// process/TTY behavior or sending a signal to a real process. +const observedCleanup = (signaled: boolean, rendererFailure?: () => boolean) => + new ProcessCaptureResourceScope().release({ + timers: new Set(), + terminal: { pid: 321 }, + renderer: + rendererFailure === undefined + ? undefined + : { + dispose: async () => { + if (rendererFailure()) throw new Error("renderer cleanup denied"); + }, + }, + runId: "cleanup-facts-fixture", + temporaryRoot: "/fixture/root", + host: { + platform: "linux", + cleanupProcessGroup: async () => ({ cleaned: true, signaled }), + removeTemporaryRoot: async () => undefined, + }, + }); + +const intervention = PROCESS_CAPTURE_CLEANUP_INTERVENTION_UNKNOWN; + +it.each(["capture", "observations"] as const)( + "retains a producer's known signal after failed cleanup in %s", + async (shape) => { + let recovered = false; + const cleanup = await new ProcessCaptureResourceScope().release({ + timers: new Set(), + terminal: { pid: 321 }, + renderer: undefined, + runId: "failed-cleanup-signal-fixture", + temporaryRoot: "/fixture/root", + host: { + platform: "linux", + cleanupProcessGroup: async () => + recovered + ? { cleaned: true, signaled: false } + : { + cleaned: false, + signaled: true, + reason: "post-signal verification failed", + }, + removeTemporaryRoot: async () => undefined, + }, + }); + const observations = createProcessCaptureObservationBuffer({ + frames: [], + interactions: [], + samples: [], + eventJournal: [], + before: undefined, + }); + const failure = new Error("original terminal failure"); + try { + resolveProcessResult( + shape === "capture" ? pendingCapture() : undefined, + failure, + cleanup, + shape === "observations" ? observations : undefined, + ); + throw new Error("Expected incomplete cleanup"); + } catch (cause: unknown) { + if (!(cause instanceof ProcessCaptureError)) throw cause; + expect(cause.cause).toBe(failure); + expect(cause.cleanupIncomplete).toBe(true); + expect(cause.cleanupReport?.owned_process_group).toEqual({ + state: "unverified", + reason: "post-signal verification failed", + }); + const partial = cause.partialObservation; + if (partial === undefined) + throw new Error("Expected partial observations"); + const facts = + "capture" in partial ? partial.capture : partial.observations; + expect(facts.residual_unknowns ?? []).toContainEqual(intervention); + expect( + analysisErrorProjectionSchema.parse(projectAnalysisError(cause)), + ).toMatchObject({ + details: { + partial_observation: { + [shape]: { + residual_unknowns: expect.arrayContaining([intervention]), + }, + }, + }, + }); + if ("capture" in partial) + expect(partial.capture.settlement.cleanup_outcome).toBe("failed"); + const historical = JSON.stringify(partial); + recovered = true; + await cleanup.cleanupLease?.release(); + expect(cause.cleanupIncomplete).toBe(false); + expect(JSON.stringify(cause.partialObservation)).toBe(historical); + } finally { + recovered = true; + await cleanup.cleanupLease?.release(); + } + }, +); + +it("reports cleanup intervention even when settlement had already quiesced", async () => { + const cleanup = await observedCleanup(true); + const capture = resolveProcessResult(pendingCapture(), undefined, cleanup); + expect(capture.settlement).toMatchObject({ + state: "quiesced", + cleanup_outcome: "cleaned", + }); + expect(capture.residual_unknowns).toContainEqual(intervention); + expect(capture.cleanup.owned_process_group).toBe("verified"); +}); + +it("keeps non-signaling cleanup distinct from intervention", async () => { + const cleanup = await observedCleanup(false); + const capture = resolveProcessResult(pendingCapture(), undefined, cleanup); + expect(capture.settlement.cleanup_outcome).toBe("not_required"); + expect(capture.residual_unknowns).not.toContainEqual(intervention); +}); + +it("updates a validated capture once and does not duplicate intervention facts", async () => { + const cleanup = await observedCleanup(true); + const capture = resolveProcessResult( + emptyProcessCapture(), + undefined, + cleanup, + ); + expect(capture.settlement.cleanup_outcome).toBe("cleaned"); + const repeated = resolveProcessResult(capture, undefined, cleanup); + expect(repeated.residual_unknowns).toEqual(capture.residual_unknowns); + expect( + repeated.residual_unknowns.filter( + (unknown) => + unknown.scope === intervention.scope && + unknown.reason === intervention.reason, + ), + ).toHaveLength(1); +}); + +it("retains intervention in partial facts without replacing the execution failure", async () => { + const cleanup = await observedCleanup(true); + const failure = new Error("final snapshot failed"); + try { + resolveProcessResult(pendingCapture(), failure, cleanup); + throw new Error("Expected the execution failure"); + } catch (cause: unknown) { + if (!(cause instanceof ProcessCaptureError)) throw cause; + expect(cause.cause).toBe(failure); + expect(cause.cleanupIncomplete).toBe(false); + expect(cause.partialObservation).toMatchObject({ + capture: { + settlement: { cleanup_outcome: "cleaned" }, + residual_unknowns: expect.arrayContaining([intervention]), + }, + }); + } +}); + +it("keeps failed cleanup authoritative while retaining its known intervention", async () => { + let rendererFails = true; + const cleanup = await observedCleanup(true, () => rendererFails); + try { + resolveProcessResult(pendingCapture(), undefined, cleanup); + throw new Error("Expected incomplete cleanup"); + } catch (cause: unknown) { + if (!(cause instanceof ProcessCaptureError)) throw cause; + expect(cause.cleanupIncomplete).toBe(true); + expect(cause.partialObservation).toMatchObject({ + capture: { + settlement: { cleanup_outcome: "failed" }, + residual_unknowns: expect.arrayContaining([intervention]), + }, + }); + } finally { + rendererFails = false; + await cleanup.cleanupLease?.release(); + } +}); + +it("does not invent intervention facts for a historical report", async () => { + const observed = await observedCleanup(true); + const historical: ProcessCaptureCleanupOutcome = { + report: observed.report, + cleanupLease: undefined, + }; + const capture = resolveProcessResult( + emptyProcessCapture(), + undefined, + historical, + ); + expect(capture.settlement.cleanup_outcome).toBe("not_required"); + expect(capture.residual_unknowns).not.toContainEqual(intervention); +}); + +it("preserves saved intervention when a later report has no signal fact", async () => { + const cleanup = await observedCleanup(true); + const capture = resolveProcessResult(pendingCapture(), undefined, cleanup); + const historical: ProcessCaptureCleanupOutcome = { + report: cleanup.report, + cleanupLease: undefined, + }; + const restored = resolveProcessResult(capture, undefined, historical); + expect(restored.settlement).toEqual(capture.settlement); + expect(restored.residual_unknowns).toEqual(capture.residual_unknowns); +}); + +it.each([false, true])( + "retains intervention before a capture exists, including failed cleanup: %s", + async (cleanupFails) => { + let rendererFails = cleanupFails; + const cleanup = await observedCleanup(true, () => rendererFails); + const observations = createProcessCaptureObservationBuffer({ + frames: [{ sequence: 0, at_ms: 0, data: "collected terminal output" }], + interactions: [], + samples: [], + eventJournal: [], + before: undefined, + }); + observations.settlement = { + state: "available", + value: { state: "quiesced", elapsed_ms: 12 }, + }; + const failure = new Error("final filesystem snapshot failed"); + try { + resolveProcessResult(undefined, failure, cleanup, observations); + throw new Error("Expected incomplete observations"); + } catch (cause: unknown) { + if (!(cause instanceof ProcessCaptureError)) throw cause; + expect(cause.cause).toBe(failure); + expect(cause.cleanupIncomplete).toBe(cleanupFails); + expect(cause.partialObservation).toMatchObject({ + observations: { + frames: observations.frames, + settlement: observations.settlement, + filesystem_snapshots: { + after: { state: "unavailable" }, + }, + residual_unknowns: expect.arrayContaining([intervention]), + }, + }); + expect( + analysisErrorProjectionSchema.parse(projectAnalysisError(cause)), + ).toMatchObject({ + details: { + partial_observation: { + observations: { + residual_unknowns: expect.arrayContaining([intervention]), + }, + }, + }, + }); + } finally { + rendererFails = false; + await cleanup.cleanupLease?.release(); + } + }, +); + +it.each([false, undefined])( + "does not infer intervention for incomplete observations from %s", + async (signaled) => { + const observed = await observedCleanup(false); + const cleanup: ProcessCaptureCleanupOutcome = { + report: observed.report, + cleanupLease: observed.cleanupLease, + ...(signaled === undefined ? {} : { ownedProcessesSignaled: signaled }), + }; + const observations = createProcessCaptureObservationBuffer({ + frames: [], + interactions: [], + samples: [], + eventJournal: [], + before: undefined, + }); + try { + resolveProcessResult( + undefined, + new Error("terminal read failed"), + cleanup, + observations, + ); + throw new Error("Expected incomplete observations"); + } catch (cause: unknown) { + if (!(cause instanceof ProcessCaptureError)) throw cause; + const partial = cause.partialObservation; + if (partial === undefined || !("observations" in partial)) + throw new Error("Expected incomplete observations"); + expect(partial.observations).not.toHaveProperty("residual_unknowns"); + expect(partial.observations.settlement.state).toBe("unavailable"); + } + }, +); diff --git a/src/process/capture/ProcessCaptureLifecycle.test.ts b/src/process/capture/ProcessCaptureLifecycle.test.ts index 781ae11c59..4fa526f8d3 100644 --- a/src/process/capture/ProcessCaptureLifecycle.test.ts +++ b/src/process/capture/ProcessCaptureLifecycle.test.ts @@ -291,7 +291,7 @@ it("records no exit code when no exit was observed", async () => { result.finalization?.exit_code, "an unobserved exit has no code to record", ).toBeNull(); -}, 10_000); +}); it("records every delivery result and publishes attempts incrementally", async () => { snapshots.length = 0; @@ -422,7 +422,7 @@ it("stops waiting for a hung delivery once the exit is observed", async () => { }, ]); expect(exit.signal, "the observed exit is kept").toBe(0); -}, 10_000); +}); it("retains a thrown signal error as an unverified delivery reason", async () => { const fake = fakeTerminal( @@ -491,7 +491,7 @@ it("stops waiting when the escalation delivery never settles and no exit arrives reason: "delivery did not settle within 1000 ms", }); expect(result.finalization?.elapsed_ms, "no exit was observed").toBeNull(); -}, 10_000); +}); const activeTimers = (): number => process.getActiveResourcesInfo().filter((name) => name === "Timeout").length; diff --git a/src/process/capture/ProcessCaptureLifecycle.ts b/src/process/capture/ProcessCaptureLifecycle.ts index ea1029b8ae..d45047fba3 100644 --- a/src/process/capture/ProcessCaptureLifecycle.ts +++ b/src/process/capture/ProcessCaptureLifecycle.ts @@ -31,6 +31,7 @@ import { } from "./ProcessCaptureRead.js"; import { classifyFilesystemEffects } from "./ProcessFilesystemEffects.js"; import { + cleanupFailureFollowedSignal, cleanupOwnedProcessGroup, type OwnedProcessGroup, type ProcessOwnershipBaseline, @@ -1096,12 +1097,18 @@ const releaseCapturedProcessGroup = async ( { readonly sweepTokenOwnedProcesses: true } >, host: ProcessCaptureCleanupHost, -): Promise => { +): Promise<{ + readonly report: ProcessCaptureCleanupReport["owned_process_group"]; + readonly signaled: boolean | undefined; +}> => { if (host.platform === "win32") return { - state: "unverified", - reason: - "owned process cleanup is unverifiable on Windows without process-job authority", + report: { + state: "unverified", + reason: + "owned process cleanup is unverifiable on Windows without process-job authority", + }, + signaled: undefined, }; let outcome: ProcessCaptureCleanupReport["owned_process_group"] = { state: "cleaned", @@ -1115,19 +1122,23 @@ const releaseCapturedProcessGroup = async ( unverified.push(...(cleaned.unverified ?? [])); if (!cleaned.cleaned) outcome = { state: "unverified", reason: cleaned.reason }; - return unverified.length === 0 - ? outcome - : { - ...outcome, - unverified_processes: [ - ...new Map( - unverified.map(({ pid, diagnostic }) => [ - `${String(pid)}:${diagnostic}`, - { pid, reason: diagnostic }, - ]), - ).values(), - ], - }; + return { + signaled: cleaned.signaled, + report: + unverified.length === 0 + ? outcome + : { + ...outcome, + unverified_processes: [ + ...new Map( + unverified.map(({ pid, diagnostic }) => [ + `${String(pid)}:${diagnostic}`, + { pid, reason: diagnostic }, + ]), + ).values(), + ], + }, + }; }; export interface ProcessCaptureResourceCleanupOptions { @@ -1148,6 +1159,8 @@ export interface ProcessCaptureResourceCleanupOptions { /** Historical cleanup facts and the exact authority that can confirm recovery. */ export interface ProcessCaptureCleanupOutcome { readonly report: ProcessCaptureCleanupReport; + /** Missing historical or failed-attempt facts do not establish no signaling. */ + readonly ownedProcessesSignaled?: boolean; /** Undefined means that only historical facts, not an owner, are available. */ readonly cleanupLease: AnalysisCleanupLease | undefined; /** Explicit failures observed by this cleanup owner, absent for plain historical reports. */ @@ -1168,6 +1181,7 @@ class ProcessCaptureResourceOwner { | ProcessCaptureCleanupReport["owned_process_group"] | undefined; #temporaryRoot: ProcessCaptureCleanupReport["temporary_root"] | undefined; + #ownedProcessesSignaled: boolean | undefined = false; constructor(private readonly options: ProcessCaptureResourceCleanupOptions) { this.#host = options.host ?? processCaptureCleanupHost; @@ -1249,29 +1263,30 @@ class ProcessCaptureResourceOwner { async release(): Promise { try { await this.cleanupLease.release(); - return { - report: this.report, - cleanupLease: this.cleanupLease, - ...(this.#cleanupInspections.size === 0 - ? {} - : { inspectionFailures: [...this.#cleanupInspections] }), - }; + return this.#outcome(this.report); } catch (cause: unknown) { if ( !(cause instanceof ProcessCaptureError) || cause.cleanupReport === undefined ) throw cause; - return { - report: cause.cleanupReport, - cleanupLease: this.cleanupLease, - ...(this.#cleanupInspections.size === 0 - ? {} - : { inspectionFailures: [...this.#cleanupInspections] }), - }; + return this.#outcome(cause.cleanupReport); } } + #outcome(report: ProcessCaptureCleanupReport): ProcessCaptureCleanupOutcome { + return { + report, + cleanupLease: this.cleanupLease, + ...(this.#ownedProcessesSignaled === undefined + ? {} + : { ownedProcessesSignaled: this.#ownedProcessesSignaled }), + ...(this.#cleanupInspections.size === 0 + ? {} + : { inspectionFailures: [...this.#cleanupInspections] }), + }; + } + async #releaseRenderer(): Promise { if ( this.options.renderer === undefined || @@ -1306,7 +1321,7 @@ class ProcessCaptureResourceOwner { return; } try { - this.#ownedProcessGroup = await releaseCapturedProcessGroup( + const outcome = await releaseCapturedProcessGroup( { runId: this.options.runId, leaderPid: this.options.terminal.pid, @@ -1322,7 +1337,14 @@ class ProcessCaptureResourceOwner { }, this.#host, ); + this.#ownedProcessGroup = outcome.report; + this.#recordSignaled(outcome.signaled); } catch (cause: unknown) { + // A thrown attempt keeps a signal it is known to have sent; otherwise + // whether it signaled before failing is unknown. + this.#recordSignaled( + cleanupFailureFollowedSignal(cause) ? true : undefined, + ); const normalized = normalizeCaptureFailure(cause); if ( normalized instanceof ProcessCaptureInspectionFailure && @@ -1346,6 +1368,17 @@ class ProcessCaptureResourceOwner { ); } + // Retain observed intervention across retries; an unknown attempt followed + // by a non-signaling retry cannot prove that no earlier signal was sent. + #recordSignaled(signaled: boolean | undefined): void { + this.#ownedProcessesSignaled = + this.#ownedProcessesSignaled === true || signaled === true + ? true + : this.#ownedProcessesSignaled === undefined || signaled === undefined + ? undefined + : false; + } + async #releaseTemporaryRoot(): Promise { if (this.#temporaryRoot?.state === "cleaned") return; const owned = this.#ownedProcessGroup; @@ -1677,6 +1710,61 @@ export interface TerminalCaptureRetention { readonly normalized_terminal: NormalizedTerminalRetention; } +export const PROCESS_CAPTURE_CLEANUP_INTERVENTION_UNKNOWN = { + scope: "process" as const, + reason: + "Owned-process cleanup sent termination signals. Later terminal and filesystem observations may omit work ended by this intervention.", +}; + +const cleanupResidualUnknowns = ( + unknowns: UnverifiedProcessCapture["residual_unknowns"], + signaled: boolean | undefined, +): UnverifiedProcessCapture["residual_unknowns"] => + signaled === true && + !unknowns.some( + (unknown) => + unknown.scope === PROCESS_CAPTURE_CLEANUP_INTERVENTION_UNKNOWN.scope && + unknown.reason === PROCESS_CAPTURE_CLEANUP_INTERVENTION_UNKNOWN.reason, + ) + ? [...unknowns, PROCESS_CAPTURE_CLEANUP_INTERVENTION_UNKNOWN] + : unknowns; + +/** Interpret settlement and observed cleanup action once for full/partial output. */ +export const processCaptureCleanupFacts = ( + capture: Pick< + PendingProcessCapture | ProcessCapture, + "settlement" | "residual_unknowns" + >, + outcome: { + readonly ownedProcessesSignaled: boolean | undefined; + }, +): Pick => ({ + settlement: + capture.settlement.state === "quiesced" + ? { + ...capture.settlement, + cleanup_outcome: + outcome.ownedProcessesSignaled === true || + ("cleanup_outcome" in capture.settlement && + capture.settlement.cleanup_outcome === "cleaned") + ? "cleaned" + : "not_required", + } + : { + ...capture.settlement, + cleanup_outcome: + outcome.ownedProcessesSignaled !== true && + "cleanup_outcome" in capture.settlement && + capture.settlement.cleanup_outcome === "failed" + ? "failed" + : "cleaned", + }, + residual_unknowns: cleanupResidualUnknowns( + capture.residual_unknowns, + outcome.ownedProcessesSignaled, + ), +}); + export const resolveProcessResult = ( capture: PendingProcessCapture | ProcessCapture | undefined, executionFailure: unknown, @@ -1703,7 +1791,17 @@ export const resolveProcessResult = ( if (capture === undefined) { if (normalizedObservations !== undefined) { const partialResult = partialProcessCaptureObservationSchema.safeParse({ - observations: normalizedObservations, + observations: { + ...normalizedObservations, + ...(cleanupOutcome.ownedProcessesSignaled === true + ? { + residual_unknowns: cleanupResidualUnknowns( + normalizedObservations.residual_unknowns ?? [], + cleanupOutcome.ownedProcessesSignaled, + ), + } + : {}), + }, cleanup, execution_failure: executionFailureReason ?? null, }); @@ -1718,17 +1816,18 @@ export const resolveProcessResult = ( return data; })() : capture; - const settlement = - cleanupFailure !== undefined - ? { ...capture.settlement, cleanup_outcome: "failed" as const } - : capture.settlement.state === "quiesced" - ? { - ...capture.settlement, - cleanup_outcome: "not_required" as const, - } - : { ...capture.settlement, cleanup_outcome: "cleaned" as const }; + const cleanupFacts = processCaptureCleanupFacts(capture, { + ownedProcessesSignaled: cleanupOutcome.ownedProcessesSignaled, + }); const partialResult = partialProcessCaptureObservationSchema.safeParse({ - capture: { ...captureData, settlement }, + capture: { + ...captureData, + ...cleanupFacts, + settlement: + cleanupFailure === undefined + ? cleanupFacts.settlement + : { ...cleanupFacts.settlement, cleanup_outcome: "failed" }, + }, cleanup, execution_failure: executionFailureReason ?? null, }); @@ -1812,18 +1911,23 @@ export const resolveProcessResult = ( if (capture === undefined) throw new ProcessCaptureError("process capture produced no result"); const unverifiedProcesses = cleanup.owned_process_group.unverified_processes; - if ("cleanup" in capture && unverifiedProcesses === undefined) return capture; - - const settlement: UnverifiedProcessCapture["settlement"] = - capture.settlement.state === "quiesced" - ? { ...capture.settlement, cleanup_outcome: "not_required" } - : { ...capture.settlement, cleanup_outcome: "cleaned" }; + const cleanupFacts = processCaptureCleanupFacts(capture, { + ownedProcessesSignaled: cleanupOutcome.ownedProcessesSignaled, + }); + if ( + "cleanup" in capture && + unverifiedProcesses === undefined && + capture.settlement.cleanup_outcome === + cleanupFacts.settlement.cleanup_outcome && + capture.residual_unknowns === cleanupFacts.residual_unknowns + ) + return capture; try { return parseProcessCapture({ ...capture, - settlement, + ...cleanupFacts, residual_unknowns: [ - ...capture.residual_unknowns, + ...cleanupFacts.residual_unknowns, ...(unverifiedProcesses ?? []).map(({ pid, reason }) => ({ scope: "process" as const, reason: `Ownership of unrelated process ${String(pid)} could not be verified; it was left untouched: ${reason}`, diff --git a/src/process/capture/ProcessHarness.test.ts b/src/process/capture/ProcessHarness.test.ts index 3743042559..3b8194620b 100644 --- a/src/process/capture/ProcessHarness.test.ts +++ b/src/process/capture/ProcessHarness.test.ts @@ -9,6 +9,7 @@ import { } from "./ProcessHarness.js"; import { settleProcessCaptureJournal, + PROCESS_CAPTURE_CLEANUP_INTERVENTION_UNKNOWN, ProcessCaptureResourceScope, createProcessCaptureObservationBuffer, prepareProcessCapture, @@ -479,6 +480,7 @@ it("retains unrelated withheld processes through cleanup, capture, and JSON", as }); expect(capture.residual_unknowns).toEqual([ ...original.residual_unknowns, + PROCESS_CAPTURE_CLEANUP_INTERVENTION_UNKNOWN, { scope: "process", reason: expect.stringContaining("900 could not be verified"), @@ -712,45 +714,67 @@ it("retains completed observations when finalization fails after clean cleanup", ).toBe(true); }); -it("preserves cancellation while projecting observations and successful cleanup", () => { - const capture = parseProcessCapture({ - ...accountFullyObservedProcessCapture({ - ...emptyProcessCapture(), - frames: [{ sequence: 0, at_ms: 0, data: "observed before cancellation" }], - }), - event_journal: [], - }); - const cleanup = { - owned_process_group: { state: "cleaned" as const, reason: null }, - terminal_renderer: { state: "cleaned" as const, reason: null }, - temporary_root: { state: "cleaned" as const, reason: null }, - }; - let error: ProcessCaptureError | undefined; - try { - resolveProcessResult( - capture, - processCaptureCancelled(), - historicalCleanupOutcome(cleanup), - ); - } catch (cause: unknown) { - if (!(cause instanceof ProcessCaptureError)) throw cause; - error = cause; - } - if (error === undefined) throw new Error("expected cancellation"); - - expect(projectAnalysisError(error)).toMatchObject({ - code: "cancelled", - message: expect.stringMatching(/process capture.*cancelled.*again/iu), - details: { - operation: "process_capture", - cleanup: "complete", - cleanup_report: cleanup, - partial_observation: { - capture: { frames: [{ data: "observed before cancellation" }] }, +it.each([false, true])( + "preserves finalization cancellation alongside cleanup intervention=%s", + async (signaled) => { + const capture = parseProcessCapture({ + ...accountFullyObservedProcessCapture({ + ...emptyProcessCapture(), + frames: [ + { sequence: 0, at_ms: 0, data: "observed before cancellation" }, + ], + }), + event_journal: [], + }); + const cleanup = await new ProcessCaptureResourceScope().release({ + timers: new Set(), + terminal: { pid: 321 }, + renderer: undefined, + runId: "finalization-cancellation", + temporaryRoot: "/fixture/root", + host: { + platform: "linux", + cleanupProcessGroup: async () => ({ cleaned: true, signaled }), + removeTemporaryRoot: async () => undefined, }, - }, - }); -}); + }); + const reason = { request: "cancel after resource cleanup" }; + let error: ProcessCaptureError | undefined; + try { + resolveProcessResult(capture, processCaptureCancelled(reason), cleanup); + } catch (cause: unknown) { + if (!(cause instanceof ProcessCaptureError)) throw cause; + error = cause; + } + if (error === undefined) throw new Error("expected cancellation"); + + expect(error.cause).toBe(reason); + expect(projectAnalysisError(error)).toMatchObject({ + code: "cancelled", + message: expect.stringMatching(/process capture.*cancelled.*again/iu), + details: { + operation: "process_capture", + cleanup: "complete", + cleanup_report: cleanup.report, + partial_observation: { + capture: { + frames: [{ data: "observed before cancellation" }], + settlement: { + cleanup_outcome: signaled ? "cleaned" : "not_required", + }, + ...(signaled + ? { + residual_unknowns: expect.arrayContaining([ + PROCESS_CAPTURE_CLEANUP_INTERVENTION_UNKNOWN, + ]), + } + : {}), + }, + }, + }, + }); + }, +); it("fails closed on Windows before resolving or launching scenario paths", async () => { const scenario = processScenarioSchema.parse({ diff --git a/src/process/capture/ProcessHarness.ts b/src/process/capture/ProcessHarness.ts index aa765e1949..048b6174e7 100644 --- a/src/process/capture/ProcessHarness.ts +++ b/src/process/capture/ProcessHarness.ts @@ -54,6 +54,7 @@ import { observeSettlement, prepareProcessCapture, resolveProcessResult, + processCaptureCleanupFacts, settleProcessCaptureJournal, type ProcessCaptureCleanupHost, type ProcessCaptureObservationBuffer, @@ -567,13 +568,11 @@ const finishProcessRun = async (options: { const cleanupFailure = cleanupReportFailure(cleanup); let verifiedCapture: ProcessCapture | undefined; if (capture !== undefined && cleanupFailure === undefined) { - const settlement: UnverifiedProcessCapture["settlement"] = - capture.settlement.state === "quiesced" - ? { ...capture.settlement, cleanup_outcome: "not_required" } - : { ...capture.settlement, cleanup_outcome: "cleaned" }; const completedCapture: UnverifiedProcessCapture = { ...capture, - settlement, + ...processCaptureCleanupFacts(capture, { + ownedProcessesSignaled: cleanupOutcome.ownedProcessesSignaled, + }), cleanup: { owned_process_group: "verified", temporary_root: "removed", diff --git a/src/process/capture/ProcessSamplingShutdown.test.ts b/src/process/capture/ProcessSamplingShutdown.test.ts index 78fa772144..34c4ac0a00 100644 --- a/src/process/capture/ProcessSamplingShutdown.test.ts +++ b/src/process/capture/ProcessSamplingShutdown.test.ts @@ -52,10 +52,6 @@ it.each(["plain", "all-cancel", "mixed-fd", "opaque-peer"] as const)( configurable: true, value: "linux", }); - let started!: () => void; - const ready = new Promise((resolve) => { - started = resolve; - }); let original: unknown; const physical = new Error("opaque physical sibling rejection"); const observations = new Map([ @@ -68,7 +64,6 @@ it.each(["plain", "all-cancel", "mixed-fd", "opaque-peer"] as const)( .spyOn(systemProcessOwnershipHost, "environment") .mockImplementation(async (_pid, signal) => { if (signal === undefined) throw new Error("Missing sampling signal"); - started(); await new Promise((resolve) => { if (signal.aborted) resolve(); else @@ -122,19 +117,25 @@ it.each(["plain", "all-cancel", "mixed-fd", "opaque-peer"] as const)( }); onTestFinished(async () => { file?.allow(); - await scope.close(); - await stop().catch((cause: unknown) => { - if ( - !(cause instanceof ProcessCaptureInspectionFailure) && - !(cause instanceof ProcessCaptureSamplingFailure) - ) - throw cause; - }); - vi.restoreAllMocks(); - if (platform !== undefined) - Object.defineProperty(process, "platform", platform); + try { + try { + await scope.close(); + } finally { + await stop().catch((cause: unknown) => { + if ( + !(cause instanceof ProcessCaptureInspectionFailure) && + !(cause instanceof ProcessCaptureSamplingFailure) + ) + throw cause; + }); + } + } finally { + vi.restoreAllMocks(); + if (platform !== undefined) + Object.defineProperty(process, "platform", platform); + } }); - await ready; + await vi.waitFor(() => expect(environment).toHaveBeenCalledOnce()); const outcome = stop(); if (kind === "opaque-peer") { const failure: unknown = await outcome.catch((cause: unknown) => cause); @@ -174,6 +175,5 @@ it.each(["plain", "all-cancel", "mixed-fd", "opaque-peer"] as const)( throw new Error("Expected batch"); expect(original.observations).toBe(observations); } - expect(environment).toHaveBeenCalledOnce(); }, ); diff --git a/src/process/capture/TerminalRenderer.test.ts b/src/process/capture/TerminalRenderer.test.ts index 7bd1ff7fec..e670054868 100644 --- a/src/process/capture/TerminalRenderer.test.ts +++ b/src/process/capture/TerminalRenderer.test.ts @@ -204,10 +204,13 @@ it.each(["write", "resize"] as const)( ); await expect(renderer.frames()).rejects.toBe(failure); } finally { - await scope.close(); - terminalDispose.mockRestore(); - addonDispose.mockRestore(); - await rm(temporaryRoot, { recursive: true, force: true }); + try { + await scope.close(); + } finally { + terminalDispose.mockRestore(); + addonDispose.mockRestore(); + await rm(temporaryRoot, { recursive: true, force: true }); + } } }, ); @@ -244,9 +247,12 @@ it("retains disposal failures for a later cleanup retry", async () => { ); expect(terminalDispose).toHaveBeenCalledTimes(2); } finally { - await scope.close(); - terminalDispose.mockRestore(); - await rm(temporaryRoot, { recursive: true, force: true }); + try { + await scope.close(); + } finally { + terminalDispose.mockRestore(); + await rm(temporaryRoot, { recursive: true, force: true }); + } } }); @@ -327,8 +333,11 @@ it("renders retained chunks once after late PID knowledge, preserving event time expect(frame.serialized_state).toContain("child=selected"); } } finally { - await renderer.dispose(); - write.mockRestore(); + try { + await renderer.dispose(); + } finally { + write.mockRestore(); + } } }); diff --git a/src/providers/AnalysisClientFailure.test.ts b/src/providers/AnalysisClientFailure.test.ts index 82dbeb6c99..da9e735388 100644 --- a/src/providers/AnalysisClientFailure.test.ts +++ b/src/providers/AnalysisClientFailure.test.ts @@ -163,6 +163,7 @@ it("keeps the structured limit when a resource refusal changes operation", () => if (!(attributed instanceof AnalysisResourceConstraintError)) throw new Error("expected a resource constraint"); expect(attributed.operation).toBe("binary_overview"); + expect(attributed.retryable).toBe(false); expect(attributed.limit).toEqual(source.limit); expect(projectAnalysisError(attributed)).toMatchObject({ code: "resource_constraint", diff --git a/src/providers/OwnedCommandCleanup.ts b/src/providers/OwnedCommandCleanup.ts index cb222c871e..0e31d03126 100644 --- a/src/providers/OwnedCommandCleanup.ts +++ b/src/providers/OwnedCommandCleanup.ts @@ -124,7 +124,7 @@ export const retainOwnedCommandCleanup = ( }; /** A cancellation keeps its exact inspection record without exposing machine observations. */ -export class ProcessInspectionAnalysisCancelledError extends AnalysisCancelledError { +class ProcessInspectionAnalysisCancelledError extends AnalysisCancelledError { constructor( operation: string, readonly inspectionFailure: ProcessInspectionFailure, diff --git a/src/python/PythonPycProvider.ts b/src/python/PythonPycProvider.ts index 706176c8e2..30b2da8fc6 100644 --- a/src/python/PythonPycProvider.ts +++ b/src/python/PythonPycProvider.ts @@ -1,10 +1,10 @@ import { PYTHON_PYC_PROVIDER_IDENTITY } from "./PythonPycProviderIdentity.js"; import { + projectSelectedArtifactFileFailure, projectStableArtifactFailure, stableArtifactInputRecovery, projectArtifactCloseFailure, } from "../artifacts/StableArtifactFailure.js"; -import { isSelectedRegularFileError } from "../filesystem/RegularFile.js"; import { artifactFileCancellationCause, artifactFilePrimaryFacts, @@ -20,7 +20,6 @@ import type { PythonPycPort } from "../application/python/PythonPycPort.js"; import type { ExecutionOptions } from "../application/AnalysisProvider.js"; import { AnalysisError } from "../domain/analysisErrorBase.js"; import { - AnalysisAccessDeniedError, AnalysisArtifactChangedError, AnalysisCancelledError, AnalysisInputError, @@ -142,20 +141,13 @@ const failureForPrimary = ( ], ); } - if (cause instanceof Error && "code" in cause) { - if (cause.code === "EACCES" || cause.code === "EPERM") - return new AnalysisAccessDeniedError(OPERATION, input.path, cause.code, { - cause, - }); - if (isSelectedRegularFileError(cause)) - return new AnalysisInputError(OPERATION, { cause }, [ - { - path: ["path"], - reason: "invalid_value", - message: `Selected pyc could not be read (${String(cause.code)}): ${input.path}.`, - }, - ]); - } + const fileFailure = projectSelectedArtifactFileFailure( + cause, + OPERATION, + input.path, + "pyc", + ); + if (fileFailure !== undefined) return fileFailure; return new ProviderAdapterError(PYTHON_PYC_PROVIDER_IDENTITY.id, OPERATION, { cause, diagnostics: { diff --git a/src/reference/ReferenceSourceReaderEntries.ts b/src/reference/ReferenceSourceReaderEntries.ts index 39dc66fa76..adac16d650 100644 --- a/src/reference/ReferenceSourceReaderEntries.ts +++ b/src/reference/ReferenceSourceReaderEntries.ts @@ -252,8 +252,7 @@ const scheduleObservedDirectories = ( state: TraversalState, directories: PendingDirectory[], ): void => { - directories.reverse(); - state.pending.push(...directories); + for (const directory of directories.reverse()) state.pending.push(directory); }; const directoryReadFailure = (cause: unknown): FilesystemFailureDetail => { diff --git a/src/reference/ReferenceSourceReaderFile.ts b/src/reference/ReferenceSourceReaderFile.ts index 1f9f853281..3fb2c1214b 100644 --- a/src/reference/ReferenceSourceReaderFile.ts +++ b/src/reference/ReferenceSourceReaderFile.ts @@ -283,7 +283,8 @@ const readStableFileEntry = async ( total: contents.total, ...(signal === undefined ? {} : { signal }), }); - } catch (cause: unknown) { + } catch (caught: unknown) { + let cause = caught; if (cause instanceof RegularFileAdmissionFailure) { retain(cause.owner); cause = cause.cause; diff --git a/src/rizin/RizinCommand.ts b/src/rizin/RizinCommand.ts index 7ed8566054..9cfec0aee3 100644 --- a/src/rizin/RizinCommand.ts +++ b/src/rizin/RizinCommand.ts @@ -21,10 +21,11 @@ export const rizinCommand = (input: RizinInput): readonly string[] => [ absoluteCallerPath(input.path), ]; -/** What REA cannot attest about the decoder and map inputs it passed to Rizin. */ +/** What REA cannot attest about command outcomes and selected decoder/map inputs. */ export const rizinCommandLimitations = ( input: RizinInput, ): readonly string[] => [ + "completion_status covers process completion and Rizin shell refusals only. A statement that ran can still fail while Rizin returns exit_code 0, so inspect stderr before treating an empty response as absence.", ...(input.asm_arch === undefined && input.asm_bits === undefined && input.asm_cpu === undefined @@ -38,3 +39,43 @@ export const rizinCommandLimitations = ( "With base_address, REA passed -n and -m so that Rizin maps file offset 0 at that address as a raw image; REA does not confirm the map, which oml shows. -n skips Rizin's format loaders, so expect no sections, symbols or entry point, and arch, bits and byte order stay at Rizin's defaults unless the call selects them.", ]), ]; + +/** + * Rizin's shell logs exactly these lines when it refuses a statement + * (librz/core/cmd/cmd.c, unchanged from 0.6 through 0.9.1). Rizin then skips + * the remaining statements and still exits 0. Other ERROR lines come from + * commands that ran, so they are diagnostics rather than refusals. + */ +const RIZIN_SHELL_REFUSALS: readonly { + readonly line: RegExp; + readonly refused: (match: RegExpExecArray) => string; +}[] = [ + { + line: /^ERROR: Command '(.*)' does not exist\.$/u, + refused: (match) => `the unknown command ${JSON.stringify(match[1])}`, + }, + { + line: /^ERROR: Wrong number of arguments passed to `(.*)`, see its help with `.*\?`$/u, + refused: (match) => + `a wrong argument count for ${JSON.stringify(match[1])}`, + }, + { + line: /^ERROR: core: Error while executing command: /u, + refused: () => "an invalid statement", + }, + { + line: /^ERROR: core: Error while parsing command: /u, + refused: () => "input that failed to parse", + }, +]; + +/** Explain the first Rizin shell refusal in a completed process's stderr. */ +export const rizinCommandRejection = (stderr: string): string | undefined => { + for (const text of stderr.split(/\r?\n/u)) + for (const { line, refused } of RIZIN_SHELL_REFUSALS) { + const match = line.exec(text); + if (match !== null) + return `Rizin refused ${refused(match)} (ERROR line on stderr). It skips a refused statement and every later one, so stdout holds at most the output of earlier statements; empty stdout is not an empty result. Correct the statement, using ? for Rizin's help, and run it again.`; + } + return undefined; +}; diff --git a/src/rizin/RizinDebugSessionManager.ts b/src/rizin/RizinDebugSessionManager.ts index 4c525ed61f..5dc1335797 100644 --- a/src/rizin/RizinDebugSessionManager.ts +++ b/src/rizin/RizinDebugSessionManager.ts @@ -22,6 +22,11 @@ import { AnalysisInputError, AnalysisTimeoutError, } from "../domain/analysisErrorCore.js"; +import { + RIZIN_DEBUG_SESSION_TOOLS, + stoppedDebuggerSessionError, + unknownDebuggerSessionError, +} from "../domain/debuggerSessionErrors.js"; import type { AnalysisError, AnalysisErrorOptions, @@ -224,9 +229,18 @@ interface RizinDebugSession { frameChunks: Buffer[]; bufferedFrameBytes: number; ready: boolean; + /** start returned this session's ID, so the caller must close it. */ + published: boolean; + closeRequested: boolean; exited: boolean; commandTail: Promise; protocolError: string | undefined; + /** + * Why the session stopped accepting commands. Cleanup synchronization + * clears protocolError once the transport is correlated again, but the + * session stays stopped, so the diagnosis is retained here. + */ + stopReason: string | undefined; cleanupSynchronization: | { readonly marker: string; @@ -332,9 +346,6 @@ export class RizinDebugSessionManager { if (!parsed.ok) throw parsed.error; return parsed.value; } - get #maxCommandOutputBytes(): number { - return this.#commandOutput.value; - } async start( input: { @@ -367,7 +378,7 @@ export class RizinDebugSessionManager { "start_rizin_debug_session", "memory", `Rizin session capacity reached (${String(this.#maxSessions.value)} sessions).`, - "Close a Rizin debug session before starting another.", + "Close a Rizin debug session before starting another; a stopped session that rizin_debug_session_status reports closed still holds its slot until close_rizin_debug_session releases it.", this.#maxSessions, { max_sessions: this.#maxSessions.value, @@ -612,9 +623,12 @@ export class RizinDebugSessionManager { frameChunks: [], bufferedFrameBytes: 0, ready: false, + published: false, + closeRequested: false, exited: false, commandTail: Promise.resolve(), protocolError: undefined, + stopReason: undefined, cleanupSynchronization: undefined, commandCaptureActive: false, commandOutputDropped: false, @@ -654,6 +668,7 @@ export class RizinDebugSessionManager { if (session.protocolError !== undefined) throw new Error(session.protocolError); session.ready = true; + session.published = true; return ok({ session_id: id, backend: input.backend ?? null }); } catch (caught) { const protocolFailure = session.protocolError; @@ -722,15 +737,22 @@ export class RizinDebugSessionManager { > > { const session = this.#sessions.get(sessionId); - if ( - session === undefined || - !session.ready || - session.exited || - session.protocolError !== undefined || - session.closing || - this.#closingAll - ) - return err(new AnalysisInputError("rizin_debug_command")); + if (session === undefined) + return err( + unknownDebuggerSessionError( + RIZIN_DEBUG_SESSION_TOOLS, + "rizin_debug_command", + ), + ); + const stopped = this.#stopReason(session); + if (stopped !== undefined) + return err( + stoppedDebuggerSessionError( + RIZIN_DEBUG_SESSION_TOOLS, + "rizin_debug_command", + stopped, + ), + ); if (signal?.aborted) return err(rizinCancellation("rizin_debug_command", signal)); if (session.pendingCommandCount >= this.#maxPendingCommands.value) @@ -785,14 +807,15 @@ export class RizinDebugSessionManager { acquired = true; if (signal?.aborted) return err(rizinCancellation("rizin_debug_command", signal)); - if ( - !session.ready || - session.exited || - session.closing || - this.#closingAll || - session.protocolError !== undefined - ) - return err(new AnalysisInputError("rizin_debug_command")); + const stoppedWhileQueued = this.#stopReason(session); + if (stoppedWhileQueued !== undefined) + return err( + stoppedDebuggerSessionError( + RIZIN_DEBUG_SESSION_TOOLS, + "rizin_debug_command", + stoppedWhileQueued, + ), + ); if (hasUnclaimedRizinFrames(session)) { const message = "Unsolicited Rizin frames arrived before a command; command output can no longer be safely correlated."; @@ -1071,27 +1094,40 @@ export class RizinDebugSessionManager { readonly recent_output_truncated: boolean; readonly diagnostics_truncated: boolean; readonly output_invalid_utf8: boolean; + readonly closed_reason?: string; } | undefined { const session = this.#sessions.get(sessionId); - return session === undefined - ? undefined - : { - session_id: session.id, - state: - session.exited || - session.protocolError !== undefined || - session.closing || - !session.ready - ? "closed" - : "ready", - inferior_tty: session.inferiorTty, - recent_output: [...session.history], - recent_output_truncated: session.historyTruncated, - diagnostics_truncated: - session.supervisor.snapshot().diagnosticTruncated === true, - output_invalid_utf8: session.invalidUtf8Observed, - }; + if (session === undefined) return undefined; + const stopped = this.#stopReason(session); + return { + session_id: session.id, + state: stopped === undefined ? "ready" : "closed", + inferior_tty: session.inferiorTty, + recent_output: [...session.history], + recent_output_truncated: session.historyTruncated, + diagnostics_truncated: + session.supervisor.snapshot().diagnosticTruncated === true, + output_invalid_utf8: session.invalidUtf8Observed, + ...(stopped === undefined ? {} : { closed_reason: stopped }), + }; + } + + /** Why a retained session accepts no commands, or undefined while it is ready. */ + #stopReason(session: RizinDebugSession): string | undefined { + const diagnosed = session.stopReason ?? session.protocolError; + if (diagnosed !== undefined) return diagnosed; + if (this.#closingAll) + return "REA is shutting down and closing every Rizin debug session."; + if (session.closeRequested) + return "close_rizin_debug_session started releasing this session, and its owned cleanup has not completed."; + if (session.exited) { + const { exitCode, signalCode } = session.launched.process; + return `The Rizin process exited (exit code ${String(exitCode)}, signal ${String(signalCode)}); a command such as q, or a crash, can end it.`; + } + if (session.closing || !session.ready) + return "REA stopped this Rizin debug session."; + return undefined; } async close( @@ -1101,7 +1137,13 @@ export class RizinDebugSessionManager { return err(new AnalysisInputError("close_rizin_debug_session")); const session = this.#sessions.get(sessionId); if (session === undefined) - return err(new AnalysisInputError("close_rizin_debug_session")); + return err( + unknownDebuggerSessionError( + RIZIN_DEBUG_SESSION_TOOLS, + "close_rizin_debug_session", + ), + ); + session.closeRequested = true; session.closing = true; const stopped = await this.#stopDebuggerOnly(session); if (stopped.status === "incomplete") @@ -1144,7 +1186,10 @@ export class RizinDebugSessionManager { stopped.reason, ), ); - this.#sessions.delete(session.id); + // The caller still holds a published session's ID; keep it addressable + // so commands and status report why it stopped until it is closed. + if (!session.published || session.closeRequested) + this.#sessions.delete(session.id); } return ok(null); } @@ -1519,6 +1564,7 @@ export class RizinDebugSessionManager { clearTimeout(timer); session.cleanupSynchronization = undefined; if (cause === undefined) { + session.stopReason ??= session.protocolError; session.protocolError = undefined; session.commandOutputDropped = false; resolve(); diff --git a/src/rust/RustAuditableReader.test.ts b/src/rust/RustAuditableReader.test.ts index bc60927689..effecc2a48 100644 --- a/src/rust/RustAuditableReader.test.ts +++ b/src/rust/RustAuditableReader.test.ts @@ -1,13 +1,16 @@ import { deflateSync } from "node:zlib"; import { expect, it } from "vitest"; import { rustAuditableMetadataSchema } from "../domain/rust/rustBinary.js"; + import { - RUST_SECTION_BYTES, readAuditableSection, RustAuditableFormatFailure, RustAuditableResourceFailure, } from "./RustAuditableReader.js"; +// Explicit 1 MiB input boundary, independent of the reader implementation. +const RUST_SECTION_BYTES = 1024 * 1024; + const section = (document: unknown) => readAuditableSection( ".dep-v0", diff --git a/src/rust/RustAuditableReader.ts b/src/rust/RustAuditableReader.ts index 6b57e6c830..a0ae2a99b4 100644 --- a/src/rust/RustAuditableReader.ts +++ b/src/rust/RustAuditableReader.ts @@ -11,7 +11,7 @@ import { import { jsonValueByteLength } from "../domain/jsonValueByteLength.js"; /** Compressed zlib stream limit. */ -export const RUST_SECTION_BYTES = 1024 * 1024; +const RUST_SECTION_BYTES = 1024 * 1024; /** Inflated JSON may grow by at most this multiple of the compressed stream. */ export const RUST_INFLATE_RATIO = 64; /** Absolute inflated JSON limit, applied together with the ratio. */ diff --git a/src/rust/RustBinaryProvider.ts b/src/rust/RustBinaryProvider.ts index 37817adad5..08a33cb150 100644 --- a/src/rust/RustBinaryProvider.ts +++ b/src/rust/RustBinaryProvider.ts @@ -1,10 +1,10 @@ import { RUST_BINARY_PROVIDER_IDENTITY } from "./RustBinaryProviderIdentity.js"; import { + projectSelectedArtifactFileFailure, projectStableArtifactFailure, stableArtifactInputRecovery, projectArtifactCloseFailure, } from "../artifacts/StableArtifactFailure.js"; -import { isSelectedRegularFileError } from "../filesystem/RegularFile.js"; import { type StableArtifactSnapshot, readStableArtifact, @@ -21,7 +21,6 @@ import type { ExecutionOptions } from "../application/AnalysisProvider.js"; import type { RustBinaryPort } from "../application/rust/RustBinaryPort.js"; import { AnalysisError } from "../domain/analysisErrorBase.js"; import { - AnalysisAccessDeniedError, AnalysisArtifactChangedError, AnalysisCancelledError, AnalysisInputError, @@ -199,7 +198,7 @@ const outputLimit = (path: string, maxOutputBytes: number) => ); /** Resource-guard guidance delivered with every Rust inspection report. */ -export const RUST_RESOURCE_GUARD_LIMITATION = +const RUST_RESOURCE_GUARD_LIMITATION = "Default input and complete-report admission are 256 MiB and 8 MiB; producer_limits.input_bytes/output_bytes select larger or smaller values for the next call without restart. Independent parser guards bound the compressed dependency stream to 1 MiB and inflated JSON to the smaller of 8 MiB and 64 times the consumed compressed-stream length. Report admission applies before package schema cloning, graph objects, or reverse-edge allocation."; const limitationsFor = ( @@ -395,20 +394,13 @@ const artifactFailure = ( cause instanceof ArtifactReaderFailure ) return artifactReaderFailure(cause, path, limits); - if (cause instanceof Error && "code" in cause) { - if (cause.code === "EACCES" || cause.code === "EPERM") - return new AnalysisAccessDeniedError(OPERATION, path, cause.code, { - cause, - }); - if (isSelectedRegularFileError(cause)) - return new AnalysisInputError(OPERATION, { cause }, [ - { - path: ["path"], - reason: "invalid_value", - message: `Selected artifact could not be read (${String(cause.code)}): ${path}.`, - }, - ]); - } + const fileFailure = projectSelectedArtifactFileFailure( + cause, + OPERATION, + path, + "artifact", + ); + if (fileFailure !== undefined) return fileFailure; return new ProviderAdapterError(RUST_BINARY_PROVIDER_IDENTITY.id, OPERATION, { cause, diagnostics: { diff --git a/src/server/EvidenceMcpServerCore.ts b/src/server/EvidenceMcpServerCore.ts index a6369dd467..cd28b97e89 100644 --- a/src/server/EvidenceMcpServerCore.ts +++ b/src/server/EvidenceMcpServerCore.ts @@ -44,10 +44,7 @@ import { AnalysisResourceConstraintError, type AnalysisInputIssue, } from "../domain/analysisErrorCore.js"; -import { - projectAnalysisError, - projectAnalysisErrorWithoutPartialObservation, -} from "../domain/analysisErrorProjection.js"; +import { projectAnalysisErrorWithoutPartialObservation } from "../domain/analysisErrorProjection.js"; import { projectInputIssueGroupsWithinBudget } from "../domain/inputIssueProjection.js"; import type { z } from "zod"; import { createEvidence } from "../domain/evidence.js"; diff --git a/src/server/ToolResultDelivery.ts b/src/server/ToolResultDelivery.ts index 25e2817312..68acb2b171 100644 --- a/src/server/ToolResultDelivery.ts +++ b/src/server/ToolResultDelivery.ts @@ -14,7 +14,7 @@ import { } from "../domain/analysisErrorProjection.js"; import type { AnalysisError } from "../domain/analysisErrorBase.js"; import { NoBinaryOpenError } from "../domain/configurationErrors.js"; -import { jsonValueSchema, type JsonValue } from "../domain/jsonValue.js"; +import type { JsonValue } from "../domain/jsonValue.js"; import type { Result } from "../domain/result.js"; import { estimateJsonStorage } from "../application/JsonAuthenticationHeadroom.js"; import { AnalysisResourceConstraintError } from "../domain/analysisErrorCore.js"; diff --git a/src/server/deliverInvestigationWorkflow.ts b/src/server/deliverInvestigationWorkflow.ts new file mode 100644 index 0000000000..60b6cb57ee --- /dev/null +++ b/src/server/deliverInvestigationWorkflow.ts @@ -0,0 +1,42 @@ +import type { BinarySessionPort } from "../application/binary/BinarySessionPort.js"; +import { + recordEvidenceSources, + retainAnalysisResultSources, + type AnalysisResultWithSources, +} from "../application/investigation/recordEvidenceSources.js"; +import type { InvestigationWorkflowExecution } from "../application/investigation/InvestigationWorkflows.js"; +import type { ToolContract } from "../contracts/toolContractTypes.js"; +import type { EvidenceMcpServer } from "./EvidenceMcpServer.js"; +import { recordDerivedEvidence } from "./recordDerivedEvidence.js"; + +/** Retain workflow sources before delivering the derived Evidence or its failure. */ +export const deliverInvestigationWorkflow = ( + server: EvidenceMcpServer, + session: BinarySessionPort, + contract: ToolContract, + computed: AnalysisResultWithSources, +) => { + const record = session.recordEvidence.bind(session); + const completed = retainAnalysisResultSources( + contract.name, + record, + computed, + ); + if (!completed.ok) + return server.delivery.toPartialErrorToolResult( + completed.error, + contract.returnsTargetContent, + record, + ); + const { evidence, sources, unknown } = completed.value; + const retained = recordEvidenceSources(record, sources, { + evidence, + evidenceForAnalysis: session.evidenceForAnalysis.bind(session), + }); + if (!retained.ok) return server.delivery.toCallToolResult(retained, contract); + return server.delivery.toEvidenceToolResult( + evidence, + contract, + recordDerivedEvidence(session, evidence, unknown), + ); +}; diff --git a/src/server/environment/environmentCapacity.test.ts b/src/server/environment/environmentCapacity.test.ts index d7dfe71871..f37cc6beb1 100644 --- a/src/server/environment/environmentCapacity.test.ts +++ b/src/server/environment/environmentCapacity.test.ts @@ -3,10 +3,22 @@ import { environmentServeSource } from "../../../tests/fixtures/environmentServe import { environmentCard, environmentToolDefinitions, - ENVIRONMENT_CATALOG_LIMIT_BYTES, } from "../../application/environment/environmentTools.js"; import { serveEnvironment } from "./serveEnvironment.js"; +const reportedCatalogLimit = (): number => { + const source = environmentServeSource(); + const result = environmentToolDefinitions( + source, + "x".repeat(2 * 1024 * 1024), + ); + if (result.ok) throw new Error("Expected catalog capacity refusal"); + const limit = result.error.reportedLimits?.max_catalog_bytes; + if (typeof limit !== "number" || limit <= 0) + throw new Error("Expected a positive catalog limit"); + return limit; +}; + it("refuses a complete oversized discovery before trying to bind", async () => { const source = environmentServeSource(); const tool = source.manifest.tools[0]!; @@ -33,7 +45,7 @@ it("refuses a complete oversized discovery before trying to bind", async () => { _tag: "AnalysisResourceConstraintError", resource: "capacity", retryable: false, - reportedLimits: { max_catalog_bytes: ENVIRONMENT_CATALOG_LIMIT_BYTES }, + reportedLimits: { max_catalog_bytes: expect.any(Number) }, }, }); expect(manifest.tools[0]?.query_parameter_names).toHaveLength(10000); @@ -41,6 +53,7 @@ it("refuses a complete oversized discovery before trying to bind", async () => { it("charges environment identity, aggregate tools and escaped text before producing a card", () => { const source = environmentServeSource(); + const limit = reportedCatalogLimit(); const tools = Array.from({ length: 1000 }, (_, index) => ({ ...source.manifest.tools[0]!, name: `tool_${index}`, @@ -56,12 +69,7 @@ it("charges environment identity, aggregate tools and escaped text before produc source.manifest.name, ).ok, ).toBe(false); - expect( - environmentToolDefinitions( - source, - "x".repeat(ENVIRONMENT_CATALOG_LIMIT_BYTES), - ).ok, - ).toBe(false); + expect(environmentToolDefinitions(source, "x".repeat(limit)).ok).toBe(false); const normal = environmentToolDefinitions(source, source.manifest.name); if (!normal.ok) throw normal.error; expect( @@ -70,20 +78,19 @@ it("charges environment identity, aggregate tools and escaped text before produc normal.value.map(({ tool: _, ...definition }) => definition), ), ), - ).toBeLessThan(ENVIRONMENT_CATALOG_LIMIT_BYTES); + ).toBeLessThan(limit); }); it("reserves the state card only when the state data plane is served", () => { const source = environmentServeSource(); + const limit = reportedCatalogLimit(); const empty = { ...source, manifest: { ...source.manifest, tools: [] } }; const admits = (length: number, dataPlane: "none" | "state") => environmentToolDefinitions(empty, "x".repeat(length), { dataPlane }).ok; // With no tools, the name and the card are the whole reservation: six bytes per name unit plus quotes. const longest = (dataPlane: "none" | "state") => Math.floor( - (ENVIRONMENT_CATALOG_LIMIT_BYTES - - 2 - - JSON.stringify(environmentCard("", [], dataPlane)).length) / + (limit - 2 - JSON.stringify(environmentCard("", [], dataPlane)).length) / 6, ); const stateless = longest("none"); diff --git a/src/server/environment/environmentDataPlane.ts b/src/server/environment/environmentDataPlane.ts index 0129b8a6aa..07ea71eef3 100644 --- a/src/server/environment/environmentDataPlane.ts +++ b/src/server/environment/environmentDataPlane.ts @@ -1,3 +1,4 @@ +import { escapeJsonPointerToken } from "../../domain/jsonPointer.js"; import { ResponseBodyCleanup } from "../../providers/ResponseBodyCleanup.js"; import type { EnvironmentStateStore } from "../../application/environment/environmentStateStore.js"; import { ArtifactResourceScope } from "../../artifacts/ArtifactResourceScope.js"; @@ -26,14 +27,11 @@ export type RpcMethods = ( const isRecord = (value: unknown): value is Readonly> => typeof value === "object" && value !== null && !Array.isArray(value); -const pointerSegment = (segment: string | number): string => - String(segment).replaceAll("~", "~0").replaceAll("/", "~1"); - /** The first document problem as `-32602`, located by a JSON pointer into the document. */ const documentError = (error: AnalysisInputError): RpcError => { const issue = error.issues[0]; const pointer = (issue?.path ?? []) - .map((segment) => `/${pointerSegment(segment)}`) + .map((segment) => `/${escapeJsonPointerToken(String(segment))}`) .join(""); return invalidParams( `${pointer === "" ? "the document" : pointer} ${issue?.message ?? "is not a rea-state/v1 document"}`, diff --git a/src/server/environment/serveEnvironment.test.ts b/src/server/environment/serveEnvironment.test.ts index 9bd4f5e16c..5b2c68bd4b 100644 --- a/src/server/environment/serveEnvironment.test.ts +++ b/src/server/environment/serveEnvironment.test.ts @@ -18,7 +18,6 @@ import { ENVIRONMENT_CARD_PATH, ENVIRONMENT_RPC_PATH, environmentRoutes, - MAX_RPC_BATCH_MESSAGES, serveEnvironment as startEnvironment, type ServedEnvironment, } from "./serveEnvironment.js"; @@ -335,13 +334,11 @@ describe("environment data plane validation", () => { ).json(), ).resolves.toMatchObject({ id: null, error: { code: -32601 } }); // A batch is bounded before any reply is built for it. - const oversized = await post( - Array.from({ length: MAX_RPC_BATCH_MESSAGES + 1 }, () => 0), - ); + const oversized = await post(Array.from({ length: 101 }, () => 0)); expect(oversized.status).toBe(400); await expect(oversized.json()).resolves.toMatchObject({ id: null, - error: { code: -32600 }, + error: { code: -32600, message: "batch exceeds 100 messages" }, }); }); }); diff --git a/src/server/environment/serveEnvironment.ts b/src/server/environment/serveEnvironment.ts index 99b9a22329..f64a3ddc2b 100644 --- a/src/server/environment/serveEnvironment.ts +++ b/src/server/environment/serveEnvironment.ts @@ -85,7 +85,7 @@ const isRecord = (value: unknown): value is Readonly> => typeof value === "object" && value !== null && !Array.isArray(value); /** At most this many messages in one data-plane batch; a larger batch is refused whole. */ -export const MAX_RPC_BATCH_MESSAGES = 100; +const MAX_RPC_BATCH_MESSAGES = 100; /** A JSON-RPC 2.0 request or notification: an id, when present, is a string, number or null; params are structured. */ const isRpcMessage = ( diff --git a/src/server/environment/serveEnvironmentState.test.ts b/src/server/environment/serveEnvironmentState.test.ts index 6937fd182b..5a2fd433bd 100644 --- a/src/server/environment/serveEnvironmentState.test.ts +++ b/src/server/environment/serveEnvironmentState.test.ts @@ -7,6 +7,7 @@ import { mkdir, writeFile } from "node:fs/promises"; import { join } from "node:path"; import { pathToFileURL } from "node:url"; import { afterEach, describe, expect, it, onTestFinished, vi } from "vitest"; +import { awaitAcknowledgement } from "../../../tests/fixtures/lifecycleAcknowledgement.js"; const closeFailureControl = vi.hoisted(() => ({ failure: null as Error | null, @@ -43,11 +44,7 @@ import { createTestTempDirectory } from "../../../tests/fixtures/temporaryDirect import { EnvironmentStateStore } from "../../application/environment/environmentStateStore.js"; import { ArtifactResourceRetryFailure } from "../../artifacts/ArtifactResourceScope.js"; import { OwnedFileHandle } from "../../filesystem/OwnedFileHandle.js"; -import { - environmentCard, - STATE_INTAKE_EXTENSION, - STATE_OPERATIONS, -} from "../../application/environment/environmentTools.js"; +import { environmentCard } from "../../application/environment/environmentTools.js"; import { stateDataPlane } from "./environmentDataPlane.js"; import { ENVIRONMENT_CARD_PATH, @@ -991,8 +988,28 @@ describe("serveEnvironment with a state", () => { ).json(); expect(card).toEqual(JSON.parse(JSON.stringify(environment.card))); const { capabilities } = environment.card; - expect(capabilities.extensions).toEqual([STATE_INTAKE_EXTENSION]); - expect(capabilities.operations).toEqual(STATE_OPERATIONS); + expect(capabilities.extensions).toMatchObject([ + { + uri: "urn:agentenv:intake/v1", + params: { + add: [ + { part: "data", format: "rea-state/v1", load: "replace" }, + { + part: "file", + format: "rea-state/v1", + mimeTypes: ["application/json"], + load: "replace", + }, + ], + get: [{ part: "data", format: "rea-state/v1" }], + }, + }, + ]); + expect(capabilities.operations).toEqual([ + "data/reset", + "data/add", + "data/get", + ]); const names = capabilities.tools.map((tool) => tool.name); expect(names).toContain("get_users_by_id"); for (const write of [ @@ -1124,7 +1141,11 @@ describe("state response cleanup", () => { signal: AbortSignal.timeout(10_000), }).catch(() => undefined); try { - await held.fetching; + await awaitAcknowledgement( + held.fetching, + answered, + "state fetch admission", + ); await environment.close(); giveResponse( new Response( @@ -1136,7 +1157,7 @@ describe("state response cleanup", () => { }), ), ); - await entered; + await awaitAcknowledgement(entered, cleanup, "late response cleanup"); await expect(environment.close()).rejects.toMatchObject({ _tag: "AnalysisOutputError", cleanupIncomplete: true, @@ -1145,6 +1166,7 @@ describe("state response cleanup", () => { release(); await expect(environment.close()).resolves.toBeUndefined(); } finally { + giveResponse(new Response(null, { status: 500 })); release(); await answered; await environment.close(); diff --git a/src/server/environment/stateIntake.test.ts b/src/server/environment/stateIntake.test.ts index 60119728e7..73b2b3580a 100644 --- a/src/server/environment/stateIntake.test.ts +++ b/src/server/environment/stateIntake.test.ts @@ -81,9 +81,15 @@ it("downloads with redirects refused and the 30 s timeout by default", async () }); expect(DEFAULT_STATE_INTAKE.timeoutMs).toBe(30_000); } finally { - await responses.close(); - await files.close(); - fetched.mockRestore(); + try { + await responses.close(); + } finally { + try { + await files.close(); + } finally { + fetched.mockRestore(); + } + } } }); diff --git a/src/server/http/serveStreamableHttp.test.ts b/src/server/http/serveStreamableHttp.test.ts index a16ce5926a..f47df71ffe 100644 --- a/src/server/http/serveStreamableHttp.test.ts +++ b/src/server/http/serveStreamableHttp.test.ts @@ -785,7 +785,7 @@ it("answers an oversized sessionless POST promptly instead of stalling on the pr { type: "text", text: "still served" }, ]); await client.close(); -}, 20_000); +}); it("closes the connection after a reply sent before the request body was read, on a keep-alive client", async () => { const { served } = await start(); diff --git a/src/server/promptCompletion.ts b/src/server/promptCompletion.ts index bcac51502e..8127b73f04 100644 --- a/src/server/promptCompletion.ts +++ b/src/server/promptCompletion.ts @@ -175,7 +175,7 @@ const evidenceCandidates = ( if (session === undefined) return []; const candidates: string[] = []; for (const evidence of session.immutableEvidenceBundle().records) - candidates.push(...evidenceValues(kind, evidence)); + for (const value of evidenceValues(kind, evidence)) candidates.push(value); return candidates; }; diff --git a/src/server/recordDerivedEvidence.ts b/src/server/recordDerivedEvidence.ts index c0575ff648..6b3748d913 100644 --- a/src/server/recordDerivedEvidence.ts +++ b/src/server/recordDerivedEvidence.ts @@ -10,10 +10,9 @@ export const recordDerivedEvidence = ( evidence: Evidence, unknown: RecordUnknownInput | undefined, ): Result => { - if (unknown !== undefined) { - const recorded = session.recordEvidenceWithUnknown(evidence, unknown); - return recorded.ok ? ok(evidence) : recorded; - } - const recorded = session.recordEvidence(evidence); + const recorded = + unknown === undefined + ? session.recordEvidence(evidence) + : session.recordEvidenceWithUnknown(evidence, unknown); return recorded.ok ? ok(evidence) : recorded; }; diff --git a/src/server/registerEvidenceTools.ts b/src/server/registerEvidenceTools.ts index 493aa47b53..b77b1285ee 100644 --- a/src/server/registerEvidenceTools.ts +++ b/src/server/registerEvidenceTools.ts @@ -88,6 +88,8 @@ export const registerEvidenceTools = ( analysis.execute(contract.name, executionParameters, { signal: context.mcpReq.signal, progress, + isOperationAdvertised: (operation) => + server.advertisesTool(operation), }), ); await reportOperationEnd(progress, contract.name, execution.ok); diff --git a/src/server/registerFunctionComparisonTool.ts b/src/server/registerFunctionComparisonTool.ts index 3f7209c33c..1ca84ca55d 100644 --- a/src/server/registerFunctionComparisonTool.ts +++ b/src/server/registerFunctionComparisonTool.ts @@ -1,12 +1,8 @@ import type { EvidenceMcpServer } from "./EvidenceMcpServer.js"; import type { BinarySessionPort } from "../application/binary/BinarySessionPort.js"; import { compareFunctionEvidence } from "../application/investigation/FunctionComparisonEvidence.js"; -import { - recordEvidenceSources, - retainAnalysisResultSources, -} from "../application/investigation/recordEvidenceSources.js"; import { toolContract } from "../contracts/toolContracts.js"; -import { recordDerivedEvidence } from "./recordDerivedEvidence.js"; +import { deliverInvestigationWorkflow } from "./deliverInvestigationWorkflow.js"; import { runDerivedOperation } from "./runDerivedOperation.js"; import { toolRegistrationOptions } from "./toolRegistrationOptions.js"; import { withAdmittedAnalysis } from "./analysisAdmission.js"; @@ -43,32 +39,11 @@ export const registerFunctionComparisonTool = ( }, (value) => value, ); - const completed = retainAnalysisResultSources( - contract.name, - session.recordEvidence.bind(session), - computed, - ); - if (!completed.ok) - return server.delivery.toPartialErrorToolResult( - completed.error, - contract.returnsTargetContent, - session.recordEvidence.bind(session), - ); - const { evidence, sources, unknown } = completed.value; - const retained = recordEvidenceSources( - session.recordEvidence.bind(session), - sources, - { - evidence, - evidenceForAnalysis: session.evidenceForAnalysis.bind(session), - }, - ); - if (!retained.ok) - return server.delivery.toCallToolResult(retained, contract); - return server.delivery.toEvidenceToolResult( - evidence, + return deliverInvestigationWorkflow( + server, + session, contract, - recordDerivedEvidence(session, evidence, unknown), + computed, ); }, ), diff --git a/src/server/registerGdbTools.ts b/src/server/registerGdbTools.ts index 298f7fb2b2..c47a2898cf 100644 --- a/src/server/registerGdbTools.ts +++ b/src/server/registerGdbTools.ts @@ -2,6 +2,10 @@ import type { McpServer, ServerContext } from "@modelcontextprotocol/server"; import type { Logger } from "pino"; import type { GdbSessionManager } from "../gdb/GdbSessionManager.js"; +import { + GDB_SESSION_TOOLS, + unknownDebuggerSessionError, +} from "../domain/debuggerSessionErrors.js"; import type { EvidenceWriter } from "../application/investigation/InvestigationRecordPort.js"; import type { EvidenceLookup } from "../application/EvidenceInputResolver.js"; import { @@ -10,7 +14,6 @@ import { } from "../application/analysisView/AnalysisViewService.js"; import { toolContract } from "../contracts/toolContracts.js"; import { err, ok } from "../domain/result.js"; -import { AnalysisInputError } from "../domain/analysisErrorCore.js"; import { runOwnedCleanupWithExternalMutationAdmission, runWithExternalMutationAdmission, @@ -117,7 +120,12 @@ export const registerGdbTools = ( const result = manager.status(input.session_id); if (result === undefined) return delivery.toCallToolResult( - err(invalidSession("gdb_session_status")), + err( + unknownDebuggerSessionError( + GDB_SESSION_TOOLS, + "gdb_session_status", + ), + ), status, ); return delivery.toCallToolResult( @@ -146,12 +154,7 @@ export const registerGdbTools = ( manager.close(input.session_id), ), ); - return result.ok - ? delivery.toCallToolResult({ ok: true, value: result.value }, close) - : delivery.toCallToolResult(result, close); + return delivery.toCallToolResult(result, close); }, ); }; - -const invalidSession = (operation: string): AnalysisInputError => - new AnalysisInputError(operation); diff --git a/src/server/registerInvestigationTools.ts b/src/server/registerInvestigationTools.ts index bc4b31ad31..2497661240 100644 --- a/src/server/registerInvestigationTools.ts +++ b/src/server/registerInvestigationTools.ts @@ -7,17 +7,13 @@ import { correlateStaticAndRuntimeEvidence, type InvestigationWorkflowExecution, } from "../application/investigation/InvestigationWorkflows.js"; -import { - recordEvidenceSources, - retainAnalysisResultSources, -} from "../application/investigation/recordEvidenceSources.js"; import type { AnalysisError } from "../domain/analysisErrorBase.js"; import type { Result } from "../domain/result.js"; import { toolContract } from "../contracts/toolContracts.js"; import { createEvidence } from "../domain/evidence.js"; import { createAnalysisProfile } from "../domain/analysisProfile.js"; import { recordWorkflowEvidence } from "./registerInvestigationTools/helpers.js"; -import { recordDerivedEvidence } from "./recordDerivedEvidence.js"; +import { deliverInvestigationWorkflow } from "./deliverInvestigationWorkflow.js"; import { runDerivedOperation } from "./runDerivedOperation.js"; import { RECONSTRUCTION_PROVIDER } from "../domain/investigationProviders.js"; import { @@ -97,32 +93,11 @@ const registerInvestigationWorkflow = ( }, (value) => value, ); - const completed = retainAnalysisResultSources( - contract.name, - session.recordEvidence.bind(session), - computed, - ); - if (!completed.ok) - return server.delivery.toPartialErrorToolResult( - completed.error, - contract.returnsTargetContent, - session.recordEvidence.bind(session), - ); - const { evidence, sources, unknown } = completed.value; - const retained = recordEvidenceSources( - session.recordEvidence.bind(session), - sources, - { - evidence, - evidenceForAnalysis: session.evidenceForAnalysis.bind(session), - }, - ); - if (!retained.ok) - return server.delivery.toCallToolResult(retained, contract); - return server.delivery.toEvidenceToolResult( - evidence, + return deliverInvestigationWorkflow( + server, + session, contract, - recordDerivedEvidence(session, evidence, unknown), + computed, ); }, ), diff --git a/src/server/registerProcessComparisonTool.ts b/src/server/registerProcessComparisonTool.ts index 2310cedb36..fdcc994759 100644 --- a/src/server/registerProcessComparisonTool.ts +++ b/src/server/registerProcessComparisonTool.ts @@ -1,12 +1,8 @@ import type { EvidenceMcpServer } from "./EvidenceMcpServer.js"; import type { BinarySessionPort } from "../application/binary/BinarySessionPort.js"; import { compareProcessEvidence } from "../application/process/ProcessComparisonService.js"; -import { - recordEvidenceSources, - retainAnalysisResultSources, -} from "../application/investigation/recordEvidenceSources.js"; import { toolContract } from "../contracts/toolContracts.js"; -import { recordDerivedEvidence } from "./recordDerivedEvidence.js"; +import { deliverInvestigationWorkflow } from "./deliverInvestigationWorkflow.js"; import { runDerivedOperation } from "./runDerivedOperation.js"; import { toolRegistrationOptions } from "./toolRegistrationOptions.js"; import { withAdmittedAnalysis } from "./analysisAdmission.js"; @@ -43,32 +39,11 @@ export const registerProcessComparisonTool = ( }, (value) => value, ); - const completed = retainAnalysisResultSources( - contract.name, - session.recordEvidence.bind(session), - computed, - ); - if (!completed.ok) - return server.delivery.toPartialErrorToolResult( - completed.error, - contract.returnsTargetContent, - session.recordEvidence.bind(session), - ); - const { evidence, sources, unknown } = completed.value; - const retained = recordEvidenceSources( - session.recordEvidence.bind(session), - sources, - { - evidence, - evidenceForAnalysis: session.evidenceForAnalysis.bind(session), - }, - ); - if (!retained.ok) - return server.delivery.toCallToolResult(retained, contract); - return server.delivery.toEvidenceToolResult( - evidence, + return deliverInvestigationWorkflow( + server, + session, contract, - recordDerivedEvidence(session, evidence, unknown), + computed, ); }, ), diff --git a/src/server/registerRizinDebugTools.ts b/src/server/registerRizinDebugTools.ts index 3040695970..04d4ae49cb 100644 --- a/src/server/registerRizinDebugTools.ts +++ b/src/server/registerRizinDebugTools.ts @@ -3,7 +3,10 @@ import type { Logger } from "pino"; import type { EvidenceWriter } from "../application/investigation/InvestigationRecordPort.js"; import { toolContract } from "../contracts/toolContracts.js"; -import { AnalysisInputError } from "../domain/analysisErrorCore.js"; +import { + RIZIN_DEBUG_SESSION_TOOLS, + unknownDebuggerSessionError, +} from "../domain/debuggerSessionErrors.js"; import type { RizinDebugSessionManager } from "../rizin/RizinDebugSessionManager.js"; import { runOwnedCleanupWithExternalMutationAdmission, @@ -57,9 +60,7 @@ export const registerRizinDebugTools = ( () => manager.retryCleanupFailures(), (value) => ({ session_id: value.session_id }), ); - return result.ok - ? delivery.toCallToolResult({ ok: true, value: result.value }, start) - : delivery.toCallToolResult(result, start); + return delivery.toCallToolResult(result, start); }, ); server.registerTool( @@ -101,7 +102,13 @@ export const registerRizinDebugTools = ( const result = manager.status(input.session_id); if (result === undefined) return delivery.toCallToolResult( - { ok: false, error: new AnalysisInputError(status.name) }, + { + ok: false, + error: unknownDebuggerSessionError( + RIZIN_DEBUG_SESSION_TOOLS, + status.name, + ), + }, status, ); return delivery.toCallToolResult( @@ -127,9 +134,7 @@ export const registerRizinDebugTools = ( manager.close(input.session_id), ), ); - return result.ok - ? delivery.toCallToolResult({ ok: true, value: result.value }, close) - : delivery.toCallToolResult(result, close); + return delivery.toCallToolResult(result, close); }, ); }; diff --git a/src/server/registerSessionStatusTool.ts b/src/server/registerSessionStatusTool.ts index 5839e1819a..c71626d556 100644 --- a/src/server/registerSessionStatusTool.ts +++ b/src/server/registerSessionStatusTool.ts @@ -127,27 +127,31 @@ export const registerSessionStatusTool = ( return server.delivery.toCallToolResult(err(cause), contract); } const statusObject = jsonObjectSchema.parse(status); - // Availability must be derived from the complete provider facts: an + // Availability is derived from the complete provider facts: an // advertised composed tool still depends on direct operations the - // registration hides. Narrow the public report only after this. - const toolAvailability = buildCapabilityInventory(status, { - ...availabilityPolicy(), - ...(availabilityResults.android === undefined - ? {} - : { androidAnalysisAvailability: availabilityResults.android }), - ...(availabilityResults.adb === undefined - ? {} - : { adbAnalysisAvailability: availabilityResults.adb }), - ...(availabilityResults.apktool === undefined - ? {} - : { apktoolAnalysisAvailability: availabilityResults.apktool }), - ...(availabilityResults.jeb === undefined - ? {} - : { jebAnalysisAvailability: availabilityResults.jeb }), - ...(availabilityResults.evmLimiter === undefined - ? {} - : { evmLimiterAvailability: availabilityResults.evmLimiter }), - }).filter(({ name }) => server.advertisesTool(name)); + // registration hides. Advertisement narrows only the public report. + const toolAvailability = buildCapabilityInventory( + status, + { + ...availabilityPolicy(), + ...(availabilityResults.android === undefined + ? {} + : { androidAnalysisAvailability: availabilityResults.android }), + ...(availabilityResults.adb === undefined + ? {} + : { adbAnalysisAvailability: availabilityResults.adb }), + ...(availabilityResults.apktool === undefined + ? {} + : { apktoolAnalysisAvailability: availabilityResults.apktool }), + ...(availabilityResults.jeb === undefined + ? {} + : { jebAnalysisAvailability: availabilityResults.jeb }), + ...(availabilityResults.evmLimiter === undefined + ? {} + : { evmLimiterAvailability: availabilityResults.evmLimiter }), + }, + (name) => server.advertisesTool(name), + ); const serverIdentity = createServerIdentity({ startedAt, commandPath: CURRENT_ENTRYPOINT_PATH, diff --git a/src/server/registerSessionTools.ts b/src/server/registerSessionTools.ts index ee49fbab43..b943376b8a 100644 --- a/src/server/registerSessionTools.ts +++ b/src/server/registerSessionTools.ts @@ -520,6 +520,6 @@ const missingAndroidAvailability = async (): Promise => ({ code: "not_configured", reason: "No Android JADX analysis provider is configured.", remediation: - "Set REA_JADX_MCP_JAR to a caller-supplied jadx-headless-mcp 0.7.1 JAR and provide a full JDK on Linux, macOS, or Windows x64 with its matching bundled native controls.", + "Set REA_JADX_MCP_JAR to the jadx-headless-mcp 0.7.1 release JAR (rea doctor --provider jadx names its download URL and SHA-256) and provide a full JDK on Linux, macOS, or Windows x64 with its matching bundled native controls.", diagnostics: { configured: false }, }); diff --git a/src/setupEnvironment.test.ts b/src/setupEnvironment.test.ts index 40310f8136..df43665537 100644 --- a/src/setupEnvironment.test.ts +++ b/src/setupEnvironment.test.ts @@ -141,8 +141,11 @@ it("includes the macOS Hopper installer diagnostic in setup remediation", async if (result.status !== "failed") throw new Error("expected setup failure"); expect(result.remediation).not.toContain("\u001b"); } finally { - await host.close?.(); - install.mockRestore(); + try { + await host.close?.(); + } finally { + install.mockRestore(); + } } }); diff --git a/src/signals/SigrokRecordingProvider.ts b/src/signals/SigrokRecordingProvider.ts index 8a4d04fa0e..933c6e3144 100644 --- a/src/signals/SigrokRecordingProvider.ts +++ b/src/signals/SigrokRecordingProvider.ts @@ -34,6 +34,7 @@ import { import type { SignalRecordingPort } from "../application/signals/SignalRecordingService.js"; import { ArtifactReaderFailure } from "../artifacts/ArtifactReader.js"; import { + ARTIFACT_PRODUCER_LIMITATION, ArtifactAnalysisResources, artifactCommandOutput, type ArtifactAnalysisRuntime, @@ -55,7 +56,7 @@ import { AnalysisOutputError, AnalysisResourceConstraintError, } from "../domain/analysisErrorCore.js"; -import { createEvidence } from "../domain/evidence.js"; +import { createAnalysisExecutionEvidence } from "../domain/analysisObservation.js"; import { fixedAllowanceOptions } from "../domain/fixedAllowanceRemediation.js"; import { ProviderAdapterError } from "../domain/providerAdapterError.js"; import { windowAnnotationText } from "../domain/signals/annotationTextWindow.js"; @@ -426,7 +427,7 @@ export class SigrokRecordingProvider implements SignalRecordingPort { "Annotations are the selected decoder's interpretation of the recorded samples and caller-selected settings, not independently verified protocol semantics. Empty output does not establish absence of traffic.", "Sample-number ranges and display text are preserved verbatim, not converted to timestamps or parsed as a lossless byte stream. All producer stderr is retained, including warnings on exit zero.", "Only a private copy of the selected file is decoded. Hardware discovery/acquisition and target execution are not requested. Caller-supplied tools, plugins and decoder modules are trusted; this is not an OS sandbox.", - "Linux prlimit enforces the reported per-process address-space, per-file and CPU limits before execution with no core dumps, preserving tighter inherited soft and hard limits. Input, command deadline and retained output budgets are reported in limits; producer_limits can change their requested defaults on the next call without restart. Output overflow fails. Parser and transport bounds remain independent. These are per-process/per-file limits, not aggregate memory or filesystem quotas; caller-supplied tools and the limiter are trusted.", + ARTIFACT_PRODUCER_LIMITATION, ], }), ); @@ -454,14 +455,12 @@ export class SigrokRecordingProvider implements SignalRecordingPort { return this.#resources.finish( result, (execution) => - createEvidence(execution.subject ?? undefined, execution.provider, { + createAnalysisExecutionEvidence( operation, - parameters: input, - result: execution.result, - confidence: "observed", - locations: execution.locations, - limitations: execution.limitations, - }), + input, + execution, + "observed", + ), operation, options?.signal, ); diff --git a/src/sqlite/SqliteDatabaseProvider.ts b/src/sqlite/SqliteDatabaseProvider.ts index 5a66ca4942..feeb42835f 100644 --- a/src/sqlite/SqliteDatabaseProvider.ts +++ b/src/sqlite/SqliteDatabaseProvider.ts @@ -18,7 +18,7 @@ import { } from "../domain/analysisErrorCore.js"; import { projectAnalysisError } from "../domain/analysisErrorProjection.js"; import type { JsonValue } from "../domain/jsonValue.js"; -import { createEvidence } from "../domain/evidence.js"; +import { createAnalysisExecutionEvidence } from "../domain/analysisObservation.js"; import { ProviderCleanupError } from "../domain/providerCleanupError.js"; import { err, ok, type Result } from "../domain/result.js"; import { @@ -537,14 +537,12 @@ const completedSqliteEvidence = ( input: InspectSqliteDatabaseInput, execution: AnalysisExecution, ) => - createEvidence(execution.subject ?? undefined, execution.provider, { + createAnalysisExecutionEvidence( operation, - parameters: sqliteInspectionParameters(input), - result: execution.result, - confidence: "observed", - locations: execution.locations, - limitations: execution.limitations, - }); + sqliteInspectionParameters(input), + execution, + "observed", + ); const validateSqliteInput = (input: InspectSqliteDatabaseInput): void => { const parsed = inspectSqliteDatabaseInputSchema.safeParse(input); diff --git a/src/wasm/WabtArtifactProvider.ts b/src/wasm/WabtArtifactProvider.ts index 567392d9f4..9a6a1c3b68 100644 --- a/src/wasm/WabtArtifactProvider.ts +++ b/src/wasm/WabtArtifactProvider.ts @@ -392,8 +392,7 @@ export class WabtArtifactProvider implements WasmArtifactPort { version.exitCode === 0 && version.stderr.text === "" ? admitWabtVersion(version.stdout.text) : undefined; - if (admission === undefined || admission.status === "unsupported") { - const reason = `${admission?.message ?? `Unsupported ${command.tool} version banner`}; expected stdout from ${wabtReleaseLine()} with empty stderr.`; + function rejectVersion(reason: string): never { throw new AnalysisCapabilityUnavailableError( WABT_PROVIDER_IDENTITY.id, OPERATION, @@ -410,25 +409,15 @@ export class WabtArtifactProvider implements WasmArtifactPort { }, ); } + if (admission === undefined || admission.status === "unsupported") { + const reason = `${admission?.message ?? `Unsupported ${command.tool} version banner`}; expected stdout from ${wabtReleaseLine()} with empty stderr.`; + rejectVersion(reason); + } // All three tools come from one directory, so admit only banners that agree. if (observedRelease === undefined) observedRelease = admission.version; if (observedRelease !== admission.version) { const reason = `Configured WABT tools report different releases (${observedRelease} and ${admission.version}); select one complete ${wabtReleaseLine()} installation.`; - throw new AnalysisCapabilityUnavailableError( - WABT_PROVIDER_IDENTITY.id, - OPERATION, - reason, - { - userMessage: reason, - diagnostics: versionProbe, - capturedOutput: { - stdout: version.stdout.text, - stderr: version.stderr.text, - truncated: version.diagnosticTruncated === true, - }, - remediationAction: wabtConfigurationRecoveryAction(), - }, - ); + rejectVersion(reason); } if (admission.status === "compatible") compatibleLimitation = admission.limitation; diff --git a/src/windows/WindowsPrivateRuntime.test.ts b/src/windows/WindowsPrivateRuntime.test.ts index db2a71d289..65746e2144 100644 --- a/src/windows/WindowsPrivateRuntime.test.ts +++ b/src/windows/WindowsPrivateRuntime.test.ts @@ -125,8 +125,10 @@ describe("Windows private runtime cleanup retry", () => { const first = runtime.close(); const second = runtime.close(); - expect(second).toBe(first); - await Promise.all([first, second]); + await expect(Promise.all([first, second])).resolves.toEqual([ + undefined, + undefined, + ]); expect(closes).toBe(2); }); diff --git a/tests/acceptance/analysisSettingsMcp.test.ts b/tests/acceptance/analysisSettingsMcp.test.ts index dbae94f312..5e2bb51de0 100644 --- a/tests/acceptance/analysisSettingsMcp.test.ts +++ b/tests/acceptance/analysisSettingsMcp.test.ts @@ -80,7 +80,7 @@ it("recovers expanded ASAR listing admission on the same MCP connection independ } finally { await client.close(); } -}, 30000); +}); it("advertises exact contracts and recovers a full large-index archive over one MCP connection", async () => { const root = await createTestTempDirectory("rea-runtime-settings-"); @@ -214,7 +214,7 @@ it("advertises exact contracts and recovers a full large-index archive over one } finally { await client.close(); } -}, 30000); +}); it.each(["0", "2147483648"])( "keeps invalid JADX launch timeout %s local and repairable through MCP", @@ -279,7 +279,6 @@ it.each(["0", "2147483648"])( await client.close(); } }, - 30000, ); it("lowers, restores and seeds the native matching allowance over MCP", async () => { @@ -427,7 +426,7 @@ it("lowers, restores and seeds the native matching allowance over MCP", async () } finally { await invalid.client.close(); } -}, 30000); +}); it("starts with invalid runtime seeds and recovers ASAR and capture work on the same connection", async () => { const root = await createTestTempDirectory("rea-invalid-runtime-seeds-"); @@ -517,7 +516,7 @@ it("starts with invalid runtime seeds and recovers ASAR and capture work on the } finally { await client.close(); } -}, 30000); +}); it.skipIf(process.platform === "win32")( "ignores Windows ChangeTime launch settings on other hosts", @@ -539,5 +538,4 @@ it.skipIf(process.platform === "win32")( await client.close(); } }, - 30000, ); diff --git a/tests/acceptance/apkZip64FramingCli.test.ts b/tests/acceptance/apkZip64FramingCli.test.ts index d098d8c806..9c8faecb4c 100644 --- a/tests/acceptance/apkZip64FramingCli.test.ts +++ b/tests/acceptance/apkZip64FramingCli.test.ts @@ -12,7 +12,7 @@ import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js"; import { cliTest, type TestCli } from "../support/cli/cliFixture.js"; const inspectBoth = async (cli: TestCli, path: string) => { - const output = await cli.sharedCliRun!({ + const output = await cli.run({ arguments: [ "inspect-apk-signature", path, diff --git a/tests/acceptance/appleIpsCli.test.ts b/tests/acceptance/appleIpsCli.test.ts index 1fc118e568..648ba7041f 100644 --- a/tests/acceptance/appleIpsCli.test.ts +++ b/tests/acceptance/appleIpsCli.test.ts @@ -29,7 +29,6 @@ cliTest( "--json", ], environment: { NODE_OPTIONS: "--max-old-space-size=256" }, - timeoutMs: 30000, }); expect(result.exitCode, result.stderr).toBe(0); const evidence = toolContract("inspect_apple_ips").outputSchema.parse( @@ -44,7 +43,6 @@ cliTest( ).toBe(createHash("sha256").update(name).digest("hex")); expect(evidence.normalized_result.coverage.status).toBe("complete"); }, - 45000, ); cliTest( diff --git a/tests/acceptance/applications/adbListingNumbers.test.ts b/tests/acceptance/applications/adbListingNumbers.test.ts index 0fcaeab621..971dd7a34f 100644 --- a/tests/acceptance/applications/adbListingNumbers.test.ts +++ b/tests/acceptance/applications/adbListingNumbers.test.ts @@ -6,7 +6,7 @@ import { expect } from "vitest"; import { toolContract } from "../../../src/contracts/toolContracts.js"; import { connectLocalToolsMcp } from "../../fixtures/localToolsMcp.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; -import { cliTest, runFileCli } from "../../support/cli/cliFixture.js"; +import { cliTest } from "../../support/cli/cliFixture.js"; const SERIAL = "listing-numbers-device"; const UNSAFE = "9007199254740993"; @@ -224,9 +224,10 @@ process.stdout.write(require('node:fs').readFileSync(path)); arguments: [operation.replaceAll("_", "-"), SERIAL, "--json"], environment, }; - const output = await (variant === "safe" - ? cli.run(invocation) - : runFileCli(cli, invocation)); + const output = await cli.run({ + ...invocation, + freshProcess: variant === "safe", + }); expect(output.exitCode, output.stderr).toBe(0); const evidence = contract.outputSchema.parse(output.json); expect(evidence.parameters).toMatchObject({ serial: SERIAL }); diff --git a/tests/acceptance/applications/adbNumericDeclarations.test.ts b/tests/acceptance/applications/adbNumericDeclarations.test.ts index 4c1af433ec..10c2e376ac 100644 --- a/tests/acceptance/applications/adbNumericDeclarations.test.ts +++ b/tests/acceptance/applications/adbNumericDeclarations.test.ts @@ -9,7 +9,7 @@ import { adbDisplayPartialObservationSchema } from "../../../src/domain/adb/adbD import { connectLocalToolsMcp } from "../../fixtures/localToolsMcp.js"; import { parseMcpToolError } from "../../fixtures/mcpToolError.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; -import { cliTest, runFileCli } from "../../support/cli/cliFixture.js"; +import { cliTest } from "../../support/cli/cliFixture.js"; const SERIAL = "numeric-declarations-device"; const PACKAGE = "com.example.numeric"; @@ -224,9 +224,10 @@ cliTest.skipIf(process.platform === "win32")( arguments: ["inspect-adb-display", SERIAL, "--json"], environment: fixture.environment, }; - const output = await (scenario === displayScenarios[0] - ? cli.run(invocation) - : runFileCli(cli, invocation)); + const output = await cli.run({ + ...invocation, + freshProcess: scenario === displayScenarios[0], + }); expect(output.exitCode, `${scenario.name}: ${output.stderr}`).toBe(0); const evidence = contract.outputSchema.parse(output.json); expect(evidence.parameters).toEqual({ serial: SERIAL }); @@ -281,9 +282,10 @@ cliTest.skipIf(process.platform === "win32")( arguments: ["inspect-adb-display", SERIAL, "--json"], environment: fixture.environment, }; - const output = await (value === null - ? cli.run(invocation) - : runFileCli(cli, invocation)); + const output = await cli.run({ + ...invocation, + freshProcess: value === null, + }); expect(output.exitCode, output.stderr).toBe(1); const projected = analysisErrorProjectionSchema.parse(output.json); expect(projected.code).toBe("unreadable_output"); @@ -454,9 +456,10 @@ cliTest.skipIf(process.platform === "win32")( arguments: ["inspect-adb-package", SERIAL, PACKAGE, "--json"], environment: fixture.environment, }; - const output = await (scenario === packageScenarios[0] - ? cli.run(invocation) - : runFileCli(cli, invocation)); + const output = await cli.run({ + ...invocation, + freshProcess: scenario === packageScenarios[0], + }); expect(output.exitCode, `${scenario.name}: ${output.stderr}`).toBe(0); const evidence = contract.outputSchema.parse(output.json); expect(evidence.parameters).toEqual({ serial: SERIAL, package: PACKAGE }); diff --git a/tests/acceptance/applications/adbPackageInventoryCoverage.test.ts b/tests/acceptance/applications/adbPackageInventoryCoverage.test.ts index 78179e780f..8ce5535b63 100644 --- a/tests/acceptance/applications/adbPackageInventoryCoverage.test.ts +++ b/tests/acceptance/applications/adbPackageInventoryCoverage.test.ts @@ -8,7 +8,7 @@ import { toolContract } from "../../../src/contracts/toolContracts.js"; import { parseEvidence } from "../../../src/domain/evidence.js"; import { connectLocalToolsMcp } from "../../fixtures/localToolsMcp.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; -import { cliTest, runFileCli } from "../../support/cli/cliFixture.js"; +import { cliTest } from "../../support/cli/cliFixture.js"; const PACKAGE_LINE = "package:/data/app/example/base.apk=com.example.app"; @@ -139,9 +139,10 @@ process.exit(91); arguments: [command, "emulator-5554", ...tail, "--json"], environment, }; - const output = await (count === 0 - ? cli.run(invocation) - : runFileCli(cli, invocation)); + const output = await cli.run({ + ...invocation, + freshProcess: count === 0, + }); expect(output.exitCode, output.stderr).toBe(0); const evidence = parseEvidence(output.json); expect( diff --git a/tests/acceptance/applications/apiContract.test.ts b/tests/acceptance/applications/apiContract.test.ts index 3f67f1c393..0302a05e2d 100644 --- a/tests/acceptance/applications/apiContract.test.ts +++ b/tests/acceptance/applications/apiContract.test.ts @@ -131,7 +131,7 @@ const checkCliPublication = async ( }, ) => { const { root, path, parent, summary, selected } = context; - const run = cli.sharedCliRun?.bind(cli) ?? cli.run.bind(cli); + const run = cli.run.bind(cli); const complete = await run({ arguments: ["inspect-api-contract", path, "--json"], }); @@ -395,7 +395,7 @@ cliTest( coverage: { total: 3 }, source_coverage: { status: "partial" }, }); - const run = cli.sharedCliRun?.bind(cli) ?? cli.run.bind(cli); + const run = cli.run.bind(cli); const cliResult = await run({ arguments: ["inspect-api-contract", path, "--json"], }); @@ -442,7 +442,7 @@ cliTest( details: { partial_observation: snapshot }, }); expect(JSON.stringify(response)).not.toContain("u:p@h"); - const run = cli.sharedCliRun?.bind(cli) ?? cli.run.bind(cli); + const run = cli.run.bind(cli); const result = await run({ arguments: ["inspect-api-contract", path, "--json"], }); diff --git a/tests/acceptance/applications/appBundleEncodingCli.test.ts b/tests/acceptance/applications/appBundleEncodingCli.test.ts index f0cafe53d7..28c6740e97 100644 --- a/tests/acceptance/applications/appBundleEncodingCli.test.ts +++ b/tests/acceptance/applications/appBundleEncodingCli.test.ts @@ -171,7 +171,6 @@ describe.skipIf(process.platform !== "darwin")( } } }, - 45_000, ); }, ); diff --git a/tests/acceptance/applications/applicationVersionMatchingCli.test.ts b/tests/acceptance/applications/applicationVersionMatchingCli.test.ts index 787eb4ff03..5197b8d00d 100644 --- a/tests/acceptance/applications/applicationVersionMatchingCli.test.ts +++ b/tests/acceptance/applications/applicationVersionMatchingCli.test.ts @@ -7,7 +7,7 @@ import { parseEvidence } from "../../../src/domain/evidence.js"; import { javascriptApplicationAnalysisResultSchema } from "../../../src/domain/javascript/javascriptApplicationAnalysis.js"; import { applicationVersionComparisonResultSchema } from "../../../src/domain/javascript/javascriptApplicationVersionComparisonSchemas.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; -import { cliTest, runFileCli } from "../../support/cli/cliFixture.js"; +import { cliTest } from "../../support/cli/cliFixture.js"; cliTest( "keeps unavailable source-selection scopes unknown in version comparisons", @@ -72,7 +72,7 @@ cliTest( ) => { const inputPath = join(root, `${name}.json`); await writeFile(inputPath, JSON.stringify({ left, right })); - const response = await runFileCli(cli, { + const response = await cli.run({ arguments: ["compare-application-versions", inputPath, "--json"], environment: { REA_LOG_LEVEL: "silent" }, }); @@ -492,7 +492,7 @@ cliTest( const compare = async (left: unknown, right: unknown) => { const inputPath = join(root, `comparison-${Math.random()}.json`); await writeFile(inputPath, JSON.stringify({ left, right })); - const response = await runFileCli(cli, { + const response = await cli.run({ arguments: ["compare-application-versions", inputPath, "--json"], environment: { REA_LOG_LEVEL: "silent" }, }); diff --git a/tests/acceptance/applications/applicationWorkflowCli.test.ts b/tests/acceptance/applications/applicationWorkflowCli.test.ts index 799db70b8b..6b79a53c24 100644 --- a/tests/acceptance/applications/applicationWorkflowCli.test.ts +++ b/tests/acceptance/applications/applicationWorkflowCli.test.ts @@ -534,7 +534,7 @@ it("preserves complete line-comment source-map URLs through CLI and MCP", async ); } expect((await analyzeThroughStdioMcp(root)).graph).toEqual(graph); -}, 20_000); +}); it("resolves source-map URL paths once without changing their declarations", async () => { const root = await createTestTempDirectory("rea-source-map-encoding-cli-"); @@ -654,7 +654,7 @@ it("resolves source-map URL paths once without changing their declarations", asy ), ).toBe(false); expect((await analyzeThroughStdioMcp(root)).graph).toEqual(graph); -}, 20_000); +}); describe("JavaScript application path CLI", () => { it("retains the admitted relative CLI selection and canonical artifact identity", async () => { @@ -683,7 +683,7 @@ describe("JavaScript application path CLI", () => { }, subject: { local_path: absolute.value.subject?.local_path }, }); - }, 20_000); + }); it("returns the self-import graph consistently through CLI and stdio MCP", async () => { const root = await createTestTempDirectory( @@ -706,7 +706,7 @@ describe("JavaScript application path CLI", () => { expect(mcpResult.semantic_graph).toEqual( result.normalized_result.semantic_graph, ); - }, 20_000); + }); it("preserves canonical Electron API aliases through CLI and stdio MCP", async () => { const root = await createTestTempDirectory("rea-electron-api-alias-"); @@ -758,7 +758,7 @@ describe("JavaScript application path CLI", () => { expect(mcpResult.summary).toEqual(cliResult.summary); expect(mcpResult.graph).toEqual(cliResult.graph); expect(mcpResult.semantic_graph).toEqual(cliResult.semantic_graph); - }, 20_000); + }); }); describe("application workflow CLI parity", () => { @@ -857,7 +857,7 @@ describe("application workflow CLI parity", () => { }, }, }); - }, 20_000); + }); it("traces the same authenticated semantic graph through the CLI", async () => { const root = await createTestTempDirectory("rea-semantic-cli-"); @@ -897,7 +897,7 @@ describe("application workflow CLI parity", () => { source_graph_id: result.semantic_graph.graph_id, }, }); - }, 20_000); + }); }); it("traces all endpoint carriers and their response consumers through CLI and MCP", async () => { @@ -1018,7 +1018,7 @@ it("traces all endpoint carriers and their response consumers through CLI and MC coverage: { status: "partial" }, }); expect(await readFile(sourcePath, "utf8")).toBe(source); -}, 20_000); +}); describe("rest parameter semantic trace CLI", () => { it("traces each ordinary rest argument to its parameter through public application Evidence", async () => { @@ -1068,7 +1068,7 @@ describe("rest parameter semantic trace CLI", () => { }, }); } - }, 20_000); + }); }); describe("empty property key application CLI", () => { @@ -1100,7 +1100,6 @@ describe("empty property key application CLI", () => { }, }); }, - 20_000, ); }); @@ -1251,7 +1250,7 @@ describe("Webpack factory CommonJS export keys", () => { expectBundleExports(evidence, "5", []); expectBundleExports(evidence, "6", ["__proto__"]); } - }, 20_000); + }); }); describe("application workflow CLI input", () => { @@ -1411,7 +1410,7 @@ describe("application workflow CLI copy boundaries", () => { ), }, }); - }, 20_000); + }); }); describe("application workflow CLI and MCP capture lifetimes", () => { @@ -1521,7 +1520,7 @@ describe("application workflow CLI and MCP capture lifetimes", () => { await runCli(["compare-javascript-export-shapes", inputPath, "--json"]), ).toMatchObject(expected); expect(await compareThroughStdioMcp(input)).toMatchObject(expected); - }, 20_000); + }); }); describe("application workflow CLI export Evidence", () => { @@ -1568,7 +1567,7 @@ describe("application workflow CLI export Evidence", () => { ), ).toMatchObject(expected); expect(await compareThroughStdioMcp(input)).toMatchObject(expected); - }, 20_000); + }); it("preserves uncertainty after a helper can mutate an awaited return object", async () => { const root = await createTestTempDirectory("rea-awaited-export-shape-cli-"); @@ -1604,7 +1603,7 @@ describe("application workflow CLI export Evidence", () => { ]), }, }); - }, 20_000); + }); it("compares exact export shapes from file-backed Evidence", async () => { const root = await createTestTempDirectory("rea-export-shape-cli-"); @@ -1663,7 +1662,7 @@ describe("application workflow CLI export Evidence", () => { ]), }, }); - }, 20_000); + }); }); describe("application workflow CLI export property presence", () => { @@ -1759,7 +1758,7 @@ describe("application workflow CLI export property presence", () => { ]), }, }); - }, 20_000); + }); }); describe("application workflow CLI validation", () => { @@ -1794,7 +1793,7 @@ describe("application workflow CLI validation", () => { ]), }, }); - }, 20_000); + }); }); const compareThroughStdioMcp = async ( diff --git a/tests/acceptance/applications/artifactComparisonCoverage.test.ts b/tests/acceptance/applications/artifactComparisonCoverage.test.ts index 83e5ecc6bf..7b1693dbd4 100644 --- a/tests/acceptance/applications/artifactComparisonCoverage.test.ts +++ b/tests/acceptance/applications/artifactComparisonCoverage.test.ts @@ -432,7 +432,6 @@ cliTest( await assertCoverageControls({ left, right, inventory, compare }); }, - 30_000, ); cliTest.skipIf(process.platform === "win32")( @@ -469,7 +468,6 @@ cliTest.skipIf(process.platform === "win32")( ], }); }, - 30_000, ); cliTest.skipIf(process.platform === "win32")( @@ -521,7 +519,6 @@ cliTest.skipIf(process.platform === "win32")( ]), }); }, - 30_000, ); cliTest.skipIf(process.platform === "win32")( @@ -579,7 +576,6 @@ cliTest.skipIf(process.platform === "win32")( ); } }, - 30_000, ); cliTest( @@ -620,7 +616,6 @@ cliTest( ]), }); }, - 30_000, ); cliTest( diff --git a/tests/acceptance/applications/assetCatalogResourceKeys.test.ts b/tests/acceptance/applications/assetCatalogResourceKeys.test.ts index 42f69ecf22..2006614220 100644 --- a/tests/acceptance/applications/assetCatalogResourceKeys.test.ts +++ b/tests/acceptance/applications/assetCatalogResourceKeys.test.ts @@ -170,5 +170,4 @@ cliTest.skipIf(process.platform !== "darwin")( }); } }, - 30_000, ); diff --git a/tests/acceptance/applications/binaryPlistDates.test.ts b/tests/acceptance/applications/binaryPlistDates.test.ts index 010d128f7e..30883607c5 100644 --- a/tests/acceptance/applications/binaryPlistDates.test.ts +++ b/tests/acceptance/applications/binaryPlistDates.test.ts @@ -225,7 +225,6 @@ cliTest( ibResult.documents[0]?.archive_sha256, ); }, - 60_000, ); cliTest( @@ -278,5 +277,4 @@ cliTest( expect(mcpResult).toEqual(cliResult); expect(Buffer.compare(await readFile(archivePath), bytes)).toBe(0); }, - 60_000, ); diff --git a/tests/acceptance/applications/electronBlankPathCli.test.ts b/tests/acceptance/applications/electronBlankPathCli.test.ts index a485add76c..fc4ceeb235 100644 --- a/tests/acceptance/applications/electronBlankPathCli.test.ts +++ b/tests/acceptance/applications/electronBlankPathCli.test.ts @@ -16,7 +16,6 @@ cliTest.for(["", " \t "])( "--json", ], environment: { REA_LOG_LEVEL: "silent" }, - timeoutMs: 15000, }); expect(response.exitCode).toBe(1); expect(response.json).toMatchObject({ diff --git a/tests/acceptance/applications/electronInvalidPaths.test.ts b/tests/acceptance/applications/electronInvalidPaths.test.ts index 31724ce058..65a155179c 100644 --- a/tests/acceptance/applications/electronInvalidPaths.test.ts +++ b/tests/acceptance/applications/electronInvalidPaths.test.ts @@ -176,6 +176,7 @@ cliTest.skipIf(isWindows)( cliTest.skipIf(isWindows)( "returns a typed failure when an executable exits before Electron starts and keeps the MCP server and its Evidence", + { tags: ["long-running"] }, async ({ cli }) => { const root = await createTestTempDirectory("rea-electron-early-exit-"); const application = await createElectronApplication(root); @@ -229,7 +230,6 @@ cliTest.skipIf(isWindows)( expect(session.isError, JSON.stringify(session)).not.toBe(true); expect(await retained()).toEqual(before); }, - 180_000, ); cliTest( diff --git a/tests/acceptance/applications/erlangBeamViews.test.ts b/tests/acceptance/applications/erlangBeamViews.test.ts index b8778311c3..02bb9ea33a 100644 --- a/tests/acceptance/applications/erlangBeamViews.test.ts +++ b/tests/acceptance/applications/erlangBeamViews.test.ts @@ -103,12 +103,11 @@ const checkSelectors = async ( cliTest( "queries same-name BEAM export arities through CLI/MCP after deleting the source", - { timeout: 60_000 }, async ({ cli }) => { const root = await createTestTempDirectory("rea-beam-views-"); const path = join(root, "app.beam"); await writeFile(path, erlangBeamFixture("views")); - const run = cli.sharedCliRun?.bind(cli) ?? cli.run.bind(cli); + const run = cli.run.bind(cli); const complete = await run({ arguments: ["inspect-erlang-beam", path, "--json"], }); @@ -313,7 +312,7 @@ cliTest( async ({ cli }) => { const root = await createTestTempDirectory("rea-beam-invalid-view-"); const output = join(root, "unused.json"); - const run = cli.sharedCliRun?.bind(cli) ?? cli.run.bind(cli); + const run = cli.run.bind(cli); const view = JSON.stringify({ kind: "beam", collection: "exports" }); for (const [flags, key] of [ [["--detail", "summary"], "evidence_output"], diff --git a/tests/acceptance/applications/firmwareComparison.test.ts b/tests/acceptance/applications/firmwareComparison.test.ts index 6eaab8ea01..e3dc9fb2fc 100644 --- a/tests/acceptance/applications/firmwareComparison.test.ts +++ b/tests/acceptance/applications/firmwareComparison.test.ts @@ -137,7 +137,7 @@ cliTest( }, }), ); - const run = cli.sharedCliRun?.bind(cli) ?? cli.run.bind(cli); + const run = cli.run.bind(cli); const result = await run({ arguments: ["compare-artifacts", requestPath, "--json"], }); diff --git a/tests/acceptance/applications/graphqlContract.test.ts b/tests/acceptance/applications/graphqlContract.test.ts index c3c26063d5..cc21b4c963 100644 --- a/tests/acceptance/applications/graphqlContract.test.ts +++ b/tests/acceptance/applications/graphqlContract.test.ts @@ -35,7 +35,7 @@ const checkCliPublication = async ( }, ) => { const { root, parent, selected } = context; - const run = cli.sharedCliRun?.bind(cli) ?? cli.run.bind(cli); + const run = cli.run.bind(cli); const args = [ "inspect-api-contract", "./SDL 雪.graphql", @@ -271,7 +271,7 @@ cliTest( }, }), ); - const run = cli.sharedCliRun?.bind(cli) ?? cli.run.bind(cli); + const run = cli.run.bind(cli); const offline = await run({ arguments: ["inspect-analysis-view", queryPath, "--json"], }); @@ -339,7 +339,7 @@ cliTest( source_coverage: { status: "partial", operations: "unavailable" }, }, }); - const run = cli.sharedCliRun?.bind(cli) ?? cli.run.bind(cli); + const run = cli.run.bind(cli); const cliResult = await run({ arguments: [ "inspect-api-contract", @@ -392,7 +392,7 @@ cliTest( items: [{ code: "schema-parse-refused", source: { role: "schema" } }], }); await writeFile(path, text); - const run = cli.sharedCliRun?.bind(cli) ?? cli.run.bind(cli); + const run = cli.run.bind(cli); const result = await run({ arguments: [ "inspect-api-contract", diff --git a/tests/acceptance/applications/interfaceBuilderClassNameIdentity.test.ts b/tests/acceptance/applications/interfaceBuilderClassNameIdentity.test.ts index 919c290e19..76f7271eba 100644 --- a/tests/acceptance/applications/interfaceBuilderClassNameIdentity.test.ts +++ b/tests/acceptance/applications/interfaceBuilderClassNameIdentity.test.ts @@ -142,5 +142,4 @@ cliTest( }); expect(withoutAuthoredField?.attributes).not.toHaveProperty("className"); }, - 60_000, ); diff --git a/tests/acceptance/applications/interfaceBuilderPlistNumbers.test.ts b/tests/acceptance/applications/interfaceBuilderPlistNumbers.test.ts index 942c1b43ff..03b5fd8acc 100644 --- a/tests/acceptance/applications/interfaceBuilderPlistNumbers.test.ts +++ b/tests/acceptance/applications/interfaceBuilderPlistNumbers.test.ts @@ -9,11 +9,7 @@ import { interfaceBuilderAnalysisSchema } from "../../../src/domain/apple/interf import { thinMach } from "../../../src/domain/binaryTarget.fixture.js"; import { parseEvidence } from "../../../src/domain/evidence.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; -import { - cliTest, - runFileCli, - type TestCli, -} from "../../support/cli/cliFixture.js"; +import { cliTest, type TestCli } from "../../support/cli/cliFixture.js"; const unknownMixedInteger = { $plist_type: "unknown", @@ -39,7 +35,7 @@ const inspectArchive = (cli: TestCli, app: string, fresh: boolean) => { arguments: ["decode-interface-builder", app, "--json"], environment: { REA_LOG_LEVEL: "silent", REA_ANALYSIS_PROVIDER: "auto" }, }; - return fresh ? cli.run(invocation) : runFileCli(cli, invocation); + return cli.run({ ...invocation, freshProcess: fresh }); }; cliTest.for(scenarios)( diff --git a/tests/acceptance/applications/javascriptAnalysisLimits.test.ts b/tests/acceptance/applications/javascriptAnalysisLimits.test.ts index 94399944aa..7cd732937c 100644 --- a/tests/acceptance/applications/javascriptAnalysisLimits.test.ts +++ b/tests/acceptance/applications/javascriptAnalysisLimits.test.ts @@ -147,7 +147,7 @@ it("preserves MCP analysis limits in retained Evidence and an exported bundle ro const imported = await call("import_evidence_bundle", { path }); expect(imported.isError, JSON.stringify(imported.content)).not.toBe(true); expect(await getBundle(call)).toEqual(retained); -}, 60_000); +}); cliTest( "retains caller limits and applicable file scopes after an actual analysis timeout", @@ -261,7 +261,6 @@ cliTest( expectFileAnalysisApplicability(evidence, interrupted); await client.ping(); }, - 60_000, ); cliTest( diff --git a/tests/acceptance/applications/javascriptBuiltinDefaultsCli.test.ts b/tests/acceptance/applications/javascriptBuiltinDefaultsCli.test.ts index 96a18ae8cd..ab8352944a 100644 --- a/tests/acceptance/applications/javascriptBuiltinDefaultsCli.test.ts +++ b/tests/acceptance/applications/javascriptBuiltinDefaultsCli.test.ts @@ -124,7 +124,7 @@ it("preserves earlier and sibling effects without inventing replaced cached-modu expect(response.isError, JSON.stringify(response)).not.toBe(true); expect(response.structuredContent).toEqual(evidence); await client.ping(); -}, 60000); +}); it("excludes monkey-patched CommonJS effects from CLI and MCP evidence", async () => { const root = await createTestTempDirectory("rea-builtin-mutated-"); @@ -175,7 +175,7 @@ it("excludes monkey-patched CommonJS effects from CLI and MCP evidence", async ( expect(response.isError, JSON.stringify(response)).not.toBe(true); expect(response.structuredContent).toEqual(evidence); await client.ping(); -}, 60000); +}); it("retains CommonJS builtin effects and handle links through CLI and MCP", async () => { const root = await createTestTempDirectory("rea-builtin-commonjs-"); @@ -252,7 +252,7 @@ it("retains CommonJS builtin effects and handle links through CLI and MCP", asyn expect(response.isError, JSON.stringify(response)).not.toBe(true); expect(response.structuredContent).toEqual(evidence); await client.ping(); -}, 60000); +}); it.each([ "import fs from 'node:fs';", @@ -294,7 +294,6 @@ it.each([ ]), ); }, - 20_000, ); it("keeps only direct logical defaults and actual argv elements in the public CLI graph", async () => { @@ -399,4 +398,4 @@ function shadowed(process) { return process.argv[3]; }`, }, ), ).toHaveLength(1); -}, 20_000); +}); diff --git a/tests/acceptance/applications/javascriptCallLiteralTransfer.test.ts b/tests/acceptance/applications/javascriptCallLiteralTransfer.test.ts index 90ceba64dd..3b553f0ae3 100644 --- a/tests/acceptance/applications/javascriptCallLiteralTransfer.test.ts +++ b/tests/acceptance/applications/javascriptCallLiteralTransfer.test.ts @@ -30,7 +30,6 @@ cliTest( bundlePath, "--json", ], - timeoutMs: 60_000, }); expect(response.exitCode).toBe(0); const bundle = evidenceBundleSchema.parse( @@ -57,5 +56,4 @@ cliTest( ]), ); }, - 90_000, ); diff --git a/tests/acceptance/applications/javascriptCancellationCli.test.ts b/tests/acceptance/applications/javascriptCancellationCli.test.ts index 0e7ce3c5ab..4b416ca9f7 100644 --- a/tests/acceptance/applications/javascriptCancellationCli.test.ts +++ b/tests/acceptance/applications/javascriptCancellationCli.test.ts @@ -9,16 +9,12 @@ processTest( "returns a nonzero exit for a streamed JSON analysis failure", async ({ processes }) => { const root = await createTestTempDirectory("rea-cli-js-missing-"); - const result = await processes.run( - process.execPath, - [ - "scripts/rea.mjs", - "analyze-javascript-application", - join(root, "missing"), - "--json", - ], - { timeoutMs: 20_000 }, - ); + const result = await processes.run(process.execPath, [ + "scripts/rea.mjs", + "analyze-javascript-application", + join(root, "missing"), + "--json", + ]); expect(result.exitCode).toBe(1); expect(JSON.parse(result.stdout)).toMatchObject({ code: "invalid_request", diff --git a/tests/acceptance/applications/javascriptCapturedParameters.test.ts b/tests/acceptance/applications/javascriptCapturedParameters.test.ts index 164048e35c..5aaf88ad19 100644 --- a/tests/acceptance/applications/javascriptCapturedParameters.test.ts +++ b/tests/acceptance/applications/javascriptCapturedParameters.test.ts @@ -67,7 +67,6 @@ cliTest( }, ); }, - 120_000, ); cliTest( @@ -133,7 +132,6 @@ console.log(JSON.stringify([captured(5),assigned(5),between(5),intermediate(5),n ); }); }, - 120_000, ); cliTest( @@ -180,7 +178,6 @@ console.log(JSON.stringify([delayed(),incoming(5),delayedPrivate(),staticField() ); }); }, - 120_000, ); cliTest( @@ -246,7 +243,6 @@ console.log(JSON.stringify([optionalCall(null),optionalCall(receiver),optionalCa "app.ts", ); }, - 120_000, ); cliTest( @@ -310,7 +306,6 @@ console.log(JSON.stringify([objectOrder(),bothSides(),arrayOrder(),computedOrder ); }); }, - 120_000, ); cliTest( @@ -361,7 +356,6 @@ console.log(JSON.stringify([read(),captured(object=>{object.value=3;})]));`, ); }); }, - 120_000, ); cliTest( @@ -431,7 +425,6 @@ console.log(JSON.stringify([objectPresent(),objectMissing(),arrayPresent(),array ); }); }, - 120_000, ); cliTest( @@ -508,5 +501,4 @@ cliTest( await harness.close(); } }, - 120_000, ); diff --git a/tests/acceptance/applications/javascriptCheckpointReserveCli.test.ts b/tests/acceptance/applications/javascriptCheckpointReserveCli.test.ts index 51935ae178..77d86d8d9f 100644 --- a/tests/acceptance/applications/javascriptCheckpointReserveCli.test.ts +++ b/tests/acceptance/applications/javascriptCheckpointReserveCli.test.ts @@ -60,7 +60,6 @@ cliTest.skipIf(process.versions.bun !== undefined).for([ { laterSources: 1, heapMb: 448, methodsCount: 600, retainGraph: false }, ])( "preserves evidence with $laterSources later checkpoints and a $heapMb MiB parent heap", - { timeout: 90_000 }, async ({ laterSources, heapMb, methodsCount, retainGraph }, { cli }) => { const root = await createTestTempDirectory("rea-js-checkpoint-reserve-"); const source = Array.from( @@ -85,7 +84,6 @@ cliTest.skipIf(process.versions.bun !== undefined).for([ environment: { NODE_OPTIONS: `--max-old-space-size=${String(heapMb)} --max-semi-space-size=8`, }, - timeoutMs: 60_000, }); expect(response.exitCode).toBe(0); const result = javascriptApplicationAnalysisResultSchema.parse( @@ -173,7 +171,6 @@ cliTest.skipIf(process.versions.bun !== undefined).for([ cliTest.skipIf(process.versions.bun !== undefined)( "keeps every source's static facts after semantic results use the parent result budget", - { timeout: 90_000 }, async ({ cli }) => { const root = await createTestTempDirectory("rea-js-static-capacity-"); // Plain functions produce semantic graphs large enough to use the result @@ -203,7 +200,6 @@ cliTest.skipIf(process.versions.bun !== undefined)( environment: { NODE_OPTIONS: "--max-old-space-size=640 --max-semi-space-size=8", }, - timeoutMs: 75_000, }); expect(response.stderr).not.toMatch(/FATAL ERROR:.*heap/iu); expect( diff --git a/tests/acceptance/applications/javascriptChildHandleOwnership.test.ts b/tests/acceptance/applications/javascriptChildHandleOwnership.test.ts index 19fc88ab95..5b55e7dc73 100644 --- a/tests/acceptance/applications/javascriptChildHandleOwnership.test.ts +++ b/tests/acceptance/applications/javascriptChildHandleOwnership.test.ts @@ -99,7 +99,7 @@ stable.kill(); family === "child-process" && reason === "ambiguous-target", ), ).toHaveLength(2); -}, 60000); +}); it("keeps deferred instance field child ownership uncertain through CLI and MCP", async () => { const evidence = await analyzeThroughCliAndMcp( @@ -140,4 +140,4 @@ stable.kill(); family === "child-process" && reason === "ambiguous-target", ), ).toHaveLength(1); -}, 60000); +}); diff --git a/tests/acceptance/applications/javascriptChildProcessArgumentsCli.test.ts b/tests/acceptance/applications/javascriptChildProcessArgumentsCli.test.ts index 84bfca0304..5714fbe564 100644 --- a/tests/acceptance/applications/javascriptChildProcessArgumentsCli.test.ts +++ b/tests/acceptance/applications/javascriptChildProcessArgumentsCli.test.ts @@ -32,11 +32,10 @@ cliTest.for(["CLI", "stdio MCP"] as const)( ["a", "b"], [], ]) { - const control = await processes.run( - process.execPath, - [join(root, "child.cjs"), ...values], - { timeoutMs: 5_000 }, - ); + const control = await processes.run(process.execPath, [ + join(root, "child.cjs"), + ...values, + ]); expect(control.exitCode, control.stderr).toBe(0); counts.push(Number(control.stdout)); } diff --git a/tests/acceptance/applications/javascriptConfigurationReadsCli.test.ts b/tests/acceptance/applications/javascriptConfigurationReadsCli.test.ts index db7af4df5c..021964670d 100644 --- a/tests/acceptance/applications/javascriptConfigurationReadsCli.test.ts +++ b/tests/acceptance/applications/javascriptConfigurationReadsCli.test.ts @@ -27,7 +27,7 @@ cliTest.for(["CLI", "stdio MCP"] as const)( const control = await processes.run( process.execPath, [join(root, "entry.mjs"), "abc", " mode "], - { env: { REA_ENDPOINT: "endpoint" }, timeoutMs: 15_000 }, + { env: { REA_ENDPOINT: "endpoint" } }, ); expect(control.exitCode, control.stderr).toBe(0); expect(JSON.parse(control.stdout)).toEqual([ diff --git a/tests/acceptance/applications/javascriptEndpointCandidateCli.test.ts b/tests/acceptance/applications/javascriptEndpointCandidateCli.test.ts index dd09dca96e..a853f7f35d 100644 --- a/tests/acceptance/applications/javascriptEndpointCandidateCli.test.ts +++ b/tests/acceptance/applications/javascriptEndpointCandidateCli.test.ts @@ -160,7 +160,7 @@ it("reports a candidate endpoint through the worker boundary in the public CLI", ({ target_node_id }) => target_node_id === endpoint?.node_id, ); expect(edge?.properties).toMatchObject({ interpretation: "candidate" }); -}, 20_000); +}); it("preserves SQLite constructor certainty through the public worker and graph", async () => { const root = await createTestTempDirectory("rea-storage-candidate-cli-"); @@ -218,4 +218,4 @@ it("preserves SQLite constructor certainty through the public worker and graph", ]), }, }); -}, 20_000); +}); diff --git a/tests/acceptance/applications/javascriptEndpointPayload.test.ts b/tests/acceptance/applications/javascriptEndpointPayload.test.ts index e43d146687..447ccc48a9 100644 --- a/tests/acceptance/applications/javascriptEndpointPayload.test.ts +++ b/tests/acceptance/applications/javascriptEndpointPayload.test.ts @@ -1,8 +1,7 @@ import { Client } from "@modelcontextprotocol/client"; -import { StdioClientTransport } from "@modelcontextprotocol/client/stdio"; import { createHash } from "node:crypto"; import { writeFile } from "node:fs/promises"; -import { join, resolve } from "node:path"; +import { join } from "node:path"; import { expect } from "vitest"; import { toolContract } from "../../../src/contracts/toolContracts.js"; @@ -10,6 +9,7 @@ import { javascriptApplicationAnalysisResultSchema } from "../../../src/domain/j import { applicationVersionComparisonResultSchema } from "../../../src/domain/javascript/javascriptApplicationVersionComparisonSchemas.js"; import { applicationFeatureTraceResultSchema } from "../../../src/domain/javascript/javascriptFeatureTraceSchemas.js"; import { javaScriptSemanticTraceResultSchema } from "../../../src/domain/javascript/javascriptSemanticTraceSchemas.js"; +import { connectLocalToolsMcp } from "../../fixtures/localToolsMcp.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; import { cliTest } from "../../support/cli/cliFixture.js"; @@ -46,118 +46,100 @@ cliTest( "directory", "--json", ], - timeoutMs: 60_000, }); expect(cliResponse.exitCode).toBe(0); const cliEvidence = toolContract( "analyze_javascript_application", ).outputSchema.parse(cliResponse.json); - const client = new Client({ name: "endpoint-payload-e2e", version: "1" }); - const transport = new StdioClientTransport({ - command: process.execPath, - args: [resolve("scripts/rea.mjs"), "mcp"], - cwd: process.cwd(), - env: { PATH: process.env.PATH ?? "" }, - stderr: "pipe", + const { client } = await connectLocalToolsMcp(); + const response = await client.callTool({ + name: "analyze_javascript_application", + arguments: { input_path: root, format: "directory" }, }); - transport.stderr?.on("data", () => undefined); - try { - await client.connect(transport); - const response = await client.callTool({ - name: "analyze_javascript_application", - arguments: { input_path: root, format: "directory" }, - }); - expect(response.isError).not.toBe(true); - const evidence = toolContract( - "analyze_javascript_application", - ).outputSchema.parse(response.structuredContent); - expect(evidence.evidence_id).toBe(cliEvidence.evidence_id); - const text = response.content.find((item) => item.type === "text"); - if (text?.type !== "text") throw new Error("Missing MCP text result"); - const textEvidence = toolContract( - "analyze_javascript_application", - ).outputSchema.parse(JSON.parse(text.text)); - expect(textEvidence.evidence_id).toBe(evidence.evidence_id); - const result = javascriptApplicationAnalysisResultSchema.parse( - evidence.normalized_result, - ); - expect(result.graph.coverage.limits).toContainEqual( - expect.objectContaining({ - name: "javascript_semantic_primitive_bytes", - }), - ); - expect( - result.semantic_graph.nodes.some( - ({ kind, properties }) => - kind === "literal" && properties.value === endpoint, + expect(response.isError).not.toBe(true); + const evidence = toolContract( + "analyze_javascript_application", + ).outputSchema.parse(response.structuredContent); + expect(evidence.evidence_id).toBe(cliEvidence.evidence_id); + const text = response.content.find((item) => item.type === "text"); + if (text?.type !== "text") throw new Error("Missing MCP text result"); + const textEvidence = toolContract( + "analyze_javascript_application", + ).outputSchema.parse(JSON.parse(text.text)); + expect(textEvidence.evidence_id).toBe(evidence.evidence_id); + const result = javascriptApplicationAnalysisResultSchema.parse( + evidence.normalized_result, + ); + expect(result.graph.coverage.limits).toContainEqual( + expect.objectContaining({ + name: "javascript_semantic_primitive_bytes", + }), + ); + expect( + result.semantic_graph.nodes.some( + ({ kind, properties }) => + kind === "literal" && properties.value === endpoint, + ), + ).toBe(false); + const endpoints = result.graph.nodes.filter( + (node) => node.kind === "endpoint", + ); + const nodeFor = (value: string) => { + const node = endpoints.find((candidate) => + candidate.observations.some( + ({ properties }) => properties.value === value, ), - ).toBe(false); - const endpoints = result.graph.nodes.filter( - (node) => node.kind === "endpoint", ); - const nodeFor = (value: string) => { - const node = endpoints.find((candidate) => - candidate.observations.some( - ({ properties }) => properties.value === value, - ), - ); - if (node === undefined) throw new Error("Complete endpoint was lost"); - return node; - }; - const endpointNode = nodeFor(endpoint); - expect( - new Set( - [endpoint, digestLookalike, surrogate, replacement].map( - (value) => nodeFor(value).node_id, - ), - ).size, - ).toBe(4); - const repeatedNode = nodeFor("/ok"); - expect( - endpoints.filter((node) => - node.observations.some( - ({ properties }) => properties.value === "/ok", - ), + if (node === undefined) throw new Error("Complete endpoint was lost"); + return node; + }; + const endpointNode = nodeFor(endpoint); + expect( + new Set( + [endpoint, digestLookalike, surrogate, replacement].map( + (value) => nodeFor(value).node_id, ), - ).toHaveLength(1); - expect(repeatedNode.observations).toHaveLength(2); - expect(repeatedNode.identity).toMatchObject({ - key: JSON.stringify(["literal", "/ok"]), - }); - expect( - repeatedNode.observations.every(({ label }) => label === "/ok"), - ).toBe(true); - expect(endpointNode.identity).toMatchObject({ - artifact_sha256: createHash("sha256").update(source).digest("hex"), - }); - expect(endpointNode.observations[0]?.evidence.location).toMatchObject({ - available: true, - value: { kind: "source-range", source: "main.js" }, - }); - const requestNodes = result.semantic_graph.nodes.filter( - (node) => - node.kind === "request" && node.properties.endpoint === endpoint, - ); - expect(requestNodes).toHaveLength(1); + ).size, + ).toBe(4); + const repeatedNode = nodeFor("/ok"); + expect( + endpoints.filter((node) => + node.observations.some(({ properties }) => properties.value === "/ok"), + ), + ).toHaveLength(1); + expect(repeatedNode.observations).toHaveLength(2); + expect(repeatedNode.identity).toMatchObject({ + key: JSON.stringify(["literal", "/ok"]), + }); + expect( + repeatedNode.observations.every(({ label }) => label === "/ok"), + ).toBe(true); + expect(endpointNode.identity).toMatchObject({ + artifact_sha256: createHash("sha256").update(source).digest("hex"), + }); + expect(endpointNode.observations[0]?.evidence.location).toMatchObject({ + available: true, + value: { kind: "source-range", source: "main.js" }, + }); + const requestNodes = result.semantic_graph.nodes.filter( + (node) => + node.kind === "request" && node.properties.endpoint === endpoint, + ); + expect(requestNodes).toHaveLength(1); - await verifyEndpointWorkflows({ - client, - evidenceId: evidence.evidence_id, - endpoint, - endpointNodeId: endpointNode.node_id, - requestNodeIds: requestNodes.map(({ node_id }) => node_id), - root, - source, - sourcePath, - }); - await client.ping(); - } finally { - await client.close(); - await transport.close(); - } + await verifyEndpointWorkflows({ + client, + evidenceId: evidence.evidence_id, + endpoint, + endpointNodeId: endpointNode.node_id, + requestNodeIds: requestNodes.map(({ node_id }) => node_id), + root, + source, + sourcePath, + }); + await client.ping(); }, - 120_000, ); interface EndpointWorkflowInput { diff --git a/tests/acceptance/applications/javascriptEventPayload.test.ts b/tests/acceptance/applications/javascriptEventPayload.test.ts index fbe3a9422a..2390d826b6 100644 --- a/tests/acceptance/applications/javascriptEventPayload.test.ts +++ b/tests/acceptance/applications/javascriptEventPayload.test.ts @@ -1,13 +1,12 @@ -import { Client } from "@modelcontextprotocol/client"; -import { StdioClientTransport } from "@modelcontextprotocol/client/stdio"; import { createHash } from "node:crypto"; import { writeFile } from "node:fs/promises"; -import { join, resolve } from "node:path"; +import { join } from "node:path"; import { expect } from "vitest"; import { toolContract } from "../../../src/contracts/toolContracts.js"; import { javascriptApplicationAnalysisResultSchema } from "../../../src/domain/javascript/javascriptApplicationAnalysis.js"; import { javaScriptSemanticTraceResultSchema } from "../../../src/domain/javascript/javascriptSemanticTraceSchemas.js"; +import { connectLocalToolsMcp } from "../../fixtures/localToolsMcp.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; import { cliTest } from "../../support/cli/cliFixture.js"; @@ -36,131 +35,112 @@ cliTest( "directory", "--json", ], - timeoutMs: 60_000, }); expect(cliResponse.exitCode).toBe(0); const cliEvidence = toolContract( "analyze_javascript_application", ).outputSchema.parse(cliResponse.json); - const client = new Client({ name: "event-payload-e2e", version: "1" }); - const transport = new StdioClientTransport({ - command: process.execPath, - args: [resolve("scripts/rea.mjs"), "mcp"], - cwd: process.cwd(), - env: { PATH: process.env.PATH ?? "" }, - stderr: "pipe", + const { client } = await connectLocalToolsMcp(); + const response = await client.callTool({ + name: "analyze_javascript_application", + arguments: { input_path: root, format: "directory" }, }); - transport.stderr?.on("data", () => undefined); - try { - await client.connect(transport); - const response = await client.callTool({ - name: "analyze_javascript_application", - arguments: { input_path: root, format: "directory" }, - }); - expect(response.isError).not.toBe(true); - const evidence = toolContract( - "analyze_javascript_application", - ).outputSchema.parse(response.structuredContent); - expect(evidence.evidence_id).toBe(cliEvidence.evidence_id); - const text = response.content.find((item) => item.type === "text"); - if (text?.type !== "text") throw new Error("Missing MCP text result"); - expect( - toolContract("analyze_javascript_application").outputSchema.parse( - JSON.parse(text.text), - ).evidence_id, - ).toBe(evidence.evidence_id); - const graph = javascriptApplicationAnalysisResultSchema.parse( - evidence.normalized_result, - ).semantic_graph; - const events = graph.nodes.filter((node) => node.kind === "event"); - const selected = events.filter( - (node) => node.properties.event_name === name, - ); - expect(selected).toHaveLength(1); - const event = selected[0]; - if (event === undefined) throw new Error("Complete event was lost"); - const listeners = graph.nodes.filter( + expect(response.isError).not.toBe(true); + const evidence = toolContract( + "analyze_javascript_application", + ).outputSchema.parse(response.structuredContent); + expect(evidence.evidence_id).toBe(cliEvidence.evidence_id); + const text = response.content.find((item) => item.type === "text"); + if (text?.type !== "text") throw new Error("Missing MCP text result"); + expect( + toolContract("analyze_javascript_application").outputSchema.parse( + JSON.parse(text.text), + ).evidence_id, + ).toBe(evidence.evidence_id); + const graph = javascriptApplicationAnalysisResultSchema.parse( + evidence.normalized_result, + ).semantic_graph; + const events = graph.nodes.filter((node) => node.kind === "event"); + const selected = events.filter( + (node) => node.properties.event_name === name, + ); + expect(selected).toHaveLength(1); + const event = selected[0]; + if (event === undefined) throw new Error("Complete event was lost"); + const listeners = graph.nodes.filter( + (node) => node.kind === "listener" && node.properties.event_name === name, + ); + expect(new Set(listeners.map((node) => node.properties.method))).toEqual( + new Set(["on", "off"]), + ); + const listenerIds = new Set(listeners.map((node) => node.node_id)); + expect( + graph.relations + .filter( + (relation) => + relation.source_node_id === event.node_id && + listenerIds.has(relation.target_node_id), + ) + .map((relation) => relation.relation), + ).toEqual( + expect.arrayContaining(["registers-listener", "removes-listener"]), + ); + const lookalikeEvent = events.find( + (node) => node.properties.event_name === lookalike, + ); + expect(lookalikeEvent).toBeDefined(); + expect(lookalikeEvent?.node_id).not.toBe(event.node_id); + const shortEvents = events.filter( + (node) => node.properties.event_name === "ok", + ); + expect(shortEvents).toHaveLength(2); + expect( + new Set(shortEvents.map((node) => node.properties.emitter_key)).size, + ).toBe(2); + expect( + shortEvents.every( (node) => - node.kind === "listener" && node.properties.event_name === name, - ); - expect(new Set(listeners.map((node) => node.properties.method))).toEqual( - new Set(["on", "off"]), - ); - const listenerIds = new Set(listeners.map((node) => node.node_id)); - expect( - graph.relations - .filter( - (relation) => - relation.source_node_id === event.node_id && - listenerIds.has(relation.target_node_id), - ) - .map((relation) => relation.relation), - ).toEqual( - expect.arrayContaining(["registers-listener", "removes-listener"]), - ); - const lookalikeEvent = events.find( - (node) => node.properties.event_name === lookalike, - ); - expect(lookalikeEvent).toBeDefined(); - expect(lookalikeEvent?.node_id).not.toBe(event.node_id); - const shortEvents = events.filter( - (node) => node.properties.event_name === "ok", - ); - expect(shortEvents).toHaveLength(2); - expect( - new Set(shortEvents.map((node) => node.properties.emitter_key)).size, - ).toBe(2); - expect( - shortEvents.every( - (node) => - node.label === "ok" && node.identity.role_key.endsWith("\0ok"), - ), - ).toBe(true); - const dynamic = events.find( - (node) => node.properties.event_name === null, - ); - expect(dynamic?.label).toBeNull(); - expect( - graph.unknowns.some( - (unknown) => - unknown.node_id === dynamic?.node_id && unknown.family === "event", - ), - ).toBe(true); - const traceResponse = await client.callTool({ - name: "trace_javascript_semantics", - arguments: { - application: { - kind: "retained-evidence", - evidence_id: evidence.evidence_id, - }, - query: { - seed: { kind: "event", name }, - direction: "forward-influence", - }, + node.label === "ok" && node.identity.role_key.endsWith("\0ok"), + ), + ).toBe(true); + const dynamic = events.find((node) => node.properties.event_name === null); + expect(dynamic?.label).toBeNull(); + expect( + graph.unknowns.some( + (unknown) => + unknown.node_id === dynamic?.node_id && unknown.family === "event", + ), + ).toBe(true); + const traceResponse = await client.callTool({ + name: "trace_javascript_semantics", + arguments: { + application: { + kind: "retained-evidence", + evidence_id: evidence.evidence_id, }, - }); - expect(traceResponse.isError).not.toBe(true); - const traceEvidence = toolContract( - "trace_javascript_semantics", - ).outputSchema.parse(traceResponse.structuredContent); - const trace = javaScriptSemanticTraceResultSchema.parse( - traceEvidence.normalized_result, - ); - expect(new Set(trace.seed_node_ids)).toEqual( - new Set([event.node_id, ...listenerIds]), - ); - expect( - trace.nodes - .filter((node) => listenerIds.has(node.node_id)) - .map((node) => node.properties.event_name), - ).toEqual(listeners.map(() => name)); - await client.ping(); - } finally { - await client.close(); - await transport.close(); - } + query: { + seed: { kind: "event", name }, + direction: "forward-influence", + }, + }, + }); + expect(traceResponse.isError).not.toBe(true); + const traceEvidence = toolContract( + "trace_javascript_semantics", + ).outputSchema.parse(traceResponse.structuredContent); + const trace = javaScriptSemanticTraceResultSchema.parse( + traceEvidence.normalized_result, + ); + expect(new Set(trace.seed_node_ids)).toEqual( + new Set([event.node_id, ...listenerIds]), + ); + expect( + trace.nodes + .filter((node) => listenerIds.has(node.node_id)) + .map((node) => node.properties.event_name), + ).toEqual(listeners.map(() => name)); + await client.ping(); }, - 120_000, ); cliTest( @@ -179,7 +159,6 @@ cliTest( "directory", "--json", ], - timeoutMs: 60_000, }); expect(analyzed.exitCode, analyzed.stderr).toBe(0); const application = toolContract( @@ -212,7 +191,6 @@ cliTest( ); const traced = await cli.run({ arguments: ["trace-javascript-semantics", request, "--json"], - timeoutMs: 60_000, }); expect(traced.exitCode, traced.stdout).toBe(0); const trace = javaScriptSemanticTraceResultSchema.parse( @@ -221,5 +199,4 @@ cliTest( ); expect(new Set(trace.seed_node_ids)).toEqual(retained); }, - 120_000, ); diff --git a/tests/acceptance/applications/javascriptExportBindingModuleView.test.ts b/tests/acceptance/applications/javascriptExportBindingModuleView.test.ts index e335d525c5..f496b35277 100644 --- a/tests/acceptance/applications/javascriptExportBindingModuleView.test.ts +++ b/tests/acceptance/applications/javascriptExportBindingModuleView.test.ts @@ -6,7 +6,7 @@ import { expect } from "vitest"; import { analysisViewResultSchema } from "../../../src/domain/analysisView/analysisView.js"; import { parseEvidence } from "../../../src/domain/evidence.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; -import { cliTest, runFileCli } from "../../support/cli/cliFixture.js"; +import { cliTest } from "../../support/cli/cliFixture.js"; cliTest( "pages only module units for a CommonJS file that exports symbols", @@ -23,7 +23,7 @@ cliTest( "exports.beta = function beta() { return 2; };", ].join("\n"), ); - const analyzed = await runFileCli(cli, { + const analyzed = await cli.run({ arguments: ["analyze-javascript-application", root, "--format", "json"], }); expect(analyzed.exitCode, analyzed.stdout + analyzed.stderr).toBe(0); @@ -54,7 +54,7 @@ cliTest( traceInput, JSON.stringify({ application: evidence, seed }), ); - const traced = await runFileCli(cli, { + const traced = await cli.run({ arguments: ["trace-application-feature", traceInput, "--json"], }); expect(traced.exitCode, traced.stdout + traced.stderr).toBe(0); @@ -86,7 +86,7 @@ cliTest( view, }), ); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: ["inspect-analysis-view", queryPath, "--json"], }); expect(result.exitCode, result.stdout + result.stderr).toBe(0); diff --git a/tests/acceptance/applications/javascriptFactoryCompletion.test.ts b/tests/acceptance/applications/javascriptFactoryCompletion.test.ts index fb348085ab..fdc6ff1d4c 100644 --- a/tests/acceptance/applications/javascriptFactoryCompletion.test.ts +++ b/tests/acceptance/applications/javascriptFactoryCompletion.test.ts @@ -417,7 +417,6 @@ cliTest( } } }, - 120_000, ); cliTest( @@ -500,5 +499,4 @@ cliTest( ); } }, - 30_000, ); diff --git a/tests/acceptance/applications/javascriptFailureEvidenceOutput.test.ts b/tests/acceptance/applications/javascriptFailureEvidenceOutput.test.ts index 1c5cd6430a..801fc1f979 100644 --- a/tests/acceptance/applications/javascriptFailureEvidenceOutput.test.ts +++ b/tests/acceptance/applications/javascriptFailureEvidenceOutput.test.ts @@ -119,7 +119,6 @@ cliTest( Buffer.byteLength(inline.stdout), ); }, - 60_000, ); cliTest( @@ -152,7 +151,6 @@ cliTest( }); expect(await readFile(output, "utf8")).toBe("kept"); }, - 60_000, ); // A one-line module finishes well within 5 s; a string-array-style module of @@ -227,7 +225,6 @@ cliTest( ]), ); }, - 120_000, ); // Drive the real staged publication of the partial Evidence across its @@ -305,7 +302,6 @@ cliTest( }); expect(staging).toHaveLength(1); }, - 60_000, ); cliTest( @@ -327,7 +323,6 @@ cliTest( }); expect(staging).toEqual([]); }, - 60_000, ); cliTest( @@ -356,5 +351,4 @@ cliTest( }); expect(staging).toEqual([]); }, - 60_000, ); diff --git a/tests/acceptance/applications/javascriptFeatureTraceSeedSelectionCli.test.ts b/tests/acceptance/applications/javascriptFeatureTraceSeedSelectionCli.test.ts index b69d75dcca..dd46c698f7 100644 --- a/tests/acceptance/applications/javascriptFeatureTraceSeedSelectionCli.test.ts +++ b/tests/acceptance/applications/javascriptFeatureTraceSeedSelectionCli.test.ts @@ -13,7 +13,6 @@ import { findApplicationFeatureSeeds } from "../../../src/domain/javascript/java import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; import { cliTest, - runFileCli, type SharedMcpCall, type TestCli, } from "../../support/cli/cliFixture.js"; @@ -170,7 +169,7 @@ const traceThroughCli = async ( const invocation = { arguments: ["trace-application-feature", input, "--json"], }; - const response = await runFileCli(cli, invocation); + const response = await cli.run(invocation); expect(response.exitCode, `${scenario.name} via CLI`).toBe(0); const evidence = toolContract("trace_application_feature").outputSchema.parse( response.json, diff --git a/tests/acceptance/applications/javascriptFsFunctionArgumentsCli.test.ts b/tests/acceptance/applications/javascriptFsFunctionArgumentsCli.test.ts new file mode 100644 index 0000000000..8a733ad398 --- /dev/null +++ b/tests/acceptance/applications/javascriptFsFunctionArgumentsCli.test.ts @@ -0,0 +1,143 @@ +import { expect } from "vitest"; +import { createHash } from "node:crypto"; +import { readFile } from "node:fs/promises"; +import { join } from "node:path"; +import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; +import { cliTest } from "../../support/cli/cliFixture.js"; +import { + analyzeJavaScriptDirectory, + runNodeJsonControl, +} from "../../support/cli/javascriptApplication.js"; +import { writeFixtureFiles } from "../../support/javascriptApplicationFixture.js"; +import { resolveJavaScriptSemanticEvidence } from "../../support/javascriptSemanticGraphFixture.js"; + +cliTest.for(["CLI", "stdio MCP"] as const)( + "matches Node's fs function-valued argument rejection through %s", + async (surface, { cli }) => { + const root = await createTestTempDirectory("rea-fs-function-arguments-"); + await writeFixtureFiles(root, { + input: "fixture", + "entry.mjs": ` + import {open, openSync, closeSync} from "node:fs"; + import {open as openPromise} from "node:fs/promises"; + export const badSyncFlag = p => openSync(p, () => {}); + export const badSyncMode = p => openSync(p, "r", () => {}); + export const badSyncClassFlag = p => openSync(p, class {}); + export const badSyncClassMode = p => openSync(p, "r", class {}); + export const badPromiseFlag = p => openPromise(p, () => {}); + export const badPromiseClassFlag = p => openPromise(p, class {}); + export const badCallbackFlag = p => open(p, () => {}, () => {}); + export const missingCallback = p => open(p); + export const missingShortCallback = p => open(p, "r"); + export const nonfunctionCallback = p => open(p, "r", 0o644, 17); + export async function badPromiseMode(p) { + const h = await openPromise(p, "r", () => {}); h.close(); + } + export async function badPromiseClassMode(p) { + const h = await openPromise(p, "r", class {}); h.close(); + } + export const callbackOnly = p => new Promise((resolve, reject) => + open(p, (error, fd) => { + if (error) reject(error); + else { closeSync(fd); resolve("opened"); } + })); + export const callbackMode = p => new Promise((resolve, reject) => + open(p, "r", (error, fd) => { + if (error) reject(error); + else { closeSync(fd); resolve("opened"); } + })); + export const emptySpread = (p, ...args) => new Promise((resolve, reject) => + open(p, (error, fd) => { + if (error) reject(error); + else { closeSync(fd); resolve("opened"); } + }, ...args)); + `, + "control.mjs": ` + import * as fixture from "./entry.mjs"; + import {closeSync} from "node:fs"; + const observed = {}; + for (const [name, fn] of Object.entries(fixture)) { + try { + const value = await fn("input"); + if (typeof value === "number") closeSync(value); + else if (typeof value?.close === "function") await value.close(); + observed[name] = value === "opened" ? value : "unexpected success"; + } catch (error) { observed[name] = error.code; } + } + console.log(JSON.stringify(observed)); + `, + }); + expect(await runNodeJsonControl(root, "control.mjs")).toEqual({ + badSyncFlag: "ERR_INVALID_ARG_VALUE", + badSyncMode: "ERR_INVALID_ARG_TYPE", + badSyncClassFlag: "ERR_INVALID_ARG_VALUE", + badSyncClassMode: "ERR_INVALID_ARG_TYPE", + badPromiseFlag: "ERR_INVALID_ARG_VALUE", + badPromiseClassFlag: "ERR_INVALID_ARG_VALUE", + badCallbackFlag: "ERR_INVALID_ARG_VALUE", + missingCallback: "ERR_INVALID_ARG_TYPE", + missingShortCallback: "ERR_INVALID_ARG_TYPE", + nonfunctionCallback: "ERR_INVALID_ARG_TYPE", + badPromiseMode: "ERR_INVALID_ARG_TYPE", + badPromiseClassMode: "ERR_INVALID_ARG_TYPE", + callbackOnly: "opened", + callbackMode: "opened", + emptySpread: "opened", + }); + + const result = await analyzeJavaScriptDirectory(surface, cli, root, { + sourcePaths: ["entry.mjs"], + }); + const resources = result.semantic_graph.nodes.filter( + ({ kind }) => kind === "resource", + ); + expect(resources).toHaveLength(3); + expect(resources.map(({ properties }) => properties.method)).toEqual([ + "open", + "open", + "open", + ]); + const source = await readFile(join(root, "entry.mjs"), "utf8"); + const expectedLines = source + .split("\n") + .flatMap((line, index) => + line.includes("open(p, (error, fd)") || + line.includes('open(p, "r", (error, fd)') + ? [index + 1] + : [], + ); + const lines = resources.map(({ evidence: reference }) => { + const evidence = resolveJavaScriptSemanticEvidence( + result.semantic_graph, + reference, + ); + expect(evidence).toMatchObject({ + state: "observed", + authority: "ast-static-analysis", + artifact: { + available: true, + sha256: createHash("sha256").update(source).digest("hex"), + }, + location: { + available: true, + value: { kind: "source-range", source: "entry.mjs" }, + }, + }); + const location = evidence.location; + if (!location.available || location.value.kind !== "source-range") + throw new Error("Expected a source range for the accepted open"); + return location.value.start.line; + }); + expect(lines.sort((a, b) => a - b)).toEqual(expectedLines); + expect( + result.semantic_graph.relations.filter( + ({ relation }) => relation === "acquires", + ), + ).toHaveLength(3); + expect( + result.semantic_graph.relations.filter( + ({ relation }) => relation === "releases", + ), + ).toEqual([]); + }, +); diff --git a/tests/acceptance/applications/javascriptFunctionNameCli.test.ts b/tests/acceptance/applications/javascriptFunctionNameCli.test.ts index b402c5d7f3..c05ca69de9 100644 --- a/tests/acceptance/applications/javascriptFunctionNameCli.test.ts +++ b/tests/acceptance/applications/javascriptFunctionNameCli.test.ts @@ -61,5 +61,4 @@ it.each([ ); expect(targets).toEqual(targetLine === null ? [] : [targetLine]); }, - 20_000, ); diff --git a/tests/acceptance/applications/javascriptGraphqlWideSourceCli.test.ts b/tests/acceptance/applications/javascriptGraphqlWideSourceCli.test.ts new file mode 100644 index 0000000000..91d920fc86 --- /dev/null +++ b/tests/acceptance/applications/javascriptGraphqlWideSourceCli.test.ts @@ -0,0 +1,58 @@ +import { createHash } from "node:crypto"; +import { readFile, writeFile } from "node:fs/promises"; +import { join } from "node:path"; +import { expect } from "vitest"; + +import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; +import { cliTest } from "../../support/cli/cliFixture.js"; +import { analyzeJavaScriptDirectory } from "../../support/cli/javascriptApplication.js"; + +cliTest.for(["CLI", "stdio MCP"] as const)( + "retains wide GraphQL Source ownership without dropping its file through %s", + async (surface, { cli }) => { + const root = await createTestTempDirectory("rea-graphql-wide-source-"); + const path = join(root, "main.mjs"); + // This exceeds the worker's 2 MiB V8 argument stack while staying within + // admitted source/AST limits. Duplicate properties are valid inert JS. + const source = `const source = { + locationOffset: { line: 1, column: 1 }, + ...{locationOffset: { line: 1, column: 1 }, + ${'body:"query Overwritten { x }",'.repeat(270_000)}}, + body: "query Active { viewer { id } }", + note: "query Unrelated { viewer { id } }", + };`; + await writeFile(path, source); + + const result = await analyzeJavaScriptDirectory(surface, cli, root); + expect(result.statistics).toMatchObject({ + parsed_javascript_files: 1, + skipped_javascript_files: 0, + parse_failures: 0, + }); + const operations = result.graph.nodes + .filter((node) => node.kind === "endpoint") + .flatMap((node) => node.observations) + .filter(({ properties }) => properties.mechanism === "graphql:query"); + // Overwritten body copies stay owned by the Source; its unrelated string + // remains independently scannable, and the final body supplies the query. + expect(operations.map(({ properties }) => properties.value).sort()).toEqual( + ["Active", "Unrelated"], + ); + expect(result.graph.coverage.status).toBe("complete"); + for (const operation of operations) + expect(operation.evidence).toMatchObject({ + authority: "ast-static-analysis", + state: "observed", + artifact: { available: true }, + location: { + available: true, + value: { kind: "source-range", source: "main.mjs" }, + }, + }); + expect( + createHash("sha256") + .update(await readFile(path)) + .digest("hex"), + ).toBe(createHash("sha256").update(source).digest("hex")); + }, +); diff --git a/tests/acceptance/applications/javascriptIterationAliases.test.ts b/tests/acceptance/applications/javascriptIterationAliases.test.ts index a7f039eb83..bb9246c6a9 100644 --- a/tests/acceptance/applications/javascriptIterationAliases.test.ts +++ b/tests/acceptance/applications/javascriptIterationAliases.test.ts @@ -72,5 +72,4 @@ const assertReturns = (fields: JavaScriptReturnFields): void => { cliTest( "preserves yielded-object mutation uncertainty through CLI and stdio MCP", ({ cli }) => verifyJavaScriptReturnShapes(cli, source, assertReturns), - 120_000, ); diff --git a/tests/acceptance/applications/javascriptPartialEndpointsCli.test.ts b/tests/acceptance/applications/javascriptPartialEndpointsCli.test.ts index 168364905b..51f74cb6d5 100644 --- a/tests/acceptance/applications/javascriptPartialEndpointsCli.test.ts +++ b/tests/acceptance/applications/javascriptPartialEndpointsCli.test.ts @@ -19,7 +19,6 @@ cliTest( const cliResult = await driver.run({ arguments: ["analyze-javascript-application", root, "--json"], environment: process.env, - timeoutMs: 15_000, }); expect(cliResult.exitCode, cliResult.stderr).toBe(0); const result = javascriptApplicationAnalysisResultSchema.parse( @@ -57,5 +56,4 @@ cliTest( ), ).toBe(true); }, - 20_000, ); diff --git a/tests/acceptance/applications/javascriptReturnedReferenceCycles.test.ts b/tests/acceptance/applications/javascriptReturnedReferenceCycles.test.ts index 74b7a8a478..1b834f13a2 100644 --- a/tests/acceptance/applications/javascriptReturnedReferenceCycles.test.ts +++ b/tests/acceptance/applications/javascriptReturnedReferenceCycles.test.ts @@ -141,5 +141,4 @@ fetch("/copied-module");`, expect(analyzed.isError, JSON.stringify(analyzed.content)).not.toBe(true); verify(analyzed.structuredContent); }, - 90_000, ); diff --git a/tests/acceptance/applications/javascriptReturnedReferences.test.ts b/tests/acceptance/applications/javascriptReturnedReferences.test.ts index 8ad3a8da1a..a1ed7a89f2 100644 --- a/tests/acceptance/applications/javascriptReturnedReferences.test.ts +++ b/tests/acceptance/applications/javascriptReturnedReferences.test.ts @@ -210,7 +210,6 @@ cliTest( assertReturns(fields); assertWideReturns(fields); }), - 120_000, ); cliTest( @@ -224,5 +223,4 @@ cliTest( assertWideReturns, ); }, - 120_000, ); diff --git a/tests/acceptance/applications/javascriptSlotStores.test.ts b/tests/acceptance/applications/javascriptSlotStores.test.ts index 3af7e3eeed..d9c9e7f532 100644 --- a/tests/acceptance/applications/javascriptSlotStores.test.ts +++ b/tests/acceptance/applications/javascriptSlotStores.test.ts @@ -94,5 +94,4 @@ const assertReturns = (fields: JavaScriptReturnFields): void => { cliTest( "does not report stale literals after storing references in member slots", ({ cli }) => verifyJavaScriptReturnShapes(cli, source, assertReturns), - 120_000, ); diff --git a/tests/acceptance/applications/javascriptSourceSelectionCli.test.ts b/tests/acceptance/applications/javascriptSourceSelectionCli.test.ts index e152f300c0..4ff9d06605 100644 --- a/tests/acceptance/applications/javascriptSourceSelectionCli.test.ts +++ b/tests/acceptance/applications/javascriptSourceSelectionCli.test.ts @@ -20,7 +20,6 @@ cliTest.for([ { format: "asar", command: "analyze-javascript-application" }, ])( "analyzes selected $format sources through $command with complete artifact identity", - { timeout: 60_000 }, async ({ format, command }, { cli }) => { const directory = await createTestTempDirectory("rea-js-source-selection-"); const files = await writeJavaScriptSourceSelectionFixture(directory); @@ -202,7 +201,6 @@ cliTest.for([ }, ])( "appends $source --source-paths-json selections for $format through $command", - { timeout: 60_000 }, async ({ command, source, format }, { cli }) => { const directory = await createTestTempDirectory( "rea-js-source-paths-json-", @@ -324,7 +322,6 @@ cliTest( cliTest( "accepts legal ASAR JSON whitespace while preserving selected-source identity", - { timeout: 60_000 }, async ({ cli }) => { const root = await createTestTempDirectory("rea-js-asar-whitespace-"); await writeJavaScriptSourceSelectionFixture(root); diff --git a/tests/acceptance/applications/javascriptUnknownCapacity.test.ts b/tests/acceptance/applications/javascriptUnknownCapacity.test.ts index 6af4858728..68ee3052b9 100644 --- a/tests/acceptance/applications/javascriptUnknownCapacity.test.ts +++ b/tests/acceptance/applications/javascriptUnknownCapacity.test.ts @@ -1,10 +1,9 @@ -import { Client } from "@modelcontextprotocol/client"; -import { StdioClientTransport } from "@modelcontextprotocol/client/stdio"; import { writeFile } from "node:fs/promises"; -import { join, resolve } from "node:path"; +import { join } from "node:path"; import { expect } from "vitest"; import { toolContract } from "../../../src/contracts/toolContracts.js"; import { javascriptApplicationAnalysisResultSchema } from "../../../src/domain/javascript/javascriptApplicationAnalysis.js"; +import { connectLocalToolsMcp } from "../../fixtures/localToolsMcp.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; import { cliTest } from "../../support/cli/cliFixture.js"; @@ -58,35 +57,15 @@ cliTest( 0, ), ).toBe(29); - const transport = new StdioClientTransport({ - command: process.execPath, - args: [resolve("scripts/rea.mjs"), "mcp"], - cwd: process.cwd(), - env: { PATH: process.env.PATH ?? "", REA_LOG_LEVEL: "silent" }, - stderr: "pipe", + const { client } = await connectLocalToolsMcp(); + const response = await client.callTool({ + name: contract.name, + arguments: { input_path: root, detail: "complete", ...options }, }); - const client = new Client({ - name: "unknown-capacity-regression", - version: "1", - }); - try { - await client.connect(transport); - const response = await client.callTool({ - name: contract.name, - arguments: { input_path: root, detail: "complete", ...options }, - }); - expect(response.isError, JSON.stringify(response)).not.toBe(true); - const mcp = contract.outputSchema.parse(response.structuredContent); - expect(mcp.parameters).toEqual(evidence.parameters); - expect(mcp.evidence_id).toBe(evidence.evidence_id); - expect(mcp.normalized_result).toEqual(evidence.normalized_result); - } finally { - try { - await client.close(); - } finally { - await transport.close(); - } - } + expect(response.isError, JSON.stringify(response)).not.toBe(true); + const mcp = contract.outputSchema.parse(response.structuredContent); + expect(mcp.parameters).toEqual(evidence.parameters); + expect(mcp.evidence_id).toBe(evidence.evidence_id); + expect(mcp.normalized_result).toEqual(evidence.normalized_result); }, - 30_000, ); diff --git a/tests/acceptance/applications/javascriptWorkerUrlCli.test.ts b/tests/acceptance/applications/javascriptWorkerUrlCli.test.ts index a029f003e2..3da8f80d9e 100644 --- a/tests/acceptance/applications/javascriptWorkerUrlCli.test.ts +++ b/tests/acceptance/applications/javascriptWorkerUrlCli.test.ts @@ -591,7 +591,7 @@ cliTest( const result = await analyzeJavaScriptDirectory( "CLI", - { run: (invocation) => cli.run({ ...invocation, timeoutMs: 20_000 }) }, + { run: (invocation) => cli.run({ ...invocation }) }, root, ); expect(result.statistics.parse_failures).toBe(0); diff --git a/tests/acceptance/applications/keyedArchiveTextUnicode.test.ts b/tests/acceptance/applications/keyedArchiveTextUnicode.test.ts index 27873a7e8d..40c0341594 100644 --- a/tests/acceptance/applications/keyedArchiveTextUnicode.test.ts +++ b/tests/acceptance/applications/keyedArchiveTextUnicode.test.ts @@ -8,7 +8,7 @@ import { afterAll, expect } from "vitest"; import { parse as parseYaml } from "yaml"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; -import { cliTest, runFileCli } from "../../support/cli/cliFixture.js"; +import { cliTest } from "../../support/cli/cliFixture.js"; afterAll(() => disposeMdItInstance()); @@ -45,9 +45,10 @@ cliTest.for([ const invocation = { arguments: ["inspect-keyed-archive", path, ...args], }; - return label === "high" || label === "pair" - ? cli.run(invocation) - : runFileCli(cli, invocation); + return cli.run({ + ...invocation, + freshProcess: label === "high" || label === "pair", + }); }; for (const format of ["json", "yaml"] as const) { const output = await run(["--format", format]); @@ -120,9 +121,7 @@ cliTest( const invocation = { arguments: ["inspect-keyed-archive", path, ...args], }; - return label === "nul" - ? cli.run(invocation) - : runFileCli(cli, invocation); + return cli.run({ ...invocation, freshProcess: label === "nul" }); }; const json = await run(["--json"]); expect(json.exitCode, json.stderr).toBe(0); diff --git a/tests/acceptance/applications/keyedArchiveXmlNumbers.test.ts b/tests/acceptance/applications/keyedArchiveXmlNumbers.test.ts index 0c10ff8019..252169987d 100644 --- a/tests/acceptance/applications/keyedArchiveXmlNumbers.test.ts +++ b/tests/acceptance/applications/keyedArchiveXmlNumbers.test.ts @@ -8,7 +8,7 @@ import { expect } from "vitest"; import { keyedArchiveResultSchema } from "../../../src/domain/apple/keyedArchive.js"; import { parseEvidence } from "../../../src/domain/evidence.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; -import { cliTest, runFileCli } from "../../support/cli/cliFixture.js"; +import { cliTest } from "../../support/cli/cliFixture.js"; cliTest.for([ { name: "plain real", element: "1.25", expected: 1.25 }, @@ -74,7 +74,7 @@ cliTest.for([ await writeFile(path, bytes); const call = await cli.sharedMcpCall?.(); if (call === undefined) throw new Error("Missing file-scoped MCP session"); - const output = await runFileCli(cli, { + const output = await cli.run({ arguments: [ "inspect-keyed-archive", path, @@ -155,7 +155,7 @@ cliTest( }).replace("__VALUE__", element), ); await writeFile(path, bytes); - const output = await runFileCli(cli, { + const output = await cli.run({ arguments: ["inspect-keyed-archive", path, "--json"], environment: { REA_LOG_LEVEL: "silent" }, }); diff --git a/tests/acceptance/applications/mobileDirectoryComponents.test.ts b/tests/acceptance/applications/mobileDirectoryComponents.test.ts index eb68d8ebb3..d7e60bb985 100644 --- a/tests/acceptance/applications/mobileDirectoryComponents.test.ts +++ b/tests/acceptance/applications/mobileDirectoryComponents.test.ts @@ -19,7 +19,7 @@ import { parseEvidence } from "../../../src/domain/evidence.js"; import { harmonyApplicationProjectionResultSchema } from "../../../src/domain/harmony/harmonyApplication.js"; import { connectLocalToolsMcp } from "../../fixtures/localToolsMcp.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; -import { cliTest, runFileCli } from "../../support/cli/cliFixture.js"; +import { cliTest, type CliInvocation } from "../../support/cli/cliFixture.js"; const harmony = { family: "HarmonyOS", @@ -140,17 +140,14 @@ const fixtures = [ cliTest.for(fixtures)( "preserves entry kinds in $family projections through CLI and stdio MCP", - { timeout: 60_000 }, async (fixture, { cli }) => { const root = await createTestTempDirectory("rea-mobile-directory-"); const environment = { HOME: root, USERPROFILE: root }; const { call } = await connectLocalToolsMcp(environment); for (const mode of ["directories", "files", "nested"] as const) { // Keep one fresh dispatcher check per family; reuse later commands. - const run = - mode === "directories" - ? cli.run.bind(cli) - : runFileCli.bind(undefined, cli); + const run = (invocation: CliInvocation) => + cli.run({ ...invocation, freshProcess: mode === "directories" }); const writer = new ZipWriter(new Uint8ArrayWriter()); for (const path of fixture.paths) { if (mode !== "files") @@ -320,15 +317,13 @@ cliTest.for([ }, ])( "preserves encrypted $family path facts through CLI and stdio MCP", - { timeout: 60_000 }, async (fixture, { cli }) => { const root = await createTestTempDirectory("rea-mobile-encrypted-paths-"); const environment = { HOME: root, USERPROFILE: root }; const { call } = await connectLocalToolsMcp(environment); for (const encrypted of [false, true]) { - const run = encrypted - ? runFileCli.bind(undefined, cli) - : cli.run.bind(cli); + const run = (invocation: CliInvocation) => + cli.run({ ...invocation, freshProcess: !encrypted }); const writer = new ZipWriter(new Uint8ArrayWriter()); for (const member of fixture.paths) await writer.add( diff --git a/tests/acceptance/applications/mobileFrameworkConventions.test.ts b/tests/acceptance/applications/mobileFrameworkConventions.test.ts index 4a9242e7a8..3a8d3e3870 100644 --- a/tests/acceptance/applications/mobileFrameworkConventions.test.ts +++ b/tests/acceptance/applications/mobileFrameworkConventions.test.ts @@ -138,5 +138,4 @@ cliTest( } } }, - 30_000, ); diff --git a/tests/acceptance/applications/nativeExportsVersionRequirements.test.ts b/tests/acceptance/applications/nativeExportsVersionRequirements.test.ts index ba3bbea7cc..c733cb0943 100644 --- a/tests/acceptance/applications/nativeExportsVersionRequirements.test.ts +++ b/tests/acceptance/applications/nativeExportsVersionRequirements.test.ts @@ -72,7 +72,6 @@ cliTest( }, ); }, - 30_000, ); cliTest( @@ -113,5 +112,4 @@ cliTest( }, ); }, - 30_000, ); diff --git a/tests/acceptance/applications/nativeExportsViews.test.ts b/tests/acceptance/applications/nativeExportsViews.test.ts index 83b1199410..d249d36a9c 100644 --- a/tests/acceptance/applications/nativeExportsViews.test.ts +++ b/tests/acceptance/applications/nativeExportsViews.test.ts @@ -20,7 +20,6 @@ const viewContract = toolContract("inspect_analysis_view"); cliTest.for(["pe", "elf"] as const)( "queries retained %s exports through CLI and actual stdio MCP after the source is deleted", - { timeout: 60_000 }, async (format, { cli }) => { const root = await createTestTempDirectory("rea-export-views-"); const path = join(root, `library.${format}`); @@ -41,7 +40,7 @@ cliTest.for(["pe", "elf"] as const)( versionDefinitions: ["fixture", "V1"], }); await writeFile(path, bytes); - const run = cli.sharedCliRun?.bind(cli) ?? cli.run.bind(cli); + const run = cli.run.bind(cli); const complete = await run({ arguments: ["inspect-native-exports", path, "--json"], }); diff --git a/tests/acceptance/applications/nativePlistBinaryScalars.test.ts b/tests/acceptance/applications/nativePlistBinaryScalars.test.ts index 0b17b4b893..176437ff78 100644 --- a/tests/acceptance/applications/nativePlistBinaryScalars.test.ts +++ b/tests/acceptance/applications/nativePlistBinaryScalars.test.ts @@ -189,7 +189,6 @@ describe.skipIf(process.platform !== "darwin")( expect(Buffer.compare(await readFile(binaryPath), binaryBytes)).toBe(0); expect(Buffer.compare(await readFile(xmlPath), xmlBytes)).toBe(0); }, - 60_000, ); }, ); diff --git a/tests/acceptance/applications/nativePlistNesting.test.ts b/tests/acceptance/applications/nativePlistNesting.test.ts index 816d481ce7..9ad448625f 100644 --- a/tests/acceptance/applications/nativePlistNesting.test.ts +++ b/tests/acceptance/applications/nativePlistNesting.test.ts @@ -169,7 +169,6 @@ describe.skipIf(process.platform !== "darwin")( ).toBe(false); } }, - 60_000, ); }, ); diff --git a/tests/acceptance/applications/nibFieldIdentity.test.ts b/tests/acceptance/applications/nibFieldIdentity.test.ts index 44c31f5315..b752c65f17 100644 --- a/tests/acceptance/applications/nibFieldIdentity.test.ts +++ b/tests/acceptance/applications/nibFieldIdentity.test.ts @@ -196,5 +196,4 @@ cliTest( ); expect(Buffer.compare(await readFile(path), archive)).toBe(0); }, - 60_000, ); diff --git a/tests/acceptance/applications/peResources.test.ts b/tests/acceptance/applications/peResources.test.ts index 6544701a4f..29560a42c8 100644 --- a/tests/acceptance/applications/peResources.test.ts +++ b/tests/acceptance/applications/peResources.test.ts @@ -1,19 +1,15 @@ import { Client } from "@modelcontextprotocol/client"; -import { StdioClientTransport } from "@modelcontextprotocol/client/stdio"; import { createHash } from "node:crypto"; import { readFile, writeFile } from "node:fs/promises"; -import { join, resolve } from "node:path"; +import { join } from "node:path"; import { expect } from "vitest"; import { toolContract } from "../../../src/contracts/toolContracts.js"; import { peResourcesSchema } from "../../../src/domain/native/peResources.js"; import { peResourceFixture } from "../../../src/native/pe/PeResources.fixture.js"; +import { connectLocalToolsMcp } from "../../fixtures/localToolsMcp.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; import { parseMcpToolError } from "../../fixtures/mcpToolError.js"; -import { - cliTest, - runFileCli, - type TestCli, -} from "../../support/cli/cliFixture.js"; +import { cliTest, type TestCli } from "../../support/cli/cliFixture.js"; type PeResourceCall = ( arguments_: Record, @@ -26,7 +22,7 @@ const expectPeInputFailureAcrossAdapters = async (input: { readonly reason: "invalid_format" | "invalid_value"; }) => { const { cli, call, path, reason } = input; - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: ["inspect-pe-resources", path, "--json"], }); expect(result.exitCode).toBe(1); @@ -76,9 +72,17 @@ const expectPeInputFailuresAcrossAdapters = async (input: { const { cli, call, root } = input; const malformedPath = join(root, "malformed.exe"); await writeFile(malformedPath, Buffer.from("not a PE image")); + const truncatedPath = join(root, "truncated-resource-table.exe"); + const truncated = peResourceFixture(); + const directory = truncated.directoryOffsets[0]; + if (directory === undefined) throw new Error("Missing resource directory"); + truncated.bytes.writeUInt16LE(0xffff, directory + 12); + truncated.bytes.writeUInt16LE(2, directory + 14); + await writeFile(truncatedPath, truncated.bytes); for (const [path, reason] of [ [join(root, "missing.exe"), "invalid_value"], [malformedPath, "invalid_format"], + [truncatedPath, "invalid_format"], ] as const) await expectPeInputFailureAcrossAdapters({ cli, @@ -109,7 +113,7 @@ const expectPeWordAlignedRecordsAcrossAdapters = async (input: { ); const path = join(root, `word-aligned-${plus}-${index}.exe`); await writeFile(path, fixture.bytes); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: ["inspect-pe-resources", path, "--json"], }); expect(result.exitCode, result.stderr).toBe(0); @@ -200,7 +204,7 @@ const expectPeMappingOwnershipAcrossAdapters = async ( }); continue; } - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: ["inspect-pe-resources", path, "--json"], }); expect(result.exitCode, name).toBe(0); @@ -258,9 +262,7 @@ const expectPeInventoriesAcrossAdapters = async ( }; // Keep an independent public-wrapper process for both PE32 and PE32+. // The empty-payload variants reuse only compiled-command startup. - const result = await (empty - ? runFileCli(cli, invocation) - : cli.run(invocation)); + const result = await (empty ? cli.run(invocation) : cli.run(invocation)); expect(result.exitCode).toBe(0); const evidence = contract.outputSchema.parse(result.json); const report = peResourcesSchema.parse(evidence.normalized_result); @@ -340,7 +342,7 @@ const expectPeEmptyBranchesAcrossAdapters = async ( shared ? "shared-empty64.exe" : "empty-branches32.exe", ); await writeFile(path, fixture.bytes); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: ["inspect-pe-resources", path, "--json"], }); expect(result.exitCode, result.stderr).toBe(0); @@ -414,114 +416,97 @@ cliTest( "inspects PE32/PE32+ resources through compiled CLI and real stdio MCP without an active target", async ({ cli }) => { const root = await createTestTempDirectory("rea-pe-resource-acceptance-"); - const transport = new StdioClientTransport({ - command: process.execPath, - args: [resolve("scripts/rea.mjs"), "mcp"], - cwd: process.cwd(), - env: { PATH: process.env.PATH ?? "", REA_LOG_LEVEL: "silent" }, - stderr: "pipe", - }); - const client = new Client({ name: "pe-resource-acceptance", version: "1" }); - try { - await client.connect(transport); - const inventory = await client.listTools(); - const definition = inventory.tools.find( - ({ name }) => name === contract.name, - ); - if (definition === undefined) - throw new Error("Missing PE resource tool contract"); - expect(inventory.tools).toContainEqual( - expect.objectContaining({ - name: contract.name, - annotations: expect.objectContaining({ - readOnlyHint: false, - openWorldHint: false, - }), + const { client } = await connectLocalToolsMcp(); + const inventory = await client.listTools(); + const definition = inventory.tools.find( + ({ name }) => name === contract.name, + ); + if (definition === undefined) + throw new Error("Missing PE resource tool contract"); + expect(inventory.tools).toContainEqual( + expect.objectContaining({ + name: contract.name, + annotations: expect.objectContaining({ + readOnlyHint: false, + openWorldHint: false, }), + }), + ); + const call: PeResourceCall = (arguments_) => + client.callTool( + { name: definition.name, arguments: arguments_ }, + { toolDefinition: definition }, ); - const call: PeResourceCall = (arguments_) => - client.callTool( - { name: definition.name, arguments: arguments_ }, - { toolDefinition: definition }, - ); - const sessionDefinition = inventory.tools.find( - ({ name }) => name === "binary_session", - ); - if (sessionDefinition === undefined) - throw new Error("Missing binary_session tool contract"); - const session = await client.callTool( - { name: sessionDefinition.name, arguments: {} }, - { toolDefinition: sessionDefinition }, - ); - expect(session.isError, JSON.stringify(session)).not.toBe(true); - const status = toolContract("binary_session").outputSchema.parse( - session.structuredContent, - ).result; - expect( - status.tool_availability.groups.find((group) => - group.names.includes("inspect_pe_resources"), - ), - ).toMatchObject({ available: true }); - await expectPeInventoriesAcrossAdapters(cli, call, root); - await expectPeEmptyBranchesAcrossAdapters(cli, call, root); - await expectPeWordAlignedRecordsAcrossAdapters({ - cli, - call, - root, - }); - await expectPeMappingOwnershipAcrossAdapters(cli, call, root); - await expectPePaddingFailureAcrossAdapters({ - cli, - call, - path: join(root, "raw-padding.exe"), - }); - await expectPeInputFailuresAcrossAdapters({ - cli, - call, - root, - }); - const absent = peResourceFixture(); - absent.bytes.fill(0, absent.directoryAt, absent.directoryAt + 8); - const path = join(root, "absent.exe"); - await writeFile(path, absent.bytes); - const empty = await call({ path }); - expect(empty.isError).not.toBe(true); - expect( - contract.outputSchema.parse(empty.structuredContent).normalized_result, - ).toMatchObject({ - directory: null, - directories: [], - resources: [], - coverage: { status: "complete", resources: 0 }, - }); - for (const arguments_ of [ - { path, max_file_bytes: 64 }, - { path: "relative.exe" }, - { path, extra: true }, - ]) { - const rejected = await call(arguments_); - expect(rejected.isError).toBe(true); - expect( - contract.outputSchema.safeParse(rejected.structuredContent).success, - ).toBe(false); - } - const damaged = peResourceFixture(); - damaged.bytes.writeUInt32LE(17, damaged.directoryAt + 4); - const badPath = join(root, "damaged.exe"); - await writeFile(badPath, damaged.bytes); - const rejected = await call({ path: badPath }); + const sessionDefinition = inventory.tools.find( + ({ name }) => name === "binary_session", + ); + if (sessionDefinition === undefined) + throw new Error("Missing binary_session tool contract"); + const session = await client.callTool( + { name: sessionDefinition.name, arguments: {} }, + { toolDefinition: sessionDefinition }, + ); + expect(session.isError, JSON.stringify(session)).not.toBe(true); + const status = toolContract("binary_session").outputSchema.parse( + session.structuredContent, + ).result; + expect( + status.tool_availability.groups.find((group) => + group.names.includes("inspect_pe_resources"), + ), + ).toMatchObject({ available: true }); + await expectPeInventoriesAcrossAdapters(cli, call, root); + await expectPeEmptyBranchesAcrossAdapters(cli, call, root); + await expectPeWordAlignedRecordsAcrossAdapters({ + cli, + call, + root, + }); + await expectPeMappingOwnershipAcrossAdapters(cli, call, root); + await expectPePaddingFailureAcrossAdapters({ + cli, + call, + path: join(root, "raw-padding.exe"), + }); + await expectPeInputFailuresAcrossAdapters({ + cli, + call, + root, + }); + const absent = peResourceFixture(); + absent.bytes.fill(0, absent.directoryAt, absent.directoryAt + 8); + const path = join(root, "absent.exe"); + await writeFile(path, absent.bytes); + const empty = await call({ path }); + expect(empty.isError).not.toBe(true); + expect( + contract.outputSchema.parse(empty.structuredContent).normalized_result, + ).toMatchObject({ + directory: null, + directories: [], + resources: [], + coverage: { status: "complete", resources: 0 }, + }); + for (const arguments_ of [ + { path, max_file_bytes: 64 }, + { path: "relative.exe" }, + { path, extra: true }, + ]) { + const rejected = await call(arguments_); expect(rejected.isError).toBe(true); expect( contract.outputSchema.safeParse(rejected.structuredContent).success, ).toBe(false); - await client.ping(); - } finally { - try { - await client.close(); - } finally { - await transport.close(); - } } + const damaged = peResourceFixture(); + damaged.bytes.writeUInt32LE(17, damaged.directoryAt + 4); + const badPath = join(root, "damaged.exe"); + await writeFile(badPath, damaged.bytes); + const rejected = await call({ path: badPath }); + expect(rejected.isError).toBe(true); + expect( + contract.outputSchema.safeParse(rejected.structuredContent).success, + ).toBe(false); + await client.ping(); }, - 30_000, ); diff --git a/tests/acceptance/applications/playwrightTrace.test.ts b/tests/acceptance/applications/playwrightTrace.test.ts index 2f1c64fd5f..10a5b21e82 100644 --- a/tests/acceptance/applications/playwrightTrace.test.ts +++ b/tests/acceptance/applications/playwrightTrace.test.ts @@ -73,7 +73,7 @@ cliTest( (await call("inspect_playwright_trace", { path: fixture.path, view })) .structuredContent, ).toEqual(selected); - const run = cli.sharedCliRun?.bind(cli) ?? cli.run.bind(cli); + const run = cli.run.bind(cli); const result = await run({ arguments: ["inspect-playwright-trace", fixture.path, "--json"], }); @@ -102,7 +102,7 @@ const checkCliPublication = async ({ call: McpCall; }) => { const source = { kind: "retained-evidence", evidence_id: parent.evidence_id }; - const run = cli.sharedCliRun?.bind(cli) ?? cli.run.bind(cli); + const run = cli.run.bind(cli); const fullCli = await run({ arguments: ["inspect-playwright-trace", fixture.path, "--json"], }); @@ -420,7 +420,7 @@ cliTest( expect( parseEvidence(full.structuredContent).normalized_result, ).toMatchObject({ items: [{ text: message }] }); - const run = cli.sharedCliRun?.bind(cli) ?? cli.run.bind(cli); + const run = cli.run.bind(cli); const result = await run({ arguments: ["inspect-playwright-trace", fixture.path, "--json"], }); diff --git a/tests/acceptance/applications/pythonPycViews.test.ts b/tests/acceptance/applications/pythonPycViews.test.ts index 788d006ee2..486420d166 100644 --- a/tests/acceptance/applications/pythonPycViews.test.ts +++ b/tests/acceptance/applications/pythonPycViews.test.ts @@ -70,12 +70,11 @@ const assertSparseSelectors = async ( cliTest( "queries same-name pyc objects through CLI/MCP after deleting the source", - { timeout: 60_000 }, async ({ cli }) => { const root = await createTestTempDirectory("rea-pyc-views-"); const path = join(root, "module.pyc"); await writeSameNamePyc(path); - const run = cli.sharedCliRun?.bind(cli) ?? cli.run.bind(cli); + const run = cli.run.bind(cli); const complete = await run({ arguments: ["inspect-python-pyc", path, "--json"], }); @@ -223,7 +222,7 @@ cliTest( async ({ cli }) => { const root = await createTestTempDirectory("rea-pyc-invalid-delivery-"); const output = join(root, "unused.json"); - const run = cli.sharedCliRun?.bind(cli) ?? cli.run.bind(cli); + const run = cli.run.bind(cli); const view = JSON.stringify({ kind: "python-pyc", collection: "code_objects", diff --git a/tests/acceptance/applications/runtime.test.ts b/tests/acceptance/applications/runtime.test.ts index c8dedddf63..f201124f23 100644 --- a/tests/acceptance/applications/runtime.test.ts +++ b/tests/acceptance/applications/runtime.test.ts @@ -2,7 +2,7 @@ import { fileURLToPath } from "node:url"; import { Client } from "@modelcontextprotocol/client"; import { StdioClientTransport } from "@modelcontextprotocol/client/stdio"; -import { describe, expect, it } from "vitest"; +import { afterAll, describe, expect, it } from "vitest"; import { z } from "zod"; import { FULL_CATALOG_IDENTITY } from "../../../src/catalogIdentity.js"; @@ -14,6 +14,11 @@ import { } from "../../../src/contracts/toolOutputSchemaPrimitives.js"; import { parseEvidence } from "../../../src/domain/evidence.js"; import { parseMcpToolError } from "../../fixtures/mcpToolError.js"; +import { createMcpSchemaValidation } from "../../fixtures/mcpSchemaValidation.js"; + +// Compile SDK output validators for the tools a test calls, not the whole catalog. +const schemaValidation = createMcpSchemaValidation(); +afterAll(() => schemaValidation.clear()); const mainPath = fileURLToPath( new URL("../../../dist/main.js", import.meta.url), @@ -102,7 +107,10 @@ const callCurrentDocument = async ( transport.stderr?.on("data", (chunk: Buffer | string) => { stderr += chunk.toString(); }); - const client = new Client({ name: "runtime-smoke", version: "1.0.0" }); + const client = new Client( + { name: "runtime-smoke", version: "1.0.0" }, + { jsonSchemaValidator: schemaValidation }, + ); let result: Awaited>; try { @@ -145,7 +153,7 @@ describe("production stdio runtime", () => { ); expect(stderr).not.toContain("HOPPER_LOADER_ARGS_JSON"); expect(stderr).not.toContain(fixturePath); - }, 15_000); + }); it.runIf(process.platform === "darwin")( "returns the selected document on macOS through MCP and structured logs", @@ -175,7 +183,6 @@ describe("production stdio runtime", () => { expect(stderr).not.toContain("HOPPER_LOADER_ARGS_JSON"); expect(stderr).not.toContain(fixturePath); }, - 15_000, ); // Use the suite's bounded acceptance deadline, including its coverage allowance. @@ -195,7 +202,10 @@ describe("production stdio runtime", () => { transport.stderr?.on("data", (chunk: Buffer | string) => { stderr += chunk.toString(); }); - const client = new Client({ name: "catalog-runtime", version: "1.0.0" }); + const client = new Client( + { name: "catalog-runtime", version: "1.0.0" }, + { jsonSchemaValidator: schemaValidation }, + ); try { await client.connect(transport); @@ -315,6 +325,5 @@ describe("production stdio saved-database admission", () => { ), ).toBe(false); }, - 15_000, ); }); diff --git a/tests/acceptance/applications/staticObservedEndpoints.test.ts b/tests/acceptance/applications/staticObservedEndpoints.test.ts index 83b458b2e5..b5581fc496 100644 --- a/tests/acceptance/applications/staticObservedEndpoints.test.ts +++ b/tests/acceptance/applications/staticObservedEndpoints.test.ts @@ -47,7 +47,7 @@ const prepareInputs = async (cli: TestCli) => { "f0e7d2c869c811d4272a7b2916c6531c8c6cc7938336a35575e2a6b7b6346714", ); await writeFile(harPath, harBytes); - const run = cli.sharedCliRun?.bind(cli) ?? cli.run.bind(cli); + const run = cli.run.bind(cli); const analyzed = await run({ arguments: [ "analyze-javascript-application", diff --git a/tests/acceptance/applications/typescriptThisParametersCli.test.ts b/tests/acceptance/applications/typescriptThisParametersCli.test.ts index a90dddec5c..43cd08c52c 100644 --- a/tests/acceptance/applications/typescriptThisParametersCli.test.ts +++ b/tests/acceptance/applications/typescriptThisParametersCli.test.ts @@ -39,11 +39,9 @@ cliTest.for(["CLI", "stdio MCP"] as const)( "console.log(JSON.stringify([a, b, c, d, first.length, rest.length, mixed.length]));", ].join("\n"), }); - const control = await processes.run( - process.execPath, - [join(root, "entry.mts")], - { timeoutMs: 15_000 }, - ); + const control = await processes.run(process.execPath, [ + join(root, "entry.mts"), + ]); expect(control.exitCode, control.stderr).toBe(0); expect(JSON.parse(control.stdout)).toEqual([ 7, diff --git a/tests/acceptance/artifactComparisonAsarUnpacked.test.ts b/tests/acceptance/artifactComparisonAsarUnpacked.test.ts index 19992608a7..b440403e04 100644 --- a/tests/acceptance/artifactComparisonAsarUnpacked.test.ts +++ b/tests/acceptance/artifactComparisonAsarUnpacked.test.ts @@ -4,61 +4,12 @@ import { mkdir, writeFile } from "node:fs/promises"; import { join } from "node:path"; import { expect, it } from "vitest"; -import { - artifactInventoryResultSchema, - type ArtifactInventoryResult, -} from "../../src/domain/artifactGraph.js"; -import { - artifactGraphDigest, - artifactManifestId, -} from "../../src/domain/artifactIdentity.js"; +import { artifactInventoryResultSchema } from "../../src/domain/artifactGraph.js"; import { parseArtifactInventoryEvidence } from "../../src/domain/artifactInventoryEvidence.js"; -import { createEvidence, type Evidence } from "../../src/domain/evidence.js"; +import { historicalInventoryEvidence } from "../fixtures/artifactEvidence.js"; import { connectArtifactComparisonMcp } from "../fixtures/artifactComparisonMcp.js"; import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js"; -const historicalInventory = (source: Evidence): Evidence => { - const inventory = artifactInventoryResultSchema.parse( - source.normalized_result, - ); - const occurrences = inventory.occurrences.map( - ({ unpacked: _unpacked, ...occurrence }) => occurrence, - ); - const graphSha256 = artifactGraphDigest({ - nodes: inventory.nodes, - occurrences, - edges: inventory.edges, - contradictions: inventory.integrity_contradictions, - }); - const historical: ArtifactInventoryResult = { - ...inventory, - occurrences, - manifest: { - ...inventory.manifest, - graph_sha256: graphSha256, - manifest_id: artifactManifestId( - inventory.manifest.root_artifact_id, - graphSha256, - ), - }, - }; - return createEvidence( - { - path: source.subject!.local_path, - sha256: inventory.manifest.root_sha256, - format: inventory.manifest.root_format, - }, - source.provider, - { - operation: "inventory_artifact", - parameters: {}, - result: historical, - confidence: "derived", - authority: "shipped-artifact", - }, - ); -}; - it("compares ASAR companion declarations from public MCP inventories and preserves historical unknowns", async () => { const root = await createTestTempDirectory("rea-asar-unpacked-comparison-"); const input = join(root, "source"); @@ -117,7 +68,10 @@ it("compares ASAR companion declarations from public MCP inventories and preserv // Model an authenticated pre-field snapshot with its original graph commitment, // rather than editing a current Evidence payload without updating its identity. - const historical = historicalInventory(packed); + const historical = historicalInventoryEvidence( + packed, + ({ unpacked: _unpacked, ...occurrence }) => occurrence, + ); const historicalParsed = parseArtifactInventoryEvidence(historical); expect( historicalParsed.inventory.occurrences.map( diff --git a/tests/acceptance/artifactComparisonEspImage.test.ts b/tests/acceptance/artifactComparisonEspImage.test.ts index a6fcfed74e..ee566a1c75 100644 --- a/tests/acceptance/artifactComparisonEspImage.test.ts +++ b/tests/acceptance/artifactComparisonEspImage.test.ts @@ -7,15 +7,8 @@ import { artifactInventorySummaryViewResultSchema } from "../../src/domain/analy import { artifactComparisonResultSchema } from "../../src/domain/artifactComparison.js"; import { artifactInspectionResultSchema } from "../../src/domain/artifactInspection.js"; import { artifactInventoryResultSchema } from "../../src/domain/artifactGraph.js"; -import { - artifactGraphDigest, - artifactManifestId, -} from "../../src/domain/artifactIdentity.js"; -import { - createEvidence, - parseEvidence, - type Evidence, -} from "../../src/domain/evidence.js"; +import { parseEvidence, type Evidence } from "../../src/domain/evidence.js"; +import { historicalInventoryEvidence } from "../fixtures/artifactEvidence.js"; import { connectArtifactComparisonMcp } from "../fixtures/artifactComparisonMcp.js"; import { connectLocalToolsMcp } from "../fixtures/localToolsMcp.js"; import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js"; @@ -80,46 +73,14 @@ it("reports an ESP segment whose load address alone changed", async () => { /** The inventory as REA recorded it before ESP image roots had `flash_dump`. */ const withoutFlashDump = (source: Evidence): Evidence => { - const inventory = artifactInventoryResultSchema.parse( - source.normalized_result, - ); - const occurrences = inventory.occurrences.map((occurrence) => { - if (occurrence.format_metadata?.kind !== "esp-image") return occurrence; - const { flash_dump: _dump, ...historical } = occurrence.format_metadata; - return { ...occurrence, format_metadata: historical }; - }); - // Recommit the reduced graph, as that REA committed it. - const graphSha256 = artifactGraphDigest({ - nodes: inventory.nodes, - occurrences, - edges: inventory.edges, - contradictions: inventory.integrity_contradictions, - }); - return createEvidence( - { - path: source.subject!.local_path, - sha256: inventory.manifest.root_sha256, - format: inventory.manifest.root_format, - }, - source.provider, - { - operation: "inventory_artifact", - parameters: {}, - result: { - ...inventory, - occurrences, - manifest: { - ...inventory.manifest, - graph_sha256: graphSha256, - manifest_id: artifactManifestId( - inventory.manifest.root_artifact_id, - graphSha256, - ), - }, - }, - confidence: "observed", - authority: "shipped-artifact", + return historicalInventoryEvidence( + source, + (occurrence) => { + if (occurrence.format_metadata?.kind !== "esp-image") return occurrence; + const { flash_dump: _dump, ...historical } = occurrence.format_metadata; + return { ...occurrence, format_metadata: historical }; }, + "observed", ); }; diff --git a/tests/acceptance/artifactComparisonNodeSea.test.ts b/tests/acceptance/artifactComparisonNodeSea.test.ts index 6fa8b8d7e1..37d98bd124 100644 --- a/tests/acceptance/artifactComparisonNodeSea.test.ts +++ b/tests/acceptance/artifactComparisonNodeSea.test.ts @@ -3,65 +3,13 @@ import { join } from "node:path"; import { expect, it } from "vitest"; import { nodeSeaPe } from "../../src/artifacts/nodeSea/NodeSea.fixture.js"; -import { - artifactInventoryResultSchema, - type ArtifactInventoryResult, -} from "../../src/domain/artifactGraph.js"; -import { FORMAT_DECLARATION_UNKNOWN_LIMITATION } from "../../src/domain/artifactComparison.js"; -import { - artifactGraphDigest, - artifactManifestId, -} from "../../src/domain/artifactIdentity.js"; +import { artifactInventoryResultSchema } from "../../src/domain/artifactGraph.js"; import { parseArtifactInventoryEvidence } from "../../src/domain/artifactInventoryEvidence.js"; -import { createEvidence, type Evidence } from "../../src/domain/evidence.js"; +import type { Evidence } from "../../src/domain/evidence.js"; +import { historicalInventoryEvidence } from "../fixtures/artifactEvidence.js"; import { connectArtifactComparisonMcp } from "../fixtures/artifactComparisonMcp.js"; import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js"; -const withoutEntryMetadata = (source: Evidence): Evidence => { - const inventory = artifactInventoryResultSchema.parse( - source.normalized_result, - ); - const occurrences = inventory.occurrences.map((occurrence) => { - if (occurrence.format_metadata?.kind !== "node-sea-entry") - return occurrence; - const { format_metadata: _metadata, ...historical } = occurrence; - return historical; - }); - const graphSha256 = artifactGraphDigest({ - nodes: inventory.nodes, - occurrences, - edges: inventory.edges, - contradictions: inventory.integrity_contradictions, - }); - const historical: ArtifactInventoryResult = { - ...inventory, - occurrences, - manifest: { - ...inventory.manifest, - graph_sha256: graphSha256, - manifest_id: artifactManifestId( - inventory.manifest.root_artifact_id, - graphSha256, - ), - }, - }; - return createEvidence( - { - path: source.subject!.local_path, - sha256: inventory.manifest.root_sha256, - format: inventory.manifest.root_format, - }, - source.provider, - { - operation: "inventory_artifact", - parameters: {}, - result: historical, - confidence: "derived", - authority: "shipped-artifact", - }, - ); -}; - it("compares raw SEA asset keys without interpreting serialization order as metadata", async () => { const root = await createTestTempDirectory("rea-sea-key-comparison-"); const nfc = "caf\u00e9.txt"; @@ -138,7 +86,12 @@ it("compares raw SEA asset keys without interpreting serialization order as meta ).toEqual([]); // Recommit the reduced graph before sealing modeled historical Evidence. - const historical = withoutEntryMetadata(left); + const historical = historicalInventoryEvidence(left, (occurrence) => { + if (occurrence.format_metadata?.kind !== "node-sea-entry") + return occurrence; + const { format_metadata: _metadata, ...historical } = occurrence; + return historical; + }); const parsed = parseArtifactInventoryEvidence(historical); expect( parsed.inventory.occurrences.find( @@ -153,7 +106,11 @@ it("compares raw SEA asset keys without interpreting serialization order as meta dimensions: ["availability"], evidence_links: [historical.evidence_id, right.evidence_id], }); - expect(unknown.limitations).toContain(FORMAT_DECLARATION_UNKNOWN_LIMITATION); + expect(unknown.limitations).toContainEqual( + expect.stringMatching( + /metadata declarations are missing.*complete declaration unavailable/u, + ), + ); const reversed = await compare(right, historical); expect( reversed.changes.find(({ logical_path }) => logical_path === nfc), diff --git a/tests/acceptance/artifactComparisonPlist.test.ts b/tests/acceptance/artifactComparisonPlist.test.ts index b21de2aa13..236c9406e6 100644 --- a/tests/acceptance/artifactComparisonPlist.test.ts +++ b/tests/acceptance/artifactComparisonPlist.test.ts @@ -25,7 +25,6 @@ cliTest( const cliResult = await driver.run({ arguments: ["inspect-artifact", leftPath, "--json"], environment: process.env, - timeoutMs: 15_000, }); expect(cliResult.exitCode, cliResult.stderr).toBe(0); const cliEvidence = parseEvidence(JSON.parse(cliResult.stdout)); diff --git a/tests/acceptance/artifactComparisonUnavailableMetadata.test.ts b/tests/acceptance/artifactComparisonUnavailableMetadata.test.ts index 1172074866..268df1965d 100644 --- a/tests/acceptance/artifactComparisonUnavailableMetadata.test.ts +++ b/tests/acceptance/artifactComparisonUnavailableMetadata.test.ts @@ -23,7 +23,6 @@ cliTest( const result = await driver.run({ arguments: ["inspect-artifact", path, "--detail", "complete", "--json"], environment: process.env, - timeoutMs: 15_000, }); expect(result.exitCode, result.stderr).toBe(0); const inspection = artifactInspectionResultSchema.parse( diff --git a/tests/acceptance/artifactDmgAttachmentRecoveryCli.test.ts b/tests/acceptance/artifactDmgAttachmentRecoveryCli.test.ts index ace76f9e32..127d28339e 100644 --- a/tests/acceptance/artifactDmgAttachmentRecoveryCli.test.ts +++ b/tests/acceptance/artifactDmgAttachmentRecoveryCli.test.ts @@ -8,6 +8,9 @@ import { parseNativeDmgInfo } from "../../src/artifacts/NativeDmgOwnership.js"; import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js"; import { cliTest } from "../support/cli/cliFixture.js"; +// Each command verifies the whole image before attaching; hosted macOS runners +// take about three times as long as a local host. The project deadline bounds a +// hang, so no step here hard-codes host speed. cliTest.skipIf(process.platform !== "darwin")( "DMG recovery preserves the caller's real attachment and succeeds after its owner releases it", async ({ cli, processes }) => { @@ -16,23 +19,19 @@ cliTest.skipIf(process.platform !== "darwin")( await mkdir(source); await writeFile(join(source, "fixture.txt"), "caller-owned image\n"); const image = join(root, "fixture.dmg"); - const created = await processes.run( - "/usr/bin/hdiutil", - [ - "create", - "-quiet", - "-srcfolder", - source, - "-volname", - "ReaRecoveryFixture", - "-size", - "20m", - "-format", - "UDZO", - image, - ], - { timeoutMs: 30_000 }, - ); + const created = await processes.run("/usr/bin/hdiutil", [ + "create", + "-quiet", + "-srcfolder", + source, + "-volname", + "ReaRecoveryFixture", + "-size", + "20m", + "-format", + "UDZO", + image, + ]); expect(created.exitCode, created.stderr).toBe(0); const digest = createHash("sha256") .update(await readFile(image)) @@ -41,9 +40,7 @@ cliTest.skipIf(process.platform !== "darwin")( const owner = new NativeDmgArtifactReader(image, process.env); let mountRoot: string | undefined; const attachedImages = async () => { - const info = await processes.run("/usr/bin/hdiutil", ["info", "-plist"], { - timeoutMs: 10_000, - }); + const info = await processes.run("/usr/bin/hdiutil", ["info", "-plist"]); expect(info.exitCode, info.stderr).toBe(0); return parseNativeDmgInfo(info.stdout).images; }; @@ -80,8 +77,9 @@ cliTest.skipIf(process.platform !== "darwin")( expect(before.length).toBeGreaterThan(0); expect(await attachedImagePaths()).toContain(resolvedImage); const busy = await cli.run({ + // Each command must release its own attachment when its process ends. + freshProcess: true, arguments: ["inspect-artifact", image, "--json"], - timeoutMs: 30_000, }); expect(busy.exitCode, busy.stdout).toBe(1); expect(busy.json).toMatchObject({ @@ -119,8 +117,8 @@ cliTest.skipIf(process.platform !== "darwin")( } expect(await mountedDevices()).toEqual([]); const control = await cli.run({ + freshProcess: true, arguments: ["inspect-artifact", image, "--json"], - timeoutMs: 30_000, }); expect(control.exitCode, control.stdout).toBe(0); expect(control.json).toMatchObject({ @@ -140,5 +138,4 @@ cliTest.skipIf(process.platform !== "darwin")( }); expect(await attachedImagePaths()).not.toContain(resolvedImage); }, - 60_000, ); diff --git a/tests/acceptance/binaryHardeningCli.test.ts b/tests/acceptance/binaryHardeningCli.test.ts index 643708e121..cca2caa678 100644 --- a/tests/acceptance/binaryHardeningCli.test.ts +++ b/tests/acceptance/binaryHardeningCli.test.ts @@ -83,5 +83,4 @@ cliTest( expect(response.isError).toBe(true); expect(parseMcpToolError(response)).toEqual({ error: rejected.json }); }, - 60_000, ); diff --git a/tests/acceptance/browser/browserScenarioLaunchFailure.test.ts b/tests/acceptance/browser/browserScenarioLaunchFailure.test.ts index 3e220768f7..39f4486931 100644 --- a/tests/acceptance/browser/browserScenarioLaunchFailure.test.ts +++ b/tests/acceptance/browser/browserScenarioLaunchFailure.test.ts @@ -39,7 +39,6 @@ cliTest.skipIf(process.platform === "win32")( "--json", ], environment: { BROWSER_FIXTURE_SECRET: secret }, - timeoutMs: 20_000, }); expect(response.exitCode, response.stdout + response.stderr).toBe(1); expect(response.stdout).not.toContain(secret); diff --git a/tests/acceptance/browser/webBundleContainmentCli.test.ts b/tests/acceptance/browser/webBundleContainmentCli.test.ts index 59d6159072..d09e694f23 100644 --- a/tests/acceptance/browser/webBundleContainmentCli.test.ts +++ b/tests/acceptance/browser/webBundleContainmentCli.test.ts @@ -47,7 +47,6 @@ cliTest( "--total-analysis-timeout-ms", "1", ], - timeoutMs: 30_000, }); expect( interrupted.exitCode, @@ -74,7 +73,6 @@ cliTest( const recovered = await cli.run({ arguments: baseArguments, - timeoutMs: 30_000, }); expect(recovered.exitCode, recovered.stdout + recovered.stderr).toBe(0); expect(recovered.json).toMatchObject({ diff --git a/tests/acceptance/browser/webBundleSchemaCoverageCliMcp.test.ts b/tests/acceptance/browser/webBundleSchemaCoverageCliMcp.test.ts index 3990e8f490..69f4f3596a 100644 --- a/tests/acceptance/browser/webBundleSchemaCoverageCliMcp.test.ts +++ b/tests/acceptance/browser/webBundleSchemaCoverageCliMcp.test.ts @@ -131,5 +131,4 @@ cliTest( await browser.close(); } }, - 120_000, ); diff --git a/tests/acceptance/browserScenarioReferenceDiagnostics.test.ts b/tests/acceptance/browserScenarioReferenceDiagnostics.test.ts new file mode 100644 index 0000000000..8b97ba0cbf --- /dev/null +++ b/tests/acceptance/browserScenarioReferenceDiagnostics.test.ts @@ -0,0 +1,85 @@ +import { Client } from "@modelcontextprotocol/client"; +import { StdioClientTransport } from "@modelcontextprotocol/client/stdio"; +import { writeFile } from "node:fs/promises"; +import { join, resolve } from "node:path"; +import { expect } from "vitest"; +import { browserScenarioReferences } from "../fixtures/browserScenarioReferences.js"; +import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js"; +import { parseMcpToolError } from "../fixtures/mcpToolError.js"; +import { cliTest } from "../support/cli/cliFixture.js"; + +cliTest.for([ + ["CLI", "query"], + ["CLI", "storage"], + ["CLI", "session-storage"], + ["CLI", "cookie"], + ["stdio MCP", "query"], + ["stdio MCP", "storage"], + ["stdio MCP", "session-storage"], + ["stdio MCP", "cookie"], +] as const)( + "reports undeclared references through %s for a wide %s list", + async ([surface, placement], { cli }) => { + const home = await createTestTempDirectory("rea-scenario-reference-"); + const input = browserScenarioReferences(placement, 130_000); + input.browser.executable_path = join(home, "missing-chromium"); + input.secrets = []; + const expected = { + code: "invalid_request", + category: "invalid_input", + retryable: false, + details: { + operation: "capture_browser_scenario", + issues: expect.arrayContaining([ + expect.objectContaining({ + path: ["secrets"], + reason: "invalid_value", + message: "Secret reference token is not declared", + }), + ]), + }, + }; + if (surface === "CLI") { + const path = join(home, "scenario.json"); + await writeFile(path, JSON.stringify(input)); + // A real file argument avoids the host argv limit for this valid JSON. + const result = await cli.run({ + arguments: ["capture-browser-scenario", path, "--json"], + environment: { HOME: home, REA_LOG_LEVEL: "silent" }, + }); + expect(result.exitCode).toBe(1); + expect(result.json).toMatchObject(expected); + return; + } + const client = new Client({ + name: "scenario-reference-diagnostics", + version: "1", + }); + const transport = new StdioClientTransport({ + command: process.execPath, + args: [resolve("scripts/rea.mjs"), "mcp"], + cwd: process.cwd(), + env: { + PATH: process.env.PATH ?? "", + HOME: home, + REA_LOG_LEVEL: "silent", + // This workload explicitly selects admission above the 1024 default. + REA_MCP_MAX_INPUT_ELEMENTS: "1000000", + }, + stderr: "pipe", + }); + transport.stderr?.on("data", () => undefined); + try { + await client.connect(transport); + const result = await client.callTool({ + name: "capture_browser_scenario", + arguments: input, + }); + expect(result.isError).toBe(true); + expect(parseMcpToolError(result).error).toMatchObject(expected); + await expect(client.ping()).resolves.toEqual({}); + } finally { + await Promise.all([client.close(), transport.close()]); + } + }, +); diff --git a/tests/acceptance/bunPathBudgetCli.test.ts b/tests/acceptance/bunPathBudgetCli.test.ts index c2a69fd4d0..e8ece793ba 100644 --- a/tests/acceptance/bunPathBudgetCli.test.ts +++ b/tests/acceptance/bunPathBudgetCli.test.ts @@ -56,4 +56,4 @@ for (const shared of [false, true]) )?.evidence.normalized_result; expect(graph).toMatchObject({ manifest: { occurrence_count: 1001 } }); } - }, 120_000); + }); diff --git a/tests/acceptance/cliBuiltinDocumentTokens.test.ts b/tests/acceptance/cliBuiltinDocumentTokens.test.ts index e84901de20..97669593ab 100644 --- a/tests/acceptance/cliBuiltinDocumentTokens.test.ts +++ b/tests/acceptance/cliBuiltinDocumentTokens.test.ts @@ -2,7 +2,6 @@ import { estimateTokenCount } from "tokenx"; import { expect } from "vitest"; import { workspaceCliTest } from "../support/cli/workspaceCliFixture.js"; -import { runFileCli } from "../support/cli/cliFixture.js"; workspaceCliTest( "applies token controls to SDK manifest and schema documents", @@ -12,12 +11,15 @@ workspaceCliTest( const freshTokenCount = arguments_.join(" ") === "--llms --token-count"; const freshTokenLimit = arguments_.join(" ") === "--llms-full --token-limit 1"; - const result = await (!freshTokenCount && - !freshTokenLimit && - (arguments_.includes("--token-count") || - arguments_.includes("--token-limit")) - ? runFileCli(cli, invocation) - : cli.run(invocation)); + const result = await cli.run({ + ...invocation, + freshProcess: !( + !freshTokenCount && + !freshTokenLimit && + (arguments_.includes("--token-count") || + arguments_.includes("--token-limit")) + ), + }); expect(result.exitCode, result.stderr).toBe(0); expect(result.stderr).toBe(""); return result.stdout; diff --git a/tests/acceptance/dotnetBundleJourney.test.ts b/tests/acceptance/dotnetBundleJourney.test.ts index 7a48589d15..f448357943 100644 --- a/tests/acceptance/dotnetBundleJourney.test.ts +++ b/tests/acceptance/dotnetBundleJourney.test.ts @@ -4,7 +4,7 @@ import { join, resolve } from "node:path"; import { Client } from "@modelcontextprotocol/client"; import { StdioClientTransport } from "@modelcontextprotocol/client/stdio"; -import { expect } from "vitest"; +import { afterAll, expect } from "vitest"; import { dotnetBundleFixture } from "../../src/artifacts/dotnet/DotnetBundle.fixture.js"; import { cArchiveExecutable } from "../../src/artifacts/pyinstaller/CArchive.fixture.js"; @@ -16,6 +16,11 @@ import { artifactInventorySummaryViewResultSchema, } from "../../src/domain/analysisView/analysisView.js"; import { workspaceCliTest as test } from "../support/cli/workspaceCliFixture.js"; +import { createMcpSchemaValidation } from "../fixtures/mcpSchemaValidation.js"; + +// Compile SDK output validators for the tools a test calls, not the whole catalog. +const schemaValidation = createMcpSchemaValidation(); +afterAll(() => schemaValidation.clear()); const checkProjectedMember = async ( client: Client, @@ -125,10 +130,13 @@ for (const format of ["dotnet-bundle", "pyinstaller"] as const) { GHIDRA_INSTALL_DIR: workspace.path("unavailable-ghidra"), }; const outputs: string[] = []; - const client = new Client({ - name: "rea-dotnet-bundle-acceptance", - version: "1", - }); + const client = new Client( + { + name: "rea-dotnet-bundle-acceptance", + version: "1", + }, + { jsonSchemaValidator: schemaValidation }, + ); const transport = new StdioClientTransport({ command: process.execPath, args: [resolve("scripts/rea.mjs"), "mcp"], @@ -219,7 +227,7 @@ for (const format of ["dotnet-bundle", "pyinstaller"] as const) { outputs.map((output) => rm(output, { recursive: true, force: true })), ); } - }, 120_000); + }); } test("reports excessive bundle path depth as a public CLI limit", async ({ diff --git a/tests/acceptance/elfNoteCoreProfile.test.ts b/tests/acceptance/elfNoteCoreProfile.test.ts index bb91121501..6d36d98d48 100644 --- a/tests/acceptance/elfNoteCoreProfile.test.ts +++ b/tests/acceptance/elfNoteCoreProfile.test.ts @@ -36,7 +36,7 @@ cliTest.for([ bytes[8] = version; await writeFile(path, bytes); const hash = createHash("sha256").update(bytes).digest("hex"); - const output = await cli.sharedCliRun!({ + const output = await cli.run({ arguments: ["inspect-elf-notes", path, "--json"], }); expect(output.exitCode, output.stderr).toBe(0); diff --git a/tests/acceptance/elfNotesAbiEndian.test.ts b/tests/acceptance/elfNotesAbiEndian.test.ts index 819b11051b..c971da4ea2 100644 --- a/tests/acceptance/elfNotesAbiEndian.test.ts +++ b/tests/acceptance/elfNotesAbiEndian.test.ts @@ -51,7 +51,7 @@ for (const { bits, ownership, machine } of [ }); await writeFile(path, bytes); - const output = await cli.sharedCliRun!({ + const output = await cli.run({ arguments: ["inspect-elf-notes", path, "--json"], }); expect(output.exitCode, output.stderr).toBe(0); diff --git a/tests/acceptance/elfNotesAndroid.test.ts b/tests/acceptance/elfNotesAndroid.test.ts index 34289625e4..ae9f37cff1 100644 --- a/tests/acceptance/elfNotesAndroid.test.ts +++ b/tests/acceptance/elfNotesAndroid.test.ts @@ -65,7 +65,7 @@ for (const target of [ }), ); - const cliOut = await cli.sharedCliRun!({ + const cliOut = await cli.run({ arguments: ["inspect-elf-notes", path, "--json"], }); expect(cliOut.exitCode, cliOut.stderr).toBe(0); diff --git a/tests/acceptance/elfNotesCore.test.ts b/tests/acceptance/elfNotesCore.test.ts index 7f9076cd25..d2c7fadcd5 100644 --- a/tests/acceptance/elfNotesCore.test.ts +++ b/tests/acceptance/elfNotesCore.test.ts @@ -152,7 +152,7 @@ cliTest( }); await writeFile(path, bytes); - const output = await cli.sharedCliRun!({ + const output = await cli.run({ arguments: ["inspect-elf-notes", path, "--json"], }); expect(output.exitCode, output.stderr).toBe(0); diff --git a/tests/acceptance/goBinaryCli.test.ts b/tests/acceptance/goBinaryCli.test.ts index e1d0fc6156..63a8296e7a 100644 --- a/tests/acceptance/goBinaryCli.test.ts +++ b/tests/acceptance/goBinaryCli.test.ts @@ -21,7 +21,7 @@ import { } from "../fixtures/go/image.js"; import { createGoRodataStringFixture } from "../fixtures/go/rodataStringImage.js"; import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js"; -import { cliTest, runFileCli } from "../support/cli/cliFixture.js"; +import { cliTest } from "../support/cli/cliFixture.js"; import { parseEvidence } from "../../src/domain/evidence.js"; import { connectLocalToolsMcp } from "../fixtures/localToolsMcp.js"; import { expectCliMcpEvidenceParity } from "../fixtures/cliMcpEvidenceParity.js"; @@ -47,7 +47,7 @@ cliTest( ] as const) { const path = join(root, `${item.name}.macho`); await writeFile(path, item.bytes); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: ["inspect-go-binary", path, "--json"], }); const response = await call("inspect_go_binary", { path }); @@ -79,7 +79,6 @@ cliTest( } await client.ping(); }, - 60000, ); cliTest( @@ -88,6 +87,13 @@ cliTest( const root = await createTestTempDirectory("rea-go-inline-budget-"); const { client, call } = await connectLocalToolsMcp(); const maximum = 1024 * 1024; + const impossibleInline = createGoBinaryFixture(); + impossibleInline.bytes.set( + [0x81, 0x80, 0x40], + impossibleInline.headerOffset + 32, + ); + const impossiblePointer = createGoBinaryFixture({ encoding: "pointer" }); + impossiblePointer.bytes.writeBigUInt64LE(BigInt(maximum + 1), 1544); const cases = [ { name: "exact", @@ -104,6 +110,16 @@ cliTest( fixture: createGoInlineAggregateFixture(512), outcome: "malformed", }, + { + name: "impossible-inline", + fixture: impossibleInline, + outcome: "malformed", + }, + { + name: "impossible-pointer", + fixture: impossiblePointer, + outcome: "malformed", + }, ] as const; for (const item of cases) { @@ -114,7 +130,7 @@ cliTest( ); const path = join(root, `${item.name}.elf`); await writeFile(path, item.fixture.bytes); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: ["inspect-go-binary", path, "--json"], }); const response = await call("inspect_go_binary", { path }); @@ -174,7 +190,6 @@ cliTest( } await client.ping(); }, - 60000, ); cliTest( @@ -184,12 +199,12 @@ cliTest( const path = join(root, "application.elf"); const output = join(root, "source.json"); await writeFile(path, createGoBinaryFixture().bytes); - const complete = await runFileCli(cli, { + const complete = await cli.run({ arguments: ["inspect-go-binary", path, "--json"], }); expect(complete.exitCode, complete.stderr).toBe(0); const full = evidenceResultOf(goBinarySchema).parse(complete.json); - const summary = await runFileCli(cli, { + const summary = await cli.run({ arguments: [ "inspect-go-binary", path, @@ -218,7 +233,7 @@ cliTest( .map((record) => parseEvidence(record)) .find((record) => record.evidence_id === full.evidence_id), ).toEqual(full); - const refused = await runFileCli(cli, { + const refused = await cli.run({ arguments: [ "inspect-go-binary", join(root, "missing"), @@ -233,7 +248,6 @@ cliTest( details: { issues: [{ path: ["evidence_output"] }] }, }); }, - 30000, ); cliTest( @@ -242,7 +256,7 @@ cliTest( const root = await createTestTempDirectory("rea-go-overlap-"); const path = join(root, "overlapping.elf"); await writeFile(path, createGoOverlappingElfFixture()); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: ["inspect-go-binary", path, "--json"], }); expect(result.exitCode).toBe(1); @@ -263,7 +277,6 @@ cliTest( expect(parseMcpToolError(response).error).toMatchObject(expected); await client.ping(); }, - 30000, ); cliTest( @@ -276,7 +289,7 @@ cliTest( const fixture = createGoSplitElfFixture({ sectionless }); const path = join(root, sectionless ? "sectionless.elf" : "original.elf"); await writeFile(path, fixture.bytes); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: ["inspect-go-binary", path, "--json"], }); expect(result.exitCode, result.stderr).toBe(0); @@ -290,7 +303,6 @@ cliTest( expect(infos[1]).toEqual(infos[0]); await client.ping(); }, - 30000, ); for (const bits of [32, 64] as const) { @@ -312,7 +324,7 @@ for (const bits of [32, 64] as const) { ] as const) { const path = join(root, `${label}.exe`); await writeFile(path, bytes); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: ["inspect-go-binary", path, "--json"], }); expect(result.exitCode, result.stderr).toBe(0); @@ -340,7 +352,6 @@ for (const bits of [32, 64] as const) { expect(buildInfos[1]).toEqual(buildInfos[0]); await client.ping(); }, - 30000, ); } } @@ -368,7 +379,7 @@ cliTest( path, machoImageWithUnknownCommand(fixture.wide, fixture.size), ); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: ["inspect-go-binary", path, "--json"], }); const response = await call("inspect_go_binary", { path }); @@ -396,7 +407,6 @@ cliTest( } await client.ping(); }, - 30000, ); for (const expectedErrno of ["ELOOP", "ENAMETOOLONG"] as const) { @@ -408,7 +418,7 @@ for (const expectedErrno of ["ELOOP", "ENAMETOOLONG"] as const) { root, expectedErrno, ); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: ["inspect-go-binary", path, "--json"], }); expect(result.exitCode).toBe(1); @@ -439,7 +449,7 @@ cliTest( const path = join(root, "byte-strings.elf"); const fixture = createGoBinaryByteStringFixture(); await writeFile(path, fixture.bytes); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: ["inspect-go-binary", path, "--json"], }); expect(result.exitCode, result.stderr).toBe(0); @@ -457,7 +467,7 @@ for (const fixture of createGoBinaryDiagnosticFixtures()) { const root = await createTestTempDirectory("rea-go-cli-mz-"); const path = join(root, `${fixture.name}.exe`); await writeFile(path, fixture.bytes); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: ["inspect-go-binary", path, "--json"], }); expect(result.exitCode).toBe(1); @@ -478,7 +488,7 @@ cliTest( const root = await createTestTempDirectory("rea-go-cli-"); const path = join(root, "text.bin"); await writeFile(path, "This is not an executable container."); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: ["inspect-go-binary", path, "--json"], }); expect(result.exitCode).toBe(1); @@ -498,7 +508,7 @@ for (const format of ["elf", "pe", "macho"] as const) { const fixture = createGoBinaryFixture({ format, encoding }); const path = join(root, "application.bin"); await writeFile(path, fixture.bytes); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: ["inspect-go-binary", "application.bin", "--json"], cwd: root, }); @@ -572,7 +582,7 @@ cliTest("preserves MIPS identity in Go CLI Evidence", async ({ cli }) => { fixture.bytes.writeUInt16LE(8, 18); // ELF EM_MIPS. const path = join(root, "application.elf"); await writeFile(path, fixture.bytes); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: ["inspect-go-binary", path, "--json"], }); expect(result.exitCode, result.stderr).toBe(0); @@ -591,7 +601,7 @@ cliTest( const root = await createTestTempDirectory("rea-go-cli-"); const path = join(root, "metadata-free.elf"); await writeFile(path, createGoBinaryFixture({ omitBuildInfo: true }).bytes); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: ["inspect-go-binary", path, "--json"], }); expect(result.exitCode).toBe(0); @@ -615,7 +625,7 @@ cliTest( const root = await createTestTempDirectory("rea-go-gopath-cli-"); const path = join(root, "application.elf"); await writeFile(path, createGoBinaryFixture({ moduleText }).bytes); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: ["inspect-go-binary", path, "--json"], }); expect(result.exitCode, result.stderr).toBe(0); @@ -652,7 +662,7 @@ cliTest( }); await writeFile(path, fixture.bytes); - const standaloneCap = await runFileCli(cli, { + const standaloneCap = await cli.run({ arguments: [ "inspect-go-binary", path, @@ -670,7 +680,7 @@ cliTest( }, }); - const cliResult = await runFileCli(cli, { + const cliResult = await cli.run({ arguments: [ "inspect-go-binary", path, @@ -700,5 +710,4 @@ cliTest( expectCliMcpEvidenceParity(evidence, mcpReply.structuredContent, path); await client.ping(); }, - 60000, ); diff --git a/tests/acceptance/goBinaryP32PointerCli.test.ts b/tests/acceptance/goBinaryP32PointerCli.test.ts index e5b87b5391..632bb35f44 100644 --- a/tests/acceptance/goBinaryP32PointerCli.test.ts +++ b/tests/acceptance/goBinaryP32PointerCli.test.ts @@ -5,7 +5,7 @@ import { evidenceResultOf } from "../../src/contracts/toolOutputSchemaPrimitives import { goBinarySchema } from "../../src/domain/go/goBinary.js"; import { createGoBinaryFixture, GO_MODULE_TEXT } from "../fixtures/go/image.js"; import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js"; -import { cliTest, runFileCli } from "../support/cli/cliFixture.js"; +import { cliTest } from "../support/cli/cliFixture.js"; cliTest( "inspects ELF64 Go metadata with four-byte amd64p32 pointers through CLI", @@ -20,7 +20,7 @@ cliTest( }); const path = join(root, "nacl-amd64p32.nexe"); await writeFile(path, fixture.bytes); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: ["inspect-go-binary", path, "--json"], }); diff --git a/tests/acceptance/goBinaryPointerMappingsCli.test.ts b/tests/acceptance/goBinaryPointerMappingsCli.test.ts index cc858ccbe8..92a293e0d2 100644 --- a/tests/acceptance/goBinaryPointerMappingsCli.test.ts +++ b/tests/acceptance/goBinaryPointerMappingsCli.test.ts @@ -12,7 +12,7 @@ import { connectLocalToolsMcp } from "../fixtures/localToolsMcp.js"; import { expectCliMcpEvidenceParity } from "../fixtures/cliMcpEvidenceParity.js"; import { parseMcpToolError } from "../fixtures/mcpToolError.js"; import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js"; -import { cliTest, runFileCli } from "../support/cli/cliFixture.js"; +import { cliTest } from "../support/cli/cliFixture.js"; cliTest( "retains header-backed PE and cross-load ELF pointer metadata through CLI and MCP", @@ -34,7 +34,7 @@ cliTest( for (const { name, fixture } of cases) { const path = join(root, name); await writeFile(path, fixture.bytes); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: ["inspect-go-binary", path, "--json"], }); expect(result.exitCode, result.stderr).toBe(0); @@ -64,7 +64,7 @@ cliTest( damaged.bytes.writeBigUInt64LE(0x11001n, 200); // A one-byte virtual gap. const path = join(root, "elf-cross-load-gap"); await writeFile(path, damaged.bytes); - const rejected = await runFileCli(cli, { + const rejected = await cli.run({ arguments: ["inspect-go-binary", path, "--json"], }); expect(rejected.exitCode).toBe(1); @@ -76,5 +76,4 @@ cliTest( expect(parseMcpToolError(response)).toEqual({ error: rejected.json }); await client.ping(); }, - 60000, ); diff --git a/tests/acceptance/godotPck.test.ts b/tests/acceptance/godotPck.test.ts index 0e69243b97..bb0d56e9f9 100644 --- a/tests/acceptance/godotPck.test.ts +++ b/tests/acceptance/godotPck.test.ts @@ -383,7 +383,6 @@ cliTest( }), ]); }, - 30000, ); cliTest( @@ -424,5 +423,4 @@ cliTest( } } }, - 30000, ); diff --git a/tests/acceptance/gpuCodeObjectsCli.test.ts b/tests/acceptance/gpuCodeObjectsCli.test.ts index a5f952775c..2967daf08f 100644 --- a/tests/acceptance/gpuCodeObjectsCli.test.ts +++ b/tests/acceptance/gpuCodeObjectsCli.test.ts @@ -13,7 +13,7 @@ import { parseEvidence, createEvidence } from "../../src/domain/evidence.js"; import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js"; import { connectLocalToolsMcp } from "../fixtures/localToolsMcp.js"; import { parseMcpToolError } from "../fixtures/mcpToolError.js"; -import { cliTest, runFileCli } from "../support/cli/cliFixture.js"; +import { cliTest } from "../support/cli/cliFixture.js"; import { cudaFixture, peGpuFixture, @@ -38,7 +38,7 @@ cliTest( ] as const) { const path = join(root, name); await writeFile(path, bytes); - const response = await runFileCli(cli, { + const response = await cli.run({ arguments: [ "inspect-gpu-code-objects", path, @@ -76,7 +76,7 @@ cliTest( const bytes = cubinFixture({ name: "k".repeat(65_536) }); const path = join(root, "long-symbol.cubin"); await writeFile(path, bytes); - const response = await runFileCli(cli, { + const response = await cli.run({ arguments: [ "inspect-gpu-code-objects", path, @@ -124,7 +124,7 @@ cliTest( const bytes = Buffer.concat([header, compressed]), path = join(root, `v${version}`); await writeFile(path, bytes); - const response = await runFileCli(cli, { + const response = await cli.run({ arguments: [ "inspect-gpu-code-objects", path, @@ -183,7 +183,7 @@ cliTest( const bytes = hipFixture(payload, bundleId), path = join(root, name); await writeFile(path, bytes); - const response = await runFileCli(cli, { + const response = await cli.run({ arguments: [ "inspect-gpu-code-objects", path, @@ -233,7 +233,7 @@ cliTest( } const path = join(root, "raw-bitcode"); await writeFile(path, bitcode); - const raw = await runFileCli(cli, { + const raw = await cli.run({ arguments: [ "inspect-gpu-code-objects", path, @@ -271,7 +271,7 @@ cliTest( ]), path = join(root, name); await writeFile(path, bytes); - const response = await runFileCli(cli, { + const response = await cli.run({ arguments: [ "inspect-gpu-code-objects", path, @@ -355,7 +355,7 @@ cliTest( ]), path = join(root, name); await writeFile(path, bytes); - const response = await runFileCli(cli, { + const response = await cli.run({ arguments: [ "inspect-gpu-code-objects", path, @@ -396,7 +396,7 @@ cliTest( ]), path = join(root, "large-parameter.cubin"); await writeFile(path, bytes); - const response = await runFileCli(cli, { + const response = await cli.run({ arguments: [ "inspect-gpu-code-objects", path, @@ -433,7 +433,7 @@ cliTest( const bytes = hipFixture(payload, "hip-amdgcn-amd-amdhsa--gfx906+xnack"), path = join(root, "feature.bundle"); await writeFile(path, bytes); - const response = await runFileCli(cli, { + const response = await cli.run({ arguments: [ "inspect-gpu-code-objects", path, @@ -478,7 +478,7 @@ cliTest( root = await createTestTempDirectory("rea-ptx-callprototype-"), path = join(root, "prototype.ptx"); await writeFile(path, source); - const response = await runFileCli(cli, { + const response = await cli.run({ arguments: [ "inspect-gpu-code-objects", path, @@ -528,7 +528,7 @@ cliTest( const bytes = cubinFixture({ extendedSectionIndex: true }); const path = join(root, "extended.cubin"); await writeFile(path, bytes); - const response = await runFileCli(cli, { + const response = await cli.run({ arguments: [ "inspect-gpu-code-objects", path, @@ -576,7 +576,7 @@ cliTest( malformedBytes.writeBigUInt64LE(4n, sectionTable + indexTable! * 64 + 32); const malformedPath = join(root, "short-shndx.cubin"); await writeFile(malformedPath, malformedBytes); - const malformed = await runFileCli(cli, { + const malformed = await cli.run({ arguments: [ "inspect-gpu-code-objects", malformedPath, @@ -607,7 +607,7 @@ cliTest( const text = `.version 7.0\n.target sm_80\n.address_size 64\n.visible .entry kernel() {\n.reg .b32 ${register};\nmov.u32 ${register}, 0;\nret;\n}\n`; const path = join(root, `${register.slice(1)}.ptx`); await writeFile(path, text); - const response = await runFileCli(cli, { + const response = await cli.run({ arguments: [ "inspect-gpu-code-objects", path, @@ -644,7 +644,7 @@ cliTest( const text = `.version 8.8\n.target sm_80\n.address_size 64\n.entry %kernel(.param .u32 input) {\nret;\n}\n`; const path = join(root, "identifiers.ptx"); await writeFile(path, text); - const response = await runFileCli(cli, { + const response = await cli.run({ arguments: [ "inspect-gpu-code-objects", path, @@ -707,7 +707,7 @@ cliTest( const text = `${preamble}${declaration}\n`, path = join(root, `${name}.ptx`); await writeFile(path, text); - const response = await runFileCli(cli, { + const response = await cli.run({ arguments: [ "inspect-gpu-code-objects", path, @@ -757,7 +757,7 @@ cliTest( const text = `.version 8.8\n.target compute_80\n.address_size 64\n.entry kernel() {\nret;\n}\n`; const path = join(root, "target.ptx"); await writeFile(path, text); - const response = await runFileCli(cli, { + const response = await cli.run({ arguments: [ "inspect-gpu-code-objects", path, @@ -787,7 +787,7 @@ cliTest( const text = `.version 1.3\n.target sm_80\n.address_size 64\n.entry kernel {\n.param .u32 input;\nret;\n}\n`; const path = join(root, "legacy.ptx"); await writeFile(path, text); - const response = await runFileCli(cli, { + const response = await cli.run({ arguments: [ "inspect-gpu-code-objects", path, @@ -826,7 +826,7 @@ cliTest( malformedPath, `.version 1.3\n.target sm_80\n.entry kernel {\n.param .u32 input\n`, ); - const malformed = await runFileCli(cli, { + const malformed = await cli.run({ arguments: [ "inspect-gpu-code-objects", malformedPath, @@ -853,7 +853,7 @@ cliTest( "attributes malformed selector JSON to its actual CLI option", async ({ cli }) => { for (const name of ["object_ids", "targets", "view"]) { - const response = await runFileCli(cli, { + const response = await cli.run({ arguments: [ "inspect-gpu-code-objects", "/not-opened-for-invalid-selectors", @@ -893,7 +893,7 @@ cliTest( }, }); // PowerShell's Get-FileHash prints the digest in upper case. - const response = await runFileCli(cli, { + const response = await cli.run({ arguments: [ "inspect-gpu-code-objects", "/not-opened-for-invalid-selectors", @@ -1339,7 +1339,7 @@ cliTest( ] as const) { const path = join(root, name); await writeFile(path, bytes); - const response = await runFileCli(cli, { + const response = await cli.run({ arguments: [ "inspect-gpu-code-objects", path, diff --git a/tests/acceptance/harmonyArtifactJourney.test.ts b/tests/acceptance/harmonyArtifactJourney.test.ts index 2b383e292c..571e2cd27c 100644 --- a/tests/acceptance/harmonyArtifactJourney.test.ts +++ b/tests/acceptance/harmonyArtifactJourney.test.ts @@ -234,4 +234,4 @@ test("preserves Harmony archive formats through the public CLI/MCP lifecycle", a } finally { await client.close(); } -}, 120_000); +}); diff --git a/tests/acceptance/il2cppMetadataCli.test.ts b/tests/acceptance/il2cppMetadataCli.test.ts index 595968ad3b..a90187d1ed 100644 --- a/tests/acceptance/il2cppMetadataCli.test.ts +++ b/tests/acceptance/il2cppMetadataCli.test.ts @@ -44,7 +44,7 @@ import { import { connectLocalToolsMcp } from "../fixtures/localToolsMcp.js"; import { parseMcpToolError } from "../fixtures/mcpToolError.js"; import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js"; -import { cliTest, runFileCli } from "../support/cli/cliFixture.js"; +import { cliTest } from "../support/cli/cliFixture.js"; const parseCliMetadata = (value: unknown) => il2cppMetadataSchema.parse( @@ -75,7 +75,7 @@ cliTest( } strings.copy(bytes, 500); await writeFile(path, bytes); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: ["inspect-il2cpp-metadata", path, "--json"], }); expect(result.exitCode).toBe(1); @@ -109,7 +109,6 @@ cliTest( }); await client.ping(); }, - 30000, ); cliTest( @@ -123,7 +122,7 @@ cliTest( createLinkedIl2cppMetadataFixture({ version: 39 }).bytes, ); await writeFile(binaryPath, createIl2cppBinaryFixture()); - const produced = await runFileCli(cli, { + const produced = await cli.run({ arguments: [ "inspect-il2cpp-metadata", path, @@ -162,7 +161,7 @@ cliTest( }; const query = join(root, `image-${image}.json`); await writeFile(query, JSON.stringify(request)); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: ["inspect-analysis-view", query, "--json"], }); const mcp = await call("inspect_analysis_view", request); @@ -176,7 +175,6 @@ cliTest( } } }, - 30000, ); cliTest( @@ -224,7 +222,7 @@ cliTest( }; const query = join(root, `${scenario}.json`); await writeFile(query, JSON.stringify(request)); - const cliResult = await runFileCli(cli, { + const cliResult = await cli.run({ arguments: ["inspect-analysis-view", query, "--json"], }); const mcp = await call("inspect_analysis_view", request); @@ -238,7 +236,6 @@ cliTest( } } }, - 30000, ); cliTest( @@ -252,7 +249,7 @@ cliTest( }); const path = join(root, `valid-${count}.dat`); await writeFile(path, bytes); - const output = await runFileCli(cli, { + const output = await cli.run({ arguments: ["inspect-il2cpp-metadata", path, "--json"], }); expect(output.exitCode, output.stderr).toBe(0); @@ -285,7 +282,7 @@ cliTest( ); const path = join(root, `invalid-${declaredCount}-${declaredBytes}.dat`); await writeFile(path, fixture.bytes); - const output = await runFileCli(cli, { + const output = await cli.run({ arguments: ["inspect-il2cpp-metadata", path, "--json"], }); expect(output.exitCode).toBe(1); @@ -312,7 +309,6 @@ cliTest( expect(Buffer.compare(await readFile(path), fixture.bytes)).toBe(0); } }, - 30000, ); cliTest( @@ -325,7 +321,7 @@ cliTest( propertiesAndEvents: true, }); await writeFile(path, bytes); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: ["inspect-il2cpp-metadata", path, "--json"], }); expect(result.exitCode, result.stderr).toBe(0); @@ -431,7 +427,6 @@ cliTest( } await client.ping(); }, - 30000, ); cliTest( @@ -477,7 +472,7 @@ cliTest( ] as const) { const path = join(root, name); await writeFile(path, bytes); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: ["inspect-il2cpp-metadata", path, "--json"], }); expect(result.exitCode).toBe(1); @@ -487,7 +482,6 @@ cliTest( } await client.ping(); }, - 30000, ); cliTest( @@ -527,7 +521,7 @@ cliTest( }, }, }; - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: ["inspect-il2cpp-metadata", path, "--json"], }); expect(result.exitCode).toBe(1); @@ -562,7 +556,6 @@ cliTest( } await client.ping(); }, - 30000, ); const metadataPageReader = @@ -750,7 +743,7 @@ it("queries compact retained metadata through the real SDK with exact row identi }); expect(parseMcpToolError(invalid).error.code).toBe("invalid_request"); await client.ping(); -}, 30000); +}); it("imports historical metadata Evidence with unavailable member collections without inventing empty results", async () => { const root = await createTestTempDirectory("rea-il2cpp-history-"), @@ -827,7 +820,7 @@ it("imports historical metadata Evidence with unavailable member collections wit view: { kind: "il2cpp-metadata", collection: "parameters" }, }); expect(parseMcpToolError(unavailable).error.code).toBe("invalid_request"); -}, 30000); +}); cliTest( "exports complete metadata Evidence for compact CLI delivery and portable selected reads", @@ -835,7 +828,7 @@ cliTest( const root = await createTestTempDirectory("rea-il2cpp-cli-export-"); const path = join(root, "global-metadata.dat"); const bundle = join(root, "evidence.json"); - const refused = await runFileCli(cli, { + const refused = await cli.run({ arguments: [ "inspect-il2cpp-metadata", join(root, "must-not-be-read"), @@ -859,14 +852,14 @@ cliTest( bundle, "--json", ]; - const result = await runFileCli(cli, { arguments: args }); + const result = await cli.run({ arguments: args }); expect(result.exitCode, result.stderr).toBe(0); expect(result.json).toMatchObject({ evidence_bundle: { path: bundle, bytes: expect.any(Number) }, summary: { normalized_result: { kind: "summary" } }, }); const before = await readFile(bundle); - const overwrite = await runFileCli(cli, { arguments: args }); + const overwrite = await cli.run({ arguments: args }); expect(overwrite.exitCode).toBe(1); expect(Buffer.compare(await readFile(bundle), before)).toBe(0); const saved = await readEvidenceBundle(bundle); @@ -894,7 +887,7 @@ cliTest( }, }), ); - const selected = await runFileCli(cli, { + const selected = await cli.run({ arguments: ["inspect-analysis-view", query, "--json"], }); expect(selected.exitCode, selected.stderr).toBe(0); @@ -906,7 +899,6 @@ cliTest( parent_evidence_id: evidence.evidence_id, }); }, - 30000, ); cliTest.skipIf(process.platform === "win32")( @@ -921,7 +913,7 @@ cliTest.skipIf(process.platform === "win32")( const decoy = join(root, "evidence.json"); await writeFile(decoy, "unrelated caller data"); const selected = `${root}/link/../evidence.json`; - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: [ "inspect-il2cpp-metadata", path, @@ -940,7 +932,7 @@ cliTest.skipIf(process.platform === "win32")( expect( JSON.parse(await readFile(join(actual, "evidence.json"), "utf8")), ).toEqual(saved.value); - const read = await runFileCli(cli, { + const read = await cli.run({ arguments: [ "inspect-evidence-bundle", JSON.stringify({ path: selected, detail: "summary" }), @@ -950,7 +942,6 @@ cliTest.skipIf(process.platform === "win32")( expect(read.exitCode, read.stderr).toBe(0); expect(read.stdout).toContain(saved.value.records[0]?.evidence_id); }, - 30000, ); cliTest( @@ -975,7 +966,7 @@ cliTest( rva: `0x${(address - FIXTURE_BINARY_BASE).toString(16)}`, }, ); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: [ "inspect-il2cpp-metadata", path, @@ -1042,7 +1033,6 @@ cliTest( ); await client.ping(); }, - 30000, ); cliTest( @@ -1054,7 +1044,7 @@ cliTest( await writeFile(path, createLinkedIl2cppMetadataFixture().bytes); await writeFile(binaryPath, createIl2cppDyldBindFixture(0n)); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: [ "inspect-il2cpp-metadata", path, @@ -1088,7 +1078,6 @@ cliTest( ).toEqual(report.method_addresses); await client.ping(); }, - 30000, ); cliTest( @@ -1100,7 +1089,7 @@ cliTest( await writeFile(path, createLinkedIl2cppMetadataFixture().bytes); const inspect = async (bytes: Buffer) => { await writeFile(binaryPath, bytes); - const output = await runFileCli(cli, { + const output = await cli.run({ arguments: [ "inspect-il2cpp-metadata", path, @@ -1157,7 +1146,6 @@ cliTest( } await client.ping(); }, - 30000, ); cliTest( @@ -1174,7 +1162,7 @@ cliTest( bytes.fill(0xff, 0x604, 0x608); const binaryPath = join(root, `registration-${tailBytes}.dylib`); await writeFile(binaryPath, bytes); - const output = await runFileCli(cli, { + const output = await cli.run({ arguments: [ "inspect-il2cpp-metadata", path, @@ -1218,7 +1206,6 @@ cliTest( } await client.ping(); }, - 30000, ); cliTest( @@ -1265,7 +1252,7 @@ cliTest( const binaryPath = join(root, `${entry.name}.dylib`); await writeFile(path, entry.metadata.bytes); await writeFile(binaryPath, entry.binary); - const cliResult = await runFileCli(cli, { + const cliResult = await cli.run({ arguments: [ "inspect-il2cpp-metadata", path, @@ -1301,7 +1288,6 @@ cliTest( } await client.ping(); }, - 30000, ); cliTest( @@ -1321,7 +1307,7 @@ cliTest( { machine: 62 }, ), ); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: [ "inspect-il2cpp-metadata", path, @@ -1339,7 +1325,6 @@ cliTest( ), ).toBe(true); }, - 30000, ); cliTest( @@ -1353,7 +1338,7 @@ cliTest( await writeFile(path, createLinkedIl2cppMetadataFixture().bytes); await writeFile(binaryPath, createElfFixture(createIl2cppBinaryFixture())); const run = (output: string, extra: readonly string[] = []) => - runFileCli(cli, { + cli.run({ arguments: [ "inspect-il2cpp-metadata", path, @@ -1408,7 +1393,7 @@ cliTest( }); expect(await readFile(seeds, "utf8")).toBe("kept\n"); - const unlinked = await runFileCli(cli, { + const unlinked = await cli.run({ arguments: [ "inspect-il2cpp-metadata", path, @@ -1447,7 +1432,7 @@ cliTest( binaryPath, createElfFixture(createIl2cppBinaryFixture({ genericMethods: {} })), ); - const linked = await runFileCli(cli, { + const linked = await cli.run({ arguments: [ "inspect-il2cpp-metadata", path, @@ -1473,7 +1458,7 @@ cliTest( ].join("\n"), ); - const unlinked = await runFileCli(cli, { + const unlinked = await cli.run({ arguments: [ "inspect-il2cpp-metadata", path, @@ -1523,7 +1508,7 @@ cliTest( const inspect = async (binary: Buffer) => { await writeFile(binaryPath, binary); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: [ "inspect-il2cpp-metadata", path, @@ -1566,7 +1551,7 @@ cliTest.skipIf(process.platform === "win32")( await writeFile(path, createLinkedIl2cppMetadataFixture().bytes); await writeFile(binaryPath, createElfFixture(createIl2cppBinaryFixture())); await writeFile(parent, "unchanged"); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: [ "inspect-il2cpp-metadata", path, @@ -1653,5 +1638,4 @@ cliTest( ).toEqual(entry.methods); } }, - 60000, ); diff --git a/tests/acceptance/il2cppStructuralExtentsCli.test.ts b/tests/acceptance/il2cppStructuralExtentsCli.test.ts index d2754a2c15..dea00a124b 100644 --- a/tests/acceptance/il2cppStructuralExtentsCli.test.ts +++ b/tests/acceptance/il2cppStructuralExtentsCli.test.ts @@ -5,7 +5,7 @@ import { createIl2cppMetadataFixture } from "../fixtures/il2cpp/metadata.js"; import { connectLocalToolsMcp } from "../fixtures/localToolsMcp.js"; import { parseMcpToolError } from "../fixtures/mcpToolError.js"; import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js"; -import { cliTest, runFileCli } from "../support/cli/cliFixture.js"; +import { cliTest } from "../support/cli/cliFixture.js"; cliTest( "rejects partial IL2CPP table extents through CLI and MCP without a binary", @@ -24,7 +24,7 @@ cliTest( }); const path = join(root, `${version}-${name}.dat`); await writeFile(path, fixture.bytes); - const intact = await runFileCli(cli, { + const intact = await cli.run({ arguments: ["inspect-il2cpp-metadata", path, "--json"], }); expect(intact.exitCode, intact.stderr).toBe(0); @@ -35,7 +35,7 @@ cliTest( fixture.header(name, "size"), ); await writeFile(path, damaged); - const invalid = await runFileCli(cli, { + const invalid = await cli.run({ arguments: ["inspect-il2cpp-metadata", path, "--json"], }); const expected = { @@ -57,5 +57,4 @@ cliTest( expect(parseMcpToolError(result).error).toMatchObject(expected); } }, - 30000, ); diff --git a/tests/acceptance/javascriptAsyncChunkBudget.test.ts b/tests/acceptance/javascriptAsyncChunkBudget.test.ts new file mode 100644 index 0000000000..4e095d3a5d --- /dev/null +++ b/tests/acceptance/javascriptAsyncChunkBudget.test.ts @@ -0,0 +1,68 @@ +import { writeFile } from "node:fs/promises"; +import { join } from "node:path"; +import { expect } from "vitest"; +import { parseEvidence } from "../../src/domain/evidence.js"; +import { javascriptApplicationAnalysisResultSchema } from "../../src/domain/javascript/javascriptApplicationAnalysis.js"; +import { JAVASCRIPT_STATIC_FACT_BYTES } from "../../src/domain/javascript/javascriptAnalysisResourceControls.js"; +import { javascriptAsyncChunkCapacity } from "../fixtures/javascriptAsyncChunkCapacity.js"; +import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js"; +import { cliTest } from "../support/cli/cliFixture.js"; + +cliTest( + "reports the complete-fact ceiling for wide async chunk metadata", + async ({ cli }) => { + // This bounded public control exceeds the complete-fact allowance. The + // forked boundary regression separately owns call-argument capacity. + const { source } = javascriptAsyncChunkCapacity(50_000); + const root = await createTestTempDirectory("rea-async-chunk-budget-"); + await writeFile(join(root, "entry.cjs"), source); + const response = await cli.run({ + arguments: [ + "analyze-javascript-application", + root, + "--max-semantic-nodes", + "1", + "--max-semantic-nodes-per-file", + "1", + "--max-heap-mb", + "2048", + "--analysis-timeout-ms", + "30000", + "--json", + ], + }); + // Analysis retains the interrupted file rather than failing the command. + expect(response.exitCode, `${response.stdout}\n${response.stderr}`).toBe(0); + const result = javascriptApplicationAnalysisResultSchema.parse( + parseEvidence(response.json).normalized_result, + ); + const failures = result.graph.nodes.flatMap(({ observations }) => + observations.flatMap(({ properties }) => + properties.analysis_failure === undefined + ? [] + : [properties.analysis_failure], + ), + ); + expect(failures).toEqual([ + expect.objectContaining({ + code: "resource_constraint", + retryable: false, + message: expect.stringContaining("static checkpoint"), + remediation: { + action: expect.stringContaining("no runtime override"), + }, + details: expect.objectContaining({ + resource: "transport", + reported_limits: expect.objectContaining({ + checkpoint_bytes: expect.any(Number), + checkpoint_limit_bytes: JAVASCRIPT_STATIC_FACT_BYTES, + }), + }), + }), + ]); + expect(JSON.stringify(failures)).not.toContain("call stack"); + expect(JSON.stringify(failures)).toContain( + "Raising max_heap_mb does not raise it", + ); + }, +); diff --git a/tests/acceptance/javascriptRouteBudgetRecovery.test.ts b/tests/acceptance/javascriptRouteBudgetRecovery.test.ts index 6d7fe16845..17a67c1960 100644 --- a/tests/acceptance/javascriptRouteBudgetRecovery.test.ts +++ b/tests/acceptance/javascriptRouteBudgetRecovery.test.ts @@ -29,7 +29,6 @@ cliTest( }), "--json", ], - timeoutMs: 60_000, }); expect(unpublished.exitCode, unpublished.stderr).toBe(1); expect(unpublished.json).toMatchObject({ @@ -60,7 +59,6 @@ cliTest( bundle, "--json", ], - timeoutMs: 60_000, }); expect(analyzed.exitCode, analyzed.stderr).toBe(0); const request = (fields?: readonly string[]) => diff --git a/tests/acceptance/javascriptRoutesCli.test.ts b/tests/acceptance/javascriptRoutesCli.test.ts index 0d5a3ea18b..786ff5253c 100644 --- a/tests/acceptance/javascriptRoutesCli.test.ts +++ b/tests/acceptance/javascriptRoutesCli.test.ts @@ -607,7 +607,6 @@ cliTest( await browser.close(); } }, - 120000, ); it("keeps a large source's partial route facts queryable through SDK delivery", async () => { @@ -662,4 +661,4 @@ it("keeps a large source's partial route facts queryable through SDK delivery", expect( routeViewFromEvidence(unknowns.structuredContent).items.length, ).toBeGreaterThan(0); -}, 60000); +}); diff --git a/tests/acceptance/localPathAdmissionCli.test.ts b/tests/acceptance/localPathAdmissionCli.test.ts index ecc1c46842..84eb2d83f6 100644 --- a/tests/acceptance/localPathAdmissionCli.test.ts +++ b/tests/acceptance/localPathAdmissionCli.test.ts @@ -6,7 +6,7 @@ import { expect } from "vitest"; import { parseEvidence } from "../../src/domain/evidence.js"; import { pythonPycFixture } from "../../src/python/PythonPyc.fixture.js"; import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js"; -import { cliTest, runFileCli } from "../support/cli/cliFixture.js"; +import { cliTest } from "../support/cli/cliFixture.js"; const fileCommands = [ "inspect-sqlite-database", @@ -51,7 +51,7 @@ cliTest( }, ]; for (const selection of selections) { - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: [...selection.arguments, "--json"], cwd, }); @@ -161,7 +161,7 @@ cliTest( }, ]; for (const selection of selections) { - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: [...selection.arguments, "--json"], cwd, }); @@ -183,7 +183,7 @@ cliTest( const bytes = pythonPycFixture(); const path = join(cwd, "selected.pyc"); await writeFile(path, bytes); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: ["inspect-python-pyc", "./selected.pyc", "--json"], cwd, }); diff --git a/tests/acceptance/managedCliOutput.test.ts b/tests/acceptance/managedCliOutput.test.ts index 737e5bc55c..590079ba37 100644 --- a/tests/acceptance/managedCliOutput.test.ts +++ b/tests/acceptance/managedCliOutput.test.ts @@ -7,7 +7,7 @@ import { expect } from "vitest"; import { elf, thinMach } from "../../src/domain/binaryTarget.fixture.js"; import { buildManagedPeFixture } from "../../src/dotnet/ManagedPe.fixture.js"; import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js"; -import { cliTest, runFileCli } from "../support/cli/cliFixture.js"; +import { cliTest } from "../support/cli/cliFixture.js"; cliTest( "recovers the same managed artifact through decimal CLI budgets and retains caller choices", @@ -157,7 +157,7 @@ cliTest( ilBody: Buffer.concat([Buffer.from([(il.length << 2) | 2]), il]), }), ); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: ["inspect-managed-members", path, "--json"], }); expect(result.exitCode, result.stderr).toBe(0); @@ -546,7 +546,7 @@ cliTest( }); const path = join(root, `virtual-${virtualSectionSize}.dll`); await writeFile(path, bytes); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: ["inspect-managed-members", path, "--json"], }); expect(result.exitCode).toBe(0); @@ -647,7 +647,7 @@ cliTest( "inspect-managed-members", "inspect-managed-native-boundaries", ]) { - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: [command, path, "--json"], }); expect(result.exitCode).toBe(1); @@ -682,7 +682,7 @@ cliTest( "inspect-managed-members", "inspect-managed-native-boundaries", ]) { - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: [command, path, "--json"], }); expect(result.exitCode).not.toBe(0); diff --git a/tests/acceptance/managedMemberPointers.test.ts b/tests/acceptance/managedMemberPointers.test.ts index 1d395ba482..4f5740f99b 100644 --- a/tests/acceptance/managedMemberPointers.test.ts +++ b/tests/acceptance/managedMemberPointers.test.ts @@ -1,12 +1,11 @@ import { createHash } from "node:crypto"; import { writeFile } from "node:fs/promises"; -import { join, resolve } from "node:path"; -import { Client } from "@modelcontextprotocol/client"; -import { StdioClientTransport } from "@modelcontextprotocol/client/stdio"; +import { join } from "node:path"; import { expect } from "vitest"; import { parseEvidence } from "../../src/domain/evidence.js"; import { buildManagedMemberPointerFixture } from "../../src/dotnet/ManagedPe.fixture.js"; +import { connectLocalToolsMcp } from "../fixtures/localToolsMcp.js"; import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js"; import { cliTest } from "../support/cli/cliFixture.js"; @@ -18,87 +17,70 @@ cliTest( const bytes = buildManagedMemberPointerFixture(); const sha256 = createHash("sha256").update(bytes).digest("hex"); await writeFile(path, bytes); - const transport = new StdioClientTransport({ - command: process.execPath, - args: [resolve("scripts/rea.mjs"), "mcp"], - cwd: process.cwd(), - env: { PATH: process.env.PATH ?? "", REA_LOG_LEVEL: "silent" }, - stderr: "pipe", - }); - transport.stderr?.on("data", () => undefined); - const client = new Client({ name: "managed-pointer-parity", version: "1" }); - try { - await client.connect(transport); - for (const name of [ - "inspect_managed_artifact", - "inspect_managed_members", - ] as const) { - const cliResult = await cli.run({ - arguments: [name.replaceAll("_", "-"), path, "--json"], - }); - expect(cliResult.exitCode, cliResult.stderr).toBe(0); - const cliEvidence = parseEvidence(cliResult.json); - const response = await client.callTool({ name, arguments: { path } }); - expect(response.isError, JSON.stringify(response)).not.toBe(true); - const mcpEvidence = parseEvidence(response.structuredContent); - expect(mcpEvidence.normalized_result).toEqual( - cliEvidence.normalized_result, - ); - expect(mcpEvidence.subject?.digest.sha256).toBe(sha256); - expect(cliEvidence.subject?.digest.sha256).toBe(sha256); + const { client } = await connectLocalToolsMcp(); + for (const name of [ + "inspect_managed_artifact", + "inspect_managed_members", + ] as const) { + const cliResult = await cli.run({ + arguments: [name.replaceAll("_", "-"), path, "--json"], + }); + expect(cliResult.exitCode, cliResult.stderr).toBe(0); + const cliEvidence = parseEvidence(cliResult.json); + const response = await client.callTool({ name, arguments: { path } }); + expect(response.isError, JSON.stringify(response)).not.toBe(true); + const mcpEvidence = parseEvidence(response.structuredContent); + expect(mcpEvidence.normalized_result).toEqual( + cliEvidence.normalized_result, + ); + expect(mcpEvidence.subject?.digest.sha256).toBe(sha256); + expect(cliEvidence.subject?.digest.sha256).toBe(sha256); + expect(mcpEvidence.normalized_result).toMatchObject({ + coverage: { state: "complete", issues: [] }, + }); + if (name === "inspect_managed_artifact") { expect(mcpEvidence.normalized_result).toMatchObject({ - coverage: { state: "complete", issues: [] }, + metadata: { stream_names: expect.arrayContaining(["#-"]) }, + attributes: [ + expect.objectContaining({ + parent_token: "0x20000001", + constructor_token: "0x06000001", + type_name: "Fixture.B", + }), + ], + }); + } else { + expect(mcpEvidence.normalized_result).toMatchObject({ + types: [ + expect.objectContaining({ full_name: "" }), + expect.objectContaining({ full_name: "Fixture.A" }), + expect.objectContaining({ full_name: "Fixture.B" }), + ], + fields: [ + expect.objectContaining({ + token: "0x04000001", + declaring_type: "Fixture.B", + declaring_type_token: "0x02000003", + }), + expect.objectContaining({ + token: "0x04000002", + declaring_type: "Fixture.A", + declaring_type_token: "0x02000002", + }), + ], + methods: [ + expect.objectContaining({ + token: "0x06000001", + declaring_type: "Fixture.B", + declaring_type_token: "0x02000003", + }), + expect.objectContaining({ + token: "0x06000002", + declaring_type: "Fixture.A", + declaring_type_token: "0x02000002", + }), + ], }); - if (name === "inspect_managed_artifact") { - expect(mcpEvidence.normalized_result).toMatchObject({ - metadata: { stream_names: expect.arrayContaining(["#-"]) }, - attributes: [ - expect.objectContaining({ - parent_token: "0x20000001", - constructor_token: "0x06000001", - type_name: "Fixture.B", - }), - ], - }); - } else { - expect(mcpEvidence.normalized_result).toMatchObject({ - types: [ - expect.objectContaining({ full_name: "" }), - expect.objectContaining({ full_name: "Fixture.A" }), - expect.objectContaining({ full_name: "Fixture.B" }), - ], - fields: [ - expect.objectContaining({ - token: "0x04000001", - declaring_type: "Fixture.B", - declaring_type_token: "0x02000003", - }), - expect.objectContaining({ - token: "0x04000002", - declaring_type: "Fixture.A", - declaring_type_token: "0x02000002", - }), - ], - methods: [ - expect.objectContaining({ - token: "0x06000001", - declaring_type: "Fixture.B", - declaring_type_token: "0x02000003", - }), - expect.objectContaining({ - token: "0x06000002", - declaring_type: "Fixture.A", - declaring_type_token: "0x02000002", - }), - ], - }); - } - } - } finally { - try { - await client.close(); - } finally { - await transport.close(); } } }, diff --git a/tests/acceptance/managedMetadataDelta.test.ts b/tests/acceptance/managedMetadataDelta.test.ts index ef20016df5..832834a6e1 100644 --- a/tests/acceptance/managedMetadataDelta.test.ts +++ b/tests/acceptance/managedMetadataDelta.test.ts @@ -37,7 +37,7 @@ cliTest( ], ] as const) { const selected = operation === "inspect_managed_members"; - const output = await cli.sharedCliRun!({ + const output = await cli.run({ arguments: [ command, path, diff --git a/tests/acceptance/managedMetadataExtraData.test.ts b/tests/acceptance/managedMetadataExtraData.test.ts index 6eb93c0554..a5c401d448 100644 --- a/tests/acceptance/managedMetadataExtraData.test.ts +++ b/tests/acceptance/managedMetadataExtraData.test.ts @@ -1,12 +1,11 @@ import { createHash } from "node:crypto"; import { writeFile } from "node:fs/promises"; -import { join, resolve } from "node:path"; -import { Client } from "@modelcontextprotocol/client"; -import { StdioClientTransport } from "@modelcontextprotocol/client/stdio"; +import { join } from "node:path"; import { expect } from "vitest"; import { parseEvidence } from "../../src/domain/evidence.js"; import { buildManagedPeFixture } from "../../src/dotnet/ManagedPe.fixture.js"; +import { connectLocalToolsMcp } from "../fixtures/localToolsMcp.js"; import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js"; import { cliTest } from "../support/cli/cliFixture.js"; @@ -18,66 +17,46 @@ cliTest( const bytes = buildManagedPeFixture({ metadataExtraData: 0x1234_5678 }); const sha256 = createHash("sha256").update(bytes).digest("hex"); await writeFile(path, bytes); - const transport = new StdioClientTransport({ - command: process.execPath, - args: [resolve("scripts/rea.mjs"), "mcp"], - cwd: process.cwd(), - env: { PATH: process.env.PATH ?? "", REA_LOG_LEVEL: "silent" }, - stderr: "pipe", - }); - transport.stderr?.on("data", () => undefined); - const client = new Client({ - name: "managed-extra-data-parity", - version: "1", - }); - try { - await client.connect(transport); - for (const name of [ - "inspect_managed_artifact", - "inspect_managed_members", - ] as const) { - const cliResult = await cli.run({ - arguments: [name.replaceAll("_", "-"), path, "--json"], - }); - expect(cliResult.exitCode, cliResult.stderr).toBe(0); - const cliEvidence = parseEvidence(cliResult.json); - const response = await client.callTool({ name, arguments: { path } }); - expect(response.isError, JSON.stringify(response)).not.toBe(true); - const mcpEvidence = parseEvidence(response.structuredContent); - expect(mcpEvidence.normalized_result).toEqual( - cliEvidence.normalized_result, - ); - expect(mcpEvidence.subject?.digest.sha256).toBe(sha256); - expect(cliEvidence.subject?.digest.sha256).toBe(sha256); + const { client } = await connectLocalToolsMcp(); + for (const name of [ + "inspect_managed_artifact", + "inspect_managed_members", + ] as const) { + const cliResult = await cli.run({ + arguments: [name.replaceAll("_", "-"), path, "--json"], + }); + expect(cliResult.exitCode, cliResult.stderr).toBe(0); + const cliEvidence = parseEvidence(cliResult.json); + const response = await client.callTool({ name, arguments: { path } }); + expect(response.isError, JSON.stringify(response)).not.toBe(true); + const mcpEvidence = parseEvidence(response.structuredContent); + expect(mcpEvidence.normalized_result).toEqual( + cliEvidence.normalized_result, + ); + expect(mcpEvidence.subject?.digest.sha256).toBe(sha256); + expect(cliEvidence.subject?.digest.sha256).toBe(sha256); + expect(mcpEvidence.normalized_result).toMatchObject({ + module: { + name: "Fixture.dll", + mvid: "00112233-4455-6677-8899-aabbccddeeff", + }, + coverage: { state: "complete", issues: [] }, + }); + if (name === "inspect_managed_artifact") { expect(mcpEvidence.normalized_result).toMatchObject({ - module: { - name: "Fixture.dll", - mvid: "00112233-4455-6677-8899-aabbccddeeff", - }, - coverage: { state: "complete", issues: [] }, + assembly: { name: "Fixture.Managed" }, + }); + } else { + expect(mcpEvidence.normalized_result).toMatchObject({ + types: [expect.objectContaining({ full_name: "Fixture.Program" })], + methods: [ + expect.objectContaining({ + token: "0x06000001", + name: "Main", + declaring_type: "Fixture.Program", + }), + ], }); - if (name === "inspect_managed_artifact") { - expect(mcpEvidence.normalized_result).toMatchObject({ - assembly: { name: "Fixture.Managed" }, - }); - } else { - expect(mcpEvidence.normalized_result).toMatchObject({ - types: [expect.objectContaining({ full_name: "Fixture.Program" })], - methods: [ - expect.objectContaining({ - token: "0x06000001", - name: "Main", - declaring_type: "Fixture.Program", - }), - ], - }); - } - } - } finally { - try { - await client.close(); - } finally { - await transport.close(); } } }, diff --git a/tests/acceptance/managedSelectedScope.test.ts b/tests/acceptance/managedSelectedScope.test.ts index 5eb2789289..52a60986b3 100644 --- a/tests/acceptance/managedSelectedScope.test.ts +++ b/tests/acceptance/managedSelectedScope.test.ts @@ -27,7 +27,7 @@ cliTest( ["Inner", "Fixture.Inner"], ["Fixture.Inner"], ]) { - const output = await cli.sharedCliRun!({ + const output = await cli.run({ arguments: [ "inspect-managed-members", path, @@ -99,7 +99,7 @@ cliTest( const sha256 = createHash("sha256").update(content).digest("hex"); await writeFile(path, content); for (const isSelected of [true, false]) { - const output = await cli.sharedCliRun!({ + const output = await cli.run({ arguments: [ "inspect-managed-members", path, diff --git a/tests/acceptance/mcpInputCapacityPcap.test.ts b/tests/acceptance/mcpInputCapacityPcap.test.ts index 4d2bbc1f9e..e97e87a84c 100644 --- a/tests/acceptance/mcpInputCapacityPcap.test.ts +++ b/tests/acceptance/mcpInputCapacityPcap.test.ts @@ -92,5 +92,4 @@ it.skipIf( .digest("hex"), ).toBe(digest); }, - 90_000, ); diff --git a/tests/acceptance/modelArtifactCli.test.ts b/tests/acceptance/modelArtifactCli.test.ts index 23e766f398..d6d39fc5fd 100644 --- a/tests/acceptance/modelArtifactCli.test.ts +++ b/tests/acceptance/modelArtifactCli.test.ts @@ -22,11 +22,7 @@ import { safetensorsFixture, } from "../fixtures/model/model.js"; import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js"; -import { - cliTest, - createSharedMcpSession, - runFileCli, -} from "../support/cli/cliFixture.js"; +import { cliTest, createSharedMcpSession } from "../support/cli/cliFixture.js"; // Model queries are target-free; share immutable registration and schema setup. // Portable export keeps its separate post-CLI connection below. @@ -113,7 +109,7 @@ cliTest( }); const bytes = Buffer.concat([fixture.bytes, Buffer.alloc(16)]); await writeFile(path, bytes); - const cliResult = await runFileCli(cli, { + const cliResult = await cli.run({ arguments: [ "inspect-model-artifact", path, @@ -365,7 +361,7 @@ cliTest( tensors, }); await writeFile(path, fixture.bytes); - const response = await runFileCli(cli, { + const response = await cli.run({ arguments: [ "inspect-model-artifact", path, @@ -999,7 +995,7 @@ cliTest( ] as const) { const path = join(root, name); await writeFile(path, bytes); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: [ "inspect-model-artifact", path, @@ -1047,7 +1043,7 @@ cliTest( Buffer.alloc(0), ).bytes, ); - const mapCli = await runFileCli(cli, { + const mapCli = await cli.run({ arguments: [ "inspect-model-artifact", mapPath, @@ -1084,7 +1080,7 @@ cliTest( safetensorsFixture(`{"__metadata__":${numeric}}`, Buffer.alloc(0)) .bytes, ); - const malformedCli = await runFileCli(cli, { + const malformedCli = await cli.run({ arguments: ["inspect-model-artifact", malformedPath, "--json"], }); expect(malformedCli.exitCode).toBe(1); diff --git a/tests/acceptance/nativeDispatchSnapshot.test.ts b/tests/acceptance/nativeDispatchSnapshot.test.ts index c819ed2ea5..4906a329c8 100644 --- a/tests/acceptance/nativeDispatchSnapshot.test.ts +++ b/tests/acceptance/nativeDispatchSnapshot.test.ts @@ -117,7 +117,6 @@ cliTest.skipIf(process.platform !== "darwin")( expect(Buffer.compare(await readFile(first), Buffer.from(bytes))).toBe(0); expect(Buffer.compare(await readFile(second), Buffer.from(bytes))).toBe(0); }, - 30000, ); cliTest.skipIf(process.platform !== "darwin")( @@ -174,7 +173,6 @@ cliTest.skipIf(process.platform !== "darwin")( expect(Buffer.compare(await readFile(target), bytes)).toBe(0); await client.ping(); }, - 30000, ); cliTest.skipIf(process.platform !== "darwin")( @@ -308,7 +306,6 @@ cliTest.skipIf(process.platform !== "darwin")( expect(Buffer.compare(await readFile(target), Buffer.from(bytes))).toBe(0); await client.ping(); }, - 30000, ); cliTest.skipIf(process.platform !== "darwin")( diff --git a/tests/acceptance/nativeElfArchitecture.test.ts b/tests/acceptance/nativeElfArchitecture.test.ts index 22c91e2ff6..e9fa56b141 100644 --- a/tests/acceptance/nativeElfArchitecture.test.ts +++ b/tests/acceptance/nativeElfArchitecture.test.ts @@ -88,5 +88,4 @@ cliTest( } await client.ping(); }, - 30000, ); diff --git a/tests/acceptance/nonBinaryComposition.test.ts b/tests/acceptance/nonBinaryComposition.test.ts index 6b9ff1f8de..dbbbd22746 100644 --- a/tests/acceptance/nonBinaryComposition.test.ts +++ b/tests/acceptance/nonBinaryComposition.test.ts @@ -66,7 +66,6 @@ for (const { jarConfigured, setting } of [ const cliResult = await cli.run({ arguments: ["inspect-android-package", apk, "--json"], environment, - timeoutMs: 10_000, }); expect(cliResult.exitCode).toBe(1); expect(cliResult.json).toMatchObject({ code: "capability_unavailable" }); @@ -91,7 +90,6 @@ cliTest.skipIf(process.platform === "win32")( const cliResult = await cli.run({ arguments: ["inspect-android-package", apk, "--json"], environment: { REA_LOG_LEVEL: "silent", ...environment }, - timeoutMs: 10_000, }); expect(cliResult.exitCode).toBe(1); const client = await connect({ REA_LOG_LEVEL: "silent", ...environment }); @@ -146,7 +144,6 @@ cliTest.skipIf(process.platform !== "linux")( const cliResult = await cli.run({ arguments: ["inspect-firmware-regions", path, "--json"], environment, - timeoutMs: 10_000, }); expect(cliResult.exitCode).toBe(1); expect(cliResult.json).toMatchObject({ code: "capability_unavailable" }); @@ -172,7 +169,6 @@ cliTest.skipIf(process.platform !== "linux")( arguments: ["inspect-firmware-regions", "input.bin", "--json"], cwd: root, environment: { REA_LOG_LEVEL: "silent" }, - timeoutMs: 10_000, }); // A relative path must pass input validation and reach the provider, // which reports the missing firmware engine — not a validation error. @@ -199,7 +195,6 @@ cliTest( "--json", ], environment, - timeoutMs: 10000, }); expect(cliResult.exitCode).toBe(1); expect(cliResult.json).toMatchObject({ code: "capability_unavailable" }); @@ -239,7 +234,6 @@ cliTest( "--json", ], environment, - timeoutMs: 10_000, }); const response = await client.callTool({ name: "recover_javascript_sources", diff --git a/tests/acceptance/pcapCredentialErrorsCliMcp.test.ts b/tests/acceptance/pcapCredentialErrorsCliMcp.test.ts index 61123d5477..7f8980d64a 100644 --- a/tests/acceptance/pcapCredentialErrorsCliMcp.test.ts +++ b/tests/acceptance/pcapCredentialErrorsCliMcp.test.ts @@ -60,7 +60,6 @@ cliTest.skipIf(missingNative)( const success = await cli.run({ arguments: ["inspect-pcap-capture", single, "--json"], environment: environmentFor(root), - timeoutMs: 60_000, }); expect(success.exitCode, success.stderr).toBe(0); expect(success.stdout.includes(credential)).toBe(false); @@ -78,7 +77,6 @@ cliTest.skipIf(missingNative)( const failed = await cli.run({ arguments: ["inspect-pcap-capture", many, "--json"], environment: environmentFor(root), - timeoutMs: 60_000, }); expect(failed.exitCode, failed.stderr).toBe(1); expect(failed.stdout.includes(credential)).toBe(false); @@ -89,7 +87,6 @@ cliTest.skipIf(missingNative)( .digest("hex"), ).toBe(before); }, - 120_000, ); it.skipIf(missingNative)( @@ -141,5 +138,4 @@ it.skipIf(missingNative)( } } }, - 120_000, ); diff --git a/tests/acceptance/pcapVerifier.test.ts b/tests/acceptance/pcapVerifier.test.ts index d2f739c24b..e6df5f86f8 100644 --- a/tests/acceptance/pcapVerifier.test.ts +++ b/tests/acceptance/pcapVerifier.test.ts @@ -58,5 +58,4 @@ it.skipIf( ); } }, - 120_000, ); diff --git a/tests/acceptance/peCertificatesCli.test.ts b/tests/acceptance/peCertificatesCli.test.ts index 9f01c5328f..e4aa45f812 100644 --- a/tests/acceptance/peCertificatesCli.test.ts +++ b/tests/acceptance/peCertificatesCli.test.ts @@ -12,7 +12,7 @@ import { import { repeatedCertificateSignedData } from "../fixtures/peCertificateAllowance.js"; import { parseMcpToolError } from "../fixtures/mcpToolError.js"; import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js"; -import { cliTest, runFileCli } from "../support/cli/cliFixture.js"; +import { cliTest } from "../support/cli/cliFixture.js"; cliTest.for([0x00, 0x01, 0x7f, 0xfe, 0xff, null])( "requires canonical extension critical encoding %s through CLI and MCP", @@ -41,7 +41,7 @@ cliTest.for([0x00, 0x01, 0x7f, 0xfe, 0xff, null])( }), ); await writeFile(path, bytes); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: ["inspect-pe-certificates", path, "--json"], }); const call = await cli.sharedMcpCall!(); @@ -104,7 +104,7 @@ cliTest.for(["extra-field", "empty-rdn"] as const)( ); const bytes = peCertificateFixture(false, payload); await writeFile(path, bytes); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: ["inspect-pe-certificates", path, "--json"], }); expect.soft(result.exitCode, result.stdout).toBe(1); @@ -138,7 +138,7 @@ cliTest.for(["pubkey", "signature"])( expect(at).toBeGreaterThanOrEqual(0); payload[at + 2] = 1; await writeFile(path, peCertificateFixture(false, payload)); - const invalid = await runFileCli(cli, { + const invalid = await cli.run({ arguments: ["inspect-pe-certificates", path, "--json"], }); expect(invalid.exitCode).toBe(1); @@ -159,7 +159,7 @@ cliTest.for(["pubkey", "signature"])( const last = at + marker.length - 1; payload[last] = payload[last]! & 0xfe; await writeFile(path, peCertificateFixture(false, payload)); - const valid = await runFileCli(cli, { + const valid = await cli.run({ arguments: ["inspect-pe-certificates", path, "--json"], }); expect(valid.exitCode, valid.stderr).toBe(0); @@ -210,7 +210,7 @@ cliTest("reports unsigned attributes through CLI and MCP", async ({ cli }) => { [1, 3, 6, 1, 4, 1, 311, 3, 3, 1], ]); await writeFile(path, peCertificateFixture(true, payload)); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: ["inspect-pe-certificates", path, "--json"], }); expect(result.exitCode, result.stderr).toBe(0); @@ -239,7 +239,7 @@ cliTest( const payload = pkcs7SignedDataIndefiniteEnvelopeFixture(); await writeFile(path, peCertificateFixture(false, payload)); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: ["inspect-pe-certificates", path, "--json"], }); @@ -297,7 +297,7 @@ cliTest("reports a truncated PE as invalid input", async ({ cli }) => { const root = await createTestTempDirectory("rea-pe-cert-cli-bad-"); const path = join(root, "broken.exe"); await writeFile(path, Buffer.from("MZ")); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: ["inspect-pe-certificates", path, "--json"], }); expect(result.exitCode).toBe(1); @@ -318,7 +318,7 @@ cliTest( // Replace an equal-length UTF8String with T.61's acute accent plus 'e'. Buffer.from([0x14, 0x02, 0xc2, 0x65]).copy(payload, nameAt); await writeFile(path, peCertificateFixture(false, payload)); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: ["inspect-pe-certificates", path, "--json"], }); expect(result.exitCode, result.stderr).toBe(0); @@ -348,7 +348,7 @@ cliTest( payload.subarray(4), ]); await writeFile(path, peCertificateFixture(false, malformed)); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: ["inspect-pe-certificates", path, "--json"], }); expect(result.exitCode).toBe(1); @@ -372,7 +372,7 @@ cliTest( // Equal-size replacement keeps the DER lengths and all unrelated fields intact. Buffer.from([0x1e, 0x04, 0xd8, 0, 0xdc, 0]).copy(payload, nameAt); await writeFile(path, peCertificateFixture(false, payload)); - const invalid = await runFileCli(cli, { + const invalid = await cli.run({ arguments: ["inspect-pe-certificates", path, "--json"], }); expect(invalid.exitCode).toBe(1); @@ -382,7 +382,7 @@ cliTest( }); Buffer.from([0x1e, 0x04, 0xd7, 0xff, 0xe0, 0]).copy(payload, nameAt); await writeFile(path, peCertificateFixture(false, payload)); - const valid = await runFileCli(cli, { + const valid = await cli.run({ arguments: ["inspect-pe-certificates", path, "--json"], }); expect(valid.exitCode, valid.stderr).toBe(0); @@ -407,7 +407,7 @@ cliTest( // 2025 has no 29 February; the equal-length edit keeps every DER length. payload.write("250229000000Z", timeAt, "ascii"); await writeFile(path, peCertificateFixture(false, payload)); - const invalid = await runFileCli(cli, { + const invalid = await cli.run({ arguments: ["inspect-pe-certificates", path, "--json"], }); expect(invalid.exitCode).toBe(1); @@ -417,7 +417,7 @@ cliTest( }); payload.write("240229000000Z", timeAt, "ascii"); await writeFile(path, peCertificateFixture(false, payload)); - const valid = await runFileCli(cli, { + const valid = await cli.run({ arguments: ["inspect-pe-certificates", path, "--json"], }); expect(valid.exitCode, valid.stderr).toBe(0); @@ -521,7 +521,7 @@ cliTest.for([ expect(at).toBeGreaterThanOrEqual(0); payload[at] = tag; await writeFile(path, peCertificateFixture(false, payload)); - const invalid = await runFileCli(cli, { + const invalid = await cli.run({ arguments: ["inspect-pe-certificates", path, "--json"], }); expect(invalid.exitCode).toBe(1); @@ -532,7 +532,7 @@ cliTest.for([ // Only restore the string tag: the same bytes are valid UTF8String content. payload[at] = 0x0c; await writeFile(path, peCertificateFixture(false, payload)); - const valid = await runFileCli(cli, { + const valid = await cli.run({ arguments: ["inspect-pe-certificates", path, "--json"], }); expect(valid.exitCode, valid.stderr).toBe(0); diff --git a/tests/acceptance/peImportsCli.test.ts b/tests/acceptance/peImportsCli.test.ts index e2ced905e7..a26015719a 100644 --- a/tests/acceptance/peImportsCli.test.ts +++ b/tests/acceptance/peImportsCli.test.ts @@ -14,7 +14,7 @@ import { } from "../fixtures/binaryDiagnostics/peImports.js"; import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js"; import { parseMcpToolError } from "../fixtures/mcpToolError.js"; -import { cliTest, runFileCli } from "../support/cli/cliFixture.js"; +import { cliTest } from "../support/cli/cliFixture.js"; for (const bits of [32, 64] as const) { cliTest( @@ -263,9 +263,10 @@ cliTest( const invocation = { arguments: ["inspect-pe-imports", path, "--json"], }; - const result = await (rva === 0xffffffec - ? cli.run(invocation) - : runFileCli(cli, invocation)); + const result = await cli.run({ + ...invocation, + freshProcess: rva === 0xffffffec, + }); expect(result.exitCode, result.stderr).toBe(0); const report = toolContract("inspect_pe_imports").outputSchema.parse( result.json, diff --git a/tests/acceptance/process/processFinalization.test.ts b/tests/acceptance/process/processFinalization.test.ts index 75abe7f582..f3317d9a89 100644 --- a/tests/acceptance/process/processFinalization.test.ts +++ b/tests/acceptance/process/processFinalization.test.ts @@ -276,7 +276,6 @@ const expectIgnoringFinalization = (outcome: CaptureOutcome): void => { cliCaptureTest.for(["CLI", "MCP"] as const)( "captures a cooperative finalizer through the compiled %s workflow", - { timeout: 20_000 }, async (adapter, { cli }) => { const root = await createTestTempDirectory("rea-finalization-accept-"); const scenario = captureScenario("cooperative", root, { @@ -293,7 +292,6 @@ cliCaptureTest.for(["CLI", "MCP"] as const)( cliCaptureTest.for(["CLI", "MCP"] as const)( "records the non-zero exit code of a finalizer through the compiled %s workflow", - { timeout: 20_000 }, async (adapter, { cli }) => { const root = await createTestTempDirectory("rea-finalization-accept-"); const scenario = captureScenario( @@ -312,7 +310,6 @@ cliCaptureTest.for(["CLI", "MCP"] as const)( cliCaptureTest.for(["CLI", "MCP"] as const)( "forces SIGKILL after the finalization interval through the compiled %s workflow", - { timeout: 20_000 }, async (adapter, { cli }) => { const root = await createTestTempDirectory("rea-finalization-accept-"); const scenario = captureScenario("ignoring", root, { @@ -386,12 +383,10 @@ captureTest( observeProcessStartIdentity(childPid), ).resolves.toBeUndefined(); }, - 20_000, ); cliCaptureTest.for(["CLI", "MCP"] as const)( "keeps the default identity through the compiled %s workflow without finalization_ms", - { timeout: 20_000 }, async (adapter, { cli }) => { const root = await createTestTempDirectory("rea-finalization-default-"); const scenario = captureScenario("ignoring", root, { timeout_ms: 500 }); @@ -583,5 +578,4 @@ captureTest( "MCP cancellation does not wait for the grace interval", ).toBeLessThan(8_000); }, - 20_000, ); diff --git a/tests/acceptance/process/processSensitiveInput.test.ts b/tests/acceptance/process/processSensitiveInput.test.ts index 83f9105054..9825b59c28 100644 --- a/tests/acceptance/process/processSensitiveInput.test.ts +++ b/tests/acceptance/process/processSensitiveInput.test.ts @@ -68,7 +68,6 @@ cliTest.skipIf(CAPTURE_SKIP_REASON)( expect(result.stderr).not.toContain(literal); verify(result.json); }, - 30_000, ); itWithCaptureCapability( diff --git a/tests/acceptance/process/processSignalExit.test.ts b/tests/acceptance/process/processSignalExit.test.ts index 6ea4499395..b51ff99a86 100644 --- a/tests/acceptance/process/processSignalExit.test.ts +++ b/tests/acceptance/process/processSignalExit.test.ts @@ -66,7 +66,6 @@ cliCaptureTest( expect(capture.exit, name).toMatchObject(expected); } }, - 60_000, ); captureTest( @@ -94,5 +93,4 @@ captureTest( reason: "exited", }); }, - 30_000, ); diff --git a/tests/acceptance/providers/hopperStartupRecovery.test.ts b/tests/acceptance/providers/hopperStartupRecovery.test.ts index e9121b54d0..0aae52cec5 100644 --- a/tests/acceptance/providers/hopperStartupRecovery.test.ts +++ b/tests/acceptance/providers/hopperStartupRecovery.test.ts @@ -61,7 +61,6 @@ const closeWithLateBridge = async ( test.for(["target", "loader"] as const)( "preserves a prepared target or refuses literal-unsafe %s before handoff", - { timeout: 90_000 }, async (source, { workspace, cli }) => { const target = workspace.path( source === "target" ? 'quoted"target' : "loader-target", @@ -133,7 +132,6 @@ test.for(["target", "loader"] as const)( const result = await cli.run({ arguments: ["analyze", target, "--provider", "hopper", "--json"], environment, - timeoutMs: 15_000, }); expect(result.exitCode).toBe(1); expect(result.json).toMatchObject({ @@ -224,7 +222,6 @@ test("recovers a denied Hopper launch through the same stdio MCP session and CLI const deniedCli = await cli.run({ arguments: ["analyze", target, "--provider", "hopper", "--json"], environment, - timeoutMs: 15_000, }); expect(deniedCli.exitCode).toBe(1); expect(deniedCli.json).toMatchObject({ @@ -244,7 +241,6 @@ test("recovers a denied Hopper launch through the same stdio MCP session and CLI test.for(["handoff", "not-found"] as const)( "preserves an unconfirmed %s handoff and closes only a late bridge with matching run identity", - { timeout: 60_000 }, async (launchMode, { workspace }) => { const target = workspace.path("target"); await copyFile("/usr/bin/true", target); diff --git a/tests/acceptance/providers/jebConfigurationIsolation.test.ts b/tests/acceptance/providers/jebConfigurationIsolation.test.ts index 9b8b957a35..e2e3e80e3f 100644 --- a/tests/acceptance/providers/jebConfigurationIsolation.test.ts +++ b/tests/acceptance/providers/jebConfigurationIsolation.test.ts @@ -4,11 +4,16 @@ import { StdioClientTransport } from "@modelcontextprotocol/client/stdio"; import { resolve } from "node:path"; import { createServer } from "node:http"; import { once } from "node:events"; -import { expect } from "vitest"; +import { afterAll, expect } from "vitest"; import { parseMcpToolError } from "../../fixtures/mcpToolError.js"; import { workspaceCliTest as test } from "../../support/cli/workspaceCliFixture.js"; import type { TestWorkspace } from "../../support/workspace/workspaceFixture.js"; +import { createMcpSchemaValidation } from "../../fixtures/mcpSchemaValidation.js"; + +// Compile SDK output validators for the tools a test calls, not the whole catalog. +const schemaValidation = createMcpSchemaValidation(); +afterAll(() => schemaValidation.clear()); const isolatedEnvironment = (workspace: TestWorkspace) => ({ // Discovery still needs the host's process-ownership prerequisites, such as ps. @@ -71,10 +76,13 @@ test("retains the selected endpoint when a direct JEB connection fails", async ( }, stderr: "pipe", }); - const client = new Client({ - name: "jeb-endpoint-acceptance", - version: "1", - }); + const client = new Client( + { + name: "jeb-endpoint-acceptance", + version: "1", + }, + { jsonSchemaValidator: schemaValidation }, + ); await withVerifierCleanup(async () => { await client.connect(transport); const definition = (await client.listTools()).tools.find( @@ -142,7 +150,10 @@ test("starts production stdio MCP and retains peer requests around a JEB configu env: isolatedEnvironment(workspace), stderr: "pipe", }); - const client = new Client({ name: "jeb-isolation-acceptance", version: "1" }); + const client = new Client( + { name: "jeb-isolation-acceptance", version: "1" }, + { jsonSchemaValidator: schemaValidation }, + ); let stderr = ""; transport.stderr?.on("data", (chunk: Buffer) => { stderr += chunk.toString("utf8"); diff --git a/tests/acceptance/pyinstallerArchive.test.ts b/tests/acceptance/pyinstallerArchive.test.ts index 7f503df9dc..cd3b69ce39 100644 --- a/tests/acceptance/pyinstallerArchive.test.ts +++ b/tests/acceptance/pyinstallerArchive.test.ts @@ -160,7 +160,6 @@ cliTest( const result = await driver.run({ arguments: ["inspect-artifact", path, "--json"], environment: process.env, - timeoutMs: 15_000, }); expect(result.exitCode, result.stderr).toBe(0); const inspection = artifactInspectionResultSchema.parse( @@ -328,7 +327,6 @@ cliTest( const result = await driver.run({ arguments: ["extract-artifact", path, "--json"], environment: process.env, - timeoutMs: 15_000, }); expect(result.exitCode, result.stderr).toBe(0); const extraction = artifactExtractionResultSchema.parse( @@ -392,7 +390,6 @@ cliTest( const result = await driver.run({ arguments: ["inspect-artifact", path, "--json"], environment: process.env, - timeoutMs: 15_000, }); expect(result.exitCode, result.stderr).toBe(0); const inspection = artifactInspectionResultSchema.parse( @@ -445,7 +442,6 @@ cliTest( const result = await driver.run({ arguments: ["inspect-artifact", path, "--json"], environment: process.env, - timeoutMs: 15_000, }); expect(result.exitCode, result.stderr).toBe(0); const inspection = artifactInspectionResultSchema.parse( @@ -473,7 +469,6 @@ cliTest( const result = await driver.run({ arguments: ["extract-artifact", path, "--json"], environment: process.env, - timeoutMs: 15_000, }); expect(result.exitCode, result.stderr).toBe(0); const extracted = artifactExtractionResultSchema.parse( @@ -536,7 +531,6 @@ cliTest( const inspectResult = await driver.run({ arguments: ["inspect-artifact", path, "--json"], environment: process.env, - timeoutMs: 15_000, }); expect(inspectResult.exitCode, inspectResult.stderr).toBe(0); const inspection = artifactInspectionResultSchema.parse( @@ -610,7 +604,6 @@ cliTest( const extractResult = await driver.run({ arguments: ["extract-artifact", path, "--json"], environment: process.env, - timeoutMs: 15_000, }); expect(extractResult.exitCode, extractResult.stderr).toBe(0); const extracted = artifactExtractionResultSchema.parse( @@ -645,7 +638,6 @@ cliTest( const result = await driver.run({ arguments: ["inspect-artifact", path, "--json"], environment: process.env, - timeoutMs: 15_000, }); expect(result.exitCode, result.stderr).toBe(0); const inspection = artifactInspectionResultSchema.parse( @@ -688,7 +680,6 @@ cliTest.for([ const result = await driver.run({ arguments: ["inspect-artifact", path, "--json"], environment: process.env, - timeoutMs: 15_000, }); expect(result.exitCode, result.stderr).toBe(0); const inspection = artifactInspectionResultSchema.parse( @@ -752,7 +743,6 @@ cliTest.for([ const result = await driver.run({ arguments: ["inspect-artifact", path, "--json"], environment: process.env, - timeoutMs: 15_000, }); expect(result.exitCode, result.stderr).toBe(0); const inspection = artifactInspectionResultSchema.parse( @@ -800,7 +790,6 @@ cliTest( const result = await driver.run({ arguments: ["inspect-artifact", path, "--json"], environment: process.env, - timeoutMs: 15_000, }); expect(result.exitCode).toBe(1); expect(result.json).toMatchObject({ diff --git a/tests/acceptance/pythonPycCli.test.ts b/tests/acceptance/pythonPycCli.test.ts index 023c21534c..9e7d1714a5 100644 --- a/tests/acceptance/pythonPycCli.test.ts +++ b/tests/acceptance/pythonPycCli.test.ts @@ -10,7 +10,7 @@ import { pythonPycHeader, } from "../../src/python/PythonPyc.fixture.js"; import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js"; -import { cliTest, runFileCli } from "../support/cli/cliFixture.js"; +import { cliTest } from "../support/cli/cliFixture.js"; import { pythonPycEqualityGolden } from "../fixtures/python-pyc-equality/golden.js"; const completePycEvidenceSchema = toolContract( @@ -24,7 +24,7 @@ cliTest( const directory = await createTestTempDirectory("rea-pyc-code-equality-"); const path = join(directory, "equality.pyc"); await writeFile(path, Buffer.from(golden.pyc_base64, "base64")); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: ["inspect-python-pyc", path, "--json"], }); expect(result.exitCode, result.stderr).toBe(0); @@ -84,7 +84,7 @@ cliTest( }), ]), ); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: ["inspect-python-pyc", path, "--json"], }); expect(result.exitCode, result.stderr).toBe(0); @@ -104,7 +104,7 @@ cliTest( }), ]), ); - const ordinaryInt = await runFileCli(cli, { + const ordinaryInt = await cli.run({ arguments: ["inspect-python-pyc", ordinaryIntPath, "--json"], }); expect(ordinaryInt.exitCode, ordinaryInt.stderr).toBe(0); @@ -122,7 +122,7 @@ cliTest( Buffer.alloc(7), ]), ); - const truncated = await runFileCli(cli, { + const truncated = await cli.run({ arguments: ["inspect-python-pyc", truncatedPath, "--json"], }); expect(truncated.exitCode).toBe(1); @@ -210,7 +210,7 @@ cliTest( const path = join(root, `inline-caches-${magic}.pyc`); await writeFile(path, pyc); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: ["inspect-python-pyc", path, "--json"], }); expect( @@ -305,7 +305,7 @@ cliTest( header[2] = 0x0d; header[3] = 0x0a; await writeFile(path, Buffer.concat([header, marshal])); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: ["inspect-python-pyc", path, "--json"], }); expect(result.exitCode).toBe(1); @@ -322,7 +322,7 @@ cliTest( for (const magic of [3392, 3394]) { const path = join(root, `${magic}.pyc`); await writeFile(path, Buffer.concat([pythonPycHeader(magic), marshal])); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: ["inspect-python-pyc", path, "--json"], }); expect(result.exitCode, result.stderr).toBe(0); @@ -381,7 +381,7 @@ cliTest( path, Buffer.concat([pythonPycHeader(magic), marshalCode(layout, fields)]), ); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: ["inspect-python-pyc", path, "--json"], }); expect(result.exitCode, result.stderr).toBe(0); @@ -440,7 +440,7 @@ cliTest( path, Buffer.concat([pythonPycHeader(magic), marshalCode(layout, fields)]), ); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: ["inspect-python-pyc", path, "--json"], }); expect(result.exitCode).toBe(1); @@ -530,7 +530,7 @@ cliTest( }), ]), ); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: ["inspect-python-pyc", path, "--json"], }); expect(result.exitCode, result.stderr).toBe(0); @@ -580,7 +580,7 @@ cliTest( marshalCode(testCase.layout, { consts: [frozenSet, dict] }), ]), ); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: ["inspect-python-pyc", path, "--json"], }); expect(result.exitCode, result.stderr).toBe(0); @@ -658,7 +658,7 @@ cliTest( marshalCode(testCase.layout, { consts: [frozenSet, dict] }), ]), ); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: ["inspect-python-pyc", path, "--json"], }); expect(result.exitCode, result.stderr).toBe(0); @@ -689,7 +689,7 @@ cliTest( }), ]), ); - const rawResult = await runFileCli(cli, { + const rawResult = await cli.run({ arguments: ["inspect-python-pyc", rawPath, "--json"], }); expect(rawResult.exitCode, rawResult.stderr).toBe(0); diff --git a/tests/acceptance/releaseExposureCli.test.ts b/tests/acceptance/releaseExposureCli.test.ts index d1c0b821a3..e63469238c 100644 --- a/tests/acceptance/releaseExposureCli.test.ts +++ b/tests/acceptance/releaseExposureCli.test.ts @@ -14,57 +14,44 @@ if (releaseExposureContract === undefined) throw new Error("Missing release exposure contract"); const outputSchema = releaseExposureContract.outputSchema; -cliTest( - "exits 3 when a shipped source map embeds sources", - async ({ cli }) => { - const root = await createTestTempDirectory("rea-release-exposure-cli-"); - await writeFile(join(root, "app.js"), "export const value = 1;\n"); - await writeFile( - join(root, "app.js.map"), - JSON.stringify({ - version: 3, - file: "app.js", - sources: ["app.js"], - names: [], - mappings: "", - sourcesContent: [`export const value = "${PLANTED_SOURCE}";\n`], - }), - ); - const clean = await cli.run({ - arguments: ["audit-release-exposure", root, "--json"], - }); - expect(clean.exitCode, clean.stderr).toBe(0); - const evidence = outputSchema.parse(clean.json); - const report = evidence.normalized_result; - expect( - report.checks.find( - (check) => check.id === "javascript.source_map_sources", - ), - ).toMatchObject({ status: "exposed", severity: "high" }); - expect( - report.checks.find((check) => check.id === "electron.run_as_node") - ?.status, - ).toBe("unknown"); - expect( - report.checks.find((check) => check.id === "native.pdb_path")?.status, - ).toBe("not_applicable"); - expect( - report.checks.find((check) => check.id === "go.build_paths")?.status, - ).toBe("not_applicable"); - expect(JSON.stringify(evidence)).not.toContain(PLANTED_SOURCE); - const gated = await cli.run({ - arguments: [ - "audit-release-exposure", - root, - "--fail_on", - "high", - "--json", - ], - }); - expect(gated.exitCode, gated.stderr).toBe(3); - }, - 60_000, -); +cliTest("exits 3 when a shipped source map embeds sources", async ({ cli }) => { + const root = await createTestTempDirectory("rea-release-exposure-cli-"); + await writeFile(join(root, "app.js"), "export const value = 1;\n"); + await writeFile( + join(root, "app.js.map"), + JSON.stringify({ + version: 3, + file: "app.js", + sources: ["app.js"], + names: [], + mappings: "", + sourcesContent: [`export const value = "${PLANTED_SOURCE}";\n`], + }), + ); + const clean = await cli.run({ + arguments: ["audit-release-exposure", root, "--json"], + }); + expect(clean.exitCode, clean.stderr).toBe(0); + const evidence = outputSchema.parse(clean.json); + const report = evidence.normalized_result; + expect( + report.checks.find((check) => check.id === "javascript.source_map_sources"), + ).toMatchObject({ status: "exposed", severity: "high" }); + expect( + report.checks.find((check) => check.id === "electron.run_as_node")?.status, + ).toBe("unknown"); + expect( + report.checks.find((check) => check.id === "native.pdb_path")?.status, + ).toBe("not_applicable"); + expect( + report.checks.find((check) => check.id === "go.build_paths")?.status, + ).toBe("not_applicable"); + expect(JSON.stringify(evidence)).not.toContain(PLANTED_SOURCE); + const gated = await cli.run({ + arguments: ["audit-release-exposure", root, "--fail_on", "high", "--json"], + }); + expect(gated.exitCode, gated.stderr).toBe(3); +}); cliTest( "gates a marker in a text file outside the extension list", @@ -98,7 +85,6 @@ cliTest( ], }); }, - 60_000, ); cliTest( @@ -120,5 +106,4 @@ cliTest( expect(report.summary.no_exposure_observed_in_assessed_checks).toBe(true); expect(report.summary.by_status.not_assessed).toBe(3); }, - 60_000, ); diff --git a/tests/acceptance/rizinCommandOutcomes.test.ts b/tests/acceptance/rizinCommandOutcomes.test.ts new file mode 100644 index 0000000000..180e2ab161 --- /dev/null +++ b/tests/acceptance/rizinCommandOutcomes.test.ts @@ -0,0 +1,93 @@ +import { createHash } from "node:crypto"; +import { chmod, writeFile } from "node:fs/promises"; +import { join } from "node:path"; +import { expect } from "vitest"; +import { REVERSE_ENGINEERING_TOOL_CONTRACTS } from "../../src/contracts/reverseEngineeringToolContracts.js"; +import { rizinCommandLimitations } from "../../src/rizin/RizinCommand.js"; +import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js"; +import { connectLocalToolsMcp } from "../fixtures/localToolsMcp.js"; +import { cliTest } from "../support/cli/cliFixture.js"; + +const outcomes = [ + { + command: "axffj", + stdout: "", + stderr: "ERROR: Command 'axffj' does not exist.\n", + // The refusal line on stderr is classified; process facts stay as observed. + completion: "rejected", + }, + { command: "q", stdout: "", stderr: "", completion: "complete" }, + { + command: "echo diagnostic", + stdout: "ERROR: Command 'axffj' does not exist.\n", + stderr: "WARNING: fixture warning\n", + // Refusal text on stdout is command output, not a Rizin diagnostic. + completion: "complete", + }, +] as const; + +cliTest.skipIf(process.platform === "win32").for(outcomes)( + "preserves process facts and exposes command-validity uncertainty: $command", + async (outcome, { cli }) => { + const root = await createTestTempDirectory("rea-rizin-command-outcomes-"); + const provider = join(root, "rizin-fixture"); + const artifact = join(root, "artifact.bin"); + const content = "caller-selected inert artifact"; + await writeFile(artifact, content); + // Real Node subprocesses simulate producer output; they are not Rizin. + await writeFile( + provider, + `#!${process.execPath}\n` + + `const cases=${JSON.stringify(outcomes)};\n` + + "const selected=cases.find(item=>item.command===process.argv[process.argv.indexOf('-c')+1]);\n" + + "if(!selected)process.exit(99);\n" + + "process.stdout.write(selected.stdout);process.stderr.write(selected.stderr);\n", + ); + await chmod(provider, 0o755); + const environment = { REA_RIZIN_COMMAND: provider }; + const response = await cli.run({ + arguments: ["execute-rizin-command", artifact, outcome.command, "--json"], + environment, + }); + expect(response.exitCode, `${response.stdout}\n${response.stderr}`).toBe(0); + const { client, call } = await connectLocalToolsMcp(environment); + const definition = (await client.listTools()).tools.find( + ({ name }) => name === "execute_rizin_command", + ); + const mcp = await call("execute_rizin_command", { + path: artifact, + command: outcome.command, + }); + expect(mcp.isError, JSON.stringify(mcp.content)).not.toBe(true); + const contract = REVERSE_ENGINEERING_TOOL_CONTRACTS.find( + ({ name }) => name === "execute_rizin_command", + ); + if (!contract) throw new Error("Missing execute_rizin_command contract"); + const limitations = rizinCommandLimitations({ + path: artifact, + command: outcome.command, + }); + // Even unqualified commands must disclose the provider validity gap. + expect(limitations).not.toHaveLength(0); + for (const raw of [response.json, mcp.structuredContent]) { + const evidence = contract.outputSchema.parse(raw); + expect(evidence.normalized_result).toMatchObject({ + stdout: outcome.stdout, + stderr: outcome.stderr, + exit_code: 0, + completion_status: outcome.completion, + output_truncated: false, + artifact_changed: false, + }); + expect(evidence.raw_result).toEqual(evidence.normalized_result); + expect(evidence.subject?.digest.sha256).toBe( + createHash("sha256").update(content).digest("hex"), + ); + expect(evidence.parameters.command).toBe(outcome.command); + expect(evidence.limitations).toEqual( + expect.arrayContaining([...limitations]), + ); + } + expect(definition?.description).toBe(contract.description); + }, +); diff --git a/tests/acceptance/rizinCommandRejection.test.ts b/tests/acceptance/rizinCommandRejection.test.ts new file mode 100644 index 0000000000..0b2f5e8b3e --- /dev/null +++ b/tests/acceptance/rizinCommandRejection.test.ts @@ -0,0 +1,179 @@ +import { execFileSync } from "node:child_process"; +import { chmod, writeFile } from "node:fs/promises"; +import { join } from "node:path"; +import { expect } from "vitest"; +import { REVERSE_ENGINEERING_TOOL_CONTRACTS } from "../../src/contracts/reverseEngineeringToolContracts.js"; +import { connectLocalToolsMcp } from "../fixtures/localToolsMcp.js"; +import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js"; +import { cliTest } from "../support/cli/cliFixture.js"; + +const contract = REVERSE_ENGINEERING_TOOL_CONTRACTS.find( + ({ name }) => name === "execute_rizin_command", +); +if (contract === undefined) + throw new Error("execute_rizin_command contract is missing"); + +interface Outcome { + readonly command: string; + readonly stdout: string; + readonly stderr: string; + /** Expected completion and the refusal the limitation must name. */ + readonly refused?: RegExp; +} + +// Captured from the official Rizin 0.9.1 static Linux build on /usr/bin/true. +const outcomes: readonly Outcome[] = [ + { + command: "axffj", + stdout: "", + stderr: + "ERROR: Command 'axffj' does not exist.\nERROR: Displaying the help of command 'axf'.\n\nERROR: Usage: axf[jq] # List xrefs from current seek\n", + refused: /unknown command "axffj"/u, + }, + { + // Rizin runs the statements before a refusal and skips the rest. + command: "%v 1; zz; %v 2", + stdout: "0x1\n", + stderr: "ERROR: Command 'zz' does not exist.\n", + refused: /unknown command "zz"/u, + }, + { + command: "%v 1; afl 1 2; %v 2", + stdout: "0x1\n", + stderr: + "ERROR: Wrong number of arguments passed to `afl`, see its help with `afl?`\n\nERROR: Usage: afl[?] # List functions\n", + refused: /wrong argument count for "afl"/u, + }, + { + command: "pd 1 @ nosuch", + stdout: "", + stderr: + "ERROR: core: Invalid address (nosuch)\nERROR: core: Error while executing command: pd 1 @ nosuch\n", + refused: /an invalid statement/u, + }, + { + command: "?e", + stdout: "", + stderr: "ERROR: core: Error while parsing command: `?e`\n", + refused: /input that failed to parse/u, + }, + // A command that ran and reported its own failure is not a shell refusal. + { + command: "afxj", + stdout: "", + stderr: "ERROR: No function found in 0x000023d0.\n", + }, + // Refusal text on stdout is command output, not a Rizin diagnostic. + { + command: "echo refusal", + stdout: "ERROR: Command 'axffj' does not exist.\n", + stderr: "", + }, +]; + +const expectClassified = (raw: unknown, outcome: Outcome): void => { + const evidence = contract.outputSchema.parse(raw); + expect(evidence.normalized_result).toMatchObject({ + stdout: outcome.stdout, + stderr: outcome.stderr, + exit_code: 0, + completion_status: outcome.refused === undefined ? "complete" : "rejected", + }); + expect(evidence.raw_result).toEqual(evidence.normalized_result); + const refusals = evidence.limitations.filter((limitation) => + limitation.startsWith("Rizin refused "), + ); + if (outcome.refused === undefined) expect(refusals).toEqual([]); + else { + expect(refusals).toHaveLength(1); + expect(refusals[0]).toMatch(outcome.refused); + expect(refusals[0]).toMatch(/empty stdout is not an empty result/u); + } +}; + +cliTest.skipIf(process.platform === "win32")( + "reports Rizin shell refusals as rejected through CLI and MCP (simulated Rizin)", + async ({ cli }) => { + const root = await createTestTempDirectory("rea-rizin-rejection-"); + const provider = join(root, "rizin-fixture"); + const artifact = join(root, "artifact.bin"); + await writeFile(artifact, "caller-selected inert artifact"); + // A Node script replays recorded Rizin output; it is not Rizin. + await writeFile( + provider, + `#!${process.execPath}\n` + + `const cases=${JSON.stringify(outcomes)};\n` + + "const selected=cases.find(item=>item.command===process.argv[process.argv.indexOf('-c')+1]);\n" + + "if(!selected)process.exit(99);\n" + + "process.stdout.write(selected.stdout);process.stderr.write(selected.stderr);\n", + ); + await chmod(provider, 0o755); + const environment = { REA_RIZIN_COMMAND: provider }; + const { call } = await connectLocalToolsMcp(environment); + // Both adapters share the service's classification; the CLI checks one + // refusal and one command diagnostic. + for (const outcome of [outcomes[0]!, outcomes[5]!]) { + const response = await cli.run({ + arguments: [ + "execute-rizin-command", + artifact, + outcome.command, + "--json", + ], + environment, + }); + expect(response.exitCode, `${response.stdout}\n${response.stderr}`).toBe( + 0, + ); + expectClassified(response.json, outcome); + } + for (const outcome of outcomes) { + const mcp = await call("execute_rizin_command", { + path: artifact, + command: outcome.command, + }); + expect(mcp.isError, JSON.stringify(mcp.content)).not.toBe(true); + expectClassified(mcp.structuredContent, outcome); + } + }, +); + +// Real-provider gate: runs the Rizin found on PATH and skips without one. +const rizin = (() => { + try { + const path = execFileSync("which", ["rizin"], { encoding: "utf8" }).trim(); + return path === "" ? undefined : path; + } catch { + return undefined; + } +})(); + +cliTest.skipIf(rizin === undefined)( + "classifies real Rizin refusals and command diagnostics", + async () => { + const root = await createTestTempDirectory("rea-real-rizin-rejection-"); + const artifact = join(root, "artifact.bin"); + await writeFile(artifact, Buffer.from([0x90, 0xc3])); + const { call } = await connectLocalToolsMcp({ REA_RIZIN_COMMAND: rizin! }); + const expected = [ + ["axffj", "rejected"], + ["%v 1; zz; %v 2", "rejected"], + ["%v 1; afl 1 2; %v 2", "rejected"], + ["pd 1 @ nosuch", "rejected"], + ["s nosuch", "complete"], + ["%v 1; %v 2", "complete"], + ] as const; + for (const [command, status] of expected) { + const response = await call("execute_rizin_command", { + path: artifact, + command, + }); + expect(response.isError, JSON.stringify(response.content)).not.toBe(true); + const evidence = contract.outputSchema.parse(response.structuredContent); + expect(evidence.normalized_result, command).toMatchObject({ + exit_code: 0, + completion_status: status, + }); + } + }, +); diff --git a/tests/acceptance/rustBinaryCli.test.ts b/tests/acceptance/rustBinaryCli.test.ts index d9f44a0ef6..f9f22e94ca 100644 --- a/tests/acceptance/rustBinaryCli.test.ts +++ b/tests/acceptance/rustBinaryCli.test.ts @@ -6,7 +6,6 @@ import { pathToFileURL } from "node:url"; import { join } from "node:path"; import { expect } from "vitest"; import { toolContract } from "../../src/contracts/toolContracts.js"; -import { RUST_RESOURCE_GUARD_LIMITATION } from "../../src/rust/RustBinaryProvider.js"; import { machoImageWithUnknownCommand } from "../../src/artifacts/apple/MachoImage.fixture.js"; import { compressRustElfSectionNames, @@ -17,7 +16,7 @@ import { rustDependencyPayload, } from "../fixtures/rust/image.js"; import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js"; -import { cliTest, runFileCli } from "../support/cli/cliFixture.js"; +import { cliTest } from "../support/cli/cliFixture.js"; import { connectLocalToolsMcp } from "../fixtures/localToolsMcp.js"; import { expectCliMcpEvidenceParity } from "../fixtures/cliMcpEvidenceParity.js"; import { parseMcpToolError } from "../fixtures/mcpToolError.js"; @@ -75,7 +74,6 @@ cliTest( } await client.ping(); }, - 30000, ); cliTest( @@ -189,7 +187,7 @@ cliTest( }), ); if (surface === "cli") { - const response = await runFileCli(cli, { + const response = await cli.run({ arguments: ["inspect-rust-binary", path, "--json"], }); expect(response.exitCode, response.stderr).toBe(0); @@ -214,7 +212,7 @@ cliTest( } if (surface === "cli") { - const response = await runFileCli(cli, { + const response = await cli.run({ arguments: ["inspect-rust-binary", malformedPath, "--json"], }); expect(response.exitCode).toBe(1); @@ -241,14 +239,13 @@ cliTest( } await client.ping(); }, - 30000, ); cliTest("returns null metadata when .dep-v0 is absent", async ({ cli }) => { const root = await createTestTempDirectory("rea-rust-cli-missing-"); const path = join(root, "plain"); await writeFile(path, createRustImage({ format: "elf", omitSection: true })); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: ["inspect-rust-binary", path, "--json"], }); expect(result.exitCode, result.stderr).toBe(0); @@ -281,7 +278,7 @@ cliTest( fixture.type, ), ); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: ["inspect-rust-binary", path, "--json"], }); expect(result.exitCode, result.stderr).toBe(0); @@ -327,7 +324,7 @@ cliTest( bytes.writeUInt32LE(0x0200000c, 4); await writeFile(path, bytes); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: ["inspect-rust-binary", path, "--json"], }); @@ -359,7 +356,7 @@ cliTest( ], }); await writeFile(path, createRustImage({ format: "elf", payload })); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: ["inspect-rust-binary", path, "--json"], }); expect(result.exitCode, result.stderr).toBe(0); @@ -388,7 +385,7 @@ cliTest("rejects malformed dependency JSON", async ({ cli }) => { includeRustc: false, }), ); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: ["inspect-rust-binary", path, "--json"], }); expect(result.exitCode).toBe(1); @@ -413,7 +410,7 @@ cliTest( }), ); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: ["inspect-rust-binary", path, "--json"], }); expect(result.exitCode).toBe(1); @@ -428,7 +425,6 @@ cliTest( }); await client.ping(); }, - 30000, ); cliTest( @@ -446,7 +442,7 @@ cliTest( bytes.writeUInt32LE(alignedSize, 264 + 16); // .dep-v0 SizeOfRawData await writeFile(path, bytes); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: ["inspect-rust-binary", path, "--json"], }); expect(result.exitCode, result.stderr).toBe(0); @@ -469,7 +465,13 @@ cliTest( ); // Padding separates the section size from the consumed stream that the // inflate guidance names. - expect(evidence.limitations).toContain(RUST_RESOURCE_GUARD_LIMITATION); + expect(evidence.limitations).toEqual( + expect.arrayContaining([ + expect.stringMatching( + /inflated JSON.*64 times the consumed compressed-stream length/iu, + ), + ]), + ); expect(evidence.locations).toContainEqual({ kind: "file-offset-range", start: metadata?.file_offset, @@ -480,7 +482,6 @@ cliTest( expectCliMcpEvidenceParity(evidence, response.structuredContent, path); await client.ping(); }, - 30000, ); cliTest( @@ -518,7 +519,7 @@ cliTest( reported_limits: { maximum_bytes: compressed.length * 64 }, }, }; - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: ["inspect-rust-binary", path, "--json"], }); expect(result.exitCode).toBe(1); @@ -527,7 +528,6 @@ cliTest( expect(parseMcpToolError(response).error).toMatchObject(expectedFailure); await client.ping(); }, - 30000, ); cliTest( @@ -625,7 +625,7 @@ cliTest( } const path = join(root, `mixed.${format}`); await writeFile(path, bytes); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: ["inspect-rust-binary", path, "--json"], }); expect(result.exitCode, result.stderr).toBe(0); @@ -656,7 +656,7 @@ cliTest( const response = await call("inspect_rust_binary", { path }); expect(response.isError, JSON.stringify(response)).not.toBe(true); expectCliMcpEvidenceParity(evidence, response.structuredContent, path); - const go = await runFileCli(cli, { + const go = await cli.run({ arguments: ["inspect-go-binary", path, "--json"], }); expect(go.exitCode).toBe(1); @@ -684,7 +684,6 @@ cliTest( } await client.ping(); }, - 30000, ); cliTest( diff --git a/tests/acceptance/rustDependencyViews.test.ts b/tests/acceptance/rustDependencyViews.test.ts index c7802653ec..612d5d2aff 100644 --- a/tests/acceptance/rustDependencyViews.test.ts +++ b/tests/acceptance/rustDependencyViews.test.ts @@ -309,7 +309,6 @@ cliTest( ).toMatchObject({ code: "invalid_request" }); } }, - 60000, ); cliTest( @@ -451,5 +450,4 @@ cliTest( } expect((await readFile(path)).length).toBeGreaterThan(0); }, - 30000, ); diff --git a/tests/acceptance/rustUnnamedSections.test.ts b/tests/acceptance/rustUnnamedSections.test.ts index b101d221f9..4809e3e279 100644 --- a/tests/acceptance/rustUnnamedSections.test.ts +++ b/tests/acceptance/rustUnnamedSections.test.ts @@ -26,7 +26,7 @@ cliTest( if (name === "writable") bytes.writeBigUInt64LE(3n, 384 + 8); const path = join(root, `${name}.elf`); await writeFile(path, bytes); - const output = await cli.sharedCliRun!({ + const output = await cli.run({ arguments: ["inspect-rust-binary", path, "--json"], }); expect(output.exitCode, output.stderr).toBe(0); diff --git a/tests/acceptance/setup/dshClientSetup.test.ts b/tests/acceptance/setup/dshClientSetup.test.ts index c59b94d711..3a900b5b1f 100644 --- a/tests/acceptance/setup/dshClientSetup.test.ts +++ b/tests/acceptance/setup/dshClientSetup.test.ts @@ -200,7 +200,6 @@ cliTest( arguments: [...args, "--json"], cwd: home, environment, - timeoutMs: 20_000, }); const plan = await run(["setup", "--client", "dsh", "--dry-run"]); expect(plan.exitCode).toBe(0); diff --git a/tests/acceptance/setup/geminiWorkspaceTrust.test.ts b/tests/acceptance/setup/geminiWorkspaceTrust.test.ts index 32d4711cff..fef2a13d58 100644 --- a/tests/acceptance/setup/geminiWorkspaceTrust.test.ts +++ b/tests/acceptance/setup/geminiWorkspaceTrust.test.ts @@ -31,9 +31,61 @@ const geminiWorkspace = async (cli: TestCli) => { const originalUser = await readFile(user); const run = (arguments_: readonly string[]) => cli.run({ arguments: arguments_, cwd: project, environment }); - return { workspace, user, originalUser, environment, run }; + return { project, workspace, user, originalUser, environment, run }; }; +for (const scenario of [ + ...["/", "/."].flatMap((suffix) => + [false, true].map((trusted) => ({ + name: `the final normalized rule (${suffix}, trusted: ${String(trusted)})`, + trusted, + rules: (project: string) => + Object.fromEntries([ + [`${project}${suffix}`, trusted ? "DO_NOT_TRUST" : "TRUST_FOLDER"], + [project, trusted ? "TRUST_FOLDER" : "DO_NOT_TRUST"], + ]), + })), + ), + ...["__proto__", "ordinary"].map((child) => ({ + name: `the relative TRUST_PARENT rule ${child}`, + trusted: true, + rules: () => Object.fromEntries([[child, "TRUST_PARENT"]]), + })), + { + name: "the more specific TRUST_PARENT key", + trusted: true, + rules: (project: string) => + Object.fromEntries([ + [project, "DO_NOT_TRUST"], + [join(project, "child"), "TRUST_PARENT"], + ]), + }, +]) + cliTest(`Gemini setup and doctor honor ${scenario.name}`, async ({ cli }) => { + const fixture = await geminiWorkspace(cli); + const trust = fixture.environment.GEMINI_CLI_TRUSTED_FOLDERS_PATH; + if (trust === undefined) throw new Error("Expected a trust-file path"); + const trustText = JSON.stringify(scenario.rules(fixture.project)); + const workspaceText = JSON.stringify({ mcp: { excluded: ["rea"] } }); + await writeFile(trust, trustText); + await writeFile(fixture.workspace, workspaceText); + + const setup = await fixture.run(setupArguments); + expect(setup.exitCode, setup.stdout).toBe(scenario.trusted ? 1 : 0); + expect(setup.json).toMatchObject({ + status: scenario.trusted ? "needs_human" : "ready", + appliedActions: [], + }); + const doctor = await fixture.run(doctorArguments); + expect(doctor.exitCode, doctor.stdout).toBe(scenario.trusted ? 1 : 0); + expect(doctor.json).toMatchObject({ healthy: !scenario.trusted }); + expect(await readFile(trust, "utf8")).toBe(trustText); + expect(await readFile(fixture.workspace, "utf8")).toBe(workspaceText); + expect( + Buffer.compare(await readFile(fixture.user), fixture.originalUser), + ).toBe(0); + }); + cliTest( "Gemini does not require workspace trust until workspace settings exist", async ({ cli }) => { diff --git a/tests/acceptance/setup/hermesAliasedTableSetup.test.ts b/tests/acceptance/setup/hermesAliasedTableSetup.test.ts index 6b8a913be2..031917a870 100644 --- a/tests/acceptance/setup/hermesAliasedTableSetup.test.ts +++ b/tests/acceptance/setup/hermesAliasedTableSetup.test.ts @@ -50,7 +50,6 @@ for (const version of ["1.1", "1.2"] as const) { HERMES_HOME: profile, HOPPER_LAUNCHER_PATH: process.execPath, }, - timeoutMs: 20_000, }); expect(removed.exitCode).toBe(0); expect(await readFile(config, "utf8")).toBe(original); @@ -84,7 +83,6 @@ for (const version of ["1.1", "1.2"] as const) { GHIDRA_INSTALL_DIR: ghidra, JAVA_HOME: java, }, - timeoutMs: 20_000, }); const setup = [ "setup", @@ -166,7 +164,6 @@ cliTest("Hermes still registers into an explicit table", async ({ cli }) => { HERMES_HOME: profile, HOPPER_LAUNCHER_PATH: process.execPath, }, - timeoutMs: 20_000, }); expect(applied.exitCode).toBe(0); expect(applied.json).toMatchObject({ status: "ready" }); @@ -209,7 +206,6 @@ for (const version of ["1.1", "1.2"] as const) { HERMES_HOME: profile, HOPPER_LAUNCHER_PATH: process.execPath, }, - timeoutMs: 20_000, }); expect(applied.exitCode).toBe(0); const configured = parse(await readFile(config, "utf8"), { version }); @@ -237,7 +233,6 @@ cliTest( HERMES_HOME: profile, HOPPER_LAUNCHER_PATH: process.execPath, }, - timeoutMs: 20_000, }); const [command, ...args] = currentClientRegistrationCommand({ entrypointPath: resolve("scripts/rea.mjs"), @@ -316,7 +311,6 @@ for (const original of [ HERMES_HOME: profile, HOPPER_LAUNCHER_PATH: process.execPath, }, - timeoutMs: 20_000, }); expect(rejected.exitCode).not.toBe(0); expect(rejected.json).toMatchObject({ diff --git a/tests/acceptance/setup/hermesAnchoredEntrySetup.test.ts b/tests/acceptance/setup/hermesAnchoredEntrySetup.test.ts index 92c65b2276..121cebf93b 100644 --- a/tests/acceptance/setup/hermesAnchoredEntrySetup.test.ts +++ b/tests/acceptance/setup/hermesAnchoredEntrySetup.test.ts @@ -23,7 +23,6 @@ const cliLifecycle = (cli: TestCli, home: string, profile: string) => { arguments: arguments_, cwd: home, environment: { HOME: home, USERPROFILE: home, HERMES_HOME: profile }, - timeoutMs: 20_000, }); const setup = [ "setup", diff --git a/tests/acceptance/setup/hermesClientSetup.test.ts b/tests/acceptance/setup/hermesClientSetup.test.ts index 00c45c74d0..c140a32298 100644 --- a/tests/acceptance/setup/hermesClientSetup.test.ts +++ b/tests/acceptance/setup/hermesClientSetup.test.ts @@ -61,7 +61,6 @@ for (const [version, keyStyle] of [ arguments: arguments_, cwd: home, environment: { USERPROFILE: home, HERMES_HOME: profile }, - timeoutMs: 20_000, }); const setup = [ "setup", @@ -141,7 +140,6 @@ cliTest( arguments: arguments_, cwd: home, environment: { USERPROFILE: home, HERMES_HOME: profile }, - timeoutMs: 20_000, }); const setup = ["setup", "--client", "hermes", "--skill=false", "--json"]; const preview = await run([...setup, "--dry-run"]); @@ -223,7 +221,6 @@ cliTest( REA_CLIENT_PROFILE_HOME: home, HERMES_HOME: " ${REA_CLIENT_PROFILE_HOME}/profile ", }, - timeoutMs: 20_000, }); const planned = await run([ "setup", @@ -333,7 +330,6 @@ for (const selection of ["default", "custom", "suffix", "explicit"] as const) { ? root : undefined, }, - timeoutMs: 20_000, }); const plan = await run([ "setup", @@ -468,7 +464,6 @@ for (const selection of [ LOCALAPPDATA: join(home, "AppData", "Local"), HERMES_HOME: root, }, - timeoutMs: 20_000, }); if (selection === "missing") { expect(plan.exitCode).toBe(1); @@ -573,7 +568,6 @@ for (const layout of ["symlinked-default", "escaping-prefix"] as const) { LOCALAPPDATA: join(home, "AppData", "Local"), HERMES_HOME: root, }, - timeoutMs: 20_000, }); expect(plan.exitCode, plan.stdout).toBe(0); expect(plan.json).toMatchObject({ @@ -650,7 +644,6 @@ for (const name of ["missing", "deleted", "ghost", "../escape", "root"]) { arguments: args, cwd: home, environment: { USERPROFILE: home, HERMES_HOME: root }, - timeoutMs: 20_000, }); const rejected = await run([ "setup", @@ -720,7 +713,6 @@ cliTest.skipIf(process.platform === "win32")( HERMES_HOME: "$REA_HERMES_PROFILE", REA_HERMES_PROFILE: override, }, - timeoutMs: 20_000, }); const planned = await run([ "setup", @@ -783,7 +775,6 @@ cliTest( arguments: arguments_, cwd: home, environment: { USERPROFILE: home, HERMES_HOME: override }, - timeoutMs: 20_000, }); const setup = await run(["setup", "--client", "hermes", "--yes", "--json"]); expect(setup.exitCode).not.toBe(0); diff --git a/tests/acceptance/setup/launcherAliasesCli.test.ts b/tests/acceptance/setup/launcherAliasesCli.test.ts index 8270a3b622..8864b3923f 100644 --- a/tests/acceptance/setup/launcherAliasesCli.test.ts +++ b/tests/acceptance/setup/launcherAliasesCli.test.ts @@ -161,7 +161,6 @@ cliTest.skipIf(process.platform === "win32").for([ XDG_CONFIG_HOME: join(home, ".config"), PATH: process.env.PATH ?? "", }, - timeoutMs: 30_000, }); const setup = await run(registered, [ @@ -296,7 +295,6 @@ cliTest.skipIf(process.platform === "win32")( XDG_CONFIG_HOME: join(home, ".config"), PATH: process.env.PATH ?? "", }, - timeoutMs: 30_000, }); const uninstall = await run(["uninstall", "--json"]); expect(uninstall.exitCode, uninstall.stderr).toBe(0); diff --git a/tests/acceptance/setup/ompClientSetup.test.ts b/tests/acceptance/setup/ompClientSetup.test.ts index 1a1ddfa9c2..0cda7950a7 100644 --- a/tests/acceptance/setup/ompClientSetup.test.ts +++ b/tests/acceptance/setup/ompClientSetup.test.ts @@ -34,7 +34,6 @@ for (const [variable, profile] of [ PI_CODING_AGENT_DIR: agent, [variable]: profile, }, - timeoutMs: 20_000, }); for (const option of ["--dry-run", "--yes"]) { const rejected = await run( @@ -101,7 +100,6 @@ cliTest( OMP_PROFILE: " ", PI_PROFILE: "Bad Name", }, - timeoutMs: 20_000, }); expect(result.exitCode).toBe(0); expect(result.json).toMatchObject({ status: "ready" }); diff --git a/tests/acceptance/setup/piClientSetup.test.ts b/tests/acceptance/setup/piClientSetup.test.ts index 5939b4443a..2837db434f 100644 --- a/tests/acceptance/setup/piClientSetup.test.ts +++ b/tests/acceptance/setup/piClientSetup.test.ts @@ -138,7 +138,6 @@ cliTest( arguments: arguments_, cwd: home, environment, - timeoutMs: 20_000, }); expect(applied.exitCode).toBe(0); expect(applied.json).toMatchObject({ @@ -193,7 +192,6 @@ cliTest( arguments: arguments_, cwd: home, environment, - timeoutMs: 20_000, }); expect(repeated.exitCode).toBe(0); expect(repeated.json).toMatchObject({ @@ -232,7 +230,6 @@ cliTest( PI_PROFILE: "legacy", PI_CONFIG_DIR: ".omp-other", }, - timeoutMs: 20_000, }); const planned = await run([ @@ -345,7 +342,6 @@ cliTest( HOPPER_LAUNCHER_PATH: join(home, "unconfigured-hopper"), PI_CODING_AGENT_DIR: "custom-agent", }, - timeoutMs: 20_000, }); const run = runIn(created); const setup = await run(["setup", "--client", "pi", "--yes", "--json"]); @@ -456,7 +452,6 @@ cliTest( PI_CODING_AGENT_DIR: "file:///agent%2fdir", REA_DARWIN_MODULE_CACHE_PATH: moduleCache, }, - timeoutMs: 20_000, }); const plan = await run([ "setup", diff --git a/tests/acceptance/setup/qwenProfileSetup.test.ts b/tests/acceptance/setup/qwenProfileSetup.test.ts index c14c915131..17855e78dc 100644 --- a/tests/acceptance/setup/qwenProfileSetup.test.ts +++ b/tests/acceptance/setup/qwenProfileSetup.test.ts @@ -86,5 +86,4 @@ cliTest( code: "ENOENT", }); }, - 120_000, ); diff --git a/tests/acceptance/setup/registrationRuntimeEnvironmentCli.test.ts b/tests/acceptance/setup/registrationRuntimeEnvironmentCli.test.ts index 25de44f969..e9f2a777a0 100644 --- a/tests/acceptance/setup/registrationRuntimeEnvironmentCli.test.ts +++ b/tests/acceptance/setup/registrationRuntimeEnvironmentCli.test.ts @@ -139,7 +139,6 @@ cliTest( }); expect(await readFile(config, "utf8")).toBe(updatedConfiguration); }, - 120_000, ); cliTest( @@ -767,7 +766,6 @@ cliTest( }, }); }, - 120_000, ); for (const [client, provider] of [ @@ -903,7 +901,6 @@ for (const [client, provider] of [ }); } }, - 120_000, ); for (const invalidSetting of [ @@ -955,7 +952,6 @@ for (const invalidSetting of [ code: "ENOENT", }); }, - 120_000, ); cliTest( @@ -1018,5 +1014,4 @@ cliTest( code: "ENOENT", }); }, - 120_000, ); diff --git a/tests/acceptance/setup/updateAliasCli.test.ts b/tests/acceptance/setup/updateAliasCli.test.ts index 7587c4638b..f4ba6bf367 100644 --- a/tests/acceptance/setup/updateAliasCli.test.ts +++ b/tests/acceptance/setup/updateAliasCli.test.ts @@ -46,7 +46,6 @@ cliTest( cli.run({ arguments: arguments_, cwd: home, - timeoutMs: 20_000, environment: { USERPROFILE: home, PATH: [ diff --git a/tests/acceptance/sqliteDatabaseCli.test.ts b/tests/acceptance/sqliteDatabaseCli.test.ts index 25a7e6de34..0e7a0b1834 100644 --- a/tests/acceptance/sqliteDatabaseCli.test.ts +++ b/tests/acceptance/sqliteDatabaseCli.test.ts @@ -33,7 +33,7 @@ import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js"; import { cliTest, createSharedMcpSession, - runFileCli, + type TestCli, } from "../support/cli/cliFixture.js"; const digest = (bytes: Buffer): string => @@ -49,11 +49,8 @@ const parityMcp = createSharedMcpSession(async (registerCleanup, isClosed) => { afterAll(() => parityMcp.close()); /** Run the offset page through the CLI so the option name is covered as well as the MCP input. */ -const pagedCliRows = async ( - cli: Parameters[0], - path: string, -): Promise => { - const result = await runFileCli(cli, { +const pagedCliRows = async (cli: TestCli, path: string): Promise => { + const result = await cli.run({ arguments: [ "inspect-sqlite-database", path, @@ -168,7 +165,7 @@ cliTest.runIf(SQLITE_NATIVE_LIMITS_AVAILABLE)( `${base}[0,1].value,${base}.1.value`, `${base}.1.value,${base}[0,1].value`, ]) { - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: [ "inspect-sqlite-database", path, @@ -187,7 +184,7 @@ cliTest.runIf(SQLITE_NATIVE_LIMITS_AVAILABLE)( }, }); } - const refused = await runFileCli(cli, { + const refused = await cli.run({ arguments: [ "inspect-sqlite-database", path, @@ -220,7 +217,7 @@ cliTest.runIf(SQLITE_NATIVE_LIMITS_AVAILABLE)( } finally { database.close(); } - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: [ "inspect-sqlite-database", path, @@ -506,7 +503,7 @@ cliTest.runIf(SQLITE_NATIVE_LIMITS_AVAILABLE)( }, }, }); - const cliResult = await runFileCli(cli, { + const cliResult = await cli.run({ arguments: [ "inspect-sqlite-database", path, @@ -672,7 +669,7 @@ cliTest.runIf(SQLITE_NATIVE_LIMITS_AVAILABLE)( } const { call } = await connectLocalToolsMcp(); for (const limit of [1001, Number.MAX_SAFE_INTEGER]) { - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: [ "inspect-sqlite-database", path, @@ -896,7 +893,7 @@ cliTest.runIf(SQLITE_NATIVE_LIMITS_AVAILABLE)( const wal = before[1]; expect(db).toBeDefined(); expect(wal).toBeDefined(); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: [ "inspect-sqlite-database", path, @@ -962,7 +959,7 @@ cliTest.runIf(SQLITE_NATIVE_LIMITS_AVAILABLE)( ], }, }; - const corrupt = await runFileCli(cli, { + const corrupt = await cli.run({ arguments: ["inspect-sqlite-database", selected, "--json"], environment: { REA_LOG_LEVEL: "silent" }, }); @@ -998,7 +995,7 @@ cliTest.runIf(SQLITE_NATIVE_LIMITS_AVAILABLE)( }, }, }); - const ambiguous = await runFileCli(cli, { + const ambiguous = await cli.run({ arguments: [ "inspect-sqlite-database", valid, @@ -1018,7 +1015,7 @@ cliTest.runIf(SQLITE_NATIVE_LIMITS_AVAILABLE)( }) ).isError, ).toBe(true); - const missing = await runFileCli(cli, { + const missing = await cli.run({ arguments: [ "inspect-sqlite-database", valid, @@ -1068,7 +1065,7 @@ cliTest.runIf(SQLITE_NATIVE_LIMITS_AVAILABLE)( producer.close(); } const before = await sourceState(path); - const control = await runFileCli(cli, { + const control = await cli.run({ arguments: [ "inspect-sqlite-database", path, @@ -1098,7 +1095,7 @@ cliTest.runIf(SQLITE_NATIVE_LIMITS_AVAILABLE)( const columns = Array.from({ length: selectedColumns }, () => "value"); // The framed fixture executes the compiled public CLI without the host's // process-argument ceiling rejecting this many repeated flags first. - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: [ "inspect-sqlite-database", path, @@ -1155,7 +1152,7 @@ cliTest.runIf(SQLITE_NATIVE_LIMITS_AVAILABLE)( const path = createSqliteApplicationCollationFixture(root); const before = await sourceState(path); const { call } = await connectLocalToolsMcp(); - const schema = await runFileCli(cli, { + const schema = await cli.run({ arguments: [ "inspect-sqlite-database", path, @@ -1194,7 +1191,7 @@ cliTest.runIf(SQLITE_NATIVE_LIMITS_AVAILABLE)( expect(schemaResponse.isError).not.toBe(true); expect(schemaResponse.structuredContent).toMatchObject(expectedSchema); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: [ "inspect-sqlite-database", path, @@ -1269,7 +1266,7 @@ cliTest.runIf(SQLITE_NATIVE_LIMITS_AVAILABLE)( }, }; for (const columns of [["b", "a"], undefined]) { - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: [ "inspect-sqlite-database", path, @@ -1290,7 +1287,7 @@ cliTest.runIf(SQLITE_NATIVE_LIMITS_AVAILABLE)( }); expect(parseMcpToolError(response).error).toMatchObject(expectedError); } - const base = await runFileCli(cli, { + const base = await cli.run({ arguments: [ "inspect-sqlite-database", path, @@ -1347,7 +1344,7 @@ cliTest.runIf(SQLITE_NATIVE_LIMITS_AVAILABLE)( "selected_table", "database", ] as const) { - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: [ "inspect-sqlite-database", path, @@ -1400,7 +1397,7 @@ cliTest.runIf(SQLITE_NATIVE_LIMITS_AVAILABLE)( ["UTF-16be", "database"], ] as const) { const { path, names } = createSqliteLongNameFixture(root, encoding); - const response = await runFileCli(cli, { + const response = await cli.run({ arguments: [ "inspect-sqlite-database", path, @@ -1492,7 +1489,7 @@ cliTest.runIf(SQLITE_NATIVE_LIMITS_AVAILABLE)( native.close(); } - const cliResult = await runFileCli(cli, { + const cliResult = await cli.run({ arguments: [ "inspect-sqlite-database", path, @@ -1571,7 +1568,7 @@ cliTest.runIf(SQLITE_NATIVE_LIMITS_AVAILABLE)( producer.close(); } const original = await readFile(path); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: ["inspect-sqlite-database", path, "--json"], environment: { REA_LOG_LEVEL: "silent" }, }); diff --git a/tests/acceptance/tools/harBoundaryFacts.test.ts b/tests/acceptance/tools/harBoundaryFacts.test.ts index 398c04347f..2bb420c45c 100644 --- a/tests/acceptance/tools/harBoundaryFacts.test.ts +++ b/tests/acceptance/tools/harBoundaryFacts.test.ts @@ -9,7 +9,7 @@ import { historicalHar } from "../../fixtures/historicalHar.js"; import { connectLocalToolsMcp } from "../../fixtures/localToolsMcp.js"; import { parseMcpToolError } from "../../fixtures/mcpToolError.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; -import { cliTest, runFileCli } from "../../support/cli/cliFixture.js"; +import { cliTest } from "../../support/cli/cliFixture.js"; cliTest( "omits HAR extension payloads from capture previews while retaining complete records", @@ -166,10 +166,10 @@ cliTest( "--json", ], }; - const output = - label === "null-timings" - ? await cli.run(invocation) - : await runFileCli(cli, invocation); + const output = await cli.run({ + ...invocation, + freshProcess: label === "null-timings", + }); expect(output.exitCode).toBe(1); const expected = { code: "invalid_request", diff --git a/tests/acceptance/tools/historicalCaptureResources.test.ts b/tests/acceptance/tools/historicalCaptureResources.test.ts index d15cc08f9b..2b4adf2814 100644 --- a/tests/acceptance/tools/historicalCaptureResources.test.ts +++ b/tests/acceptance/tools/historicalCaptureResources.test.ts @@ -11,8 +11,9 @@ import { fileURLToPath } from "node:url"; import { promisify } from "node:util"; import { expect, it } from "vitest"; -import { captureDecoderProcessMemoryLimitBytes } from "../../../src/browser/history/CaptureRelease.js"; +import { javaScriptWorkerMemoryLimitBytes } from "../../../src/process/JavaScriptWorkerMemoryLimit.js"; import { WEB_NETWORK_CAPTURE_LIMITS } from "../../../src/domain/webNetworkCapture.js"; +import { CAPTURE_RUNTIME_SUPPORTED } from "../../fixtures/captureDecoderLimits.js"; import { connectLocalToolsMcp } from "../../fixtures/localToolsMcp.js"; import { parseMcpToolError } from "../../fixtures/mcpToolError.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; @@ -53,10 +54,11 @@ const capture = (count: number) => // the decoder's Job Object commit limit is the enforced bound instead, and // Bun elsewhere has no bound this capture can reach. const testHeap = { oldGenerationMiB: 64, source: "setting" } as const; -const jobLimit = captureDecoderProcessMemoryLimitBytes( - testHeap.oldGenerationMiB, -); -it.skipIf(process.versions.bun !== undefined && jobLimit === null)( +const jobLimit = javaScriptWorkerMemoryLimitBytes(testHeap.oldGenerationMiB); +it.skipIf( + !CAPTURE_RUNTIME_SUPPORTED || + (process.versions.bun !== undefined && jobLimit === null), +)( "reports real HAR memory exhaustion through CLI and MCP and preserves server usability", async () => { const root = await createTestTempDirectory("rea-har-capacity-"); @@ -192,5 +194,4 @@ it.skipIf(process.versions.bun !== undefined && jobLimit === null)( error: { code: "invalid_request" }, }); }, - 60_000, ); diff --git a/tests/acceptance/tools/inspectorFileUrlIdentity.test.ts b/tests/acceptance/tools/inspectorFileUrlIdentity.test.ts index a465ce02b8..619866d86e 100644 --- a/tests/acceptance/tools/inspectorFileUrlIdentity.test.ts +++ b/tests/acceptance/tools/inspectorFileUrlIdentity.test.ts @@ -131,4 +131,4 @@ it("preserves file URL scheme spelling and script identity after real Inspector expect(original.predicate_type).toBe(first.predicate_type); expect(original.normalized_result).toEqual(first.normalized_result); expect(await readFile(source, "utf8")).toBe(sourceText); -}, 30_000); +}); diff --git a/tests/acceptance/tools/inspectorLifecycle.test.ts b/tests/acceptance/tools/inspectorLifecycle.test.ts index 02aa53cf14..cebeef7db7 100644 --- a/tests/acceptance/tools/inspectorLifecycle.test.ts +++ b/tests/acceptance/tools/inspectorLifecycle.test.ts @@ -122,4 +122,4 @@ it("observes a real Node Inspector and releases attachments after cancellation a details: { reason: "endpoint_unreachable" }, }, }); -}, 30_000); +}); diff --git a/tests/acceptance/tools/localToolContracts.test.ts b/tests/acceptance/tools/localToolContracts.test.ts index 2b4b29446e..2eec0250cf 100644 --- a/tests/acceptance/tools/localToolContracts.test.ts +++ b/tests/acceptance/tools/localToolContracts.test.ts @@ -413,7 +413,7 @@ it("rejects OS-invalid local inputs through the real advertised MCP contracts", ], }, }); -}, 60_000); +}); const ignoredOptions = [ { terminal: { columns: 80, row: 12 } }, @@ -583,9 +583,6 @@ captureTest( }, }); }, - // 22 malformed requests plus two filesystem errors each start a fresh CLI. - // Cache compilation, but allow for startup at the bounded fanout of two. - 120_000, ); captureTest.each(["untruncated", "truncated"] as const)( diff --git a/tests/acceptance/tools/mobileGraphExamples.test.ts b/tests/acceptance/tools/mobileGraphExamples.test.ts index c30b13c3f7..7eeb4eff33 100644 --- a/tests/acceptance/tools/mobileGraphExamples.test.ts +++ b/tests/acceptance/tools/mobileGraphExamples.test.ts @@ -12,7 +12,7 @@ import { import { createEvidence, parseEvidence } from "../../../src/domain/evidence.js"; import { artifactInventoryResultSchema } from "../../../src/domain/artifactGraph.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; -import { cliTest, runFileCli } from "../../support/cli/cliFixture.js"; +import { cliTest } from "../../support/cli/cliFixture.js"; const graphExamples = [ { @@ -112,10 +112,10 @@ cliTest.for(graphExamples)( const invocation = { arguments: [command, JSON.stringify(input), "--json"], }; - const result = - code === "evidence_integrity_mismatch" - ? await cli.run(invocation) - : await runFileCli(cli, invocation); + const result = await cli.run({ + ...invocation, + freshProcess: code === "evidence_integrity_mismatch", + }); expect(result.exitCode).toBe(1); expect(result.json).toMatchObject({ code, diff --git a/tests/acceptance/tools/nativeFunctionMatching.test.ts b/tests/acceptance/tools/nativeFunctionMatching.test.ts index 50bab5153b..d13cda3ee6 100644 --- a/tests/acceptance/tools/nativeFunctionMatching.test.ts +++ b/tests/acceptance/tools/nativeFunctionMatching.test.ts @@ -3,7 +3,7 @@ import { join } from "node:path"; import { expect } from "vitest"; import { cliTest, - runFileCli, + type CliInvocation, type TestCli, } from "../../support/cli/cliFixture.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; @@ -173,7 +173,7 @@ const verifyPortableCli = async ( ); const original = await readFile(bundlePath); await writeFile(inputPath, JSON.stringify({ ...input, detail: "complete" })); - const cliFull = await runFileCli(cli, { + const cliFull = await cli.run({ arguments: [ "match-native-functions", inputPath, @@ -187,7 +187,7 @@ const verifyPortableCli = async ( expect(cliFull.exitCode, cliFull.stderr).toBe(0); expect(parseEvidence(cliFull.json).evidence_id).toBe(evidenceId); await writeFile(inputPath, JSON.stringify(input)); - const cliSummary = await runFileCli(cli, { + const cliSummary = await cli.run({ arguments: [ "match-native-functions", inputPath, @@ -222,7 +222,7 @@ const verifyPortableCli = async ( expect(ids.has(unrelated.evidence_id)).toBe(false); expect(ids.has(evidenceId)).toBe(true); expect(Buffer.compare(await readFile(bundlePath), original)).toBe(0); - const incomplete = await runFileCli(cli, { + const incomplete = await cli.run({ arguments: [ "match-native-functions", inputPath, @@ -240,7 +240,6 @@ const verifyPortableCli = async ( cliTest.for([{ profile: "full" }, { profile: "compact" }] as const)( "shared public CLI/MCP matching retains sources, pages exact facts, and feeds selected dossier pairs directly to compare_functions ($profile)", - { timeout: 60000 }, async ({ profile }, { cli }) => { const fixture = await createMatchingFixture(); const { left, right, leftDetails, rightDetails, importedPath } = fixture; @@ -484,7 +483,6 @@ cliTest( retainedBefore.structuredContent, ); }, - 60000, ); cliTest( @@ -510,8 +508,8 @@ cliTest( "right_observations", ] as const) { // Keep fresh-process coverage for both source-admission routes on left. - const run = - field === "left" ? cli.run.bind(cli) : runFileCli.bind(undefined, cli); + const run = (invocation: CliInvocation) => + cli.run({ ...invocation, freshProcess: field === "left" }); const observation = field.endsWith("observations"); const request = { left, @@ -583,7 +581,7 @@ cliTest( detail: "complete", }; await writeFile(requestPath, JSON.stringify(inlineRequest)); - const inline = await runFileCli(cli, { + const inline = await cli.run({ arguments: ["match-native-functions", requestPath, "--json"], }); await writeFile( @@ -591,7 +589,7 @@ cliTest( JSON.stringify(createEvidenceBundle([wrongRole, right])), ); await writeFile(requestPath, JSON.stringify(portableRequest)); - const portable = await runFileCli(cli, { + const portable = await cli.run({ arguments: [ "match-native-functions", requestPath, @@ -613,7 +611,7 @@ cliTest( const corruptRequest = { left: badCoordinate, right, detail: "complete" }; await writeFile(requestPath, JSON.stringify(corruptRequest)); - const corruptInline = await runFileCli(cli, { + const corruptInline = await cli.run({ arguments: ["match-native-functions", requestPath, "--json"], }); const corruptPortableRequest = { @@ -625,7 +623,7 @@ cliTest( detail: "complete", }; await writeFile(requestPath, JSON.stringify(corruptPortableRequest)); - const corruptPortable = await runFileCli(cli, { + const corruptPortable = await cli.run({ arguments: [ "match-native-functions", requestPath, @@ -643,7 +641,7 @@ cliTest( const malformedBundlePath = join(root, "malformed-outer-frame.json"); await writeFile(malformedBundlePath, JSON.stringify({ records: [left] })); - const malformedBundle = await runFileCli(cli, { + const malformedBundle = await cli.run({ arguments: [ "match-native-functions", requestPath, @@ -657,7 +655,6 @@ cliTest( code: "evidence_integrity_mismatch", }); }, - 60000, ); cliTest( @@ -693,12 +690,10 @@ cliTest( before.structuredContent, ); }, - 60000, ); cliTest.for([{ profile: "full" }, { profile: "compact" }] as const)( "ties byte qualifications to selected observations through paged and offline MCP ($profile)", - { timeout: 60000 }, async ({ profile }) => { const left = nativeMatchingInventory("1", [ { address: "0x10", value: "left" }, @@ -831,7 +826,6 @@ cliTest.for([{ profile: "full" }, { profile: "compact" }] as const)( cliTest( "bounds byte-source pages and recovers an oversized source", - { timeout: 60000 }, async () => { const left = nativeMatchingInventory("1", [ { address: "0x10", value: "left" }, @@ -940,7 +934,6 @@ cliTest( cliTest( "oversized inline matching Evidence points back to the caller's original record", - { timeout: 60000 }, async () => { const left = nativeMatchingInventory("1", [ { address: "0x10", value: "left" }, diff --git a/tests/acceptance/tools/passiveCaptureCorrespondence.test.ts b/tests/acceptance/tools/passiveCaptureCorrespondence.test.ts index 6a2bb0705f..e2990fdd4b 100644 --- a/tests/acceptance/tools/passiveCaptureCorrespondence.test.ts +++ b/tests/acceptance/tools/passiveCaptureCorrespondence.test.ts @@ -17,7 +17,7 @@ import { withPassiveDroppedEvents, } from "../../fixtures/passiveNetworkCapture.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; -import { cliTest, runFileCli } from "../../support/cli/cliFixture.js"; +import { cliTest } from "../../support/cli/cliFixture.js"; type Capture = { inspection: CompareWebCapturesInput["before"]["inspection"]; @@ -516,9 +516,10 @@ cliTest.for(cases.filter(({ name }) => cliCaseNames.has(name)))( arguments: ["compare-web-captures", ...paths, "--json"], environment: { REA_LOG_LEVEL: "silent" }, }; - const response = await (scenario.name === "heterogeneous HTTP omission 0" - ? cli.run(invocation) - : runFileCli(cli, invocation)); + const response = await cli.run({ + ...invocation, + freshProcess: scenario.name === "heterogeneous HTTP omission 0", + }); expect(response.exitCode, response.stderr).toBe(0); expectComparison(response.json, scenario); }, diff --git a/tests/acceptance/tools/passiveNetworkInventory.test.ts b/tests/acceptance/tools/passiveNetworkInventory.test.ts index 22679d8a5f..4a68881335 100644 --- a/tests/acceptance/tools/passiveNetworkInventory.test.ts +++ b/tests/acceptance/tools/passiveNetworkInventory.test.ts @@ -10,7 +10,7 @@ import { withPassiveDroppedEvents, } from "../../fixtures/passiveNetworkCapture.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; -import { cliTest, runFileCli } from "../../support/cli/cliFixture.js"; +import { cliTest } from "../../support/cli/cliFixture.js"; const comparisonCases = () => { const before = passiveNetworkCapture(); @@ -191,9 +191,10 @@ cliTest.for(cases)( arguments: ["compare-web-captures", ...paths, "--json"], environment: { REA_LOG_LEVEL: "silent" }, }; - const response = await (name === "added" || name === "removed" - ? cli.run(options) - : runFileCli(cli, options)); + const response = await cli.run({ + ...options, + freshProcess: name === "added" || name === "removed", + }); expect(response.exitCode, response.stderr).toBe(0); expectComparison(response.json, name, change); expect( diff --git a/tests/acceptance/tools/protobufCapture.test.ts b/tests/acceptance/tools/protobufCapture.test.ts index 62de66e78e..c161b36352 100644 --- a/tests/acceptance/tools/protobufCapture.test.ts +++ b/tests/acceptance/tools/protobufCapture.test.ts @@ -9,7 +9,7 @@ import { } from "node:fs/promises"; import { join } from "node:path"; import { expect } from "vitest"; -import { cliTest, runFileCli } from "../../support/cli/cliFixture.js"; +import { cliTest } from "../../support/cli/cliFixture.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; import { protobufCaptureDescriptor, @@ -649,9 +649,10 @@ captureTest.for( "--json", ], }; - const response = await (name === "valid" - ? cli.run(options) - : runFileCli(cli, options)); + const response = await cli.run({ + ...options, + freshProcess: name === "valid", + }); expect(response.exitCode, response.stdout + response.stderr).toBe(0); const evidence = parseEvidence(response.json); const report = webNetworkCaptureSchema.parse(evidence.normalized_result); diff --git a/tests/acceptance/tools/protobufDescriptorFiles.test.ts b/tests/acceptance/tools/protobufDescriptorFiles.test.ts index 6b964a085a..f37f1fead7 100644 --- a/tests/acceptance/tools/protobufDescriptorFiles.test.ts +++ b/tests/acceptance/tools/protobufDescriptorFiles.test.ts @@ -147,7 +147,7 @@ cliTest( }, }; if (change === "declarations") { - const output = await cli.sharedCliRun!({ + const output = await cli.run({ arguments: [ "inspect-web-network-capture", capture, @@ -254,7 +254,7 @@ cliTest( const bytes = writer.finish(); const descriptor = join(root, `${mode}.pb`); await writeFile(descriptor, bytes); - const output = await cli.sharedCliRun!({ + const output = await cli.run({ arguments: [ "inspect-web-network-capture", capture, diff --git a/tests/acceptance/tools/protobufDescriptorIdentity.test.ts b/tests/acceptance/tools/protobufDescriptorIdentity.test.ts index 24ce91033a..d57476ad7e 100644 --- a/tests/acceptance/tools/protobufDescriptorIdentity.test.ts +++ b/tests/acceptance/tools/protobufDescriptorIdentity.test.ts @@ -132,7 +132,7 @@ cliTest.for([ const descriptor = join(root, `${variant}.pb`); const bytes = toBinary(FileDescriptorSetSchema, definition); await writeFile(descriptor, bytes); - const output = await cli.sharedCliRun!({ + const output = await cli.run({ arguments: [ "inspect-web-network-capture", capture, @@ -222,7 +222,7 @@ cliTest( const definition = toBinary(FileDescriptorSetSchema, valid); await writeFile(capture, protobufHar(body, "application/x-protobuf")); await writeFile(descriptor, definition); - const output = await cli.sharedCliRun!({ + const output = await cli.run({ arguments: [ "inspect-web-network-capture", capture, diff --git a/tests/acceptance/tools/protobufMimeAdmission.test.ts b/tests/acceptance/tools/protobufMimeAdmission.test.ts index c9d2066314..46b1cae62b 100644 --- a/tests/acceptance/tools/protobufMimeAdmission.test.ts +++ b/tests/acceptance/tools/protobufMimeAdmission.test.ts @@ -30,7 +30,7 @@ cliTest( const path = join(root, `${index}.har`); const original = protobufHar(body, mime); await writeFile(path, original); - const output = await cli.sharedCliRun!({ + const output = await cli.run({ arguments: [ "inspect-web-network-capture", path, @@ -109,7 +109,7 @@ cliTest( await writeFile(path, original); const call = await cli.sharedMcpCall!(); for (const framing of ["auto", "raw"] as const) { - const output = await cli.sharedCliRun!({ + const output = await cli.run({ arguments: [ "inspect-web-network-capture", path, @@ -233,7 +233,7 @@ cliTest( const mime = 'application/protobuf;encoding="binary'; const original = protobufHar(body, mime); await writeFile(path, original); - const output = await cli.sharedCliRun!({ + const output = await cli.run({ arguments: [ "inspect-web-network-capture", path, diff --git a/tests/acceptance/tools/protobufRegistryIdentity.test.ts b/tests/acceptance/tools/protobufRegistryIdentity.test.ts index f6cc0bb65b..6cb481def0 100644 --- a/tests/acceptance/tools/protobufRegistryIdentity.test.ts +++ b/tests/acceptance/tools/protobufRegistryIdentity.test.ts @@ -117,7 +117,7 @@ cliTest( // Both orders cross the actual CLI adapter; other geometries reuse // the production MCP session and the same application workflow. if (scope === "same-file") { - const output = await cli.sharedCliRun!({ + const output = await cli.run({ arguments: [ "inspect-web-network-capture", capture, diff --git a/tests/acceptance/tools/qlogCapture.test.ts b/tests/acceptance/tools/qlogCapture.test.ts index f492d987de..4bda480872 100644 --- a/tests/acceptance/tools/qlogCapture.test.ts +++ b/tests/acceptance/tools/qlogCapture.test.ts @@ -4,8 +4,10 @@ import { join } from "node:path"; import { fileURLToPath } from "node:url"; import { promisify } from "node:util"; import { expect, it as runtimeTest } from "vitest"; -import { NODE_CAPTURE_HEAP_ENFORCED } from "../../../src/browser/history/CaptureRelease.js"; -import { NODE_CAPTURE_PROCESS_MEMORY_LIMIT_BYTES } from "../../fixtures/captureDecoderLimits.js"; +import { + CAPTURE_DECODER_HAS_MEMORY_BOUND as hasMemoryBound, + CAPTURE_RUNTIME_SUPPORTED as runtimeSupported, +} from "../../fixtures/captureDecoderLimits.js"; import { webNetworkCaptureSummaryViewResultSchema } from "../../../src/domain/analysisView/analysisView.js"; import { parseEvidence } from "../../../src/domain/evidence.js"; import { @@ -17,10 +19,7 @@ import { parseMcpToolError } from "../../fixtures/mcpToolError.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; const execute = promisify(execFile); -const hasMemoryBound = - NODE_CAPTURE_HEAP_ENFORCED || - NODE_CAPTURE_PROCESS_MEMORY_LIMIT_BYTES !== null; -const it = runtimeTest.skipIf(!hasMemoryBound); +const it = runtimeTest.skipIf(!hasMemoryBound || !runtimeSupported); const cli = fileURLToPath(new URL("../../../scripts/rea.mjs", import.meta.url)); const event = { name: "transport:packet_sent", @@ -307,9 +306,9 @@ it("pages original trace-scoped facts after source deletion without conflating s }), ), ).toMatchObject({ error: { code: "invalid_request" } }); -}, 60_000); +}); -runtimeTest.runIf(!hasMemoryBound)( +runtimeTest.runIf(!hasMemoryBound && runtimeSupported)( "refuses qlog when this runtime cannot enforce a decoder memory bound", async () => { const { call } = await connectLocalToolsMcp(); @@ -418,7 +417,7 @@ it("runs built CLI and real stdio SDK with exact evidence parity, selection, ret error: { code: "invalid_request" }, }); expect(JSON.stringify(invalid)).not.toContain("mysecret"); -}, 60_000); +}); it("fails whole-capture validation before selection and survives malformed, unsupported and oversized qlog", async () => { const root = await createTestTempDirectory("rea-qlog-errors-"); @@ -525,9 +524,11 @@ it("fails whole-capture validation before selection and survives malformed, unsu await writeFile(path, qlog([event])); await client.ping(); await expectRecoveredCapture(call, path); -}, 60_000); +}); -runtimeTest.skipIf(process.versions.bun !== undefined || !hasMemoryBound)( +runtimeTest.skipIf( + process.versions.bun !== undefined || !hasMemoryBound || !runtimeSupported, +)( "contains actual qlog decoder heap exhaustion and keeps MCP usable", async () => { const root = await createTestTempDirectory("rea-qlog-capacity-"); @@ -565,5 +566,4 @@ runtimeTest.skipIf(process.versions.bun !== undefined || !hasMemoryBound)( await client.ping(); await expectRecoveredCapture(call, path); }, - 60_000, ); diff --git a/tests/acceptance/tools/unavailableNativeCollectionViews.test.ts b/tests/acceptance/tools/unavailableNativeCollectionViews.test.ts index fc1a27e2b5..245048f02e 100644 --- a/tests/acceptance/tools/unavailableNativeCollectionViews.test.ts +++ b/tests/acceptance/tools/unavailableNativeCollectionViews.test.ts @@ -124,7 +124,7 @@ cliTest( }, ]; const call = await cli.sharedMcpCall!(); - const run = cli.sharedCliRun!.bind(cli); + const run = cli.run.bind(cli); for (const test of cases) { const request = { source: { kind: "inline", evidence: test.evidence }, diff --git a/tests/acceptance/webArtifactAcquisitionCli.test.ts b/tests/acceptance/webArtifactAcquisitionCli.test.ts index 8a4d2e51bd..44b47eb7f5 100644 --- a/tests/acceptance/webArtifactAcquisitionCli.test.ts +++ b/tests/acceptance/webArtifactAcquisitionCli.test.ts @@ -36,7 +36,6 @@ cliTest( "--json", ], environment, - timeoutMs: 30_000, }); expect(result.exitCode, result.stdout + result.stderr).toBe(1); expect(result.json).toMatchObject({ @@ -81,5 +80,4 @@ cliTest( expect(response.structuredContent).toBeUndefined(); expect(result.json).not.toHaveProperty("normalized_result"); }, - 90_000, ); diff --git a/tests/acceptance/webArtifactErrorProvenance.test.ts b/tests/acceptance/webArtifactErrorProvenance.test.ts index 34b47097cf..f077ee279c 100644 --- a/tests/acceptance/webArtifactErrorProvenance.test.ts +++ b/tests/acceptance/webArtifactErrorProvenance.test.ts @@ -165,7 +165,6 @@ cliTest( }); } }, - 120_000, ); cliTest( @@ -230,5 +229,4 @@ cliTest( }); } }, - 120_000, ); diff --git a/tests/acceptance/webModuleCli.test.ts b/tests/acceptance/webModuleCli.test.ts index 468d1551b9..e49f0e0259 100644 --- a/tests/acceptance/webModuleCli.test.ts +++ b/tests/acceptance/webModuleCli.test.ts @@ -43,7 +43,6 @@ cliTest( // This single CLI invocation cannot reuse a newly written cache. NODE_DISABLE_COMPILE_CACHE: "1", }, - timeoutMs: 15000, }); expect(cliResponse.exitCode, cliResponse.stdout + cliResponse.stderr).toBe( 0, diff --git a/tests/acceptance/webRuntimeCli.test.ts b/tests/acceptance/webRuntimeCli.test.ts index f4252aecf9..7945a4bbba 100644 --- a/tests/acceptance/webRuntimeCli.test.ts +++ b/tests/acceptance/webRuntimeCli.test.ts @@ -291,7 +291,7 @@ const cancellationCases = cancellationCommands.flatMap((command) => it.each(cancellationCases)( "$command preserves $signal status and awaited cleanup during CDP discovery", - async ({ command, operation, buildArgs, signal, expectedExitCode }) => { + async ({ operation, buildArgs, signal, expectedExitCode }) => { const sockets = new Set(); const server = createServer(() => undefined); server.on("connection", (socket) => { diff --git a/tests/acceptance/webSourceLocationCli.test.ts b/tests/acceptance/webSourceLocationCli.test.ts index cc85f089db..dfcf13cd5d 100644 --- a/tests/acceptance/webSourceLocationCli.test.ts +++ b/tests/acceptance/webSourceLocationCli.test.ts @@ -72,7 +72,6 @@ const expectCliSourceLocationSummary = async ( const { bundlePath, evidenceId, sha256 } = expected; const missingSink = await cli.run({ arguments: [...arguments_, "--detail", "summary"], - timeoutMs: 45000, }); expect(missingSink.exitCode).toBe(1); expect(missingSink.json).toMatchObject({ @@ -87,7 +86,6 @@ const expectCliSourceLocationSummary = async ( "--evidence_output", bundlePath, ], - timeoutMs: 45000, }); expect(summary.exitCode, summary.stderr).toBe(0); expect(summary.stdout.length).toBeLessThan(30000); @@ -176,6 +174,8 @@ const expectColumnOutOfRange = ( cliTest( "accepts numeric UTF-16 coordinates and runs the actual owned source-map codec without a browser", + // Allow cold CLI and MCP ownership preparation across the full parity sequence. + { tags: ["long-running"] }, async ({ cli }) => { const root = await createTestTempDirectory("rea-source-map-cli-"); const fixture = webSourceLocationFixture( @@ -215,7 +215,6 @@ cliTest( REA_BROWSER_EXECUTABLE: "relative-unconfigured-browser", CLANG_MODULE_CACHE_PATH: map, }, - timeoutMs: 45000, }); expect(response.exitCode, response.stdout + response.stderr).toBe(0); expect(response.json).toMatchObject({ @@ -313,7 +312,6 @@ cliTest( "--json", ], environment: { REA_LOG_LEVEL: "silent" }, - timeoutMs: 45000, }); expect(invalid.exitCode).toBe(1); expectInvalidSourceLocation( @@ -324,12 +322,11 @@ cliTest( fixture.sourceMap.text, ); }, - // Allow cold CLI and MCP ownership preparation across the full parity sequence. - 180_000, ); cliTest( "applies the shared version constraint to nested indexed maps through CLI and stdio MCP", + { tags: ["long-running"] }, async ({ cli }) => { const root = await createTestTempDirectory("rea-indexed-map-version-"); const fixture = webSourceLocationFixture(); @@ -371,7 +368,6 @@ cliTest( REA_LOG_LEVEL: "silent", REA_BROWSER_EXECUTABLE: "relative-unconfigured-browser", }, - timeoutMs: 45_000, }); const response = await call("trace_web_source_location", { manifest_path: manifest, @@ -438,11 +434,11 @@ cliTest( } } }, - 180_000, ); cliTest( "traces empty equal-offset sections while rejecting decreasing offsets and represented overlaps", + { tags: ["long-running"] }, async ({ cli }) => { const root = await createTestTempDirectory("rea-empty-indexed-map-"); const fixture = webSourceLocationFixture(); @@ -490,7 +486,6 @@ cliTest( "--json", ], environment: { REA_LOG_LEVEL: "silent" }, - timeoutMs: 45_000, }); const response = await call("trace_web_source_location", { manifest_path: manifest, @@ -538,11 +533,11 @@ cliTest( } } }, - 180_000, ); cliTest( "resolves empty-sourceRoot sources against the map URL through CLI and stdio MCP", + { tags: ["long-running"] }, async ({ cli }) => { const root = await createTestTempDirectory("rea-empty-source-root-"); // webpack 5 and tsc emit `"sourceRoot": ""` for map-relative sources. @@ -577,7 +572,6 @@ cliTest( REA_LOG_LEVEL: "silent", REA_BROWSER_EXECUTABLE: "relative-unconfigured-browser", }, - timeoutMs: 45_000, }); expect(result.exitCode, result.stdout + result.stderr).toBe(0); expect(result.json).toMatchObject({ @@ -613,7 +607,6 @@ cliTest( expect(response.isError, JSON.stringify(response)).not.toBe(true); expect(response.structuredContent).toEqual(result.json); }, - 180_000, ); cliTest( @@ -649,7 +642,6 @@ cliTest( ]; const refused = await cli.run({ arguments: [...args, "--source-map-bytes", "128"], - timeoutMs: 45000, }); expect(refused.exitCode).toBe(1); expect((refused.json as { code?: unknown }).code).toBe("truncated"); @@ -665,7 +657,6 @@ cliTest( "--source-map-bytes", String(Buffer.byteLength(fixture.sourceMap.text)), ], - timeoutMs: 45000, }); expect(recovered.exitCode, recovered.stderr).toBe(0); expect(recovered.json).toMatchObject({ @@ -690,6 +681,7 @@ cliTest( cliTest( "admits a complete map above 4 MiB by default while honoring a smaller caller budget", + { tags: ["long-running"] }, async ({ cli }) => { const root = await createTestTempDirectory("rea-map-default-admission-"); const fixture = webSourceLocationFixture( @@ -720,7 +712,6 @@ cliTest( ]; const refused = await cli.run({ arguments: [...arguments_, "--source-map-bytes", "4194304"], - timeoutMs: 45000, }); expect(refused.exitCode).toBe(1); expect(JSON.stringify(refused.json)).toContain( @@ -728,7 +719,6 @@ cliTest( ); const completed = await cli.run({ arguments: arguments_, - timeoutMs: 45000, }); expect(completed.exitCode, completed.stderr).toBe(0); const evidence = completed.json as { @@ -840,5 +830,4 @@ cliTest( }, }); }, - 180_000, ); diff --git a/tests/acceptance/windowsShortcutCli.test.ts b/tests/acceptance/windowsShortcutCli.test.ts index 67819b3745..710a330680 100644 --- a/tests/acceptance/windowsShortcutCli.test.ts +++ b/tests/acceptance/windowsShortcutCli.test.ts @@ -4,7 +4,10 @@ import { expect } from "vitest"; import { BINARY_DIAGNOSTICS_TOOL_CONTRACTS } from "../../src/contracts/native/binaryDiagnosticsToolContracts.js"; import { powershellHiddenIconSpoofShortcut, + TRACKER_MAC, truncatedStringDataShortcut, + uuidV1Bytes, + windowsShortcutBytes, wrongClsidShortcut, } from "../fixtures/binaryDiagnostics/windowsShortcut.js"; import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js"; @@ -19,6 +22,62 @@ const shortcutContract = (() => { return contract; })(); +cliTest( + "preserves pre-epoch shortcut timestamps through CLI and stdio MCP", + async ({ cli }) => { + const root = await createTestTempDirectory("rea-shortcut-time-cli-"); + const path = join(root, "time.lnk"); + const uuid = uuidV1Bytes(122192927999999999n, 1, TRACKER_MAC); + await writeFile( + path, + windowsShortcutBytes({ + creationTime: 116444735999999999n, + accessTime: 0n, + writeTime: 116444736000000001n, + extraData: [ + { kind: "tracker", droid: [uuid, uuid], birthDroid: [uuid, uuid] }, + ], + }), + ); + const result = await cli.run({ + arguments: ["inspect-windows-shortcut", path, "--json"], + }); + expect(result.exitCode, result.stderr).toBe(0); + const call = await cli.sharedMcpCall?.(); + if (call === undefined) throw new Error("Expected a stdio MCP session"); + const response = await call("inspect_windows_shortcut", { path }); + expect(response.isError, JSON.stringify(response.content)).not.toBe(true); + for (const value of [result.json, response.structuredContent]) { + const evidence = shortcutContract.outputSchema.parse(value); + const report = evidence.normalized_result; + expect(evidence.subject?.local_path).toBe(path); + expect(report.header).toMatchObject({ + creation_time: { + raw: "116444735999999999", + iso8601: "1969-12-31T23:59:59.999Z", + }, + access_time: { raw: "0", iso8601: null }, + write_time: { + raw: "116444736000000001", + iso8601: "1970-01-01T00:00:00.000Z", + }, + }); + const tracker = report.extra_data.blocks[0]?.decoded; + if (tracker?.kind !== "tracker") + throw new Error("Expected TrackerDataBlock"); + for (const time of [ + ...tracker.droid_uuid_v1, + ...tracker.birth_droid_uuid_v1, + ]) + expect(time).toMatchObject({ + timestamp_100ns: "122192927999999999", + timestamp_iso8601: "1969-12-31T23:59:59.999Z", + }); + expect(report.coverage.status).toBe("complete"); + } + }, +); + cliTest( "reports Shell Link facts through CLI and stdio MCP", async ({ cli }) => { diff --git a/tests/boundary/android/jadxDoctor.test.ts b/tests/boundary/android/jadxDoctor.test.ts index f48c86d341..2b21e1dccc 100644 --- a/tests/boundary/android/jadxDoctor.test.ts +++ b/tests/boundary/android/jadxDoctor.test.ts @@ -5,6 +5,7 @@ import { join } from "node:path"; import { expect, it as test, onTestFinished } from "vitest"; import { inspectJadxProvider as inspectProvider } from "../../../src/android/JadxDoctor.js"; +import { JADX_RELEASE } from "../../../src/android/JadxRelease.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; import { writeJadxJarInventory } from "../../fixtures/android/jadxJar.js"; import { writeOrderedZip } from "../../fixtures/artifactEntryOrder.js"; @@ -131,6 +132,9 @@ it("publishes no registration environment when REA_JADX_MCP_JAR is unset", async }, ], }); + // The recovery names where to obtain the exact audited JAR. + expect(inspection.checks[0]?.remediation).toContain(JADX_RELEASE.url); + expect(inspection.checks[0]?.remediation).toContain(JADX_RELEASE.sha256); }); it("reports a relative REA_JADX_MCP_JAR as a rejected selection, not an unset one", async () => { diff --git a/tests/boundary/binaryDiagnostics/recordedCrashProvider.test.ts b/tests/boundary/binaryDiagnostics/recordedCrashProvider.test.ts index b39b3a6eed..e872c15ebf 100644 --- a/tests/boundary/binaryDiagnostics/recordedCrashProvider.test.ts +++ b/tests/boundary/binaryDiagnostics/recordedCrashProvider.test.ts @@ -550,7 +550,6 @@ it.runIf(host)( expect(projected.remediation.action).toContain("above 1500"); expect(projected.remediation.action).not.toContain("above 30000"); }, - 10_000, ); const fixture = async ( diff --git a/tests/boundary/browser/browserFixtureHttpTargets.test.ts b/tests/boundary/browser/browserFixtureHttpTargets.test.ts index 7619fb9484..4b4c09f1df 100644 --- a/tests/boundary/browser/browserFixtureHttpTargets.test.ts +++ b/tests/boundary/browser/browserFixtureHttpTargets.test.ts @@ -7,19 +7,15 @@ for (const kind of ["proxy", "workers", "storage"] as const) { processTest( `${kind} HTTP fixture preserves targets and survives invalid requests`, async ({ processes }) => { - const result = await processes.run( - process.execPath, - [ - fileURLToPath( - new URL( - "../../fixtures/browserFixtureHttpTargets.mjs", - import.meta.url, - ), + const result = await processes.run(process.execPath, [ + fileURLToPath( + new URL( + "../../fixtures/browserFixtureHttpTargets.mjs", + import.meta.url, ), - kind, - ], - { timeoutMs: 15_000 }, - ); + ), + kind, + ]); expect(result.signal, result.stderr).toBeNull(); expect(result.exitCode, result.stderr).toBe(0); expect(result.stderr).toBe(""); diff --git a/tests/boundary/browser/browserScenarioReferenceCapacity.test.ts b/tests/boundary/browser/browserScenarioReferenceCapacity.test.ts new file mode 100644 index 0000000000..73077cfa9e --- /dev/null +++ b/tests/boundary/browser/browserScenarioReferenceCapacity.test.ts @@ -0,0 +1,56 @@ +import { expect, it } from "vitest"; +import { browserScenarioSchema } from "../../../src/domain/browserScenario.js"; +import { browserScenarioReferences } from "../../fixtures/browserScenarioReferences.js"; + +it.each(["query", "storage", "session-storage", "cookie"] as const)( + "preserves a declared %s reference list wider than call arguments", + (placement) => { + // Forked tests share the CLI/MCP main-thread stack, unlike domain threads. + const count = 130_000; + const parsed = browserScenarioSchema.parse( + browserScenarioReferences(placement, count), + ); + const entries = + placement === "query" + ? parsed.start_url.query + : placement === "cookie" + ? parsed.storage.cookies[0]?.destination.query + : parsed.storage[ + placement === "session-storage" + ? "session_storage" + : "local_storage" + ][0]?.entries; + expect(entries).toHaveLength(count); + expect(entries?.[0]).toEqual({ + name: "entry_0", + value: { source: "secret", secret_id: "token" }, + }); + expect(entries?.at(-1)).toEqual({ + name: `entry_${String(count - 1)}`, + value: { source: "secret", secret_id: "token" }, + }); + }, +); + +it("reports each distinct undeclared identity once across query and storage uses", () => { + const input = browserScenarioReferences("query", 3); + input.secrets = []; + input.storage.local_storage = [ + { + origin: "http://127.0.0.1:1", + entries: [ + { name: "one", value: { source: "secret", secret_id: "other" } }, + { name: "two", value: { source: "secret", secret_id: "token" } }, + { name: "three", value: { source: "secret", secret_id: "other" } }, + ], + }, + ]; + const result = browserScenarioSchema.safeParse(input); + expect(result.success).toBe(false); + expect( + result.error?.issues.map(({ path, message }) => ({ path, message })), + ).toEqual([ + { path: ["secrets"], message: "Secret reference token is not declared" }, + { path: ["secrets"], message: "Secret reference other is not declared" }, + ]); +}); diff --git a/tests/boundary/browser/browserStorageListenerScope.test.ts b/tests/boundary/browser/browserStorageListenerScope.test.ts index 8149ac3cf0..9ea17251a1 100644 --- a/tests/boundary/browser/browserStorageListenerScope.test.ts +++ b/tests/boundary/browser/browserStorageListenerScope.test.ts @@ -11,20 +11,16 @@ const hasNonLoopbackIpv4 = Object.values(networkInterfaces()) processTest.skipIf(!hasNonLoopbackIpv4)( "storage fixture serves localhost and refuses this host's non-loopback interface", async ({ processes }) => { - const result = await processes.run( - process.execPath, - [ - fileURLToPath( - new URL( - "../../fixtures/browserFixtureHttpTargets.mjs", - import.meta.url, - ), + const result = await processes.run(process.execPath, [ + fileURLToPath( + new URL( + "../../fixtures/browserFixtureHttpTargets.mjs", + import.meta.url, ), - "storage", - "listener-scope", - ], - { timeoutMs: 15_000 }, - ); + ), + "storage", + "listener-scope", + ]); expect(result.signal, result.stderr).toBeNull(); expect(result.exitCode, result.stderr).toBe(0); expect(result.stderr).toBe(""); diff --git a/tests/boundary/browser/cdpObservationWindowScope.test.ts b/tests/boundary/browser/cdpObservationWindowScope.test.ts index 66afde5c16..fb51f0dc17 100644 --- a/tests/boundary/browser/cdpObservationWindowScope.test.ts +++ b/tests/boundary/browser/cdpObservationWindowScope.test.ts @@ -409,7 +409,7 @@ it("ends an armed observation at a fatal protocol failure without waiting out it ok: false, error: { _tag: "BrowserObservationError", reason: "protocol_error" }, }); -}, 15_000); +}); it("does not join an empty document request identity to a later loading failure", async () => { const { observation } = await observeDuringClockSampling( diff --git a/tests/boundary/browser/domVerifierCleanup.test.ts b/tests/boundary/browser/domVerifierCleanup.test.ts index f3d5375da2..eb99985091 100644 --- a/tests/boundary/browser/domVerifierCleanup.test.ts +++ b/tests/boundary/browser/domVerifierCleanup.test.ts @@ -73,7 +73,6 @@ processTest( ["--input-type=module", "--eval", source], { env: { ...process.env, REA_BROWSER_EXECUTABLE: "fixture-chrome" }, - timeoutMs: 15_000, }, ); expect(result.exitCode, result.stderr).toBe(0); diff --git a/tests/boundary/browser/electronActiveHook.test.ts b/tests/boundary/browser/electronActiveHook.test.ts index fd2660702d..360126a76d 100644 --- a/tests/boundary/browser/electronActiveHook.test.ts +++ b/tests/boundary/browser/electronActiveHook.test.ts @@ -3774,7 +3774,7 @@ const runNode = async ( const result = await processes.run( process.execPath, [...nodeArguments, ...(preloadHook ? ["-r", hook] : []), script], - { cwd: root, env: process.env, timeoutMs: 10_000 }, + { cwd: root, env: process.env }, ); return { code: result.exitCode, diff --git a/tests/boundary/browser/historicalCaptureDecoder.test.ts b/tests/boundary/browser/historicalCaptureDecoder.test.ts index 7d75687955..947c3609f1 100644 --- a/tests/boundary/browser/historicalCaptureDecoder.test.ts +++ b/tests/boundary/browser/historicalCaptureDecoder.test.ts @@ -392,7 +392,6 @@ it.each(["cancelled", "process"] as const)( outcome === "cancelled" ? "cancelled" : "execution_failure", ); }, - 15000, ); it("applies a non-default capture deadline and releases its runtime", async () => { @@ -435,7 +434,7 @@ it("applies a non-default capture deadline and releases its runtime", async () = expect(result.error.userMessage).toContain("capture_timeout_ms"); if (runtimePath === undefined) throw new Error("runtime was not acquired"); await expect(access(runtimePath)).rejects.toMatchObject({ code: "ENOENT" }); -}, 8_000); +}); it.each([true, false])( "binds decoder replies to the complete selected provider identity (matching name: %s)", diff --git a/tests/boundary/browser/historicalCaptureWideNumbers.test.ts b/tests/boundary/browser/historicalCaptureWideNumbers.test.ts new file mode 100644 index 0000000000..dc9fa86cad --- /dev/null +++ b/tests/boundary/browser/historicalCaptureWideNumbers.test.ts @@ -0,0 +1,119 @@ +import { createHash } from "node:crypto"; +import { readFile, writeFile } from "node:fs/promises"; +import { join } from "node:path"; +import { expect, it, onTestFinished } from "vitest"; + +import { HistoricalCaptureDecoder } from "../../../src/browser/history/HistoricalCaptureDecoder.js"; +import { MITMPROXY_CAPTURE_PROVIDER_IDENTITY } from "../../../src/browser/history/CaptureProviderIdentities.js"; +import { decodeHarCapture } from "../../../src/browser/history/HarCapture.js"; +import { inspectWebNetworkCaptureInputSchema } from "../../../src/domain/webNetworkCapture.js"; +import { historicalHar } from "../../fixtures/historicalHar.js"; +import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; + +it("retains every exclusion in a wide native-number reply through the owned decoder", async () => { + const root = await createTestTempDirectory("rea-wide-native-numbers-"); + const capturePath = join(root, "capture.mitm"); + const original = "source-owned native decoder input"; + await writeFile(capturePath, original); + const count = 150_000; + const payload = decodeHarCapture(JSON.stringify(historicalHar()), []); + payload.decoder = MITMPROXY_CAPTURE_PROVIDER_IDENTITY; + payload.container.reported = { normalized: 1e-7, ordinary: "container" }; + payload.container.numeric_literals = [ + { pointer: "/normalized", producer_type: "float", literal: "1e-07" }, + ]; + const record = payload.records[0]!; + record.location = { kind: "byte-range", offset: 0, bytes: original.length }; + // Python's native projection retains 1e-07; the reply owner must also check + // the JavaScript spelling 1e-7 before retaining Evidence. + record.reported = { + values: Array.from({ length: count }, () => 1e-7), + ordinary: 42, + request: { headers: [["authorization", null]] }, + }; + record.binary_fields = []; + const existingRedaction = { + pointer: "/request/headers/0/1", + reason: "transport-credential" as const, + }; + record.redactions = [existingRedaction]; + record.numeric_literals = Array.from({ length: count }, (_, index) => ({ + pointer: `/values/${String(index)}`, + producer_type: "float" as const, + literal: "1e-07", + })); + const ordinary = { + pointer: "/ordinary", + producer_type: "integer" as const, + literal: "42", + }; + record.numeric_literals.push(ordinary); + const replyPath = join(root, "producer-reply.json"); + await writeFile(replyPath, JSON.stringify({ ok: true, value: payload })); + const decoder = new HistoricalCaptureDecoder( + [ + { + format: "mitmproxy", + identity: MITMPROXY_CAPTURE_PROVIDER_IDENTITY, + command: async (_, runtime) => ({ + command: process.execPath, + arguments: [ + "-e", + "require('node:fs').copyFileSync(process.argv[1], process.argv[2])", + replyPath, + join(runtime, "reply.json"), + ], + }), + }, + ], + process.env, + ); + onTestFinished(() => decoder.close()); + for (const marked of [false, true]) { + const result = await decoder.inspect( + inspectWebNetworkCaptureInputSchema.parse({ + capture_path: capturePath, + format: "mitmproxy", + sensitive_values: marked ? ["1e-7"] : [], + }), + ); + if (!result.ok) throw result.error; + expect(result.value.artifact.sha256).toBe( + createHash("sha256").update(original).digest("hex"), + ); + expect(result.value.container.reported).toEqual({ + normalized: marked ? null : 1e-7, + ordinary: "container", + }); + expect(result.value.container.numeric_literals).toEqual( + marked ? [] : payload.container.numeric_literals, + ); + const retained = result.value.records[0]!; + expect(retained.location).toEqual(record.location); + expect(retained.reported).toMatchObject({ + ordinary: 42, + request: { headers: [["authorization", null]] }, + }); + const values = (retained.reported as { values: unknown[] }).values; + expect(values).toHaveLength(count); + expect(values.every((value) => value === (marked ? null : 1e-7))).toBe( + true, + ); + expect(retained.numeric_literals).toEqual( + marked ? [ordinary] : record.numeric_literals, + ); + expect(retained.redactions[0]).toEqual(existingRedaction); + expect(retained.redactions).toHaveLength(marked ? count + 1 : 1); + if (marked) + expect( + retained.redactions + .slice(1) + .every( + (redaction, index) => + redaction.pointer === `/values/${String(index)}` && + redaction.reason === "explicit-sensitive-value", + ), + ).toBe(true); + expect(await readFile(capturePath, "utf8")).toBe(original); + } +}); diff --git a/tests/boundary/browser/historicalWebCapture.test.ts b/tests/boundary/browser/historicalWebCapture.test.ts index 538438a051..ded321ecf1 100644 --- a/tests/boundary/browser/historicalWebCapture.test.ts +++ b/tests/boundary/browser/historicalWebCapture.test.ts @@ -283,7 +283,7 @@ it("excludes declared binary lengths through CLI, MCP and retained capture pages }); } } -}, 60_000); +}); it("reads actual upstream HAR packages in an owned process and retains selected producer order", async () => { const root = await mkdtemp(join(tmpdir(), "rea-history-har-")); diff --git a/tests/boundary/browser/largeBase64Capture.test.ts b/tests/boundary/browser/largeBase64Capture.test.ts index 06ebc313d5..8a7abb9ee7 100644 --- a/tests/boundary/browser/largeBase64Capture.test.ts +++ b/tests/boundary/browser/largeBase64Capture.test.ts @@ -90,7 +90,6 @@ cliTest( const fromCli = await cli.run({ arguments: ["inspect-web-network-capture", path, "har", "--json"], - timeoutMs: 120_000, }); expect(fromCli.exitCode).toBe(0); const cliSummary = summarize(fromCli.json); diff --git a/tests/boundary/browser/sazWebCapture.test.ts b/tests/boundary/browser/sazWebCapture.test.ts index 59803eb57b..4ebe004e19 100644 --- a/tests/boundary/browser/sazWebCapture.test.ts +++ b/tests/boundary/browser/sazWebCapture.test.ts @@ -26,13 +26,12 @@ import { parseMcpToolError } from "../../fixtures/mcpToolError.js"; import { protobufFrame } from "../../fixtures/protobufCapture.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; -import { NODE_CAPTURE_HEAP_ENFORCED } from "../../../src/browser/history/CaptureRelease.js"; -import { NODE_CAPTURE_PROCESS_MEMORY_LIMIT_BYTES } from "../../fixtures/captureDecoderLimits.js"; -const hasMemoryBound = - NODE_CAPTURE_HEAP_ENFORCED || - NODE_CAPTURE_PROCESS_MEMORY_LIMIT_BYTES !== null; +import { + CAPTURE_DECODER_HAS_MEMORY_BOUND as hasMemoryBound, + CAPTURE_RUNTIME_SUPPORTED as runtimeSupported, +} from "../../fixtures/captureDecoderLimits.js"; -const it = runtimeTest.skipIf(!hasMemoryBound); +const it = runtimeTest.skipIf(!hasMemoryBound || !runtimeSupported); // Share schema compilation, not sessions or their retained Evidence. const schemaValidation = createMcpSchemaValidation(); @@ -540,7 +539,7 @@ it("inspects native SAZ through the public CLI and MCP with equal evidence, byte expect( records.find((record) => record.evidence_id === fromMcp.evidence_id), ).toEqual(fromMcp); -}, 60_000); +}); it("pages retained SAZ Evidence through public MCP and portable CLI after source deletion", async () => { const path = await fixture(await archive()); @@ -625,7 +624,7 @@ it("pages retained SAZ Evidence through public MCP and portable CLI after source expect( parseEvidence(JSON.parse(standalone.stdout)).normalized_result, ).toEqual(summary.normalized_result); -}, 60_000); +}); it("keeps CRLF separators inside a body after LF-only HTTP headers", async () => { const body = Buffer.from("\ufefffirst\r\n\r\nsecond\n\nthird"); @@ -947,7 +946,7 @@ it.each([ expect(JSON.stringify(result)).not.toContain("malformed-private-input"); }); -runtimeTest.runIf(!hasMemoryBound)( +runtimeTest.runIf(!hasMemoryBound && runtimeSupported)( "reports SAZ unavailable when its runtime cannot enforce decoder memory", async () => { const response = await cliResponse(await fixture(await archive())); diff --git a/tests/boundary/browser/webSourceMapFetcher.test.ts b/tests/boundary/browser/webSourceMapFetcher.test.ts index 2cf8991d7d..b6413bc39e 100644 --- a/tests/boundary/browser/webSourceMapFetcher.test.ts +++ b/tests/boundary/browser/webSourceMapFetcher.test.ts @@ -970,7 +970,7 @@ describe("selected source-map fetch deadline", () => { ); }); } - }, 3_000); + }); }); describe("browser map decoded validation", () => { @@ -1802,7 +1802,7 @@ describe("source-map set output admission", () => { expect(Buffer.byteLength(JSON.stringify(result))).toBeLessThanOrEqual( outputLimitBytes, ); - }, 120_000); + }); it("reserves large failure contexts and reports unprocessed requests as unknown", async () => { const largeDeclaredUrl = `${origin}/${"u".repeat(8 * 1_024 * 1_024)}`; @@ -1901,7 +1901,7 @@ describe("expanded source-map evidence budget", () => { "included", ]); expect(result.items[2]?.artifact?.text).toBe(valid); - }, 15_000); + }); it("bounds expanded evidence across individually valid maps", async () => { const expanded = JSON.stringify({ diff --git a/tests/boundary/cli/accessibilityCaptureComparison.test.ts b/tests/boundary/cli/accessibilityCaptureComparison.test.ts index 5d136db29c..99c4225bc3 100644 --- a/tests/boundary/cli/accessibilityCaptureComparison.test.ts +++ b/tests/boundary/cli/accessibilityCaptureComparison.test.ts @@ -101,7 +101,6 @@ cliTest.for([ "policy_filtered", ] as const)( "does not infer an accessibility change from a %s tree", - { timeout: 60_000 }, async (coverage, { cli }) => { const before = accessibleCapture(); const after = structuredClone(before); @@ -158,7 +157,6 @@ cliTest.for([ ["known roles differ", "button", "link", "changed"], ] as const)( "does not infer role equality or change when %s", - { timeout: 60_000 }, async ([_label, beforeRole, afterRole, status], { cli }) => { const before = accessibleCapture(); const after = structuredClone(before); @@ -218,7 +216,6 @@ cliTest( }, }); }, - 60_000, ); cliTest( diff --git a/tests/boundary/cli/adbCli.test.ts b/tests/boundary/cli/adbCli.test.ts index 9ad8521817..59352dc72f 100644 --- a/tests/boundary/cli/adbCli.test.ts +++ b/tests/boundary/cli/adbCli.test.ts @@ -274,6 +274,14 @@ printf 'Android Debug Bridge version 1.0.41\\nVersion 34.0.5-cli\\n' result, `Execution of the selected adb binary was denied (EACCES): ${binary}. Set REA_ADB_PATH to an absolute path to the adb executable file, not a directory`, ); + const { details } = JSON.parse(result.stdout) as { + details: { diagnostics: Record }; + }; + expect(details.diagnostics).toMatchObject({ + selected_binary: binary, + path_source: "environment", + filesystem_error: "EACCES", + }); } // A parent component that is a file, a symlink loop and a missing path // fail before any process starts, so no exit code is reported. diff --git a/tests/boundary/cli/ampRegistrationPreservation.test.ts b/tests/boundary/cli/ampRegistrationPreservation.test.ts index 1f9faf34b4..960e98cf6f 100644 --- a/tests/boundary/cli/ampRegistrationPreservation.test.ts +++ b/tests/boundary/cli/ampRegistrationPreservation.test.ts @@ -29,7 +29,6 @@ cliTest( arguments: ["mcp", "add", "--agent=amp", ...options, "--json"], cwd: root, environment, - timeoutMs: 20_000, }); const settingsPath = ampSettings(home); const original = JSON.stringify( @@ -133,7 +132,6 @@ cliTest( arguments: ["mcp", "add", "--agent", "amp", "--json"], cwd: root, environment, - timeoutMs: 20_000, }); expect(rejected.exitCode, rejected.stderr).toBe(1); expect(mcpRegistrationReportSchema.parse(rejected.json)).toMatchObject({ diff --git a/tests/boundary/cli/analysisSelectorCli.test.ts b/tests/boundary/cli/analysisSelectorCli.test.ts index df18fff028..e9d919c123 100644 --- a/tests/boundary/cli/analysisSelectorCli.test.ts +++ b/tests/boundary/cli/analysisSelectorCli.test.ts @@ -1,7 +1,7 @@ import { describe, expect } from "vitest"; import { nativeDataTypeInputSchema } from "../../../src/domain/native/nativeDataType.js"; -import { cliTest, runFileCli } from "../../support/cli/cliFixture.js"; +import { cliTest } from "../../support/cli/cliFixture.js"; const selectors = [ ["function", "procedure", "analyze_function"], @@ -101,7 +101,7 @@ describe("CLI analysis selector boundaries", () => { ["selector", `--${option}=selector`], ]) { const label = `${command} ${selection.join(" ")}`; - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: [ command, "does-not-exist", @@ -174,7 +174,7 @@ cliTest( [["--address", "0x1000"], { code: "target_unavailable" }], ] as const) { const label = `inspect-native-data-type ${selection.join(" ")}`; - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: [ "inspect-native-data-type", "does-not-exist", diff --git a/tests/boundary/cli/analysisViewCli.test.ts b/tests/boundary/cli/analysisViewCli.test.ts index 9a32ee6555..60dc7cc75e 100644 --- a/tests/boundary/cli/analysisViewCli.test.ts +++ b/tests/boundary/cli/analysisViewCli.test.ts @@ -16,7 +16,7 @@ import { expect } from "vitest"; import { TextReader, Uint8ArrayWriter, ZipWriter } from "@zip.js/zip.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; -import { cliTest, runFileCli } from "../../support/cli/cliFixture.js"; +import { cliTest } from "../../support/cli/cliFixture.js"; import { analysisViewLayoutEvidence, @@ -119,7 +119,7 @@ cliTest( ); await writeFile(path, JSON.stringify(createEvidenceBundle([parent]))); const inspect = async (view: JsonValue) => { - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: [ "inspect-analysis-view", JSON.stringify({ @@ -177,7 +177,7 @@ cliTest( ]); const original = JSON.stringify(bundle); await writeFile(path, original); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: [ "inspect-analysis-view", JSON.stringify(request), @@ -216,10 +216,10 @@ cliTest( "--json", ], }; - const rejected = - source.kind === "inline" - ? await cli.run(invocation) - : await runFileCli(cli, invocation); + const rejected = await cli.run({ + ...invocation, + freshProcess: source.kind === "inline", + }); expect(rejected.exitCode).toBe(1); expect(rejected.json).toMatchObject({ code: expectedCode }); expect(rejected.json).not.toHaveProperty("evidence_id"); @@ -251,9 +251,10 @@ cliTest( }; // Keep a fresh-process integrity check for the altered bundle while the // worker must reread the same path for the other invalid inputs. - const rejected = freshProcess - ? await cli.run(invocation) - : await runFileCli(cli, invocation); + const rejected = await cli.run({ + ...invocation, + freshProcess: freshProcess, + }); expect(rejected.exitCode).toBe(1); expect(rejected.json).toMatchObject({ code: expectedCode }); expect(rejected.json).not.toHaveProperty("evidence_id"); @@ -280,7 +281,7 @@ cliTest( view: { kind: "native", facet: "callees", offset: 0, limit: 1 }, }), ); - const response = await runFileCli(cli, { + const response = await cli.run({ arguments: ["inspect-analysis-view", request, "--json"], }); expect(response.exitCode).toBe(0); @@ -319,7 +320,7 @@ cliTest( const expected = await inspectAnalysisView(input); if (!expected.ok) throw expected.error; await writeFile(request, JSON.stringify(input)); - const response = await runFileCli(cli, { + const response = await cli.run({ arguments: ["inspect-analysis-view", request, "--json"], }); expect(response.exitCode).toBe(0); @@ -360,7 +361,7 @@ cliTest( view: { kind: "api-surface", include_query_parameter_names: true }, }), ); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: ["inspect-analysis-view", path, "--json"], }); expect(result.exitCode).toBe(0); @@ -390,7 +391,7 @@ cliTest( await writer.add("main.js", new TextReader("console.log('portable');\n")); await writeFile(archive, await writer.close()); const bundlePath = join(root, "inspection-bundle.json"); - const inspected = await runFileCli(cli, { + const inspected = await cli.run({ arguments: [ "inspect-artifact", archive, @@ -440,7 +441,7 @@ cliTest( view: { kind: "page", collection: "sections", offset: 0, limit: 20 }, }; await writeFile(unsupportedRequest, JSON.stringify(unsupportedInput)); - const unsupported = await runFileCli(cli, { + const unsupported = await cli.run({ arguments: ["inspect-analysis-view", unsupportedRequest, "--json"], }); expect(unsupported.exitCode).toBe(1); @@ -468,7 +469,7 @@ cliTest( }, }), ); - const projected = await runFileCli(cli, { + const projected = await cli.run({ arguments: ["inspect-analysis-view", request, "--json"], }); expect(projected.exitCode).toBe(0); @@ -497,7 +498,7 @@ cliTest( const writer = new ZipWriter(new Uint8ArrayWriter()); await writer.add("main.js", new TextReader("console.log(1);\n")); await writeFile(archive, await writer.close()); - const inspected = await runFileCli(cli, { + const inspected = await cli.run({ arguments: ["inspect-artifact", archive, "--json"], }); expect(inspected.exitCode).toBe(0); @@ -541,10 +542,10 @@ cliTest( }; // Exercise a valid and a malformed parent in fresh processes as well // as the shared worker's repeated reads of the rewritten input path. - const reply = - group === "manifest" && variant !== "missing" - ? await cli.run(invocation) - : await runFileCli(cli, invocation); + const reply = await cli.run({ + ...invocation, + freshProcess: group === "manifest" && variant !== "missing", + }); expect(reply.stderr).toBe(""); if (variant === "valid") { expect(application.ok).toBe(true); @@ -599,7 +600,7 @@ cliTest( ], }); await writeFile(path, JSON.stringify(request)); - const refused = await runFileCli(cli, { + const refused = await cli.run({ arguments: ["inspect-analysis-view", path, "--json"], }); expect(refused.exitCode).toBe(1); @@ -648,7 +649,7 @@ cliTest( view: { kind: "api-surface" }, }), ); - const refused = await runFileCli(cli, { + const refused = await cli.run({ arguments: ["inspect-analysis-view", path, "--json"], environment, }); @@ -679,7 +680,7 @@ cliTest( view: { kind: "api-surface" }, }), ); - const response = await runFileCli(cli, { + const response = await cli.run({ arguments: ["inspect-analysis-view", path, "--json"], environment, }); @@ -716,7 +717,7 @@ cliTest( detail: "summary" as const, filters: { evidence_id: parent.evidence_id }, }; - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: [ "inspect-evidence-bundle", JSON.stringify({ path, ...options }), @@ -729,7 +730,7 @@ cliTest( if (!expected.ok) throw expected.error; expect(result.json).toEqual(expected.value); // The same shared owner selects the complete record from the portable file. - const records = await runFileCli(cli, { + const records = await cli.run({ arguments: [ "inspect-evidence-bundle", JSON.stringify({ path, ...options, detail: "records", limit: 1 }), @@ -746,7 +747,7 @@ cliTest( records: [parent], missing_evidence_links: [], }); - const complete = await runFileCli(cli, { + const complete = await cli.run({ arguments: [ "inspect-evidence-bundle", JSON.stringify({ path }), @@ -781,7 +782,7 @@ cliTest( detail: "links", traversal: "retained-closure", } as const; - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: [ "inspect-evidence-bundle", JSON.stringify({ path, ...options }), @@ -850,7 +851,7 @@ cliTest( }; { - const json = await runFileCli(cli, { + const json = await cli.run({ arguments: [ "inspect-analysis-view", input, @@ -869,7 +870,7 @@ cliTest( ).toHaveLength(rowCount); } - const markdown = await runFileCli(cli, { + const markdown = await cli.run({ arguments: [ "inspect-analysis-view", input, @@ -908,7 +909,7 @@ cliTest( const input = join(root, "view.json"); await mkdir(root, { recursive: true }); await writeFile(input, JSON.stringify(request)); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: ["inspect-analysis-view", input, "--json"], environment: { HOME: root, @@ -934,7 +935,7 @@ cliTest( const root = await createTestTempDirectory("rea-empty-layout-view-cli-"); const empty = analysisViewLayoutEvidence(binaryLayoutFixture()); for (const collection of ["sections", "symbols"] as const) { - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: [ "inspect-analysis-view", JSON.stringify({ @@ -965,7 +966,7 @@ cliTest( expect(result.json).not.toHaveProperty("details.issues.0.minimum"); expect(result.json).not.toHaveProperty("details.issues.0.maximum"); } - const populated = await runFileCli(cli, { + const populated = await cli.run({ arguments: [ "inspect-analysis-view", JSON.stringify({ @@ -1022,7 +1023,7 @@ cliTest( view: { kind: "native", facet: "pseudocode", offset: 0, limit: 12 }, }), ); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: ["inspect-analysis-view", input, "--json"], environment: { HOME: root, XDG_CONFIG_HOME: root, XDG_CACHE_HOME: root }, }); @@ -1065,7 +1066,7 @@ cliTest( { kind: "summary" }, { kind: "evm", facet: "bytecode_hex", offset: 0, limit: 4 }, ]) { - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: [ "inspect-analysis-view", JSON.stringify({ @@ -1087,7 +1088,7 @@ cliTest( cliTest("rejects malformed JSON before projection", async ({ cli }) => { const root = await createTestTempDirectory("rea-analysis-view-cli-bad-"); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: ["inspect-analysis-view", "{", "--json"], environment: { HOME: root, @@ -1107,7 +1108,7 @@ cliTest("rejects a request that omits source", async ({ cli }) => { const root = await createTestTempDirectory("rea-analysis-view-cli-missing-"); const input = join(root, "missing-source.json"); await writeFile(input, JSON.stringify({ view: { kind: "summary" } })); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: ["inspect-analysis-view", input, "--json"], environment: { HOME: root, @@ -1142,7 +1143,7 @@ for (const [view, reason] of [ view, }), ); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: ["inspect-analysis-view", input, "--json"], environment: { HOME: root, @@ -1195,7 +1196,7 @@ cliTest( XDG_CACHE_HOME: root, }; await writeFile(capturePath, bytes); - const capture = await runFileCli(cli, { + const capture = await cli.run({ arguments: ["inspect-web-network-capture", capturePath, "har", "--json"], environment, }); @@ -1212,7 +1213,7 @@ cliTest( view: { kind: "api-surface" }, }), ); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: ["inspect-analysis-view", input, "--json"], environment, }); @@ -1266,7 +1267,7 @@ cliTest( view, }), ); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: ["inspect-analysis-view", requestPath, "--json"], }); expect(result.exitCode, result.stdout).toBe(0); @@ -1312,7 +1313,7 @@ cliTest( }, }), ); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: ["inspect-analysis-view", path, "--json"], }); expect(result.exitCode, result.stdout).toBe(0); @@ -1348,7 +1349,7 @@ cliTest( view: { kind: "environment-manifest" }, }), ); - const refused = await runFileCli(cli, { + const refused = await cli.run({ arguments: ["inspect-analysis-view", path, "--json"], }); expect(refused.exitCode).toBe(1); @@ -1383,7 +1384,7 @@ cliTest( }, }), ); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: ["inspect-analysis-view", path, "--json"], }); expect(result.exitCode, result.stdout).toBe(0); @@ -1457,7 +1458,7 @@ cliTest.for([ view: { kind: "environment-state" }, }); await writeFile(path, request); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: ["inspect-analysis-view", path, "--json"], }); expect(result.exitCode, result.stdout).toBe(0); @@ -1485,7 +1486,7 @@ cliTest( const root = await createTestTempDirectory("rea-manifest-chain-cli-"); const capturePath = join(root, "capture.har"); await writeFile(capturePath, JSON.stringify(environmentManifestHar())); - const capture = await runFileCli(cli, { + const capture = await cli.run({ arguments: ["inspect-web-network-capture", capturePath, "har", "--json"], }); expect(capture.exitCode, capture.stdout).toBe(0); @@ -1498,7 +1499,7 @@ cliTest( view: { kind: "environment-manifest" }, }), ); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: ["inspect-analysis-view", input, "--json"], }); expect(result.exitCode, result.stdout).toBe(0); diff --git a/tests/boundary/cli/appleBundleDirectoryCli.test.ts b/tests/boundary/cli/appleBundleDirectoryCli.test.ts index ef330fa5bd..bd24f82317 100644 --- a/tests/boundary/cli/appleBundleDirectoryCli.test.ts +++ b/tests/boundary/cli/appleBundleDirectoryCli.test.ts @@ -97,7 +97,6 @@ cliTest.for([ cliTest( "Apple CLI and MCP read a flattened framework's Resources/Info.plist", - { timeout: 60_000 }, async ({ cli }) => { const root = await createTestTempDirectory("rea-apple-flat-framework-"); const app = join(root, "Fixture.app"); diff --git a/tests/boundary/cli/artifactComparisonSources.test.ts b/tests/boundary/cli/artifactComparisonSources.test.ts index 35c6e40fb7..fdfc33b756 100644 --- a/tests/boundary/cli/artifactComparisonSources.test.ts +++ b/tests/boundary/cli/artifactComparisonSources.test.ts @@ -60,7 +60,6 @@ const inspectedPair = async ( cliTest.for(["full", "compact"] as const)( "gives one comparison for retained, inline, file and mixed sources through MCP and the CLI (%s)", - { timeout: 60_000 }, async (profile, { cli }) => { const { root, one, call, client, inspect, left, right } = await inspectedPair("rea-artifact-sources-", profile); diff --git a/tests/boundary/cli/browserCli.test.ts b/tests/boundary/cli/browserCli.test.ts index 99ee7c2e53..9f374ee60b 100644 --- a/tests/boundary/cli/browserCli.test.ts +++ b/tests/boundary/cli/browserCli.test.ts @@ -12,7 +12,6 @@ import { } from "../../fixtures/fakeCdpBrowser.js"; const browsers: FakeCdpBrowser[] = []; -const INTEGRATION_TEST_TIMEOUT_MS = 60_000; const partialScriptEvents: FakeOptions["commandEvents"] = ( { method }, @@ -118,7 +117,6 @@ cliTest( }); } }, - INTEGRATION_TEST_TIMEOUT_MS, ); cliTest( @@ -208,7 +206,6 @@ cliTest( }); await assertScenarioComparisonPolicy(cli); }, - INTEGRATION_TEST_TIMEOUT_MS, ); cliTest( @@ -295,7 +292,6 @@ cliTest( }, }); }, - INTEGRATION_TEST_TIMEOUT_MS, ); cliTest( @@ -349,7 +345,6 @@ cliTest( }); expect(JSON.stringify(refused)).not.toContain("not-this-browser"); }, - INTEGRATION_TEST_TIMEOUT_MS, ); describe("scenario comparison CLI diagnostics", () => { @@ -721,7 +716,6 @@ describe("browser CLI capture parity", () => { normalized_result: { status: "identical", changed_pixels: 0 }, }); }, - INTEGRATION_TEST_TIMEOUT_MS, ); }); @@ -1003,7 +997,6 @@ cliTest( normalized_result: { overall_status: "changed" }, }); }, - INTEGRATION_TEST_TIMEOUT_MS, ); const scenarioCaptureWithConsole = ( diff --git a/tests/boundary/cli/browserCompareJsonFiles.test.ts b/tests/boundary/cli/browserCompareJsonFiles.test.ts index 96ee1f5cde..e64e9564ed 100644 --- a/tests/boundary/cli/browserCompareJsonFiles.test.ts +++ b/tests/boundary/cli/browserCompareJsonFiles.test.ts @@ -9,7 +9,6 @@ import { createWebScreenshotArtifact } from "../../../src/domain/webScreenshot.j import { cliTest, type TestCli } from "../../support/cli/cliFixture.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; -const INTEGRATION_TEST_TIMEOUT_MS = 60_000; // Linux limits one argument to 128 KiB and macOS all arguments to 1 MiB. const COMMAND_LINE_LIMIT_BYTES = 1024 * 1024; @@ -66,7 +65,6 @@ describe("browser comparison JSON file inputs", () => { normalized_result: { status: "identical", changed_pixels: 0 }, }); }, - INTEGRATION_TEST_TIMEOUT_MS, ); cliTest( @@ -102,7 +100,6 @@ describe("browser comparison JSON file inputs", () => { }, }); }, - INTEGRATION_TEST_TIMEOUT_MS, ); cliTest( @@ -110,7 +107,6 @@ describe("browser comparison JSON file inputs", () => { async ({ cli }) => { await assertPngInputErrorProjections(cli); }, - INTEGRATION_TEST_TIMEOUT_MS, ); cliTest( @@ -140,7 +136,6 @@ describe("browser comparison JSON file inputs", () => { input_reason: "read-failed", }); }, - INTEGRATION_TEST_TIMEOUT_MS, ); }); diff --git a/tests/boundary/cli/browserRelationSets.test.ts b/tests/boundary/cli/browserRelationSets.test.ts new file mode 100644 index 0000000000..80c02a72cb --- /dev/null +++ b/tests/boundary/cli/browserRelationSets.test.ts @@ -0,0 +1,106 @@ +import { expect } from "vitest"; +import { parseEvidence } from "../../../src/domain/evidence.js"; +import { webPageInspectionSchema } from "../../../src/domain/browserObservationSchemas.js"; +import { startFakeCdpBrowser } from "../../fixtures/fakeCdpBrowser.js"; +import { connectLocalToolsMcp } from "../../fixtures/localToolsMcp.js"; +import { cliTest } from "../../support/cli/cliFixture.js"; + +cliTest.for(["CLI", "stdio MCP"] as const)( + "owns recognized relation types per link through %s", + async (surface, { cli }) => { + const browser = await startFakeCdpBrowser({ + commandResult: (command, origin) => + command.method === "DOMSnapshot.captureSnapshot" + ? { + strings: [ + "#document", + "html", + "head", + "link", + "", + "xmlns", + "http://www.w3.org/1999/xhtml", + "rel", + `${"mcp ".repeat(150_000)}model-context model-context AI-PLUGIN`, + "href", + "/agent", + `${origin}/root/page.xhtml`, + "MCP service-desc service-desc mcp unrelated", + "/other-agent", + ], + documents: [ + { + documentURL: 11, + nodes: { + nodeType: [9, 1, 1, 1, 1], + nodeName: [0, 1, 2, 3, 3], + nodeValue: [4, 4, 4, 4, 4], + parentIndex: [-1, 0, 1, 2, 2], + attributes: [[], [5, 6], [], [7, 8, 9, 10], [7, 12, 9, 13]], + }, + }, + ], + } + : undefined, + }); + try { + let document: unknown; + if (surface === "CLI") { + const reply = await cli.run({ + arguments: [ + "inspect-web-page", + browser.endpoint, + "allowed-page", + "--observation-ms", + "0", + "--json", + ], + }); + expect(reply.exitCode, reply.stderr).toBe(0); + document = reply.json; + } else { + const { call } = await connectLocalToolsMcp(); + const reply = await call("inspect_web_page", { + cdp_endpoint: browser.endpoint, + target_id: "allowed-page", + observation_ms: 0, + }); + expect(reply.isError, JSON.stringify(reply)).not.toBe(true); + document = reply.structuredContent; + } + const evidence = parseEvidence(document); + expect(evidence.operation).toBe("inspect_web_page"); + const inspection = webPageInspectionSchema.parse( + evidence.normalized_result, + ); + expect( + inspection.metadata.agent_hints.filter( + ({ mechanism }) => mechanism === "dom_link_rel", + ), + ).toEqual( + [ + ["mcp", "/agent"], + ["model-context", "/agent"], + ["ai-plugin", "/agent"], + ["mcp", "/other-agent"], + ["service-desc", "/other-agent"], + ].map(([declaration, path]) => ({ + mechanism: "dom_link_rel", + declaration, + url: `${browser.allowedOrigin}${path}`, + trust: "page-declared-untrusted", + })), + ); + expect(inspection.metadata.dom_urls).toEqual( + ["/agent", "/other-agent"].map((path, index) => ({ + node_index: index + 3, + attribute: "href", + url: `${browser.allowedOrigin}${path}`, + destination_scope: "approved", + })), + ); + } finally { + await browser.close(); + } + }, +); diff --git a/tests/boundary/cli/browserWorkerOwnershipCli.test.ts b/tests/boundary/cli/browserWorkerOwnershipCli.test.ts index c5e338a093..32d4531270 100644 --- a/tests/boundary/cli/browserWorkerOwnershipCli.test.ts +++ b/tests/boundary/cli/browserWorkerOwnershipCli.test.ts @@ -127,7 +127,6 @@ cliTest( ), ).toBe(true); }, - 20_000, ); cliTest( @@ -205,5 +204,4 @@ cliTest( ), ).toBe(true); }, - 20_000, ); diff --git a/tests/boundary/cli/bundleComparisonCli.test.ts b/tests/boundary/cli/bundleComparisonCli.test.ts index d09f466eff..72dfa6cf80 100644 --- a/tests/boundary/cli/bundleComparisonCli.test.ts +++ b/tests/boundary/cli/bundleComparisonCli.test.ts @@ -265,5 +265,4 @@ cliTest( changes: [], }); }, - 60_000, ); diff --git a/tests/boundary/cli/callerPathSymlinkParentCli.test.ts b/tests/boundary/cli/callerPathSymlinkParentCli.test.ts index a7d4ef2ca0..0e4a7ba36f 100644 --- a/tests/boundary/cli/callerPathSymlinkParentCli.test.ts +++ b/tests/boundary/cli/callerPathSymlinkParentCli.test.ts @@ -46,7 +46,6 @@ describe.skipIf(process.platform === "win32")( ); } }, - 30_000, ); cliTest( @@ -84,7 +83,6 @@ describe.skipIf(process.platform === "win32")( expect(evidence.subject?.digest.sha256, path).toBe(selectedDigest); } }, - 30_000, ); }, ); diff --git a/tests/boundary/cli/cliDecimalArguments.test.ts b/tests/boundary/cli/cliDecimalArguments.test.ts index 29aacd7883..95b0345262 100644 --- a/tests/boundary/cli/cliDecimalArguments.test.ts +++ b/tests/boundary/cli/cliDecimalArguments.test.ts @@ -68,8 +68,16 @@ const REQUIRED_NUMERIC_FIELDS = [ "inspect-web-page options.observationMs", "observe-web-execution options.observationMs", "extract-firmware options.maxDepth", + "trace-web-source-location args.line", + "cutter-command args.expectedGeneration", + "observe-native-ui options.maxNodes", + "inspect-web-network-capture options.record", + "environment-serve options.port", ] as const; +/** Decimal argv tokens the parser admits as numbers, for fields advertised as strings. */ +const PROBE_TOKENS = ["0", "1"] as const; + describe("decimal integer CLI admission", () => { it("rejects 0x10 and 1e3 for every numeric input and keeps an in-range decimal", () => { const cli = createCli({}); @@ -83,7 +91,6 @@ describe("decimal integer CLI admission", () => { const advertised = Schema.toJsonSchema(parser); if (!isRecord(advertised) || !isRecord(advertised.properties)) continue; for (const [name, property] of Object.entries(advertised.properties)) { - if (!numericBranches(property)) continue; const field = parser.shape[name]; if (field === undefined) { failures.push( @@ -92,11 +99,31 @@ describe("decimal integer CLI admission", () => { continue; } const label = `${path} ${group}.${name}`; + // A repeated option parses each argv token as one array item. + const repeated = isRecord(property) && property.type === "array"; + const item = repeated ? property.items : property; + const isolated = z.object({ [name]: field }); + const read = (token: string): unknown => { + const parsed = + group === "options" + ? Parser.parse([flagName(name), token], { options: isolated }) + .options[name] + : Parser.parse([token], { args: isolated }).args[name]; + return repeated && Array.isArray(parsed) ? parsed[0] : parsed; + }; + const readsNumber = (token: string): boolean => { + try { + return typeof read(token) === "number"; + } catch { + return false; + } + }; + // String-advertised token grammars (for example --port) still count. + const probed = PROBE_TOKENS.find(readsNumber); + if (!numericBranches(item) && probed === undefined) continue; let validate: ((value: unknown) => boolean) | undefined; try { - validate = ajv.compile( - property as Parameters[0], - ); + validate = ajv.compile(item as Parameters[0]); } catch (error) { failures.push( `${label}: Ajv could not compile the advertised schema (${error instanceof Error ? error.message : String(error)})`, @@ -107,15 +134,6 @@ describe("decimal integer CLI admission", () => { if (validate(token)) failures.push(`${label}: advertised schema accepts ${token}`); } - const isolated = z.object({ [name]: field }); - const read = (token: string): unknown => { - if (group === "options") { - return Parser.parse([flagName(name), token], { - options: isolated, - }).options[name]; - } - return Parser.parse([token], { args: isolated }).args[name]; - }; for (const token of NON_DECIMAL) { try { read(token); @@ -124,7 +142,9 @@ describe("decimal integer CLI admission", () => { // Decimal tokens are the only numeric spelling. } } - const candidate = bounds(property)[0]?.min; + const candidate = + bounds(item)[0]?.min ?? + (probed === undefined ? undefined : Number(probed)); if (candidate === undefined) { failures.push(`${label}: no in-range decimal candidate`); continue; @@ -138,8 +158,7 @@ describe("decimal integer CLI admission", () => { ); continue; } - const admitsLeadingZeros = - JSON.stringify(property).includes("^[0-9]+$"); + const admitsLeadingZeros = JSON.stringify(item).includes("^[0-9]+$"); try { const parsed = read(`0${decimal}`); if (parsed !== candidate) @@ -152,9 +171,10 @@ describe("decimal integer CLI admission", () => { `${label}: leading-zero decimal was rejected (${error instanceof Error ? error.message : String(error)})`, ); } - if (!(validate(decimal) || validate(candidate))) + // Argv supplies text, so the advertisement must admit the token itself. + if (!validate(decimal)) failures.push( - `${label}: advertised schema rejects in-range ${decimal}`, + `${label}: advertised schema rejects in-range token "${decimal}"`, ); checked.push(label); } @@ -168,3 +188,60 @@ describe("decimal integer CLI admission", () => { expect(failures).toEqual([]); }); }); + +describe("decimal integer CLI advertisement", () => { + const schemaOf = async (command: string): Promise => { + let stdout = ""; + let exitCode = 0; + await createCli({}).serve([command, "--schema", "--json"], { + env: {}, + exit: (code) => { + exitCode = code; + }, + stdout: (text) => { + stdout += text; + }, + }); + expect(exitCode, command).toBe(0); + return JSON.parse(stdout) as unknown; + }; + + it("publishes the token grammar in --schema for arguments and options", async () => { + const location = await schemaOf("trace-web-source-location"); + expect(location).toMatchObject({ + args: { + properties: { + line: { + description: "One-based generated line", + anyOf: [ + { type: "integer", minimum: 1 }, + { type: "string", pattern: "^[0-9]+$" }, + ], + }, + }, + }, + }); + expect(await schemaOf("inspect-web-network-capture")).toMatchObject({ + options: { + properties: { + record: { + type: "array", + items: { + anyOf: [ + { type: "integer", minimum: 0 }, + { type: "string", pattern: "^[0-9]+$" }, + ], + }, + }, + }, + }, + }); + expect(await schemaOf("environment-serve")).toMatchObject({ + options: { + properties: { + port: { type: "string", pattern: "^(?:0|[1-9]\\d*)$" }, + }, + }, + }); + }); +}); diff --git a/tests/boundary/cli/cliInputSchemaRequired.test.ts b/tests/boundary/cli/cliInputSchemaRequired.test.ts index 74f37fd62f..4b09092eb4 100644 --- a/tests/boundary/cli/cliInputSchemaRequired.test.ts +++ b/tests/boundary/cli/cliInputSchemaRequired.test.ts @@ -739,7 +739,12 @@ describe("reference-source outline-depth input schema", () => { const cli = createCli({}); const advertised = await advertisedSchema(cli, "import-reference-source"); const outlineDepth = advertised.options?.properties?.outlineDepth; - expect(outlineDepth).toMatchObject({ type: "integer", minimum: 0 }); + expect(outlineDepth).toMatchObject({ + anyOf: [ + { type: "integer", minimum: 0 }, + { type: "string", pattern: "^[0-9]+$" }, + ], + }); const parser = parserSchema(cli, "import-reference-source", "options"); expect(parser?.safeParse({}).success).toBe(true); @@ -785,7 +790,10 @@ describe("reference-source outline-depth input schema", () => { if (parsed === undefined || !parsed.success) throw new Error(`Expected outline-depth token ${token} to parse`); expect(validate(parsed.data)).toBe(true); + expect(validate({ outlineDepth: token, detail: "summary" })).toBe(true); } + for (const token of ["0x2", "2e0", "-1"]) + expect(validate({ outlineDepth: token, detail: "summary" })).toBe(false); expect(validate({ outlineDepth: -1 })).toBe(false); expect(validate({ outlineDepth: 9007199254740992 })).toBe(false); expect(validate({ outlineDepth: 2 })).toBe(false); diff --git a/tests/boundary/cli/cliIntegerArguments.test.ts b/tests/boundary/cli/cliIntegerArguments.test.ts index 77fc6a710a..7fd51fe70e 100644 --- a/tests/boundary/cli/cliIntegerArguments.test.ts +++ b/tests/boundary/cli/cliIntegerArguments.test.ts @@ -4,11 +4,7 @@ import { join } from "node:path"; import { describe, expect } from "vitest"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; -import { - cliTest, - runFileCli, - type CliResult, -} from "../../support/cli/cliFixture.js"; +import { cliTest, type CliResult } from "../../support/cli/cliFixture.js"; const SESSION_ID = "27d3e3f1-f1e5-49ae-91ec-95af1f343a5a"; const NON_INTEGER_TEXT = [ @@ -132,7 +128,7 @@ describe("CLI integer arguments", () => { cliTest.for(NON_INTEGER_OPTION_TEXT)( "rejects %j as an Android reference overload index", async (value, { cli }) => { - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: [ "trace-android-references", "/nonexistent/missing.apk", @@ -149,7 +145,7 @@ describe("CLI integer arguments", () => { cliTest.for(NON_INTEGER_TEXT)( "rejects %j as a Cutter expected generation", async (value, { cli }) => { - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: ["cutter-command", SESSION_ID, value, "pdi", "--json"], }); @@ -160,7 +156,7 @@ describe("CLI integer arguments", () => { cliTest.for(NON_INTEGER_TEXT)( "rejects %j as a web module script index", async (value, { cli }) => { - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: [ "trace-web-module-imports", "/nonexistent/manifest.json", @@ -176,7 +172,7 @@ describe("CLI integer arguments", () => { cliTest.for(NON_INTEGER_OPTION_TEXT)( "rejects %j in a repeated web capture record selector", async (value, { cli }) => { - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: [ "inspect-web-network-capture", "/nonexistent/capture.qlog", @@ -206,7 +202,7 @@ describe("byte-entropy window options", () => { cliTest.for(values)( `rejects %j as a byte-entropy ${flag} value`, async (value, { cli }) => { - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: [ "inspect-byte-entropy", await entropySample(), @@ -225,7 +221,7 @@ describe("byte-entropy window options", () => { async ({ cli }) => { const path = await entropySample(); const window = async (...flags: string[]): Promise => { - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: ["inspect-byte-entropy", path, ...flags, "--json"], }); expect(result.exitCode, result.stdout).toBe(0); @@ -271,7 +267,7 @@ describe("pyc and BEAM limit options", () => { cliTest.for(values)( `rejects %j as ${command} ${flag}`, async (value, { cli }) => { - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: [ command, "/nonexistent/missing-artifact", @@ -289,7 +285,7 @@ describe("pyc and BEAM limit options", () => { ["inspect-python-pyc", "--max-depth=016"], ["inspect-erlang-beam", "--max-chunks=016"], ] as const)("keeps a decimal %s limit", async ([command, flag], { cli }) => { - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: [command, "/nonexistent/missing-artifact", flag, "--json"], }); @@ -322,7 +318,7 @@ describe("artifact decimal options", () => { cliTest.for(values)( `rejects %j as ${command} ${flag}`, async (value, { cli }) => { - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: [ command, "/nonexistent/missing-artifact", @@ -337,7 +333,7 @@ describe("artifact decimal options", () => { } cliTest("keeps decimal keyed-archive offset and limit", async ({ cli }) => { - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: [ "inspect-keyed-archive", "/nonexistent/missing-archive", diff --git a/tests/boundary/cli/cliJavaScriptResourceClose.test.ts b/tests/boundary/cli/cliJavaScriptResourceClose.test.ts index a3410760eb..cbbceaf5c1 100644 --- a/tests/boundary/cli/cliJavaScriptResourceClose.test.ts +++ b/tests/boundary/cli/cliJavaScriptResourceClose.test.ts @@ -133,10 +133,15 @@ it("shares one in-flight JavaScript worker shutdown between callers", async () = await openCliScope(); const first = closeCliJavaScriptResources(); const second = closeCliJavaScriptResources(); - expect(second).toBe(first); + let secondSettled = false; + void second.then(() => { + secondSettled = true; + }); + await Promise.resolve(); + expect(secondSettled).toBe(false); expect(calls).toBe(1); release(); - await first; + await Promise.all([first, second]); expect(calls).toBe(1); } finally { release(); diff --git a/tests/boundary/cli/cliLogLevelConfiguration.test.ts b/tests/boundary/cli/cliLogLevelConfiguration.test.ts index 9aba657f40..11cfd91609 100644 --- a/tests/boundary/cli/cliLogLevelConfiguration.test.ts +++ b/tests/boundary/cli/cliLogLevelConfiguration.test.ts @@ -1,6 +1,6 @@ import { expect } from "vitest"; -import { cliTest, runFileCli } from "../../support/cli/cliFixture.js"; +import { cliTest } from "../../support/cli/cliFixture.js"; const invalidLogLevelMessage = 'REA configuration is invalid: REA_LOG_LEVEL: Invalid option: expected one of "trace"|"debug"|"info"|"warn"|"error"|"fatal"|"silent".\n'; @@ -23,7 +23,7 @@ for (const level of ["loud", "INFO"]) }; for (const doctor of [ await cli.run(doctorInvocation), - await runFileCli(cli, doctorInvocation), + await cli.run(doctorInvocation), ]) { expect(doctor.exitCode, doctor.stderr).toBe(1); expect(doctor.stderr).toBe(""); @@ -82,8 +82,9 @@ cliTest( arguments: arguments_, environment: { REA_LOG_LEVEL: "loud" }, }; - const fresh = await cli.run(invocation); - const shared = await runFileCli(cli, invocation); + // Compare the launcher itself with the file's shared process. + const fresh = await cli.run({ ...invocation, freshProcess: true }); + const shared = await cli.run(invocation); for (const result of [fresh, shared]) { expect(result.exitCode, JSON.stringify(arguments_)).toBe(1); expect(result.stdout, JSON.stringify(arguments_)).toBe(""); @@ -97,8 +98,8 @@ cliTest( arguments: ["doctor"], environment: { REA_LOG_LEVEL: "loud", COMPLETE: "bash" }, }; - const fresh = await cli.run(completion); - const shared = await runFileCli(cli, completion); + const fresh = await cli.run({ ...completion, freshProcess: true }); + const shared = await cli.run(completion); for (const result of [fresh, shared]) { expect(result.exitCode).toBe(1); expect(result.stdout).toBe(""); diff --git a/tests/boundary/cli/cliOutput.test.ts b/tests/boundary/cli/cliOutput.test.ts index 6b3c970009..85cc02eb1d 100644 --- a/tests/boundary/cli/cliOutput.test.ts +++ b/tests/boundary/cli/cliOutput.test.ts @@ -19,8 +19,6 @@ import { parseCliOutputArguments, } from "../../../src/cliOutput.js"; -const CLI_INTEGRATION_TIMEOUT_MS = 60_000; - describe("CLI output argument boundary", () => { it("requires an exact row selector and explicit columns for delimited formats", () => { expect( @@ -295,14 +293,12 @@ describe("CLI output flag value forms", () => { ]) { const result = await cli.run({ arguments: [...filter, "providers", "--json"], - timeoutMs: 10_000, }); expect(result.exitCode).toBe(0); expect(JSON.parse(result.stdout)).toEqual({}); expect(result.stderr).toBe(""); } }, - CLI_INTEGRATION_TIMEOUT_MS, ); it("splits only the equals forms of valued output flags", () => { @@ -396,7 +392,6 @@ describe("unclosed CLI output filter slices", () => { "--filter-output", "result[0,1].name[", ], - timeoutMs: 3000, }); expect(result.exitCode).toBe(1); expect(result.stdout).toContain("INVALID_OUTPUT_FILTER"); @@ -405,7 +400,6 @@ describe("unclosed CLI output filter slices", () => { } const splitPath = await cli.run({ arguments: ["providers", "--json", "--filter-output", "][,]"], - timeoutMs: 3000, }); expect(splitPath.exitCode).toBe(1); expect(splitPath.json).toMatchObject({ @@ -413,7 +407,6 @@ describe("unclosed CLI output filter slices", () => { error: { code: "INVALID_OUTPUT_FILTER" }, }); }, - CLI_INTEGRATION_TIMEOUT_MS, ); workspaceCliTest( @@ -426,7 +419,6 @@ describe("unclosed CLI output filter slices", () => { "--filter-output", "capabilities[0,1].provider.id,open", ], - timeoutMs: 3000, }); expect(result.exitCode).toBe(0); expect(result.json).toEqual({ @@ -434,7 +426,6 @@ describe("unclosed CLI output filter slices", () => { open: false, }); }, - CLI_INTEGRATION_TIMEOUT_MS, ); }); @@ -455,7 +446,6 @@ describe("CLI output valued-flag parsing", () => { }); } }, - CLI_INTEGRATION_TIMEOUT_MS, ); workspaceCliTest( @@ -470,7 +460,6 @@ describe("CLI output valued-flag parsing", () => { error: { code: "UNSUPPORTED_OUTPUT_COMBINATION" }, }); }, - CLI_INTEGRATION_TIMEOUT_MS, ); workspaceCliTest( @@ -558,7 +547,6 @@ describe("CLI output valued-flag parsing", () => { expect(result.stdout).not.toContain("[truncated:"); } }, - CLI_INTEGRATION_TIMEOUT_MS, ); }); @@ -644,7 +632,6 @@ describe("compiled CLI output boundary", () => { }, }); }, - CLI_INTEGRATION_TIMEOUT_MS, ); workspaceCliTest( @@ -679,7 +666,6 @@ describe("compiled CLI output boundary", () => { expect(result.stdout).toContain("expected string"); } }, - CLI_INTEGRATION_TIMEOUT_MS, ); workspaceCliTest( @@ -710,7 +696,6 @@ describe("compiled CLI output boundary", () => { }, }); }, - CLI_INTEGRATION_TIMEOUT_MS, ); workspaceCliTest( @@ -735,7 +720,6 @@ describe("compiled CLI output boundary", () => { ); expect(result.stdout).toContain("See below for usage."); }, - CLI_INTEGRATION_TIMEOUT_MS, ); }); @@ -766,7 +750,6 @@ describe("compiled CLI artifact diagnostics", () => { }, }); }, - CLI_INTEGRATION_TIMEOUT_MS, ); }); @@ -1061,6 +1044,5 @@ describe("unknown MCP subcommand output", () => { expect(toon.exitCode).toBe(1); expect(toon.stdout).toMatch(/^code: COMMAND_NOT_FOUND\n/u); }, - CLI_INTEGRATION_TIMEOUT_MS, ); }); diff --git a/tests/boundary/cli/cliSessionParity.test.ts b/tests/boundary/cli/cliSessionParity.test.ts new file mode 100644 index 0000000000..de0d2926fe --- /dev/null +++ b/tests/boundary/cli/cliSessionParity.test.ts @@ -0,0 +1,52 @@ +import { expect } from "vitest"; + +import { + cliProcessEnvironment, + cliTest, + definedCliEnvironment, +} from "../../support/cli/cliFixture.js"; + +// The file session and the executable run one shared one-shot owner. Compare +// a fresh process with the session itself: `cli.run` may route either way. +cliTest.for([ + { name: "a formatted version report", arguments: ["--version", "--json"] }, + { + name: "a project-scoped Amp registration refusal", + arguments: ["mcp", "add", "--no-global", "--agent", "amp", "--json"], + }, + { + name: "a rejected output combination", + arguments: ["capabilities", "--json", "--token-limit", "5"], + }, + { + // The session's own stdin carries its frames; a command that reads + // stdin must see the same empty input a fresh process does. + name: "a command reading absent stdin", + arguments: ["compare-wasm-interfaces", "-", "--json"], + }, +])( + "the file session reports $name exactly as a fresh process does", + async ({ arguments: arguments_ }, { cli, cliCommandSession }) => { + const environment = { REA_LOG_LEVEL: "silent" }; + const fresh = await cli.run({ + arguments: arguments_, + environment, + freshProcess: true, + }); + const shared = await cliCommandSession.run({ + arguments: arguments_, + cwd: process.cwd(), + environment: definedCliEnvironment(cliProcessEnvironment(environment)), + }); + expect(fresh.stdout + fresh.stderr).not.toBe(""); + expect({ + exitCode: shared.exitCode, + stdout: shared.stdout, + stderr: shared.stderr, + }).toEqual({ + exitCode: fresh.exitCode, + stdout: fresh.stdout, + stderr: fresh.stderr, + }); + }, +); diff --git a/tests/boundary/cli/cliSetup.test.ts b/tests/boundary/cli/cliSetup.test.ts index 93fe9ba1d4..98fe26ee3f 100644 --- a/tests/boundary/cli/cliSetup.test.ts +++ b/tests/boundary/cli/cliSetup.test.ts @@ -365,7 +365,6 @@ cliTest.skipIf(process.platform === "win32")( XDG_DATA_HOME: join(home, ".local", "share"), XDG_CACHE_HOME: join(home, ".cache"), }, - timeoutMs: 20_000, }); const planned = await setup("--dry-run"); diff --git a/tests/boundary/cli/clientProfileLifecycle.test.ts b/tests/boundary/cli/clientProfileLifecycle.test.ts index 6667e6b186..fd9da15c50 100644 --- a/tests/boundary/cli/clientProfileLifecycle.test.ts +++ b/tests/boundary/cli/clientProfileLifecycle.test.ts @@ -306,7 +306,6 @@ cliTest( XDG_CONFIG_HOME: join(root, "xdg-config"), XDG_CACHE_HOME: join(root, "xdg-cache"), }, - timeoutMs: 20_000, }); expect(result.exitCode, result.stderr).toBe(0); @@ -352,7 +351,6 @@ for (const group of groups) { arguments: arguments_, cwd: home, environment, - timeoutMs: 20_000, }); const planned = await run([ "setup", @@ -470,7 +468,6 @@ cliTest("uninstall names each retained skill directory", async ({ cli }) => { USERPROFILE: home, CLAUDE_CONFIG_DIR: join(home, ".claude"), }, - timeoutMs: 20_000, }); expect(result.exitCode, result.stderr).toBe(0); diff --git a/tests/boundary/cli/clientProfiles.test.ts b/tests/boundary/cli/clientProfiles.test.ts index d8ff64e722..261b7de194 100644 --- a/tests/boundary/cli/clientProfiles.test.ts +++ b/tests/boundary/cli/clientProfiles.test.ts @@ -33,7 +33,6 @@ cliTest( USERPROFILE: home, CLAUDE_CONFIG_DIR: "", }, - timeoutMs: 20_000, }); const setup = (flags: readonly string[] = []) => run(["setup", "--client", "claude_code", ...flags, "--json"]); @@ -151,7 +150,6 @@ for (const policy of [{ excluded: ["rea"] }, { allowed: ["other"] }]) { arguments: arguments_, cwd: home, environment: { HOME: home, USERPROFILE: home }, - timeoutMs: 20_000, }); const setup = await run([ "setup", @@ -221,7 +219,6 @@ cliTest( arguments: arguments_, cwd: home, environment: { HOME: home, USERPROFILE: home }, - timeoutMs: 20_000, }); const before = await run(["doctor", "--client", "opencode", "--json"]); expect(before.exitCode).toBe(1); diff --git a/tests/boundary/cli/cutterCommands.test.ts b/tests/boundary/cli/cutterCommands.test.ts index 28d17503d2..95031e2599 100644 --- a/tests/boundary/cli/cutterCommands.test.ts +++ b/tests/boundary/cli/cutterCommands.test.ts @@ -1,11 +1,10 @@ import { createServer } from "node:net"; import { createHmac, randomUUID } from "node:crypto"; -import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; +import { mkdtemp, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { afterEach, describe, expect, it } from "vitest"; -import ts from "typescript6"; import { serveCli } from "./cliServeHarness.js"; import { @@ -24,64 +23,6 @@ const listen = async ( return address.port; }; -const assertRealVerifierJsonInspection = async ( - directory: string, - commandJsonModes: readonly boolean[], -): Promise => { - // Exercise the actual real-verifier command selection against the public CLI. - const source = ts.createSourceFile( - "verify-real-cutter.mjs", - await readFile("scripts/verify-real-cutter.mjs", "utf8"), - ts.ScriptTarget.Latest, - true, - ts.ScriptKind.JS, - ); - const observations: string[][] = []; - const visit = (node: ts.Node): void => { - if ( - ts.isCallExpression(node) && - ts.isIdentifier(node.expression) && - node.expression.text === "runCli" && - ts.isStringLiteral(node.arguments[0]!) && - node.arguments[0].text === "cutter-command" && - node.arguments[3] !== undefined && - ts.isStringLiteral(node.arguments[3]) && - node.arguments[3].text === "ij" - ) { - observations.push( - node.arguments.slice(4).map((argument) => { - if (!ts.isStringLiteral(argument)) - throw new Error( - "Verifier inspection flags must be explicit arguments", - ); - return argument.text; - }), - ); - } - ts.forEachChild(node, visit); - }; - visit(source); - expect(observations).toHaveLength(1); - const verification = await serveCli( - [ - "cutter-command", - SESSION_ID, - "0", - "ij", - ...observations[0]!, - "--format", - "json", - ], - { REA_CUTTER_BRIDGE_DIR: directory }, - ); - expect(verification.exitCode).toBe(0); - expect(commandJsonModes.at(-1)).toBe(true); - expect(JSON.parse(verification.stdout)).toMatchObject({ - predicate_type: "rea.cutter.command-observation", - raw_result: { answer: 42 }, - }); -}; - describe("Cutter CLI commands", () => { let directory: string | undefined; let server: ReturnType | undefined; @@ -354,7 +295,5 @@ describe("Cutter CLI command output flags", () => { expect(rejected.stdout).toContain("Unknown flag: --json"); } expect(commandJsonModes).toHaveLength(commandsBeforeRejectedFlags); - - await assertRealVerifierJsonInspection(directory, commandJsonModes); }); }); diff --git a/tests/boundary/cli/debuggerOneShotExit.test.ts b/tests/boundary/cli/debuggerOneShotExit.test.ts index 44d38848ab..4daf27251d 100644 --- a/tests/boundary/cli/debuggerOneShotExit.test.ts +++ b/tests/boundary/cli/debuggerOneShotExit.test.ts @@ -143,7 +143,6 @@ type DebuggerName = keyof typeof debuggers; type FakeMode = "clean" | "malformed" | "unanswered"; const EXIT_DEADLINE_MS = 20_000; -const TEST_DEADLINE_MS = 2 * EXIT_DEADLINE_MS; const isAlive = (pid: number): boolean => { try { @@ -291,7 +290,6 @@ describe.skipIf(process.platform === "win32")( ["gdb", "unanswered"], ] as const)( "ends one-shot %s with a retained debugger whose cleanup is %s", - { timeout: TEST_DEADLINE_MS }, async ([name, mode], { processes, cliCompileCache }) => { const tty = await rizinTestInferiorTty(); const launch = await prepareLaunch(name, mode, cliCompileCache); @@ -317,7 +315,6 @@ describe.skipIf(process.platform === "win32")( cliTest.for(["rizin", "gdb"] as const)( "keeps a one-shot %s success and its exit status when cleanup is confirmed", - { timeout: TEST_DEADLINE_MS }, async (name, { processes, cliCompileCache }) => { const tty = await rizinTestInferiorTty(); const launch = await prepareLaunch(name, "clean", cliCompileCache); @@ -343,7 +340,6 @@ describe.skipIf(process.platform === "win32")( cliTest.for(["rizin", "gdb"] as const)( "ends one-shot %s with its signal status when interrupted during final cleanup", - { timeout: TEST_DEADLINE_MS }, async (name, { processes, cliCompileCache }) => { const tty = await rizinTestInferiorTty(); const launch = await prepareLaunch(name, "unanswered", cliCompileCache); @@ -385,7 +381,6 @@ describe.skipIf(process.platform === "win32")( ["rizin", "SIGTERM"], ] as const)( "ends the stdio MCP server holding a retained %s session on %s and names it", - { timeout: TEST_DEADLINE_MS }, async ([name, trigger], { processes, cliCompileCache }) => { const tty = await rizinTestInferiorTty(); const launch = await prepareLaunch(name, "malformed", cliCompileCache); diff --git a/tests/boundary/cli/decompileAllCli.test.ts b/tests/boundary/cli/decompileAllCli.test.ts index bfb1934d89..3c4449ee54 100644 --- a/tests/boundary/cli/decompileAllCli.test.ts +++ b/tests/boundary/cli/decompileAllCli.test.ts @@ -2,6 +2,7 @@ import { readdir, readFile, writeFile } from "node:fs/promises"; import { join } from "node:path"; import { describe, expect } from "vitest"; +import { decompileDirectoryIndexSchema } from "../../../src/contracts/decompileDirectory.js"; import { elf } from "../../../src/domain/binaryTarget.fixture.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; import { cliTest } from "../../support/cli/cliFixture.js"; @@ -97,6 +98,50 @@ describe("decompile-all CLI", () => { counts: { selected: 2, written: 1, skipped: 1, remaining: 0 }, }, }); + // Kuna lacks procedure_info, so every retained record keeps an + // unavailable body. A resume that writes nothing still summarizes them. + const bodyCaveat = expect.stringMatching( + /^2 exported function record.*procedure_info is unavailable/u, + ); + expect(resumed.json).toMatchObject({ + normalized_result: { limitations: [bodyCaveat] }, + }); + const noop = await cli.run({ + arguments: [ + "decompile-all", + target, + "--provider", + "kuna", + "--output", + "out", + "--resume", + "--address-start", + "0X1000", + "--address-end", + "0X1040", + "--format", + "json", + ], + cwd: root, + environment, + }); + expect(noop.exitCode, JSON.stringify(noop.json)).toBe(0); + expect(noop.json).toMatchObject({ + normalized_result: { + counts: { selected: 2, requested: 0, skipped: 2 }, + limitations: [bodyCaveat], + }, + }); + const index = decompileDirectoryIndexSchema.parse( + JSON.parse( + await readFile(join(root, "out", "decompile-index.json"), "utf8"), + ), + ); + expect(index.limitations).toEqual([bodyCaveat]); + expect(index.functions.map((record) => record.body.available)).toEqual([ + false, + false, + ]); }, ); }); diff --git a/tests/boundary/cli/defaultAmpRegistration.test.ts b/tests/boundary/cli/defaultAmpRegistration.test.ts index b735f5581c..c0041d3d53 100644 --- a/tests/boundary/cli/defaultAmpRegistration.test.ts +++ b/tests/boundary/cli/defaultAmpRegistration.test.ts @@ -49,7 +49,6 @@ cliTest( ], cwd: f.home, environment: f.environment, - timeoutMs: 20_000, }); const first = await run(); expect(first.exitCode, first.stderr).toBe(0); @@ -107,7 +106,6 @@ cliTest( arguments: ["mcp", "add", ...(json ? ["--json"] : [])], cwd: f.home, environment: f.environment, - timeoutMs: 20_000, }); expect(result.exitCode).toBe(1); const document = report(result.stdout, json); @@ -142,7 +140,6 @@ cliTest( arguments: arguments_, cwd: f.home, environment: f.environment, - timeoutMs: 20_000, }); expect(result.exitCode, result.stdout).toBe(0); expect(await readFile(f.settings, "utf8")).toBe("broken JSON\n"); @@ -167,7 +164,6 @@ cliTest( arguments: arguments_, cwd: f.home, environment: f.environment, - timeoutMs: 20_000, }); const registered = await run(); expect(registered.exitCode, registered.stdout).toBe(0); @@ -235,7 +231,6 @@ cliTest( ], cwd: f.home, environment: f.environment, - timeoutMs: 20_000, }); expect(result.exitCode, result.stdout).toBe(0); expect(report(result.stdout, true)).toMatchObject({ @@ -265,7 +260,6 @@ cliTest( arguments: ["mcp", "add", ...options, "--json"], cwd: f.home, environment: f.environment, - timeoutMs: 20_000, }); expect(result.exitCode, result.stderr).toBe(1); expect(result.json).toMatchObject({ diff --git a/tests/boundary/cli/desktopBuildHost.test.ts b/tests/boundary/cli/desktopBuildHost.test.ts index 22c1494b92..6bbc48e7d9 100644 --- a/tests/boundary/cli/desktopBuildHost.test.ts +++ b/tests/boundary/cli/desktopBuildHost.test.ts @@ -51,7 +51,6 @@ const buildHost = async (launcher = "/opt/rea/scripts/rea.mjs") => { cliTest.skipIf(!["darwin", "linux"].includes(process.platform))( "doctor checks the selected client's runner even when it is not on PATH", - { timeout: 60_000 }, async ({ cli }) => { const { environment, home } = await buildHost(); const bin = join(home, "..", "bin"); @@ -92,7 +91,6 @@ cliTest.skipIf(!["darwin", "linux"].includes(process.platform))( "--json", ], environment, - timeoutMs: 45_000, }); const report = z .object({ scope_checks: z.array(checkSchema) }) @@ -116,7 +114,6 @@ cliTest.skipIf(!["darwin", "linux"].includes(process.platform))( cliTest.skipIf(!["darwin", "linux"].includes(process.platform))( "doctor accepts client paths matching resolved build defaults", - { timeout: 60_000 }, async ({ cli }) => { const { environment, home } = await buildHost(); const bin = join(home, "..", "bin"); @@ -154,7 +151,6 @@ cliTest.skipIf(!["darwin", "linux"].includes(process.platform))( "--json", ], environment, - timeoutMs: 45_000, }); const report = z .object({ scope_checks: z.array(checkSchema) }) @@ -169,7 +165,6 @@ cliTest.skipIf(!["darwin", "linux"].includes(process.platform))( cliTest.skipIf(!["darwin", "linux"].includes(process.platform))( "doctor checks a desktop build host and names settings without their values", - { timeout: 60_000 }, async ({ cli }) => { const { environment } = await buildHost(); @@ -186,7 +181,6 @@ cliTest.skipIf(!["darwin", "linux"].includes(process.platform))( ...environment, REA_DOCKER_COMMAND: join(environment.HOME, "..", "bin", "docker"), }, - timeoutMs: 45_000, }); expect(result.exitCode, result.stderr).toBe(1); @@ -231,7 +225,6 @@ cliTest.skipIf(!["darwin", "linux"].includes(process.platform))( cliTest.skipIf(!["darwin", "linux"].includes(process.platform))( "setup plans the runtime archive and the build settings for a client", - { timeout: 60_000 }, async ({ cli }) => { const { environment } = await buildHost(resolve("scripts/rea.mjs")); @@ -246,7 +239,6 @@ cliTest.skipIf(!["darwin", "linux"].includes(process.platform))( "--json", ], environment, - timeoutMs: 45_000, }); const plan = z diff --git a/tests/boundary/cli/developmentTests.test.ts b/tests/boundary/cli/developmentTests.test.ts index c29a19dcde..2088c552f9 100644 --- a/tests/boundary/cli/developmentTests.test.ts +++ b/tests/boundary/cli/developmentTests.test.ts @@ -163,6 +163,74 @@ describe("development test runner symlink aliases", () => { }); }); +describe("development test runner prerequisite ownership", () => { + it("prepares artifacts for filesystem aliases without folding distinct filenames", async () => { + const root = await fixture(); + const canonical = "tests/boundary/mcp/deferredRuntime.test.ts"; + const selected = "tests/boundary/mcp/DEFERREDRuntime.test.ts"; + await mkdir(join(root, "tests/boundary/mcp"), { recursive: true }); + await writeFile( + join(root, canonical), + "// Artifact-consuming test fixture\n", + ); + let sameFile = true; + try { + await lstat(join(root, selected)); + } catch (cause) { + if ( + !(cause instanceof Error) || + !("code" in cause) || + cause.code !== "ENOENT" + ) + throw cause; + sameFile = false; + await writeFile(join(root, selected), "// Distinct test fixture\n"); + } + for (const path of [canonical, selected]) { + const result = await testCommand(root, "focused", path, "--dry-run"); + expect(JSON.parse(result.stdout)).toMatchObject({ + paths: [path], + needsBuild: true, + artifactTasks: + path === canonical || sameFile ? ["artifacts:mcp-catalog"] : [], + vitestArguments: ["run", path], + }); + } + }); + + it("retains built-project admission for an aliased provider conformance test", async ({ + skip, + }) => { + const root = await fixture(); + const directory = "tests/conformance/providers/hopper"; + const canonical = `${directory}/realHopperSearch.test.ts`; + const selected = `${directory}/REALHopperSearch.test.ts`; + await mkdir(join(root, directory), { recursive: true }); + await writeFile(join(root, canonical), "// Built conformance fixture\n"); + try { + await lstat(join(root, selected)); + } catch (cause) { + if (cause instanceof Error && "code" in cause && cause.code === "ENOENT") + skip("The host distinguishes the selected filename casing"); + throw cause; + } + const result = await testCommand(root, "focused", selected, "--dry-run"); + expect(JSON.parse(result.stdout)).toMatchObject({ + paths: [selected], + needsBuild: true, + vitestArguments: ["run", selected], + }); + await expect( + testCommand(root, "local", selected, "--dry-run"), + ).rejects.toMatchObject({ + code: 1, + stderr: expect.stringContaining( + `Use test:focused for built test projects: ${selected}`, + ), + }); + }); +}); + describe("development test runner exact selection", () => { it("matches selected path casing to the host filesystem", async () => { const root = await fixture(); @@ -366,7 +434,6 @@ describe("development test runner cancellation", () => { await closed; } }, - 20_000, ); }); @@ -442,7 +509,7 @@ describe("development test runner public CLI", () => { expect(changed.stdout).toContain("1 passed"); const focused = await testCommand(root, "focused", "src/value.test.ts"); expect(focused.stdout).toContain("1 passed"); - }, 60_000); + }); it("reports a missing comparison base and rejects missing or compiled-boundary paths", async () => { const root = await fixture(); diff --git a/tests/boundary/cli/dispatcher.test.ts b/tests/boundary/cli/dispatcher.test.ts index f1804a99f8..da05a9c482 100644 --- a/tests/boundary/cli/dispatcher.test.ts +++ b/tests/boundary/cli/dispatcher.test.ts @@ -1,4 +1,4 @@ -import { access, copyFile, cp } from "node:fs/promises"; +import { access, copyFile, cp, symlink } from "node:fs/promises"; import { join, resolve } from "node:path"; import { pathToFileURL } from "node:url"; @@ -8,9 +8,6 @@ import { unsupportedRuntimeMessage } from "../../../scripts/lib/runtime-admissio import { workspaceCliTest } from "../../support/cli/workspaceCliFixture.js"; import type { TestWorkspace } from "../../support/workspace/workspaceFixture.js"; -const CLI_OUTPUT_RUNTIME = - "export const cliConfigurationError = () => undefined; export const parseCliOutputArguments = () => ({ validation: { ok: true } }); export const renderCliOutputArgumentError = () => ''; export const renderEmptyFilteredCliOutput = () => undefined; export const renderVersionReport = () => undefined;\n"; - const installCliRuntime = async ( workspace: TestWorkspace, cliSource: string, @@ -18,10 +15,18 @@ const installCliRuntime = async ( ): Promise => { await workspace.mkdir("scripts/lib"); await workspace.mkdir("dist"); - // The bundle's entrypoints and shared chunks form one emitted runtime. + // The bundle's entrypoints and shared chunks form one emitted runtime. The + // one-shot owner and its output admission stay production code; only the + // command runtime and result output are replaced below. await cp("dist/cli-runtime", workspace.path("dist/cli-runtime"), { recursive: true, }); + // Production chunks import their packages from the installation root. + await symlink( + resolve("node_modules"), + workspace.path("node_modules"), + "junction", + ); await Promise.all([ copyFile("scripts/rea.mjs", workspace.path("scripts/rea.mjs")), copyFile( @@ -37,7 +42,6 @@ const installCliRuntime = async ( JSON.stringify({ type: "module", version: "1.0.0" }), ), workspace.write("dist/cli-runtime/cli.js", cliSource), - workspace.write("dist/cli-runtime/cliOutput.js", CLI_OUTPUT_RUNTIME), workspace.write("dist/cli-runtime/cliResultOutput.js", resultOutput), ]); }; @@ -57,11 +61,6 @@ describe("version report dispatch", () => { version: "1.0.0", }), ); - // Use the built production preflight, while making full CLI imports fail. - await workspace.write( - "dist/cli-runtime/cliOutput.js", - `export * from ${JSON.stringify(pathToFileURL(resolve("dist/cli-runtime/cliOutput.js")).href)};\n`, - ); const invoke = (arguments_: readonly string[], invalidLogLevel = false) => processes.run( process.execPath, @@ -112,11 +111,6 @@ describe("doctor configuration dispatch", () => { workspace, `process.stderr.write(${JSON.stringify(`${imported}\n`)}); export const closeCliResources = () => {}; export const createCli = () => ({ serve: (args, options) => options.stdout(JSON.stringify({ adapter: "cli", args })) });\n`, ); - // Both token ownership and output/configuration admission use production code. - await workspace.write( - "dist/cli-runtime/cliOutput.js", - `export * from ${JSON.stringify(pathToFileURL(resolve("dist/cli-runtime/cliOutput.js")).href)};\n`, - ); const invoke = (args: readonly string[], completion = "") => processes.run( process.execPath, @@ -595,19 +589,10 @@ describe("project-scoped Amp registration dispatch", () => { }, ); - const installProductionOutputRuntime = async (workspace: TestWorkspace) => { - await installGuardRuntime(workspace); - // The guard's structured refusal renders through production output code. - await workspace.write( - "dist/cli-runtime/cliOutput.js", - `export * from ${JSON.stringify(pathToFileURL(resolve("dist/cli-runtime/cliOutput.js")).href)};\n`, - ); - }; - workspaceCliTest( "renders the Amp scope refusal as a structured document when a structured format is requested", async ({ processes, workspace }) => { - await installProductionOutputRuntime(workspace); + await installGuardRuntime(workspace); const invoke = (arguments_: readonly string[]) => processes.run( process.execPath, diff --git a/tests/boundary/cli/doctorHopperDiscovery.test.ts b/tests/boundary/cli/doctorHopperDiscovery.test.ts index 7e9c692bed..d099756d4b 100644 --- a/tests/boundary/cli/doctorHopperDiscovery.test.ts +++ b/tests/boundary/cli/doctorHopperDiscovery.test.ts @@ -178,7 +178,6 @@ cliTest.skipIf(!["darwin", "linux"].includes(process.platform))( REA_HOPPER_TEST_MARKER: launchMarker, GHIDRA_INSTALL_DIR: join(home, "missing-ghidra"), }, - timeoutMs: 20_000, }); const hopperReady = process.platform === "darwin"; diff --git a/tests/boundary/cli/doctorRegistrationEnvironment.test.ts b/tests/boundary/cli/doctorRegistrationEnvironment.test.ts index 888c6e186d..d337548b4e 100644 --- a/tests/boundary/cli/doctorRegistrationEnvironment.test.ts +++ b/tests/boundary/cli/doctorRegistrationEnvironment.test.ts @@ -75,7 +75,6 @@ const run = async ( const result = await cli.run({ arguments: [...arguments_, "--json"], environment, - timeoutMs: 30_000, }); return { result, report: reportSchema.safeParse(result.json) }; }; @@ -97,7 +96,6 @@ const CHECK = "registration_environment:claude_code"; cliTest.skipIf(!["darwin", "linux"].includes(process.platform))( "doctor does not let a configured terminal stand in for an unconfigured registration", - { timeout: 120_000 }, async ({ cli }) => { const root = await createTestTempDirectory("rea-doctor-registration-env-"); const home = join(root, "home"); diff --git a/tests/boundary/cli/domValueLengthUnits.test.ts b/tests/boundary/cli/domValueLengthUnits.test.ts index eb3bf4c656..d1583bb9a0 100644 --- a/tests/boundary/cli/domValueLengthUnits.test.ts +++ b/tests/boundary/cli/domValueLengthUnits.test.ts @@ -193,7 +193,7 @@ cliTest( before.dom = { total_nodes: 0, nodes: [] }; after.dom = { total_nodes: 0, nodes: [] }; } - const result = await cli.sharedCliRun!({ + const result = await cli.run({ arguments: [ "compare-web-captures", JSON.stringify({ inspection: before }), diff --git a/tests/boundary/cli/dylibResolutionCli.test.ts b/tests/boundary/cli/dylibResolutionCli.test.ts index 703596818a..a06e5231e3 100644 --- a/tests/boundary/cli/dylibResolutionCli.test.ts +++ b/tests/boundary/cli/dylibResolutionCli.test.ts @@ -17,11 +17,7 @@ import { import { dylibResolutionResultSchema } from "../../../src/domain/apple/dylibResolution.js"; import { parseEvidence } from "../../../src/domain/evidence.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; -import { - cliTest, - runFileCli, - type TestCli, -} from "../../support/cli/cliFixture.js"; +import { cliTest, type TestCli } from "../../support/cli/cliFixture.js"; const ENVIRONMENT = { REA_LOG_LEVEL: "silent", REA_ANALYSIS_PROVIDER: "auto" }; @@ -50,7 +46,7 @@ const expectRootInputError = async ( readonly message: string; }, ): Promise => { - const rejected = await runFileCli(cli, { + const rejected = await cli.run({ arguments: ["trace-dylib-resolution", app, "--root", root, "--json"], environment: ENVIRONMENT, }); @@ -121,7 +117,7 @@ cliTest.skipIf(process.platform !== "linux")( ]), machoImage({}), ); - const observed = await runFileCli(cli, { + const observed = await cli.run({ arguments: ["trace-dylib-resolution", app, "--json"], environment: ENVIRONMENT, }); @@ -141,7 +137,7 @@ cliTest( async ({ cli }) => { const app = await fixtureApp(); await symlink("unexamined", join(app, "Contents", "LinkedResources")); - const observed = await runFileCli(cli, { + const observed = await cli.run({ arguments: ["trace-dylib-resolution", app, "--json"], environment: ENVIRONMENT, }); @@ -179,7 +175,7 @@ cliTest( ], }), }); - const output = await runFileCli(cli, { + const output = await cli.run({ arguments: ["trace-dylib-resolution", app, "--json"], environment: ENVIRONMENT, }); @@ -373,7 +369,7 @@ for (const dependency of ["eager", "lazy", "none", "conditional"] as const) { async ({ cli }) => { const { app, root, nested, uncertain } = await loadedSiblingFixture(dependency); - const output = await runFileCli(cli, { + const output = await cli.run({ arguments: ["trace-dylib-resolution", join(app, root), "--json"], environment: ENVIRONMENT, }); @@ -466,7 +462,7 @@ for (const dependency of ["eager", "lazy", "none", "conditional"] as const) { describe("trace-dylib-resolution CLI", () => { cliTest("traces every executable root of an app bundle", async ({ cli }) => { const app = await fixtureApp(); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: ["trace-dylib-resolution", app, "--json"], environment: ENVIRONMENT, }); @@ -517,7 +513,7 @@ describe("trace-dylib-resolution CLI", () => { "narrows roots and architecture, and rejects escaping roots", async ({ cli }) => { const app = await fixtureApp(); - const narrowed = await runFileCli(cli, { + const narrowed = await cli.run({ arguments: [ "trace-dylib-resolution", app, @@ -573,7 +569,7 @@ describe("trace-dylib-resolution CLI", () => { }), "unrelated.txt": "not traversed", }); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: [ "trace-dylib-resolution", join(directory, "tool"), @@ -598,7 +594,7 @@ describe("trace-dylib-resolution CLI", () => { async ({ cli }) => { const app = await fixtureApp(); await withBrokenHelper(app); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: ["trace-dylib-resolution", app, "--json"], environment: ENVIRONMENT, }); @@ -649,7 +645,7 @@ cliTest( }); } - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: ["trace-dylib-resolution", app, "--json"], environment: ENVIRONMENT, }); @@ -706,7 +702,7 @@ cliTest( commands: [buildVersionCommand(1)], }), }); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: ["trace-dylib-resolution", join(directory, "tool"), "--json"], environment: ENVIRONMENT, }); @@ -756,7 +752,7 @@ describe.skipIf(process.platform === "win32" || process.getuid?.() === 0)( }); await chmod(join(directory, "libsecret.dylib"), 0o000); try { - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: [ "trace-dylib-resolution", join(directory, "tool"), @@ -804,7 +800,7 @@ cliTest( ], }), }); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: [ "trace-dylib-resolution", app, @@ -877,7 +873,7 @@ cliTest( { cpu: CPU.x86_64, bytes: machoImage({ cpu: CPU.x86_64 }) }, ]), }); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: [ "trace-dylib-resolution", app, @@ -931,7 +927,7 @@ cliTest( commands: [buildVersionCommand(1)], }), }); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: [ "trace-dylib-resolution", app, @@ -988,7 +984,7 @@ cliTest( ], }), ); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: ["trace-dylib-resolution", path, "--json"], environment: ENVIRONMENT, }); @@ -1036,7 +1032,7 @@ describe("trace-dylib-resolution root enumeration", () => { "classifies every Mach-O among more files than one batch of magic reads", async ({ cli }) => { const app = await crowdedApp(); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: ["trace-dylib-resolution", app, "--json"], environment: ENVIRONMENT, }); @@ -1067,7 +1063,7 @@ describe("trace-dylib-resolution root enumeration", () => { await writeFile(secret, "x"); await chmod(secret, 0o000); try { - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: ["trace-dylib-resolution", app, "--json"], environment: ENVIRONMENT, }); diff --git a/tests/boundary/cli/electronCli.test.ts b/tests/boundary/cli/electronCli.test.ts index 5b8e36d678..6a9957bef9 100644 --- a/tests/boundary/cli/electronCli.test.ts +++ b/tests/boundary/cli/electronCli.test.ts @@ -26,7 +26,6 @@ import { JAVASCRIPT_RUNTIME_RECONCILIATION_EXAMPLE } from "../../../src/contract const cliProcesses = createTestProcessRegistry(); const cli = createTestCli(cliProcesses); -const INTEGRATION_TEST_TIMEOUT_MS = 20_000; const browsers: FakeCdpBrowser[] = []; const temporary: string[] = []; @@ -44,121 +43,113 @@ afterEach(async () => { ]); }); -it( - "returns the same endpoint-scoped Electron Evidence contracts", - async () => { - const root = await createTestTempDirectory("rea-electron-cli-"); - temporary.push(root); - await writeFile(join(root, "index.html"), ""); - await writeFile(join(root, "app.js"), "export const app = true;"); - const browser = await startFakeCdpBrowser({ - electronFileUrl: `${pathToFileURL(join(root, "index.html")).href}?next=%2Fsettings#%5Csettings`, - commandEvents: ({ method }) => - method === "Debugger.enable" - ? [ - { - method: "Debugger.scriptParsed", - sessionId: "session-1", - params: { - scriptId: "partial-script", - url: pathToFileURL(join(root, "app.js")).href, - executionContextId: 1, - }, +it("returns the same endpoint-scoped Electron Evidence contracts", async () => { + const root = await createTestTempDirectory("rea-electron-cli-"); + temporary.push(root); + await writeFile(join(root, "index.html"), ""); + await writeFile(join(root, "app.js"), "export const app = true;"); + const browser = await startFakeCdpBrowser({ + electronFileUrl: `${pathToFileURL(join(root, "index.html")).href}?next=%2Fsettings#%5Csettings`, + commandEvents: ({ method }) => + method === "Debugger.enable" + ? [ + { + method: "Debugger.scriptParsed", + sessionId: "session-1", + params: { + scriptId: "partial-script", + url: pathToFileURL(join(root, "app.js")).href, + executionContextId: 1, }, - ] - : undefined, - }); - browsers.push(browser); - const environment = { ...process.env }; - const listed = parseEvidence( - await runCli( - ["list-electron-targets", browser.endpoint, "--json"], - environment, - ), - ); - expect(listed).toMatchObject({ - operation: "list_electron_targets", - environment: { - id: expect.any(String), - platform: "unknown", - architecture: "unknown", - isolation: "none", - }, - limitations: expect.arrayContaining([ - expect.stringMatching(/platform.*architecture.*not observed/iu), - ]), - normalized_result: { - targets: [{ target_id: "electron-page" }], - }, - }); - const inspected = await runCli( - [ - "inspect-electron-page", - browser.endpoint, - "electron-page", - "--observation-ms", - "0", - "--json", - ], + }, + ] + : undefined, + }); + browsers.push(browser); + const environment = { ...process.env }; + const listed = parseEvidence( + await runCli( + ["list-electron-targets", browser.endpoint, "--json"], environment, - ); - expect(inspected).toMatchObject({ - operation: "inspect_electron_page", - environment: listed.environment, - provider: { id: "rea-cdp-electron" }, - normalized_result: { - target: { file_path: join(root, "index.html") }, - scripts: { - items: [{ cdp_hash: null, length: null, is_module: null }], - }, + ), + ); + expect(listed).toMatchObject({ + operation: "list_electron_targets", + environment: { + id: expect.any(String), + platform: "unknown", + architecture: "unknown", + isolation: "none", + }, + limitations: expect.arrayContaining([ + expect.stringMatching(/platform.*architecture.*not observed/iu), + ]), + normalized_result: { + targets: [{ target_id: "electron-page" }], + }, + }); + const inspected = await runCli( + [ + "inspect-electron-page", + browser.endpoint, + "electron-page", + "--observation-ms", + "0", + "--json", + ], + environment, + ); + expect(inspected).toMatchObject({ + operation: "inspect_electron_page", + environment: listed.environment, + provider: { id: "rea-cdp-electron" }, + normalized_result: { + target: { file_path: join(root, "index.html") }, + scripts: { + items: [{ cdp_hash: null, length: null, is_module: null }], }, - }); - }, - INTEGRATION_TEST_TIMEOUT_MS, -); + }, + }); +}); -it( - "reports rejected Electron CLI arguments with structured issue details", - async () => { - const invalidTargetList = await runCliResult( - ["list-electron-targets", "not a url", "--json"], - process.env, - ); - expect(invalidTargetList.signal).toBeNull(); - expect(invalidTargetList.exitCode).toBe(1); - expect(invalidTargetList.json).toMatchObject({ - error: "Electron analysis failed", - code: "invalid_request", - category: "invalid_input", - details: { - operation: "list_electron_targets", - issues: [ - { - path: ["cdp_endpoint"], - reason: "invalid_value", - }, - ], - }, - }); - - const invalidPageInspect = await runCliResult( - ["inspect-electron-page", "http://127.0.0.1:9222", "", "--json"], - process.env, - ); - expect(invalidPageInspect.signal).toBeNull(); - expect(invalidPageInspect.exitCode).toBe(1); - expect(invalidPageInspect.json).toMatchObject({ - code: "VALIDATION_ERROR", - fieldErrors: [ +it("reports rejected Electron CLI arguments with structured issue details", async () => { + const invalidTargetList = await runCliResult( + ["list-electron-targets", "not a url", "--json"], + process.env, + ); + expect(invalidTargetList.signal).toBeNull(); + expect(invalidTargetList.exitCode).toBe(1); + expect(invalidTargetList.json).toMatchObject({ + error: "Electron analysis failed", + code: "invalid_request", + category: "invalid_input", + details: { + operation: "list_electron_targets", + issues: [ { - code: "too_small", - path: "targetId", + path: ["cdp_endpoint"], + reason: "invalid_value", }, ], - }); - }, - INTEGRATION_TEST_TIMEOUT_MS, -); + }, + }); + + const invalidPageInspect = await runCliResult( + ["inspect-electron-page", "http://127.0.0.1:9222", "", "--json"], + process.env, + ); + expect(invalidPageInspect.signal).toBeNull(); + expect(invalidPageInspect.exitCode).toBe(1); + expect(invalidPageInspect.json).toMatchObject({ + code: "VALIDATION_ERROR", + fieldErrors: [ + { + code: "too_small", + path: "targetId", + }, + ], + }); +}); // On Windows child.kill("SIGINT") terminates the process without a console signal. it.skipIf(process.platform === "win32")( @@ -238,13 +229,41 @@ it.skipIf(process.platform === "win32")( } } }, - INTEGRATION_TEST_TIMEOUT_MS, ); -it( - "returns the static JavaScript application Evidence contract", - async () => { - const root = await createTestTempDirectory("rea-electron-static-cli-"); +it("returns the static JavaScript application Evidence contract", async () => { + const root = await createTestTempDirectory("rea-electron-static-cli-"); + temporary.push(root); + await writeElectronBoundaryFixture(root); + const environment = { + ...process.env, + }; + + const analyzed = await runCli( + ["analyze-javascript-application", root, "--json"], + environment, + ); + + expect(analyzed).toMatchObject({ + operation: "analyze_javascript_application", + provider: { id: "rea-javascript-application" }, + normalized_result: { + input_path: root, + summary: { + browser_windows: 3, + context_bridge_apis: 2, + ipc: { + paired_renderer_transmissions: 0, + ambiguous_renderer_transmissions: 7, + unpaired_literal_renderer_transmissions: 0, + }, + }, + }, + }); +}); +describe("Electron CLI artifact behavior", () => { + it("keeps artifact format separate from structured output format", async () => { + const root = await createTestTempDirectory("rea-electron-format-cli-"); temporary.push(root); await writeElectronBoundaryFixture(root); const environment = { @@ -252,18 +271,40 @@ it( }; const analyzed = await runCli( - ["analyze-javascript-application", root, "--json"], + [ + "--format", + "json", + "analyze-javascript-application", + root, + "--artifact-format", + "directory", + ], environment, ); + expect(analyzed).toMatchObject({ + operation: "analyze_javascript_application", + normalized_result: { input_path: root, format: "directory" }, + }); + }); + + it("routes generic directory analysis to static JavaScript", async () => { + const root = await createTestTempDirectory("rea-electron-routed-cli-"); + temporary.push(root); + await writeElectronBoundaryFixture(root); + const environment = { + ...process.env, + }; + + const analyzed = await runCli(["analyze", root, "--json"], environment); + expect(analyzed).toMatchObject({ operation: "analyze_javascript_application", provider: { id: "rea-javascript-application" }, normalized_result: { input_path: root, + format: "directory", summary: { - browser_windows: 3, - context_bridge_apis: 2, ipc: { paired_renderer_transmissions: 0, ambiguous_renderer_transmissions: 7, @@ -272,241 +313,166 @@ it( }, }, }); - }, - INTEGRATION_TEST_TIMEOUT_MS, -); -describe("Electron CLI artifact behavior", () => { - it( - "keeps artifact format separate from structured output format", - async () => { - const root = await createTestTempDirectory("rea-electron-format-cli-"); - temporary.push(root); - await writeElectronBoundaryFixture(root); - const environment = { - ...process.env, - }; - - const analyzed = await runCli( - [ - "--format", - "json", - "analyze-javascript-application", - root, - "--artifact-format", - "directory", - ], - environment, - ); - - expect(analyzed).toMatchObject({ - operation: "analyze_javascript_application", - normalized_result: { input_path: root, format: "directory" }, - }); - }, - INTEGRATION_TEST_TIMEOUT_MS, - ); - - it( - "routes generic directory analysis to static JavaScript", - async () => { - const root = await createTestTempDirectory("rea-electron-routed-cli-"); - temporary.push(root); - await writeElectronBoundaryFixture(root); - const environment = { - ...process.env, - }; - - const analyzed = await runCli(["analyze", root, "--json"], environment); - - expect(analyzed).toMatchObject({ - operation: "analyze_javascript_application", - provider: { id: "rea-javascript-application" }, - normalized_result: { - input_path: root, - format: "directory", - summary: { - ipc: { - paired_renderer_transmissions: 0, - ambiguous_renderer_transmissions: 7, - unpaired_literal_renderer_transmissions: 0, - }, - }, - }, - }); - }, - INTEGRATION_TEST_TIMEOUT_MS, - ); + }); }); describe("Electron CLI runtime reconciliation input", () => { - it( - "labels schema-invalid reconciliation input without naming Electron", - async () => { - const rejected = await runCli( - ["reconcile-javascript-runtime", "{}", "--json"], - process.env, - 1, - ); - expect(rejected).toMatchObject({ - error: "Application workflow failed", - code: "invalid_request", - category: "invalid_input", - details: { operation: "reconcile_javascript_runtime" }, - }); - }, - INTEGRATION_TEST_TIMEOUT_MS, - ); + it("labels schema-invalid reconciliation input without naming Electron", async () => { + const rejected = await runCli( + ["reconcile-javascript-runtime", "{}", "--json"], + process.env, + 1, + ); + expect(rejected).toMatchObject({ + error: "Application workflow failed", + code: "invalid_request", + category: "invalid_input", + details: { operation: "reconcile_javascript_runtime" }, + }); + }); }); describe("Electron CLI runtime reconciliation", () => { - it( - "reads a file and returns the derived static/runtime reconciliation contract", - async () => { - const root = await createTestTempDirectory("rea-runtime-cli-"); - const inputPath = join(root, "reconciliation.json"); - temporary.push(root); - await writeFile( - inputPath, - JSON.stringify(JAVASCRIPT_RUNTIME_RECONCILIATION_EXAMPLE), - ); - const reconciled = await runCli( - ["reconcile-javascript-runtime", inputPath, "--json"], - process.env, - ); + it("reads a file and returns the derived static/runtime reconciliation contract", async () => { + const root = await createTestTempDirectory("rea-runtime-cli-"); + const inputPath = join(root, "reconciliation.json"); + temporary.push(root); + await writeFile( + inputPath, + JSON.stringify(JAVASCRIPT_RUNTIME_RECONCILIATION_EXAMPLE), + ); + const reconciled = await runCli( + ["reconcile-javascript-runtime", inputPath, "--json"], + process.env, + ); - expect(reconciled).toMatchObject({ - operation: "reconcile_javascript_runtime", - provider: { id: "rea-javascript-runtime-reconciliation" }, - normalized_result: { - summary: { runtime_scripts: 1, matched: 1 }, - source_map_authority: { used_for_primary_matching: false }, + expect(reconciled).toMatchObject({ + operation: "reconcile_javascript_runtime", + provider: { id: "rea-javascript-runtime-reconciliation" }, + normalized_result: { + summary: { runtime_scripts: 1, matched: 1 }, + source_map_authority: { used_for_primary_matching: false }, + }, + }); + const missingPath = join(root, "missing.json"); + const missing = await runCli( + ["reconcile-javascript-runtime", missingPath, "--json"], + process.env, + 1, + ); + expect(missing).toMatchObject({ + input_path: missingPath, + input_reason: "read-failed", + }); + }); + + it("keeps ambiguous runtime candidates unknown while unrelated scoped assets remain absent", async () => { + const application = await applicationFixture(); + const assets = await createTestTempDirectory("rea-runtime-cli-assets-"); + const runtime = await createTestTempDirectory("rea-runtime-cli-live-"); + const inputPath = join(assets, "reconciliation.json"); + temporary.push(application, assets, runtime); + await Promise.all([ + writeFile(join(assets, "app.js"), SOURCE), + writeFile(join(assets, "absent.js"), "export const absent = true;\n"), + writeFile(join(runtime, "app.js"), SOURCE), + ]); + const rootMapping = [{ kind: "file-root", root: runtime }] as const; + const input = { + static_layers: [ + { + role: "application", + analysis: await analyzeFixture(application), + runtime_mappings: rootMapping, }, - }); - const missingPath = join(root, "missing.json"); - const missing = await runCli( - ["reconcile-javascript-runtime", missingPath, "--json"], - process.env, - 1, - ); - expect(missing).toMatchObject({ - input_path: missingPath, - input_reason: "read-failed", - }); - }, - INTEGRATION_TEST_TIMEOUT_MS, - ); + { + role: "assets", + analysis: await analyzeFixture(assets), + runtime_mappings: rootMapping, + }, + ], + runtime_observations: [ + electronRuntimeEvidence(runtime, SOURCE, { includeWorker: false }), + ], + }; + await writeFile(inputPath, JSON.stringify(input)); + const reconciled = await runCli( + ["reconcile-javascript-runtime", inputPath, "--json"], + process.env, + ); - it( - "keeps ambiguous runtime candidates unknown while unrelated scoped assets remain absent", - async () => { - const application = await applicationFixture(); - const assets = await createTestTempDirectory("rea-runtime-cli-assets-"); - const runtime = await createTestTempDirectory("rea-runtime-cli-live-"); - const inputPath = join(assets, "reconciliation.json"); - temporary.push(application, assets, runtime); - await Promise.all([ - writeFile(join(assets, "app.js"), SOURCE), - writeFile(join(assets, "absent.js"), "export const absent = true;\n"), - writeFile(join(runtime, "app.js"), SOURCE), - ]); - const rootMapping = [{ kind: "file-root", root: runtime }] as const; - const input = { - static_layers: [ - { - role: "application", - analysis: await analyzeFixture(application), - runtime_mappings: rootMapping, - }, - { - role: "assets", - analysis: await analyzeFixture(assets), - runtime_mappings: rootMapping, - }, - ], - runtime_observations: [ - electronRuntimeEvidence(runtime, SOURCE, { includeWorker: false }), - ], - }; - await writeFile(inputPath, JSON.stringify(input)); - const reconciled = await runCli( - ["reconcile-javascript-runtime", inputPath, "--json"], - process.env, - ); - - const normalized = - isRecord(reconciled) && isRecord(reconciled.normalized_result) - ? reconciled.normalized_result - : undefined; - const reconciliations = normalized?.reconciliations; - const loadStates = normalized?.static_load_states; - if (!Array.isArray(reconciliations) || !Array.isArray(loadStates)) - throw new Error("Missing runtime reconciliation output"); - const ambiguous = reconciliations.find( + const normalized = + isRecord(reconciled) && isRecord(reconciled.normalized_result) + ? reconciled.normalized_result + : undefined; + const reconciliations = normalized?.reconciliations; + const loadStates = normalized?.static_load_states; + if (!Array.isArray(reconciliations) || !Array.isArray(loadStates)) + throw new Error("Missing runtime reconciliation output"); + const ambiguous = reconciliations.find( + (item) => + isRecord(item) && + item.status === "ambiguous" && + item.entity_kind === "script", + ); + if ( + !isRecord(ambiguous) || + !Array.isArray(ambiguous.candidate_static_nodes) + ) + throw new Error("Missing ambiguous runtime script"); + const candidateIds = ambiguous.candidate_static_nodes.flatMap((item) => + isRecord(item) && typeof item.static_node_id === "string" + ? [item.static_node_id] + : [], + ); + expect(candidateIds).toHaveLength(2); + const candidateStates = loadStates.filter( + (item) => + isRecord(item) && + typeof item.static_node_id === "string" && + candidateIds.includes(item.static_node_id), + ); + expect(candidateStates).toHaveLength(2); + expect( + candidateStates.every( (item) => isRecord(item) && - item.status === "ambiguous" && - item.entity_kind === "script", - ); - if ( - !isRecord(ambiguous) || - !Array.isArray(ambiguous.candidate_static_nodes) - ) - throw new Error("Missing ambiguous runtime script"); - const candidateIds = ambiguous.candidate_static_nodes.flatMap((item) => - isRecord(item) && typeof item.static_node_id === "string" - ? [item.static_node_id] - : [], - ); - expect(candidateIds).toHaveLength(2); - const candidateStates = loadStates.filter( - (item) => - isRecord(item) && - typeof item.static_node_id === "string" && - candidateIds.includes(item.static_node_id), - ); - expect(candidateStates).toHaveLength(2); - expect( - candidateStates.every( - (item) => - isRecord(item) && - item.status === "unknown" && - item.reason === "ambiguous-runtime-candidate", - ), - ).toBe(true); - expect(loadStates).toEqual( - expect.arrayContaining([ + item.status === "unknown" && + item.reason === "ambiguous-runtime-candidate", + ), + ).toBe(true); + expect(loadStates).toEqual( + expect.arrayContaining([ + expect.objectContaining({ + status: "not-observed-in-capture", + reason: "not-observed-in-bounded-capture", + }), + ]), + ); + + const singleLayerPath = join(application, "single-reconciliation.json"); + await writeFile( + singleLayerPath, + JSON.stringify({ + static_layers: [input.static_layers[0]], + runtime_observations: input.runtime_observations, + }), + ); + const uniquelyMatched = await runCli( + ["reconcile-javascript-runtime", singleLayerPath, "--json"], + process.env, + ); + expect(uniquelyMatched).toMatchObject({ + normalized_result: { + static_load_states: expect.arrayContaining([ expect.objectContaining({ - status: "not-observed-in-capture", - reason: "not-observed-in-bounded-capture", + status: "loaded", + reason: "runtime-script-correspondence", }), ]), - ); - - const singleLayerPath = join(application, "single-reconciliation.json"); - await writeFile( - singleLayerPath, - JSON.stringify({ - static_layers: [input.static_layers[0]], - runtime_observations: input.runtime_observations, - }), - ); - const uniquelyMatched = await runCli( - ["reconcile-javascript-runtime", singleLayerPath, "--json"], - process.env, - ); - expect(uniquelyMatched).toMatchObject({ - normalized_result: { - static_load_states: expect.arrayContaining([ - expect.objectContaining({ - status: "loaded", - reason: "runtime-script-correspondence", - }), - ]), - }, - }); - }, - INTEGRATION_TEST_TIMEOUT_MS, - ); + }, + }); + }); }); const isRecord = (value: unknown): value is Record => diff --git a/tests/boundary/cli/electronFuseCleanupCli.test.ts b/tests/boundary/cli/electronFuseCleanupCli.test.ts index e03a72fe48..51373bcf1a 100644 --- a/tests/boundary/cli/electronFuseCleanupCli.test.ts +++ b/tests/boundary/cli/electronFuseCleanupCli.test.ts @@ -44,7 +44,6 @@ cliTest.for([ REA_FUSE_PROBE_TRACE: tracePath, REA_FUSE_PROBE_ALLOW: join(root, "allow"), }, - timeoutMs: 30_000, }, ); const trace = (await readFile(tracePath, "utf8")) diff --git a/tests/boundary/cli/electronWrappedCallPaths.test.ts b/tests/boundary/cli/electronWrappedCallPaths.test.ts index fe131f0fb8..40909faeb3 100644 --- a/tests/boundary/cli/electronWrappedCallPaths.test.ts +++ b/tests/boundary/cli/electronWrappedCallPaths.test.ts @@ -7,11 +7,7 @@ import { z } from "zod"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; import { connectLocalToolsMcp } from "../../fixtures/localToolsMcp.js"; import { writeFixtureFiles } from "../../support/javascriptApplicationFixture.js"; -import { - cliTest, - runFileCli, - type TestCli, -} from "../../support/cli/cliFixture.js"; +import { cliTest, type TestCli } from "../../support/cli/cliFixture.js"; import { javascriptApplicationAnalysisResultSchema } from "../../../src/domain/javascript/javascriptApplicationAnalysis.js"; import { applicationFeatureTraceResultSchema } from "../../../src/domain/javascript/javascriptFeatureTraceSchemas.js"; import { analyzeJavaScriptDirectory } from "../../support/cli/javascriptApplication.js"; @@ -223,7 +219,7 @@ cliTest( direction: "outgoing", }), ); - const traced = await runFileCli(cli, { + const traced = await cli.run({ arguments: ["trace-application-feature", input, "--json"], }); expect(traced.exitCode).toBe(0); @@ -425,7 +421,7 @@ cliTest( "main.cjs": `const { ipcMain, clipboard } = require("electron"); ipcMain.handle("nested:write", (event, value) => clipboard.writeText(value));`, }); - const output = await runFileCli(cli, { + const output = await cli.run({ arguments: ["analyze-javascript-application", root, "--json"], }); expect(output.exitCode, output.stderr).toBe(0); @@ -467,7 +463,7 @@ cliTest( direction: "outgoing", }), ); - const traced = await runFileCli(cli, { + const traced = await cli.run({ arguments: ["trace-application-feature", traceInput, "--json"], }); expect(traced.exitCode).toBe(0); @@ -495,7 +491,7 @@ cliTest( "main.js": `const { ipcMain } = require("electron"); ipcMain.on("write", (event) => event.sender.copy());`, }); - const output = await runFileCli(cli, { + const output = await cli.run({ arguments: ["analyze-javascript-application", root, "--json"], }); expect(output.exitCode).toBe(0); @@ -552,7 +548,7 @@ cliTest( "main.js": `const { ipcMain } = require("electron"); ipcMain.handle("nested:write", (event, value) => value);`, }); - const output = await runFileCli(cli, { + const output = await cli.run({ arguments: ["analyze-javascript-application", root, "--json"], }); expect(output.exitCode).toBe(0); @@ -585,7 +581,7 @@ cliTest( direction: "outgoing", }), ); - const traced = await runFileCli(cli, { + const traced = await cli.run({ arguments: ["trace-application-feature", traceInput, "--json"], }); expect(traced.exitCode).toBe(0); @@ -855,7 +851,7 @@ cliTest( e.ipcMain.on("shallow:run", event => event.sender.copy()); e.ipcMain.on("deep:run", event => event.sender.copy());`, }); - const output = await runFileCli(cli, { + const output = await cli.run({ arguments: ["analyze-javascript-application", root, "--json"], }); expect(output.exitCode).toBe(0); @@ -909,7 +905,7 @@ cliTest( "main.cjs": `const e=require("electron"); e.ipcMain.on("shared:run", event => event.sender.copy());`, }); - const output = await runFileCli(cli, { + const output = await cli.run({ arguments: ["analyze-javascript-application", root, "--json"], }); expect(output.exitCode, output.stderr).toBe(0); @@ -944,7 +940,7 @@ cliTest( join(root, "main.cjs"), 'const e=require("electron"); e.ipcMain.on("shared:run", () => {}); e.ipcMain.on("shared:run", () => {});', ); - const competing = await runFileCli(cli, { + const competing = await cli.run({ arguments: ["analyze-javascript-application", root, "--json"], }); expect(competing.exitCode, competing.stderr).toBe(0); @@ -975,7 +971,7 @@ cliTest( "main.cjs": `const { ipcMain } = require("electron"); ipcMain.on("empty:key", event => event.sender.copy());`, }); - const output = await runFileCli(cli, { + const output = await cli.run({ arguments: ["analyze-javascript-application", root, "--json"], }); expect(output.exitCode).toBe(0); diff --git a/tests/boundary/cli/emptyDoctorTargetCli.test.ts b/tests/boundary/cli/emptyDoctorTargetCli.test.ts new file mode 100644 index 0000000000..4ae3de233f --- /dev/null +++ b/tests/boundary/cli/emptyDoctorTargetCli.test.ts @@ -0,0 +1,23 @@ +import { expect } from "vitest"; + +import { cliTest } from "../../support/cli/cliFixture.js"; + +// An empty --target names no app. Like every other target-taking command, +// doctor refuses it before readiness checks instead of checking the +// working directory. +cliTest( + "refuses an empty doctor --target before any readiness check", + async ({ cli }) => { + const result = await cli.run({ + arguments: ["doctor", "--target", "", "--json"], + }); + expect(result.exitCode).toBe(1); + expect(JSON.parse(result.stdout)).toMatchObject({ + code: "VALIDATION_ERROR", + fieldErrors: expect.arrayContaining([ + expect.objectContaining({ path: "target" }), + ]), + }); + expect(result.stdout).not.toContain("target_readiness"); + }, +); diff --git a/tests/boundary/cli/environmentBuildCli.test.ts b/tests/boundary/cli/environmentBuildCli.test.ts index 3d3500938b..5c3482c568 100644 --- a/tests/boundary/cli/environmentBuildCli.test.ts +++ b/tests/boundary/cli/environmentBuildCli.test.ts @@ -59,7 +59,6 @@ cliTest( "--json", ], cwd: root, - timeoutMs: 60_000, }); expect(built.exitCode, built.stderr).toBe(0); expect(built.json).toMatchObject({ @@ -111,7 +110,6 @@ cliTest( join(root, "three-step"), "--json", ], - timeoutMs: 60_000, }); expect(exported.exitCode, exported.stderr).toBe(0); @@ -178,7 +176,6 @@ cliTest( "--capture-timeout-ms=030000", "--json", ], - timeoutMs: 60_000, }); expect(planned.exitCode, planned.stderr).toBe(0); expect(planned.json).toMatchObject({ @@ -231,7 +228,6 @@ cliTest( const noDirectory = await cli.run({ arguments: ["environment-build", har, "--json"], - timeoutMs: 60_000, }); expect(noDirectory.exitCode).toBe(1); expect(noDirectory.json).toMatchObject({ @@ -265,7 +261,6 @@ cliTest.for(["--dry-run=abc", "--dry-run="] as const)( await writeFile(har, JSON.stringify(environmentManifestHar())); const result = await cli.run({ arguments: ["environment-build", har, output, dryRun, "--json"], - timeoutMs: 60_000, }); expect(result.exitCode).not.toBe(0); expect(`${result.stdout}\n${result.stderr}`).toContain("--dry-run"); @@ -284,7 +279,6 @@ cliTest.for(["--dry-run=true", "--dry-run=false"] as const)( await writeFile(har, JSON.stringify(environmentManifestHar())); const result = await cli.run({ arguments: ["environment-build", har, output, dryRun, "--json"], - timeoutMs: 60_000, }); expect(result.exitCode, result.stderr).toBe(0); expect(result.json).toMatchObject({ @@ -295,3 +289,26 @@ cliTest.for(["--dry-run=true", "--dry-run=false"] as const)( ); }, ); + +cliTest( + "reports its default build-stage logging through the routed CLI driver", + async ({ cli }) => { + const root = await createTestTempDirectory("rea-environment-build-logs-"); + const har = join(root, "grafana.har"); + await writeFile(har, JSON.stringify(environmentManifestHar())); + // Without REA_LOG_LEVEL the build logs its stages at info on stderr. + const invocation = { + arguments: ["environment-build", har, "--dry-run", "--json"], + }; + const routed = await cli.run(invocation); + const fresh = await cli.run({ ...invocation, freshProcess: true }); + expect(fresh.stderr).toContain("Building environment"); + const stages = (stderr: string) => + stderr + .split("\n") + .filter((line) => line.length > 0) + .map((line) => (JSON.parse(line) as { stage?: unknown }).stage); + expect(stages(routed.stderr)).toEqual(stages(fresh.stderr)); + expect(routed.exitCode).toBe(fresh.exitCode); + }, +); diff --git a/tests/boundary/cli/environmentCompareCli.test.ts b/tests/boundary/cli/environmentCompareCli.test.ts index 0113e4b1a0..f12dbc0532 100644 --- a/tests/boundary/cli/environmentCompareCli.test.ts +++ b/tests/boundary/cli/environmentCompareCli.test.ts @@ -709,7 +709,6 @@ describe("rea environment-compare replay and chronology", () => { files.probe, "--json", ], - timeoutMs: 30_000, }); expect(result.exitCode).toBe(0); expect(result.json).toMatchObject({ @@ -737,7 +736,6 @@ describe("rea environment-compare replay and chronology", () => { files.manifest, "--json", ], - timeoutMs: 30_000, }); expect(result.exitCode).toBe(1); expect(result.json).toMatchObject({ diff --git a/tests/boundary/cli/environmentExportCli.test.ts b/tests/boundary/cli/environmentExportCli.test.ts index bf71062df2..bde25d3117 100644 --- a/tests/boundary/cli/environmentExportCli.test.ts +++ b/tests/boundary/cli/environmentExportCli.test.ts @@ -46,7 +46,6 @@ cliTest( NODE_OPTIONS: `--import=${preload} --max-old-space-size=768`, NODE_DISABLE_COMPILE_CACHE: "1", }, - timeoutMs: 30_000, }); expect(result.exitCode).toBe(1); expect(result.json).toMatchObject({ @@ -93,7 +92,6 @@ cliTest( name, "--json", ], - timeoutMs: 30_000, }); expect(result.exitCode).toBe(1); expect(result.json).toMatchObject({ @@ -123,7 +121,6 @@ cliTest( directory, "--json", ], - timeoutMs: 30_000, }); expect(result.stderr).toBe(""); expect(result.exitCode).toBe(0); @@ -211,7 +208,6 @@ cliTest( directory, "--json", ], - timeoutMs: 30_000, }); expect(result.exitCode).toBe(1); expect(result.json).toMatchObject({ @@ -253,7 +249,6 @@ cliTest( directory, "--json", ], - timeoutMs: 30_000, }); expect(result.exitCode).toBe(1); expect(result.json).toMatchObject({ @@ -290,7 +285,6 @@ cliTest.skipIf(process.platform === "win32")( NODE_OPTIONS: `--import=${preload} --max-old-space-size=768`, NODE_DISABLE_COMPILE_CACHE: "1", }, - timeoutMs: 30_000, }); expect(result.exitCode, result.stderr).toBe(0); for (const relative of BUNDLE_FILES) { @@ -332,7 +326,6 @@ cliTest.skipIf(process.platform === "win32")( packagePath, "--json", ], - timeoutMs: 30_000, }); expect(result.exitCode, result.stderr).toBe(0); expect(result.json).toMatchObject({ @@ -387,7 +380,6 @@ cliTest.skipIf(HOST_PATH_MAX[process.platform] === undefined)( directory, "--json", ], - timeoutMs: 30_000, }); expect(result.exitCode).toBe(1); expect(result.json).toMatchObject({ diff --git a/tests/boundary/cli/environmentExportInputsCli.test.ts b/tests/boundary/cli/environmentExportInputsCli.test.ts index cf1e986ac9..c0b6ee4ceb 100644 --- a/tests/boundary/cli/environmentExportInputsCli.test.ts +++ b/tests/boundary/cli/environmentExportInputsCli.test.ts @@ -46,7 +46,6 @@ for (const role of ["manifest", "evidence"] as const) { REA_TEST_EXPORT_READ_MARKER: marker, REA_TEST_EXPORT_READ_ACTION: "append", }, - timeoutMs: 30_000, }); expect(await readFile(marker, "utf8")).toBe("read"); expect( @@ -122,7 +121,6 @@ cliTest( REA_TEST_EXPORT_READ_MARKER: marker, REA_TEST_EXPORT_READ_ACTION: "record", }, - timeoutMs: 30_000, }); expect(result.exitCode, result.stderr).toBe(1); const limits = heapRefusalSchema.parse(result.json).details @@ -178,7 +176,6 @@ cliTest( REA_TEST_EXPORT_READ_MARKER: marker, REA_TEST_EXPORT_READ_ACTION: "fail-copy", }, - timeoutMs: 30_000, }); expect(await readFile(marker, "utf8")).toBe("copy"); expect(await readFile(tarball, "utf8")).toBe("admitted package"); diff --git a/tests/boundary/cli/environmentServeCli.test.ts b/tests/boundary/cli/environmentServeCli.test.ts index b552aa333b..bf7da50111 100644 --- a/tests/boundary/cli/environmentServeCli.test.ts +++ b/tests/boundary/cli/environmentServeCli.test.ts @@ -41,7 +41,6 @@ describe("rea environment-serve refusals", () => { "0", "--json", ], - timeoutMs: 30_000, }); expect(result.exitCode).toBe(1); expect(result.json).toMatchObject({ @@ -66,7 +65,6 @@ describe("rea environment-serve refusals", () => { const files = await writeEnvironmentServeFiles(root); const result = await cli.run({ arguments: ["environment-serve", files.manifestPath, "--json"], - timeoutMs: 30_000, }); expect(result.exitCode).toBe(1); expect(result.json).toMatchObject({ @@ -97,7 +95,6 @@ describe("rea environment-serve refusals", () => { flag, "--json", ], - timeoutMs: 30_000, }); expect(result.exitCode).toBe(1); expect(result.json).toMatchObject({ @@ -121,7 +118,6 @@ describe("rea environment-serve refusals", () => { files.capturePath, "--json", ], - timeoutMs: 30_000, }); expect(result.exitCode).toBe(1); expect(result.json).toMatchObject({ @@ -195,7 +191,6 @@ cliTest( "0", "--json", ], - timeoutMs: 30000, }); expect(result.exitCode).toBe(1); expect(result.json).toMatchObject({ diff --git a/tests/boundary/cli/environmentServeRemediation.test.ts b/tests/boundary/cli/environmentServeRemediation.test.ts index 77bbf7fd84..e219f747d8 100644 --- a/tests/boundary/cli/environmentServeRemediation.test.ts +++ b/tests/boundary/cli/environmentServeRemediation.test.ts @@ -37,7 +37,6 @@ const serve = async ( String(port), "--json", ], - timeoutMs: 30_000, }); return { exitCode: result.exitCode, diff --git a/tests/boundary/cli/evidenceBundleCommands.test.ts b/tests/boundary/cli/evidenceBundleCommands.test.ts index d7a59fe659..2e3b3a504b 100644 --- a/tests/boundary/cli/evidenceBundleCommands.test.ts +++ b/tests/boundary/cli/evidenceBundleCommands.test.ts @@ -22,7 +22,7 @@ import { updateResidualUnknown, } from "../../../src/domain/residualUnknown.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; -import { cliTest, runFileCli } from "../../support/cli/cliFixture.js"; +import { cliTest } from "../../support/cli/cliFixture.js"; const revisionBundle = () => { const records = ["open", "investigate"].map((operation) => @@ -78,7 +78,7 @@ cliTest( [["compare", "bundle.json", ""], "right"], [["evidence-export", "missing.json", ""], "output"], ] as const) { - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: [...args, "--json"], cwd: root, }); @@ -138,7 +138,7 @@ cliTest( ["evidence-export", { source: " bundle.json", output: " exported.json" }], ["compare", { left: " bundle.json", right: " other.json" }], ] as const) { - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: [command, "--schema", "--json"], }); expect(result.exitCode, result.stderr).toBe(0); @@ -246,7 +246,7 @@ cliTest.for([ : operation === "compare-left" ? ["compare", path, canonical] : ["compare", canonical, path]; - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: [...arguments_, "--json"], environment: { REA_LOG_LEVEL: "silent" }, }); @@ -325,7 +325,7 @@ cliTest( await writeFile(source, JSON.stringify(bundle)); for (const full of [false, true]) { const output = join(root, `${label}-${String(full)}.json`); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: [ "evidence-export", source, @@ -387,7 +387,7 @@ cliTest( { ...bundle, unknowns: bundle.unknowns.slice(1) }, ]) { await writeFile(path, JSON.stringify(invalid)); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: ["evidence-import", path, "--json"], }); expect(result.exitCode).toBe(1); @@ -414,7 +414,7 @@ cliTest( ); await writeFile(path, JSON.stringify(createEvidenceBundle([record]))); - const output = await runFileCli(cli, { + const output = await cli.run({ arguments: [ "inspect-evidence-bundle", JSON.stringify({ path, detail: "complete" }), @@ -466,12 +466,12 @@ cliTest( "--filter-output", "records.0.normalized_result", ]; - const json = await runFileCli(cli, { arguments: [...base, "--json"] }); + const json = await cli.run({ arguments: [...base, "--json"] }); expect(json.exitCode, json.stderr).toBe(0); expect(json.stdout).toContain(JSON.stringify(result.text)); expect(json.stdout).toContain(JSON.stringify(result.type)); expect(json.stdout).toContain(JSON.stringify(result.literal)); - const markdown = await runFileCli(cli, { + const markdown = await cli.run({ arguments: [...base, "--format", "md"], }); expect(markdown.exitCode, markdown.stderr).toBe(0); @@ -497,7 +497,7 @@ cliTest( records: [record], }; await writeFile(path, JSON.stringify(bundle)); - const imported = await runFileCli(cli, { + const imported = await cli.run({ arguments: ["evidence-import", path, "--json"], }); expect(imported.exitCode).toBe(1); diff --git a/tests/boundary/cli/flutterCli.test.ts b/tests/boundary/cli/flutterCli.test.ts index bb9b0e99cd..034cddc705 100644 --- a/tests/boundary/cli/flutterCli.test.ts +++ b/tests/boundary/cli/flutterCli.test.ts @@ -1,13 +1,8 @@ -import { rm } from "node:fs/promises"; -import { Cli } from "incur"; -import { expect, it, onTestFinished } from "vitest"; +import { expect, it } from "vitest"; import { Ajv } from "ajv"; import type { AnySchema } from "ajv"; -import { registerFlutterCommands } from "../../../src/cli/flutterCommands.js"; import { aotFixtureImage } from "../../../src/flutter/FlutterBuild.fixture.js"; -import { FlutterBuildProvider } from "../../../src/flutter/FlutterBuildProvider.js"; -import { silentLogger } from "../../../src/logger.js"; import { flutterInputSchemas } from "../../../src/domain/flutter/flutterBuildAnalysis.js"; import { createFlutterApk } from "../../fixtures/flutterApk.js"; import { serveCli } from "./cliServeHarness.js"; @@ -137,91 +132,3 @@ it("inspects the caller-selected nonstandard ABI through the public CLI", async ]); expect(JSON.parse(invalid.stdout).code).toBe("VALIDATION_ERROR"); }); - -/** Serve one command whose first snapshot-root removal fails and is retried on close. */ -const serveWithCleanupRetry = async ( - command: string, - apk: string, - interrupt: boolean, - retryFails = false, -): Promise> => { - const inherited = new Set(process.listeners("SIGINT")); - const roots: string[] = []; - const provider = new FlutterBuildProvider({ - removeRoot: async (root) => { - roots.push(root); - if (roots.length > 2) return rm(root, { recursive: true, force: true }); - // Deliver SIGINT to the command's own listener during the retry. - if (roots.length === 2 && interrupt) - for (const listener of process.listeners("SIGINT")) - if (!inherited.has(listener)) listener("SIGINT"); - if (roots.length === 1 || retryFails) - throw Object.assign(new Error("transient snapshot removal refusal"), { - code: "EACCES", - }); - await rm(root, { recursive: true, force: true }); - }, - }); - onTestFinished(() => provider.close()); - const cli = Cli.create("flutter-cleanup-retry-test", { sync: false }); - registerFlutterCommands(cli, silentLogger, provider); - let stdout = ""; - await cli.serve([command, apk, "--format", "json"], { - stdout: (text) => { - stdout += text; - }, - exit: () => {}, - }); - expect(roots).toEqual([roots[0], roots[0]]); - return JSON.parse(stdout) as Record; -}; - -it.each([ - ["identify-flutter-build", "identify_flutter_build"], - ["inspect-dart-aot", "inspect_dart_aot"], -] as const)( - "%s reports SIGINT during the cleanup retry as cancellation with completed facts", - async (command, operation) => { - const apk = await createFlutterApk("cleanup-retry.apk", [ - { path: "lib/arm64-v8a/libapp.so", bytes: aotFixtureImage() }, - ]); - const exitCode = process.exitCode; - onTestFinished(() => { - process.exitCode = exitCode; - }); - const control = await serveWithCleanupRetry(command, apk, false); - expect(control).toMatchObject({ - operation, - confidence: "observed", - caller_input_path: apk, - }); - expect(process.exitCode).toBe(exitCode); - const interrupted = await serveWithCleanupRetry(command, apk, true); - expect(process.exitCode).toBe(130); - expect(interrupted).toMatchObject({ - code: "cancelled", - details: { - operation, - partial_observation: { - operation, - caller_input_path: apk, - subject: { local_path: apk }, - confidence: "observed", - normalized_result: control.normalized_result, - }, - }, - }); - const incomplete = await serveWithCleanupRetry(command, apk, true, true); - expect(incomplete).toMatchObject({ - code: "cleanup_incomplete", - details: { - resources: [expect.stringContaining("rea-flutter-")], - partial_observation: { - operation, - caller_input_path: apk, - normalized_result: control.normalized_result, - }, - }, - }); - }, -); diff --git a/tests/boundary/cli/foreignUniversalMachoCli.test.ts b/tests/boundary/cli/foreignUniversalMachoCli.test.ts index 9139b0ef41..116f4aa7d5 100644 --- a/tests/boundary/cli/foreignUniversalMachoCli.test.ts +++ b/tests/boundary/cli/foreignUniversalMachoCli.test.ts @@ -23,24 +23,22 @@ cliTest.skipIf(process.platform !== "darwin")( const directory = await createTestTempDirectory("rea-foreign-fat-cli-"); const thin = join(directory, "thin"); const universal = join(directory, "universal"); - const compiled = await processes.run( - "/usr/bin/xcrun", - [ - "clang", - "-arch", - foreign.name, - resolve("tests/conformance/c/fixture.c"), - "-o", - thin, - ], - { timeoutMs: 30_000 }, - ); + const compiled = await processes.run("/usr/bin/xcrun", [ + "clang", + "-arch", + foreign.name, + resolve("tests/conformance/c/fixture.c"), + "-o", + thin, + ]); expect(compiled.exitCode, compiled.stderr).toBe(0); - const created = await processes.run( - "/usr/bin/xcrun", - ["lipo", "-create", thin, "-output", universal], - { timeoutMs: 30_000 }, - ); + const created = await processes.run("/usr/bin/xcrun", [ + "lipo", + "-create", + thin, + "-output", + universal, + ]); expect(created.exitCode, created.stderr).toBe(0); const result = await cli.run({ @@ -65,7 +63,6 @@ cliTest.skipIf(process.platform !== "darwin")( evidence.normalized_result.architectures.items[0]?.file_offset, ).toBeGreaterThan(0); }, - 60_000, ); cliTest.skipIf(process.platform === "darwin")( diff --git a/tests/boundary/cli/ghidraInstallationProperties.test.ts b/tests/boundary/cli/ghidraInstallationProperties.test.ts index 968c52b67e..0edba30c03 100644 --- a/tests/boundary/cli/ghidraInstallationProperties.test.ts +++ b/tests/boundary/cli/ghidraInstallationProperties.test.ts @@ -3,10 +3,11 @@ import { promisify } from "node:util"; import { mkdir, truncate, writeFile } from "node:fs/promises"; import { join } from "node:path"; import { expect } from "vitest"; -import { MAX_GHIDRA_INSTALLATION_PROPERTIES_BYTES } from "../../../src/ghidra/GhidraInstallationProperties.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; import { cliTest } from "../../support/cli/cliFixture.js"; +const MAX_GHIDRA_INSTALLATION_PROPERTIES_BYTES = 1024 * 1024; + cliTest.for(["pipe", "capacity"] as const)( "doctor reports selected properties %s without launching Java", async (kind, { cli, skip }) => { @@ -30,7 +31,6 @@ cliTest.for(["pipe", "capacity"] as const)( JAVA_HOME: join(root, "missing-jdk"), REA_LOG_LEVEL: "silent", }, - timeoutMs: 5000, }); expect(result.exitCode, `${result.stdout}\n${result.stderr}`).toBe(1); expect(result.signal).toBeNull(); diff --git a/tests/boundary/cli/ghidraJavaDoctor.test.ts b/tests/boundary/cli/ghidraJavaDoctor.test.ts index 1377170ea3..07e1dc5e9a 100644 --- a/tests/boundary/cli/ghidraJavaDoctor.test.ts +++ b/tests/boundary/cli/ghidraJavaDoctor.test.ts @@ -156,7 +156,6 @@ setTimeout(() => process.exit(92), 12000); GHIDRA_INSTALL_DIR: install, REA_LOG_LEVEL: "silent", }, - timeoutMs: 15000, }); // Capture the actual group while the selected process is alive; the PID // alone is never used as cleanup authority. Release is a private protocol. diff --git a/tests/boundary/cli/githubActionsExample.test.ts b/tests/boundary/cli/githubActionsExample.test.ts index a8e7df0a38..03bf5e4cd7 100644 --- a/tests/boundary/cli/githubActionsExample.test.ts +++ b/tests/boundary/cli/githubActionsExample.test.ts @@ -147,7 +147,6 @@ describe.skipIf(process.platform === "win32")( { cwd: workspace, env, - timeoutMs: 30_000, }, ); const checkoutReports = join(workspace, "reports", "rea"); diff --git a/tests/boundary/cli/harBrowserSerializedUrlsCli.test.ts b/tests/boundary/cli/harBrowserSerializedUrlsCli.test.ts index b50fb8e87b..d559d513ab 100644 --- a/tests/boundary/cli/harBrowserSerializedUrlsCli.test.ts +++ b/tests/boundary/cli/harBrowserSerializedUrlsCli.test.ts @@ -79,7 +79,6 @@ cliTest( }); const built = await cli.run({ arguments: ["environment-build", har, "--dry-run", "--json"], - timeoutMs: 60_000, }); expect(built.exitCode, built.stdout + built.stderr).toBe(0); expect(built.json).toMatchObject({ diff --git a/tests/boundary/cli/hopperLaunchFailureCli.test.ts b/tests/boundary/cli/hopperLaunchFailureCli.test.ts index 29e132b8a0..133d3be61d 100644 --- a/tests/boundary/cli/hopperLaunchFailureCli.test.ts +++ b/tests/boundary/cli/hopperLaunchFailureCli.test.ts @@ -29,7 +29,6 @@ describe("Hopper launcher failure through the one-shot CLI", () => { // Hopper target leases are acquired only for the macOS launcher route. workspaceCliTest.skipIf(process.platform !== "darwin")( "exits after a retained-cleanup failure and releases the target to a retry", - { timeout: 90_000 }, async ({ cli, workspace }) => { const launcherPath = await workspace.write( "failure-helper", @@ -55,7 +54,6 @@ describe("Hopper launcher failure through the one-shot CLI", () => { HOPPER_LOADER_ARGS_JSON: JSON.stringify(loaderArgs), REA_LOG_LEVEL: "silent", }, - timeoutMs: 45_000, }); const retained = RetainedRuntime.safeParse(result.json); if (retained.success) @@ -66,7 +64,10 @@ describe("Hopper launcher failure through the one-shot CLI", () => { ); expect(result.exitCode, attempt).toBe(1); - expect(result.json, attempt).toMatchObject({ + expect( + result.json, + `${attempt}: ${JSON.stringify(result.json)}`, + ).toMatchObject({ code: "cleanup_incomplete", details: { cleanup: "incomplete", diff --git a/tests/boundary/cli/httpCaptureComparison.test.ts b/tests/boundary/cli/httpCaptureComparison.test.ts index 86be2d58bf..7d4f08efa8 100644 --- a/tests/boundary/cli/httpCaptureComparison.test.ts +++ b/tests/boundary/cli/httpCaptureComparison.test.ts @@ -116,5 +116,4 @@ cliTest( await fixture.browser.close(); } }, - 60_000, ); diff --git a/tests/boundary/cli/idaDoctorRegistration.test.ts b/tests/boundary/cli/idaDoctorRegistration.test.ts index 46c005e2a6..856449d2a4 100644 --- a/tests/boundary/cli/idaDoctorRegistration.test.ts +++ b/tests/boundary/cli/idaDoctorRegistration.test.ts @@ -15,7 +15,6 @@ cliTest.skipIf(process.platform === "win32")( const result = await cli.run({ arguments: ["doctor", "--provider", "ida", "--json"], environment: { HOME: root, REA_IDA_MCP_CONFIG: registration }, - timeoutMs: 10_000, }); expect(result.exitCode, result.stderr).toBe(1); expect(result.json).toMatchObject({ @@ -64,7 +63,6 @@ cliTest.skipIf(process.platform !== "darwin").for([true, false])( const result = await cli.run({ arguments: ["doctor", "--provider", "ida", "--json"], environment: { HOME: root, REA_IDA_MCP_CONFIG: registration }, - timeoutMs: 10_000, }); expect(result.exitCode, result.stderr).toBe(usable ? 0 : 1); expect(result.json).toMatchObject({ @@ -144,7 +142,6 @@ cliTest.for([ const result = await cli.run({ arguments: ["doctor", "--provider", "ida", "--json"], environment: { HOME: root, REA_IDA_MCP_CONFIG: path }, - timeoutMs: 10_000, }); expect(result.exitCode, result.stderr).toBe( constraint === undefined ? 0 : 1, @@ -171,7 +168,6 @@ cliTest.for([ const providers = await cli.run({ arguments: ["providers", "--json"], environment: { HOME: root, REA_IDA_MCP_CONFIG: path }, - timeoutMs: 10_000, }); expect(providers.exitCode, providers.stderr).toBe(0); expect(providers.json).toMatchObject({ diff --git a/tests/boundary/cli/idaRetainedCleanupCli.test.ts b/tests/boundary/cli/idaRetainedCleanupCli.test.ts index e21054020f..eb439e007a 100644 --- a/tests/boundary/cli/idaRetainedCleanupCli.test.ts +++ b/tests/boundary/cli/idaRetainedCleanupCli.test.ts @@ -58,7 +58,6 @@ describe("headless IDA timeout through the one-shot CLI", () => { REA_IDA_MCP_CONFIG: registration, REA_LOG_LEVEL: "silent", }, - timeoutMs: 30_000, }); expect(result.exitCode).toBe(1); diff --git a/tests/boundary/cli/idaStartupAttributionCli.test.ts b/tests/boundary/cli/idaStartupAttributionCli.test.ts index 68a233dcb4..07395ed961 100644 --- a/tests/boundary/cli/idaStartupAttributionCli.test.ts +++ b/tests/boundary/cli/idaStartupAttributionCli.test.ts @@ -227,7 +227,6 @@ workspaceCliTest.for([ REA_IDA_MCP_CONFIG: registration, REA_LOG_LEVEL: "silent", }, - timeoutMs: 15000, }); expect(result.stderr).toBe(""); expect(supervisor.database).toBeUndefined(); diff --git a/tests/boundary/cli/il2cppCallerPath.test.ts b/tests/boundary/cli/il2cppCallerPath.test.ts index 1b3884dc39..2e8f9be846 100644 --- a/tests/boundary/cli/il2cppCallerPath.test.ts +++ b/tests/boundary/cli/il2cppCallerPath.test.ts @@ -6,7 +6,7 @@ import { readEvidenceBundle } from "../../../src/application/EvidenceBundleFiles import { createLinkedIl2cppMetadataFixture } from "../../fixtures/il2cpp/metadata.js"; import { createIl2cppDyldBindFixture } from "../../fixtures/il2cpp/binary.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; -import { cliTest, runFileCli } from "../../support/cli/cliFixture.js"; +import { cliTest } from "../../support/cli/cliFixture.js"; cliTest( "preserves both selected IL2CPP input spellings in inline and bundled Evidence", @@ -22,7 +22,7 @@ cliTest( ); const input = "./metadata.dat"; const binary = "./GameAssembly.dylib"; - const inline = await runFileCli(cli, { + const inline = await cli.run({ cwd: root, arguments: [ "inspect-il2cpp-metadata", @@ -47,7 +47,7 @@ cliTest( "./evidence.json", "--json", ]; - const summary = await runFileCli(cli, { + const summary = await cli.run({ cwd: root, arguments: summaryArgs, }); @@ -56,7 +56,7 @@ cliTest( parseEvidence((summary.json as { summary: unknown }).summary) .caller_input_paths, ).toEqual({ binary_path: binary }); - const refused = await runFileCli(cli, { + const refused = await cli.run({ cwd: root, arguments: summaryArgs, }); @@ -71,7 +71,7 @@ cliTest( expect(saved.value.records[0]?.caller_input_paths).toEqual({ binary_path: binary, }); - const without = await runFileCli(cli, { + const without = await cli.run({ cwd: root, arguments: ["inspect-il2cpp-metadata", input, "--json"], }); diff --git a/tests/boundary/cli/javascriptAssignmentInfluence.test.ts b/tests/boundary/cli/javascriptAssignmentInfluence.test.ts index 3266dfe8e0..2cb6ddd006 100644 --- a/tests/boundary/cli/javascriptAssignmentInfluence.test.ts +++ b/tests/boundary/cli/javascriptAssignmentInfluence.test.ts @@ -4,7 +4,7 @@ import { expect } from "vitest"; import { z } from "zod"; import { javaScriptSemanticTraceResultSchema } from "../../../src/domain/javascript/javascriptSemanticTraceSchemas.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; -import { cliTest, runFileCli } from "../../support/cli/cliFixture.js"; +import { cliTest } from "../../support/cli/cliFixture.js"; cliTest( "traces reassignment inputs through the public analysis and trace commands", @@ -51,9 +51,10 @@ cliTest( const trace = { arguments: ["trace-javascript-semantics", queryFile, "--json"], }; - const traced = await (name === "declaration" - ? cli.run(trace) - : runFileCli(cli, trace)); + const traced = await cli.run({ + ...trace, + freshProcess: name === "declaration", + }); expect(traced.exitCode, `${name} traced: ${traced.stderr}`).toBe(0); const { normalized_result: result } = z .object({ normalized_result: javaScriptSemanticTraceResultSchema }) @@ -74,7 +75,7 @@ cliTest( query: { ...query.query, include_ambiguous_dynamic_edges: true }, }), ); - const expanded = await runFileCli(cli, { + const expanded = await cli.run({ arguments: ["trace-javascript-semantics", queryFile, "--json"], }); expect(expanded.exitCode, `${name} expanded: ${expanded.stderr}`).toBe(0); @@ -86,5 +87,4 @@ cliTest( ); } }, - 120_000, ); diff --git a/tests/boundary/cli/javascriptRuntimeWideLoadStates.test.ts b/tests/boundary/cli/javascriptRuntimeWideLoadStates.test.ts new file mode 100644 index 0000000000..0c566a4aaa --- /dev/null +++ b/tests/boundary/cli/javascriptRuntimeWideLoadStates.test.ts @@ -0,0 +1,147 @@ +import { createHash } from "node:crypto"; +import { readFile, writeFile } from "node:fs/promises"; +import { join } from "node:path"; +import { fileURLToPath } from "node:url"; +import { expect } from "vitest"; + +import { createEvidence, parseEvidence } from "../../../src/domain/evidence.js"; +import { + createJavaScriptApplicationGraph, + createJavaScriptApplicationNode, +} from "../../../src/domain/javascript/javascriptApplicationGraph.js"; +import { javascriptApplicationAnalysisResultSchema } from "../../../src/domain/javascript/javascriptApplicationAnalysis.js"; +import { createJavaScriptSemanticGraph } from "../../../src/domain/javascript/javascriptSemanticGraph.js"; +import { javascriptRuntimeReconciliationResultSchema } from "../../../src/domain/javascript/javascriptRuntimeReconciliationSchemas.js"; +import { nodeReconciliationFixture } from "../../fixtures/nodeRuntimeReconciliation.js"; +import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; +import { processTest } from "../../support/process/processFixture.js"; + +/** Construct authenticated synthetic observations, without a live Inspector. */ +const wideRequest = (count: number) => { + const input = nodeReconciliationFixture(false); + const original = input.static_layers[0]!.analysis; + const result = javascriptApplicationAnalysisResultSchema.parse( + original.normalized_result, + ); + const observation = result.graph.nodes.find( + ({ kind }) => kind === "javascript-asset", + )!.observations[0]!; + const nodes = [...result.graph.nodes]; + for (let index = 0; index < count; index++) { + const path = `part-${String(index)}.js`; + const source = `export const value=${String(index)};\n`; + const sha256 = createHash("sha256").update(source).digest("hex"); + nodes.push( + createJavaScriptApplicationNode({ + kind: "javascript-asset", + identity: { + strategy: "content-digest", + stability: "global-exact", + sha256, + }, + observations: [ + { + label: path, + properties: { path, bytes: source.length }, + evidence: { + ...observation.evidence, + artifact: { + available: true, + artifact_id: `art_${sha256}`, + sha256, + }, + location: { + available: true, + value: { kind: "artifact-path", path }, + }, + }, + }, + ], + }), + ); + } + const { graph_id: _oldGraphId, ...graphContent } = result.graph; + const graph = createJavaScriptApplicationGraph({ ...graphContent, nodes }); + const { graph_id: _oldSemanticId, ...semanticContent } = + result.semantic_graph; + const semanticGraph = createJavaScriptSemanticGraph({ + ...semanticContent, + application_graph_id: graph.graph_id, + }); + const analysis = createEvidence( + { + path: result.input_path, + sha256: result.root_artifact_sha256, + format: result.format, + }, + original.provider, + { + predicateType: original.predicate_type, + operation: original.operation, + parameters: original.parameters, + result: { ...result, graph, semantic_graph: semanticGraph }, + confidence: original.confidence, + authority: original.authority, + }, + ); + return { + input: { ...input, static_layers: [{ analysis }] }, + assetIds: graph.nodes + .filter(({ kind }) => kind === "javascript-asset") + .map(({ node_id }) => node_id), + analysisId: analysis.evidence_id, + }; +}; + +processTest.for([1_000, 16_000])( + "retains every static load state for %s added assets through the compiled CLI", + async (count, { processes }) => { + const root = await createTestTempDirectory("rea-runtime-wide-states-"); + const path = join(root, "request.json"); + const { input, assetIds, analysisId } = wideRequest(count); + const text = JSON.stringify(input); + await writeFile(path, text); + const response = await processes.run( + process.execPath, + [ + // Exercise the same argument-stack bound with a smaller portable fixture. + // The normal-stack 130,001-asset reproducer is recorded in #10352. + "--stack_size=128", + fileURLToPath(new URL("../../../scripts/rea.mjs", import.meta.url)), + "reconcile-javascript-runtime", + path, + "--json", + ], + { cwd: root }, + ); + expect( + response.exitCode, + response.stderr + response.stdout.slice(0, 2000), + ).toBe(0); + const evidence = parseEvidence(JSON.parse(response.stdout)); + expect(evidence.operation).toBe("reconcile_javascript_runtime"); + const result = javascriptRuntimeReconciliationResultSchema.parse( + evidence.normalized_result, + ); + expect(result.static_layers[0]?.evidence_id).toBe(analysisId); + expect(result.static_load_states).toHaveLength(count + 1); + const expected = new Set(assetIds); + for (const state of result.static_load_states) { + expect(expected.delete(state.static_node_id)).toBe(true); + expect(state).toMatchObject({ + kind: "javascript-asset", + status: "unknown", + runtime_node_ids: [], + reason: "static-or-runtime-coverage-incomplete", + }); + } + expect(expected.size).toBe(0); + expect(result.summary).toMatchObject({ + runtime_scripts: 0, + static_loaded: 0, + static_not_observed: 0, + static_unknown: count + 1, + }); + expect(await readFile(path, "utf8")).toBe(text); + }, +); diff --git a/tests/boundary/cli/jebCli.test.ts b/tests/boundary/cli/jebCli.test.ts index 2ec76b0fa8..ef3cf8bdf2 100644 --- a/tests/boundary/cli/jebCli.test.ts +++ b/tests/boundary/cli/jebCli.test.ts @@ -124,7 +124,6 @@ it.each( const result = await cli.run({ arguments: argv, environment: { HOME: home, REA_JEB_MCP_URL: fixture.endpoint }, - timeoutMs: 10_000, }); const output = JSON.parse(result.stdout); if (stage === "success") { diff --git a/tests/boundary/cli/jsonInputSize.test.ts b/tests/boundary/cli/jsonInputSize.test.ts index 33996b42f2..6fe0417e46 100644 --- a/tests/boundary/cli/jsonInputSize.test.ts +++ b/tests/boundary/cli/jsonInputSize.test.ts @@ -57,7 +57,7 @@ cliTest( cliTest( "traces valid Evidence beyond the whole-document string limit", - async ({ cli, processes }) => { + async ({ cli }) => { const root = await createTestTempDirectory("rea-json-streamed-evidence-"); const source = join(root, "application"); await mkdir(source); diff --git a/tests/boundary/cli/jsonOutput.test.ts b/tests/boundary/cli/jsonOutput.test.ts index 1ccdf1c24c..7a8082c00c 100644 --- a/tests/boundary/cli/jsonOutput.test.ts +++ b/tests/boundary/cli/jsonOutput.test.ts @@ -856,7 +856,7 @@ describe("CLI result output surface", () => { }, meta: { command: "inspect-managed-members" }, }); - }, 60_000); + }); }); describe("streamed CLI filtered selections", () => { diff --git a/tests/boundary/cli/keyedArchivePrototypeKeyCli.test.ts b/tests/boundary/cli/keyedArchivePrototypeKeyCli.test.ts index ba14408175..1d4121c832 100644 --- a/tests/boundary/cli/keyedArchivePrototypeKeyCli.test.ts +++ b/tests/boundary/cli/keyedArchivePrototypeKeyCli.test.ts @@ -9,7 +9,7 @@ import { expectKeyedArchivePrototypeKeys, keyedArchivePrototypeKeyFixture, } from "../../fixtures/keyedArchivePrototypeKeys.js"; -import { cliTest, runFileCli } from "../../support/cli/cliFixture.js"; +import { cliTest } from "../../support/cli/cliFixture.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; import { keyedArchiveResultSchema } from "../../../src/domain/apple/keyedArchive.js"; import { connectLocalToolsMcp } from "../../fixtures/localToolsMcp.js"; @@ -86,9 +86,10 @@ cliTest( USERPROFILE: directory, }, }; - const output = await (name !== "plain" && rootArguments.length === 0 - ? cli.run(invocation) - : runFileCli(cli, invocation)); + const output = await cli.run({ + ...invocation, + freshProcess: name !== "plain" && rootArguments.length === 0, + }); expect(output.exitCode, output.stdout).toBe(0); const result = keyedArchiveResultSchema.parse( parseEvidence(output.json).normalized_result, diff --git a/tests/boundary/cli/kunaDoctorIdentity.test.ts b/tests/boundary/cli/kunaDoctorIdentity.test.ts index 26b3b9a8bc..9de389ca59 100644 --- a/tests/boundary/cli/kunaDoctorIdentity.test.ts +++ b/tests/boundary/cli/kunaDoctorIdentity.test.ts @@ -41,7 +41,6 @@ ${mutation} REA_KUNA_CACHE_DIR: join(root, "cache"), REA_LOG_LEVEL: "silent", }, - timeoutMs: 20_000, }); const after = await readFile(executable); const afterHash = createHash("sha256").update(after).digest("hex"); diff --git a/tests/boundary/cli/managedMemberComparisonCli.test.ts b/tests/boundary/cli/managedMemberComparisonCli.test.ts index 5680390577..d79bee2a3e 100644 --- a/tests/boundary/cli/managedMemberComparisonCli.test.ts +++ b/tests/boundary/cli/managedMemberComparisonCli.test.ts @@ -134,7 +134,7 @@ cliTest( const sha256 = createHash("sha256").update(bytes).digest("hex"); await writeFile(path, bytes); await writeFile(peerPath, bytes); - const inspected = await cli.sharedCliRun!({ + const inspected = await cli.run({ arguments: ["inspect-managed-members", path, "--json"], }); expect(inspected.exitCode, inspected.stderr).toBe(0); @@ -160,7 +160,7 @@ cliTest( : {}), }); } - const compared = await cli.sharedCliRun!({ + const compared = await cli.run({ arguments: ["compare-managed-members", path, path, "--json"], }); expect(compared.exitCode, compared.stderr).toBe(0); diff --git a/tests/boundary/cli/managedMemberSummaryCli.test.ts b/tests/boundary/cli/managedMemberSummaryCli.test.ts index 97e964c357..5f74d5118d 100644 --- a/tests/boundary/cli/managedMemberSummaryCli.test.ts +++ b/tests/boundary/cli/managedMemberSummaryCli.test.ts @@ -28,11 +28,7 @@ import { } from "../../../src/domain/managed/managedArtifact.js"; import { buildManagedPeFixture } from "../../../src/dotnet/ManagedPe.fixture.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; -import { - cliTest, - runFileCli, - type TestCli, -} from "../../support/cli/cliFixture.js"; +import { cliTest, type TestCli } from "../../support/cli/cliFixture.js"; const memberFixture = (): Buffer => buildManagedPeFixture({ @@ -86,7 +82,7 @@ const verifyCliSelection = async ({ parent, report, }: CliMemberViewContext) => { - const selected = await runFileCli(cli, { + const selected = await cli.run({ cwd, arguments: [ "inspect-analysis-view", @@ -154,7 +150,7 @@ const verifyCliFullPages = async ( expect(reportedCounts[collection]).toBe(rows.length); const paged: unknown[] = []; for (let offset: number | null = 0; offset !== null;) { - const response = await runFileCli(cli, { + const response = await cli.run({ cwd, arguments: [ "inspect-analysis-view", @@ -243,6 +239,7 @@ cliTest( cliTest( "retains complete member Evidence for exact CLI pages after process exit and target removal", + { tags: ["long-running"] }, async ({ cli }) => { const root = await createTestTempDirectory("rea-member-summary-cli-"); const path = join(root, "fixture.dll"); @@ -359,7 +356,6 @@ cliTest( await verifyCliSelection(pageContext); expect(await readFile(bundlePath, "utf8")).toBe(originalBytes); }, - 180_000, ); cliTest( diff --git a/tests/boundary/cli/managedNativeVerificationLargeInventory.test.ts b/tests/boundary/cli/managedNativeVerificationLargeInventory.test.ts new file mode 100644 index 0000000000..5f499922b5 --- /dev/null +++ b/tests/boundary/cli/managedNativeVerificationLargeInventory.test.ts @@ -0,0 +1,110 @@ +import { writeFile } from "node:fs/promises"; +import { join } from "node:path"; +import { expect } from "vitest"; + +import { MANAGED_NATIVE_VERIFICATION_EXAMPLE } from "../../../src/contracts/managed/managedWorkflowExamples.js"; +import { + createEvidence, + parseEvidence, + type Evidence, +} from "../../../src/domain/evidence.js"; +import { + managedNativeVerificationResultSchema, + type ManagedNativeVerificationResult, +} from "../../../src/domain/managed/managedNativeVerificationSchemas.js"; +import { inspectMachoSchema } from "../../../src/domain/native/nativeInspection.js"; +import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; +import { cliTest } from "../../support/cli/cliFixture.js"; + +/** More exports than one call can receive as arguments on V8's default stack. */ +const EXPORTS = 130_000; + +/** Recreate the example's Mach-O Evidence with `count` exports, keeping its matching first row. */ +const machoExports = (count: number): Evidence => { + const [source] = MANAGED_NATIVE_VERIFICATION_EXAMPLE.native_observations; + if (source?.subject == null) throw new Error("Missing native example"); + const result = inspectMachoSchema.parse(source.normalized_result); + const [match] = result.exports.items; + if (match === undefined) throw new Error("Missing matching export"); + const items = [match]; + for (let index = 1; index < count; index += 1) + items.push({ + name: `_audit_${String(index)}`, + runtime_name: `audit_${String(index)}`, + address: `0x${(0x1000 + index).toString(16)}`, + weak: false, + reexport: false, + source: "nm", + }); + return createEvidence( + { + path: source.subject.local_path, + sha256: source.subject.digest.sha256, + format: source.subject.format, + }, + source.provider, + { + operation: source.operation, + parameters: source.parameters, + result: { + ...result, + exports: { ...result.exports, items, total: count }, + }, + rawResult: null, + limitations: source.limitations, + }, + ); +}; + +/** P/Invoke outcomes, apart from the native record identity they cite. */ +const outcomes = ( + result: ManagedNativeVerificationResult, + native: Evidence, +): unknown => + JSON.parse( + JSON.stringify(result.pinvoke_imports, (key, value: unknown) => + key === "item_id" ? undefined : value, + ).replaceAll(native.evidence_id, ""), + ); + +cliTest( + "verifies P/Invoke boundaries against a native inventory wider than call arguments allow", + async ({ cli }) => { + const directory = await createTestTempDirectory("rea-managed-wide-"); + const verify = async (count: number) => { + const native = machoExports(count); + const path = join(directory, `verify-${String(count)}.json`); + await writeFile( + path, + JSON.stringify({ + managed_boundaries: + MANAGED_NATIVE_VERIFICATION_EXAMPLE.managed_boundaries, + native_observations: [native], + }), + ); + const command = await cli.run({ + arguments: ["verify-managed-native-boundaries", path, "--json"], + }); + expect(command.exitCode, command.stdout + command.stderr).toBe(0); + const result = managedNativeVerificationResultSchema.parse( + parseEvidence(command.json).normalized_result, + ); + return { result, native }; + }; + + const control = await verify(3); + const wide = await verify(EXPORTS); + expect(wide.result.summary).toEqual(control.result.summary); + expect(wide.result.summary).toMatchObject({ verified: 1 }); + expect(outcomes(wide.result, wide.native)).toEqual( + outcomes(control.result, control.native), + ); + expect(wide.result.pinvoke_imports[0]).toMatchObject({ + status: "verified", + matched_native: { + evidence_id: wide.native.evidence_id, + name: "open_native", + }, + }); + }, +); diff --git a/tests/boundary/cli/markdownLiteralCells.test.ts b/tests/boundary/cli/markdownLiteralCells.test.ts index 9fb142afee..1c37a8052a 100644 --- a/tests/boundary/cli/markdownLiteralCells.test.ts +++ b/tests/boundary/cli/markdownLiteralCells.test.ts @@ -12,7 +12,7 @@ import { modelArtifactSchema } from "../../../src/domain/model/modelArtifact.js" import { renderCliDocument } from "../../../src/cliOutput.js"; import { safetensorsFixture } from "../../fixtures/model/model.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; -import { cliTest, runFileCli } from "../../support/cli/cliFixture.js"; +import { cliTest } from "../../support/cli/cliFixture.js"; afterAll(() => disposeMdItInstance()); @@ -212,7 +212,6 @@ cliTest( "md", "--full-output", ], - timeoutMs: 20_000, }); expect(markdown.exitCode, markdown.stderr).toBe(0); const renderer = await createMarkdownRenderer(root, { linkify: false }); @@ -231,7 +230,7 @@ cliTest( "preserves capability provider identities in rendered Markdown", async ({ cli }) => { const json = await cli.run({ arguments: ["capabilities", "--json"] }); - const markdown = await runFileCli(cli, { + const markdown = await cli.run({ arguments: ["capabilities", "--format", "md"], }); expect(json.exitCode, json.stderr).toBe(0); @@ -277,7 +276,7 @@ cliTest( "--filter-output", "records.0.normalized_result", ]; - const markdown = await runFileCli(cli, { + const markdown = await cli.run({ arguments: [...base, "--format", "md"], }); const json = await cli.run({ arguments: [...base, "--json"] }); @@ -343,7 +342,7 @@ cliTest( ); const base = ["inspect-model-artifact", path, "--detail", "complete"]; const json = await cli.run({ arguments: [...base, "--json"] }); - const markdown = await runFileCli(cli, { + const markdown = await cli.run({ arguments: [...base, "--format", "md"], }); expect(json.exitCode, json.stderr).toBe(0); @@ -398,7 +397,7 @@ cliTest( "--filter-output", "records.0.normalized_result", ]; - const markdown = await runFileCli(cli, { + const markdown = await cli.run({ arguments: [...base, "--format", "md"], }); const json = await cli.run({ arguments: [...base, "--json"] }); diff --git a/tests/boundary/cli/mcpAddDiscovery.test.ts b/tests/boundary/cli/mcpAddDiscovery.test.ts index 6d20faece2..4c2e31811b 100644 --- a/tests/boundary/cli/mcpAddDiscovery.test.ts +++ b/tests/boundary/cli/mcpAddDiscovery.test.ts @@ -19,7 +19,6 @@ cliTest( arguments: arguments_, cwd: home, environment, - timeoutMs: 30_000, }); const schema = await run([ "mcp", @@ -110,7 +109,6 @@ cliTest( arguments: arguments_, cwd: home, environment, - timeoutMs: 30_000, }); const filtered = await run([ "mcp", diff --git a/tests/boundary/cli/mcpAddOptionValidation.test.ts b/tests/boundary/cli/mcpAddOptionValidation.test.ts index ea9b6563cc..9fdff480f5 100644 --- a/tests/boundary/cli/mcpAddOptionValidation.test.ts +++ b/tests/boundary/cli/mcpAddOptionValidation.test.ts @@ -23,7 +23,6 @@ cliTest("mcp add rejects valued options without a value", async ({ cli }) => { arguments: [...arguments_, "--json"], cwd: root, environment: await registrationEnvironment(selectedHome), - timeoutMs: 20_000, }); for (const [flag, field] of [ @@ -106,7 +105,6 @@ cliTest( arguments: [...arguments_, "--json"], cwd: root, environment, - timeoutMs: 20_000, }); // Incur skips a stray word, so the registration used to succeed for a request @@ -178,7 +176,6 @@ cliTest( arguments: [...arguments_, "--json"], cwd: root, environment, - timeoutMs: 20_000, }); // A stray word alongside a missing value: the message describes the missing @@ -223,7 +220,6 @@ cliTest( ], cwd: project, environment, - timeoutMs: 20_000, }); expect(result.exitCode, result.stderr).toBe(1); expect(result.json).toMatchObject({ diff --git a/tests/boundary/cli/mcpDoctorHelpCli.test.ts b/tests/boundary/cli/mcpDoctorHelpCli.test.ts index 8f630f7eef..613f778ac6 100644 --- a/tests/boundary/cli/mcpDoctorHelpCli.test.ts +++ b/tests/boundary/cli/mcpDoctorHelpCli.test.ts @@ -2,8 +2,6 @@ import { describe, expect } from "vitest"; import { workspaceCliTest } from "../../support/cli/workspaceCliFixture.js"; -const CLI_INTEGRATION_TIMEOUT_MS = 60_000; - describe("MCP doctor output routing boundary", () => { workspaceCliTest( "keeps output options from selecting the CLI-derived MCP doctor", @@ -41,7 +39,6 @@ describe("MCP doctor output routing boundary", () => { expect(result.stderr).toBe(""); } }, - CLI_INTEGRATION_TIMEOUT_MS, ); }); @@ -70,7 +67,6 @@ describe("MCP doctor help boundary", () => { expect(result.stderr).toBe(""); } }, - CLI_INTEGRATION_TIMEOUT_MS, ); workspaceCliTest( @@ -88,7 +84,6 @@ describe("MCP doctor help boundary", () => { expect(result.stdout).not.toContain("healthy"); } }, - CLI_INTEGRATION_TIMEOUT_MS, ); workspaceCliTest( @@ -105,7 +100,6 @@ describe("MCP doctor help boundary", () => { expect(result.stdout).not.toContain("Usage: rea mcp doctor"); } }, - CLI_INTEGRATION_TIMEOUT_MS, ); workspaceCliTest( @@ -126,7 +120,6 @@ describe("MCP doctor help boundary", () => { 'code: VALIDATION_ERROR\nmessage: "Invalid output format: bogus"\n', }); }, - CLI_INTEGRATION_TIMEOUT_MS, ); workspaceCliTest( @@ -156,7 +149,6 @@ describe("MCP doctor help boundary", () => { }); } }, - CLI_INTEGRATION_TIMEOUT_MS, ); }); @@ -180,7 +172,6 @@ describe("MCP doctor timing boundary", () => { expect(Number.isFinite(duration)).toBe(true); expect(duration).toBeGreaterThanOrEqual(0); }, - CLI_INTEGRATION_TIMEOUT_MS, ); workspaceCliTest( @@ -193,7 +184,6 @@ describe("MCP doctor timing boundary", () => { expect(result.stdout).toContain("Invalid output format: --timings"); expect(result.stderr).toBe(""); }, - CLI_INTEGRATION_TIMEOUT_MS, ); }); @@ -222,5 +212,4 @@ workspaceCliTest( else expect(result.stderr).toBe(""); } }, - CLI_INTEGRATION_TIMEOUT_MS, ); diff --git a/tests/boundary/cli/nativeDemangleCli.test.ts b/tests/boundary/cli/nativeDemangleCli.test.ts index c18b6f5422..361f216836 100644 --- a/tests/boundary/cli/nativeDemangleCli.test.ts +++ b/tests/boundary/cli/nativeDemangleCli.test.ts @@ -58,7 +58,6 @@ describe("Swift demangling CLI with line-terminator characters", () => { "--json", ], environment, - timeoutMs: 20_000, }); expect(result.signal).toBeNull(); expect(result.exitCode).toBe(1); @@ -76,7 +75,6 @@ describe("Swift demangling CLI with line-terminator characters", () => { const result = await cli.run({ arguments: ["demangle-swift", "--json"], environment, - timeoutMs: 20_000, }); expect(result.signal).toBeNull(); expect(result.exitCode).toBe(1); diff --git a/tests/boundary/cli/nativeMachoArm64_32Cli.test.ts b/tests/boundary/cli/nativeMachoArm64_32Cli.test.ts index 4ebe08a92b..71a03e2de3 100644 --- a/tests/boundary/cli/nativeMachoArm64_32Cli.test.ts +++ b/tests/boundary/cli/nativeMachoArm64_32Cli.test.ts @@ -31,11 +31,15 @@ describe.skipIf(process.platform !== "darwin")( const source = join(directory, "watch.c"); await writeFile(source, "int rea_watch_value(void) { return 42; }\n"); const object = join(directory, "watch.o"); - const compiled = await processes.run( - "/usr/bin/xcrun", - ["clang", "-arch", "arm64_32", "-c", source, "-o", object], - { timeoutMs: 30_000 }, - ); + const compiled = await processes.run("/usr/bin/xcrun", [ + "clang", + "-arch", + "arm64_32", + "-c", + source, + "-o", + object, + ]); expect(compiled.exitCode, compiled.stderr).toBe(0); for (const command of [ "list-architectures", @@ -71,7 +75,6 @@ describe.skipIf(process.platform !== "darwin")( details: { selection_reason: "target_unsupported" }, }); }, - 60_000, ); }, ); diff --git a/tests/boundary/cli/nativeMachoDylibIdentityCli.test.ts b/tests/boundary/cli/nativeMachoDylibIdentityCli.test.ts index 091897b891..8c55433dd6 100644 --- a/tests/boundary/cli/nativeMachoDylibIdentityCli.test.ts +++ b/tests/boundary/cli/nativeMachoDylibIdentityCli.test.ts @@ -20,18 +20,14 @@ describe.skipIf(process.platform !== "darwin")( const source = join(directory, "library.c"); const library = join(directory, "libprobe.dylib"); await writeFile(source, "int exported_value(void) { return 42; }\n"); - const compiled = await processes.run( - "/usr/bin/xcrun", - [ - "clang", - "-dynamiclib", - source, - "-Wl,-install_name,@rpath/libprobe.dylib", - "-o", - library, - ], - { timeoutMs: 30_000 }, - ); + const compiled = await processes.run("/usr/bin/xcrun", [ + "clang", + "-dynamiclib", + source, + "-Wl,-install_name,@rpath/libprobe.dylib", + "-o", + library, + ]); expect(compiled.exitCode, compiled.stderr).toBe(0); const result = await cli.run({ arguments: ["inspect-macho", library, "--json"], @@ -54,11 +50,12 @@ describe.skipIf(process.platform !== "darwin")( // One architecture can still have a FAT container header. const universal = join(directory, "libprobe-universal.dylib"); - const combined = await processes.run( - "/usr/bin/lipo", - ["-create", library, "-output", universal], - { timeoutMs: 30_000 }, - ); + const combined = await processes.run("/usr/bin/lipo", [ + "-create", + library, + "-output", + universal, + ]); expect(combined.exitCode, combined.stderr).toBe(0); const fatResult = await cli.run({ arguments: ["inspect-macho", universal, "--json"], diff --git a/tests/boundary/cli/nativeMachoEchoedPathCli.test.ts b/tests/boundary/cli/nativeMachoEchoedPathCli.test.ts index 5854281a23..76f9465aa2 100644 --- a/tests/boundary/cli/nativeMachoEchoedPathCli.test.ts +++ b/tests/boundary/cli/nativeMachoEchoedPathCli.test.ts @@ -52,20 +52,16 @@ describe.skipIf(process.platform !== "darwin")( "rea-native-cli-echo-", ); const fixture = join(directory, "fat-fixture"); - const compiled = await processes.run( - "/usr/bin/xcrun", - [ - "clang", - "-arch", - "arm64", - "-arch", - "x86_64", - resolve("tests/conformance/c/fixture.c"), - "-o", - fixture, - ], - { timeoutMs: 30_000 }, - ); + const compiled = await processes.run("/usr/bin/xcrun", [ + "clang", + "-arch", + "arm64", + "-arch", + "x86_64", + resolve("tests/conformance/c/fixture.c"), + "-o", + fixture, + ]); expect(compiled.exitCode, compiled.stderr).toBe(0); const inspect = async (name: string) => { const path = join(directory, name); @@ -98,7 +94,6 @@ describe.skipIf(process.platform !== "darwin")( expect(forged).toEqual(ordinary); expect(JSON.stringify(forged)).not.toMatch(/forged|4242/u); }, - 60_000, ); } }, diff --git a/tests/boundary/cli/nativeMachoEncryptionCli.test.ts b/tests/boundary/cli/nativeMachoEncryptionCli.test.ts index 2a60d39b8f..841ff86fb6 100644 --- a/tests/boundary/cli/nativeMachoEncryptionCli.test.ts +++ b/tests/boundary/cli/nativeMachoEncryptionCli.test.ts @@ -37,9 +37,7 @@ describe.skipIf(process.platform !== "darwin")( const fat = join(directory, "fat_enc"); await writeFile(source, "int main(void) { return 0; }\n"); const run = async (command: string, arguments_: string[]) => { - const result = await processes.run(command, arguments_, { - timeoutMs: 30_000, - }); + const result = await processes.run(command, arguments_); expect(result.exitCode, result.stderr).toBe(0); return result.stdout; }; diff --git a/tests/boundary/cli/nativeMachoLinkerOptionsCli.test.ts b/tests/boundary/cli/nativeMachoLinkerOptionsCli.test.ts index 706863201e..3a76a3aa7d 100644 --- a/tests/boundary/cli/nativeMachoLinkerOptionsCli.test.ts +++ b/tests/boundary/cli/nativeMachoLinkerOptionsCli.test.ts @@ -28,11 +28,13 @@ describe.skipIf(process.platform !== "darwin")( "", ].join("\n"), ); - const compiled = await processes.run( - "/usr/bin/xcrun", - ["clang", "-c", source, "-o", object], - { timeoutMs: 30_000 }, - ); + const compiled = await processes.run("/usr/bin/xcrun", [ + "clang", + "-c", + source, + "-o", + object, + ]); expect(compiled.exitCode, compiled.stderr).toBe(0); const result = await cli.run({ arguments: ["inspect-macho", object, "--json"], diff --git a/tests/boundary/cli/nativeMachoMissingUuidCli.test.ts b/tests/boundary/cli/nativeMachoMissingUuidCli.test.ts index a054c0fefe..c20ddcc713 100644 --- a/tests/boundary/cli/nativeMachoMissingUuidCli.test.ts +++ b/tests/boundary/cli/nativeMachoMissingUuidCli.test.ts @@ -58,9 +58,7 @@ describe.skipIf(process.platform !== "darwin")( ["clang", "-o", control, source], ["clang", "-c", source, "-o", object], ]) { - const compiled = await processes.run("/usr/bin/xcrun", arguments_, { - timeoutMs: 30_000, - }); + const compiled = await processes.run("/usr/bin/xcrun", arguments_); expect(compiled.exitCode, compiled.stderr).toBe(0); } diff --git a/tests/boundary/cli/nativeMachoObjectSegmentCli.test.ts b/tests/boundary/cli/nativeMachoObjectSegmentCli.test.ts index 83eb8fd40f..3c6852ece7 100644 --- a/tests/boundary/cli/nativeMachoObjectSegmentCli.test.ts +++ b/tests/boundary/cli/nativeMachoObjectSegmentCli.test.ts @@ -20,11 +20,13 @@ describe.skipIf(process.platform !== "darwin")( const source = join(directory, "qa.c"); const object = join(directory, "qa.o"); await writeFile(source, "int qa(void) { return 7; }\n"); - const compiled = await processes.run( - "/usr/bin/xcrun", - ["clang", "-c", source, "-o", object], - { timeoutMs: 30_000 }, - ); + const compiled = await processes.run("/usr/bin/xcrun", [ + "clang", + "-c", + source, + "-o", + object, + ]); expect(compiled.exitCode, compiled.stderr).toBe(0); const result = await cli.run({ arguments: ["inspect-macho", object, "--json"], diff --git a/tests/boundary/cli/nativeSignatureCli.test.ts b/tests/boundary/cli/nativeSignatureCli.test.ts index f69de01daa..3e3a4f01ef 100644 --- a/tests/boundary/cli/nativeSignatureCli.test.ts +++ b/tests/boundary/cli/nativeSignatureCli.test.ts @@ -136,7 +136,6 @@ describe.skipIf(process.platform !== "darwin" || process.arch !== "arm64")( normalized_result: { signed: true, cdhashes: [cdhash] }, }); }, - 60_000, ); }, ); diff --git a/tests/boundary/cli/overnightBatch1CliSchemas.test.ts b/tests/boundary/cli/overnightBatch1CliSchemas.test.ts index b9aac999f1..9576d153ce 100644 --- a/tests/boundary/cli/overnightBatch1CliSchemas.test.ts +++ b/tests/boundary/cli/overnightBatch1CliSchemas.test.ts @@ -1,9 +1,8 @@ import { join } from "node:path"; import { Ajv2020 } from "ajv/dist/2020.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; -import { Cli } from "incur"; +import { Cli, z } from "incur"; import { expect, it, vi } from "vitest"; -import { z } from "incur"; import { createAnalysisExecution } from "../../../src/application/AnalysisProvider.js"; import type { AdbDeviceAnalysisPort } from "../../../src/application/adb/AdbDeviceAnalysisPort.js"; import type { ApktoolResourceAnalysisPort } from "../../../src/application/apktool/ApktoolResourceAnalysisPort.js"; diff --git a/tests/boundary/cli/packageRunnerBoundInstallation.test.ts b/tests/boundary/cli/packageRunnerBoundInstallation.test.ts index 05a5aaa6bf..28ec890050 100644 --- a/tests/boundary/cli/packageRunnerBoundInstallation.test.ts +++ b/tests/boundary/cli/packageRunnerBoundInstallation.test.ts @@ -70,7 +70,6 @@ cliTest.skipIf(process.platform === "win32")( const setup = await processes.run(npm.command, npm.arguments, { cwd: trusted, env: environment, - timeoutMs: 30_000, }); expect(setup.exitCode, setup.stderr).toBe(0); const configPath = join(home, ".omp", "agent", "mcp.json"); @@ -160,7 +159,7 @@ cliTest.skipIf(process.platform === "win32")( const missing = await processes.run( registration.command, registration.args, - { cwd: project, env: clientEnvironment, timeoutMs: 10_000 }, + { cwd: project, env: clientEnvironment }, ); expect(missing.exitCode).not.toBe(0); await expect(access(marker)).rejects.toMatchObject({ code: "ENOENT" }); @@ -171,7 +170,6 @@ cliTest.skipIf(process.platform === "win32")( expect(await readFile(configPath, "utf8")).toBe(original); await expect(access(marker)).rejects.toMatchObject({ code: "ENOENT" }); }, - 60_000, ); cliTest.skipIf(process.platform === "win32")( @@ -193,7 +191,6 @@ cliTest.skipIf(process.platform === "win32")( USERPROFILE: home, PATH: [bin, process.env.PATH ?? ""].join(delimiter), }, - timeoutMs: 20_000, }, ); expect(result.exitCode, result.stderr).toBe(0); @@ -212,7 +209,7 @@ cliTest.skipIf(process.platform === "win32")( const start = await processes.run( settings["amp.mcpServers"].rea.command, [alias, "--version"], - { cwd: home, timeoutMs: 10_000 }, + { cwd: home }, ); expect(start.exitCode, start.stderr).toBe(0); expect(start.stdout).toContain(PRODUCT_IDENTITY.packageVersion); diff --git a/tests/boundary/cli/packageRunnerRegistration.test.ts b/tests/boundary/cli/packageRunnerRegistration.test.ts index 4329be78b4..f883bce32d 100644 --- a/tests/boundary/cli/packageRunnerRegistration.test.ts +++ b/tests/boundary/cli/packageRunnerRegistration.test.ts @@ -75,7 +75,6 @@ for (const { runner, userAgent } of [ arguments: arguments_, cwd: home, environment, - timeoutMs: 20_000, }); const setup = await run([ "setup", diff --git a/tests/boundary/cli/pcapCli.test.ts b/tests/boundary/cli/pcapCli.test.ts index e042f4985b..34de45077e 100644 --- a/tests/boundary/cli/pcapCli.test.ts +++ b/tests/boundary/cli/pcapCli.test.ts @@ -1,4 +1,4 @@ -import { expect, it } from "vitest"; +import { it } from "vitest"; import { expectConfigurationRefusal, serveCli } from "./cliServeHarness.js"; diff --git a/tests/boundary/cli/projectedErrorStatus.test.ts b/tests/boundary/cli/projectedErrorStatus.test.ts index ca3777635f..7bd4a5e110 100644 --- a/tests/boundary/cli/projectedErrorStatus.test.ts +++ b/tests/boundary/cli/projectedErrorStatus.test.ts @@ -45,6 +45,5 @@ describe("projected CLI failures", () => { expect(result.json).toMatchObject({ code: "invalid_request" }); } }, - 30_000, ); }); diff --git a/tests/boundary/cli/rawDiskImageCli.test.ts b/tests/boundary/cli/rawDiskImageCli.test.ts index 307c00326f..bcbff8f3b5 100644 --- a/tests/boundary/cli/rawDiskImageCli.test.ts +++ b/tests/boundary/cli/rawDiskImageCli.test.ts @@ -108,7 +108,6 @@ describe.skipIf(process.platform !== "darwin")( expect(attached.stdout).not.toContain(raw); expect(attached.stdout).not.toContain(compressed); }, - 120_000, ); cliTest( diff --git a/tests/boundary/cli/reconstructionObligationLedgerCli.test.ts b/tests/boundary/cli/reconstructionObligationLedgerCli.test.ts index 357219444e..6b08587d76 100644 --- a/tests/boundary/cli/reconstructionObligationLedgerCli.test.ts +++ b/tests/boundary/cli/reconstructionObligationLedgerCli.test.ts @@ -6,7 +6,7 @@ import { join } from "node:path"; import { describe, expect, it } from "vitest"; -import { cliTest, runFileCli } from "../../support/cli/cliFixture.js"; +import { cliTest } from "../../support/cli/cliFixture.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; import { javascriptApplicationAnalysisResultSchema } from "../../../src/domain/javascript/javascriptApplicationAnalysis.js"; import { reconstructionObligationLedgerSchema } from "../../../src/domain/reconstructionObligationLedgerSchemas.js"; @@ -76,7 +76,7 @@ describe("reconstruction obligation ledger CLI parity", () => { ); } } - }, 20_000); + }); }); describe("reconstruction obligation ledger CLI bundle validation", () => { @@ -147,7 +147,7 @@ describe("reconstruction obligation ledger CLI bundle validation", () => { ), }, }); - }, 20_000); + }); it("builds the same Evidence-backed empty ledger through the CLI", async () => { const input = { @@ -181,7 +181,7 @@ describe("reconstruction obligation ledger CLI bundle validation", () => { summary: { total: 0 }, }, }); - }, 20_000); + }); it("summarizes obligations by their cited Evidence authorities", async () => { const { stdout } = await execute( @@ -221,7 +221,7 @@ describe("reconstruction obligation ledger CLI bundle validation", () => { ], ["review.shipped", ["shipped-artifact/candidate"]], ]); - }, 20_000); + }); }); describe("reconstruction obligation ledger CLI contradictions", () => { @@ -282,7 +282,7 @@ describe("reconstruction obligation ledger CLI contradictions", () => { ], }); } - }, 20_000); + }); }); cliTest( @@ -376,44 +376,38 @@ describe("reconstruction proof applicability through CLI", () => { reason, codes, } of reconstructionProofApplicabilityCases()) - cliTest( - name, - async ({ cli }) => { - const invocation = { - arguments: [ - "build-reconstruction-obligation-ledger", - JSON.stringify(input), - "--json", - ], - }; - const result = - name === "matching implementation and revision" - ? await cli.run(invocation) - : await runFileCli(cli, invocation); - expect(result.exitCode, result.stderr).toBe(0); - const ledger = reconstructionObligationLedgerSchema.parse( - (result.json as { normalized_result: unknown }).normalized_result, - ); - expect(ledger.status).toBe(status); - const obligation = ledger.obligations[0]!; - expect(obligation.status).toBe( - status === "ready" ? "verified" : "implemented", - ); - expect(obligation.diagnostics.map(({ code }) => code).sort()).toEqual( - [...codes].sort(), - ); - expect(ledger.summary.required_open).toBe(status === "ready" ? 0 : 1); - for (const fixture of input.manifest.bindings[0]!.fixtures) - for (const evidenceId of fixture.evidence_ids) - expect(ledger.evidence_links).toContain(evidenceId); - if (reason === null) expect(obligation.diagnostics).toEqual([]); - else - expect( - obligation.diagnostics.some(({ detail }) => - detail.includes(reason), - ), - ).toBe(true); - }, - 20_000, - ); + cliTest(name, async ({ cli }) => { + const invocation = { + arguments: [ + "build-reconstruction-obligation-ledger", + JSON.stringify(input), + "--json", + ], + }; + const result = await cli.run({ + ...invocation, + freshProcess: name === "matching implementation and revision", + }); + expect(result.exitCode, result.stderr).toBe(0); + const ledger = reconstructionObligationLedgerSchema.parse( + (result.json as { normalized_result: unknown }).normalized_result, + ); + expect(ledger.status).toBe(status); + const obligation = ledger.obligations[0]!; + expect(obligation.status).toBe( + status === "ready" ? "verified" : "implemented", + ); + expect(obligation.diagnostics.map(({ code }) => code).sort()).toEqual( + [...codes].sort(), + ); + expect(ledger.summary.required_open).toBe(status === "ready" ? 0 : 1); + for (const fixture of input.manifest.bindings[0]!.fixtures) + for (const evidenceId of fixture.evidence_ids) + expect(ledger.evidence_links).toContain(evidenceId); + if (reason === null) expect(obligation.diagnostics).toEqual([]); + else + expect( + obligation.diagnostics.some(({ detail }) => detail.includes(reason)), + ).toBe(true); + }); }); diff --git a/tests/boundary/cli/referenceSourceImportStatus.test.ts b/tests/boundary/cli/referenceSourceImportStatus.test.ts index a9565c09bc..5fa336b11d 100644 --- a/tests/boundary/cli/referenceSourceImportStatus.test.ts +++ b/tests/boundary/cli/referenceSourceImportStatus.test.ts @@ -73,7 +73,6 @@ describe("compiled Windows reference-source import", () => { }); } else expect(result.stderr).toBe(""); }, - 60_000, ); } }); diff --git a/tests/boundary/cli/referenceSourceSummary.test.ts b/tests/boundary/cli/referenceSourceSummary.test.ts index 2647f09aa4..06adf583fe 100644 --- a/tests/boundary/cli/referenceSourceSummary.test.ts +++ b/tests/boundary/cli/referenceSourceSummary.test.ts @@ -5,9 +5,9 @@ import { join } from "node:path"; import canonicalize from "canonicalize"; import { describe, expect } from "vitest"; -import { parseHistoricalSourceGraph } from "../../../src/domain/referenceSourceGraph.js"; +import { historicalSourceGraphSchema } from "../../../src/domain/referenceSourceGraph.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; -import { cliTest, runFileCli } from "../../support/cli/cliFixture.js"; +import { cliTest } from "../../support/cli/cliFixture.js"; describe.skipIf(process.platform === "win32")( "compiled historical-source summaries", @@ -37,7 +37,7 @@ describe.skipIf(process.platform === "win32")( }); const complete = await run(); expect(complete.exitCode).toBe(0); - const graph = parseHistoricalSourceGraph(complete.json); + const graph = historicalSourceGraphSchema.parse(complete.json); const graphJson = canonicalize(graph); if (graphJson === undefined) throw new TypeError("Unserializable complete graph"); @@ -199,10 +199,10 @@ describe("compiled historical-source summary input boundaries", () => { arguments: ["import-reference-source", root, ...options, "--json"], cwd: root, }; - const result = - label === "--outline-depth=" - ? await cli.run(invocation) - : await runFileCli(cli, invocation); + const result = await cli.run({ + ...invocation, + freshProcess: label === "--outline-depth=", + }); expect(result.exitCode).toBe(1); expect(result.json).toMatchObject({ code: "VALIDATION_ERROR", @@ -226,7 +226,7 @@ describe("compiled historical-source summary input boundaries", () => { const root = await createTestTempDirectory( "rea-reference-summary-input-", ); - const result = await runFileCli(cli, { + const result = await cli.run({ arguments: ["import-reference-source", root, ...options, "--json"], cwd: root, }); diff --git a/tests/boundary/cli/runtimeUrlPrefixMappingCli.test.ts b/tests/boundary/cli/runtimeUrlPrefixMappingCli.test.ts index 7c6c157998..2c8735d9ab 100644 --- a/tests/boundary/cli/runtimeUrlPrefixMappingCli.test.ts +++ b/tests/boundary/cli/runtimeUrlPrefixMappingCli.test.ts @@ -14,7 +14,6 @@ import { passiveNetworkCapture } from "../../fixtures/passiveNetworkCapture.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; const execute = promisify(execFile); -const INTEGRATION_TEST_TIMEOUT_MS = 30_000; const temporary: string[] = []; afterEach(async () => { @@ -197,109 +196,97 @@ const mappedLocation = (path: string) => ({ observed_as: 1, }); -it( - "matches a source-free webpack script to its containing asset", - async () => { - const root = await createTestTempDirectory("rea-url-bundler-asset-"); - temporary.push(root); - await mkdir(join(root, "chunks")); - await writeFile( - join(root, "chunks", "runtime.js"), - '(globalThis["webpackChunkfixture"] = globalThis["webpackChunkfixture"] || []).push([["main"],{1:(module)=>{module.exports=1;}},(__webpack_require__)=>{__webpack_require__(1);}]);\n', - ); - const analysis = await analyzeFixture(root); - const result = await reconcileThroughCli(root, analysis, [ - "chunks/runtime.js", - ]); - expect(result.scripts.get("chunks/runtime.js")).toEqual( - mappedLocation("chunks/runtime.js"), - ); - expect(result.loadStates.get("chunks/runtime.js")).toEqual({ +it("matches a source-free webpack script to its containing asset", async () => { + const root = await createTestTempDirectory("rea-url-bundler-asset-"); + temporary.push(root); + await mkdir(join(root, "chunks")); + await writeFile( + join(root, "chunks", "runtime.js"), + '(globalThis["webpackChunkfixture"] = globalThis["webpackChunkfixture"] || []).push([["main"],{1:(module)=>{module.exports=1;}},(__webpack_require__)=>{__webpack_require__(1);}]);\n', + ); + const analysis = await analyzeFixture(root); + const result = await reconcileThroughCli(root, analysis, [ + "chunks/runtime.js", + ]); + expect(result.scripts.get("chunks/runtime.js")).toEqual( + mappedLocation("chunks/runtime.js"), + ); + expect(result.loadStates.get("chunks/runtime.js")).toEqual({ + status: "loaded", + reason: "runtime-script-correspondence", + }); +}); + +it("keeps an encoded slash inside one URL segment rather than naming a nested artifact", async () => { + const root = await createTestTempDirectory("rea-url-prefix-segments-"); + temporary.push(root); + await mkdir(join(root, "child")); + await Promise.all([ + writeFile(join(root, "child", "part.js"), "export const nested = 1;\n"), + writeFile(join(root, "child%2Fpart.js"), "export const literal = 2;\n"), + writeFile(join(root, "sp ace.js"), "export const space = 3;\n"), + writeFile(join(root, "é.js"), "export const accent = 4;\n"), + ]); + const analysis = await analyzeFixture(root); + + const { scripts } = await reconcileThroughCli(root, analysis, [ + "child/part.js", + "child/part.js?v=%2F#f%2F", + "child%2Fpart.js", + "child%2fpart.js", + "child%252Fpart.js", + "sp%20ace.js", + "%C3%A9.js", + "child/%E0.js", + ]); + + expect(Object.fromEntries(scripts)).toEqual({ + "child/part.js": mappedLocation("child/part.js"), + "child/part.js?v=%2F#f%2F": mappedLocation("child/part.js"), + "child%2Fpart.js": unmappedLocation, + "child%2fpart.js": unmappedLocation, + "child%252Fpart.js": mappedLocation("child%2Fpart.js"), + "sp%20ace.js": mappedLocation("sp ace.js"), + "%C3%A9.js": mappedLocation("é.js"), + "child/%E0.js": unmappedLocation, + }); +}); + +it("keeps static load state unknown beneath a prefix whose runtime script had no artifact path", async () => { + const root = await createTestTempDirectory("rea-url-prefix-load-state-"); + temporary.push(root); + await mkdir(join(root, "child")); + await Promise.all([ + writeFile(join(root, "child", "part.js"), "export const nested = 1;\n"), + writeFile(join(root, "other.js"), "export const other = 2;\n"), + ]); + const analysis = await analyzeFixture(root); + + const control = await reconcileThroughCli(root, analysis, ["other.js"]); + expect(Object.fromEntries(control.loadStates)).toEqual({ + "child/part.js": { + status: "not-observed-in-capture", + reason: "not-observed-in-bounded-capture", + }, + "other.js": { status: "loaded", reason: "runtime-script-correspondence", - }); - }, - INTEGRATION_TEST_TIMEOUT_MS, -); + }, + }); -it( - "keeps an encoded slash inside one URL segment rather than naming a nested artifact", - async () => { - const root = await createTestTempDirectory("rea-url-prefix-segments-"); - temporary.push(root); - await mkdir(join(root, "child")); - await Promise.all([ - writeFile(join(root, "child", "part.js"), "export const nested = 1;\n"), - writeFile(join(root, "child%2Fpart.js"), "export const literal = 2;\n"), - writeFile(join(root, "sp ace.js"), "export const space = 3;\n"), - writeFile(join(root, "é.js"), "export const accent = 4;\n"), - ]); - const analysis = await analyzeFixture(root); - - const { scripts } = await reconcileThroughCli(root, analysis, [ - "child/part.js", - "child/part.js?v=%2F#f%2F", - "child%2Fpart.js", - "child%2fpart.js", - "child%252Fpart.js", - "sp%20ace.js", - "%C3%A9.js", - "child/%E0.js", - ]); - - expect(Object.fromEntries(scripts)).toEqual({ - "child/part.js": mappedLocation("child/part.js"), - "child/part.js?v=%2F#f%2F": mappedLocation("child/part.js"), - "child%2Fpart.js": unmappedLocation, - "child%2fpart.js": unmappedLocation, - "child%252Fpart.js": mappedLocation("child%2Fpart.js"), - "sp%20ace.js": mappedLocation("sp ace.js"), - "%C3%A9.js": mappedLocation("é.js"), - "child/%E0.js": unmappedLocation, - }); - }, - INTEGRATION_TEST_TIMEOUT_MS, -); - -it( - "keeps static load state unknown beneath a prefix whose runtime script had no artifact path", - async () => { - const root = await createTestTempDirectory("rea-url-prefix-load-state-"); - temporary.push(root); - await mkdir(join(root, "child")); - await Promise.all([ - writeFile(join(root, "child", "part.js"), "export const nested = 1;\n"), - writeFile(join(root, "other.js"), "export const other = 2;\n"), - ]); - const analysis = await analyzeFixture(root); - - const control = await reconcileThroughCli(root, analysis, ["other.js"]); - expect(Object.fromEntries(control.loadStates)).toEqual({ - "child/part.js": { - status: "not-observed-in-capture", - reason: "not-observed-in-bounded-capture", - }, - "other.js": { - status: "loaded", - reason: "runtime-script-correspondence", - }, - }); - - const encoded = await reconcileThroughCli(root, analysis, [ - "other.js", - "child%2Fpart.js", - ]); - expect(encoded.scripts.get("child%2Fpart.js")).toEqual(unmappedLocation); - expect(Object.fromEntries(encoded.loadStates)).toEqual({ - "child/part.js": { - status: "unknown", - reason: "static-or-runtime-coverage-incomplete", - }, - "other.js": { - status: "loaded", - reason: "runtime-script-correspondence", - }, - }); - }, - INTEGRATION_TEST_TIMEOUT_MS, -); + const encoded = await reconcileThroughCli(root, analysis, [ + "other.js", + "child%2Fpart.js", + ]); + expect(encoded.scripts.get("child%2Fpart.js")).toEqual(unmappedLocation); + expect(Object.fromEntries(encoded.loadStates)).toEqual({ + "child/part.js": { + status: "unknown", + reason: "static-or-runtime-coverage-incomplete", + }, + "other.js": { + status: "loaded", + reason: "runtime-script-correspondence", + }, + }); +}); diff --git a/tests/boundary/cli/setupProviderReadiness.test.ts b/tests/boundary/cli/setupProviderReadiness.test.ts index 3c29d86a01..d4a065bc5a 100644 --- a/tests/boundary/cli/setupProviderReadiness.test.ts +++ b/tests/boundary/cli/setupProviderReadiness.test.ts @@ -28,7 +28,6 @@ cliTest.skipIf(process.platform === "win32").for(["blocked", "ready"] as const)( REA_KUNA_COMMAND: command, REA_KUNA_CACHE_DIR: cachePath, }, - timeoutMs: 20_000, }); expect(result.exitCode, result.stderr).toBe(0); expect(result.json).toMatchObject({ @@ -88,7 +87,6 @@ cliTest.skipIf(process.platform === "win32")( const setup = await cli.run({ arguments: ["setup", "--client", "claude_code", "--dry-run", "--json"], environment, - timeoutMs: 20_000, }); expect(setup.exitCode, setup.stdout).toBe(0); expect(setup.json).toMatchObject({ @@ -111,7 +109,6 @@ cliTest.skipIf(process.platform === "win32")( const doctor = await cli.run({ arguments: ["doctor", "--json"], environment, - timeoutMs: 20_000, }); expect(doctor.json).toMatchObject({ providerInspections: expect.arrayContaining([ diff --git a/tests/boundary/cli/sharedJavaScriptCliSession.test.ts b/tests/boundary/cli/sharedJavaScriptCliSession.test.ts index 420d019622..c39a5630f8 100644 --- a/tests/boundary/cli/sharedJavaScriptCliSession.test.ts +++ b/tests/boundary/cli/sharedJavaScriptCliSession.test.ts @@ -922,15 +922,6 @@ test("closes the real JavaScript CLI worker through its resource cleanup", async expect(session.processId()).toBeUndefined(); }); -const requireSharedCli = (cli: { - sharedCliRun?: (invocation: CliInvocation) => Promise; -}): ((invocation: CliInvocation) => Promise) => { - const run = cli.sharedCliRun; - if (run === undefined) - throw new Error("cliTest did not provide a shared CLI session"); - return run; -}; - const normalized = (result: CliResult) => z .object({ normalized_result: javascriptApplicationAnalysisResultSchema }) @@ -957,12 +948,12 @@ const writeApplication = async (prefix: string): Promise => { cliTest( "matches a fresh CLI process and reuses that process for the same environment", async ({ cli, cliCommandSession }) => { - const sharedCliRun = requireSharedCli(cli); const fresh = await cli.run({ + freshProcess: true, arguments: ["--version"], environment: { REA_LOG_LEVEL: "silent" }, }); - const shared = await sharedCliRun({ + const shared = await cli.run({ arguments: ["--version"], environment: { REA_LOG_LEVEL: "silent" }, }); @@ -971,7 +962,7 @@ cliTest( expect(shared.stderr).toBe(fresh.stderr); const pid = cliCommandSession.processId(); expect(pid).toEqual(expect.any(Number)); - const again = await sharedCliRun({ + const again = await cli.run({ arguments: ["--version"], environment: { REA_LOG_LEVEL: "silent" }, }); @@ -979,10 +970,11 @@ cliTest( expect(cliCommandSession.processId()).toBe(pid); const freshInvalid = await cli.run({ + freshProcess: true, arguments: ["--version"], environment: { REA_LOG_LEVEL: "not-a-level" }, }); - const sharedInvalid = await sharedCliRun({ + const sharedInvalid = await cli.run({ arguments: ["--version"], environment: { REA_LOG_LEVEL: "not-a-level" }, }); @@ -992,7 +984,7 @@ cliTest( expect(sharedInvalid.stderr).toBe(freshInvalid.stderr); expect(cliCommandSession.processId()).not.toBe(pid); - const restored = await sharedCliRun({ + const restored = await cli.run({ arguments: ["--version"], environment: { REA_LOG_LEVEL: "silent" }, }); @@ -1005,13 +997,13 @@ cliTest( cliTest( "reuses one CLI process across JavaScript application directories", async ({ cli, cliCommandSession }) => { - const sharedCliRun = requireSharedCli(cli); const firstRoot = await writeApplication("rea-shared-cli-"); const fresh = await cli.run({ + freshProcess: true, arguments: analyzeArguments(firstRoot), environment: { REA_LOG_LEVEL: "silent" }, }); - const shared = await sharedCliRun({ + const shared = await cli.run({ arguments: analyzeArguments(firstRoot), environment: { REA_LOG_LEVEL: "silent" }, }); @@ -1022,10 +1014,11 @@ cliTest( const secondRoot = await writeApplication("rea-shared-cli-next-"); const freshNext = await cli.run({ + freshProcess: true, arguments: analyzeArguments(secondRoot), environment: { REA_LOG_LEVEL: "silent" }, }); - const sharedNext = await sharedCliRun({ + const sharedNext = await cli.run({ arguments: analyzeArguments(secondRoot), environment: { REA_LOG_LEVEL: "silent" }, }); diff --git a/tests/boundary/cli/skillSync.test.ts b/tests/boundary/cli/skillSync.test.ts index 5024ccd2f2..c78969b69c 100644 --- a/tests/boundary/cli/skillSync.test.ts +++ b/tests/boundary/cli/skillSync.test.ts @@ -4,8 +4,6 @@ import { describe, expect } from "vitest"; import { workspaceCliTest } from "../../support/cli/workspaceCliFixture.js"; -const CLI_INTEGRATION_TIMEOUT_MS = 60_000; - describe("CLI skill lifecycle boundary", () => { workspaceCliTest( "does not expose the unmanaged Incur skill generator", @@ -25,6 +23,5 @@ describe("CLI skill lifecycle boundary", () => { expect(result.stdout).toContain("COMMAND_NOT_FOUND"); await expect(readdir(workspace.home)).resolves.toEqual([]); }, - CLI_INTEGRATION_TIMEOUT_MS, ); }); diff --git a/tests/boundary/cli/webScriptSelectionCli.test.ts b/tests/boundary/cli/webScriptSelectionCli.test.ts index 8c6bfc8f40..725576ac5d 100644 --- a/tests/boundary/cli/webScriptSelectionCli.test.ts +++ b/tests/boundary/cli/webScriptSelectionCli.test.ts @@ -130,15 +130,12 @@ cliTest.skipIf(process.platform === "win32")( const root = await createTestTempDirectory("rea-script-selection-cli-"); const capturePath = join(root, "capture.pipe"); const outputDirectory = join(root, "export"); - const created = await processes.run("mkfifo", [capturePath], { - timeoutMs: 5_000, - }); + const created = await processes.run("mkfifo", [capturePath]); expect(created.exitCode).toBe(0); const response = await cli.run({ arguments: ["export-web-scripts", capturePath, outputDirectory, "--json"], environment: { REA_LOG_LEVEL: "silent" }, - timeoutMs: 5_000, }); expect(response.exitCode).toBe(1); expect(response.signal).toBeNull(); diff --git a/tests/boundary/cli/workflowInputIdentities.test.ts b/tests/boundary/cli/workflowInputIdentities.test.ts index 7f45350f60..7163712abd 100644 --- a/tests/boundary/cli/workflowInputIdentities.test.ts +++ b/tests/boundary/cli/workflowInputIdentities.test.ts @@ -4,12 +4,17 @@ import { INLINE_EVIDENCE_MCP_ENVIRONMENT } from "../../../scripts/lib/mcp-verifi import { Ajv2020 } from "ajv/dist/2020.js"; import { mkdir, writeFile } from "node:fs/promises"; import { join, resolve } from "node:path"; -import { expect, onTestFinished } from "vitest"; +import { afterAll, expect, onTestFinished } from "vitest"; import { toolContract } from "../../../src/contracts/toolContracts.js"; import { parseMcpToolError } from "../../fixtures/mcpToolError.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; import { cliTest, type TestCli } from "../../support/cli/cliFixture.js"; +import { createMcpSchemaValidation } from "../../fixtures/mcpSchemaValidation.js"; + +// Compile SDK output validators for the tools a test calls, not the whole catalog. +const schemaValidation = createMcpSchemaValidation(); +afterAll(() => schemaValidation.clear()); const compareContract = toolContract("compare_application_versions"); const analyzeContract = toolContract("analyze_javascript_application"); @@ -36,7 +41,7 @@ const runJson = async ( await writeFile(path, JSON.stringify(input)); // These JSON commands share no provider state; reuse the fixture-owned CLI // process so the matrix measures validation rather than repeated startup. - const run = cli.sharedCliRun?.bind(cli) ?? cli.run.bind(cli); + const run = cli.run.bind(cli); return run({ arguments: [ command, @@ -51,10 +56,13 @@ const runJson = async ( }); }; const connectMcp = async (root: string) => { - const client = new Client({ - name: "application-input-identities", - version: "1", - }); + const client = new Client( + { + name: "application-input-identities", + version: "1", + }, + { jsonSchemaValidator: schemaValidation }, + ); const transport = new StdioClientTransport({ command: process.execPath, args: [resolve("scripts/rea.mjs"), "mcp"], diff --git a/tests/boundary/evm/evmLimiterAvailability.test.ts b/tests/boundary/evm/evmLimiterAvailability.test.ts index 7684146571..9f83640783 100644 --- a/tests/boundary/evm/evmLimiterAvailability.test.ts +++ b/tests/boundary/evm/evmLimiterAvailability.test.ts @@ -6,10 +6,7 @@ import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; import { withVerifierCleanup } from "../../../scripts/lib/verifier-cleanup.mjs"; import { buildCapabilityInventory } from "../../../src/application/CapabilityInventory.js"; import { AnalysisCancelledError } from "../../../src/domain/analysisErrorCore.js"; -import { - EVM_PRLIMIT_COMMAND_DEFAULT, - evmLimiterAvailability, -} from "../../../src/evm/EvmoleInterfaceProvider.js"; +import { evmLimiterAvailability } from "../../../src/evm/EvmoleInterfaceProvider.js"; import { awaitAcknowledgement } from "../../fixtures/lifecycleAcknowledgement.js"; const linux = { platform: "linux" as const, arch: "x64" }; @@ -36,11 +33,11 @@ it("probes the default limiter with executable and regular-file checks", async ( expect(availability).toMatchObject({ status: "available", - diagnostics: { limiter_path: EVM_PRLIMIT_COMMAND_DEFAULT }, + diagnostics: { limiter_path: "/usr/bin/prlimit" }, }); expect(calls).toEqual([ - ["access", EVM_PRLIMIT_COMMAND_DEFAULT, 1], - ["stat", EVM_PRLIMIT_COMMAND_DEFAULT], + ["access", "/usr/bin/prlimit", 1], + ["stat", "/usr/bin/prlimit"], ]); }); diff --git a/tests/boundary/filesystem/appleXmlDeclarations.test.ts b/tests/boundary/filesystem/appleXmlDeclarations.test.ts index df8a1676a5..1f916a017c 100644 --- a/tests/boundary/filesystem/appleXmlDeclarations.test.ts +++ b/tests/boundary/filesystem/appleXmlDeclarations.test.ts @@ -40,7 +40,7 @@ it("preflights repeated unterminated quoted declarations without suffix rescans" expected: number; }; expect(result.observed.estimatedBytes).toBe(result.expected); -}, 10000); +}); const fixture = async (declaration: string) => { const bundlePath = await createTestTempDirectory("rea-xml-declaration-"); diff --git a/tests/boundary/filesystem/artifactComparison.test.ts b/tests/boundary/filesystem/artifactComparison.test.ts index 5cdf91d12e..716d2056f9 100644 --- a/tests/boundary/filesystem/artifactComparison.test.ts +++ b/tests/boundary/filesystem/artifactComparison.test.ts @@ -567,7 +567,7 @@ describe("artifact comparison completeness", () => { summary: { unchanged: 502, unknown: 0 }, changes: [], }); - }, 15_000); + }); }); it.runIf(process.platform !== "win32" && process.getuid?.() !== 0)( @@ -974,7 +974,7 @@ describe("artifact comparison fragment integrity", () => { issues: [expect.objectContaining({ path: ["left"] })], }), ); - }, 15_000); + }); it("rejects non-inventory and tampered Evidence", async () => { const root = await createTestTempDirectory("rea-artifact-invalid-"); diff --git a/tests/boundary/filesystem/asarSelectedCompanion.test.ts b/tests/boundary/filesystem/asarSelectedCompanion.test.ts index 4b500abdba..c03bc8328a 100644 --- a/tests/boundary/filesystem/asarSelectedCompanion.test.ts +++ b/tests/boundary/filesystem/asarSelectedCompanion.test.ts @@ -4,7 +4,6 @@ import { parseBinaryTarget } from "../../../src/application/BinaryTargetResolver import { createDirectAnalysis } from "../../../src/composition/directAnalysis.js"; import { withArtifactClient } from "../../support/artifactClientScope.js"; import { artifactInventoryResultSchema } from "../../../src/domain/artifactGraph.js"; -import { createHash } from "node:crypto"; import { mkdir, readFile, diff --git a/tests/boundary/filesystem/asarStreamingAllocation.test.ts b/tests/boundary/filesystem/asarStreamingAllocation.test.ts index 9eee454386..433a81302a 100644 --- a/tests/boundary/filesystem/asarStreamingAllocation.test.ts +++ b/tests/boundary/filesystem/asarStreamingAllocation.test.ts @@ -840,6 +840,5 @@ describe("ASAR entry streaming", () => { await reader.close(); } }, - 120_000, ); }); diff --git a/tests/boundary/filesystem/binaryTarget.test.ts b/tests/boundary/filesystem/binaryTarget.test.ts index 1797e34900..ebf2df30b3 100644 --- a/tests/boundary/filesystem/binaryTarget.test.ts +++ b/tests/boundary/filesystem/binaryTarget.test.ts @@ -17,10 +17,7 @@ import { describe, expect, it, vi } from "vitest"; import { parseBinaryTarget } from "../../../src/application/BinaryTargetResolver.js"; import { espFlashSelectionError } from "../../../src/domain/firmware/espFlashSelection.js"; -import { - resolveAppBundleExecutable, - type AppBundleFileSystem, -} from "../../../src/application/AppBundleExecutable.js"; +import { resolveAppleBundleExecutable } from "../../../src/application/AppBundleExecutable.js"; import { dosMz, elf, @@ -355,13 +352,15 @@ describe("binary target I/O: app plist permissions and decoding", () => { await mkdir(join(app, "Contents"), { recursive: true }); await writeFile(join(app, "Contents", "Info.plist"), "placeholder"); const denied = Object.assign(new Error("denied"), { code }); - const fileSystem: AppBundleFileSystem = { + const fileSystem: NonNullable< + Parameters[1] + > = { readFile: async () => { throw denied; }, realpath, }; - const result = await resolveAppBundleExecutable(app, fileSystem); + const result = await resolveAppleBundleExecutable(app, fileSystem); expect(result).toMatchObject({ ok: false, error: { @@ -380,7 +379,7 @@ describe("binary target I/O: app plist permissions and decoding", () => { await mkdir(join(app, "Contents"), { recursive: true }); await writeFile(join(app, "Contents", "Info.plist"), "placeholder"); const readError = Object.assign(new Error("I/O failure"), { code: "EIO" }); - const result = await resolveAppBundleExecutable(app, { + const result = await resolveAppleBundleExecutable(app, { readFile: async () => { throw readError; }, @@ -408,14 +407,16 @@ describe("binary target I/O: app plist permissions and decoding", () => { "CFBundleExecutableApp", ); const denied = Object.assign(new Error("denied"), { code }); - const fileSystem: AppBundleFileSystem = { + const fileSystem: NonNullable< + Parameters[1] + > = { readFile, realpath: async (path) => { if (String(path) === join(programs, "App")) throw denied; return realpath(path); }, }; - const result = await resolveAppBundleExecutable(app, fileSystem); + const result = await resolveAppleBundleExecutable(app, fileSystem); expect(result).toMatchObject({ ok: false, error: { @@ -436,7 +437,7 @@ describe("binary target I/O: app plist permissions and decoding", () => { const directory = await createTestTempDirectory("rea-app-diagnostics-"); const missing = join(directory, "Missing.app"); await mkdir(join(missing, "Contents"), { recursive: true }); - const missingResult = await resolveAppBundleExecutable(missing); + const missingResult = await resolveAppleBundleExecutable(missing); expect(missingResult).toMatchObject({ ok: false, error: { reason: expect.stringContaining("missing") }, @@ -445,7 +446,7 @@ describe("binary target I/O: app plist permissions and decoding", () => { const notDirectory = join(directory, "NotDirectory.app"); await mkdir(notDirectory, { recursive: true }); await writeFile(join(notDirectory, "Contents"), "not a directory"); - const notDirectoryResult = await resolveAppBundleExecutable(notDirectory); + const notDirectoryResult = await resolveAppleBundleExecutable(notDirectory); expect(notDirectoryResult).toMatchObject({ ok: false, error: { reason: expect.stringContaining("missing") }, @@ -457,7 +458,7 @@ describe("binary target I/O: app plist permissions and decoding", () => { join(malformed, "Contents", "Info.plist"), "", ); - const malformedResult = await resolveAppBundleExecutable(malformed); + const malformedResult = await resolveAppleBundleExecutable(malformed); expect(malformedResult).toMatchObject({ ok: false, error: { @@ -484,7 +485,7 @@ describe("binary target I/O: OpenStep app plist escapes", () => { await mkdir(programs, { recursive: true }); await writeFile(join(app, "Contents", "Info.plist"), plistContent); await writeFile(join(programs, "Main"), "binary"); - const result = await resolveAppBundleExecutable(app); + const result = await resolveAppleBundleExecutable(app); expect(result).toEqual({ ok: true, value: { @@ -1553,7 +1554,7 @@ describe("binary Info.plist app bundle targets", () => { join(app, "Contents", "MacOS", "Forged"), thinMach(0xfeedfacf, 0x0100000c), ); - expect(await resolveAppBundleExecutable(app)).toMatchObject({ + expect(await resolveAppleBundleExecutable(app)).toMatchObject({ ok: false, error: { reason: expect.stringContaining("CFBundleExecutable is missing"), diff --git a/tests/boundary/filesystem/browserFixtureRequestFailures.test.ts b/tests/boundary/filesystem/browserFixtureRequestFailures.test.ts index bfe54017ce..ff2e8ea91e 100644 --- a/tests/boundary/filesystem/browserFixtureRequestFailures.test.ts +++ b/tests/boundary/filesystem/browserFixtureRequestFailures.test.ts @@ -53,7 +53,6 @@ for (const [fixture, factory, ordinaryPath, malformedPath] of [ ["--input-type=module", "-e", program], { cwd: fileURLToPath(new URL("../../../", import.meta.url)), - timeoutMs: 10_000, }, ); expect(result.signal, result.stderr).toBeNull(); diff --git a/tests/boundary/filesystem/bunStandaloneIntegration.test.ts b/tests/boundary/filesystem/bunStandaloneIntegration.test.ts index 367bc9d918..de2d4ba2f4 100644 --- a/tests/boundary/filesystem/bunStandaloneIntegration.test.ts +++ b/tests/boundary/filesystem/bunStandaloneIntegration.test.ts @@ -6,7 +6,6 @@ import { setImmediate } from "node:timers/promises"; import { expect, it, vi } from "vitest"; import { BunStandaloneArtifactReader } from "../../../src/artifacts/BunStandaloneArtifactReader.js"; import { ArtifactResourceScope } from "../../../src/artifacts/ArtifactResourceScope.js"; -import * as layout from "../../../src/artifacts/bun/BunModuleGraphLayout.js"; import { decodeBunModuleGraph } from "../../../src/artifacts/bun/BunModuleGraphMembers.js"; import { locateBunGraphFixture, diff --git a/tests/boundary/filesystem/ciTestProjects.test.ts b/tests/boundary/filesystem/ciTestProjects.test.ts index 0e6c0baec2..d980ac215e 100644 --- a/tests/boundary/filesystem/ciTestProjects.test.ts +++ b/tests/boundary/filesystem/ciTestProjects.test.ts @@ -1,5 +1,6 @@ import { execFileSync } from "node:child_process"; import { mkdir, writeFile } from "node:fs/promises"; +import { availableParallelism } from "node:os"; import { join } from "node:path"; import { pathToFileURL } from "node:url"; @@ -161,45 +162,60 @@ describe("Vitest project configuration", () => { ); }); - it.each([ - { flags: [], timeout: 30_000, lines: 68 }, - { flags: ["--coverage"], timeout: 60_000, lines: 68 }, - { flags: ["--coverage.enabled"], timeout: 60_000, lines: 68 }, - { flags: ["--coverage=false"], timeout: 30_000, lines: 68 }, - { - flags: ["--coverage.enabled=false"], - timeout: 30_000, - lines: 68, - }, - { - flags: ["--coverage.provider=v8"], - timeout: 30_000, - lines: 68, - }, - { - flags: ["--coverage.enabled", "--shard", "1/6"], - timeout: 60_000, - lines: 0, - }, - { - flags: ["--coverage.enabled=false", "--shard=1/6"], - timeout: 30_000, - lines: 0, - }, - ])( - "resolves worker resources from actual CLI options: $flags", - async ({ flags, timeout, lines }) => { + it.each( + [ + { flags: [], timeout: 30_000, lines: 68 }, + { flags: ["--coverage"], timeout: 60_000, lines: 68 }, + { flags: ["--coverage.enabled"], timeout: 60_000, lines: 68 }, + { flags: ["--coverage=false"], timeout: 30_000, lines: 68 }, + { + flags: ["--coverage.enabled=false"], + timeout: 30_000, + lines: 68, + }, + { + flags: ["--coverage.provider=v8"], + timeout: 30_000, + lines: 68, + }, + { + flags: ["--coverage.enabled", "--shard", "1/6"], + timeout: 60_000, + lines: 0, + }, + { + flags: ["--coverage.enabled=false", "--shard=1/6"], + timeout: 30_000, + lines: 0, + }, + ].flatMap((policy) => + // Hosted CI owns a four-core runner: up to four workers, doubled deadlines. + [ + { ...policy, hosted: false, workers: 2, scale: 1 }, + { ...policy, hosted: true, workers: 4, scale: 2 }, + ], + ), + )( + "resolves worker resources from actual CLI options: $flags (hosted: $hosted)", + async ({ flags, timeout, lines, hosted, workers, scale }) => { const root = await createTestTempDirectory("rea-vitest-worker-policy-"); const script = join(root, "worker-policy.mjs"); await writeFile( script, ` import config from ${JSON.stringify(pathToFileURL(join(repository, "vitest.config.ts")).href)}; - const project = config.test.projects.find(project => project.test.name === 'cli-boundary'); + const project = (name) => config.test.projects.find(project => project.test.name === name).test; + const deadlines = (name) => [project(name).testTimeout, project(name).hookTimeout]; console.log(JSON.stringify({ - execArgv: project.test.execArgv, - timeout: config.test.testTimeout, + execArgv: project('cli-boundary').execArgv, + deadlines: { + default: [config.test.testTimeout, config.test.hookTimeout], + source: deadlines('domain'), + built: deadlines('cli-boundary'), + longRunning: config.test.tags.find(tag => tag.name === 'long-running').timeout, + }, lines: config.test.coverage.thresholds.lines, + workers: config.test.maxWorkers, })); `, ); @@ -208,15 +224,26 @@ describe("Vitest project configuration", () => { ["--import", "tsx", script, ...flags], { cwd: repository, + // Select the runner explicitly; CI itself sets GITHUB_ACTIONS. + env: { ...process.env, GITHUB_ACTIONS: hosted ? "true" : "" }, timeout: 10_000, maxBuffer: 64 * 1024, stopSignal: "SIGTERM", }, ); + // Deadlines are hang detection owned by the project: the source + // deadline scales with coverage and hosted runners, built projects allow + // four times it, and the long-running tag ten times. expect(JSON.parse(result.stdout)).toEqual({ execArgv: ["--max-old-space-size=512"], - timeout, + deadlines: { + default: [timeout * scale, timeout * scale], + source: [timeout * scale, timeout * scale], + built: [4 * timeout * scale, 4 * timeout * scale], + longRunning: 10 * timeout * scale, + }, lines, + workers: Math.min(workers, availableParallelism()), }); }, ); diff --git a/tests/boundary/filesystem/ciTestSequencer.test.ts b/tests/boundary/filesystem/ciTestSequencer.test.ts index 7705d181d5..452bfae0e4 100644 --- a/tests/boundary/filesystem/ciTestSequencer.test.ts +++ b/tests/boundary/filesystem/ciTestSequencer.test.ts @@ -11,7 +11,6 @@ import { parseTestDurations, readTestDurations, scheduleWall, - seedLoaderOwnershipDurations, } from "../../ciTestSequencer.js"; describe("CI test history", () => { @@ -86,72 +85,6 @@ describe("CI test history", () => { }); }); -describe("loader ownership history", () => { - const source = - "tests/acceptance/applications/javascriptLiteralPercentPaths.test.ts"; - const consolidated = - "tests/acceptance/applications/javascriptLiteralLoaderOwnership.test.ts"; - const parts = [ - "tests/acceptance/applications/javascriptLiteralLoaderOwnershipEarly.test.ts", - "tests/acceptance/applications/javascriptLiteralLoaderOwnershipMiddle.test.ts", - "tests/acceptance/applications/javascriptLiteralLoaderOwnershipLate.test.ts", - ]; - - it("assigns the pre-split loader share to the consolidated file", () => { - const seeded = seedLoaderOwnershipDurations( - new Map([ - [source, 192_000], - ["tests/other.test.ts", 10], - ]), - ); - expect(seeded.get(source)).toBe(54_000); - expect(seeded.get(consolidated)).toBe(138_000); - for (const path of parts) expect(seeded.has(path)).toBe(false); - expect(seeded.get("tests/other.test.ts")).toBe(10); - expect( - [...seeded.values()].reduce((sum, duration) => sum + duration, 0), - ).toBe(192_000 + 10); - }); - - it("sums retired shard durations onto the consolidated file", () => { - const seeded = seedLoaderOwnershipDurations( - new Map([ - [source, 20_000], - [parts[0]!, 30_000], - [parts[1]!, 40_000], - [parts[2]!, 50_000], - ["tests/other.test.ts", 10], - ]), - ); - expect(seeded.get(source)).toBe(20_000); - expect(seeded.get(consolidated)).toBe(120_000); - expect(seeded.get("tests/other.test.ts")).toBe(10); - }); - - it("scales a partial retired shard as three equal shares", () => { - const seeded = seedLoaderOwnershipDurations( - new Map([ - [source, 20_000], - [parts[0]!, 30_000], - ]), - ); - expect(seeded.get(source)).toBe(20_000); - expect(seeded.get(consolidated)).toBe(90_000); - }); - - it("keeps a published consolidated duration", () => { - const recorded = new Map([ - [source, 20_000], - [parts[0]!, 30_000], - [consolidated, 12_000], - ]); - expect(seedLoaderOwnershipDurations(recorded)).toEqual(recorded); - expect( - seedLoaderOwnershipDurations(new Map([["tests/other.test.ts", 10]])), - ).toEqual(new Map([["tests/other.test.ts", 10]])); - }); -}); - describe("duration shard allocation", () => { it.prop([ fc.uniqueArray(fc.integer({ min: 0, max: 10_000 }), { diff --git a/tests/boundary/filesystem/claudeDesktopClientConfiguration.test.ts b/tests/boundary/filesystem/claudeDesktopClientConfiguration.test.ts index 898cc3ff7f..7eccca93b2 100644 --- a/tests/boundary/filesystem/claudeDesktopClientConfiguration.test.ts +++ b/tests/boundary/filesystem/claudeDesktopClientConfiguration.test.ts @@ -13,7 +13,7 @@ import { dirname, join } from "node:path"; import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; -import { CLAUDE_DESKTOP_PACKAGE_FAMILY } from "../../../src/application/ClaudeDesktopConfigLocation.js"; +import { CLAUDE_DESKTOP_PACKAGE_FAMILY } from "../../fixtures/claudeDesktop.js"; import { readClientRegistrationStatuses } from "../../../src/application/ClientRegistrationStatus.js"; import { diff --git a/tests/boundary/filesystem/claudeDesktopSkillUninstallWindows.test.ts b/tests/boundary/filesystem/claudeDesktopSkillUninstallWindows.test.ts index d64dc857a5..c7b6caab1d 100644 --- a/tests/boundary/filesystem/claudeDesktopSkillUninstallWindows.test.ts +++ b/tests/boundary/filesystem/claudeDesktopSkillUninstallWindows.test.ts @@ -1,7 +1,7 @@ import { access, mkdir, readFile, symlink, writeFile } from "node:fs/promises"; import { dirname, join } from "node:path"; import { afterEach, beforeEach, expect, it, vi } from "vitest"; -import { CLAUDE_DESKTOP_PACKAGE_FAMILY } from "../../../src/application/ClaudeDesktopConfigLocation.js"; +import { CLAUDE_DESKTOP_PACKAGE_FAMILY } from "../../fixtures/claudeDesktop.js"; import { runUninstall, systemUninstallHost, diff --git a/tests/boundary/filesystem/cliJsonInput.test.ts b/tests/boundary/filesystem/cliJsonInput.test.ts index a4eec98606..b569757680 100644 --- a/tests/boundary/filesystem/cliJsonInput.test.ts +++ b/tests/boundary/filesystem/cliJsonInput.test.ts @@ -420,10 +420,9 @@ describe("CLI JSON standard input", () => { }); it("rejects oversized stdin with too-large", async () => { - const stream = Readable.from(['{"a":"' + "x".repeat(100) + '"}']); + const stream = Readable.from([Buffer.alloc(8 * 1024 * 1024 + 1, 0x78)]); const result = await parseCliJsonInput("-", "test-input", undefined, { stdin: stream, - maxStdinBytes: 50, }); expect(result).toMatchObject({ ok: false, @@ -491,7 +490,6 @@ describe("CLI JSON stdin cancellation and encoding", () => { expect(stream.destroyed).toBe(true); expect(getEventListeners(controller.signal, "abort")).toHaveLength(0); }, - 1_000, ); it.each([ @@ -522,13 +520,24 @@ describe("CLI JSON stdin cancellation and encoding", () => { ); it("reads UTF-8 split between chunks and accepts the exact byte limit", async () => { - const bytes = Buffer.from('{"label":"中文🦀"}'); - expect( - await parseCliJsonInput("-", "test-input", undefined, { - stdin: Readable.from([...bytes].map((byte) => Buffer.from([byte]))), - maxStdinBytes: bytes.length, - }), - ).toEqual({ ok: true, value: { label: "中文🦀" } }); + const prefix = Buffer.from('{"label":"'); + const suffix = Buffer.from('中文🦀"}'); + const padding = Buffer.alloc( + 8 * 1024 * 1024 - prefix.length - suffix.length, + 0x61, + ); + const result = await parseCliJsonInput("-", "test-input", undefined, { + stdin: Readable.from([ + prefix, + padding, + ...[...suffix].map((byte) => Buffer.from([byte])), + ]), + }); + expect(result.ok).toBe(true); + if (!result.ok) throw new Error("Expected valid JSON at the stdin limit"); + expect(result.value).toEqual({ + label: `${"a".repeat(padding.length)}中文🦀`, + }); }); it("projects cancellation during stdin snapshot sealing", async () => { diff --git a/tests/boundary/filesystem/clientLifecycle.test.ts b/tests/boundary/filesystem/clientLifecycle.test.ts index 25b150612c..b99bf1be52 100644 --- a/tests/boundary/filesystem/clientLifecycle.test.ts +++ b/tests/boundary/filesystem/clientLifecycle.test.ts @@ -48,6 +48,8 @@ describe("client configuration filesystem lifecycle", () => { for (const client of supportedClients(home)) if (client.markerPath !== undefined) await mkdir(client.markerPath, { recursive: true }); + // Gemini CLI follows its own session state, not the shared .gemini root. + await mkdir(join(home, ".gemini", "tmp"), { recursive: true }); const detected = await detectClients(home); expect(detected.map(({ name }) => name)).toEqual([ "claude_code", @@ -88,6 +90,21 @@ describe("client configuration filesystem lifecycle", () => { expect(await detectClients(emptyHome)).toEqual([]); }); + it("does not detect Gemini CLI from Antigravity's shared .gemini storage", async () => { + const home = await createTestTempDirectory("rea-detect-gemini-"); + const antigravity = supportedClients(home).find( + ({ name }) => name === "antigravity", + ); + if (antigravity?.markerPath === undefined) + throw new Error("Antigravity marker is missing"); + await mkdir(antigravity.markerPath, { recursive: true }); + const names = async () => + (await detectClients(home)).map(({ name }) => name); + expect(await names()).toEqual(["antigravity"]); + await writeFile(join(home, ".gemini", "settings.json"), "{}\n"); + expect(await names()).toEqual(["gemini_cli", "antigravity"]); + }); + it("preserves unrelated Codex TOML and supports an installed command", async () => { const home = await createTestTempDirectory("rea-toml-"); const configPath = join(home, ".codex/config.toml"); diff --git a/tests/boundary/filesystem/clientNativeConnectionAdmission.test.ts b/tests/boundary/filesystem/clientNativeConnectionAdmission.test.ts index bc13078a2f..40a4f8672d 100644 --- a/tests/boundary/filesystem/clientNativeConnectionAdmission.test.ts +++ b/tests/boundary/filesystem/clientNativeConnectionAdmission.test.ts @@ -116,7 +116,6 @@ it("keeps owned Gemini launcher facts separate from native connection admission process.execPath, ["--import", "tsx", entrypoint], { - timeoutMs: 20_000, env: { PATH: process.env.PATH, SystemRoot: process.env.SystemRoot, diff --git a/tests/boundary/filesystem/composedWorkflowEvidenceLinks.test.ts b/tests/boundary/filesystem/composedWorkflowEvidenceLinks.test.ts index 2fc5678cdd..437086ab55 100644 --- a/tests/boundary/filesystem/composedWorkflowEvidenceLinks.test.ts +++ b/tests/boundary/filesystem/composedWorkflowEvidenceLinks.test.ts @@ -3,6 +3,7 @@ import { executeComposedWorkflow } from "../../../src/application/WorkflowEviden import { parseAnalysisSnapshot } from "../../../src/domain/analysisSnapshot.js"; import { ok } from "../../../src/domain/result.js"; import { createDeferred } from "../../fixtures/binarySession.js"; +import { awaitAcknowledgement } from "../../fixtures/lifecycleAcknowledgement.js"; import { expect, it } from "vitest"; import { runDirectAnalysis } from "../../../src/application/DirectAnalysis.js"; import { createEvidence, parseEvidence } from "../../../src/domain/evidence.js"; @@ -200,8 +201,8 @@ it("deduplicates repeated and nested consumed records without linking unrelated return nested.ok ? ok(nested.value.result) : nested; }, ); - await entered.promise; try { + await awaitAcknowledgement(entered.promise, overview, "summary execution"); const other = await executeComposedWorkflow( fixture.session, async (observed) => { diff --git a/tests/boundary/filesystem/denoStandaloneIntegration.test.ts b/tests/boundary/filesystem/denoStandaloneIntegration.test.ts index 35ed28746a..a15741b5e9 100644 --- a/tests/boundary/filesystem/denoStandaloneIntegration.test.ts +++ b/tests/boundary/filesystem/denoStandaloneIntegration.test.ts @@ -13,12 +13,12 @@ import { denoElf, denoIntelTrailer, } from "../../../src/artifacts/deno/DenoStandalone.fixture.js"; -import { MAX_DENO_PATH_COMPONENT_BYTES } from "../../../src/artifacts/deno/DenoVirtualFs.js"; import { extractArtifact } from "../../../src/artifacts/extraction/ArtifactExtraction.js"; import { SafeOutputTree } from "../../../src/artifacts/SafeOutputTree.js"; import { scanCanonicalArtifactInventory } from "../../fixtures/artifactInventory.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; +const MAX_DENO_PATH_COMPONENT_BYTES = 255; const source = "console.log('deno')\n"; const asset = "hello-asset\n"; diff --git a/tests/boundary/filesystem/dependencyInstall.test.ts b/tests/boundary/filesystem/dependencyInstall.test.ts index 2dd9a542df..3132de1a6d 100644 --- a/tests/boundary/filesystem/dependencyInstall.test.ts +++ b/tests/boundary/filesystem/dependencyInstall.test.ts @@ -140,7 +140,7 @@ describe("dependency install freshness", () => { const cached = await runTurbo(root); expect(cached.status, cached.stderr).toBe(0); expect(`${cached.stdout}${cached.stderr}`).toMatch(/cache hit/u); - }, 60_000); + }); it("preserves caller resource controls in strict cached builds without hashing their values", async () => { const root = await createBuildCacheFixture(); @@ -173,7 +173,7 @@ describe("dependency install freshness", () => { expect(cached.status, `${cached.stdout}${cached.stderr}`).toBe(0); expect(`${cached.stdout}${cached.stderr}`).toMatch(/cache hit/u); expect(`${cached.stdout}${cached.stderr}`).not.toMatch(/cache miss/u); - }, 60_000); + }); }); const createBuildCacheFixture = async (): Promise => { diff --git a/tests/boundary/filesystem/desktopApplication.test.ts b/tests/boundary/filesystem/desktopApplication.test.ts index d497b4ce2a..7287015569 100644 --- a/tests/boundary/filesystem/desktopApplication.test.ts +++ b/tests/boundary/filesystem/desktopApplication.test.ts @@ -12,6 +12,7 @@ import { execFileSync } from "node:child_process"; import { createServer as createNetServer } from "node:net"; import { expect, it, onTestFinished, vi } from "vitest"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; +import { awaitAcknowledgement } from "../../fixtures/lifecycleAcknowledgement.js"; import { createDesktopApplicationService } from "../../../src/composition/desktop.js"; import { desktopApplicationSchema, @@ -778,7 +779,7 @@ it("bounds simultaneous directory scans without cancelling the admitted caller", ); const first = service.inspect({ path: root }); try { - await started; + await awaitAcknowledgement(started, first, "desktop directory scan"); const second = await service.inspect({ path: root }); expect(second.ok).toBe(false); if (!second.ok) @@ -787,15 +788,20 @@ it("bounds simultaneous directory scans without cancelling the admitted caller", resource: "capacity", retryable: true, }); + release(); + const result = await first; + if (!result.ok) throw result.error; + expect( + desktopApplicationSchema.parse(result.value.normalized_result).files, + ).toHaveLength(1); } finally { release(); + try { + await first; + } finally { + await service.close(); + } } - const result = await first; - if (!result.ok) throw result.error; - expect( - desktopApplicationSchema.parse(result.value.normalized_result).files, - ).toHaveLength(1); - await service.close(); }); it("keeps directory-only marker identities separate across installation roots", async () => { diff --git a/tests/boundary/filesystem/directAnalysisSnapshotReplay.test.ts b/tests/boundary/filesystem/directAnalysisSnapshotReplay.test.ts index afe2666d16..1130072523 100644 --- a/tests/boundary/filesystem/directAnalysisSnapshotReplay.test.ts +++ b/tests/boundary/filesystem/directAnalysisSnapshotReplay.test.ts @@ -1,5 +1,8 @@ import { withVerifierCleanup } from "../../../scripts/lib/verifier-cleanup.mjs"; -import { parseEvidenceBundle } from "../../../src/domain/evidenceBundle.js"; +import { + parseEvidenceBundle, + createEvidenceBundle, +} from "../../../src/domain/evidenceBundle.js"; import { EvidenceIntegrityError } from "../../../src/domain/evidenceErrors.js"; import { ProviderCleanupError } from "../../../src/domain/providerCleanupError.js"; import { AnalysisCancelledError } from "../../../src/domain/analysisErrorCore.js"; @@ -15,14 +18,14 @@ import { describe, expect, it, vi } from "vitest"; import { z } from "zod"; import { runDirectAnalysis } from "../../../src/application/DirectAnalysis.js"; -import type { - AnalysisProvider, - CapabilityDescriptor, - ProviderIdentity, +import { + createAnalysisExecution, + type AnalysisProvider, + type CapabilityDescriptor, + type ProviderIdentity, } from "../../../src/application/AnalysisProvider.js"; import type { DirectAnalysisDependencies } from "../../../src/application/DirectAnalysisDependencies.js"; import { createAnalysisProfile } from "../../../src/domain/analysisProfile.js"; -import { createAnalysisExecution } from "../../../src/application/AnalysisProvider.js"; import { err, ok } from "../../../src/domain/result.js"; import { readAnalysisSnapshot, @@ -33,7 +36,6 @@ import { parseEvidence, type Evidence, } from "../../../src/domain/evidence.js"; -import { createEvidenceBundle } from "../../../src/domain/evidenceBundle.js"; import { REA_WORKFLOW_PROVIDER } from "../../../src/domain/investigationProviders.js"; import { workflowAnalysisProfile } from "../../../src/application/WorkflowAnalysisProfile.js"; import { diff --git a/tests/boundary/filesystem/doctorSystemHostFilesystem.test.ts b/tests/boundary/filesystem/doctorSystemHostFilesystem.test.ts index c69db398e8..fe3eb09509 100644 --- a/tests/boundary/filesystem/doctorSystemHostFilesystem.test.ts +++ b/tests/boundary/filesystem/doctorSystemHostFilesystem.test.ts @@ -59,7 +59,7 @@ it("threads the selected host and environment into system diagnostics", async () expect(host.homeDirectory).toBe("/selected/home"); expect(host.platform).toBe("linux"); expect(host.architecture).toBe("x64"); - await host.installationPaths?.(); + await host.installationLaunchers?.(); expect(await host.macosVersion()).toBe("15.0"); await host.executable(process.execPath); @@ -75,7 +75,7 @@ it("threads the selected host and environment into system diagnostics", async () it.each([ "macosVersion", - "installationPaths", + "installationLaunchers", "brewHopperPath", "executable", "ilspyCmdVersion", @@ -124,7 +124,7 @@ it.each(["complete", "failed"] as const)( }, }); try { - const pending = host.installationPaths?.(); + const pending = host.installationLaunchers?.(); if (outcome === "failed") await expect(pending).resolves.toEqual([]); else await expect(pending).rejects.toMatchObject({ @@ -169,7 +169,7 @@ fs.writeFileSync(${JSON.stringify(started)}, "ready"); environment: { HOME: home, PATH: home }, signal: controller.signal, }); - const pending = host.installationPaths!().then( + const pending = host.installationLaunchers!().then( (value) => value, (cause: unknown) => cause, ); @@ -207,7 +207,7 @@ it.skipIf(process.platform === "win32")( signal: controller.signal, }); try { - await expect(host.installationPaths?.()).rejects.toMatchObject({ + await expect(host.installationLaunchers?.()).rejects.toMatchObject({ _tag: "AnalysisCancelledError", cause: reason, }); @@ -337,3 +337,70 @@ it("reports no Homebrew Hopper when the cask has no receipt", async () => { new Set(["--caskroom"]), ); }); + +it("groups the Windows npm shims `where` lists under one installation", async () => { + const npm = "C:\\Users\\user\\AppData\\Roaming\\npm"; + const other = "D:\\tools\\rea"; + const listed = [ + `${npm}\\rea`, + `${npm}\\rea.cmd`, + `${npm}\\REA.ps1`, + `${other}\\rea.cmd`, + ]; + const host = systemDoctorHost({ + platform: "win32", + architecture: "x64", + environment: { PATH: "C:\\Windows" }, + execFileOutput: async (command) => { + if (command !== "where") + throw new Error(`Unexpected diagnostic command: ${command}`); + const stdout = `${listed.join("\r\n")}\r\n`; + return { + stdout, + stderr: "", + stdoutBytes: Buffer.byteLength(stdout), + stderrBytes: 0, + }; + }, + }); + const launchers = (await host.installationLaunchers?.()) ?? []; + // Every listed launcher stays visible; identity groups them by directory. + expect(launchers.map(({ path }) => path)).toEqual(listed); + expect(new Set(launchers.map(({ installation }) => installation)).size).toBe( + 2, + ); +}); + +it.skipIf(process.platform === "win32")( + "resolves POSIX launchers that reach one package through symlinks", + async () => { + const root = await createTestTempDirectory("rea-doctor-launchers-"); + const scripts = join(root, "lib/node_modules/rea-agents/scripts"); + await mkdir(scripts, { recursive: true }); + await writeFile(join(scripts, "rea.mjs"), "", { mode: 0o755 }); + const launchers = [join(root, "bin/rea"), join(root, "home/bin/rea")]; + for (const launcher of launchers) { + await mkdir(dirname(launcher), { recursive: true }); + await symlink(join(scripts, "rea.mjs"), launcher); + } + const host = systemDoctorHost({ + platform: "linux", + architecture: "x64", + environment: { PATH: "/selected/bin" }, + execFileOutput: async () => { + const stdout = `${launchers.join("\n")}\n`; + return { + stdout, + stderr: "", + stdoutBytes: Buffer.byteLength(stdout), + stderrBytes: 0, + }; + }, + }); + const resolved = (await host.installationLaunchers?.()) ?? []; + expect(resolved.map(({ path }) => path)).toEqual(launchers); + expect(new Set(resolved.map(({ installation }) => installation)).size).toBe( + 1, + ); + }, +); diff --git a/tests/boundary/filesystem/dotnetBundleArtifacts.test.ts b/tests/boundary/filesystem/dotnetBundleArtifacts.test.ts index f3e532da53..80ea12f33a 100644 --- a/tests/boundary/filesystem/dotnetBundleArtifacts.test.ts +++ b/tests/boundary/filesystem/dotnetBundleArtifacts.test.ts @@ -24,6 +24,31 @@ import { } from "../../../src/artifacts/dotnet/DotnetBundle.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; +it.each(["close", "cancel"] as const)( + "does not complete a .NET bundle inventory after %s at the last entry", + async (interruption) => { + const root = await createTestTempDirectory("rea-dotnet-paused-"); + const path = join(root, "app"); + await writeFile( + path, + dotnetBundleFixture([{ path: "member", bytes: Buffer.from("x") }]).bytes, + ); + const reader = new DotnetBundleArtifactReader(path); + const controller = new AbortController(); + try { + const entries = reader.entries(controller.signal)[Symbol.asyncIterator](); + expect((await entries.next()).done).toBe(false); + if (interruption === "close") await reader.close(); + else controller.abort(); + await expect(entries.next()).rejects.toMatchObject({ + reason: interruption === "close" ? "unavailable" : "cancelled", + }); + } finally { + await reader.close(); + } + }, +); + it.each([ { field: "entry_count", maximum: 1, required: 2 }, { field: "string_bytes", maximum: 13, required: 14 }, diff --git a/tests/boundary/filesystem/evidenceBundleFiles.test.ts b/tests/boundary/filesystem/evidenceBundleFiles.test.ts index afbc43315d..c62039f54e 100644 --- a/tests/boundary/filesystem/evidenceBundleFiles.test.ts +++ b/tests/boundary/filesystem/evidenceBundleFiles.test.ts @@ -46,7 +46,6 @@ import { createEvidenceBundle, parseEvidenceBundle, serializeEvidenceBundle, - DuplicateEvidenceRecordError, isImmutableEvidenceBundle, } from "../../../src/domain/evidenceBundle.js"; import { analysisViewLayoutEvidence } from "../../fixtures/analysisView.js"; @@ -247,7 +246,10 @@ it("preserves direct duplicate rejection and CLI identity merging for repeated i ); await expect( deliverAnalysisEvidence(complete, join(root, "direct.json")), - ).rejects.toBeInstanceOf(DuplicateEvidenceRecordError); + ).rejects.toMatchObject({ + name: "DuplicateEvidenceRecordError", + message: "Evidence bundle contains duplicate record IDs", + }); const path = join(root, "cli.json"); const delivered = await publishCliAnalysisEvidence( { ok: true, value: complete }, diff --git a/tests/boundary/filesystem/flutterBuildProvider.test.ts b/tests/boundary/filesystem/flutterBuildProvider.test.ts index 50c521f97e..bdb9f94396 100644 --- a/tests/boundary/filesystem/flutterBuildProvider.test.ts +++ b/tests/boundary/filesystem/flutterBuildProvider.test.ts @@ -3,7 +3,6 @@ import { createReadStream } from "node:fs"; import { open, readFile, - rm, rmdir, stat, utimes, @@ -44,6 +43,10 @@ import { import * as regularFile from "../../../src/filesystem/RegularFile.js"; import * as fsPromises from "node:fs/promises"; import * as dartElf from "../../../src/flutter/DartElf.js"; +import { + interceptFlutterRootRemoval, + realRm, +} from "../../fixtures/flutterCleanup.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; vi.mock("node:fs", async (importOriginal) => { @@ -523,12 +526,11 @@ it.each(["close", "elf", "pool", "zip-library", "cleanup"] as const)( onTestFinished(() => { vi.restoreAllMocks(); }); - const provider = new FlutterBuildProvider({ - removeRoot: async (root) => { - await rm(root, { recursive: true, force: true }); - if (stage === "cleanup") controller.abort(reason); - }, + interceptFlutterRootRemoval(async (root) => { + await realRm(root, { recursive: true, force: true }); + if (stage === "cleanup") controller.abort(reason); }); + const provider = new FlutterBuildProvider(); onTestFinished(() => provider.close()); const result = await provider.execute( { operation: "inspect_dart_aot", input: { path } }, @@ -559,13 +561,12 @@ it.each(["identify_flutter_build", "inspect_dart_aot"] as const)( const controller = new AbortController(); const reason = new Error("caller interrupted snapshot removal"); const removedRoots: string[] = []; - const provider = new FlutterBuildProvider({ - removeRoot: async (root) => { - await rm(root, { recursive: true, force: true }); - removedRoots.push(root); - controller.abort(reason); - }, + interceptFlutterRootRemoval(async (root) => { + await realRm(root, { recursive: true, force: true }); + removedRoots.push(root); + controller.abort(reason); }); + const provider = new FlutterBuildProvider(); const result = await new FlutterBuildAnalysisService(provider).execute( operation, { path }, @@ -834,13 +835,12 @@ it("retains failed snapshot cleanup and retries it on close", async () => { ]); const roots: string[] = []; let failing = true; - const provider = new FlutterBuildProvider({ - removeRoot: async (path) => { - roots.push(path); - if (failing) throw new Error("cleanup refused"); - await rm(path, { recursive: true, force: true }); - }, + interceptFlutterRootRemoval(async (path) => { + roots.push(path); + if (failing) throw new Error("cleanup refused"); + await realRm(path, { recursive: true, force: true }); }); + const provider = new FlutterBuildProvider(); onTestFinished(async () => { failing = false; await provider.close(); @@ -1142,12 +1142,11 @@ it.each([ streamMock.mockReset(); }); const removedRoots: string[] = []; - const provider = new FlutterBuildProvider({ - removeRoot: async (root) => { - await rm(root, { recursive: true, force: true }); - removedRoots.push(root); - }, + interceptFlutterRootRemoval(async (root) => { + await realRm(root, { recursive: true, force: true }); + removedRoots.push(root); }); + const provider = new FlutterBuildProvider(); onTestFinished(() => provider.close()); const result = await provider.execute( { operation, input: { path } }, @@ -1301,9 +1300,10 @@ it.each(["identify_flutter_build", "inspect_dart_aot"] as const)( await originalClose.call(this); }); const removeRoot = vi.fn((root: string) => - rm(root, { recursive: true, force: true }), + realRm(root, { recursive: true, force: true }), ); - const provider = new FlutterBuildProvider({ removeRoot }); + interceptFlutterRootRemoval(removeRoot); + const provider = new FlutterBuildProvider(); onTestFinished(async () => { failClose = false; await provider.close(); @@ -1418,9 +1418,10 @@ it.each([0, 1, 2, 3])( await originalClose.call(this); }); const removeRoot = vi.fn((root: string) => - rm(root, { recursive: true, force: true }), + realRm(root, { recursive: true, force: true }), ); - const provider = new FlutterBuildProvider({ removeRoot }); + interceptFlutterRootRemoval(removeRoot); + const provider = new FlutterBuildProvider(); onTestFinished(async () => { failing = false; await provider.close(); @@ -1732,9 +1733,10 @@ it.each([ }); onTestFinished(() => admitted.mockRestore()); const removeRoot = vi.fn((root: string) => - rm(root, { recursive: true, force: true }), + realRm(root, { recursive: true, force: true }), ); - const provider = new FlutterBuildProvider({ removeRoot }); + interceptFlutterRootRemoval(removeRoot); + const provider = new FlutterBuildProvider(); onTestFinished(() => provider.close()); const result = await provider.execute( { operation: "identify_flutter_build", input: { path } }, @@ -1842,9 +1844,10 @@ it.each([ vi.restoreAllMocks(); }); const removeRoot = vi.fn((root: string) => - rm(root, { recursive: true, force: true }), + realRm(root, { recursive: true, force: true }), ); - const provider = new FlutterBuildProvider({ removeRoot }); + interceptFlutterRootRemoval(removeRoot); + const provider = new FlutterBuildProvider(); onTestFinished(() => provider.close()); const result = await provider.execute( { operation: "inspect_dart_aot", input: { path } }, @@ -1870,25 +1873,24 @@ it.each([false, true])( let retainedRoot = ""; const controller = new AbortController(); const reason = { caller: "cancel during refused cleanup" }; - const provider = new FlutterBuildProvider({ - removeRoot: async (root) => { - retainedRoot = root; - if (refuse) { - if (cancel) controller.abort(reason); - await rmdir(root); // Actual OS refusal: the owned snapshot still occupies the directory. - } else await rm(root, { recursive: true, force: true }); - }, - }); - onTestFinished(async () => { - refuse = false; - await provider.close(); - }); const controlProvider = new FlutterBuildProvider(); const control = await new FlutterBuildAnalysisService( controlProvider, ).execute("identify_flutter_build", { path }); await controlProvider.close(); if (!control.ok) throw control.error; + interceptFlutterRootRemoval(async (root) => { + retainedRoot = root; + if (refuse) { + if (cancel) controller.abort(reason); + await rmdir(root); // Actual OS refusal: the owned snapshot still occupies the directory. + } else await realRm(root, { recursive: true, force: true }); + }); + const provider = new FlutterBuildProvider(); + onTestFinished(async () => { + refuse = false; + await provider.close(); + }); const result = await provider.execute( { operation: "identify_flutter_build", input: { path } }, { signal: controller.signal }, @@ -1942,9 +1944,10 @@ it("drains admitted snapshot work before close and fences later execution", asyn return original.apply(this, args); }); const removeRoot = vi.fn((root: string) => - rm(root, { recursive: true, force: true }), + realRm(root, { recursive: true, force: true }), ); - const provider = new FlutterBuildProvider({ removeRoot }); + interceptFlutterRootRemoval(removeRoot); + const provider = new FlutterBuildProvider(); onTestFinished(async () => { proceed(); list.mockRestore(); @@ -1979,12 +1982,11 @@ it("drains admitted snapshot work before close and fences later execution", asyn it("preserves source admission failure when cancellation arrives during its root cleanup", async () => { const root = await createTestTempDirectory("flutter-primary-"); const controller = new AbortController(); - const provider = new FlutterBuildProvider({ - removeRoot: async (path) => { - await rm(path, { recursive: true, force: true }); - controller.abort({ caller: "late cleanup cancellation" }); - }, + interceptFlutterRootRemoval(async (path) => { + await realRm(path, { recursive: true, force: true }); + controller.abort({ caller: "late cleanup cancellation" }); }); + const provider = new FlutterBuildProvider(); onTestFinished(() => provider.close()); const result = await provider.execute( { @@ -2005,13 +2007,12 @@ it("retains mixed-root cleanup diagnostics and releases aggregate errors only af ]); const roots: string[] = []; const allowed = new Set(); - const provider = new FlutterBuildProvider({ - removeRoot: async (root) => { - if (!roots.includes(root)) roots.push(root); - if (!allowed.has(root)) await rmdir(root); - else await rm(root, { recursive: true, force: true }); - }, + interceptFlutterRootRemoval(async (root) => { + if (!roots.includes(root)) roots.push(root); + if (!allowed.has(root)) await rmdir(root); + else await realRm(root, { recursive: true, force: true }); }); + const provider = new FlutterBuildProvider(); onTestFinished(async () => { roots.forEach((root) => allowed.add(root)); await provider.close(); @@ -2063,13 +2064,12 @@ it("retries retained cleanup before admitting replacement snapshots", async () = ]); const roots = new Set(); let refuse = true; - const provider = new FlutterBuildProvider({ - removeRoot: async (root) => { - roots.add(root); - if (refuse) await rmdir(root); - else await rm(root, { recursive: true, force: true }); - }, + interceptFlutterRootRemoval(async (root) => { + roots.add(root); + if (refuse) await rmdir(root); + else await realRm(root, { recursive: true, force: true }); }); + const provider = new FlutterBuildProvider(); onTestFinished(async () => { refuse = false; await provider.close(); @@ -2108,17 +2108,16 @@ it("does not create a replacement snapshot when cancelled during cleanup retry", const controller = new AbortController(); const reason = { caller: "cancel during cleanup retry" }; let attempts = 0; - const provider = new FlutterBuildProvider({ - removeRoot: async (root) => { - roots.add(root); - attempts++; - if (attempts === 1) await rmdir(root); - else { - controller.abort(reason); - await rm(root, { recursive: true, force: true }); - } - }, + interceptFlutterRootRemoval(async (root) => { + roots.add(root); + attempts++; + if (attempts === 1) await rmdir(root); + else { + controller.abort(reason); + await realRm(root, { recursive: true, force: true }); + } }); + const provider = new FlutterBuildProvider(); onTestFinished(() => provider.close()); const request = { operation: "identify_flutter_build" as const, @@ -2143,13 +2142,12 @@ it.each([false, true])( const controller = new AbortController(); const reason = { caller: "cancel during snapshot cleanup" }; let refuse = cleanupFails; - const provider = new FlutterBuildProvider({ - removeRoot: async (root) => { - controller.abort(reason); - if (refuse) await rmdir(root); - else await rm(root, { recursive: true, force: true }); - }, + interceptFlutterRootRemoval(async (root) => { + controller.abort(reason); + if (refuse) await rmdir(root); + else await realRm(root, { recursive: true, force: true }); }); + const provider = new FlutterBuildProvider(); onTestFinished(async () => { refuse = false; await provider.close(); @@ -2179,15 +2177,14 @@ it("retains cancellation when a prior failed root cannot be retired on admission const reason = { caller: "abort during failed retry" }; let refuse = true; let attempts = 0; - const provider = new FlutterBuildProvider({ - removeRoot: async (root) => { - attempts += 1; - if (refuse) { - if (attempts === 2) controller.abort(reason); - await rmdir(root); - } else await rm(root, { recursive: true, force: true }); - }, + interceptFlutterRootRemoval(async (root) => { + attempts += 1; + if (refuse) { + if (attempts === 2) controller.abort(reason); + await rmdir(root); + } else await realRm(root, { recursive: true, force: true }); }); + const provider = new FlutterBuildProvider(); onTestFinished(async () => { refuse = false; await provider.close(); @@ -2220,14 +2217,13 @@ it("preserves a completed analysis failure through refused cleanup and late canc if (control.ok) throw new Error("Expected unsupported package control"); const controller = new AbortController(); let refuse = true; - const provider = new FlutterBuildProvider({ - removeRoot: async (root) => { - if (refuse) { - controller.abort({ caller: "late failed cleanup" }); - await rmdir(root); - } else await rm(root, { recursive: true, force: true }); - }, + interceptFlutterRootRemoval(async (root) => { + if (refuse) { + controller.abort({ caller: "late failed cleanup" }); + await rmdir(root); + } else await realRm(root, { recursive: true, force: true }); }); + const provider = new FlutterBuildProvider(); onTestFinished(async () => { refuse = false; await provider.close(); diff --git a/tests/boundary/filesystem/ghidraExecutableHeader.test.ts b/tests/boundary/filesystem/ghidraExecutableHeader.test.ts index e42d5950aa..871b1f839a 100644 --- a/tests/boundary/filesystem/ghidraExecutableHeader.test.ts +++ b/tests/boundary/filesystem/ghidraExecutableHeader.test.ts @@ -9,10 +9,7 @@ import { } from "node:fs/promises"; import { join } from "node:path"; import { afterEach, expect, it, vi } from "vitest"; -import { - readGhidraExecutableHeader, - GHIDRA_EXECUTABLE_HEADER_BYTES, -} from "../../../src/ghidra/GhidraExecutableHeader.js"; +import { readGhidraExecutableHeader } from "../../../src/ghidra/GhidraExecutableHeader.js"; import { ProviderCleanupError } from "../../../src/domain/providerCleanupError.js"; import { createInspectionResources } from "../../fixtures/inspectionResources.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; @@ -64,11 +61,11 @@ it.each([0, 23, 8192])( ).toEqual({ ok: true, value: { - bytes: bytes.subarray(0, GHIDRA_EXECUTABLE_HEADER_BYTES), + bytes: bytes.subarray(0, 4096), size: length, }, }); - expect(bytesRead).toBe(Math.min(length, GHIDRA_EXECUTABLE_HEADER_BYTES)); + expect(bytesRead).toBe(Math.min(length, 4096)); } finally { await resources.close(); } diff --git a/tests/boundary/filesystem/ghidraInstallationProperties.test.ts b/tests/boundary/filesystem/ghidraInstallationProperties.test.ts index 5f8b147de0..136b0317e2 100644 --- a/tests/boundary/filesystem/ghidraInstallationProperties.test.ts +++ b/tests/boundary/filesystem/ghidraInstallationProperties.test.ts @@ -14,13 +14,12 @@ import { afterEach, expect, it, vi } from "vitest"; import { AnalysisError } from "../../../src/domain/analysisErrorBase.js"; import { ProviderCleanupError } from "../../../src/domain/providerCleanupError.js"; import { projectAnalysisError } from "../../../src/domain/analysisErrorProjection.js"; -import { - readGhidraInstallationProperties, - MAX_GHIDRA_INSTALLATION_PROPERTIES_BYTES, -} from "../../../src/ghidra/GhidraInstallationProperties.js"; +import { readGhidraInstallationProperties } from "../../../src/ghidra/GhidraInstallationProperties.js"; import { createInspectionResources } from "../../fixtures/inspectionResources.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; +const MAX_GHIDRA_INSTALLATION_PROPERTIES_BYTES = 1024 * 1024; + const controls = vi.hoisted(() => ({ file: undefined as ((handle: FileHandle, path: string) => void) | undefined, })); diff --git a/tests/boundary/filesystem/ghidraLanguageCatalogRecovery.test.ts b/tests/boundary/filesystem/ghidraLanguageCatalogRecovery.test.ts index 31adbada80..457d5f9115 100644 --- a/tests/boundary/filesystem/ghidraLanguageCatalogRecovery.test.ts +++ b/tests/boundary/filesystem/ghidraLanguageCatalogRecovery.test.ts @@ -11,6 +11,7 @@ import { GhidraProvider } from "../../../src/ghidra/GhidraProvider.js"; import { silentLogger } from "../../../src/logger.js"; import { createServer } from "../../../src/server/createServer.js"; import { createDeferred } from "../../fixtures/binarySession.js"; +import { awaitAcknowledgement } from "../../fixtures/lifecycleAcknowledgement.js"; import { parseMcpToolError } from "../../fixtures/mcpToolError.js"; import { nativeAotPeFixture } from "../../fixtures/nativeaotPe.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; @@ -199,39 +200,45 @@ it.each(["released", "still failing"] as const)( .finally(() => { settled = true; }); - await entered.promise; - controller.abort(reason); - await Promise.resolve(); - // Cancellation never abandons the in-flight close of the retained owner. - expect(settled).toBe(false); - if (outcome === "released") fault.deny = false; - fault.onClose = undefined; - release.resolve(undefined); - const cancelled = await admission; - if (cancelled.ok) throw new Error("Expected cancelled admission"); - expect(fault.closes).toBe(2); - expect(fault.opens).toBe(1); + try { + await awaitAcknowledgement(entered.promise, admission, "catalog cleanup"); + controller.abort(reason); + await Promise.resolve(); + // Cancellation never abandons the in-flight close of the retained owner. + expect(settled).toBe(false); + if (outcome === "released") fault.deny = false; + fault.onClose = undefined; + release.resolve(undefined); + const cancelled = await admission; + if (cancelled.ok) throw new Error("Expected cancelled admission"); + expect(fault.closes).toBe(2); + expect(fault.opens).toBe(1); - if (outcome === "released") { - expect(cancelled.error).toMatchObject({ - _tag: "AnalysisCancelledError", - cause: reason, - cleanupIncomplete: false, - }); - expect(cancelled.error.cause).toBe(reason); + if (outcome === "released") { + expect(cancelled.error).toMatchObject({ + _tag: "AnalysisCancelledError", + cause: reason, + cleanupIncomplete: false, + }); + expect(cancelled.error.cause).toBe(reason); + expect(first.error.cleanupIncomplete).toBe(false); + expect(registry.hasPendingOwnedResources()).toBe(false); + } else { + expect(cancelled.error.cleanupIncomplete).toBe(true); + expect(cancelled.error.cleanupResources).toEqual([definition]); + expect(first.error.cleanupIncomplete).toBe(true); + expect(registry.hasPendingOwnedResources()).toBe(true); + fault.deny = false; + } + const recovered = await registry.select(target, "ghidra"); + expect(recovered.ok).toBe(true); expect(first.error.cleanupIncomplete).toBe(false); expect(registry.hasPendingOwnedResources()).toBe(false); - } else { - expect(cancelled.error.cleanupIncomplete).toBe(true); - expect(cancelled.error.cleanupResources).toEqual([definition]); - expect(first.error.cleanupIncomplete).toBe(true); - expect(registry.hasPendingOwnedResources()).toBe(true); - fault.deny = false; + } finally { + fault.onClose = undefined; + release.resolve(undefined); + await admission.catch(() => undefined); } - const recovered = await registry.select(target, "ghidra"); - expect(recovered.ok).toBe(true); - expect(first.error.cleanupIncomplete).toBe(false); - expect(registry.hasPendingOwnedResources()).toBe(false); }, ); diff --git a/tests/boundary/filesystem/ghidraProfileArtifactOwnership.test.ts b/tests/boundary/filesystem/ghidraProfileArtifactOwnership.test.ts index 5cc068c49f..8b0ec0e5ad 100644 --- a/tests/boundary/filesystem/ghidraProfileArtifactOwnership.test.ts +++ b/tests/boundary/filesystem/ghidraProfileArtifactOwnership.test.ts @@ -7,6 +7,7 @@ import { GhidraProvider } from "../../../src/ghidra/GhidraProvider.js"; import { ghidraProfileInstallationHost } from "../../../src/ghidra/GhidraInstallation.fixture.js"; import { silentLogger } from "../../../src/logger.js"; import { createDeferred } from "../../fixtures/binarySession.js"; +import { awaitAcknowledgement } from "../../fixtures/lifecycleAcknowledgement.js"; import { STARTUP_OWNER_CLOSED } from "../../../src/process/SharedStartupAttempt.js"; import { readFile, @@ -391,15 +392,21 @@ it("joins whole provider resolution before shutdown and gates the next admission opens++; }; const resolving = provider.resolveAnalysisProfile(target); - await entered.promise; - const count = opens; - let closed = false; - const closing = provider.closeOwnedResources().then((result) => { - closed = true; - return result; - }); - const next = provider.resolveAnalysisProfile(target); + let closing: ReturnType | undefined; + let next: ReturnType | undefined; try { + await awaitAcknowledgement( + entered.promise, + resolving, + "profile resolution", + ); + const count = opens; + let closed = false; + closing = provider.closeOwnedResources().then((result) => { + closed = true; + return result; + }); + next = provider.resolveAnalysisProfile(target); await new Promise((resolve) => setImmediate(resolve)); expect(closed).toBe(false); expect(opens).toBe(count); @@ -421,7 +428,11 @@ it("joins whole provider resolution before shutdown and gates the next admission expect(provider.hasPendingOwnedResources()).toBe(false); } finally { release.resolve(); - await Promise.all([resolving, closing, next]); + await Promise.allSettled([ + resolving, + ...(closing ? [closing] : []), + ...(next ? [next] : []), + ]); await provider.closeOwnedResources(); } }); diff --git a/tests/boundary/filesystem/inesCancellation.test.ts b/tests/boundary/filesystem/inesCancellation.test.ts index 1961b2ae5c..14ff5eef64 100644 --- a/tests/boundary/filesystem/inesCancellation.test.ts +++ b/tests/boundary/filesystem/inesCancellation.test.ts @@ -21,6 +21,33 @@ import { import { readFileHandlePrefix } from "../../../src/filesystem/readFileHandlePrefix.js"; import { abortIfNeeded } from "../../../src/artifacts/ArtifactHash.js"; +it.each(["close", "cancel"] as const)( + "does not complete an iNES inventory after %s while paused", + async (interruption) => { + const root = await createTestTempDirectory("rea-ines-paused-"); + const path = join(root, "source.nes"); + await writeFile(path, inesFixture()); + const handle = await open(path, "r"); + const reader = new InesArtifactReader(path, { + handle, + initial: await handle.stat(), + }); + const controller = new AbortController(); + try { + const entries = reader.entries(controller.signal)[Symbol.asyncIterator](); + expect((await entries.next()).done).toBe(false); + if (interruption === "close") await reader.close(); + else controller.abort(); + await expect(entries.next()).rejects.toMatchObject({ + reason: interruption === "close" ? "unavailable" : "cancelled", + }); + } finally { + await reader.close(); + await handle.close(); + } + }, +); + // Complete the real I/O, then cancel while its caller is still awaiting it. const cancelAfterNextStat = ( handle: FileHandle, diff --git a/tests/boundary/filesystem/installScript.test.ts b/tests/boundary/filesystem/installScript.test.ts index d21815c72b..b55a769bbd 100644 --- a/tests/boundary/filesystem/installScript.test.ts +++ b/tests/boundary/filesystem/installScript.test.ts @@ -31,7 +31,7 @@ const UNSUPPORTED_NODE_RECOVERY_LINUX_X64 = " https://nodejs.org/dist/latest-v24.x/ (grab the linux-x64 tarball)\n" + "Then rerun this installer.\n"; -describe("installer Node.js admission", { timeout: 20_000 }, () => { +describe("installer Node.js admission", () => { it.each(["22.23.4", "24.22.1", "26.12.1", "26.13.0", "24.22.1+build.5"])( "accepts supported Node %s without installing or replacing it", async (version) => { @@ -71,7 +71,7 @@ describe("installer Node.js admission", { timeout: 20_000 }, () => { }); }); -describe("curl installer scenarios", { timeout: 20_000 }, () => { +describe("curl installer scenarios", () => { it("installs only a pinned REA CLI with closed stdin", async () => { const fixture = await createFixture(); const result = await runInstaller(fixture, ["--version", "0.3.0"]); @@ -194,7 +194,7 @@ describe("curl installer scenarios", { timeout: 20_000 }, () => { }); }); -describe("installer reported version", { timeout: 20_000 }, () => { +describe("installer reported version", () => { it("rejects an empty version from a successful installed command", async () => { const fixture = await createFixture(); await expect( @@ -206,7 +206,7 @@ describe("installer reported version", { timeout: 20_000 }, () => { }); }); -describe("installer TTY fallback", { timeout: 20_000 }, () => { +describe("installer TTY fallback", () => { it("skips automatic setup when stderr is redirected", async () => { const fixture = await createFixture(); const script = join(process.cwd(), "install.sh"); @@ -263,7 +263,7 @@ describe("installer TTY fallback", { timeout: 20_000 }, () => { }); }); -describe("truncated installer download", { timeout: 20_000 }, () => { +describe("truncated installer download", () => { it("runs nothing from a download truncated after the npm install line", async () => { const fixture = await createFixture(); const source = await readFile(join(process.cwd(), "install.sh"), "utf8"); @@ -295,7 +295,7 @@ describe("truncated installer download", { timeout: 20_000 }, () => { describe( "installer download truncated at any byte", { - timeout: 180_000, + tags: ["long-running"], }, () => { // Every prefix of the script, from the empty download to the full file, as @@ -356,7 +356,7 @@ describe( }, ); -describe("Linux user-prefix installer", { timeout: 20_000 }, () => { +describe("Linux user-prefix installer", () => { it("prints the executed arguments as a reusable command with shell metacharacters", async () => { const fixture = await createFixture(); const home = join(fixture.home, "home with 'quotes' $cash; [glob]"); @@ -392,7 +392,7 @@ describe("Linux user-prefix installer", { timeout: 20_000 }, () => { }); }); -describe("native macOS installer", { timeout: 20_000 }, () => { +describe("native macOS installer", () => { it("installs on Darwin with an empty prefix argument array", async () => { const fixture = await createFixture(); const result = await runInstaller(fixture, ["--version", "0.3.0"], { @@ -411,7 +411,7 @@ describe("native macOS installer", { timeout: 20_000 }, () => { }); }); -describe("installer semantic version parsing", { timeout: 20_000 }, () => { +describe("installer semantic version parsing", () => { it("rejects an explicitly empty version instead of selecting latest", async () => { const fixture = await createFixture(); await expect( diff --git a/tests/boundary/filesystem/interfaceBuilderBundle.test.ts b/tests/boundary/filesystem/interfaceBuilderBundle.test.ts index cc88f4864e..1e2006c562 100644 --- a/tests/boundary/filesystem/interfaceBuilderBundle.test.ts +++ b/tests/boundary/filesystem/interfaceBuilderBundle.test.ts @@ -663,7 +663,6 @@ describe("keyed archive hierarchy coverage", () => { analysis.graph.edges.filter(({ relation }) => relation === "contains"), ).toHaveLength(41); }, - 120_000, ); }); diff --git a/tests/boundary/filesystem/javascriptArtifactReconstruction.test.ts b/tests/boundary/filesystem/javascriptArtifactReconstruction.test.ts index 756d92f59f..4f4dc0435b 100644 --- a/tests/boundary/filesystem/javascriptArtifactReconstruction.test.ts +++ b/tests/boundary/filesystem/javascriptArtifactReconstruction.test.ts @@ -383,7 +383,7 @@ it("retains every repeated content observation and containment path", async () = truncated: false, omitted_count: 0, }); -}, 15_000); +}); it("rejects NUL paths through the public request contract before reconstruction", async () => { const input = { input_path: "invalid\u0000.asar" }; diff --git a/tests/boundary/filesystem/keyedArchiveNumberMetadata.test.ts b/tests/boundary/filesystem/keyedArchiveNumberMetadata.test.ts index 691645d13f..466f7085e2 100644 --- a/tests/boundary/filesystem/keyedArchiveNumberMetadata.test.ts +++ b/tests/boundary/filesystem/keyedArchiveNumberMetadata.test.ts @@ -13,7 +13,7 @@ import { expectKeyedArchiveNumberMetadata, keyedArchiveNumberMetadataFixture, } from "../../fixtures/keyedArchiveNumberMetadata.js"; -import { cliTest, runFileCli } from "../../support/cli/cliFixture.js"; +import { cliTest } from "../../support/cli/cliFixture.js"; it("preserves 16-byte binary integers and adjacent 64-bit integer metadata", async () => { const fixture = await keyedArchiveWideIntegerFixture(); @@ -133,7 +133,7 @@ cliTest.for(archiveNumberMetadataCases)( "built CLI preserves numeric meaning for $name", async (item, { cli }) => { const fixture = await keyedArchiveNumberMetadataFixture(item); - const output = await runFileCli(cli, { + const output = await cli.run({ arguments: ["inspect-keyed-archive", fixture.path, "--json"], environment: { REA_LOG_LEVEL: "silent", REA_ANALYSIS_PROVIDER: "auto" }, }); diff --git a/tests/boundary/filesystem/lintRuleConfiguration.test.ts b/tests/boundary/filesystem/lintRuleConfiguration.test.ts index e4f2fcafeb..5187faac5c 100644 --- a/tests/boundary/filesystem/lintRuleConfiguration.test.ts +++ b/tests/boundary/filesystem/lintRuleConfiguration.test.ts @@ -249,7 +249,6 @@ for (const scenario of cases) { ], { cwd: project, - timeoutMs: 15_000, }, ); expect(result.signal, result.stderr).toBeNull(); @@ -294,7 +293,7 @@ export const unnecessary = /\!/; "--fix", applicationSource, ], - { cwd: project, timeoutMs: 15_000 }, + { cwd: project }, ); expect(result.signal, result.stderr).toBeNull(); expect(result.exitCode, result.stderr || result.stdout).toBe(0); diff --git a/tests/boundary/filesystem/nativeExports.test.ts b/tests/boundary/filesystem/nativeExports.test.ts index 1079012301..9a1ccdbbee 100644 --- a/tests/boundary/filesystem/nativeExports.test.ts +++ b/tests/boundary/filesystem/nativeExports.test.ts @@ -126,6 +126,38 @@ describe("native export stable filesystem boundary", () => { }); describe("native export text capacity", () => { + it("retains valid exports and partial Evidence when a symbol name exceeds the read allowance", async () => { + const root = await createTestTempDirectory("rea-native-export-name-limit-"); + const path = join(root, "libfixture.so"); + const bytes = elfExportImage({ + symbols: [{ name: "n".repeat(65_536) }, { name: "kept" }], + }); + await writeFile(path, bytes); + const service = createNativeExportsService(); + try { + const result = await service.inspect({ path }); + if (!result.ok) throw result.error; + const evidence = parseEvidence(result.value); + const report = nativeExportsSchema.parse(evidence.normalized_result); + expect(evidence.subject).toMatchObject({ + local_path: path, + digest: { sha256: createHash("sha256").update(bytes).digest("hex") }, + }); + expect(report).toMatchObject({ + exports: [expect.objectContaining({ name: "kept" })], + coverage: { status: "partial" }, + issues: expect.arrayContaining([ + expect.objectContaining({ + code: "name_unreadable", + message: expect.stringContaining("65536-byte read allowance"), + }), + ]), + }); + } finally { + await service.close(); + } + }); + it.each(["symbol", "version"] as const)( "bounds UTF-8 bytes of retained ELF %s text and its exact-byte spelling", async (kind) => { diff --git a/tests/boundary/filesystem/peImports.test.ts b/tests/boundary/filesystem/peImports.test.ts index 9c79798e35..0a76b580e2 100644 --- a/tests/boundary/filesystem/peImports.test.ts +++ b/tests/boundary/filesystem/peImports.test.ts @@ -1,6 +1,7 @@ import { symlink, writeFile } from "node:fs/promises"; import { join } from "node:path"; import { describe, expect, it } from "vitest"; +import { peImportsSchema } from "../../../src/domain/native/peImports.js"; import { createPeImportsService } from "../../../src/composition/binaryDiagnostics.js"; import { peImportImage } from "../../fixtures/binaryDiagnostics/peImports.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; @@ -38,6 +39,30 @@ describe("PE import stable filesystem boundary", () => { } }); + it("retains partial Evidence for an import name beyond the read allowance", async () => { + const root = await createTestTempDirectory("rea-pe-imports-name-limit-"); + const path = join(root, "app.exe"); + await writeFile( + path, + peImportImage({ + sectionBytes: 8192, + dlls: [{ name: "n".repeat(4097), functions: [] }], + }), + ); + const result = await createPeImportsService().inspect({ path }); + if (!result.ok) throw result.error; + const report = peImportsSchema.parse(result.value.normalized_result); + expect(report.coverage.status).toBe("partial"); + expect(report.imports[0]?.name).toBeNull(); + expect(report.issues).toContainEqual( + expect.objectContaining({ + code: "name_unreadable", + message: expect.stringContaining("4096-byte read allowance"), + }), + ); + expect(result.value.subject?.local_path).toBe(path); + }); + it("returns typed resource and cancellation failures", async () => { const root = await createTestTempDirectory("rea-pe-imports-budget-"); const path = join(root, "app.exe"); diff --git a/tests/boundary/filesystem/productCatalog.test.ts b/tests/boundary/filesystem/productCatalog.test.ts index a0ab49b9f5..ee86acba34 100644 --- a/tests/boundary/filesystem/productCatalog.test.ts +++ b/tests/boundary/filesystem/productCatalog.test.ts @@ -121,7 +121,7 @@ describe("canonical product catalog", () => { await readFile("docs/public/product-catalog.json", "utf8"), ); await expect(documentationFactIssues(root, catalog)).resolves.toEqual([]); - }, 60_000); + }); }); describe("canonical CLI catalog", () => { @@ -129,7 +129,7 @@ describe("canonical CLI catalog", () => { const inventory = createCliInventory(createCli()); expect(inventory.primary).toEqual([...CLI_COMMAND_NAMES].sort()); expect(inventory.aliases).toEqual([]); - }, 30_000); + }); it("describes every primary command argument and option", () => { expect(cliCommandDescriptionIssues(createCli())).toEqual([]); diff --git a/tests/boundary/filesystem/referenceSourceFifoRace.test.ts b/tests/boundary/filesystem/referenceSourceFifoRace.test.ts index eb6d1be1ef..5f771c7084 100644 --- a/tests/boundary/filesystem/referenceSourceFifoRace.test.ts +++ b/tests/boundary/filesystem/referenceSourceFifoRace.test.ts @@ -18,11 +18,11 @@ processTest.skipIf(process.platform === "win32")( import.meta.url, ), ); - const result = await processes.run( - process.execPath, - [probe, readerModule, root], - { timeoutMs: 3_000 }, - ); + const result = await processes.run(process.execPath, [ + probe, + readerModule, + root, + ]); expect(result.exitCode, result.stderr).toBe(0); expect(result.stdout.trim()).toBe("changed"); }, diff --git a/tests/boundary/filesystem/releaseExposure.test.ts b/tests/boundary/filesystem/releaseExposure.test.ts index 24e10c0880..afd8ef246f 100644 --- a/tests/boundary/filesystem/releaseExposure.test.ts +++ b/tests/boundary/filesystem/releaseExposure.test.ts @@ -340,5 +340,5 @@ describe("release exposure ASAR header admission", () => { } finally { await lowered.close(); } - }, 60_000); + }); }); diff --git a/tests/boundary/filesystem/stableArtifact.test.ts b/tests/boundary/filesystem/stableArtifact.test.ts index 2b03a7a75a..fe62f4d10b 100644 --- a/tests/boundary/filesystem/stableArtifact.test.ts +++ b/tests/boundary/filesystem/stableArtifact.test.ts @@ -10,6 +10,7 @@ import { import { join } from "node:path"; import { createHash } from "node:crypto"; import { expect, it, onTestFinished } from "vitest"; +import { awaitAcknowledgement } from "../../fixtures/lifecycleAcknowledgement.js"; import { readStableArtifact } from "../../../src/artifacts/readStableArtifact.js"; import { artifactFileCompletion, @@ -162,7 +163,7 @@ it.each(["complete", "fail"] as const)( settled = true; }); try { - await entered; + await awaitAcknowledgement(entered, result, "stable artifact read"); controller.abort(reason); await new Promise((resolve) => setImmediate(resolve)); expect(settled).toBe(false); @@ -176,6 +177,7 @@ it.each(["complete", "fail"] as const)( expect(artifactFileCompletion(failure)).toBeUndefined(); expect(descriptor?.fd).toBe(-1); } finally { + controller.abort(reason); release(); await result; } diff --git a/tests/boundary/filesystem/testDeadlines.test.ts b/tests/boundary/filesystem/testDeadlines.test.ts new file mode 100644 index 0000000000..6b366a486d --- /dev/null +++ b/tests/boundary/filesystem/testDeadlines.test.ts @@ -0,0 +1,27 @@ +import { describe, expect, inject, it } from "vitest"; +import { + LONG_RUNNING_DEADLINE_MS, + TEST_DEADLINE_MS, +} from "../../support/testDeadlines.js"; + +// These probes read the deadline Vitest collected, so they fail if project +// inheritance or tag resolution stops applying the configured policy. +const scale = inject("testDeadlineScale"); + +it("applies the built project deadline to an untagged test", ({ task }) => { + expect(task.timeout).toBe(TEST_DEADLINE_MS.built * scale); +}); + +it( + "applies the long-running deadline to a tagged test", + { tags: ["long-running"] }, + ({ task }) => { + expect(task.timeout).toBe(LONG_RUNNING_DEADLINE_MS * scale); + }, +); + +describe("a long-running suite", { tags: ["long-running"] }, () => { + it("passes the long-running deadline to its tests", ({ task }) => { + expect(task.timeout).toBe(LONG_RUNNING_DEADLINE_MS * scale); + }); +}); diff --git a/tests/boundary/filesystem/testDeadlinesModuleTag.test.ts b/tests/boundary/filesystem/testDeadlinesModuleTag.test.ts new file mode 100644 index 0000000000..abb79d2629 --- /dev/null +++ b/tests/boundary/filesystem/testDeadlinesModuleTag.test.ts @@ -0,0 +1,11 @@ +// @module-tag long-running +import { expect, inject, it } from "vitest"; +import { LONG_RUNNING_DEADLINE_MS } from "../../support/testDeadlines.js"; + +it("applies the long-running deadline to every test of a tagged module", ({ + task, +}) => { + expect(task.timeout).toBe( + LONG_RUNNING_DEADLINE_MS * inject("testDeadlineScale"), + ); +}); diff --git a/tests/boundary/filesystem/typedLintRules.test.ts b/tests/boundary/filesystem/typedLintRules.test.ts index eaf3ddf178..5e18a87ec0 100644 --- a/tests/boundary/filesystem/typedLintRules.test.ts +++ b/tests/boundary/filesystem/typedLintRules.test.ts @@ -154,7 +154,6 @@ processTest( [reporter, "typed-lint", ...Object.keys(sources)], { cwd: root, - timeoutMs: 20_000, env: { ...process.env, GOMAXPROCS: "2", GOMEMLIMIT: "1GiB" }, }, ); diff --git a/tests/boundary/filesystem/undeliveredSourceClosure.test.ts b/tests/boundary/filesystem/undeliveredSourceClosure.test.ts index ae7444d84b..cf4ae0afa7 100644 --- a/tests/boundary/filesystem/undeliveredSourceClosure.test.ts +++ b/tests/boundary/filesystem/undeliveredSourceClosure.test.ts @@ -243,7 +243,6 @@ it.each(["final-close cancellation", "missing sources"] as const)( }); expect(jsonByteLength(closure)).toBeLessThan(16 * 1024); }, - 60_000, ); it("reports a data-sized link set cited by the observation itself", async () => { @@ -264,7 +263,7 @@ it("reports a data-sized link set cited by the observation itself", async () => ); expect(closure.omitted_count).toBe(DATA_SIZED_LINKS); expect(closure.omitted).toHaveLength(64); -}, 60_000); +}); it("examines repeated links once across every captured source", async () => { const root = await createTestTempDirectory("rea-closure-dedupe-"); @@ -288,7 +287,7 @@ it("examines repeated links once across every captured source", async () => { ); expect(closure.records).toEqual([shared]); expect(closure.omitted_count).toBe(2); -}, 60_000); +}); it.each([ ["a captured source", "captured"], diff --git a/tests/boundary/filesystem/verifierFileSnapshots.test.ts b/tests/boundary/filesystem/verifierFileSnapshots.test.ts index 65b8ed6ebb..4781886f24 100644 --- a/tests/boundary/filesystem/verifierFileSnapshots.test.ts +++ b/tests/boundary/filesystem/verifierFileSnapshots.test.ts @@ -263,11 +263,11 @@ processTest( else await removeTestWorkspace(workspace.root); } `; - const result = await processes.run( - process.execPath, - ["--input-type=module", "--eval", program], - { timeoutMs: 20_000 }, - ); + const result = await processes.run(process.execPath, [ + "--input-type=module", + "--eval", + program, + ]); expect(result.exitCode, result.stderr).toBe(0); expect(result.signal, result.stderr).toBeNull(); diff --git a/tests/boundary/filesystem/wideDirectoryTraversal.test.ts b/tests/boundary/filesystem/wideDirectoryTraversal.test.ts new file mode 100644 index 0000000000..4df1f1fdb5 --- /dev/null +++ b/tests/boundary/filesystem/wideDirectoryTraversal.test.ts @@ -0,0 +1,104 @@ +import { mkdirSync } from "node:fs"; +import { join } from "node:path"; +import { afterAll, beforeAll, describe, expect, it } from "vitest"; + +import { ArtifactReaderFailure } from "../../../src/artifacts/ArtifactReader.js"; +import { DirectoryArtifactReader } from "../../../src/artifacts/DirectoryArtifactReader.js"; +import { readReferenceSource } from "../../support/referenceSourceResourceScope.js"; +import { + createTestWorkspace, + removeTestWorkspace, + type TestWorkspace, +} from "../../support/workspace/workspaceFixture.js"; + +/** More children than one call can receive as arguments on V8's default stack. */ +const CHILD_DIRECTORIES = 130_000; +const childNames = (): string[] => + Array.from({ length: CHILD_DIRECTORIES }, (_, index) => `d${index}`); + +const missingChildren = (paths: Iterable): string[] => { + const listed = new Set(paths); + return childNames().filter((name) => !listed.has(name)); +}; + +const traverse = async ( + root: string, + maximumEntries: number, + stopAfter?: { readonly entries: number; readonly reason: Error }, +): Promise<{ readonly paths: string[]; readonly failure: unknown }> => { + const reader = new DirectoryArtifactReader(root); + const controller = new AbortController(); + const paths: string[] = []; + try { + for await (const entry of reader.entries(controller.signal, { + maximumEntries, + })) { + paths.push(entry.path); + if (paths.length === stopAfter?.entries) + controller.abort(stopAfter.reason); + } + return { paths, failure: undefined }; + } catch (failure: unknown) { + return { paths, failure }; + } finally { + await reader.close(); + } +}; + +// One shallow tree serves both independent traversal owners; every child is +// empty, so only the parent's child list is wide. +describe("directory traversal of a parent wider than call arguments allow", () => { + let workspace: TestWorkspace | undefined; + let root = ""; + beforeAll(async () => { + workspace = await createTestWorkspace("rea-wide-directory-"); + root = await workspace.mkdir("tree"); + for (const name of childNames()) mkdirSync(join(root, name)); + }); + afterAll(async () => { + if (workspace !== undefined) await removeTestWorkspace(workspace.root); + }); + + it("schedules the artifact reader's children within the selected entry budget", async () => { + // Cancel once the parent listing is complete. The next cancellation + // checkpoint follows scheduling its children for descent. + const reason = new Error("Stop after the wide parent listing"); + const listed = await traverse(root, CHILD_DIRECTORIES, { + entries: CHILD_DIRECTORIES, + reason, + }); + expect(listed.failure).toBeInstanceOf(ArtifactReaderFailure); + expect(listed.failure).toMatchObject({ + reason: "cancelled", + cause: reason, + }); + expect(listed.paths).toHaveLength(CHILD_DIRECTORIES); + expect(missingChildren(listed.paths)).toEqual([]); + + // A smaller selected allowance stays a typed refusal at its actual bound. + const refused = await traverse(root, 2_000); + expect(refused.paths).toHaveLength(2_000); + expect(refused.failure).toBeInstanceOf(ArtifactReaderFailure); + expect(refused.failure).toMatchObject({ + reason: "limit", + examinedEntries: 2_000, + maximumEntries: 2_000, + }); + }); + + it.skipIf(process.platform === "win32")( + "reads every child directory into the reference-source inventory", + async () => { + const result = await readReferenceSource(root); + if (!result.ok) throw result.error; + const { entries } = result.value; + expect(entries).toHaveLength(CHILD_DIRECTORIES); + expect( + entries.filter( + (entry) => entry.status !== "read" || entry.kind !== "directory", + ), + ).toEqual([]); + expect(missingChildren(entries.map(({ path }) => path))).toEqual([]); + }, + ); +}); diff --git a/tests/boundary/filesystem/windowsNpxRegistration.test.ts b/tests/boundary/filesystem/windowsNpxRegistration.test.ts index 1e52a5f643..ae67a8af3b 100644 --- a/tests/boundary/filesystem/windowsNpxRegistration.test.ts +++ b/tests/boundary/filesystem/windowsNpxRegistration.test.ts @@ -234,5 +234,4 @@ it.skipIf(process.platform !== "win32")( await transport.close(); } }, - 30_000, ); diff --git a/tests/boundary/go/goBinaryImage.test.ts b/tests/boundary/go/goBinaryImage.test.ts index 51eaeb9ab3..b7e0c00467 100644 --- a/tests/boundary/go/goBinaryImage.test.ts +++ b/tests/boundary/go/goBinaryImage.test.ts @@ -285,6 +285,48 @@ it("refuses mapped pointer lengths outside the file instead of rounding 64-bit a expect(() => readGoBinaryImage(length.bytes)).toThrowError(/limit|length/i); }); +it.each(["inline", "pointer"] as const)( + "classifies impossible %s string extents before the decoding allowance", + (encoding) => { + for (const length of [1048577n, 0xffffffffffffffffn]) { + const fixture = createGoBinaryFixture({ encoding }); + if (encoding === "pointer") fixture.bytes.writeBigUInt64LE(length, 1544); + else { + let value = length; + let at = fixture.headerOffset + 32; + do { + const byte = Number(value & 127n); + value >>= 7n; + fixture.bytes[at++] = byte | (value === 0n ? 0 : 128); + } while (value !== 0n); + } + expect(() => readGoBinaryImage(fixture.bytes)).toThrowError( + expect.objectContaining({ kind: "malformed" }), + ); + } + }, +); + +it("validates pointer mapping before a smaller caller allowance", () => { + const fixture = createGoBinaryFixture({ encoding: "pointer" }); + fixture.bytes.writeBigUInt64LE(0x10fffn, 1536); + expect(() => + readGoBinaryImage(fixture.bytes, { budgets: { max_build_info_bytes: 1 } }), + ).toThrowError(expect.objectContaining({ kind: "malformed" })); +}); + +it.each(["inline", "pointer"] as const)( + "preserves resource refusal for physically valid %s strings", + (encoding) => { + const fixture = createGoBinaryFixture({ encoding }); + expect(() => + readGoBinaryImage(fixture.bytes, { + budgets: { max_build_info_bytes: 1 }, + }), + ).toThrowError(BinaryResourceFailure); + }, +); + it.each(["file bytes", "zero-fill"])( "refuses a header-backed PE module descriptor contradicted by %s", (alias) => { diff --git a/tests/boundary/go/goBinaryProvider.test.ts b/tests/boundary/go/goBinaryProvider.test.ts index 2403308689..d6696fde2d 100644 --- a/tests/boundary/go/goBinaryProvider.test.ts +++ b/tests/boundary/go/goBinaryProvider.test.ts @@ -12,6 +12,7 @@ import { projectAnalysisError } from "../../../src/domain/analysisErrorProjectio import { createGoBinaryDiagnosticFixtures, createGoBinaryFixture, + createGoInlineAggregateFixture, createGoOverlappingElfFixture, } from "../../fixtures/go/image.js"; import { createGoRodataStringFixture } from "../../fixtures/go/rodataStringImage.js"; @@ -55,8 +56,7 @@ it.each([32, 64] as const)( ); it("preserves the actual build-info byte guard as a resource failure", async () => { - const fixture = createGoBinaryFixture(); - fixture.bytes.set([0x81, 0x80, 0x40], fixture.headerOffset + 32); + const fixture = createGoInlineAggregateFixture(1024 * 1024 + 2); const root = await createTestTempDirectory("rea-go-resource-"); const path = join(root, "oversized-metadata.elf"); await writeFile(path, fixture.bytes); diff --git a/tests/boundary/gpu/codeObjects.test.ts b/tests/boundary/gpu/codeObjects.test.ts index a57858211b..0a0a73073c 100644 --- a/tests/boundary/gpu/codeObjects.test.ts +++ b/tests/boundary/gpu/codeObjects.test.ts @@ -963,6 +963,16 @@ it.each(["section-names", "symbols"] as const)( ); }, ); +it("preserves non-UTF-8 CUBIN symbol bytes in the kernel name", async () => { + const bytes = cubinFixture(); + const symbolName = bytes.indexOf(Buffer.from("\0add_one\0")); + expect(symbolName).toBeGreaterThanOrEqual(0); + bytes[symbolName + 1] = 0xff; + const result = await inspect(bytes); + if (!result.ok) throw result.error; + expect(result.value.kernels[0]?.name).toBe("ÿdd_one"); +}); + it.each([9, 10])( "preserves parameters and register coverage for text count %d", async (textRegisters) => { diff --git a/tests/boundary/il2cpp/il2cppMetadataProvider.test.ts b/tests/boundary/il2cpp/il2cppMetadataProvider.test.ts index 8051bcadb1..0ee51abb39 100644 --- a/tests/boundary/il2cpp/il2cppMetadataProvider.test.ts +++ b/tests/boundary/il2cpp/il2cppMetadataProvider.test.ts @@ -43,6 +43,35 @@ const inspectMetadata = async (path: string, limit?: number) => { } }; +it("identifies which selected IL2CPP file is missing", async () => { + const root = await createTestTempDirectory("rea-il2cpp-missing-"); + const path = join(root, "global-metadata.dat"); + const binaryPath = join(root, "missing-binary"); + const provider = new Il2cppMetadataProvider(); + try { + const missingMetadata = await provider.inspect({ path }); + if (missingMetadata.ok) + throw new Error("Expected missing metadata failure"); + expect(missingMetadata.error).toMatchObject({ + _tag: "AnalysisInputError", + issues: [{ path: ["path"], reason: "invalid_value" }], + }); + + await writeFile(path, createIl2cppMetadataFixture({ version: 31 }).bytes); + const missingBinary = await provider.inspect({ + path, + binary_path: binaryPath, + }); + if (missingBinary.ok) throw new Error("Expected missing binary failure"); + expect(missingBinary.error).toMatchObject({ + _tag: "AnalysisInputError", + issues: [{ path: ["binary_path"], reason: "invalid_value" }], + }); + } finally { + await provider.close(); + } +}); + /** Synthetic v108 header: checks bounded observations, not record-layout compatibility. */ const version108Header = (stringBytes: Buffer) => { const header = Buffer.alloc(500); diff --git a/tests/boundary/javascript-recovery/wakaruOwnedCommand.test.ts b/tests/boundary/javascript-recovery/wakaruOwnedCommand.test.ts index c3ba0d2685..a5f6d2a503 100644 --- a/tests/boundary/javascript-recovery/wakaruOwnedCommand.test.ts +++ b/tests/boundary/javascript-recovery/wakaruOwnedCommand.test.ts @@ -118,5 +118,4 @@ it.skipIf(process.platform === "win32").each([ } } }, - 15_000, ); diff --git a/tests/boundary/javascript/asyncChunkCapacity.test.ts b/tests/boundary/javascript/asyncChunkCapacity.test.ts new file mode 100644 index 0000000000..1296bd243a --- /dev/null +++ b/tests/boundary/javascript/asyncChunkCapacity.test.ts @@ -0,0 +1,41 @@ +import { createHash } from "node:crypto"; +import { expect, it } from "vitest"; +import { javascriptAsyncChunkCapacity } from "../../fixtures/javascriptAsyncChunkCapacity.js"; +import { analyzeJavaScriptStaticSource } from "../../fixtures/javascriptStaticAnalysis.js"; + +it("retains a wide factory's async keys before normalizing the registration", () => { + // The forked boundary project preserves the default main-thread stack. + const count = 130_000; + const { source, factory } = javascriptAsyncChunkCapacity(count); + const result = analyzeJavaScriptStaticSource(source); + expect(result.parse_status).toBe("partial"); + expect(result.analysis_coverage?.syntax).toEqual({ + status: "complete", + error_count: 0, + }); + expect(result.bundler_registrations).toHaveLength(1); + const registration = result.bundler_registrations[0]; + if (registration === undefined) throw new Error("Missing registration"); + const keys = new Set(registration.async_chunk_keys); + expect(registration.async_chunk_keys).toHaveLength(count + 1); + expect(keys.size).toBe(count + 1); + expect( + Array.from({ length: count }, (_, index) => String(index)).every((key) => + keys.has(key), + ), + ).toBe(true); + expect(registration.async_chunk_keys[0]).toBe("0"); + expect(registration.async_chunk_keys.at(-1)).toBe("later"); + expect(registration.unknown_async_chunk_keys).toBe(1); + expect(registration.modules).toHaveLength(2); + const start = source.indexOf(factory); + expect(registration.modules[0]).toMatchObject({ + module_key: "1", + factory_require_name: "r", + source_sha256: createHash("sha256").update(factory).digest("hex"), + location: { + start: { line: 1, column: start }, + end: { line: 1, column: start + factory.length }, + }, + }); +}); diff --git a/tests/boundary/javascript/javascriptApplicationCancellation.test.ts b/tests/boundary/javascript/javascriptApplicationCancellation.test.ts index 3183738cd6..28ec16b85c 100644 --- a/tests/boundary/javascript/javascriptApplicationCancellation.test.ts +++ b/tests/boundary/javascript/javascriptApplicationCancellation.test.ts @@ -95,6 +95,62 @@ it.each(["worker-phase", "final-cleanup", "no-cancellation"] as const)( }, ); +it("retains worker cleanup when progress reporting fails during analysis", async () => { + const root = await createTestTempDirectory("rea-js-progress-cleanup-"); + await writeFile(join(root, "main.js"), "export const observed = 7;"); + await writeFile(join(root, "second.js"), "export const later = 8;"); + const primary = new Error("progress reporting failed"); + let sourceReports = 0; + let denyRemoval = true; + let runtimePath: string | undefined; + const originalClose = PrivateRuntimeRoot.prototype.close; + const close = vi + .spyOn(PrivateRuntimeRoot.prototype, "close") + .mockImplementation(async function (this: PrivateRuntimeRoot) { + if (basename(this.path).startsWith("rea-javascript-analysis-")) { + runtimePath = this.path; + if (denyRemoval) throw new Error("worker root removal failed"); + } + await originalClose.call(this); + }); + const reuse = vi + .spyOn(JavaScriptAnalysisWorker.prototype, "canReuse") + .mockReturnValue(false); + const scope = new ArtifactResourceScope(); + onTestFinished(async () => { + denyRemoval = false; + try { + await scope.close(); + } finally { + close.mockRestore(); + reuse.mockRestore(); + } + }); + const result = await analyzeOwnedApplication( + { input_path: root, format: "directory" }, + scope, + { + progress: { + async report(event) { + if ( + event.phase === "parse_javascript_source" && + ++sourceReports === 2 + ) + throw primary; + }, + }, + }, + ); + if (result.ok) throw new Error("Expected progress failure"); + expect(runtimePath).toBeDefined(); + expect(result.error.cleanupIncomplete).toBe(true); + expect(result.error.cleanupResources).toContain(runtimePath); + expect(result.error.cause).toMatchObject({ cause: { cause: primary } }); + denyRemoval = false; + await scope.close(); + expect(result.error.cleanupIncomplete).toBe(false); +}); + it.each(["during cleanup", "on the next control turn"] as const)( "retains the final source when cancellation arrives %s after analysis", async (timing) => { diff --git a/tests/boundary/javascript/javascriptSourceAnalysisWorker.test.ts b/tests/boundary/javascript/javascriptSourceAnalysisWorker.test.ts index 2227bbf703..120efbeb61 100644 --- a/tests/boundary/javascript/javascriptSourceAnalysisWorker.test.ts +++ b/tests/boundary/javascript/javascriptSourceAnalysisWorker.test.ts @@ -860,7 +860,7 @@ it("retains legacy routes and a bounded route-fact prefix inside one checkpoint } finally { await worker.close(); } -}, 60000); +}); it("keeps wrapped route ancestry, navigation and unknown named parents in the real worker", async () => { const worker = new JavaScriptAnalysisWorker({ diff --git a/tests/boundary/javascript/nativeTypeBindings.test.ts b/tests/boundary/javascript/nativeTypeBindings.test.ts index 783dc9f9ee..9c73059ee1 100644 --- a/tests/boundary/javascript/nativeTypeBindings.test.ts +++ b/tests/boundary/javascript/nativeTypeBindings.test.ts @@ -87,7 +87,7 @@ it("does not request native-addon members from erased TypeScript names", async ( value.graph.nodes.filter((node) => node.kind === "native-export")[0] ?.observations[0]?.properties.requested_members, ).toEqual(["load"]); -}, 30_000); +}); it.each(["node", "NODE", "Node"])( "keeps path spelling while classifying .%s native requests", @@ -141,5 +141,4 @@ it.each(["node", "NODE", "Node"])( }), ); }, - 30_000, ); diff --git a/tests/boundary/javascript/semanticFingerprintAccumulation.test.ts b/tests/boundary/javascript/semanticFingerprintAccumulation.test.ts new file mode 100644 index 0000000000..7be54c72bc --- /dev/null +++ b/tests/boundary/javascript/semanticFingerprintAccumulation.test.ts @@ -0,0 +1,102 @@ +import { createHash } from "node:crypto"; +import { expect, it } from "vitest"; +import { + createJavaScriptSemanticGraphProjection, + projectJavaScriptSemanticFileSteps, +} from "../../../src/application/javascript/JavaScriptSemanticGraphBuilder.js"; +import { analyzeJavaScriptSemantics } from "../../../src/domain/javascript/javascriptSemanticAnalysis.js"; +import type { JavaScriptArtifactFile } from "../../../src/domain/javascript/javascriptArtifactFiles.js"; +import { resolveJavaScriptSemanticEvidence } from "../../support/javascriptSemanticGraphFixture.js"; + +// This forked project uses the same main-thread stack as the CLI parent. +// These real callable facts exceed V8's call-argument capacity, but fit the +// caller-selectable one-million-node graph ceiling. +const count = 130_000; +const source = "(()=>{});\n".repeat(count); +const sha256 = createHash("sha256").update(source).digest("hex"); +const file: JavaScriptArtifactFile = { + path: "entry.mjs", + container_sha256: sha256, + sha256, + bytes: Buffer.byteLength(source), + inventory_artifact_id: `art_${sha256}`, + kind: "javascript", + unpacked: false, + text: { included: true, value: source }, +}; +// Projection reads the analysis without changing it, so both projection modes +// share one analysis of the wide source instead of repeating it. +let analysis: ReturnType | undefined; +const analyzedSource = () => (analysis ??= analyzeJavaScriptSemantics(source)); + +it.each(["direct", "transferred"] as const)( + "retains an admitted source's wide fingerprint list through %s projection", + (mode) => { + const ir = analyzedSource(); + expect(ir.callables).toHaveLength(count); + expect(ir.functionFingerprints).toHaveLength(count); + const projection = createJavaScriptSemanticGraphProjection({ + totalNodes: count + 1, + fileNodes: count + 1, + }); + if (mode === "direct") projection.projectFile(file, ir); + else { + const steps = projectJavaScriptSemanticFileSteps( + file, + ir, + projection.remainingFileCapacity(), + ); + let step = steps.next(); + while (!step.done) step = steps.next(); + projection.adoptFileProjection(step.value); + } + const graph = projection.finish(sha256, { + graph_id: `jag_${sha256}`, + nodes: [], + }); + const functions = graph.nodes.filter(({ kind }) => kind === "function"); + expect(functions).toHaveLength(count); + expect(graph.fingerprints).toHaveLength(count); + // The graph sorts nodes and fingerprints by their independent digests. + const functionIds = new Set(functions.map(({ node_id }) => node_id)); + expect( + graph.fingerprints.every(({ function_node_id }) => + functionIds.has(function_node_id), + ), + ).toBe(true); + expect( + new Set( + graph.fingerprints.map(({ function_node_id }) => function_node_id), + ).size, + ).toBe(count); + expect( + new Set(graph.fingerprints.map(({ fingerprint_id }) => fingerprint_id)) + .size, + ).toBe(count); + expect(graph.coverage.truncated).toBe(false); + for (const line of [1, count]) { + const callable = functions.find( + ({ identity }) => identity.source_range?.start.line === line, + ); + const fingerprint = graph.fingerprints.find( + ({ function_node_id }) => function_node_id === callable?.node_id, + ); + if (fingerprint === undefined) throw new Error("Missing fingerprint"); + expect( + resolveJavaScriptSemanticEvidence(graph, fingerprint.evidence), + ).toMatchObject({ + state: "observed", + authority: "ast-static-analysis", + artifact: { available: true, sha256 }, + location: { + available: true, + value: { + kind: "source-range", + source: "entry.mjs", + start: { line }, + }, + }, + }); + } + }, +); diff --git a/tests/boundary/javascript/wideExportBindings.test.ts b/tests/boundary/javascript/wideExportBindings.test.ts new file mode 100644 index 0000000000..c54cf35718 --- /dev/null +++ b/tests/boundary/javascript/wideExportBindings.test.ts @@ -0,0 +1,25 @@ +import { expect, it } from "vitest"; + +import { analyzeCommonJsStaticFixture } from "../../fixtures/javascriptCommonJsAnalysis.js"; + +// The source analyzer runs on a child process's main-thread stack. This forked +// project shares that stack size; worker threads have a larger default stack. +it("selects a published binding list wider than call arguments allow", () => { + const publish = (count: number) => { + const bindings = Array.from({ length: count }, (_, i) => `v${i}`); + const source = `const{app}=require('electron');export const[${bindings.join(",")}]=[0,app.setAppPath];`; + const analysis = analyzeCommonJsStaticFixture(source); + expect(analysis.parse_error_count).toBe(0); + // The possible change is the exported declaration, which ends the source. + return (analysis.electron.app_path_changes ?? []).map( + ({ location, ...change }) => { + expect(location.end).toEqual({ line: 1, column: source.length }); + return { ...change, start: location.start }; + }, + ); + }; + const control = publish(3); + expect(control).toHaveLength(1); + // More bindings than one call can receive as arguments on V8's default stack. + expect(publish(130_000)).toEqual(control); +}); diff --git a/tests/boundary/mcp/absoluteLocalPathSchema.test.ts b/tests/boundary/mcp/absoluteLocalPathSchema.test.ts index 329daf21c8..436124fd08 100644 --- a/tests/boundary/mcp/absoluteLocalPathSchema.test.ts +++ b/tests/boundary/mcp/absoluteLocalPathSchema.test.ts @@ -353,3 +353,75 @@ it("rejects relative paths through in-memory MCP handlers", async () => { await close(); } }); + +// advertisedPatternsVMode.test.ts proves no schema profile advertises the +// surrogate escape; this case proves the handlers still refuse such paths. +it("rejects unpaired surrogates through in-memory MCP handlers", async () => { + const firmwareCalls: string[] = []; + const { client, close } = await createMcp(firmwareCalls); + try { + const absolute = resolve("rea-absolute-input"); + const cases = inputCases(absolute, `${absolute}\ud800`); + const checked: string[] = []; + for (const { name, field, invalid, issuePath } of cases) { + const result = await client.callTool({ name, arguments: invalid }); + expect(result.isError, `${name}.${field}`).toBe(true); + expect(parseMcpToolError(result).error, `${name}.${field}`).toMatchObject( + { + code: "invalid_request", + details: { + operation: name, + issues: [ + { + path: issuePath, + reason: "invalid_format", + expected: "regex", + message: + "Local filesystem paths must contain well-formed Unicode", + }, + ], + }, + }, + ); + checked.push(`${name}.${field}`); + } + expect(checked).toEqual( + expect.arrayContaining([ + "open_binary.path", + "open_binary.snapshot_path", + "extract_firmware.path", + "extract_firmware.output_directory", + "inspect_analysis_view.source.path", + "capture_browser_scenario.browser.executable_path", + "capture_electron_scenario.application_root", + ]), + ); + + // Disassembly symbols share the rule and its correction shape. + const symbol = await client.callTool({ + name: "inspect_with_objdump", + arguments: { + path: absolute, + operation: "disassemble", + symbol: "main\ud800", + }, + }); + expect(symbol.isError).toBe(true); + expect(parseMcpToolError(symbol).error).toMatchObject({ + code: "invalid_request", + details: { + issues: [ + { + path: ["symbol"], + reason: "invalid_format", + expected: "regex", + message: "Symbol must be well-formed Unicode.", + }, + ], + }, + }); + expect(firmwareCalls).toEqual([]); + } finally { + await close(); + } +}); diff --git a/tests/boundary/mcp/advertisedPatternsVMode.test.ts b/tests/boundary/mcp/advertisedPatternsVMode.test.ts index 1291d79d9a..022b43d673 100644 --- a/tests/boundary/mcp/advertisedPatternsVMode.test.ts +++ b/tests/boundary/mcp/advertisedPatternsVMode.test.ts @@ -64,11 +64,16 @@ const collectPatterns = ( } }; -it("advertises portable NUL escapes and patterns that compile in all JS modes", async () => { +const listAdvertisedTools = async ( + environment?: Readonly, +) => { const session = createTestBinarySession(() => { throw new Error("No deep provider may start for a schema projection"); }); - const server = createServer({ kind: "session", session }); + const server = createServer( + { kind: "session", session }, + environment === undefined ? {} : { environment }, + ); const client = new Client({ name: "advertised-patterns", version: "1" }); onTestFinished(async () => { await client.close(); @@ -79,8 +84,20 @@ it("advertises portable NUL escapes and patterns that compile in all JS modes", InMemoryTransport.createLinkedPair(); await server.connect(serverTransport); await client.connect(clientTransport); + return (await client.listTools()).tools; +}; - const advertised = (await client.listTools()).tools; +// A `\u` or `\0` escape that is not itself escaped. RE2 has no `\u` escape, +// Rust refuses `\u` surrogate ranges, and DeepSeek rejects the short NUL escape +// even though V8 accepts all of them; any one refuses the complete tools +// request. Unlike the compile checks above, this guard covers every profile. +const NON_PORTABLE_ESCAPE = /(?:^|[^\\])(?:\\\\)*\\[0u]/u; + +const nonPortableEscapes = (patterns: readonly AdvertisedPattern[]) => + patterns.filter(({ pattern }) => NON_PORTABLE_ESCAPE.test(pattern)); + +it("advertises portable NUL escapes and patterns that compile in all JS modes", async () => { + const advertised = await listAdvertisedTools(); const patterns: AdvertisedPattern[] = []; const canonicalBase64Leaves: Record[] = []; for (const tool of advertised) { @@ -129,14 +146,9 @@ it("advertises portable NUL escapes and patterns that compile in all JS modes", patterns.filter(({ pattern }) => /\(\? pattern.includes("\\0") || pattern.includes("\\u0000"), - ), - ).toEqual([]); + // Keep the shared hex NUL spelling across the advertised catalog; actual + // provider acceptance is verified separately from JS compilation. + expect(nonPortableEscapes(patterns)).toEqual([]); expect(patterns.some(({ pattern }) => pattern.includes("\\x00"))).toBe(true); expect(canonicalBase64Leaves.length).toBeGreaterThan(0); @@ -154,3 +166,29 @@ it("advertises portable NUL escapes and patterns that compile in all JS modes", ), ); }); + +it.each( + (["compact", "full"] as const).flatMap((input) => + (["full", "compact", "off"] as const).map((output) => [input, output]), + ), +)( + "advertises portable escapes for %s input and %s output schemas", + async (input, output) => { + const advertised = await listAdvertisedTools({ + ...process.env, + REA_MCP_INPUT_SCHEMA_PROFILE: input, + REA_MCP_OUTPUT_SCHEMA_PROFILE: output, + }); + const patterns: AdvertisedPattern[] = []; + for (const tool of advertised) { + collectPatterns(tool.inputSchema, "inputSchema", tool.name, patterns); + if (tool.outputSchema !== undefined) + collectPatterns(tool.outputSchema, "outputSchema", tool.name, patterns); + } + // Every profile carries local path inputs, so the guard is not vacuous. + expect(patterns.some(({ pattern }) => pattern.includes("\\x00"))).toBe( + true, + ); + expect(nonPortableEscapes(patterns)).toEqual([]); + }, +); diff --git a/tests/boundary/mcp/analysisViewMcp.test.ts b/tests/boundary/mcp/analysisViewMcp.test.ts index 27e21db721..fe2e4f4d08 100644 --- a/tests/boundary/mcp/analysisViewMcp.test.ts +++ b/tests/boundary/mcp/analysisViewMcp.test.ts @@ -210,7 +210,7 @@ it("selects every IL2CPP native type reference by rendered name with stable row it("preserves native relationship coverage through inline and retained MCP views", async () => { const { client, session } = await connect(); - const { ajv, outputSchema } = await advertisedMcpToolSchemas( + const { toolDefinition } = await advertisedMcpToolSchemas( client, "inspect_analysis_view", schemaValidation.assertions, @@ -246,18 +246,17 @@ it("preserves native relationship coverage through inline and retained MCP views { kind: "inline", evidence: parent }, { kind: "retained-evidence", evidence_id: parent.evidence_id }, ]) { - const response = await client.callTool({ - name: "inspect_analysis_view", - arguments: { - source, - view: { kind: "native", facet: "callees", offset: 0, limit: 1 }, + const response = await client.callTool( + { + name: "inspect_analysis_view", + arguments: { + source, + view: { kind: "native", facet: "callees", offset: 0, limit: 1 }, + }, }, - }); + { toolDefinition }, + ); expect(response.isError).not.toBe(true); - expect( - ajv.validate(outputSchema, response.structuredContent), - JSON.stringify(ajv.errors), - ).toBe(true); const evidence = parseEvidence(response.structuredContent); expect(evidence.relationship_coverage).toEqual( parent.relationship_coverage, @@ -344,7 +343,7 @@ it("advertises typed module references and preserves portable/retained Evidence ); const recorded = session.recordEvidence(parent); if (!recorded.ok) throw recorded.error; - const { ajv, inputSchema, outputSchema } = await advertisedMcpToolSchemas( + const { ajv, inputSchema, toolDefinition } = await advertisedMcpToolSchemas( client, "inspect_analysis_view", schemaValidation.assertions, @@ -368,15 +367,11 @@ it("advertises typed module references and preserves portable/retained Evidence view: { ...view, where: { glob: "*" } }, }), ).toBe(false); - const response = await client.callTool({ - name: "inspect_analysis_view", - arguments: { source, view }, - }); + const response = await client.callTool( + { name: "inspect_analysis_view", arguments: { source, view } }, + { toolDefinition }, + ); expect(response.isError).not.toBe(true); - expect( - ajv.validate(outputSchema, response.structuredContent), - JSON.stringify(ajv.errors), - ).toBe(true); const evidence = parseEvidence(response.structuredContent); expect(evidence).toMatchObject({ evidence_links: [parent.evidence_id], @@ -596,7 +591,7 @@ it("advertises exact schemas and projects one layout section from retained Evide inspect: () => Promise.resolve(ok(layout)), }); const { client, session } = await connect(service); - const { ajv, inputSchema, outputSchema } = await advertisedMcpToolSchemas( + const { ajv, inputSchema, toolDefinition } = await advertisedMcpToolSchemas( client, "inspect_analysis_view", schemaValidation.assertions, @@ -630,25 +625,24 @@ it("advertises exact schemas and projects one layout section from retained Evide const parent = toolContract("inspect_binary_layout").outputSchema.parse( inspected.structuredContent, ); - const response = await client.callTool({ - name: "inspect_analysis_view", - arguments: { - source: { - kind: "retained-evidence", - evidence_id: parent.evidence_id, - }, - view: { - kind: "item", - collection: "sections", - selector: { name: ".data" }, + const response = await client.callTool( + { + name: "inspect_analysis_view", + arguments: { + source: { + kind: "retained-evidence", + evidence_id: parent.evidence_id, + }, + view: { + kind: "item", + collection: "sections", + selector: { name: ".data" }, + }, }, }, - }); + { toolDefinition }, + ); expect(response.isError).not.toBe(true); - expect( - ajv.validate(outputSchema, response.structuredContent), - JSON.stringify(ajv.errors), - ).toBe(true); const parsed = toolContract("inspect_analysis_view").outputSchema.parse( response.structuredContent, ); @@ -997,15 +991,11 @@ it("delivers a bounded native dossier view over MCP without a Ghidra provider or ajv.validate(inputSchema, arguments_), JSON.stringify(ajv.errors), ).toBe(true); - const response = await client.callTool({ - name: "inspect_analysis_view", - arguments: arguments_, - }); + const response = await client.callTool( + { name: "inspect_analysis_view", arguments: arguments_ }, + { toolDefinition: advertised }, + ); expect(response.isError).not.toBe(true); - expect( - ajv.validate(outputSchema, response.structuredContent), - JSON.stringify(ajv.errors), - ).toBe(true); const parsed = toolContract("inspect_analysis_view").outputSchema.parse( response.structuredContent, ); @@ -1542,7 +1532,7 @@ it.each([false, true])( it("advertises unserved learned clock rules when a retained capture has no global clock", async () => { const { client, session } = await connect(); - const { ajv, outputSchema } = await advertisedMcpToolSchemas( + const { toolDefinition } = await advertisedMcpToolSchemas( client, "inspect_analysis_view", schemaValidation.assertions, @@ -1558,18 +1548,20 @@ it("advertises unserved learned clock rules when a retained capture has no globa for (const parent of parents) { const original = structuredClone(parent); expect(session.recordEvidence(parent).ok).toBe(true); - const response = await client.callTool({ - name: "inspect_analysis_view", - arguments: { - source: { kind: "retained-evidence", evidence_id: parent.evidence_id }, - view: { kind: "environment-state" }, + const response = await client.callTool( + { + name: "inspect_analysis_view", + arguments: { + source: { + kind: "retained-evidence", + evidence_id: parent.evidence_id, + }, + view: { kind: "environment-state" }, + }, }, - }); + { toolDefinition }, + ); expect(response.isError, JSON.stringify(response.content)).not.toBe(true); - expect( - ajv.validate(outputSchema, response.structuredContent), - JSON.stringify(ajv.errors), - ).toBe(true); const state = analysisViewResultSchema.parse( parseEvidence(response.structuredContent).normalized_result, ); @@ -1605,7 +1597,7 @@ it("advertises unserved learned clock rules when a retained capture has no globa it("advertises Mach-O pages whose selected facet owns their exact rows", async () => { const { client, session } = await connect(); - const { ajv, outputSchema } = await advertisedMcpToolSchemas( + const { ajv, outputSchema, toolDefinition } = await advertisedMcpToolSchemas( client, "inspect_analysis_view", schemaValidation.assertions, @@ -1690,21 +1682,20 @@ it("advertises Mach-O pages whose selected facet owns their exact rows", async ( "encryption", ] as const) { for (const offset of [0, 999]) { - const response = await client.callTool({ - name: "inspect_analysis_view", - arguments: { - source: { - kind: "retained-evidence", - evidence_id: parent.evidence_id, + const response = await client.callTool( + { + name: "inspect_analysis_view", + arguments: { + source: { + kind: "retained-evidence", + evidence_id: parent.evidence_id, + }, + view: { kind: "macho", facet, offset, limit: 64 }, }, - view: { kind: "macho", facet, offset, limit: 64 }, }, - }); + { toolDefinition }, + ); expect(response.isError, JSON.stringify(response.content)).not.toBe(true); - expect( - ajv.validate(outputSchema, response.structuredContent), - JSON.stringify(ajv.errors), - ).toBe(true); const delivered = parseEvidence(response.structuredContent); const page = analysisViewResultSchema.parse(delivered.normalized_result); if (page.kind !== "macho") throw new Error("Expected Mach-O page"); @@ -1744,7 +1735,7 @@ it("advertises Mach-O pages whose selected facet owns their exact rows", async ( it("extracts an environment state identically from retained, portable and file MCP Evidence", async () => { // Full inline capture conformance explicitly selects its transport allowance. const { client, session } = await connect(undefined, undefined, 8_192); - const { ajv, inputSchema, outputSchema } = await advertisedMcpToolSchemas( + const { ajv, inputSchema, toolDefinition } = await advertisedMcpToolSchemas( client, "inspect_analysis_view", schemaValidation.assertions, @@ -1768,15 +1759,14 @@ it("extracts an environment state identically from retained, portable and file M { kind: "evidence-file", path: evidencePath }, ]) { expect(ajv.validate(inputSchema, { source, view })).toBe(true); - const response = await client.callTool({ - name: "inspect_analysis_view", - arguments: { source, view }, - }); + const response = await client.callTool( + { + name: "inspect_analysis_view", + arguments: { source, view }, + }, + { toolDefinition }, + ); expect(response.isError).not.toBe(true); - expect( - ajv.validate(outputSchema, response.structuredContent), - JSON.stringify(ajv.errors), - ).toBe(true); const evidence = parseEvidence(response.structuredContent); expect(evidence.confidence).toBe("inferred"); expect(evidence.normalized_result).toMatchObject({ @@ -1822,7 +1812,7 @@ it("extracts an environment state identically from retained, portable and file M it("reports environment coverage identically from retained, portable and file MCP Evidence", async () => { const { client, session } = await connect(undefined, undefined, 8_192); - const { ajv, inputSchema, outputSchema } = await advertisedMcpToolSchemas( + const { ajv, inputSchema, toolDefinition } = await advertisedMcpToolSchemas( client, "inspect_analysis_view", schemaValidation.assertions, @@ -1843,15 +1833,14 @@ it("reports environment coverage identically from retained, portable and file MC ]) { const view = { kind: "environment-coverage", template_rules: rules }; expect(ajv.validate(inputSchema, { source, view })).toBe(true); - const response = await client.callTool({ - name: "inspect_analysis_view", - arguments: { source, view }, - }); + const response = await client.callTool( + { + name: "inspect_analysis_view", + arguments: { source, view }, + }, + { toolDefinition }, + ); expect(response.isError).not.toBe(true); - expect( - ajv.validate(outputSchema, response.structuredContent), - JSON.stringify(ajv.errors), - ).toBe(true); const evidence = parseEvidence(response.structuredContent); expect(evidence.confidence).toBe("inferred"); expect(evidence.normalized_result).toMatchObject({ @@ -1868,7 +1857,7 @@ it("reports environment coverage identically from retained, portable and file MC it("accounts for every safe HTTP method in environment state and coverage", async () => { const { client } = await connect(); - const { ajv, outputSchema } = await advertisedMcpToolSchemas( + const { toolDefinition } = await advertisedMcpToolSchemas( client, "inspect_analysis_view", schemaValidation.assertions, @@ -1896,18 +1885,17 @@ it("accounts for every safe HTTP method in environment state and coverage", asyn "environment-state", "environment-coverage", ]) { - const response = await client.callTool({ - name: "inspect_analysis_view", - arguments: { - source: { kind: "inline", evidence: parent }, - view: { kind, template_rules: [] }, + const response = await client.callTool( + { + name: "inspect_analysis_view", + arguments: { + source: { kind: "inline", evidence: parent }, + view: { kind, template_rules: [] }, + }, }, - }); + { toolDefinition }, + ); expect(response.isError).not.toBe(true); - expect( - ajv.validate(outputSchema, response.structuredContent), - JSON.stringify(ajv.errors), - ).toBe(true); results.push(parseEvidence(response.structuredContent).normalized_result); } expect(results[0]).toMatchObject({ @@ -1934,7 +1922,7 @@ it("accounts for every safe HTTP method in environment state and coverage", asyn it("projects an environment manifest identically from retained, portable and file MCP Evidence", async () => { const { client, session } = await connect(undefined, undefined, 8_192); - const { ajv, inputSchema, outputSchema } = await advertisedMcpToolSchemas( + const { ajv, inputSchema, toolDefinition } = await advertisedMcpToolSchemas( client, "inspect_analysis_view", schemaValidation.assertions, @@ -1971,15 +1959,14 @@ it("projects an environment manifest identically from retained, portable and fil view: { ...view, template_rules: ["glob"] }, }), ).toBe(false); - const response = await client.callTool({ - name: "inspect_analysis_view", - arguments: { source, view }, - }); + const response = await client.callTool( + { + name: "inspect_analysis_view", + arguments: { source, view }, + }, + { toolDefinition }, + ); expect(response.isError).not.toBe(true); - expect( - ajv.validate(outputSchema, response.structuredContent), - JSON.stringify(ajv.errors), - ).toBe(true); const evidence = parseEvidence(response.structuredContent); expect(evidence.confidence).toBe("inferred"); expect(evidence.normalized_result).toMatchObject({ @@ -2167,7 +2154,7 @@ it.each([ "pages a recorded row when its parent parameters do not fit", async ({ queryBytes, rowBytes }) => { const { client, session } = await connect(); - const { ajv, outputSchema } = await advertisedMcpToolSchemas( + const { toolDefinition } = await advertisedMcpToolSchemas( client, "inspect_analysis_view", schemaValidation.assertions, @@ -2189,18 +2176,25 @@ it.each([ }, ); expect(session.recordEvidence(parent).ok).toBe(true); - const response = await client.callTool({ - name: "inspect_analysis_view", - arguments: { - source: { kind: "retained-evidence", evidence_id: parent.evidence_id }, - view: { kind: "recorded-array", path: ["values"], offset: 0, limit: 1 }, + const response = await client.callTool( + { + name: "inspect_analysis_view", + arguments: { + source: { + kind: "retained-evidence", + evidence_id: parent.evidence_id, + }, + view: { + kind: "recorded-array", + path: ["values"], + offset: 0, + limit: 1, + }, + }, }, - }); + { toolDefinition }, + ); expect(response.isError).not.toBe(true); - expect( - ajv.validate(outputSchema, response.structuredContent), - JSON.stringify(ajv.errors), - ).toBe(true); const parsed = toolContract("inspect_analysis_view").outputSchema.parse( response.structuredContent, ); diff --git a/tests/boundary/mcp/androidDescriptorAdmissionMcp.test.ts b/tests/boundary/mcp/androidDescriptorAdmissionMcp.test.ts index d9ac95dfe4..dbbe215650 100644 --- a/tests/boundary/mcp/androidDescriptorAdmissionMcp.test.ts +++ b/tests/boundary/mcp/androidDescriptorAdmissionMcp.test.ts @@ -29,6 +29,13 @@ it.each(["compact", "full"])( const ajv = new Ajv2020({ strict: false, validateFormats: false }); expect(ajv.validateSchema(definition.inputSchema)).toBe(true); const validate = ajv.compile(definition.inputSchema); + // Compact delivery keeps validation keywords for a schema within its byte + // budget and omits nested detail only from a reduced schema, so the + // advertised dimension bound depends on the presentation, not the profile. + const advertisesDimensionBound = JSON.stringify( + definition.inputSchema, + ).includes("[{0,255}"); + expect(profile === "compact" || advertisesDimensionBound).toBe(true); for (const dimensions of [255, 256]) { const type = "[".repeat(dimensions) + "I"; const request = { @@ -36,8 +43,7 @@ it.each(["compact", "full"])( descriptors: [`(${type})${type}`], }; expect(validate(request), JSON.stringify(validate.errors)).toBe( - // Compact delivery intentionally omits scalar pattern constraints. - profile === "compact" || dimensions === 255, + dimensions === 255 || !advertisesDimensionBound, ); if (dimensions === 256) { const result = await client.callTool({ diff --git a/tests/boundary/mcp/androidPackageSummaryMcp.test.ts b/tests/boundary/mcp/androidPackageSummaryMcp.test.ts index 831fbdb6b9..b9eb64f367 100644 --- a/tests/boundary/mcp/androidPackageSummaryMcp.test.ts +++ b/tests/boundary/mcp/androidPackageSummaryMcp.test.ts @@ -1,6 +1,10 @@ import { unlink } from "node:fs/promises"; import { join } from "node:path"; -import { Client, InMemoryTransport } from "@modelcontextprotocol/client"; +import { + Client, + InMemoryTransport, + type CallToolRequestOptions, +} from "@modelcontextprotocol/client"; import { Ajv2020 } from "ajv/dist/2020.js"; import { expect, it, onTestFinished, vi } from "vitest"; import { AndroidAnalysisService } from "../../../src/application/android/AndroidAnalysisService.js"; @@ -38,29 +42,27 @@ import { async function assertAndroidPackageSelectedFacetContract(options: { client: Pick; - ajv: Ajv2020; - outputSchema: Record; + toolDefinition: NonNullable; apk: string; parent: Evidence; retained: (id: string) => Evidence | undefined; sourceStatus: string; }): Promise { - const { client, ajv, outputSchema, apk, parent, retained, sourceStatus } = + const { client, toolDefinition, apk, parent, retained, sourceStatus } = options; for (const facet of ["components", "component-summary"] as const) { - const selected = await client.callTool({ - name: "inspect_android_package", - arguments: { - path: apk, - detail: "summary", - view: { kind: "android", facet }, + const selected = await client.callTool( + { + name: "inspect_android_package", + arguments: { + path: apk, + detail: "summary", + view: { kind: "android", facet }, + }, }, - }); + { toolDefinition }, + ); expect(selected.isError).not.toBe(true); - expect( - ajv.validate(outputSchema, selected.structuredContent), - JSON.stringify(ajv.errors), - ).toBe(true); const result = parseEvidence(selected.structuredContent).normalized_result; const projected = facet === "components" @@ -185,22 +187,17 @@ it("defaults JNI MCP inspection to a retained summary while preserving explicit ); if (definition?.outputSchema === undefined) throw new Error("Missing JNI output schema"); - const outputSchema: Record = definition.outputSchema; const ajv = new Ajv2020({ strict: false, validateFormats: false }); const request = { path: apk, ...selection }; expect( ajv.validate(definition.inputSchema, request), JSON.stringify(ajv.errors), ).toBe(true); - const compact = await client.callTool({ - name: "inspect_android_jni_bindings", - arguments: request, - }); + const compact = await client.callTool( + { name: "inspect_android_jni_bindings", arguments: request }, + { toolDefinition: definition }, + ); expect(compact.isError).not.toBe(true); - expect( - ajv.validate(outputSchema, compact.structuredContent), - JSON.stringify(ajv.errors), - ).toBe(true); const summaryEvidence = parseEvidence(compact.structuredContent); const summary = androidJniSummaryViewResultSchema.parse( summaryEvidence.normalized_result, @@ -226,15 +223,14 @@ it("defaults JNI MCP inspection to a retained summary while preserving explicit expect(fullReport.declarations).toEqual(declarations); expect(parent.parameters).toEqual(request); expect(summaryEvidence.evidence_links).toEqual([parent.evidence_id]); - const complete = await client.callTool({ - name: "inspect_android_jni_bindings", - arguments: { ...request, detail: "complete" }, - }); + const complete = await client.callTool( + { + name: "inspect_android_jni_bindings", + arguments: { ...request, detail: "complete" }, + }, + { toolDefinition: definition }, + ); expect(complete.isError).not.toBe(true); - expect( - ajv.validate(outputSchema, complete.structuredContent), - JSON.stringify(ajv.errors), - ).toBe(true); const completeEvidence = parseEvidence(complete.structuredContent); expect(completeEvidence.normalized_result).toEqual(fullReport); expect(completeEvidence.raw_result).toEqual(raw); @@ -309,15 +305,14 @@ it.skipIf(process.platform === "win32")( }, ), ).toBe(true); - const reply = await client.callTool({ - name: "inspect_android_package", - arguments: { path: apk, detail: "summary" }, - }); + const reply = await client.callTool( + { + name: "inspect_android_package", + arguments: { path: apk, detail: "summary" }, + }, + { toolDefinition: definition }, + ); expect(reply.isError).not.toBe(true); - expect( - ajv.validate(outputSchema, reply.structuredContent), - JSON.stringify(ajv.errors), - ).toBe(true); const receipt = parseEvidence(reply.structuredContent); const view = androidPackageSummaryViewResultSchema.parse( receipt.normalized_result, @@ -363,8 +358,7 @@ it.skipIf(process.platform === "win32")( } await assertAndroidPackageSelectedFacetContract({ client, - ajv, - outputSchema, + toolDefinition: definition, apk, parent, retained: (id) => session.evidenceById(id), @@ -415,7 +409,6 @@ it.skipIf(process.platform === "win32")( await harness.close(); await verifyJadxFixtureCleanup(launches); }, - 30000, ); it("preserves partial/unknown declarations and Unicode text while rejecting missing APK identity and malformed source", async () => { diff --git a/tests/boundary/mcp/appleIpsMcp.test.ts b/tests/boundary/mcp/appleIpsMcp.test.ts index f3c2a4023c..60d7c0f26a 100644 --- a/tests/boundary/mcp/appleIpsMcp.test.ts +++ b/tests/boundary/mcp/appleIpsMcp.test.ts @@ -46,10 +46,6 @@ it("advertises and validates IPS facts and failures through production stdio MCP }); const response = await call("inspect_apple_ips", { path }); expect(response.isError, JSON.stringify(response.content)).not.toBe(true); - expect( - ajv.validate(outputSchema, response.structuredContent), - JSON.stringify(ajv.errors), - ).toBe(true); const evidence = parseEvidence(response.structuredContent); const report = appleIpsSchema.parse(evidence.normalized_result); expect(evidence.operation).toBe("inspect_apple_ips"); @@ -101,10 +97,6 @@ it("advertises and validates IPS facts and failures through production stdio MCP await writeFile(malformedPath, appleIpsBytes({ uuid: "malformed/report" })); const malformed = await call("inspect_apple_ips", { path: malformedPath }); expect(malformed.isError, JSON.stringify(malformed.content)).not.toBe(true); - expect( - ajv.validate(outputSchema, malformed.structuredContent), - JSON.stringify(ajv.errors), - ).toBe(true); const partial = appleIpsSchema.parse( parseEvidence(malformed.structuredContent).normalized_result, ); @@ -126,10 +118,6 @@ it("advertises and validates IPS facts and failures through production stdio MCP ); const indexes = await call("inspect_apple_ips", { path: indexPath }); expect(indexes.isError, JSON.stringify(indexes.content)).not.toBe(true); - expect( - ajv.validate(outputSchema, indexes.structuredContent), - JSON.stringify(ajv.errors), - ).toBe(true); const unresolved = appleIpsSchema.parse( parseEvidence(indexes.structuredContent).normalized_result, ); diff --git a/tests/boundary/mcp/applicationExportShapeMcp.test.ts b/tests/boundary/mcp/applicationExportShapeMcp.test.ts index a3c8cfe00a..2146aed223 100644 --- a/tests/boundary/mcp/applicationExportShapeMcp.test.ts +++ b/tests/boundary/mcp/applicationExportShapeMcp.test.ts @@ -474,7 +474,7 @@ ${returnSites} left_omitted_fields: 0, right_omitted_fields: 0, }); - }, 30_000); + }); }); const analyzeSourceEvidence = async (source: string) => { diff --git a/tests/boundary/mcp/applicationMcpHarnessLifecycle.test.ts b/tests/boundary/mcp/applicationMcpHarnessLifecycle.test.ts index 0003844932..27422118c4 100644 --- a/tests/boundary/mcp/applicationMcpHarnessLifecycle.test.ts +++ b/tests/boundary/mcp/applicationMcpHarnessLifecycle.test.ts @@ -16,6 +16,7 @@ import { createTestBinarySession, } from "../../fixtures/binarySession.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; +import { awaitAcknowledgement } from "../../fixtures/lifecycleAcknowledgement.js"; const failureLeaves = (reason: unknown): unknown[] => reason instanceof AggregateError @@ -80,7 +81,7 @@ it("reports a failed session close, joins concurrent attempts and retries the re harness = acquired.harness; const closing = harness.close(); const outcome = closing.catch((reason: unknown) => reason); - await entered.promise; + await awaitAcknowledgement(entered.promise, outcome, "provider close"); const concurrent = harness.close(); const concurrentOutcome = concurrent.catch((reason: unknown) => reason); expect(closeProvider).toHaveBeenCalledTimes(1); @@ -90,7 +91,6 @@ it("reports a failed session close, joins concurrent attempts and retries the re if (!(reason instanceof AggregateError)) throw new Error("Harness ignored the session close failure"); expect(reason.errors).toContain(failure); - expect(concurrent).toBe(closing); expect(await concurrentOutcome).toBe(reason); expect(failure.cleanupIncomplete).toBe(true); expect(await harness.session.status()).toMatchObject({ diff --git a/tests/boundary/mcp/artifactComparisonIntegrityMcp.test.ts b/tests/boundary/mcp/artifactComparisonIntegrityMcp.test.ts new file mode 100644 index 0000000000..f12040ffd7 --- /dev/null +++ b/tests/boundary/mcp/artifactComparisonIntegrityMcp.test.ts @@ -0,0 +1,255 @@ +import { expect, it, onTestFinished } from "vitest"; + +import { ARTIFACT_COMPARISON_EXAMPLE } from "../../../src/contracts/artifactComparisonExample.js"; +import { compareArtifacts } from "../../../src/domain/artifactComparison.js"; +import { createEvidence } from "../../../src/domain/evidence.js"; + +import { parseMcpToolError } from "../../fixtures/mcpToolError.js"; +import { createApplicationMcpHarness } from "../../fixtures/applicationMcpHarness.js"; +import { createTestBinarySession } from "../../fixtures/binarySession.js"; + +it("rejects an unchanged artifact comparison that retains a changed row", async () => { + const left = ARTIFACT_COMPARISON_EXAMPLE.left; + const right = ARTIFACT_COMPARISON_EXAMPLE.right; + const comparison = createEvidence( + undefined, + { + id: "rea-artifact-comparison", + name: "REA artifact comparison", + version: "1", + }, + { + predicateType: "rea.artifact-comparison", + operation: "compare_artifacts", + parameters: { + left_evidence_ids: [left.evidence_id], + right_evidence_ids: [right.evidence_id], + }, + result: { + ...compareArtifacts(left, right), + status: "unchanged", + }, + confidence: "derived", + authority: "analyst-inference", + evidenceLinks: [left.evidence_id, right.evidence_id], + }, + ); + const session = createTestBinarySession(() => { + throw new Error( + "Retained comparison verification must not launch a provider", + ); + }); + const harness = await createApplicationMcpHarness({ session }); + onTestFinished(harness.close); + for (const record of [left, right, comparison]) + expect(session.recordEvidence(record).ok).toBe(true); + + const response = await harness.client.callTool({ + name: "verify_reconstruction", + arguments: { + specification: { + name: "Artifact structure", + claims: [ + { + kind: "structural-artifact", + claim_id: "artifact-graph", + title: "Artifact graph remains equal", + comparison_evidence_id: comparison.evidence_id, + dimension: "overall", + }, + ], + }, + }, + }); + + expect(response.isError).toBe(true); + expect(parseMcpToolError(response)).toMatchObject({ + error: { + code: "evidence_integrity_mismatch", + category: "integrity_mismatch", + }, + }); + expect(session.evidenceById(comparison.evidence_id)).toEqual(comparison); +}); + +it("does not fail a changed aggregate when source inventories are equal", async () => { + const left = ARTIFACT_COMPARISON_EXAMPLE.left; + const right = createEvidence( + { path: "independent-copy", sha256: "0".repeat(64), format: "file" }, + left.provider, + { + operation: "inventory_artifact", + parameters: { path: "independent-copy" }, + result: left.normalized_result, + confidence: "observed", + authority: "shipped-artifact", + }, + ); + const equal = compareArtifacts(left, right); + expect(equal.status).toBe("unchanged"); + const changed = compareArtifacts(left, ARTIFACT_COMPARISON_EXAMPLE.right); + const comparison = createEvidence( + undefined, + { + id: "rea-artifact-comparison", + name: "REA artifact comparison", + version: "1", + }, + { + predicateType: "rea.artifact-comparison", + operation: "compare_artifacts", + parameters: { + left_evidence_ids: [left.evidence_id], + right_evidence_ids: [right.evidence_id], + }, + result: { + ...changed, + left_manifest_id: equal.left_manifest_id, + right_manifest_id: equal.right_manifest_id, + changes: changed.changes.map((row) => ({ + ...row, + evidence_links: [left.evidence_id, right.evidence_id], + })), + }, + confidence: "derived", + authority: "analyst-inference", + evidenceLinks: [left.evidence_id, right.evidence_id], + }, + ); + const session = createTestBinarySession(() => { + throw new Error("No provider expected"); + }); + const harness = await createApplicationMcpHarness({ session }); + onTestFinished(harness.close); + for (const record of [left, right, comparison]) + expect(session.recordEvidence(record).ok).toBe(true); + const response = await harness.client.callTool({ + name: "verify_reconstruction", + arguments: { + specification: { + name: "Artifact structure", + claims: [ + { + kind: "structural-artifact", + claim_id: "artifact-graph", + title: "Artifact graph remains equal", + comparison_evidence_id: comparison.evidence_id, + dimension: "overall", + }, + ], + }, + }, + }); + expect(response.isError, JSON.stringify(response)).not.toBe(true); + expect(response.structuredContent).toMatchObject({ + normalized_result: { + status: "unknown", + claims: { + items: [ + { + status: "unknown", + observed_status: "changed", + limitations: expect.arrayContaining([ + "Artifact source inventories do not corroborate the claimed comparison status.", + ]), + }, + ], + }, + }, + }); +}); + +it("does not pass an unchanged aggregate over unavailable source coverage", async () => { + const original = ARTIFACT_COMPARISON_EXAMPLE.left; + const left = createEvidence( + { path: "fixture-left", sha256: "0".repeat(64), format: "file" }, + original.provider, + { + operation: "inventory_artifact", + parameters: {}, + result: { + ...(original.normalized_result as Record), + coverage: { scope: "root", status: "unknown" }, + }, + confidence: "observed", + authority: "shipped-artifact", + }, + ); + const right = ARTIFACT_COMPARISON_EXAMPLE.right; + const actual = compareArtifacts(left, right); + expect(actual.status).toBe("unknown"); + const comparison = createEvidence( + undefined, + { + id: "rea-artifact-comparison", + name: "REA artifact comparison", + version: "1", + }, + { + predicateType: "rea.artifact-comparison", + operation: "compare_artifacts", + parameters: { + left_evidence_ids: [left.evidence_id], + right_evidence_ids: [right.evidence_id], + }, + result: { + ...actual, + status: "unchanged", + changes: [], + summary: { + unchanged: 1, + added: 0, + removed: 0, + changed: 0, + unknown: 0, + contradiction: 0, + }, + }, + confidence: "derived", + authority: "analyst-inference", + evidenceLinks: [left.evidence_id, right.evidence_id], + }, + ); + const session = createTestBinarySession(() => { + throw new Error("No provider expected"); + }); + const harness = await createApplicationMcpHarness({ session }); + onTestFinished(harness.close); + for (const record of [left, right, comparison]) + expect(session.recordEvidence(record).ok).toBe(true); + const response = await harness.client.callTool({ + name: "verify_reconstruction", + arguments: { + specification: { + name: "Artifact structure", + claims: [ + { + kind: "structural-artifact", + claim_id: "artifact-graph", + title: "Artifact graph remains equal", + comparison_evidence_id: comparison.evidence_id, + dimension: "overall", + }, + ], + }, + }, + }); + expect(response.isError, JSON.stringify(response)).not.toBe(true); + expect(response.structuredContent).toMatchObject({ + normalized_result: { + status: "unknown", + claims: { + items: [ + { + status: "unknown", + observed_status: "unchanged", + limitations: expect.arrayContaining([ + "Artifact source inventories do not corroborate the claimed comparison status.", + ]), + }, + ], + }, + }, + }); + expect(session.evidenceById(comparison.evidence_id)).toEqual(comparison); +}); diff --git a/tests/boundary/mcp/artifactInspectionCoverageMcp.test.ts b/tests/boundary/mcp/artifactInspectionCoverageMcp.test.ts index e1a6a48b4e..aa1f277ff6 100644 --- a/tests/boundary/mcp/artifactInspectionCoverageMcp.test.ts +++ b/tests/boundary/mcp/artifactInspectionCoverageMcp.test.ts @@ -71,7 +71,6 @@ describe.skipIf(process.platform === "win32")( }, ] as const)( "derives inspection and summary coverage from inventory traversal (FIFO: $withFifo)", - { timeout: 30_000 }, async ({ withFifo, status, sourceCoverage }, { mcp, onTestFinished }) => { const path = await writeBundle(withFifo); const { client, outputSchema } = await connect(mcp, onTestFinished); diff --git a/tests/boundary/mcp/artifactMcp.inlineInventory.test.ts b/tests/boundary/mcp/artifactMcp.inlineInventory.test.ts index 209af5f6f5..1208ff60de 100644 --- a/tests/boundary/mcp/artifactMcp.inlineInventory.test.ts +++ b/tests/boundary/mcp/artifactMcp.inlineInventory.test.ts @@ -203,7 +203,6 @@ const createInventoryViewReader = async ( throw new Error("Missing advertised view schemas"); const ajv = schemaValidation.assertions; const inputSchema: Record = advertised.inputSchema; - const outputSchema: Record = advertised.outputSchema; const malformed = { source: { kind: "retained-evidence", evidence_id: parent.evidence_id }, view: { kind: "inventory", collection: "nodes", logical_paths: [] }, @@ -229,16 +228,12 @@ const createInventoryViewReader = async ( ajv.validate(inputSchema, arguments_), JSON.stringify(ajv.errors), ).toBe(true); - const reply = await client.callTool({ - name: "inspect_analysis_view", - arguments: arguments_, - }); + const reply = await client.callTool( + { name: "inspect_analysis_view", arguments: arguments_ }, + { toolDefinition: advertised }, + ); if (reply.isError) throw new Error(JSON.stringify(parseMcpToolError(reply))); - expect( - ajv.validate(outputSchema, reply.structuredContent), - JSON.stringify(ajv.errors), - ).toBe(true); const evidence = parseEvidence(reply.structuredContent); return { evidence, diff --git a/tests/boundary/mcp/artifactMcp.test.ts b/tests/boundary/mcp/artifactMcp.test.ts index 0c02df9834..c9da62206d 100644 --- a/tests/boundary/mcp/artifactMcp.test.ts +++ b/tests/boundary/mcp/artifactMcp.test.ts @@ -120,7 +120,6 @@ it.for([ { unpacked: true, label: "unpacked" }, ])( "returns actionable local details for an $label ASAR integrity error", - { timeout: 20_000 }, async ({ unpacked }, { mcp, onTestFinished }) => { const root = await createTestTempDirectory("rea-asar-integrity-mcp-"); const source = join(root, "source"); @@ -981,6 +980,67 @@ it("does not apply PyInstaller declaration unknowns to foreign metadata or neste } }); +const verifyComparableDeclarations = async ( + client: Client, + session: ReturnType, + clean: ReturnType, + declarationsPath: string, + declarationsCopyPath: string, +) => { + // Complete PyInstaller declarations can prove equality even when their + // payloads were not hashed. Verification must reuse that producer rule. + const manifest = artifactInventoryResultSchema.parse( + clean.normalized_result, + ).manifest; + const declarationEvidence = (path: string) => + createEvidence( + { path, sha256: manifest.root_sha256, format: manifest.root_format }, + clean.provider, + { + operation: "inventory_artifact", + parameters: { path }, + result: clean.normalized_result, + confidence: "observed", + }, + ); + const original = declarationEvidence(declarationsPath); + const reconstruction = declarationEvidence(declarationsCopyPath); + expect(session.recordEvidence(original).ok).toBe(true); + expect(session.recordEvidence(reconstruction).ok).toBe(true); + const separateDeclarations = await compareArtifactEvidenceThroughMcp( + client, + original, + reconstruction, + ); + expect(separateDeclarations.isError).not.toBe(true); + const comparison = toolEvidence(separateDeclarations.structuredContent); + expect(comparison.normalized_result).toMatchObject({ status: "unchanged" }); + const verification = await client.callTool({ + name: "verify_reconstruction", + arguments: { + specification: { + name: "Declaration comparison", + claims: [ + { + kind: "structural-artifact", + claim_id: "declarations", + title: "Declared structure remains equal", + comparison_evidence_id: comparison.evidence_id, + dimension: "overall", + }, + ], + }, + }, + }); + expect(verification.isError, JSON.stringify(verification)).not.toBe(true); + expect( + toolEvidence(verification.structuredContent).normalized_result, + JSON.stringify(verification), + ).toMatchObject({ + status: "pass", + }); +}; + it("compares PyInstaller declaration facts without inventing payload identity", async ({ mcp, onTestFinished, @@ -989,12 +1049,14 @@ it("compares PyInstaller declaration facts without inventing payload identity", const leftPath = join(root, "module"); const rightPath = join(root, "script"); const declarationsPath = join(root, "declarations"); + const declarationsCopyPath = join(root, "declarations-copy"); const modulePath = join(root, "module-clean"); const shiftedPath = join(root, "module-shifted"); await Promise.all([ writeFile(leftPath, pyInstallerComparisonArchive("m", true)), writeFile(rightPath, pyInstallerComparisonArchive("s", true)), writeFile(declarationsPath, pyInstallerComparisonArchive("s", false)), + writeFile(declarationsCopyPath, pyInstallerComparisonArchive("s", false)), writeFile(modulePath, pyInstallerComparisonArchive("m", false)), writeFile(shiftedPath, pyInstallerComparisonArchive("m", false, true)), ]); @@ -1070,18 +1132,12 @@ it("compares PyInstaller declaration facts without inventing payload identity", }), ]), }); - expect( - z - .object({ changes: z.array(z.object({ logical_path: z.string() })) }) - .parse(changedResult) - .changes.map(({ logical_path }) => logical_path), - ).not.toContain("__pyinstaller__/runtime-options/1"); - expect( - z - .object({ changes: z.array(z.object({ logical_path: z.string() })) }) - .parse(changedResult) - .changes.map(({ logical_path }) => logical_path), - ).not.toContain("__pyinstaller__/dependencies/2"); + const changedPaths = z + .object({ changes: z.array(z.object({ logical_path: z.string() })) }) + .parse(changedResult) + .changes.map(({ logical_path }) => logical_path); + expect(changedPaths).not.toContain("__pyinstaller__/runtime-options/1"); + expect(changedPaths).not.toContain("__pyinstaller__/dependencies/2"); const declarationsOnly = await client.callTool({ name: "compare_artifacts", @@ -1096,6 +1152,14 @@ it("compares PyInstaller declaration facts without inventing payload identity", changes: [], }); + await verifyComparableDeclarations( + client, + session, + clean, + declarationsPath, + declarationsCopyPath, + ); + const layoutOnly = await client.callTool({ name: "compare_artifacts", arguments: { left: module, right: shifted }, diff --git a/tests/boundary/mcp/batchDecompileViewsMcp.test.ts b/tests/boundary/mcp/batchDecompileViewsMcp.test.ts index 810d9fccdd..30847dfd1c 100644 --- a/tests/boundary/mcp/batchDecompileViewsMcp.test.ts +++ b/tests/boundary/mcp/batchDecompileViewsMcp.test.ts @@ -129,28 +129,28 @@ const connect = async ( ); onTestFinished(() => harness.close()); const { tools } = await harness.mcp.listTools(); - const outputSchemas = new Map( + const advertisedTools = new Map( ADVERTISED_OUTPUTS.map((name) => { - const advertised = tools.find((tool) => tool.name === name)?.outputSchema; - if (advertised === undefined) + const advertised = tools.find((tool) => tool.name === name); + if (advertised?.outputSchema === undefined) throw new Error(`${name} advertises no output schema`); - const schema: Record = advertised; - return [name, schema] as const; + return [name, advertised] as const; }), ); - const reply = async (name: string, args: Record) => - z + const reply = async (name: string, args: Record) => { + const toolDefinition = advertisedTools.get(name); + return z .custom() - .parse(await harness.mcp.callTool({ name, arguments: args })); + .parse( + await harness.mcp.callTool( + { name, arguments: args }, + toolDefinition === undefined ? {} : { toolDefinition }, + ), + ); + }; const call = async (name: string, args: Record) => { const result = await reply(name, args); expect(result.isError, JSON.stringify(result)).not.toBe(true); - const schema = outputSchemas.get(name); - if (schema !== undefined) - expect( - schemaValidation.assertions.validate(schema, result.structuredContent), - JSON.stringify(schemaValidation.assertions.errors), - ).toBe(true); return { evidence: parseEvidence(result.structuredContent), result }; }; return { ...harness, calls, reply, call }; diff --git a/tests/boundary/mcp/binaryDiagnosticsMcp.test.ts b/tests/boundary/mcp/binaryDiagnosticsMcp.test.ts index a09acba305..4db106d0c8 100644 --- a/tests/boundary/mcp/binaryDiagnosticsMcp.test.ts +++ b/tests/boundary/mcp/binaryDiagnosticsMcp.test.ts @@ -261,14 +261,13 @@ it("records inline layout with compact schemas and canonical path checks", async approval: true, }), ).toBe(false); - const response = await client.callTool({ - name: "inspect_binary_layout", - arguments: { path: value.artifact.path }, - }); + const response = await client.callTool( + { name: "inspect_binary_layout", arguments: { path: value.artifact.path } }, + { toolDefinition: schemas.toolDefinition }, + ); const evidence = assertRecordedMcpEvidence( response, "inspect_binary_layout", - schemas, session, value, "observed", @@ -326,20 +325,16 @@ it("profiles real bytes with exact offsets, valid advertised schemas and read-on InMemoryTransport.createLinkedPair(); await server.connect(serverTransport); await client.connect(clientTransport); - const { ajv, inputSchema, outputSchema } = await advertisedMcpToolSchemas( + const { ajv, inputSchema, toolDefinition } = await advertisedMcpToolSchemas( client, "inspect_byte_entropy", ); expect(ajv.validate(inputSchema, { path, surprise: true })).toBe(false); - const response = await client.callTool({ - name: "inspect_byte_entropy", - arguments: { path }, - }); + const response = await client.callTool( + { name: "inspect_byte_entropy", arguments: { path } }, + { toolDefinition }, + ); expect(response.isError).not.toBe(true); - expect( - ajv.validate(outputSchema, response.structuredContent), - JSON.stringify(ajv.errors), - ).toBe(true); const evidence = parseEvidence(response.structuredContent); const report = byteEntropySchema.parse(evidence.normalized_result); expect(report.runs).toEqual([ @@ -402,10 +397,10 @@ it("records a portable export inventory through MCP without a binary target", as "inspect_native_exports", ); expect(schemas.ajv.validate(schemas.inputSchema, { path })).toBe(true); - const response = await client.callTool({ - name: "inspect_native_exports", - arguments: { path }, - }); + const response = await client.callTool( + { name: "inspect_native_exports", arguments: { path } }, + { toolDefinition: schemas.toolDefinition }, + ); expect( parseEvidence(response.structuredContent).normalized_result, ).toMatchObject({ @@ -425,7 +420,6 @@ it("records a portable export inventory through MCP without a binary target", as assertRecordedMcpEvidence( response, "inspect_native_exports", - schemas, session, parseNativeExports(bytes, { path, diff --git a/tests/boundary/mcp/binaryHardeningMcp.test.ts b/tests/boundary/mcp/binaryHardeningMcp.test.ts index f1c61c8fc9..307ae2c03b 100644 --- a/tests/boundary/mcp/binaryHardeningMcp.test.ts +++ b/tests/boundary/mcp/binaryHardeningMcp.test.ts @@ -64,7 +64,6 @@ mcpTest( await session.close(); } }, - 60_000, ); mcpTest( @@ -108,5 +107,4 @@ mcpTest( await session.close(); } }, - 60_000, ); diff --git a/tests/boundary/mcp/browserMcp.test.ts b/tests/boundary/mcp/browserMcp.test.ts index 838083d5d9..7ec5149c40 100644 --- a/tests/boundary/mcp/browserMcp.test.ts +++ b/tests/boundary/mcp/browserMcp.test.ts @@ -43,7 +43,6 @@ afterAll(async () => { schemaValidation.clear(); } }); -const INTEGRATION_TEST_TIMEOUT_MS = 20_000; it("preserves Link parameter order and context through the advertised MCP contract", async () => { const links = [ @@ -101,21 +100,20 @@ it("preserves Link parameter order and context through the advertised MCP contra if (advertised?.outputSchema === undefined) throw new Error("inspect_web_page must advertise its output schema"); - const inspected = await connected.client.callTool({ - name: "inspect_web_page", - arguments: { - cdp_endpoint: browser.endpoint, - target_id: "allowed-page", - observation_ms: 0, + const inspected = await connected.client.callTool( + { + name: "inspect_web_page", + arguments: { + cdp_endpoint: browser.endpoint, + target_id: "allowed-page", + observation_ms: 0, + }, }, - }); + { toolDefinition: advertised }, + ); expect(inspected.isError).not.toBe(true); const ajv = new Ajv2020({ strict: false, validateFormats: false }); const outputSchema: Record = advertised.outputSchema; - expect( - ajv.validate(outputSchema, inspected.structuredContent), - JSON.stringify(ajv.errors), - ).toBe(true); const normalized = webPageInspectionSchema.parse( normalizedResultOf(inspected.structuredContent), @@ -249,7 +247,7 @@ cliTest( ], environment: process.env, }); - expect(cliResult.exitCode).toBe(0); + expect(cliResult.exitCode, cliResult.stderr).toBe(0); expect(cliResult.signal).toBeNull(); const mcp = await client.callTool({ name: "analyze_web_bundle", @@ -281,7 +279,6 @@ cliTest( }, }); }, - INTEGRATION_TEST_TIMEOUT_MS, ); afterEach(() => resources.retryCleanup()); @@ -329,122 +326,118 @@ it("closes the browser and retains failed MCP and session cleanup for retry", as } }); -it( - "exposes CLI-equivalent Evidence results and session tools", - async () => { - const browser = await startFakeCdpBrowser({ - sessionTimeline: "same_origin", - webMcpTools: true, - sensitiveShapes: true, - eventsBeforeReply: ["Network.enable"], - }); - retainBrowser(browser); - const connected = await connectBrowser(); +it("exposes CLI-equivalent Evidence results and session tools", async () => { + const browser = await startFakeCdpBrowser({ + sessionTimeline: "same_origin", + webMcpTools: true, + sensitiveShapes: true, + eventsBeforeReply: ["Network.enable"], + }); + retainBrowser(browser); + const connected = await connectBrowser(); - const tools = await connected.client.listTools(); - expect(tools.tools).toHaveLength(TOOL_CONTRACTS.length); - expect(tools.tools.map(({ name }) => name)).toEqual( - expect.arrayContaining([ - "list_browser_targets", - "inspect_web_page", - "analyze_web_bundle", - "observe_web_session", - "discover_webmcp_tools", - "compare_web_captures", - "capture_web_screenshot", - "compare_web_screenshots", - ]), - ); - const status = await connected.client.callTool({ - name: "binary_session", - arguments: {}, - }); - const sessionStatus = toolContract("binary_session").outputSchema.parse( - status.structuredContent, - ); - const availability = expandToolAvailability( - sessionStatus.result.tool_availability, - ); - expect(availability).toContainEqual( - expect.objectContaining({ - name: "inspect_web_page", - reason: expect.any(String), - }), - ); - const listed = await connected.client.callTool({ - name: "list_browser_targets", - arguments: { - cdp_endpoint: browser.endpoint, - }, - }); - expect(listed.isError).not.toBe(true); - expect(listed.structuredContent).toMatchObject({ - normalized_result: { - targets: expect.arrayContaining([ - expect.objectContaining({ target_id: "allowed-page" }), - ]), - }, - }); - const inspected = await connected.client.callTool({ + const tools = await connected.client.listTools(); + expect(tools.tools).toHaveLength(TOOL_CONTRACTS.length); + expect(tools.tools.map(({ name }) => name)).toEqual( + expect.arrayContaining([ + "list_browser_targets", + "inspect_web_page", + "analyze_web_bundle", + "observe_web_session", + "discover_webmcp_tools", + "compare_web_captures", + "capture_web_screenshot", + "compare_web_screenshots", + ]), + ); + const status = await connected.client.callTool({ + name: "binary_session", + arguments: {}, + }); + const sessionStatus = toolContract("binary_session").outputSchema.parse( + status.structuredContent, + ); + const availability = expandToolAvailability( + sessionStatus.result.tool_availability, + ); + expect(availability).toContainEqual( + expect.objectContaining({ name: "inspect_web_page", - arguments: { - cdp_endpoint: browser.endpoint, - target_id: "allowed-page", - observation_ms: 0, - include_console_text: true, - include_json_body_shapes: true, - include_websocket_shapes: true, - include_script_sources: true, + reason: expect.any(String), + }), + ); + const listed = await connected.client.callTool({ + name: "list_browser_targets", + arguments: { + cdp_endpoint: browser.endpoint, + }, + }); + expect(listed.isError).not.toBe(true); + expect(listed.structuredContent).toMatchObject({ + normalized_result: { + targets: expect.arrayContaining([ + expect.objectContaining({ target_id: "allowed-page" }), + ]), + }, + }); + const inspected = await connected.client.callTool({ + name: "inspect_web_page", + arguments: { + cdp_endpoint: browser.endpoint, + target_id: "allowed-page", + observation_ms: 0, + include_console_text: true, + include_json_body_shapes: true, + include_websocket_shapes: true, + include_script_sources: true, + }, + }); + expect(inspected.isError).not.toBe(true); + expect(inspected.structuredContent).toMatchObject({ + normalized_result: { + target: { target_id: "allowed-page" }, + console: { prior_activity_available: false }, + network: { + prior_activity_available: false, + requests: [ + expect.objectContaining({ + body_shapes: expect.objectContaining({ status: "included" }), + }), + ], }, - }); - expect(inspected.isError).not.toBe(true); - expect(inspected.structuredContent).toMatchObject({ - normalized_result: { - target: { target_id: "allowed-page" }, - console: { prior_activity_available: false }, - network: { - prior_activity_available: false, - requests: [ - expect.objectContaining({ - body_shapes: expect.objectContaining({ status: "included" }), - }), - ], - }, - }, - }); - const reconciled = await connected.client.callTool({ - name: "reconcile_javascript_runtime", - arguments: { - static_layers: JAVASCRIPT_RUNTIME_RECONCILIATION_EXAMPLE.static_layers, - runtime_observations: [evidenceFor(inspected.structuredContent)], - }, - }); - expect(reconciled.isError).not.toBe(true); - expect(reconciled.structuredContent).toMatchObject({ - normalized_result: { summary: { runtime_scripts: 1 } }, - }); - expect(JSON.stringify(reconciled.structuredContent)).not.toContain( - "source-secret", - ); - const analyzed = await connected.client.callTool({ - name: "analyze_web_bundle", - arguments: { - cdp_endpoint: browser.endpoint, - target_id: "allowed-page", - observation_ms: 0, - }, - }); - expect(analyzed.isError).not.toBe(true); - expect(analyzed.structuredContent).toMatchObject({ - normalized_result: { - capture: { scripts_analyzed: 1 }, - observations: { source_maps: { status: "not_requested" } }, - }, - }); - await verifySessionAndComparisonTools(connected, browser, inspected); - }, - INTEGRATION_TEST_TIMEOUT_MS, -); + }, + }); + const reconciled = await connected.client.callTool({ + name: "reconcile_javascript_runtime", + arguments: { + static_layers: JAVASCRIPT_RUNTIME_RECONCILIATION_EXAMPLE.static_layers, + runtime_observations: [evidenceFor(inspected.structuredContent)], + }, + }); + expect(reconciled.isError).not.toBe(true); + expect(reconciled.structuredContent).toMatchObject({ + normalized_result: { summary: { runtime_scripts: 1 } }, + }); + expect(JSON.stringify(reconciled.structuredContent)).not.toContain( + "source-secret", + ); + const analyzed = await connected.client.callTool({ + name: "analyze_web_bundle", + arguments: { + cdp_endpoint: browser.endpoint, + target_id: "allowed-page", + observation_ms: 0, + }, + }); + expect(analyzed.isError).not.toBe(true); + expect(analyzed.structuredContent).toMatchObject({ + normalized_result: { + capture: { scripts_analyzed: 1 }, + observations: { source_maps: { status: "not_requested" } }, + }, + }); + await verifySessionAndComparisonTools(connected, browser, inspected); +}); const verifySessionAndComparisonTools = async ( connected: Awaited>, @@ -775,7 +768,7 @@ cliTest( ], environment: process.env, }); - expect(cliResult.exitCode).toBe(0); + expect(cliResult.exitCode, cliResult.stderr).toBe(0); expect(cliResult.signal).toBeNull(); const cliEvidence = parseEvidence(cliResult.json); expect(cliEvidence.normalized_result).toMatchObject(expectedResult); diff --git a/tests/boundary/mcp/browserScenarioInputDiagnosticsMcp.test.ts b/tests/boundary/mcp/browserScenarioInputDiagnosticsMcp.test.ts index df1f57ae7c..b704c20343 100644 --- a/tests/boundary/mcp/browserScenarioInputDiagnosticsMcp.test.ts +++ b/tests/boundary/mcp/browserScenarioInputDiagnosticsMcp.test.ts @@ -2,12 +2,17 @@ import { Client } from "@modelcontextprotocol/client"; import { StdioClientTransport } from "@modelcontextprotocol/client/stdio"; import { Ajv2020 } from "ajv/dist/2020.js"; import { join, resolve } from "node:path"; -import { expect } from "vitest"; +import { afterAll, expect } from "vitest"; import { z } from "zod"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; import { parseMcpToolError } from "../../fixtures/mcpToolError.js"; -import { cliTest, runFileCli } from "../../support/cli/cliFixture.js"; +import { cliTest } from "../../support/cli/cliFixture.js"; +import { createMcpSchemaValidation } from "../../fixtures/mcpSchemaValidation.js"; + +// Compile SDK output validators for the tools a test calls, not the whole catalog. +const schemaValidation = createMcpSchemaValidation(); +afterAll(() => schemaValidation.clear()); const scenarios = [ { @@ -78,15 +83,17 @@ const scenarios = [ cliTest.for(["full", "compact"] as const)( "projects browser scenario input diagnostics over stdio MCP (%s)", - { timeout: 60_000 }, async (profile, { cli }) => { const home = await createTestTempDirectory( "rea-browser-scenario-mcp-home-", ); - const client = new Client({ - name: "browser-scenario-input-diagnostics", - version: "1", - }); + const client = new Client( + { + name: "browser-scenario-input-diagnostics", + version: "1", + }, + { jsonSchemaValidator: schemaValidation }, + ); const transport = new StdioClientTransport({ command: process.execPath, args: [resolve("scripts/rea.mjs"), "mcp"], @@ -127,7 +134,7 @@ cliTest.for(["full", "compact"] as const)( `${scenario.name}: ${JSON.stringify(validate.errors)}`, ).toBe(scenario.fullSchemaValid || profile === "compact"); - const cliResult = await runFileCli(cli, { + const cliResult = await cli.run({ arguments: [ "capture-browser-scenario", JSON.stringify(input), diff --git a/tests/boundary/mcp/browserSourceCoordinates.test.ts b/tests/boundary/mcp/browserSourceCoordinates.test.ts index c26384bedd..dcb4c4223c 100644 --- a/tests/boundary/mcp/browserSourceCoordinates.test.ts +++ b/tests/boundary/mcp/browserSourceCoordinates.test.ts @@ -1,4 +1,3 @@ -import { Ajv2020 } from "ajv/dist/2020.js"; import { expect, it, onTestFinished } from "vitest"; import { CdpBrowserProvider } from "../../../src/browser/CdpBrowserProvider.js"; import { createApplicationMcpHarness } from "../../fixtures/applicationMcpHarness.js"; @@ -66,15 +65,16 @@ const inspectSourceCoordinates = async (position: { target_id: "allowed-page", observation_ms: 0, }; - const page = await client.callTool({ - name: "inspect_web_page", - arguments: args, - }); - const tools = await client.callTool({ - name: "discover_webmcp_tools", - arguments: args, - }); - return { client, page, tools }; + const definitions = (await client.listTools()).tools; + const call = async (name: "inspect_web_page" | "discover_webmcp_tools") => { + const toolDefinition = definitions.find((tool) => tool.name === name); + if (toolDefinition?.outputSchema === undefined) + throw new Error(`Missing advertised output schema: ${name}`); + return client.callTool({ name, arguments: args }, { toolDefinition }); + }; + const page = await call("inspect_web_page"); + const tools = await call("discover_webmcp_tools"); + return { page, tools }; }; it("reports malformed source coordinates as unknown through MCP", async () => { @@ -126,29 +126,15 @@ it("reports malformed source coordinates as unknown through MCP", async () => { }, }, }); -}, 20_000); +}); it("keeps a negative column Chrome reports in passive source locations", async () => { - const { client, page, tools } = await inspectSourceCoordinates({ + const { page, tools } = await inspectSourceCoordinates({ lineNumber: 21, columnNumber: -40, }); - const advertised = (await client.listTools()).tools; - const ajv = new Ajv2020({ strict: false, validateFormats: false }); - for (const [name, response] of [ - ["inspect_web_page", page], - ["discover_webmcp_tools", tools], - ] as const) { + for (const response of [page, tools]) { expect(response.isError).not.toBe(true); - const outputSchema: Record | undefined = advertised.find( - (tool) => tool.name === name, - )?.outputSchema; - if (outputSchema === undefined) - throw new Error(`Missing advertised output schema: ${name}`); - expect( - ajv.validate(outputSchema, response.structuredContent), - JSON.stringify(ajv.errors), - ).toBe(true); } const location = { line: 21, column: -40 }; expect(page.structuredContent).toMatchObject({ @@ -192,4 +178,4 @@ it("keeps a negative column Chrome reports in passive source locations", async ( }, }, }); -}, 20_000); +}); diff --git a/tests/boundary/mcp/buildCaptureRetryMcp.test.ts b/tests/boundary/mcp/buildCaptureRetryMcp.test.ts index a33daaacc3..cf445a0156 100644 --- a/tests/boundary/mcp/buildCaptureRetryMcp.test.ts +++ b/tests/boundary/mcp/buildCaptureRetryMcp.test.ts @@ -208,7 +208,7 @@ it("releases a capture whose start was cancelled after launch, so another start computer_id: id, }); expect(again.structuredContent).toMatchObject({ package_lists: 1 }); -}, 60_000); +}); it("stops a detached capture when the computer closes", async () => { const { rea, client, computer } = await fakes(); @@ -225,7 +225,7 @@ it("stops a detached capture when the computer closes", async () => { expect(running().length).toBeGreaterThan(0); await computer.close(); expect(running()).toEqual([]); -}, 60_000); +}); it("cancels a capture's start when the computer closes while it waits for the opened app's screenshot", async () => { // The start's own screenshot answers at once; the capture's waits until its request is cancelled. @@ -238,7 +238,7 @@ it("cancels a capture's start when the computer closes while it waits for the op await vi.waitFor(() => expect(screenshots()).toBe(2), { timeout: 30_000 }); await callOk(client, "close_build_computer", { computer_id: id }); expect(parseMcpToolError(await started).error.code).toBe("cancelled"); -}, 60_000); +}); it("reports cancellation and the published bundle when the computer closes during commit", async () => { const { client } = await fakes(); @@ -265,7 +265,7 @@ it("reports cancellation and the published bundle when the computer closes durin }); expect((await readdir(directory)).length).toBeGreaterThan(0); await closed; -}, 60_000); +}); /** Capture and export an app the request installs with `install`; the start's result, the bundle's folder and the stand-in's argv for a command. */ const captureRequest = async ( @@ -496,4 +496,4 @@ it("brings back a bundle whose file list outgrows a command's 1 MiB of output", expect( await readdir(join(directory, "seed", long("a"), long("b"))), ).toHaveLength(1500); -}, 120_000); +}); diff --git a/tests/boundary/mcp/buildComputerMcp.test.ts b/tests/boundary/mcp/buildComputerMcp.test.ts index 121a3a893d..1fa68ac35b 100644 --- a/tests/boundary/mcp/buildComputerMcp.test.ts +++ b/tests/boundary/mcp/buildComputerMcp.test.ts @@ -472,7 +472,7 @@ it("returns the latest frame when a retried screenshot outlasts the wait for the expect(progress.map(({ message }) => message)).toContainEqual( expect.stringContaining("waiting for the desktop to draw"), ); -}, 40_000); +}); it("leaves the bundle folder's files as they were when it starts a computer from it", async () => { const { runner } = await fakes(); @@ -689,7 +689,7 @@ it("runs one computer's calls in the order they arrive and another computer's be "reset", "screenshot behind the reset", ]); -}, 30_000); +}); it("sends progress while an action waits for the call before it on its computer", async () => { const app = slowApp(14); @@ -715,7 +715,7 @@ it("sends progress while an action waits for the call before it on its computer" expect(waited.isError, JSON.stringify(waited)).not.toBe(true); expect(progress.length).toBeGreaterThan(0); await reset; -}, 30_000); +}); it("cancels a computer's call in progress on close and refuses the calls waiting behind it", async () => { const app = slowApp(600); diff --git a/tests/boundary/mcp/buildWitnessMcp.test.ts b/tests/boundary/mcp/buildWitnessMcp.test.ts index 2dd4554f1b..65802e2209 100644 --- a/tests/boundary/mcp/buildWitnessMcp.test.ts +++ b/tests/boundary/mcp/buildWitnessMcp.test.ts @@ -157,6 +157,78 @@ const expectUndecided = ( for (const fact of [stage, ...facts]) expect(error.message).toContain(fact); }; +it.each([ + { kind: "file_present", passed: true }, + { kind: "file_absent", passed: false }, +] as const)( + "grades $kind against an own __proto__ state file over MCP", + async ({ kind, passed }) => { + const witness = await witnessed( + 0, + {}, + { + name: "selected-file", + prompt: "Check the selected file.", + checks: [{ kind, file: "__proto__" }], + }, + ); + witness.app.state = JSON.parse( + '{"__proto__":{"format":"json","view":{"count":7}}}', + ); + const report = parsed(await witness.check({})); + expect(report).toMatchObject({ + passed: passed ? 1 : 0, + failed: passed ? 0 : 1, + results: [{ id: "check-1", passed }], + }); + }, +); + +it.each([false, true])( + "keeps own row conditions through MCP (only special column: %s)", + async (onlySpecial) => { + const where = onlySpecial + ? JSON.parse('{"__proto__":"other"}') + : JSON.parse('{"id":1,"__proto__":"wanted"}'); + const common = { file: "db.sqlite", table: "rows", where }; + const witness = await witnessed( + 0, + {}, + { + name: "find-row", + prompt: "Check every selected column of the row.", + checks: [ + { kind: "row_present", ...common }, + { kind: "row_absent", ...common }, + { kind: "row_count", ...common, equals: onlySpecial ? 1 : 0 }, + ], + }, + ); + witness.app.state = { + "db.sqlite": { + format: "sqlite", + view: { + tables: { + rows: { + columns: ["id", "__proto__"], + primary_key: ["id"], + rows: [[1, "other"]], + truncated: false, + }, + }, + tables_truncated: false, + }, + truncated: false, + }, + }; + const report = parsed(await witness.check({})); + expect(report).toMatchObject({ passed: 2, failed: 1 }); + expect(report.results.map(({ passed }) => passed)).toEqual( + onlySpecial ? [true, false, true] : [false, true, true], + ); + }, +); + it("witnesses a task: the seed fails, the edit passes, a near miss fails and so does the reset seed", async () => { const { app, computer, directory, check, act } = await witnessed(); @@ -829,7 +901,7 @@ it("keeps a completed reset when the screenshot after it is cancelled", async () const near = parseMcpToolError(await witness.check({ stage: "near_miss" })); expect(near.error.message).toContain("put the seed back"); expect(witness.app.state).toEqual(lines("milk")); -}, 30_000); +}); it("checks after_reset once the reset sent before it has relaunched the app", async () => { const witness = await witnessed(4); @@ -853,7 +925,7 @@ it("checks after_reset once the reset sent before it has relaunched the app", as outcome: "applied", }); expect(app.methods.slice(-2)).toEqual(["data/reset", "data/get"]); -}, 30_000); +}); it("sends progress while a reset waits for the relaunched app", async () => { const witness = await witnessed(14); @@ -867,7 +939,7 @@ it("sends progress while a reset waits for the relaunched app", async () => { ); expect(reset.isError, JSON.stringify(reset)).not.toBe(true); expect(progress.length).toBeGreaterThan(0); -}, 30_000); +}); /** The shipped timing scaled down a tenfold: a read every 50 ms, a 300 ms window, a 1.5 s bound. */ const SETTLE: SettleTiming = { intervalMs: 50, windowMs: 300, boundMs: 1500 }; diff --git a/tests/boundary/mcp/commandObservationViews.test.ts b/tests/boundary/mcp/commandObservationViews.test.ts index dcd4b867b0..03949aaf13 100644 --- a/tests/boundary/mcp/commandObservationViews.test.ts +++ b/tests/boundary/mcp/commandObservationViews.test.ts @@ -131,17 +131,16 @@ it("delivers ordered GDB records once and queries their retained parent after cl "inspect_analysis_view", ); const input = { session_id: sessionId, command: capture.command }; - const full = await client.callTool({ name: "gdb_console", arguments: input }); - const summary = await client.callTool({ - name: "gdb_console", - arguments: { ...input, detail: "summary" }, - }); + const full = await client.callTool( + { name: "gdb_console", arguments: input }, + { toolDefinition: schemas.toolDefinition }, + ); + const summary = await client.callTool( + { name: "gdb_console", arguments: { ...input, detail: "summary" } }, + { toolDefinition: schemas.toolDefinition }, + ); for (const reply of [full, summary]) { expect(reply.isError).not.toBe(true); - expect( - schemas.ajv.validate(schemas.outputSchema, reply.structuredContent), - JSON.stringify(schemas.ajv.errors), - ).toBe(true); } expect(parseEvidence(full.structuredContent)).toEqual(gdbEvidence()); const selected = parseEvidence(summary.structuredContent); @@ -174,23 +173,19 @@ it("delivers ordered GDB records once and queries their retained parent after cl name: "close_gdb_session", arguments: { session_id: sessionId }, }); - const queried = await client.callTool({ - name: "inspect_analysis_view", - arguments: { - source: { - kind: "retained-evidence", - evidence_id: gdbEvidence().evidence_id, + const queried = await client.callTool( + { + name: "inspect_analysis_view", + arguments: { + source: { + kind: "retained-evidence", + evidence_id: gdbEvidence().evidence_id, + }, + view: { kind: "summary" }, }, - view: { kind: "summary" }, }, - }); - expect( - viewSchemas.ajv.validate( - viewSchemas.outputSchema, - queried.structuredContent, - ), - JSON.stringify(viewSchemas.ajv.errors), - ).toBe(true); + { toolDefinition: viewSchemas.toolDefinition }, + ); expect(parseEvidence(queried.structuredContent)).toEqual(selected); expect(ledger.get(gdbEvidence().evidence_id)).toEqual(gdbEvidence()); }); @@ -207,19 +202,17 @@ it("keeps pathless objdump observations subjectless and preserves the default co detail: "summary", }), ).toBe(true); - const full = await client.callTool({ - name: "inspect_with_objdump", - arguments: { operation: "architectures" }, - }); - const selected = await client.callTool({ - name: "inspect_with_objdump", - arguments: { operation: "architectures", detail: "summary" }, - }); - for (const reply of [full, selected]) - expect( - schemas.ajv.validate(schemas.outputSchema, reply.structuredContent), - JSON.stringify(schemas.ajv.errors), - ).toBe(true); + const full = await client.callTool( + { name: "inspect_with_objdump", arguments: { operation: "architectures" } }, + { toolDefinition: schemas.toolDefinition }, + ); + const selected = await client.callTool( + { + name: "inspect_with_objdump", + arguments: { operation: "architectures", detail: "summary" }, + }, + { toolDefinition: schemas.toolDefinition }, + ); const parent = parseEvidence(full.structuredContent); const summary = parseEvidence(selected.structuredContent); expect(summary.normalized_result).toMatchObject({ diff --git a/tests/boundary/mcp/contractPresentation.test.ts b/tests/boundary/mcp/contractPresentation.test.ts index 0aaf8bd9e9..d1db7f3d79 100644 --- a/tests/boundary/mcp/contractPresentation.test.ts +++ b/tests/boundary/mcp/contractPresentation.test.ts @@ -61,74 +61,70 @@ const resultSchema = z.object({ ), }); -it( - "preserves full advertised schemas and handler meaning when presentation order changes", - { timeout: 70_000 }, - async () => { - const cancellation = new AbortController(); - const command = runOwnedCommand( - { - command: process.execPath, - arguments: [fixture, "reversed"], - runId: randomUUID(), - env: { - ...process.env, - REA_MCP_INPUT_SCHEMA_PROFILE: "full", - REA_MCP_OUTPUT_SCHEMA_PROFILE: "full", - }, - sweepTokenOwnedProcesses: true, +it("preserves full advertised schemas and handler meaning when presentation order changes", async () => { + const cancellation = new AbortController(); + const command = runOwnedCommand( + { + command: process.execPath, + arguments: [fixture, "reversed"], + runId: randomUUID(), + env: { + ...process.env, + REA_MCP_INPUT_SCHEMA_PROFILE: "full", + REA_MCP_OUTPUT_SCHEMA_PROFILE: "full", }, - { - // A prepared full/full catalog run needs about 32s on two CPU cores. - timeoutMs: 60_000, - diagnosticBytes: 16 * 1024 * 1024, - }, - { signal: cancellation.signal }, + sweepTokenOwnedProcesses: true, + }, + { + // A prepared full/full catalog run needs about 32s on two CPU cores. + timeoutMs: 60_000, + diagnosticBytes: 16 * 1024 * 1024, + }, + { signal: cancellation.signal }, + ); + onTestFinished(async () => { + cancellation.abort(); + await command; + }); + const snapshot = await command; + const stdout = snapshot.stdout.text; + const result = resultSchema.parse(JSON.parse(stdout)); + expect(result.catalog).toHaveLength(GENERATED_MCP_TOOL_CATALOG.length); + expect(new Set(result.catalog.map(({ name }) => name))).toEqual( + new Set(GENERATED_MCP_TOOL_CATALOG.map(({ name }) => name)), + ); + for (const advertised of result.catalog) { + const canonical = GENERATED_MCP_TOOL_CATALOG.find( + ({ name }) => name === advertised.name, ); - onTestFinished(async () => { - cancellation.abort(); - await command; - }); - const snapshot = await command; - const stdout = snapshot.stdout.text; - const result = resultSchema.parse(JSON.parse(stdout)); - expect(result.catalog).toHaveLength(GENERATED_MCP_TOOL_CATALOG.length); - expect(new Set(result.catalog.map(({ name }) => name))).toEqual( - new Set(GENERATED_MCP_TOOL_CATALOG.map(({ name }) => name)), - ); - for (const advertised of result.catalog) { - const canonical = GENERATED_MCP_TOOL_CATALOG.find( - ({ name }) => name === advertised.name, - ); - expect(advertised.inputSchema).toEqual(canonical?.inputSchema); - expect(advertised.outputSchema).toEqual(canonical?.outputSchema); + expect(advertised.inputSchema).toEqual(canonical?.inputSchema); + expect(advertised.outputSchema).toEqual(canonical?.outputSchema); + } + expect(result.responses.map(({ name }) => name).sort()).toEqual( + [ + ...BROWSER_TOOL_CONTRACTS, + ...BROWSER_SCENARIO_TOOL_CONTRACTS, + ...ELECTRON_TOOL_CONTRACTS.filter( + ({ name }) => name !== "analyze_javascript_application", + ), + ...JAVASCRIPT_RUNTIME_OBSERVATION_TOOL_CONTRACTS, + ] + .map(({ name }) => name) + .sort(), + ); + for (const { name, response } of result.responses) { + if (!isCallToolResult(response)) + throw new Error(`Invalid tools/call fixture result for ${name}`); + if (name === "reconcile_javascript_runtime") { + expect(response.isError).not.toBe(true); + expect(response.structuredContent).toMatchObject({ + operation: name, + }); + } else { + expect(response.isError, name).toBe(true); + expect(parseMcpToolError(response), name).toMatchObject({ + error: { details: { operation: name } }, + }); } - expect(result.responses.map(({ name }) => name).sort()).toEqual( - [ - ...BROWSER_TOOL_CONTRACTS, - ...BROWSER_SCENARIO_TOOL_CONTRACTS, - ...ELECTRON_TOOL_CONTRACTS.filter( - ({ name }) => name !== "analyze_javascript_application", - ), - ...JAVASCRIPT_RUNTIME_OBSERVATION_TOOL_CONTRACTS, - ] - .map(({ name }) => name) - .sort(), - ); - for (const { name, response } of result.responses) { - if (!isCallToolResult(response)) - throw new Error(`Invalid tools/call fixture result for ${name}`); - if (name === "reconcile_javascript_runtime") { - expect(response.isError).not.toBe(true); - expect(response.structuredContent).toMatchObject({ - operation: name, - }); - } else { - expect(response.isError, name).toBe(true); - expect(parseMcpToolError(response), name).toMatchObject({ - error: { details: { operation: name } }, - }); - } - } - }, -); + } +}); diff --git a/tests/boundary/mcp/cutterCancellationMcp.test.ts b/tests/boundary/mcp/cutterCancellationMcp.test.ts index f0f276331e..6162d4fa3c 100644 --- a/tests/boundary/mcp/cutterCancellationMcp.test.ts +++ b/tests/boundary/mcp/cutterCancellationMcp.test.ts @@ -1,6 +1,7 @@ import { Client, InMemoryTransport } from "@modelcontextprotocol/client"; import { McpServer } from "@modelcontextprotocol/server"; import { expect, it } from "vitest"; +import { awaitAcknowledgement } from "../../fixtures/lifecycleAcknowledgement.js"; import { chmod, rm, writeFile } from "node:fs/promises"; import { createServer, type Socket } from "node:net"; import { join } from "node:path"; @@ -164,7 +165,11 @@ it("shares one cleanup proof across concurrent Cutter MCP commands without Binar const first = call("first queued command"); const second = call("second queued command"); pending = [first, second]; - await entered; + await awaitAcknowledgement( + entered, + Promise.race([first, second]), + "Cutter cleanup verification", + ); await new Promise((resolve) => setImmediate(resolve)); releaseProof(); const results = await Promise.all([first, second]); @@ -252,7 +257,7 @@ it("cancels active discovery sockets, stops queued probes and recovers on the sa { name: "list_cutter_sessions", arguments: {} }, { signal: controller.signal }, ); - await connected; + await awaitAcknowledgement(connected, request, "Cutter discovery", 10_000); controller.abort(new Error("selected discovery cancellation")); await expect(request).rejects.toThrow("selected discovery cancellation"); await expect.poll(() => settled).toBe(true); @@ -279,9 +284,12 @@ it("cancels active discovery sockets, stops queued probes and recovers on the sa } finally { controller.abort(); for (const socket of sockets) socket.destroy(); - await Promise.allSettled([client.close(), server.close()]); - await service.close(); - await new Promise((resolve) => peer.close(() => resolve())); + try { + await Promise.allSettled([client.close(), server.close()]); + await service.close(); + } finally { + await new Promise((resolve) => peer.close(() => resolve())); + } } }); diff --git a/tests/boundary/mcp/dartAotSummaryMcp.test.ts b/tests/boundary/mcp/dartAotSummaryMcp.test.ts index 9794888856..70fa9fd11c 100644 --- a/tests/boundary/mcp/dartAotSummaryMcp.test.ts +++ b/tests/boundary/mcp/dartAotSummaryMcp.test.ts @@ -48,21 +48,18 @@ it("retains the selected pool policy through MCP summary projection", async () = ); if (definition?.outputSchema === undefined) throw new Error("Missing AOT output schema"); - const outputSchema: Record = definition.outputSchema; - const ajv = new Ajv2020({ strict: false, validateFormats: false }); - const filteredReply = await client.callTool({ - name: "inspect_dart_aot", - arguments: { - path, - detail: "summary", - pool_policy: { contains: "package:animations/", uri_limit: 1 }, + const filteredReply = await client.callTool( + { + name: "inspect_dart_aot", + arguments: { + path, + detail: "summary", + pool_policy: { contains: "package:animations/", uri_limit: 1 }, + }, }, - }); + { toolDefinition: definition }, + ); expect(filteredReply.isError).not.toBe(true); - expect( - ajv.validate(outputSchema, filteredReply.structuredContent), - JSON.stringify(ajv.errors), - ).toBe(true); const filteredView = dartAotSummaryViewResultSchema.parse( parseEvidence(filteredReply.structuredContent).normalized_result, ); @@ -97,15 +94,14 @@ it("honors explicit ABI and old full requests, validates SDK schemas and reuses { path, detail: "summary" }, ), ).toBe(false); - const reply = await client.callTool({ - name: "inspect_dart_aot", - arguments: { path, abi: "x86_64", detail: "summary" }, - }); + const reply = await client.callTool( + { + name: "inspect_dart_aot", + arguments: { path, abi: "x86_64", detail: "summary" }, + }, + { toolDefinition: definition }, + ); expect(reply.isError).not.toBe(true); - expect( - ajv.validate(outputSchema, reply.structuredContent), - JSON.stringify(ajv.errors), - ).toBe(true); const receipt = parseEvidence(reply.structuredContent); const view = dartAotSummaryViewResultSchema.parse(receipt.normalized_result); const parent = session.evidenceById(view.parent_evidence_id); @@ -201,7 +197,7 @@ it("honors explicit ABI and old full requests, validates SDK schemas and reuses }); if (!portable.ok) throw portable.error; expect(portable.value.normalized_result).toEqual(view); -}, 30000); +}); it("preserves null snapshot facts and pool lower bounds after exhaustion, rejecting malformed and conflicting identities", async () => { const report = { diff --git a/tests/boundary/mcp/deferredRuntime.test.ts b/tests/boundary/mcp/deferredRuntime.test.ts index 4009cf47a2..a0735b1054 100644 --- a/tests/boundary/mcp/deferredRuntime.test.ts +++ b/tests/boundary/mcp/deferredRuntime.test.ts @@ -11,10 +11,11 @@ import { writeFile, } from "node:fs/promises"; import { resolve, join, dirname } from "node:path"; -import { expect, it, onTestFinished } from "vitest"; +import { afterAll, expect, it, onTestFinished } from "vitest"; import { artifactMcpCall } from "../../../scripts/lib/artifact-e2e.mjs"; import { closeVerifierResources } from "../../../scripts/lib/verifier-cleanup.mjs"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; +import { createMcpSchemaValidation } from "../../fixtures/mcpSchemaValidation.js"; import { GENERATED_MCP_TOOL_CATALOG } from "../../fixtures/mcpToolCatalog.js"; import { PROMPT_CONTRACTS } from "../../../src/contracts/promptContracts.js"; import { TOOL_FAMILY_DEFINITIONS } from "../../../src/contracts/toolFamilies.js"; @@ -30,6 +31,9 @@ import { readGuidanceSectionOutputSchema, } from "../../../src/contracts/guidanceContracts.js"; +const schemaValidation = createMcpSchemaValidation(); +afterAll(() => schemaValidation.clear()); + const connect = async ( mode: "legacy" | "auto", environment: Record = {}, @@ -53,7 +57,8 @@ const connect = async ( }); const client = new Client( { name: "deferred-runtime-boundary", version: "1" }, - { versionNegotiation: { mode } }, + // Compile output validators for the tools a test calls, not the catalog. + { versionNegotiation: { mode }, jsonSchemaValidator: schemaValidation }, ); onTestFinished(async () => { await closeVerifierResources([ diff --git a/tests/boundary/mcp/desktopApplicationMcp.test.ts b/tests/boundary/mcp/desktopApplicationMcp.test.ts index 8c20a74dee..3dc9937e77 100644 --- a/tests/boundary/mcp/desktopApplicationMcp.test.ts +++ b/tests/boundary/mcp/desktopApplicationMcp.test.ts @@ -118,15 +118,11 @@ it("advertises exact schemas and retains a compact directory answer with typed s expect( schemas.ajv.validate(schemas.inputSchema, { path: root, execute: true }), ).toBe(false); - const result = await client.callTool({ - name: "inspect_desktop_application", - arguments: { path: root }, - }); + const result = await client.callTool( + { name: "inspect_desktop_application", arguments: { path: root } }, + { toolDefinition: schemas.toolDefinition }, + ); expect(result.isError).not.toBe(true); - expect( - schemas.ajv.validate(schemas.outputSchema, result.structuredContent), - JSON.stringify(schemas.ajv.errors), - ).toBe(true); const summary = parseEvidence(result.structuredContent); const view = analysisViewResultSchema.parse(summary.normalized_result); expect(view.kind).toBe("summary"); @@ -139,27 +135,26 @@ it("advertises exact schemas and retains a compact directory answer with typed s const archive = full.files.find((file) => file.path === "resources/app.asar"); if (archive === undefined) throw new Error("Missing retained ASAR convention"); - const selected = await client.callTool({ - name: "inspect_analysis_view", - arguments: { - source: { - kind: "retained-evidence", - evidence_id: view.parent_evidence_id, - }, - view: { - kind: "desktop-application", - collection: "files", - file_ids: [archive.file_id], - fields: ["path", "read_status"], - limit: 1, + const selected = await client.callTool( + { + name: "inspect_analysis_view", + arguments: { + source: { + kind: "retained-evidence", + evidence_id: view.parent_evidence_id, + }, + view: { + kind: "desktop-application", + collection: "files", + file_ids: [archive.file_id], + fields: ["path", "read_status"], + limit: 1, + }, }, }, - }); + { toolDefinition: views.toolDefinition }, + ); expect(selected.isError).not.toBe(true); - expect( - views.ajv.validate(views.outputSchema, selected.structuredContent), - JSON.stringify(views.ajv.errors), - ).toBe(true); const page = parseEvidence(selected.structuredContent); expect(page.evidence_links).toEqual([view.parent_evidence_id]); expect(page.normalized_result).toMatchObject({ @@ -204,14 +199,14 @@ it("surfaces unavailable probes and filters their complete retained diagnostics join(partialRoot, `probe${String(index).padStart(2, "0")}.exe`), buildNativePeFixture(), ); - const partial = await client.callTool({ - name: "inspect_desktop_application", - arguments: { path: partialRoot, max_probe_bytes: 4096 }, - }); + const partial = await client.callTool( + { + name: "inspect_desktop_application", + arguments: { path: partialRoot, max_probe_bytes: 4096 }, + }, + { toolDefinition: schemas.toolDefinition }, + ); expect(partial.isError).not.toBe(true); - expect( - schemas.ajv.validate(schemas.outputSchema, partial.structuredContent), - ).toBe(true); const partialEvidence = parseEvidence(partial.structuredContent); const partialView = analysisViewResultSchema.parse( partialEvidence.normalized_result, @@ -275,14 +270,11 @@ it("surfaces unavailable probes and filters their complete retained diagnostics }, }; expect(views.ajv.validate(views.inputSchema, args)).toBe(true); - const response = await client.callTool({ - name: "inspect_analysis_view", - arguments: args, - }); + const response = await client.callTool( + { name: "inspect_analysis_view", arguments: args }, + { toolDefinition: views.toolDefinition }, + ); expect(response.isError).not.toBe(true); - expect( - views.ajv.validate(views.outputSchema, response.structuredContent), - ).toBe(true); const evidence = parseEvidence(response.structuredContent); expect(evidence.evidence_links).toEqual([partialView.parent_evidence_id]); expect(evidence.normalized_result).toMatchObject({ @@ -329,21 +321,21 @@ it.skipIf(process.platform === "win32")( kind: "retained-evidence", evidence_id: summary.parent_evidence_id, }; - const listed = await harness.client.callTool({ - name: "inspect_analysis_view", - arguments: { - source, - view: { - kind: "desktop-application", - collection: "omissions", - limit: 2, + const listed = await harness.client.callTool( + { + name: "inspect_analysis_view", + arguments: { + source, + view: { + kind: "desktop-application", + collection: "omissions", + limit: 2, + }, }, }, - }); + { toolDefinition: schemas.toolDefinition }, + ); expect(listed.isError).not.toBe(true); - expect( - schemas.ajv.validate(schemas.outputSchema, listed.structuredContent), - ).toBe(true); const page = analysisViewResultSchema.parse( parseEvidence(listed.structuredContent).normalized_result, ); @@ -369,14 +361,11 @@ it.skipIf(process.platform === "win32")( }, }; expect(schemas.ajv.validate(schemas.inputSchema, args)).toBe(true); - const selected = await harness.client.callTool({ - name: "inspect_analysis_view", - arguments: args, - }); + const selected = await harness.client.callTool( + { name: "inspect_analysis_view", arguments: args }, + { toolDefinition: schemas.toolDefinition }, + ); expect(selected.isError).not.toBe(true); - expect( - schemas.ajv.validate(schemas.outputSchema, selected.structuredContent), - ).toBe(true); const evidence = parseEvidence(selected.structuredContent); expect(evidence.evidence_links).toEqual([summary.parent_evidence_id]); expect(evidence.normalized_result).toHaveProperty( diff --git a/tests/boundary/mcp/devicetreeMcp.test.ts b/tests/boundary/mcp/devicetreeMcp.test.ts index 71806dd0e5..8fb056b481 100644 --- a/tests/boundary/mcp/devicetreeMcp.test.ts +++ b/tests/boundary/mcp/devicetreeMcp.test.ts @@ -55,15 +55,11 @@ mcpTest( const root = await createTestTempDirectory("rea-dtb-mcp-"); const path = join(root, "board.dtb"); await writeFile(path, devicetreeFixture()); - const response = await client.callTool({ - name: "inspect_devicetree", - arguments: { path, detail: "summary" }, - }); + const response = await client.callTool( + { name: "inspect_devicetree", arguments: { path, detail: "summary" } }, + { toolDefinition: schemas.toolDefinition }, + ); expect(response.isError, JSON.stringify(response)).not.toBe(true); - expect( - schemas.ajv.validate(schemas.outputSchema, response.structuredContent), - JSON.stringify(schemas.ajv.errors), - ).toBe(true); const result = toolContract("inspect_devicetree").outputSchema.parse( response.structuredContent, ).normalized_result; @@ -107,8 +103,6 @@ mcpTest( await session.close(); } }, - // Includes SDK schema conversion, dialect validation and two public calls. - 60_000, ); mcpTest( "pages nodes without repeating a large bootargs summary", @@ -170,7 +164,6 @@ mcpTest( await session.close(); } }, - 60_000, ); mcpTest( "keeps a malformed canonical phandle unresolved in retained reference pages", @@ -235,7 +228,6 @@ mcpTest( await session.close(); } }, - 60_000, ); mcpTest("rejects relative paths and unsupported versions", async ({ mcp }) => { const session = createTestBinarySession(() => { diff --git a/tests/boundary/mcp/electronFusesMcp.test.ts b/tests/boundary/mcp/electronFusesMcp.test.ts index a18b02d893..01b1b2daaf 100644 --- a/tests/boundary/mcp/electronFusesMcp.test.ts +++ b/tests/boundary/mcp/electronFusesMcp.test.ts @@ -82,15 +82,11 @@ fuseMcpTest( const elf = elfFuseImage([ELECTRON_FUSE_STATE_BYTES.removed, 0x99]); const path = join(root, "application"); await writeFile(path, elf.bytes); - const response = await client.callTool({ - name: "inspect_electron_fuses", - arguments: { path }, - }); + const response = await client.callTool( + { name: "inspect_electron_fuses", arguments: { path } }, + { toolDefinition: advertised }, + ); expect(response.isError, JSON.stringify(response.content)).not.toBe(true); - expect( - ajv.validate(outputSchema, response.structuredContent), - JSON.stringify(ajv.errors), - ).toBe(true); const parsed = toolContract("inspect_electron_fuses").outputSchema.parse( response.structuredContent, ); diff --git a/tests/boundary/mcp/electronMcp.test.ts b/tests/boundary/mcp/electronMcp.test.ts index bee6486868..51f7f95901 100644 --- a/tests/boundary/mcp/electronMcp.test.ts +++ b/tests/boundary/mcp/electronMcp.test.ts @@ -421,7 +421,7 @@ it("exposes endpoint-scoped Electron discovery and inspection as Evidence", asyn ], }, }); -}, 20_000); +}); /** Create the selected-path aliases and retained IPC-gap capture for the public workflow. */ const activeElectronScenarioFixture = async () => { @@ -614,7 +614,7 @@ it("runs active Electron scenarios with selected paths and inferred working dire ], }, }); -}, 20_000); +}); it("exposes the target-free static JavaScript application workflow", async () => { const root = await createTestTempDirectory("rea-electron-static-mcp-"); diff --git a/tests/boundary/mcp/enhancedTools.traceFeatureCoverage.test.ts b/tests/boundary/mcp/enhancedTools.traceFeatureCoverage.test.ts index 99fa648ef7..40e78fd670 100644 --- a/tests/boundary/mcp/enhancedTools.traceFeatureCoverage.test.ts +++ b/tests/boundary/mcp/enhancedTools.traceFeatureCoverage.test.ts @@ -3,7 +3,6 @@ import { afterEach, describe, expect, it } from "vitest"; import type { AnalysisOperationPort } from "../../../src/application/AnalysisProvider.js"; import { createAnalysisExecution } from "../../../src/application/AnalysisProvider.js"; import { EnhancedTools } from "../../../src/application/EnhancedTools.js"; -import { TRACE_FEATURE_REFERENCE_LIMITATION } from "../../../src/application/WorkflowEvidence.js"; import { AnalysisCapabilityUnavailableError, AnalysisOutputError, @@ -438,12 +437,11 @@ describe("trace_feature reference coverage", () => { const output = await trace(provider({ emptyXrefsOf: "0x10" })); expect(output.normalized_result).toEqual(expectedTrace([], [], ["0x10"])); - expect(output.limitations).toContain(TRACE_FEATURE_REFERENCE_LIMITATION); - expect(TRACE_FEATURE_REFERENCE_LIMITATION).toMatch(/start address/); - expect(TRACE_FEATURE_REFERENCE_LIMITATION).toMatch( - /interior bytes of strings and procedures/, + expect(output.limitations).toContainEqual( + expect.stringMatching( + /start address.*interior bytes of strings and procedures.*truncated: false/u, + ), ); - expect(TRACE_FEATURE_REFERENCE_LIMITATION).toMatch(/truncated: false/); }); it("marks the trace truncated for the target whose references the provider capped", async () => { diff --git a/tests/boundary/mcp/environmentBundleMcp.test.ts b/tests/boundary/mcp/environmentBundleMcp.test.ts index 47244b7243..c3ab6b8d1e 100644 --- a/tests/boundary/mcp/environmentBundleMcp.test.ts +++ b/tests/boundary/mcp/environmentBundleMcp.test.ts @@ -107,7 +107,7 @@ it("reports an unmodelled literal delete without claiming recorded reads are abs it("writes the bundle environment-build writes from retained capture Evidence", async () => { const { client, session } = await connect(); - const { ajv, inputSchema, outputSchema } = await advertisedMcpToolSchemas( + const { ajv, inputSchema, toolDefinition } = await advertisedMcpToolSchemas( client, TOOL, ); @@ -121,12 +121,11 @@ it("writes the bundle environment-build writes from retained capture Evidence", output_directory: join(root, "grafana"), }; expect(ajv.validate(inputSchema, input)).toBe(true); - const response = await client.callTool({ name: TOOL, arguments: input }); + const response = await client.callTool( + { name: TOOL, arguments: input }, + { toolDefinition }, + ); expect(response.isError, JSON.stringify(response)).not.toBe(true); - expect( - ajv.validate(outputSchema, response.structuredContent), - JSON.stringify(ajv.errors), - ).toBe(true); const report = toolContract(TOOL).outputSchema.parse( response.structuredContent, ); diff --git a/tests/boundary/mcp/environmentServeMcp.test.ts b/tests/boundary/mcp/environmentServeMcp.test.ts index ba29ccba49..4ea03ae2fa 100644 --- a/tests/boundary/mcp/environmentServeMcp.test.ts +++ b/tests/boundary/mcp/environmentServeMcp.test.ts @@ -22,7 +22,6 @@ import { type TestProcesses, } from "../../support/process/processFixture.js"; -const HTTP_BOUNDARY_TIMEOUT_MS = 90_000; const CARD_PATH = "/.well-known/agent-env.json"; /** SHA-256 of the 14,519-byte card served for the fixture named "renamed", its descriptions naming each tool's recorded responses; pure replay must serve these bytes. */ const REPLAY_CARD_SHA256 = @@ -89,7 +88,6 @@ processTest( await client.close(); } }, - HTTP_BOUNDARY_TIMEOUT_MS, ); processTest( @@ -123,7 +121,6 @@ processTest( await client.close(); } }, - HTTP_BOUNDARY_TIMEOUT_MS, ); processTest( @@ -153,7 +150,6 @@ processTest( await client.close(); } }, - HTTP_BOUNDARY_TIMEOUT_MS, ); processTest( @@ -199,7 +195,6 @@ processTest( await client.close(); } }, - HTTP_BOUNDARY_TIMEOUT_MS, ); describe("rea environment-serve --replay-only", () => { @@ -264,7 +259,6 @@ describe("rea environment-serve --replay-only", () => { await client.close(); } }, - HTTP_BOUNDARY_TIMEOUT_MS, ); processTest( @@ -307,7 +301,6 @@ describe("rea environment-serve --replay-only", () => { }); expect(started.stderr()).toContain('"transport":"streamable-http"'); }, - HTTP_BOUNDARY_TIMEOUT_MS, ); }); @@ -426,6 +419,5 @@ describe("rea environment-serve", () => { await client.close(); } }, - HTTP_BOUNDARY_TIMEOUT_MS, ); }); diff --git a/tests/boundary/mcp/environmentServeWritesMcp.test.ts b/tests/boundary/mcp/environmentServeWritesMcp.test.ts index c6c9fdfcb0..abfa5edf7d 100644 --- a/tests/boundary/mcp/environmentServeWritesMcp.test.ts +++ b/tests/boundary/mcp/environmentServeWritesMcp.test.ts @@ -37,8 +37,6 @@ import { type JsonValue, } from "../../../src/domain/jsonValue.js"; -const HTTP_BOUNDARY_TIMEOUT_MS = 90_000; - /** The book the first served create of "Boundary Book" makes: one second past the served clock's start. */ const BOUNDARY_BOOK = book(3, "Boundary Book", bookClock(13)); @@ -160,7 +158,6 @@ processTest( await client.close(); } }, - HTTP_BOUNDARY_TIMEOUT_MS, ); processTest( @@ -220,7 +217,6 @@ processTest( await client.close(); } }, - HTTP_BOUNDARY_TIMEOUT_MS, ); processTest( @@ -357,7 +353,6 @@ processTest( await client.close(); } }, - HTTP_BOUNDARY_TIMEOUT_MS, ); describe("rea environment-serve write tools", () => { @@ -428,7 +423,6 @@ describe("rea environment-serve write tools", () => { await client.close(); } }, - HTTP_BOUNDARY_TIMEOUT_MS, ); processTest( @@ -504,7 +498,6 @@ describe("rea environment-serve write tools", () => { await client.close(); } }, - HTTP_BOUNDARY_TIMEOUT_MS, ); }); @@ -597,7 +590,6 @@ processTest( await client.close(); } }, - HTTP_BOUNDARY_TIMEOUT_MS, ); processTest.for([ @@ -619,7 +611,6 @@ processTest.for([ })), ])( "withholds $label from public tools", - { timeout: HTTP_BOUNDARY_TIMEOUT_MS }, async ({ rows, read, write }, { processes }) => { const root = await createTestTempDirectory("rea-create-reference-mcp-"); const files = await writeEnvironmentServeFiles( diff --git a/tests/boundary/mcp/erlangBeamMcp.test.ts b/tests/boundary/mcp/erlangBeamMcp.test.ts index 0d57115c11..e9e04144b3 100644 --- a/tests/boundary/mcp/erlangBeamMcp.test.ts +++ b/tests/boundary/mcp/erlangBeamMcp.test.ts @@ -206,5 +206,4 @@ mcpTest( await session.close(); } }, - 60_000, ); diff --git a/tests/boundary/mcp/espImageMcp.test.ts b/tests/boundary/mcp/espImageMcp.test.ts index 34cc035ab0..bc15db92ea 100644 --- a/tests/boundary/mcp/espImageMcp.test.ts +++ b/tests/boundary/mcp/espImageMcp.test.ts @@ -111,5 +111,4 @@ it.each(["full", "compact"] as const)( await rm(outputRoot, { recursive: true, force: true }); } }, - 60_000, ); diff --git a/tests/boundary/mcp/evidenceImportCancellation.test.ts b/tests/boundary/mcp/evidenceImportCancellation.test.ts index cd1fe90b33..0ba700cc6b 100644 --- a/tests/boundary/mcp/evidenceImportCancellation.test.ts +++ b/tests/boundary/mcp/evidenceImportCancellation.test.ts @@ -293,7 +293,6 @@ it("retries a transient import close failure before admitting the bundle", async ); await writeFile(path, JSON.stringify(createEvidenceBundle([imported]))); - const originalClose = OwnedFileHandle.prototype.close; const close = vi .spyOn(OwnedFileHandle.prototype, "close") .mockImplementationOnce(async () => { diff --git a/tests/boundary/mcp/evidenceLinkInspectionMcp.test.ts b/tests/boundary/mcp/evidenceLinkInspectionMcp.test.ts index 2981381ca3..7da3a8ff1f 100644 --- a/tests/boundary/mcp/evidenceLinkInspectionMcp.test.ts +++ b/tests/boundary/mcp/evidenceLinkInspectionMcp.test.ts @@ -8,7 +8,6 @@ import { createEvidence, parseEvidence } from "../../../src/domain/evidence.js"; import { createEvidenceBundle } from "../../../src/domain/evidenceBundle.js"; import { EVIDENCE_LINK_INSPECTION_LIMIT, - EVIDENCE_LINK_INSPECTION_LIMITATIONS, evidenceBundleSummarySchema, evidenceLinkInspectionSchema, } from "../../../src/domain/evidenceRecordSummary.js"; @@ -74,13 +73,16 @@ const connect = async ( }); for (const retained of options.records ?? PARTIAL) expect(session.recordEvidence(retained).ok).toBe(true); - const reply = (args: Record) => - client.callTool({ name: "get_evidence_bundle", arguments: args }); // Every delivered result must satisfy what this profile actually advertises. const { tools } = await client.listTools(); const advertised = tools.find(({ name }) => name === "get_evidence_bundle"); if (advertised?.outputSchema === undefined) throw new Error("get_evidence_bundle advertises no output schema"); + const reply = (args: Record) => + client.callTool( + { name: "get_evidence_bundle", arguments: args }, + { toolDefinition: advertised }, + ); const inputSchema: Record = advertised.inputSchema; const outputSchema: Record = advertised.outputSchema; const validation = schemaValidation.assertions; @@ -93,10 +95,6 @@ const connect = async ( ).toBe(true); const result = await reply(args); expect(result.isError, JSON.stringify(result)).not.toBe(true); - expect( - validation.validate(outputSchema, result.structuredContent), - JSON.stringify(validation.errors), - ).toBe(true); return z.object({ result: z.unknown() }).parse(result.structuredContent) .result; }; @@ -137,7 +135,9 @@ it.each(["full", "compact"] as const)( matching_links: 0, next_offset: null, rows: [], - limitations: [...EVIDENCE_LINK_INSPECTION_LIMITATIONS], + limitations: [ + expect.stringMatching(/missing means absent here.*analysis coverage/u), + ], }); // The record that names the absent source reports it without any payload. diff --git a/tests/boundary/mcp/evidenceRecordSelectionMcp.test.ts b/tests/boundary/mcp/evidenceRecordSelectionMcp.test.ts index 86d267d16d..afb9aec137 100644 --- a/tests/boundary/mcp/evidenceRecordSelectionMcp.test.ts +++ b/tests/boundary/mcp/evidenceRecordSelectionMcp.test.ts @@ -6,7 +6,6 @@ import { createEvidence, parseEvidence } from "../../../src/domain/evidence.js"; import { MCP_TRANSPORT_EVIDENCE_RECORDS } from "../../../src/domain/analysisErrorPresentation.js"; import { EVIDENCE_RECORD_SELECTION_LIMIT, - EVIDENCE_RECORD_SELECTION_LIMITATIONS, evidenceRecordSelectionSchema, } from "../../../src/domain/evidenceRecordSummary.js"; import { createToolResultDelivery } from "../../../src/server/toolResult.js"; @@ -85,13 +84,21 @@ const connect = async ( }); for (const record of options.records ?? LEDGER) expect(session.recordEvidence(record).ok).toBe(true); + const definitions = (await client.listTools()).tools.filter( + (tool) => tool.name === "get_evidence_bundle", + ); + if (definitions.length !== 1 || definitions[0]?.outputSchema === undefined) + throw new Error("Expected one advertised evidence-bundle output schema"); + const toolDefinition = definitions[0]; const reply = (args: Record) => - client.callTool({ name: "get_evidence_bundle", arguments: args }); + client.callTool( + { name: "get_evidence_bundle", arguments: args }, + { toolDefinition }, + ); const select = async (args: Record) => { const result = await reply({ detail: "records", ...args }); expect(result.isError, JSON.stringify(result)).not.toBe(true); return { - result, selection: evidenceRecordSelectionSchema.parse( z.object({ result: z.unknown() }).parse(result.structuredContent) .result, @@ -123,9 +130,11 @@ it.each(["full", "compact"] as const)( expect(parseEvidence(exact.selection.records[0]).evidence_id).toBe( annotatedA.evidence_id, ); - // Historical and non-bundle labels come from their owning constant. expect(exact.selection.limitations).toEqual([ - ...EVIDENCE_RECORD_SELECTION_LIMITATIONS, + expect.stringMatching( + /historical observations.*not establish current live/u, + ), + expect.stringMatching(/not an Evidence bundle.*cannot be imported/u), ]); const { tools } = await client.listTools(); @@ -140,11 +149,6 @@ it.each(["full", "compact"] as const)( limit: 1, }), ).toBe(true); - const outputSchema: Record = tool.outputSchema!; - expect(ajv.validate(outputSchema, exact.result.structuredContent)).toBe( - true, - ); - // Pages follow canonical bundle order across distinct targets. const annotations = canonical([annotatedA, annotatedB]); const first = await select({ diff --git a/tests/boundary/mcp/evidenceRetentionRecovery.test.ts b/tests/boundary/mcp/evidenceRetentionRecovery.test.ts index 17a9b16122..0d30615509 100644 --- a/tests/boundary/mcp/evidenceRetentionRecovery.test.ts +++ b/tests/boundary/mcp/evidenceRetentionRecovery.test.ts @@ -201,7 +201,7 @@ it("keeps an oversized unretained observation failure bounded and the connection await server.close(); await session.close(); } -}, 120000); +}); it("bounds fallback when the retention error itself has oversized diagnostics", async () => { const records = new InvestigationRecords({ maximumRepresentationBytes: 0 }); @@ -257,4 +257,4 @@ it("bounds fallback when the retention error itself has oversized diagnostics", await client.close(); await server.close(); } -}, 120000); +}); diff --git a/tests/boundary/mcp/evmInterfaceMcp.test.ts b/tests/boundary/mcp/evmInterfaceMcp.test.ts index a63b93533c..808742e557 100644 --- a/tests/boundary/mcp/evmInterfaceMcp.test.ts +++ b/tests/boundary/mcp/evmInterfaceMcp.test.ts @@ -1,3 +1,4 @@ +import { awaitAcknowledgement } from "../../fixtures/lifecycleAcknowledgement.js"; import { advertisedMcpToolSchemas, assertRecordedMcpEvidence, @@ -131,14 +132,16 @@ mcpTest( (tool) => tool.name === "inspect_evm_interface", ), ).toHaveLength(1); - const response = await client.callTool({ - name: "inspect_evm_interface", - arguments: { path: value.artifact.path, encoding: "hex" }, - }); + const response = await client.callTool( + { + name: "inspect_evm_interface", + arguments: { path: value.artifact.path, encoding: "hex" }, + }, + { toolDefinition: schemas.toolDefinition }, + ); assertRecordedMcpEvidence( response, "inspect_evm_interface", - schemas, session, value, "inferred", @@ -312,7 +315,8 @@ it("settles the admitted EVM operation after SDK cancellation and accepts a subs () => "completed", (error: unknown) => error, ); - await entered.promise; + onTestFinished(() => controller.abort()); + await awaitAcknowledgement(entered.promise, cancellation, "EVM inspection"); controller.abort(new Error("SDK EVM cancellation verification")); expect(await cancellation).toMatchObject({ message: expect.stringContaining("SDK EVM cancellation verification"), @@ -368,30 +372,28 @@ it("returns the exact inferred interface summary while preserving complete defau const input = { path: value.artifact.path, encoding: "hex" }; let original; for (const detail of [undefined, "complete"] as const) { - const response = await harness.client.callTool({ - name: "inspect_evm_interface", - arguments: { ...input, ...(detail ? { detail } : {}) }, - }); + const response = await harness.client.callTool( + { + name: "inspect_evm_interface", + arguments: { ...input, ...(detail ? { detail } : {}) }, + }, + { toolDefinition: definition }, + ); expect(response.isError).not.toBe(true); - expect( - ajv.validate(outputSchema, response.structuredContent), - JSON.stringify(ajv.errors), - ).toBe(true); const evidence = parseEvidence(response.structuredContent); expect(evidence.normalized_result).toEqual(value); expect(evidence.parameters).toEqual(input); original = evidence; } if (original === undefined) throw new Error("No complete control"); - const response = await harness.client.callTool({ - name: "inspect_evm_interface", - arguments: { ...input, detail: "summary" }, - }); + const response = await harness.client.callTool( + { + name: "inspect_evm_interface", + arguments: { ...input, detail: "summary" }, + }, + { toolDefinition: definition }, + ); expect(response.isError).not.toBe(true); - expect( - ajv.validate(outputSchema, response.structuredContent), - JSON.stringify(ajv.errors), - ).toBe(true); const derived = parseEvidence(response.structuredContent); const view = evmInterfaceSummaryViewResultSchema.parse( derived.normalized_result, diff --git a/tests/boundary/mcp/fridaLifecycleMcp.test.ts b/tests/boundary/mcp/fridaLifecycleMcp.test.ts index 572c32118b..1271ade8bb 100644 --- a/tests/boundary/mcp/fridaLifecycleMcp.test.ts +++ b/tests/boundary/mcp/fridaLifecycleMcp.test.ts @@ -13,6 +13,7 @@ import type { Session, } from "frida"; import { expect, it, onTestFinished, vi } from "vitest"; +import { awaitAcknowledgement } from "../../fixtures/lifecycleAcknowledgement.js"; import { FridaScriptLoadCancelledError } from "../../../src/application/frida/FridaInstrumentationPort.js"; import type { @@ -333,7 +334,11 @@ it.each(["attached", "during-resume", "after-resume"] as const)( name: "resume_frida_session", arguments: { session_id: id }, }); - await held.entered; + await awaitAcknowledgement( + held.entered, + resuming, + "Frida operation admission", + ); if (timing === "during-resume") fixture.markDetached(); expect(await state()).toBe( timing === "during-resume" ? "detached" : "paused", @@ -467,7 +472,11 @@ it.each([ (value) => ({ kind: "result" as const, value }), (cause: unknown) => ({ kind: "rejected" as const, cause }), ); - await held.entered; + await awaitAcknowledgement( + held.entered, + pending, + "Frida operation admission", + ); expect(fixture.unloaded).toEqual(["completed-before-detach"]); if (cancel) { controller.abort(new Error("cancel during final detach")); @@ -610,7 +619,11 @@ it("preserves the native resume failure after MCP cancellation reaches the serve { signal: controller.signal }, ) .catch((cause: unknown) => cause); - await held.entered; + await awaitAcknowledgement( + held.entered, + pending, + "Frida operation admission", + ); controller.abort(new Error("cancel pending resume")); await fixture.client.ping(); expect(serverSignal?.aborted).toBe(true); @@ -1034,7 +1047,11 @@ it.each(["load", "resume", "unload"] as const)( original.structuredContent, ).normalized_result.script_id; const first = fixture.load(id, "blocking"); - await held.entered; + await awaitAcknowledgement( + held.entered, + first, + "Frida operation admission", + ); const controller = new AbortController(); const cancelled = operation === "load" @@ -1078,7 +1095,11 @@ it.each(["create", "load"] as const)( const rejected = expect(cancelled).rejects.toThrow( "cancel native operation", ); - await held.entered; + await awaitAcknowledgement( + held.entered, + cancelled, + "Frida operation admission", + ); controller.abort(new Error("cancel native operation")); await fixture.client.ping(); await rejected; @@ -1120,7 +1141,11 @@ it("retains cancellation source facts without claiming native creation when no s const loading = fixture .load(id, "no-returned-handle", controller.signal) .catch((cause: unknown) => cause); - await held.entered; + await awaitAcknowledgement( + held.entered, + loading, + "Frida operation admission", + ); controller.abort(new Error("cancel while creating")); await fixture.client.ping(); held.release(); @@ -1166,7 +1191,11 @@ it("retains original malformed file identity and decoded representation through { signal: controller.signal }, ) .catch((cause: unknown) => cause); - await held.entered; + await awaitAcknowledgement( + held.entered, + loading, + "Frida operation admission", + ); controller.abort(new Error("cancel file load")); await fixture.client.ping(); held.release(); @@ -1235,7 +1264,11 @@ it.each([ (value) => ({ kind: "result" as const, value }), (cause: unknown) => ({ kind: "rejected" as const, cause }), ); - await held.entered; + await awaitAcknowledgement( + held.entered, + loading, + "Frida operation admission", + ); if (cancel) { controller.abort(new Error("cancel before native rejection")); await fixture.client.ping(); @@ -1308,7 +1341,11 @@ it("awaits an admitted attach on server shutdown and retries failed native clean }, }) .catch(() => undefined); - await held.entered; + await awaitAcknowledgement( + held.entered, + starting, + "Frida operation admission", + ); const { closing } = await pendingShutdown(fixture.server); expect(fixture.controls.detachCalls).toBe(0); held.release(); @@ -1343,7 +1380,11 @@ it("drains remote discovery before shutdown and retains failed connection cleanu }, }) .catch(() => undefined); - await held.entered; + await awaitAcknowledgement( + held.entered, + discovering, + "Frida operation admission", + ); const { closing } = await pendingShutdown(fixture.server); held.release(); expect(await closing).toBeInstanceOf(AggregateError); @@ -1363,7 +1404,11 @@ it.each(["create", "load"] as const)( const rejected = expect(cancelled).rejects.toThrow( "cancel retained script", ); - await held.entered; + await awaitAcknowledgement( + held.entered, + cancelled, + "Frida operation admission", + ); controller.abort(new Error("cancel retained script")); await fixture.client.ping(); await rejected; @@ -1416,7 +1461,11 @@ it.each([0, 1])( const rejected = expect(loading).rejects.toThrow( "cancel before queued resume", ); - await held.entered; + await awaitAcknowledgement( + held.entered, + loading, + "Frida operation admission", + ); const resume = () => fixture.client.callTool({ name: "resume_frida_session", @@ -1474,7 +1523,7 @@ it.each([0, 1, 2, 3, 4, 8])( }; const loading = fixture.load(id, "settling-native", controller.signal); const observed = loading.catch(() => undefined); - await held.entered; + await awaitAcknowledgement(held.entered, loading, "Frida script load"); const resuming = fixture.client.callTool({ name: "resume_frida_session", arguments: { session_id: id }, diff --git a/tests/boundary/mcp/fridaMcp.test.ts b/tests/boundary/mcp/fridaMcp.test.ts index 26d3e78cfe..6bdc8f6203 100644 --- a/tests/boundary/mcp/fridaMcp.test.ts +++ b/tests/boundary/mcp/fridaMcp.test.ts @@ -226,12 +226,11 @@ it("delivers Frida status summaries with complete retained Evidence for both tru arguments: { session_id: id, detail: "complete" }, }); expect(explicit.structuredContent).toEqual(parent); - const summary = await client.callTool({ - name: tool.name, - arguments: { session_id: id, detail: "summary" }, - }); + const summary = await client.callTool( + { name: tool.name, arguments: { session_id: id, detail: "summary" } }, + { toolDefinition: tool }, + ); expect(summary.isError).not.toBe(true); - expect(ajv.validate(outputSchema, summary.structuredContent)).toBe(true); const evidence = toolContract("frida_session_status").outputSchema.parse( summary.structuredContent, ); @@ -426,17 +425,19 @@ it("advertises Frida MCP schemas and keeps remote credentials out of results", a const outputSchema: Record = advertised.outputSchema; expect(ajv.validateSchema(inputSchema)).toBe(true); expect(ajv.validateSchema(outputSchema)).toBe(true); - const response = await client.callTool({ - name: "list_frida_devices", - arguments: { - remote: { address: "127.0.0.1:27042", token: "synthetic-secret-token" }, + const response = await client.callTool( + { + name: "list_frida_devices", + arguments: { + remote: { address: "127.0.0.1:27042", token: "synthetic-secret-token" }, + }, }, - }); + { toolDefinition: advertised }, + ); expect(response.isError).not.toBe(true); expect(JSON.stringify(response.structuredContent)).not.toContain( "synthetic-secret-token", ); - expect(ajv.validate(outputSchema, response.structuredContent)).toBe(true); expect( toolContract("list_frida_devices").outputSchema.parse( response.structuredContent, diff --git a/tests/boundary/mcp/gdbCancellationMcp.test.ts b/tests/boundary/mcp/gdbCancellationMcp.test.ts index 492ab15807..38e2f4b595 100644 --- a/tests/boundary/mcp/gdbCancellationMcp.test.ts +++ b/tests/boundary/mcp/gdbCancellationMcp.test.ts @@ -3,6 +3,7 @@ import { tmpdir } from "node:os"; import { join } from "node:path"; import { Client, InMemoryTransport } from "@modelcontextprotocol/client"; +import type { CallToolResult } from "@modelcontextprotocol/server"; import { McpServer } from "@modelcontextprotocol/server"; import { Ajv2020 } from "ajv/dist/2020.js"; import { expect, it } from "vitest"; @@ -28,6 +29,7 @@ import { createCacheProvider, createTestBinarySession, } from "../../fixtures/binarySession.js"; +import { parseMcpToolError } from "../../fixtures/mcpToolError.js"; import { validateDebuggerStatusResponse, waitForStartedFile, @@ -222,8 +224,32 @@ process.stdin.on("data", async (chunk) => { "gdb_session_status", sessionId, ); - expect(status.state).toBe("closed"); + expect(status).toMatchObject({ + state: "closed", + closed_reason: expect.stringContaining( + "A GDB MI command was cancelled", + ), + }); expect(status).toHaveProperty("diagnostics_truncated"); + // A later command names the retained reason instead of an argument error. + expect( + parseMcpToolError( + (await client.callTool({ + name: "gdb_console", + arguments: { session_id: sessionId, command: "info registers" }, + })) as CallToolResult, + ).error, + ).toMatchObject({ + code: "capability_unavailable", + message: expect.stringContaining( + "The GDB debug session stopped and accepts no more commands. A GDB MI command was cancelled", + ), + remediation: { + action: expect.stringMatching( + /close_gdb_session.*start_gdb_session/u, + ), + }, + }); await manager.closeAll(); expect(stopResults).toContain("stopped"); } finally { diff --git a/tests/boundary/mcp/gdbRetirementCancellation.test.ts b/tests/boundary/mcp/gdbRetirementCancellation.test.ts index 54e0e41c43..d8aabbd45a 100644 --- a/tests/boundary/mcp/gdbRetirementCancellation.test.ts +++ b/tests/boundary/mcp/gdbRetirementCancellation.test.ts @@ -25,122 +25,111 @@ import { createTestBinarySession, } from "../../fixtures/binarySession.js"; -it( - "compares cancellation before dispatch, during retirement, and an uncancelled control", - { timeout: 8000 }, - async () => { - for (const mode of [ - "pre-aborted", - "during-retirement", - "control", - ] as const) { - const [target] = await createBinarySessionTargets(); - const entered = createDeferred(); - const released = createDeferred(); - let closes = 0; - const session = createTestBinarySession( - () => - ({ - execute: async () => ok({ operation: "health", value: null }), - close: async () => { - closes++; - entered.resolve(); - await released.promise; - return ok(null); - }, - }) as never, +it("compares cancellation before dispatch, during retirement, and an uncancelled control", async () => { + for (const mode of ["pre-aborted", "during-retirement", "control"] as const) { + const [target] = await createBinarySessionTargets(); + const entered = createDeferred(); + const released = createDeferred(); + let closes = 0; + const session = createTestBinarySession( + () => + ({ + execute: async () => ok({ operation: "health", value: null }), + close: async () => { + closes++; + entered.resolve(); + await released.promise; + return ok(null); + }, + }) as never, + ); + expect((await session.open(target)).ok).toBe(true); + const id = "00000000-0000-4000-8000-000000000001"; + const evidence = createEvidence( + undefined, + { id: "gdb", name: "GDB", version: null }, + { + predicateType: "rea.debugger.command-observation", + operation: "gdb_console", + parameters: { session_id: id, command: "info files" }, + result: { command: "info files", completion_status: "complete" }, + rawResult: { command: "info files" }, + }, + ); + let handler!: (input: unknown, context: unknown) => Promise; + const server = { + registerTool(name: string, _options: unknown, fn: typeof handler) { + if (name === "gdb_console") handler = fn; + }, + } as unknown as McpServer; + const execute = vi.fn(async () => ok({ evidence })); + const manager = { + execute, + retryCleanupFailures: async () => ok(null), + } as unknown as GdbToolManager; + const record = vi.fn((value: typeof evidence) => + session.recordEvidence(value), + ); + registerGdbTools( + server, + manager, + silentLogger, + createToolResultDelivery(undefined), + record, + session, + ); + const controller = new AbortController(); + const reason = { phase: mode }; + if (mode === "pre-aborted") controller.abort(reason); + try { + const pending = handler( + { session_id: id, command: "info files", detail: "complete" }, + { mcpReq: { signal: controller.signal } }, ); - expect((await session.open(target)).ok).toBe(true); - const id = "00000000-0000-4000-8000-000000000001"; - const evidence = createEvidence( - undefined, - { id: "gdb", name: "GDB", version: null }, - { - predicateType: "rea.debugger.command-observation", - operation: "gdb_console", - parameters: { session_id: id, command: "info files" }, - result: { command: "info files", completion_status: "complete" }, - rawResult: { command: "info files" }, - }, - ); - let handler!: ( - input: unknown, - context: unknown, - ) => Promise; - const server = { - registerTool(name: string, _options: unknown, fn: typeof handler) { - if (name === "gdb_console") handler = fn; - }, - } as unknown as McpServer; - const execute = vi.fn(async () => ok({ evidence })); - const manager = { - execute, - retryCleanupFailures: async () => ok(null), - } as unknown as GdbToolManager; - const record = vi.fn((value: typeof evidence) => - session.recordEvidence(value), - ); - registerGdbTools( - server, - manager, - silentLogger, - createToolResultDelivery(undefined), - record, - session, - ); - const controller = new AbortController(); - const reason = { phase: mode }; - if (mode === "pre-aborted") controller.abort(reason); - try { - const pending = handler( - { session_id: id, command: "info files", detail: "complete" }, - { mcpReq: { signal: controller.signal } }, + if (mode !== "pre-aborted") { + await awaitAcknowledgement( + entered.promise, + pending, + "session retirement", ); - if (mode !== "pre-aborted") { - await awaitAcknowledgement( - entered.promise, - pending, - "session retirement", - ); - expect(execute).toHaveBeenCalledTimes(1); - expect(record).not.toHaveBeenCalled(); - if (mode === "during-retirement") controller.abort(reason); - } - released.resolve(); - const result = await pending; - const body = result.structuredContent as any; - const observation = { - mode, - aborted: controller.signal.aborted, - executions: execute.mock.calls.length, - retirementCloses: closes, - recordings: record.mock.calls.length, - isError: result.isError === true, - code: body.error?.code ?? null, - hasEvidence: typeof body.evidence_id === "string", - stored: session.hasEvidence(evidence.evidence_id), - }; - expect(observation).toEqual({ - mode, - aborted: mode !== "control", - executions: mode === "pre-aborted" ? 0 : 1, - retirementCloses: mode === "pre-aborted" ? 0 : 1, - recordings: mode === "pre-aborted" ? 0 : 1, - isError: mode !== "control", - code: mode === "control" ? null : "cancelled", - hasEvidence: mode === "control", - stored: mode !== "pre-aborted", - }); - - if (mode === "during-retirement") - expect(record.mock.calls[0]?.[0]).toEqual(evidence); - } finally { - released.resolve(); - await session.close(); + expect(execute).toHaveBeenCalledTimes(1); + expect(record).not.toHaveBeenCalled(); + if (mode === "during-retirement") controller.abort(reason); } + released.resolve(); + const result = await pending; + const body = result.structuredContent as any; + const observation = { + mode, + aborted: controller.signal.aborted, + executions: execute.mock.calls.length, + retirementCloses: closes, + recordings: record.mock.calls.length, + isError: result.isError === true, + code: body.error?.code ?? null, + hasEvidence: typeof body.evidence_id === "string", + stored: session.hasEvidence(evidence.evidence_id), + }; + expect(observation).toEqual({ + mode, + aborted: mode !== "control", + executions: mode === "pre-aborted" ? 0 : 1, + retirementCloses: mode === "pre-aborted" ? 0 : 1, + recordings: mode === "pre-aborted" ? 0 : 1, + isError: mode !== "control", + code: mode === "control" ? null : "cancelled", + hasEvidence: mode === "control", + stored: mode !== "pre-aborted", + }); + + if (mode === "during-retirement") + expect(record.mock.calls[0]?.[0]).toEqual(evidence); + } finally { + released.resolve(); + await session.close(); } - }, -); + } +}); it.each(["completed", "failed"] as const)( "preserves a %s command and cleanup ownership when cancellation overlaps failed retirement", diff --git a/tests/boundary/mcp/ghidraCompactReferencesMcp.test.ts b/tests/boundary/mcp/ghidraCompactReferencesMcp.test.ts index 980366ea12..468967b423 100644 --- a/tests/boundary/mcp/ghidraCompactReferencesMcp.test.ts +++ b/tests/boundary/mcp/ghidraCompactReferencesMcp.test.ts @@ -61,7 +61,7 @@ describe("compact Ghidra reference MCP evidence", () => { } finally { await harness.close(); } - }, 30_000); + }); it("preserves empty references and unresolved targets without manufacturing provenance", async () => { const outputs = { @@ -107,7 +107,7 @@ describe("compact Ghidra reference MCP evidence", () => { } finally { await harness.close(); } - }, 30_000); + }); it("preserves ARM TMode and Thumb decoder context across the MCP boundary", async () => { const instructionWithContext = (value: string | null) => ({ @@ -148,5 +148,5 @@ describe("compact Ghidra reference MCP evidence", () => { await harness.close(); } } - }, 30_000); + }); }); diff --git a/tests/boundary/mcp/ghidraDecompileDeadlineLifetime.test.ts b/tests/boundary/mcp/ghidraDecompileDeadlineLifetime.test.ts index 59104ae2ed..227411928b 100644 --- a/tests/boundary/mcp/ghidraDecompileDeadlineLifetime.test.ts +++ b/tests/boundary/mcp/ghidraDecompileDeadlineLifetime.test.ts @@ -126,7 +126,7 @@ it("tells a finished CLI invocation to rerun with the launch setting, not to reu const { message } = cliFailure.parse(projected); expect(message.startsWith(BRIDGE_MESSAGE)).toBe(true); expect(message).not.toContain(GHIDRA_SESSION_RETAINED); -}, 30_000); +}); it("keeps same-session and runtime-update guidance for a continuing MCP connection", async () => { const launcher = new GhidraFixtureLauncher("decompile_timed_out"); @@ -184,4 +184,4 @@ it("keeps same-session and runtime-update guidance for a continuing MCP connecti await harness.close(); await expectReleased(launcher); } -}, 30_000); +}); diff --git a/tests/boundary/mcp/ghidraEvidenceMcp.test.ts b/tests/boundary/mcp/ghidraEvidenceMcp.test.ts index fe1a09ec64..086c142cca 100644 --- a/tests/boundary/mcp/ghidraEvidenceMcp.test.ts +++ b/tests/boundary/mcp/ghidraEvidenceMcp.test.ts @@ -190,7 +190,7 @@ it("retains terminal-call qualifications across live dossiers and snapshot-backe expect(evidence.limitations, name).toContain(limitation); expect(evidence.analysis_profile?.parameters).toMatchObject({ function_boundary_observations: - "ghidra-function-boundary-limitations-v4", + "ghidra-function-boundary-limitations-v5", }); const replay = await harness.session.execute(name, evidence.parameters); if (!replay.ok) throw replay.error; @@ -736,5 +736,5 @@ describe("Ghidra MCP evidence parity", () => { } finally { await harness.close(); } - }, 30_000); + }); }); diff --git a/tests/boundary/mcp/ghidraFailureDiagnostics.test.ts b/tests/boundary/mcp/ghidraFailureDiagnostics.test.ts index 1649d538af..a40dbe93cf 100644 --- a/tests/boundary/mcp/ghidraFailureDiagnostics.test.ts +++ b/tests/boundary/mcp/ghidraFailureDiagnostics.test.ts @@ -99,7 +99,7 @@ it("preserves queue failure diagnostics through the provider, CLI adapter and SD process.exitCode = previousExitCode; await harness.close(); } -}, 30_000); +}); it.each( (["cancelled", "protocol"] as const).flatMap((kind) => diff --git a/tests/boundary/mcp/ghidraFileOffsetMcp.test.ts b/tests/boundary/mcp/ghidraFileOffsetMcp.test.ts index a30e19f262..d5be3e1afb 100644 --- a/tests/boundary/mcp/ghidraFileOffsetMcp.test.ts +++ b/tests/boundary/mcp/ghidraFileOffsetMcp.test.ts @@ -197,7 +197,7 @@ it("reports file-backed, uninitialized and unmapped addresses through MCP withou } finally { await mcp.close(); } -}, 30_000); +}); const segments = z.array( z.object({ name: z.string(), start: z.string(), end: z.string() }), @@ -259,7 +259,7 @@ it("maps the last byte of a file-backed block and refuses the first address past } finally { await mcp.close(); } -}, 30_000); +}); it("gives the CLI analysis path the same unbacked-address outcome as MCP", async () => { const launcher = new GhidraFixtureLauncher(); @@ -308,4 +308,4 @@ it("gives the CLI analysis path the same unbacked-address outcome as MCP", async } finally { await expectReleased(launcher); } -}, 30_000); +}); diff --git a/tests/boundary/mcp/ghidraMcpHarness.ts b/tests/boundary/mcp/ghidraMcpHarness.ts index 36fe94a0d9..a2f6f90eed 100644 --- a/tests/boundary/mcp/ghidraMcpHarness.ts +++ b/tests/boundary/mcp/ghidraMcpHarness.ts @@ -36,7 +36,7 @@ export const connectGhidraMcp = async ( readonly attestDosTargetSnapshot?: ReturnType["attestDosTargetSnapshot"]; readonly close?: ReturnType["close"]; readonly delivery?: ToolResultDelivery; - readonly environment?: Readonly>; + readonly environment?: Readonly; readonly schemaValidator?: ReturnType; } = {}, ) => { diff --git a/tests/boundary/mcp/ghidraMcpHarnessLifecycle.test.ts b/tests/boundary/mcp/ghidraMcpHarnessLifecycle.test.ts index aaaf396a09..d16a8b3672 100644 --- a/tests/boundary/mcp/ghidraMcpHarnessLifecycle.test.ts +++ b/tests/boundary/mcp/ghidraMcpHarnessLifecycle.test.ts @@ -37,8 +37,9 @@ it("reports independent cleanup failures and retries only retained owners", asyn .mockRejectedValueOnce(sdkFailure); try { const first = harness.close(); - expect(harness.close()).toBe(first); + const concurrent = harness.close().catch((cause: unknown) => cause); const failure: unknown = await first.catch((cause: unknown) => cause); + expect(failureLeaves(await concurrent)).toEqual(failureLeaves(failure)); expect(failureLeaves(failure)).toEqual( expect.arrayContaining([ sdkFailure, diff --git a/tests/boundary/mcp/ghidraNativeApiMcp.test.ts b/tests/boundary/mcp/ghidraNativeApiMcp.test.ts index 71b127a4dc..39c78d2dc7 100644 --- a/tests/boundary/mcp/ghidraNativeApiMcp.test.ts +++ b/tests/boundary/mcp/ghidraNativeApiMcp.test.ts @@ -57,10 +57,6 @@ it("retains complete load-image facts and pages selected relocations through the { toolDefinition: definition }, ); expect(reply.isError).not.toBe(true); - expect( - ajv.validate(outputSchema, reply.structuredContent), - JSON.stringify(ajv.errors), - ).toBe(true); return sessionEvidence(harness.session, reply.structuredContent); }; const complete = await call("inspect_native_load_image", {}); @@ -249,7 +245,7 @@ describe("Ghidra MCP native API evidence", () => { } finally { await harness.close(); } - }, 30_000); + }); it("returns complete native API boundaries without output-cap unknowns", async () => { const harness = await connectGhidraMcp("ghidra-native-api"); @@ -294,7 +290,7 @@ describe("Ghidra MCP native API evidence", () => { } finally { await harness.close(); } - }, 30_000); + }); }); it("retains unavailable Ghidra native API observations across live reobservation and provenance", async () => { @@ -413,4 +409,4 @@ it("retains unavailable Ghidra native API observations across live reobservation } finally { await harness.close(); } -}, 30_000); +}); diff --git a/tests/boundary/mcp/ghidraRequestCeilingMcp.test.ts b/tests/boundary/mcp/ghidraRequestCeilingMcp.test.ts index eef1059887..68c2543932 100644 --- a/tests/boundary/mcp/ghidraRequestCeilingMcp.test.ts +++ b/tests/boundary/mcp/ghidraRequestCeilingMcp.test.ts @@ -108,4 +108,4 @@ it("reports local Ghidra admission through SDK MCP without dispatching or losing for (const child of launcher.processes) expect(child.exitCode !== null || child.signalCode !== null).toBe(true); } -}, 30_000); +}); diff --git a/tests/boundary/mcp/ghidraRoutingMcp.test.ts b/tests/boundary/mcp/ghidraRoutingMcp.test.ts index 0b0160d133..7edc1b6801 100644 --- a/tests/boundary/mcp/ghidraRoutingMcp.test.ts +++ b/tests/boundary/mcp/ghidraRoutingMcp.test.ts @@ -78,7 +78,7 @@ describe("Ghidra MCP capability routing", () => { } finally { await harness.close(); } - }, 30_000); + }); it("keeps a Ghidra null-pseudocode observation's limitations on its batch item", async () => { const bodyless = "0x401020"; @@ -136,5 +136,5 @@ describe("Ghidra MCP capability routing", () => { } finally { await harness.close(); } - }, 30_000); + }); }); diff --git a/tests/boundary/mcp/ghidraStartupCancellationMcp.test.ts b/tests/boundary/mcp/ghidraStartupCancellationMcp.test.ts index cc33a01f10..bc92d1b7a5 100644 --- a/tests/boundary/mcp/ghidraStartupCancellationMcp.test.ts +++ b/tests/boundary/mcp/ghidraStartupCancellationMcp.test.ts @@ -13,6 +13,7 @@ import type { GhidraProviderClientFactory } from "../../../src/ghidra/GhidraProv import { silentLogger } from "../../../src/logger.js"; import { createServer } from "../../../src/server/createServer.js"; import { createDeferred } from "../../fixtures/binarySession.js"; +import { awaitAcknowledgement } from "../../fixtures/lifecycleAcknowledgement.js"; import { GhidraFixtureLauncher } from "../providers/ghidra/ghidraClient.fixture.js"; @@ -109,9 +110,17 @@ it.each([0, 1])( (cause: unknown) => ({ ok: false as const, cause }), ), ); - await admitted[index]?.promise; + await awaitAcknowledgement( + admitted[index]!.promise, + calls[index]!, + "MCP caller admission", + ); } - await entered.promise; + await awaitAcknowledgement( + entered.promise, + Promise.race(calls), + "Ghidra startup", + ); controllers[cancelledIndex]?.abort(); await vi.waitFor(() => expect(startResults[cancelledIndex]).toMatchObject({ diff --git a/tests/boundary/mcp/goBinaryMcp.test.ts b/tests/boundary/mcp/goBinaryMcp.test.ts index 0ccb8c3cd9..e3dc205f97 100644 --- a/tests/boundary/mcp/goBinaryMcp.test.ts +++ b/tests/boundary/mcp/goBinaryMcp.test.ts @@ -347,15 +347,11 @@ goMcpTest( moduleText: "path\texample.com/tool\nnew-record\tfuture\n", }); await writeFile(path, fixture.bytes); - const response = await client.callTool({ - name: "inspect_go_binary", - arguments: { path }, - }); + const response = await client.callTool( + { name: "inspect_go_binary", arguments: { path } }, + { toolDefinition: advertised }, + ); expect(response.isError).not.toBe(true); - expect( - ajv.validate(outputSchema, response.structuredContent), - JSON.stringify(ajv.errors), - ).toBe(true); const parsed = evidenceResultOf(goBinarySchema).parse( response.structuredContent, ); diff --git a/tests/boundary/mcp/godotPckMcp.test.ts b/tests/boundary/mcp/godotPckMcp.test.ts index 4c47f381fd..bff61f7029 100644 --- a/tests/boundary/mcp/godotPckMcp.test.ts +++ b/tests/boundary/mcp/godotPckMcp.test.ts @@ -40,7 +40,7 @@ const viewValidators = async (client: Client) => { const outputSchema: Record = contract.outputSchema; expect(ajv.validateSchema(inputSchema)).toBe(true); expect(ajv.validateSchema(outputSchema)).toBe(true); - return { ajv, inputSchema, outputSchema }; + return { ajv, inputSchema, toolDefinition: contract }; }; const retainedInventoryView = @@ -58,17 +58,13 @@ const retainedInventoryView = validators.ajv.validate(validators.inputSchema, arguments_), JSON.stringify(validators.ajv.errors), ).toBe(true); - const reply = await client.callTool({ - name: "inspect_analysis_view", - arguments: arguments_, - }); + const reply = await client.callTool( + { name: "inspect_analysis_view", arguments: arguments_ }, + { toolDefinition: validators.toolDefinition }, + ); expect(reply.isError, JSON.stringify(reply.structuredContent)).not.toBe( true, ); - expect( - validators.ajv.validate(validators.outputSchema, reply.structuredContent), - JSON.stringify(validators.ajv.errors), - ).toBe(true); const evidence = parseEvidence(reply.structuredContent); expect(evidence.evidence_links).toContain(evidenceId); return analysisViewResultSchema.parse(evidence.normalized_result); diff --git a/tests/boundary/mcp/guidedWorkflowsMcp.test.ts b/tests/boundary/mcp/guidedWorkflowsMcp.test.ts index ddedbed726..1f4799ba61 100644 --- a/tests/boundary/mcp/guidedWorkflowsMcp.test.ts +++ b/tests/boundary/mcp/guidedWorkflowsMcp.test.ts @@ -153,8 +153,6 @@ it.each([ ); } }, - // Full catalog startup took up to 39s with concurrent work on a shared host. - 60_000, ); it("uses the native prompt scope for tool discovery and rendering", async () => { diff --git a/tests/boundary/mcp/investigationAnalysisMcp.test.ts b/tests/boundary/mcp/investigationAnalysisMcp.test.ts index 2c0b08509c..c6f537eb57 100644 --- a/tests/boundary/mcp/investigationAnalysisMcp.test.ts +++ b/tests/boundary/mcp/investigationAnalysisMcp.test.ts @@ -71,7 +71,7 @@ it("aggregates comparison Evidence and records an approved runtime gap", async ( } finally { await close(session, server, client); } -}, 10_000); +}); it("builds exact-address paths with qualified callees and dossier citations", async () => { const { session, server, client } = await connected(); @@ -226,7 +226,7 @@ it("keeps call-path publication ahead of close_binary record draining", async () await Promise.allSettled(pending); await close(session, server, client); } -}, 10_000); +}); const dossierWithCallees = (addresses: readonly string[]) => { const base = FUNCTION_COMPARISON_EXAMPLE.left; diff --git a/tests/boundary/mcp/investigationEvidenceReferencesMcp.test.ts b/tests/boundary/mcp/investigationEvidenceReferencesMcp.test.ts index 427febcdf5..8a328e95d1 100644 --- a/tests/boundary/mcp/investigationEvidenceReferencesMcp.test.ts +++ b/tests/boundary/mcp/investigationEvidenceReferencesMcp.test.ts @@ -263,4 +263,4 @@ it("reports missing sources for a retained dossier with a large link set", async }, }, }); -}, 30_000); +}); diff --git a/tests/boundary/mcp/javascriptAnalysisResources.test.ts b/tests/boundary/mcp/javascriptAnalysisResources.test.ts index cfe46db6e9..49a77863e7 100644 --- a/tests/boundary/mcp/javascriptAnalysisResources.test.ts +++ b/tests/boundary/mcp/javascriptAnalysisResources.test.ts @@ -3,7 +3,7 @@ import { StdioClientTransport } from "@modelcontextprotocol/client/stdio"; import { createHash } from "node:crypto"; import { readFile, writeFile } from "node:fs/promises"; import { join, resolve } from "node:path"; -import { expect, it } from "vitest"; +import { afterAll, expect, it } from "vitest"; import { z } from "zod"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; @@ -12,6 +12,11 @@ import { recoveryInstructions } from "../../../src/server/recoveryInstructions.j import { UNTRUSTED_CONTENT_INSTRUCTIONS } from "../../../src/server/toolResultEncoding.js"; import { toolContract } from "../../../src/contracts/toolContracts.js"; import { retainedEvidenceReferenceSchema } from "../../../src/domain/evidenceReference.js"; +import { createMcpSchemaValidation } from "../../fixtures/mcpSchemaValidation.js"; + +// Compile SDK output validators for the tools a test calls, not the whole catalog. +const schemaValidation = createMcpSchemaValidation(); +afterAll(() => schemaValidation.clear()); it("delivers deadline recovery guidance at initialization before any tool call", async () => { const { client, transport } = createResourceClient(); @@ -174,7 +179,7 @@ it("accepts more work after application expansion exceeds memory and retains eve await client.close(); await transport.close(); } -}, 90000); +}); const repeatedSourceCount = 1500; const repeatedSourceText = "export const observed = 7;\n"; @@ -306,7 +311,7 @@ it("returns a directly usable partial analysis reference for an oversized timeou await client.close(); await transport.close(); } -}, 90000); +}); const verifyRecovery = async ( client: Client, @@ -345,7 +350,10 @@ const verifyRecovery = async ( const createResourceClient = ({ parentHeapMb = 512, }: { readonly parentHeapMb?: number } = {}) => { - const client = new Client({ name: "javascript-resources", version: "1" }); + const client = new Client( + { name: "javascript-resources", version: "1" }, + { jsonSchemaValidator: schemaValidation }, + ); const transport = new StdioClientTransport({ command: process.execPath, args: [resolve("scripts/rea.mjs"), "mcp"], diff --git a/tests/boundary/mcp/javascriptEndpointCandidate.test.ts b/tests/boundary/mcp/javascriptEndpointCandidate.test.ts index 6c315cf0b7..e8c9c50b6c 100644 --- a/tests/boundary/mcp/javascriptEndpointCandidate.test.ts +++ b/tests/boundary/mcp/javascriptEndpointCandidate.test.ts @@ -1,11 +1,10 @@ -import { Client } from "@modelcontextprotocol/client"; -import { StdioClientTransport } from "@modelcontextprotocol/client/stdio"; import { writeFile } from "node:fs/promises"; -import { join, resolve } from "node:path"; +import { join } from "node:path"; import { expect, it } from "vitest"; import { parseEvidence } from "../../../src/domain/evidence.js"; import { javascriptApplicationAnalysisResultSchema } from "../../../src/domain/javascript/javascriptApplicationAnalysis.js"; +import { connectLocalToolsMcp } from "../../fixtures/localToolsMcp.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; it("preserves Map omission and uncertain endpoint Evidence through public MCP", async () => { @@ -21,64 +20,51 @@ it("preserves Map omission and uncertain endpoint Evidence through public MCP", fetch('/network'); `, ); - const client = new Client({ name: "map-endpoint-regression", version: "1" }); - const transport = new StdioClientTransport({ - command: process.execPath, - args: [resolve("scripts/rea.mjs"), "mcp"], - cwd: process.cwd(), - env: { PATH: process.env.PATH ?? "" }, - stderr: "pipe", + const { client } = await connectLocalToolsMcp(); + const output = await client.callTool({ + name: "analyze_javascript_application", + arguments: { input_path: root }, }); - try { - await client.connect(transport); - const output = await client.callTool({ - name: "analyze_javascript_application", - arguments: { input_path: root }, - }); - expect(output.isError).not.toBe(true); - const evidence = parseEvidence(output.structuredContent); - const text = output.content.find((block) => block.type === "text"); - expect(text?.type === "text" ? JSON.parse(text.text) : undefined).toEqual( - evidence, - ); - const result = javascriptApplicationAnalysisResultSchema.parse( - evidence.normalized_result, - ); - const endpoints = result.graph.nodes.filter( - ({ kind }) => kind === "endpoint", - ); - expect( - endpoints - .flatMap(({ observations }) => - observations.map(({ properties }) => properties.value), - ) - .sort(), - ).toEqual(["/network", "/uncertain"]); - const candidate = endpoints.find( - ({ observations }) => observations[0]?.properties.value === "/uncertain", - ); - expect(candidate?.observations[0]).toMatchObject({ - properties: { interpretation: "candidate" }, - evidence: { - state: "inferred", - confidence: "low", - limitations: expect.arrayContaining([ - expect.stringContaining("only a candidate"), - ]), - }, - }); - expect( - result.graph.edges.find( - ({ target_node_id }) => target_node_id === candidate?.node_id, - )?.evidence, - ).toMatchObject({ state: "inferred", confidence: "low" }); - expect( - endpoints.find( - ({ observations }) => observations[0]?.properties.value === "/network", - )?.observations[0]?.evidence, - ).toMatchObject({ state: "observed", confidence: "exact" }); - } finally { - await client.close(); - await transport.close(); - } -}, 20_000); + expect(output.isError).not.toBe(true); + const evidence = parseEvidence(output.structuredContent); + const text = output.content.find((block) => block.type === "text"); + expect(text?.type === "text" ? JSON.parse(text.text) : undefined).toEqual( + evidence, + ); + const result = javascriptApplicationAnalysisResultSchema.parse( + evidence.normalized_result, + ); + const endpoints = result.graph.nodes.filter( + ({ kind }) => kind === "endpoint", + ); + expect( + endpoints + .flatMap(({ observations }) => + observations.map(({ properties }) => properties.value), + ) + .sort(), + ).toEqual(["/network", "/uncertain"]); + const candidate = endpoints.find( + ({ observations }) => observations[0]?.properties.value === "/uncertain", + ); + expect(candidate?.observations[0]).toMatchObject({ + properties: { interpretation: "candidate" }, + evidence: { + state: "inferred", + confidence: "low", + limitations: expect.arrayContaining([ + expect.stringContaining("only a candidate"), + ]), + }, + }); + expect( + result.graph.edges.find( + ({ target_node_id }) => target_node_id === candidate?.node_id, + )?.evidence, + ).toMatchObject({ state: "inferred", confidence: "low" }); + expect( + endpoints.find( + ({ observations }) => observations[0]?.properties.value === "/network", + )?.observations[0]?.evidence, + ).toMatchObject({ state: "observed", confidence: "exact" }); +}); diff --git a/tests/boundary/mcp/javascriptFactsViewMcp.test.ts b/tests/boundary/mcp/javascriptFactsViewMcp.test.ts index 38b27c241e..cdf36533bf 100644 --- a/tests/boundary/mcp/javascriptFactsViewMcp.test.ts +++ b/tests/boundary/mcp/javascriptFactsViewMcp.test.ts @@ -38,10 +38,15 @@ const facts = async ( source: unknown, view: unknown, ) => { - const wire = await harness.client.callTool({ - name: "inspect_analysis_view", - arguments: { source, view }, - }); + const matches = (await harness.client.listTools()).tools.filter( + (tool) => tool.name === "inspect_analysis_view", + ); + if (matches.length !== 1 || matches[0]?.outputSchema === undefined) + throw new Error("Expected one advertised analysis-view output schema"); + const wire = await harness.client.callTool( + { name: "inspect_analysis_view", arguments: { source, view } }, + { toolDefinition: matches[0] }, + ); expect(wire.isError).not.toBe(true); const evidence = parseEvidence(wire.structuredContent); const page = analysisViewResultSchema.parse(evidence.normalized_result); @@ -137,7 +142,7 @@ it("pages selected real filesystem observations with shared identities and porta property_keys: ["bytes", "parse_status", "operation", "not_present"], limit: 64, }; - const { ajv, inputSchema, outputSchema } = await advertisedMcpToolSchemas( + const { ajv, inputSchema } = await advertisedMcpToolSchemas( harness.client, "inspect_analysis_view", ); @@ -155,7 +160,6 @@ it("pages selected real filesystem observations with shared identities and porta error: { code: "invalid_request", category: "invalid_input" }, }); const full = await facts(harness, source, query); - expect(ajv.validate(outputSchema, full.wire.structuredContent)).toBe(true); const portable = await facts( harness, { kind: "inline", evidence: parent }, @@ -235,10 +239,7 @@ it("pages selected real filesystem observations with shared identities and porta expect(ajv.validate(inputSchema, { source, view: discoveryQuery })).toBe( true, ); - const discovery = await facts(harness, source, discoveryQuery); - expect(ajv.validate(outputSchema, discovery.wire.structuredContent)).toBe( - true, - ); + await facts(harness, source, discoveryQuery); }); const evidenceWithMixedLocations = () => { diff --git a/tests/boundary/mcp/javascriptLiteralBudget.test.ts b/tests/boundary/mcp/javascriptLiteralBudget.test.ts index 0a396bf233..6222051493 100644 --- a/tests/boundary/mcp/javascriptLiteralBudget.test.ts +++ b/tests/boundary/mcp/javascriptLiteralBudget.test.ts @@ -1,30 +1,15 @@ -import { Client } from "@modelcontextprotocol/client"; -import { StdioClientTransport } from "@modelcontextprotocol/client/stdio"; import { createHash } from "node:crypto"; import { readFile, writeFile } from "node:fs/promises"; -import { join, resolve } from "node:path"; -import { afterAll, beforeAll, expect } from "vitest"; +import { join } from "node:path"; +import { expect } from "vitest"; import { toolContract } from "../../../src/contracts/toolContracts.js"; import { javascriptApplicationAnalysisResultSchema } from "../../../src/domain/javascript/javascriptApplicationAnalysis.js"; +import { fileLocalToolsMcp } from "../../fixtures/localToolsMcp.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; import { cliTest } from "../../support/cli/cliFixture.js"; -const client = new Client({ name: "literal-budget-boundary", version: "1" }); -const transport = new StdioClientTransport({ - command: process.execPath, - args: [resolve("scripts/rea.mjs"), "mcp"], - cwd: process.cwd(), - env: { PATH: process.env.PATH ?? "" }, - stderr: "pipe", -}); -beforeAll(async () => { - await client.connect(transport); - transport.stderr?.on("data", () => undefined); -}); -afterAll(async () => { - await Promise.all([client.close(), transport.close()]); -}); +const mcp = fileLocalToolsMcp(); const large = "x".repeat(200_000); for (const scenario of [ @@ -61,13 +46,12 @@ for (const scenario of [ const digest = createHash("sha256").update(source).digest("hex"); const cliResponse = await cli.run({ arguments: ["analyze-javascript-application", root, "--json"], - timeoutMs: 60_000, }); expect(cliResponse.exitCode).toBe(0); const cliEvidence = toolContract( "analyze_javascript_application", ).outputSchema.parse(cliResponse.json); - const response = await client.callTool({ + const response = await mcp.client.callTool({ name: "analyze_javascript_application", arguments: { input_path: root }, }); @@ -131,7 +115,7 @@ for (const scenario of [ ).toMatchObject({ sha256: digest }); } expect(await readFile(path, "utf8")).toBe(source); - await client.ping(); + await mcp.client.ping(); }, ); } diff --git a/tests/boundary/mcp/javascriptLiteralPayload.test.ts b/tests/boundary/mcp/javascriptLiteralPayload.test.ts index fb8442f5db..3eb9649d4a 100644 --- a/tests/boundary/mcp/javascriptLiteralPayload.test.ts +++ b/tests/boundary/mcp/javascriptLiteralPayload.test.ts @@ -1,14 +1,13 @@ -import { Client } from "@modelcontextprotocol/client"; -import { StdioClientTransport } from "@modelcontextprotocol/client/stdio"; import { createHash } from "node:crypto"; import { writeFile } from "node:fs/promises"; -import { join, resolve } from "node:path"; +import { join } from "node:path"; import { expect } from "vitest"; import { toolContract } from "../../../src/contracts/toolContracts.js"; import { javascriptApplicationAnalysisResultSchema } from "../../../src/domain/javascript/javascriptApplicationAnalysis.js"; import { javaScriptSemanticTraceResultSchema } from "../../../src/domain/javascript/javascriptSemanticTraceSchemas.js"; import { compareUnicodeCodePoints } from "../../../src/domain/unicodeCodePointOrder.js"; +import { connectLocalToolsMcp } from "../../fixtures/localToolsMcp.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; import { cliTest } from "../../support/cli/cliFixture.js"; @@ -49,135 +48,119 @@ cliTest( "directory", "--json", ], - timeoutMs: 60_000, }); expect(cliResponse.exitCode).toBe(0); const cliEvidence = toolContract( "analyze_javascript_application", ).outputSchema.parse(cliResponse.json); - const client = new Client({ name: "literal-payload-e2e", version: "1" }); - const transport = new StdioClientTransport({ - command: process.execPath, - args: [resolve("scripts/rea.mjs"), "mcp"], - cwd: process.cwd(), - env: { PATH: process.env.PATH ?? "" }, - stderr: "pipe", + const { client } = await connectLocalToolsMcp(); + const response = await client.callTool({ + name: "analyze_javascript_application", + arguments: { input_path: root, format: "directory" }, }); - transport.stderr?.on("data", () => undefined); - try { - await client.connect(transport); - const response = await client.callTool({ - name: "analyze_javascript_application", - arguments: { input_path: root, format: "directory" }, - }); - // Default wire delivery must retain every admitted value in the aggregate. - expect(response.isError).not.toBe(true); - const evidence = toolContract( - "analyze_javascript_application", - ).outputSchema.parse(response.structuredContent); - expect(evidence.evidence_id).toBe(cliEvidence.evidence_id); - const text = response.content.find((item) => item.type === "text"); - if (text?.type !== "text") throw new Error("Missing MCP text result"); - const textEvidence = toolContract( - "analyze_javascript_application", - ).outputSchema.parse(JSON.parse(text.text)); - expect(textEvidence.evidence_id).toBe(evidence.evidence_id); - const graph = javascriptApplicationAnalysisResultSchema.parse( - evidence.normalized_result, - ).semantic_graph; - const retainedPayloads = graph.nodes - .filter( - (node) => - node.kind === "literal" && - typeof node.properties.value === "string" && - values.includes(node.properties.value), - ) - .map((node) => node.properties.value); - expect(retainedPayloads.toSorted()).toEqual(values.toSorted()); - const literal = graph.nodes.find( - (node) => node.kind === "literal" && node.properties.value === value, - ); - if (literal === undefined) throw new Error("Complete literal was lost"); - const sourceSha256 = createHash("sha256").update(source).digest("hex"); - expect(literal.identity).toMatchObject({ - module_path: "main.js", - artifact_sha256: sourceSha256, - }); - expect(literal.evidence.location).toMatchObject({ - available: true, - value: { kind: "source-range", source: "main.js" }, - }); - const choices = graph.nodes.filter( + // Default wire delivery must retain every admitted value in the aggregate. + expect(response.isError).not.toBe(true); + const evidence = toolContract( + "analyze_javascript_application", + ).outputSchema.parse(response.structuredContent); + expect(evidence.evidence_id).toBe(cliEvidence.evidence_id); + const text = response.content.find((item) => item.type === "text"); + if (text?.type !== "text") throw new Error("Missing MCP text result"); + const textEvidence = toolContract( + "analyze_javascript_application", + ).outputSchema.parse(JSON.parse(text.text)); + expect(textEvidence.evidence_id).toBe(evidence.evidence_id); + const graph = javascriptApplicationAnalysisResultSchema.parse( + evidence.normalized_result, + ).semantic_graph; + const retainedPayloads = graph.nodes + .filter( (node) => node.kind === "literal" && - (node.properties.value === 1 || node.properties.value === "1"), - ); - expect(choices.length).toBe(4); - expect(new Set(choices.map((node) => node.node_id)).size).toBe(4); - expect( - graph.nodes.some( - (node) => node.kind === "literal" && node.properties.value === "", - ), - ).toBe(true); + typeof node.properties.value === "string" && + values.includes(node.properties.value), + ) + .map((node) => node.properties.value); + expect(retainedPayloads.toSorted()).toEqual(values.toSorted()); + const literal = graph.nodes.find( + (node) => node.kind === "literal" && node.properties.value === value, + ); + if (literal === undefined) throw new Error("Complete literal was lost"); + const sourceSha256 = createHash("sha256").update(source).digest("hex"); + expect(literal.identity).toMatchObject({ + module_path: "main.js", + artifact_sha256: sourceSha256, + }); + expect(literal.evidence.location).toMatchObject({ + available: true, + value: { kind: "source-range", source: "main.js" }, + }); + const choices = graph.nodes.filter( + (node) => + node.kind === "literal" && + (node.properties.value === 1 || node.properties.value === "1"), + ); + expect(choices.length).toBe(4); + expect(new Set(choices.map((node) => node.node_id)).size).toBe(4); + expect( + graph.nodes.some( + (node) => node.kind === "literal" && node.properties.value === "", + ), + ).toBe(true); - const traceResponse = await client.callTool({ - name: "trace_javascript_semantics", - arguments: { - application: { - kind: "retained-evidence", - evidence_id: evidence.evidence_id, - }, - query: { - seed: { kind: "semantic-node", node_id: literal.node_id }, - direction: "backward-provenance", - }, + const traceResponse = await client.callTool({ + name: "trace_javascript_semantics", + arguments: { + application: { + kind: "retained-evidence", + evidence_id: evidence.evidence_id, }, - }); - expect(traceResponse.isError).not.toBe(true); - const traceEvidence = toolContract( - "trace_javascript_semantics", - ).outputSchema.parse(traceResponse.structuredContent); - const trace = javaScriptSemanticTraceResultSchema.parse( - traceEvidence.normalized_result, - ); - expect( - trace.nodes.some( - (node) => - node.node_id === literal.node_id && node.properties.value === value, - ), - ).toBe(true); - const valueTraceResponse = await client.callTool({ - name: "trace_javascript_semantics", - arguments: { - application: { - kind: "retained-evidence", - evidence_id: evidence.evidence_id, - }, - query: { - seed: { kind: "literal", value: "1" }, - direction: "backward-provenance", - }, + query: { + seed: { kind: "semantic-node", node_id: literal.node_id }, + direction: "backward-provenance", }, - }); - expect(valueTraceResponse.isError).not.toBe(true); - const valueTraceEvidence = toolContract( - "trace_javascript_semantics", - ).outputSchema.parse(valueTraceResponse.structuredContent); - const valueTrace = javaScriptSemanticTraceResultSchema.parse( - valueTraceEvidence.normalized_result, - ); - expect(valueTrace.seed_node_ids).toEqual( - choices - .filter((node) => node.properties.value === "1") - .map((node) => node.node_id) - .sort(compareUnicodeCodePoints), - ); - await client.ping(); - } finally { - await client.close(); - await transport.close(); - } + }, + }); + expect(traceResponse.isError).not.toBe(true); + const traceEvidence = toolContract( + "trace_javascript_semantics", + ).outputSchema.parse(traceResponse.structuredContent); + const trace = javaScriptSemanticTraceResultSchema.parse( + traceEvidence.normalized_result, + ); + expect( + trace.nodes.some( + (node) => + node.node_id === literal.node_id && node.properties.value === value, + ), + ).toBe(true); + const valueTraceResponse = await client.callTool({ + name: "trace_javascript_semantics", + arguments: { + application: { + kind: "retained-evidence", + evidence_id: evidence.evidence_id, + }, + query: { + seed: { kind: "literal", value: "1" }, + direction: "backward-provenance", + }, + }, + }); + expect(valueTraceResponse.isError).not.toBe(true); + const valueTraceEvidence = toolContract( + "trace_javascript_semantics", + ).outputSchema.parse(valueTraceResponse.structuredContent); + const valueTrace = javaScriptSemanticTraceResultSchema.parse( + valueTraceEvidence.normalized_result, + ); + expect(valueTrace.seed_node_ids).toEqual( + choices + .filter((node) => node.properties.value === "1") + .map((node) => node.node_id) + .sort(compareUnicodeCodePoints), + ); + await client.ping(); }, - 60_000, ); diff --git a/tests/boundary/mcp/javascriptNativeHandoffIdentity.test.ts b/tests/boundary/mcp/javascriptNativeHandoffIdentity.test.ts index 851a28397b..29613f874d 100644 --- a/tests/boundary/mcp/javascriptNativeHandoffIdentity.test.ts +++ b/tests/boundary/mcp/javascriptNativeHandoffIdentity.test.ts @@ -1,9 +1,7 @@ -import { Client } from "@modelcontextprotocol/client"; -import { StdioClientTransport } from "@modelcontextprotocol/client/stdio"; import { createHash } from "node:crypto"; import { readFile, rm, writeFile } from "node:fs/promises"; -import { join, resolve } from "node:path"; -import { afterAll, beforeAll, expect } from "vitest"; +import { join } from "node:path"; +import { expect } from "vitest"; import { toolContract } from "../../../src/contracts/toolContracts.js"; import { analysisViewResultSchema } from "../../../src/domain/analysisView/analysisView.js"; @@ -12,24 +10,11 @@ import { applicationFeatureTraceResultSchema, type ApplicationFeatureTraceResult, } from "../../../src/domain/javascript/javascriptFeatureTraceSchemas.js"; +import { fileLocalToolsMcp } from "../../fixtures/localToolsMcp.js"; import { createStrippedAsarAddon } from "../../fixtures/strippedAsarAddon.js"; import { cliTest } from "../../support/cli/cliFixture.js"; -const client = new Client({ name: "native-handoff-identity", version: "1" }); -const transport = new StdioClientTransport({ - command: process.execPath, - args: [resolve("scripts/rea.mjs"), "mcp"], - cwd: process.cwd(), - env: { PATH: process.env.PATH ?? "" }, - stderr: "pipe", -}); -beforeAll(async () => { - await client.connect(transport); - transport.stderr?.on("data", () => undefined); -}); -afterAll(async () => { - await Promise.all([client.close(), transport.close()]); -}); +const mcp = fileLocalToolsMcp(); const sha256 = (value: Uint8Array) => createHash("sha256").update(value).digest("hex"); @@ -126,7 +111,7 @@ for (const available of [false, true]) { const parent = toolContract( "analyze_javascript_application", ).outputSchema.parse(analysis.json); - const summary = await client.callTool({ + const summary = await mcp.client.callTool({ name: "analyze_javascript_application", arguments: { input_path: archive, detail: "summary" }, }); @@ -179,7 +164,7 @@ for (const available of [false, true]) { const cliEvidence = toolContract( "trace_application_feature", ).outputSchema.parse(cliTrace.json); - const response = await client.callTool({ + const response = await mcp.client.callTool({ name: "trace_application_feature", arguments: { ...query, @@ -217,7 +202,7 @@ for (const available of [false, true]) { expect(sha256(await readFile(archive))).toBe(archiveDigest); if (available) expect(sha256(await readFile(companion))).toBe(nativeDigest); - await client.ping(); + await mcp.client.ping(); }, ); } diff --git a/tests/boundary/mcp/javascriptRecoveryMcp.test.ts b/tests/boundary/mcp/javascriptRecoveryMcp.test.ts index 60748e7c1a..b9eaf79dc1 100644 --- a/tests/boundary/mcp/javascriptRecoveryMcp.test.ts +++ b/tests/boundary/mcp/javascriptRecoveryMcp.test.ts @@ -57,15 +57,11 @@ it ...(multiple ? { additional_paths: [additional] } : {}), }; expect(ajv.validate(inputSchema, args)).toBe(true); - const response = await client.callTool({ - name: "recover_javascript_sources", - arguments: args, - }); + const response = await client.callTool( + { name: "recover_javascript_sources", arguments: args }, + { toolDefinition: advertised }, + ); expect(response.isError, JSON.stringify(response)).not.toBe(true); - expect( - ajv.validate(outputSchema, response.structuredContent), - JSON.stringify(ajv.errors), - ).toBe(true); const parsed = toolContract( "recover_javascript_sources", ).outputSchema.parse(response.structuredContent); diff --git a/tests/boundary/mcp/javascriptSourceSelection.test.ts b/tests/boundary/mcp/javascriptSourceSelection.test.ts index ef614f0ee9..cbb2c34896 100644 --- a/tests/boundary/mcp/javascriptSourceSelection.test.ts +++ b/tests/boundary/mcp/javascriptSourceSelection.test.ts @@ -5,7 +5,7 @@ import { createHash } from "node:crypto"; import { readFile, rm, writeFile } from "node:fs/promises"; import { StdioClientTransport } from "@modelcontextprotocol/client/stdio"; import { join, resolve } from "node:path"; -import { expect, it } from "vitest"; +import { afterAll, expect, it } from "vitest"; import { z } from "zod"; import { closeVerifierResources } from "../../../scripts/lib/verifier-cleanup.mjs"; @@ -19,12 +19,20 @@ import { import { createStrippedAsarAddon } from "../../fixtures/strippedAsarAddon.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; import { writeFixtureFiles } from "../../support/javascriptApplicationFixture.js"; +import { createMcpSchemaValidation } from "../../fixtures/mcpSchemaValidation.js"; + +// Compile SDK output validators for the tools a test calls, not the whole catalog. +const schemaValidation = createMcpSchemaValidation(); +afterAll(() => schemaValidation.clear()); const createSelectionClient = () => { - const client = new Client({ - name: "javascript-source-selection", - version: "1", - }); + const client = new Client( + { + name: "javascript-source-selection", + version: "1", + }, + { jsonSchemaValidator: schemaValidation }, + ); const transport = new StdioClientTransport({ command: process.execPath, args: [resolve("scripts/rea.mjs"), "mcp"], @@ -82,7 +90,7 @@ it("resolves ESM whitespace specifiers through the public application tool", asy () => transport.close(), ]); } -}, 60_000); +}); it("retains missing ASAR native-addon inventory through the public application tool", async () => { const { archive, addon } = await createStrippedAsarAddon(); @@ -115,7 +123,7 @@ it("retains missing ASAR native-addon inventory through the public application t () => transport.close(), ]); } -}, 60_000); +}); it("advertises exact source selection and retains its scope through summary, Evidence retrieval and invalid-input recovery", async () => { const root = await createTestTempDirectory("rea-js-selected-mcp-"); @@ -244,7 +252,7 @@ it("advertises exact source selection and retains its scope through summary, Evi () => transport.close(), ]); } -}, 60_000); +}); it("selects nested ASAR paths and distinguishes missing inventoried bytes from invalid paths", async () => { const root = await createTestTempDirectory("rea-js-selected-containers-"); @@ -315,7 +323,7 @@ it("selects nested ASAR paths and distinguishes missing inventoried bytes from i () => transport.close(), ]); } -}, 60_000); +}); it("accepts ASAR headers with long legal JSON whitespace through the public application tool", async () => { const root = await createTestTempDirectory("rea-js-asar-whitespace-mcp-"); @@ -358,7 +366,7 @@ it("accepts ASAR headers with long legal JSON whitespace through the public appl () => transport.close(), ]); } -}, 60_000); +}); it("does not parse ASAR-looking JSON beyond the declared JSON extent through MCP", async () => { const archive = join( @@ -395,4 +403,4 @@ it("does not parse ASAR-looking JSON beyond the declared JSON extent through MCP () => transport.close(), ]); } -}, 60_000); +}); diff --git a/tests/boundary/mcp/javascriptTimerDelay.test.ts b/tests/boundary/mcp/javascriptTimerDelay.test.ts index 71af083487..b6c9125549 100644 --- a/tests/boundary/mcp/javascriptTimerDelay.test.ts +++ b/tests/boundary/mcp/javascriptTimerDelay.test.ts @@ -1,18 +1,16 @@ -import { Client } from "@modelcontextprotocol/client"; -import { StdioClientTransport } from "@modelcontextprotocol/client/stdio"; import { createHash } from "node:crypto"; import { readFile, writeFile } from "node:fs/promises"; -import { join, resolve } from "node:path"; +import { join } from "node:path"; import { expect } from "vitest"; import { toolContract } from "../../../src/contracts/toolContracts.js"; import { javascriptApplicationAnalysisResultSchema } from "../../../src/domain/javascript/javascriptApplicationAnalysis.js"; +import { connectLocalToolsMcp } from "../../fixtures/localToolsMcp.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; import { cliTest } from "../../support/cli/cliFixture.js"; cliTest( "retains unknown timer delays and unrelated facts through public CLI and MCP", - { timeout: 60_000 }, async ({ cli }) => { const root = await createTestTempDirectory("rea-timer-delay-"); const source = [ @@ -66,44 +64,31 @@ cliTest( }); } - const client = new Client({ name: "timer-delay-boundary", version: "1" }); - const transport = new StdioClientTransport({ - command: process.execPath, - args: [resolve("scripts/rea.mjs"), "mcp"], - cwd: process.cwd(), - env: { PATH: process.env.PATH ?? "" }, - stderr: "pipe", + const { client } = await connectLocalToolsMcp(); + const reply = await client.callTool({ + name: contract.name, + arguments: { input_path: root }, }); - transport.stderr?.on("data", () => undefined); - try { - await client.connect(transport); - const reply = await client.callTool({ - name: contract.name, - arguments: { input_path: root }, - }); - expect(reply.isError).not.toBe(true); - const evidence = contract.outputSchema.parse(reply.structuredContent); - expect(evidence.evidence_id).toBe(cliEvidence.evidence_id); - const text = reply.content.find((item) => item.type === "text"); - if (text?.type !== "text") throw new Error("Missing inline Evidence"); - expect( - contract.outputSchema.parse(JSON.parse(text.text)).evidence_id, - ).toBe(evidence.evidence_id); - const retained = javascriptApplicationAnalysisResultSchema.parse( - evidence.normalized_result, - ).semantic_graph; - expect(retained.nodes.filter(({ kind }) => kind === "timer")).toEqual( - timers, - ); - expect( - retained.nodes.some( - ({ kind, label }) => kind === "function" && label === "greet", - ), - ).toBe(true); - await expect(client.ping()).resolves.toEqual({}); - } finally { - await Promise.all([client.close(), transport.close()]); - } + expect(reply.isError).not.toBe(true); + const evidence = contract.outputSchema.parse(reply.structuredContent); + expect(evidence.evidence_id).toBe(cliEvidence.evidence_id); + const text = reply.content.find((item) => item.type === "text"); + if (text?.type !== "text") throw new Error("Missing inline Evidence"); + expect(contract.outputSchema.parse(JSON.parse(text.text)).evidence_id).toBe( + evidence.evidence_id, + ); + const retained = javascriptApplicationAnalysisResultSchema.parse( + evidence.normalized_result, + ).semantic_graph; + expect(retained.nodes.filter(({ kind }) => kind === "timer")).toEqual( + timers, + ); + expect( + retained.nodes.some( + ({ kind, label }) => kind === "function" && label === "greet", + ), + ).toBe(true); + await expect(client.ping()).resolves.toEqual({}); expect(await readFile(path, "utf8")).toBe(source); }, ); diff --git a/tests/boundary/mcp/lateAbortApplicationEvidenceMcp.test.ts b/tests/boundary/mcp/lateAbortApplicationEvidenceMcp.test.ts index cd43628d70..24fa2d7a05 100644 --- a/tests/boundary/mcp/lateAbortApplicationEvidenceMcp.test.ts +++ b/tests/boundary/mcp/lateAbortApplicationEvidenceMcp.test.ts @@ -4,6 +4,7 @@ import { createTestBinarySession, createDeferred, } from "../../fixtures/binarySession.js"; +import { awaitAcknowledgement } from "../../fixtures/lifecycleAcknowledgement.js"; import { GoBinaryService } from "../../../src/application/go/GoBinaryService.js"; import type { GoBinary } from "../../../src/domain/go/goBinary.js"; import { silentLogger } from "../../../src/logger.js"; @@ -60,7 +61,7 @@ it("retains late-cancelled Go artifact Evidence through MCP and keeps the server { name: "inspect_go_binary", arguments: { path: report.artifact.path } }, { signal: controller.signal }, ); - await started.promise; + await awaitAcknowledgement(started.promise, call, "MCP observation"); controller.abort(); gate.resolve(); await expect(call).rejects.toThrow(); @@ -127,7 +128,7 @@ it("retains late-cancelled Inspector target Evidence through MCP and keeps the s }, { signal: controller.signal }, ); - await started.promise; + await awaitAcknowledgement(started.promise, call, "MCP observation"); controller.abort(); gate.resolve(); await expect(call).rejects.toThrow(); @@ -214,7 +215,7 @@ it("records late-cancelled Browser target Evidence and keeps the server usable", }, { signal: controller.signal }, ); - await started.promise; + await awaitAcknowledgement(started.promise, call, "MCP observation"); controller.abort(); gate.resolve(); await expect(call).rejects.toThrow(); diff --git a/tests/boundary/mcp/mainErrors.test.ts b/tests/boundary/mcp/mainErrors.test.ts index f7ce005646..52f1ac2d6c 100644 --- a/tests/boundary/mcp/mainErrors.test.ts +++ b/tests/boundary/mcp/mainErrors.test.ts @@ -8,7 +8,7 @@ import { MCP_CONNECTION_LOST, MCP_TRANSPORT_ERROR, MCP_CONNECTION_START_FAILED, - MCP_HTTP_LISTEN_FAILED, + mcpHttpListenFailed, MCP_SHUTDOWN_FAILED, SERVER_START_FAILED, } from "../../../src/main/messages.js"; @@ -251,7 +251,9 @@ describe("MCP runtime HTTP transport errors", () => { }), ), ).toBe(1); - expect(output).toEqual([`${refused.remediationAction}\n`]); + expect(output).toEqual([ + `${mcpHttpListenFailed(refused.remediationAction)}\n`, + ]); expect(output[0]).toContain("Set MCP_ALLOWED_HOSTS"); expect(output[0]).not.toContain("Choose another --port"); }); @@ -273,6 +275,38 @@ describe("MCP runtime HTTP transport errors", () => { }), ), ).toBe(1); - expect(output).toEqual([`${MCP_HTTP_LISTEN_FAILED}\n`]); + expect(output).toHaveLength(1); + expect(output[0]).toMatch( + /^REA could not listen for MCP HTTP connections\./u, + ); + expect(output[0]).not.toContain("SECRET"); }); + + it.each([ + ["ENOTFOUND", /host name resolution/u], + ["EADDRNOTAVAIL", /address assigned to this machine/u], + ["EACCES", /permitted to bind/u], + ["EADDRINUSE", /another --port or MCP_PORT/u], + ])( + "reports %s listener recovery from its parsed failure", + async (code, recovery) => { + const output: string[] = []; + const cause = Object.assign(new Error(`SECRET ${code}`), { code }); + expect( + await run( + dependencies({ + serve: () => Promise.reject(new StreamableHttpListenError(cause)), + output, + shutdown: [], + exitCodes: [], + }), + ), + ).toBe(1); + expect(output).toHaveLength(1); + expect(output[0]).toMatch(recovery); + expect(output[0]).not.toContain("SECRET"); + if (code !== "EADDRINUSE") + expect(output[0]).not.toContain("Choose another --port"); + }, + ); }); diff --git a/tests/boundary/mcp/managedArtifactSummaryMcp.test.ts b/tests/boundary/mcp/managedArtifactSummaryMcp.test.ts index 68b57aed0f..8ecd613779 100644 --- a/tests/boundary/mcp/managedArtifactSummaryMcp.test.ts +++ b/tests/boundary/mcp/managedArtifactSummaryMcp.test.ts @@ -55,15 +55,14 @@ it("returns a first-call overview and pages the unchanged source after its file { path, detail: "summary" }, ), ).toBe(false); - const reply = await client.callTool({ - name: "inspect_managed_artifact", - arguments: { path, detail: "summary" }, - }); + const reply = await client.callTool( + { + name: "inspect_managed_artifact", + arguments: { path, detail: "summary" }, + }, + { toolDefinition: definition }, + ); expect(reply.isError).not.toBe(true); - expect( - ajv.validate(outputSchema, reply.structuredContent), - JSON.stringify(ajv.errors), - ).toBe(true); const receipt = parseEvidence(reply.structuredContent); const view = managedArtifactSummaryViewResultSchema.parse( receipt.normalized_result, @@ -140,7 +139,7 @@ it("returns a first-call overview and pages the unchanged source after its file }); if (!portable.ok) throw portable.error; expect(portable.value.normalized_result).toEqual(view); -}, 30000); +}); it("keeps malformed metadata, empty returned rows and historical null subjects explicit", async () => { const directory = await createTestTempDirectory( @@ -216,7 +215,7 @@ it("keeps malformed metadata, empty returned rows and historical null subjects e ok: false, error: { _tag: "EvidenceIntegrityError" }, }); -}, 30000); +}); it("refuses selected delivery before file access when the server cannot retain Evidence", async () => { const directory = await createTestTempDirectory("rea-managed-fixed-view-"); @@ -259,4 +258,4 @@ it("refuses selected delivery before file access when the server cannot retain E complete.structuredContent, ).success, ).toBe(true); -}, 30000); +}); diff --git a/tests/boundary/mcp/managedFieldConstantsMcp.test.ts b/tests/boundary/mcp/managedFieldConstantsMcp.test.ts index 82bad9b386..2827e9cba4 100644 --- a/tests/boundary/mcp/managedFieldConstantsMcp.test.ts +++ b/tests/boundary/mcp/managedFieldConstantsMcp.test.ts @@ -38,17 +38,11 @@ it("retains lossless field constants in MCP Evidence and pages them after the so ); if (definition?.outputSchema === undefined) throw new Error("Member output schema missing"); - const outputSchema: Record = definition.outputSchema; const reply = await client.callTool( { name: definition.name, arguments: { path, detail: "complete" } }, { toolDefinition: definition }, ); expect(reply.isError).not.toBe(true); - const ajv = new Ajv2020({ strict: false, validateFormats: false }); - expect( - ajv.validate(outputSchema, reply.structuredContent), - JSON.stringify(ajv.errors), - ).toBe(true); const parent = parseEvidence(reply.structuredContent); const { fields } = managedMemberInspectionSchema.parse( parent.normalized_result, @@ -68,7 +62,7 @@ it("retains lossless field constants in MCP Evidence and pages them after the so const viewTool = tools.find((tool) => tool.name === "inspect_analysis_view"); if (viewTool?.outputSchema === undefined) throw new Error("View output schema missing"); - const viewOutputSchema: Record = viewTool.outputSchema; + const ajv = new Ajv2020({ strict: false, validateFormats: false }); const request = { source: { kind: "retained-evidence", evidence_id: parent.evidence_id }, view: { @@ -88,10 +82,6 @@ it("retains lossless field constants in MCP Evidence and pages them after the so { toolDefinition: viewTool }, ); expect(page.isError).not.toBe(true); - expect( - ajv.validate(viewOutputSchema, page.structuredContent), - JSON.stringify(ajv.errors), - ).toBe(true); const view = parseEvidence(page.structuredContent).normalized_result; expect(view).toMatchObject({ parent_evidence_id: parent.evidence_id, diff --git a/tests/boundary/mcp/managedMcp.test.ts b/tests/boundary/mcp/managedMcp.test.ts index ba370ca045..fe66104342 100644 --- a/tests/boundary/mcp/managedMcp.test.ts +++ b/tests/boundary/mcp/managedMcp.test.ts @@ -193,7 +193,7 @@ it("runs every managed static inspection independently of an active native targe }, }); await assertFieldMemberRefToken(client, fieldTokenPath); -}, 30_000); +}); it("compares observed call-target names through the managed MCP contract", async () => { const directory = await createTestTempDirectory("rea-managed-call-target-"); @@ -308,7 +308,7 @@ it("opens a managed PE and executes the managed static provider through MCP", as rightPath, assertOutput, ); -}, 30_000); +}); const verifyManagedCatalogAndNativeWorkflow = async ( client: Client, diff --git a/tests/boundary/mcp/managedMemberSummaryMcp.test.ts b/tests/boundary/mcp/managedMemberSummaryMcp.test.ts index c261085e4d..43092a482f 100644 --- a/tests/boundary/mcp/managedMemberSummaryMcp.test.ts +++ b/tests/boundary/mcp/managedMemberSummaryMcp.test.ts @@ -211,15 +211,14 @@ it("summarizes selected members and pages every original collection after the fi detail: "summary", }), ).toBe(true); - const reply = await client.callTool({ - name: "inspect_managed_members", - arguments: { path, type_names, detail: "summary" }, - }); + const reply = await client.callTool( + { + name: "inspect_managed_members", + arguments: { path, type_names, detail: "summary" }, + }, + { toolDefinition: definition }, + ); expect(reply.isError).not.toBe(true); - expect( - ajv.validate(outputSchema, reply.structuredContent), - JSON.stringify(ajv.errors), - ).toBe(true); const receipt = parseEvidence(reply.structuredContent); const view = managedMemberSummaryViewResultSchema.parse( receipt.normalized_result, @@ -305,7 +304,7 @@ it("summarizes selected members and pages every original collection after the fi }); if (!portable.ok) throw portable.error; expect(portable.value.normalized_result).toEqual(view); -}, 30000); +}); it("keeps collections that historical member Evidence never examined unknown", async () => { const directory = await createTestTempDirectory("rea-member-historical-"); @@ -361,7 +360,7 @@ it("keeps collections that historical member Evidence never examined unknown", a view: { kind: "managed", collection: "attributes", offset: 0, limit: 1 }, }), ).toMatchObject({ ok: false, error: { _tag: "AnalysisInputError" } }); -}, 30000); +}); it("binds managed member views to their recorded token scope", async () => { const original = MANAGED_RECONSTRUCTION_IMPORT_EXAMPLE.static_members; diff --git a/tests/boundary/mcp/managedNativeVerificationAdapters.test.ts b/tests/boundary/mcp/managedNativeVerificationAdapters.test.ts index 2ed4fbc581..67affdb330 100644 --- a/tests/boundary/mcp/managedNativeVerificationAdapters.test.ts +++ b/tests/boundary/mcp/managedNativeVerificationAdapters.test.ts @@ -1,8 +1,6 @@ -import { Client } from "@modelcontextprotocol/client"; -import { StdioClientTransport } from "@modelcontextprotocol/client/stdio"; import { writeFile } from "node:fs/promises"; -import { join, resolve } from "node:path"; -import { afterAll, beforeAll, expect } from "vitest"; +import { join } from "node:path"; +import { expect } from "vitest"; import { createEvidence, parseEvidence } from "../../../src/domain/evidence.js"; import { functionDossierSchema } from "../../../src/domain/native/functionDossier.js"; @@ -14,25 +12,12 @@ import { managedMemberInspectionSchema } from "../../../src/domain/managed/manag import { managedApplicationGraphReferenceInputSchema } from "../../../src/domain/managed/managedApplicationGraph.js"; import { managedNativeVerificationResultSchema } from "../../../src/domain/managed/managedNativeVerificationSchemas.js"; import { buildManagedPeFixture } from "../../../src/dotnet/ManagedPe.fixture.js"; +import { fileLocalToolsMcp } from "../../fixtures/localToolsMcp.js"; import { parseMcpToolError } from "../../fixtures/mcpToolError.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; -import { cliTest, runFileCli } from "../../support/cli/cliFixture.js"; +import { cliTest } from "../../support/cli/cliFixture.js"; -const client = new Client({ name: "managed-native-adapters", version: "1" }); -const transport = new StdioClientTransport({ - command: process.execPath, - args: [resolve("scripts/rea.mjs"), "mcp"], - cwd: process.cwd(), - env: { PATH: process.env.PATH ?? "" }, - stderr: "pipe", -}); -beforeAll(async () => { - await client.connect(transport); - transport.stderr?.on("data", () => undefined); -}); -afterAll(async () => { - await Promise.all([client.close(), transport.close()]); -}); +const fileMcp = fileLocalToolsMcp(); cliTest( "preserves managed graph identity failures and wrong-role controls through CLI and stdio MCP", @@ -74,7 +59,7 @@ cliTest( const input = managedApplicationGraphReferenceInputSchema.parse({ managed_members: parseEvidence(JSON.parse(JSON.stringify(source))), }); - const response = await client.callTool({ + const response = await fileMcp.client.callTool({ name: "project_managed_application_graph", arguments: input, }); @@ -110,7 +95,7 @@ cliTest( const wrongRole = MANAGED_NATIVE_VERIFICATION_EXAMPLE.managed_boundaries; const wrongInput = { managed_members: wrongRole }; - const wrongResponse = await client.callTool({ + const wrongResponse = await fileMcp.client.callTool({ name: "project_managed_application_graph", arguments: wrongInput, }); @@ -142,7 +127,7 @@ cliTest( expect(wrongCommand.exitCode, wrongCommand.stderr).toBe(1); expect(wrongCommand.json).toMatchObject(wrongError); - const control = await client.callTool({ + const control = await fileMcp.client.callTool({ name: "project_managed_application_graph", arguments: MANAGED_APPLICATION_GRAPH_EXAMPLE, }); @@ -161,7 +146,6 @@ cliTest( ); expect(evidence.evidence_links).toEqual([original.evidence_id]); }, - 60_000, ); /** Authentic native Evidence whose operation the workflow cannot read symbols from. */ @@ -300,7 +284,7 @@ cliTest( }, }), ); - const inspected = await client.callTool({ + const inspected = await fileMcp.client.callTool({ name: "inspect_managed_native_boundaries", arguments: { path: managedPath }, }); @@ -333,7 +317,7 @@ cliTest( managed_boundaries: managed, native_observations: [native], }; - const mcp = await client.callTool({ + const mcp = await fileMcp.client.callTool({ name: "verify_managed_native_boundaries", arguments: input, }); @@ -346,10 +330,10 @@ cliTest( const invocation = { arguments: ["verify-managed-native-boundaries", request, "--json"], }; - const cliRun = - index === 0 - ? await cli.run(invocation) - : await runFileCli(cli, invocation); + const cliRun = await cli.run({ + ...invocation, + freshProcess: index === 0, + }); expect(cliRun.exitCode, cliRun.stderr).toBe(0); const cliResult = managedNativeVerificationResultSchema.parse( parseEvidence(cliRun.json).normalized_result, @@ -372,7 +356,6 @@ cliTest( ]); } }, - 60_000, ); cliTest( @@ -381,7 +364,7 @@ cliTest( const root = await createTestTempDirectory("rea-managed-native-adapters-"); const path = join(root, "fixture.dll"); await writeFile(path, buildManagedPeFixture({ pinvoke: {} })); - const inspected = await client.callTool({ + const inspected = await fileMcp.client.callTool({ name: "inspect_managed_native_boundaries", arguments: { path }, }); @@ -393,7 +376,7 @@ cliTest( native_observations: [native], }; - const mcp = await client.callTool({ + const mcp = await fileMcp.client.callTool({ name: "verify_managed_native_boundaries", arguments: input, }); @@ -431,7 +414,7 @@ cliTest( "Some native Evidence operations were unsupported by this workflow.", ); - const tampered = await client.callTool({ + const tampered = await fileMcp.client.callTool({ name: "verify_managed_native_boundaries", arguments: { ...input, @@ -442,5 +425,4 @@ cliTest( error: { code: "evidence_integrity_mismatch" }, }); }, - 60_000, ); diff --git a/tests/boundary/mcp/mcpDoctor.test.ts b/tests/boundary/mcp/mcpDoctor.test.ts index 4d5b9140d3..0171a2b298 100644 --- a/tests/boundary/mcp/mcpDoctor.test.ts +++ b/tests/boundary/mcp/mcpDoctor.test.ts @@ -10,7 +10,7 @@ import { mkdir, readFile, writeFile } from "node:fs/promises"; import { join, resolve } from "node:path"; import { promisify } from "node:util"; -import { afterEach, describe, expect, it, vi } from "vitest"; +import { afterAll, afterEach, describe, expect, it, vi } from "vitest"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; import { createTestBinarySession } from "../../fixtures/binarySession.js"; @@ -39,6 +39,11 @@ import { mcpDoctorDeadlineRecovery, MCP_STARTUP_POLICY, } from "../../../src/mcpStartupPolicy.js"; +import { createMcpSchemaValidation } from "../../fixtures/mcpSchemaValidation.js"; + +// Compile SDK output validators for the tools a test calls, not the whole catalog. +const schemaValidation = createMcpSchemaValidation(); +afterAll(() => schemaValidation.clear()); const expectDoctorDeadline = ( detail: string | undefined, @@ -264,7 +269,7 @@ describe("production MCP doctor", () => { ], { timeout: 30_000 }, ); - }, 35_000); + }); it("preserves the full-output envelope for startup failures", async () => { const result = await runProductionMcpDoctorCli( @@ -332,7 +337,6 @@ describe("production MCP doctor", () => { if (cliParity) await assertCliCatalogIdentity(selectedEnvironment, identity); }, - 30_000, ); }); @@ -384,32 +388,51 @@ describe("production MCP doctor family scopes", () => { await assertStatusCatalogIdentity(environment, identity); if (cliParity) await assertCliCatalogIdentity(environment, identity); }, - 30_000, ); - it("discovers an empty prompt inventory and explains a scope without session", async () => { - const result = await runProductionMcpDoctor({ - command: process.execPath, - args: [resolve("scripts/rea.mjs"), "mcp"], - cwd: process.cwd(), - environment: { ...process.env, REA_MCP_TOOL_FAMILIES: "browser" }, - }); - expect(result).toMatchObject({ - healthy: false, - adapter: "production-stdio", - inventory: { - prompts: { expected: 0, observed: 0, missing: [], unexpected: [] }, - }, - request_flow: { tool: "binary_session", ok: false }, - }); - expect(result.checks).toContainEqual({ - name: "request-flow", - ok: false, - detail: expect.stringMatching( - /binary_session.*outside.*selected tool families.*include.*session family.*request.flow.*identity/iu, - ), - }); - }); + it.each(["browser", "native"])( + "explains unobserved identity for a %s scope without session", + async (families) => { + const result = await runProductionMcpDoctor({ + command: process.execPath, + args: [resolve("scripts/rea.mjs"), "mcp"], + cwd: process.cwd(), + environment: { ...process.env, REA_MCP_TOOL_FAMILIES: families }, + }); + expect(result).toMatchObject({ + healthy: false, + adapter: "production-stdio", + inventory: { + prompts: { expected: 0, observed: 0, missing: [], unexpected: [] }, + }, + request_flow: { tool: "binary_session", ok: false }, + identity: { + observed_package_version: null, + observed_catalog_digest: null, + }, + }); + const scopeLimitation = + /binary_session.*outside.*selected tool families.*include.*session family.*request.flow.*identity/iu; + expect(result.checks).toContainEqual({ + name: "server-identity", + ok: false, + detail: expect.stringMatching( + new RegExp(`^identity not observed: ${scopeLimitation.source}`, "iu"), + ), + }); + expect(result.checks).not.toContainEqual( + expect.objectContaining({ + name: "server-identity", + detail: expect.stringContaining("missing"), + }), + ); + expect(result.checks).toContainEqual({ + name: "request-flow", + ok: false, + detail: expect.stringMatching(scopeLimitation), + }); + }, + ); it("fails startup instead of misreporting inventory for an invalid scope", async () => { const result = await runProductionMcpDoctor({ @@ -428,7 +451,7 @@ describe("production MCP doctor family scopes", () => { expect(result.diagnostics?.stderr).toEqual( expect.stringContaining("REA_MCP_TOOL_FAMILIES"), ); - }, 30_000); + }); }); describe("production MCP doctor output", () => { @@ -494,7 +517,7 @@ describe("production MCP doctor output", () => { }, }, }); - }, 35_000); + }); it.each([false, true])( "frames unhealthy JSONL diagnostics and retains the selected envelope (%s)", @@ -576,7 +599,10 @@ describe("production MCP doctor probe validation", () => { const root = await createTestTempDirectory("rea-mcp-doctor-reply-"); const snapshotPath = join(root, "reply.json"); const environment = { PATH: process.env.PATH ?? "" }; - const client = new Client({ name: "doctor-reply-capture", version: "1" }); + const client = new Client( + { name: "doctor-reply-capture", version: "1" }, + { jsonSchemaValidator: schemaValidation }, + ); const transport = new StdioClientTransport({ command: process.execPath, args: [resolve("scripts/rea.mjs"), "mcp"], @@ -641,6 +667,14 @@ describe("production MCP doctor probe validation", () => { ok: true, detail: expect.any(String), }); + expect(result.checks).toContainEqual({ + name: "server-identity", + ok: mode !== "invalid", + detail: + mode === "invalid" + ? expect.stringMatching(/^identity not observed: .*output schema/iu) + : expect.stringMatching(/^package=.+; catalog=.+$/u), + }); if (mode === "invalid") expect(result.checks).toContainEqual({ name: "request-flow", @@ -662,7 +696,7 @@ describe("production MCP doctor probe validation", () => { }); } } - }, 30_000); + }); }); describe("production MCP doctor child lifecycle", () => { @@ -698,7 +732,7 @@ describe("production MCP doctor child lifecycle", () => { stderr_bytes: 4 * 1024 * 1024, stderr_truncated: true, }); - }, 10_000); + }); it("does not manufacture a replacement character when the retained prefix ends inside UTF-8", async () => { const prefixBytes = MCP_STARTUP_POLICY.doctorStderrBytes - 1; @@ -771,7 +805,78 @@ describe("production MCP doctor child lifecycle", () => { expectDoctorDeadline(result.checks[0]?.detail, 60_000); expect(result.checks[0]?.detail).not.toContain("REA_MCP_STARTUP"); await expect(waitForExit(pid)).resolves.toBeUndefined(); - }, 10_000); + }); +}); + +describe("production MCP doctor child ownership", () => { + it("does not hand an ambient target variable to the diagnostic child", async () => { + const root = await createTestTempDirectory("rea-mcp-doctor-target-free-"); + const seenPath = join(root, "env.json"); + const result = await runProductionMcpDoctor({ + command: process.execPath, + args: [resolve("tests/fixtures/mcpDoctorEnvironment.mjs")], + cwd: root, + environment: { + ...process.env, + HOPPER_TARGET_PATH: "/tmp/ambient-target", + HOPPER_TARGET_KIND: "hop", + REA_MCP_DOCTOR_SEEN_PATH: seenPath, + REA_MCP_DOCTOR_DEADLINE_MS: "5000", + }, + }); + expect(result.healthy).toBe(false); + const seen = JSON.parse(await readFile(seenPath, "utf8")) as Record< + string, + unknown + >; + expect(seen).not.toHaveProperty("HOPPER_TARGET_PATH"); + expect(seen).not.toHaveProperty("HOPPER_TARGET_KIND"); + // The rest of the caller's environment still reaches the child. + expect(seen.REA_MCP_DOCTOR_SEEN_PATH).toBe(seenPath); + }); + + it.each([ + ["in the launcher's process group", "0"], + ["in its own session", "1"], + ])( + "stops a descendant %s that outlives the launcher", + async (_case, detached) => { + const root = await createTestTempDirectory("rea-mcp-doctor-descendant-"); + const pidPath = join(root, "pids.json"); + vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] }); + const pending = runProductionMcpDoctor({ + command: process.execPath, + args: [resolve("tests/fixtures/mcpDoctorForkDescendant.mjs")], + cwd: process.cwd(), + environment: { + ...process.env, + REA_MCP_DOCTOR_PID_PATH: pidPath, + REA_MCP_DOCTOR_DESCENDANT_DETACHED: detached, + REA_MCP_DOCTOR_DEADLINE_MS: "60000", + }, + }); + const pids = await vi.waitFor( + async () => { + const value = JSON.parse(await readFile(pidPath, "utf8")) as { + launcher: number; + descendant: number; + }; + expect(Number.isSafeInteger(value.descendant)).toBe(true); + return value; + }, + { timeout: 10_000 }, + ); + await vi.advanceTimersByTimeAsync(60_000); + const result = await vi.waitFor(() => pending, { timeout: 10_000 }); + vi.useRealTimers(); + // The launcher exits at once, so only owned cleanup can stop the descendant. + // The SDK transport signaled only its direct child and reported success. + expect(result.healthy).toBe(false); + expect(result.checks.map(({ name }) => name)).toEqual(["transport"]); + expectDoctorDeadline(result.checks[0]?.detail, 60_000); + await expect(waitForExit(pids.descendant)).resolves.toBeUndefined(); + }, + ); }); describe("production MCP doctor request-flow deadline", () => { @@ -806,7 +911,7 @@ describe("production MCP doctor request-flow deadline", () => { expect(requestFlow?.ok).toBe(false); expectDoctorDeadline(requestFlow?.detail, 60_000); expect(requestFlow?.detail).not.toContain("REA_MCP_STARTUP"); - }, 15_000); + }); it.each([ "unrelated fixture method refusal", @@ -835,7 +940,6 @@ describe("production MCP doctor request-flow deadline", () => { expect(requestFlow?.detail).toContain(message); expect(requestFlow?.detail).not.toContain("REA_MCP_DOCTOR_DEADLINE_MS"); }, - 10_000, ); it("refuses an invalid phase budget before starting a child", async () => { diff --git a/tests/boundary/mcp/mcpFixtureLifecycle.test.ts b/tests/boundary/mcp/mcpFixtureLifecycle.test.ts index eeaa2d8815..8b9a804262 100644 --- a/tests/boundary/mcp/mcpFixtureLifecycle.test.ts +++ b/tests/boundary/mcp/mcpFixtureLifecycle.test.ts @@ -3,6 +3,7 @@ import { McpServer } from "@modelcontextprotocol/server"; import { expect, it, vi } from "vitest"; import { createDeferred } from "../../fixtures/binarySession.js"; +import { awaitAcknowledgement } from "../../fixtures/lifecycleAcknowledgement.js"; import { createTestMcp } from "../../support/mcp/mcpFixture.js"; /** Keep real SDK objects recoverable even when a red ownership assertion fails. */ @@ -110,7 +111,11 @@ it("registers acquisition before a producer can reenter shutdown and attach its (reason: unknown) => ({ ok: false as const, reason }), ); try { - await entered.promise; + await awaitAcknowledgement( + entered.promise, + opening, + "MCP lifecycle boundary", + ); await Promise.resolve(); await Promise.resolve(); expect(closeSettled).toBe(false); @@ -154,7 +159,11 @@ it("shares one concurrent close attempt and does not reclose released SDK object await close(); }); first = fixture.mcp.close(); - await entered.promise; + await awaitAcknowledgement( + entered.promise, + first, + "MCP lifecycle boundary", + ); second = fixture.mcp.close(); await Promise.resolve(); await Promise.resolve(); @@ -195,7 +204,11 @@ it("cancels a real initialize handshake before awaiting its held server handler" ); const serverClose = vi.spyOn(server, "close"); try { - await entered.promise; + await awaitAcknowledgement( + entered.promise, + opening, + "MCP lifecycle boundary", + ); await fixture.mcp.close(); expect(openingSettled).toBe(true); expect(await opening).toMatchObject({ ok: false }); diff --git a/tests/boundary/mcp/mcpSchemaValidation.test.ts b/tests/boundary/mcp/mcpSchemaValidation.test.ts index e82a633d77..02935a6cf1 100644 --- a/tests/boundary/mcp/mcpSchemaValidation.test.ts +++ b/tests/boundary/mcp/mcpSchemaValidation.test.ts @@ -1,6 +1,28 @@ -import { expect, it } from "vitest"; +import { AjvJsonSchemaValidator } from "@modelcontextprotocol/client/validators/ajv"; +import { expect, it, vi } from "vitest"; import { createMcpSchemaValidation } from "../../fixtures/mcpSchemaValidation.js"; +it("compiles a listed tool's validator only when it first validates", () => { + // The SDK requests a validator for every listed tool before its first call. + const compile = vi.spyOn(AjvJsonSchemaValidator.prototype, "getValidator"); + const validation = createMcpSchemaValidation(); + try { + const listed = [ + { type: "string" }, + { type: "integer" }, + { type: "boolean" }, + ]; + const validators = listed.map((schema) => validation.getValidator(schema)); + expect(compile).not.toHaveBeenCalled(); + expect(validators[1]?.(7).valid).toBe(true); + expect(validators[1]?.("seven").valid).toBe(false); + expect(compile).toHaveBeenCalledTimes(1); + } finally { + compile.mockRestore(); + validation.clear(); + } +}); + it("reuses equal schemas and recompiles changed schemas sharing an id", () => { const validation = createMcpSchemaValidation(); try { diff --git a/tests/boundary/mcp/nativeDispatchBoundTarget.test.ts b/tests/boundary/mcp/nativeDispatchBoundTarget.test.ts index 519240269c..163de3508b 100644 --- a/tests/boundary/mcp/nativeDispatchBoundTarget.test.ts +++ b/tests/boundary/mcp/nativeDispatchBoundTarget.test.ts @@ -123,14 +123,13 @@ describe.skipIf(process.platform !== "darwin")( () => { it("reports missing, directory and symlink-loop replacements as artifact changes and recovers in the same session", async () => { await inspectReplacements(["missing", "directory", "loop"]); - }, 30_000); + }); it.skipIf(process.getuid?.() === 0)( "keeps permission denial distinct and recovers after restoring read access", async () => { await inspectReplacements(["unreadable"]); }, - 30_000, ); }, ); diff --git a/tests/boundary/mcp/nativeInstructionReferencesView.test.ts b/tests/boundary/mcp/nativeInstructionReferencesView.test.ts index 8416916a88..aa5858e5e7 100644 --- a/tests/boundary/mcp/nativeInstructionReferencesView.test.ts +++ b/tests/boundary/mcp/nativeInstructionReferencesView.test.ts @@ -69,7 +69,9 @@ const instruction = ( const checkContinuation = async ( client: Client, - schema: Readonly>, + toolDefinition: NonNullable< + NonNullable[1]>["toolDefinition"] + >, continuation: { name: "inspect_analysis_view"; arguments: z.output; @@ -99,15 +101,11 @@ const checkContinuation = async ( const recovered: unknown[] = []; let request = continuation.arguments; for (;;) { - const response = await client.callTool({ - name: continuation.name, - arguments: request, - }); + const response = await client.callTool( + { name: continuation.name, arguments: request }, + { toolDefinition }, + ); expect(response.isError, JSON.stringify(response.content)).not.toBe(true); - expect( - validation.assertions.validate(schema, response.structuredContent), - JSON.stringify(validation.assertions.errors), - ).toBe(true); const page = analysisViewResultSchema.parse( parseEvidence(response.structuredContent).normalized_result, ); @@ -160,10 +158,10 @@ it.each(["full", "compact"] as const)( }, }, ); - const client = new Client({ - name: "instruction-reference-pages", - version: "1", - }); + const client = new Client( + { name: "instruction-reference-pages", version: "1" }, + { jsonSchemaValidator: validation }, + ); const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair(); try { @@ -218,12 +216,7 @@ it.each(["full", "compact"] as const)( throw new Error("Expected retained parent in continuation"); source = continuation.arguments.source; const before = reads; - await checkContinuation( - client, - advertised.outputSchema, - continuation, - source, - ); + await checkContinuation(client, advertised, continuation, source); expect(reads).toBe(before); } else { expect(result.isError).not.toBe(true); @@ -233,26 +226,22 @@ it.each(["full", "compact"] as const)( }; } const before = reads; - const reply = await client.callTool({ - name: "inspect_analysis_view", - arguments: { - source, - view: { - kind: "native-instruction-references", - collection: "incoming_references", - offset: incoming === "rows" ? 1 : 0, - limit: 1, + const reply = await client.callTool( + { + name: "inspect_analysis_view", + arguments: { + source, + view: { + kind: "native-instruction-references", + collection: "incoming_references", + offset: incoming === "rows" ? 1 : 0, + limit: 1, + }, }, }, - }); + { toolDefinition: advertised }, + ); expect(reply.isError, JSON.stringify(reply.content)).not.toBe(true); - expect( - validation.assertions.validate( - advertised.outputSchema, - reply.structuredContent, - ), - JSON.stringify(validation.assertions.errors), - ).toBe(true); const page = analysisViewResultSchema.parse( parseEvidence(reply.structuredContent).normalized_result, ); @@ -286,7 +275,6 @@ it.each(["full", "compact"] as const)( if (!closed.ok) throw closed.error; } }, - 90_000, ); it("recovers the complete parent when a single reference exceeds the page budget", async () => { @@ -374,4 +362,4 @@ it("recovers the complete parent when a single reference exceeds the page budget const closed = await session.close(); if (!closed.ok) throw closed.error; } -}, 90_000); +}); diff --git a/tests/boundary/mcp/nativeInventoryViewsMcp.test.ts b/tests/boundary/mcp/nativeInventoryViewsMcp.test.ts index 8a89c274e3..70e9027a11 100644 --- a/tests/boundary/mcp/nativeInventoryViewsMcp.test.ts +++ b/tests/boundary/mcp/nativeInventoryViewsMcp.test.ts @@ -116,15 +116,11 @@ it("shares complete/summary source identity and advertised contracts across all const parameters = operation.startsWith("search_") ? { pattern: ".", mode: "regex" } : {}; - const completeReply = await harness.mcp.callTool({ - name: operation, - arguments: parameters, - }); + const completeReply = await harness.mcp.callTool( + { name: operation, arguments: parameters }, + { toolDefinition: advertised }, + ); if (completeReply.isError) throw new Error(JSON.stringify(completeReply)); - expect( - ajv.validate(outputSchema, completeReply.structuredContent), - JSON.stringify(ajv.errors), - ).toBe(true); const complete = parseEvidence( contract.outputSchema.parse(completeReply.structuredContent), ); @@ -136,15 +132,11 @@ it("shares complete/summary source identity and advertised contracts across all if (operation === "search_strings") expect(complete.normalized_result).toEqual(stringRows); expect(complete.parameters).not.toHaveProperty("detail"); - const summaryReply = await harness.mcp.callTool({ - name: operation, - arguments: { ...parameters, detail: "summary" }, - }); + const summaryReply = await harness.mcp.callTool( + { name: operation, arguments: { ...parameters, detail: "summary" } }, + { toolDefinition: advertised }, + ); if (summaryReply.isError) throw new Error(JSON.stringify(summaryReply)); - expect( - ajv.validate(outputSchema, summaryReply.structuredContent), - JSON.stringify(ajv.errors), - ).toBe(true); const summary = parseEvidence( contract.outputSchema.parse(summaryReply.structuredContent), ); @@ -175,21 +167,20 @@ it("shares complete/summary source identity and advertised contracts across all offset < (complete.normalized_result as unknown[]).length; offset++ ) { - const pageReply = await harness.mcp.callTool({ - name: "inspect_analysis_view", - arguments: { - source: { - kind: "retained-evidence", - evidence_id: complete.evidence_id, + const pageReply = await harness.mcp.callTool( + { + name: "inspect_analysis_view", + arguments: { + source: { + kind: "retained-evidence", + evidence_id: complete.evidence_id, + }, + view: { kind: "native-inventory", offset, limit: 1 }, }, - view: { kind: "native-inventory", offset, limit: 1 }, }, - }); + { toolDefinition: viewTool }, + ); if (pageReply.isError) throw new Error(JSON.stringify(pageReply)); - expect( - ajv.validate(viewOutputSchema, pageReply.structuredContent), - JSON.stringify(ajv.errors), - ).toBe(true); const page = toolContract("inspect_analysis_view").outputSchema.parse( pageReply.structuredContent, ).normalized_result; diff --git a/tests/boundary/mcp/nativeLoadImageViewsMcp.test.ts b/tests/boundary/mcp/nativeLoadImageViewsMcp.test.ts index 9f2b733643..d52a2cb33e 100644 --- a/tests/boundary/mcp/nativeLoadImageViewsMcp.test.ts +++ b/tests/boundary/mcp/nativeLoadImageViewsMcp.test.ts @@ -99,7 +99,7 @@ it("preserves raw and DOL verification facts and selected DOL sections through t limitations: expect.arrayContaining(report.limitations), }); } -}, 30_000); +}); it("delivers load-image summaries from the complete retained public-tool observation", async () => { const report = nativeRawLoadImageFixture(); diff --git a/tests/boundary/mcp/nativeMachoSummaryMcp.test.ts b/tests/boundary/mcp/nativeMachoSummaryMcp.test.ts index d9a2612e15..cb50b6bae4 100644 --- a/tests/boundary/mcp/nativeMachoSummaryMcp.test.ts +++ b/tests/boundary/mcp/nativeMachoSummaryMcp.test.ts @@ -201,15 +201,11 @@ mcpTest( view.ajv.validate(view.inputSchema, request), JSON.stringify(view.ajv.errors), ).toBe(true); - const reply = await client.callTool({ - name: "inspect_analysis_view", - arguments: request, - }); + const reply = await client.callTool( + { name: "inspect_analysis_view", arguments: request }, + { toolDefinition: view.toolDefinition }, + ); expect(reply.isError, JSON.stringify(reply.content)).not.toBe(true); - expect( - view.ajv.validate(view.outputSchema, reply.structuredContent), - JSON.stringify(view.ajv.errors), - ).toBe(true); const result = parseEvidence(reply.structuredContent).normalized_result; expect(result).toMatchObject({ item: [expected], @@ -254,18 +250,17 @@ mcpTest( tool.ajv.validate(tool.outputSchema, historical), JSON.stringify(tool.ajv.errors), ).toBe(true); - const oldView = await client.callTool({ - name: "inspect_analysis_view", - arguments: { - source: { kind: "inline", evidence: historical }, - view: { kind: "macho", facet: "segments" }, + const oldView = await client.callTool( + { + name: "inspect_analysis_view", + arguments: { + source: { kind: "inline", evidence: historical }, + view: { kind: "macho", facet: "segments" }, + }, }, - }); + { toolDefinition: view.toolDefinition }, + ); expect(oldView.isError, JSON.stringify(oldView.content)).not.toBe(true); - expect( - view.ajv.validate(view.outputSchema, oldView.structuredContent), - JSON.stringify(view.ajv.errors), - ).toBe(true); expect( parseEvidence(oldView.structuredContent).normalized_result, ).toMatchObject({ item: [historicalSegment], source_exhaustive: true }); @@ -358,15 +353,11 @@ mcpTest( schemas.ajv.validate(schemas.inputSchema, request), JSON.stringify(schemas.ajv.errors), ).toBe(true); - const reply = await client.callTool({ - name: "inspect_analysis_view", - arguments: request, - }); + const reply = await client.callTool( + { name: "inspect_analysis_view", arguments: request }, + { toolDefinition: schemas.toolDefinition }, + ); expect(reply.isError, JSON.stringify(reply.content)).not.toBe(true); - expect( - schemas.ajv.validate(schemas.outputSchema, reply.structuredContent), - JSON.stringify(schemas.ajv.errors), - ).toBe(true); expect( pageSchema.parse( parseEvidence(reply.structuredContent).normalized_result, diff --git a/tests/boundary/mcp/nativeObservationViewsMcp.test.ts b/tests/boundary/mcp/nativeObservationViewsMcp.test.ts index 745a9b4cad..9d83777bc8 100644 --- a/tests/boundary/mcp/nativeObservationViewsMcp.test.ts +++ b/tests/boundary/mcp/nativeObservationViewsMcp.test.ts @@ -12,7 +12,6 @@ import { analysisViewResultSchema, inspectAnalysisViewInputSchema, } from "../../../src/domain/analysisView/analysisView.js"; -import { XREFS_ADDRESS_PAGE_LIMITATION } from "../../../src/domain/analysisView/nativeObservationView.js"; import { NATIVE_PRIMITIVE_OBSERVATION_OPERATIONS } from "../../../src/domain/native/nativeObservation.js"; import { connectGhidraMcp } from "./ghidraMcpHarness.js"; import { createMcpSchemaValidation } from "../../fixtures/mcpSchemaValidation.js"; @@ -35,13 +34,6 @@ it("delivers native primitive and graph summaries with exact retained parents an { toolDefinition: tool }, ); expect(result.isError, JSON.stringify(result.content)).not.toBe(true); - expect( - validation.assertions.validate( - tool.outputSchema, - result.structuredContent, - ), - JSON.stringify(validation.assertions.errors), - ).toBe(true); return parseEvidence(result.structuredContent); }; const viewSchemas = await advertisedMcpToolSchemas( @@ -141,7 +133,13 @@ it("delivers native primitive and graph summaries with exact retained parents an }); expect( analysisViewResultSchema.parse(page.normalized_result).unknowns, - ).toContain(XREFS_ADDRESS_PAGE_LIMITATION); + ).toEqual( + expect.arrayContaining([ + expect.stringMatching( + /source addresses only.*reference kinds or provenance.*complete parent Evidence/iu, + ), + ]), + ); const caveats = await call("inspect_analysis_view", { source, view: { kind: "native-context", facet: "limitations" }, @@ -153,7 +151,7 @@ it("delivers native primitive and graph summaries with exact retained parents an } finally { await harness.close(); } -}, 90_000); +}); it("follows native overflow continuations through MCP without repeating provider work", async () => { let reads = 0; @@ -269,4 +267,4 @@ it("follows native overflow continuations through MCP without repeating provider const closed = await session.close(); if (!closed.ok) throw closed.error; } -}, 90_000); +}); diff --git a/tests/boundary/mcp/nativeValueFlowAvailability.test.ts b/tests/boundary/mcp/nativeValueFlowAvailability.test.ts index 7333b3ece1..03f96047f1 100644 --- a/tests/boundary/mcp/nativeValueFlowAvailability.test.ts +++ b/tests/boundary/mcp/nativeValueFlowAvailability.test.ts @@ -121,10 +121,6 @@ it.each([ { toolDefinition: schema.toolDefinition }, ); expect(reply.isError, JSON.stringify(reply)).not.toBe(true); - expect( - schema.ajv.validate(schema.outputSchema, reply.structuredContent), - JSON.stringify(schema.ajv.errors), - ).toBe(true); return reply.structuredContent; }; await call("open_binary", { path: target, format: "dos-com" }); diff --git a/tests/boundary/mcp/nativeValueTraceCompleteness.test.ts b/tests/boundary/mcp/nativeValueTraceCompleteness.test.ts index 5bf1c7a2ab..8f869ad1f9 100644 --- a/tests/boundary/mcp/nativeValueTraceCompleteness.test.ts +++ b/tests/boundary/mcp/nativeValueTraceCompleteness.test.ts @@ -59,8 +59,10 @@ it.each(["full", "compact"] as const)( profile, valueTraceDiamond({ sessionSubject: true }).port, ); - const { toolDefinition, ajv, outputSchema } = - await advertisedMcpToolSchemas(client, "trace_native_values"); + const { toolDefinition, outputSchema } = await advertisedMcpToolSchemas( + client, + "trace_native_values", + ); if (profile === "full") for (const field of [ "page_truncated", @@ -75,10 +77,6 @@ it.each(["full", "compact"] as const)( { toolDefinition }, ); expect(reply.isError).not.toBe(true); - expect( - ajv.validate(outputSchema, reply.structuredContent), - JSON.stringify(ajv.errors), - ).toBe(true); return nativeValueTraceSchema.parse( parseEvidence(reply.structuredContent).normalized_result, ); @@ -168,8 +166,10 @@ it.each(["full", "compact"] as const)( profile, valueTraceUnboundArguments({ sessionSubject: true }).port, ); - const { toolDefinition, ajv, outputSchema } = - await advertisedMcpToolSchemas(client, "trace_native_values"); + const { toolDefinition } = await advertisedMcpToolSchemas( + client, + "trace_native_values", + ); const reply = await client.callTool( { name: "trace_native_values", @@ -178,10 +178,6 @@ it.each(["full", "compact"] as const)( { toolDefinition }, ); expect(reply.isError).not.toBe(true); - expect( - ajv.validate(outputSchema, reply.structuredContent), - JSON.stringify(ajv.errors), - ).toBe(true); const trace = nativeValueTraceSchema.parse( parseEvidence(reply.structuredContent).normalized_result, ); diff --git a/tests/boundary/mcp/nativeViewPredicateMcp.test.ts b/tests/boundary/mcp/nativeViewPredicateMcp.test.ts index d07ec6bd62..a2ced02fa8 100644 --- a/tests/boundary/mcp/nativeViewPredicateMcp.test.ts +++ b/tests/boundary/mcp/nativeViewPredicateMcp.test.ts @@ -156,13 +156,6 @@ it.each(["full", "compact"] as const)( expect(goodReply.isError, JSON.stringify(goodReply.content)).not.toBe( true, ); - expect( - validation.assertions.validate( - viewTool.outputSchema, - goodReply.structuredContent, - ), - JSON.stringify(validation.assertions.errors), - ).toBe(true); const goodView = parseEvidence(goodReply.structuredContent); expect(goodView.evidence_links).toContain(good.evidence_id); const raw = join(root, `${index}.json`), diff --git a/tests/boundary/mcp/navigationAlternativesMcp.test.ts b/tests/boundary/mcp/navigationAlternativesMcp.test.ts new file mode 100644 index 0000000000..8d21ffb5d1 --- /dev/null +++ b/tests/boundary/mcp/navigationAlternativesMcp.test.ts @@ -0,0 +1,246 @@ +import { describe, expect, it } from "vitest"; + +import { offeredOperationAlternatives } from "../../../src/application/OperationAlternatives.js"; +import { toolContract } from "../../../src/contracts/toolContracts.js"; +import { expandToolAvailability } from "../../../src/contracts/toolOutputSchemaPrimitives.js"; +import { parseEvidence } from "../../../src/domain/evidence.js"; +import { err, ok } from "../../../src/domain/result.js"; +import { GhidraSessionError } from "../../../src/ghidra/GhidraSessionError.js"; +import { silentLogger } from "../../../src/logger.js"; +import { createApplicationMcpHarness } from "../../fixtures/applicationMcpHarness.js"; +import { + createBinarySessionTargets, + createCacheProvider, + createTestBinarySession, +} from "../../fixtures/binarySession.js"; +import { parseMcpToolError } from "../../fixtures/mcpToolError.js"; +import { connectGhidraMcp, ghidraFixtureResult } from "./ghidraMcpHarness.js"; + +type Client = Awaited>["mcp"]; + +const NAVIGATION = ["next_address", "prev_address"] as const; +const GHIDRA_ALTERNATIVES = [ + "inspect_native_instruction", + "read_function_instructions", + "procedure_assembly", +]; +/** The owning formatter's remediation for exactly these offered tools. */ +const remediationFor = (operation: string, offered: readonly string[]) => + offeredOperationAlternatives(operation, (alternative) => + offered.includes(alternative), + )?.remediation; + +const navigationRefusal = async ( + client: Client, + name: (typeof NAVIGATION)[number], +) => + parseMcpToolError( + await client.callTool({ name, arguments: { address: "0x401000" } }), + ).error; + +const navigationAvailability = async ( + client: Client, + detail: "summary" | "complete", +) => { + const reply = await client.callTool({ + name: "binary_session", + arguments: { detail }, + }); + const status = toolContract("binary_session").outputSchema.parse( + reply.structuredContent, + ).result; + const groups = status.tool_availability.groups.filter((group) => + NAVIGATION.some((name) => group.names.includes(name)), + ); + return { + groups, + tools: expandToolAvailability(status.tool_availability), + }; +}; + +describe("address navigation refusals", () => { + it.each([ + // Explicitly unset so a developer's scoped catalog cannot leak in. + { + scope: "every family", + environment: { REA_MCP_TOOL_FAMILIES: undefined }, + }, + { + scope: "session and direct families", + environment: { REA_MCP_TOOL_FAMILIES: "session,direct" }, + }, + ])( + "name Ghidra's instruction observations and their scope ($scope)", + async ({ environment }) => { + const harness = await connectGhidraMcp( + "navigation-alternatives", + undefined, + { environment }, + ); + try { + for (const name of NAVIGATION) + expect(await navigationRefusal(harness.mcp, name)).toEqual({ + code: "capability_unavailable", + category: "unsupported_provider", + message: `The selected provider does not declare ${name}.`, + retryable: false, + remediation: { + action: remediationFor(name, GHIDRA_ALTERNATIVES), + }, + details: { + provider_id: "ghidra", + operation: name, + reason: "operation is not declared by this provider", + alternatives: GHIDRA_ALTERNATIVES, + }, + }); + + // Follow the suggested next step on the same connection. + const listing = parseEvidence( + ( + await harness.mcp.callTool({ + name: "read_function_instructions", + arguments: { procedure: "0x401000" }, + }) + ).structuredContent, + ); + expect(listing.provider.id).toBe("ghidra"); + expect(harness.calls).toContain("read_function_instructions"); + + // Discovery names the same relation once for both navigation tools. + for (const detail of ["summary", "complete"] as const) { + const { groups, tools } = await navigationAvailability( + harness.mcp, + detail, + ); + expect(groups).toEqual([ + expect.objectContaining({ + names: [...NAVIGATION], + available: false, + reason: "provider_missing", + remediation: + "Install or configure a provider that declares this operation.", + alternatives: GHIDRA_ALTERNATIVES, + }), + ]); + for (const alternative of GHIDRA_ALTERNATIVES) + expect(tools).toContainEqual( + expect.objectContaining({ name: alternative, available: true }), + ); + } + } finally { + await harness.close(); + } + }, + ); +}); + +describe("address navigation refusal alternatives", () => { + it("stop offering tools a provider failure made unavailable", async () => { + let failAssembly = true; + const harness = await connectGhidraMcp( + "navigation-after-provider-failure", + (operation, input) => { + if (operation === "procedure_assembly" && failAssembly) { + failAssembly = false; + return Promise.resolve( + err(new GhidraSessionError("protocol", "Malformed bridge reply")), + ); + } + return Promise.resolve(ok(ghidraFixtureResult(operation, input))); + }, + { environment: { REA_MCP_TOOL_FAMILIES: undefined } }, + ); + const offeredInDiscovery = async () => + (await navigationAvailability(harness.mcp, "summary")).tools + .filter(({ name }) => name === "next_address") + .map((tool) => + "alternatives" in tool ? tool.alternatives : undefined, + ); + try { + const failed = await harness.mcp.callTool({ + name: "procedure_assembly", + arguments: { procedure: "0x401000" }, + }); + expect(parseMcpToolError(failed).error.code).toBe("execution_failure"); + // The failure marks Ghidra's operations unavailable (assembly observed, + // the others inferred), so neither discovery nor the refusal offers one. + const generic = await navigationRefusal(harness.mcp, "next_address"); + expect(generic.remediation.action).toEqual( + expect.stringContaining("binary_session"), + ); + expect(generic.details).not.toHaveProperty("alternatives"); + expect(await offeredInDiscovery()).toEqual([undefined]); + + // A later Ghidra success clears the inferred entries, while the + // observed assembly failure stays until assembly itself succeeds. + const listing = await harness.mcp.callTool({ + name: "read_function_instructions", + arguments: { procedure: "0x401000" }, + }); + expect(listing.isError).not.toBe(true); + const recovered = [ + "inspect_native_instruction", + "read_function_instructions", + ]; + const restored = await navigationRefusal(harness.mcp, "next_address"); + expect(restored.details?.alternatives).toEqual(recovered); + expect(restored.remediation.action).toBe( + remediationFor("next_address", recovered), + ); + expect(await offeredInDiscovery()).toEqual([recovered]); + + const assembly = await harness.mcp.callTool({ + name: "procedure_assembly", + arguments: { procedure: "0x401000" }, + }); + expect(assembly.isError).not.toBe(true); + expect( + (await navigationRefusal(harness.mcp, "next_address")).details + ?.alternatives, + ).toEqual(GHIDRA_ALTERNATIVES); + expect(await offeredInDiscovery()).toEqual([GHIDRA_ALTERNATIVES]); + } finally { + await harness.close(); + } + }); + + it("keep generic advice when the provider offers no declared alternative", async () => { + const [target] = await createBinarySessionTargets(); + const { client } = await createApplicationMcpHarness({ + session: createTestBinarySession(createCacheProvider([])), + clientIdentity: { name: "navigation-without-alternatives", version: "1" }, + serverOptions: { environment: {}, logger: silentLogger }, + }); + const opened = await client.callTool({ + name: "open_binary", + arguments: { path: target }, + }); + expect(opened.isError).not.toBe(true); + for (const name of NAVIGATION) { + const { error } = parseMcpToolError( + await client.callTool({ name, arguments: { address: "0x1000" } }), + ); + expect(error).toEqual({ + code: "capability_unavailable", + category: "unsupported_provider", + message: `The selected provider does not declare ${name}.`, + retryable: false, + remediation: { action: expect.stringContaining("binary_session") }, + details: { + provider_id: "fixture", + operation: name, + reason: "operation is not declared by this provider", + }, + }); + } + const { groups } = await navigationAvailability(client, "summary"); + expect(groups).toHaveLength(1); + expect(groups[0]).toMatchObject({ + available: false, + reason: "provider_missing", + }); + expect(groups[0]).not.toHaveProperty("alternatives"); + expect(groups[0]?.names).toEqual(expect.arrayContaining([...NAVIGATION])); + }); +}); diff --git a/tests/boundary/mcp/oversizedErrorDeliveryMcp.test.ts b/tests/boundary/mcp/oversizedErrorDeliveryMcp.test.ts index 64fcb3790a..7df2b02f3e 100644 --- a/tests/boundary/mcp/oversizedErrorDeliveryMcp.test.ts +++ b/tests/boundary/mcp/oversizedErrorDeliveryMcp.test.ts @@ -419,5 +419,4 @@ it.each([false, true])( await Promise.all([client.close(), server.close()]); } }, - 20000, ); diff --git a/tests/boundary/mcp/peCertificatesMcp.test.ts b/tests/boundary/mcp/peCertificatesMcp.test.ts index 99c6386391..20e886c89a 100644 --- a/tests/boundary/mcp/peCertificatesMcp.test.ts +++ b/tests/boundary/mcp/peCertificatesMcp.test.ts @@ -56,15 +56,11 @@ mcpTest( ]), ), ); - const result = await client.callTool({ - name: "inspect_pe_certificates", - arguments: { path }, - }); + const result = await client.callTool( + { name: "inspect_pe_certificates", arguments: { path } }, + { toolDefinition: advertised }, + ); expect(result.isError).not.toBe(true); - expect( - ajv.validate(schema, result.structuredContent), - JSON.stringify(ajv.errors), - ).toBe(true); }, ); diff --git a/tests/boundary/mcp/peImportsMcp.test.ts b/tests/boundary/mcp/peImportsMcp.test.ts index c4efbc4f8d..3002408267 100644 --- a/tests/boundary/mcp/peImportsMcp.test.ts +++ b/tests/boundary/mcp/peImportsMcp.test.ts @@ -27,11 +27,10 @@ mcpTest( async ({ mcp }) => { const { client, session } = await connect(mcp); try { - const { - toolDefinition: advertised, - ajv, - outputSchema, - } = await advertisedMcpToolSchemas(client, "inspect_pe_imports"); + const { toolDefinition: advertised } = await advertisedMcpToolSchemas( + client, + "inspect_pe_imports", + ); expect(advertised?.annotations).toMatchObject({ readOnlyHint: false, idempotentHint: true, @@ -39,15 +38,11 @@ mcpTest( const root = await createTestTempDirectory("rea-pe-imports-mcp-"); const path = join(root, "app.exe"); await writeFile(path, peImportImage({ bits: 64 })); - const response = await client.callTool({ - name: "inspect_pe_imports", - arguments: { path }, - }); + const response = await client.callTool( + { name: "inspect_pe_imports", arguments: { path } }, + { toolDefinition: advertised }, + ); expect(response.isError, JSON.stringify(response.content)).not.toBe(true); - expect( - ajv.validate(outputSchema, response.structuredContent), - JSON.stringify(ajv.errors), - ).toBe(true); const evidence = toolContract("inspect_pe_imports").outputSchema.parse( response.structuredContent, ); @@ -62,15 +57,11 @@ mcpTest( ] as const) { const partialPath = join(root, `invalid-first-descriptor-${label}.exe`); await writeFile(partialPath, bytes); - const partial = await client.callTool({ - name: "inspect_pe_imports", - arguments: { path: partialPath }, - }); + const partial = await client.callTool( + { name: "inspect_pe_imports", arguments: { path: partialPath } }, + { toolDefinition: advertised }, + ); expect(partial.isError, JSON.stringify(partial.content)).not.toBe(true); - expect( - ajv.validate(outputSchema, partial.structuredContent), - JSON.stringify(ajv.errors), - ).toBe(true); const partialReport = toolContract( "inspect_pe_imports", ).outputSchema.parse(partial.structuredContent).normalized_result; diff --git a/tests/boundary/mcp/peLoadConfigMcp.test.ts b/tests/boundary/mcp/peLoadConfigMcp.test.ts index f749fbeebe..667cf90a1f 100644 --- a/tests/boundary/mcp/peLoadConfigMcp.test.ts +++ b/tests/boundary/mcp/peLoadConfigMcp.test.ts @@ -70,5 +70,4 @@ mcpTest( await session.close(); } }, - 60_000, ); diff --git a/tests/boundary/mcp/processCaptureCleanupSchema.test.ts b/tests/boundary/mcp/processCaptureCleanupSchema.test.ts new file mode 100644 index 0000000000..6e7038c81e --- /dev/null +++ b/tests/boundary/mcp/processCaptureCleanupSchema.test.ts @@ -0,0 +1,78 @@ +import { Client, InMemoryTransport } from "@modelcontextprotocol/client"; +import { afterAll, expect, it, onTestFinished } from "vitest"; +import { createServer } from "../../../src/server/createServer.js"; +import { createTestBinarySession } from "../../fixtures/binarySession.js"; +import { advertisedMcpToolSchemas } from "../../fixtures/advertisedMcpTool.js"; +import { createMcpSchemaValidation } from "../../fixtures/mcpSchemaValidation.js"; +import { emptyProcessCapture } from "../../../src/domain/process/processCapture.fixture.js"; +import { parseProcessCapture } from "../../../src/domain/process/processCaptureParsing.js"; +import { createProcessCaptureEvidence } from "../../../src/application/process/ProcessEvidence.js"; +import { processScenarioSchema } from "../../../src/domain/process/processScenario.js"; + +const validation = createMcpSchemaValidation(); +afterAll(() => validation.clear()); + +it.each(["full", "compact"] as const)( + "advertises quiesced cleanup intervention without rejecting saved captures (%s)", + async (profile) => { + const session = createTestBinarySession(() => { + throw new Error("Schema discovery must not start a provider"); + }); + const server = createServer( + { kind: "session", session }, + { + environment: { + ...process.env, + REA_MCP_INPUT_SCHEMA_PROFILE: profile, + REA_MCP_OUTPUT_SCHEMA_PROFILE: "full", + }, + }, + ); + const client = new Client( + { name: "capture-cleanup-schema", version: "1" }, + { jsonSchemaValidator: validation }, + ); + onTestFinished(async () => { + await client.close(); + await server.close(); + await session.close(); + }); + const [clientTransport, serverTransport] = + InMemoryTransport.createLinkedPair(); + await server.connect(serverTransport); + await client.connect(clientTransport); + const schemas = await advertisedMcpToolSchemas( + client, + "capture_process_scenario", + validation.assertions, + ); + const saved = emptyProcessCapture(); + const scenario = processScenarioSchema.parse({ + executable: "fixture-program", + }); + const intervened = parseProcessCapture({ + ...saved, + settlement: { ...saved.settlement, cleanup_outcome: "cleaned" }, + residual_unknowns: [ + { scope: "process", reason: "Fixture cleanup intervention" }, + ], + }); + for (const capture of [saved, intervened]) { + const evidence = createProcessCaptureEvidence(scenario, capture); + expect( + schemas.ajv.validate(schemas.outputSchema, evidence), + JSON.stringify(schemas.ajv.errors), + ).toBe(true); + } + const evidence = createProcessCaptureEvidence(scenario, intervened); + expect( + schemas.ajv.validate(schemas.outputSchema, { + ...evidence, + normalized_result: { + ...intervened, + settlement: { ...intervened.settlement, cleanup_outcome: "invented" }, + }, + }), + ).toBe(false); + }, +); diff --git a/tests/boundary/mcp/promptMcp.test.ts b/tests/boundary/mcp/promptMcp.test.ts index f8d699eaf2..2d6bf66abf 100644 --- a/tests/boundary/mcp/promptMcp.test.ts +++ b/tests/boundary/mcp/promptMcp.test.ts @@ -5,6 +5,7 @@ import { import { ok as resultOk, err, ok } from "../../../src/domain/result.js"; import { readFile, writeFile } from "node:fs/promises"; import { basename, join } from "node:path"; +import { awaitAcknowledgement } from "../../fixtures/lifecycleAcknowledgement.js"; import { Client, @@ -616,7 +617,7 @@ it("guided prompt completion avoids cold startup and cancels warm lookup on clos const directory = await createTestTempDirectory("rea-prompt-warm-only-"); const target = join(directory, "target.hop"); await writeFile(target, "fixture target"); - const fixture = await createWarmCompletionFixture(target); + const fixture = await createWarmCompletionFixture(); const { client } = fixture; const opened = await client.callTool({ @@ -679,7 +680,7 @@ it("guided prompt completion avoids cold startup and cancels warm lookup on clos expect(closed.isError).not.toBe(true); await lifecycle.aborted; expect((await pending).completion.values).toEqual([]); -}, 10_000); +}); it.each(["replacement", "shutdown"])( "waits for cancelled completion cleanup before HTTP %s", @@ -742,9 +743,11 @@ it.each(["replacement", "shutdown"])( const first = await connectHttp(); let next: Client | undefined; let finishing: Promise | undefined; + let initial: ReturnType | undefined; try { - void complete(first, "document", "").catch(() => undefined); - await started; + initial = complete(first, "document", ""); + void initial.catch(() => undefined); + await awaitAcknowledgement(started, initial, "prompt completion start"); let finished = false; finishing = ( ending === "shutdown" @@ -755,7 +758,11 @@ it.each(["replacement", "shutdown"])( ).then(() => { finished = true; }); - await aborted; + await awaitAcknowledgement( + aborted, + finishing, + "prompt completion cancellation", + ); await new Promise((resolve) => setImmediate(resolve)); expect(connections).toHaveBeenCalledTimes(1); expect(finished).toBe(false); @@ -820,7 +827,7 @@ describe("guided prompt completion lifecycle", () => { { signal: controller.signal }, ); await withVerifierCleanup(async () => { - await started; + await awaitAcknowledgement(started, pending, "prompt completion start"); controller.abort(); await expect(pending).rejects.toThrow(); release(); @@ -895,10 +902,10 @@ describe("guided prompt completion lifecycle", () => { ).not.toBe(true); expect(await complete(client, "document", "")).toEqual([]); expect(await complete(client, "procedure", "")).toEqual([]); - }, 10_000); + }); }); -const createWarmCompletionFixture = async (target: string) => { +const createWarmCompletionFixture = async () => { const identity = { id: "deep-fixture", name: "Deep fixture provider", diff --git a/tests/boundary/mcp/providerSelectionMcp.test.ts b/tests/boundary/mcp/providerSelectionMcp.test.ts index 03d2e603e3..a8851c1a3b 100644 --- a/tests/boundary/mcp/providerSelectionMcp.test.ts +++ b/tests/boundary/mcp/providerSelectionMcp.test.ts @@ -1907,7 +1907,7 @@ it.each(["ready", "cancelled", "physical"] as const)( }); let closing: ReturnType | undefined; try { - await entered; + await awaitAcknowledgement(entered, observing, "provider readiness"); if (mode !== "ready") { closing = session.close(); const during = structured( @@ -1979,9 +1979,11 @@ it.each(["ready", "cancelled", "physical"] as const)( expect(starts).toEqual([]); } finally { finish(); - await observing; - await closing; - await session.close(); + try { + await Promise.all([observing, closing]); + } finally { + await session.close(); + } } }, ); diff --git a/tests/boundary/mcp/pseudocodeDeliveryMcp.test.ts b/tests/boundary/mcp/pseudocodeDeliveryMcp.test.ts index a7a1ab4963..55564664cf 100644 --- a/tests/boundary/mcp/pseudocodeDeliveryMcp.test.ts +++ b/tests/boundary/mcp/pseudocodeDeliveryMcp.test.ts @@ -41,15 +41,11 @@ it("retains a complete pseudocode observation once and pages its exact text thro const schema = await advertisedMcpToolSchemas(mcp, "procedure_pseudo_code"); const input = { procedure: "fixture_main", detail: "summary" }; expect(schema.ajv.validate(schema.inputSchema, input)).toBe(true); - const reply = await mcp.callTool({ - name: "procedure_pseudo_code", - arguments: input, - }); + const reply = await mcp.callTool( + { name: "procedure_pseudo_code", arguments: input }, + { toolDefinition: schema.toolDefinition }, + ); expect(reply.isError, JSON.stringify(reply)).not.toBe(true); - expect( - schema.ajv.validate(schema.outputSchema, reply.structuredContent), - JSON.stringify(schema.ajv.errors), - ).toBe(true); const summary = parseEvidence(reply.structuredContent); const result = jsonObjectSchema.parse(summary.normalized_result); expect(result).toMatchObject({ diff --git a/tests/boundary/mcp/pyinstallerArtifactMcp.test.ts b/tests/boundary/mcp/pyinstallerArtifactMcp.test.ts index d079b4b4b5..eab462af30 100644 --- a/tests/boundary/mcp/pyinstallerArtifactMcp.test.ts +++ b/tests/boundary/mcp/pyinstallerArtifactMcp.test.ts @@ -33,10 +33,10 @@ const assertMetadataQueries = async ( const definition = (await client.listTools()).tools.find( (tool) => tool.name === "inspect_analysis_view", ); - expect(definition?.outputSchema).toBeDefined(); + if (definition?.outputSchema === undefined) + throw new Error("Analysis view output schema is missing"); const ajv = schemaValidation.assertions; - const inputSchema: Record = definition!.inputSchema; - const outputSchema: Record = definition!.outputSchema!; + const inputSchema: Record = definition.inputSchema; const ids: string[] = []; const selected: unknown[] = []; for (const offset of [0, 1, 2, 3]) { @@ -54,15 +54,11 @@ const assertMetadataQueries = async ( expect(ajv.validate(inputSchema, args), JSON.stringify(ajv.errors)).toBe( true, ); - const reply = await client.callTool({ - name: "inspect_analysis_view", - arguments: args, - }); + const reply = await client.callTool( + { name: "inspect_analysis_view", arguments: args }, + { toolDefinition: definition }, + ); expect(reply.isError).not.toBe(true); - expect( - ajv.validate(outputSchema, reply.structuredContent), - JSON.stringify(ajv.errors), - ).toBe(true); const evidence = parseEvidence(reply.structuredContent); const view = analysisViewResultSchema.parse(evidence.normalized_result); if (view.kind !== "inventory") throw new Error("Expected inventory page"); diff --git a/tests/boundary/mcp/pythonPycMcp.test.ts b/tests/boundary/mcp/pythonPycMcp.test.ts index 9fde999066..cade91afbd 100644 --- a/tests/boundary/mcp/pythonPycMcp.test.ts +++ b/tests/boundary/mcp/pythonPycMcp.test.ts @@ -167,18 +167,14 @@ it("advertises inspect_python_pyc and reads nested code objects", async () => { }), ]), ); - const response = await client.callTool({ - name: "inspect_python_pyc", - arguments: { path }, - }); + const response = await client.callTool( + { name: "inspect_python_pyc", arguments: { path } }, + { toolDefinition: advertised }, + ); expect(response.isError, JSON.stringify(response.content)).not.toBe(true); const evidence = completePycEvidenceSchema.parse( response.structuredContent, ); - expect( - ajv.validate(outputSchema, response.structuredContent), - JSON.stringify(ajv.errors), - ).toBe(true); expect(evidence.normalized_result.header.python_version).toBe("3.12"); expect(evidence.normalized_result.code_objects[0]?.firstlineno).toBe(-1); expect(evidence.normalized_result.code_objects[1]?.name).toBe("inner"); @@ -218,8 +214,6 @@ it("advertised pyc output schema preserves historical signed fields", async () = ); if (advertised?.outputSchema === undefined) throw new Error("Missing inspect_python_pyc output schema"); - const outputSchema: Record = advertised.outputSchema; - const ajv = schemaValidation.assertions; const root = await createTestTempDirectory("rea-python-pyc-mcp-flags-"); for (const testCase of [ { @@ -259,18 +253,14 @@ it("advertised pyc output schema preserves historical signed fields", async () = marshalCode(testCase.layout, testCase.fields), ]), ); - const response = await client.callTool({ - name: "inspect_python_pyc", - arguments: { path }, - }); + const response = await client.callTool( + { name: "inspect_python_pyc", arguments: { path } }, + { toolDefinition: advertised }, + ); expect(response.isError, JSON.stringify(response.content)).not.toBe(true); const evidence = completePycEvidenceSchema.parse( response.structuredContent, ); - expect( - ajv.validate(outputSchema, response.structuredContent), - JSON.stringify(ajv.errors), - ).toBe(true); expect(evidence.normalized_result.code_objects[0]).toMatchObject( testCase.expected, ); diff --git a/tests/boundary/mcp/recordedCrashMcp.test.ts b/tests/boundary/mcp/recordedCrashMcp.test.ts index f52a1d72b5..10fc985834 100644 --- a/tests/boundary/mcp/recordedCrashMcp.test.ts +++ b/tests/boundary/mcp/recordedCrashMcp.test.ts @@ -65,15 +65,14 @@ it("records inline recorded-core evidence with compact schemas and canonical pat approval: true, }), ).toBe(false); - const response = await client.callTool({ - name: "inspect_recorded_crash", - arguments: { path: value.artifact.path }, - }); + const response = await client.callTool( + { + name: "inspect_recorded_crash", + arguments: { path: value.artifact.path }, + }, + { toolDefinition: advertised }, + ); expect(response.isError).not.toBe(true); - expect( - ajv.validate(outputSchema, response.structuredContent), - JSON.stringify(ajv.errors), - ).toBe(true); const parsed = toolContract("inspect_recorded_crash").outputSchema.parse( response.structuredContent, ); diff --git a/tests/boundary/mcp/releaseExposureMcp.test.ts b/tests/boundary/mcp/releaseExposureMcp.test.ts index 14228b979e..fbfbb327cd 100644 --- a/tests/boundary/mcp/releaseExposureMcp.test.ts +++ b/tests/boundary/mcp/releaseExposureMcp.test.ts @@ -71,5 +71,4 @@ mcpTest( await session.close(); } }, - 60_000, ); diff --git a/tests/boundary/mcp/reverseEngineeringPartialMcp.test.ts b/tests/boundary/mcp/reverseEngineeringPartialMcp.test.ts index 6f44974b04..e32801c019 100644 --- a/tests/boundary/mcp/reverseEngineeringPartialMcp.test.ts +++ b/tests/boundary/mcp/reverseEngineeringPartialMcp.test.ts @@ -1,8 +1,7 @@ import pino from "pino"; import type { CallToolResult } from "@modelcontextprotocol/server"; import { parseMcpToolError } from "../../fixtures/mcpToolError.js"; -import { mkdtemp, rm, symlink, writeFile } from "node:fs/promises"; -import { tmpdir } from "node:os"; +import { rm, symlink, writeFile } from "node:fs/promises"; import { dirname, join } from "node:path"; import { expect, it, vi } from "vitest"; diff --git a/tests/boundary/mcp/rizinStoppedSessionMcp.test.ts b/tests/boundary/mcp/rizinStoppedSessionMcp.test.ts new file mode 100644 index 0000000000..81ea4a813b --- /dev/null +++ b/tests/boundary/mcp/rizinStoppedSessionMcp.test.ts @@ -0,0 +1,154 @@ +import { Client, InMemoryTransport } from "@modelcontextprotocol/client"; +import type { CallToolResult } from "@modelcontextprotocol/server"; +import { McpServer } from "@modelcontextprotocol/server"; +import { expect, it } from "vitest"; + +import { closeVerifierResources } from "../../../scripts/lib/verifier-cleanup.mjs"; +import { toolContract } from "../../../src/contracts/toolContracts.js"; +import { silentLogger } from "../../../src/logger.js"; +import { registerRizinDebugTools } from "../../../src/server/registerRizinDebugTools.js"; +import { createToolResultDelivery } from "../../../src/server/toolResult.js"; +import { parseMcpToolError } from "../../fixtures/mcpToolError.js"; +import { validateDebuggerStatusResponse } from "../../support/reverseDebuggerMcpTestHelpers.js"; +import { createInjectedProcess } from "../providers/RizinDebugSessionCoverage.fixtures.js"; +import { + createRizinTestManager, + rizinTestInferiorTty, +} from "../providers/RizinTestInferiorTty.js"; + +const test = it.skipIf(process.platform === "win32"); + +const LIMIT_REASON = "Rizin command output exceeded the 8 byte command limit."; + +// The injected stream simulates Rizin's NUL-framed protocol; it is not Rizin. +test.each([ + { cleanupRetry: false, command: "noisy", reason: LIMIT_REASON }, + { cleanupRetry: true, command: "noisy", reason: LIMIT_REASON }, + { + cleanupRetry: false, + command: "q", + reason: "The Rizin process exited (exit code null, signal SIGTERM)", + }, +])( + "reports why a Rizin session stopped through MCP after $command (cleanup retry: $cleanupRetry)", + async ({ cleanupRetry, command, reason }) => { + let stops = 0; + const injected = createInjectedProcess({ + onWrite: (text, control) => { + const marker = /!echo (.+)\n/u.exec(text)?.[1]; + if (marker === undefined) return; + if (text.startsWith("noisy\n")) + control.output(`too much output\0${marker}\0`); + else if (text.startsWith("q\n")) setImmediate(() => control.exit()); + else if (text.startsWith("!echo __REA_CLEANUP_SYNC_")) + control.output(`${marker}\0`); + else if (text.startsWith("olj\n")) control.output(`[]\0${marker}\0`); + }, + }); + const manager = createRizinTestManager( + { REA_DEBUGGER_MAX_COMMAND_OUTPUT_BYTES: "8" }, + { + spawnProcess: injected.spawnProcess, + // The injected stream has no OS process; detach through the protocol. + stopProcess: async (session) => { + stops += 1; + if (cleanupRetry && stops === 1) + return { status: "incomplete", reason: "injected stop pending" }; + if (session.exited) return { status: "already_exited" }; + await session.detachInferior?.(); + injected.control.exit(); + return { status: "stopped" }; + }, + }, + ); + const server = new McpServer({ name: "rizin-stopped", version: "1" }); + registerRizinDebugTools( + server, + manager, + silentLogger, + createToolResultDelivery(undefined), + ); + const client = new Client({ name: "rizin-stopped", version: "1" }); + const [clientTransport, serverTransport] = + InMemoryTransport.createLinkedPair(); + const call = (name: string, arguments_: Record) => + client.callTool({ + name, + arguments: arguments_, + }) as Promise; + try { + await server.connect(serverTransport); + await client.connect(clientTransport); + const { session_id } = toolContract( + "start_rizin_debug_session", + ).outputSchema.parse( + ( + await call("start_rizin_debug_session", { + path: "fixture.bin", + inferior_tty: await rizinTestInferiorTty(), + }) + ).structuredContent, + ); + + const stopping = await call("rizin_debug_command", { + session_id, + command, + }); + if (cleanupRetry) + expect(parseMcpToolError(stopping).error.code).toBe( + "cleanup_incomplete", + ); + else expect(stopping.isError).not.toBe(true); + + // The next call retries retained cleanup first, then refuses the + // command with the retained diagnosis instead of an argument error. + const later = parseMcpToolError( + await call("rizin_debug_command", { session_id, command: "dr" }), + ).error; + expect(later).toMatchObject({ + code: "capability_unavailable", + message: expect.stringContaining( + "The Rizin debug session stopped and accepts no more commands.", + ), + remediation: { + action: expect.stringMatching( + /close_rizin_debug_session.*start_rizin_debug_session/u, + ), + }, + details: { reason: expect.stringContaining(reason) }, + }); + expect(later.message).toContain(reason); + expect(stops).toBe(cleanupRetry ? 2 : 1); + + const status = await validateDebuggerStatusResponse( + client, + "rizin_debug_session_status", + session_id, + ); + expect(status).toMatchObject({ + state: "closed", + closed_reason: expect.stringContaining(reason), + }); + + expect( + (await call("close_rizin_debug_session", { session_id })).isError, + ).not.toBe(true); + // A released ID is unknown, which differs from a retained stopped session. + expect( + parseMcpToolError( + await call("rizin_debug_command", { session_id, command: "dr" }), + ).error, + ).toMatchObject({ + code: "invalid_request", + message: + "No Rizin debug session has this session_id in this REA server.", + }); + } finally { + await closeVerifierResources([ + () => client.close(), + () => server.close(), + () => manager.closeAll(), + ]); + } + }, +); diff --git a/tests/boundary/mcp/sameFunctionObservation.test.ts b/tests/boundary/mcp/sameFunctionObservation.test.ts index 915c4e5d1a..948d2913a1 100644 --- a/tests/boundary/mcp/sameFunctionObservation.test.ts +++ b/tests/boundary/mcp/sameFunctionObservation.test.ts @@ -1,6 +1,4 @@ import { afterAll, expect, it } from "vitest"; -import { Ajv2020 } from "ajv/dist/2020.js"; -import type { AnySchema } from "ajv"; import type { Client } from "@modelcontextprotocol/client"; import { createApplicationMcpHarness } from "../../fixtures/applicationMcpHarness.js"; import { createTestBinarySession } from "../../fixtures/binarySession.js"; @@ -102,21 +100,22 @@ it.each(["full", "compact"] as const)( environment: { REA_MCP_OUTPUT_SCHEMA_PROFILE: profile }, }, }); - const schema = (await client.listTools()).tools.find( + const toolDefinition = (await client.listTools()).tools.find( ({ name }) => name === "compare_functions", - )?.outputSchema; - if (schema === undefined) throw new Error("Missing output schema"); - const reply = await client.callTool({ - name: "compare_functions", - arguments: { left: ref(main.evidence_id), right: ref(main.evidence_id) }, - }); + ); + if (toolDefinition?.outputSchema === undefined) + throw new Error("Missing output schema"); + const reply = await client.callTool( + { + name: "compare_functions", + arguments: { + left: ref(main.evidence_id), + right: ref(main.evidence_id), + }, + }, + { toolDefinition }, + ); expect(reply.isError, JSON.stringify(reply.content)).not.toBe(true); - expect( - new Ajv2020({ strict: false }).validate( - schema as AnySchema, - reply.structuredContent, - ), - ).toBe(true); const result = parseEvidence(reply.structuredContent).normalized_result; expect(result).toMatchObject({ status: "unknown", diff --git a/tests/boundary/mcp/serverLifecycleMcp.test.ts b/tests/boundary/mcp/serverLifecycleMcp.test.ts index 051cd1ea50..2083a89048 100644 --- a/tests/boundary/mcp/serverLifecycleMcp.test.ts +++ b/tests/boundary/mcp/serverLifecycleMcp.test.ts @@ -3,6 +3,7 @@ import { parseMcpEvidenceBundle } from "../../fixtures/mcpEvidenceBundle.js"; import { Client, InMemoryTransport } from "@modelcontextprotocol/client"; import type { CallToolResult } from "@modelcontextprotocol/server"; import { afterEach, expect, it } from "vitest"; +import { awaitAcknowledgement } from "../../fixtures/lifecycleAcknowledgement.js"; import type { AnalysisOperationPort } from "../../../src/application/AnalysisProvider.js"; import { HopperRemoteError } from "../../../src/domain/hopperErrors.js"; @@ -615,6 +616,7 @@ it("preserves distinct results across concurrent tool calls", async () => { ["list_names", [{ address: "0x3000", value: "fixture_global" }]], ]); const pending = new Map void>(); + let releaseRemaining = false; let allStarted: () => void = () => { throw new Error("start gate missing"); }; @@ -629,6 +631,7 @@ it("preserves distinct results across concurrent tool calls", async () => { await new Promise((resolve) => { pending.set(name, resolve); if (pending.size === payloads.size) allStarted(); + if (releaseRemaining) resolve(); }); return ok(payload); }, @@ -637,18 +640,28 @@ it("preserves distinct results across concurrent tool calls", async () => { const requests = names.map((name) => client.callTool({ name, arguments: {} }), ); - await started; - for (const name of [...names].reverse()) { - const release = pending.get(name); - if (release === undefined) - throw new Error("request did not reach provider"); - release(); - } - const results = await Promise.all(requests); - results.forEach((result, index) => { - expect(result.isError).not.toBe(true); - expect(structured(result)).toMatchObject({ - normalized_result: payloads.get(names[index] ?? ""), + try { + await awaitAcknowledgement( + started, + Promise.all(requests), + "MCP tool calls", + ); + for (const name of [...names].reverse()) { + const release = pending.get(name); + if (release === undefined) + throw new Error("request did not reach provider"); + release(); + } + const results = await Promise.all(requests); + results.forEach((result, index) => { + expect(result.isError).not.toBe(true); + expect(structured(result)).toMatchObject({ + normalized_result: payloads.get(names[index] ?? ""), + }); }); - }); + } finally { + releaseRemaining = true; + for (const release of pending.values()) release(); + await Promise.allSettled(requests); + } }); diff --git a/tests/boundary/mcp/serverWorkflowMcp.test.ts b/tests/boundary/mcp/serverWorkflowMcp.test.ts index f25027b2a6..e7b16d33bc 100644 --- a/tests/boundary/mcp/serverWorkflowMcp.test.ts +++ b/tests/boundary/mcp/serverWorkflowMcp.test.ts @@ -168,4 +168,4 @@ it("advertises the complete currently available inventory with a session", async expect(tool.inputSchema, tool.name).toBeDefined(); expect(tool.outputSchema, tool.name).toBeDefined(); } -}, 10_000); +}); diff --git a/tests/boundary/mcp/sessionAvailabilityRecovery.test.ts b/tests/boundary/mcp/sessionAvailabilityRecovery.test.ts index 0122663abf..49f1b5b95c 100644 --- a/tests/boundary/mcp/sessionAvailabilityRecovery.test.ts +++ b/tests/boundary/mcp/sessionAvailabilityRecovery.test.ts @@ -118,7 +118,7 @@ it("restores MCP tool availability after cancelling a target switch", async () = { signal: controller.signal }, ); const cancelled = expect(switching).rejects.toThrow(); - await closing.promise; + await awaitAcknowledgement(closing.promise, switching, "provider close"); controller.abort(); await cancellationObserved.promise; release.resolve(); diff --git a/tests/boundary/mcp/sessionMcp.test.ts b/tests/boundary/mcp/sessionMcp.test.ts index 2f2aeb01ad..a91a59312b 100644 --- a/tests/boundary/mcp/sessionMcp.test.ts +++ b/tests/boundary/mcp/sessionMcp.test.ts @@ -6,10 +6,11 @@ import { readFile, realpath, rm, writeFile } from "node:fs/promises"; import { join } from "node:path"; import { Client, InMemoryTransport } from "@modelcontextprotocol/client"; import type { CallToolResult } from "@modelcontextprotocol/server"; -import { afterEach, describe, expect, it, vi } from "vitest"; +import { afterEach, describe, expect, it, onTestFinished, vi } from "vitest"; import { z } from "zod"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; +import { awaitAcknowledgement } from "../../fixtures/lifecycleAcknowledgement.js"; import { createDeferred, @@ -98,7 +99,12 @@ describe("MCP snapshot lifecycle ordering", () => { name: "address_name", arguments: { address: "0x1000", document: "fixture" }, }); - await started.promise; + onTestFinished(() => release.resolve(ok(first))); + await awaitAcknowledgement( + started.promise, + observation, + "session observation", + ); const snapshotPath = join(directory, "analysis.json"); const closing = mcp.callTool({ name: "close_binary", @@ -164,7 +170,7 @@ describe("MCP snapshot lifecycle ordering", () => { }) ).isError, ).not.toBe(true); - }, 10_000); + }); }); describe("public provider availability", () => { @@ -360,8 +366,16 @@ describe("target-free MCP lifecycle", () => { await server.connect(serverTransport); await mcp.connect(clientTransport); + onTestFinished(() => + availability.resolve({ + status: "available", + code: null, + reason: null, + diagnostics: { configured: true }, + }), + ); const status = mcp.callTool({ name: "binary_session", arguments: {} }); - await entered.promise; + await awaitAcknowledgement(entered.promise, status, "provider readiness"); expect((await session.open(targetPath)).ok).toBe(true); availability.resolve({ status: "available", @@ -437,7 +451,7 @@ describe("target-free MCP lifecycle", () => { path: canonicalTargetPath, sha256: z.object({ sha256: z.string() }).parse(second).sha256, }); - }, 30_000); + }); }); describe("snapshot receipt after MCP cleanup", () => { @@ -688,7 +702,7 @@ describe("MCP snapshot open ordering", () => { expect( text(await mcp.callTool({ name: "binary_session", arguments: {} })), ).toContain('"open":false'); - }, 10_000); + }); }); describe("session filesystem path boundaries over MCP", () => { @@ -741,7 +755,7 @@ describe("session filesystem path boundaries over MCP", () => { }, }); await mcp.callTool({ name: "close_binary", arguments: {} }); - }, 10_000); + }); }); describe("json depth bounds over MCP", () => { @@ -795,7 +809,7 @@ describe("json depth bounds over MCP", () => { expect(recovered.isError, JSON.stringify(recovered.content)).not.toBe(true); expect(recovered.structuredContent).toEqual(baseline.structuredContent); expect(createClient).not.toHaveBeenCalled(); - }, 10_000); + }); }); const client = (path: string, closed: string[]): AnalysisClient => ({ diff --git a/tests/boundary/mcp/shutdown.test.ts b/tests/boundary/mcp/shutdown.test.ts index 663a27c803..b2b7ff2e38 100644 --- a/tests/boundary/mcp/shutdown.test.ts +++ b/tests/boundary/mcp/shutdown.test.ts @@ -471,7 +471,7 @@ it("does not leave a provider running when a target open is queued behind shutdo }, }); expect(session.activeTarget()).toBeUndefined(); - expect(session.activeBundleResourceRoot()).toBeUndefined(); + expect(await session.status()).toMatchObject({ open: false }); expect(created).toBe(closed); }); diff --git a/tests/boundary/mcp/sourcePublicationContext.test.ts b/tests/boundary/mcp/sourcePublicationContext.test.ts index c7261b661b..ac20643d8d 100644 --- a/tests/boundary/mcp/sourcePublicationContext.test.ts +++ b/tests/boundary/mcp/sourcePublicationContext.test.ts @@ -126,13 +126,6 @@ it.each( { timeout: 120000, toolDefinition: tool }, ); expect(result.isError, JSON.stringify(result.content)).not.toBe(true); - expect( - validation.assertions.validate( - tool.outputSchema, - result.structuredContent, - ), - JSON.stringify(validation.assertions.errors), - ).toBe(true); expect(result.structuredContent).toMatchObject({ result: { name: { state: "available", value: "entry" }, @@ -171,7 +164,6 @@ it.each( await session.close(); } }, - 120000, ); it.each([false, true])( @@ -283,7 +275,6 @@ it.each([false, true])( await session.close(); } }, - 120000, ); it("keeps a smaller sibling recovery when a later completed observation exceeds delivery", async () => { @@ -389,4 +380,4 @@ it("keeps a smaller sibling recovery when a later completed observation exceeds await server.close(); await session.close(); } -}, 120000); +}); diff --git a/tests/boundary/mcp/sourcePublicationRecovery.test.ts b/tests/boundary/mcp/sourcePublicationRecovery.test.ts index d44e8d54a7..718a6b8dde 100644 --- a/tests/boundary/mcp/sourcePublicationRecovery.test.ts +++ b/tests/boundary/mcp/sourcePublicationRecovery.test.ts @@ -325,7 +325,6 @@ it.each([ await session.close(); } }, - 120000, ); it("reports omission when the primary cannot be safely authenticated", async () => { diff --git a/tests/boundary/mcp/stableArtifactSymlinkRecoveryMcp.test.ts b/tests/boundary/mcp/stableArtifactSymlinkRecoveryMcp.test.ts index c1611a1021..edb1892e00 100644 --- a/tests/boundary/mcp/stableArtifactSymlinkRecoveryMcp.test.ts +++ b/tests/boundary/mcp/stableArtifactSymlinkRecoveryMcp.test.ts @@ -26,6 +26,10 @@ mcpTest.skipIf(process.platform === "win32")( for (const name of [ "inspect_rust_binary", "inspect_elf_notes", + "inspect_native_initializers", + "inspect_binary_hardening", + "inspect_native_exports", + "inspect_apk_signature", "inspect_devicetree", "inspect_sqlite_database", ]) { diff --git a/tests/boundary/mcp/stdioFrameDiagnostics.test.ts b/tests/boundary/mcp/stdioFrameDiagnostics.test.ts index def2a1c846..aee9ea79fd 100644 --- a/tests/boundary/mcp/stdioFrameDiagnostics.test.ts +++ b/tests/boundary/mcp/stdioFrameDiagnostics.test.ts @@ -3,12 +3,17 @@ import { StdioClientTransport } from "@modelcontextprotocol/client/stdio"; import { createHash } from "node:crypto"; import { writeFile } from "node:fs/promises"; import { resolve } from "node:path"; -import { expect } from "vitest"; +import { afterAll, expect } from "vitest"; import { z } from "zod"; import { parseEvidence } from "../../../src/domain/evidence.js"; import { byteEntropySchema } from "../../../src/domain/native/byteEntropy.js"; import { workspaceCliTest } from "../../support/cli/workspaceCliFixture.js"; +import { createMcpSchemaValidation } from "../../fixtures/mcpSchemaValidation.js"; + +// Compile SDK output validators for the tools a test calls, not the whole catalog. +const schemaValidation = createMcpSchemaValidation(); +afterAll(() => schemaValidation.clear()); workspaceCliTest.for([ { mode: "deferred", profile: "full" }, @@ -37,7 +42,10 @@ workspaceCliTest.for([ }); const client = new Client( { name: "frame-diagnostic-regression", version: "1" }, - { versionNegotiation: { mode: "legacy" } }, + { + versionNegotiation: { mode: "legacy" }, + jsonSchemaValidator: schemaValidation, + }, ); try { await client.connect(transport); diff --git a/tests/boundary/mcp/streamableHttpMcp.test.ts b/tests/boundary/mcp/streamableHttpMcp.test.ts index 0feae65669..78db023402 100644 --- a/tests/boundary/mcp/streamableHttpMcp.test.ts +++ b/tests/boundary/mcp/streamableHttpMcp.test.ts @@ -13,8 +13,6 @@ import { import { processTest } from "../../support/process/processFixture.js"; import { assertAdvertisedMcpContracts } from "./mcpContractHarness.js"; -const HTTP_BOUNDARY_TIMEOUT_MS = 90_000; - const openSession = async (url: URL, name: string) => { const transport = new StreamableHTTPClientTransport(url); const client = new Client({ name, version: "1" }); @@ -95,7 +93,6 @@ describe("rea mcp --http", () => { await expectIdentity(second); await second.transport.terminateSession(); }, - HTTP_BOUNDARY_TIMEOUT_MS, ); processTest( @@ -140,7 +137,6 @@ describe("rea mcp --http", () => { "MCP HTTP session replaced by a new client", ); }, - HTTP_BOUNDARY_TIMEOUT_MS, ); processTest( @@ -150,6 +146,5 @@ describe("rea mcp --http", () => { const refused = await expectForeignHostRefused(url); expect(refused.body).not.toContain("127.0.0.1"); }, - HTTP_BOUNDARY_TIMEOUT_MS, ); }); diff --git a/tests/boundary/mcp/toolInputCapacityHttp.test.ts b/tests/boundary/mcp/toolInputCapacityHttp.test.ts index aa3619f865..c5ef69368c 100644 --- a/tests/boundary/mcp/toolInputCapacityHttp.test.ts +++ b/tests/boundary/mcp/toolInputCapacityHttp.test.ts @@ -103,7 +103,6 @@ processTest( expect(observed.bytes).toBeLessThan(4096); } }, - 90_000, ); processTest( @@ -162,7 +161,6 @@ processTest( JSON.stringify(observations), ); }, - 90_000, ); processTest( @@ -225,5 +223,4 @@ processTest( }), ); }, - 90_000, ); diff --git a/tests/boundary/mcp/toolInputCapacityStdio.test.ts b/tests/boundary/mcp/toolInputCapacityStdio.test.ts index e766ed57ab..b4b5d3cc60 100644 --- a/tests/boundary/mcp/toolInputCapacityStdio.test.ts +++ b/tests/boundary/mcp/toolInputCapacityStdio.test.ts @@ -55,4 +55,4 @@ it("applies the selected input allowance through the public deferred stdio entry arguments: {}, }); expect(healthy.isError).not.toBe(true); -}, 90_000); +}); diff --git a/tests/boundary/mcp/uImageMcp.test.ts b/tests/boundary/mcp/uImageMcp.test.ts index 9d88e9a86d..d079bef6c0 100644 --- a/tests/boundary/mcp/uImageMcp.test.ts +++ b/tests/boundary/mcp/uImageMcp.test.ts @@ -93,5 +93,4 @@ it.each(["full", "compact"] as const)( await rm(outputRoot, { recursive: true, force: true }); } }, - 60_000, ); diff --git a/tests/boundary/mcp/unityFsMcp.test.ts b/tests/boundary/mcp/unityFsMcp.test.ts index b6e232fda6..aa43c1d3fc 100644 --- a/tests/boundary/mcp/unityFsMcp.test.ts +++ b/tests/boundary/mcp/unityFsMcp.test.ts @@ -107,5 +107,4 @@ it.each(["full", "compact"] as const)( await rm(outputRoot, { recursive: true, force: true }); } }, - 60_000, ); diff --git a/tests/boundary/mcp/wasmArtifactMcp.test.ts b/tests/boundary/mcp/wasmArtifactMcp.test.ts index dede3d394f..85314d2890 100644 --- a/tests/boundary/mcp/wasmArtifactMcp.test.ts +++ b/tests/boundary/mcp/wasmArtifactMcp.test.ts @@ -4,6 +4,7 @@ import { } from "../../fixtures/advertisedMcpTool.js"; import { Client, InMemoryTransport } from "@modelcontextprotocol/client"; import { afterAll, expect, it, onTestFinished, vi } from "vitest"; +import { awaitAcknowledgement } from "../../fixtures/lifecycleAcknowledgement.js"; import { createMcpSchemaValidation } from "../../fixtures/mcpSchemaValidation.js"; import { readFile, realpath, writeFile } from "node:fs/promises"; import { join } from "node:path"; @@ -36,6 +37,7 @@ const assertFirstSummaryDelivery = async ( ajv: wasmAjv, inputSchema, outputSchema, + toolDefinition, } = await advertisedMcpToolSchemas( harness.client, "inspect_wasm_artifact", @@ -43,15 +45,11 @@ const assertFirstSummaryDelivery = async ( ); const summaryInput = { path: value.artifact.path, detail: "summary" }; expect(wasmAjv.validate(inputSchema, summaryInput)).toBe(true); - const directSummary = await harness.client.callTool({ - name: "inspect_wasm_artifact", - arguments: summaryInput, - }); + const directSummary = await harness.client.callTool( + { name: "inspect_wasm_artifact", arguments: summaryInput }, + { toolDefinition }, + ); 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, @@ -227,14 +225,16 @@ it("records inline WASM evidence with compact inputs and canonical option checks approval: true, }), ).toBe(false); - const response = await client.callTool({ - name: "inspect_wasm_artifact", - arguments: { path: value.artifact.path, command_timeout_ms: 42_000 }, - }); + const response = await client.callTool( + { + name: "inspect_wasm_artifact", + arguments: { path: value.artifact.path, command_timeout_ms: 42_000 }, + }, + { toolDefinition: schemas.toolDefinition }, + ); assertRecordedMcpEvidence( response, "inspect_wasm_artifact", - schemas, session, value, "observed", @@ -309,45 +309,52 @@ it.each([undefined, "summary"] as const)( { signal: controller.signal }, ); const rejected = expect(cancellation).rejects.toThrow(); - await started; - controller.abort(new Error("cancel after provider work starts")); - let serverReason: unknown; try { - await harness.client.ping(); - serverReason = serverSignal?.reason; - } finally { - releaseProvider?.(); - } - await rejected; - expect(inspect).toHaveBeenCalledOnce(); - const result = await inspect.mock.results[0]?.value; - expect(result).toMatchObject({ - ok: false, - error: { - _tag: "AnalysisCancelledError", - partialObservation: { - operation: "inspect_wasm_artifact", - normalized_result: value, - confidence: "observed", + await awaitAcknowledgement(started, cancellation, "WASM provider start"); + controller.abort(new Error("cancel after provider work starts")); + let serverReason: unknown; + try { + await harness.client.ping(); + serverReason = serverSignal?.reason; + } finally { + releaseProvider?.(); + } + await rejected; + expect(inspect).toHaveBeenCalledOnce(); + const result = await inspect.mock.results[0]?.value; + expect(result).toMatchObject({ + ok: false, + error: { + _tag: "AnalysisCancelledError", + partialObservation: { + operation: "inspect_wasm_artifact", + normalized_result: value, + confidence: "observed", + }, }, - }, - }); - if (result === undefined || result.ok) - throw new Error("Expected cancelled WASM Evidence"); - expect(serverSignal?.aborted).toBe(true); - expect(result.error.cause).toBe(serverReason); - const partial = parseEvidence(result.error.partialObservation); - expect(partial.parameters).toEqual({ - path: value.artifact.path, - glue_paths: [], - candidate_paths: [], - }); - await harness.client.ping(); - await vi.waitFor(() => { - // Failed summary delivery retains the complete producer, without a derived view. - expect(session.exportEvidenceBundle().records).toEqual([partial]); - }); - expect(session.evidenceById(partial.evidence_id)).toEqual(partial); + }); + if (result === undefined || result.ok) + throw new Error("Expected cancelled WASM Evidence"); + expect(serverSignal?.aborted).toBe(true); + expect(result.error.cause).toBe(serverReason); + const partial = parseEvidence(result.error.partialObservation); + expect(partial.parameters).toEqual({ + path: value.artifact.path, + glue_paths: [], + candidate_paths: [], + }); + await harness.client.ping(); + await vi.waitFor(() => { + // Failed summary delivery retains the complete producer, without a derived view. + expect(session.exportEvidenceBundle().records).toEqual([partial]); + }); + expect(session.evidenceById(partial.evidence_id)).toEqual(partial); + } finally { + controller.abort(); + releaseProvider?.(); + await cancellation.catch(() => undefined); + await rejected.catch(() => undefined); + } }, ); @@ -549,7 +556,6 @@ it("reads an oversized completed WASM result through its reported retained refer throw new Error("Missing WASM view schemas"); const ajv = schemaValidation.assertions; const input: Record = advertised.inputSchema; - const output: Record = advertised.outputSchema; for (const [view, expected] of [ [ { kind: "summary" }, @@ -581,18 +587,14 @@ it("reads an oversized completed WASM result through its reported retained refer ] 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, - }); + const selected = await harness.client.callTool( + { name: "inspect_analysis_view", arguments: args }, + { toolDefinition: advertised }, + ); 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]); diff --git a/tests/boundary/mcp/webBundleContainmentMcp.test.ts b/tests/boundary/mcp/webBundleContainmentMcp.test.ts index f35c0992f6..e4bb56bfd7 100644 --- a/tests/boundary/mcp/webBundleContainmentMcp.test.ts +++ b/tests/boundary/mcp/webBundleContainmentMcp.test.ts @@ -157,4 +157,4 @@ it("retains timeout Evidence and recovers on the same MCP server", async () => { }); await client.ping(); expect(browser.activeConnectionCount()).toBe(0); -}, 60_000); +}); diff --git a/tests/boundary/mcp/webBundleViewsMcp.test.ts b/tests/boundary/mcp/webBundleViewsMcp.test.ts index 978ec282c2..867258559e 100644 --- a/tests/boundary/mcp/webBundleViewsMcp.test.ts +++ b/tests/boundary/mcp/webBundleViewsMcp.test.ts @@ -7,7 +7,7 @@ import { webBundlePageViewResultSchema, webBundleSummaryViewResultSchema, } from "../../../src/domain/analysisView/analysisView.js"; -import type { WebBundleAnalysis } from "../../../src/domain/webBundleAnalysis.js"; +import { sampleWebBundleAnalysis } from "../../fixtures/webBundleAnalysis.js"; import { createTestBinarySession } from "../../fixtures/binarySession.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; import { createMcpSchemaValidation } from "../../fixtures/mcpSchemaValidation.js"; @@ -16,104 +16,6 @@ import { advertisedMcpToolSchemas } from "../../fixtures/advertisedMcpTool.js"; const schemaValidation = createMcpSchemaValidation(); afterAll(() => schemaValidation.clear()); -const sampleWebBundleAnalysis = (): WebBundleAnalysis => ({ - capture: { - target_url: "https://example.test/app", - target_origin: "https://example.test", - scripts_observed: 3, - scripts_analyzed: 2, - source_artifacts: [], - }, - observations: { - chunks: { - nodes: [ - { - script_key: "app.js", - url: "https://example.test/app.js", - artifact_sha256: "a".repeat(64), - bytes: 1200, - }, - { - script_key: "vendor.js", - url: "https://example.test/vendor.js", - artifact_sha256: "b".repeat(64), - bytes: 4500, - }, - ], - edges: [], - }, - routes: [], - endpoints: [ - { - value: "/api/users", - mechanism: "call:fetch", - location: { script_key: "app.js", line: 10, column: 2 }, - }, - { - value: "/api/users", - mechanism: "call:fetch", - location: { script_key: "vendor.js", line: 42, column: 8 }, - }, - { - value: "/api/orders", - mechanism: "call:sendBeacon", - location: { script_key: "app.js", line: 85, column: 4 }, - }, - ], - webmcp_declarations: [], - source_maps: { - status: "not_requested", - requested: 0, - processed: 0, - items: [], - }, - }, - inferences: [ - { - kind: "endpoint_candidate", - value: "/api/dynamic-user", - confidence: "low", - basis: [ - { - script_key: "app.js", - artifact_sha256: "a".repeat(64), - line: 25, - column: 4, - detector: "call:client.get:unverified-network-receiver", - }, - ], - }, - { - kind: "vendor_fingerprint", - value: "React", - confidence: "high", - basis: [ - { - script_key: "vendor.js", - artifact_sha256: "b".repeat(64), - line: 1, - column: 0, - detector: "vendor:react", - }, - ], - }, - ], - unknowns: [ - { - dimension: "network", - reason: "Unverified client receiver", - affected_script_keys: ["app.js"], - }, - ], - completeness: { - status: "partial", - parsed_scripts: 2, - parse_failures: 1, - visited_ast_nodes: 150, - }, - limitations: ["Script evaluation was omitted"], -}); - const makeParent = () => createEvidence( undefined, @@ -150,18 +52,18 @@ it("projects web bundle summary views over MCP using retained and inline sources const parent = makeParent(); expect(session.recordEvidence(parent).ok).toBe(true); - const summaryCall = await client.callTool({ - ...schemas.toolDefinition, - arguments: { - source: { kind: "retained-evidence", evidence_id: parent.evidence_id }, - view: { kind: "summary" }, + const summaryCall = await client.callTool( + { + name: schemas.toolDefinition.name, + arguments: { + source: { kind: "retained-evidence", evidence_id: parent.evidence_id }, + view: { kind: "summary" }, + }, }, - }); + { toolDefinition: schemas.toolDefinition }, + ); expect(summaryCall.isError, JSON.stringify(summaryCall)).not.toBe(true); - expect( - schemas.ajv.validate(schemas.outputSchema, summaryCall.structuredContent), - JSON.stringify(schemas.ajv.errors), - ).toBe(true); + const summaryResult = webBundleSummaryViewResultSchema.parse( parseEvidence(summaryCall.structuredContent).normalized_result, ); @@ -173,21 +75,18 @@ it("projects web bundle summary views over MCP using retained and inline sources completeness: { status: "partial" }, }); - const collectionSummaryCall = await client.callTool({ - ...schemas.toolDefinition, - arguments: { - source: { kind: "inline", evidence: parent }, - view: { kind: "web-bundle", collection: "summary" }, + const collectionSummaryCall = await client.callTool( + { + name: schemas.toolDefinition.name, + arguments: { + source: { kind: "inline", evidence: parent }, + view: { kind: "web-bundle", collection: "summary" }, + }, }, - }); + { toolDefinition: schemas.toolDefinition }, + ); expect(collectionSummaryCall.isError).not.toBe(true); - expect( - schemas.ajv.validate( - schemas.outputSchema, - collectionSummaryCall.structuredContent, - ), - JSON.stringify(schemas.ajv.errors), - ).toBe(true); + const collectionSummaryResult = webBundlePageViewResultSchema.parse( parseEvidence(collectionSummaryCall.structuredContent).normalized_result, ); @@ -205,23 +104,23 @@ it("pages web bundle endpoints and inferences over MCP using evidence-file sourc const evidenceFile = join(tempDir, "webbundle-evidence.json"); await writeFile(evidenceFile, JSON.stringify(parent)); - const page1Call = await client.callTool({ - ...schemas.toolDefinition, - arguments: { - source: { kind: "evidence-file", path: evidenceFile }, - view: { - kind: "web-bundle", - collection: "endpoints", - offset: 0, - limit: 2, + const page1Call = await client.callTool( + { + name: schemas.toolDefinition.name, + arguments: { + source: { kind: "evidence-file", path: evidenceFile }, + view: { + kind: "web-bundle", + collection: "endpoints", + offset: 0, + limit: 2, + }, }, }, - }); + { toolDefinition: schemas.toolDefinition }, + ); expect(page1Call.isError).not.toBe(true); - expect( - schemas.ajv.validate(schemas.outputSchema, page1Call.structuredContent), - JSON.stringify(schemas.ajv.errors), - ).toBe(true); + const page1 = webBundlePageViewResultSchema.parse( parseEvidence(page1Call.structuredContent).normalized_result, ); @@ -243,23 +142,23 @@ it("pages web bundle endpoints and inferences over MCP using evidence-file sourc }); expect(page1.completeness.status).toBe("partial"); - const page2Call = await client.callTool({ - ...schemas.toolDefinition, - arguments: { - source: { kind: "evidence-file", path: evidenceFile }, - view: { - kind: "web-bundle", - collection: "endpoints", - offset: 2, - limit: 2, + const page2Call = await client.callTool( + { + name: schemas.toolDefinition.name, + arguments: { + source: { kind: "evidence-file", path: evidenceFile }, + view: { + kind: "web-bundle", + collection: "endpoints", + offset: 2, + limit: 2, + }, }, }, - }); + { toolDefinition: schemas.toolDefinition }, + ); expect(page2Call.isError).not.toBe(true); - expect( - schemas.ajv.validate(schemas.outputSchema, page2Call.structuredContent), - JSON.stringify(schemas.ajv.errors), - ).toBe(true); + const page2 = webBundlePageViewResultSchema.parse( parseEvidence(page2Call.structuredContent).normalized_result, ); @@ -276,25 +175,22 @@ it("pages web bundle endpoints and inferences over MCP using evidence-file sourc exhausted: true, }); - const candidatesCall = await client.callTool({ - ...schemas.toolDefinition, - arguments: { - source: { kind: "evidence-file", path: evidenceFile }, - view: { - kind: "web-bundle", - collection: "inferences", - kinds: ["endpoint_candidate"], + const candidatesCall = await client.callTool( + { + name: schemas.toolDefinition.name, + arguments: { + source: { kind: "evidence-file", path: evidenceFile }, + view: { + kind: "web-bundle", + collection: "inferences", + kinds: ["endpoint_candidate"], + }, }, }, - }); + { toolDefinition: schemas.toolDefinition }, + ); expect(candidatesCall.isError).not.toBe(true); - expect( - schemas.ajv.validate( - schemas.outputSchema, - candidatesCall.structuredContent, - ), - JSON.stringify(schemas.ajv.errors), - ).toBe(true); + const candidatesPage = webBundlePageViewResultSchema.parse( parseEvidence(candidatesCall.structuredContent).normalized_result, ); @@ -307,23 +203,23 @@ it("pages web bundle endpoints and inferences over MCP using evidence-file sourc }), ]); - const emptyCall = await client.callTool({ - ...schemas.toolDefinition, - arguments: { - source: { kind: "evidence-file", path: evidenceFile }, - view: { - kind: "web-bundle", - collection: "endpoints", - offset: 100, - limit: 10, + const emptyCall = await client.callTool( + { + name: schemas.toolDefinition.name, + arguments: { + source: { kind: "evidence-file", path: evidenceFile }, + view: { + kind: "web-bundle", + collection: "endpoints", + offset: 100, + limit: 10, + }, }, }, - }); + { toolDefinition: schemas.toolDefinition }, + ); expect(emptyCall.isError).not.toBe(true); - expect( - schemas.ajv.validate(schemas.outputSchema, emptyCall.structuredContent), - JSON.stringify(schemas.ajv.errors), - ).toBe(true); + const emptyPage = webBundlePageViewResultSchema.parse( parseEvidence(emptyCall.structuredContent).normalized_result, ); diff --git a/tests/boundary/mcp/webModuleTraceMcp.test.ts b/tests/boundary/mcp/webModuleTraceMcp.test.ts index 1707726c6d..8f8c30f260 100644 --- a/tests/boundary/mcp/webModuleTraceMcp.test.ts +++ b/tests/boundary/mcp/webModuleTraceMcp.test.ts @@ -52,18 +52,13 @@ it("publishes valid SDK schemas and records the named module trace without a bin throw new Error("module trace must advertise both schemas"); const ajv = new Ajv2020({ strict: false, validateFormats: false }); const inputSchema: Record = advertised.inputSchema; - const outputSchema: Record = advertised.outputSchema; const args = { manifest_path: "/analysis/manifest.json", script_index: 0 }; expect(ajv.validate(inputSchema, args)).toBe(true); - const response = await client.callTool({ - name: "trace_web_module_imports", - arguments: args, - }); + const response = await client.callTool( + { name: "trace_web_module_imports", arguments: args }, + { toolDefinition: advertised }, + ); expect(response.isError).not.toBe(true); - expect( - ajv.validate(outputSchema, response.structuredContent), - JSON.stringify(ajv.errors), - ).toBe(true); const parsed = toolContract("trace_web_module_imports").outputSchema.parse( response.structuredContent, ); diff --git a/tests/boundary/mcp/webNetworkCaptureMcp.test.ts b/tests/boundary/mcp/webNetworkCaptureMcp.test.ts index 51c9d47c4a..514c2d372c 100644 --- a/tests/boundary/mcp/webNetworkCaptureMcp.test.ts +++ b/tests/boundary/mcp/webNetworkCaptureMcp.test.ts @@ -69,15 +69,11 @@ it("publishes valid SDK schemas and retains historical inline Evidence with the expect(ajv.validateSchema(outputSchema)).toBe(true); const args = { capture_path: "/capture.har", format: "har" }; expect(ajv.validate(inputSchema, args)).toBe(true); - const response = await client.callTool({ - name: "inspect_web_network_capture", - arguments: args, - }); + const response = await client.callTool( + { name: "inspect_web_network_capture", arguments: args }, + { toolDefinition: advertised }, + ); expect(response.isError).not.toBe(true); - expect( - ajv.validate(outputSchema, response.structuredContent), - JSON.stringify(ajv.errors), - ).toBe(true); const parsed = toolContract("inspect_web_network_capture").outputSchema.parse( response.structuredContent, ); diff --git a/tests/boundary/mcp/webRuntimeMcp.test.ts b/tests/boundary/mcp/webRuntimeMcp.test.ts index 7f66b2eba9..af55101182 100644 --- a/tests/boundary/mcp/webRuntimeMcp.test.ts +++ b/tests/boundary/mcp/webRuntimeMcp.test.ts @@ -71,12 +71,11 @@ it("advertises actual SDK schemas, instrumentation effects and session-owned inl selector: "#selected", }; expect(ajv.validate(inputSchema, args)).toBe(true); - const response = await client.callTool({ name, arguments: args }); + const response = await client.callTool( + { name, arguments: args }, + { toolDefinition: advertised }, + ); expect(response.isError).not.toBe(true); - expect( - ajv.validate(outputSchema, response.structuredContent), - JSON.stringify(ajv.errors), - ).toBe(true); const parsed = toolContract(name).outputSchema.parse( response.structuredContent, ); @@ -178,7 +177,7 @@ cliTest( ], environment: process.env, }); - expect(cliResult.exitCode).toBe(0); + expect(cliResult.exitCode, cliResult.stderr).toBe(0); expect(cliResult.signal).toBeNull(); expect(parseEvidence(cliResult.json).normalized_result).toMatchObject({ target: { @@ -187,7 +186,6 @@ cliTest( }); } }, - 20_000, ); cliTest( @@ -219,25 +217,22 @@ cliTest( await client.connect(clientTransport); const advertised = (await client.listTools()).tools.find( ({ name }) => name === "inspect_web_event_listeners", - )?.outputSchema; - if (advertised === undefined) + ); + if (advertised?.outputSchema === undefined) throw new Error("Missing advertised listener output schema"); - const outputSchema: Record = advertised; - const response = await client.callTool({ - name: "inspect_web_event_listeners", - arguments: { - cdp_endpoint: browser.endpoint, - target_id: "allowed-page", - allowed_origins: [browser.allowedOrigin], - selector: "#selected", + const response = await client.callTool( + { + name: "inspect_web_event_listeners", + arguments: { + cdp_endpoint: browser.endpoint, + target_id: "allowed-page", + allowed_origins: [browser.allowedOrigin], + selector: "#selected", + }, }, - }); + { toolDefinition: advertised }, + ); expect(response.isError).not.toBe(true); - const ajv = schemaValidation.assertions; - expect( - ajv.validate(outputSchema, response.structuredContent), - JSON.stringify(ajv.errors), - ).toBe(true); const cliResult = await cli.run({ arguments: [ "inspect-web-event-listeners", @@ -250,7 +245,7 @@ cliTest( ], environment: process.env, }); - expect(cliResult.exitCode).toBe(0); + expect(cliResult.exitCode, cliResult.stderr).toBe(0); for (const evidence of [ toolContract("inspect_web_event_listeners").outputSchema.parse( response.structuredContent, @@ -275,7 +270,6 @@ cliTest( ], }); }, - 20_000, ); it.each([ diff --git a/tests/boundary/mcp/webSourceLocationMcp.test.ts b/tests/boundary/mcp/webSourceLocationMcp.test.ts index 98828cc4e6..a46fb994f4 100644 --- a/tests/boundary/mcp/webSourceLocationMcp.test.ts +++ b/tests/boundary/mcp/webSourceLocationMcp.test.ts @@ -54,17 +54,12 @@ it("publishes valid SDK schemas and records the named module trace without a bin throw new Error("module trace must advertise both schemas"); const ajv = new Ajv2020({ strict: false, validateFormats: false }); const inputSchema: Record = advertised.inputSchema; - const outputSchema: Record = advertised.outputSchema; expect(ajv.validate(inputSchema, args)).toBe(true); - const response = await client.callTool({ - name: "trace_web_source_location", - arguments: args, - }); + const response = await client.callTool( + { name: "trace_web_source_location", arguments: args }, + { toolDefinition: advertised }, + ); expect(response.isError).not.toBe(true); - expect( - ajv.validate(outputSchema, response.structuredContent), - JSON.stringify(ajv.errors), - ).toBe(true); const parsed = toolContract("trace_web_source_location").outputSchema.parse( response.structuredContent, ); diff --git a/tests/boundary/mcp/wideOccurrenceCompletion.test.ts b/tests/boundary/mcp/wideOccurrenceCompletion.test.ts new file mode 100644 index 0000000000..a1fe147d14 --- /dev/null +++ b/tests/boundary/mcp/wideOccurrenceCompletion.test.ts @@ -0,0 +1,29 @@ +import { fileURLToPath } from "node:url"; +import { expect } from "vitest"; +import { processTest } from "../../support/process/processFixture.js"; + +processTest.for([3, 130_000])( + "completes an authenticated inventory with %i child occurrences on the server stack", + async (count, { processes }) => { + // Thread-pool tests have a larger stack than the stdio server. Use a fresh + // main thread and enough heap for the ledger's conservative admission. + const result = await processes.run(process.execPath, [ + "--max-old-space-size=4096", + "--import", + "tsx", + fileURLToPath( + new URL( + "../../fixtures/promptOccurrenceCompletion.ts", + import.meta.url, + ), + ), + String(count), + ]); + expect(result.exitCode, result.stderr).toBe(0); + expect(JSON.parse(result.stdout)).toMatchObject({ + values: Math.min(count + 1, 100), + total: count + 1, + hasMore: count + 1 > 100, + }); + }, +); diff --git a/tests/boundary/mcp/windowsShortcutMcp.test.ts b/tests/boundary/mcp/windowsShortcutMcp.test.ts index dc2fbc05b9..a4eaf83080 100644 --- a/tests/boundary/mcp/windowsShortcutMcp.test.ts +++ b/tests/boundary/mcp/windowsShortcutMcp.test.ts @@ -29,11 +29,10 @@ mcpTest( createServer({ kind: "session", session }), ); try { - const { - toolDefinition: advertised, - ajv, - outputSchema, - } = await advertisedMcpToolSchemas(client, "inspect_windows_shortcut"); + const { toolDefinition: advertised } = await advertisedMcpToolSchemas( + client, + "inspect_windows_shortcut", + ); expect(advertised?.annotations).toMatchObject({ readOnlyHint: false, idempotentHint: true, @@ -41,15 +40,11 @@ mcpTest( const root = await createTestTempDirectory("rea-windows-shortcut-mcp-"); const path = join(root, "shortcut.lnk"); await writeFile(path, powershellHiddenIconSpoofShortcut()); - const response = await client.callTool({ - name: "inspect_windows_shortcut", - arguments: { path }, - }); + const response = await client.callTool( + { name: "inspect_windows_shortcut", arguments: { path } }, + { toolDefinition: advertised }, + ); expect(response.isError, JSON.stringify(response.content)).not.toBe(true); - expect( - ajv.validate(outputSchema, response.structuredContent), - JSON.stringify(ajv.errors), - ).toBe(true); const evidence = shortcutContract.outputSchema.parse( response.structuredContent, ); diff --git a/tests/boundary/mcp/wxapkgMcp.test.ts b/tests/boundary/mcp/wxapkgMcp.test.ts index 3d1901421a..3e655e2870 100644 --- a/tests/boundary/mcp/wxapkgMcp.test.ts +++ b/tests/boundary/mcp/wxapkgMcp.test.ts @@ -83,5 +83,4 @@ it.each(["full", "compact"] as const)( await rm(outputRoot, { recursive: true, force: true }); } }, - 60_000, ); diff --git a/tests/boundary/native/pwndbgReplyFinalization.test.ts b/tests/boundary/native/pwndbgReplyFinalization.test.ts index 833cd161a5..cee654ca00 100644 --- a/tests/boundary/native/pwndbgReplyFinalization.test.ts +++ b/tests/boundary/native/pwndbgReplyFinalization.test.ts @@ -2,7 +2,6 @@ import { createHash } from "node:crypto"; import { resolvePwntoolsLimits } from "../../../src/domain/native/pwntoolsResourceLimits.js"; import { mkdir, writeFile } from "node:fs/promises"; import type { FileHandle } from "node:fs/promises"; -import { join } from "node:path"; import { expect, it, onTestFinished, vi } from "vitest"; const replyClose = vi.hoisted(() => ({ diff --git a/tests/boundary/pcap/pcapCommand.test.ts b/tests/boundary/pcap/pcapCommand.test.ts index 30aeaf9bce..f165b1caf6 100644 --- a/tests/boundary/pcap/pcapCommand.test.ts +++ b/tests/boundary/pcap/pcapCommand.test.ts @@ -18,10 +18,9 @@ it.skipIf(process.platform !== "darwin")( const controller = new AbortController(); const reason = new ProviderCleanupError("caller", ["foreign-resource"], {}); const prepare = vi - .spyOn(ownership, "prepareProcessOwnershipInspection") - .mockImplementationOnce(async (signal) => { + .spyOn(ownership.systemProcessOwnershipHost, "prepare") + .mockImplementationOnce(async () => { controller.abort(reason); - signal?.throwIfAborted(); }); onTestFinished(() => prepare.mockRestore()); const launcher = vi.fn(async () => { diff --git a/tests/boundary/pcap/pcapCredentialErrors.test.ts b/tests/boundary/pcap/pcapCredentialErrors.test.ts index 9f57a47ecb..0f151d4298 100644 --- a/tests/boundary/pcap/pcapCredentialErrors.test.ts +++ b/tests/boundary/pcap/pcapCredentialErrors.test.ts @@ -66,7 +66,7 @@ it("keeps complete parser input while exposing safe late-failure diagnostics", a exit_code: 0, truncated: false, }); -}, 20_000); +}); it("preserves producer failure facts while excluding HTTP credentials", async () => { const failure: unknown = await ownedCommand( @@ -82,7 +82,7 @@ it("preserves producer failure facts while excluding HTTP credentials", async () credential, ); expect(failure.capturedOutput?.stdout).toContain("http://caller:keep@label"); -}, 20_000); +}); it("excludes HTTP credentials after output-observed cancellation and preserves its reason", async () => { const controller = new AbortController(); @@ -110,7 +110,7 @@ it("excludes HTTP credentials after output-observed cancellation and preserves i expect(JSON.stringify(projectAnalysisError(failure))).not.toContain( credential, ); -}, 20_000); +}); it("retains safe cleanup observations and retries their original owner", async () => { let mayClean = false; @@ -154,7 +154,7 @@ it("retains safe cleanup observations and retries their original owner", async ( expect(output.cleanup?.capturedOutput.stdout).toContain( "http://caller:keep@label", ); -}, 20_000); +}); const nativeOptions = ( path: string, @@ -224,7 +224,6 @@ it.skipIf(process.platform !== "linux" || !existsSync("/usr/bin/tshark"))( credential, ); }, - 20_000, ); it.skipIf(process.platform !== "linux" || !existsSync("/usr/bin/tshark"))( @@ -250,5 +249,4 @@ it.skipIf(process.platform !== "linux" || !existsSync("/usr/bin/tshark"))( expect(frames[0]?._source.layers["http.request.uri"]).toEqual([target]); expect(result.capturedOutput.truncated).toBe(false); }, - 20_000, ); diff --git a/tests/boundary/pcap/pcapProvider.test.ts b/tests/boundary/pcap/pcapProvider.test.ts index c491841c34..d16118ac6f 100644 --- a/tests/boundary/pcap/pcapProvider.test.ts +++ b/tests/boundary/pcap/pcapProvider.test.ts @@ -129,6 +129,29 @@ it("inspects a capture with endpoints, DNS, TLS SNI, and HTTP", async () => { expect(result.coverage).toBe("complete"); }); +it("retains DNS answers and exposes declared truncation as partial coverage", async () => { + const { provider, root } = await providerWith({ + dnsJson: TSHARK_DNS_JSON.replace("00018180", "00018380"), + }); + const result = await provider.execute({ + operation: "inspect_pcap_capture", + input: { path: await captureFixture(root) }, + }); + if (!result.ok) throw new Error(result.error.message); + expect(result.value.result).toMatchObject({ + coverage: "partial", + dns_queries: [ + { query: "api.example.com", response_addresses: ["93.184.216.34"] }, + ], + }); + expect(result.value.limitations).toContain( + "DNS wire parsing: DNS message declares truncation (TC); captured records are incomplete", + ); + expect(result.value.limitations).toContainEqual( + expect.stringContaining("DNS messages declare truncation"), + ); +}); + it.each([0, 1])( "preserves packet-count mismatch facts while accepting an empty capture: capinfos=%s", async (count) => { diff --git a/tests/boundary/pcap/pcapRetainedView.test.ts b/tests/boundary/pcap/pcapRetainedView.test.ts index 6f165bd364..0ee5a20925 100644 --- a/tests/boundary/pcap/pcapRetainedView.test.ts +++ b/tests/boundary/pcap/pcapRetainedView.test.ts @@ -212,5 +212,4 @@ it.runIf(process.platform === "linux")( ), ).toMatchObject({ error: { code: "invalid_request" } }); }, - 60_000, ); diff --git a/tests/boundary/process/androidFixtureBootstrap.test.ts b/tests/boundary/process/androidFixtureBootstrap.test.ts index 215dadf52c..40bb7a1228 100644 --- a/tests/boundary/process/androidFixtureBootstrap.test.ts +++ b/tests/boundary/process/androidFixtureBootstrap.test.ts @@ -26,11 +26,11 @@ processTest( const entry = join(root, "scripts/fixtures/android/fetch.mjs"); // Invalid arguments stop before downloads while exercising Node's real // source-module loading, without Vitest's .js-to-.ts resolution. - const result = await processes.run( - process.execPath, - [entry, "extra", "argument"], - { timeoutMs: 10000 }, - ); + const result = await processes.run(process.execPath, [ + entry, + "extra", + "argument", + ]); expect(result.exitCode).toBe(1); expect(result.stderr).toContain( "Usage: npm run fixtures:android -- [DIRECTORY]", diff --git a/tests/boundary/process/applicationWorkflowVerifier.test.ts b/tests/boundary/process/applicationWorkflowVerifier.test.ts index b94551bbe2..bf984f78c1 100644 --- a/tests/boundary/process/applicationWorkflowVerifier.test.ts +++ b/tests/boundary/process/applicationWorkflowVerifier.test.ts @@ -57,7 +57,7 @@ describe("local application workflow verifier", () => { coverage: { status: expect.any(String) }, trace: { summary: { matched_seeds: expect.any(Number) } }, }); - }, 20_000); + }); it("reports an added parser return field", async () => { const root = await createTestTempDirectory("rea-shape-verifier-"); @@ -105,7 +105,7 @@ describe("local application workflow verifier", () => { }), ]), }); - }, 20_000); + }); }); it.each([false, true])( @@ -167,5 +167,4 @@ registerHooks({ resolve(specifier, context, next) { stderr: expect.stringContaining("authored late cleanup failure"), }); }, - 20_000, ); diff --git a/tests/boundary/process/artifactProducerCancellation.test.ts b/tests/boundary/process/artifactProducerCancellation.test.ts index 55c808550c..00baf2b7a5 100644 --- a/tests/boundary/process/artifactProducerCancellation.test.ts +++ b/tests/boundary/process/artifactProducerCancellation.test.ts @@ -13,7 +13,6 @@ import { OwnedFileHandle } from "../../../src/filesystem/OwnedFileHandle.js"; import { readStableArtifact } from "../../../src/artifacts/readStableArtifact.js"; import { ArtifactReaderFailure } from "../../../src/artifacts/ArtifactReader.js"; import { ProviderCleanupError } from "../../../src/domain/providerCleanupError.js"; -import { AnalysisError } from "../../../src/domain/analysisErrorBase.js"; import type { ExecutionOptions } from "../../../src/application/AnalysisProvider.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; diff --git a/tests/boundary/process/browserRequestVerifierOwnership.test.ts b/tests/boundary/process/browserRequestVerifierOwnership.test.ts index a51805cbb2..7797e1e34e 100644 --- a/tests/boundary/process/browserRequestVerifierOwnership.test.ts +++ b/tests/boundary/process/browserRequestVerifierOwnership.test.ts @@ -93,7 +93,6 @@ for (const { name, module, verify, rootPrefix } of [ ]) { processTest.skipIf(process.platform === "win32")( `stops a failed capture's CLI and browser before removing the ${name} lane's root`, - { timeout: 40_000 }, async ({ processes }) => { const root = await createTestTempDirectory(`rea-browser-${name}-owner-`); const tmp = join(root, "tmp"); @@ -140,7 +139,6 @@ process.stdout.write(JSON.stringify({ pids, live, roots, failure })); ); try { const result = await processes.run(process.execPath, [driver], { - timeoutMs: 30_000, env: { ...process.env, TMPDIR: tmp, diff --git a/tests/boundary/process/darwinProcessRunTokenReader.test.ts b/tests/boundary/process/darwinProcessRunTokenReader.test.ts index 0fae8ba918..cb82a4d162 100644 --- a/tests/boundary/process/darwinProcessRunTokenReader.test.ts +++ b/tests/boundary/process/darwinProcessRunTokenReader.test.ts @@ -1269,6 +1269,7 @@ it.skipIf(process.platform === "win32")( it.skipIf(!onDarwin)( "parses exact NUL-delimited token records and rejects truncated argv", + { tags: ["long-running"] }, async () => { const root = await mkdtemp( join(tmpdir(), "rea-process-token-parser-test-"), @@ -1397,11 +1398,11 @@ it.skipIf(!onDarwin)( await rm(root, { recursive: true, force: true }); } }, - 150_000, ); it.skipIf(!onDarwin)( "retries a growing process table after ENOMEM and preserves other sysctl errors", + { tags: ["long-running"] }, async () => { const root = await mkdtemp( join(tmpdir(), "rea-process-snapshot-retry-test-"), @@ -1444,7 +1445,6 @@ it.skipIf(!onDarwin)( await rm(root, { recursive: true, force: true }); } }, - 150_000, ); it.skipIf(!onDarwin)( @@ -1512,6 +1512,7 @@ it.skipIf(!onDarwin)( it.skipIf(!onDarwin)( "keeps a long title overwrite unknown when it hides the child's token", + { tags: ["long-running"] }, async () => { const host = createSystemProcessOwnershipHost("darwin"); const children: ChildProcess[] = []; @@ -1580,7 +1581,6 @@ it.skipIf(!onDarwin)( } } }, - 150_000, ); it.skipIf(!onDarwin)( diff --git a/tests/boundary/process/desktopBuildOwnedCommand.test.ts b/tests/boundary/process/desktopBuildOwnedCommand.test.ts index 8aa3a89806..30c229c306 100644 --- a/tests/boundary/process/desktopBuildOwnedCommand.test.ts +++ b/tests/boundary/process/desktopBuildOwnedCommand.test.ts @@ -9,7 +9,6 @@ import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; it.skipIf(process.platform === "win32")( "stops the packing process family before removing its partial archive on timeout", - { timeout: 15_000 }, async () => { await prepareProcessOwnershipInspection(); const directory = await createTestTempDirectory("rea-owned-pack-"); @@ -56,7 +55,6 @@ it.skipIf(process.platform === "win32")( it.skipIf(process.platform === "win32")( "cancels a running setup process family before removing its partial output", - { timeout: 15_000 }, async () => { await prepareProcessOwnershipInspection(); const directory = await createTestTempDirectory("rea-owned-pack-cancel-"); diff --git a/tests/boundary/process/domCallbackFailures.test.ts b/tests/boundary/process/domCallbackFailures.test.ts index b069057287..8c13c53ad7 100644 --- a/tests/boundary/process/domCallbackFailures.test.ts +++ b/tests/boundary/process/domCallbackFailures.test.ts @@ -88,16 +88,12 @@ for (const scenario of ${JSON.stringify(cases)}) { } } `; - const result = await processes.run( - process.execPath, - [ - "--unhandled-rejections=strict", - "--input-type=module", - "--eval", - source, - ], - { timeoutMs: 10_000 }, - ); + const result = await processes.run(process.execPath, [ + "--unhandled-rejections=strict", + "--input-type=module", + "--eval", + source, + ]); expect(result.signal, result.stderr).toBeNull(); expect(result.exitCode, result.stderr).toBe(0); }, @@ -193,17 +189,13 @@ for (const [directory, fails] of ${JSON.stringify([ } } `; - const result = await processes.run( - process.execPath, - [ - "--experimental-vm-modules", - "--unhandled-rejections=strict", - "--input-type=module", - "--eval", - source, - ], - { timeoutMs: 10_000 }, - ); + const result = await processes.run(process.execPath, [ + "--experimental-vm-modules", + "--unhandled-rejections=strict", + "--input-type=module", + "--eval", + source, + ]); expect(result.signal, result.stderr).toBeNull(); expect(result.exitCode, result.stderr).toBe(0); }, diff --git a/tests/boundary/process/electronLaunchRejections.test.ts b/tests/boundary/process/electronLaunchRejections.test.ts index 7c4b0c47f6..9e9c8981f4 100644 --- a/tests/boundary/process/electronLaunchRejections.test.ts +++ b/tests/boundary/process/electronLaunchRejections.test.ts @@ -1,7 +1,7 @@ import { chmod, mkdir, writeFile } from "node:fs/promises"; import { join } from "node:path"; -import { expect, vi } from "vitest"; +import { expect } from "vitest"; import type { AnalysisErrorProjection } from "../../../src/domain/analysisErrorProjection.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; @@ -123,7 +123,7 @@ console.log(JSON.stringify({ // A cold macOS Swift toolchain may spend up to 120 seconds building the // ownership reader before any child launch reaches its assertion. -vi.setConfig({ testTimeout: 180_000 }); +// @module-tag long-running type Outcome = { readonly before: number; @@ -140,16 +140,12 @@ const run = async ( executable = "/not/launched", root = "/", ) => - processes.run( - process.execPath, - [ - "--input-type=module", - "--eval", - script, - JSON.stringify([scenario, executable, root]), - ], - { timeoutMs: 150_000 }, - ); + processes.run(process.execPath, [ + "--input-type=module", + "--eval", + script, + JSON.stringify([scenario, executable, root]), + ]); const outcome = (result: { readonly stdout: string }): Outcome => JSON.parse(result.stdout.trim().split("\n").at(-1) ?? "") as Outcome; diff --git a/tests/boundary/process/environmentServeShutdown.test.ts b/tests/boundary/process/environmentServeShutdown.test.ts index fd06617d84..85b3a1c7d8 100644 --- a/tests/boundary/process/environmentServeShutdown.test.ts +++ b/tests/boundary/process/environmentServeShutdown.test.ts @@ -17,8 +17,6 @@ import { rawHttpRequest } from "../../support/http/rawRequest.js"; import { startListeningProcess } from "../../support/mcp/streamableHttpProcess.js"; import { processTest } from "../../support/process/processFixture.js"; -const SHUTDOWN_TIMEOUT_MS = 90_000; - /** How each case is served and what its stopped document reports about the state. */ const MODES = [ { @@ -114,7 +112,6 @@ describe("rea environment-serve shutdown", () => { code: "ECONNREFUSED", }); }, - SHUTDOWN_TIMEOUT_MS, ); }); @@ -230,7 +227,6 @@ syncBuiltinESMExports(); }); expect(stderr).not.toContain("MCP transport listening"); }, - SHUTDOWN_TIMEOUT_MS, ); } }, diff --git a/tests/boundary/process/fixtureAsyncFailures.test.ts b/tests/boundary/process/fixtureAsyncFailures.test.ts index 0ffff41141..11a601c810 100644 --- a/tests/boundary/process/fixtureAsyncFailures.test.ts +++ b/tests/boundary/process/fixtureAsyncFailures.test.ts @@ -25,11 +25,13 @@ for (const blocked of [false, true]) { const handoff = `${receipt}.document-created`; await writeFile(mode, "denied-after-handoff"); if (blocked) await mkdir(handoff); - const result = await processes.run( - process.execPath, - [hopper, mode, receipt, "-Y", "controlled-bootstrap"], - { timeoutMs: 5_000 }, - ); + const result = await processes.run(process.execPath, [ + hopper, + mode, + receipt, + "-Y", + "controlled-bootstrap", + ]); expect(result.signal, result.stderr).toBeNull(); expect(result.exitCode, result.stderr).toBe(blocked ? 1 : 2); expect(result.stderr).toContain("Not authorized to send Apple events"); @@ -54,7 +56,6 @@ processTest( const pending = `${endpointPath}.pending`; await writeFile(pending, "previous selected evidence"); const result = await processes.run(process.execPath, [ghidra], { - timeoutMs: 5_000, env: { ...process.env, REA_GHIDRA_FIXTURE_CONFIG: JSON.stringify({ diff --git a/tests/boundary/process/ghidraDecompilerVerifierCleanup.test.ts b/tests/boundary/process/ghidraDecompilerVerifierCleanup.test.ts index 7e181f9880..fa7774d7c7 100644 --- a/tests/boundary/process/ghidraDecompilerVerifierCleanup.test.ts +++ b/tests/boundary/process/ghidraDecompilerVerifierCleanup.test.ts @@ -82,7 +82,6 @@ processTest TMP: temporary, TEMP: temporary, }, - timeoutMs: 15_000, }, ); expect(result.exitCode, result.stderr).toBe(1); diff --git a/tests/boundary/process/ghidraVerifierAssertions.test.ts b/tests/boundary/process/ghidraVerifierAssertions.test.ts index 6f8aebe956..6de924acce 100644 --- a/tests/boundary/process/ghidraVerifierAssertions.test.ts +++ b/tests/boundary/process/ghidraVerifierAssertions.test.ts @@ -57,11 +57,11 @@ processTest( /Ghidra session commitment drifted/, ); `; - const result = await processes.run( - process.execPath, - ["--input-type=module", "--eval", source], - { timeoutMs: 10_000 }, - ); + const result = await processes.run(process.execPath, [ + "--input-type=module", + "--eval", + source, + ]); expect(result.signal, result.stderr).toBeNull(); expect(result.exitCode, result.stderr).toBe(0); }, @@ -102,11 +102,11 @@ processTest( result = "EACCES"; await assert.rejects(assertCleanup(coordinates), { code: "EACCES" }); `; - const result = await processes.run( - process.execPath, - ["--input-type=module", "--eval", source], - { timeoutMs: 10_000 }, - ); + const result = await processes.run(process.execPath, [ + "--input-type=module", + "--eval", + source, + ]); expect(result.signal, result.stderr).toBeNull(); expect(result.exitCode, result.stderr).toBe(0); }, diff --git a/tests/boundary/process/goVerifierLifetime.test.ts b/tests/boundary/process/goVerifierLifetime.test.ts new file mode 100644 index 0000000000..b8e1bd6db8 --- /dev/null +++ b/tests/boundary/process/goVerifierLifetime.test.ts @@ -0,0 +1,83 @@ +import { mkdir } from "node:fs/promises"; +import { join } from "node:path"; +import { fileURLToPath } from "node:url"; +import { expect } from "vitest"; +import { z } from "zod"; + +import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; +import { processTest } from "../../support/process/processFixture.js"; + +const harness = fileURLToPath( + new URL("../../fixtures/goVerifierLifetime.mjs", import.meta.url), +); +const outcomeSchema = z.object({ + messages: z.array(z.string()), + retained: z.string().nullable(), + existedAtFailure: z.boolean(), + removedAfterRetry: z.boolean(), + rootRemoved: z.boolean(), + beforeRetry: z.array(z.string()), + events: z.array(z.string()), +}); + +processTest + .skipIf(process.platform === "win32") + .for([ + "settled", + "client-constructor", + "transport-constructor", + "handshake", + "client-close", + "transport-close", + "client-close+transport-close", + "commands-close", + "root-close", + ])( + "owns the Go verifier fixture through %s and cleanup retry", + async (scenario, { processes }) => { + const directory = await createTestTempDirectory("rea-go-lifetime-"); + const temporary = join(directory, "temporary"); + await mkdir(temporary); + const result = await processes.run( + process.execPath, + [harness, scenario, directory], + { + env: { + ...process.env, + TMPDIR: temporary, + TMP: temporary, + TEMP: temporary, + }, + }, + ); + expect(result.exitCode, result.stderr + result.stdout).toBe(0); + const outcome = outcomeSchema.parse(JSON.parse(result.stdout)); + const incomplete = scenario.includes("close"); + if (incomplete) { + expect(outcome.retained).not.toBeNull(); + expect(outcome.existedAtFailure).toBe(true); + if (scenario !== "root-close") + expect(outcome.beforeRetry).not.toContain("root-close"); + expect(outcome.messages[0]).toMatch(/ENOENT.*linux-amd64/u); + } else { + expect(outcome.retained).toBeNull(); + expect(outcome.messages[0]).toMatch(/fault|ENOENT.*linux-amd64/u); + } + expect(outcome.removedAfterRetry).toBe(true); + expect(outcome.rootRemoved).toBe(true); + if (!scenario.includes("constructor")) { + expect(outcome.events.indexOf("transport-close")).toBeGreaterThan( + outcome.events.indexOf("transport-start"), + ); + expect( + outcome.events.lastIndexOf("transport-settled"), + ).toBeGreaterThanOrEqual(0); + expect(outcome.events.lastIndexOf("root-close")).toBeGreaterThan( + outcome.events.lastIndexOf("transport-settled"), + ); + } + for (const name of ["client", "transport", "commands", "root"]) + if (scenario.includes(`${name}-close`)) + expect(outcome.messages).toContain(`${name} close fault`); + }, +); diff --git a/tests/boundary/process/javascriptRuntimeReconciliationAmbiguity.test.ts b/tests/boundary/process/javascriptRuntimeReconciliationAmbiguity.test.ts index 8bbc75c84b..5600ff9867 100644 --- a/tests/boundary/process/javascriptRuntimeReconciliationAmbiguity.test.ts +++ b/tests/boundary/process/javascriptRuntimeReconciliationAmbiguity.test.ts @@ -217,4 +217,4 @@ it("runs the local verifier from operator-provided paths without emitting source summary: { runtime_scripts: 1 }, }); expect(stdout).not.toContain(SOURCE.trim()); -}, 10_000); +}); diff --git a/tests/boundary/process/javascriptSourceModes.test.ts b/tests/boundary/process/javascriptSourceModes.test.ts index 64c15a2d84..d3b9df2caa 100644 --- a/tests/boundary/process/javascriptSourceModes.test.ts +++ b/tests/boundary/process/javascriptSourceModes.test.ts @@ -15,13 +15,13 @@ for (const source of [ const commonjs = await processes.run( process.execPath, ["--check", "--input-type=commonjs"], - { input: source, timeoutMs: 5_000 }, + { input: source }, ); expect(commonjs.exitCode).toBe(0); const module = await processes.run( process.execPath, ["--check", "--input-type=module"], - { input: source, timeoutMs: 5_000 }, + { input: source }, ); expect(module.exitCode).not.toBe(0); diff --git a/tests/boundary/process/javascriptVerifierBoundedEvidence.test.ts b/tests/boundary/process/javascriptVerifierBoundedEvidence.test.ts index 1fe3fb7320..6837842304 100644 --- a/tests/boundary/process/javascriptVerifierBoundedEvidence.test.ts +++ b/tests/boundary/process/javascriptVerifierBoundedEvidence.test.ts @@ -100,7 +100,6 @@ process.on('exit', () => fs.writeSync(2, 'Evidence close attempts: ' + attempts ); } }, - 30_000, ); it("refuses oversized Evidence before a whole-file read", async () => { @@ -147,4 +146,4 @@ syncBuiltinESMExports(); `Runtime Evidence exceeds 64 MiB: ${evidence}`, ), }); -}, 30_000); +}); diff --git a/tests/boundary/process/javascriptVerifierSelectedPaths.test.ts b/tests/boundary/process/javascriptVerifierSelectedPaths.test.ts index 19b4e51755..0cf90aa477 100644 --- a/tests/boundary/process/javascriptVerifierSelectedPaths.test.ts +++ b/tests/boundary/process/javascriptVerifierSelectedPaths.test.ts @@ -83,7 +83,7 @@ it("retains selected layer paths in runtime verifier parent Evidence", async () verified: true, layers: [{ evidence_id: expected.evidence_id }], }); -}, 20_000); +}); it("retains workflow selections while rejecting aliases of the same target", async () => { const left = await selectedFixture(); @@ -117,4 +117,4 @@ it("retains workflow selections while rejecting aliases of the same target", asy ).rejects.toMatchObject({ stderr: expect.stringContaining("must resolve to distinct paths"), }); -}, 20_000); +}); diff --git a/tests/boundary/process/mcpHttpShutdown.test.ts b/tests/boundary/process/mcpHttpShutdown.test.ts index fd13e0be06..1acdfda193 100644 --- a/tests/boundary/process/mcpHttpShutdown.test.ts +++ b/tests/boundary/process/mcpHttpShutdown.test.ts @@ -6,8 +6,6 @@ import { rawHttpRequest } from "../../support/http/rawRequest.js"; import { startMcpHttpProcess } from "../../support/mcp/streamableHttpProcess.js"; import { processTest } from "../../support/process/processFixture.js"; -const SHUTDOWN_TIMEOUT_MS = 90_000; - const shutdownSignals: readonly (readonly [NodeJS.Signals, number])[] = [ ["SIGTERM", 143], ["SIGINT", 130], @@ -32,7 +30,6 @@ describe.skipIf(process.platform === "win32")("rea mcp --http shutdown", () => { code: "ECONNREFUSED", }); }, - SHUTDOWN_TIMEOUT_MS, ); processTest( @@ -47,6 +44,5 @@ describe.skipIf(process.platform === "win32")("rea mcp --http shutdown", () => { expect(signalCode).toBeNull(); expect(started.stderr()).not.toContain(MCP_SHUTDOWN_FAILED); }, - SHUTDOWN_TIMEOUT_MS, ); }); diff --git a/tests/boundary/process/nativeVerifierCommandOwnership.test.ts b/tests/boundary/process/nativeVerifierCommandOwnership.test.ts index fd1ffdc60b..d670fa0492 100644 --- a/tests/boundary/process/nativeVerifierCommandOwnership.test.ts +++ b/tests/boundary/process/nativeVerifierCommandOwnership.test.ts @@ -231,5 +231,4 @@ it.skipIf(process.platform === "win32")( await removeTestWorkspace(workspace.root); } }, - 30_000, ); diff --git a/tests/boundary/process/nativeVerifierPreflightOwnership.test.ts b/tests/boundary/process/nativeVerifierPreflightOwnership.test.ts index 22785b59ce..da3a3c0fb3 100644 --- a/tests/boundary/process/nativeVerifierPreflightOwnership.test.ts +++ b/tests/boundary/process/nativeVerifierPreflightOwnership.test.ts @@ -268,5 +268,4 @@ it.skipIf(process.platform === "win32").each(clients)( await removeTestWorkspace(workspace.root); } }, - 30_000, ); diff --git a/tests/boundary/process/numericLimitGuardPermission.test.ts b/tests/boundary/process/numericLimitGuardPermission.test.ts index 58f01d3bad..825b039c3b 100644 --- a/tests/boundary/process/numericLimitGuardPermission.test.ts +++ b/tests/boundary/process/numericLimitGuardPermission.test.ts @@ -9,7 +9,6 @@ processTest.skipIf( (typeof process.getuid === "function" && process.getuid() === 0), )( "fails the numeric limit guard when a required scan directory is unreadable", - { timeout: 60_000 }, async ({ processes }) => { const root = await createTestTempDirectory("rea-limit-permission-"); await mkdir(join(root, "scripts/verify"), { recursive: true }); @@ -33,7 +32,7 @@ processTest.skipIf( processes.run( process.execPath, ["scripts/verify/rea-numeric-limits.mjs"], - { cwd: root, timeoutMs: 20_000 }, + { cwd: root }, ); const healthy = await run(); expect(healthy.exitCode, healthy.stderr).toBe(0); diff --git a/tests/boundary/process/packageNpmInvocation.test.ts b/tests/boundary/process/packageNpmInvocation.test.ts index f5150975e5..ec1e5b37bf 100644 --- a/tests/boundary/process/packageNpmInvocation.test.ts +++ b/tests/boundary/process/packageNpmInvocation.test.ts @@ -39,7 +39,7 @@ processTest( const result = await processes.run( process.execPath, ["--input-type=module", "--eval", program], - { windowsHide: true, timeoutMs: 20_000 }, + { windowsHide: true }, ); expect(result.exitCode, result.stderr).toBe(0); expect(result.signal, result.stderr).toBeNull(); @@ -95,7 +95,7 @@ windowsIt( const result = await processes.run( node, ["--input-type=module", "--eval", program], - { env: environment, windowsHide: true, timeoutMs: 20_000 }, + { env: environment, windowsHide: true }, ); expect(result.exitCode, result.stderr).toBe(0); expect(result.signal, result.stderr).toBeNull(); diff --git a/tests/boundary/process/packageSetupTerminalOwnership.test.ts b/tests/boundary/process/packageSetupTerminalOwnership.test.ts index f04c2c4b2b..2398f37d38 100644 --- a/tests/boundary/process/packageSetupTerminalOwnership.test.ts +++ b/tests/boundary/process/packageSetupTerminalOwnership.test.ts @@ -113,7 +113,6 @@ const releaseStandin = async (release: string, readyPath: string) => { // The PTY, deadline, process census, command owner and workspace removal are real. processTest.skipIf(process.platform === "win32")( "joins a redirected-stdout setup descendant before removing its verifier workspace", - { timeout: 35_000 }, async ({ processes }) => { const root = await createTestTempDirectory("rea-package-setup-terminal-"); const scratch = join(root, "scratch"); @@ -262,7 +261,6 @@ processTest .skipIf(process.platform === "win32") .each(["interactive", "redirected"] as const)( "preserves selected configuration and closes the real PTY for %s setup", - { timeout: 30_000 }, async (mode) => { const root = await createTestTempDirectory("rea-package-setup-success-"); const scratch = join(root, "scratch"); diff --git a/tests/boundary/process/packageUpdateRegistryFailure.test.ts b/tests/boundary/process/packageUpdateRegistryFailure.test.ts index aef11c0641..03ddc0bd25 100644 --- a/tests/boundary/process/packageUpdateRegistryFailure.test.ts +++ b/tests/boundary/process/packageUpdateRegistryFailure.test.ts @@ -134,4 +134,4 @@ process.stdout.write(JSON.stringify({ failure, observedPids: pids.length, livePi observedPids: 2, livePids: [], }); -}, 45_000); +}); diff --git a/tests/boundary/process/packageVerifierCommandOwnership.test.ts b/tests/boundary/process/packageVerifierCommandOwnership.test.ts index c4a3a64296..c0d5ab8d3d 100644 --- a/tests/boundary/process/packageVerifierCommandOwnership.test.ts +++ b/tests/boundary/process/packageVerifierCommandOwnership.test.ts @@ -129,7 +129,6 @@ const releaseDescendant = async (release: string, receipt: string) => { // install, native engine, or WABT behavior is simulated or claimed. processTest.skipIf(process.platform === "win32")( "joins a timed-out npm entry and its acknowledged descendant before removing the verifier root", - { timeout: 30_000 }, async ({ processes }) => { const root = await createTestTempDirectory("rea-package-command-owner-"); const scratch = join(root, "scratch"); diff --git a/tests/boundary/process/packagedWasmVerifierOwnership.test.ts b/tests/boundary/process/packagedWasmVerifierOwnership.test.ts index 837d0e1b2a..1ffd9a31c7 100644 --- a/tests/boundary/process/packagedWasmVerifierOwnership.test.ts +++ b/tests/boundary/process/packagedWasmVerifierOwnership.test.ts @@ -373,5 +373,4 @@ it.skipIf(process.platform === "win32").each([ await removeTestWorkspace(workspace.root); } }, - 45_000, ); diff --git a/tests/boundary/process/processCaptureLifecycle.test.ts b/tests/boundary/process/processCaptureLifecycle.test.ts index ef54a0e133..c34b01a4c1 100644 --- a/tests/boundary/process/processCaptureLifecycle.test.ts +++ b/tests/boundary/process/processCaptureLifecycle.test.ts @@ -24,6 +24,7 @@ import { snapshotRoots } from "../../../src/process/capture/FilesystemSnapshot.j import { ProcessCaptureError } from "../../../src/process/capture/ProcessCaptureError.js"; import { expectUnverifiedHostCleanup } from "../../support/hostCleanup.js"; import { + PROCESS_CAPTURE_CLEANUP_INTERVENTION_UNKNOWN, ProcessCaptureResourceScope, type ProcessCaptureCleanupHost, observeLaunchedExecutable, @@ -184,7 +185,7 @@ itWithCaptureCapability( onTestFinished(async () => { now.mockRestore(); await resources.close(); - }, 10_000); + }); try { const result = await captureProcessScenario( processScenarioSchema.parse({ @@ -313,10 +314,15 @@ itWithCaptureCapability( ); if (cleanupIncomplete) expect(capture.settlement.cleanup_outcome).toBe("failed"); - else + else { expect(result.ok && result.value.cleanup.owned_process_group).toBe( "verified", ); + expect(capture.settlement.cleanup_outcome).toBe("cleaned"); + expect(capture.residual_unknowns).toContainEqual( + PROCESS_CAPTURE_CLEANUP_INTERVENTION_UNKNOWN, + ); + } await waitForProcessExit(ownedPid); const unownedState = await processState(unownedPid); expect(unownedState).toBeDefined(); diff --git a/tests/boundary/process/processCaptureProgress.test.ts b/tests/boundary/process/processCaptureProgress.test.ts index c127d90c5c..ad41e7eb02 100644 --- a/tests/boundary/process/processCaptureProgress.test.ts +++ b/tests/boundary/process/processCaptureProgress.test.ts @@ -335,19 +335,16 @@ itWithCaptureCapability( code: "process_capture_failed", details: { execution_failure: expect.stringContaining(missingTempRoot) }, }); - // With caller-owned compilation, Darwin fails preparing its inspector - // before acquiring capture resources. - // Other hosts reach the capture root allocation, whose failure is accounted. - const cleanupState = - process.platform === "darwin" ? "unverified" : "not_required"; + // Inspector preparation or temporary-root allocation failed before any + // capture resources were acquired, so no capture cleanup is required. expect(capture.lines.at(-1)?.update).toMatchObject({ phase: "cleanup", terminal: true, details: { disposition: "failed", - owned_process_group: cleanupState, - terminal_renderer: cleanupState, - temporary_root: cleanupState, + owned_process_group: "not_required", + terminal_renderer: "not_required", + temporary_root: "not_required", }, }); }, diff --git a/tests/boundary/process/processCleanupRetryOwnership.test.ts b/tests/boundary/process/processCleanupRetryOwnership.test.ts index d87df69072..2a4644eb56 100644 --- a/tests/boundary/process/processCleanupRetryOwnership.test.ts +++ b/tests/boundary/process/processCleanupRetryOwnership.test.ts @@ -4,6 +4,7 @@ import { join } from "node:path"; import { setImmediate } from "node:timers/promises"; import { expect, it } from "vitest"; import { createDeferred } from "../../fixtures/binarySession.js"; +import { awaitAcknowledgement } from "../../fixtures/lifecycleAcknowledgement.js"; import { projectAnalysisError } from "../../../src/domain/analysisErrorProjection.js"; import { ProcessCaptureError } from "../../../src/process/capture/ProcessCaptureError.js"; @@ -203,20 +204,24 @@ it("joins an already-started release when shutdown begins", async () => { await rm(path, { recursive: true }); }), ); - await entered.promise; let closed = false; - const closing = scope.close().then(() => { - closed = true; - }); + let closing: Promise | undefined; try { + await awaitAcknowledgement(entered.promise, releasing, "resource release"); + closing = scope.close().then(() => { + closed = true; + }); await setImmediate(); expect(closed).toBe(false); await expect(stat(root)).resolves.toBeDefined(); } finally { unblock.resolve(); - await Promise.all([releasing, closing]); + await Promise.allSettled([releasing, ...(closing ? [closing] : [])]); + await scope.close(); await rm(root, { recursive: true, force: true }); } + await releasing; + await closing; expect(attempts).toBe(1); expect(closed).toBe(true); await expect(stat(root)).rejects.toMatchObject({ code: "ENOENT" }); diff --git a/tests/boundary/process/processCli.test.ts b/tests/boundary/process/processCli.test.ts index dfcb2f9025..3b24ae6f5a 100644 --- a/tests/boundary/process/processCli.test.ts +++ b/tests/boundary/process/processCli.test.ts @@ -121,7 +121,6 @@ describe("documented process CLI workflow", () => { operation: "compare_process_captures", }); }, - CLI_INTEGRATION_TIMEOUT_MS, ); }); @@ -149,9 +148,7 @@ describe("process CLI cleanup errors", () => { try { // A retained PTY can keep its CLI process alive. Read the completed // compiled command result before releasing the child and joining exit. - if (cli.sharedCliRun === undefined) - throw new Error("Expected the compiled CLI command session"); - const result = await cli.sharedCliRun({ + const result = await cli.run({ arguments: ["capture-process", scenario, "--json"], environment: { TMPDIR: root, @@ -212,7 +209,6 @@ describe("process CLI cleanup errors", () => { } } }, - CLI_INTEGRATION_TIMEOUT_MS, ); }); @@ -279,25 +275,20 @@ describe("process CLI errors", () => { true, ); }, - CLI_INTEGRATION_TIMEOUT_MS, ); - it( - "exits unsuccessfully without writing failure-shaped evidence", - async () => { - await expect( - execFileAsync( - process.execPath, - ["scripts/rea.mjs", "capture-process", "/missing/scenario.json"], - { cwd: process.cwd() }, - ), - ).rejects.toMatchObject({ - code: 1, - stdout: expect.stringContaining("category: invalid_input"), - }); - }, - CLI_INTEGRATION_TIMEOUT_MS, - ); + it("exits unsuccessfully without writing failure-shaped evidence", async () => { + await expect( + execFileAsync( + process.execPath, + ["scripts/rea.mjs", "capture-process", "/missing/scenario.json"], + { cwd: process.cwd() }, + ), + ).rejects.toMatchObject({ + code: 1, + stdout: expect.stringContaining("category: invalid_input"), + }); + }); it("rejects NUL arguments before process launch", async () => { const root = await createTestTempDirectory("rea-process-cli-"); @@ -489,43 +480,33 @@ describe("process CLI evidence validation", () => { }); }); -it( - "reports the JSON depth constraint through the compiled comparison CLI", - async () => { - const root = await createTestTempDirectory("rea-process-cli-"); - const left = join(root, "left.json"); - const right = join(root, "right.json"); - const input = INVESTIGATION_EXAMPLES.compare_process_captures.input; - await writeFile(left, JSON.stringify(input.left)); - await writeFile(right, JSON.stringify(input.right)); - const cli = fileURLToPath( - new URL("../../../scripts/rea.mjs", import.meta.url), - ); - const cliArguments = [ - cli, - "compare-process-captures", - left, - right, - "--json", - ]; - const baseline = await execFileAsync(process.execPath, cliArguments); - expect(JSON.parse(baseline.stdout)).toMatchObject({ - operation: "compare_process_captures", - }); - const nested = '{"nested":'.repeat(10_000) + "1" + "}".repeat(10_000); - await writeFile( - left, - JSON.stringify({ - ...input.left, - parameters: { attack: "depth-placeholder" }, - }).replace('"depth-placeholder"', nested), - ); - await expect( - execFileAsync(process.execPath, cliArguments), - ).rejects.toMatchObject({ - code: 1, - stdout: expect.stringContaining("maximum nesting depth"), - }); - }, - CLI_INTEGRATION_TIMEOUT_MS, -); +it("reports the JSON depth constraint through the compiled comparison CLI", async () => { + const root = await createTestTempDirectory("rea-process-cli-"); + const left = join(root, "left.json"); + const right = join(root, "right.json"); + const input = INVESTIGATION_EXAMPLES.compare_process_captures.input; + await writeFile(left, JSON.stringify(input.left)); + await writeFile(right, JSON.stringify(input.right)); + const cli = fileURLToPath( + new URL("../../../scripts/rea.mjs", import.meta.url), + ); + const cliArguments = [cli, "compare-process-captures", left, right, "--json"]; + const baseline = await execFileAsync(process.execPath, cliArguments); + expect(JSON.parse(baseline.stdout)).toMatchObject({ + operation: "compare_process_captures", + }); + const nested = '{"nested":'.repeat(10_000) + "1" + "}".repeat(10_000); + await writeFile( + left, + JSON.stringify({ + ...input.left, + parameters: { attack: "depth-placeholder" }, + }).replace('"depth-placeholder"', nested), + ); + await expect( + execFileAsync(process.execPath, cliArguments), + ).rejects.toMatchObject({ + code: 1, + stdout: expect.stringContaining("maximum nesting depth"), + }); +}); diff --git a/tests/boundary/process/processFilesystemCoverageAdapters.test.ts b/tests/boundary/process/processFilesystemCoverageAdapters.test.ts index 708e35605f..9fdd10101f 100644 --- a/tests/boundary/process/processFilesystemCoverageAdapters.test.ts +++ b/tests/boundary/process/processFilesystemCoverageAdapters.test.ts @@ -74,7 +74,6 @@ it ) .for(["cli", "mcp"] as const)( "preserves selected-root permission denial and earlier snapshot facts through %s", - { timeout: 20_000 }, async (adapter, context) => { const root = await createTestTempDirectory("rea-fs-root-denial-adapter-"); const denied = join(root, "denied"); @@ -446,7 +445,6 @@ it.skipIf(CAPTURE_SKIP_REASON || process.platform === "win32")( }); expect(compareProcessCaptures(capture, capture).filesystem).toBe("unknown"); }, - 20_000, ); it @@ -516,7 +514,6 @@ it }); expect(compareProcessCaptures(capture, capture).filesystem).toBe("unknown"); }, - 20_000, ); itWithCaptureCapability.each(["cli", "mcp"] as const)( @@ -625,7 +622,6 @@ it await chmod(join(root, "state"), 0o700); } }, - 20_000, ); itWithCaptureCapability( diff --git a/tests/boundary/process/processFixtureLifecycle.test.ts b/tests/boundary/process/processFixtureLifecycle.test.ts index 3cc810a942..2923a8ca96 100644 --- a/tests/boundary/process/processFixtureLifecycle.test.ts +++ b/tests/boundary/process/processFixtureLifecycle.test.ts @@ -6,7 +6,10 @@ import type { ChildProcessWithoutNullStreams } from "node:child_process"; import { expect, it, vi } from "vitest"; import { awaitAcknowledgement } from "../../fixtures/lifecycleAcknowledgement.js"; -import { createTestProcessRegistry } from "../../support/process/processFixture.js"; +import { + createTestProcessRegistry, + remainingProcessDeadline, +} from "../../support/process/processFixture.js"; const within = async ( operation: Promise, @@ -248,7 +251,7 @@ it("times out when a real descendant holds the exited leader's output open", asy ); } } -}, 17_000); +}); it("terminates a stalled real child at its deadline while retaining captured output", async () => { const processes = createTestProcessRegistry(); @@ -277,3 +280,40 @@ it("terminates a stalled real child at its deadline while retaining captured out await processes.shutdown(); } }); + +it("derives a child deadline from the test deadline, keeping a reporting reserve", () => { + expect(remainingProcessDeadline(120_000, 0)).toBe(108_000); + expect(remainingProcessDeadline(30_000, 20_000)).toBe(5_000); + // The reserve never drops below five seconds, nor the deadline below one. + expect(remainingProcessDeadline(8_000, 0)).toBe(3_000); + expect(remainingProcessDeadline(8_000, 7_500)).toBe(1_000); + expect(remainingProcessDeadline(0, 0)).toBeUndefined(); +}); + +it( + "stops a hung child before its running test's deadline and reports its output", + // The test deadline is the behavior under test: the child gets what remains. + { timeout: 8_000 }, + async () => { + // processTest uses this registry, so one registry covers both entry points. + const processes = createTestProcessRegistry(); + const started = performance.now(); + try { + const failure: unknown = await processes + .run(process.execPath, [ + "-e", + "process.stdout.write('started'); setInterval(() => {}, 1_000);", + ]) + .catch((error: unknown) => error); + expect(failure).toMatchObject({ + message: expect.stringMatching( + /^Test process exceeded its \d+ms deadline$/u, + ), + cause: { stdout: "started" }, + }); + expect(performance.now() - started).toBeLessThan(6_000); + } finally { + await processes.shutdown(); + } + }, +); diff --git a/tests/boundary/process/providerProcess.test.ts b/tests/boundary/process/providerProcess.test.ts index 38c6da05ac..414fdf5052 100644 --- a/tests/boundary/process/providerProcess.test.ts +++ b/tests/boundary/process/providerProcess.test.ts @@ -64,7 +64,6 @@ describe("provider process runtime and wait primitives", () => { const first = runtime.close(); const second = runtime.close(); - expect(second).toBe(first); await Promise.all([first, second]); await expect(access(runtime.path)).rejects.toMatchObject({ code: "ENOENT", @@ -283,6 +282,7 @@ describe("provider process output and cleanup primitives", () => { ownsProcessLifetime: true, }); await waitForProviderProcessReady(child); + const signal = vi.spyOn(child, "kill"); const first = supervisor.stop({ terminationGraceMs: 20, @@ -292,11 +292,13 @@ describe("provider process output and cleanup primitives", () => { terminationGraceMs: 20, killGraceMs: 500, }); - // Concurrent callers must share one escalation instead of signalling - // the same child twice. - expect(second).toBe(first); - await expect(first).resolves.toEqual({ status: "killed" }); + // Both callers observe the completed escalation. + await expect(Promise.all([first, second])).resolves.toEqual([ + { status: "killed" }, + { status: "killed" }, + ]); expect(child.signalCode).toBe("SIGKILL"); + expect(signal.mock.calls).toEqual([["SIGTERM"], ["SIGKILL"]]); }, ); diff --git a/tests/boundary/process/providerVerifierCleanup.test.ts b/tests/boundary/process/providerVerifierCleanup.test.ts index c96b3cccb2..b543d38db2 100644 --- a/tests/boundary/process/providerVerifierCleanup.test.ts +++ b/tests/boundary/process/providerVerifierCleanup.test.ts @@ -149,7 +149,6 @@ processTest.for(cases)( REA_TEST_VERIFIER_CLOSE_FAIL: closeFails ? "1" : "0", REA_TEST_VERIFIER_CLOSE_LOG: fixture.closeLog, }, - timeoutMs: 10_000, }, ); diff --git a/tests/boundary/process/publishedPackageNpmSelection.test.ts b/tests/boundary/process/publishedPackageNpmSelection.test.ts index e5881b33ea..34b32212b2 100644 --- a/tests/boundary/process/publishedPackageNpmSelection.test.ts +++ b/tests/boundary/process/publishedPackageNpmSelection.test.ts @@ -120,7 +120,7 @@ processTest.skipIf(process.platform === "win32").for([true, false])( const result = await processes.run( process.execPath, ["--import", hook, "scripts/verify-published-package.mjs", "6.9.0"], - { cwd: controller, env, timeoutMs: 20_000 }, + { cwd: controller, env }, ); expect(result.exitCode, result.stderr).toBe(0); const calls = z diff --git a/tests/boundary/process/regexMatcher.test.ts b/tests/boundary/process/regexMatcher.test.ts index dc628aeadf..4535a7a2e7 100644 --- a/tests/boundary/process/regexMatcher.test.ts +++ b/tests/boundary/process/regexMatcher.test.ts @@ -18,7 +18,6 @@ import { matchRegexValues, REGEX_MATCHING_LIMITATION, regexMatchingDeadlineError, - regexMatchingLimitation, regexWorkerHeapError, execArgvFromCommandLine, resolveLaunchRegexWorkerHeap, @@ -73,7 +72,6 @@ it("names the configurable regex deadline and the effective worker heap", () => readLaunchExecArgv(), readLaunchNodeOptions(), ); - expect(REGEX_MATCHING_LIMITATION).toBe(regexMatchingLimitation(heap)); expect(REGEX_MATCHING_LIMITATION).toMatch( /5000 ms matching deadline \(source default\)/iu, ); diff --git a/tests/boundary/process/releaseArtifactCanary.test.ts b/tests/boundary/process/releaseArtifactCanary.test.ts index 0850fe06e4..2257ce8508 100644 --- a/tests/boundary/process/releaseArtifactCanary.test.ts +++ b/tests/boundary/process/releaseArtifactCanary.test.ts @@ -113,7 +113,7 @@ if (process.argv[2] === "mcp") { const result = await processes.run( "npm", ["pack", "--ignore-scripts", "--json"], - { cwd: candidate, timeoutMs: 30_000 }, + { cwd: candidate }, ); expect(result.exitCode, result.stderr).toBe(0); return result; diff --git a/tests/boundary/process/runtimeBundle.test.ts b/tests/boundary/process/runtimeBundle.test.ts index 7a642da892..1ba446ee7a 100644 --- a/tests/boundary/process/runtimeBundle.test.ts +++ b/tests/boundary/process/runtimeBundle.test.ts @@ -50,7 +50,7 @@ export const lazyUrl = import.meta.url;`, 'export const generatedPackageMetadata = { version: "1.0.0" };', "dist/cli.js": 'export { facts, run } from "./main.js";', "dist/cliBuiltinArguments.js": "export {};\n", - "dist/cliOutput.js": 'export { facts } from "./provider/probe λ.js";', + "dist/cliOneShot.js": 'export { facts } from "./provider/probe λ.js";', "dist/cli/cliResultOutput.js": 'export { facts } from "../provider/probe λ.js";', "dist/mcpDoctor.js": 'export { run } from "./main.js";', @@ -99,7 +99,7 @@ export const run = async () => { [ "--input-type=module", "--eval", - `const { facts } = await import(${JSON.stringify(pathToFileURL(join(cliDirectory, "cli.js")).href)}); const output = await import(${JSON.stringify(pathToFileURL(join(cliDirectory, "cliOutput.js")).href)}); const streamed = await import(${JSON.stringify(pathToFileURL(join(cliDirectory, "cliResultOutput.js")).href)}); process.stdout.write(JSON.stringify(facts === output.facts && facts === streamed.facts));`, + `const { facts } = await import(${JSON.stringify(pathToFileURL(join(cliDirectory, "cli.js")).href)}); const oneShot = await import(${JSON.stringify(pathToFileURL(join(cliDirectory, "cliOneShot.js")).href)}); const streamed = await import(${JSON.stringify(pathToFileURL(join(cliDirectory, "cliResultOutput.js")).href)}); process.stdout.write(JSON.stringify(facts === oneShot.facts && facts === streamed.facts));`, ], { cwd: workspace.root }, ); @@ -137,7 +137,7 @@ export const run = async () => { await workspace.write("package.json", '{"type":"module"}'); for (const path of [ "dist/cliBuiltinArguments.js", - "dist/cliOutput.js", + "dist/cliOneShot.js", "dist/cli/cliResultOutput.js", "dist/mcpDoctor.js", ]) diff --git a/tests/boundary/process/sourceMapUncHosts.test.ts b/tests/boundary/process/sourceMapUncHosts.test.ts index 99953509f8..e09bfec02a 100644 --- a/tests/boundary/process/sourceMapUncHosts.test.ts +++ b/tests/boundary/process/sourceMapUncHosts.test.ts @@ -68,18 +68,14 @@ processTest.for(cases)( const sourceRoot = "root" in scenario ? scenario.root : null; // Import the built production helper in a child. A native assertion must // become a failed signal assertion here, never terminate the Vitest worker. - const result = await processes.run( - process.execPath, - [ - "--input-type=module", - "--eval", - `import { resolveJavaScriptSourceMapReferences } from "./dist/domain/javascript/javascriptSourceMapPaths.js"; + const result = await processes.run(process.execPath, [ + "--input-type=module", + "--eval", + `import { resolveJavaScriptSourceMapReferences } from "./dist/domain/javascript/javascriptSourceMapPaths.js"; const [source, root] = JSON.parse(process.argv[1]); console.log(JSON.stringify(resolveJavaScriptSourceMapReferences([source], root, "maps/main.js.map")[0]));`, - JSON.stringify([scenario.source, sourceRoot]), - ], - { timeoutMs: 10_000 }, - ); + JSON.stringify([scenario.source, sourceRoot]), + ]); expect(result.signal, result.stderr).toBeNull(); expect(result.exitCode, result.stderr).toBe(0); expect(JSON.parse(result.stdout)).toMatchObject({ diff --git a/tests/boundary/process/spawnArgumentLimit.test.ts b/tests/boundary/process/spawnArgumentLimit.test.ts index 78e64518ac..e7bd6472d3 100644 --- a/tests/boundary/process/spawnArgumentLimit.test.ts +++ b/tests/boundary/process/spawnArgumentLimit.test.ts @@ -66,7 +66,7 @@ processTest( const result = await processes.run( process.execPath, ["-e", "process.exit(0)", file], - { env: minimalSpawnEnvironment(), timeoutMs: 15_000 }, + { env: minimalSpawnEnvironment() }, ); expect(result.exitCode).toBe(0); }, @@ -161,7 +161,7 @@ processTest.skipIf(process.platform !== "linux")( processes.run( process.execPath, ["-e", script, "x".repeat(payloadBytes)], - { env: environment, timeoutMs: 15_000 }, + { env: environment }, ); if (accepted !== undefined) { const result = await run(accepted); diff --git a/tests/boundary/process/testGroupCancellation.test.ts b/tests/boundary/process/testGroupCancellation.test.ts new file mode 100644 index 0000000000..eb705bd9d5 --- /dev/null +++ b/tests/boundary/process/testGroupCancellation.test.ts @@ -0,0 +1,170 @@ +import { spawn } from "node:child_process"; +import { + copyFile, + mkdir, + readFile, + symlink, + writeFile, +} from "node:fs/promises"; +import { createServer, type Socket } from "node:net"; +import { join } from "node:path"; +import { setTimeout as delay } from "node:timers/promises"; + +import { expect, it } from "vitest"; + +import { execFileOutput } from "../../../src/process/ExecFileOutput.js"; +import { createDeferred } from "../../fixtures/binarySession.js"; +import { awaitAcknowledgement } from "../../fixtures/lifecycleAcknowledgement.js"; +import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; + +const groups = ["source", "built"] as const; + +const fixture = async () => { + const root = await createTestTempDirectory("rea-test-group-cancellation-"); + await mkdir(join(root, "scripts/ci"), { recursive: true }); + await mkdir(join(root, "scripts/lib")); + for (const path of [ + "scripts/ci/test-group.mjs", + "scripts/lib/test-projects.mjs", + "scripts/run-exclusive.mjs", + "scripts/run-exclusive-command.mjs", + "scripts/lib/windows-owned-command.mjs", + ]) + await copyFile(join(process.cwd(), path), join(root, path)); + await symlink( + join(process.cwd(), "node_modules"), + join(root, "node_modules"), + process.platform === "win32" ? "junction" : undefined, + ); + await writeFile(join(root, "package.json"), '{"type":"module"}'); + await writeFile( + join(root, "vitest.config.mjs"), + 'export default { test: { projects: ["domain", "boundary"].map(name => ({ test: { name, include: ["value.test.ts"], pool: "forks" } })) } };', + ); + return root; +}; + +it.each(groups)( + "preserves %s test results and forwarded reporter options", + async (group) => { + const root = await fixture(); + const path = join(root, "value.test.ts"); + await writeFile( + path, + 'import { it } from "vitest"; it("passes", () => {});', + ); + const run = () => + execFileOutput( + process.execPath, + [ + "scripts/ci/test-group.mjs", + group, + "--reporter=json", + "--outputFile=group-report.json", + ], + { cwd: root, timeout: 10_000, maxBuffer: 1024 * 1024 }, + ); + await run(); + const report = () => readFile(join(root, "group-report.json"), "utf8"); + expect(JSON.parse(await report())).toMatchObject({ + numPassedTests: 1, + success: true, + }); + await writeFile( + path, + 'import { it } from "vitest"; it("fails", () => { throw new Error("group fixture failure"); });', + ); + await expect(run()).rejects.toMatchObject({ + code: 1, + }); + expect(JSON.parse(await report())).toMatchObject({ + numFailedTests: 1, + success: false, + }); + expect(await report()).toContain("group fixture failure"); + }, +); + +const cancellations = groups.flatMap((group) => + (["SIGTERM", "SIGINT"] as const).map((signal) => ({ group, signal })), +); + +it.skipIf(process.platform === "win32").each(cancellations)( + "settles $group descendants before returning from $signal cancellation", + async ({ group, signal }) => { + const root = await fixture(); + const ready = createDeferred(); + const peerClosed = createDeferred(); + let peer: Socket | undefined; + let activity = ""; + const server = createServer((socket) => { + peer = socket; + socket.on("data", (chunk: Buffer) => { + activity += chunk.toString("utf8"); + if (activity.includes("ready\n")) ready.resolve(); + }); + socket.once("close", () => peerClosed.resolve()); + }); + await new Promise((resolve, reject) => { + server.once("error", reject); + server.listen(0, "127.0.0.1", resolve); + }); + const address = server.address(); + if (address === null || typeof address === "string") + throw new Error("No fixture listener"); + let runner: ReturnType | undefined; + let closed: + | Promise<{ code: number | null; signal: string | null }> + | undefined; + try { + await writeFile( + join(root, "descendant.cjs"), + `const net = require("node:net"); +process.on("SIGTERM", () => {}); process.on("SIGINT", () => {}); +const socket = net.connect(${address.port}, "127.0.0.1", () => socket.write("ready\\n")); +const timer = setInterval(() => socket.write("active\\n"), 20); +const deadline = setTimeout(release, 10000); +socket.on("data", release); +function release() { clearInterval(timer); clearTimeout(deadline); socket.end(); } +`, + ); + await writeFile( + join(root, "value.test.ts"), + 'import { it } from "vitest"; import { spawn } from "node:child_process"; it("waits", async () => { spawn(process.execPath, ["descendant.cjs"], { stdio: "ignore" }); await new Promise(() => {}); }, 30000);', + ); + runner = spawn(process.execPath, ["scripts/ci/test-group.mjs", group], { + cwd: root, + stdio: "ignore", + }); + closed = new Promise((resolve) => + runner!.once("exit", (code, signal) => resolve({ code, signal })), + ); + await awaitAcknowledgement( + ready.promise, + closed, + "test-owned descendant readiness", + 5_000, + ); + runner.kill(signal); + expect(await closed).toEqual({ + code: signal === "SIGTERM" ? 143 : 130, + signal: null, + }); + const stopped = activity; + await delay(100); + expect(activity).toBe(stopped); + await awaitAcknowledgement( + peerClosed.promise, + delay(1_000), + "test-owned descendant completion", + ); + } finally { + if (peer !== undefined && !peer.destroyed) peer.write("release"); + if (runner?.exitCode === null && runner.signalCode === null) + runner.kill("SIGTERM"); + if (closed !== undefined) await closed; + if (peer !== undefined) await peerClosed.promise; + await new Promise((resolve) => server.close(() => resolve())); + } + }, +); diff --git a/tests/boundary/process/verifierAdoptedLifetime.test.ts b/tests/boundary/process/verifierAdoptedLifetime.test.ts new file mode 100644 index 0000000000..61e5bbe4ef --- /dev/null +++ b/tests/boundary/process/verifierAdoptedLifetime.test.ts @@ -0,0 +1,107 @@ +import { open, writeFile } from "node:fs/promises"; +import { join } from "node:path"; +import { expect, it } from "vitest"; + +import { withArtifactMcp } from "../../../scripts/lib/artifact-e2e.mjs"; +import { createVerifierCommands } from "../../../scripts/lib/verify-commands.mjs"; +import { cleanupOwnedProcessGroup } from "../../../src/process/ProcessOwnership.js"; +import { spawnOwnedProviderProcess } from "../../../src/process/ProviderProcess.js"; +import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; + +it("keeps the shared owner's real MCP connection open across an unrelated command", async () => { + const root = await createTestTempDirectory("rea-verifier-adopted-mcp-"); + const target = join(root, "fixture.plist"); + await writeFile( + target, + 'namefixture', + ); + const commands = await createVerifierCommands(); + try { + await withArtifactMcp( + target, + async (client) => { + await client.ping(); + expect( + ( + await commands.exec( + process.execPath, + ["-e", "process.stdout.write('ok')"], + { timeout: 5_000 }, + ) + ).stdout, + ).toBe("ok"); + // The helper adopted this exact transport before entering the callback. + // A new protocol request must still work after command supervision. + await client.ping(); + }, + { commands }, + ); + } finally { + await commands.close(); + } +}); + +it.skipIf(process.platform === "win32")( + "retries failed command cleanup before another launch without closing live adopted files", + async () => { + const root = await createTestTempDirectory("rea-verifier-adopted-pending-"); + const path = join(root, "evidence.txt"); + await writeFile(path, "retained evidence"); + const handle = await open(path, "r"); + let deny = true; + let launches = 0; + let releases = 0; + const commands = await createVerifierCommands({ + launcher: async (input) => { + launches++; + const owned = await spawnOwnedProviderProcess(input); + return { + ...owned, + cleanup: () => + deny + ? Promise.resolve({ + cleaned: false as const, + reason: "authored cleanup denial", + }) + : owned.cleanup === undefined + ? cleanupOwnedProcessGroup(owned.ownership) + : owned.cleanup(), + }; + }, + }); + commands.adopt( + { + async close() { + releases++; + await handle.close(); + }, + }, + path, + ); + const execute = () => + commands.exec(process.execPath, ["-e", "process.stdout.write('ok')"], { + timeout: 5_000, + }); + try { + await expect(execute()).rejects.toBeInstanceOf(Error); + expect(launches).toBe(1); + expect(releases).toBe(0); + await expect(execute()).rejects.toBeInstanceOf(Error); + expect(launches).toBe(1); + expect((await handle.stat()).size).toBe( + Buffer.byteLength("retained evidence"), + ); + expect(commands.cleanupResources).toContain(path); + deny = false; + expect((await execute()).stdout).toBe("ok"); + expect(launches).toBe(2); + expect(releases).toBe(0); + } finally { + deny = false; + await commands.close(); + } + expect(releases).toBe(1); + expect(handle.fd).toBe(-1); + expect(commands.cleanupResources).toEqual([]); + }, +); diff --git a/tests/boundary/process/verifierCommandSemantics.test.ts b/tests/boundary/process/verifierCommandSemantics.test.ts index dbeedc9523..2a98086631 100644 --- a/tests/boundary/process/verifierCommandSemantics.test.ts +++ b/tests/boundary/process/verifierCommandSemantics.test.ts @@ -371,7 +371,6 @@ process.stdout.write("unexpected stdin EOF\\n"); expect(commands.cleanupResources).toEqual([]); expect(existsSync(directory)).toBe(true); }, - 10_000, ); it.skipIf(process.platform === "win32")( @@ -444,7 +443,6 @@ it.skipIf(process.platform === "win32")( throw new Error("Expected the exact acquired process owner"); controller.abort(reason); const closing = commands.close(); - expect(commands.close()).toBe(closing); let closeSettled = false; const closeOutcome = observed(closing).then((outcome) => { closeSettled = true; @@ -522,7 +520,6 @@ it.skipIf(process.platform === "win32")( } } }, - 15_000, ); it.skipIf(process.platform === "win32")( diff --git a/tests/boundary/process/verifierSourceLoading.test.ts b/tests/boundary/process/verifierSourceLoading.test.ts index 3533e96214..f77e224a71 100644 --- a/tests/boundary/process/verifierSourceLoading.test.ts +++ b/tests/boundary/process/verifierSourceLoading.test.ts @@ -25,11 +25,11 @@ module.syncBuiltinESMExports(); const { importVerifierSource } = await import(${scriptUrl("verifier-source-loader.mjs")}); await importVerifierSource(new URL("./src/domain/skillManifest.ts", import.meta.url)); `; - const result = await processes.run( - process.execPath, - ["--input-type=module", "--eval", source], - { timeoutMs: 20_000 }, - ); + const result = await processes.run(process.execPath, [ + "--input-type=module", + "--eval", + source, + ]); expect(result.exitCode).toBe(1); expect(result.stderr).toContain("Node.js version pinned in .nvmrc"); expect(result.stderr).toContain("source build and verifier scripts"); @@ -86,7 +86,7 @@ console.log(JSON.stringify({ const result = await processes.run( process.execPath, ["--input-type=module", "--eval", source], - { env: { ...process.env, TSX_DISABLE_CACHE: "1" }, timeoutMs: 20_000 }, + { env: { ...process.env, TSX_DISABLE_CACHE: "1" } }, ); expect(result.exitCode, result.stderr).toBe(0); const observation = z @@ -151,7 +151,7 @@ await withVerifierCleanup(async () => { const result = await processes.run( process.execPath, ["--input-type=module", "--eval", source], - { timeoutMs: 20_000, env: { ...process.env, TSX_DISABLE_CACHE: "1" } }, + { env: { ...process.env, TSX_DISABLE_CACHE: "1" } }, ); expect(result.exitCode, result.stderr).toBe(0); expect(JSON.parse(result.stdout)).toEqual({ @@ -233,7 +233,7 @@ console.log(JSON.stringify({ const result = await processes.run( process.execPath, [workspace.path("probe.mjs")], - { cwd: workspace.root, timeoutMs: 20_000 }, + { cwd: workspace.root }, ); expect(result.exitCode, result.stderr).toBe(0); expect(JSON.parse(result.stdout)).toEqual({ diff --git a/tests/boundary/process/websiteInitialization.test.ts b/tests/boundary/process/websiteInitialization.test.ts index 73b5fa1bf4..683b472198 100644 --- a/tests/boundary/process/websiteInitialization.test.ts +++ b/tests/boundary/process/websiteInitialization.test.ts @@ -36,11 +36,11 @@ const [message, cause] = await reported; assert.equal(message, "Website initialization failed"); assert.equal(cause, globalThis.translationFailure); `; - const result = await processes.run( - process.execPath, - ["--input-type=module", "--eval", source], - { timeoutMs: 10_000 }, - ); + const result = await processes.run(process.execPath, [ + "--input-type=module", + "--eval", + source, + ]); expect(result.signal, result.stderr).toBeNull(); expect(result.exitCode, result.stderr).toBe(0); }, diff --git a/tests/boundary/process/windowsExecutableSelection.test.ts b/tests/boundary/process/windowsExecutableSelection.test.ts index bc508b223e..dd057304bb 100644 --- a/tests/boundary/process/windowsExecutableSelection.test.ts +++ b/tests/boundary/process/windowsExecutableSelection.test.ts @@ -74,7 +74,6 @@ processTest.skipIf(process.platform !== "win32")( ["--input-type=module", "--eval", program], { cwd: working, - timeoutMs: 30_000, windowsHide: true, }, ); @@ -133,7 +132,6 @@ processTest.skipIf(process.platform !== "win32")( ["--input-type=module", "--eval", program], { cwd: root, - timeoutMs: 40_000, windowsHide: true, }, ); diff --git a/tests/boundary/process/windowsNativeArtifactVerifier.test.ts b/tests/boundary/process/windowsNativeArtifactVerifier.test.ts index 9bd0ce1ac3..1289dbca4b 100644 --- a/tests/boundary/process/windowsNativeArtifactVerifier.test.ts +++ b/tests/boundary/process/windowsNativeArtifactVerifier.test.ts @@ -97,7 +97,7 @@ for (const mode of ["preinstall source", "packed source-free"]) { processes.run( process.execPath, [join(root, "scripts/verify-windows-native-artifact.mjs")], - { cwd: root, timeoutMs: 10_000 }, + { cwd: root }, ); const valid = await run(); expect(valid.exitCode, valid.stderr).toBe(0); diff --git a/tests/boundary/process/windowsNativeInspectionWorkspace.test.ts b/tests/boundary/process/windowsNativeInspectionWorkspace.test.ts index 8d70ecf2cd..e9a69ae856 100644 --- a/tests/boundary/process/windowsNativeInspectionWorkspace.test.ts +++ b/tests/boundary/process/windowsNativeInspectionWorkspace.test.ts @@ -39,7 +39,7 @@ processTest( 'assert.equal(typeof loader.systemWindowsNativeAuthority, "function");', ].join("\n"), ], - { cwd: workspace, timeoutMs: 10_000 }, + { cwd: workspace }, ); expect(imported.exitCode, imported.stderr).toBe(0); expect(await realpath(join(workspace, "node_modules"))).toBe( diff --git a/tests/boundary/providers/CutterBridgeClient.test.ts b/tests/boundary/providers/CutterBridgeClient.test.ts index c4be289712..81334c929d 100644 --- a/tests/boundary/providers/CutterBridgeClient.test.ts +++ b/tests/boundary/providers/CutterBridgeClient.test.ts @@ -507,13 +507,10 @@ describe("CutterBridgeClient: scenario group 2", () => { ); const sessionId = "27d3e3f1-f1e5-49ae-91ec-95af1f343a5a"; const token = "synthetic-cutter-bridge-token-for-test"; - server = createServer((connection) => { - connection.setEncoding("utf8"); - connection.once("data", () => - connection.end( - `${JSON.stringify({ ok: true, session_id: "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa" })}\n`, - ), - ); + server = statusServer(token, null, 0); + let connections = 0; + server.on("connection", () => { + connections += 1; }); const port = await listen(server); await writeDescriptor(directory, sessionId, port, token); @@ -565,16 +562,12 @@ describe("CutterBridgeClient: scenario group 2", () => { ).toBe(0o600); } } - // Parsing and filename identity reject only the malformed candidates; - // the authenticated candidate remains discoverable before status checking. - expect( - (await client.listDescriptors()).map(({ session_id }) => session_id), - ).toEqual([sessionId]); const result = await client.listSessions(); expect(result).toMatchObject({ - discovery_status: "no_live_bridge_found", - sessions: [], + discovery_status: "sessions_found", + sessions: [{ session_id: sessionId }], }); + expect(connections).toBe(1); }); it("ignores a descriptor whose status endpoint is unavailable", async () => { @@ -1052,14 +1045,16 @@ describe("CutterBridgeClient: scenario group 6", () => { if (process.platform === "win32") return; directory = await mkdtemp(join(tmpdir(), "rea-cutter-unsafe-descriptor-")); const sessionId = "27d3e3f1-f1e5-49ae-91ec-95af1f343a5a"; + const token = "synthetic-cutter-bridge-token-for-test"; + server = statusServer(token, null, 0); + let connections = 0; + server.on("connection", () => { + connections += 1; + }); + const port = await listen(server); const name = `cutter-${process.pid}-${sessionId}.json`; const target = join(directory, "target.json"); - await writeDescriptor( - directory, - sessionId, - 1234, - "synthetic-cutter-bridge-token-for-test", - ); + await writeDescriptor(directory, sessionId, port, token); await writeFile(target, await readFile(join(directory, name)), { mode: 0o600, }); @@ -1067,15 +1062,36 @@ describe("CutterBridgeClient: scenario group 6", () => { await expect( new CutterBridgeClient({ REA_CUTTER_BRIDGE_DIR: directory, - }).listDescriptors(), - ).resolves.toEqual([]); + }).listSessions(), + ).resolves.toMatchObject({ + sessions: [], + discovery_status: "no_live_bridge_found", + }); + expect(connections).toBe(0); await rm(join(directory, name)); await symlink(target, join(directory, name)); await expect( new CutterBridgeClient({ REA_CUTTER_BRIDGE_DIR: directory, - }).listDescriptors(), - ).resolves.toEqual([]); + }).listSessions(), + ).resolves.toMatchObject({ + sessions: [], + discovery_status: "no_live_bridge_found", + }); + expect(connections).toBe(0); + await rm(join(directory, name)); + await writeFile(join(directory, name), await readFile(target), { + mode: 0o600, + }); + await expect( + new CutterBridgeClient({ + REA_CUTTER_BRIDGE_DIR: directory, + }).listSessions(), + ).resolves.toMatchObject({ + sessions: [{ session_id: sessionId }], + discovery_status: "sessions_found", + }); + expect(connections).toBe(1); }); }); @@ -1126,12 +1142,10 @@ describe("CutterBridgeClient: scenario group 7", () => { it("supports platforms without O_NOFOLLOW using the documented read-only flags", async () => { directory = await mkdtemp(join(tmpdir(), "rea-cutter-no-nofollow-")); const sessionId = "27d3e3f1-f1e5-49ae-91ec-95af1f343a5a"; - await writeDescriptor( - directory, - sessionId, - 1234, - "synthetic-cutter-bridge-token-for-test", - ); + const token = "synthetic-cutter-bridge-token-for-test"; + server = statusServer(token, null, 0); + const port = await listen(server); + await writeDescriptor(directory, sessionId, port, token); await vi.resetModules(); vi.doMock("node:fs", async () => { const real = await vi.importActual("node:fs"); @@ -1146,8 +1160,11 @@ describe("CutterBridgeClient: scenario group 7", () => { await expect( new NoNoFollowClient({ REA_CUTTER_BRIDGE_DIR: directory, - }).listDescriptors(), - ).resolves.toMatchObject([{ session_id: sessionId }]); + }).listSessions(), + ).resolves.toMatchObject({ + sessions: [{ session_id: sessionId }], + discovery_status: "sessions_found", + }); } finally { vi.doUnmock("node:fs"); await vi.resetModules(); @@ -1157,11 +1174,15 @@ describe("CutterBridgeClient: scenario group 7", () => { describe("CutterBridgeClient: conservative platform checks", () => { let directory: string | undefined; + let server: ReturnType | undefined; afterEach(async () => { + if (server !== undefined) + await new Promise((resolve) => server?.close(() => resolve())); if (directory !== undefined) await rm(directory, { recursive: true, force: true }); directory = undefined; + server = undefined; }); it("uses conservative platform and Windows descriptor checks", async () => { @@ -1183,12 +1204,14 @@ describe("CutterBridgeClient: conservative platform checks", () => { getuid.mockRestore(); } const sessionId = "27d3e3f1-f1e5-49ae-91ec-95af1f343a5a"; - await writeDescriptor( - directory, - sessionId, - 1234, - "synthetic-cutter-bridge-token-for-test", - ); + const token = "synthetic-cutter-bridge-token-for-test"; + server = statusServer(token, null, 0); + let connections = 0; + server.on("connection", () => { + connections += 1; + }); + const port = await listen(server); + await writeDescriptor(directory, sessionId, port, token); const result = await new CutterBridgeClient( { REA_CUTTER_BRIDGE_DIR: directory }, { @@ -1198,8 +1221,27 @@ describe("CutterBridgeClient: conservative platform checks", () => { readDescriptor: () => Buffer.alloc(64 * 1024 + 1), }, }, - ).listDescriptors(); - expect(result).toEqual([]); + ).listSessions(); + expect(result).toMatchObject({ + sessions: [], + discovery_status: "no_live_bridge_found", + }); + expect(connections).toBe(0); + const valid = await new CutterBridgeClient( + { REA_CUTTER_BRIDGE_DIR: directory }, + { + platform: "win32", + windowsPrivateReader: { + verifyDirectory: () => true, + readDescriptor: (root, name) => readFileSync(join(root, name)), + }, + }, + ).listSessions(); + expect(valid).toMatchObject({ + sessions: [{ session_id: sessionId }], + discovery_status: "sessions_found", + }); + expect(connections).toBe(1); await expect( new CutterBridgeClient( { LOCALAPPDATA: "C:\\Users\\fixture\\AppData\\Local" }, @@ -1333,13 +1375,16 @@ describe("CutterBridgeClient: bridge paths and native descriptor reads", () => { it("uses the packaged Windows authority for descriptor verification and reads", async () => { directory = await mkdtemp(join(tmpdir(), "rea-cutter-native-reader-")); const sessionId = "27d3e3f1-f1e5-49ae-91ec-95af1f343a5a"; + const token = "synthetic-cutter-bridge-token-for-test"; + server = statusServer(token, null, 0); + const port = await listen(server); const entry = `cutter-${process.pid}-${sessionId}.json`; const descriptor = { session_id: sessionId, pid: process.pid, host: "127.0.0.1", - port: 1234, - token: "synthetic-cutter-bridge-token-for-test", + port, + token, document_generation: 0, current_file: null, cutter_version: null, @@ -1375,9 +1420,10 @@ describe("CutterBridgeClient: bridge paths and native descriptor reads", () => { bridge_directory_security: "unverified_platform_acl", }); failVerification = false; - await expect(client.listDescriptors()).resolves.toMatchObject([ - { session_id: sessionId, pid: process.pid }, - ]); + await expect(client.listSessions()).resolves.toMatchObject({ + sessions: [{ session_id: sessionId, pid: process.pid }], + discovery_status: "sessions_found", + }); } finally { vi.doUnmock("../../../src/windows/WindowsNativeLoader.js"); await vi.resetModules(); diff --git a/tests/boundary/providers/GdbSessionManagerCoverage.test.ts b/tests/boundary/providers/GdbSessionManagerCoverage.test.ts index 11fa9ffd1c..b498ebb6f4 100644 --- a/tests/boundary/providers/GdbSessionManagerCoverage.test.ts +++ b/tests/boundary/providers/GdbSessionManagerCoverage.test.ts @@ -14,7 +14,7 @@ import { import { commandMayChangeInferiorOutput, gdbOneShotCommandMayInvalidateDetachProof, - gdbThreadGroupCount, + gdbThreadGroupPids, GdbSessionManager, miStringField, } from "../../../src/gdb/GdbSessionManager.js"; @@ -114,13 +114,21 @@ it.each([ undefined, ], ] as const)("parses recursive GDB MI groups: %s", (_label, mi, expected) => { - expect(gdbThreadGroupCount(mi)).toBe(expected); + expect(gdbThreadGroupPids(mi)?.length).toBe(expected); +}); + +it("preserves the reported inferior PIDs in inventory order", () => { + expect( + gdbThreadGroupPids( + '1^done,groups=[{id="i1",pid="123"},{id="i2",pid="456"}]', + ), + ).toEqual(["123", "456"]); }); it("keeps GDB inventory unknown when MI nesting exceeds the parser frame budget", () => { const nestedValue = "[".repeat(257) + "]".repeat(257); expect( - gdbThreadGroupCount("1^done,groups=[],extension=" + nestedValue), + gdbThreadGroupPids("1^done,groups=[],extension=" + nestedValue), ).toBeUndefined(); }); @@ -687,9 +695,9 @@ it("rejects GDB commands queued before closeAll when their turn arrives", async await expect(queued).resolves.toMatchObject({ ok: false, error: { - _tag: "AnalysisInputError", + _tag: "AnalysisCapabilityUnavailableError", operation: "gdb_console", - issues: [{ path: ["session_id"], reason: "invalid_value" }], + reason: expect.stringContaining("REA is shutting down"), }, }); await closing; @@ -724,9 +732,9 @@ it("rejects GDB commands queued behind a session close", async () => { await expect(queued).resolves.toMatchObject({ ok: false, error: { - _tag: "AnalysisInputError", + _tag: "AnalysisCapabilityUnavailableError", operation: "gdb_console", - issues: [{ path: ["session_id"], reason: "invalid_value" }], + reason: expect.stringContaining("close_gdb_session started releasing"), }, }); expect(commands.some((command) => command.includes('"run"'))).toBe(false); @@ -777,9 +785,9 @@ it.each(["close", "retryCleanupFailures"] as const)( const unavailable = { ok: false, error: { - _tag: "AnalysisInputError", + _tag: "AnalysisCapabilityUnavailableError", operation: "gdb_console", - issues: [{ path: ["session_id"], reason: "invalid_value" }], + reason: expect.stringContaining("close_gdb_session started releasing"), }, }; // Finish the barrier before assertions so a regression cannot strand teardown. @@ -1224,7 +1232,7 @@ it("closes an idle session when its quoted executable contains groups=[", async commands.filter((command) => command.includes("list-thread-groups")), ).toHaveLength(1); expect(commands).toContain("-gdb-exit"); -}, 10_000); +}); it("reports incomplete stop if GDB exits while close waits in the serialized queue", async () => { let heldToken = ""; @@ -1514,23 +1522,28 @@ it("rejects an inferior TTY when GDB does not confirm the setting command", asyn }); it("rejects a truncated inferior TTY readback before publishing the session", async () => { - const tty = `/dev/pts/${"x".repeat(200)}`; + const tty = "/dev/pts/42"; const fake = createFakeSpawner((child, token, command) => { - child.stdout.write( - command.includes("inferior-tty-show") - ? `${token}^done,inferior_tty_terminal=${JSON.stringify(tty)}\n` - : `${token}^done\n`, - ); - }); - const manager = managerWithFakeSpawner(fake.spawnProcess, { - controlReplyBytes: 80, + if (command.includes("inferior-tty-show")) { + const record = `~"${"x".repeat(1024 * 1024)}"\n`; + for (let index = 0; index < 17; index += 1) child.stdout.write(record); + child.stdout.write( + `${token}^done,inferior_tty_terminal=${JSON.stringify(tty)}\n`, + ); + } else child.stdout.write(`${token}^done\n`); }); + const manager = managerWithFakeSpawner(fake.spawnProcess); const started = await manager.start(undefined, { inferiorTty: tty }); expect(started).toMatchObject({ ok: false, - error: { _tag: "AnalysisCapabilityUnavailableError" }, + error: { + _tag: "AnalysisCapabilityUnavailableError", + message: expect.stringContaining( + "did not report the exact caller-selected inferior TTY", + ), + }, }); expect(fake.children[0]?.killSignals).toEqual(["SIGTERM"]); }); @@ -1861,7 +1874,10 @@ it.each([ const command = await manager.execute(sessionId, "show version"); expect(command).toMatchObject({ ok: false, - error: { _tag: "AnalysisInputError" }, + error: { + _tag: "AnalysisCapabilityUnavailableError", + operation: "gdb_console", + }, }); expect(commands).not.toContain('-interpreter-exec console "show version"'); expect(fake.children[0]?.exitCode).toBeNull(); @@ -2288,7 +2304,11 @@ it("rejects the command queued behind a timed-out GDB request", async () => { const rejected = await queued; expect(rejected.ok).toBe(false); - if (!rejected.ok) expect(rejected.error._tag).toBe("AnalysisInputError"); + if (!rejected.ok) + expect(rejected.error).toMatchObject({ + _tag: "AnalysisCapabilityUnavailableError", + reason: expect.stringContaining("timed out"), + }); }); it("retains a multi-run console command through its completion query before dispatching a follower", async () => { @@ -2456,7 +2476,7 @@ it("retains unknown completion and partial Evidence when the completion query fa await manager.execute(started.value.session_id, "show version"), ).toMatchObject({ ok: false, - error: { _tag: "AnalysisInputError" }, + error: { _tag: "AnalysisCapabilityUnavailableError" }, }); }); @@ -2587,7 +2607,7 @@ it.each([false, true])( if (follower !== undefined) expect(await follower).toMatchObject({ ok: false, - error: { _tag: "AnalysisInputError" }, + error: { _tag: "AnalysisCapabilityUnavailableError" }, }); expect(commands.some((command) => command.includes("show version"))).toBe( false, @@ -3982,6 +4002,8 @@ it.each(["incomplete", "rejected"] as const)( fake.children[0]?.exit(0, null); return { status: "stopped" }; } + // That simulated verification stands for later stops of the session. + if (stopCalls > 3) return { status: "already_exited" }; return stopOwnedDebuggerProcess(session, name); }, }); @@ -4031,15 +4053,25 @@ it.each(["incomplete", "rejected"] as const)( ); expect(subsequent).toMatchObject({ ok: false, - error: { _tag: "AnalysisInputError" }, + error: { + _tag: "AnalysisCapabilityUnavailableError", + reason: expect.stringContaining("GDB stdin write failed"), + }, }); expect((await manager.retryCleanupFailures()).ok).toBe(false); expect(stopCalls).toBe(2); if (!failed.ok) expect(failed.error.cleanupIncomplete).toBe(true); expect((await manager.retryCleanupFailures()).ok).toBe(true); expect(stopCalls).toBe(3); - expect(manager.status(started.value.session_id)).toBeUndefined(); if (!failed.ok) expect(failed.error.cleanupIncomplete).toBe(false); + // The caller still holds the published ID, so the diagnosis outlives + // cleanup until close_gdb_session releases it. + expect(manager.status(started.value.session_id)).toMatchObject({ + state: "closed", + closed_reason: expect.stringContaining("GDB stdin write failed"), + }); + expect((await manager.close(started.value.session_id)).ok).toBe(true); + expect(manager.status(started.value.session_id)).toBeUndefined(); }, ); diff --git a/tests/boundary/providers/RizinDebugSessionCoverage.part3.test.ts b/tests/boundary/providers/RizinDebugSessionCoverage.part3.test.ts index 1ac7bc939f..0f433bdefd 100644 --- a/tests/boundary/providers/RizinDebugSessionCoverage.part3.test.ts +++ b/tests/boundary/providers/RizinDebugSessionCoverage.part3.test.ts @@ -1,10 +1,7 @@ import { afterEach, expect, it, vi } from "vitest"; import { withVerifierCleanup } from "../../../scripts/lib/verifier-cleanup.mjs"; import { awaitAcknowledgement } from "../../fixtures/lifecycleAcknowledgement.js"; -import { - AnalysisCancelledError, - AnalysisInputError, -} from "../../../src/domain/analysisErrorCore.js"; +import { AnalysisCancelledError } from "../../../src/domain/analysisErrorCore.js"; import { stopOwnedDebuggerProcess } from "../../../src/process/ownedDebuggerSession.js"; import { rizinTestInferiorTty } from "./RizinTestInferiorTty.js"; import { @@ -355,7 +352,13 @@ it.skipIf(process.platform === "win32")( expect((await first).ok).toBe(true); const second = await queued; expect(second.ok).toBe(false); - if (!second.ok) expect(second.error).toBeInstanceOf(AnalysisInputError); + if (!second.ok) + expect(second.error).toMatchObject({ + _tag: "AnalysisCapabilityUnavailableError", + reason: expect.stringContaining( + "The Rizin process exited (exit code null, signal SIGTERM)", + ), + }); expect(writes.some((text) => text.includes("second\n"))).toBe(false); }, ); diff --git a/tests/boundary/providers/RizinDebugSessionCoverage.part4.test.ts b/tests/boundary/providers/RizinDebugSessionCoverage.part4.test.ts index d66cbbfd49..5e8d84b101 100644 --- a/tests/boundary/providers/RizinDebugSessionCoverage.part4.test.ts +++ b/tests/boundary/providers/RizinDebugSessionCoverage.part4.test.ts @@ -3,14 +3,13 @@ import { access, writeFile } from "node:fs/promises"; import { join } from "node:path"; import { evidenceSchema } from "../../../src/domain/evidence.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; -import { rizinTestInferiorTty } from "./RizinTestInferiorTty.js"; import { createRizinCoverageHarness, createInjectedProcess, incompleteStop, } from "./RizinDebugSessionCoverage.fixtures.js"; -const { cleanup, makeProvider, start, startInjected, createManager } = +const { cleanup, makeProvider, start, startInjected } = createRizinCoverageHarness(); afterEach(cleanup); diff --git a/tests/boundary/providers/RizinDebugSessionManagers.test.ts b/tests/boundary/providers/RizinDebugSessionManagers.test.ts index 61e31ee8c7..49768049c6 100644 --- a/tests/boundary/providers/RizinDebugSessionManagers.test.ts +++ b/tests/boundary/providers/RizinDebugSessionManagers.test.ts @@ -5,7 +5,6 @@ import { join, resolve } from "node:path"; import { afterEach, describe, expect, it } from "vitest"; import { evidenceSchema } from "../../../src/domain/evidence.js"; -import { RizinDebugSessionManager } from "../../../src/rizin/RizinDebugSessionManager.js"; import { createRizinTestManager, rizinTestInferiorTty, diff --git a/tests/boundary/providers/RizinInferiorTty.test.ts b/tests/boundary/providers/RizinInferiorTty.test.ts index 35603c119a..901a2d0ce2 100644 --- a/tests/boundary/providers/RizinInferiorTty.test.ts +++ b/tests/boundary/providers/RizinInferiorTty.test.ts @@ -304,7 +304,6 @@ it.skipIf(process.platform !== "linux")( }); await expectCloseRejectedAndReleased(sessionId); }, - 4_000, ); it("rejects changed or missing Rizin inferior identity during detach", () => { diff --git a/tests/boundary/providers/adb/adbCommandFinalization.test.ts b/tests/boundary/providers/adb/adbCommandFinalization.test.ts index 52397f92a1..816ccc36a6 100644 --- a/tests/boundary/providers/adb/adbCommandFinalization.test.ts +++ b/tests/boundary/providers/adb/adbCommandFinalization.test.ts @@ -313,7 +313,6 @@ nativeClientTest.each([false, true])( expect(prerequisite.capturedOutput).toEqual(capturedOutput); expect(result.error.capturedOutput).toEqual(capturedOutput); }, - 20000, ); nativeClientTest( @@ -350,5 +349,4 @@ nativeClientTest( details: { timeout_ms: 10000 }, }); }, - 20000, ); diff --git a/tests/boundary/providers/adb/adbScreenPublicationCancellation.test.ts b/tests/boundary/providers/adb/adbScreenPublicationCancellation.test.ts index a1cd6b1abc..a1dfaa07ce 100644 --- a/tests/boundary/providers/adb/adbScreenPublicationCancellation.test.ts +++ b/tests/boundary/providers/adb/adbScreenPublicationCancellation.test.ts @@ -316,7 +316,7 @@ test("does not replace a destination created after the initial existence check", ); const output = join(f.root, "captures"); const destination = join(output, "screen.png"); - vi.mocked(link).mockImplementationOnce(async (...args) => { + vi.mocked(link).mockImplementationOnce(async () => { await actualFs.writeFile(destination, "caller-owned"); throw Object.assign(new Error("hard links are unsupported"), { code: "EPERM", diff --git a/tests/boundary/providers/apktoolDecodeDeadline.test.ts b/tests/boundary/providers/apktoolDecodeDeadline.test.ts index 1390e47568..f6b773ee03 100644 --- a/tests/boundary/providers/apktoolDecodeDeadline.test.ts +++ b/tests/boundary/providers/apktoolDecodeDeadline.test.ts @@ -79,5 +79,4 @@ it.skipIf(process.platform === "win32")( await fixture.cleanup([pending, provider.close()]); } }, - 30_000, ); diff --git a/tests/boundary/providers/apktoolProcessFixture.test.ts b/tests/boundary/providers/apktoolProcessFixture.test.ts index 1855975496..729a157a26 100644 --- a/tests/boundary/providers/apktoolProcessFixture.test.ts +++ b/tests/boundary/providers/apktoolProcessFixture.test.ts @@ -60,5 +60,4 @@ it await fixture.cleanup([probe, commands.close()]); } }, - 30_000, ); diff --git a/tests/boundary/providers/cutterBridgeRequestLimit.test.ts b/tests/boundary/providers/cutterBridgeRequestLimit.test.ts index 0de0ef406d..34a2e9e55e 100644 --- a/tests/boundary/providers/cutterBridgeRequestLimit.test.ts +++ b/tests/boundary/providers/cutterBridgeRequestLimit.test.ts @@ -175,7 +175,23 @@ it.each([false, true])( async (malformed) => { const directory = await privateDirectory(); const currentFile = "/selected/\uFFFD"; - await writeDescriptor(directory, SESSION, 9, TOKEN, { currentFile }); + let connections = 0; + const server = authenticatedServer( + TOKEN, + SESSION, + (request, respond) => { + respond({ ok: true, session_id: request.session_id }); + }, + true, + ); + server.on("connection", () => { + connections += 1; + }); + const port = await listen(server); + await writeDescriptor(directory, SESSION, port, TOKEN, { + currentFile, + maxRequestBytes: 8192, + }); const path = join(directory, `cutter-${process.pid}-${SESSION}.json`); if (malformed) { const bytes = await readFile(path); @@ -192,9 +208,13 @@ it.each([false, true])( ); } const client = clientFor(directory, "8192"); - const descriptors = await client.listDescriptors(); - expect(descriptors).toHaveLength(malformed ? 0 : 1); - if (!malformed) expect(descriptors[0]?.current_file).toBe(currentFile); + const result = await client.listSessions(); + expect(result.discovery_status).toBe( + malformed ? "no_live_bridge_found" : "sessions_found", + ); + expect(result.sessions).toHaveLength(malformed ? 0 : 1); + if (!malformed) expect(result.sessions[0]?.current_file).toBe(currentFile); + expect(connections).toBe(malformed ? 0 : 1); }, ); diff --git a/tests/boundary/providers/desktopExportJsonDepth.test.ts b/tests/boundary/providers/desktopExportJsonDepth.test.ts index 0dc7a65864..5c7783782d 100644 --- a/tests/boundary/providers/desktopExportJsonDepth.test.ts +++ b/tests/boundary/providers/desktopExportJsonDepth.test.ts @@ -174,7 +174,7 @@ print(json.dumps({"view": result["view"], "peak": peak})) ) as { view: unknown; peak: number }; expect(measured.view).toEqual({ value: "done" }); expect(measured.peak).toBeLessThan(8 * 1024 * 1024); -}, 40000); +}); it("bounds a wide container already beyond the retained view depth", async () => { const root = await createTestTempDirectory("desktop-json-wide-depth-"); @@ -203,4 +203,4 @@ print(json.dumps({"error": result.get("error"), "peak": peak})) "nested deeper than the 500 levels a JSON view keeps", ); expect(measured.peak).toBeLessThan(8 * 1024 * 1024); -}, 40000); +}); diff --git a/tests/boundary/providers/desktopStateHeap.test.ts b/tests/boundary/providers/desktopStateHeap.test.ts index 4bfe75962b..cbcd1b288d 100644 --- a/tests/boundary/providers/desktopStateHeap.test.ts +++ b/tests/boundary/providers/desktopStateHeap.test.ts @@ -32,27 +32,34 @@ const inSmallHeap = async (module: string, name: string, body: string) => { return JSON.parse(output) as unknown; }; -it("checks JSON depth with one iterator per level, so a wide array fits a small heap", async () => { - // Queuing a pair per member needed more than 64 MiB here; one iterator per level runs in 18 MiB. - const depths = await inSmallHeap( - "stateFormats.ts", - "deeperThan", - `const wide = new Array(800_000).fill(null); +it( + "checks JSON depth with one iterator per level, so a wide array fits a small heap", + { tags: ["long-running"] }, + async () => { + // Queuing a pair per member needed more than 64 MiB here; one iterator per level runs in 18 MiB. + const depths = await inSmallHeap( + "stateFormats.ts", + "deeperThan", + `const wide = new Array(800_000).fill(null); let nested = []; for (let level = 0; level < 501; level += 1) nested = [nested]; console.log(JSON.stringify({ wide: deeperThan(wide, 500), edge: deeperThan(nested, 501), over: deeperThan(nested, 500) })); `, - ); + ); - expect(depths).toEqual({ wide: false, edge: false, over: true }); -}, 180_000); + expect(depths).toEqual({ wide: false, edge: false, over: true }); + }, +); -it("pairs elements with a long tag and many attributes in a small heap, spelling a selector only once chosen", async () => { - // Spelling the tag into a selector per attribute needed more than 140 MiB here. - const pairing = await inSmallHeap( - "memberPairing.ts", - "pairMembers", - `const tag = "t".repeat(40_000); +it( + "pairs elements with a long tag and many attributes in a small heap, spelling a selector only once chosen", + { tags: ["long-running"] }, + async () => { + // Spelling the tag into a selector per attribute needed more than 140 MiB here. + const pairing = await inSmallHeap( + "memberPairing.ts", + "pairMembers", + `const tag = "t".repeat(40_000); const element = (id) => { const attributes = { id }; for (let index = 0; index < 2000; index += 1) attributes["a" + index] = String(index); @@ -62,21 +69,25 @@ const members = [element("1"), element("2")]; const { newer, older } = pairMembers(members, members, undefined, "xml"); console.log(JSON.stringify({ keys: newer.keys.map((key) => key.replace(tag, "T")), partners: newer.partners, same: older.keys.every((key, index) => key === newer.keys[index]) })); `, - ); + ); - expect(pairing).toEqual({ - keys: ["[name=T][@id=1]", "[name=T][@id=2]"], - partners: [0, 1], - same: true, - }); -}, 180_000); + expect(pairing).toEqual({ + keys: ["[name=T][@id=1]", "[name=T][@id=2]"], + partners: [0, 1], + same: true, + }); + }, +); -it("refuses JSON over the parsed-view limit in a small heap, telling it apart from text without building it", async () => { - // Parsing the entry to detect its format needed more than 80 MiB here. - const entries = await inSmallHeap( - "zipView.ts", - "zipEntry", - `const read = (name, text) => { +it( + "refuses JSON over the parsed-view limit in a small heap, telling it apart from text without building it", + { tags: ["long-running"] }, + async () => { + // Parsing the entry to detect its format needed more than 80 MiB here. + const entries = await inSmallHeap( + "zipView.ts", + "zipEntry", + `const read = (name, text) => { const bytes = new TextEncoder().encode(text); const entry = zipEntry({ name, size: bytes.length, compressedSize: bytes.length, method: 0, encrypted: false }, bytes); return [entry.format, entry.view_error ?? entry.view.line_count]; @@ -86,20 +97,24 @@ console.log(JSON.stringify([ read("objects.txt", "[{}" + ",{}".repeat(1_400_000) + "}"), ])); `, - ); + ); - expect(entries).toEqual([ - ["json", "larger than the 4 MiB limit for JSON and key file views"], - ["text", 1], - ]); -}, 180_000); + expect(entries).toEqual([ + ["json", "larger than the 4 MiB limit for JSON and key file views"], + ["text", 1], + ]); + }, +); -it("scrubs UTF-16 XML and JSON seeds of a million elements in a small heap, telling their format without building them", async () => { - // Building the XML document to choose the scrubber needed more than 768 MiB here, and parsing the JSON more than 64 MiB; the syntax checks and copying only changes run in 24 MiB. - const outcomes = await inSmallHeap( - "seedScrub.ts", - "scrubSeed", - `const scrub = (home, text, tail, clean) => { +it( + "scrubs UTF-16 XML and JSON seeds of a million elements in a small heap, telling their format without building them", + { tags: ["long-running"] }, + async () => { + // Building the XML document to choose the scrubber needed more than 768 MiB here, and parsing the JSON more than 64 MiB; the syntax checks and copying only changes run in 24 MiB. + const outcomes = await inSmallHeap( + "seedScrub.ts", + "scrubSeed", + `const scrub = (home, text, tail, clean) => { const seed = Buffer.from("\\uFEFF" + text + tail, "utf16le"); const scrubbed = scrubSeed(seed, { home, format: "binary" }, { home: "/home/ada", hostname: "e81232be457d", user: "ada" }); const expected = Buffer.concat([seed.subarray(0, -2 * tail.length), Buffer.from(clean, "utf16le")]); @@ -110,20 +125,24 @@ console.log(JSON.stringify([ scrub(".config/wide.json", "[{}" + ",{}".repeat(1_000_000), ',"ada@e81232be457d"]', ',"user@localhost"]'), ])); `, - ); + ); - expect(outcomes).toEqual([ - { exact: true, holdsIdentity: false }, - { exact: true, holdsIdentity: false }, - ]); -}, 180_000); + expect(outcomes).toEqual([ + { exact: true, holdsIdentity: false }, + { exact: true, holdsIdentity: false }, + ]); + }, +); -it("compares objects of selector keys with arrays in a small heap, however many field sets or spellings they use", async () => { - // An index per field set or spelling needed more than 48 MiB for either comparison here. - const removed = await inSmallHeap( - "stateChanges.ts", - "diffStateViews", - `const fields = ["f0", "f1", "f2", "f3", "f4", "f5", "f6"]; +it( + "compares objects of selector keys with arrays in a small heap, however many field sets or spellings they use", + { tags: ["long-running"] }, + async () => { + // An index per field set or spelling needed more than 48 MiB for either comparison here. + const removed = await inSmallHeap( + "stateChanges.ts", + "diffStateViews", + `const fields = ["f0", "f1", "f2", "f3", "f4", "f5", "f6"]; const members = Array.from({ length: 5000 }, (_, i) => Object.fromEntries(fields.map((f) => [f, f + "-" + i]))); const combinations = {}; for (let mask = 1; mask < 128; mask += 1) @@ -136,7 +155,8 @@ console.log(JSON.stringify([ diffStateViews("json", { items: repeated }, { items: named }), ].map((difference) => difference.removed_count))); `, - ); + ); - expect(removed).toEqual([0, 0]); -}, 180_000); + expect(removed).toEqual([0, 0]); + }, +); diff --git a/tests/boundary/providers/desktopTaskPython.test.ts b/tests/boundary/providers/desktopTaskPython.test.ts index 2241097d88..ed6b620ae3 100644 --- a/tests/boundary/providers/desktopTaskPython.test.ts +++ b/tests/boundary/providers/desktopTaskPython.test.ts @@ -298,6 +298,38 @@ it("evaluates every kind of file check without computer-exec as REA does", async ); }); +it("decodes JSON pointer escapes once in the exported verifier", async () => { + const checks: DesktopTaskCheck[] = [ + { + id: "tilde", + kind: "value_equals", + file: "settings.json", + pointer: "/~01", + value: "literal", + }, + { + id: "slash", + kind: "value_equals", + file: "settings.json", + pointer: "/~1", + value: "slash", + }, + ]; + const views = { + "settings.json": { + format: "json" as const, + view: { "~1": "literal", "/": "slash" }, + truncated: false, + }, + }; + const output = await runVerifier(checks, [[0, views]], [], { exec: false }); + const expected = evaluateTaskChecks(checks, new Map(Object.entries(views))); + expect(output.results.map(({ id, result }) => [id, result])).toEqual( + expected.map(({ id, passed }) => [id, passed]), + ); + expect(output.results.map(({ result }) => result)).toEqual([true, true]); +}); + it("grades an app that saves seconds after the agent stops on what it saved", async () => { // Gnote saved 2.2 s after the verifier's first read in the run this missed. const saved = 2.2; diff --git a/tests/boundary/providers/desktopZipHeap.test.ts b/tests/boundary/providers/desktopZipHeap.test.ts index 498735ca4e..23389cf73b 100644 --- a/tests/boundary/providers/desktopZipHeap.test.ts +++ b/tests/boundary/providers/desktopZipHeap.test.ts @@ -10,41 +10,45 @@ import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; const ROOT = join(import.meta.dirname, "../../.."); -it("reads a 600,000-entry archive's view in a small heap, parsing no more of its directory than it lists", async () => { - const folder = await createTestTempDirectory("desktop-zip-heap-"); - const archive = join(folder, "wide.zip"); - await writeFile(archive, emptyEntriesArchive(600_000)); - const script = join(folder, "read.mjs"); - const reader = pathToFileURL( - join(ROOT, "src/application/desktop/DesktopZipView.ts"), - ).href; - await writeFile( - script, - `import { readFileSync } from "node:fs"; +it( + "reads a 600,000-entry archive's view in a small heap, parsing no more of its directory than it lists", + { tags: ["long-running"] }, + async () => { + const folder = await createTestTempDirectory("desktop-zip-heap-"); + const archive = join(folder, "wide.zip"); + await writeFile(archive, emptyEntriesArchive(600_000)); + const script = join(folder, "read.mjs"); + const reader = pathToFileURL( + join(ROOT, "src/application/desktop/DesktopZipView.ts"), + ).href; + await writeFile( + script, + `import { readFileSync } from "node:fs"; import { readZipView } from ${JSON.stringify(reader)}; const read = await readZipView(readFileSync(process.argv[2])); if (!read.ok) throw new Error(read.error); console.log(JSON.stringify({ listed: read.value.entries.length, count: read.value.entry_count, truncated: read.value.truncated })); `, - ); + ); - // zip.js parsing every record needed between 48 and 64 MiB here; the bounded directory reads in 24 MiB. - const output = execFileSync( - process.execPath, - [ - "--max-old-space-size=36", - "--max-semi-space-size=1", - "--import", - "tsx", - script, - archive, - ], - { cwd: ROOT, encoding: "utf8", timeout: 120_000 }, - ); + // zip.js parsing every record needed between 48 and 64 MiB here; the bounded directory reads in 24 MiB. + const output = execFileSync( + process.execPath, + [ + "--max-old-space-size=36", + "--max-semi-space-size=1", + "--import", + "tsx", + script, + archive, + ], + { cwd: ROOT, encoding: "utf8", timeout: 120_000 }, + ); - expect(JSON.parse(output)).toEqual({ - listed: 1000, - count: 600_000, - truncated: true, - }); -}, 180_000); + expect(JSON.parse(output)).toEqual({ + listed: 1000, + count: 600_000, + truncated: true, + }); + }, +); diff --git a/tests/boundary/providers/desktopZipPython.test.ts b/tests/boundary/providers/desktopZipPython.test.ts index 45adf9c122..fb9342c28d 100644 --- a/tests/boundary/providers/desktopZipPython.test.ts +++ b/tests/boundary/providers/desktopZipPython.test.ts @@ -356,7 +356,7 @@ describe("zip entries the exported module cannot fully read", () => { ); // Parsing the 4,200,004-byte entry before refusing it traced about 110 MB here. expect(peak).toBeLessThan(4 * objects.length); - }, 60_000); + }); it("keeps a JSON entry nested too deeply on that entry and reads its siblings, as REA does", async () => { const nested = (levels: number) => diff --git a/tests/boundary/providers/ghidra/ghidraClient.test.ts b/tests/boundary/providers/ghidra/ghidraClient.test.ts index 6b80daa975..cd0d013804 100644 --- a/tests/boundary/providers/ghidra/ghidraClient.test.ts +++ b/tests/boundary/providers/ghidra/ghidraClient.test.ts @@ -39,10 +39,7 @@ import { GhidraLaunchError, } from "../../../../src/ghidra/GhidraLauncher.js"; import type { GhidraDiagnostic } from "../../../../src/ghidra/GhidraClientTypes.js"; -import type { - GhidraLaunchSession, - GhidraLauncher, -} from "../../../../src/ghidra/GhidraLauncher.js"; +import type { GhidraLauncher } from "../../../../src/ghidra/GhidraLauncher.js"; import type { ProviderProcessHandle } from "../../../../src/process/ProviderProcess.js"; import type { GhidraTransportKind } from "../../../../src/ghidra/GhidraTransport.js"; import { diff --git a/tests/boundary/providers/ghidra/ghidraJavaLaunch.test.ts b/tests/boundary/providers/ghidra/ghidraJavaLaunch.test.ts index e63bdf687c..6e36174217 100644 --- a/tests/boundary/providers/ghidra/ghidraJavaLaunch.test.ts +++ b/tests/boundary/providers/ghidra/ghidraJavaLaunch.test.ts @@ -781,7 +781,7 @@ describe.skipIf(process.platform === "win32")( expect(result.ok).toBe(false); expect(analysisArguments).toEqual([]); expect(resources.hasPending).toBe(false); - }, 5000); + }); }, ); diff --git a/tests/boundary/providers/ghidra/ghidraOomRetention.test.ts b/tests/boundary/providers/ghidra/ghidraOomRetention.test.ts index 45dc30453d..e293e26c0b 100644 --- a/tests/boundary/providers/ghidra/ghidraOomRetention.test.ts +++ b/tests/boundary/providers/ghidra/ghidraOomRetention.test.ts @@ -144,7 +144,7 @@ describe.skipIf(process.platform === "win32")( }, }, }); - }, 30_000); + }); it.each([ ["process exit", "flood_then_exit_tools"], @@ -167,7 +167,6 @@ describe.skipIf(process.platform === "win32")( diagnostics: { failure_output: { observed_oome: [] } }, }); }, - 30_000, ); it("does not mistake a budget-cut identifier prefix for an OOME", async () => { @@ -190,7 +189,7 @@ describe.skipIf(process.platform === "win32")( }, }, }); - }, 30_000); + }); it("keeps a startup timeout after budget-exhausting output", async () => { const projected = await failedOperation("flood_silent", { @@ -201,7 +200,7 @@ describe.skipIf(process.platform === "win32")( expect(projected.remediation.action).not.toContain( "GHIDRA_HEADLESS_MAXMEM", ); - }, 30_000); + }); }, ); diff --git a/tests/boundary/providers/hopper/hopperClient.cleanup.test.ts b/tests/boundary/providers/hopper/hopperClient.cleanup.test.ts index cc4a8d1ff6..48bd19bffd 100644 --- a/tests/boundary/providers/hopper/hopperClient.cleanup.test.ts +++ b/tests/boundary/providers/hopper/hopperClient.cleanup.test.ts @@ -56,7 +56,7 @@ const clients: HopperClient[] = []; afterEach(async () => { await Promise.all(clients.splice(0).map((client) => client.close())); -}, 45_000); +}); describe("HopperClient cleanup", () => { it("waits for an operation reply without a fixed request deadline", async () => { diff --git a/tests/boundary/providers/hopper/hopperClient.diagnostics.test.ts b/tests/boundary/providers/hopper/hopperClient.diagnostics.test.ts index 833da518d2..5b89ef4d21 100644 --- a/tests/boundary/providers/hopper/hopperClient.diagnostics.test.ts +++ b/tests/boundary/providers/hopper/hopperClient.diagnostics.test.ts @@ -3,12 +3,11 @@ import { access } from "node:fs/promises"; import { expect, it, onTestFinished, vi } from "vitest"; import { HopperTimeoutError } from "../../../../src/domain/hopperErrors.js"; -import { - HopperClient, - HOPPER_PROCESS_DIAGNOSTIC_BYTES, -} from "../../../../src/hopper/HopperClient.js"; +import { HopperClient } from "../../../../src/hopper/HopperClient.js"; import { HopperFixtureLauncher } from "./hopperClient.fixture.js"; +const HOPPER_PROCESS_DIAGNOSTIC_BYTES = 8 * 1024 * 1024; + const expectStoppedLauncher = (launcher: HopperFixtureLauncher): void => { const child = launcher.processes[0]; if (child === undefined) throw new Error("Diagnostic fixture did not launch"); diff --git a/tests/boundary/providers/hopper/hopperClient.externalExit.test.ts b/tests/boundary/providers/hopper/hopperClient.externalExit.test.ts index d65dc07a63..23aa8994e8 100644 --- a/tests/boundary/providers/hopper/hopperClient.externalExit.test.ts +++ b/tests/boundary/providers/hopper/hopperClient.externalExit.test.ts @@ -1,14 +1,12 @@ -import { spawn, type ChildProcess } from "node:child_process"; +import type { ChildProcess } from "node:child_process"; import { once } from "node:events"; import { rm } from "node:fs/promises"; -import { join } from "node:path"; import { expect, it, onTestFinished } from "vitest"; import { createAnalysisExecution, type AnalysisClient, - type AnalysisOperation, type AnalysisProviderCandidate, type CapabilityDescriptor, type ProviderIdentity, @@ -89,8 +87,7 @@ const makeCandidate = ( }; const fixtureAnalysisClient = (identity: ProviderIdentity): AnalysisClient => ({ - execute: (operation: AnalysisOperation) => - Promise.resolve(ok(createAnalysisExecution(null, identity))), + execute: () => Promise.resolve(ok(createAnalysisExecution(null, identity))), close: () => Promise.resolve(ok(null)), }); @@ -132,7 +129,7 @@ class ExternalFixtureBridge { rm(directory, { recursive: true, force: true }), ), ); - }, 45_000); + }); } } diff --git a/tests/boundary/providers/hopper/hopperClient.handshakeRecovery.test.ts b/tests/boundary/providers/hopper/hopperClient.handshakeRecovery.test.ts index faf69e347a..af21f16974 100644 --- a/tests/boundary/providers/hopper/hopperClient.handshakeRecovery.test.ts +++ b/tests/boundary/providers/hopper/hopperClient.handshakeRecovery.test.ts @@ -82,7 +82,7 @@ const setup = ( rm(directory, { recursive: true, force: true }), ), ); - }, 45_000); + }); return { client, launcher }; }; diff --git a/tests/boundary/providers/hopper/hopperClient.health.test.ts b/tests/boundary/providers/hopper/hopperClient.health.test.ts index f8267c7e86..b5284676ad 100644 --- a/tests/boundary/providers/hopper/hopperClient.health.test.ts +++ b/tests/boundary/providers/hopper/hopperClient.health.test.ts @@ -2,6 +2,7 @@ import { EventEmitter } from "node:events"; import { PassThrough } from "node:stream"; import { describe, expect, it, onTestFinished, vi } from "vitest"; +import { awaitAcknowledgement } from "../../../fixtures/lifecycleAcknowledgement.js"; import { projectAnalysisError } from "../../../../src/domain/analysisErrorProjection.js"; import { ok } from "../../../../src/domain/result.js"; @@ -59,7 +60,7 @@ const start = async (owned: boolean) => { onTestFinished(async () => { await client.close(); for (const child of launcher.fixture.processes) child.kill(); - }, 45_000); + }); expect((await client.start()).ok).toBe(true); return { client, launcher }; }; @@ -89,7 +90,7 @@ it("reports launch work until the first authenticated handshake completes", asyn await client.close(); }); const starting = client.callTool("current_document"); - await entered; + await awaitAcknowledgement(entered, starting, "Hopper launch", 10_000); expect(client.requestActivity()).toBeNull(); expect(client.operationHealth()).toMatchObject({ state: "busy", diff --git a/tests/boundary/providers/hopper/hopperClient.startupFailures.test.ts b/tests/boundary/providers/hopper/hopperClient.startupFailures.test.ts index 03b64fbd32..9a0c329421 100644 --- a/tests/boundary/providers/hopper/hopperClient.startupFailures.test.ts +++ b/tests/boundary/providers/hopper/hopperClient.startupFailures.test.ts @@ -1,6 +1,6 @@ import { spawn, type ChildProcess } from "node:child_process"; import { access } from "node:fs/promises"; -import { createServer, type Server } from "node:net"; +import { createServer } from "node:net"; import { afterEach, describe, expect, it } from "vitest"; @@ -129,9 +129,6 @@ class LateSilentLauncher implements BridgeLauncher { * auth/preamble request added before it would hit the -32601 branch. */ class BadHealthLauncher implements BridgeLauncher { - #server: Server | undefined; - #process: ChildProcess | undefined; - async launch(session: BridgeSession) { const server = createServer((socket) => { let buffer = ""; @@ -173,7 +170,6 @@ class BadHealthLauncher implements BridgeLauncher { resolve(); }); }); - this.#server = server; const child = spawn( process.execPath, ["-e", "setInterval(() => {}, 1000)"], @@ -181,7 +177,6 @@ class BadHealthLauncher implements BridgeLauncher { stdio: "ignore", }, ); - this.#process = child; return ok({ process: child, ownsProcessLifetime: true as const, @@ -200,7 +195,7 @@ const clients: HopperClient[] = []; afterEach(async () => { await Promise.all(clients.splice(0).map((client) => client.close())); -}, 45_000); +}); describe("HopperClient startup deadline", () => { it("applies one deadline to launcher, socket, and health startup phases", async () => { diff --git a/tests/boundary/providers/hopper/hopperClientObservedExitPriority.test.ts b/tests/boundary/providers/hopper/hopperClientObservedExitPriority.test.ts index bffe579bfc..0a856f5661 100644 --- a/tests/boundary/providers/hopper/hopperClientObservedExitPriority.test.ts +++ b/tests/boundary/providers/hopper/hopperClientObservedExitPriority.test.ts @@ -91,12 +91,12 @@ describe("Hopper startup failure priority", () => { if (!(error instanceof HopperProcessError)) return; expect(error.exitCode).toBe(3); expect(error.stage).toBe("launch"); - }, 30_000); + }); it("reports the same launcher exit without any cancellation", async () => { const error = await start("never"); expect(error._tag).toBe("HopperProcessError"); - }, 30_000); + }); it("keeps cancellation first when its abort handler causes the later launcher exit", async () => { const controller = new AbortController(); @@ -117,5 +117,5 @@ describe("Hopper startup failure priority", () => { } finally { await client.close().catch(() => undefined); } - }, 30_000); + }); }); diff --git a/tests/boundary/providers/ida/verifierObservations.test.ts b/tests/boundary/providers/ida/verifierObservations.test.ts index d9d0abe2ec..dcb2c64e5b 100644 --- a/tests/boundary/providers/ida/verifierObservations.test.ts +++ b/tests/boundary/providers/ida/verifierObservations.test.ts @@ -305,5 +305,4 @@ it.each([ await upstream.close(cleanupConfirmed); } }, - 120000, ); diff --git a/tests/boundary/providers/kuna/kunaBatchDecompileMcp.test.ts b/tests/boundary/providers/kuna/kunaBatchDecompileMcp.test.ts index 2c5cac6d23..577e5dddd3 100644 --- a/tests/boundary/providers/kuna/kunaBatchDecompileMcp.test.ts +++ b/tests/boundary/providers/kuna/kunaBatchDecompileMcp.test.ts @@ -192,4 +192,4 @@ test("keeps each Kuna no-pseudocode observation's limitations on its batch item" expect(await session.close()).toEqual({ ok: true, value: null }); }, ]); -}, 60_000); +}); diff --git a/tests/boundary/providers/kuna/kunaDirectoryIndependentRetryMcp.test.ts b/tests/boundary/providers/kuna/kunaDirectoryIndependentRetryMcp.test.ts index 10f8b050c0..80c8a68908 100644 --- a/tests/boundary/providers/kuna/kunaDirectoryIndependentRetryMcp.test.ts +++ b/tests/boundary/providers/kuna/kunaDirectoryIndependentRetryMcp.test.ts @@ -7,7 +7,7 @@ import { parseEvidence } from "../../../../src/domain/evidence.js"; import { KunaProvider } from "../../../../src/kuna/KunaProvider.js"; import { elfNoteFixture } from "../../../../src/native/elf/ElfNotes.fixture.js"; import { createApplicationMcpHarness } from "../../../fixtures/applicationMcpHarness.js"; -import { advertisedMcpToolSchemas } from "../../../fixtures/advertisedMcpTool.js"; +import { callAdvertisedMcpTool } from "../../../fixtures/advertisedMcpTool.js"; import { createMcpSchemaValidation } from "../../../fixtures/mcpSchemaValidation.js"; import { awaitAcknowledgement } from "../../../fixtures/lifecycleAcknowledgement.js"; import { createTestTempDirectory } from "../../../fixtures/temporaryDirectory.js"; @@ -71,23 +71,8 @@ it.each(["compact", "full"] as const)( }, }); try { - const call = async (name: string, args: Record) => { - const schema = await advertisedMcpToolSchemas( - harness.client, - name, - schemas.assertions, - ); - const result = await harness.client.callTool( - { name, arguments: args }, - { toolDefinition: schema.toolDefinition }, - ); - expect(result.isError, JSON.stringify(result)).not.toBe(true); - expect( - schema.ajv.validate(schema.outputSchema, result.structuredContent), - JSON.stringify(schema.ajv.errors), - ).toBe(true); - return result.structuredContent; - }; + const call = (name: string, args: Record) => + callAdvertisedMcpTool(harness.client, name, args, schemas.assertions); await call("open_binary", { path: target, provider_id: "kuna" }); await call("procedure_assembly", { procedure: "main" }); const initial = parseEvidence( diff --git a/tests/boundary/providers/kuna/kunaIndependentDecompileMcp.test.ts b/tests/boundary/providers/kuna/kunaIndependentDecompileMcp.test.ts index ecb5e614e1..f0e68bb690 100644 --- a/tests/boundary/providers/kuna/kunaIndependentDecompileMcp.test.ts +++ b/tests/boundary/providers/kuna/kunaIndependentDecompileMcp.test.ts @@ -8,7 +8,7 @@ import { KunaProvider } from "../../../../src/kuna/KunaProvider.js"; import { KunaRunSlots } from "../../../../src/kuna/KunaRunSlots.js"; import { elfNoteFixture } from "../../../../src/native/elf/ElfNotes.fixture.js"; import { createApplicationMcpHarness } from "../../../fixtures/applicationMcpHarness.js"; -import { advertisedMcpToolSchemas } from "../../../fixtures/advertisedMcpTool.js"; +import { callAdvertisedMcpTool } from "../../../fixtures/advertisedMcpTool.js"; import { createMcpSchemaValidation } from "../../../fixtures/mcpSchemaValidation.js"; import { awaitAcknowledgement } from "../../../fixtures/lifecycleAcknowledgement.js"; import { createTestTempDirectory } from "../../../fixtures/temporaryDirectory.js"; @@ -145,23 +145,8 @@ it.each(["compact", "full"] as const)( }, }); try { - const call = async (name: string, args: Record) => { - const schema = await advertisedMcpToolSchemas( - harness.client, - name, - schemas.assertions, - ); - const result = await harness.client.callTool( - { name, arguments: args }, - { toolDefinition: schema.toolDefinition }, - ); - expect(result.isError, JSON.stringify(result)).not.toBe(true); - expect( - schema.ajv.validate(schema.outputSchema, result.structuredContent), - JSON.stringify(schema.ajv.errors), - ).toBe(true); - return result.structuredContent; - }; + const call = (name: string, args: Record) => + callAdvertisedMcpTool(harness.client, name, args, schemas.assertions); await call("open_binary", { path: target, provider_id: "kuna" }); await call("procedure_assembly", { procedure: "broken" }); const original = parseEvidence( diff --git a/tests/boundary/providers/kuna/kunaInstallation.test.ts b/tests/boundary/providers/kuna/kunaInstallation.test.ts index 61b58e7f95..03322223ee 100644 --- a/tests/boundary/providers/kuna/kunaInstallation.test.ts +++ b/tests/boundary/providers/kuna/kunaInstallation.test.ts @@ -317,7 +317,7 @@ it("stops a version probe that ignores SIGTERM instead of waiting on it", async expect(Date.now() - started).toBeLessThan(10_000); const pid = (await fakeKunaCalls(kuna))[0]?.pid ?? 0; expect(() => process.kill(pid, 0)).toThrow(); -}, 20_000); +}); it("keeps a version deadline ahead of cancellation during its joined stop", async () => { const root = await createTestTempDirectory("rea-kuna-deadline-first-"); diff --git a/tests/boundary/providers/kuna/kunaNavigationAlternativesMcp.test.ts b/tests/boundary/providers/kuna/kunaNavigationAlternativesMcp.test.ts new file mode 100644 index 0000000000..2fc8758068 --- /dev/null +++ b/tests/boundary/providers/kuna/kunaNavigationAlternativesMcp.test.ts @@ -0,0 +1,113 @@ +import { writeFile } from "node:fs/promises"; +import { join } from "node:path"; + +import { expect } from "vitest"; + +import { AnalysisProviderRegistry } from "../../../../src/application/binary/AnalysisProviderRegistry.js"; +import { offeredOperationAlternatives } from "../../../../src/application/OperationAlternatives.js"; +import { composeBinarySession } from "../../../../src/application/binary/BinarySessionComposition.js"; +import { toolContract } from "../../../../src/contracts/toolContracts.js"; +import { expandToolAvailability } from "../../../../src/contracts/toolOutputSchemaPrimitives.js"; +import { parseEvidence } from "../../../../src/domain/evidence.js"; +import { KunaProvider } from "../../../../src/kuna/KunaProvider.js"; +import { elfNoteFixture } from "../../../../src/native/elf/ElfNotes.fixture.js"; +import { createServer } from "../../../../src/server/createServer.js"; +import { parseMcpToolError } from "../../../fixtures/mcpToolError.js"; +import { createTestTempDirectory } from "../../../fixtures/temporaryDirectory.js"; +import { mcpTest } from "../../../support/mcp/mcpFixture.js"; +import { writeFakeKuna } from "./fakeKuna.js"; +import { FUNCTIONS } from "./kunaSessionFixture.js"; + +const test = mcpTest.skipIf(process.platform === "win32"); + +test("names only Kuna's assembly text for navigation refusals in a scoped catalog", async ({ + mcp, +}) => { + const root = await createTestTempDirectory("rea-kuna-navigation-mcp-"); + const kuna = await writeFakeKuna(root, { functions: FUNCTIONS }); + const target = join(root, "program"); + await writeFile(target, elfNoteFixture({ bits: 64, type: 2 })); + const session = composeBinarySession( + new AnalysisProviderRegistry( + [ + new KunaProvider( + { REA_KUNA_COMMAND: kuna, REA_KUNA_CACHE_DIR: join(root, "cache") }, + { platform: process.platform }, + ), + ], + "kuna", + ), + ); + try { + const client = await mcp.connect( + createServer( + { kind: "session", session }, + { environment: { REA_MCP_TOOL_FAMILIES: "session,direct" } }, + ), + ); + const opened = await client.callTool({ + name: "open_binary", + arguments: { path: target }, + }); + expect(opened.isError).not.toBe(true); + + for (const name of ["next_address", "prev_address"]) { + const { error } = parseMcpToolError( + await client.callTool({ name, arguments: { address: "0x1000" } }), + ); + expect(error).toMatchObject({ + code: "capability_unavailable", + category: "unsupported_provider", + retryable: false, + remediation: { + action: offeredOperationAlternatives( + name, + (alternative) => alternative === "procedure_assembly", + )?.remediation, + }, + details: { + provider_id: "kuna", + operation: name, + alternatives: ["procedure_assembly"], + }, + }); + } + + const status = toolContract("binary_session").outputSchema.parse( + ( + await client.callTool({ + name: "binary_session", + arguments: { detail: "complete" }, + }) + ).structuredContent, + ).result; + const tools = expandToolAvailability(status.tool_availability); + for (const name of ["next_address", "prev_address"]) + expect(tools).toContainEqual( + expect.objectContaining({ + name, + reason: "provider_missing", + alternatives: ["procedure_assembly"], + }), + ); + // The structured reads stay unavailable and are not offered. + for (const name of [ + "inspect_native_instruction", + "read_function_instructions", + ]) + expect(tools).toContainEqual( + expect.objectContaining({ name, available: false }), + ); + + // The offered follow-up works on the same connection. + const assembly = await client.callTool({ + name: "procedure_assembly", + arguments: { procedure: "main" }, + }); + expect(assembly.isError).not.toBe(true); + expect(parseEvidence(assembly.structuredContent).provider.id).toBe("kuna"); + } finally { + await mcp.close(); + expect(await session.close()).toEqual({ ok: true, value: null }); + } +}); diff --git a/tests/boundary/providers/kuna/kunaProvider.test.ts b/tests/boundary/providers/kuna/kunaProvider.test.ts index 44f206bca2..cf8713f736 100644 --- a/tests/boundary/providers/kuna/kunaProvider.test.ts +++ b/tests/boundary/providers/kuna/kunaProvider.test.ts @@ -11,6 +11,7 @@ import { import { join } from "node:path"; import { expect, it as test, vi } from "vitest"; +import { awaitAcknowledgement } from "../../../fixtures/lifecycleAcknowledgement.js"; import type { BinaryTarget } from "../../../../src/domain/binaryTargetTypes.js"; import type { JsonValue } from "../../../../src/domain/jsonValue.js"; @@ -749,7 +750,12 @@ it("keeps an overflow as the primary failure when cancellation arrives during fi { signal: caller.signal }, ); try { - await entered; + await awaitAcknowledgement( + entered, + pending, + "Kuna overflow cleanup", + 10_000, + ); caller.abort("cancel during overflow cleanup"); releaseCleanup(); const result = await pending; diff --git a/tests/boundary/providers/kuna/kunaRunOwnership.test.ts b/tests/boundary/providers/kuna/kunaRunOwnership.test.ts index cfd98e7f61..ad97d7e2a3 100644 --- a/tests/boundary/providers/kuna/kunaRunOwnership.test.ts +++ b/tests/boundary/providers/kuna/kunaRunOwnership.test.ts @@ -1,4 +1,5 @@ import { setImmediate } from "node:timers/promises"; +import { awaitAcknowledgement } from "../../../fixtures/lifecycleAcknowledgement.js"; import { expect, it as test, vi } from "vitest"; @@ -48,7 +49,11 @@ it.each(["procedure_callees", "xrefs"] as const)( const text = run(client, "procedure_pseudo_code", { procedure: "main" }); let reference: ReturnType | undefined; try { - await started.promise; + await awaitAcknowledgement( + started.promise, + text, + "Kuna decompile admission", + ); const budget = grants[0]?.owner; expect(budget).toBeDefined(); reference = run( diff --git a/tests/boundary/providers/kuna/kunaVerifier.test.ts b/tests/boundary/providers/kuna/kunaVerifier.test.ts index 022465a4dd..3c9a5ff430 100644 --- a/tests/boundary/providers/kuna/kunaVerifier.test.ts +++ b/tests/boundary/providers/kuna/kunaVerifier.test.ts @@ -109,7 +109,7 @@ it("accepts requested functions distributed across caller targets", async () => cleanup: "complete", caller_target_selection: expectedCallerSelection(targets), }); -}, 45_000); +}); test.skipIf( process.platform === "win32" || process.env.REA_KUNA_COMMAND === undefined, @@ -170,7 +170,6 @@ test.skipIf( ]); expect(await hashes()).toEqual(before); }, - 120_000, ); it("accepts empty caller inventories but requires source-owned fixture literals", async () => { diff --git a/tests/boundary/providers/kuna/kunaVersionProbeOwnership.test.ts b/tests/boundary/providers/kuna/kunaVersionProbeOwnership.test.ts index 0c394bbc8b..bc8ee2eed3 100644 --- a/tests/boundary/providers/kuna/kunaVersionProbeOwnership.test.ts +++ b/tests/boundary/providers/kuna/kunaVersionProbeOwnership.test.ts @@ -315,7 +315,7 @@ it.skipIf(process.platform === "win32")( }); const provider = new KunaProvider( { REA_KUNA_COMMAND: executable, REA_KUNA_CACHE_DIR: join(root, "cache") }, - { launcher: launcher.launcher, probeTimeoutMs: 2_500 }, + { launcher: launcher.launcher }, ); const input = target(join(root, "target")); await writeFile(input.path, "target"); diff --git a/tests/boundary/providers/managed/managedMemberComparison.test.ts b/tests/boundary/providers/managed/managedMemberComparison.test.ts index 191d949347..f4bd516b89 100644 --- a/tests/boundary/providers/managed/managedMemberComparison.test.ts +++ b/tests/boundary/providers/managed/managedMemberComparison.test.ts @@ -79,8 +79,11 @@ it("preserves a selected comparison source digest contradiction through failed c expect(result.error.cleanupIncomplete).toBe(false); } finally { blocked = false; - await resources.close(); - close.mockRestore(); + try { + await resources.close(); + } finally { + close.mockRestore(); + } } }); @@ -841,7 +844,6 @@ describe("managed comparison post-admission read failures", () => { const result = await compareManagedMemberPaths( { leftPath, rightPath }, { - resolveTarget: parseBinaryTarget, readSnapshot: async (path, operation, options) => { if (path === (side === "left" ? leftPath : rightPath)) { const failure = Object.assign(new Error(`read failed: ${code}`), { diff --git a/tests/boundary/providers/native/artifactProvider.universalExtraction.test.ts b/tests/boundary/providers/native/artifactProvider.universalExtraction.test.ts index 80826f614c..db9ec952a7 100644 --- a/tests/boundary/providers/native/artifactProvider.universalExtraction.test.ts +++ b/tests/boundary/providers/native/artifactProvider.universalExtraction.test.ts @@ -9,11 +9,6 @@ import { CPU_ARM64, universalMacho } from "../../../fixtures/universalMacho.js"; import { thinMach } from "../../../../src/domain/binaryTarget.fixture.js"; import { ArtifactReaderFailure } from "../../../../src/artifacts/ArtifactReader.js"; -import { - CPU, - fatImage, - machoImage, -} from "../../../../src/artifacts/apple/MachoImage.fixture.js"; import { parseBinaryTarget } from "../../../../src/application/BinaryTargetResolver.js"; import { artifactExtractionExecutionSchema } from "../../../../src/contracts/artifactToolContracts.js"; diff --git a/tests/boundary/providers/native/keyedArchiveEncoding.test.ts b/tests/boundary/providers/native/keyedArchiveEncoding.test.ts index b62061cbb6..88b5935af3 100644 --- a/tests/boundary/providers/native/keyedArchiveEncoding.test.ts +++ b/tests/boundary/providers/native/keyedArchiveEncoding.test.ts @@ -14,9 +14,9 @@ it.each(["utf8", "utf8-bom", "utf16le", "utf16be", "binary"] as const)( async (encoding) => { const root = await createTestTempDirectory("rea-keyed-encoding-"); const xml = await nativeFixture("archive-xml-encoding/archive.xml"); - const utf16 = Buffer.from(xml.replace("UTF-8", "UTF-16"), "utf16le"); // The binary builder emits type-8 records for UID; CF$UID is XML's convention. const binarySource = xml.replaceAll("CF$UID", "UID"); + const utf16 = Buffer.from(xml.replace("UTF-8", "UTF-16"), "utf16le"); const bytes = encoding === "utf8" ? Buffer.from(xml) @@ -67,8 +67,6 @@ it.each(["utf8-as-utf16", "utf16-as-utf8"] as const)( async (kind) => { const root = await createTestTempDirectory("rea-keyed-encoding-"); const xml = await nativeFixture("archive-xml-encoding/archive.xml"); - // The binary builder emits type-8 records for UID; CF$UID is XML's convention. - const binarySource = xml.replaceAll("CF$UID", "UID"); const bytes = kind === "utf8-as-utf16" ? Buffer.from(xml.replace("UTF-8", "UTF-16")) diff --git a/tests/boundary/providers/native/lldbLauncherBoundary.test.ts b/tests/boundary/providers/native/lldbLauncherBoundary.test.ts index e1c09ff44d..8d3245bb51 100644 --- a/tests/boundary/providers/native/lldbLauncherBoundary.test.ts +++ b/tests/boundary/providers/native/lldbLauncherBoundary.test.ts @@ -207,5 +207,4 @@ setTimeout(() => process.exit(0), 10000); } else expect(closed.ok).toBe(true); } }, - 15_000, ); diff --git a/tests/boundary/providers/native/nativeCallObservation.test.ts b/tests/boundary/providers/native/nativeCallObservation.test.ts index 4447396d79..efd6999f7e 100644 --- a/tests/boundary/providers/native/nativeCallObservation.test.ts +++ b/tests/boundary/providers/native/nativeCallObservation.test.ts @@ -13,7 +13,6 @@ import type { AnalysisError } from "../../../../src/domain/analysisErrorBase.js" import { EvidenceIntegrityError } from "../../../../src/domain/evidenceErrors.js"; import { nativeCallPartialObservationSchema } from "../../../../src/domain/native/nativeCallPartialObservation.js"; import { nativeCallObservationResultSchema } from "../../../../src/domain/native/nativeCallObservation.js"; -import { NATIVE_CALL_BREAKPOINT_LOCATION_LIST_INCOMPLETE_LIMITATION } from "../../../../src/native/NativeCallObservation.js"; import { err, ok, type Result } from "../../../../src/domain/result.js"; import type { NativeCallTrace, @@ -295,8 +294,12 @@ describe("observe_native_calls projection: keeps locations within the metadata b const result = nativeCallObservationResultSchema.parse( observed.value.result, ); - expect(result.limitations).toContain( - NATIVE_CALL_BREAKPOINT_LOCATION_LIST_INCOMPLETE_LIMITATION, + expect(result.limitations).toEqual( + expect.arrayContaining([ + expect.stringMatching( + /breakpoint location list is incomplete.*location_count.*full matching count/iu, + ), + ]), ); }); }); @@ -352,8 +355,12 @@ describe("observe_native_calls projection: bounded breakpoint metadata", () => { "capture_limits.breakpoint_location_bytes (1024 bytes)", ), ); - expect(result.limitations).not.toContain( - NATIVE_CALL_BREAKPOINT_LOCATION_LIST_INCOMPLETE_LIMITATION, + expect(result.limitations).not.toEqual( + expect.arrayContaining([ + expect.stringMatching( + /breakpoint location list is incomplete.*location_count.*full matching count/iu, + ), + ]), ); expect(result.limitations.join(" ")).not.toContain("process was killed"); }); diff --git a/tests/boundary/providers/native/nativeCallTargetCleanup.test.ts b/tests/boundary/providers/native/nativeCallTargetCleanup.test.ts index 524daf8833..8c9360f5df 100644 --- a/tests/boundary/providers/native/nativeCallTargetCleanup.test.ts +++ b/tests/boundary/providers/native/nativeCallTargetCleanup.test.ts @@ -7,6 +7,7 @@ import { nativeMachoTargetForFile, } from "../../../fixtures/nativeCommands.js"; import { createTestTempDirectory } from "../../../fixtures/temporaryDirectory.js"; +import { awaitAcknowledgement } from "../../../fixtures/lifecycleAcknowledgement.js"; import { AnalysisOutputError } from "../../../../src/domain/analysisErrorCore.js"; import { resolvedAnalysisError } from "../../../../src/domain/analysisErrorBase.js"; import { err, ok } from "../../../../src/domain/result.js"; @@ -131,12 +132,15 @@ it("joins admitted preflight before closing the tracer", async () => { ).createClient(target); onTestFinished(async () => { resume(); - await observing; - read.mockRestore(); - await client.close(); + try { + await observing; + } finally { + read.mockRestore(); + await client.close(); + } }); const observing = client.execute("observe_native_calls", parameters); - await entered; + await awaitAcknowledgement(entered, observing, "native target read"); let closed = false; const closing = client.close().then((result) => { closed = true; diff --git a/tests/boundary/providers/native/nativeCommandResolver.test.ts b/tests/boundary/providers/native/nativeCommandResolver.test.ts index ba7d02b845..0d14b80a41 100644 --- a/tests/boundary/providers/native/nativeCommandResolver.test.ts +++ b/tests/boundary/providers/native/nativeCommandResolver.test.ts @@ -2,6 +2,7 @@ import { EventEmitter } from "node:events"; import { PassThrough } from "node:stream"; import { afterEach, expect, it, vi } from "vitest"; +import { awaitAcknowledgement } from "../../../fixtures/lifecycleAcknowledgement.js"; import { resolveXcrunTool, @@ -60,96 +61,130 @@ it("keeps a realpath I/O failure primary when cancellation is observed before re const started = new Promise((resolve) => { realpathStarted = resolve; }); - let rejectRealpath!: (cause: unknown) => void; + let rejectRealpath: ((cause: unknown) => void) | undefined; realpathState.resolve = () => new Promise((_resolve, reject) => { + if (controller.signal.aborted) { + reject(controller.signal.reason); + return; + } rejectRealpath = reject; realpathStarted(); }); const controller = new AbortController(); const cancellationReason = { source: "caller", phase: "tool resolution" }; const pending = resolveXcrunTool("file", {}, controller.signal, launcher); - await started; - controller.abort(cancellationReason); - const ioFailure = Object.assign(new Error("fixture realpath failure"), { - code: "EACCES", - }); - rejectRealpath(ioFailure); + try { + await awaitAcknowledgement(started, pending, "native tool realpath"); + controller.abort(cancellationReason); + const ioFailure = Object.assign(new Error("fixture realpath failure"), { + code: "EACCES", + }); + rejectRealpath?.(ioFailure); - const result = await pending; - if (result.ok) throw new Error("Expected realpath I/O failure"); - expect(result.error).toMatchObject({ - reason: "io", - cancellation: { reason: cancellationReason }, - }); - expect(result.error.cancellation?.reason).toBe(cancellationReason); - expect(result.error.cause).toBe(ioFailure); + const result = await pending; + if (result.ok) throw new Error("Expected realpath I/O failure"); + expect(result.error).toMatchObject({ + reason: "io", + cancellation: { reason: cancellationReason }, + }); + expect(result.error.cancellation?.reason).toBe(cancellationReason); + expect(result.error.cause).toBe(ioFailure); + } finally { + controller.abort(cancellationReason); + rejectRealpath?.(new Error("fixture cleanup")); + await pending; + } }); it("retains exact cancellation observed after successful tool path resolution", async () => { - let finishRealpath!: () => void; + let finishRealpath: (() => void) | undefined; let realpathStarted!: () => void; const started = new Promise((resolve) => { realpathStarted = resolve; }); realpathState.resolve = () => - new Promise((resolve) => { + new Promise((resolve, reject) => { + if (controller.signal.aborted) { + reject(controller.signal.reason); + return; + } finishRealpath = () => resolve("/controlled/physical-tool"); realpathStarted(); }); const controller = new AbortController(); const cancellationReason = { source: "caller", phase: "tool selection" }; const pending = resolveXcrunTool("file", {}, controller.signal, launcher); - await started; - controller.abort(cancellationReason); - finishRealpath(); + try { + await awaitAcknowledgement(started, pending, "native tool realpath"); + controller.abort(cancellationReason); + finishRealpath?.(); - const result = await pending; - if (result.ok) throw new Error("Expected selection cancellation"); - expect(result.error).toMatchObject({ - reason: "cancelled", - cancellation: { reason: cancellationReason }, - }); - expect(result.error.cancellation?.reason).toBe(cancellationReason); - expect(result.error.cause).toMatchObject({ - reason: "cancelled", - cause: cancellationReason, - }); + const result = await pending; + if (result.ok) throw new Error("Expected selection cancellation"); + expect(result.error).toMatchObject({ + reason: "cancelled", + cancellation: { reason: cancellationReason }, + }); + expect(result.error.cancellation?.reason).toBe(cancellationReason); + expect(result.error.cause).toMatchObject({ + reason: "cancelled", + cause: cancellationReason, + }); + } finally { + controller.abort(cancellationReason); + finishRealpath?.(); + await pending; + } }); it.skipIf(process.platform === "win32")( "uses the AbortError cause as the exact tool-hash cancellation fact", async () => { - let finishRealpath!: () => void; + let finishRealpath: (() => void) | undefined; let realpathStarted!: () => void; const started = new Promise((resolve) => { realpathStarted = resolve; }); realpathState.resolve = () => - new Promise((resolve) => { + new Promise((resolve, reject) => { + if (controller.signal.aborted) { + reject(controller.signal.reason); + return; + } finishRealpath = () => resolve("/dev/zero"); realpathStarted(); }); const controller = new AbortController(); const cancellationReason = { source: "caller", phase: "tool hash" }; const pending = resolveXcrunTool("file", {}, controller.signal, launcher); - await started; - finishRealpath(); - setImmediate(() => controller.abort(cancellationReason)); + try { + await awaitAcknowledgement( + started, + pending, + "native tool hash selection", + ); + finishRealpath?.(); + setImmediate(() => controller.abort(cancellationReason)); - const result = await pending; - if (result.ok) throw new Error("Expected hash cancellation"); - expect(result.error).toMatchObject({ - reason: "cancelled", - cancellation: { reason: cancellationReason }, - }); - expect(result.error.cause).toMatchObject({ - name: "AbortError", - code: "ABORT_ERR", - cause: cancellationReason, - }); - expect(result.error.cancellation?.reason).toBe(cancellationReason); - expect(result.error.cause).toBeInstanceOf(Error); - expect((result.error.cause as Error).cause).toBe(cancellationReason); + const result = await pending; + if (result.ok) throw new Error("Expected hash cancellation"); + expect(result.error).toMatchObject({ + reason: "cancelled", + cancellation: { reason: cancellationReason }, + }); + expect(result.error.cause).toMatchObject({ + name: "AbortError", + code: "ABORT_ERR", + cause: cancellationReason, + }); + expect(result.error.cancellation?.reason).toBe(cancellationReason); + expect(result.error.cause).toBeInstanceOf(Error); + expect((result.error.cause as Error).cause).toBe(cancellationReason); + } finally { + controller.abort(cancellationReason); + finishRealpath?.(); + await pending; + } }, ); diff --git a/tests/boundary/providers/native/nativeDmgArtifactReader.test.ts b/tests/boundary/providers/native/nativeDmgArtifactReader.test.ts index 76fda7c2b4..604f465c40 100644 --- a/tests/boundary/providers/native/nativeDmgArtifactReader.test.ts +++ b/tests/boundary/providers/native/nativeDmgArtifactReader.test.ts @@ -459,7 +459,10 @@ describe("native DMG directory ownership", () => { await expect(realpath(mount.root() ?? "")).rejects.toMatchObject({ code: "ENOENT", }); - expect((await iterator.next()).done).toBe(true); + await expect(iterator.next()).rejects.toMatchObject({ + reason: "unavailable", + message: "DMG reader is closed", + }); }); }); @@ -517,7 +520,10 @@ describe("native DMG traversal shutdown", () => { await Promise.resolve(); expect(state.detachAttempts).toBe(4); releaseAdvance(); - expect((await advancing).done).toBe(true); + await expect(advancing).rejects.toMatchObject({ + reason: "unavailable", + message: "DMG reader is closed", + }); await closing; expect(state.detachAttempts).toBe(5); await expect(realpath(state.mountRoot ?? "")).rejects.toMatchObject({ @@ -1500,7 +1506,6 @@ describe("native DMG localized verification diagnostics", () => { ).rejects.toMatchObject({ reason: "unavailable" }); expect(run).toHaveBeenCalledTimes(3); }, - 10_000, ); it("keeps a localized failure without an untranslated status line unavailable", async () => { @@ -1752,7 +1757,6 @@ describe("native DMG real verification diagnostics", () => { expect(Reflect.get(failure.cause, "code")).toBe(1); expect(Reflect.get(failure.cause, "stderr")).toContain("is INVALID"); }, - 60_000, ); }); diff --git a/tests/boundary/providers/native/nativePlistFileCleanup.test.ts b/tests/boundary/providers/native/nativePlistFileCleanup.test.ts index c4f66566c1..0afd7da0ff 100644 --- a/tests/boundary/providers/native/nativePlistFileCleanup.test.ts +++ b/tests/boundary/providers/native/nativePlistFileCleanup.test.ts @@ -9,6 +9,7 @@ import { nativeMachoTargetForFile, } from "../../../fixtures/nativeCommands.js"; import { createTestTempDirectory } from "../../../fixtures/temporaryDirectory.js"; +import { awaitAcknowledgement } from "../../../fixtures/lifecycleAcknowledgement.js"; import { OwnedFileHandle } from "../../../../src/filesystem/OwnedFileHandle.js"; import { ProviderCleanupError } from "../../../../src/domain/providerCleanupError.js"; import { resolvedAnalysisError } from "../../../../src/domain/analysisErrorBase.js"; @@ -388,17 +389,22 @@ it.for([ exitCode: args[1] === "json" ? 1 : 0, }); }); - onTestFinished(() => { + onTestFinished(async () => { resume(); resumeValidation(); - read.mockRestore(); - commands.mockRestore(); + try { + await inspecting; + } finally { + read.mockRestore(); + commands.mockRestore(); + await client.close(); + } }); const inspecting = client.execute("inspect_plist", { path, native_command_timeout_ms: 1500, }); - await entered; + await awaitAcknowledgement(entered, inspecting, "plist file read"); let finished = false; const closing = client.close().then((result) => { finished = true; @@ -411,7 +417,11 @@ it.for([ error: { _tag: "AnalysisCapabilityUnavailableError" }, }); resume(); - await Promise.race([validationEntered, inspecting]); + await awaitAcknowledgement( + validationEntered, + inspecting, + "plist validation", + ); expect(validationStarted).toBe(true); await new Promise((resolve) => setImmediate(resolve)); expect(finished).toBe(false); diff --git a/tests/boundary/providers/native/nativeProvider.test.ts b/tests/boundary/providers/native/nativeProvider.test.ts index f885eb2d3b..e5c437d7ea 100644 --- a/tests/boundary/providers/native/nativeProvider.test.ts +++ b/tests/boundary/providers/native/nativeProvider.test.ts @@ -24,7 +24,7 @@ import { type NativeCommandRunner, } from "../../../../src/native/CommandRunner.js"; import { err, ok } from "../../../../src/domain/result.js"; -import { parseLipoOutput } from "../../../../src/native/parsers/lipo.js"; +import { parseLipoFixture } from "../../../fixtures/lipo.js"; import { dylibCommand, LC, @@ -951,8 +951,12 @@ describe("native late cancellation with cleanup", () => { {}, { signal: controller.signal }, ); - await started; - await cleanupAttempt; + await awaitAcknowledgement(started, pending, "native command start"); + await awaitAcknowledgement( + cleanupAttempt, + pending, + "native cleanup start", + ); controller.abort(cancellationReason); releaseFirstCleanup(); @@ -1143,51 +1147,59 @@ describe("native command cleanup lifecycle", () => { ); const execution = client.execute("list_architectures", {}); - await started; - const closing = client.close(); - await Promise.resolve(); - expect(cleanupRetries).toBe(0); - finishCommand( - err( - new NativeCommandFailure("lipo", "timeout", null, undefined, { - stdout: "partial architecture output", - stderr: "partial diagnostic", - stdoutBytes: 27, - stderrBytes: 19, - exitCode: null, - signal: null, - truncated: false, - }), - ), + const commandResult = err( + new NativeCommandFailure("lipo", "timeout", null, undefined, { + stdout: "partial architecture output", + stderr: "partial diagnostic", + stdoutBytes: 27, + stderrBytes: 19, + exitCode: null, + signal: null, + truncated: false, + }), ); + try { + await awaitAcknowledgement(started, execution, "native command start"); + const closing = client.close(); + await Promise.resolve(); + expect(cleanupRetries).toBe(0); + finishCommand(commandResult); - const [observed, closed] = await Promise.all([execution, closing]); - expect(observed.ok).toBe(false); - expect(closed.ok).toBe(false); - expect(cleanupRetries).toBe(1); - if (!closed.ok) { - expect(closed.error.cleanupResources).toEqual([ - "run:first", - "process-group:1234", - "run:second", - "process-group:5678", - ]); - expect(closed.error).toHaveProperty("diagnostics", { - reason: "Native command process cleanup remains incomplete", - failures: [ - { - reason: "first process group remains live", - resources: ["run:first", "process-group:1234"], - }, - { - reason: "second process group remains live", - resources: ["run:second", "process-group:5678"], - }, - ], - }); + const [observed, closed] = await Promise.all([execution, closing]); + expect(observed.ok).toBe(false); + expect(closed.ok).toBe(false); + expect(cleanupRetries).toBe(1); + if (!closed.ok) { + expect(closed.error.cleanupResources).toEqual([ + "run:first", + "process-group:1234", + "run:second", + "process-group:5678", + ]); + expect(closed.error).toHaveProperty("diagnostics", { + reason: "Native command process cleanup remains incomplete", + failures: [ + { + reason: "first process group remains live", + resources: ["run:first", "process-group:1234"], + }, + { + reason: "second process group remains live", + resources: ["run:second", "process-group:5678"], + }, + ], + }); + } + await expect(client.close()).resolves.toMatchObject({ ok: true }); + expect(cleanupRetries).toBe(2); + } finally { + finishCommand(commandResult); + try { + await execution; + } finally { + await client.close(); + } } - await expect(client.close()).resolves.toMatchObject({ ok: true }); - expect(cleanupRetries).toBe(2); }); }); @@ -1789,11 +1801,11 @@ describe("native macOS provider failures and parsing", () => { }); it("parses thin and universal lipo fixtures", async () => { - expect(parseLipoOutput(await fixture("lipo-thin.txt"))).toMatchObject({ + expect(parseLipoFixture(await fixture("lipo-thin.txt"))).toMatchObject({ format: "thin", architectures: [{ name: "arm64" }], }); - expect(parseLipoOutput(await fixture("lipo-fat.txt"))).toMatchObject({ + expect(parseLipoFixture(await fixture("lipo-fat.txt"))).toMatchObject({ format: "fat", architectures: [ { name: "x86_64", file_offset: 16384, size: 4096 }, @@ -1811,7 +1823,7 @@ describe("native macOS provider failures and parsing", () => { " size 0x10", " align 2^14 (16384)", ].join("\n"); - expect(parseLipoOutput(output).architectures).toMatchObject([ + expect(parseLipoFixture(output).architectures).toMatchObject([ { name: "arm64", file_offset: null, size: null, alignment: 16384 }, ]); }); @@ -1824,11 +1836,11 @@ describe("native macOS provider failures and parsing", () => { " align 2^14 (16384)", ].join("\n"); expect( - parseLipoOutput(output.replace("(16384)", "(8192)")).architectures[0] + parseLipoFixture(output.replace("(16384)", "(8192)")).architectures[0] ?.alignment, ).toBeNull(); expect( - parseLipoOutput(output.replace("2^14 (16384)", "2^14")).architectures[0] + parseLipoFixture(output.replace("2^14 (16384)", "2^14")).architectures[0] ?.alignment, ).toBe(16384); }); diff --git a/tests/boundary/providers/native/nativeSignatureTargetBinding.test.ts b/tests/boundary/providers/native/nativeSignatureTargetBinding.test.ts index 3548c66545..2e8f52547a 100644 --- a/tests/boundary/providers/native/nativeSignatureTargetBinding.test.ts +++ b/tests/boundary/providers/native/nativeSignatureTargetBinding.test.ts @@ -263,7 +263,7 @@ it.each([ const { path, target } = await fixture(); const replay = new NativeFixtureRunner(); const unsigned: NativeCommandRunner = { - async run(tool, args, options) { + async run(tool, args) { const diagnostic = `${args.at(-1)}: code object is not signed at all\n`; const capture = await new NativeFixtureRunner( { codesign: diagnostic }, diff --git a/tests/boundary/providers/native/nativeTextRobustness.test.ts b/tests/boundary/providers/native/nativeTextRobustness.test.ts index c1ee638e81..f4349e6f3c 100644 --- a/tests/boundary/providers/native/nativeTextRobustness.test.ts +++ b/tests/boundary/providers/native/nativeTextRobustness.test.ts @@ -1,7 +1,7 @@ import { expect, it } from "vitest"; import { readPlistJson } from "../../../../src/native/parsers/plist.js"; -import { parseLipoOutput } from "../../../../src/native/parsers/lipo.js"; +import { parseLipoOutputResult } from "../../../../src/native/parsers/lipo.js"; it.each([ "line\nbreak", @@ -11,9 +11,9 @@ it.each([ "retains the reported thin architecture when lipo echoes filename %j", (filename) => { const output = `input file /owned/${filename} is not a fat file\nNon-fat file: /owned/${filename} is architecture: arm64\n`; - expect(parseLipoOutput(output)).toMatchObject({ - format: "thin", - architectures: [{ name: "arm64" }], + expect(parseLipoOutputResult(output)).toMatchObject({ + ok: true, + value: { format: "thin", architectures: [{ name: "arm64" }] }, }); }, ); @@ -37,12 +37,15 @@ it("does not mistake fat slice output for a thin Non-fat file record", () => { " align 2^12 (4096)", "", ].join("\n"); - expect(parseLipoOutput(output)).toMatchObject({ - format: "fat", - architectures: [ - { name: "arm64", file_offset: 16384 }, - { name: "x86_64", file_offset: 16793600 }, - ], + expect(parseLipoOutputResult(output)).toMatchObject({ + ok: true, + value: { + format: "fat", + architectures: [ + { name: "arm64", file_offset: 16384 }, + { name: "x86_64", file_offset: 16793600 }, + ], + }, }); }); diff --git a/tests/boundary/providers/native/nativeUiOffscreenWindow.test.ts b/tests/boundary/providers/native/nativeUiOffscreenWindow.test.ts index a2de197863..841749f113 100644 --- a/tests/boundary/providers/native/nativeUiOffscreenWindow.test.ts +++ b/tests/boundary/providers/native/nativeUiOffscreenWindow.test.ts @@ -61,7 +61,7 @@ const launch = async (bundle: string, name: string) => { // facts need no Accessibility or Screen Recording permission. it.skipIf(process.platform !== "darwin")( "selects a window that exists off screen and still refuses absent and foreign windows", - { timeout: 240_000 }, + { tags: ["long-running"] }, async () => { const root = await createTestTempDirectory("rea-native-ui-offscreen-"); const bundle = join(root, "Fixture.app"); diff --git a/tests/boundary/setup/linuxHopperPublisherVerification.test.ts b/tests/boundary/setup/linuxHopperPublisherVerification.test.ts index 8c9521ed66..f53b3ee9f4 100644 --- a/tests/boundary/setup/linuxHopperPublisherVerification.test.ts +++ b/tests/boundary/setup/linuxHopperPublisherVerification.test.ts @@ -709,7 +709,8 @@ linux( ).toMatchObject({ ok: false, error: { - reason: "integrity", + // The signature verified; only the feed's launcher digest differs. + reason: "launcher_digest_mismatch", message: expect.stringContaining(f.release.main_exe_hash), }, }); diff --git a/tests/boundary/sqlite/sqliteDatabaseBusy.test.ts b/tests/boundary/sqlite/sqliteDatabaseBusy.test.ts index c4779ca5e2..3eb5e7169e 100644 --- a/tests/boundary/sqlite/sqliteDatabaseBusy.test.ts +++ b/tests/boundary/sqlite/sqliteDatabaseBusy.test.ts @@ -159,5 +159,4 @@ it.runIf(process.platform === "win32")( expectRetryableBusy(result.error, database.path); await expectRootsRemoved(roots); }, - 20_000, ); diff --git a/tests/ciTestSequencer.ts b/tests/ciTestSequencer.ts index 47d5eec9aa..ec80212b67 100644 --- a/tests/ciTestSequencer.ts +++ b/tests/ciTestSequencer.ts @@ -60,68 +60,6 @@ export async function readTestDurations( } } -/** - * Case counts from the competing-loader file before it was split: 54 cases - * stayed in the percent-path file and 46 ran in each of the three dual-surface - * shards. History that still names only the original path is that combined - * cost. The shards are retired; one file now runs every loader through stdio - * MCP plus a few compiled CLI parity cases. - */ -const LOADER_HISTORY_SOURCE = - "tests/acceptance/applications/javascriptLiteralPercentPaths.test.ts"; -const LOADER_HISTORY_PARTS = [ - "tests/acceptance/applications/javascriptLiteralLoaderOwnershipEarly.test.ts", - "tests/acceptance/applications/javascriptLiteralLoaderOwnershipMiddle.test.ts", - "tests/acceptance/applications/javascriptLiteralLoaderOwnershipLate.test.ts", -] as const; -const LOADER_HISTORY_CONSOLIDATED = - "tests/acceptance/applications/javascriptLiteralLoaderOwnership.test.ts"; -const LOADER_HISTORY_REMAINDER_CASES = 54; -const LOADER_HISTORY_PART_CASES = 46; -const LOADER_HISTORY_CASES = - LOADER_HISTORY_REMAINDER_CASES + - LOADER_HISTORY_PART_CASES * LOADER_HISTORY_PARTS.length; - -/** - * Charge the consolidated loader file until its own duration is published. - * A pre-split percent-path duration keeps 54/192 and gives 138/192 to the - * consolidated file. Recorded Early/Middle/Late durations are summed instead, - * and a partial set is scaled as three equal shares. That weight is the old - * dual-surface cost, so the first schedule overestimates the demoted file. - * A published consolidated duration stands, including beside retired shards. - */ -export function seedLoaderOwnershipDurations( - durations: ReadonlyMap, -): Map { - if (durations.has(LOADER_HISTORY_CONSOLIDATED)) return new Map(durations); - const recordedParts = LOADER_HISTORY_PARTS.flatMap((path) => { - const duration = durations.get(path); - return duration === undefined ? [] : [duration]; - }); - if (recordedParts.length > 0) { - const seeded = new Map(durations); - const sum = recordedParts.reduce((total, duration) => total + duration, 0); - seeded.set( - LOADER_HISTORY_CONSOLIDATED, - (sum * LOADER_HISTORY_PARTS.length) / recordedParts.length, - ); - return seeded; - } - const combined = durations.get(LOADER_HISTORY_SOURCE); - if (combined === undefined) return new Map(durations); - const seeded = new Map(durations); - const loaderCases = LOADER_HISTORY_CASES - LOADER_HISTORY_REMAINDER_CASES; - seeded.set( - LOADER_HISTORY_SOURCE, - (combined * LOADER_HISTORY_REMAINDER_CASES) / LOADER_HISTORY_CASES, - ); - seeded.set( - LOADER_HISTORY_CONSOLIDATED, - (combined * loaderCases) / LOADER_HISTORY_CASES, - ); - return seeded; -} - type TimedItem = { item: T; key: string; @@ -263,9 +201,8 @@ export class CiTestSequencer extends BaseSequencer { const shard = this.ctx.config.shard; if (!historyPath || !shard) return super.shard(files); const root = this.ctx.config.root; - const recorded = await readTestDurations(historyPath, root); - if (!recorded) return super.shard(files); - const durations = seedLoaderOwnershipDurations(recorded); + const durations = await readTestDurations(historyPath, root); + if (!durations) return super.shard(files); const weighted = files.map((item) => { const path = relative(root, resolve(root, item.moduleId)) diff --git a/tests/composition/analysis-sessions/binarySession.test.ts b/tests/composition/analysis-sessions/binarySession.test.ts index 2260ddee0e..de05296e04 100644 --- a/tests/composition/analysis-sessions/binarySession.test.ts +++ b/tests/composition/analysis-sessions/binarySession.test.ts @@ -1,3 +1,4 @@ +import { awaitAcknowledgement } from "../../fixtures/lifecycleAcknowledgement.js"; import { createAnalysisExecution, type AnalysisClient, @@ -35,7 +36,7 @@ import { writeFile, } from "node:fs/promises"; import { dirname, extname, join } from "node:path"; -import { describe, expect, it, vi } from "vitest"; +import { describe, expect, it, onTestFinished, vi } from "vitest"; const client = (fail = false): AnalysisClient => ({ execute: () => Promise.resolve(fail ? err(new HopperStartError()) : ok(null)), @@ -235,7 +236,15 @@ it("drains session calls and retires analysis after an admitted external mutatio }, (_value, error) => error, ); - await started.promise; + onTestFinished(async () => { + release.resolve(); + try { + await mutation; + } finally { + await session.close(); + } + }); + await awaitAcknowledgement(started.promise, mutation, "external mutation"); const queuedAnalysis = session.execute("address_name", {}); expect(calls).toEqual([]); @@ -894,8 +903,8 @@ describe("snapshot invalidation at mutation dispatch", () => { ...parameters, name: "after", }); - await started.promise; try { + await awaitAcknowledgement(started.promise, writing, "session write"); const during = await session.execute("address_name", parameters); if (!during.ok) throw during.error; expect(during.value.result).toBe("after"); @@ -1713,14 +1722,19 @@ describe("cancellation during profile resolution", () => { openSettled = true; return result; }); - await admitted.promise; - controller.abort(reason); let closeSettled = false; - const closing = session.close().then((result) => { - closeSettled = true; - return result; - }); + let closing: ReturnType | undefined; try { + await awaitAcknowledgement( + admitted.promise, + opening, + "profile admission", + ); + controller.abort(reason); + closing = session.close().then((result) => { + closeSettled = true; + return result; + }); await new Promise((resolve) => setImmediate(resolve)); expect(observedSignal?.reason).toBe(reason); expect(openSettled).toBe(false); @@ -1735,9 +1749,13 @@ describe("cancellation during profile resolution", () => { expect(created).toBe(0); expect(await session.status()).toMatchObject({ cleanup_pending: false }); } finally { + controller.abort(reason); release.resolve(); - await Promise.all([opening, closing]); - await session.close(); + try { + await Promise.all([opening, closing]); + } finally { + await session.close(); + } } }); }); diff --git a/tests/composition/analysis-sessions/binarySessionCleanupRetry.test.ts b/tests/composition/analysis-sessions/binarySessionCleanupRetry.test.ts index 40ac09a36d..5c8e315414 100644 --- a/tests/composition/analysis-sessions/binarySessionCleanupRetry.test.ts +++ b/tests/composition/analysis-sessions/binarySessionCleanupRetry.test.ts @@ -23,6 +23,7 @@ import { createTestBinarySession, } from "../../fixtures/binarySession.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; +import { awaitAcknowledgement } from "../../fixtures/lifecycleAcknowledgement.js"; const CLOSE_RETRY = { operation: "close_binary" } as const; @@ -512,7 +513,7 @@ it("does not start a replacement cancelled while pending cleanup completes", asy allowed = true; const controller = new AbortController(); const opening = session.open(second, { signal: controller.signal }); - await entered.promise; + await awaitAcknowledgement(entered.promise, opening, "session cleanup retry"); controller.abort(); release.resolve(); expect(await opening).toMatchObject({ diff --git a/tests/composition/analysis-sessions/mips-identity.test.ts b/tests/composition/analysis-sessions/mips-identity.test.ts deleted file mode 100644 index cf6c52fb75..0000000000 --- a/tests/composition/analysis-sessions/mips-identity.test.ts +++ /dev/null @@ -1,91 +0,0 @@ -import { describe, expect, it } from "vitest"; -import type { BinaryTarget } from "../../../src/domain/binaryTargetTypes.js"; -import { createAnalysisProfile } from "../../../src/domain/analysisProfile.js"; -import { createEvidence, parseEvidence } from "../../../src/domain/evidence.js"; -import { createEvidenceBundle } from "../../../src/domain/evidenceBundle.js"; -import { - createAnalysisSnapshotEntry, - parseAnalysisSnapshot, - serializeAnalysisSnapshot, - snapshotBinding, - snapshotTarget, - snapshotMatchesProfile, -} from "../../../src/domain/analysisSnapshot.js"; -import { sessionOutputSchemas } from "../../../src/contracts/toolOutputSchemaGroups.js"; - -const target: BinaryTarget = { - path: "/source-owned.elf", - sha256: "a".repeat(64), - kind: "executable", - format: "elf", - architecture: "mips", - availableArchitectures: ["mips"], - mips: { elfClass: 32, byteOrder: "little", type: 2, flags: 0x70001001 }, -}; -const provider = { id: "ghidra", name: "Ghidra", version: "12.1.4" }; -const profile = createAnalysisProfile(provider, { - mips_elf: { elf_class: 32, byte_order: "little", type: 2, flags: 0x70001001 }, -}); - -describe("MIPS identity through lifecycle, Evidence and snapshot consumers", () => { - it("round-trips query bindings without relaxing integrity", () => { - const result = [{ address: "0x20150", value: "rea_mips_entry" }]; - const parameters = {}; - const evidence = createEvidence(target, provider, { - operation: "list_procedures", - parameters, - result, - analysisProfile: profile, - }); - const binding = snapshotBinding(profile); - const entry = createAnalysisSnapshotEntry({ - target: snapshotTarget(target), - binding, - operation: "list_procedures", - parameters, - execution: { - result, - rawResult: null, - provider, - analysisProfile: profile, - limitations: [], - locations: [], - subject: target, - }, - }); - const snapshot = { - target: snapshotTarget(target), - binding, - entries: [entry], - workflow_entries: [], - evidence_bundle: createEvidenceBundle([evidence]), - }; - const restored = parseAnalysisSnapshot( - JSON.parse(serializeAnalysisSnapshot(snapshot)), - ); - expect(restored.target.architecture).toBe("mips"); - expect(restored.evidence_bundle.records[0]?.subject?.architecture).toBe( - "mips", - ); - expect(restored.entries[0]?.execution.subject?.architecture).toBe("mips"); - expect( - sessionOutputSchemas.open_binary.parse({ result: target }).result - .architecture, - ).toBe("mips"); - expect(() => - parseEvidence({ - ...evidence, - subject: { ...evidence.subject, architecture: "x86" }, - }), - ).toThrow("semantic identifier"); - const changed = createAnalysisProfile(provider, { - mips_elf: { - elf_class: 32, - byte_order: "big", - type: 2, - flags: 0x70001001, - }, - }); - expect(snapshotMatchesProfile(binding, changed)).toBe(false); - }); -}); diff --git a/tests/composition/analysis-sessions/powerpc-identity.test.ts b/tests/composition/analysis-sessions/nativeArchitectureIdentity.test.ts similarity index 56% rename from tests/composition/analysis-sessions/powerpc-identity.test.ts rename to tests/composition/analysis-sessions/nativeArchitectureIdentity.test.ts index 3dd1d374a2..30ed31a8b9 100644 --- a/tests/composition/analysis-sessions/powerpc-identity.test.ts +++ b/tests/composition/analysis-sessions/nativeArchitectureIdentity.test.ts @@ -1,4 +1,4 @@ -import { describe, expect, it } from "vitest"; +import { expect, it } from "vitest"; import type { BinaryTarget } from "../../../src/domain/binaryTargetTypes.js"; import { createAnalysisProfile } from "../../../src/domain/analysisProfile.js"; import { createEvidence, parseEvidence } from "../../../src/domain/evidence.js"; @@ -13,23 +13,80 @@ import { } from "../../../src/domain/analysisSnapshot.js"; import { sessionOutputSchemas } from "../../../src/contracts/toolOutputSchemaGroups.js"; -const target: BinaryTarget = { +const provider = { id: "ghidra", name: "Ghidra", version: "12.1.4" }; +const commonTarget = { path: "/source-owned.elf", sha256: "a".repeat(64), kind: "executable", format: "elf", - architecture: "powerpc", - availableArchitectures: ["powerpc"], - powerpc: { elfClass: 32, byteOrder: "big", type: 2, flags: 0 }, -}; -const provider = { id: "ghidra", name: "Ghidra", version: "12.1.4" }; -const profile = createAnalysisProfile(provider, { - powerpc_elf: { elf_class: 32, byte_order: "big", type: 2, flags: 0 }, -}); +} as const; +const cases = [ + { + architecture: "mips", + target: { + ...commonTarget, + architecture: "mips", + availableArchitectures: ["mips"], + mips: { elfClass: 32, byteOrder: "little", type: 2, flags: 0x70001001 }, + }, + profile: { + mips_elf: { + elf_class: 32, + byte_order: "little", + type: 2, + flags: 0x70001001, + }, + }, + changed: { + mips_elf: { + elf_class: 32, + byte_order: "big", + type: 2, + flags: 0x70001001, + }, + }, + observation: { address: "0x20150", value: "rea_mips_entry" }, + }, + { + architecture: "powerpc", + target: { + ...commonTarget, + architecture: "powerpc", + availableArchitectures: ["powerpc"], + powerpc: { elfClass: 32, byteOrder: "big", type: 2, flags: 0 }, + }, + profile: { + powerpc_elf: { elf_class: 32, byte_order: "big", type: 2, flags: 0 }, + }, + changed: { + powerpc_elf: { + elf_class: 32, + byte_order: "big", + type: 2, + flags: 0x00010000, + }, + }, + observation: { address: "0x100101b0", value: "rea_ppc_entry" }, + }, +] as const satisfies readonly { + architecture: string; + target: BinaryTarget; + profile: object; + changed: object; + observation: { address: string; value: string }; +}[]; -describe("PowerPC identity through lifecycle, Evidence and snapshot consumers", () => { - it("round-trips query bindings without relaxing integrity", () => { - const result = [{ address: "0x100101b0", value: "rea_ppc_entry" }]; +it.each(cases)( + "round-trips $architecture identity through Evidence and snapshots", + ({ + architecture, + target, + observation, + profile: facts, + changed: changedFacts, + }) => { + const result = [observation]; + const profile = createAnalysisProfile(provider, facts); const parameters = {}; const evidence = createEvidence(target, provider, { operation: "list_procedures", @@ -63,31 +120,24 @@ describe("PowerPC identity through lifecycle, Evidence and snapshot consumers", const restored = parseAnalysisSnapshot( JSON.parse(serializeAnalysisSnapshot(snapshot)), ); - expect(restored.target.architecture).toBe("powerpc"); + expect(restored.target.architecture).toBe(architecture); expect(restored.evidence_bundle.records[0]?.subject?.architecture).toBe( - "powerpc", + architecture, ); expect(restored.entries[0]?.execution.subject?.architecture).toBe( - "powerpc", + architecture, ); expect( sessionOutputSchemas.open_binary.parse({ result: target }).result .architecture, - ).toBe("powerpc"); + ).toBe(architecture); expect(() => parseEvidence({ ...evidence, subject: { ...evidence.subject, architecture: "x86" }, }), ).toThrow("semantic identifier"); - const changed = createAnalysisProfile(provider, { - powerpc_elf: { - elf_class: 32, - byte_order: "big", - type: 2, - flags: 0x00010000, - }, - }); + const changed = createAnalysisProfile(provider, changedFacts); expect(snapshotMatchesProfile(binding, changed)).toBe(false); - }); -}); + }, +); diff --git a/tests/composition/reverseEngineering.test.ts b/tests/composition/reverseEngineering.test.ts index ff7fc18e01..e5480ff776 100644 --- a/tests/composition/reverseEngineering.test.ts +++ b/tests/composition/reverseEngineering.test.ts @@ -404,7 +404,11 @@ it("marks signal-terminated provider commands as unknown completion", async () = /files.*network.*outside the selected artifact.*not bound to Evidence/iu, ), ); - expect(ordinaryExit.value.limitations).toHaveLength(4); + expect(ordinaryExit.value.limitations).toEqual( + expect.arrayContaining([ + ...rizinCommandLimitations({ path, command: "iI" }), + ]), + ); } } finally { await rm(directory, { recursive: true, force: true }); @@ -476,7 +480,11 @@ describe("Rizin command service", () => { /files.*network.*outside the selected artifact.*not bound to Evidence/iu, ), ); - expect(result.value.limitations).toHaveLength(4); + expect(result.value.limitations).toEqual( + expect.arrayContaining([ + ...rizinCommandLimitations({ path: callerPath, command }), + ]), + ); expect(result.value.parameters).toMatchObject({ path: callerPath, command, @@ -611,15 +619,15 @@ it("passes decoder and map inputs to Rizin before the path and records them", as expect(result.value.limitations).toEqual( expect.arrayContaining([...rizinCommandLimitations(input)]), ); - // One statement for the decoder inputs and one for the raw map. - expect(rizinCommandLimitations(input)).toHaveLength(2); + // Command validity stays unverified independently of decoder/map inputs. + expect(rizinCommandLimitations(input)).toHaveLength(3); expect( rizinCommandLimitations({ path, command: "pi 1", asm_bits: 16 }), - ).toHaveLength(1); + ).toHaveLength(2); expect( rizinCommandLimitations({ path, command: "pi 1", base_address: "0x0" }), - ).toHaveLength(1); - expect(rizinCommandLimitations({ path, command: "pi 1" })).toEqual([]); + ).toHaveLength(2); + expect(rizinCommandLimitations({ path, command: "pi 1" })).toHaveLength(1); } finally { await rm(directory, { recursive: true, force: true }); } diff --git a/tests/composition/reverseProviderLaunchErrors.test.ts b/tests/composition/reverseProviderLaunchErrors.test.ts index 73ecd8abf6..b2a1841d14 100644 --- a/tests/composition/reverseProviderLaunchErrors.test.ts +++ b/tests/composition/reverseProviderLaunchErrors.test.ts @@ -1,6 +1,7 @@ import { rm } from "node:fs/promises"; import { resolve } from "node:path"; -import { expect, it, vi } from "vitest"; +import { expect, it, onTestFinished, vi } from "vitest"; +import * as processOutput from "../../src/process/ExecFileOutput.js"; import { OwnedCommandFailure } from "../../src/process/OwnedCommand.js"; import { DarwinProcessOwnershipInspectionError, @@ -799,15 +800,13 @@ it("restricts Linux process ownership prerequisite remediation to missing ps and }); let currentError: Error = eioError; - const host = createSystemProcessOwnershipHost( - "linux", - {}, - { - execFileOutput: async () => { - throw currentError; - }, - }, - ); + const command = vi + .spyOn(processOutput, "execFileOutput") + .mockImplementation(async () => { + throw currentError; + }); + onTestFinished(() => command.mockRestore()); + const host = createSystemProcessOwnershipHost("linux", {}); currentError = eioError; const eioFailure = (await host @@ -871,18 +870,13 @@ it("restricts Linux process ownership prerequisite remediation to missing ps and }); expect(noCodeFailure.diagnostics).not.toHaveProperty("system_code"); - const emptyTableHost = createSystemProcessOwnershipHost( - "linux", - {}, - { - execFileOutput: async () => ({ - stdout: " 1 0 1 0 Ss /sbin/init\n", - stderr: "", - stdoutBytes: 40, - stderrBytes: 0, - }), - }, - ); + command.mockResolvedValue({ + stdout: " 1 0 1 0 Ss /sbin/init\n", + stderr: "", + stdoutBytes: 40, + stderrBytes: 0, + }); + const emptyTableHost = createSystemProcessOwnershipHost("linux", {}); const unreportedFailure = (await emptyTableHost .prepare?.() .catch((error: unknown) => error)) as LinuxOwnedProcessPrerequisiteError; @@ -1006,7 +1000,7 @@ it("retains successful architecture and artifact output for an admitted provider }); it("tracks observed launch failures and clears them after subsequent successful launch", async () => { - const { directory, path } = await createArtifact(); + const { directory } = await createArtifact(); const command = resolve(directory, "failing-objdump"); let failLaunch = true; const service = new TestReverseEngineeringService({ diff --git a/tests/conformance/ghidra/mips-fixture.mjs b/tests/conformance/ghidra/mips-fixture.mjs index 1d19aaa578..5d6202d46a 100644 --- a/tests/conformance/ghidra/mips-fixture.mjs +++ b/tests/conformance/ghidra/mips-fixture.mjs @@ -59,10 +59,6 @@ export async function buildMipsFixture( let attested = compiled; if (legacy) { const raw = await readFile(path); - const write16 = (offset, value) => - byteOrder === "little" - ? raw.writeUInt16LE(value, offset) - : raw.writeUInt16BE(value, offset); const write32 = (offset, value) => byteOrder === "little" ? raw.writeUInt32LE(value, offset) diff --git a/tests/conformance/package/conformanceFixtureVerifier.test.ts b/tests/conformance/package/conformanceFixtureVerifier.test.ts index 3c17c20506..aabbcefa79 100644 --- a/tests/conformance/package/conformanceFixtureVerifier.test.ts +++ b/tests/conformance/package/conformanceFixtureVerifier.test.ts @@ -46,6 +46,7 @@ it.skipIf(process.platform !== "darwin" && process.platform !== "linux").each([ { mode: "fragment", marker: "performAction:error:" }, ])( "verifies compiler-produced fixture records: $mode / $marker", + { tags: ["long-running"] }, async ({ mode, marker }) => { const { commands, root } = await ownedFixture("rea-fixture-symbols-"); const sources = join(root, "sources"); @@ -129,48 +130,43 @@ it.skipIf(process.platform !== "darwin" && process.platform !== "linux").each([ expect(JSON.parse(completed.stdout)).toMatchObject({ verified: 2 }); } }, - 120_000, ); it.each([ { schemaVersion: undefined, expectations: {} }, { schemaVersion: 1, expectations: {} }, { schemaVersion: 2, expectations: { strings: ["ambiguous"] } }, -])( - "refuses retired manifest semantics: %j", - async (legacy) => { - const { commands, root } = await ownedFixture( - "rea-fixture-manifest-version-", - ); - const manifest = join(root, "manifest.json"); - await writeFile( - manifest, - JSON.stringify({ - schemaVersion: legacy.schemaVersion, - platform: "linux", - fixtures: [{ expectations: legacy.expectations }], - }), - ); - await expect( - commands.exec(process.execPath, [verifier, manifest, root], { - cwd: root, - timeout: 10_000, - }), - ).rejects.toMatchObject({ - code: 1, - snapshot: { - stderr: { - text: expect.stringContaining( - legacy.schemaVersion === 2 - ? "Invalid conformance fixture string expectations" - : "Unsupported conformance manifest", - ), - }, +])("refuses retired manifest semantics: %j", async (legacy) => { + const { commands, root } = await ownedFixture( + "rea-fixture-manifest-version-", + ); + const manifest = join(root, "manifest.json"); + await writeFile( + manifest, + JSON.stringify({ + schemaVersion: legacy.schemaVersion, + platform: "linux", + fixtures: [{ expectations: legacy.expectations }], + }), + ); + await expect( + commands.exec(process.execPath, [verifier, manifest, root], { + cwd: root, + timeout: 10_000, + }), + ).rejects.toMatchObject({ + code: 1, + snapshot: { + stderr: { + text: expect.stringContaining( + legacy.schemaVersion === 2 + ? "Invalid conformance fixture string expectations" + : "Unsupported conformance manifest", + ), }, - }); - }, - 30_000, -); + }, + }); +}); it("preserves a carriage return that belongs to an nm symbol name", () => { expect(splitNmSymbolRecords("rea_entry\r\nrea_inventory_0000\n")).toEqual([ diff --git a/tests/conformance/package/environmentBundlePackagePin.test.ts b/tests/conformance/package/environmentBundlePackagePin.test.ts index a75e324cf8..2cbecdfead 100644 --- a/tests/conformance/package/environmentBundlePackagePin.test.ts +++ b/tests/conformance/package/environmentBundlePackagePin.test.ts @@ -4,12 +4,31 @@ import { describe, expect, it } from "vitest"; import { z } from "zod"; import { - bundleDockerfile, + planEnvironmentBundle, registryPackageSource, } from "../../../src/application/environment/environmentBundle.js"; +import { + environmentServeFixture, + environmentServeServed, +} from "../../fixtures/environmentServe.js"; import { PRODUCT_IDENTITY } from "../../../src/identity.js"; import { supportsNodeVersion } from "../../../src/domain/runtimeVersion.js"; +const plannedDockerfile = (): string => { + const { source, served } = environmentServeServed(environmentServeFixture()); + const planned = planEnvironmentBundle(source, { + packageSource: registryPackageSource(), + served: () => served, + }); + if (!planned.ok) throw planned.error; + const dockerfile = planned.value.files.find(({ path }) => + path.endsWith("/Dockerfile"), + ); + if (dockerfile === undefined) + throw new Error("Expected a planned Dockerfile"); + return dockerfile.content; +}; + /** * The exported Dockerfile installs the CLI that produced the bundle, so the * pin must be the exact package version of this build, read from the package @@ -20,7 +39,7 @@ describe("environment bundle package pin", () => { const pinned = ( await readFile(new URL("../../../.nvmrc", import.meta.url), "utf8") ).trim(); - const dockerfile = bundleDockerfile(registryPackageSource()); + const dockerfile = plannedDockerfile(); const nodeVersion = /^FROM node:(\d+\.\d+\.\d+)(?:[-@][^\s]+)?$/u.exec( dockerfile.split("\n")[0] ?? "", )?.[1]; @@ -39,7 +58,7 @@ describe("environment bundle package pin", () => { ), ), ); - const dockerfile = bundleDockerfile(registryPackageSource()); + const dockerfile = plannedDockerfile(); const install = dockerfile .split("\n") .find((line) => line.startsWith("RUN npm install -g")); diff --git a/tests/evaluation/noSharedModuleMockRule.test.ts b/tests/evaluation/noSharedModuleMockRule.test.ts index e5288b2f86..42d66c10b9 100644 --- a/tests/evaluation/noSharedModuleMockRule.test.ts +++ b/tests/evaluation/noSharedModuleMockRule.test.ts @@ -1,4 +1,4 @@ -import { describe, expect, it } from "vitest"; +import { describe, it } from "vitest"; import { fileURLToPath } from "node:url"; import { RuleTester } from "oxlint/plugins-dev"; diff --git a/tests/evaluation/numericLimitCheck.test.ts b/tests/evaluation/numericLimitCheck.test.ts index 09fd0e54c5..9ff48209e7 100644 --- a/tests/evaluation/numericLimitCheck.test.ts +++ b/tests/evaluation/numericLimitCheck.test.ts @@ -265,6 +265,53 @@ it("keeps mutable and shadowed aliases from proving a setting read", () => { ); }); +it("reports each mutable alias once while keeping dynamic reads unresolved", () => { + const source = ` + let key = "REA_SAMPLE_MAX_BYTES"; + consume(key); + consume(key); + Number(process.env[key]); + let other = "REA_OTHER_MAX_BYTES"; + consume(other); + `; + const discovered = discoverNumericLimitUses([ + { path: "src/aliases.ts", source }, + ]); + expect( + discovered.unresolved.filter( + (issue) => issue.reason === "mutable setting-key or value alias", + ), + ).toEqual([ + expect.objectContaining({ offset: source.indexOf("key =") }), + expect.objectContaining({ offset: source.indexOf("other =") }), + ]); + expect(discovered.unresolved).toContainEqual( + expect.objectContaining({ + reason: "dynamic environment key is not statically known", + }), + ); +}); + +it("does not attribute an unrelated same-named local to a numeric limit", () => { + const discovered = discoverNumericLimitUses([ + { + path: "src/shadowed-paths.ts", + source: ` + const selectedPath = process.env.REA_SAMPLE_MAX_BYTES; + Number(selectedPath); + function inspect(path: string) { + let selectedPath = path; + consume(selectedPath); + } + `, + }, + ]); + expect(discovered.get("REA_SAMPLE_MAX_BYTES")?.sites).toEqual( + expect.arrayContaining([expect.objectContaining({ evidence: "direct" })]), + ); + expect(discovered.unresolved).toEqual([]); +}); + it("keeps a destructured environment key, including a rename and a default", () => { const name = "REA_NEW_MAX_BYTES"; const discovered = discoverNumericLimitUses([ diff --git a/tests/evaluation/testDeadlines.test.ts b/tests/evaluation/testDeadlines.test.ts new file mode 100644 index 0000000000..bd3d249d24 --- /dev/null +++ b/tests/evaluation/testDeadlines.test.ts @@ -0,0 +1,16 @@ +import { expect, inject, it } from "vitest"; +import { + COVERAGE_DEADLINE_SCALE, + HOSTED_RUNNER_DEADLINE_SCALE, + TEST_DEADLINE_MS, +} from "../support/testDeadlines.js"; + +it("applies the source project deadline, scaled by coverage and hosted runners", ({ + task, +}) => { + const scale = inject("testDeadlineScale"); + const coverage = [1, COVERAGE_DEADLINE_SCALE]; + const hosted = [1, HOSTED_RUNNER_DEADLINE_SCALE]; + expect(coverage.flatMap((c) => hosted.map((h) => c * h))).toContain(scale); + expect(task.timeout).toBe(TEST_DEADLINE_MS.source * scale); +}); diff --git a/tests/fixtures/advertisedMcpTool.ts b/tests/fixtures/advertisedMcpTool.ts index b1d254f1bd..dc6da77f83 100644 --- a/tests/fixtures/advertisedMcpTool.ts +++ b/tests/fixtures/advertisedMcpTool.ts @@ -46,6 +46,26 @@ export const advertisedMcpToolSchemas = async ( }; }; +/** Call an advertised tool and validate its structured result against its schema. */ +export const callAdvertisedMcpTool = async ( + client: Client, + name: string, + args: Record, + validator: AdvertisedSchemaAssertions, +) => { + const schema = await advertisedMcpToolSchemas(client, name, validator); + const result = await client.callTool( + { name, arguments: args }, + { toolDefinition: schema.toolDefinition }, + ); + expect(result.isError, JSON.stringify(result)).not.toBe(true); + expect( + schema.ajv.validate(schema.outputSchema, result.structuredContent), + JSON.stringify(schema.ajv.errors), + ).toBe(true); + return result.structuredContent; +}; + /** Keep public output, provenance and session retention checks together. */ export const assertRecordedMcpEvidence = ( response: Awaited>, @@ -54,16 +74,11 @@ export const assertRecordedMcpEvidence = ( | "inspect_binary_layout" | "inspect_evm_interface" | "inspect_native_exports", - schemas: Awaited>, session: BinarySession, expected: { readonly artifact: { readonly sha256: string } }, confidence: ReturnType["confidence"], ) => { expect(response.isError).not.toBe(true); - expect( - schemas.ajv.validate(schemas.outputSchema, response.structuredContent), - JSON.stringify(schemas.ajv.errors), - ).toBe(true); const parsed = toolContract(name).outputSchema.parse( response.structuredContent, ); diff --git a/tests/fixtures/artifactEvidence.ts b/tests/fixtures/artifactEvidence.ts index 49c748ed13..9c4fe7ffb0 100644 --- a/tests/fixtures/artifactEvidence.ts +++ b/tests/fixtures/artifactEvidence.ts @@ -1,6 +1,61 @@ -import { artifactInventoryResultSchema } from "../../src/domain/artifactGraph.js"; +import { + artifactInventoryResultSchema, + type ArtifactInventoryResult, +} from "../../src/domain/artifactGraph.js"; +import { + artifactGraphDigest, + artifactManifestId, +} from "../../src/domain/artifactIdentity.js"; import { createEvidence, type Evidence } from "../../src/domain/evidence.js"; +/** Recommit altered inventory occurrences as a historically recorded observation. */ +export const historicalInventoryEvidence = ( + source: Evidence, + alter: ( + occurrence: ArtifactInventoryResult["occurrences"][number], + ) => ArtifactInventoryResult["occurrences"][number], + confidence: "observed" | "derived" = "derived", +): Evidence => { + const inventory = artifactInventoryResultSchema.parse( + source.normalized_result, + ); + if (source.subject === null) + throw new TypeError("Inventory has no artifact subject"); + const occurrences = inventory.occurrences.map(alter); + const graphSha256 = artifactGraphDigest({ + nodes: inventory.nodes, + occurrences, + edges: inventory.edges, + contradictions: inventory.integrity_contradictions, + }); + return createEvidence( + { + path: source.subject.local_path, + sha256: inventory.manifest.root_sha256, + format: inventory.manifest.root_format, + }, + source.provider, + { + operation: "inventory_artifact", + parameters: {}, + result: { + ...inventory, + occurrences, + manifest: { + ...inventory.manifest, + graph_sha256: graphSha256, + manifest_id: artifactManifestId( + inventory.manifest.root_artifact_id, + graphSha256, + ), + }, + }, + confidence, + authority: "shipped-artifact", + }, + ); +}; + /** Partition a real scan into authenticated partial observations for merge tests. */ export const fragmentInventoryEvidence = ( evidence: Evidence, diff --git a/tests/fixtures/artifactReaderLifecycle.ts b/tests/fixtures/artifactReaderLifecycle.ts index a6f14c24aa..e0fbe66099 100644 --- a/tests/fixtures/artifactReaderLifecycle.ts +++ b/tests/fixtures/artifactReaderLifecycle.ts @@ -1,5 +1,6 @@ import { writeFile } from "node:fs/promises"; import { join } from "node:path"; +import { getDefaultHighWaterMark } from "node:stream"; import { buffer } from "node:stream/consumers"; import { finished } from "node:stream/promises"; import { setImmediate } from "node:timers/promises"; @@ -60,8 +61,18 @@ const consumptionCases = ({ image, createReader }: LifecycleFixture): void => { const controller = new AbortController(); const reason = new Error("cancel buffered member"); try { - const [entry] = await entries(reader); - const stream = await reader.open(entry!, controller.signal); + // Unread bytes buffer fully only below the readable high-water mark. + // Node's default is 16 KiB on Windows and 64 KiB elsewhere. + const entry = (await entries(reader)).find( + ({ declaredSize }) => + declaredSize !== null && + declaredSize > 0 && + declaredSize < getDefaultHighWaterMark(false), + ); + if (entry === undefined) + throw new Error("fixture has no member that fits one stream buffer"); + const stream = await reader.open(entry, controller.signal); + expect(entry.declaredSize).toBeLessThan(stream.readableHighWaterMark); await finished(stream, { readable: false, cleanup: true }); await setImmediate(); expect(stream.readableLength).toBeGreaterThan(0); @@ -243,7 +254,8 @@ const validationCases = ({ image, createReader }: LifecycleFixture): void => { }; /** Stored-member readers share ownership and native I/O regression oracles. - * The fixture must have a nonempty first entry with a physical byte range. + * The fixture must have a nonempty first entry with a physical byte range, + * and a nonempty entry smaller than Node's default stream buffer. */ export const artifactReaderLifecycleCases = (setup: LifecycleFixture): void => { describe("stored member lifecycle", () => { diff --git a/tests/fixtures/binaryDiagnostics/windowsShortcut.ts b/tests/fixtures/binaryDiagnostics/windowsShortcut.ts index 90eddc6792..105964eed6 100644 --- a/tests/fixtures/binaryDiagnostics/windowsShortcut.ts +++ b/tests/fixtures/binaryDiagnostics/windowsShortcut.ts @@ -5,13 +5,13 @@ export const SHELL_LINK_CLSID = Buffer.from([ 0x00, 0x00, 0x46, ]); -export const MY_COMPUTER_CLSID = Buffer.from([ +const MY_COMPUTER_CLSID = Buffer.from([ 0xe0, 0x4f, 0xd0, 0x20, 0xea, 0x3a, 0x69, 0x10, 0xa2, 0xd8, 0x08, 0x00, 0x2b, 0x30, 0x30, 0x9d, ]); /** 2024-01-15T12:00:00.000Z as FILETIME 100-nanosecond intervals. */ -export const FIXTURE_FILETIME = 133497936000000000n; +const FIXTURE_FILETIME = 133497936000000000n; export const LINK_FLAG = { HasLinkTargetIDList: 0x00000001, @@ -26,7 +26,7 @@ export const LINK_FLAG = { HasExpIcon: 0x00004000, } as const; -export const EXTRA_SIGNATURE = { +const EXTRA_SIGNATURE = { EnvironmentVariable: 0xa0000001, Tracker: 0xa0000003, ConsoleFE: 0xa0000004, @@ -40,7 +40,7 @@ export const TRACKER_MAC = Buffer.from([0x00, 0x11, 0x22, 0x33, 0x44, 0x55]); /** UUID timestamp 100ns since 1582-10-15 matching 2024-01-15T12:00:00.000Z. */ export const TRACKER_UUID_TIMESTAMP = 139289664000000000n; -export type FixtureIdListItem = +type FixtureIdListItem = | { readonly kind: "root"; readonly sortIndex?: number; diff --git a/tests/fixtures/binaryPlistObjectTable.ts b/tests/fixtures/binaryPlistObjectTable.ts index a258f529b3..4925aa2079 100644 --- a/tests/fixtures/binaryPlistObjectTable.ts +++ b/tests/fixtures/binaryPlistObjectTable.ts @@ -1,4 +1,4 @@ -export interface BinaryPlistObjectTableLayout { +interface BinaryPlistObjectTableLayout { readonly offsetWidth: number; readonly objectCount: number; readonly tableOffset: number; diff --git a/tests/fixtures/browserScenarioCapture.ts b/tests/fixtures/browserScenarioCapture.ts index 550f2c51bb..ea7398430a 100644 --- a/tests/fixtures/browserScenarioCapture.ts +++ b/tests/fixtures/browserScenarioCapture.ts @@ -37,7 +37,7 @@ export interface ScenarioExchange { readonly responseHeaders?: readonly ScenarioHeader[]; } -export interface ScenarioHeader { +interface ScenarioHeader { readonly name: string; readonly value: string | null; } diff --git a/tests/fixtures/browserScenarioReferences.ts b/tests/fixtures/browserScenarioReferences.ts new file mode 100644 index 0000000000..aec892a126 --- /dev/null +++ b/tests/fixtures/browserScenarioReferences.ts @@ -0,0 +1,46 @@ +import { resolve } from "node:path"; + +/** Inert inputs for reference validation; no browser or secret value is used. */ +export const browserScenarioReferences = ( + placement: "query" | "storage" | "session-storage" | "cookie", + count: number, +) => { + const entries = Array.from({ length: count }, (_, index) => ({ + name: `entry_${String(index)}`, + value: { source: "secret", secret_id: "token" }, + })); + return { + browser: { mode: "launch", executable_path: resolve("unused-chromium") }, + start_url: { + url: "http://127.0.0.1:1/", + query: placement === "query" ? entries : [], + }, + actions: [{ step_id: "wait", action: "wait_for_timeout", duration_ms: 1 }], + secrets: [ + { secret_id: "token", environment_variable: "REA_REFERENCE_FIXTURE" }, + ], + storage: { + cookies: + placement === "cookie" + ? [ + { + name: "fixture", + value: { source: "literal", value: "fixture" }, + destination: { url: "http://127.0.0.1:1/", query: entries }, + http_only: false, + secure: false, + same_site: "Lax", + }, + ] + : [], + local_storage: + placement === "storage" + ? [{ origin: "http://127.0.0.1:1", entries }] + : [], + session_storage: + placement === "session-storage" + ? [{ origin: "http://127.0.0.1:1", entries }] + : [], + }, + }; +}; diff --git a/tests/fixtures/buildCaptureFakes.ts b/tests/fixtures/buildCaptureFakes.ts index 1e42449238..ac97a54671 100644 --- a/tests/fixtures/buildCaptureFakes.ts +++ b/tests/fixtures/buildCaptureFakes.ts @@ -34,7 +34,7 @@ export const BUILD_REQUEST = { const ENDS = ["--seed-checkpoint", "seed", "--end-checkpoint", "end"]; /** The app's package as the capture records it, the URL the export downloads its file from, if it needs one, the download it was installed from, if any, and the packages it installs beside the app. */ -export interface CapturedInstall { +interface CapturedInstall { readonly package?: DesktopPackage; readonly packageUrl?: string; readonly downloadInstall?: Readonly>; @@ -42,7 +42,7 @@ export interface CapturedInstall { } /** A task the export takes with the checkpoint it is checked at. */ -export interface BuildTaskAtEnd { +interface BuildTaskAtEnd { readonly task: unknown; readonly end_checkpoint: string; } diff --git a/tests/fixtures/buildComputerMcp.ts b/tests/fixtures/buildComputerMcp.ts index 33c14484cf..84adc63e6a 100644 --- a/tests/fixtures/buildComputerMcp.ts +++ b/tests/fixtures/buildComputerMcp.ts @@ -35,7 +35,7 @@ const hostEnvironment = Object.fromEntries( ); /** A check grades its first read, for tests whose app changes nothing while it is checked. */ -export const FIRST_READ: SettleTiming = { +const FIRST_READ: SettleTiming = { intervalMs: 0, windowMs: 0, boundMs: 0, diff --git a/tests/fixtures/captureDecoderLimits.ts b/tests/fixtures/captureDecoderLimits.ts index fe47f96d0f..44969bb4a0 100644 --- a/tests/fixtures/captureDecoderLimits.ts +++ b/tests/fixtures/captureDecoderLimits.ts @@ -1,10 +1,19 @@ +import { javaScriptWorkerMemoryLimitBytes } from "../../src/process/JavaScriptWorkerMemoryLimit.js"; +import { DEFAULT_CAPTURE_DECODER_OLD_GENERATION_MIB } from "../../src/domain/analysisResourceLimits.js"; +import { NODE_CAPTURE_HEAP_ENFORCED } from "../../src/browser/history/CaptureRelease.js"; import { - captureDecoderProcessMemoryLimitBytes, - NODE_CAPTURE_HEAP_LIMITS, -} from "../../src/browser/history/CaptureRelease.js"; + javaScriptRuntime, + supportsJavaScriptRuntime, +} from "../../src/domain/runtimeVersion.js"; /** Default Job Object limit used by capture decoder fixtures. */ export const NODE_CAPTURE_PROCESS_MEMORY_LIMIT_BYTES = - captureDecoderProcessMemoryLimitBytes( - NODE_CAPTURE_HEAP_LIMITS.oldGenerationMiB, - ); + javaScriptWorkerMemoryLimitBytes(DEFAULT_CAPTURE_DECODER_OLD_GENERATION_MIB); + +export const CAPTURE_RUNTIME_SUPPORTED = supportsJavaScriptRuntime( + javaScriptRuntime(process.versions), +); + +export const CAPTURE_DECODER_HAS_MEMORY_BOUND = + NODE_CAPTURE_HEAP_ENFORCED || + NODE_CAPTURE_PROCESS_MEMORY_LIMIT_BYTES !== null; diff --git a/tests/fixtures/claudeDesktop.ts b/tests/fixtures/claudeDesktop.ts new file mode 100644 index 0000000000..84a6aa46e9 --- /dev/null +++ b/tests/fixtures/claudeDesktop.ts @@ -0,0 +1,2 @@ +/** Known Claude Desktop MSIX identity used by the registration fixtures. */ +export const CLAUDE_DESKTOP_PACKAGE_FAMILY = "Claude_pzs8sxrjxfjjc"; diff --git a/tests/fixtures/clientEnvironment.ts b/tests/fixtures/clientEnvironment.ts index 78c9cca555..8d68a49560 100644 --- a/tests/fixtures/clientEnvironment.ts +++ b/tests/fixtures/clientEnvironment.ts @@ -1,7 +1,7 @@ import { vi } from "vitest"; /** Environment variables that redirect where supported clients keep config. */ -export const CLIENT_LOCATION_ENVIRONMENT = [ +const CLIENT_LOCATION_ENVIRONMENT = [ "HOME", "USERPROFILE", "HOMEDRIVE", diff --git a/tests/fixtures/descriptorCloseFailure.ts b/tests/fixtures/descriptorCloseFailure.ts index 86ccfe3cca..d6b605cf92 100644 --- a/tests/fixtures/descriptorCloseFailure.ts +++ b/tests/fixtures/descriptorCloseFailure.ts @@ -3,7 +3,7 @@ import { expect } from "vitest"; import type { Result } from "../../src/domain/result.js"; import type { AnalysisError } from "../../src/domain/analysisErrorBase.js"; -export interface DescriptorCloseFailure { +interface DescriptorCloseFailure { path: string; denied: boolean; handles: FileHandle[]; diff --git a/tests/fixtures/fakeAgentEnvSolves.ts b/tests/fixtures/fakeAgentEnvSolves.ts index 80ca1cf9e2..48022c5ff7 100644 --- a/tests/fixtures/fakeAgentEnvSolves.ts +++ b/tests/fixtures/fakeAgentEnvSolves.ts @@ -76,7 +76,7 @@ def run_bundle(root, *, tasks=(), model=None, agent=None, sandbox=None, emulate= return types.SimpleNamespace(runs=(run,)) `; -export interface FakeAgentEnvSolves { +interface FakeAgentEnvSolves { readonly environment: Readonly>; notes(name: string): Promise; } diff --git a/tests/fixtures/fakeBuildComputerRea.ts b/tests/fixtures/fakeBuildComputerRea.ts index 7a19b422e7..6c022f0a4b 100644 --- a/tests/fixtures/fakeBuildComputerRea.ts +++ b/tests/fixtures/fakeBuildComputerRea.ts @@ -185,7 +185,7 @@ sys.exit(0) `; /** Prepared outputs of the real CLI for the fake REA to print. */ -export interface FakeReaOutputs { +interface FakeReaOutputs { readonly capture: string; readonly draft: string; readonly export: string; @@ -195,7 +195,7 @@ export interface FakeReaOutputs { readonly withheld?: string; } -export interface FakeBuildComputerRea { +interface FakeBuildComputerRea { readonly home: string; readonly bin: string; readonly env: Readonly>; diff --git a/tests/fixtures/firmware/producer.mjs b/tests/fixtures/firmware/producer.mjs index ef013ec8bf..4cd25d5217 100644 --- a/tests/fixtures/firmware/producer.mjs +++ b/tests/fixtures/firmware/producer.mjs @@ -102,43 +102,46 @@ if (args.includes("--version")) { }); const hash = (value) => ({ __typename__: "HashReport", sha256: value }); const childTask = task(child, depth, "gzip-id"); + const nestedUnknownRecords = (childSize, digest) => [ + { + task: childTask, + reports: [ + stat(child, childSize), + hash(digest), + { + __typename__: "UnknownChunkReport", + id: "unknown-id", + start_offset: 0, + end_offset: content.length, + size: content.length, + }, + ], + subtasks: [], + }, + { + task: task(input, 0, ""), + reports: [ + stat(input, bytes.length), + hash(sha256), + { + __typename__: "ChunkReport", + id: "gzip-id", + start_offset: 0, + end_offset: bytes.length, + size: bytes.length, + handler_name: "gzip", + is_encrypted: false, + extraction_reports: [], + }, + ], + subtasks: [childTask], + }, + ]; if (mode === "nested-unknown") { - const nestedUnknownRecords = [ - { - task: childTask, - reports: [ - stat(child, content.length), - hash(childHash), - { - __typename__: "UnknownChunkReport", - id: "unknown-id", - start_offset: 0, - end_offset: content.length, - size: content.length, - }, - ], - subtasks: [], - }, - { - task: task(input, 0, ""), - reports: [ - stat(input, bytes.length), - hash(sha256), - { - __typename__: "ChunkReport", - id: "gzip-id", - start_offset: 0, - end_offset: bytes.length, - size: bytes.length, - handler_name: "gzip", - is_encrypted: false, - extraction_reports: [], - }, - ], - subtasks: [childTask], - }, - ]; - await writeFile(reportPath, JSON.stringify(nestedUnknownRecords)); + await writeFile( + reportPath, + JSON.stringify(nestedUnknownRecords(content.length, childHash)), + ); } if (mode === "nested-unknown-oversized") { // The child exceeds the firmware input limit, so a fresh run cannot @@ -165,42 +168,10 @@ if (args.includes("--version")) { await source.close(); } const oversizedHash = oversizedDigest.digest("hex"); - const nestedUnknownRecords = [ - { - task: childTask, - reports: [ - stat(child, oversized), - hash(oversizedHash), - { - __typename__: "UnknownChunkReport", - id: "unknown-id", - start_offset: 0, - end_offset: content.length, - size: content.length, - }, - ], - subtasks: [], - }, - { - task: task(input, 0, ""), - reports: [ - stat(input, bytes.length), - hash(sha256), - { - __typename__: "ChunkReport", - id: "gzip-id", - start_offset: 0, - end_offset: bytes.length, - size: bytes.length, - handler_name: "gzip", - is_encrypted: false, - extraction_reports: [], - }, - ], - subtasks: [childTask], - }, - ]; - await writeFile(reportPath, JSON.stringify(nestedUnknownRecords)); + await writeFile( + reportPath, + JSON.stringify(nestedUnknownRecords(oversized, oversizedHash)), + ); } if (mode === "depth-orphan") { // A depth-limited directory with nothing published under it cannot diff --git a/tests/fixtures/flutterCleanup.ts b/tests/fixtures/flutterCleanup.ts new file mode 100644 index 0000000000..baa1a541a3 --- /dev/null +++ b/tests/fixtures/flutterCleanup.ts @@ -0,0 +1,20 @@ +import * as fsPromises from "node:fs/promises"; +import { onTestFinished, vi } from "vitest"; + +export const realRm = ( + await vi.importActual("node:fs/promises") +).rm; + +export const interceptFlutterRootRemoval = ( + remove: (root: string) => Promise, +): void => { + const spy = vi + .spyOn(fsPromises, "rm") + .mockImplementation((path, options) => + typeof path === "string" && + path.split(/[\\/]/u).at(-1)?.startsWith("rea-flutter-") + ? remove(path) + : realRm(path, options), + ); + onTestFinished(() => spy.mockRestore()); +}; diff --git a/tests/fixtures/go/rodataStringImage.ts b/tests/fixtures/go/rodataStringImage.ts index e3bffc5e2e..afcd3ae70b 100644 --- a/tests/fixtures/go/rodataStringImage.ts +++ b/tests/fixtures/go/rodataStringImage.ts @@ -1,11 +1,11 @@ -export interface GoStringHeaderFixtureSpec { +interface GoStringHeaderFixtureSpec { readonly section: "rodata" | "data" | "text"; readonly offsetInSection: number; readonly ptr: bigint; readonly len: bigint; } -export interface GoRodataStringFixtureOptions { +interface GoRodataStringFixtureOptions { readonly format: "elf" | "pe" | "macho"; readonly bits?: 32 | 64; readonly byteOrder?: "little" | "big"; diff --git a/tests/fixtures/goVerifierLifetime.mjs b/tests/fixtures/goVerifierLifetime.mjs new file mode 100644 index 0000000000..21f1a36d61 --- /dev/null +++ b/tests/fixtures/goVerifierLifetime.mjs @@ -0,0 +1,160 @@ +// Exercise the actual verifier entrypoint with a fixture compiler and authored +// SDK/transport failures. The fixture root and command owner remain real. +import assert from "node:assert/strict"; +import { existsSync } from "node:fs"; +import { chmod, writeFile } from "node:fs/promises"; +import { registerHooks } from "node:module"; +import { join } from "node:path"; +import { setTimeout as delay } from "node:timers/promises"; + +import { PrivateRuntimeRoot } from "../../dist/process/PrivateRuntimeRoot.js"; +import { createVerifierCommands } from "../../scripts/lib/verify-commands.mjs"; + +const scenario = process.argv[2]; +const directory = process.argv[3]; +assert.equal(typeof directory, "string"); +const compiler = join(directory, "go-fixture"); +await writeFile( + compiler, + '#!/bin/sh\ncase "$1" in\nversion) echo "go version fixture";;\nenv) printf "linux\\namd64\\n";;\nesac\n', +); +await chmod(compiler, 0o755); +process.env.REA_VERIFY_GO_COMMAND = compiler; +const events = []; +let retrying = false; +let transport; +const fails = (name) => !retrying && scenario.includes(name); +const state = { + Client: class { + constructor() { + if (fails("client-constructor")) + throw new Error("client constructor fault"); + } + async connect(value) { + transport = value; + await value.start(); + } + async close() { + events.push("client-close"); + if (fails("client-close")) throw new Error("client close fault"); + await transport?.close(); + } + }, + createTransport() { + if (fails("transport-constructor")) + throw new Error("transport constructor fault"); + let closed = false; + return { + async start() { + if (closed) throw new Error("Transport closed before handshake"); + events.push("transport-start"); + if (fails("handshake")) throw new Error("handshake fault"); + }, + async close() { + events.push("transport-close"); + if (fails("transport-close")) throw new Error("transport close fault"); + // A close promise is the completion boundary, not its initial call. + await delay(20); + closed = true; + events.push("transport-settled"); + }, + }; + }, + async createOwnedMcpStdioTransport() { + return state.createTransport(); + }, + async createVerifierCommands() { + const owner = await createVerifierCommands(); + const close = owner.close.bind(owner); + owner.close = async () => { + events.push("commands-close"); + if (fails("commands-close")) throw new Error("commands close fault"); + await close(); + }; + return owner; + }, +}; +globalThis.goVerifierLifetime = state; +const entrypoint = new URL( + "../../scripts/verify-go-binary.mjs", + import.meta.url, +); +const moduleSources = new Map([ + [ + "@modelcontextprotocol/client", + "export const Client = globalThis.goVerifierLifetime.Client;", + ], + [ + "@modelcontextprotocol/client/stdio", + "export class StdioClientTransport { constructor() { return globalThis.goVerifierLifetime.createTransport(); } }", + ], + [ + "./lib/owned-mcp-stdio.mjs", + "export const createOwnedMcpStdioTransport = globalThis.goVerifierLifetime.createOwnedMcpStdioTransport;", + ], + [ + "./lib/verify-commands.mjs", + "export const createVerifierCommands = globalThis.goVerifierLifetime.createVerifierCommands;", + ], +]); +const hook = registerHooks({ + resolve(specifier, context, next) { + const source = + context.parentURL === entrypoint.href + ? moduleSources.get(specifier) + : undefined; + return source === undefined + ? next(specifier, context) + : { + shortCircuit: true, + url: `data:text/javascript,${encodeURIComponent(source)}`, + }; + }, +}); +const closeRoot = PrivateRuntimeRoot.prototype.close; +let rootPath; +PrivateRuntimeRoot.prototype.close = async function () { + rootPath = this.path; + events.push("root-close"); + if (fails("root-close")) throw new Error("root close fault"); + await closeRoot.call(this); +}; +const reports = []; +const stderr = console.error; +console.error = (...values) => { + if (typeof values[0] === "string" && values[0].startsWith("{")) + reports.push(JSON.parse(values[0])); + else stderr(...values); +}; +let failure; +try { + await import(entrypoint.href); +} catch (cause) { + failure = cause; +} finally { + hook.deregister(); + console.error = stderr; +} +assert.ok(failure, "Fixture compiler intentionally produces no executable"); +const messages = (cause) => + cause instanceof AggregateError + ? cause.errors.flatMap(messages) + : [cause.message]; +const report = reports.at(-1); +const retained = report?.retained_root ?? null; +const existedAtFailure = retained !== null && existsSync(retained); +const beforeRetry = [...events]; +retrying = true; +for (const lease of failure.cleanupLeases ?? []) await lease.release(); +console.log( + JSON.stringify({ + messages: messages(failure), + report, + retained, + existedAtFailure, + removedAfterRetry: retained === null || !existsSync(retained), + rootRemoved: rootPath !== undefined && !existsSync(rootPath), + beforeRetry, + events, + }), +); diff --git a/tests/fixtures/gpu/codeObjects.ts b/tests/fixtures/gpu/codeObjects.ts index d4b711a296..e77207f6b0 100644 --- a/tests/fixtures/gpu/codeObjects.ts +++ b/tests/fixtures/gpu/codeObjects.ts @@ -12,7 +12,7 @@ export const ptxFixture = `// Producer-shaped static PTX; portable tests do not ret; } `; -export interface ElfFixtureSection { +interface ElfFixtureSection { name: string; type?: number; flags?: bigint; diff --git a/tests/fixtures/il2cpp/metadata.ts b/tests/fixtures/il2cpp/metadata.ts index ad6fcea63d..585dab20ae 100644 --- a/tests/fixtures/il2cpp/metadata.ts +++ b/tests/fixtures/il2cpp/metadata.ts @@ -485,16 +485,6 @@ export const fixtureRows = (options: Il2cppMetadataFixtureOptions = {}) => { return { types, methods }; }; -/** A zero-filled section of `count` records, or none when `count` is 0. */ -const zeroFilledSection = ( - name: SectionName, - count: number, - recordBytes: number, -): [SectionName, SectionContent][] => - count === 0 - ? [] - : [[name, { bytes: Buffer.alloc(count * recordBytes), count }]]; - /** Real rows followed by zero-filled rows that no owner claims. */ const paddedRows = ( rows: Buffer, diff --git a/tests/fixtures/importCycles.ts b/tests/fixtures/importCycles.ts new file mode 100644 index 0000000000..51ba28ed3a --- /dev/null +++ b/tests/fixtures/importCycles.ts @@ -0,0 +1,107 @@ +import { readdirSync, readFileSync, statSync } from "node:fs"; +import { dirname, join, relative, resolve } from "node:path"; +import { expect } from "vitest"; +import { readImportSpecifiers } from "../../scripts/lib/import-specifiers.mjs"; + +const relativeSpecifiers = (source: string): readonly string[] => + readImportSpecifiers(source) + .map(({ specifier }) => specifier) + .filter((specifier) => specifier.startsWith(".")); + +const productionFiles = (directory: string): string[] => { + const files: string[] = []; + for (const name of readdirSync(directory)) { + const path = join(directory, name); + if (statSync(path).isDirectory()) files.push(...productionFiles(path)); + else if (name.endsWith(".ts") && !name.includes(".test.")) files.push(path); + } + return files; +}; + +const importGraph = (root: string): Map> => { + const files = new Set( + productionFiles(root).map((path) => relative(root, path)), + ); + const graph = new Map>(); + for (const file of files) { + const source = readFileSync(join(root, file), "utf8"); + const edges = new Set(); + for (const spec of relativeSpecifiers(source)) { + const resolved = resolve(dirname(join(root, file)), spec).replace( + /\.js$/, + ".ts", + ); + const target = relative(root, resolved); + if (files.has(target)) edges.add(target); + } + graph.set(file, edges); + } + return graph; +}; + +const stronglyConnected = ( + graph: Map>, +): readonly (readonly string[])[] => { + let index = 0; + const stack: string[] = []; + const onStack = new Set(); + const indices = new Map(); + const low = new Map(); + const components: string[][] = []; + const visit = (node: string): void => { + indices.set(node, index); + low.set(node, index); + index += 1; + stack.push(node); + onStack.add(node); + for (const next of graph.get(node) ?? []) { + if (!indices.has(next)) { + visit(next); + low.set(node, Math.min(low.get(node) ?? index, low.get(next) ?? index)); + } else if (onStack.has(next)) { + low.set( + node, + Math.min(low.get(node) ?? index, indices.get(next) ?? index), + ); + } + } + if (low.get(node) === indices.get(node)) { + const component: string[] = []; + let member = ""; + while (member !== node) { + member = stack.pop() ?? node; + onStack.delete(member); + component.push(member); + } + if (component.length > 1) components.push(component.sort()); + } + }; + for (const node of [...graph.keys()].sort()) { + if (!indices.has(node)) visit(node); + } + return components.sort( + (left, right) => + right.length - left.length || left.join().localeCompare(right.join()), + ); +}; + +/** Check cycle membership and internal edges against the subsystem's allowance. */ +export const expectImportCycles = ( + root: string, + allowedCycles: readonly (readonly string[])[], + allowedCycleEdges: readonly string[], +): void => { + const graph = importGraph(root); + const cycles = stronglyConnected(graph).map((cycle) => cycle.join("\n")); + expect(cycles).toEqual( + allowedCycles.map((cycle) => [...cycle].sort().join("\n")), + ); + const internalEdges = allowedCycles.flatMap((cycle) => + cycle.flatMap((source) => + [...(graph.get(source) ?? [])] + .filter((target) => cycle.includes(target)) + .map((target) => `${source} -> ${target}`), + ), + ); + expect(internalEdges.sort()).toEqual(allowedCycleEdges); +}; diff --git a/tests/fixtures/javascriptAsyncChunkCapacity.ts b/tests/fixtures/javascriptAsyncChunkCapacity.ts new file mode 100644 index 0000000000..e4a8406125 --- /dev/null +++ b/tests/fixtures/javascriptAsyncChunkCapacity.ts @@ -0,0 +1,13 @@ +/** Inert generated Webpack source; no fixture code is executed. */ +export const javascriptAsyncChunkCapacity = (count: number) => { + const factory = `(m,e,r)=>{${Array.from( + { length: count }, + (_, index) => `r.e(${String(index)});`, + ).join("")}}`; + return { + factory, + source: + `self.webpackChunkapp.push([[1],{1:${factory},` + + "2:(m,e,r)=>{r.e(0);r.e('later');r.e(dynamic);}}]);", + }; +}; diff --git a/tests/fixtures/javascriptReturnShapes.ts b/tests/fixtures/javascriptReturnShapes.ts index f9c873599a..8e24c39ef4 100644 --- a/tests/fixtures/javascriptReturnShapes.ts +++ b/tests/fixtures/javascriptReturnShapes.ts @@ -9,7 +9,7 @@ import { toolContract } from "../../src/contracts/toolContracts.js"; import { javascriptApplicationAnalysisResultSchema } from "../../src/domain/javascript/javascriptApplicationAnalysis.js"; import { projectedExportReturnShapesSchema } from "../../src/domain/javascript/javascriptExportShapeComparisonSchemas.js"; import { createTestTempDirectory } from "./temporaryDirectory.js"; -import { runFileCli, type TestCli } from "../support/cli/cliFixture.js"; +import type { TestCli } from "../support/cli/cliFixture.js"; import { createMcpSchemaValidation } from "./mcpSchemaValidation.js"; const exportedReturnFields = (value: unknown) => { @@ -46,7 +46,7 @@ const analyzeReturnShapesThroughCli = async ( ) => { const root = await createTestTempDirectory("rea-return-shapes-"); await writeFile(join(root, fileName), source); - const cliResponse = await runFileCli(cli, { + const cliResponse = await cli.run({ arguments: [ "analyze-javascript-application", root, diff --git a/tests/fixtures/lipo.ts b/tests/fixtures/lipo.ts new file mode 100644 index 0000000000..fefd7cf9d6 --- /dev/null +++ b/tests/fixtures/lipo.ts @@ -0,0 +1,8 @@ +import { parseLipoOutputResult } from "../../src/native/parsers/lipo.js"; + +/** Unwrap the production parser boundary for fixture setup and error assertions. */ +export const parseLipoFixture = (output: string, operandPath?: string) => { + const parsed = parseLipoOutputResult(output, operandPath); + if (!parsed.ok) throw parsed.error; + return parsed.value; +}; diff --git a/tests/fixtures/localToolsMcp.ts b/tests/fixtures/localToolsMcp.ts index b3aef903e4..fc785cf473 100644 --- a/tests/fixtures/localToolsMcp.ts +++ b/tests/fixtures/localToolsMcp.ts @@ -9,7 +9,7 @@ import { } from "@modelcontextprotocol/client/stdio"; import { closeVerifierResources } from "../../scripts/lib/verifier-cleanup.mjs"; import { fileURLToPath } from "node:url"; -import { expect, onTestFinished } from "vitest"; +import { afterAll, beforeAll, expect, onTestFinished } from "vitest"; import { createMcpSchemaValidation } from "./mcpSchemaValidation.js"; /** Default to native tools while preserving explicit caller configuration. */ @@ -89,3 +89,33 @@ export async function connectLocalToolsMcp( }; return { client, call }; } + +/** + * One production stdio MCP client for the calling test file. It connects + * before the file's tests. Teardown removes each cleanup only after it + * succeeds, so a failed cleanup stays owned. + */ +export const fileLocalToolsMcp = ( + environment: Readonly> = {}, +) => { + let connected: Awaited> | undefined; + const cleanups: Array<() => Promise> = []; + beforeAll(async () => { + connected = await connectLocalToolsMcp(environment, (cleanup) => { + cleanups.push(cleanup); + }); + }); + afterAll(async () => { + for (let cleanup = cleanups.at(-1); cleanup; cleanup = cleanups.at(-1)) { + await cleanup(); + cleanups.pop(); + } + }); + return { + get client() { + if (connected === undefined) + throw new Error("The file's MCP client is not connected"); + return connected.client; + }, + }; +}; diff --git a/tests/fixtures/mcpDoctorEnvironment.mjs b/tests/fixtures/mcpDoctorEnvironment.mjs new file mode 100644 index 0000000000..3ae197285e --- /dev/null +++ b/tests/fixtures/mcpDoctorEnvironment.mjs @@ -0,0 +1,10 @@ +import { writeFile } from "node:fs/promises"; + +// Publish the environment the doctor handed this child, then exit before any +// protocol work so the doctor reports the startup failure under test. +if (process.env.REA_MCP_DOCTOR_SEEN_PATH !== undefined) + await writeFile( + process.env.REA_MCP_DOCTOR_SEEN_PATH, + JSON.stringify(process.env), + ); +process.exit(0); diff --git a/tests/fixtures/mcpDoctorForkDescendant.mjs b/tests/fixtures/mcpDoctorForkDescendant.mjs new file mode 100644 index 0000000000..6b5fafbfbb --- /dev/null +++ b/tests/fixtures/mcpDoctorForkDescendant.mjs @@ -0,0 +1,24 @@ +import { spawn } from "node:child_process"; +import { writeFile } from "node:fs/promises"; + +// Start an inherited-stdio descendant, publish both PIDs, then exit so the +// doctor sees a dead launcher while the descendant keeps its output open. A +// detached descendant leads its own session, outside the launcher's group. It +// exits by itself after 30 seconds so a failed cleanup cannot leak it forever. +const descendant = spawn( + process.execPath, + [ + "-e", + "setInterval(() => {}, 1_000); setTimeout(() => process.exit(0), 30_000);", + ], + { + detached: process.env.REA_MCP_DOCTOR_DESCENDANT_DETACHED === "1", + stdio: "inherit", + }, +); +if (process.env.REA_MCP_DOCTOR_PID_PATH !== undefined) + await writeFile( + process.env.REA_MCP_DOCTOR_PID_PATH, + JSON.stringify({ launcher: process.pid, descendant: descendant.pid }), + ); +process.exit(0); diff --git a/tests/fixtures/mcpSchemaValidation.ts b/tests/fixtures/mcpSchemaValidation.ts index c082006275..0add64e973 100644 --- a/tests/fixtures/mcpSchemaValidation.ts +++ b/tests/fixtures/mcpSchemaValidation.ts @@ -15,34 +15,43 @@ export const createMcpSchemaValidation = () => { const assertionValidators = new Map(); const meta = new Ajv2020({ strict: false, validateFormats: false }); let errors: unknown = null; + const compiledValidator = (key: string): JsonSchemaValidator => { + const schema = JSON.parse(key) as Record; + // Keep shared definitions as compiled functions instead of expanding them + // at every reference. Other dialects retain SDK dispatch. + const provider = + schema.$schema === undefined || + schema.$schema === "https://json-schema.org/draft/2020-12/schema" + ? new AjvJsonSchemaValidator( + addFormats( + new Ajv2020({ + strict: false, + validateFormats: true, + validateSchema: false, + allErrors: true, + inlineRefs: false, + code: { optimize: false }, + }), + ), + ) + : new AjvJsonSchemaValidator(); + // Each full schema owns its provider, including dialect dispatch. + // Distinct schemas may legally reuse the same $id in separate servers. + return provider.getValidator(schema as JsonSchemaType); + }; return { getValidator( schema: JsonSchemaType | Readonly>, ): JsonSchemaValidator { + // Snapshot the schema now, so later caller mutation cannot change it. const key = JSON.stringify(schema); let validator = sdkValidators.get(key); if (validator === undefined) { - // Keep shared definitions as compiled functions instead of expanding - // them at every reference. Other dialects retain SDK dispatch. - const provider = - schema.$schema === undefined || - schema.$schema === "https://json-schema.org/draft/2020-12/schema" - ? new AjvJsonSchemaValidator( - addFormats( - new Ajv2020({ - strict: false, - validateFormats: true, - validateSchema: false, - allErrors: true, - inlineRefs: false, - code: { optimize: false }, - }), - ), - ) - : new AjvJsonSchemaValidator(); - // Each full schema owns its provider, including dialect dispatch. - // Distinct schemas may legally reuse the same $id in separate servers. - validator = provider.getValidator(JSON.parse(key)); + // The SDK prepares a validator for every listed tool before its first + // call. Compile on first use, so a session pays only for the tools it + // calls; REA's full catalog otherwise costs seconds of compilation. + let compiled: JsonSchemaValidator | undefined; + validator = (input) => (compiled ??= compiledValidator(key))(input); sdkValidators.set(key, validator); } return validator as JsonSchemaValidator; diff --git a/tests/fixtures/promptOccurrenceCompletion.ts b/tests/fixtures/promptOccurrenceCompletion.ts new file mode 100644 index 0000000000..8bcd536d65 --- /dev/null +++ b/tests/fixtures/promptOccurrenceCompletion.ts @@ -0,0 +1,180 @@ +import assert from "node:assert/strict"; +import { Client, InMemoryTransport } from "@modelcontextprotocol/client"; +import { AnalysisProviderRegistry } from "../../src/application/binary/AnalysisProviderRegistry.js"; +import { BinarySession } from "../../src/application/binary/BinarySession.js"; +import { SessionProviderRouter } from "../../src/application/binary/SessionProviderRouter.js"; +import type { + ArtifactNode, + ArtifactOccurrence, +} from "../../src/domain/artifactGraph.js"; +import { + artifactGraphDigest, + artifactIdForContent, + artifactManifestId, + occurrenceIdForLocation, +} from "../../src/domain/artifactIdentity.js"; +import { parseArtifactInventoryEvidence } from "../../src/domain/artifactInventoryEvidence.js"; +import { compareUnicodeCodePoints } from "../../src/domain/unicodeCodePointOrder.js"; +import { createEvidence } from "../../src/domain/evidence.js"; +import { createServer } from "../../src/server/createServer.js"; + +// Run on Node's main-thread stack, like the stdio MCP server. No real ZIP or +// provider is involved: these are authenticated, synthetic inventory facts. +const count = Number(process.argv[2]); +const started = performance.now(); +const sha256 = "0".repeat(64); +const root = artifactIdForContent(sha256); +const rootOccurrence = occurrenceIdForLocation({ + rootArtifactId: root, + logicalPath: ".", + entryKind: "file", +}); +const nodes: ArtifactNode[] = [ + { + artifact_id: root, + format: "zip", + sha256, + size: 16_000_000, + media_type: null, + architecture: null, + content_state: "materialized", + limitations: [], + }, +]; +const base: Omit = { + artifact_id: null, + parent_occurrence_id: rootOccurrence, + relationship_coverage: "unknown", + entry_kind: "file", + artifact_kind: "resource", + artifact_format: "file", + declared_size: 0, + compressed_size: 0, + executable: false, + encrypted: false, + hash_status: "not-hashed", + source_location: null, + limitations: [], +}; +const occurrences: ArtifactOccurrence[] = [ + { + ...base, + occurrence_id: rootOccurrence, + artifact_id: root, + parent_occurrence_id: null, + logical_path: ".", + artifact_kind: "container", + artifact_format: "zip", + declared_size: 16_000_000, + compressed_size: null, + hash_status: "verified", + }, + ...Array.from({ length: count }, (_, index): ArtifactOccurrence => { + const logicalPath = `entry${String(index).padStart(6, "0")}`; + return { + ...base, + logical_path: logicalPath, + occurrence_id: occurrenceIdForLocation({ + rootArtifactId: root, + logicalPath, + entryKind: "file", + }), + }; + }), +]; +const graphSha256 = artifactGraphDigest({ + nodes, + occurrences, + edges: [], + contradictions: [], +}); +const evidence = createEvidence( + { path: "synthetic-inventory.zip", sha256, format: "zip" }, + { id: "rea-artifact", name: "REA artifact graph", version: "1" }, + { + operation: "inventory_artifact", + parameters: {}, + confidence: "observed", + authority: "shipped-artifact", + result: { + manifest: { + manifest_id: artifactManifestId(root, graphSha256), + root_artifact_id: root, + root_sha256: sha256, + root_format: "zip", + graph_sha256: graphSha256, + node_count: nodes.length, + occurrence_count: occurrences.length, + edge_count: 0, + }, + nodes, + occurrences, + edges: [], + provenance: [], + integrity_contradictions: [], + coverage: { scope: "container-entries", status: "unknown" }, + limitations: ["Synthetic inventory; no actual ZIP contents are claimed."], + }, + }, +); +assert.equal( + parseArtifactInventoryEvidence(evidence).inventory.occurrences.length, + count + 1, +); +const session = new BinarySession( + SessionProviderRouter.selectable(new AnalysisProviderRegistry([]), []), +); +const retained = session.recordEvidence(evidence); +if (!retained.ok) throw retained.error; +const server = createServer({ kind: "session", session }, { environment: {} }); +const client = new Client({ + name: "occurrence-completion-control", + version: "1", +}); +const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair(); +try { + await server.connect(serverTransport); + await client.connect(clientTransport); + const complete = async (value: string) => + ( + await client.complete({ + ref: { type: "ref/prompt", name: "compare_application_versions" }, + argument: { name: "focus_occurrence_id", value }, + }) + ).completion; + const expected = occurrences + .map(({ occurrence_id }) => occurrence_id) + .sort(compareUnicodeCodePoints); + const result = await complete("OCC_"); + assert.deepEqual(result, { + values: expected.slice(0, 100), + total: expected.length, + hasMore: expected.length > 100, + }); + // Prefix matching must examine IDs beyond the first completion page. + const last = expected.at(-1)!; + assert.deepEqual(await complete(last), { + values: [last], + total: 1, + hasMore: false, + }); + assert.deepEqual(await complete("missing"), { + values: [], + total: 0, + hasMore: false, + }); + console.log( + JSON.stringify({ + values: result.values.length, + total: result.total, + hasMore: result.hasMore, + elapsed_ms: performance.now() - started, + cpu: process.cpuUsage(), + process_peak_kib: process.resourceUsage().maxRSS, + }), + ); +} finally { + await client.close(); + await server.close(); + await session.close(); +} diff --git a/tests/fixtures/uimage.ts b/tests/fixtures/uimage.ts index 64cfd5e462..71dafb3360 100644 --- a/tests/fixtures/uimage.ts +++ b/tests/fixtures/uimage.ts @@ -5,7 +5,7 @@ const UIMAGE_HEADER_SIZE = 64; const UIMAGE_MAGIC = 0x27051956; const UIMAGE_NAME_LENGTH = 32; -export interface UImageFixtureOptions { +interface UImageFixtureOptions { readonly arch?: number; readonly os?: number; readonly type?: number; diff --git a/tests/fixtures/universalMacho.ts b/tests/fixtures/universalMacho.ts index 208c1257ac..ef4289e28a 100644 --- a/tests/fixtures/universalMacho.ts +++ b/tests/fixtures/universalMacho.ts @@ -1,5 +1,5 @@ /** One thin Mach-O slice and the CPU identity its header and FAT entry declare. */ -export interface UniversalSlice { +interface UniversalSlice { readonly bytes: Buffer; readonly cpuType: number; readonly cpuSubtype: number; @@ -8,7 +8,7 @@ export interface UniversalSlice { } /** The FAT32 container and where each slice landed. */ -export interface UniversalMachoFixture { +interface UniversalMachoFixture { readonly bytes: Buffer; readonly slices: readonly { readonly offset: number; diff --git a/tests/fixtures/webBundleAnalysis.ts b/tests/fixtures/webBundleAnalysis.ts new file mode 100644 index 0000000000..e48d334eaa --- /dev/null +++ b/tests/fixtures/webBundleAnalysis.ts @@ -0,0 +1,99 @@ +import type { WebBundleAnalysis } from "../../src/domain/webBundleAnalysis.js"; + +export const sampleWebBundleAnalysis = (): WebBundleAnalysis => ({ + capture: { + target_url: "https://example.test/app", + target_origin: "https://example.test", + scripts_observed: 3, + scripts_analyzed: 2, + source_artifacts: [], + }, + observations: { + chunks: { + nodes: [ + { + script_key: "app.js", + url: "https://example.test/app.js", + artifact_sha256: "a".repeat(64), + bytes: 1200, + }, + { + script_key: "vendor.js", + url: "https://example.test/vendor.js", + artifact_sha256: "b".repeat(64), + bytes: 4500, + }, + ], + edges: [], + }, + routes: [], + endpoints: [ + { + value: "/api/users", + mechanism: "call:fetch", + location: { script_key: "app.js", line: 10, column: 2 }, + }, + { + value: "/api/users", + mechanism: "call:fetch", + location: { script_key: "vendor.js", line: 42, column: 8 }, + }, + { + value: "/api/orders", + mechanism: "call:sendBeacon", + location: { script_key: "app.js", line: 85, column: 4 }, + }, + ], + webmcp_declarations: [], + source_maps: { + status: "not_requested", + requested: 0, + processed: 0, + items: [], + }, + }, + inferences: [ + { + kind: "endpoint_candidate", + value: "/api/dynamic-user", + confidence: "low", + basis: [ + { + script_key: "app.js", + artifact_sha256: "a".repeat(64), + line: 25, + column: 4, + detector: "call:client.get:unverified-network-receiver", + }, + ], + }, + { + kind: "vendor_fingerprint", + value: "React", + confidence: "high", + basis: [ + { + script_key: "vendor.js", + artifact_sha256: "b".repeat(64), + line: 1, + column: 0, + detector: "vendor:react", + }, + ], + }, + ], + unknowns: [ + { + dimension: "network", + reason: "Unverified client receiver", + affected_script_keys: ["app.js"], + }, + ], + completeness: { + status: "partial", + parsed_scripts: 2, + parse_failures: 1, + visited_ast_nodes: 150, + }, + limitations: ["Script evaluation was omitted"], +}); diff --git a/tests/fixtures/webSourceLocation.ts b/tests/fixtures/webSourceLocation.ts index 41744da508..15e988ca09 100644 --- a/tests/fixtures/webSourceLocation.ts +++ b/tests/fixtures/webSourceLocation.ts @@ -1,8 +1,10 @@ import { createHash } from "node:crypto"; -import type { WebSourceLocationArtifacts } from "../../src/application/WebSourceLocationPorts.js"; +import type { + WebSourceLocationArtifacts, + WebSourceMapPort, +} from "../../src/application/WebSourceLocationPorts.js"; import { webModuleArtifactsFixture } from "./webModuleTrace.js"; import { traceSourceMap } from "../../src/javascript/sourceMaps/TraceSourceMap.js"; -import type { WebSourceMapPort } from "../../src/application/WebSourceLocationPorts.js"; import { ok } from "../../src/domain/result.js"; /** Inert codec seam for workflow tests; real process verification is separate. */ diff --git a/tests/fixtures/wxapkg.ts b/tests/fixtures/wxapkg.ts index 58fa5211d0..9267ef2138 100644 --- a/tests/fixtures/wxapkg.ts +++ b/tests/fixtures/wxapkg.ts @@ -9,7 +9,7 @@ export interface WxapkgMember { readonly size?: number; } -export interface WxapkgOptions { +interface WxapkgOptions { readonly firstMark?: number; readonly lastMark?: number; readonly info?: number; diff --git a/tests/process-global/apkSignatureLifecycle.test.ts b/tests/process-global/apkSignatureLifecycle.test.ts index 407a61d82a..5972eb9f3a 100644 --- a/tests/process-global/apkSignatureLifecycle.test.ts +++ b/tests/process-global/apkSignatureLifecycle.test.ts @@ -3,6 +3,7 @@ import { tmpdir } from "node:os"; import { join } from "node:path"; import { TextReader, Uint8ArrayWriter, ZipWriter } from "@zip.js/zip.js"; import { afterAll, afterEach, beforeAll, expect, it, vi } from "vitest"; +import { awaitAcknowledgement } from "../fixtures/lifecycleAcknowledgement.js"; const fault = vi.hoisted(() => ({ path: "", @@ -100,16 +101,19 @@ it("serializes reads across providers, preserves queued aborts and drains admitt resume: resume.promise, }; const reading = first.inspect({ path }); - await started.promise; - const queued = second.inspect({ path }); const controller = new AbortController(); const reason = new Error("cancel while queued"); - const cancelled = second.inspect({ path }, { signal: controller.signal }); + let queued: ReturnType | undefined; + let cancelled: ReturnType | undefined; + let closing: Promise | undefined; let closed = false; - const closing = second.close().then(() => { - closed = true; - }); try { + await awaitAcknowledgement(started.promise, reading, "APK signature read"); + queued = second.inspect({ path }); + cancelled = second.inspect({ path }, { signal: controller.signal }); + closing = second.close().then(() => { + closed = true; + }); controller.abort(reason); expect(await cancelled).toMatchObject({ ok: false, @@ -123,7 +127,12 @@ it("serializes reads across providers, preserves queued aborts and drains admitt expect(closed).toBe(true); } finally { resume.resolve(); - await Promise.all([reading, queued, cancelled, closing]); + await Promise.allSettled([ + reading, + ...(queued ? [queued] : []), + ...(cancelled ? [cancelled] : []), + ...(closing ? [closing] : []), + ]); await first.close(); await second.close(); } diff --git a/tests/process-global/curatedWindowsLane.test.ts b/tests/process-global/curatedWindowsLane.test.ts index 83b144075a..cf31c7fd96 100644 --- a/tests/process-global/curatedWindowsLane.test.ts +++ b/tests/process-global/curatedWindowsLane.test.ts @@ -69,7 +69,7 @@ describe("curated Windows test lane", () => { paths.map(async (path) => ((await exists(path)) ? undefined : path)), ); expect(missing.filter((path) => path !== undefined)).toEqual([]); - }, 10_000); + }); it("keeps the native multi-megabyte output regression in the lane", async () => { expect(await explicitPaths("windows")).toContain( @@ -188,7 +188,7 @@ it("keeps every explicit focused CI test path executable after file moves", asyn paths.map(async (path) => ((await exists(path)) ? undefined : path)), ); expect(missing.filter((path) => path !== undefined)).toEqual([]); -}, 10_000); +}); it("checks the complete source with the fallback compiler before Windows build reuse", async () => { const workflow = workflowSchema.parse( diff --git a/tests/process-global/cutterDescriptorLifetime.test.ts b/tests/process-global/cutterDescriptorLifetime.test.ts index f0e6044626..f2838321b2 100644 --- a/tests/process-global/cutterDescriptorLifetime.test.ts +++ b/tests/process-global/cutterDescriptorLifetime.test.ts @@ -40,6 +40,7 @@ import { projectAnalysisError } from "../../src/domain/analysisErrorProjection.j import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js"; import { awaitAcknowledgement } from "../fixtures/lifecycleAcknowledgement.js"; import { cliTest } from "../support/cli/cliFixture.js"; +import { authenticatedServer } from "../fixtures/cutterBridgeProtocol.js"; beforeEach(() => { fault.root = ""; @@ -54,7 +55,7 @@ afterEach(async () => { for (const handle of fault.handles) await handle.close(); }); -const descriptor = async (root: string, index: number) => { +const descriptor = async (root: string, index: number, port = 9) => { const session_id = `00000000-0000-4000-8000-${String(index).padStart(12, "0")}`; const path = join(root, `cutter-${process.pid}-${session_id}.json`); await writeFile( @@ -63,7 +64,7 @@ const descriptor = async (root: string, index: number) => { session_id, pid: process.pid, host: "127.0.0.1", - port: 9, + port, token: "fixture-token-never-sent-to-network", document_generation: 0, current_file: null, @@ -109,16 +110,14 @@ it("retains exact failed descriptors and keeps aggregate status until every clos const client = new CutterBridgeClient({ REA_CUTTER_BRIDGE_DIR: root }); try { const original = await client - .listDescriptors() + .listSessions() .catch((cause: unknown) => cause); if (!(original instanceof AnalysisError)) throw new Error("Expected typed cleanup aggregate"); expect(original.cleanupIncomplete).toBe(true); expect(original.cleanupResources).toHaveLength(2); expect(fault.handles).toHaveLength(2); - const retry = await client - .listDescriptors() - .catch((cause: unknown) => cause); + const retry = await client.listSessions().catch((cause: unknown) => cause); if (!(retry instanceof AnalysisError)) throw new Error("Expected blocked retry"); expect(fault.handles).toHaveLength(2); @@ -150,7 +149,18 @@ it("shutdown waits for descriptor finalization before retrying retained cleanup" const root = await createTestTempDirectory( "rea-cutter-descriptor-finalization-", ); - await descriptor(root, 1); + const sessionId = "00000000-0000-4000-8000-000000000001"; + const server = authenticatedServer( + "fixture-token-never-sent-to-network", + sessionId, + (request, respond) => respond({ ok: true, session_id: request.session_id }), + true, + ); + await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve)); + const address = server.address(); + if (address === null || typeof address === "string") + throw new Error("Expected a local Cutter test port"); + await descriptor(root, 1, address.port); fault.root = root; let entered!: () => void; let release!: () => void; @@ -165,7 +175,7 @@ it("shutdown waits for descriptor finalization before retrying retained cleanup" await gate; }; const client = new CutterBridgeClient({ REA_CUTTER_BRIDGE_DIR: root }); - const reading = client.listDescriptors(); + const reading = client.listSessions(); let shutdown: Promise | undefined; try { await awaitAcknowledgement(closing, reading, "descriptor finalization"); @@ -177,7 +187,10 @@ it("shutdown waits for descriptor finalization before retrying retained cleanup" expect(settled).toBe(false); fault.blocked.clear(); release(); - expect(await reading).toHaveLength(1); + expect(await reading).toMatchObject({ + discovery_status: "sessions_found", + sessions: [{ session_id: sessionId }], + }); await shutdown; expect(fault.handles.every((handle) => handle.fd === -1)).toBe(true); } finally { @@ -186,6 +199,7 @@ it("shutdown waits for descriptor finalization before retrying retained cleanup" fault.blocked.clear(); await Promise.allSettled([reading, ...(shutdown ? [shutdown] : [])]); await client.close(); + await new Promise((resolve) => server.close(() => resolve())); } }); diff --git a/tests/process-global/environmentFinalizationCli.test.ts b/tests/process-global/environmentFinalizationCli.test.ts index feeb77af9d..0a7a5ce318 100644 --- a/tests/process-global/environmentFinalizationCli.test.ts +++ b/tests/process-global/environmentFinalizationCli.test.ts @@ -5,6 +5,7 @@ import type { ServedEnvironment } from "../../src/server/environment/serveEnviro import type { ServeStreamableHttpOptions } from "../../src/server/http/serveStreamableHttp.js"; import { StreamableHttpListenError } from "../../src/server/http/streamableHttpListenError.js"; import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js"; +import { awaitAcknowledgement } from "../fixtures/lifecycleAcknowledgement.js"; import { writeEnvironmentServeFiles } from "../fixtures/environmentServe.js"; const control = vi.hoisted(() => ({ @@ -130,7 +131,12 @@ it.each([ }, ); try { - await started; + await awaitAcknowledgement( + started, + pending, + "environment CLI startup", + 10_000, + ); const served = observedEnvironment(); if (served === undefined) throw new Error("Environment was not admitted"); if (ending === "signal") { diff --git a/tests/process-global/firmwareFileLifetime.test.ts b/tests/process-global/firmwareFileLifetime.test.ts index 75bf3db075..6361594cb1 100644 --- a/tests/process-global/firmwareFileLifetime.test.ts +++ b/tests/process-global/firmwareFileLifetime.test.ts @@ -13,6 +13,7 @@ import { join } from "node:path"; import { pathToFileURL } from "node:url"; import { promisify } from "node:util"; import { afterEach, beforeEach, expect, it, vi } from "vitest"; +import { awaitAcknowledgement } from "../fixtures/lifecycleAcknowledgement.js"; const fault = vi.hoisted(() => ({ selected: "", @@ -903,7 +904,7 @@ it("waits through selected-range snapshot finalization when shutdown starts duri new AbortController().signal, ); try { - await entered; + await awaitAcknowledgement(entered, pending, "firmware source close"); let closed = false; const closing = files.close().then(() => { closed = true; @@ -1038,9 +1039,14 @@ it("shares one workspace cleanup attempt across concurrent shutdown callers", as blocked = false; const first = provider.close(); try { - await entered; + await awaitAcknowledgement(entered, first, "firmware workspace cleanup"); const second = provider.close(); - expect(second).toBe(first); + let secondSettled = false; + void second.then(() => { + secondSettled = true; + }); + await Promise.resolve(); + expect(secondSettled).toBe(false); expect(attempts).toBe(2); release(); await Promise.all([first, second]); diff --git a/tests/process-global/firmwarePublicationDeadline.test.ts b/tests/process-global/firmwarePublicationDeadline.test.ts index e0dea71d36..0e247f21ed 100644 --- a/tests/process-global/firmwarePublicationDeadline.test.ts +++ b/tests/process-global/firmwarePublicationDeadline.test.ts @@ -2,6 +2,7 @@ import { projectCliAnalysisResult } from "../../src/cli/projectCliAnalysisResult import { access, readFile } from "node:fs/promises"; import { join } from "node:path"; import { afterEach, expect, it, vi } from "vitest"; +import { awaitAcknowledgement } from "../fixtures/lifecycleAcknowledgement.js"; import * as firmwareFiles from "../../src/firmware/FirmwareFiles.js"; import { SafeOutputTree } from "../../src/artifacts/SafeOutputTree.js"; import { PrivateRuntimeRoot } from "../../src/process/PrivateRuntimeRoot.js"; @@ -219,9 +220,10 @@ test("keeps each queued request's independent analysis and publication budgets", publication_timeout_ms: 123_456, analysis_timeout_ms: 234_567, }); + let queued: typeof pending | undefined; try { - await entered; - const queued = fixture.service.execute("extract_firmware", { + await awaitAcknowledgement(entered, pending, "firmware publication"); + queued = fixture.service.execute("extract_firmware", { path: fixture.path, output_directory: join(fixture.root, "second-output"), publication_timeout_ms: 654_321, @@ -246,8 +248,12 @@ test("keeps each queued request's independent analysis and publication budgets", expect(budgets).toEqual([234_567, 123_456, 765_432, 654_321]); } finally { release(); + try { + await Promise.allSettled([pending, queued]); + } finally { + await fixture.close(); + } } - await fixture.close(); await assertFirmwareCleanup(fixture.launches); }); diff --git a/tests/process-global/flutterCliCleanup.test.ts b/tests/process-global/flutterCliCleanup.test.ts new file mode 100644 index 0000000000..c13144a4e1 --- /dev/null +++ b/tests/process-global/flutterCliCleanup.test.ts @@ -0,0 +1,103 @@ +import { Cli } from "incur"; +import { expect, it, onTestFinished, vi } from "vitest"; +import { registerFlutterCommands } from "../../src/cli/flutterCommands.js"; +import { aotFixtureImage } from "../../src/flutter/FlutterBuild.fixture.js"; +import { FlutterBuildProvider } from "../../src/flutter/FlutterBuildProvider.js"; +import { silentLogger } from "../../src/logger.js"; +import { + interceptFlutterRootRemoval, + realRm, +} from "../fixtures/flutterCleanup.js"; +import { createFlutterApk } from "../fixtures/flutterApk.js"; + +vi.mock("node:fs/promises", async (importOriginal) => { + const original = await importOriginal(); + return { ...original }; +}); + +/** Serve one command whose first snapshot-root removal fails and is retried on close. */ +const serveWithCleanupRetry = async ( + command: string, + apk: string, + interrupt: boolean, + retryFails = false, +): Promise> => { + const inherited = new Set(process.listeners("SIGINT")); + const roots: string[] = []; + interceptFlutterRootRemoval(async (root) => { + roots.push(root); + if (roots.length > 2) return realRm(root, { recursive: true, force: true }); + // Deliver SIGINT to the command's own listener during the retry. + if (roots.length === 2 && interrupt) + for (const listener of process.listeners("SIGINT")) + if (!inherited.has(listener)) listener("SIGINT"); + if (roots.length === 1 || retryFails) + throw Object.assign(new Error("transient snapshot removal refusal"), { + code: "EACCES", + }); + await realRm(root, { recursive: true, force: true }); + }); + const provider = new FlutterBuildProvider(); + onTestFinished(() => provider.close()); + const cli = Cli.create("flutter-cleanup-retry-test", { sync: false }); + registerFlutterCommands(cli, silentLogger, provider); + let stdout = ""; + await cli.serve([command, apk, "--format", "json"], { + stdout: (text) => { + stdout += text; + }, + exit: () => {}, + }); + expect(roots).toEqual([roots[0], roots[0]]); + return JSON.parse(stdout) as Record; +}; + +it.each([ + ["identify-flutter-build", "identify_flutter_build"], + ["inspect-dart-aot", "inspect_dart_aot"], +] as const)( + "%s reports SIGINT during the cleanup retry as cancellation with completed facts", + async (command, operation) => { + const apk = await createFlutterApk("cleanup-retry.apk", [ + { path: "lib/arm64-v8a/libapp.so", bytes: aotFixtureImage() }, + ]); + const exitCode = process.exitCode; + onTestFinished(() => { + process.exitCode = exitCode; + }); + const control = await serveWithCleanupRetry(command, apk, false); + expect(control).toMatchObject({ + operation, + confidence: "observed", + caller_input_path: apk, + }); + expect(process.exitCode).toBe(exitCode); + const interrupted = await serveWithCleanupRetry(command, apk, true); + expect(process.exitCode).toBe(130); + expect(interrupted).toMatchObject({ + code: "cancelled", + details: { + operation, + partial_observation: { + operation, + caller_input_path: apk, + subject: { local_path: apk }, + confidence: "observed", + normalized_result: control.normalized_result, + }, + }, + }); + const incomplete = await serveWithCleanupRetry(command, apk, true, true); + expect(incomplete).toMatchObject({ + code: "cleanup_incomplete", + details: { + resources: [expect.stringContaining("rea-flutter-")], + partial_observation: { + operation, + caller_input_path: apk, + normalized_result: control.normalized_result, + }, + }, + }); + }, +); diff --git a/tests/process-global/ghidraEndpointLifetime.test.ts b/tests/process-global/ghidraEndpointLifetime.test.ts index 8ea97201bd..cf13ffd30b 100644 --- a/tests/process-global/ghidraEndpointLifetime.test.ts +++ b/tests/process-global/ghidraEndpointLifetime.test.ts @@ -11,6 +11,7 @@ import { } from "node:fs/promises"; import { basename, join } from "node:path"; import { afterEach, beforeEach, expect, it, vi } from "vitest"; +import { awaitAcknowledgement } from "../fixtures/lifecycleAcknowledgement.js"; const fault = vi.hoisted(() => ({ path: "", @@ -243,7 +244,7 @@ it.skipIf(process.platform === "win32")( resourceScope: resources, }); operations.push(pending); - await observed; + await awaitAcknowledgement(observed, pending, "Ghidra endpoint read"); let closed = false; const closing = resources.close().then(() => { closed = true; diff --git a/tests/process-global/gpuFileLifetime.test.ts b/tests/process-global/gpuFileLifetime.test.ts index 3122e74e54..98ba4ede28 100644 --- a/tests/process-global/gpuFileLifetime.test.ts +++ b/tests/process-global/gpuFileLifetime.test.ts @@ -9,6 +9,7 @@ import { gpuCodeObjectsSchema, } from "../../src/domain/gpu/gpuCodeObjects.js"; import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js"; +import { awaitAcknowledgement } from "../fixtures/lifecycleAcknowledgement.js"; import { amdFixture, cudaFixture } from "../fixtures/gpu/codeObjects.js"; type ReadFile = ( buffer: Buffer, @@ -231,7 +232,7 @@ it("joins an admitted file read during shutdown and rejects overlapping work", a let closing: Promise | undefined; try { active = provider.inspect({ path, analysis: "kernels" }); - await admitted; + await awaitAcknowledgement(admitted, active, "GPU file read admission"); let closed = false; closing = provider.close().then(() => { closed = true; @@ -249,10 +250,16 @@ it("joins an admitted file read during shutdown and rejects overlapping work", a expect(closed).toBe(true); } finally { release(); - await active; - await closing; - spy.mockRestore(); - await provider.close(); + try { + try { + await active; + } finally { + await closing; + } + } finally { + spy.mockRestore(); + await provider.close(); + } } }); diff --git a/tests/process-global/hopperDownloadDeadline.test.ts b/tests/process-global/hopperDownloadDeadline.test.ts index 7ede01a7df..a73c609c28 100644 --- a/tests/process-global/hopperDownloadDeadline.test.ts +++ b/tests/process-global/hopperDownloadDeadline.test.ts @@ -1,12 +1,10 @@ import { afterEach, beforeEach, expect, it, vi } from "vitest"; -import { - downloadHopperPackage, - HOPPER_DOWNLOAD_STALL_MS, - HOPPER_DOWNLOAD_TIMEOUT_MS, -} from "../../src/application/HopperDownload.js"; +import { downloadHopperPackage } from "../../src/application/HopperDownload.js"; import { err, ok } from "../../src/domain/result.js"; +const HOPPER_DOWNLOAD_STALL_MS = 30_000; +const HOPPER_DOWNLOAD_TIMEOUT_MS = 15 * 60_000; const url = "https://www.hopperapp.com/downloader/public/Hopper-Linux-demo.rpm"; beforeEach(() => { diff --git a/tests/process-global/javascriptRuntimeObservationCli.test.ts b/tests/process-global/javascriptRuntimeObservationCli.test.ts index 50a52769a5..4f5d1e8e0c 100644 --- a/tests/process-global/javascriptRuntimeObservationCli.test.ts +++ b/tests/process-global/javascriptRuntimeObservationCli.test.ts @@ -29,8 +29,6 @@ vi.mock("../../src/composition/javascriptRuntimeObservation.js", () => ({ }, })); -const TIMEOUT_MS = 20_000; - describe("JavaScript runtime observation CLI parity", () => { const resources: Array<{ close(): Promise }> = []; @@ -114,7 +112,6 @@ describe("JavaScript runtime observation CLI parity", () => { await provider.close(); } }, - TIMEOUT_MS, ); test("waits for provider cleanup and retains completed Evidence on failure", async () => { diff --git a/tests/process-global/mainStartupCancellation.test.ts b/tests/process-global/mainStartupCancellation.test.ts index e1a3fec4b8..7c9c4ed83f 100644 --- a/tests/process-global/mainStartupCancellation.test.ts +++ b/tests/process-global/mainStartupCancellation.test.ts @@ -12,6 +12,7 @@ import { run } from "../../src/main.js"; import { registerProcessShutdown } from "../../src/main/processShutdown.js"; import { ownStdioStartupInput } from "../../src/main/StdioStartupInput.js"; import { MCP_SHUTDOWN_FAILED } from "../../src/main/messages.js"; +import { awaitAcknowledgement } from "../fixtures/lifecycleAcknowledgement.js"; import { createDeferred, createTestBinarySession, @@ -107,37 +108,49 @@ it.each([ registerShutdown: (handler) => registerProcessShutdown(handler, { signals, stdin: input }), }); - const signal = await entered.promise; - expect(signals.listenerCount("SIGINT")).toBe(1); - expect(signals.listenerCount("SIGTERM")).toBe(1); - if (trigger === "EOF") input.end(); - else if (trigger === "CAPACITY") - input.write(Buffer.alloc(STDIO_DEFAULT_MAX_BUFFER_SIZE + 1)); - else if (trigger === "READ_ERROR") - input.emit("error", new Error("controlled stdin read failure")); - else signals.emit(trigger); - expect(await pending).toBe(failedClose ? 1 : 0); - expect(signal.aborted).toBe(true); - expect(serve).not.toHaveBeenCalled(); - expect(close).toHaveBeenCalledWith({ retainProviderDocuments: true }); - if (failedClose) { - expect(output.join("")).toContain(MCP_SHUTDOWN_FAILED); - if (trigger === "CAPACITY" || trigger === "READ_ERROR") { - const primary = - trigger === "CAPACITY" - ? "MCP input exceeded" - : "MCP stdin could not be read"; - expect(output.join("")).toContain(primary); - expect(output.join("").indexOf(primary)).toBeLessThan( - output.join("").indexOf(MCP_SHUTDOWN_FAILED), - ); - } + try { + await awaitAcknowledgement( + entered.promise.then(() => undefined), + pending, + "initial target open", + 10_000, + ); + const signal = await entered.promise; + expect(signals.listenerCount("SIGINT")).toBe(1); + expect(signals.listenerCount("SIGTERM")).toBe(1); + if (trigger === "EOF") input.end(); + else if (trigger === "CAPACITY") + input.write(Buffer.alloc(STDIO_DEFAULT_MAX_BUFFER_SIZE + 1)); + else if (trigger === "READ_ERROR") + input.emit("error", new Error("controlled stdin read failure")); + else signals.emit(trigger); + expect(await pending).toBe(failedClose ? 1 : 0); + expect(signal.aborted).toBe(true); + expect(serve).not.toHaveBeenCalled(); + expect(close).toHaveBeenCalledWith({ retainProviderDocuments: true }); + if (failedClose) { + expect(output.join("")).toContain(MCP_SHUTDOWN_FAILED); + if (trigger === "CAPACITY" || trigger === "READ_ERROR") { + const primary = + trigger === "CAPACITY" + ? "MCP input exceeded" + : "MCP stdin could not be read"; + expect(output.join("")).toContain(primary); + expect(output.join("").indexOf(primary)).toBeLessThan( + output.join("").indexOf(MCP_SHUTDOWN_FAILED), + ); + } + closeFails = false; + signals.emit("SIGINT"); + await expect.poll(() => signals.listenerCount("SIGINT")).toBe(0); + } else expect(output).toEqual([]); + expect(signals.listenerCount("SIGTERM")).toBe(0); + } finally { closeFails = false; signals.emit("SIGINT"); - await expect.poll(() => signals.listenerCount("SIGINT")).toBe(0); - } else expect(output).toEqual([]); - expect(signals.listenerCount("SIGTERM")).toBe(0); - input.destroy(); + input.destroy(); + await pending.catch(() => undefined); + } }, ); @@ -173,25 +186,36 @@ it.each(["optional", "late-handle"] as const)( registerShutdown: (handler) => registerProcessShutdown(handler, { signals, stdin: input }), }); - await entered.promise; - if (stage === "optional") { - await new Promise((resolve) => { - input.once("end", resolve); - input.end(); + try { + await awaitAcknowledgement( + entered.promise, + pending, + "startup stage", + 10_000, + ); + if (stage === "optional") { + await new Promise((resolve) => { + input.once("end", resolve); + input.end(); + }); + } else signals.emit("SIGTERM"); + expect(closeHandle).not.toHaveBeenCalled(); + expect(closeSession).not.toHaveBeenCalled(); + release.resolve(); + expect(await pending).toBe(0); + expect(serve).toHaveBeenCalledTimes(stage === "optional" ? 0 : 1); + expect(closeHandle).toHaveBeenCalledTimes(stage === "optional" ? 0 : 1); + expect(closeSession).toHaveBeenCalledTimes(1); + expect(closeSession).toHaveBeenCalledWith({ + retainProviderDocuments: true, }); - } else signals.emit("SIGTERM"); - expect(closeHandle).not.toHaveBeenCalled(); - expect(closeSession).not.toHaveBeenCalled(); - release.resolve(); - expect(await pending).toBe(0); - expect(serve).toHaveBeenCalledTimes(stage === "optional" ? 0 : 1); - expect(closeHandle).toHaveBeenCalledTimes(stage === "optional" ? 0 : 1); - expect(closeSession).toHaveBeenCalledTimes(1); - expect(closeSession).toHaveBeenCalledWith({ - retainProviderDocuments: true, - }); - expect(signals.listenerCount("SIGINT")).toBe(0); - input.destroy(); + expect(signals.listenerCount("SIGINT")).toBe(0); + } finally { + signals.emit("SIGTERM"); + release.resolve(); + input.destroy(); + await pending.catch(() => undefined); + } }, ); diff --git a/tests/process-global/oneShotSourceClosure.test.ts b/tests/process-global/oneShotSourceClosure.test.ts index 9afbdaed77..7bfcba5cfd 100644 --- a/tests/process-global/oneShotSourceClosure.test.ts +++ b/tests/process-global/oneShotSourceClosure.test.ts @@ -318,7 +318,7 @@ it("keeps cancellation typed for a captured source with a data-sized link set", omitted_count: 1, omitted: [{ evidence_id: wide.evidence_id, reason: "not_inlined" }], }); -}, 60_000); +}); it("hands off the same closure for summary delivery and permits a clean retry", async () => { const input = await fixture(); diff --git a/tests/process-global/pwntoolsCleanup.test.ts b/tests/process-global/pwntoolsCleanup.test.ts index 7735c8ba9a..b7e35683b1 100644 --- a/tests/process-global/pwntoolsCleanup.test.ts +++ b/tests/process-global/pwntoolsCleanup.test.ts @@ -15,6 +15,7 @@ import { spawnOwnedProviderProcess } from "../../src/process/ProviderProcess.js" import { cleanupOwnedProcessGroup } from "../../src/process/ProcessOwnership.js"; import { prepareProcessOwnershipInspection } from "../../src/process/ProcessOwnershipObservation.js"; import { stopProviderProcessFixture } from "../fixtures/providerProcess.js"; +import { awaitAcknowledgement } from "../fixtures/lifecycleAcknowledgement.js"; import { projectAnalysisError } from "../../src/domain/analysisErrorProjection.js"; import { createTestWorkspace, @@ -265,7 +266,7 @@ it.runIf(supportedHost)( await provider.close(); }); const first = provider.inspect({ path }); - await started; + await awaitAcknowledgement(started, first, "pwntools launch", 10_000); const controller = new AbortController(); const cancelled = provider.inspect({ path }, { signal: controller.signal }); const reason = { stage: "queued" }; diff --git a/tests/process-global/referenceSourceCancellation.test.ts b/tests/process-global/referenceSourceCancellation.test.ts index 87fc63e5c5..553cc54d34 100644 --- a/tests/process-global/referenceSourceCancellation.test.ts +++ b/tests/process-global/referenceSourceCancellation.test.ts @@ -113,11 +113,14 @@ it.each([ } finally { held?.release(); releaseClose?.(); - await running; - fileClose.mockRestore(); - scopeClose.mockRestore(); - vi.restoreAllMocks(); - process.exitCode = previousExitCode; + try { + await running; + } finally { + fileClose.mockRestore(); + scopeClose.mockRestore(); + vi.restoreAllMocks(); + process.exitCode = previousExitCode; + } } expect(await readFile(file, "utf8")).toBe(contents); }, diff --git a/tests/process-global/setupConfigurationConflicts.test.ts b/tests/process-global/setupConfigurationConflicts.test.ts index 6be1b320f4..1600b43e0a 100644 --- a/tests/process-global/setupConfigurationConflicts.test.ts +++ b/tests/process-global/setupConfigurationConflicts.test.ts @@ -11,6 +11,7 @@ import { } from "node:fs/promises"; import { join, relative } from "node:path"; import { afterEach, expect, it, vi } from "vitest"; +import { awaitAcknowledgement } from "../fixtures/lifecycleAcknowledgement.js"; const writers = vi.hoisted(() => ({ before: undefined as undefined | ((path: string) => Promise), @@ -371,9 +372,14 @@ it.skipIf(process.platform === "win32")( }, { FIRST: "1" }, ); + let second: ReturnType | undefined; try { - await started; - const second = configureClientConfiguration( + await awaitAcknowledgement( + started, + first, + "setup configuration writer", + ); + second = configureClientConfiguration( { name: "cursor", registrationProfile: supportedClientRegistrationProfile("cursor"), @@ -413,6 +419,7 @@ it.skipIf(process.platform === "win32")( } finally { releaseFirst(); writers.before = undefined; + await Promise.allSettled([first, second]); } } }, @@ -466,26 +473,35 @@ it("releases a failed setup transaction so a queued request can configure the fi }; const first = configureClientConfiguration(client, { FIRST: "1" }); - await started; - const second = configureClientConfiguration(client, { SECOND: "2" }); - releaseWriter(); - const [failed, queued] = await Promise.all([first, second]); + let second: ReturnType | undefined; + try { + await awaitAcknowledgement(started, first, "setup configuration writer"); + second = configureClientConfiguration(client, { SECOND: "2" }); + releaseWriter(); + const [failed, queued] = await Promise.all([first, second]); - expect(failed).toMatchObject({ - status: "failed", - reason: "write", - failure: { - operation: "publish", - path, - detail: "The first writer could not stage its configuration", - }, - }); - expect(queued.status).toBe("configured"); - expect(JSON.parse(await readFile(path, "utf8")).mcpServers.rea.env).toEqual({ - SECOND: "2", - }); - expect(await readdir(root)).toEqual(["config.json"]); - expect(await configureClientConfiguration(client)).toEqual({ - status: "unchanged", - }); + expect(failed).toMatchObject({ + status: "failed", + reason: "write", + failure: { + operation: "publish", + path, + detail: "The first writer could not stage its configuration", + }, + }); + expect(queued.status).toBe("configured"); + expect(JSON.parse(await readFile(path, "utf8")).mcpServers.rea.env).toEqual( + { + SECOND: "2", + }, + ); + expect(await readdir(root)).toEqual(["config.json"]); + expect(await configureClientConfiguration(client)).toEqual({ + status: "unchanged", + }); + } finally { + releaseWriter(); + writers.before = undefined; + await Promise.allSettled([first, second]); + } }); diff --git a/tests/process-global/setupConfigurationRollback.test.ts b/tests/process-global/setupConfigurationRollback.test.ts index 48357c8a41..27e8b4e245 100644 --- a/tests/process-global/setupConfigurationRollback.test.ts +++ b/tests/process-global/setupConfigurationRollback.test.ts @@ -156,6 +156,7 @@ import { systemUninstallHost } from "../../src/application/Uninstall.js"; import { ConfigurationError } from "../../src/domain/configurationErrors.js"; import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js"; import { createDeferred } from "../fixtures/binarySession.js"; +import { awaitAcknowledgement } from "../fixtures/lifecycleAcknowledgement.js"; import { supportedClientRegistrationProfile } from "../../src/application/SupportedClients.js"; const ownReadHandle = async (path: string) => { @@ -854,18 +855,25 @@ it("uninstall close joins a publication already admitted by its host", async () configPath: path, format: "json", }); - await arrived.promise; let closed = false; - const closing = host.close?.().then((result) => { - closed = true; - return result; - }); + let closing: ReturnType> | undefined; try { + await awaitAcknowledgement( + arrived.promise, + removal, + "configuration publication", + ); + closing = host.close?.().then((result) => { + closed = true; + return result; + }); await new Promise((resolve) => setImmediate(resolve)); expect(closed).toBe(false); expect(state.publicationHandles[0]!.fd).toBeGreaterThanOrEqual(0); } finally { release.resolve(); + await Promise.allSettled([removal, ...(closing ? [closing] : [])]); + await host.close?.(); } expect((await removal).status).toBe("failed"); expect(await closing).toBeUndefined(); @@ -1035,7 +1043,11 @@ it("uninstall progresses when a peer leaves a denied-close descriptor pending", }); let timeout: ReturnType | undefined; try { - await arrived.promise; + await awaitAcknowledgement( + arrived.promise, + removal, + "configuration publication", + ); state.initialCleanupFailure = new RegularFileCleanupFailure({ path: other, owner, diff --git a/tests/process-global/shortFileReads.test.ts b/tests/process-global/shortFileReads.test.ts index 94f0d6d212..3d16a688c3 100644 --- a/tests/process-global/shortFileReads.test.ts +++ b/tests/process-global/shortFileReads.test.ts @@ -337,7 +337,7 @@ it("keeps fuse sentinel offsets, states and complete-file digest across short re fault.path = path; fault.maximum = 2; expect(await provider.inspect({ path })).toEqual(expected); - const scanned = await readElectronFuseImage(path, { chunkBytes: 7 }); + const scanned = await readElectronFuseImage(path); expect(scanned.sha256).toBe( createHash("sha256").update(bytes).digest("hex"), ); @@ -413,7 +413,8 @@ it.each(["cancelled", "eof", "io"] as const)( async (outcome) => { const root = await createTestTempDirectory("rea-fuse-scan-short-read-"); const path = join(root, "Electron"); - await writeFile(path, thinFuseImage(OBSIDIAN_FUSE_BYTES).bytes); + const image = thinFuseImage(OBSIDIAN_FUSE_BYTES); + await writeFile(path, image.bytes); const controller = new AbortController(); const physicalFailure = Object.assign(new Error("scan read failed"), { code: "EIO", @@ -422,9 +423,8 @@ it.each(["cancelled", "eof", "io"] as const)( const scanPositions: number[] = []; fault.path = path; fault.beforeRead = (position, length) => { - // Header reads use their format windows. A seven-byte read at zero - // starts this invocation's explicitly selected scan window. - if (position === 0 && length === 7) { + // Header reads use their format windows; the scan starts with the full file. + if (position === 0 && length === image.bytes.length) { scanning = true; fault.maximum = 2; } @@ -440,7 +440,6 @@ it.each(["cancelled", "eof", "io"] as const)( }; const failure: unknown = await readElectronFuseImage(path, { signal: controller.signal, - chunkBytes: 7, }).catch((cause: unknown) => cause); expect(scanning).toBe(true); expect(scanPositions).toEqual([0, 2, 4]); diff --git a/tests/process-global/updateMaintenance.test.ts b/tests/process-global/updateMaintenance.test.ts index 75b18ad8ef..1bd4a290f4 100644 --- a/tests/process-global/updateMaintenance.test.ts +++ b/tests/process-global/updateMaintenance.test.ts @@ -1,7 +1,4 @@ -import { - UpdateCommandRunner, - UPDATE_COMMAND_OUTPUT_BYTES, -} from "../../src/application/UpdateCommandRunner.js"; +import { UpdateCommandRunner } from "../../src/application/UpdateCommandRunner.js"; import { mkdir, readFile, rm, writeFile, access } from "node:fs/promises"; import { delimiter, dirname, join, relative, resolve } from "node:path"; import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; @@ -19,6 +16,7 @@ import { systemUpdateHost } from "../../src/application/UpdateRuntime.js"; import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js"; import { cliTest } from "../support/cli/cliFixture.js"; +const UPDATE_COMMAND_OUTPUT_BYTES = 4 * 1024 * 1024; const entryPoint = resolve("scripts/rea.mjs"); let home: string; @@ -575,19 +573,15 @@ describe("updated executable maintenance planning", () => { }); describe("update subprocess boundaries", () => { - cliTest( - "exposes update in CLI help", - async ({ cli }) => { - const help = await cli.run({ - arguments: ["--help"], - environment: process.env, - }); - expect(help.exitCode, help.stderr).toBe(0); - expect(help.stdout).toMatch(/^\s{2}update\s/mu); - expect(help.stdout).not.toMatch(/^\s{2}upgrade\s/mu); - }, - 60_000, - ); + cliTest("exposes update in CLI help", async ({ cli }) => { + const help = await cli.run({ + arguments: ["--help"], + environment: process.env, + }); + expect(help.exitCode, help.stderr).toBe(0); + expect(help.stdout).toMatch(/^\s{2}update\s/mu); + expect(help.stdout).not.toMatch(/^\s{2}upgrade\s/mu); + }); cliTest("rejects the removed upgrade CLI command", async ({ cli }) => { const removed = await cli.run({ diff --git a/tests/process-global/updateOutput.test.ts b/tests/process-global/updateOutput.test.ts index a8de5b7cd1..d74be6b2d7 100644 --- a/tests/process-global/updateOutput.test.ts +++ b/tests/process-global/updateOutput.test.ts @@ -1,14 +1,13 @@ import { ProviderProcessSupervisor } from "../../src/process/ProviderProcess.js"; import { systemUpdateHost } from "../../src/application/UpdateRuntime.js"; -import { - UpdateCommandRunner, - UPDATE_COMMAND_OUTPUT_BYTES, -} from "../../src/application/UpdateCommandRunner.js"; +import { UpdateCommandRunner } from "../../src/application/UpdateCommandRunner.js"; import { spawn, type ChildProcess } from "node:child_process"; import { setImmediate as nextTurn } from "node:timers/promises"; import { Writable } from "node:stream"; import { afterEach, beforeEach, expect, it, vi } from "vitest"; +import { awaitAcknowledgement } from "../fixtures/lifecycleAcknowledgement.js"; +const UPDATE_COMMAND_OUTPUT_BYTES = 4 * 1024 * 1024; const execution = vi.hoisted(() => ({ spawn: vi.fn() })); vi.mock("cross-spawn", () => ({ default: execution.spawn })); let child: ChildProcess | undefined; @@ -191,14 +190,19 @@ it("stops and joins metadata discovery when the update host closes", async () => const ready = nextChildReady(hangingProbe); const host = systemUpdateHost(process.cwd(), process.cwd(), process.env); const probing = host.installation(); - await ready; - expect(child?.exitCode).toBeNull(); - expect(host.close).toBeTypeOf("function"); - if (host.close === undefined) throw new Error("Missing update host close"); - expect(await host.close()).toBeUndefined(); - // Close must join the probe itself, not rely on the later probe deadline. - expect(child?.signalCode).not.toBeNull(); - await probing; + try { + await awaitAcknowledgement(ready, probing, "update probe readiness"); + expect(child?.exitCode).toBeNull(); + expect(host.close).toBeTypeOf("function"); + if (host.close === undefined) throw new Error("Missing update host close"); + expect(await host.close()).toBeUndefined(); + // Close must join the probe itself, not rely on the later probe deadline. + expect(child?.signalCode).not.toBeNull(); + await probing; + } finally { + await host.close?.(); + await probing; + } }); it("bounds a stalled metadata probe and keeps the runner usable", async () => { @@ -206,7 +210,7 @@ it("bounds a stalled metadata probe and keeps the runner usable", async () => { updateCommands = new UpdateCommandRunner(2000); const ready = nextChildReady(); const pending = updateCommands.runProbe(command(hangingProbe), process.env); - await ready; + await awaitAcknowledgement(ready, pending, "update probe readiness"); const result = await pending; expect(result).toMatchObject({ ok: false, @@ -235,7 +239,7 @@ it("does not launch probes after close, including a queued launch", async () => it("retains incomplete probe cleanup until a later close succeeds", async () => { const ready = nextChildReady(); const pending = updateCommands.runProbe(command(hangingProbe), process.env); - await ready; + await awaitAcknowledgement(ready, pending, "update probe readiness"); const stop = vi .spyOn(ProviderProcessSupervisor.prototype, "stop") .mockResolvedValue({ @@ -269,20 +273,25 @@ it("joins an install past the metadata deadline without cancelling it", async () process.env, "structured", ); - await ready; + await awaitAcknowledgement(ready, pending, "update install readiness"); await updateCommands.close(); expect(await pending).toEqual({ ok: true, value: "" }); expect(child?.exitCode).toBe(0); }); it("preserves cancellation during final progress acknowledgement", async () => { - let acknowledge!: () => void; + let acknowledge: (() => void) | undefined; + let releasing = false; let writing!: () => void; const started = new Promise((resolve) => { writing = resolve; }); const destination = new Writable({ write(_chunk, _encoding, callback) { + if (releasing) { + callback(); + return; + } acknowledge = callback; writing(); }, @@ -295,12 +304,23 @@ it("preserves cancellation during final progress acknowledgement", async () => { process.env, "human", ); - await started; - const closing = updateCommands.close(); - acknowledge(); - expect(await pending).toMatchObject({ - ok: false, - error: { detail: expect.stringContaining("cancelled") }, - }); - await closing; + try { + await awaitAcknowledgement(started, pending, "update progress write"); + const closing = updateCommands.close(); + acknowledge?.(); + acknowledge = undefined; + expect(await pending).toMatchObject({ + ok: false, + error: { detail: expect.stringContaining("cancelled") }, + }); + await closing; + } finally { + releasing = true; + acknowledge?.(); + try { + await updateCommands.close(); + } finally { + await pending; + } + } }); diff --git a/tests/support/cli/cliCommandSessionWorker.mjs b/tests/support/cli/cliCommandSessionWorker.mjs index c98d0f6ba1..dec542971c 100644 --- a/tests/support/cli/cliCommandSessionWorker.mjs +++ b/tests/support/cli/cliCommandSessionWorker.mjs @@ -1,4 +1,4 @@ -import { Writable } from "node:stream"; +import { Readable, Writable } from "node:stream"; import { access } from "node:fs/promises"; import { dirname, resolve } from "node:path"; import { fileURLToPath } from "node:url"; @@ -10,8 +10,8 @@ import { encodeCliSessionFrame, } from "./cliCommandSessionFrames.mjs"; -// One-shot command sequence matches scripts/rea.mjs. Output is captured per -// command so this process can serve the next invocation without restarting. +// Commands run through the same one-shot owner as scripts/rea.mjs. Output is +// captured per command so this process can serve the next invocation. const packageRoot = resolve( dirname(fileURLToPath(import.meta.url)), "../../..", @@ -21,11 +21,13 @@ const { default: packageJson } = await import( { with: { type: "json" } } ); +// Present the launcher's entrypoint, as a fresh `rea` process does. The +// runtime derives the current registration command from it at load time. +process.argv[1] = resolve(packageRoot, "scripts/rea.mjs"); + const runtimeFiles = [ "dist/cli-runtime/cli.js", - "dist/cli-runtime/cliOutput.js", - "dist/cli-runtime/cliBuiltinArguments.js", - "dist/cli-runtime/cliResultOutput.js", + "dist/cli-runtime/cliOneShot.js", ]; prepareCliProcessEnvironment( @@ -40,21 +42,9 @@ if (!(await compiledRuntimeExists(runtimeFiles))) { process.exit(1); } -const { closeCliResources, createCli } = - await import("../../../dist/cli-runtime/cli.js"); -const { createCliResultOutput } = - await import("../../../dist/cli-runtime/cliResultOutput.js"); -const { - cliCompletionShell, - normalizeCliArguments, - splitIncurBuiltinArguments, -} = await import("../../../dist/cli-runtime/cliBuiltinArguments.js"); -const { - cliConfigurationError, - renderCliOutputArgumentError, - renderEmptyFilteredCliOutput, - parseCliOutputArguments, -} = await import("../../../dist/cli-runtime/cliOutput.js"); +const { closeCliResources } = await import("../../../dist/cli-runtime/cli.js"); +const { runOneShotCli } = + await import("../../../dist/cli-runtime/cliOneShot.js"); const writeFrame = (value) => new Promise((resolveWrite, rejectWrite) => { @@ -172,6 +162,15 @@ async function runOneShot(args) { const originalStderrWrite = process.stderr.write.bind(process.stderr); process.stdout.write = captureWrite(stdoutChunks); process.stderr.write = captureWrite(stderrChunks); + // This process's stdin carries session frames. Give each command the empty, + // ended stdin a fresh process receives when the caller supplies no input. + const stdinDescriptor = Object.getOwnPropertyDescriptor(process, "stdin"); + const commandStdin = Readable.from([]); + Object.defineProperty(process, "stdin", { + configurable: true, + enumerable: stdinDescriptor?.enumerable ?? true, + get: () => commandStdin, + }); const destination = new Writable({ write(chunk, encoding, callback) { process.stdout.write(chunk, encoding, callback); @@ -181,60 +180,50 @@ async function runOneShot(args) { const previousExitCode = process.exitCode; process.exitCode = undefined; let explicitExit; + let exitOutput; try { - const isShellCompletion = cliCompletionShell(process.env) !== undefined; - const cliArgs = isShellCompletion ? args : normalizeCliArguments(args); - const outputOptions = parseCliOutputArguments( - isShellCompletion ? [] : cliArgs, - ); - const outputArguments = outputOptions.validation; - const configurationError = cliConfigurationError(process.env); - if ( - configurationError !== undefined && - (isShellCompletion || - splitIncurBuiltinArguments(cliArgs).command[0] !== "doctor") - ) { - process.stderr.write(`${configurationError}\n`); - process.exitCode = 1; - } else if (!outputArguments.ok) { - process.stdout.write(renderCliOutputArgumentError(outputArguments)); - process.exitCode = 1; - } else { - let wroteOutput = false; - const resultOutput = createCliResultOutput(outputOptions, destination); - try { - await createCli(process.env, resultOutput).serve(cliArgs, { - stdout: (output) => { - if (resultOutput?.handled) { - if (resultOutput.failed) process.stderr.write(output); - return; - } - if (output.length > 0) wroteOutput = true; - if (resultOutput?.captureSdkOutput(output)) return; - process.stdout.write(output); - }, - exit: (code) => { - explicitExit = code; - }, - }); - } catch (cause) { - if (wroteOutput) throw cause; - const recovered = await resultOutput?.recoverFormattingFailure(cause); - if (!recovered) throw cause; - process.exitCode = 1; - } - if (!wroteOutput && !resultOutput?.handled) { - const empty = renderEmptyFilteredCliOutput(outputOptions); - if (empty) process.stdout.write(empty); - } - await resultOutput?.finishSdkOutput( - explicitExit ?? - (typeof process.exitCode === "number" ? process.exitCode : 0), - ); - } + await runOneShotCli(args, { + environment: process.env, + runtime: { + packageName: packageJson.name, + packageVersion: packageJson.version, + versions: process.versions, + platform: process.platform, + architecture: process.arch, + }, + stdoutIsTerminal: destination.isTTY, + resultDestination: destination, + // The session frames own this process's stdout; a command's reader + // cannot close it. + closedStdoutStatus: 1, + writeStdout: (text) => { + process.stdout.write(text); + }, + writeStderr: (text) => { + process.stderr.write(text); + }, + stdoutClosed: () => false, + exitCode: () => + typeof process.exitCode === "number" ? process.exitCode : 0, + setExitCode: (code) => { + process.exitCode = code; + }, + // The session closes CLI resources once, after its last command. + ownCliResources: () => {}, + exitRequested: () => exitOutput !== undefined, + // This process serves the next command; record the status and finish + // the pending result in place of exiting. + exit: (code, finishOutput) => { + explicitExit = code; + exitOutput = finishOutput(code); + }, + }); + await exitOutput; } finally { process.stdout.write = originalStdoutWrite; process.stderr.write = originalStderrWrite; + if (stdinDescriptor !== undefined) + Object.defineProperty(process, "stdin", stdinDescriptor); } const exitCode = explicitExit ?? diff --git a/tests/support/cli/cliFixture.ts b/tests/support/cli/cliFixture.ts index db955c3ca3..de85866343 100644 --- a/tests/support/cli/cliFixture.ts +++ b/tests/support/cli/cliFixture.ts @@ -1,5 +1,11 @@ import { resolve } from "node:path"; import { tmpdir } from "node:os"; +import { + productionMcpDoctorArguments, + splitIncurBuiltinArguments, +} from "../../../src/cliBuiltinArguments.js"; +import { DEFAULT_INFO_LOGGING_COMMANDS } from "../../../src/cliCommandNames.js"; +import { parseLogLevel } from "../../../src/config/processStart.js"; import { INLINE_EVIDENCE_MCP_ENVIRONMENT } from "../../../scripts/lib/mcp-verification-environment.mjs"; import { @@ -23,6 +29,11 @@ export interface CliInvocation { readonly environment?: NodeJS.ProcessEnv; readonly input?: string | Uint8Array; readonly timeoutMs?: number; + /** + * Start a new process even when the file's shared process could serve the + * command. Use it for process-level facts such as startup or exit. + */ + readonly freshProcess?: boolean; } /** Normalized result from the compiled REA CLI. */ @@ -39,18 +50,17 @@ export type SharedMcpCall = Awaited< /** Compiled REA CLI driver owned by one test. */ export interface TestCli { + /** + * Run one command. `cliTest` drivers use the file's shared process when it + * reports the command exactly as a fresh process would, and start a fresh + * process otherwise. Drivers from `createTestCli` always start one. + */ run(invocation: CliInvocation): Promise; /** * File-scoped stdio MCP caller. Present on `cliTest` drivers and absent for * drivers created with `createTestCli` alone. */ sharedMcpCall?(): Promise; - /** - * File-scoped compiled CLI process for commands with no stdin. Present on - * `cliTest` drivers. A different environment restarts the process. Stdin and - * explicit deadlines stay on `run`, which starts a fresh process. - */ - sharedCliRun?(invocation: CliInvocation): Promise; } /** One file-scoped stdio MCP session. */ @@ -256,17 +266,45 @@ export const cliCompileCacheEnvironment = ( }); /** - * Run one command on the file-scoped compiled CLI. Stdin and explicit - * deadlines stay on `TestCli.run`, which starts a fresh process. + * Whether the file's shared process reports this command exactly as a fresh + * `rea` process would. Stdin and deadlines belong to a process; the launcher + * routes MCP and its doctor before the shared one-shot path; Node options + * change the process itself; and enabled logging, whether selected or a + * command default, writes outside the + * per-command output capture. */ -export const runFileCli = ( - cli: TestCli, - invocation: CliInvocation, -): Promise => { - const run = cli.sharedCliRun; - if (run === undefined) - throw new Error("cliTest did not provide a shared CLI session"); - return run(invocation); +const sharedProcessCanServe = ({ + arguments: arguments_, + environment, + input, + timeoutMs, + freshProcess, +}: CliInvocation): boolean => + freshProcess !== true && + input === undefined && + timeoutMs === undefined && + arguments_[0] !== "mcp" && + arguments_[0] !== "--mcp" && + productionMcpDoctorArguments([...arguments_]) === undefined && + environment?.NODE_OPTIONS === undefined && + !enablesLogging(environment?.REA_LOG_LEVEL) && + !logsByDefault(arguments_, environment?.REA_LOG_LEVEL); + +/** A command that logs at info by default unless REA_LOG_LEVEL selects otherwise. */ +const logsByDefault = ( + arguments_: readonly string[], + level: string | undefined, +): boolean => + level === undefined && + DEFAULT_INFO_LOGGING_COMMANDS.has( + splitIncurBuiltinArguments([...arguments_]).command[0] ?? "", + ); + +/** A valid level other than silent; an invalid level is a captured refusal. */ +const enablesLogging = (level: string | undefined): boolean => { + if (level === undefined) return false; + const parsed = parseLogLevel(level); + return parsed.ok && parsed.value !== "silent"; }; /** Create a compiled CLI driver backed by the supplied process registry. */ @@ -342,22 +380,17 @@ export const cliTest = processTest.extend<{ ) => { const cli = createTestCli(processes, cliCompileCache); await use({ - ...cli, + run: (invocation) => + sharedProcessCanServe(invocation) + ? cliCommandSession.run({ + arguments: invocation.arguments, + cwd: invocation.cwd ?? process.cwd(), + environment: definedCliEnvironment( + cliProcessEnvironment(invocation.environment, cliCompileCache), + ), + }) + : cli.run(invocation), sharedMcpCall: () => mcpSession.call(), - sharedCliRun: (invocation) => { - if ( - invocation.input !== undefined || - invocation.timeoutMs !== undefined - ) - return cli.run(invocation); - return cliCommandSession.run({ - arguments: invocation.arguments, - cwd: invocation.cwd ?? process.cwd(), - environment: definedCliEnvironment( - cliProcessEnvironment(invocation.environment, cliCompileCache), - ), - }); - }, }); }, }); diff --git a/tests/support/cli/javascriptApplication.ts b/tests/support/cli/javascriptApplication.ts index 7d8ac90fe7..3fc5de5f91 100644 --- a/tests/support/cli/javascriptApplication.ts +++ b/tests/support/cli/javascriptApplication.ts @@ -55,7 +55,7 @@ export const analyzeJavaScriptDirectory = async ( ) => { let document: unknown; if (surface === "CLI") { - const run = cli.sharedCliRun ?? ((invocation) => cli.run(invocation)); + const run = cli.run.bind(cli); const response = await run({ arguments: [ "analyze-javascript-application", diff --git a/tests/support/process/processFixture.ts b/tests/support/process/processFixture.ts index 496ab20ec4..7b7c8f92dc 100644 --- a/tests/support/process/processFixture.ts +++ b/tests/support/process/processFixture.ts @@ -5,9 +5,41 @@ import { type SpawnOptionsWithoutStdio, } from "node:child_process"; -import { test as base } from "vitest"; +import { TestRunner, test as base } from "vitest"; const SHUTDOWN_GRACE_MS = 1_000; +/** Minimum time kept back from a test deadline to stop a child and report it. */ +const MIN_REPORT_RESERVE_MS = 5_000; + +/** + * Default child deadline: the test's remaining deadline, less a reserve for + * stopping the child and reporting its captured output. A hung child then fails + * with its output instead of an opaque test timeout, without a fixed value that + * assumes a host's speed. Undefined when the test itself has no deadline. + */ +export const remainingProcessDeadline = ( + testDeadlineMs: number, + elapsedMs: number, +): number | undefined => { + if (!(testDeadlineMs > 0)) return undefined; + const reserve = Math.max(MIN_REPORT_RESERVE_MS, testDeadlineMs / 10); + return Math.max( + SHUTDOWN_GRACE_MS, + Math.floor(testDeadlineMs - elapsedMs - reserve), + ); +}; + +/** + * Deadline left for a child started by the running test, or undefined outside + * a test. Uses the real clock, so tests that fake `Date` keep a real deadline. + */ +const currentTestProcessDeadline = (): number | undefined => { + const test = TestRunner.getCurrentTest(); + const started = test?.result?.startTime; + if (test === undefined || started === undefined) return undefined; + const now = performance.timeOrigin + performance.now(); + return remainingProcessDeadline(test.timeout, now - started); +}; /** Captured output and termination state from one child process. */ export interface ProcessResult { @@ -70,7 +102,10 @@ const stopChild = async ( return waitForExit(child, SHUTDOWN_GRACE_MS); }; -/** Create a child-process registry for a fixture composition root. */ +/** + * Create a child-process registry for a fixture composition root. A run without + * its own `timeoutMs` is bounded by the running test's remaining deadline. + */ export const createTestProcessRegistry = ( spawnChild: ( command: string, @@ -98,7 +133,11 @@ export const createTestProcessRegistry = ( return { spawn, run: async (command, arguments_, options = {}) => { - const { input, timeoutMs, ...spawnOptions } = options; + const { + input, + timeoutMs = currentTestProcessDeadline(), + ...spawnOptions + } = options; const child = spawn(command, arguments_, spawnOptions); const stdout: Buffer[] = []; const stderr: Buffer[] = []; diff --git a/tests/support/testDeadlines.ts b/tests/support/testDeadlines.ts new file mode 100644 index 0000000000..727a505985 --- /dev/null +++ b/tests/support/testDeadlines.ts @@ -0,0 +1,14 @@ +/** + * Hang-detection deadlines that the Vitest project policy applies. Tests do not + * restate them; see docs/development/testing.md#developer-commands. + */ +export const TEST_DEADLINE_MS = { source: 30_000, built: 120_000 } as const; + +/** Deadline for a bounded workload tagged `long-running`. */ +export const LONG_RUNNING_DEADLINE_MS = 300_000; + +/** Coverage instrumentation slows every lane, so it scales every deadline. */ +export const COVERAGE_DEADLINE_SCALE = 2; + +/** Hosted CI runs a worker per core, so it also scales every deadline. */ +export const HOSTED_RUNNER_DEADLINE_SCALE = 2; diff --git a/vitest.config.ts b/vitest.config.ts index 2da24fbea8..116807352a 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -10,6 +10,12 @@ import { } from "./scripts/lib/test-projects.mjs"; import { CiTestSequencer } from "./tests/ciTestSequencer.js"; +import { + COVERAGE_DEADLINE_SCALE, + HOSTED_RUNNER_DEADLINE_SCALE, + LONG_RUNNING_DEADLINE_MS, + TEST_DEADLINE_MS as BASE_TEST_DEADLINE_MS, +} from "./tests/support/testDeadlines.js"; // Playwright's registry does not derive a cache directory for Termux's // `android` platform. Match the production CLI fallback before workers import @@ -32,8 +38,27 @@ const { options: testOptions } = parseCLI( ); const COVERAGE_ENABLED = testOptions.coverage?.enabled === true; const COVERAGE_SHARD = testOptions.shard !== undefined; -// Keep local and CI concurrency bounded by the runner rather than command locks. -const MAX_TEST_WORKERS = Math.min(2, availableParallelism()); +// Bound concurrency by the runner rather than command locks. A hosted CI job +// owns its whole four-core runner, so it may use every core; other hosts stay +// at two workers because developers and agents share them with other checks. +const DEDICATED_CI_RUNNER = process.env.GITHUB_ACTIONS === "true"; +const MAX_TEST_WORKERS = Math.min( + DEDICATED_CI_RUNNER ? 4 : 2, + availableParallelism(), +); +// A test deadline only turns a hang into a named failure; it is not a +// performance assertion. Projects own it so tests do not hard-code host speed. +// Built projects launch compiled CLI, MCP, and provider processes while other +// lanes share the runner, and coverage instrumentation slows every lane. Hosted +// CI runs a worker per core, so CPU-bound cases there share every core with +// other workers and their subprocesses. +const DEADLINE_SCALE = + (COVERAGE_ENABLED ? COVERAGE_DEADLINE_SCALE : 1) * + (DEDICATED_CI_RUNNER ? HOSTED_RUNNER_DEADLINE_SCALE : 1); +const TEST_DEADLINE_MS = { + source: BASE_TEST_DEADLINE_MS.source * DEADLINE_SCALE, + built: BASE_TEST_DEADLINE_MS.built * DEADLINE_SCALE, +} as const; const TEST_PROJECTS = [...SOURCE_TEST_PROJECTS, ...BUILT_TEST_PROJECTS]; const PROJECT_POLICIES = { @@ -126,6 +151,8 @@ const projects = TEST_PROJECTS.map((project) => { include: [...project.include], ...(project.exclude ? { exclude: [...project.exclude] } : {}), isolate: project.isolate !== false, + testTimeout: TEST_DEADLINE_MS[project.group], + hookTimeout: TEST_DEADLINE_MS[project.group], ...policy, }, }; @@ -142,10 +169,18 @@ export default defineConfig({ retry: 0, reporters: ["default"], runner: "./tests/processOwnershipRunner.ts", - // Boundary projects may compete with TypeScript, docs, and package checks - // under Turbo. Keep the deadline bounded while avoiding false failures from - // host-level CPU and filesystem contention. - testTimeout: COVERAGE_ENABLED ? 60_000 : 30_000, + testTimeout: TEST_DEADLINE_MS.source, + hookTimeout: TEST_DEADLINE_MS.source, + // Deadline probes compare what Vitest applies with the configured policy. + provide: { testDeadlineScale: DEADLINE_SCALE }, + tags: [ + { + name: "long-running", + description: + "A bounded workload that needs more than its project's deadline", + timeout: LONG_RUNNING_DEADLINE_MS * DEADLINE_SCALE, + }, + ], coverage: { enabled: false, provider: "v8", @@ -178,3 +213,9 @@ export default defineConfig({ }, }, }); + +declare module "vitest" { + export interface ProvidedContext { + readonly testDeadlineScale: number; + } +}