@chrono-meta/fh-gate 3.20.1 → 3.22.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. package/.claude-plugin/marketplace.json +4 -4
  2. package/CATALOG.md +5 -0
  3. package/CLAUDE.md +67 -283
  4. package/knowledge/shared/dialogue/claude_code_runtime_flow.md +1 -0
  5. package/knowledge/shared/harness-core/checkout_layer_drift.md +28 -0
  6. package/knowledge/shared/harness-core/claude_md_gate_details.md +343 -0
  7. package/knowledge/shared/harness-core/dispatch_conditional_prohibition.md +89 -0
  8. package/knowledge/shared/harness-core/fh_detail_protocols.md +88 -0
  9. package/knowledge/shared/harness-core/fh_three_layer_canon.md +49 -0
  10. package/knowledge/shared/harness-core/field_verdict_crossfamily_gate.md +29 -0
  11. package/knowledge/shared/harness-core/hook_channel_visibility.md +80 -0
  12. package/knowledge/shared/harness-core/measurement-integrity-checklist.md +9 -0
  13. package/knowledge/shared/learnings/subagent_invocations_log.yaml +39 -0
  14. package/knowledge/shared/rules/operations.md +7 -0
  15. package/package.json +8 -1
  16. package/plugins/fh-commons/.claude-plugin/plugin.json +1 -1
  17. package/plugins/fh-meta/.claude-plugin/plugin.json +1 -1
  18. package/plugins/fh-meta/CHANGELOG.md +36 -0
  19. package/plugins/fh-meta/skills/goal-quench/SKILL.md +1 -1
  20. package/plugins/fh-preprep/.claude-plugin/plugin.json +1 -1
  21. package/plugins/fh-qp/.claude-plugin/plugin.json +1 -1
  22. package/scripts/fh_node_check.sh +4 -1
  23. package/scripts/fh_session_load.sh +55 -0
  24. package/scripts/hook_drift_check.sh +188 -0
  25. package/scripts/selfcheck.sh +71 -0
  26. package/scripts/session_close_check.sh +44 -7
  27. package/scripts/session_config_fingerprint.sh +139 -0
  28. package/scripts/sidecar_calibrate.sh +168 -13
  29. package/scripts/test_claude_md_size_lanes.sh +74 -0
  30. package/scripts/test_fh_audit_check_lanes.sh +140 -0
  31. package/scripts/test_hook_drift_lanes.sh +143 -0
  32. package/scripts/test_ollama_panel_lanes.sh +69 -10
  33. package/scripts/test_session_config_fingerprint_lanes.sh +116 -0
  34. package/scripts/test_session_load_mirror_fp_lanes.sh +122 -0
  35. package/scripts/test_subagent_tally_hook_lanes.sh +11 -7
  36. package/templates/.claude/rules/mcp_tool_gating.md +17 -1
@@ -0,0 +1,188 @@
1
+ #!/usr/bin/env bash
2
+ # hook_drift_check.sh — are the hooks INSTALLED in .claude/settings.json the ones the tracked
3
+ # templates currently ship? Per template hook entry: CURRENT / STALE / ABSENT / UNKNOWN.
4
+ #
5
+ # WHY (2026-09-25/26, measured on this repo): session_close_check.sh ④-e used to decide «the tally
6
+ # hook is installed» with `grep -q '"SubagentStop"'`. That is a PRESENCE test. The template's body
7
+ # had just moved into scripts/subagent_tally_hook.sh (it now skips Claude Code's internal agents),
8
+ # and templates/subagent-tally-hook.json says «RE-MERGE — an existing inline hook keeps the old
9
+ # behaviour». An install still carrying the OLD inline hook therefore passed ④-e as «measured»
10
+ # while it was over-counting. `.claude/settings.json` is gitignored, so git never tells you the
11
+ # installed copy has drifted from the tracked source; nothing else in the repo compared them.
12
+ #
13
+ # The sibling instrument, env_layer_fingerprint.sh, answers «is a layer PRESENT»; this one answers
14
+ # «is the present thing the CURRENT thing». They are different questions — a present-but-stale hook
15
+ # is exactly what a presence check renders as green.
16
+ #
17
+ # VERDICTS (per template entry, keyed by event):
18
+ # CURRENT an installed hook in that event has the same matcher AND the same command string
19
+ # STALE an installed hook in that event is the SAME hook (shares an identity token — a
20
+ # scripts/<name>.sh|py it runs, or a tracks/_meta/.<file> it writes) but its command or
21
+ # matcher differs from the template → re-merge needed
22
+ # ABSENT no installed hook in that event is related (includes: settings file does not exist)
23
+ # UNKNOWN the settings file or the template could not be read/parsed, or python3 is missing.
24
+ # 🟥 UNKNOWN is never folded into CURRENT — a checker that cannot read is not a pass.
25
+ #
26
+ # SCOPE: compares `project_settings_json` (and a template's top-level `hooks`) against
27
+ # .claude/settings.json. The SessionStart snippet's `settings_local_json_MODE_D_ONLY` block carries
28
+ # an operator-private path by design and is NOT compared (reported as a note, not as a verdict).
29
+ # READ-ONLY: never writes to any settings file.
30
+ #
31
+ # Usage:
32
+ # bash scripts/hook_drift_check.sh [--settings FILE] [--templates DIR] [--only TOKEN] [--verdict]
33
+ # --only TOKEN restrict to template entries whose identity contains TOKEN
34
+ # (e.g. --only subagent_tally_hook.sh — what ④-e asks)
35
+ # --verdict print ONE aggregate word instead of the table
36
+ # (priority UNKNOWN > STALE > ABSENT > CURRENT)
37
+ # Exit: 0 all CURRENT · 1 some STALE/ABSENT · 3 UNKNOWN · 2 usage error
38
+ set -uo pipefail
39
+
40
+ HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
41
+ ROOT="$(cd "$HERE/.." && pwd)"
42
+ SETTINGS="$ROOT/.claude/settings.json"
43
+ TEMPLATES="$ROOT/templates"
44
+ ONLY=""
45
+ VERDICT_ONLY=0
46
+
47
+ while [ $# -gt 0 ]; do
48
+ case "$1" in
49
+ --settings) [ -n "${2:-}" ] || { echo "usage: --settings needs a non-empty path" >&2; exit 2; }; SETTINGS="$2"; shift 2 ;;
50
+ --templates) [ -n "${2:-}" ] || { echo "usage: --templates needs a non-empty path" >&2; exit 2; }; TEMPLATES="$2"; shift 2 ;;
51
+ --only) [ -n "${2:-}" ] || { echo "usage: --only needs a non-empty token" >&2; exit 2; }; ONLY="$2"; shift 2 ;;
52
+ --verdict) VERDICT_ONLY=1; shift ;;
53
+ -h|--help) sed -n '2,40p' "$0"; exit 0 ;;
54
+ *) echo "usage: unknown argument: $1" >&2; exit 2 ;;
55
+ esac
56
+ done
57
+
58
+ PY="$(command -v python3 2>/dev/null || true)"
59
+ [ -n "$PY" ] || PY=/usr/bin/python3
60
+ if [ ! -x "$PY" ]; then
61
+ if [ "$VERDICT_ONLY" -eq 1 ]; then echo UNKNOWN; else echo "UNKNOWN * * python3 not found — cannot compare (not a pass)"; fi
62
+ exit 3
63
+ fi
64
+
65
+ "$PY" - "$SETTINGS" "$TEMPLATES" "$ONLY" "$VERDICT_ONLY" <<'PY'
66
+ import glob, json, os, re, sys
67
+
68
+ settings_path, tdir, only, verdict_only = sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4] == "1"
69
+ ID_RE = re.compile(r"scripts/([A-Za-z0-9_.-]+\.(?:sh|py))|tracks/_meta/(\.[A-Za-z0-9_.-]+)")
70
+
71
+ def ident(cmd):
72
+ return {a or b for a, b in ID_RE.findall(cmd or "")}
73
+
74
+ def flatten(hooks, bad=None):
75
+ # bad: optional list collecting events whose hook entries are malformed (no/empty command).
76
+ # 🟥 2026-09-26 (cross-family): such an entry used to be read as command "" → never related →
77
+ # ABSENT. An entry we cannot identify is not evidence of absence; the caller renders UNKNOWN.
78
+ out = [] # (event, matcher, command)
79
+ if not isinstance(hooks, dict):
80
+ return None
81
+ for ev, groups in hooks.items():
82
+ if not isinstance(groups, list):
83
+ return None
84
+ for g in groups:
85
+ if not isinstance(g, dict):
86
+ return None
87
+ m = g.get("matcher", "")
88
+ for h in g.get("hooks", []) or []:
89
+ if isinstance(h, dict) and h.get("type", "command") == "command":
90
+ cmd = h.get("command")
91
+ if not isinstance(cmd, str) or not cmd.strip():
92
+ if bad is not None:
93
+ bad.append(ev)
94
+ continue
95
+ out.append((ev, m or "", cmd))
96
+ elif not isinstance(h, dict) and bad is not None:
97
+ bad.append(ev)
98
+ return out
99
+
100
+ rows, notes = [], []
101
+
102
+ # ── templates ──
103
+ tfiles = sorted(set(glob.glob(os.path.join(tdir, "*hook*.json")) +
104
+ glob.glob(os.path.join(tdir, "settings.*.snippet.json"))))
105
+ tentries = [] # (event, matcher, command, template-name)
106
+ if not tfiles:
107
+ rows.append(("UNKNOWN", "*", "*", "no templates found under %s" % tdir))
108
+ for tf in tfiles:
109
+ name = os.path.basename(tf)
110
+ try:
111
+ with open(tf, encoding="utf-8") as fh:
112
+ d = json.load(fh)
113
+ except Exception as e:
114
+ rows.append(("UNKNOWN", "*", "*", "%s unreadable: %s" % (name, e.__class__.__name__)))
115
+ continue
116
+ if "settings_local_json_MODE_D_ONLY" in d:
117
+ notes.append("note: %s settings_local_json_MODE_D_ONLY not compared (private path by design)" % name)
118
+ block = d.get("project_settings_json", d)
119
+ fl = flatten(block.get("hooks", {}) if isinstance(block, dict) else None)
120
+ if fl is None:
121
+ rows.append(("UNKNOWN", "*", "*", "%s: hooks block malformed" % name))
122
+ continue
123
+ for ev, m, c in fl:
124
+ tentries.append((ev, m, c, name))
125
+
126
+ # ── installed ──
127
+ installed, inst_err, inst_bad = None, None, []
128
+ if not os.path.exists(settings_path):
129
+ installed = [] # no file = no hooks installed = ABSENT (not a read failure)
130
+ else:
131
+ try:
132
+ with open(settings_path, encoding="utf-8") as fh:
133
+ sd = json.load(fh)
134
+ installed = flatten(sd.get("hooks", {}) if isinstance(sd, dict) else None, inst_bad)
135
+ if installed is None:
136
+ inst_err = "hooks block malformed"
137
+ except Exception as e:
138
+ inst_err = "unreadable: %s" % e.__class__.__name__
139
+
140
+ for ev, m, c, name in tentries:
141
+ tid = ident(c)
142
+ label = ",".join(sorted(tid)) or "(inline)"
143
+ if only and not any(only in t for t in tid):
144
+ continue
145
+ if inst_err:
146
+ rows.append(("UNKNOWN", ev, label, "%s — settings %s" % (name, inst_err)))
147
+ continue
148
+ if ev in inst_bad:
149
+ rows.append(("UNKNOWN", ev, label, "%s — settings has a malformed %s entry (형식 불량 항목: no command)" % (name, ev)))
150
+ continue
151
+ same_ev = [(im, ic) for (iev, im, ic) in installed if iev == ev]
152
+ related = [(im, ic) for im, ic in same_ev if tid and (ident(ic) & tid)]
153
+ exact = [(im, ic) for im, ic in same_ev if im == m and ic == c]
154
+ # 🟥 2026-09-26 (cross-family, real fail-open): `any(exact)` called this CURRENT even when the
155
+ # OLD inline form was installed alongside the template form. Claude Code runs every matching
156
+ # hook, so the old behaviour stayed live. CURRENT now means: the same hook is installed
157
+ # EXACTLY ONCE, and that one copy is the template's.
158
+ if exact and len(related) <= 1 and len(exact) == 1:
159
+ rows.append(("CURRENT", ev, label, name))
160
+ continue
161
+ if exact:
162
+ rows.append(("STALE", ev, label, "%s — duplicate install (%d copies of this hook); 중복 설치 — remove the old entries" % (name, max(len(related), len(exact)))))
163
+ continue
164
+ if related:
165
+ why = "matcher differs" if any(ic == c for _, ic in related) else "command differs"
166
+ rows.append(("STALE", ev, label, "%s — %s; re-merge needed" % (name, why)))
167
+ else:
168
+ rows.append(("ABSENT", ev, label, name))
169
+
170
+ if only and not any(r[2] != "*" for r in rows):
171
+ rows.append(("UNKNOWN", "*", only, "no template entry carries this identity"))
172
+
173
+ order = {"UNKNOWN": 3, "STALE": 2, "ABSENT": 1, "CURRENT": 0}
174
+ worst = max((order[r[0]] for r in rows), default=3)
175
+ word = [k for k, v in order.items() if v == worst][0]
176
+
177
+ if verdict_only:
178
+ print(word)
179
+ else:
180
+ for r in rows:
181
+ print("\t".join(r))
182
+ for n in notes:
183
+ print(n)
184
+ cnt = {k: sum(1 for r in rows if r[0] == k) for k in order}
185
+ print("hook-drift: current=%d stale=%d absent=%d unknown=%d (settings=%s)"
186
+ % (cnt["CURRENT"], cnt["STALE"], cnt["ABSENT"], cnt["UNKNOWN"], settings_path))
187
+ sys.exit({"CURRENT": 0, "ABSENT": 1, "STALE": 1, "UNKNOWN": 3}[word])
188
+ PY
@@ -451,6 +451,47 @@ else
451
451
  fail=1
452
452
  fi
453
453
 
454
+ # session-load mirror FP — fh_session_load.sh listed sync-restamped mirror copies (byte-identical to
455
+ # the hub canonical) as «NEWER THAN SESSION CARD». Lane = FP stays quiet + every not-provably-same
456
+ # case (genuine · differs · no hub copy · lookalike banner · outside $TM) still listed.
457
+ if [ ! -f scripts/fh_session_load.sh ]; then
458
+ _absent_subject_verdict "session-load mirror FP lanes" "scripts/fh_session_load.sh" || fail=1
459
+ elif [ -f scripts/test_session_load_mirror_fp_lanes.sh ]; then
460
+ if ! bash scripts/test_session_load_mirror_fp_lanes.sh; then
461
+ fail=1
462
+ fi
463
+ else
464
+ echo "FAIL session-load mirror FP lanes: fh_session_load.sh present but its anchor is missing"
465
+ fail=1
466
+ fi
467
+
468
+ # CLAUDE.md size guard — Claude Code warns «CLAUDE.md is over the 150.0k-char limit» on every session
469
+ # start of a clone (measured 2026-09-26: 154,149 chars since ~v3.16.0). Guard at 145k (headroom ~1k/week).
470
+ if [ ! -f CLAUDE.md ]; then
471
+ _absent_subject_verdict "claude-md size lanes" "CLAUDE.md" || fail=1
472
+ elif [ -f scripts/test_claude_md_size_lanes.sh ]; then
473
+ if ! bash scripts/test_claude_md_size_lanes.sh; then
474
+ fail=1
475
+ fi
476
+ else
477
+ echo "FAIL claude-md size lanes: CLAUDE.md present but its anchor is missing"
478
+ fail=1
479
+ fi
480
+
481
+ # terminal audit banner — templates/fh_audit_check.zsh (sourced from every user's ~/.zshrc by install-wizard)
482
+ # fired 2 false lines forever (harvest glob nothing writes · state-marker sentinels aged as audits) and
483
+ # aborted whole on zsh NOMATCH for a fresh install. Lane = false lines gone · real overdue audits still warn.
484
+ if [ ! -f templates/fh_audit_check.zsh ]; then
485
+ _absent_subject_verdict "audit banner lanes" "templates/fh_audit_check.zsh" || fail=1
486
+ elif [ -f scripts/test_fh_audit_check_lanes.sh ]; then
487
+ if ! bash scripts/test_fh_audit_check_lanes.sh; then
488
+ fail=1
489
+ fi
490
+ else
491
+ echo "FAIL audit banner lanes: fh_audit_check.zsh present but its anchor is missing"
492
+ fail=1
493
+ fi
494
+
454
495
  # typed-finding pipeline — fleet (multi-family, parallel) + reject stage (cross-family verdict, code
455
496
  # drops false positives). Subject = scripts/finding_fleet.sh + scripts/finding_verify.py.
456
497
  if [ ! -f scripts/finding_verify.py ]; then
@@ -2225,6 +2266,36 @@ if [ -f scripts/test_dispatch_log_lanes.sh ]; then
2225
2266
  fail=1
2226
2267
  fi
2227
2268
  fi
2269
+ # ④-e's «hook installed» verdict + the checker behind it (2026-09-26): a PRESENT-but-STALE tally
2270
+ # hook (the pre-2026-09-25 inline form) used to pass as measured. Revert probe inside the lane.
2271
+ if [ ! -f scripts/hook_drift_check.sh ]; then
2272
+ _absent_subject_verdict "hook-drift lanes" "scripts/hook_drift_check.sh" || fail=1
2273
+ elif [ -f scripts/test_hook_drift_lanes.sh ]; then
2274
+ if _out=$(bash scripts/test_hook_drift_lanes.sh 2>&1); then
2275
+ echo "PASS test_hook_drift_lanes.sh (CURRENT/STALE/ABSENT/UNKNOWN + ④-e consumer + revert probe)"
2276
+ else
2277
+ echo "FAIL test_hook_drift_lanes.sh: installed-hook drift would be misjudged"
2278
+ _show_failure "$_out"
2279
+ fail=1
2280
+ fi
2281
+ else
2282
+ echo "FAIL hook-drift lanes: hook_drift_check.sh present but its anchor is missing"; fail=1
2283
+ fi
2284
+ # ④-f session config fingerprint (2026-09-26): a mid-session overwrite of settings/CLAUDE.md went
2285
+ # unnoticed on 2026-09-25. Advisory at close; the lane pins «no snapshot = NOT MEASURED».
2286
+ if [ ! -f scripts/session_config_fingerprint.sh ]; then
2287
+ _absent_subject_verdict "config-fingerprint lanes" "scripts/session_config_fingerprint.sh" || fail=1
2288
+ elif [ -f scripts/test_session_config_fingerprint_lanes.sh ]; then
2289
+ if _out=$(bash scripts/test_session_config_fingerprint_lanes.sh 2>&1); then
2290
+ echo "PASS test_session_config_fingerprint_lanes.sh (snap/compare + ④-f consumer + revert probe)"
2291
+ else
2292
+ echo "FAIL test_session_config_fingerprint_lanes.sh: a config overwrite would go unnamed"
2293
+ _show_failure "$_out"
2294
+ fail=1
2295
+ fi
2296
+ else
2297
+ echo "FAIL config-fingerprint lanes: session_config_fingerprint.sh present but its anchor is missing"; fail=1
2298
+ fi
2228
2299
  # The tally hook's BODY (2026-09-25): SubagentStop also fires for Claude Code's internal agents
2229
2300
  # (compaction → agent_type ""), and the date-only inline hook counted them as dispatches.
2230
2301
  if [ -f scripts/test_subagent_tally_hook_lanes.sh ]; then
@@ -470,15 +470,34 @@ fi
470
470
  # from. Installable snippet: templates/subagent-tally-hook.json.
471
471
  TALLY="$FH/tracks/_meta/.subagent_dispatch_tally"
472
472
  LOG="$FH/knowledge/shared/learnings/subagent_invocations_log.yaml"
473
+ # 🟥 2026-09-26 — «installed» means CURRENT, not PRESENT. The old test was
474
+ # `grep -q '"SubagentStop"'`, which also passed an install still carrying the pre-2026-09-25
475
+ # INLINE hook (date-only append, counts Claude Code's internal agents as dispatches). The template
476
+ # itself says «RE-MERGE», and nothing compared the gitignored installed copy against it. So the
477
+ # verdict now comes from hook_drift_check.sh: CURRENT → measured · STALE → old hook, re-merge ·
478
+ # ABSENT → none · UNKNOWN (read failure / checker missing) → NOT MEASURED, never folded into OK.
479
+ # Lane: scripts/test_hook_drift_lanes.sh (incl. a revert probe back to the presence grep).
473
480
  HOOK_OK=0
474
- if [ -f "$FH/.claude/settings.json" ]; then
475
- grep -q '"SubagentStop"' "$FH/.claude/settings.json" 2>/dev/null && HOOK_OK=1
476
- fi
477
- if [ "$HOOK_OK" -eq 0 ]; then
478
- echo "⚠️ ④-e dispatch log NOT MEASURED — no SubagentStop tally hook in .claude/settings.json"
479
- echo " (that file is gitignored, so a fresh clone has none). An unmeasured dispatch count is"
480
- echo " NOT a count of zero. Install: templates/subagent-tally-hook.json → .claude/settings.json"
481
+ HOOK_STATE=UNKNOWN
482
+ if [ -f "$FH/scripts/hook_drift_check.sh" ]; then
483
+ HOOK_STATE=$(bash "$FH/scripts/hook_drift_check.sh" --settings "$FH/.claude/settings.json" \
484
+ --templates "$FH/templates" --only subagent_tally_hook.sh --verdict 2>/dev/null)
485
+ case "$HOOK_STATE" in (CURRENT|STALE|ABSENT|UNKNOWN) ;; (*) HOOK_STATE=UNKNOWN ;; esac
481
486
  fi
487
+ [ "$HOOK_STATE" = CURRENT ] && HOOK_OK=1
488
+ case "$HOOK_STATE" in
489
+ (STALE)
490
+ echo "⚠️ ④-e dispatch log NOT MEASURED — 옛 훅: the SubagentStop tally hook in .claude/settings.json"
491
+ echo " differs from templates/subagent-tally-hook.json (re-merge needed — an old inline hook"
492
+ echo " counts internal agents as dispatches). Re-merge that block, then re-run." ;;
493
+ (ABSENT)
494
+ echo "⚠️ ④-e dispatch log NOT MEASURED — no SubagentStop tally hook in .claude/settings.json"
495
+ echo " (that file is gitignored, so a fresh clone has none). An unmeasured dispatch count is"
496
+ echo " NOT a count of zero. Install: templates/subagent-tally-hook.json → .claude/settings.json" ;;
497
+ (UNKNOWN)
498
+ echo "⚠️ ④-e dispatch log NOT MEASURED — could not determine whether the tally hook is current"
499
+ echo " (settings unreadable, or scripts/hook_drift_check.sh missing). Not a pass." ;;
500
+ esac
482
501
  # `grep -c` PRINTS 0 and EXITS 1 when the count is zero. Under `set -o pipefail` (line 16) that
483
502
  # makes the pipeline fail, `|| echo 0` appends a SECOND line, and the value becomes "0\n0" — which
484
503
  # `[ -eq ]` rejects as a bash error, so the branch it guards is skipped. The guard was therefore
@@ -549,6 +568,24 @@ elif [ "$HOOK_OK" -eq 1 ]; then
549
568
  echo "✅ ④-e dispatch log: no sub-agent dispatches tallied today"
550
569
  fi
551
570
 
571
+ # ④-f CONFIG FINGERPRINT (advisory, never blocks) — 2026-09-26.
572
+ # A sub-agent's measurement run overwrote this checkout's .claude/settings.json and CLAUDE.md on 2026-09-26 and
573
+ # nothing noticed: settings is gitignored (no status line, no undo). fh_session_load.sh snaps
574
+ # sha256+mtime of six config files at SessionStart; this compares and NAMES what changed.
575
+ # Advisory because a session may change its own settings on purpose. No snapshot → NOT MEASURED
576
+ # (never «unchanged»). Lane: scripts/test_session_config_fingerprint_lanes.sh.
577
+ if [ -f "$FH/scripts/session_config_fingerprint.sh" ]; then
578
+ # Capture FIRST, then decorate — piping into sed hid the checker's rc (2026-09-26, cross-family).
579
+ _FP_OUT=$(bash "$FH/scripts/session_config_fingerprint.sh" compare --fh "$FH" 2>&1); _FP_RC=$?
580
+ printf '%s\n' "$_FP_OUT" | sed 's/^/④-f /'
581
+ case "$_FP_RC" in
582
+ (0|1|3) ;; # unchanged · changed (advisory) · NOT MEASURED — all already printed above
583
+ (*) echo "⚠️ ④-f config fingerprint NOT MEASURED — checker exited $_FP_RC (instrument error, not «unchanged»)" ;;
584
+ esac
585
+ else
586
+ echo "⚠️ ④-f config fingerprint NOT MEASURED — scripts/session_config_fingerprint.sh missing"
587
+ fi
588
+
552
589
  # ⑤ CARD-LAST invariant — the card must be the NEWEST close artifact. A card older than
553
590
  # fh_completed / signal files written this session = ⑤ ran before ①–④ finished (the bug class).
554
591
  if [ -f "$CARD" ]; then
@@ -0,0 +1,139 @@
1
+ #!/usr/bin/env bash
2
+ # session_config_fingerprint.sh — snapshot the session's config files at start, compare at close.
3
+ #
4
+ # WHY (2026-09-26, measured on this repo): a sub-agent's measurement run (empty work-dir path →
5
+ # `cd "" ` succeeded in place) overwrote this checkout's
6
+ # .claude/settings.json and CLAUDE.md mid-session, and FH did not notice. The settings file is
7
+ # gitignored (no `git status`, no `git checkout` undo), and the only existing guard —
8
+ # sim_isolated_run.sh — compares ~/.claude/settings.json mtime around ITS OWN runs, nothing else.
9
+ # So a silent config rewrite by anything else in the session had no detector at all.
10
+ #
11
+ # WHAT: `snap` records sha256 + mtime of six files (below) into a gitignored snapshot with the
12
+ # session id. `compare` recomputes and NAMES every file that changed. It is ADVISORY by design —
13
+ # a session may legitimately change its own settings — so it never blocks; it makes a change
14
+ # visible, and says where the recovery procedure lives.
15
+ #
16
+ # Files: <FH>/.claude/settings.json · <FH>/.claude/settings.local.json · <FH>/CLAUDE.local.md ·
17
+ # <FH>/CLAUDE.md (tracked, but an overwrite target on 2026-09-26) ·
18
+ # <HOME>/.claude/settings.json · <HOME>/.claude/CLAUDE.md
19
+ #
20
+ # 🟥 No snapshot → NOT MEASURED, never «no change». A compare against nothing is not a zero.
21
+ # READ-ONLY on every file it fingerprints; writes only its own snapshot file.
22
+ #
23
+ # Usage:
24
+ # bash scripts/session_config_fingerprint.sh snap [--fh DIR] [--home DIR] [--out FILE] [--source SRC]
25
+ # bash scripts/session_config_fingerprint.sh compare [--fh DIR] [--home DIR] [--out FILE]
26
+ # --source clear|compact keeps an existing snapshot of the SAME session (a /clear or compaction
27
+ # is the same session continuing — re-snapping there would erase the evidence).
28
+ # Exit (compare): 0 unchanged · 1 changed (advisory) · 3 NOT MEASURED · 2 usage
29
+ # Exit (snap): 0 written or kept · 3 could not write · 2 usage
30
+ set -uo pipefail
31
+
32
+ MODE="${1:-}"; shift 2>/dev/null || true
33
+ FH="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
34
+ H="${HOME:-}"
35
+ OUT=""
36
+ SRC="unknown"
37
+ while [ $# -gt 0 ]; do
38
+ case "$1" in
39
+ --fh) [ -n "${2:-}" ] || { echo "usage: --fh needs a path" >&2; exit 2; }; FH="$2"; shift 2 ;;
40
+ --home) [ -n "${2:-}" ] || { echo "usage: --home needs a path" >&2; exit 2; }; H="$2"; shift 2 ;;
41
+ --out) [ -n "${2:-}" ] || { echo "usage: --out needs a path" >&2; exit 2; }; OUT="$2"; shift 2 ;;
42
+ --source) SRC="${2:-unknown}"; shift 2 ;;
43
+ *) echo "usage: unknown argument: $1" >&2; exit 2 ;;
44
+ esac
45
+ done
46
+ [ -n "$OUT" ] || OUT="$FH/tracks/_meta/.session_config_fingerprint"
47
+ SID="${CLAUDE_CODE_SESSION_ID:-pid-${PPID:-$$}}"
48
+
49
+ _mtime() { stat -c %Y "$1" 2>/dev/null || stat -f %m "$1" 2>/dev/null || echo "?"; }
50
+ _hash() {
51
+ if command -v shasum >/dev/null 2>&1; then shasum -a 256 "$1" 2>/dev/null | awk '{print $1}'
52
+ elif command -v sha256sum >/dev/null 2>&1; then sha256sum "$1" 2>/dev/null | awk '{print $1}'
53
+ else cksum "$1" 2>/dev/null | awk '{print "cksum-"$1"-"$2}'; fi
54
+ }
55
+ # label<TAB>path — labels are what the report prints, so they never carry an absolute home path.
56
+ _targets() {
57
+ printf 'repo:.claude/settings.json\t%s\n' "$FH/.claude/settings.json"
58
+ printf 'repo:.claude/settings.local.json\t%s\n' "$FH/.claude/settings.local.json"
59
+ printf 'repo:CLAUDE.local.md\t%s\n' "$FH/CLAUDE.local.md"
60
+ printf 'repo:CLAUDE.md\t%s\n' "$FH/CLAUDE.md"
61
+ printf 'home:~/.claude/settings.json\t%s\n' "$H/.claude/settings.json"
62
+ printf 'home:~/.claude/CLAUDE.md\t%s\n' "$H/.claude/CLAUDE.md"
63
+ }
64
+ # one line per target: label<TAB>state<TAB>hash<TAB>mtime (state PRESENT|ABSENT|UNREADABLE)
65
+ _measure() {
66
+ _targets | while IFS="$(printf '\t')" read -r label path; do
67
+ if [ ! -e "$path" ]; then printf '%s\tABSENT\t-\t-\n' "$label"
68
+ elif [ ! -r "$path" ]; then printf '%s\tUNREADABLE\t-\t-\n' "$label"
69
+ else
70
+ h="$(_hash "$path")"
71
+ if [ -n "$h" ]; then printf '%s\tPRESENT\t%s\t%s\n' "$label" "$h" "$(_mtime "$path")"
72
+ else printf '%s\tUNREADABLE\t-\t-\n' "$label"; fi
73
+ fi
74
+ done
75
+ }
76
+
77
+ case "$MODE" in
78
+ snap)
79
+ if [ -f "$OUT" ]; then
80
+ prev_sid="$(sed -n '/^session=/{s/^session=//p;q;}' "$OUT" 2>/dev/null)"
81
+ case "$SRC" in
82
+ clear|compact) if [ "$prev_sid" = "$SID" ]; then exit 0; fi ;;
83
+ esac
84
+ fi
85
+ mkdir -p "$(dirname "$OUT")" 2>/dev/null
86
+ tmp="$OUT.tmp.$$"
87
+ { printf 'session=%s\nat=%s\nsource=%s\n' "$SID" "$(date +%s)" "$SRC"; _measure; } > "$tmp" 2>/dev/null \
88
+ && mv "$tmp" "$OUT" 2>/dev/null && exit 0
89
+ rm -f "$tmp" 2>/dev/null; exit 3 ;;
90
+ compare)
91
+ if [ ! -r "$OUT" ]; then
92
+ echo "⚠️ config fingerprint NOT MEASURED — no session-start snapshot ($(basename "$OUT"))."
93
+ echo " No snapshot is not «no change». The snap runs from scripts/fh_session_load.sh at SessionStart."
94
+ exit 3
95
+ fi
96
+ snap_sid="$(sed -n '/^session=/{s/^session=//p;q;}' "$OUT")"
97
+ now="$(_measure)"
98
+ changed=""; n=0; touched=""
99
+ while IFS="$(printf '\t')" read -r label state hash mt; do
100
+ [ -n "$label" ] || continue
101
+ old="$(awk -F'\t' -v l="$label" '$1==l {print $2"\t"$3"\t"$4; exit}' "$OUT")"
102
+ if [ -z "$old" ]; then changed="$changed
103
+ ↳ $label — not in snapshot (NOT MEASURED)"; n=$((n+1)); continue; fi
104
+ old_state="${old%% *}"; _rest="${old#* }"; old_hash="${_rest%% *}"; old_mt="${_rest#* }"
105
+ if [ "$state" = UNREADABLE ] || [ "$old_state" = UNREADABLE ]; then
106
+ changed="$changed
107
+ ↳ $label — unreadable now or at start (NOT MEASURED)"; n=$((n+1))
108
+ elif [ "$state" != "$old_state" ]; then
109
+ changed="$changed
110
+ ↳ $label — $old_state → $state"; n=$((n+1))
111
+ elif [ "$hash" != "$old_hash" ]; then
112
+ changed="$changed
113
+ ↳ $label — content changed"; n=$((n+1))
114
+ elif [ "$state" = PRESENT ] && [ "$mt" != "$old_mt" ]; then
115
+ # 🟥 2026-09-26 (cross-family): mtime was snapped and never compared. Decision: the content
116
+ # hash is the verdict (a rewrite with identical bytes restores nothing and loses nothing),
117
+ # but a touch is still a sign something wrote there — so it is REPORTED separately and
118
+ # NOT counted as a change. Folding it into «changed» would cry wolf on every editor save;
119
+ # dropping it would hide the one trace a same-bytes rewrite leaves.
120
+ touched="$touched
121
+ ↳ $label — touched (내용 동일, mtime only)"
122
+ fi
123
+ done <<EOF_NOW
124
+ $now
125
+ EOF_NOW
126
+ note=""
127
+ [ "$snap_sid" = "$SID" ] || note=" (snapshot belongs to session $snap_sid, not this one — baseline may be older)"
128
+ if [ "$n" -eq 0 ]; then
129
+ echo "✅ config fingerprint: none of the 6 config files changed since session start$note"
130
+ [ -z "$touched" ] || echo " ⓘ written without content change (advisory, not counted):$touched"
131
+ exit 0
132
+ fi
133
+ echo "⚠️ config fingerprint: $n config file(s) changed since session start$note$changed"
134
+ [ -z "$touched" ] || echo " ⓘ written without content change (advisory, not counted):$touched"
135
+ echo " Intended change → ignore. Not yours → back it up now and recover per"
136
+ echo " knowledge/shared/harness-core/checkout_layer_drift.md §Config-Overwrite-Recovery"
137
+ exit 1 ;;
138
+ *) sed -n '2,30p' "$0"; exit 2 ;;
139
+ esac