#!/usr/bin/env bash
# edit-syntax-gate.sh  -  PostToolUse syntax check for the multi-agent pipeline.
#
# Runs after a file edit (Claude Code Edit / Write / MultiEdit, Codex
# apply_patch) and parses the edited file with the
# fastest parser its extension has, so a broken file is reported in the turn
# that broke it instead of at the next build. The parsing is in
# edit-syntax-gate.mjs; this wrapper exists because every hook the pipeline
# registers is a `bash "$HOME/.<host>/scripts/<name>"` command.
#
# Contract (PostToolUse hook on each host):
#   - Reads the tool-call JSON on stdin.
#   - On a parse error prints the context object the host reads back on
#     stdout ({"hookSpecificOutput": {"hookEventName": "PostToolUse",
#     "additionalContext": "..."}}), which the model reads as context for its
#     next step. Copilot CLI's payload is read too, but the pipeline does not
#     register the hook there: as of 1.0.45 Copilot discards its output.
#   - Exits 0 on every path: the edit has already happened, so blocking would
#     only hide the message.
#
# Inert unless `prefs.global.editSyntaxGate.enabled` is true. No node on PATH,
# a missing parser, a timeout or any internal error means no message.

set -u

HERE="$(cd "$(dirname "$0")" 2>/dev/null && pwd || true)"
CORE="$HERE/edit-syntax-gate.mjs"
[ -f "$CORE" ] || exit 0
command -v node >/dev/null 2>&1 || exit 0

node "$CORE" 2>/dev/null || true
exit 0
