@vectorize-io/hindsight-coding-agents 0.5.5 → 0.6.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.
Files changed (50) hide show
  1. package/README.md +19 -5
  2. package/dist/antigravity-hook.js +84 -70
  3. package/dist/antigravity-statusline.js +32 -22
  4. package/dist/antigravity-stop-hook.js +160 -71
  5. package/dist/claude-hook.js +84 -70
  6. package/dist/claude-sessionstart-hook.js +111 -97
  7. package/dist/claude-stop-hook.js +160 -71
  8. package/dist/cline.js +158 -75
  9. package/dist/codex-hook.js +84 -70
  10. package/dist/codex-sessionstart-hook.js +111 -97
  11. package/dist/codex-stop-hook.js +160 -71
  12. package/dist/copilot-hook.js +84 -70
  13. package/dist/copilot-sessionstart-hook.js +111 -97
  14. package/dist/copilot-stop-hook.js +160 -71
  15. package/dist/cursor-hook.js +84 -70
  16. package/dist/cursor-sessionstart-hook.js +111 -97
  17. package/dist/cursor-stop-hook.js +160 -71
  18. package/dist/daemon-start.js +28 -18
  19. package/dist/dcode-hook.js +84 -70
  20. package/dist/dcode-sessionstart-hook.js +111 -97
  21. package/dist/dcode-stop-hook.js +160 -71
  22. package/dist/deepen.js +42 -32
  23. package/dist/devin-hook.js +84 -70
  24. package/dist/devin-sessionstart-hook.js +111 -97
  25. package/dist/devin-stop-hook.js +160 -71
  26. package/dist/droid-hook.js +84 -70
  27. package/dist/droid-sessionstart-hook.js +111 -97
  28. package/dist/droid-stop-hook.js +160 -71
  29. package/dist/dsh.js +156 -74
  30. package/dist/grok-hook.js +84 -70
  31. package/dist/grok-sessionstart-hook.js +111 -97
  32. package/dist/grok-stop-hook.js +160 -71
  33. package/dist/hindsight-seed.js +32 -22
  34. package/dist/index.js +159 -77
  35. package/dist/installer.js +308 -214
  36. package/dist/kilo.js +159 -77
  37. package/dist/mcp-server.js +41 -31
  38. package/dist/opencode2.js +158 -76
  39. package/dist/pi.js +158 -76
  40. package/dist/prime-agent.js +158 -76
  41. package/dist/qwen-hook.js +84 -70
  42. package/dist/qwen-sessionstart-hook.js +111 -97
  43. package/dist/qwen-stop-hook.js +160 -71
  44. package/dist/status.js +38 -28
  45. package/dist/zcode-hook.js +84 -70
  46. package/dist/zcode-sessionstart-hook.js +111 -97
  47. package/dist/zcode-stop-hook.js +160 -71
  48. package/package.json +1 -1
  49. package/plugin.json +1 -1
  50. package/skill/SKILL.md +19 -5
package/dist/cline.js CHANGED
@@ -6,8 +6,8 @@ var __export = (target, all) => {
6
6
 
7
7
  // src/core/config.ts
8
8
  import { readFileSync as readFileSync2 } from "fs";
9
- import { homedir as homedir2 } from "os";
10
- import { join as join6 } from "path";
9
+ import { homedir as homedir3 } from "os";
10
+ import { join as join7 } from "path";
11
11
 
12
12
  // src/core/seed.ts
13
13
  import { spawn as realSpawn } from "child_process";
@@ -44,16 +44,28 @@ function startBackgroundSeed(repoDir, opts = {}) {
44
44
 
45
45
  // src/core/bank.ts
46
46
  import { existsSync } from "fs";
47
- import { homedir } from "os";
48
- import { basename, dirname as dirname4, join as join4, normalize, sep } from "path";
47
+ import { homedir as homedir2 } from "os";
48
+ import { basename, dirname as dirname4, join as join5, normalize, sep } from "path";
49
49
 
50
50
  // src/core/diag.ts
51
- import { appendFileSync as appendFileSync2 } from "fs";
51
+ import { join as join3 } from "path";
52
52
 
53
53
  // src/core/log.ts
54
- import { appendFileSync, mkdirSync } from "fs";
55
- import { tmpdir } from "os";
54
+ import { appendFileSync, mkdirSync, renameSync, statSync } from "fs";
55
+ import { homedir } from "os";
56
56
  import { dirname as dirname2, join as join2 } from "path";
57
+ function logsDir() {
58
+ return join2(homedir(), ".hindsight", "coding-agents-logs");
59
+ }
60
+ var LOG_MAX_BYTES = 10 * 1024 * 1024;
61
+ function appendLogLine(file2, text) {
62
+ mkdirSync(dirname2(file2), { recursive: true, mode: 448 });
63
+ try {
64
+ if (statSync(file2).size >= LOG_MAX_BYTES) renameSync(file2, `${file2}.1`);
65
+ } catch {
66
+ }
67
+ appendFileSync(file2, text, { mode: 384 });
68
+ }
57
69
  var WEIGHT = { debug: 10, info: 20, warn: 30, error: 40 };
58
70
  var current = ["debug", "info", "warn", "error"].find(
59
71
  (l) => l === process.env.HINDSIGHT_LOG_LEVEL
@@ -62,15 +74,13 @@ function setLogLevel(level) {
62
74
  if (!process.env.HINDSIGHT_LOG_LEVEL) current = level;
63
75
  }
64
76
  function logFilePath() {
65
- return process.env.HINDSIGHT_LOG_FILE || join2(tmpdir(), "hindsight-coding-agent", "plugin.log");
77
+ return process.env.HINDSIGHT_LOG_FILE || join2(logsDir(), "plugin.log");
66
78
  }
67
79
  function write(level, scope, msg, extra) {
68
80
  if (WEIGHT[level] < WEIGHT[current]) return;
69
81
  try {
70
- const file2 = logFilePath();
71
- mkdirSync(dirname2(file2), { recursive: true });
72
- appendFileSync(
73
- file2,
82
+ appendLogLine(
83
+ logFilePath(),
74
84
  `${(/* @__PURE__ */ new Date()).toISOString()} ${level.toUpperCase().padEnd(5)} [${scope}] ${msg}` + (extra && Object.keys(extra).length ? ` ${JSON.stringify(extra)}` : "") + "\n"
75
85
  );
76
86
  } catch {
@@ -99,12 +109,12 @@ function describeError(value, maxChars = 200) {
99
109
 
100
110
  // src/core/diag.ts
101
111
  function diagFilePath() {
102
- return process.env.HINDSIGHT_DIAG_FILE || "/tmp/hindsight-plugin.log";
112
+ return process.env.HINDSIGHT_DIAG_FILE || join3(logsDir(), "diag.jsonl");
103
113
  }
104
114
  function diag(harness, event, extra = {}) {
105
115
  log.debug(harness, `diag:${event}`, extra);
106
116
  try {
107
- appendFileSync2(
117
+ appendLogLine(
108
118
  diagFilePath(),
109
119
  JSON.stringify({ ts: (/* @__PURE__ */ new Date()).toISOString(), harness, event, ...extra }) + "\n"
110
120
  );
@@ -114,7 +124,7 @@ function diag(harness, event, extra = {}) {
114
124
 
115
125
  // src/core/git-layout.ts
116
126
  import { lstatSync, readFileSync, realpathSync } from "fs";
117
- import { dirname as dirname3, isAbsolute, join as join3, resolve } from "path";
127
+ import { dirname as dirname3, isAbsolute, join as join4, resolve } from "path";
118
128
  var TRANSIENT = /* @__PURE__ */ new Set(["EAGAIN", "EMFILE", "ENFILE", "EBUSY", "EINTR", "EIO", "ETIMEDOUT"]);
119
129
  var ATTEMPTS = 3;
120
130
  var BACKOFF_MS = 20;
@@ -150,16 +160,16 @@ function gitDirFromPointer(text, holder) {
150
160
  return isAbsolute(target) ? target : resolve(holder, target);
151
161
  }
152
162
  function commonDirOf(gitDir) {
153
- const pointer = entryKind(join3(gitDir, "commondir")) === "file" ? readTextOrNull(join3(gitDir, "commondir"))?.trim() : null;
163
+ const pointer = entryKind(join4(gitDir, "commondir")) === "file" ? readTextOrNull(join4(gitDir, "commondir"))?.trim() : null;
154
164
  if (!pointer) return gitDir;
155
165
  return isAbsolute(pointer) ? pointer : resolve(gitDir, pointer);
156
166
  }
157
167
  function isBare(commonDir) {
158
- const config2 = readTextOrNull(join3(commonDir, "config"));
168
+ const config2 = readTextOrNull(join4(commonDir, "config"));
159
169
  return config2 !== null && /^\s*bare\s*=\s*true\s*$/im.test(config2);
160
170
  }
161
171
  function looksLikeBareRepository(directory) {
162
- return entryKind(join3(directory, "HEAD")) === "file" && entryKind(join3(directory, "objects")) === "dir" && entryKind(join3(directory, "refs")) === "dir";
172
+ return entryKind(join4(directory, "HEAD")) === "file" && entryKind(join4(directory, "objects")) === "dir" && entryKind(join4(directory, "refs")) === "dir";
163
173
  }
164
174
  function canonical(path) {
165
175
  try {
@@ -176,7 +186,7 @@ function resolved(commonDir) {
176
186
  function probeOnce(directory) {
177
187
  let current2 = resolve(directory);
178
188
  for (; ; ) {
179
- const dotGit = join3(current2, ".git");
189
+ const dotGit = join4(current2, ".git");
180
190
  const kind = entryKind(dotGit);
181
191
  if (kind === "dir") {
182
192
  const layout = resolved(commonDirOf(dotGit));
@@ -290,7 +300,7 @@ function gitProjectName(directory, resolveWorktrees, sessionRoot = "") {
290
300
  return dirName(sessionRoot || directory);
291
301
  }
292
302
  function configuredDir(dir) {
293
- const expanded = dir === "~" || dir.startsWith("~/") ? join4(homedir(), dir.slice(1)) : dir;
303
+ const expanded = dir === "~" || dir.startsWith("~/") ? join5(homedir2(), dir.slice(1)) : dir;
294
304
  return normalize(expanded).replace(new RegExp(`\\${sep}+$`), "");
295
305
  }
296
306
  function isWithin(directory, configured) {
@@ -594,7 +604,7 @@ function codingBankManifest(overrides) {
594
604
 
595
605
  // src/core/util.ts
596
606
  import { accessSync, constants } from "fs";
597
- import { delimiter, join as join5 } from "path";
607
+ import { delimiter, join as join6 } from "path";
598
608
  function binOnPath(bin) {
599
609
  try {
600
610
  if (bin.includes("/")) {
@@ -604,7 +614,7 @@ function binOnPath(bin) {
604
614
  for (const dir of (process.env.PATH || "").split(delimiter)) {
605
615
  if (!dir) continue;
606
616
  try {
607
- accessSync(join5(dir, bin), constants.X_OK);
617
+ accessSync(join6(dir, bin), constants.X_OK);
608
618
  return true;
609
619
  } catch {
610
620
  }
@@ -1285,7 +1295,7 @@ var HindsightClient = class {
1285
1295
  };
1286
1296
 
1287
1297
  // src/core/config.ts
1288
- var CONFIG_PATH = process.env.HINDSIGHT_CONFIG || join6(homedir2(), ".hindsight", "coding-agent.json");
1298
+ var CONFIG_PATH = process.env.HINDSIGHT_CONFIG || join7(homedir3(), ".hindsight", "coding-agent.json");
1289
1299
  var DEFAULT_DAEMON_PORT = 9077;
1290
1300
  var DEFAULT_DAEMON_PROFILE = "coding-agent";
1291
1301
  function resolvePageTrigger(raw) {
@@ -1579,20 +1589,20 @@ import {
1579
1589
  unlinkSync,
1580
1590
  writeFileSync
1581
1591
  } from "fs";
1582
- import { homedir as homedir3, tmpdir as tmpdir2 } from "os";
1583
- import { dirname as dirname5, join as join7 } from "path";
1592
+ import { homedir as homedir4, tmpdir } from "os";
1593
+ import { dirname as dirname5, join as join8 } from "path";
1584
1594
  import { fileURLToPath as fileURLToPath2 } from "url";
1585
1595
  var PACKAGE_NAME = "@vectorize-io/hindsight-coding-agents";
1586
1596
  var CHECK_INTERVAL_MS = 24 * 60 * 60 * 1e3;
1587
1597
  var FETCH_TIMEOUT_MS = 5e3;
1588
1598
  function stateFile(runtimeDir) {
1589
- return join7(runtimeDir, ".update-check.json");
1599
+ return join8(runtimeDir, ".update-check.json");
1590
1600
  }
1591
1601
  function packageRoot() {
1592
- return join7(dirname5(fileURLToPath2(import.meta.url)), "..");
1602
+ return join8(dirname5(fileURLToPath2(import.meta.url)), "..");
1593
1603
  }
1594
1604
  function stagedRuntimeDir() {
1595
- return join7(homedir3(), ".hindsight", "coding-agents");
1605
+ return join8(homedir4(), ".hindsight", "coding-agents");
1596
1606
  }
1597
1607
  function sameDir(a, b) {
1598
1608
  try {
@@ -1603,7 +1613,7 @@ function sameDir(a, b) {
1603
1613
  }
1604
1614
  function stagedVersion(pkgRoot) {
1605
1615
  try {
1606
- const pkg = JSON.parse(readFileSync3(join7(pkgRoot, "package.json"), "utf8"));
1616
+ const pkg = JSON.parse(readFileSync3(join8(pkgRoot, "package.json"), "utf8"));
1607
1617
  return typeof pkg.version === "string" ? pkg.version : "";
1608
1618
  } catch {
1609
1619
  return "";
@@ -1637,7 +1647,7 @@ function dueForCheck(file2, now) {
1637
1647
  var ORIGIN_FILE = ".install-origin.json";
1638
1648
  function selfUpdatable(runtimeDir) {
1639
1649
  try {
1640
- const origin = JSON.parse(readFileSync3(join7(runtimeDir, ORIGIN_FILE), "utf8"));
1650
+ const origin = JSON.parse(readFileSync3(join8(runtimeDir, ORIGIN_FILE), "utf8"));
1641
1651
  if (typeof origin.source !== "string" || !origin.source) return false;
1642
1652
  return origin.source.split(/[\\/]/).includes("_npx");
1643
1653
  } catch {
@@ -1646,7 +1656,7 @@ function selfUpdatable(runtimeDir) {
1646
1656
  }
1647
1657
  var LOCK_STALE_MS = 10 * 60 * 1e3;
1648
1658
  function lockFile() {
1649
- return join7(tmpdir2(), "hindsight-coding-agent", "auto-update.lock");
1659
+ return join8(tmpdir(), "hindsight-coding-agent", "auto-update.lock");
1650
1660
  }
1651
1661
  function acquireUpdateLock(file2, now) {
1652
1662
  try {
@@ -1763,7 +1773,7 @@ async function maybeAutoUpdate(cfg, opts = {}) {
1763
1773
 
1764
1774
  // src/core/daemon.ts
1765
1775
  import { execFileSync, spawn as realSpawn3 } from "child_process";
1766
- import { dirname as dirname6, join as join8 } from "path";
1776
+ import { dirname as dirname6, join as join9 } from "path";
1767
1777
  import { fileURLToPath as fileURLToPath3 } from "url";
1768
1778
  var DAEMON_WAIT_SESSION_START_MS = 12e3;
1769
1779
  var DAEMON_WAIT_RETAIN_MS = 4e4;
@@ -1838,7 +1848,7 @@ function onPath(bin) {
1838
1848
  }
1839
1849
  function startDaemonDetached(cfg, harness, spawnFn = realSpawn3) {
1840
1850
  try {
1841
- const starter = join8(dirname6(fileURLToPath3(import.meta.url)), "daemon-start.js");
1851
+ const starter = join9(dirname6(fileURLToPath3(import.meta.url)), "daemon-start.js");
1842
1852
  const child = spawnFn("node", [starter, "--harness", harness], {
1843
1853
  detached: true,
1844
1854
  stdio: "ignore",
@@ -16407,8 +16417,8 @@ config(en_default());
16407
16417
 
16408
16418
  // src/core/knowledge-tools.ts
16409
16419
  import { existsSync as existsSync4 } from "fs";
16410
- import { homedir as homedir5 } from "os";
16411
- import { join as join11 } from "path";
16420
+ import { homedir as homedir6 } from "os";
16421
+ import { join as join12 } from "path";
16412
16422
 
16413
16423
  // src/core/status.ts
16414
16424
  import { execFileSync as execFileSync3 } from "child_process";
@@ -16497,8 +16507,8 @@ ${toolGuide(opts)}
16497
16507
  import { spawn as realSpawn5 } from "child_process";
16498
16508
  import { createHash as createHash2 } from "crypto";
16499
16509
  import { existsSync as existsSync3 } from "fs";
16500
- import { homedir as homedir4, tmpdir as tmpdir3 } from "os";
16501
- import { dirname as dirname7, join as join10 } from "path";
16510
+ import { homedir as homedir5, tmpdir as tmpdir2 } from "os";
16511
+ import { dirname as dirname7, join as join11 } from "path";
16502
16512
  import { fileURLToPath as fileURLToPath4 } from "url";
16503
16513
 
16504
16514
  // src/core/survey-lease.ts
@@ -16508,20 +16518,20 @@ import {
16508
16518
  mkdirSync as mkdirSync3,
16509
16519
  mkdtempSync,
16510
16520
  readdirSync,
16511
- renameSync,
16521
+ renameSync as renameSync2,
16512
16522
  rmdirSync,
16513
16523
  rmSync,
16514
- statSync,
16524
+ statSync as statSync2,
16515
16525
  unlinkSync as unlinkSync2,
16516
16526
  utimesSync,
16517
16527
  writeFileSync as writeFileSync2
16518
16528
  } from "fs";
16519
- import { join as join9 } from "path";
16529
+ import { join as join10 } from "path";
16520
16530
  var LEASE_STALE_MS = 3e4;
16521
16531
  var SURVEY_SPEC_ENV = "HINDSIGHT_SURVEY_SPEC";
16522
16532
  function tryRename(from, to) {
16523
16533
  try {
16524
- renameSync(from, to);
16534
+ renameSync2(from, to);
16525
16535
  return true;
16526
16536
  } catch {
16527
16537
  return false;
@@ -16529,7 +16539,7 @@ function tryRename(from, to) {
16529
16539
  }
16530
16540
  function releaseLease(lease) {
16531
16541
  try {
16532
- unlinkSync2(join9(lease.directory, lease.owner));
16542
+ unlinkSync2(join10(lease.directory, lease.owner));
16533
16543
  } catch {
16534
16544
  return;
16535
16545
  }
@@ -16548,7 +16558,7 @@ function reclaimIfStale(directory, staleMs) {
16548
16558
  return true;
16549
16559
  }
16550
16560
  if (owners.length !== 1) return false;
16551
- const age = Date.now() - statSync(join9(directory, owners[0])).mtimeMs;
16561
+ const age = Date.now() - statSync2(join10(directory, owners[0])).mtimeMs;
16552
16562
  if (Math.abs(age) <= staleMs) return false;
16553
16563
  releaseLease({ directory, owner: owners[0] });
16554
16564
  return true;
@@ -16557,10 +16567,10 @@ function acquireLease(root, key, staleMs = LEASE_STALE_MS) {
16557
16567
  let staging;
16558
16568
  try {
16559
16569
  mkdirSync3(root, { recursive: true, mode: 448 });
16560
- staging = mkdtempSync(join9(root, "claim-"));
16570
+ staging = mkdtempSync(join10(root, "claim-"));
16561
16571
  const owner = randomUUID();
16562
- writeFileSync2(join9(staging, owner), "", { flag: "wx", mode: 384 });
16563
- const directory = join9(root, `survey-${key}.lock`);
16572
+ writeFileSync2(join10(staging, owner), "", { flag: "wx", mode: 384 });
16573
+ const directory = join10(root, `survey-${key}.lock`);
16564
16574
  if (!tryRename(staging, directory)) {
16565
16575
  if (!reclaimIfStale(directory, staleMs)) return void 0;
16566
16576
  if (!tryRename(staging, directory)) return void 0;
@@ -16583,7 +16593,7 @@ var SURVEY_DOC_IDS = [
16583
16593
  function resolveClaudeBin(explicit) {
16584
16594
  if (explicit) return explicit;
16585
16595
  if (process.env.HINDSIGHT_CLAUDE_BIN) return process.env.HINDSIGHT_CLAUDE_BIN;
16586
- const nativeInstallPath = join10(homedir4(), ".claude", "local", "claude");
16596
+ const nativeInstallPath = join11(homedir5(), ".claude", "local", "claude");
16587
16597
  try {
16588
16598
  if (existsSync3(nativeInstallPath)) return nativeInstallPath;
16589
16599
  } catch {
@@ -16701,7 +16711,7 @@ async function startCodebaseSurvey(repoDir, opts = {}) {
16701
16711
  try {
16702
16712
  const spawnFn = opts.spawn ?? realSpawn5;
16703
16713
  const exists = opts.exists ?? binOnPath;
16704
- const mcpServerPath = opts.mcpServerPath ?? join10(dirname7(fileURLToPath4(import.meta.url)), "mcp-server.js");
16714
+ const mcpServerPath = opts.mcpServerPath ?? join11(dirname7(fileURLToPath4(import.meta.url)), "mcp-server.js");
16705
16715
  const preferred = opts.harness ?? "claude-code";
16706
16716
  const order = [
16707
16717
  preferred,
@@ -16725,7 +16735,7 @@ async function startCodebaseSurvey(repoDir, opts = {}) {
16725
16735
  if (cfg.disabled) return false;
16726
16736
  const key = createHash2("sha256").update(JSON.stringify([cfg.apiUrl.replace(/\/+$/, ""), cfg.apiToken ?? "", bankId])).digest("hex");
16727
16737
  const lease = acquireLease(
16728
- opts.lease?.dir ?? join10(tmpdir3(), "hindsight-coding-agent", "surveys"),
16738
+ opts.lease?.dir ?? join11(tmpdir2(), "hindsight-coding-agent", "surveys"),
16729
16739
  key,
16730
16740
  opts.lease?.staleMs ?? LEASE_STALE_MS
16731
16741
  );
@@ -16736,7 +16746,7 @@ async function startCodebaseSurvey(repoDir, opts = {}) {
16736
16746
  args: plan.args,
16737
16747
  ...opts.lease?.heartbeatMs ? { heartbeatMs: opts.lease.heartbeatMs } : {}
16738
16748
  };
16739
- const supervisorPath = opts.supervisorPath ?? join10(dirname7(fileURLToPath4(import.meta.url)), "survey-supervisor.js");
16749
+ const supervisorPath = opts.supervisorPath ?? join11(dirname7(fileURLToPath4(import.meta.url)), "survey-supervisor.js");
16740
16750
  return await new Promise((resolve3) => {
16741
16751
  try {
16742
16752
  const child = spawnFn("node", [supervisorPath], {
@@ -16878,7 +16888,7 @@ function buildKnowledgeTools(client, bankId, opts = {}) {
16878
16888
  inputSchema: {},
16879
16889
  annotations: READ_ONLY_ANNOTATIONS,
16880
16890
  handler: async (_args) => {
16881
- const configPath = process.env.HINDSIGHT_CONFIG || join11(homedir5(), ".hindsight", "coding-agent.json");
16891
+ const configPath = process.env.HINDSIGHT_CONFIG || join12(homedir6(), ".hindsight", "coding-agent.json");
16882
16892
  const harness = opts.harness ?? "unknown";
16883
16893
  const cfg = applyBankConfig(
16884
16894
  loadConfig({ harness: opts.harness }),
@@ -16908,7 +16918,7 @@ function buildKnowledgeTools(client, bankId, opts = {}) {
16908
16918
  config_override: Boolean(process.env.HINDSIGHT_CONFIG),
16909
16919
  hooks_disabled: Boolean(process.env.HINDSIGHT_DISABLE_HOOKS),
16910
16920
  log_level: process.env.HINDSIGHT_LOG_LEVEL ?? null,
16911
- diagnostics_file: process.env.HINDSIGHT_DIAG_FILE ?? "/tmp/hindsight-plugin.log",
16921
+ diagnostics_file: diagFilePath(),
16912
16922
  channel_id_configured: Boolean(process.env.HINDSIGHT_CHANNEL_ID),
16913
16923
  user_id_configured: Boolean(process.env.HINDSIGHT_USER_ID)
16914
16924
  }
@@ -17209,6 +17219,64 @@ ${content}`);
17209
17219
  }
17210
17220
  }
17211
17221
 
17222
+ // src/core/usage.ts
17223
+ import { readFileSync as readFileSync4 } from "fs";
17224
+ import { join as join13 } from "path";
17225
+ function usageFilePath() {
17226
+ return process.env.HINDSIGHT_USAGE_FILE || join13(logsDir(), "usage.jsonl");
17227
+ }
17228
+ var HINDSIGHT_TOOL_RE = /hindsight_(?:[a-z]+_)*[a-z]+$/;
17229
+ var CREDIT_RE = /from hindsight memory/i;
17230
+ var ASSISTANT_ROLES = /* @__PURE__ */ new Set(["assistant", "model"]);
17231
+ function summarizeTurns(turns) {
17232
+ const out = [];
17233
+ for (const t of turns) {
17234
+ if (t.role === "user") {
17235
+ out.push({ turn: out.length + 1, calls: [], credited: false });
17236
+ continue;
17237
+ }
17238
+ const current2 = out.at(-1);
17239
+ if (!current2) continue;
17240
+ if (t.role === "action") {
17241
+ const tool = t.content.split(" ", 1)[0].match(HINDSIGHT_TOOL_RE)?.[0];
17242
+ if (tool) current2.calls.push(tool);
17243
+ } else if (ASSISTANT_ROLES.has(t.role) && CREDIT_RE.test(t.content)) {
17244
+ current2.credited = true;
17245
+ }
17246
+ }
17247
+ return out;
17248
+ }
17249
+ function memoryUsageCursorStore() {
17250
+ const recorded = /* @__PURE__ */ new Map();
17251
+ return {
17252
+ read: (sessionId2) => recorded.get(sessionId2),
17253
+ write: (sessionId2, turns) => void recorded.set(sessionId2, turns)
17254
+ };
17255
+ }
17256
+ function recordUsage(args) {
17257
+ const summary = summarizeTurns(args.turns);
17258
+ const finished = args.lastTurnComplete ? summary : summary.slice(0, -1);
17259
+ const from = args.cursors.read(args.sessionId) ?? 0;
17260
+ if (finished.length <= from) return;
17261
+ const ts = (/* @__PURE__ */ new Date()).toISOString();
17262
+ const lines = finished.slice(from).map(
17263
+ (u) => JSON.stringify({
17264
+ ts,
17265
+ harness: args.harness,
17266
+ session: args.sessionId,
17267
+ bank: args.bankId,
17268
+ turn: u.turn,
17269
+ calls: u.calls,
17270
+ credited: u.credited
17271
+ }) + "\n"
17272
+ ).join("");
17273
+ try {
17274
+ appendLogLine(usageFilePath(), lines);
17275
+ args.cursors.write(args.sessionId, finished.length);
17276
+ } catch {
17277
+ }
17278
+ }
17279
+
17212
17280
  // src/core/retain-stamp.ts
17213
17281
  import { basename as basename2 } from "path";
17214
17282
  var RESERVED_TAG_PREFIX = /^(source|harness):/;
@@ -17257,12 +17325,12 @@ function buildRetainStamp(cfg, ctx) {
17257
17325
  }
17258
17326
 
17259
17327
  // src/core/session-start.ts
17260
- import { readFileSync as readFileSync7 } from "fs";
17328
+ import { readFileSync as readFileSync8 } from "fs";
17261
17329
 
17262
17330
  // src/core/skill-sync.ts
17263
- import { cpSync, existsSync as existsSync5, readFileSync as readFileSync4 } from "fs";
17264
- import { homedir as homedir6 } from "os";
17265
- import { dirname as dirname8, join as join12 } from "path";
17331
+ import { cpSync, existsSync as existsSync5, readFileSync as readFileSync5 } from "fs";
17332
+ import { homedir as homedir7 } from "os";
17333
+ import { dirname as dirname8, join as join14 } from "path";
17266
17334
  import { fileURLToPath as fileURLToPath5 } from "url";
17267
17335
 
17268
17336
  // src/core/brand.ts
@@ -17280,15 +17348,15 @@ function brandWord() {
17280
17348
  }
17281
17349
 
17282
17350
  // src/core/legacy.ts
17283
- import { existsSync as existsSync6, readFileSync as readFileSync5 } from "fs";
17284
- import { homedir as homedir7 } from "os";
17285
- import { join as join13, relative, isAbsolute as isAbsolute2 } from "path";
17351
+ import { existsSync as existsSync6, readFileSync as readFileSync6 } from "fs";
17352
+ import { homedir as homedir8 } from "os";
17353
+ import { join as join15, relative, isAbsolute as isAbsolute2 } from "path";
17286
17354
  var LEGACY_CLAUDE_PLUGIN = "hindsight-memory";
17287
- function detectLegacyClaudePlugin(cwd, claudeDir = join13(homedir7(), ".claude")) {
17288
- const registry2 = readJson(join13(claudeDir, "plugins", "installed_plugins.json"));
17355
+ function detectLegacyClaudePlugin(cwd, claudeDir = join15(homedir8(), ".claude")) {
17356
+ const registry2 = readJson(join15(claudeDir, "plugins", "installed_plugins.json"));
17289
17357
  const plugins = registry2?.plugins;
17290
17358
  if (!plugins || typeof plugins !== "object") return void 0;
17291
- const enabled = readJson(join13(claudeDir, "settings.json"))?.enabledPlugins;
17359
+ const enabled = readJson(join15(claudeDir, "settings.json"))?.enabledPlugins;
17292
17360
  for (const [key, raw] of Object.entries(plugins)) {
17293
17361
  if (key.split("@")[0] !== LEGACY_CLAUDE_PLUGIN) continue;
17294
17362
  if (enabled && enabled[key] === false) continue;
@@ -17305,7 +17373,7 @@ function detectLegacyClaudePlugin(cwd, claudeDir = join13(homedir7(), ".claude")
17305
17373
  }
17306
17374
  function readJson(path) {
17307
17375
  try {
17308
- const parsed = JSON.parse(readFileSync5(path, "utf8"));
17376
+ const parsed = JSON.parse(readFileSync6(path, "utf8"));
17309
17377
  return parsed && typeof parsed === "object" ? parsed : void 0;
17310
17378
  } catch {
17311
17379
  return void 0;
@@ -17317,15 +17385,15 @@ function legacyClaudePluginWarning(key) {
17317
17385
  }
17318
17386
 
17319
17387
  // src/core/session-cache.ts
17320
- import { mkdirSync as mkdirSync4, readFileSync as readFileSync6, renameSync as renameSync2, rmSync as rmSync2, writeFileSync as writeFileSync3 } from "fs";
17321
- import { tmpdir as tmpdir4 } from "os";
17322
- import { dirname as dirname9, join as join14 } from "path";
17388
+ import { mkdirSync as mkdirSync4, readFileSync as readFileSync7, renameSync as renameSync3, rmSync as rmSync2, writeFileSync as writeFileSync3 } from "fs";
17389
+ import { tmpdir as tmpdir3 } from "os";
17390
+ import { dirname as dirname9, join as join16 } from "path";
17323
17391
  function sessionCacheFile(harness, sessionId2) {
17324
- return join14(tmpdir4(), `hindsight-${harness}`, `${sessionId2}.json`);
17392
+ return join16(tmpdir3(), `hindsight-${harness}`, `${sessionId2}.json`);
17325
17393
  }
17326
17394
  function readSessionCache(cacheFile) {
17327
17395
  try {
17328
- return JSON.parse(readFileSync6(cacheFile, "utf8"));
17396
+ return JSON.parse(readFileSync7(cacheFile, "utf8"));
17329
17397
  } catch {
17330
17398
  return {};
17331
17399
  }
@@ -17335,7 +17403,7 @@ function writeFileAtomic(path, body) {
17335
17403
  const tmp = `${path}.${process.pid}.tmp`;
17336
17404
  try {
17337
17405
  writeFileSync3(tmp, body);
17338
- renameSync2(tmp, path);
17406
+ renameSync3(tmp, path);
17339
17407
  } catch (e) {
17340
17408
  try {
17341
17409
  rmSync2(tmp, { force: true });
@@ -17511,7 +17579,7 @@ ${legacyClaudePluginWarning(legacyPlugin)}`;
17511
17579
  }
17512
17580
 
17513
17581
  // src/core/hook.ts
17514
- import { existsSync as existsSync7, readFileSync as readFileSync9 } from "fs";
17582
+ import { existsSync as existsSync7, readFileSync as readFileSync10 } from "fs";
17515
17583
 
17516
17584
  // src/core/inject.ts
17517
17585
  function buildReflectQuery(prompt) {
@@ -17552,9 +17620,9 @@ If you VERIFY this memory is wrong or outdated (the code or facts contradict it)
17552
17620
  }
17553
17621
 
17554
17622
  // src/core/turn-journal.ts
17555
- import { appendFileSync as appendFileSync3, mkdirSync as mkdirSync5, readFileSync as readFileSync8, statSync as statSync2 } from "fs";
17556
- import { tmpdir as tmpdir5 } from "os";
17557
- import { dirname as dirname10, join as join15 } from "path";
17623
+ import { appendFileSync as appendFileSync2, mkdirSync as mkdirSync5, readFileSync as readFileSync9, statSync as statSync3 } from "fs";
17624
+ import { tmpdir as tmpdir4 } from "os";
17625
+ import { dirname as dirname10, join as join17 } from "path";
17558
17626
  var JOURNAL_MAX_BYTES = 8 * 1024 * 1024;
17559
17627
 
17560
17628
  // src/core/hook.ts
@@ -17725,6 +17793,8 @@ var RuntimeCore = class {
17725
17793
  sessionState = /* @__PURE__ */ new Map();
17726
17794
  /** Live write-back cursors. In memory, unlike the hook harnesses': this host outlives the session. */
17727
17795
  cursors = memoryCursorStore();
17796
+ /** Turns already written to the usage log (core/usage.ts), per session. */
17797
+ usageCursors = memoryUsageCursorStore();
17728
17798
  /** Pulls a session's CURRENT transcript from the host (set by the adapter); see onSessionIdle. */
17729
17799
  fetchTranscript;
17730
17800
  lastInjection = "";
@@ -17847,9 +17917,10 @@ var RuntimeCore = class {
17847
17917
  * (`engine.retain.fold`), so submitting every turn costs one extraction, not one per turn, and
17848
17918
  * nothing is ever held somewhere it can be lost.
17849
17919
  */
17850
- async onTranscript(sessionId2, turns) {
17920
+ async onTranscript(sessionId2, turns, lastTurnComplete) {
17851
17921
  if (process.env.HINDSIGHT_DISABLE_HOOKS) return;
17852
17922
  if (!this.writeBackEnabled || !sessionId2 || !turns.length) return;
17923
+ this.recordUsage(sessionId2, turns, lastTurnComplete);
17853
17924
  const st = this.stateFor(sessionId2);
17854
17925
  this.retain(sessionId2, turns, st.startTs);
17855
17926
  }
@@ -17879,11 +17950,22 @@ var RuntimeCore = class {
17879
17950
  return;
17880
17951
  }
17881
17952
  if (!turns.length) return;
17953
+ this.recordUsage(sessionId2, turns, true);
17882
17954
  const st = this.stateFor(sessionId2);
17883
17955
  if (turns.length <= st.retainedTurns) return;
17884
17956
  st.retainedTurns = turns.length;
17885
17957
  this.retain(sessionId2, turns, st.startTs, "idle");
17886
17958
  }
17959
+ recordUsage(sessionId2, turns, lastTurnComplete) {
17960
+ recordUsage({
17961
+ harness: this.harness,
17962
+ sessionId: sessionId2,
17963
+ bankId: this.bankId,
17964
+ turns,
17965
+ cursors: this.usageCursors,
17966
+ lastTurnComplete
17967
+ });
17968
+ }
17887
17969
  stateFor(sessionId2) {
17888
17970
  let st = this.sessionState.get(sessionId2);
17889
17971
  if (!st) {
@@ -18013,7 +18095,8 @@ function createClineHooks(core, configuredSessionId, sessionStart) {
18013
18095
  },
18014
18096
  async afterRun({ snapshot }) {
18015
18097
  const turns = clineTranscript(snapshot.messages);
18016
- if (turns.length) await core.onTranscript(sessionId(snapshot, configuredSessionId), turns);
18098
+ if (turns.length)
18099
+ await core.onTranscript(sessionId(snapshot, configuredSessionId), turns, true);
18017
18100
  }
18018
18101
  };
18019
18102
  }