sessions: a resumed session is the whole transcript, not a chat log

A session file was written as if it were a chat: role and text, nothing
else. Reading it back kept "user" and "assistant" and dropped everything
else - and nearly every line of an agent transcript is an observation.
Measured on three real sessions: 21 of 1507 messages came back, 8 of 1356,
3 of 317. --resume then sent 1% of the conversation and the model had no
idea what it was continuing.

Three writers had to change with the loader:

  - A turn that asked for tools wrote no line at all (agent.c only wrote
    when ncalls == 0). It now goes out with its tool_calls, and each result
    with the name of its tool and its call id; a provider rejects a tool
    message without them.
  - {"role":"user","text":""} came from the steer path and showed the model
    a blank human turn it answered. sp_session_append_rec writes nothing
    when text and calls are both empty.
  - A record from the old format has no name and no call id. It is paired
    with the next unanswered call of its pending assistant turn, or given a
    synthetic one (recovered-N). Dropping it is what emptied every resume;
    keeping it unpaired is what a provider answers with 400.

The same pass gives the core its own compactor (compact.c): a thread digest
- one line per dropped message, OPERATOR: 800 chars, assistant: 300, tool
result 160 - so the model keeps the story, not a count. lcm stays a plugin
that runs after it; sp_core_compactor() is the fallback only when no plugin
registered one, because running both eats the conversation before lcm sees
work to do.

Two bugs fell out of the same area:

  - The plugin host was only built when a plugin was configured, so a
    plugin-less run had no compactor at all: over max_bytes the turn was
    refused instead of compacted (90 KB session, budget 8000, "nothing
    sent").
  - lcm walked *forward* past tool results at the tail boundary, which
    silently shrank the tail: a turn with two results kept four messages
    where keep_tail says six. Walking back to the assistant that carries
    the call keeps six and no orphan.

And the .meta sidecar is shared now: the core rewrote it with only the keys
it knew, so anything a plugin put there was gone on the next open. It
carries unknown lines through (extra[8][160]), which is what lets a plugin
mark a session in the core's own file.

Tests: tests/test_corecompact.c (10 checks, no plugin), the resume cases in
tests/test_session.c, and the tail-size check in tests/test_compactor.c.
Each was seen red on the old code before it was green on the new.
This commit is contained in:
lotherk committed 2026-11-21 21:25:51 +00:00
1 parent 226fff3461
commit f7120882fa
15 files changed
+1753 -35

No files matched your search

+18
View File
@@ -260,6 +260,24 @@ int sp_session_open(struct sp_session_info *, const char *dir,
const char *id, struct sp_buf *err);
int sp_session_append(const struct sp_session_info *, const char *role,
const char *text, struct sp_buf *err);
/*
* A full record. The short form above writes a role and a text; that was enough
* to read a session back as a chat log and not enough to resume one: a tool
* result without the name of its tool, and an assistant turn without its
* tool_calls, cannot be sent to a provider at all. An empty text writes
* nothing - a record with no content is not a message, it is noise in the file
* that made every resumed session carry blank user turns.
*/
struct sp_sess_rec {
const char *role;
const char *text;
const char *name; /* tool name, when role is "tool" */
const char *call_id; /* tool_call id */
const char *calls; /* raw tool_calls JSON of an assistant turn */
};
int sp_session_append_rec(const struct sp_session_info *,
const struct sp_sess_rec *, struct sp_buf *err);
int sp_session_count(const struct sp_session_info *, size_t *n,
struct sp_buf *err);
int sp_session_latest(const char *dir, char *id, size_t cap);
+20
View File
@@ -150,6 +150,26 @@ int sp_host_note(struct sp_host *, const char *text);
int sp_host_status_msg(struct sp_host *, const char *text);
int sp_host_status_take(struct sp_host *, char *buf, size_t len);
int sp_host_add_compactor(struct sp_host *, const struct sp_compactor *);
/*
* The compactor the core registers itself, before any plugin. It is what
* compact_threshold and compact_target drive; a plugin that compacts too runs
* after it.
*/
const struct sp_compactor *sp_core_compactor(void);
/*
* "Leave this conversation alone for this round."
*
* A compactor that works asynchronously - it copies the conversation, hands it
* to a worker and applies the plan on its next call - cannot survive the core
* cutting the conversation between the snapshot and the apply: the plan is
* made for a conversation that no longer exists, the size check fails, and the
* work is thrown away every round while the conversation keeps growing.
*
* So a plugin in that state calls this before returning 0. The host then keeps
* the core compactor out of this one call and asks again next time, which is
* when the plan lands. The flag lasts one call and is not remembered.
*/
void sp_host_compactor_hold(struct sp_host *);
int sp_host_add_gate(struct sp_host *, const struct sp_gate *);
int sp_host_add_status(struct sp_host *, const struct sp_status *);
const struct sp_meter *sp_host_meter(struct sp_host *);