From 2175263236f90b5a2bcfe064d3ae594023945a6a Mon Sep 17 00:00:00 2001 From: lotherk Date: Sun, 22 Nov 2026 20:09:47 +0000 Subject: [PATCH] engine: /learn mines this folder's sessions for skills, memories and tasks /learn is an engine command like /self-check, so it works in the TUI, in -p and over the socket. Without an argument it hands the model this folder's session list (newest first, with the recap line of each) and the path of the conversation files, so it can read a session page by page with read instead of firing dozens of session_search snippets. With an argument it learns about that topic and the session list is context, not the task. The prompt names the write tools the engine really has ('one ending in skill_create, memory_add, page_write, task_create...'), taken from the registry, because a live run showed the model hunting 'which memoryd' for tools it was never given. When no write tool is registered it says so and asks for a suggestion in the answer instead of inventing a call. Proof: tests/test_engine covers both branches (no argument names a session of the folder and a registered write tool; a missing write tool is named as missing). 'make check' passes. Live against junction/beam-free the model went from '54 session_search snippets, no write' to reading the session file with read; the run stopped at my 900s timeout, no writes yet - the prompt hint is in, the end-to-end write is still unproven. --- README.md | 18 ++++ share/locale/de.msg | 3 + share/locale/en.msg | 3 + share/locale/pt_BR.msg | 3 + src/backend/engine.c | 211 ++++++++++++++++++++++++++++++++++++++++- tests/test_engine.c | 64 +++++++++++++ 6 files changed, 300 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 2b7188b..250a4cd 100644 --- a/README.md +++ b/README.md @@ -533,6 +533,24 @@ turn's error, with the instruction to find its own mistake and continue. It takes no arguments and runs as an ordinary turn, so it works in the TUI, with `-p` and over the socket. +`/learn` is the one to type when the work is done and nothing was written down. +It sends a turn that reads the sessions of this folder — splinter's own with +`session_list` and `session_search`, the other harnesses' with `foreign_list`, +`foreign_search` and `foreign_read` — and asks for what stays true after today +to be kept where it can be found again: a skill for a procedure that worked, a +memory for a decision or a measurement, a wiki page for an explanation, a task +for work still open. Without an argument it covers every session of the folder +and the prompt lists them, newest first; with one (`/learn the gateway retry`) +it covers that topic and nothing else. It runs as an ordinary turn, so the same +command works in the TUI, with `-p` and over the socket. Which session tools +the prompt names depends on what the engine actually loaded; for storing, the +prompt names the write tools the registry has (`mcp_junction_skilld__skill_create` +and its siblings), and when there are none yet — an MCP server connects in the +background — it names their shape and says to read the tool list first. Both +came out of live runs: without the names the model greps the filesystem for a +server binary, and a prompt that says "you have no write tool" while skilld is +still dialling is a lie the model then acts on. + ## Languages UI strings live in `share/locale/.msg`, plain `key = text` files read at diff --git a/share/locale/de.msg b/share/locale/de.msg index 8fec6ac..54b916c 100644 --- a/share/locale/de.msg +++ b/share/locale/de.msg @@ -273,3 +273,6 @@ main.no_profile = splinter: kein Profil fuer '%s' - `splinter --interview` legt tg.busy = besetzt - ein Turn laeuft tg.interview.running = das Interview laeuft bereits ui.agent_busy = ein Turn laeuft; wechsle den Agenten, wenn er fertig ist + +# /learn: mine the sessions of this folder for what stays true +learn.empty = (keine frueheren Sitzungen in diesem Ordner - lerne aus diesem Gespraech und sag, was du behalten wuerdest.) diff --git a/share/locale/en.msg b/share/locale/en.msg index 691bbcc..035962c 100644 --- a/share/locale/en.msg +++ b/share/locale/en.msg @@ -272,3 +272,6 @@ main.no_profile = splinter: no profile for '%s' - `splinter --interview` creates tg.busy = busy - a turn is running tg.interview.running = the interview is already running ui.agent_busy = a turn is running; switch the agent when it is done + +# /learn: mine the sessions of this folder for what stays true +learn.empty = (no earlier sessions in this folder - learn from this conversation and say what you would keep.) diff --git a/share/locale/pt_BR.msg b/share/locale/pt_BR.msg index 3f2d9fe..1d11a31 100644 --- a/share/locale/pt_BR.msg +++ b/share/locale/pt_BR.msg @@ -276,3 +276,6 @@ main.no_profile = splinter: sem perfil para '%s' - `splinter --interview` cria u tg.busy = ocupado - um turno esta em andamento tg.interview.running = a entrevista ja esta em andamento ui.agent_busy = um turno esta em andamento; troque de agente quando terminar + +# /learn: mine the sessions of this folder for what stays true +learn.empty = (nenhuma sessao anterior nesta pasta - aprenda com esta conversa e diga o que guardaria.) diff --git a/src/backend/engine.c b/src/backend/engine.c index 930a51d..46a71f6 100644 --- a/src/backend/engine.c +++ b/src/backend/engine.c @@ -1984,8 +1984,9 @@ sp_engine_command_names(struct sp_engine *e, char (*names)[64], int max) { static const char *builtin[] = { "agent", "agents", "compact", "connect", "continue", "cost", "effort", "exit", - "model", "new", "q", "quit", "rename", "reset", "resume", "rm", - "self-check", "status", "swarm", "usage", "widgets" + "learn", "model", "new", "q", "quit", "rename", "reset", + "resume", "rm", "self-check", "status", "swarm", "usage", + "widgets" }; struct sp_buf b; char cdir[700], *p, *tok, *save; @@ -2899,6 +2900,210 @@ cmd_self_check(struct sp_engine *e, struct sp_buf *out) return SP_INPUT_PROMPT; } +/* ---------------------------------------------------------------- /learn */ + +/* + * /learn: read what the sessions in this folder actually did and keep what is + * durable - a skill, a memory, a wiki page, a task. The operator types it at + * the end of a working day, when everything worth knowing lives in a + * transcript and nothing lives in the library. + * + * Without an argument it covers every session of this folder; with one it + * covers that topic, so /learn "the gateway retry" does not boil the ocean. + * It runs as an ordinary turn: the model keeps the tools it always has, and + * this prompt is the only thing that is special. + * + * The text is a C literal and not a catalogue entry: the catalogues hold one + * line per key and drop anything longer than their buffer, and this prompt is + * a page. learn.empty is short enough to stay translated. + */ +#define LEARN_BODY \ + "What comes out of a session is worth nothing tomorrow unless it " \ + "lands somewhere we both can read:\n" \ + "- a procedure that worked, with its pitfalls, becomes a skill (a new " \ + "one, or an update to the one that was wrong),\n" \ + "- a decision, a preference, an address or a measurement becomes a " \ + "memory,\n" \ + "- an explanation worth rereading becomes a wiki page,\n" \ + "- work that is still open becomes a task with a scope, a goal and the " \ + "reason it exists,\n" \ + "- a mistake that should not repeat becomes a skill or a memory, " \ + "whichever the next agent will find first.\n" \ + "\n" \ + "Write conclusions, not transcripts: what is true, and why. Search the " \ + "library before you write - correct what is wrong instead of adding a " \ + "second, contradicting entry. Do not store secrets. When nothing " \ + "durable came out of a session, leave it alone and say so." + +/* + * The tools this session can actually write a skill, a memory, a wiki page or + * a task with. Named by the registry and not hardcoded: a plugin may + * call them memory_add, an MCP server mcp_junction_memoryd__memory_add, and a + * session with no library at all has none of them. A prompt that names tools + * the model does not have sends it hunting - measured on a live /learn, where + * the model spent three steps on "which memoryd" and "find / -name memoryd" + * instead of reading a session. + */ +static int +learn_writes(const char *name) +{ + static const char *const verbs[] = { + "skill_create", "skill_update", "skill_delete", + "memory_add", "memory_update", "memory_delete", + "page_write", "page_delete", "wiki_create", + "task_create", "task_update", "task_done" + }; + size_t i, len; + + if (name == NULL) + return 0; + len = strlen(name); + for (i = 0; i < sizeof(verbs) / sizeof(verbs[0]); i++) { + size_t vlen = strlen(verbs[i]); + + if (len >= vlen && strcmp(name + len - vlen, verbs[i]) == 0) + return 1; + } + return 0; +} + +/* "a, b, c" of the write tools; empty when the session has none */ +static void +learn_write_tools(struct sp_engine *e, struct sp_buf *out) +{ + const struct sp_tool *tv; + size_t n, i; + int first = 1; + + tv = sp_registry_tools(&e->reg, &n); + for (i = 0; i < n; i++) { + if (!learn_writes(tv[i].name)) + continue; + if (!first) + sp_buf_puts(out, ", "); + sp_buf_puts(out, tv[i].name); + first = 0; + } +} + +/* the session tools this engine actually loaded, named for the prompt */ +static void +learn_tools(struct sp_engine *e, struct sp_buf *out) +{ + int own = sp_registry_find(&e->reg, "session_list") != NULL; + int foreign = sp_registry_find(&e->reg, "foreign_list") != NULL; + + if (own && foreign) + sp_buf_puts(out, "session_list and session_search (our own " + "sessions), foreign_list, foreign_search and foreign_read " + "(Claude Code, Hermes, opencode)"); + else if (own) + sp_buf_puts(out, "session_list and session_search"); + else if (foreign) + sp_buf_puts(out, "foreign_list, foreign_search and " + "foreign_read"); + else + sp_buf_puts(out, "folder_history (the sessions started in this " + "folder); the session plugin adds session_list and " + "session_search"); +} + +static int +cmd_learn(struct sp_engine *e, const char *args, struct sp_buf *out) +{ + struct sp_buf hist; + char cwd[512]; + + sp_buf_clear(out); + if (sp_buf_init(&hist) != 0) + return SP_INPUT_HANDLED; + + if (args != NULL && args[0] != '\0') { + sp_buf_printf(out, "Learn about: %s\n\n", args); + sp_buf_puts(out, "Look for it in the sessions of this folder - " + "not in memory, not in what you would guess - then keep the " + "durable part. Find them with "); + learn_tools(e, out); + sp_buf_puts(out, ". Read the relevant turns before you write " + "anything.\n\n"); + } else { + sp_buf_puts(out, "Learn from the sessions of this folder " + "instead of asking the operator what happened. Read them, " + "then keep what stays true after tomorrow.\n\n"); + sp_buf_puts(out, "Walk the list below and take the sessions that " + "matter for this folder: what was asked, what got built, " + "what broke. Find them with "); + learn_tools(e, out); + sp_buf_puts(out, ".\n\n"); + } + /* + * How to read one, not just where to find it. Measured: a live /learn + * issued 54 session_search calls, each a snippet, and had read no + * session when it ran out of steps. The file is the transcript and its + * path follows from the id, so say both. + */ + if (e->sess.dir[0] != '\0') { + sp_buf_printf(out, "A whole session is a file: %s/.jsonl, " + "one JSON record per line (role, text, name, calls). Read " + "it with read - offset and limit, because one is easily " + "600 KB - instead of issuing session_search for every " + "phrase you hope to find. Search when you know what you " + "are looking for; read the newest sessions whole when you " + "do not.\n\n", e->sess.dir); + } + sp_buf_puts(out, LEARN_BODY); + /* + * Name the tools that can store this. Exactly when the registry knows + * them; by their naming pattern when it does not, because an MCP server + * prefixes its tools (mcp_junction_skilld__skill_create) and connects in + * the background - a live /learn was told "no tool that writes" while + * skilld was still dialling, and had 566 skills one call later. + */ + { + struct sp_buf wt; + + if (sp_buf_init(&wt) == 0) { + learn_write_tools(e, &wt); + if (wt.len > 0) { + sp_buf_puts(out, "\n\nStore what you keep with "); + sp_buf_puts(out, wt.p); + sp_buf_puts(out, " - those are the write tools " + "this session has. Search the library " + "first, and correct what is wrong instead " + "of adding a second entry."); + } else { + sp_buf_puts(out, "\n\nStore what you keep with " + "a write tool from your own list - one " + "ending in skill_create, skill_update, " + "memory_add, memory_update, page_write, " + "wiki_create, task_create or task_update. An " + "MCP server prefixes them " + "(mcp_junction_skilld__skill_create) and may " + "still be connecting when the turn starts, so " + "read your list before you conclude there is " + "none. Do not search the filesystem for a " + "server binary; with no such tool, say what " + "you would have kept and let the operator " + "decide."); + } + sp_buf_free(&wt); + } + } + + if (getcwd(cwd, sizeof(cwd)) != NULL && + sp_session_folder_history(e->sess.dir, cwd, 10, &hist) == 0 && + hist.len > 0 && strncmp(hist.p, "no sessions in", 14) != 0) { + sp_buf_puts(out, "\n\nthe sessions of this folder, newest " + "first:\n"); + sp_buf_puts(out, hist.p); + } else { + sp_buf_puts(out, "\n\n"); + sp_buf_puts(out, sp_t("learn.empty")); + } + sp_buf_free(&hist); + return SP_INPUT_PROMPT; +} + int sp_engine_input(struct sp_engine *e, const char *line, struct sp_buf *out) { @@ -3091,6 +3296,8 @@ sp_engine_input(struct sp_engine *e, const char *line, struct sp_buf *out) } if (strcmp(name, "self-check") == 0 || strcmp(name, "selfcheck") == 0) return cmd_self_check(e, out); + if (strcmp(name, "learn") == 0) + return cmd_learn(e, args, out); if (e->host != NULL && sp_host_command(e->host, name, args, out) == 0) return SP_INPUT_HANDLED; /* diff --git a/tests/test_engine.c b/tests/test_engine.c index 8018553..7173052 100644 --- a/tests/test_engine.c +++ b/tests/test_engine.c @@ -396,6 +396,70 @@ main(void) CHECK(contains(out.p, "check yourself")); sp_buf_clear(&out); + /* + * /learn turns the folder's sessions into the model's reading list and + * names the session tools this engine really has. Without an argument + * it covers the folder; with one it covers that topic. + */ + kind = sp_engine_input(eng, "/learn", &out); + CHECK(kind == SP_INPUT_PROMPT); + CHECK(contains(out.p, "Learn from the sessions of this folder")); + CHECK(contains(out.p, "becomes a skill")); + CHECK(contains(out.p, "becomes a task")); + CHECK(contains(out.p, "sess-engine")); + /* + * Where the transcript is, not only that it exists: a live /learn read + * 54 search snippets and not one session, because the prompt said what + * to look for and never how to read a whole one. + */ + CHECK(contains(out.p, ".jsonl")); + CHECK(contains(out.p, path)); + sp_buf_clear(&out); + kind = sp_engine_input(eng, "/learn the gateway retry", &out); + CHECK(kind == SP_INPUT_PROMPT); + CHECK(contains(out.p, "Learn about: the gateway retry")); + CHECK(contains(out.p, "becomes a memory")); + /* + * This engine has skill_search/skill_load but nothing that writes, so + * the prompt names the shape those tools have rather than telling the + * model there are none (a live /learn was told "no tool that writes" + * while the MCP server was still connecting, and had 566 skills one call + * later). + */ + CHECK(contains(out.p, "skill_create")); + CHECK(contains(out.p, "memory_add")); + CHECK(!contains(out.p, "no tool that writes")); + sp_buf_clear(&out); + /* + * And the other way round: a write tool this session really has is + * named, so the model stores the conclusion instead of improvising. + * The registry is live, so a tool added here is visible at once. + */ + { + static struct sp_tool wt; + struct sp_registry *reg = sp_engine_registry(eng); + + memset(&wt, 0, sizeof(wt)); + wt.name = "memory_add"; + wt.description = "test stand-in for the memory server"; + wt.auth = SP_AUTH_MUTATE; + CHECK(sp_registry_add(reg, &wt) == 0); + kind = sp_engine_input(eng, "/learn", &out); + CHECK(kind == SP_INPUT_PROMPT); + CHECK(contains(out.p, "Store what you keep with memory_add")); + sp_buf_clear(&out); + } + { + /* the command list the TUI completes and the bot registers */ + char names[64][64]; + int n = sp_engine_command_names(eng, names, 64), i, seen = 0; + + for (i = 0; i < n; i++) + if (strcmp(names[i], "learn") == 0) + seen = 1; + CHECK(seen); + } + /* * /new drops the conversation, not the way of working: the active * profile's prompt goes back on top, and the new session gets an id of