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:
1 parent
226fff3461
commit
f7120882fa
15 files changed
+1753
-35
No files matched your search
@@ -138,6 +138,12 @@ cps_compact(struct sp_host *h, struct sp_conv *conv, size_t budget,
|
||||
if (sp_conv_bytes(conv) <= budget)
|
||||
return 0; /* fits: no plan needed */
|
||||
copy_snapshot(conv, budget);
|
||||
/*
|
||||
* The worker is making a plan for exactly this conversation. The
|
||||
* core compactor must not cut it in the meantime, or the plan is
|
||||
* invalid before it lands and every round throws it away.
|
||||
*/
|
||||
sp_host_compactor_hold(h);
|
||||
return 0; /* plan is being prepared */
|
||||
}
|
||||
|
||||
|
||||
+9
-2
@@ -186,9 +186,16 @@ lcm_compact(struct sp_host *h, struct sp_conv *conv, size_t budget,
|
||||
* The tail must not start with a tool result: its assistant turn, the
|
||||
* one carrying the tool_call, would be gone, and a provider answers an
|
||||
* orphaned tool message with a 400. Such results go with their call.
|
||||
*
|
||||
* Walk BACK to that assistant, not forward past the results. Forward
|
||||
* was the first try and it silently shrank the tail: a turn with two
|
||||
* results at the boundary kept four messages where keep_tail says six.
|
||||
* Measured 2026-11-21 on system + user + [assistant, tool, tool] x4 +
|
||||
* user: fifteen messages, tail of six, result was a tail of four.
|
||||
*/
|
||||
while (keep_from < conv->n && is_role(&conv->v[keep_from], "tool"))
|
||||
keep_from++;
|
||||
while (keep_from < conv->n && keep_from > first &&
|
||||
is_role(&conv->v[keep_from], "tool"))
|
||||
keep_from--;
|
||||
if (keep_from >= conv->n || keep_from <= first)
|
||||
return 0;
|
||||
/*
|
||||
|
||||
Reference in new issue
Block a user