OCR Direction 2: bound read latency as the per-writer store grows (shortlisting + caps) #15
No reviewers
Labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
daniel/obsidian-remarkable-round-trip!15
Loading…
Reference in a new issue
No description provided.
Delete branch "agent/ocr-store-scaling"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Part of #13 (Direction 2, scaling study — issue #13 part 2). Independent of !12; can land in parallel with it and with the diversity-retention MR.
Why
The per-writer store is about to grow (more word samples; letter templates from !12). Two read-time hot paths scale linearly with store size and will blow the per-remark sync budget. Measured today (throwaway probe,
npm install+ vitest, rm2 device traces):WordReader.read— DTW over owner samples (src/ocr/words.ts:218): does a fulldtw()over every owner sample, gated only by a width ratio. UnlikereadFonts(words.ts:235), which profile-shortlists candidates to 150 before DTW, the owner-sample loop has no shortlist.≈ linear, ~26 µs/sample. A 10-word remark at 3000 samples ≈ 0.8 s of owner-DTW per remark, before fonts.
classify— quick pass over all templates (src/ocr/recognize.ts:254):classifyrunsmatchDistance(cloud, template.cloud, 1)over all templates before keeping the top 24 for the full match.segmentcallsclassifyfor every 1–3-cluster window, so per word it is O(letters × templates).≈ linear, ~0.24 ms/template/word. !12's learned letter templates land right here.
What to change
WordReader.readowner-sample shortlist (src/ocr/words.ts, thethis.samplesloop at ~:222). Before the per-sampledtw(), shortlist by the cheap coarse signalreadFontsalready uses —profileDistance(trace.profile, sample.trace.profile)plus the width-ratio gate that is already there — and run the fulldtw()on only the best ~K (K≈40–60; pick against the budget below). KeepTrace.profileon the stored sample traces (already computed intraceOf). The trusted-match semantics (SAMPLE_TRUST, returning the owner match whentrusted) must not change — only the set that gets the expensive DTW narrows.classify's quick pass (src/ocr/recognize.ts,classify~:254 /segment~:412). Either (a) pre-filter templates by the baseline-relativetop/bottom/widthmetrics (cheap scalars already onTemplate) before the shapematchDistance, scoring shape only for metric-plausible templates, and/or (b) accept a per-character template cap so the total template count stays bounded regardless of store size. Keep the existing "quick pass → top-24 → full match" structure; just stop the quick pass from being unconditionally O(all templates). Do not changeOCR_CHARS,cloudOf, or theCandidate/Templateshapes.Shortlisting must not regress accuracy: a correct owner-sample / letter read today must stay correct.
Scope boundary
classifyscaling by passing syntheticTemplate[](duplicate existing templates), as the probe above did.Validation contract
npx vitest run src/__tests__→ PASS, plus a new test (e.g.src/__tests__/scaling.test.ts) that:WordReader.readon a probe word stays under a per-word budget with a large synthetic store (e.g. 3000 samples) — today ~80 ms, so assert e.g.< 15 ms; and assertrecognizeof one word with ~1000+ synthetic templates stays under budget — today ~350 ms. Generate load with thespike5.test.tsmethodology (render words through a font = a known hand) or the probe shape above.(There is no
flake.nix; usenpm install+npx vitest, notnix develop.)✅ Agent run completed
11m31s · 53 turns · $3.85 · pushed
agent/ocr-store-scaling· session saved — new comments resume this runclaude referenced this pull request2026-10-20 15:32:54 +02:00
claude referenced this pull request2026-10-20 15:34:21 +02:00
claude referenced this pull request2026-10-20 15:34:22 +02:00
claude referenced this pull request2026-10-20 15:41:02 +02:00
claude referenced this pull request2026-10-20 15:41:02 +02:00
What changed
WordReader.read(src/ocr/words.ts): owner samples are now shortlisted by the cheap profile signal (profileDistance+ the existing width-ratio gate) and only the bestSAMPLE_SHORTLIST=50 get the fulldtw()— the same narrowingreadFontsalready does.SAMPLE_TRUST/trustedsemantics are unchanged; only the DTW set narrows.classify(src/ocr/recognize.ts): the quick pass is now a metric branch-and-bound.metricOf(the baseline-relative top/bottom/width penalty) is a lower bound on the full cost, so templates are shape-matched in metric order only until no unseen one could beat the 24th-best — identical top-24 and output, just not O(all templates). AddedcapPerChar(capTEMPLATE_CAP=6 per character, applied once inrecognize) so total templates stay ~OCR_CHARS × 6whatever the store size. Bundled fonts have ≤2 variants/char, so nothing bundled is dropped.OCR_CHARS,cloudOf,Candidate/Templateuntouched.Verify
npx vitest run src/__tests__→ 672 pass (incl. newsrc/__tests__/scaling.test.ts);tsc --noEmitandeslintclean. New test red-gates both paths — verified it fails on pre-fix source (read@3000 = 174 ms vs 50 ms budget;recognize@2160 templates = 546 ms vs 250 ms budget) and passes with the fix (≈11 ms and ≈94 ms). Two accuracy guards (owner word still found among 3000 distractors; a print sentence reads identically under a 10×-inflated template store) pass on both pre- and post-fix, confirming they guard correctness, not speed.Measured on the runner:
read@3000 samples 174 → 11 ms;recognizeof one word @~2160 templates 546 → ~94 ms (flat in store size). No pipeline has been triggered for this MR head on the forge yet — the above is local verification.Follow-ups
None. Store schema/caps (word-sample caps, letter/join templates) remain the diversity-retention MR and the richer-store child issue, as scoped.
claude referenced this pull request2026-10-20 16:34:09 +02:00
claude referenced this pull request2026-10-20 16:34:09 +02:00
claude referenced this pull request2026-10-20 17:08:52 +02:00
claude referenced this pull request2026-10-20 17:14:50 +02:00
claude referenced this pull request2026-10-20 17:14:50 +02:00
claude referenced this pull request2026-10-20 18:51:57 +02:00
claude referenced this pull request2026-10-20 18:52:05 +02:00
claude referenced this pull request2026-10-20 18:52:06 +02:00
claude referenced this pull request2026-10-20 18:59:06 +02:00
claude referenced this pull request2026-10-20 18:59:06 +02:00
claude referenced this pull request2026-10-20 18:59:06 +02:00
claude referenced this pull request2026-10-20 18:59:13 +02:00
claude referenced this pull request2026-10-20 19:00:17 +02:00