Skip to content

fix(lineage): settle inherited prefixes through guard tables - #5803

Merged
Sinity merged 1 commit into
masterfrom
claude/ednjz-disk-settlement
Sep 29, 2026
Merged

Sinity merged 1 commit into
masterfrom
claude/ednjz-disk-settlement

Conversation

@Sinity

@Sinity Sinity commented Sep 29, 2026

Copy link
Copy Markdown
Owner

Summary

Settlement of a parent rewrite no longer holds any child's inherited prefix in Python (polylogue-ednjz, part 2 of 2; Codex finding m5Esr on #5685). The guard, the reanchor matching, the materialization plan and the before/after comparison stream through connection-private TEMP tables.

Problem

_capture_inherited_prefixes built each inheriting child's composed signature list and kept its inherited ids and signatures as Python tuples, for every child at once. _settle_inherited_prefixes composed each child again into a list. _materialize_inherited_prefix built full sources and plan lists and a remap dict. A whale parent with several forks multiplies a prefix larger than the writer's memory. All of this runs inside the parent's replacement transaction, so an OOM rolls back and the same revision fails again on every retry.

Solution

  • _iter_composed_rows streams (message_id, signature, owner) segment by segment, with _composed_db_signatures' composition rules; _disk_composed_db_signatures now builds on it.
  • The guard writes each child's pre-write composed transcript into TEMP before (child, ordinal, message_id, signature, owner) and keeps only the child-to-parent map in Python. Reference capture and the rewritten-owned id set are SQL over it.
  • Settlement streams each child's post-write composition into TEMP now:
    • _reanchor_inherited_rows matches lost rows forward by signature with indexed queries;
    • re-anchors and copies go to TEMP reanchors and copies;
    • _composition_matches_before compares the composition with before in lockstep.
  • _materialize_inherited_prefix builds TEMP sources (snapshot or live rows) and placed (_plan_prefix_positions: the same keep-or-renumber rule, in SQL). Each plan row is decided one at a time against TEMP taken natives and occurrences. The identity scope's digests stream.
  • _restore_message_refs, _restore_dispatch_refs and _copy_in_lineage look up copies and re-anchors in the TEMP tables. _session_message_ids, _inherited_entries, _reanchored_ids, _prefix_positions and _materialized_owner_in_lineage are removed.

Schema classification: no DDL; write.py is in the derived-identity closure, so this lands before the rebuild. Identities, positions, hashes and the recorded scope are unchanged for the same inputs.

Verification

  • agentctl … verify_quick: Success (job 2329).
  • agentctl … pytest_focused -- tests/unit/storage/test_lineage_normalization.py tests/unit/storage/test_branch_point_witness.py tests/unit/daemon/test_convergence_debt.py tests/unit/storage/test_archive_tiers_write.py (job 2335): everything passes except the 3 provider-usage tests that fail on master (polylogue-ezvw3). That includes every existing materialization, reanchor, dispatch, scope and deep-chain test and the part-1 regressions.
  • New test_settlement_streams_inherited_prefixes_through_the_guard_tables materializes three children of one parent while _composed_db_signatures (the list builder) raises inside capture and settlement.

Self-review

I read the full diff line by line against the replaced list-based code. What I checked:

  • Inherited rows. They are the composed rows the child does not own (owner <> child), the same predicate as the removed _inherited_entries, and they precede the child's own rows, so before ordinals number the prefix from 0.
  • Row sources. A snapshotted row is read from the snapshot and any other row from the live table. A missing row still raises by name.
  • Placement. _plan_prefix_positions reproduces _prefix_positions: unique (position, variant), non-decreasing in order and below the child's minimum means kept; otherwise it is a dense (session, position) slot by first appearance.
  • Identity decisions.
    • A native is kept only if INSERT OR IGNORE into taken inserts it.
    • A content copy takes the identity's next occurrence (the child's max+1, then +1 per copy).
    • The per-identity prefix ordinal counts every identity-bearing row.
    • copy_bases and the key and tail digests use _identity_sequence_digest's framing.
  • Reanchor matching. It keeps the removed function's semantics (surviving ids need no entry; lost ids match forward by signature among rows no pre-write id holds). It returns None when a lost row has no match, which the caller treats as "materialize". Previously an unmatched row made the all(...) check fail; the outcome is the same.
  • Cursors. They are iterated while writing only to other tables, and every per-child TEMP table is dropped before the next child.
  • Residual risks.
    • The recorded identity scope is still one JSON value holding the content-copy ordinals and a one-byte kind per prefix row. It is persisted state, and replay reads it whole.
    • _repair_stale_prefix_branch_points_db and _extract_prefix_tail, outside settlement, still compose into lists on the same parent write. They are pre-existing, and polylogue-fg2ar tracks them.

🤖 Generated with Claude Code

https://claude.ai/code/session_015pdKaZWyGe6Ngvsz468dzg

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, add credits to your account and enable them for code reviews in your settings.

@Sinity
Sinity enabled auto-merge (squash) September 29, 2026 04:38
@Sinity
Sinity merged commit ab28700 into master Sep 29, 2026
2 of 3 checks passed
@Sinity
Sinity deleted the claude/ednjz-disk-settlement branch September 29, 2026 04:42
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
🔒 Security Review ✅ Completed 2026-09-29T04:50:29.066099Z 801c53b PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

Sinity added a commit that referenced this pull request Sep 29, 2026
…#5809)

## Summary

Realigns three tests in
`tests/unit/storage/test_lineage_normalization.py` that fail on master
(polylogue-ezvw3). Each one encodes a contract that a named PR changed
deliberately. No production code changes.

## Problem

At master 1beb2e5 (AgentCTL job 2441), these fail:

-
`test_prefix_sharing_child_provider_usage_keeps_its_own_reported_totals`
(line 352). The child's `total_tokens`-only event (272 000) stores NULL
input, cached and output totals, but the test expects 0. #5530
(904b2f8) replaced `_payload_int` with `_payload_optional_int` in
`_provider_usage_event_row`, so an unreported lane now stays NULL.
- `test_child_before_parent_reextracts_provider_usage_tail` (line 1730).
The child rollup reads 0 instead of 130. The test put an all-zero
cumulative tick after the child's 160/30/25 cumulative. #5530 also
changed `_provider_usage_row_has_lane_totals` from "any non-zero" to
"any present", and added
`test_explicit_zero_cumulative_supersedes_prior_positive`. An explicit
zero cumulative is now a measurement, and the latest one wins.
- `test_late_parent_resolution_invalidates_child_derived_products` (line
873). The child is non-valid right after the test seeds a fresh profile.
#5525 (ca25740) added trigger-captured `session_profile_demand`, and
`_classify_partition_with_demand` reports a demanded session as stale
until publication consumes the demand. The test's seed never consumed
it.

None of the suspects named on the bead (#5803, #5792, #5764, #5691) is
the cause. #5803's PR body already lists these three as pre-existing
failures.

## Solution

- Prefix-sharing test: the `total_tokens`-only row now expects NULL
input, cached and output totals. The docstring cites #5530. The rollup
assertion (130/25/30) is unchanged, which proves the NULL row does not
displace the latest lane totals.
- Child-before-parent test: the same measured-zero tick, unchanged, now
comes before the child's latest cumulative. The rollup stays exactly
130/25/30 before and after the parent arrives. The re-extraction
assertion keeps both `cy` rows, including the tick's `request_id`, in
their new order. A stale comment about the evidence predicate is
corrected.
- Late-parent test: `_seed_fresh_session_products` deletes the session's
`session_profile_demand` row, as
`publish_prepared_session_insight_partition` does. The docstring now
says the retained-row assertion carries the anti-vacuity, because
re-captured demand alone would also mark the child stale.

Schema classification: none. No production file changes, so nothing
enters the derived-identity closure.

## Verification

- `agentctl job start polylogue pytest_focused --workspace
/realm/worktrees/polylogue-mh-lineage --
tests/unit/storage/test_lineage_normalization.py` → job 2573: `devtools
test: PASSED exit=0` (`105 passed` of 105, head 2d1d0a4).
- `agentctl job start polylogue verify_quick --workspace
/realm/worktrees/polylogue-mh-lineage` → job 2584: `verify: PASSED
exit=0` (head 2d1d0a4).

## Self-review

- **Contract, not bug.** For each failure I found the commit that
changed the behavior and checked that it was intended. #5530's summary
says "distinguish absent usage from explicit zero", and it added a test
asserting that a zero cumulative supersedes a prior positive one. #5525
added the demand table, its triggers and the demand-aware classifier on
purpose. No assertion was loosened. Every expected value is still exact.
- **Anti-vacuity.** Test 1 would fail if re-extraction kept the `c1`
row, dropped a `cy` row, or rebased the child against the parent. Test
2's NULL expectation fails if absent lanes are coerced to 0 again. Test
3's retained-row assertion fails if `_reextract_prefix_tail_db` stops
deleting profiles.
- **Siblings.** `_seed_fresh_session_products` has one caller. No other
test in this file seeds profiles or reads partition status.
- **Public boundary.** Test-only diff with synthetic fixtures.

## Residuals

- Under #5530, a zero cumulative that follows a positive one within a
session sets the rollup to 0. That is the declared contract. Whether a
real provider emits such a reset, and whether billing should then take
the maximum, is a product question this PR does not reopen.

polylogue-ezvw3

🤖 Generated with [Claude Code](https://claude.com/claude-code)

https://claude.ai/code/session_015pdKaZWyGe6Ngvsz468dzg

Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
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