Skip to content

fix: dead token-budget tracking after working memory section in BuildPrompt #264

@claude

Description

@claude

Problem

In internal/template/render.go, BuildPrompt tracks a token remaining budget and decrements it as sections are appended. At lines 305-307, the budget is decremented after writing the working memory section, then immediately discarded:

wmSection := buildWorkingMemorySection(wm, remaining)
if wmSection != "" {
    sb.WriteString(wmSection)
    remaining -= CountTokens(wmSection)
    _ = remaining   // value is never read after this
}

The _ = remaining confirms the value is dead. The footer on line 311 is always appended unconditionally without a budget check. This means:

  1. The remaining -= line is dead code.
  2. The footer appends even when the prompt is already at or over the token budget.
  3. Any section added after working memory in the future would use the wrong (pre-working-memory) remaining value.

Fix

Remove the dead remaining -= and _ = remaining lines. If the footer should respect the token budget, add a guard before appending it.

Location

  • internal/template/render.go, lines 303-311 (BuildPrompt)

@claude please implement this

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions