From 803f21d4c75e52a009bb82b9aa268b60c44eeeaa Mon Sep 17 00:00:00 2001 From: dawn <90008@klbr.net> Date: Fri, 30 Oct 2026 03:46:10 +0300 Subject: [PATCH] rolling: recap only summarized user messages so the prefix stays cached Signed-off-by: dawn <90008@klbr.net> --- robot-docs/compaction.md | 4 ++-- .../extensions/rolling/extension.gleam | 24 ++++++++++--------- test/e2e/extensions_test.py | 12 +++++++++- 3 files changed, 26 insertions(+), 14 deletions(-) diff --git a/robot-docs/compaction.md b/robot-docs/compaction.md index 24f3d95ad..23f9e5f4c 100644 --- a/robot-docs/compaction.md +++ b/robot-docs/compaction.md @@ -80,10 +80,10 @@ All three settings are optional, and `triggerPercent` defaults to the window-siz 1. the system prompt (including enabled extension context), which is never compacted 2. one incremental summary of the older conversation it has already evicted -3. a short recap built from recent user messages, quoted verbatim and bounded +3. a short recap of the last user messages the summary covers, quoted verbatim and bounded 4. the newest conversation tail, unchanged -It compacts when the estimated request reaches `triggerPercent` of the configured context window, so roughly the last 20% stays free for work. The tail keeps about `tailPercent` of the window. A cut is only made between whole conversation units, so an assistant tool call always keeps its result. +It compacts when the estimated request reaches `triggerPercent` of the configured context window, so roughly the last 20% stays free for work. The tail keeps about `tailPercent` of the window. A cut is only made between whole conversation units, so an assistant tool call always keeps its result. The summary and recap change only when compaction runs, so between compactions every request repeats the previous one's prefix and the provider cache covers everything before the newest messages. Each rolling compaction combines the previous summary with newly evicted history in a replacement summary. A long eviction is summarized in chunks of at most half the window, with the summary carried from chunk to chunk. The summarizer call has no tools. Rolling writes its summary and cut position only after a successful call. A provider failure leaves the previous projection and the transcript intact. A branch starts without rolling state. diff --git a/src/albedo/harness/extensions/rolling/extension.gleam b/src/albedo/harness/extensions/rolling/extension.gleam index 6de1270a9..a40c31860 100644 --- a/src/albedo/harness/extensions/rolling/extension.gleam +++ b/src/albedo/harness/extensions/rolling/extension.gleam @@ -307,14 +307,14 @@ fn compact( ), int.min(2048, int.max(128, capacity / 10)), )) - let next = view_of(summary, tail, history) + let evicted = list.append(evicted, newly_evicted) + let next = view_of(summary, tail, evicted) // The fit guarantee belongs to a real window; a forced compaction's // synthetic window is a tail budget, so the projection always saves. use _ <- result.try(compaction.require( pinned_tokens + compaction.estimate_inputs(next.inputs) < capacity || force, "summary, recap, and indivisible recent tail do not fit the context window", )) - let evicted = list.append(evicted, newly_evicted) use _ <- result.try(save_state( ledger, session, @@ -405,16 +405,18 @@ fn summarize_chunks( fn projection(resumed: Option(Resumed), history: List(types.Input)) -> View { case resumed { None -> View([], [], history, history) - Some(Resumed(State(summary, _), _, tail)) -> view_of(summary, tail, history) + Some(Resumed(State(summary, _), evicted, tail)) -> + view_of(summary, tail, evicted) } } -/// The saved summary as a model input, the recent user recap, and the -/// verbatim tail they sit before. +/// The saved summary as a model input, a recap of the last user messages it +/// evicted, and the verbatim tail they sit before. Both change only when +/// compaction runs, so the request prefix stays cached between compactions. fn view_of( summary: String, tail: List(types.Input), - history: List(types.Input), + evicted: List(types.Input), ) -> View { let folded = [ types.User( @@ -423,13 +425,13 @@ fn view_of( <> "\n[end older conversation summary]", ), ] - let recap = recap(history) + let recap = recap(evicted) View(folded, recap, tail, list.append(folded, list.append(recap, tail))) } -fn recap(history: List(types.Input)) -> List(types.Input) { +fn recap(evicted: List(types.Input)) -> List(types.Input) { let excerpts = - history + evicted |> list.filter_map(fn(input) { case input { types.User(text) -> Ok(tool.excerpt(text, recap_chars_per_message)) @@ -448,9 +450,9 @@ fn recap(history: List(types.Input)) -> List(types.Input) { [] -> [] values -> [ types.User( - "[recent user excerpts; verbatim and possibly truncated]\n--- user excerpt ---\n" + "[latest summarized user excerpts; verbatim and possibly truncated]\n--- user excerpt ---\n" <> string.join(values, "\n--- user excerpt ---\n") - <> "\n[end recent user excerpts]", + <> "\n[end latest summarized user excerpts]", ), ] } diff --git a/test/e2e/extensions_test.py b/test/e2e/extensions_test.py index 4f33de8e9..cc81d7c5b 100644 --- a/test/e2e/extensions_test.py +++ b/test/e2e/extensions_test.py @@ -95,7 +95,7 @@ def assert_result_follows_call(test, request): def capability_notes(request): """The capability notes a request carries as inputs of their own; the - rolling recap may quote the newest one too.""" + rolling recap may quote a summarized one too.""" return [ item["content"] for item in request["input"] @@ -593,6 +593,16 @@ class ExtensionTests(unittest.TestCase): self.assertIn("older conversation summary", compacted["summary"]) self.assertIn("older conversation summary", self.section_text("history")) + def test_rolling_prefix_stays_cached_across_turns_after_compaction(self): + for prompt in ("first turn", "second turn", "third turn", "fourth turn"): + self.turn(prompt) + self.assertEqual(self.compact()["state"], "compacted") + (first,) = self.turn("fifth turn") + (second,) = self.turn("sixth turn") + sent = first["input"] + self.assertIn("[older conversation summary;", json.dumps(sent)) + self.assertEqual(second["input"][: len(sent)], sent) + def test_every_shipped_skill_loads(self): self.reload() shipped = sorted( -- knotmirror 0.0.1