Skip to content

feat(ghidra): persistent project cache with cross-session identity - #3531

Open
goncalompontes wants to merge 98 commits into
morluto:mainfrom
goncalompontes:feat/ghidra-project-cache
Open

goncalompontes wants to merge 98 commits into
morluto:mainfrom
goncalompontes:feat/ghidra-project-cache

Conversation

@goncalompontes

@goncalompontes goncalompontes commented Oct 14, 2026 •

Copy link
Copy Markdown

Adds a managed, persistent Ghidra project cache plus an open-mode-independent analysis identity so prior analysis snapshots replay across sessions.

Related issue: #1048. The pre-imported project selection that issue describes is implemented by #2358; this PR adds the persistent cache layer on top.

NOTE: this branch is stacked on #2358 (feat/1048-existing-project-wiring), so until #2358 merges this PR's diff also contains its commits (existing-project selection, launcher wiring, and the related profile/config changes). A fork-only stacked copy exists at goncalompontes#1.

Problem

Large Ghidra imports re-pay import and auto-analysis on every open_binary. A saved project was not reusable across sessions, and a cache miss and a later cache hit committed different analysis profiles, so retained analysis snapshots could not replay.

What

  • Config: REA_GHIDRA_PROJECT_CACHE=off|read|on (default off), _DIR, _MAX_BYTES, _MAX_AGE_DAYS.
  • Store: key over target + provider + JVM + platform + import-profile; canonical manifest; expanded-project admission; streaming digests; atomic publish under a per-key lock; byte/age-capped prune; integrity verification.
  • Session: a miss imports writable and publishes only after a confirmed shutdown and a natural process exit; a hit opens -process -noanalysis -readOnly.
  • Identity: the committed profile drops import-mode provenance and adds a stable project_cache commitment, so miss and hit share a profile digest.
  • CLI: rea cache-list|cache-verify|cache-prune|cache-remove.
  • Durability: a hit leases its entry through the runtime copy; pruning and removal respect that lease.
  • Security: the cache root is owner-only before project bytes are written. POSIX creates 0700 and tightens an existing widened root; Windows creates the root through the native protected-DACL boundary, and an existing root that does not pass that verification is rebuilt as a private directory that keeps the stored projects.
  • Diagnostics: a corrupt stored entry that is discarded is logged and reported in the session's Evidence limitations before the fresh import rebuilds it. A rebuilt cache root is logged and reported in the session's Evidence limitations in the same way.

Measured (Windows Ghidra P0)

engine2.dll: first open 256.8 s to cache hit 13.1 s.

Verification

Local Windows checkout at d4b8afbba:

  • npx tsc --noEmit ┬┤ÔöÉ┬ó+┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó+┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó+┬┤ÔöÉ┬ó pass.
  • npm run format:check ┬┤ÔöÉ┬ó+┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó+┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó+┬┤ÔöÉ┬ó pass.
  • npm run lint ┬┤ÔöÉ┬ó+┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó+┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó+┬┤ÔöÉ┬ó 0 errors (repository-wide warnings only).
  • npm run metadata:check, npm run jscpd, npm run scan:todos, npm run verify:architecture-guards ┬┤ÔöÉ┬ó+┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó+┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó+┬┤ÔöÉ┬ó pass.
  • npm run test:local for the cache store, cache-root, key, manifest, provider behavior, config, and CLI logging suites ┬┤ÔöÉ┬ó+┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó+┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó+┬┤ÔöÉ┬ó pass.
  • npm run test:focused -- tests/boundary/cli/jsonOutput.test.ts tests/acceptance/cacheVerifyCli.test.ts ┬┤ÔöÉ┬ó+┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó+┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó+┬┤ÔöÉ┬ó 44 passed.
  • npm run test:fast ┬┤ÔöÉ┬ó+┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó+┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó+┬┤ÔöÉ┬ó 10,530 passed; the 63 failures are pre-existing POSIX/macOS-only suites on this Windows host (ADB paths, Hopper, WABT, Mach-O/Electron). No Ghidra or cache suite failed.
  • A real Windows probe created a durable cache root through the native DACL boundary and verified it.
  • A real Windows run rebuilt a cache root whose DACL had been reset to an inherited one, preserved the stored project, reused it in the same session, and reported the rebuild in the session Evidence.

The Linux-only ghidraClient boundary file could not run on this Windows host because its fixture rename hits EBUSY; Linux CI covers it. npm run knip reports the pre-existing unresolved #web-bundle-analysis-worker import.

Merge refresh (bf31ce2f9)

Merged upstream/main (fa3665725) into the branch. The only conflict was src/ghidra/GhidraSnapshotCache.test.ts: the branch's Windows describe.skipIf wrapper and main's new degraded-Evidence replay test were combined, so the existing relationship-coverage replay case and main's new case run in the same suite.

Validation on Linux, merged head:

  • npx tsc --noEmit ┬┤ÔöÉ┬ó+┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó+┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó+┬┤ÔöÉ┬ó pass.
  • npx vitest run src/ghidra ┬┤ÔöÉ┬ó+┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó+┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó+┬┤ÔöÉ┬ó 820 passed, 1 skipped; one GhidraProjectCacheRoot lane failed on the first run and passed on rerun (the flake already documented below).
  • npx vitest run src/ghidra/projectCache src/ghidra/GhidraProjectCache.behavior.test.ts src/ghidra/GhidraProvider.behavior.test.ts ┬┤ÔöÉ┬ó+┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó+┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó+┬┤ÔöÉ┬ó 210 passed.
  • npx vitest run src/ghidra/GhidraSnapshotCache.test.ts ┬┤ÔöÉ┬ó+┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó+┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó+┬┤ÔöÉ┬ó 17 passed.
  • npx oxfmt --check src/ghidra/GhidraSnapshotCache.test.ts ┬┤ÔöÉ┬ó+┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó+┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó+┬┤ÔöÉ┬ó pass; npx oxlint on the file ┬┤ÔöÉ┬ó+┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó+┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó+┬┤ÔöÉ┬ó no findings.
  • npm run build:cached ┬┤ÔöÉ┬ó+┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó+┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó+┬┤ÔöÉ┬ó pass.

User-visible CLI output

Linux, merged head, empty cache directory:

$ REA_GHIDRA_PROJECT_CACHE=on REA_GHIDRA_PROJECT_CACHE_DIR=/tmp/opencode/rea-cache-sample node scripts/rea.mjs cache-list --format json
{"cache_directory":"/tmp/opencode/rea-cache-sample","mode":"on","max_bytes":10737418240,"entry_count":0,"total_bytes":0,"entries":[]}

Security, privacy, process, containment

The store remains local and owner-only. Entries are derived data and are never executed. Publishing is gated on a confirmed shutdown, a natural process exit, completed non-timed-out analysis, and no client mutation. Interrupted or forced stops never publish. Admission rejects links/reparse points and malformed expanded projects.

A rebuilt Windows root copies the stored projects into the private replacement instead of moving them, so the preserved projects carry the replacement's owner-only DACL rather than the permissions the shared root gave them. The rebuild sets the previous root aside first and removes it only after every project is copied, so a rebuild that cannot complete leaves the stored projects where they were.

Author readiness

Human owner: @goncalompontes

  • I reviewed the final diff and relevant existing code, understand the change and its edge cases, and take responsibility for the complete PR, including AI-assisted work.
  • I checked the evidence above, confirmed it supports the current change, and recorded meaningful validation gaps.
  • I declare this PR ready for initial maintainer review.

Review follow-up

37efe1c2d answers the review finding that the Linux run had no passing test for "a changed session does not publish".

  • The fake bridge advertised the database mutations in its handshake but never answered them, so the publication test waited for a reply that never arrived and hit the 30 s test deadline. The fake bridge now answers the mutating operations the way the real bridge does: a Unix socket applies the mutation and returns the refreshed observation, and a read-only transport refuses it with method_unavailable. The publication test covers every mutating operation and asserts the bridge answered it.
  • The Windows cache-root admission decisions are now pinned portably: a root that is not a directory, a root a concurrent initializer created, a private leaf that could not become the root, and a root native verification does not confirm.
  • Validation on this Windows host: npx tsc --noEmit; npx vitest run src/ghidra/projectCache src/ghidra/GhidraProjectCache.behavior.test.ts src/ghidra/GhidraProvider.behavior.test.ts (186 passed); the fake bridge mutation surface probed over a loopback transport (applied on a writable transport, refused on a read-only one); the client boundary exercised end to end against the same fixture.

Not re-run here: the Linux-only ghidraClient socket tests still cannot run on this host: the fixture cannot publish a Windows endpoint without the native runtime, and AF_UNIX listen is denied here.

Windows cache root follow-up

d4b8afbba fixes two defects that kept the cache unusable on a real Windows host, both found by running the feature end to end there.

  • A cache root that native verification refused was rejected for the whole session, and the refusal had no supported remedy. The default root inherits its DACL from the user profile, so on an administrator account it is owned by BUILTIN\Administrators and always fails the owner-only check; the store then re-imported the target every session. Such a root is now rebuilt as a private directory: the previous root is set aside first, the stored children move into the replacement, and the replacement is verified before the previous root is removed. A rebuild that cannot complete restores the previous root and leaves the stored projects where they were.
  • The durable root was the native runtime root itself. The runtime root stays sealed by the handle that owns it, and Windows refuses a path-based rename into a sealed directory, so a sealed cache root refused the stored projects moved into it and the entries the store published into it. The durable root is now created as a child of the runtime root: the child carries the same protected, owner-only DACL and is not sealed, and the emptied runtime root is closed so the native handles are released deterministically.

The rebuild is reported through the store, the provider log, and the session Evidence, so a rebuilt root is stated rather than applied silently.

Validation on this Windows host (Ghidra 12.1.4, JDK 21):

  • npx tsc --noEmit; npx oxfmt --check; npx oxlint (0 errors, the repository-wide warnings only); npm run verify:module-boundaries.
  • npx vitest run src/ghidra ┬┤ÔöÉ┬ó+┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó+┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó+┬┤ÔöÉ┬ó 769 passed, 28 skipped.
  • npx vitest run src/ghidra/projectCache ┬┤ÔöÉ┬ó+┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó+┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó+┬┤ÔöÉ┬ó 51 passed, 3 skipped.
  • npm run test:fast ┬┤ÔöÉ┬ó+┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó+┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó+┬┤ÔöÉ┬ó 10,530 passed; the same 63 pre-existing POSIX/macOS-only failures (ADB paths, Hopper, WABT, Mach-O/Electron) and no Ghidra or cache failure.
  • A real run against a cache root whose DACL had been reset to an inherited one: the root was rebuilt, the stored project survived the rebuild, the same session reused it from the cache, and the session Evidence reported "The Ghidra project cache root was not owner-only and was rebuilt as a private directory; 4 stored item(s) were preserved."
  • The rebuilt root reads back as owned by the current user with a protected DACL that admits only that user and SYSTEM.

Not re-run here: the native artifact build lane. It requires Linux and MinGW-w64, so the native sources are unchanged by this commit; the change uses only operations the shipped artifact already provides (runtime_create, runtime_mkdir, runtime_close, cutter_bridge_verify_directory), each exercised against the real artifact on this host.

Windows cache root repair follow-up

26d6b1c54 answers the two review findings on the Windows root rebuild.

  • The rebuild moved the stored children into the private replacement, so a preserved project kept the permissions it was created with and stayed readable by the principals its DACL admits. Windows accounts hold SeChangeNotifyPrivilege, which bypasses the traverse check on the private parent, and a moved staging directory gave every project written into it the same shared permissions. The rebuild now copies the stored projects into the private replacement, so each copy is created inside it and inherits its owner-only DACL. A link is refused instead of carried into the private root, where it would keep naming the shared directory it points at.
  • A failed rebuild erased projects: the restoration move could fail partway and the replacement was then deleted with the projects it still held. The replacement now holds only copies, so it is removed without losing a stored project, and the set-aside root is put back with everything it held. When the set-aside root cannot be put back, both roots stay in place and the message names the one that holds the projects. When it cannot be removed after a successful rebuild, its path is reported, because it still holds a copy other principals can read.

Validation on this Windows host:

  • npx tsc --noEmit; npx oxfmt --check; npx oxlint (0 errors, the repository-wide warnings only); node scripts/check-doc-facts.mjs; npm run verify:module-boundaries.
  • npx vitest run src/ghidra ┬┤ÔöÉ┬ó+┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó+┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó+┬┤ÔöÉ┬ó 772 passed, 30 skipped.
  • npx vitest run tests/acceptance/cacheVerifyCli.test.ts tests/acceptance/cachePruneCli.test.ts ┬┤ÔöÉ┬ó+┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó+┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó+┬┤ÔöÉ┬ó 4 passed.
  • The new Windows cache root repair failures lanes cover a preserved project that cannot be copied, a replacement that cannot be removed, and a set-aside root that cannot be removed.

Not re-run here: the two POSIX-only cleanup lanes (a set-aside root that cannot be removed, and a replacement that cannot be removed) did not run on this Windows host; their expected outcomes were confirmed by forcing the removal to fail. The native artifact build lane was not re-run either: it requires Linux and MinGW-w64, and the native sources are unchanged by this commit.

Windows cache root publication follow-up

6b9c2e5b7 answers the review finding that a rebuilt root could publish unfinished projects.

  • The rebuild copied the preserved projects straight into their final paths, so a rebuild that stopped part-way left a half-copied project where a later session looks for it. The lookup admits an entry that is readable rather than complete, so the next session accepted the private root as ready and served that project as a cache hit, and another session could read or discard it while the rebuild was still running. Each preserved project is now copied into a staging directory inside the private root and published by an atomic rename, so a project is never visible at its final path while it is still being copied. A name a concurrent publisher already took keeps the published project and discards the copy, so a rebuild never replaces a project a live session stored.
  • One session rebuilds the root at a time. The rebuild holds a lock beside the root, and a session that finds the lock held reports the root as busy instead of changing a root another session is rebuilding. A lock left behind by a session that died is taken over once it has been silent for longer than any plausible rebuild, and the lock is kept alive while the projects are copied.

The new regression test fails against the previous implementation: it observed a published project holding 199 of its 200 files.

Validation on this Windows host:

  • npx tsc --noEmit; npx oxfmt --check; npx oxlint (0 errors, the repository-wide warnings only); node scripts/check-doc-facts.mjs; npm run verify:module-boundaries.
  • npx vitest run src/ghidra/projectCache src/ghidra/GhidraProjectCache.behavior.test.ts src/ghidra/GhidraProvider.behavior.test.ts ┬┤ÔöÉ┬ó+┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó+┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó+┬┤ÔöÉ┬ó 197 passed, 5 skipped.
  • npx vitest run src/ghidra ┬┤ÔöÉ┬ó+┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó+┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó+┬┤ÔöÉ┬ó 776 passed, 30 skipped.
  • npx vitest run tests/acceptance/cacheVerifyCli.test.ts tests/acceptance/cachePruneCli.test.ts ┬┤ÔöÉ┬ó+┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó+┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó+┬┤ÔöÉ┬ó 4 passed.
  • The new Windows cache root repair coordination and Windows cache root repair locking lanes cover a project that is published only once it is copied, a project a concurrent publisher already stored, a root another session is rebuilding, a lock a dead session left behind, and the lock released when a rebuild cannot complete.

Not re-run here: the two POSIX-only cleanup lanes (a set-aside root that cannot be removed, and a replacement that cannot be removed) did not run on this Windows host; their expected outcomes were confirmed by forcing the removal to fail. The native artifact build lane was not re-run either: it requires Linux and MinGW-w64, and the native sources are unchanged by this commit.

Windows cache root repair reclamation follow-up

7f0c75843 answers the review findings on the reclamation of the copies an interrupted rebuild leaves behind.

  • The reclamation deleted any directory named <root>.repair-staging beside the cache root, even when REA never created it. The cache root is a path the caller chose, so a sibling of it can hold anything, and an ordinary open or cache-prune deleted its contents. The copies now live in a staging directory inside the cache root, which is the one directory REA owns, so the reclamation only removes state REA created. A stored entry is named by its digest, so the store never counts the copies, and a rebuild does not carry over the copies a previous interrupted rebuild left behind, which were never published either.
  • The reclamation checked the repair lock but never held it, so a rebuild that started while the reclamation measured the copies could have its live copies deleted. The reclamation now takes that lock before it checks, measures, and removes the copies: a rebuild that is running keeps the lock, so the reclamation steps aside, and a lock a dead session left behind is taken over, so the copies it left are reclaimed with it. The lock itself stays beside the root, because the rebuild replaces the root and a lock inside it would not survive the replacement.
  • The POSIX repair-failure lane created its read-only directory in every private directory the fake host made, so the repair could not remove the staging directory it created and the lane's cleanup failed with a permission error. Only the replacement root holds the read-only directory now.

The new regression tests fail against the previous implementation: the unrelated sibling directory was deleted, and the reclamation held no lock while it removed the copies.

Validation on this Windows host:

  • npx tsc --noEmit; npx oxfmt --check and npx oxlint on the changed files; node scripts/check-doc-facts.mjs; npm run verify:module-boundaries; npm run verify:architecture-guards; npm run scan:todos; npm run knip.
  • npx vitest run src/ghidra/projectCache ┬┤ÔöÉ┬ó+┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó+┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó+┬┤ÔöÉ┬ó 65 passed, 5 skipped.
  • npx vitest run src/ghidra ┬┤ÔöÉ┬ó+┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó+┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó+┬┤ÔöÉ┬ó 784 passed, 30 skipped.
  • npx vitest run tests/acceptance/cacheVerifyCli.test.ts tests/acceptance/cachePruneCli.test.ts ┬┤ÔöÉ┬ó+┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó+┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó+┬┤ÔöÉ┬ó 4 passed.
  • The real native boundary on this host: a root that native verification refuses was rebuilt, the rebuild created the staging directory for the preserved copies inside the private replacement through the native boundary, the stored project survived the rebuild, and the reclamation removed the copies of an interrupted rebuild from inside the root and reported the bytes they held. Nothing was left beside the root.

Not re-run here: the two POSIX-only cleanup lanes (a set-aside root that cannot be removed, and a replacement that cannot be removed) did not run on this Windows host; their expected outcomes were confirmed by forcing the removal to fail. The native artifact build lane was not re-run either: it requires Linux and MinGW-w64, and the native sources are unchanged by this commit.

Pre-existing on this host, not from this commit: the publishes a preserved project only after it is copied lane is flaky at roughly one run in thirty. A background scanner can hold the temporary cache root open, so the rename that sets the root aside fails with EPERM, and the lane then reports either a rejected rebuild or an empty publication. The same failure reproduces on the previous commit.

Before merge

  • This PR addresses a concrete problem or an agreed enhancement.
  • Material scope and authority questions are resolved, and any required maintainer scope decision covers the final diff. No new target-execution or mutation authority is added: the cache stores derived Ghidra project data locally and never executes it.
  • The PR has one focused outcome and the title follows type(scope): outcome.
  • Related issue is linked, or the reason for not linking one is stated above. References [Feature] Open a pre-imported Ghidra project so import and auto-analysis can happen outside the startup deadline #1048; feat(ghidra): select a pre-imported project with -process #2358 implements the issue's pre-imported project selection, and this PR is the cache layer stacked on it.
  • Tests cover changed observable behavior and meaningful failure paths.
  • Owning docs, contracts, and generated metadata are updated where needed. docs/ghidra-project-cache.md, the installation guide, and the configuration reference are updated.
  • User-visible CLI/MCP changes include representative output. The output above is from the merged head; MCP tool contracts are unchanged.
  • I checked the final diff for secrets, unrelated cleanup, and unsupported claims. A pattern scan over the 107-file diff found only secret-shaped code identifiers and deliberate test fixtures; no credential was found.

Cache root ownership follow-up

d208cf012 answers the three review findings on the cache root, the repair
lock, and the store's working directories.

  • The store's staging, lock, and lease directories are never followed. A root
    that other principals could write could hold a link at the name of one of
    those directories, and the prune listed the link's target and removed what
    was older than an hour in it. The store removes the link and creates its
    own directory in its place, so a cleanup never reaches outside the cache
    root. The copy of a published project is made inside that directory too, so
    it cannot land in what the link pointed at.
  • The repair lock carries the token of the session that holds it. A lock that
    is taken over is moved aside and compared with the lock that was observed,
    so a takeover never removes the lock of a session that holds it, a session
    releases only the lock it holds, and a repair that lost its lock stops
    instead of changing a root another session is rebuilding. The lock protocol
    moves to src/ghidra/projectCache/GhidraProjectCacheLock.ts, because it
    owns the lock file.
  • A repair that cannot complete keeps the stored projects another session
    published while it ran. Those entries are moved into the set-aside root
    before the replacement is removed, so the rollback no longer discards a
    project another session stored.

The new regression tests fail against the previous implementation: the
published entry was discarded by the rollback, the repair removed the lock of
the session that had taken it over, two sessions could take over the same
abandoned lock, and a file at the name of a working directory was not
replaced.

Validation on this Windows host:

  • npx tsc --noEmit ┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó pass.
  • npx oxfmt --check and npx oxlint on the changed files ┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó pass (0 errors,
    the repository-wide warnings only).
  • npx vitest run src/ghidra ┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó 794 passed, 34 skipped.
  • npx vitest run src/ghidra/projectCache ┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó 69 passed, 7 skipped.
  • npx vitest run tests/acceptance/cacheVerifyCli.test.ts ┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó 3 passed.
  • npx vitest run tests/acceptance/cachePruneCli.test.ts ┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó 1 passed.
  • npm run verify:module-boundaries; npm run verify:architecture-guards;
    npm run knip; npm run jscpd; npm run scan:todos;
    node scripts/check-doc-facts.mjs.

Cache root working directory and POSIX follow-up (06ef06c61, fd4428601, 1e0854488)

Answers the two follow-up review findings and runs the POSIX lanes the Windows
host could not run.

  • The store replaces a working directory by moving the entry it observed aside
    and comparing that entry's inode before removing it. A directory another
    session created after the observation is put back, or left beside the root
    when the name was taken again, so a repair never recursively removes a live
    staging copy or a publisher's lock.
  • The non-root POSIX repair-failure lane now makes the replacement read-only,
    which blocks the move the rollback performs instead of relying on removing a
    read-only child. Cleanup runs in a finally block, so a failed assertion no
    longer leaves a read-only tree behind.
  • The repair-lock takeover lane waits for the lock's owner token before handing
    the lock over; the file exists before its token is written, so the old wait
    could overwrite the repair's token and let the repair run to completion. The
    published-project lane pins its observation to one root inode, so the
    repair's quarantine of the root no longer looks like a zero-file
    publication.

Validation on Linux, head 1e0854488 (the two POSIX-only lanes now run):

  • npx tsc --noEmit ┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó pass.
  • npx vitest run src/ghidra/projectCache ┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó 76 passed, no skips;
    GhidraProjectCacheRoot.test.ts passed 10 consecutive runs.
  • npx vitest run src/ghidra ┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó 827 passed, 1 skipped.
  • npx vitest run tests/boundary/mcp/sessionMcp.test.ts tests/boundary/mcp/optionalProviderIsolation.test.ts tests/composition/directAnalysisFactories.test.ts tests/composition/directAnalysisXrefs.test.ts ┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó 46 passed.
  • npx vitest run tests/acceptance/cacheVerifyCli.test.ts tests/acceptance/cachePruneCli.test.ts ┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó 4 passed.
  • npx oxfmt --check and npx oxlint on src/ghidra/projectCache ┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó pass
    (0 errors, the repository-wide warnings only).
  • npm run verify:module-boundaries ┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó pass.
  • npm run build:cached ┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó pass; the acceptance lanes above ran against the
    build.

The merge at 1e0854488 also resolves the conflicts with main:
src/application/DirectAnalysis.ts keeps main's native dispatch profile
binding, and src/server/registerSessionStatusTool.ts keeps main's
probeSessionAvailability with this branch's target_id and wait_ms status
arguments.

The two POSIX-only lanes are now covered on Linux, so the earlier "not re-run
here" note for them no longer applies.

Lease and staging follow-up (a90a17584, aee75d863, 89e9685f5)

Answers the two cache findings from the latest automated review pass.

  • A lease is written beside its final name and renamed into place. The rename
    replaces the name atomically and never follows a link at the destination, so
    a concurrent remove or prune never sees the lease absent while a hit is
    using it, and a link planted at leases/<key>.lease before the root was
    tightened is replaced instead of followed. Regression: a hit with a linked
    lease leaves the link target unchanged and the lease a regular file.
  • Each client receives its own staging path even when two clients share a
    profile digest, so a failed publish in one client cannot discard another
    client's copy. Regression: two clients for one shared profile receive
    distinct staging slots.

Validation on Linux, head 89e9685f5:

  • npx tsc --noEmit ┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó pass.
  • npx vitest run src/ghidra ┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó 829 passed, 1 skipped.
  • npx vitest run src/ghidra/projectCache ┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó 77 passed.
  • npx vitest run src/ghidra/projectCache/GhidraProjectStore.test.ts src/ghidra/GhidraProvider.behavior.test.ts ┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó 172 passed.
  • npx oxfmt --check and npx oxlint on the changed files ┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó┬┤ÔöÉ┬ó pass (0 errors,
    the repository-wide warnings only).

The latest automated review also raised findings in the multi-target session
and background-analysis work that this branch now carries
(src/application/binary/BinaryTargetRegistry.ts,
src/application/binary/BinarySession.ts, and
src/server/registerSessionTools.ts). Those are outside the cache change and
are left to that work's owners.

Multi-target session follow-up (4ec21a489)

Answers the two review findings on the multi-target session work that this
branch carries (src/application/binary/BinaryTargetRegistry.ts and
src/application/binary/BinarySession.ts).

  • A forced close that could not release the provider keeps the start permit
    that the retained cleanup owns, and the cleanup is retried inside the open.
    With REA_MAX_CONCURRENT_ANALYSES=1 the open therefore waited for a permit
    that only the open itself could give back, so a replacement open hung until
    another request retried the cleanup explicitly. The registry now retries the
    retained cleanup before it takes a new start permit; a successful retry
    gives the permit back and stops reporting the slot as retained. One retry
    runs at a time, so two overlapping opens do not close the same provider
    twice.
  • Every ordinary request raced the shared retirement promise of its epoch.
    Promise.race leaves its reaction attached to that promise, so every
    request that settled through the transition left a retained reaction behind
    until the epoch was retired, which a long-lived session never does. The
    retirement is now a removable listener: a request detaches itself when it
    settles or is aborted, and the epoch keeps nothing for it.
  • The client a cleanup closed is marked as released where the close happens,
    so a provider that a retry released is not closed a second time when the
    start that never unwound finally unwinds.

The new regression tests fail against the previous implementation: the
replacement open never started a second provider, the epoch kept a waiter for
a request that had already settled, and the released provider was closed
twice.

Validation on this Windows host:

  • npx tsc --noEmit ├ö├ç├ pass.
  • npx vitest run src/application/binary ├ö├ç├ 197 passed.
  • npx vitest run --project mcp-boundary ├ö├ç├ 967 passed; the 16 failures are
    the pre-existing POSIX-only failures this host already had without the
    change.
  • npm run test:fast ├ö├ç├ 12,210 passed; the 70 failures are the pre-existing
    POSIX/macOS-only suites on this host (Hopper, ADB, WABT, Mach-O, Electron),
    and no binary or session suite failed.
  • npx oxfmt --check and npx oxlint on the changed files ├ö├ç├ pass (0 errors,
    the repository-wide warnings only).
  • npm run verify:module-boundaries; npm run verify:architecture-guards;
    npm run knip; npm run scan:todos ├ö├ç├ pass.

Not re-run here: the POSIX-only cleanup lanes of the cache suites and the
native artifact build lane; they are unchanged by this commit.

Cancelled and closed-slot follow-up (794e3050d)

Answers the two review findings on the failed-start cleanup that the last
commit introduced.

  • An open that was cancelled while it waited for a start permit skipped the
    cleanup of its failed start. The admission reported the failure it could
    not acquire the permit for without running that cleanup, and the open
    assumed the cleanup had already run: the client the open created was never
    closed, and the target the open had replaced was closed and never
    restored.

    The session owns the cleanup of its failed start now. It runs once: the
    admission runs it under the shared start limit, and a failure the
    admission reported without running it runs it in the open. The admission
    runs the cleanup for a start it could not attempt as well, so the limit
    still counts a provider the cleanup could not release, and the slot that
    owns that provider stays registered as retained.

    The health probe of a client that starts the provider it selected runs
    inside the admitted start now, so a probe that fails runs the cleanup of
    the failed open under the limit that counts the provider the probe left
    behind. The probe of a composed client answers without touching a child,
    so the limit is not held for a provider start that never ran.

  • The cleanup of a failed start restored the target the open replaced before
    the open checked whether it had been retired. A forced close that
    released the start while it was in flight therefore left the closed slot
    holding a target again, and a target that opened in that slot afterwards
    could have its binding overwritten.

    The cleanup restores the target only while the slot still owns it. The
    lifecycle generation of the open is checked before the restoration and
    again after it waited for the restored client, so a slot that was closed
    while the restoration waited releases the restored client instead of
    binding it.

The new regression tests fail against the previous implementation: the client
of a cancelled open was never closed, the target it replaced was never
restored, and a closed slot held the target it had before the failed open.

Validation on this Windows host:

  • npx tsc --noEmit - pass.
  • npx vitest run src/application/binary src/server tests/composition/analysis-sessions - 434 passed.
  • npx vitest run tests/boundary/mcp - the 16 failures are the pre-existing POSIX-only failures this host already had without the change.
  • npx oxfmt --check and npx oxlint on the changed files - pass (0 errors, the repository-wide warnings only).
  • npm run check:fast; npm run verify:module-boundaries; npm run verify:architecture-guards; npm run scan:todos; npm run knip; node scripts/check-doc-facts.mjs - pass.

Restored start limit follow-up (1cd961008)

Answers the review finding that a restored target starts its provider outside
the shared start limit.

  • A cancelled replacement waits for a start permit, and the admission runs the
    cleanup of that failed open, which restores the target the open replaced. The
    restoration created its client without the start admission, so the provider
    start that client defers ran outside the shared limit: a query on the
    restored target launched an import beside a provider that was still
    starting, and the analysis cap that bounds the concurrent analyses was
    exceeded.

    The session keeps the admission the open carries and hands it to the restored
    client, so the restored target starts its provider under the same limit as
    every other start of the session. The restoration does not start the provider
    under that admission: the health probe of a composed client answers without
    touching a child, and a probe that waited for the limit would make a
    cancelled open wait for a permit it cannot use.

The new regression test fails against the previous implementation: the query on
the restored target did not run under the admission.

Validation on this Windows host:

  • npx tsc --noEmit - pass.
  • npx vitest run src/application/binary - 230 passed.
  • npx vitest run src/application/binary src/server tests/composition/analysis-sessions - 435 passed.
  • npx vitest run src/ghidra - 801 passed, 35 skipped.
  • npx vitest run --project mcp-boundary - the failing suites are the POSIX-only and the WABT/IDA suites this Windows host cannot run; the same suites fail without the change.
  • npx oxfmt --check and npx oxlint on the changed files - pass (0 errors, the repository-wide warnings only).
  • npm run check:fast; npm run verify:module-boundaries; npm run verify:architecture-guards; npm run scan:todos; npm run knip; node scripts/check-doc-facts.mjs - pass.

akushonkamen and others added 28 commits October 12, 2026 21:35
…-importing

Every session paid a fresh -import plus auto-analysis into a per-session
project that -deleteProject removed, so large targets re-paid the
cold-import tax on every open and could not fit the startup deadline
(morluto#1048).

Add an opt-in existing-project launch path following the first-slice
spec from the issue. ghidraHeadlessArguments gains an existing-project
branch that selects the project location, project name and program
explicitly with -process <program> -noanalysis, keeps -readOnly and
drops -import/-deleteProject. Import-only selections (DOS, raw binary,
language override, PSP, analysis seeds, analyzer overrides, MIPS
soft-float) are rejected with their reasons, and Windows keeps its
current P0 launch path unchanged. GhidraHeadlessLauncher copies the
caller's project into the owned runtime project root before launching
(copy-first), so Ghidra never touches the original project files and a
missing project directory fails before any process starts.

Wiring the profile/CLI selection surface is left for a follow-up slice.
The headless invocation selects the fixed project name rea-project while
copyExistingGhidraProject copied caller entries verbatim, so a directory
holding any other project file was copied and spawned successfully only
to fail inside Ghidra with an opaque error. Reject that layout before any
copy or process start with its actual reason, and state the fixed-name
contract on GhidraExistingProject and the copy helper.

Also open morluto#2147 to track the product wiring, target-digest binding with
stale/mismatch rejection, provenance/mutation documentation, and the
real-provider verification lane this fixture-only slice has not run
(morluto#1048).
Add REA_GHIDRA_EXISTING_PROJECT and REA_GHIDRA_EXISTING_PROGRAM so a caller can
open a project produced outside REA with '-process -noanalysis' instead of
re-importing and re-analyzing the target inside the provider startup deadline.

Both settings are required together and validated against import-time options
(language, compiler spec, seeds, disabled analyzers) that only apply to a fresh
import. The pair is threaded through AppConfig to the launcher, and rejected for
the Windows launch path and DOS, raw-binary, and PSP targets. The analysis
profile records import_mode: existing-project-copy; the bridge's executable
SHA-256 check already rejects a stale or mismatched project.

Stacks on morluto#2150, which adds the launcher existingProject branch.

Refs morluto#1048, morluto#2147.
Copy only selected project files through a cancelable sequential stream traversal, reject symlinks, use the regular-file ownership helper, keep import-only derived flags out of existing-project launches and reject caller import overrides during profile resolution.

Source inspection and git diff --check; tests/builds/typechecks/CI reruns skipped at explicit user request.
Keep the established missing-project message for ENOENT and preserve other filesystem failures instead of relabeling them as absence.

Source inspection and git diff --check only. Tests, builds and typechecks skipped by explicit user request. GitHub review writes paused globally.
REA_GHIDRA_EXISTING_PROJECT / REA_GHIDRA_EXISTING_PROGRAM were rejected on
win32 at profile resolution and again in headless argument construction, so
the existing-project `-process -noanalysis -readOnly` path was Linux/macOS
only even though the Windows P0 launcher builds the same invocation.

Remove both win32 gates. The Windows batch launch already quotes the project
location, the fixed `rea-project` name and the `-process <program>` program
name, and copyExistingGhidraProject writes the caller's project into the owned
private runtime, so no POSIX-only behavior is required. Import-time selections
(DOS/raw-binary/PSP/language override/seeds/disabled analyzers) stay rejected,
and the no-return repair post-script remains POSIX-only, matching a fresh
Windows import's `disabled-windows-p0` profile.

Verified on Windows with Ghidra 12.1.4 and a pre-imported engine2.dll (6.8 MB
PE x86-64, project produced by analyzeHeadless.bat under the fixed
`rea-project` name): `rea analyze` opened the owned copy in ~12 s with
import_mode `existing-project-copy` and reported 13,302 procedures, versus a
~237 s cold import. The Windows-rejection regressions now assert the admitted
behavior, and the installation guide documents the batch pre-import command.
Design for reusing previously analyzed Ghidra programs across sessions. The fresh-import path is -import -readOnly -deleteProject, so nothing is saved; the existing-project path from morluto#2358 is manual and unmanaged, and its random session identity blocks cross-session snapshot replay. The plan stores projects under <home>/.rea/cache/ghidra-projects keyed by target+provider+JVM+import-profile, publishes on a clean import, opens a copy with -process -noanalysis -readOnly, and makes the bound analysis profile open-mode independent for managed sessions so prior snapshots replay.

Includes the independent-review corrections (purge path, publish seam, JVM in the key, mutating-op publish gate, cheap hit verification, expanded-project admission).
Add REA_GHIDRA_PROJECT_CACHE (off|read|on, default off), REA_GHIDRA_PROJECT_CACHE_DIR (default <home>/.rea/cache/ghidra-projects so uninstall --purge-data removes it), and REA_GHIDRA_PROJECT_CACHE_MAX_BYTES. Reject the cache combined with a caller-selected REA_GHIDRA_EXISTING_PROJECT, which is a competing source of saved project state.
Key derivation over target+provider+JVM+platform+import-profile, a canonical manifest schema, expanded-project admission (rea-project.gpr + .rep, no symlinks), a streaming digest of stored files, and a store that resolves a hit or reserves a staging slot, publishes a clean import atomically under a per-key lock, handles a concurrent-publisher loser, and prunes the oldest entries to the byte ceiling. Unit tests cover key sensitivity, manifest round-trip/tamper, admission, hit/miss, the publish race, abandon, and pruning.
Add GhidraHeadlessLauncherOptions.saveImportedProject, which omits -readOnly and -deleteProject so Ghidra writes the analyzed program into the owned project for the caller to persist. Combined with a pre-imported existing project it is rejected, since the two select opposite import modes.

Default behaviour is unchanged: without the flag the invocation emits the same -readOnly -deleteProject pair as before.
Mark config, the provider-independent store, and the launcher save flag complete; note the identity rebinding and session-lifecycle wiring as the remaining real-provider-checked slices.
Resolve a managed project-cache entry when the cache is enabled: a miss imports writable and publishes the saved project on a clean, complete, mutation-free close; a hit hands the stored project to the launcher and opens it with -process -noanalysis -readOnly. The committed profile drops import_mode provenance and adds a stable project_cache commitment so a miss and a hit produce the same profile digest, letting a retained analysis snapshot replay across sessions. A caller-selected REA_GHIDRA_EXISTING_PROJECT keeps its per-session identity and stays non-replayable.

Track client mutations and gate publishing on them so session annotations are never stored as engine analysis. Add a hit limitation naming reused analysis. The default off path is unchanged.
Describe enabling, identity, publishing gates, admission, and the intent-bound limitation; link it from the provider guide and the pre-imported-project section.
The language-override host reported a Linux platform while the temporary install directory is a Windows path, so the absolute-path check rejected it; report the running platform so the path flavor matches the filesystem. Skip the MIPS saved-project lane (POSIX bridge-path assertion) and the imported-snapshot replay lane (Windows Ghidra requires native source admission before replay) on Windows, as the suite already does for other POSIX-only tests.
Windows Ghidra P0: engine2.dll first open 256.8s, cache hit 13.1s; a minimal PE fixture 17.4s to 11.1s. Note the fixed Node/JVM startup cost and the hit limitation/profile binding that confirm the hit path.
Discover and maintain the managed Ghidra project cache without opening a target. cache-list reports the directory, mode, ceiling, and each entry's key, program name, target digest, provider version, size, and creation time; cache-prune enforces the ceiling; cache-remove drops one entry by key. Adds GhidraProjectStore.remove(key) with key validation and a unit test, and documents the commands.
… digest

cache-verify re-admits every stored entry and re-hashes its files against the recorded project digest, reporting corrupt entries; --repair removes them. Exclude the store's own manifest.json from the digest so a published entry and its later verification compute the same project identity. Refactor the cache command handlers and the store verification to keep the module lint-clean. Live-tested: verify reports ok:1 on a populated entry.
REA_GHIDRA_PROJECT_CACHE_MAX_AGE_DAYS discards entries older than the configured number of days during prune, alongside the existing byte ceiling. Threaded through the store options and the provider/CLI; extracted the cache-config builder to keep parseConfig within its previous size. Adds config and store tests and documents the setting.
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Repo admins can enable using credits for code reviews in their settings.

@greptile-apps

greptile-apps Bot commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[High impact] The changes since the previous review appear safe to merge.

Summary

Adds a persistent Ghidra project cache and stable analysis identity across cache misses and hits. The branch also carries multi-target sessions and background analysis.

  • Ghidra reuses matching analyzed projects across sessions.
  • MCP sessions keep targets open and let callers track analysis jobs.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Open target] --> B[Resolve import identity]
  B --> C{Matching stored project?}
  C -->|Yes| D[Lease entry and copy into private runtime]
  D --> E[Open without auto-analysis]
  C -->|No| F[Import and analyze]
  F --> G{Clean shutdown, natural exit, no edits?}
  G -->|Yes| H[Copy into staging and publish atomically]
  G -->|No| I[Skip publication]
  E --> J[Reuse stable snapshot identity]
  H --> J
Loading

Reviews (38) · Last reviewed commit: "test(ghidra): support unprivileged Windo..." · Reviewed by Greptile

A start that waited for the retained cleanup retry could be cancelled while
that retry ran. The retry is an await, so the abort event had already fired
when the request took the permit or entered the queue: a listener attached
after the abort never runs, so a cancelled request that entered the queue
waited for a permit that may never come, and a cancelled request that took
the permit started a provider nobody asked for.

The start now rechecks the cancellation after the cleanup retry, so a
cancelled request takes no permit, enters no queue and reports the
cancellation.

The retained cleanup fixture now counts its closes and can hold a retry
open, so the cancellation can be placed inside the retry window. The setup
the three retained cleanup tests share moved into one helper, which removed
the duplicated setup blocks the file carried.

Validation on this Windows host:

- npx tsc --noEmit - pass.
- npx vitest run src/application/binary - 198 passed.
- npm run test:local -- src/application/binary/TargetOpenLimits.test.ts and
  the other registry and background analysis suites - 54 passed.
- npx oxfmt --check and npx oxlint on the changed files - pass (0 errors,
  the repository-wide warnings only).
- npm run lint - pass (0 errors).
- npm run jscpd - 3132 clones, one below the 3133 of the unchanged tree;
  the 3.55% duplication stays over the 3.5% threshold, as it already was
  before this change.
- npx vitest run src/application - 10 failures in the Hopper and setup
  planning suites, the same macOS and Linux only failures this host already
  had without the change.

The new regression test fails against the previous implementation: the
cancelled open never settled.
A background open that reuses the target the primary already holds
answered before the replace path ran, so the other targets stayed open
although the request asked to replace them. The reuse skipped the step
that closes them: the reused slot answered for the primary, and the
replace path that closes every other slot was never reached.

Both background open paths now close the replaced targets before they
answer for the reused one. The primary keeps the target and the job the
reuse answers for; only the other slots are closed, and a close that
fails is reported as the replace path reports it.

Validation on this Windows host:

- npx tsc --noEmit - pass.
- npx vitest run src/application/binary src/ghidra - 997 passed, 35
  skipped.
- npx oxfmt --check and npx oxlint on the changed files - pass (0 errors,
  the repository-wide warnings only).
- npm run lint - pass (0 errors).
- npm run jscpd - 3132 clones, unchanged by this change; the 3.55%
  duplication stays over the 3.5% threshold, as it already was before it.

The new regression test fails against the previous implementation: the
replaced target stayed open.
disappointement and others added 3 commits October 21, 2026 11:52
A background open that reuses an open target closed the other targets
before it answered for the reused one, and that close is an await. The
slot the reuse was made for can take on another analysis while the close
waits: a rebuilt binary at the same path, a job that took the slot over,
or another provider bound to it. The reuse then answered with the slot's
new job or target, so the caller received the handle and the analysis of
a target the request never asked for.

The reuse is checked against the slot again after the close. The same job
has to bind the slot, the slot has to answer for the requested target,
and a slot that no job binds has to be bound to the provider the decision
was made against. A slot that took on another analysis answers with
nothing, so the request opens the target it asked for instead of
answering for the replacement.

Both background open paths share the check, so the resolved open has the
same rule. The shared helper also removes the duplicated reuse block the
two paths carried.

Validation on this Windows host:

- npx tsc --noEmit - pass.
- npx vitest run src/application/binary - 201 passed.
- npx vitest run src/ghidra - 798 passed, 35 skipped.
- npx vitest run src - 11,416 passed; the 65 failures are the pre-existing
  POSIX/macOS-only suites this host already had without the change (WABT,
  Electron, Hopper, ADB, Mac, Doctor, source maps, Godot, browser worker).
- npx vitest run --project mcp-boundary - 959 passed; the 24 failures are the
  pre-existing POSIX/macOS-only suites this host already had without the
  change.
- npx oxfmt --check and npx oxlint on the changed files - pass (0 errors, the
  repository-wide warnings only).
- npm run verify:module-boundaries; npm run verify:architecture-guards;
  npm run scan:todos; npm run knip - pass.
- npm run jscpd - 3131 clones, one less than the 3132 before this change; the
  3.55% duplication stays over the 3.5% threshold, as it already was before.

The new regression tests fail against the previous implementation: the
answer was the replacement's target instead of the requested one.
…he session composition

Merges upstream/main into feat/ghidra-project-cache. The two branches changed
the same session composition, so each conflict keeps both sides' behaviour.

- src/application/binary/BinarySession.ts keeps the transition-retirement
  admission of this branch and the app-bundle resource session of upstream.
  The bundle-resource open and close now run inside the branch's named
  transition, the close reports a bundle-resource cleanup failure, and the
  session keeps both the epoch retirement and the bundle-resource factory.
- src/application/binary/BinaryTargetRegistry.ts takes the bundle-resource
  factory, hands it to every slot it creates, and implements
  openBundleResources, which upstream's BinarySessionPort requires and this
  branch's registry did not yet provide.
- src/application/binary/BinarySessionComposition.ts composes the registry
  with the target limits of this branch and the bundle-resource factory of
  upstream.
- src/application/binary/CompositeProvider.ts keeps the provider list this
  branch selects the started provider from and the bound-client factories
  upstream binds a profile with.
- src/composition/binary.ts passes the target limits and the bundle-resource
  factory to the same composition.
- src/config/parseConfig.ts keeps the native-analysis environment filter of
  upstream and the project-cache configuration of this branch.
- src/domain/analysisErrorCore.ts keeps the target-not-open and in-progress
  errors of this branch and the resource-constraint recovery of upstream.
Comment thread src/server/registerSessionTools.ts Resolved
…he handles

The review raised four findings. All four are valid and are fixed here.

- The cache root is admitted before the store reads or removes what it
  holds. The maintenance commands reached `readdir(root)` and the recursive
  child removal without the root check the open path uses, so a root another
  principal replaced with a link was followed and the linked directory's
  staging copies and repair copies were removed. The root admission is now
  shared: a linked root is refused with a typed `root` failure, a root that
  does not exist reports an empty cache instead of being created, and the
  publish and the abandoned staging slot are admitted as well.

- A foreground open no longer releases the start permit before the provider
  starts. The production route is a composite, whose health probe answers
  without touching a child, so the import started with the first query that
  needed it, outside the shared start limit. The client now starts a
  provider it defers under a start admission that takes the permit for that
  call, so `REA_MAX_CONCURRENT_ANALYSES` covers the real start.

- A call that names a handle is checked under the same lifecycle admission
  that protects the execution. A request that waited behind a foreground
  replacement ran against the target that replaced the one its handle named;
  it is now refused with `target_not_open` before it reaches a provider.

- The open reply names the target the open produced. The reply read the
  current slot after the open, so an open that another one overtook during
  its wait reported another target's handle and job state; the open now
  returns its own handle and state with the target it opened.

The new regression tests fail against the previous implementation: the prune
removed the linked directory's children, both provider starts ran beside each
other, the queued call changed the replacement target, and the open reply
named the other target.
The review raised one finding. It is valid and is fixed here.

- The shared start permit was taken before an open and given back after the
  open returned, so it was held while the open waited for the calls that
  ran before it. With `REA_MAX_CONCURRENT_ANALYSES=1` a foreground reopen
  and an earlier analysis call waited for each other forever: the call
  waited for the permit the reopen released only after the open finished,
  and the reopen waited for that call in the session's drain. Later
  provider starts stayed queued behind the two.

  The permit is now taken around the provider start itself, after the
  earlier calls have drained. The session runs the start of a client that
  starts its provider with its first call, and the explicit start of a
  client that defers it, under the shared start admission, and the slot
  that runs the start owns the permit. A close that gave up on a start
  that never unwound gives the permit back, and a close that could not
  release the provider keeps it, so the analysis cap still counts a
  provider that is still alive.

  The capacity tests now observe the provider start instead of the client
  creation, which is no longer the start itself: the client is created
  before the permit is taken, and the permit covers the start that follows.

The new regression test fails against the previous implementation: the call
never reached the provider start, because it waited for the permit the
reopen held while it drained that call.

Validated with `npx tsc --noEmit`, `npx vitest run src/application/binary`
(222 passed), `npx vitest run src/server` (121 passed), `npm run check:fast`,
`npm run verify:architecture-guards`, and `npm run verify:module-boundaries`.
The POSIX-only failures of the other lanes on this Windows host are
pre-existing and unchanged.
The review raised one finding. It is valid and is fixed here.

- A failed start released its permit before the cleanup of the failed start
  closed the client. The provider that start left behind could still be
  running when that cleanup could not release it, and the analysis cap no
  longer counted it: with `REA_MAX_CONCURRENT_ANALYSES=1` the next queued
  import started beside the provider that was still alive, and the server
  started more provider processes than the limit allows.

  The cleanup of a failed start now runs under the shared start limit, so the
  limit counts that provider until the cleanup settles. A cleanup that could
  not release the provider hands the limit to the retained cleanup, which
  gives it back when the provider is gone. The limit is still taken after
  the calls that ran before the start have drained, so the reopen deadlock
  fix is preserved.

  The session owns the provider and reports whether it may still be running,
  so the registry owns the accounting: a failed open that still owns a
  provider keeps its registration and is reported as retained, and the
  retained cleanup releases the provider and the limit. A start that rejects
  instead of answering is reported as a failure inside the limit, so that
  cleanup still runs and releases the client it started.

- The new regression test fails against the previous implementation: the next
  start ran while the provider of the failed start was still alive.

Validated with `npx tsc --noEmit`, `npx vitest run src/application/binary
src/server tests/composition/analysis-sessions` (429 passed), `npx vitest run
tests/boundary/mcp/multiTargetSessionMcp.test.ts` (7 passed),
`npm run check:fast`, `npm run verify:architecture-guards`, and
`npm run verify:module-boundaries`.
Comment thread src/application/binary/BinarySession.ts Resolved
Comment thread src/application/binary/BinarySession.ts Resolved
The review raised two findings on the failed-start cleanup. Both are valid
and are fixed here.

- An open that was cancelled while it waited for a start permit skipped the
  cleanup of its failed start. The admission reported the failure it could
  not acquire the permit for without running that cleanup, and the open
  assumed the cleanup had already run: the client the open created was never
  closed, and the target the open had replaced was closed and never
  restored.

  The session owns the cleanup of its failed start now. It runs once: the
  admission runs it under the shared start limit, and a failure the
  admission reported without running it runs it in the open. The admission
  runs the cleanup for a start it could not attempt as well, so the limit
  still counts a provider the cleanup could not release, and the slot that
  owns that provider stays registered as retained.

  The health probe of a client that starts the provider it selected runs
  inside the admitted start now, so a probe that fails runs the cleanup of
  the failed open under the limit that counts the provider the probe left
  behind. The probe of a composed client answers without touching a child,
  so the limit is not held for a provider start that never ran.

- The cleanup of a failed start restored the target the open replaced before
  the open checked whether it had been retired. A forced close that
  released the start while it was in flight therefore left the closed slot
  holding a target again, and a target that opened in that slot afterwards
  could have its binding overwritten.

  The cleanup restores the target only while the slot still owns it. The
  lifecycle generation of the open is checked before the restoration and
  again after it waited for the restored client, so a slot that was closed
  while the restoration waited releases the restored client instead of
  binding it.

The new regression tests fail against the previous implementation: the
client of a cancelled open was never closed, the target it replaced was
never restored, and a closed slot held the target it had before the failed
open.

Validated with "npx tsc --noEmit", "npx vitest run src/application/binary
src/server tests/composition/analysis-sessions" (434 passed), "npx vitest run
tests/boundary/mcp" (the 16 failures are the pre-existing POSIX-only
failures this host already had without the change), "npx oxfmt --check" and
"npx oxlint" on the changed files, "npm run check:fast",
"npm run verify:module-boundaries", "npm run verify:architecture-guards",
"npm run scan:todos", "npm run knip", and "node scripts/check-doc-facts.mjs".
The review raised a finding on the restoration of a failed open. It is
valid, and it is fixed here.

A cancelled replacement waits for a start permit, and the admission runs the
cleanup of that failed open, which restores the target the open replaced. The
restoration created its client without the start admission, so the provider
start that client defers ran outside the shared limit: a query on the restored
target launched an import beside a provider that was still starting, and the
analysis cap that bounds the concurrent analyses was exceeded.

The session keeps the admission the open carries and hands it to the restored
client, so the restored target starts its provider under the same limit as
every other start of the session. The restoration does not start the provider
under that admission: the health probe of a composed client answers without
touching a child, and a probe that waited for the limit would make a cancelled
open wait for a permit it cannot use.

The new regression test fails against the previous implementation: the query on
the restored target did not run under the admission.

Validated with "npx tsc --noEmit", "npx vitest run src/application/binary"
(230 passed), "npx vitest run src/application/binary src/server
tests/composition/analysis-sessions" (435 passed), "npx vitest run src/ghidra"
(801 passed, 35 skipped), "npx vitest run --project mcp-boundary" (the failing
suites are the POSIX-only and WABT/IDA suites this Windows host cannot run;
the same suites fail without the change), "npx oxfmt --check" and "npx oxlint"
on the changed files (0 errors, the repository-wide warnings only), "npm run
verify:module-boundaries", "npm run verify:architecture-guards", "npm run
scan:todos", "npm run knip", and "node scripts/check-doc-facts.mjs".
Brings upstream/main (22c4b52) into the branch.

The only conflict was src/cli/options.ts. Upstream removed the obsolete
directAnalysisOptions forwarding API and inlined its mapping into
directAnalysisOptionsFromCli; this branch adds the analysis-mode flag and
the guard that refuses a background analysis the one-shot CLI cannot own.

The resolution keeps upstream's inlined mapping and drops the forwarding
helper upstream removed, and keeps this branch's analysis-mode flag and its
guard.

Validated with "npx tsc --noEmit" and "npx vitest run src/application/binary
src/cli tests/boundary/cli". The failures in the cli-boundary suites are
pre-existing on this Windows host: the same suites fail on upstream/main
without this branch.
… on their owner

The multi-target registry answered an admitted request with the target that
happened to be current when the request wrote its Evidence, and a replacement
open dropped the job of a background start that ignored its abort, so the
replacement, the close and the shutdown could wait for a start that never
finished. A background open also read its staged snapshot under the request's
signal, so a cancelled request failed the job the server owns.

- The registry carries the slot a request was admitted for. The Evidence, the
  workflow snapshot and the snapshot cache of that request belong to that
  slot, not to the slot another open made current while the request ran.
- A replacement cancels the job of the previous start and waits for it within
  the settle bound. A start that ignored its abort still holds the session
  transition, so the slot is released with the forced close before it is
  reused. The job stays registered until the close released its client, so a
  later close finds it and takes the bounded wait and the forced close.
- The staged snapshot is read under the signal of the open that owns the
  read, so a background job reads under the job's own signal and a cancelled
  request stops only the wait for that job.

The new regression tests fail against the previous implementation: the
Evidence of an admitted request named another target, the replacement open
waited for a start that never unwound, and the snapshot loader received no
signal at all.
…ose a newer analysis

The cancellation of a background start that ignored its abort belonged to the
request that asked for it, so two replacement opens that cancelled the same
job each ran their own bounded wait and their own forced close. The first
wait expired, released the wedged start and opened a new analysis in the
slot; the second wait expired later and force-closed that slot again, so the
newer analysis was closed and its job removed from the registry by a
cancellation that owned neither.

- The job owns its cancellation. Every request that cancels the same job
  waits for the same operation, so the slot is force-closed once and the job
  leaves the slot once. A cancellation that failed is given up, so a later
  cancel retries the cleanup instead of replaying the failure.
- The cleanup runs while the job still holds the slot. A slot that took on
  another analysis in the meantime keeps that analysis, and the job of that
  analysis stays registered.

The new regression test fails against the previous implementation: the
analysis the first replacement opened was closed by the stale cancellation
of the second one.

Validated with "npx tsc --noEmit", "npm run check:fast" and "npx vitest run
--project services src/application/binary", plus the multi-target MCP
boundary suite. The 10 failures of the services project (LinuxHopper,
MacHopper, Setup.planning, Doctor) are pre-existing on this Windows host:
the same suites fail without this change.
@pablodiazjorge

Copy link
Copy Markdown
Contributor

Ran the Windows half that is still open, on a non-elevated account: the account is a member of Administrators, but its UAC-filtered token runs at medium integrity, so files it creates are owned by the user rather than by BUILTIN\Administrators. That is the other case from the administrator account described in the PR.

Host: Windows 11 Pro x64 (build 26300), NTFS, Node 24.22.1, Ghidra 12.1.4 with Temurin 21 and the 6.7.0 Windows native addon. Head 3d3820a3a, built with npm run compile. Target: WallaceSrc.exe from the GhidraClass exercises shipped with Ghidra, through rea analyze --provider ghidra.

Cache hit with real Ghidra

Run Wall time What showed up
REA_GHIDRA_PROJECT_CACHE=off 17.8 s fresh import
on, empty root 17.7 s one entry stored (about 2 MB); cache-list shows it
on, new process, same file 8.6 s "Ghidra analysis was reused from a local project cache" in the Evidence

Is the root private? Yes. icacls on the root after the first run: owner the current user, inheritance disabled, and only the user and NT AUTHORITY\SYSTEM with (OI)(CI)(F). The default root, %USERPROFILE%\.rea\cache\ghidra-projects, read back the same on its first run, so no rebuild was needed there.

Rebuild of a widened root. I granted BUILTIN\Users read on the root (icacls <root> /grant *S-1-5-32-545:(OI)(CI)RX) and analyzed again. The session reported "The Ghidra project cache root was not owner-only and was rebuilt as a private directory; 4 stored item(s) were preserved", reused the entry (8.6 s), and the root read back owner-only again. The preserved entry and its files inherit the same two entries, and no set-aside root or staging copies were left.

Two sessions on a widened root at once. Repeated three times. One session rebuilt the root and reused the entry (about 9.5 s); the other imported fresh (about 18 s) with no cache note in its Evidence and no warning in its log, so it found no entry while the copy was in flight rather than hitting the busy path. The root ended owner-only with one entry that cache-verify reports as valid, and the repair lock and staging area were empty. With a 2 MB entry the copy is too short to catch a held lock, so the busy report itself was not exercised.

PR tests on this host. npx vitest run src/ghidra/projectCache src/ghidra/GhidraProjectCache.behavior.test.ts src/ghidra/GhidraProvider.behavior.test.ts: 210 passed, 8 skipped, 6 failed. All six fail because the fixture creates a symbolic link, which Windows refuses to a non-elevated user without Developer Mode (EPERM ... symlink); an elevated account can create them:

  • GhidraProjectCacheRoot.test.ts: "rejects a root that is a link", "rejects a linked root before verification", "keeps an entry another session published while the repair was running", "keeps every stored project when the repair cannot copy it into the private root", and "rejects a linked root a concurrent initializer created" (there the link is made inside the injected initializer, so the assertion sees a different message instead of an EPERM).
  • GhidraProjectStore.test.ts: "replaces a link at a lease file without writing through it".

Not run: the mutation case (Windows Ghidra P0 has no mutation authority), an elevated account, the busy path with a held lock, a large target, macOS and Linux.

@pablodiazjorge

Copy link
Copy Markdown
Contributor

Re-ran the PR tests on d696fa8ca, same host and account as above (non-elevated, no Developer Mode).

npx vitest run src/ghidra/projectCache src/ghidra/GhidraProjectCache.behavior.test.ts src/ghidra/GhidraProvider.behavior.test.ts: 215 passed, 9 skipped, 0 failed (it was 210 passed, 8 skipped and 6 failed on 3d3820a3a).

  • The five directory-link cases now pass with junctions: "rejects a root that is a link", "rejects a linked root before verification", "keeps an entry another session published while the repair was running", "keeps every stored project when the repair cannot copy it into the private root" and "rejects a linked root a concurrent initializer created".
  • "replaces a link at a lease file without writing through it" is skipped with "Windows symlink creation requires unavailable privileges".

The commit only touches the two test files, so the real Ghidra results above still describe the product code at this head.

@morluto

morluto commented Oct 26, 2026

Copy link
Copy Markdown
Owner

Thanks for the persistent-cache proposal. The existing-project prerequisite is now available through #7236, which uses a verified copy and explicit project selection; #2358 was closed as overlapping. This PR’s persistent publication, eviction and cross-session identity are separate work. Could you narrow it against the landed project owner and update the dependency and verification description? The PR remains open.

@morluto

morluto commented Nov 24, 2026

Copy link
Copy Markdown
Owner

@goncalompontes thanks for the PR — this branch has merge conflicts with main. Could you merge or rebase onto the latest main and resolve them? Once it's clean and CI is green we can take another look.
cc @gensokyo-dev @dandgabr

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request provider: ghidra Analysis provider / engine: ghidra

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants