agent-orchestrator/packages/plugins/agent-claude-code
prateek 8e5b23e6a0
feat: auto-update session metadata via Claude Code hooks (#34)
* feat: auto-update session metadata via Claude Code hooks

Implements INT-1355: Auto-update session metadata when agent creates PR or switches branch.

## What Changed

- **agent-claude-code plugin**: Added `postLaunchSetup` method that writes a Claude Code hook to `.claude/settings.json` and `.claude/metadata-updater.sh` in each workspace
- **session-manager**: Added `AO_DATA_DIR` environment variable to agent sessions
- **Hook script**: Monitors Bash commands and automatically updates metadata on:
  - `gh pr create` → extracts PR URL, sets `pr=<url>` and `status=pr_open`
  - `git checkout -b` / `git switch -c` → extracts branch name, sets `branch=<name>`
  - `git checkout` / `git switch` (existing branches) → updates branch name for feature branches
  - `gh pr merge` → sets `status=merged`

## How It Works

1. When a session is spawned, `postLaunchSetup` runs after the agent launches
2. Creates `.claude/settings.json` with a PostToolUse hook for Bash commands
3. Writes the metadata updater script to `.claude/metadata-updater.sh`
4. The hook fires after every Bash command execution
5. Parses command and output to detect git/gh operations
6. Updates the session metadata file directly using atomic file operations

## Benefits

- Dashboard shows correct PR associations immediately without manual intervention
- Branch tracking stays in sync with agent actions
- Orchestrator has accurate session state for automation and notifications

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>

* fix: correct hook payload field name and JSON parsing

Fixes bugbot issues from PR #34:

1. Changed tool_output to tool_response - Claude Code hooks provide
   output in the tool_response field, not tool_output. This fixes PR
   URL extraction from gh pr create commands.

2. Replaced over-escaped sed patterns with cut - The fallback JSON
   parser now uses 'cut -d\" -f4' instead of sed with capture groups,
   which is simpler and avoids escaping issues.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>

* fix: escape special characters in metadata values

Fixes bugbot issue: metadata updates break on special branch names.

The update_metadata_key function now escapes sed special characters
(& | / \) in values before using them in sed replacement. This prevents
branch names or PR URLs containing these characters from breaking the
metadata update.

Example: branch name "feature/foo&bar" now correctly updates metadata
instead of causing sed to interpret "&" as a backreference.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>

* fix: only update metadata on successful commands

Fixes bugbot issue: metadata updates ignore command failure.

The hook now checks the exit_code field from the hook payload and only
updates metadata if the command succeeded (exit code 0). This prevents
updating metadata when git/gh commands fail.

Example: if 'gh pr create' fails due to auth issues, the metadata won't
be incorrectly updated with a pr= line.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>

* fix: use correct timeout units for Claude Code hook

Fixes bugbot issue: hook timeout uses wrong units (High Severity).

Claude Code hook timeouts are in milliseconds, not seconds. Changed
timeout from 5 to 5000 (5 seconds) to give the metadata updater script
enough time to parse tool output and update metadata.

Note: The hook command path is properly handled by JSON.stringify
and does not need additional escaping as Claude Code handles command
execution correctly.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>

* fix: PR URL regex and stale hook path issues

Fixes 2 bugbot issues:

1. PR URL extraction regex never matches (High Severity): Changed
   'github\.com' to 'github[.]com' because in single-quoted bash
   strings, backslashes are literal. The [.] syntax correctly matches
   a literal dot in grep regex.

2. Shared Claude settings keep stale hook path (Medium Severity):
   Changed hook detection to always update the command path to the
   current workspace, not just check for existence. This handles cases
   where .claude settings are shared/symlinked across workspaces.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-02-15 02:29:31 +05:30
..
src feat: auto-update session metadata via Claude Code hooks (#34) 2026-02-15 02:29:31 +05:30
package.json feat: agent plugins, OpenCode plugin, integration tests, CI (#5) 2026-02-14 11:28:42 +05:30
tsconfig.json feat: agent plugins, OpenCode plugin, integration tests, CI (#5) 2026-02-14 11:28:42 +05:30