refactor(audit): use agent chains in context#166
Merged
Conversation
- Rename agentNameKeyType/agentNameKey to agentChainKeyType/agentChainKey to reflect that the context value is a delegation chain, not a single name. - Fix slice aliasing bug in WithAgentName: copy the chain before appending to prevent parent context mutation across independent branches. - Update tests: fix stale first-write-wins expectation to match chain semantics, add AgentChainFromContext tests covering empty, single, multi-level delegation, and branch isolation scenarios. All 29 audit tests pass, 0 lint issues.
There was a problem hiding this comment.
Code Review
This pull request transitions the audit context from storing a single agent name to maintaining a full delegation chain, enabling better visibility into nested agent calls. The review feedback highlights opportunities to optimize performance by avoiding unnecessary slice copies when retrieving the active agent, preventing redundant entries in the chain, and ensuring context immutability by returning a copy of the delegation chain to callers.
… direct access in AgentNameFromContext - AgentNameFromContext reads ctx.Value directly to avoid unnecessary slice copy when only the last element is needed. - AgentChainFromContext returns a defensive copy and nil for empty chains. - Extract agentChainFromContext private helper for internal use without copying.
WithAgentName now returns ctx unchanged when name matches the last element in the chain, avoiding redundant allocations and confusing 'switching from A to A' log messages.
- Consecutive identical name is deduplicated (chain stays [bot-a]) - Same name at different positions still appends ([bot-a, bot-b, bot-a])
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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
Refactors
audit/context.goto properly use agent chains for tracking agent delegation paths.Changes
agentNameKeyType/agentNameKey→agentChainKeyType/agentChainKeyto reflect that the context value is a delegation chainWithAgentNamenow copies the chain before appending, preventing parent context mutation across independent branchesAgentChainFromContexttests (empty, single, multi-level, branch isolation)Testing