Files
splinter/tests/test_agents_ui.sh
T
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

105 lines
4.2 KiB
Bash

#!/bin/sh
#
# splinter -- Tab switches the agent profile; plan cannot run commands.
set -u
cd "$(dirname "$0")/.." || exit 1
. "$(dirname "$0")/sh/port.sh"
command -v tmux >/dev/null 2>&1 || { echo "test_agents_ui: skipped (no tmux)"; exit 0; }
D=$(mktemp -d "${TMPDIR:-/var/tmp}/splinter-ag-XXXXXX") || exit 1
SPLINTER_SESSION_DIR="$D/home/.splinter"; export SPLINTER_SESSION_DIR
# Our own socket, never the user's. $TMUX (set when this runs inside somebody's
# tmux) wins over TMUX_TMPDIR, so drop it and point the server at $D: a key
# this test sends can then not land in a real session, and the kill-session
# below cannot touch one.
unset TMUX TMUX_PANE
export TMUX_TMPDIR="$D"
own_server() {
if [ -n "${TMUX:-}" ] || [ "${TMUX_TMPDIR:-}" != "$D" ]; then
echo "test_agents_ui: refusing to touch a tmux server this test does not own" >&2
exit 1
fi
}
killserver() { own_server; tmux kill-server 2>/dev/null; }
P=$(free_port)
S=sp-agents-$$
F=
trap 'killserver; kill $F 2>/dev/null; rm -rf "$D"' EXIT
fails=0
check() { d=$1; shift; if "$@"; then :; else echo "FAIL $d"; fails=$((fails + 1)); fi; }
shows() {
for i in $(seq 1 40); do
tmux capture-pane -t $S -p | grep -q -- "$1" && return 0
sleep 0.25
done
return 1
}
mkdir -p "$D/home/.splinter/agents" "$D/proj"
printf -- '---\ndescription: only reads\ntools: read\n---\nBe careful.\n' \
> "$D/home/.splinter/agents/careful.md"
python3 -I tests/fake_openai_tool.py "$P" "$D/made" &
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
# --agent-list names the profiles and where a file came from
HOME="$D/home" ./splinter --no-config --agent-list </dev/null > "$D/list.txt" 2>&1
check "build is listed" grep -q '^build ' "$D/list.txt"
check "plan is listed read-only" grep -q '^plan .*read-only' "$D/list.txt"
check "a file profile is listed" grep -q '^careful ' "$D/list.txt"
tmux new-session -d -s $S -x 120 -y 30 -c "$D/proj" \
"HOME=$D/home SPLINTER_LOCALE=en SPLINTER_USAGE_DIR=$D/u SPLINTER_COOLDOWN_DIR=$D/c \
$PWD/splinter --no-config --endpoint http://127.0.0.1:$P/v1/chat/completions \
--model m --chat 2>/dev/null"
check "starts as build" shows '^build>'
tmux send-keys -t $S Tab
check "Tab goes to plan" shows 'agent: plan'
tmux send-keys -t $S Tab
check "Tab goes on to a file profile" shows 'agent: careful'
tmux send-keys -t $S Tab
check "and wraps to build" shows '^build>'
tmux send-keys -t $S BTab
check "Shift-Tab goes back" shows 'agent: careful'
tmux send-keys -t $S BTab
check "back to plan" shows 'agent: plan'
# in plan, the command is refused with the reason; nothing is created
tmux send-keys -t $S -l 'mach etwas'
tmux send-keys -t $S Enter
check "plan refuses the command" shows "not available in the 'plan' agent"
check "nothing was created" test ! -e "$D/made"
# in build the same request goes through (autonomous is off, so it asks)
tmux send-keys -t $S Tab
tmux send-keys -t $S Tab
sleep 1
tmux send-keys -t $S -l 'nochmal'
tmux send-keys -t $S Enter
check "build asks for consent" shows 'y/n\|allow\|approve\|Allow\|Approve'
# The language is English unless a config or SPLINTER_LOCALE says otherwise;
# the desktop's LANG must not change it behind the operator's back.
# A fresh session name: reusing one right after kill-session races, and a lost
# keystroke looked like a rendering bug.
tmux kill-session -t $S 2>/dev/null
S=sp-agents-de-$$
tmux new-session -d -s $S -x 120 -y 30 -c "$D/proj" \
"env -u SPLINTER_LOCALE LANG=de_DE.UTF-8 HOME=$D/home SPLINTER_USAGE_DIR=$D/u \
SPLINTER_COOLDOWN_DIR=$D/c $PWD/splinter --no-config \
--endpoint http://127.0.0.1:$P/v1/chat/completions --model m --chat 2>/dev/null"
check "English UI despite LANG=de_DE" shows 'ready'
# an asterisk in code is an asterisk; real emphasis loses its markers
tmux send-keys -t $S -l 'pointers bitte'
tmux send-keys -t $S Enter
check "pointers keep their asterisks" shows 'void \*ctx, const char \*name'
check "bold loses its markers" shows -- '-- bold and it'
check "no raw catalogue keys on the screen" sh -c \
"! tmux capture-pane -t $S -p | grep -Eq '(^|[ >])(ui|sidebar|status|widget|agent|usage|role)\.[a-z_]+'"
if [ "$fails" -eq 0 ]; then echo "test_agents_ui: ok"; exit 0; fi
tmux capture-pane -t $S -p | tail -15
exit 1