opencode-model-router 2.8.0 → 2.9.1

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 (91) hide show
  1. package/CHANGELOG.md +372 -0
  2. package/README.md +22 -2
  3. package/package.json +5 -1
  4. package/src/commands/output.ts +23 -1
  5. package/src/compat/background.ts +2439 -0
  6. package/src/compat/child-session.ts +6 -0
  7. package/src/compat/turn-state.ts +215 -0
  8. package/src/compat/v2-client.ts +136 -17
  9. package/src/compat/v2-hooks.ts +2656 -114
  10. package/src/compat/v2-observations.ts +328 -0
  11. package/src/executor/capacity.ts +362 -0
  12. package/src/executor/child-guards.ts +40 -0
  13. package/src/executor/command.ts +265 -0
  14. package/src/executor/compile/edges.ts +809 -0
  15. package/src/executor/compile/estimate.ts +158 -0
  16. package/src/executor/compile/footprint.ts +829 -0
  17. package/src/executor/compile/index.ts +1080 -0
  18. package/src/executor/compile/parse.ts +1492 -0
  19. package/src/executor/compile/residue.ts +706 -0
  20. package/src/executor/compile/validate.ts +280 -0
  21. package/src/executor/create.ts +672 -0
  22. package/src/executor/expand.ts +551 -0
  23. package/src/executor/git.ts +1214 -0
  24. package/src/executor/host-preamble.ts +17 -0
  25. package/src/executor/instructions.ts +35 -0
  26. package/src/executor/integrate/bisect.ts +73 -0
  27. package/src/executor/integrate/checks.ts +1086 -0
  28. package/src/executor/integrate/land.ts +177 -0
  29. package/src/executor/integrate/phase-suite.ts +407 -0
  30. package/src/executor/integrate/queue.ts +3083 -0
  31. package/src/executor/integrate/run-resolver.ts +19 -0
  32. package/src/executor/launch.ts +652 -0
  33. package/src/executor/ledger.ts +2208 -0
  34. package/src/executor/observations.ts +212 -0
  35. package/src/executor/paths.ts +159 -0
  36. package/src/executor/platform.ts +39 -0
  37. package/src/executor/process-identity.ts +19 -0
  38. package/src/executor/qa-loop.ts +470 -0
  39. package/src/executor/recovery.ts +403 -0
  40. package/src/executor/runtime.ts +6233 -0
  41. package/src/executor/schedule.ts +246 -0
  42. package/src/executor/slots/env.ts +145 -0
  43. package/src/executor/slots/pool.ts +1564 -0
  44. package/src/executor/slots/provision.ts +853 -0
  45. package/src/executor/tool.ts +534 -0
  46. package/src/executor/types.ts +2064 -0
  47. package/src/executor/unattended.ts +488 -0
  48. package/src/guard/store.ts +3 -1
  49. package/src/index.ts +1498 -288
  50. package/src/router/config.ts +776 -25
  51. package/src/router/git-tools.ts +106 -15
  52. package/src/router/leases.ts +1469 -0
  53. package/src/router/logger.ts +44 -32
  54. package/src/router/protocol.ts +40 -10
  55. package/src/router/role-agents.ts +224 -17
  56. package/src/router/roles-config.ts +10 -1
  57. package/src/router/roles.ts +1 -1
  58. package/src/router/run-tools.ts +56 -4
  59. package/src/router/tool-paths.ts +308 -0
  60. package/src/router/work-roots.ts +86 -0
  61. package/src/routing/advisor/findings.ts +25 -5
  62. package/src/routing/classify/index.ts +7 -1
  63. package/src/routing/classify/rules.ts +11 -0
  64. package/src/routing/commands/stats.ts +2 -2
  65. package/src/routing/outcomes/durations.ts +225 -0
  66. package/src/routing/outcomes/index.ts +59 -2
  67. package/src/routing/outcomes/persist.ts +409 -0
  68. package/src/routing/outcomes/stats.ts +170 -11
  69. package/src/routing/outcomes/timings.ts +836 -0
  70. package/src/routing/outcomes/types.ts +98 -0
  71. package/src/routing/roles/approvals.ts +86 -5
  72. package/src/routing/roles/binding.ts +99 -9
  73. package/src/routing/roles/orphans.ts +68 -0
  74. package/src/routing/roles/policy.ts +21 -3
  75. package/src/routing/roles/work-root.ts +3 -1
  76. package/src/routing/wire/dispatch.ts +86 -12
  77. package/src/routing/wire/hint.ts +56 -8
  78. package/src/v2.ts +94 -49
  79. package/src/verify/branch.ts +8 -2
  80. package/src/verify/canonical-path.ts +35 -0
  81. package/src/verify/deterministic.ts +38 -9
  82. package/src/verify/dispatch.ts +64 -28
  83. package/src/verify/exec.ts +52 -9
  84. package/src/verify/pending.ts +1691 -10
  85. package/src/verify/perimeter.ts +12 -6
  86. package/src/verify/plan-task.ts +729 -0
  87. package/src/verify/reference.ts +216 -3
  88. package/src/verify/tree.ts +30 -13
  89. package/src/verify/types.ts +9 -2
  90. package/src/verify/wiring.ts +290 -31
  91. package/tiers.json +5 -5
@@ -0,0 +1,280 @@
1
+ import {
2
+ RESERVED_TASK_IDS,
3
+ isTaskId,
4
+ ledgerViolations,
5
+ type Edge,
6
+ type LedgerV1,
7
+ type TaskId,
8
+ type TaskKind,
9
+ type TaskNode,
10
+ } from "../types";
11
+ import { Reach, minimalCycle } from "./edges";
12
+ import { overlaps } from "./footprint";
13
+
14
+ // Ledger validation (memo 2.3.1 section 7). Pure; returns errors only, warnings stay in `summary.warnings`.
15
+
16
+ export interface ValidationError { code: string; message: string; taskIds: TaskId[] }
17
+
18
+ export interface ValidateOptions {
19
+ /** Case-insensitive path comparison; defaults to the platform default (win32). */
20
+ ci?: boolean;
21
+ }
22
+
23
+ export const VALIDATION_CODES: readonly string[] = [
24
+ "ledger:*",
25
+ "cycle",
26
+ "id-unknown",
27
+ "id-duplicate",
28
+ "id-invalid",
29
+ "id-reserved",
30
+ "path-outside",
31
+ "closure",
32
+ "qa-multiple",
33
+ "shared-context-header",
34
+ ];
35
+
36
+ const LAUNCHABLE_KINDS: ReadonlySet<TaskKind> = new Set<TaskKind>(["task", "tests", "fix", "qa"]);
37
+ const SETTLED_STATES: ReadonlySet<string> = new Set(["done", "cancelled", "integrated"]);
38
+ const DEAD_STATES: ReadonlySet<string> = new Set(["done", "cancelled"]);
39
+ const GATE_PREFIX = "gate:";
40
+
41
+ function gateOf(phase: string): string {
42
+ return `${GATE_PREFIX}${phase}`;
43
+ }
44
+
45
+ /** A task that edits files: only these can be the top element (writes everything) when their footprint is unknown. */
46
+ export function isEditingNode(t: TaskNode): boolean {
47
+ return t.kind === "task" || t.kind === "tests" || t.kind === "fix";
48
+ }
49
+
50
+ /** True when `t` writes everything (D4): an editing task whose write set is still unknown. */
51
+ export function isTopNode(t: TaskNode): boolean {
52
+ return t.footprint.confidence === "unknown" && isEditingNode(t);
53
+ }
54
+
55
+ /** Stricter than the ledger's `isRepoPath`: '/'-separated, no '.'/'..'/empty/':' segment, no NUL, not rooted. */
56
+ export function isStrictRepoPath(p: string): boolean {
57
+ if (p.length === 0 || p.includes("\\") || p.includes("\0") || p.startsWith("/")) return false;
58
+ return p.split("/").every((seg) => seg !== "" && seg !== "." && seg !== ".." && !seg.includes(":"));
59
+ }
60
+
61
+ function ancestorsOf(key: string): string[] {
62
+ const out: string[] = [];
63
+ let i = key.lastIndexOf("/");
64
+ while (i > 0) {
65
+ out.push(key.slice(0, i));
66
+ i = key.lastIndexOf("/", i - 1);
67
+ }
68
+ return out;
69
+ }
70
+
71
+ function defaultCi(): boolean {
72
+ return process.platform === "win32";
73
+ }
74
+
75
+ function taskIdsAt(at: string, ids: readonly string[], edges: readonly Edge[]): TaskId[] {
76
+ const em = /^\$\.edges\[(\d+)\]/.exec(at);
77
+ if (em !== null) {
78
+ const e = edges[Number(em[1])];
79
+ if (e === undefined) return [];
80
+ return e.from.startsWith(GATE_PREFIX) ? [e.to] : [e.from, e.to];
81
+ }
82
+ if (!at.startsWith("$.tasks")) return [];
83
+ const rest = at.slice("$.tasks".length);
84
+ let best: string | null = null;
85
+ for (const id of ids) {
86
+ const dot = `.${id}`;
87
+ const br = `["${id}"]`;
88
+ const hit = rest.startsWith(br) || (rest.startsWith(dot) && (rest.length === dot.length || /[.[]/.test(rest.charAt(dot.length))));
89
+ if (hit && (best === null || id.length > best.length)) best = id;
90
+ }
91
+ return best === null ? [] : [best];
92
+ }
93
+
94
+ function pairKey(a: number, b: number): string {
95
+ return a < b ? `${a}:${b}` : `${b}:${a}`;
96
+ }
97
+
98
+ export function validateLedger(l: LedgerV1, o: ValidateOptions = {}): ValidationError[] {
99
+ const ci = o.ci ?? defaultCi();
100
+ const errors: ValidationError[] = [];
101
+ const tasks = Object.values(l.tasks) as TaskNode[];
102
+ const ids = Object.keys(l.tasks);
103
+ const idSet = new Set(ids);
104
+ const phaseIds = l.phases.map((p) => p.id);
105
+ const phaseSet = new Set(phaseIds);
106
+
107
+ // id-invalid / id-reserved / id-duplicate
108
+ const folded = new Map<string, string>();
109
+ for (const id of ids) {
110
+ // QA-2.3 coverage: the reserved check comes first, because `isTaskId` rejects the reserved names too.
111
+ if (RESERVED_TASK_IDS.has(id)) errors.push({ code: "id-reserved", message: `task id ${id} is reserved`, taskIds: [id] });
112
+ else if (!isTaskId(id)) errors.push({ code: "id-invalid", message: `task id ${JSON.stringify(id)} is not a valid task id`, taskIds: [id] });
113
+ const key = id.toLowerCase();
114
+ const first = folded.get(key);
115
+ if (first === undefined) folded.set(key, id);
116
+ else errors.push({ code: "id-duplicate", message: `task ids ${first} and ${id} are equal after case folding`, taskIds: [first, id] });
117
+ }
118
+
119
+ // id-unknown (pre-empts the ledger's edge-endpoint violation)
120
+ const badEdge = new Set<number>();
121
+ const validEdges: Edge[] = [];
122
+ l.edges.forEach((e, i) => {
123
+ const fromOk = e.from.startsWith(GATE_PREFIX) ? phaseSet.has(e.from.slice(GATE_PREFIX.length)) : idSet.has(e.from);
124
+ const toOk = idSet.has(e.to);
125
+ if (fromOk && toOk) {
126
+ validEdges.push(e);
127
+ return;
128
+ }
129
+ badEdge.add(i);
130
+ const unknown = [fromOk ? null : e.from, toOk ? null : e.to].filter((x): x is string => x !== null);
131
+ errors.push({
132
+ code: "id-unknown",
133
+ message: `edge ${e.from} -> ${e.to} names unknown ${unknown.join(", ")}`,
134
+ taskIds: [e.from, e.to].filter((x) => idSet.has(x)),
135
+ });
136
+ });
137
+
138
+ // ledger:<c>
139
+ for (const v of ledgerViolations(l)) {
140
+ if (v.code === "edge-endpoint") {
141
+ const m = /^\$\.edges\[(\d+)\]/.exec(v.at);
142
+ if (m !== null && badEdge.has(Number(m[1]))) continue;
143
+ }
144
+ errors.push({ code: `ledger:${v.code}`, message: `${v.code} at ${v.at}`, taskIds: taskIdsAt(v.at, ids, l.edges) });
145
+ }
146
+
147
+ // path-outside
148
+ for (const t of tasks) {
149
+ for (const [access, list] of [["write", t.footprint.writes], ["read", t.footprint.reads]] as const) {
150
+ for (const p of list) {
151
+ if (!isStrictRepoPath(p)) {
152
+ errors.push({ code: "path-outside", message: `${t.id} ${access} path ${JSON.stringify(p)} is not a repo-relative '/' path`, taskIds: [t.id] });
153
+ }
154
+ }
155
+ }
156
+ }
157
+
158
+ // cycle: tasks and gates, plus the implicit t -> gate:phase(t)
159
+ const nodes = [...ids, ...phaseIds.map(gateOf)];
160
+ const graphEdges: Array<Pick<Edge, "from" | "to">> = validEdges.map((e) => ({ from: e.from, to: e.to }));
161
+ for (const t of tasks) if (t.phase !== null && phaseSet.has(t.phase)) graphEdges.push({ from: t.id, to: gateOf(t.phase) });
162
+ const cycle = minimalCycle(nodes, graphEdges);
163
+ if (cycle !== null) {
164
+ errors.push({ code: "cycle", message: `dependency cycle: ${[...cycle, cycle[0] ?? ""].join(" -> ")}`, taskIds: cycle });
165
+ }
166
+
167
+ // qa-multiple
168
+ const qaByPhase = new Map<string, string[]>();
169
+ for (const t of tasks) {
170
+ if (t.kind !== "qa" || t.phase === null || DEAD_STATES.has(t.state)) continue;
171
+ const list = qaByPhase.get(t.phase);
172
+ if (list === undefined) qaByPhase.set(t.phase, [t.id]);
173
+ else list.push(t.id);
174
+ }
175
+ for (const [phase, list] of qaByPhase) {
176
+ if (list.length > 1) errors.push({ code: "qa-multiple", message: `phase ${phase} has ${list.length} live qa tasks: ${list.join(", ")}`, taskIds: list });
177
+ }
178
+
179
+ // A launch-header-shaped line in sharedContext is reported by `ledgerViolations` above (`ledger:shared-context-header`).
180
+ errors.push(...closureErrors(l, tasks, validEdges, ci));
181
+ return errors;
182
+ }
183
+
184
+ interface ClosureTask {
185
+ node: TaskNode;
186
+ top: boolean;
187
+ writes: readonly string[];
188
+ reads: readonly string[];
189
+ }
190
+
191
+ function closureErrors(l: LedgerV1, all: readonly TaskNode[], edges: readonly Edge[], ci: boolean): ValidationError[] {
192
+ const live: ClosureTask[] = [];
193
+ for (const t of all) {
194
+ if (!LAUNCHABLE_KINDS.has(t.kind) || SETTLED_STATES.has(t.state)) continue;
195
+ const strict = (p: string): boolean => isStrictRepoPath(p);
196
+ // QA-2.3-1: a write path that is not a strict repo path cannot be compared, so the task writes ⊤ (the path itself is
197
+ // reported as `path-outside`); it is never dropped silently.
198
+ live.push({
199
+ node: t,
200
+ top: isTopNode(t) || !t.footprint.writes.every(strict),
201
+ writes: t.footprint.writes.filter(strict),
202
+ reads: t.kind === "qa" ? [] : t.footprint.reads.filter(strict),
203
+ });
204
+ }
205
+ if (live.length < 2) return [];
206
+
207
+ const reach = new Reach(
208
+ all.map((t) => t.id),
209
+ (id) => l.tasks[id]?.phase ?? null,
210
+ l.phases.map((p) => p.id),
211
+ );
212
+ for (const e of edges) reach.add(e.from, e.to);
213
+
214
+ const key = (p: string): string => (ci ? p.toLowerCase() : p);
215
+ // exact: path -> tasks having it; below: ancestor dir -> tasks having a path strictly beneath it
216
+ const exact = new Map<string, Set<number>>();
217
+ const below = new Map<string, Set<number>>();
218
+ const put = (m: Map<string, Set<number>>, k: string, i: number): void => {
219
+ const s = m.get(k);
220
+ if (s === undefined) m.set(k, new Set([i]));
221
+ else s.add(i);
222
+ };
223
+ const tops: number[] = [];
224
+ const nonEmpty: number[] = [];
225
+ live.forEach((t, i) => {
226
+ if (t.top) tops.push(i);
227
+ if (t.top || t.writes.length > 0 || t.reads.length > 0) nonEmpty.push(i);
228
+ for (const p of [...t.writes, ...t.reads]) {
229
+ const k = key(p);
230
+ put(exact, k, i);
231
+ for (const a of ancestorsOf(k)) put(below, a, i);
232
+ }
233
+ });
234
+
235
+ const conflicts = (u: ClosureTask, v: ClosureTask): string | null => {
236
+ const hasAny = (t: ClosureTask): boolean => t.top || t.writes.length > 0 || t.reads.length > 0;
237
+ if (u.top && hasAny(v)) return "unknown write set (top) overlaps everything";
238
+ if (v.top && hasAny(u)) return "unknown write set (top) overlaps everything";
239
+ for (const w of u.writes) {
240
+ for (const x of v.writes) if (overlaps(w, x, ci)) return `write/write ${w}`;
241
+ for (const x of v.reads) if (overlaps(w, x, ci)) return `write/read ${w}`;
242
+ }
243
+ for (const w of v.writes) for (const x of u.reads) if (overlaps(w, x, ci)) return `read/write ${w}`;
244
+ return null;
245
+ };
246
+
247
+ const out: ValidationError[] = [];
248
+ const seen = new Set<string>();
249
+ const consider = (i: number, j: number): void => {
250
+ if (i === j) return;
251
+ const pk = pairKey(i, j);
252
+ if (seen.has(pk)) return;
253
+ seen.add(pk);
254
+ const u = live[i] as ClosureTask;
255
+ const v = live[j] as ClosureTask;
256
+ if (!reach.concurrent(u.node.id, v.node.id)) return;
257
+ const why = conflicts(u, v);
258
+ if (why === null) return;
259
+ out.push({
260
+ code: "closure",
261
+ message: `${u.node.id} and ${v.node.id} have no path between them and conflict: ${why}`,
262
+ taskIds: [u.node.id, v.node.id],
263
+ });
264
+ };
265
+
266
+ live.forEach((t, i) => {
267
+ if (t.top) {
268
+ for (const j of nonEmpty) consider(i, j);
269
+ return;
270
+ }
271
+ for (const j of tops) consider(i, j);
272
+ for (const w of t.writes) {
273
+ const k = key(w);
274
+ for (const j of exact.get(k) ?? []) consider(i, j);
275
+ for (const j of below.get(k) ?? []) consider(i, j);
276
+ for (const a of ancestorsOf(k)) for (const j of exact.get(a) ?? []) consider(i, j);
277
+ }
278
+ });
279
+ return out;
280
+ }