Skip to content

Write the Codex async question cursor only when it moves - #8171

Merged
ymichael merged 1 commit into
get-bb:mainfrom
lanjak:perf-codex-async-question-cursor
Nov 25, 2026
Merged

ymichael merged 1 commit into
get-bb:mainfrom
lanjak:perf-codex-async-question-cursor

Conversation

@lanjak

@lanjak lanjak commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Human comments

What was wrong

readNewQuestions in plugins/provider-codex/src/async-questions.ts runs on every experimental_thread.events, which the server fires once per second per thread while the thread receives events (plugin-thread-events.ts). It ended with an unconditional bb.storage.kv.set(cursorKey(threadId), cursor) (async-questions.ts). The cursor moves only when a new thread/extensionState/updated row exists, so almost every call wrote the value it had just read. Plugins run in the server process, and kv.set calls setPluginKvValue synchronously: one INSERT ... ON CONFLICT DO UPDATE on plugin_kv in the main database (plugin-api.ts, plugin-storage.ts). So each streaming Codex thread caused one write transaction per second that changed nothing. I confirmed the call pattern with a test against the unfixed code (two notifications with no new rows give two writes of cursor 0) and measured the per-tick database cost before changing anything.

What changed

  • readNewQuestions keeps the cursor it read and calls kv.set only when the cursor moved forward.
  • The skip cannot change the result. The cursor only grows (Math.max over row sequences), and when it did not move, the skipped write would have stored the value already in plugin_kv. The next run reads the same cursor either way.
  • New test: notifications with no new rows write nothing; a new row gives exactly one write of its sequence, and the next list starts after it.
  • No wire, CLI or protocol changes.

Deviation from the issue's second question: the threads.events.list loopback call stays. The event payload carries only the thread's latest sequence and the list is filtered to extension-state rows, so the plugin cannot know if a new question exists without the call. To skip it, the plugin would have to store the last scanned sequence, which brings back a write on every tick. An in-memory high-water mark would almost never skip, because the event fires only after new events were appended.

How you verified

  • New test "writes the cursor only when a new event row moved it", on the unfixed code:

    AssertionError: expected "set" to not be called at all, but actually been called 2 times
      1st set call: [ "async-question-cursor:thr_async", 0 ]
      2nd set call: [ "async-question-cursor:thr_async", 0 ]
    Tests  1 failed | 5 passed (6)
    

    With the fix: Tests 6 passed (6).

  • Per-tick database work, measured with getPluginKvValue and setPluginKvValue from @bb/db on a file database built with createConnection and migrate (WAL, synchronous = NORMAL, as in production). 50 cursor keys, 20,000 ticks per round, 3 rounds:

    Tick with no new question Statements Row writes per 20,000 ticks us per tick (3 rounds)
    Before 1 select + 1 upsert 20,000 44.59, 40.43, 42.10
    After 1 select 0 15.85, 15.63, 15.90
  • pnpm exec turbo run typecheck test lint lint:type-aware --filter=bb-plugin-provider-codex: 11 of 11 tasks pass, 626 tests in 49 files.

Fixes #8170

AGENT GENERATED

The experimental_thread.events handler ran about once per second per
streaming Codex thread and wrote the unchanged cursor to plugin kv on
every run. Each write is an upsert on the main database. Write the
cursor only when a new event row moved it.
@lanjak lanjak changed the title Write the Codex async question cursor only when a new question event moves it Write the Codex async question cursor only when it moves Nov 25, 2026
@ymichael
ymichael merged commit b56ed04 into get-bb:main Nov 25, 2026
35 checks passed
@lanjak
lanjak deleted the perf-codex-async-question-cursor branch November 25, 2026 22:58
alexburlis added a commit to alexburlis/omnyo that referenced this pull request Nov 26, 2026
Record the upstream PRs, revisions, contributors, and Omnyo integration
commits for merge d2a24ac in docs/upstream-credits.md.

Original-PR: get-bb/bb#7663
Original-Commit: get-bb/bb@1737a03
Original-Author: rufi
Original-PR: get-bb/bb#8055
Original-Commit: get-bb/bb@651ae20
Original-Author: Jake Lancaster
Original-PR: get-bb/bb#8075
Original-Commit: get-bb/bb@511e1ed
Original-Author: Jake Lancaster
Original-Co-Author: Michael Yong
Original-Co-Author: Claude Opus 5.5
Original-PR: get-bb/bb#8096
Original-Commit: get-bb/bb@775090b
Original-Author: Michael Yong
Original-Co-Author: Claude Opus 5.5
Original-PR: get-bb/bb#8145
Original-Commit: get-bb/bb@8de6711
Original-Commit: get-bb/bb@0d3db49
Original-Commit: get-bb/bb@92d3a76
Original-Commit: get-bb/bb@6705422
Original-Author: Michael Yong
Original-Co-Author: Claude Opus 5.5
Original-PR: get-bb/bb#8171
Original-Commit: get-bb/bb@b56ed04
Original-Author: Jake Lancaster
Original-PR: get-bb/bb#8238
Original-Commit: get-bb/bb@2a9a142
Original-Author: SawyerHood
Original-PR: get-bb/bb#8247
Original-Commit: get-bb/bb@ba4ae22
Original-Author: Salem
Original-Co-Author: Claude Opus 5.5
Original-PR: get-bb/bb#8259
Original-Commit: get-bb/bb@96f40a9
Original-Author: SawyerHood
Original-PR: get-bb/bb#8263
Original-Commit: get-bb/bb@141e40a
Original-Author: mk
Original-PR: get-bb/bb#8266
Original-Commit: get-bb/bb@84e20e9
Original-Author: ILIA GOLUBEV
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.

Codex async question handler writes an unchanged kv cursor about once per second per streaming thread

2 participants