Skip to content

The agent that grows with you

test(compression): trim the salvaged repair-accounting tests to the invariants

kshitijk4poorf64c9a89a598
diff --git a/tests/agent/test_in_place_merged_user_rows.py b/tests/agent/test_in_place_merged_user_rows.py
index 6c2038375d4..1ffb7ca1039 100644
--- a/tests/agent/test_in_place_merged_user_rows.py
+++ b/tests/agent/test_in_place_merged_user_rows.py
@@ -2,16 +2,18 @@
A crash between turn start and the reply leaves a durable ``user;user`` pair. The alternation repair
hands the compressor one dict for both rows, so the commit has to account for two originals behind it.
-A turn killed later leaves a tool call with no result, or a result the repair cannot pair. The repair
-drops those rows, and they are still behind the dict before them. Two assistant rows in a row are
-folded into one turn the same way.
+A turn killed later leaves a tool call with no result; the repair drops that row, and it is still
+behind the dict before it.
"""
from types import SimpleNamespace
import pytest
-from tests.agent.test_in_place_preflight_rewind import _replies_displayed, _turn, session # noqa: F401
+from tests.agent import test_in_place_preflight_rewind as _rewind
+from tests.agent.test_in_place_preflight_rewind import _replies_displayed, _turn
+
+session = _rewind.session # shared fixture
def _call(call_id):
@@ -20,17 +22,10 @@ def _call(call_id):
_UNANSWERED = ("user", "U12x this prompt never got a reply", {})
-_RETRIED = ("user", "U12y the same request again", {})
-_RESULT = ("tool", "partial output", {"tool_call_id": "call_killed"})
KILLED_TURN_LEFT = {
- "prompt": [_UNANSWERED],
- "two_prompts": [_UNANSWERED, _RETRIED],
- "tool_call": [_UNANSWERED, _call("call_killed")],
- "tool_result": [_UNANSWERED, _RESULT],
- "two_prompts_tool_call": [_UNANSWERED, _RETRIED, _call("call_killed")],
- "two_tool_calls": [_UNANSWERED, _call("call_killed"), _call("call_killed_again")],
- "result_after_a_reply": [_RESULT],
- "two_replies": [("assistant", "A12b a second reply to the same prompt", {})],
+ "prompt": [_UNANSWERED], # #125564: user;user, the unanswered prompt must not be sent twice
+ "tool_call": [_UNANSWERED, _call("call_killed")], # #129123: the dropped tool row is accounted for
+ "benign": [], # plain alternating history: compaction is unchanged
}
@@ -68,50 +63,3 @@ def test_compaction_over_an_unanswered_prompt_keeps_one_copy_of_every_row(sessio
"SELECT COUNT(*) FROM messages WHERE session_id = 'sid' AND active = 1"
" AND (role = 'tool' OR tool_calls IS NOT NULL)").fetchone()[0] == 0
assert [c.split(" ")[0] for c in live[-2:]] == ["U15", "A15"]
-
-
[email protected]("row_ids", [True, False])
[email protected]("left", ["tool_result", "tool_call"])
-def test_a_row_dropped_ahead_of_the_first_survivor_is_archived_with_it(tmp_path, left, row_ids):
- """Nothing is kept before a row the repair drops at the head of the history, so the first
- survivor stands for it."""
- from agent.conversation_compression_archive import coverage_for_commit
- from hermes_state import SessionDB
-
- db = SessionDB(db_path=tmp_path / "state.db")
- db.create_session("sid", source="test")
- for role, content, fields in (KILLED_TURN_LEFT[left][-1], ("user", "U1", {}), ("assistant", "A1", {})):
- db.append_message("sid", role, content, **fields)
- watermark = db.get_active_message_watermark("sid")
- held = db.get_messages_as_conversation("sid", repair_alternation=True, include_row_ids=row_ids)
- assert [m["content"] for m in held] == ["U1", "A1"]
- covered_ids, unresolved = coverage_for_commit(db, "sid", held)
-
- db.archive_and_compact(
- "sid", [{"role": "user", "content": "summary"}], watermark=watermark,
- covered_ids=covered_ids, unresolved_held=unresolved)
-
- assert [row["content"] for row in db.get_messages("sid")] == ["summary"]
- db.close()
-
-
-def test_the_repair_row_counts_stay_off_the_request_copy(session):
- """The commit reads the counts off the live dict. A transport is handed the request copy, and
- one that forwards unknown keys must not find them there."""
- from agent.conversation_compression_archive import MERGED_DURABLE_ROWS, UNNAMED_DURABLE_ROWS
- from agent.turn_context import build_api_messages
-
- db, agent = session
- _turn(db, agent, SimpleNamespace(conversation_history=[]), "cli", 1, 5_000)
- for role, content, fields in KILLED_TURN_LEFT["two_prompts_tool_call"]:
- db.append_message("sid", role, content, **fields)
- history = db.get_messages_as_conversation("sid", repair_alternation=True)
- counts = {MERGED_DURABLE_ROWS, UNNAMED_DURABLE_ROWS}
- assert counts <= set(history[-1]) # two prompts merged, and the tool call dropped behind them
-
- request, _ = build_api_messages(
- agent, history, current_turn_user_idx=len(history) - 1,
- ext_prefetch_cache="", plugin_user_context="", moa_config=None, active_system_prompt="")
-
- assert not any(counts & set(message) for message in request)
- assert counts <= set(history[-1])
diff --git a/tests/agent/test_message_sequence_repair.py b/tests/agent/test_message_sequence_repair.py
index 581cb074c35..2a1e24fcfad 100644
--- a/tests/agent/test_message_sequence_repair.py
+++ b/tests/agent/test_message_sequence_repair.py
@@ -1692,20 +1692,6 @@ def test_repair_decode_of_durable_sentinel_row_does_not_reappend(tmp_path):
assert after == ["user", "assistant"], f"flush changed the durable transcript: {before} -> {after}"
-def test_repair_records_dropped_stray_tool_row_on_survivor():
- agent = _bare_agent()
- messages = [
- {"role": "user", "content": "prompt", "_row_id": 10},
- {"role": "tool", "tool_call_id": "orphan", "content": "out", "_row_id": 11},
- ]
-
- repairs = AIAgent._repair_message_sequence(agent, messages)
-
- assert repairs >= 1
- assert len(messages) == 1
- assert messages[0]["_absorbed_row_ids"] == [11]
-
-
def test_repair_records_dropped_unanswered_tool_call_row_on_survivor():
agent = _bare_agent()
messages = [
diff --git a/tests/hermes_state/test_archive_merged_user_rows.py b/tests/hermes_state/test_archive_merged_user_rows.py
deleted file mode 100644
index 463593edcac..00000000000
--- a/tests/hermes_state/test_archive_merged_user_rows.py
+++ /dev/null
@@ -1,90 +0,0 @@
-"""A held dict may only name rows the compressor could have been handed.
-
-The commit resolves a merged user dict to the durable ``user;user`` run behind it. A prompt that was
-never persisted can carry the same text as rows another surface appended after the snapshot.
-"""
-
-from hermes_state import SessionDB
-
-
-def test_unpersisted_prompt_leaves_rows_appended_after_the_snapshot_live(tmp_path):
- db = SessionDB(tmp_path / "state.db")
- db.create_session("sid", source="test")
- held = [db.append_message("sid", "user", "U1"), db.append_message("sid", "assistant", "A1")]
- watermark = db.get_active_message_watermark("sid")
- db.append_message("sid", "user", "same first paragraph")
- db.append_message("sid", "user", "same second paragraph")
- prompt = {"role": "user", "content": "same first paragraph\n\nsame second paragraph"}
-
- db.archive_and_compact(
- "sid", [{"role": "user", "content": "summary"}, {"role": "assistant", "content": "ok"}, prompt],
- watermark=watermark, covered_ids=held, unresolved_held=[prompt])
-
- assert [row["content"] for row in db.get_messages("sid")] == [
- "summary", "ok", prompt["content"], "same first paragraph", "same second paragraph"]
- db.close()
-
-
-def _unanswered_pair(tmp_path, *after):
- db = SessionDB(tmp_path / "state.db")
- db.create_session("sid", source="test")
- for role, content in (("user", "first prompt"), ("user", "second prompt"), *after):
- db.append_message("sid", role, content)
- return db, db.get_active_message_watermark("sid")
-
-
-def _live(db):
- return [row["content"] for row in db.get_messages("sid")]
-
-
-def test_carried_merged_prompt_rendered_with_a_timestamp_keeps_one_copy_of_each_row(tmp_path):
- from gateway.run import _build_gateway_agent_history
-
- db, watermark = _unanswered_pair(tmp_path)
- restored = db.get_messages_as_conversation("sid", repair_alternation=True)
- held, _ = _build_gateway_agent_history(restored, inject_timestamps=True)
- assert held[0]["content"] != restored[0]["content"] # the render is what hides the rows
-
- db.archive_and_compact(
- "sid", [{"role": "assistant", "content": "summary"}, held[0]], watermark=watermark, tail_count=1,
- covered_ids=[], unresolved_held=held)
-
- assert _live(db) == ["summary", held[0]["content"]]
- recalled = " ".join(row["content"] for row in db._conn.execute(
- "SELECT content FROM messages WHERE session_id = 'sid' AND (active = 1 OR compacted = 1)").fetchall())
- assert (recalled.count("first prompt"), recalled.count("second prompt")) == (1, 1)
- db.close()
-
-
-def test_merged_prompt_never_names_a_run_appended_after_the_snapshot(tmp_path):
- from gateway.run import _build_gateway_agent_history
-
- db, watermark = _unanswered_pair(tmp_path)
- restored = db.get_messages_as_conversation("sid", repair_alternation=True)
- held, _ = _build_gateway_agent_history(restored, inject_timestamps=True)
- late = held[0]["content"].split("\n\n")
- assert len(late) == 2 and late[0] != "first prompt" # only the late run joins to the rendered text
- for part in late:
- db.append_message("sid", "user", part)
-
- db.archive_and_compact(
- "sid", [{"role": "assistant", "content": "summary"}, held[0]], watermark=watermark, tail_count=1,
- covered_ids=[], unresolved_held=held)
-
- assert _live(db) == ["summary", held[0]["content"], *late]
- db.close()
-
-
-def test_merged_prompt_names_its_pair_not_a_later_row_with_the_merged_text(tmp_path):
- db, watermark = _unanswered_pair(tmp_path, ("assistant", "A1"))
- held = db.get_messages_as_conversation("sid", repair_alternation=True)
- for message in held:
- message.pop("_row_id", None)
- message.pop("_absorbed_row_ids", None)
- db.append_message("sid", "user", held[0]["content"])
-
- db.archive_and_compact(
- "sid", [{"role": "user", "content": "summary"}], watermark=watermark, covered_ids=[], unresolved_held=held)
-
- assert _live(db) == ["summary", "first prompt\n\nsecond prompt"]
- db.close()
diff --git a/tests/hermes_state/test_chained_repair_coverage.py b/tests/hermes_state/test_chained_repair_coverage.py
deleted file mode 100644
index b20e84222e9..00000000000
--- a/tests/hermes_state/test_chained_repair_coverage.py
+++ /dev/null
@@ -1,87 +0,0 @@
-"""Composed repairs must not archive context the compressor never held (#129123)."""
-
-import pytest
-
-from agent.agent_runtime_helpers import repair_message_sequence
-from agent.conversation_compression_archive import coverage_for_commit
-from hermes_state import SessionDB
-
-
-def _call(call_id):
- return {"tool_calls": [{"id": call_id, "type": "function", "function": {
- "name": "terminal", "arguments": "{}",
- }}]}
-
-
[email protected]("row_ids", [False, True])
[email protected]("shape", ["chained_calls", "users_split_by_drop"])
-def test_composed_repairs_preserve_unseen_context(tmp_path, row_ids, shape):
- db = SessionDB(tmp_path / "state.db")
- try:
- db.create_session("sid", source="test")
- db.append_message("sid", "user", "U1")
- db.append_message("sid", "assistant", "A1")
- held = db.get_messages_as_conversation("sid", include_row_ids=row_ids)
- # Deterministic interleaving: these rows never enter the held snapshot.
- db.append_message("sid", "user", "UNSEEN_USER")
- db.append_message("sid", "assistant", "UNSEEN_REPLY")
- db.append_message("sid", "user", "U2")
- if shape == "chained_calls":
- db.append_message("sid", "assistant", "tool setup", **_call("z"))
- db.append_message("sid", "tool", "answer z", tool_call_id="z")
- db.append_message("sid", "assistant", "", **_call("a"))
- db.append_message("sid", "assistant", "", **_call("b"))
- else:
- db.append_message("sid", "tool", "orphan", tool_call_id="orphan")
- db.append_message("sid", "user", "U3")
- db.append_message("sid", "assistant", "A3")
- held.extend(db.get_messages_as_conversation("sid", include_row_ids=row_ids)[4:])
- repair_message_sequence(None, held)
- covered, unresolved = coverage_for_commit(db, "sid", held)
- db.archive_and_compact(
- "sid", [{"role": "user", "content": "summary HELD only"}],
- watermark=db.get_active_message_watermark("sid"),
- covered_ids=covered, unresolved_held=unresolved,
- )
- assert [m["content"] for m in db.get_messages("sid")] == [
- "summary HELD only", "UNSEEN_USER", "UNSEEN_REPLY",
- ]
- finally:
- db.close()
-
-
[email protected]("row_ids", [False, True])
[email protected]("layout", ["gap", "after_watermark"])
-def test_folded_assistant_does_not_claim_unseen_row_with_its_text(tmp_path, row_ids, layout):
- db = SessionDB(tmp_path / "state.db")
- try:
- db.create_session("sid", source="test")
- db.append_message("sid", "user", "U1")
- db.append_message("sid", "assistant", "A1")
- held = db.get_messages_as_conversation("sid", include_row_ids=row_ids)
- # Another surface's reply whose text equals the fold of A2 and A2b, below or above the watermark.
- if layout == "gap":
- db.append_message("sid", "user", "UNSEEN_USER")
- db.append_message("sid", "assistant", "A2\nA2b")
- db.append_message("sid", "user", "U2")
- db.append_message("sid", "assistant", "A2")
- db.append_message("sid", "assistant", "A2b")
- db.append_message("sid", "user", "U3")
- db.append_message("sid", "assistant", "A3")
- loaded = db.get_messages_as_conversation("sid", include_row_ids=row_ids)
- held.extend(loaded[4:] if layout == "gap" else loaded[2:])
- watermark = db.get_active_message_watermark("sid")
- repair_message_sequence(None, held)
- if layout == "after_watermark":
- db.append_message("sid", "user", "UNSEEN_USER")
- db.append_message("sid", "assistant", "A2\nA2b")
- covered, unresolved = coverage_for_commit(db, "sid", held)
- db.archive_and_compact(
- "sid", [{"role": "user", "content": "summary HELD only"}],
- watermark=watermark, covered_ids=covered, unresolved_held=unresolved,
- )
- assert [m["content"] for m in db.get_messages("sid")] == [
- "summary HELD only", "UNSEEN_USER", "A2\nA2b",
- ]
- finally:
- db.close()
diff --git a/tests/hermes_state/test_dropped_row_unseen_gap.py b/tests/hermes_state/test_dropped_row_unseen_gap.py
deleted file mode 100644
index 3df1d18326d..00000000000
--- a/tests/hermes_state/test_dropped_row_unseen_gap.py
+++ /dev/null
@@ -1,114 +0,0 @@
-"""A row the alternation repair drops is named at commit, so the rest of the held history keeps exact coverage.
-
-Falling back to the watermark instead archives rows another surface appended below it, which the
-compressor never saw: they leave the model's history without being in the summary.
-"""
-
-from types import SimpleNamespace
-
-import pytest
-
-from agent.agent_runtime_helpers import repair_message_sequence
-from agent.conversation_compression_archive import coverage_for_commit
-from hermes_state import SessionDB
-from tests.agent.test_in_place_preflight_rewind import _turn, session # noqa: F401
-
-_CALL = {"tool_calls": [{"id": "call_killed", "type": "function",
- "function": {"name": "terminal", "arguments": "{}"}}]}
-DROPPED = {
- "tool_result": [("tool", "ORPHAN_RESULT", {"tool_call_id": "call_killed"})],
- "tool_call": [("assistant", "", _CALL)],
- "two_replies": [("assistant", "A2b a second reply", {})],
-}
-
-
[email protected]("row_ids", [False, True])
[email protected]("left", list(DROPPED))
-def test_an_unseen_gap_survives_a_dropped_row(tmp_path, left, row_ids):
- db = SessionDB(tmp_path / "state.db")
- try:
- db.create_session("sid", source="test")
- db.append_message("sid", "user", "U1")
- db.append_message("sid", "assistant", "A1")
- held = db.get_messages_as_conversation("sid", include_row_ids=row_ids)
- # Another surface writes after this process loaded its prefix; its live list never loads the gap.
- db.append_message("sid", "user", "UNSEEN_USER")
- db.append_message("sid", "assistant", "UNSEEN_REPLY")
- db.append_message("sid", "user", "U2")
- db.append_message("sid", "assistant", "A2")
- for role, content, fields in DROPPED[left]:
- db.append_message("sid", role, content, **fields)
- db.append_message("sid", "user", "U3")
- db.append_message("sid", "assistant", "A3")
- held.extend(db.get_messages_as_conversation("sid", include_row_ids=row_ids)[4:])
- repair_message_sequence(None, held)
- covered, unresolved = coverage_for_commit(db, "sid", held)
-
- db.archive_and_compact(
- "sid", [{"role": "user", "content": "summary of the held rows"}],
- watermark=db.get_active_message_watermark("sid"), covered_ids=covered, unresolved_held=unresolved)
-
- live = db.get_messages("sid")
- assert [row["content"] for row in live] == ["summary of the held rows", "UNSEEN_USER", "UNSEEN_REPLY"]
- assert not any(row["tool_calls"] or row["role"] == "tool" for row in live)
- finally:
- db.close()
-
-
[email protected]("trigger", ["ambiguous", "rendered", "orphan", "folded"])
-def test_unresolved_repair_provenance_never_archives_unseen_rows(tmp_path, trigger):
- from gateway.run import _build_gateway_agent_history
-
- db = SessionDB(tmp_path / "state.db")
- try:
- db.create_session("sid", source="test")
- db.append_message("sid", "user", "U1")
- db.append_message("sid", "assistant", "A1")
- held = db.get_messages_as_conversation("sid", include_row_ids=True)
- pair = ["continue", "please continue"] if trigger == "ambiguous" else ["U2", "U3"]
- unseen = [("user", pair[0], {}), ("user", pair[1], {}), ("assistant", "UNSEEN_REPLY", {})]
- if trigger == "rendered":
- unseen = [("user", "UNSEEN_USER", {}), ("assistant", "UNSEEN_REPLY", {})]
- if trigger in {"orphan", "folded"}:
- extra = DROPPED["tool_result" if trigger == "orphan" else "two_replies"]
- unseen = [("user", "UNSEEN_USER", {}), *extra, ("assistant", "UNSEEN_REPLY", {})]
- suffix = [("user", "U2", {}), ("assistant", "A2", {}), *extra,
- ("user", "U3", {}), ("assistant", "A3", {})]
- else:
- suffix = [("user", pair[0], {}), ("user", pair[1], {}), ("assistant", "A3", {})]
- for role, content, fields in [*unseen, *suffix]:
- db.append_message("sid", role, content, **fields)
- held.extend(db.get_messages_as_conversation("sid", include_row_ids=False)[2 + len(unseen):])
- repair_message_sequence(None, held)
- if trigger == "rendered":
- held[2:], _ = _build_gateway_agent_history(held[2:], inject_timestamps=True)
- covered, unresolved = coverage_for_commit(db, "sid", held)
- db.archive_and_compact(
- "sid", [{"role": "user", "content": "summary of the held rows"}],
- watermark=db.get_active_message_watermark("sid"), covered_ids=covered, unresolved_held=unresolved)
- active = [row["content"] for row in db.get_messages("sid")]
- assert "UNSEEN_REPLY" in active
- assert all(content in active for role, content, fields in unseen)
- if trigger == "rendered":
- assert active == ["summary of the held rows", *[content for role, content, fields in unseen]]
- finally:
- db.close()
-
-
-def test_the_named_rows_stay_off_the_request_copy(session):
- from agent.conversation_compression_archive import RETIRED_DURABLE_ROWS
- from agent.turn_context import build_api_messages
- from tests.agent.test_in_place_merged_user_rows import KILLED_TURN_LEFT
-
- db, agent = session
- _turn(db, agent, SimpleNamespace(conversation_history=[]), "cli", 1, 5_000)
- for role, content, fields in KILLED_TURN_LEFT["two_prompts_tool_call"]:
- db.append_message("sid", role, content, **fields)
- history = db.get_messages_as_conversation("sid", repair_alternation=True)
- assert RETIRED_DURABLE_ROWS in history[-1]
-
- request, _ = build_api_messages(
- agent, history, current_turn_user_idx=len(history) - 1,
- ext_prefetch_cache="", plugin_user_context="", moa_config=None, active_system_prompt="")
-
- assert not any(RETIRED_DURABLE_ROWS in message for message in request)