Skip to content

fix: reliably flush hook telemetry before process exit#515

Closed
chhhee10 wants to merge 2 commits into
mainfrom
worktree-telemetry-flush-fix
Closed

fix: reliably flush hook telemetry before process exit#515
chhhee10 wants to merge 2 commits into
mainfrom
worktree-telemetry-flush-fix

Conversation

@chhhee10

Copy link
Copy Markdown
Contributor

Problem

The hook binary is short-lived — bin/failproofai.mjs calls process.exit() the moment handleHookEvent returns. Events fired with un-awaited void trackHookEvent(...) (custom_hooks_loaded, convention_policies_loaded, the *_error events) were killed mid-flight and never reached PostHog. They only survived when a deny/instruct decision happened to add a trailing await.

Verified: an allowed tool call through the installed binary sent zero events to a local capture sink — the loads/errors were dropped by process.exit. This is the "telemetry not sending regularly" symptom.

Fix

hook-telemetry.ts now tracks every in-flight POST in a pending set and exposes flushHookTelemetry(). handleHookEvent awaits it before returning; bin's hook error path drains on throw. Delivery is now reliable regardless of decision.

No coverage change — allow decisions still send nothing by design; this only makes the events that already fire land reliably.

Verification

  • flushHookTelemetry unit tests: awaits an un-awaited send, no-op when idle, never throws on reject.
  • Handler test: flush is called even on the allow path.
  • End-to-end against a local sink: the allow path now delivers its events (was zero), and all 12 CLIs emit hook_policy_triggered tagged with the correct cli on a deny.
  • Full unit suite green (2020 tests); typecheck clean.

Note: local Next.js build can't run inside a nested git worktree (Turbopack workspace-root limitation); the CLI bundle builds fine and CI runs the full build on a clean checkout.

🤖 Generated with Claude Code

The hook binary is short-lived — bin/failproofai.mjs calls process.exit()
the moment handleHookEvent returns — so events fired with un-awaited
`void trackHookEvent(...)` (custom_hooks_loaded, convention_policies_loaded,
the *_error events) were killed mid-flight and never reached PostHog. They
only survived when a deny/instruct decision happened to add a trailing await;
on the common allow path they were dropped entirely (verified: an allowed
tool call sent ZERO events to a local capture sink).

hook-telemetry.ts now tracks every in-flight POST in a pending set and
exposes flushHookTelemetry(), which handleHookEvent awaits before returning
(and bin's hook error path drains on throw). Delivery is now reliable
regardless of decision. No change to which events fire — allow decisions
still send nothing by design.

Verified end-to-end against a local sink: the allow path now delivers its
events, and all 12 CLIs emit hook_policy_triggered tagged with the correct
cli on a deny.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Jul 15, 2026

Copy link
Copy Markdown

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 62a0c8ef-52e4-4304-99d8-9b2cb3b0ff4a

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@hermes-exosphere

Copy link
Copy Markdown
Contributor

Automated code review started - full review. Results will be posted here.

@chhhee10

Copy link
Copy Markdown
Contributor Author

Closing — folding this telemetry fix into the combined TUI + PostHog PR instead of shipping it standalone.

@chhhee10 chhhee10 closed this Jul 15, 2026
@chhhee10 chhhee10 deleted the worktree-telemetry-flush-fix branch July 15, 2026 13:54
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.

2 participants