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
This commit is contained in:
倪程伟 2026-06-07 17:51:08 +08:00 committed by GitHub
parent 39b6bdc522
commit 9baef57871
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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<CronJobRunEntity>()
.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_<wsId>