Files
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

212 lines
7.7 KiB
Bash
Executable File

#!/bin/sh
# splinter -- the A2A client transport, against a fake A2A agent.
#
# What it guards: SendMessage as JSON-RPC (--no-stream), SendStreamingMessage
# with SSE (the default one-shot path), the message shape, a Task result
# (status message plus artifact text), a direct Message, the bearer token, and
# a JSON-RPC error that must fail the turn instead of becoming an empty answer.
set -u
cd "$(dirname "$0")/.." || exit 1
. "$(dirname "$0")/sh/port.sh"
D=$(mktemp -d "${TMPDIR:-/var/tmp}/splinter-a2a-XXXXXX") || exit 1
SPLINTER_SESSION_DIR="$D/home/.splinter"; export SPLINTER_SESSION_DIR
P=$(free_port)
F=
cleanup() { [ -n "$F" ] && kill "$F" 2>/dev/null; rm -rf "$D"; }
trap cleanup EXIT
export SPLINTER_A2A_INBOX="$D/inbox"
python3 -I tests/fake_a2a.py "$P" "$D/req.log" &
F=$!
for i in $(seq 1 50); do
curl -s -o /dev/null "http://127.0.0.1:$P/x" && break
sleep 0.2
done
fails=0
check() { if "$@"; then :; else echo "FAIL $*"; fails=$((fails + 1)); fi; }
blocking() { # the plain send path
SPLINTER_A2A_URL="http://127.0.0.1:$P/rpc" SPLINTER_A2A_TIMEOUT=20 \
./splinter --no-config --plugin plugins/a2a/a2a.so \
--transport a2a --no-stream -p "$1"
}
streaming() { # the one-shot default: stream when the transport can
SPLINTER_A2A_URL="http://127.0.0.1:$P/rpc" SPLINTER_A2A_TIMEOUT=20 \
./splinter --no-config --plugin plugins/a2a/a2a.so \
--transport a2a -p "$1"
}
discovered() { # <card-path> <text>: no URL, only the agent card
SPLINTER_A2A_CARD="http://127.0.0.1:$P/$1" SPLINTER_A2A_TIMEOUT=20 \
./splinter --no-config --plugin plugins/a2a/a2a.so \
--transport a2a --no-stream -p "$2"
}
# ---- blocking: a Task, status message and artifact text ----
out=$(blocking "hallo" 2>&1)
echo "$out" | grep -q 'a2a sagt hallo'
check test $? -eq 0
echo "$out" | grep -q 'und tschuess'
check test $? -eq 0
# the wire shape of the blocking call
grep -q '"method":"SendMessage"' "$D/req.log"
check test $? -eq 0
grep -q '"role":"ROLE_USER"' "$D/req.log"
check test $? -eq 0
grep -q '"parts":\[{"text":"hallo"}\]' "$D/req.log"
check test $? -eq 0
# a direct Message is an answer, not a task
out=$(blocking "direkt" 2>&1)
echo "$out" | grep -q 'direkt vom agenten'
check test $? -eq 0
# a binary part is not dropped: it is saved and the path is in the answer
out=$(blocking "schick eine datei" 2>&1)
echo "$out" | grep -q 'hier ist die datei'
check test $? -eq 0
echo "$out" | grep -q '\[file bild.png at'
check test $? -eq 0
found=$(ls "$SPLINTER_A2A_INBOX"/*bild.png 2>/dev/null | head -1)
check test -n "$found"
check sh -c "printf PNGDATA | cmp - '$found'"
# raw (text/plain base64) and string data parts are read
out=$(blocking "raw" 2>&1)
echo "$out" | grep -q 'hallo'
check test $? -eq 0
echo "$out" | grep -q 'struktur'
check test $? -eq 0
# the token goes out as a bearer header, and is required when the agent asks
out=$(SPLINTER_A2A_TOKEN=tok blocking "auth bitte" 2>&1)
echo "$out" | grep -q 'a2a sagt hallo'
check test $? -eq 0
out=$(blocking "auth ohne token" 2>&1)
echo "$out" | grep -q 'unauthorized'
check test $? -eq 0
# a JSON-RPC error fails the turn with the agent's message
blocking "fehler" >/dev/null 2>"$D/err"
check test $? -ne 0
grep -q 'boom from a2a' "$D/err"
check test $? -eq 0
# ---- streaming: artifact chunks arrive as the answer ----
out=$(streaming "stream bitte" 2>&1)
echo "$out" | grep -q 'teil eins teil zwei'
check test $? -eq 0
if echo "$out" | grep -q 'fertig'; then
echo "FAIL the final status note leaked into the streamed answer"
fails=$((fails + 1))
fi
# no artifact: the final status message is the answer
out=$(streaming "streamstatus" 2>&1)
echo "$out" | grep -q 'nur status'
check test $? -eq 0
# a direct Message also streams
out=$(streaming "direkt" 2>&1)
echo "$out" | grep -q 'direkt vom agenten'
check test $? -eq 0
# the wire method, not the blocking one
grep -q '"method":"SendStreamingMessage"' "$D/req.log"
check test $? -eq 0
# a plain JSON-RPC error instead of a stream: the server's message, and the
# request file (the whole conversation) is not left behind
tmpd="$D/tmp"
mkdir -p "$tmpd"
out=$(TMPDIR="$tmpd" streaming "plainerr" 2>&1)
echo "$out" | grep -q 'plain boom'
check test $? -eq 0
check test -z "$(ls "$tmpd" | grep splinter-a2a-)"
# ---- discovery: the card names the JSON-RPC endpoint ----
out=$(discovered ".well-known/agent-card.json" "hallo" 2>&1)
echo "$out" | grep -q 'a2a sagt hallo'
check test $? -eq 0
out=$(discovered "old-card.json" "hallo" 2>&1)
echo "$out" | grep -q 'a2a sagt hallo'
check test $? -eq 0
# ---- the plugin's GetTask/CancelTask tools against the same agent ----
mkdir -p "$D/home"
printf 'tool a2a_get_task {"id":"task-1"}\nanswer done\n' > "$D/tool-get.txt"
out=$(HOME="$D/home" SPLINTER_A2A_URL="http://127.0.0.1:$P/rpc" \
SPLINTER_A2A_TIMEOUT=20 ./splinter --no-config \
--plugin plugins/a2a/a2a.so --script "$D/tool-get.txt" \
-p "tool" --autonomous 2>&1)
echo "$out" | grep -q '^done'
check test $? -eq 0
sess=$(ls -t "$D/home/.splinter"/sess-*.jsonl 2>/dev/null | head -1)
check test -n "$sess"
check grep -q 'TASK_STATE_COMPLETED' "$sess"
printf 'tool a2a_cancel_task {"id":"task-1"}\nanswer done\n' \
> "$D/tool-cancel.txt"
out=$(HOME="$D/home" SPLINTER_A2A_URL="http://127.0.0.1:$P/rpc" \
SPLINTER_A2A_TIMEOUT=20 ./splinter --no-config \
--plugin plugins/a2a/a2a.so --script "$D/tool-cancel.txt" \
-p "tool" --autonomous 2>&1)
echo "$out" | grep -q '^done'
check test $? -eq 0
sess=$(ls -t "$D/home/.splinter"/sess-*.jsonl 2>/dev/null | head -1)
check grep -q -- '-32002' "$sess"
# ---- SubscribeToTask: the events of a subscription as one observation ----
printf 'tool a2a_subscribe {"id":"task-1"}\nanswer done\n' \
> "$D/tool-sub.txt"
out=$(HOME="$D/home" SPLINTER_A2A_URL="http://127.0.0.1:$P/rpc" \
SPLINTER_A2A_TIMEOUT=20 ./splinter --no-config \
--plugin plugins/a2a/a2a.so --script "$D/tool-sub.txt" \
-p "tool" --autonomous 2>&1)
echo "$out" | grep -q '^done'
check test $? -eq 0
sess=$(ls -t "$D/home/.splinter"/sess-*.jsonl 2>/dev/null | head -1)
check test -n "$sess"
check grep -q 'TASK_STATE_COMPLETED' "$sess"
check grep -q 'abo text' "$sess"
check grep -q 'abo teil' "$sess"
printf 'tool a2a_subscribe {"id":"nope"}\nanswer done\n' \
> "$D/tool-sub2.txt"
out=$(HOME="$D/home" SPLINTER_A2A_URL="http://127.0.0.1:$P/rpc" \
SPLINTER_A2A_TIMEOUT=20 ./splinter --no-config \
--plugin plugins/a2a/a2a.so --script "$D/tool-sub2.txt" \
-p "tool" --autonomous 2>&1)
echo "$out" | grep -q '^done'
check test $? -eq 0
sess=$(ls -t "$D/home/.splinter"/sess-*.jsonl 2>/dev/null | head -1)
check grep -q -- '-32001' "$sess"
# ---- a2a_send: a message with a file attached ----
printf 'PNGDATA' > "$D/att.png"
printf 'tool a2a_send {"text":"hier ist eine datei","file":"%s"}\nanswer done\n' \
"$D/att.png" > "$D/tool-send.txt"
out=$(HOME="$D/home" SPLINTER_A2A_URL="http://127.0.0.1:$P/rpc" \
SPLINTER_A2A_TIMEOUT=20 ./splinter --no-config \
--plugin plugins/a2a/a2a.so --script "$D/tool-send.txt" \
-p "tool" --autonomous 2>&1)
echo "$out" | grep -q '^done'
check test $? -eq 0
sess=$(ls -t "$D/home/.splinter"/sess-*.jsonl 2>/dev/null | head -1)
check grep -q 'datei empfangen: att.png 7' "$sess"
check grep -q '"file":{"name":"att.png","mimeType":"image/png"' "$D/req.log"
check grep -q '"bytes":"UE5HREFUQQ=="' "$D/req.log"
printf 'tool a2a_send {"text":"x","file":"%s/%s"}\nanswer done\n' \
"$D" "gibtsnicht.png" > "$D/tool-send2.txt"
out=$(HOME="$D/home" SPLINTER_A2A_URL="http://127.0.0.1:$P/rpc" \
SPLINTER_A2A_TIMEOUT=20 ./splinter --no-config \
--plugin plugins/a2a/a2a.so --script "$D/tool-send2.txt" \
-p "tool" --autonomous 2>&1)
echo "$out" | grep -q '^done'
check test $? -eq 0
sess=$(ls -t "$D/home/.splinter"/sess-*.jsonl 2>/dev/null | head -1)
check grep -q 'cannot read' "$sess"
if [ "$fails" -eq 0 ]; then echo "test_a2a: ok"; exit 0; fi
exit 1