From 47d45bfd159c0f8071679c24bcd71f884335ca24 Mon Sep 17 00:00:00 2001 From: lotherk Date: Thu, 19 Nov 2026 07:57:03 +0000 Subject: [PATCH] session: search only sessions that ran in this working directory "What did we do last?" in a folder searched every session ever and wandered off in another direction: the plugin walked the whole session directory while only the prompt note was folder-scoped. Both tools now read the cwd from the session's meta sidecar (sp_session_cwd, the same record the folder note uses) and skip sessions from other folders -- and sessions that have no cwd record. test_sessiontools creates a session in a second folder with the same needle: with the filter disabled it shows up (red), with it the search and the list stay in the folder, and even session= answers "no message". make check: all tests passed. make check-asan: all 41 test binaries clean. --- README.md | 5 +++-- docs/PLUGINS.md | 10 ++++++--- include/splinter.h | 2 ++ plugins/session/session.c | 43 ++++++++++++++++++++++++++++++--------- src/backend/session.c | 14 +++++++++++++ tests/test_sessiontools.c | 34 ++++++++++++++++++++++++++++++- 6 files changed, 92 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index c70be3c..9f26c8d 100644 --- a/README.md +++ b/README.md @@ -578,8 +578,9 @@ dropped stays in the session log, and the `recall` tool searches it: the model can look an earlier turn up instead of guessing. The `session` plugin reaches further back: `session_search` finds text in the -logs of earlier sessions in this folder (with date, session id, title and a -snippet) and `session_list` shows them. When the working directory has history, +logs of earlier sessions that ran in this working directory (with date, session +id, title and a snippet) and `session_list` shows them. Sessions from other +folders are left out. When the working directory has history, the system prompt says so and tells the model to look there before asking the user something that was already answered. diff --git a/docs/PLUGINS.md b/docs/PLUGINS.md index 30c5820..7f14f80 100644 --- a/docs/PLUGINS.md +++ b/docs/PLUGINS.md @@ -523,9 +523,13 @@ server cannot keep. Beyond loopback ## session -`session_search` and `session_list` read the logs of earlier sessions in this -folder. Both are read-only; the running session is left out (it is already in -the prompt), unless a call names it with `session=`. +`session_search` and `session_list` read the logs of earlier sessions that +**ran in this working directory**. The cwd recorded in each session's meta +sidecar decides that: a session from another folder is left out (a search that +wanders answers a question nobody asked), and a session without that record is +skipped. Both are read-only; the running session is left out too (it is +already in the prompt), unless a call names it with `session=` -- and even +then it must belong to this folder. `session_search { query, limit?, days?, session? }` walks the sessions newest first, one `getline` per JSONL record, and answers one line per hit: diff --git a/include/splinter.h b/include/splinter.h index d6fb98b..08c3451 100644 --- a/include/splinter.h +++ b/include/splinter.h @@ -276,6 +276,8 @@ int sp_session_folder_history(const char *dir, const char *cwd, */ int sp_session_folder_note(const char *dir, const char *skip_id, struct sp_buf *out); +/* the working directory a session was started in (meta sidecar) */ +int sp_session_cwd(const char *dir, const char *id, char *buf, size_t cap); int sp_session_load(const struct sp_session_info *, struct sp_conv *conv, size_t *loaded, struct sp_buf *err); diff --git a/plugins/session/session.c b/plugins/session/session.c index b3045eb..45f6d44 100644 --- a/plugins/session/session.c +++ b/plugins/session/session.c @@ -21,6 +21,7 @@ #include #include #include +#include #include "splinter.h" #include "splinter_plugin.h" @@ -111,13 +112,18 @@ session_search(const char *args, struct sp_buf *out, void *ctx) const char *dir = sp_host_session_dir(h); const char *cur = sp_host_session_id(h); struct sp_session_brief *v; - char query[200] = "", only[64] = ""; + char query[200] = "", only[64] = "", cwd[512]; int limit = 10, days = 0, nv, i, hits = 0, scanned = 0; long now = (long)time(NULL), budget = SCAN_BYTES; mcp_json *a; if (dir == NULL || *dir == '\0') return sp_buf_puts(out, "error: no session directory"); + /* Only sessions that ran in this working directory: "what did we do + * last?" is about this folder, not about every conversation ever. */ + if (getcwd(cwd, sizeof(cwd)) == NULL) + return sp_buf_puts(out, "error: cannot determine the working " + "directory"); if (args != NULL && args[0] == '{' && (a = mcp_parse(args)) != NULL) { const char *q = mcp_arg_string(a, "query"); const char *s = mcp_arg_string(a, "session"); @@ -152,6 +158,13 @@ session_search(const char *args, struct sp_buf *out, void *ctx) continue; /* running session: context, not history */ if (only[0] != '\0' && strcmp(v[i].id, only) != 0) continue; + { + char scwd[512]; + + if (sp_session_cwd(dir, v[i].id, scwd, + sizeof(scwd)) != 0 || strcmp(scwd, cwd) != 0) + continue; + } if (days > 0 && v[i].mtime < now - (long)days * 86400) continue; if (budget <= 0) @@ -210,12 +223,16 @@ session_list_tool(const char *args, struct sp_buf *out, void *ctx) const char *dir = sp_host_session_dir(h); const char *cur = sp_host_session_id(h); struct sp_session_brief *v; + char cwd[512]; int limit = 15, days = 0, nv, i, shown = 0; long now = (long)time(NULL); mcp_json *a; if (dir == NULL || *dir == '\0') return sp_buf_puts(out, "error: no session directory"); + if (getcwd(cwd, sizeof(cwd)) == NULL) + return sp_buf_puts(out, "error: cannot determine the working " + "directory"); if (args != NULL && args[0] == '{' && (a = mcp_parse(args)) != NULL) { limit = mcp_arg_int(a, "limit", 15); days = mcp_arg_int(a, "days", 0); @@ -232,8 +249,11 @@ session_list_tool(const char *args, struct sp_buf *out, void *ctx) return sp_buf_puts(out, "error: cannot read the session directory"); } for (i = 0; i < nv && shown < limit; i++) { - char stamp[32]; + char stamp[32], scwd[512]; + if (sp_session_cwd(dir, v[i].id, scwd, sizeof(scwd)) != 0 || + strcmp(scwd, cwd) != 0) + continue; if (days > 0 && v[i].mtime < now - (long)days * 86400) continue; stamp_of(v[i].mtime, stamp, sizeof(stamp)); @@ -254,11 +274,13 @@ session_list_tool(const char *args, struct sp_buf *out, void *ctx) static const struct sp_tool search_tool = { "session_search", - "Search the logs of earlier sessions in this folder: what the user and " - "the tools said, with date, session id and title. Use it before asking " - "the user something the fleet may already have answered, and when the " - "prompt says this directory has earlier sessions. The running session is " - "not searched; it is already in your context.", + "Search the logs of earlier sessions that ran in this working directory: " + "what the user and the tools said, with date, session id and title. " + "Sessions from other folders are not searched -- this answers \"what did " + "we do here?\". Use it before asking the user something the fleet may " + "already have answered, and when the prompt says this directory has " + "earlier sessions. The running session is not searched; it is already in " + "your context.", "{\"type\":\"object\",\"properties\":{" "\"query\":{\"type\":\"string\",\"description\":\"text to find " "(case-blind)\"}," @@ -277,9 +299,10 @@ static const struct sp_tool search_tool = { static const struct sp_tool list_tool = { "session_list", - "List the sessions of this folder, newest first: date, id, size and " - "title. Use it to see what was worked on here before, then " - "session_search to read the part you need.", + "List the earlier sessions that ran in this working directory, newest " + "first: date, id, size and title. Sessions from other folders are left " + "out. Use it to see what was worked on here before, then session_search " + "to read the part you need.", "{\"type\":\"object\",\"properties\":{" "\"limit\":{\"type\":\"integer\",\"description\":\"max sessions, default " "15, max 100\"}," diff --git a/src/backend/session.c b/src/backend/session.c index a6353f3..f323f42 100644 --- a/src/backend/session.c +++ b/src/backend/session.c @@ -766,6 +766,20 @@ sp_session_folder_history(const char *dir, const char *cwd, unsigned limit, return 0; } +/* + * The folder a session was started in, from its meta sidecar. The session + * tools use it to stay in the folder: a search that wanders into sessions from + * other directories answers a question nobody asked. + */ +int +sp_session_cwd(const char *dir, const char *id, char *buf, size_t cap) +{ + if (dir == NULL || id == NULL || buf == NULL || cap == 0) + return -1; + buf[0] = '\0'; + return meta_cwd(dir, id, buf, cap); +} + /* * One paragraph for the system prompt: does this working directory have earlier * sessions? Then the model should look there before asking the user or starting diff --git a/tests/test_sessiontools.c b/tests/test_sessiontools.c index 1aa91b9..b877d8d 100644 --- a/tests/test_sessiontools.c +++ b/tests/test_sessiontools.c @@ -88,6 +88,28 @@ main(void) CHECK(sp_session_append(&cur, "user", "the OLD-NEEDLE in the running session", &err) == 0); + /* + * A session that ran in another folder must never show up: "what did we + * do last?" is about this directory, and a search that wanders into + * every conversation ever answers a question nobody asked. + */ + { + char other[800], back[512]; + struct sp_session_info away; + + CHECK(getcwd(back, sizeof(back)) != NULL); + snprintf(other, sizeof(other), "%s/other", dir); + CHECK(mkdir(other, 0700) == 0); + CHECK(chdir(other) == 0); + memset(&away, 0, sizeof(away)); + sp_buf_clear(&err); + CHECK(sp_session_open(&away, dir, "sess-elsewhere", &err) == 0); + sp_buf_clear(&err); + CHECK(sp_session_append(&away, "user", + "the OLD-NEEDLE from another folder", &err) == 0); + CHECK(chdir(back) == 0); + } + host = sp_host_new(®, &ag, &cur); CHECK(host != NULL); sp_buf_clear(&err); @@ -113,8 +135,10 @@ main(void) CHECK(contains(out.p, "sess-old")); CHECK(contains(out.p, "old basalt work")); CHECK(contains(out.p, "OLD-NEEDLE")); - /* the running session is context, not history */ + /* the running session is context, not history; another folder is another + * conversation */ CHECK(!contains(out.p, "sess-now")); + CHECK(!contains(out.p, "sess-elsewhere")); sp_buf_clear(&out); /* nothing found is a sentence, not an empty answer */ @@ -137,6 +161,13 @@ main(void) CHECK(!contains(out.p, "sess-old")); sp_buf_clear(&out); + /* even an explicit id cannot pull in another folder */ + CHECK(search->run("{\"query\":\"OLD-NEEDLE\"," + "\"session\":\"sess-elsewhere\"}", &out, search->ctx) == 0); + CHECK(contains(out.p, "no message")); + CHECK(!contains(out.p, "sess-elsewhere")); + sp_buf_clear(&out); + /* days: the old session is 40 days old, 7 days must not see it */ age_session(dir, "sess-old", 40L * 86400L); CHECK(search->run("{\"query\":\"OLD-NEEDLE\",\"days\":7}", &out, @@ -149,6 +180,7 @@ main(void) CHECK(contains(out.p, "sess-old")); CHECK(contains(out.p, "sess-now")); CHECK(contains(out.p, "(current)")); + CHECK(!contains(out.p, "sess-elsewhere")); sp_buf_clear(&out); /* missing query is an error, not a wildcard search */