Skip to content

fix(hooks): Prevent duplicate agent captures and skip background launch messages#523

Open
ajbmachon wants to merge 1 commit intodanielmiessler:mainfrom
ajbmachon:fix/agent-output-capture-duplicates
Open

fix(hooks): Prevent duplicate agent captures and skip background launch messages#523
ajbmachon wants to merge 1 commit intodanielmiessler:mainfrom
ajbmachon:fix/agent-output-capture-duplicates

Conversation

@ajbmachon
Copy link

Summary

This PR fixes two bugs in the AgentOutputCapture.hook.ts that were causing issues with research capture:

  • Duplicate file prevention: Same agent output was being captured 4-6 times with slightly different timestamps
  • Background agent handling: Background agents were only capturing the "launched" message, missing the actual research results

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:

2026-01-25-103947_AGENT-default_RESEARCH_council-response...md  (3013 bytes)
2026-01-25-103957_AGENT-default_RESEARCH_council-response...md  (3013 bytes)
2026-01-25-104132_AGENT-default_RESEARCH_council-response...md  (3013 bytes)
2026-01-25-104133_AGENT-default_RESEARCH_council-response...md  (3013 bytes)

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:

Full task output for debugging: Async agent launched successfully.
agentId: a27e24b
output_file: /private/tmp/claude/.../tasks/a27e24b.output

The actual research results in the .output file were never captured.

Solution

Deduplication

  • Added contentHash() using MD5 to fingerprint content
  • Added isDuplicateContent() to check existing files before writing
  • Compares both full file hash and extracted agent output section

Background Agent Detection

  • Added parseBackgroundAgentLaunch() to detect async launch messages
  • Hook exits early for background launches
  • Capture happens when agent actually completes (subsequent SubagentStop event)

Files Changed

  • Releases/v2.4/.claude/hooks/AgentOutputCapture.hook.ts
  • Packs/pai-hook-system/src/hooks/AgentOutputCapture.hook.ts

Testing

  • Verified hook compiles and runs without errors
  • Logic tested against observed debug logs showing the failure patterns
  • Changes are backward compatible with existing captures

Impact

  • Eliminates duplicate files in MEMORY/RESEARCH/
  • Background agents captured with actual results
  • No behavioral changes for synchronous agents

Thank you for reviewing! Happy to make any adjustments.

🤖 Generated with Claude Code

…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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant