feat: Wire BudgetEngine into CLI for token-aware robot output #7
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Problem Statement
The
BudgetEnginemodule (crates/terraphim_agent/src/robot/budget.rs, ~13 KB) is fully implemented — counting tokens, trimming results, enforcing--max-content-lengthand field-mode selection — but it has zero call sites outside therobot/module. Task 1.5 of the Phase 1 spec remains unmet: users cannot pass--max-tokens,--max-results,--max-content-length, or--fieldson thesearchcommand, so robot output is unbounded and unsuitable for agent-to-agent budgets.Current State
BudgetEngine,BudgetedResults, and the field-mode enum (full/summary/minimal/custom) are exported fromrobot/mod.rsandlib.rs.grepconfirms no callers inrepl/,bin/, orcli.rs— the engine is dead code.Acceptance Criteria
searchsubcommand (REPL and CLI) accepts--max-tokens <N>,--max-results <N>,--max-content-length <N>,--fields <mode>flags.--fieldsacceptsfull|summary|minimal|custom:<comma-list>and maps to the existing field-mode enum.BudgetEngine::apply()before serialisation.budgetblock withtokens_used,tokens_budget,results_returned,results_trimmed,content_truncated: bool.2(soft-limit) matching the existing robot exit-code taxonomy.cargo test -p terraphim_agent --features repl-fullpasses.cargo clippy -p terraphim_agent --features repl-full -- -D warningspasses.Proposed Approach
searchclap struct incrates/terraphim_agent/src/repl/commands.rswith the four new optional flags, gated behind the existing robot feature.ReplHandler::handle_search(repl/handler.rs), when any budget flag is set, build aBudgetConfigand pass the raw result set throughBudgetEngine::apply()before the formatter call.robot/output.rsRobotResponseto carry an optionalbudget: Option<BudgetReport>field.BudgetEngine::Error::OverBudgettoExitCode::SoftLimitin the CLI dispatcher.robot/budget.rsfor the new CLI path and a snapshot test for the JSON shape.Dependencies
None — BudgetEngine API is already stable.
Verification
Expect JSON to contain the
budgetblock; verifytokens_used <= 500and results are truncated accordingly.Scope
~200 LOC across
repl/commands.rs,repl/handler.rs,robot/output.rs,robot/budget.rs. Single PR.Upstream: Implements Task 1.5 of
docs/specifications/terraphim-agent-session-search-tasks.mdfor terraphim/terraphim-ai.