Skip to content

Encapsulate WG21Index.papers behind snapshot and count accessors#92

Open
henry0816191 wants to merge 6 commits into
developfrom
fix/encapsulate-wg21-papers
Open

Encapsulate WG21Index.papers behind snapshot and count accessors#92
henry0816191 wants to merge 6 commits into
developfrom
fix/encapsulate-wg21-papers

Conversation

@henry0816191

@henry0816191 henry0816191 commented Jul 9, 2026

Copy link
Copy Markdown
Collaborator

Changes

src/paperscout/sources.py

  • self.papersself._papers (private backing store)
  • Added paper_count(), _replace_papers_for_test()
  • Documented atomic reference swap in _commit_index
  • Accessors read from _papers

Production readers

Docs

Tests

Related issue

Summary by CodeRabbit

  • New Features
    • Added a unified paper_count() accessor for WG21 index usage.
    • Improved snapshot-based access for paper data via read-only snapshots.
  • Bug Fixes
    • Health/status and scheduler reporting now use the unified paper-count source for more reliable counts.
  • Documentation
    • Expanded concurrency guidance to explicitly forbid thread access to WG21/ISOProber internals and require event-loop-only access or snapshot/counter accessor methods.
  • Tests
    • Updated mocks and assertions to use internal _papers, paper_count(), and snapshot APIs (including a test-only replacement helper).

Privatize the index map to _papers, add paper_count() and a test seam,
migrate cross-thread readers and tests, and update concurrency docs.
@henry0816191 henry0816191 self-assigned this Jul 9, 2026
@henry0816191 henry0816191 requested a review from wpak-ai as a code owner July 9, 2026 15:45
@coderabbitai

coderabbitai Bot commented Jul 9, 2026

Copy link
Copy Markdown

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: c494630b-975f-48ad-b21f-a35d21043e43

📥 Commits

Reviewing files that changed from the base of the PR and between 4b73b0b and 6c94b15.

📒 Files selected for processing (1)
  • src/paperscout/sources.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/paperscout/sources.py

📝 Walkthrough

Walkthrough

WG21Index now keeps paper data in a private _papers dict and exposes snapshot/count accessors. Callers, docs, and tests now use paper_count(), get_papers_snapshot(), and get_known_paper_ids() where needed.

Changes

WG21Index Encapsulation

Layer / File(s) Summary
WG21Index storage and accessors
src/paperscout/sources.py
WG21Index stores papers in _papers, refresh returns snapshot copies, _commit_index swaps the backing dict, and reader helpers expose counts and snapshots.
Consumer and guidance updates
src/paperscout/__main__.py, src/paperscout/monitor.py, src/paperscout/concurrency.py, CONTRIBUTING.md, docs/architecture.md
paper_count_fn and Scheduler.seed() use paper_count(), and the concurrency docs and comments rename the internal state references.
Test migration to _papers and accessors
tests/test_callback_protocols.py, tests/test_monitor.py, tests/test_sources.py
Tests update WG21 mocks and assertions to use _papers, paper_count(), get_papers_snapshot(), get_known_paper_ids(), and _replace_papers_for_test().

Estimated code review effort: 3 (Moderate) | ~25 minutes

Possibly related PRs

Suggested reviewers: wpak-ai

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 30.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: encapsulating WG21Index.papers behind snapshot and count accessors.
Description check ✅ Passed The description covers the changes and related issue, but it omits the template's test plan section.
Linked Issues check ✅ Passed The PR implements the issue's requirements: private backing store, snapshot/count accessors, atomic swap, reader migrations, and test updates.
Out of Scope Changes check ✅ Passed The docs and test edits stay aligned with the encapsulation and concurrency work, with no clear unrelated changes.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/encapsulate-wg21-papers

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

🧹 Nitpick comments (1)
src/paperscout/monitor.py (1)

372-379: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Redundant paper_count() call.

wg21.paper_count() is called twice: once to compute paper_count (line 374) and again at line 379 for the log message, even though paper_count == wg21.paper_count() whenever wg21 is not None. Reuse the already-computed value.

♻️ Proposed simplification
         if self.cfg.enable_bulk_wg21 and wg21 is not None:
-            log.info("SEED  wg21.link loaded  papers=%d", wg21.paper_count())
+            log.info("SEED  wg21.link loaded  papers=%d", paper_count)
🤖 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 `@src/paperscout/monitor.py` around lines 372 - 379, Reuse the already computed
paper_count in the wg21 branch of monitor.py instead of calling
wg21.paper_count() again in the log message. Update the logic around
self._wg21_index() so the SEED wg21.link loaded log uses paper_count directly
when wg21 is not None, keeping the existing fallback behavior unchanged.
🤖 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 `@src/paperscout/sources.py`:
- Line 77: The refresh methods are returning the live backing map via
IndexRefreshResult, so callers can mutate internal state through result.papers.
Update the return sites in the refresh paths that use _commit_index() to pass a
copied snapshot of self._papers instead of the original dict, preserving the
snapshot contract while keeping IndexRefreshResult and the related accessors
unchanged.

---

Nitpick comments:
In `@src/paperscout/monitor.py`:
- Around line 372-379: Reuse the already computed paper_count in the wg21 branch
of monitor.py instead of calling wg21.paper_count() again in the log message.
Update the logic around self._wg21_index() so the SEED wg21.link loaded log uses
paper_count directly when wg21 is not None, keeping the existing fallback
behavior unchanged.
🪄 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: defaults

Review profile: CHILL

Plan: Pro

Run ID: b702d900-5981-4839-8632-58dc03e88e02

📥 Commits

Reviewing files that changed from the base of the PR and between 42477ba and d26f855.

📒 Files selected for processing (9)
  • CONTRIBUTING.md
  • docs/architecture.md
  • src/paperscout/__main__.py
  • src/paperscout/concurrency.py
  • src/paperscout/monitor.py
  • src/paperscout/sources.py
  • tests/test_callback_protocols.py
  • tests/test_monitor.py
  • tests/test_sources.py

Comment thread src/paperscout/sources.py Outdated

@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.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/paperscout/sources.py (1)

94-103: 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Restore stale=True for the persisted-cache fallback
This branch runs after a live download failure, so returning IndexRefreshResult(..., stale=False) makes degraded data look fresh and suppresses the stale-path handling in src/paperscout/monitor.py. The fallback should keep stale=True.

🤖 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 `@src/paperscout/sources.py` around lines 94 - 103, The persisted-cache
fallback in the index refresh flow is incorrectly marking degraded data as
fresh. In the branch inside the refresh logic where `self._cache.read()`
succeeds and `_parse_raw`, `_commit_index`, and the `INDEX-STALE-FALLBACK`
warning are used, change the returned `IndexRefreshResult` to preserve the stale
state so downstream handling in `monitor.py` still treats it as stale.
🤖 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.

Outside diff comments:
In `@src/paperscout/sources.py`:
- Around line 94-103: The persisted-cache fallback in the index refresh flow is
incorrectly marking degraded data as fresh. In the branch inside the refresh
logic where `self._cache.read()` succeeds and `_parse_raw`, `_commit_index`, and
the `INDEX-STALE-FALLBACK` warning are used, change the returned
`IndexRefreshResult` to preserve the stale state so downstream handling in
`monitor.py` still treats it as stale.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: d8a42518-9003-4dbd-82b4-fd65b08efed5

📥 Commits

Reviewing files that changed from the base of the PR and between d26f855 and 43b1ec9.

📒 Files selected for processing (1)
  • src/paperscout/sources.py

@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.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/paperscout/sources.py (1)

94-103: 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Return stale=True when falling back to persisted cache
src/paperscout/sources.py:94-103 — this branch runs only after the download fails and the code reuses persisted cache, so stale=False hides degraded data from monitor._poll_sources(), which keys _index_stale and the INDEX-STALE warning off refresh_result.stale.

Proposed fix
-                return IndexRefreshResult(dict(self._papers), stale=False)
+                return IndexRefreshResult(dict(self._papers), stale=True)
🤖 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 `@src/paperscout/sources.py` around lines 94 - 103, The fallback branch in
`PaperIndexSource` currently returns `IndexRefreshResult(..., stale=False)` even
though it is reusing persisted cache after a failed download. Update the
stale-cache fallback path in `sources.py` so the `IndexRefreshResult` returned
from this branch is marked `stale=True`, keeping `monitor._poll_sources()` and
the `INDEX-STALE` warning aligned with degraded data. Use the existing `stale`
handling around `_cache.read()`, `_parse_raw()`, and `_commit_index()` to locate
the branch and change only the returned refresh result state.
🤖 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.

Outside diff comments:
In `@src/paperscout/sources.py`:
- Around line 94-103: The fallback branch in `PaperIndexSource` currently
returns `IndexRefreshResult(..., stale=False)` even though it is reusing
persisted cache after a failed download. Update the stale-cache fallback path in
`sources.py` so the `IndexRefreshResult` returned from this branch is marked
`stale=True`, keeping `monitor._poll_sources()` and the `INDEX-STALE` warning
aligned with degraded data. Use the existing `stale` handling around
`_cache.read()`, `_parse_raw()`, and `_commit_index()` to locate the branch and
change only the returned refresh result state.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 3397cd48-3905-4a6c-b4ec-a5d947835327

📥 Commits

Reviewing files that changed from the base of the PR and between 43b1ec9 and 3792d8e.

📒 Files selected for processing (1)
  • src/paperscout/sources.py

@henry0816191

Copy link
Copy Markdown
Collaborator Author

@coderabbitai full review again

@coderabbitai

coderabbitai Bot commented Jul 9, 2026

Copy link
Copy Markdown

@henry0816191 Sure, I'll do a full review of the PR again.

✅ Action performed

Full review finished.

@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: 1

🧹 Nitpick comments (1)
tests/test_callback_protocols.py (1)

46-69: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Wire paper_count on the mock for consistency.

wg21 is a MagicMock(spec=WG21Index) with _papers set directly, but paper_count isn't wired, so wg21.paper_count() returns a generic MagicMock, not a real count derived from _papers. Scheduler.seed() uses this value in a %d-formatted log message. Consider mirroring the pattern used in tests/test_monitor.py::_make_mock_wg21, which wires mock.paper_count = lambda: len(mock._papers).

🔧 Suggested fix
     wg21 = MagicMock(spec=WG21Index)
     wg21.source_id = SOURCE_WG21_INDEX
     wg21.fetch = AsyncMock(return_value=IndexRefreshResult({}, stale=False))
     wg21.diff = MagicMock(return_value=DiffResult([], []))
+    wg21._papers = {}
+    wg21.paper_count = lambda: len(wg21._papers)
🤖 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 `@tests/test_callback_protocols.py` around lines 46 - 69, Wire the WG21Index
mock in this test so Scheduler.seed() sees a real paper count instead of a
generic MagicMock: in the setup for wg21, keep the existing _papers assignment
but also make paper_count return the length of _papers, matching the pattern
used by _make_mock_wg21 in tests/test_monitor.py. This will ensure the %d log
path in Scheduler and poll_once() behave consistently with the mock state.
🤖 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 `@src/paperscout/sources.py`:
- Around line 195-202: The atomic publish in the index commit path is reusing
the caller-owned papers dict as the live backing store, which allows later
external mutation to affect the index. In the commit/publish flow around the
atomic swap in the paper index class methods such as _commit_index(),
_parse_and_index(), and the _papers assignment, create owned copies of the maps
before storing them so the published snapshot is detached from caller state
while preserving the same atomic reference swap behavior.

---

Nitpick comments:
In `@tests/test_callback_protocols.py`:
- Around line 46-69: Wire the WG21Index mock in this test so Scheduler.seed()
sees a real paper count instead of a generic MagicMock: in the setup for wg21,
keep the existing _papers assignment but also make paper_count return the length
of _papers, matching the pattern used by _make_mock_wg21 in
tests/test_monitor.py. This will ensure the %d log path in Scheduler and
poll_once() behave consistently with the mock state.
🪄 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: defaults

Review profile: CHILL

Plan: Pro

Run ID: e9f7a4bb-76fa-4f75-a140-cf4757ae30ae

📥 Commits

Reviewing files that changed from the base of the PR and between 42477ba and 4b73b0b.

📒 Files selected for processing (9)
  • CONTRIBUTING.md
  • docs/architecture.md
  • src/paperscout/__main__.py
  • src/paperscout/concurrency.py
  • src/paperscout/monitor.py
  • src/paperscout/sources.py
  • tests/test_callback_protocols.py
  • tests/test_monitor.py
  • tests/test_sources.py

Comment thread src/paperscout/sources.py Outdated
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.

Encapsulate WG21Index.papers; enforce access via existing snapshot accessors

1 participant