Commit Graph

10 Commits

Author SHA1 Message Date
Teknium
3745ea63bb fix(state): a dedupe claim needs the occurrence, its version and its replay bytes
Review round 5 (unsupportedpastels) on #144938, three findings:

- Compaction claims no longer accept matching text: the identity+timestamp and
  bare-identity claims are gone. Only a dict naming the row (_row_id, else
  message_uid) may stand for a tail row, so an identical message another
  surface appended after the watermark keeps its model-visible row (as main).
- A rewritten-carry (containment) claim needs the stored-row digest to equal
  the row's current version; a _row_id without a digest no longer proves it,
  so a row shortened in place after a row-addressed load keeps its new text.
  (A loader-stamped version witness would add an underscore key to
  row-addressed projections, which test_message_reactions pins to _row_id and
  _db_persisted; such carries are cloned as on main instead.)
- Both claim passes and the flush re-insert guard now compare what the next
  request replays: the guard compares _REPLAY_COLUMNS (owned columns +
  api_content + codex_reasoning_items) instead of the CAS ownership columns,
  and claims check api_content and the reasoning carriers through the
  writer's serializer. A newer sidecar or encrypted reasoning item is stored
  / kept instead of being dropped as "the same message".
2026-10-27 11:49:40 -07:00
Teknium
6928ae70ff fix(state): a rewritten carry claims its tail row only when it was copied from the row's current version
Review H4 F1: text containment could not tell "the compressor added text" from "the row lost text after
the copy". An in-place edit that SHORTENED a tail row ("fix bug in parser" -> "fix bug", or
"yes, but first delete the prod database" -> "yes") left the new text inside the older held copy, so the
row was weakly claimed and the model stayed on the pre-edit text; the edit survived only in display.

The second claim pass now requires a copy of the row's current version before containment counts:
- a held dict carrying the stored-row digest (DB_ROW_SNAPSHOT) must match transcript_row_snapshot of the
  row (tail rows are read with every column for this); a mismatch means a rewrite after the copy;
- without a digest only a _row_id carry qualifies (row-addressed loaders, e.g. a Desktop resume, omit
  the digest by design); a bare uid copy may predate any rewrite, so it is cloned;
- the row's text must sit in the copy as whole lines (blanks beside it allowed): the compressor only adds
  whole blocks (header, markers, summary) around a carry.
Image-placeholder swaps stay claimed without a digest: that check already compares everything else.
Anything else is cloned: a duplicate, never a stale model view.
2026-10-27 11:49:40 -07:00
Teknium
d652e53fdd fix(state): a rewritten carry claims its tail row only when the compressor's rewrite explains it
The weak uid claim let a held copy stand for a tail row whose content
differed, so a row rewritten in place after the compressor took its copy
(an @file expansion) left the model on the older text, and claims made
greedily in id order cloned the matching edit of a re-inserted uid.

Claims now run in two passes. The first settles every claim whose dict
holds the row's stored content (_row_id, uid, identity+timestamp, bare
identity). The second lets a still-unclaimed dict naming the row by
_row_id or uid claim it only when exactly one tail row carries that uid,
no covered row does, exactly one candidate dict remains, and the
difference is the compressor's own rewrite: image parts swapped for text
placeholders, or the row's text kept verbatim inside a longer text. The
summary flag alone is not trusted. Anything else is cloned (a duplicate,
never a stale model view). The _row_id shape the in-process compressor
produces now takes the same claim, so its merged copy is no longer cloned.
2026-10-27 11:49:40 -07:00
Teknium
33db60eefe fix(state): the re-insert guard compares through the restore lens; a rewritten carry keeps its uid claim
The durable-uid guard compared a restored dict with the RAW stored row, but restore sanitizes and strips
user/assistant text (context blocks, edge whitespace), drops the compaction-summary flag by default, and a
wire strip loses the underscore identity maps. Those restored copies never matched, so they were
inserted again: the #134799 duplicate survived for ~2% of user rows, summary rows, and every tool-call
assistant row behind a full underscore strip.

_same_as_stored now views the stored content through SessionMessagesMixin._loaded_view_content (the
function restore uses) and takes the row's value for token_count, the summary flag, tool_call_uids and
absorbed_message_uids when the dict carries none. The dict's own content is never normalized, and
content/tool_calls/reasoning are still compared, so an edited restored dict still inserts.

A compaction uid claim with different content (the compressor merged the summary into a carried message or
stripped its images) now claims the tail row when no active row outside the tail carries that uid. It is a
weak claim: the original is archived as compacted history and stays visible. When the dict's original sits
below the watermark, the tail row is another version and is still cloned (B3 unchanged).
2026-10-27 11:49:40 -07:00
Teknium
a995b69417 fix(state,codex): a dedupe claim never drops a different message; a cut codex stream retries
Review H on #144938:

- Codex: the narration recovery for an announced answer item now needs a terminal frame. A stream that
  ends mid-turn (answer announced, partial text, a call announced, no terminal frame) raises "did not
  emit a terminal response" and retries, as on main, instead of completing on the cut-off sentence with
  the call lost. Text deltas follow the phase of the item they name, so commentary that arrives after a
  call announcement is no longer fused into the recovered answer.
- Compaction carry: a dict that names its row by _row_id claims only that row; a message_uid claim needs
  the same identity (a same-uid row with newer content is kept and cloned); an identity+timestamp claim
  is refused when the dict's uid differs from the row's. Rows claimed by identity alone are archived as
  compacted history instead of rewound, so a wrong claim stays visible in display.
- Flush guard: a restored dict whose uid is ACTIVE is skipped only when it serializes to that row's owned
  columns; an edit since the restore inserts, as on main.
2026-10-27 11:49:40 -07:00
Sahil Jagtap
3fd9be25a1 fix(state): stop in-place compaction from storing a carried message twice
A row the compressor already holds can also sit above the commit watermark
(flushed after the watermark was read, or left unnamed so coverage fell back
to the watermark). The commit inserted the carried copy and then cloned the
same row again as a "concurrent append", so the message landed twice with a
fresh id and timestamp each time. The positional tail rewind then took one
row too many from below the watermark and hid a summarized row from search.

archive_and_compact now recognises a concurrent-tail row that the compacted
set already carries (by row id with matching content, by message_uid, by
content plus timestamp, or by content alone when the dict carries none) and
skips its clone: the carried copy is the one live row and the original keeps
rewind flags. The tail rewind below the watermark shrinks by the rows that
were found above it. Both the watermark path and the named-coverage path take
the same step.

Fixes #137197
2026-10-27 11:49:40 -07:00
Maciej Lisiewski
aa2d120cba ci: prefix unused variables with underscore (RUF059) 2026-10-08 19:22:03 +02:00
Maciej Lisiewski
d76cce4f11 ci: apply all automatic linter fixes available 2026-10-08 17:22:54 +02:00
kshitijk4poor
28a0bf19e6 refactor(state): name absorbed-row stamps by constant, hoist replay-entry import
hermes_state_coverage.py read "_absorbed_row_ids" by literal in
_proved_coverage and _uncounted_merged_rows while every other stamp
there goes through its conversation_compression_archive constant; use
ABSORBED_ROW_IDS so a rename cannot leave one reader behind.

gateway/run.py imported the durable-row stamp constants inside
_build_replay_entry, which runs once per replayed message. The module
already imports agent.message_metadata, which imports the same
constants at module level, so a top-level import adds no cycle.

_tail_rows_after_watermark is a plain SQL tail read used only by
SessionMessagesMixin's archive/rewind paths, not coverage resolution;
move it back beside _clone_message_rows where it lived on main.
hermes_state_messages.py stays under 2000 lines (1972).
2026-10-03 18:17:35 +05:30
kshitijk4poor
a9ee040807 refactor(state): move held-row coverage resolution into SessionCoverageMixin
hermes_state_messages.py grew past the 2000-line shape gate (2215) with
the repair-accounting stack. The coverage-resolution helpers are one
topic: naming which durable rows the compressor held
(_tail_rows_after_watermark, _resolve_carried_row_ids,
_matching_active_ids, _matching_retired_ids, _merged_user_run,
_proved_coverage, _uncounted_merged_rows, _tail_originals). Move them
verbatim into hermes_state_coverage.py as SessionCoverageMixin (the
hermes_state_rewind.py / SessionRewindMixin pattern) and add it to
SessionDB's bases next to SessionMessagesMixin. Callers reach them via
self, so nothing else changes; no re-export shims, and no test patches
these names on hermes_state_messages.

hermes_state_messages.py is now 1967 lines.
2026-10-03 18:17:35 +05:30