Repository navigation
perf(agent): memoize toolSetDigest per tool set - #1410
Merged
Merged
Conversation
toolSetDigest re-hashed the full tool array on every call. Cache the digest in a WeakMap keyed by array identity: the director owns stable tool arrays and swaps them wholesale (constructor, updateToolDefinitions) without in-place mutation, so the same reference always means the same content and a new reference (tools changed) recomputes. A WeakMap also avoids retaining arrays the director has dropped. Output is byte-identical for the same tool set. Add a spy test proving one sha256 per tool set across repeated digest calls, using withMockedModule from testkit/mock-module.ts.
Contributor
|
No changes needed from you. A maintainer will review this pull request. |
The digest is only ever consumed by ChatDirectorImpl.updateToolDefinitions, so a module-level WeakMap keyed by array identity is heavier than the call pattern needs. Move the memoization onto the director as a last-digest field: the incoming set is hashed once when installed and the previous set's digest is reused from the field on the next update. toolSetDigest becomes a pure function with identical digest values; the proof test now spies through updateToolDefinitions to show one hash per installed tool set per director.
TheGreatAxios
commented
Oct 12, 2026
TheGreatAxios
left a comment
Collaborator
Author
There was a problem hiding this comment.
Review · Comment
Memoizes toolSetDigest on the director. Incoming tool sets are hashed when installed; the previous digest is an instance field used only to skip a repeat debug log.
Findings
No findings. bun test src/agent/tool-set-digest.test.ts src/agent/director.test.ts — 88 pass, 0 fail.
Commit bodies on this branch wrap past 72 characters and walk the implementation (perf(agent): memoize toolSetDigest per director, not per module). Subjects are within the limit. Not a code defect.
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.
Summary
toolSetDigest(src/agent/director.ts) re-hashed the full tool array on every call. Memoize the digest per stable tool-set identity.Fix approach — cache semantics
WeakMap<readonly ToolDefinition[], string>keyed by array identity.updateToolDefinitions) — callers pass freshly computed arrays (computeAdvertised,currentDefinitions) and_toolDefinitionsis never mutated in place. So the same reference always means the same content, and a new reference (tools changed) misses the WeakMap and recomputes.Proof test
src/agent/tool-set-digest.test.tsspies onnode:cryptocreateHash(viawithMockedModulefromtestkit/mock-module.ts) and asserts:toolSetDigest(tools)calls with the same array run sha256 exactly once.Verification
bun test src/agent/director.test.ts src/agent/director-compaction.test.ts src/agent/tool-set-digest.test.ts→ 93 pass, 0 failbun run typecheck→ exit 0oxfmt --check+oxlinton changed files → cleanDiff stat