Skip to content

refactor(agent): replace hardcoded * 4 / 5 token threshold with a named constant #417

Closed

Description

@lizhengfeng101

Description

The "80% of max_tokens" threshold is expressed inconsistently. internal/llmloop/compression.go already extracts it into a well-named constant:

// internal/llmloop/compression.go:18
tokenWarningThreshold = 0.80 // immediate sync compression

But four other call sites still hardcode the same threshold as a magic * 4 / 5 expression:

// internal/agent/agent.go:594
tokenLimit := maxAllowed * 4 / 5 // 80% of MaxTokens

// internal/agent/agent.go:790
limit := a.args.Template.MaxTokens * 4 / 5

// internal/scan/agent.go:321
limit := a.args.Template.MaxTokens * 4 / 5

// internal/scan/agent.go:555
tokenLimit := maxAllowed * 4 / 5

Using * 4 / 5 scattered across files (with an explanatory comment needed each time) is error-prone and harder to change than a single named constant.

Scope

  • File(s): internal/agent/agent.go, internal/scan/agent.go (and possibly internal/llmloop/compression.go)
  • Replace the hardcoded * 4 / 5 computations with a shared named threshold constant (e.g. reuse/promote tokenWarningThreshold = 0.80), computing int(float64(maxTokens) * threshold).
  • Keep the resulting value identical to the current integer arithmetic (verify rounding — x * 4 / 5 truncates; int(float64(x) * 0.80) should match for the relevant range, add a test if unsure).

Acceptance Criteria

  • No remaining hardcoded * 4 / 5 token-threshold computations in agent/scan.
  • The threshold is defined once as a named constant and reused.
  • Behavior is unchanged (same effective limit).
  • Tests pass (make test)
  • Code check passes (make check)

Context

Discovered during a codebase cleanup pass. Slightly more involved than a one-liner because the constant lives in a different package (llmloop) than the call sites (agent, scan), so contributors get a taste of cross-package refactoring. Watch out for integer-truncation vs float rounding when swapping the expression.

Activity

  1. added
    enhancement
    good first issue
    go
    on Jul 21, 2026
  2. added a commit that references this issue on Jul 21, 2026
    9ab0616
  3. added a commit that references this issue on Sep 17, 2026
    95478ea
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementgogood first issue

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions