architecture-harness 0.17.0 → 0.18.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.
- package/package.json +1 -1
- package/src/cli/budget-counter.mjs +25 -4
- package/src/cli/budget-guard.mjs +73 -4
- package/src/cli/budget-guard.test.mjs +111 -0
- package/src/cli/budget.mjs +3 -2
- package/src/cli/context-signal.mjs +92 -0
- package/src/cli/context-signal.test.mjs +99 -0
- package/src/cli/dispatch-guard.mjs +9 -5
- package/src/cli/dispatch-guard.test.mjs +5 -6
- package/src/cli/dispatch.mjs +20 -2
- package/src/cli/dispatch.test.mjs +25 -1
- package/src/cli/explore-hint.mjs +67 -7
- package/src/cli/explore-hint.test.mjs +104 -4
- package/src/cli/handoff-note.mjs +69 -0
- package/src/cli/handoff-note.test.mjs +71 -0
- package/src/cli/ledger.mjs +4 -2
- package/src/cli/plugin-agents.test.mjs +9 -0
- package/src/cli/qc.mjs +6 -0
- package/src/cli/report-stop.mjs +15 -5
- package/src/cli/report-stop.test.mjs +39 -0
- package/src/cli/task-gate.mjs +34 -12
- package/src/cli/task-gate.test.mjs +88 -2
- package/src/cli/tokens.mjs +239 -0
- package/src/cli/tokens.test.mjs +286 -0
- package/src/cli/transcript-usage.mjs +135 -0
- package/src/cli/transcript-usage.test.mjs +129 -0
- package/src/config.mjs +15 -1
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
// The reader of Claude Code transcripts that the context signal and `qc tokens` share. It keeps only
|
|
2
|
+
// the usage, id, model, and time of each assistant request, once per request, and never message text.
|
|
3
|
+
|
|
4
|
+
import { closeSync, existsSync, fstatSync, openSync, readFileSync, readSync } from "node:fs";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
|
|
7
|
+
export const WINDOW_BYTES = 256 * 1024;
|
|
8
|
+
const SYNTHETIC = "<synthetic>";
|
|
9
|
+
|
|
10
|
+
/** The input tokens one request sent: fresh input, cache reads, and cache writes. */
|
|
11
|
+
export const contextOf = (usage) =>
|
|
12
|
+
(usage?.input_tokens ?? 0) + (usage?.cache_read_input_tokens ?? 0) + (usage?.cache_creation_input_tokens ?? 0);
|
|
13
|
+
|
|
14
|
+
/** One transcript line as an assistant request with usage, or null for any other line. */
|
|
15
|
+
export function requestOf(line) {
|
|
16
|
+
if (typeof line !== "string" || !line.includes('"usage"')) return null;
|
|
17
|
+
let record;
|
|
18
|
+
try {
|
|
19
|
+
record = JSON.parse(line);
|
|
20
|
+
} catch {
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
const message = record?.message;
|
|
24
|
+
if (record?.type !== "assistant" || !message?.usage || message.model === SYNTHETIC) return null;
|
|
25
|
+
const at = Date.parse(record.timestamp ?? "");
|
|
26
|
+
return { id: message.id ?? record.requestId ?? null, model: message.model ?? null, at: Number.isNaN(at) ? null : at, usage: message.usage };
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Every request of a transcript, in order, once per id. @returns [] for a file that does not read */
|
|
30
|
+
export function readRequests(file) {
|
|
31
|
+
let text;
|
|
32
|
+
try {
|
|
33
|
+
text = readFileSync(file, "utf8");
|
|
34
|
+
} catch {
|
|
35
|
+
return [];
|
|
36
|
+
}
|
|
37
|
+
const seen = new Set();
|
|
38
|
+
const requests = [];
|
|
39
|
+
for (const line of text.split(/\r?\n/)) {
|
|
40
|
+
const request = requestOf(line);
|
|
41
|
+
if (!request) continue;
|
|
42
|
+
if (request.id !== null) {
|
|
43
|
+
if (seen.has(request.id)) continue;
|
|
44
|
+
seen.add(request.id);
|
|
45
|
+
}
|
|
46
|
+
requests.push(request);
|
|
47
|
+
}
|
|
48
|
+
return requests;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** The whole lines of one window at the head or the tail of a file, or [] when it does not read. */
|
|
52
|
+
function windowLines(file, fromEnd, bytes) {
|
|
53
|
+
let fd;
|
|
54
|
+
try {
|
|
55
|
+
fd = openSync(file, "r");
|
|
56
|
+
const size = fstatSync(fd).size;
|
|
57
|
+
const length = Math.min(bytes, size);
|
|
58
|
+
const start = fromEnd ? size - length : 0;
|
|
59
|
+
const buffer = Buffer.alloc(length);
|
|
60
|
+
const read = readSync(fd, buffer, 0, length, start);
|
|
61
|
+
const lines = buffer.subarray(0, read).toString("utf8").split(/\r?\n/);
|
|
62
|
+
// A window that starts or ends inside a line holds half of it, and half a line is no record.
|
|
63
|
+
if (fromEnd && start > 0) lines.shift();
|
|
64
|
+
if (!fromEnd && length < size) lines.pop();
|
|
65
|
+
return lines;
|
|
66
|
+
} catch {
|
|
67
|
+
return [];
|
|
68
|
+
} finally {
|
|
69
|
+
if (fd !== undefined) closeSync(fd);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** The usage of the first request in the head window, or null. */
|
|
74
|
+
export function firstUsage(file, bytes = WINDOW_BYTES) {
|
|
75
|
+
for (const line of windowLines(file, false, bytes)) {
|
|
76
|
+
const request = requestOf(line);
|
|
77
|
+
if (request) return request.usage;
|
|
78
|
+
}
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** The usage of the last request in the tail window, or null. */
|
|
83
|
+
export function lastUsage(file, bytes = WINDOW_BYTES) {
|
|
84
|
+
const lines = windowLines(file, true, bytes);
|
|
85
|
+
for (let index = lines.length - 1; index >= 0; index -= 1) {
|
|
86
|
+
const request = requestOf(lines[index]);
|
|
87
|
+
if (request) return request.usage;
|
|
88
|
+
}
|
|
89
|
+
return null;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** The text of the first user message in the head window, which for a subagent is its dispatch prompt, or null. */
|
|
93
|
+
export function firstPromptOf(file, bytes = WINDOW_BYTES) {
|
|
94
|
+
for (const line of windowLines(file, false, bytes)) {
|
|
95
|
+
let record;
|
|
96
|
+
try {
|
|
97
|
+
record = JSON.parse(line);
|
|
98
|
+
} catch {
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
if (record?.type !== "user" && record?.message?.role !== "user") continue;
|
|
102
|
+
const content = record.message?.content;
|
|
103
|
+
if (typeof content === "string") return content;
|
|
104
|
+
if (Array.isArray(content)) return content.map((block) => (typeof block?.text === "string" ? block.text : "")).join("\n");
|
|
105
|
+
return null;
|
|
106
|
+
}
|
|
107
|
+
return null;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** The transcript of subagent `agentId`, in the folder Claude Code keeps beside the main transcript. */
|
|
111
|
+
export const subagentTranscriptOf = (mainTranscript, agentId) =>
|
|
112
|
+
path.join(path.dirname(mainTranscript), path.basename(mainTranscript, ".jsonl"), "subagents", `agent-${agentId}.jsonl`);
|
|
113
|
+
|
|
114
|
+
/** The transcript of the subagent a hook call runs in, or null for the main session or a file not found. */
|
|
115
|
+
export function agentTranscriptOf(call) {
|
|
116
|
+
const agentId = call?.agent_id;
|
|
117
|
+
if (typeof agentId !== "string" || agentId === "") return null;
|
|
118
|
+
const own = call.agent_transcript_path;
|
|
119
|
+
if (typeof own === "string" && own !== "") return own;
|
|
120
|
+
const main = call.transcript_path;
|
|
121
|
+
if (typeof main !== "string" || main === "") return null;
|
|
122
|
+
if (path.basename(main) === `agent-${agentId}.jsonl`) return main;
|
|
123
|
+
const derived = subagentTranscriptOf(main, agentId);
|
|
124
|
+
return existsSync(derived) ? derived : null;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** The agent type in the `.meta.json` beside a subagent transcript, or null. */
|
|
128
|
+
export function agentTypeOf(transcript) {
|
|
129
|
+
try {
|
|
130
|
+
const meta = JSON.parse(readFileSync(transcript.replace(/\.jsonl$/, ".meta.json"), "utf8"));
|
|
131
|
+
return typeof meta?.agentType === "string" && meta.agentType !== "" ? meta.agentType : null;
|
|
132
|
+
} catch {
|
|
133
|
+
return null;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { strict as assert } from "node:assert";
|
|
2
|
+
import { mkdirSync, mkdtempSync, realpathSync, rmSync, writeFileSync } from "node:fs";
|
|
3
|
+
import os from "node:os";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { test } from "node:test";
|
|
6
|
+
import {
|
|
7
|
+
agentTranscriptOf,
|
|
8
|
+
agentTypeOf,
|
|
9
|
+
contextOf,
|
|
10
|
+
firstPromptOf,
|
|
11
|
+
firstUsage,
|
|
12
|
+
lastUsage,
|
|
13
|
+
readRequests,
|
|
14
|
+
requestOf,
|
|
15
|
+
subagentTranscriptOf,
|
|
16
|
+
} from "./transcript-usage.mjs";
|
|
17
|
+
|
|
18
|
+
function folder(t) {
|
|
19
|
+
const base = realpathSync.native(mkdtempSync(path.join(os.tmpdir(), "qc-transcript-")));
|
|
20
|
+
t.after(() => rmSync(base, { recursive: true, force: true }));
|
|
21
|
+
return base;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const usage = (read, write = 0, input = 2) => ({
|
|
25
|
+
input_tokens: input,
|
|
26
|
+
cache_read_input_tokens: read,
|
|
27
|
+
cache_creation_input_tokens: write,
|
|
28
|
+
output_tokens: 10,
|
|
29
|
+
});
|
|
30
|
+
const AT = "2026-01-01T00:00:00.000Z";
|
|
31
|
+
const assistant = (id, u, model = "claude-sonnet-5-5") => ({
|
|
32
|
+
type: "assistant",
|
|
33
|
+
timestamp: AT,
|
|
34
|
+
message: { id, model, role: "assistant", content: [{ type: "text", text: "words" }], usage: u },
|
|
35
|
+
});
|
|
36
|
+
const user = (content) => ({ type: "user", message: { role: "user", content } });
|
|
37
|
+
function write(file, records) {
|
|
38
|
+
mkdirSync(path.dirname(file), { recursive: true });
|
|
39
|
+
writeFileSync(file, `${records.map((r) => (typeof r === "string" ? r : JSON.stringify(r))).join("\n")}\n`);
|
|
40
|
+
return file;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
test("contextOf adds fresh input, cache reads, and cache writes, and a missing usage is zero", () => {
|
|
44
|
+
assert.equal(contextOf(usage(100, 20, 3)), 123);
|
|
45
|
+
assert.equal(contextOf(null), 0);
|
|
46
|
+
assert.equal(contextOf({}), 0);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
test("requestOf keeps an assistant request with usage and drops every other line", () => {
|
|
50
|
+
assert.deepEqual(requestOf(JSON.stringify(assistant("m1", usage(5)))), {
|
|
51
|
+
id: "m1",
|
|
52
|
+
model: "claude-sonnet-5-5",
|
|
53
|
+
at: Date.parse(AT),
|
|
54
|
+
usage: usage(5),
|
|
55
|
+
});
|
|
56
|
+
assert.equal(requestOf(JSON.stringify(user("hi"))), null);
|
|
57
|
+
assert.equal(requestOf(JSON.stringify(assistant("m2", usage(5), "<synthetic>"))), null);
|
|
58
|
+
assert.equal(requestOf('{"usage": broken'), null);
|
|
59
|
+
assert.equal(requestOf(""), null);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
test("readRequests counts one request once, though Claude Code writes a line per content block", (t) => {
|
|
63
|
+
const base = folder(t);
|
|
64
|
+
const file = write(path.join(base, "a.jsonl"), [
|
|
65
|
+
user("go"),
|
|
66
|
+
assistant("m1", usage(10)),
|
|
67
|
+
assistant("m1", usage(10)),
|
|
68
|
+
assistant("m2", usage(20)),
|
|
69
|
+
'{"partial": "usage',
|
|
70
|
+
]);
|
|
71
|
+
assert.deepEqual(readRequests(file).map((request) => request.id), ["m1", "m2"]);
|
|
72
|
+
assert.deepEqual(readRequests(path.join(base, "missing.jsonl")), []);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
test("firstUsage and lastUsage read one window at each end, and skip a line the window cuts", (t) => {
|
|
76
|
+
const base = folder(t);
|
|
77
|
+
const records = [
|
|
78
|
+
user("go"),
|
|
79
|
+
assistant("m1", usage(1000)),
|
|
80
|
+
...Array.from({ length: 200 }, (_, index) => assistant(`x${index}`, usage(5000 + index))),
|
|
81
|
+
assistant("last", usage(9999)),
|
|
82
|
+
];
|
|
83
|
+
const file = write(path.join(base, "a.jsonl"), records);
|
|
84
|
+
assert.equal(contextOf(firstUsage(file)), 1002);
|
|
85
|
+
assert.equal(contextOf(lastUsage(file)), 10001);
|
|
86
|
+
const lastLine = JSON.stringify(records.at(-1));
|
|
87
|
+
assert.equal(contextOf(lastUsage(file, lastLine.length + 10)), 10001);
|
|
88
|
+
assert.equal(lastUsage(file, 20), null);
|
|
89
|
+
const head = JSON.stringify(records[0]).length + 1 + JSON.stringify(records[1]).length + 5;
|
|
90
|
+
assert.equal(contextOf(firstUsage(file, head)), 1002);
|
|
91
|
+
assert.equal(firstUsage(file, 10), null);
|
|
92
|
+
assert.equal(firstUsage(path.join(base, "missing.jsonl")), null);
|
|
93
|
+
assert.equal(lastUsage(path.join(base, "missing.jsonl")), null);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
test("firstPromptOf gives the first user message as text, from a string or from text blocks", (t) => {
|
|
97
|
+
const base = folder(t);
|
|
98
|
+
assert.equal(firstPromptOf(write(path.join(base, "a.jsonl"), [user("Brief: b.md\nEstimate: 12"), assistant("m1", usage(1))])), "Brief: b.md\nEstimate: 12");
|
|
99
|
+
const blocks = user([{ type: "text", text: "one" }, { type: "image" }, { type: "text", text: "two" }]);
|
|
100
|
+
assert.equal(firstPromptOf(write(path.join(base, "b.jsonl"), [assistant("m1", usage(1)), blocks])), "one\n\ntwo");
|
|
101
|
+
assert.equal(firstPromptOf(write(path.join(base, "c.jsonl"), [assistant("m1", usage(1))])), null);
|
|
102
|
+
assert.equal(firstPromptOf(path.join(base, "missing.jsonl")), null);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
test("a subagent's transcript is found however the hook input names it", (t) => {
|
|
106
|
+
const base = folder(t);
|
|
107
|
+
const main = path.join(base, "proj", "sess.jsonl");
|
|
108
|
+
const own = subagentTranscriptOf(main, "a1");
|
|
109
|
+
assert.equal(own, path.join(base, "proj", "sess", "subagents", "agent-a1.jsonl"));
|
|
110
|
+
write(main, [user("hi")]);
|
|
111
|
+
assert.equal(agentTranscriptOf({ agent_id: "a1", transcript_path: main }), null, "no file yet");
|
|
112
|
+
write(own, [user("go")]);
|
|
113
|
+
assert.equal(agentTranscriptOf({ agent_id: "a1", transcript_path: main }), own);
|
|
114
|
+
assert.equal(agentTranscriptOf({ agent_id: "a1", transcript_path: own }), own);
|
|
115
|
+
assert.equal(
|
|
116
|
+
agentTranscriptOf({ agent_id: "a1", agent_transcript_path: "/x/agent-a1.jsonl", transcript_path: main }),
|
|
117
|
+
"/x/agent-a1.jsonl",
|
|
118
|
+
);
|
|
119
|
+
assert.equal(agentTranscriptOf({ transcript_path: main }), null, "the main session has no agent id");
|
|
120
|
+
assert.equal(agentTranscriptOf({ agent_id: "a1" }), null);
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
test("agentTypeOf reads the type from the meta file beside a transcript", (t) => {
|
|
124
|
+
const base = folder(t);
|
|
125
|
+
const file = write(path.join(base, "agent-a1.jsonl"), [user("go")]);
|
|
126
|
+
assert.equal(agentTypeOf(file), null);
|
|
127
|
+
writeFileSync(path.join(base, "agent-a1.meta.json"), JSON.stringify({ agentType: "architecture:sdd-reviewer" }));
|
|
128
|
+
assert.equal(agentTypeOf(file), "architecture:sdd-reviewer");
|
|
129
|
+
});
|
package/src/config.mjs
CHANGED
|
@@ -515,8 +515,22 @@ export const exploreDefaults = {
|
|
|
515
515
|
summarizer: null,
|
|
516
516
|
maxReadLines: 300,
|
|
517
517
|
maxGrepLines: 80,
|
|
518
|
-
maxOutputChars:
|
|
518
|
+
maxOutputChars: 10000,
|
|
519
519
|
exempt: [],
|
|
520
|
+
// "excerpt" saves a long successful command output to a file and shows its head and tail; "hint" only names the summarizer.
|
|
521
|
+
longOutput: "excerpt",
|
|
522
|
+
excerptChars: 4000,
|
|
523
|
+
};
|
|
524
|
+
|
|
525
|
+
// What each key of `swarm.context` means when a repository omits it. The signal is on wherever the config
|
|
526
|
+
// is, and `"context": false` turns it off. Each ratio compares an agent's latest request with its first.
|
|
527
|
+
export const contextDefaults = {
|
|
528
|
+
every: 5,
|
|
529
|
+
summarizeRatio: 3,
|
|
530
|
+
handoffRatio: 5,
|
|
531
|
+
minCallsLeft: 15,
|
|
532
|
+
repeatEvery: 20,
|
|
533
|
+
handoffTypes: ["sdd-implementer", "architecture:sdd-implementer", "general-purpose"],
|
|
520
534
|
};
|
|
521
535
|
|
|
522
536
|
// What each key of `swarm.review` means when a repository omits it. The workflow checks run only while
|