Add skill governance and runtime routing

This commit is contained in:
dahyeon committed 2026-05-14 14:28:59 -07:00
1 parent 6f09408423
commit 41dcfcdef7
71 files changed
+7057 -102

No files matched your search

+86 -18
View File
@@ -6,6 +6,8 @@ import { createBuiltInToolRegistry } from "@tools/builtins/index";
import type { ProcessTraceSnapshot } from "@skills/onboarding/runtime/trace";
import {
getEvalProcessLlmJudgeMaxTokens,
getEvalPrmEnabled,
getEvalPrmWeightsPath,
getLlmJudgeModel,
} from "../../config.js";
import type {
@@ -13,6 +15,12 @@ import type {
ProcessJudgeCheck,
ProcessJudgeResult,
} from "../types.js";
import { attributeTraceWithPrm } from "./attribution.js";
import {
extractPrmFeatures,
loadPrmModel,
predictPrmScore,
} from "./prm.js";
const processLlmJudgeSchema = z.object({
rationale: z.string().trim().min(1).max(300),
@@ -70,6 +78,37 @@ function finalize(checks: ProcessJudgeCheck[], rationale?: string): ProcessJudge
return result;
}
function enrichWithPrm(
result: ProcessJudgeResult,
trace: ProcessTraceSnapshot,
): ProcessJudgeResult {
if (!getEvalPrmEnabled()) {
return result;
}
const prmFeatures = extractPrmFeatures(trace);
const model = loadPrmModel(getEvalPrmWeightsPath());
if (!model) {
return {
...result,
prmFeatures,
};
}
const attribution = attributeTraceWithPrm(trace, model);
return {
...result,
prmFeatures,
prmScore: predictPrmScore(model, prmFeatures),
...(attribution.rootCauseStep
? {
rootCauseScoreLoss: attribution.rootCauseScoreLoss,
rootCauseStep: attribution.rootCauseStep,
}
: {}),
};
}
function hasAllTools(
actualTools: string[],
expectedTools: string[],
@@ -207,6 +246,23 @@ export function judgeProcessRule(
checks.push(validateToolArgs(trace));
const toolPlanStepCount = trace.steps.plan?.output.toolPlanStepCount;
if (toolPlanStepCount !== undefined) {
const actualToolCalls = trace.steps.toolCalls?.length ?? 0;
checks.push(
makeCheck(
"tool-call",
"tool-plan-consistent",
actualToolCalls >= toolPlanStepCount,
`planned=${toolPlanStepCount}, actual=${actualToolCalls}`,
),
);
} else {
checks.push(
makeSkippedCheck("tool-call", "tool-plan-consistent", "no toolPlan"),
);
}
if (processExpected?.minSteps !== undefined) {
const actualSteps = countRecordedSteps(trace);
checks.push(
@@ -258,7 +314,7 @@ export function judgeProcessRule(
);
}
return finalize(checks);
return enrichWithPrm(finalize(checks), trace);
}
function buildProcessJudgePrompt(
@@ -289,7 +345,8 @@ export async function judgeProcessWithLlm(
options: { finalText: string; request: AgentRequest },
): Promise<ProcessJudgeResult> {
if (!llmClient.hasEndpoints()) {
return finalize(
return enrichWithPrm(
finalize(
[
makeSkippedCheck(
"trace",
@@ -298,6 +355,8 @@ export async function judgeProcessWithLlm(
),
],
"skipped: no LLM endpoint",
),
trace,
);
}
@@ -324,30 +383,39 @@ export async function judgeProcessWithLlm(
);
if (!response) {
return finalize(
[makeCheck("trace", "validate-no-reject", false, "judge call failed")],
"process judge call failed",
return enrichWithPrm(
finalize(
[makeCheck("trace", "validate-no-reject", false, "judge call failed")],
"process judge call failed",
),
trace,
);
}
try {
const parsed = processLlmJudgeSchema.parse(JSON.parse(response.content));
const passed = parsed.score >= 0.7;
return finalize(
[
makeCheck(
"trace",
"validate-no-reject",
passed,
`llmScore=${round(parsed.score)}`,
),
],
parsed.rationale,
return enrichWithPrm(
finalize(
[
makeCheck(
"trace",
"validate-no-reject",
passed,
`llmScore=${round(parsed.score)}`,
),
],
parsed.rationale,
),
trace,
);
} catch {
return finalize(
[makeCheck("trace", "validate-no-reject", false, "invalid judge json")],
"invalid process judge json",
return enrichWithPrm(
finalize(
[makeCheck("trace", "validate-no-reject", false, "invalid judge json")],
"invalid process judge json",
),
trace,
);
}
}