12:step() { STEP=$1; } # name the section that follows, so fail() can say where it was
13:fail() { echo "FAIL [$STEP] line $1: $2" >&2; exit 1; }
15:"$CTX" version | grep -q '^sqlite 3\.' || fail "version"
22:[ -s "$CTX_DB" ] || fail "init created no database"
27: [ -s "$HOME/.ctx/ctx.sqlite" ]) || fail "init in HOME"
32:cmp "$T/out.txt" test/expected.txt || { diff "$T/out.txt" test/expected.txt >&2 || true; fail "extract"; }
46:[ "$("$CTX" extract "$T/speakers.jsonl" | sed -n 's/^--- speakers turn [0-9]* \([a-z]*\) .*/\1/p' | tr '\n' ' ')" = "user user meta meta meta tool assistant " ] || fail "speakers: $("$CTX" extract "$T/speakers.jsonl" | grep '^---')"
127:[ "$(grep -c '^--- ' "$T/partial.txt")" = 1 ] || fail "partial line"
139:[ "$("$CTX" archive "$A")" = "3 turns archived" ] || fail "first archive"
140:[ "$("$CTX" archive "$A")" = "0 turns archived" ] || fail "second archive stored something"
141:[ "$("$CTX" sql "SELECT count(*) FROM ctx_capture_day")" = 2 ] || fail "one technote per day"
142:[ "$("$CTX" sql "SELECT project FROM ctx_capture")" = src ] || fail "project falls back to the directory name"
143:[ "$("$CTX" sql "SELECT count(*) FROM ctx_chunk_fts WHERE ctx_chunk_fts MATCH 'zebras'")" = 1 ] || fail "search finds a turn"
144:"$CTX" sql "SELECT fsl_technote(technote) FROM ctx_capture_day WHERE day='2026-03-01'" | grep -q 'first answer' || fail "technote body"
145:"$CTX" sql "SELECT fsl_technote(technote) FROM ctx_capture_day WHERE day='2026-03-01'" | grep -q '^## assistant 2026-03-01T10:00:05Z [A-Za-z0-9-][A-Za-z0-9-]*$' || fail "technote heading"
150:[ "$("$CTX" archive "$A")" = "1 turns archived" ] || fail "append"
151:[ "$("$CTX" sql "SELECT count(*) FROM ctx_capture_day")" = 2 ] || fail "append made a new technote"
153:grep -q 'next day' "$T/day.txt" && grep -q 'later reply' "$T/day.txt" || fail "append lost text"
158:[ "$("$CTX" archive "$A")" = "0 turns archived" ] || fail "partial line was stored"
160:[ "$("$CTX" archive "$A")" = "1 turns archived" ] || fail "completed line was not stored"
166:[ "$("$CTX" archive "$A")" = "1 turns archived" ] || fail "replaced file"
167:[ "$("$CTX" sql "SELECT count(*) FROM ctx_chunk")" = 6 ] || fail "duplicate turns after replacement"
178:"$CTX" sql "SELECT fsl_technote('$NOTE')" | grep -q '^## user 2026-01-02T00:00:03Z$' || fail "an old heading has no turn id"
183:[ "$("$CTX" sql "SELECT count(*) FROM ctx_chunk WHERE heading='user'")" = 7 ] || fail "the old labels are set up: $("$CTX" sql "SELECT count(*) FROM ctx_chunk WHERE heading='user'")"
185:grep -q '4 turns in 1 technotes now say tool or meta, not user; 1 request copies removed' "$T/rel.err" || fail "relabel reports: $(cat "$T/rel.err")"
186:"$CTX" search --all 'request copy' 2>/dev/null | grep -q 'request copy' && fail "a request copy leaves the index"
187:"$CTX" sql "SELECT fsl_technote('$NOTE')" | grep -q 'a request copy' && fail "a request copy leaves the technote"
188:[ "$("$CTX" sql "SELECT group_concat(heading, ' ') FROM (SELECT heading FROM ctx_chunk ORDER BY seq)")" = "user user meta meta meta tool assistant" ] || fail "the index follows: $("$CTX" sql "SELECT group_concat(heading, ' ') FROM ctx_chunk")"
189:"$CTX" sql "SELECT fsl_technote('$NOTE')" | grep -c '^## \(tool\|meta\) ' | grep -qx 4 || fail "the technote is corrected, so it syncs"
191:[ "$("$CTX" sql "SELECT count(*) FROM ctx_chunk WHERE heading='user'")" = 2 ] || fail "a rebuild keeps the correction"
193:[ ! -s "$T/rel.err" ] || fail "relabel runs once: $(cat "$T/rel.err")"
207:CLAUDE_CONFIG_DIR="$RC" "$CTX" rules import | grep -q '^2 rules changed, 2 active' || fail "import CLAUDE.md and a memory file"
209:grep -q 'rp .*Raw NOTAM' "$T/rules.txt" 2>/dev/null || grep -q 'notam-mono' "$T/rules.txt" || fail "a memory file belongs to the project its transcripts are in: $(cat "$T/rules.txt")"
210:grep -q '\*  Never push straight to main' "$T/rules.txt" || fail "CLAUDE.md applies to every project: $(cat "$T/rules.txt")"
211:"$CTX" search --all monospaced | grep -q '^#[0-9]* rp rule' || fail "a rule is searchable"
212:"$CTX" search --all ghp_abcdefghijklmnop 2>/dev/null | grep -q rule && fail "a secret in a memory file is redacted"
213:CLAUDE_CONFIG_DIR="$RC" "$CTX" rules import | grep -q '^0 rules changed' || fail "an unchanged file changes nothing"
216:"$CTX" rules --project rp | grep -q "^$K1  rp  Add a changelog" || fail "remember: $("$CTX" rules --project rp)"
217:"$CTX" rules --project rp | grep -q "x.x.1" && fail "an expired rule is not active"
218:"$CTX" rules --all --project rp | grep -q "^$K2  rp expired" || fail "an expired rule is kept: $("$CTX" rules --all --project rp)"
219:"$CTX" search --all beta 2>/dev/null | grep -q rule && fail "search leaves an expired rule out"
220:"$CTX" search --all --history beta | grep -q rule || fail "--history brings an expired rule back"
222:if "$CTX" rules retire "$K1" 2>/dev/null; then fail "a retirement needs a reason"; fi
223:"$CTX" rules retire "$K1" --reason "release notes too" --by "$K3" | grep -q replaced || fail "replace a rule"
224:"$CTX" rules --all --project rp | grep -q "^$K1  rp replaced" || fail "a replaced rule is kept and marked"
226:CLAUDE_CONFIG_DIR="$RC" "$CTX" rules import | grep -q '^1 rules changed' || fail "an edited file makes a new version"
227:"$CTX" search --all jeppesen | grep -q rule || fail "the new version is searchable"
229:[ "$("$CTX" sql "SELECT count(*) FROM fslrepo_me_tagxref x JOIN fslrepo_me_tag t ON t.tagid = x.tagid WHERE t.tagname = 'event-$N'")" = 2 ] || fail "the old version stays in history"
232:"$CTX" rules --all --project rp | grep -q 'notam-mono.md  rp retired' || fail "a removed file retires its rule: $("$CTX" rules --all --project rp)"
233:[ "$("$CTX" sql "SELECT count(*) FROM ctx_doc WHERE kind='session' AND id IN (SELECT doc_id FROM ctx_chunk WHERE body LIKE '%changelog%')")" = 0 ] || fail "a rule is not a session"
236:[ "$("$CTX" sql "SELECT count(*) FROM ctx_doc WHERE kind='rule'")" = "$B" ] || fail "a rebuild keeps the rules"
237:"$CTX" sql "SELECT count(*) FROM ctx_doc WHERE kind='session' AND name IN (SELECT technote FROM ctx_rule)" | grep -qx 0 || fail "a rebuild does not read rules as sessions"
244:"$CTX" rules --all --project rp | grep -q '^backlog.md  rp faded' || fail "an old status note fades: $("$CTX" rules --all --project rp)"
245:"$CTX" rules --project rp | grep -q 'Commit messages are short' || fail "an old correction does not fade"
246:"$CTX" rules --project rp | grep -q 'Backlog after' && fail "a faded note is not in force"
247:"$CTX" search --all kestrel | grep -q 'backlog.md' && fail "search leaves a faded note out"
248:"$CTX" search --all --history kestrel | grep -q 'backlog.md' || fail "--history brings a faded note back"
249:"$CTX" sql "SELECT fsl_technote(technote) FROM ctx_rule WHERE key LIKE '%backlog.md'" | grep -q '^kind: project$' || fail "a rule records its kind"
252:"$CTX" rules --project rp | grep -q 'Backlog after' || fail "editing a status note brings it back"
265:"$CTX" rules judge 2>&1 | grep -q 'no judge' || fail "the judge is off until set"
267:"$CTX" rules judge | grep -q '1 may be outdated' || fail "the judge flags a contradicted rule: $("$CTX" rules judge 2>&1)"
269:grep -q 'One rule may be outdated' "$T/ask.json" && grep -q 'we no longer keep kestrel commit messages short' "$T/ask.json" || fail "the hook asks once, with the quote: $(cat "$T/ask.json")"
270:(cd "$T/w/rp" && "$CTX" rules offer) | grep -q 'One rule may be outdated' && fail "the same rule is not asked about again within 30 days"
271:"$CTX" rules keep short-commits.md | grep -q kept || fail "keep records the person's answer"
272:[ "$("$CTX" sql "SELECT verdict FROM ctx_rule_check WHERE key LIKE '%short-commits.md'")" = true ] || fail "a kept rule is no longer flagged"
276:[ "$("$CTX" sql "SELECT verdict FROM ctx_rule_check WHERE key LIKE '%short-commits.md'")" = true ] || fail "an invented quote is not evidence"
278:if CTX_JUDGE="sh -c 'exit 3'" "$CTX" rules judge 2>"$T/jf.txt" >/dev/null; then fail "a failing judge fails"; fi
280:if CTX_MODEL_DEADLINE=1 CTX_JUDGE="sh -c 'sleep 95 & sleep 95; echo late'" "$CTX" rules judge 2>"$T/jd.txt" >/dev/null; then fail "a judge past its deadline fails"; fi
281:[ $(( $(date +%s) - s0 )) -lt 10 ] && grep -q 'passed 1 seconds; stopped' "$T/jd.txt" || fail "a stuck judge is stopped at its deadline: $(cat "$T/jd.txt")"
282:sleep 1; pgrep -f 'sleep 95' >/dev/null && fail "the stuck judge's children are stopped too"
283:grep -q 'judge failed (exit 3)' "$T/jf.txt" || fail "the judge's real exit code is reported: $(cat "$T/jf.txt")"
285:grep -q 'unreadable' "$T/jf.txt" || fail "an unreadable answer is reported"
286:[ "$("$CTX" sql "SELECT checked FROM ctx_rule_check WHERE key LIKE '%short-commits.md'")" = 2026-01-01 ] || fail "a failing or unreadable judge records nothing for the rule it judged"
289:[ "$("$CTX" sql "SELECT verdict FROM ctx_rule_check WHERE key LIKE '%short-commits.md'")" = true ] || fail "a quote spanning two turns is not evidence"
296:[ "$("$CTX" sql "SELECT count(*) FROM ctx_rule_check WHERE key GLOB '*/CLAUDE.md'")" = 0 ] || fail "a project's CLAUDE.md is never judged"
299:(cd "$T/w/rp" && "$CTX" rules offer) | grep -q 'One rule may be outdated' && fail "a retired rule is never asked about"
304:grep -q 'backlog.md' "$T/review.txt" || fail "review lists rules due a check: $(cat "$T/review.txt")"
305:grep -q 'short-commits.md' "$T/review.txt" && fail "review leaves retired rules out"
306:(cd "$T/w/rp" && "$CTX" rules review --done) | grep -q 'marked reviewed' || fail "review --done marks them"
307:(cd "$T/w/rp" && "$CTX" rules review) | grep -q 'no rules are due' || fail "reviewed rules are not due again"
310:(cd "$T/w/rp" && "$CTX" rules offer) | grep -q 'monthly check' || fail "the hook mentions a due review"
311:(cd "$T/w/rp" && "$CTX" rules offer) | grep -q 'monthly check' && fail "the review is mentioned once a month"
322:[ -z "$(cd "$T" && CTX_DB="$T/none.sqlite" "$CTX" rules offer)" ] || fail "the hook says nothing without a database"
350:grep -A1 'answered' "$T/dec.txt" | grep -q 'Which store for the plover index' || fail "a question and its answer are recorded: $(cat "$T/dec.txt")"
351:grep -q 'A: SQLite$' "$T/dec.txt" || fail "the answer is the chosen option alone: $(cat "$T/dec.txt")"
352:grep -q 'A: No$' "$T/dec.txt" || fail "an answer in the next day's technote is found: $(cat "$T/dec.txt")"
353:[ "$("$CTX" sql "SELECT status FROM ctx_decision WHERE question LIKE 'Rename the plover%'")" = interrupted ] || fail "a call cut off by the session ending is interrupted"
354:[ "$("$CTX" sql "SELECT status FROM ctx_decision WHERE question LIKE 'Which plover colour%'")" = open ] || fail "an unanswered question is open"
355:(cd "$T/w/rp" && "$CTX" decisions --open) | grep -q 'Which store' && fail "--open leaves answered questions out"
366:printf '{"source":"startup","session_id":"d00d0002-0000-0000-0000-000000000002"}' | (cd "$T/w/rp" && "$CTX" rules offer) | grep -q 'Which plover colour' && fail "a fresh start carries no brief"
394:grep -q 'Heron Plan' "$T/art.txt" && grep -A1 'Heron Plan' "$T/art.txt" | grep -q 'artifact/HERON1  version 2 of 2 publishes' || fail "an artifact lists its title, address and versions: $(cat "$T/art.txt")"
395:grep -A2 'Heron Plan' "$T/art.txt" | grep -q 'approved: version 1 on .* (turn:y-1)' || fail "a short yes approves the version before it, citing the turn: $(cat "$T/art.txt")"
396:grep -A2 'egret.html' "$T/art.txt" | grep -q 'approved: version 2 on' || fail "the approval belongs to the latest version before it: $(cat "$T/art.txt")"
397:[ "$("$CTX" sql "SELECT count(*) FROM ctx_artifact WHERE approved_turn IS NOT NULL")" = 2 ] || fail "a superseded version and one followed by a comment are not approved"
398:grep -q QUOTED3 "$T/art.txt" && fail "text that quotes a publish line is not a publish"
399:(cd "$T/w/rp" && "$CTX" artifacts heron | grep -q 'Heron Plan') && ! (cd "$T/w/rp" && "$CTX" artifacts heron | grep -q egret) || fail "words narrow the list"
401:[ "$("$CTX" sql "SELECT count(*) FROM ctx_artifact")" = 4 ] || fail "reading again adds nothing"
413:[ -f "$CTX_DB.artifacts/HERON1/v1.html" ] && [ -f "$CTX_DB.artifacts/HERON1/v2.html" ] && [ ! -e "$CTX_DB.artifacts/EGRET2/v2.html" ] || fail "each published version's file is copied, and only those"
415:grep -A3 'Heron Plan, first cut' "$T/art2.txt" | grep -q 'kept: version 1' || fail "an approved version is filed, under the page's own title: $(cat "$T/art2.txt")"
416:"$CTX" artifacts show https://claude.ai/artifact/HERON1 | grep -q '<p>one</p>' || fail "show prints the kept copy"
417:[ ! -e "$CTX_DB.artifacts/HERON1/v1.html" ] && [ -f "$CTX_DB.artifacts/HERON1/v2.html" ] || fail "a filed copy is removed; the newest waits a day"
420:grep -A3 'Heron Plan' "$T/art3.txt" | grep -q 'kept: version 2' || fail "the newest version is filed once a day has passed: $(cat "$T/art3.txt")"
422:grep -q '<p>two</p>' "$T/h2.html" && ! grep -q hunter2 "$T/h2.html" || fail "the kept copy is redacted"
423:[ "$("$CTX" sql "SELECT count(*) FROM ctx_artifact_file")" = 1 ] && [ ! -e "$CTX_DB.artifacts/EGRET2/v1.html" ] || fail "a version that was neither approved nor newest is dropped, not filed"
424:[ "$("$CTX" sql "SELECT count(*) FROM ctx_doc WHERE kind = 'session' AND title LIKE '%Heron%'")" = 0 ] || fail "a kept page is not a session"
454:grep -q 'Kite Report' "$T/kite.txt" && grep -q 'pages.example.test/v/kite7#ab12.*  version 2 of 2 publishes' "$T/kite.txt" || fail "a publisher page lists its title, read link and versions: $(cat "$T/kite.txt")"
455:grep -q 'approved: version 2 on .* (turn:y-2)' "$T/kite.txt" || fail "a publisher page records an approval: $(cat "$T/kite.txt")"
456:[ "$("$CTX" sql "SELECT group_concat(version_id) FROM (SELECT version_id FROM ctx_artifact WHERE url LIKE '%kite7%' ORDER BY version)")" = c0ffee01,c0ffee02 ] || fail "each version keeps its check-in"
457:[ "$("$CTX" sql "SELECT count(*) FROM ctx_artifact WHERE url LIKE '%abc123%'")" = 0 ] || fail "an example link in a document is not a publish"
469:s0=$(date +%s); sleep 4 | (cd "$T/w/rp" && "$CTX" rules offer >/dev/null; date +%s > "$T/end"); [ $(( $(cat "$T/end") - s0 )) -lt 3 ] || fail "the hook does not wait on a stdin that stays open"
470:s0=$(date +%s); (printf '{"source":"compact"'; sleep 4) | (cd "$T/w/rp" && "$CTX" rules offer >/dev/null; date +%s > "$T/end"); [ $(( $(cat "$T/end") - s0 )) -lt 3 ] || fail "the hook does not wait on a writer that never closes"
480:[ -z "$("$CTX" rules proposed --all)" ] || fail "nothing is proposed without a classifier"
481:[ -z "$("$CTX" rules propose 2>&1 | grep -v 'no classifier')" ] || fail "propose without a classifier only says so"
482:"$CTX" rules prompt | grep -q 'STANDING instruction' || fail "ctx owns the prompt"
484:grep -q '"turn":"r-2"' "$T/cand.txt" && grep -q '"turn":"r-3"' "$T/cand.txt" || fail "the person's turns are candidates: $(cat "$T/cand.txt")"
485:grep -q 'push to main' "$T/cand.txt" && fail "tool output is never a candidate"
494:if CTX_CLASSIFY="sh -c 'exit 3'" "$CTX" rules propose 2>"$T/fail.txt" >/dev/null; then fail "a failing classifier fails propose"; fi
495:grep -q 'classifier failed' "$T/fail.txt" || fail "a failing classifier is reported"
496:"$CTX" rules candidates | grep -q '"turn":"r-2"' || fail "turns wait after a failed classification"
498:"$CTX" rules propose | grep -q '1 proposed' || fail "propose records what the classifier calls standing: $("$CTX" rules propose 2>&1)"
500:[ "$(wc -l < "$T/prop.txt" | tr -d ' ')" = 1 ] && grep -q 'Use strata' "$T/prop.txt" || fail "one proposal, in the classifier's words: $(cat "$T/prop.txt")"
501:grep -q 'Delete everything' "$T/prop.txt" && fail "an answer about a turn outside the batch is ignored"
502:"$CTX" rules propose | grep -q '^0 turns classified' || fail "a classified turn is never sent again"
503:[ "$(wc -l < "$T/calls.txt" | tr -d ' ')" = 1 ] || fail "the classifier ran once"
504:(cd "$T/w/rp" && "$CTX" rules offer) | grep -q 'wait for their decision' || fail "the hook mentions waiting proposals"
506:"$CTX" rules suggest r-3 "Email goes through resend" | grep -q proposed || fail "suggest records a proposal by hand"
508:"$CTX" rules accept "$P1" --until 2099-01-01 | grep -q accepted || fail "accept a proposal"
509:"$CTX" rules reject "$P2" | grep -q rejected || fail "reject a proposal"
510:[ -z "$("$CTX" rules proposed --all)" ] || fail "decided proposals leave the list"
511:"$CTX" rules --project rp | grep -q 'Use strata when several' || fail "an accepted proposal is a rule"
513:"$CTX" turn turn:r-2 | grep -q 'just use strata' || fail "ctx turn finds a turn by its transcript id"
515:"$CTX" sql "SELECT fsl_technote(technote) FROM ctx_rule WHERE key = 'manual:$K4'" | grep -q '^origin: turn:r-3$' || fail "remember --from records the turn"
546:"$CTX" stats > "$T/stats.txt" || fail "stats runs"
547:grep -qE '^statsproj +1 +4 +1 +2 +1$' "$T/stats.txt" || fail "stats by project: $(cat "$T/stats.txt")"
548:grep -qE '^[0-9]{4}-[0-9]{2}-[0-9]{2} +1 +4 +1 +2 +1$' "$T/stats.txt" || fail "stats by week: $(cat "$T/stats.txt")"
549:if "$CTX" stats --weeks 0 2>/dev/null; then fail "weeks must be positive"; fi
563:[ "$("$CTX" sql "SELECT count(*) FROM ctx_chunk WHERE body LIKE '%aB3aB3aB3%'")" = 0 ] || fail "secret in the index"
565:grep -q 'aB3aB3aB3' "$T/k.txt" && fail "secret in the technote"
566:grep -q 'REDACTED' "$T/k.txt" || fail "redaction marker"
582:mkdir -p "$T/alpha"; R=$(cd "$T/alpha" && "$CTX" search --all walrus); [ "$(echo "$R" | grep -c '^#')" = 2 ] || fail "search --all"
583:echo "$R" | grep -q '\[walrus\]' || fail "search highlights the match"
584:[ "$(cd "$T/alpha" && "$CTX" search walrus --all --limit 1 | grep -c '^#')" = 1 ] || fail "options after the words"
588:grep -q 'walrus migration' "$T/turn.txt" && grep -q 'lock was held' "$T/turn.txt" || fail "turn and its neighbour"
589:[ "$("$CTX" turn "$ID" | grep -c '^--- #')" = 1 ] || fail "turn alone"
591:[ "$(cd "$T/alpha" && "$CTX" search walrus | grep -c '^#')" = 1 ] || fail "search stays in the project"
592:[ "$(cd "$T" && "$CTX" search walrus 2>/dev/null | grep -c '^#')" = 2 ] || fail "falls back when the project has nothing"
593:if "$CTX" search the 2>/dev/null; then fail "search with only stopwords"; fi
594:if "$CTX" turn 999999 2>/dev/null; then fail "missing turn"; fi
606:"$CTX" turn "$ID" | grep -q 'more bytes; use --full' || fail "long turn is cut"
607:[ "$("$CTX" turn "$ID" --full | wc -c)" -gt 20000 ] || fail "--full shows it all"
621:[ "$("$CTX" sql "SELECT count(*) FROM ctx_chunk WHERE project='repo'")" = 2 ] || fail "a turn's project comes from its own directory"
622:[ "$(cd "$T/repo/internal/deep" && "$CTX" search pelican | grep -c '^#')" = 2 ] || fail "search from a subdirectory finds the repository's turns"
658:[ -e "$T/hook.sqlite.scan" ] || fail "a turn's Stop hook runs the hourly scan, so long sessions get it too"
662:echo 'not json' | "$CTX" hook || fail "hook on junk input"
663:"$CTX" hook </dev/null || fail "hook on empty input"
670:[ "$("$CTX" scan "$T/projects")" = "1 transcripts read" ] || fail "scan reads a new transcript"
671:[ "$("$CTX" scan "$T/projects")" = "0 transcripts read" ] || fail "scan has nothing left"
672:[ "$("$CTX" sql "SELECT count(*) FROM ctx_chunk WHERE turn='sc-1'")" = 1 ] || fail "scan stored the turn"
718:[ "$("$CTX" sql "SELECT count(*) FROM ctx_chunk WHERE body LIKE '%[REDACTED] end'")" = 30 ] || fail "every prefixed rule redacts"
726:[ "$("$CTX" sql "SELECT count(*) FROM ctx_chunk WHERE turn='gl-1' AND body LIKE '%xxxxxxxxxxxx%'")" = 0 ] || fail "a token glued to another is still redacted"
727:[ "$("$CTX" sql "SELECT count(*) FROM ctx_chunk WHERE body LIKE '%aB3dE6gH9%' OR body LIKE '%AB3DE6GH9%' OR body LIKE '%abc123abc123%'")" = 0 ] || fail "a value was stored"
740:[ "$("$CTX" sql "SELECT count(*) FROM ctx_chunk WHERE body LIKE '%plainvalue%'")" = 0 ] || fail "secret manager output is redacted"
741:[ "$("$CTX" sql "SELECT count(*) FROM ctx_chunk WHERE turn='dj-1' AND body LIKE '%keep this text%'")" = 1 ] || fail "ordinary raw fields stay"
742:"$CTX" sql "SELECT body FROM ctx_chunk WHERE turn='dj-1'" | grep -q '"computed":"\[REDACTED\]"' || fail "the redaction marker is written"
767:if "$CTX" audit --limit 20 > "$T/a1.txt"; then fail "audit exits 1 with open findings"; fi
768:head -1 "$T/a1.txt" | grep -q '6 turns scanned' || fail "audit scans every turn"
769:grep -q 'github-token' "$T/a1.txt" || fail "audit finds a token"
770:grep -q 'entropy' "$T/a1.txt" || fail "audit finds a high-entropy value"
771:grep -c 'turn 5 ' "$T/a1.txt" | grep -q '^0$' || fail "placeholders are not findings"
772:if "$CTX" audit --known --limit 20 < "$T/seed.sql.known" > "$T/a2.txt"; then fail "known exits 1"; fi
773:grep -q 'known .*line 1, len=21' "$T/a2.txt" || fail "known value located"
775:  if grep -qF -- "$v" "$T/a1.txt" "$T/a2.txt"; then fail "audit printed a value"; fi
777:if "$CTX" sql "SELECT shape || fingerprint FROM ctx_audit_finding" | grep -qF -f "$T/seed.sql.values"; then fail "audit stored a value"; fi
780:out=$("$CTX" audit --limit 0 || true); echo "$out" | head -1 | grep -q '^0 turns scanned, 0 new' || fail "audit resumes where it stopped"
782:"$CTX" audit --limit 0 > "$T/a3.txt" || fail "audit exits 0 when nothing is open"
783:out=$("$CTX" audit --all --limit 0) || fail "allowed findings stay allowed on a full rescan"
784:[ "$("$CTX" sql "SELECT count(*) FROM ctx_audit_run")" -ge 4 ] || fail "runs are counted over time"
787:grep -q 'ticket .* Audit: entropy findings' "$T/a4.txt" || fail "findings become tickets"
789:if grep -q '^ticket ' "$T/a5.txt"; then fail "one ticket per rule at a time"; fi
790:[ "$("$CTX" ticket ready --all | grep -c 'Audit: entropy')" = 1 ] || fail "the ticket is listed"
800:[ "$("$CTX" ticket ready | wc -l | tr -d ' ')" = 1 ] || fail "a blocked ticket is not ready"
801:"$CTX" ticket ready | grep -q "^$A proj Task First job" || fail "ready lists the ticket"
802:[ "$("$CTX" ticket ready --all | wc -l | tr -d ' ')" = 2 ] || fail "ready --all"
803:if "$CTX" ticket claim "$B" 2>/dev/null; then fail "a blocked ticket cannot be claimed"; fi
804:"$CTX" ticket offer > "$T/offer.json"; python3 -c "import json,sys; d=json.load(open(sys.argv[1])); assert 'First job' in d['hookSpecificOutput']['additionalContext']; assert 'Elsewhere' not in d['hookSpecificOutput']['additionalContext']" "$T/offer.json" || fail "offer lists this project"
805:[ -z "$(cd "$T" && mkdir -p idle && cd idle && "$CTX" ticket offer)" ] || fail "offer is silent with no work"
806:python3 - "$CTX" "$A" <<'EOF' || fail "only one claim wins"
812:"$CTX" ticket show "$A" | grep -q 'Claimed' || fail "ticket is claimed"
813:"$CTX" ticket show "$A" | grep -q '^assignee: agent' || fail "claim records who"
814:"$CTX" ticket show "$A" | grep -q '^details here' || fail "show has the body"
815:[ "$("$CTX" ticket ready | wc -l | tr -d ' ')" = 0 ] || fail "a claimed ticket is not ready"
817:"$CTX" ticket show "$A" | grep -q '^result: abc123' || fail "done records the result"
818:"$CTX" ticket ready | grep -q 'Second job' || fail "finishing a blocker readies the next"
820:[ "$("$CTX" ticket ready --all | wc -l | tr -d ' ')" = 1 ] || fail "dropped tickets leave the list"
821:if "$CTX" ticket claim nosuch 2>/dev/null; then fail "unknown ticket"; fi
822:if "$CTX" ticket new "bad type" --type Nope 2>/dev/null; then fail "ticket type is checked"; fi
831:"$CTX" project add my-proj | grep -q 'repo:myproj' || fail "project add maps the name"
832:mkdir -p "$T/other/two"; (cd "$T/other" && "$CTX" project add my-proj-two --root "$T/other/two" >/dev/null) || fail "a name extending another one works"
833:if "$CTX" project add my-proj 2>/dev/null; then fail "a project is added once"; fi
835:(cd "$T/other" && "$CTX" project add imported --import "$T/export.fossil" >/dev/null) || fail "project import"
836:[ "$("$CTX" project list | wc -l | tr -d ' ')" = 3 ] || fail "project list"
837:if "$CTX" project add repo --root "$T/other" 2>/dev/null; then fail "reserved repository names"; fi
842:if "$CTX" source add "$T/design.md" --class evergreen 2>/dev/null; then fail "origin is required"; fi
843:if "$CTX" source add "$T/design.md" --origin "x" 2>/dev/null; then fail "class is required"; fi
844:if "$CTX" source add "$T/design.md" --origin "x" --class stale 2>/dev/null; then fail "class is checked"; fi
845:if "$CTX" source add "$T/design.md" --origin "x" --class perishable 2>/dev/null; then fail "perishable needs a review date"; fi
846:if "$CTX" source add "$T/design.md" --origin "x" --class evergreen --as ../escape.md 2>/dev/null; then fail "paths stay inside sources"; fi
847:if (cd "$T" && "$CTX" source add "$T/design.md" --origin x --class evergreen 2>/dev/null); then fail "needs a registered project"; fi
848:(cd sub && "$CTX" source add "$T/design.md" --origin "https://example.test/design" --class evergreen --as notes/design.md --how fetched >/dev/null) || fail "source add"
849:cmp -s "$T/design.md" sources/notes/design.md || fail "bytes are kept"
850:python3 - sources/notes/design.md <<'EOF' || fail "origin record"
858:"$CTX" source add "$T/design.md" --origin other --class evergreen --as notes/design.md | grep -q 'already recorded' || fail "adding again changes nothing"
859:grep -q 'example.test' sources/notes/design.md.origin.json || fail "the first origin stands"
861:if "$CTX" source add "$T/changed.md" --origin x --class evergreen --as notes/design.md 2>/dev/null; then fail "a source is not edited in place"; fi
862:"$CTX" source check | grep -q '^1 sources, 0 problems' || fail "check passes"
864:"$CTX" source check > "$T/c1.txt" || fail "due is not a failure"
865:grep -q 'due for review: sources/old.md' "$T/c1.txt" || fail "due is reported"
867:if "$CTX" source check > "$T/c2.txt"; then fail "tampering fails the check"; fi
868:grep -q 'it changed after its origin' "$T/c2.txt" || fail "tampering is named"
870:"$CTX" source check | grep -q 'no origin recorded for sources/bare.md' || fail "missing origin is named"
913:out=$(cd "$T/ix/root" && "$CTX" index); echo "$out" | grep -q '^1 sources, 2 wiki pages, 3 changed, 3 citations, 2 failing' || fail "index counts: $out"
914:"$CTX" sql "SELECT count(*) FROM ctx_doc WHERE name LIKE '%origin.json'" | grep -q '^0' || fail "origin files are not indexed as sources"
916:[ "$(q "SELECT status FROM ctx_citation WHERE ref='a'")" = ok ] || fail "a quote is found across a line break"
917:[ "$(q "SELECT status FROM ctx_citation WHERE ref='b'")" = quote-missing ] || fail "a missing quote is caught"
918:[ "$(q "SELECT status FROM ctx_citation WHERE ref='c'")" = no-source ] || fail "a citation to a session not in this repository fails"
919:[ "$(q "SELECT ci FROM ctx_citation WHERE ref='a'")" = "$CI" ] || fail "citation pins the check-in"
920:[ "$(q "SELECT length(file_hash) FROM ctx_citation WHERE ref='a'")" = 64 ] || fail "citation pins a full hash"
921:[ "$(q "SELECT count(*) FROM ctx_link WHERE kind='links_to'")" = 2 ] || fail "page links, ignoring code"
922:[ "$(q "SELECT count(*) FROM ctx_link WHERE kind='compiled_from' AND dst_name='sources/sub/design.md'")" = 1 ] || fail "compiled_from link"
923:[ "$(q "SELECT count(*) FROM ctx_stale_wiki")" = 0 ] || fail "nothing is stale yet"
924:[ "$(q "SELECT group_concat(heading) FROM (SELECT heading FROM ctx_chunk c JOIN ctx_doc d ON d.id=c.doc_id WHERE d.kind='source' ORDER BY seq)")" = "Design,Other" ] || fail "chunks by heading"
925:(cd "$T/ix/root" && "$CTX" search walrus | grep -q 'source Other') || fail "sources are searchable"
926:out=$(cd "$T/ix/root" && "$CTX" index); echo "$out" | grep -q ' 2 changed\| 0 changed' || fail "an unchanged source is not rewritten: $out"
932:[ "$(q "SELECT count(*) FROM ctx_stale_wiki")" = 1 ] || fail "a changed source makes the page stale"
933:[ "$(q "SELECT status FROM ctx_citation WHERE ref='a'")" = ok ] || fail "a pinned citation still resolves at its version"
936:[ "$(q "SELECT count(*) FROM ctx_doc WHERE kind='source'")" = 0 ] || fail "a removed source leaves the index"
937:[ "$(q "SELECT count(*) FROM ctx_chunk c JOIN ctx_doc d ON d.id=c.doc_id WHERE d.kind='source'")" = 0 ] || fail "its chunks go too"
938:[ "$(q "SELECT count(*) FROM ctx_stale_wiki")" = 1 ] || fail "a page citing a removed source is stale"
945:"$CTX" project pull kb2 --from "$T/ix/upstream.fossil" | grep -q 'received' || fail "pull"
946:(cd "$T/ix/root" && "$CTX" project add kb3 --root "$T/ix" --clone "$T/ix/upstream.fossil" >/dev/null) || fail "clone from a repository"
947:"$CTX" project pull kb3 | grep -q 'received' || fail "a clone remembers where to pull from"
948:if "$CTX" project pull kb2 2>/dev/null; then fail "pull needs a source"; fi
1008:refused() { page "$1"; if "$CTX" wiki save Cache "$T/wk/p.md" --model m > "$T/wk/o.txt" 2>&1; then fail "gate let through: $1"; fi; grep -q "$2" "$T/wk/o.txt" || fail "gate reason for $1: $(cat "$T/wk/o.txt")"; }
1009:[ "$(w_repo "fsl_wiki('Cache') IS NULL")" = 1 ] || fail "no page yet"
1011:if "$CTX" wiki save Cache "$T/wk/p.md" 2>/dev/null; then fail "--model is required"; fi
1012:"$CTX" wiki save Cache "$T/wk/p.md" --model test-model --ticket abc --session sess1 | grep -q '^saved Cache' || fail "a good page saves"
1015:[ "$("$CTX" sql "SELECT count(*) FROM fslrepo_kb_backlink WHERE srctype = 2 AND target = '$CI'")" = 1 ] || fail "a saved page records a backlink to the check-in it cites: $("$CTX" sql "SELECT target, srctype FROM fslrepo_kb_backlink")"
1021:printf '# Other\n\nBack to [Cache](/wiki?name=Cache).\n' > "$T/wk/op.md"; "$CTX" wiki save "Other Page" "$T/wk/op.md" --model m >/dev/null || fail "a second page saves"
1022:printf '# Spaced\n\nSee [Other](/wiki?name=Other Page).\n' > "$T/wk/sp.md"; "$CTX" wiki save Spaced "$T/wk/sp.md" --model m >/dev/null || fail "a raw space in a link is accepted"
1023:w_repo "fsl_wiki('Spaced')" | grep -q '(/wiki?name=Other+Page)' || fail "a raw space in a link is encoded"
1029:page synth2; "$CTX" wiki save Cache "$T/wk/p.md" --model m >/dev/null || fail "synthesis citing both saves"
1030:page session; "$CTX" wiki save Cache "$T/wk/p.md" --model m >/dev/null || fail "session and ticket citations verify"
1032:page good; "$CTX" wiki save Cache "$T/wk/p.md" --model test-model --ticket abc --session sess1 | grep -q 'removed claim: "chose a thirty' || fail "dropped claims are reported"
1034:grep -q '^## Provenance' "$T/wk/saved.md" && grep -q 'Compiled by: test-model' "$T/wk/saved.md" && grep -q 'Ticket: abc' "$T/wk/saved.md" || fail "provenance block"
1035:grep -q "^- \[sources/a.md @ ${CI%${CI#????????????}}\](/info/$CI)" "$T/wk/saved.md" || fail "sources section links the check-in"
1036:[ "$(grep -c '^## Provenance' "$T/wk/saved.md")" = 1 ] || fail "provenance is replaced, not repeated"
1037:if grep -q sess1 "$T/wk/saved.md"; then fail "a shared page must not name a private session"; fi
1038:[ "$("$CTX" sql "SELECT session FROM ctx_wiki_log ORDER BY id DESC LIMIT 1")" = sess1 ] || fail "session is in the private log"
1040:w_repo "cast(fsl_content('$UUID') as text)" | grep -q '^N text/x-markdown' || fail "saved as Markdown"
1042:grep -q '^\[^a\] Entries live thirty seconds' "$T/wk/why.txt" || fail "why shows the claim: $(head -3 "$T/wk/why.txt")"
1043:grep -q '^\[^b\] Evictions vs. e.g. expiry are nightly.$' "$T/wk/why.txt" || fail "why keeps a sentence whole across abbreviations: $(grep '^\[^b' "$T/wk/why.txt")"
1044:grep -q 'quote: "The cache TTL is 30 seconds."' "$T/wk/why.txt" || fail "why shows the quote"
1045:grep -q 'origin: https://example.test/a, retrieved' "$T/wk/why.txt" || fail "why reaches the origin"
1046:"$CTX" source uses a.md | grep -q '^2 claims depend on sources/a.md' || fail "source uses counts claims"
1047:"$CTX" wiki check > "$T/wk/c.txt" || fail "check passes: $(cat "$T/wk/c.txt")"
1048:grep -q '^0 problems' "$T/wk/c.txt" || fail "check is clean"
1051:if "$CTX" wiki check > "$T/wk/c2.txt"; then fail "a stale page fails the check"; fi
1052:grep -q 'Cache: stale, source changed: sources/a.md' "$T/wk/c2.txt" || fail "stale is named"
1053:"$CTX" source uses a.md | grep -q 'older version' || fail "uses shows an older pin"
1088:"$CTX" wiki save Limits "$T/rt/p.md" --model m >/dev/null || fail "first page saves"
1091:if ev old.md --relation replaces --reason r 2>/dev/null; then fail "replaces needs a successor"; fi
1092:if ev old.md --relation replaces --by new.md 2>/dev/null; then fail "a reason is required"; fi
1093:if ev old.md --relation nonsense --by new.md --reason r 2>/dev/null; then fail "relation is checked"; fi
1094:if ev old.md --relation retracts --by new.md --reason r 2>/dev/null; then fail "a retraction has no successor"; fi
1095:if ev old.md --relation replaces --by nosuch.md --reason r 2>/dev/null; then fail "successor needs an origin"; fi
1096:if ev old.md --relation replaces --by old.md --reason r 2>/dev/null; then fail "no self supersession"; fi
1097:ev old.md --relation replaces --by new.md --reason "heron timeout raised" >/dev/null || fail "supersede"
1098:ev bad.md --relation retracts --reason "was wrong" >/dev/null || fail "retract"
1099:if ev old.md --relation replaces --by new.md --reason again 2>/dev/null; then fail "superseded once"; fi
1100:if ev new.md --relation replaces --by old.md --reason cycle 2>/dev/null; then fail "a successor must be current"; fi
1101:(cd "$T/rt/co" && "$CTX" source check) | grep -q ' 0 problems' || fail "the events file is not a source"
1102:[ "$(wc -l < "$T/rt/co/sources/SUPERSEDED.jsonl" | tr -d ' ')" = 2 ] || fail "events are appended"
1106:[ "$(q "SELECT status FROM ctx_doc WHERE name='sources/old.md'")" = superseded ] || fail "old is superseded"
1107:[ "$(q "SELECT status FROM ctx_doc WHERE name='sources/bad.md'")" = retracted ] || fail "bad is retracted"
1108:[ "$("$CTX" search zebra | grep -c '^#[0-9]* kb source')" = 1 ] || fail "only the current source is served"
1109:"$CTX" search albatross 2>/dev/null | grep -q '^#[0-9]* kb source' && fail "a retracted source is not served"
1110:"$CTX" search --history albatross | grep -q '^#[0-9]* kb source' || fail "--history brings it back"
1111:if "$CTX" wiki check > "$T/rt/c.txt"; then fail "check sees superseded citations"; fi
1112:grep -q 'cites a superseded source' "$T/rt/c.txt" && grep -q 'cites a retracted source' "$T/rt/c.txt" || fail "check names them"
1114:page2; if "$CTX" wiki save Limits "$T/rt/p2.md" --model m > "$T/rt/g.txt"; then fail "the gate refuses a superseded source"; fi
1115:grep -q 'cites a superseded source' "$T/rt/g.txt" || fail "gate names supersession"
1117:grep -q '^1 re-pinned, 2 tickets opened' "$T/rt/r.txt" || fail "repin: $(cat "$T/rt/r.txt")"
1119:grep -q 'name=sources/new.md' "$T/rt/s.md" || fail "a surviving quote moves to the successor"
1120:grep -q '\[^b\]: .*name=sources/old.md' "$T/rt/s.md" || fail "a lost quote keeps its old pin"
1121:[ "$("$CTX" ticket ready --all | grep -c 'Wiki: re-source')" = 2 ] || fail "lost quotes become tickets"
1122:"$CTX" wiki repin | grep -q '^0 re-pinned, 0 tickets' || fail "repin is idempotent"
1123:[ "$("$CTX" ticket ready --all | grep -c 'Wiki: re-source')" = 2 ] || fail "tickets are not repeated"
1127:"$CTX" search --as-of 2026-03-01 heron | grep -q '^#[0-9]* kb source' || fail "as-of sees the old source"
1128:[ "$("$CTX" search --as-of 2026-03-01 heron | grep -c '^#')" = 1 ] || fail "as-of excludes what was not yet valid"
1129:[ "$("$CTX" search --as-of 2026-08-01 heron | grep -c '^#')" = 1 ] || fail "as-of respects a later supersession"
1131:[ "$("$CTX" search --as-of 2026-08-01 heron | grep -c '^#')" = 1 ] || fail "as-of serves a source with no valid-from date"
1134:if "$CTX" wiki retire Limits 2>/dev/null; then fail "retirement needs a reason"; fi
1135:"$CTX" wiki retire Limits --reason "no longer relevant" | grep -q 'retired' || fail "retire"
1136:r_repo "fsl_wiki('Limits')" | head -1 | grep -q '^> \*\*Retired' || fail "the page carries a banner"
1137:[ "$(q "SELECT status FROM ctx_doc WHERE kind='wiki' AND name='Limits'")" = retired ] || fail "retired status"
1138:"$CTX" search limit 2>/dev/null | grep -q '^#[0-9]* kb wiki' && fail "a retired page is not served"
1139:"$CTX" search --history limit | grep -q '^#[0-9]* kb wiki' || fail "--history finds a retired page"
1141:[ "$(q "SELECT status FROM ctx_doc WHERE kind='wiki' AND name='Limits'")" = retired ] || fail "retirement survives a rebuild"
1142:[ "$(q "SELECT count(*) FROM fslrepo_kb_event WHERE type='w'")" -ge 2 ] || fail "history is kept"
1143:"$CTX" wiki restore Limits | grep -q 'restored' || fail "restore"
1144:if r_repo "fsl_wiki('Limits')" | head -1 | grep -q 'Retired'; then fail "restore removes the banner"; fi
1149:grep -Eq '^[0-9.]+ sources/(bad|old).md' "$T/rt/q.txt" || fail "queue lists stale sources: $(cat "$T/rt/q.txt")"
1150:grep -q 'sources/new.md' "$T/rt/q.txt" && fail "a current source is not queued"
1153:"$CTX" wiki publish | grep -q ' 2 generated pages updated' || fail "publish writes both pages"
1154:"$CTX" wiki publish | grep -q ' 0 generated pages updated' || fail "publish is stable"
1155:r_repo "fsl_wiki('Wiki Index')" | grep -q '^- \[Limits\](/wiki?name=Limits): 3 claims' || fail "index lists the page"
1156:r_repo "fsl_wiki('Wiki Health')" | grep -q 'sources/bad.md' || fail "health lists the queue"
1157:[ "$(q "SELECT count(*) FROM ctx_doc WHERE kind='wiki' AND name LIKE 'Wiki %'")" = 0 ] || fail "generated pages are not indexed"
1158:if "$CTX" wiki save "Wiki Index" "$T/rt/p.md" --model m 2>/dev/null; then fail "generated pages cannot be saved by hand"; fi
1172:grep -q 'needs a person: Dead Page' "$T/rt/a.txt" || fail "linked page needs a person: $(cat "$T/rt/a.txt")"
1173:grep -q 'would retire: Kept Page' "$T/rt/a.txt" || fail "unused page is eligible: $(cat "$T/rt/a.txt")"
1174:[ "$(q "SELECT count(*) FROM ctx_doc WHERE kind='wiki' AND status='retired'")" = 0 ] || fail "a dry run changes nothing"
1176:[ "$(q "SELECT status FROM ctx_doc WHERE name='Kept Page'")" = retired ] || fail "auto retires the eligible page"
1177:[ "$(q "SELECT coalesce(status,'current') FROM ctx_doc WHERE name='Dead Page'")" = current ] || fail "a linked page stays"
1178:"$CTX" ticket ready --all | grep -q 'decide whether to retire Dead Page' || fail "a person is asked"
1188:"$CTX" remote me "$T/hubdir/sub/hub.fossil" | grep -q 'copied repo:me' || fail "a remote that does not exist yet is created from this machine"
1189:[ "$(ls -l "$T/hubdir/sub/hub.fossil" | cut -c1-10)" = "-rw-------" ] || fail "the copy of the private archive is private"
1191:[ -s "$T/hub.fossil" ] || fail "the copy is written"
1193:"$CTX" remote me "$T/hub.fossil" | grep -q 'copied' && fail "an existing remote is never overwritten"
1194:cmp -s "$T/hub.fossil" "$T/hub.before" || fail "an existing remote is left as it is"
1197:"$CTX" sync >/dev/null || fail "first machine syncs"
1199:"$CTX" init --clone "$T/hub.fossil" | grep -q '1 sessions from other machines indexed' || fail "a clone indexes the other machine's sessions"
1200:"$CTX" search --all quokka | grep -q 'quokka' || fail "the other machine's session is searchable"
1201:"$CTX" rules --project syncproj | grep -q 'Quokka migrations' || fail "a rule syncs to another machine"
1202:[ "$("$CTX" sql "SELECT remote FROM ctx_repo WHERE name='me'")" = "$T/hub.fossil" ] || fail "a clone remembers its remote"
1203:if "$CTX" init --clone "$T/hub.fossil" 2>/dev/null; then fail "a private repository is cloned once"; fi
1209:"$CTX" sync >/dev/null || fail "push"
1212:"$CTX" sync | grep -q 'sessions from other machines indexed' || fail "a pull indexes new sessions"
1213:"$CTX" search --all wombat | grep -q 'wombat' || fail "a new turn arrives"
1214:[ "$("$CTX" sql "SELECT count(*) FROM ctx_chunk WHERE body LIKE '%platypus%' AND body NOT LIKE '%wombat%'")" = 1 ] || fail "a turn with no timestamp stays its own turn"
1215:"$CTX" search --all echidna | grep -q echidna || fail "text after a NUL byte arrives"
1216:[ "$("$CTX" sql "SELECT count(*) FROM ctx_doc WHERE kind='session'")" = "$NOTES" ] || fail "one session per technote, not duplicated"
1217:"$CTX" sync | grep -q 'sessions from other machines' && fail "an unchanged note is not indexed again"
1218:"$CTX" ticket claim "$TK" --as machine2 >/dev/null || fail "machine 2 claims"
1220:if "$CTX" ticket claim "$TK" --as machine1 2>"$T/claim.err" >/dev/null; then fail "a claim made on another machine is seen first"; fi
1221:grep -q 'Claimed by machine2' "$T/claim.err" || fail "the other machine's claim is named: $(cat "$T/claim.err")"
1223:[ "$("$CTX" ticket show "$TK" | sed -n 's/^assignee: //p')" = machine2 ] || fail "the first claim stands"
1224:if "$CTX" sync nosuch 2>/dev/null; then fail "sync of an unknown name"; fi
1239:printf '%s' "$out" | python3 -c "import json,sys; c=json.load(sys.stdin)['hookSpecificOutput']['additionalContext']; assert 'flamingo cache stalls' in c, c; assert 'another project' not in c; assert 'not instructions' in c" || fail "inject finds the strong match in this project only: $out"
1240:printf '%s' "$out" | grep -q 'why does the flamingo' && fail "the current session is not injected"
1241:[ -z "$(inj aa000004-now 'lunch')" ] || fail "a one-word prompt gets nothing"
1242:[ -z "$(inj aa000004-now 'please run the tests again')" ] || fail "a prompt with no strong match gets nothing"
1243:[ -z "$(echo 'not json' | "$CTX" inject)" ] || fail "junk input gets nothing"
1244:CTX_DB=/nonexistent/x.sqlite "$CTX" inject < /dev/null || fail "inject never fails a prompt"
1248:rc "Do you remember why the flamingo cache stalls?" | python3 -c "import json,sys; c=json.load(sys.stdin)['hookSpecificOutput']; assert c['hookEventName'] == 'UserPromptSubmit' and 'ctx search' in c['additionalContext'], c" || fail "recall fires on a reference to earlier work"
1249:[ -n "$(rc "Didn\u2019t we already land that?")" ] || fail "recall reads a typographic apostrophe"
1250:[ -z "$(rc "fix the build and run the tests")" ] || fail "recall says nothing for an ordinary prompt"
1251:[ -z "$(rc "<task-notification> do you remember")" ] || fail "recall ignores harness text"
1252:[ -z "$(rc "$(printf 'x%.0s' $(seq 1 450)) do you remember")" ] || fail "recall ignores a cue deep in pasted text"
1253:[ -z "$(echo 'not json' | "$CTX" recall)" ] || fail "recall ignores junk input"
1254:[ -z "$(printf '{"prompt":"do you remember"}' | CTX_DB=/nonexistent/x.sqlite "$CTX" recall)" ] || fail "recall says nothing without an archive"
1284:[ "$("$CTX" sql "SELECT count(*), sum(turn GLOB 'note:*') FROM ctx_chunk" | tr '\t' ' ')" = "6 3" ] || fail "the fixture holds each turn twice: $("$CTX" sql "SELECT count(*), sum(turn GLOB 'note:*') FROM ctx_chunk")"
1287:grep -q '3 turns stored twice removed' "$T/tidy.err" || fail "the pass reports what it removed: $(cat "$T/tidy.err")"
1288:[ "$("$CTX" sql "SELECT count(*), sum(turn GLOB 'note:*') FROM ctx_chunk" | tr '\t' ' ')" = "3 0" ] || fail "each turn is indexed once, under its own id"
1289:[ "$("$CTX" sql "SELECT count(*) FROM ctx_chunk WHERE body LIKE '%0123456789abcdef%'")" = 0 ] && [ "$("$CTX" sql "SELECT count(*) FROM ctx_chunk WHERE body LIKE '%/v/k#[REDACTED]%'")" = 1 ] || fail "an indexed turn is redacted again"
1292:[ "$(grep -c '## ' "$T/tidy.note")" = 3 ] && ! grep -q 0123456789abcdef "$T/tidy.note" && grep -q '/v/k#\[REDACTED\]' "$T/tidy.note" || fail "the stored technote holds each turn once, redacted: $(grep -c '## ' "$T/tidy.note")"
1293:"$CTX" scan "$T/tidy/none" 2>"$T/tidy.err" >/dev/null; [ ! -s "$T/tidy.err" ] || fail "the pass runs once: $(cat "$T/tidy.err")"
1297:[ "$("$CTX" sql "SELECT count(*), group_concat(turn) FROM (SELECT turn FROM ctx_chunk ORDER BY seq)" | tr '\t' ' ')" = "3 u-1,a-1,u-2" ] || fail "a turn known by a placeholder id is adopted, not stored twice: $("$CTX" sql "SELECT count(*), group_concat(turn) FROM ctx_chunk")"
1301:[ "$("$CTX" search --all --jsonish 2>&1 | grep -c 'unknown option')" = 0 ] || fail "an unrecognised --word is a search word, not an error"
1302:"$CTX" search --all -- --limit --all capybara 2>&1 | grep -q 'unknown option\|must be' && fail "after --, option names are search words"
1309:[ "$(grep -c '^B ' "$CTX_DB.calls")" = 2 ] && [ "$(grep -c '^E ' "$CTX_DB.calls")" = 2 ] || fail "a hook and a search are logged, other commands are not: $(cat "$CTX_DB.calls")"
1310:"$CTX" status | grep -q '^ctx 1 search · hooks [0-9]* ms$' || fail "status gives one short line: $("$CTX" status)"
1314:awk '$1 == "rules-offer" && $2 == 3 && $5 == 4200 && $6 == 1 && $7 == 1 { ok = 1 } END { exit !ok }' "$T/hooks.txt" || fail "a start with no end is cut off, and a slow end is over its limit: $(cat "$T/hooks.txt")"
1315:"$CTX" status | grep -q '2 cut off' || fail "status warns when hooks were cut off: $("$CTX" status)"
1316:CTX_DB="$T/nocalls.sqlite" "$CTX" recall </dev/null; [ ! -e "$T/nocalls.sqlite.calls" ] || fail "nothing is logged without an archive"
1319:[ "$("$CTX" sql "PRAGMA user_version")" = 6 ] || fail "a new file is at the current version"
1320:[ "$("$CTX" sql "SELECT value FROM ctx_meta WHERE key='derived_version'")" = 6 ] || fail "derived version recorded"
1325:"$CTX" rebuild | tail -1 | grep -q '^2 turns' || fail "rebuild reindexes the sessions"
1326:[ "$("$CTX" search --all capybara | grep -c '^#')" = "$before" ] || fail "search finds the same turns after a rebuild"
1327:[ "$("$CTX" sql "SELECT count(*) FROM ctx_chunk WHERE turn IN ('rb-1','rb-2')")" = 2 ] || fail "turn ids survive a rebuild"
1328:"$CTX" archive "$T/ab12cd99-rb.jsonl" | grep -q '^0 turns' || fail "nothing is stored twice after a rebuild"
1329:"$CTX" ticket show "$RT" | grep -q 'Kept across' || fail "tickets are state"
1330:[ "$("$CTX" sql "SELECT count(*) FROM ctx_capture")" = 1 ] || fail "capture state is kept"
1335:grep -q 'rebuilding it once' "$T/rb.err" || fail "an old index is rebuilt on open"
1336:[ "$("$CTX" sql "SELECT value FROM ctx_meta WHERE key='derived_version'")" = 6 ] || fail "then it is current"
1337:"$CTX" search --all capybara 2>"$T/rb2.err" >/dev/null; [ -s "$T/rb2.err" ] && fail "and is not rebuilt again"
1341:"$CTX" search --all capybara 2>"$T/rb3.err" | grep -q capybara || fail "an unversioned file still works"
1342:[ -s "$T/rb3.err" ] && fail "an unversioned file is upgraded without a rebuild"
1343:[ "$("$CTX" sql "PRAGMA user_version")" = 6 ] || fail "and is marked current"
1348:if "$CTX" extract "$T/none.jsonl" 2>/dev/null; then fail "missing file"; fi
set -u
fail() { echo "FAIL [$STEP] line $1: $2" >&2; exit 1; }
sh test/run.sh
all tests passed
