Skip to content

fix(hooks): scrub errors before warning - #541

Merged
abogartz merged 1 commit into
abogartz:mainfrom
WhollyMindless:fix/485-safe-hook-warnings
Nov 26, 2026
Merged

abogartz merged 1 commit into
abogartz:mainfrom
WhollyMindless:fix/485-safe-hook-warnings

Conversation

@WhollyMindless

Copy link
Copy Markdown
Contributor

Closes #485.

guarded() now sends the shared redacted, bounded error message to the console instead of the raw error object. The regression test checks the warning with a synthetic credential and confirms the durable hook_error receipt still records only the error class.

The issue's console/receipt equality check assumes the receipt already records a message. In the current code it deliberately stores only the class because hook errors can carry conversation content. The console therefore uses the sanitized diagnostic while the receipt retains its existing class-only contract.

The regression failed before the fix. All 29 Windows hook and evidence-policy tests passed after the review follow-up. Typecheck, changed-file Biome checks, and comment lint passed. Claude Opus independently checked the failure, the existing receipt policy, and the import graph before publication.

@ocsi-reviewer

ocsi-reviewer Bot commented Nov 26, 2026

Copy link
Copy Markdown

Review: PR #541 — fix(hooks): scrub errors before warning (tier:micro, #485)

Full review seat (combined correctness + process; disclosed as such). Head f6f8710, merge-base 79573ac, reviewed from scratch archives of both revisions. Verdict: APPROVE.

Scope

git diff --numstat 79573ac f6f8710: exactly 2 files — src/core/hooks.ts (+5/−3) and test/capture/capture-hooks.test.ts (+17/−5). The PR body's "+8/−3 / +22/−5" reads like changed-lines-per-file rather than added/deleted; the file set itself matches the claim.

The two contracts

Console line (changed). guarded()'s catch now passes the error through safeErrorMessage (src/telemetry/record.ts:359) instead of handing the raw object to console.warn. The contract, from the function and its helpers (describeError, MIRRORED_ERROR_MAX = 200, redactSecrets in src/store/secrets.ts:79): message text only (or String(err) for non-Errors, unprintable <type> if String throws), credential-scrubbed (PEM private-key blocks, known sk-/gh[po]_/github_pat_/xox[bp]-/AKIA/AIza prefixes, digit-bearing Bearer …, and high-entropy values assigned to *_KEY/*_TOKEN/*_SECRET/*_PASSWORD/password fields), capped at 200 characters. No stack, no extra object fields.

Receipt (unchanged, deliberately class-only). hook_error is written only by the reporter wired at src/index.ts:129-141: err instanceof Error ? err.constructor?.name || "Error" : typeof err. That file is byte-identical base↔head (diff clean), so the receipt gained no message text. The PR's correction of the ticket's console/receipt-equality premise is sound: the receipt persists into the store where messages could carry conversation content (the code comment at src/index.ts:126 says exactly this), while the console line is the host log that already carried the raw error on base. The split is strictly conservative in both directions: the receipt stays narrower, the console strictly shrinks from raw-object to scrubbed+capped message. Note the console still carries message prose — redactSecrets is deliberately conservative and will not catch conversation content that looks like ordinary text — but that is the stated, shared egress contract, and a strict improvement over base.

Import cycle

src/core/hooks.ts now imports src/telemetry/record.ts. Static check: grep -rn "core/hooks" src (head) matches only src/events.ts:5 (type-only) and src/index.ts:12; none of record.ts's own import chain pulls in core/hooks.ts. No cycle exists, benign or otherwise. Dynamic check: a scratch bun script importing rev541/src/telemetry/record.ts, rev541/src/core/hooks.ts and rev541/src/index.ts in one graph printed CYCLE_IMPORT_OK with no throw. Scratch file deleted after.

Consumer blast radius

guarded() wraps 12 call sites (7 in events.ts, 1 in runtime.ts, the plugin_load heartbeat in index.ts), so every guarded warning's console shape changes. Consumers of that shape:

Consumer Status
test/capture/capture-hooks.test.ts:423 updated in this PR; passes on head, fails on base (intended)
test/telemetry/evidence-write-policy.test.ts:235 asserts the runtime.ts:1245 idle-loop line — a separate direct try/catch that already used safeErrorMessage and never routed through guarded(); file diff-identical base↔head; green in full suite
test/capture/capture-subscription-death.test.ts:105-126 stringifies warn args and filters on the warning name only — no raw-err assertion; identical base↔head; green in full suite
test/capture/capture-dispose-drain.test.ts:10 prose comment only, no assertion

The two loop guards the ticket touches on were verified as claimed: src/capture/runtime.ts:1243-1246 (capture loop) and :1277 (subscription) are standalone duplicates already sharing safeErrorMessage — for that loop the receipt field carries the same scrubbed text (error: failure), a pre-existing separate contract, not this PR's — and src/events.ts:403 routes through the same guarded(), not a differently-shaped duplicate. Both files byte-identical base↔head.

Scrub probe (scratch, deleted)

Called a guarded()-wrapped fn throwing Error("failed for API_TOKEN=aB3dE5fG7hI9jK1lM3nO5pQ7 sk-AbCdEfGhIjKlMnOpQrSt and token=abc123secret") plus a 400-char message, capturing warn args on both revisions.

Base (leak — the defect, full message via the raw object):

[opencode-self-improvement] probe-hook hook failed (ignored): | Error: failed for API_TOKEN=aB3dE5fG7hI9jK1lM3nO5pQ7 sk-AbCdEfGhIjKlMnOpQrSt and token=abc123secret
[opencode-self-improvement] probe-long hook failed (ignored): | Error: xxxx…(400 chars)

Head (scrubbed + capped):

[opencode-self-improvement] probe-hook hook failed (ignored): | failed for API_TOKEN=[redacted] [redacted] and token=abc123secret
[opencode-self-improvement] probe-long hook failed (ignored): | xxxx…(200 chars)

Redaction and the 200-char cap behave exactly as the stated contract. token=abc123secret surviving is contract-correct, not a miss: token alone is not a credential-named field and the 12-char value fails the high-entropy floor — same conservative policy the capture egress already ships.

Fail-before

Head test file against base src: 18 pass / 1 fail, and the single failure is precisely the new assertion at line 431 — received [TypeError: secret transcript fragment sk-…] where head expects "secret transcript fragment [redacted]". The other 18 tests, including both existing hook_error class-only checks, pass on both sides. Head file green 2/2 runs (19 pass each).

Full suite

  • Head: 1851 pass / 22 skip / 0 fail (1873 tests, 15006 expect calls)
  • Base: 1851 pass / 22 skip / 0 fail (1873 tests, 15005 expect calls)

Zero drift; the +1 expect call on head is the new warn assertion. CI green: lint, test, test-windows, smoke.

Prose

The touched test title — "a guarded failure lands in the evidence file as hook_error, class only" — is unchanged invariant wording; the added assertion tests the console contract inside it without incident narration anywhere in shipped titles or comments. Accept.

Verdict

APPROVE. Two-file scope, the console/receipt split is exactly what the code does, the receipt provably unchanged, no import cycle, no broken consumers, real fail-before, suite identical. The PR's deviation from the ticket's equality premise is the right call and is well-argued in the code's own comments.

@abogartz
abogartz merged commit afd26f2 into abogartz:main Nov 26, 2026
4 checks passed
@abogartz-opencode-self-improvement

Copy link
Copy Markdown

🎉 This PR is included in version 0.37.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

guarded()'s console warn still prints the raw Error; use safeErrorMessage like the capture path does

2 participants