Files
ai-agent/backend/test_pi_compaction.py
Gabriel VidalandClaude Opus 5.5 53a3d1c5a5 feat(runner): pi 1.6.0 — compact inside a run, show it, continue a cut-off reply
A long local run (Inkling, 01a20cee) grew to 127k of 131k tokens and
stopped: pi 0.80.6 checks auto-compaction only once a run is over, so one
prompt with hundreds of tool calls never compacted. Near the full window
pi capped the reply at 1 token, it stopped at 'length' (not an overflow
error, so no compact-and-retry), pi compacted after the run and ended.

- pi 0.80.6 -> 1.6.0: it checks the threshold (contextWindow -
  compaction.reserveTokens, 75 % here) before every request in a run.
- Each compaction is mirrored as Claude Code's compact_boundary +
  isCompactSummary records: the viewer's auto-compacted divider.
- Safety net: a run whose last reply stopped at 'length' and was then
  compacted is resumed with a continue prompt (RUNNER_AUTO_CONTINUES,
  default 2); turn_end only after the last pi settles.
- Tests: runner/test/cli.test.mjs (fake pi), backend/test_pi_compaction.py.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-24 16:28:16 +01:00

71 lines
2.9 KiB
Python

"""A pi run's compaction reads as the viewer's divider, and the runner's
auto-continue prompt stays out of the thread.
runner/cli.mjs mirrors pi's ``compaction_end`` as Claude Code's
``compact_boundary`` + ``isCompactSummary`` records, and when a reply a full
context cut off is continued, writes the continue prompt as an injected
``<pi-runner-auto-continue>`` text — the divider above already says it."""
import json
import unittest
from pathlib import Path
from tempfile import TemporaryDirectory
import conversations as c
SID = "01a2ffff-0000-7000-8000-00000000c0de"
MODEL = "inkling-2-mini"
def _rec(ts, **r):
return {"sessionId": SID, "timestamp": ts, **r}
def _assistant(ts, stop, text, inp, out):
return _rec(ts, type="assistant", costUSD=0, message={
"role": "assistant", "model": MODEL, "stop_reason": stop,
"usage": {"input_tokens": inp, "output_tokens": out,
"cache_read_input_tokens": 0, "cache_creation_input_tokens": 0},
"content": [{"type": "text", "text": text}] if text else []})
RECORDS = [
_rec("2026-11-24T10:00:00Z", type="user", message={"role": "user", "content": "do the task"}),
_assistant("2026-11-24T10:25:32Z", "length", "", 127134, 1),
_rec("2026-11-24T10:30:14Z", type="system", subtype="compact_boundary",
content="Conversation compacted",
compactMetadata={"trigger": "auto", "preTokens": 127135, "postTokens": 24000,
"durationMs": 282000}),
_rec("2026-11-24T10:30:14Z", type="user", isCompactSummary=True,
message={"role": "user", "content": "## Goal\nthe corner badge"}),
_rec("2026-11-24T10:30:15Z", type="user", isMeta=True, message={
"role": "user",
"content": "<pi-runner-auto-continue>\nYour last reply was cut off\n</pi-runner-auto-continue>"}),
_assistant("2026-11-24T10:31:00Z", "stop", "Picked it up ✅", 25000, 40),
_rec("2026-11-24T10:31:01Z", type="system", subtype="turn_end", entrypoint="pi"),
]
class PiCompactionTest(unittest.TestCase):
def test_divider_summary_and_hidden_continue(self):
with TemporaryDirectory() as d:
p = Path(d) / f"{SID}.jsonl"
p.write_text("".join(json.dumps(r) + "\n" for r in RECORDS))
st = c.ParserState(p, full=True)
for r in RECORDS:
st.feed(r)
dividers = [it for it in st.thread if it.get("kind") == "compact"]
self.assertEqual(len(dividers), 1)
d = dividers[0]
self.assertEqual(d["compact"]["trigger"], "auto")
self.assertEqual(d["compact"]["preTokens"], 127135)
self.assertEqual(d["compact"]["postTokens"], 24000)
self.assertEqual(d["text"], "## Goal\nthe corner badge")
prompts = [it.get("text") for it in st.thread
if it.get("role") == "user" and it.get("kind") == "text"]
self.assertEqual(prompts, ["do the task"])
if __name__ == "__main__":
unittest.main()