Skip to content

fix(storage): route session native_id through single normalization helper - #3450

Merged
Sinity merged 3 commits into
masterfrom
feature/fix/session-identity-collisions
Jul 31, 2026
Merged

Sinity merged 3 commits into
masterfrom
feature/fix/session-identity-collisions

Conversation

@Sinity

@Sinity Sinity commented Jul 31, 2026

Copy link
Copy Markdown
Owner

Summary

Fixes polylogue-lyr2 (session identity computed two ways) with a single
source-of-truth helper mirroring the message-level ab5bad1f fix. Also
records verified live-archive forensics on polylogue-t83e (Drive-cache
Claude Code duplicates colliding with local session identity) — diagnosed,
not closed, since the correct fix needs acquisition-provenance plumbing
verification this PR's effort budget can't safely cover.

Problem

polylogue-lyr2: an invariants audit found _write_session
(archive_tiers/write.py) bound a session's native_id RAW into the
sessions INSERT, while session_id (used for cache keys, lineage lookups,
the FK-failure log message, and everything else downstream in the same
write) was computed via core.identity_law.session_id, which strips
whitespace. Same logical session, two spellings, one row — the exact
incident class that produced ab5bad1f's lost 10-hour rebuild, fixed at
message level via _stored_message_native_id + a property test, but never
given a session-level sibling. session_links.dst_native_id (parent
references written by _write_session_link) had the same raw-vs-stripped
mismatch against the sessions.native_id it must exact-match to resolve
lineage edges.

polylogue-t83e: a prior audit (hs3y, PR #3436) confirmed 12
~/.local/share/polylogue/drive-cache/gemini/*.jsonl.txt.json rows are
genuinely Claude-Code-shaped content (not a misclassification) and left
them alone. It never cross-checked those native_ids against local raws for
actual session_id collisions. This session verified, against the live
archive (read-only): 6 of those native_ids collide with genuinely-local
~/.claude/projects/... raws
— confirmed by SQL join, not inference —
and in all 6 the drive-cache copy currently wins the row. Byte-diffing one
pair in the blob store shows the drive copy is an exact byte prefix of the
local file (same conversation, stale/truncated snapshot, not a distinct
identity) with a concrete live consequence: sessions.title for
claude-code-session:0213d48f-5b7a-4241-b77a-eb714672dc3b currently reads
the generic fallback "continue" instead of the real summary title, because
the stale drive copy won a freshness tie.

Solution

  • Added _stored_session_native_id (polylogue/storage/sqlite/archive_tiers/write.py),
    mirroring _stored_message_native_id — the single source of truth every
    session-identity call site now routes through. Applied once right after
    provider_session_id is read, so session_id, the sessions INSERT bind,
    and _resolve_session_graph's native_id argument all see the same value.
  • _write_session_link now normalizes dst_native_id (strip + surrogate
    substitution) before writing a parent-session reference, matching how the
    resolver (_resolve_outbound_session_links) exact-matches it against
    sessions.native_id.
  • Property test tests/property/test_session_identity_normalization.py
    mirrors test_message_identity_normalization.py: sweeps pathological
    provider_session_id shapes (empty, whitespace-only, padded, mixed)
    through the real write path and asserts the DB-generated
    sessions.session_id matches identity_law.session_id, plus the
    ValueError path for unidentifiable (whitespace-only) native ids.
  • polylogue-t83e: no code change. Recorded the full forensic trail (SQL used,
    exact session/raw ids, byte-diff result, root-cause trace through the three
    duplicate skip-stale-replace implementations in write.py,
    pipeline/services/ingest_batch/_core.py, and
    storage/sqlite/archive_tiers/revision_governance.py, and why a detection
    fix needs fallback_provider/DRIVE_LIKE_PROVIDERS-scoped plumbing I
    could not safely verify this session) as a bd comment plus a
    chore(beads) commit. Bead left open, not closed.

Rebuild impact (explicit, per both beads)

  • polylogue-lyr2: verified 0 padded/whitespace sessions.native_id
    and 0 padded session_links.dst_native_id rows exist in the live
    archive today (sqlite3 "file:/realm/db/polylogue/index.db?mode=ro").
    This PR changes nothing a rebuild would write for current data — it closes
    a latent divergence that only a future/pathological provider export would
    trigger. No SEMANTIC_REPARSE delta needed: output is byte-identical
    for every live row.
  • polylogue-t83e: unchanged by this PR. A rebuild still replays every
    raw for the 6 known collisions (and any undiscovered siblings) and picks
    whichever raw its replay order processes last for that native_id —
    exactly today's live, order-dependent behavior, now precisely diagnosed
    rather than merely suspected. No SEMANTIC_REPARSE declaration applies
    since no detection/parsing code changed here.

Verification

devtools test tests/property/test_session_identity_normalization.py \
  tests/unit/storage/test_archive_tiers_write.py \
  tests/unit/storage/test_lineage_normalization.py \
  tests/property/test_message_identity_normalization.py
# 106 passed in 18.47s

devtools verify --quick
# exit_code: 0 (ruff format/check, mypy --strict, render all --check,
# layering, closure-matrix, schema-versioning policy, schema promotion audit
# — all ok)

Live-archive counts (read-only, /realm/db/polylogue/{index,source}.db):

-- lyr2: padded native_id check
SELECT count(*) FROM sessions WHERE native_id <> trim(native_id);        -- 0
SELECT count(*) FROM session_links WHERE dst_native_id <> trim(dst_native_id); -- 0

-- t83e: collision confirmation (6 native_ids, both a drive raw and a local raw)
SELECT native_id, raw_id, source_path FROM raw_sessions
WHERE native_id IN (
  '0213d48f-5b7a-4241-b77a-eb714672dc3b','063a6885-8d6a-4f91-80b2-7f67fa06d680',
  '705f1fcb-8953-4b8b-92f1-9244fcf9db91','8c9f8c3d-4859-44cf-be9c-338803a8e7de',
  'a952ffa4-73b0-48bd-a212-ebe5b9772d1e','cf3404fa-89e0-400a-af3e-ff1450eecef4'
);
-- each id resolves to exactly 2 rows: one drive-cache/gemini source_path, one ~/.claude/projects/ source_path

Full evidence trail (exact commands, byte-diff output, root-cause trace) is
recorded on polylogue-t83e's bd comment for the next implementer.

Ref polylogue-lyr2, Ref polylogue-t83e

Sinity and others added 2 commits July 31, 2026 13:50
…lper

Problem: an invariants audit (polylogue-lyr2) found _write_session bound a
session's native_id RAW into the sessions INSERT while session_id (used for
cache keys, lineage lookups, the FK-failure log message, and everything else
downstream in the same write) was computed via core.identity_law.session_id,
which strips whitespace. Same logical session, two spellings, one row -- the
exact incident class that produced ab5bad1f's lost 10-hour rebuild, which
was fixed at message level via _stored_message_native_id + a property test
but never given a session-level sibling. session_links.dst_native_id (parent
references written by _write_session_link) had the same raw-vs-stripped
mismatch against the sessions.native_id it must exact-match to resolve.

Solution: add _stored_session_native_id (archive_tiers/write.py), mirroring
_stored_message_native_id -- the single source of truth every session
identity call site now routes through, applied once right after
provider_session_id is read so session_id, the sessions INSERT bind, and
_resolve_session_graph's native_id all see the same value. _write_session_link
normalizes dst_native_id the same way before writing a parent-session
reference, since the resolver matches it against sessions.native_id by exact
string equality. Property test
tests/property/test_session_identity_normalization.py mirrors
test_message_identity_normalization.py: sweeps pathological native_id shapes
(empty, whitespace-only, padded, mixed) and asserts the DB-generated
sessions.session_id matches identity_law.session_id, plus the ValueError
path for unidentifiable (whitespace-only) native ids.

Rebuild impact: verified 0 padded/whitespace native_ids exist in the live
archive today (see index.db query in the PR description), so this changes
nothing a rebuild would write for current data -- it closes a latent
divergence that only a future/edge-case provider export would trigger.
No SEMANTIC_REPARSE delta needed: output is byte-identical for all live rows.

Verification: devtools test tests/property/test_session_identity_normalization.py
tests/unit/storage/test_archive_tiers_write.py
tests/unit/storage/test_lineage_normalization.py
tests/property/test_message_identity_normalization.py -- 106 passed.

Co-Authored-By: Claude <[email protected]>
…logue-t83e

Problem: polylogue-t83e (P1) flagged 6 Drive-cache Claude Code transcripts
colliding with local raw session identities, but the prior hs3y content-shape
audit only confirmed the bytes were genuinely Claude-Code-shaped -- it never
cross-checked those native_ids against local raw_sessions for actual
session_id collisions, so the collision claim was unverified against the
live archive.

What changed: verified against /realm/db/polylogue (index.db/source.db,
read-only) -- confirmed 6 real collisions via SQL join, confirmed the
drive-cache raw currently wins all 6, and byte-diffed one pair in the blob
store: the drive copy is an exact prefix of the local file (same
conversation, stale/truncated snapshot), with a concrete live consequence
(session title regressed to a generic "continue" fallback). Recorded the
root cause (a freshness-tie gap shared by three duplicate skip-stale-replace
implementations), why a full fix isn't safe to land without deeper
acquisition-provenance plumbing verification, and a scoped recommendation
for the next implementer. Left the bead open (not closed) -- this is
diagnosis, not a closing fix.

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 31, 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: 31 seconds

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: 0c8f08d0-567e-457e-9f03-89ebbd68d8ec

📥 Commits

Reviewing files that changed from the base of the PR and between 33c62a3 and 7c16799.

📒 Files selected for processing (3)
  • .beads/issues.jsonl
  • polylogue/storage/sqlite/archive_tiers/write.py
  • tests/property/test_session_identity_normalization.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 decca43 into master Jul 31, 2026
1 of 3 checks passed
@Sinity
Sinity deleted the feature/fix/session-identity-collisions branch July 31, 2026 12:13
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