Skip to content

Seek to the reply a prompt books, instead of reading its session again - #19741

Open
rjlee wants to merge 1 commit into
omacom:quattrofrom
rjlee:agents-panel-index-seek
Open

rjlee wants to merge 1 commit into
omacom:quattrofrom
rjlee:agents-panel-index-seek

Conversation

@rjlee

@rjlee rjlee commented Nov 22, 2026 •

Copy link
Copy Markdown

What changes

Rebased onto the agents-panel rework.

scan_opencode pairs each typed prompt with the first reply after it. Its
condition was an OR over the ordering column, and SQLite can't turn that
into an index range:

CORRELATED SCALAR SUBQUERY 1
`--SEARCH a USING INDEX session_message_session_seq_idx (session_id=?)

Only session_id is constrained, so each prompt read forward through every
remaining row of its session, and booking was quadratic in the session's
length.

Long sessions are ordinary. The opencode.db this was found on is 3.3 GB,
and its largest session holds 51,560 rows (5,884 prompts, 45,373 replies)
of a 1.2 GB session_message table. About a dozen collectors read that store
on each refresh, so one Agents panel refresh pinned twelve cores for minutes
and left 35 processes behind.

Adding the lower bound the OR already implies lets the index seek from the
prompt's own position:

        for session_id, reply_id, created_at, typed_at in conn.execute(
          "SELECT u.session_id, (SELECT a.id FROM session_message a WHERE a.session_id = u.session_id AND a.type = 'assistant'"
+         f" AND a.{order} >= u.{order}"
          f" AND (a.{order} > u.{order} OR (a.{order} = u.{order} AND a.id > u.id)) ORDER BY a.{order}, a.id LIMIT 1),"
CORRELATED SCALAR SUBQUERY 1
`--SEARCH a USING INDEX session_message_session_seq_idx (session_id=? AND seq>?)

The pairs booked are unchanged — only the work to find them changes.

Verification

On that 3.3 GB database:

before after
the booking query 159 s 1.1 s
one collector's scan 201 s 7 s
omarchy agent usage-update --force minutes on every core 11 s

The reorg (#19904) moved the per-agent scanner tests into the shared
collector suite, so the regression test lives in
agent-usage-shared-test.sh, where the opencode tests already do. It builds
a 1500-prompt session with opencode's own index on (session_id, seq), and
counts the SQLite VM work through set_progress_handler, so the check doesn't
depend on machine speed:

ok - OpenCode books each prompt of a long session to the reply after it
ok - OpenCode's reply lookup seeks from the prompt instead of reading its session again

It fails on the old query (18.1M steps) and passes on the new one (100k steps,
threshold 1M). The shared, partition, update, and agents-panel tests all pass.

Screenshots

No visible change: the panel renders the same records, only the scan behind
them is faster.

@greptile-apps

greptile-apps Bot commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no actionable issues were found.

Summary

Speeds up OpenCode’s first-reply lookup without changing which reply each prompt counts toward.

  • OpenCode finds each prompt’s next reply by seeking from its position.

Reviews (3) · Last reviewed commit: "Seek to the reply a prompt books, instea..." · Reviewed by Greptile

@omarchybot

Copy link
Copy Markdown
Collaborator

I read the diff, the surrounding OpenCode scanner, and the base branch’s contributor rules at ab59a447df70a4e4fd44da5f25621c65e7f2c0ef. My classification is a bug fix, but the configured classifier, deepseek-v4p1-flash, returned no usable classification, so classification remains unsettled.

GPT-6 in Codex (reasoning effort not exposed) was the only model to review the code in this session. No code-review second opinion or tests ran, and the unused disposable worker was destroyed. No code or labels changed. Verification is waiting on classification; this is an incomplete review, with no defect established.

@omarchybot omarchybot added bug Something isn't working verified Omarchy Triage has verified that this issue is ready for final review labels Nov 22, 2026
@omarchybot

Copy link
Copy Markdown
Collaborator

GPT-6 in Codex (effort not exposed) checked this head on a disposable ISO worker. A separate reproduction exercised both seq and timestamp ordering, equal-position ties, multiple replies per prompt, unanswered prompts and NULL positions. It preserved all 2,402 expected per-reply prompt counts while reducing SQLite work from about 19.6 million instructions to 114–115 thousand. Removing the predicate restored the slow plan and failed the work budget.

The CLI suite and all 44 agent-usage test files passed. Across the full run and retries with the worker's installed mise tools and UTF-8 locale, the 437-file shell suite completed without remaining failures; six files retained skipped hardware, optional-tool, locale or translation checks. Opus 5.5 High and muse-wonder via pi (thinking high) found no current correctness defect and agreed with the query analysis; independence is not guaranteed. No code changes were needed.

Verification is recorded, but GitHub's Tests and Check translation structure workflows are waiting for maintainer approval. That approval and successful CI are needed before this can leave verification for the maintainer's merge decision.

The scan pairs each typed prompt with the first reply after it. Its
condition was an OR over the ordering column, which SQLite cannot turn
into an index range: the search stayed constrained by session_id alone,
so every prompt read forward through all the rows left in its session.
Booking was therefore quadratic in the session's length, and every
collector that reads opencode's store paid it again on each refresh.

Adding the lower bound the OR already implies lets the index seek from
the prompt's own position. The pairs booked are unchanged; only the work
to find them changes.

On a 3.3 GB opencode.db whose largest session holds 51,560 rows, the
query drops from 159 s to 1.1 s, and a full usage refresh from minutes
on every core to about ten seconds.

Rebased onto the agents-panel rework: the booking query now lives at its
relocated site in scan_opencode, and the regression test now lives in the
shared collector suite, where the per-agent scanner tests consolidated.
It counts SQLite VM work through set_progress_handler: 100k steps with
the seek, 18.1M with the walk.
@rjlee
rjlee force-pushed the agents-panel-index-seek branch from ab59a44 to 6e57f87 Compare November 24, 2026 10:56
@omarchybot omarchybot added the verified Omarchy Triage has verified that this issue is ready for final review label Nov 24, 2026
@omarchybot

Copy link
Copy Markdown
Collaborator

GPT-6 in Codex (effort not exposed) checked the rebased head 6e57f87bc33aa214aa205ab6f52410d0dffbe8b8 on a disposable ISO worker. The separate reproduction still preserves all 2,402 expected reply-to-prompt assignments on both seq and timestamp schemas, including ties and NULL positions. SQLite work falls from about 19.6 million instructions to 114–115 thousand; removing the predicate restores the slow query plan.

The CLI suite passed. Of 255 shell test files, 254 completed without failures, including the shared collector and provider-partition tests; three files retained locale, translation or battery-dependent skips. remote-session-service-test.sh failed its capture-owner assertion in the full run and once alone on head; the isolated base run passed. Those service and test files are unchanged here, but the failure was not reproduced on base, so the full suite is recorded as incomplete.

Opus 5.5 High and muse-wonder via pi (thinking high) found no actionable query or regression-test defect and agreed with the analysis; independence is not guaranteed. No code changes were needed, and the worker was destroyed. Verification of the query is recorded. The remaining blockers are the full-suite failure and maintainer approval for GitHub’s Tests and Check translation structure workflows, followed by successful CI.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working verified Omarchy Triage has verified that this issue is ready for final review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants