fix: add centralized appendToOutput helper for null-safe output writes in after-hooks #1762
+124
−17
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
appendToOutput()helper to Prevent silent "undefinedstring" data corruption when MCP tools return undefined as output.output, complementing the TypeError crash fix in fix: guard output.output in tool after-hooks for MCP tools #1756 when MCP tools returnundefinedasoutput.outputoutput.output +=) across 13 hook filesChanges
Problem
#1756 fixed READ operations (
.toLowerCase()) onoutput.outputwith?? ""guards in 3 files, but WRITE operations (output.output += text) remained unguarded across 13 hook files. When MCP tools (Atlassian, Exa, Chrome DevTools, grep.app, etc.) returnundefinedinstead of a string foroutput.output, these writes cause:TypeError: undefined is not an object— crashes the entire tool execution pipeline"undefinedSome reminder text"— silent data corruption when JS coercesundefined + stringSolution
New helper (
src/hooks/hook-output-guard.ts):Design decisions:
== null(loose equality) catches bothnullandundefinedin one check13 hook files updated — all
output.output += ...andoutput.output = `${output.output}...`patterns replaced withappendToOutput()calls:agent-usage-reminder/hook.ts+= REMINDER_MESSAGE→appendToOutputcategory-skill-reminder/hook.ts+= reminderMessage→appendToOutputclaude-code-hooks/.../tool-execute-after-handler.tsappendToOutput(2 sites)comment-checker/cli-runner.ts+= result.message→appendToOutput(2 sites)context-window-monitor.ts+= CONTEXT_REMINDER→appendToOutputdelegate-task-retry/hook.ts+= guidance→appendToOutput+ READ guard?? ""directory-agents-injector/injector.ts+= directory context→appendToOutputdirectory-readme-injector/injector.ts+= README content→appendToOutputedit-error-recovery/hook.ts+= EDIT_ERROR_REMINDER→appendToOutputinteractive-bash-session/hook.ts+= reminder→appendToOutputinteractive-bash-session/interactive-bash-session-hook.ts+= reminderToAppend→appendToOutputrules-injector/injector.ts+= rule content→appendToOutputtask-reminder/hook.ts+= REMINDER_MESSAGE→appendToOutputTesting
Test cases cover:
undefined→ initializes with text (core MCP bug scenario)null→ initializes with textundefinedRelated Issues
Complements #1756 (
d5fd918) — that PR guards READ operations, this PR guards WRITE operations.Related to #1720 (original MCP tool output crash report).
Summary by cubic
Adds a centralized appendToOutput helper to make after-hook output writes null-safe, preventing crashes and "undefinedstring" corruption when MCP tools return undefined. Replaces ad-hoc string concatenation across hooks and adds tests.
Written for commit 4b21310. Summary will update on new commits.