Skip to content

perf(mobile): cache thread snapshots across restarts - #273

Merged
nixuuu merged 1 commit into
perf/thread-cache-2from
perf/thread-cache-3
Nov 6, 2026
Merged

nixuuu merged 1 commit into
perf/thread-cache-2from
perf/thread-cache-3

Conversation

@nixuuu

@nixuuu nixuuu commented Nov 6, 2026 •

Copy link
Copy Markdown
Member

Thread switches render the device's latest snapshot before connecting, then resume the watch from its journal checkpoint. The header and composer use real thread/project/model/worker metadata from the copy or overview. Cold reads reuse the generic skeleton components merged in #269. A quiet fixed-space “Syncing…” state keeps cached content visible until live; sending and stopping wait for live, and other actions retain authoritative gateway checks.

The cache uses existing Expo app-private expo-file-system storage at Paths.cache/thread-snapshots-v1, so no new native dependency is needed. The OS may evict it safely. Binary protobuf files use atomic replacement and persistent LRU timestamps. Disk limits are 16 threads / 64 MiB, with 16 MiB maximum per entry; the memory LRU holds 6 threads / 24 MiB. Writes coalesce into 2-second windows and flush on screen exit/background. Size budgets measure encoded protobuf, not the entire decoded object heap.

Pairing device ID and certificate pin isolate cache scopes. Unpair or detected revocation fences new writes, clears memory and serializes disk deletion; orphan scopes are removed before mounting screens. An offline device detects remote revocation upon reconnect. Failed deletion reports an actionable storage error. Snapshot secret fields contain request metadata only, never stored secret values, ciphertext, device tokens or provider credentials. Ordinary conversation text remains private conversation data.

Android development builds disable Expo's network inspector: its HTTP/2 response preview waits for up to 1 MiB, blocking small open-ended Connect streams. Certificate pinning, authorization and wire format remain unchanged. Device verification identified this behavior in both thread deltas and the pre-existing overview watch. The fresh EAS build with the inspector disabled receives small deltas, becomes live and clears Syncing. No temporary transport bypass is committed.

Measurements use two synthetic conversations with 500 completed turns / 3,001 timeline items each, including tool output. Initial baseline app samples on Medium_Phone_API_36.1 were 3177.15 / 3660.23 / 3324.30 ms to first content (median 3324.30 ms), transferring about 6.23 MB protobuf per open. The final controlled comparison below uses the same Canopy_Fold emulator, fresh native build and SSH network path. The full-watch control disables cache reads/writes and requests the original full snapshot; both modes use the current timeline renderer.

Watch open on Fold Full-watch control Warm cache + delta
First content, three samples 3119.1 / 3677.3 / 3104.1 ms 653.6 / 580.9 / 693.4 ms
Median 3119.1 ms 653.6 ms
Initial protobuf update received by the app 6,233,091–6,233,101 B 513–523 B

After a process restart, a disk-backed open showed content in 709.5 ms, then received a 523 B delta. Empty-cache controls took 2234.9 / 2708.1 ms and received full snapshots, as expected. Cached switches visibly kept content in place and reached live. Revoking the live synthetic device returned the app to pairing with the revocation message; inspecting its private cache confirmed no snapshot files remained.

Timing runs from the thread hook mounting to snapshot render plus two animation frames; pre-mount navigation animation is excluded. Byte figures measure the initial WatchThread protobuf update, excluding framing, TLS and separate metadata RPCs. These are instrumented development-mode, synthetic, shared-host results, not production guarantees. Provisional HMR/debugging samples are excluded. The browser results are in the preceding layer. A separate local tail-loading stage is deferred; the existing mobile FlatList remains, and two nested item/turn scans were replaced with a Set and grouped Map.

Verification: install, generated-contract check, typecheck, lint, formatting, Expo lint and Expo Doctor (20/20) pass. iOS and Android production JS exports pass. The complete check:tmp run passed 1,490 tests, 159 skipped, 0 failed, with 10,039 assertions. Meaningful filesystem/gateway tests cover restart, cursor durability, byte/count LRU limits, oversized/corrupt entries, unpair races and late writes, real paired TLS HTTP/1.1 and HTTP/2 delta reads, secret/token exclusion and actual device revocation. Protocol owner/fallback/restart and two-gateway remote coverage are in the first layer.

Device qualification: the fresh Android dev build installed on Canopy_Fold on the Mac Studio. Light/dark and open/closed hinge postures were checked; this generic AVD retains its wide viewport when closed. Phone-width light/dark layouts were checked on Medium's existing dev client with the current JS. Installing the fresh native build on Medium failed, including the tool-prescribed retry, with “Requested internal only, but not enough space”; it was not reinstalled or cleared to make room. No iOS runtime or physical Galaxy check is claimed. The task-owned app, servers, gateway/Postgres, port forwards and emulator were cleaned up afterward.

Copy audit (§8): removed temporary “Thread / Thread”, “Default model” and “Worker” values when metadata is known. Unknown metadata/timeline uses shared skeletons. The existing quiet “Syncing…” copy has reserved space. Revocation/storage errors state the cause and recovery action. No new glossary term or colour-only state. Draft stack; do not merge individual layers.

@nixuuu
nixuuu added this pull request to stack #274 November 6, 2026 11:25
@nixuuu
nixuuu marked this pull request as ready for review November 6, 2026 11:52
@nixuuu
nixuuu merged commit 9c65f32 into dev Nov 6, 2026
12 checks passed
@nixuuu
nixuuu deleted the perf/thread-cache-3 branch November 6, 2026 11:52
nixuuu added a commit that referenced this pull request Nov 6, 2026
Opening a long bot conversation currently transfers every message again.
This adds durable, owner-scoped checkpoints for bot conversations and
bot-to-bot exchanges, so clients can request only changed messages and
current control state.

The bot tables have their own commit-ordered key journal, following the
thread mirror epoch/cursor design. A checkpoint binds the owner, view
and revision. Message edits, deletion and turn failures produce
upserts/removals. A metadata digest confirms approvals, read state,
pending cards, configuration, streaming text, routines and reminders;
changed control state is replaced atomically. Missing/expired cursors,
gaps, another epoch, rewritten exchange membership and moves fall back
to a full projection.

Linked bot reads pass the cursor to the home through the existing route.
Offline reads retain the existing mirror path and gateway action
enforcement. Proto additions are opt-in: old clients get full snapshots,
and a home ignoring the new fields returns full state that new clients
can apply. No link protocol version change. Migration 0091 is the next
free number on the rebased dev; schema expectations and generated
contracts are included.

### Measurements

Single-run development-server measurements with a real local
gateway/Postgres and 3,000 stored messages, each containing about 2 KB
of long output. Browser first content is measured from route switch to
message DOM appearing. SSE bytes include the ready frame and initial
state, excluding HTTP/TLS headers and unrelated sidebar polling. The
after column uses the whole stack, including the opt-in web client in
the next layer.

| Measurement | Before | After |
|---|---:|---:|
| Web warm switch, first content | 3,819.9 ms | 187.2 ms |
| Initial bot SSE, unchanged reopen | 7,050,627 B | 393 B |
| Web cold route, first content | 4,512.9 ms | 910.2 ms |
| Initially mounted message rows | 3,000 | 100 |
| Android first content / bytes | Unverified | Unverified |

The baseline full protobuf was 6,772,217 B. Cold full history remains
approximately 6.77 MB: this does not add server-side pagination. The
cold browser sample includes development module loading; these are
observations, not statistical latency bounds. Android timing and runtime
visuals are unverified: the device tool's dev-client deep link failed
with `COMMAND_FAILED: adb exited with code 1`, including its advised
debug retry. No SSH/ADB UI fallback was used.

### Verification

- Real-gateway integration and schema tests: 28 passed, including
edits/deletes/failures, control confirmation, gap/epoch/view fallback,
owner isolation, cursor after restart, linked chat/exchange, and a home
ignoring the additive request fields.
- `bun install`, `bun run gen:check`, `bun run typecheck`, `bun run
lint`, `bun run format:check` passed on this stage after rebasing.
- Polish protocol/read-path documentation updated. No new UI copy in
this layer.

The bot mirror publisher still uses its existing full projection
protocol; this stack changes UI reads, not partner journal publishing.
Live control metadata is a complete replacement when changed, rather
than a second journal for approvals and linked thread cards.

Related: #271, #272, #273.
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