Skip to content

refactor(audit): use agent chains in context#166

Merged
sks merged 4 commits intomainfrom
refactor/audit-context-agent-chains
Mar 31, 2026
Merged

refactor(audit): use agent chains in context#166
sks merged 4 commits intomainfrom
refactor/audit-context-agent-chains

Conversation

@sks
Copy link
Copy Markdown
Contributor

@sks sks commented Mar 31, 2026

Summary

Refactors audit/context.go to properly use agent chains for tracking agent delegation paths.

Changes

  • Rename key types: agentNameKeyType/agentNameKeyagentChainKeyType/agentChainKey to reflect that the context value is a delegation chain
  • Fix slice aliasing bug: WithAgentName now copies the chain before appending, preventing parent context mutation across independent branches
  • Update tests: Fix stale first-write-wins expectation, add AgentChainFromContext tests (empty, single, multi-level, branch isolation)

Testing

  • 29/29 audit tests pass
  • 13/13 orchestratorcontext tests pass
  • 0 lint issues

- 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.
@sks sks requested a review from a team as a code owner March 31, 2026 06:15
Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

sks added 3 commits March 30, 2026 23:21
… 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])
@sks sks merged commit 26b310a into main Mar 31, 2026
6 checks passed
@sks sks deleted the refactor/audit-context-agent-chains branch March 31, 2026 06:29
@github-actions github-actions bot locked and limited conversation to collaborators Mar 31, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant