Skip to content

SecurityValidator hook fires PreToolUse:Read error: missing 'yaml' package in ~/.pai/ #156

@virtualian

Description

@virtualian

Symptom

Every Read tool call in a Claude Code session emits this non-blocking warning:

PreToolUse:Read hook error
Failed with non-blocking status code: error:
Cannot find package 'yaml' from '/Users/ianmarr/.pai/hooks/SecurityValidator.hook.ts'

Status is non-blocking, so the Read still succeeds — but the warning fires on every Read across the entire session, polluting output and obscuring real errors.

Root cause

SecurityValidator.hook.ts imports the third-party yaml package:

// ~/.pai/hooks/SecurityValidator.hook.ts:66
import { parse as parseYaml } from 'yaml';

But there is no package.json or node_modules resolvable from ~/.pai/hooks/ upward:

$ ls /Users/ianmarr/.pai/package.json /Users/ianmarr/.pai/node_modules
ls: /Users/ianmarr/.pai/node_modules: No such file or directory
ls: /Users/ianmarr/.pai/package.json: No such file or directory

$ ls /Users/ianmarr/.pai/hooks/package.json /Users/ianmarr/.pai/hooks/node_modules
ls: /Users/ianmarr/.pai/hooks/node_modules: No such file or directory
ls: /Users/ianmarr/.pai/hooks/package.json: No such file or directory

Bun resolves npm imports starting from the source file's directory and walking up. With no node_modules containing yaml anywhere on that path, the hook crashes during import.

Scope

Likely affects more than just SecurityValidator.hook.ts. The same pattern exists in Releases/v4.0.3+/.claude/hooks/SecurityValidator.hook.ts:66, so this is not runtime-only drift — fresh installs of v4.0.3+ should hit the same issue unless the install path arranges yaml to be resolvable. Worth auditing every hook in ~/.pai/hooks/*.ts for third-party imports (17 hooks present) to scope the full failure surface.

Reproduction

  1. Have PAI v4.0.3+ installed with hooks at ~/.pai/hooks/.
  2. Start any Claude Code session.
  3. Trigger any tool that fires PreToolUse:Read (e.g. read any file).
  4. Observe Cannot find package 'yaml' from '/Users/ianmarr/.pai/hooks/SecurityValidator.hook.ts' warning.

Suggested directions (not committing to one)

  • (a) Add package.json + bun-install at runtime root. Place a minimal ~/.pai/package.json declaring yaml (and any other hook deps), run bun install during install, and ensure ~/.pai/node_modules is populated. Most direct fix.
  • (b) Resolve from ~/.claude/. If ~/.claude/ already has node_modules with yaml, configure Bun resolution roots so ~/.pai/hooks/*.ts can import from ~/.claude/node_modules. Avoids duplicating deps.
  • (c) Bundle deps into the hook. bun build --bundle the hook so all imports are inlined. No runtime resolution needed but loses easy patching.
  • (d) Eliminate the dep. yaml is only used to parse patterns.yaml (security pattern definitions). Could swap for a JSON file or hand-written parser if the format is small enough. Reduces dependency surface.

Investigation context

  • Hook mtime: 7 Apr 20:31 — predates current PAI work; not introduced by recent PRs.
  • Same import { parse as parseYaml } from 'yaml'; line is present in repo at Releases/v4.0.3+/.claude/hooks/SecurityValidator.hook.ts:66 — so the fix needs to land in the shipped artefact, not just the runtime.
  • Discovered while applying Implement PAI user-choice Q&A contract (closes #148) #154 to live runtime; unrelated to that work, but visible because every Read in the session triggered the warning.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions