The operator's claude ran into its limit mid-work; the session was too full to continue there. The foreign plugin could already read and import it, but only through a tool call inside a turn - there was no way in from the command line. --claude, --opencode, --hermes and --from=NAME make the newest session that tool ran in this directory the session of the run: it is imported before the history is loaded, so the first turn already carries it. -c no longer fails in a folder that has no splinter session yet; it falls back to "no session" and lets the import decide. An empty placeholder session is removed again, whether the import worked or not. Repeating the call continues the same session instead of importing a second time: sp_session_foreign() reads the foreign= marker back, so the work done in splinter is kept. A second foreign_import into the same session replaces the history rather than appending it twice (path_join built "id/.jsonl", so the replace branch never fired). Test: tests/test_foreignopen.c builds a claude fixture under its own HOME and covers import, idempotency, replace-not-append, the empty placeholder and a folder with no foreign session. make check exit 0, check_asan.sh clean.
332 lines
9.5 KiB
C
332 lines
9.5 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;
|
|
|
|
#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);
|
|
}
|
|
|
|
/* 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);
|
|
|
|
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;
|
|
}
|