wenjin272 commented on code in PR #1042:
URL: https://github.com/apache/flink-agents/pull/1042#discussion_r3886692692


##########
plan/src/main/java/org/apache/flink/agents/plan/actions/ModelRoutingResolver.java:
##########
@@ -111,12 +143,247 @@ public RoutingDecision call() throws Exception {
             if (!router.isCandidate(selectedModel)) {
                 throw new IllegalStateException(
                         String.format(
-                                "Routing strategy for router '%s' returned 
non-candidate model '%s'; candidates are %s.",
+                                "Routing decision for router '%s' selected 
non-candidate model '%s'; candidates are %s.",
                                 model, selectedModel, 
router.getCandidateNames()));
             }
-            decisionSource = ModelRoutingEvent.SOURCE_STRATEGY;
+            decisionSource = concreteSource;
+        }
+        return finish(requestId, model, router, decision, selectedModel, 
decisionSource, ctx);
+    }
+
+    /** Records the decision latency histogram sample (also for decisions the 
guards reject). */
+    private static void recordDecisionLatency(RunnerContext ctx, 
RoutingDecision decision) {
+        Double decisionMs = decision.getDecisionMs();
+        FlinkAgentsMetricGroup actionMetrics = ctx.getActionMetricGroup();
+        if (actionMetrics != null && decisionMs != null) {
+            
actionMetrics.getHistogram("routingDecisionLatencyMs").update(Math.round(decisionMs));
         }
+    }
+
+    /**
+     * LLM-as-judge path (framework-managed, per discussion #897): the engine 
runs the judge chat
+     * itself through the normal durable/metered/observable invoker path — 
durable id {@code
+     * "judge:<router>"} so a recovered run replays the original verdict 
instead of re-calling the
+     * judge (with a durable action-state store configured; without one the 
judge re-runs on replay,
+     * like any non-deterministic strategy) — then derives the decision from 
the verdict as a pure
+     * function. The decision (including its wall time, which covers the judge 
call) is persisted
+     * under the standard {@code "route:<router>"} durable call, preserving 
the replay-fingerprint
+     * property. Verdict abstains are persisted <i>as abstains</i>, so a 
replay after a
+     * candidate-set change re-resolves to the current default exactly like 
the strategy path.
+     *
+     * <p>Failure policy: an unparseable or non-candidate verdict always 
abstains to the router's
+     * default model. A judge call that exhausts its retries honors the 
request's error-handling
+     * strategy, exactly like a throwing rule/custom strategy: {@code FAIL} 
surfaces the outage
+     * loudly, {@code IGNORE} degrades to the default with the cause recorded. 
Interrupts
+     * (cancellation) propagate and are never persisted as routing outcomes.
+     */
+    private static ResolvedModelRoute resolveViaJudge(
+            UUID requestId,
+            String model,
+            ModelRouter router,
+            LlmJudgeRoutingStrategy judge,
+            RoutingContext routingContext,
+            RunnerContext ctx)
+            throws Exception {
+        long start = System.nanoTime();
+        Agent.ErrorHandlingStrategy errorStrategy =
+                
ctx.getConfig().get(AgentExecutionOptions.ERROR_HANDLING_STRATEGY);
+        int numRetries = ChatModelInvoker.configuredRetries(ctx, 
errorStrategy);
+        int retryWaitIntervalSec = 
ChatModelInvoker.configuredRetryWaitSec(ctx, errorStrategy);
 
+        Map<String, Object> judgeMetadata = new LinkedHashMap<>();
+        judgeMetadata.put("judge_model", judge.getJudgeModel());
+        String verdictModel = null;
+        String abstainReason = null;
+        // A misconfigured judge follows the same policy as a failed judge 
call: FAIL is loud
+        // (a config error should not hide), IGNORE abstains so the default 
model keeps
+        // answering. Under IGNORE a replayed request is unaffected either way 
— the stored
+        // decision below wins over the freshly computed abstain.
+        String misconfigured = 
judgeSetupMisconfiguration(judge.getJudgeModel(), ctx);
+        if (misconfigured != null && errorStrategy != 
Agent.ErrorHandlingStrategy.IGNORE) {
+            throw new IllegalStateException(misconfigured);
+        }
+        if (misconfigured != null) {
+            abstainReason = misconfigured;
+        } else {
+            try {
+                ChatModelInvoker.ChatAttemptResult judgeResult =
+                        ChatModelInvoker.chatWithRetries(
+                                requestId,
+                                judge.getJudgeModel(),
+                                "judge:" + model,
+                                judge.buildJudgeMessages(routingContext),
+                                Map.of(),
+                                null,
+                                ctx,
+                                errorStrategy,
+                                numRetries,
+                                retryWaitIntervalSec);
+                ChatModelAction.recordAttemptRetryStats(
+                        ctx,
+                        requestId,
+                        judgeResult.chatModel,
+                        judgeResult.retryCount,
+                        judgeResult.totalRetryWaitSec);
+                ChatMessage reply = judgeResult.response;
+                Object promptTokens = reply.getExtraArgs().get("promptTokens");
+                Object completionTokens = 
reply.getExtraArgs().get("completionTokens");
+                if (promptTokens != null) {
+                    judgeMetadata.put("judge_prompt_tokens", promptTokens);
+                }
+                if (completionTokens != null) {
+                    judgeMetadata.put("judge_completion_tokens", 
completionTokens);
+                }
+                verdictModel =
+                        judge.parseVerdict(reply.getContent(), 
router.getCandidateNames())
+                                .orElse(null);
+                abstainReason =
+                        verdictModel == null ? "judge verdict was not a 
candidate name" : null;
+            } catch (InterruptedException cancellation) {
+                // Cancellation surfacing from the between-retries backoff 
sleep.
+                Thread.currentThread().interrupt();
+                throw cancellation;
+            } catch (ChatModelInvoker.ChatAttemptFailed failure) {
+                ChatModelAction.recordAttemptRetryStats(
+                        ctx,
+                        requestId,
+                        failure.chatModel,
+                        failure.retryCount,
+                        failure.totalRetryWaitSec);
+                // Cancellation surfacing from inside the judge attempt (the 
invoker wraps every
+                // attempt exception): it must propagate, never persist as a 
routing outcome.
+                if (containsInterrupt(failure)) {

Review Comment:
   The judge call can persist an interruption before it is identified as 
cancellation, but this comes from the existing shared durable execution path 
and also affects direct chat calls. I’ve opened #1070 to track the common fix, 
so this does not need to block this PR.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to