chompyy 0.1.0 → 0.4.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.
Files changed (184) hide show
  1. package/README.md +82 -8
  2. package/THIRD_PARTY_NOTICES.md +29 -1
  3. package/dist/appstate.js +121 -4
  4. package/dist/away.js +191 -25
  5. package/dist/blueprint.js +102 -7
  6. package/dist/board.js +143 -15
  7. package/dist/catalog.js +81 -0
  8. package/dist/claudesettings.js +23 -0
  9. package/dist/cli.js +804 -55
  10. package/dist/coldstart.js +208 -0
  11. package/dist/config.js +43 -8
  12. package/dist/costs.js +162 -0
  13. package/dist/deps.js +124 -0
  14. package/dist/dream.js +422 -0
  15. package/dist/duration.js +3 -3
  16. package/dist/gate.js +40 -5
  17. package/dist/history.js +308 -0
  18. package/dist/live.js +255 -0
  19. package/dist/livequota.js +42 -5
  20. package/dist/macapp.js +25 -2
  21. package/dist/pacing.js +189 -0
  22. package/dist/permissions.js +106 -0
  23. package/dist/picture.js +124 -0
  24. package/dist/picturetype.js +208 -0
  25. package/dist/preflight.js +168 -0
  26. package/dist/projects.js +5 -4
  27. package/dist/quota.js +31 -5
  28. package/dist/refresh.js +290 -0
  29. package/dist/report.js +41 -21
  30. package/dist/review.js +111 -11
  31. package/dist/runner.js +318 -10
  32. package/dist/taste.js +528 -0
  33. package/dist/tokens.js +75 -0
  34. package/dist/tokenstore.js +61 -0
  35. package/dist/trace.js +284 -0
  36. package/dist/triggers.js +193 -0
  37. package/dist/usage.js +467 -0
  38. package/dist/vendor/codeburn/prices.js +45 -0
  39. package/dist/verify.js +150 -0
  40. package/dist/verifycard.js +146 -0
  41. package/dist/workspace.js +132 -18
  42. package/package.json +4 -2
  43. package/skills/taste/SKILL.md +4 -1
  44. package/vendor/diagram-design/LICENSE +21 -0
  45. package/vendor/diagram-design/SKILL.md +399 -0
  46. package/vendor/diagram-design/UPSTREAM.md +18 -0
  47. package/vendor/diagram-design/assets/example-arc-diagram.html +111 -0
  48. package/vendor/diagram-design/assets/example-architecture-containment.html +205 -0
  49. package/vendor/diagram-design/assets/example-architecture-delta.html +193 -0
  50. package/vendor/diagram-design/assets/example-architecture.html +183 -0
  51. package/vendor/diagram-design/assets/example-axonometric-plan-campus.html +108 -0
  52. package/vendor/diagram-design/assets/example-axonometric-plan-coffee-shop.html +121 -0
  53. package/vendor/diagram-design/assets/example-axonometric-plan-warehouse.html +127 -0
  54. package/vendor/diagram-design/assets/example-axonometric-plan.html +114 -0
  55. package/vendor/diagram-design/assets/example-bar.html +129 -0
  56. package/vendor/diagram-design/assets/example-beeswarm.html +384 -0
  57. package/vendor/diagram-design/assets/example-box-plot.html +127 -0
  58. package/vendor/diagram-design/assets/example-bubble.html +147 -0
  59. package/vendor/diagram-design/assets/example-bump.html +142 -0
  60. package/vendor/diagram-design/assets/example-data-flow.html +143 -0
  61. package/vendor/diagram-design/assets/example-datalake.html +269 -0
  62. package/vendor/diagram-design/assets/example-db-schema.html +302 -0
  63. package/vendor/diagram-design/assets/example-dependency.html +224 -0
  64. package/vendor/diagram-design/assets/example-deployment.html +190 -0
  65. package/vendor/diagram-design/assets/example-dp-integration.html +81 -0
  66. package/vendor/diagram-design/assets/example-dp-security-matrix.html +88 -0
  67. package/vendor/diagram-design/assets/example-er.html +202 -0
  68. package/vendor/diagram-design/assets/example-event-modeling.html +64 -0
  69. package/vendor/diagram-design/assets/example-exploded-ai-stack.html +85 -0
  70. package/vendor/diagram-design/assets/example-exploded-keyboard.html +84 -0
  71. package/vendor/diagram-design/assets/example-exploded-phone.html +83 -0
  72. package/vendor/diagram-design/assets/example-exploded-unboxing.html +84 -0
  73. package/vendor/diagram-design/assets/example-exploded.html +83 -0
  74. package/vendor/diagram-design/assets/example-fishbone.html +152 -0
  75. package/vendor/diagram-design/assets/example-flowchart.html +163 -0
  76. package/vendor/diagram-design/assets/example-gantt.html +137 -0
  77. package/vendor/diagram-design/assets/example-heatmap.html +224 -0
  78. package/vendor/diagram-design/assets/example-high-level-vertical.html +325 -0
  79. package/vendor/diagram-design/assets/example-high-level.html +301 -0
  80. package/vendor/diagram-design/assets/example-import-drawio.html +185 -0
  81. package/vendor/diagram-design/assets/example-import-excalidraw.html +70 -0
  82. package/vendor/diagram-design/assets/example-import-mermaid.html +70 -0
  83. package/vendor/diagram-design/assets/example-import-plantuml.html +107 -0
  84. package/vendor/diagram-design/assets/example-it-state.html +128 -0
  85. package/vendor/diagram-design/assets/example-journey.html +148 -0
  86. package/vendor/diagram-design/assets/example-kanban.html +174 -0
  87. package/vendor/diagram-design/assets/example-layers.html +123 -0
  88. package/vendor/diagram-design/assets/example-line.html +111 -0
  89. package/vendor/diagram-design/assets/example-loop.html +100 -0
  90. package/vendor/diagram-design/assets/example-marimekko.html +148 -0
  91. package/vendor/diagram-design/assets/example-medallion.html +159 -0
  92. package/vendor/diagram-design/assets/example-nested.html +136 -0
  93. package/vendor/diagram-design/assets/example-org-chart.html +45 -0
  94. package/vendor/diagram-design/assets/example-polar.html +116 -0
  95. package/vendor/diagram-design/assets/example-process.html +64 -0
  96. package/vendor/diagram-design/assets/example-pyramid.html +119 -0
  97. package/vendor/diagram-design/assets/example-quadrant-consultant.html +166 -0
  98. package/vendor/diagram-design/assets/example-quadrant.html +135 -0
  99. package/vendor/diagram-design/assets/example-radar.html +139 -0
  100. package/vendor/diagram-design/assets/example-requirement-traceability.html +122 -0
  101. package/vendor/diagram-design/assets/example-ridgeline.html +104 -0
  102. package/vendor/diagram-design/assets/example-sankey.html +150 -0
  103. package/vendor/diagram-design/assets/example-sbgn.html +213 -0
  104. package/vendor/diagram-design/assets/example-scatter.html +141 -0
  105. package/vendor/diagram-design/assets/example-sequence-oauth.html +227 -0
  106. package/vendor/diagram-design/assets/example-sequence.html +222 -0
  107. package/vendor/diagram-design/assets/example-service-blueprint.html +185 -0
  108. package/vendor/diagram-design/assets/example-slopegraph.html +119 -0
  109. package/vendor/diagram-design/assets/example-state-lifecycle.html +92 -0
  110. package/vendor/diagram-design/assets/example-state.html +147 -0
  111. package/vendor/diagram-design/assets/example-steps.html +78 -0
  112. package/vendor/diagram-design/assets/example-story-map.html +184 -0
  113. package/vendor/diagram-design/assets/example-streamgraph.html +99 -0
  114. package/vendor/diagram-design/assets/example-swimlane.html +172 -0
  115. package/vendor/diagram-design/assets/example-timeline.html +135 -0
  116. package/vendor/diagram-design/assets/example-tree-block-decomposition.html +151 -0
  117. package/vendor/diagram-design/assets/example-tree.html +173 -0
  118. package/vendor/diagram-design/assets/example-treemap.html +107 -0
  119. package/vendor/diagram-design/assets/example-uml-class.html +188 -0
  120. package/vendor/diagram-design/assets/example-unit-grid.html +141 -0
  121. package/vendor/diagram-design/assets/example-venn.html +110 -0
  122. package/vendor/diagram-design/assets/example-wardley.html +153 -0
  123. package/vendor/diagram-design/assets/example-waterfall.html +129 -0
  124. package/vendor/diagram-design/assets/template-dark.html +99 -0
  125. package/vendor/diagram-design/assets/template.html +100 -0
  126. package/vendor/diagram-design/references/layout-budget.md +103 -0
  127. package/vendor/diagram-design/references/output-spec.md +277 -0
  128. package/vendor/diagram-design/references/primitive-annotation.md +36 -0
  129. package/vendor/diagram-design/references/primitive-sketchy.md +43 -0
  130. package/vendor/diagram-design/references/primitives-core.md +146 -0
  131. package/vendor/diagram-design/references/semantic-patterns.md +154 -0
  132. package/vendor/diagram-design/references/style-guide.md +234 -0
  133. package/vendor/diagram-design/references/type-architecture-delta.md +143 -0
  134. package/vendor/diagram-design/references/type-architecture.md +117 -0
  135. package/vendor/diagram-design/references/type-axonometric-plan.md +83 -0
  136. package/vendor/diagram-design/references/type-bar.md +108 -0
  137. package/vendor/diagram-design/references/type-data-flow.md +371 -0
  138. package/vendor/diagram-design/references/type-db-schema.md +48 -0
  139. package/vendor/diagram-design/references/type-dependency.md +113 -0
  140. package/vendor/diagram-design/references/type-deployment.md +45 -0
  141. package/vendor/diagram-design/references/type-dp-integration.md +407 -0
  142. package/vendor/diagram-design/references/type-dp-security-matrix.md +379 -0
  143. package/vendor/diagram-design/references/type-er.md +25 -0
  144. package/vendor/diagram-design/references/type-event-modeling.md +36 -0
  145. package/vendor/diagram-design/references/type-exploded.md +115 -0
  146. package/vendor/diagram-design/references/type-fishbone.md +75 -0
  147. package/vendor/diagram-design/references/type-flowchart.md +23 -0
  148. package/vendor/diagram-design/references/type-gantt.md +45 -0
  149. package/vendor/diagram-design/references/type-heatmap.md +80 -0
  150. package/vendor/diagram-design/references/type-high-level.md +470 -0
  151. package/vendor/diagram-design/references/type-it-state.md +468 -0
  152. package/vendor/diagram-design/references/type-journey.md +55 -0
  153. package/vendor/diagram-design/references/type-kanban.md +49 -0
  154. package/vendor/diagram-design/references/type-layers.md +26 -0
  155. package/vendor/diagram-design/references/type-line.md +337 -0
  156. package/vendor/diagram-design/references/type-loop.md +223 -0
  157. package/vendor/diagram-design/references/type-medallion.md +356 -0
  158. package/vendor/diagram-design/references/type-nested.md +22 -0
  159. package/vendor/diagram-design/references/type-org-chart.md +44 -0
  160. package/vendor/diagram-design/references/type-polar.md +136 -0
  161. package/vendor/diagram-design/references/type-process.md +490 -0
  162. package/vendor/diagram-design/references/type-pyramid.md +33 -0
  163. package/vendor/diagram-design/references/type-quadrant.md +81 -0
  164. package/vendor/diagram-design/references/type-radar.md +82 -0
  165. package/vendor/diagram-design/references/type-requirement-traceability.md +45 -0
  166. package/vendor/diagram-design/references/type-sankey.md +75 -0
  167. package/vendor/diagram-design/references/type-sbgn.md +80 -0
  168. package/vendor/diagram-design/references/type-scatter.md +276 -0
  169. package/vendor/diagram-design/references/type-sequence.md +130 -0
  170. package/vendor/diagram-design/references/type-service-blueprint.md +103 -0
  171. package/vendor/diagram-design/references/type-state.md +30 -0
  172. package/vendor/diagram-design/references/type-steps.md +180 -0
  173. package/vendor/diagram-design/references/type-story-map.md +46 -0
  174. package/vendor/diagram-design/references/type-swimlane.md +22 -0
  175. package/vendor/diagram-design/references/type-timeline.md +20 -0
  176. package/vendor/diagram-design/references/type-tree.md +24 -0
  177. package/vendor/diagram-design/references/type-treemap.md +137 -0
  178. package/vendor/diagram-design/references/type-uml-class.md +65 -0
  179. package/vendor/diagram-design/references/type-unit-grid.md +63 -0
  180. package/vendor/diagram-design/references/type-venn.md +26 -0
  181. package/vendor/diagram-design/references/type-wardley.md +39 -0
  182. package/vendor/diagram-design/references/type-waterfall.md +75 -0
  183. package/vendor/diagram-design/scripts/self_check.py +1177 -0
  184. package/vendor/diagram-design/scripts/verify_geometry.py +696 -0
package/dist/runner.js CHANGED
@@ -1,8 +1,55 @@
1
1
  // The one seam: something that runs a prompt headless. ClaudeRunner wraps `claude -p`;
2
2
  // tests pass a fake.
3
3
  import { spawn, spawnSync } from 'node:child_process';
4
+ import { TokenTally } from './tokens.js';
5
+ import { closeSync, openSync, writeSync } from 'node:fs';
6
+ import { commandPattern, hostsInErrors } from './permissions.js';
7
+ /** The raw output written to disk, cut off after `max` bytes (with a note saying so). */
8
+ export class RawLog {
9
+ max;
10
+ fd = null;
11
+ written = 0;
12
+ constructor(path, max = 50 * 1024 * 1024) {
13
+ this.max = max;
14
+ if (!path)
15
+ return;
16
+ try {
17
+ this.fd = openSync(path, 'w');
18
+ }
19
+ catch { }
20
+ }
21
+ write(chunk) {
22
+ if (this.fd === null)
23
+ return;
24
+ const room = this.max - this.written;
25
+ const buf = Buffer.from(chunk);
26
+ if (buf.length <= room) {
27
+ writeSync(this.fd, buf);
28
+ this.written += buf.length;
29
+ return;
30
+ }
31
+ if (room > 0)
32
+ writeSync(this.fd, buf.subarray(0, room));
33
+ writeSync(this.fd, `\n[raw log cut off at ${this.max} bytes]\n`);
34
+ this.written = this.max;
35
+ this.close();
36
+ }
37
+ close() {
38
+ if (this.fd !== null)
39
+ closeSync(this.fd);
40
+ this.fd = null;
41
+ }
42
+ }
43
+ // Chompyy's runs never pick up other tools' hooks or MCP servers: a SessionStart hook would add its rules to
44
+ // every call, and a blocking Stop hook can force an extra turn whose reply replaces ours (headroom hit this;
45
+ // research/detective/RULEBOOK.md rule T3). Your login, settings, permissions and CLAUDE.md still load.
46
+ // (--bare would skip hooks too, but it also skips the subscription login.)
47
+ export const ISOLATION_ARGS = ['--strict-mcp-config'];
48
+ export const ISOLATION_SETTINGS = { disableAllHooks: true };
4
49
  export function claudeArgs(req) {
5
- const args = ['-p', '--output-format', 'text', '--no-session-persistence'];
50
+ const args = req.loopRepeat || req.stream
51
+ ? ['-p', '--output-format', 'stream-json', '--verbose', '--no-session-persistence', ...ISOLATION_ARGS]
52
+ : ['-p', '--output-format', 'text', '--no-session-persistence', ...ISOLATION_ARGS];
6
53
  if (req.model)
7
54
  args.push('--model', req.model);
8
55
  if (req.maxTurns)
@@ -17,12 +64,177 @@ export function claudeArgs(req) {
17
64
  args.push('--allowedTools', ...req.allowedTools);
18
65
  if (req.disallowedTools?.length)
19
66
  args.push('--disallowedTools', ...req.disallowedTools);
20
- if (req.settings)
21
- args.push('--settings', JSON.stringify(req.settings));
67
+ args.push('--settings', JSON.stringify({ ...req.settings, ...ISOLATION_SETTINGS }));
22
68
  if (req.extraArgs?.length)
23
69
  args.push(...req.extraArgs);
24
70
  return args;
25
71
  }
72
+ /**
73
+ * Circuit breaker (ralph-claude-code, codeburn guard): notices a run going in circles.
74
+ * - the same tool call (tool + exact input) `repeat` times among the last 20 calls, or
75
+ * - the same shell command failing `repeat - 1` times in a row.
76
+ */
77
+ export class LoopWatch {
78
+ repeat;
79
+ calls = [];
80
+ pending = new Map();
81
+ lastFailed = '';
82
+ failStreak = 0;
83
+ constructor(repeat) {
84
+ this.repeat = repeat;
85
+ }
86
+ /** Feed one stream-json event; returns why it is stuck, or null. */
87
+ feed(event) {
88
+ const e = event;
89
+ const content = Array.isArray(e.message?.content) ? e.message.content : [];
90
+ if (e.type === 'assistant') {
91
+ for (const block of content) {
92
+ if (block.type !== 'tool_use')
93
+ continue;
94
+ // The free-text description can differ between otherwise identical calls.
95
+ const { description: _d, ...input } = (block.input ?? {});
96
+ const sig = `${block.name}(${JSON.stringify(input)})`;
97
+ this.pending.set(String(block.id), sig);
98
+ this.calls = [...this.calls, sig].slice(-20);
99
+ const times = this.calls.filter((c) => c === sig).length;
100
+ if (times >= this.repeat)
101
+ return `called ${short(sig)} ${times} times`;
102
+ }
103
+ }
104
+ if (e.type === 'user') {
105
+ for (const block of content) {
106
+ if (block.type !== 'tool_result')
107
+ continue;
108
+ const sig = this.pending.get(String(block.tool_use_id)) ?? '';
109
+ if (!sig.startsWith('Bash('))
110
+ continue;
111
+ if (block.is_error) {
112
+ this.failStreak = sig === this.lastFailed ? this.failStreak + 1 : 1;
113
+ this.lastFailed = sig;
114
+ if (this.failStreak >= Math.max(2, this.repeat - 1))
115
+ return `${short(sig)} failed ${this.failStreak} times in a row`;
116
+ }
117
+ else if (sig === this.lastFailed) {
118
+ this.failStreak = 0;
119
+ this.lastFailed = '';
120
+ }
121
+ }
122
+ }
123
+ return null;
124
+ }
125
+ }
126
+ function short(sig) {
127
+ return sig.length > 120 ? `${sig.slice(0, 117)}…` : sig;
128
+ }
129
+ /** The final text of a stream-json run: the result event, else the assistant's text blocks. */
130
+ export function streamText(events) {
131
+ const t = new FinalText();
132
+ for (const e of events)
133
+ t.feed(e);
134
+ return t.text();
135
+ }
136
+ /**
137
+ * Keeps only what the final text needs: the result event, else the assistant's text blocks (capped).
138
+ * Not every event: with --verbose the stream carries whole tool results (file reads, build logs), and keeping
139
+ * them all is what ran an 8-hour away out of memory on 2026-11-17.
140
+ */
141
+ export class FinalText {
142
+ max;
143
+ result = null;
144
+ texts = [];
145
+ size = 0;
146
+ constructor(max = 256 * 1024) {
147
+ this.max = max;
148
+ }
149
+ feed(event) {
150
+ const e = event;
151
+ if (e.type === 'result' && typeof e.result === 'string')
152
+ this.result = e.result;
153
+ if (e.type !== 'assistant' || !Array.isArray(e.message?.content))
154
+ return;
155
+ for (const b of e.message.content) {
156
+ if (b?.type !== 'text' || !b.text)
157
+ continue;
158
+ this.texts.push(b.text);
159
+ this.size += b.text.length;
160
+ while (this.size > this.max && this.texts.length > 1)
161
+ this.size -= this.texts.shift().length;
162
+ }
163
+ }
164
+ text() {
165
+ return this.result ?? this.texts.join('\n');
166
+ }
167
+ }
168
+ /** A stream-json line longer than this is a giant tool result we don't need: skip it rather than buffer it. */
169
+ export const MAX_LINE = 4 * 1024 * 1024;
170
+ /**
171
+ * Splits a stream into lines in constant memory per line: looks for newlines only in the new chunk (never
172
+ * re-splits what is buffered), and drops a line once it grows past `maxLine`.
173
+ */
174
+ export class LineReader {
175
+ onLine;
176
+ maxLine;
177
+ buf = '';
178
+ skipping = false;
179
+ constructor(onLine, maxLine = MAX_LINE) {
180
+ this.onLine = onLine;
181
+ this.maxLine = maxLine;
182
+ }
183
+ push(chunk) {
184
+ let start = 0;
185
+ let nl;
186
+ while ((nl = chunk.indexOf('\n', start)) !== -1) {
187
+ if (!this.skipping) {
188
+ const line = this.buf + chunk.slice(start, nl);
189
+ if (line.length <= this.maxLine)
190
+ this.onLine(line);
191
+ }
192
+ this.buf = '';
193
+ this.skipping = false;
194
+ start = nl + 1;
195
+ }
196
+ if (this.skipping || start === chunk.length)
197
+ return;
198
+ if (this.buf.length + chunk.length - start > this.maxLine) {
199
+ this.buf = '';
200
+ this.skipping = true;
201
+ }
202
+ else
203
+ this.buf += chunk.slice(start);
204
+ }
205
+ end() {
206
+ if (!this.skipping && this.buf.trim())
207
+ this.onLine(this.buf);
208
+ this.buf = '';
209
+ this.skipping = false;
210
+ }
211
+ }
212
+ /** Text that keeps its head and its tail once it passes `max` (CHECKS is at the end, the summary at the start). */
213
+ export class CappedText {
214
+ max;
215
+ head = '';
216
+ tail = '';
217
+ dropped = 0;
218
+ constructor(max = 2 * 1024 * 1024) {
219
+ this.max = max;
220
+ }
221
+ push(chunk) {
222
+ const half = this.max / 2;
223
+ if (this.head.length < half) {
224
+ const take = chunk.slice(0, half - this.head.length);
225
+ this.head += take;
226
+ chunk = chunk.slice(take.length);
227
+ }
228
+ this.tail += chunk;
229
+ if (this.tail.length > half) {
230
+ this.dropped += this.tail.length - half;
231
+ this.tail = this.tail.slice(-half);
232
+ }
233
+ }
234
+ toString() {
235
+ return this.dropped ? `${this.head}\n[… ${this.dropped} characters cut …]\n${this.tail}` : this.head + this.tail;
236
+ }
237
+ }
26
238
  /** Process groups of agents still running, so Ctrl-C on chompyy stops them too. */
27
239
  const active = new Set();
28
240
  let handlersInstalled = false;
@@ -54,8 +266,10 @@ export class ClaudeRunner {
54
266
  // Own process group, so a timeout also stops commands the agent started (tests, builds).
55
267
  const child = spawn(this.bin, claudeArgs(req), { cwd: req.cwd, stdio: ['pipe', 'pipe', 'pipe'], detached: true, env: { ...process.env, ...req.env } });
56
268
  stopActiveOnExit();
57
- if (child.pid)
269
+ if (child.pid) {
58
270
  active.add(child.pid);
271
+ req.onSpawn?.(child.pid);
272
+ }
59
273
  const killAll = (signal) => {
60
274
  try {
61
275
  if (child.pid)
@@ -65,11 +279,98 @@ export class ClaudeRunner {
65
279
  child.kill(signal);
66
280
  }
67
281
  };
68
- let output = '';
69
- let stderr = '';
282
+ const text = new CappedText();
283
+ const stderr = new CappedText(256 * 1024);
70
284
  let timedOut = false;
71
- child.stdout.on('data', (d) => (output += d.toString()));
72
- child.stderr.on('data', (d) => (stderr += d.toString()));
285
+ let stuck;
286
+ const final = new FinalText();
287
+ let denied = 0;
288
+ const deniedTools = new Map();
289
+ // What it was blocked on: blocked Bash calls link back to their command by id; hosts come from error text.
290
+ const bashCommands = new Map();
291
+ const neededCommands = new Set();
292
+ const neededHosts = new Set();
293
+ const raw = new RawLog(req.rawLogPath, req.rawLogMaxBytes);
294
+ const tally = new TokenTally();
295
+ const watch = req.loopRepeat ? new LoopWatch(req.loopRepeat) : req.stream ? new LoopWatch(Number.POSITIVE_INFINITY) : null;
296
+ const lines = new LineReader((line) => {
297
+ if (!line.trim())
298
+ return;
299
+ let event;
300
+ try {
301
+ event = JSON.parse(line);
302
+ }
303
+ catch {
304
+ return;
305
+ }
306
+ final.feed(event);
307
+ tally.feed(event);
308
+ const ev = event;
309
+ if (Array.isArray(ev.message?.content)) {
310
+ for (const b of ev.message.content) {
311
+ if (b?.type === 'tool_use' && b.name === 'Bash' && b.id && typeof b.input?.command === 'string')
312
+ bashCommands.set(b.id, b.input.command);
313
+ if (b?.type === 'tool_result') {
314
+ const text = typeof b.content === 'string' ? b.content : JSON.stringify(b.content ?? '');
315
+ for (const h of hostsInErrors(text.slice(0, 20_000)))
316
+ neededHosts.add(h);
317
+ }
318
+ }
319
+ }
320
+ if (ev.type === 'system' && ev.subtype === 'permission_denied') {
321
+ if (ev.tool_name === 'Bash' && ev.tool_use_id) {
322
+ const pattern = commandPattern(bashCommands.get(ev.tool_use_id) ?? '');
323
+ if (pattern)
324
+ neededCommands.add(pattern);
325
+ }
326
+ // A blocked action isn't progress. Thousands of them (npm retrying with no network) mean it's stuck.
327
+ denied++;
328
+ deniedTools.set(ev.tool_name ?? '?', (deniedTools.get(ev.tool_name ?? '?') ?? 0) + 1);
329
+ if (req.maxDenied && denied >= req.maxDenied && !stuck) {
330
+ const top = [...deniedTools.entries()].sort((a, b) => b[1] - a[1])[0]?.[0] ?? '?';
331
+ stuck = `the sandbox blocked it ${denied} times (mostly ${top === 'SandboxNetworkAccess' ? 'network access: away runs are offline' : top})`;
332
+ killAll('SIGTERM');
333
+ setTimeout(() => killAll('SIGKILL'), 5000).unref();
334
+ }
335
+ return;
336
+ }
337
+ poke();
338
+ const why = stuck ? null : watch.feed(event);
339
+ if (why) {
340
+ stuck = why;
341
+ killAll('SIGTERM');
342
+ setTimeout(() => killAll('SIGKILL'), 5000).unref();
343
+ }
344
+ });
345
+ child.stdout.setEncoding('utf8');
346
+ child.stderr.setEncoding('utf8');
347
+ // No-progress watchdog: any output counts as progress (stream-json prints every tool call and result).
348
+ let stalled;
349
+ let idle;
350
+ const poke = () => {
351
+ if (!req.idleTimeoutMs)
352
+ return;
353
+ clearTimeout(idle);
354
+ idle = setTimeout(() => {
355
+ stalled = `no progress for ${Math.round(req.idleTimeoutMs / 60_000)}m`;
356
+ killAll('SIGTERM');
357
+ setTimeout(() => killAll('SIGKILL'), 5000).unref();
358
+ }, req.idleTimeoutMs);
359
+ };
360
+ poke();
361
+ child.stdout.on('data', (d) => {
362
+ if (!watch)
363
+ poke(); // with stream-json, a line counts as progress only if it isn't a blocked action
364
+ raw.write(d);
365
+ if (watch)
366
+ lines.push(d);
367
+ else
368
+ text.push(d);
369
+ });
370
+ child.stderr.on('data', (d) => {
371
+ poke();
372
+ stderr.push(d);
373
+ });
73
374
  const timer = setTimeout(() => {
74
375
  timedOut = true;
75
376
  killAll('SIGTERM');
@@ -77,12 +378,19 @@ export class ClaudeRunner {
77
378
  }, req.timeoutMs);
78
379
  const done = (exitCode) => {
79
380
  clearTimeout(timer);
381
+ clearTimeout(idle);
382
+ raw.close();
80
383
  if (child.pid)
81
384
  active.delete(child.pid);
82
- resolve({ exitCode, output, stderr, timedOut, durationMs: Date.now() - started });
385
+ if (watch)
386
+ lines.end();
387
+ const output = watch ? final.text() : text.toString();
388
+ resolve({ exitCode, output, stderr: stderr.toString(), timedOut, durationMs: Date.now() - started, ...(stuck ? { stuck } : {}), ...(stalled ? { stalled } : {}),
389
+ ...(tally.total() ? { tokens: tally.total() } : {}),
390
+ ...(neededCommands.size || neededHosts.size ? { needs: { commands: [...neededCommands], hosts: [...neededHosts] } } : {}) });
83
391
  };
84
392
  child.on('error', (err) => {
85
- stderr += String(err);
393
+ stderr.push(String(err));
86
394
  done(null);
87
395
  });
88
396
  child.on('close', (code) => done(code));