@jwilger/pi-development-system 0.20.0 → 0.21.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jwilger/pi-development-system",
3
- "version": "0.20.0",
3
+ "version": "0.21.0",
4
4
  "description": "A pi extension package representing a seasoned approach to software development using a full AI SDLC.",
5
5
  "keywords": [
6
6
  "pi-package"
@@ -50,8 +50,8 @@ The verdict must agree with the findings.
50
50
  - **false-positive**: refuted; ignored.
51
51
 
52
52
  Jev may raise a finding's severity when it is at least 0.8 confident; it never
53
- lowers a blocking or should-fix finding (it only suggests, in the reply, and you
54
- re-check it yourself). A round is clean when it has no blocking or should-fix findings.
53
+ lowers a finding to a severity that does not count (it only suggests, in the
54
+ reply, and you re-check it yourself). A round is clean when it has no blocking or should-fix findings.
55
55
  The default is three consecutive clean rounds (`review.required_clean_rounds`).
56
56
  Only real findings reset the count; a changed diff alone does not, so a small fix
57
57
  commit keeps the clean rounds already earned unless findings come back.
@@ -119,7 +119,11 @@ async function jevNeeds(
119
119
  }
120
120
 
121
121
  /** While work is in flight, committing needs the slice review satisfied on the diff as it is now. */
122
- async function reviewNeeds(deps: CommitGuardDeps, ctx: ExtensionContext): Promise<Need[]> {
122
+ async function reviewNeeds(
123
+ deps: CommitGuardDeps,
124
+ ctx: ExtensionContext,
125
+ command: string,
126
+ ): Promise<Need[]> {
123
127
  const { phase, activeSlice } = deps.state.get();
124
128
  if ((phase !== "implementing" && phase !== "reviewing") || activeSlice === undefined) return [];
125
129
  const snap = await snapshotDiff(deps.exec, ctx.cwd, "HEAD");
@@ -129,7 +133,45 @@ async function reviewNeeds(deps: CommitGuardDeps, ctx: ExtensionContext): Promis
129
133
  activeSlice,
130
134
  snap.ok ? { digest: snap.value.digest, files: snap.value.files } : { digest: "unknown" },
131
135
  );
132
- return gap === undefined ? [] : [{ gate: REVIEW, why: gap }];
136
+ if (gap !== undefined) return [{ gate: REVIEW, why: gap }];
137
+ // The review saw the work tree; a plain `git commit` records the index. A file staged and then edited
138
+ // again would commit the older, unreviewed content.
139
+ const stale = stagedThenEdited(await changedNames(deps.exec, ctx.cwd), command);
140
+ return stale.length === 0
141
+ ? []
142
+ : [
143
+ {
144
+ gate: REVIEW,
145
+ why: `the staged content of ${stale.join(", ")} differs from the work tree that was reviewed (edited after \`git add\`); stage it again`,
146
+ },
147
+ ];
148
+ }
149
+
150
+ /** Files staged and also changed since staging; empty when the commit stages everything itself. */
151
+ const stagedThenEdited = (
152
+ names: { staged: readonly string[]; unstaged: readonly string[] } | undefined,
153
+ command: string,
154
+ ): string[] =>
155
+ names === undefined || stagesEverything(command)
156
+ ? []
157
+ : names.staged.filter((name) => names.unstaged.includes(name));
158
+
159
+ async function changedNames(
160
+ exec: Exec,
161
+ cwd: string,
162
+ ): Promise<{ staged: string[]; unstaged: string[] } | undefined> {
163
+ try {
164
+ const base = ["-c", "core.quotePath=false", "diff", "--no-ext-diff", "--name-only", "-z"];
165
+ const [staged, unstaged] = await Promise.all([
166
+ exec("git", [...base, "--cached"], { cwd, timeout: 10_000 }),
167
+ exec("git", base, { cwd, timeout: 10_000 }),
168
+ ]);
169
+ if (staged.code !== 0 || unstaged.code !== 0) return undefined;
170
+ const split = (text: string) => text.split("\0").filter((n) => n !== "");
171
+ return { staged: split(staged.stdout), unstaged: split(unstaged.stdout) };
172
+ } catch {
173
+ return undefined;
174
+ }
133
175
  }
134
176
 
135
177
  const reviewReason = (need: Need): string =>
@@ -187,7 +229,7 @@ export function registerCommitGuard(deps: CommitGuardDeps): void {
187
229
  if (forbidden.length > 0) return { block: true, reason: forbiddenReason(forbidden) };
188
230
 
189
231
  const known = messages.flatMap((m) => (m === undefined ? [] : [m]));
190
- const all: Need[] = await reviewNeeds(deps, ctx);
232
+ const all: Need[] = await reviewNeeds(deps, ctx, command);
191
233
  // A -F file that cannot be read now (written by this same command, or stdin) cannot be checked.
192
234
  if (extractions.some((e, i) => e.kind === "file" && messages[i] === undefined)) {
193
235
  all.push({
@@ -1,4 +1,6 @@
1
1
  import { createHash } from "node:crypto";
2
+ import { lstat, readlink } from "node:fs/promises";
3
+ import { join } from "node:path";
2
4
  import type { Exec } from "../core/exec.ts";
3
5
  import { err, ok, type Result } from "../core/result.ts";
4
6
  import { splitDiffByFile } from "../core/review-flow.ts";
@@ -35,13 +37,18 @@ export async function snapshotDiff(
35
37
  const [diff, stat, others] = await Promise.all([
36
38
  exec("git", [...plain, "--full-index", "--no-renames", range], { cwd, timeout: 15_000 }),
37
39
  exec("git", [...plain, "--stat", range], { cwd, timeout: 15_000 }),
38
- range === "HEAD"
39
- ? exec("git", ["ls-files", "--others", "--exclude-standard"], { cwd, timeout: 15_000 })
40
+ // Any range that ends in the work tree (HEAD, HEAD~1, a branch) leaves untracked files out of git diff.
41
+ !range.includes("..")
42
+ ? exec(
43
+ "git",
44
+ ["-c", "core.quotePath=false", "ls-files", "-z", "--others", "--exclude-standard"],
45
+ { cwd, timeout: 15_000 },
46
+ )
40
47
  : Promise.resolve({ code: 0, stdout: "", stderr: "" }),
41
48
  ]);
42
49
  if (diff.code !== 0) return err(diff.stderr.trim() || `git diff ${range} failed`);
43
50
  if (others.code !== 0) return err(others.stderr.trim() || "git ls-files --others failed");
44
- const listed = others.stdout.split("\n").filter((p) => p !== "");
51
+ const listed = others.stdout.split("\0").filter((p) => p !== "");
45
52
  // A nested repository is listed as `dir/` and has no blob to hash.
46
53
  const nested = listed.filter((p) => p.endsWith("/"));
47
54
  const untracked = listed.filter((p) => !p.endsWith("/"));
@@ -58,15 +65,27 @@ export async function snapshotDiff(
58
65
  return err("could not read the diff: no per-file sections were found");
59
66
  }
60
67
  if (untracked.length > 0) {
61
- const hashes = await exec("git", ["hash-object", "--", ...untracked], {
62
- cwd,
63
- timeout: 15_000,
64
- });
65
- const lines = hashes.stdout.split("\n").filter((l) => l !== "");
66
- if (hashes.code !== 0 || lines.length !== untracked.length) {
67
- return err(hashes.stderr.trim() || "git hash-object could not hash every untracked file");
68
+ const links = new Set<string>();
69
+ for (const path of untracked) {
70
+ const target = await linkTarget(cwd, path);
71
+ if (target !== undefined) {
72
+ files[path] = `new:${blobOf(target)}`;
73
+ links.add(path);
74
+ }
75
+ }
76
+ // `git hash-object <path>` follows symlinks (and fails on dangling ones), so links are hashed above.
77
+ const regular = untracked.filter((p) => !links.has(p));
78
+ if (regular.length > 0) {
79
+ const hashes = await exec("git", ["hash-object", "--", ...regular], {
80
+ cwd,
81
+ timeout: 15_000,
82
+ });
83
+ const lines = hashes.stdout.split("\n").filter((l) => l !== "");
84
+ if (hashes.code !== 0 || lines.length !== regular.length) {
85
+ return err(hashes.stderr.trim() || "git hash-object could not hash every untracked file");
86
+ }
87
+ for (const [i, path] of regular.entries()) files[path] = `new:${lines[i]}`;
68
88
  }
69
- for (const [i, path] of untracked.entries()) files[path] = `new:${lines[i]}`;
70
89
  }
71
90
  const names = Object.keys(files).sort();
72
91
  const untrackedStat = [
@@ -91,3 +110,20 @@ const fileDigest = (section: string): string => {
91
110
  const blob = NEW_FILE.exec(section)?.[1];
92
111
  return blob === undefined ? digestOf(section) : `new:${blob}`;
93
112
  };
113
+
114
+ /** The text of a symlink, or undefined when `path` is not one (or cannot be inspected). */
115
+ async function linkTarget(cwd: string, path: string): Promise<string | undefined> {
116
+ try {
117
+ const full = join(cwd, path);
118
+ return (await lstat(full)).isSymbolicLink() ? await readlink(full) : undefined;
119
+ } catch {
120
+ return undefined;
121
+ }
122
+ }
123
+
124
+ /** Git's blob id for some content: what `git add` stores for a symlink is its target text. */
125
+ const blobOf = (content: string): string =>
126
+ createHash("sha1")
127
+ .update(`blob ${Buffer.byteLength(content)}\0`)
128
+ .update(content)
129
+ .digest("hex");