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

+96 -2
View File
@@ -26,9 +26,13 @@ import {
getAlertThresholdP95LatencyMs,
getEvalAggTickMs,
getEvalArchiveTickMs,
getEvalArchiveBatchSize,
getEvalAttributionTickMs,
getEvalDriftTickMs,
getEvalProcessLlmJudgeEnabled,
getEvalProcessLlmJudgeSamplingRate,
getEvalPrmEnabled,
getEvalPrmWeightsPath,
getEvalPromoteTickMs,
getEvalReviewTickMs,
getEvalStreamMaxLen,
@@ -41,10 +45,13 @@ import {
import {
archiveEvalResults,
EVAL_ARCHIVE_CURSOR_KEY,
getEvalArchiveCollection,
type MongoConnectionLike,
} from "./archive.js";
import { computeDriftReports } from "./drift.js";
import { judgeOnboardingWithLlm } from "./judges/llm-judge.js";
import { writeProcessAttributions } from "./judges/attribution.js";
import { loadPrmModel } from "./judges/prm.js";
import {
judgeProcessRule,
judgeProcessWithLlm,
@@ -160,16 +167,28 @@ function parseOptionalJsonField<T>(value: string | undefined): T | undefined {
function toDerivedSnapshot(derived: DerivedState): EvalDerivedSnapshot {
return {
...(derived.abstentionScore !== undefined
? { abstentionScore: derived.abstentionScore }
: {}),
allowShortResponse: derived.allowShortResponse,
intent: derived.intent.kind,
phase: derived.phase.kind,
responseMode: derived.responseMode,
...(derived.routingDecision
? { routingDecision: derived.routingDecision }
: {}),
shouldRespond: derived.shouldRespond,
suggestedMaxTokens: derived.suggestedMaxTokens,
tier: derived.tier,
};
}
function getTraceAbstention(process: ProcessTraceSnapshot | undefined):
| NonNullable<ProcessTraceSnapshot["steps"]["classify"]>["output"]["abstention"]
| undefined {
return process?.steps.classify?.output.abstention;
}
function toEvalCase(job: ShadowEvalJob, messageId: string): EvalCaseDefinition {
return {
id: `shadow:${messageId}`,
@@ -264,6 +283,7 @@ export class ShadowWorker {
private archiveFailureCount = 0;
private lastAlertCheckAt = 0;
private lastArchiveAt = 0;
private lastAttributionAt = 0;
private lastDriftAt = 0;
private lastEvalAggregateAt = 0;
private lastPromoteAt = 0;
@@ -477,8 +497,15 @@ export class ShadowWorker {
private async processShadowEval(message: StreamEntry): Promise<void> {
const job = parseShadowEvalJob(message);
const abstention = getTraceAbstention(job.process);
const responseTextForValidation =
job.response.text.trim()
? job.response.text
: abstention?.action.kind === "no-response"
? "[NO_RESPONSE]"
: job.response.text;
const validation = validateOnboardingResponse(
job.response.text,
responseTextForValidation,
job.derived,
{
allowShortResponse: job.derived.allowShortResponse,
@@ -529,6 +556,12 @@ export class ShadowWorker {
evaluatedAt: new Date().toISOString(),
};
if (abstention) {
resultFields.abstentionAction = abstention.action.kind;
resultFields.abstentionReason = abstention.reason ?? "";
resultFields.abstentionScore = String(abstention.score);
}
if (job.process) {
resultFields.process = JSON.stringify(job.process);
}
@@ -566,7 +599,7 @@ export class ShadowWorker {
finalText: job.response.text,
});
return {
const merged: ProcessJudgeResult = {
checks: [...ruleResult.checks, ...llmResult.checks],
passedCount: ruleResult.passedCount + llmResult.passedCount,
totalCount: ruleResult.totalCount + llmResult.totalCount,
@@ -579,6 +612,21 @@ export class ShadowWorker {
),
rationale: llmResult.rationale,
};
const prmSource = llmResult.prmFeatures ? llmResult : ruleResult;
if (prmSource.prmFeatures) {
merged.prmFeatures = prmSource.prmFeatures;
}
if (prmSource.prmScore !== undefined) {
merged.prmScore = prmSource.prmScore;
}
if (prmSource.rootCauseStep) {
merged.rootCauseStep = prmSource.rootCauseStep;
}
if (prmSource.rootCauseScoreLoss !== undefined) {
merged.rootCauseScoreLoss = prmSource.rootCauseScoreLoss;
}
return merged;
}
private async processSummarize(message: StreamEntry): Promise<void> {
@@ -677,6 +725,7 @@ export class ShadowWorker {
await this.runAggregationTickIfNeeded(now);
await this.runArchiveTickIfNeeded(now);
await this.runAttributionTickIfNeeded(now);
await this.runDriftTickIfNeeded(now);
await this.runPromoteTickIfNeeded(now);
await this.runReviewTickIfNeeded(now);
@@ -851,6 +900,51 @@ export class ShadowWorker {
}
}
private async runAttributionTickIfNeeded(now: number): Promise<void> {
if (now - this.lastAttributionAt < getEvalAttributionTickMs()) {
return;
}
this.lastAttributionAt = now;
if (!getEvalPrmEnabled()) {
return;
}
if (!this.mongoConnection) {
this.warnMissingMongoOnce();
return;
}
const model = loadPrmModel(getEvalPrmWeightsPath());
if (!model) {
return;
}
const entries = await getEvalArchiveCollection(this.mongoConnection)
.find({
process: { $exists: true },
})
.sort({ evaluatedAt: -1 })
.limit(getEvalArchiveBatchSize())
.toArray();
const result = await writeProcessAttributions(
this.mongoConnection,
entries,
model,
{ nowMs: now },
);
if (result.attributedCount > 0) {
this.logger.info(
{
attributedCount: result.attributedCount,
modifiedCount: result.modifiedCount,
upsertedCount: result.upsertedCount,
},
"[ShadowWorker] Wrote process attribution entries",
);
}
}
private async runPromoteTickIfNeeded(now: number): Promise<void> {
if (now - this.lastPromoteAt < getEvalPromoteTickMs()) {
return;