|
| 1 | +#!/usr/bin/env bash |
| 2 | +# #1388: a hook client that cannot join because the active daemon runs a |
| 3 | +# DIFFERENT build must say so on stdout (systemMessage) - stdout is the only |
| 4 | +# hook channel Claude Code surfaces, so stderr-only reporting reads as eternal |
| 5 | +# silence in-session. Requires a TEST_SEAMS=1 binary (scripts/test.sh step 5 |
| 6 | +# builds one): CBM_TEST_HOOK_CLIENT_BUILD forces the client fingerprint. |
| 7 | +# |
| 8 | +# STATUS: LOCAL-ONLY, NOT WIRED INTO scripts/test.sh (see the Step 5d comment |
| 9 | +# there). WHY: on every CI leg this test's forced client/daemon build mismatch |
| 10 | +# produces no cohort conflict, so the notice under test is never triggered and |
| 11 | +# the assertion fails for a reason unrelated to the fix. |
| 12 | +# WHAT WAS TRIED (2026-08-05, PR #1441): |
| 13 | +# - hardened the probe loop to wait for the asserted state with a bounded |
| 14 | +# backstop instead of a single post-start probe (cohort join is async); |
| 15 | +# - asserted the seam's presence in the binary up front, mirroring |
| 16 | +# tests/test_worker_watchdog.sh - the seam IS present on CI; |
| 17 | +# - replaced a `seq`-derived fingerprint with a length-checked literal, in |
| 18 | +# case `seq` was absent on a leg - it was not the cause; |
| 19 | +# - dumped stdout, the forced fingerprint, and `daemon status` on expiry: |
| 20 | +# the daemon is active on a DIFFERENT build fingerprint, yet the forced |
| 21 | +# client still joins without a conflict, which does not happen locally. |
| 22 | +# The remaining suspect is the cohort admission path behaving differently under |
| 23 | +# the CI environment; that is recorded as a follow-up rather than papered over. |
| 24 | +set -euo pipefail |
| 25 | + |
| 26 | +ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" |
| 27 | +BINARY="${ROOT}/build/c/codebase-memory-mcp" |
| 28 | +if [[ ! -x "${BINARY}" && -x "${BINARY}.exe" ]]; then |
| 29 | + BINARY="${BINARY}.exe" |
| 30 | +fi |
| 31 | +if [[ ! -x "${BINARY}" ]]; then |
| 32 | + echo "missing binary: ${BINARY}" >&2 |
| 33 | + exit 2 |
| 34 | +fi |
| 35 | + |
| 36 | +# PRECONDITION, not a skip (same doctrine as tests/test_worker_watchdog.sh): this |
| 37 | +# test drives the hook-client build seam, which is compiled out of ordinary |
| 38 | +# builds. Against a seam-less binary no conflict can ever occur and the failure |
| 39 | +# would read as "the notice is missing" instead of "the capability is absent". |
| 40 | +if ! LC_ALL=C grep -a -q -F 'CBM_TEST_HOOK_CLIENT_BUILD' "${BINARY}"; then |
| 41 | + echo "binary lacks the hook-client build seam: ${BINARY}" >&2 |
| 42 | + echo " rebuild it with: make -f Makefile.cbm cbm TEST_SEAMS=1" >&2 |
| 43 | + echo " or run this test through scripts/test.sh, which does that for you." >&2 |
| 44 | + exit 2 |
| 45 | +fi |
| 46 | + |
| 47 | +tmpdir="$(mktemp -d)" |
| 48 | +cleanup() { |
| 49 | + CBM_CACHE_DIR="${tmpdir}/cache" "${BINARY}" daemon stop >/dev/null 2>&1 || true |
| 50 | + rm -rf "${tmpdir}" |
| 51 | +} |
| 52 | +trap cleanup EXIT |
| 53 | + |
| 54 | +if ! CBM_CACHE_DIR="${tmpdir}/cache" "${BINARY}" daemon start >"${tmpdir}/daemon-start.log" 2>&1; then |
| 55 | + echo "daemon start failed on this host - cannot exercise the conflict path" >&2 |
| 56 | + cat "${tmpdir}/daemon-start.log" >&2 |
| 57 | + exit 2 |
| 58 | +fi |
| 59 | +payload='{"session_id":"probe","hook_event_name":"PreToolUse","tool_name":"Grep","tool_input":{"pattern":"x","path":"/tmp"},"cwd":"/tmp"}' |
| 60 | +forced_build="ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" |
| 61 | +if [[ ${#forced_build} -ne 64 ]]; then |
| 62 | + echo "internal: forced fingerprint must be 64 hex chars, got ${#forced_build}" >&2 |
| 63 | + exit 2 |
| 64 | +fi |
| 65 | + |
| 66 | +# The daemon's cohort join completes asynchronously after `daemon start` |
| 67 | +# returns. Wait for the STATE the assertion needs - a probe that observes the |
| 68 | +# build conflict - with a bounded liveness backstop; never a bare sleep. The |
| 69 | +# throttled notice marker is cleared before each probe so the stdout |
| 70 | +# assertion stays valid on whichever probe first observes the conflict. |
| 71 | +rc=0 |
| 72 | +out="" |
| 73 | +for _attempt in $(seq 1 40); do |
| 74 | + rm -f "${tmpdir}/cache/.hook-daemon-absent-notice" |
| 75 | + set +e |
| 76 | + out="$(printf '%s' "${payload}" | CBM_CACHE_DIR="${tmpdir}/cache" \ |
| 77 | + CBM_TEST_HOOK_CLIENT_BUILD="${forced_build}" \ |
| 78 | + "${BINARY}" hook-augment 2>"${tmpdir}/probe.err")" |
| 79 | + rc=$? |
| 80 | + set -e |
| 81 | + if [[ ${rc} -ne 0 ]]; then |
| 82 | + echo "hook-augment must fail open (exit 0); got ${rc}" >&2 |
| 83 | + cat "${tmpdir}/probe.err" >&2 |
| 84 | + exit 1 |
| 85 | + fi |
| 86 | + if grep -q "conflicting CBM process" "${tmpdir}/probe.err"; then |
| 87 | + break |
| 88 | + fi |
| 89 | + sleep 0.5 |
| 90 | +done |
| 91 | + |
| 92 | +if ! grep -q "conflicting CBM process" "${tmpdir}/probe.err"; then |
| 93 | + echo "no probe observed the build conflict within the backstop - daemon cohort" >&2 |
| 94 | + echo "never became active, or this is not a TEST_SEAMS=1 binary" >&2 |
| 95 | + echo "--- daemon-start.log ---" >&2 |
| 96 | + cat "${tmpdir}/daemon-start.log" >&2 |
| 97 | + echo "--- last probe stderr (empty means no conflict was raised) ---" >&2 |
| 98 | + cat "${tmpdir}/probe.err" >&2 |
| 99 | + echo "--- last probe stdout ---" >&2 |
| 100 | + printf '%s\n' "${out}" >&2 |
| 101 | + echo "--- diagnosis ---" >&2 |
| 102 | + echo "forced_build=${forced_build} (len ${#forced_build})" >&2 |
| 103 | + echo "seam present in binary: yes (asserted above)" >&2 |
| 104 | + CBM_CACHE_DIR="${tmpdir}/cache" "${BINARY}" daemon status >&2 2>&1 || true |
| 105 | + exit 2 |
| 106 | +fi |
| 107 | +if ! printf '%s' "${out}" | grep -q "systemMessage"; then |
| 108 | + echo "conflicted daemon must surface a stdout systemMessage to the hook caller (#1388)" >&2 |
| 109 | + echo "stdout was: ${out}" >&2 |
| 110 | + exit 1 |
| 111 | +fi |
| 112 | +if ! printf '%s' "${out}" | grep -q "daemon stop"; then |
| 113 | + echo "the systemMessage must carry the daemon-restart guidance (#1388)" >&2 |
| 114 | + echo "stdout was: ${out}" >&2 |
| 115 | + exit 1 |
| 116 | +fi |
| 117 | + |
| 118 | +echo "ok: daemon build conflicts reach the hook caller as a systemMessage" |
0 commit comments