fix(state): keep stored token_count when the agent flush row carries token_count=None

The agent flush row (_db_flush_row) always sets token_count, None for a replayed
message, so keying the keep on the key's absence missed the main production writer
and a digest-matched rewrite still nulled the stored count. Key it on the value.
This commit is contained in:
kshitijk4poor
2026-10-01 20:58:02 +05:30
committed by kshitij
parent a9d2114078
commit 815b79652d
2 changed files with 6 additions and 3 deletions

View File

@@ -155,8 +155,9 @@ def resolve_and_repair_transcript_batch(
adopt = transcript_row_snapshot(target_row) != expected
if not adopt:
serialized = serialize_message_fn(msg, float(target_row["timestamp"]))
if "token_count" not in msg:
# Replay dicts never carry token_count; a missing key means "unknown", not NULL.
if serialized["token_count"] is None:
# Replays never decode token_count (the agent flush row sets it to None): None means
# "unknown", not NULL.
serialized = {**serialized, "token_count": target_row["token_count"]}
if any(target_row[column] != serialized[column] for column in _OWNED_COLUMNS):
_rewrite_row(conn, session_id, target_row, serialized)

View File

@@ -55,8 +55,10 @@ def test_mutated_replay_rewrites_its_original_row(db, include_row_ids, content,
assert restored[0][MESSAGE_UID] == before[0]["message_uid"]
assert (DB_ROW_SNAPSHOT in restored[0]) is not include_row_ids
# Real rewrite sites change the payload BEFORE clearing the persisted marker.
# Real rewrite sites change the payload BEFORE clearing the persisted marker. The agent flush row
# (session_persistence._db_flush_row) always carries the token_count key, None for a replay.
restored[0]["content"] = "after repair"
restored[0]["token_count"] = None
restored[0].pop(_DB_PERSISTED_MARKER, None)
assert db.append_messages_batch(sid, restored) == 0