story-kit: merge 296_story_track_per_agent_token_usage_for_cost_visibility_and_optimisation

This commit is contained in:
Dave
2026-03-19 09:53:32 +00:00
parent 6c413e1fc7
commit 9cdb0d4ea8
5 changed files with 411 additions and 9 deletions

View File

@@ -500,7 +500,24 @@ impl AgentPool {
)
.await
{
Ok(session_id) => {
Ok(pty_result) => {
// Persist token usage if the agent reported it.
if let Some(ref usage) = pty_result.token_usage
&& let Ok(agents) = agents_ref.lock()
&& let Some(agent) = agents.get(&key_clone)
&& let Some(ref pr) = agent.project_root
{
let record = super::token_usage::build_record(
&sid, &aname, usage.clone(),
);
if let Err(e) = super::token_usage::append_record(pr, &record) {
slog_error!(
"[agents] Failed to persist token usage for \
{sid}:{aname}: {e}"
);
}
}
// Server-owned completion: run acceptance gates automatically
// when the agent process exits normally.
run_server_owned_completion(
@@ -508,7 +525,7 @@ impl AgentPool {
port_for_task,
&sid,
&aname,
session_id,
pty_result.session_id,
watcher_tx_clone.clone(),
)
.await;