Files
splinter/tests/test_anthropic.sh
lotherk fa16bfd0a9 foreign: adopt a claude, hermes or opencode session as a splinter session
Another harness's conversation is not a session until splinter can resume
it. plugins/foreign reads the three stores that already exist on the
machine - ~/.claude/projects/<slug>/<id>.jsonl, <root>/state.db (and
profiles/*/state.db) and opencode.db's session_v2/session_message - and
offers four tools plus /foreign:

  list, search TEXT, read SOURCE ID, import SOURCE ID

The stores are read with sqlite3 -readonly: an import copies, it never
moves. Reasoning blocks are dropped on the way in, because a provider does
not accept another model's thinking as evidence. Tool calls arrive with
their results in place, which is what makes the imported session worth
resuming instead of retyping.

import writes a real splinter session: the conversation as a replayable
jsonl, and a .meta with the source's title, its start time and *its*
working directory. So /open continues it in the chat,
--session-id <id> --resume in a shell, and -c finds it in the directory the
conversation actually ran in. The marker foreign=<source>:<id> survives
every later reopen, which needed the core's .meta sidecar to carry keys it
does not own.

Building this found nine bugs, each one seen red before it was green:

  - four use-after-frees: mcp_arg_string returns a pointer *into* the parse
    tree, read after mcp_json_free. That garbage in `only`/`src` was why a
    list of three stores answered "no sessions" and named AÃ_Â¥U.
  - sql_json took "no rows" for a failure (NULL), so every empty answer
    looked like a broken database.
  - args_split never terminated its buffers: /foreign list became
    "unknown subcommand 'list('".
  - path_join(dir, id, ".meta") built dir/id/.meta, so the sidecar landed
    in a subdirectory.
  - claude_slug_dir had the sign inverted and returned -1 for every
    non-empty cwd, so the project directory was never found.
  - sp_session_open overwrote cwd with getcwd() on *every* open, though the
    comment beside it promised the opposite: imports claimed the directory
    they were imported from and -c then picked the wrong session.
  - hermes_text ran strncmp(p, "\x00json:", 6) on an empty content, a
    one-byte allocation - heap-buffer-overflow, found by ASAN. The marker is
    stripped in SQL anyway.
  - hermes_calls never freed the root node on the object path (5456 bytes
    over 42 allocations).
  - list_any reported strerror(errno) with errno never set: a real file with
    the wrong schema read as "No such file or directory". The SQL reason is
    passed through now.

Checked end to end against the stores on this machine: claude 64 sessions,
opencode 25, hermes 0 (both hermes databases here are empty, count(*) = 0).
A real opencode session of 150 messages reads back with its tool output; one
of 75 imports with the right cwd and title, --resume sends its history, and
-c from the source directory opens it.

tests/sh/port.sh comes along: the shell tests guessed a port with
20000 + $$ % 30000 and on 2026-11-21 landed on one dtsession held. The fake
died, curl got an answer from dtsession, and twenty checks blamed the
gateway. They ask the kernel for a free port now.
2026-11-21 21:26:43 +00:00

95 lines
3.4 KiB
Bash

#!/bin/sh
#
# splinter -- the Anthropic transport against a fake Messages API.
#
# The fake is as strict as the real endpoint: an OAuth token must arrive as a
# Bearer token with the oauth beta, the system prompt must be the top-level
# `system` field starting with Claude Code's block, and tools must carry
# input_schema. So a run that answers here has a request shape the real API
# accepts -- not just a socket that says 200.
#
# Also checked: streaming and non-streaming give the same answer, a 429 keeps
# the provider's own message (instead of "no answer"), and -m picks the pool.
set -u
cd "$(dirname "$0")/.." || exit 1
. "$(dirname "$0")/sh/port.sh"
D=$(mktemp -d "${TMPDIR:-/var/tmp}/splinter-as-XXXXXX") || exit 1
P=$(free_port)
trap 'kill $FP 2>/dev/null; rm -rf "$D"' EXIT
fails=0
check() { d=$1; shift; if "$@"; then :; else echo "FAIL $d"; fails=$((fails + 1)); fi; }
mkdir -p "$D/home"
python3 -I tests/fake_anthropic.py "$P" "$D/req.log" &
FP=$!
for i in $(seq 1 50); do
curl -s -o /dev/null "http://127.0.0.1:$P/" && break
sleep 0.2
done
cat > "$D/home/splinter.conf" <<CONF
core { default = duo }
provider api0 {
about = "anthropic, Konto 0"
kind = anthropic
endpoint = http://127.0.0.1:$P/v1/messages
model = claude-sonnet-5-5
tags = [analyze, decide]
key = "sk-ant-oat01-test-token"
}
provider api1 {
about = "anthropic, am Limit"
kind = anthropic
endpoint = http://127.0.0.1:$P/v1/messages
model = claude-limit
key_env = SPLINTER_TEST_OAUTH
}
pool duo { members = [api0] strategy = balance }
CONF
export HOME="$D/home" SPLINTER_USAGE_DIR="$D/usage"
export SPLINTER_COOLDOWN_DIR="$D/cd"
export SPLINTER_TEST_OAUTH="sk-ant-oat01-second-token"
# one answer, streamed (the default for -p) and as one whole reply
./splinter --config "$D/home/splinter.conf" -m api0 -p hallo </dev/null \
> "$D/o1.txt" 2>&1
check "a streamed anthropic answer arrives" grep -q 'weiter gemacht' "$D/o1.txt"
./splinter --config "$D/home/splinter.conf" -m api0 --no-stream -p hallo \
</dev/null > "$D/o2.txt" 2>&1
check "a whole anthropic reply arrives" grep -q 'weiter gemacht' "$D/o2.txt"
# -m with the pool name reaches the same provider
./splinter --config "$D/home/splinter.conf" -m duo --no-stream -p hallo \
</dev/null > "$D/o3.txt" 2>&1
check "the pool name selects the member" grep -q 'weiter gemacht' "$D/o3.txt"
# the request says anthropic, not openai
check "the endpoint was addressed" grep -q '^/v1/messages' "$D/req.log"
check "the system prompt is top level" grep -q '"system":\[{"type":"text"' \
"$D/req.log"
check "tools carry input_schema" grep -q '"input_schema"' "$D/req.log"
check "no chat/completions shape leaked in" \
test "$(grep -c 'tool_calls' "$D/req.log")" -eq 0
# a limit keeps the provider's own sentence and the HTTP status; on_limit=fail
# so the run says it now instead of waiting for the window to move
cat > "$D/home/limit.conf" <<CONF
core { default = solo on_limit = fail }
provider solo {
kind = anthropic
endpoint = http://127.0.0.1:$P/v1/messages
model = claude-limit
key_env = SPLINTER_TEST_OAUTH
}
CONF
./splinter --config "$D/home/limit.conf" --no-stream -p hallo \
</dev/null > "$D/o4.txt" 2>&1
rc=$?
check "a 429 is not reported as an empty answer" grep -q 'rate limit' \
"$D/o4.txt"
check "a 429 fails the turn" test "$rc" -ne 0
if [ "$fails" -eq 0 ]; then echo "test_anthropic: ok"; exit 0; fi
cat "$D/o1.txt" "$D/o2.txt" "$D/o3.txt" "$D/o4.txt"
exit 1