Skip to content

fix(storage): Stop profile publication from writing canonical usage - #5386

Merged
Sinity merged 1 commit into
masterfrom
fix/profile-usage-owner
Sep 22, 2026
Merged

Sinity merged 1 commit into
masterfrom
fix/profile-usage-owner

Conversation

@Sinity

@Sinity Sinity commented Sep 22, 2026

Copy link
Copy Markdown
Owner

Summary

polylogue-bp12n.1 AC2 and the refresh-wrapper half of AC7. The bead's rollup read "3/3 children complete — eligible for close"; that was wrong, and this is the half that is now actually true.

Problem (at ba2c63c47)

PR #5312 cleaned only the prepared publisher. _refresh_provider_usage_rollup + _stamp_refreshed_usage_bindings survived in the rebuild_session_insights_sync/_async chunk loops, reachable from publish_session_profile and from InsightsRebuildActuator.apply — a real mutation actuator. So publishing a profile performed a hidden committed write to canonical usage.

Solution

The reconciliation moves to storage/derived/session/usage_rollup.py as reconcile_session_usage_rollup(_async) / reconcile_session_usage_rollups(_async) — aggregation, provider reconciliation, token lanes and catalog repricing moved verbatim, with no provider total re-summed a second way.

The rebuild keeps it behind an explicit reconcile_usage_rollup keyword: the bulk index rebuild commits rollup rows + binding stamp + profiles in one chunk transaction (AC2's joint-publication branch), the mutation actuator states that choice explicitly, and publish_session_profile passes False.

Because the profile binding covers the rollup's inputs and not its rows, publish_session_profile additionally refuses — returns False, commits nothing — when inspect_session_usage_rollups does not report VALID. The prerequisite domain is then the only writer of canonical usage.

Anti-vacuity — two mutations executed

  1. Revert to reconcile_usage_rollup=True: test_profile_publish_writes_no_usage FAILED — At index 0 diff: ('claude-sonnet-4-5', 1200, ...) != ('claude-sonnet-4-5', 10200, ...). The skewed 10200 was silently reconciled back to the ingest total inside a profile publication.
  2. Delete the prerequisite guard: test_profile_needs_a_settled_rollup FAILED — assert publish_session_profile(...) is False → AssertionError: assert True is False.

Both restored; the file returns 8 passed.

The skew in mutation 1 is load-bearing and worth naming: it edits a session_model_usage row directly, leaving the binding untouched so the rollup still inspects VALID. On a fresh archive whose rollup already agrees with its inputs, the refresh would rewrite identical numbers and the test would have stayed green under its own mutation. Opposite direction is pinned in both tests — a blanket refusal fails them.

Verification

Exit codes read directly from $?, not through a filter.

devtools test <10 selections: usage rollup, profile derivation, partition convergence,
  insight refresh, model-usage consistency, mutation actuators, parallel fanout,
  rebuild progress, insight readiness, verify-rebuild-routes>
    -> exit 0, 207 passed in 123.11s
  base ba2c63c47, identical selection
    -> exit 0, 205 passed   (207 = 205 + exactly the two new tests; no inherited red)

devtools verify --quick            exit 0, 25 gates (base control also exit 0)
devtools render all --check        exit 0, grep -c 'out of sync' = 0

Route census unchanged (gate rebuild-routes ok). Writer-module census unchanged — rebuild.py and usage_rollup.py were both already declared tiers:[index], so moving DML between them moves no baseline (gate layering ok).

Schema identity

All six changed production files are closure IN, so membership is unchanged and gate schema-closure passes with no --update. INDEX moves dba2f05e… → df72816c…; OPS unchanged at 86866235…. Must land before a fresh rebuild starts, never during one. No INDEX_SCHEMA_VERSION bump — that is the coordinator's per-wave call, not a per-lane one.

AC4 is not addressed, and its text needs repair before anyone dispatches it

The _STALE marker gates are live production, not dead code: operations/session_profile_convergence.py passes both marker connections into SessionProfileDerivation, and two gates consume them (inspect and selected_part_facts). _marker_assertions_present counts rows with no status predicate, and lower_markers preserves every terminal agent judgment as a row — so a human judgment does not trip the gate; only a genuinely absent row does, i.e. lowering never ran or failed. That is exactly the livelock AC4 names: a SessionProfileMarkerLoweringError leaves a partition that is current by its own binding reported STALE, so every later pass republishes the whole profile family purely to retry a user-tier write. No delivery cursor exists — applied_position / source_batch / marker_cursor / stream_position return nothing across archive_tiers and markers/.

On the authoring conflict I flagged when dispatching: AC4's "user effects and applied stream position commit together" reads as one commit across source.db and user.db, which SQLite cannot do — but the bead's own design field disambiguates it the other way ("the user consumer reads a bounded contiguous batch above its own last applied source position … under its one real user-db transaction … and advances the applied position"). So the position is a single-file user.db commit and the AC is under-specified, not impossible. The repair is to restate AC4 in the design's terms.

Residuals

  • AC7's other half (removing the marker-presence gate and the full-block-text rescan) is blocked behind AC4; deleting the gate before the batch/cursor consumer exists would drop marker delivery entirely.
  • publish_session_profile stays exported with route unreached in the census (tests only; the daemon owner uses the prepared route). Retiring it also means moving ~20 test call sites — a separate cut.
  • test_a_drifted_rollup_no_longer_dooms_the_first_profile_computation has a 127-character nodeid and cannot be selected by id (pre-existing; see polylogue-5coki). The two new tests are 96 and 95.

🤖 Generated with Claude Code

https://claude.ai/code/session_01ToWqpkXotr573DiQex5vcy

PR #5312 removed the hidden usage commit from the *prepared* session-profile
publisher only. The same reconciliation survived on the unprepared route:
`publish_session_profile` rebuilds through `rebuild_session_insights_sync`,
whose chunk loop re-aggregated `session_model_usage` and stamped the usage
domain's binding for every session it touched. A call whose subject is a
profile therefore rewrote canonical usage and certified a rollup no caller
asked it to reconcile — and could still return False afterwards, which is the
untruthful refusal polylogue-bp12n.1 AC2 forbids.

`SESSION_USAGE_ROLLUP_DOMAIN` already exists and is already a declared
prerequisite of the profile domain, so the reconciliation now lives there
outright: `_refresh_provider_usage_rollup`, its async twin and the binding
stamp move out of `rebuild.py` into `usage_rollup.py` as
`reconcile_session_usage_rollup(s)`, verbatim aggregation, provider
reconciliation, disjoint token lanes and catalog repricing. This also removes
the inverted lazy import `usage_rollup` needed to call back into `rebuild`.

The rebuild keeps the stage under an explicit `reconcile_usage_rollup` flag:
a bulk index rebuild owns both jobs and commits the rollup rows, their stamp
and the profiles built from them in one chunk transaction, and the mutation
actuator states that choice rather than inheriting it. A profile publisher
passes False, and because the profile's stored binding covers the rollup's
inputs rather than its rows, `publish_session_profile` refuses a session whose
rollup the prerequisite has not settled instead of silently publishing against
superseded cost values. Both refusals now commit nothing.

Verification: devtools test over the session profile/rollup/partition/insight
rebuild and mutation-actuator suites (185 passed), plus the wider readiness /
rebuild-route / actuator route selection (248 passed); devtools verify --quick;
devtools render all --check (0 "out of sync"). Anti-vacuity executed, not
asserted: passing `reconcile_usage_rollup=True` from `publish_session_profile`
turns the skewed rollup 10200 back into the ingest total 1200 inside a profile
publication (test_profile_publish_writes_no_usage red); deleting the
prerequisite guard publishes against a MISSING rollup
(test_profile_needs_a_settled_rollup red). The skew is load-bearing — on an
archive whose rollup already agrees with its inputs a refresh rewrites the same
numbers and neither test could tell the fix from the defect.

Derived index schema identity moves dba2f05e -> df72816c (ops unchanged); all
six changed production files were already closure IN, so membership is
unchanged and no --update is needed.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01ToWqpkXotr573DiQex5vcy
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 22, 2026 •

Copy link
Copy Markdown

Codex Review Summary

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-22T03:34:32.411202Z 3688078 PR opened
🔒 Security Review ✅ Completed 2026-09-22T03:37:10.844951Z 3688078 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.

@coderabbitai

coderabbitai Bot commented Sep 22, 2026

Copy link
Copy Markdown

Warning

Review limit reached

Next included review available in 13 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

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

Learn how review limits work.

Review configuration:

⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Advanced

Run ID: 5db16078-59aa-4596-b77d-0f706c5c0a58

📥 Commits

Reviewing files that changed from the base of the PR and between a2df1fc and 3688078.

📒 Files selected for processing (10)
  • polylogue/operations/mutation_actuators.py
  • polylogue/storage/derived/session/derivation.py
  • polylogue/storage/derived/session/input_binding.py
  • polylogue/storage/derived/session/rebuild.py
  • polylogue/storage/derived/session/status.py
  • polylogue/storage/derived/session/usage_rollup.py
  • tests/unit/core/test_insight_readiness.py
  • tests/unit/storage/test_session_partition_convergence.py
  • tests/unit/storage/test_session_profile_derivation.py
  • tests/unit/storage/test_session_usage_rollup_derivation.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 enabled auto-merge (squash) September 22, 2026 03:31
@Sinity
Sinity merged commit 785a2e0 into master Sep 22, 2026
3 checks passed
@Sinity
Sinity deleted the fix/profile-usage-owner branch September 22, 2026 03:42
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