co-maintainer 0.6.13 → 0.7.0-beta.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.
Files changed (156) hide show
  1. package/README.md +1 -1
  2. package/dist/package.json +1 -1
  3. package/dist/src/ai/anthropic.js +2 -0
  4. package/dist/src/ai/batch.d.ts +3 -1
  5. package/dist/src/ai/batch.js +23 -2
  6. package/dist/src/ai/mermaid_loop.js +1 -1
  7. package/dist/src/ai/oauth/sign-in-with-chatgpt.js +8 -10
  8. package/dist/src/ai/openai.js +2 -0
  9. package/dist/src/ai/openrouter.js +2 -0
  10. package/dist/src/cli/args.js +47 -0
  11. package/dist/src/cli/command_json.d.ts +20 -1
  12. package/dist/src/cli/command_json.js +4 -0
  13. package/dist/src/cli/commands/config.d.ts +1 -1
  14. package/dist/src/cli/commands/config.js +1 -0
  15. package/dist/src/cli/commands/registry.js +226 -1
  16. package/dist/src/cli/commands/review.js +50 -9
  17. package/dist/src/cli/commands/serve.js +5 -0
  18. package/dist/src/cli/commands/set.js +70 -2
  19. package/dist/src/cli/commands/view.js +46 -5
  20. package/dist/src/cli/commands/view_sources.d.ts +11 -0
  21. package/dist/src/cli/commands/view_sources.js +40 -0
  22. package/dist/src/cli/main.js +5 -0
  23. package/dist/src/cli/pr_carry.d.ts +25 -0
  24. package/dist/src/cli/pr_carry.js +128 -0
  25. package/dist/src/cli/prompt.js +26 -1
  26. package/dist/src/cli/review_args.d.ts +8 -0
  27. package/dist/src/cli/review_args.js +18 -0
  28. package/dist/src/cli/review_carry.d.ts +32 -0
  29. package/dist/src/cli/review_carry.js +60 -0
  30. package/dist/src/cli/review_github.d.ts +16 -2
  31. package/dist/src/cli/review_github.js +49 -12
  32. package/dist/src/cli/review_intent.d.ts +25 -0
  33. package/dist/src/cli/review_intent.js +87 -0
  34. package/dist/src/cli/review_result.d.ts +53 -2
  35. package/dist/src/cli/review_result.js +90 -23
  36. package/dist/src/config.d.ts +27 -0
  37. package/dist/src/config.js +20 -12
  38. package/dist/src/extensions/command.d.ts +1 -0
  39. package/dist/src/extensions/command.js +355 -0
  40. package/dist/src/extensions/contexts.d.ts +65 -0
  41. package/dist/src/extensions/contexts.js +134 -0
  42. package/dist/src/extensions/cost.d.ts +8 -0
  43. package/dist/src/extensions/cost.js +6 -0
  44. package/dist/src/extensions/local_tracking.d.ts +7 -0
  45. package/dist/src/extensions/local_tracking.js +29 -0
  46. package/dist/src/extensions/mock.d.ts +29 -0
  47. package/dist/src/extensions/mock.js +67 -0
  48. package/dist/src/extensions/process.d.ts +43 -0
  49. package/dist/src/extensions/process.js +244 -0
  50. package/dist/src/extensions/registry.d.ts +79 -0
  51. package/dist/src/extensions/registry.js +240 -0
  52. package/dist/src/extensions/render_actions.d.ts +6 -0
  53. package/dist/src/extensions/render_actions.js +50 -0
  54. package/dist/src/extensions/render_cli.d.ts +18 -0
  55. package/dist/src/extensions/render_cli.js +88 -0
  56. package/dist/src/extensions/render_json.d.ts +51 -0
  57. package/dist/src/extensions/render_json.js +144 -0
  58. package/dist/src/extensions/render_markdown.d.ts +22 -0
  59. package/dist/src/extensions/render_markdown.js +96 -0
  60. package/dist/src/extensions/render_review.d.ts +44 -0
  61. package/dist/src/extensions/render_review.js +167 -0
  62. package/dist/src/extensions/report.d.ts +57 -0
  63. package/dist/src/extensions/report.js +97 -0
  64. package/dist/src/extensions/review_result.d.ts +2 -0
  65. package/dist/src/extensions/review_result.js +14 -0
  66. package/dist/src/extensions/run_folder.d.ts +74 -0
  67. package/dist/src/extensions/run_folder.js +172 -0
  68. package/dist/src/extensions/runner.d.ts +66 -0
  69. package/dist/src/extensions/runner.js +251 -0
  70. package/dist/src/extensions/schema.d.ts +103 -0
  71. package/dist/src/extensions/schema.js +539 -0
  72. package/dist/src/extensions/session.d.ts +56 -0
  73. package/dist/src/extensions/session.js +173 -0
  74. package/dist/src/github/collect.d.ts +7 -2
  75. package/dist/src/github/collect.js +66 -6
  76. package/dist/src/github/gh.d.ts +7 -0
  77. package/dist/src/github/gh.js +24 -29
  78. package/dist/src/knowledge/guide.js +3 -2
  79. package/dist/src/knowledge/instruction_files.d.ts +45 -0
  80. package/dist/src/knowledge/instruction_files.js +91 -0
  81. package/dist/src/knowledge/instructions.d.ts +78 -0
  82. package/dist/src/knowledge/instructions.js +280 -0
  83. package/dist/src/knowledge/rule_changes.d.ts +39 -0
  84. package/dist/src/knowledge/rule_changes.js +90 -0
  85. package/dist/src/knowledge/rules.d.ts +44 -0
  86. package/dist/src/knowledge/rules.js +82 -0
  87. package/dist/src/knowledge/synthesis.d.ts +1 -0
  88. package/dist/src/knowledge/synthesis.js +12 -5
  89. package/dist/src/knowledge/types.d.ts +4 -0
  90. package/dist/src/local/carry_over_store.d.ts +10 -0
  91. package/dist/src/local/carry_over_store.js +17 -0
  92. package/dist/src/local/review_local.js +100 -62
  93. package/dist/src/local/review_lock.js +15 -4
  94. package/dist/src/pr/diff_plan.d.ts +110 -0
  95. package/dist/src/pr/diff_plan.js +429 -0
  96. package/dist/src/pr/diff_summary.d.ts +17 -1
  97. package/dist/src/pr/diff_summary.js +87 -13
  98. package/dist/src/pr/review_diff.d.ts +47 -0
  99. package/dist/src/pr/review_diff.js +112 -0
  100. package/dist/src/pr/reviewer.d.ts +44 -19
  101. package/dist/src/pr/reviewer.js +114 -142
  102. package/dist/src/remote/client.js +67 -8
  103. package/dist/src/remote/http.d.ts +12 -0
  104. package/dist/src/remote/http.js +31 -0
  105. package/dist/src/remote/push_client.js +14 -3
  106. package/dist/src/remote/server/guides.js +12 -1
  107. package/dist/src/remote/server/routes.js +4 -2
  108. package/dist/src/remote/server/sessions.js +4 -1
  109. package/dist/src/remote/validate.js +11 -0
  110. package/dist/src/review/carry_over.d.ts +6 -1
  111. package/dist/src/review/carry_over.js +10 -2
  112. package/dist/src/review/coverage.d.ts +57 -0
  113. package/dist/src/review/coverage.js +186 -0
  114. package/dist/src/review/diagram.d.ts +73 -0
  115. package/dist/src/review/diagram.js +309 -0
  116. package/dist/src/review/engine.d.ts +2 -6
  117. package/dist/src/review/engine.js +1 -1
  118. package/dist/src/review/guides.d.ts +6 -0
  119. package/dist/src/review/guides.js +8 -1
  120. package/dist/src/review/intent.d.ts +24 -0
  121. package/dist/src/review/intent.js +28 -0
  122. package/dist/src/review/not_initialized.d.ts +13 -0
  123. package/dist/src/review/not_initialized.js +18 -0
  124. package/dist/src/review/system_prompt.d.ts +6 -0
  125. package/dist/src/review/system_prompt.js +13 -0
  126. package/dist/src/review/visible_paths.d.ts +22 -0
  127. package/dist/src/review/visible_paths.js +41 -0
  128. package/dist/src/server/pages/extensions.d.ts +5 -0
  129. package/dist/src/server/pages/extensions.js +72 -0
  130. package/dist/src/server/pages/pr.js +3 -2
  131. package/dist/src/services/dashboard.d.ts +8 -0
  132. package/dist/src/services/dashboard.js +8 -0
  133. package/dist/src/services/push_review.js +14 -5
  134. package/dist/src/services/remote_review.js +81 -26
  135. package/dist/src/services/replies.js +15 -8
  136. package/dist/src/services/review.d.ts +9 -3
  137. package/dist/src/services/review.js +109 -28
  138. package/dist/src/services/rules_build.d.ts +46 -0
  139. package/dist/src/services/rules_build.js +155 -0
  140. package/dist/src/services/setup.d.ts +31 -0
  141. package/dist/src/services/setup.js +120 -25
  142. package/dist/src/services/watch_comments.js +2 -0
  143. package/dist/src/store/cache_db.js +3 -0
  144. package/dist/src/store/extensions.d.ts +51 -0
  145. package/dist/src/store/extensions.js +132 -0
  146. package/dist/src/store/migrations.js +40 -0
  147. package/dist/src/types.d.ts +22 -1
  148. package/dist/src/util/atomic.d.ts +15 -1
  149. package/dist/src/util/atomic.js +59 -29
  150. package/dist/src/util/interrupt.d.ts +6 -0
  151. package/dist/src/util/interrupt.js +27 -0
  152. package/dist/src/util/runtime.d.ts +3 -1
  153. package/dist/src/util/runtime.js +2 -2
  154. package/dist/src/util/test_location_guard.d.ts +6 -0
  155. package/dist/src/util/test_location_guard.js +47 -0
  156. package/package.json +1 -1
@@ -0,0 +1,29 @@
1
+ /** The review `extension debug-run` runs an extension against: pull request 45
2
+ * of GroophyLifefor/heap-analysis, with its context, diff and a finished AI
3
+ * review. All of it is written here, so a debug run needs no AI, no network
4
+ * and no GitHub access. */
5
+ import type { ExtensionDetails } from "./run_folder.ts";
6
+ export declare const MOCK_DETAILS: Omit<ExtensionDetails, "reviewResultPath" | "diffPath">;
7
+ export declare const MOCK_DIFF = "diff --git a/src/snapshot/toolbar.tsx b/src/snapshot/toolbar.tsx\n--- a/src/snapshot/toolbar.tsx\n+++ b/src/snapshot/toolbar.tsx\n@@ -12,9 +12,14 @@ export function Toolbar({ onSnapshot, busy }: ToolbarProps) {\n return (\n <div className=\"toolbar\">\n- <button onClick={onSnapshot} disabled={busy}>\n- Take snapshot\n- </button>\n+ {busy ? (\n+ <span className=\"toolbar-status\">Taking snapshot...</span>\n+ ) : (\n+ <button onClick={onSnapshot} aria-label=\"Take snapshot\">\n+ Take snapshot\n+ </button>\n+ )}\n <ExportMenu />\n </div>\n );\n }\ndiff --git a/src/heap/parse.ts b/src/heap/parse.ts\n--- a/src/heap/parse.ts\n+++ b/src/heap/parse.ts\n@@ -40,6 +40,10 @@ export function parseNodes(raw: Uint32Array, fields: number): HeapNode[] {\n const nodes: HeapNode[] = [];\n for (let offset = 0; offset < raw.length; offset += fields) {\n+ if (offset + fields > raw.length) {\n+ break;\n+ }\n nodes.push(readNode(raw, offset));\n }\n return nodes;\n }\n";
8
+ export declare const MOCK_REVIEW_RESULT: {
9
+ schemaVersion: number;
10
+ summary: {
11
+ new: number;
12
+ open: number;
13
+ closed: number;
14
+ blocking: number;
15
+ };
16
+ findings: {
17
+ id: string;
18
+ state: string;
19
+ closeReason: null;
20
+ severity: string;
21
+ blocking: boolean;
22
+ path: string;
23
+ lineFrom: number;
24
+ lineTo: number;
25
+ title: string;
26
+ body: string;
27
+ suggestion: null;
28
+ }[];
29
+ };
@@ -0,0 +1,67 @@
1
+ export const MOCK_DETAILS = {
2
+ schemaVersion: 1,
3
+ reviewType: "pr",
4
+ repo: "GroophyLifefor/heap-analysis",
5
+ prNumber: 45,
6
+ branch: null,
7
+ workdir: "",
8
+ headSha: "4f1c2d9a7b3e5f60718293a4b5c6d7e8f9012345",
9
+ baseSha: "9a8b7c6d5e4f30211203f4e5d6c7b8a990817263",
10
+ scope: "whole-pr",
11
+ sinceCommit: null,
12
+ trigger: "debug-run",
13
+ round: 1,
14
+ };
15
+ export const MOCK_DIFF = `diff --git a/src/snapshot/toolbar.tsx b/src/snapshot/toolbar.tsx
16
+ --- a/src/snapshot/toolbar.tsx
17
+ +++ b/src/snapshot/toolbar.tsx
18
+ @@ -12,9 +12,14 @@ export function Toolbar({ onSnapshot, busy }: ToolbarProps) {
19
+ return (
20
+ <div className="toolbar">
21
+ - <button onClick={onSnapshot} disabled={busy}>
22
+ - Take snapshot
23
+ - </button>
24
+ + {busy ? (
25
+ + <span className="toolbar-status">Taking snapshot...</span>
26
+ + ) : (
27
+ + <button onClick={onSnapshot} aria-label="Take snapshot">
28
+ + Take snapshot
29
+ + </button>
30
+ + )}
31
+ <ExportMenu />
32
+ </div>
33
+ );
34
+ }
35
+ diff --git a/src/heap/parse.ts b/src/heap/parse.ts
36
+ --- a/src/heap/parse.ts
37
+ +++ b/src/heap/parse.ts
38
+ @@ -40,6 +40,10 @@ export function parseNodes(raw: Uint32Array, fields: number): HeapNode[] {
39
+ const nodes: HeapNode[] = [];
40
+ for (let offset = 0; offset < raw.length; offset += fields) {
41
+ + if (offset + fields > raw.length) {
42
+ + break;
43
+ + }
44
+ nodes.push(readNode(raw, offset));
45
+ }
46
+ return nodes;
47
+ }
48
+ `;
49
+ export const MOCK_REVIEW_RESULT = {
50
+ schemaVersion: 1,
51
+ summary: { new: 1, open: 0, closed: 0, blocking: 0 },
52
+ findings: [
53
+ {
54
+ id: "8d6c1f4e-1a2b-4c3d-9e8f-0a1b2c3d4e5f",
55
+ state: "new",
56
+ closeReason: null,
57
+ severity: "P3",
58
+ blocking: false,
59
+ path: "src/heap/parse.ts",
60
+ lineFrom: 42,
61
+ lineTo: 44,
62
+ title: "[P3 · non-blocking] `src/heap/parse.ts`: `parseNodes`",
63
+ body: "A truncated snapshot now stops quietly. Logging how many trailing values were dropped would make a corrupt file easier to spot.",
64
+ suggestion: null,
65
+ },
66
+ ],
67
+ };
@@ -0,0 +1,43 @@
1
+ /** How long a process group gets between SIGTERM and SIGKILL. */
2
+ export declare const KILL_GRACE_MS = 30000;
3
+ export declare const SPLIT_MARKER = "\n[... output cut here, the middle is left out ...]\n";
4
+ export type ProcessOutcome = {
5
+ exitCode: number | null;
6
+ signal: string | null;
7
+ stdout: string;
8
+ /** stdout and stderr together, in arrival order, with the exit code.
9
+ * At most 128 KB: the first 64 KB, a marker, the last 64 KB. */
10
+ details: string;
11
+ timedOut: boolean;
12
+ canceled: boolean;
13
+ stdoutTooLarge: boolean;
14
+ spawnError?: string;
15
+ };
16
+ /** A path as one shell word. Only paths co-maintainer made are substituted,
17
+ * but a config directory can hold a space (`Application Support`). */
18
+ export declare function shellQuote(value: string): string;
19
+ export declare const TEMPLATE_VARIABLES: readonly ["details_path", "review_result_path", "diff_path"];
20
+ export type TemplateValues = Record<(typeof TEMPLATE_VARIABLES)[number], string>;
21
+ /** Variables in a command that co-maintainer does not know. */
22
+ export declare function unknownVariables(command: string): string[];
23
+ /** Puts the run folder's paths into the command. Each value is quoted here, so
24
+ * a template writes `{{details_path}}` bare, not inside its own quotes. */
25
+ export declare function substitute(command: string, values: TemplateValues): string;
26
+ /** How long the pipes may stay open once the shell has exited. Whatever the
27
+ * command left behind in its group is killed at that moment. A process that
28
+ * left the group (`setsid`, a daemon) cannot be reached, so its pipes are
29
+ * closed from this side after this window and the run ends with what it read. */
30
+ export declare const DRAIN_MS = 250;
31
+ /** The arguments the shell gets. On Windows, `cmd /s /c` strips the first
32
+ * and the last quote of the line, so the command is wrapped in one more pair,
33
+ * the way Node's own `shell: true` does. Without it, a command that starts
34
+ * with a quoted path such as `"C:\Program Files\nodejs\node.exe"` breaks. */
35
+ export declare function shellArgs(command: string, windows?: boolean): [string, string[]];
36
+ export declare function runCommandOnce(input: {
37
+ command: string;
38
+ cwd?: string;
39
+ timeoutMs: number;
40
+ signal?: AbortSignal;
41
+ killGraceMs?: number;
42
+ drainMs?: number;
43
+ }): Promise<ProcessOutcome>;
@@ -0,0 +1,244 @@
1
+ /** Runs one extension command once.
2
+ *
3
+ * The command is the user's own shell line, so it runs through a shell. It
4
+ * starts with an empty environment: co-maintainer's tokens and keys live in
5
+ * its environment and config, and none of them may reach a third-party
6
+ * command. The command sets what it needs itself, `HOME=... node ...`.
7
+ *
8
+ * It runs in its own process group so a timeout or a cancel reaches every
9
+ * process it started, not only the shell: SIGTERM to the group, then SIGKILL
10
+ * once the grace period is over. */
11
+ import { spawn, spawnSync } from "node:child_process";
12
+ import { getEnv, isWindows } from "../util/runtime.js";
13
+ /** How long a process group gets between SIGTERM and SIGKILL. */
14
+ export const KILL_GRACE_MS = 30_000;
15
+ /** Half of the 128 KB of output kept for a failure's details. */
16
+ const DETAIL_HALF_BYTES = 64 * 1024;
17
+ /** stdout is read whole as one JSON document. Past this it is not a response
18
+ * anyone meant to send. */
19
+ const MAX_STDOUT_BYTES = 16 * 1024 * 1024;
20
+ export const SPLIT_MARKER = "\n[... output cut here, the middle is left out ...]\n";
21
+ /** Keeps the first and the last 64 KB of a stream that may be any size. */
22
+ class HeadTail {
23
+ head = [];
24
+ headBytes = 0;
25
+ tail = [];
26
+ tailBytes = 0;
27
+ total = 0;
28
+ push(chunk) {
29
+ this.total += chunk.length;
30
+ let rest = chunk;
31
+ if (this.headBytes < DETAIL_HALF_BYTES) {
32
+ const room = DETAIL_HALF_BYTES - this.headBytes;
33
+ this.head.push(rest.subarray(0, room));
34
+ this.headBytes += Math.min(room, rest.length);
35
+ rest = rest.subarray(room);
36
+ }
37
+ if (rest.length === 0)
38
+ return;
39
+ this.tail.push(rest);
40
+ this.tailBytes += rest.length;
41
+ while (this.tailBytes - (this.tail[0]?.length ?? 0) >= DETAIL_HALF_BYTES) {
42
+ this.tailBytes -= this.tail.shift().length;
43
+ }
44
+ }
45
+ text() {
46
+ const head = Buffer.concat(this.head).toString("utf8");
47
+ let tail = Buffer.concat(this.tail);
48
+ if (this.total <= DETAIL_HALF_BYTES * 2) {
49
+ return head + tail.toString("utf8");
50
+ }
51
+ tail = tail.subarray(tail.length - DETAIL_HALF_BYTES);
52
+ return head + SPLIT_MARKER + tail.toString("utf8");
53
+ }
54
+ }
55
+ /** A path as one shell word. Only paths co-maintainer made are substituted,
56
+ * but a config directory can hold a space (`Application Support`). */
57
+ export function shellQuote(value) {
58
+ if (isWindows())
59
+ return `"${value.replace(/"/g, '""')}"`;
60
+ return `'${value.replace(/'/g, `'\\''`)}'`;
61
+ }
62
+ export const TEMPLATE_VARIABLES = [
63
+ "details_path",
64
+ "review_result_path",
65
+ "diff_path",
66
+ ];
67
+ /** Variables in a command that co-maintainer does not know. */
68
+ export function unknownVariables(command) {
69
+ const names = [...command.matchAll(/\{\{\s*([^}]*?)\s*\}\}/g)].map((match) => match[1]);
70
+ return names.filter((name) => !TEMPLATE_VARIABLES.includes(name));
71
+ }
72
+ /** Puts the run folder's paths into the command. Each value is quoted here, so
73
+ * a template writes `{{details_path}}` bare, not inside its own quotes. */
74
+ export function substitute(command, values) {
75
+ return command.replace(/\{\{\s*(details_path|review_result_path|diff_path)\s*\}\}/g, (_, name) => shellQuote(values[name]));
76
+ }
77
+ /** The environment the command starts with. Empty on POSIX. Windows cannot
78
+ * start a process at all without `SystemRoot`, so that one value is kept. */
79
+ function commandEnv() {
80
+ if (!isWindows())
81
+ return {};
82
+ const root = getEnv("SystemRoot");
83
+ return root ? { SystemRoot: root } : {};
84
+ }
85
+ function killGroup(pid, signal) {
86
+ if (isWindows()) {
87
+ // Windows has no process groups to signal. taskkill /T walks the tree.
88
+ spawnSync("taskkill", ["/pid", String(pid), "/T", "/F"], {
89
+ stdio: "ignore",
90
+ });
91
+ return;
92
+ }
93
+ try {
94
+ process.kill(-pid, signal);
95
+ }
96
+ catch {
97
+ // The group is already gone.
98
+ }
99
+ }
100
+ /** Process groups still running. An extension runs in its own group, so a
101
+ * Ctrl-C in the terminal, which signals the terminal's group, never reaches
102
+ * it. co-maintainer exiting ends them. A signal is left alone: `serve` shuts
103
+ * down gracefully on one and waits for its jobs, and an extension killed then
104
+ * would be reported as failed instead of running again after the restart. */
105
+ const liveGroups = new Set();
106
+ let exitHooked = false;
107
+ function hookExit() {
108
+ if (exitHooked)
109
+ return;
110
+ exitHooked = true;
111
+ process.on("exit", () => {
112
+ for (const pid of liveGroups)
113
+ killGroup(pid, "SIGKILL");
114
+ liveGroups.clear();
115
+ });
116
+ }
117
+ /** How long the pipes may stay open once the shell has exited. Whatever the
118
+ * command left behind in its group is killed at that moment. A process that
119
+ * left the group (`setsid`, a daemon) cannot be reached, so its pipes are
120
+ * closed from this side after this window and the run ends with what it read. */
121
+ export const DRAIN_MS = 250;
122
+ /** The arguments the shell gets. On Windows, `cmd /s /c` strips the first
123
+ * and the last quote of the line, so the command is wrapped in one more pair,
124
+ * the way Node's own `shell: true` does. Without it, a command that starts
125
+ * with a quoted path such as `"C:\Program Files\nodejs\node.exe"` breaks. */
126
+ export function shellArgs(command, windows = isWindows()) {
127
+ return windows
128
+ ? [getEnv("ComSpec") ?? "cmd.exe", ["/d", "/s", "/c", `"${command}"`]]
129
+ : ["/bin/sh", ["-c", command]];
130
+ }
131
+ export function runCommandOnce(input) {
132
+ const grace = input.killGraceMs ?? KILL_GRACE_MS;
133
+ const drain = input.drainMs ?? DRAIN_MS;
134
+ return new Promise((resolve) => {
135
+ const details = new HeadTail();
136
+ const stdout = [];
137
+ let stdoutBytes = 0;
138
+ let stdoutTooLarge = false;
139
+ let timedOut = false;
140
+ let canceled = false;
141
+ let settled = false;
142
+ const [shell, args] = shellArgs(input.command);
143
+ let child;
144
+ try {
145
+ child = spawn(shell, args, {
146
+ cwd: input.cwd || undefined,
147
+ env: commandEnv(),
148
+ detached: !isWindows(),
149
+ windowsVerbatimArguments: isWindows(),
150
+ windowsHide: true,
151
+ stdio: ["ignore", "pipe", "pipe"],
152
+ });
153
+ }
154
+ catch (error) {
155
+ resolve({
156
+ exitCode: null,
157
+ signal: null,
158
+ stdout: "",
159
+ details: "",
160
+ timedOut: false,
161
+ canceled: false,
162
+ stdoutTooLarge: false,
163
+ spawnError: error instanceof Error ? error.message : String(error),
164
+ });
165
+ return;
166
+ }
167
+ const pid = child.pid;
168
+ if (pid !== undefined) {
169
+ hookExit();
170
+ liveGroups.add(pid);
171
+ }
172
+ let killTimer;
173
+ let drainTimer;
174
+ const stop = () => {
175
+ if (pid === undefined || killTimer)
176
+ return;
177
+ killGroup(pid, "SIGTERM");
178
+ killTimer = setTimeout(() => killGroup(pid, "SIGKILL"), grace);
179
+ };
180
+ const timer = setTimeout(() => {
181
+ timedOut = true;
182
+ stop();
183
+ }, input.timeoutMs);
184
+ const onAbort = () => {
185
+ canceled = true;
186
+ stop();
187
+ };
188
+ if (input.signal?.aborted)
189
+ onAbort();
190
+ else
191
+ input.signal?.addEventListener("abort", onAbort, { once: true });
192
+ child.stdout?.on("data", (chunk) => {
193
+ details.push(chunk);
194
+ stdoutBytes += chunk.length;
195
+ if (stdoutBytes > MAX_STDOUT_BYTES) {
196
+ stdoutTooLarge = true;
197
+ return;
198
+ }
199
+ stdout.push(chunk);
200
+ });
201
+ child.stderr?.on("data", (chunk) => details.push(chunk));
202
+ const finish = (code, signal, spawnError) => {
203
+ if (settled)
204
+ return;
205
+ settled = true;
206
+ if (pid !== undefined)
207
+ liveGroups.delete(pid);
208
+ clearTimeout(timer);
209
+ if (killTimer)
210
+ clearTimeout(killTimer);
211
+ if (drainTimer)
212
+ clearTimeout(drainTimer);
213
+ input.signal?.removeEventListener("abort", onAbort);
214
+ child.stdout?.destroy();
215
+ child.stderr?.destroy();
216
+ const exit = spawnError
217
+ ? `could not start: ${spawnError}`
218
+ : signal
219
+ ? `exit: killed by ${signal}`
220
+ : `exit code: ${code}`;
221
+ const output = details.text();
222
+ resolve({
223
+ exitCode: code,
224
+ signal,
225
+ stdout: Buffer.concat(stdout).toString("utf8"),
226
+ details: output ? `${exit}\n${output}` : exit,
227
+ timedOut,
228
+ canceled,
229
+ stdoutTooLarge,
230
+ ...(spawnError ? { spawnError } : {}),
231
+ });
232
+ };
233
+ child.on("error", (error) => finish(null, null, error.message));
234
+ child.on("close", (code, signal) => finish(code, signal));
235
+ // `close` waits for every holder of the pipes. Once the shell itself has
236
+ // exited, what is left of its group is killed and the pipes get a short
237
+ // window to drain, so a background child cannot hold the run open.
238
+ child.on("exit", (code, signal) => {
239
+ if (pid !== undefined)
240
+ killGroup(pid, "SIGKILL");
241
+ drainTimer = setTimeout(() => finish(code, signal), drain);
242
+ });
243
+ });
244
+ }
@@ -0,0 +1,79 @@
1
+ /** Extension registrations: what `extension register` saves, how a value is
2
+ * checked, and which registrations apply to one review.
3
+ *
4
+ * Registrations live in `config.json` as an `extensions` array. The array
5
+ * order is the registration order, which breaks priority ties, so an update
6
+ * rewrites an entry in place instead of moving it to the end. */
7
+ import { type UserConfig } from "../config.ts";
8
+ /** The review kinds an extension can run in. `pr` is a pull request review,
9
+ * from the CLI or from `serve`. */
10
+ export declare const REVIEW_TYPES: readonly ["local", "pr", "remote"];
11
+ export type ReviewType = (typeof REVIEW_TYPES)[number];
12
+ /** The run-after target that means "the AI review itself". */
13
+ export declare const REVIEW_TARGET = "review";
14
+ export declare const MAX_PRIORITY = 3;
15
+ export declare const DEFAULT_TIMEOUT_SECONDS = 600;
16
+ export declare const DEFAULT_MAX_REQUESTED_RETRIES = 3;
17
+ export type ExtensionRegistration = {
18
+ name: string;
19
+ command: string;
20
+ /** `*`, `owner/repo`, `owner/*`, `*\/repo`, comma separated. */
21
+ repos: string;
22
+ /** Unset means every review type. */
23
+ reviewTypes?: ReviewType[];
24
+ /** An extension name or `review`. */
25
+ runAfter?: string;
26
+ parallelism: boolean;
27
+ priority: number;
28
+ failFast: boolean;
29
+ retryOnFailTimes: number;
30
+ maxRequestedRetries: number;
31
+ timeoutSeconds: number;
32
+ /** Unset follows the global `extensionRerunOnRestart`. */
33
+ rerunOnRestart?: boolean;
34
+ disabled?: boolean;
35
+ };
36
+ /** `CM_EXTENSIONS_DISABLED=1` turns extensions off hard. The hosted cloud sets
37
+ * it from outside the container, so nothing in the user's data volume can turn
38
+ * them back on. */
39
+ export declare function extensionsDisabled(): boolean;
40
+ export declare const DISABLED_MESSAGE = "Extensions are turned off on this machine (CM_EXTENSIONS_DISABLED=1).";
41
+ export declare function nameProblem(name: string): string | undefined;
42
+ /** Checks a `--repos` value. Each entry is `*` or `owner/repo` where either
43
+ * side may be `*`. */
44
+ export declare function reposProblem(value: string): string | undefined;
45
+ /** Whether a registration's `repos` pattern covers `repo`. GitHub names are
46
+ * case-insensitive, so the match is too. */
47
+ export declare function matchesRepo(pattern: string, repo: string): boolean;
48
+ export declare function parseReviewTypes(value: string): ReviewType[] | string;
49
+ /** `10m`, `30s`, `1h` or a plain number of seconds. */
50
+ export declare function parseTimeout(value: string): number | string;
51
+ /** `600` reads back as `10m`. */
52
+ export declare function formatTimeout(seconds: number): string;
53
+ export declare function parseCount(value: string, flag: string, max: number): number | string;
54
+ export declare const LIMITS: {
55
+ maxRetries: number;
56
+ };
57
+ export declare function parseBoolean(value: string, flag: string): boolean | string;
58
+ /** `review`, in any case, is the AI review. */
59
+ export declare function normalRunAfter(value: unknown): string | undefined;
60
+ /** Why an entry from config.json cannot run, or its normal form. A hand edit
61
+ * gets the same defaults and limits `register` applies, so a missing timeout
62
+ * or retry limit cannot turn into an instant timeout or an endless loop. */
63
+ export declare function normalizeRegistration(entry: unknown): ExtensionRegistration | string;
64
+ /** The registrations in config.json, oldest first, in their normal form. An
65
+ * entry that cannot run is left out with a warning that names it, rather than
66
+ * failing every review. */
67
+ export declare function readRegistrations(config?: Pick<UserConfig, "extensions">, warn?: (message: string) => void): ExtensionRegistration[];
68
+ /** Writes the registrations. Entries `readRegistrations` ignored stay in the
69
+ * file as they were, so a command never deletes what someone wrote by hand. */
70
+ export declare function writeRegistrations(registrations: ExtensionRegistration[]): Promise<void>;
71
+ /** The first cycle the run-after links form, as `a -> b -> a`, or undefined.
72
+ * `review` is the start point and never waits, so it cannot close a cycle. */
73
+ export declare function runAfterCycle(registrations: Pick<ExtensionRegistration, "name" | "runAfter">[]): string | undefined;
74
+ /** Whether `rerunOnRestart` is on for one registration: its own value, then the
75
+ * global config key, then on. */
76
+ export declare function rerunsOnRestart(registration: Pick<ExtensionRegistration, "rerunOnRestart">, config?: Pick<UserConfig, "extensionRerunOnRestart">): boolean;
77
+ /** The registrations that run for one review: enabled, matching the repository
78
+ * and the review type, in registration order. */
79
+ export declare function applicableRegistrations(registrations: ExtensionRegistration[], repo: string, type: ReviewType): ExtensionRegistration[];