Skip to content

Harden context token tracking and improve tool result management#40

Merged
fuseraft merged 7 commits into
mainfrom
feature/ctx
Jun 2, 2026
Merged

Harden context token tracking and improve tool result management#40
fuseraft merged 7 commits into
mainfrom
feature/ctx

Conversation

@fuseraft
Copy link
Copy Markdown
Owner

@fuseraft fuseraft commented Jun 2, 2026

Summary

Type of change

  • Bug fix
  • New feature
  • Plugin
  • Config example
  • Documentation
  • Refactor / cleanup
  • Other:

Testing

  • Added or updated unit tests
  • All tests pass locally (./build.sh --target=Test)
  • Tested manually end-to-end
  • Documentation updated where relevant
  • config/examples/ updated if config schema changed

Scott Stauffer added 7 commits June 1, 2026 20:32
- Default MaxInTurnToolPairs to 12 when MaxContextTokens is set; without
  this, tool results accumulate O(N²) within a turn for budgeted agents
  that omit explicit in-turn limits
- ShouldCompact (window mode) uses Usage.TotalTokens when available so
  reasoning tokens stripped from Content are counted toward the budget
- AccumulateCompactedUsage folds all compacted-turn costs onto the summary
  message across every compaction path, keeping MaxTotalTokens accurate
  after resume cycles
- TransitionAlreadyFired distinguishes blocked markers from fired ones so
  an A→B contract failure no longer suppresses the independent A→C signal
- Corrections injected mid-session were silently dropped for Context-spec
  agents because AssembleForAgentAsync bypasses shared-history replay;
  ExtractPendingCorrections re-injects any correction messages that appear
  after the agent's last assistant turn so retries always see the feedback
- MaxTailMessages cut by raw position, so earlier corrections on long
  histories could fall outside the retained window; step 4 now pins
  correction messages and applies the tail limit only to non-pinned
  messages, preserving original order
- IsCorrectionMessage is public so both paths share the same detection
  logic (prefix table + [fuseraft:blocked substring)
- Global MaxToolResultChars cut search/grep at the same limit as file
  reads; ToolResultCharOverrides lets callers raise the cap per tool
  without relaxing the global default
- ToolCallRecord entries were silently lost for compacted turns;
  AccumulateCompactedToolCalls now wires them into all five summary
  paths so telemetry and BuildModifiedFilesNote stay accurate
- AgentOrchestrator: add EstimateContextTokens helper and a pre-turn budget
  guard that aborts before agent.RunAsync when cumulativeTokens + estimated
  input tokens > MaxTotalTokens, preventing expensive one-turn overshoots.

- CompactionConfig: raise AntiThrashWindow default from 3 to 10 so a single
  productive compaction no longer resets the guard in long sessions.

- CorrectionEngine / GraphOrchestrator: add optional turnToolCalls parameter
  to InjectNoKeywordCorrection; the no-tool-calls gate also checks
  AgentMessage.ToolCalls so SubAgentPlugin responses are not misclassified
  as stagnation.
- intent mode silently downgraded when intentLog is null; agents resuming
  after compaction had no signal the summary was degraded — now prepends a
  visible COMPACTION WARNING block and emits a startup LogWarning when
  Compaction.Mode=intent but no IntentLogPath is configured
- patch_file / create_directory / delete_directory / set_permissions /
  copy_file / move_file bypassed sandbox boundary check when no
  FileSystemPermissions globs were configured; added SandboxedExtendedWriteFunctions
  so these always route through InspectFileSystem regardless of glob state
- verifier post-turn block only fired on EveryNTurns; TriggerOnSuspiciousTransition
  had no effect outside StateMachineSelectionStrategy — wired
  HasSuspiciousTransitionSignal to detect ConflictingEvidence/NoProgress
  corrections injected by SelectAsync and trigger the verifier immediately;
  pins EVIDENCE INCONSISTENCY / EVIDENCE AUDIT REQUIRED / MISSING ARTIFACT
  in CorrectionPrefixes so they survive MaxTailMessages trim
- expose ReadSessionContextAsync on ContextAssembler and auto-inject session
  context summary for agents without an explicit Context spec
- default IncludeReasoning and IncludeSymbolGraph to true; add MaxReplayChars
  to ContextWindowConfig with per-agent TruncateAssistantContent step in Apply
- IncludeReasoning and IncludeSymbolGraph now default to true; docs reflected
  the old false defaults, which would mislead users who expect opt-in behavior
- AntiThrashWindow default raised from 3 to 10 to prevent a single productive
  compaction from prematurely resetting the guard on long sessions
- MaxReplayChars and ToolResultCharOverrides are new ContextWindow fields;
  omitting them from the reference left the per-agent replay-truncation and
  per-tool cap-override knobs undiscoverable
- Security sandbox table was missing the extended write functions
  (patch_file, create_directory, etc.) whose sandbox enforcement was just fixed
  to fire even when no FileSystemPermissions globs are configured
- ShouldCompact was summing Usage.TotalTokens (cumulative API call cost
  per turn, growing quadratically) while TrimToWindow used chars/4;
  the trigger could fire repeatedly while the trim found nothing to drop,
  producing a stuck compaction loop in window mode
- Both now use chars/4, matching the TokenBudget calibration documented
  in CompactionConfig and sessions.md
@fuseraft fuseraft merged commit 5cbe00b into main Jun 2, 2026
3 checks passed
fuseraft pushed a commit that referenced this pull request Jun 2, 2026
- craft-orchestration and config-audit had no mention of the three
  knowledge-layer plugins shipped in PR #40/#41; agents building or
  auditing configs would silently omit them
- schema-cheatsheet updated so the full plugin table is current
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant