@a2f0/agent-tool 0.1.60 → 0.1.62
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 +1 -1
- package/src/commands/run.ts +21 -7
- package/src/workflows/branch.ts +2 -2
- package/src/workflows/core.ts +25 -1
- package/src/workflows/hooks.ts +2 -2
- package/src/workflows/repository.ts +5 -5
package/package.json
CHANGED
package/src/commands/run.ts
CHANGED
|
@@ -8,6 +8,22 @@ export const FORWARDED_SIGNALS = ["SIGINT", "SIGTERM", "SIGHUP"] as const;
|
|
|
8
8
|
|
|
9
9
|
export interface RunReceipt { status: string; label?: string; exitCode: number; signal: string | null; durationMs: number; outputBytes: number; log: string; leftoverProcessGroup?: number }
|
|
10
10
|
|
|
11
|
+
/**
|
|
12
|
+
* Whether a process group still has members. macOS answers EPERM, not ESRCH,
|
|
13
|
+
* for a group whose only members are zombies awaiting their reaper, such as a
|
|
14
|
+
* descendant that exited after its parent and before launchd reaped it, and
|
|
15
|
+
* for a group holding only processes this user cannot signal. Both still exist.
|
|
16
|
+
*/
|
|
17
|
+
export function processGroupExists(group: number): boolean {
|
|
18
|
+
try { process.kill(-group, 0); return true; }
|
|
19
|
+
catch (error) {
|
|
20
|
+
const code = (error as NodeJS.ErrnoException).code;
|
|
21
|
+
if (code === "ESRCH") return false;
|
|
22
|
+
if (code === "EPERM") return true;
|
|
23
|
+
throw error;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
11
27
|
/**
|
|
12
28
|
* Run directly into a private file: child output never accumulates in memory.
|
|
13
29
|
* A caller that must record the interruption before exiting passes
|
|
@@ -46,7 +62,10 @@ export async function runCommand(args: string[], record?: (receipt: RunReceipt)
|
|
|
46
62
|
if (grouped) process.kill(-child.pid, value);
|
|
47
63
|
else child.kill(value);
|
|
48
64
|
} catch (error) {
|
|
49
|
-
|
|
65
|
+
// ESRCH: the group is gone. EPERM: no member could be signalled, as in
|
|
66
|
+
// a group of zombies on macOS; there is nothing left to stop.
|
|
67
|
+
const code = (error as NodeJS.ErrnoException).code;
|
|
68
|
+
if (code !== "ESRCH" && code !== "EPERM") throw error;
|
|
50
69
|
}
|
|
51
70
|
};
|
|
52
71
|
for (const value of FORWARDED_SIGNALS) {
|
|
@@ -72,12 +91,7 @@ export async function runCommand(args: string[], record?: (receipt: RunReceipt)
|
|
|
72
91
|
if (interrupted) {
|
|
73
92
|
// The direct child can exit before its descendants finish cleanup. Keep
|
|
74
93
|
// the original grace period for the remaining process group as well.
|
|
75
|
-
while (grouped && child.pid && performance.now() < cancelDeadline) {
|
|
76
|
-
try { process.kill(-child.pid, 0); }
|
|
77
|
-
catch (error) {
|
|
78
|
-
if ((error as NodeJS.ErrnoException).code === "ESRCH") break;
|
|
79
|
-
throw error;
|
|
80
|
-
}
|
|
94
|
+
while (grouped && child.pid && performance.now() < cancelDeadline && processGroupExists(child.pid)) {
|
|
81
95
|
await new Promise(resolve => setTimeout(resolve, 25));
|
|
82
96
|
}
|
|
83
97
|
forward("SIGKILL");
|
package/src/workflows/branch.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { readFileSync } from "node:fs";
|
|
|
3
3
|
import { loadConfig, type AgentToolConfig } from "../config";
|
|
4
4
|
import { remoteBranchHead } from "../git/prContext";
|
|
5
5
|
import { gitEnvironment } from "../git/environment";
|
|
6
|
-
import { assert, clean, currentBranch, flag, git, head, mutateGit, nativeBranch, oid, option, output, repository } from "./core";
|
|
6
|
+
import { assert, clean, currentBranch, flag, git, head, mutateGit, mutateGitCaptured, nativeBranch, oid, option, output, repository } from "./core";
|
|
7
7
|
|
|
8
8
|
export function branchErrors(name: string, config: AgentToolConfig): string[] {
|
|
9
9
|
const errors: string[] = [];
|
|
@@ -75,7 +75,7 @@ export function branchCommand(args: string[]): number {
|
|
|
75
75
|
assert(baseOid, `Remote base '${base}' does not exist.`);
|
|
76
76
|
git(["fetch", "--no-tags", remote, `refs/heads/${base}`]);
|
|
77
77
|
assert(git(["rev-parse", "FETCH_HEAD"]) === baseOid && remoteBranchHead(url, base) === baseOid, "Base moved while fetching; retry creation.");
|
|
78
|
-
|
|
78
|
+
mutateGitCaptured(["switch", "--no-track", "-c", name, oid(baseOid)]);
|
|
79
79
|
assert(currentBranch() === name && head() === baseOid, "Checkout changed during branch creation.");
|
|
80
80
|
output({ branch: name, headOid: baseOid, baseBranch: base, baseOid, remote }); return 0;
|
|
81
81
|
}
|
package/src/workflows/core.ts
CHANGED
|
@@ -12,12 +12,36 @@ export function read(command: string, args: string[], cwd = process.cwd()): stri
|
|
|
12
12
|
}
|
|
13
13
|
export function git(args: string[], cwd = process.cwd()): string { return read("git", args, cwd); }
|
|
14
14
|
export function json<T>(args: string[]): T { return JSON.parse(read("gh", args)) as T; }
|
|
15
|
-
/**
|
|
15
|
+
/**
|
|
16
|
+
* Mutating Git commands deliberately retain normal hooks and their environment.
|
|
17
|
+
* This one streams Git's and its hooks' output to stderr for commands people
|
|
18
|
+
* watch live, such as a push and its push gate.
|
|
19
|
+
*/
|
|
16
20
|
export function mutateGit(args: string[]): void {
|
|
17
21
|
const result = spawnSync("git", args, { stdio: ["inherit", 2, 2], env: process.env });
|
|
18
22
|
if (result.error) throw result.error;
|
|
19
23
|
assert(result.status === 0, `git ${args[0]} failed (${result.signal ?? result.status}). Inspect checkout state before retrying.`);
|
|
20
24
|
}
|
|
25
|
+
/**
|
|
26
|
+
* A local Git mutation whose output nobody reads live, with the same hooks and
|
|
27
|
+
* environment as `mutateGit`. Its output and its hooks' are captured rather than
|
|
28
|
+
* inherited: a caller whose stderr pipe lost its reader (`2>&1 | jq` once jq
|
|
29
|
+
* quits) would otherwise kill Git with SIGPIPE part-way through a multi-step
|
|
30
|
+
* mutation. Stdin is closed, since a prompt could not be seen. On failure the
|
|
31
|
+
* error carries Git's exit status or signal and the tail of its stderr.
|
|
32
|
+
*/
|
|
33
|
+
export function mutateGitCaptured(args: string[]): void {
|
|
34
|
+
const result = spawnSync("git", args, { stdio: ["ignore", "pipe", "pipe"], env: process.env, encoding: "utf8", maxBuffer: 64 * 1024 * 1024 });
|
|
35
|
+
if (result.error) throw result.error;
|
|
36
|
+
if (result.status === 0) return;
|
|
37
|
+
const detail = outputTail(result.stderr || result.stdout || "");
|
|
38
|
+
throw new Error(`git ${args[0]} failed (${result.signal ?? result.status})${detail ? `: ${detail}` : "."} Inspect checkout state before retrying.`);
|
|
39
|
+
}
|
|
40
|
+
/** The last lines of a command's output, bounded for an error message. */
|
|
41
|
+
export function outputTail(text: string, lines = 20, characters = 2000): string {
|
|
42
|
+
const tail = text.trim().split("\n").slice(-lines).join("\n");
|
|
43
|
+
return tail.length > characters ? `…${tail.slice(-characters)}` : tail;
|
|
44
|
+
}
|
|
21
45
|
export function clean(): void { assert(!git(["status", "--porcelain=v1", "--untracked-files=all"]), "Checkout has local changes; preserve them before continuing."); }
|
|
22
46
|
export function head(): string { return oid(git(["rev-parse", "HEAD"])); }
|
|
23
47
|
export function currentBranch(): string {
|
package/src/workflows/hooks.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { createHash, randomUUID } from "node:crypto";
|
|
|
3
3
|
import { chmodSync, copyFileSync, existsSync, lstatSync, mkdirSync, readFileSync, readlinkSync, realpathSync, renameSync, symlinkSync, unlinkSync, writeFileSync } from "node:fs";
|
|
4
4
|
import path from "node:path";
|
|
5
5
|
import { loadConfig } from "../config";
|
|
6
|
-
import { assert, flag, git, output } from "./core";
|
|
6
|
+
import { assert, flag, git, mutateGitCaptured, output } from "./core";
|
|
7
7
|
|
|
8
8
|
const manifestName = ".agent-tool-hooks.json";
|
|
9
9
|
interface Manifest { schemaVersion: 1; files: Record<string, { hash: string; executable: boolean }> }
|
|
@@ -126,7 +126,7 @@ function install(root: string, common: string, destination: string, sources: Hoo
|
|
|
126
126
|
const temporary = path.join(destination, `.agent-tool-manifest-${randomUUID()}`);
|
|
127
127
|
writeFileSync(temporary, `${JSON.stringify(next, null, 2)}\n`, { mode: 0o600, flag: "wx" });
|
|
128
128
|
renameSync(temporary, manifestFile);
|
|
129
|
-
if (configure)
|
|
129
|
+
if (configure) mutateGitCaptured(["config", "--local", "core.hooksPath", destination]);
|
|
130
130
|
const after = config(["--type=path", "--get", "core.hooksPath"]);
|
|
131
131
|
assert(resolveDirectory(after ? path.resolve(root, after) : path.join(common, "hooks")) === destination, "Effective hooksPath does not match installation; inspect Git configuration.");
|
|
132
132
|
output({ ok: true, applied: true, destination, changes, backups });
|
|
@@ -4,7 +4,7 @@ import path from "node:path";
|
|
|
4
4
|
import { loadConfig } from "../config";
|
|
5
5
|
import { remoteBranchHead, resolveFreshBaseRef, resolveRepositoryGitUrl } from "../git/prContext";
|
|
6
6
|
import { viewCurrentBranchPr } from "../git/currentBranchPr";
|
|
7
|
-
import { assert, clean, currentBranch, flag, git, head,
|
|
7
|
+
import { assert, clean, currentBranch, flag, git, head, mutateGitCaptured, oid, output, read, repository } from "./core";
|
|
8
8
|
import { pullRequest } from "./pr";
|
|
9
9
|
import { assertStrictBaseFreshness } from "../pr/baseFreshness";
|
|
10
10
|
import { baseHealthCommand } from "./baseHealth";
|
|
@@ -55,15 +55,15 @@ export function repoCommand(args: string[]): number {
|
|
|
55
55
|
const localDefault = spawnSync("git", ["rev-parse", "--verify", `refs/heads/${defaultBranch}`], { encoding: "utf8" });
|
|
56
56
|
if (localDefault.status === 0) {
|
|
57
57
|
assert(git(["merge-base", localDefault.stdout.trim(), defaultOid]) === localDefault.stdout.trim(), "Local default branch diverged; preserve it for inspection.");
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
} else { assert(localDefault.status === 128, "Could not inspect local default branch.");
|
|
58
|
+
mutateGitCaptured(["switch", defaultBranch]);
|
|
59
|
+
mutateGitCaptured(["merge", "--ff-only", defaultOid]);
|
|
60
|
+
} else { assert(localDefault.status === 128, "Could not inspect local default branch."); mutateGitCaptured(["switch", "--no-track", "-c", defaultBranch, defaultOid]); }
|
|
61
61
|
assert(currentBranch() === defaultBranch && head() === defaultOid, "Checkout changed during default-branch update.");
|
|
62
62
|
clean(); completed.push("updated-default");
|
|
63
63
|
if (!keep) {
|
|
64
64
|
if (feature.status === 0) {
|
|
65
65
|
assert(git(["rev-parse", `refs/heads/${pr.headRefName}`]) === shipped, "Local feature branch changed before deletion.");
|
|
66
|
-
|
|
66
|
+
mutateGitCaptured(["branch", "-D", "--", pr.headRefName]); completed.push("deleted-local-branch");
|
|
67
67
|
}
|
|
68
68
|
const live = remoteBranchHead(url, pr.headRefName);
|
|
69
69
|
assert(!live || live === shipped, "Remote feature branch changed before deletion.");
|