Skip to content

Background work costs less: each pane read once a tick, the cached sessions drawn before the tools are found, a GitHub sync with nothing new redraws nothing, telemetry costs kept as running totals - #289

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

Conversation

@Mpdreamz

Copy link
Copy Markdown
Contributor

What heed does while you work now costs a fraction of what it did, and
launch draws your sessions sooner. What you see, and when, is the same.

  • Screen reading. The once-a-second tick reads each pane at most once
    (a run's, review's or worker's pane and a session's agent pane were read
    up to three times), and only after the cheap checks say it is needed. Each
    agent's patterns are compiled once (ScreenGuess.Compiled, again only when
    [agents.<name>.patterns] change) instead of ~11 regexes per pane per
    tick. A pane whose screen and agent did not change since the last tick
    keeps its last guess without matching anything (ScreenGuesser): a guess
    depends only on the screen and the patterns. The stuck signal uses the
    same compiled patterns. Screen reading still runs while the window is
    hidden: notifications, the Dock and menu bar counts, run, review and
    worker endings, quiet turns and Hunch all depend on it then.
  • Startup. The cached domain is read while the call log opens and is
    drawn as soon as config.toml is read, before tool detection (login shell,
    Tailscale, zmx check), the LLM spend seed and telemetry. A pane drawn
    before the search path, zmx and zmx's live sessions are known waits in its
    host for its terminal (TerminalSurfaces.holdPanes/releasePanes), so a
    pane still reattaches to its zmx session and gets the detected PATH.
    Telemetry opens while tools are detected. Today's model spend is read
    from the call log off the launch path; the runner's first call that gets
    a slot, and spentToday, wait for it, so the daily cap still counts it
    (LLMRunner.seedSpend(loading:)). Everything after the panes start runs
    in the order it did. New signpost start.toPanes.
  • GitHub sync. A sync changes syncedAt, each source's fetch time and
    the team-mention bookkeeping every time, so it reassigned github (a
    sidebar rebuild and every GitHub notice in commit) every poll. These are
    now the sync's stamp (GitHubSyncStamp). A sync whose data equals what is
    there (GitHubState.sameData) only updates githubStamp and saves the
    full state to the store as before; the next sync starts from the newest
    stamp. "synced … ago" and Attention's coverage read the newest stamp
    (githubNow, freshened(by:)).
  • Telemetry costs. Each export with a cost re-read up to 200k spans
    and re-summed every session. TelemetryStore now reads the store once,
    then keeps what costs depend on (model spans grouped by request, cost
    samples, the latest of each cumulative series) indexed by whose they are
    (TelemetryCosts), and sums only the touched sessions' own records, with
    the context of now. A prune that removed rows makes it read again. The
    sessions it raises are committed in one commit.

Benches (mise run bench, BackgroundBench; this machine was under a
load average of ~170, so the absolute numbers are high):

  • Guessing a pane: 1042 µs compiling each time, 679 µs compiled once, 0.0
    µs for an unchanged screen.
  • Costs per telemetry batch over 29k spans: 1951 ms reading everything,
    11.3 ms with running totals.

Verification: mise run lint passes; mise run test passes. New tests:
compiled guesses give the expected states, an agent's patterns compile
once until they change, an unchanged screen keeps its guess, a sync with
nothing new has the same data, the newer stamp wins, running totals equal
a full read after every batch (three seeds, the agents' own OTLP fixtures,
an agent session learned later) with one read of the store, a prune that
removed rows reads again, and a call made while the spend is read waits
for it and is capped. Mutations applied and reverted, each failing its
test: no compile cache, a remembered guess that ignores the screen,
sameData as plain equality, inserts not added to the totals, the first
call not waiting for the spend, prunes not invalidating, no span-over-event
preference, no since filter, agent sessions not mapped.

Not covered by tests: the AppModel wiring (no app test target): the
per-tick read cache in readScreens, holding panes at launch, the GitHub
stamp path in syncGitHub, batching the cost commits.

Not run: mise run tutor-check (asked to open the PR without waiting for it; the machine was at a load average of ~170).

🤖 Generated with Claude Code

…ssions drawn before the tools are found, a GitHub sync with nothing new redraws nothing, telemetry costs kept as running totals

What heed does while you work now costs a fraction of what it did, and
launch draws your sessions sooner. What you see, and when, is the same.

- **Screen reading.** The once-a-second tick reads each pane at most once
(a run's, review's or worker's pane and a session's agent pane were read
up to three times), and only after the cheap checks say it is needed. Each
agent's patterns are compiled once (`ScreenGuess.Compiled`, again only when
`[agents.<name>.patterns]` change) instead of ~11 regexes per pane per
tick. A pane whose screen and agent did not change since the last tick
keeps its last guess without matching anything (`ScreenGuesser`): a guess
depends only on the screen and the patterns. The stuck signal uses the
same compiled patterns. Screen reading still runs while the window is
hidden: notifications, the Dock and menu bar counts, run, review and
worker endings, quiet turns and Hunch all depend on it then.
- **Startup.** The cached domain is read while the call log opens and is
drawn as soon as config.toml is read, before tool detection (login shell,
Tailscale, zmx check), the LLM spend seed and telemetry. A pane drawn
before the search path, zmx and zmx's live sessions are known waits in its
host for its terminal (`TerminalSurfaces.holdPanes`/`releasePanes`), so a
pane still reattaches to its zmx session and gets the detected PATH.
Telemetry opens while tools are detected. Today's model spend is read
from the call log off the launch path; the runner's first call that gets
a slot, and `spentToday`, wait for it, so the daily cap still counts it
(`LLMRunner.seedSpend(loading:)`). Everything after the panes start runs
in the order it did. New signpost `start.toPanes`.
- **GitHub sync.** A sync changes `syncedAt`, each source's fetch time and
the team-mention bookkeeping every time, so it reassigned `github` (a
sidebar rebuild and every GitHub notice in `commit`) every poll. These are
now the sync's stamp (`GitHubSyncStamp`). A sync whose data equals what is
there (`GitHubState.sameData`) only updates `githubStamp` and saves the
full state to the store as before; the next sync starts from the newest
stamp. "synced … ago" and Attention's coverage read the newest stamp
(`githubNow`, `freshened(by:)`).
- **Telemetry costs.** Each export with a cost re-read up to 200k spans
and re-summed every session. `TelemetryStore` now reads the store once,
then keeps what costs depend on (model spans grouped by request, cost
samples, the latest of each cumulative series) indexed by whose they are
(`TelemetryCosts`), and sums only the touched sessions' own records, with
the context of now. A prune that removed rows makes it read again. The
sessions it raises are committed in one `commit`.

Benches (`mise run bench`, `BackgroundBench`; this machine was under a
load average of ~170, so the absolute numbers are high):
- Guessing a pane: 1042 µs compiling each time, 679 µs compiled once, 0.0
µs for an unchanged screen.
- Costs per telemetry batch over 29k spans: 1951 ms reading everything,
11.3 ms with running totals.

Verification: `mise run lint` passes; `mise run test` passes. New tests:
compiled guesses give the expected states, an agent's patterns compile
once until they change, an unchanged screen keeps its guess, a sync with
nothing new has the same data, the newer stamp wins, running totals equal
a full read after every batch (three seeds, the agents' own OTLP fixtures,
an agent session learned later) with one read of the store, a prune that
removed rows reads again, and a call made while the spend is read waits
for it and is capped. Mutations applied and reverted, each failing its
test: no compile cache, a remembered guess that ignores the screen,
`sameData` as plain equality, inserts not added to the totals, the first
call not waiting for the spend, prunes not invalidating, no span-over-event
preference, no `since` filter, agent sessions not mapped.

Not covered by tests: the AppModel wiring (no app test target): the
per-tick read cache in `readScreens`, holding panes at launch, the GitHub
stamp path in `syncGitHub`, batching the cost commits.

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 2cdafd1 into main Nov 16, 2026
5 checks passed
@Mpdreamz
Mpdreamz deleted the fix/perf-background 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