@a2f0/agent-tool 0.1.58 → 0.1.60

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.
@@ -63,6 +63,7 @@ function changedFiles(
63
63
  rootDir: string,
64
64
  mergeBase: string,
65
65
  packageDir: string,
66
+ head: string,
66
67
  ): string[] {
67
68
  return git(rootDir, [
68
69
  "diff",
@@ -70,7 +71,7 @@ function changedFiles(
70
71
  "--no-renames",
71
72
  "-z",
72
73
  mergeBase,
73
- "HEAD",
74
+ head,
74
75
  "--",
75
76
  packageDir,
76
77
  ])
@@ -87,9 +88,10 @@ function packageChanged(
87
88
  mergeBase: string,
88
89
  packageDir: string,
89
90
  headManifest: string,
91
+ head: string,
90
92
  ): boolean {
91
93
  const manifest = manifestPath(packageDir);
92
- const changed = changedFiles(rootDir, mergeBase, packageDir);
94
+ const changed = changedFiles(rootDir, mergeBase, packageDir, head);
93
95
  if (changed.some((file) => file !== manifest)) {
94
96
  return true;
95
97
  }
@@ -125,44 +127,46 @@ function bundledPackageChanged(
125
127
  rootDir: string,
126
128
  mergeBase: string,
127
129
  packageDir: string,
130
+ head: string,
128
131
  ): boolean {
129
132
  const manifest = manifestPath(packageDir);
130
- const changed = changedFiles(rootDir, mergeBase, packageDir);
133
+ const changed = changedFiles(rootDir, mergeBase, packageDir, head);
131
134
  if (changed.some((file) => file !== manifest)) return true;
132
135
  if (!changed.includes(manifest)) return false;
133
136
  return (
134
137
  withoutVersion(showFile(rootDir, mergeBase, manifest)) !==
135
- withoutVersion(showFile(rootDir, "HEAD", manifest))
138
+ withoutVersion(showFile(rootDir, head, manifest))
136
139
  );
137
140
  }
138
141
 
139
142
  /**
140
- * The version each package should carry at HEAD to merge onto `baseCommit`:
141
- * one patch past the base when the branch changes the package or a package it
142
- * bundles, the base's own version when it does not, and a deliberate major or
143
- * minor bump left alone.
143
+ * The version each package should carry at `head` (HEAD by default) to merge
144
+ * onto `baseCommit`: one patch past the base when the branch changes the
145
+ * package or a package it bundles, the base's own version when it does not,
146
+ * and a deliberate major or minor bump left alone.
144
147
  */
145
148
  export function planVersions(
146
149
  rootDir: string,
147
150
  baseOid: string | undefined,
151
+ head = "HEAD",
148
152
  ): VersionPlan[] {
149
153
  const baseCommit = resolveBaseCommit(rootDir, baseOid);
150
- const mergeBase = git(rootDir, ["merge-base", baseCommit, "HEAD"]).trim();
151
- const bundles = bundledPackages(rootDir);
154
+ const mergeBase = git(rootDir, ["merge-base", baseCommit, head]).trim();
155
+ const bundles = bundledPackages(rootDir, head);
152
156
  const bundledChanges = new Map<string, boolean>();
153
157
  const bundledChanged = (packageDir: string): boolean => {
154
158
  let result = bundledChanges.get(packageDir);
155
159
  if (result === undefined) {
156
- result = bundledPackageChanged(rootDir, mergeBase, packageDir);
160
+ result = bundledPackageChanged(rootDir, mergeBase, packageDir, head);
157
161
  bundledChanges.set(packageDir, result);
158
162
  }
159
163
  return result;
160
164
  };
161
165
  const plans: VersionPlan[] = [];
162
- for (const packageDir of workspacePackages(rootDir)) {
166
+ for (const packageDir of workspacePackages(rootDir, head)) {
163
167
  const manifest = manifestPath(packageDir);
164
168
  const baseManifest = showFile(rootDir, baseCommit, manifest);
165
- const headManifest = showFile(rootDir, "HEAD", manifest);
169
+ const headManifest = showFile(rootDir, head, manifest);
166
170
  if (headManifest === null) {
167
171
  continue;
168
172
  }
@@ -177,7 +181,7 @@ export function planVersions(
177
181
  if (isReleaseBump(headVersion, baseVersion)) {
178
182
  targetVersion = headVersion;
179
183
  } else if (
180
- packageChanged(rootDir, mergeBase, packageDir, headManifest) ||
184
+ packageChanged(rootDir, mergeBase, packageDir, headManifest, head) ||
181
185
  (bundles.get(packageDir) ?? []).some(bundledChanged)
182
186
  ) {
183
187
  targetVersion = bumpPatch(baseVersion);
@@ -12,16 +12,16 @@ interface WorkspaceManifest {
12
12
  dependencies?: unknown;
13
13
  }
14
14
 
15
- function headManifest(rootDir: string, packageDir: string): WorkspaceManifest {
15
+ function headManifest(rootDir: string, packageDir: string, commit: string): WorkspaceManifest {
16
16
  const file = manifestPath(packageDir);
17
- const result = spawnSync("git", ["show", `HEAD:${file}`], {
17
+ const result = spawnSync("git", ["show", `${commit}:${file}`], {
18
18
  cwd: rootDir,
19
19
  env: gitEnvironment(),
20
20
  encoding: "utf8",
21
21
  stdio: ["ignore", "pipe", "ignore"],
22
22
  });
23
23
  if (result.error) throw result.error;
24
- if (result.status !== 0) throw new Error(`${file} is not committed at HEAD.`);
24
+ if (result.status !== 0) throw new Error(`${file} is not committed at ${commit}.`);
25
25
  return JSON.parse(result.stdout);
26
26
  }
27
27
 
@@ -42,21 +42,21 @@ function workspaceDependencies(manifest: WorkspaceManifest): string[] {
42
42
  /**
43
43
  * The workspace packages each configured bundle ships inside its own artifact:
44
44
  * every package it reaches through `workspace:` ranges in `dependencies`, read
45
- * from HEAD. A change to any of them is a change to the bundle.
45
+ * from `commit`. A change to any of them is a change to the bundle.
46
46
  */
47
- export function bundledPackages(rootDir: string): Map<string, string[]> {
47
+ export function bundledPackages(rootDir: string, commit = "HEAD"): Map<string, string[]> {
48
48
  const bundles = loadConfig(rootDir).versions.bundles;
49
49
  const result = new Map<string, string[]>();
50
50
  if (bundles.length === 0) return result;
51
- const versioned = workspacePackages(rootDir);
51
+ const versioned = workspacePackages(rootDir, commit);
52
52
  // Discovered only when a bundle has a workspace dependency, so a bundle
53
53
  // without any needs no workspace declaration.
54
54
  let directories: Map<string, string> | undefined;
55
55
  const directoryOf = (name: string): string | undefined => {
56
56
  if (directories === undefined) {
57
57
  directories = new Map();
58
- for (const packageDir of discoveredWorkspaces(rootDir)) {
59
- const manifest = headManifest(rootDir, packageDir);
58
+ for (const packageDir of discoveredWorkspaces(rootDir, commit)) {
59
+ const manifest = headManifest(rootDir, packageDir, commit);
60
60
  if (typeof manifest.name === "string") directories.set(manifest.name, packageDir);
61
61
  }
62
62
  }
@@ -67,7 +67,7 @@ export function bundledPackages(rootDir: string): Map<string, string[]> {
67
67
  throw new Error(`versions.bundles names ${bundle}, which is not a versioned package.`);
68
68
  }
69
69
  const reached = new Set<string>();
70
- const pending = workspaceDependencies(headManifest(rootDir, bundle));
70
+ const pending = workspaceDependencies(headManifest(rootDir, bundle, commit));
71
71
  for (let name = pending.pop(); name !== undefined; name = pending.pop()) {
72
72
  const packageDir = directoryOf(name);
73
73
  if (packageDir === undefined) {
@@ -75,7 +75,7 @@ export function bundledPackages(rootDir: string): Map<string, string[]> {
75
75
  }
76
76
  if (packageDir === bundle || reached.has(packageDir)) continue;
77
77
  reached.add(packageDir);
78
- pending.push(...workspaceDependencies(headManifest(rootDir, packageDir)));
78
+ pending.push(...workspaceDependencies(headManifest(rootDir, packageDir, commit)));
79
79
  }
80
80
  result.set(bundle, [...reached].sort());
81
81
  }
@@ -6,14 +6,14 @@ import {
6
6
  import { posix } from "node:path";
7
7
  import { loadConfig } from "../config";
8
8
 
9
- /** The versioned packages: the configured directories, or every workspace. */
10
- export function workspacePackages(rootDir: string): string[] {
9
+ /** The versioned packages: the configured directories, or every workspace committed at `commit`. */
10
+ export function workspacePackages(rootDir: string, commit = "HEAD"): string[] {
11
11
  const configured = loadConfig(rootDir).versions.packages;
12
- return configured === null ? discoveredWorkspaces(rootDir) : [...configured];
12
+ return configured === null ? discoveredWorkspaces(rootDir, commit) : [...configured];
13
13
  }
14
14
 
15
- /** Discover every committed workspace, including globbed and newly added ones. */
16
- export function discoveredWorkspaces(rootDir: string): string[] {
15
+ /** Discover every workspace committed at `commit`, including globbed and newly added ones. */
16
+ export function discoveredWorkspaces(rootDir: string, commit = "HEAD"): string[] {
17
17
  const options: ExecFileSyncOptionsWithStringEncoding = {
18
18
  cwd: rootDir,
19
19
  env: gitEnvironment(),
@@ -21,7 +21,7 @@ export function discoveredWorkspaces(rootDir: string): string[] {
21
21
  stdio: ["ignore", "pipe", "pipe"],
22
22
  };
23
23
  const root: { workspaces?: string[] | { packages?: string[] } } = JSON.parse(
24
- execFileSync("git", ["show", "HEAD:package.json"], options),
24
+ execFileSync("git", ["show", `${commit}:package.json`], options),
25
25
  );
26
26
  const patterns = Array.isArray(root.workspaces)
27
27
  ? root.workspaces
@@ -30,7 +30,7 @@ export function discoveredWorkspaces(rootDir: string): string[] {
30
30
  throw new Error("package.json must declare workspace package paths.");
31
31
  }
32
32
  const globs = patterns.map((pattern) => new Bun.Glob(pattern));
33
- return execFileSync("git", ["ls-tree", "-rz", "--name-only", "HEAD"], options)
33
+ return execFileSync("git", ["ls-tree", "-rz", "--name-only", commit], options)
34
34
  .split("\0")
35
35
  .filter((file) => file.endsWith("/package.json"))
36
36
  .map((file) => posix.dirname(file))
@@ -26,16 +26,18 @@ function stage(rootDir: string, index: 1 | 2 | 3, file: string): string {
26
26
  return git(rootDir, ["show", `:${index}:${file}`]);
27
27
  }
28
28
 
29
+ function mergeIgnoringVersion(rootDir: string, file: string): string | null {
30
+ return mergeManifestIgnoringVersion(rootDir, stage(rootDir, 1, file), stage(rootDir, 2, file), stage(rootDir, 3, file));
31
+ }
32
+
29
33
  /**
30
34
  * Three-way merge a manifest with every side's version set to one version, so
31
35
  * a version-only conflict merges cleanly: the branch's when it is a deliberate
32
36
  * major or minor release over the incoming base, which `bumpVersions` keeps,
33
37
  * and the base's otherwise. Null when anything besides the version still
34
- * conflicts.
38
+ * conflicts. `review carry` repeats it to check a recorded resolution.
35
39
  */
36
- function mergeIgnoringVersion(rootDir: string, file: string): string | null {
37
- const ours = stage(rootDir, 2, file);
38
- const theirs = stage(rootDir, 3, file);
40
+ export function mergeManifestIgnoringVersion(rootDir: string, mergeBase: string, ours: string, theirs: string): string | null {
39
41
  const oursVersion = readVersion(ours);
40
42
  const theirsVersion = readVersion(theirs);
41
43
  const version = isReleaseBump(oursVersion, theirsVersion)
@@ -45,7 +47,7 @@ function mergeIgnoringVersion(rootDir: string, file: string): string | null {
45
47
  try {
46
48
  const inputs = {
47
49
  ours: withVersion(ours, version),
48
- base: withVersion(stage(rootDir, 1, file), version),
50
+ base: withVersion(mergeBase, version),
49
51
  theirs: withVersion(theirs, version),
50
52
  };
51
53
  const paths = Object.entries(inputs).map(([name, source]) => {
@@ -259,7 +259,9 @@ export interface PushGateCheck {
259
259
  notes: string[];
260
260
  }
261
261
 
262
- /** Read-only: the tracked hook is the canonical body, hooks.files installs it, and no step runs tests or installs unless it says why. */
262
+ const FINDING_TEXT: Record<StepFinding["kind"], string> = { test: "a test run", build: "a build", install: "a dependency install" };
263
+
264
+ /** Read-only: the tracked hook is the canonical body, hooks.files installs it, and no step runs tests, builds, or installs unless it says why. */
263
265
  export function checkPushGate(root: string, config: AgentToolConfig): PushGateCheck {
264
266
  const errors: string[] = [], notes: string[] = [];
265
267
  const tracked = spawnSync("git", ["ls-files", "--error-unmatch", "--", PRE_PUSH_SOURCE], { cwd: root, stdio: "ignore", env: gitEnvironment() }).status === 0;
@@ -278,7 +280,7 @@ export function checkPushGate(root: string, config: AgentToolConfig): PushGateCh
278
280
  const steps = (gate?.steps ?? []).map(step => {
279
281
  const { findings, errors: stepErrors } = resolveStep(root, step.command);
280
282
  const ok = !stepErrors.length && (!findings.length || !!step.allowSlow);
281
- for (const finding of step.allowSlow ? [] : findings) errors.push(`Step '${step.label}' runs ${finding.kind === "test" ? "a test run" : "a dependency install"} (${finding.runner}): ${finding.chain.join(" -> ")}. Move it to CI, or give the step an allowSlow reason.`);
283
+ for (const finding of step.allowSlow ? [] : findings) errors.push(`Step '${step.label}' runs ${FINDING_TEXT[finding.kind]} (${finding.runner}): ${finding.chain.join(" -> ")}. Move it to CI, or give the step an allowSlow reason.`);
282
284
  for (const error of stepErrors) errors.push(`Step '${step.label}': ${error}`);
283
285
  return { label: step.label, command: step.command, allowSlow: step.allowSlow ?? null, ok, findings, errors: stepErrors };
284
286
  });