Skip to content

perf(schemas): add bounded __bool__ to replay membership views - #3565

Merged
Sinity merged 2 commits into
masterfrom
feature/perf/replay-membership-bool
Aug 2, 2026
Merged

Sinity merged 2 commits into
masterfrom
feature/perf/replay-membership-bool

Conversation

@Sinity

@Sinity Sinity commented Aug 2, 2026

Copy link
Copy Markdown
Owner

Summary

Adds bounded __bool__ to the four lazy membership views in polylogue/schemas/generation/replay.py, fixes ArtifactMemberships.__getitem__'s full-materialization on bounded access, and closes the last "loaded gun" from PR #3546 by making ReplayableRecordSamples.__getitem__'s slice path bounded too.

Problem

polylogue-5svw0 (Fable perf audit findings F1+F2, 2026-08-02): ArtifactMemberships, MembershipSamples, MembershipSessionIds, and MembershipObservedAts all had O(n) __len__ with no __bool__, so if view: / bool(view) fell back to Sequence/Collection's default truthiness protocol — a full rescan of the filtered membership source just to answer "is this empty?". Same defect class as PR #3546 (ReplayableRecordSamples.__bool__), currently latent in this repo (no live call site does implicit truthiness on these views yet) but "one wrong future call site from recreating #3546" per the audit finding. ArtifactMemberships.__getitem__ also fully materialized via list(self) for a single bounded index/slice access.

Separately: ReplayableRecordSamples.__getitem__'s slice path (sampling_extract.py) still called index.indices(len(self)) — forcing a full file decode — even for the common forward-bounded shape (samples[:5]), after PR #3546 had already added __bool__ but left the slice path alone. No current caller slices it, but observation_runtime.py already had to route around this exact shape with islice + a warning comment.

Solution

  • polylogue/core/common.py: two new shared helpers.
    • peek_truthy(iterable) answers truthiness from a bounded single-item peek instead of falling back to __len__.
    • forward_bounded_slice(index) resolves a slice's (start, stop, step) via itertools.islice without needing the sequence's true length, for the common non-negative start/stop + positive-step shape; returns None (caller falls back to len()-based resolution) for a slice that genuinely needs true length (negative bound, non-positive step).
  • polylogue/schemas/generation/replay.py: all four membership views get __bool__ via peek_truthy(). ArtifactMemberships.__getitem__ now uses forward_bounded_slice() for slices and a bounded islice for non-negative int indices, only falling back to full materialization for negative indices (which genuinely need the true length to normalize).
  • polylogue/archive/raw_payload/sampling_extract.py: ReplayableRecordSamples.__getitem__'s slice path now tries forward_bounded_slice() first, avoiding len(self) (a full file decode) for the common samples[:N] shape.

No current call site suffers from the membership-view gap yet (grepped for implicit truthiness on these classes across the repo — none found), so this is a preventive fix per the audit's own framing, not a live perf regression fix.

Verification

  • New tests:
    • tests/unit/core/test_common.py — unit coverage for peek_truthy/forward_bounded_slice.
    • tests/unit/core/test_schema_replay_memberships.py — bounded-access assertions for all four replay.py views, using an instrumented counting membership source that records how many items were actually touched.
    • tests/unit/core/test_sampling.py — bounded-slice/bool coverage for ReplayableRecordSamples with instrumented JSON-decode counting.
    • Anti-vacuity check: git stash'd the three production files and reran the new tests — 7 failures + 1 import error (only test_bool_still_does_not_decode_full_file stayed green, since ReplayableRecordSamples.__bool__ already existed from PR perf(schemas): stop full-corpus schema inference rescanning raw files #3546). Restored the fix and reran green.
  • devtools test tests/unit/core/test_common.py tests/unit/core/test_schema_replay_memberships.py tests/unit/core/test_sampling.py tests/unit/core/test_schema_workload_profiles.py tests/unit/core/test_schema_observation_journal.py tests/unit/core/test_schema_generation.py — 129 passed, 1 skipped, 1 pre-existing unrelated failure (TestLoadSamplesFromDb::test_schema_observation_records_included_and_decode_failed_raws, confirmed via git stash against the unmodified checkout).
  • devtools verify --quick — clean, exit 0.

Ref polylogue-5svw0

Co-Authored-By: Claude [email protected]

@coderabbitai

coderabbitai Bot commented Aug 2, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

@Sinity, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 8 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 8b9d0abc-4b7f-4dbd-b994-3953e251d44b

📥 Commits

Reviewing files that changed from the base of the PR and between e7e0193 and 654038d.

📒 Files selected for processing (7)
  • .beads/issues.jsonl
  • polylogue/archive/raw_payload/sampling_extract.py
  • polylogue/core/common.py
  • polylogue/schemas/generation/replay.py
  • tests/unit/core/test_common.py
  • tests/unit/core/test_sampling.py
  • tests/unit/core/test_schema_replay_memberships.py

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.

Sinity added a commit that referenced this pull request Aug 2, 2026
Ref polylogue-5svw0

Co-Authored-By: Claude <[email protected]>
Sinity added a commit that referenced this pull request Aug 2, 2026
Ref polylogue-5svw0

Co-Authored-By: Claude <[email protected]>
@Sinity
Sinity force-pushed the feature/perf/replay-membership-bool branch from 98afe01 to e95ab96 Compare August 2, 2026 14:23
Sinity and others added 2 commits August 2, 2026 16:58
Problem

polylogue-5svw0 (Fable perf audit F1+F2): ArtifactMemberships,
MembershipSamples, MembershipSessionIds, and MembershipObservedAts
(polylogue/schemas/generation/replay.py) all had O(n) __len__ with no
__bool__, so `if view:` / `bool(view)` fell back to Sequence/Collection's
default truthiness protocol -- a full rescan of the filtered membership
source just to answer "is this empty?". Same defect class as PR #3546
(ReplayableRecordSamples.__bool__), currently latent (no live call site
does implicit truthiness on these views yet) but "one wrong future call
site from recreating #3546" per the audit finding.
ArtifactMemberships.__getitem__ also fully materialized via list(self)
for a single bounded index/slice access. Separately,
ReplayableRecordSamples.__getitem__'s slice path (sampling_extract.py)
still called index.indices(len(self)) -- forcing a full file decode --
even for the common forward-bounded shape (samples[:5]); no current
caller slices it, but observation_runtime.py already had to route
around it with islice + a warning comment.

Solution

- polylogue/core/common.py: two new shared helpers. peek_truthy()
  answers truthiness from a bounded single-item peek instead of
  __len__. forward_bounded_slice() resolves a slice's (start, stop,
  step) via itertools.islice without needing the sequence's true
  length, for the common non-negative start/stop + positive-step
  shape; returns None (fall back to len()-based resolution) for a
  slice that genuinely needs true length (negative bound, non-positive
  step).
- replay.py: all four membership views get __bool__ via peek_truthy().
  ArtifactMemberships.__getitem__ now uses forward_bounded_slice() for
  slices and a bounded islice for non-negative int indices, only
  falling back to full materialization for negative indices.
- sampling_extract.py: ReplayableRecordSamples.__getitem__'s slice path
  now tries forward_bounded_slice() first, avoiding len(self) (a full
  file decode) for the common samples[:N] shape.

Verification

- New tests: tests/unit/core/test_common.py (peek_truthy,
  forward_bounded_slice unit coverage), tests/unit/core/
  test_schema_replay_memberships.py (bounded-access assertions for all
  four replay.py views, using an instrumented counting membership
  source), tests/unit/core/test_sampling.py (bounded-slice/bool
  coverage for ReplayableRecordSamples with instrumented JSON-decode
  counting). Confirmed each new test genuinely fails without the fix
  (git stash of the three production files reproduced 7 failures + 1
  import error; only test_bool_still_does_not_decode_full_file stayed
  green, since ReplayableRecordSamples.__bool__ already existed from
  PR #3546).
- `devtools test tests/unit/core/test_common.py
  tests/unit/core/test_schema_replay_memberships.py
  tests/unit/core/test_sampling.py
  tests/unit/core/test_schema_workload_profiles.py
  tests/unit/core/test_schema_observation_journal.py
  tests/unit/core/test_schema_generation.py` --
  129 passed, 1 skipped, 1 pre-existing unrelated failure (confirmed
  via git stash against the unmodified checkout:
  TestLoadSamplesFromDb::test_schema_observation_records_included_and_decode_failed_raws).
- `devtools verify --quick` -- clean, exit 0.

Ref polylogue-5svw0

Co-Authored-By: Claude <[email protected]>
Ref polylogue-5svw0

Co-Authored-By: Claude <[email protected]>
@Sinity
Sinity force-pushed the feature/perf/replay-membership-bool branch from e95ab96 to 654038d Compare August 2, 2026 15:01
@Sinity
Sinity merged commit 8d37ce8 into master Aug 2, 2026
3 checks passed
@Sinity
Sinity deleted the feature/perf/replay-membership-bool branch August 2, 2026 15:03
Sinity added a commit that referenced this pull request Aug 2, 2026
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