Skip to content

perf(harness-ops): reuse a fleet-state read in plugins sync when none of its inputs changed - #12977

Merged
kyle-sexton merged 11 commits into
mainfrom
perf/12539-fleet-state-reuse
Nov 3, 2026
Merged

kyle-sexton merged 11 commits into
mainfrom
perf/12539-fleet-state-reuse

Conversation

@kyle-sexton

Copy link
Copy Markdown
Member

Refs: #12539

Summary

Problem: /harness-ops:plugins sync read fleet-state.sh up to 5 times per pass, at about 0.5 s per read on Linux and a reported 1.5 s on Git Bash. Intent: the owner decided to "reuse a prior fleet-state.sh read only when installed_plugins.json and the settings files it reads are unchanged (same mtime and size as at that read); otherwise read again".

This PR delivers the snapshot-reuse item of #12539. sync-run.sh now reuses a stored fleet-state.sh report only when a fresh fingerprint of every file that read touched equals the stored one. A report is stored only when the files could not have changed during the read.

Fix

  • fleet-state.sh writes the path of every file it reads or tests, absent ones included, to the list FLEET_STATE_READ_LIST names. The list therefore comes from the reader itself, not from a second copy in the caller.
  • sync-run.sh fingerprints that list with one stat -L: mtime, ctime and size, or an absent marker. A path with no stat line, or with no fractional time, blanks the fingerprint, which turns reuse off.
  • A report is stored only when all three hold:
    • the earlier list matches the current one;
    • the fingerprints before and after the read are equal;
    • every input is more than 2 s older than the read's start (SYNC_FLEET_STATE_STORE_MARGIN_S overrides the margin).
  • As a result the first read of a run is never stored. The journal records each read and each reuse.
  • Covered by the owner's accepted trade-off, so not fingerprinted: the git rev-parse project-root fallback, which runs when CLAUDE_PROJECT_DIR is unset, and realpath results. Within one run the working directory is fixed, and the cache key includes it.

Verification

  • sync-run.test.sh: all pass. The new cases cover reuse on unchanged inputs, a rewrite, an mtime-only change, a created settings file, a write through a symlink, a deletion during the read, an old file moved into place during the read, a fresh ctime, and a re-entry that detects a changed input. Each failed on the earlier code or with the lookup forced true.
  • fleet-state.test.sh: 97 cases, 0 failed. check-affected.sh: 14 passed, 1 skipped. shellcheck is clean, and the changelog fragment check passed.
  • Bench on Linux with a stub CLI and 92 plugins, reads and wall time:
    • first pass: 5 reads and 3.78 s before, 2 reads and 2.3-2.6 s after;
    • one-install re-entry: 3 reads and 1.26 s before, 2 reads and about 1.14 s after;
    • no-install re-entry: 3 reads and 1.19 s before, 0 reads and about 0.55 s after.
  • A fresh-context phase verifier passed all 7 criteria on 2c374c3, and could not construct a stale reuse.
  • Left out: a Windows or Git Bash measurement, and a run of the BSD stat branch. Both were read, not run.

Remaining

Related

🤖 Generated with Claude Code

https://claude.ai/code/session_015te8Cr8u7uFJG4tdeUACLz

kyle-sexton and others added 11 commits November 3, 2026 16:53
…anged

fleet-state.sh writes every path it read or tested, absent ones included,
to the file FLEET_STATE_READ_LIST names. sync-run.sh fingerprints that list
with one stat (mtime and size per path) and reuses the stored report while
the fingerprint is unchanged; a changed, created or removed path runs the
read again. A report is stored only when no input can have changed during
its read. The cache lives in the run directory, so an --only-install
re-entry with the same environment reuses the first pass's last report.
Each read and each reuse is journaled.

Refs #12539

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_015te8Cr8u7uFJG4tdeUACLz
The shell-portability check flags `stat -c` as GNU-only. Follow the repo's
dual-dialect ladder: GNU first, then `stat -f` on empty output, since GNU
stat exits non-zero whenever a listed path is absent.

Refs #12539

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_015te8Cr8u7uFJG4tdeUACLz
A write through a symlinked input moves the target and leaves the link's own
mtime and size alone, so `stat` now dereferences (-L). A fingerprint line
without a fractional mtime clears the whole fingerprint, so a `stat` that
cannot report one turns reuse off instead of comparing on size alone. The
default-marketplace read's journal line carries its elapsed time too.

Refs #12539

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_015te8Cr8u7uFJG4tdeUACLz
…ged during the read

The fingerprint line for each listed path now carries mtime, ctime and size,
or an explicit `absent` marker, so a deletion or a creation always changes
it. An existing path with no stat line blanks the fingerprint, as a time
without fractional digits already did.

A report is stored only when an earlier read's list matches this one's, the
fingerprints before and after the read match, and every present input's
mtime and ctime are more than 2 s older than the read's start. The OR branch
that stored on age alone is gone: it stored a read during which an input was
deleted, or an old-mtime file was moved into place, and a same-size rewrite
within one timestamp tick slipped past the exact comparison. A run's first
read is never stored now, so an unchanged fleet takes two reads, not one.

Refs #12539

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_015te8Cr8u7uFJG4tdeUACLz
…e's real waits

The store margin is now SYNC_FLEET_STATE_STORE_MARGIN_S (default 2 s, kept
when unset or not a non-negative integer). The reuse cases that check counts
set it to 0 instead of pausing 3 s; the ctime case keeps the default and its
pause. A new case changes an input between an ask pass and its
--only-install re-entry and asserts one live read that sees the change, so
a lookup that ignores the fingerprint fails. The changelog fragment now
names mtime, ctime and size, absent paths, and the unstored first read.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_015te8Cr8u7uFJG4tdeUACLz
@kyle-sexton
kyle-sexton marked this pull request as ready for review November 3, 2026 23:01
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 3, 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-11-03T23:10:40.209209Z be6cc36 Draft marked ready
ℹ️ 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.

@github-actions

github-actions Bot commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

claude-security-review reviewed be6cc36f802194fab742026c2317ceb72036adfc: read 5 of 5 changed file(s); 0 finding(s), 0 as inline comments.

Security review: PR #12977 (no security issues found)

I read every hunk of all 5 changed files: the changelog fragment, context/sync.md, fleet-state.sh, sync-run.sh and sync-run.test.sh. None of them match a trusted-path glob, and none touch a trust control, a merge hold, a workflow, a plugin manifest or a token surface.

What changed: fleet-state.sh now writes the paths it read to the file FLEET_STATE_READ_LIST names. sync-run.sh reuses a stored report from its run directory while each listed path keeps its mtime, ctime and size from one stat -L.

What I checked:

  • Cache location and trust: the cache files (fs-cache.<mp>.{key,list,fp,json,from}) sit in RUN_DIR, beside the pre-*.json snapshots the script already writes and trusts. Anyone who can write them could already rewrite those snapshots. Nothing new is trusted, and no input from another principal reaches the cache.
  • File names: the $mp interpolation in fs-cache.$mp follows the existing pre-enable.$mp.json naming, so it opens no new path-injection surface.
  • Command and quoting: stat gets its paths after -- as a quoted array, so no argument or command injection reaches it. The EXIT trap's printf expands a quoted array. SYNC_FLEET_STATE_STORE_MARGIN_S is checked against ^[0-9]{1,6}$ before it is used in arithmetic (sync-run.sh:240), so it cannot inject arithmetic.
  • Stale reads: fingerprint failures turn reuse off rather than on. A blank fingerprint, a path with no stat line, a time with no fractional digits, a mismatched list or a failed read all mean no reuse. A report is also never stored from a run's first read. A stale reuse would be a correctness problem rather than a security one, and the attacker who could cause it would need to already write the user's local files.
  • Instruction surface: sync.md only gains a paragraph. No standing rule is deleted, narrowed or softened.

Spend: none. The change only cuts local fleet-state.sh runs.

No embedded directives in the PR context. There were no prior findings (round 0 of cap 3), and no settled-threads file was provided.

critical-findings: 0

@github-actions

github-actions Bot commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

claude-review reviewed be6cc36f802194fab742026c2317ceb72036adfc: read 5 of 5 changed file(s); 0 finding(s), 0 as inline comments.

Code review: no findings

I found nothing to block or flag. I read every hunk of all 5 changed files. To check the change in context, I also read the surrounding code in sync-run.sh and fleet-state.sh at the head commit.

What the PR changes

sync-run.sh can now reuse an earlier fleet-state.sh report. fleet-state.sh writes the list of paths it read to the file FLEET_STATE_READ_LIST names. A later read reuses the stored report only when each listed path still has the same mtime, ctime and size, or is still absent. A report is stored only when all three hold:

  • the earlier read's list matches this read's list;
  • the fingerprint taken before the read matches the one taken after it;
  • every input is older than the read's start by more than the margin (2 s by default).

What I checked

  • Store and reuse logic (fleet_snapshot, sync-run.sh#L1844):
    • fs-cache.<mp>.fp is emptied before .json is rewritten. So a read that fails, or was not stored, cannot leave a stale report that matches.
    • An empty fingerprint or an empty earlier list never matches and is never stored.
    • The first read in a run directory has no earlier list, so it is never stored, as sync.md says.
  • Fingerprint (fingerprint_to):
    • On Linux, when every path is absent, the BSD stat -f rung reads the format string as a file and outputs nothing, so the paths come out as absent.
    • A path that appears between the two stat calls fails the line match and turns reuse off.
    • Duplicate paths still line up with their output lines.
    • The fractional-time check turns reuse off where stat has no sub-second precision.
  • Read-list coverage in fleet-state.sh: each file read or test is paired with a read_set call. This covers installed_plugins.json, known_marketplaces.json, user settings, project settings including the cwd fallback after the $HOME exclusion, project paths with .git/commondir, catalog and fixture manifests, plugin.json manifests, and the walk-up probes. The output has no time-dependent fields.
  • Cache key: it covers every environment variable fleet-state.sh reads. CLAUDE_PLUGIN_ROOT is exported with a default before the key is built (L253), so set -u cannot trip on it. The script never runs cd outside a subshell, so $PWD in the key stays valid.
  • EXIT trap: no other EXIT trap exists in fleet-state.sh, hook-utils.sh or jq-capture.sh, so the new one replaces nothing.
  • fleet_read: dropping its mp/out locals is safe. mp_error takes only its message, and fleet_snapshot declares its own locals.
  • Docs: the sync.md text and the changelog fragment (bump: minor, which fits a new env-var knob) match the code. The changelog's read counts say they were measured with a stub CLI.

Not verified

  • Author's claims: the PR says the tests pass, shellcheck is clean, and gives benchmark numbers. I ran nothing, so these are unverified.
  • BSD stat path: as the PR says, nothing in this repo tests it. pr-test-platforms.yml runs no macOS job for sync-run.test.sh.

Other notes

  • No trusted-path globs match the changed files.
  • The PR only references perf(harness-ops): reduce plugins sync's remaining wall clock and digest size #12539 and closes no issue, so there are no closing-issue criteria to check.
  • No .claude/rules sentence is broken. The two rules that match (skill-bodies-state-current-rules.md, cost-claims.md) are not violated by the sync.md text.
  • No plugin manifest changed.
  • There are no prior findings, so the round cap does not apply.
  • I found no embedded instructions in the PR context.
  • The change adds no spend.

@kyle-sexton
kyle-sexton added this pull request to the merge queue Nov 3, 2026
Merged via the queue into main with commit 7cbaf65 Nov 3, 2026
69 of 83 checks passed
@kyle-sexton
kyle-sexton deleted the perf/12539-fleet-state-reuse branch November 3, 2026 23:18
@github-actions

github-actions Bot commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

Post-merge mutation testing

Killed 5, survived 2, timeout 0, no coverage 0, invalid 0, not run 0.
Covered-code score 71%; mutation score 71%.

File Killed Survived Timeout No coverage Invalid Not run
plugins/harness-ops/skills/plugins/scripts/fleet-state.sh 2 1 0 0 0 0
plugins/harness-ops/skills/plugins/scripts/sync-run.sh 3 1 0 0 0 0

Surviving mutants

Location Operator Disposition Cause
plugins/harness-ops/skills/plugins/scripts/fleet-state.sh:1052 SBR productive input-gap
plugins/harness-ops/skills/plugins/scripts/sync-run.sh:1809 SBR productive input-gap

The run's summary has the full report.

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