Skip to content

feat(drive): add JSON-structural revision-diff classifier for Drive re-acquisition - #3687

Merged
Sinity merged 3 commits into
masterfrom
feature/storage/drive-structural-revision-diff
Aug 3, 2026
Merged

Sinity merged 3 commits into
masterfrom
feature/storage/drive-structural-revision-diff

Conversation

@Sinity

@Sinity Sinity commented Aug 3, 2026

Copy link
Copy Markdown
Owner

Summary

Adds a JSON-structural growth classifier for Drive/Gemini re-acquisition bytes and wires it into _bind_drive_revision_lineage so a genuine Drive re-acquisition growth (the live-attachment-backfill shape, not a byte-append) gets typed, predecessor-linked lineage instead of falling straight through to the byte-prefix classifier's ambiguous/quarantined outcome. This satisfies polylogue-1fijp AC (b) ("drive re-acquire with changed bytes produces typed lineage, not quarantined-unknown"), which PR #3668 explicitly left unattempted and PR #3656 explicitly deferred.

Problem

iter_drive_raw_data's live-attachment backfill (_inject_live_drive_attachment_bytes) re-serializes the WHOLE Gemini/AI-Studio JSON document on every re-acquisition pass that resolves a Drive-hosted attachment reference, rather than byte-appending at the document's end. PR #3656 (polylogue-sp72) proved this concretely with a real fixture threaded through the production write path (test_iter_drive_raw_data_two_revision_fixture_gets_real_lineage): the second raw's bytes are not a byte-prefix superset of the first's (not second_bytes.startswith(first_bytes)), so the archive's existing byte-prefix revision classifier (classify_historical_full_revision_streams, archive/revision_authority.py) lands revision_authority='quarantined' with no predecessor_raw_id for this exact, realistic shape — even though the underlying conversation genuinely only grew.

Solution

  • polylogue/sources/drive/structural_diff.py: classify_drive_structural_relation(old_bytes, new_bytes) decodes both payloads as JSON and proves whether new is a structural extension of old — every dict key/value and list element old asserts is still present, unchanged, in new; new dict keys, new trailing list elements (a conversation growing a chunkedPrompt.chunks entry), and null-to-populated values are allowed growth. Any changed or removed existing value breaks the proof and the relation is AMBIGUOUS — the same fail-closed posture as the byte-prefix classifier it complements, proven a different way. Pure function, no SQLite/archive coupling.
  • polylogue/pipeline/services/ingest_batch/_core.py: _bind_drive_revision_lineage now tries _drive_structural_growth_predecessor first — it looks for exactly one existing revision_kind='full' sibling raw for the logical source key whose bytes are a proven structural predecessor of the new raw's bytes. A unique match is bound directly as a FULL/ASSERTED revision with a real predecessor_raw_id and baseline_raw_id (ASSERTED, not BYTE_PROVEN — the proof is JSON-structural, not byte-level, and that vocabulary distinction is deliberate: BYTE_PROVEN is reserved for the bytes.startswith() relation elsewhere in archive/revision_authority.py/storage/sqlite/archive_tiers/raw_admission.py). Zero or multiple structural matches fall through unchanged to the existing byte-prefix quarantine-then-classify dance — strictly additive, no existing behavior narrowed.
  • Updated test_iter_drive_raw_data_two_revision_fixture_gets_real_lineage (PR fix(drive): let proven revision lineage bypass Drive freshness tie-break #3656's own end-to-end fixture, through the real _write_session production path) to assert the now-typed predecessor link instead of the documented residual gap it previously proved.
  • tests/unit/sources/test_drive_structural_diff.py: 15 focused tests on the classifier's semantics directly — trailing-append growth, dict-enrichment growth, combined growth, null→populated growth, new top-level keys, and the ambiguous/rejection cases (scalar mutation, removed/reordered elements, unrelated documents, non-JSON bytes, populated→null regression) — including one fixture built through the real production fetch_live_drive_attachment_bytes injector (not a hand-rolled approximation) and a two-generation sequential-fetch fixture.

Scope / what's not attempted

Per the bead's own fallback guidance, this is a scoped, real, well-tested slice, not the full raw-admission-chokepoint migration:

  • The new structural pre-check lives only in the Drive-specific _bind_drive_revision_lineage adapter function — it does not touch the shared _classify_raw_revision_cohort/classify_historical_full_revision_streams byte-prefix machinery used by every other origin's live-watch/rebuild-repair paths. That shared function's blast radius (every provider, both live-watch and rebuild-repair callers) made it too risky to modify for a Drive-only concern in this pass.
  • Drive acquisition is still not routed through admit_raw_observation() (PR feat(storage): add raw-admission chokepoint mechanism + atomic-read contract #3668's chokepoint) — it continues to use the bind_raw_revision/classify_raw_revision_cohort_for_live_watch adapter path, now with the structural pre-check inserted ahead of it. Full chokepoint migration for Drive remains open scope on polylogue-1fijp.
  • The 157 already-contaminated aistudio-drive raw pairs mentioned in PR fix(drive): let proven revision lineage bypass Drive freshness tie-break #3656 need a live re-measure/backfill after deploy — not attempted here (retroactive durable-tier cleanup needs its own explicit-consent process per this repo's schema regime).

Verification

  • mypy --strict polylogue/sources/drive/structural_diff.py polylogue/pipeline/services/ingest_batch/_core.py tests/unit/sources/test_drive_structural_diff.py tests/unit/sources/test_drive_ops.py — Success: no issues found in 4 source files.
  • devtools test tests/unit/sources/test_drive_structural_diff.py tests/unit/sources/test_drive_ops.py tests/unit/pipeline/test_ingest_batch.py — 89 passed.
  • devtools test -k "drive or revision_governance or raw_admission" — 308 passed, 1 failed (tests/integration/test_blob_lifecycle_e2e.py::TestOrphanedBlobsCatalogRouting::test_catalog_resolution_drives_end_to_end_cleanup), confirmed pre-existing/unrelated: reproduces identically on a clean checkout with no changes applied (same failure PR fix(drive): let proven revision lineage bypass Drive freshness tie-break #3656 already documented as pre-existing).
  • devtools verify --quick — exit_code: 0.

Ref polylogue-1fijp

Co-Authored-By: Claude [email protected]

Sinity added 3 commits August 3, 2026 22:22
Problem: iter_drive_raw_data's live-attachment backfill re-serializes the
whole Gemini/AI-Studio JSON document on each re-acquisition pass, so a
genuinely-grown conversation is never a byte-prefix superset of its
predecessor (confirmed by PR #3656/polylogue-sp72). The existing byte-prefix
revision classifier can only ever see this shape as ambiguous.

Solution: classify_drive_structural_relation compares two JSON documents
structurally -- new dict keys, trailing list growth, and null-to-populated
values are treated as growth; any changed/removed existing value is not.
Pure function, no SQLite/archive coupling.

Ref polylogue-1fijp
Growth cases (trailing list append, dict-key enrichment, null->populated,
combinations, and a fixture built through the real production
fetch_live_drive_attachment_bytes injector) and ambiguous cases (scalar
mutation, removed/reordered elements, unrelated documents, non-JSON bytes).

Ref polylogue-1fijp
Problem: _bind_drive_revision_lineage only ever proved lineage via the
byte-prefix classifier (classify_raw_revision_cohort_for_live_watch), so a
Drive re-acquisition whose bytes structurally grew via the live-attachment
backfill's whole-document re-serialization still landed
revision_authority='quarantined' with no predecessor_raw_id -- the
polylogue-1fijp AC (b) gap PR #3656 deferred.

Solution: _drive_structural_growth_predecessor looks for exactly one
existing 'full' sibling raw for the logical_source_key that is a JSON-
structural predecessor of the new raw's bytes. A unique match is bound
directly as a FULL/ASSERTED revision with a real predecessor_raw_id
(ASSERTED, not BYTE_PROVEN -- the proof is structural, not byte-level, and
that vocabulary distinction is deliberate per raw_admission.py's existing
arms). Zero or multiple matches fall through unchanged to the existing
byte-prefix quarantine-then-classify path -- strictly additive, no existing
behavior narrowed.

Updated test_iter_drive_raw_data_two_revision_fixture_gets_real_lineage
(polylogue-sp72's own end-to-end fixture through the real _write_session
production path) to assert the now-typed predecessor link instead of the
documented residual gap it previously proved.

Ref polylogue-1fijp
@coderabbitai

coderabbitai Bot commented Aug 3, 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: 39 minutes

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

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: 0db28f3e-6d94-4492-9255-af430457d741

📥 Commits

Reviewing files that changed from the base of the PR and between d080927 and 60a67b5.

📒 Files selected for processing (4)
  • polylogue/pipeline/services/ingest_batch/_core.py
  • polylogue/sources/drive/structural_diff.py
  • tests/unit/sources/test_drive_ops.py
  • tests/unit/sources/test_drive_structural_diff.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
Sinity merged commit a2acb12 into master Aug 3, 2026
3 checks passed
@Sinity
Sinity deleted the feature/storage/drive-structural-revision-diff branch August 3, 2026 20:35
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