harnessed 4.32.11 → 4.32.13
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-inject-state.mjs +358 -363
- package/bin/harnessed-stop-hook.mjs +87 -135
- package/dist/cli.mjs +8 -5
- 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.13",
|
|
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"
|
|
@@ -9867,12 +9869,12 @@ function findPhaseContextExcerpt(repoRoot, phase, budget) {
|
|
|
9867
9869
|
}
|
|
9868
9870
|
return null;
|
|
9869
9871
|
}
|
|
9870
|
-
function buildInjection(repoRoot, wf, learningsMd, budget = DEFAULT_INJECT_BUDGET, intent = null, nowMs = Date.now()) {
|
|
9872
|
+
function buildInjection(repoRoot, wf, learningsMd, budget = DEFAULT_INJECT_BUDGET, intent = null, nowMs = Date.now(), opts = {}) {
|
|
9871
9873
|
const ledgerEmpty = (wf?.sub_progress ?? []).length === 0;
|
|
9872
9874
|
const intentBlock = ledgerEmpty ? buildIntentBlock(intent, nowMs) : "";
|
|
9873
9875
|
if (!wf) return intentBlock;
|
|
9874
9876
|
const forward = nextPending(wf.sub_progress ?? []) === null ? forwardPointer(repoRoot, wf) : null;
|
|
9875
|
-
const ws = buildWorkflowStateBlock(wf, forward);
|
|
9877
|
+
const ws = buildWorkflowStateBlock(wf, forward, opts.ledgerAgeMs);
|
|
9876
9878
|
const ledgerSubs = (wf.sub_progress ?? []).map((e) => e.sub);
|
|
9877
9879
|
const rel = selectWithinBudget(
|
|
9878
9880
|
filterRelevantLearnings(parseLearnings(learningsMd), { phase: wf.phase, ledgerSubs }),
|
|
@@ -9880,7 +9882,8 @@ function buildInjection(repoRoot, wf, learningsMd, budget = DEFAULT_INJECT_BUDGE
|
|
|
9880
9882
|
);
|
|
9881
9883
|
const usedTokens = rel.reduce((a, e) => a + tok(e.raw), 0);
|
|
9882
9884
|
const ctx = findPhaseContextExcerpt(repoRoot, wf.phase, Math.max(0, budget - usedTokens));
|
|
9883
|
-
const
|
|
9885
|
+
const pcRaw = buildProjectContextBlock({ learnings: rel, contextExcerpt: ctx ?? void 0 });
|
|
9886
|
+
const pc2 = pcRaw && opts.pcGate ? opts.pcGate(pcRaw) ? pcRaw : "" : pcRaw;
|
|
9884
9887
|
const normal = pc2 ? `${ws}
|
|
9885
9888
|
${pc2}` : ws;
|
|
9886
9889
|
return intentBlock ? `${intentBlock}
|