Skip to content

Dedupe redundant per-tick ContentIdle reads + gate the outer task-list fetch on DB change - #949

Merged
anutron merged 1 commit into
masterfrom
argus/tick-cost-followup
Aug 23, 2026
Merged

Dedupe redundant per-tick ContentIdle reads + gate the outer task-list fetch on DB change#949
anutron merged 1 commit into
masterfrom
argus/tick-cost-followup

Conversation

@anutron

@anutron anutron commented Aug 22, 2026

Copy link
Copy Markdown
Collaborator

Two follow-up CPU optimizations to the TUI's 1s onTick, companion to #944 (ce74221f).

1. agent.ContentIdle reuses detectNeedsInputSticky's already-computed signal

ContentIdle (BUG-036) ran right after detectNeedsInputSticky every tick but independently re-read each session's tail and re-ran ContentIdleFingerprint/ParkedSelectionSignal VT emulation — even though detectNeedsInputSticky's resumed-activity and content-stability passes had just computed the identical values for the identical tail moments earlier in the same tick.

ContentIdle gained a trailing cachedSignal func(id string) (ContentIdleSignal, bool) parameter: a cache hit skips the tail read + VT emulation entirely; ok=false (including a nil cachedSignal) falls back to the exact pre-existing independent computation, byte-for-byte. internal/api/push.go's daemon-side idle watcher (which has no equivalent shared per-tick pass) passes nil and is untouched. The TUI wires App.contentIdleSignalOf, sourced from a.needsInputRawSignals (the dirty-check cache from #944), which now also threads through the fp that pass was already computing and discarding.

Safe because it's the literal same function call, not an approximation — ScreenRenderer.render's RIS-reset-per-call idempotency (already relied on by #944) guarantees identical output for identical (tail, cols, rows).

2. refreshTasksWithIDs's outer a.db.Tasks() fetch is gated on DB change

Mirrors hera.HeraPage's existing shouldRebuild/markRebuilt/InvalidateChangeGate PRAGMA data_version pattern, applied to the plain task-list fetch — but only on the periodic onTick path (new allowCachedTasks param). Every other caller (refreshTasksLocal, refreshTasksAsync, a direct bulk-prune call) keeps unconditional-fetch, since db.DB.DataVersion's documented same-connection blind spot means the gate can never be trusted to see a write made by the very call that would also be consulting it.

A code review of the first pass at this caught that it only handled the one write site named in scope (reconciliation's own SetStatus) and missed four more: several hera mutations funneled through heraRefresh() (nuke/cascade-nuke/step-status), startSession (called from 5 sites, only 2 of which separately refresh), switchSession's live-session branch, and the async session-ID recapture path in handleSessionExitUI. All five now call invalidateTasksChangeGate() at their write site; each has a dedicated regression test that fails when its invalidate call is removed (verified by hand before writing this up).

One accepted, named, bounded gap remains: internal/agent/autorename.go's background rename goroutine has no App reference to invalidate through — cosmetic-only (a suggested rename lags until an unrelated write), documented in context/knowledge/gotchas/events.md.

Testing

  • New differential/equivalence tests for both fixes (TestContentIdle_CachedSignal, TestRefreshTasksWithIDs_GatedFetch, plus 4 regression tests pinning each newly-found write site) — each regression test verified to fail when its fix is reverted.
  • make pre-pr green except make vuln (pre-existing stdlib CVEs, continue-on-error in CI — not introduced by this PR).
  • Full -race suite green; coverage 88.7% (floor 88).

Specs: No behavioral changes (internal perf/caching only, no observable behavior change) — no openspec/ change folder, matching precedent set by #944.

🤖 Generated with Claude Code

@anutron
anutron force-pushed the argus/tick-cost-followup branch from e999c69 to 737a80c Compare August 23, 2026 03:58
…t fetch on DB change

Two follow-up CPU optimizations to the 1s TUI tick (companion to #944):

1. agent.ContentIdle gained a cachedSignal parameter so it can reuse the
   ContentIdleFingerprint/ParkedSelectionSignal reading detectNeedsInputSticky
   already computed for the same session tail moments earlier in the same
   tick, instead of paying for a second tail read + VT re-emulation pass.
   Nil-safe (push.go's daemon-side watcher is unaffected).

2. refreshTasksWithIDs's outer a.db.Tasks() fetch is now gated by a
   PRAGMA data_version dirty-check (mirroring hera.HeraPage's existing
   pattern), but ONLY on the periodic onTick path — every other caller
   still fetches unconditionally, since a write it just made through the
   same connection is invisible to that connection's own next
   DataVersion() read. Closes every write site that needed an explicit
   invalidateTasksChangeGate() call (reconciliation's own SetStatus,
   several hera mutations via heraRefresh, startSession, switchSession's
   live-session branch, and the async session-ID recapture path) —
   found via a code review that caught the initial narrower
   implementation missing four of these five sites.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
@anutron
anutron force-pushed the argus/tick-cost-followup branch from 737a80c to acc19a0 Compare August 23, 2026 04:05
@github-actions

Copy link
Copy Markdown

Merging this branch will increase overall coverage

Impacted Packages Coverage Δ 🤖
github.com/drn/argus/internal/agent 89.94% (+0.04%) 👍
github.com/drn/argus/internal/api 87.00% (ø)
github.com/drn/argus/internal/tui 83.27% (+0.04%) 👍

Coverage by file

Changed files (no unit tests)

Changed File Coverage Δ Total Covered Missed 🤖
github.com/drn/argus/internal/agent/needsinput.go 95.24% (+0.14%) 273 (+8) 260 (+8) 13 👍
github.com/drn/argus/internal/api/push.go 94.88% (ø) 254 241 13
github.com/drn/argus/internal/tui/app.go 78.92% (+0.22%) 2785 (+33) 2198 (+32) 587 (+1) 👍
github.com/drn/argus/internal/tui/heraactions.go 76.21% (+0.04%) 580 (+1) 442 (+1) 138 👍

Please note that the "Total", "Covered", and "Missed" counts above refer to code statements instead of lines of code. The value in brackets refers to the test coverage of that file in the old version of the code.

Changed unit test files

  • github.com/drn/argus/internal/agent/needsinput_test.go
  • github.com/drn/argus/internal/api/push_test.go
  • github.com/drn/argus/internal/tui/app_test.go
  • github.com/drn/argus/internal/tui/smoke_test.go

@anutron
anutron merged commit 39aaff3 into master Aug 23, 2026
1 check passed
@anutron
anutron deleted the argus/tick-cost-followup branch August 23, 2026 04:13
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