opencode-model-router 2.2.0 → 2.4.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.
- package/CHANGELOG.md +214 -0
- package/README.md +90 -12
- package/package.json +4 -1
- package/src/commands/output.ts +24 -1
- package/src/compat/child-session.ts +43 -0
- package/src/compat/v2-client.ts +107 -7
- package/src/compat/v2-hooks.ts +1435 -44
- package/src/escalate/attempt-recorder.ts +263 -0
- package/src/escalate/ladder.ts +496 -10
- package/src/escalate/resume.ts +208 -0
- package/src/escalate/variants.ts +391 -0
- package/src/guard/enforce.ts +186 -22
- package/src/guard/guards.ts +132 -6
- package/src/guard/store.ts +5 -0
- package/src/index.ts +1193 -56
- package/src/router/build-info.ts +151 -0
- package/src/router/config.ts +1611 -19
- package/src/router/dispatch-header.ts +46 -4
- package/src/router/git-tools.ts +1152 -0
- package/src/router/guard-profile.ts +100 -0
- package/src/router/plugin-agents.ts +433 -0
- package/src/router/prompts.ts +26 -1
- package/src/router/protocol.ts +180 -0
- package/src/router/read-only.ts +149 -0
- package/src/router/role-agents.ts +409 -0
- package/src/router/roles-config.ts +429 -0
- package/src/router/roles.ts +600 -0
- package/src/router/run-tools.ts +913 -0
- package/src/router/sensitive-paths.ts +101 -0
- package/src/router/sessions.ts +533 -9
- package/src/router/subagents.ts +28 -1
- package/src/routing/advisor/findings.ts +898 -0
- package/src/routing/advisor/index.ts +614 -0
- package/src/routing/classify/backends/host.ts +163 -0
- package/src/routing/classify/backends/openai-compatible.ts +275 -0
- package/src/routing/classify/backends/shared.ts +741 -0
- package/src/routing/classify/backends/typesafe.ts +315 -0
- package/src/routing/classify/fences.ts +96 -0
- package/src/routing/classify/index.ts +594 -0
- package/src/routing/classify/route-line.ts +362 -0
- package/src/routing/classify/rules.ts +667 -0
- package/src/routing/classify/scrub.ts +214 -0
- package/src/routing/classify/state.ts +148 -0
- package/src/routing/classify/text.ts +20 -0
- package/src/routing/classify/types.ts +1310 -0
- package/src/routing/commands/annotate-plan.ts +652 -0
- package/src/routing/commands/stats.ts +134 -0
- package/src/routing/commands/v1-roles.ts +64 -0
- package/src/routing/engine/index.ts +32 -0
- package/src/routing/engine/kernel.ts +760 -0
- package/src/routing/engine/ladders.ts +802 -0
- package/src/routing/engine/plan.ts +404 -0
- package/src/routing/engine/protocol-line.ts +150 -0
- package/src/routing/engine/roles-taxonomy.ts +52 -0
- package/src/routing/engine/simulate.ts +167 -0
- package/src/routing/engine/types.ts +368 -0
- package/src/routing/file-lock.ts +53 -0
- package/src/routing/outcomes/beta.ts +147 -0
- package/src/routing/outcomes/cost.ts +313 -0
- package/src/routing/outcomes/index.ts +215 -0
- package/src/routing/outcomes/ingest.ts +960 -0
- package/src/routing/outcomes/persist.ts +1080 -0
- package/src/routing/outcomes/signals.ts +650 -0
- package/src/routing/outcomes/stats.ts +873 -0
- package/src/routing/outcomes/store.ts +673 -0
- package/src/routing/outcomes/types.ts +1093 -0
- package/src/routing/roles/authority.ts +495 -0
- package/src/routing/roles/binding.ts +543 -0
- package/src/routing/roles/policy.ts +416 -0
- package/src/routing/roles/work-root.ts +132 -0
- package/src/routing/wire/dispatch.ts +2088 -0
- package/src/routing/wire/hint.ts +222 -0
- package/src/routing/wire/host-info.ts +256 -0
- package/src/routing/wire/runtime.ts +348 -0
- package/src/v2.ts +40 -1
- package/src/verify/checker.ts +249 -7
- package/src/verify/deterministic.ts +10 -1
- package/src/verify/dispatch.ts +63 -6
- package/src/verify/dod.ts +127 -10
- package/src/verify/gate.ts +75 -7
- package/src/verify/types.ts +12 -0
- package/src/verify/wiring.ts +55 -10
- package/tiers.json +13 -5
|
@@ -0,0 +1,362 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `[route …]` line (D13) — parse, strip and apply. Design L1–L3 of
|
|
3
|
+
* `docs/qa/cost-aware-routing/phase-1.2.md`.
|
|
4
|
+
*
|
|
5
|
+
* Pure. The route line is a typed override written by the orchestrator or by
|
|
6
|
+
* `/annotate-plan`; it is read from its own line only (a `[route …]` mention
|
|
7
|
+
* inside a sentence is text) and every route line is stripped from the prompt.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { fenceMask } from "./fences";
|
|
11
|
+
import {
|
|
12
|
+
CLASS_IMPLIED_NEEDS,
|
|
13
|
+
CONFIDENCE,
|
|
14
|
+
DETECTIONS,
|
|
15
|
+
NEEDS,
|
|
16
|
+
RISKS,
|
|
17
|
+
ROUTE_LINE_MAX_CHARS,
|
|
18
|
+
ROUTE_LINE_RE,
|
|
19
|
+
SCOPES,
|
|
20
|
+
TASK_CLASSES,
|
|
21
|
+
type Detection,
|
|
22
|
+
type Need,
|
|
23
|
+
type Risk,
|
|
24
|
+
type RouteLine,
|
|
25
|
+
type RouteLineParse,
|
|
26
|
+
type Scope,
|
|
27
|
+
type TaskClass,
|
|
28
|
+
type TaskFacts,
|
|
29
|
+
} from "./types";
|
|
30
|
+
|
|
31
|
+
const ROUTE_MENTION_RE = /\[route/i;
|
|
32
|
+
/**
|
|
33
|
+
* `needs=shell, edit , network`: whitespace around the commas of a needs list is
|
|
34
|
+
* not a separator (QA-1.2-14). A word followed by `=` starts the next field, so
|
|
35
|
+
* `needs=shell, class=debug` keeps its two fields.
|
|
36
|
+
*/
|
|
37
|
+
const NEEDS_LIST_RE = /(\bneeds=[a-z_]*)((?:\s*,\s*[a-z_]+(?![a-z_]|\s*=))+)/gi;
|
|
38
|
+
const LINE_SPLIT_RE = /(\r\n|\n|\r)/;
|
|
39
|
+
/** `root="D:\my dir"`: the only way to carry whitespace in a value; extracted before tokenising. */
|
|
40
|
+
const QUOTED_ROOT_RE = /\broot\s*=\s*"([^"]*)"/gi;
|
|
41
|
+
const QUOTED_PLACEHOLDER_RE = /^@q(\d+)$/;
|
|
42
|
+
/** `tier=` literals the parser can know; the engine validates against the active tier list. */
|
|
43
|
+
const TIER_NAMES = ["fast", "medium", "heavy"] as const;
|
|
44
|
+
export const ROUTE_BUDGET_MAX = 10000;
|
|
45
|
+
/** A drive-absolute Windows path (`D:\…`, `D:/…`) or a POSIX path with exactly one leading `/`. */
|
|
46
|
+
const WORK_ROOT_RE = /^(?:[A-Za-z]:[\\/]|\/(?![\\/]))/;
|
|
47
|
+
/** A `..` path segment, with either separator. */
|
|
48
|
+
const PARENT_SEGMENT_RE = /(?:^|[\\/])\.\.(?:[\\/]|$)/;
|
|
49
|
+
/**
|
|
50
|
+
* A first line that starts like a route line (`[route` + a separator, `]` or the end) but is not one (QA-P12-1-6).
|
|
51
|
+
* `[router …]` and friends are other tags, not malformed route lines.
|
|
52
|
+
*/
|
|
53
|
+
const ROUTE_START_RE = /^[ \t]*\[route(?![A-Za-z0-9_-])/i;
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* A role work root (QA-P12-1-6): a local, drive-absolute Windows path or a POSIX path. Rejected: relative and
|
|
57
|
+
* drive-relative paths, every `\\` or `//` prefix (UNC `\\host\share`, `\\?\`, `\\.\` device paths — a work root is a
|
|
58
|
+
* local worktree), any `..` segment, and NUL.
|
|
59
|
+
*/
|
|
60
|
+
function isWorkRootPath(value: string): boolean {
|
|
61
|
+
return WORK_ROOT_RE.test(value) && !PARENT_SEGMENT_RE.test(value) && !value.includes("\0");
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function isMember<T extends string>(values: readonly T[], value: string): value is T {
|
|
65
|
+
return (values as readonly string[]).includes(value);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function stripTail(value: string): string {
|
|
69
|
+
return value.replace(/[,;]+$/, "");
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** `pin` values: bare / true / yes / 1 → true; false / no / 0 → false; anything else → undefined. */
|
|
73
|
+
function parsePin(value: string | null): boolean | undefined {
|
|
74
|
+
if (value === null) return true;
|
|
75
|
+
if (value === "true" || value === "yes" || value === "1") return true;
|
|
76
|
+
if (value === "false" || value === "no" || value === "0") return false;
|
|
77
|
+
return undefined;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function parseNeeds(value: string): readonly Need[] | undefined {
|
|
81
|
+
const found = new Set<Need>();
|
|
82
|
+
for (const token of value.split(",")) {
|
|
83
|
+
const t = token.trim();
|
|
84
|
+
if (isMember(NEEDS, t)) found.add(t);
|
|
85
|
+
}
|
|
86
|
+
if (found.size === 0) return undefined;
|
|
87
|
+
return NEEDS.filter((need) => found.has(need));
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** L2: the fields of one route line body. */
|
|
91
|
+
function parseFields(body: string): RouteLine {
|
|
92
|
+
let taskClass: TaskClass | undefined;
|
|
93
|
+
let risk: Risk | undefined;
|
|
94
|
+
let scope: Scope | undefined;
|
|
95
|
+
let needs: readonly Need[] | undefined;
|
|
96
|
+
let detection: Detection | undefined;
|
|
97
|
+
let pin = false;
|
|
98
|
+
let tier: string | undefined;
|
|
99
|
+
let budget: number | undefined;
|
|
100
|
+
let root: string | undefined;
|
|
101
|
+
const ignored: string[] = [];
|
|
102
|
+
const seen = new Set<string>();
|
|
103
|
+
|
|
104
|
+
const quotedRoots: string[] = [];
|
|
105
|
+
const tokens = body
|
|
106
|
+
.replace(QUOTED_ROOT_RE, (_whole, path: string) => `root=@q${quotedRoots.push(path) - 1}`)
|
|
107
|
+
.replace(/\s*=\s*/g, "=")
|
|
108
|
+
.replace(NEEDS_LIST_RE, (_whole, head: string, tail: string) => head + tail.replace(/\s+/g, ""))
|
|
109
|
+
.split(/\s+/)
|
|
110
|
+
.filter((token) => token !== "");
|
|
111
|
+
for (const token of tokens) {
|
|
112
|
+
const eq = token.indexOf("=");
|
|
113
|
+
const key = stripTail((eq === -1 ? token : token.slice(0, eq)).toLowerCase());
|
|
114
|
+
const rawValue = eq === -1 ? null : token.slice(eq + 1);
|
|
115
|
+
const value = rawValue === null ? null : stripTail(rawValue.toLowerCase());
|
|
116
|
+
if (key === "") {
|
|
117
|
+
ignored.push(token);
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
if (seen.has(key)) {
|
|
121
|
+
ignored.push(`dup:${key}`);
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
seen.add(key);
|
|
125
|
+
switch (key) {
|
|
126
|
+
case "class":
|
|
127
|
+
if (value !== null && isMember(TASK_CLASSES, value)) taskClass = value;
|
|
128
|
+
else ignored.push(key);
|
|
129
|
+
break;
|
|
130
|
+
case "risk":
|
|
131
|
+
if (value !== null && isMember(RISKS, value)) risk = value;
|
|
132
|
+
else ignored.push(key);
|
|
133
|
+
break;
|
|
134
|
+
case "scope":
|
|
135
|
+
if (value !== null && isMember(SCOPES, value)) scope = value;
|
|
136
|
+
else ignored.push(key);
|
|
137
|
+
break;
|
|
138
|
+
case "needs": {
|
|
139
|
+
const parsed = value === null ? undefined : parseNeeds(value);
|
|
140
|
+
if (parsed) needs = parsed;
|
|
141
|
+
else ignored.push(key);
|
|
142
|
+
break;
|
|
143
|
+
}
|
|
144
|
+
case "d":
|
|
145
|
+
if (value !== null && isMember(DETECTIONS, value)) detection = value;
|
|
146
|
+
else ignored.push(key);
|
|
147
|
+
break;
|
|
148
|
+
case "pin": {
|
|
149
|
+
const parsed = parsePin(value);
|
|
150
|
+
if (parsed === undefined) ignored.push(key);
|
|
151
|
+
else pin = parsed;
|
|
152
|
+
break;
|
|
153
|
+
}
|
|
154
|
+
case "tier":
|
|
155
|
+
if (value !== null && isMember(TIER_NAMES, value)) tier = value;
|
|
156
|
+
else ignored.push(key);
|
|
157
|
+
break;
|
|
158
|
+
case "budget": {
|
|
159
|
+
const n = value !== null && /^\d+$/.test(value) ? Number(value) : 0;
|
|
160
|
+
if (n >= 1 && n <= ROUTE_BUDGET_MAX) budget = n;
|
|
161
|
+
else ignored.push(key);
|
|
162
|
+
break;
|
|
163
|
+
}
|
|
164
|
+
case "root": {
|
|
165
|
+
const q = value === null ? null : QUOTED_PLACEHOLDER_RE.exec(value);
|
|
166
|
+
const candidate = q ? quotedRoots[Number(q[1])] : rawValue === null ? undefined : stripTail(rawValue);
|
|
167
|
+
if (candidate !== undefined && isWorkRootPath(candidate)) root = candidate;
|
|
168
|
+
else ignored.push(key);
|
|
169
|
+
break;
|
|
170
|
+
}
|
|
171
|
+
default:
|
|
172
|
+
ignored.push(key);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
return {
|
|
177
|
+
...(taskClass ? { class: taskClass } : {}),
|
|
178
|
+
...(risk ? { risk } : {}),
|
|
179
|
+
...(scope ? { scope } : {}),
|
|
180
|
+
...(needs ? { needs } : {}),
|
|
181
|
+
...(detection ? { detection } : {}),
|
|
182
|
+
...(tier ? { tier } : {}),
|
|
183
|
+
...(budget !== undefined ? { budget } : {}),
|
|
184
|
+
...(root ? { root } : {}),
|
|
185
|
+
pin,
|
|
186
|
+
ignored,
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/** Leading whitespace of 4+ columns (tabs count 4): an indented code block, not a directive. */
|
|
191
|
+
const INDENTED_CODE_RE = /^(?: {0,3}\t| {4})/;
|
|
192
|
+
const QUOTED_RE = /^\s*>/;
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Only a plain, unquoted, unindented, outside-any-fence, short line can be a
|
|
196
|
+
* route line: anything else is text quoted or pasted into the prompt (a file,
|
|
197
|
+
* a log, an issue) and must neither steer routing nor be stripped.
|
|
198
|
+
*/
|
|
199
|
+
function isRecognisable(line: string, inFence: boolean): boolean {
|
|
200
|
+
return (
|
|
201
|
+
!inFence &&
|
|
202
|
+
line.length <= ROUTE_LINE_MAX_CHARS &&
|
|
203
|
+
!INDENTED_CODE_RE.test(line) &&
|
|
204
|
+
!QUOTED_RE.test(line) &&
|
|
205
|
+
ROUTE_LINE_RE.test(line)
|
|
206
|
+
);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
function canonical(line: RouteLine): string {
|
|
210
|
+
return JSON.stringify([line.class, line.risk, line.scope, line.needs, line.detection, line.pin, line.tier, line.budget, line.root]);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Several route lines: when they differ in any field the parse is a conflict.
|
|
215
|
+
* The effective line keeps the FIRST line's class/risk/scope/needs only where no
|
|
216
|
+
* other line carries a different value for the same field, keeps the first
|
|
217
|
+
* line's own `pin` (a later line can neither add one nor take it away, A22) and
|
|
218
|
+
* never keeps `d` (a conflict must not be able to switch verification off).
|
|
219
|
+
*/
|
|
220
|
+
function resolveConflict(lines: readonly RouteLine[]): RouteLine {
|
|
221
|
+
const first = lines[0]!;
|
|
222
|
+
const ignored = [...first.ignored, "conflict"];
|
|
223
|
+
const contradicted = (pick: (l: RouteLine) => unknown): boolean => {
|
|
224
|
+
const mine = JSON.stringify(pick(first));
|
|
225
|
+
return lines.some((other) => {
|
|
226
|
+
const theirs = pick(other);
|
|
227
|
+
return theirs !== undefined && JSON.stringify(theirs) !== mine;
|
|
228
|
+
});
|
|
229
|
+
};
|
|
230
|
+
const keep = <T>(field: string, value: T | undefined, pick: (l: RouteLine) => unknown): T | undefined => {
|
|
231
|
+
if (value === undefined) return undefined;
|
|
232
|
+
if (contradicted(pick)) {
|
|
233
|
+
ignored.push(`conflict:${field}`);
|
|
234
|
+
return undefined;
|
|
235
|
+
}
|
|
236
|
+
return value;
|
|
237
|
+
};
|
|
238
|
+
const taskClass = keep("class", first.class, (l) => l.class);
|
|
239
|
+
const risk = keep("risk", first.risk, (l) => l.risk);
|
|
240
|
+
const scope = keep("scope", first.scope, (l) => l.scope);
|
|
241
|
+
const needs = keep("needs", first.needs, (l) => l.needs);
|
|
242
|
+
const tier = keep("tier", first.tier, (l) => l.tier);
|
|
243
|
+
const budget = keep("budget", first.budget, (l) => l.budget);
|
|
244
|
+
const root = keep("root", first.root, (l) => l.root);
|
|
245
|
+
if (lines.some((l) => l.detection !== undefined)) ignored.push("conflict:d");
|
|
246
|
+
return {
|
|
247
|
+
...(taskClass ? { class: taskClass } : {}),
|
|
248
|
+
...(risk ? { risk } : {}),
|
|
249
|
+
...(scope ? { scope } : {}),
|
|
250
|
+
...(needs ? { needs } : {}),
|
|
251
|
+
...(tier ? { tier } : {}),
|
|
252
|
+
...(budget !== undefined ? { budget } : {}),
|
|
253
|
+
...(root ? { root } : {}),
|
|
254
|
+
// The first line is the directive; a later line cannot take its pin away or add one (A22).
|
|
255
|
+
pin: first.pin,
|
|
256
|
+
ignored,
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* Where a route line is recognised (A22): `first` = only the first non-empty line
|
|
262
|
+
* of the text (the protocol: the orchestrator's directive is the first line),
|
|
263
|
+
* `edges` = the first or the last non-empty line, `any` = anywhere outside
|
|
264
|
+
* fences, quotes and indented code. Elsewhere a `[route …]` line is plain text
|
|
265
|
+
* and stays in the prompt.
|
|
266
|
+
*/
|
|
267
|
+
export type RouteLinePositions = "first" | "edges" | "any";
|
|
268
|
+
|
|
269
|
+
export interface RouteLineOptions {
|
|
270
|
+
/** Default `any` for the parser itself; `classify` defaults to `first`. */
|
|
271
|
+
readonly positions?: RouteLinePositions;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* L1 + L2: find every recognisable route line, parse them, strip them. Route
|
|
276
|
+
* lines are whole lines; the terminator that follows a dropped line goes with
|
|
277
|
+
* it, everything else is kept byte for byte (including route-looking lines in
|
|
278
|
+
* fences, indented code and quotes).
|
|
279
|
+
*/
|
|
280
|
+
export function parseRouteLine(text: string, options: RouteLineOptions = {}): RouteLineParse {
|
|
281
|
+
if (typeof text !== "string") {
|
|
282
|
+
return { line: null, count: 0, stripped: "", conflict: false, edgeOnly: true };
|
|
283
|
+
}
|
|
284
|
+
if (!ROUTE_MENTION_RE.test(text)) {
|
|
285
|
+
return { line: null, count: 0, stripped: text, conflict: false, edgeOnly: true };
|
|
286
|
+
}
|
|
287
|
+
const positions = options.positions ?? "any";
|
|
288
|
+
const parts = text.split(LINE_SPLIT_RE);
|
|
289
|
+
const lines: string[] = [];
|
|
290
|
+
for (let i = 0; i < parts.length; i += 2) lines.push(parts[i]!);
|
|
291
|
+
const fenced = fenceMask(lines);
|
|
292
|
+
|
|
293
|
+
let firstNonEmpty = -1;
|
|
294
|
+
let lastNonEmpty = -1;
|
|
295
|
+
lines.forEach((line, i) => {
|
|
296
|
+
if (line.trim() === "") return;
|
|
297
|
+
if (firstNonEmpty === -1) firstNonEmpty = i;
|
|
298
|
+
lastNonEmpty = i;
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
// QA-P12-1-6: a first non-empty line that starts like a route line but does not parse is flagged, never silently
|
|
302
|
+
// dropped. It stays text (not stripped, not applied), so tier-mode facts are unchanged.
|
|
303
|
+
const head = firstNonEmpty >= 0 ? lines[firstNonEmpty]! : "";
|
|
304
|
+
const malformed = firstNonEmpty >= 0 && !fenced[firstNonEmpty] && !INDENTED_CODE_RE.test(head)
|
|
305
|
+
&& ROUTE_START_RE.test(head) && !isRecognisable(head, false);
|
|
306
|
+
|
|
307
|
+
const kept: string[] = [];
|
|
308
|
+
const parsed: RouteLine[] = [];
|
|
309
|
+
let edgeOnly = true;
|
|
310
|
+
for (let i = 0; i < lines.length; i++) {
|
|
311
|
+
const terminator = parts[2 * i + 1];
|
|
312
|
+
const line = lines[i]!;
|
|
313
|
+
const atEdge = i === firstNonEmpty || i === lastNonEmpty;
|
|
314
|
+
const placed = positions === "any" || (positions === "edges" ? atEdge : i === firstNonEmpty);
|
|
315
|
+
if (!placed || !isRecognisable(line, fenced[i]!)) {
|
|
316
|
+
kept.push(line);
|
|
317
|
+
if (terminator !== undefined) kept.push(terminator);
|
|
318
|
+
continue;
|
|
319
|
+
}
|
|
320
|
+
if (!atEdge) edgeOnly = false;
|
|
321
|
+
parsed.push(parseFields(ROUTE_LINE_RE.exec(line)?.[1] ?? ""));
|
|
322
|
+
}
|
|
323
|
+
if (parsed.length === 0) {
|
|
324
|
+
return { line: null, count: 0, stripped: kept.join(""), conflict: false, edgeOnly: true, ...(malformed ? { malformed } : {}) };
|
|
325
|
+
}
|
|
326
|
+
const conflict = new Set(parsed.map(canonical)).size > 1;
|
|
327
|
+
return {
|
|
328
|
+
// QA-P12-2-2: the flag lives on the parse result only; the line's `ignored` list is the tier-mode one.
|
|
329
|
+
line: conflict ? resolveConflict(parsed) : parsed[0]!,
|
|
330
|
+
count: parsed.length,
|
|
331
|
+
stripped: kept.join(""),
|
|
332
|
+
conflict,
|
|
333
|
+
edgeOnly,
|
|
334
|
+
...(malformed ? { malformed } : {}),
|
|
335
|
+
};
|
|
336
|
+
}
|
|
337
|
+
const RISK_ORDER: readonly Risk[] = RISKS;
|
|
338
|
+
|
|
339
|
+
function maxRisk(a: Risk, b: Risk): Risk {
|
|
340
|
+
return RISK_ORDER.indexOf(a) >= RISK_ORDER.indexOf(b) ? a : b;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/**
|
|
344
|
+
* L3: merge a route line over rules facts. Class and scope override; risk may
|
|
345
|
+
* only rise; needs are unioned (and completed by the class's implied needs and
|
|
346
|
+
* by `network` ⇒ `shell`). A valid class makes the route line the source and
|
|
347
|
+
* sets confidence 0.9; without one, confidence and source stay the base's.
|
|
348
|
+
*/
|
|
349
|
+
export function applyRouteLine(base: TaskFacts, line: RouteLine): TaskFacts {
|
|
350
|
+
const taskClass = line.class ?? base.class;
|
|
351
|
+
const needs = new Set<Need>([...base.needs, ...(line.needs ?? []), ...CLASS_IMPLIED_NEEDS[taskClass]]);
|
|
352
|
+
if (needs.has("network")) needs.add("shell");
|
|
353
|
+
const hasClass = line.class !== undefined;
|
|
354
|
+
return {
|
|
355
|
+
class: taskClass,
|
|
356
|
+
risk: maxRisk(base.risk, line.risk ?? base.risk),
|
|
357
|
+
scope: line.scope ?? base.scope,
|
|
358
|
+
needs: NEEDS.filter((need) => needs.has(need)),
|
|
359
|
+
confidence: hasClass ? CONFIDENCE.routeLine : base.confidence,
|
|
360
|
+
source: hasClass ? (line.detection ? "plan" : "route-line") : base.source,
|
|
361
|
+
};
|
|
362
|
+
}
|