Skip to content

perf(plugins): stop re-hashing a verified plugin binary on manifest reads - #1371

Merged
Quick104 merged 2 commits into
mainfrom
perf/plugins-manifest-read-without-rehash
Sep 24, 2026
Merged

Quick104 merged 2 commits into
mainfrom
perf/plugins-manifest-read-without-rehash

Conversation

@Quick104

Copy link
Copy Markdown
Contributor

Problem

Related issue: N/A
Validation tasks: could reach #1224 C1 (load a catalog plugin, verify SHA-256, reject mismatches) and #1225 C1 (install a catalog plugin and see usable status); neither case has a recorded result. The checksum check before a plugin launches is unchanged.

Every read of an installed plugin's manifest hashed the plugin's whole binary. ArchiveCache.Ensure reads the binary, computes its SHA-256, and compares it with the checksum in the installed manifest, all under the cache's one mutex. Callers that only need the manifest went through it too:

  • the admin plugin list, once per installation on every load (the admin sidebar requests this list to build its Plugin Apps group)
  • plugin route matching and plugin asset requests (an asset request reads the manifest twice)
  • user config schemas, watch-sync provider config, and saving a plugin's global config
  • network-access provider discovery

With a 16 MiB binary, one warm manifest read took about 59 ms and allocated 16.8 MB in the benchmark below. The mutex serializes these reads, so a plugin page load hashes every installed binary in turn, and concurrent plugin asset requests wait behind it.

Approach

ArchiveCache gets a Manifest method for callers that read the manifest or serve packaged assets and never execute the binary. After a successful hash, the cache records per installation the binary's path, the checksum it matched, and the file's identity (device and inode), size, and mtime, taken from the open file before hashing. A later Manifest call skips the hash only if the binary at that path is still the same file with the same size and mtime, and the manifest still names the same checksum. Any other case goes through the full check and repair that Ensure runs: the first read in a process, a new release path, a replaced or rewritten file, a changed checksum. A failed check drops the record.

Ensure still hashes the binary on every call and never reads the record. Every launch goes through start (reached from Start, the on-demand client path, and the resident supervisor) or the admin connection test, and both call Ensure before exec. Startup preload, which checks and rehydrates files without running them, also still calls Ensure. The manifest-only service paths (manifestForInstallation, ResolveAssetPath, SetGlobalConfigWithClears, networkAccessManifest) now call Manifest.

Security reasoning:

  • The skipped hash never protected what manifest readers use. It proves only that the binary matches the checksum written in the manifest file. It does not cover the manifest or the packaged assets, and anyone who can rewrite the binary can rewrite the manifest to match. Manifest reads still load and validate the manifest from disk on every call and still check that the binary and every packaged asset exist.
  • Nothing runs the binary on the skipped path. If someone rewrites a binary in place and restores its size and mtime, manifest reads keep answering, and the next launch's Ensure hashes the binary, rejects it, and restores the stored archive before exec. TestArchiveCacheManifestTrustsOnlyAnUnchangedVerifiedBinary covers this case.
  • Nothing is persisted, and the launch path never consults the record. Launches do not get a way to skip the hash.

On a cluster, the record lives in each process's memory. Every API server and proxy node hashes its own copy of a binary once after it starts before trusting it. A new release lands in a new install directory, so its path changes and it is hashed before the record applies.

The hash now streams the file with io.Copy instead of loading it with os.ReadFile. That gives tests a hook to count hashed bytes, and it cuts the allocation of each launch check from the binary's size to about 37 KB.

Alternatives considered:

  • A manifest accessor that only checks the files exist and never hashes. It is simpler, but a corrupted binary would then be repaired only at the next launch, never by a manifest read. The record keeps that repair for any change visible in the file's stat.
  • A persisted checksum stamp that launches trust instead of hashing. Rejected because it weakens the tamper check in front of exec.

Before and after

Fixture: an installed plugin with a 16 MiB binary and one packaged asset, read after one warm-up read. For "before", a temporary copy of the same fixture and benchmark ran against the base commit. Its hashed-byte counter was a temporary one-line counter at the base's hash call, not committed.

Metric Before After How measured
Bytes hashed per warm manifest read 16,777,216 0 Counter at the hash call: temporary on the base, countHashedBytes after
Bytes hashed per warm plugin asset request 33,554,432 0 Same counter, one ServeAsset call
Bytes hashed per plugin launch (Start) 16,777,216 16,777,216 Same counter; unchanged by design
Bytes read per warm manifest read 16,777,749 to 16,777,773 533 to 534 rchar delta in /proc/self/io around one call, no instrumentation. The after value is the 420-byte manifest file plus about 113 bytes from reading /proc/self/io itself
Warm manifest read, allocs/op 95 88 BenchmarkInstalledPluginWarmRead/manifest, -benchmem
Warm manifest read, B/op 16,789,699 3,792 Same benchmark
Warm manifest read, time 58.8 ms median (54.3 to 61.1) 40.9 µs median (34.6 to 58.1) Same benchmark, 6 runs each on a shared 48-core host at load average about 40; timings are noisy
Pre-launch check (Ensure), B/op 16.79 MB 37,240 BenchmarkInstalledPluginWarmRead/ensure (the check only; no process starts)
Pre-launch check (Ensure), time 70.4 ms median 60.4 ms median 8 interleaved runs each; the runs overlap (49 to 145 ms), so treat launch time as unchanged

TestServiceManifestReadsSkipRehashOfVerifiedBinary asserts zero hashed bytes for each warm manifest-only path and a full hash on every Start. With the record disabled, it fails: each warm read hashes 1,048,576 bytes (its 1 MiB fixture) and an asset request hashes 2,097,152.

TestArchiveCacheManifestTrustsOnlyAnUnchangedVerifiedBinary covers each part of the file check. Manifest hashes and repairs after a visible change and after a different file is renamed over the binary with the verified size and mtime. It skips an in-place rewrite that keeps size and mtime, which the next Ensure catches and repairs. A failed check clears the record, and a new release path is hashed before it is trusted.

Validation

All checks below ran on the branch after a clean rebase onto current main. The branch changes no web, migration or workflow files, so the web build, make migrate-validate and workflow lint did not apply.

  • make embed-stub, go build ./..., go vet ./...: pass. gofmt -l .: empty. make lint-changed: 0 issues.
  • GOFLAGS=-p=6 make test-go: 158 packages pass, including internal/plugins, internal/api/handlers and internal/apiv2. One failure: internal/policy TestEngineReloadRejectsInvalidCustomSource, where a policy evaluation hit its 25 ms timeout while the shared host ran at load average about 165. internal/policy does not import internal/plugins. Run alone, the test passed 5 of 5 times on this branch and on main, and the whole package passed on a re-run.
  • make verify-settings-bindings-all verify-playback-fixtures verify-route-inventory verify-migration-ledger verify-scenario-catalogs verify-offline-routes verify-apiv2-openapi verify-apiv2-contract verify-apiv2-fixtures verify-local-paths: all pass. go test -count=1 -run '^TestCommittedArtifactMatchesRouter$' ./internal/apiv2/: pass.
  • Both new tests pass. The archive cache, manifest-read, network-access, HTTP proxy and asset tests also pass under -race.
  • Negative checks: with Manifest forced to hash, both new tests fail, and the service test reports 1,048,576 hashed bytes per warm read and 2,097,152 per asset request. With Ensure forced to trust the record, both fail. With the size and mtime comparison removed, or the file identity comparison removed, the archive cache test fails.
  • Measurements re-run on the rebased branch with the 16 MiB fixture: the hashed-byte rows match the table exactly. The benchmark again reports 3,792 B/op and 88 allocs/op for a warm manifest read and 37,240 B/op for Ensure. Six runs each at load average about 50 gave a 42.9 µs median (36.0 to 58.7) for a warm manifest read and a 60.9 ms median (52.2 to 64.5) for Ensure, in line with the table.
  • The bytes-read row was measured again on both sides in this pass. An earlier run recorded 546 bytes after, 13 more than now. The fixture manifest has 13 commas, and the protojson encoder adds a space after each comma in some builds, which likely explains the difference.
  • DB-backed tests: not run. The change touches no SQL, and the plugin package's DB-backed tests cover the builtin delete guard and catalog settings, not the archive cache.
  • Not run: a manual walk of Plugin management / marketplace — Server #1224 C1 or Plugin management / marketplace — Web admin #1225 C1 on a deployed build.

Risks

  • A binary rewritten in place with its size and mtime restored, or corrupted on disk without changing either, is no longer detected by manifest reads. The next launch still detects and repairs it before exec. Before this change, the next manifest read did.
  • On a filesystem with coarse mtime resolution, a same-size rewrite within one tick also looks unchanged to manifest reads, with the same outcome: caught at launch.
  • The cache keeps one small record per installation it has verified, including uninstalled ones, until the process restarts.
  • Manifest reads still take the archive cache's mutex. A warm read now holds it for a few stat calls and a manifest parse instead of a full hash.

Checklist

  • I read and can explain the complete diff.
  • This pull request addresses one concern.

AI Disclosure

  • Harness: Claude Code, running in T3 Code
  • Tool(s): Claude Code built-in tools (shell, file read/edit, Workflow subagents), GitHub CLI (gh)
  • Model(s): claude-opus-5-5[1m] for the main session and all subagents
  • Involvement: Fully AI-generated; maintainer review pending
  • Adversarial review: a separate reviewer subagent (claude-opus-5-5) reviewed the full diff, traced every caller of Ensure and Manifest, and re-ran the measurements in scratch worktrees of the base and the branch. Every deterministic number in the table reproduced exactly; its timings differed only with host load. It also ran negative checks: forcing Manifest to hash, forcing Ensure to trust the record, and removing parts of the file check. It approved with no blocking findings. Its findings and how each was resolved:
    • Minor: no test covered the file identity half of the check; removing os.SameFile left the package passing. Fixed: the archive cache test now renames a different same-size file with the verified mtime over the binary and expects a hash and repair. It fails without os.SameFile.
    • Nit: this description and the first commit's message named startup preload as a path that executes the binary. Fixed in this description and noted in the review-fix commit message: launches go through start or the connection test, and preload only checks and rehydrates files. The first commit's message is left as is because the squash merge replaces it.
    • Nit: the new service method installedManifest shared its name with a local variable in doEnsureClient. Fixed: renamed to loadForManifestRead.
    • Nit: the benchmark case launch measured only Ensure, not a process start. Fixed: renamed to ensure.
    • Nit, optional: compare ctime on Unix so manifest reads also catch an in-place rewrite that restores mtime. Not taken. Manifest readers consume the manifest and packaged assets, which the binary hash never covered, so a ctime check would guard a file those callers never run. The launch path re-hashes regardless. It would add build-tagged per-OS stat code to the plugins package for that. The gap is listed under Risks.

🤖 Generated with Claude Code

Quick104 and others added 2 commits September 24, 2026 02:56
…eads

Every manifest read went through ArchiveCache.Ensure, which reads and
SHA-256 hashes the whole plugin binary under the cache's global mutex.
The admin plugin list reads the manifest of every installation on each
load, and a plugin asset request reads it twice, so each navigation
hashed tens of megabytes serially per installed plugin.

Add ArchiveCache.Manifest for callers that only read the manifest or
serve packaged assets. It skips the hash while the binary is the same
file, with the same size and mtime, that this process last hashed
against the checksum the manifest still names. Any change, a new
release path, or a failed check sends the read through the full check
and repair. Launch paths (Start, the connection test, preload) keep
calling Ensure, which still hashes the binary on every call, so the
tamper check in front of exec is unchanged.

The hash now streams the file instead of loading it whole, which drops
a launch check's allocation from the binary's size to about 37 KB.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Review follow-up for the manifest read change.

The archive cache test now renames a different binary with the
verified size and mtime over the installed one and expects Manifest to
hash and repair it. Without the os.SameFile comparison the test fails;
before, nothing covered it.

Rename the service helper installedManifest to loadForManifestRead so
it no longer shares a name with a local variable in doEnsureClient, and
rename the benchmark case launch to ensure, since it measures
ArchiveCache.Ensure and starts no process.

Correction to the previous commit message: startup preload checks and
rehydrates files without executing them. Launches go through start or
the connection test, and both call Ensure.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Sep 24, 2026

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

Next included review available in 1 minute.

Check out review usage here.

View limit details

Limit details: You’ve used all 2 included reviews currently available.

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Learn how review limits work.

Review configuration:

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Advanced

Run ID: 401a080f-83dc-4395-90df-f87f59639526

📥 Commits

Reviewing files that changed from the base of the PR and between d4e35ba and dc46993.

📒 Files selected for processing (6)
  • internal/plugins/archive_cache.go
  • internal/plugins/archive_cache_test.go
  • internal/plugins/network_access.go
  • internal/plugins/service.go
  • internal/plugins/service_admin_config.go
  • internal/plugins/service_manifest_read_test.go

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quick104 has reached the 50-credit limit for trial accounts. To continue receiving code reviews, upgrade your plan.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 24, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-24T03:58:59.357481Z dc46993 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@Quick104
Quick104 merged commit 2d6a100 into main Sep 24, 2026
10 checks passed
@Quick104
Quick104 deleted the perf/plugins-manifest-read-without-rehash branch September 24, 2026 14:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant