diff --git a/src/backend/engine.c b/src/backend/engine.c index 6602cfb..d5c2223 100644 --- a/src/backend/engine.c +++ b/src/backend/engine.c @@ -4121,6 +4121,15 @@ sp_engine_switch(struct sp_engine **ep, const struct sp_engine_opts *tmpl, snprintf(keep, sizeof(keep), "%s", sp_engine_session_id(old)); dir = sp_engine_state_dir(old); o = *tmpl; + /* + * The template is how this run was started, and a run started with + * --claude carries from_foreign. Every branch below decides what to + * open from scratch, so neither field may survive from the template: + * left behind, an ordinary "/open " imported the newest claude + * session instead of opening the one that was picked (and segfaulted + * when that import failed with no plugin loaded - found 2026-11-22). + */ + o.from_foreign = NULL; o.from_foreign_id = NULL; if (foreign_spec(id, src, fid, sizeof(fid))) { /* @@ -4184,6 +4193,13 @@ sp_engine_switch(struct sp_engine **ep, const struct sp_engine_opts *tmpl, back.session_id = keep; back.resume = 1; back.continue_last = 0; + /* + * Same reason as `o` above: "go back to the session we had" must not + * turn into "import the newest claude session again", which would + * replace the session the caller is being put back on. + */ + back.from_foreign = NULL; + back.from_foreign_id = NULL; if (sp_engine_open(&ne, &back, &oerr) == 0) { *ep = ne; } else { @@ -4192,6 +4208,8 @@ sp_engine_switch(struct sp_engine **ep, const struct sp_engine_opts *tmpl, fresh.session_id = NULL; fresh.resume = 0; fresh.continue_last = 0; + fresh.from_foreign = NULL; + fresh.from_foreign_id = NULL; if (sp_engine_open(&ne, &fresh, &oerr) == 0) *ep = ne; } diff --git a/tests/test_engine.c b/tests/test_engine.c index 3825c5f..ac860fc 100644 --- a/tests/test_engine.c +++ b/tests/test_engine.c @@ -141,6 +141,7 @@ main(void) 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"); @@ -704,12 +705,18 @@ main(void) * starts in plan again, and the profile prompt appears once, not twice. */ { - char sess[64], sdir[512]; + 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; @@ -738,11 +745,47 @@ main(void) CHECK(marks == 1); CHECK(sp_engine_take_steer(eng2, &out) == 0); sp_buf_clear(&out); - sp_engine_close(eng2); + /* + * 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));