@soleur/opencode 3.357.16 → 3.357.17

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@soleur/opencode",
3
- "version": "3.357.16",
3
+ "version": "3.357.17",
4
4
  "description": "Soleur AI organization plugin for OpenCode",
5
5
  "type": "module",
6
6
  "bin": {
@@ -29,8 +29,9 @@
29
29
  # Exit contract — produced ONLY by finish() in lib/affected-tests/common.sh,
30
30
  # the single exit site for the whole gate:
31
31
  # 0 every selected test passed, OR NO_TESTS_AFFECTED for an all-inert diff
32
- # 1 a selected test failed, or the delegated runner was signal-killed (rc 3
33
- # maps to 1 reason=runner-signal); a failure seen before budget expiry
32
+ # 1 a selected test failed, the delegated runner was signal-killed (rc 3
33
+ # maps to 1 reason=runner-signal), or the content-trigger command failed
34
+ # (reason=content-trigger-failed); a failure seen before budget expiry
34
35
  # wins over deferral
35
36
  # 4 AFFECTED_GATE_REFUSED — no-stack, bad-config key=<k>,
36
37
  # vacuous-test-command, no-base-ref, not-a-repo (--self-check outside a
@@ -71,6 +72,22 @@
71
72
  # must be a positive integer
72
73
  # max_workers number overrides the width formula; positive integer
73
74
  # budget_ci boolean forces CI-deferral eligibility without a workflow
75
+ # content_trigger_pattern / content_trigger_paths / content_trigger_command
76
+ # string / string[] / string ALL THREE or none (a partial or
77
+ # empty group → rc 4 bad-config naming the key, as do a
78
+ # match-everything glob, a glob that can never match
79
+ # (`./x/*`, `/x/*`, `x/`) and a vacuous command). When a
80
+ # diff ADDS a line (tracked) or holds a line (untracked)
81
+ # containing the fixed string in a file under the globs,
82
+ # a passing run — selected, inert, or a gate-side
83
+ # escalation to full — ALSO runs the command (via
84
+ # `bash -c`, same trust as test_command). An operator's
85
+ # own --full, test_command and --explain never run it.
86
+ # A scan that cannot decide runs it; a failing primary
87
+ # skips it. The marker gains a trailing
88
+ # `content_trigger=<clear|ran|failed|deferred|skipped|
89
+ # scan-error>`; --explain prints `CONTENT_TRIGGER
90
+ # armed=1 state=…`. Lib: content-trigger.sh
74
91
  #
75
92
  # Base-ref order (adapter arm only — a delegated runner resolves its own):
76
93
  # --base > @{upstream} > origin/HEAD > origin/main >
@@ -118,6 +135,8 @@ source "$GATE_DIR/lib/affected-tests/js.sh"
118
135
  source "$GATE_DIR/lib/affected-tests/python.sh"
119
136
  # shellcheck source=plugins/soleur/scripts/lib/affected-tests/go.sh
120
137
  source "$GATE_DIR/lib/affected-tests/go.sh"
138
+ # shellcheck source=plugins/soleur/scripts/lib/affected-tests/content-trigger.sh
139
+ source "$GATE_DIR/lib/affected-tests/content-trigger.sh"
121
140
 
122
141
  # Scratch files (config dump, changed-set census, adapter logs) are registered
123
142
  # in AFFECTED_SCRATCH by affected_mktemp — remove them on every exit shape,
@@ -162,6 +181,10 @@ while (( $# > 0 )); do
162
181
  shift
163
182
  done
164
183
 
184
+ # The operator's own --full, captured before any arm can escalate ARG_FULL: the
185
+ # content trigger skips only an explicit --full, never a gate-side escalation.
186
+ ARG_FULL_REQUESTED=$ARG_FULL
187
+
165
188
  # --- repository root ------------------------------------------------------------
166
189
  REPO_ROOT="$(git rev-parse --show-toplevel 2>/dev/null || true)"
167
190
  if [[ -z "$REPO_ROOT" ]]; then
@@ -213,6 +236,22 @@ affected_detect_ci() {
213
236
  return 1
214
237
  }
215
238
 
239
+ # --- glob matcher ---------------------------------------------------------------------
240
+ # A pattern containing `/` matches the whole path; `**/` also matches zero
241
+ # directories (its `**/` prefix is stripped for the second try). A bare
242
+ # pattern matches the basename. Defined before the config block: the
243
+ # match-everything probes and the content trigger both call it.
244
+ affected_glob_match() {
245
+ local p="$1" pat="$2" alt
246
+ if [[ "$pat" == */* ]]; then
247
+ [[ "$p" == $pat ]] && return 0
248
+ alt="${pat//\*\//}"
249
+ [[ "$alt" != "$pat" && "$p" == $alt ]] && return 0
250
+ return 1
251
+ fi
252
+ [[ "${p##*/}" == $pat ]]
253
+ }
254
+
216
255
  # --- config: .soleur-test-gate.json ------------------------------------------------
217
256
  CFG_TEST_COMMAND=""
218
257
  CFG_BUDGET_SECONDS=900 # default, unmeasured
@@ -222,6 +261,10 @@ CFG_GLOBAL_SET=0
222
261
  CFG_INERT_SET=0
223
262
  CFG_GLOBAL_PATHS=()
224
263
  CFG_INERT_PATHS=()
264
+ # Content trigger (lib content-trigger.sh): three flat keys, all or none. Values
265
+ # land directly in AFFECTED_CT_PATTERN/_PATHS/_COMMAND; this flag records that any
266
+ # of the three appeared, so a partial group is refused rather than silently ignored.
267
+ CFG_CT_SET=0
225
268
 
226
269
  # Dump the JSON object as NUL-separated KEY\0TYPE\0VALUE\0 triples on stdout —
227
270
  # array elements arrive as one `elem:<type>` record each, an empty array as a
@@ -300,6 +343,10 @@ JSEOF
300
343
  return 2
301
344
  }
302
345
 
346
+ # Refusal of a content-trigger key: names the offending key (a missing key and an empty one
347
+ # are the same refusal).
348
+ _affected_ct_bad() { finish 4 none "bad-config key=$1" config "$CI_NAME" - - -; }
349
+
303
350
  # Load and validate. Fail-closed everywhere: malformed, unknown key, or wrong
304
351
  # type → rc 4 reason=bad-config key=<k>; a vacuous test_command →
305
352
  # reason=vacuous-test-command.
@@ -346,6 +393,23 @@ affected_load_config() {
346
393
  budget_ci)
347
394
  [[ "$t" == boolean ]] || { badkey="$k"; break; }
348
395
  CFG_BUDGET_CI="$v" ;;
396
+ content_trigger_pattern)
397
+ [[ "$t" == string ]] || { badkey="$k"; break; }
398
+ CFG_CT_SET=1
399
+ AFFECTED_CT_PATTERN="$v" ;;
400
+ content_trigger_command)
401
+ [[ "$t" == string ]] || { badkey="$k"; break; }
402
+ CFG_CT_SET=1
403
+ AFFECTED_CT_COMMAND="$v" ;;
404
+ content_trigger_paths)
405
+ if [[ "$t" == array ]]; then
406
+ CFG_CT_SET=1
407
+ elif [[ "$t" == elem:string ]]; then
408
+ CFG_CT_SET=1
409
+ AFFECTED_CT_PATHS+=("$v")
410
+ else
411
+ badkey="$k"; break
412
+ fi ;;
349
413
  *) badkey="$k"; break ;;
350
414
  esac
351
415
  done <"$tmp"
@@ -353,6 +417,39 @@ affected_load_config() {
353
417
  finish 4 none "bad-config key=$(affected_marker_field "$badkey")" config "$CI_NAME" - - -
354
418
  fi
355
419
 
420
+ # Content trigger: all three keys or none, each refused naming ITS key (a missing
421
+ # key and an empty one are the same refusal). A half-declared trigger that silently
422
+ # never fires is exactly the failure it exists to prevent.
423
+ if (( CFG_CT_SET )); then
424
+ if [[ -z "$AFFECTED_CT_PATTERN" || "$AFFECTED_CT_PATTERN" == *$'\n'* ]]; then
425
+ _affected_ct_bad content_trigger_pattern
426
+ fi
427
+ ((${#AFFECTED_CT_PATHS[@]} > 0)) || _affected_ct_bad content_trigger_paths
428
+ local _ct_g _ct_probe
429
+ for _ct_g in "${AFFECTED_CT_PATHS[@]}"; do
430
+ # Refused: empty, multi-line or whitespace-bearing globs; shapes that can never match
431
+ # a repo-relative path in `[[ == ]]` (leading `./`, `/` or `!`, trailing `/`, `//`, a
432
+ # `.`/`..` segment, `{a,b}` which is not expanded); and a match-everything glob (the
433
+ # scan would be unbounded and the command near-permanent, as for inert_paths): any
434
+ # glob that matches one of a handful of improbable probe paths.
435
+ if [[ -z "$_ct_g" || "$_ct_g" == *$'\n'* || "$_ct_g" == *[[:space:]]* \
436
+ || "$_ct_g" == ./* || "$_ct_g" == /* || "$_ct_g" == '!'* || "$_ct_g" == */ \
437
+ || "$_ct_g" == *//* || "$_ct_g" == */./* || "$_ct_g" == . || "$_ct_g" == */. \
438
+ || "$_ct_g" == *..* || "$_ct_g" == *[{}]* ]]; then
439
+ _affected_ct_bad content_trigger_paths
440
+ fi
441
+ for _ct_probe in "zz9q8/probe.nonexistent" "a" ".hidden" "x/y.z" "Q9"; do
442
+ if affected_glob_match "$_ct_probe" "$_ct_g"; then _affected_ct_bad content_trigger_paths; fi
443
+ done
444
+ done
445
+ # A vacuous command (`true`, `cmd || true`) would certify nothing: same denylist
446
+ # as test_command, but the refusal names THIS key.
447
+ if [[ -z "$AFFECTED_CT_COMMAND" ]] || _affected_cmd_vacuous "$AFFECTED_CT_COMMAND"; then
448
+ _affected_ct_bad content_trigger_command
449
+ fi
450
+ AFFECTED_CT_ARMED=1
451
+ fi
452
+
356
453
  if [[ -n "$CFG_TEST_COMMAND" ]]; then
357
454
  # Vacuous escape hatches are refused — `true`, `:`, `exit 0`, a bare echo,
358
455
  # and every composition that ends in a swallower: `cmd || true`,
@@ -470,6 +567,7 @@ if [[ -n "$RUNNER" ]]; then
470
567
  # the operator (it can cost real time), so explain reports the delegation.
471
568
  echo "SELECTION mode=delegated runner=$RUNNER flag=$_flag"
472
569
  echo "runner owns selection — inspect with: bash $RUNNER --print-selection"
570
+ (( ARG_FULL_REQUESTED )) || AFFECTED_CT_ELIGIBLE=1
473
571
  finish 0 explain delegated runner "$CI_NAME" - - -
474
572
  fi
475
573
  if [[ "$CI_NAME" != none ]]; then
@@ -480,6 +578,9 @@ if [[ -n "$RUNNER" ]]; then
480
578
  fi
481
579
  rc=0
482
580
  affected_bound bash "$RUNNER" "$_flag" || rc=$?
581
+ # A delegated verdict is eligible for the content trigger unless the operator
582
+ # passed --full; finish() runs it only when this run passed.
583
+ (( ARG_FULL_REQUESTED )) || AFFECTED_CT_ELIGIBLE=1
483
584
  # Delegated-runner rc mapping: 4 (REFUSED — nothing ran) → 4
484
585
  # reason=runner-refused; 3 (UNRESOLVED — killed/declined suites) → 1
485
586
  # reason=runner-signal; 2 (usage) → 4 runner-refused; the rest pass through
@@ -589,20 +690,8 @@ while IFS= read -r _p; do
589
690
  done <"$CHANGED_TMP"
590
691
 
591
692
  # --- classifiers ---------------------------------------------------------------------
592
- # A pattern containing `/` matches the whole path; `**/` also matches zero
593
- # directories (its `**/` prefix is stripped for the second try). A bare
594
- # pattern matches the basename.
595
- affected_glob_match() {
596
- local p="$1" pat="$2" alt
597
- if [[ "$pat" == */* ]]; then
598
- [[ "$p" == $pat ]] && return 0
599
- alt="${pat//\*\//}"
600
- [[ "$alt" != "$pat" && "$p" == $alt ]] && return 0
601
- return 1
602
- fi
603
- [[ "${p##*/}" == $pat ]]
604
- }
605
-
693
+ # (affected_glob_match is defined above the config block: config validation and
694
+ # the content trigger need it before this point.)
606
695
  GLOBAL_PATHS_BUILTIN=(
607
696
  # manifests + lockfiles (any depth — a monorepo's nested manifests count too)
608
697
  package.json package-lock.json npm-shrinkwrap.json yarn.lock pnpm-lock.yaml
@@ -698,6 +787,13 @@ if (( ALL_INERT )) && (( ${#AFFECTED_CHANGED[@]} > 0 )) && (( ARG_FULL == 0 )) &
698
787
  (( ARG_EXPLAIN )) && echo "SELECTION mode=none reason=inert inert_rule=$(affected_marker_field "$INERT_RULE")"
699
788
  # UNIT NOTE: on this path of= counts CHANGED files (all inert), not the
700
789
  # repo test-file census other paths emit — units differ by design here.
790
+ AFFECTED_CT_ELIGIBLE=1 # under --explain the hook only reports; it runs nothing
791
+ # The inert exit precedes the budget arming below: arm it here so the content-trigger
792
+ # command is bounded (and deferred to CI at expiry) like every other post-primary run.
793
+ if [[ "$CI_NAME" != none ]]; then
794
+ AFFECTED_BUDGET_ACTIVE=1
795
+ AFFECTED_DEADLINE=$(( $(affected_now) + CFG_BUDGET_SECONDS ))
796
+ fi
701
797
  finish 0 none inert - "$CI_NAME" 0 "${#AFFECTED_CHANGED[@]}" "${#AFFECTED_CHANGED[@]}"
702
798
  fi
703
799
 
@@ -714,6 +810,7 @@ fi
714
810
 
715
811
  if (( ARG_EXPLAIN )); then
716
812
  echo "SELECTION mode=${MODE} reason=${REASON} global_hit=$(affected_marker_field "${GLOBAL_HIT:--}") ci=${CI_NAME}"
813
+ (( ARG_FULL_REQUESTED )) || AFFECTED_CT_ELIGIBLE=1
717
814
  finish 0 explain "$REASON" - "$CI_NAME" - - -
718
815
  fi
719
816
 
@@ -899,6 +996,10 @@ if (( GATE_ESCALATED_FULL )) && [[ "$OUT_MODE" == selected ]]; then
899
996
  OUT_MODE=full
900
997
  [[ "$REASON" == ok ]] && REASON=no-selector
901
998
  fi
999
+ # Eligible unless the operator passed --full. A gate-side escalation to full
1000
+ # (global paths, empty diff, degraded census, no selector) is still eligible: the
1001
+ # stack adapter's full run never runs the repo-declared command.
1002
+ (( ARG_FULL_REQUESTED )) || AFFECTED_CT_ELIGIBLE=1
902
1003
  case "$WORST" in
903
1004
  0) finish 0 "$OUT_MODE" "$REASON" "${STACKS:--}" "$CI_NAME" "$OUT_RAN" "$OUT_OF" "$SKIP_SUM" ;;
904
1005
  4) finish 4 "$OUT_MODE" "${AGG_REASON:-failed}" "${STACKS:--}" "$CI_NAME" \
@@ -690,7 +690,8 @@ reset_fakes() {
690
690
  export FAKE_TURBO_RC=0 FAKE_TURBO_EXPECT=""
691
691
  export FAKE_RUNNER_RC=0 FAKE_RUNNER_AFFECTED_RC="" FAKE_RUNNER_FULL_RC=""
692
692
  export FAKE_PM_TEST_RC=0
693
- export FAKE_GIT_FAIL_OTHERS=0
693
+ export FAKE_GIT_FAIL_OTHERS=0 FAKE_GIT_FAIL_VERB=""
694
+ export FAKE_CT_RC=0 FAKE_CT_SLEEP="" FAKE_CT_NOLEDGER=0
694
695
  }
695
696
 
696
697
  RSEQ=0; RC=0; OUT=""
@@ -1282,6 +1283,14 @@ GITFAILBIN="$TESTROOT/bin-gitfail"; mkdir -p "$GITFAILBIN"
1282
1283
  ln -sf "$BIN"/* "$GITFAILBIN/"
1283
1284
  cat > "$GITFAILBIN/git" <<'SHIM'
1284
1285
  #!/usr/bin/env bash
1286
+ if [[ -n "${FAKE_GIT_FAIL_VERB:-}" ]]; then
1287
+ for _a in "$@"; do
1288
+ if [[ "$_a" == "$FAKE_GIT_FAIL_VERB" ]]; then
1289
+ echo "stub git: simulated $FAKE_GIT_FAIL_VERB failure" >&2
1290
+ exit 1
1291
+ fi
1292
+ done
1293
+ fi
1285
1294
  if [[ "${FAKE_GIT_FAIL_OTHERS:-0}" == "1" ]]; then
1286
1295
  _ls=0; _oth=0
1287
1296
  for _a in "$@"; do
@@ -1757,6 +1766,574 @@ else
1757
1766
  fi
1758
1767
  unset _f _hits _head _PORT_FILES port_hits _BANNED _allsrc _h _golist_unb
1759
1768
 
1769
+ # ====================== Guard 4 — content trigger (#10924 P4b) ==================
1770
+ # A config-declared string, ADDED to a file under configured globs, makes a passing
1771
+ # selected/inert run also run a configured command. Every "ran" claim below is
1772
+ # asserted against the invocation ledger (the `ctcmd` line the stub appends), never
1773
+ # against an exit code alone, and every marker row pins the WHOLE line. The pattern
1774
+ # is a synthesized `zz-marker/` so nothing here is hard-coded to this repository.
1775
+
1776
+ CT_PAT='zz-marker/'
1777
+ CT_RUN='AFFECTED_GATE ran=- of=- skipped=- mode=selected reason=delegated stack=runner ci=none'
1778
+
1779
+ write_ct_cmd() { # <repo> — logs `ctcmd seq=<ledger line>`; honours FAKE_CT_RC / _SLEEP / _NOLEDGER
1780
+ mkdir -p "$1/scripts"
1781
+ cat > "$1/scripts/ct-cmd.sh" <<'CT'
1782
+ #!/usr/bin/env bash
1783
+ [[ "${FAKE_CT_NOLEDGER:-0}" == 1 ]] || printf 'ctcmd\tseq=%s\n' "$(( $(wc -l < "${FAKE_LOG:?}") + 1 ))" >> "$FAKE_LOG"
1784
+ [[ -z "${FAKE_CT_SLEEP:-}" ]] || sleep "$FAKE_CT_SLEEP"
1785
+ exit "${FAKE_CT_RC:-0}"
1786
+ CT
1787
+ cat > "$1/scripts/other-cmd.sh" <<'CT'
1788
+ #!/usr/bin/env bash
1789
+ printf 'otherctcmd\n' >> "${FAKE_LOG:?}"
1790
+ exit 0
1791
+ CT
1792
+ chmod +x "$1/scripts/ct-cmd.sh" "$1/scripts/other-cmd.sh"
1793
+ }
1794
+
1795
+ ct_cfg() { # <paths-json> [pattern] [command] [extra-json-members]
1796
+ printf '{"content_trigger_pattern":"%s","content_trigger_paths":%s,"content_trigger_command":"%s"%s}' \
1797
+ "${2:-$CT_PAT}" "$1" "${3:-bash scripts/ct-cmd.sh}" "${4:-}"
1798
+ }
1799
+
1800
+ # mk_ct_repo <name> <paths-json> [pattern] [command] [extra] — delegated-runner repo with
1801
+ # the trigger armed. Config, command stubs and a base test file that ALREADY names the
1802
+ # pattern are all committed in BASE, so only the rows' own changes are in the diff.
1803
+ mk_ct_repo() {
1804
+ local r; r="$(mk_repo "$1" "${CT_BRANCH:-main}")"
1805
+ write_runner_capable "$r"; write_ct_cmd "$r"
1806
+ write_config "$r" "$(ct_cfg "$2" "${3:-$CT_PAT}" "${4:-bash scripts/ct-cmd.sh}" "${5:-}")"
1807
+ mkdir -p "$r/tests"
1808
+ printf 'l1\n%s present\nl3\n' "$CT_PAT" > "$r/tests/base.test.sh"
1809
+ [[ "${CT_WITH_CI:-0}" == 1 ]] && seed_ci "$r"
1810
+ seal_base "$r"
1811
+ printf '%s' "$r"
1812
+ }
1813
+
1814
+ ct_seq() { sed -n "/^$1/{=;q}" "$CALLS"; } # <ledger-prefix> -> first ledger line number
1815
+ want_marker() { # <label> <exact AFFECTED_GATE line>
1816
+ local got; got="$(sed -n 's/^.*\(AFFECTED_GATE ran=.*\)$/\1/p' <<<"$OUT" | tail -n 1)"
1817
+ if [[ "$got" == "$2" ]]; then pass "$1"
1818
+ else fail "$1 — marker '$got' want '$2'"; fi
1819
+ }
1820
+
1821
+ # T1 [M1]: a committed file under a glob adds the pattern → the command runs, after the
1822
+ # runner, and the marker names it.
1823
+ R_T1="$(mk_ct_repo ct1 '["tests/*"]')"
1824
+ commit_chg "$R_T1" tests/new.test.sh "added $CT_PAT here"
1825
+ reset_fakes; run_gate "$R_T1" gate
1826
+ want_rc 0 "T1: a firing diff passes"
1827
+ want_call "T1: the runner ran" 'test-all --affected'
1828
+ want_call "T1: the content-trigger command ran (ledger)" '^ctcmd '
1829
+ want_marker "T1: whole marker line carries content_trigger=ran" "$CT_RUN content_trigger=ran"
1830
+ _rl="$(ct_seq 'test-all')"; _cl="$(ct_seq 'ctcmd')"
1831
+ if [[ -n "$_rl" && -n "$_cl" ]] && (( _rl < _cl )); then
1832
+ pass "T1/M3: the command ran AFTER the primary run (ledger sequence $_rl < $_cl)"
1833
+ else
1834
+ fail "T1/M3: ordering broken — runner line '$_rl', command line '$_cl'"
1835
+ fi
1836
+ unset _rl _cl
1837
+
1838
+ # T2 [M2]: two changed files; the first is clean, the SECOND matches — a scan that
1839
+ # stops at the first compliant file would miss it.
1840
+ R_T2="$(mk_ct_repo ct2 '["tests/*"]')"
1841
+ commit_chg "$R_T2" tests/a-clean.test.sh 'nothing to see'
1842
+ commit_chg "$R_T2" tests/b-hit.test.sh "x $CT_PAT y"
1843
+ reset_fakes; run_gate "$R_T2" gate
1844
+ want_call "T2: the second changed file fires the trigger" '^ctcmd '
1845
+ want_marker "T2: marker" "$CT_RUN content_trigger=ran"
1846
+
1847
+ # T3 [M8]: a NEW UNTRACKED file with the pattern fires (a typical in-progress fixture).
1848
+ R_T3="$(mk_ct_repo ct3 '["tests/*"]')"
1849
+ chg "$R_T3" tests/untracked.test.sh "u $CT_PAT"
1850
+ reset_fakes; run_gate "$R_T3" gate
1851
+ want_call "T3: an untracked file under the globs fires the trigger" '^ctcmd '
1852
+ want_marker "T3: marker" "$CT_RUN content_trigger=ran"
1853
+
1854
+ # T4 [M4]: the pattern is already in the file; an unrelated line is added ADJACENT to
1855
+ # it. A scan of whole files, or with context lines, would fire — the added line must be
1856
+ # the only text that counts.
1857
+ R_T4="$(mk_ct_repo ct4 '["tests/*"]')"
1858
+ printf 'l1\n%s present\nNEW unrelated line\nl3\n' "$CT_PAT" > "$R_T4/tests/base.test.sh"
1859
+ assert_fixture_dir "$R_T4"; git -C "$R_T4" add -A; git -C "$R_T4" commit -q -m "adjacent"
1860
+ reset_fakes; run_gate "$R_T4" gate
1861
+ want_no_call "T4: a pre-existing mention does not fire" '^ctcmd '
1862
+ want_marker "T4: marker says clear" "$CT_RUN content_trigger=clear"
1863
+
1864
+ # T5 [M5]: no resolvable base → the scan cannot decide → the command RUNS.
1865
+ R_T5="$(mk_repo ct5)"; write_runner_capable "$R_T5"; write_ct_cmd "$R_T5"
1866
+ write_config "$R_T5" "$(ct_cfg '["tests/*"]')"; mkdir -p "$R_T5/tests"; printf 'base\n' > "$R_T5/tests/base.test.sh"
1867
+ assert_fixture_dir "$R_T5"; git -C "$R_T5" add -A; git -C "$R_T5" commit -q -m base; git -C "$R_T5" checkout -q -b feature
1868
+ commit_chg "$R_T5" tests/x.test.sh 'no pattern at all'
1869
+ reset_fakes; run_gate "$R_T5" gate
1870
+ want_rc 0 "T5: an undecidable scan still passes when the command passes"
1871
+ want_call "T5: the command ran although the scan could not decide" '^ctcmd '
1872
+ want_marker "T5: marker names scan-error" "$CT_RUN content_trigger=scan-error"
1873
+ want_out "T5: the undecided leg is named" 'content-trigger scan could not decide \(leg=no-base\)'
1874
+
1875
+ # T6 [M6]: the command's own failure is the gate's failure.
1876
+ R_T6="$(mk_ct_repo ct6 '["tests/*"]')"
1877
+ commit_chg "$R_T6" tests/new.test.sh "$CT_PAT"
1878
+ reset_fakes; export FAKE_CT_RC=1; run_gate "$R_T6" gate
1879
+ want_rc 1 "T6: a failing command fails the gate"
1880
+ want_marker "T6: whole marker line" "AFFECTED_GATE ran=- of=- skipped=- mode=selected reason=content-trigger-failed stack=runner ci=none content_trigger=failed"
1881
+ want_out "T6: the failure is named on stdout with its rc" '^CONTENT_TRIGGER_FAILED rc=1$'
1882
+ want_out "T6: and a remedy points at the config key" 'remedy: run the content_trigger_command'
1883
+
1884
+ # T7 [M3]: a failing PRIMARY run skips the command (ledger proves it) and says so.
1885
+ R_T7="$(mk_ct_repo ct7 '["tests/*"]')"
1886
+ commit_chg "$R_T7" tests/new.test.sh "$CT_PAT"
1887
+ reset_fakes; export FAKE_RUNNER_RC=1; run_gate "$R_T7" gate
1888
+ want_rc 1 "T7: a failing primary run fails the gate"
1889
+ want_no_call "T7: the command did not run behind a failed primary" '^ctcmd '
1890
+ want_marker "T7: marker says skipped" "AFFECTED_GATE ran=- of=- skipped=- mode=selected reason=failed stack=runner ci=none content_trigger=skipped"
1891
+
1892
+ # T8: the pattern added OUTSIDE the globs does not fire.
1893
+ R_T8="$(mk_ct_repo ct8 '["tests/*"]')"
1894
+ commit_chg "$R_T8" src/outside.sh "$CT_PAT"
1895
+ reset_fakes; run_gate "$R_T8" gate
1896
+ want_no_call "T8: a changed file outside the globs does not fire" '^ctcmd '
1897
+ want_marker "T8: marker says clear" "$CT_RUN content_trigger=clear"
1898
+
1899
+ # T9 [M7]: a LARGE untracked file (many SHORT lines — one 300 KB line does not reproduce it)
1900
+ # naming the pattern on its first line. Read through a pipe under pipefail, grep -q's early exit SIGPIPEs the producer and the hit reads as a
1901
+ # miss; the scan reads the file directly.
1902
+ R_T9="$(mk_ct_repo ct9 '["tests/*"]')"
1903
+ { printf '%s\n' "$CT_PAT"; yes 'filler line of text' | head -n 40000; } > "$R_T9/tests/big.test.sh"
1904
+ reset_fakes; run_gate "$R_T9" gate
1905
+ want_call "T9: a >64 KB untracked file with an early hit fires" '^ctcmd '
1906
+ want_marker "T9: and the scan DECIDED (ran, not scan-error: an undecided scan also runs the command)" "$CT_RUN content_trigger=ran"
1907
+
1908
+ # T10 [M9]: nothing here ADDS text containing the pattern, so none may fire:
1909
+ # (a) a path that names it; (b) a diff that only REMOVES the line naming it; (c) a pure
1910
+ # rename of a file that names it.
1911
+ R_T10A="$(mk_ct_repo ct10a '["tests/*"]')"
1912
+ commit_chg "$R_T10A" "tests/${CT_PAT}hello.txt" 'plain content'
1913
+ reset_fakes; run_gate "$R_T10A" gate
1914
+ want_no_call "T10a: a path naming the pattern does not fire" '^ctcmd '
1915
+ R_T10B="$(mk_ct_repo ct10b '["tests/*"]')"
1916
+ assert_fixture_dir "$R_T10B"; printf 'l1\nl3\n' > "$R_T10B/tests/base.test.sh"; git -C "$R_T10B" add -A; git -C "$R_T10B" commit -q -m "removal"
1917
+ reset_fakes; run_gate "$R_T10B" gate
1918
+ want_no_call "T10b: a removal-only diff does not fire" '^ctcmd '
1919
+ R_T10C="$(mk_ct_repo ct10c '["tests/*"]')"
1920
+ assert_fixture_dir "$R_T10C"; git -C "$R_T10C" mv tests/base.test.sh tests/renamed.test.sh; git -C "$R_T10C" commit -q -m "rename"
1921
+ reset_fakes; run_gate "$R_T10C" gate
1922
+ want_call "T10c: a rename of a file that names the string fires (--no-renames: delete plus add)" '^ctcmd '
1923
+
1924
+ # T11 [M10]: the exits that are not a selected verdict never run the command, and
1925
+ # --explain reports without running anything.
1926
+ R_T11="$(mk_ct_repo ct11 '["tests/*"]')"
1927
+ commit_chg "$R_T11" tests/new.test.sh "$CT_PAT"
1928
+ reset_fakes; run_gate "$R_T11" selfcheck --self-check
1929
+ want_no_call "T11: --self-check never runs the command" '^ctcmd '
1930
+ reset_fakes; run_gate "$R_T11" explain --explain
1931
+ want_rc 0 "T11: --explain exits 0"
1932
+ want_out "T11: --explain reports the armed trigger and its state" '^CONTENT_TRIGGER armed=1 state=fired paths=1$'
1933
+ want_no_call "T11: --explain never runs the command" '^ctcmd '
1934
+ reset_fakes; run_gate "$R_T11" gate --full
1935
+ want_no_call "T11: --full never runs the command" '^ctcmd '
1936
+ want_no_out "T11: a --full marker carries no content_trigger field" 'content_trigger='
1937
+ R_T11B="$(mk_ct_repo ct11b '["tests/*"]' "$CT_PAT" 'bash scripts/ct-cmd.sh' ',"test_command":"bash scripts/other-cmd.sh"')"
1938
+ reset_fakes; run_gate "$R_T11B" explain --explain
1939
+ want_no_out "T11: the test_command explain arm prints no CONTENT_TRIGGER line" 'CONTENT_TRIGGER'
1940
+
1941
+ # T12: the budget bounds the command. With a CI workflow the deferral is rc 6; with none
1942
+ # the command's own expiry exit (124) is a plain failure.
1943
+ R_T12="$(CT_WITH_CI=1 mk_ct_repo ct12 '["tests/*"]' "$CT_PAT" 'bash scripts/ct-cmd.sh' ',"budget_seconds":2')"
1944
+ commit_chg "$R_T12" tests/new.test.sh "$CT_PAT"
1945
+ reset_fakes; export FAKE_CT_SLEEP=6; run_gate "$R_T12" gate
1946
+ want_rc 6 "T12: the budget expiring inside the command defers (CI detected)"
1947
+ want_out "T12: the deferral marker prints" 'AFFECTED_GATE_BUDGET_DEFERRED'
1948
+ want_marker "T12: the command started and timed out: field is deferred, not skipped" "AFFECTED_GATE ran=- of=- skipped=- mode=selected reason=budget-deferred stack=runner ci=.github/workflows/ci.yml content_trigger=deferred"
1949
+ R_T12C="$(CT_WITH_CI=1 mk_ct_repo ct12c '["tests/*"]' "$CT_PAT" 'bash scripts/ct-cmd.sh' ',"budget_seconds":60')"
1950
+ commit_chg "$R_T12C" tests/new.test.sh "$CT_PAT"
1951
+ reset_fakes; run_gate "$R_T12C" gate
1952
+ want_rc 0 "T12: the same CI repo without the sleep passes (the budget, not the CI, caused the deferral)"
1953
+ want_marker "T12: and reports ran" "AFFECTED_GATE ran=- of=- skipped=- mode=selected reason=delegated stack=runner ci=.github/workflows/ci.yml content_trigger=ran"
1954
+ R_T12B="$(mk_ct_repo ct12b '["tests/*"]')"
1955
+ commit_chg "$R_T12B" tests/new.test.sh "$CT_PAT"
1956
+ reset_fakes; export FAKE_CT_RC=124; run_gate "$R_T12B" gate
1957
+ want_rc 1 "T12: an expiry-shaped exit without a CI workflow is a plain failure"
1958
+
1959
+ # T13: config errors are refused, each naming the key.
1960
+ ct_bad() { # <name> <json> <ere>
1961
+ local r; r="$(mk_cfg_repo "$1" "$2")"
1962
+ reset_fakes; run_gate "$r" gate
1963
+ want_rc 4 "T13: $1 refused"
1964
+ want_out "T13: $1 names the cause" "$3"
1965
+ }
1966
+ ct_bad ct13-nopaths '{"content_trigger_pattern":"x/"}' 'bad-config key=content_trigger_paths'
1967
+ ct_bad ct13-nocmd '{"content_trigger_pattern":"x/","content_trigger_paths":["tests/*"]}' 'bad-config key=content_trigger_command'
1968
+ ct_bad ct13-nopattern '{"content_trigger_paths":["tests/*"],"content_trigger_command":"bash scripts/ct-cmd.sh"}' 'bad-config key=content_trigger_pattern'
1969
+ ct_bad ct13-emptypaths '{"content_trigger_pattern":"x/","content_trigger_paths":[],"content_trigger_command":"bash scripts/ct-cmd.sh"}' 'bad-config key=content_trigger_paths'
1970
+ ct_bad ct13-all '{"content_trigger_pattern":"x/","content_trigger_paths":["*"],"content_trigger_command":"bash scripts/ct-cmd.sh"}' 'bad-config key=content_trigger_paths'
1971
+ ct_bad ct13-deepall '{"content_trigger_pattern":"x/","content_trigger_paths":["**/*"],"content_trigger_command":"bash scripts/ct-cmd.sh"}' 'bad-config key=content_trigger_paths'
1972
+ ct_bad ct13-vacuous '{"content_trigger_pattern":"x/","content_trigger_paths":["tests/*"],"content_trigger_command":"true"}' 'bad-config key=content_trigger_command'
1973
+ ct_bad ct13-emptypat '{"content_trigger_pattern":"","content_trigger_paths":["tests/*"],"content_trigger_command":"bash scripts/ct-cmd.sh"}' 'bad-config key=content_trigger_pattern'
1974
+ ct_bad ct13-nlpat '{"content_trigger_pattern":"a\nb","content_trigger_paths":["tests/*"],"content_trigger_command":"bash scripts/ct-cmd.sh"}' 'bad-config key=content_trigger_pattern'
1975
+ ct_bad ct13-numpat '{"content_trigger_pattern":3,"content_trigger_paths":["tests/*"],"content_trigger_command":"bash scripts/ct-cmd.sh"}' 'bad-config key=content_trigger_pattern'
1976
+ ct_bad ct13-strpaths '{"content_trigger_pattern":"x/","content_trigger_paths":"tests/*","content_trigger_command":"bash scripts/ct-cmd.sh"}' 'bad-config key=content_trigger_paths'
1977
+ ct_bad ct13-dotslash '{"content_trigger_pattern":"x/","content_trigger_paths":["./tests/*"],"content_trigger_command":"bash scripts/ct-cmd.sh"}' 'bad-config key=content_trigger_paths'
1978
+ ct_bad ct13-abs '{"content_trigger_pattern":"x/","content_trigger_paths":["/tests/*"],"content_trigger_command":"bash scripts/ct-cmd.sh"}' 'bad-config key=content_trigger_paths'
1979
+ ct_bad ct13-trailslash '{"content_trigger_pattern":"x/","content_trigger_paths":["tests/"],"content_trigger_command":"bash scripts/ct-cmd.sh"}' 'bad-config key=content_trigger_paths'
1980
+ ct_bad ct13-secondall '{"content_trigger_pattern":"x/","content_trigger_paths":["tests/*","*"],"content_trigger_command":"bash scripts/ct-cmd.sh"}' 'bad-config key=content_trigger_paths'
1981
+ ct_bad ct13-emptyglob '{"content_trigger_pattern":"x/","content_trigger_paths":["tests/*",""],"content_trigger_command":"bash scripts/ct-cmd.sh"}' 'bad-config key=content_trigger_paths'
1982
+ ct_bad ct13-arrcmd '{"content_trigger_pattern":"x/","content_trigger_paths":["tests/*"],"content_trigger_command":["bash"]}' 'bad-config key=content_trigger_command'
1983
+
1984
+ # T14: a path with a space and a non-ASCII character, under a repo whose diff config
1985
+ # strips or rewrites prefixes, fires; the hook honours --base while defaulting to
1986
+ # origin/main.
1987
+ R_T14="$(mk_ct_repo ct14 '["tests/*"]')"
1988
+ assert_fixture_dir "$R_T14"; git -C "$R_T14" config diff.noprefix true
1989
+ assert_fixture_dir "$R_T14"; git -C "$R_T14" config diff.mnemonicPrefix true
1990
+ _b0="$(git -C "$R_T14" rev-parse origin/main)"
1991
+ commit_chg "$R_T14" 'tests/dir with space/é.test.sh' "$CT_PAT"
1992
+ reset_fakes; run_gate "$R_T14" gate
1993
+ want_call "T14: a spaced, non-ASCII path fires despite diff.noprefix/mnemonicPrefix" '^ctcmd '
1994
+ assert_fixture_dir "$R_T14"; git -C "$R_T14" update-ref refs/remotes/origin/main HEAD
1995
+ reset_fakes; run_gate "$R_T14" gate
1996
+ want_no_call "T14: with origin/main at HEAD nothing is pending" '^ctcmd '
1997
+ reset_fakes; run_gate "$R_T14" gate --base "$_b0"
1998
+ want_call "T14: --base steers the scan" '^ctcmd '
1999
+ unset _b0
2000
+
2001
+ # T15: the inert path. A docs-only diff that nonetheless names the pattern runs the
2002
+ # command and no longer claims "docs-only"; without the pattern it still does.
2003
+ mk_inert_ct_repo() { # <name>
2004
+ local r; r="$(mk_repo "$1")"; seed_js "$r"; write_ct_cmd "$r"
2005
+ write_config "$r" "$(ct_cfg '["docs/*"]')"
2006
+ [[ "${CT_WITH_CI:-0}" == 1 ]] && seed_ci "$r"
2007
+ seal_base "$r"; printf '%s' "$r"
2008
+ }
2009
+ R_T15="$(mk_inert_ct_repo ct15)"
2010
+ commit_chg "$R_T15" docs/note.md "$CT_PAT"
2011
+ reset_fakes; run_gate "$R_T15" gate
2012
+ want_rc 0 "T15: an inert diff that fires the trigger passes"
2013
+ want_call "T15: the command ran on the inert path" '^ctcmd '
2014
+ want_no_out "T15: no 'docs-only change' claim after the command ran" 'docs-only change'
2015
+ want_marker "T15: whole marker line" "AFFECTED_GATE ran=- of=- skipped=- mode=selected reason=inert-content-trigger stack=- ci=none content_trigger=ran"
2016
+ R_T15B="$(mk_inert_ct_repo ct15b)"
2017
+ commit_chg "$R_T15B" docs/note.md 'plain prose'
2018
+ reset_fakes; run_gate "$R_T15B" gate
2019
+ want_out "T15: a clean inert diff keeps the docs-only claim" 'docs-only change'
2020
+ want_no_call "T15: and runs nothing" '^ctcmd '
2021
+ want_out "T15: and the marker says clear" 'reason=inert stack=- ci=none content_trigger=clear$'
2022
+
2023
+ # T16: the committed repo config arms the trigger on a real checkout.
2024
+ run_gate "$REPO_ROOT" explain --explain
2025
+ want_rc 0 "T16: the live repo's gate config validates"
2026
+ want_out "T16: the live repo's trigger is armed" '^CONTENT_TRIGGER armed=1 state='
2027
+ _ct_cfg_cmd="$(python3 -I -c 'import json,sys; print(json.load(open(sys.argv[1])).get("content_trigger_command",""))' "$REPO_ROOT/.soleur-test-gate.json" 2>/dev/null || true)"
2028
+ _ct_cfg_pat="$(python3 -I -c 'import json,sys; print(json.load(open(sys.argv[1])).get("content_trigger_pattern",""))' "$REPO_ROOT/.soleur-test-gate.json" 2>/dev/null || true)"
2029
+ if [[ "$_ct_cfg_cmd" == "bash scripts/test-affected-kb-consumers.test.sh" && "$_ct_cfg_pat" == "knowledge""-base/" ]]; then
2030
+ pass "T16: the committed trigger runs the dropped-consumer ratchet on the kb path"
2031
+ else
2032
+ fail "T16: the committed trigger is '$_ct_cfg_pat' -> '$_ct_cfg_cmd'"
2033
+ fi
2034
+ unset _ct_cfg_cmd _ct_cfg_pat
2035
+
2036
+
2037
+ # ---- adapter arm, scan legs, glob/config edge cases, marker honesty ----
2038
+
2039
+ # E1: --explain on an INERT diff reports and runs nothing (the inert exit is a third
2040
+ # explain-capable exit; the flag that arms the hook must not make it run the command).
2041
+ R_CTE1="$(mk_inert_ct_repo cte1)"
2042
+ commit_chg "$R_CTE1" docs/note.md "$CT_PAT"
2043
+ reset_fakes; run_gate "$R_CTE1" explain --explain
2044
+ want_no_call "CTE1: --explain on an inert diff never runs the command" '^ctcmd '
2045
+ want_out "CTE1: and reports the scan state" '^CONTENT_TRIGGER armed=1 state=fired paths=1$'
2046
+
2047
+ # A: the ADAPTER arm (a repo with no capability-positive runner).
2048
+ mk_adapter_ct() { # <name> — vitest stack, trigger armed on src/*
2049
+ local r; r="$(mk_repo "$1")"; seed_js "$r"; write_ct_cmd "$r"
2050
+ write_config "$r" "$(ct_cfg '["src/*"]')"; seal_base "$r"; printf '%s' "$r"
2051
+ }
2052
+ R_A1="$(mk_adapter_ct cta1)"
2053
+ commit_chg "$R_A1" src/fx.test.ts "import x; // $CT_PAT"
2054
+ reset_fakes; run_gate "$R_A1" gate
2055
+ want_rc 0 "A1: an adapter-arm selected run passes"
2056
+ want_call "A1: and ran the command" '^ctcmd '
2057
+ want_field "A1: marker field" content_trigger 'ran'
2058
+ want_field "A1: mode is selected" mode 'selected'
2059
+ reset_fakes; run_gate "$R_A1" gate --full
2060
+ want_no_call "A2: an operator's --full never runs the command (adapter arm)" '^ctcmd '
2061
+ want_no_out "A2: and the marker carries no field" 'content_trigger='
2062
+ reset_fakes; run_gate "$R_A1" explain --explain --full
2063
+ want_no_out "A3: the adapter-arm --explain under an operator --full prints no CONTENT_TRIGGER line" 'CONTENT_TRIGGER'
2064
+ want_no_call "A3: and runs nothing" '^ctcmd '
2065
+ reset_fakes; run_gate "$R_A1" explain --explain
2066
+ want_no_call "A3: --explain on the adapter arm runs nothing" '^ctcmd '
2067
+ want_out "A3: and reports the armed trigger" '^CONTENT_TRIGGER armed=1 state=fired paths=1$'
2068
+ # A4: a gate-side escalation to full (a global-paths hit) still runs the command: the
2069
+ # stack adapter's full run never runs the repo-declared command.
2070
+ R_A4="$(mk_adapter_ct cta4)"
2071
+ commit_chg "$R_A4" src/fx.test.ts "import x; // $CT_PAT"
2072
+ commit_chg "$R_A4" package.json '{"name":"fixture-js","private":true,"scripts":{"test":"vitest run"},"devDependencies":{"vitest":"^4.0.0"},"x":1}'
2073
+ reset_fakes; run_gate "$R_A4" gate
2074
+ want_field "A4: the diff escalated to full (global paths)" reason 'global-paths'
2075
+ want_field "A4: mode=full" mode 'full'
2076
+ want_call "A4: the command ran although the gate escalated to full" '^ctcmd '
2077
+ want_field "A4: marker field" content_trigger 'ran'
2078
+
2079
+ # S: every scan leg that cannot decide runs the command (fail toward coverage).
2080
+ for _leg in diff merge-base; do
2081
+ R_S="$(mk_ct_repo cts-$_leg '["tests/*"]')"
2082
+ commit_chg "$R_S" tests/new.test.sh 'no pattern here'
2083
+ reset_fakes; export FAKE_GIT_FAIL_VERB="$_leg"
2084
+ run_gate_path "$GITFAILBIN:$BASEPATH" "$R_S" gate
2085
+ want_call "S: a failing git $_leg still runs the command" '^ctcmd '
2086
+ want_field "S: git $_leg failure is a scan-error" content_trigger 'scan-error'
2087
+ case "$_leg" in diff) _want='leg=diff' ;; *) _want='leg=no-merge-base' ;; esac
2088
+ want_out "S: git $_leg failure names its own leg" "$_want"
2089
+ done
2090
+ unset _want
2091
+ R_S="$(mk_ct_repo cts-ls '["tests/*"]')"
2092
+ commit_chg "$R_S" tests/new.test.sh 'no pattern here'
2093
+ reset_fakes; export FAKE_GIT_FAIL_VERB=ls-files
2094
+ run_gate_path "$GITFAILBIN:$BASEPATH" "$R_S" gate
2095
+ want_call "S: a failing git ls-files still runs the command" '^ctcmd '
2096
+ want_out "S: and names the leg" 'leg=ls-files'
2097
+ # A --base that looks like an option is never handed to git as one.
2098
+ reset_fakes; run_gate "$R_S" gate --base --all
2099
+ want_call "S: an option-shaped --base is a scan-error that runs the command" '^ctcmd '
2100
+ want_out "S: and names the leg" 'leg=bad-base'
2101
+ unset _leg R_S
2102
+
2103
+ # W: what the tracked/untracked legs read.
2104
+ R_W1="$(mk_ct_repo ctw1 '["tests/*"]')"
2105
+ printf 'l1\n%s present\nl3\nunstaged %s\n' "$CT_PAT" "$CT_PAT" > "$R_W1/tests/base.test.sh"
2106
+ reset_fakes; run_gate "$R_W1" gate
2107
+ want_call "W1: an UNSTAGED edit of a tracked file fires" '^ctcmd '
2108
+ R_W2="$(mk_ct_repo ctw2 '["tests/*"]')"
2109
+ printf 'l1\n%s present\nl3\nstaged %s\n' "$CT_PAT" "$CT_PAT" > "$R_W2/tests/base.test.sh"
2110
+ assert_fixture_dir "$R_W2"; git -C "$R_W2" add tests/base.test.sh
2111
+ reset_fakes; run_gate "$R_W2" gate
2112
+ want_call "W2: a STAGED edit of a tracked file fires" '^ctcmd '
2113
+ R_W3="$(mk_ct_repo ctw3 '["docs/*","tests/*"]')"
2114
+ commit_chg "$R_W3" tests/new.test.sh "$CT_PAT"
2115
+ reset_fakes; run_gate "$R_W3" gate
2116
+ want_call "W3: a hit under the LAST of two globs fires" '^ctcmd '
2117
+ reset_fakes; run_gate "$R_W3" explain --explain
2118
+ want_out "W3: --explain counts both globs" '^CONTENT_TRIGGER armed=1 state=fired paths=2$'
2119
+ R_W4="$(mk_ct_repo ctw4 '["tests/*","docs/*"]')"
2120
+ commit_chg "$R_W4" docs/new.txt "$CT_PAT"
2121
+ reset_fakes; run_gate "$R_W4" gate
2122
+ want_call "W4: a hit under the SECOND of two globs fires" '^ctcmd '
2123
+ R_W5="$(mk_ct_repo ctw5 '["tests/*"]')"
2124
+ chg "$R_W5" src/outside.sh "$CT_PAT"
2125
+ reset_fakes; run_gate "$R_W5" gate
2126
+ want_no_call "W5: an UNTRACKED file outside the globs does not fire" '^ctcmd '
2127
+ want_field "W5: marker says clear" content_trigger 'clear'
2128
+ R_W6="$(mk_ct_repo ctw6 '["tests/*"]')"
2129
+ commit_chg "$R_W6" .gitignore 'tests/ignored.test.sh'
2130
+ chg "$R_W6" tests/ignored.test.sh "$CT_PAT"
2131
+ reset_fakes; run_gate "$R_W6" gate
2132
+ want_no_call "W6: a gitignored untracked file does not fire" '^ctcmd '
2133
+ want_field "W6: and the scan answered clear" content_trigger 'clear'
2134
+ R_W7="$(mk_ct_repo ctw7 '["tests/*"]')"
2135
+ printf 'a\0%s\n' "$CT_PAT" > "$R_W7/tests/blob.test.sh"
2136
+ reset_fakes; run_gate "$R_W7" gate
2137
+ want_call "W7: an untracked file with a NUL byte fires (as the tracked --text leg does)" '^ctcmd '
2138
+ R_W8="$(mk_ct_repo ctw8 '["tests/*"]')"
2139
+ commit_chg "$R_W8" .gitattributes 'tests/attr.test.sh -diff'
2140
+ commit_chg "$R_W8" tests/attr.test.sh "x $CT_PAT"
2141
+ reset_fakes; run_gate "$R_W8" gate
2142
+ want_field "W8: a tracked file marked -diff is still read (--text)" content_trigger 'ran'
2143
+ R_W9="$(mk_ct_repo ctw9 '["tests/*"]')"
2144
+ printf 'outside %s\n' "$CT_PAT" > "$TESTROOT/outside-target.txt"
2145
+ ln -s "$TESTROOT/outside-target.txt" "$R_W9/tests/link.test.sh"
2146
+ reset_fakes; run_gate "$R_W9" gate
2147
+ want_no_call "W9: an untracked symlink is not read" '^ctcmd '
2148
+ want_field "W9: and the scan answered clear" content_trigger 'clear'
2149
+ R_W10="$(mk_ct_repo ctw10 '["tests/*"]')"
2150
+ commit_chg "$R_W10" 'tests/q"uote.test.sh' "$CT_PAT"
2151
+ reset_fakes; run_gate "$R_W10" gate
2152
+ want_call "W10: a tracked file whose name git C-quotes still runs the command" '^ctcmd '
2153
+ want_out "W10: scan-error, leg quoted-path" 'leg=quoted-path'
2154
+ R_W11="$(mk_ct_repo ctw11 '["tests/*"]')"
2155
+ chg "$R_W11" 'tests/q"u.test.sh' 'nothing'
2156
+ reset_fakes; run_gate "$R_W11" gate
2157
+ want_call "W11: an untracked C-quoted name runs the command" '^ctcmd '
2158
+ want_out "W11: scan-error, leg quoted-path" 'leg=quoted-path'
2159
+ R_W12="$(mk_ct_repo ctw12 '["tests/*"]')"
2160
+ for _i in $(seq 1 501); do printf 'x\n' > "$R_W12/tests/gen$_i.txt"; done
2161
+ reset_fakes; run_gate "$R_W12" gate
2162
+ want_call "W12: an untracked tree past the read cap runs the command" '^ctcmd '
2163
+ want_out "W12: scan-error, leg untracked-cap" 'leg=untracked-cap'
2164
+ unset _i
2165
+ R_W13="$(mk_ct_repo ctw13 '["tests/*"]' 'a.b/')"
2166
+ commit_chg "$R_W13" tests/new.test.sh 'only axb/ here'
2167
+ reset_fakes; run_gate "$R_W13" gate
2168
+ want_no_call "W13: the pattern is a FIXED string ('.' is not a wildcard)" '^ctcmd '
2169
+ want_field "W13: and the scan answered clear" content_trigger 'clear'
2170
+ commit_chg "$R_W13" tests/other.test.sh 'literal a.b/ here'
2171
+ reset_fakes; run_gate "$R_W13" gate
2172
+ want_call "W13: and the literal form fires" '^ctcmd '
2173
+ R_W14="$(CT_BRANCH=master mk_ct_repo ctw14 '["tests/*"]')"
2174
+ commit_chg "$R_W14" tests/new.test.sh "$CT_PAT"
2175
+ reset_fakes; run_gate "$R_W14" gate
2176
+ want_field "W14: origin/master is the fallback base" content_trigger 'ran'
2177
+ R_W15="$(mk_ct_repo ctw15 '["tests/*.test.sh"]')"
2178
+ assert_fixture_dir "$R_W15"; git -C "$R_W15" config diff.renames false
2179
+ commit_chg "$R_W15" 'tests/dir with space/é.test.sh' "$CT_PAT"
2180
+ reset_fakes; run_gate "$R_W15" gate
2181
+ want_field "W15: a spaced path under a SUFFIX glob fires (the TAB git appends is stripped)" content_trigger 'ran'
2182
+
2183
+ # P: a primary that is refused owns the verdict.
2184
+ R_P1="$(mk_ct_repo ctp1 '["tests/*"]')"
2185
+ commit_chg "$R_P1" tests/new.test.sh "$CT_PAT"
2186
+ reset_fakes; export FAKE_RUNNER_RC=4; run_gate "$R_P1" gate
2187
+ want_no_call "P1: a refused (rc 4) primary does not run the command" '^ctcmd '
2188
+ want_marker "P1: marker says skipped" "AFFECTED_GATE ran=- of=- skipped=- mode=selected reason=runner-refused stack=runner ci=none content_trigger=skipped"
2189
+
2190
+ # I: an inert diff whose command fails keeps the marker honest.
2191
+ R_I1="$(mk_inert_ct_repo cti1)"
2192
+ commit_chg "$R_I1" docs/note.md "$CT_PAT"
2193
+ reset_fakes; export FAKE_CT_RC=1; run_gate "$R_I1" gate
2194
+ want_rc 1 "I1: an inert diff whose command fails fails the gate"
2195
+ want_marker "I1: marker is not the inert counts under mode=none" "AFFECTED_GATE ran=- of=- skipped=- mode=selected reason=content-trigger-failed stack=- ci=none content_trigger=failed"
2196
+ # I2: the inert exit precedes the generic budget arming, so it arms its own: a command
2197
+ # killed (137) on an inert diff with a detected CI workflow defers to CI like any other run.
2198
+ R_I2="$(CT_WITH_CI=1 mk_inert_ct_repo cti2)"
2199
+ commit_chg "$R_I2" docs/note.md "$CT_PAT"
2200
+ reset_fakes; export FAKE_CT_RC=137; run_gate "$R_I2" gate
2201
+ want_rc 6 "I2: an inert diff with CI and a killed command defers (the budget is armed on the inert exit)"
2202
+ want_field "I2: field is deferred" content_trigger 'deferred'
2203
+
2204
+ # ---- second batch: escalated full, deferred primary, 137, bounds, fixed strings, dead globs ----
2205
+
2206
+ # A5: a degraded census (git ls-files --others fails) escalates the adapter to full; the
2207
+ # trigger must still run. (ARG_FULL is set by the escalation, ARG_FULL_REQUESTED is not.)
2208
+ R_A5="$(mk_adapter_ct cta5)"
2209
+ commit_chg "$R_A5" src/fx.test.ts "import x; // $CT_PAT"
2210
+ printf 'import { test } from "vitest";\ntest("n", () => {});\n' > "$R_A5/src/new.test.ts"
2211
+ reset_fakes; export FAKE_GIT_FAIL_OTHERS=1
2212
+ run_gate_path "$GITFAILBIN:$BASEPATH" "$R_A5" gate
2213
+ want_field "A5: the census degraded to full" reason 'census-degraded'
2214
+ want_call "A5: the command ran although the gate escalated to full" '^ctcmd '
2215
+
2216
+ # P2: a delegated primary that defers (rc 6 via budget) owns the verdict: command skipped.
2217
+ R_P2="$(CT_WITH_CI=1 mk_ct_repo ctp2 '["tests/*"]' "$CT_PAT" 'bash scripts/ct-cmd.sh' ',"budget_seconds":60')"
2218
+ commit_chg "$R_P2" tests/new.test.sh "$CT_PAT"
2219
+ reset_fakes; export FAKE_RUNNER_RC=124; run_gate "$R_P2" gate
2220
+ want_rc 6 "P2: a runner expiry defers (CI detected)"
2221
+ want_no_call "P2: and the command does not run behind a deferred primary" '^ctcmd '
2222
+ want_field "P2: marker says skipped" content_trigger 'skipped'
2223
+
2224
+ # D137: the command itself exiting 137 under an active budget with CI defers like 124.
2225
+ R_D1="$(CT_WITH_CI=1 mk_ct_repo ctd1 '["tests/*"]' "$CT_PAT" 'bash scripts/ct-cmd.sh' ',"budget_seconds":60')"
2226
+ commit_chg "$R_D1" tests/new.test.sh "$CT_PAT"
2227
+ reset_fakes; export FAKE_CT_RC=137; run_gate "$R_D1" gate
2228
+ want_rc 6 "D137: a command exit of 137 under an active budget defers"
2229
+ want_field "D137: field is deferred" content_trigger 'deferred'
2230
+ want_out "D137: the SIGKILL note is printed" 'exited 137'
2231
+ want_out "D137: and the human line says only the command is owed" 'only the command is owed to CI'
2232
+
2233
+ # U: untracked-leg bounds and guards.
2234
+ R_U1="$(mk_ct_repo ctu1 '["tests/*"]' 'a.b/')"
2235
+ printf 'axb/ only\n' > "$R_U1/tests/untracked.test.sh"
2236
+ reset_fakes; run_gate "$R_U1" gate
2237
+ want_no_call "U1: the UNTRACKED leg also matches a fixed string ('.' is not a wildcard)" '^ctcmd '
2238
+ want_field "U1: and answered clear" content_trigger 'clear'
2239
+ printf 'a.b/ here\n' > "$R_U1/tests/untracked2.test.sh"
2240
+ reset_fakes; run_gate "$R_U1" gate
2241
+ want_call "U1: the literal form fires" '^ctcmd '
2242
+ R_U2="$(mk_ct_repo ctu2 '["tests/*"]')"
2243
+ for _i in $(seq 1 500); do printf 'x\n' > "$R_U2/tests/gen$_i.txt"; done
2244
+ reset_fakes; run_gate "$R_U2" gate
2245
+ want_field "U2: exactly 500 candidate files is within the read cap" content_trigger 'clear'
2246
+ unset _i
2247
+ R_U3="$(mk_ct_repo ctu3 '["tests/*"]')"
2248
+ mkdir -p "$R_U3/gen"; ( cd "$R_U3/gen" && seq 1 20001 | sed 's/^/f/' | xargs touch )
2249
+ reset_fakes; run_gate "$R_U3" gate
2250
+ want_out "U3: an untracked listing past 20000 entries declines (cap, outside the globs)" 'leg=untracked-cap'
2251
+ want_call "U3: and runs the command" '^ctcmd '
2252
+ if [[ "$(id -u)" != "0" ]]; then
2253
+ R_U4="$(mk_ct_repo ctu4 '["tests/*"]')"
2254
+ printf 'x\n' > "$R_U4/tests/locked.test.sh"; chmod 000 "$R_U4/tests/locked.test.sh"
2255
+ reset_fakes; run_gate "$R_U4" gate
2256
+ want_out "U4: an unreadable untracked file (grep error) declines" 'leg=grep'
2257
+ chmod 600 "$R_U4/tests/locked.test.sh"
2258
+ fi
2259
+
2260
+ # X: pattern shapes the scan must treat literally.
2261
+ R_X1="$(mk_ct_repo ctx1 '["tests/*"]' '-xq/')"
2262
+ commit_chg "$R_X1" tests/new.test.sh 'a -xq/ b'
2263
+ reset_fakes; run_gate "$R_X1" gate
2264
+ want_field "X1: a leading-dash pattern is a pattern, not an option" content_trigger 'ran'
2265
+ R_X1U="$(mk_ct_repo ctx1u '["tests/*"]' '-xq/')"
2266
+ chg "$R_X1U" tests/untracked.test.sh 'c -xq/ d'
2267
+ reset_fakes; run_gate "$R_X1U" gate
2268
+ want_field "X1: (untracked leg alone: nothing committed to mask it)" content_trigger 'ran'
2269
+ R_X2="$(mk_ct_repo ctx2 '["tests/*"]' 'a\\b/')"
2270
+ printf '%s\n' 'x a\b/ y' > "$R_X2/tests/new.test.sh" # commit_chg's printf %b would eat the backslash
2271
+ assert_fixture_dir "$R_X2"; git -C "$R_X2" add -A; git -C "$R_X2" commit -q -m "backslash"
2272
+ reset_fakes; run_gate "$R_X2" gate
2273
+ want_field "X2: a backslash in the pattern reaches awk verbatim (ENVIRON, not -v)" content_trigger 'ran'
2274
+
2275
+ # B: base preference.
2276
+ R_B1="$(mk_ct_repo ctb1 '["tests/*"]')"
2277
+ commit_chg "$R_B1" tests/new.test.sh "$CT_PAT"
2278
+ assert_fixture_dir "$R_B1"; git -C "$R_B1" update-ref refs/remotes/origin/master HEAD
2279
+ reset_fakes; run_gate "$R_B1" gate
2280
+ want_field "B1: origin/main is preferred over origin/master (a stale master at HEAD must not hide the fixture)" content_trigger 'ran'
2281
+
2282
+ # R: renames.
2283
+ R_R1="$(mk_ct_repo ctr1 '["tests/*"]')"
2284
+ mkdir -p "$R_R1/docs"; printf 'moves in %s\n' "$CT_PAT" > "$R_R1/docs/fx.sh"
2285
+ assert_fixture_dir "$R_R1"; git -C "$R_R1" add -A; git -C "$R_R1" commit -q -m "docs fx"
2286
+ git -C "$R_R1" update-ref refs/remotes/origin/main HEAD
2287
+ assert_fixture_dir "$R_R1"; git -C "$R_R1" mv docs/fx.sh tests/fx.sh
2288
+ printf 'unrelated edit\n' >> "$R_R1/tests/fx.sh"
2289
+ reset_fakes; run_gate "$R_R1" gate
2290
+ want_field "R1: moving a file that names the string INTO the globs, with an edit, fires" content_trigger 'ran'
2291
+
2292
+ # V: explain under an operator --full is silent (a real --full would not run the command).
2293
+ R_V1="$(mk_ct_repo ctv1 '["tests/*"]')"
2294
+ commit_chg "$R_V1" tests/new.test.sh "$CT_PAT"
2295
+ reset_fakes; run_gate "$R_V1" explain --explain --full
2296
+ want_no_out "V1: --explain --full prints no CONTENT_TRIGGER line" 'CONTENT_TRIGGER'
2297
+ want_no_call "V1: and runs nothing" '^ctcmd '
2298
+
2299
+ # G: config globs that can never match, or match too much.
2300
+ ct_bad ct13-brace '{"content_trigger_pattern":"x/","content_trigger_paths":["{a,b}/x"],"content_trigger_command":"bash scripts/ct-cmd.sh"}' 'bad-config key=content_trigger_paths'
2301
+ ct_bad ct13-dslash '{"content_trigger_pattern":"x/","content_trigger_paths":["tests//x"],"content_trigger_command":"bash scripts/ct-cmd.sh"}' 'bad-config key=content_trigger_paths'
2302
+ ct_bad ct13-dotseg '{"content_trigger_pattern":"x/","content_trigger_paths":["tests/./x"],"content_trigger_command":"bash scripts/ct-cmd.sh"}' 'bad-config key=content_trigger_paths'
2303
+ ct_bad ct13-dotdot '{"content_trigger_pattern":"x/","content_trigger_paths":["../tests/*"],"content_trigger_command":"bash scripts/ct-cmd.sh"}' 'bad-config key=content_trigger_paths'
2304
+ ct_bad ct13-space '{"content_trigger_pattern":"x/","content_trigger_paths":["tests/* "],"content_trigger_command":"bash scripts/ct-cmd.sh"}' 'bad-config key=content_trigger_paths'
2305
+ ct_bad ct13-bang '{"content_trigger_pattern":"x/","content_trigger_paths":["!tests/*"],"content_trigger_command":"bash scripts/ct-cmd.sh"}' 'bad-config key=content_trigger_paths'
2306
+ ct_bad ct13-notp '{"content_trigger_pattern":"x/","content_trigger_paths":["[!p]*"],"content_trigger_command":"bash scripts/ct-cmd.sh"}' 'bad-config key=content_trigger_paths'
2307
+ ct_bad ct13-dot '{"content_trigger_pattern":"x/","content_trigger_paths":["."],"content_trigger_command":"bash scripts/ct-cmd.sh"}' 'bad-config key=content_trigger_paths'
2308
+ ct_bad ct13-trailingdot '{"content_trigger_pattern":"x/","content_trigger_paths":["tests/."],"content_trigger_command":"bash scripts/ct-cmd.sh"}' 'bad-config key=content_trigger_paths'
2309
+ ct_bad ct13-hiddenonly '{"content_trigger_pattern":"x/","content_trigger_paths":[".h*"],"content_trigger_command":"bash scripts/ct-cmd.sh"}' 'bad-config key=content_trigger_paths'
2310
+ ct_bad ct13-dotted '{"content_trigger_pattern":"x/","content_trigger_paths":["x/*.z"],"content_trigger_command":"bash scripts/ct-cmd.sh"}' 'bad-config key=content_trigger_paths'
2311
+ ct_bad ct13-capq '{"content_trigger_pattern":"x/","content_trigger_paths":["Q*"],"content_trigger_command":"bash scripts/ct-cmd.sh"}' 'bad-config key=content_trigger_paths'
2312
+ ct_bad ct13-newlineglob '{"content_trigger_pattern":"x/","content_trigger_paths":["tests/*\nfoo"],"content_trigger_command":"bash scripts/ct-cmd.sh"}' 'bad-config key=content_trigger_paths'
2313
+
2314
+ # H1: the ledger is the discriminator. With the stub's ledger write removed the gate
2315
+ # still REPORTS the command ran — so a row asserting the ledger would be RED.
2316
+ R_H1="$(mk_ct_repo cth1 '["tests/*"]')"
2317
+ commit_chg "$R_H1" tests/new.test.sh "$CT_PAT"
2318
+ reset_fakes; export FAKE_CT_NOLEDGER=1; run_gate "$R_H1" gate
2319
+ want_out "H1: the marker still claims the run" 'content_trigger=ran$'
2320
+ if grep -q '^ctcmd ' "$CALLS"; then fail "H1: the ledger was written despite the stub write being removed"
2321
+ else pass "H1: no ledger line without the stub write — every ledger row would go RED"; fi
2322
+
2323
+ # H2: different pattern, globs and command still fire — nothing is hard-coded.
2324
+ R_H2="$(mk_ct_repo cth2 '["src/*"]' 'qq-other/' 'bash scripts/other-cmd.sh')"
2325
+ commit_chg "$R_H2" src/new.sh 'uses qq-other/ here'
2326
+ reset_fakes; run_gate "$R_H2" gate
2327
+ want_call "H2: a different pattern/glob/command fires" '^otherctcmd'
2328
+ want_no_call "H2: and the default command stays dark" '^ctcmd '
2329
+
2330
+ # H3: an UNARMED gate prints the pre-change marker line, byte for byte.
2331
+ R_H3="$(mk_repo cth3)"; write_runner_capable "$R_H3"; seal_base "$R_H3"
2332
+ commit_chg "$R_H3" tests/new.test.sh "$CT_PAT"
2333
+ reset_fakes; run_gate "$R_H3" gate
2334
+ want_marker "H3: unarmed marker is unchanged" "$CT_RUN"
2335
+ unset R_CTE1 R_A1 R_A4 R_A5 R_P1 R_P2 R_D1 R_I1 R_I2 R_U1 R_U2 R_U3 R_U4 R_X1 R_X1U R_X2 R_B1 R_R1 R_V1 R_W1 R_W2 R_W3 R_W4 R_W5 R_W6 R_W7 R_W8 R_W9 R_W10 R_W11 R_W12 R_W13 R_W14 R_W15 R_T12C R_T1 R_T2 R_T3 R_T4 R_T5 R_T6 R_T7 R_T8 R_T9 R_T10A R_T10B R_T10C R_T11 R_T11B R_T12 R_T12B R_T14 R_T15 R_T15B R_H1 R_H2 R_H3
2336
+
1760
2337
  # ============================ cross-run contract ===============================
1761
2338
  # G1-10: every run path emits the marker — a second exit site that fires before the
1762
2339
  # emit leaves a run with no AFFECTED_GATE line, and these loops catch it.
@@ -1792,6 +2369,8 @@ else
1792
2369
  fi
1793
2370
  unset _kind _rc _of _xfail _badword
1794
2371
 
1795
- print_results "${AFFECTED_TESTS_MIN_ASSERTIONS:-60}"
2372
+ # Floor = the full count less a little slack for the rows that
2373
+ # depend on a host binary (jq): it is a floor, so a deleted block of rows reddens it.
2374
+ print_results "${AFFECTED_TESTS_MIN_ASSERTIONS:-467}"
1796
2375
 
1797
2376
 
@@ -204,6 +204,12 @@ affected_timeout() {
204
204
  # only when a CI test workflow was detected.
205
205
  AFFECTED_BUDGET_ACTIVE=0
206
206
  AFFECTED_DEADLINE=0
207
+ # The content trigger (lib content-trigger.sh) is armed by the gate only after the
208
+ # whole three-key config group validates, and made eligible only at the exits that
209
+ # ran or reported a non-`--full` verdict. Same rule as above: initialised here,
210
+ # never inherited from the caller's environment.
211
+ AFFECTED_CT_ARMED=0
212
+ AFFECTED_CT_ELIGIBLE=0
207
213
  affected_bound() {
208
214
  if [[ "$AFFECTED_BUDGET_ACTIVE" == "1" ]]; then
209
215
  local rem
@@ -297,12 +303,23 @@ _affected_cmd_vacuous() { # <command string> → rc 0 when it certifies nothing
297
303
  # global-paths | full-requested |
298
304
  # custom-command | delegated | runner-refused | runner-signal | foreign-runner |
299
305
  # no-stack | bad-config key=<k> | vacuous-test-command | no-base-ref |
300
- # toolchain-missing | no-tests | budget-deferred | bad-usage | help
306
+ # toolchain-missing | no-tests | budget-deferred | bad-usage | help |
307
+ # content-trigger-failed | inert-content-trigger
301
308
  # Counts are decimal, or `-` when the path does not produce a count
302
309
  # (delegated runner, opaque test_command, refused runs).
303
310
  finish() {
304
311
  local rc="$1" mode="${2:--}" reason="${3:--}" stack="${4:--}" ci="${5:-none}"
305
- local ran="${6:--}" of="${7:--}" skipped="${8:--}"
312
+ local ran="${6:--}" of="${7:--}" skipped="${8:--}" ct_field=""
313
+ # Content trigger (content-trigger.sh): runs the configured command after a
314
+ # passing verdict whose diff adds the configured string, folds its result into
315
+ # rc/reason, and names the outcome in one trailing marker field. The hook never
316
+ # exits and never calls finish(); an unarmed gate skips this block byte-for-byte.
317
+ if [[ "${AFFECTED_CT_ARMED:-0}" == 1 ]]; then
318
+ affected_ct_hook "$rc" "$reason"
319
+ rc="$AFFECTED_CT_OUT_RC"; reason="$AFFECTED_CT_OUT_REASON"
320
+ if [[ "${AFFECTED_CT_OUT_RESET:-0}" == 1 ]]; then mode="selected"; ran="-"; of="-"; skipped="-"; fi
321
+ ct_field="$AFFECTED_CT_OUT_FIELD"
322
+ fi
306
323
  case "$rc" in
307
324
  0)
308
325
  if [[ "$reason" == inert ]]; then
@@ -360,6 +377,6 @@ finish() {
360
377
  echo "No test suite detected in this repository; the affected-suite gate had nothing to run."
361
378
  ;;
362
379
  esac
363
- echo "AFFECTED_GATE ran=$ran of=$of skipped=$skipped mode=$mode reason=$reason stack=$stack ci=$ci"
380
+ echo "AFFECTED_GATE ran=$ran of=$of skipped=$skipped mode=$mode reason=$reason stack=$stack ci=$ci${ct_field:+ content_trigger=$ct_field}"
364
381
  exit "$rc"
365
382
  }
@@ -0,0 +1,248 @@
1
+ #!/usr/bin/env bash
2
+ # content-trigger.sh — the gate's config-declared CONTENT trigger (#10924 P4b).
3
+ #
4
+ # Sourced by plugins/soleur/scripts/affected-tests.sh. Declarations only: no
5
+ # `exit`, no top-level side effects — the one exit site stays finish() in
6
+ # common.sh, which calls affected_ct_hook just before it prints the verdict.
7
+ #
8
+ # WHAT IT IS. A selector that matches diff PATHS cannot see a test fixture that
9
+ # merely NAMES a path (a `knowledge-base/` literal in a new test file): the path
10
+ # is a test-tree path, the dependency is in the text. The three flat keys in
11
+ # `.soleur-test-gate.json` —
12
+ # content_trigger_pattern fixed string (never a regex)
13
+ # content_trigger_paths globs the scan is limited to
14
+ # content_trigger_command what to run when the string is ADDED there
15
+ # — declare one such rule. When the diff adds a line (tracked files) or holds a
16
+ # line (untracked files) containing the pattern, in a file under the globs, a
17
+ # passing run that the operator did not make `--full` additionally runs the
18
+ # command. The plugin stays repo-agnostic: nothing here names a repository suite.
19
+ #
20
+ # FAIL DIRECTION. Anything that stops the scan from deciding (no base ref, a git
21
+ # failure, a path git C-quotes, an untracked tree too large to read) RUNS the
22
+ # command. A missed ratchet is a CI-only red; a spurious run is a few minutes.
23
+ #
24
+ # Variables are `_ct_`-prefixed locals; results travel through the AFFECTED_CT_*
25
+ # globals. The scan keeps its working data in variables, never in scratch files:
26
+ # nothing to register with affected_mktemp, nothing to leak on an early exit.
27
+
28
+ AFFECTED_CT_PATTERN=""
29
+ AFFECTED_CT_COMMAND=""
30
+ AFFECTED_CT_PATHS=()
31
+
32
+ # Scan results.
33
+ AFFECTED_CT_STATE=none # none | fired | scan-error
34
+ AFFECTED_CT_LEG="" # which leg could not decide (scan-error only)
35
+
36
+ # Hook results, read by finish().
37
+ AFFECTED_CT_OUT_RC=""
38
+ AFFECTED_CT_OUT_REASON=""
39
+ AFFECTED_CT_OUT_RESET=0 # 1 => the command ran on an inert diff: finish() replaces mode and counts
40
+ AFFECTED_CT_OUT_FIELD=""
41
+
42
+ # Untracked-leg bounds: past either the verdict is "could not decide" (command runs).
43
+ # The first bounds the listing the glob loop walks, the second the per-file reads.
44
+ AFFECTED_CT_MAX_LISTED=20000
45
+ AFFECTED_CT_MAX_READ=500
46
+
47
+ # _affected_ct_in_globs <path> — rc 0 when the path matches a configured glob.
48
+ _affected_ct_in_globs() {
49
+ local _ct_p="$1" _ct_g
50
+ for _ct_g in ${AFFECTED_CT_PATHS[@]+"${AFFECTED_CT_PATHS[@]}"}; do
51
+ affected_glob_match "$_ct_p" "$_ct_g" && return 0
52
+ done
53
+ return 1
54
+ }
55
+
56
+ # affected_ct_scan — sets AFFECTED_CT_STATE / _LEG; returns at the first hit.
57
+ #
58
+ # base: --base, else origin/main, else origin/master — never @{upstream}
59
+ # (on a pushed feature branch it hides every pushed fixture);
60
+ # then `git merge-base <base> HEAD`.
61
+ # tracked: ONE `git diff -U0 --text` of the working tree against the merge-base
62
+ # (committed + staged + unstaged), parsed by an awk state machine so
63
+ # a header, a path or a removal line can never count as added text.
64
+ # `--text` makes git diff files marked `-diff`/`binary` or holding NUL.
65
+ # untracked: `git ls-files --others --exclude-standard`, limited to the globs,
66
+ # regular files only (a symlink is not read), each read with
67
+ # `grep -aqF` (as text, like the tracked leg's `--text`; under `-q` GNU grep
68
+ # exits 0 on a binary match either way, so `-a` is belt-and-braces for
69
+ # greps that would otherwise answer `Binary file matches`).
70
+ affected_ct_scan() {
71
+ AFFECTED_CT_STATE=none
72
+ AFFECTED_CT_LEG=""
73
+ local _ct_base="" _ct_mb="" _ct_b _ct_rc=0 _ct_p _ct_hits _ct_untr _ct_listed=0 _ct_read=0
74
+
75
+ if ! declare -F affected_glob_match >/dev/null 2>&1; then
76
+ AFFECTED_CT_STATE=scan-error; AFFECTED_CT_LEG="glob-matcher"; return 0
77
+ fi
78
+
79
+ if [[ -n "${ARG_BASE:-}" ]]; then
80
+ # A ref the operator named, handed to git as an operand: never an option.
81
+ if [[ "$ARG_BASE" == -* ]]; then
82
+ AFFECTED_CT_STATE=scan-error; AFFECTED_CT_LEG="bad-base"; return 0
83
+ fi
84
+ _ct_base="$ARG_BASE"
85
+ else
86
+ for _ct_b in origin/main origin/master; do
87
+ if git rev-parse --verify --quiet "$_ct_b" >/dev/null 2>&1; then
88
+ _ct_base="$_ct_b"
89
+ break
90
+ fi
91
+ done
92
+ fi
93
+ if [[ -z "$_ct_base" ]]; then
94
+ AFFECTED_CT_STATE=scan-error; AFFECTED_CT_LEG="no-base"; return 0
95
+ fi
96
+ _ct_mb="$(git merge-base "$_ct_base" HEAD 2>/dev/null)" || _ct_rc=$?
97
+ if (( _ct_rc != 0 )) || [[ -z "$_ct_mb" ]]; then
98
+ AFFECTED_CT_STATE=scan-error; AFFECTED_CT_LEG="no-merge-base"; return 0
99
+ fi
100
+
101
+ # --- tracked leg ---------------------------------------------------------
102
+ # The diff is piped straight into awk (which reads to EOF, so the producer can
103
+ # never take a SIGPIPE) and only the matching PATHS are kept, in a variable:
104
+ # no scratch file, so nothing to leak and no redirect target to prove absolute.
105
+ # pipefail (set by the gate) turns a failing `git diff` into a non-zero status.
106
+ #
107
+ # The pattern travels in the environment: `awk -v` processes backslash
108
+ # escapes, ENVIRON does not. State resets on every `diff --git`; the path comes
109
+ # from the `+++ b/<path>` header (git appends a TAB when it contains a space);
110
+ # nothing counts until the first `@@` of that file, so a header or path that
111
+ # NAMES the pattern, or a removal line, can never fire. A header git C-quotes
112
+ # (`+++ "b/..."`: a name holding a quote, backslash or control byte) cannot be
113
+ # decoded here, so a quoted-name file that adds the pattern reports `?quoted`
114
+ # and the scan declines to decide rather than dropping it.
115
+ _ct_rc=0
116
+ # `--no-renames`: a renamed file is then a delete plus an add, so moving a file that
117
+ # already names the string INTO the globs (or editing it while moving) fires, as an
118
+ # added file does; with rename detection git shows only the delta. `tr -d` keeps a
119
+ # NUL (which `--text` can emit) from truncating a line in awks that stop at one.
120
+ _ct_hits="$(git -c core.quotePath=false diff -U0 --text --no-color --no-ext-diff --no-textconv --no-renames \
121
+ --src-prefix=a/ --dst-prefix=b/ "$_ct_mb" -- 2>/dev/null \
122
+ | LC_ALL=C tr -d '\000' \
123
+ | CT_PAT="$AFFECTED_CT_PATTERN" awk '
124
+ BEGIN { pat = ENVIRON["CT_PAT"]; path = ""; inh = 0; seen = 0 }
125
+ /^diff --git / { path = ""; inh = 0; seen = 0; next }
126
+ inh == 0 && /^\+\+\+ / {
127
+ p = substr($0, 5)
128
+ sub(/\t$/, "", p)
129
+ if (substr(p, 1, 1) == "\"") path = "?quoted"
130
+ else if (substr(p, 1, 2) == "b/") path = substr(p, 3)
131
+ else path = ""
132
+ next
133
+ }
134
+ inh == 0 && /^@@ / { inh = 1; next }
135
+ inh == 1 && substr($0, 1, 1) == "+" && seen == 0 && path != "" {
136
+ if (index(substr($0, 2), pat) > 0) { print path; seen = 1 }
137
+ }
138
+ ')" || _ct_rc=$?
139
+ if (( _ct_rc != 0 )); then
140
+ AFFECTED_CT_STATE=scan-error; AFFECTED_CT_LEG="diff"; return 0
141
+ fi
142
+ while IFS= read -r _ct_p; do
143
+ [[ -n "$_ct_p" ]] || continue
144
+ if [[ "$_ct_p" == "?quoted" ]]; then
145
+ AFFECTED_CT_STATE=scan-error; AFFECTED_CT_LEG="quoted-path"; return 0
146
+ fi
147
+ if _affected_ct_in_globs "$_ct_p"; then AFFECTED_CT_STATE=fired; return 0; fi
148
+ done <<<"$_ct_hits"
149
+
150
+ # --- untracked leg -------------------------------------------------------
151
+ _ct_rc=0
152
+ _ct_untr="$(git -c core.quotePath=false ls-files --others --exclude-standard 2>/dev/null)" || _ct_rc=$?
153
+ if (( _ct_rc != 0 )); then
154
+ AFFECTED_CT_STATE=scan-error; AFFECTED_CT_LEG="ls-files"; return 0
155
+ fi
156
+ while IFS= read -r _ct_p; do
157
+ [[ -n "$_ct_p" ]] || continue
158
+ _ct_listed=$((_ct_listed + 1))
159
+ if (( _ct_listed > AFFECTED_CT_MAX_LISTED )); then
160
+ AFFECTED_CT_STATE=scan-error; AFFECTED_CT_LEG="untracked-cap"; return 0
161
+ fi
162
+ if [[ "$_ct_p" == \"* ]]; then
163
+ AFFECTED_CT_STATE=scan-error; AFFECTED_CT_LEG="quoted-path"; return 0
164
+ fi
165
+ [[ -f "$_ct_p" && ! -L "$_ct_p" ]] || continue
166
+ _affected_ct_in_globs "$_ct_p" || continue
167
+ _ct_read=$((_ct_read + 1))
168
+ if (( _ct_read > AFFECTED_CT_MAX_READ )); then
169
+ AFFECTED_CT_STATE=scan-error; AFFECTED_CT_LEG="untracked-cap"; return 0
170
+ fi
171
+ _ct_rc=0
172
+ grep -aqF -- "$AFFECTED_CT_PATTERN" "$_ct_p" 2>/dev/null || _ct_rc=$?
173
+ case "$_ct_rc" in
174
+ 0) AFFECTED_CT_STATE=fired; return 0 ;;
175
+ 1) : ;;
176
+ *) AFFECTED_CT_STATE=scan-error; AFFECTED_CT_LEG="grep"; return 0 ;;
177
+ esac
178
+ done <<<"$_ct_untr"
179
+ return 0
180
+ }
181
+
182
+ # affected_ct_hook <rc> <reason> — called by finish() (armed only) on its way out.
183
+ # Sets AFFECTED_CT_OUT_*; never exits and never calls finish().
184
+ affected_ct_hook() {
185
+ local _ct_rc="$1" _ct_reason="$2" _ct_crc=0
186
+ AFFECTED_CT_OUT_RC="$_ct_rc"
187
+ AFFECTED_CT_OUT_REASON="$_ct_reason"
188
+ AFFECTED_CT_OUT_RESET=0
189
+ AFFECTED_CT_OUT_FIELD=""
190
+
191
+ # Only the exits that set the flag are decided here. An operator's --full,
192
+ # test_command, --self-check, help, refusals and no-test exits never reach it.
193
+ [[ "${AFFECTED_CT_ELIGIBLE:-0}" == 1 ]] || return 0
194
+
195
+ # --explain: report what the scan sees, run nothing, add no marker field.
196
+ if [[ "${ARG_EXPLAIN:-0}" == 1 ]]; then
197
+ affected_ct_scan
198
+ echo "CONTENT_TRIGGER armed=1 state=${AFFECTED_CT_STATE} paths=${#AFFECTED_CT_PATHS[@]}"
199
+ return 0
200
+ fi
201
+
202
+ # A primary run that did not pass owns the verdict; the command adds nothing.
203
+ if (( _ct_rc != 0 )); then
204
+ AFFECTED_CT_OUT_FIELD=skipped
205
+ return 0
206
+ fi
207
+
208
+ affected_ct_scan
209
+ case "$AFFECTED_CT_STATE" in
210
+ none)
211
+ AFFECTED_CT_OUT_FIELD=clear
212
+ return 0 ;;
213
+ scan-error)
214
+ AFFECTED_CT_OUT_FIELD=scan-error
215
+ echo "affected-tests: the content-trigger scan could not decide (leg=$(affected_marker_field "$AFFECTED_CT_LEG")) — running the declared command." ;;
216
+ *)
217
+ AFFECTED_CT_OUT_FIELD=ran
218
+ echo "affected-tests: content trigger fired (the configured string is added under the configured paths) — running the declared command." ;;
219
+ esac
220
+
221
+ # The command's result is a gate result: it runs under the same remaining
222
+ # budget as the primary run, and its failure is the gate's failure.
223
+ affected_bound bash -c "$AFFECTED_CT_COMMAND" || _ct_crc=$?
224
+ if (( _ct_crc != 0 )); then
225
+ (( _ct_crc == 137 )) && echo "affected-tests: the content-trigger command exited 137 (SIGKILL — OOM or kill escalation), not necessarily budget expiry." >&2
226
+ if [[ ( "$_ct_crc" == 124 || "$_ct_crc" == 137 ) && "$AFFECTED_BUDGET_ACTIVE" == 1 && "${CI_NAME:-none}" != none ]]; then
227
+ AFFECTED_CT_OUT_RC=6
228
+ AFFECTED_CT_OUT_REASON=budget-deferred
229
+ AFFECTED_CT_OUT_FIELD=deferred
230
+ echo "affected-tests: the content-trigger command did not finish within the shared budget — the selected tests passed; only the command is owed to CI."
231
+ else
232
+ AFFECTED_CT_OUT_RC=1
233
+ AFFECTED_CT_OUT_REASON=content-trigger-failed
234
+ AFFECTED_CT_OUT_FIELD=failed
235
+ echo "CONTENT_TRIGGER_FAILED rc=$_ct_crc"
236
+ echo " remedy: run the content_trigger_command from .soleur-test-gate.json directly (bash -c '<command>'), fix it, re-run the gate"
237
+ fi
238
+ fi
239
+
240
+ # An inert diff that nonetheless ran the command is no longer "docs-only":
241
+ # keep the pass line honest, and keep changed-file counts (the inert path's
242
+ # unit) out of a marker that describes a command run.
243
+ if [[ "$_ct_reason" == inert ]]; then
244
+ AFFECTED_CT_OUT_RESET=1
245
+ [[ "$AFFECTED_CT_OUT_RC" == 0 ]] && AFFECTED_CT_OUT_REASON=inert-content-trigger
246
+ fi
247
+ return 0
248
+ }
@@ -52,9 +52,9 @@ gate probes registration, not presence.
52
52
  | rc | Meaning | Skill action |
53
53
  |----|---------|--------------|
54
54
  | 0 | Selected tests passed, or `NO_TESTS_AFFECTED` (all-inert diff) | proceed |
55
- | 1 | A selected test failed / runner signal-killed | stop; report failures |
55
+ | 1 | A selected test failed / runner signal-killed / the content-trigger command failed (`reason=content-trigger-failed`) | stop; report failures (for `content-trigger-failed`, re-run the `content_trigger_command` directly) |
56
56
  | 4 | `AFFECTED_GATE_REFUSED reason=<no-stack\|bad-config\|vacuous-test-command\|no-base-ref\|not-a-repo\|toolchain-missing\|runner-refused>` + a `remedy:` line | stop; fix the named cause — never retry blindly |
57
- | 6 | `AFFECTED_GATE_BUDGET_DEFERRED` — budget expired AND a CI test workflow exists | proceed to CI; record CI as the authoritative gate; never label the run passed |
57
+ | 6 | `AFFECTED_GATE_BUDGET_DEFERRED` — budget expired AND a CI test workflow exists (with `content_trigger=deferred` the selected tests passed and only the content-trigger command is owed to CI) | proceed to CI; record CI as the authoritative gate; never label the run passed |
58
58
  | 7 | No test suite detected at all | proceed with the disclosed note; never label passed |
59
59
 
60
60
  Multi-stack aggregation: worst-of `1 > 4 > 6 > 0`; `7` only when every adapter
@@ -70,7 +70,9 @@ runner `3` → rc 1 `reason=runner-signal`.
70
70
  `affected-suite gate passed.`).
71
71
  - Machine line: `AFFECTED_GATE ran=X of=N skipped=Y mode=<selected|full|custom|explain|none|help> reason=… stack=… ci=<file|none>`
72
72
  on every verdict path, including refusals and non-run paths (`explain`,
73
- `help`, `self-check`) — counts are `-` where no honest count exists.
73
+ `help`, `self-check`) — counts are `-` where no honest count exists. An armed
74
+ content trigger appends one trailing field `content_trigger=<state>` (see
75
+ [Content trigger](#content-trigger)); parse the line by field name.
74
76
  - `NO_TESTS_AFFECTED` + `No tests ran: docs-only change.` for an all-inert diff
75
77
  (adapter arm only — a capability-positive runner decides inertness itself).
76
78
  - `--explain` prints the selection and per-path reasons without running; under
@@ -100,6 +102,61 @@ runner `3` → rc 1 `reason=runner-signal`.
100
102
  | `budget_seconds` | number | Wall-clock cap; enforced only when CI detected; default 900; must be a positive integer |
101
103
  | `max_workers` | number | Overrides `max(1, ncpu/2) / (1 + running gate processes)`; positive integer |
102
104
  | `budget_ci` | boolean | Forces CI-deferral eligibility |
105
+ | `content_trigger_pattern`, `content_trigger_paths`, `content_trigger_command` | string, string[], string | The content trigger below. All three or none: a partial or empty group, a multi-line pattern, an empty, whitespace-bearing or match-everything glob (`*`, `**/*`, `[!p]*`: any glob matching one of a few improbable probe paths), a glob that can never match a repo-relative path (leading `./`, `/` or `!`; trailing `/`; `//`; a `.`/`..` segment; `{a,b}`, which `[[ == ]]` does not expand), or an empty or vacuous command (same denylist as `test_command`) is `bad-config key=<the offending key>` |
106
+
107
+ ## Content trigger
108
+
109
+ Selection by path cannot see a test file that merely NAMES a path (a fixture whose text
110
+ contains a repo directory). The trigger covers that class from config, so the plugin stays
111
+ repo-agnostic: when the diff **adds** a line (tracked files) or any file **holds** a line
112
+ (untracked files) containing the fixed string `content_trigger_pattern`, in a file matching
113
+ one of `content_trigger_paths`, a passing run ALSO runs `content_trigger_command` (via
114
+ `bash -c` from the repo root with the gate's environment — the same trust level as
115
+ `test_command`; a PR can edit the config and the code together, so CI stays the independent
116
+ gate). Its exit status is the only failure signal.
117
+
118
+ - **Order and scope.** It runs after the primary run, on any passing verdict the operator
119
+ did not make `--full`: delegated, adapter, an inert diff, and a gate-side escalation to
120
+ full (global paths, empty diff, degraded census, no selector — the stack adapter's full
121
+ run never runs the repo-declared command). An operator's own `--full`, `test_command`,
122
+ `--self-check`, help, refusals, and `rc 7` (no test suite) never run it, and neither does
123
+ `--explain` (it only reports; under an operator's `--full` it prints nothing). On a repo with no
124
+ runner an operator's `--full` is the stack's full command only: it does not cover the trigger. A failed primary (marker field `skipped`) owns the verdict.
125
+ The command runs under the same remaining wall-clock budget: with a detected CI workflow
126
+ an expiry is rc 6 (field `deferred`), else rc 1.
127
+ - **What counts.** Added lines only, taken from one `git diff -U0 --text` of the working tree
128
+ against `merge-base(<base>, HEAD)` (renames are a delete plus an add, so moving a file that
129
+ already names the string into the globs fires) plus untracked files (read as text; symlinks
130
+ skipped). The
131
+ base is `--base`, else `origin/main`, else `origin/master` — never the upstream ref, which on
132
+ a pushed branch would hide every pushed fixture. A path or removal line that names the
133
+ string never fires. The string is matched as a
134
+ fixed string, not a regex. Globs: a pattern with a `/` matches the whole path (and `*`
135
+ crosses `/`, `**/` also matches zero directories); a bare name matches the basename.
136
+ - **Fail direction.** A scan that cannot decide runs the command and says so
137
+ (`content_trigger=scan-error`, with `leg=` on stdout): no base ref, a failing `git`, a path
138
+ git C-quotes (a name holding a quote, backslash or control byte), a `--base` that looks
139
+ like an option, or an untracked tree past 20000 listed paths or 500 candidate files.
140
+ A miss is a CI-only red, a spurious run is minutes.
141
+ - **Marker.** One trailing field on the `AFFECTED_GATE` line, only when armed and only on an
142
+ eligible exit: `content_trigger=<clear|ran|failed|deferred|skipped|scan-error>`. `failed` is
143
+ the command exiting non-zero (rc 1 `reason=content-trigger-failed`, with a
144
+ `CONTENT_TRIGGER_FAILED rc=N` line and a remedy); `deferred` is the command hitting the
145
+ shared budget with CI authoritative (rc 6); `skipped` is a primary that did not pass. An
146
+ inert diff that ran it reports `mode=selected` with `-` counts and `reason=inert-content-trigger`
147
+ on a pass (no "docs-only" claim). An unarmed gate's line is unchanged.
148
+ - **`--explain`** prints `CONTENT_TRIGGER armed=1 state=<none|fired|scan-error> paths=<n>`
149
+ and runs nothing (the `test_command` explain arm prints nothing: `test_command`
150
+ bypasses selection, so an armed trigger beside it is inert).
151
+ - **Double run.** When the primary run already selected the same suite, it runs twice; this
152
+ is accepted (about a minute and a half for this repo's ratchet) rather than deduplicated.
153
+ - **Choosing `content_trigger_paths`.** Keep the globs to trees where a fixture legitimately
154
+ names the string; the trigger rate over recent history should stay small (this repo's
155
+ set was held to about 10 percent of first-parent commits). Re-measure when the globs
156
+ change: count commits whose diff adds the string to a file under the globs.
157
+ - **Known limits.** A tracked file that git filters (clean/smudge, LFS pointers) is diffed
158
+ after the filter; a file in a nested repository is not read; a command that swallows its
159
+ own exit status (`cmd | cat`, a trailing `&`) is outside the vacuity denylist.
103
160
 
104
161
  ## Worked examples
105
162