From 6ab4ba79cd4cec927b616c1547c31812408f0522 Mon Sep 17 00:00:00 2001 From: xgopilot Date: Thu, 23 Apr 2026 04:51:08 +0000 Subject: [PATCH] fix(runtime): increment compact counter only when compact applied Generated with [codeagent](https://github.com/qbox/codeagent) Co-authored-by: phantom5099 <245659304+phantom5099@users.noreply.github.com> --- internal/runtime/run.go | 6 ++-- .../runtime_remaining_branches_test.go | 30 +++++++++++++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/internal/runtime/run.go b/internal/runtime/run.go index 55a86ab5..7c05dee5 100644 --- a/internal/runtime/run.go +++ b/internal/runtime/run.go @@ -494,11 +494,11 @@ func (s *Service) applyCompactForState( if compactErr != nil { return compactErr } - if mode == contextcompact.ModeProactive || mode == contextcompact.ModeReactive { - state.compactCount++ - } state.session = session if result.Applied { + if mode == contextcompact.ModeProactive || mode == contextcompact.ModeReactive { + state.compactCount++ + } state.resetTokenTotals() state.nextAttemptSeq++ applied = true diff --git a/internal/runtime/runtime_remaining_branches_test.go b/internal/runtime/runtime_remaining_branches_test.go index 2eeabc2c..53400e1f 100644 --- a/internal/runtime/runtime_remaining_branches_test.go +++ b/internal/runtime/runtime_remaining_branches_test.go @@ -16,6 +16,7 @@ import ( providertypes "neo-code/internal/provider/types" approvalflow "neo-code/internal/runtime/approval" + "neo-code/internal/runtime/controlplane" "neo-code/internal/runtime/streaming" "neo-code/internal/security" agentsession "neo-code/internal/session" @@ -329,6 +330,35 @@ func TestApplyCompactForStateStrictErrorBranch(t *testing.T) { } } +func TestApplyCompactForStateDoesNotIncreaseCompactCountWhenNotApplied(t *testing.T) { + t.Parallel() + + service := &Service{ + events: make(chan RuntimeEvent, 8), + compactRunner: &stubCompactRunner{ + result: contextcompact.Result{ + Applied: false, + }, + }, + } + state := newRunState("run-apply-compact-not-applied", newRuntimeSession("session-apply-compact-not-applied")) + state.compactCount = 1 + if err := service.setBaseRunState(context.Background(), &state, controlplane.RunStatePlan); err != nil { + t.Fatalf("set base run state: %v", err) + } + + applied, err := service.applyCompactForState(context.Background(), &state, config.Config{}, contextcompact.ModeProactive, compactErrorStrict) + if err != nil { + t.Fatalf("applyCompactForState() error = %v", err) + } + if applied { + t.Fatalf("expected applied=false when compact runner result is not applied") + } + if state.compactCount != 1 { + t.Fatalf("expected compactCount to stay 1 when compact not applied, got %d", state.compactCount) + } +} + func TestExecuteToolCallWithPermissionRemainingBranches(t *testing.T) { t.Parallel()