chore: 初始化仓库,导入项目基线代码

Academy v0.8.3 完整代码基线:Go 后端(internal/cmd/cli/mcp-server/docreader)、
Vue 前端(frontend)、数据库迁移(migrations)、部署编排(docker/helm/deploy)与文档。
今日开发的培训推送管理改动随后单独提交。
This commit is contained in:
runny committed 2026-11-16 18:12:23 +08:00
commit 7ec21f35b0
5767 files changed
+1436788

No files matched your search

@@ -0,0 +1,244 @@
package compaction
import (
"context"
"fmt"
"testing"
"academy/internal/models/chat"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// storedTurn is a turn replayed from the database: every message carries the
// ID of the assistant message that closes it.
func storedTurn(id string, rounds int, path string) []chat.Message {
msgs := []chat.Message{{Role: "user", Content: "question " + id}}
for i := 0; i < rounds; i++ {
callID := id + "-call-" + string(rune('a'+i%26))
msgs = append(msgs,
chat.Message{
Role: "assistant",
Content: filler(20),
ToolCalls: []chat.ToolCall{{
ID: callID,
Type: "function",
Function: chat.FunctionCall{
Name: "write_sandbox_file",
Arguments: `{"path":"` + path + `","content":"` + filler(40) + `"}`,
},
}},
},
chat.Message{Role: "tool", Name: "write_sandbox_file", ToolCallID: callID, Content: filler(30)},
)
}
msgs = append(msgs, chat.Message{Role: "assistant", Content: "answer " + id})
for i := range msgs {
msgs[i].TurnID = id
}
return msgs
}
// withStoredHistory puts stored turns between the system prompt and the live
// turn, the way the engine assembles a request.
func withStoredHistory(live []chat.Message, turns ...[]chat.Message) []chat.Message {
out := []chat.Message{live[0]}
for _, turn := range turns {
out = append(out, turn...)
}
return append(out, live[1:]...)
}
// The common case: the live turn outgrows the budget, so every stored turn is
// summarized and the cut splits the live turn. The history part ends on the
// last stored turn and can stand in for it later; the live turn's prefix
// cannot, because that turn will be replayed verbatim once it is stored.
func TestCheckpointEndsOnTheLastStoredTurn(t *testing.T) {
llm := &stubChat{response: "## Goal\nsummary"}
c := New(llm, newEstimator(t), testSettings())
msgs := withStoredHistory(reactTurn(12),
storedTurn("turn-a", 1, "/workspace/a.txt"),
storedTurn("turn-b", 1, "/workspace/b.txt"))
result, err := c.Compact(context.Background(), msgs, ReasonThreshold)
require.NoError(t, err)
require.True(t, result.SplitTurn)
require.NotNil(t, result.Checkpoint)
assert.Equal(t, "turn-b", result.Checkpoint.TurnID)
assert.Contains(t, result.Summary, "Turn Context (split turn)")
assert.NotContains(t, result.Checkpoint.Summary, "Turn Context (split turn)",
"the live turn's prefix must not outlive the turn")
assert.Contains(t, result.Checkpoint.Summary, "/workspace/b.txt")
assert.NotContains(t, result.Checkpoint.Summary, "/workspace/out.html",
"files touched only by the live turn belong to its own replay")
}
// A cut that splits a stored turn still ends the history part on the turn
// before it. The split turn's prefix is summarized for this request only; the
// next request replays that turn whole.
func TestCheckpointStopsBeforeASplitStoredTurn(t *testing.T) {
c := New(&stubChat{response: "## Goal\nsummary"}, newEstimator(t), testSettings())
live := []chat.Message{{Role: "system", Content: "you are an agent"}, {Role: "user", Content: "next"}}
msgs := withStoredHistory(live,
storedTurn("turn-a", 1, "/workspace/a.txt"),
storedTurn("turn-b", 12, "/workspace/b.txt"))
result, err := c.Compact(context.Background(), msgs, ReasonThreshold)
require.NoError(t, err)
require.True(t, result.SplitTurn)
require.NotNil(t, result.Checkpoint)
assert.Equal(t, "turn-a", result.Checkpoint.TurnID)
assert.NotContains(t, result.Checkpoint.Summary, "/workspace/b.txt")
}
// A steered message opens a compaction turn but not a stored one. Cutting
// there summarizes half of a stored turn as history, which no stored boundary
// describes, so nothing is persisted.
func TestNoCheckpointWhenHistoryEndsInsideAStoredTurn(t *testing.T) {
c := New(&stubChat{response: "## Goal\nsummary"}, newEstimator(t), testSettings())
turn := []chat.Message{
{Role: "user", Content: "question"},
{Role: "assistant", ToolCalls: []chat.ToolCall{{
ID: "call-1", Type: "function",
Function: chat.FunctionCall{Name: "read_file", Arguments: `{"path":"/workspace/big.txt"}`},
}}},
{Role: "tool", Name: "read_file", ToolCallID: "call-1", Content: filler(800)},
{Role: "user", Content: "steered: also check the footer"},
{Role: "assistant", Content: "done"},
}
for i := range turn {
turn[i].TurnID = "turn-a"
}
live := []chat.Message{{Role: "system", Content: "you are an agent"}, {Role: "user", Content: "next"}}
result, err := c.Compact(context.Background(), withStoredHistory(live, turn), ReasonThreshold)
require.NoError(t, err)
require.False(t, result.SplitTurn, "the steered message is where the cut lands")
assert.Nil(t, result.Checkpoint)
}
func TestNoCheckpointForALiveTurnOnly(t *testing.T) {
c := New(&stubChat{response: "## Goal\nsummary"}, newEstimator(t), testSettings())
result, err := c.Compact(context.Background(), reactTurn(12), ReasonThreshold)
require.NoError(t, err)
assert.Nil(t, result.Checkpoint, "nothing stored was summarized")
}
// A summarizer that keeps failing used to leave no checkpoint, so every later
// turn loaded the same history, compacted it, and failed again. The raw
// archive is kept as a degraded checkpoint instead; the next compaction folds
// it in as the previous summary.
func TestDegradedSummaryIsStillACheckpoint(t *testing.T) {
c := New(&stubChat{err: assert.AnError}, newEstimator(t), testSettings())
msgs := withStoredHistory(reactTurn(12), storedTurn("turn-a", 1, "/workspace/a.txt"))
result, err := c.Compact(context.Background(), msgs, ReasonThreshold)
require.NoError(t, err)
require.True(t, result.Degraded)
require.NotNil(t, result.Checkpoint)
assert.Equal(t, "turn-a", result.Checkpoint.TurnID)
assert.True(t, result.Checkpoint.Degraded)
assert.Contains(t, result.Checkpoint.Summary, "Raw conversation archive")
assert.Contains(t, result.Checkpoint.Summary, "/workspace/a.txt")
}
// The archive stands in for a summary, so it is held to the summary budget,
// newest messages first. Unbounded, an archive of a history that filled the
// window was nearly as large as the history, and the compaction freed nothing.
func TestRawArchiveIsHeldToTheSummaryBudget(t *testing.T) {
est := newEstimator(t)
c := New(&stubChat{err: assert.AnError}, est, testSettings())
msgs := []chat.Message{{Role: "system", Content: "you are an agent"}}
for i := 0; i < 150; i++ {
msgs = append(msgs, storedTurn(fmt.Sprintf("turn-%03d", i), 1, "/workspace/f.txt")...)
}
msgs = append(msgs, chat.Message{Role: "user", Content: "next"})
result, err := c.Compact(context.Background(), msgs, ReasonThreshold)
require.NoError(t, err)
require.NotNil(t, result.Checkpoint)
archive := result.Checkpoint.Summary
assert.LessOrEqual(t, est.EstimateString(archive), c.Settings().summaryBudget()+300,
"the archive must fit where a summary would")
assert.Contains(t, archive, "earlier messages omitted]")
assert.Contains(t, archive, "question turn-14", "the newest messages are the ones kept")
assert.NotContains(t, archive, "question turn-000")
assert.Less(t, result.TokensAfter, result.TokensBefore/2, "a degraded compaction still frees room")
}
// History loaded from a checkpoint starts with that summary. The next
// checkpoint must fold it in, including the files it recorded, or everything
// before the loaded turns is lost the moment a new checkpoint replaces it.
func TestCheckpointCarriesTheLoadedSummaryForward(t *testing.T) {
llm := &stubChat{response: "## Goal\nupdated"}
c := New(llm, newEstimator(t), testSettings())
loaded := SummaryMessage("## Goal\nearlier work" + fileOps{written: []string{"/workspace/old.txt"}}.format())
live := reactTurn(12)
msgs := append([]chat.Message{live[0], loaded}, storedTurn("turn-c", 1, "/workspace/c.txt")...)
msgs = append(msgs, live[1:]...)
result, err := c.Compact(context.Background(), msgs, ReasonThreshold)
require.NoError(t, err)
require.NotNil(t, result.Checkpoint)
assert.Equal(t, "turn-c", result.Checkpoint.TurnID)
assert.Contains(t, llm.prompts[0], "<previous-summary>")
assert.Contains(t, llm.prompts[0], "earlier work")
assert.Contains(t, result.Checkpoint.Summary, "/workspace/old.txt")
assert.Contains(t, result.Checkpoint.Summary, "/workspace/c.txt")
}
// History can reach the whole window by design, so the part being summarized
// can outgrow one summarization request. An overflowing request degrades to a
// raw archive, which is never persisted, so every later turn would fail the
// same way. The oldest messages are left out instead, the prompt says so, and
// their file paths are still recorded.
func TestSummarizerInputIsFittedToTheWindow(t *testing.T) {
est := newEstimator(t)
llm := &stubChat{response: "## Goal\nsummary"}
settings := Settings{Enabled: true, MaxContextTokens: 20000, ReserveTokens: 8000, KeepRecentTokens: 2000}
c := New(llm, est, settings)
msgs := []chat.Message{{Role: "system", Content: "you are an agent"}}
for i := 0; i < 150; i++ {
id := fmt.Sprintf("turn-%03d", i)
msgs = append(msgs, storedTurn(id, 1, "/workspace/"+id+".txt")...)
}
msgs = append(msgs, chat.Message{Role: "user", Content: "next"})
result, err := c.Compact(context.Background(), msgs, ReasonThreshold)
require.NoError(t, err)
require.False(t, result.Degraded)
require.Positive(t, result.Omitted)
prompt := llm.prompts[0]
request := est.EstimateString(summarizationSystemPrompt) + est.EstimateString(prompt) +
c.Settings().summaryBudget()
assert.LessOrEqual(t, request, settings.MaxContextTokens, "the request must fit the window")
assert.Contains(t, prompt, fmt.Sprintf("[%d earlier messages are not shown", result.Omitted))
assert.NotContains(t, prompt, "question turn-000", "the oldest messages are the ones left out")
assert.Contains(t, prompt, "question turn-140", "the newest summarized messages stay")
require.NotNil(t, result.Checkpoint)
assert.Contains(t, result.Checkpoint.Summary, "/workspace/turn-000.txt",
"paths come from every summarized message, not only the ones shown")
}
func TestSummarizerInputIsUntouchedWhenItFits(t *testing.T) {
llm := &stubChat{response: "## Goal\nsummary"}
c := New(llm, newEstimator(t), testSettings())
result, err := c.Compact(context.Background(),
withStoredHistory(reactTurn(12), storedTurn("turn-a", 1, "/workspace/a.txt")), ReasonThreshold)
require.NoError(t, err)
assert.Zero(t, result.Omitted)
assert.NotContains(t, llm.prompts[0], "earlier messages are not shown")
}
@@ -0,0 +1,395 @@
package compaction
import (
"context"
"encoding/json"
"errors"
"strings"
"testing"
agenttoken "academy/internal/agent/token"
"academy/internal/models/chat"
"academy/internal/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// stubChat records what the summarizer was asked and returns a canned reply.
type stubChat struct {
response string
finishReason string
err error
calls int
prompts []string
}
func (s *stubChat) Chat(
_ context.Context, messages []chat.Message, _ *chat.ChatOptions,
) (*types.ChatResponse, error) {
s.calls++
if len(messages) > 1 {
s.prompts = append(s.prompts, messages[len(messages)-1].Content)
}
if s.err != nil {
return nil, s.err
}
return &types.ChatResponse{Content: s.response, FinishReason: s.finishReason}, nil
}
// ChatStream answers like Chat, as a stream: compaction summarizes through it.
func (s *stubChat) ChatStream(
ctx context.Context, messages []chat.Message, opts *chat.ChatOptions,
) (<-chan types.StreamResponse, error) {
resp, err := s.Chat(ctx, messages, opts)
if err != nil {
return nil, err
}
return streamOf(resp), nil
}
func streamOf(resp *types.ChatResponse) <-chan types.StreamResponse {
ch := make(chan types.StreamResponse, 2)
ch <- types.StreamResponse{ResponseType: types.ResponseTypeAnswer, Content: resp.Content}
ch <- types.StreamResponse{ResponseType: types.ResponseTypeAnswer, Done: true, FinishReason: resp.FinishReason}
close(ch)
return ch
}
func (s *stubChat) GetModelName() string { return "stub" }
func (s *stubChat) GetModelID() string { return "stub" }
func newEstimator(t *testing.T) *agenttoken.Estimator {
t.Helper()
est, err := agenttoken.NewEstimator()
require.NoError(t, err)
return est
}
func testSettings() Settings {
return Settings{
Enabled: true,
MaxContextTokens: 40000,
ReserveTokens: 8000,
KeepRecentTokens: 2000,
}
}
// filler produces text large enough to matter against the token budgets above.
func filler(n int) string { return strings.Repeat("some conversation content ", n) }
// reactTurn builds what an agent turn actually looks like: one user message
// followed by many assistant/tool rounds, with no further user message. This
// is the shape that the previous "never touch the current turn" rule made
// impossible to compact.
func reactTurn(rounds int) []chat.Message {
msgs := []chat.Message{
{Role: "system", Content: "you are an agent"},
{Role: "user", Content: "build me a deck"},
}
for i := 0; i < rounds; i++ {
msgs = append(msgs,
chat.Message{
Role: "assistant",
Content: filler(20),
ToolCalls: []chat.ToolCall{{
ID: "call-" + string(rune('a'+i%26)),
Type: "function",
Function: chat.FunctionCall{
Name: "write_sandbox_file",
Arguments: `{"path":"/workspace/out.html","content":"` + filler(40) + `"}`,
},
}},
},
chat.Message{
Role: "tool",
Name: "write_sandbox_file",
ToolCallID: "call-" + string(rune('a'+i%26)),
Content: filler(30),
},
)
}
return msgs
}
// The regression this package exists for: a single ReAct turn with no second
// user message must still compact. The old cut rule exempted everything after
// the last user message, so a twenty-round turn was entirely untouchable and
// every round paid for a summarization that freed nothing.
func TestCompactsInsideASingleTurn(t *testing.T) {
est := newEstimator(t)
llm := &stubChat{response: "## Goal\nbuild a deck"}
c := New(llm, est, testSettings())
require.NotNil(t, c)
msgs := reactTurn(40)
require.Equal(t, 1, countRole(msgs, "user"), "fixture must have no turn boundary to hide behind")
result, err := c.Compact(context.Background(), msgs, ReasonThreshold)
require.NoError(t, err)
assert.Greater(t, result.Freed(), 0, "compaction must actually shrink the context")
assert.Less(t, result.TokensAfter, result.TokensBefore/2)
assert.True(t, result.SplitTurn, "a turn larger than the budget has to be split")
assert.Equal(t, "system", result.Messages[0].Role)
assert.Equal(t, chat.MessageKindCompactionSummary, result.Messages[1].Kind)
}
// Cutting mid-turn discards the user's original request, so the prefix gets its
// own summary. Without it the retained tail is a pile of tool calls with no
// statement of what they were for.
func TestSplitTurnSummarizesThePrefixSeparately(t *testing.T) {
est := newEstimator(t)
llm := &stubChat{response: "summary text"}
c := New(llm, est, testSettings())
result, err := c.Compact(context.Background(), reactTurn(12), ReasonThreshold)
require.NoError(t, err)
require.True(t, result.SplitTurn)
assert.Contains(t, result.Summary, "Turn Context (split turn)")
require.GreaterOrEqual(t, len(llm.prompts), 1)
assert.Contains(t, llm.prompts[len(llm.prompts)-1], "PREFIX of a turn",
"the prefix must be summarized with the prefix instructions, not the history ones")
}
// The retained tail has to stay valid for the provider: a tool result whose
// originating assistant message was summarized away is rejected outright.
func TestKeptTailNeverStartsWithAnOrphanToolResult(t *testing.T) {
est := newEstimator(t)
c := New(&stubChat{response: "s"}, est, testSettings())
for rounds := 4; rounds <= 40; rounds++ {
result, err := c.Compact(context.Background(), reactTurn(rounds), ReasonThreshold)
if errors.Is(err, ErrNothingToCompact) {
continue // still inside the keep-recent budget
}
require.NoError(t, err)
pending := map[string]bool{}
for _, msg := range result.Messages {
for _, tc := range msg.ToolCalls {
pending[tc.ID] = true
}
if msg.Role == "tool" {
assert.True(t, pending[msg.ToolCallID],
"rounds=%d: tool result %s has no preceding call", rounds, msg.ToolCallID)
}
}
}
}
// The keep-recent budget is a ceiling. Treating it as a floor — stop at the
// first message that reaches the budget and keep that message too — is what
// made compaction fire every couple of rounds: one knowledge_search result is
// routinely larger than the whole budget, so the retained tail overshot and
// left almost no headroom before the next threshold crossing.
func TestRetainedTailStaysWithinTheKeepRecentBudget(t *testing.T) {
est := newEstimator(t)
s := testSettings()
c := New(&stubChat{response: "summary"}, est, s)
// A search result far larger than the budget, followed by small rounds.
msgs := []chat.Message{
{Role: "system", Content: "you are an agent"},
{Role: "user", Content: "research this"},
}
for i := 0; i < 6; i++ {
id := "search-" + string(rune('a'+i))
msgs = append(msgs,
chat.Message{Role: "assistant", ToolCalls: []chat.ToolCall{{
ID: id, Type: "function",
Function: chat.FunctionCall{Name: "knowledge_search", Arguments: `{"query":"x"}`},
}}},
chat.Message{Role: "tool", Name: "knowledge_search", ToolCallID: id, Content: filler(600)},
)
}
result, err := c.Compact(context.Background(), msgs, ReasonThreshold)
require.NoError(t, err)
// Measure the verbatim tail only: the summary is budgeted separately.
tail := result.Messages[2:]
tailTokens := est.EstimateMessages(tail)
assert.LessOrEqual(t, tailTokens, s.Normalize().KeepRecentTokens,
"the retained tail must fit the budget, not merely reach it")
}
// The point of compaction is the headroom it leaves, not the tokens it frees.
// Landing just under the threshold counts as a successful compaction and still
// re-triggers a round or two later, which is what the user sees as "constantly
// compacting".
func TestCompactionLeavesRoomForSeveralMoreRounds(t *testing.T) {
est := newEstimator(t)
s := testSettings().Normalize()
c := New(&stubChat{response: strings.Repeat("summary line\n", 40)}, est, s)
result, err := c.Compact(context.Background(), reactTurn(40), ReasonThreshold)
require.NoError(t, err)
assert.Less(t, result.TokensAfter, s.Threshold()*2/3,
"post-compaction context must sit well below the threshold, not just under it")
assert.False(t, s.ShouldCompact(result.TokensAfter),
"a compaction that still leaves the context over the threshold is a no-op")
}
// Compacting an already-compacted context must report that it cannot help,
// rather than spending another summarization call to produce the same result.
// This is what stops the every-round loop.
func TestSecondCompactionReportsNothingToDo(t *testing.T) {
est := newEstimator(t)
llm := &stubChat{response: "## Goal\nbuild a deck"}
c := New(llm, est, testSettings())
first, err := c.Compact(context.Background(), reactTurn(12), ReasonThreshold)
require.NoError(t, err)
callsAfterFirst := llm.calls
_, err = c.Compact(context.Background(), first.Messages, ReasonThreshold)
require.ErrorIs(t, err, ErrNothingToCompact)
assert.Equal(t, callsAfterFirst, llm.calls, "a no-op compaction must not call the LLM")
}
// A summary must never be summarized again: successive passes would degrade it
// into a summary of a summary. It is instead handed to the update prompt as
// prior context, and only the messages after it are new input.
func TestPreviousSummaryIsUpdatedNotResummarized(t *testing.T) {
est := newEstimator(t)
llm := &stubChat{response: "## Goal\nfirst pass"}
c := New(llm, est, testSettings())
first, err := c.Compact(context.Background(), reactTurn(12), ReasonThreshold)
require.NoError(t, err)
// New work arrives after the compaction, so there is something to fold in.
grown := append(append([]chat.Message{}, first.Messages...), reactTurn(10)[2:]...)
llm.prompts = nil
second, err := c.Compact(context.Background(), grown, ReasonThreshold)
require.NoError(t, err)
prompt := llm.prompts[0]
assert.Contains(t, prompt, "<previous-summary>")
assert.Contains(t, prompt, "first pass")
conversation := prompt[strings.Index(prompt, "<conversation>"):strings.Index(prompt, "</conversation>")]
assert.NotContains(t, conversation, "compacted into the following summary",
"the prior summary must not re-enter as conversation input")
assert.Equal(t, 1, countKind(second.Messages, chat.MessageKindCompactionSummary),
"exactly one summary survives; they must not stack")
}
// A summary cut off by the token cap reads like a valid checkpoint but ends
// mid-section, and every later round inherits that truncation as its only
// memory. A length stop is a failure, not a result.
func TestTruncatedSummaryIsRejected(t *testing.T) {
est := newEstimator(t)
llm := &stubChat{response: "## Goal\nhalf a sum", finishReason: "length"}
c := New(llm, est, testSettings())
result, err := c.Compact(context.Background(), reactTurn(12), ReasonThreshold)
require.NoError(t, err)
assert.True(t, result.Degraded)
assert.Contains(t, result.Summary, "Raw conversation archive")
assert.NotContains(t, result.Summary, "half a sum")
}
func TestSummarizerFailureFallsBackToArchive(t *testing.T) {
est := newEstimator(t)
c := New(&stubChat{err: assert.AnError}, est, testSettings())
result, err := c.Compact(context.Background(), reactTurn(12), ReasonThreshold)
require.NoError(t, err)
assert.True(t, result.Degraded)
assert.Greater(t, result.Freed(), 0, "a degraded summary still has to free room")
assert.Contains(t, result.Summary, "Raw conversation archive")
}
// Sandbox paths are the detail a prose summarizer drops first, and losing them
// makes the agent rebuild files it already wrote.
func TestFilePathsSurviveCompaction(t *testing.T) {
est := newEstimator(t)
c := New(&stubChat{response: "prose that mentions no paths"}, est, testSettings())
result, err := c.Compact(context.Background(), reactTurn(12), ReasonThreshold)
require.NoError(t, err)
assert.Contains(t, result.Summary, "<modified-files>")
assert.Contains(t, result.Summary, "/workspace/out.html")
}
// A conversation that already fits has nothing outside the keep-recent budget.
func TestSmallConversationIsLeftAlone(t *testing.T) {
est := newEstimator(t)
llm := &stubChat{response: "s"}
c := New(llm, est, testSettings())
_, err := c.Compact(context.Background(), []chat.Message{
{Role: "system", Content: "sys"},
{Role: "user", Content: "hi"},
{Role: "assistant", Content: "hello"},
}, ReasonThreshold)
require.ErrorIs(t, err, ErrNothingToCompact)
assert.Zero(t, llm.calls)
}
func TestKeepRecentIsScaledDownOnSmallWindows(t *testing.T) {
// A 20k window cannot keep the 20k default and still have room for the
// reserve and the summary; keeping it would guarantee a re-trigger.
s := Settings{MaxContextTokens: 20000, ReserveTokens: 8000}.Normalize()
assert.Equal(t, 3000, s.KeepRecentTokens, "a quarter of the usable window")
// A large window keeps the default untouched.
big := Settings{MaxContextTokens: 200000, ReserveTokens: 16384}.Normalize()
assert.Equal(t, DefaultKeepRecentTokens, big.KeepRecentTokens)
}
func TestThresholdAndShouldCompact(t *testing.T) {
s := Settings{Enabled: true, MaxContextTokens: 128000, ReserveTokens: 28672}.Normalize()
assert.Equal(t, 128000-28672, s.Threshold())
assert.False(t, s.ShouldCompact(s.Threshold()))
assert.True(t, s.ShouldCompact(s.Threshold()+1))
// No window means no budget to enforce.
assert.Zero(t, Settings{Enabled: true}.Threshold())
assert.False(t, Settings{Enabled: true}.ShouldCompact(1_000_000))
// Disabled never triggers, whatever the size.
assert.False(t, Settings{MaxContextTokens: 1000, ReserveTokens: 100}.
Normalize().ShouldCompact(1_000_000))
}
// Kind is engine bookkeeping; a provider must never see it.
func TestSummaryKindIsNotSerialized(t *testing.T) {
msg := SummaryMessage("text")
encoded, err := marshalMessage(msg)
require.NoError(t, err)
assert.NotContains(t, encoded, "compaction_summary")
assert.NotContains(t, encoded, "Kind")
}
func marshalMessage(msg chat.Message) (string, error) {
encoded, err := json.Marshal(msg)
return string(encoded), err
}
func countRole(msgs []chat.Message, role string) int {
n := 0
for _, m := range msgs {
if m.Role == role {
n++
}
}
return n
}
func countKind(msgs []chat.Message, kind chat.MessageKind) int {
n := 0
for _, m := range msgs {
if m.Kind == kind {
n++
}
}
return n
}
+366
View File
@@ -0,0 +1,366 @@
package compaction
import (
"context"
"errors"
"fmt"
"strings"
"time"
agenttoken "academy/internal/agent/token"
"academy/internal/models/chat"
"academy/internal/types"
)
const (
// maxSummarizationAttempts bounds the retries for one compaction. Each
// attempt is a full LLM round-trip, and a compaction that keeps failing is
// better served by the raw archive than by stalling the turn.
maxSummarizationAttempts = 2
// llmCallLabel identifies compaction traffic in tracing and usage
// accounting, separating it from the agent's own reasoning rounds.
llmCallLabel = "agent_context_compaction"
)
// Reason records what asked for a compaction, for logging and for the UI.
type Reason string
const (
// ReasonThreshold is the routine case: context crossed the budget.
ReasonThreshold Reason = "threshold"
// ReasonOverflow is repair after the provider rejected or truncated a
// request because the window was full.
ReasonOverflow Reason = "overflow"
)
// ErrNothingToCompact means the context cannot be made smaller: everything
// outside the keep-recent budget is already gone. Callers must treat this as a
// stop signal rather than retrying, or they reproduce the every-round
// summarization loop this package exists to prevent.
var ErrNothingToCompact = errors.New("compaction: nothing outside the keep-recent budget")
// Result describes what one compaction did.
type Result struct {
Messages []chat.Message
Summary string
Reason Reason
TokensBefore int
TokensAfter int
MessagesBefore int
MessagesAfter int
// Degraded reports that at least one summary came from the raw archive
// because the summarizer failed. The context still shrank; its memory is
// just coarser.
Degraded bool
// SplitTurn reports that the cut divided a single turn, so a turn-prefix
// summary was generated alongside the history summary.
SplitTurn bool
// Checkpoint is the part of this compaction a later turn can start from,
// or nil when there is none (see Preparation.checkpoint).
Checkpoint *Checkpoint
// Omitted counts the oldest messages left out of the summarization
// requests because they did not fit (see fitSummarizerInput).
Omitted int
}
// Checkpoint is a summary that ends exactly on a stored turn. Persisted onto
// that turn, it lets the next turn load the summary in place of everything up
// to and including the turn, instead of summarizing the same history again.
type Checkpoint struct {
// TurnID is the stored turn the summary covers through, inclusive.
TurnID string
Summary string
// Degraded reports that Summary holds a raw archive because the
// summarizer failed. It is still a checkpoint: the next compaction
// folds it in as the previous summary.
Degraded bool
}
// Freed is how much room the compaction actually recovered. A non-positive
// value means the summary cost as much as the history it replaced.
func (r *Result) Freed() int {
return r.TokensBefore - r.TokensAfter
}
// Compactor summarizes conversation history into a checkpoint.
type Compactor struct {
chatModel chat.Chat
estimator *agenttoken.Estimator
settings Settings
}
// New builds a compactor. It returns nil when compaction cannot run, so the
// caller can treat a nil compactor as "feature off" without a second flag.
func New(chatModel chat.Chat, estimator *agenttoken.Estimator, settings Settings) *Compactor {
if chatModel == nil || estimator == nil || settings.MaxContextTokens <= 0 {
return nil
}
return &Compactor{chatModel: chatModel, estimator: estimator, settings: settings.Normalize()}
}
// Settings returns the normalized settings, including the derived threshold.
func (c *Compactor) Settings() Settings {
if c == nil {
return Settings{}
}
return c.settings
}
// Compact replaces history older than the keep-recent budget with a summary.
// It returns ErrNothingToCompact when no such history exists.
func (c *Compactor) Compact(
ctx context.Context, messages []chat.Message, reason Reason,
) (*Result, error) {
if c == nil {
return nil, ErrNothingToCompact
}
prep := Prepare(messages, c.settings, c.estimator)
if prep == nil {
return nil, ErrNothingToCompact
}
history, historyDegraded := c.summarizeHistory(ctx, prep)
summary, prefixDegraded := c.appendTurnPrefix(ctx, prep, history)
summary += prep.fileOps.format()
compacted := Apply(messages, prep, summary)
return &Result{
Messages: compacted,
Summary: summary,
Reason: reason,
TokensBefore: prep.TokensBefore,
TokensAfter: c.estimator.EstimateMessages(compacted),
MessagesBefore: len(messages),
MessagesAfter: len(compacted),
Degraded: historyDegraded || prefixDegraded,
SplitTurn: prep.IsSplitTurn,
Checkpoint: prep.checkpoint(history, historyDegraded),
Omitted: prep.OmittedHistory + prep.OmittedTurnPrefix,
}, nil
}
// summarizeHistory folds the complete turns being dropped into the previous
// summary, falling back to a raw archive when the summarizer fails.
func (c *Compactor) summarizeHistory(ctx context.Context, p *Preparation) (string, bool) {
if len(p.MessagesToSummarize) == 0 {
return p.PreviousSummary, false
}
instructions := initialSummarizationInstructions
if p.PreviousSummary != "" {
instructions = updateSummarizationInstructions
}
text, err := c.summarize(
ctx, p.MessagesToSummarize, p.OmittedHistory, p.PreviousSummary, instructions, c.settings.summaryBudget(),
)
if err != nil {
// The previous summary is still the best record of everything
// before this span, so the archive is appended to it rather than
// replacing it.
return joinNonEmpty(p.PreviousSummary,
rawArchive(p.MessagesToSummarize, c.settings.summaryBudget(), c.estimator)), true
}
return text, false
}
// appendTurnPrefix adds the summary of a split turn's discarded head, so the
// retained tail still has its originating request.
func (c *Compactor) appendTurnPrefix(ctx context.Context, p *Preparation, history string) (string, bool) {
if !p.IsSplitTurn || len(p.TurnPrefixMessages) == 0 {
return history, false
}
degraded := false
prefix, err := c.summarize(
ctx, p.TurnPrefixMessages, p.OmittedTurnPrefix, "", turnPrefixInstructions, c.settings.turnPrefixBudget(),
)
if err != nil {
degraded = true
prefix = rawArchive(p.TurnPrefixMessages, c.settings.turnPrefixBudget(), c.estimator)
}
if history == "" {
history = "No prior history."
}
return history + splitTurnSeparator + prefix, degraded
}
// summarize runs one summarization call with retries.
//
// The call streams and is cancelled only when it stops producing output for
// the stall timeout, the rule the engine applies to its own rounds. A fixed
// total budget was wrong for exactly the calls that matter: the history being
// summarized can be most of the window, and a request that was prefilling and
// writing normally was cut off at 60 seconds, degraded, and started over on
// the next turn. The overall ceiling belongs to the provider transport.
func (c *Compactor) summarize(
ctx context.Context,
messages []chat.Message,
omitted int,
previousSummary, instructions string,
maxTokens int,
) (string, error) {
request := []chat.Message{
{Role: "system", Content: summarizationSystemPrompt},
{Role: "user", Content: buildSummarizationPrompt(messages, omitted, previousSummary, instructions)},
}
opts := &chat.ChatOptions{
Temperature: 0.3, // low temperature for factual summarization
MaxTokens: maxTokens,
CacheRetention: chat.CacheRetentionNone,
}
var lastErr error
for attempt := 1; attempt <= maxSummarizationAttempts; attempt++ {
content, finishReason, err := c.streamSummary(ctx, request, opts)
if err == nil {
err = validateSummary(content, finishReason)
}
if err == nil {
return strings.TrimSpace(content), nil
}
lastErr = err
if ctx.Err() != nil {
break // the turn itself was stopped; another attempt cannot help
}
}
return "", fmt.Errorf("summarization failed after %d attempts: %w",
maxSummarizationAttempts, lastErr)
}
// streamSummary collects one summarization stream. Output of any kind, the
// model's reasoning included, counts as progress and resets the stall timer.
func (c *Compactor) streamSummary(
ctx context.Context, messages []chat.Message, opts *chat.ChatOptions,
) (content, finishReason string, err error) {
callCtx, cancel := context.WithCancelCause(types.WithLLMCallMetadata(ctx, llmCallLabel, ""))
defer cancel(nil)
// Arm before ChatStream: opening the connection and waiting for headers
// must not bypass the time-to-first-output budget.
timeout := c.settings.stallTimeout()
stall := time.AfterFunc(timeout, func() {
cancel(fmt.Errorf("summarization stalled: no output for %s", timeout))
})
defer stall.Stop()
callError := func() error {
if err := ctx.Err(); err != nil {
return err
}
return context.Cause(callCtx)
}
stream, err := c.chatModel.ChatStream(callCtx, messages, opts)
if err != nil {
if callCtx.Err() != nil {
return "", "", callError()
}
return "", "", err
}
if stream == nil {
return "", "", errors.New("summarization stream was not opened")
}
// Anything the provider still sends after this returns is discarded, so
// its goroutine can finish once the cancelled context closes the stream.
defer func() {
go func() {
for {
if _, ok := <-stream; !ok {
return
}
}
}()
}()
var sb strings.Builder
streamErr := ""
for {
// select picks at random among ready cases, and a provider closes its
// stream once the turn's context is cancelled, so a stopped turn could
// otherwise read as a completed (partial) summary.
if err := callError(); err != nil {
return "", "", err
}
select {
case <-callCtx.Done():
return "", "", callError()
case chunk, ok := <-stream:
if !ok {
if err := callError(); err != nil {
return "", "", err
}
if streamErr != "" {
return "", finishReason, fmt.Errorf("summarization stream error: %s", streamErr)
}
// A stream that closes because the call was cancelled is a
// cancelled call, not an empty successful summary. Checking the
// context here, and not only in the ctx.Done() arm above, also
// settles the race where a cancellation closes the stream while
// both arms are ready and select picks between them at random.
if err := callError(); err != nil {
return "", finishReason, err
}
return sb.String(), finishReason, nil
}
stall.Reset(timeout)
if chunk.FinishReason != "" {
finishReason = chunk.FinishReason
}
switch chunk.ResponseType {
case types.ResponseTypeError:
streamErr = chunk.Content
case types.ResponseTypeThinking:
// progress, but not part of the summary
default:
sb.WriteString(chunk.Content)
}
}
}
}
// validateSummary rejects responses that cannot serve as a checkpoint.
//
// The length stop is the one worth spelling out: a summary cut off by the token
// cap reads like a valid summary but silently ends mid-section, and every later
// round inherits that truncation as its only memory of the dropped history.
// A partial summary is a failure: it cannot serve as a checkpoint.
func validateSummary(content, finishReason string) error {
if strings.TrimSpace(content) == "" {
return errors.New("empty response from LLM")
}
switch strings.ToLower(strings.TrimSpace(finishReason)) {
case types.FinishReasonIncomplete:
return errors.New(types.StreamEndedEarlyError)
case "length", "max_tokens", "max_output_tokens":
return errors.New("generation hit the token cap and the summary is incomplete")
}
return nil
}
// buildSummarizationPrompt wraps the transcript in a tag and puts the
// instructions last, so the summarizer cannot mistake conversation text for
// its own instructions.
func buildSummarizationPrompt(messages []chat.Message, omitted int, previousSummary, instructions string) string {
var sb strings.Builder
sb.WriteString("<conversation>\n")
if omitted > 0 {
fmt.Fprintf(&sb, "[%d earlier messages are not shown: they did not fit in one summarization "+
"request, and no previous summary covers them. Do not guess at what they said.]\n\n", omitted)
}
sb.WriteString(serializeConversation(messages))
sb.WriteString("\n</conversation>\n\n")
if previousSummary != "" {
sb.WriteString("<previous-summary>\n")
sb.WriteString(previousSummary)
sb.WriteString("\n</previous-summary>\n\n")
}
sb.WriteString(instructions)
return sb.String()
}
func joinNonEmpty(parts ...string) string {
kept := make([]string, 0, len(parts))
for _, p := range parts {
if strings.TrimSpace(p) != "" {
kept = append(kept, p)
}
}
return strings.Join(kept, "\n\n")
}
+118
View File
@@ -0,0 +1,118 @@
package compaction
import (
agenttoken "academy/internal/agent/token"
"academy/internal/models/chat"
)
// CutPoint is where the conversation is divided into "summarize this" and
// "keep this verbatim".
type CutPoint struct {
// FirstKeptIdx is the index of the first message kept verbatim.
FirstKeptIdx int
// TurnStartIdx is the user message that opened the turn the cut lands in,
// or -1 when the cut falls on a turn boundary.
TurnStartIdx int
// IsSplitTurn reports that the cut divides a single turn, so the part
// before it needs its own summary to explain the part after it.
IsSplitTurn bool
}
// isCutPointMessage reports whether history may be cut immediately before this
// message. Everything except a tool result qualifies. Tool results are excluded
// because they must stay with the assistant message that requested them —
// cutting between the two leaves an orphaned tool result and providers reject
// the request outright.
//
// The corollary is what makes mid-turn compaction safe: a cut that lands on an
// assistant message keeps that message and every tool result following it, so
// the pairing is never split from either side.
func isCutPointMessage(msg *chat.Message) bool {
return msg.Role != "tool"
}
// isTurnStartMessage reports whether this message opens a turn. Compaction
// summaries count: they stand in for everything before them, so the turn they
// begin is complete on its own.
func isTurnStartMessage(msg *chat.Message) bool {
return msg.Role == "user"
}
// historyStart is the first index compaction may touch. The system prompt is
// never a candidate — it carries the agent's instructions, not its history.
func historyStart(messages []chat.Message) int {
if len(messages) > 0 && messages[0].Role == "system" {
return 1
}
return 0
}
// FindCutPoint walks backwards from the newest message accumulating estimated
// size, and keeps the largest suffix that still fits keepRecentTokens.
//
// The budget is a ceiling, not a floor. Stopping at the first message that
// reaches the budget and keeping that message too is the obvious reading and
// the wrong one: a single knowledge_search result is routinely ten thousand
// tokens on its own, so "keep the recent 16k" would hand back 25k and leave
// barely a round of headroom before the next compaction.
//
// Nothing here consults the current turn. That is deliberate: the budget is the
// only rule, and a turn large enough to exceed it on its own gets split rather
// than exempted.
func FindCutPoint(
messages []chat.Message,
start, keepRecentTokens int,
estimator *agenttoken.Estimator,
) CutPoint {
if start < 0 {
start = 0
}
var cutPoints []int
for i := start; i < len(messages); i++ {
if isCutPointMessage(&messages[i]) {
cutPoints = append(cutPoints, i)
}
}
if len(cutPoints) == 0 {
return CutPoint{FirstKeptIdx: start, TurnStartIdx: -1}
}
// Worst case, keep only the newest group. A single message larger than the
// whole budget cannot be cut any further here — trimming that is the tool
// result trimmer's job, not the cut point's.
cutIdx := cutPoints[len(cutPoints)-1]
accumulated := 0
for i := len(messages) - 1; i >= start; i-- {
accumulated += estimator.EstimateMessage(&messages[i])
if accumulated > keepRecentTokens {
break
}
// Still within budget, so this is a better (earlier) place to cut.
// Only legal cut points are recorded, which keeps every tool result
// attached to the assistant message that requested it.
if isCutPointMessage(&messages[i]) {
cutIdx = i
}
}
if isTurnStartMessage(&messages[cutIdx]) {
return CutPoint{FirstKeptIdx: cutIdx, TurnStartIdx: -1}
}
turnStart := findTurnStartIdx(messages, cutIdx, start)
return CutPoint{
FirstKeptIdx: cutIdx,
TurnStartIdx: turnStart,
IsSplitTurn: turnStart >= 0,
}
}
// findTurnStartIdx returns the index of the user message that opened the turn
// containing entryIdx, or -1 if the turn started before the searchable range.
func findTurnStartIdx(messages []chat.Message, entryIdx, start int) int {
for i := entryIdx; i >= start; i-- {
if isTurnStartMessage(&messages[i]) {
return i
}
}
return -1
}
+181
View File
@@ -0,0 +1,181 @@
package compaction
// Sandbox file operations have to survive compaction.
//
// Dropping the history that says "I wrote /workspace/output/deck.html" leaves
// the model with no record of the artifacts it already produced, so it starts
// rebuilding files that are already on disk — and for a file large enough to
// need chunked writes, restarting is exactly the loop that never terminates.
// The LLM summary is supposed to carry this, but it is prose and a list of
// paths is the first detail a summarizer drops. So the paths are extracted
// mechanically, re-attached to every summary, and inherited by the next one.
//
// Three sets while collecting, two lists when rendering: a file that was
// written is reported only as modified even if it was also read.
import (
"encoding/json"
"fmt"
"strings"
agenttools "academy/internal/agent/tools"
"academy/internal/models/chat"
)
const (
readFilesTag = "read-files"
modifiedFilesTag = "modified-files"
// maxTrackedFilePaths bounds what the block can add to every subsequent
// request. A session touching more files than this has its oldest entries
// dropped rather than growing the prompt without limit.
maxTrackedFilePaths = 50
)
// fileOps is the set of sandbox paths the compacted history touched. Reads and
// writes are kept apart while collecting so a path can be reclassified when a
// later call writes something that was only read before.
type fileOps struct {
read []string
written []string
edited []string
}
func appendUnique(list []string, path string) []string {
path = strings.TrimSpace(path)
if path == "" || len(list) >= maxTrackedFilePaths {
return list
}
for _, existing := range list {
if existing == path {
return list
}
}
return append(list, path)
}
// extractFileOps collects the sandbox paths the summarized-away messages read
// and wrote. previousSummary carries the block inherited from an earlier
// compaction, which is no longer part of the message range being summarized
// now that summaries are excluded from their own successor's input.
func extractFileOps(previousSummary string, groups ...[]chat.Message) fileOps {
var ops fileOps
ops.inherit(previousSummary)
for _, group := range groups {
for _, msg := range group {
ops.inherit(messageText(msg))
for _, tc := range msg.ToolCalls {
path := toolCallPath(tc.Function.Arguments)
if path == "" {
continue
}
switch tc.Function.Name {
case agenttools.ToolWriteSandboxFile:
ops.written = appendUnique(ops.written, path)
case agenttools.ToolEditSandboxFile:
ops.edited = appendUnique(ops.edited, path)
case agenttools.ToolReadFile, agenttools.LegacyToolReadSandboxFile:
ops.read = appendUnique(ops.read, path)
}
}
}
}
return ops
}
// inherit folds an already-rendered block back into the collecting sets. Its
// modified list has been through resolve(), so it goes back in as written.
func (o *fileOps) inherit(content string) {
if content == "" {
return
}
inheritedRead, inheritedModified := parseFileOpsBlock(content)
for _, p := range inheritedRead {
o.read = appendUnique(o.read, p)
}
for _, p := range inheritedModified {
o.written = appendUnique(o.written, p)
}
}
// resolve collapses the three sets into what the model is shown: everything
// touched is "modified", and only files never written are listed as read.
func (o fileOps) resolve() (read, modified []string) {
for _, p := range o.written {
modified = appendUnique(modified, p)
}
for _, p := range o.edited {
modified = appendUnique(modified, p)
}
for _, p := range o.read {
if !contains(modified, p) {
read = appendUnique(read, p)
}
}
return read, modified
}
func contains(list []string, want string) bool {
for _, p := range list {
if p == want {
return true
}
}
return false
}
// toolCallPath pulls the `path` argument out of a tool call. Malformed or
// truncated arguments simply contribute nothing.
func toolCallPath(arguments string) string {
var args struct {
Path string `json:"path"`
}
if err := json.Unmarshal([]byte(arguments), &args); err != nil {
return ""
}
return strings.TrimSpace(args.Path)
}
// format renders the block appended to a summary, or "" when nothing was
// touched.
func (o fileOps) format() string {
read, modified := o.resolve()
if len(read) == 0 && len(modified) == 0 {
return ""
}
var sb strings.Builder
writeTagged(&sb, readFilesTag, read)
writeTagged(&sb, modifiedFilesTag, modified)
return sb.String()
}
func writeTagged(sb *strings.Builder, tag string, paths []string) {
if len(paths) == 0 {
return
}
fmt.Fprintf(sb, "\n\n<%s>\n%s\n</%s>", tag, strings.Join(paths, "\n"), tag)
}
// parseFileOpsBlock reads back what format wrote, so consecutive compactions
// accumulate instead of each forgetting the one before it.
func parseFileOpsBlock(content string) (read, modified []string) {
return parseTagged(content, readFilesTag), parseTagged(content, modifiedFilesTag)
}
func parseTagged(content, tag string) []string {
open, closeTag := "<"+tag+">", "</"+tag+">"
start := strings.Index(content, open)
if start < 0 {
return nil
}
start += len(open)
end := strings.Index(content[start:], closeTag)
if end < 0 {
return nil
}
var paths []string
for _, line := range strings.Split(content[start:start+end], "\n") {
paths = appendUnique(paths, line)
}
return paths
}
+91
View File
@@ -0,0 +1,91 @@
package compaction
import (
"strings"
"testing"
"academy/internal/models/chat"
"github.com/stretchr/testify/assert"
)
func toolCallMsg(name, arguments string) chat.Message {
return chat.Message{
Role: "assistant",
ToolCalls: []chat.ToolCall{{
ID: "call-1",
Type: "function",
Function: chat.FunctionCall{Name: name, Arguments: arguments},
}},
}
}
func TestResolveSplitsReadsFromWrites(t *testing.T) {
read, modified := extractFileOps("", []chat.Message{
toolCallMsg("write_sandbox_file", `{"path":"/workspace/output/deck.html","content":"<html>"}`),
toolCallMsg("edit_sandbox_file", `{"path":"/workspace/output/deck.html","old_string":"a"}`),
toolCallMsg("read_file", `{"path":"/workspace/input/notes.txt"}`),
toolCallMsg("read_file", `{"path":"skill://pdf/SKILL.md"}`),
toolCallMsg("shell_exec", `{"command":"ls"}`),
}).resolve()
// Written then edited is one entry, not two.
assert.Equal(t, []string{"/workspace/output/deck.html"}, modified)
assert.Equal(t, []string{"/workspace/input/notes.txt", "skill://pdf/SKILL.md"}, read)
}
// A file the agent read and then rewrote is only interesting as something it
// modified; listing it twice invites the model to re-read its own output.
func TestResolveDropsReadsThatWereAlsoWritten(t *testing.T) {
read, modified := extractFileOps("", []chat.Message{
toolCallMsg("read_sandbox_file", `{"path":"/workspace/output/deck.html"}`),
toolCallMsg("write_sandbox_file", `{"path":"/workspace/output/deck.html","content":"x"}`),
}).resolve()
assert.Equal(t, []string{"/workspace/output/deck.html"}, modified)
assert.Empty(t, read)
}
// Truncated arguments are the normal case for the calls this feature exists
// to survive, so they must be skipped rather than poison the list.
func TestExtractFileOpsIgnoresUnparseableArguments(t *testing.T) {
read, modified := extractFileOps("", []chat.Message{
toolCallMsg("write_sandbox_file", `{"path":"/workspace/output/a.html","content":"<htm`),
toolCallMsg("write_sandbox_file", `{"content":"no path here"}`),
}).resolve()
assert.Empty(t, modified)
assert.Empty(t, read)
}
// Without inheritance the second compaction forgets what the first one
// recorded, which is the whole failure mode this guards against. The prior
// block now arrives via the previous summary rather than as a message, because
// summaries no longer re-enter their successor's input.
func TestExtractFileOpsInheritsFromPreviousSummary(t *testing.T) {
earlier := fileOps{
written: []string{"/workspace/output/deck.html"},
read: []string{"/workspace/input/notes.txt"},
}
read, modified := extractFileOps("prose"+earlier.format(), []chat.Message{
toolCallMsg("write_sandbox_file", `{"path":"/workspace/output/report.md","content":"x"}`),
}).resolve()
assert.Equal(t, []string{"/workspace/output/deck.html", "/workspace/output/report.md"}, modified)
assert.Equal(t, []string{"/workspace/input/notes.txt"}, read)
}
func TestFileOpsFormatIsEmptyWhenNothingTouched(t *testing.T) {
assert.Empty(t, fileOps{}.format())
}
func TestFileOpsCapsTrackedPaths(t *testing.T) {
msgs := make([]chat.Message, 0, maxTrackedFilePaths+10)
for i := 0; i < maxTrackedFilePaths+10; i++ {
msgs = append(msgs, toolCallMsg("write_sandbox_file",
`{"path":"/workspace/output/`+strings.Repeat("a", i+1)+`.txt"}`))
}
_, modified := extractFileOps("", msgs).resolve()
assert.Len(t, modified, maxTrackedFilePaths)
}
@@ -0,0 +1,62 @@
package compaction
import (
"strings"
"testing"
agenttools "academy/internal/agent/tools"
"academy/internal/models/chat"
"github.com/stretchr/testify/require"
)
func TestOverflowAfterSanitizationPreservesPreviousSummary(t *testing.T) {
const marker = "KEEP_PRIOR_USER_DECISION_7E93"
live := reactTurn(40)
summary := SummaryMessage(marker + "\n<modified-files>\n/workspace/prior.txt\n</modified-files>")
image := chat.Message{Role: "user", MultiContent: []chat.MessageContentPart{
{Type: "image_url", ImageURL: &chat.ImageURL{URL: "data:image/png;base64,AA=="}},
{Type: "text", Text: "continue from this screenshot"},
}}
messages := append([]chat.Message{live[0], summary, image}, live[2:]...)
sanitized := agenttools.SanitizeMessages(messages)
require.Equal(t, chat.MessageKindCompactionSummary, sanitized[1].Kind)
require.Empty(t, sanitized[1].Content, "exercise the multipart-only summary produced by sanitization")
require.NotEmpty(t, sanitized[1].MultiContent)
llm := &stubChat{response: "## Goal\ncontinue the task"}
c := New(llm, newEstimator(t), testSettings())
result, err := c.Compact(t.Context(), sanitized, ReasonOverflow)
require.NoError(t, err)
require.NotEmpty(t, llm.prompts)
require.Contains(t, strings.Join(llm.prompts, "\n"), marker)
require.Contains(t, strings.Join(llm.prompts, "\n"), "<previous-summary>")
require.Contains(t, result.Summary, "/workspace/prior.txt")
require.Equal(t, marker+"\n<modified-files>\n/workspace/prior.txt\n</modified-files>",
unwrapSummary(messages[1].Content), "caller-owned messages must not be changed")
}
func TestCompactionSerializesMultipartTextWithoutLegacyDuplication(t *testing.T) {
for _, role := range []string{"user", "assistant", "tool"} {
t.Run(role, func(t *testing.T) {
message := chat.Message{Role: role, Content: "stale legacy text", MultiContent: []chat.MessageContentPart{
{Type: "text", Text: "keep first"},
{Type: "image_url", ImageURL: &chat.ImageURL{URL: "https://example.com/image.png"}},
{Type: "text", Text: " and second"},
}}
for _, rendered := range []string{serializeConversation([]chat.Message{message}), archiveLine(&message)} {
require.Contains(t, rendered, "keep first and second")
require.NotContains(t, rendered, "stale legacy text")
require.NotContains(t, rendered, "https://example.com")
}
})
}
}
func TestCompactionInheritsMultipartFileOperations(t *testing.T) {
message := chat.Message{Role: "user", MultiContent: []chat.MessageContentPart{
{Type: "text", Text: "<modified-files>\n/workspace/kept.txt\n</modified-files>"},
}}
ops := extractFileOps("", []chat.Message{message})
_, modified := ops.resolve()
require.Equal(t, []string{"/workspace/kept.txt"}, modified)
}
+116
View File
@@ -0,0 +1,116 @@
package compaction
import (
"regexp"
"strings"
"academy/internal/types"
)
// Context overflow is worth detecting precisely because the recovery is
// specific: compact and retry once. Misread as a generic failure it ends the
// turn; misread as a transient error it retries the same oversized request
// until the attempts run out. Patterns are collected from real provider
// responses.
var overflowPatterns = []*regexp.Regexp{
regexp.MustCompile(`(?i)prompt is too long`), // Anthropic
regexp.MustCompile(`(?i)request_too_large`), // Anthropic (HTTP 413)
regexp.MustCompile(`(?i)input is too long for requested model`), // Amazon Bedrock
regexp.MustCompile(`(?i)exceeds the context window`), // OpenAI
regexp.MustCompile(`(?i)exceeds (the )?(model'?s )?maximum context length`), // OpenAI-compatible proxies
regexp.MustCompile(`(?i)input token count.*exceeds the maximum`), // Google Gemini
regexp.MustCompile(`(?i)maximum prompt length is \d+`), // xAI Grok
regexp.MustCompile(`(?i)reduce the length of the messages`), // Groq
regexp.MustCompile(`(?i)maximum context length is \d+ tokens`), // OpenRouter
regexp.MustCompile(`(?i)exceeds (the )?maximum allowed input length`), // OpenRouter / Poolside
regexp.MustCompile(`(?i)is longer than the model'?s context length`), // Together AI
regexp.MustCompile(`(?i)exceeds the limit of \d+`), // GitHub Copilot
regexp.MustCompile(`(?i)exceeds the available context size`), // llama.cpp
regexp.MustCompile(`(?i)greater than the context length`), // LM Studio
regexp.MustCompile(`(?i)context window exceeds limit`), // MiniMax
regexp.MustCompile(`(?i)exceeded model token limit`), // Kimi
regexp.MustCompile(`(?i)too large for model with \d+ maximum context length`), // Mistral
regexp.MustCompile(`(?i)but the configured context size is`), // DS4
regexp.MustCompile(`(?i)model_context_window_exceeded`), // z.ai
regexp.MustCompile(`(?i)prompt too long; exceeded (max )?context length`), // Ollama
regexp.MustCompile(`(?i)range of input length should be`), // DashScope / Qwen
regexp.MustCompile(`(?i)context[_ ]length[_ ]exceeded`), // generic
regexp.MustCompile(`(?i)too many tokens`), // generic
regexp.MustCompile(`(?i)token limit exceeded`), // generic
}
// nonOverflowPatterns are errors that mention token counts while meaning
// something else. Bedrock throttling reads "ThrottlingException: Too many
// tokens, please wait before trying again", which matches an overflow pattern
// and is not one. Matched anywhere in the message rather than anchored, since
// Academy sees raw provider errors rather than a pre-normalized prefix.
var nonOverflowPatterns = []*regexp.Regexp{
regexp.MustCompile(`(?i)throttling`),
regexp.MustCompile(`(?i)service unavailable`),
regexp.MustCompile(`(?i)rate limit`),
regexp.MustCompile(`(?i)too many requests`),
}
// IsOverflowError reports whether a failed LLM call failed because the request
// did not fit in the context window.
func IsOverflowError(err error) bool {
if err == nil {
return false
}
msg := err.Error()
for _, p := range nonOverflowPatterns {
if p.MatchString(msg) {
return false
}
}
for _, p := range overflowPatterns {
if p.MatchString(msg) {
return true
}
}
return false
}
// ResponseHitContextLimit reports whether a *successful* response was shaped by
// a full context window rather than by the completion budget we asked for.
// Three shapes:
//
// - The provider accepted an oversized request silently and reported a prompt
// larger than the window (z.ai).
// - The provider truncated the input to fill the window exactly, leaving no
// room to generate, and stopped on length with no output (Xiaomi MiMo).
// - The provider stopped on length having emitted less than we allowed, so
// our own cap cannot be what stopped it (the general recoverable case).
//
// completionBudget is the max_tokens we requested, before any context-based
// clamping; passing the clamped value would hide exactly the case this detects.
func ResponseHitContextLimit(
resp *types.ChatResponse, contextWindow, completionBudget int,
) bool {
if resp == nil {
return false
}
prompt := resp.Usage.PromptTokens
completion := resp.Usage.CompletionTokens
lengthStop := isLengthStop(resp.FinishReason)
if contextWindow > 0 && prompt > contextWindow {
return true
}
if contextWindow > 0 && lengthStop && completion == 0 && prompt > 0 &&
prompt >= contextWindow*99/100 {
return true
}
// Without usage there is nothing to compare against; treating that as
// overflow would retry every ordinary truncation.
return lengthStop && completionBudget > 0 && completion > 0 && completion < completionBudget
}
func isLengthStop(reason string) bool {
switch strings.ToLower(strings.TrimSpace(reason)) {
case "length", "max_tokens", "max_output_tokens":
return true
default:
return false
}
}
@@ -0,0 +1,93 @@
package compaction
import (
"errors"
"testing"
"academy/internal/types"
"github.com/stretchr/testify/assert"
)
// Real error strings from the providers Academy talks to. An overflow read as
// a generic failure ends the turn; read as transient it retries the identical
// oversized request until the attempts run out.
func TestIsOverflowError(t *testing.T) {
overflow := []string{
"prompt is too long: 213462 tokens > 200000 maximum",
`413 {"error":{"type":"request_too_large","message":"Request exceeds the maximum size"}}`,
"Your input exceeds the context window of this model",
"Requested token count exceeds the model's maximum context length of 131072 tokens",
"Input length (265330) exceeds model's maximum context length (262144).",
"The input token count (1196265) exceeds the maximum number of tokens allowed (1048575)",
"This model's maximum prompt length is 131072 but the request contains 537812 tokens",
"Please reduce the length of the messages or completion",
"This endpoint's maximum context length is 131072 tokens",
"The input (300 tokens) is longer than the model's context length (200 tokens).",
"the request exceeds the available context size, try increasing it",
"prompt token count of 5000 exceeds the limit of 4096",
"invalid params, context window exceeds limit",
"Your request exceeded model token limit: 128000",
"Prompt has 5000 tokens, but the configured context size is 4096 tokens",
"Range of input length should be [1, 30000]",
"context_length_exceeded",
}
for _, msg := range overflow {
assert.True(t, IsOverflowError(errors.New(msg)), "should detect overflow: %s", msg)
}
// Throttling formats itself as a token complaint and is not an overflow.
notOverflow := []string{
"ThrottlingException: Too many tokens, please wait before trying again.",
"429 rate limit exceeded",
"Too many requests",
"connection reset by peer",
"context deadline exceeded",
}
for _, msg := range notOverflow {
assert.False(t, IsOverflowError(errors.New(msg)), "should not detect overflow: %s", msg)
}
assert.False(t, IsOverflowError(nil))
}
func TestResponseHitContextLimit(t *testing.T) {
const window, budget = 100000, 8000
// The general recoverable case: stopped on length below our own cap, so
// our cap is not what stopped it.
assert.True(t, ResponseHitContextLimit(&types.ChatResponse{
FinishReason: "length",
Usage: types.TokenUsage{PromptTokens: 90000, CompletionTokens: 2000},
}, window, budget))
// Used the full budget: the model simply had more to say, and retrying
// after a compaction would produce the same truncation.
assert.False(t, ResponseHitContextLimit(&types.ChatResponse{
FinishReason: "length",
Usage: types.TokenUsage{PromptTokens: 50000, CompletionTokens: budget},
}, window, budget))
// Silent overflow: the provider accepted a prompt larger than the window
// instead of rejecting it (z.ai).
assert.True(t, ResponseHitContextLimit(&types.ChatResponse{
FinishReason: "stop",
Usage: types.TokenUsage{PromptTokens: window + 1, CompletionTokens: 10},
}, window, budget))
// Server truncated the input to fill the window exactly, leaving no room
// to generate (Xiaomi MiMo).
assert.True(t, ResponseHitContextLimit(&types.ChatResponse{
FinishReason: "length",
Usage: types.TokenUsage{PromptTokens: window, CompletionTokens: 0},
}, window, budget))
// An ordinary completed response is not an overflow.
assert.False(t, ResponseHitContextLimit(&types.ChatResponse{
FinishReason: "stop",
Usage: types.TokenUsage{PromptTokens: 1000, CompletionTokens: 100},
}, window, budget))
// No usage means nothing to compare against.
assert.False(t, ResponseHitContextLimit(&types.ChatResponse{FinishReason: "length"}, window, budget))
assert.False(t, ResponseHitContextLimit(nil, window, budget))
}
+228
View File
@@ -0,0 +1,228 @@
package compaction
import (
"strings"
agenttoken "academy/internal/agent/token"
"academy/internal/models/chat"
)
// The summary is injected as a `user` message rather than a second `system`
// one. A system message is an instruction; this is conversation history, and
// providers that merge or specially weight system messages were the wrong
// place to put it.
const (
summaryPrefix = "The conversation history before this point was compacted " +
"into the following summary:\n\n<summary>\n"
summarySuffix = "\n</summary>"
)
// Preparation is everything the summarization step needs, computed before any
// LLM call so the caller can decide not to make one.
type Preparation struct {
// FirstKeptIdx is where the verbatim tail begins.
FirstKeptIdx int
// MessagesToSummarize are the complete turns being replaced by prose.
MessagesToSummarize []chat.Message
// TurnPrefixMessages is the discarded head of a split turn, summarized
// separately so the retained tail still has its originating request.
TurnPrefixMessages []chat.Message
IsSplitTurn bool
// PreviousSummary is the prior compaction's text, updated in place rather
// than summarized again.
PreviousSummary string
TokensBefore int
// OmittedHistory and OmittedTurnPrefix count the oldest messages of each
// range left out because one summarization request could not carry them
// (see fitSummarizerInput). Their file paths are still recorded.
OmittedHistory int
OmittedTurnPrefix int
fileOps fileOps
// historyFileOps is fileOps without the turn prefix, for the checkpoint.
historyFileOps fileOps
// storedTurnID is the stored turn MessagesToSummarize ends exactly on, or
// "" when it ends inside a turn or reaches into the live one.
storedTurnID string
}
// Prepare picks the cut point and collects the message ranges around it.
//
// It returns nil when compaction cannot help — no messages fall outside the
// keep-recent budget, or the last compaction already left nothing to remove.
// That nil is the guard against the failure mode where every round spends a
// summarization call on a context it cannot shrink.
func Prepare(messages []chat.Message, s Settings, estimator *agenttoken.Estimator) *Preparation {
if estimator == nil || len(messages) == 0 {
return nil
}
s = s.Normalize()
// Summarizing starts after the previous compaction's boundary. The
// summary message itself is excluded: feeding it back in is what turns
// successive compactions into a summary of a summary of a summary.
boundaryStart := historyStart(messages)
previousSummary := ""
for i := len(messages) - 1; i >= boundaryStart; i-- {
if messages[i].Kind != chat.MessageKindCompactionSummary {
continue
}
previousSummary = unwrapSummary(messageText(messages[i]))
boundaryStart = i + 1
break
}
if boundaryStart >= len(messages) {
return nil
}
cut := FindCutPoint(messages, boundaryStart, s.KeepRecentTokens, estimator)
historyEnd := cut.FirstKeptIdx
if cut.IsSplitTurn {
historyEnd = cut.TurnStartIdx
}
toSummarize := cloneRange(messages, boundaryStart, historyEnd)
var turnPrefix []chat.Message
if cut.IsSplitTurn {
turnPrefix = cloneRange(messages, cut.TurnStartIdx, cut.FirstKeptIdx)
}
if len(toSummarize) == 0 && len(turnPrefix) == 0 {
return nil
}
p := &Preparation{
FirstKeptIdx: cut.FirstKeptIdx,
IsSplitTurn: cut.IsSplitTurn,
PreviousSummary: previousSummary,
TokensBefore: estimator.EstimateMessages(messages),
fileOps: extractFileOps(previousSummary, toSummarize, turnPrefix),
historyFileOps: extractFileOps(previousSummary, toSummarize),
storedTurnID: storedTurnEndingAt(messages, historyEnd),
}
p.MessagesToSummarize, p.OmittedHistory = fitSummarizerInput(
toSummarize, s.summarizerInputBudget(estimator, previousSummary, s.summaryBudget()), estimator)
p.TurnPrefixMessages, p.OmittedTurnPrefix = fitSummarizerInput(
turnPrefix, s.summarizerInputBudget(estimator, "", s.turnPrefixBudget()), estimator)
return p
}
// fitSummarizerInput keeps the newest messages whose transcript fits budget
// and reports how many older ones it left out. The newest message is kept
// regardless; transcripts truncate each message, so one alone never matters.
//
// History can reach the whole window by design (the loader leaves overflow to
// compaction), so the part being summarized can be larger than one request
// can carry. An overflowing request is rejected, the summary degrades to a raw
// archive, and a degraded summary is never persisted, so the next turn would
// load the same history and fail the same way. Leaving the oldest messages out
// breaks that loop; the prompt says so rather than hiding it.
func fitSummarizerInput(
messages []chat.Message, budget int, estimator *agenttoken.Estimator,
) ([]chat.Message, int) {
if budget <= 0 {
return messages, 0
}
used := 0
for i := len(messages) - 1; i >= 0; i-- {
used += estimator.EstimateString(serializeConversation(messages[i : i+1]))
if used > budget && i < len(messages)-1 {
return messages[i+1:], i + 1
}
}
return messages, 0
}
// checkpoint returns the history summary as a Checkpoint when it ends exactly
// on a stored turn. The turn-prefix summary is left out: it describes part of a
// turn the next turn replays verbatim. Nothing newly summarized means the
// previous checkpoint still stands.
//
// A raw archive is kept too. Not keeping it made a failing summarizer a loop:
// the next turn loaded the same history, compacted it, failed the same way,
// and so on every turn. The archive is bounded like a summary, and the next
// compaction folds it in as the previous summary, so it is refined rather
// than kept as is.
func (p *Preparation) checkpoint(history string, degraded bool) *Checkpoint {
if p.storedTurnID == "" || len(p.MessagesToSummarize) == 0 {
return nil
}
return &Checkpoint{
TurnID: p.storedTurnID,
Summary: history + p.historyFileOps.format(),
Degraded: degraded,
}
}
// storedTurnEndingAt returns the stored turn that ends immediately before end,
// or "" if the message there is live or the same turn continues past end.
//
// Stored history always precedes the live turn, so a tagged message just
// before end means everything since the previous summary is stored history
// too, and the previous summary itself covered nothing live: a cut inside the
// live turn leaves only live messages after it.
func storedTurnEndingAt(messages []chat.Message, end int) string {
if end <= 0 || end > len(messages) {
return ""
}
turnID := messages[end-1].TurnID
if turnID == "" {
return ""
}
if end < len(messages) && messages[end].TurnID == turnID {
return ""
}
return turnID
}
// Apply rebuilds the message list as system prompt, summary, and the verbatim
// tail. Nothing between the system prompt and the cut point survives.
func Apply(messages []chat.Message, p *Preparation, summary string) []chat.Message {
tailStart := min(max(p.FirstKeptIdx, 0), len(messages))
out := make([]chat.Message, 0, 2+len(messages)-tailStart)
if historyStart(messages) == 1 {
out = append(out, messages[0])
}
out = append(out, SummaryMessage(summary))
return append(out, messages[tailStart:]...)
}
// SummaryMessage wraps summary text in the envelope the model sees, tagged so
// the next compaction recognizes it as its own output.
func SummaryMessage(summary string) chat.Message {
return chat.Message{
Role: "user",
Kind: chat.MessageKindCompactionSummary,
Content: summaryPrefix + summary + summarySuffix,
}
}
// unwrapSummary recovers the raw text from a summary message so it can be
// handed to the update prompt without its envelope.
func unwrapSummary(content string) string {
start := strings.Index(content, "<summary>")
if start < 0 {
return strings.TrimSpace(content)
}
start += len("<summary>")
end := strings.LastIndex(content, "</summary>")
if end < start {
return strings.TrimSpace(content[start:])
}
return strings.TrimSpace(content[start:end])
}
func cloneRange(messages []chat.Message, start, end int) []chat.Message {
if start < 0 {
start = 0
}
if end > len(messages) {
end = len(messages)
}
if start >= end {
return nil
}
out := make([]chat.Message, end-start)
copy(out, messages[start:end])
return out
}
+82
View File
@@ -0,0 +1,82 @@
package compaction
// The sections are not decoration: the summary is the agent's only memory of
// the dropped turns, and unstructured prose reliably loses the two things the
// next round actually needs — what is already finished, and what was about to
// happen next.
const summarizationSystemPrompt = "" +
"You are a context summarization assistant. Your task is to read a conversation " +
"between a user and an AI assistant, then produce a structured summary following " +
"the exact format specified.\n\n" +
"Do NOT continue the conversation. Do NOT respond to any questions in the " +
"conversation. ONLY output the structured summary."
const summaryFormat = "" +
"## Goal\n" +
"[What the user is ultimately trying to accomplish. May be multiple items.]\n\n" +
"## Constraints & Preferences\n" +
"- [Requirements, formats, tools, or styles the user asked for, and anything they rejected]\n" +
"- [Or \"(none)\"]\n\n" +
"## Progress\n" +
"### Done\n" +
"- [x] [Completed work, naming the specific artifacts produced and where they were written]\n\n" +
"### In Progress\n" +
"- [ ] [Current work]\n\n" +
"### Blocked\n" +
"- [Issues preventing progress, if any]\n\n" +
"## Key Decisions\n" +
"- **[Decision]**: [Brief rationale, so it is not revisited]\n\n" +
"## Next Steps\n" +
"1. [Ordered list of what should happen next]\n\n" +
"## Critical Context\n" +
"- [Facts, numbers, identifiers, paths, and error messages later steps depend on]\n" +
"- [Or \"(none)\"]\n\n" +
"Keep each section concise. Write in the same language as the conversation. " +
"Preserve exact values — paths, names, numbers, and error text — rather than " +
"paraphrasing them. Output only the summary, with no preamble.\n\n" +
"Length matters: this summary shares the context window with the retained " +
"recent messages, so it must stay compact enough to be worth having. Aim for " +
"under 500 words. Prefer short bullets over prose, and drop detail that later " +
"steps cannot act on."
const initialSummarizationInstructions = "" +
"The messages above are a conversation to summarize. Create a structured context " +
"checkpoint that another LLM will use to continue the work.\n\n" +
"Use this EXACT format:\n\n" + summaryFormat
const updateSummarizationInstructions = "" +
"The messages above are NEW conversation messages to incorporate into the existing " +
"summary provided in <previous-summary> tags.\n\n" +
"RULES:\n" +
"- PRESERVE information from the previous summary that later steps still need\n" +
"- ADD new progress, decisions, and context from the new messages\n" +
"- UPDATE the Progress section: move items from \"In Progress\" to \"Done\" when completed\n" +
"- UPDATE \"Next Steps\" based on what was accomplished\n" +
"- PRESERVE exact file paths, function names, and error messages\n" +
"- CONDENSE as you go: the result must not be longer than the previous " +
"summary unless genuinely new facts require it. Completed work collapses to " +
"one line each; superseded plans, resolved errors, and abandoned approaches " +
"come out entirely. An update that only ever grows defeats the compaction " +
"it is part of.\n\n" +
"Use this EXACT format:\n\n" + summaryFormat
// turnPrefixInstructions summarizes the discarded head of a split turn. It is
// a different job from the history summary: the retained suffix is still in
// context, so this only has to supply what the suffix cannot explain about
// itself — above all, what the user originally asked for.
const turnPrefixInstructions = "" +
"This is the PREFIX of a turn that was too large to keep. The SUFFIX (recent work) " +
"is retained in the conversation and is NOT shown here.\n\n" +
"Summarize the prefix to provide context for the retained suffix, using this EXACT " +
"format:\n\n" +
"## Original Request\n" +
"[What did the user ask for in this turn?]\n\n" +
"## Early Progress\n" +
"- [Key decisions and work done in the prefix, naming artifacts and paths]\n\n" +
"## Context for Suffix\n" +
"- [Information needed to understand the retained recent work]\n\n" +
"Be concise. Write in the same language as the conversation. Preserve exact file " +
"paths, names, and error messages. Output only the summary."
const splitTurnSeparator = "\n\n---\n\n**Turn Context (split turn):**\n\n"
+176
View File
@@ -0,0 +1,176 @@
package compaction
import (
"encoding/json"
"fmt"
"sort"
"strings"
agenttoken "academy/internal/agent/token"
"academy/internal/models/chat"
)
const (
// toolResultMaxChars caps a single tool result inside the summarization
// request. Tool output is the largest contributor to context size and the
// summary needs its gist, not its bytes.
toolResultMaxChars = 2000
// textMaxChars caps user and assistant prose, which is rarely the problem
// but should not be unbounded either.
textMaxChars = 4000
// toolArgsMaxChars caps rendered tool-call arguments. A write_sandbox_file
// call carries an entire file body in its arguments; the summary needs the
// path and the fact of the write, never the content.
toolArgsMaxChars = 400
)
// serializeConversation renders messages as a transcript rather than passing
// them as a conversation. A model handed real messages tries to continue them;
// handed a transcript, it summarizes them.
func serializeConversation(messages []chat.Message) string {
var parts []string
for i := range messages {
msg := &messages[i]
text := messageText(*msg)
switch msg.Role {
case "system":
continue
case "user":
if content := truncate(text, textMaxChars); content != "" {
parts = append(parts, "[User]: "+content)
}
case "assistant":
if msg.ReasoningContent != "" {
parts = append(parts,
"[Assistant thinking]: "+truncate(msg.ReasoningContent, textMaxChars))
}
if text != "" {
parts = append(parts, "[Assistant]: "+truncate(text, textMaxChars))
}
if calls := serializeToolCalls(msg.ToolCalls); calls != "" {
parts = append(parts, "[Assistant tool calls]: "+calls)
}
case "tool":
if content := truncate(text, toolResultMaxChars); content != "" {
parts = append(parts, fmt.Sprintf("[Tool result %s]: %s", msg.Name, content))
}
}
}
return strings.Join(parts, "\n\n")
}
func serializeToolCalls(calls []chat.ToolCall) string {
if len(calls) == 0 {
return ""
}
rendered := make([]string, 0, len(calls))
for _, tc := range calls {
rendered = append(rendered,
fmt.Sprintf("%s(%s)", tc.Function.Name, renderToolArgs(tc.Function.Arguments)))
}
return strings.Join(rendered, "; ")
}
// renderToolArgs turns an arguments JSON blob into `key=value` pairs, dropping
// oversized values. Keys are sorted so the same call always renders the same
// way, which matters when the transcript is compared across compactions.
func renderToolArgs(arguments string) string {
var parsed map[string]any
if err := json.Unmarshal([]byte(arguments), &parsed); err != nil {
return truncate(arguments, toolArgsMaxChars)
}
keys := make([]string, 0, len(parsed))
for k := range parsed {
keys = append(keys, k)
}
sort.Strings(keys)
pairs := make([]string, 0, len(keys))
for _, k := range keys {
encoded, err := json.Marshal(parsed[k])
if err != nil {
continue
}
pairs = append(pairs, fmt.Sprintf("%s=%s", k, truncate(string(encoded), toolArgsMaxChars)))
}
return strings.Join(pairs, ", ")
}
func truncate(s string, maxChars int) string {
s = strings.TrimSpace(s)
runes := []rune(s)
if len(runes) <= maxChars {
return s
}
return fmt.Sprintf("%s\n\n[... %d more characters truncated]",
string(runes[:maxChars]), len(runes)-maxChars)
}
// rawArchive is the fallback when the summarizer is unavailable. It is lossy
// and unstructured, but it keeps the tool names and paths that the next round
// needs in order not to redo finished work.
//
// It stands in for a summary, so it is held to budget tokens like one, keeping
// the newest messages. Unbounded, an archive of a history that filled the
// window could be nearly as large as the history, and a compaction that frees
// nothing leaves the request over the window.
func rawArchive(messages []chat.Message, budget int, estimator *agenttoken.Estimator) string {
lines := make([]string, 0, len(messages))
for i := range messages {
if line := archiveLine(&messages[i]); line != "" {
lines = append(lines, line)
}
}
kept := len(lines)
if budget > 0 && estimator != nil {
used := 0
for i := len(lines) - 1; i >= 0; i-- {
used += estimator.EstimateString(lines[i])
if used > budget && i < len(lines)-1 {
kept = len(lines) - 1 - i
break
}
}
}
var sb strings.Builder
sb.WriteString("Raw conversation archive (LLM summarization unavailable):\n\n")
if omitted := len(lines) - kept; omitted > 0 {
fmt.Fprintf(&sb, "[%d earlier messages omitted]\n", omitted)
}
for _, line := range lines[len(lines)-kept:] {
sb.WriteString(line)
}
return sb.String()
}
func archiveLine(msg *chat.Message) string {
text := messageText(*msg)
switch msg.Role {
case "user":
return fmt.Sprintf("- User: %s\n", truncate(text, 500))
case "assistant":
if calls := serializeToolCalls(msg.ToolCalls); calls != "" {
return fmt.Sprintf("- Assistant [%s]: %s\n", calls, truncate(text, 500))
}
return fmt.Sprintf("- Assistant: %s\n", truncate(text, 500))
case "tool":
return fmt.Sprintf("- Tool[%s]: %s\n", msg.Name, truncate(text, 500))
}
return ""
}
// messageText follows the providers' preference for multipart content. The
// sanitizer can move a compaction summary's text into MultiContent while
// retaining its Kind, so scalar Content alone is not a complete transcript.
func messageText(msg chat.Message) string {
if len(msg.MultiContent) == 0 {
return msg.Content
}
var text strings.Builder
for _, part := range msg.MultiContent {
if part.Type == "text" {
text.WriteString(part.Text)
}
}
return text.String()
}
+161
View File
@@ -0,0 +1,161 @@
// Package compaction keeps an agent turn inside the model's context window by
// replacing older conversation with a structured summary.
//
// The cut point is chosen by token budget alone — never by "keep the current
// turn intact". A ReAct loop is a single turn that can run twenty rounds and
// produce a hundred thousand tokens of tool traffic; a compactor that refuses
// to touch the current turn has nothing left to compact and burns an LLM call
// per round achieving nothing.
package compaction
import (
"time"
agenttoken "academy/internal/agent/token"
)
// DefaultReserveTokens is the floor on room kept free for the next response.
const DefaultReserveTokens = 16384
// DefaultKeepRecentTokens is how much recent conversation survives a
// compaction untouched. It is what makes compaction terminate: whatever the
// context grew to, it comes out of a compaction at roughly summary + this,
// which is far below any sane threshold, so the next round cannot immediately
// re-trigger.
const DefaultKeepRecentTokens = 20000
// Settings configures when compaction runs and how much it keeps.
type Settings struct {
// Enabled turns automatic compaction on. Disabled still allows an
// explicit overflow-recovery compaction, which is a repair, not a policy.
Enabled bool
// MaxContextTokens is the model's context window. Zero disables
// compaction entirely: without a window there is no budget to enforce.
MaxContextTokens int
// ReserveTokens is the part of the window history may not occupy because
// the next response has to fit there.
ReserveTokens int
// KeepRecentTokens is the token budget of recent messages kept verbatim.
KeepRecentTokens int
// MaxSummaryTokens caps the summarization completion. Zero falls back to
// the reserve-derived budget alone.
MaxSummaryTokens int
// StallTimeout is how long a summarization stream may produce no output
// before it is cancelled. Zero means DefaultSummarizationStallTimeout.
StallTimeout time.Duration
}
// DefaultSummarizationStallTimeout matches the engine's default stall budget
// for its own rounds.
const DefaultSummarizationStallTimeout = 120 * time.Second
func (s Settings) stallTimeout() time.Duration {
if s.StallTimeout > 0 {
return s.StallTimeout
}
return DefaultSummarizationStallTimeout
}
// Normalize fills in defaults and reconciles budgets that cannot all be
// honored at once.
func (s Settings) Normalize() Settings {
if s.ReserveTokens <= 0 {
s.ReserveTokens = DefaultReserveTokens
}
if s.KeepRecentTokens <= 0 {
s.KeepRecentTokens = DefaultKeepRecentTokens
}
// What matters is not how much a compaction frees but how much room it
// leaves before the next one. Keeping half the usable window technically
// compacts and still re-triggers within a round or two, because the
// summary takes a further slice and a single tool result can be ten
// thousand tokens. A quarter leaves the post-compaction context at roughly
// half the threshold, which is several rounds of work.
//
// Large windows are unaffected: the default keep-recent budget is already
// well under a quarter of them. Tool schemas are not subtracted here —
// keepRecentTokens is a fixed conversation budget, not "whatever is left
// after the tool list".
if s.MaxContextTokens > 0 {
usable := s.MaxContextTokens - s.ReserveTokens
if quarter := usable / 4; quarter > 0 && s.KeepRecentTokens > quarter {
s.KeepRecentTokens = quarter
}
}
return s
}
// Threshold is the context size above which history must be compacted. It is
// an absolute reserve rather than a fraction of the window, because what has
// to fit is the response, whose size does not scale with the window.
func (s Settings) Threshold() int {
if s.MaxContextTokens <= 0 {
return 0
}
return max(s.MaxContextTokens-s.ReserveTokens, 1)
}
// ShouldCompact reports whether the current context has crossed the threshold.
func (s Settings) ShouldCompact(contextTokens int) bool {
if !s.Enabled {
return false
}
threshold := s.Threshold()
return threshold > 0 && contextTokens > threshold
}
// summaryBudget is the completion cap for a summarization call: 0.8 of
// reserveTokens, clamped by the model's own output limit. The old hard-coded
// 2000 was the reason long sessions produced summaries that stopped
// mid-sentence.
//
// It is additionally capped at the keep-recent budget. The summary occupies the
// compacted context alongside the retained tail, and successive compactions
// update the summary in place under instructions to preserve what is already
// there — so without a ceiling tied to the same budget it grows every round and
// slowly reclaims the room compaction just freed.
func (s Settings) summaryBudget() int {
budget := s.ReserveTokens * 4 / 5
if s.MaxSummaryTokens > 0 && s.MaxSummaryTokens < budget {
budget = s.MaxSummaryTokens
}
if s.KeepRecentTokens > 0 && s.KeepRecentTokens < budget {
budget = s.KeepRecentTokens
}
return max(budget, 1024)
}
// summarizerInputBudget is how much transcript one summarization request can
// carry: the window less the reply, the previous summary and the prompt's own
// text, with a tenth held back because the provider counts tokens its own way.
// Zero means unbounded (no window configured).
func (s Settings) summarizerInputBudget(
estimator *agenttoken.Estimator, previousSummary string, maxTokens int,
) int {
if s.MaxContextTokens <= 0 {
return 0
}
fixed := maxTokens + summarizerPromptTokens(estimator) + estimator.EstimateString(previousSummary)
return max((s.MaxContextTokens-fixed)*9/10, 1)
}
// summarizerPromptTokens is the prompt text that surrounds the transcript:
// the system prompt and the longest instructions, plus the tags around them.
func summarizerPromptTokens(estimator *agenttoken.Estimator) int {
instructions := max(
estimator.EstimateString(initialSummarizationInstructions),
estimator.EstimateString(updateSummarizationInstructions),
estimator.EstimateString(turnPrefixInstructions),
)
return estimator.EstimateString(summarizationSystemPrompt) + instructions + summarizerPromptSlack
}
// summarizerPromptSlack covers the tags and the omission note the prompt adds
// around the transcript.
const summarizerPromptSlack = 256
// turnPrefixBudget is the smaller cap for summarizing the discarded prefix of
// a split turn: it only has to explain the retained suffix, not the session.
func (s Settings) turnPrefixBudget() int {
return max(s.summaryBudget()/2, 512)
}
@@ -0,0 +1,68 @@
package compaction
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"sync/atomic"
"testing"
"academy/internal/models/api"
"academy/internal/models/api/openaicompletions"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestCompactionHTTPStreamCheckpoint(t *testing.T) {
for _, tc := range []struct {
name string
failures int32
wantCalls int32
degraded bool
}{
{name: "complete", wantCalls: 2},
{name: "retry after EOF", failures: 1, wantCalls: 3},
{name: "persistent EOF", failures: 4, wantCalls: 4, degraded: true},
} {
t.Run(tc.name, func(t *testing.T) {
t.Setenv("SSRF_WHITELIST", "127.0.0.1")
var calls atomic.Int32
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
writeSummarySSE(w, calls.Add(1) <= tc.failures)
}))
defer server.Close()
model := openaicompletions.New(openaicompletions.Config{Endpoint: api.Endpoint{
BaseURL: server.URL, Model: "summary-test", Client: server.Client(),
}})
c := New(model, newEstimator(t), testSettings())
messages := withStoredHistory(reactTurn(12), storedTurn("stored-turn", 1, "/workspace/a.txt"))
result, err := c.Compact(context.Background(), messages, ReasonThreshold)
require.NoError(t, err)
require.NotNil(t, result.Checkpoint)
assert.Equal(t, tc.wantCalls, calls.Load())
assert.Equal(t, tc.degraded, result.Degraded)
assert.Equal(t, tc.degraded, result.Checkpoint.Degraded)
assert.NotContains(t, result.Summary, "INCOMPLETE_SUMMARY")
assert.NotContains(t, result.Checkpoint.Summary, "INCOMPLETE_SUMMARY")
if tc.degraded {
assert.Contains(t, result.Checkpoint.Summary, "Raw conversation archive")
assert.Contains(t, result.Checkpoint.Summary, "question stored-turn")
} else {
assert.Contains(t, result.Checkpoint.Summary, "COMPLETE_SUMMARY")
}
})
}
}
// The real provider must classify a clean HTTP EOF
// without finish_reason as incomplete before the compactor decides to commit.
func writeSummarySSE(w http.ResponseWriter, incomplete bool) {
w.Header().Set("Content-Type", "text/event-stream")
if incomplete {
_, _ = fmt.Fprint(w, `data: {"choices":[{"index":0,"delta":{"content":"INCOMPLETE_SUMMARY"}}]}`+"\n\n")
} else {
_, _ = fmt.Fprint(w, `data: {"choices":[{"index":0,"delta":{"content":"COMPLETE_SUMMARY"},`+
`"finish_reason":"stop"}]}`+"\n\n")
}
}
@@ -0,0 +1,120 @@
package compaction
import (
"context"
"io"
"net/http"
"net/http/httptest"
"sync/atomic"
"testing"
"time"
"academy/internal/models/api"
"academy/internal/models/api/openaicompletions"
"academy/internal/models/chat"
"github.com/stretchr/testify/require"
)
func startupTestCompactor(t *testing.T, handler http.HandlerFunc) *Compactor {
t.Helper()
t.Setenv("SSRF_WHITELIST", "127.0.0.1")
server := httptest.NewServer(handler)
t.Cleanup(server.Close)
model := openaicompletions.New(openaicompletions.Config{Endpoint: api.Endpoint{
BaseURL: server.URL, Model: "timeout-test", Client: server.Client(),
}})
s := testSettings()
s.StallTimeout = 200 * time.Millisecond
return New(model, newEstimator(t), s)
}
func TestCompactionStartupRetriesAndDegrades(t *testing.T) {
for _, persistent := range []bool{false, true} {
t.Run(map[bool]string{false: "retry recovers", true: "retry degrades"}[persistent], func(t *testing.T) {
var calls atomic.Int32
c := startupTestCompactor(t, func(w http.ResponseWriter, r *http.Request) {
_, _ = io.Copy(io.Discard, r.Body)
if calls.Add(1) == 1 || persistent {
<-r.Context().Done()
return
}
writeSummarySSE(w, false)
})
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
result, err := c.Compact(ctx, reactTurn(12), ReasonThreshold)
require.NoError(t, err)
require.NoError(t, ctx.Err())
require.EqualValues(t, maxSummarizationAttempts, calls.Load())
require.Equal(t, persistent, result.Degraded)
if persistent {
require.Contains(t, result.Summary, "Raw conversation archive")
} else {
require.Contains(t, result.Summary, "COMPLETE_SUMMARY")
}
})
}
}
func TestCompactionStartupCancellationIsNotRetried(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
var calls atomic.Int32
c := startupTestCompactor(t, func(_ http.ResponseWriter, r *http.Request) {
_, _ = io.Copy(io.Discard, r.Body)
calls.Add(1)
cancel()
<-r.Context().Done()
})
_, err := c.summarize(ctx, reactTurn(2), 0, "", initialSummarizationInstructions, 1024)
require.ErrorIs(t, err, context.Canceled)
require.EqualValues(t, 1, calls.Load())
require.NotContains(t, err.Error(), "stalled")
}
func TestCompactionStartupPreservesProviderError(t *testing.T) {
c := startupTestCompactor(t, func(w http.ResponseWriter, _ *http.Request) {
http.Error(w, "invalid API key", http.StatusUnauthorized)
})
_, _, err := c.streamSummary(context.Background(), nil, &chat.ChatOptions{})
var httpErr *api.HTTPError
require.ErrorAs(t, err, &httpErr)
require.Equal(t, http.StatusUnauthorized, httpErr.StatusCode)
}
func TestCompactionHeadersDoNotResetFirstOutputBudget(t *testing.T) {
const headerDelay = 700 * time.Millisecond
c := startupTestCompactor(t, func(w http.ResponseWriter, r *http.Request) {
_, _ = io.Copy(io.Discard, r.Body)
select {
case <-time.After(headerDelay):
case <-r.Context().Done():
return
}
w.Header().Set("Content-Type", "text/event-stream")
w.(http.Flusher).Flush()
<-r.Context().Done()
})
c.settings.StallTimeout = time.Second
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
start := time.Now()
_, _, err := c.streamSummary(ctx, nil, &chat.ChatOptions{})
require.ErrorContains(t, err, "stalled")
require.Less(t, time.Since(start), 1500*time.Millisecond)
require.NoError(t, ctx.Err())
}
func TestCompactionStallIncludesHTTPHeaders(t *testing.T) {
c := startupTestCompactor(t, func(_ http.ResponseWriter, r *http.Request) {
_, _ = io.Copy(io.Discard, r.Body)
<-r.Context().Done()
})
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
start := time.Now()
_, _, err := c.streamSummary(ctx, []chat.Message{{Role: "user", Content: "summarize"}}, &chat.ChatOptions{})
t.Logf("stall_timeout=%s; elapsed=%s; error=%v", c.settings.StallTimeout, time.Since(start), err)
require.ErrorContains(t, err, "stalled", "the configured idle timeout must cover response headers")
require.NoError(t, ctx.Err(), "the parent must remain usable for retry")
}
+156
View File
@@ -0,0 +1,156 @@
package compaction
import (
"context"
"errors"
"sync/atomic"
"testing"
"time"
"academy/internal/models/chat"
"academy/internal/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// pacedChat streams a summary as chunks spaced by gap, optionally stopping
// after a number of chunks without closing the stream: a provider that stalls.
type pacedChat struct {
stubChat
chunks []types.StreamResponse
gap time.Duration
stallFrom int // stop sending after this many chunks; 0 sends all
opened atomic.Int32
}
func (p *pacedChat) ChatStream(
ctx context.Context, _ []chat.Message, _ *chat.ChatOptions,
) (<-chan types.StreamResponse, error) {
p.opened.Add(1)
ch := make(chan types.StreamResponse)
go func() {
defer close(ch)
for i, chunk := range p.chunks {
if p.stallFrom > 0 && i >= p.stallFrom {
<-ctx.Done() // stalled until cancelled
return
}
select {
case <-time.After(p.gap):
case <-ctx.Done():
return
}
select {
case ch <- chunk:
case <-ctx.Done():
return
}
}
}()
return ch, nil
}
// closedStreamChat hands back a stream that is already closed: a provider that
// gives up the instant the call is cancelled. Closing before ChatStream returns
// makes the closed-stream arm and the ctx.Done() arm of the select ready at the
// same moment, so the outcome cannot depend on which one select happens to pick.
type closedStreamChat struct {
stubChat
}
func (c *closedStreamChat) ChatStream(
context.Context, []chat.Message, *chat.ChatOptions,
) (<-chan types.StreamResponse, error) {
ch := make(chan types.StreamResponse)
close(ch)
return ch, nil
}
func pacedSettings() Settings {
s := testSettings()
s.StallTimeout = 80 * time.Millisecond
return s
}
func answerChunks(n int) []types.StreamResponse {
chunks := []types.StreamResponse{{ResponseType: types.ResponseTypeThinking, Content: "considering"}}
for range n {
chunks = append(chunks, types.StreamResponse{ResponseType: types.ResponseTypeAnswer, Content: "## Goal "})
}
return append(chunks,
types.StreamResponse{ResponseType: types.ResponseTypeAnswer, Done: true, FinishReason: "stop"})
}
// A summarization that keeps producing output is left to finish, however long
// it takes in total: the old 60-second total budget cut exactly the large
// requests compaction exists for. Reasoning counts as progress but is not
// part of the summary.
func TestSummarizationRunsAsLongAsItProgresses(t *testing.T) {
chatModel := &pacedChat{chunks: answerChunks(10), gap: 30 * time.Millisecond}
c := New(chatModel, newEstimator(t), pacedSettings())
start := time.Now()
result, err := c.Compact(context.Background(), reactTurn(12), ReasonThreshold)
require.NoError(t, err)
require.Greater(t, time.Since(start), 4*80*time.Millisecond, "longer in total than the stall timeout")
require.False(t, result.Degraded)
assert.Contains(t, result.Summary, "## Goal")
assert.NotContains(t, result.Summary, "considering")
}
// A stream that stops producing output is cancelled after the stall timeout,
// retried once, and then degrades to the archive rather than hanging the turn.
func TestSummarizationIsCancelledWhenItStalls(t *testing.T) {
chatModel := &pacedChat{chunks: answerChunks(10), gap: 10 * time.Millisecond, stallFrom: 2}
c := New(chatModel, newEstimator(t), pacedSettings())
result, err := c.Compact(context.Background(), reactTurn(12), ReasonThreshold)
require.NoError(t, err)
require.True(t, result.Degraded)
assert.Contains(t, result.Summary, "Raw conversation archive")
assert.EqualValues(t, maxSummarizationAttempts, chatModel.opened.Load(),
"a single live turn has only its prefix to summarize, attempted twice")
}
// A stream that closes because the call was cancelled is a cancelled call, not
// an empty successful summary. Both the ctx.Done() arm and the closed-stream arm
// are ready at once here, so this pins down the answer instead of letting select
// choose it at random.
func TestSummarizationIsCancelledWhenTheStreamCloses(t *testing.T) {
c := New(&closedStreamChat{}, newEstimator(t), pacedSettings())
ctx, cancel := context.WithCancel(context.Background())
cancel()
_, _, err := c.streamSummary(ctx, nil, &chat.ChatOptions{})
require.True(t, errors.Is(err, context.Canceled), "got %v", err)
}
// An error reported inside the stream is a failed attempt, not a summary.
func TestSummarizationStreamErrorIsAFailure(t *testing.T) {
chatModel := &pacedChat{chunks: []types.StreamResponse{
{ResponseType: types.ResponseTypeAnswer, Content: "## Goal half"},
{ResponseType: types.ResponseTypeError, Content: "upstream reset", Done: true},
}}
c := New(chatModel, newEstimator(t), pacedSettings())
result, err := c.Compact(context.Background(), reactTurn(12), ReasonThreshold)
require.NoError(t, err)
require.True(t, result.Degraded)
assert.NotContains(t, result.Summary, "## Goal half")
}
// A stopped turn is not retried: another attempt cannot outlive the turn.
func TestSummarizationIsNotRetriedOnceTheTurnIsCancelled(t *testing.T) {
chatModel := &pacedChat{chunks: answerChunks(10), gap: 10 * time.Millisecond, stallFrom: 1}
c := New(chatModel, newEstimator(t), pacedSettings())
ctx, cancel := context.WithCancel(context.Background())
cancel()
_, _, err := c.streamSummary(ctx, nil, &chat.ChatOptions{})
require.True(t, errors.Is(err, context.Canceled))
_, err = c.summarize(ctx, reactTurn(2), 0, "", initialSummarizationInstructions, 1024)
require.Error(t, err)
assert.EqualValues(t, 2, chatModel.opened.Load(), "one open for streamSummary, one for summarize")
}