sessions get titles, /rename and /rm

- .meta carries a title: the first user line is stored once, /rename
  [ID] TITLE replaces it. Reopening preserves the title and the original
  creation time.
- /sessions shows the title, locally and over the link; the protocol's
  sessions event carries it. Old sessions without a stored title fall back
  to their first user line when listed.
- /rm ID removes a session that is not open: JSONL, meta, saved agent
  profile and logs/<id>; the open session is refused, /rm last resolves
  the newest.
- tests: test_session (auto title, explicit title survives reopen, old-log
  fallback, recursive removal), test_protocol (title, rename, rm, errors),
  test_ui (title, rename, rm). make check green.
This commit is contained in:
lotherk committed 2026-11-16 12:10:06 +00:00
1 parent a01bfe9df1
commit dd135f3b33
18 files changed
+643 -34

No files matched your search

+4 -2
View File
@@ -131,8 +131,10 @@ The engine keeps more than one session: `sessions` lists the directory, `open`
switches to an id (or `new`/`last`) and sends the state and history of that
session. The screen offers the same as `/sessions` and `/open ID`, locally and
over the socket; a local switch closes the engine and reopens it from the same
options, because plugins keep process globals. One session is active at a time;
over the socket, one client at a time.
options, because plugins keep process globals. `/sessions` shows a title - the
first user line until `/rename [ID] TITLE` says otherwise - and `/rm ID`
removes a session that is not open. One session is active at a time; over the
socket, one client at a time.
`--attach` reconnects: if the server dies, the screen says so, keeps running,
and attaches again when a server is back on the socket - asking for the session
+9 -3
View File
@@ -132,8 +132,9 @@ out — this is what the person and the model said.
### `{"cmd":"sessions"}`
List the sessions in the server's session directory, newest first:
`"ev":"sessions"`, `"items":[{"id","mtime","bytes","current"}]`. `current` marks
the session the connection is on.
`"ev":"sessions"`, `"items":[{"id","title","mtime","bytes","current"}]`.
`title` is the first user line or what `/rename` set (empty when there is no
message yet), `current` marks the session the connection is on.
### `{"cmd":"open","session":"ID"}`
@@ -144,6 +145,11 @@ Switch this connection to another session. `"new"` starts a fresh one,
While a turn runs the command is refused. An unknown id is an error and the
connection stays on its session.
`{"cmd":"input","line":"/rename [ID] TITLE"}` names the current session (or the
one named by ID), and `{"cmd":"input","line":"/rm ID"}` removes a session that is
not the open one: its JSONL, sidecars and `<dir>/logs/<id>` directory. Both
answer with a `reply` event.
### `{"cmd":"commands"}`
Ask what a command palette may offer: `"ev":"commands"`,
@@ -175,7 +181,7 @@ with 0.
| `reply` | `text` | the answer to a command; may be empty |
| `error` | `text` | the command was refused |
| `commands` | `names[]` | the slash commands, for a palette |
| `sessions` | `items[]` | the sessions, with id/mtime/bytes/current |
| `sessions` | `items[]` | the sessions, with id/title/mtime/bytes/current |
| `opened` | `session`, `dir` | the switch is complete |
| `history` | `truncated`, `items[]` | the transcript so far (role/text) |
| `pong` | — | answer to `ping` |
+5
View File
@@ -12,6 +12,11 @@ Stream.
## Erledigt
- **Session-Titel 2026-11-16**: `.meta` traegt `title` (erste User-Zeile,
ueberschreibbar per `/rename [ID] TITEL`); `sessions` liefert ihn als
`title`, `/sessions` zeigt ihn. `/rm ID` entfernt JSONL, Sidecars und
`logs/<id>` und verweigert die offene Session. Tests: `test_session`,
`test_protocol.sh`, `test_ui.sh`.
- **Nachtrag 2026-11-16**: Session-Wechsel auch im lokalen UI
(`sp_engine_switch`, `/sessions` + `/open` ohne `--attach`), Gateway reicht
`temperature`/`top_p`/`max_tokens` durch, liest chunked Bodies und gibt bei
+9 -1
View File
@@ -127,7 +127,15 @@ The commands
.Pa /sessions
and
.Pa /open Ar ID
switch the server's session.
switch the session.
.Pa /sessions
shows each session with its title: the first user line, or what
.Pa /rename
.Pq Op Ar ID
.Ar TITLE
set.
.Pa /rm Ar ID
removes a session that is not open.
If the server goes away, the screen keeps running, reconnects and asks for the
session it was on.
.It Fl -socket Ar PATH
+10 -1
View File
@@ -225,15 +225,24 @@ int sp_session_folder_history(const char *dir, const char *cwd,
int sp_session_load(const struct sp_session_info *, struct sp_conv *conv,
size_t *loaded, struct sp_buf *err);
/* one line of a session listing: id, mtime, bytes; newest first */
/* one line of a session listing: id, title, mtime, bytes; newest first */
struct sp_session_brief {
char id[64];
char title[96]; /* set title, or the first user line */
long mtime;
size_t bytes;
};
/* up to max entries from dir; returns the count (0..max), or -1 */
int sp_session_list(const char *dir, struct sp_session_brief *v,
size_t max);
/* give a session a title (one line, at most 95 bytes) */
int sp_session_set_title(const char *dir, const char *id,
const char *title, struct sp_buf *err);
/*
* Remove a session: its log, meta, saved profile and the logs/<id> directory.
* The caller must not delete the session its engine has open.
*/
int sp_session_delete(const char *dir, const char *id, struct sp_buf *err);
/* ----------------------------------------------------------------- agent */
+9
View File
@@ -10,6 +10,7 @@ ui.working = laeuft...
ui.reconnected = wieder verbunden
ui.reconnecting = Verbindung weg - verbinde neu ...
ui.no_sessions = noch keine Sitzungen
ui.untitled = (ohne Titel)
ui.session_opened = Sitzung geoeffnet
ui.session_gone = Sitzung %s ist weg - jetzt auf %s
ui.compacted = Kontext kompaktiert: %zu->%zu Nachrichten, %s->%s (%s->%s)
@@ -130,6 +131,14 @@ interview.error.write = interview: kann %s nicht schreiben
# /model und /connect (Gateway)
session.new = Neue Konversation: %s
session.failed = Konnte keine neue Sitzung anlegen: %s
session.renamed = Sitzung %s heisst jetzt "%s"
session.rename_failed = Konnte die Sitzung nicht umbenennen: %s
session.rename_usage = Aufruf: /rename [ID] TITEL
session.removed = Sitzung %s entfernt
session.remove_failed = Konnte %s nicht entfernen: %s
session.rm_current = Die offene Sitzung nicht entfernbar; vorher /open auf eine andere
session.rm_usage = Aufruf: /rm ID
session.no_session = Keine solche Sitzung: %s
tg.cmd.new = neue Konversation
tg.cmd.model = ein Modell waehlen
+9
View File
@@ -9,6 +9,7 @@ ui.working = working...
ui.reconnected = reconnected
ui.reconnecting = connection lost - reconnecting ...
ui.no_sessions = no sessions yet
ui.untitled = (untitled)
ui.session_opened = session opened
ui.session_gone = session %s is gone - now on %s
ui.compacted = context compacted: %zu->%zu msgs, %s->%s (%s->%s)
@@ -129,6 +130,14 @@ interview.error.write = interview: cannot write %s
# /model and /connect (gateway)
session.new = New conversation: %s
session.failed = Could not start a new session: %s
session.renamed = Session %s renamed to "%s"
session.rename_failed = Cannot rename the session: %s
session.rename_usage = Usage: /rename [ID] TITLE
session.removed = Session %s removed
session.remove_failed = Cannot remove %s: %s
session.rm_current = Cannot remove the open session; /open another first
session.rm_usage = Usage: /rm ID
session.no_session = No such session: %s
tg.cmd.new = new conversation
tg.cmd.model = choose a model
+9
View File
@@ -10,6 +10,7 @@ ui.working = trabalhando...
ui.reconnected = reconectado
ui.reconnecting = conexao perdida - reconectando ...
ui.no_sessions = nenhuma sessao ainda
ui.untitled = (sem titulo)
ui.session_opened = sessao aberta
ui.session_gone = sessao %s sumiu - agora em %s
ui.compacted = contexto compactado: %zu->%zu msgs, %s->%s (%s->%s)
@@ -133,6 +134,14 @@ interview.error.write = interview: nao consigo escrever %s
# /model e /connect (gateway)
session.new = Nova conversa: %s
session.failed = Nao consegui abrir uma sessao nova: %s
session.renamed = Sessao %s agora se chama "%s"
session.rename_failed = Nao consegui renomear a sessao: %s
session.rename_usage = Uso: /rename [ID] TITULO
session.removed = Sessao %s removida
session.remove_failed = Nao consegui remover %s: %s
session.rm_current = Nao posso remover a sessao aberta; use /open em outra antes
session.rm_usage = Uso: /rm ID
session.no_session = Nenhuma sessao assim: %s
tg.cmd.new = nova conversa
tg.cmd.model = escolher um modelo
+112 -2
View File
@@ -1439,8 +1439,8 @@ sp_engine_command_names(struct sp_engine *e, char (*names)[64], int max)
{
static const char *builtin[] = {
"agent", "agents", "connect", "exit", "model", "new", "q",
"quit", "reset", "resume", "status", "swarm", "usage",
"widgets"
"quit", "rename", "reset", "resume", "rm", "status", "swarm",
"usage", "widgets"
};
struct sp_buf b;
char cdir[700], *p, *tok, *save;
@@ -1637,6 +1637,108 @@ cmd_new(struct sp_engine *e, struct sp_buf *out)
sp_buf_free(&serr);
}
static const char *
skip_ws(const char *p)
{
while (*p == ' ' || *p == '\t')
p++;
return p;
}
/*
* /rename [ID] TITLE. Without an id the current session gets the title; an id
* that names an existing session in this directory renames that one, so
* /rename sess-... Titel works while another session stays open.
*/
static void
cmd_rename(struct sp_engine *e, const char *args, struct sp_buf *out)
{
char id[64], path[800], title[1536];
struct sp_buf serr;
const char *p = skip_ws(args), *rest;
size_t n;
if (*p == '\0') {
sp_buf_puts(out, sp_t("session.rename_usage"));
return;
}
n = strcspn(p, " \t");
rest = skip_ws(p + n);
if (*rest != '\0' && n < sizeof(id)) {
snprintf(id, sizeof(id), "%.*s", (int)n, p);
snprintf(path, sizeof(path), "%s/%s.jsonl", e->sess.dir, id);
if (access(path, R_OK) == 0) {
if (sp_buf_init(&serr) != 0)
return;
if (sp_session_set_title(e->sess.dir, id, rest,
&serr) == 0) {
sp_buf_printf(out, sp_t("session.renamed"), id,
rest);
} else
sp_buf_printf(out, sp_t("session.rename_failed"),
serr.p != NULL ? serr.p : "?");
sp_buf_free(&serr);
return;
}
}
/* no such session id in the first token: the whole line is the title */
if (sp_buf_init(&serr) != 0)
return;
snprintf(title, sizeof(title), "%s", p);
if (sp_session_set_title(e->sess.dir, e->sess.id, title, &serr) == 0)
sp_buf_printf(out, sp_t("session.renamed"), e->sess.id, title);
else
sp_buf_printf(out, sp_t("session.rename_failed"),
serr.p != NULL ? serr.p : "?");
sp_buf_free(&serr);
}
/*
* /rm ID removes a session that is not the one this engine has open. The id
* must be complete; "last" is the newest session in the directory.
*/
static void
cmd_rm(struct sp_engine *e, const char *args, struct sp_buf *out)
{
char id[64], latest[64];
struct sp_buf serr;
const char *p = skip_ws(args);
size_t n;
if (*p == '\0') {
sp_buf_puts(out, sp_t("session.rm_usage"));
return;
}
n = strcspn(p, " \t");
if (n == 0 || n >= sizeof(id) || *skip_ws(p + n) != '\0') {
sp_buf_puts(out, sp_t("session.rm_usage"));
return;
}
snprintf(id, sizeof(id), "%.*s", (int)n, p);
if (strcmp(id, "last") == 0) {
if (sp_session_latest(e->sess.dir, latest,
sizeof(latest)) != 0) {
sp_buf_printf(out, sp_t("session.no_session"), id);
return;
}
snprintf(id, sizeof(id), "%s", latest);
}
if (strcmp(id, e->sess.id) == 0) {
sp_buf_puts(out, sp_t("session.rm_current"));
return;
}
if (sp_buf_init(&serr) != 0)
return;
if (sp_session_delete(e->sess.dir, id, &serr) == 0)
sp_buf_printf(out, sp_t("session.removed"), id);
else if (serr.p != NULL && strstr(serr.p, "no such session") != NULL)
sp_buf_printf(out, sp_t("session.no_session"), id);
else
sp_buf_printf(out, sp_t("session.remove_failed"), id,
serr.p != NULL ? serr.p : "?");
sp_buf_free(&serr);
}
static void
cmd_status(struct sp_engine *e, struct sp_buf *out)
{
@@ -1859,6 +1961,14 @@ sp_engine_input(struct sp_engine *e, const char *line, struct sp_buf *out)
cmd_new(e, out);
return SP_INPUT_HANDLED;
}
if (strcmp(name, "rename") == 0) {
cmd_rename(e, args, out);
return SP_INPUT_HANDLED;
}
if (strcmp(name, "rm") == 0) {
cmd_rm(e, args, out);
return SP_INPUT_HANDLED;
}
if (strcmp(name, "connect") == 0) {
if (sp_config_loaded_path() == NULL) {
sp_buf_puts(out, sp_t("connect.nodir"));
+308 -13
View File
@@ -49,6 +49,212 @@ mkdir_p(const char *path)
return 0;
}
/* the session's small sidecar: where it started, when, and its title */
struct session_meta {
char cwd[512];
long created;
long opened;
char title[96];
};
/* a title is one line, no control bytes, at most 95 bytes of UTF-8 */
static void
title_from_text(const char *text, char *out, size_t cap)
{
const char *p = text != NULL ? text : "";
size_t n = 0;
if (cap == 0)
return;
while (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r')
p++;
while (*p != '\0' && *p != '\n' && *p != '\r' && n + 1 < cap) {
unsigned char c = (unsigned char)*p;
if (c >= 0x20 && c != 0x7f)
out[n++] = (c == '\t') ? ' ' : (char)c;
p++;
}
while (n > 0 && (out[n - 1] == ' ' || out[n - 1] == '\t'))
n--;
/*
* Do not leave half a UTF-8 sequence: if the last character was cut,
* drop the whole character.
*/
while (n > 0) {
size_t start = n;
size_t need;
unsigned char c;
while (start > 0 && ((unsigned char)out[start - 1] & 0xC0) == 0x80)
start--;
if (start == 0)
break;
c = (unsigned char)out[start - 1];
if ((c & 0x80) == 0)
need = 1;
else if ((c & 0xE0) == 0xC0)
need = 2;
else if ((c & 0xF0) == 0xE0)
need = 3;
else if ((c & 0xF8) == 0xF0)
need = 4;
else {
n = start - 1;
continue;
}
if (start - 1 + need > n) {
n = start - 1;
continue;
}
break;
}
out[n] = '\0';
}
static int
meta_read(const char *dir, const char *id, struct session_meta *m)
{
char path[700], line[700];
FILE *f;
memset(m, 0, sizeof(*m));
snprintf(path, sizeof(path), "%s/%s.meta", dir, id);
f = fopen(path, "r");
if (f == NULL)
return -1;
while (fgets(line, sizeof(line), f) != NULL) {
char *v = strchr(line, '=');
if (v == NULL)
continue;
*v++ = '\0';
v[strcspn(v, "\r\n")] = '\0';
if (strcmp(line, "cwd") == 0)
snprintf(m->cwd, sizeof(m->cwd), "%s", v);
else if (strcmp(line, "created") == 0)
m->created = strtol(v, NULL, 10);
else if (strcmp(line, "opened") == 0)
m->opened = strtol(v, NULL, 10);
else if (strcmp(line, "title") == 0)
snprintf(m->title, sizeof(m->title), "%s", v);
}
fclose(f);
return 0;
}
static int
meta_write(const char *dir, const char *id, const struct session_meta *m)
{
char path[700], tmp[800];
FILE *f;
snprintf(path, sizeof(path), "%s/%s.meta", dir, id);
snprintf(tmp, sizeof(tmp), "%s.tmp", path);
f = fopen(tmp, "w");
if (f == NULL)
return -1;
fprintf(f, "cwd=%s\ncreated=%ld\nopened=%ld\n", m->cwd, m->created,
m->opened);
if (m->title[0] != '\0')
fprintf(f, "title=%s\n", m->title);
if (fclose(f) != 0) {
(void)unlink(tmp);
return -1;
}
if (rename(tmp, path) != 0) {
(void)unlink(tmp);
return -1;
}
return 0;
}
/* an old session has no stored title: its first user line is the title */
static void
first_user_title(const char *path, char *out, size_t cap)
{
char *line = NULL;
size_t linecap = 0;
FILE *f;
out[0] = '\0';
f = fopen(path, "r");
if (f == NULL)
return;
while (getline(&line, &linecap, f) > 0) {
mcp_json *v;
const char *role, *text;
size_t len = strlen(line);
if (len == 0 || line[len - 1] != '\n')
break; /* a torn tail is not a record */
v = mcp_parse(line);
if (v == NULL)
continue;
role = mcp_arg_string(v, "role");
text = mcp_arg_string(v, "text");
if (role != NULL && text != NULL &&
strcmp(role, "user") == 0) {
title_from_text(text, out, cap);
mcp_json_free(v);
break;
}
mcp_json_free(v);
}
free(line);
fclose(f);
}
static int
valid_id(const char *id)
{
size_t i, n;
if (id == NULL || *id == '\0')
return 0;
n = strlen(id);
if (n >= 64)
return 0;
for (i = 0; i < n; i++)
if (!((id[i] >= 'a' && id[i] <= 'z') ||
(id[i] >= 'A' && id[i] <= 'Z') ||
(id[i] >= '0' && id[i] <= '9') ||
id[i] == '-' || id[i] == '_' || id[i] == '.'))
return 0;
return strstr(id, "..") == NULL;
}
/* remove a directory tree without following symlinks */
static int
rm_tree(const char *path)
{
struct stat st;
DIR *d;
struct dirent *e;
if (lstat(path, &st) != 0)
return errno == ENOENT ? 0 : -1;
if (!S_ISDIR(st.st_mode))
return unlink(path);
d = opendir(path);
if (d == NULL)
return -1;
while ((e = readdir(d)) != NULL) {
char child[800];
if (strcmp(e->d_name, ".") == 0 ||
strcmp(e->d_name, "..") == 0)
continue;
snprintf(child, sizeof(child), "%s/%s", path, e->d_name);
if (rm_tree(child) != 0) {
closedir(d);
return -1;
}
}
closedir(d);
return rmdir(path);
}
int
sp_session_open(struct sp_session_info *info, const char *dir,
const char *id, struct sp_buf *err)
@@ -83,23 +289,22 @@ sp_session_open(struct sp_session_info *info, const char *dir,
/*
* A session belongs to the directory it was started in. That is what '-c'
* needs: the last session that happened *here*, not the last one anywhere.
* The title and the original creation time survive a reopen.
*/
{
char mpath[700], cwd[512], tmp[800];
FILE *m;
time_t now = time(NULL);
struct session_meta m;
time_t now = time(NULL);
if (getcwd(cwd, sizeof(cwd)) == NULL)
snprintf(cwd, sizeof(cwd), "?");
snprintf(mpath, sizeof(mpath), "%s/%s.meta", dir, info->id);
snprintf(tmp, sizeof(tmp), "%s.tmp", mpath);
m = fopen(tmp, "w");
if (m != NULL) {
fprintf(m, "cwd=%s\ncreated=%ld\nopened=%ld\n", cwd,
(long)now, (long)now);
fclose(m);
(void)rename(tmp, mpath);
if (meta_read(dir, info->id, &m) != 0) {
memset(&m, 0, sizeof(m));
m.created = (long)now;
}
if (getcwd(m.cwd, sizeof(m.cwd)) == NULL)
snprintf(m.cwd, sizeof(m.cwd), "?");
if (m.created == 0)
m.created = (long)now;
m.opened = (long)now;
(void)meta_write(dir, info->id, &m);
}
return 0;
}
@@ -160,6 +365,17 @@ sp_session_list(const char *dir, struct sp_session_brief *v, size_t max)
if (stat(path, &st) != 0)
continue;
snprintf(v[n].id, sizeof(v[n].id), "%s", id);
v[n].title[0] = '\0';
{
struct session_meta m;
if (meta_read(dir, id, &m) == 0)
snprintf(v[n].title, sizeof(v[n].title),
"%s", m.title);
}
if (v[n].title[0] == '\0')
first_user_title(path, v[n].title,
sizeof(v[n].title));
v[n].mtime = (long)st.st_mtime;
v[n].bytes = (size_t)st.st_size;
n++;
@@ -211,6 +427,21 @@ sp_session_append(const struct sp_session_info *info, const char *role,
sp_buf_printf(err, "session: write failed: %s", strerror(errno));
close(fd);
sp_buf_free(&line);
/*
* The first user line is the session's title until /rename says
* otherwise. Written once, so a long conversation does not rewrite the
* sidecar on every turn.
*/
if (rc == 0 && role != NULL && strcmp(role, "user") == 0) {
struct session_meta m;
if (meta_read(info->dir, info->id, &m) == 0 &&
m.title[0] == '\0') {
title_from_text(text, m.title, sizeof(m.title));
if (m.title[0] != '\0')
(void)meta_write(info->dir, info->id, &m);
}
}
return rc;
}
@@ -521,3 +752,67 @@ sp_session_folder_history(const char *dir, const char *cwd, unsigned limit,
sp_buf_printf(out, "(%zu more)\n", n - limit);
return 0;
}
int
sp_session_set_title(const char *dir, const char *id, const char *title,
struct sp_buf *err)
{
struct session_meta m;
char path[700], clean[96];
if (dir == NULL || id == NULL || title == NULL ||
!valid_id(id)) {
sp_buf_puts(err, "session: invalid id");
return -1;
}
snprintf(path, sizeof(path), "%s/%s.jsonl", dir, id);
if (access(path, F_OK) != 0) {
sp_buf_puts(err, "session: no such session");
return -1;
}
title_from_text(title, clean, sizeof(clean));
if (clean[0] == '\0') {
sp_buf_puts(err, "session: empty title");
return -1;
}
if (meta_read(dir, id, &m) != 0)
memset(&m, 0, sizeof(m));
snprintf(m.title, sizeof(m.title), "%s", clean);
if (meta_write(dir, id, &m) != 0) {
sp_buf_printf(err, "session: cannot write the meta file: %s",
strerror(errno));
return -1;
}
return 0;
}
int
sp_session_delete(const char *dir, const char *id, struct sp_buf *err)
{
char path[700], side[700], logdir[700];
if (dir == NULL || id == NULL || !valid_id(id)) {
sp_buf_puts(err, "session: invalid id");
return -1;
}
snprintf(path, sizeof(path), "%s/%s.jsonl", dir, id);
if (access(path, F_OK) != 0) {
sp_buf_puts(err, "session: no such session");
return -1;
}
if (unlink(path) != 0) {
sp_buf_printf(err, "session: cannot remove %s: %s", path,
strerror(errno));
return -1;
}
snprintf(side, sizeof(side), "%s/%s.meta", dir, id);
(void)unlink(side);
snprintf(side, sizeof(side), "%s/%s.meta.tmp", dir, id);
(void)unlink(side);
snprintf(side, sizeof(side), "%s/%s.agent", dir, id);
(void)unlink(side);
/* logs are a convenience, not the record: a leftover must not fail this */
snprintf(logdir, sizeof(logdir), "%s/logs/%s", dir, id);
(void)rm_tree(logdir);
return 0;
}
+5 -2
View File
@@ -2202,7 +2202,8 @@ ui_turn(struct ui_state *s, const char *text)
}
static void
session_line(void *cbctx, const char *id, int current, long mtime)
session_line(void *cbctx, const char *id, const char *title, int current,
long mtime)
{
struct sp_buf *b = cbctx;
struct tm tm;
@@ -2211,7 +2212,9 @@ session_line(void *cbctx, const char *id, int current, long mtime)
if (localtime_r(&t, &tm) != NULL)
strftime(when, sizeof(when), "%Y-%m-%d %H:%M", &tm);
sp_buf_printf(b, "%c %-32s %s\n", current ? '*' : ' ', id, when);
sp_buf_printf(b, "%c %s %-32s %-40.40s\n", current ? '*' : ' ',
when, id, title != NULL && *title != '\0' ?
title : sp_t("ui.untitled"));
}
static void
+2
View File
@@ -453,6 +453,8 @@ send_sessions(struct client *c, struct proto *p)
sp_buf_puts(&b, ",");
sp_buf_puts(&b, "{\"id\":\"");
(void)sp_json_escape(&b, v[i].id);
sp_buf_puts(&b, "\",\"title\":\"");
(void)sp_json_escape(&b, v[i].title);
sp_buf_printf(&b, "\",\"mtime\":%ld,\"bytes\":%zu,"
"\"current\":%s}", v[i].mtime, v[i].bytes,
strcmp(v[i].id, cur) == 0 ? "true" : "false");
+2 -2
View File
@@ -61,8 +61,8 @@ struct ui_backend {
int (*command_names)(void *ctx, char (*names)[64], int max);
/* the sessions in this backend's directory; cb per entry */
int (*sessions)(void *ctx,
void (*cb)(void *cbctx, const char *id, int current,
long mtime), void *cbctx);
void (*cb)(void *cbctx, const char *id, const char *title,
int current, long mtime), void *cbctx);
/* switch to another session; the backend replaces its engine */
int (*open_session)(void *ctx, const char *id, struct sp_buf *err);
/* the transcript so far, one callback per message (no system prompt) */
+4 -3
View File
@@ -114,8 +114,8 @@ static int loc_agent_switch(void *ctx, int idx)
}
static int
loc_sessions(void *ctx, void (*cb)(void *, const char *, int, long),
void *cbctx)
loc_sessions(void *ctx, void (*cb)(void *, const char *, const char *, int,
long), void *cbctx)
{
struct sp_session_brief v[100];
const char *dir = sp_engine_state_dir(ENG(ctx));
@@ -126,7 +126,8 @@ loc_sessions(void *ctx, void (*cb)(void *, const char *, int, long),
if (n < 0)
return 0;
for (i = 0; i < n; i++)
cb(cbctx, v[i].id, strcmp(v[i].id, cur) == 0, v[i].mtime);
cb(cbctx, v[i].id, v[i].title,
strcmp(v[i].id, cur) == 0, v[i].mtime);
return n;
}
+9 -4
View File
@@ -46,6 +46,7 @@ struct rem_item {
struct rem_sess {
char id[64];
char title[96];
long mtime;
int current;
};
@@ -359,11 +360,15 @@ dispatch(struct rem *r, const char *line)
for (i = 0; i < n && r->nsess < 100; i++) {
const mcp_json *it = mcp_array_at(items, i);
const char *id = mcp_arg_string(it, "id");
const char *title = mcp_arg_string(it, "title");
if (id == NULL)
continue;
snprintf(r->sess[r->nsess].id,
sizeof(r->sess[0].id), "%s", id);
snprintf(r->sess[r->nsess].title,
sizeof(r->sess[0].title), "%s",
title != NULL ? title : "");
r->sess[r->nsess].mtime = mcp_arg_int(it, "mtime", 0);
r->sess[r->nsess].current =
mcp_arg_bool(it, "current", 0);
@@ -722,8 +727,8 @@ static int rem_take_steer(void *ctx, struct sp_buf *out)
}
static int
rem_sessions(void *ctx, void (*cb)(void *, const char *, int, long),
void *cbctx)
rem_sessions(void *ctx, void (*cb)(void *, const char *, const char *, int,
long), void *cbctx)
{
struct rem *r = ctx;
long deadline;
@@ -741,8 +746,8 @@ rem_sessions(void *ctx, void (*cb)(void *, const char *, int, long),
(void)dispatch(r, r->line.p);
}
for (i = 0; i < r->nsess; i++)
cb(cbctx, r->sess[i].id, r->sess[i].current,
r->sess[i].mtime);
cb(cbctx, r->sess[i].id, r->sess[i].title,
r->sess[i].current, r->sess[i].mtime);
return r->nsess;
}
+44 -1
View File
@@ -152,14 +152,35 @@ while True:
print("long_prompt_ok" if ev.get("ok") else "long_prompt_failed")
break
# ---- sessions: list, create, switch back, a missing id is an error ----
# ---- sessions: list, titles, rename, delete, switch back, errors ----
def command(text):
f.write(json.dumps({"cmd": "input", "line": text}) + "\n")
f.flush()
while True:
ev = json.loads(f.readline())
if ev.get("ev") == "reply":
return ev.get("text", "")
f.write(json.dumps({"cmd": "sessions"}) + "\n")
f.flush()
while True:
ev = json.loads(f.readline())
if ev.get("ev") == "sessions":
cur = [i.get("id") for i in ev.get("items", []) if i.get("current")]
titles = {i.get("id"): i.get("title") for i in ev.get("items", [])}
print("sessions_ok" if cur == [sessA] else "sessions_bad")
print("title_ok" if titles.get(sessA) == "hallo" else "title_bad")
break
reply = command("/rename Feiner Titel")
print("rename_ok" if "Feiner Titel" in reply else "rename_bad")
f.write(json.dumps({"cmd": "sessions"}) + "\n")
f.flush()
while True:
ev = json.loads(f.readline())
if ev.get("ev") == "sessions":
titles = {i.get("id"): i.get("title") for i in ev.get("items", [])}
print("rename_listed" if titles.get(sessA) == "Feiner Titel" else
"rename_not_listed")
break
f.write(json.dumps({"cmd": "open", "session": "new"}) + "\n")
f.flush()
@@ -208,6 +229,21 @@ while True:
if ev.get("ev") == "state":
print("still_a" if ev.get("session") == sessA else "not_a")
break
reply = command("/rm does-not-exist")
print("rm_missing_ok" if "No such session" in reply else "rm_missing_bad")
reply = command("/rm " + sessB)
print("rm_ok" if sessB in reply and "removed" in reply else "rm_bad")
f.write(json.dumps({"cmd": "sessions"}) + "\n")
f.flush()
while True:
ev = json.loads(f.readline())
if ev.get("ev") == "sessions":
ids = [i.get("id") for i in ev.get("items", [])]
print("rm_gone_ok" if sessB not in ids and sessA in ids else
"rm_gone_bad")
break
reply = command("/rm " + sessA)
print("rm_current_ok" if "Cannot remove" in reply else "rm_current_bad")
PY
checkgrep 'ready' "$D/events.out"
checkgrep 'state' "$D/events.out"
@@ -226,6 +262,13 @@ checkgrep 'open_back_ok' "$D/events.out"
checkgrep 'history_restored' "$D/events.out"
checkgrep 'open_missing$' "$D/events.out"
checkgrep 'still_a' "$D/events.out"
checkgrep 'title_ok' "$D/events.out"
checkgrep 'rename_ok' "$D/events.out"
checkgrep 'rename_listed' "$D/events.out"
checkgrep 'rm_missing_ok' "$D/events.out"
checkgrep 'rm_ok' "$D/events.out"
checkgrep 'rm_gone_ok' "$D/events.out"
checkgrep 'rm_current_ok' "$D/events.out"
checkgrep 'answer fertig' "$D/events.out"
check grep -q 'vom gateway' "$D/out.txt"
+82
View File
@@ -1,7 +1,9 @@
/* splinter -- append-only session tests. */
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include "splinter.h"
@@ -73,10 +75,90 @@ main(void)
CHECK(sp_session_count(&s, &n, &err) == 0);
CHECK(n == 2);
/* the first user line is the title, and it survives reopening */
{
struct sp_session_brief v[4];
char mpath[700];
FILE *m;
char buf[512];
size_t got;
snprintf(mpath, sizeof(mpath), "%s/s1.meta", dir);
m = fopen(mpath, "r");
CHECK(m != NULL);
if (m != NULL) {
got = fread(buf, 1, sizeof(buf) - 1, m);
buf[got] = '\0';
CHECK(strstr(buf, "title=hi \"there\"") != NULL);
fclose(m);
}
CHECK(sp_session_list(dir, v, 4) == 1);
CHECK(strcmp(v[0].title, "hi \"there\"") == 0);
}
/* an explicit title wins and is not lost when the session is opened */
CHECK(sp_session_set_title(dir, "s1", "Ein Titel", &err) == 0);
CHECK(sp_session_open(&s, dir, "s1", &err) == 0);
{
struct sp_session_brief v[4];
CHECK(sp_session_list(dir, v, 4) == 1);
CHECK(strcmp(v[0].title, "Ein Titel") == 0);
}
/* reopening an existing session appends, it never truncates */
CHECK(sp_session_open(&s, dir, "s1", &err) == 0);
CHECK(sp_session_append(&s, "user", "again", &err) == 0);
/* removal takes the log, sidecars and the per-session log directory */
{
char lpath[700], lfile[760];
FILE *lf;
snprintf(lpath, sizeof(lpath), "%s/logs", dir);
CHECK(mkdir(lpath, 0700) == 0 || errno == EEXIST);
snprintf(lpath, sizeof(lpath), "%s/logs/s1", dir);
CHECK(mkdir(lpath, 0700) == 0);
snprintf(lfile, sizeof(lfile), "%s/session.log", lpath);
lf = fopen(lfile, "w");
CHECK(lf != NULL);
if (lf != NULL) {
fputs("x\n", lf);
fclose(lf);
}
snprintf(path, sizeof(path), "%s/s1.jsonl", dir);
CHECK(sp_session_delete(dir, "s1", &err) == 0);
CHECK(access(path, F_OK) != 0);
CHECK(access(lpath, F_OK) != 0);
{
struct sp_session_brief v[4];
CHECK(sp_session_list(dir, v, 4) == 0);
}
CHECK(sp_session_delete(dir, "s1", &err) == -1);
}
/*
* A session from before titles existed has no title in its meta (or no
* meta at all): the listing reads the first user line itself.
*/
{
struct sp_session_brief v[4];
FILE *of;
snprintf(path, sizeof(path), "%s/old.jsonl", dir);
of = fopen(path, "w");
CHECK(of != NULL);
if (of != NULL) {
fputs("{\"role\":\"user\",\"text\":\"alte frage\"}\n",
of);
fclose(of);
}
CHECK(sp_session_list(dir, v, 4) == 1);
CHECK(strcmp(v[0].title, "alte frage") == 0);
CHECK(sp_session_delete(dir, "old", &err) == 0);
}
sp_buf_free(&err);
printf("test_session: ok (%s)\n", dir);
return failures == 0 ? 0 : 1;
+11
View File
@@ -172,15 +172,26 @@ check shows '<m> ok'
old=$(basename "$(ls "$D/home/.splinter/"*.jsonl | head -1)" .jsonl)
typed '/sessions'; key Enter
check shows "$old"
check shows 'single word ok' # the first user line is the title
typed '/rename Lokale Sitzung'; key Enter
check shows 'Lokale Sitzung'
typed '/sessions'; key Enter
check shows 'Lokale Sitzung'
typed '/open new'; key Enter
check shows 'session opened'
sleep 1
check not_shows '<m> ok'
typed 'single word ok'; key Enter
check shows '<m> ok'
new=$(ls "$D/home/.splinter/"*.jsonl | xargs -n1 basename | \
sed 's/\.jsonl$//' | grep -v "^$old$" | head -1)
check test -n "$new"
typed "/open $old"; key Enter
check shows 'session opened'
check shows '<m> ok'
typed "/rm $new"; key Enter
check shows 'removed'
check test ! -e "$D/home/.splinter/$new.jsonl"
check test "$(quit_code)" = 0
if [ "$fails" -eq 0 ]; then echo "test_ui: ok"; exit 0; fi