agent: a step that reports no usage does not empty the ctx meter

The guard was there since 16.11. ("keep the last known prompt size, or the
status bar drops to 0 after every tool call") but nothing tested it, so
t0429 stayed open on the one point that had no evidence.

usage_gap_send reports 1000 prompt tokens on the call that starts the round,
9000 on the next, and nothing at all on the answer. Without the guard
last_in becomes 0 on that answer; with it the meter keeps 9000 and the cache
figure 4000, while tokens_in still sums what was actually reported.

Evidence: make check exit 0 (45 binaries + 20 shell tests), tests/check_asan.sh
all 45 clean. Counter-check: with the old unconditional
`last_in = reply.tokens_in > 0 ? ... : 0`, test_agent fails on
last_in == 9000 and last_cached == 4000.

The other four points of t0429 are covered elsewhere and were verified:
the goal sidecar and /continue in test_session.c and test_engine.c, the
plain sidebar page in test_theme.c (flat light/dark: bg white/black, fg the
opposite, input on the same page), and sysinfo was dropped on purpose in
t0469.
This commit is contained in:
lotherk committed 2026-11-22 15:27:54 +00:00
1 parent d714efb16e
commit 082223af96
1 file changed
+77
+77
View File
@@ -157,6 +157,46 @@ cut_off_send(void *ctx, const struct sp_conv *conv, struct sp_reply *out,
out->calls[0].args != NULL && out->calls_json != NULL ? 0 : -1;
}
/*
* Step 1 reports prompt usage and asks for a tool; step 2 reports the real
* prompt size; step 3 answers without reporting anything. Providers do this (a
* bridge that drops the usage object on the final chunk), and the meter must
* not read that as an empty context.
*/
static int usage_gap_calls;
static int
usage_gap_send(void *ctx, const struct sp_conv *conv, struct sp_reply *out,
struct sp_buf *err)
{
(void)ctx;
(void)conv;
(void)err;
usage_gap_calls++;
memset(out, 0, sizeof(*out));
out->status = 200;
if (usage_gap_calls == 1 || usage_gap_calls == 2) {
out->content = strdup("");
out->tokens_in = usage_gap_calls == 1 ? 1000 : 9000;
out->tokens_out = 20;
out->tokens_cached = usage_gap_calls == 1 ? 0 : 4000;
out->ncalls = 1;
out->calls = calloc(1, sizeof(*out->calls));
if (out->content == NULL || out->calls == NULL)
return -1;
out->calls[0].id = strdup(usage_gap_calls == 1 ?
"call-0" : "call-1");
out->calls[0].name = strdup("echo");
out->calls[0].args = strdup(usage_gap_calls == 1 ?
"{\"text\":\"eins\"}" : "{\"text\":\"zwei\"}");
return out->calls[0].id != NULL &&
out->calls[0].name != NULL && out->calls[0].args != NULL ?
0 : -1;
}
/* silent: no tokens_in, no tokens_out, no cache */
out->content = strdup("done without usage");
return out->content != NULL ? 0 : -1;
}
/* a reply whose tool call carries arguments that are not JSON */
static int bad_args_calls;
static struct sp_buf bad_args_second; /* the second request, verbatim */
@@ -638,6 +678,43 @@ main(void)
(void)unsetenv("SPLINTER_RETRY_WAIT_MS");
}
/*
* A tool-call step that reports no usage does not empty the context:
* zero there is "unknown", not "the prompt became empty". The status
* bar read a bare last_in, so it dropped to 0 after every tool call
* (seen on olivia over Telegram). The last known prompt size has to
* survive the step that did not report one.
*/
{
struct sp_agent a;
struct sp_agent_config cfg;
struct sp_transport tr;
memset(&a, 0, sizeof(a));
memset(&cfg, 0, sizeof(cfg));
memset(&tr, 0, sizeof(tr));
cfg.max_steps = 8;
cfg.autonomous = 1;
tr.send = usage_gap_send;
a.reg = &r;
a.cfg = &cfg;
a.tr = tr;
usage_gap_calls = 0;
sp_buf_clear(&answer);
sp_buf_clear(&err);
CHECK(sp_agent_run(&a, "count the context", &answer, &err) == 0);
CHECK(usage_gap_calls == 3);
/* 9000 from the step that reported, kept through the silent one */
CHECK(a.stats.last_in == 9000);
CHECK(a.stats.last_cached == 4000);
/*
* The sum still grows by what was reported: the guard is about
* the meter, not about the usage total.
*/
CHECK(a.stats.tokens_in == 1000 + 9000);
sp_conv_free(a.conv);
}
/*
* A round that fails between its calls still answers every call:
* an assistant message with two tool_calls and one result made each