fix(hooks): Prevent duplicate agent captures and skip background launch messages#523
Open
ajbmachon wants to merge 1 commit intodanielmiessler:mainfrom
Open
Conversation
…ch messages ## Problem Two bugs were causing issues with agent output capture: 1. **Duplicate captures**: Multiple SubagentStop events were capturing the same Task result multiple times, creating files with identical content but different timestamps (observed 4-6 duplicates per agent output). 2. **Missing background agent captures**: Background agents only captured the "Async agent launched successfully" message instead of actual output, because SubagentStop fires immediately when Task returns (before agent completes). ## Solution ### Deduplication (prevents duplicate files) - Added `contentHash()` using MD5 to generate content fingerprints - Added `isDuplicateContent()` that checks existing files before writing - Compares both full file hash and extracted agent output section ### Background agent detection (prevents capturing launch messages) - Added `parseBackgroundAgentLaunch()` to detect async launch messages - Added `readBackgroundAgentOutput()` to check output file status - Hook now exits early for background launches, allowing capture when agent actually completes (subsequent SubagentStop event) ## Impact - Eliminates duplicate files in MEMORY/RESEARCH/ - Background agents now only get captured once with actual results - No behavioral changes for synchronous agents - Backward compatible with existing captured files Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
ajbmachon
added a commit
to ajbmachon/Personal_AI_Infrastructure
that referenced
this pull request
Jan 28, 2026
…ch messages Cherry-picked from fix/agent-output-capture-duplicates branch. See PR danielmiessler#523 for full details. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
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
This PR fixes two bugs in the
AgentOutputCapture.hook.tsthat were causing issues with research capture:Problem Details
1. Duplicate Captures
When multiple SubagentStop events fired in sequence, each one would find and capture the same Task result from the transcript, creating multiple identical files:
2. Background Agents Not Captured
For background agents (
run_in_background: true), the hook was capturing the launch message instead of waiting for actual completion:The actual research results in the
.outputfile were never captured.Solution
Deduplication
contentHash()using MD5 to fingerprint contentisDuplicateContent()to check existing files before writingBackground Agent Detection
parseBackgroundAgentLaunch()to detect async launch messagesFiles Changed
Releases/v2.4/.claude/hooks/AgentOutputCapture.hook.tsPacks/pai-hook-system/src/hooks/AgentOutputCapture.hook.tsTesting
Impact
Thank you for reviewing! Happy to make any adjustments.
🤖 Generated with Claude Code