engine: /open <id> no longer re-imports the newest foreign session

sp_engine_switch() copies the options the run was started with, and a run
started with --claude carries from_foreign. Two branches (a foreign
"claude:<id>" pick and the recovery path back to the session we had) reset
it, the branch for an ordinary session id did not - so a plain /open <id>
in a --claude run ignored the pick and imported the newest claude session
over it. With no plugin loaded that import failed and sp_engine_switch
returned -1 with *ep already NULL, which the caller then dereferenced.

from_foreign is now cleared once, before the branches, and again on the
recovery path (which would otherwise re-import instead of going back).

Evidence: tests/test_engine.c sets from_foreign="claude" on the template and
switches to an ordinary id. Without the fix it FAILs and segfaults (rc=139);
with it the test is green. make check: 21 suites, 0 failures.
tests/check_asan.sh: 45 binaries clean.
This commit is contained in:
lotherk committed 2026-11-22 23:19:12 +00:00
1 parent f5d63f6199
commit 7634c57872
2 files changed
+63 -2

No files matched your search

+18
View File
@@ -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 <id>" 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;
}
+45 -2
View File
@@ -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));