harnessed 4.32.10 → 4.32.12
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/bin/harnessed-stop-hook.mjs +87 -135
- package/dist/cli.mjs +13 -7
- package/dist/cli.mjs.map +1 -1
- package/dist/index.mjs +1 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -2
|
@@ -1,180 +1,132 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
// 4.30.0 (issue #6) — Stop hook: auto-recover silent "mode-B" tool-call
|
|
3
|
-
// corruption. Claude Code sometimes mis-samples the tool-open delimiter into a
|
|
4
|
-
// garbage word (count/court/…), so the whole <invoke>…</invoke> block is emitted
|
|
5
|
-
// as PLAIN TEXT — no tool runs, no error, the turn just ends and the model gets
|
|
6
|
-
// no failure signal. The Stop hook fires at turn end; on detecting the mode-B
|
|
7
|
-
// signature it returns {"decision":"block","reason":…} to hand the model one
|
|
8
|
-
// fresh turn to re-emit the call as a real tool_use — no user "retry" needed.
|
|
9
|
-
//
|
|
10
|
-
// Self-contained plain JS (no project imports) — must stay equivalent to
|
|
11
|
-
// src/checkpoint/modeBDetect.ts detectModeB; the parity test in
|
|
12
|
-
// tests/checkpoint/stopHook.test.ts runs this file and cross-checks. Fail-soft:
|
|
13
|
-
// ANY error or non-match exits 0 with no output (a Stop hook must never wedge a
|
|
14
|
-
// normal turn end).
|
|
15
|
-
//
|
|
16
|
-
// stdin payload (CC Stop hook): {session_id, transcript_path, stop_hook_active,
|
|
17
|
-
// hook_event_name, cwd, …}. Loop guard: stop_hook_active + a per-(session,
|
|
18
|
-
// message-signature) retry cap of MAX_RETRIES persisted under the state root.
|
|
19
|
-
import { createHash } from 'node:crypto'
|
|
20
|
-
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'
|
|
21
|
-
import { homedir } from 'node:os'
|
|
22
|
-
import { join } from 'node:path'
|
|
23
2
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
return override !== undefined && override !== ''
|
|
30
|
-
? override
|
|
31
|
-
: join(homedir(), '.claude', 'harnessed')
|
|
32
|
-
}
|
|
3
|
+
// src/checkpoint/stopHookMain.ts
|
|
4
|
+
import { createHash } from "node:crypto";
|
|
5
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
6
|
+
import { homedir } from "node:os";
|
|
7
|
+
import { join } from "node:path";
|
|
33
8
|
|
|
9
|
+
// src/checkpoint/modeBDetect.ts
|
|
10
|
+
var TAIL_WINDOW = 200;
|
|
11
|
+
var INVOKE_RE = /<invoke name="/g;
|
|
12
|
+
var PARAM_RE = /<parameter name="/g;
|
|
34
13
|
function stripCode(s) {
|
|
35
|
-
return s.replace(/```[\s\S]*?```/g,
|
|
14
|
+
return s.replace(/```[\s\S]*?```/g, "").replace(/`[^`]*`/g, "");
|
|
36
15
|
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
let last =
|
|
41
|
-
|
|
42
|
-
return last
|
|
16
|
+
function lastIndexOfRe(s, re) {
|
|
17
|
+
let last = -1;
|
|
18
|
+
re.lastIndex = 0;
|
|
19
|
+
for (let m = re.exec(s); m !== null; m = re.exec(s)) last = m.index;
|
|
20
|
+
return last;
|
|
43
21
|
}
|
|
44
|
-
|
|
45
|
-
// Mirror of src/checkpoint/modeBDetect.ts detectModeB.
|
|
46
22
|
function detectModeB(content) {
|
|
47
|
-
if (!Array.isArray(content) || content.length === 0) return false
|
|
48
|
-
if (content.some((b) => b && b.type ===
|
|
49
|
-
const text = content
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
const
|
|
55
|
-
if (
|
|
56
|
-
const
|
|
57
|
-
|
|
58
|
-
if (invokeAt < 0 || paramAt < 0) return false
|
|
59
|
-
const lastTag = Math.max(invokeAt, paramAt)
|
|
60
|
-
return stripped.length - lastTag <= TAIL_WINDOW
|
|
23
|
+
if (!Array.isArray(content) || content.length === 0) return false;
|
|
24
|
+
if (content.some((b) => b && b.type === "tool_use")) return false;
|
|
25
|
+
const text = content.filter((b) => b && b.type === "text" && typeof b.text === "string").map((b) => b.text).join("\n");
|
|
26
|
+
if (text.length === 0) return false;
|
|
27
|
+
const stripped = stripCode(text);
|
|
28
|
+
if (stripped.length === 0) return false;
|
|
29
|
+
const invokeAt = lastIndexOfRe(stripped, INVOKE_RE);
|
|
30
|
+
const paramAt = lastIndexOfRe(stripped, PARAM_RE);
|
|
31
|
+
if (invokeAt < 0 || paramAt < 0) return false;
|
|
32
|
+
const lastTag = Math.max(invokeAt, paramAt);
|
|
33
|
+
return stripped.length - lastTag <= TAIL_WINDOW;
|
|
61
34
|
}
|
|
62
35
|
|
|
63
|
-
|
|
36
|
+
// src/checkpoint/stopHookMain.ts
|
|
37
|
+
var MAX_RETRIES = 2;
|
|
38
|
+
function stateRoot() {
|
|
39
|
+
const override = process.env.HARNESSED_ROOT_OVERRIDE;
|
|
40
|
+
return override !== void 0 && override !== "" ? override : join(homedir(), ".claude", "harnessed");
|
|
41
|
+
}
|
|
64
42
|
function messageSig(content) {
|
|
65
|
-
const text = content
|
|
66
|
-
|
|
67
|
-
.map((b) => b.text)
|
|
68
|
-
.join('\n')
|
|
69
|
-
return createHash('sha256').update(text).digest('hex').slice(0, 16)
|
|
43
|
+
const text = content.filter((b) => b && b.type === "text" && typeof b.text === "string").map((b) => b.text).join("\n");
|
|
44
|
+
return createHash("sha256").update(text).digest("hex").slice(0, 16);
|
|
70
45
|
}
|
|
71
|
-
|
|
72
|
-
/** Read the LAST assistant message's content blocks from a transcript JSONL.
|
|
73
|
-
* Streams from the file tail (transcripts run to 100k+ lines) — read the last
|
|
74
|
-
* ~256KB, split to lines, scan bottom-up for the last {type:'assistant'} entry.
|
|
75
|
-
* Returns null on any failure (fail-soft). */
|
|
76
46
|
function lastAssistantContent(transcriptPath) {
|
|
77
47
|
try {
|
|
78
|
-
if (!transcriptPath || !existsSync(transcriptPath)) return null
|
|
79
|
-
const buf = readFileSync(transcriptPath)
|
|
80
|
-
const slice = buf.length > 262144 ? buf.subarray(buf.length - 262144) : buf
|
|
81
|
-
const lines = slice.toString(
|
|
48
|
+
if (!transcriptPath || !existsSync(transcriptPath)) return null;
|
|
49
|
+
const buf = readFileSync(transcriptPath);
|
|
50
|
+
const slice = buf.length > 262144 ? buf.subarray(buf.length - 262144) : buf;
|
|
51
|
+
const lines = slice.toString("utf8").split("\n");
|
|
82
52
|
for (let i = lines.length - 1; i >= 0; i--) {
|
|
83
|
-
const line = lines[i].trim()
|
|
84
|
-
if (!line) continue
|
|
85
|
-
let obj
|
|
53
|
+
const line = (lines[i] ?? "").trim();
|
|
54
|
+
if (!line) continue;
|
|
55
|
+
let obj;
|
|
86
56
|
try {
|
|
87
|
-
obj = JSON.parse(line)
|
|
57
|
+
obj = JSON.parse(line);
|
|
88
58
|
} catch {
|
|
89
|
-
continue
|
|
59
|
+
continue;
|
|
90
60
|
}
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
return Array.isArray(content) ? content : null
|
|
61
|
+
if (obj && obj.type === "assistant") {
|
|
62
|
+
const content = obj.message?.content ?? obj.content;
|
|
63
|
+
return Array.isArray(content) ? content : null;
|
|
95
64
|
}
|
|
96
65
|
}
|
|
97
66
|
} catch {
|
|
98
|
-
// fall through
|
|
99
67
|
}
|
|
100
|
-
return null
|
|
68
|
+
return null;
|
|
101
69
|
}
|
|
102
|
-
|
|
103
70
|
function retryStore(sessionId) {
|
|
104
|
-
const id = createHash(
|
|
105
|
-
|
|
106
|
-
.digest('hex')
|
|
107
|
-
.slice(0, 16)
|
|
108
|
-
return join(stateRoot(), 'stop-hook-retries', `${id}.json`)
|
|
71
|
+
const id = createHash("sha256").update(String(sessionId ?? "nosession")).digest("hex").slice(0, 16);
|
|
72
|
+
return join(stateRoot(), "stop-hook-retries", `${id}.json`);
|
|
109
73
|
}
|
|
110
|
-
|
|
111
74
|
function readRetry(path) {
|
|
112
75
|
try {
|
|
113
|
-
if (!existsSync(path)) return { sig:
|
|
114
|
-
const j = JSON.parse(readFileSync(path,
|
|
115
|
-
return { sig: typeof j.sig ===
|
|
76
|
+
if (!existsSync(path)) return { sig: "", count: 0 };
|
|
77
|
+
const j = JSON.parse(readFileSync(path, "utf8"));
|
|
78
|
+
return { sig: typeof j.sig === "string" ? j.sig : "", count: Number(j.count) || 0 };
|
|
116
79
|
} catch {
|
|
117
|
-
return { sig:
|
|
80
|
+
return { sig: "", count: 0 };
|
|
118
81
|
}
|
|
119
82
|
}
|
|
120
|
-
|
|
121
83
|
function writeRetry(path, sig, count) {
|
|
122
84
|
try {
|
|
123
|
-
mkdirSync(join(path,
|
|
124
|
-
writeFileSync(path, JSON.stringify({ sig, count }),
|
|
125
|
-
return true
|
|
85
|
+
mkdirSync(join(path, ".."), { recursive: true });
|
|
86
|
+
writeFileSync(path, JSON.stringify({ sig, count }), "utf8");
|
|
87
|
+
return true;
|
|
126
88
|
} catch {
|
|
127
|
-
return false
|
|
89
|
+
return false;
|
|
128
90
|
}
|
|
129
91
|
}
|
|
130
|
-
|
|
131
92
|
function readStdin() {
|
|
132
93
|
return new Promise((resolve) => {
|
|
133
|
-
let data =
|
|
134
|
-
if (process.stdin.isTTY) return resolve(
|
|
135
|
-
process.stdin.setEncoding(
|
|
136
|
-
process.stdin.on(
|
|
137
|
-
data += c
|
|
138
|
-
})
|
|
139
|
-
process.stdin.on(
|
|
140
|
-
process.stdin.on(
|
|
141
|
-
setTimeout(() => resolve(data),
|
|
142
|
-
})
|
|
94
|
+
let data = "";
|
|
95
|
+
if (process.stdin.isTTY) return resolve("");
|
|
96
|
+
process.stdin.setEncoding("utf8");
|
|
97
|
+
process.stdin.on("data", (c) => {
|
|
98
|
+
data += c;
|
|
99
|
+
});
|
|
100
|
+
process.stdin.on("end", () => resolve(data));
|
|
101
|
+
process.stdin.on("error", () => resolve(data));
|
|
102
|
+
setTimeout(() => resolve(data), 2e3).unref?.();
|
|
103
|
+
});
|
|
143
104
|
}
|
|
144
|
-
|
|
145
105
|
async function main() {
|
|
146
|
-
const raw = await readStdin()
|
|
147
|
-
let payload
|
|
106
|
+
const raw = await readStdin();
|
|
107
|
+
let payload;
|
|
148
108
|
try {
|
|
149
|
-
payload = JSON.parse(raw)
|
|
109
|
+
payload = JSON.parse(raw);
|
|
150
110
|
} catch {
|
|
151
|
-
return
|
|
111
|
+
return;
|
|
152
112
|
}
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
const
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
const
|
|
162
|
-
|
|
163
|
-
const prev = readRetry(store)
|
|
164
|
-
const count = prev.sig === sig ? prev.count : 0
|
|
165
|
-
if (count >= MAX_RETRIES) return // give up — avoid an infinite corrupt loop
|
|
166
|
-
const saved = writeRetry(store, sig, count + 1)
|
|
167
|
-
if (!saved) return // could not record → do not block (fail-soft, no loop risk)
|
|
168
|
-
|
|
113
|
+
if (payload.stop_hook_active === true) return;
|
|
114
|
+
const content = lastAssistantContent(payload.transcript_path);
|
|
115
|
+
if (!content || !detectModeB(content)) return;
|
|
116
|
+
const sig = messageSig(content);
|
|
117
|
+
const store = retryStore(payload.session_id);
|
|
118
|
+
const prev = readRetry(store);
|
|
119
|
+
const count = prev.sig === sig ? prev.count : 0;
|
|
120
|
+
if (count >= MAX_RETRIES) return;
|
|
121
|
+
const saved = writeRetry(store, sig, count + 1);
|
|
122
|
+
if (!saved) return;
|
|
169
123
|
process.stdout.write(
|
|
170
124
|
`${JSON.stringify({
|
|
171
|
-
decision:
|
|
172
|
-
reason:
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
)
|
|
125
|
+
decision: "block",
|
|
126
|
+
reason: "MODE-B tool corruption detected: your previous message emitted a tool call as plain text (the opening delimiter was mis-sampled to a garbage word). Re-emit that exact same tool call now as a real tool call \u2014 content unchanged."
|
|
127
|
+
})}
|
|
128
|
+
`
|
|
129
|
+
);
|
|
176
130
|
}
|
|
177
|
-
|
|
178
131
|
main().catch(() => {
|
|
179
|
-
|
|
180
|
-
})
|
|
132
|
+
});
|
package/dist/cli.mjs
CHANGED
|
@@ -38,7 +38,7 @@ var init_package = __esm({
|
|
|
38
38
|
"package.json"() {
|
|
39
39
|
package_default = {
|
|
40
40
|
name: "harnessed",
|
|
41
|
-
version: "4.32.
|
|
41
|
+
version: "4.32.12",
|
|
42
42
|
description: "AI coding harness package manager + composition orchestrator",
|
|
43
43
|
type: "module",
|
|
44
44
|
license: "Apache-2.0",
|
|
@@ -96,7 +96,8 @@ var init_package = __esm({
|
|
|
96
96
|
],
|
|
97
97
|
scripts: {
|
|
98
98
|
dev: "tsup --watch",
|
|
99
|
-
build: "tsc --noEmit && tsup",
|
|
99
|
+
build: "tsc --noEmit && tsup && node ./scripts/build-hooks.mjs",
|
|
100
|
+
"build:hooks": "node ./scripts/build-hooks.mjs",
|
|
100
101
|
"build:schema": "node ./scripts/build-schema.mjs",
|
|
101
102
|
"validate:schema": "node ./scripts/validate-schema.mjs",
|
|
102
103
|
typecheck: "tsc --noEmit",
|
|
@@ -128,6 +129,7 @@ var init_package = __esm({
|
|
|
128
129
|
"@types/node": "^22.10.0",
|
|
129
130
|
"@types/proper-lockfile": "^4.1.4",
|
|
130
131
|
"@vitest/coverage-v8": "^4.0.0",
|
|
132
|
+
esbuild: "0.27.7",
|
|
131
133
|
tsup: "^8.3.0",
|
|
132
134
|
typescript: "^5.6.0",
|
|
133
135
|
vitest: "^4.0.0"
|
|
@@ -3733,14 +3735,18 @@ var init_fallbackHandlers = __esm({
|
|
|
3733
3735
|
|
|
3734
3736
|
// src/workflow/lib/ralphLoop.ts
|
|
3735
3737
|
function isComplete(output) {
|
|
3738
|
+
let env;
|
|
3736
3739
|
try {
|
|
3737
|
-
|
|
3738
|
-
if (env.subtype === "success" && env.structured_output?.status === "COMPLETE") return true;
|
|
3739
|
-
if (extractPromise(env.text ?? env.result ?? "") === "COMPLETE") return true;
|
|
3740
|
-
return false;
|
|
3740
|
+
env = JSON.parse(output);
|
|
3741
3741
|
} catch {
|
|
3742
3742
|
return extractPromise(output) === "COMPLETE";
|
|
3743
3743
|
}
|
|
3744
|
+
const succeeded = env.subtype === "success";
|
|
3745
|
+
const status = env.structured_output?.status;
|
|
3746
|
+
if (status === "COMPLETE") return succeeded;
|
|
3747
|
+
if (status === "PARTIAL" || status === "BLOCKED") return false;
|
|
3748
|
+
if (succeeded) return true;
|
|
3749
|
+
return extractPromise(env.text ?? env.result ?? "") === "COMPLETE";
|
|
3744
3750
|
}
|
|
3745
3751
|
async function ralphLoopWrap(spawn6, maxIter) {
|
|
3746
3752
|
let last = "";
|
|
@@ -4450,7 +4456,7 @@ This applies to: strategic-layer review skip / phase-layer clarification skip /
|
|
|
4450
4456
|
};
|
|
4451
4457
|
}
|
|
4452
4458
|
const env = JSON.parse(envelopeJson);
|
|
4453
|
-
const status =
|
|
4459
|
+
const status = isComplete(envelopeJson) ? "ok" : "fail";
|
|
4454
4460
|
const escalation = env.structured_output?.needs_teams_escalation === true;
|
|
4455
4461
|
return {
|
|
4456
4462
|
status,
|