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

+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;
}