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.
- package/CHANGELOG.md +372 -0
- package/README.md +22 -2
- package/package.json +5 -1
- package/src/commands/output.ts +23 -1
- package/src/compat/background.ts +2439 -0
- package/src/compat/child-session.ts +6 -0
- package/src/compat/turn-state.ts +215 -0
- package/src/compat/v2-client.ts +136 -17
- package/src/compat/v2-hooks.ts +2656 -114
- package/src/compat/v2-observations.ts +328 -0
- package/src/executor/capacity.ts +362 -0
- package/src/executor/child-guards.ts +40 -0
- package/src/executor/command.ts +265 -0
- package/src/executor/compile/edges.ts +809 -0
- package/src/executor/compile/estimate.ts +158 -0
- package/src/executor/compile/footprint.ts +829 -0
- package/src/executor/compile/index.ts +1080 -0
- package/src/executor/compile/parse.ts +1492 -0
- package/src/executor/compile/residue.ts +706 -0
- package/src/executor/compile/validate.ts +280 -0
- package/src/executor/create.ts +672 -0
- package/src/executor/expand.ts +551 -0
- package/src/executor/git.ts +1214 -0
- package/src/executor/host-preamble.ts +17 -0
- package/src/executor/instructions.ts +35 -0
- package/src/executor/integrate/bisect.ts +73 -0
- package/src/executor/integrate/checks.ts +1086 -0
- package/src/executor/integrate/land.ts +177 -0
- package/src/executor/integrate/phase-suite.ts +407 -0
- package/src/executor/integrate/queue.ts +3083 -0
- package/src/executor/integrate/run-resolver.ts +19 -0
- package/src/executor/launch.ts +652 -0
- package/src/executor/ledger.ts +2208 -0
- package/src/executor/observations.ts +212 -0
- package/src/executor/paths.ts +159 -0
- package/src/executor/platform.ts +39 -0
- package/src/executor/process-identity.ts +19 -0
- package/src/executor/qa-loop.ts +470 -0
- package/src/executor/recovery.ts +403 -0
- package/src/executor/runtime.ts +6233 -0
- package/src/executor/schedule.ts +246 -0
- package/src/executor/slots/env.ts +145 -0
- package/src/executor/slots/pool.ts +1564 -0
- package/src/executor/slots/provision.ts +853 -0
- package/src/executor/tool.ts +534 -0
- package/src/executor/types.ts +2064 -0
- package/src/executor/unattended.ts +488 -0
- package/src/guard/store.ts +3 -1
- package/src/index.ts +1498 -288
- package/src/router/config.ts +776 -25
- package/src/router/git-tools.ts +106 -15
- package/src/router/leases.ts +1469 -0
- package/src/router/logger.ts +44 -32
- package/src/router/protocol.ts +40 -10
- package/src/router/role-agents.ts +224 -17
- package/src/router/roles-config.ts +10 -1
- package/src/router/roles.ts +1 -1
- package/src/router/run-tools.ts +56 -4
- package/src/router/tool-paths.ts +308 -0
- package/src/router/work-roots.ts +86 -0
- package/src/routing/advisor/findings.ts +25 -5
- package/src/routing/classify/index.ts +7 -1
- package/src/routing/classify/rules.ts +11 -0
- package/src/routing/commands/stats.ts +2 -2
- package/src/routing/outcomes/durations.ts +225 -0
- package/src/routing/outcomes/index.ts +59 -2
- package/src/routing/outcomes/persist.ts +409 -0
- package/src/routing/outcomes/stats.ts +170 -11
- package/src/routing/outcomes/timings.ts +836 -0
- package/src/routing/outcomes/types.ts +98 -0
- package/src/routing/roles/approvals.ts +86 -5
- package/src/routing/roles/binding.ts +99 -9
- package/src/routing/roles/orphans.ts +68 -0
- package/src/routing/roles/policy.ts +21 -3
- package/src/routing/roles/work-root.ts +3 -1
- package/src/routing/wire/dispatch.ts +86 -12
- package/src/routing/wire/hint.ts +56 -8
- package/src/v2.ts +94 -49
- package/src/verify/branch.ts +8 -2
- package/src/verify/canonical-path.ts +35 -0
- package/src/verify/deterministic.ts +38 -9
- package/src/verify/dispatch.ts +64 -28
- package/src/verify/exec.ts +52 -9
- package/src/verify/pending.ts +1691 -10
- package/src/verify/perimeter.ts +12 -6
- package/src/verify/plan-task.ts +729 -0
- package/src/verify/reference.ts +216 -3
- package/src/verify/tree.ts +30 -13
- package/src/verify/types.ts +9 -2
- package/src/verify/wiring.ts +290 -31
- package/tiers.json +5 -5
|
@@ -0,0 +1,809 @@
|
|
|
1
|
+
import { ExecutorError, type Edge, type Footprint, type TaskKind } from "../types";
|
|
2
|
+
import { overlaps, touchKey, UNKNOWN_WRITES } from "./footprint";
|
|
3
|
+
import type { CompileIssue, ExplicitPhrase, ParsedPhase, ParsedPlan } from "./parse";
|
|
4
|
+
|
|
5
|
+
// Edge derivation (memo 2.3.1 section 5). Passes in precedence order: structural, graph, explicit, contract,
|
|
6
|
+
// conflict. Edges are deduplicated by `${from}>${to}`; the first pass that produces a pair wins. llm and learned
|
|
7
|
+
// edges are never produced here.
|
|
8
|
+
|
|
9
|
+
/** One task as the edge passes see it; `writes === UNKNOWN_WRITES` is the top element (writes everything, D4). */
|
|
10
|
+
export interface EdgeTask {
|
|
11
|
+
id: string;
|
|
12
|
+
phase: string | null;
|
|
13
|
+
kind: TaskKind;
|
|
14
|
+
planIndex: number;
|
|
15
|
+
writes: readonly string[] | typeof UNKNOWN_WRITES;
|
|
16
|
+
reads: readonly string[];
|
|
17
|
+
confidence: Footprint["confidence"];
|
|
18
|
+
isContract: boolean;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// ---------------------------------------------------------------------------------------------------------------
|
|
22
|
+
// Small helpers
|
|
23
|
+
|
|
24
|
+
const LAUNCHABLE: ReadonlySet<TaskKind> = new Set<TaskKind>(["task", "tests", "fix", "qa"]);
|
|
25
|
+
const EVIDENCE_MAX = 120;
|
|
26
|
+
const MERGED_RE = /\bmerge[sd]?\b/i;
|
|
27
|
+
const LINE_IN_EVIDENCE_RE = /\bL(\d+):/;
|
|
28
|
+
|
|
29
|
+
function at<T>(a: readonly T[], i: number): T {
|
|
30
|
+
return a[i] as T;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function gateOf(phaseId: string): string {
|
|
34
|
+
return `gate:${phaseId}`;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function excerpt(s: string): string {
|
|
38
|
+
const t = s.replace(/\s+/g, " ").trim();
|
|
39
|
+
return t.length > EVIDENCE_MAX ? t.slice(0, EVIDENCE_MAX) : t;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function evidenceOf(rule: string, line: number, text: string): string {
|
|
43
|
+
return `${rule} L${line}: ${excerpt(text)}`;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function lineOfEvidence(evidence: string): number {
|
|
47
|
+
const m = LINE_IN_EVIDENCE_RE.exec(evidence);
|
|
48
|
+
return m === null ? 0 : Number(m[1]);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function mkEdge(from: string, to: string, kind: Edge["kind"], confidence: Edge["confidence"], evidence: string): Edge {
|
|
52
|
+
return { from, to, kind, confidence, evidence };
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function isTop(t: EdgeTask): boolean {
|
|
56
|
+
return t.writes === UNKNOWN_WRITES;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function writesOf(t: EdgeTask): readonly string[] {
|
|
60
|
+
return t.writes === UNKNOWN_WRITES ? [] : t.writes;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** A QA review runs on a fixed commit, so its reads never take part in conflicts (anti false-serialization). */
|
|
64
|
+
function conflictReadsOf(t: EdgeTask): readonly string[] {
|
|
65
|
+
return t.kind === "qa" ? [] : t.reads;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Insertion-ordered edge collection, deduplicated by `${from}>${to}`. */
|
|
69
|
+
class EdgeSet {
|
|
70
|
+
private readonly byKey = new Map<string, Edge>();
|
|
71
|
+
add(e: Edge): boolean {
|
|
72
|
+
const key = `${e.from}>${e.to}`;
|
|
73
|
+
if (e.from === e.to || this.byKey.has(key)) return false;
|
|
74
|
+
this.byKey.set(key, e);
|
|
75
|
+
return true;
|
|
76
|
+
}
|
|
77
|
+
has(from: string, to: string): boolean {
|
|
78
|
+
return this.byKey.has(`${from}>${to}`);
|
|
79
|
+
}
|
|
80
|
+
get(from: string, to: string): Edge | undefined {
|
|
81
|
+
return this.byKey.get(`${from}>${to}`);
|
|
82
|
+
}
|
|
83
|
+
list(): Edge[] {
|
|
84
|
+
return [...this.byKey.values()];
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function byPlanIndex(a: EdgeTask, b: EdgeTask): number {
|
|
89
|
+
return a.planIndex - b.planIndex || (a.id < b.id ? -1 : a.id > b.id ? 1 : 0);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function groupByPhase(tasks: readonly EdgeTask[]): Map<string, EdgeTask[]> {
|
|
93
|
+
const out = new Map<string, EdgeTask[]>();
|
|
94
|
+
for (const t of tasks) {
|
|
95
|
+
if (t.phase === null) continue;
|
|
96
|
+
const l = out.get(t.phase);
|
|
97
|
+
if (l === undefined) out.set(t.phase, [t]);
|
|
98
|
+
else l.push(t);
|
|
99
|
+
}
|
|
100
|
+
return out;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function isPostPhase(ph: ParsedPhase): boolean {
|
|
104
|
+
return ph.synthetic && ph.id.endsWith(".post");
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// ---------------------------------------------------------------------------------------------------------------
|
|
108
|
+
// Reach: bitset transitive closure over tasks and phase gates
|
|
109
|
+
|
|
110
|
+
export class Reach {
|
|
111
|
+
private readonly index = new Map<string, number>();
|
|
112
|
+
private readonly words: number;
|
|
113
|
+
private readonly rows: Uint32Array[];
|
|
114
|
+
private readonly n: number;
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Nodes are the tasks and `gate:P` for every phase. Every task t of phase P implicitly reaches `gate:P`
|
|
118
|
+
* (a gate passes only once the phase is integrated, D7).
|
|
119
|
+
*/
|
|
120
|
+
constructor(taskIds: readonly string[], phaseOf: (id: string) => string | null, phaseIds: readonly string[]) {
|
|
121
|
+
for (const id of taskIds) if (!this.index.has(id)) this.index.set(id, this.index.size);
|
|
122
|
+
for (const ph of phaseIds) {
|
|
123
|
+
const g = gateOf(ph);
|
|
124
|
+
if (!this.index.has(g)) this.index.set(g, this.index.size);
|
|
125
|
+
}
|
|
126
|
+
this.n = this.index.size;
|
|
127
|
+
this.words = Math.max(1, Math.ceil(this.n / 32));
|
|
128
|
+
this.rows = [];
|
|
129
|
+
for (let i = 0; i < this.n; i += 1) this.rows.push(new Uint32Array(this.words));
|
|
130
|
+
for (const id of taskIds) {
|
|
131
|
+
const ph = phaseOf(id);
|
|
132
|
+
if (ph === null) continue;
|
|
133
|
+
const g = this.index.get(gateOf(ph));
|
|
134
|
+
if (g === undefined) continue;
|
|
135
|
+
this.setBit(this.row(this.slot(id)), g);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
private slot(id: string): number {
|
|
140
|
+
const i = this.index.get(id);
|
|
141
|
+
if (i === undefined) throw new ExecutorError("E_INVALID_INPUT", `unknown node ${id}`, { id });
|
|
142
|
+
return i;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
private row(i: number): Uint32Array {
|
|
146
|
+
return at(this.rows, i);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
private setBit(row: Uint32Array, bit: number): void {
|
|
150
|
+
const w = bit >>> 5;
|
|
151
|
+
row[w] = (row[w] as number) | (1 << (bit & 31));
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
private getBit(row: Uint32Array, bit: number): boolean {
|
|
155
|
+
return (((row[bit >>> 5] as number) >>> (bit & 31)) & 1) === 1;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** Adds the edge and closes the relation transitively; O(n * n/32) worst case. */
|
|
159
|
+
add(from: string, to: string): void {
|
|
160
|
+
const f = this.slot(from);
|
|
161
|
+
const t = this.slot(to);
|
|
162
|
+
if (f === t || this.getBit(this.row(f), t)) return;
|
|
163
|
+
const below = new Uint32Array(this.row(t));
|
|
164
|
+
this.setBit(below, t);
|
|
165
|
+
for (let x = 0; x < this.n; x += 1) {
|
|
166
|
+
const row = this.row(x);
|
|
167
|
+
if (x !== f && !this.getBit(row, f)) continue;
|
|
168
|
+
for (let w = 0; w < this.words; w += 1) row[w] = (row[w] as number) | (below[w] as number);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** True when a path from `a` to `b` of length >= 1 exists. */
|
|
173
|
+
reaches(a: string, b: string): boolean {
|
|
174
|
+
return this.getBit(this.row(this.slot(a)), this.slot(b));
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
concurrent(a: string, b: string): boolean {
|
|
178
|
+
if (a === b) return false;
|
|
179
|
+
return !this.reaches(a, b) && !this.reaches(b, a);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// ---------------------------------------------------------------------------------------------------------------
|
|
184
|
+
// Structural pass
|
|
185
|
+
|
|
186
|
+
interface IntraCore {
|
|
187
|
+
edges: Edge[];
|
|
188
|
+
warnings: CompileIssue[];
|
|
189
|
+
/** Per phase: its preflight items, else its tasks with no in-phase predecessor. */
|
|
190
|
+
sources: Map<string, string[]>;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/** Edges between consecutive non-empty stages; `pre` feeds the first one. */
|
|
194
|
+
function stageEdges(pre: readonly EdgeTask[], stages: ReadonlyArray<readonly EdgeTask[]>, push: (a: string, b: string) => void): void {
|
|
195
|
+
let prev: readonly EdgeTask[] = pre;
|
|
196
|
+
for (const s of stages) {
|
|
197
|
+
if (s.length === 0) continue;
|
|
198
|
+
for (const a of prev) for (const b of s) push(a.id, b.id);
|
|
199
|
+
prev = s;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
function intraCore(p: ParsedPlan, tasks: readonly EdgeTask[]): IntraCore {
|
|
204
|
+
const byPhase = groupByPhase(tasks);
|
|
205
|
+
const known = new Set(tasks.map((t) => t.id));
|
|
206
|
+
const edges: Edge[] = [];
|
|
207
|
+
const warnings: CompileIssue[] = [];
|
|
208
|
+
const seen = new Set<string>();
|
|
209
|
+
const pusher = (evidence: string) => (from: string, to: string): void => {
|
|
210
|
+
const key = `${from}>${to}`;
|
|
211
|
+
if (from === to || seen.has(key)) return;
|
|
212
|
+
seen.add(key);
|
|
213
|
+
edges.push(mkEdge(from, to, "structural", "high", evidence));
|
|
214
|
+
};
|
|
215
|
+
|
|
216
|
+
for (const ph of p.phases) {
|
|
217
|
+
if (isPostPhase(ph)) continue;
|
|
218
|
+
const mine = byPhase.get(ph.id) ?? [];
|
|
219
|
+
const pre = mine.filter((t) => t.kind === "preflight");
|
|
220
|
+
const qa = mine.filter((t) => t.kind === "qa");
|
|
221
|
+
const rest = mine.filter((t) => t.kind !== "preflight" && t.kind !== "qa");
|
|
222
|
+
const order = ph.order;
|
|
223
|
+
if (order === null) {
|
|
224
|
+
const work = rest.filter((t) => t.kind !== "tests");
|
|
225
|
+
const tests = rest.filter((t) => t.kind === "tests");
|
|
226
|
+
stageEdges(pre, [work, tests, qa], pusher(evidenceOf("stage", ph.span.line, `${ph.id}: preflight, tasks, tests, QA`)));
|
|
227
|
+
continue;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
const push = pusher(evidenceOf("order", order.line, order.raw));
|
|
231
|
+
const succ = new Map<string, string[]>();
|
|
232
|
+
const pred = new Map<string, string[]>();
|
|
233
|
+
const link = (a: string, b: string): void => {
|
|
234
|
+
if (a === b) return;
|
|
235
|
+
const s = succ.get(a) ?? [];
|
|
236
|
+
if (s.includes(b)) return;
|
|
237
|
+
s.push(b);
|
|
238
|
+
succ.set(a, s);
|
|
239
|
+
const q = pred.get(b) ?? [];
|
|
240
|
+
q.push(a);
|
|
241
|
+
pred.set(b, q);
|
|
242
|
+
};
|
|
243
|
+
const groups = order.groups;
|
|
244
|
+
for (let i = 0; i + 1 < groups.length; i += 1) {
|
|
245
|
+
for (const a of at(groups, i)) for (const b of at(groups, i + 1)) link(a, b);
|
|
246
|
+
}
|
|
247
|
+
const listed = new Set(groups.flat());
|
|
248
|
+
const first = groups.length > 0 ? at(groups, 0) : [];
|
|
249
|
+
const last = groups.length > 0 ? at(groups, groups.length - 1) : [];
|
|
250
|
+
for (const t of pre) if (!listed.has(t.id)) for (const b of first) link(t.id, b);
|
|
251
|
+
const qaId = `${ph.id}.QA`;
|
|
252
|
+
if (!listed.has(qaId) && known.has(qaId)) for (const b of last) link(b, qaId);
|
|
253
|
+
|
|
254
|
+
// D3: items that are no task of the ledger (FIX, a missing QA) are contracted: preds(v) -> succs(v).
|
|
255
|
+
const virtual: string[] = [];
|
|
256
|
+
for (const id of groups.flat()) if (!known.has(id) && !virtual.includes(id)) virtual.push(id);
|
|
257
|
+
for (const v of virtual) {
|
|
258
|
+
const ps = [...(pred.get(v) ?? [])];
|
|
259
|
+
const ss = [...(succ.get(v) ?? [])];
|
|
260
|
+
for (const a of ps) for (const b of ss) link(a, b);
|
|
261
|
+
for (const a of ps) succ.set(a, (succ.get(a) ?? []).filter((x) => x !== v));
|
|
262
|
+
for (const b of ss) pred.set(b, (pred.get(b) ?? []).filter((x) => x !== v));
|
|
263
|
+
succ.delete(v);
|
|
264
|
+
pred.delete(v);
|
|
265
|
+
}
|
|
266
|
+
for (const [a, bs] of succ) {
|
|
267
|
+
if (!known.has(a)) continue;
|
|
268
|
+
for (const b of bs) if (known.has(b)) push(a, b);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
const unlisted = rest.filter((t) => !listed.has(t.id));
|
|
272
|
+
if (unlisted.length > 0) {
|
|
273
|
+
warnings.push({
|
|
274
|
+
code: "order-unlisted",
|
|
275
|
+
line: order.line,
|
|
276
|
+
message: `Order: of ${ph.id} omits ${unlisted.map((t) => t.id).join(", ")}; stage edges added`,
|
|
277
|
+
});
|
|
278
|
+
stageEdges(
|
|
279
|
+
pre,
|
|
280
|
+
[unlisted.filter((t) => t.kind !== "tests"), unlisted.filter((t) => t.kind === "tests"), qa],
|
|
281
|
+
pusher(evidenceOf("stage", ph.span.line, `${ph.id}: tasks missing from Order:`)),
|
|
282
|
+
);
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
const phaseOfTask = new Map<string, string | null>(tasks.map((t) => [t.id, t.phase]));
|
|
287
|
+
const hasIn = new Set<string>();
|
|
288
|
+
for (const e of edges) {
|
|
289
|
+
const pf = phaseOfTask.get(e.from);
|
|
290
|
+
if (pf !== undefined && pf !== null && pf === phaseOfTask.get(e.to)) hasIn.add(e.to);
|
|
291
|
+
}
|
|
292
|
+
const sources = new Map<string, string[]>();
|
|
293
|
+
for (const ph of p.phases) {
|
|
294
|
+
const mine = byPhase.get(ph.id) ?? [];
|
|
295
|
+
const pre = mine.filter((t) => t.kind === "preflight");
|
|
296
|
+
const src = pre.length > 0 ? pre : mine.filter((t) => !hasIn.has(t.id));
|
|
297
|
+
sources.set(ph.id, src.map((t) => t.id));
|
|
298
|
+
}
|
|
299
|
+
return { edges, warnings, sources };
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/** Warnings of the structural pass (`order-unlisted`). */
|
|
303
|
+
export function structuralWarnings(p: ParsedPlan, tasks: readonly EdgeTask[]): CompileIssue[] {
|
|
304
|
+
return intraCore(p, [...tasks].sort(byPlanIndex)).warnings;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
export function structuralEdges(p: ParsedPlan, tasks: readonly EdgeTask[], crossPhase: "gate" | "inferred"): Edge[] {
|
|
308
|
+
const sorted = [...tasks].sort(byPlanIndex);
|
|
309
|
+
const core = intraCore(p, sorted);
|
|
310
|
+
const out = new EdgeSet();
|
|
311
|
+
for (const e of core.edges) out.add(e);
|
|
312
|
+
|
|
313
|
+
const phaseById = new Map(p.phases.map((ph) => [ph.id, ph]));
|
|
314
|
+
for (const ph of p.phases) {
|
|
315
|
+
for (const dep of ph.dependsOn) {
|
|
316
|
+
// An external dependency (a dogfood switch) is not a phase of this plan: no gate to wait for.
|
|
317
|
+
if (dep.external || dep.id === ph.id || !phaseById.has(dep.id)) continue;
|
|
318
|
+
for (const s of core.sources.get(ph.id) ?? []) {
|
|
319
|
+
out.add(mkEdge(gateOf(dep.id), s, "structural", "high", evidenceOf("depends-on", dep.line, `${ph.id} depends on ${dep.id}`)));
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
if (crossPhase === "gate") {
|
|
325
|
+
const waves = [...p.waves].sort((a, b) => a.line - b.line);
|
|
326
|
+
// A wave made only of dogfood phases has no gates: the chain goes through to the previous gated wave.
|
|
327
|
+
const gated = (w: (typeof waves)[number]): boolean => w.phases.some((pid) => {
|
|
328
|
+
const pph = phaseById.get(pid);
|
|
329
|
+
return pph !== undefined && !pph.dogfood;
|
|
330
|
+
});
|
|
331
|
+
let prev: (typeof waves)[number] | undefined;
|
|
332
|
+
for (const cur of waves) {
|
|
333
|
+
if (prev !== undefined) {
|
|
334
|
+
for (const pid of prev.phases) {
|
|
335
|
+
const pph = phaseById.get(pid);
|
|
336
|
+
if (pph === undefined || pph.dogfood) continue;
|
|
337
|
+
for (const qid of cur.phases) {
|
|
338
|
+
for (const s of core.sources.get(qid) ?? []) {
|
|
339
|
+
out.add(mkEdge(gateOf(pid), s, "structural", "high", evidenceOf("wave-gate", cur.line, `wave ${prev.id} before wave ${cur.id}`)));
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
if (gated(cur)) prev = cur;
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
return out.list();
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
// ---------------------------------------------------------------------------------------------------------------
|
|
351
|
+
// Node resolution shared by the graph and explicit passes
|
|
352
|
+
|
|
353
|
+
interface Ref {
|
|
354
|
+
kind: "task" | "phase" | "wave";
|
|
355
|
+
id: string;
|
|
356
|
+
/** What must be done for this node to be finished: the task itself, or the gates of its phases. */
|
|
357
|
+
pred: string[];
|
|
358
|
+
/** What starts this node: the task itself, or the sources of its phases. */
|
|
359
|
+
succ: string[];
|
|
360
|
+
phases: string[];
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
function makeResolver(p: ParsedPlan, tasks: readonly EdgeTask[], sources: ReadonlyMap<string, string[]>): (id: string) => Ref | null {
|
|
364
|
+
const taskIds = new Set(tasks.map((t) => t.id));
|
|
365
|
+
const phases = new Map(p.phases.map((ph) => [ph.id, ph]));
|
|
366
|
+
const waves = new Map(p.waves.map((w) => [w.id, w]));
|
|
367
|
+
const gatesOf = (ids: readonly string[]): string[] =>
|
|
368
|
+
ids.filter((id) => {
|
|
369
|
+
const ph = phases.get(id);
|
|
370
|
+
return ph !== undefined && !ph.dogfood;
|
|
371
|
+
}).map(gateOf);
|
|
372
|
+
return (id) => {
|
|
373
|
+
if (taskIds.has(id)) return { kind: "task", id, pred: [id], succ: [id], phases: [] };
|
|
374
|
+
if (phases.has(id)) return { kind: "phase", id, pred: gatesOf([id]), succ: sources.get(id) ?? [], phases: [id] };
|
|
375
|
+
const w = waves.get(id);
|
|
376
|
+
if (w !== undefined) {
|
|
377
|
+
return { kind: "wave", id, pred: gatesOf(w.phases), succ: w.phases.flatMap((x) => sources.get(x) ?? []), phases: [...w.phases] };
|
|
378
|
+
}
|
|
379
|
+
return null;
|
|
380
|
+
};
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
// ---------------------------------------------------------------------------------------------------------------
|
|
384
|
+
// Graph pass
|
|
385
|
+
|
|
386
|
+
/**
|
|
387
|
+
* `known` (QA-2.3-26): the ids the whole plan defines (tasks, phases, waves). When given, `ref-outside-run` is emitted only
|
|
388
|
+
* for a node in it; any other node is unknown to the plan and the parser already reported it (`graph-node-unknown`).
|
|
389
|
+
*/
|
|
390
|
+
export function graphEdges(
|
|
391
|
+
p: ParsedPlan, tasks: readonly EdgeTask[], known?: ReadonlySet<string>,
|
|
392
|
+
): { edges: Edge[]; warnings: CompileIssue[] } {
|
|
393
|
+
const sorted = [...tasks].sort(byPlanIndex);
|
|
394
|
+
const resolve = makeResolver(p, sorted, intraCore(p, sorted).sources);
|
|
395
|
+
const out = new EdgeSet();
|
|
396
|
+
const warnings: CompileIssue[] = [];
|
|
397
|
+
for (const g of p.graphs) {
|
|
398
|
+
for (const e of g.edges) {
|
|
399
|
+
const from = resolve(e.from);
|
|
400
|
+
const to = resolve(e.to);
|
|
401
|
+
// A node that is not a task, phase or wave of this run (a phase outside the run, or one the parser already
|
|
402
|
+
// reported as `graph-node-unknown`) drops the edge, loudly.
|
|
403
|
+
if (from === null || to === null) {
|
|
404
|
+
const missing = [from === null ? e.from : null, to === null ? e.to : null].filter((x): x is string => x !== null);
|
|
405
|
+
for (const m of missing) {
|
|
406
|
+
if (known !== undefined && !known.has(m)) continue;
|
|
407
|
+
warnings.push({ code: "ref-outside-run", line: e.line, message: `graph edge ${e.raw.trim()} dropped: ${m} is not part of this run` });
|
|
408
|
+
}
|
|
409
|
+
continue;
|
|
410
|
+
}
|
|
411
|
+
if (from.kind === to.kind && from.id === to.id) continue;
|
|
412
|
+
const evidence = evidenceOf("graph", e.line, e.raw);
|
|
413
|
+
for (const a of from.pred) for (const b of to.succ) out.add(mkEdge(a, b, "graph", "high", evidence));
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
return { edges: out.list(), warnings };
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
// ---------------------------------------------------------------------------------------------------------------
|
|
420
|
+
// Explicit pass
|
|
421
|
+
|
|
422
|
+
/** Does `t` touch (read or write) anything `c` writes? Returns the path that proves it. */
|
|
423
|
+
function touchedBy(c: EdgeTask, t: EdgeTask, ci: boolean): string | null {
|
|
424
|
+
const tAll = [...writesOf(t), ...conflictReadsOf(t)];
|
|
425
|
+
if (isTop(c)) return isTop(t) ? "*" : (tAll[0] ?? null);
|
|
426
|
+
if (isTop(t)) return writesOf(c)[0] ?? null;
|
|
427
|
+
return firstOverlap(writesOf(c), tAll, ci);
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
/** The more specific path of the first overlapping pair, or null. */
|
|
431
|
+
function firstOverlap(a: readonly string[], b: readonly string[], ci: boolean): string | null {
|
|
432
|
+
for (const x of a) {
|
|
433
|
+
for (const y of b) {
|
|
434
|
+
if (overlaps(x, y, ci)) return touchKey(x, ci).length >= touchKey(y, ci).length ? x : y;
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
return null;
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
export function explicitEdges(p: ParsedPlan, tasks: readonly EdgeTask[], ci = false): { edges: Edge[]; warnings: CompileIssue[] } {
|
|
441
|
+
const sorted = [...tasks].sort(byPlanIndex);
|
|
442
|
+
const resolve = makeResolver(p, sorted, intraCore(p, sorted).sources);
|
|
443
|
+
const out = new EdgeSet();
|
|
444
|
+
const warnings: CompileIssue[] = [];
|
|
445
|
+
const waveById = new Map(p.waves.map((w) => [w.id, w]));
|
|
446
|
+
|
|
447
|
+
const emit = (a: readonly string[], b: readonly string[], conf: Edge["confidence"], evidence: string): void => {
|
|
448
|
+
for (const x of a) for (const y of b) out.add(mkEdge(x, y, "explicit", conf, evidence));
|
|
449
|
+
};
|
|
450
|
+
const confidenceFor = (ref: Ref, text: string): Edge["confidence"] =>
|
|
451
|
+
ref.kind === "task" || MERGED_RE.test(text) ? "high" : "medium";
|
|
452
|
+
|
|
453
|
+
const waveMergedFirst = (ph: ExplicitPhrase, subject: Ref, ref: Ref | null, evidence: string): void => {
|
|
454
|
+
const wave = waveById.get(subject.id);
|
|
455
|
+
const contracts = ref === null
|
|
456
|
+
? []
|
|
457
|
+
: sorted.filter((t) => t.isContract && (ref.kind === "task" ? t.id === ref.id : ref.phases.includes(t.phase ?? "")));
|
|
458
|
+
if (wave === undefined || contracts.length === 0) {
|
|
459
|
+
warnings.push({
|
|
460
|
+
code: "explicit-merged-first-unresolved",
|
|
461
|
+
line: ph.line,
|
|
462
|
+
message: `"merged first" names no detectable contract task (residue candidate): ${excerpt(ph.excerpt)}`,
|
|
463
|
+
});
|
|
464
|
+
return;
|
|
465
|
+
}
|
|
466
|
+
const excluded = new Set(ref === null ? [] : ref.phases);
|
|
467
|
+
const targets = sorted.filter((t) => t.phase !== null && wave.phases.includes(t.phase) && !excluded.has(t.phase));
|
|
468
|
+
for (const c of contracts) {
|
|
469
|
+
for (const t of targets) {
|
|
470
|
+
if (t.id !== c.id && touchedBy(c, t, ci) !== null) out.add(mkEdge(c.id, t.id, "explicit", "medium", evidence));
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
};
|
|
474
|
+
|
|
475
|
+
for (const ph of p.phrases) {
|
|
476
|
+
const subject = resolve(ph.subject.id);
|
|
477
|
+
if (subject === null) {
|
|
478
|
+
warnings.push({ code: "explicit-ref-unknown", line: ph.line, message: `unknown subject ${ph.subject.id}: ${excerpt(ph.excerpt)}` });
|
|
479
|
+
continue;
|
|
480
|
+
}
|
|
481
|
+
const evidence = evidenceOf(`explicit-${ph.verb}`, ph.line, ph.excerpt);
|
|
482
|
+
if (ph.verb === "merged-first" && subject.kind === "wave") {
|
|
483
|
+
if (ph.refs.length === 0) waveMergedFirst(ph, subject, null, evidence);
|
|
484
|
+
for (const r of ph.refs) {
|
|
485
|
+
const ref = resolve(r);
|
|
486
|
+
if (ref === null) {
|
|
487
|
+
warnings.push({ code: "explicit-ref-unknown", line: ph.line, message: `unknown reference ${r}: ${excerpt(ph.excerpt)}` });
|
|
488
|
+
continue;
|
|
489
|
+
}
|
|
490
|
+
waveMergedFirst(ph, subject, ref, evidence);
|
|
491
|
+
}
|
|
492
|
+
continue;
|
|
493
|
+
}
|
|
494
|
+
if (ph.verb !== "after" && ph.verb !== "before" && ph.verb !== "merged-first") {
|
|
495
|
+
warnings.push({ code: "explicit-verb-unknown", line: ph.line, message: `unknown phrase verb ${ph.verb}: ${excerpt(ph.excerpt)}` });
|
|
496
|
+
continue;
|
|
497
|
+
}
|
|
498
|
+
for (const r of ph.refs) {
|
|
499
|
+
const ref = resolve(r);
|
|
500
|
+
if (ref === null) {
|
|
501
|
+
warnings.push({ code: "explicit-ref-unknown", line: ph.line, message: `unknown reference ${r}: ${excerpt(ph.excerpt)}` });
|
|
502
|
+
continue;
|
|
503
|
+
}
|
|
504
|
+
if (ph.verb === "before") emit(subject.pred, ref.succ, confidenceFor(ref, ph.excerpt), evidence);
|
|
505
|
+
else emit(ref.pred, subject.succ, confidenceFor(ref, ph.excerpt), evidence);
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
return { edges: out.list(), warnings };
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
// ---------------------------------------------------------------------------------------------------------------
|
|
512
|
+
// Contract pass
|
|
513
|
+
|
|
514
|
+
export function contractEdges(tasks: readonly EdgeTask[], reach: Reach, ci: boolean): Edge[] {
|
|
515
|
+
const sorted = [...tasks].sort(byPlanIndex);
|
|
516
|
+
const byPhase = groupByPhase(sorted);
|
|
517
|
+
const out: Edge[] = [];
|
|
518
|
+
for (const c of sorted) {
|
|
519
|
+
if (!c.isContract || c.phase === null) continue;
|
|
520
|
+
for (const t of byPhase.get(c.phase) ?? []) {
|
|
521
|
+
if (t.id === c.id || !reach.concurrent(c.id, t.id)) continue;
|
|
522
|
+
const cw = writesOf(c);
|
|
523
|
+
const tAll = [...writesOf(t), ...t.reads];
|
|
524
|
+
let path: string | null;
|
|
525
|
+
if (isTop(c)) path = isTop(t) ? "*" : (tAll[0] ?? null);
|
|
526
|
+
else if (isTop(t)) path = cw[0] ?? null;
|
|
527
|
+
else path = firstOverlap(cw, tAll, ci);
|
|
528
|
+
if (path === null) continue;
|
|
529
|
+
out.push(mkEdge(c.id, t.id, "contract", "medium", `contract: ${c.id} writes ${path}`));
|
|
530
|
+
reach.add(c.id, t.id);
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
return out;
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
// ---------------------------------------------------------------------------------------------------------------
|
|
537
|
+
// Conflict pass
|
|
538
|
+
|
|
539
|
+
function ancestorKeys(key: string): string[] {
|
|
540
|
+
const out: string[] = [];
|
|
541
|
+
let cut = key.lastIndexOf("/");
|
|
542
|
+
while (cut > 0) {
|
|
543
|
+
out.push(key.slice(0, cut));
|
|
544
|
+
cut = key.lastIndexOf("/", cut - 1);
|
|
545
|
+
}
|
|
546
|
+
if (key !== "") out.push("");
|
|
547
|
+
return out;
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
/** Path index: a path is keyed under itself (`exact`) and under every ancestor directory (`below`, root included). */
|
|
551
|
+
class PathIndex {
|
|
552
|
+
private readonly exact = new Map<string, number[]>();
|
|
553
|
+
private readonly below = new Map<string, number[]>();
|
|
554
|
+
constructor(private readonly ci: boolean) {}
|
|
555
|
+
|
|
556
|
+
private push(m: Map<string, number[]>, key: string, v: number): void {
|
|
557
|
+
const l = m.get(key);
|
|
558
|
+
if (l === undefined) m.set(key, [v]);
|
|
559
|
+
else l.push(v);
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
add(path: string, v: number): void {
|
|
563
|
+
const key = touchKey(path, this.ci);
|
|
564
|
+
this.push(this.exact, key, v);
|
|
565
|
+
for (const a of ancestorKeys(key)) this.push(this.below, a, v);
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
/** Every registered position whose path equals `path`, is a directory above it or lies below it. */
|
|
569
|
+
collect(path: string, into: Set<number>): void {
|
|
570
|
+
const key = touchKey(path, this.ci);
|
|
571
|
+
for (const k of [key, ...ancestorKeys(key)]) for (const v of this.exact.get(k) ?? []) into.add(v);
|
|
572
|
+
for (const v of this.below.get(key) ?? []) into.add(v);
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
/** First overlap of two sets, either of which may be the top element; `*` stands for "everything". */
|
|
577
|
+
function overlapOf(aTop: boolean, a: readonly string[], bTop: boolean, b: readonly string[], ci: boolean): string | null {
|
|
578
|
+
if (aTop) return bTop ? "*" : (b[0] ?? null);
|
|
579
|
+
if (bTop) return a[0] ?? null;
|
|
580
|
+
return firstOverlap(a, b, ci);
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
function classify(u: EdgeTask, v: EdgeTask, ci: boolean): string | null {
|
|
584
|
+
const uTop = isTop(u);
|
|
585
|
+
const vTop = isTop(v);
|
|
586
|
+
const ww = overlapOf(uTop, writesOf(u), vTop, writesOf(v), ci);
|
|
587
|
+
if (ww !== null) return `write/write ${ww}`;
|
|
588
|
+
const wr = overlapOf(uTop, writesOf(u), false, conflictReadsOf(v), ci);
|
|
589
|
+
if (wr !== null) return `write/read ${wr}`;
|
|
590
|
+
const rw = overlapOf(false, conflictReadsOf(u), vTop, writesOf(v), ci);
|
|
591
|
+
if (rw !== null) return `read/write ${rw}`;
|
|
592
|
+
return null;
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
function conflictConfidence(u: EdgeTask, v: EdgeTask): Edge["confidence"] {
|
|
596
|
+
if (isTop(u) || isTop(v)) return "low";
|
|
597
|
+
const strong = (c: Footprint["confidence"]): boolean => c === "explicit" || c === "ownership";
|
|
598
|
+
return strong(u.confidence) && strong(v.confidence) ? "high" : "medium";
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
export function conflictEdges(tasks: readonly EdgeTask[], reach: Reach, ci: boolean): Edge[] {
|
|
602
|
+
const ord = tasks.filter((t) => LAUNCHABLE.has(t.kind)).sort(byPlanIndex);
|
|
603
|
+
const index = new PathIndex(ci);
|
|
604
|
+
const tops: number[] = [];
|
|
605
|
+
const nonEmpty: number[] = [];
|
|
606
|
+
const out: Edge[] = [];
|
|
607
|
+
for (let j = 0; j < ord.length; j += 1) {
|
|
608
|
+
const v = at(ord, j);
|
|
609
|
+
const vPaths = [...writesOf(v), ...conflictReadsOf(v)];
|
|
610
|
+
const vHas = isTop(v) || vPaths.length > 0;
|
|
611
|
+
if (vHas) {
|
|
612
|
+
const cand = new Set<number>();
|
|
613
|
+
if (isTop(v)) for (const u of nonEmpty) cand.add(u);
|
|
614
|
+
else {
|
|
615
|
+
for (const path of vPaths) index.collect(path, cand);
|
|
616
|
+
for (const u of tops) cand.add(u);
|
|
617
|
+
}
|
|
618
|
+
// Nearest predecessor first: once linked, earlier ones are no longer concurrent with v (linear, not quadratic).
|
|
619
|
+
for (const ui of [...cand].sort((a, b) => b - a)) {
|
|
620
|
+
const u = at(ord, ui);
|
|
621
|
+
if (!reach.concurrent(u.id, v.id)) continue;
|
|
622
|
+
const why = classify(u, v, ci);
|
|
623
|
+
if (why === null) continue;
|
|
624
|
+
out.push(mkEdge(u.id, v.id, "conflict", conflictConfidence(u, v), `conflict: ${why}`));
|
|
625
|
+
reach.add(u.id, v.id);
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
if (!vHas) continue;
|
|
629
|
+
nonEmpty.push(j);
|
|
630
|
+
if (isTop(v)) tops.push(j);
|
|
631
|
+
else for (const path of vPaths) index.add(path, j);
|
|
632
|
+
}
|
|
633
|
+
return out;
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
// ---------------------------------------------------------------------------------------------------------------
|
|
637
|
+
// Cycles
|
|
638
|
+
|
|
639
|
+
/** The shortest cycle (node ids, closing edge implied) of the graph, or null. Tarjan SCC, then BFS inside each SCC. */
|
|
640
|
+
export function minimalCycle(nodes: readonly string[], edges: readonly Pick<Edge, "from" | "to">[]): string[] | null {
|
|
641
|
+
const idx = new Map<string, number>();
|
|
642
|
+
nodes.forEach((n, i) => { if (!idx.has(n)) idx.set(n, i); });
|
|
643
|
+
const n = nodes.length;
|
|
644
|
+
const adj: number[][] = nodes.map(() => []);
|
|
645
|
+
for (const e of edges) {
|
|
646
|
+
const a = idx.get(e.from);
|
|
647
|
+
const b = idx.get(e.to);
|
|
648
|
+
if (a !== undefined && b !== undefined) at(adj, a).push(b);
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
const order: number[] = new Array<number>(n).fill(-1);
|
|
652
|
+
const low: number[] = new Array<number>(n).fill(0);
|
|
653
|
+
const onStack: boolean[] = new Array<boolean>(n).fill(false);
|
|
654
|
+
const stack: number[] = [];
|
|
655
|
+
const comps: number[][] = [];
|
|
656
|
+
let counter = 0;
|
|
657
|
+
for (let s = 0; s < n; s += 1) {
|
|
658
|
+
if (at(order, s) !== -1) continue;
|
|
659
|
+
const work: Array<{ v: number; i: number }> = [{ v: s, i: 0 }];
|
|
660
|
+
order[s] = counter;
|
|
661
|
+
low[s] = counter;
|
|
662
|
+
counter += 1;
|
|
663
|
+
stack.push(s);
|
|
664
|
+
onStack[s] = true;
|
|
665
|
+
while (work.length > 0) {
|
|
666
|
+
const fr = at(work, work.length - 1);
|
|
667
|
+
const succ = at(adj, fr.v);
|
|
668
|
+
if (fr.i < succ.length) {
|
|
669
|
+
const w = at(succ, fr.i);
|
|
670
|
+
fr.i += 1;
|
|
671
|
+
if (at(order, w) === -1) {
|
|
672
|
+
order[w] = counter;
|
|
673
|
+
low[w] = counter;
|
|
674
|
+
counter += 1;
|
|
675
|
+
stack.push(w);
|
|
676
|
+
onStack[w] = true;
|
|
677
|
+
work.push({ v: w, i: 0 });
|
|
678
|
+
} else if (at(onStack, w)) {
|
|
679
|
+
low[fr.v] = Math.min(at(low, fr.v), at(order, w));
|
|
680
|
+
}
|
|
681
|
+
continue;
|
|
682
|
+
}
|
|
683
|
+
work.pop();
|
|
684
|
+
if (work.length > 0) {
|
|
685
|
+
const parent = at(work, work.length - 1).v;
|
|
686
|
+
low[parent] = Math.min(at(low, parent), at(low, fr.v));
|
|
687
|
+
}
|
|
688
|
+
if (at(low, fr.v) === at(order, fr.v)) {
|
|
689
|
+
const comp: number[] = [];
|
|
690
|
+
let w: number;
|
|
691
|
+
do {
|
|
692
|
+
w = stack.pop() as number;
|
|
693
|
+
onStack[w] = false;
|
|
694
|
+
comp.push(w);
|
|
695
|
+
} while (w !== fr.v);
|
|
696
|
+
comps.push(comp);
|
|
697
|
+
}
|
|
698
|
+
}
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
let best: string[] | null = null;
|
|
702
|
+
const better = (c: string[]): boolean => {
|
|
703
|
+
if (best === null || c.length < best.length) return true;
|
|
704
|
+
return c.length === best.length && c.join("\u0000") < best.join("\u0000");
|
|
705
|
+
};
|
|
706
|
+
const normalize = (ids: string[]): string[] => {
|
|
707
|
+
let m = 0;
|
|
708
|
+
for (let i = 1; i < ids.length; i += 1) if (at(ids, i) < at(ids, m)) m = i;
|
|
709
|
+
return [...ids.slice(m), ...ids.slice(0, m)];
|
|
710
|
+
};
|
|
711
|
+
for (const comp of comps) {
|
|
712
|
+
const members = new Set(comp);
|
|
713
|
+
if (comp.length === 1) {
|
|
714
|
+
const only = at(comp, 0);
|
|
715
|
+
if (at(adj, only).includes(only)) {
|
|
716
|
+
const c = [at(nodes, only)];
|
|
717
|
+
if (better(c)) best = c;
|
|
718
|
+
}
|
|
719
|
+
continue;
|
|
720
|
+
}
|
|
721
|
+
for (const s of [...comp].sort((a, b) => a - b)) {
|
|
722
|
+
const parent = new Map<number, number>([[s, -1]]);
|
|
723
|
+
const queue: number[] = [s];
|
|
724
|
+
let found = -1;
|
|
725
|
+
for (let qi = 0; qi < queue.length && found < 0; qi += 1) {
|
|
726
|
+
const u = at(queue, qi);
|
|
727
|
+
for (const w of at(adj, u)) {
|
|
728
|
+
if (!members.has(w)) continue;
|
|
729
|
+
if (w === s) {
|
|
730
|
+
found = u;
|
|
731
|
+
break;
|
|
732
|
+
}
|
|
733
|
+
if (!parent.has(w)) {
|
|
734
|
+
parent.set(w, u);
|
|
735
|
+
queue.push(w);
|
|
736
|
+
}
|
|
737
|
+
}
|
|
738
|
+
}
|
|
739
|
+
if (found < 0) continue;
|
|
740
|
+
const path: number[] = [];
|
|
741
|
+
for (let c = found; c !== -1; c = parent.get(c) as number) path.push(c);
|
|
742
|
+
path.reverse();
|
|
743
|
+
const c = normalize(path.map((i) => at(nodes, i)));
|
|
744
|
+
if (better(c)) best = c;
|
|
745
|
+
}
|
|
746
|
+
}
|
|
747
|
+
return best;
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
// ---------------------------------------------------------------------------------------------------------------
|
|
751
|
+
// All passes
|
|
752
|
+
|
|
753
|
+
export function buildEdges(
|
|
754
|
+
p: ParsedPlan,
|
|
755
|
+
tasks: readonly EdgeTask[],
|
|
756
|
+
o: { crossPhase: "gate" | "inferred"; ci: boolean; planNodes?: ReadonlySet<string> },
|
|
757
|
+
): { edges: Edge[]; warnings: CompileIssue[] } {
|
|
758
|
+
const sorted = [...tasks].sort(byPlanIndex);
|
|
759
|
+
const warnings: CompileIssue[] = [];
|
|
760
|
+
const set = new EdgeSet();
|
|
761
|
+
|
|
762
|
+
// 1. structural, 2. graph
|
|
763
|
+
for (const e of structuralEdges(p, sorted, o.crossPhase)) set.add(e);
|
|
764
|
+
warnings.push(...structuralWarnings(p, sorted));
|
|
765
|
+
const graph = graphEdges(p, sorted, o.planNodes);
|
|
766
|
+
for (const e of graph.edges) set.add(e);
|
|
767
|
+
warnings.push(...graph.warnings);
|
|
768
|
+
|
|
769
|
+
// Cycles among structural and graph edges are fatal (D6); every task also reaches the gate of its phase.
|
|
770
|
+
const taskIds = sorted.map((t) => t.id);
|
|
771
|
+
const phaseIds = p.phases.map((ph) => ph.id);
|
|
772
|
+
const nodes = [...taskIds, ...phaseIds.map(gateOf)];
|
|
773
|
+
const implicit: Array<Pick<Edge, "from" | "to">> = [];
|
|
774
|
+
for (const t of sorted) if (t.phase !== null && phaseIds.includes(t.phase)) implicit.push({ from: t.id, to: gateOf(t.phase) });
|
|
775
|
+
const cycle = minimalCycle(nodes, [...set.list(), ...implicit]);
|
|
776
|
+
if (cycle !== null) {
|
|
777
|
+
const evidence = cycle.map((from, i) => {
|
|
778
|
+
const to = at(cycle, (i + 1) % cycle.length);
|
|
779
|
+
return set.get(from, to)?.evidence ?? `phase-gate: ${from} -> ${to} (a task reaches the gate of its phase)`;
|
|
780
|
+
});
|
|
781
|
+
throw new ExecutorError("E_CYCLE", `dependency cycle: ${[...cycle, at(cycle, 0)].join(" -> ")}`, { cycle, evidence });
|
|
782
|
+
}
|
|
783
|
+
|
|
784
|
+
const phaseOfTask = new Map(sorted.map((t) => [t.id, t.phase]));
|
|
785
|
+
const reach = new Reach(taskIds, (id) => phaseOfTask.get(id) ?? null, phaseIds);
|
|
786
|
+
for (const e of set.list()) reach.add(e.from, e.to);
|
|
787
|
+
|
|
788
|
+
// 3. explicit: an edge that would close a cycle is dropped; the pair stays ordered the other way.
|
|
789
|
+
const explicit = explicitEdges(p, sorted, o.ci);
|
|
790
|
+
warnings.push(...explicit.warnings);
|
|
791
|
+
for (const e of explicit.edges) {
|
|
792
|
+
if (set.has(e.from, e.to)) continue;
|
|
793
|
+
if (e.from === e.to || reach.reaches(e.to, e.from)) {
|
|
794
|
+
warnings.push({
|
|
795
|
+
code: "explicit-edge-cycle-dropped",
|
|
796
|
+
line: lineOfEvidence(e.evidence),
|
|
797
|
+
message: `explicit edge ${e.from} -> ${e.to} would close a cycle and was dropped: ${e.evidence}`,
|
|
798
|
+
});
|
|
799
|
+
continue;
|
|
800
|
+
}
|
|
801
|
+
set.add(e);
|
|
802
|
+
reach.add(e.from, e.to);
|
|
803
|
+
}
|
|
804
|
+
|
|
805
|
+
// 4. contract, 5. conflict: only concurrent pairs, so no cycle can result.
|
|
806
|
+
for (const e of contractEdges(sorted, reach, o.ci)) set.add(e);
|
|
807
|
+
for (const e of conflictEdges(sorted, reach, o.ci)) set.add(e);
|
|
808
|
+
return { edges: set.list(), warnings };
|
|
809
|
+
}
|