refactor(ocr): versioned migrate() seam for the sample store #19
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!19
Loading…
Reference in a new issue
No description provided.
Delete branch "agent/ocr-store-migrate"
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 #17 (OCR Direction 2 — richer persisted store), Part of #13.
Context
Today
loadSamples(src/inkedge.ts:315) casts raw JSON straight toSampleStorefrom eitherhost.state.readSamples()or the on-diskocr/samples.json, and theformat: 1literal (src/ocr/samples.ts:118;emptyStore, :137) is never read or checked. Direction 2 keeps adding optional fields (letters?landed in !12;joins?is coming in the join-synthesis work). Optional fields are backward-compatible, but the store has no seam for the first change that isn't, and no validation that a loaded blob is actually a store. This MR adds that seam — the first real format version transition — so later shape changes have a home. This is issue #17 part 3.What to change
src/ocr/samples.tsexport const CURRENT_FORMAT = 2. ChangeSampleStore.formatfrom1to2, andemptyStore()to returnformat: CURRENT_FORMAT.export function migrate(raw: unknown): SampleStore, a single linear version ladder so the next bump is "add a case":null/ wrong-typed input → returnemptyStore()(defends the raw cast that exists today).raw.format. Missing or1⇒ a format-1 store: upgrade losslessly to 2 — default absent required containers (words,remarks→{}), leave optional fields (letters, and futurejoins) exactly as-is, setformat: 2. A format-1 store carries the same information, so the upgrade adds no data, only the version.format === 2⇒ return as-is after the same defensive container defaulting.src/inkedge.tsloadSamples(:315) route both sources throughmigrate(...)instead of the raw cast:this.samples ??= migrate((await this.host.state.readSamples()) ?? (await this.readJson<unknown>(\${this.dir}/ocr/samples.json`)) ?? emptyStore())(emptyStore()is already current, somigrate` is a no-op on it.)Notes for the implementer
format: 1literals insrc/ink/handsheet.ts:330,477are a different, unrelated interface (the hand sheet). Do not touch them.SampleStoreliteral withformat: 1— they all go throughemptyStore(). So the type change to2should not break test literals; if the typechecker surfaces any, fix them to useemptyStore()/CURRENT_FORMAT.agent/ocr-letter-diversity) and the join work (child issue) are separate.Validation contract
npm installthennpx vitest run src/__tests__→ PASS (noflake.nix).Add a test (new
src/__tests__/migrate.test.ts, or extendsamples.test.ts):{ format: 1, words: { und: [[...one stroke...]] }, remarks: { r1: {...a RemarkRecord...} } }with noletters. Assertmigrate(fixture)returnsformat === 2, withwordsandremarksdeep-equal to the input, andlettersabsent-or-empty.main): onmainthere is nomigrate; the raw cast leavesformat: 1, so an assertion that the loaded store isformat: 2fails before this change. State that you watched it fail onmainfirst.migrate(emptyStore())is idempotent and staysformat: 2.migrate(JSON.parse(JSON.stringify(store)))deep-equals a currentstore.migrate(null)andmigrate(42)each return a valid empty current store (not a throw).✅ Agent run completed
3m41s · 28 turns · $1.49 · pushed
agent/ocr-store-migrate· session saved — new comments resume this runclaude referenced this pull request2026-10-20 17:13:37 +02:00
claude referenced this pull request2026-10-20 17:14:35 +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
What changed
src/ocr/samples.ts: addedexport const CURRENT_FORMAT = 2, bumpedSampleStore.format1 → 2, andemptyStore()now returnsformat: CURRENT_FORMAT. Addedexport function migrate(raw: unknown): SampleStore— a linear version ladder: non-object/null/array →emptyStore(); missing/1→ lossless upgrade to 2 (default absentwords/remarks, leaveletters/futurejoinsas-is);2→ no-op after the same container defaulting; unknown/newer format → preserved as-is (downgrade-safe).src/inkedge.ts:loadSamplesnow routes both sources (host.state.readSamples()and on-diskocr/samples.json) throughmigrate(...)instead of the raw cast.format: 1hand-sheet literals insrc/ink/handsheet.tsuntouched.Verify
npx tsc --noEmitclean;npx vitest run src/__tests__→ 683 pass / 0 fail. Newsrc/__tests__/migrate.test.ts(6 tests): format-1 fixture upgrades losslessly (format===2,words/remarksdeep-equal,lettersabsent/empty), idempotence onemptyStore(), JSON round-trip, newer-format downgrade preservation, and garbage input (null/42/…) → valid empty store. Red gate: watchedmigrate.test.tsfail on the pre-change tree withmigrate is not a functionbefore implementing.Follow-ups
None. (Pure plumbing + version bump; the letter-retention and join-synthesis work remain separate per the MR description.)
claude referenced this pull request2026-10-20 18:50:14 +02:00
claude referenced this pull request2026-10-20 18:51:10 +02:00
claude referenced this pull request2026-10-20 18:51:10 +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:52:21 +02:00
claude referenced this pull request2026-10-20 18:52:22 +02:00
claude referenced this pull request2026-10-20 18:54:48 +02:00
claude referenced this pull request2026-10-20 18:59:13 +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