Skip to content

fix(insights): populate terminal_state_method provenance for session profiles - #3344

Merged
Sinity merged 1 commit into
masterfrom
fix/terminal-state-method-provenance
Jul 27, 2026
Merged

Sinity merged 1 commit into
masterfrom
fix/terminal-state-method-provenance

Conversation

@Sinity

@Sinity Sinity commented Jul 27, 2026

Copy link
Copy Markdown
Owner

Summary

session_profiles.terminal_state_method has existed in the canonical
index.db DDL for a while but was never written by any code path, so it
read 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_method
is NULL for 100% of labeled sessions (4,371 error_left + 4,136
clean_finish, v14 materialization). Root cause traced to
archive/session/runtime.py::_terminal_state, which only ever returned
(state, confidence, evidence) — there was never a method value computed
anywhere, so the SessionProfile domain model, SessionProfileRecord,
the _SESSION_PROFILE_BASE_COLUMNS write list, and the explicit-column
SELECTs in archive_tiers/archive.py had nothing to carry. This blocks
the D3 mass-annotation campaign (tracked on polylogue-rxdo), which
deliberately withholds terminal-state annotation until structural-outcome
provenance is auditable — without terminal_state_method there's no way
to tell whether a label rests on the now-deleted prose-keyword heuristic
or genuine structural evidence.

Solution

  • _terminal_state now returns a 4-tuple including method, drawn from 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 through every hop: 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 inside
    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), the same framing as
    the session_profiles token-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

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
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

Extended the existing per-branch terminal_state unit tests in
test_semantic_facts.py with a terminal_state_method assertion each
(one per structural detection rule), and added two new end-to-end
DB-materialization regression tests in test_session_insight_refresh.py
that read the persisted session_profiles.terminal_state_method column
back 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_tier both
before and after this change (verified via git stash) — not touched by
this PR.

Acceptance criteria (polylogue-vhjs)

  1. Satisfied — every _terminal_state return path now carries a
    method token from the closed vocabulary, wired through to the
    persisted terminal_state_method 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 after real
    materialization.
  3. Deferred — no rigor-matrix doc currently references
    terminal_state_method by name; docs/library-api.md (the doc that
    enumerates SessionProfileInsight fields) was updated instead. A
    dedicated rigor-matrix entry can follow if the annotation program
    (polylogue-rxdo) wants one.

Ref polylogue-vhjs

…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]>
@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, you can upgrade your account or add credits to your account and enable them for code reviews in your settings.

@coderabbitai

coderabbitai Bot commented Jul 27, 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: 10 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: d2464a3a-72ce-4840-83a9-4b2d5e6dbe4f

📥 Commits

Reviewing files that changed from the base of the PR and between 13924f5 and 17857ca.

📒 Files selected for processing (15)
  • docs/library-api.md
  • polylogue/archive/session/documents.py
  • polylogue/archive/session/models.py
  • polylogue/archive/session/runtime.py
  • polylogue/insights/archive.py
  • polylogue/insights/archive_models.py
  • polylogue/storage/insights/session/profiles.py
  • polylogue/storage/insights/session/rebuild.py
  • polylogue/storage/insights/session/records.py
  • polylogue/storage/insights/session/storage.py
  • polylogue/storage/sqlite/archive_tiers/archive.py
  • polylogue/storage/sqlite/queries/mappers_insight_profiles.py
  • tests/unit/core/test_semantic_facts.py
  • tests/unit/storage/test_session_insight_profiles.py
  • tests/unit/storage/test_session_insight_refresh.py
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/terminal-state-method-provenance

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 3972815 into master Jul 27, 2026
3 checks passed
@Sinity
Sinity deleted the fix/terminal-state-method-provenance branch July 27, 2026 17:50
Sinity added a commit that referenced this pull request Jul 27, 2026
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]>
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