diff --git a/README.md b/README.md index 166d5d0..ba1548c 100644 --- a/README.md +++ b/README.md @@ -859,7 +859,11 @@ Three limits worth knowing. The stores are read with `sqlite3 -readonly`, so splinter never writes into them — an import copies, it does not move. Reasoning blocks are dropped on the way in: a provider does not accept another model's thinking as evidence. And the tool calls arrive with their results in place, -which is what makes the imported session worth resuming instead of retyping. +which is what makes the imported session worth resuming instead of retyping: +every `tool_calls` the source recorded is kept with the call id, and every +result is kept with the same id, so the pairing a provider insists on survives +the import (an imported claude session of 3436 messages reaches the model with +its 1630 calls and 1630 results). ## MCP diff --git a/plugins/foreign/foreign.c b/plugins/foreign/foreign.c index 7f2cad4..9c8893a 100644 --- a/plugins/foreign/foreign.c +++ b/plugins/foreign/foreign.c @@ -679,8 +679,7 @@ calls_keep(struct frec *r, char **answered, size_t nans, char ***kept, sp_buf_free(&one_buf); } mcp_json_free(arr); - if (any && b.len > 0 && - sp_buf_printf(&(struct sp_buf){ 0 }, "") == 0) { + if (any && b.len > 0) { struct sp_buf full; if (sp_buf_init(&full) == 0) { @@ -918,13 +917,20 @@ args_text(const mcp_json *input, struct sp_buf *out) } } +/* + * One tool call as it goes into a `calls` array: the OpenAI shape the session + * file and the provider both read. The brackets belong here, not to the + * callers: `calls_keep` parses what this produces as an array, and a run of + * elements without them parsed as nothing, so every imported tool call was + * dropped and the import kept only the prose around it. + */ static void calls_append(struct sp_buf *b, const char *id, const char *name, const char *args) { int first = b->len == 0; - if (sp_buf_printf(b, "%s{\"id\":\"", first ? "" : ",") != 0 || + if (sp_buf_printf(b, "%s{\"id\":\"", first ? "[" : ",") != 0 || sp_json_escape(b, id != NULL ? id : "call") != 0 || sp_buf_puts(b, "\",\"type\":\"function\",\"function\":{\"name\":\"") != 0 || @@ -935,6 +941,28 @@ calls_append(struct sp_buf *b, const char *id, const char *name, (void)sp_buf_puts(b, "\"}}"); } +/* + * Hand the finished array to the record writer and leave the buffer empty for + * the next message. The closing bracket is written here, once, so a call site + * cannot forget it: `calls_append` writes elements, this makes them an array. + */ +static const char * +calls_take(struct sp_buf *b) +{ + if (b->len == 0) + return NULL; + if (sp_buf_puts(b, "]") != 0) + return NULL; + return b->p; +} + +/* after the record is written, the buffer starts over */ +static void +calls_done(struct sp_buf *b) +{ + sp_buf_clear(b); +} + /* ------------------------------------------------------------------- ids */ static int @@ -1435,10 +1463,12 @@ load_claude(const char *id, struct fconv *conv, char *err, size_t errcap) /* one message can arrive as several lines */ if (!p.used || (mid != NULL && p.id[0] != '\0' && strcmp(mid, p.id) != 0)) { - if (p.used) + if (p.used) { (void)fconv_add(conv, "assistant", p.text.p, NULL, NULL, - p.calls.len > 0 ? p.calls.p : NULL); + calls_take(&p.calls)); + calls_done(&p.calls); + } p.used = 1; p.id[0] = '\0'; sp_buf_clear(&p.text); @@ -1491,7 +1521,8 @@ load_claude(const char *id, struct fconv *conv, char *err, size_t errcap) /* user: tool results answer the open calls, text is the turn */ if (p.used) { (void)fconv_add(conv, "assistant", p.text.p, NULL, NULL, - p.calls.len > 0 ? p.calls.p : NULL); + calls_take(&p.calls)); + calls_done(&p.calls); p.used = 0; p.id[0] = '\0'; sp_buf_clear(&p.text); @@ -1538,9 +1569,11 @@ load_claude(const char *id, struct fconv *conv, char *err, size_t errcap) } mcp_json_free(o); } - if (p.used) + if (p.used && (p.text.len > 0 || p.calls.len > 0)) { (void)fconv_add(conv, "assistant", p.text.p, NULL, NULL, - p.calls.len > 0 ? p.calls.p : NULL); + calls_take(&p.calls)); + calls_done(&p.calls); + } sp_buf_free(&p.text); sp_buf_free(&p.calls); free(line); @@ -1870,8 +1903,7 @@ load_hermes(const char *id, struct fconv *conv, char *err, size_t errcap) hermes_calls(mcp_arg_string(r, "tool_calls"), &calls); (void)fconv_add(conv, role, text.p, istool ? name : NULL, - istool ? cid : NULL, - calls.len > 0 ? calls.p : NULL); + istool ? cid : NULL, calls_take(&calls)); sp_buf_free(&text); sp_buf_free(&calls); } @@ -2073,7 +2105,7 @@ oc_assistant(struct fconv *conv, const mcp_json *data) */ if (text.len > 0 || calls.len > 0) (void)fconv_add(conv, "assistant", text.p, NULL, NULL, - calls.len > 0 ? calls.p : NULL); + calls_take(&calls)); for (i = 0; i < mcp_array_len(content); i++) { const mcp_json *blk = mcp_array_at(content, i); const char *bt = mcp_arg_string(blk, "type"); @@ -2248,7 +2280,7 @@ oc_load_legacy(const char *id, struct fconv *conv, char *why, size_t whycap) mcp_arg(state, "input") : NULL, &args); calls_append(&calls, cid, name, args.p); (void)fconv_add(conv, "assistant", "", NULL, - NULL, calls.p); + NULL, calls_take(&calls)); oc_tool_output(pd, &out); (void)fconv_add(conv, "tool", out.p, name, cid, NULL); diff --git a/tests/test_foreign.c b/tests/test_foreign.c index 42fe0dc..709deee 100644 --- a/tests/test_foreign.c +++ b/tests/test_foreign.c @@ -107,6 +107,58 @@ run_sql(const char *db, const char *sql) 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) @@ -477,6 +529,16 @@ main(void) 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); @@ -485,6 +547,11 @@ main(void) 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); @@ -493,6 +560,11 @@ main(void) 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);