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:
1 parent
f6211362cb
commit
f5d63f6199
5 files changed
+297
-97
No files matched your search
+26
-18
@@ -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;
|
||||
|
||||
+11
-6
@@ -601,7 +601,7 @@ int
|
||||
sp_engine_sources(struct sp_engine *e, struct sp_session_source *v, size_t max)
|
||||
{
|
||||
struct sp_foreign_target ft;
|
||||
char names[4][16];
|
||||
struct sp_foreign_source fs[4];
|
||||
struct sp_session_brief sb[100];
|
||||
size_t i, n = 0;
|
||||
int nf;
|
||||
@@ -617,10 +617,15 @@ sp_engine_sources(struct sp_engine *e, struct sp_session_source *v, size_t max)
|
||||
n++;
|
||||
}
|
||||
foreign_target_of(e, &ft);
|
||||
nf = sp_foreign_sources(&ft, names, sizeof(names) / sizeof(names[0]));
|
||||
nf = sp_foreign_sources(&ft, fs, sizeof(fs) / sizeof(fs[0]));
|
||||
for (i = 0; i < (size_t)nf && n < max; i++, n++) {
|
||||
snprintf(v[n].name, sizeof(v[n].name), "%.15s", names[i]);
|
||||
v[n].count = 0;
|
||||
snprintf(v[n].name, sizeof(v[n].name), "%.15s", fs[i].name);
|
||||
/*
|
||||
* The count is what the picker prints beside the source, so it
|
||||
* has to be the real number: a hard 0 said "claude (0)" next
|
||||
* to 82 rows.
|
||||
*/
|
||||
v[n].count = fs[i].count;
|
||||
}
|
||||
return (int)n;
|
||||
}
|
||||
@@ -634,7 +639,7 @@ struct src_rows {
|
||||
};
|
||||
|
||||
static void
|
||||
src_row(void *cbctx, const char *id, const char *label)
|
||||
src_row(void *cbctx, const char *id, const char *label, long mtime)
|
||||
{
|
||||
struct src_rows *r = cbctx;
|
||||
char prefixed[128];
|
||||
@@ -645,7 +650,7 @@ src_row(void *cbctx, const char *id, const char *label)
|
||||
* and not be found.
|
||||
*/
|
||||
snprintf(prefixed, sizeof(prefixed), "%s:%s", r->source, id);
|
||||
r->cb(r->cbctx, prefixed, label, 0, 0);
|
||||
r->cb(r->cbctx, prefixed, label, 0, mtime);
|
||||
r->n++;
|
||||
}
|
||||
|
||||
|
||||
+131
-66
@@ -17,6 +17,7 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "splinter.h"
|
||||
@@ -36,6 +37,13 @@ tool_of(struct sp_registry *reg, const char *name)
|
||||
return sp_registry_find(reg, name);
|
||||
}
|
||||
|
||||
/*
|
||||
* How many rows one listing asks for. The foreign plugin accepts 1..100 and
|
||||
* silently falls back to its own default (25) for anything else, so asking for
|
||||
* more would *shrink* the answer.
|
||||
*/
|
||||
#define FOREIGN_ROW_MAX 100
|
||||
|
||||
/*
|
||||
* A list row is "DATE SOURCE ID TITLE CWD", fields separated by two
|
||||
* spaces. The id is the third field and the title is free text, so the row is
|
||||
@@ -258,7 +266,11 @@ sp_foreign_open(struct sp_foreign_target *t, const char *source,
|
||||
memcpy(id, idp, idn);
|
||||
id[idn] = '\0';
|
||||
}
|
||||
return sp_foreign_import(t, source, id, err);
|
||||
rc = sp_foreign_import(t, source, id, err);
|
||||
sp_buf_free(&args);
|
||||
sp_buf_free(&out);
|
||||
sp_buf_free(&tmp);
|
||||
return rc;
|
||||
failed:
|
||||
/* keep what the plugin said: it knows why a store was unreadable */
|
||||
if (err->len == 0 && out.p != NULL && out.len > 0)
|
||||
@@ -364,67 +376,15 @@ foreign_list_tool(struct sp_foreign_target *t, const struct sp_tool **tool,
|
||||
}
|
||||
|
||||
/*
|
||||
* Which foreign stores have sessions. The plugin's own listing is the
|
||||
* authority: a source that lists nothing (no store, an unreadable one, a
|
||||
* tracedb that sqlite3 cannot open) is not offered by the picker, and a store
|
||||
* that gained sessions since the last start shows up without a restart.
|
||||
* Ask the plugin for one source's sessions and hand every row to `cb`.
|
||||
* The row format is the plugin's ("DATE SOURCE ID TITLE CWD"), so the
|
||||
* splitting happens here once and both the count and the picker's labels come
|
||||
* out of it. Returns the number of rows, or -1 with `err` filled.
|
||||
*/
|
||||
int
|
||||
sp_foreign_sources(struct sp_foreign_target *t, char (*names)[16], size_t max)
|
||||
{
|
||||
static const char *const src[] = { "claude", "hermes", "opencode" };
|
||||
const struct sp_tool *list;
|
||||
struct sp_buf args, out, tmp;
|
||||
size_t i, n = 0;
|
||||
|
||||
if (max == 0 || foreign_list_tool(t, &list, &tmp) != 0)
|
||||
return 0;
|
||||
if (sp_buf_init(&args) != 0)
|
||||
return 0;
|
||||
if (sp_buf_init(&out) != 0) {
|
||||
sp_buf_free(&args);
|
||||
return 0;
|
||||
}
|
||||
if (sp_buf_init(&tmp) != 0) {
|
||||
sp_buf_free(&args);
|
||||
sp_buf_free(&out);
|
||||
return 0;
|
||||
}
|
||||
for (i = 0; i < sizeof(src) / sizeof(src[0]) && n < max; i++) {
|
||||
const char *p;
|
||||
|
||||
sp_buf_clear(&args);
|
||||
(void)sp_buf_printf(&args, "{\"source\":\"%s\",\"cwd\":\"*\","
|
||||
"\"limit\":1}", src[i]);
|
||||
if (run_tool(list, args.p, &out, &tmp) != 0)
|
||||
continue;
|
||||
if (out.p == NULL || strncmp(out.p, "no foreign sessions", 19) == 0)
|
||||
continue;
|
||||
/* a store that answers with an error is not a source either */
|
||||
p = out.p;
|
||||
if (strchr(p, '\n') == NULL && strchr(p, ' ') == NULL)
|
||||
continue;
|
||||
if (p[0] != '\0' && p[1] != '\0' && p[2] == '-' &&
|
||||
strncmp(p, "error:", 6) == 0)
|
||||
continue;
|
||||
snprintf(names[n], 16, "%s", src[i]);
|
||||
n++;
|
||||
}
|
||||
sp_buf_free(&args);
|
||||
sp_buf_free(&out);
|
||||
sp_buf_free(&tmp);
|
||||
return (int)n;
|
||||
}
|
||||
|
||||
/*
|
||||
* Every session of one foreign source, as the plugin lists it. One row is
|
||||
* "DATE SOURCE ID TITLE CWD"; the picker gets the id and the rest of the
|
||||
* row, so a title with an odd character in it cannot break the id.
|
||||
*/
|
||||
int
|
||||
sp_foreign_sessions(struct sp_foreign_target *t, const char *source,
|
||||
void (*cb)(void *cbctx, const char *id, const char *label), void *cbctx,
|
||||
struct sp_buf *err)
|
||||
static int
|
||||
list_rows(struct sp_foreign_target *t, const char *source,
|
||||
void (*cb)(void *cbctx, const char *id, const char *label, long mtime),
|
||||
void *cbctx, struct sp_buf *err)
|
||||
{
|
||||
const struct sp_tool *list;
|
||||
struct sp_buf args, out, tmp;
|
||||
@@ -445,7 +405,7 @@ sp_foreign_sessions(struct sp_foreign_target *t, const char *source,
|
||||
return -1;
|
||||
}
|
||||
(void)sp_buf_printf(&args, "{\"source\":\"%s\",\"cwd\":\"*\","
|
||||
"\"limit\":100}", source);
|
||||
"\"limit\":%d}", source, FOREIGN_ROW_MAX);
|
||||
if (run_tool(list, args.p, &out, &tmp) != 0 ||
|
||||
out.p == NULL || strncmp(out.p, "error:", 6) == 0) {
|
||||
sp_buf_printf(err, "%s", out.len > 0 && out.p != NULL ?
|
||||
@@ -457,9 +417,10 @@ sp_foreign_sessions(struct sp_foreign_target *t, const char *source,
|
||||
}
|
||||
for (line = strtok_r(out.p, "\n", &save); line != NULL;
|
||||
line = strtok_r(NULL, "\n", &save)) {
|
||||
const char *idp = NULL, *rest;
|
||||
size_t idn;
|
||||
char id[96], label[160];
|
||||
const char *idp = NULL, *datep = NULL, *rest;
|
||||
size_t idn, daten;
|
||||
char id[96], label[160], date[32];
|
||||
long mtime;
|
||||
|
||||
/* the plugin's own notes are not sessions */
|
||||
if (strncmp(line, "error:", 6) == 0 ||
|
||||
@@ -481,7 +442,38 @@ sp_foreign_sessions(struct sp_foreign_target *t, const char *source,
|
||||
while (*rest == ' ')
|
||||
rest++;
|
||||
snprintf(label, sizeof(label), "%.31s %s", id, rest);
|
||||
cb(cbctx, id, label);
|
||||
/*
|
||||
* The row carries the date the session last changed, and the
|
||||
* picker draws by it: a hard 0 would show every foreign row as
|
||||
* 1970-01-01. The plugin prints "?" when it has no date, which
|
||||
* stays 0 and is drawn as such.
|
||||
*/
|
||||
mtime = 0;
|
||||
daten = column(line, 0, &datep);
|
||||
if (daten > 0 && daten < sizeof(date)) {
|
||||
struct tm tm;
|
||||
int y = 0, mo = 0, d = 0, h = 0, mi = 0;
|
||||
|
||||
memcpy(date, datep, daten);
|
||||
date[daten] = '\0';
|
||||
/*
|
||||
* The plugin prints "%Y-%m-%d %H:%M" (stamp_of). Parsed
|
||||
* by hand because strptime is not declared under
|
||||
* _POSIX_C_SOURCE, and the format is fixed anyway.
|
||||
*/
|
||||
if (sscanf(date, "%d-%d-%d %d:%d", &y, &mo, &d, &h,
|
||||
&mi) == 5) {
|
||||
memset(&tm, 0, sizeof(tm));
|
||||
tm.tm_year = y - 1900;
|
||||
tm.tm_mon = mo - 1;
|
||||
tm.tm_mday = d;
|
||||
tm.tm_hour = h;
|
||||
tm.tm_min = mi;
|
||||
tm.tm_isdst = -1;
|
||||
mtime = (long)mktime(&tm);
|
||||
}
|
||||
}
|
||||
cb(cbctx, id, label, mtime);
|
||||
n++;
|
||||
}
|
||||
sp_buf_free(&args);
|
||||
@@ -489,3 +481,76 @@ sp_foreign_sessions(struct sp_foreign_target *t, const char *source,
|
||||
sp_buf_free(&tmp);
|
||||
return n;
|
||||
}
|
||||
|
||||
static void
|
||||
count_row(void *cbctx, const char *id, const char *label, long mtime)
|
||||
{
|
||||
int *n = cbctx;
|
||||
|
||||
(void)id;
|
||||
(void)label;
|
||||
(void)mtime;
|
||||
(*n)++;
|
||||
}
|
||||
|
||||
/*
|
||||
* Which foreign stores have sessions, and how many. The plugin's own listing
|
||||
* is the authority: a source that lists nothing (no store, an unreadable one,
|
||||
* a tracedb that sqlite3 cannot open) is not offered by the picker, and a store
|
||||
* that gained sessions since the last start shows up without a restart.
|
||||
*/
|
||||
int
|
||||
sp_foreign_sources(struct sp_foreign_target *t, struct sp_foreign_source *v,
|
||||
size_t max)
|
||||
{
|
||||
static const char *const src[] = { "claude", "hermes", "opencode" };
|
||||
struct sp_buf err;
|
||||
size_t i, n = 0;
|
||||
|
||||
if (v == NULL || max == 0)
|
||||
return 0;
|
||||
if (sp_buf_init(&err) != 0)
|
||||
return 0;
|
||||
for (i = 0; i < sizeof(src) / sizeof(src[0]) && n < max; i++) {
|
||||
int rows = 0;
|
||||
int rc;
|
||||
|
||||
/*
|
||||
* The count comes from the same listing the picker will show,
|
||||
* so "claude (82)" and the 82 rows behind it cannot disagree.
|
||||
*/
|
||||
rc = list_rows(t, src[i], count_row, &rows, &err);
|
||||
if (rc < 0 || rows == 0) {
|
||||
sp_buf_clear(&err);
|
||||
continue;
|
||||
}
|
||||
snprintf(v[n].name, sizeof(v[n].name), "%s", src[i]);
|
||||
v[n].count = rows;
|
||||
n++;
|
||||
}
|
||||
sp_buf_free(&err);
|
||||
return (int)n;
|
||||
}
|
||||
|
||||
/*
|
||||
* Every session of one foreign source, as the plugin lists it. One row is
|
||||
* "DATE SOURCE ID TITLE CWD"; the picker gets the id and the rest of the
|
||||
* row, so a title with an odd character in it cannot break the id.
|
||||
*/
|
||||
int
|
||||
sp_foreign_sessions(struct sp_foreign_target *t, const char *source,
|
||||
void (*cb)(void *cbctx, const char *id, const char *label, long mtime),
|
||||
void *cbctx, struct sp_buf *err)
|
||||
{
|
||||
struct sp_buf e;
|
||||
int n;
|
||||
|
||||
if (sp_buf_init(&e) != 0)
|
||||
return -1;
|
||||
n = list_rows(t, source, cb, cbctx, &e);
|
||||
if (n < 0 && err != NULL)
|
||||
sp_buf_printf(err, "%s", e.len > 0 && e.p != NULL ?
|
||||
e.p : "the foreign list failed");
|
||||
sp_buf_free(&e);
|
||||
return n;
|
||||
}
|
||||
@@ -48,20 +48,26 @@ int sp_foreign_import(struct sp_foreign_target *t, const char *source,
|
||||
* foreign plugin's own list tool, so the three formats stay in the plugin and
|
||||
* this file only reads the rows it prints.
|
||||
*
|
||||
* sp_foreign_sources fills up to `max` source names ("claude", "hermes",
|
||||
* "opencode") and returns how many it found; a source whose store is missing
|
||||
* or unreadable is not one of them - the picker offers what is there.
|
||||
* sp_foreign_sources fills up to `max` entries with a name ("claude",
|
||||
* "hermes", "opencode") and the number of sessions it holds, and returns how
|
||||
* many it found; a source whose store is missing or unreadable is not one of
|
||||
* them - the picker offers what is there.
|
||||
*/
|
||||
int sp_foreign_sources(struct sp_foreign_target *t, char (*names)[16],
|
||||
size_t max);
|
||||
struct sp_foreign_source {
|
||||
char name[16];
|
||||
int count;
|
||||
};
|
||||
int sp_foreign_sources(struct sp_foreign_target *t,
|
||||
struct sp_foreign_source *v, size_t max);
|
||||
|
||||
/*
|
||||
* Every session of `source`, newest first, as the plugin lists it. `label` is
|
||||
* the row without the date and source columns: what a picker can show as is.
|
||||
* `mtime` is the row's date as epoch seconds (0 when the plugin printed "?").
|
||||
* Returns the number of rows, or -1 with `err` filled when the store failed.
|
||||
*/
|
||||
int sp_foreign_sessions(struct sp_foreign_target *t, const char *source,
|
||||
void (*cb)(void *cbctx, const char *id, const char *label), void *cbctx,
|
||||
struct sp_buf *err);
|
||||
void (*cb)(void *cbctx, const char *id, const char *label, long mtime),
|
||||
void *cbctx, struct sp_buf *err);
|
||||
|
||||
#endif
|
||||
@@ -29,6 +29,33 @@
|
||||
|
||||
static int failures;
|
||||
|
||||
/*
|
||||
* Poison the stack below this frame. A function that reads an uninitialised
|
||||
* local then sees 0xAA bytes instead of a zeroed page, which turns "undefined
|
||||
* behaviour that happens to work" into a crash the test can catch.
|
||||
*/
|
||||
static void
|
||||
dirty_stack(int depth)
|
||||
{
|
||||
volatile char junk[512];
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < sizeof(junk); i++)
|
||||
junk[i] = (char)0xAA;
|
||||
if (depth > 0)
|
||||
dirty_stack(depth - 1);
|
||||
}
|
||||
|
||||
/* a row callback that does nothing: only the refusal is being checked */
|
||||
static void
|
||||
noop_row(void *ctx, const char *id, const char *label, long mtime)
|
||||
{
|
||||
(void)ctx;
|
||||
(void)id;
|
||||
(void)label;
|
||||
(void)mtime;
|
||||
}
|
||||
|
||||
#define CHECK(cond) do { \
|
||||
if (!(cond)) { \
|
||||
fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, \
|
||||
@@ -107,6 +134,28 @@ claude_session(const char *home, const char *cwd, const char *uuid,
|
||||
sp_buf_free(&j);
|
||||
}
|
||||
|
||||
|
||||
/* the rows one listing yields: id, label and the date the picker draws */
|
||||
struct seen_rows {
|
||||
int n;
|
||||
long mtime;
|
||||
char id[96];
|
||||
char label[192];
|
||||
};
|
||||
|
||||
static void
|
||||
seen_row(void *ctx, const char *id, const char *label, long mtime)
|
||||
{
|
||||
struct seen_rows *r = ctx;
|
||||
|
||||
if (r->n == 0) {
|
||||
snprintf(r->id, sizeof(r->id), "%s", id);
|
||||
snprintf(r->label, sizeof(r->label), "%s", label);
|
||||
r->mtime = mtime;
|
||||
}
|
||||
r->n++;
|
||||
}
|
||||
|
||||
/* how many sessions with this id prefix the directory holds */
|
||||
static int
|
||||
sessions_named(const char *dir, const char *prefix)
|
||||
@@ -310,6 +359,73 @@ main(void)
|
||||
CHECK(sp_session_remove_if_empty(sessions, "sess-placeholder") != 0);
|
||||
CHECK(session_exists(sessions, "sess-placeholder") == 1);
|
||||
|
||||
/*
|
||||
* --- without the plugin the picker's source list is empty, not a crash ---
|
||||
*
|
||||
* sp_foreign_sources() used to hand an *uninitialised* sp_buf to the
|
||||
* helper that reports why the list tool is missing, so the reason was
|
||||
* written through a garbage pointer. A registry with nothing registered
|
||||
* reproduces that in one call.
|
||||
*/
|
||||
{
|
||||
struct sp_registry empty;
|
||||
struct sp_foreign_target bare;
|
||||
struct sp_foreign_source fs[4];
|
||||
struct sp_buf serr;
|
||||
|
||||
memset(&empty, 0, sizeof(empty));
|
||||
dirty_stack(8);
|
||||
sp_buf_init(&serr);
|
||||
bare.sess = &sess;
|
||||
bare.reg = ∅
|
||||
bare.host = host;
|
||||
bare.emb = NULL;
|
||||
CHECK(sp_foreign_sources(&bare, fs, 4) == 0);
|
||||
CHECK(sp_foreign_sessions(&bare, "claude", noop_row, NULL,
|
||||
&serr) != 0);
|
||||
CHECK(contains(serr.p, "foreign plugin is not loaded"));
|
||||
sp_buf_free(&serr);
|
||||
/* a target with no registry at all is refused the same way */
|
||||
bare.reg = NULL;
|
||||
CHECK(sp_foreign_sources(&bare, fs, 4) == 0);
|
||||
}
|
||||
|
||||
/*
|
||||
* --- the picker's source list: a real count and a real date ---
|
||||
*
|
||||
* sp_foreign_sources() used to hard-set count to 0, so the picker said
|
||||
* "claude (0)" next to 82 rows, and sp_foreign_sessions() hard-set the
|
||||
* date to 0, so every foreign row drew as 1970-01-01. Both are read
|
||||
* from the same listing now; the fixture above has exactly one claude
|
||||
* session in this kit's store.
|
||||
*/
|
||||
{
|
||||
struct sp_foreign_source fs[4];
|
||||
struct seen_rows rows;
|
||||
struct sp_buf serr;
|
||||
int ns, i, claude = -1;
|
||||
|
||||
ns = sp_foreign_sources(&ft, fs, 4);
|
||||
CHECK(ns >= 1);
|
||||
for (i = 0; i < ns; i++)
|
||||
if (strcmp(fs[i].name, "claude") == 0)
|
||||
claude = i;
|
||||
CHECK(claude >= 0);
|
||||
if (claude >= 0)
|
||||
CHECK(fs[claude].count == 1);
|
||||
|
||||
memset(&rows, 0, sizeof(rows));
|
||||
sp_buf_init(&serr);
|
||||
CHECK(sp_foreign_sessions(&ft, "claude", seen_row, &rows,
|
||||
&serr) == 1);
|
||||
CHECK(rows.n == 1);
|
||||
CHECK(strcmp(rows.id,
|
||||
"11111111-2222-3333-4444-555555555555") == 0);
|
||||
/* the store was written now, so the date is today, not 1970 */
|
||||
CHECK(rows.mtime > 1600000000L);
|
||||
sp_buf_free(&serr);
|
||||
}
|
||||
|
||||
sp_buf_free(&err);
|
||||
sp_buf_free(&out);
|
||||
sp_host_free(host);
|
||||
|
||||
Reference in new issue
Block a user