Skip to content

feat(agents): surface before_tool_call hook failures to Sentry + audit trace (ENG-19492) - #209

Merged
william-antoline-boon merged 13 commits into
boonfrom
william-antoline-boon/eng-19492-hook-failure-visibility
Sep 8, 2026
Merged

william-antoline-boon merged 13 commits into
boonfrom
william-antoline-boon/eng-19492-hook-failure-visibility

Conversation

@william-antoline-boon

@william-antoline-boon william-antoline-boon commented Sep 8, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

When a before_tool_call hook throws, the runtime catches it and reports the tool call downstream as the bare status "blocked" — indistinguishable from a deliberate policy veto — while the real exception text goes only to one local gateway log line. In the Egan "Thread tangled — tools aren't landing" P0 (ENG-19492) that log had rotated by the time anyone investigated, leaving the failure undiagnosable for days. This is the ticket's acceptance criterion #2: "currently we cannot count occurrences."

This PR makes that error text visible in two durable places, without changing behaviour for a deliberate block or a successful call.

What changed

Half A — Alert (Sentry). A new fire-and-forget before_tool_call_hook_failed hook, emitted from the catch block only on the kind:"failure" path (a genuinely thrown handler — never a kind:"veto"), consumed by sentry-monitor and reported as a fingerprinted exception (grouped by tool + normalized error).

Half B — Persist (audit trace). The same error text is threaded as an optional detail field through HookOutcome → pre-execution-block state map → toolMetas → normalizeToolMetas → buildTraceToolSummary → AgentDecisionTrace, so it lands in boon-core's durable audit_trace (bounded to 500 chars, blocked-entries only). The next occurrence is diagnosable by DB query — no log spelunking.

The invariant

kind:"veto" (loop-breaker, approval-required, plugin block:true — the system working as intended) is byte-for-byte unchanged: no detail, no Sentry alert. Only a genuine crash (kind:"failure") gets the new treatment. A successful call is untouched.

Notable implementation details

  • The pre-execution-block registry changes from a Set to a Map to carry the detail; eviction switches to .keys() accordingly.
  • consumePreExecutionBlockedToolCall now returns { blocked, detail? }; the codex app-server consumer extracts .blocked to preserve its exact prior behaviour.
  • The hook emission is wrapped in its own try/catch so observability can never mask or replace the block outcome being returned.

Scope

This is the "make it diagnosable" layer. Softening the customer-facing "Thread tangled" fallback and in-thread retry are separate follow-ups that depend on this landing first — a retry policy needs to know what failed (not just "blocked") to decide between a silent retry and telling the user. The customer's experience is intentionally unchanged in this PR.

Compatibility

Fully additive — detail is optional at every hop and absent for a veto, so schemaVersion stays 1 and existing consumers ignore it.

Testing

TDD throughout; each pipeline hop has a test so a regression that drops detail anywhere fails loudly. 333 targeted tests pass, 0 fail across 13 files (incl. an end-to-end test reproducing the exact Egan shape: exec ok → message blocked-by-hook-failure → real error text in evidence[].detail). All four tsgo configs (core/extensions, prod/test) are clean. oxlint + oxfmt clean.

Fleet-Impact: A-free (ships via the standard version roll; no config-schema change).

🤖 Generated with Claude Code


Summary by cubic

Sends thrown before_tool_call hook errors to Sentry and persists pre-execution failure detail in the audit trace so failures stay diagnosable after the fact; previously they collapsed to a bare "blocked" status indistinguishable from a policy veto, with the real exception text confined to a transient local log line. Deliberate vetoes and successful calls are unchanged, and the customer-facing experience is intentionally untouched — in-thread retry and copy changes are follow-ups that depend on this landing first.

Bug Fixes

  • Fires a new before_tool_call_hook_failed hook only when the handler invocation itself throws — never a veto, never a surrounding-pipeline throw (trusted policy, approval, skill-workshop) — reported as a fingerprinted Sentry exception correlated by the real sessionId (with the sessionKey routing key kept as its own session_key field).
  • Records the error text in the catch block so the detail survives both the throw and return block paths; the map-held copy is capped at 1024 characters (the full text still reaches the log and Sentry) and the audit-trace detail field is capped at 500, both using UTF-16-safe truncation so no cap splits a surrogate pair.
  • Pre-execution block registry becomes a Map; consumePreExecutionBlockedToolCall returns { blocked, detail? }; emission is fire-and-forget so observability never masks the block outcome.
  • Fully additive: schemaVersion stays 1 and existing consumers ignore detail.
  • Satisfies the ENG-19492 acceptance criterion of being able to count occurrences.
  • Registers the new hook in the plugin contract allowlist, bumps the SDK export budget so CI contract checks stay green, and documents it in the sentry-monitor capture table (capture requires the hook in a host's hooks allow-list).

Written for commit 976cb61. Summary will update on new commits.

Review in cubic

…t trace (ENG-19492)

When a before_tool_call hook throws, the runtime caught it and reported the tool
call downstream as the bare status "blocked" — indistinguishable from a
deliberate policy veto — while the real exception text went only to one local
gateway log line. In the Egan "Thread tangled — tools aren't landing" P0 that log
had rotated by the time anyone looked, leaving the failure undiagnosable. This is
the ticket's AC #2 ("currently we cannot count occurrences").

Make that error text visible in two durable places, without changing behaviour
for a deliberate block or a successful call:

- Alert: a new fire-and-forget `before_tool_call_hook_failed` hook, emitted from
  the catch block ONLY on the kind:"failure" path (never a kind:"veto"), consumed
  by sentry-monitor and reported as a fingerprinted exception.
- Persist: the same error text is threaded as an optional `detail` field through
  HookOutcome -> pre-execution-block state map -> toolMetas -> normalizeToolMetas
  -> buildTraceToolSummary -> AgentDecisionTrace, so it lands in boon-core's
  durable audit_trace (bounded to 500 chars, blocked-entries only).

Fully additive: `detail` is optional at every hop and absent for a veto, so
schemaVersion stays 1 and existing consumers are unaffected. A deliberate block
(loop-breaker, approval-required, plugin veto) and a successful call are byte-for-
byte unchanged, with no new Sentry noise.

The pre-execution-block registry changes from a Set to a Map to carry the detail;
its eviction switches to .keys() accordingly, and the codex app-server consumer
extracts .blocked from the new { blocked, detail? } return shape.

This is the "make it diagnosable" layer; softening the customer-facing fallback
and in-thread retry are separate follow-ups that depend on this landing first.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@william-antoline-boon
william-antoline-boon requested a review from a team September 8, 2026 07:06

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

All reported issues were addressed across 26 files

Tip: instead of fixing issues one by one fix them all with cubic

Re-trigger cubic

Comment thread src/plugins/hooks.ts Resolved
Comment thread src/auto-reply/reply-payload.ts Resolved
…/eng-19492-hook-failure-visibility

# Conflicts:
#	src/agents/embedded-agent-runner/run.trace-tool-summary.test.ts
#	src/agents/embedded-agent-runner/run.ts
…+ SDK export budget (ENG-19492)

Two contract checks flagged the new hook/type surface:
- boundary-invariants: sentry-monitor's api.on("before_tool_call_hook_failed")
  must appear in the bundled-plugin typed-hook allowlist.
- plugin-sdk-surface-report: the new public PluginHookBeforeToolCallFailedEvent
  type grows the public export surface by 2 (across two entrypoints, like every
  hook event type), so bump the default budget 10402 -> 10404.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…address review (ENG-19492)

Addresses cubic review on PR #209:

- P1 (real bug): both execution wrappers THROW on a kind:"failure" block
  (they never call buildBlockedToolResult), so the detail added at the
  veto-only call site never reached production. Record the detail in
  runBeforeToolCallHook's catch so it survives to consumePreExecutionBlockedToolCall
  -> toolMetas -> audit_trace on every failure path, throw or return. Added a
  regression test proving the state-map round-trip.
- P1 (identity): carry sessionKey on the failure event and into the Sentry
  run context. org_id/user_id are boon-core concepts not available at the
  openclaw gateway layer; the customer is already identified by the Sentry
  environment tag.
- P2: fire-and-forget the observer emission so a slow/failing observer never
  blocks the block outcome; outer try/catch + .catch handle sync and async
  failures.
- P3: remove Linear ticket references from all changed source comments and
  test titles per the repo convention.

Completed the partial-mock hook runners in the before_tool_call e2e tests with
the new method.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

All reported issues were addressed across 22 files (changes from recent commits).

Requires human review: Auto-approval blocked because this review re-detected 1 unresolved issue already reported by Cubic.
Tip: Review your code locally with the cubic CLI to iterate faster.

Fix all with cubic | Re-trigger cubic

…try hook

Addresses two more cubic review findings on PR #209:
- P3: bound the audit-trace detail with the repo's truncateUtf16Safe helper so
  the 500-unit cap can never split a surrogate pair (emoji) into a malformed
  final character. Added a surrogate-boundary test.
- P3: document before_tool_call_hook_failed in the sentry-monitor capture table
  and prose so the Sentry contract stays accurate.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Addresses cubic re-review on PR #209:
- P3: the failure-path tests write to the module-level pre-execution-block Map;
  clear it in afterEach so no entry leaks between tests.
- P3: collapse the two long narrative comment blocks in the catch to concise
  lines.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

All reported issues were addressed across 23 files (changes from recent commits).

Tip: Review your code locally with the cubic CLI to iterate faster.

Fix all with cubic | Re-trigger cubic

…ock types

Addresses cubic re-review + the failing check-test-types on PR #209:
- P2: the failure event mislabeled sessionKey (a routing key) as Sentry
  session_id. Carry the actual sessionId on the event and pass it to
  runContext; keep sessionKey as its own `session_key` extra.
- P3: add capture tests asserting session_id correlation and its omission.
- check-test-types: the before_tool_call e2e mock objects gained
  runBeforeToolCallHookFailed but the mock's type declaration did not — add it
  to both, resolving TS2353.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

All reported issues were addressed across 24 files (changes from recent commits).

Tip: Review your code locally with the cubic CLI to iterate faster.

Fix all with cubic | Re-trigger cubic

Address PR review: truncate the detail stored in the pending-call Map to
MAX_RECORDED_BLOCK_DETAIL_CHARS (1024) so up to 1024 unconsumed blocked
entries cannot retain unbounded memory. The full causeText still flows to
the log line and the Sentry event; only the map-held copy is capped.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

All reported issues were addressed across 24 files (changes from recent commits).

Tip: Review your code locally with the cubic CLI to iterate faster.

Fix all with cubic | Re-trigger cubic

Address PR review (cubic P2): the try in runBeforeToolCallHook also covers
trusted-policy / approval / skill-workshop pipeline work, so a throw from any
of those was mis-attributed to the before_tool_call_hook_failed Sentry signal.
Track whether the exception came from the handler invocation itself
(hookInvocationThrew, set in a dedicated inner try that rethrows) and gate the
observability emission on it. Non-handler pipeline throws are still caught,
blocked, logged, and recorded with detail (unchanged graceful degradation) —
they just no longer pollute the hook-failure bucket. The log line now names the
fault class (hook vs pipeline). Adds a regression test.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

All reported issues were addressed across 24 files (changes from recent commits).

Tip: Review your code locally with the cubic CLI to iterate faster.

Fix all with cubic | Re-trigger cubic

Comment thread docs/gateway/sentry-monitor.md Resolved
…bled

Address PR review (cubic P3): the capture table said 'always', but like every
sentry-monitor hook this subscription is gated by the hooks allow-list — if a
host sets an explicit hooks list omitting before_tool_call_hook_failed, the
monitor never registers it. Reword the 'Captured when' cell to state the hook
must be enabled. The prose 'always captured as an exception' is unchanged (it
correctly means no denied-style suppression once the hook fires).

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

All reported issues were addressed across 24 files (changes from recent commits).

Tip: Review your code locally with the cubic CLI to iterate faster.

Fix all with cubic | Re-trigger cubic

Address PR review (cubic P3): after the pipeline-classification fix, the map
value now stores detail for ANY thrown pre-execution failure (the
before_tool_call handler OR surrounding trusted-policy/approval/skill-workshop
processing), not just handler throws. Update the value comment and the
consumePreExecutionBlockedToolCall doc so future consumers don't discard
pipeline diagnostics. A plain policy veto still records no detail. Comment-only.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

All reported issues were addressed across 24 files (changes from recent commits).

Tip: Review your code locally with the cubic CLI to iterate faster.

Fix all with cubic | Re-trigger cubic

Comment thread scripts/plugin-sdk-surface-report.mjs Resolved
Comment thread src/auto-reply/reply-payload.ts Resolved
Address PR review (cubic P3 x3): update the sibling comments left stale by the
pipeline-classification change so they match the corrected state-map contract —
detail is present for ANY thrown pre-execution failure (before_tool_call handler
OR surrounding trusted-policy/approval/skill-workshop pipeline), never a veto.
Touches the AgentDecisionTrace toolInvocations/evidence detail docs
(reply-payload.ts) and the tool-meta inline comment (handlers.tools.ts), and
condenses the verbose hookInvocationThrew comment to the invariant + consequence.
Comment-only, no behavior change.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

All reported issues were addressed across 24 files (changes from recent commits).

Tip: Review your code locally with the cubic CLI to iterate faster.

Fix all with cubic | Re-trigger cubic

…on-failure

Address PR review (cubic P3) and preempt the rest: after the pipeline-
classification change the detail field carries ANY thrown pre-execution
failure (before_tool_call handler OR surrounding trusted-policy/approval/
skill-workshop pipeline), never a veto. Reword every remaining 'hook-failure'
contract comment across the projection chain (state cap, buildBlockedToolResult,
normalize-tool-metas, run.ts, embedded types, subscribe types, agent-decision-
trace) to the generic pre-execution-failure contract so the comments are
consistent and accurate. Comment-only, no behavior change.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

All reported issues were addressed across 24 files (changes from recent commits).

Tip: Review your code locally with the cubic CLI to iterate faster.

Fix all with cubic | Re-trigger cubic

@william-antoline-boon
william-antoline-boon merged commit fecd938 into boon Sep 8, 2026
153 of 158 checks passed
@william-antoline-boon
william-antoline-boon deleted the william-antoline-boon/eng-19492-hook-failure-visibility branch September 8, 2026 18:03
william-antoline-boon added a commit that referenced this pull request Sep 9, 2026
…211)

## Release 2026.6.11-boon.39

Surfaces a swallowed before_tool_call hook failure to Sentry + the durable
audit trace so the ENG-19492 incident class is diagnosable after the fact.

### Contents (merged onto `boon` since boon.38)
- **#209 (ENG-19492)** — emit a `before_tool_call_hook_failed` observability
  hook (genuine handler exception only, never a policy veto), paged by
  sentry-monitor, and thread the real error text into boon-core's audit_trace
  as an optional `detail` field. Requires boon-core companion getboon/boon#14629
  (merged + live) which accepts the new field. Non-hook pipeline failures are
  classified separately.
- **#208** — derive legacy tool counts from surfaced failures.

### Release prep
- Root version boon.38 -> boon.39; lockstep regenerated (211 files:
  135 plugin package.json + 70 npm-shrinkwrap.json + SDK/config baselines).
  All release-preflight --check steps pass.
- CHANGELOG.md `## 2026.6.11-boon.39` section added.

### After merge
Tag `v2026.6.11-boon.39` -> `boon-release.yml`. Fleet is pinned to boon.36, so
a roll also picks up boon.37 (#194-#198) and boon.38 (#201, #202, #205).


Claude-Session: https://claude.ai/code/session_01AMjtZJAWLVBwa5Dtc4oNog

Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
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.

3 participants