pacework 1.7.14 → 1.7.16

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/lib/stuck.mjs ADDED
@@ -0,0 +1,121 @@
1
+ /**
2
+ * Notices a run going in circles: the same command failing the same way again
3
+ * and again, with no success in between.
4
+ *
5
+ * A run like that used to go on until its time or cost limit, and was then
6
+ * reported as "limit reached". Here it is stopped as soon as the repeat count is
7
+ * reached, with the command and its error named, so a person can step in.
8
+ *
9
+ * What counts as the same error: the same shell command (whitespace aside)
10
+ * failing with the same output once numbers are taken out of it. Durations,
11
+ * counts, line numbers and timestamps change between two runs of one failing
12
+ * test, so they must not make two failures look different. A command that fails
13
+ * in a new way has made progress and starts counting again; one that succeeds is
14
+ * forgotten.
15
+ *
16
+ * Only the main session's shell commands count. A helper agent's calls and other
17
+ * tools are left out: a file that is not found or an edit that does not apply is
18
+ * ordinary work, not a loop.
19
+ *
20
+ * Everything here stays on the runner machine. Only a stop's reason, with the
21
+ * command and the first line of its error, goes to the run and the issue.
22
+ */
23
+
24
+ /** How many identical failures in a row stop a run, unless the runner says otherwise. */
25
+ export const DEFAULT_SAME_ERROR_LIMIT = 6
26
+
27
+ const SHELL_TOOLS = new Set(["Bash"])
28
+ /** Commands remembered at once; the oldest is dropped beyond it. */
29
+ const TRACKED_COMMANDS = 200
30
+ /** How much of an error is compared: its start and its end, where the cause and the summary are. */
31
+ const FINGERPRINT_EDGE = 600
32
+
33
+ const squash = (text) => String(text ?? "").replace(/\s+/g, " ").trim()
34
+
35
+ /** A tool result's text, whether Claude Code sent a string or content blocks. */
36
+ export function resultText(content) {
37
+ if (typeof content === "string") return content
38
+ if (!Array.isArray(content)) return ""
39
+ return content.map((block) => (typeof block === "string" ? block : typeof block?.text === "string" ? block.text : "")).join("\n")
40
+ }
41
+
42
+ /** An error's output reduced to what stays the same when the same thing fails again. */
43
+ export function errorFingerprint(text) {
44
+ const flat = squash(text).toLowerCase().replace(/0x[0-9a-f]+/g, "#").replace(/\d+(?:[.,:]\d+)*/g, "#")
45
+ return flat.length <= FINGERPRINT_EDGE * 2 ? flat : `${flat.slice(0, FINGERPRINT_EDGE)} … ${flat.slice(-FINGERPRINT_EDGE)}`
46
+ }
47
+
48
+ /** The first line of an error that says something, for a person to read. */
49
+ export function errorHeadline(text, max = 200) {
50
+ const lines = String(text ?? "").split(/\r?\n/).map((line) => line.trim()).filter(Boolean)
51
+ const line = lines.find((entry) => !/^exit code \d+$/i.test(entry)) ?? lines[0] ?? ""
52
+ return line.length > max ? `${line.slice(0, max - 1)}…` : line
53
+ }
54
+
55
+ /**
56
+ * Follows one run's stream of Claude Code events.
57
+ *
58
+ * @param {{ stopAfter?: number | null }} [options] Identical failures in a row that
59
+ * count as stuck; null or 0 turns the check off.
60
+ * @returns {{ observe: (event: unknown) => null | { command: string, error: string, repeats: number } }}
61
+ * `observe` returns a hit once, at the event that reaches the count.
62
+ */
63
+ export function createStuckTracker({ stopAfter = DEFAULT_SAME_ERROR_LIMIT } = {}) {
64
+ const limit = typeof stopAfter === "number" && stopAfter > 0 ? Math.floor(stopAfter) : null
65
+ /** tool_use id → command, until its result arrives. */
66
+ const pending = new Map()
67
+ /** command → { fingerprint, repeats, error }, while it keeps failing. */
68
+ const failing = new Map()
69
+ let reported = false
70
+
71
+ function observe(event) {
72
+ if (!limit || reported || !event || typeof event !== "object") return null
73
+ // A helper agent's own calls carry the id of the call that started it.
74
+ if (event.parent_tool_use_id) return null
75
+ const blocks = Array.isArray(event.message?.content) ? event.message.content : []
76
+ for (const block of blocks) {
77
+ if (!block || typeof block !== "object") continue
78
+ if (event.type === "assistant" && block.type === "tool_use" && SHELL_TOOLS.has(block.name)) {
79
+ const command = squash(block.input?.command)
80
+ if (command && typeof block.id === "string") pending.set(block.id, command)
81
+ continue
82
+ }
83
+ if (event.type !== "user" || block.type !== "tool_result" || typeof block.tool_use_id !== "string") continue
84
+ const command = pending.get(block.tool_use_id)
85
+ if (!command) continue
86
+ pending.delete(block.tool_use_id)
87
+ if (block.is_error !== true) {
88
+ failing.delete(command)
89
+ continue
90
+ }
91
+ const text = resultText(block.content)
92
+ const fingerprint = errorFingerprint(text)
93
+ const before = failing.get(command)
94
+ const repeats = before && before.fingerprint === fingerprint ? before.repeats + 1 : 1
95
+ // Re-inserted so the map's order is "least recently failed first".
96
+ failing.delete(command)
97
+ failing.set(command, { fingerprint, repeats, error: errorHeadline(text) })
98
+ if (failing.size > TRACKED_COMMANDS) failing.delete(failing.keys().next().value)
99
+ if (repeats >= limit) {
100
+ reported = true
101
+ return { command, error: errorHeadline(text), repeats }
102
+ }
103
+ }
104
+ return null
105
+ }
106
+
107
+ return { observe }
108
+ }
109
+
110
+ /** The stop for a run that kept repeating one failing command. Reads like the other limit stops. */
111
+ export function sameErrorStop(limit, hit) {
112
+ const command = hit.command.length > 160 ? `${hit.command.slice(0, 159)}…` : hit.command
113
+ return {
114
+ kind: "stuck",
115
+ limit,
116
+ actual: hit.repeats,
117
+ command,
118
+ error: hit.error,
119
+ reason: `the same command failed the same way ${hit.repeats} times in a row: \`${command}\`${hit.error ? ` (${hit.error})` : ""}`,
120
+ }
121
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pacework",
3
- "version": "1.7.14",
3
+ "version": "1.7.16",
4
4
  "description": "pacework CLI: connect this machine as a runner for pacework AI agents",
5
5
  "type": "module",
6
6
  "bin": {