| 1 | package agent |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | |
| 9 | "reasonix/internal/provider" |
| 10 | ) |
| 11 | |
| 12 | func TestHardCeilingAllowsRepeatedSameTurnProgress(t *testing.T) { |
| 13 | sess := foldableSessionOverForce(6) |
| 14 | a := agentOverForce(t, &fakeProvider{reply: "digest"}, sess) |
| 15 | a.activeTurnCreatedAt.Store(42) |
| 16 | |
| 17 | if err := prepareContext(context.Background(), a, CompactionTriggerPressure); err != nil { |
| 18 | t.Fatalf("first pressure fold: %v", err) |
| 19 | } |
| 20 | version := a.currentProjectionVersion() |
| 21 | if version == 0 || a.sess.compaction.lastTurn.Load() != 42 { |
| 22 | t.Fatalf("first fold version=%d lastTurn=%d", version, a.sess.compaction.lastTurn.Load()) |
| 23 | } |
| 24 | |
| 25 | big := strings.Repeat("word ", 400) |
| 26 | for i := 0; i < 100 && a.estimatedVisibleRequestTokens(a.modelVisibleMessages()) < a.hardInputCeiling(); i++ { |
| 27 | sess.Add(provider.Message{Role: provider.RoleAssistant, Content: big}) |
| 28 | sess.Add(provider.Message{Role: provider.RoleUser, Content: "continue"}) |
| 29 | } |
| 30 | if est, hard := a.estimatedVisibleRequestTokens(a.modelVisibleMessages()), a.hardInputCeiling(); est < hard { |
| 31 | t.Fatalf("fixture did not reach hard ceiling: %d < %d", est, hard) |
| 32 | } |
| 33 | |
| 34 | if err := prepareContext(context.Background(), a, CompactionTriggerOverflow); err != nil { |
| 35 | t.Fatalf("same-turn overflow recovery: %v", err) |
| 36 | } |
| 37 | if got := a.currentProjectionVersion(); got == version { |
| 38 | t.Fatalf("same-turn overflow kept projection version %d; recovery did not run", got) |
| 39 | } |
| 40 | recoveryVersion := a.currentProjectionVersion() |
| 41 | |
| 42 | for i := 0; i < 100 && a.estimatedVisibleRequestTokens(a.modelVisibleMessages()) < a.hardInputCeiling(); i++ { |
| 43 | sess.Add(provider.Message{Role: provider.RoleAssistant, Content: big}) |
| 44 | sess.Add(provider.Message{Role: provider.RoleUser, Content: "continue"}) |
| 45 | } |
| 46 | if err := prepareContext(context.Background(), a, CompactionTriggerOverflow); err != nil { |
| 47 | t.Fatalf("second same-turn overflow recovery: %v", err) |
| 48 | } |
| 49 | secondRecoveryVersion := a.currentProjectionVersion() |
| 50 | if secondRecoveryVersion == recoveryVersion { |
| 51 | t.Fatalf("second same-turn recovery kept projection version %d", recoveryVersion) |
| 52 | } |
| 53 | |
| 54 | // The same view cannot pay for another summary. New provider-visible input, |
| 55 | // not merely another overflow signal, is what permits same-turn recovery. |
| 56 | err := prepareContext(context.Background(), a, CompactionTriggerOverflow) |
| 57 | if !errors.Is(err, ErrCompactionRequired) { |
| 58 | t.Fatalf("unchanged-view recovery error = %v, want ErrCompactionRequired", err) |
| 59 | } |
| 60 | if got := a.currentProjectionVersion(); got != secondRecoveryVersion { |
| 61 | t.Fatalf("unchanged-view recovery advanced projection version to %d, want %d", got, secondRecoveryVersion) |
| 62 | } |
| 63 | } |
| 64 |