perf: preserve conversation prefixes across agent turns

This commit is contained in:
chenmingxuan committed 2026-10-17 17:25:15 +08:00
1 parent 1440a1f508
commit f1bb5d02aa
19 files changed
+552 -205

No files matched your search

@@ -283,13 +283,13 @@ def test_injected_instruction_body_outlives_its_wrapper(mock_context):
assert guidance.wrapper.startswith(USER_GUIDANCE_MARKER)
assert "don't send messages" not in guidance.wrapper
# Scrub edge parity: once the turn has left the live position (a newer
# committed observation exists) the wrapper is gone and the body is
# still in the committed turn.
# Normal turns preserve guidance verbatim; an explicit compaction removes
# the old wrapper but preserves the user's standing instruction.
ledger.stage_turn([tail, AIMessage(content="ok")])
ledger.commit_staged(step_key="s1")
ledger.stage_turn([runner._build_tail(ledger, 3, b"IMG", "[1] Send"), AIMessage(content="ok")])
ledger.commit_staged(step_key="s2")
ledger.compact_active()
rendered = ledger.render([runner._build_tail(ledger, 4, b"IMG", "[1] Send")])
committed = next(
m
@@ -679,8 +679,8 @@ async def test_run_calibrates_the_ledger_only_from_measured_usage(mock_context):
assert record.call_count == 2
first_call, second_call = record.call_args_list
assert first_call.args[0] == 1200 and first_call.kwargs["messages"] is not None
# The report turn carries no usage metadata: estimated, recorded, not calibrated.
assert second_call.args[0] >= 1 and second_call.kwargs["messages"] is None
# Missing usage stays unknown; the next render computes its own estimate.
assert second_call.args[0] is None and second_call.kwargs["messages"] is None
@pytest.mark.asyncio
@@ -30,7 +30,7 @@ from artemis.agents.operator.operator import OperatorNode
from artemis.config.agent import MemoryTranscriptConfig
from artemis.context import ArtemisContext
from artemis.memory.step_memory import StepMemoryService
from artemis.memory.transcript import TranscriptLedger, estimate_prompt_tokens
from artemis.memory.transcript import TranscriptLedger
SCREENSHOT_B64 = base64.b64encode(b"fake-jpeg-bytes").decode("utf-8")
@@ -92,7 +92,7 @@ def _llm(captured: list, usage_metadata=None):
@pytest.mark.asyncio
async def test_operator_records_an_estimate_when_the_provider_reports_no_usage():
async def test_operator_uses_projected_context_without_fabricating_provider_usage():
ctx = _ctx()
captured: list = []
with patch("artemis.agents.operator.operator.get_llm", return_value=_llm(captured)):
@@ -102,8 +102,8 @@ async def test_operator_records_an_estimate_when_the_provider_reports_no_usage()
ledger = ctx.transcript_ledger
assert isinstance(ledger, TranscriptLedger)
assert captured, "the operator made no model call"
assert ledger.last_prompt_tokens == estimate_prompt_tokens(captured[-1])
assert ledger.last_prompt_tokens > 0
assert ledger.last_prompt_tokens is None
assert ledger.projected_prompt_tokens > 0
# No measurement, no calibration.
assert ledger.chars_per_token == 4.0
@@ -355,7 +355,7 @@ async def test_transcript_mode_two_turns_build_four_regions(tmp_path):
# Text edge at depth 1: the committed observation has lost its plan
# recitation as soon as the next tail exists (its screenshot stays
# until the screenshot edge).
assert PLAN_RECITATION_MARKER not in str(committed_obs.content)
assert PLAN_RECITATION_MARKER in str(committed_obs.content) # retained until compaction
# The live tail still recites the plan.
assert PLAN_RECITATION_MARKER in str(turn2_messages[-1].content)
# Message layout is S + A + tail.