From fb877e263fe8acdfa246a9a013dcfb1e4d03541e Mon Sep 17 00:00:00 2001 From: Konrad Lother Date: Fri, 20 Nov 2026 15:07:36 +0000 Subject: [PATCH] sessions: state root vs session root, sessions under ~/.splinter/sessions Add state_dir (config/token/commands/USER.txt, default ~/.splinter) and default session_dir to /sessions. Telegram uses /telegram, so CLI and gateway sessions are separate trees. Keep --session and the old configs working. Also carries the pending apply_selection model-pointer fix from the n95 tree. --- src/backend/config.c | 26 ++++++++++++++++++++++ src/backend/config.h | 9 ++++++++ src/backend/engine.c | 23 ++++++-------------- src/backend/telegram.c | 11 +++------- src/main.c | 49 ++++++++++++++++++++++++++---------------- tests/test_gateway.sh | 2 +- 6 files changed, 76 insertions(+), 44 deletions(-) diff --git a/src/backend/config.c b/src/backend/config.c index a5f2090..befb9a0 100644 --- a/src/backend/config.c +++ b/src/backend/config.c @@ -26,6 +26,30 @@ sp_config_init(struct sp_config *c) snprintf(c->key_env, sizeof(c->key_env), "SPLINTER_API_KEY"); } +const char * +sp_config_state_dir(const struct sp_config *c) +{ + static char buf[512]; + const char *home; + + if (c != NULL && c->state_dir[0] != '\0') + return c->state_dir; + home = getenv("HOME"); + snprintf(buf, sizeof(buf), "%s/.splinter", home != NULL ? home : "."); + return buf; +} + +const char * +sp_config_session_dir(const struct sp_config *c) +{ + static char buf[512]; + + if (c != NULL && c->session_dir[0] != '\0') + return c->session_dir; + snprintf(buf, sizeof(buf), "%s/sessions", sp_config_state_dir(c)); + return buf; +} + int sp_config_path(char *buf, size_t len) { @@ -686,6 +710,8 @@ load_stream(struct sp_config *c, FILE *f, const char *path, set_str(c->system_prompt, sizeof(c->system_prompt), val); else if (!strcmp(key, "session_dir")) set_str(c->session_dir, sizeof(c->session_dir), val); + else if (!strcmp(key, "state_dir")) + set_str(c->state_dir, sizeof(c->state_dir), val); else if (!strcmp(key, "cli_cmd")) set_str(c->cli_cmd, sizeof(c->cli_cmd), val); else if (!strcmp(key, "cli_fallback")) diff --git a/src/backend/config.h b/src/backend/config.h index e62ea0b..4857c88 100644 --- a/src/backend/config.h +++ b/src/backend/config.h @@ -141,6 +141,7 @@ struct sp_config { char theme[64]; char system_prompt[1024]; char session_dir[512]; + char state_dir[512]; /* config, token, commands, USER.txt; default ~/.splinter */ char cli_cmd[512]; char cli_env[SP_CONFIG_MAX_ENV][SP_CONFIG_ENV_LEN]; /* NAME=VALUE */ size_t ncli_env; @@ -200,6 +201,14 @@ const char *sp_config_loaded_path(void); /* Write a commented starter file. Returns 0 or -1. */ int sp_config_write_example(const char *path, struct sp_buf *err); +/* + * The state root: config, token, commands and USER.txt. Default ~/.splinter. + * The session root: where the .jsonl sessions live. Default /sessions; + * the Telegram daemon puts its own tree below /telegram. + */ +const char *sp_config_state_dir(const struct sp_config *); +const char *sp_config_session_dir(const struct sp_config *); + /* * A plugin's own setting (plugin NAME { key = value }), or NULL. 'key' may be * dotted for a nested block: "server.junction.url". diff --git a/src/backend/engine.c b/src/backend/engine.c index 9657ab6..2649e22 100644 --- a/src/backend/engine.c +++ b/src/backend/engine.c @@ -1633,9 +1633,7 @@ capture_interview(struct sp_engine *e, struct sp_buf *answer) if (answer == NULL || answer->p == NULL) return 0; - if (!sp_interview_capture( - e->conf != NULL && e->conf->session_dir[0] != '\0' ? - e->conf->session_dir : e->sess.dir, answer->p, clean, + if (!sp_interview_capture(sp_config_state_dir(e->conf), answer->p, clean, sizeof(clean))) return 0; sp_buf_clear(answer); @@ -1977,7 +1975,7 @@ sp_engine_command_names(struct sp_engine *e, char (*names)[64], int max) sp_buf_free(&b); } if (sp_buf_init(&b) == 0) { - if (sp_cmdscript_dir(e->sess.dir, cdir, sizeof(cdir)) == 0 && + if (sp_cmdscript_dir(sp_config_state_dir(e->conf), cdir, sizeof(cdir)) == 0 && sp_cmdscript_list(cdir, &b) > 0 && b.p != NULL) { for (tok = strtok_r(b.p, " \t\r\n", &save); tok != NULL; tok = strtok_r(NULL, " \t\r\n", &save)) @@ -2991,7 +2989,7 @@ sp_engine_input(struct sp_engine *e, const char *line, struct sp_buf *out) sp_buf_clear(out); if (sp_buf_init(&cerr) != 0) return SP_INPUT_HANDLED; - if (sp_cmdscript_dir(e->sess.dir, cdir, sizeof(cdir)) == 0 && + if (sp_cmdscript_dir(sp_config_state_dir(e->conf), cdir, sizeof(cdir)) == 0 && sp_cmdscript_exists(cdir, name)) { if (sp_slash_script(cdir, name, args, out, &cerr) == 0 && out->p != NULL) { @@ -3021,7 +3019,7 @@ static int open_session(struct sp_engine *e, const struct sp_engine_opts *o, struct sp_buf *err, int *code) { - static char sessbuf[512], idbuf[64]; + static char idbuf[64]; const char *dir = o->session_dir; const char *id = o->session_id; const char *envdir = getenv("SPLINTER_SESSION_DIR"); @@ -3033,13 +3031,8 @@ open_session(struct sp_engine *e, const struct sp_engine_opts *o, */ if (dir == NULL && envdir != NULL && *envdir != '\0') dir = envdir; - if (dir == NULL) { - const char *home = getenv("HOME"); - - snprintf(sessbuf, sizeof(sessbuf), "%s/.splinter", - home != NULL ? home : "."); - dir = sessbuf; - } + if (dir == NULL) + dir = sp_config_session_dir(o->conf); /* * -c means "the last session that happened *here*". The cwd at start is * the key, so a session in another tree is never silently loaded. @@ -3439,9 +3432,7 @@ sp_engine_open(struct sp_engine **out, const struct sp_engine_opts *o, * What the interview learned about the user rides in the system * prompt of every session, not in a tool the model has to call. */ - char *ub = sp_interview_user_block( - e->conf != NULL && e->conf->session_dir[0] != '\0' ? - e->conf->session_dir : e->sess.dir); + char *ub = sp_interview_user_block(sp_config_state_dir(e->conf)); if (ub != NULL) { size_t bl = e->base_prompt != NULL ? diff --git a/src/backend/telegram.c b/src/backend/telegram.c index aee3183..c0d58fe 100644 --- a/src/backend/telegram.c +++ b/src/backend/telegram.c @@ -2081,7 +2081,7 @@ sp_telegram_notify(const struct sp_config *conf, const char *text, struct sp_buf *err) { char token[256], path[700]; - const char *base, *home, *p; + const char *base, *p; long chat = 0; if (conf == NULL || text == NULL) @@ -2099,13 +2099,8 @@ sp_telegram_notify(const struct sp_config *conf, const char *text, sp_buf_puts(err, "telegram: no status_chat and no allow id"); return -1; } - base = conf->session_dir[0] != '\0' ? conf->session_dir : NULL; - home = getenv("HOME"); - if (base != NULL) - snprintf(path, sizeof(path), "%s/telegram.token", base); - else - snprintf(path, sizeof(path), "%s/.splinter/telegram.token", - home != NULL ? home : "."); + base = sp_config_state_dir(conf); + snprintf(path, sizeof(path), "%s/telegram.token", base); if (sp_telegram_token(token, sizeof(token), path) != 0) { if (err != NULL) sp_buf_printf(err, "telegram: no token in %s", path); diff --git a/src/main.c b/src/main.c index 5735b3c..00ae536 100644 --- a/src/main.c +++ b/src/main.c @@ -943,7 +943,18 @@ apply_selection(struct opts *o, struct sp_config *conf) } } o->endpoint = d->endpoint; - o->model = ch.model[0] != '\0' ? ch.model : d->model; + /* + * ch lives on this frame and dies with it; pointing o->model into it + * handed the transport a freed stack slot, and junction answered 403 + * model_not_allowed with a model name of pure garbage. + */ + { + static char kmodel[256]; + + snprintf(kmodel, sizeof(kmodel), "%s", + ch.model[0] != '\0' ? ch.model : d->model); + o->model = kmodel; + } if (d->key_env[0] != '\0') o->key_env = d->key_env; return 0; @@ -1239,7 +1250,7 @@ main(int argc, char **argv) struct sp_engine *eng = NULL; struct sp_buf err; const char *daemon_base = NULL; - char tgdir[1024], basebuf[1024]; + char tgdir[1024], statebuf[1024], sessbuf[1024]; int rc; memset(&o, 0, sizeof(o)); @@ -1423,25 +1434,25 @@ main(int argc, char **argv) * (and /new in the chat never creates a file a CLI would resume). */ if (o.daemon) { - const char *base = o.sessdir; + const char *state = sp_config_state_dir(&conf); + const char *sess = o.sessdir; - if (base == NULL || *base == '\0') { - const char *home = getenv("HOME"); + snprintf(statebuf, sizeof(statebuf), "%s", state); + if (sess == NULL || *sess == '\0') + sess = sp_config_session_dir(&conf); + snprintf(sessbuf, sizeof(sessbuf), "%s", sess); + { + size_t blen = strlen(sessbuf); - snprintf(basebuf, sizeof(basebuf), "%s/.splinter", - home != NULL ? home : "."); - base = basebuf; + if (blen + sizeof("/telegram") > sizeof(tgdir)) { + fprintf(stderr, "splinter: session path too long\n"); + sp_buf_free(&err); + return 2; + } + memcpy(tgdir, sessbuf, blen); + memcpy(tgdir + blen, "/telegram", sizeof("/telegram")); } - size_t blen = strlen(base); - - if (blen + sizeof("/telegram") > sizeof(tgdir)) { - fprintf(stderr, "splinter: session path too long\n"); - sp_buf_free(&err); - return 2; - } - memcpy(tgdir, base, blen); - memcpy(tgdir + blen, "/telegram", sizeof("/telegram")); - daemon_base = base; + daemon_base = statebuf; eo.session_dir = tgdir; eo.session_id = NULL; eo.resume = 1; @@ -1578,7 +1589,7 @@ main(int argc, char **argv) } else if (o.daemon) rc = run_daemon(eng, daemon_base, &err); else if (o.chat) { - if (!sp_profile_exists(sp_engine_state_dir(eng))) + if (!sp_profile_exists(sp_config_state_dir(&conf))) fprintf(stderr, sp_td("main.no_profile", "splinter: no profile for '%s' - " "`splinter --interview` creates one\n"), diff --git a/tests/test_gateway.sh b/tests/test_gateway.sh index f081d70..39ebba6 100755 --- a/tests/test_gateway.sh +++ b/tests/test_gateway.sh @@ -173,7 +173,7 @@ check "the reference does not stay in the chat" \ # /new starts a fresh conversation and a fresh session file run_text "/new" 'Neue Konversation: sess-' 45 check "the bot says the new session" grep -q '^Neue Konversation: sess-' "$D/sent.log" -check "a session file was opened" sh -c "ls '$D'/home/.splinter/telegram/sess-*.jsonl | tail -1 | grep -q sess-" +check "a session file was opened" sh -c "ls '$D'/home/.splinter/sessions/telegram/sess-*.jsonl | tail -1 | grep -q sess-" # /model offers the configured models and switches on the button run_text "/model" 'Provider far' 60 '^openai:'