cc-hindsight 1.1.0 → 1.2.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/dist/cli.js +297 -43
- package/npm-shrinkwrap.json +2 -2
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -10,7 +10,7 @@ import { stripVTControlCharacters, styleText } from "node:util";
|
|
|
10
10
|
import readline from "node:readline";
|
|
11
11
|
import { spinner } from "@clack/prompts";
|
|
12
12
|
//#region package.json
|
|
13
|
-
var version = "1.
|
|
13
|
+
var version = "1.2.0";
|
|
14
14
|
var description = "Mine your coding-agent session history (Claude Code, kiro-cli) into a oneshot prompt library and CLAUDE.md/AGENTS.md preferences. The prompt you'd write if you knew then what you know now.";
|
|
15
15
|
//#endregion
|
|
16
16
|
//#region src/commands/_shared.ts
|
|
@@ -317,27 +317,49 @@ const defaultSpawn$1 = (cmd, args, input) => new Promise((resolve) => {
|
|
|
317
317
|
"ignore",
|
|
318
318
|
"ignore"
|
|
319
319
|
] });
|
|
320
|
-
|
|
320
|
+
let settled = false;
|
|
321
|
+
const settle = (v) => {
|
|
322
|
+
if (settled) return;
|
|
323
|
+
settled = true;
|
|
324
|
+
resolve(v);
|
|
325
|
+
};
|
|
326
|
+
child.on("error", (err) => settle({
|
|
321
327
|
code: null,
|
|
322
328
|
error: err.message
|
|
323
329
|
}));
|
|
324
|
-
child.on("close", (code) =>
|
|
330
|
+
child.on("close", (code) => settle({ code }));
|
|
331
|
+
child.on("exit", (code) => settle({ code }));
|
|
332
|
+
child.stdin?.on("error", () => {});
|
|
325
333
|
child.stdin?.write(input);
|
|
326
334
|
child.stdin?.end();
|
|
327
335
|
});
|
|
328
336
|
/** Copy text to the system clipboard. Reports which tool was used, or why not. */
|
|
329
337
|
async function copyToClipboard(text, io = {}) {
|
|
330
338
|
const { cmd, args } = clipboardCommand(io.platform, io.env);
|
|
331
|
-
const
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
};
|
|
339
|
+
const spawn = io.spawn ?? defaultSpawn$1;
|
|
340
|
+
const timeoutMs = io.timeoutMs ?? 5e3;
|
|
341
|
+
let timer;
|
|
342
|
+
const timeout = new Promise((resolve) => {
|
|
343
|
+
timer = setTimeout(() => resolve({
|
|
344
|
+
code: null,
|
|
345
|
+
error: `${cmd} timed out after ${timeoutMs}ms`
|
|
346
|
+
}), timeoutMs);
|
|
347
|
+
timer.unref?.();
|
|
348
|
+
});
|
|
349
|
+
try {
|
|
350
|
+
const result = await Promise.race([spawn(cmd, args, text), timeout]);
|
|
351
|
+
if (result.code === 0) return {
|
|
352
|
+
ok: true,
|
|
353
|
+
tool: cmd
|
|
354
|
+
};
|
|
355
|
+
return {
|
|
356
|
+
ok: false,
|
|
357
|
+
tool: cmd,
|
|
358
|
+
error: result.error ?? `${cmd} exited with code ${result.code}`
|
|
359
|
+
};
|
|
360
|
+
} finally {
|
|
361
|
+
if (timer) clearTimeout(timer);
|
|
362
|
+
}
|
|
341
363
|
}
|
|
342
364
|
//#endregion
|
|
343
365
|
//#region src/ui/style.ts
|
|
@@ -533,6 +555,11 @@ function renderPlan(plan) {
|
|
|
533
555
|
}
|
|
534
556
|
function ask(question, input, output) {
|
|
535
557
|
if (input.readableEnded) return Promise.resolve("");
|
|
558
|
+
let raw = "";
|
|
559
|
+
const tap = (chunk) => {
|
|
560
|
+
raw += chunk.toString();
|
|
561
|
+
};
|
|
562
|
+
input.on("data", tap);
|
|
536
563
|
const rl = readline.createInterface({
|
|
537
564
|
input,
|
|
538
565
|
output
|
|
@@ -542,11 +569,12 @@ function ask(question, input, output) {
|
|
|
542
569
|
const done = (answer) => {
|
|
543
570
|
if (answered) return;
|
|
544
571
|
answered = true;
|
|
572
|
+
input.off("data", tap);
|
|
545
573
|
rl.close();
|
|
546
574
|
resolve(answer);
|
|
547
575
|
};
|
|
548
576
|
rl.question(question, done);
|
|
549
|
-
rl.on("close", () => done(""));
|
|
577
|
+
rl.on("close", () => done(raw.split(/\r?\n/)[0] ?? ""));
|
|
550
578
|
});
|
|
551
579
|
}
|
|
552
580
|
/**
|
|
@@ -1173,11 +1201,10 @@ function defaultSpawn(bin, args, opts) {
|
|
|
1173
1201
|
child.stderr?.on("data", (d) => {
|
|
1174
1202
|
stderr += d.toString();
|
|
1175
1203
|
});
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
child.on("close", (code, signal) => {
|
|
1204
|
+
let settled = false;
|
|
1205
|
+
const settle = (code, signal) => {
|
|
1206
|
+
if (settled) return;
|
|
1207
|
+
settled = true;
|
|
1181
1208
|
clearTimers();
|
|
1182
1209
|
resolve({
|
|
1183
1210
|
code,
|
|
@@ -1186,6 +1213,17 @@ function defaultSpawn(bin, args, opts) {
|
|
|
1186
1213
|
stderr,
|
|
1187
1214
|
timedOut
|
|
1188
1215
|
});
|
|
1216
|
+
};
|
|
1217
|
+
child.on("error", (err) => {
|
|
1218
|
+
if (settled) return;
|
|
1219
|
+
settled = true;
|
|
1220
|
+
clearTimers();
|
|
1221
|
+
reject(err);
|
|
1222
|
+
});
|
|
1223
|
+
child.on("close", (code, signal) => settle(code, signal));
|
|
1224
|
+
child.on("exit", (code, signal) => {
|
|
1225
|
+
if (settled) return;
|
|
1226
|
+
setImmediate(() => settle(code, signal));
|
|
1189
1227
|
});
|
|
1190
1228
|
child.stdin?.on("error", () => {});
|
|
1191
1229
|
child.stdin?.write(opts.input);
|
|
@@ -1202,11 +1240,27 @@ function snippet$2(s, max = 500) {
|
|
|
1202
1240
|
const t = s.trim();
|
|
1203
1241
|
return t.length > max ? `${t.slice(0, max)}…` : t;
|
|
1204
1242
|
}
|
|
1205
|
-
/**
|
|
1243
|
+
/**
|
|
1244
|
+
* Strip a Markdown code fence if the model wrapped its JSON in one.
|
|
1245
|
+
*
|
|
1246
|
+
* This is the last line of defense on the schema-in-prompt path (no
|
|
1247
|
+
* `--json-schema`, and the kiro runner always): the model's raw text is all we
|
|
1248
|
+
* get, and a fence we fail to strip becomes a JSON parse error that burns the
|
|
1249
|
+
* one corrective retry. Verified against the live CLI on that path: the model
|
|
1250
|
+
* really does answer with a ```json fence even when told to respond ONLY with
|
|
1251
|
+
* JSON, so the neighboring shapes are worth tolerating too.
|
|
1252
|
+
*
|
|
1253
|
+
* The anchored form only matched a fence that was the WHOLE string, so a single
|
|
1254
|
+
* "Here you go:" preamble or "Hope that helps!" sign-off defeated it, as did an
|
|
1255
|
+
* uppercase ```JSON tag (which got captured into the body). Now: find the first
|
|
1256
|
+
* fenced block anywhere in the text, case-insensitively; fall back to the
|
|
1257
|
+
* trimmed text so non-fenced input and unterminated fences pass through
|
|
1258
|
+
* unchanged and the caller can report the real content in its snippet.
|
|
1259
|
+
*/
|
|
1206
1260
|
function stripFence(text) {
|
|
1207
1261
|
const trimmed = text.trim();
|
|
1208
|
-
const
|
|
1209
|
-
return
|
|
1262
|
+
const body = trimmed.match(/```[a-z]*[ \t]*\r?\n?([\s\S]*?)\r?\n?```/i)?.[1]?.trim();
|
|
1263
|
+
return body ? body : trimmed;
|
|
1210
1264
|
}
|
|
1211
1265
|
/**
|
|
1212
1266
|
* Append the JSON schema to a prompt for backends that cannot validate
|
|
@@ -1251,10 +1305,12 @@ async function runWithCorrectiveRetry(basePrompt, schema, attemptFn, makeExhaust
|
|
|
1251
1305
|
* runners/shared.ts. A shim at src/claude/runner.ts re-exports both halves,
|
|
1252
1306
|
* including a `ClaudeRunnerError` class alias so `instanceof` keeps working.)
|
|
1253
1307
|
*
|
|
1254
|
-
* Verified against Claude Code CLI 2.1.207 (macOS):
|
|
1308
|
+
* Verified against Claude Code CLI 2.1.207 (macOS) and 2.1.220 (Linux):
|
|
1255
1309
|
* - `claude -p --output-format json` → single JSON envelope on stdout
|
|
1256
1310
|
* ({ type, subtype, is_error, result, session_id, total_cost_usd, usage, ... });
|
|
1257
|
-
* `result` carries the model's text output.
|
|
1311
|
+
* `result` carries the model's text output. With verbose mode ON stdout is
|
|
1312
|
+
* instead a JSON ARRAY of stream events terminated by the `type: "result"`
|
|
1313
|
+
* event; both shapes are accepted (see `selectEnvelope`).
|
|
1258
1314
|
* - `--json-schema <json>` → server-side structured-output validation.
|
|
1259
1315
|
* - `--tools ""` → disable all built-in tools.
|
|
1260
1316
|
* - `--model <name>` → model pass-through.
|
|
@@ -1265,8 +1321,44 @@ async function runWithCorrectiveRetry(basePrompt, schema, attemptFn, makeExhaust
|
|
|
1265
1321
|
const CLAUDE_INSTALL_HINT = "`claude` CLI not found on PATH. Install it with `npm install -g @anthropic-ai/claude-code` (docs: https://docs.claude.com/en/docs/claude-code). Deterministic commands (scan/export) work without it — only distill needs claude.";
|
|
1266
1322
|
let capabilitiesCache = null;
|
|
1267
1323
|
/**
|
|
1324
|
+
* Fallback help flag, tried when the first `--help` looks like a WRAPPER's own
|
|
1325
|
+
* help rather than the native binary's. A distribution wrapper (one observed in
|
|
1326
|
+
* the wild resolves credentials and model routing, then execs the native binary)
|
|
1327
|
+
* documents only its own options and forwards everything else through; this flag
|
|
1328
|
+
* is its documented escape hatch to the real help.
|
|
1329
|
+
*/
|
|
1330
|
+
const NATIVE_HELP_FLAG = "--claude-help";
|
|
1331
|
+
/** Derive the capability flags from a `--help` text. */
|
|
1332
|
+
function parseCapabilities(text) {
|
|
1333
|
+
const disableTools = text.includes("--tools") ? "tools-empty" : /--disallowed-?[tT]ools/.test(text) ? "disallowed" : "none";
|
|
1334
|
+
return {
|
|
1335
|
+
jsonSchema: text.includes("--json-schema"),
|
|
1336
|
+
disableTools
|
|
1337
|
+
};
|
|
1338
|
+
}
|
|
1339
|
+
/**
|
|
1340
|
+
* Does this help text plausibly come from the native Claude Code binary?
|
|
1341
|
+
*
|
|
1342
|
+
* `--output-format` is the signal we key on: distill cannot work without that
|
|
1343
|
+
* flag, so any native help that matters here documents it. Its absence means we
|
|
1344
|
+
* are reading someone else's help text (a wrapper's), and the capability answers
|
|
1345
|
+
* drawn from it are not about the binary we will actually invoke.
|
|
1346
|
+
*/
|
|
1347
|
+
function looksLikeNativeHelp(text) {
|
|
1348
|
+
return text.includes("--output-format");
|
|
1349
|
+
}
|
|
1350
|
+
/**
|
|
1268
1351
|
* Probe the installed CLI's `--help` once and cache the result. `--help` is a
|
|
1269
1352
|
* local, deterministic, no-API invocation, so this never costs anything.
|
|
1353
|
+
*
|
|
1354
|
+
* A wrapper distribution can HIDE natively-supported flags from `--help` while
|
|
1355
|
+
* still forwarding them (observed in the wild: a wrapper whose `--help` lists 5
|
|
1356
|
+
* wrapper options and advertises neither `--json-schema` nor `--tools`, though
|
|
1357
|
+
* both work; the native 69-flag help is behind `--claude-help`). Probing only
|
|
1358
|
+
* the wrapper help silently degrades EVERY distill stage to the prompt-embedded
|
|
1359
|
+
* schema with tools disabled by instruction alone. So when the help does not
|
|
1360
|
+
* look native, re-probe once via {@link NATIVE_HELP_FLAG} and prefer that
|
|
1361
|
+
* answer if it finds more. Worst case this costs one extra local `--help`.
|
|
1270
1362
|
*/
|
|
1271
1363
|
async function probeCapabilities(bin, io) {
|
|
1272
1364
|
if (capabilitiesCache) return capabilitiesCache;
|
|
@@ -1275,23 +1367,51 @@ async function probeCapabilities(bin, io) {
|
|
|
1275
1367
|
timeoutMs: 15e3
|
|
1276
1368
|
});
|
|
1277
1369
|
const text = `${help.stdout}\n${help.stderr}`;
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1370
|
+
let caps = parseCapabilities(text);
|
|
1371
|
+
if (!looksLikeNativeHelp(text)) try {
|
|
1372
|
+
const native = await io.spawn(bin, [NATIVE_HELP_FLAG], {
|
|
1373
|
+
input: "",
|
|
1374
|
+
timeoutMs: 15e3
|
|
1375
|
+
});
|
|
1376
|
+
const nativeText = `${native.stdout}\n${native.stderr}`;
|
|
1377
|
+
if (looksLikeNativeHelp(nativeText)) caps = parseCapabilities(nativeText);
|
|
1378
|
+
} catch {}
|
|
1283
1379
|
capabilitiesCache = caps;
|
|
1284
1380
|
return caps;
|
|
1285
1381
|
}
|
|
1286
1382
|
/**
|
|
1383
|
+
* Reduce a verbose-mode payload to the single envelope carrying the result.
|
|
1384
|
+
*
|
|
1385
|
+
* With verbose mode ON (`--verbose`, or `"verbose": true` in settings.json)
|
|
1386
|
+
* `claude -p --output-format json` emits a JSON ARRAY of stream events
|
|
1387
|
+
* (`system`/init, `assistant`, …) terminated by the `type: "result"` event,
|
|
1388
|
+
* instead of the single result object it emits when verbose is off. Only the
|
|
1389
|
+
* terminal event has `result`/`is_error`, so reading the array as an object
|
|
1390
|
+
* finds no `result` field and every call fails. The CLI has no `--no-verbose`
|
|
1391
|
+
* to force the object shape, so both shapes must be accepted.
|
|
1392
|
+
*
|
|
1393
|
+
* The result event is located by its `type` field (its key ORDER is not stable:
|
|
1394
|
+
* on a real 2.1.220 run `type` is the 19th key), with a positional
|
|
1395
|
+
* last-element fallback for a future stream that renames the type tag.
|
|
1396
|
+
*/
|
|
1397
|
+
function selectEnvelope(parsed) {
|
|
1398
|
+
if (!Array.isArray(parsed)) return parsed;
|
|
1399
|
+
const events = parsed;
|
|
1400
|
+
const resultEvent = events.findLast((e) => e?.type === "result");
|
|
1401
|
+
if (resultEvent) return resultEvent;
|
|
1402
|
+
const last = events.at(-1);
|
|
1403
|
+
if (last && (Object.hasOwn(last, "result") || Object.hasOwn(last, "is_error"))) return last;
|
|
1404
|
+
return null;
|
|
1405
|
+
}
|
|
1406
|
+
/**
|
|
1287
1407
|
* Parse the CLI envelope and pull out the structured result. Throws
|
|
1288
1408
|
* {@link AgentRunnerError} for fatal CLI errors (no point retrying) and
|
|
1289
1409
|
* {@link RetryableError} for parse/validation problems (retry may help).
|
|
1290
1410
|
*/
|
|
1291
1411
|
function extractResult(result) {
|
|
1292
|
-
let
|
|
1412
|
+
let parsed;
|
|
1293
1413
|
try {
|
|
1294
|
-
|
|
1414
|
+
parsed = JSON.parse(result.stdout);
|
|
1295
1415
|
} catch {
|
|
1296
1416
|
const detail = snippet$2(result.stdout || result.stderr) || "no output";
|
|
1297
1417
|
if (result.code !== 0) throw new AgentRunnerError("cli-error", `claude exited ${result.code}: ${detail}`, {
|
|
@@ -1300,12 +1420,14 @@ function extractResult(result) {
|
|
|
1300
1420
|
});
|
|
1301
1421
|
throw new RetryableError(`could not parse claude JSON envelope: ${detail}`);
|
|
1302
1422
|
}
|
|
1423
|
+
const envelope = selectEnvelope(parsed);
|
|
1424
|
+
if (!envelope) throw new RetryableError(`claude verbose stream carried no 'result' event: ${snippet$2(result.stdout) || "empty array"}`);
|
|
1303
1425
|
if (envelope.is_error) throw new AgentRunnerError("cli-error", `claude reported an error: ${typeof envelope.result === "string" ? envelope.result : "unknown error"}`, {
|
|
1304
1426
|
stderr: result.stderr,
|
|
1305
1427
|
output: result.stdout
|
|
1306
1428
|
});
|
|
1307
1429
|
const r = envelope.result;
|
|
1308
|
-
if (r === null || r === void 0) throw new RetryableError(
|
|
1430
|
+
if (r === null || r === void 0) throw new RetryableError(`claude envelope missing a 'result' field: ${snippet$2(result.stdout, 200) || "no output"}`);
|
|
1309
1431
|
if (typeof r === "object") return r;
|
|
1310
1432
|
if (typeof r === "string") {
|
|
1311
1433
|
const jsonText = stripFence(r);
|
|
@@ -1510,6 +1632,12 @@ async function runDigestStage(opts) {
|
|
|
1510
1632
|
let completed = 0;
|
|
1511
1633
|
let skipped = 0;
|
|
1512
1634
|
const total = opts.entries.length;
|
|
1635
|
+
const breakerEnabled = opts.breaker !== false;
|
|
1636
|
+
let streakMessage = null;
|
|
1637
|
+
let streakCount = 0;
|
|
1638
|
+
let aborted = false;
|
|
1639
|
+
let abortReason;
|
|
1640
|
+
const attempted = /* @__PURE__ */ new Set();
|
|
1513
1641
|
/** Digest one session end-to-end (skip / read / budget / call / checkpoint). */
|
|
1514
1642
|
const digestOne = async (entry, i) => {
|
|
1515
1643
|
const label = `[${i + 1}/${total}] ${entry.export}`;
|
|
@@ -1559,6 +1687,8 @@ async function runDigestStage(opts) {
|
|
|
1559
1687
|
checkpoint.digests[entry.export] = digest;
|
|
1560
1688
|
saveDigests(opts.home, checkpoint);
|
|
1561
1689
|
completed++;
|
|
1690
|
+
streakMessage = null;
|
|
1691
|
+
streakCount = 0;
|
|
1562
1692
|
write(` ${label} → ${green(digest.outcome)}: ${truncate(digest.goal, 70)}`);
|
|
1563
1693
|
} catch (err) {
|
|
1564
1694
|
const message = err instanceof Error ? err.message : String(err);
|
|
@@ -1567,23 +1697,47 @@ async function runDigestStage(opts) {
|
|
|
1567
1697
|
error: message
|
|
1568
1698
|
});
|
|
1569
1699
|
write(` ${label} ${fail(`failed: ${truncate(message, 120)}`)}`);
|
|
1700
|
+
if (message === streakMessage) streakCount++;
|
|
1701
|
+
else {
|
|
1702
|
+
streakMessage = message;
|
|
1703
|
+
streakCount = 1;
|
|
1704
|
+
}
|
|
1705
|
+
if (breakerEnabled && !aborted && streakCount >= 5 && total > 5) {
|
|
1706
|
+
aborted = true;
|
|
1707
|
+
abortReason = `${streakCount} consecutive sessions failed with the identical error, which points at the environment rather than these sessions. Stopping before spending more calls`;
|
|
1708
|
+
write("");
|
|
1709
|
+
write(fail(` ⚠ ${abortReason}:`));
|
|
1710
|
+
write(` ${truncate(message, 160)}`);
|
|
1711
|
+
write(dim(" Nothing already digested is lost; fix the cause and re-run to resume.\n Re-run with --no-breaker to attempt every session anyway."));
|
|
1712
|
+
}
|
|
1570
1713
|
}
|
|
1571
1714
|
};
|
|
1572
1715
|
let nextIndex = 0;
|
|
1573
1716
|
const worker = async () => {
|
|
1574
1717
|
while (nextIndex < opts.entries.length) {
|
|
1718
|
+
if (aborted) return;
|
|
1575
1719
|
const i = nextIndex++;
|
|
1576
1720
|
const entry = opts.entries[i];
|
|
1577
|
-
if (entry)
|
|
1721
|
+
if (entry) {
|
|
1722
|
+
attempted.add(entry.export);
|
|
1723
|
+
await digestOne(entry, i);
|
|
1724
|
+
}
|
|
1578
1725
|
}
|
|
1579
1726
|
};
|
|
1580
1727
|
await Promise.all(Array.from({ length: Math.min(concurrency, Math.max(total, 1)) }, () => worker()));
|
|
1728
|
+
const notAttempted = opts.entries.map((e) => e.export).filter((name) => !attempted.has(name));
|
|
1729
|
+
if (aborted && notAttempted.length > 0) write(dim(` ${notAttempted.length} session(s) not attempted.`));
|
|
1581
1730
|
return {
|
|
1582
1731
|
generation: checkpoint.generation,
|
|
1583
1732
|
completed,
|
|
1584
1733
|
skipped,
|
|
1585
1734
|
failed,
|
|
1586
|
-
blocked
|
|
1735
|
+
blocked,
|
|
1736
|
+
notAttempted,
|
|
1737
|
+
...aborted ? {
|
|
1738
|
+
aborted,
|
|
1739
|
+
abortReason
|
|
1740
|
+
} : {}
|
|
1587
1741
|
};
|
|
1588
1742
|
}
|
|
1589
1743
|
function tasksPath(home) {
|
|
@@ -2014,9 +2168,65 @@ const KIRO_DISTILL_SENTINEL = "[cc-hindsight distill]";
|
|
|
2014
2168
|
const BRACKET_MARKERS = [
|
|
2015
2169
|
"[AGENT SYSTEM PROMPT]",
|
|
2016
2170
|
"[Recent channel messages for context:]",
|
|
2017
|
-
"[Subagent completion event]"
|
|
2171
|
+
"[Subagent completion event]",
|
|
2172
|
+
"[LIVE STEERING - New message from user]"
|
|
2018
2173
|
];
|
|
2019
2174
|
/**
|
|
2175
|
+
* K14 — live-steering recovery. A message the human types WHILE the agent is
|
|
2176
|
+
* working arrives wrapped in a harness envelope, not as its own Prompt:
|
|
2177
|
+
*
|
|
2178
|
+
* [LIVE STEERING - New message from user]
|
|
2179
|
+
* The user sent a new message while you are working. …
|
|
2180
|
+
* <user_message id="steer-…">
|
|
2181
|
+
* the words the human actually typed
|
|
2182
|
+
* </user_message>
|
|
2183
|
+
* IMPORTANT: After completing your work, … [STEERING steer-…: …]
|
|
2184
|
+
*
|
|
2185
|
+
* The envelope leads with a bracket marker, so K6 would drop the whole thing and
|
|
2186
|
+
* the human's words with it. Recover the `<user_message>` body FIRST (the same
|
|
2187
|
+
* ordering trick R10 uses for slash-commands on the Claude side), and keep the
|
|
2188
|
+
* surrounding harness instructions out: they are machine text and would
|
|
2189
|
+
* otherwise dominate the digest prompt.
|
|
2190
|
+
*
|
|
2191
|
+
* These are the highest-value turns in a corpus, since steering is where the
|
|
2192
|
+
* human corrects course mid-run. Measured on a real 306-session store: 112
|
|
2193
|
+
* messages, ~13.6k chars.
|
|
2194
|
+
*/
|
|
2195
|
+
const LIVE_STEERING_MARKER = "[LIVE STEERING - New message from user]";
|
|
2196
|
+
const USER_MESSAGE_RE = /<user_message\b[^>]*>([\s\S]*?)<\/user_message>/;
|
|
2197
|
+
/** Pull the human words out of a live-steering envelope; null if there are none. */
|
|
2198
|
+
function recoverSteeringText(text) {
|
|
2199
|
+
if (!text.startsWith(LIVE_STEERING_MARKER)) return null;
|
|
2200
|
+
const body = USER_MESSAGE_RE.exec(text)?.[1]?.trim();
|
|
2201
|
+
return body ? body : null;
|
|
2202
|
+
}
|
|
2203
|
+
/**
|
|
2204
|
+
* K14 — steering messages recovered from a `ToolResults` entry.
|
|
2205
|
+
*
|
|
2206
|
+
* In real sessions the harness delivers a mid-run steering message by appending
|
|
2207
|
+
* it to the NEXT `ToolResults` entry's `content`, not as its own `Prompt`. Since
|
|
2208
|
+
* K1 only admits `Prompt`, all of them were invisible: on a real 306-session
|
|
2209
|
+
* store, 100 of the 112 steering messages arrive this way (12 more only in a
|
|
2210
|
+
* `Compaction` snapshot, which K12 already reports as a drop).
|
|
2211
|
+
*
|
|
2212
|
+
* Only `kind: "text"` blocks whose text opens with the steering marker are
|
|
2213
|
+
* admitted. That is deliberately narrow: the same content arrays hold 5630
|
|
2214
|
+
* `toolResult` blocks of pure machine output, and nothing else in a
|
|
2215
|
+
* `ToolResults` entry is human. Shared by extract and timeline so the
|
|
2216
|
+
* SessionSource law holds.
|
|
2217
|
+
*/
|
|
2218
|
+
function steeringFromToolResults(data) {
|
|
2219
|
+
const content = isRecord$1(data) ? data.content : void 0;
|
|
2220
|
+
if (!Array.isArray(content)) return [];
|
|
2221
|
+
const out = [];
|
|
2222
|
+
for (const block of content) {
|
|
2223
|
+
if (!isRecord$1(block) || block.kind !== "text" || typeof block.data !== "string") continue;
|
|
2224
|
+
const recovered = recoverSteeringText(block.data.trim());
|
|
2225
|
+
if (recovered !== null) out.push(recovered);
|
|
2226
|
+
}
|
|
2227
|
+
return out;
|
|
2228
|
+
}
|
|
2229
|
+
/**
|
|
2020
2230
|
* K6 / K2 — harness nudge strings. As a leading substring they mark a
|
|
2021
2231
|
* machine-injected retry prompt (drop the piece); as a whole-session's only
|
|
2022
2232
|
* "human-looking" prompt they are the sole false-positive class the `.history`
|
|
@@ -2094,6 +2304,11 @@ function promptText(data, drops, ts) {
|
|
|
2094
2304
|
});
|
|
2095
2305
|
continue;
|
|
2096
2306
|
}
|
|
2307
|
+
const steered = recoverSteeringText(text);
|
|
2308
|
+
if (steered !== null) {
|
|
2309
|
+
pieces.push(steered);
|
|
2310
|
+
continue;
|
|
2311
|
+
}
|
|
2097
2312
|
if (text.startsWith("<")) {
|
|
2098
2313
|
drops.push({
|
|
2099
2314
|
reason: "K6: machine block (leading <)",
|
|
@@ -2177,6 +2392,17 @@ function extractKiroMessages(lines) {
|
|
|
2177
2392
|
}
|
|
2178
2393
|
continue;
|
|
2179
2394
|
}
|
|
2395
|
+
if (parsed.kind === "ToolResults") {
|
|
2396
|
+
const ts = normalizeTimestamp(isRecord$1(data) ? data.meta : void 0);
|
|
2397
|
+
for (const steered of steeringFromToolResults(data)) {
|
|
2398
|
+
messages.push({
|
|
2399
|
+
timestamp: ts,
|
|
2400
|
+
text: steered
|
|
2401
|
+
});
|
|
2402
|
+
liveKeys.add(`${ts}\u0000${steered}`);
|
|
2403
|
+
}
|
|
2404
|
+
continue;
|
|
2405
|
+
}
|
|
2180
2406
|
if (parsed.kind !== "Prompt") continue;
|
|
2181
2407
|
const ts = normalizeTimestamp(isRecord$1(data) ? data.meta : void 0);
|
|
2182
2408
|
const text = promptText(data, drops, ts);
|
|
@@ -2236,6 +2462,13 @@ function kiroTimeline(lines) {
|
|
|
2236
2462
|
timestamp: "",
|
|
2237
2463
|
text
|
|
2238
2464
|
});
|
|
2465
|
+
} else if (kind === "ToolResults") {
|
|
2466
|
+
const ts = normalizeTimestamp(isRecord$1(data) ? data.meta : void 0);
|
|
2467
|
+
for (const steered of steeringFromToolResults(data)) events.push({
|
|
2468
|
+
kind: "human",
|
|
2469
|
+
timestamp: ts,
|
|
2470
|
+
text: steered
|
|
2471
|
+
});
|
|
2239
2472
|
} else if (kind === "Clear" || kind === "Compaction") events.push({
|
|
2240
2473
|
kind: "boundary",
|
|
2241
2474
|
timestamp: "",
|
|
@@ -2883,20 +3116,31 @@ function rejectReason(entry) {
|
|
|
2883
3116
|
return null;
|
|
2884
3117
|
}
|
|
2885
3118
|
/**
|
|
2886
|
-
* R3 — attachments hold messages typed while the agent was busy.
|
|
2887
|
-
* (pinned by r3-queued-command.jsonl
|
|
3119
|
+
* R3 — attachments hold messages typed while the agent was busy. Real shape
|
|
3120
|
+
* (pinned by r3-queued-command-prompt-field.jsonl, copied from a live
|
|
3121
|
+
* transcript):
|
|
2888
3122
|
* { "type": "attachment",
|
|
2889
3123
|
* "attachment": { "type": "queued_command",
|
|
2890
3124
|
* "origin": { "kind": "human" },
|
|
2891
3125
|
* "commandMode": "prompt",
|
|
2892
|
-
* "
|
|
3126
|
+
* "prompt": "the queued prompt text",
|
|
3127
|
+
* "timestamp": "..." } }
|
|
2893
3128
|
* Admit ONLY queued_command + origin.kind==="human" + commandMode==="prompt";
|
|
2894
|
-
* anything else is recorded as a Drop for observability.
|
|
3129
|
+
* anything else is recorded as a Drop for observability. In particular
|
|
3130
|
+
* `commandMode: "task-notification"` is the agent reporting a finished
|
|
3131
|
+
* background task, not typed input.
|
|
3132
|
+
*
|
|
3133
|
+
* The text lives in `prompt`. An earlier version read `text`, which no shipped
|
|
3134
|
+
* CLI writes here: the original fixture was hand-authored with that spelling, so
|
|
3135
|
+
* the suite pinned a shape that never existed and R3 dropped EVERY genuinely
|
|
3136
|
+
* queued human message (measured on a real 286-session store: 253 messages,
|
|
3137
|
+
* ~42.9k chars). `text` is still read as a fallback so the older fixture's
|
|
3138
|
+
* contract holds and any variant CLI that uses it keeps working.
|
|
2895
3139
|
*/
|
|
2896
3140
|
function collectAttachment(entry, pieces, drops, ts) {
|
|
2897
3141
|
const att = entry.attachment;
|
|
2898
3142
|
if (isRecord(att) && att.type === "queued_command" && isRecord(att.origin) && att.origin.kind === "human" && att.commandMode === "prompt") {
|
|
2899
|
-
cleanPiece(typeof att.text === "string" ? att.text : "", pieces, drops, ts);
|
|
3143
|
+
cleanPiece(typeof att.prompt === "string" ? att.prompt : typeof att.text === "string" ? att.text : "", pieces, drops, ts);
|
|
2900
3144
|
return;
|
|
2901
3145
|
}
|
|
2902
3146
|
drops.push({
|
|
@@ -2947,7 +3191,11 @@ function entrySnippet(entry) {
|
|
|
2947
3191
|
}
|
|
2948
3192
|
}
|
|
2949
3193
|
const att = entry.attachment;
|
|
2950
|
-
if (isRecord(att)
|
|
3194
|
+
if (isRecord(att)) {
|
|
3195
|
+
if (typeof att.prompt === "string") return snippet(att.prompt);
|
|
3196
|
+
if (typeof att.text === "string") return snippet(att.text);
|
|
3197
|
+
if (typeof att.type === "string") return snippet(`<attachment ${att.type}>`);
|
|
3198
|
+
}
|
|
2951
3199
|
return snippet(JSON.stringify(entry));
|
|
2952
3200
|
}
|
|
2953
3201
|
/**
|
|
@@ -4305,9 +4553,10 @@ async function runDistill(args, deps = {}) {
|
|
|
4305
4553
|
concurrency: parseClampedInt(args.concurrency, {
|
|
4306
4554
|
fallback: 3,
|
|
4307
4555
|
min: 1
|
|
4308
|
-
})
|
|
4556
|
+
}),
|
|
4557
|
+
breaker: args.breaker !== false
|
|
4309
4558
|
});
|
|
4310
|
-
const digestSummary = `digest stage: ${digestResult.completed + digestResult.skipped}/${plan.digests} done` + (digestResult.skipped ? ` (${digestResult.skipped} resumed from checkpoint)` : "") + (digestResult.failed.length ? `, ${digestResult.failed.length} failed` : "");
|
|
4559
|
+
const digestSummary = `digest stage: ${digestResult.completed + digestResult.skipped}/${plan.digests} done` + (digestResult.skipped ? ` (${digestResult.skipped} resumed from checkpoint)` : "") + (digestResult.failed.length ? `, ${digestResult.failed.length} failed` : "") + (digestResult.aborted ? `, ${digestResult.notAttempted.length} not attempted (stopped)` : "");
|
|
4311
4560
|
write(digestResult.failed.length ? digestSummary : green(digestSummary));
|
|
4312
4561
|
if (digestResult.failed.length > 0) {
|
|
4313
4562
|
write();
|
|
@@ -4482,6 +4731,11 @@ var distill_default = defineCommand({
|
|
|
4482
4731
|
default: true,
|
|
4483
4732
|
description: "Group sessions into tasks (use --no-group for 1 session = 1 task)"
|
|
4484
4733
|
},
|
|
4734
|
+
breaker: {
|
|
4735
|
+
type: "boolean",
|
|
4736
|
+
default: true,
|
|
4737
|
+
description: "Stop the digest stage after 5 consecutive identical failures (use --no-breaker to attempt every session)"
|
|
4738
|
+
},
|
|
4485
4739
|
"min-substance": {
|
|
4486
4740
|
type: "string",
|
|
4487
4741
|
default: "2",
|
package/npm-shrinkwrap.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cc-hindsight",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"lockfileVersion": 3,
|
|
5
5
|
"requires": true,
|
|
6
6
|
"packages": {
|
|
7
7
|
"": {
|
|
8
8
|
"name": "cc-hindsight",
|
|
9
|
-
"version": "1.
|
|
9
|
+
"version": "1.2.0",
|
|
10
10
|
"license": "MIT",
|
|
11
11
|
"dependencies": {
|
|
12
12
|
"@clack/prompts": "1.7.0",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cc-hindsight",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "Mine your coding-agent session history (Claude Code, kiro-cli) into a oneshot prompt library and CLAUDE.md/AGENTS.md preferences. The prompt you'd write if you knew then what you know now.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"author": "Aditya Singhal (https://adityasinghal.com)",
|