The command built its JSON request with the operator's words as a plain %s inside a string, so /foreign search he said "hi" there ended the value early and was answered with "'query' is required" - the whole phrase lost. The text (and the source/id pair) goes through sp_json_escape now. before: /foreign search he said "hi" there -> error: 'query' is required after: the same command reaches the stores and answers tests/test_foreign: red with the old line (2 failures), green with the fix make check: all tests passed, rc=0
749 lines
26 KiB
C
749 lines
26 KiB
C
/*
|
|
* splinter -- the foreign plugin: claude, hermes and opencode sessions.
|
|
*
|
|
* Copyright (c) 2026 Konrad Lother
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
*
|
|
* What it guards, against fixtures it writes itself (the machine's real stores
|
|
* are not a test):
|
|
*
|
|
* - every source lists its sessions with title, date and the directory the
|
|
* conversation ran in, and a cwd filter really filters;
|
|
* - reading returns the conversation in order, with the tool call before its
|
|
* result, and no thinking block;
|
|
* - an import writes a splinter session that sp_session_load() reads back as
|
|
* a conversation a provider accepts: every tool_calls is followed by its
|
|
* observation, every observation has a call, no blank turn;
|
|
* - the import lands in the directory the foreign session ran in, so -c and
|
|
* /sessions find it where it happened;
|
|
* - an unknown id is an error, not an empty answer, and a title is one line.
|
|
*
|
|
* The three stores are the fixture's own: SPLINTER_FOREIGN_CLAUDE/_HERMES/
|
|
* _OPENCODE point the plugin at them.
|
|
*/
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/stat.h>
|
|
#include <sys/types.h>
|
|
#include <unistd.h>
|
|
|
|
#include "mcp.h"
|
|
#include "splinter.h"
|
|
#include "splinter_plugin.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_file(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, n = strlen(path);
|
|
|
|
CHECK(n < sizeof(buf));
|
|
if (n >= sizeof(buf))
|
|
return;
|
|
memcpy(buf, path, n + 1);
|
|
for (i = 1; i < n; i++) {
|
|
if (buf[i] != '/')
|
|
continue;
|
|
buf[i] = '\0';
|
|
(void)mkdir(buf, 0700);
|
|
buf[i] = '/';
|
|
}
|
|
CHECK(mkdir(buf, 0700) == 0 || 1);
|
|
}
|
|
|
|
static int
|
|
run_sql(const char *db, const char *sql)
|
|
{
|
|
char cmd[4096 + 64], path[256];
|
|
FILE *f;
|
|
int rc;
|
|
|
|
/*
|
|
* The SQL goes through a file, not through the shell. Passing it as a
|
|
* quoted argument looked fine until the JSON fixtures arrived: they
|
|
* contain double quotes, so '{"text":"OC-NEEDLE"}' ended the shell's
|
|
* quoting in the middle of the statement and sqlite3 saw a fragment
|
|
* ("unrecognized token"). A file has no quoting to get wrong.
|
|
*/
|
|
snprintf(path, sizeof(path), "/tmp/splinter-foreign-sql-%ld.sql",
|
|
(long)getpid());
|
|
f = fopen(path, "w");
|
|
if (f == NULL)
|
|
return -1;
|
|
fputs(sql, f);
|
|
fclose(f);
|
|
snprintf(cmd, sizeof(cmd), "sqlite3 -batch '%s' < '%s'", db, path);
|
|
rc = system(cmd);
|
|
unlink(path);
|
|
return rc == 0 ? 0 : -1;
|
|
}
|
|
|
|
/*
|
|
* The call and its answer both survived: an import that drops tool calls keeps
|
|
* only the prose and still looks valid, so this checks the content, not the
|
|
* shape. `call_id` must name the call, `name` must be the tool's.
|
|
*/
|
|
static int
|
|
session_has_tool_pair(const char *dir, const char *id, const char *call_id,
|
|
const char *name, struct sp_buf *why)
|
|
{
|
|
struct sp_session_info si;
|
|
struct sp_conv conv;
|
|
struct sp_buf err;
|
|
size_t loaded = 0, i;
|
|
int have_call = 0, have_result = 0, rc = 0;
|
|
|
|
memset(&si, 0, sizeof(si));
|
|
if (sp_session_open(&si, dir, id, why) != 0)
|
|
return -1;
|
|
sp_conv_init(&conv);
|
|
sp_buf_init(&err);
|
|
if (sp_session_load(&si, &conv, &loaded, &err) != 0) {
|
|
sp_buf_printf(why, "load failed: %s", err.p != NULL ? err.p : "?");
|
|
sp_conv_free(&conv);
|
|
sp_buf_free(&err);
|
|
return -1;
|
|
}
|
|
for (i = 0; i < conv.n; i++) {
|
|
struct sp_msg *m = &conv.v[i];
|
|
|
|
if (strcmp(m->role, "assistant") == 0 && m->calls != NULL) {
|
|
mcp_json *a = mcp_parse(m->calls);
|
|
|
|
if (a != NULL && a->t == MCP_ARR &&
|
|
strstr(m->calls, call_id) != NULL &&
|
|
strstr(m->calls, name) != NULL)
|
|
have_call = 1;
|
|
mcp_json_free(a);
|
|
} else if (strcmp(m->role, "tool") == 0 &&
|
|
m->call_id != NULL && strcmp(m->call_id, call_id) == 0 &&
|
|
m->name != NULL && strcmp(m->name, name) == 0)
|
|
have_result = 1;
|
|
}
|
|
if (!have_call)
|
|
sp_buf_printf(why, "no assistant message carries a call to "
|
|
"%s (%s)", name, call_id), rc = -1;
|
|
else if (!have_result)
|
|
sp_buf_printf(why, "no tool message answers %s", call_id), rc = -1;
|
|
sp_conv_free(&conv);
|
|
sp_buf_free(&err);
|
|
return rc;
|
|
}
|
|
|
|
/* the conversation a provider would accept, out of a session file */
|
|
static int
|
|
conversation_ok(const char *dir, const char *id, struct sp_buf *why)
|
|
{
|
|
struct sp_session_info si;
|
|
struct sp_conv conv;
|
|
struct sp_buf err;
|
|
size_t loaded = 0, i;
|
|
char open_calls[64][80];
|
|
size_t nopen = 0;
|
|
int rc = 0;
|
|
|
|
memset(&si, 0, sizeof(si));
|
|
if (sp_session_open(&si, dir, id, why) != 0)
|
|
return -1;
|
|
sp_conv_init(&conv);
|
|
sp_buf_init(&err);
|
|
if (sp_session_load(&si, &conv, &loaded, &err) != 0) {
|
|
sp_buf_printf(why, "load failed: %s", err.p != NULL ? err.p : "?");
|
|
sp_conv_free(&conv);
|
|
sp_buf_free(&err);
|
|
return -1;
|
|
}
|
|
for (i = 0; i < conv.n && rc == 0; i++) {
|
|
struct sp_msg *m = &conv.v[i];
|
|
|
|
if (m->role == NULL) {
|
|
sp_buf_printf(why, "message %zu has no role", i);
|
|
rc = -1;
|
|
} else if (strcmp(m->role, "assistant") == 0) {
|
|
/* every call must be answered by the next tool turns */
|
|
nopen = 0;
|
|
if (m->calls != NULL && m->calls[0] != '\0') {
|
|
mcp_json *a = mcp_parse(m->calls);
|
|
size_t k;
|
|
|
|
if (a == NULL || a->t != MCP_ARR) {
|
|
sp_buf_printf(why, "message %zu: calls "
|
|
"not a JSON array", i);
|
|
rc = -1;
|
|
} else {
|
|
for (k = 0; k < mcp_array_len(a) &&
|
|
nopen < 64; k++) {
|
|
const char *cid =
|
|
mcp_arg_string(
|
|
mcp_array_at(a, k), "id");
|
|
const mcp_json *fn =
|
|
mcp_arg(mcp_array_at(a, k),
|
|
"function");
|
|
|
|
if (cid == NULL || fn == NULL) {
|
|
sp_buf_printf(why,
|
|
"message %zu: a call "
|
|
"has no id or no "
|
|
"function", i);
|
|
rc = -1;
|
|
break;
|
|
}
|
|
snprintf(open_calls[nopen++],
|
|
sizeof(open_calls[0]), "%s",
|
|
cid);
|
|
}
|
|
}
|
|
mcp_json_free(a);
|
|
}
|
|
if ((m->content == NULL || m->content[0] == '\0') &&
|
|
(m->calls == NULL || m->calls[0] == '\0')) {
|
|
sp_buf_printf(why, "message %zu: empty assistant "
|
|
"turn", i);
|
|
rc = -1;
|
|
}
|
|
} else if (strcmp(m->role, "tool") == 0) {
|
|
if (m->call_id == NULL || m->call_id[0] == '\0') {
|
|
sp_buf_printf(why, "message %zu: tool without a "
|
|
"call id", i);
|
|
rc = -1;
|
|
} else if (nopen == 0) {
|
|
sp_buf_printf(why, "message %zu: tool result %s "
|
|
"with no call before it", i, m->call_id);
|
|
rc = -1;
|
|
}
|
|
} else if (strcmp(m->role, "user") == 0) {
|
|
nopen = 0;
|
|
}
|
|
}
|
|
if (rc == 0 && loaded == 0) {
|
|
sp_buf_printf(why, "loaded 0 messages");
|
|
rc = -1;
|
|
}
|
|
sp_conv_free(&conv);
|
|
sp_buf_free(&err);
|
|
return rc;
|
|
}
|
|
|
|
static const char *claude_jsonl =
|
|
"{\"type\":\"queue-operation\",\"operation\":\"enqueue\"}\n"
|
|
"{\"type\":\"user\",\"cwd\":\"/work/alpha\",\"isSidechain\":false,"
|
|
"\"message\":{\"role\":\"user\",\"content\":\"fix the FIXME in "
|
|
"main.c\"}}\n"
|
|
"{\"type\":\"assistant\",\"cwd\":\"/work/alpha\",\"message\":{\"id\":"
|
|
"\"msg_1\",\"role\":\"assistant\",\"content\":[{\"type\":\"thinking\","
|
|
"\"thinking\":\"THE-THINKING should never surface\"}]}}\n"
|
|
"{\"type\":\"assistant\",\"cwd\":\"/work/alpha\",\"message\":{\"id\":"
|
|
"\"msg_1\",\"role\":\"assistant\",\"content\":[{\"type\":\"text\","
|
|
"\"text\":\"I will read main.c.\"}]}}\n"
|
|
"{\"type\":\"assistant\",\"cwd\":\"/work/alpha\",\"message\":{\"id\":"
|
|
"\"msg_1\",\"role\":\"assistant\",\"content\":[{\"type\":\"tool_use\","
|
|
"\"id\":\"toolu_a\",\"name\":\"read\",\"input\":{\"path\":\"main.c\"}}]}}\n"
|
|
"{\"type\":\"user\",\"cwd\":\"/work/alpha\",\"message\":{\"role\":"
|
|
"\"user\",\"content\":[{\"type\":\"tool_result\",\"tool_use_id\":"
|
|
"\"toolu_a\",\"content\":[{\"type\":\"text\",\"text\":\"int main(){} "
|
|
"// FIXME\"}]}]}}\n"
|
|
"{\"type\":\"assistant\",\"cwd\":\"/work/alpha\",\"message\":{\"id\":"
|
|
"\"msg_2\",\"role\":\"assistant\",\"content\":[{\"type\":\"text\","
|
|
"\"text\":\"The FIXME is gone now.\"}]}}\n"
|
|
/* a subagent's own conversation: not part of this one */
|
|
"{\"type\":\"assistant\",\"isSidechain\":true,\"message\":{\"id\":"
|
|
"\"msg_side\",\"role\":\"assistant\",\"content\":[{\"type\":\"text\","
|
|
"\"text\":\"SIDECHAIN-NOISE\"}]}}\n";
|
|
|
|
int
|
|
main(void)
|
|
{
|
|
char tmpl[] = "/tmp/splinter-foreign-XXXXXX";
|
|
char *base, cla[900], her[900], ocd[900], sessions[900];
|
|
char path[1000], outbuf[8000];
|
|
struct sp_registry reg;
|
|
struct sp_agent ag;
|
|
struct sp_session_info cur;
|
|
struct sp_host *host;
|
|
struct sp_buf err, out;
|
|
const struct sp_tool *t_list, *t_search, *t_read, *t_import;
|
|
struct sp_session_brief v[200];
|
|
int n;
|
|
|
|
base = mkdtemp(tmpl);
|
|
CHECK(base != NULL);
|
|
if (base == NULL)
|
|
return 1;
|
|
snprintf(cla, sizeof(cla), "%s/claude", base);
|
|
snprintf(her, sizeof(her), "%s/hermes", base);
|
|
snprintf(ocd, sizeof(ocd), "%s/oc.db", base);
|
|
snprintf(sessions, sizeof(sessions), "%s/sessions", base);
|
|
|
|
mkdir_p(cla);
|
|
mkdir_p(her);
|
|
mkdir_p(sessions);
|
|
CHECK(setenv("SPLINTER_FOREIGN_CLAUDE", cla, 1) == 0);
|
|
CHECK(setenv("SPLINTER_FOREIGN_HERMES", her, 1) == 0);
|
|
CHECK(setenv("SPLINTER_FOREIGN_OPENCODE", ocd, 1) == 0);
|
|
|
|
/* --- claude: a project directory and one session with a tool call --- */
|
|
{
|
|
char proj[950];
|
|
|
|
snprintf(proj, sizeof(proj), "%s/-work-alpha", cla);
|
|
mkdir_p(proj);
|
|
snprintf(path, sizeof(path), "%s/aaaa-1111.jsonl", proj);
|
|
write_file(path, claude_jsonl);
|
|
}
|
|
|
|
/* --- hermes: state.db with one session and a tool round --- */
|
|
{
|
|
char dbe[950];
|
|
|
|
snprintf(dbe, sizeof(dbe), "%s/state.db", her);
|
|
CHECK(run_sql(dbe, "create table sessions (id text primary key, "
|
|
"title text, cwd text, started_at real, message_count int);") == 0);
|
|
CHECK(run_sql(dbe, "create table messages (id integer primary key "
|
|
"autoincrement, session_id text, role text, content text, "
|
|
"tool_call_id text, tool_calls text, tool_name text, "
|
|
"timestamp real, active int default 1, compacted int default "
|
|
"0);") == 0);
|
|
CHECK(run_sql(dbe, "insert into sessions values ('h-1','hermes "
|
|
"H ITLE','/work/beta',1795200000,4);") == 0);
|
|
CHECK(run_sql(dbe, "insert into messages (session_id,role,content,"
|
|
"timestamp) values ('h-1','user','HERMES-NEEDLE what does it "
|
|
"do',1);") == 0);
|
|
CHECK(run_sql(dbe, "insert into messages (session_id,role,content,"
|
|
"tool_calls,timestamp) values ('h-1','assistant','',"
|
|
"'[{\"id\":\"call_h\",\"type\":\"function\",\"function\":"
|
|
"{\"name\":\"grep\",\"arguments\":\"{\\\"q\\\":\\\"x\\\"}\"}}]',"
|
|
"2);") == 0);
|
|
CHECK(run_sql(dbe, "insert into messages (session_id,role,content,"
|
|
"tool_call_id,tool_name,timestamp) values ('h-1','tool',"
|
|
"'HERMES-OUTPUT: 3 matches','call_h','grep',3);") == 0);
|
|
CHECK(run_sql(dbe, "insert into messages (session_id,role,content,"
|
|
"timestamp) values ('h-1','assistant','three matches',4);") == 0);
|
|
/* a needle past the 400 bytes the search used to cut to */
|
|
CHECK(run_sql(dbe, "insert into messages (session_id,role,"
|
|
"content,timestamp) values ('h-1','user','' || "
|
|
"replace(hex(zeroblob(400)),'00','y') || ' HERMES-FAR',"
|
|
"1795200000000);") == 0);
|
|
/* an inactive row is history, not the conversation */
|
|
CHECK(run_sql(dbe, "insert into messages (session_id,role,content,"
|
|
"timestamp,active) values ('h-1','user','SUPERSEDED',5,0);") == 0);
|
|
}
|
|
|
|
/* --- opencode: session_v2 + session_message, one tool round --- */
|
|
CHECK(run_sql(ocd, "create table session_v2 (id text primary key, title "
|
|
"text, directory text, time_created integer, time_updated integer);")
|
|
== 0);
|
|
CHECK(run_sql(ocd, "create table session_message (id text primary key, "
|
|
"session_id text, type text, seq integer, time_created integer, "
|
|
"time_updated integer, data text);") == 0);
|
|
CHECK(run_sql(ocd, "insert into session_v2 values ('ses_oc1','oc TITLE',"
|
|
"'/work/gamma',1795200000000,1795200100000);") == 0);
|
|
CHECK(run_sql(ocd, "insert into session_message values ('m1','ses_oc1',"
|
|
"'user',1,1795200000000,1795200000000,'{\"text\":\"OC-NEEDLE "
|
|
"run the tests\"}');") == 0);
|
|
CHECK(run_sql(ocd, "insert into session_message values ('m2','ses_oc1',"
|
|
"'assistant',2,1795200001000,1795200001000,'{\"content\":[{\"type\":"
|
|
"\"reasoning\",\"text\":\"OC-REASONING must not surface\"},"
|
|
"{\"type\":\"text\",\"text\":\"Running them.\"},{\"type\":\"tool\","
|
|
"\"id\":\"call_oc\",\"name\":\"shell\",\"state\":{\"status\":"
|
|
"\"completed\",\"input\":{\"command\":\"make test\"},\"content\":"
|
|
"[{\"type\":\"text\",\"text\":\"OC-OUTPUT 12 passed\"}]}}]}');") == 0);
|
|
CHECK(run_sql(ocd, "insert into session_message values ('m3','ses_oc1',"
|
|
"'assistant',3,1795200002000,1795200002000,'{\"content\":[{\"type\":"
|
|
"\"text\",\"text\":\"All twelve pass.\"}]}');") == 0);
|
|
/*
|
|
* Two shapes search got wrong, both silent: an assistant message keeps
|
|
* its prose in content[], not in data.text, and a message longer than
|
|
* the 20000 bytes the search used to cut it to was dropped as
|
|
* unparsable JSON. The needle sits past byte 20000 in m4, so only a
|
|
* search that reads the whole row can find it.
|
|
*/
|
|
CHECK(run_sql(ocd, "insert into session_message values ('m4','ses_oc2',"
|
|
"'user',4,1795200003000,1795200003000,'{\"text\":\"OC-FAR ' || "
|
|
"replace(hex(zeroblob(12500)),'00','x') || '\"}');") == 0);
|
|
CHECK(run_sql(ocd, "insert into session_message values ('m5','ses_oc2',"
|
|
"'assistant',5,1795200004000,1795200004000,'{\"content\":[{\"type\":"
|
|
"\"text\",\"text\":\"OC-ASSISTANT-PROSE in the content array "
|
|
"only.\"}]}');") == 0);
|
|
CHECK(run_sql(ocd, "insert into session_v2 values ('ses_oc2','oc TWO',"
|
|
"'/work/gamma',1795200003000,1795200004000);") == 0);
|
|
|
|
/* a splinter session to give the host its directory */
|
|
memset(®, 0, sizeof(reg));
|
|
memset(&ag, 0, sizeof(ag));
|
|
memset(&cur, 0, sizeof(cur));
|
|
sp_buf_init(&err);
|
|
sp_buf_init(&out);
|
|
CHECK(sp_session_open(&cur, sessions, "sess-cur", &err) == 0);
|
|
sp_buf_clear(&err);
|
|
host = sp_host_new(®, &ag, &cur);
|
|
CHECK(host != NULL);
|
|
CHECK(sp_plugin_load(host, "plugins/foreign/foreign.so", &err) == 0);
|
|
|
|
t_list = sp_registry_find(®, "foreign_list");
|
|
t_search = sp_registry_find(®, "foreign_search");
|
|
t_read = sp_registry_find(®, "foreign_read");
|
|
t_import = sp_registry_find(®, "foreign_import");
|
|
CHECK(t_list != NULL && t_search != NULL && t_read != NULL &&
|
|
t_import != NULL);
|
|
if (t_list == NULL || t_search == NULL || t_read == NULL ||
|
|
t_import == NULL) {
|
|
fprintf(stderr, "plugin not loaded: %s\n",
|
|
err.p != NULL ? err.p : "?");
|
|
goto out;
|
|
}
|
|
/* an import writes a session: it must not be a read tool */
|
|
CHECK(t_import->auth == SP_AUTH_MUTATE);
|
|
CHECK(t_list->auth == SP_AUTH_READ && t_search->auth == SP_AUTH_READ &&
|
|
t_read->auth == SP_AUTH_READ);
|
|
|
|
/* --- list: all three stores, cwd filters really filter --- */
|
|
CHECK(t_list->run("{\"cwd\":\"*\"}", &out, t_list->ctx) == 0);
|
|
CHECK(contains(out.p, "aaaa-1111"));
|
|
CHECK(contains(out.p, "h-1"));
|
|
CHECK(contains(out.p, "ses_oc1"));
|
|
CHECK(contains(out.p, "/work/alpha") && contains(out.p, "/work/beta") &&
|
|
contains(out.p, "/work/gamma"));
|
|
sp_buf_clear(&out);
|
|
|
|
CHECK(t_list->run("{\"cwd\":\"/work/beta\"}", &out, t_list->ctx) == 0);
|
|
CHECK(contains(out.p, "h-1"));
|
|
CHECK(!contains(out.p, "ses_oc1"));
|
|
CHECK(!contains(out.p, "aaaa-1111"));
|
|
sp_buf_clear(&out);
|
|
|
|
/*
|
|
* A cwd whose slug directory exists: the list must read that one
|
|
* directory and not walk everything. The second project below holds a
|
|
* session that claims /work/alpha too - a directory walk would list it,
|
|
* the slug lookup must not. claude_slug_dir used to answer -1 for every
|
|
* non-empty path (inverted check), so the fast path was dead and the
|
|
* walk decided.
|
|
*/
|
|
{
|
|
char proj[950], other[950];
|
|
|
|
snprintf(proj, sizeof(proj), "%s/-work-alpha", cla);
|
|
mkdir_p(proj);
|
|
snprintf(other, sizeof(other), "%s/-work-omega", cla);
|
|
mkdir_p(other);
|
|
snprintf(path, sizeof(path), "%s/dead-beef.jsonl", other);
|
|
write_file(path, claude_jsonl);
|
|
}
|
|
CHECK(t_list->run("{\"source\":\"claude\",\"cwd\":\"/work/alpha\"}",
|
|
&out, t_list->ctx) == 0);
|
|
CHECK(contains(out.p, "aaaa-1111"));
|
|
CHECK(!contains(out.p, "dead-beef"));
|
|
sp_buf_clear(&out);
|
|
|
|
CHECK(t_list->run("{\"source\":\"opencode\",\"cwd\":\"*\"}", &out,
|
|
t_list->ctx) == 0);
|
|
CHECK(contains(out.p, "ses_oc1"));
|
|
CHECK(!contains(out.p, "h-1"));
|
|
sp_buf_clear(&out);
|
|
|
|
CHECK(t_list->run("{\"cwd\":\"/nowhere\"}", &out, t_list->ctx) == 0);
|
|
CHECK(contains(out.p, "no foreign sessions"));
|
|
sp_buf_clear(&out);
|
|
|
|
/* --- search: case-blind, hits name the session, the answer is not a hit */
|
|
CHECK(t_search->run("{\"query\":\"hermes-needle\"}", &out,
|
|
t_search->ctx) == 0);
|
|
CHECK(contains(out.p, "h-1"));
|
|
sp_buf_clear(&out);
|
|
CHECK(t_search->run("{\"query\":\"OC-OUTPUT\"}", &out, t_search->ctx) == 0);
|
|
CHECK(contains(out.p, "no message"));
|
|
sp_buf_clear(&out);
|
|
/*
|
|
* The two silent holes the search had. `content[]` is where an
|
|
* assistant message keeps its prose; a needle past byte 400 (hermes)
|
|
* or byte 20000 (opencode) existed in the row but was cut away before
|
|
* it was looked for.
|
|
*/
|
|
CHECK(t_search->run("{\"query\":\"OC-ASSISTANT-PROSE\"}", &out,
|
|
t_search->ctx) == 0);
|
|
CHECK(contains(out.p, "ses_oc2"));
|
|
sp_buf_clear(&out);
|
|
CHECK(t_search->run("{\"query\":\"OC-FAR\"}", &out, t_search->ctx) == 0);
|
|
CHECK(contains(out.p, "ses_oc2"));
|
|
sp_buf_clear(&out);
|
|
CHECK(t_search->run("{\"query\":\"HERMES-FAR\"}", &out, t_search->ctx) ==
|
|
0);
|
|
CHECK(contains(out.p, "h-1"));
|
|
sp_buf_clear(&out);
|
|
CHECK(t_search->run("{\"query\":\"main.c\"}", &out, t_search->ctx) == 0);
|
|
CHECK(contains(out.p, "aaaa-1111"));
|
|
sp_buf_clear(&out);
|
|
CHECK(t_search->run("{\"query\":\"NOT-ANYWHERE-AT-ALL\"}", &out,
|
|
t_search->ctx) == 0);
|
|
CHECK(contains(out.p, "no message"));
|
|
sp_buf_clear(&out);
|
|
|
|
/*
|
|
* The cwd filter the schema promises. It was read and then thrown away
|
|
* ("the stores are searched whole"), so a search that named a folder
|
|
* answered from every folder -- the caller got another folder's fix and
|
|
* no sign that the filter did nothing.
|
|
*/
|
|
CHECK(t_search->run("{\"query\":\"HERMES-NEEDLE\",\"cwd\":\"/work/gamma\"}",
|
|
&out, t_search->ctx) == 0);
|
|
CHECK(contains(out.p, "no message"));
|
|
sp_buf_clear(&out);
|
|
CHECK(t_search->run("{\"query\":\"HERMES-NEEDLE\",\"cwd\":\"/work/beta\"}",
|
|
&out, t_search->ctx) == 0);
|
|
CHECK(contains(out.p, "h-1"));
|
|
sp_buf_clear(&out);
|
|
/* claude: a slug directory exists for /work/alpha, so this is the fast
|
|
* path, and the transcript that says /work/omega must stay out */
|
|
CHECK(t_search->run("{\"query\":\"FIXME\",\"cwd\":\"/work/alpha\"}",
|
|
&out, t_search->ctx) == 0);
|
|
CHECK(contains(out.p, "aaaa-1111"));
|
|
CHECK(!contains(out.p, "dead-beef"));
|
|
sp_buf_clear(&out);
|
|
/*
|
|
* The other direction: dead-beef sits in the slug directory of
|
|
* /work/omega but its transcript says /work/alpha. Asking for omega
|
|
* must not return it -- the file's own claim is what the conversation
|
|
* ran in, not the directory it was filed under.
|
|
*/
|
|
CHECK(t_search->run("{\"query\":\"FIXME\",\"cwd\":\"/work/omega\"}",
|
|
&out, t_search->ctx) == 0);
|
|
CHECK(!contains(out.p, "dead-beef"));
|
|
sp_buf_clear(&out);
|
|
/* opencode, through its own session table */
|
|
CHECK(t_search->run("{\"query\":\"OC-NEEDLE\",\"cwd\":\"/work/beta\"}",
|
|
&out, t_search->ctx) == 0);
|
|
CHECK(contains(out.p, "no message"));
|
|
sp_buf_clear(&out);
|
|
CHECK(t_search->run("{\"query\":\"OC-NEEDLE\",\"cwd\":\"/work/gamma\"}",
|
|
&out, t_search->ctx) == 0);
|
|
CHECK(contains(out.p, "ses_oc1"));
|
|
sp_buf_clear(&out);
|
|
/* cwd="*" is every folder, and so is no cwd at all */
|
|
CHECK(t_search->run("{\"query\":\"HERMES-NEEDLE\",\"cwd\":\"*\"}", &out,
|
|
t_search->ctx) == 0);
|
|
CHECK(contains(out.p, "h-1"));
|
|
sp_buf_clear(&out);
|
|
|
|
CHECK(t_search->run("{}", &out, t_search->ctx) == 0);
|
|
CHECK(contains(out.p, "error: 'query' is required"));
|
|
sp_buf_clear(&out);
|
|
|
|
/* --- read: order, the call before its result, no thinking/reasoning --- */
|
|
CHECK(t_read->run("{\"source\":\"claude\",\"id\":\"aaaa-1111\"}", &out,
|
|
t_read->ctx) == 0);
|
|
snprintf(outbuf, sizeof(outbuf), "%s", out.p != NULL ? out.p : "");
|
|
CHECK(contains(outbuf, "fix the FIXME"));
|
|
CHECK(contains(outbuf, "I will read main.c."));
|
|
CHECK(contains(outbuf, "[tool read]"));
|
|
CHECK(contains(outbuf, "int main(){} // FIXME"));
|
|
CHECK(contains(outbuf, "The FIXME is gone now."));
|
|
CHECK(!contains(outbuf, "THE-THINKING"));
|
|
CHECK(!contains(outbuf, "SIDECHAIN-NOISE"));
|
|
CHECK(strstr(outbuf, "I will read main.c.") <
|
|
strstr(outbuf, "[tool read]"));
|
|
sp_buf_clear(&out);
|
|
|
|
CHECK(t_read->run("{\"source\":\"hermes\",\"id\":\"h-1\"}", &out,
|
|
t_read->ctx) == 0);
|
|
CHECK(contains(out.p, "HERMES-NEEDLE"));
|
|
CHECK(contains(out.p, "[tool grep]"));
|
|
CHECK(contains(out.p, "HERMES-OUTPUT"));
|
|
CHECK(contains(out.p, "three matches"));
|
|
CHECK(!contains(out.p, "SUPERSEDED"));
|
|
sp_buf_clear(&out);
|
|
|
|
CHECK(t_read->run("{\"source\":\"opencode\",\"id\":\"ses_oc1\"}", &out,
|
|
t_read->ctx) == 0);
|
|
CHECK(contains(out.p, "OC-NEEDLE"));
|
|
CHECK(contains(out.p, "[tool shell]"));
|
|
CHECK(contains(out.p, "OC-OUTPUT"));
|
|
CHECK(contains(out.p, "All twelve pass."));
|
|
CHECK(!contains(out.p, "OC-REASONING"));
|
|
CHECK(strstr(out.p, "[assistant]") < strstr(out.p, "[tool shell]"));
|
|
sp_buf_clear(&out);
|
|
|
|
/* --- errors are sentences, not silence --- */
|
|
CHECK(t_read->run("{\"source\":\"claude\",\"id\":\"nope\"}", &out,
|
|
t_read->ctx) == 0);
|
|
CHECK(contains(out.p, "error:"));
|
|
CHECK(contains(out.p, "no claude session"));
|
|
sp_buf_clear(&out);
|
|
CHECK(t_read->run("{\"id\":\"x\"}", &out, t_read->ctx) == 0);
|
|
CHECK(contains(out.p, "error:"));
|
|
sp_buf_clear(&out);
|
|
CHECK(t_read->run("{\"source\":\"claude\",\"id\":\"../etc/passwd\"}", &out,
|
|
t_read->ctx) == 0);
|
|
CHECK(contains(out.p, "not a session id"));
|
|
sp_buf_clear(&out);
|
|
|
|
/* --- import: a session splinter can read back and resume --- */
|
|
CHECK(t_import->run("{\"source\":\"claude\",\"id\":\"aaaa-1111\"}", &out,
|
|
t_import->ctx) == 0);
|
|
CHECK(contains(out.p, "imported claude session aaaa-1111 as fcl-aaaa-1111"));
|
|
sp_buf_clear(&out);
|
|
CHECK(conversation_ok(sessions, "fcl-aaaa-1111", &err) == 0);
|
|
if (err.len > 0)
|
|
fprintf(stderr, "claude import: %s\n", err.p);
|
|
sp_buf_clear(&err);
|
|
/*
|
|
* Complete, not merely valid: `conversation_ok` only rejects a call
|
|
* without its answer, so an import that drops every tool call passes it.
|
|
* The call, its result and the pairing have to survive the import.
|
|
*/
|
|
CHECK(session_has_tool_pair(sessions, "fcl-aaaa-1111", "toolu_a",
|
|
"read", &err) == 0);
|
|
if (err.len > 0)
|
|
fprintf(stderr, "claude import tools: %s\n", err.p);
|
|
sp_buf_clear(&err);
|
|
|
|
CHECK(t_import->run("{\"source\":\"hermes\",\"id\":\"h-1\"}", &out,
|
|
t_import->ctx) == 0);
|
|
sp_buf_clear(&out);
|
|
CHECK(conversation_ok(sessions, "fhm-h-1", &err) == 0);
|
|
if (err.len > 0)
|
|
fprintf(stderr, "hermes import: %s\n", err.p);
|
|
sp_buf_clear(&err);
|
|
CHECK(session_has_tool_pair(sessions, "fhm-h-1", "call_h", "grep",
|
|
&err) == 0);
|
|
if (err.len > 0)
|
|
fprintf(stderr, "hermes import tools: %s\n", err.p);
|
|
sp_buf_clear(&err);
|
|
|
|
CHECK(t_import->run("{\"source\":\"opencode\",\"id\":\"ses_oc1\"}", &out,
|
|
t_import->ctx) == 0);
|
|
sp_buf_clear(&out);
|
|
CHECK(conversation_ok(sessions, "foc-ses_oc1", &err) == 0);
|
|
if (err.len > 0)
|
|
fprintf(stderr, "opencode import: %s\n", err.p);
|
|
sp_buf_clear(&err);
|
|
CHECK(session_has_tool_pair(sessions, "foc-ses_oc1", "call_oc", "shell",
|
|
&err) == 0);
|
|
if (err.len > 0)
|
|
fprintf(stderr, "opencode import tools: %s\n", err.p);
|
|
sp_buf_clear(&err);
|
|
|
|
/* the import is in the directory the conversation ran in */
|
|
CHECK(sp_session_cwd(sessions, "fcl-aaaa-1111", path, sizeof(path)) == 0);
|
|
CHECK(strcmp(path, "/work/alpha") == 0);
|
|
CHECK(sp_session_cwd(sessions, "fhm-h-1", path, sizeof(path)) == 0);
|
|
CHECK(strcmp(path, "/work/beta") == 0);
|
|
CHECK(sp_session_cwd(sessions, "foc-ses_oc1", path, sizeof(path)) == 0);
|
|
CHECK(strcmp(path, "/work/gamma") == 0);
|
|
|
|
/* and it has the source's title, on one line */
|
|
n = sp_session_list(sessions, v, 200);
|
|
CHECK(n >= 3);
|
|
{
|
|
int i, found = 0;
|
|
|
|
for (i = 0; i < n; i++)
|
|
if (strcmp(v[i].id, "fhm-h-1") == 0) {
|
|
CHECK(strcmp(v[i].title, "hermes H ITLE") == 0);
|
|
found = 1;
|
|
}
|
|
CHECK(found);
|
|
}
|
|
|
|
/* the imported session grows like any other: a turn is appended */
|
|
{
|
|
struct sp_session_info si;
|
|
struct sp_buf serr;
|
|
|
|
sp_buf_init(&serr);
|
|
CHECK(sp_session_open(&si, sessions, "fcl-aaaa-1111", &serr) == 0);
|
|
CHECK(sp_session_append(&si, "user", "and now?", &serr) == 0);
|
|
sp_buf_free(&serr);
|
|
}
|
|
CHECK(conversation_ok(sessions, "fcl-aaaa-1111", &err) == 0);
|
|
sp_buf_clear(&err);
|
|
|
|
/* an import of something unknown fails and writes nothing */
|
|
CHECK(t_import->run("{\"source\":\"opencode\",\"id\":\"ses_none\"}", &out,
|
|
t_import->ctx) == 0);
|
|
CHECK(contains(out.p, "error:"));
|
|
sp_buf_clear(&out);
|
|
snprintf(path, sizeof(path), "%s/foc-ses_none.jsonl", sessions);
|
|
CHECK(access(path, F_OK) != 0);
|
|
|
|
/* the command reports the stores and their counts */
|
|
CHECK(sp_host_command(host, "foreign", "list", &out) == 0);
|
|
CHECK(contains(out.p, "claude"));
|
|
CHECK(contains(out.p, "hermes"));
|
|
CHECK(contains(out.p, "opencode"));
|
|
CHECK(contains(out.p, cla));
|
|
sp_buf_clear(&out);
|
|
|
|
/*
|
|
* The command's argument is the operator's text and goes into a JSON
|
|
* request. Unescaped, a quote in it ended the string early and
|
|
* /foreign search he said "hi" was answered with "'query' is
|
|
* required" - the whole phrase was lost.
|
|
*/
|
|
CHECK(sp_host_command(host, "foreign",
|
|
"search OC-ASSISTANT-PROSE \"quoted\"", &out) == 0);
|
|
CHECK(contains(out.p, "OC-ASSISTANT-PROSE"));
|
|
CHECK(!contains(out.p, "'query' is required"));
|
|
sp_buf_clear(&out);
|
|
|
|
/*
|
|
* A store that cannot be read says why. The message used to be built
|
|
* from errno, which nothing on the path had set: a database with the
|
|
* wrong schema was reported as "No such file or directory" while the
|
|
* file was right there. This drops the tables from the store the plugin
|
|
* resolved and asks again.
|
|
*/
|
|
CHECK(run_sql(ocd, "drop table session_v2;") == 0);
|
|
CHECK(t_list->run("{\"source\":\"opencode\",\"cwd\":\"*\"}", &out,
|
|
t_list->ctx) == 0);
|
|
CHECK(contains(out.p, "no such table"));
|
|
CHECK(!contains(out.p, "No such file"));
|
|
sp_buf_clear(&out);
|
|
|
|
out:
|
|
sp_host_free(host);
|
|
sp_registry_free(®);
|
|
sp_buf_free(&err);
|
|
sp_buf_free(&out);
|
|
{
|
|
char cmd[1100];
|
|
|
|
if (getenv("KEEP_FOREIGN") == NULL) {
|
|
snprintf(cmd, sizeof(cmd), "rm -rf '%s'", base);
|
|
(void)system(cmd);
|
|
} else
|
|
fprintf(stderr, "kept: %s\n", base);
|
|
}
|
|
if (failures == 0)
|
|
printf("test_foreign: ok\n");
|
|
return failures == 0 ? 0 : 1;
|
|
}
|