From 9baef5787171e8cf64cad9220dbd14237d575c0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=80=AA=E7=A8=8B=E4=BC=9F?= Date: Sun, 7 Jun 2026 17:51:08 +0800 Subject: [PATCH] fix(cron): persist token usage to the run row on completion (#263) finishRunAndPublish updated the run row with status and finished_at but dropped the LLM token usage, so mate_cron_job_run.token_usage stayed NULL and the scheduler history always showed 0. Write the prompt+completion token total (already computed for the assistant message row) to the run row, guarded so non-LLM paths (reminder direct-push with a null ChatResult) leave the column untouched. Closes #262 --- .../java/vip/mate/cron/service/CronJobLifecycleService.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/mateclaw-server/src/main/java/vip/mate/cron/service/CronJobLifecycleService.java b/mateclaw-server/src/main/java/vip/mate/cron/service/CronJobLifecycleService.java index cfc7f123..fc3c1168 100644 --- a/mateclaw-server/src/main/java/vip/mate/cron/service/CronJobLifecycleService.java +++ b/mateclaw-server/src/main/java/vip/mate/cron/service/CronJobLifecycleService.java @@ -196,10 +196,13 @@ public class CronJobLifecycleService { String convId = conversationId != null ? conversationId : run.getConversationId(); String text = result != null && result.getText() != null ? result.getText() : ""; + int totalTokens = chatResult != null + ? chatResult.promptTokens() + chatResult.completionTokens() : 0; runMapper.update(null, new LambdaUpdateWrapper() .eq(CronJobRunEntity::getId, run.getId()) .set(CronJobRunEntity::getStatus, "succeeded") - .set(CronJobRunEntity::getFinishedAt, LocalDateTime.now())); + .set(CronJobRunEntity::getFinishedAt, LocalDateTime.now()) + .set(totalTokens > 0, CronJobRunEntity::getTokenUsage, totalTokens)); if (silent) { // No-op run: persist a short marker so the tasks_