| 1 | #!/bin/sh |
| 2 | # tests/run.sh - offline checks for plugin_seqthink across all 4 modes. |
| 3 | # |
| 4 | # Runs against ./plugin_seqthink (native build) on the live machine. The --mcp |
| 5 | # leg is driven with a scripted JSON-RPC session on stdin; the CGI leg is |
| 6 | # driven through QUERY_STRING / REQUEST_METHOD / CONTENT_LENGTH; the chain |
| 7 | # store is pointed at a scratch directory through SEQTHINK_STATE_DIR so the |
| 8 | # real node directory is never touched. The last section audits the whole git |
| 9 | # history for secrets and internal addresses. |
| 10 | # |
| 11 | # Exit code 0 = everything passed. |
| 12 | |
| 13 | set -u |
| 14 | |
| 15 | ROOT=$(cd "$(dirname "$0")/.." && pwd) |
| 16 | cd "$ROOT" || exit 1 |
| 17 | |
| 18 | BIN=./plugin_seqthink |
| 19 | PASS=0 |
| 20 | FAIL=0 |
| 21 | TMP=$(mktemp -d 2>/dev/null || echo /tmp/seqthink_test_$$) |
| 22 | mkdir -p "$TMP" |
| 23 | trap 'rm -rf "$TMP"' EXIT |
| 24 | |
| 25 | if [ -t 1 ]; then |
| 26 | C_OK=$(printf '\033[32m'); C_BAD=$(printf '\033[31m'); C_B=$(printf '\033[1m'); C_0=$(printf '\033[0m') |
| 27 | else |
| 28 | C_OK=; C_BAD=; C_B=; C_0= |
| 29 | fi |
| 30 | pass() { PASS=$((PASS + 1)); printf ' %sok%s %s\n' "$C_OK" "$C_0" "$1"; } |
| 31 | fail() { FAIL=$((FAIL + 1)); printf ' %sFAIL%s %s\n' "$C_BAD" "$C_0" "$1"; } |
| 32 | |
| 33 | check() { # check <desc> <expected-substring> <actual> |
| 34 | if printf '%s' "$3" | grep -F -q -- "$2"; then pass "$1"; else |
| 35 | fail "$1 (want: $2)"; printf ' got: %s\n' "$(printf '%s' "$3" | head -c 300)" |
| 36 | fi |
| 37 | } |
| 38 | |
| 39 | check_not() { # check_not <desc> <forbidden-substring> <actual> |
| 40 | if printf '%s' "$3" | grep -F -q -- "$2"; then |
| 41 | fail "$1 (must NOT contain: $2)"; else pass "$1" |
| 42 | fi |
| 43 | } |
| 44 | |
| 45 | section() { printf '\n%s== %s ==%s\n' "$C_B" "$1" "$C_0"; } |
| 46 | body() { printf '%s' "$1" | sed '1,/^\r$/d'; } |
| 47 | |
| 48 | jq_() { # jq_ <desc> <python-expression using d> <json> |
| 49 | printf '%s' "$3" > "$TMP/_j.json" |
| 50 | if python3 - "$TMP/_j.json" "$2" <<'PY' 2>/dev/null |
| 51 | import json, sys |
| 52 | d = json.load(open(sys.argv[1])) |
| 53 | assert eval(sys.argv[2], {"d": d}), sys.argv[2] |
| 54 | PY |
| 55 | then pass "$1"; else fail "$1"; fi |
| 56 | } |
| 57 | |
| 58 | mcp() { # mcp <state-dir> [env assignments...] -- feeds $TMP/in.jsonl |
| 59 | local dir="$1"; shift |
| 60 | SEQTHINK_STATE_DIR="$dir" env "$@" "$BIN" --mcp < "$TMP/in.jsonl" 2>"$TMP/err.txt" |
| 61 | } |
| 62 | |
| 63 | # ---------------------------------------------------------------- build |
| 64 | section "build" |
| 65 | if [ ! -x "$BIN" ]; then |
| 66 | echo " building native binary..." |
| 67 | cc -O2 -std=c99 -Wall -Wextra -I src -I vendor -o "$BIN" src/plugin_seqthink.c vendor/yyjson.c \ |
| 68 | || { echo " build failed"; exit 1; } |
| 69 | fi |
| 70 | [ -x "$BIN" ] && pass "native binary exists and is executable" || fail "native binary missing" |
| 71 | |
| 72 | SRC=src/plugin_seqthink.c |
| 73 | SRC_TXT=$(cat "$SRC") |
| 74 | |
| 75 | # ---------------------------------------------------------------- mode 1: --meta |
| 76 | section "mode 1 --meta (must work without auth - discovery)" |
| 77 | META=$("$BIN" --meta) |
| 78 | check "meta emits name seqthink" '"name":"seqthink"' "$META" |
| 79 | check "meta emits desc" '"desc":"Thought chains"' "$META" |
| 80 | check "meta declares kind mcp" '"kind":"mcp"' "$META" |
| 81 | check "meta declares mcp true" '"mcp":true' "$META" |
| 82 | check "meta declares daemon false" '"daemon":false' "$META" |
| 83 | check "meta declares menus false" '"menus":false' "$META" |
| 84 | check "meta declares verified" '"verified":"live"' "$META" |
| 85 | METAENV=$(SX_PLUGIN_META=1 "$BIN") |
| 86 | check "SX_PLUGIN_META=1 yields same meta" '"name":"seqthink"' "$METAENV" |
| 87 | [ "$META" = "$METAENV" ] && pass "argv and env meta identical" || fail "argv and env meta differ" |
| 88 | if [ "$(printf '%s\n' "$META" | wc -l)" -eq 1 ]; then pass "meta is exactly one line"; else fail "meta is more than one line"; fi |
| 89 | check "--version names the plugin" "plugin_seqthink" "$("$BIN" --version)" |
| 90 | check "--schema is the input schema" '"required":["thought","nextThoughtNeeded","thoughtNumber","totalThoughts"]' \ |
| 91 | "$("$BIN" --schema)" |
| 92 | |
| 93 | section "plugin.json agrees with --meta" |
| 94 | PJ=$(cat plugin.json) |
| 95 | jq_ "plugin.json slug/id" "d['slug']=='seqthink' and d['id']=='plugin_seqthink'" "$PJ" |
| 96 | jq_ "plugin.json category" "d['category']=='devtools'" "$PJ" |
| 97 | jq_ "plugin.json kind/mcp" "d['kind']=='mcp' and d['mcp'] is True" "$PJ" |
| 98 | jq_ "plugin.json daemon/menus" "d['daemon'] is False and d['menus'] is False" "$PJ" |
| 99 | jq_ "plugin.json verified" "d['verified']=='live'" "$PJ" |
| 100 | jq_ "plugin.json has title/summary/trust" "d['title'] and len(d['summary'])>80 and d['trust_notice']" "$PJ" |
| 101 | check_not "plugin.json has no LAN address" "192.168." "$PJ" |
| 102 | |
| 103 | # ---------------------------------------------------------------- mode 2: --mcp |
| 104 | section "mode 2 --mcp handshake" |
| 105 | cat > "$TMP/in.jsonl" <<'JSONL' |
| 106 | {"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"probe","version":"1"}}} |
| 107 | {"jsonrpc":"2.0","method":"notifications/initialized"} |
| 108 | {"jsonrpc":"2.0","id":2,"method":"tools/list"} |
| 109 | {"jsonrpc":"2.0","id":3,"method":"ping"} |
| 110 | {"jsonrpc":"2.0","id":4,"method":"not/a/method"} |
| 111 | JSONL |
| 112 | OUT=$(mcp "$TMP/m_hand" env) |
| 113 | R1=$(printf '%s' "$OUT" | sed -n 1p) |
| 114 | R2=$(printf '%s' "$OUT" | sed -n 2p) |
| 115 | R3=$(printf '%s' "$OUT" | sed -n 3p) |
| 116 | R4=$(printf '%s' "$OUT" | sed -n 4p) |
| 117 | check "initialize answers jsonrpc 2.0" '"jsonrpc":"2.0"' "$R1" |
| 118 | check "initialize echoes id" '"id":1' "$R1" |
| 119 | check "initialize carries protocol" '"protocolVersion":"2024-11-05"' "$R1" |
| 120 | check "initialize advertises tools" '"capabilities":{"tools":{}}' "$R1" |
| 121 | check "initialize names the server" '"name":"seqthink"' "$R1" |
| 122 | check "notifications get no reply" '"id":2' "$R2" |
| 123 | check "tools/list offers sequentialthinking" '"name":"sequentialthinking"' "$R2" |
| 124 | check "tool carries a title" '"title":"Sequential Thinking"' "$R2" |
| 125 | check "tool carries annotations" '"readOnlyHint":false' "$R2" |
| 126 | check "annotations not idempotent" '"idempotentHint":false' "$R2" |
| 127 | check "tool carries inputSchema" '"inputSchema"' "$R2" |
| 128 | check "inputSchema requires 4 fields" '"required":["thought","nextThoughtNeeded","thoughtNumber","totalThoughts"]' "$R2" |
| 129 | check "inputSchema minimum on thoughtNumber" '"thoughtNumber":{"type":"integer"' "$R2" |
| 130 | check "tool carries outputSchema" '"thoughtHistoryLength":{"type":"number"}' "$R2" |
| 131 | check "ping answered empty result" '"result":{}' "$R3" |
| 132 | check "unknown method -> -32601" '"code":-32601' "$R4" |
| 133 | |
| 134 | section "mode 2 --mcp tool: happy path, revision, branch" |
| 135 | cat > "$TMP/in.jsonl" <<'JSONL' |
| 136 | {"jsonrpc":"2.0","id":11,"method":"tools/call","params":{"name":"sequentialthinking","arguments":{"thought":"break the problem down","nextThoughtNeeded":true,"thoughtNumber":1,"totalThoughts":3}}} |
| 137 | {"jsonrpc":"2.0","id":12,"method":"tools/call","params":{"name":"sequentialthinking","arguments":{"thought":"question an earlier step","nextThoughtNeeded":"true","isRevision":true,"revisesThought":1,"thoughtNumber":2,"totalThoughts":3,"needsMoreThoughts":true}}} |
| 138 | {"jsonrpc":"2.0","id":13,"method":"tools/call","params":{"name":"sequentialthinking","arguments":{"thought":"an alternative path","nextThoughtNeeded":"false","branchFromThought":1,"branchId":"alt-route","thoughtNumber":3,"totalThoughts":3}}} |
| 139 | {"jsonrpc":"2.0","id":14,"method":"tools/call","params":{"name":"sequentialthinking","arguments":{"thought":"past the estimate","nextThoughtNeeded":false,"thoughtNumber":9,"totalThoughts":3}}} |
| 140 | JSONL |
| 141 | OUT=$(mcp "$TMP/m_ok" env) |
| 142 | L11=$(printf '%s' "$OUT" | sed -n 1p) |
| 143 | L12=$(printf '%s' "$OUT" | sed -n 2p) |
| 144 | L13=$(printf '%s' "$OUT" | sed -n 3p) |
| 145 | L14=$(printf '%s' "$OUT" | sed -n 4p) |
| 146 | check "call 1 content is text" '"type":"text"' "$L11" |
| 147 | check "call 1 reports thoughtNumber" 'thoughtNumber\":1' "$L11" |
| 148 | check "call 1 history length 1" 'thoughtHistoryLength\":1}' "$L11" |
| 149 | check "call 1 not an error" '"isError":false' "$L11" |
| 150 | check "call 1 has structuredContent" '"structuredContent":{"thoughtNumber":1' "$L11" |
| 151 | check "call 2 accepted a string bool" '"isError":false' "$L12" |
| 152 | check "call 2 history length 2" 'thoughtHistoryLength\":2}' "$L12" |
| 153 | check "call 3 records the branch" 'branches\":[\"alt-route\"' "$L13" |
| 154 | check "call 4 raised totalThoughts" 'totalThoughts\":9' "$L14" |
| 155 | check "call 4 kept the branch list" 'alt-route' "$L14" |
| 156 | check "stderr logged thought 1" 'Thought 1/3' "$(cat "$TMP/err.txt")" |
| 157 | check "stderr marked the revision" 'Revision 2/3 (revising thought 1)' "$(cat "$TMP/err.txt")" |
| 158 | check "stderr marked the branch" 'Branch 3/3 (from thought 1, ID: alt-route)' "$(cat "$TMP/err.txt")" |
| 159 | # (structuredContent parity is asserted in detail below) |
| 160 | printf '%s' "$L11" > "$TMP/_c.json" |
| 161 | if python3 - "$TMP/_c.json" <<'PY' 2>/dev/null |
| 162 | import json, sys |
| 163 | d = json.load(open(sys.argv[1])) |
| 164 | r = d["result"] |
| 165 | sc = r["structuredContent"] |
| 166 | txt = json.loads(r["content"][0]["text"]) |
| 167 | assert sc == txt, (sc, txt) |
| 168 | assert sorted(sc) == ["branches", "nextThoughtNeeded", "thoughtHistoryLength", |
| 169 | "thoughtNumber", "totalThoughts"], sorted(sc) |
| 170 | assert sc["thoughtNumber"] == 1 and sc["totalThoughts"] == 3 |
| 171 | assert sc["nextThoughtNeeded"] is True |
| 172 | assert sc["branches"] == [] and sc["thoughtHistoryLength"] == 1 |
| 173 | PY |
| 174 | then pass "structuredContent == parsed content text, exactly 5 fields" |
| 175 | else fail "structuredContent == parsed content text, exactly 5 fields" |
| 176 | fi |
| 177 | |
| 178 | section "mode 2 --mcp tool: validation errors mirror upstream" |
| 179 | cat > "$TMP/in.jsonl" <<'JSONL' |
| 180 | {"jsonrpc":"2.0","id":21,"method":"tools/call","params":{"name":"sequentialthinking","arguments":{"thought":"","nextThoughtNeeded":true,"thoughtNumber":1,"totalThoughts":1}}} |
| 181 | {"jsonrpc":"2.0","id":22,"method":"tools/call","params":{"name":"sequentialthinking","arguments":{"nextThoughtNeeded":true,"thoughtNumber":1,"totalThoughts":1}}} |
| 182 | {"jsonrpc":"2.0","id":23,"method":"tools/call","params":{"name":"sequentialthinking","arguments":{"thought":"x","nextThoughtNeeded":true,"thoughtNumber":0,"totalThoughts":1}}} |
| 183 | {"jsonrpc":"2.0","id":24,"method":"tools/call","params":{"name":"sequentialthinking","arguments":{"thought":"x","nextThoughtNeeded":true,"thoughtNumber":"abc","totalThoughts":1}}} |
| 184 | {"jsonrpc":"2.0","id":25,"method":"tools/call","params":{"name":"sequentialthinking","arguments":{"thought":"x","nextThoughtNeeded":true,"thoughtNumber":1,"totalThoughts":0}}} |
| 185 | {"jsonrpc":"2.0","id":26,"method":"tools/call","params":{"name":"sequentialthinking","arguments":{"thought":"x","nextThoughtNeeded":"yes","thoughtNumber":1,"totalThoughts":1}}} |
| 186 | {"jsonrpc":"2.0","id":27,"method":"tools/call","params":{"name":"sequentialthinking","arguments":{"thought":"x","thoughtNumber":1,"totalThoughts":1}}} |
| 187 | {"jsonrpc":"2.0","id":28,"method":"tools/call","params":{"name":"sequentialthinking","arguments":{"thought":"x","nextThoughtNeeded":true,"isRevision":"maybe","thoughtNumber":1,"totalThoughts":1}}} |
| 188 | {"jsonrpc":"2.0","id":29,"method":"tools/call","params":{"name":"sequentialthinking","arguments":{"thought":"x","nextThoughtNeeded":true,"revisesThought":0,"thoughtNumber":1,"totalThoughts":1}}} |
| 189 | {"jsonrpc":"2.0","id":30,"method":"tools/call","params":{"name":"sequentialthinking","arguments":{"thought":"x","nextThoughtNeeded":true,"branchFromThought":-2,"thoughtNumber":1,"totalThoughts":1}}} |
| 190 | {"jsonrpc":"2.0","id":31,"method":"tools/call","params":{"name":"sequentialthinking","arguments":{"thought":"x","nextThoughtNeeded":true,"branchId":42,"thoughtNumber":1,"totalThoughts":1}}} |
| 191 | {"jsonrpc":"2.0","id":32,"method":"tools/call","params":{"name":"sequentialthinking","arguments":{"thought":"x","nextThoughtNeeded":true,"needsMoreThoughts":"perhaps","thoughtNumber":1,"totalThoughts":1}}} |
| 192 | {"jsonrpc":"2.0","id":33,"method":"tools/call","params":{"name":"nope","arguments":{}}} |
| 193 | {"jsonrpc":"2.0","id":34,"method":"tools/call","params":{"name":"sequentialthinking"}} |
| 194 | JSONL |
| 195 | OUT=$(mcp "$TMP/m_err" env) |
| 196 | E1=$(printf '%s' "$OUT" | sed -n 1p) |
| 197 | E2=$(printf '%s' "$OUT" | sed -n 2p) |
| 198 | E3=$(printf '%s' "$OUT" | sed -n 3p) |
| 199 | E4=$(printf '%s' "$OUT" | sed -n 4p) |
| 200 | E5=$(printf '%s' "$OUT" | sed -n 5p) |
| 201 | E6=$(printf '%s' "$OUT" | sed -n 6p) |
| 202 | E7=$(printf '%s' "$OUT" | sed -n 7p) |
| 203 | E8=$(printf '%s' "$OUT" | sed -n 8p) |
| 204 | E9=$(printf '%s' "$OUT" | sed -n 9p) |
| 205 | E10=$(printf '%s' "$OUT" | sed -n 10p) |
| 206 | E11=$(printf '%s' "$OUT" | sed -n 11p) |
| 207 | E12=$(printf '%s' "$OUT" | sed -n 12p) |
| 208 | E13=$(printf '%s' "$OUT" | sed -n 13p) |
| 209 | E14=$(printf '%s' "$OUT" | sed -n 14p) |
| 210 | check "empty thought refused" 'Invalid thought: must be a string' "$E1" |
| 211 | check "missing thought refused" 'Invalid thought: must be a string' "$E2" |
| 212 | check "thoughtNumber 0 refused" 'Invalid thoughtNumber: must be a number' "$E3" |
| 213 | check "non-numeric thoughtNumber" 'Invalid thoughtNumber: must be a number' "$E4" |
| 214 | check "totalThoughts 0 refused" 'Invalid totalThoughts: must be a number' "$E5" |
| 215 | check "bad nextThoughtNeeded" 'Invalid nextThoughtNeeded: must be a boolean' "$E6" |
| 216 | check "missing nextThoughtNeeded" 'Invalid nextThoughtNeeded: must be a boolean' "$E7" |
| 217 | check "bad isRevision" 'Invalid isRevision: must be a boolean' "$E8" |
| 218 | check "bad revisesThought" 'Invalid revisesThought: must be a number' "$E9" |
| 219 | check "bad branchFromThought" 'Invalid branchFromThought: must be a number' "$E10" |
| 220 | check "branchId must be a string" 'Invalid branchId: must be a string' "$E11" |
| 221 | check "bad needsMoreThoughts" 'Invalid needsMoreThoughts: must be a boolean' "$E12" |
| 222 | check "all refusals set isError" '"isError":true' "$E1" |
| 223 | check "all refusals say failed" '"status\":\"failed\"' "$E1" |
| 224 | check "unknown tool -> -32602" '"code":-32602' "$E13" |
| 225 | check "missing arguments refused" 'Invalid thought: must be a string' "$E14" |
| 226 | cat > "$TMP/in.jsonl" <<'JSONL' |
| 227 | {"jsonrpc":"2.0","id":41,"method":"tools/call","params":{"name":"sequentialthinking","arguments":{"thought":"coerce me","nextThoughtNeeded":"TRUE","thoughtNumber":"2","totalThoughts":"4"}}} |
| 228 | JSONL |
| 229 | OUT=$(mcp "$TMP/m_coerce" env) |
| 230 | check "string numbers coerce (upstream z.coerce)" 'totalThoughts\":4' "$OUT" |
| 231 | check "string TRUE coerces to true" 'nextThoughtNeeded\":true' "$OUT" |
| 232 | |
| 233 | section "mode 2 --mcp: history cap and branch eviction" |
| 234 | python3 - > "$TMP/in.jsonl" <<'PY' |
| 235 | import json |
| 236 | print(json.dumps({"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"sequentialthinking","arguments":{"thought":"base","nextThoughtNeeded":True,"thoughtNumber":1,"totalThoughts":9}}})) |
| 237 | for i in range(2, 7): |
| 238 | print(json.dumps({"jsonrpc":"2.0","id":i,"method":"tools/call","params":{"name":"sequentialthinking","arguments":{"thought":"beta%d" % i,"nextThoughtNeeded":True,"branchFromThought":1,"branchId":"beta","thoughtNumber":i,"totalThoughts":9}}})) |
| 239 | for i in range(7, 9): |
| 240 | print(json.dumps({"jsonrpc":"2.0","id":i,"method":"tools/call","params":{"name":"sequentialthinking","arguments":{"thought":"gamma%d" % i,"nextThoughtNeeded":True,"branchFromThought":1,"branchId":"gamma","thoughtNumber":i,"totalThoughts":9}}})) |
| 241 | PY |
| 242 | OUT=$(mcp "$TMP/m_cap1" MAX_THOUGHT_HISTORY=1) |
| 243 | LAST=$(printf '%s' "$OUT" | tail -1) |
| 244 | check "cap 1 keeps one thought" 'thoughtHistoryLength\":1}' "$LAST" |
| 245 | check "cap 1 emptied beta dropped" 'branches\":[\"gamma\"]' "$LAST" |
| 246 | check_not "cap 1 does not list beta" '\"beta\"' "$LAST" |
| 247 | OUT=$(mcp "$TMP/m_cap2" MAX_THOUGHT_HISTORY=3) |
| 248 | LAST=$(printf '%s' "$OUT" | tail -1) |
| 249 | check "cap 3 keeps three thoughts" 'thoughtHistoryLength\":3}' "$LAST" |
| 250 | check "cap 3 still lists beta" 'beta' "$LAST" |
| 251 | OUT=$(mcp "$TMP/m_bad" MAX_THOUGHT_HISTORY=abc) |
| 252 | check "bad cap warns on stderr" 'MAX_THOUGHT_HISTORY must be a positive integer' "$(cat "$TMP/err.txt")" |
| 253 | check "bad cap falls back to default" "thoughtHistoryLength\\\":1}" "$OUT" |
| 254 | OUT=$(mcp "$TMP/m_neg" MAX_THOUGHT_HISTORY=0) |
| 255 | check "zero cap rejected too" 'must be a positive integer' "$(cat "$TMP/err.txt")" |
| 256 | |
| 257 | section "mode 2 --mcp: DISABLE_THOUGHT_LOGGING (upstream parity)" |
| 258 | OUT=$(mcp "$TMP/m_dis" DISABLE_THOUGHT_LOGGING=true) |
| 259 | check "call still succeeds when logging disabled" '"isError":false' "$OUT" |
| 260 | if [ -s "$TMP/err.txt" ]; then fail "logging silenced on stderr"; else pass "logging silenced on stderr"; fi |
| 261 | if [ -f "$TMP/m_dis/chains.json" ]; then fail "logging disabled also skips persistence"; else pass "logging disabled also skips persistence"; fi |
| 262 | OUT=$(mcp "$TMP/m_dis2" DISABLE_THOUGHT_LOGGING=1) |
| 263 | if [ -s "$TMP/err.txt" ]; then fail "DISABLE_THOUGHT_LOGGING=1 silences stderr"; else pass "DISABLE_THOUGHT_LOGGING=1 silences stderr"; fi |
| 264 | |
| 265 | section "mode 2 --mcp: chains.json snapshot for the viewer" |
| 266 | CHAINJSON="$TMP/m_ok/chains.json" |
| 267 | [ -f "$CHAINJSON" ] && pass "chains.json written" || fail "chains.json missing" |
| 268 | CJ=$(cat "$CHAINJSON" 2>/dev/null || echo '{}') |
| 269 | jq_ "chains.json has one chain" "len(d['chains'])==1" "$CJ" |
| 270 | jq_ "chain keeps 4 thoughts" "d['chains'][0]['count']==4" "$CJ" |
| 271 | jq_ "chain records the branch" "d['chains'][0]['branches']==['alt-route']" "$CJ" |
| 272 | jq_ "thought carries fields" "set(['thought','thought_number','total_thoughts','next_needed','at']) <= set(d['chains'][0]['thoughts'][0])" "$CJ" |
| 273 | jq_ "revision flagged" "d['chains'][0]['thoughts'][1].get('is_revision') is True" "$CJ" |
| 274 | jq_ "revision target recorded" "d['chains'][0]['thoughts'][1]['revises']==1" "$CJ" |
| 275 | jq_ "branch flagged" "d['chains'][0]['thoughts'][2]['branch_id']=='alt-route'" "$CJ" |
| 276 | jq_ "needs_more flagged" "d['chains'][0]['thoughts'][1]['needs_more'] is True" "$CJ" |
| 277 | # a second process must merge, not clobber |
| 278 | cat > "$TMP/in.jsonl" <<'JSONL' |
| 279 | {"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"sequentialthinking","arguments":{"thought":"second session","nextThoughtNeeded":false,"thoughtNumber":1,"totalThoughts":1}}} |
| 280 | JSONL |
| 281 | mcp "$TMP/m_ok" env >/dev/null |
| 282 | CJ=$(cat "$CHAINJSON") |
| 283 | jq_ "second session merged" "len(d['chains'])==2" "$CJ" |
| 284 | jq_ "first chain untouched" "any(c['count']==4 for c in d['chains'])" "$CJ" |
| 285 | # malformed store degrades to clean, never a crash or a poisoned file |
| 286 | printf 'not json {{{\n' > "$TMP/m_ok/chains.json" |
| 287 | mcp "$TMP/m_ok" env >/dev/null 2>&1 |
| 288 | check "corrupt store recovered" '"thought"' "$(cat "$TMP/m_ok/chains.json")" |
| 289 | # the file is written .tmp then rename()d |
| 290 | check "persistence uses a temp file" "rename(tmp, path)" "$SRC_TXT" |
| 291 | check_not "persistence never writes the store in place" 'fopen(path, "wb")' "$SRC_TXT" |
| 292 | |
| 293 | # ---------------------------------------------------------------- mode 3: JSON API |
| 294 | section "mode 3 JSON API: authentication" |
| 295 | S="$TMP/api"; mkdir -p "$S" |
| 296 | cat > "$TMP/in.jsonl" <<'JSONL' |
| 297 | {"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"sequentialthinking","arguments":{"thought":"alpha one","nextThoughtNeeded":true,"thoughtNumber":1,"totalThoughts":2}}} |
| 298 | {"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"sequentialthinking","arguments":{"thought":"alpha two","nextThoughtNeeded":false,"isRevision":true,"revisesThought":1,"thoughtNumber":2,"totalThoughts":2}}} |
| 299 | JSONL |
| 300 | mcp "$S" env >/dev/null |
| 301 | R=$(SEQTHINK_STATE_DIR="$S" BOT_PASSWORD=secretpw QUERY_STRING="action=chains" "$BIN"; true) |
| 302 | check "401 without a credential" "Status: 401 Unauthorized" "$R" |
| 303 | check "401 body says invalid" "invalid node password" "$R" |
| 304 | check_not "401 leaks no chains" '"chains":[' "$R" |
| 305 | R=$(SEQTHINK_STATE_DIR="$S" BOT_PASSWORD=secretpw HTTP_X_BOT_PASSWORD=secretpw QUERY_STRING="action=chains" "$BIN"; true) |
| 306 | check "header credential accepted" '"chains":[' "$R" |
| 307 | R=$(SEQTHINK_STATE_DIR="$S" BOT_PASSWORD=secretpw HTTP_AUTHORIZATION="Bearer secretpw" QUERY_STRING="action=stats" "$BIN"; true) |
| 308 | check "bearer credential accepted" '"ok":true' "$R" |
| 309 | R=$(SEQTHINK_STATE_DIR="$S" BOT_PASSWORD=secretpw QUERY_STRING="action=chains&pwd=secretpw" "$BIN"; true) |
| 310 | check "query pwd credential accepted" '"chains":[' "$R" |
| 311 | R=$(SEQTHINK_STATE_DIR="$S" BOT_PASSWORD=secretpw QUERY_STRING="action=chains&pwd=nope" "$BIN"; true) |
| 312 | check "wrong credential still 401" "Status: 401" "$R" |
| 313 | R=$(SEQTHINK_STATE_DIR="$S" QUERY_STRING="action=chains" "$BIN"; true) |
| 314 | check "open node (no BOT_PASSWORD) answers" '"chains":[' "$R" |
| 315 | R=$(SEQTHINK_STATE_DIR="$S" BOT_PASSWORD=secretpw "$BIN" --meta; true) |
| 316 | check "--meta stays unauthenticated" '"name":"seqthink"' "$R" |
| 317 | printf '%s\n' '{"jsonrpc":"2.0","id":1,"method":"ping"}' > "$TMP/ping.jsonl" |
| 318 | R=$(SEQTHINK_STATE_DIR="$S" BOT_PASSWORD=secretpw "$BIN" --mcp < "$TMP/ping.jsonl" 2>/dev/null; true) |
| 319 | check "--mcp stays unauthenticated (stdio)" '"jsonrpc":"2.0"' "$R" |
| 320 | check_not "--mcp never emits a CGI 401" "Status: 401" "$R" |
| 321 | R=$(SEQTHINK_STATE_DIR="$S" BOT_PASSWORD=secretpw "$BIN" < /dev/null; true) |
| 322 | check "bare HTML GET 401 without a credential" "Status: 401 Unauthorized" "$R" |
| 323 | check_not "401 HTML leaks no page" "<!DOCTYPE html>" "$R" |
| 324 | R=$(SEQTHINK_STATE_DIR="$S" BOT_PASSWORD=secretpw HTTP_X_BOT_PASSWORD=secretpw "$BIN" < /dev/null; true) |
| 325 | check "HTML renders with a credential" "<!DOCTYPE html>" "$R" |
| 326 | |
| 327 | section "mode 3 JSON API: actions" |
| 328 | R=$(SEQTHINK_STATE_DIR="$S" QUERY_STRING="action=chains" "$BIN"; true) |
| 329 | CB=$(body "$R") |
| 330 | jq_ "chains lists one chain" "d['count']==1" "$CB" |
| 331 | jq_ "chains reports the plugin" "d['plugin']=='plugin_seqthink'" "$CB" |
| 332 | jq_ "chain summary has thoughts" "d['chains'][0]['thoughts']==2" "$CB" |
| 333 | jq_ "chain summary has last_head" "d['chains'][0]['last_head']=='alpha two'" "$CB" |
| 334 | jq_ "chain summary lists branches" "isinstance(d['chains'][0]['branches'], list)" "$CB" |
| 335 | jq_ "chains never leaks an absolute state path" "'/' not in d['state_dir_name']" "$CB" |
| 336 | CID=$(printf '%s' "$CB" | python3 -c "import json,sys;print(json.load(sys.stdin)['chains'][0]['id'])") |
| 337 | R=$(SEQTHINK_STATE_DIR="$S" QUERY_STRING="action=chain&id=$CID" "$BIN"; true) |
| 338 | DB=$(body "$R") |
| 339 | jq_ "chain returns thoughts" "len(d['thoughts'])==2" "$DB" |
| 340 | jq_ "chain thought text intact" "d['thoughts'][0]['thought']=='alpha one'" "$DB" |
| 341 | jq_ "chain marks the revision" "d['thoughts'][1]['is_revision'] is True" "$DB" |
| 342 | R=$(SEQTHINK_STATE_DIR="$S" QUERY_STRING="action=chain&id=does-not-exist" "$BIN"; true) |
| 343 | check "unknown chain id -> error" 'chain not found' "$(body "$R")" |
| 344 | R=$(SEQTHINK_STATE_DIR="$S" QUERY_STRING="action=chain&id=..%2F..%2Fetc%2Fpasswd" "$BIN"; true) |
| 345 | check "path traversal id refused" 'invalid chain id' "$(body "$R")" |
| 346 | R=$(SEQTHINK_STATE_DIR="$S" QUERY_STRING="action=chain&id=a%3Brm" "$BIN"; true) |
| 347 | check "shell metacharacter id refused" 'invalid chain id' "$(body "$R")" |
| 348 | R=$(SEQTHINK_STATE_DIR="$S" QUERY_STRING="action=stats" "$BIN"; true) |
| 349 | SB=$(body "$R") |
| 350 | jq_ "stats counts thoughts" "d['thoughts']==2" "$SB" |
| 351 | jq_ "stats counts chains" "d['chains']==1" "$SB" |
| 352 | jq_ "stats counts revisions" "d['revisions']==1" "$SB" |
| 353 | jq_ "stats reports the cap" "d['max_chains']==50" "$SB" |
| 354 | R=$(SEQTHINK_STATE_DIR="$S" QUERY_STRING="action=info" "$BIN"; true) |
| 355 | IB=$(body "$R") |
| 356 | jq_ "info declares kind mcp" "d['kind']=='mcp' and d['mcp'] is True" "$IB" |
| 357 | jq_ "info declares menus false" "d['menus'] is False and d['daemon'] is False" "$IB" |
| 358 | jq_ "info lists the tool" "d['tools']==['sequentialthinking']" "$IB" |
| 359 | jq_ "info carries the schema" "'thought' in d['tool_schema']['properties'] and 'branchId' in d['tool_schema']['properties']" "$IB" |
| 360 | jq_ "info schema requires 4" "sorted(d['tool_schema']['required'])==sorted(['thought','nextThoughtNeeded','thoughtNumber','totalThoughts'])" "$IB" |
| 361 | R=$(SEQTHINK_STATE_DIR="$S" QUERY_STRING="action=bogus" "$BIN"; true) |
| 362 | check "unknown action refused" 'unknown action' "$(body "$R")" |
| 363 | R=$(SEQTHINK_STATE_DIR="$S" QUERY_STRING="format=json" "$BIN"; true) |
| 364 | check "format=json answers JSON" 'Content-Type: application/json' "$R" |
| 365 | R=$(SEQTHINK_STATE_DIR="$S" QUERY_STRING="format=jsonx" "$BIN"; true) |
| 366 | check "format=jsonx still serves HTML" 'Content-Type: text/html' "$R" |
| 367 | R=$(SEQTHINK_STATE_DIR="$S" QUERY_STRING="action=clear" "$BIN"; true) |
| 368 | check "clear needs confirmation" 'confirm=1 required' "$(body "$R")" |
| 369 | [ -f "$S/chains.json" ] && pass "clear refused left the store" || fail "clear refused wiped the store" |
| 370 | printf '{"action":"clear","confirm":"1"}' > "$S/_body.json" |
| 371 | R=$(SEQTHINK_STATE_DIR="$S" REQUEST_METHOD=POST CONTENT_LENGTH=$(wc -c < "$S/_body.json") \ |
| 372 | HTTP_X_BOT_PASSWORD=sh "$BIN" < "$S/_body.json"; true) |
| 373 | check "POST JSON body clear works" '"cleared":true' "$(body "$R")" |
| 374 | [ -f "$S/chains.json" ] && fail "clear removed the store" || pass "clear removed the store" |
| 375 | |
| 376 | section "mode 3 JSON API: bounded store" |
| 377 | S2="$TMP/bound"; mkdir -p "$S2" |
| 378 | i=1 |
| 379 | while [ $i -le 55 ]; do |
| 380 | printf '{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"sequentialthinking","arguments":{"thought":"chain %s","nextThoughtNeeded":false,"thoughtNumber":1,"totalThoughts":1}}}\n' "$i" \ |
| 381 | > "$TMP/one.jsonl" |
| 382 | SEQTHINK_STATE_DIR="$S2" env "$BIN" --mcp < "$TMP/one.jsonl" >/dev/null 2>&1 |
| 383 | i=$((i + 1)) |
| 384 | done |
| 385 | BJ=$(cat "$S2/chains.json") |
| 386 | jq_ "store never exceeds 50 chains" "len(d['chains'])<=50" "$BJ" |
| 387 | jq_ "newest chain survived" "any(c['thoughts'][0]['thought']=='chain 55' for c in d['chains'])" "$BJ" |
| 388 | |
| 389 | # ---------------------------------------------------------------- mode 4: HTML |
| 390 | section "mode 4 HTML UI" |
| 391 | Q=$(SEQTHINK_STATE_DIR="$TMP/api" QUERY_STRING="embed=1" "$BIN"; true) |
| 392 | check "html content type" "Content-Type: text/html; charset=utf-8" "$Q" |
| 393 | check "html declares charset" "<meta charset=" "$Q" |
| 394 | check "html has a viewport" "width=device-width" "$Q" |
| 395 | check "html is CSP-locked" "default-src 'none'" "$Q" |
| 396 | check "html sets X-Frame-Options" "X-Frame-Options: SAMEORIGIN" "$Q" |
| 397 | check_not "html never sends Content-Length" "Content-Length:" "$Q" |
| 398 | check_not "html has no CDN script" "<script src=" "$Q" |
| 399 | check_not "html has no cdnjs" "cdnjs" "$Q" |
| 400 | check_not "html has no googleapis" "googleapis" "$Q" |
| 401 | check_not "html has no private IPs" "192.168." "$Q" |
| 402 | check_not "html has no localhost URL" "http://127.0.0.1" "$Q" |
| 403 | check "html has the bot token block" "--bg-card" "$Q" |
| 404 | check "html prepaints the theme" "data-theme" "$Q" |
| 405 | check "html follows the bot" "sx-theme" "$Q" |
| 406 | check "html asks for the theme" "sx-theme-request" "$Q" |
| 407 | check "html flags embed mode" "embedded" "$Q" |
| 408 | check "html names the plugin" "Thought Chains" "$Q" |
| 409 | check "html branches the API route" "/plugin_seqthink?action=" "$Q" |
| 410 | check "html supports the hub proxy route" "/plugins?plugin=" "$Q" |
| 411 | check "html carries the node password" "X-Bot-Password" "$Q" |
| 412 | check "html calls the chains API" "api('chains')" "$Q" |
| 413 | check_not "html has no emoji" "💭" "$Q" |
| 414 | check_not "html has no emoji (revision)" "🔄" "$Q" |
| 415 | check_not "html has no emoji (branch)" "🌿" "$Q" |
| 416 | if grep -qF "💭" src/ui.html; then fail "ui source has no emoji"; else pass "ui source has no emoji"; fi |
| 417 | grep -c "<script>" src/ui.html >/dev/null 2>&1 && pass "ui has its own script tag" || fail "ui script tag missing" |
| 418 | grep -q "<style>" src/ui.html && pass "ui has its own style tag" || fail "ui style tag missing" |
| 419 | check_not "ui has no inline hex palette outside tokens" "#123456" "$Q" |
| 420 | if python3 tools/gen_ui.py --check >/dev/null 2>&1; then |
| 421 | pass "src/ui_html.h is generated from src/ui.html" |
| 422 | else |
| 423 | fail "src/ui_html.h is stale (run tools/gen_ui.py)" |
| 424 | fi |
| 425 | |
| 426 | section "source invariants" |
| 427 | check "_WIN32 forces binary stdout" "_setmode(_fileno(stdout), _O_BINARY)" "$SRC_TXT" |
| 428 | check "_WIN32 gate precedes includes" "_WIN32_WINNT 0x0601" "$SRC_TXT" |
| 429 | check "Apple needs _DARWIN_C_SOURCE" "_DARWIN_C_SOURCE" "$SRC_TXT" |
| 430 | check "meta is emitted before auth" "return run_meta();" "$SRC_TXT" |
| 431 | check "mcp mode is dispatched" "return run_mcp();" "$SRC_TXT" |
| 432 | check "CGI surface gated before dispatch" "require_node_auth();" "$SRC_TXT" |
| 433 | check_not "no shell command execution" "popen(" "$SRC_TXT" |
| 434 | check_not "no system()" "system(" "$SRC_TXT" |
| 435 | check_not "no sockets" "socket(" "$SRC_TXT" |
| 436 | check_not "no TLS client" "mbedtls" "$SRC_TXT" |
| 437 | check_not "no outbound http helper" "WinHttp" "$SRC_TXT" |
| 438 | check "chain ids are validated" "valid_chain_id" "$SRC_TXT" |
| 439 | check "history cap is bounded" "MAX_THOUGHT_HISTORY" "$SRC_TXT" |
| 440 | check "thought text is length-capped" "THOUGHT_MAX" "$SRC_TXT" |
| 441 | check "branch id is length-capped" "BRANCHID_MAX" "$SRC_TXT" |
| 442 | check "state dir honours the test override" "SEQTHINK_STATE_DIR" "$SRC_TXT" |
| 443 | check "state dir prefers /workspace" "/workspace/.scorpiox/connectors/" "$SRC_TXT" |
| 444 | check "state dir uses LOCALAPPDATA on win" "LOCALAPPDATA" "$SRC_TXT" |
| 445 | printf '%s' "$SRC_TXT" | grep -q getline && pass "mcp loop reads line-delimited stdio" || fail "mcp loop has no line reader" |
| 446 | |
| 447 | section "ASan + fortify (parses files it did not write)" |
| 448 | if [ ! -x ./plugin_seqthink.asan ] || [ "$SRC_TXT" -nt ./plugin_seqthink.asan ]; then |
| 449 | cc -g -O0 -std=c99 -Wall -Wextra -fsanitize=address -I src -I vendor \ |
| 450 | -o plugin_seqthink.asan src/plugin_seqthink.c vendor/yyjson.c >/dev/null 2>&1 \ |
| 451 | || echo " note: ASan build unavailable here" |
| 452 | fi |
| 453 | if [ -x ./plugin_seqthink.asan ]; then |
| 454 | cat > "$TMP/in.jsonl" <<'JSONL' |
| 455 | {"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"sequentialthinking","arguments":{"thought":"asan thought with \"quotes\" and \\ backslash and \ntab\t","nextThoughtNeeded":true,"isRevision":true,"revisesThought":1,"thoughtNumber":1,"totalThoughts":2}}} |
| 456 | {"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"sequentialthinking","arguments":{"thought":"unicode ünïcødé and émojis ok","nextThoughtNeeded":false,"branchFromThought":1,"branchId":"ünï","thoughtNumber":2,"totalThoughts":2}}} |
| 457 | JSONL |
| 458 | AOUT=$(SEQTHINK_STATE_DIR="$TMP/asan" ./plugin_seqthink.asan --mcp < "$TMP/in.jsonl" 2>"$TMP/aerr.txt") |
| 459 | if grep -q "AddressSanitizer" "$TMP/aerr.txt"; then |
| 460 | fail "ASan reported a problem"; sed -n 1,12p "$TMP/aerr.txt" |
| 461 | else |
| 462 | pass "ASan clean on quotes/backslash/newline/unicode thoughts" |
| 463 | fi |
| 464 | check "ASan run still answered" '"isError":false' "$AOUT" |
| 465 | R=$(SEQTHINK_STATE_DIR="$TMP/asan" QUERY_STRING="action=chains" ./plugin_seqthink.asan; true) |
| 466 | if grep -q "AddressSanitizer" "$TMP/aerr.txt"; then fail "ASan clean on the CGI read path" |
| 467 | else pass "ASan clean on the CGI read path"; fi |
| 468 | printf '{{{ not json' > "$TMP/asan/chains.json" |
| 469 | SEQTHINK_STATE_DIR="$TMP/asan" QUERY_STRING="action=chains" ./plugin_seqthink.asan >/dev/null 2>>"$TMP/aerr.txt" |
| 470 | SEQTHINK_STATE_DIR="$TMP/asan" QUERY_STRING="action=chain&id=th-x" ./plugin_seqthink.asan >/dev/null 2>>"$TMP/aerr.txt" |
| 471 | printf '{"chains":[{"id":42,"thoughts":"not an array"},{"thoughts":[]}]}' > "$TMP/asan/chains.json" |
| 472 | SEQTHINK_STATE_DIR="$TMP/asan" QUERY_STRING="action=stats" ./plugin_seqthink.asan >/dev/null 2>>"$TMP/aerr.txt" |
| 473 | if grep -q "AddressSanitizer" "$TMP/aerr.txt"; then fail "ASan clean on malformed stores" |
| 474 | else pass "ASan clean on malformed stores"; fi |
| 475 | else |
| 476 | echo " skip ASan checks (build unavailable)" |
| 477 | fi |
| 478 | |
| 479 | if [ ! -x ./plugin_seqthink.fortify ]; then |
| 480 | cc -O2 -std=c99 -Wall -Wextra -D_FORTIFY_SOURCE=2 -I src -I vendor \ |
| 481 | -o plugin_seqthink.fortify src/plugin_seqthink.c vendor/yyjson.c >/dev/null 2>&1 \ |
| 482 | || echo " note: fortify build unavailable here" |
| 483 | fi |
| 484 | if [ -x ./plugin_seqthink.fortify ]; then |
| 485 | SEQTHINK_STATE_DIR="$TMP/fort" ./plugin_seqthink.fortify --mcp < "$TMP/in.jsonl" >/dev/null 2>"$TMP/ferr.txt" |
| 486 | if grep -qi "buffer overflow" "$TMP/ferr.txt"; then fail "fortify build aborted" |
| 487 | else pass "fortify build survives the full session"; fi |
| 488 | else |
| 489 | echo " skip fortify checks (build unavailable)" |
| 490 | fi |
| 491 | |
| 492 | # ---------------------------------------------------------------- audit |
| 493 | section "clean-room audit (working tree + full git history)" |
| 494 | audit() { # audit <label> <regex> |
| 495 | local out |
| 496 | out=$(git grep -InE -e "$2" -- . 2>/dev/null | grep -v 'tests/run.sh' | head -5 || true) |
| 497 | if [ -n "$out" ]; then |
| 498 | fail "audit: $1"; printf '%s\n' "$out" | sed 's/^/ /' |
| 499 | else |
| 500 | pass "audit: $1" |
| 501 | fi |
| 502 | } |
| 503 | audit "no LAN 192.168 addresses" '192\.168\.[0-9]+\.[0-9]+' |
| 504 | audit "no private 10.x addresses" '\b10\.[0-9]+\.[0-9]+\.[0-9]+' |
| 505 | PAT="gh""p_[A-Za-z0-9]{36}" |
| 506 | audit "no PAT prefix" "$PAT" |
| 507 | TOK="3e""f2c00c" |
| 508 | audit "no token prefix" "$TOK" |
| 509 | SFP="ssh""pass -p [A-Za-z0-9]" |
| 510 | audit "no ssh password flag" "$SFP" |
| 511 | HOSTS="macm""ini|intel-""ultra""2|lazar""us" |
| 512 | audit "no internal hostnames" "$HOSTS" |
| 513 | if ! git rev-parse HEAD >/dev/null 2>&1; then |
| 514 | echo " skip git-history audit (repository has no commits yet)" |
| 515 | elif command -v scorpiox-cli-botplugin >/dev/null 2>&1; then |
| 516 | if scorpiox-cli-botplugin audit "$ROOT" >/dev/null 2>&1; then |
| 517 | pass "scorpiox-cli-botplugin audit accepts the repo" |
| 518 | else |
| 519 | fail "scorpiox-cli-botplugin audit rejected the repo" |
| 520 | scorpiox-cli-botplugin audit "$ROOT" 2>&1 | tail -10 | sed 's/^/ /' |
| 521 | fi |
| 522 | else |
| 523 | echo " skip scorpiox-cli-botplugin audit (not installed here)" |
| 524 | fi |
| 525 | |
| 526 | printf '\n%s==============================%s\n' "$C_B" "$C_0" |
| 527 | printf ' %s%d passed%s %s%d failed%s\n' "$C_OK" "$PASS" "$C_0" "$([ "$FAIL" -gt 0 ] && printf '%s' "$C_BAD" || printf '%s')" "$FAIL" "$C_0" |
| 528 | [ "$FAIL" -eq 0 ] || exit 1 |
| 529 | exit 0 |