Skip to content

Audit, other people's text and the origin hook stop redoing work: rows kept, text stripped once, the book read only for GitHub calls - #291

Merged
Mpdreamz merged 1 commit into
mainfrom
fix/perf-audit
Nov 16, 2026
Merged

Mpdreamz merged 1 commit into
mainfrom
fix/perf-audit

Conversation

@Mpdreamz

Copy link
Copy Markdown
Contributor

Part (c) of the code-health performance work: Audit, ExternalText, Prompt.defuse and the origin hook stop redoing work they had already done. The outputs are the same; only what gets recomputed changes. This PR merges on its own and does not touch the hook path, the sidebar, Attention, screen reading, startup, sync or telemetry.

What changed

  • Audit keeps its rows (finding 8). AuditBook stores its rows. An action recorded is inserted by binary search, after equal ones, which is where the stable sort put it, and only its own row is built. All rows are rebuilt only when a cause that some chain reads changes, including a cause a chain stopped at because it was missing. count(since:) is a binary search and outsideKeys is stored. == and hash are still over the records only. apply now returns what changed. AuditBrowser uses it to insert a new row into the filtered list when / keeps it, and to recompute grant rows only when grants, revocations or their causes changed. The section toggle reads a stored standingGrants instead of calling book.grantRows(before:) in a view body.
  • ExternalText.display is stored (finding 9). It is stripped once in init, and shares the raw string's storage when stripping changes nothing. ==, hash(into:) and the encoding are still over the raw text, source and author.
  • Prompt.defuse uses a pattern compiled once (finding 9).
  • The origin hook checks the call's text before reading the book (finding 10). OriginNote.mayBringGitHub returns false only when the parser could find nothing:
    • Bash: true when the command contains gh or git once quotes and backslashes are dropped. These are the only characters the tokenizer removes, so 'g'h and g\h still pass, and every github URL contains git.
    • WebFetch: true when the URL contains github once percent escapes are decoded, case-insensitively, and always true for a URL with non-ASCII characters or escapes that do not decode.
  • OriginKeeper writes in order and coalesces (finding 10). Snapshots go through submit into a Latest slot, numbered on the main actor in the order they are taken. A single drain applies the newest one, and anything older than what was already applied is dropped. Before this, each snapshot went through its own Task.detached, so an older snapshot could overwrite a newer one.
  • Checkouts capped (finding 10) at OriginBook.checkoutLimit = 500, the size of the existing full-book fixture. Checkouts carry no date, so "oldest" cannot be known. The first ones listed are kept (repos, then worktrees) and the rest are dropped.
  • The audit.refilter signpost from Signposts on the app's hot paths, HeedCore benchmarks, and a bench that builds only what it runs #282 stays; audit.follow covers the incremental path.

Benches (mise run bench)

Run on origin/main (93a656c) with AuditBench.swift copied in, then on this branch. The machine had a load average of about 240 on 8 cores and the load varied between the two runs. The unchanged branch name bench took 1036 ms on main and 299 ms on the branch, so on the base run everything was roughly 3.5× slower. Read the ratios against that.

bench main this branch
audit record: one call into 5,000 actions, then rows + grant rows (AuditBench) 145 ms 0.074 ms
origin note, ls -la && swift build, full book on disk (AuditBench) 188 ms 0.002 ms
ExternalText.display, 20 KB × 100 (CoreBench) 679 ms 2.6 ms
Prompt.text, 10 fences (CoreBench) 0.325 ms 0.131 ms
AuditBook.apply + rows, 50k actions in one go (CoreBench) 1190 ms 321 ms (about the same after adjusting for load; the bulk path still sorts and builds every row)
origin note, GitHub call (HookBench) 133 ms 44 ms (unchanged path; difference is load)
branch name, 200k word (unchanged, load reference) 1036 ms 299 ms

Verification

  • New tests:
    • AuditBookRowsTests checks:
      • one action recorded builds one row (rowsBuilt);
      • rows recorded one at a time equal a stable sort of the records, including a same-time duplicate id;
      • a cause that arrives late rebuilds the rows, and an unrelated or identical cause builds none;
      • which changes flag grants;
      • equality and hash;
      • count(since:) against a linear count.
    • ExternalTextTests checks display, equality, hash and encoding.
    • OriginNoteTests:
      • ls and a non-GitHub URL read the book 0 times (also added to theHookReadsTheBookOnceAndItStaysSmall);
      • every command and URL in a set of 16 with a subject passes the text check;
      • the checkout cap;
      • the Latest slot;
      • an older snapshot never overwrites a newer one.
  • Mutation checks (each made to fail the new tests, then reverted):
    • stale-cause detection off;
    • count(since:) with >;
    • quote stripping removed from the text check;
    • URL check case-sensitive;
    • cap removed;
    • generation guard removed;
    • drain never ending;
    • == on display instead of raw;
    • always rebuild;
    • ties inserted before equal rows. This one at first passed: the duplicate was identical, and whole also took the insertion path. The test now compares against an explicit stable sort.
  • Full mise run test passed (2597 tests) on the branch rebased onto 93a656c. After the final rebase onto 661d7f4, the build and the touched suites (92 tests in 10 suites) passed; the full suite was not run again, as asked. mise run lint and mise run text-check passed before the final rebase.

Not done or untested

  • mise run tutor-check was not run: it bundles the app, and the machine was too loaded. AuditBrowser's incremental insert and cursor shift are app-layer code with no unit test. They reuse the tested AuditBook.insertionIndex(of:in:).
  • AuditView still maps rows.map(\.id) for ListBody in its body; I left that alone.

🤖 Generated with Claude Code

…s kept, text stripped once, the book read only for GitHub calls

Each path did the same work again on every call or redraw. The outputs are
the same; only what is recomputed changes.

- **Audit keeps its rows.** `AuditBook` stores its rows next to the
actions. An action recorded is inserted at its place by binary search
(after equal ones, as the stable sort put it) and builds only its own
row. The rows are built again only when a cause that a row's or a grant's
chain reads changes, including a cause that a chain stopped at for want
of it. `count(since:)` is a binary search and `outsideKeys` is kept.
Equality and hashing stay over the records. `apply` says what changed,
and `AuditBrowser` inserts a new row into the filtered list when `/`
keeps it, instead of filtering every row again. It recomputes grant rows
only when a grant, a revocation or a cause they read changed. The section
toggle reads a stored count of standing grants instead of building grant
rows in the view body.
- **`ExternalText.display` is stored.** It is stripped once in `init` and
shares the raw string's storage when stripping changes nothing. `==`,
`hash(into:)` and the encoding stay those of the text as written.
- **`Prompt.defuse` compiles its pattern once**, not once per fence.
- **The origin hook reads the book only for calls that may be about
GitHub.** `OriginNote.mayBringGitHub` checks a Bash command for `gh` or
`git` once quotes and backslashes are dropped (so `'g'h` and `g\h` still
count), and a WebFetch URL for `github` once percent escapes are decoded.
Any non-ASCII URL counts as a GitHub URL. The check is conservative: a call
the parser would report always passes it. `ls` reads the book 0 times.
- **The origin book's writes are ordered and coalesced.** `OriginKeeper`
used to get each snapshot through its own detached task, so an older
snapshot could land after a newer one. Snapshots are now `submit`ted into
a `Latest` slot, numbered in the order the main actor takes them. One
drain applies the newest. A snapshot older than the one applied is
dropped.
- **Checkouts are capped** at `OriginBook.checkoutLimit` (500). The first
ones listed are kept: repos, then worktrees.

Tests: `AuditBookRowsTests` (rows built one at a time, rows recorded one
by one against a stable sort including a same-time duplicate, a late
cause rebuilding, grant changes, equality and hash). Also added: a
display/equality/encoding test in `ExternalTextTests`, and prefilter, cap,
`Latest` and keeper-ordering tests in `OriginNoteTests`.
`theHookReadsTheBookOnceAndItStaysSmall` now also checks that `ls` reads
the book 0 times. Benches: `AuditBench` (one call recorded into 5,000
actions; a non-GitHub hook call with a full book), next to `CoreBench`'s.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions github-actions Bot added the bug Something isn't working label Nov 16, 2026
@Mpdreamz
Mpdreamz merged commit be8a0f2 into main Nov 16, 2026
5 checks passed
@Mpdreamz
Mpdreamz deleted the fix/perf-audit branch November 16, 2026 20:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant