@lynn123411/dsh-subagent-judge-dispatch 0.6.15 → 0.6.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.js CHANGED
@@ -193,9 +193,7 @@ var JudgeBudget = class {
193
193
 
194
194
  // src/server/judge-prompt.ts
195
195
  var QUESTION_KEY = "task_type";
196
- var NONE_OPTION_ID = "none";
197
- var NONE_CRITERION = "None of the configured task types applies. Keep the original model selection.";
198
- var QUESTION_INSTRUCTIONS = 'Which single task type below does the delegated job best match? Weigh what the job actually requires, not how it is phrased. Choose the number of a matching configured type, including an applicable catch-all type. Choose "none" only if no configured type applies.';
196
+ var QUESTION_INSTRUCTIONS = "Which single task type below does the delegated job best match? Weigh what the job actually requires, not how it is phrased. Answer with the number of exactly one listed type.";
199
197
  var PROBE_OPTION_ID = "1";
200
198
  var PROBE_TASK_TEXT = "\u8FDE\u63A5\u6D4B\u8BD5\uFF1A\u8FD9\u662F\u4E00\u6B21\u5224\u65AD\u670D\u52A1\u7684\u8FDE\u901A\u6027\u68C0\u67E5\u3002";
201
199
  var PROBE_INSTRUCTIONS = "Answer with the number of the one option listed below.";
@@ -243,7 +241,6 @@ ${tail}`;
243
241
  function buildJudgeRequest(rows, model, subject) {
244
242
  const criteria = {};
245
243
  for (const row of selectableRows(rows)) criteria[String(row.id)] = row.description;
246
- criteria[NONE_OPTION_ID] = NONE_CRITERION;
247
244
  return {
248
245
  model,
249
246
  state: { message: subject },
@@ -291,7 +288,6 @@ function readChoice(payload) {
291
288
  function readJudgeAnswer(payload, rows) {
292
289
  const choice = readChoice(payload);
293
290
  if (choice === void 0) return { kind: "empty" };
294
- if (choice === NONE_OPTION_ID) return { kind: "none" };
295
291
  const row = rows.find((candidate) => String(candidate.id) === choice && isOfferedRow(candidate));
296
292
  return row === void 0 ? { kind: "unmatched" } : { kind: "row", row };
297
293
  }
@@ -367,7 +363,7 @@ async function judgeTaskType(endpoint, rows, subject, fetchImpl = fetch, sharedB
367
363
  const decision = readJudgeAnswer(payload, offered);
368
364
  const latencyMs = budget.elapsed();
369
365
  if (decision.kind === "row") return { kind: "matched", row: decision.row, latencyMs, metrics };
370
- return { kind: "fallback", reason: decision.kind === "none" ? "no-match" : "unmatched", latencyMs, metrics };
366
+ return { kind: "fallback", reason: "unmatched", latencyMs, metrics };
371
367
  } catch {
372
368
  const reason = budget.expired() ? "timeout" : "unreachable";
373
369
  return { kind: "fallback", reason, latencyMs: budget.elapsed(), metrics };
@@ -406,6 +402,9 @@ function createJudgeCredentials(service, initialEndpoint) {
406
402
  };
407
403
  }
408
404
 
405
+ // src/server/judge-policy.ts
406
+ import { symbols } from "@deepseek-ai/cordis";
407
+
409
408
  // src/server/config.ts
410
409
  import z from "@deepseek-ai/schemastery";
411
410
 
@@ -483,11 +482,15 @@ function outcomeReason(outcome, decision) {
483
482
  }
484
483
 
485
484
  // src/server/judge-policy.ts
485
+ function serviceIdentity(llm) {
486
+ return llm?.[symbols.original] ?? llm;
487
+ }
486
488
  function createJudgePolicy(options) {
487
489
  let previousRows;
488
490
  let previousSignature = "";
489
491
  let rules = [];
490
492
  const settings = () => readConfigState(options.refs);
493
+ const isCurrentLlm = (llm) => serviceIdentity(options.llm()) === serviceIdentity(llm);
491
494
  function currentRules() {
492
495
  const stored = options.refs.rows.get();
493
496
  if (stored !== previousRows) {
@@ -500,31 +503,36 @@ function createJudgePolicy(options) {
500
503
  }
501
504
  return rules;
502
505
  }
503
- async function isInCatalog(route, budget) {
506
+ async function readCatalog(provider, llm, budget) {
507
+ if (llm === void 0) return void 0;
504
508
  try {
505
- return await budget.run(async () => {
506
- const llm = options.llm();
507
- if (llm === void 0) return false;
508
- const listed = await llm.isModelInCatalog(route.provider, route.model);
509
- return listed === true && options.llm() === llm;
510
- });
511
- } catch {
509
+ const models = await budget.run(() => llm.listModels(provider));
510
+ return new Set(models.map(({ id }) => id));
511
+ } catch (error) {
512
512
  budget.check();
513
- return false;
513
+ if (typeof error === "object" && error !== null && "code" in error && error.code === "NO_ADAPTER") {
514
+ return /* @__PURE__ */ new Set();
515
+ }
516
+ return void 0;
514
517
  }
515
518
  }
516
519
  async function eligibleRows(rows, budget) {
517
- const lookups = /* @__PURE__ */ new Map();
518
- const available = await Promise.all(rows.map((row) => {
519
- const route = { provider: row.provider.trim(), model: row.model.trim() };
520
- const key = JSON.stringify([route.provider, route.model]);
521
- const existing = lookups.get(key);
522
- if (existing !== void 0) return existing;
523
- const lookup = isInCatalog(route, budget);
524
- lookups.set(key, lookup);
525
- return lookup;
526
- }));
527
- return rows.filter((_row, index) => available[index]);
520
+ const llm = options.llm();
521
+ const providers = new Set(rows.map((row) => row.provider.trim()));
522
+ const catalogs = new Map(await Promise.all(Array.from(
523
+ providers,
524
+ async (provider) => [provider, await readCatalog(provider, llm, budget)]
525
+ )));
526
+ if (llm === void 0 || !isCurrentLlm(llm)) return { rows: [], failed: providers.size > 0 };
527
+ return {
528
+ rows: rows.filter((row) => catalogs.get(row.provider.trim())?.has(row.model.trim())),
529
+ failed: Array.from(catalogs.values()).some((catalog) => catalog === void 0)
530
+ };
531
+ }
532
+ async function checkCatalog(route, llm, budget) {
533
+ const catalog = await readCatalog(route.provider, llm, budget);
534
+ if (llm === void 0 || catalog === void 0 || !isCurrentLlm(llm)) return "catalog-unavailable";
535
+ return catalog.has(route.model) ? void 0 : "unavailable-route";
528
536
  }
529
537
  function prepare(snapshot) {
530
538
  if (!snapshot.endpointUrl || !snapshot.judgeModel) return "unconfigured";
@@ -542,18 +550,19 @@ function createJudgePolicy(options) {
542
550
  }
543
551
  async function previewRows(snapshot, budget) {
544
552
  const plan = { url: snapshot.endpointUrl, model: snapshot.judgeModel, rules: currentRules() };
545
- const rows = await eligibleRows(plan.rules, budget);
553
+ const candidates = await eligibleRows(plan.rules, budget);
546
554
  budget.check();
547
555
  if (!planIsCurrent(plan)) throw new Error("Judgement settings changed while preparing the preview");
548
- return rows;
556
+ if (candidates.failed && candidates.rows.length === 0) throw new Error("Could not read the DSH model catalog");
557
+ return candidates.rows;
549
558
  }
550
559
  async function checkFinalRoute(plan, route, budget, checkTask = () => void 0) {
551
560
  const stale = () => planIsCurrent(plan) ? checkTask() : "settings-changed";
552
561
  try {
553
562
  const changed = stale();
554
563
  if (changed !== void 0) return changed;
555
- const listed = await isInCatalog(route, budget);
556
- return stale() ?? (listed ? void 0 : "unavailable-route");
564
+ const reason = await checkCatalog(route, options.llm(), budget);
565
+ return stale() ?? reason;
557
566
  } catch {
558
567
  return budget.expired() ? "timeout" : "unavailable-route";
559
568
  }
@@ -574,12 +583,12 @@ function createJudgePolicy(options) {
574
583
  if (changed !== void 0) return { kind: "keep", stage: "judge", reason: changed };
575
584
  const candidates = await eligibleRows(plan.rules, budget);
576
585
  const changedAfterLookup = stale();
577
- if (changedAfterLookup !== void 0 || candidates.length === 0) {
578
- return { kind: "keep", stage: "judge", reason: changedAfterLookup ?? "no-authorized-route" };
586
+ if (changedAfterLookup !== void 0 || candidates.rows.length === 0) {
587
+ return { kind: "keep", stage: "judge", reason: changedAfterLookup ?? (candidates.failed ? "catalog-unavailable" : "no-authorized-route") };
579
588
  }
580
589
  outcome = await judgeTaskType(
581
590
  { url: plan.url, model: plan.model, apiKey },
582
- candidates,
591
+ candidates.rows,
583
592
  composeJudgeSubject(subject.label, subject.text),
584
593
  (url, init) => {
585
594
  rejectedBeforeSend = stale();
@@ -590,7 +599,7 @@ function createJudgePolicy(options) {
590
599
  budget
591
600
  );
592
601
  } catch {
593
- return { kind: "keep", stage: "judge", reason: budget.expired() ? "timeout" : "no-authorized-route" };
602
+ return { kind: "keep", stage: "judge", reason: budget.expired() ? "timeout" : "catalog-unavailable" };
594
603
  } finally {
595
604
  timings.judge = Math.round(performance.now() - judgeStarted);
596
605
  }
@@ -605,20 +614,22 @@ function createJudgePolicy(options) {
605
614
  const changed = stale();
606
615
  if (changed !== void 0) return { kind: "keep", stage: "preflight", reason: changed, match, metrics };
607
616
  const llm = options.llm();
608
- if (llm === void 0 || !await isInCatalog(match.route, budget)) {
609
- return { kind: "keep", stage: "preflight", reason: "unavailable-route", match, metrics };
617
+ if (llm === void 0) return { kind: "keep", stage: "preflight", reason: "catalog-unavailable", match, metrics };
618
+ const catalogReason = await checkCatalog(match.route, llm, budget);
619
+ if (catalogReason !== void 0) {
620
+ return { kind: "keep", stage: "preflight", reason: catalogReason, match, metrics };
610
621
  }
611
622
  const changedAfterLookup = stale();
612
623
  if (changedAfterLookup !== void 0) {
613
624
  return { kind: "keep", stage: "preflight", reason: changedAfterLookup, match, metrics };
614
625
  }
615
- if (options.llm() !== llm) return { kind: "keep", stage: "preflight", reason: "unavailable-route", match, metrics };
626
+ if (!isCurrentLlm(llm)) return { kind: "keep", stage: "preflight", reason: "unavailable-route", match, metrics };
616
627
  const resolved = await budget.run((signal) => llm.resolveCallConfig(match.route, signal));
617
628
  const changedAfterValidation = stale();
618
629
  if (changedAfterValidation !== void 0) {
619
630
  return { kind: "keep", stage: "preflight", reason: changedAfterValidation, match, metrics };
620
631
  }
621
- if (options.llm() !== llm) return { kind: "keep", stage: "preflight", reason: "unavailable-route", match, metrics };
632
+ if (!isCurrentLlm(llm)) return { kind: "keep", stage: "preflight", reason: "unavailable-route", match, metrics };
622
633
  const effort = resolved?.reasoningEffort;
623
634
  return { kind: "ready", match: { ...match, resolvedEffort: typeof effort === "string" ? effort : match.route.reasoningEffort }, metrics };
624
635
  } catch {
@@ -627,7 +638,7 @@ function createJudgePolicy(options) {
627
638
  timings.preflight = Math.round(performance.now() - preflightStarted);
628
639
  }
629
640
  }
630
- return { settings, prepare, evaluate, planIsCurrent, checkFinalRoute, previewRows };
641
+ return { settings, prepare, evaluate, planIsCurrent, checkFinalRoute, previewRows, hasRules: () => currentRules().length > 0 };
631
642
  }
632
643
 
633
644
  // src/server/log-store.ts
@@ -842,6 +853,7 @@ var REASONS = [
842
853
  "unavailable-route",
843
854
  "unconfigured",
844
855
  "no-authorized-route",
856
+ "catalog-unavailable",
845
857
  "unidentified-task",
846
858
  "explicit-route",
847
859
  "recorded-route",
@@ -1212,6 +1224,7 @@ function apply(ctx, config) {
1212
1224
  const cancellation = cancelWith(signal);
1213
1225
  if (agent === void 0 || !isSubagent(agent) || judged.has(agent.id) || cancellation.aborted) return;
1214
1226
  if (source !== "startup" && source !== "resume") return;
1227
+ if (!policy.hasRules()) return;
1215
1228
  const resumed = source === "resume";
1216
1229
  const budget = new JudgeBudget(cancellation);
1217
1230
  const timings = {};