Skip to content

fix(nbstore): accumulate overlapping doc priority requests - #15595

Merged
darkskygit merged 2 commits into
toeverything:canaryfrom
Dev-next-gen:fix/nbstore-accumulate-doc-priority
Sep 13, 2026
Merged

darkskygit merged 2 commits into
toeverything:canaryfrom
Dev-next-gen:fix/nbstore-accumulate-doc-priority

Conversation

@Dev-next-gen

@Dev-next-gen Dev-next-gen commented Sep 13, 2026 •

Copy link
Copy Markdown
Contributor

Description

While reading DocSyncPeer.addPriority I noticed the fix from #15338 (commit cd6593c6) was applied to only one of the three near-identical copies of that method in nbstore. The two others still carry the pre-#15338 code:

  • DocFrontend.addPriority — packages/common/nbstore/src/frontend/doc.ts
  • IndexerSyncStatus.addPriority — packages/common/nbstore/src/sync/indexer/index.ts

Both write the incoming priority over whatever was stored:

const oldPriority = this.prioritySettings.get(id) ?? 0;
this.prioritySettings.set(id, priority);                   // overwrites
this.jobDocQueue.setPriority(id, oldPriority + priority);  // adds

The two lines already disagree with each other, and the release callback returned by the method subtracts priority from the current value, so it only makes sense if the forward path adds. The result is that overlapping priority requests for the same doc don't stack, and releasing one of them drops the doc to zero instead of back to the level the remaining holder asked for.

That overlap is reachable from the UI. WorkspaceEngine.doc is a DocFrontend (entities/engine.ts → client.docFrontend), and two call sites add +10 to the same pageId: detail-page-wrapper.tsx and peek-view/view/utils.ts. Open a doc, peek the same doc, close the peek view, and the still-open doc is left at priority 0 — behind every other queued doc. The indexer has the same pair: the navigation panel node and the doc-summary store both add +10 to the same docId.

The fix is the same shape as #15338 — compute newPriority / restoredPriority once and use it for both the map and the queue.

Test

doc priority requests accumulate in packages/common/nbstore/src/__tests__/frontend.spec.ts. Two holders take +10 on high and one of them releases; low sits at +5; both load jobs are queued before start() so the queue priority alone decides the load order, which the test reads by spying on storage.getDoc.

On canary it fails:

FAIL  packages/common/nbstore/src/__tests__/frontend.spec.ts > doc priority requests accumulate
AssertionError: expected [ 'low', 'high' ] to deeply equal [ 'high', 'low' ]

With the fix, the whole package is green (yarn vitest run packages/common/nbstore: 16 files, 86 tests).

Checklist

  • I have signed the AFFiNE Contributor License Agreement
  • The PR targets the canary branch and its title follows Conventional Commits
  • Tests are added or updated where it makes sense
  • yarn lint and yarn typecheck pass locally
  • If the PR code includes AI-generated edits, I have carefully reviewed it to ensure that the scope of the PR is consistent with what is claimed in the title and description, and that there are no redundant or invalid implementations.
  • I agree that maintainers may close the PR without discussion if they consider the code quality to be too low.

AI tools used

Summary by CodeRabbit

  • Bug Fixes

    • Improved priority handling for document loading and indexing so multiple priority requests accumulate correctly.
    • Ensured queued work consistently reflects remaining priority requests after individual requests are released.
    • High-priority documents and indexing tasks are now processed ahead of lower-priority work as expected.
  • Tests

    • Added coverage validating accumulated priority behavior for document loading and index crawling.

`DocFrontend.addPriority` and `IndexerSyncStatus.addPriority` overwrite
`prioritySettings` with the incoming priority instead of adding it to the
value already stored, while the release callback they return subtracts.
When two holders prioritize the same doc, releasing one of them drops the
doc to zero instead of back to the other holder's level.

`DocSyncPeer.addPriority` was a third copy of the same code and was fixed
in toeverything#15338; these two were missed.
@CLAassistant

CLAassistant commented Sep 13, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@github-actions github-actions Bot added the test Related to test cases label Sep 13, 2026
@coderabbitai

coderabbitai Bot commented Sep 13, 2026 •

Copy link
Copy Markdown
Contributor

Review Change StackReview Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Advanced

Run ID: cd145114-6326-4df4-b4d3-27bcc75032c5

📥 Commits

Reviewing files that changed from the base of the PR and between fd8bdbc and 9c34b3a.

📒 Files selected for processing (1)
  • packages/common/nbstore/src/__tests__/sync.spec.ts

Included review availability: Your plan provides up to 10 included reviews per hour; 8 remain after this review.


📝 Walkthrough

Walkthrough

The change makes priority updates cumulative in DocFrontend and IndexerSyncStatus. Cleanup restores accumulated values consistently. Tests verify that higher-priority document and indexer jobs run first.

Changes

Priority accumulation

Layer / File(s) Summary
Priority tracking and validation
packages/common/nbstore/src/frontend/doc.ts, packages/common/nbstore/src/sync/indexer/index.ts, packages/common/nbstore/src/__tests__/frontend.spec.ts, packages/common/nbstore/src/__tests__/sync.spec.ts
addPriority now stores accumulated priority values and applies them to queued jobs. Cleanup restores the reduced value in tracking and queue state. Tests verify higher-priority document loads and indexer crawls run before lower-priority jobs.

Priority: ⬇️ Low

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Suggested reviewers: darkskygit

Merge Risk: ⚪ Minimal · up to 9c34b

Priority requests remain accumulated while other holders are active, with coverage for document loading and indexer crawl ordering. No actionable merge risk remains.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: accumulating overlapping document priority requests in nbstore.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 4…
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.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Advanced

Run ID: 79598393-d413-4136-af68-23f589c6b3a5

📥 Commits

Reviewing files that changed from the base of the PR and between cfda485 and fd8bdbc.

📒 Files selected for processing (3)
  • packages/common/nbstore/src/__tests__/frontend.spec.ts
  • packages/common/nbstore/src/frontend/doc.ts
  • packages/common/nbstore/src/sync/indexer/index.ts

Included review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review.

Comment thread packages/common/nbstore/src/sync/indexer/index.ts
The existing case goes through DocFrontend with DocSyncImpl.dummy, so
IndexerSyncStatus.addPriority was never executed. This one drives
IndexerSyncImpl: two holders take +10 on the same doc while the loop is
held inside the root doc crawl, one of them releases, and the crawl order
shows the doc still outranks a +5 neighbour instead of falling back to 0.
@codecov

codecov Bot commented Sep 13, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 54.41%. Comparing base (cfda485) to head (9c34b3a).

Additional details and impacted files
@@            Coverage Diff             @@
##           canary   #15595      +/-   ##
==========================================
+ Coverage   54.36%   54.41%   +0.05%     
==========================================
  Files        4004     4004              
  Lines      191872   191876       +4     
  Branches    30920    30923       +3     
==========================================
+ Hits       104306   104411     +105     
+ Misses      83690    83352     -338     
- Partials     3876     4113     +237     
Flag Coverage Δ
server-test 78.49% <ø> (+0.05%) ⬆️
unittest 33.31% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@darkskygit
darkskygit merged commit 868acf8 into toeverything:canary Sep 13, 2026
66 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

test Related to test cases

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

3 participants