Skip to content

feat(schema): add v3 cache invalidation - #233

Merged
Moskize91 merged 4 commits into
mainfrom
feat/schema-v3-cache-invalidation
Aug 4, 2026
Merged

feat(schema): add v3 cache invalidation#233
Moskize91 merged 4 commits into
mainfrom
feat/schema-v3-cache-invalidation

Conversation

@Moskize91

Copy link
Copy Markdown
Contributor

Summary

  • bump archive/home schema handling to v3 and invalidate incompatible index caches
  • salvage v2 FTS index data into chapter index artifacts while dropping legacy embedded index cache files
  • add upgrade coverage for archive, home, directory cache, and library cache paths

Verification

  • pnpm run lint:fix
  • pnpm exec vitest run packages/core/src/document/directory/search-index.test.ts test/core/storage/schema-upgrade/index.test.ts
  • pnpm test:run
  • pnpm exec tsc --noEmit --pretty false
  • real archive smoke: copied 三国演义.wikg and 客体关系.wikg from iCloud to temp dirs, upgraded v2 -> v3, verified FTS artifacts, no embedded index.db/fts.db, imported into a temp library, synced wikg://lib/index, and queried 刘备 successfully

@coderabbitai

coderabbitai Bot commented Aug 4, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: a25c333e-a944-4ed9-a2ed-f669ae33e3a7

📥 Commits

Reviewing files that changed from the base of the PR and between ecf813c and b539d64.

📒 Files selected for processing (4)
  • packages/core/src/document/directory/search-index.test.ts
  • packages/core/src/document/directory/search-index.ts
  • packages/core/src/storage/schema-upgrade/index.ts
  • test/core/storage/schema-upgrade/index.test.ts
🚧 Files skipped from review as they are similar to previous changes (3)
  • packages/core/src/document/directory/search-index.test.ts
  • packages/core/src/storage/schema-upgrade/index.ts
  • test/core/storage/schema-upgrade/index.test.ts

Summary by CodeRabbit

  • New Features

    • Added schema v2→v3 upgrade support for archives and homes.
    • Rebuilt chapter search data during upgrades while preserving important content.
    • Automatically removed outdated search caches and recreated them when needed.
    • Added safeguards to block upgrades when active or unsupported overlay state is present.
  • Bug Fixes

    • Search index reads now treat missing, unreadable, or outdated caches as unavailable.
    • Improved handling of incompatible archive-backed search indexes.
  • Documentation

    • Documented upgrade behavior, cleanup actions, and blocking conditions.

Walkthrough

The change upgrades archive, home, and WIKG manifest schemas from version 2 to version 3. Archive upgrades rebuild chapter FTS artifacts, remove obsolete index settings, and create refreshed database overlays. Home upgrades remove derived state. Search-index access now invalidates incompatible caches, reports them as missing on reads, and recreates them on writes. Tests cover upgrades, cache concurrency, archive overlays, and isolated runtime state.

Sequence Diagram(s)

sequenceDiagram
  participant SchemaUpgrade
  participant Archive
  participant DirectoryDocument
  participant FTSArtifacts
  SchemaUpgrade->>Archive: extract archive database
  Archive->>DirectoryDocument: open extracted document
  DirectoryDocument->>FTSArtifacts: refresh chapter artifacts
  DirectoryDocument->>DirectoryDocument: remove archive_index_settings
  SchemaUpgrade->>Archive: add database.db overlay
Loading
sequenceDiagram
  participant SearchIndexClient
  participant openSearchIndexDatabase
  participant SQLiteCache
  participant IndexFile
  SearchIndexClient->>openSearchIndexDatabase: open index.db
  openSearchIndexDatabase->>SQLiteCache: inspect search_index_state.version
  SQLiteCache-->>openSearchIndexDatabase: return missing or incompatible version
  openSearchIndexDatabase->>IndexFile: delete incompatible cache
  openSearchIndexDatabase-->>SearchIndexClient: report missing or initialize cache
Loading

Possibly related PRs

🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title follows the required type(scope): subject format and accurately describes the schema v3 cache invalidation changes.
Description check ✅ Passed The description clearly summarizes the schema upgrades, cache invalidation, data migration, tests, and verification steps.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
✨ Simplify code
  • Create PR with simplified code
  • Commit simplified code in branch feat/schema-v3-cache-invalidation

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (3)
test/core/storage/schema-upgrade/index.test.ts (2)

89-135: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Good coverage. One addition would strengthen it.

The test proves the FTS artifact is created and archive_index_settings is dropped. Add an assertion that the mutation token is preserved, because writeWikgArchiveWithOverlays runs with preserveMutationToken: true and the new database.db overlay changes archive content.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@test/core/storage/schema-upgrade/index.test.ts` around lines 89 - 135, Extend
the test around upgradeWikiGraphArchiveSchema to assert that the archive’s
mutation token remains unchanged after the v2-to-v3 upgrade. Capture the token
before upgrading and compare it with the token read afterward, preserving
coverage for writeWikgArchiveWithOverlays configured with preserveMutationToken:
true.

1207-1213: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Include all production document paths.

WIKG_ARCHIVE_PATTERNS also permits cover/data.bin and cover/info.json, but isTestArchiveDocumentPath omits them. Reuse isWikgArchivePath or add these paths to keep the fixture complete.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@test/core/storage/schema-upgrade/index.test.ts` around lines 1207 - 1213,
Update isTestArchiveDocumentPath to recognize every production archive path
allowed by WIKG_ARCHIVE_PATTERNS, including cover/data.bin and cover/info.json.
Prefer reusing isWikgArchivePath if it provides the same validation; otherwise
extend the existing predicate while preserving its current database, TOC,
source, and summary path handling.
packages/core/src/storage/schema-upgrade/index.ts (1)

256-264: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Use DATABASE_ENTRY_PATH for the archive database path.

The constant is defined in packages/core/src/storage/wikg/wikg-coordinator/constants.ts. Use it for both databasePath and entryPath.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/core/src/storage/schema-upgrade/index.ts` around lines 256 - 264,
Update the database archive path construction and returned entry metadata in the
surrounding schema-upgrade logic to use the existing DATABASE_ENTRY_PATH
constant for both the join filename and entryPath value, replacing the
duplicated literal "database.db".
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/core/src/document/directory/search-index.ts`:
- Around line 28-42: Serialize the search-index lifecycle in the surrounding
search-index operation using a per-document lock that covers compatibility
validation, cache deletion, initialization, and the database operation. Ensure
readonly callers cannot delete a cache while a writer is rebuilding it, while
preserving existing readonly error behavior. Add a concurrent read/write
regression test covering simultaneous incompatible-cache handling.

In `@packages/core/src/runtime/common/wiki-graph/dir.ts`:
- Line 11: Remove the process-wide testingStateDirectoryPathOverride fallback
and update the related directory-path resolution and cleanup logic to use only
the caller’s AsyncLocalStorage context. Ensure overlapping test scopes cannot
read or restore a shared latest path; if context-local access is unavailable,
use the existing API-boundary serialization mechanism instead.

---

Nitpick comments:
In `@packages/core/src/storage/schema-upgrade/index.ts`:
- Around line 256-264: Update the database archive path construction and
returned entry metadata in the surrounding schema-upgrade logic to use the
existing DATABASE_ENTRY_PATH constant for both the join filename and entryPath
value, replacing the duplicated literal "database.db".

In `@test/core/storage/schema-upgrade/index.test.ts`:
- Around line 89-135: Extend the test around upgradeWikiGraphArchiveSchema to
assert that the archive’s mutation token remains unchanged after the v2-to-v3
upgrade. Capture the token before upgrading and compare it with the token read
afterward, preserving coverage for writeWikgArchiveWithOverlays configured with
preserveMutationToken: true.
- Around line 1207-1213: Update isTestArchiveDocumentPath to recognize every
production archive path allowed by WIKG_ARCHIVE_PATTERNS, including
cover/data.bin and cover/info.json. Prefer reusing isWikgArchivePath if it
provides the same validation; otherwise extend the existing predicate while
preserving its current database, TOC, source, and summary path handling.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 801f832e-8ade-4a0e-8f71-f27720f24840

📥 Commits

Reviewing files that changed from the base of the PR and between 1479fbe and ecf813c.

📒 Files selected for processing (15)
  • docs/schema-upgrade.md
  • packages/cli/src/commands/archive-command/index.test.ts
  • packages/core/src/document/directory/search-index.test.ts
  • packages/core/src/document/directory/search-index.ts
  • packages/core/src/document/home-schema-upgrade.ts
  • packages/core/src/document/schema.ts
  • packages/core/src/retrieval/search-index/search/errors.ts
  • packages/core/src/runtime/common/wiki-graph/dir.ts
  • packages/core/src/storage/schema-upgrade/index.ts
  • packages/core/src/storage/wikg/archive/manifest.ts
  • test/core/library/query.test.ts
  • test/core/retrieval/query/archive-view/index.test.ts
  • test/core/storage/schema-upgrade/index.test.ts
  • test/core/storage/wikg/archive.test.ts
  • test/core/storage/wikg/wiki-graph-archive-file.test.ts
💤 Files with no reviewable changes (1)
  • packages/core/src/document/schema.ts

Comment thread packages/core/src/document/directory/search-index.ts
Comment thread packages/core/src/runtime/common/wiki-graph/dir.ts Outdated
@Moskize91 Moskize91 changed the title Add schema v3 cache invalidation feat(schema): add v3 cache invalidation Aug 4, 2026
@Moskize91
Moskize91 merged commit e456fa9 into main Aug 4, 2026
4 checks passed
@Moskize91
Moskize91 deleted the feat/schema-v3-cache-invalidation branch August 4, 2026 01:31
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