opencode-model-router 2.6.1 → 2.8.0

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.
Files changed (47) hide show
  1. package/CHANGELOG.md +239 -0
  2. package/README.md +3 -1
  3. package/package.json +2 -2
  4. package/src/commands/approvals.ts +354 -0
  5. package/src/commands/output.ts +12 -1
  6. package/src/compat/v2-hooks.ts +255 -53
  7. package/src/index.ts +328 -37
  8. package/src/router/config.ts +48 -2
  9. package/src/router/dispatch-header.ts +95 -2
  10. package/src/router/git-tools.ts +165 -9
  11. package/src/router/protocol.ts +5 -1
  12. package/src/router/role-agents.ts +110 -3
  13. package/src/router/roles-config.ts +83 -0
  14. package/src/router/roles.ts +1 -1
  15. package/src/router/sessions.ts +11 -0
  16. package/src/routing/classify/route-line.ts +96 -5
  17. package/src/routing/classify/types.ts +26 -0
  18. package/src/routing/outcomes/signals.ts +17 -0
  19. package/src/routing/roles/admission.ts +1028 -0
  20. package/src/routing/roles/approvals.ts +876 -0
  21. package/src/routing/roles/binding.ts +17 -4
  22. package/src/routing/roles/check-placement.ts +112 -0
  23. package/src/routing/roles/git-root.ts +500 -0
  24. package/src/routing/roles/policy.ts +9 -0
  25. package/src/routing/roles/read-set.ts +325 -0
  26. package/src/routing/roles/work-root.ts +9 -1
  27. package/src/routing/wire/dispatch.ts +611 -18
  28. package/src/routing/wire/working-set.ts +58 -0
  29. package/src/util/linear-trim.ts +32 -0
  30. package/src/verify/batch.ts +7 -2
  31. package/src/verify/branch.ts +135 -0
  32. package/src/verify/deterministic.ts +232 -10
  33. package/src/verify/dispatch.ts +284 -9
  34. package/src/verify/dod.ts +46 -0
  35. package/src/verify/edges.ts +817 -0
  36. package/src/verify/gate.ts +349 -16
  37. package/src/verify/git-top.ts +43 -0
  38. package/src/verify/head.ts +107 -0
  39. package/src/verify/pending.ts +155 -8
  40. package/src/verify/perimeter.ts +1180 -0
  41. package/src/verify/pin.ts +109 -0
  42. package/src/verify/reference.ts +174 -30
  43. package/src/verify/risk.ts +16 -3
  44. package/src/verify/runner.ts +706 -32
  45. package/src/verify/tree.ts +347 -8
  46. package/src/verify/types.ts +57 -0
  47. package/src/verify/wiring.ts +1324 -35
@@ -382,9 +382,11 @@
382
382
  import { randomBytes } from "node:crypto";
383
383
 
384
384
  import type { DoD } from "./dod";
385
+ import type { PerimeterObservation } from "./gate";
386
+ import type { LinkPin } from "./pin";
385
387
  import type { RiskAssessment, RiskLevel } from "./risk";
386
388
  import type { ChangedPath } from "./runner";
387
- import type { ReferenceState, Verdict } from "./types";
389
+ import type { CheckPlacement, ReferenceState, Verdict } from "./types";
388
390
 
389
391
  // ---------------------------------------------------------------------------------------------
390
392
  // Constants (R2, R7, R9)
@@ -456,6 +458,60 @@ export const REPORTED_MEMO_MAX = TOMBSTONE_MAX;
456
458
 
457
459
  export type PendingState = "unverified" | "verifying" | "verified";
458
460
 
461
+ /**
462
+ * R3 cross-repo P2e: the frozen perimeter of a deferred entry (data only; the registry stores it, nothing here
463
+ * interprets it). `pins` is kept by identity (pinNow admission is keyed by the array's identity); a rejection
464
+ * normalises to [] for readers.
465
+ */
466
+ export interface DeferredPerimeter {
467
+ readonly observation: PerimeterObservation;
468
+ readonly workingSet: readonly { readonly root: string; readonly name?: string }[];
469
+ readonly links: readonly {
470
+ readonly at: string;
471
+ readonly root: string;
472
+ readonly name?: string;
473
+ readonly head: string;
474
+ readonly fingerprint: string;
475
+ readonly admit?: (root: string) => boolean;
476
+ /**
477
+ * Cross-repo P3 QA-1 M3: a dispatch-time dependency W no longer declared at return (the producer removed or retargeted
478
+ * it): not an input of the change, so router_verify skips it entirely (neither "no longer links" nor re-observed).
479
+ */
480
+ readonly removedAtReturn?: true;
481
+ }[];
482
+ /**
483
+ * Cross-repo P3 QA-1 M4: the dependency targets the producer added during the dispatch that were not admitted (an unadmitted
484
+ * edge's text, a link outside every parent and worktree, no or a refused top level), as read at return. Covering only, never
485
+ * seeds (I13) nor observed (I11): router_verify does not name them new, as it does not the dispatch-time unadmitted ones.
486
+ */
487
+ readonly unadmittedAtReturn?: readonly string[];
488
+ readonly pins: Promise<readonly LinkPin[]>;
489
+ /**
490
+ * The W directory `planPerimeter` read package links from at dispatch (<W>/node_modules): the bound worktree, else
491
+ * the canonical session folder. It can be a subfolder of the git top level. Absent: fall back to observation.w.root.
492
+ */
493
+ readonly linkBase?: string;
494
+ /**
495
+ * Cross-repo P3c.4 (SD-P3c-9): the admission W's dependencies were read with at dispatch, as data. At router_verify W's
496
+ * dependency targets are read again through it (`dependencyTargets`), so that a target outside the parents is named from its
497
+ * text and a new target's top level is found below its parent (the `gitTopOf` ceiling). Absent: an entry registered without
498
+ * one; then only W's package links are read again, admitted by nothing (as before P3c).
499
+ */
500
+ readonly admission?: DeferredAdmission;
501
+ }
502
+
503
+ /** Cross-repo P3c.4: the frozen admission of a deferred perimeter (data only, `admissionContext`'s inputs and parents). */
504
+ export interface DeferredAdmission {
505
+ /** Both spellings of each parent (as configured, and canonical), as `admissionContext` computed them at dispatch. */
506
+ readonly parents: readonly string[];
507
+ /** `routing.repoDepth` at dispatch. */
508
+ readonly depth: number;
509
+ /** `routing.repoExclude` at dispatch. Absent: none. */
510
+ readonly exclude?: readonly string[];
511
+ /** The working set at dispatch (the orchestrator's, W's own entry included). */
512
+ readonly workingSet: readonly { readonly worktree: string; readonly name?: string }[];
513
+ }
514
+
459
515
  /** Absolute path -> sha256 hex of the file bytes, or ABSENT_DIGEST for a missing path (R3). */
460
516
  export type FileDigests = ReadonlyMap<string, string>;
461
517
  export const ABSENT_DIGEST = "absent";
@@ -484,6 +540,30 @@ export interface PendingRegistration {
484
540
  readonly concurrentDispatches?: number;
485
541
  /** QA-3.1-3: the tool whose call discarded the dispatch snapshot (the change set is then unavailable). */
486
542
  readonly contaminatedBy?: string;
543
+ /**
544
+ * I7 (P0.2, S12): the checkout the producer returned on (the deferred finish's tree snapshot). router_verify refuses a tree
545
+ * on another branch by then, or one it cannot read while a branch was recorded. Absent: no snapshot then, and no check.
546
+ */
547
+ readonly returnedTree?: ReturnedTree;
548
+ /**
549
+ * I5 (P0.3): tools the producer lineage ran whose writes the router cannot observe (sorted). With an empty change set,
550
+ * router_verify's pass carries a caveat. Absent: none.
551
+ */
552
+ readonly unattributedWriters?: readonly string[];
553
+ /** R3 cross-repo P2e: the frozen perimeter of a deferred entry. Absent: none (the entry has no such key). */
554
+ readonly perimeter?: DeferredPerimeter;
555
+ /** R3 cross-repo P2e: the check placements of a deferred entry (an `undefined` slot: no placement). Absent: none. */
556
+ readonly placements?: readonly (CheckPlacement | undefined)[];
557
+ }
558
+
559
+ /** I7 (P0.2): HEAD and branch at return (TreeSnapshot.head / .branch: a short name, null when detached, absent when unknown). */
560
+ export interface ReturnedTree {
561
+ readonly head: string;
562
+ readonly branch?: string | null;
563
+ /** HeadState.root at return (realpath of the toplevel); absent on an entry registered without a head state. */
564
+ readonly root?: string;
565
+ /** HeadState.reflog at return: the HEAD reflog entry count; absent when it could not be counted. */
566
+ readonly reflog?: number;
487
567
  }
488
568
 
489
569
  /** What a verification of one handle produced (2.4.3 builds it from the gate result). */
@@ -535,6 +615,14 @@ export interface PendingEntry {
535
615
  readonly concurrentDispatches?: number;
536
616
  /** PendingRegistration.contaminatedBy, as registered. */
537
617
  readonly contaminatedBy?: string;
618
+ /** PendingRegistration.returnedTree, as registered. */
619
+ readonly returnedTree?: ReturnedTree;
620
+ /** PendingRegistration.unattributedWriters, as registered. */
621
+ readonly unattributedWriters?: readonly string[];
622
+ /** PendingRegistration.perimeter, frozen; `pins` never rejects. Absent when not provided or released. */
623
+ readonly perimeter?: DeferredPerimeter;
624
+ /** PendingRegistration.placements, frozen. Absent when not provided or released. */
625
+ readonly placements?: readonly (CheckPlacement | undefined)[];
538
626
  readonly createdAt: number;
539
627
  readonly state: PendingState;
540
628
  /** Set while state is "verifying". */
@@ -733,6 +821,10 @@ interface EntryRecord {
733
821
  readonly changedFilesDropped: number;
734
822
  readonly concurrentDispatches: number | undefined;
735
823
  readonly contaminatedBy: string | undefined;
824
+ readonly returnedTree: ReturnedTree | undefined;
825
+ readonly unattributedWriters: readonly string[] | undefined;
826
+ perimeter: DeferredPerimeter | undefined;
827
+ placements: readonly (CheckPlacement | undefined)[] | undefined;
736
828
  dod: DoD | undefined;
737
829
  reference: Promise<ReferenceState>;
738
830
  changedFiles: readonly ChangedPath[] | "unavailable";
@@ -757,6 +849,39 @@ interface LedgerRecord {
757
849
  readonly recordedAt: number;
758
850
  }
759
851
 
852
+ /**
853
+ * P2e: a frozen copy of a deferred perimeter. `pins` is kept by identity (the resolved array is never copied):
854
+ * only a rejection is normalised, to [] (so readers never see one and none goes unhandled).
855
+ */
856
+ function freezePerimeter(perimeter: DeferredPerimeter | undefined): DeferredPerimeter | undefined {
857
+ if (perimeter === undefined) return undefined;
858
+ return Object.freeze({
859
+ observation: perimeter.observation,
860
+ // Kept as given (a path string); absent stays absent (router_verify then falls back to observation.w.root).
861
+ ...(perimeter.linkBase === undefined ? {} : { linkBase: perimeter.linkBase }),
862
+ // P3c.4: copied and frozen (the caller's arrays never alias it); absent stays absent (links only at router_verify).
863
+ ...(perimeter.admission === undefined ? {} : { admission: freezeAdmission(perimeter.admission) }),
864
+ workingSet: Object.freeze(perimeter.workingSet.map((w) => Object.freeze({ ...w }))),
865
+ links: Object.freeze(perimeter.links.map((l) => Object.freeze({ ...l }))),
866
+ // P3 QA-1 M4: copied and frozen; absent stays absent.
867
+ ...(perimeter.unadmittedAtReturn === undefined ? {} : { unadmittedAtReturn: Object.freeze([...perimeter.unadmittedAtReturn]) }),
868
+ pins: perimeter.pins.then(
869
+ (pins) => pins,
870
+ (): readonly LinkPin[] => [],
871
+ ),
872
+ });
873
+ }
874
+
875
+ /** P3c.4: a frozen copy of a deferred perimeter's admission data. */
876
+ function freezeAdmission(admission: DeferredAdmission): DeferredAdmission {
877
+ return Object.freeze({
878
+ parents: Object.freeze([...admission.parents]),
879
+ depth: admission.depth,
880
+ ...(admission.exclude === undefined ? {} : { exclude: Object.freeze([...admission.exclude]) }),
881
+ workingSet: Object.freeze(admission.workingSet.map((entry) => Object.freeze({ ...entry }))),
882
+ });
883
+ }
884
+
760
885
  function defaultRandom(bytes: number): Uint8Array {
761
886
  return randomBytes(bytes);
762
887
  }
@@ -828,6 +953,10 @@ export function createPendingRegistry(options: PendingRegistryOptions): PendingR
828
953
  digests: rec.digests,
829
954
  ...(rec.concurrentDispatches !== undefined ? { concurrentDispatches: rec.concurrentDispatches } : {}),
830
955
  ...(rec.contaminatedBy !== undefined ? { contaminatedBy: rec.contaminatedBy } : {}),
956
+ ...(rec.returnedTree !== undefined ? { returnedTree: rec.returnedTree } : {}),
957
+ ...(rec.unattributedWriters !== undefined ? { unattributedWriters: rec.unattributedWriters } : {}),
958
+ ...(rec.perimeter !== undefined ? { perimeter: rec.perimeter } : {}),
959
+ ...(rec.placements !== undefined ? { placements: rec.placements } : {}),
831
960
  createdAt: rec.createdAt,
832
961
  state: rec.state,
833
962
  verifyingSince: rec.verifyingSince,
@@ -854,13 +983,19 @@ export function createPendingRegistry(options: PendingRegistryOptions): PendingR
854
983
  byHandle.delete(rec.handle);
855
984
  counters.weight -= rec.pathWeight + rec.refWeight;
856
985
  addTombstone(rec.handle, rec.orchestratorSessionID);
857
- const hook = options.onEvict;
858
- if (hook === undefined) return;
859
986
  try {
860
- hook(snapshot(rec), cause);
861
- } catch {
862
- // A logging hook must never break the registry (PendingRegistryOptions.onEvict).
863
- counters.hookFailures += 1;
987
+ const hook = options.onEvict;
988
+ if (hook === undefined) return;
989
+ try {
990
+ hook(snapshot(rec), cause);
991
+ } catch {
992
+ // A logging hook must never break the registry (PendingRegistryOptions.onEvict).
993
+ counters.hookFailures += 1;
994
+ }
995
+ } finally {
996
+ // P2e: an evicted entry is gone; it keeps neither its perimeter nor its placements.
997
+ rec.perimeter = undefined;
998
+ rec.placements = undefined;
864
999
  }
865
1000
  }
866
1001
 
@@ -869,6 +1004,8 @@ export function createPendingRegistry(options: PendingRegistryOptions): PendingR
869
1004
  rec.pathWeight = 0;
870
1005
  rec.refWeight = 0;
871
1006
  rec.released = true;
1007
+ rec.perimeter = undefined;
1008
+ rec.placements = undefined;
872
1009
  rec.dod = undefined;
873
1010
  rec.digests = undefined;
874
1011
  rec.changedFiles = "unavailable";
@@ -1003,7 +1140,12 @@ export function createPendingRegistry(options: PendingRegistryOptions): PendingR
1003
1140
  if (incoming.length > MAX_STORED_CHANGED_FILES) changedFilesDropped = incoming.length;
1004
1141
  else changedFiles = Object.freeze([...incoming]);
1005
1142
  }
1006
- const pathWeight = changedFiles === "unavailable" ? 0 : changedFiles.length;
1143
+ const perimeter = freezePerimeter(reg.perimeter);
1144
+ const placements = reg.placements !== undefined ? Object.freeze([...reg.placements]) : undefined;
1145
+ const pathWeight =
1146
+ (changedFiles === "unavailable" ? 0 : changedFiles.length) +
1147
+ (perimeter !== undefined ? perimeter.workingSet.length + perimeter.links.length + (perimeter.unadmittedAtReturn?.length ?? 0) : 0) +
1148
+ (placements !== undefined ? placements.length : 0);
1007
1149
 
1008
1150
  // Plan the evictions first: a registration that cannot fit evicts nothing.
1009
1151
  const session = sessions.get(reg.orchestratorSessionID);
@@ -1068,6 +1210,11 @@ export function createPendingRegistry(options: PendingRegistryOptions): PendingR
1068
1210
  changedFilesDropped,
1069
1211
  concurrentDispatches: reg.concurrentDispatches,
1070
1212
  contaminatedBy: reg.contaminatedBy,
1213
+ returnedTree: reg.returnedTree !== undefined ? Object.freeze({ ...reg.returnedTree }) : undefined,
1214
+ unattributedWriters: reg.unattributedWriters !== undefined && reg.unattributedWriters.length > 0
1215
+ ? Object.freeze([...reg.unattributedWriters]) : undefined,
1216
+ perimeter,
1217
+ placements,
1071
1218
  dod: reg.dod,
1072
1219
  reference,
1073
1220
  changedFiles,