Skip to content

feat: add hooks system for CLI event interception#11029

Merged
sestinj merged 11 commits intomainfrom
nate/cli-hooks-service
Mar 4, 2026
Merged

feat: add hooks system for CLI event interception#11029
sestinj merged 11 commits intomainfrom
nate/cli-hooks-service

Conversation

@sestinj
Copy link
Contributor

@sestinj sestinj commented Mar 4, 2026

Summary

  • Adds a Claude Code-compatible hooks system for the Continue CLI that allows external handlers (shell commands, HTTP endpoints) to intercept and respond to 16 CLI event types
  • Supports regex-based event matching, typed inputs/outputs, sync/async execution, and multi-source config merging from .continue/ and .claude/ directories
  • Includes comprehensive test coverage

Test plan

  • Verify hooks config loading from multiple sources
  • Test command hook execution with stdin JSON piping
  • Validate exit code semantics (0=proceed, 2=block)
  • Confirm async hooks fire without blocking

🤖 Generated with Claude Code


Continue Tasks: ✅ 7 no changes — View all


Summary by cubic

Adds a Claude Code–compatible hooks system to the Continue CLI so external command and HTTP handlers can intercept and influence 17 CLI events. Registers HookService and tightens execution with UTF-8-safe streams, correct env var interpolation, clean EPIPE handling, and ESLint fixes.

  • New Features

    • HookService with fireEvent, reloadConfig, and getCommonFields; exposed via services.hooks.
    • Config merging from ~/.continue, .continue (including settings.local.json), and .claude; supports disableAllHooks.
    • Command/HTTP hooks follow Claude semantics (exit 0/2, JSON parsing); CONTINUE_PROJECT_DIR/CLAUDE_PROJECT_DIR set; async hooks, regex matching, dedup, parallel execution; defaults: 600s command, 30s HTTP.
    • fireHook helpers for common events; documented in AGENTS.md.
  • Bug Fixes

    • Use setEncoding("utf8") for stdout/stderr to avoid multibyte corruption.
    • Fix HTTP header env var interpolation to handle ${VAR} and $VAR forms.
    • Only suppress EPIPE errors on stdin writes; log other stdin errors.
    • Register HookService in the service container and expose via services.
    • React hooks cleanup in useService: memoize reload/deps, remove invalid eslint-disable comment, and skip hookRunner tests on Windows.

Written for commit 8782e38. Summary will update on new commits.

Adds a Claude Code-compatible hooks system that allows external handlers
(shell commands, HTTP endpoints) to intercept and respond to CLI events.
Supports 16 event types with regex-based matching, typed inputs/outputs,
sync/async execution, and multi-source config merging.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@sestinj sestinj requested a review from a team as a code owner March 4, 2026 16:04
@sestinj sestinj requested review from RomneyDa and removed request for a team March 4, 2026 16:04
@dosubot dosubot bot added size:XXL This PR changes 1000+ lines, ignoring generated files. size:XS This PR changes 0-9 lines, ignoring generated files. labels Mar 4, 2026
The fireHook.ts module references services.hooks, which requires the
HookService to be registered in the service container and exposed in
the services object.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@dosubot dosubot bot added size:XXL This PR changes 1000+ lines, ignoring generated files. and removed size:XS This PR changes 0-9 lines, ignoring generated files. size:XXL This PR changes 1000+ lines, ignoring generated files. labels Mar 4, 2026
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

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

3 issues found across 6 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="extensions/cli/src/hooks/hookRunner.ts">

<violation number="1" location="extensions/cli/src/hooks/hookRunner.ts:89">
P2: Using `data.toString()` per chunk corrupts multi-byte UTF-8 characters split across buffer boundaries. Set encoding on the streams so Node's internal `StringDecoder` handles this correctly.

(Based on your team's feedback about setting encoding to "utf8" on stdout/stderr streams instead of using `toString()` per 'data' event.) [FEEDBACK_USED]</violation>

<violation number="2" location="extensions/cli/src/hooks/hookRunner.ts:98">
P1: Missing error handler on `child.stdin` — if the child process closes its stdin pipe before the write completes, the unhandled `'error'` event on the stdin stream can crash the Node.js process.</violation>

<violation number="3" location="extensions/cli/src/hooks/hookRunner.ts:149">
P2: Regex `\$\{?(\w+)\}?` incorrectly matches unbalanced braces (e.g., `$FOO}` consumes the `}` even without an opening `{`). Use alternation to properly handle both `${VAR}` and `$VAR` forms.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

sestinj and others added 2 commits March 4, 2026 08:13
Fix unused imports, import ordering, and negated condition lint errors.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
When a hook command exits before we finish writing to stdin, an EPIPE
error is emitted. Add an error handler to suppress it gracefully.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

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

1 issue found across 1 file (changes from recent commits).

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="extensions/cli/src/hooks/hookRunner.ts">

<violation number="1" location="extensions/cli/src/hooks/hookRunner.ts:98">
P2: The new stdin error handler ignores every error, not just EPIPE as intended; this masks unexpected write failures.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

sestinj and others added 7 commits March 4, 2026 08:32
The hookRunner tests use /bin/sh syntax (>&2, single-quote echo, sleep)
that is incompatible with Windows cmd.exe.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- Use setEncoding("utf8") on stdout/stderr instead of data.toString()
  to prevent multi-byte character corruption at buffer boundaries
- Fix env var interpolation regex to use alternation instead of
  independent optionals, preventing unbalanced brace matching
- Only suppress EPIPE errors on stdin, log other errors

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The react-hooks eslint plugin is not configured in the CLI package,
so the disable comment causes an eslint error.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@sestinj sestinj merged commit abb1fc0 into main Mar 4, 2026
61 checks passed
@sestinj sestinj deleted the nate/cli-hooks-service branch March 4, 2026 23:37
@github-project-automation github-project-automation bot moved this from Todo to Done in Issues and PRs Mar 4, 2026
@github-actions github-actions bot locked and limited conversation to collaborators Mar 4, 2026
@github-actions github-actions bot added the tier 1 Big feature that took multiple weeks to launch and represents a big milestone for the product label Mar 4, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

size:XXL This PR changes 1000+ lines, ignoring generated files. tier 1 Big feature that took multiple weeks to launch and represents a big milestone for the product

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant