perf(core): hot-path CPU reduction in agent loop#912
Merged
Conversation
Replace should_compact() O(N) message scan with direct comparison against cached_prompt_tokens (#880). Remove token_counter and token_safety_margin from ContextManager as they are no longer needed. Cache EnvironmentContext on Agent at bootstrap; on skill reload refresh only git_branch and model_name instead of spawning a full git subprocess every turn (#881). Hash doom-loop content in-place by feeding stable segments directly into DefaultHasher, eliminating the intermediate normalized String allocation per tool iteration (#882). Fix double count_tokens call in prune_stale_tool_outputs for ToolResult parts: compute tokens once into a local and reuse (#883).
This was
linked to
issues
Feb 25, 2026
…mizations (#860) Add 11 unit tests covering all four sub-issues: - doom_loop_hash in-place correctness: 7 tests verifying hash output matches normalize_then_hash for plain text, tool_result, tool_use, mixed tags, multiple results, same-content different IDs, empty string - refresh_git_branch: 2 tests verifying no panic and field isolation - prune_stale_tool_outputs multi-part ToolResult pruned once per part Update docs/src/architecture/performance.md and docs/src/advanced/context.md to document cached EnvironmentContext, O(1) compaction check, in-place doom-loop hashing, and single-call token counting. Update README.md and crates/zeph-core/README.md with optimized agent loop hot-path notes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #860. Implements all four sub-issues: #880, #881, #882, #883.
Changes
should_compact()O(N) message scan with direct comparison againstcached_prompt_tokens. Removetoken_counterandtoken_safety_marginfromContextManager(no longer needed).EnvironmentContextonAgentat bootstrap; refresh onlygit_branchandmodel_nameon skill reload instead of spawning a git subprocess every turn.DefaultHasher, eliminating the intermediate normalizedStringallocation per tool iteration.count_tokenscall inprune_stale_tool_outputsforToolResultparts: compute once into a local variable and reuse.Test plan
cargo +nightly fmt --check— passescargo clippy --workspace -- -D warnings— passes (0 warnings)cargo nextest run --workspace --lib --bins— 2797 tests passed, 11 skipped