@cmgmyr/hive 1.11.9 → 1.12.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.
@@ -0,0 +1,124 @@
1
+ import { spawn } from "node:child_process";
2
+ import { isTrusted } from "./leadLaunch.js";
3
+ import { loadProjectYml } from "./projectYml.js";
4
+ const GATE_NAME = "hive:todo-completion-gate";
5
+ const TIMEOUT_MS = 30_000;
6
+ const OUTPUT_TAIL_BYTES = 4096;
7
+ const GATE_APPROVAL_MESSAGE = "completion gate in hive.yml is not approved yet; run `hive` in a terminal in this project to review it.";
8
+ export function completionGateTrust(argv) {
9
+ return {
10
+ name: GATE_NAME,
11
+ command: JSON.stringify(argv),
12
+ dir: null,
13
+ env: {},
14
+ };
15
+ }
16
+ export function todoCompletionGateTrusted(projectId, argv) {
17
+ const trust = completionGateTrust(argv);
18
+ return isTrusted(projectId, trust.name, trust.command, trust.dir, trust.env);
19
+ }
20
+ export function assertValidTodoCompletionGate(input, message) {
21
+ if (message)
22
+ throw failure("invalid", input, null, null, Buffer.alloc(0), Buffer.alloc(0), message);
23
+ }
24
+ export function assertTodoCompletionGateCurrent(input, originalArgv) {
25
+ const current = loadProjectYml(input.projectPath);
26
+ if (current.completionGateError || !current.config?.completion_gate) {
27
+ throw failure("changed", input, null, null, Buffer.alloc(0), Buffer.alloc(0));
28
+ }
29
+ if (JSON.stringify(current.config.completion_gate) !== JSON.stringify(originalArgv)) {
30
+ throw failure("changed", input, null, null, Buffer.alloc(0), Buffer.alloc(0), GATE_APPROVAL_MESSAGE);
31
+ }
32
+ if (!todoCompletionGateTrusted(input.projectId, originalArgv)) {
33
+ throw failure("untrusted", input, null, null, Buffer.alloc(0), Buffer.alloc(0));
34
+ }
35
+ }
36
+ function failure(reason, input, code, signal, stdout, stderr, detail) {
37
+ const output = [
38
+ stdout.length ? `stdout tail:\n${stdout.toString("utf8")}` : "",
39
+ stderr.length ? `stderr tail:\n${stderr.toString("utf8")}` : "",
40
+ ].filter(Boolean).join("\n");
41
+ return new Error(`[todo_completion_gate:${reason}] todo ${input.todoId}; exit ${code ?? "unavailable"}` +
42
+ `${signal ? `; signal ${signal}` : ""}${output ? `\n${output}` : ""}` +
43
+ (detail ? `\n${detail}` : "") +
44
+ (reason === "untrusted"
45
+ ? `\n${GATE_APPROVAL_MESSAGE}`
46
+ : ""));
47
+ }
48
+ export async function runTodoCompletionGate(input) {
49
+ if (!todoCompletionGateTrusted(input.projectId, input.argv)) {
50
+ throw failure("untrusted", input, null, null, Buffer.alloc(0), Buffer.alloc(0));
51
+ }
52
+ const [file, ...args] = input.argv;
53
+ await new Promise((resolve, reject) => {
54
+ const stdout = [];
55
+ const stderr = [];
56
+ let stdoutBytes = 0;
57
+ let stderrBytes = 0;
58
+ let settled = false;
59
+ let timedOut = false;
60
+ const child = spawn(file, args, {
61
+ cwd: input.projectPath,
62
+ env: {
63
+ ...process.env,
64
+ HIVE_TODO_ID: String(input.todoId),
65
+ HIVE_TODO_TITLE: input.title,
66
+ HIVE_PROJECT_ID: String(input.projectId),
67
+ HIVE_ACTOR_ID: input.actorId,
68
+ },
69
+ shell: false,
70
+ detached: process.platform !== "win32",
71
+ stdio: ["ignore", "pipe", "pipe"],
72
+ });
73
+ const appendTail = (chunks, chunk, current) => {
74
+ chunks.push(chunk);
75
+ let total = current + chunk.length;
76
+ while (total > OUTPUT_TAIL_BYTES && chunks.length) {
77
+ const excess = total - OUTPUT_TAIL_BYTES;
78
+ if (chunks[0].length <= excess) {
79
+ total -= chunks.shift().length;
80
+ }
81
+ else {
82
+ chunks[0] = chunks[0].subarray(excess);
83
+ total -= excess;
84
+ }
85
+ }
86
+ return total;
87
+ };
88
+ child.stdout.on("data", (chunk) => { stdoutBytes = appendTail(stdout, chunk, stdoutBytes); });
89
+ child.stderr.on("data", (chunk) => { stderrBytes = appendTail(stderr, chunk, stderrBytes); });
90
+ const finish = (error) => {
91
+ if (settled)
92
+ return;
93
+ settled = true;
94
+ clearTimeout(timer);
95
+ if (error)
96
+ reject(error);
97
+ else
98
+ resolve();
99
+ };
100
+ const timer = setTimeout(() => {
101
+ timedOut = true;
102
+ if (child.pid && process.platform !== "win32") {
103
+ try {
104
+ process.kill(-child.pid, "SIGKILL");
105
+ }
106
+ catch { }
107
+ }
108
+ else
109
+ child.kill("SIGKILL");
110
+ child.stdout.destroy();
111
+ child.stderr.destroy();
112
+ finish(failure("timeout", input, null, null, Buffer.concat(stdout), Buffer.concat(stderr), `completion gate timed out after ${TIMEOUT_MS}ms`));
113
+ }, TIMEOUT_MS);
114
+ child.once("error", () => finish(failure("spawn", input, null, null, Buffer.concat(stdout), Buffer.concat(stderr))));
115
+ child.once("close", (code, signal) => {
116
+ if (timedOut)
117
+ return;
118
+ if (code === 0)
119
+ finish();
120
+ else
121
+ finish(failure(signal ? "signal" : "failed", input, code, signal, Buffer.concat(stdout), Buffer.concat(stderr)));
122
+ });
123
+ });
124
+ }