Repository navigation
fix(insights): populate terminal_state_method provenance for session profiles - #3344
Conversation
…profiles Problem: session_profiles.terminal_state_method has existed in the canonical index.db DDL (a plain TEXT column, no CHECK) since it was added alongside terminal_state_confidence/terminal_state_evidence_json, but no writer ever set it. Live-archive audit (polylogue-vhjs) found it NULL for all 8,507 labeled sessions (4,371 error_left + 4,136 clean_finish, v14 materialization). Root cause: the SessionProfile domain model (archive/session/models.py), SessionProfileRecord, the _SESSION_PROFILE_BASE_COLUMNS write list, and the SQL SELECTs in archive_tiers/archive.py simply never carried the field -- _terminal_state (archive/session/runtime.py) only ever returned (state, confidence, evidence), so there was no method value anywhere to persist. This blocks the D3 mass-annotation campaign (recorded on polylogue-rxdo), which deliberately withholds terminal-state annotation until structural-outcome provenance is auditable. What changed: - _terminal_state now returns a 4-tuple including `method`, a closed vocabulary (TERMINAL_STATE_METHODS in runtime.py): pending_tool_blocks, pending_tool_events, action_outcome, event_status, last_message_role, no_signal, plus bounded_materialization for the rebuild.py large-session degraded fallback path. - Threaded terminal_state_method end-to-end: SessionProfile -> SessionProfileRecord -> _SESSION_PROFILE_BASE_COLUMNS write list -> SessionInferencePayload (public payload) -> the two native-column reconciliation sites (SessionProfileInsight.from_record and _session_profile_components_from_archive_row) -> the explicit-column SELECTs in archive_tiers/archive.py -> hydrate_session_profile. Added it to _INFERENCE_NATIVE_MIRRORED_FIELDS alongside terminal_state so it isn't double-persisted in inference_payload_json. - Derived-tier fix only: no schema bump, no migration. Existing NULL rows self-heal via the standard rebuild path (`polylogue ops reset --index && polylogued run`), same framing as the session_profiles token-column fix earlier this session (PR #3174) -- no manual backfill script needed or written. Verification: - devtools test tests/unit/core/test_semantic_facts.py tests/unit/storage/test_session_insight_refresh.py tests/unit/storage/test_session_insight_profiles.py tests/unit/sources/test_compaction.py tests/unit/insights/test_objective_posture.py tests/unit/insights/test_archive_rollups.py tests/unit/storage/test_session_profile_model_usage_consistency.py tests/unit/storage/test_session_insight_timeline_rows.py -> 168 passed. Extended existing terminal_state unit tests (one per structural rule) plus two new end-to-end DB-materialization tests asserting the persisted `session_profiles.terminal_state_method` column. - mypy --strict polylogue -> Success: no issues found in 1086 source files - ruff check / ruff format --check -> all checks passed - devtools verify --quick -> all 17 steps ok - devtools render all / render all --check -> in sync (docs/library-api.md updated with the new field) - Pre-existing, unrelated failure noted: tests/unit/storage/test_delegations_view.py (3 tests) fails on this checkout with "unknown database user_tier" both before and after this change (verified via git stash) -- not touched by this fix. AC honesty (polylogue-vhjs): 1. Satisfied -- every _terminal_state return path now carries a method from the closed vocabulary, wired through to the persisted column. 2. Satisfied -- test_semantic_facts.py asserts a distinct method per structural rule; test_session_insight_refresh.py adds two DB-level regression tests reading the native column back. 3. Deferred -- no rigor-matrix doc references terminal_state_method by name today; docs/library-api.md (the one doc enumerating SessionProfileInsight fields) was updated. A dedicated rigor-matrix entry can follow if the annotation program (polylogue-rxdo) wants one. Ref polylogue-vhjs Co-Authored-By: Claude <[email protected]>
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
|
Warning Review limit reached
Next review available in: 10 minutes Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. How can I continue?After more reviews become available, a review can be triggered using the 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 configurationConfiguration used: Path: .coderabbit.yaml Review profile: ASSERTIVE Plan: Pro Plus Run ID: 📒 Files selected for processing (15)
✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
Resolved a .beads/issues.jsonl merge conflict from switching back to master through a stale local branch (feature/docs/restore-drill-runbook): kept the guard-restored live state and re-added 5 records that were locally present but not yet reflected upstream (polylogue-0twa, lbgc, 57w4, a7uk, e6a0 — all real follow-up beads filed by this session's retriage/fix agents). Verified every line parses as JSON and cross-checked close states directly against live `bd show` before dropping the now-redundant stashes. Also closes polylogue-vhjs (PR #3344 merged: terminal_state_method provenance now populated for session profiles). Co-Authored-By: Claude <[email protected]>
Summary
session_profiles.terminal_state_methodhas existed in the canonicalindex.dbDDL for a while but was never written by any code path, so itread NULL for every labeled session (8,507 of them as of the 2026-07-17
audit). This wires the field end-to-end so it's actually populated going
forward.
Problem
Live-archive finding 2026-07-17 (polylogue-vhjs):
terminal_state_methodis NULL for 100% of labeled sessions (4,371
error_left+ 4,136clean_finish, v14 materialization). Root cause traced toarchive/session/runtime.py::_terminal_state, which only ever returned(state, confidence, evidence)— there was never a method value computedanywhere, so the
SessionProfiledomain model,SessionProfileRecord,the
_SESSION_PROFILE_BASE_COLUMNSwrite list, and the explicit-columnSELECTs inarchive_tiers/archive.pyhad nothing to carry. This blocksthe D3 mass-annotation campaign (tracked on polylogue-rxdo), which
deliberately withholds terminal-state annotation until structural-outcome
provenance is auditable — without
terminal_state_methodthere's no wayto tell whether a label rests on the now-deleted prose-keyword heuristic
or genuine structural evidence.
Solution
_terminal_statenow returns a 4-tuple includingmethod, drawn from aclosed vocabulary (
TERMINAL_STATE_METHODSinruntime.py):pending_tool_blocks,pending_tool_events,action_outcome,event_status,last_message_role,no_signal, plusbounded_materializationfor therebuild.pylarge-session degradedfallback path.
terminal_state_methodthrough every hop:SessionProfile->SessionProfileRecord->_SESSION_PROFILE_BASE_COLUMNSwrite list ->SessionInferencePayload(public payload) -> the two native-columnreconciliation sites (
SessionProfileInsight.from_recordand_session_profile_components_from_archive_row) -> the explicit-columnSELECTs inarchive_tiers/archive.py->hydrate_session_profile.Added it to
_INFERENCE_NATIVE_MIRRORED_FIELDS(alongsideterminal_state) so it isn't double-persisted insideinference_payload_json.rows self-heal via the standard rebuild path
(
polylogue ops reset --index && polylogued run), the same framing asthe
session_profilestoken-column fix earlier this session (fix(insights): aggregate session_profiles tokens from canonical usage substrate #3174).No manual backfill script was written or is needed.
Verification
Extended the existing per-branch
terminal_stateunit tests intest_semantic_facts.pywith aterminal_state_methodassertion each(one per structural detection rule), and added two new end-to-end
DB-materialization regression tests in
test_session_insight_refresh.pythat read the persisted
session_profiles.terminal_state_methodcolumnback after a real ingest+materialize pass.
Pre-existing, unrelated failure noted:
tests/unit/storage/test_delegations_view.py(3 tests) fails on this checkout with
unknown database user_tierbothbefore and after this change (verified via
git stash) — not touched bythis PR.
Acceptance criteria (polylogue-vhjs)
_terminal_statereturn path now carries amethod token from the closed vocabulary, wired through to the
persisted
terminal_state_methodcolumn.test_semantic_facts.pyasserts a distinct method perstructural rule;
test_session_insight_refresh.pyadds two DB-levelregression tests reading the native column back after real
materialization.
terminal_state_methodby name;docs/library-api.md(the doc thatenumerates
SessionProfileInsightfields) was updated instead. Adedicated rigor-matrix entry can follow if the annotation program
(polylogue-rxdo) wants one.
Ref polylogue-vhjs