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,829 @@
|
|
|
1
|
+
// src/executor/compile/footprint.ts
|
|
2
|
+
// Footprint extraction (task 2.3.3, memo 2.3.1 §4 in docs/qa/critical-path-execution/phase-2.3.md).
|
|
3
|
+
// Turns a task body (plus its closed `Writes:`/`Reads:` lines and the phase ownership rows) into a `Footprint`.
|
|
4
|
+
// Pure except `loadRepoView`, which runs four read-only git commands. The input types are declared here
|
|
5
|
+
// structurally; this module does not import `parse.ts` (D9).
|
|
6
|
+
|
|
7
|
+
import { posix, win32 } from "node:path";
|
|
8
|
+
import type { Footprint, TaskKind } from "../types.js";
|
|
9
|
+
|
|
10
|
+
// ---------------------------------------------------------------------------------------------------------------
|
|
11
|
+
// Types
|
|
12
|
+
// ---------------------------------------------------------------------------------------------------------------
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* The tracked files of the repository and the roots it is checked out at.
|
|
16
|
+
* `files` and `dirs` hold repo-relative '/'-separated paths in the case git reports them; the repository root is
|
|
17
|
+
* the empty path and is not a member of `dirs`. `byBasename` is keyed by `touchKey(basename, caseInsensitive)`.
|
|
18
|
+
*/
|
|
19
|
+
export interface RepoView {
|
|
20
|
+
toplevel: string;
|
|
21
|
+
worktrees: readonly string[];
|
|
22
|
+
files: ReadonlySet<string>;
|
|
23
|
+
dirs: ReadonlySet<string>;
|
|
24
|
+
byBasename: ReadonlyMap<string, readonly string[]>;
|
|
25
|
+
caseInsensitive: boolean;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* `path` is repo-relative and '/'-separated; it is "" only for the repository root (a directory). `dir` is true for a
|
|
30
|
+
* directory and for a glob reduced to its static prefix.
|
|
31
|
+
*/
|
|
32
|
+
export type PathOutcome =
|
|
33
|
+
| { ok: true; path: string; via: "worktree" | "relative" | "basename" | "suffix" | "new"; dir: boolean }
|
|
34
|
+
| {
|
|
35
|
+
ok: false;
|
|
36
|
+
reason: "url" | "outside" | "other-drive" | "unc" | "device" | "escape" | "ambiguous" | "not-found";
|
|
37
|
+
raw: string;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
/** What `extractFootprint` needs of a parsed task (structural: no dependency on parse.ts, D9). */
|
|
41
|
+
export interface FootprintInput {
|
|
42
|
+
id: string;
|
|
43
|
+
kind: TaskKind;
|
|
44
|
+
body: string;
|
|
45
|
+
manual: boolean;
|
|
46
|
+
route: { fields: { needs?: readonly string[]; class?: string } | null; inferred?: boolean } | null;
|
|
47
|
+
writesLine: { line: number; text: string; none: boolean } | null;
|
|
48
|
+
readsLine: { line: number; text: string; none: boolean } | null;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export interface OwnershipInput { path: string; owners: string[]; line: number }
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* `evidence[].line` and `ignored[].line` are plan lines for paths that come from a `Writes:`/`Reads:` line and
|
|
55
|
+
* 1-based lines of `body` for every other path. `ignored[].reason` is a `PathOutcome` reason, `"repo-root"` (a read of
|
|
56
|
+
* the whole repository was dropped, info), `"dir-write-unbounded"` (a directory write matches no tracked file or more
|
|
57
|
+
* than `MAX_DIR_EXPANSION`; the footprint is then `unknown`, i.e. writes ⊤, D4/O2) or `"write-unresolved"` (a write
|
|
58
|
+
* path that cannot be represented; also ⊤). A write path that does not exist is kept as a new write, never ignored
|
|
59
|
+
* (QA-2.3-1). `foreignRoots` lists the leading
|
|
60
|
+
* roots dropped by suffix matches.
|
|
61
|
+
*/
|
|
62
|
+
export interface TaskFootprint {
|
|
63
|
+
footprint: Footprint;
|
|
64
|
+
editing: boolean;
|
|
65
|
+
lowConfidence: boolean;
|
|
66
|
+
/** True when the writes are the phase's whole ownership set assumed for lack of evidence (confidence `inferred`). */
|
|
67
|
+
ownershipAssumed: boolean;
|
|
68
|
+
evidence: Array<{ path: string; access: "write" | "read"; source: Footprint["sources"][number]; line: number; rule: string }>;
|
|
69
|
+
/** `write` is true for a candidate found in a write context (`Writes:` line or after a write verb). */
|
|
70
|
+
ignored: Array<{ raw: string; reason: string; line: number; write?: boolean }>;
|
|
71
|
+
foreignRoots: string[];
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** ⊤ marker used by edges/validate: the write set of an editing task whose footprint confidence is `unknown`. */
|
|
75
|
+
export const UNKNOWN_WRITES: unique symbol = Symbol("footprint.UNKNOWN_WRITES");
|
|
76
|
+
|
|
77
|
+
/** O2: a directory write expands to at most this many tracked files; otherwise the task writes ⊤. */
|
|
78
|
+
export const MAX_DIR_EXPANSION = 50;
|
|
79
|
+
|
|
80
|
+
/** The read-only git runner `loadRepoView` needs (the `git` member of the compiler's dependencies). */
|
|
81
|
+
export interface RepoGit {
|
|
82
|
+
run(args: readonly string[], cwd: string): Promise<{ code: number; stdout: string }>;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// ---------------------------------------------------------------------------------------------------------------
|
|
86
|
+
// Keys
|
|
87
|
+
// ---------------------------------------------------------------------------------------------------------------
|
|
88
|
+
|
|
89
|
+
/** The key two paths collide on: lower case when the file system is case-insensitive. */
|
|
90
|
+
export function touchKey(p: string, ci: boolean): string {
|
|
91
|
+
return ci ? p.toLowerCase() : p;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** True when `a` and `b` are equal or one is a '/'-prefix of the other ("" is the repository root, a prefix of all). */
|
|
95
|
+
export function overlaps(a: string, b: string, ci: boolean): boolean {
|
|
96
|
+
const ka = touchKey(a, ci);
|
|
97
|
+
const kb = touchKey(b, ci);
|
|
98
|
+
if (ka === kb || ka === "" || kb === "") return true;
|
|
99
|
+
return ka.startsWith(`${kb}/`) || kb.startsWith(`${ka}/`);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// ---------------------------------------------------------------------------------------------------------------
|
|
103
|
+
// RepoView
|
|
104
|
+
// ---------------------------------------------------------------------------------------------------------------
|
|
105
|
+
|
|
106
|
+
interface RepoIndex {
|
|
107
|
+
fileKeys: Map<string, string>;
|
|
108
|
+
dirKeys: Map<string, string>;
|
|
109
|
+
/** Directory key -> number of tracked files below it and the first `MAX_DIR_EXPANSION + 1` of them. */
|
|
110
|
+
under: Map<string, { count: number; files: string[] }>;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const indexCache = new WeakMap<RepoView, RepoIndex>();
|
|
114
|
+
|
|
115
|
+
function buildIndex(repo: RepoView): RepoIndex {
|
|
116
|
+
const ci = repo.caseInsensitive;
|
|
117
|
+
const fileKeys = new Map<string, string>();
|
|
118
|
+
const dirKeys = new Map<string, string>();
|
|
119
|
+
const under = new Map<string, { count: number; files: string[] }>();
|
|
120
|
+
const note = (key: string, file: string): void => {
|
|
121
|
+
let e = under.get(key);
|
|
122
|
+
if (e === undefined) { e = { count: 0, files: [] }; under.set(key, e); }
|
|
123
|
+
e.count += 1;
|
|
124
|
+
if (e.files.length <= MAX_DIR_EXPANSION) e.files.push(file);
|
|
125
|
+
};
|
|
126
|
+
for (const f of repo.files) {
|
|
127
|
+
fileKeys.set(touchKey(f, ci), f);
|
|
128
|
+
note("", f);
|
|
129
|
+
let cut = f.lastIndexOf("/");
|
|
130
|
+
while (cut > 0) {
|
|
131
|
+
const dir = f.slice(0, cut);
|
|
132
|
+
const k = touchKey(dir, ci);
|
|
133
|
+
note(k, f);
|
|
134
|
+
cut = dir.lastIndexOf("/");
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
for (const d of repo.dirs) dirKeys.set(touchKey(d, ci), d);
|
|
138
|
+
return { fileKeys, dirKeys, under };
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function indexOf(repo: RepoView): RepoIndex {
|
|
142
|
+
let idx = indexCache.get(repo);
|
|
143
|
+
if (idx === undefined) { idx = buildIndex(repo); indexCache.set(repo, idx); }
|
|
144
|
+
return idx;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** Build a `RepoView` from plain data: `dirs` and `byBasename` are derived from `files` (git paths, '/'-separated). */
|
|
148
|
+
export function repoViewFrom(
|
|
149
|
+
i: { toplevel: string; worktrees: string[]; files: string[]; caseInsensitive: boolean },
|
|
150
|
+
): RepoView {
|
|
151
|
+
const ci = i.caseInsensitive;
|
|
152
|
+
const files = new Set<string>();
|
|
153
|
+
const dirs = new Set<string>();
|
|
154
|
+
const byBasename = new Map<string, string[]>();
|
|
155
|
+
for (const raw of i.files) {
|
|
156
|
+
const f = raw.replace(/\\/g, "/");
|
|
157
|
+
if (f === "" || files.has(f)) continue;
|
|
158
|
+
files.add(f);
|
|
159
|
+
const slash = f.lastIndexOf("/");
|
|
160
|
+
const base = f.slice(slash + 1);
|
|
161
|
+
const key = touchKey(base, ci);
|
|
162
|
+
const list = byBasename.get(key);
|
|
163
|
+
if (list === undefined) byBasename.set(key, [f]);
|
|
164
|
+
else list.push(f);
|
|
165
|
+
let cut = slash;
|
|
166
|
+
while (cut > 0) {
|
|
167
|
+
const dir = f.slice(0, cut);
|
|
168
|
+
if (dirs.has(dir)) break;
|
|
169
|
+
dirs.add(dir);
|
|
170
|
+
cut = dir.lastIndexOf("/");
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
const view: RepoView = { toplevel: i.toplevel, worktrees: [...i.worktrees], files, dirs, byBasename, caseInsensitive: ci };
|
|
174
|
+
indexCache.set(view, buildIndex(view));
|
|
175
|
+
return view;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Read the repository facts with `rev-parse --show-toplevel`, `worktree list --porcelain`, `ls-files -z` and
|
|
180
|
+
* `config core.ignorecase`. The first and third must succeed; `worktree list` failing leaves the toplevel as the only
|
|
181
|
+
* root, and a missing `core.ignorecase` means case-sensitive.
|
|
182
|
+
*/
|
|
183
|
+
export async function loadRepoView(git: RepoGit, cwd: string): Promise<RepoView> {
|
|
184
|
+
const top = await git.run(["rev-parse", "--show-toplevel"], cwd);
|
|
185
|
+
if (top.code !== 0 || top.stdout.trim() === "") {
|
|
186
|
+
throw new Error(`git rev-parse --show-toplevel failed in ${cwd} (exit ${top.code})`);
|
|
187
|
+
}
|
|
188
|
+
const toplevel = top.stdout.trim();
|
|
189
|
+
const wt = await git.run(["worktree", "list", "--porcelain"], toplevel);
|
|
190
|
+
const worktrees: string[] = [];
|
|
191
|
+
if (wt.code === 0) {
|
|
192
|
+
for (const line of wt.stdout.split(/\r?\n/)) {
|
|
193
|
+
if (line.startsWith("worktree ")) {
|
|
194
|
+
const p = line.slice("worktree ".length).trim();
|
|
195
|
+
if (p !== "" && p !== toplevel) worktrees.push(p);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
const ls = await git.run(["ls-files", "-z"], toplevel);
|
|
200
|
+
if (ls.code !== 0) throw new Error(`git ls-files failed in ${toplevel} (exit ${ls.code})`);
|
|
201
|
+
const files = ls.stdout.split("\0").filter((f) => f !== "");
|
|
202
|
+
const cfg = await git.run(["config", "core.ignorecase"], toplevel);
|
|
203
|
+
const caseInsensitive = cfg.code === 0 && cfg.stdout.trim().toLowerCase() === "true";
|
|
204
|
+
return repoViewFrom({ toplevel, worktrees, files, caseInsensitive });
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// ---------------------------------------------------------------------------------------------------------------
|
|
208
|
+
// normalizeRepoPath
|
|
209
|
+
// ---------------------------------------------------------------------------------------------------------------
|
|
210
|
+
|
|
211
|
+
const URL_RE = /^(?:[a-z][a-z0-9+.-]+:\/\/|file:)/i;
|
|
212
|
+
/** The win32 device prefixes `\\?\`, `\\.\`, `\??\` (the same spellings tool-paths.ts rejects). */
|
|
213
|
+
const WIN_DEVICE = /^(?:[\\/]{2}[.?]|[\\/]\?\?)[\\/]/;
|
|
214
|
+
const UNC = /^[\\/]{2}/;
|
|
215
|
+
const ROOTED_NO_DRIVE = /^[\\/](?![\\/])/;
|
|
216
|
+
const DRIVE_ABS = /^[A-Za-z]:[\\/]/;
|
|
217
|
+
const DRIVE_REL = /^[A-Za-z]:(?![\\/])/;
|
|
218
|
+
const GLOB_CHARS = /[*?[\]{}]/;
|
|
219
|
+
|
|
220
|
+
type Ok = Extract<PathOutcome, { ok: true }>;
|
|
221
|
+
type Fail = Extract<PathOutcome, { ok: false }>;
|
|
222
|
+
type FailReason = Fail["reason"];
|
|
223
|
+
|
|
224
|
+
interface Resolved { outcome: PathOutcome; foreignRoot: string | null }
|
|
225
|
+
|
|
226
|
+
function fail(reason: FailReason, raw: string): Resolved {
|
|
227
|
+
return { outcome: { ok: false, reason, raw }, foreignRoot: null };
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
function good(path: string, via: Ok["via"], dir: boolean, foreignRoot: string | null = null): Resolved {
|
|
231
|
+
return { outcome: { ok: true, path, via, dir }, foreignRoot };
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
function splitSegs(p: string): string[] {
|
|
235
|
+
return p.split(/[\\/]+/).filter((s) => s.length > 0);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
function badSegment(s: string): boolean {
|
|
239
|
+
return s.includes(":") || s.includes("\0");
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/** Lower-cased mirror of a created-directory set, extended incrementally (the set only grows, in insertion order). */
|
|
243
|
+
const createdFolded = new WeakMap<ReadonlySet<string>, { seen: number; keys: Set<string> }>();
|
|
244
|
+
|
|
245
|
+
function createdHas(created: ReadonlySet<string>, p: string, ci: boolean): boolean {
|
|
246
|
+
if (created.has(p)) return true;
|
|
247
|
+
if (!ci) return false;
|
|
248
|
+
let e = createdFolded.get(created);
|
|
249
|
+
if (e === undefined || e.seen > created.size) { e = { seen: 0, keys: new Set() }; createdFolded.set(created, e); }
|
|
250
|
+
if (e.seen < created.size) {
|
|
251
|
+
let i = 0;
|
|
252
|
+
for (const c of created) {
|
|
253
|
+
if (i >= e.seen) e.keys.add(c.toLowerCase());
|
|
254
|
+
i += 1;
|
|
255
|
+
}
|
|
256
|
+
e.seen = created.size;
|
|
257
|
+
}
|
|
258
|
+
return e.keys.has(p.toLowerCase());
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/** Canonical (git-case) spelling and kind of `rel` when it is a tracked file or a directory of one. */
|
|
262
|
+
function lookup(repo: RepoView, rel: string): { path: string; dir: boolean } | null {
|
|
263
|
+
const idx = indexOf(repo);
|
|
264
|
+
const k = touchKey(rel, repo.caseInsensitive);
|
|
265
|
+
const f = idx.fileKeys.get(k);
|
|
266
|
+
if (f !== undefined) return { path: f, dir: false };
|
|
267
|
+
const d = idx.dirKeys.get(k);
|
|
268
|
+
if (d !== undefined) return { path: d, dir: true };
|
|
269
|
+
return null;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/** Lexical '/'-separated spelling of a relative `raw` that is not tracked; null on an escape or an unusable segment. */
|
|
273
|
+
function lexicalNew(raw: string): string | null {
|
|
274
|
+
const stack: string[] = [];
|
|
275
|
+
for (const s of raw.trim().split(/[\\/]+/)) {
|
|
276
|
+
if (s === "" || s === ".") continue;
|
|
277
|
+
if (s === "..") {
|
|
278
|
+
if (stack.length === 0) return null;
|
|
279
|
+
stack.pop();
|
|
280
|
+
} else stack.push(s);
|
|
281
|
+
}
|
|
282
|
+
if (stack.length === 0 || stack.some(badSegment)) return null;
|
|
283
|
+
return stack.join("/");
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/** Rule 9: an unknown path is accepted only when `allowNew` and its parent exists or is created earlier. */
|
|
287
|
+
function asNew(repo: RepoView, rel: string, dirWanted: boolean, o: { allowNew: boolean; createdEarlier: ReadonlySet<string> }, raw: string): Resolved {
|
|
288
|
+
if (!o.allowNew) return fail("not-found", raw);
|
|
289
|
+
const slash = rel.lastIndexOf("/");
|
|
290
|
+
const parent = slash < 0 ? "" : rel.slice(0, slash);
|
|
291
|
+
const parentOk = parent === "" || lookup(repo, parent)?.dir === true || createdHas(o.createdEarlier, parent, repo.caseInsensitive);
|
|
292
|
+
return parentOk ? good(rel, "new", dirWanted) : fail("not-found", raw);
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
function resolveAbsolute(
|
|
296
|
+
raw: string, win: boolean, repo: RepoView, o: { allowNew: boolean; createdEarlier: ReadonlySet<string> },
|
|
297
|
+
): Resolved {
|
|
298
|
+
const mod = win ? win32 : posix;
|
|
299
|
+
const trailing = /[\\/]$/.test(raw);
|
|
300
|
+
const segs = splitSegs(mod.normalize(raw));
|
|
301
|
+
const ci = repo.caseInsensitive;
|
|
302
|
+
const eq = (a: string, b: string, i: number): boolean =>
|
|
303
|
+
a === b || ((ci || (win && i === 0)) && a.toLowerCase() === b.toLowerCase());
|
|
304
|
+
|
|
305
|
+
// Rule 5: under the toplevel or a worktree; the longest root wins.
|
|
306
|
+
let best: string[] | null = null;
|
|
307
|
+
for (const root of [repo.toplevel, ...repo.worktrees]) {
|
|
308
|
+
const rs = splitSegs(mod.normalize(root));
|
|
309
|
+
if (rs.length === 0 || rs.length > segs.length) continue;
|
|
310
|
+
if (best !== null && rs.length <= best.length) continue;
|
|
311
|
+
if (rs.every((s, i) => eq(s, segs[i] ?? "", i))) best = rs;
|
|
312
|
+
}
|
|
313
|
+
if (best !== null) {
|
|
314
|
+
const rel = segs.slice(best.length);
|
|
315
|
+
if (rel.some(badSegment)) return fail("outside", raw);
|
|
316
|
+
if (rel.length === 0) return good("", "worktree", true);
|
|
317
|
+
const joined = rel.join("/");
|
|
318
|
+
const hit = lookup(repo, joined);
|
|
319
|
+
if (hit !== null) return good(hit.path, "worktree", hit.dir);
|
|
320
|
+
return good(joined, "worktree", trailing);
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
// Rule 6: another root: the longest suffix that exists in the repository.
|
|
324
|
+
for (let drop = win ? 1 : 0; drop < segs.length; drop++) {
|
|
325
|
+
const rel = segs.slice(drop);
|
|
326
|
+
if (rel.some(badSegment)) continue;
|
|
327
|
+
const hit = lookup(repo, rel.join("/"));
|
|
328
|
+
if (hit !== null) {
|
|
329
|
+
const dropped = segs.slice(0, drop);
|
|
330
|
+
const root = win ? dropped.join("\\") : `/${dropped.join("/")}`;
|
|
331
|
+
return good(hit.path, "suffix", hit.dir, root);
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
// Rules 6 + 9: a new file under another root (`D:\git\other-wt\test\smoke\new.test.ts`, created by the task) maps to the
|
|
335
|
+
// longest suffix of at least two segments whose parent directory exists or is created earlier.
|
|
336
|
+
if (o.allowNew) {
|
|
337
|
+
for (let drop = win ? 1 : 0; drop + 2 <= segs.length; drop++) {
|
|
338
|
+
const rel = segs.slice(drop);
|
|
339
|
+
if (rel.some(badSegment)) continue;
|
|
340
|
+
const parent = rel.slice(0, -1).join("/");
|
|
341
|
+
if (lookup(repo, parent)?.dir === true || createdHas(o.createdEarlier, parent, ci)) {
|
|
342
|
+
const dropped = segs.slice(0, drop);
|
|
343
|
+
const root = win ? dropped.join("\\") : `/${dropped.join("/")}`;
|
|
344
|
+
return good(rel.join("/"), "suffix", trailing, root);
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
if (win) {
|
|
349
|
+
const drive = (segs[0] ?? "").toLowerCase();
|
|
350
|
+
const known = [repo.toplevel, ...repo.worktrees].some((r) => splitSegs(mod.normalize(r))[0]?.toLowerCase() === drive);
|
|
351
|
+
return fail(known ? "outside" : "other-drive", raw);
|
|
352
|
+
}
|
|
353
|
+
return fail("outside", raw);
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
function resolveRelative(
|
|
357
|
+
raw: string, repo: RepoView, o: { allowNew: boolean; createdEarlier: ReadonlySet<string> },
|
|
358
|
+
): Resolved {
|
|
359
|
+
const trailing = /[\\/]$/.test(raw);
|
|
360
|
+
const stack: string[] = [];
|
|
361
|
+
for (const s of raw.split(/[\\/]+/)) {
|
|
362
|
+
if (s === "" || s === ".") continue;
|
|
363
|
+
if (s === "..") {
|
|
364
|
+
if (stack.length === 0) return fail("escape", raw);
|
|
365
|
+
stack.pop();
|
|
366
|
+
} else stack.push(s);
|
|
367
|
+
}
|
|
368
|
+
if (stack.some(badSegment)) return fail("outside", raw);
|
|
369
|
+
if (stack.length === 0) return good("", "relative", true);
|
|
370
|
+
const rel = stack.join("/");
|
|
371
|
+
const hit = lookup(repo, rel);
|
|
372
|
+
if (hit !== null) return good(hit.path, "relative", hit.dir);
|
|
373
|
+
return asNew(repo, rel, trailing, o, raw);
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
function resolveBare(
|
|
377
|
+
raw: string, repo: RepoView, o: { allowNew: boolean; createdEarlier: ReadonlySet<string> },
|
|
378
|
+
): Resolved {
|
|
379
|
+
// `./` is the repository root, never a new file named "." (without `allowNew` it stays `not-found`, as before).
|
|
380
|
+
if (raw === "." && o.allowNew) return good("", "relative", true);
|
|
381
|
+
if (raw === "..") return fail("escape", raw);
|
|
382
|
+
if (badSegment(raw)) return fail("outside", raw);
|
|
383
|
+
const matches = repo.byBasename.get(touchKey(raw, repo.caseInsensitive));
|
|
384
|
+
if (matches !== undefined && matches.length === 1) return good(matches[0] ?? raw, "basename", false);
|
|
385
|
+
if (matches !== undefined && matches.length > 1) return fail("ambiguous", raw);
|
|
386
|
+
const dir = lookup(repo, raw);
|
|
387
|
+
if (dir !== null) return good(dir.path, "relative", dir.dir);
|
|
388
|
+
return asNew(repo, raw, false, o, raw);
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
function resolveDetailed(
|
|
392
|
+
raw0: string, repo: RepoView, o: { allowNew: boolean; createdEarlier: ReadonlySet<string> },
|
|
393
|
+
): Resolved {
|
|
394
|
+
const raw = raw0.trim();
|
|
395
|
+
if (raw === "") return fail("not-found", raw0);
|
|
396
|
+
if (URL_RE.test(raw)) return fail("url", raw0); // 1
|
|
397
|
+
if (WIN_DEVICE.test(raw)) return fail("device", raw0); // 2
|
|
398
|
+
if (UNC.test(raw)) return fail("unc", raw0); // 3
|
|
399
|
+
const winRepo = DRIVE_ABS.test(repo.toplevel) || repo.toplevel.includes("\\");
|
|
400
|
+
if (ROOTED_NO_DRIVE.test(raw) && (winRepo || raw.includes("\\"))) return fail("device", raw0); // 4
|
|
401
|
+
if (raw.includes("\0") || DRIVE_REL.test(raw)) return fail("outside", raw0);
|
|
402
|
+
|
|
403
|
+
// Glob tokens reduce to their static prefix (the segments before the first wildcard) and become directories.
|
|
404
|
+
const parts = raw.split(/[\\/]/);
|
|
405
|
+
const cut = parts.findIndex((s) => GLOB_CHARS.test(s));
|
|
406
|
+
if (cut >= 0) {
|
|
407
|
+
const sep = raw.includes("\\") ? "\\" : "/";
|
|
408
|
+
let prefix = parts.slice(0, cut).join(sep);
|
|
409
|
+
if (cut === 1 && parts[0] === "") prefix = sep;
|
|
410
|
+
const r = resolveDetailed(prefix === "" ? "." : prefix, repo, { allowNew: false, createdEarlier: o.createdEarlier });
|
|
411
|
+
if (!r.outcome.ok) return { outcome: { ...r.outcome, raw: raw0 }, foreignRoot: null };
|
|
412
|
+
return { outcome: { ...r.outcome, dir: true }, foreignRoot: r.foreignRoot };
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
const absWin = DRIVE_ABS.test(raw);
|
|
416
|
+
if (absWin || (raw.startsWith("/") && !winRepo && !raw.includes("\\"))) {
|
|
417
|
+
const r = resolveAbsolute(raw, absWin, repo, o);
|
|
418
|
+
return r.outcome.ok ? r : { outcome: { ...r.outcome, raw: raw0 }, foreignRoot: null };
|
|
419
|
+
}
|
|
420
|
+
const hasSeparator = /[\\/]/.test(raw.replace(/[\\/]+$/, ""));
|
|
421
|
+
const r = hasSeparator ? resolveRelative(raw, repo, o) : resolveBare(raw.replace(/[\\/]+$/, ""), repo, o);
|
|
422
|
+
return r.outcome.ok ? r : { outcome: { ...r.outcome, raw: raw0 }, foreignRoot: null };
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
/**
|
|
426
|
+
* Resolve a path spelling found in a plan to a repo-relative '/'-separated path, following the rules of memo §4:
|
|
427
|
+
* url, device, unc and drive-less rooted spellings are rejected; an absolute path under the toplevel or a worktree maps
|
|
428
|
+
* to its suffix; one under another root maps to the longest suffix that exists; a relative path resolves lexically
|
|
429
|
+
* (a leading `..` that escapes is `escape`); a bare file name needs exactly one tracked match; anything unknown is
|
|
430
|
+
* accepted only with `allowNew` and an existing or earlier-created parent directory. Win32 spellings always use
|
|
431
|
+
* `path.win32`, so the result does not depend on the host OS.
|
|
432
|
+
*/
|
|
433
|
+
export function normalizeRepoPath(
|
|
434
|
+
raw: string, repo: RepoView, o: { allowNew: boolean; createdEarlier: ReadonlySet<string> },
|
|
435
|
+
): PathOutcome {
|
|
436
|
+
return resolveDetailed(raw, repo, o).outcome;
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
// ---------------------------------------------------------------------------------------------------------------
|
|
440
|
+
// Candidate extraction
|
|
441
|
+
// ---------------------------------------------------------------------------------------------------------------
|
|
442
|
+
|
|
443
|
+
interface Cand {
|
|
444
|
+
raw: string;
|
|
445
|
+
/** Offsets in `text` of the candidate including its backticks. */
|
|
446
|
+
start: number;
|
|
447
|
+
end: number;
|
|
448
|
+
line: number;
|
|
449
|
+
text: string;
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
const TICK_FORBIDDEN = /[\s"'<>|=,;$]/;
|
|
453
|
+
const EXTENSION = /\.[A-Za-z][A-Za-z0-9_-]{0,11}$/;
|
|
454
|
+
const SEG = "[\\w.@~+*-]";
|
|
455
|
+
|
|
456
|
+
function stripAnnotations(tok: string): string {
|
|
457
|
+
return tok.replace(/#L?\d+(?:-L?\d+)?$/, "").replace(/(?<=[^\\/:]):\d+(?:[-:]\d+)*$/, "");
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
function lastSegment(p: string): string {
|
|
461
|
+
const parts = p.split(/[\\/]/).filter((s) => s !== "");
|
|
462
|
+
return parts[parts.length - 1] ?? "";
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
function fileish(p: string): boolean {
|
|
466
|
+
const last = lastSegment(p);
|
|
467
|
+
return EXTENSION.test(last) || last.includes("*");
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
function tickedQualifies(tok: string): boolean {
|
|
471
|
+
if (URL_RE.test(tok)) return true;
|
|
472
|
+
if (tok.length < 2 || TICK_FORBIDDEN.test(tok) || /^[\d.]+$/.test(tok) || tok.startsWith("-")) return false;
|
|
473
|
+
if (!/[^\\/]/.test(tok)) return false;
|
|
474
|
+
return /[\\/]/.test(tok) || EXTENSION.test(tok);
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
const BARE_URL = /\b[a-z][a-z0-9+.-]+:\/\/[^\s`"'<>)\]]+/gi;
|
|
478
|
+
const BARE_DRIVE = /(?<![\w])[A-Za-z]:[\\/][^\s"'<>|`]*/g;
|
|
479
|
+
const BARE_UNC = /(?<![\w\\])\\\\[^\s"'<>|`]+/g;
|
|
480
|
+
const BARE_POSIX = new RegExp(`(?<![\\w.@~+*\\\\/:)-])\\/(?!\\/)${SEG}+(?:\\/${SEG}+)*\\/?`, "g");
|
|
481
|
+
const BARE_REL = new RegExp(`(?<![\\w.@~+*\\\\/:-])${SEG}+(?:[\\\\/]${SEG}+)+[\\\\/]?`, "g");
|
|
482
|
+
|
|
483
|
+
/** The path candidates of one line: backticked tokens first, then the bare spellings outside backticks. */
|
|
484
|
+
function scanLine(text: string, line: number): Cand[] {
|
|
485
|
+
const out: Cand[] = [];
|
|
486
|
+
let blank = text;
|
|
487
|
+
const blankRange = (s: number, e: number): void => {
|
|
488
|
+
blank = blank.slice(0, s) + " ".repeat(e - s) + blank.slice(e);
|
|
489
|
+
};
|
|
490
|
+
const tick = /`([^`\n]+)`/g;
|
|
491
|
+
let m: RegExpExecArray | null;
|
|
492
|
+
while ((m = tick.exec(text)) !== null) {
|
|
493
|
+
const tok = stripAnnotations((m[1] ?? "").trim());
|
|
494
|
+
const end = m.index + m[0].length;
|
|
495
|
+
if (tickedQualifies(tok)) out.push({ raw: tok, start: m.index, end, line, text });
|
|
496
|
+
blankRange(m.index, end);
|
|
497
|
+
}
|
|
498
|
+
const bare = (re: RegExp, accept: (tok: string) => boolean): void => {
|
|
499
|
+
const source = blank;
|
|
500
|
+
re.lastIndex = 0;
|
|
501
|
+
let b: RegExpExecArray | null;
|
|
502
|
+
while ((b = re.exec(source)) !== null) {
|
|
503
|
+
const stripped = b[0].replace(/[.,;:)\]]+$/, "");
|
|
504
|
+
const tok = stripped === "" ? b[0] : stripped;
|
|
505
|
+
if (accept(tok)) out.push({ raw: tok, start: b.index, end: b.index + tok.length, line, text });
|
|
506
|
+
blankRange(b.index, b.index + b[0].length);
|
|
507
|
+
}
|
|
508
|
+
};
|
|
509
|
+
bare(BARE_URL, () => true);
|
|
510
|
+
bare(BARE_DRIVE, () => true);
|
|
511
|
+
bare(BARE_UNC, () => true);
|
|
512
|
+
bare(BARE_POSIX, (tok) => splitSegs(tok).length >= 2 || fileish(tok) || tok.endsWith("/"));
|
|
513
|
+
bare(BARE_REL, (tok) => fileish(tok) || /[\\/]$/.test(tok));
|
|
514
|
+
return out.sort((a, b) => a.start - b.start);
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
function scanText(text: string, firstLine: number): Cand[] {
|
|
518
|
+
const out: Cand[] = [];
|
|
519
|
+
text.split(/\r?\n/).forEach((l, i) => { out.push(...scanLine(l, firstLine + i)); });
|
|
520
|
+
return out;
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
// ---------------------------------------------------------------------------------------------------------------
|
|
524
|
+
// Write verbs
|
|
525
|
+
// ---------------------------------------------------------------------------------------------------------------
|
|
526
|
+
|
|
527
|
+
const WRITE_VERB =
|
|
528
|
+
/\b(?:implement(?:s|ed|ing)?|creat(?:e|es|ed|ing)|add(?:s|ed|ing)?|modif(?:y|ies|ied|ying)|updat(?:e|es|ed|ing)|replac(?:e|es|ed|ing)|mov(?:e|es|ed|ing)|delet(?:e|es|ed|ing)|extend(?:s|ed|ing)?|writ(?:e|es|ing)|wrote|edit(?:s|ed|ing)?|fix(?:es|ed|ing)?|refactor(?:s|ed|ing)?|renam(?:e|es|ed|ing)|remov(?:e|es|ed|ing)|rework(?:s|ed|ing)?|chang(?:e|es|ed|ing)|keys?\s+in)\b/i;
|
|
529
|
+
/** "wire … in `X`", "document … in `X`", "record … in `X`": the verb, up to four tokens, then `in` right before the path. */
|
|
530
|
+
const WRITE_VERB_IN = /\b(?:wir(?:e|es|ed|ing)|document(?:s|ed|ing)?|record(?:s|ed|ing)?)\b(?:\s+\S+){0,4}?\s+in\s*$/i;
|
|
531
|
+
const CREATE_VERB = /^(?:implement|creat|add)/i;
|
|
532
|
+
const TESTS_IN = /\btests?\s+in\s*$/i;
|
|
533
|
+
const NEW_MARKER = /^[\s*_]*\(new\)/i;
|
|
534
|
+
|
|
535
|
+
/** The write verb in the clause before `c` (at most 6 tokens back, or the "In `X`:" form), or null. */
|
|
536
|
+
function verbBefore(c: Cand): { rule: string; create: boolean } | null {
|
|
537
|
+
const masked = c.text.slice(0, c.start).replace(/`[^`]*`/g, "§");
|
|
538
|
+
const clause = masked.split(/(?<=[.;!?])\s+/).pop() ?? "";
|
|
539
|
+
const lead = clause.replace(/^[\s>*+-]+/, "").replace(/^\d+[.)]\s+/, "").trim();
|
|
540
|
+
if (/(?:^|\s)in$/i.test(lead) && /^\s*:/.test(c.text.slice(c.end))) return { rule: "verb:in", create: false };
|
|
541
|
+
const tail = clause.trim().split(/\s+/).slice(-6).join(" ");
|
|
542
|
+
if (TESTS_IN.test(tail)) return { rule: "verb:tests-in", create: false };
|
|
543
|
+
const m = WRITE_VERB.exec(tail);
|
|
544
|
+
if (m === null) {
|
|
545
|
+
const w = WRITE_VERB_IN.exec(tail);
|
|
546
|
+
return w === null ? null : { rule: `verb:${(/^\S+/.exec(w[0]) ?? [w[0]])[0].toLowerCase()}-in`, create: false };
|
|
547
|
+
}
|
|
548
|
+
return { rule: `verb:${m[0].toLowerCase()}`, create: CREATE_VERB.test(m[0]) };
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
// ---------------------------------------------------------------------------------------------------------------
|
|
552
|
+
// extractFootprint
|
|
553
|
+
// ---------------------------------------------------------------------------------------------------------------
|
|
554
|
+
|
|
555
|
+
/** Classes whose tasks do not edit unless they show write evidence (`TASK_CLASSES` minus implement/debug/mechanical/other). */
|
|
556
|
+
const NON_EDITING_CLASSES: ReadonlySet<string> = new Set(["search", "recon", "design", "review"]);
|
|
557
|
+
const NEVER_EDITING_KINDS: ReadonlySet<TaskKind> = new Set<TaskKind>(["manual", "preflight", "qa"]);
|
|
558
|
+
|
|
559
|
+
type Source = Footprint["sources"][number];
|
|
560
|
+
|
|
561
|
+
/** The tracked files below directory `dir`; null (⊤) when there are none or more than `MAX_DIR_EXPANSION` (O2). */
|
|
562
|
+
function expandDir(repo: RepoView, dir: string): string[] | null {
|
|
563
|
+
const e = indexOf(repo).under.get(touchKey(dir, repo.caseInsensitive));
|
|
564
|
+
if (e === undefined || e.count === 0 || e.count > MAX_DIR_EXPANSION) return null;
|
|
565
|
+
return e.files;
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
/**
|
|
569
|
+
* The footprint of one task. D5: a present `Writes:` line is closed (verbs, mentions and ownership rows add no writes)
|
|
570
|
+
* and so is a present `Reads:` line (mentions add no reads). A path both written and read stays in `writes` only. New
|
|
571
|
+
* files the task creates are added, with their parent directories, to `createdEarlier`. A directory (or glob) write
|
|
572
|
+
* expands to its tracked files (O2); when it cannot, confidence is `unknown` and the task writes ⊤ (D4).
|
|
573
|
+
* Cost is O(|body|) plus set lookups per task.
|
|
574
|
+
*/
|
|
575
|
+
export function extractFootprint(
|
|
576
|
+
t: FootprintInput, phaseOwnership: readonly OwnershipInput[], repo: RepoView, createdEarlier: Set<string>,
|
|
577
|
+
): TaskFootprint {
|
|
578
|
+
const ci = repo.caseInsensitive;
|
|
579
|
+
const evidence: TaskFootprint["evidence"] = [];
|
|
580
|
+
const ignored: TaskFootprint["ignored"] = [];
|
|
581
|
+
const foreignRoots: string[] = [];
|
|
582
|
+
const sources: Source[] = [];
|
|
583
|
+
const writes: string[] = [];
|
|
584
|
+
const writeKeys = new Set<string>();
|
|
585
|
+
const reads: string[] = [];
|
|
586
|
+
const readKeys = new Set<string>();
|
|
587
|
+
const declared = new Set<string>();
|
|
588
|
+
const mentions: Ok[] = [];
|
|
589
|
+
let explicit = false;
|
|
590
|
+
let owned = false;
|
|
591
|
+
let unbounded = false;
|
|
592
|
+
let weak = false;
|
|
593
|
+
let writeIgnored = false;
|
|
594
|
+
let assumed = false;
|
|
595
|
+
|
|
596
|
+
const note = (s: Source): void => { if (!sources.includes(s)) sources.push(s); };
|
|
597
|
+
const created = (p: string): void => {
|
|
598
|
+
createdEarlier.add(p);
|
|
599
|
+
let cut = p.lastIndexOf("/");
|
|
600
|
+
while (cut > 0) {
|
|
601
|
+
createdEarlier.add(p.slice(0, cut));
|
|
602
|
+
cut = p.lastIndexOf("/", cut - 1);
|
|
603
|
+
}
|
|
604
|
+
};
|
|
605
|
+
const resolve = (raw: string, line: number, allowNew: boolean, report: boolean): Ok | null => {
|
|
606
|
+
const r = resolveDetailed(raw, repo, { allowNew, createdEarlier });
|
|
607
|
+
if (r.outcome.ok) {
|
|
608
|
+
if (r.foreignRoot !== null && !foreignRoots.includes(r.foreignRoot)) foreignRoots.push(r.foreignRoot);
|
|
609
|
+
return r.outcome;
|
|
610
|
+
}
|
|
611
|
+
if (report) ignored.push({ raw: r.outcome.raw, reason: r.outcome.reason, line });
|
|
612
|
+
return null;
|
|
613
|
+
};
|
|
614
|
+
/**
|
|
615
|
+
* Resolve a path found in a write context (QA-2.3-1). A path that is `not-found` is kept as a new write (lexically
|
|
616
|
+
* normalized, escape-checked) so a dropped candidate never leaves an `explicit` footprint short; an `ambiguous`
|
|
617
|
+
* basename writes every match; a glob or path that cannot be represented makes the task write ⊤. Only truly outside
|
|
618
|
+
* spellings (url, device, unc, other drive, escape, outside) are ignored: they return null and set `writeIgnored`.
|
|
619
|
+
*/
|
|
620
|
+
const resolveWrites = (raw: string, line: number, allowNew: boolean, creates: boolean): Ok[] | null => {
|
|
621
|
+
const r = resolveDetailed(raw, repo, { allowNew, createdEarlier });
|
|
622
|
+
if (r.outcome.ok) {
|
|
623
|
+
if (r.foreignRoot !== null && !foreignRoots.includes(r.foreignRoot)) foreignRoots.push(r.foreignRoot);
|
|
624
|
+
return [r.outcome];
|
|
625
|
+
}
|
|
626
|
+
const o = r.outcome;
|
|
627
|
+
if (o.reason === "ambiguous") {
|
|
628
|
+
const ms = repo.byBasename.get(touchKey(raw.trim().replace(/[\\/]+$/, ""), ci)) ?? [];
|
|
629
|
+
if (ms.length > 1) return ms.map((path): Ok => ({ ok: true, path, via: "basename", dir: false }));
|
|
630
|
+
}
|
|
631
|
+
if (o.reason === "not-found") {
|
|
632
|
+
if (raw.split(/[\\/]/).some((s) => GLOB_CHARS.test(s))) {
|
|
633
|
+
unbounded = true;
|
|
634
|
+
ignored.push({ raw, reason: "dir-write-unbounded", line });
|
|
635
|
+
return [];
|
|
636
|
+
}
|
|
637
|
+
// QA-2.3-18: an unknown candidate in prose ("Update `Foo.bar`") is a write only when the context says it creates
|
|
638
|
+
// a file (Writes: line, create verb, `(new)`) or when it is spelled as a path (it has a separator).
|
|
639
|
+
if (!creates && !/[\\/]/.test(raw.trim().replace(/[\\/]+$/, ""))) {
|
|
640
|
+
ignored.push({ raw, reason: "not-found", line });
|
|
641
|
+
return null;
|
|
642
|
+
}
|
|
643
|
+
const lex = lexicalNew(raw);
|
|
644
|
+
if (lex !== null) return [{ ok: true, path: lex, via: "new", dir: false }];
|
|
645
|
+
unbounded = true;
|
|
646
|
+
ignored.push({ raw, reason: "write-unresolved", line });
|
|
647
|
+
return [];
|
|
648
|
+
}
|
|
649
|
+
if (o.reason === "url") {
|
|
650
|
+
// QA-2.3-23: a URL is never a write target; it is ignored like a read and does not widen the task.
|
|
651
|
+
ignored.push({ raw: o.raw, reason: o.reason, line });
|
|
652
|
+
return null;
|
|
653
|
+
}
|
|
654
|
+
writeIgnored = true;
|
|
655
|
+
ignored.push({ raw: o.raw, reason: o.reason, line, write: true });
|
|
656
|
+
return null;
|
|
657
|
+
};
|
|
658
|
+
/**
|
|
659
|
+
* An ownership row: resolved like a new write; a row whose parent directory does not exist yet (a phase that creates
|
|
660
|
+
* the tree) falls back to its lexical spelling, the same fallback `resolveWrites` applies (QA-2.3-30).
|
|
661
|
+
*/
|
|
662
|
+
const resolveRow = (raw: string, line: number): Ok | null => {
|
|
663
|
+
const ok = resolve(raw, line, true, false);
|
|
664
|
+
if (ok !== null) return ok;
|
|
665
|
+
const r = resolveDetailed(raw, repo, { allowNew: true, createdEarlier });
|
|
666
|
+
if (r.outcome.ok || r.outcome.reason !== "not-found") return null;
|
|
667
|
+
if (raw.split(/[\\/]/).some((s) => GLOB_CHARS.test(s))) return null;
|
|
668
|
+
const lex = lexicalNew(raw);
|
|
669
|
+
return lex === null ? null : { ok: true, path: lex, via: "new", dir: /[\\/]$/.test(raw.trim()) };
|
|
670
|
+
};
|
|
671
|
+
const putWrite = (p: string, source: Source, line: number, rule: string): void => {
|
|
672
|
+
const key = touchKey(p, ci);
|
|
673
|
+
if (!writeKeys.has(key)) { writeKeys.add(key); writes.push(p); }
|
|
674
|
+
evidence.push({ path: p, access: "write", source, line, rule });
|
|
675
|
+
if (lookup(repo, p) === null) created(p);
|
|
676
|
+
};
|
|
677
|
+
const addWrite = (ok: Ok, raw: string, source: Source, line: number, rule: string): void => {
|
|
678
|
+
note(source);
|
|
679
|
+
if (ok.dir) {
|
|
680
|
+
// A write of the repository root itself is unbounded (⊤): it names no file.
|
|
681
|
+
const files = ok.path === "" ? null : expandDir(repo, ok.path);
|
|
682
|
+
if (files === null) {
|
|
683
|
+
unbounded = true;
|
|
684
|
+
ignored.push({ raw, reason: "dir-write-unbounded", line });
|
|
685
|
+
return;
|
|
686
|
+
}
|
|
687
|
+
for (const f of files) putWrite(f, source, line, rule);
|
|
688
|
+
// QA-2.3-16: the directory itself stays a key so a file created below it later still overlaps this write.
|
|
689
|
+
if (ok.path !== "") putWrite(ok.path, source, line, rule);
|
|
690
|
+
return;
|
|
691
|
+
}
|
|
692
|
+
if (ok.via === "suffix" || ok.via === "basename") weak = true;
|
|
693
|
+
putWrite(ok.path, source, line, rule);
|
|
694
|
+
};
|
|
695
|
+
const addRead = (ok: Ok, raw: string, source: Source, line: number, rule: string): void => {
|
|
696
|
+
if (ok.path === "") {
|
|
697
|
+
ignored.push({ raw, reason: "repo-root", line });
|
|
698
|
+
return;
|
|
699
|
+
}
|
|
700
|
+
const key = touchKey(ok.path, ci);
|
|
701
|
+
if (!readKeys.has(key)) { readKeys.add(key); reads.push(ok.path); }
|
|
702
|
+
evidence.push({ path: ok.path, access: "read", source, line, rule });
|
|
703
|
+
note(source);
|
|
704
|
+
};
|
|
705
|
+
|
|
706
|
+
// Writes: line (closed).
|
|
707
|
+
if (t.writesLine !== null) {
|
|
708
|
+
if (t.writesLine.none) {
|
|
709
|
+
explicit = true;
|
|
710
|
+
note("writes-line");
|
|
711
|
+
} else {
|
|
712
|
+
for (const c of scanText(t.writesLine.text, t.writesLine.line)) {
|
|
713
|
+
declared.add(c.raw);
|
|
714
|
+
const oks = resolveWrites(c.raw, c.line, true, true);
|
|
715
|
+
if (oks === null) continue;
|
|
716
|
+
explicit = true;
|
|
717
|
+
if (oks.length === 0) note("writes-line");
|
|
718
|
+
for (const ok of oks) addWrite(ok, c.raw, "writes-line", c.line, "writes-line");
|
|
719
|
+
}
|
|
720
|
+
}
|
|
721
|
+
}
|
|
722
|
+
// Reads: line (closed).
|
|
723
|
+
if (t.readsLine !== null && !t.readsLine.none) {
|
|
724
|
+
for (const c of scanText(t.readsLine.text, t.readsLine.line)) {
|
|
725
|
+
declared.add(c.raw);
|
|
726
|
+
const ok = resolve(c.raw, c.line, false, false);
|
|
727
|
+
if (ok !== null) {
|
|
728
|
+
addRead(ok, c.raw, "writes-line", c.line, "reads-line");
|
|
729
|
+
continue;
|
|
730
|
+
}
|
|
731
|
+
// QA-2.3-20: a declared read of a file that is not tracked yet is kept (lexically), so the task that creates it
|
|
732
|
+
// gets an edge to this reader.
|
|
733
|
+
const r = resolveDetailed(c.raw, repo, { allowNew: false, createdEarlier });
|
|
734
|
+
const lex = !r.outcome.ok && r.outcome.reason === "not-found" && !c.raw.split(/[\\/]/).some((s) => GLOB_CHARS.test(s))
|
|
735
|
+
? lexicalNew(c.raw) : null;
|
|
736
|
+
if (lex !== null) addRead({ ok: true, path: lex, via: "new", dir: false }, c.raw, "writes-line", c.line, "reads-line");
|
|
737
|
+
else ignored.push({ raw: r.outcome.ok ? c.raw : r.outcome.raw, reason: r.outcome.ok ? "not-found" : r.outcome.reason, line: c.line });
|
|
738
|
+
}
|
|
739
|
+
}
|
|
740
|
+
// Body mentions: write verbs (no Writes: line) and reads (no Reads: line).
|
|
741
|
+
const wantOwnership = t.writesLine === null && phaseOwnership.length > 0;
|
|
742
|
+
if (t.writesLine === null || t.readsLine === null) {
|
|
743
|
+
t.body.split(/\r?\n/).forEach((text, i) => {
|
|
744
|
+
for (const c of scanLine(text, i + 1)) {
|
|
745
|
+
if (declared.has(c.raw)) continue;
|
|
746
|
+
const marked = NEW_MARKER.test(c.text.slice(c.end));
|
|
747
|
+
const verb = t.writesLine === null ? verbBefore(c) : null;
|
|
748
|
+
if (verb !== null) {
|
|
749
|
+
const oks = resolveWrites(c.raw, c.line, verb.create || marked, verb.create || marked);
|
|
750
|
+
if (oks === null) continue;
|
|
751
|
+
explicit = true;
|
|
752
|
+
if (oks.length === 0) note("path");
|
|
753
|
+
for (const ok of oks) {
|
|
754
|
+
mentions.push(ok);
|
|
755
|
+
addWrite(ok, c.raw, "path", c.line, verb.rule);
|
|
756
|
+
}
|
|
757
|
+
} else if (t.readsLine === null) {
|
|
758
|
+
const ok = resolve(c.raw, c.line, marked, true);
|
|
759
|
+
if (ok === null) {
|
|
760
|
+
if (wantOwnership) {
|
|
761
|
+
const guess = resolveRow(c.raw, c.line);
|
|
762
|
+
if (guess !== null) mentions.push(guess);
|
|
763
|
+
}
|
|
764
|
+
continue;
|
|
765
|
+
}
|
|
766
|
+
mentions.push(ok);
|
|
767
|
+
addRead(ok, c.raw, "path", c.line, "mention");
|
|
768
|
+
} else if (wantOwnership) {
|
|
769
|
+
const ok = resolveRow(c.raw, c.line);
|
|
770
|
+
if (ok !== null) mentions.push(ok);
|
|
771
|
+
}
|
|
772
|
+
}
|
|
773
|
+
});
|
|
774
|
+
}
|
|
775
|
+
// Ownership rows of the task's phase that the task mentions (no Writes: line).
|
|
776
|
+
if (wantOwnership) {
|
|
777
|
+
for (const row of phaseOwnership) {
|
|
778
|
+
const rp = resolveRow(row.path, row.line);
|
|
779
|
+
if (rp === null || rp.path === "") continue;
|
|
780
|
+
if (!mentions.some((m) => overlaps(m.path, rp.path, ci))) continue;
|
|
781
|
+
owned = true;
|
|
782
|
+
addWrite(rp, row.path, "ownership", row.line, "ownership-row");
|
|
783
|
+
}
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
const cls = t.route?.fields?.class;
|
|
787
|
+
const needs = t.route?.fields?.needs;
|
|
788
|
+
// D9: an editing task with no write evidence of its own writes what its phase owns. The ownership table declares the
|
|
789
|
+
// phase's write set ("Owns (writes)"), so this is a bounded over-approximation (low confidence) instead of ⊤.
|
|
790
|
+
// An automatically inferred read-only class must not suppress the phase's declared ownership (G6).
|
|
791
|
+
const editsByDefault = !t.manual && !NEVER_EDITING_KINDS.has(t.kind)
|
|
792
|
+
&& (t.route?.inferred === true || (needs !== undefined ? needs.includes("edit") : cls === undefined || !NON_EDITING_CLASSES.has(cls)));
|
|
793
|
+
// QA-2.3-5: skipped when a write candidate was ignored (the task keeps ⊤), and the result is `inferred`, not
|
|
794
|
+
// `ownership`, so the residue may still add to it.
|
|
795
|
+
if (wantOwnership && editsByDefault && writes.length === 0 && !unbounded && !explicit && !writeIgnored) {
|
|
796
|
+
for (const row of phaseOwnership) {
|
|
797
|
+
const rp = resolveRow(row.path, row.line);
|
|
798
|
+
if (rp === null || rp.path === "") continue;
|
|
799
|
+
assumed = true;
|
|
800
|
+
weak = true;
|
|
801
|
+
addWrite(rp, row.path, "ownership", row.line, "ownership-phase");
|
|
802
|
+
}
|
|
803
|
+
}
|
|
804
|
+
|
|
805
|
+
const finalReads = reads.filter((r) => !writeKeys.has(touchKey(r, ci)));
|
|
806
|
+
const hasWriteEvidence = writes.length > 0 || unbounded;
|
|
807
|
+
let editing: boolean;
|
|
808
|
+
if (t.manual || NEVER_EDITING_KINDS.has(t.kind)) editing = false;
|
|
809
|
+
else if (needs !== undefined) editing = needs.includes("edit") || hasWriteEvidence;
|
|
810
|
+
else editing = cls === undefined || !NON_EDITING_CLASSES.has(cls) || hasWriteEvidence;
|
|
811
|
+
|
|
812
|
+
let confidence: Footprint["confidence"];
|
|
813
|
+
if (unbounded) confidence = "unknown";
|
|
814
|
+
else if (explicit) confidence = "explicit";
|
|
815
|
+
else if (owned) confidence = "ownership";
|
|
816
|
+
else if (assumed) confidence = "inferred";
|
|
817
|
+
else confidence = editing ? "unknown" : "inferred";
|
|
818
|
+
const lowConfidence = editing && (confidence === "unknown" || confidence === "inferred" || weak);
|
|
819
|
+
|
|
820
|
+
return {
|
|
821
|
+
footprint: { writes, reads: finalReads, confidence, sources },
|
|
822
|
+
editing,
|
|
823
|
+
lowConfidence,
|
|
824
|
+
ownershipAssumed: assumed,
|
|
825
|
+
evidence,
|
|
826
|
+
ignored,
|
|
827
|
+
foreignRoots,
|
|
828
|
+
};
|
|
829
|
+
}
|