From 06602ee7fbcbb43c078d8ebb0029399c79424307 Mon Sep 17 00:00:00 2001
From: Junjie <fallin.jie@qq.com>
Date: 星期一, 27 四月 2026 18:15:03 +0800
Subject: [PATCH] fix: audit auto tune agent runs

---
 src/main/java/com/zy/ai/service/impl/AutoTuneCoordinatorServiceImpl.java |   80 ++++++++++++++++++++++++++++++++++++++++
 1 files changed, 80 insertions(+), 0 deletions(-)

diff --git a/src/main/java/com/zy/ai/service/impl/AutoTuneCoordinatorServiceImpl.java b/src/main/java/com/zy/ai/service/impl/AutoTuneCoordinatorServiceImpl.java
index e6420de..fc1078e 100644
--- a/src/main/java/com/zy/ai/service/impl/AutoTuneCoordinatorServiceImpl.java
+++ b/src/main/java/com/zy/ai/service/impl/AutoTuneCoordinatorServiceImpl.java
@@ -5,6 +5,7 @@
 import com.zy.ai.domain.autotune.AutoTuneJobStatus;
 import com.zy.ai.domain.autotune.AutoTuneTriggerType;
 import com.zy.ai.entity.AiAutoTuneJob;
+import com.zy.ai.enums.AiPromptScene;
 import com.zy.ai.service.AiAutoTuneJobService;
 import com.zy.ai.service.AutoTuneAgentService;
 import com.zy.ai.service.AutoTuneCoordinatorService;
@@ -40,6 +41,8 @@
     private static final int MAX_INTERVAL_MINUTES = 60;
     private static final int RUNNING_LOCK_SECONDS = 20 * 60;
     private static final long SYSTEM_USER_ID = 9527L;
+    private static final int SUMMARY_MAX_LENGTH = 512;
+    private static final int ERROR_MESSAGE_MAX_LENGTH = 1024;
     private static final List<Long> FINAL_WRK_STS_LIST = Arrays.asList(
             WrkStsType.COMPLETE_INBOUND.sts,
             WrkStsType.SETTLE_INBOUND.sts,
@@ -91,17 +94,20 @@
         }
 
         AutoTuneAgentService.AutoTuneAgentResult agentResult = null;
+        Date startTime = new Date();
         try {
             if (writeGuard && intervalMinutes != null) {
                 safeMarkLastTriggerGuard(intervalMinutes);
             }
             agentResult = autoTuneAgentService.runAutoTune(triggerType);
             safeWriteOperateLog(agentResult);
+            safeWriteAgentRunAudit(triggerType, startTime, agentResult);
             return AutoTuneCoordinatorResult.triggered(agentResult);
         } catch (Exception exception) {
             log.error("Auto tune coordinator failed to run agent", exception);
             agentResult = failedAgentResult(triggerType, exception);
             safeWriteOperateLog(agentResult);
+            safeWriteAgentRunAudit(triggerType, startTime, agentResult);
             return AutoTuneCoordinatorResult.triggered(agentResult);
         } finally {
             redisUtil.compareAndDelete(lockKey, lockToken);
@@ -198,6 +204,80 @@
         return result;
     }
 
+    private void safeWriteAgentRunAudit(String triggerType,
+                                        Date startTime,
+                                        AutoTuneAgentService.AutoTuneAgentResult agentResult) {
+        try {
+            writeAgentRunAudit(triggerType, startTime, agentResult);
+        } catch (Exception exception) {
+            log.warn("Auto tune coordinator failed to write agent run audit", exception);
+        }
+    }
+
+    private void writeAgentRunAudit(String triggerType,
+                                    Date startTime,
+                                    AutoTuneAgentService.AutoTuneAgentResult agentResult) {
+        if (agentResult == null) {
+            return;
+        }
+        Date finishTime = new Date();
+        Integer intervalMinutes = resolveIntervalMinutes();
+        AiAutoTuneJob job = new AiAutoTuneJob();
+        job.setTriggerType(AutoTuneTriggerType.normalize(triggerType));
+        job.setStatus(Boolean.TRUE.equals(agentResult.getSuccess())
+                ? AutoTuneJobStatus.SUCCESS.getCode()
+                : AutoTuneJobStatus.FAILED.getCode());
+        job.setStartTime(startTime == null ? finishTime : startTime);
+        job.setFinishTime(finishTime);
+        job.setHasActiveTasks(resolveHasActiveTasksForAudit());
+        job.setPromptSceneCode(AiPromptScene.AUTO_TUNE_DISPATCH.getCode());
+        job.setSummary(limitText(agentResult.getSummary(), SUMMARY_MAX_LENGTH));
+        job.setIntervalBefore(intervalMinutes);
+        job.setIntervalAfter(intervalMinutes);
+        job.setSuccessCount(0);
+        job.setRejectCount(0);
+        if (!Boolean.TRUE.equals(agentResult.getSuccess())) {
+            job.setErrorMessage(limitText(agentResult.getSummary(), ERROR_MESSAGE_MAX_LENGTH));
+        }
+        job.setLlmCallCount(agentResult.getLlmCallCount() == null ? 0 : agentResult.getLlmCallCount());
+        job.setPromptTokens(toSafeInteger(agentResult.getPromptTokens()));
+        job.setCompletionTokens(toSafeInteger(agentResult.getCompletionTokens()));
+        job.setTotalTokens(toSafeInteger(agentResult.getTotalTokens()));
+        job.setCreateTime(job.getStartTime());
+        if (!aiAutoTuneJobService.save(job)) {
+            log.warn("Auto tune coordinator failed to save agent run audit");
+        }
+    }
+
+    private int resolveHasActiveTasksForAudit() {
+        try {
+            return hasActiveTasks() ? 1 : 0;
+        } catch (RuntimeException exception) {
+            log.warn("Auto tune coordinator failed to query active tasks for audit", exception);
+            return 0;
+        }
+    }
+
+    private Integer toSafeInteger(Long value) {
+        if (value == null) {
+            return 0;
+        }
+        if (value > Integer.MAX_VALUE) {
+            return Integer.MAX_VALUE;
+        }
+        if (value < Integer.MIN_VALUE) {
+            return Integer.MIN_VALUE;
+        }
+        return value.intValue();
+    }
+
+    private String limitText(String value, int maxLength) {
+        if (value == null || value.length() <= maxLength) {
+            return value;
+        }
+        return value.substring(0, maxLength);
+    }
+
     private void safeWriteOperateLog(AutoTuneAgentService.AutoTuneAgentResult agentResult) {
         try {
             writeOperateLog(agentResult);

--
Gitblit v1.9.1