opencode-model-router 2.7.0 → 2.9.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 +583 -0
- package/README.md +20 -0
- package/package.json +5 -1
- package/src/commands/approvals.ts +354 -0
- package/src/commands/output.ts +34 -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 +2853 -109
- 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 +1789 -288
- package/src/router/config.ts +823 -26
- package/src/router/dispatch-header.ts +95 -2
- package/src/router/git-tools.ts +266 -19
- package/src/router/leases.ts +1469 -0
- package/src/router/logger.ts +44 -32
- package/src/router/protocol.ts +44 -10
- package/src/router/role-agents.ts +334 -20
- package/src/router/roles-config.ts +93 -1
- package/src/router/roles.ts +1 -1
- package/src/router/run-tools.ts +56 -4
- package/src/router/sessions.ts +11 -0
- 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/route-line.ts +96 -5
- package/src/routing/classify/rules.ts +11 -0
- package/src/routing/classify/types.ts +26 -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/signals.ts +17 -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/admission.ts +1028 -0
- package/src/routing/roles/approvals.ts +957 -0
- package/src/routing/roles/binding.ts +116 -13
- package/src/routing/roles/check-placement.ts +112 -0
- package/src/routing/roles/git-root.ts +500 -0
- package/src/routing/roles/orphans.ts +68 -0
- package/src/routing/roles/policy.ts +30 -3
- package/src/routing/roles/read-set.ts +325 -0
- package/src/routing/roles/work-root.ts +12 -2
- package/src/routing/wire/dispatch.ts +697 -30
- package/src/routing/wire/hint.ts +56 -8
- package/src/routing/wire/working-set.ts +58 -0
- package/src/util/linear-trim.ts +32 -0
- package/src/v2.ts +94 -49
- package/src/verify/batch.ts +7 -2
- package/src/verify/branch.ts +141 -0
- package/src/verify/canonical-path.ts +35 -0
- package/src/verify/deterministic.ts +268 -17
- package/src/verify/dispatch.ts +348 -37
- package/src/verify/dod.ts +46 -0
- package/src/verify/edges.ts +817 -0
- package/src/verify/exec.ts +52 -9
- package/src/verify/gate.ts +349 -16
- package/src/verify/git-top.ts +43 -0
- package/src/verify/head.ts +107 -0
- package/src/verify/pending.ts +1846 -18
- package/src/verify/perimeter.ts +1186 -0
- package/src/verify/pin.ts +109 -0
- package/src/verify/plan-task.ts +729 -0
- package/src/verify/reference.ts +297 -8
- package/src/verify/risk.ts +16 -3
- package/src/verify/runner.ts +706 -32
- package/src/verify/tree.ts +372 -16
- package/src/verify/types.ts +66 -2
- package/src/verify/wiring.ts +1594 -46
|
@@ -37,6 +37,8 @@ import { randomUUID } from "node:crypto";
|
|
|
37
37
|
import { isAbsolute } from "node:path";
|
|
38
38
|
import type { AuthorityAction } from "../../router/roles";
|
|
39
39
|
import { GRANT_NOTES, type DispatchGrant } from "./policy";
|
|
40
|
+
import { leaseTaskRef } from "../../executor/types";
|
|
41
|
+
import { forgetExecutorChildRound } from "../../executor/child-guards";
|
|
40
42
|
|
|
41
43
|
export interface PendingDispatch {
|
|
42
44
|
parentSessionID: string;
|
|
@@ -56,10 +58,30 @@ export type SessionLookup = (
|
|
|
56
58
|
id: string,
|
|
57
59
|
) => Promise<{ parentID?: string; agent?: string; title?: string; firstText?: string } | undefined>;
|
|
58
60
|
|
|
61
|
+
/** The plan task a plan child works on (A3 handoff, 3.1.5): the 2.1 guard joins plan-child leases with this identity. */
|
|
62
|
+
export interface PlanChildIdentity {
|
|
63
|
+
planId: string;
|
|
64
|
+
taskId: string;
|
|
65
|
+
attempt: number;
|
|
66
|
+
/** `leaseTaskRef(planId, taskId)`: the owner of the child's leases. */
|
|
67
|
+
taskRef: string;
|
|
68
|
+
scope: "until-integrated";
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export type UnknownBindingCause = "unregistered" | "retired" | "foreign" | "claimed" | "markers" | "noNonce" | "noParent" | "lookupFailed";
|
|
72
|
+
|
|
59
73
|
export interface Binding {
|
|
74
|
+
/** Resolved executor route tier; its budget and cumulative ceiling must not fall back to the role floor. */
|
|
75
|
+
tier?: string;
|
|
76
|
+
/** Set only for a plan child ({@link bindPlanChild}). */
|
|
77
|
+
role?: string;
|
|
78
|
+
/** Set only for a plan child ({@link bindPlanChild}). */
|
|
79
|
+
plan?: PlanChildIdentity;
|
|
60
80
|
childSessionID: string;
|
|
61
81
|
/** `intersection` is part of the contract but never produced under R7 (ambiguity → unknown). */
|
|
62
82
|
kind: "exact" | "intersection" | "unknown";
|
|
83
|
+
/** Set only for an unknown binding: why the session could not be matched to its dispatch. */
|
|
84
|
+
cause?: UnknownBindingCause;
|
|
63
85
|
grant: DispatchGrant;
|
|
64
86
|
/** callIDs of the pending dispatches that matched. */
|
|
65
87
|
candidates: readonly string[];
|
|
@@ -275,8 +297,19 @@ function restrict(
|
|
|
275
297
|
return { actions: kept, notes: [...new Set([...notes, ...extra])], workRoot: root };
|
|
276
298
|
}
|
|
277
299
|
|
|
300
|
+
/** Cross-repo (P1.4): a grant's repository location travels with its work root, and only with a validated (non-null) one. */
|
|
301
|
+
function locationOf(from: Pick<DispatchGrant, "workRoot" | "location">, workRoot: string | null): Pick<DispatchGrant, "location"> {
|
|
302
|
+
return workRoot !== null && workRoot === from.workRoot && from.location !== undefined ? { location: from.location } : {};
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/** {@link restrict} of a stored grant, keeping its location (P1.4). */
|
|
306
|
+
function withLocation(grant: DispatchGrant, from: DispatchGrant): DispatchGrant {
|
|
307
|
+
const kept = locationOf(from, grant.workRoot);
|
|
308
|
+
return kept.location === undefined ? grant : { ...grant, ...kept };
|
|
309
|
+
}
|
|
310
|
+
|
|
278
311
|
function copyGrant(grant: DispatchGrant): DispatchGrant {
|
|
279
|
-
return { actions: ordered(grant.actions), notes: [...grant.notes], workRoot: grant.workRoot };
|
|
312
|
+
return { actions: ordered(grant.actions), notes: [...grant.notes], workRoot: grant.workRoot, ...locationOf(grant, grant.workRoot) };
|
|
280
313
|
}
|
|
281
314
|
|
|
282
315
|
/**
|
|
@@ -288,7 +321,7 @@ function view(binding: Binding, max: ReadonlySet<AuthorityAction>): Binding {
|
|
|
288
321
|
const { actions, notes, workRoot } = binding.grant;
|
|
289
322
|
return {
|
|
290
323
|
...binding,
|
|
291
|
-
grant: restrict(actions, notes, workRoot, max, binding.kind !== "unknown"),
|
|
324
|
+
grant: withLocation(restrict(actions, notes, workRoot, max, binding.kind !== "unknown"), binding.grant),
|
|
292
325
|
candidates: [...binding.candidates],
|
|
293
326
|
};
|
|
294
327
|
}
|
|
@@ -301,11 +334,12 @@ function exact(childSessionID: string, entry: PendingDispatch): Binding {
|
|
|
301
334
|
}
|
|
302
335
|
|
|
303
336
|
/** Unknown: the local class, narrowed by every view to max ∩ local (I9) — never derived from a dispatch grant. */
|
|
304
|
-
function unknown(childSessionID: string, cause: string): Binding {
|
|
337
|
+
function unknown(childSessionID: string, cause: UnknownBindingCause, note: string): Binding {
|
|
305
338
|
return {
|
|
306
339
|
childSessionID,
|
|
307
340
|
kind: "unknown",
|
|
308
|
-
|
|
341
|
+
cause,
|
|
342
|
+
grant: { actions: ordered(LOCAL_ACTIONS), notes: [BINDING_NOTES.unknown, note], workRoot: null },
|
|
309
343
|
candidates: [],
|
|
310
344
|
decisionID: null,
|
|
311
345
|
budget: null,
|
|
@@ -339,31 +373,34 @@ async function decide(childSessionID: string, getSession: SessionLookup): Promis
|
|
|
339
373
|
session = undefined;
|
|
340
374
|
}
|
|
341
375
|
if (typeof session !== "object" || session === null) {
|
|
342
|
-
return { binding: unknown(childSessionID, BINDING_NOTES.lookupFailed), parentSessionID: undefined, agent: undefined, cacheable: false };
|
|
376
|
+
return { binding: unknown(childSessionID, "lookupFailed", BINDING_NOTES.lookupFailed), parentSessionID: undefined, agent: undefined, cacheable: false };
|
|
343
377
|
}
|
|
344
378
|
const parent = text(session.parentID);
|
|
345
379
|
const agent = text(session.agent);
|
|
346
380
|
if (parent === undefined || agent === undefined) {
|
|
347
|
-
return { binding: unknown(childSessionID, BINDING_NOTES.noParent), parentSessionID: parent, agent, cacheable: false };
|
|
381
|
+
return { binding: unknown(childSessionID, "noParent", BINDING_NOTES.noParent), parentSessionID: parent, agent, cacheable: false };
|
|
348
382
|
}
|
|
349
383
|
const out = (binding: Binding): Decision => ({ binding, parentSessionID: parent, agent, cacheable: true });
|
|
350
384
|
const fromTitle = titleNonce(session.title);
|
|
351
385
|
const fromPrompt = promptNonce(session.firstText);
|
|
352
386
|
if (fromTitle !== undefined && fromPrompt !== undefined && fromTitle !== fromPrompt) {
|
|
353
|
-
return out(unknown(childSessionID, BINDING_NOTES.markers));
|
|
387
|
+
return out(unknown(childSessionID, "markers", BINDING_NOTES.markers));
|
|
354
388
|
}
|
|
355
389
|
const nonce = fromPrompt ?? fromTitle;
|
|
356
|
-
if (nonce === undefined) return out(unknown(childSessionID, BINDING_NOTES.noNonce));
|
|
390
|
+
if (nonce === undefined) return out(unknown(childSessionID, "noNonce", BINDING_NOTES.noNonce));
|
|
357
391
|
const reg = registry();
|
|
358
392
|
prune(reg, Date.now());
|
|
359
393
|
// Live nonces are unique and never retired (registerPending refuses both), so a retired, unregistered or
|
|
360
394
|
// forged nonce finds no slot.
|
|
361
395
|
const slot = [...reg.pending.values()].find((s) => s.entry.nonce === nonce);
|
|
362
|
-
if (slot === undefined
|
|
363
|
-
return out(unknown(childSessionID, BINDING_NOTES.foreignNonce));
|
|
396
|
+
if (slot === undefined) {
|
|
397
|
+
return out(unknown(childSessionID, reg.retired.has(nonce) ? "retired" : "unregistered", BINDING_NOTES.foreignNonce));
|
|
398
|
+
}
|
|
399
|
+
if (slot.entry.parentSessionID !== parent || slot.entry.agent !== agent) {
|
|
400
|
+
return out(unknown(childSessionID, "foreign", BINDING_NOTES.foreignNonce));
|
|
364
401
|
}
|
|
365
402
|
if (slot.claimedBy !== undefined && slot.claimedBy !== childSessionID) {
|
|
366
|
-
return out(unknown(childSessionID, BINDING_NOTES.claimed));
|
|
403
|
+
return out(unknown(childSessionID, "claimed", BINDING_NOTES.claimed));
|
|
367
404
|
}
|
|
368
405
|
slot.claimedBy = childSessionID;
|
|
369
406
|
return out(exact(childSessionID, slot.entry));
|
|
@@ -404,7 +441,9 @@ export function registerPending(entry: PendingDispatch): void {
|
|
|
404
441
|
}
|
|
405
442
|
const keepClaim = previous !== undefined && previous.entry.nonce === entry.nonce;
|
|
406
443
|
if (previous !== undefined && !keepClaim) remember(reg.retired, previous.entry.nonce, now, RETIRED_MAX);
|
|
407
|
-
const
|
|
444
|
+
const { actions: copiedActions, notes: copiedNotes } = copyGrant(entry.grant);
|
|
445
|
+
const enteredRoot = rootOrNull(entry.grant.workRoot);
|
|
446
|
+
const grant: DispatchGrant = { actions: copiedActions, notes: copiedNotes, workRoot: enteredRoot, ...locationOf(entry.grant, enteredRoot) };
|
|
408
447
|
const stored: PendingDispatch = Object.freeze({ ...entry, grant });
|
|
409
448
|
remember(reg.pending, key, { entry: stored, start, claimedBy: keepClaim ? previous.claimedBy : undefined }, Infinity);
|
|
410
449
|
for (const [oldestKey, oldest] of reg.pending) {
|
|
@@ -450,6 +489,68 @@ export async function bind(childSessionID: string, getSession: SessionLookup, op
|
|
|
450
489
|
}
|
|
451
490
|
}
|
|
452
491
|
|
|
492
|
+
export interface PlanChildBindInput {
|
|
493
|
+
childSessionID: string;
|
|
494
|
+
tier?: string;
|
|
495
|
+
role: string;
|
|
496
|
+
agent: string;
|
|
497
|
+
workRoot: string;
|
|
498
|
+
grant: readonly string[];
|
|
499
|
+
planId: string;
|
|
500
|
+
taskId: string;
|
|
501
|
+
attempt: number;
|
|
502
|
+
/**
|
|
503
|
+
* QA-PDF21-2-20: the call budget of the plan child's route (its `budget=`), as a routed dispatch's binding carries; absent or
|
|
504
|
+
* null: the role's default budget.
|
|
505
|
+
*/
|
|
506
|
+
budget?: number | null;
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
/**
|
|
510
|
+
* Binds a plan child (3.1.5): the executor knows the child's role, grant and work root, so it writes the record
|
|
511
|
+
* the lazy lookup would produce (an exact binding) and adds the plan identity. There is no pending dispatch to pair
|
|
512
|
+
* with. Fails closed: a child that already has a binding with another role or work root throws, and so does a
|
|
513
|
+
* deleted session. Binding the same child again with the same role and root replaces the plan identity.
|
|
514
|
+
*/
|
|
515
|
+
export function bindPlanChild(input: PlanChildBindInput): void {
|
|
516
|
+
const reg = registry();
|
|
517
|
+
const now = Date.now();
|
|
518
|
+
const id = input.childSessionID;
|
|
519
|
+
if (text(id) === undefined || text(input.role) === undefined || text(input.agent) === undefined) {
|
|
520
|
+
throw new Error("[model-router] bindPlanChild needs a child session, a role and an agent");
|
|
521
|
+
}
|
|
522
|
+
const root = rootOrNull(input.workRoot);
|
|
523
|
+
if (root === null) throw new Error(`[model-router] bindPlanChild needs an absolute work root for ${id}`);
|
|
524
|
+
if (isDeleted(reg, id, now)) throw new Error(`[model-router] bindPlanChild: session ${id} was deleted`);
|
|
525
|
+
const existing = reg.bound.get(id);
|
|
526
|
+
if (existing !== undefined) {
|
|
527
|
+
const same = existing.binding.role === input.role && existing.binding.grant.workRoot === root;
|
|
528
|
+
if (!same) throw new Error(`[model-router] bindPlanChild: session ${id} is already bound with another role or work root`);
|
|
529
|
+
}
|
|
530
|
+
const known = new Set<string>(ACTION_ORDER);
|
|
531
|
+
const actions = ordered(input.grant.filter((a): a is AuthorityAction => known.has(a)));
|
|
532
|
+
const tier = input.tier ?? existing?.binding.tier;
|
|
533
|
+
const binding: Binding = {
|
|
534
|
+
role: input.role,
|
|
535
|
+
...(tier === undefined ? {} : { tier }),
|
|
536
|
+
plan: {
|
|
537
|
+
planId: input.planId, taskId: input.taskId, attempt: input.attempt,
|
|
538
|
+
taskRef: leaseTaskRef(input.planId, input.taskId), scope: "until-integrated",
|
|
539
|
+
},
|
|
540
|
+
childSessionID: id,
|
|
541
|
+
kind: "exact",
|
|
542
|
+
grant: { actions, notes: [], workRoot: root },
|
|
543
|
+
candidates: [],
|
|
544
|
+
decisionID: null,
|
|
545
|
+
// QA-PDF21-2-20: the route's `budget=`; a re-bind of the same child that names none keeps the budget it was bound with.
|
|
546
|
+
budget: input.budget === undefined
|
|
547
|
+
? existing?.binding.budget ?? null
|
|
548
|
+
: typeof input.budget === "number" && Number.isInteger(input.budget) && input.budget > 0 ? input.budget : null,
|
|
549
|
+
};
|
|
550
|
+
reg.inflight.delete(id);
|
|
551
|
+
remember(reg.bound, id, { binding, parentSessionID: undefined, agent: input.agent }, BOUND_MAX);
|
|
552
|
+
}
|
|
553
|
+
|
|
453
554
|
/** The cached binding of a child ∩ the caller's max, or undefined when it is not bound. No lookup. */
|
|
454
555
|
export function currentBinding(childSessionID: string, opts: BindOptions): Binding | undefined {
|
|
455
556
|
const slot = registry().bound.get(childSessionID);
|
|
@@ -493,7 +594,7 @@ export function widen(
|
|
|
493
594
|
added.push(action);
|
|
494
595
|
}
|
|
495
596
|
if (added.length > 0) notes.push(BINDING_NOTES.widened(added));
|
|
496
|
-
const grant: DispatchGrant = { actions: ordered(next), notes: [...new Set(notes)], workRoot: before.workRoot };
|
|
597
|
+
const grant: DispatchGrant = { actions: ordered(next), notes: [...new Set(notes)], workRoot: before.workRoot, ...locationOf(slot.binding.grant, before.workRoot) };
|
|
497
598
|
remember(reg.bound, childSessionID, { ...slot, binding: { ...slot.binding, grant } }, BOUND_MAX);
|
|
498
599
|
return copyGrant(grant);
|
|
499
600
|
}
|
|
@@ -512,6 +613,7 @@ export function evictCall(parentSessionID: string, callID: string): void {
|
|
|
512
613
|
* so neither a running lookup nor a later registration can attach anything to it.
|
|
513
614
|
*/
|
|
514
615
|
export function evict(sessionID: string): void {
|
|
616
|
+
forgetExecutorChildRound(sessionID);
|
|
515
617
|
const reg = registry();
|
|
516
618
|
const now = Date.now();
|
|
517
619
|
remember(reg.deleted, sessionID, now, DELETED_MAX);
|
|
@@ -535,6 +637,7 @@ export function dropBindingCacheForTests(childSessionID: string): void {
|
|
|
535
637
|
/** Test only: empties the process-wide registry. */
|
|
536
638
|
export function resetBindingRegistryForTests(): void {
|
|
537
639
|
const reg = registry();
|
|
640
|
+
for (const child of reg.bound.keys()) forgetExecutorChildRound(child);
|
|
538
641
|
reg.pending.clear();
|
|
539
642
|
reg.bound.clear();
|
|
540
643
|
reg.inflight.clear();
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cross-repo P2.2 (docs/design/cross-repo-phase2-plan.md SD3, SD4): where each check of a role dispatch runs. A check's
|
|
3
|
+
* `cwd=` and `scope=` keys (parsed by `verify/dod.ts`) become a {@link CheckPlacement}; nothing calls this yet.
|
|
4
|
+
*
|
|
5
|
+
* - Inert for a tier dispatch (no work root W) and for a DoD whose checks carry neither key: `{}`.
|
|
6
|
+
* - `scope=`: `affected` or `full`, case-insensitive; anything else refuses (`why: "scope"`).
|
|
7
|
+
* - `cwd=` is refused first, before any filesystem probe, when it is a UNC path, has a `.git` segment (case-insensitive, with
|
|
8
|
+
* any trailing dots/spaces: `.git.`, `.git ` name `.git` on win32) or, on win32 only, an 8.3 short-name segment
|
|
9
|
+
* ({@link SHORT_NAME_SEGMENT}; `my-library~2` and a POSIX `backup~1` are ordinary names). A relative `cwd=` is resolved against
|
|
10
|
+
* W with {@link canonicalAuthorityPath} and must stay inside W ({@link insideWorkRoot}); one that leaves W refuses even when it
|
|
11
|
+
* lands in the read set R. An absolute `cwd=` is canonicalised and accepted inside W, else only when `deps.inReadSet` holds it
|
|
12
|
+
* (R is injected: `work-root.ts` must not import `read-set.ts`). A canonical path with a `.git` segment refuses too.
|
|
13
|
+
* - A check with only `scope=` is placed at the block base (`scope.cwd`).
|
|
14
|
+
* - `top` is the git top level of the placement (a `.git` walk, no git process). A placement inside W is never `foreign` (a
|
|
15
|
+
* nested `.git` in W — a vendored repository, a fixture — does not make it foreign; its own `top` is still reported); outside
|
|
16
|
+
* W, `foreign` = its top differs from W's top, or is unknown.
|
|
17
|
+
*
|
|
18
|
+
* The first offending check in index order refuses the whole DoD; no top is looked up for a refused DoD.
|
|
19
|
+
*/
|
|
20
|
+
import { isAbsolute } from "node:path";
|
|
21
|
+
import type { DoD } from "../../verify/dod";
|
|
22
|
+
import { gitTopOf } from "../../verify/git-top";
|
|
23
|
+
import type { CheckPlacement, PlacementResolution } from "../../verify/types";
|
|
24
|
+
import { isSlash, trimEndWhile } from "../../util/linear-trim";
|
|
25
|
+
import { canonicalAuthorityPath, insideWorkRoot, SHORT_NAME_SEGMENT, type VerificationScope } from "./work-root";
|
|
26
|
+
|
|
27
|
+
/** What the resolver needs beyond the DoD and the scope; every member is a seam for tests. */
|
|
28
|
+
export interface CheckPlacementDeps {
|
|
29
|
+
/** The read-set predicate R (`read-set.ts` `readSetPredicate`) on a canonical absolute path. Absent: only W is allowed. */
|
|
30
|
+
readonly inReadSet?: (absPath: string) => boolean;
|
|
31
|
+
readonly canonical?: typeof canonicalAuthorityPath;
|
|
32
|
+
/** The git top level of a directory; default {@link gitTopOf}. */
|
|
33
|
+
readonly topOf?: (dir: string) => string | undefined;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** A `.git` segment, case-insensitive, with any trailing dots/spaces (win32 strips them: `.git.` and `.git ` name `.git`). */
|
|
37
|
+
const GIT_SEGMENT = /(?:^|[\\/])\.git[. ]*(?=[\\/]|$)/i;
|
|
38
|
+
|
|
39
|
+
/** Refused before any filesystem probe: a UNC/device path, a `.git` segment, on win32 an 8.3 short-name segment. */
|
|
40
|
+
function lexicallyRefused(text: string): boolean {
|
|
41
|
+
if (/^[\\/]{2}/.test(text)) return true;
|
|
42
|
+
if (GIT_SEGMENT.test(text)) return true;
|
|
43
|
+
return process.platform === "win32" && SHORT_NAME_SEGMENT.test(text);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** A comparison key for a top level: "/" separators, no trailing separator, case-folded on win32. */
|
|
47
|
+
function topKey(top: string): string {
|
|
48
|
+
const normalized = trimEndWhile(top.replace(/\\/g, "/"), isSlash); // linear: no `/\/+$/` (polynomial on a run of "/")
|
|
49
|
+
return process.platform === "win32" ? normalized.toLowerCase() : normalized;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** The canonical directory of a `cwd=` text, or undefined when it is refused. */
|
|
53
|
+
function placeCwd(
|
|
54
|
+
text: string,
|
|
55
|
+
workRoot: string,
|
|
56
|
+
canonical: typeof canonicalAuthorityPath,
|
|
57
|
+
inReadSet: ((absPath: string) => boolean) | undefined,
|
|
58
|
+
): string | undefined {
|
|
59
|
+
if (lexicallyRefused(text)) return undefined;
|
|
60
|
+
const target = canonical(text, workRoot);
|
|
61
|
+
if (target === undefined || GIT_SEGMENT.test(target)) return undefined; // `.git.`, a link or a junction can land in `.git`
|
|
62
|
+
if (insideWorkRoot(target, workRoot)) return target;
|
|
63
|
+
if (!isAbsolute(text)) return undefined; // a relative path that leaves W, even into R
|
|
64
|
+
return inReadSet?.(target) === true ? target : undefined;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* The placements of `dod.checks` for a dispatch verified in `scope`, index-aligned (undefined for a check with neither key),
|
|
69
|
+
* or the first refused check. `{}` for a tier scope or when no check carries a key.
|
|
70
|
+
*/
|
|
71
|
+
export function resolveCheckPlacements(dod: DoD, scope: VerificationScope, deps: CheckPlacementDeps = {}): PlacementResolution {
|
|
72
|
+
const workRoot = scope.workRoot;
|
|
73
|
+
if (workRoot === undefined) return {};
|
|
74
|
+
if (!dod.checks.some((check) => check.cwd !== undefined || check.scope !== undefined)) return {};
|
|
75
|
+
const canonical = deps.canonical ?? canonicalAuthorityPath;
|
|
76
|
+
const topOf = deps.topOf ?? gitTopOf;
|
|
77
|
+
const base = scope.cwd ?? workRoot;
|
|
78
|
+
|
|
79
|
+
// Pass 1: validate every key in index order; the first offending check refuses, before any top lookup.
|
|
80
|
+
const resolved: ({ readonly cwd: string; readonly scope?: "affected" | "full" } | undefined)[] = [];
|
|
81
|
+
for (let index = 0; index < dod.checks.length; index++) {
|
|
82
|
+
const check = dod.checks[index]!;
|
|
83
|
+
if (check.cwd === undefined && check.scope === undefined) {
|
|
84
|
+
resolved.push(undefined);
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
let checkScope: "affected" | "full" | undefined;
|
|
88
|
+
if (check.scope !== undefined) {
|
|
89
|
+
const lower = check.scope.toLowerCase();
|
|
90
|
+
if (lower !== "affected" && lower !== "full") return { refused: { index, kind: check.kind, text: check.scope, why: "scope" } };
|
|
91
|
+
checkScope = lower;
|
|
92
|
+
}
|
|
93
|
+
let cwd = base;
|
|
94
|
+
if (check.cwd !== undefined) {
|
|
95
|
+
const placed = placeCwd(check.cwd, workRoot, canonical, deps.inReadSet);
|
|
96
|
+
if (placed === undefined) return { refused: { index, kind: check.kind, text: check.cwd, why: "outside" } };
|
|
97
|
+
cwd = placed;
|
|
98
|
+
}
|
|
99
|
+
resolved.push(checkScope === undefined ? { cwd } : { cwd, scope: checkScope });
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// Pass 2: the top level of each placement, compared with W's. Inside W is never foreign, whatever `.git` lies below W.
|
|
103
|
+
const workTop = topOf(workRoot);
|
|
104
|
+
const workKey = workTop === undefined ? undefined : topKey(workTop);
|
|
105
|
+
const placements: (CheckPlacement | undefined)[] = resolved.map((entry) => {
|
|
106
|
+
if (entry === undefined) return undefined;
|
|
107
|
+
const top = topOf(entry.cwd);
|
|
108
|
+
const foreign = !insideWorkRoot(entry.cwd, workRoot) && (top === undefined || topKey(top) !== workKey);
|
|
109
|
+
return entry.scope === undefined ? { cwd: entry.cwd, top, foreign } : { cwd: entry.cwd, top, foreign, scope: entry.scope };
|
|
110
|
+
});
|
|
111
|
+
return { placements };
|
|
112
|
+
}
|