Claude Code runs a hook in the session's current directory, so the
autosave Stop hook's relative script path followed the shell into
subfolders: a session working inside a nested repo saved its transcript
there. autosave_hook.py and start_hook.py now write
`cd "${CLAUDE_PROJECT_DIR:-.}" && ...`; --check flags an entry from
before the fix as outdated and --install replaces it, keeping other
hooks. Smoke test fires both installed hooks from a subfolder.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
147 lines
13 KiB
Bash
Executable file
147 lines
13 KiB
Bash
Executable file
#!/usr/bin/env bash
|
|
# Template smoke test: run the deterministic scripts against the fixture KB and
|
|
# compare with the golden output. Exit non-zero on any drift.
|
|
#
|
|
# tests/smoke.sh # check
|
|
# tests/smoke.sh --update # regenerate the golden files after an intended change
|
|
set -euo pipefail
|
|
cd "$(dirname "$0")/.."
|
|
FIX=tests/fixtures/minimal-kb
|
|
TODAY=2026-09-24
|
|
LINT=.agents/skills/ckb-lint/scripts/lint_report.py
|
|
RETR=.agents/skills/ckb-retrieve/scripts/retrieve_candidates.py
|
|
SAVE=.agents/skills/ckb-add-conversation/scripts/save_conversation.py
|
|
TMP=$(mktemp -d); trap 'rm -rf "$TMP"' EXIT
|
|
|
|
QUICK=.agents/skills/ckb-lint/scripts/lint_quick.sh
|
|
( cd "$FIX" && python3 "../../../$LINT" --json --today "$TODAY" ) > "$TMP/lint.json"
|
|
# The no-Python fallback must agree with the reference on the three counts it covers.
|
|
( cd "$FIX" && python3 "../../../$LINT" --quick --today "$TODAY" || true ) > "$TMP/quick.py.txt"
|
|
( cd "$FIX" && bash "../../../$QUICK" --today "$TODAY" || true ) | sed 's/ (fallback:.*//' > "$TMP/quick.sh.txt"
|
|
python3 "$RETR" "how many times does Atlas retry a failed batch" --root "$FIX" --today "$TODAY" --json > "$TMP/retrieve.json"
|
|
# Conversation rendering must be stable: same fixture transcript, same Markdown.
|
|
mkdir -p "$TMP/conv" && python3 "$SAVE" --from tests/fixtures/conversation/session.jsonl --root "$TMP/conv" --today "$TODAY" --author "Fixture User <fixture@example.com>" > /dev/null
|
|
cp "$TMP"/conv/raw/saved-conversations/"$TODAY"/*.md "$TMP/conversation.md"
|
|
# A title set with --set-title must win over the transcript's own title and rename the saved pair.
|
|
mkdir -p "$TMP/titled" && ENC=$(python3 -c "import re,sys;print(re.sub(r'[^A-Za-z0-9]','-',sys.argv[1]))" "$(cd "$TMP/titled" && pwd -P)") \
|
|
&& mkdir -p "$TMP/cfg/projects/$ENC" && cp tests/fixtures/conversation/session.jsonl "$TMP/cfg/projects/$ENC/"
|
|
( cd "$TMP/titled" && CLAUDE_CONFIG_DIR="$TMP/cfg" python3 "$OLDPWD/$SAVE" --today "$TODAY" --author "Fixture User <fixture@example.com>" >/dev/null \
|
|
&& CLAUDE_CONFIG_DIR="$TMP/cfg" python3 "$OLDPWD/$SAVE" --set-title "Smoke Title" >/dev/null \
|
|
&& CLAUDE_CONFIG_DIR="$TMP/cfg" python3 "$OLDPWD/$SAVE" --today "$TODAY" --author "Fixture User <fixture@example.com>" >/dev/null )
|
|
ls "$TMP/titled/raw/saved-conversations/$TODAY" > "$TMP/titled.txt"
|
|
# Routines: due list is deterministic for the fixture; edits touch only the routine's own lines.
|
|
ROUT=.agents/skills/ckb-routine/scripts/routines.py
|
|
( python3 "$ROUT" --due --root "$FIX" --today "$TODAY" || true ) > "$TMP/routines-due.txt"
|
|
cp "$FIX/ckb.yaml" "$TMP/ckb.yaml"
|
|
python3 "$ROUT" --root "$TMP" --mark weekly-lint --today "$TODAY" >/dev/null
|
|
python3 "$ROUT" --root "$TMP" --add --name smoke --do "Lint" --every 2w >/dev/null
|
|
python3 "$ROUT" --root "$TMP" --remove smoke >/dev/null
|
|
|
|
# Dedup: one copy, one re-saved copy, one cited URL, two inbox twins, one new file.
|
|
DEDUP=.agents/skills/ckb-ingest/scripts/dedup_inbox.py
|
|
mkdir -p "$TMP/dedup" && cp -r "$FIX/." "$TMP/dedup/" && mkdir -p "$TMP/dedup/raw/inbox"
|
|
cp "$FIX/raw/archive/2026-09-01/kickoff-notes.md" "$TMP/dedup/raw/inbox/kickoff-copy.md"
|
|
sed 's/$/\r/' "$FIX/raw/archive/2026-09-02/dlq-review.md" > "$TMP/dedup/raw/inbox/dlq-crlf.md"
|
|
printf '\n## Sources\n\n- `https://www.example.com/atlas/retries/` — etag:abc\n' >> "$TMP/dedup/wiki/projects/atlas.md"
|
|
printf '[InternetShortcut]\nURL=https://example.com/atlas/retries?utm_source=x#top\n' > "$TMP/dedup/raw/inbox/retries.url"
|
|
echo "fresh material" > "$TMP/dedup/raw/inbox/a-new.md" && echo "twin" > "$TMP/dedup/raw/inbox/b-twin.md" && cp "$TMP/dedup/raw/inbox/b-twin.md" "$TMP/dedup/raw/inbox/c-twin.md"
|
|
( python3 "$DEDUP" --root "$TMP/dedup" || true ) | sed "s#$TMP#TMP#g" > "$TMP/dedup.txt"
|
|
# Session start: open threads, hook mode, and the start-hook installer.
|
|
mkdir -p "$TMP/start" && cp -r "$FIX/." "$TMP/start/" && mkdir -p "$TMP/start/workload"
|
|
printf -- '---\ntype: session\n---\n\n## Work\n\n### Open threads\n- [ ] Ask Maria about DLQ size\n- [x] closed one\n\n## Later\n\n- [ ] not under Open threads\n' \
|
|
> "$TMP/start/workload/2026-08-01_fixture-user_summary.md"
|
|
( cd "$TMP/start" && python3 "$OLDPWD/.agents/skills/ckb-lint/scripts/session_start.py" --today "$TODAY" --no-git ) > "$TMP/start-threads.txt"
|
|
echo '{"source":"compact","session_id":"none"}' | ( cd "$TMP/start" && python3 "$OLDPWD/.agents/skills/ckb-lint/scripts/session_start.py" --hook --today "$TODAY" --no-git ) > "$TMP/hook.json"
|
|
mkdir -p "$TMP/start/.claude" && echo '{"hooks":{"Stop":[{"hooks":[{"type":"command","command":"keep me"}]}]}}' > "$TMP/start/.claude/settings.local.json"
|
|
printf '\nsession:\n start_hook: true\n' >> "$TMP/start/ckb.yaml"
|
|
HOOK=.agents/skills/ckb-lint/scripts/start_hook.py
|
|
( cd "$TMP/start" && ! python3 "$OLDPWD/$HOOK" --check >/dev/null && python3 "$OLDPWD/$HOOK" --install >/dev/null \
|
|
&& python3 "$OLDPWD/$HOOK" --check >/dev/null && python3 "$OLDPWD/$HOOK" --remove >/dev/null ) > "$TMP/hook-cycle.txt" 2>&1 && echo ok >> "$TMP/hook-cycle.txt"
|
|
# Hooks run in the session's current directory: both must start from the project root.
|
|
FIXJSONL="$PWD/tests/fixtures/conversation/session.jsonl"
|
|
mkdir -p "$TMP/proj/sub" "$TMP/proj/wiki" "$TMP/proj/.claude" && ln -s "$PWD/.agents" "$TMP/proj/.agents"
|
|
OLDSTOP='python3 .agents/skills/ckb-add-conversation/scripts/save_conversation.py --from-hook >/dev/null 2>&1 || true'
|
|
python3 -c "import json,sys;json.dump({'hooks':{'Stop':[{'hooks':[{'type':'command','command':sys.argv[1]},{'type':'command','command':'keep me'}]}]}},open(sys.argv[2],'w'))" \
|
|
"$OLDSTOP" "$TMP/proj/.claude/settings.local.json"
|
|
printf 'conversations:\n autosave: true\nsession:\n start_hook: true\n' > "$TMP/proj/ckb.yaml"
|
|
( cd "$TMP/proj" && ! python3 .agents/skills/ckb-add-conversation/scripts/autosave_hook.py --check >/dev/null \
|
|
&& python3 .agents/skills/ckb-add-conversation/scripts/autosave_hook.py --install >/dev/null \
|
|
&& python3 .agents/skills/ckb-add-conversation/scripts/autosave_hook.py --check >/dev/null \
|
|
&& python3 .agents/skills/ckb-lint/scripts/start_hook.py --install >/dev/null ) || echo "outdated hook not flagged or not replaced" > "$TMP/hookroot.err"
|
|
hookcmd() { python3 -c "import json,sys;print(next(h['command'] for g in json.load(open(sys.argv[1]))['hooks'][sys.argv[2]] for h in g['hooks'] if sys.argv[3] in h['command']))" \
|
|
"$TMP/proj/.claude/settings.local.json" "$1" "$2"; }
|
|
STOPCMD=$(hookcmd Stop save_conversation.py 2>/dev/null) || true; STARTCMD=$(hookcmd SessionStart session_start.py 2>/dev/null) || true
|
|
( cd "$TMP/proj/sub" && echo "{\"transcript_path\": \"$FIXJSONL\"}" | CLAUDE_PROJECT_DIR="$TMP/proj" sh -c "${STOPCMD:-false}" ) || true
|
|
( cd "$TMP/proj/sub" && echo '{"source":"startup"}' | CLAUDE_PROJECT_DIR="$TMP/proj" sh -c "${STARTCMD:-false}" ) > "$TMP/hookroot.json" || true
|
|
|
|
# Secrets: two planted keys (one in a gitignored export, skipped), a private span in wiki/, then an allowlist line.
|
|
mkdir -p "$TMP/sec" && cp -r "$FIX/." "$TMP/sec/" && mkdir -p "$TMP/sec/workload" "$TMP/sec/outputs/okf"
|
|
echo "aws AKIAABCDEFGHIJKLMNOP here" > "$TMP/sec/workload/2026-09-01_x_summary.md"
|
|
echo "AKIAABCDEFGHIJKLMNOP" > "$TMP/sec/outputs/okf/page.md"
|
|
echo "<private>do not keep</private>" >> "$TMP/sec/wiki/overview.md"
|
|
( cd "$TMP/sec" && python3 "$OLDPWD/$LINT" --secrets || true ) > "$TMP/secrets.txt"
|
|
( cd "$TMP/sec" && python3 "$OLDPWD/$LINT" --quick --today "$TODAY" || true ) > "$TMP/sec.py.txt"
|
|
( cd "$TMP/sec" && bash "$OLDPWD/$QUICK" --today "$TODAY" || true ) | sed 's/ (fallback:.*//' > "$TMP/sec.sh.txt"
|
|
ALLOW=$(grep -o 'add `workload/[^`]*`' "$TMP/secrets.txt" | sed 's/^add `//; s/`$//')
|
|
echo "$ALLOW # smoke: fixture key" > "$TMP/sec/.secrets-allow"
|
|
( cd "$TMP/sec" && python3 "$OLDPWD/$LINT" --secrets || true ) > "$TMP/secrets-allowed.txt"
|
|
# Private spans never reach a saved conversation.
|
|
mkdir -p "$TMP/priv" && sed 's#How many times does Atlas retry a failed batch?#<private>hunter2 is the vault code</private> How many times does Atlas retry a failed batch?#' \
|
|
tests/fixtures/conversation/session.jsonl > "$TMP/priv-session.jsonl"
|
|
python3 "$SAVE" --from "$TMP/priv-session.jsonl" --root "$TMP/priv" --today "$TODAY" --author "Fixture User <fixture@example.com>" > /dev/null
|
|
|
|
if [[ "${1:-}" == "--update" ]]; then
|
|
cp "$TMP/dedup.txt" tests/expected/dedup.txt
|
|
cp "$TMP/lint.json" tests/expected/lint.json
|
|
cp "$TMP/retrieve.json" tests/expected/retrieve.json
|
|
cp "$TMP/conversation.md" tests/expected/conversation.md
|
|
cp "$TMP/routines-due.txt" tests/expected/routines-due.txt
|
|
echo "golden files updated"; exit 0
|
|
fi
|
|
|
|
fail=0
|
|
for f in lint retrieve; do
|
|
if ! diff -u "tests/expected/$f.json" "$TMP/$f.json"; then echo "DRIFT: $f"; fail=1; fi
|
|
done
|
|
if ! diff -u tests/expected/conversation.md "$TMP/conversation.md"; then echo "DRIFT: conversation"; fail=1; fi
|
|
[[ "$(cat "$TMP/titled.txt")" == $'smoke-title_0f1e2d3c.jsonl\nsmoke-title_0f1e2d3c.md' ]] || { echo "set-title: unexpected files: $(cat "$TMP/titled.txt")"; fail=1; }
|
|
grep -q '^# Smoke Title$' "$TMP/titled/raw/saved-conversations/$TODAY/smoke-title_0f1e2d3c.md" || { echo "set-title: heading not applied"; fail=1; }
|
|
if ! diff -u tests/expected/routines-due.txt "$TMP/routines-due.txt"; then echo "DRIFT: routines-due"; fail=1; fi
|
|
[[ "$(diff "$FIX/ckb.yaml" "$TMP/ckb.yaml" | grep -c '^[<>]')" == "2" ]] || { echo "routines: --mark/--add/--remove changed more than the last_run line"; diff "$FIX/ckb.yaml" "$TMP/ckb.yaml"; fail=1; }
|
|
grep -q 'last_run: 2026-09-24' "$TMP/ckb.yaml" || { echo "routines: --mark did not stamp today"; fail=1; }
|
|
if ! diff -u "$TMP/quick.py.txt" "$TMP/quick.sh.txt"; then echo "DRIFT: lint_quick.sh disagrees with lint_report.py --quick"; fail=1; fi
|
|
top=$(python3 -c "import json;print(json.load(open('$TMP/retrieve.json'))['candidates'][0]['path'])")
|
|
[[ "$top" == "wiki/entities/atlas-retry-policy.md" ]] || { echo "retrieve: expected atlas-retry-policy first, got $top"; fail=1; }
|
|
# Session start in one call: the fixture has one due routine and a clean wiki.
|
|
START=.agents/skills/ckb-lint/scripts/session_start.py
|
|
python3 "$START" --root "$FIX" --today "$TODAY" --no-git > "$TMP/start.txt" || { echo "session_start.py failed"; fail=1; }
|
|
grep -q '^ckb check: clean' "$TMP/start.txt" || { echo "session_start: no clean check line"; fail=1; }
|
|
grep -q '^routines: DUE weekly-lint' "$TMP/start.txt" || { echo "session_start: due routine missing"; fail=1; }
|
|
grep -q '^announce: yes' "$TMP/start.txt" || { echo "session_start: announce line missing"; fail=1; }
|
|
# Release 1.22 features.
|
|
if ! diff -u tests/expected/dedup.txt "$TMP/dedup.txt"; then echo "DRIFT: dedup"; fail=1; fi
|
|
# One open thread: the ticked one and the one outside "Open threads" must not count.
|
|
grep -q '^threads: 1 open' "$TMP/start-threads.txt" || { echo "session_start: expected exactly 1 open thread"; fail=1; }
|
|
python3 -c "import json,sys;c=json.load(open(sys.argv[1]))['hookSpecificOutput']['additionalContext'];assert 'ckb check: clean' in c and 'announce: no' in c" "$TMP/hook.json" \
|
|
|| { echo "session_start --hook: bad JSON or wrong announce on compact"; fail=1; }
|
|
[[ "$(tail -1 "$TMP/hook-cycle.txt")" == ok ]] || { echo "start_hook: check/install/remove cycle failed"; cat "$TMP/hook-cycle.txt"; fail=1; }
|
|
grep -q '"keep me"' "$TMP/start/.claude/settings.local.json" || { echo "start_hook: removed a hook it does not own"; fail=1; }
|
|
[[ "$(tail -1 "$TMP/secrets.txt")" == "secrets: 2 possible" ]] || { echo "secrets: expected 2 (key + private span)"; cat "$TMP/secrets.txt"; fail=1; }
|
|
[[ "$(tail -1 "$TMP/secrets-allowed.txt")" == "secrets: 1 possible" ]] || { echo "secrets: allowlist line not honoured"; cat "$TMP/secrets-allowed.txt"; fail=1; }
|
|
if ! diff -u "$TMP/sec.py.txt" "$TMP/sec.sh.txt"; then echo "DRIFT: lint_quick.sh secrets count disagrees with lint_report.py"; fail=1; fi
|
|
! grep -rq hunter2 "$TMP/priv/raw/saved-conversations" || { echo "save_conversation: <private> span was saved"; fail=1; }
|
|
grep -q '<private>\[redacted\]</private>' "$TMP"/priv/raw/saved-conversations/"$TODAY"/*.md || { echo "save_conversation: redaction mark missing"; fail=1; }
|
|
python3 -c "import json,sys;[json.loads(l) for l in open(sys.argv[1]) if l.strip()]" "$TMP"/priv/raw/saved-conversations/"$TODAY"/*.jsonl \
|
|
|| { echo "save_conversation: redacted JSONL is not valid JSON"; fail=1; }
|
|
[[ ! -e "$TMP/hookroot.err" ]] || { cat "$TMP/hookroot.err"; fail=1; }
|
|
python3 -c "import json,sys;h=json.load(open(sys.argv[1]))['hooks']['Stop'];c=[x['command'] for g in h for x in g['hooks']];s=[x for x in c if 'save_conversation' in x];assert c.count('keep me')==1 and len(s)==1 and 'CLAUDE_PROJECT_DIR' in s[0]" \
|
|
"$TMP/proj/.claude/settings.local.json" || { echo "autosave_hook --install: did not replace the outdated entry cleanly"; fail=1; }
|
|
ls "$TMP"/proj/raw/saved-conversations/*/*.md >/dev/null 2>&1 && [[ ! -e "$TMP/proj/sub/raw" ]] \
|
|
|| { echo "autosave hook: run from a subfolder, it did not save into the project root"; fail=1; }
|
|
grep -q 'ckb check:' "$TMP/hookroot.json" || { echo "start hook: run from a subfolder, it printed no brief"; fail=1; }
|
|
# The template must stay inside its own context budget.
|
|
python3 "$LINT" --budget > "$TMP/budget.txt" || { echo "budget: template is over its own always-loaded budget"; head -3 "$TMP/budget.txt"; fail=1; }
|
|
# mcp module: what a remote client may read and write (no MCP SDK needed).
|
|
python3 tests/mcp_policy.py "$FIX" || fail=1
|
|
[[ $fail -eq 0 ]] && echo "smoke: OK"
|
|
exit $fail
|