Skip to content

fix(core): query cache concurrency and hybrid bucket ranking - #235

Merged
Moskize91 merged 4 commits into
mainfrom
fix/query-cache-readonly
Aug 4, 2026
Merged

fix(core): query cache concurrency and hybrid bucket ranking#235
Moskize91 merged 4 commits into
mainfrom
fix/query-cache-readonly

Conversation

@Moskize91

Copy link
Copy Markdown
Contributor

Summary

  • serialize SQLite index-cache leases so concurrent query/cache access does not reuse write paths through readonly leases
  • sanitize internal worker process environment before spawning queue workers
  • feed text sentence evidence into bucketed object ranking and clarify query/index help docs

Validation

  • pnpm verify
  • pnpm test:run
  • pnpm build
  • pnpm smoke:pack-install
  • pnpm publish:dry-run:cli
  • pnpm publish:dry-run:core
  • pnpm test:run test/core/storage/schema-upgrade/index.test.ts
  • pnpm test:run packages/core/src/document/directory/search-index.test.ts test/core/storage/wikg/wiki-graph-archive-file.test.ts
  • pnpm test:run test/cli/archive/maintenance.test.ts
  • pnpm vitest run test/core/retrieval/query/archive-view/graph-search.test.ts -t "promotes object bucket hits"

@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: a19c7444-6bdb-4b0e-a3e8-edfca8bb75a4

📥 Commits

Reviewing files that changed from the base of the PR and between a25351d and fac85de.

📒 Files selected for processing (7)
  • packages/core/data/help/topics/readiness.jinja
  • packages/core/data/help/topics/uri.jinja
  • packages/core/src/retrieval/query/archive-view/search/buckets.ts
  • packages/core/src/storage/wikg/wikg-coordinator/file-store.ts
  • packages/core/src/storage/wikg/wikg-coordinator/locks.ts
  • test/core/retrieval/query/archive-view/graph-search.test.ts
  • test/core/storage/wikg/wiki-graph-archive-file.test.ts
🚧 Files skipped from review as they are similar to previous changes (6)
  • packages/core/data/help/topics/uri.jinja
  • test/core/retrieval/query/archive-view/graph-search.test.ts
  • test/core/storage/wikg/wiki-graph-archive-file.test.ts
  • packages/core/data/help/topics/readiness.jinja
  • packages/core/src/storage/wikg/wikg-coordinator/locks.ts
  • packages/core/src/retrieval/query/archive-view/search/buckets.ts

Summary by CodeRabbit

  • New Features

    • Improved archive and chapter search guidance, including readiness, caching, ranking, hybrid retrieval, and output-format behavior.
    • Enhanced search result grouping and evidence handling for more relevant archive results.
  • Bug Fixes

    • Improved coordination of archive cache updates during concurrent access.
    • Prevented unsafe runtime settings from being passed to internal processes.
    • Improved reliability when archive files are accessed or updated concurrently.
  • Documentation

    • Clarified archive indexing, synchronization, provider-backed embeddings, job listings, and summary guarantees.

Walkthrough

The change isolates internal child processes from dangerous runtime overrides. It updates archive search cache population to include bounded text hits and sentence evidence. SQLite lease acquisition now checks conflicts transactionally and retries until compatible. Archive file deletion and database resolution coordinate lease ownership. CLI help and tests document cache readiness, retrieval grouping, summary guarantees, index jobs, and JSONL output rules.

Sequence Diagram(s)

sequenceDiagram
  participant ArchiveQuery
  participant ObjectBucketPopulation
  participant SentenceEvidenceSearchCache
  participant SessionObjectCaches
  ArchiveQuery->>ObjectBucketPopulation: request bounded bucket results
  ObjectBucketPopulation->>SentenceEvidenceSearchCache: build sentence evidence cache inputs
  SentenceEvidenceSearchCache-->>ObjectBucketPopulation: return evidence-backed hits
  ObjectBucketPopulation->>SessionObjectCaches: merge and populate object caches
Loading
sequenceDiagram
  participant CacheWriteSession
  participant acquireSqliteLease
  participant SQLiteLeaseTable
  participant ArchiveReadSession
  CacheWriteSession->>acquireSqliteLease: request write lease
  acquireSqliteLease->>SQLiteLeaseTable: check locks and leases transactionally
  SQLiteLeaseTable-->>acquireSqliteLease: retry while the read lease conflicts
  ArchiveReadSession-->>SQLiteLeaseTable: release read lease
  acquireSqliteLease->>SQLiteLeaseTable: insert write lease
  SQLiteLeaseTable-->>CacheWriteSession: allow database access
Loading

Possibly related PRs

🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title uses the required type(scope): subject format and accurately describes the main query cache concurrency and hybrid ranking changes.
Description check ✅ Passed The description directly summarizes the concurrency, environment sanitization, ranking, documentation, and validation changes.
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 fix/query-cache-readonly

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: 6

🧹 Nitpick comments (1)
packages/core/src/storage/wikg/wikg-coordinator/locks.ts (1)

317-325: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Use getString for the mode column to match the other row mappers.

String(row.mode) converts a missing or non-string column into a value such as "undefined", and the thrown message then reports that value as an unsupported lease mode. mapEntryLock in packages/core/src/storage/wikg/wikg-coordinator/state.ts uses getString for the same purpose, and getString is already imported at line 6.

♻️ Proposed refactor
 function getSqliteLeaseMode(row: Record<string, unknown>): SqliteLeaseMode {
-  const mode = String(row.mode);
+  const mode = getString(row, "mode");
 
   if (mode === "read" || mode === "write") {
     return mode;
   }
🤖 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/wikg/wikg-coordinator/locks.ts` around lines 317 -
325, Update getSqliteLeaseMode to read row.mode with the already imported
getString helper, matching mapEntryLock’s row-mapping behavior, while preserving
the existing read/write validation and unsupported-mode error handling.
🤖 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/data/help/topics/readiness.jinja`:
- Line 149: Update the Hybrid workflow guidance near the retrieval instructions
to include running index sync when an existing archive cache is outdated, while
noting the exception that a missing cache may be created lazily. Preserve the
existing requirement to build both FTS and source embedding artifacts before
querying.

In `@packages/core/data/help/topics/uri.jinja`:
- Around line 80-81: Update the broad-query guidance in the URI help topic to
stop presenting `/source` and `/summary` as query lenses, since they are
readable text streams rather than collection scopes. Keep only the valid
object-family lenses, or replace them with a confirmed valid source/summary
query scope without changing the surrounding search guidance.

In `@packages/core/src/retrieval/query/archive-view/search/buckets.ts`:
- Line 248: Update the cache population flow around populateObjectBucketCaches
so the session-wide evidence window is independent of the first response page’s
limit, preventing the cache from being marked complete with only the initial
text hits. Use the established fixed session-wide window or transactionally
extend the cache for later cursor pages, and add a regression test that compares
a small-limit initial query with later evidence-backed object results.

In `@packages/core/src/storage/wikg/wikg-coordinator/locks.ts`:
- Around line 173-242: The acquireSqliteLease function at locks.ts contains an
unbounded while(true) polling loop that acquires locks while its caller
resolveSqliteDatabasePath already holds the "state" lock, creating circular
lock-acquisition order with deleteFile which holds the "write" lock before
acquiring "state". Fix the lock-acquisition order by restructuring
acquireSqliteLease to acquire the lease before the "state" lock is held
(requiring coordination with its caller), and add a deadline to the polling loop
that throws a lock-timeout error instead of looping forever. At file-store.ts in
deleteFile, align the lock-acquisition order to match the corrected pattern used
by resolveSqliteDatabasePath, and bound the waitForSqliteLeasesToDrain call with
the same deadline mechanism so both paths respect the same timeout and neither
hangs indefinitely.

In `@test/core/retrieval/query/archive-view/graph-search.test.ts`:
- Around line 134-142: Update the mention-gamma fixture in the
openedDocument.mentions.save call so its rangeStart is 29 and rangeEnd is 43,
matching the exclusive source span of “unnamed person.”

In `@test/core/storage/wikg/wiki-graph-archive-file.test.ts`:
- Around line 208-231: Update the catch block in the test around the writer and
reader promises to settle writer during failure cleanup, alongside releasing the
reader and awaiting reader. Ensure writer is awaited or caught so pending
polling is completed and any rejection is observed before rethrowing the
original error.

---

Nitpick comments:
In `@packages/core/src/storage/wikg/wikg-coordinator/locks.ts`:
- Around line 317-325: Update getSqliteLeaseMode to read row.mode with the
already imported getString helper, matching mapEntryLock’s row-mapping behavior,
while preserving the existing read/write validation and unsupported-mode error
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: ce305792-4865-4dee-9ee7-16641ac83ff1

📥 Commits

Reviewing files that changed from the base of the PR and between 830c8f4 and a25351d.

📒 Files selected for processing (13)
  • packages/cli/src/args/help.test.ts
  • packages/cli/src/runtime/entry-context.ts
  • packages/cli/src/runtime/internal-child.test.ts
  • packages/cli/src/runtime/internal-child.ts
  • packages/core/data/help/commands/predicate.jinja
  • packages/core/data/help/commands/uri.jinja
  • packages/core/data/help/topics/readiness.jinja
  • packages/core/data/help/topics/uri.jinja
  • packages/core/src/retrieval/query/archive-view/search/buckets.ts
  • packages/core/src/storage/wikg/wikg-coordinator/file-store.ts
  • packages/core/src/storage/wikg/wikg-coordinator/locks.ts
  • test/core/retrieval/query/archive-view/graph-search.test.ts
  • test/core/storage/wikg/wiki-graph-archive-file.test.ts

Comment thread packages/core/data/help/topics/readiness.jinja Outdated
Comment thread packages/core/data/help/topics/uri.jinja Outdated
Comment thread packages/core/src/retrieval/query/archive-view/search/buckets.ts Outdated
Comment thread packages/core/src/storage/wikg/wikg-coordinator/locks.ts
Comment on lines +134 to +142
await openedDocument.mentions.save({
chapterId: 1,
id: "mention-gamma",
qid: "QGamma",
rangeEnd: 31,
rangeStart: 17,
sentenceIndex: 0,
surface: "unnamed person",
});

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Correct the mention offsets in the fixture.

"unnamed person" starts at offset 29 and ends at exclusive offset 43. The current range 17..31 selects part of "appears beside an un". This creates an invalid mention record and can let the test pass without valid source-span metadata.

Proposed fix
           await openedDocument.mentions.save({
             chapterId: 1,
             id: "mention-gamma",
             qid: "QGamma",
-            rangeEnd: 31,
-            rangeStart: 17,
+            rangeEnd: 43,
+            rangeStart: 29,
             sentenceIndex: 0,
             surface: "unnamed person",
           });
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
await openedDocument.mentions.save({
chapterId: 1,
id: "mention-gamma",
qid: "QGamma",
rangeEnd: 31,
rangeStart: 17,
sentenceIndex: 0,
surface: "unnamed person",
});
await openedDocument.mentions.save({
chapterId: 1,
id: "mention-gamma",
qid: "QGamma",
rangeEnd: 43,
rangeStart: 29,
sentenceIndex: 0,
surface: "unnamed person",
});
🤖 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/retrieval/query/archive-view/graph-search.test.ts` around lines 134
- 142, Update the mention-gamma fixture in the openedDocument.mentions.save call
so its rangeStart is 29 and rangeEnd is 43, matching the exclusive source span
of “unnamed person.”

Comment thread test/core/storage/wikg/wiki-graph-archive-file.test.ts
@Moskize91 Moskize91 changed the title Fix query cache concurrency and hybrid bucket ranking fix(core): query cache concurrency and hybrid bucket ranking Aug 4, 2026
@Moskize91
Moskize91 merged commit 2ca7655 into main Aug 4, 2026
4 checks passed
@Moskize91
Moskize91 deleted the fix/query-cache-readonly branch August 4, 2026 09:33
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