#
Junjie
9 天以前 dc3f9cc91759823ce59486f19b138be4b296a0f1
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,
@@ -75,6 +78,15 @@
            return AutoTuneCoordinatorResult.skipped("interval_not_reached");
        }
        return runAgentWithLock(AutoTuneTriggerType.AUTO.getCode(), intervalMinutes, true);
    }
    @Override
    public AutoTuneCoordinatorResult runManualAutoTune() {
        return runAgentWithLock(AutoTuneTriggerType.MANUAL.getCode(), null, false);
    }
    private AutoTuneCoordinatorResult runAgentWithLock(String triggerType, Integer intervalMinutes, boolean writeGuard) {
        String lockKey = RedisKeyType.AI_AUTO_TUNE_RUNNING_LOCK.key;
        String lockToken = UUID.randomUUID().toString();
        if (!redisUtil.trySetStringIfAbsent(lockKey, lockToken, RUNNING_LOCK_SECONDS)) {
@@ -82,15 +94,20 @@
        }
        AutoTuneAgentService.AutoTuneAgentResult agentResult = null;
        markLastTriggerGuard(intervalMinutes);
        Date startTime = new Date();
        try {
            agentResult = autoTuneAgentService.runAutoTune(AutoTuneTriggerType.AUTO.getCode());
            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(exception);
            agentResult = failedAgentResult(triggerType, exception);
            safeWriteOperateLog(agentResult);
            safeWriteAgentRunAudit(triggerType, startTime, agentResult);
            return AutoTuneCoordinatorResult.triggered(agentResult);
        } finally {
            redisUtil.compareAndDelete(lockKey, lockToken);
@@ -159,16 +176,24 @@
        return System.currentTimeMillis() - latestFinishTime.getTime() >= intervalMillis;
    }
    private void safeMarkLastTriggerGuard(int intervalMinutes) {
        try {
            markLastTriggerGuard(intervalMinutes);
        } catch (Exception exception) {
            log.warn("Auto tune coordinator failed to write last trigger guard", exception);
        }
    }
    private void markLastTriggerGuard(int intervalMinutes) {
        long expireSeconds = intervalMinutes * 60L;
        redisUtil.set(RedisKeyType.AI_AUTO_TUNE_LAST_TRIGGER_GUARD.key,
                String.valueOf(System.currentTimeMillis()), expireSeconds);
    }
    private AutoTuneAgentService.AutoTuneAgentResult failedAgentResult(Exception exception) {
    private AutoTuneAgentService.AutoTuneAgentResult failedAgentResult(String triggerType, Exception exception) {
        AutoTuneAgentService.AutoTuneAgentResult result = new AutoTuneAgentService.AutoTuneAgentResult();
        result.setSuccess(false);
        result.setTriggerType(AutoTuneTriggerType.AUTO.getCode());
        result.setTriggerType(triggerType);
        result.setSummary("自动调参后台任务执行异常: " + exception.getMessage());
        result.setToolCallCount(0);
        result.setLlmCallCount(0);
@@ -177,6 +202,80 @@
        result.setTotalTokens(0L);
        result.setMaxRoundsReached(false);
        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) {
@@ -192,7 +291,7 @@
            return;
        }
        OperateLog operateLog = new OperateLog();
        operateLog.setAction("ai_auto_tune_background_scheduler");
        operateLog.setAction(resolveOperateLogAction(agentResult));
        operateLog.setUserId(SYSTEM_USER_ID);
        operateLog.setIp("system");
        operateLog.setRequest(JSON.toJSONString(buildRequestSummary(agentResult)));
@@ -201,6 +300,13 @@
        operateLogService.save(operateLog);
    }
    private String resolveOperateLogAction(AutoTuneAgentService.AutoTuneAgentResult agentResult) {
        if (agentResult != null && AutoTuneTriggerType.MANUAL.getCode().equals(agentResult.getTriggerType())) {
            return "ai_auto_tune_manual_trigger";
        }
        return "ai_auto_tune_background_scheduler";
    }
    private Map<String, Object> buildRequestSummary(AutoTuneAgentService.AutoTuneAgentResult agentResult) {
        Map<String, Object> request = new LinkedHashMap<>();
        request.put("trigger", agentResult.getTriggerType());