Skip to content

perf(knowledge): capture wakes on the chat's own writes, not a per-chat 1 s ticker - #2048

Closed
AbirAbbas wants to merge 3 commits into
perf/knowledge-enabled-cachefrom
perf/knowledge-capture-on-write
Closed

AbirAbbas wants to merge 3 commits into
perf/knowledge-enabled-cachefrom
perf/knowledge-capture-on-write

Conversation

@AbirAbbas

Copy link
Copy Markdown
Collaborator

Stacked on #2042 (same package). Base branch: perf/knowledge-enabled-cache. After #2042 squash-merges, this rebases onto app/v3 and drops #2042's commits.

What and why

#2042 stopped each idle chat's knowledge-capture tick from opening SQLite. The tick still woke every second per live chat and opened, read and closed the chat's journal (a 64 KiB reader each time). With #2042 and #2044 together it was the largest per-chat timer left in an idle engine: 20 live idle chats still cost ~188 interrupt wakeups/s on macOS. The lead's call was event-driven, with no file watch per chat.

What changed

  • An in-process write hook. The journal writer's existing onWrite hook (internal/session/agent.go), which already tells the seal watch, now also wakes capture. No fsnotify/kqueue watch; nothing new on disk.
  • The capture goroutine looks:
    • once when it starts (a resumed chat may already have lines waiting);
    • on each wake;
    • once more knowledgeCatchUp (1 s, the old cadence) after a write, and after any look that moved the cursor, so a backlog still moves one batch a second;
    • on a knowledgeSafetyTick (5 s) for lines another process writes to the same journal (a session host, a terminal) and for consent changed elsewhere.
  • A look a write woke uses a consent answer at most a second old (Vault.CaptureConsentWithin). The answer is shared per process, so a turn written just after consent changed elsewhere is captured on the trailing look, and writing chats open the database at most once a second between them. Idle chats never open it.
  • Admission is unchanged (perf(knowledge): idle chats stop opening SQLite every second #2042's ConfirmConsent before sources are admitted or passed over), as are the final bounded scan on close and the process-wide worker.

Bounds:

  • this process's own write → captured within ~1 s;
  • another process's lines → within knowledgeSafetyTick (5 s);
  • consent changed elsewhere with no write here → within consentRecheck + knowledgeSafetyTick (10 s);
  • while idle, the only timer is the 5 s tick.

Contract tests (internal/session/knowledge_wake_test.go)

  • TestAnIdleChatsCaptureRunsOnlyTheSafetyTick, capture on and off. On perf(knowledge): idle chats stop opening SQLite every second #2042's head: 6 scans in 6 idle seconds (FAIL, bound 2). Here: 1.
  • TestKnowledgeCaptureFollowsTheChatsOwnWrites: both sources of a turn are in the vault within 1 s. Here 0.03 s; 1.27 s on perf(knowledge): idle chats stop opening SQLite every second #2042's head, where it was timing-dependent.
  • TestKnowledgeCapturesAnotherWritersLinesWithinTheSafetyTick: a line appended to the journal by another writer, with no in-process write, is captured within the safety tick.
  • perf(knowledge): idle chats stop opening SQLite every second #2042's tests are unchanged and pass. That includes 20 idle tickers making 2 database opens in 5 s, within one per recheck, and consent enabled elsewhere then a turn, captured on the trailing look. TestKnowledgeKeepsSourcesWhileTheVaultIsGone restores consent elsewhere with no further write, so its deadline is now the documented bound: consentRecheck + knowledgeSafetyTick + 3 s.

Numbers

Pending: s2/s3 before (#2042) / after, same run.

Gates, CI, Codex, manual

Pending.

🤖 Generated with Claude Code

AbirAbbas and others added 3 commits October 29, 2026 19:31
After #2042 an idle chat's capture tick opened no database, but it still
woke every second and opened, read and closed the chat's journal. It was
the largest per-chat timer left in an idle engine.

The journal writer's hook now wakes capture after each line reaches the
file (an in-process signal; no file watch). Each write is followed by one
trailing look a second later, a look that moves the cursor is followed by
another, so a backlog still moves one batch a second, and a five-second
safety tick covers lines another process writes to the same journal and
consent changed elsewhere. A look a write woke uses a consent answer at
most a second old (CaptureConsentWithin), so a turn written just after
consent changed elsewhere is captured on the trailing look.

An idle chat now runs one five-second tick and nothing else.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Review found that a rebind could leave a write without its trailing look:
the retired scanner may consume the wake and answer from a consent view
under a second old, and the replacement's first look moved nothing, so it
armed no trailing look and capture waited for the safety tick.

Every scanner now looks once when it starts and once more a second later
with fresh consent.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@AbirAbbas

Copy link
Copy Markdown
Collaborator Author

Closing unmerged: the Mac push stopped early to save usage. What this fixes and what is left is tracked in #2062. The branch perf/knowledge-capture-on-write is kept.

@AbirAbbas AbirAbbas closed this Oct 29, 2026
AbirAbbas added a commit that referenced this pull request Nov 2, 2026
Co-Authored-By: Claude Opus 5.5 <[email protected]>
AbirAbbas added a commit that referenced this pull request Nov 3, 2026
…at 1 s ticker (#2255)

* knowledge: capture wakes on the chat's own journal writes

After #2042 an idle chat's capture tick opened no database, but it still
woke every second and opened, read and closed the chat's journal. It was
the largest per-chat timer left in an idle engine.

The journal writer's hook now wakes capture after each line reaches the
file (an in-process signal; no file watch). Each write is followed by one
trailing look a second later, a look that moves the cursor is followed by
another, so a backlog still moves one batch a second, and a five-second
safety tick covers lines another process writes to the same journal and
consent changed elsewhere. A look a write woke uses a consent answer at
most a second old (CaptureConsentWithin), so a turn written just after
consent changed elsewhere is captured on the trailing look.

An idle chat now runs one five-second tick and nothing else.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* docs: change entry for #2048

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* knowledge: a new capture scanner always takes one trailing look

Review found that a rebind could leave a write without its trailing look:
the retired scanner may consume the wake and answer from a consent view
under a second old, and the replacement's first look moved nothing, so it
armed no trailing look and capture waited for the safety tick.

Every scanner now looks once when it starts and once more a second later
with fresh consent.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
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