Three bugs in the /sessions picker, all of them found by review and all of
them visible on the screen:
- sp_engine_sources() hard-set every foreign source's count to 0, so the
picker offered "claude (0)" while 82 rows were behind it. The count now
comes from the same listing the picker shows.
- sp_foreign_sessions() hard-set each row's date to 0, so every foreign
session drew as 1970-01-01. The plugin prints "%Y-%m-%d %H:%M" and the
row's first column is parsed into epoch seconds.
- The foreign plugin's list tool queried with its configured default
(limit = 25) no matter what the caller asked for, so asking for 100 rows
returned 25 and a store with 290 sessions looked like one with 25. The
caller's limit now reaches the SQL.
Proof: tests/test_foreignopen.c asserts count == 1 and mtime > 1600000000
against a fixture store; reverting either fix makes both checks fail. Live
over the socket, before: [('splinter', 100), ('claude', 0), ('opencode', 0)]
with mtime 0 on every row; after: [('splinter', 100), ('claude', 82),
('opencode', 100)] with mtime 1795387200. sqlite3 counts 290 opencode
sessions and 82 claude files, so the numbers agree with the stores.
make check: 21 suites, 0 failures. check_i18n: 229 keys in 3 catalogues.
448 lines
13 KiB
C
448 lines
13 KiB
C
/*
|
|
* splinter -- -c --claude: continue the session another tool stopped in.
|
|
*
|
|
* Copyright (c) 2026 Konrad Lother
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
*
|
|
* What it guards, against a fixture it writes itself (the machine's real
|
|
* Claude store is not a test):
|
|
*
|
|
* - the newest Claude session of *this* directory is imported and becomes the
|
|
* session of the run, with the source's history in it;
|
|
* - a second start continues that same session instead of importing again:
|
|
* the plugin's import replaces a file of the same name, so a second import
|
|
* would throw away everything the conversation did in splinter since;
|
|
* - a directory without a Claude session is an error, not an empty session;
|
|
* - the empty placeholder a run opens before it knows what it continues is
|
|
* removed, and a session that already had content never is.
|
|
*/
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/stat.h>
|
|
#include <sys/types.h>
|
|
#include <unistd.h>
|
|
|
|
#include "splinter.h"
|
|
#include "splinter_plugin.h"
|
|
#include "foreignopen.h"
|
|
|
|
static int failures;
|
|
|
|
/*
|
|
* Poison the stack below this frame. A function that reads an uninitialised
|
|
* local then sees 0xAA bytes instead of a zeroed page, which turns "undefined
|
|
* behaviour that happens to work" into a crash the test can catch.
|
|
*/
|
|
static void
|
|
dirty_stack(int depth)
|
|
{
|
|
volatile char junk[512];
|
|
size_t i;
|
|
|
|
for (i = 0; i < sizeof(junk); i++)
|
|
junk[i] = (char)0xAA;
|
|
if (depth > 0)
|
|
dirty_stack(depth - 1);
|
|
}
|
|
|
|
/* a row callback that does nothing: only the refusal is being checked */
|
|
static void
|
|
noop_row(void *ctx, const char *id, const char *label, long mtime)
|
|
{
|
|
(void)ctx;
|
|
(void)id;
|
|
(void)label;
|
|
(void)mtime;
|
|
}
|
|
|
|
#define CHECK(cond) do { \
|
|
if (!(cond)) { \
|
|
fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, \
|
|
#cond); \
|
|
failures++; \
|
|
} \
|
|
} while (0)
|
|
|
|
static int
|
|
contains(const char *hay, const char *needle)
|
|
{
|
|
return hay != NULL && strstr(hay, needle) != NULL;
|
|
}
|
|
|
|
static void
|
|
write_text(const char *path, const char *text)
|
|
{
|
|
FILE *f = fopen(path, "w");
|
|
|
|
CHECK(f != NULL);
|
|
if (f == NULL)
|
|
return;
|
|
fputs(text, f);
|
|
fclose(f);
|
|
}
|
|
|
|
static void
|
|
mkdir_p(const char *path)
|
|
{
|
|
char buf[800];
|
|
size_t i;
|
|
|
|
snprintf(buf, sizeof(buf), "%s", path);
|
|
for (i = 1; buf[i] != '\0'; i++) {
|
|
if (buf[i] != '/')
|
|
continue;
|
|
buf[i] = '\0';
|
|
(void)mkdir(buf, 0755);
|
|
buf[i] = '/';
|
|
}
|
|
(void)mkdir(buf, 0755);
|
|
}
|
|
|
|
/* "the directory a session ran in" as the Claude store spells it */
|
|
static void
|
|
claude_slug(const char *dir, char *out, size_t cap)
|
|
{
|
|
size_t i, n = 0;
|
|
|
|
for (i = 0; dir[i] != '\0' && n + 1 < cap; i++)
|
|
out[n++] = dir[i] == '/' ? '-' : dir[i];
|
|
out[n] = '\0';
|
|
}
|
|
|
|
/* one Claude session that ran in `cwd`, with a title the import takes over */
|
|
static void
|
|
claude_session(const char *home, const char *cwd, const char *uuid,
|
|
const char *title, const char *asked, const char *answered)
|
|
{
|
|
char slug[400], store[1200], path[1300];
|
|
struct sp_buf j;
|
|
|
|
claude_slug(cwd, slug, sizeof(slug));
|
|
snprintf(store, sizeof(store), "%s/.claude/projects/%s", home, slug);
|
|
mkdir_p(store);
|
|
snprintf(path, sizeof(path), "%s/%s.jsonl", store, uuid);
|
|
CHECK(sp_buf_init(&j) == 0);
|
|
sp_buf_printf(&j, "{\"type\":\"summary\",\"summary\":\"%s\","
|
|
"\"cwd\":\"%s\"}\n", title, cwd);
|
|
sp_buf_printf(&j, "{\"type\":\"user\",\"cwd\":\"%s\",\"message\":"
|
|
"{\"role\":\"user\",\"content\":\"%s\"}}\n", cwd, asked);
|
|
sp_buf_printf(&j, "{\"type\":\"assistant\",\"cwd\":\"%s\",\"message\":"
|
|
"{\"role\":\"assistant\",\"content\":[{\"type\":\"text\","
|
|
"\"text\":\"%s\"}]}}\n", cwd, answered);
|
|
write_text(path, j.p);
|
|
sp_buf_free(&j);
|
|
}
|
|
|
|
|
|
/* the rows one listing yields: id, label and the date the picker draws */
|
|
struct seen_rows {
|
|
int n;
|
|
long mtime;
|
|
char id[96];
|
|
char label[192];
|
|
};
|
|
|
|
static void
|
|
seen_row(void *ctx, const char *id, const char *label, long mtime)
|
|
{
|
|
struct seen_rows *r = ctx;
|
|
|
|
if (r->n == 0) {
|
|
snprintf(r->id, sizeof(r->id), "%s", id);
|
|
snprintf(r->label, sizeof(r->label), "%s", label);
|
|
r->mtime = mtime;
|
|
}
|
|
r->n++;
|
|
}
|
|
|
|
/* how many sessions with this id prefix the directory holds */
|
|
static int
|
|
sessions_named(const char *dir, const char *prefix)
|
|
{
|
|
struct sp_session_brief v[64];
|
|
int n, i, found = 0;
|
|
|
|
n = sp_session_list(dir, v, sizeof(v) / sizeof(v[0]));
|
|
for (i = 0; i < n; i++)
|
|
if (strncmp(v[i].id, prefix, strlen(prefix)) == 0)
|
|
found++;
|
|
return found;
|
|
}
|
|
|
|
/* does exactly this session exist */
|
|
static int
|
|
session_exists(const char *dir, const char *id)
|
|
{
|
|
struct sp_session_brief v[64];
|
|
int n, i;
|
|
|
|
n = sp_session_list(dir, v, sizeof(v) / sizeof(v[0]));
|
|
for (i = 0; i < n; i++)
|
|
if (strcmp(v[i].id, id) == 0)
|
|
return 1;
|
|
return 0;
|
|
}
|
|
|
|
/* the whole file of one session, for "is the old message still there" */
|
|
static void
|
|
read_session(const char *dir, const char *id, struct sp_buf *out)
|
|
{
|
|
char path[900];
|
|
FILE *f;
|
|
char buf[4096];
|
|
size_t n;
|
|
|
|
snprintf(path, sizeof(path), "%s/%s.jsonl", dir, id);
|
|
f = fopen(path, "r");
|
|
if (f == NULL)
|
|
return;
|
|
while ((n = fread(buf, 1, sizeof(buf), f)) > 0)
|
|
(void)sp_buf_add(out, buf, n);
|
|
fclose(f);
|
|
}
|
|
|
|
int
|
|
main(void)
|
|
{
|
|
char base[512], home[700], work[700], work2[700];
|
|
char sessions[800], back[600];
|
|
struct sp_host *host;
|
|
struct sp_registry reg;
|
|
struct sp_agent ag;
|
|
struct sp_session_info sess;
|
|
struct sp_foreign_target ft;
|
|
struct sp_buf err, out;
|
|
int rc;
|
|
|
|
(void)getcwd(back, sizeof(back));
|
|
snprintf(base, sizeof(base), "/tmp/sp_fo_XXXXXX");
|
|
CHECK(mkdtemp(base) != NULL);
|
|
snprintf(home, sizeof(home), "%s/home", base);
|
|
snprintf(work, sizeof(work), "%s/work", base);
|
|
snprintf(work2, sizeof(work2), "%s/empty", base);
|
|
mkdir_p(home);
|
|
mkdir_p(work);
|
|
mkdir_p(work2);
|
|
CHECK(setenv("HOME", home, 1) == 0);
|
|
/*
|
|
* The plugin reads its stores at the first call and caches the paths,
|
|
* so HOME has to be in place before the host exists.
|
|
*/
|
|
claude_session(home, work, "11111111-2222-3333-4444-555555555555",
|
|
"CC-TITLE", "the claude question", "the claude answer");
|
|
snprintf(sessions, sizeof(sessions), "%s/.splinter/sessions", home);
|
|
|
|
memset(®, 0, sizeof(reg));
|
|
memset(&ag, 0, sizeof(ag));
|
|
memset(&sess, 0, sizeof(sess));
|
|
sp_buf_init(&err);
|
|
sp_buf_init(&out);
|
|
CHECK(sp_session_open(&sess, sessions, "sess-placeholder", &err) == 0);
|
|
host = sp_host_new(®, &ag, &sess);
|
|
CHECK(host != NULL);
|
|
sp_buf_clear(&err);
|
|
CHECK(sp_plugin_load(host, "plugins/foreign/foreign.so", &err) == 0);
|
|
sp_buf_clear(&err);
|
|
ft.sess = &sess;
|
|
ft.reg = ®
|
|
ft.host = host;
|
|
ft.emb = NULL;
|
|
|
|
/* --- the import: the newest claude session of this directory wins --- */
|
|
rc = sp_foreign_open(&ft, "claude", work, &err);
|
|
CHECK(rc == 0);
|
|
CHECK(strncmp(sess.id, "fcl-", 4) == 0);
|
|
CHECK(strstr(sess.id, "11111111-2222-3333-4444-555555555555") != NULL);
|
|
sp_buf_clear(&out);
|
|
read_session(sess.dir, sess.id, &out);
|
|
CHECK(contains(out.p, "the claude question"));
|
|
CHECK(contains(out.p, "the claude answer"));
|
|
/* the origin is what makes a second start continue instead of re-import */
|
|
{
|
|
char marker[200];
|
|
|
|
CHECK(sp_session_foreign(sess.dir, sess.id, marker,
|
|
sizeof(marker)) == 0);
|
|
CHECK(contains(marker, "claude:"));
|
|
}
|
|
|
|
/* --- what the conversation did afterwards must survive a restart --- */
|
|
{
|
|
struct sp_buf serr;
|
|
|
|
sp_buf_init(&serr);
|
|
CHECK(sp_session_append(&sess, "user", "spoken in splinter",
|
|
&serr) == 0);
|
|
sp_buf_free(&serr);
|
|
}
|
|
memset(&sess, 0, sizeof(sess));
|
|
CHECK(sp_session_open(&sess, sessions, "sess-placeholder2", &err) == 0);
|
|
ft.sess = &sess;
|
|
sp_buf_clear(&err);
|
|
rc = sp_foreign_open(&ft, "claude", work, &err);
|
|
CHECK(rc == 0);
|
|
sp_buf_clear(&out);
|
|
read_session(sess.dir, sess.id, &out);
|
|
CHECK(contains(out.p, "spoken in splinter"));
|
|
/* exactly one import: a second one would have replaced the file */
|
|
CHECK(sessions_named(sessions, "fcl-") == 1);
|
|
|
|
/*
|
|
* --- importing the same session twice replaces, it does not append ---
|
|
*
|
|
* The engine shields this by continuing an existing import, so it has to
|
|
* be checked on the tool itself: import_any deletes the target file
|
|
* first, and whether it finds it depends on how the path is built. It
|
|
* was built as dir/<id>/.jsonl - a directory that never exists - so the
|
|
* delete never happened and every import hung the whole history onto
|
|
* the last one.
|
|
*/
|
|
{
|
|
const struct sp_tool *imp = sp_registry_find(®, "foreign_import");
|
|
struct sp_buf iout, serr;
|
|
struct sp_session_info is;
|
|
size_t count = 0;
|
|
int pass;
|
|
|
|
CHECK(imp != NULL);
|
|
sp_buf_init(&iout);
|
|
sp_buf_init(&serr);
|
|
for (pass = 0; pass < 3; pass++) {
|
|
sp_buf_clear(&iout);
|
|
CHECK(imp->run("{\"source\":\"claude\",\"id\":"
|
|
"\"11111111-2222-3333-4444-555555555555\"}", &iout,
|
|
imp->ctx) == 0);
|
|
}
|
|
CHECK(sp_session_open(&is, sessions,
|
|
"fcl-11111111-2222-3333-4444-555555555555",
|
|
&serr) == 0);
|
|
CHECK(sp_session_count(&is, &count, &serr) == 0);
|
|
/* two messages in the source, however often it is imported */
|
|
CHECK(count == 2);
|
|
sp_buf_free(&iout);
|
|
sp_buf_free(&serr);
|
|
}
|
|
|
|
/* --- a directory with no claude session is an error, not a session --- */
|
|
memset(&sess, 0, sizeof(sess));
|
|
CHECK(sp_session_open(&sess, sessions, "sess-empty", &err) == 0);
|
|
ft.sess = &sess;
|
|
sp_buf_clear(&err);
|
|
rc = sp_foreign_open(&ft, "claude", work2, &err);
|
|
CHECK(rc != 0);
|
|
CHECK(contains(err.p, "no claude session"));
|
|
CHECK(sessions_named(sessions, "fcl-") == 1);
|
|
|
|
/* --- an unknown source is refused by name --- */
|
|
sp_buf_clear(&err);
|
|
CHECK(sp_foreign_open(&ft, "cursor", work, &err) != 0);
|
|
CHECK(contains(err.p, "not a source"));
|
|
|
|
/* --- the placeholder is removable, a session with content is not --- */
|
|
{
|
|
struct sp_session_info full;
|
|
struct sp_buf serr;
|
|
|
|
memset(&full, 0, sizeof(full));
|
|
sp_buf_init(&serr);
|
|
CHECK(sp_session_open(&full, sessions, "sess-placeholder",
|
|
&serr) == 0);
|
|
CHECK(sp_session_append(&full, "user", "work that must survive",
|
|
&serr) == 0);
|
|
sp_buf_free(&serr);
|
|
}
|
|
memset(&sess, 0, sizeof(sess));
|
|
CHECK(sp_session_open(&sess, sessions, "sess-emptyshell", &err) == 0);
|
|
CHECK(sp_session_remove_if_empty(sessions, "sess-emptyshell") == 0);
|
|
CHECK(session_exists(sessions, "sess-emptyshell") == 0);
|
|
CHECK(sp_session_remove_if_empty(sessions, "sess-placeholder") != 0);
|
|
CHECK(session_exists(sessions, "sess-placeholder") == 1);
|
|
|
|
/*
|
|
* --- without the plugin the picker's source list is empty, not a crash ---
|
|
*
|
|
* sp_foreign_sources() used to hand an *uninitialised* sp_buf to the
|
|
* helper that reports why the list tool is missing, so the reason was
|
|
* written through a garbage pointer. A registry with nothing registered
|
|
* reproduces that in one call.
|
|
*/
|
|
{
|
|
struct sp_registry empty;
|
|
struct sp_foreign_target bare;
|
|
struct sp_foreign_source fs[4];
|
|
struct sp_buf serr;
|
|
|
|
memset(&empty, 0, sizeof(empty));
|
|
dirty_stack(8);
|
|
sp_buf_init(&serr);
|
|
bare.sess = &sess;
|
|
bare.reg = ∅
|
|
bare.host = host;
|
|
bare.emb = NULL;
|
|
CHECK(sp_foreign_sources(&bare, fs, 4) == 0);
|
|
CHECK(sp_foreign_sessions(&bare, "claude", noop_row, NULL,
|
|
&serr) != 0);
|
|
CHECK(contains(serr.p, "foreign plugin is not loaded"));
|
|
sp_buf_free(&serr);
|
|
/* a target with no registry at all is refused the same way */
|
|
bare.reg = NULL;
|
|
CHECK(sp_foreign_sources(&bare, fs, 4) == 0);
|
|
}
|
|
|
|
/*
|
|
* --- the picker's source list: a real count and a real date ---
|
|
*
|
|
* sp_foreign_sources() used to hard-set count to 0, so the picker said
|
|
* "claude (0)" next to 82 rows, and sp_foreign_sessions() hard-set the
|
|
* date to 0, so every foreign row drew as 1970-01-01. Both are read
|
|
* from the same listing now; the fixture above has exactly one claude
|
|
* session in this kit's store.
|
|
*/
|
|
{
|
|
struct sp_foreign_source fs[4];
|
|
struct seen_rows rows;
|
|
struct sp_buf serr;
|
|
int ns, i, claude = -1;
|
|
|
|
ns = sp_foreign_sources(&ft, fs, 4);
|
|
CHECK(ns >= 1);
|
|
for (i = 0; i < ns; i++)
|
|
if (strcmp(fs[i].name, "claude") == 0)
|
|
claude = i;
|
|
CHECK(claude >= 0);
|
|
if (claude >= 0)
|
|
CHECK(fs[claude].count == 1);
|
|
|
|
memset(&rows, 0, sizeof(rows));
|
|
sp_buf_init(&serr);
|
|
CHECK(sp_foreign_sessions(&ft, "claude", seen_row, &rows,
|
|
&serr) == 1);
|
|
CHECK(rows.n == 1);
|
|
CHECK(strcmp(rows.id,
|
|
"11111111-2222-3333-4444-555555555555") == 0);
|
|
/* the store was written now, so the date is today, not 1970 */
|
|
CHECK(rows.mtime > 1600000000L);
|
|
sp_buf_free(&serr);
|
|
}
|
|
|
|
sp_buf_free(&err);
|
|
sp_buf_free(&out);
|
|
sp_host_free(host);
|
|
sp_registry_free(®);
|
|
{
|
|
char cmd[900];
|
|
|
|
snprintf(cmd, sizeof(cmd), "rm -rf %s", base);
|
|
if (system(cmd) != 0)
|
|
fprintf(stderr, "note: %s left behind\n", base);
|
|
}
|
|
CHECK(chdir(back) == 0);
|
|
if (failures != 0) {
|
|
fprintf(stderr, "test_foreignopen: %d failure(s)\n", failures);
|
|
return 1;
|
|
}
|
|
printf("test_foreignopen: ok\n");
|
|
return 0;
|
|
}
|