Skip to content

Add --hook-* CLI flags to override agent hooks from the command line#2135

Open
dgageot wants to merge 1 commit intodocker:mainfrom
dgageot:board/would-is-be-possible-to-run-an-existing-708d2f4f
Open

Add --hook-* CLI flags to override agent hooks from the command line#2135
dgageot wants to merge 1 commit intodocker:mainfrom
dgageot:board/would-is-be-possible-to-run-an-existing-708d2f4f

Conversation

@dgageot
Copy link
Member

@dgageot dgageot commented Mar 16, 2026

Summary

Add five new repeatable CLI flags that let users attach lifecycle hooks to any agent without modifying its YAML config:

  • --hook-pre-tool-use <cmd> — runs before every tool call
  • --hook-post-tool-use <cmd> — runs after every tool call
  • --hook-session-start <cmd> — runs when a session starts
  • --hook-session-end <cmd> — runs when a session ends
  • --hook-on-user-input <cmd> — runs when waiting for user input

Example

docker agent run agent.yaml \
  --hook-pre-tool-use "./scripts/validate.sh" \
  --hook-post-tool-use "./scripts/log.sh" \
  --hook-session-start "./scripts/setup-env.sh"

Details

  • CLI hooks are appended to any hooks already defined in the agent's YAML config (they don't replace them)
  • Pre/post-tool-use hooks added via CLI match all tools (equivalent to matcher: "*")
  • Empty/whitespace-only commands are silently skipped
  • All flags are repeatable: pass multiple to register multiple hooks

Changes

  • pkg/config/runtime.go — Add hook string-slice fields to Config; update Clone()
  • pkg/config/hooks.go (new)HooksFromCLI(), MergeHooks(), and CLIHooks() helpers
  • pkg/config/hooks_test.go (new) — Comprehensive tests including empty-command filtering, merge semantics, clone isolation
  • cmd/root/flags.go — Register the five --hook-* flags
  • pkg/teamloader/teamloader.go — Merge CLI hooks into agent config at load time (hoisted before loop)
  • docs/features/cli/index.md — Document new flags in CLI reference
  • docs/configuration/hooks/index.md — Add CLI Flags section to hooks docs

Add five new repeatable flags (--hook-pre-tool-use, --hook-post-tool-use,
--hook-session-start, --hook-session-end, --hook-on-user-input) that let
users attach lifecycle hooks to any agent without modifying its YAML config.

CLI hooks are merged (appended) with any hooks already defined in the
agent configuration. Empty/whitespace-only commands are silently skipped.

- Add hook string-slice fields to RuntimeConfig and CLI flag registration
- Add HooksFromCLI, MergeHooks helpers with comprehensive tests
- Hoist CLIHooks() computation before the agent loop in teamloader
- Document new flags in CLI reference and hooks configuration pages

Assisted-By: docker-agent
Copy link

@docker-agent docker-agent bot left a comment

Choose a reason for hiding this comment

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

Review Summary

Assessment: 🟢 APPROVE

This PR cleanly implements CLI flags for attaching lifecycle hooks to agents without modifying YAML config. The implementation is well-structured with proper testing and documentation.

Code Quality

Hook merging logic — Correctly appends CLI hooks to config hooks without mutation
Empty command filtering — Properly skips whitespace-only commands
Clone implementation — New hook fields properly cloned with slices.Clone()
Integration point — CLI hooks merged at the right place in teamloader
Test coverage — Comprehensive tests for all edge cases (empty commands, merging, isolation)
Documentation — Both CLI reference and hooks docs updated with examples

Findings

No issues found in the changed code. The implementation follows Go best practices and handles edge cases correctly.

Note: During review, a pre-existing nil pointer dereference was identified in pkg/config/runtime.go line 40 (DefaultModel.Clone() when DefaultModel is nil). This is outside the scope of this PR but may warrant a separate fix.


Review completed by docker-agent PR reviewer

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