@soleur/opencode 3.387.9 → 3.388.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.
@@ -0,0 +1,747 @@
1
+ #!/usr/bin/env bash
2
+ # host-state.test.sh - behavioural suite for skills/cve-exposure/scripts/host-state.sh (Guard 1, #12006).
3
+ #
4
+ # A scratch git tree holds a COPY of the helper and a stub scripts/betterstack-query.sh that serves fixture
5
+ # files, so the real reader is never reached and the helper's environment cannot redirect it (ADR-214).
6
+ # Fixtures are synthesized (cq-test-fixtures-synthesized-only): kernel strings such as 9.9.9-1-generic.
7
+ # Final line: `host-state: PASS` or `host-state: FAIL`.
8
+ #
9
+ # Set HOST_STATE_SUT_OVERRIDE to a path to run the suite against a mutated COPY of the helper (the
10
+ # mutation protocol in the plan's Guard 1 matrix uses this; the helper itself has no such seam).
11
+ set -uo pipefail
12
+ export LC_ALL=C TZ=UTC
13
+
14
+ IMG_MARKER='SOLEUR_IMAGE_CVE''_SCAN' # split: see the note on IMG_MARKER in host-state.sh
15
+ HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
16
+ SUT_SRC="${HOST_STATE_SUT_OVERRIDE:-$HERE/../scripts/host-state.sh}"
17
+ REPO_TOP="$(cd "$HERE/../../../../.." && pwd)"
18
+
19
+ PASSES=0; FAILS=0
20
+ ok() { PASSES=$((PASSES + 1)); }
21
+ QUIET=0
22
+ bad() { FAILS=$((FAILS + 1)); [ "$QUIET" -eq 1 ] || printf 'FAIL: %s\n' "$1" >&2; }
23
+ check() { # check <label> <condition-result 0|1>
24
+ if [ "$2" -eq 0 ]; then ok; else bad "$1"; fi
25
+ }
26
+ eq() { if [ "$2" = "$3" ]; then ok; else bad "$1 (want [$3] got [$2])"; fi; }
27
+ has() { if grep -qF -- "$3" <<< "$2"; then ok; else bad "$1 (missing [$3])"; fi; }
28
+
29
+ # --- instrument self-test: every comparison helper must be able to FAIL, and its failure must move the counter.
30
+ # Driving each helper once with a comparison that must fail catches a helper rewritten to always say ok (the
31
+ # floor alone cannot: it counts ok() calls). Counters are unwound so the real totals stay exact.
32
+ SP0=$PASSES; SF0=$FAILS
33
+ QUIET=1
34
+ ok; bad "selftest-bad"
35
+ eq "selftest-eq" "a" "b"
36
+ has "selftest-has" "abc" "zzz"
37
+ check "selftest-check" 1
38
+ check "selftest-check-ok" 0
39
+ QUIET=0
40
+ if [ "$PASSES" -ne $((SP0 + 2)) ] || [ "$FAILS" -ne $((SF0 + 4)) ]; then
41
+ printf 'host-state: FAIL (instrument self-test: ok/bad/eq/has/check did not each move their counter)\n'; exit 1
42
+ fi
43
+ FAILS=$SF0
44
+
45
+ [ -f "$SUT_SRC" ] || { printf 'host-state: FAIL (helper missing: %s)\n' "$SUT_SRC"; exit 1; }
46
+ command -v jq > /dev/null || { printf 'host-state: FAIL (jq required)\n'; exit 1; }
47
+
48
+ WORK="$(mktemp -d -t soleur-cve-hs-test.XXXXXXXX)"
49
+ trap 'rm -rf "$WORK"' EXIT
50
+ TREE="$WORK/tree"
51
+ mkdir -p "$TREE/scripts/lib" "$TREE/fx" "$TREE/h" "$WORK/shim" "$WORK/up/scripts/lib"
52
+ git -C "$TREE" init -q . 2> /dev/null
53
+ cp "$SUT_SRC" "$TREE/h/host-state.sh"
54
+ cp "$REPO_TOP/scripts/lib/betterstack-read-classify.sh" "$TREE/scripts/lib/" 2> /dev/null || true
55
+
56
+ # Stub reader: serves fx/<name>.rows chosen by what the SQL contains, records the SQL, honours fx/<name>.rc.
57
+ cat > "$TREE/scripts/betterstack-query.sh" <<'STUB'
58
+ #!/usr/bin/env bash
59
+ FX="${STUB_FX:?stub: STUB_FX not set}"
60
+ printf '%s\n' "$0" >> "$FX/ran"
61
+ sql="${1:-}"
62
+ case "$sql" in
63
+ *"GROUP BY h"*) name=control ;;
64
+ *"LIMIT 6 BY k"*) name=marker ;;
65
+ *"SOLEUR_IMAGE_CVE"*) name=image ;;
66
+ *) echo "stub: unexpected SQL" >&2; exit 64 ;;
67
+ esac
68
+ printf '%s\n' "$sql" >> "$FX/$name.sql"
69
+ [ -f "$FX/$name.rc" ] && rc="$(cat "$FX/$name.rc")" || rc=0
70
+ [ -f "$FX/$name.rows" ] && cat "$FX/$name.rows"
71
+ exit "$rc"
72
+ STUB
73
+ chmod +x "$TREE/scripts/betterstack-query.sh"
74
+
75
+ # Upstream fixture + curl shim. The helper downloads each reader file from upstream main over HTTPS and
76
+ # compares it with the checkout's copy; the shim serves "upstream" from $WORK/up (and can go offline).
77
+ cp "$TREE/scripts/betterstack-query.sh" "$WORK/up/scripts/betterstack-query.sh"
78
+ cp "$TREE/scripts/lib/betterstack-read-classify.sh" "$WORK/up/scripts/lib/" 2> /dev/null || printf '# lib\n' > "$WORK/up/scripts/lib/betterstack-read-classify.sh"
79
+ cp "$WORK/up/scripts/lib/betterstack-read-classify.sh" "$TREE/scripts/lib/betterstack-read-classify.sh"
80
+ cat > "$WORK/shim/curl" <<'SHIM'
81
+ #!/usr/bin/env bash
82
+ UP="$(cd "$(dirname "$0")/../up" && pwd)"
83
+ # The helper's hardening is part of the contract: refuse (rc 64) unless the argv starts with exactly the
84
+ # no-rc-file / no-proxy / https-only / TLS1.2 flags, and (rc 35) when a poisoned curl environment survived.
85
+ [ "${1:-}" = "--disable" ] && [ "${2:-}" = "--noproxy" ] && [ "${3:-}" = "*" ] && [ "${4:-}" = "--proto" ] \
86
+ && [ "${5:-}" = "=https" ] && [ "${6:-}" = "--tlsv1.2" ] && [ "${7:-}" = "-fsS" ] || { printf 'BAD-FLAGS %s\n' "$*" >> "$UP/curl.argv"; exit 64; }
87
+ if [ -n "${CURL_CA_BUNDLE:-}${SSL_CERT_FILE:-}${CURL_HOME:-}${SSLKEYLOGFILE:-}" ]; then printf 'POISONED-ENV\n' >> "$UP/curl.argv"; exit 35; fi
88
+ printf '%s\n' "$*" >> "$UP/curl.argv"
89
+ url=""; out=""
90
+ while [ "$#" -gt 0 ]; do
91
+ case "$1" in -o) out="$2"; shift 2 ;; https://*) url="$1"; shift ;; *) shift ;; esac
92
+ done
93
+ printf '%s\n' "$url" >> "$UP/curl.calls"
94
+ [ -f "$UP/offline" ] && exit 7
95
+ rel="${url#https://raw.githubusercontent.com/jikig-ai/soleur/main/}"
96
+ [ "$rel" != "$url" ] || exit 22
97
+ [ -f "$UP/$rel" ] || exit 22
98
+ cp "$UP/$rel" "$out"
99
+ SHIM
100
+ chmod +x "$WORK/shim/curl"
101
+ PATH_SHIM="$WORK/shim:$PATH"
102
+ restore_reader() { cp "$WORK/up/scripts/betterstack-query.sh" "$TREE/scripts/betterstack-query.sh"; cp "$WORK/up/scripts/lib/betterstack-read-classify.sh" "$TREE/scripts/lib/betterstack-read-classify.sh"; rm -f "$WORK/up/offline" "$WORK/up/curl.calls" "$WORK/up/curl.argv"; }
103
+
104
+ reset_fx() { rm -f "$TREE"/fx/*; }
105
+ ago() { date -u -d "-$1 hours" '+%Y-%m-%d %H:%M:%S.000000'; } # $1 = hours ago
106
+ agom() { date -u -d "$1 minutes ago" '+%Y-%m-%d %H:%M:%S.000000'; } # $1 = minutes ago
107
+ agof() { date -u -d "+$1 minutes" '+%Y-%m-%d %H:%M:%S.000000'; } # $1 = minutes AHEAD (clock skew)
108
+ rowm() { jq -nc --arg dt "$(agom "$1")" --arg raw "$2" '{dt: $dt, raw: $raw, k: "x"}'; }
109
+ row() { # row <hours-ago> <decoded-object-json> -> one reader row (raw is double encoded)
110
+ jq -nc --arg dt "$(ago "$1")" --arg raw "$2" '{dt: $dt, raw: $raw, k: "x"}'
111
+ }
112
+ obj() { jq -nc --arg si "$1" --arg hn "$2" --arg m "$3" '{SYSLOG_IDENTIFIER: $si, host_name: $hn, message: $m}'; }
113
+ ctl() { jq -nc --arg h "$1" --argjson n "$2" '{h: $h, n: $n}'; }
114
+ put() { cat > "$TREE/fx/$1.rows"; }
115
+
116
+ RUN_OUT=""; RUN_RC=0; RUN_ERR=""
117
+ run_hs() { # run_hs [args] ; runs from inside the scratch tree, captures stdout/rc/stderr
118
+ RUN_RC=0
119
+ RUN_OUT="$(cd "$TREE" && STUB_FX="$TREE/fx" PATH="$PATH_SHIM" CURL_CA_BUNDLE=/nonexistent/ca SSL_CERT_FILE=/nonexistent/ca CURL_HOME=/nonexistent SSLKEYLOGFILE=/nonexistent/k https_proxy=http://127.0.0.1:9 bash "$TREE/h/host-state.sh" "$@" 2> "$WORK/err")" || RUN_RC=$?
120
+ RUN_ERR="$(cat "$WORK/err")"
121
+ if [ -n "${HS_DEBUG:-}" ]; then printf '%s\n--- stdout:\n%s\n--- rc=%s\n' "$RUN_ERR" "$RUN_OUT" "$RUN_RC" >&2; fi
122
+ }
123
+ line() { grep -m1 -E "^$1"$'\t' <<< "$RUN_OUT"; }
124
+ col() { # col <role> <n>
125
+ awk -F'\t' -v r="$1" -v n="$2" '$1 == r { print $n; exit }' <<< "$RUN_OUT"
126
+ }
127
+ nlines() { grep -c -E "^(web-1|web-2|git-data|inngest|registry|grok-dogfood)"$'\t' <<< "$RUN_OUT"; }
128
+
129
+ WEBHOSTS=(soleur-web-platform soleur-web-2 soleur-inngest-prd soleur-grok-dogfood)
130
+ seed_control() { { for h in "${WEBHOSTS[@]}"; do ctl "$h" 40; done; } | put control; }
131
+ probe_web() { row "$1" "$(obj inngest-server-probe soleur-web-platform "SOLEUR_INNGEST_SERVER_PROBE http_code=200 server_active=active uptime_s=${2:-1234} boot_id=b host_role=web")"; }
132
+ probe_inn() { row "$1" "$(obj inngest-server-probe soleur-inngest-prd "SOLEUR_INNGEST_SERVER_PROBE http_code=200 server_active=active uptime_s=${2:-777} boot_id=b host_role=dedicated")"; }
133
+ canary() { row "$1" "$(obj ci-deploy "$2" "SOLEUR_CANARY_SANDBOX_DIAG: image=x:y trigger=legacy section=kernel val=\"kernel=$3 docker=24.0.0\"")"; }
134
+ nic() { row "$1" "$(obj "" "" "SOLEUR_PRIVATE_NIC nic_ok=true uptime_s=${2:-5555} boot_id=b")"; }
135
+ zot() { row "$1" "$(obj "" "" "SOLEUR_ZOT_DISK pcent=17 uptime_s_unused=1 boot_id=b host=${2:-soleur-registry}")"; }
136
+
137
+ # ===========================================================================================================
138
+ # Baseline: a healthy mixed fixture. fresh web-1 (probe) + stale-evidence kernel, inngest fresh, registry fresh.
139
+ # ===========================================================================================================
140
+ reset_fx; seed_control
141
+ { probe_web 2 20352602; probe_inn 1 119256; nic 0 1029794; zot 0; canary 30 soleur-web-2 9.9.9-1-generic; } | put marker
142
+ run_hs
143
+ eq "baseline exit 0" "$RUN_RC" 0
144
+ eq "baseline emits one line per role" "$(nlines)" 6
145
+ eq "web-1 marker_state fresh (probe row)" "$(col web-1 2)" fresh
146
+ eq "web-1 uptime from the probe row" "$(col web-1 6)" 20352602
147
+ eq "web-1 kernel unknown (no kernel diag row for it)" "$(col web-1 3)" unknown
148
+ eq "web-2 marker_state stale (failed-deploy diag only)" "$(col web-2 2)" stale
149
+ eq "web-2 kernel_state observed" "$(col web-2 3)" observed
150
+ eq "web-2 kernel value" "$(col web-2 5)" 9.9.9-1-generic
151
+ eq "web-2 kernel_age reported separately" "$(col web-2 7)" 30
152
+ eq "web-2 never fresh from a diag row" "$(col web-2 2)" stale
153
+ eq "inngest fresh with uptime" "$(col inngest 6)" 119256
154
+ eq "inngest kernel unknown (uptime is not a kernel)" "$(col inngest 3)" unknown
155
+ eq "registry fresh via nic/zot rows" "$(col registry 2)" fresh
156
+ eq "registry uptime from nic" "$(col registry 6)" 1029794
157
+ eq "git-data unknown with source-not-read" "$(col git-data 8)" source-not-read
158
+ eq "git-data marker_state unknown" "$(col git-data 2)" unknown
159
+ eq "grok-dogfood silent (ships logs, no marker)" "$(col grok-dogfood 2)" silent
160
+ has "verdict line counts all six" "$RUN_OUT" "SOLEUR_CVE_HOST_STATE hosts=6 fresh=3 stale=1 silent=1 unknown=1"
161
+
162
+ # G1-4: the SQL the helper sends has BOTH arms, the row-type filter on the archive arm, and the 7-day window.
163
+ SQLM="$(cat "$TREE/fx/marker.sql" 2> /dev/null)"
164
+ has "marker SQL has the hot arm" "$SQLM" 'remote($BS_TABLE)'
165
+ has "marker SQL has the archive arm" "$SQLM" 's3Cluster(primary, $BS_TABLE_S3) WHERE _row_type = 1'
166
+ has "marker SQL windows to 7 days" "$SQLM" 'INTERVAL 7 DAY'
167
+ SQLC="$(cat "$TREE/fx/control.sql" 2> /dev/null)"
168
+ has "control SQL has the archive arm too" "$SQLC" 's3Cluster(primary, $BS_TABLE_S3) WHERE _row_type = 1'
169
+
170
+ # ===========================================================================================================
171
+ # G1-1: the reader's exit code outranks its stdout.
172
+ # ===========================================================================================================
173
+ for rc in 3 2; do
174
+ reset_fx; seed_control; { probe_web 1; probe_inn 1; nic 0; } | put marker
175
+ # the control read is the first call; fail it while printing fresh rows
176
+ printf '%s\n' "$rc" > "$TREE/fx/control.rc"
177
+ { for h in "${WEBHOSTS[@]}"; do ctl "$h" 40; done; } | put control
178
+ run_hs
179
+ case "$rc" in 3) want=3 ;; *) want=4 ;; esac
180
+ eq "reader rc=$rc exits $want" "$RUN_RC" "$want"
181
+ for r in web-1 web-2 inngest registry grok-dogfood git-data; do
182
+ eq "reader rc=$rc: $r is unknown" "$(col "$r" 2)" unknown
183
+ done
184
+ has "reader rc=$rc: verdict shows zero fresh" "$RUN_OUT" "fresh=0"
185
+ done
186
+ reset_fx; seed_control; { probe_web 1; } | put marker; printf '3\n' > "$TREE/fx/marker.rc"
187
+ run_hs
188
+ eq "credentials-absent on the marker read exits 3" "$RUN_RC" 3
189
+ eq "credentials-absent on the marker read: web-1 unknown" "$(col web-1 2)" unknown
190
+
191
+ # ===========================================================================================================
192
+ # G1-2: dark source. Control empty, marker rows present but stale: unknown + exit 2. hosts=0 is a failure.
193
+ # ===========================================================================================================
194
+ reset_fx; : | put control; { probe_web 100; probe_inn 100; } | put marker
195
+ run_hs
196
+ eq "dark control exits 2" "$RUN_RC" 2
197
+ eq "dark control: web-1 unknown (not stale)" "$(col web-1 2)" unknown
198
+ eq "dark control: inngest unknown" "$(col inngest 2)" unknown
199
+ has "dark control verdict shows hosts counted and none fresh" "$RUN_OUT" "fresh=0 stale=0 silent=0 unknown=6"
200
+ reset_fx; printf '%s\n' '{"h":"","n":9}' | put control; : | put marker
201
+ run_hs
202
+ eq "control with only empty host names is dark too" "$RUN_RC" 2
203
+
204
+ # ===========================================================================================================
205
+ # G1-3: a check that stops after the first compliant host is the defect. First compliant, second only old.
206
+ # ===========================================================================================================
207
+ reset_fx; seed_control; { probe_web 1; probe_inn 100; } | put marker
208
+ run_hs
209
+ eq "first host fresh" "$(col web-1 2)" fresh
210
+ eq "second host with only an old row is stale, not fresh" "$(col inngest 2)" stale
211
+ eq "old row age is reported" "$(col inngest 4)" 100
212
+
213
+ # ===========================================================================================================
214
+ # G1-5: output that is not JSONEachRow, and valid rows followed by an exception line, both with exit 0.
215
+ # ===========================================================================================================
216
+ reset_fx; seed_control; printf '<html><body>502</body></html>\n' | put marker
217
+ run_hs
218
+ eq "HTML body exits 4" "$RUN_RC" 4
219
+ eq "HTML body: web-1 unknown" "$(col web-1 2)" unknown
220
+ reset_fx; seed_control; { probe_web 1; printf 'Code: 241. DB::Exception: Memory limit exceeded\n'; } | put marker
221
+ run_hs
222
+ eq "valid rows then an exception line exits 4" "$RUN_RC" 4
223
+ eq "valid rows then an exception line: web-1 NOT fresh" "$(col web-1 2)" unknown
224
+ reset_fx; { for h in "${WEBHOSTS[@]}"; do ctl "$h" 40; done; printf 'Code: 241. DB::Exception\n'; } | put control; : | put marker
225
+ run_hs
226
+ eq "control with a trailing exception line exits 4" "$RUN_RC" 4
227
+
228
+ # ===========================================================================================================
229
+ # G1-6: an inngest-shaped row with only uptime_s (and uptime_s=unknown) never yields a kernel.
230
+ # ===========================================================================================================
231
+ reset_fx; seed_control; { probe_inn 1 4242; } | put marker
232
+ run_hs
233
+ eq "uptime-only row: kernel_state unknown" "$(col inngest 3)" unknown
234
+ eq "uptime-only row: kernel column unknown" "$(col inngest 5)" unknown
235
+ reset_fx; seed_control; { row 1 "$(obj inngest-server-probe soleur-inngest-prd 'SOLEUR_INNGEST_SERVER_PROBE http_code=200 uptime_s=unknown host_role=dedicated')"; } | put marker
236
+ run_hs
237
+ eq "uptime_s=unknown does not crash the classifier" "$RUN_RC" 0
238
+ eq "uptime_s=unknown prints unknown uptime" "$(col inngest 6)" unknown
239
+ eq "uptime_s=unknown row is still a fresh marker" "$(col inngest 2)" fresh
240
+
241
+ # ===========================================================================================================
242
+ # G1-7: an unmapped host. The census of hcloud_server blocks is checked below; this is the runtime side.
243
+ # ===========================================================================================================
244
+ reset_fx; { for h in "${WEBHOSTS[@]}" soleur-surprise-host; do ctl "$h" 40; done; } | put control; : | put marker
245
+ run_hs
246
+ has "a seen-but-unlisted host prints an unmapped line" "$RUN_OUT" "unmapped"$'\t'"unknown"
247
+ has "unmapped counts as unknown in the verdict" "$RUN_OUT" "hosts=7 fresh=0 stale=0 silent=4 unknown=3"
248
+
249
+ # ===========================================================================================================
250
+ # G1-8: newline and tab injection through a decoded value.
251
+ # ===========================================================================================================
252
+ reset_fx; seed_control
253
+ INJ=$'9.9.9-1-generic\nweb-1\tfresh\tobserved\t0\t9.9.9-1-generic\t1\t0\tx'
254
+ { row 1 "$(obj ci-deploy soleur-web-2 "SOLEUR_CANARY_SANDBOX_DIAG: image=x trigger=legacy section=kernel val=\"kernel=$INJ docker=1\"")"; } | put marker
255
+ run_hs
256
+ eq "injection: exactly six role lines" "$(nlines)" 6
257
+ eq "injection: web-2 kernel falls to unknown" "$(col web-2 5)" unknown
258
+ eq "injection: web-2 kernel_state unknown" "$(col web-2 3)" unknown
259
+ eq "injection: web-1 stays non-fresh" "$(col web-1 2)" silent
260
+ INJ2=$'abc\ninngest-server-probe'
261
+ { row 1 "$(obj inngest-server-probe soleur-inngest-prd "SOLEUR_INNGEST_SERVER_PROBE uptime_s=12 host_role=dedicated")"; \
262
+ row 1 "$(obj inngest-server-probe soleur-inngest-prd "SOLEUR_INNGEST_SERVER_PROBE uptime_s=$INJ2 host_role=dedicated")"; } | put marker
263
+ run_hs
264
+ eq "injection through uptime keeps six lines" "$(nlines)" 6
265
+ # a value with a trailing newline must not satisfy an end anchor
266
+ reset_fx; seed_control
267
+ { row 1 "$(obj ci-deploy soleur-web-2 $'SOLEUR_CANARY_SANDBOX_DIAG: image=x trigger=l section=kernel val="kernel=9.9.9-1-generic\n docker=1"')"; } | put marker
268
+ run_hs
269
+ eq "a kernel with a trailing newline is not observed" "$(col web-2 3)" unknown
270
+
271
+ # ===========================================================================================================
272
+ # G1-9: no environment variable can redirect the reader (ADR-214).
273
+ # ===========================================================================================================
274
+ reset_fx; seed_control; { probe_web 100; } | put marker
275
+ EVIL="$WORK/evil"; mkdir -p "$EVIL"
276
+ cat > "$EVIL/betterstack-query.sh" <<'EVILSH'
277
+ #!/usr/bin/env bash
278
+ printf '%s\n' '{"dt":"2099-01-01 00:00:00.000000","raw":"{\"SYSLOG_IDENTIFIER\":\"inngest-server-probe\",\"host_name\":\"soleur-web-platform\",\"message\":\"SOLEUR_INNGEST_SERVER_PROBE uptime_s=1 host_role=web\"}","k":"x"}'
279
+ EVILSH
280
+ chmod +x "$EVIL/betterstack-query.sh"
281
+ RUN_RC=0
282
+ RUN_OUT="$(cd "$TREE" && STUB_FX="$TREE/fx" PATH="$PATH_SHIM" BETTERSTACK_QUERY_SCRIPT="$EVIL/betterstack-query.sh" QUERY_SCRIPT="$EVIL/betterstack-query.sh" HOST_STATE_QUERY="$EVIL/betterstack-query.sh" SOLEUR_QUERY="$EVIL/betterstack-query.sh" bash "$TREE/h/host-state.sh" 2> /dev/null)" || RUN_RC=$?
283
+ eq "env-named reader is ignored: web-1 stays stale" "$(col web-1 2)" stale
284
+ # GIT_DIR / GIT_WORK_TREE would redirect --show-toplevel to a repository of the caller's choosing (review finding)
285
+ EVILREPO="$WORK/evilrepo"; mkdir -p "$EVILREPO/scripts"; git -C "$EVILREPO" init -q . 2> /dev/null
286
+ cp "$EVIL/betterstack-query.sh" "$EVILREPO/scripts/betterstack-query.sh"
287
+ RUN_RC=0
288
+ RUN_OUT="$(cd "$WORK" && STUB_FX="$TREE/fx" PATH="$PATH_SHIM" GIT_DIR="$EVILREPO/.git" GIT_WORK_TREE="$EVILREPO" bash "$TREE/h/host-state.sh" 2> /dev/null)" || RUN_RC=$?
289
+ eq "GIT_DIR/GIT_WORK_TREE cannot redirect the reader (cwd outside any repo -> degraded, evil reader not run)" "$RUN_RC" 4
290
+ eq "and no host is fresh" "$(col web-1 2)" unknown
291
+ RUN_RC=0
292
+ RUN_OUT="$(cd "$TREE" && STUB_FX="$TREE/fx" PATH="$PATH_SHIM" GIT_DIR="$EVILREPO/.git" GIT_WORK_TREE="$EVILREPO" bash "$TREE/h/host-state.sh" 2> /dev/null)" || RUN_RC=$?
293
+ eq "GIT_* set while inside the real tree: the tree's reader is still the one used" "$(col web-1 2)" stale
294
+
295
+ # ===========================================================================================================
296
+ # G1-10: harness row - a stub that prints a fresh row for every host must be visible to this suite.
297
+ # ===========================================================================================================
298
+ reset_fx; seed_control
299
+ { probe_web 1; probe_inn 1; nic 0; zot 0; canary 1 soleur-web-2 9.9.9-1-generic; } | put marker
300
+ printf '3\n' > "$TREE/fx/control.rc"
301
+ run_hs
302
+ check "harness row: credentials-absent beats a stub that says fresh to everything" "$([ "$RUN_RC" -eq 3 ] && ! grep -qE $'\t''fresh'$'\t' <<< "$RUN_OUT"; echo $?)"
303
+
304
+ # ===========================================================================================================
305
+ # G1-11: must-PASS non-canonical input - another permitted kernel build and a different ABI are accepted.
306
+ # ===========================================================================================================
307
+ reset_fx; seed_control; { canary 3 soleur-web-2 9.9.9-2-generic; canary 3 soleur-web-platform 10.2.3-15-azure; } | put marker
308
+ run_hs
309
+ eq "permitted kernel variation (-2-generic) accepted" "$(col web-2 5)" 9.9.9-2-generic
310
+ eq "permitted kernel variation (-azure) accepted" "$(col web-1 5)" 10.2.3-15-azure
311
+
312
+ # a kernel that does not match the anchored shape is refused (no free text)
313
+ reset_fx; seed_control; { canary 3 soleur-web-2 'rc-kernel;rm'; } | put marker
314
+ run_hs
315
+ eq "a non-matching kernel string is refused" "$(col web-2 3)" unknown
316
+
317
+ # a diag row from the wrong syslog identifier is noise (a CI job that merely PRINTED the marker name)
318
+ reset_fx; seed_control; { row 1 "$(obj doppler soleur-inngest-prd 'SOLEUR_CANARY_SANDBOX_DIAG: image=x trigger=l section=kernel val="kernel=9.9.9-1-generic docker=1"')"; } | put marker
319
+ run_hs
320
+ eq "marker text under another SYSLOG_IDENTIFIER is not evidence" "$(col inngest 3)" unknown
321
+ # inngest is in the control fixture, so with no real marker row it is silent, never stale or fresh
322
+ eq "and does not make the host stale or fresh" "$(col inngest 2)" silent
323
+
324
+ # a ZOT_DISK row naming a different host is unmapped, not registry
325
+ reset_fx; seed_control; { zot 0 soleur-someone-else; } | put marker
326
+ run_hs
327
+ eq "ZOT_DISK host= other than the registry does not make registry fresh" "$(col registry 2)" unknown
328
+ has "and surfaces as unmapped" "$RUN_OUT" "unmapped"
329
+
330
+ # future-dated rows are ignored (clock skew must not mint a fresh)
331
+ reset_fx; seed_control
332
+ jq -nc --arg dt "$(date -u -d '+30 days' '+%Y-%m-%d %H:%M:%S.000000')" --arg raw "$(obj inngest-server-probe soleur-inngest-prd 'SOLEUR_INNGEST_SERVER_PROBE uptime_s=5 host_role=dedicated')" '{dt:$dt, raw:$raw, k:"x"}' | put marker
333
+ run_hs
334
+ eq "a future-dated row does not make a host fresh" "$(col inngest 2)" silent
335
+
336
+ # bad arguments
337
+ run_hs bogus; eq "unknown argument exits 64" "$RUN_RC" 64
338
+ run_hs image-findings 'CVE-1;rm'; eq "image-findings refuses a non-CVE argument" "$RUN_RC" 64
339
+ run_hs image-findings; eq "image-findings needs an id" "$RUN_RC" 64
340
+
341
+ # outside a git tree the helper degrades instead of guessing
342
+ RUN_RC=0; NOGIT="$WORK/nogit"; mkdir -p "$NOGIT"
343
+ RUN_OUT="$(cd "$NOGIT" && PATH="$PATH_SHIM" bash "$TREE/h/host-state.sh" 2> /dev/null)" || RUN_RC=$?
344
+ eq "no git tree: exit 4" "$RUN_RC" 4
345
+ eq "no git tree: every host unknown" "$(col web-1 2)" unknown
346
+
347
+ # ===========================================================================================================
348
+ # image-findings mode
349
+ # ===========================================================================================================
350
+ img() { # img <hours-ago> <status> <findings-json> <crit> <high> <med> <low> : the REAL shape (message + message_json)
351
+ local j
352
+ j="$(jq -nc --arg st "$2" --argjson f "$3" --arg c "$4" --arg h "$5" --arg m "$6" --arg l "$7" \
353
+ '{marker:"SOLEUR_IMAGE_CVE\u005fSCAN", schema:2, status:$st, counts:{critical:$c, high:$h, medium:$m, low:$l}, findings:$f}')"
354
+ row "$1" "$(jq -nc --argjson mj "$j" '{message:"SOLEUR_IMAGE_CVE\u005fSCAN", message_json:$mj}')"
355
+ }
356
+ img_str() { # message_json delivered as a JSON string
357
+ local j
358
+ j="$(jq -nc --argjson f "$2" '{marker:"SOLEUR_IMAGE_CVE\u005fSCAN", schema:2, status:"measured", counts:{critical:"0", high:"1", medium:"0", low:"0"}, findings:$f}')"
359
+ row "$1" "$(jq -nc --arg mj "$j" '{message:"SOLEUR_IMAGE_CVE\u005fSCAN", message_json:$mj}')"
360
+ }
361
+ img_legacy() { # the older one-line shape: the JSON inside the message text
362
+ local j
363
+ j="$(jq -nc --argjson f "$2" '{marker:"SOLEUR_IMAGE_CVE\u005fSCAN", schema:2, status:"measured", counts:{critical:"0", high:"1", medium:"0", low:"0"}, findings:$f}')"
364
+ row "$1" "$(obj "" "" "${IMG_MARKER} $j")"
365
+ }
366
+ F1='[{"id":"CVE-2099-0001","severity":"high","package":"p","version":"1","fixed_in":"2"}]'
367
+ reset_fx; img 3 measured "$F1" 0 1 0 0 | put image
368
+ run_hs image-findings CVE-2099-0001
369
+ eq "image-findings hit is match yes" "$(awk '$1=="image_findings"{print $5}' <<< "$RUN_OUT")" yes
370
+ reset_fx; img_legacy 3 "$F1" | put image
371
+ run_hs image-findings CVE-2099-0001
372
+ eq "image-findings accepts the older in-message JSON shape too" "$(awk '$1=="image_findings"{print $5}' <<< "$RUN_OUT")" yes
373
+ reset_fx; img_str 3 "$F1" | put image
374
+ run_hs image-findings CVE-2099-0001
375
+ eq "image-findings accepts message_json delivered as a string" "$(awk '$1=="image_findings"{print $5}' <<< "$RUN_OUT")" yes
376
+ reset_fx; img 3 measured "$F1" 0 1 0 0 | put image
377
+ run_hs image-findings CVE-2099-0002
378
+ eq "image-findings fresh complete absence is STILL unknown (the findings list is not the image's full contents)" "$(awk '$1=="image_findings"{print $5}' <<< "$RUN_OUT")" unknown
379
+ eq "...and the read itself succeeded (exit 0, not a degraded fallback that also prints unknown)" "$RUN_RC" 0
380
+ eq "...with a fresh state" "$(awk '$1=="image_findings"{print $2}' <<< "$RUN_OUT")" fresh
381
+ F50="$(jq -nc '[range(0;50) | {id: ("CVE-2099-" + (1000 + . | tostring)), severity: "high", package: "p", version: "1", fixed_in: "2"}]')"
382
+ F49="$(jq -nc '[range(0;49) | {id: ("CVE-2099-" + (1000 + . | tostring)), severity: "high", package: "p", version: "1", fixed_in: "2"}]')"
383
+ reset_fx; img 3 measured "$F50" 0 50 0 0 | put image
384
+ run_hs image-findings CVE-2099-0002
385
+ eq "image-findings 50-finding list (the producer cap) is unknown" "$(awk '$1=="image_findings"{print $5}' <<< "$RUN_OUT")" unknown
386
+ eq "image-findings 50-finding list reports capped yes" "$(awk '$1=="image_findings"{print $4}' <<< "$RUN_OUT")" yes
387
+ reset_fx; img 3 measured "$F49" 0 49 0 0 | put image
388
+ run_hs image-findings CVE-2099-0002
389
+ eq "image-findings 49-finding list is not capped" "$(awk '$1=="image_findings"{print $4}' <<< "$RUN_OUT")" no
390
+ reset_fx; img 3 measured "$F1" 0 1 40 40 | put image
391
+ run_hs image-findings CVE-2099-0002
392
+ eq "image-findings medium/low counts do not set capped" "$(awk '$1=="image_findings"{print $4}' <<< "$RUN_OUT")" no
393
+ reset_fx; img 100 measured "$F1" 0 1 0 0 | put image
394
+ run_hs image-findings CVE-2099-0002
395
+ eq "image-findings stale marker absence is unknown" "$(awk '$1=="image_findings"{print $5}' <<< "$RUN_OUT")" unknown
396
+ reset_fx; img 3 unmeasured "$F1" 0 1 0 0 | put image
397
+ run_hs image-findings CVE-2099-0002
398
+ eq "image-findings unmeasured state absence is unknown" "$(awk '$1=="image_findings"{print $5}' <<< "$RUN_OUT")" unknown
399
+ reset_fx; : | put image
400
+ run_hs image-findings CVE-2099-0002
401
+ eq "image-findings with no marker row is unknown" "$(awk '$1=="image_findings"{print $2}' <<< "$RUN_OUT")" unknown
402
+ reset_fx; printf '3\n' > "$TREE/fx/image.rc"; img 3 measured "$F1" 0 1 0 0 | put image
403
+ run_hs image-findings CVE-2099-0001
404
+ eq "image-findings credentials-absent exits 3 and is unknown" "$RUN_RC" 3
405
+ has "image-findings failure still prints the verdict line" "$RUN_OUT" "SOLEUR_CVE_HOST_STATE mode=image-findings state=unknown match=unknown"
406
+ # a scan marker under a journald identifier is noise
407
+ reset_fx; row 3 "$(obj doppler soleur-inngest-prd "${IMG_MARKER} $(jq -nc --argjson f "$F1" '{marker:"SOLEUR_IMAGE_CVE\u005fSCAN",schema:2,status:"measured",counts:{critical:"0",high:"1",medium:"0",low:"0"},findings:$f}')")" | put image
408
+ run_hs image-findings CVE-2099-0001
409
+ eq "image-findings row under another SYSLOG_IDENTIFIER is not evidence" "$(awk '$1=="image_findings"{print $2}' <<< "$RUN_OUT")" unknown
410
+
411
+ # aliases: a finding listed under the GHSA id answers a query for the CVE plus its alias
412
+ F2='[{"id":"GHSA-abcd-ef12-3456","severity":"high","package":"p","version":"1","fixed_in":"2"}]'
413
+ reset_fx; img 3 measured "$F2" 0 1 0 0 | put image
414
+ run_hs image-findings CVE-2099-0007 GHSA-abcd-ef12-3456
415
+ eq "image-findings alias hit (GHSA finding, CVE+GHSA query) is yes" "$(awk '$1=="image_findings"{print $5}' <<< "$RUN_OUT")" yes
416
+ run_hs image-findings CVE-2099-0007
417
+ eq "image-findings without the alias is unknown, never no" "$(awk '$1=="image_findings"{print $5}' <<< "$RUN_OUT")" unknown
418
+ run_hs image-findings GHSA-abcd-ef12-3456
419
+ eq "image-findings accepts a GHSA id" "$(awk '$1=="image_findings"{print $5}' <<< "$RUN_OUT")" yes
420
+ run_hs image-findings CVE-2099-0007 'not-an-id'; eq "image-findings rejects a bad alias" "$RUN_RC" 64
421
+ run_hs image-findings CVE-2099-0001 CVE-2099-0002 CVE-2099-0003 CVE-2099-0004 CVE-2099-0005 CVE-2099-0006 CVE-2099-0007
422
+ eq "image-findings refuses more than six ids" "$RUN_RC" 64
423
+ # the NEWEST scan row decides (older row has the finding, newer does not -> unknown; reverse -> yes)
424
+ reset_fx; { img 30 measured "$F1" 0 1 0 0; img 3 measured '[]' 0 0 0 0; } | put image
425
+ run_hs image-findings CVE-2099-0001
426
+ eq "image-findings newest row wins (older hit ignored)" "$(awk '$1=="image_findings"{print $5}' <<< "$RUN_OUT")" unknown
427
+ reset_fx; { img 3 measured "$F1" 0 1 0 0; img 30 measured '[]' 0 0 0 0; } | put image
428
+ run_hs image-findings CVE-2099-0001
429
+ eq "image-findings newest row wins (newer hit kept, input order reversed)" "$(awk '$1=="image_findings"{print $5}' <<< "$RUN_OUT")" yes
430
+ # a row dated two minutes ahead (clock skew) clamps to age 0 instead of breaking the shape check
431
+ reset_fx; { row_future=$(jq -nc --arg dt "$(agof 2)" --arg raw "$(jq -nc --argjson mj "$(jq -nc --argjson f "$F1" '{marker:"SOLEUR_IMAGE_CVE\u005fSCAN",schema:2,status:"measured",counts:{critical:"0",high:"1",medium:"0",low:"0"},findings:$f}')" '{message:"SOLEUR_IMAGE_CVE\u005fSCAN", message_json:$mj}')" '{dt:$dt, raw:$raw, k:"x"}'); printf '%s\n' "$row_future"; } | put image
432
+ run_hs image-findings CVE-2099-0001
433
+ eq "image-findings clock-skew row does not break the shape check" "$RUN_RC" 0
434
+ eq "image-findings clock-skew row reports age 0" "$(awk '$1=="image_findings"{print $3}' <<< "$RUN_OUT")" 0
435
+
436
+ # ===========================================================================================================
437
+ # review fixes: freshness boundary, newest-row selection, attribution, host-not-seen, uptime age, "-" host
438
+ # ===========================================================================================================
439
+ reset_fx; seed_control; { rowm $((47 * 60)) "$(obj inngest-server-probe soleur-web-platform 'SOLEUR_INNGEST_SERVER_PROBE uptime_s=11 host_role=web')"; } | put marker
440
+ run_hs; eq "a marker 47h old is fresh" "$(col web-1 2)" fresh
441
+ reset_fx; seed_control; { rowm $((48 * 60 + 30)) "$(obj inngest-server-probe soleur-web-platform 'SOLEUR_INNGEST_SERVER_PROBE uptime_s=11 host_role=web')"; } | put marker
442
+ run_hs; eq "a marker 48h30m old is stale (floor of the age must not admit it)" "$(col web-1 2)" stale
443
+ eq "and its age is reported as 48" "$(col web-1 4)" 48
444
+ reset_fx; seed_control; { rowm $((49 * 60)) "$(obj inngest-server-probe soleur-web-platform 'SOLEUR_INNGEST_SERVER_PROBE uptime_s=11 host_role=web')"; } | put marker
445
+ run_hs; eq "a marker 49h old is stale" "$(col web-1 2)" stale
446
+ eq "a stale row's uptime is not reported" "$(col web-1 6)" unknown
447
+
448
+ # newest-row selection, both input orders, for the periodic row and for the kernel row
449
+ for order in old-first new-first; do
450
+ reset_fx; seed_control
451
+ if [ "$order" = old-first ]; then { probe_web 100 111; probe_web 1 222; canary 60 soleur-web-2 9.9.9-1-generic; canary 3 soleur-web-2 9.9.9-2-generic; } | put marker
452
+ else { probe_web 1 222; probe_web 100 111; canary 3 soleur-web-2 9.9.9-2-generic; canary 60 soleur-web-2 9.9.9-1-generic; } | put marker; fi
453
+ run_hs
454
+ eq "newest periodic row wins ($order): state" "$(col web-1 2)" fresh
455
+ eq "newest periodic row wins ($order): age" "$(col web-1 4)" 1
456
+ eq "newest periodic row wins ($order): uptime" "$(col web-1 6)" 222
457
+ eq "newest kernel row wins ($order): kernel" "$(col web-2 5)" 9.9.9-2-generic
458
+ eq "newest kernel row wins ($order): kernel age" "$(col web-2 7)" 3
459
+ done
460
+
461
+ # a probe row dated two minutes AHEAD (clock skew) is fresh with age 0, never a negative age
462
+ reset_fx; seed_control; { jq -nc --arg dt "$(agof 2)" --arg raw "$(obj inngest-server-probe soleur-web-platform 'SOLEUR_INNGEST_SERVER_PROBE uptime_s=9 host_role=web')" '{dt:$dt, raw:$raw, k:"x"}'; } | put marker
463
+ run_hs
464
+ eq "a row dated 2 minutes ahead is fresh" "$(col web-1 2)" fresh
465
+ eq "...with age 0 (the negative age is clamped)" "$(col web-1 4)" 0
466
+
467
+ # a host the positive control did not see is unknown, not silent
468
+ reset_fx; { for h in soleur-web-platform soleur-inngest-prd soleur-grok-dogfood; do ctl "$h" 40; done; } | put control; : | put marker
469
+ run_hs
470
+ eq "web-2 absent from the control is unknown" "$(col web-2 2)" unknown
471
+ eq "web-1 present in the control with no marker is silent" "$(col web-1 2)" silent
472
+
473
+ # probe attribution: the same probe text from the WRONG host does not make a role fresh
474
+ reset_fx; seed_control; { row 1 "$(obj inngest-server-probe soleur-web-2 'SOLEUR_INNGEST_SERVER_PROBE uptime_s=5 host_role=web')"; } | put marker
475
+ run_hs
476
+ eq "a host_role=web probe from web-2 does not make web-1 fresh" "$(col web-1 2)" silent
477
+ has "and surfaces as unmapped" "$RUN_OUT" "unmapped"
478
+ reset_fx; seed_control; { row 1 "$(obj inngest-server-probe soleur-web-platform 'SOLEUR_INNGEST_SERVER_PROBE uptime_s=5 host_role=dedicated')"; } | put marker
479
+ run_hs
480
+ eq "a host_role=dedicated probe from the web host does not make inngest fresh" "$(col inngest 2)" silent
481
+
482
+ # uptime carries its own freshness: a 6-day-old NIC row beside a fresh ZOT_DISK row yields no uptime
483
+ reset_fx; seed_control; { zot 1; nic 144 99999; } | put marker
484
+ run_hs
485
+ eq "registry is fresh from the ZOT_DISK row" "$(col registry 2)" fresh
486
+ eq "a 6-day-old uptime is not reported beside a fresh state" "$(col registry 6)" unknown
487
+
488
+ # a canary row with host_name "-" (the table's no-host sentinel) must not resolve to git-data
489
+ reset_fx; seed_control; { row 1 "$(obj ci-deploy '-' 'SOLEUR_CANARY_SANDBOX_DIAG: image=x trigger=l section=kernel val="kernel=9.9.9-1-generic docker=1"')"; } | put marker
490
+ run_hs
491
+ eq "host_name '-' does not give git-data a kernel" "$(col git-data 3)" unknown
492
+ eq "host_name '-' does not give git-data a kernel value" "$(col git-data 5)" unknown
493
+ has "host_name '-' is unmapped, not git-data" "$RUN_OUT" "unmapped"$'\t'"unknown"
494
+
495
+ # SQL clause pins (the stub routes on substrings, so pin the clauses it does not execute)
496
+ reset_fx; seed_control; : | put marker; run_hs
497
+ SQLM="$(cat "$TREE/fx/marker.sql" 2> /dev/null)"
498
+ has "marker SQL keeps newest-first ordering" "$SQLM" "ORDER BY dt DESC LIMIT 6 BY k, JSONExtractString(raw,'host_name'), JSONExtractString(raw,'SYSLOG_IDENTIFIER')"
499
+ for mk in "startsWith(JSONExtractString(raw,'message'),'SOLEUR_INNGEST_SERVER_PROBE ')" "startsWith(JSONExtractString(raw,'message'),'SOLEUR_CANARY_SANDBOX_DIAG: ')" "startsWith(JSONExtractString(raw,'message'),'SOLEUR_PRIVATE_NIC ')" "startsWith(JSONExtractString(raw,'message'),'SOLEUR_ZOT_DISK ')" "position(raw,'section=kernel')>0"; do
500
+ has "marker SQL requires: $mk" "$SQLM" "$mk"
501
+ done
502
+ has "marker SQL tags each class in k" "$SQLM" "'probe'"
503
+ SQLC="$(cat "$TREE/fx/control.sql" 2> /dev/null)"
504
+ has "control SQL windows to 2 hours" "$SQLC" "INTERVAL 2 HOUR"
505
+ has "control SQL groups by host and is bounded" "$SQLC" "GROUP BY h ORDER BY n DESC LIMIT 50"
506
+
507
+ # ===========================================================================================================
508
+ # reader verification against upstream (CTO ruling on the review): the checkout's reader runs with the
509
+ # operator's credentials, so it is compared with upstream main BEFORE anything runs
510
+ # ===========================================================================================================
511
+ restore_reader
512
+ reset_fx; seed_control; { probe_web 1; } | put marker
513
+ run_hs
514
+ eq "verified reader: baseline runs" "$RUN_RC" 0
515
+ eq "verified reader: the reader ran" "$([ -f "$TREE/fx/ran" ] && echo yes || echo no)" yes
516
+ has "verified reader: both reader files were fetched" "$(cat "$WORK/up/curl.calls")" "scripts/lib/betterstack-read-classify.sh"
517
+ has "verified reader: from upstream main over https" "$(cat "$WORK/up/curl.calls")" "https://raw.githubusercontent.com/jikig-ai/soleur/main/scripts/betterstack-query.sh"
518
+ restore_reader
519
+
520
+ printf '# planted\n' >> "$TREE/scripts/betterstack-query.sh"
521
+ reset_fx; seed_control; { probe_web 1; } | put marker
522
+ run_hs
523
+ eq "a modified reader exits 4" "$RUN_RC" 4
524
+ eq "a modified reader: nothing is fresh" "$(col web-1 2)" unknown
525
+ eq "a modified reader is NEVER executed" "$([ -f "$TREE/fx/ran" ] && echo yes || echo no)" no
526
+ has "a modified reader names the cause" "$RUN_ERR" "reader-unverified-differs-from-upstream"
527
+ restore_reader
528
+
529
+ printf '# planted\n' >> "$TREE/scripts/lib/betterstack-read-classify.sh"
530
+ reset_fx; seed_control; { probe_web 1; } | put marker
531
+ run_hs
532
+ eq "a modified sourced lib exits 4" "$RUN_RC" 4
533
+ eq "a modified sourced lib: the reader is never executed" "$([ -f "$TREE/fx/ran" ] && echo yes || echo no)" no
534
+ restore_reader
535
+
536
+ : > "$WORK/up/offline"
537
+ reset_fx; seed_control; { probe_web 1; } | put marker
538
+ run_hs
539
+ eq "offline (upstream unreachable) exits 4" "$RUN_RC" 4
540
+ eq "offline: nothing is fresh" "$(col web-1 2)" unknown
541
+ eq "offline: the reader is never executed" "$([ -f "$TREE/fx/ran" ] && echo yes || echo no)" no
542
+ restore_reader
543
+
544
+ mv "$WORK/up/scripts/lib/betterstack-read-classify.sh" "$WORK/up/scripts/lib/betterstack-read-classify.sh.off"
545
+ reset_fx; seed_control; { probe_web 1; } | put marker
546
+ run_hs
547
+ eq "an upstream file that cannot be fetched exits 4" "$RUN_RC" 4
548
+ mv "$WORK/up/scripts/lib/betterstack-read-classify.sh.off" "$WORK/up/scripts/lib/betterstack-read-classify.sh"
549
+ restore_reader
550
+
551
+ # the image-findings mode verifies the reader too
552
+ printf '# planted\n' >> "$TREE/scripts/betterstack-query.sh"
553
+ reset_fx; img 3 measured "$F1" 0 1 0 0 | put image
554
+ run_hs image-findings CVE-2099-0001
555
+ eq "image-findings with a modified reader exits 4" "$RUN_RC" 4
556
+ eq "image-findings with a modified reader never runs it" "$([ -f "$TREE/fx/ran" ] && echo yes || echo no)" no
557
+ restore_reader
558
+
559
+ # ===========================================================================================================
560
+ # producer contract: every marker constant and key the helper reads still exists in its producer, so a rename
561
+ # is RED in the renaming PR (a silent host is the failure this prevents).
562
+ # ===========================================================================================================
563
+ CI_DEPLOY="$REPO_TOP/apps/web-platform/infra/ci-deploy.sh"
564
+ INN="$REPO_TOP/apps/web-platform/infra/inngest-bootstrap.sh"
565
+ REG="$REPO_TOP/apps/web-platform/infra/cloud-init-registry.yml"
566
+ IMG="$REPO_TOP/scripts/image-cve-""scan.sh"
567
+ if [ -f "$CI_DEPLOY" ] && [ -f "$INN" ] && [ -f "$REG" ] && [ -f "$IMG" ]; then
568
+ # Anchored on the EMIT LINE (logger call / LINE= assignment), not the file: a marker name that also appears in a
569
+ # comment or a fallback printf must not keep this green after the emitting line is renamed.
570
+ check "producer: ci-deploy's logger line emits SOLEUR_CANARY_SANDBOX_DIAG with section=\$2 and val=" "$(grep -qE '^[[:space:]]*logger -t "\$LOG_TAG" "SOLEUR_CANARY_SANDBOX_DIAG: .*section=\$2 val=' "$CI_DEPLOY"; echo $?)"
571
+ check "producer: ci-deploy emits the kernel section as kernel=\$(uname -r" "$(grep -qF '_canary_diag_emit "$trigger" kernel "kernel=$(uname -r' "$CI_DEPLOY"; echo $?)"
572
+ check "producer: ci-deploy keeps the ci-deploy log tag" "$(grep -qE '^readonly LOG_TAG="ci-deploy"' "$CI_DEPLOY"; echo $?)"
573
+ check "producer: inngest bootstrap keeps the inngest-server-probe log tag" "$(grep -qE '^LOG_TAG="inngest-server-probe"' "$INN"; echo $?)"
574
+ check "producer: inngest probe line carries uptime_s and host_role" "$(grep -qE '^logger -t "\$LOG_TAG" "SOLEUR_INNGEST_SERVER_PROBE .*uptime_s=\$uptime_s .*host_role=\$host_role' "$INN"; echo $?)"
575
+ check "producer: inngest probe roles are web and dedicated" "$(grep -qE '^host_role=web$' "$INN" && grep -qE '\] && host_role=dedicated$' "$INN"; echo $?)"
576
+ check "producer: registry emits SOLEUR_PRIVATE_NIC with uptime_s" "$(grep -qE 'LINE="SOLEUR_PRIVATE_NIC .*uptime_s=\$UPTIME_S' "$REG"; echo $?)"
577
+ check "producer: registry emits SOLEUR_ZOT_DISK with host=" "$(grep -qE 'LINE="SOLEUR_ZOT_DISK .*host=\$\(hostname\)' "$REG"; echo $?)"
578
+ check "producer: image report marker name and schema" "$(grep -qF "MARKER_NAME=\"${IMG_MARKER}\"" "$IMG" && grep -qF 'schema:2' "$IMG"; echo $?)"
579
+ check "producer: image report ships the marker name and JSON as one message" "$(grep -qF -- '--arg m "$1 $2"' "$IMG"; echo $?)"
580
+ check "producer: image report findings cap is 50" "$(grep -qF 'MAX_FINDINGS_IN_MARKER=50' "$IMG"; echo $?)"
581
+ fi
582
+
583
+ # ===========================================================================================================
584
+ # host census: every hcloud_server block outside a rehearsal root has a role row; web expands by var.web_hosts.
585
+ # ===========================================================================================================
586
+ INFRA="$REPO_TOP/apps/web-platform/infra"
587
+ if [ -d "$INFRA" ]; then
588
+ TABLE_ROLES="$(awk '/^readonly ROLE_TABLE=/{t=1} t{ line=$0; sub(/^readonly ROLE_TABLE=./,"",line); sub(/.$/,"",line); if (line ~ /^[a-z0-9-]+\|/) { split(line,a,"|"); print a[1] } } /^grok-dogfood\|/{t=0}' "$TREE/h/host-state.sh" | sort -u | tr '\n' ' ')"
589
+ eq "role table lists the six roles" "$TABLE_ROLES" "git-data grok-dogfood inngest registry web-1 web-2 "
590
+ BLOCKS="$(git -C "$REPO_TOP" ls-files 'apps/web-platform/infra/*.tf' | grep -v rehearsal | while IFS= read -r f; do grep -hE '^resource[[:space:]]+"hcloud_server"[[:space:]]+"[a-z0-9_]+"' "$REPO_TOP/$f"; done | sed -E 's/.*"hcloud_server"[[:space:]]+"([a-z0-9_]+)".*/\1/' | sort | tr '\n' ' ')"
591
+ eq "census: hcloud_server blocks outside rehearsal roots" "$BLOCKS" "git_data grok_dogfood inngest registry web "
592
+ # every block maps to at least one role; web expands by var.web_hosts, one role per key
593
+ WEB_KEYS="$(sed -n '/^variable "web_hosts"/,/^}/p' "$INFRA/variables.tf" | grep -oE '^\s+"web-[0-9]+" = \{' | grep -oE 'web-[0-9]+' | sort | tr '\n' ' ')"
594
+ eq "census: web_hosts keys equal the web roles" "$WEB_KEYS" "web-1 web-2 "
595
+ for blk in git_data:git-data grok_dogfood:grok-dogfood inngest:inngest registry:registry; do
596
+ b="${blk%%:*}"; r="${blk##*:}"
597
+ case " $TABLE_ROLES " in *" $r "*) ok ;; *) bad "census: block $b has no role row $r" ;; esac
598
+ done
599
+ fi
600
+
601
+ # ===========================================================================================================
602
+ # the helper's own surface: no tracing with a credential, no environment seam for the reader
603
+ # ===========================================================================================================
604
+ if grep -qE '\$\{?(BETTERSTACK_QUERY_SCRIPT|QUERY_SCRIPT|HOST_STATE_QUERY)' "$TREE/h/host-state.sh"; then bad "helper names an environment query seam"; else ok; fi
605
+ check "helper resolves the reader from git rev-parse" "$(grep -qF 'git rev-parse --show-toplevel' "$TREE/h/host-state.sh"; echo $?)"
606
+ check "helper refuses xtrace with a credential" "$(grep -qF 'BETTERSTACK_QUERY_PASSWORD' "$TREE/h/host-state.sh"; echo $?)"
607
+ check "helper prints the verdict marker" "$(grep -qF 'SOLEUR_CVE_HOST_STATE hosts=' "$TREE/h/host-state.sh"; echo $?)"
608
+ XT_RC=0; (cd "$TREE" && PATH="$PATH_SHIM" BETTERSTACK_QUERY_PASSWORD=x bash -x "$TREE/h/host-state.sh" > /dev/null 2>&1) || XT_RC=$?
609
+ eq "xtrace with a live credential is refused (exit 78)" "$XT_RC" 78
610
+
611
+ # ===========================================================================================================
612
+ # skill surface: the literal tokens, the scope guard and the disclosure anchor the skill must carry
613
+ # ===========================================================================================================
614
+ SKILLMD="$HERE/../SKILL.md"
615
+ if [ -f "$SKILLMD" ]; then
616
+ for tok in 'premise unconfirmed' 'premise unconfirmed-offline' 'vendor-only' 'cannot confirm not-affected' 'PROHIBITED-PHRASES-ANCHOR' 'apps/web-platform/infra/server.tf' 'betterstack-query.sh'; do
617
+ if grep -qF -- "$tok" "$SKILLMD"; then ok; else bad "SKILL.md lacks the literal token [$tok]"; fi
618
+ done
619
+ # the prohibited phrases appear only on the anchor line; no durable-text example asserts a safe status
620
+ STRAY_SAFE="$(grep -nEi 'not affected|unaffected|patched|not vulnerable|not exposed|mitigated|you are fine|you are safe|all clear' "$SKILLMD" "$HERE"/../references/*.md "$HERE"/../scripts/*.sh | grep -v 'PROHIBITED-PHRASES-ANCHOR' || true)"
621
+ eq "no safe-status phrase outside the anchor line" "$STRAY_SAFE" ""
622
+ # no kernel-shaped example and no command-position remote-access step
623
+ STRAY_KERNEL="$(grep -nE '[0-9]+\.[0-9]+\.[0-9]+-[0-9]+-generic' "$SKILLMD" "$HERE"/../references/*.md "$HERE"/../scripts/*.sh || true)"
624
+ eq "no kernel-shaped example in the skill" "$STRAY_KERNEL" ""
625
+ STRAY_SSH="$(grep -nEi '(^|[`$] *)(ssh|docker exec|journalctl)\b' "$SKILLMD" "$HERE"/../references/*.md "$HERE"/../scripts/*.sh || true)"
626
+ eq "no remote-access command in the skill" "$STRAY_SSH" ""
627
+ DESC_WORDS="$(sed -n 's/^description: "\(.*\)"$/\1/p' "$SKILLMD" | wc -w | tr -d ' ')"
628
+ if [ "$DESC_WORDS" -ge 1 ] && [ "$DESC_WORDS" -le 28 ]; then ok; else bad "description word count out of range ($DESC_WORDS)"; fi
629
+ fi
630
+
631
+ # ===========================================================================================================
632
+ # review round 2: execution path, strict curl contract, image semantics, SQL pins, reader drift, regex parity
633
+ # ===========================================================================================================
634
+ restore_reader
635
+ reset_fx; seed_control; { probe_web 1; } | put marker
636
+ run_hs
637
+ RAN="$(cat "$TREE/fx/ran" 2> /dev/null)"
638
+ eq "the executed reader is not the checkout path" "$(grep -cF -- "$TREE/" <<< "$RAN" || true)" 0
639
+ has "the executed reader is the verified scratch copy" "$RAN" "/reader/scripts/betterstack-query.sh"
640
+ ARGV="$(cat "$WORK/up/curl.argv" 2> /dev/null)"
641
+ has "curl is called with the hardened flag prefix" "$ARGV" "--disable --noproxy * --proto =https --tlsv1.2 -fsS"
642
+ eq "no curl call was refused for flags or poisoned environment" "$(grep -cE 'BAD-FLAGS|POISONED-ENV' <<< "$ARGV" || true)" 0
643
+ eq "one curl call per reader file" "$(grep -c . <<< "$ARGV")" 2
644
+ restore_reader
645
+
646
+ # a symlinked reader is refused before anything is downloaded or run
647
+ mv "$TREE/scripts/betterstack-query.sh" "$WORK/real-reader.sh"; ln -s "$WORK/real-reader.sh" "$TREE/scripts/betterstack-query.sh"
648
+ reset_fx; seed_control; { probe_web 1; } | put marker
649
+ run_hs
650
+ eq "a symlinked reader exits 4" "$RUN_RC" 4
651
+ has "a symlinked reader names the cause" "$RUN_ERR" "reader-unverified-not-a-regular-file"
652
+ eq "a symlinked reader is never executed" "$([ -f "$TREE/fx/ran" ] && echo yes || echo no)" no
653
+ rm -f "$TREE/scripts/betterstack-query.sh"; restore_reader
654
+
655
+ # an empty download is a distinct failure, not a byte-compare against nothing
656
+ cp "$WORK/up/scripts/betterstack-query.sh" "$WORK/up-reader.keep"
657
+ : > "$WORK/up/scripts/betterstack-query.sh"
658
+ reset_fx; seed_control; { probe_web 1; } | put marker
659
+ run_hs
660
+ eq "an empty upstream download exits 4" "$RUN_RC" 4
661
+ has "an empty upstream download names the cause" "$RUN_ERR" "reader-unverified-empty-download"
662
+ eq "an empty upstream download never runs the reader" "$([ -f "$TREE/fx/ran" ] && echo yes || echo no)" no
663
+ cp "$WORK/up-reader.keep" "$WORK/up/scripts/betterstack-query.sh"
664
+ restore_reader
665
+ printf '# planted\n' >> "$TREE/scripts/betterstack-query.sh"
666
+ reset_fx; seed_control; { probe_web 1; } | put marker
667
+ run_hs
668
+ has "offline/modified/empty reasons are distinct tokens (modified)" "$RUN_ERR" "reader-unverified-differs-from-upstream"
669
+ restore_reader
670
+ : > "$WORK/up/offline"
671
+ reset_fx; seed_control; { probe_web 1; } | put marker
672
+ run_hs
673
+ has "offline names download-failed" "$RUN_ERR" "reader-unverified-download-failed"
674
+ restore_reader
675
+
676
+ # image-findings semantics
677
+ img_raw() { # img_raw <hours-ago> <schema> <status> (one finding F1, real shape)
678
+ local j
679
+ j="$(jq -nc --argjson f "$F1" --argjson sc "$2" --arg st "$3" '{marker:"SOLEUR_IMAGE_CVE\u005fSCAN", schema:$sc, status:$st, counts:{critical:"0", high:"1", medium:"0", low:"0"}, findings:$f}')"
680
+ row "$1" "$(jq -nc --argjson mj "$j" '{message:"SOLEUR_IMAGE_CVE\u005fSCAN", message_json:$mj}')"
681
+ }
682
+ reset_fx; img_raw 3 1 measured | put image
683
+ run_hs image-findings CVE-2099-0001
684
+ eq "image-findings schema 1 is rejected (state unknown)" "$(awk '$1=="image_findings"{print $2}' <<< "$RUN_OUT")" unknown
685
+ eq "image-findings schema 1 is rejected (no match claim)" "$(awk '$1=="image_findings"{print $5}' <<< "$RUN_OUT")" unknown
686
+ reset_fx; img_raw 3 2 failed | put image
687
+ run_hs image-findings CVE-2099-0001
688
+ eq "image-findings status != measured: state unknown" "$(awk '$1=="image_findings"{print $2}' <<< "$RUN_OUT")" unknown
689
+ reset_fx; img_raw 47 2 measured | put image
690
+ run_hs image-findings CVE-2099-0001
691
+ eq "image-findings 47h is fresh" "$(awk '$1=="image_findings"{print $2}' <<< "$RUN_OUT")" fresh
692
+ reset_fx; img_raw 49 2 measured | put image
693
+ run_hs image-findings CVE-2099-0001
694
+ eq "image-findings 49h is stale" "$(awk '$1=="image_findings"{print $2}' <<< "$RUN_OUT")" stale
695
+ reset_fx; img_raw 3 2 measured | put image
696
+ run_hs image-findings CVE-2099-0001
697
+ SQLI="$(cat "$TREE/fx/image.sql" 2> /dev/null)"
698
+ has "image SQL windows to 7 days" "$SQLI" "INTERVAL 7 DAY"
699
+ has "image SQL takes the newest five" "$SQLI" "ORDER BY dt DESC LIMIT 5 FORMAT"
700
+ has "image SQL has the archive arm" "$SQLI" 's3Cluster(primary, $BS_TABLE_S3) WHERE _row_type = 1'
701
+ has "image SQL filters on the marker name" "$SQLI" "position(raw,'${IMG_MARKER}')>0"
702
+
703
+ # full marker SQL pins: hot arm window, OR joins between the four marker predicates, the class tag
704
+ reset_fx; seed_control; : | put marker; run_hs
705
+ SQLM="$(cat "$TREE/fx/marker.sql" 2> /dev/null)"
706
+ has "marker SQL hot arm is windowed" "$SQLM" 'remote($BS_TABLE) WHERE dt > now() - INTERVAL 7 DAY'
707
+ eq "marker SQL joins the four predicates with OR" "$(grep -o ') OR (position(raw,' <<< "$SQLM" | grep -c .)" 6
708
+ has "marker SQL class tag is the full multiIf" "$SQLM" "multiIf(position(raw,'SOLEUR_INNGEST_SERVER_PROBE')>0,'probe',position(raw,'SOLEUR_CANARY_SANDBOX_DIAG')>0,'canary',position(raw,'SOLEUR_PRIVATE_NIC')>0,'nic','zot') AS k"
709
+
710
+ # READER_FILES drift: the real reader and classifier source and exec nothing else, so verifying those two
711
+ # files verifies everything that runs. A new dependency in either is RED here.
712
+ for f in scripts/betterstack-query.sh scripts/lib/betterstack-read-classify.sh; do
713
+ if [ -f "$REPO_TOP/$f" ]; then
714
+ eq "real $f sources nothing" "$(grep -cE '^[[:space:]]*(source|\.)[[:space:]]+[^ ]' "$REPO_TOP/$f" || true)" 0
715
+ eq "real $f resolves no sibling path" "$(grep -cE 'BASH_SOURCE|dirname[[:space:]]+"?\$0' "$REPO_TOP/$f" || true)" 0
716
+ else bad "real $f missing from the repo"; fi
717
+ done
718
+ RF="$(sed -n "s/^readonly READER_FILES='\(.*\)'$/\1/p" "$SUT_SRC")"
719
+ eq "helper verifies exactly the two reader files" "$RF" "scripts/betterstack-query.sh scripts/lib/betterstack-read-classify.sh"
720
+
721
+ # kernel-shape parity: the bash regex and the jq regex in the helper accept and reject the same strings
722
+ BASH_KRE="$(sed -n "s/^readonly KERNEL_RE='\(.*\)'$/\1/p" "$SUT_SRC")"
723
+ JQ_KRE="$(grep -o 'test("\\\\A\[0-9\]+[^"]*\\\\z")' "$SUT_SRC" | head -1 | sed 's/^test("//; s/")$//; s/\\\\/\\/g')"
724
+ check "kernel regexes were extracted from the helper" "$([ -n "$BASH_KRE" ] && [ -n "$JQ_KRE" ]; echo $?)"
725
+ for k in 6.8.0-1-generic 9.9.9-1-generic 6.1.0-13-amd64 6.8.0-generic 6.8-1-generic 6.8.0-1-Generic $'6.8.0-1-generic\n' ' 6.8.0-1-generic' '6.8.0-1-' 'x'; do
726
+ if [[ "$k" =~ $BASH_KRE ]]; then b=yes; else b=no; fi
727
+ j="$(jq -nr --arg k "$k" --arg re "$JQ_KRE" '$k | test($re)' 2> /dev/null | sed 's/true/yes/; s/false/no/')"
728
+ eq "kernel regex parity for [$(printf '%s' "$k" | tr '\n' '~')]" "$j" "$b"
729
+ done
730
+
731
+ # ===========================================================================================================
732
+ # floor and verdict
733
+ # ===========================================================================================================
734
+ SELFTEST_PASSES=1
735
+ REAL=$((PASSES - SELFTEST_PASSES))
736
+ MIN_CASES=233
737
+ if [ "$REAL" -lt "$MIN_CASES" ]; then
738
+ printf 'host-state: FAIL (only %s assertions ran; floor %s - the suite may have been truncated)\n' "$REAL" "$MIN_CASES"
739
+ exit 1
740
+ fi
741
+ if [ "$FAILS" -gt 0 ]; then
742
+ printf '%s assertions, %s failed\n' "$((REAL + FAILS))" "$FAILS"
743
+ printf 'host-state: FAIL\n'
744
+ exit 1
745
+ fi
746
+ printf '%s assertions passed\n' "$REAL"
747
+ printf 'host-state: PASS\n'