/* * splinter -- the engine a front end talks to: one scripted turn, no screen. * * Copyright (c) 2026 Konrad Lother * SPDX-License-Identifier: BSD-3-Clause * * What it guards: the event queue carries a whole turn in order (busy, the * streamed text, the step, the call, its observation, the answer, idle); the * commands answer through sp_engine_input without touching an agent; a shell * line becomes an entry; the shared call/observation shaping reads the way the * transcript does; and closing the engine frees the turn it ran. */ #include #include #include #include #include #include #include "splinter.h" #include "splinter_engine.h" #include "config.h" #include "i18n.h" #include "mcp.h" #include "prompt.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; } /* collects the picker rows the engine offers for one source */ struct rowset { char id[8][128]; int cur[8]; int n; }; static void row_cb(void *cbctx, const char *id, const char *title, int current, long mtime) { struct rowset *r = cbctx; (void)title; (void)mtime; if (r->n >= 8) return; snprintf(r->id[r->n], sizeof(r->id[r->n]), "%s", id); r->cur[r->n] = current; r->n++; } /* the whole turn, by draining the queue until the engine says idle */ static void run_turn(struct sp_engine *eng) { struct sp_event ev; int saw_busy_on = 0, saw_stream = 0, saw_step = 0; int saw_tool = 0, saw_result = 0, saw_turn = 0; int saw_busy_off = 0; int rc = -1; char answer[256] = ""; char tool[64] = ""; int guard = 0; CHECK(sp_engine_submit(eng, "sag hallo") == 0); while (guard++ < 500) { while (sp_engine_poll(eng, &ev)) { switch (ev.kind) { case SP_EV_BUSY: if (ev.n != 0) saw_busy_on = 1; else saw_busy_off = 1; break; case SP_EV_STREAM: if (ev.n == SP_STREAM_TEXT) saw_stream = 1; break; case SP_EV_STEP: saw_step = 1; break; case SP_EV_TOOL: saw_tool = 1; snprintf(tool, sizeof(tool), "%s", ev.a != NULL ? ev.a : ""); break; case SP_EV_TOOL_RESULT: saw_result = 1; break; case SP_EV_TURN_END: saw_turn = 1; rc = (int)ev.n; snprintf(answer, sizeof(answer), "%s", ev.a != NULL ? ev.a : ""); break; default: break; } sp_engine_event_free(&ev); } if (!sp_engine_busy(eng) && saw_busy_off) break; { struct timespec ts = { 0, 10000000L }; nanosleep(&ts, NULL); } } (void)sp_engine_wait(eng); CHECK(saw_busy_on); /* the mock transport cannot stream; it is not the engine's job to fake it */ (void)saw_stream; CHECK(saw_step); CHECK(saw_tool); CHECK(strcmp(tool, "echo") == 0); CHECK(saw_result); CHECK(saw_turn); CHECK(rc == 0); CHECK(strcmp(answer, "fertig") == 0); CHECK(saw_busy_off); } int main(void) { struct sp_config conf; struct sp_agent_config base; struct sp_engine_opts opts; struct sp_engine *eng = NULL; struct sp_buf err, out; char tmpl[512], script[600], path[700], logpath[760]; char sdir[512]; char *dir; const char *tmp = getenv("TMPDIR"); /* * A turn whose mock runs out of replies is retried three times, five * seconds apart. That is the right policy against a real provider and * the wrong one inside a test suite: it turns a missing line in a * script into a hang. Same lever as test_agent.c. */ (void)setenv("SPLINTER_RETRY_WAIT_MS", "0", 1); FILE *f; char call[256]; int kind; if (tmp == NULL || *tmp == '\0') tmp = "/tmp"; snprintf(tmpl, sizeof(tmpl), "%s/splinter-engine-XXXXXX", tmp); dir = mkdtemp(tmpl); if (dir == NULL) { perror("mkdtemp"); return 1; } (void)snprintf(script, sizeof(script), "%s/script.txt", dir); snprintf(path, sizeof(path), "%s/.splinter", dir); (void)mkdir(path, 0700); f = fopen(script, "w"); if (f == NULL) { perror("script"); return 1; } /* one call, then the answer; the mock replays one line per request */ fprintf(f, "tool echo {\"text\":\"hallo-welt\"}\n"); fprintf(f, "answer fertig\n"); /* a second turn whose tool fails: /self-check must name the failure */ fprintf(f, "tool read {\"path\":\"/nonexistent-splinter-test\"}\n"); fprintf(f, "answer done\n"); /* the queued-/compact turn and the one after it */ { int k; for (k = 0; k < 20; k++) fprintf(f, "answer fertig\n"); } fclose(f); (void)setenv("HOME", dir, 1); (void)sp_i18n_init("en"); if (sp_buf_init(&err) != 0 || sp_buf_init(&out) != 0) return 1; sp_config_init(&conf); /* * One provider and one pool, so the picker's list is not empty: the * front end asks the engine, never the config file it may not have. */ snprintf(conf.endpoint, sizeof(conf.endpoint), "http://127.0.0.1:1/v1"); snprintf(conf.model, sizeof(conf.model), "test-model"); snprintf(conf.default_id, sizeof(conf.default_id), "main"); conf.npools = 1; snprintf(conf.pool[0].id, sizeof(conf.pool[0].id), "testpool"); snprintf(conf.pool[0].strategy, sizeof(conf.pool[0].strategy), "balance"); snprintf(conf.pool[0].member[0], sizeof(conf.pool[0].member[0]), "main"); conf.pool[0].nmembers = 1; memset(&base, 0, sizeof(base)); base.max_steps = 10; base.system_prompt = "test"; memset(&opts, 0, sizeof(opts)); opts.conf = &conf; opts.base = &base; opts.script = script; opts.session_dir = path; opts.session_id = "sess-engine"; opts.no_transport = 0; /* * One earlier session in this directory, so setup_conversation has a * history to point at. */ { struct sp_session_info prev; memset(&prev, 0, sizeof(prev)); sp_buf_clear(&err); CHECK(sp_session_open(&prev, path, "sess-prev", &err) == 0); CHECK(sp_session_append(&prev, "user", "an earlier question in this folder", &err) == 0); } if (sp_engine_open(&eng, &opts, &err) != 0) { fprintf(stderr, "engine: %s\n", err.p != NULL ? err.p : "?"); return 1; } /* * The host exists even with no plugin configured. It is what carries the * registry to the core compactor; without it a plugin-less run had no * compactor at all and every conversation over max_bytes was refused * instead of compacted (seen 2026-11-21 on a 90 KB session, budget * 8000, "nothing sent"). */ CHECK(sp_engine_host(eng) != NULL); /* * The prompt knows this directory has history and says how to read it; * the default prompt tells the model to use the knowledge tools (tasks, * memory, skills, knowledge bases) actively. */ { const struct sp_conv *c = sp_engine_conv(eng); char def[4096]; CHECK(c != NULL && c->n > 0); CHECK(c != NULL && c->n > 0 && contains(c->v[0].content, "earlier sessions")); CHECK(c != NULL && c->n > 0 && contains(c->v[0].content, "session_search")); /* The working directory names the default task scope, so a project * question does not follow the busiest scope in the store. */ CHECK(c != NULL && c->n > 0 && contains(c->v[0].content, "use the task scope")); CHECK(c != NULL && c->n > 0 && contains(c->v[0].content, "Do not present another project's scopes")); sp_prompt_default(def, sizeof(def), "test-model"); CHECK(contains(def, "knowledge tools actively")); CHECK(contains(def, "knowledge bases")); CHECK(contains(def, "task_*")); } run_turn(eng); /* * Every tool schema must be a valid JSON schema object. A judge model * (Claude) validates them and rejects the whole request when one has * "parameters": {} without "type": "object" - delegate_due and * delegate_jobs did, and three sessions ended in an empty stream. */ { size_t n = 0, i; const struct sp_tool *tools = sp_registry_tools( sp_engine_registry(eng), &n); CHECK(n > 0); for (i = 0; i < n; i++) { mcp_json *v; const char *type; if (tools[i].params == NULL) continue; v = mcp_parse(tools[i].params); CHECK(v != NULL); if (v == NULL) { fprintf(stderr, "tool %s: unparseable schema\n", tools[i].name); continue; } type = mcp_arg_string(v, "type"); if (type == NULL || strcmp(type, "object") != 0) fprintf(stderr, "tool %s: parameters.type is %s\n", tools[i].name, type != NULL ? type : "(missing)"); CHECK(type != NULL && strcmp(type, "object") == 0); mcp_json_free(v); } } /* * mcp_parse_exact: a tool call is one object, not the first of several. * mcp_parse() stops at the closing brace, so "{a}{b}" parsed as {a} and * the second command vanished; the exact form is what the agent uses. */ { mcp_json *v; v = mcp_parse("{\"a\":1}{\"b\":2}"); CHECK(v != NULL); mcp_json_free(v); CHECK(mcp_parse_exact("{\"a\":1}{\"b\":2}") == NULL); CHECK(mcp_parse_exact("{\"a\":1") == NULL); v = mcp_parse_exact(" {\"command\":\"ls\"} \n"); CHECK(v != NULL); CHECK(contains(mcp_arg_string(v, "command"), "ls")); mcp_json_free(v); } /* the /model picker's list: providers first, then pools */ { struct sp_model_choice mv[SP_MODEL_CHOICES]; size_t mn = sp_engine_models(eng, mv, SP_MODEL_CHOICES); CHECK(mn == 2); CHECK(strcmp(mv[0].sel, "local/test-model") == 0); CHECK(contains(mv[0].label, "test-model")); CHECK(strcmp(mv[1].sel, "testpool") == 0); CHECK(contains(mv[1].label, "pool")); } kind = sp_engine_input(eng, "/cost", &out); CHECK(kind == SP_INPUT_HANDLED); CHECK(contains(out.p, "cost:")); CHECK(contains(out.p, "judged:")); sp_buf_clear(&out); kind = sp_engine_input(eng, "/status", &out); CHECK(kind == SP_INPUT_HANDLED); CHECK(contains(out.p, "session sess-engine")); CHECK(contains(out.p, "agent build")); sp_buf_clear(&out); /* /effort sets the review depth the transport puts into the header */ kind = sp_engine_input(eng, "/effort max", &out); CHECK(kind == SP_INPUT_HANDLED); CHECK(sp_openai_review_depth() == 12); CHECK(contains(out.p, "12 review rounds")); sp_buf_clear(&out); kind = sp_engine_input(eng, "/effort 2", &out); CHECK(kind == SP_INPUT_HANDLED); CHECK(sp_openai_review_depth() == 2); sp_buf_clear(&out); kind = sp_engine_input(eng, "/effort off", &out); CHECK(kind == SP_INPUT_HANDLED); CHECK(sp_openai_review_depth() == 0); CHECK(contains(out.p, "off")); sp_buf_clear(&out); kind = sp_engine_input(eng, "/effort nope", &out); CHECK(kind == SP_INPUT_HANDLED); CHECK(sp_openai_review_depth() == 0); CHECK(contains(out.p, "off|low|medium|high|max")); sp_buf_clear(&out); kind = sp_engine_input(eng, "/no-such-command", &out); CHECK(kind == SP_INPUT_HANDLED); CHECK(contains(out.p, "/no-such-command")); sp_buf_clear(&out); /* the goal is in the session sidecar; /continue turns it into a prompt */ CHECK(sp_session_set_goal(path, "sess-engine", "Ship the config file", &err) == 0); kind = sp_engine_input(eng, "/continue", &out); CHECK(kind == SP_INPUT_PROMPT); CHECK(contains(out.p, "Ship the config file")); sp_buf_clear(&out); /* * /self-check hands the model its own tail: the turns it ran and the * observations that failed, with the framing that tells it to debug * itself. Both spellings are commands; the hyphen is the real one. */ { struct sp_event ev; int guard = 0; /* the second scripted turn reads a path that does not exist */ CHECK(sp_engine_submit(eng, "read it") == 0); while (guard++ < 500) { while (sp_engine_poll(eng, &ev)) sp_engine_event_free(&ev); if (!sp_engine_busy(eng)) break; { struct timespec ts = { 0, 10000000L }; nanosleep(&ts, NULL); } } (void)sp_engine_wait(eng); } kind = sp_engine_input(eng, "/self-check", &out); CHECK(kind == SP_INPUT_PROMPT); CHECK(contains(out.p, "check yourself")); CHECK(contains(out.p, "sag hallo")); CHECK(contains(out.p, "the last turns")); CHECK(contains(out.p, "failures in those turns")); CHECK(contains(out.p, "nonexistent-splinter-test")); sp_buf_clear(&out); kind = sp_engine_input(eng, "/selfcheck", &out); CHECK(kind == SP_INPUT_PROMPT); CHECK(contains(out.p, "check yourself")); sp_buf_clear(&out); /* * /learn turns the folder's sessions into the model's reading list and * names the session tools this engine really has. Without an argument * it covers the folder; with one it covers that topic. */ kind = sp_engine_input(eng, "/learn", &out); CHECK(kind == SP_INPUT_PROMPT); CHECK(contains(out.p, "Learn from the sessions of this folder")); CHECK(contains(out.p, "becomes a skill")); CHECK(contains(out.p, "becomes a task")); CHECK(contains(out.p, "sess-engine")); /* * Where the transcript is, not only that it exists: a live /learn read * 54 search snippets and not one session, because the prompt said what * to look for and never how to read a whole one. */ CHECK(contains(out.p, ".jsonl")); CHECK(contains(out.p, path)); sp_buf_clear(&out); kind = sp_engine_input(eng, "/learn the gateway retry", &out); CHECK(kind == SP_INPUT_PROMPT); CHECK(contains(out.p, "Learn about: the gateway retry")); CHECK(contains(out.p, "becomes a memory")); /* * This engine has skill_search/skill_load but nothing that writes, so * the prompt names the shape those tools have rather than telling the * model there are none (a live /learn was told "no tool that writes" * while the MCP server was still connecting, and had 566 skills one call * later). */ CHECK(contains(out.p, "skill_create")); CHECK(contains(out.p, "memory_add")); CHECK(!contains(out.p, "no tool that writes")); sp_buf_clear(&out); /* * And the other way round: a write tool this session really has is * named, so the model stores the conclusion instead of improvising. * The registry is live, so a tool added here is visible at once. */ { static struct sp_tool wt; struct sp_registry *reg = sp_engine_registry(eng); memset(&wt, 0, sizeof(wt)); wt.name = "memory_add"; wt.description = "test stand-in for the memory server"; wt.auth = SP_AUTH_MUTATE; CHECK(sp_registry_add(reg, &wt) == 0); kind = sp_engine_input(eng, "/learn", &out); CHECK(kind == SP_INPUT_PROMPT); CHECK(contains(out.p, "Store what you keep with memory_add")); sp_buf_clear(&out); } { /* the command list the TUI completes and the bot registers */ char names[64][64]; int n = sp_engine_command_names(eng, names, 64), i, seen = 0; for (i = 0; i < n; i++) if (strcmp(names[i], "learn") == 0) seen = 1; CHECK(seen); } /* * The picker's two halves. Sources first: this folder is always one, and * a foreign store that has nothing is not offered. Then the rows of one * source: an own session keeps its bare id (the picker turns it into * /open ), and the current one is marked. */ { struct sp_session_source src[4]; static struct rowset rows; int n, i, saw_prev = 0, saw_cur = 0; memset(&rows, 0, sizeof(rows)); n = sp_engine_sources(eng, src, 4); CHECK(n >= 1); CHECK(n >= 1 && strcmp(src[0].name, "splinter") == 0); CHECK(n >= 1 && src[0].count >= 2); for (i = 1; i < n; i++) CHECK(strcmp(src[i].name, "splinter") != 0); /* * A source this build cannot reach is empty, not a crash: the * engine logs why and the picker shows an empty list. */ CHECK(sp_engine_source_sessions(eng, "claude", row_cb, &rows) == 0); rows.n = 0; CHECK(sp_engine_source_sessions(eng, "splinter", row_cb, &rows) == 2); for (i = 0; i < rows.n; i++) { CHECK(strchr(rows.id[i], ':') == NULL); if (strcmp(rows.id[i], "sess-prev") == 0) saw_prev = 1; if (strcmp(rows.id[i], sp_engine_session_id(eng)) == 0 && rows.cur[i]) saw_cur = 1; } CHECK(saw_prev); CHECK(saw_cur); } /* * /new drops the conversation, not the way of working: the active * profile's prompt goes back on top, and the new session gets an id of * its own even when two /new happen in the same second. */ { int i, n = sp_engine_agents(eng), plan = -1; char id1[64] = "", id2[64] = ""; for (i = 0; i < n; i++) if (strcmp(sp_engine_agent_name(eng, i), "plan") == 0) plan = i; CHECK(plan >= 0); CHECK(sp_engine_agent_switch(eng, plan) == 0); CHECK(sp_engine_input(eng, "/new", &out) == SP_INPUT_HANDLED); CHECK(contains(out.p, "sess-")); snprintf(id1, sizeof(id1), "%s", out.p != NULL ? out.p : ""); /* "[agent: plan]" is the profile tag, in every language */ CHECK(sp_engine_conv(eng)->n > 0 && contains(sp_engine_conv(eng)->v[0].content, "[agent: plan]")); sp_buf_clear(&out); CHECK(sp_engine_input(eng, "/new", &out) == SP_INPUT_HANDLED); snprintf(id2, sizeof(id2), "%s", out.p != NULL ? out.p : ""); CHECK(strcmp(id1, id2) != 0); CHECK(sp_engine_conv(eng)->n > 0 && contains(sp_engine_conv(eng)->v[0].content, "[agent: plan]")); sp_buf_clear(&out); } /* * A turn outside the engine (the daemon's Telegram loop) holds the * agent: the engine reports busy, refuses a submit, a profile switch * and a second holder, and lets go when the turn ends. */ { struct sp_agent *ag = sp_engine_agent(eng); struct sp_event ev; CHECK(sp_engine_agent_turn_begin(ag) == 0); CHECK(sp_engine_busy(eng) == 1); CHECK(sp_engine_submit(eng, "hi") == -1); CHECK(sp_engine_agent_switch(eng, 0) == -1); CHECK(sp_engine_turn_begin(eng) == -1); sp_engine_agent_turn_end(ag); CHECK(sp_engine_busy(eng) == 0); CHECK(sp_engine_hold(eng) == 0); CHECK(sp_engine_busy(eng) == 0); CHECK(sp_engine_turn_begin(eng) == -1); sp_engine_release(eng); /* an agent no engine owns is nobody's business */ CHECK(sp_engine_agent_turn_begin(NULL) == 0); while (sp_engine_poll(eng, &ev)) sp_engine_event_free(&ev); } /* * /model belongs to the session: the selector is written to the * session's sidecar and comes back when the session is reopened. */ kind = sp_engine_input(eng, "/model test-model-2", &out); CHECK(kind == SP_INPUT_HANDLED); CHECK(contains(out.p, "test-model-2")); { char mp[800], line[320]; FILE *mf; snprintf(mp, sizeof(mp), "%s/%s.model", path, sp_engine_session_id(eng)); mf = fopen(mp, "r"); CHECK(mf != NULL); if (mf != NULL) { CHECK(fgets(line, sizeof(line), mf) != NULL); CHECK(contains(line, "test-model-2")); fclose(mf); } } sp_buf_clear(&out); /* the endpoint answers nothing here; the command still answers */ kind = sp_engine_input(eng, "/model probe", &out); CHECK(kind == SP_INPUT_HANDLED); CHECK(out.p != NULL && out.p[0] != '\0'); sp_buf_clear(&out); /* * A /compact typed while a turn runs. The conversation belongs to the * worker, so the command used to be refused with "not while a turn * runs" - and an operator who reads that while the model is writing * never repeats it: three /compact in one session, none of them ran * (2026-11-22, 253 KB requests). It is queued instead, with the target * it was asked for, and applied when the turn ends. */ { struct sp_buf reply; struct sp_event ev; int applied = 0, guard = 0; CHECK(sp_buf_init(&reply) == 0); CHECK(sp_engine_submit(eng, "sag hallo") == 0); /* the turn owns the conversation now; the command must not race it */ CHECK(sp_engine_busy(eng) == 1); kind = sp_engine_input(eng, "/compact 500", &reply); CHECK(kind == SP_INPUT_HANDLED); CHECK(contains(reply.p, "queued")); CHECK(contains(reply.p, "500")); while (guard++ < 500) { while (sp_engine_poll(eng, &ev)) { if (ev.kind == SP_EV_ENTRY && ev.b != NULL && contains(ev.b, "compact:")) applied = 1; sp_engine_event_free(&ev); } if (!sp_engine_busy(eng)) break; { struct timespec ts = { 0, 10000000L }; nanosleep(&ts, NULL); } } (void)sp_engine_wait(eng); /* drain what the turn end queued after the last poll */ while (sp_engine_poll(eng, &ev)) { if (ev.kind == SP_EV_ENTRY && ev.b != NULL && contains(ev.b, "compact:")) applied = 1; sp_engine_event_free(&ev); } CHECK(applied); /* the queue is a one-shot: it must not fire again on the next turn */ applied = 0; CHECK(sp_engine_submit(eng, "sag hallo") == 0); while (guard++ < 1000) { while (sp_engine_poll(eng, &ev)) { if (ev.kind == SP_EV_ENTRY && ev.b != NULL && contains(ev.b, "compact:")) applied = 1; sp_engine_event_free(&ev); } if (!sp_engine_busy(eng)) break; { struct timespec ts = { 0, 10000000L }; nanosleep(&ts, NULL); } } (void)sp_engine_wait(eng); CHECK(applied == 0); sp_buf_clear(&reply); sp_buf_free(&reply); } /* a shell line is an entry event, not a reply */ kind = sp_engine_input(eng, "!echo shell-out", &out); CHECK(kind == SP_INPUT_HANDLED); { int got = 0; struct sp_event ev; while (sp_engine_poll(eng, &ev)) { if (ev.kind == SP_EV_ENTRY && ev.a != NULL && strcmp(ev.a, "shell") == 0 && contains(ev.b, "shell-out")) got = 1; sp_engine_event_free(&ev); } CHECK(got); } sp_buf_clear(&out); /* the session log is on disk, one JSON object per line */ snprintf(logpath, sizeof(logpath), "%s/sess-engine.jsonl", opts.session_dir); CHECK(access(logpath, R_OK) == 0); /* * The session remembers the profile: a new engine on the same session * starts in plan again, and the profile prompt appears once, not twice. */ { char sess[64]; struct sp_engine *eng2 = NULL; int plan2 = -1, i, n2, marks = 0; const char *sys; snprintf(sess, sizeof(sess), "%s", sp_engine_session_id(eng)); /* * opts outlives this block and keeps the pointer, so the * directory lives at function scope: a block-local sdir here * was read after the block through opts.session_dir * (stack-use-after-scope, ASan 2026-11-22). */ snprintf(sdir, sizeof(sdir), "%s", sp_engine_state_dir(eng)); sp_engine_close(eng); eng = NULL; opts.session_id = sess; opts.session_dir = sdir; opts.resume = 1; opts.agent = NULL; CHECK(sp_engine_open(&eng2, &opts, &err) == 0); { struct sp_engine_state st; CHECK(sp_engine_state(eng2, &st) == 0); CHECK(strcmp(st.model, "test-model-2") == 0); } n2 = sp_engine_agents(eng2); for (i = 0; i < n2; i++) if (strcmp(sp_engine_agent_name(eng2, i), "plan") == 0) plan2 = i; CHECK(plan2 >= 0); CHECK(sp_engine_agent_cur(eng2) == plan2); sys = sp_engine_conv(eng2)->n > 0 ? sp_engine_conv(eng2)->v[0].content : ""; for (i = 0; sys != NULL && (sys = strstr(sys, "[agent: plan]")) != NULL; i++, sys++) marks = i + 1; CHECK(marks == 1); CHECK(sp_engine_take_steer(eng2, &out) == 0); sp_buf_clear(&out); /* * eng2 stays open: the switch test below needs a live engine, * and `eng = eng2` after sp_engine_close() left a dangling * pointer behind (found by ASan 2026-11-22 when that test * started reading it). */ eng = eng2; } /* * sp_engine_switch copies the options it was opened with, and those can * carry from_foreign (a run started with --claude). A switch to a plain * session id must NOT inherit it: the import would replace the chosen * session with the newest claude one, and the operator's pick would be * ignored. Here the template says "claude", the pick is an ordinary id, * and the refusal ("no claude session") must not appear at all. */ { struct sp_engine_opts pick = opts; struct sp_engine *sw = eng; char want[64]; snprintf(want, sizeof(want), "%s", sp_engine_session_id(eng)); pick.from_foreign = "claude"; pick.from_foreign_id = NULL; pick.session_id = NULL; pick.resume = 1; sp_buf_clear(&err); /* * A failed switch drops *ep on purpose, so `sw` is read only * after the CHECK that says it survived. Reading it while it * may be NULL turned a red test into a segfault, which tells * the next reader nothing. */ CHECK(sp_engine_switch(&sw, &pick, want, &err) == 0); if (sw != NULL) { eng = sw; /* the picked session, not "the newest claude one" */ CHECK(strcmp(sp_engine_session_id(eng), want) == 0); } } /* the shaping both front ends print */ sp_call_summary("run", "{\"command\":\"ls -l\"}", call, sizeof(call)); CHECK(strcmp(call, "run ls -l") == 0); sp_call_summary("read", "{\"path\":\"src/x.c\",\"limit\":10}", call, sizeof(call)); CHECK(strcmp(call, "read src/x.c") == 0); sp_buf_clear(&out); CHECK(sp_obs_short("[exit status 0]", &out) == SP_OBS_JOIN); CHECK(strcmp(out.p, "[exit status 0]") == 0); sp_buf_clear(&out); CHECK(sp_obs_short("", &out) == SP_OBS_NONE); sp_buf_free(&out); sp_buf_free(&err); (void)unlink(script); (void)rmdir(path); (void)rmdir(dir); if (failures == 0) { printf("test_engine: ok\n"); return 0; } return 1; }