| 1 | package agent |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "testing" |
| 6 | |
| 7 | "reasonix/internal/event" |
| 8 | "reasonix/internal/tool" |
| 9 | ) |
| 10 | |
| 11 | // A session that grew several times past the window (an earlier fold failed and |
| 12 | // every later turn added more) must be brought back by the automatic trigger, |
| 13 | // not only by a manual /compact. Each attempt folds what one summary request |
| 14 | // can carry and keeps that progress, so repeated attempts converge instead of |
| 15 | // discarding every digest because the view is still over the ceiling. |
| 16 | func TestPressureCompactionFarOverTheWindowConverges(t *testing.T) { |
| 17 | const window = 20_000 |
| 18 | prov := &denseTokenizerProvider{window: window} |
| 19 | sess := longASCIISession(120) |
| 20 | a := New(prov, tool.NewRegistry(), sess, Options{ContextWindow: window, CompactRatio: 0.8}, event.Discard) |
| 21 | hard := a.hardInputCeiling() |
| 22 | if est := a.ContextUsedTokens(); est < 3*hard { |
| 23 | t.Fatalf("fixture estimates %d tokens; want at least three times the ceiling %d", est, hard) |
| 24 | } |
| 25 | |
| 26 | used := a.ContextUsedTokens() |
| 27 | for attempt := 1; attempt <= 12; attempt++ { |
| 28 | err := prepareContext(context.Background(), a, CompactionTriggerPressure) |
| 29 | now := a.ContextUsedTokens() |
| 30 | if now >= used { |
| 31 | t.Fatalf("attempt %d left the view at %d tokens, not below the %d it started from (err=%v)", attempt, now, used, err) |
| 32 | } |
| 33 | used = now |
| 34 | if err == nil { |
| 35 | if used >= hard { |
| 36 | t.Fatalf("attempt %d reported success with %d tokens, at or above the ceiling %d", attempt, used, hard) |
| 37 | } |
| 38 | return |
| 39 | } |
| 40 | } |
| 41 | t.Fatalf("view still %d tokens (ceiling %d) after 12 attempts", used, hard) |
| 42 | } |
| 43 |