Skip to content

perf(protocol): page complete turns and bot history from the tail - #301

Merged
nixuuu merged 1 commit into
perf/tail-first-1from
perf/tail-first-2
Nov 6, 2026
Merged

nixuuu merged 1 commit into
perf/tail-first-1from
perf/tail-first-2

Conversation

@nixuuu

@nixuuu nixuuu commented Nov 6, 2026 •

Copy link
Copy Markdown
Member

Uncached thread, bot chat and exchange reads can now start at the tail. Additive protobuf fields request a page and return a stable older-page boundary, continuation flag and reset metadata; generated code is included.

Thread pages contain complete turns, including inherited fork history, work segments and footers. Bot/exchange pages use immutable message sequence boundaries. Current approvals, queue, goal and header configuration remain complete. Existing journal cursors resume deltas; the loaded-window reducers ignore edits outside the window until that older page is read. Older-page merges preserve newer live rows and replay from the pre-read checkpoint so concurrent edits cannot be lost.

Online linked-computer reads pass the client's cursor/page request to the owner-scoped home. The top stage also pages the offline mirror. Fork/moved-history changes, missing boundaries and journal gaps reset safely. Older clients request page size zero and receive full history; older gateways ignore the added fields and continue returning full snapshots. No link-version bump or migration is needed.

Measurements

Same real gateway/Postgres fixture: 1,500 completed turns, one input and one answer per turn, approximately 2 KiB of answer/tool-like text, 3,000 messages total. Client stage defaults are 24 turns / 50 bot messages.

Initial thread response Full history Tail page
Gateway protobuf bytes 3,559,707 57,453
Snapshot build, Android measurement 58.8 ms 15.1 ms
Web first content, warmed development code 1,390.6 ms 76.1 ms
Web SSE data bytes 3,906,105 63,625
Android first content 1,771.4 ms 518.4 ms

These are single loopback development measurements, not a production percentile benchmark. Web SSE JSON bytes exclude framing and differ from mobile protobuf bytes. Cold route compilation was slower (1,541.6 ms after); warm code is used for the web comparison. Client integration and device evidence belong to the top stage.

Verification

Whole-turn pages intentionally do not split one unusually large turn. Target-item navigation may fetch more than one normal page to retain a contiguous loaded window.

@nixuuu
nixuuu added this pull request to stack #303 November 6, 2026 20:28
@nixuuu
nixuuu marked this pull request as ready for review November 6, 2026 20:38
@nixuuu
nixuuu force-pushed the perf/tail-first-2 branch from 9ea1dd0 to 9000462 Compare November 6, 2026 21:04
@nixuuu
nixuuu merged commit c2a1960 into dev Nov 6, 2026
10 checks passed
@nixuuu
nixuuu deleted the perf/tail-first-2 branch November 6, 2026 21:09
nixuuu added a commit that referenced this pull request Nov 9, 2026
Overview, sidebar and watch paths repeatedly read large
settings/configuration or whole turn/journal collections to obtain small
metadata. This layer stores metadata projections, bounds page and turn
reads, batches scratch workspace reconciliation and avoids unrelated
watch refreshes.

## Cost

| Path | Before | After | How measured |
| --- | --- | --- | --- |
| Scratch overview, 101 workers | 106 SQL statements on an unchanged
listing | 6; only missing/changed workspaces write | Real SQL spans and
owner/root-change assertions |
| Worker profile-name lookup | 20,971,600-byte saved config | 23-byte
stored name map | Synthetic large config, actual JSON sizes; listing
preserves names |
| Thread list/header/watch metadata | Up to 40 MiB of instructions/title
context in the fixture | Stored UI settings only | Oversized fields plus
successful list, header, broadcast and paged snapshot reads |
| Activity timestamp | Scan all turns per listed thread | One indexed
row | 10,000-turn fixture, `EXPLAIN ANALYZE` |
| History page IDs | 10,000 IDs / 360,000 ID bytes | 24 IDs / 864 ID
bytes | Real PostgreSQL window query; fork/target/from/reset tests |
| Turn broadcast/enqueue confirmation | Read every turn and input, then
find one | Read the requested turn only | 10,000-turn fixture with an
unrelated 20 MiB input; real broadcast |
| Moved history version | Hash 8 MiB on every snapshot | Read a stored
32-byte hash | Generated-column backfill/update checks and query plan |
| Mirror journal | Transfer 1,000 repeated changes to JS; unrelated
changes copied per watch | One distinct sync key; zero unrelated
per-thread delta keys | Real journal fixture, continuity/gap checks |
| Watch heartbeat | Any owner revision could reopen every watch |
Relevant thread/fork/shared metadata only |
Unrelated/relevant/pruned-journal regression; linked liveness suite |

Migration 0098 adds stored thread settings, worker profile names and
moved-history hashes, plus activity and mutable-source indexes.
PostgreSQL backfills them and maintains them on every write. Before
building indexes it validates deferred constraint events from earlier
data repairs in the same upgrade transaction; the migration-0096 upgrade
fixture covers this path. Metadata polling no longer detoasts the full
saved configuration. Workspace grouping is linear. Update snapshots
project status fields and omit install-command decoding. Mirror
publishers coalesce keys in SQL and leave retention to the existing
commit trigger, removing a redundant DELETE every 250 ms per subscriber.

## Audit boundaries

- Existing `PeerReads`, workspace-origin caches and update-center
single-flight work are already bounded; their freshness and invalidation
behavior is retained.
- Complete thread indexes still return the required metadata rows; no
silent truncation changes list semantics. Normal history pages cap at
100 turns, while explicit target/from requests intentionally expand a
window.
- A cold durable mirror reset and applying a content delta still need
complete content. They remain distinct from metadata-only reads. Very
large direct snapshot reads retain the SQL driver's wire-message limit;
this layer does not claim to remove every content serialization cost.

Focused PostgreSQL cost, paging, upgrade and linked-liveness checks
pass. Top-of-stack validation passed: `bun install`, `bun run
gen:check`, `bun run typecheck`, `bun run lint`, `bun run format:check`,
`bun run check:tmp`, and the web production build. The full test run
finished with **1,904 passed, 161 skipped, 0 failed across 339 files**
(982.19 s), with nothing left in the private disk-backed temp directory.
Multi-gateway files ran sequentially. All three stack heads have green
CI; the stack is ready for review and has not been merged.

Copy audit §8: no UI text changes.

Related: #301
Related: #386
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