@jwilger/pi-development-system 0.8.2 → 0.8.3
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/package.json
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { splitLines } from "./git-intent.ts";
|
|
1
2
|
import { resolveGit } from "./git-invocations.ts";
|
|
2
3
|
|
|
3
4
|
export type CommitExtraction =
|
|
@@ -7,20 +8,21 @@ export type CommitExtraction =
|
|
|
7
8
|
| { readonly kind: "unknown"; readonly trailers: readonly string[] };
|
|
8
9
|
|
|
9
10
|
const HEREDOC =
|
|
10
|
-
/<<-?[ \t]*(['"]?)([A-Za-z_][A-Za-z0-9_]*)\1[^\n]
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
|
|
14
|
-
|
|
11
|
+
/<<-?[ \t]*(['"]?)([A-Za-z_][A-Za-z0-9_]*)\1([^\n]*)\n([\s\S]*?)\n[ \t]*\2[ \t]*(?:\n|$)/g;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Heredoc bodies are data: take them out so their quotes cannot confuse line splitting. The rest of
|
|
15
|
+
* the opening line stays (`| git commit -F -`) with a `#HD<n>` marker naming the removed body.
|
|
16
|
+
*/
|
|
17
|
+
function stripHeredocs(command: string): { rest: string; bodies: string[] } {
|
|
18
|
+
const bodies: string[] = [];
|
|
15
19
|
const rest = command.replace(HEREDOC, (...m: unknown[]) => {
|
|
16
|
-
bodies.push(
|
|
17
|
-
return
|
|
20
|
+
bodies.push(String(m[4]).trim());
|
|
21
|
+
return `${String(m[3])} #HD${bodies.length - 1}\n`;
|
|
18
22
|
});
|
|
19
23
|
return { rest, bodies };
|
|
20
24
|
}
|
|
21
25
|
|
|
22
|
-
type Heredoc = { readonly body: string; readonly at: number };
|
|
23
|
-
|
|
24
26
|
type Options = {
|
|
25
27
|
messages: string[];
|
|
26
28
|
trailers: string[];
|
|
@@ -35,10 +37,11 @@ type Parsed = {
|
|
|
35
37
|
readonly next: number;
|
|
36
38
|
};
|
|
37
39
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
["--
|
|
41
|
-
["--
|
|
40
|
+
/** Option, kind and the shortest prefix git still resolves uniquely (`--fi` would be `--fixup`). */
|
|
41
|
+
const LONG: ReadonlyArray<readonly [string, Parsed["kind"], number]> = [
|
|
42
|
+
["--message", "message", 4],
|
|
43
|
+
["--file", "file", 5],
|
|
44
|
+
["--trailer", "trailer", 4],
|
|
42
45
|
];
|
|
43
46
|
|
|
44
47
|
/** The option a `--name`/`--na=value` spelling means; git accepts any unambiguous prefix (3+ chars). */
|
|
@@ -46,11 +49,9 @@ function longOption(arg: string): { kind: Parsed["kind"]; inline: string | undef
|
|
|
46
49
|
if (!arg.startsWith("--")) return undefined;
|
|
47
50
|
const eq = arg.indexOf("=");
|
|
48
51
|
const name = eq < 0 ? arg : arg.slice(0, eq);
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
return hit
|
|
52
|
-
? undefined
|
|
53
|
-
: { kind: hit[1], inline: eq < 0 ? undefined : arg.slice(eq + 1) };
|
|
52
|
+
const hit = LONG.find(([long, , min]) => name.length >= min && long.startsWith(name));
|
|
53
|
+
if (hit === undefined) return undefined;
|
|
54
|
+
return { kind: hit[1], inline: eq < 0 ? undefined : arg.slice(eq + 1) };
|
|
54
55
|
}
|
|
55
56
|
|
|
56
57
|
/** Reads the message/file/trailer option starting at args[i], in any of its spellings. */
|
|
@@ -71,17 +72,21 @@ function parseArg(args: readonly string[], i: number): Parsed | undefined {
|
|
|
71
72
|
return undefined;
|
|
72
73
|
}
|
|
73
74
|
|
|
75
|
+
const OPAQUE_VALUE = /\$\(|`|^\$\{?\w+\}?$/;
|
|
76
|
+
|
|
77
|
+
function record(options: Options, parsed: Parsed): void {
|
|
78
|
+
if (parsed.kind === "file") options.file = parsed.value;
|
|
79
|
+
else if (parsed.kind === "trailer") options.trailers.push(parsed.value ?? "");
|
|
80
|
+
else if (parsed.value === undefined || OPAQUE_VALUE.test(parsed.value)) options.opaque = true;
|
|
81
|
+
else options.messages.push(parsed.value);
|
|
82
|
+
}
|
|
83
|
+
|
|
74
84
|
function readOptions(args: readonly string[]): Options {
|
|
75
85
|
const options: Options = { messages: [], trailers: [], file: undefined, opaque: false };
|
|
76
|
-
let i = 0;
|
|
77
|
-
while (i < args.length) {
|
|
86
|
+
for (let i = 0; i < args.length; ) {
|
|
78
87
|
const parsed = parseArg(args, i);
|
|
88
|
+
if (parsed !== undefined) record(options, parsed);
|
|
79
89
|
i = parsed?.next ?? i + 1;
|
|
80
|
-
if (parsed === undefined) continue;
|
|
81
|
-
if (parsed.kind === "file") options.file = parsed.value;
|
|
82
|
-
else if (parsed.kind === "trailer") options.trailers.push(parsed.value ?? "");
|
|
83
|
-
else if (parsed.value === undefined || /\$\(|`/.test(parsed.value)) options.opaque = true;
|
|
84
|
-
else options.messages.push(parsed.value);
|
|
85
90
|
}
|
|
86
91
|
return options;
|
|
87
92
|
}
|
|
@@ -92,18 +97,22 @@ const unknown = (trailers: readonly string[] = []): CommitExtraction => ({
|
|
|
92
97
|
});
|
|
93
98
|
|
|
94
99
|
const STDIN_PATHS = new Set(["-", "/dev/stdin", "/proc/self/fd/0"]);
|
|
100
|
+
const MARKER = /#HD(\d+)/g;
|
|
95
101
|
|
|
96
|
-
/**
|
|
97
|
-
const
|
|
98
|
-
const
|
|
99
|
-
|
|
102
|
+
/** Body of the first heredoc opened on this line or later (its stdin, or the `$(cat <<EOF)` message). */
|
|
103
|
+
const heredocFrom = (lines: readonly string[], line: number, bodies: readonly string[]) => {
|
|
104
|
+
for (const text of lines.slice(line)) {
|
|
105
|
+
const hit = [...text.matchAll(MARKER)][0];
|
|
106
|
+
if (hit !== undefined) return bodies[Number(hit[1])];
|
|
107
|
+
}
|
|
108
|
+
return undefined;
|
|
100
109
|
};
|
|
101
110
|
|
|
102
111
|
function extractOne(args: readonly string[], heredoc: string | undefined): CommitExtraction {
|
|
103
112
|
const options = readOptions(args);
|
|
104
113
|
if (options.file !== undefined && STDIN_PATHS.has(options.file)) {
|
|
105
114
|
return heredoc === undefined
|
|
106
|
-
?
|
|
115
|
+
? { kind: "file", path: "-" }
|
|
107
116
|
: { kind: "message", message: heredoc };
|
|
108
117
|
}
|
|
109
118
|
if (options.file !== undefined) return { kind: "file", path: options.file };
|
|
@@ -121,9 +130,10 @@ function extractOne(args: readonly string[], heredoc: string | undefined): Commi
|
|
|
121
130
|
export function extractCommits(command: string): CommitExtraction[] {
|
|
122
131
|
const { rest, bodies } = stripHeredocs(command);
|
|
123
132
|
const resolution = resolveGit(rest);
|
|
124
|
-
const
|
|
125
|
-
const
|
|
126
|
-
|
|
133
|
+
const lines = splitLines(rest);
|
|
134
|
+
const found = resolution.invocations.flatMap((g) =>
|
|
135
|
+
g.sub === "commit" ? [extractOne(g.args, heredocFrom(lines, g.line, bodies))] : [],
|
|
136
|
+
);
|
|
127
137
|
const hidden = resolution.opaque && /\bcommit\b/.test(command);
|
|
128
138
|
return hidden ? [...found, unknown()] : found;
|
|
129
139
|
}
|
|
@@ -93,7 +93,8 @@ export function findForbiddenTrailers(
|
|
|
93
93
|
});
|
|
94
94
|
}
|
|
95
95
|
|
|
96
|
-
const AI_TRAILER_ANYWHERE =
|
|
96
|
+
const AI_TRAILER_ANYWHERE =
|
|
97
|
+
/(?:co-authored-by|generated-by)\s*[:=]|signed-off-by\s*[:=][^\n]*\b(?:ai|claude|copilot|codex|gpt|chatgpt|openai|anthropic|gemini|llm)\b/gi;
|
|
97
98
|
|
|
98
99
|
/** AI-attribution trailer keys anywhere in free text (command lines, quoted strings, printf bodies). */
|
|
99
100
|
export function findForbiddenTrailerKeys(text: string): string[] {
|
|
@@ -19,6 +19,8 @@ export type GitInvocation = {
|
|
|
19
19
|
readonly args: readonly string[];
|
|
20
20
|
/** Directory the command runs in, relative to the starting one (from `cd` and `git -C`), when it moved. */
|
|
21
21
|
readonly dir?: string;
|
|
22
|
+
/** Index of the (newline-split) line of the top-level command this invocation sits on. */
|
|
23
|
+
readonly line: number;
|
|
22
24
|
};
|
|
23
25
|
|
|
24
26
|
export type GitResolution = {
|
|
@@ -27,7 +29,12 @@ export type GitResolution = {
|
|
|
27
29
|
readonly opaque: boolean;
|
|
28
30
|
};
|
|
29
31
|
|
|
30
|
-
type Collector = {
|
|
32
|
+
type Collector = {
|
|
33
|
+
invocations: GitInvocation[];
|
|
34
|
+
opaque: boolean;
|
|
35
|
+
dir: string | undefined;
|
|
36
|
+
line: number;
|
|
37
|
+
};
|
|
31
38
|
|
|
32
39
|
const firstCommand = (tokens: readonly string[]): number => {
|
|
33
40
|
const at = tokens.findIndex((t) => !(isAssignment(t) || WRAPPERS.has(t) || KEYWORDS.has(t)));
|
|
@@ -53,7 +60,8 @@ function visitGit(rest: readonly string[], into: Collector): void {
|
|
|
53
60
|
if (sub.startsWith("$")) into.opaque = true;
|
|
54
61
|
else {
|
|
55
62
|
const args = rest.slice(j + 1);
|
|
56
|
-
|
|
63
|
+
const line = into.line;
|
|
64
|
+
into.invocations.push(dir === undefined ? { sub, args, line } : { sub, args, dir, line });
|
|
57
65
|
}
|
|
58
66
|
}
|
|
59
67
|
|
|
@@ -85,14 +93,17 @@ function visitSegment(tokens: readonly string[], into: Collector): void {
|
|
|
85
93
|
else if (SHELLS.has(base) || base === "eval") visitScript(base, rest, into);
|
|
86
94
|
else if (OPAQUE.has(base)) into.opaque = into.opaque || gitIndex(rest) >= 0;
|
|
87
95
|
else if (base === "git") visitGit(rest, into);
|
|
88
|
-
else
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
96
|
+
else visitWrapped(base, rest, into);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** Wrapper with options (`sudo -E git …`, `timeout 5 git …`): resolve from the git token. */
|
|
100
|
+
function visitWrapped(base: string, rest: readonly string[], into: Collector): void {
|
|
101
|
+
const at = gitIndex(rest);
|
|
102
|
+
if (!DATA_ONLY.has(base) && at >= 0) visitSegment(rest.slice(at), into);
|
|
92
103
|
}
|
|
93
104
|
|
|
94
105
|
function merge(into: Collector, other: GitResolution): void {
|
|
95
|
-
into.invocations.push(...other.invocations);
|
|
106
|
+
into.invocations.push(...other.invocations.map((g) => ({ ...g, line: into.line })));
|
|
96
107
|
if (other.opaque) into.opaque = true;
|
|
97
108
|
}
|
|
98
109
|
|
|
@@ -101,9 +112,11 @@ function merge(into: Collector, other: GitResolution): void {
|
|
|
101
112
|
* `timeout`, `if … then`), `bash -c`/`eval` scripts and `$(…)` substitutions. Heredoc bodies are data.
|
|
102
113
|
*/
|
|
103
114
|
export function resolveGit(command: string): GitResolution {
|
|
104
|
-
const into: Collector = { invocations: [], opaque: false, dir: undefined };
|
|
115
|
+
const into: Collector = { invocations: [], opaque: false, dir: undefined, line: 0 };
|
|
105
116
|
let heredocEnd: string | undefined;
|
|
106
|
-
|
|
117
|
+
const lines = splitLines(command);
|
|
118
|
+
for (const [index, line] of lines.entries()) {
|
|
119
|
+
into.line = index;
|
|
107
120
|
if (heredocEnd !== undefined) {
|
|
108
121
|
if (line.trim() === heredocEnd) heredocEnd = undefined;
|
|
109
122
|
continue;
|
|
@@ -152,6 +152,13 @@ export function registerCommitGuard(deps: CommitGuardDeps): void {
|
|
|
152
152
|
|
|
153
153
|
const known = messages.flatMap((m) => (m === undefined ? [] : [m]));
|
|
154
154
|
const all: Need[] = [];
|
|
155
|
+
// A -F file that cannot be read now (written by this same command, or stdin) cannot be checked.
|
|
156
|
+
if (extractions.some((e, i) => e.kind === "file" && messages[i] === undefined)) {
|
|
157
|
+
all.push({
|
|
158
|
+
gate: RATIONALE,
|
|
159
|
+
why: "the message file cannot be read before the commit runs, so its rationale cannot be checked; use -m or a heredoc",
|
|
160
|
+
});
|
|
161
|
+
}
|
|
155
162
|
for (const message of known) {
|
|
156
163
|
for (const need of await needsOf(deps, ctx, message)) {
|
|
157
164
|
if (!all.some((n) => n.gate === need.gate)) all.push(need);
|
package/src/gates/push-guard.ts
CHANGED
|
@@ -32,6 +32,7 @@ const gate = (id: string): GateId => {
|
|
|
32
32
|
if (isParseError(parsed)) throw new Error(parsed.message);
|
|
33
33
|
return parsed;
|
|
34
34
|
};
|
|
35
|
+
const FIX_DIFF_LIMIT = 8000;
|
|
35
36
|
const RED_TRUNK = gate("push.red-trunk");
|
|
36
37
|
const DELIVERY_MODE = gate("push.delivery-mode");
|
|
37
38
|
|
|
@@ -54,7 +55,11 @@ async function pushesTrunk(
|
|
|
54
55
|
for (const t of targets) {
|
|
55
56
|
if (t.allBranches || t.branches.includes(trunk)) return true;
|
|
56
57
|
const where = t.dir === undefined ? cwd : resolve(cwd, t.dir);
|
|
57
|
-
if (t.branches.length === 0
|
|
58
|
+
if (t.branches.length === 0) {
|
|
59
|
+
// No refspec: the current branch decides. When it cannot be read, assume it is the trunk.
|
|
60
|
+
const branch = await currentBranch(exec, where);
|
|
61
|
+
if (branch === undefined || branch === trunk) return true;
|
|
62
|
+
}
|
|
58
63
|
}
|
|
59
64
|
return false;
|
|
60
65
|
}
|
|
@@ -74,7 +79,10 @@ async function repairsRedTrunk(
|
|
|
74
79
|
if (message.code !== 0 || !parsed.ok || parsed.value.type !== "fix") return false;
|
|
75
80
|
const range = `${config.delivery.remote}/${config.delivery.trunk}..HEAD`;
|
|
76
81
|
const diff = await deps.exec("git", ["diff", range], { cwd: ctx.cwd, timeout: 10_000 });
|
|
77
|
-
|
|
82
|
+
// Jev only reads a bounded diff; a longer range could hide unrelated change past what it sees.
|
|
83
|
+
if (diff.code !== 0 || diff.stdout.trim() === "" || diff.stdout.length > FIX_DIFF_LIMIT) {
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
78
86
|
const failingLog = await getFailureLog(deps.exec, ctx.cwd, runId);
|
|
79
87
|
if (failingLog.trim() === "") return false; // nothing to compare the diff against: fail closed
|
|
80
88
|
const judged = await judgeFixRelated(deps.jev(ctx), {
|