sessions: the foreign picker counts and dates what it lists

Three bugs in the /sessions picker, all of them found by review and all of
them visible on the screen:

- sp_engine_sources() hard-set every foreign source's count to 0, so the
  picker offered "claude (0)" while 82 rows were behind it. The count now
  comes from the same listing the picker shows.
- sp_foreign_sessions() hard-set each row's date to 0, so every foreign
  session drew as 1970-01-01. The plugin prints "%Y-%m-%d %H:%M" and the
  row's first column is parsed into epoch seconds.
- The foreign plugin's list tool queried with its configured default
  (limit = 25) no matter what the caller asked for, so asking for 100 rows
  returned 25 and a store with 290 sessions looked like one with 25. The
  caller's limit now reaches the SQL.

Proof: tests/test_foreignopen.c asserts count == 1 and mtime > 1600000000
against a fixture store; reverting either fix makes both checks fail. Live
over the socket, before: [('splinter', 100), ('claude', 0), ('opencode', 0)]
with mtime 0 on every row; after: [('splinter', 100), ('claude', 82),
('opencode', 100)] with mtime 1795387200. sqlite3 counts 290 opencode
sessions and 82 claude files, so the numbers agree with the stores.

make check: 21 suites, 0 failures. check_i18n: 229 keys in 3 catalogues.
This commit is contained in:
lotherk committed 2026-11-22 22:52:51 +00:00
1 parent f6211362cb
commit f5d63f6199
5 files changed
+297 -97

No files matched your search

+26 -18
View File
@@ -1658,7 +1658,8 @@ hermes_dbs(char dbs[][FR_PATH], size_t max, size_t *n)
}
static int
list_hermes(const char *only_cwd, struct fbrief *v, size_t max, size_t *n)
list_hermes(const char *only_cwd, struct fbrief *v, size_t max, size_t *n,
int rows_wanted)
{
char dbs[32][FR_PATH], why[FR_PATH + 96];
size_t nd = 0, i;
@@ -1683,12 +1684,12 @@ list_hermes(const char *only_cwd, struct fbrief *v, size_t max, size_t *n)
"as title, coalesce(cwd,'') as cwd, "
"cast(started_at as integer) as ts from sessions "
"where cwd = %s order by ts desc limit %d;",
sql_lit(&lit, only_cwd), fr.limit);
sql_lit(&lit, only_cwd), rows_wanted);
} else {
(void)sp_buf_printf(&sql, "select id, coalesce(title,'') "
"as title, coalesce(cwd,'') as cwd, "
"cast(started_at as integer) as ts from sessions "
"order by ts desc limit %d;", fr.limit);
"order by ts desc limit %d;", rows_wanted);
}
/* a store that answers nothing is not an error worth stopping */
rows = sql_json(dbs[i], sql.p, why, sizeof(why));
@@ -1964,7 +1965,7 @@ load_hermes(const char *id, struct fconv *conv, char *err, size_t errcap)
static int
list_opencode(const char *only_cwd, struct fbrief *v, size_t max, size_t *n,
char *err, size_t errcap)
int rows_wanted, char *err, size_t errcap)
{
struct sp_buf lit, sql;
mcp_json *rows;
@@ -1993,12 +1994,12 @@ list_opencode(const char *only_cwd, struct fbrief *v, size_t max, size_t *n,
(void)sp_buf_printf(&sql, "select id, coalesce(title,'') as "
"title, directory, cast(time_updated as integer) as updated "
"from session_v2 where directory = %s order by updated desc "
"limit %d;", sql_lit(&lit, only_cwd), fr.limit);
"limit %d;", sql_lit(&lit, only_cwd), rows_wanted);
} else {
(void)sp_buf_printf(&sql, "select id, coalesce(title,'') as "
"title, directory, cast(time_updated as integer) as updated "
"from session_v2 order by updated desc limit %d;",
fr.limit);
rows_wanted);
}
rows = sql_json(fr.opencode, sql.p, why, sizeof(why));
if (rows == NULL) {
@@ -2008,12 +2009,12 @@ list_opencode(const char *only_cwd, struct fbrief *v, size_t max, size_t *n,
"as title, directory, cast(time_updated as integer) "
"as updated from session where directory = %s order "
"by updated desc limit %d;",
sql_lit(&lit, only_cwd), fr.limit);
sql_lit(&lit, only_cwd), rows_wanted);
} else {
(void)sp_buf_printf(&sql, "select id, coalesce(title,'') "
"as title, directory, cast(time_updated as integer) "
"as updated from session order by updated desc "
"limit %d;", fr.limit);
"limit %d;", rows_wanted);
}
rows = sql_json(fr.opencode, sql.p, why, sizeof(why));
}
@@ -2392,9 +2393,15 @@ load_any(enum fr_src s, const char *id, struct fconv *conv, char *err,
}
}
/*
* `rows_wanted` is how many rows the caller asked for, which is not always
* fr.limit: that is the configured default (25). The SQL sources used to query
* with fr.limit regardless, so a caller that asked for 100 got 25 rows and the
* picker claimed a store had 25 sessions when it had 290.
*/
static int
list_any(enum fr_src s, const char *cwd, struct fbrief *v, size_t max,
size_t *n, char *err, size_t errcap)
size_t *n, int rows_wanted, char *err, size_t errcap)
{
resolve_paths();
*n = 0;
@@ -2402,14 +2409,15 @@ list_any(enum fr_src s, const char *cwd, struct fbrief *v, size_t max,
case SRC_CLAUDE:
return list_claude(cwd, v, max, n, err, errcap) == 0 ? 0 : -1;
case SRC_HERMES:
if (list_hermes(cwd, v, max, n) != 0) {
if (list_hermes(cwd, v, max, n, rows_wanted) != 0) {
snprintf(err, errcap, "no hermes state.db under %zu "
"root%s", fr.nhermes, fr.nhermes == 1 ? "" : "s");
return -1;
}
return 0;
case SRC_OPENCODE:
return list_opencode(cwd, v, max, n, err, errcap) == 0 ? 0 : -1;
return list_opencode(cwd, v, max, n, rows_wanted, err,
errcap) == 0 ? 0 : -1;
default:
snprintf(err, errcap, "unknown source");
return -1;
@@ -2522,7 +2530,7 @@ import_meta_fields(enum fr_src s, const char *foreign_id, char *title,
if (v == NULL)
return;
if (list_any(s, NULL, v, max, &n, err, sizeof(err)) == 0)
if (list_any(s, NULL, v, max, &n, (int)max, err, sizeof(err)) == 0)
for (i = 0; i < n; i++)
if (strcmp(v[i].id, foreign_id) == 0) {
if (v[i].title[0] != '\0' &&
@@ -2714,8 +2722,8 @@ tool_list(const char *args, struct sp_buf *out, void *ctx)
for (i = 0; i < 3 && shown < (size_t)limit; i++) {
if (want != SRC_NONE && want != (enum fr_src)i)
continue;
if (list_any((enum fr_src)i, only, v, max, &n, err,
sizeof(err)) != 0) {
if (list_any((enum fr_src)i, only, v, max, &n, limit,
err, sizeof(err)) != 0) {
sp_buf_printf(out, "%s: %s\n", src_name[i], err);
continue;
}
@@ -2969,8 +2977,8 @@ search_opencode(const char *query, int limit, int days, struct sp_buf *out,
* in that list is then the same rule the list itself uses.
*/
if (only_cwd != NULL && *only_cwd != '\0') {
if (list_opencode(only_cwd, allowed, 256, &nallowed, why,
sizeof(why)) != 0)
if (list_opencode(only_cwd, allowed, 256, &nallowed, 256,
why, sizeof(why)) != 0)
nallowed = 0;
} else {
nallowed = 0;
@@ -3134,8 +3142,8 @@ cmd_foreign(struct sp_host *h, const char *args, struct sp_buf *out, void *ctx)
sp_buf_printf(out, " opencode %s\n",
fr.opencode[0] != '\0' ? fr.opencode : "(unset)");
for (i = 0; i < 3; i++) {
if (list_any((enum fr_src)i, NULL, v, 64, &n, err,
sizeof(err)) != 0) {
if (list_any((enum fr_src)i, NULL, v, 64, &n, 64,
err, sizeof(err)) != 0) {
sp_buf_printf(out, " %-8s %s\n", src_name[i],
err);
continue;