@vectorize-io/hindsight-coding-agents 0.5.5 → 0.6.1

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 +87 -83
  3. package/dist/antigravity-statusline.js +32 -22
  4. package/dist/antigravity-stop-hook.js +160 -71
  5. package/dist/claude-hook.js +87 -83
  6. package/dist/claude-sessionstart-hook.js +111 -97
  7. package/dist/claude-stop-hook.js +160 -71
  8. package/dist/cline.js +161 -88
  9. package/dist/codex-hook.js +87 -83
  10. package/dist/codex-sessionstart-hook.js +111 -97
  11. package/dist/codex-stop-hook.js +160 -71
  12. package/dist/copilot-hook.js +87 -83
  13. package/dist/copilot-sessionstart-hook.js +111 -97
  14. package/dist/copilot-stop-hook.js +160 -71
  15. package/dist/cursor-hook.js +87 -83
  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 +87 -83
  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 +87 -83
  24. package/dist/devin-sessionstart-hook.js +111 -97
  25. package/dist/devin-stop-hook.js +160 -71
  26. package/dist/droid-hook.js +87 -83
  27. package/dist/droid-sessionstart-hook.js +111 -97
  28. package/dist/droid-stop-hook.js +160 -71
  29. package/dist/dsh.js +159 -87
  30. package/dist/grok-hook.js +87 -83
  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 +162 -90
  35. package/dist/installer.js +308 -214
  36. package/dist/kilo.js +162 -90
  37. package/dist/mcp-server.js +41 -31
  38. package/dist/opencode2.js +161 -89
  39. package/dist/pi.js +161 -89
  40. package/dist/prime-agent.js +161 -89
  41. package/dist/qwen-hook.js +87 -83
  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 +87 -83
  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
@@ -14520,8 +14520,8 @@ config(en_default());
14520
14520
 
14521
14521
  // src/core/config.ts
14522
14522
  import { readFileSync as readFileSync2 } from "fs";
14523
- import { homedir as homedir2 } from "os";
14524
- import { join as join6 } from "path";
14523
+ import { homedir as homedir3 } from "os";
14524
+ import { join as join7 } from "path";
14525
14525
 
14526
14526
  // src/core/seed.ts
14527
14527
  import { spawn as realSpawn } from "child_process";
@@ -14558,16 +14558,28 @@ function startBackgroundSeed(repoDir, opts = {}) {
14558
14558
 
14559
14559
  // src/core/bank.ts
14560
14560
  import { existsSync } from "fs";
14561
- import { homedir } from "os";
14562
- import { basename, dirname as dirname4, join as join4, normalize, sep } from "path";
14561
+ import { homedir as homedir2 } from "os";
14562
+ import { basename, dirname as dirname4, join as join5, normalize, sep } from "path";
14563
14563
 
14564
14564
  // src/core/diag.ts
14565
- import { appendFileSync as appendFileSync2 } from "fs";
14565
+ import { join as join3 } from "path";
14566
14566
 
14567
14567
  // src/core/log.ts
14568
- import { appendFileSync, mkdirSync } from "fs";
14569
- import { tmpdir } from "os";
14568
+ import { appendFileSync, mkdirSync, renameSync, statSync } from "fs";
14569
+ import { homedir } from "os";
14570
14570
  import { dirname as dirname2, join as join2 } from "path";
14571
+ function logsDir() {
14572
+ return join2(homedir(), ".hindsight", "coding-agents-logs");
14573
+ }
14574
+ var LOG_MAX_BYTES = 10 * 1024 * 1024;
14575
+ function appendLogLine(file2, text) {
14576
+ mkdirSync(dirname2(file2), { recursive: true, mode: 448 });
14577
+ try {
14578
+ if (statSync(file2).size >= LOG_MAX_BYTES) renameSync(file2, `${file2}.1`);
14579
+ } catch {
14580
+ }
14581
+ appendFileSync(file2, text, { mode: 384 });
14582
+ }
14571
14583
  var WEIGHT = { debug: 10, info: 20, warn: 30, error: 40 };
14572
14584
  var current = ["debug", "info", "warn", "error"].find(
14573
14585
  (l) => l === process.env.HINDSIGHT_LOG_LEVEL
@@ -14576,15 +14588,13 @@ function setLogLevel(level) {
14576
14588
  if (!process.env.HINDSIGHT_LOG_LEVEL) current = level;
14577
14589
  }
14578
14590
  function logFilePath() {
14579
- return process.env.HINDSIGHT_LOG_FILE || join2(tmpdir(), "hindsight-coding-agent", "plugin.log");
14591
+ return process.env.HINDSIGHT_LOG_FILE || join2(logsDir(), "plugin.log");
14580
14592
  }
14581
14593
  function write(level, scope, msg, extra) {
14582
14594
  if (WEIGHT[level] < WEIGHT[current]) return;
14583
14595
  try {
14584
- const file2 = logFilePath();
14585
- mkdirSync(dirname2(file2), { recursive: true });
14586
- appendFileSync(
14587
- file2,
14596
+ appendLogLine(
14597
+ logFilePath(),
14588
14598
  `${(/* @__PURE__ */ new Date()).toISOString()} ${level.toUpperCase().padEnd(5)} [${scope}] ${msg}` + (extra && Object.keys(extra).length ? ` ${JSON.stringify(extra)}` : "") + "\n"
14589
14599
  );
14590
14600
  } catch {
@@ -14613,12 +14623,12 @@ function describeError(value, maxChars = 200) {
14613
14623
 
14614
14624
  // src/core/diag.ts
14615
14625
  function diagFilePath() {
14616
- return process.env.HINDSIGHT_DIAG_FILE || "/tmp/hindsight-plugin.log";
14626
+ return process.env.HINDSIGHT_DIAG_FILE || join3(logsDir(), "diag.jsonl");
14617
14627
  }
14618
14628
  function diag(harness, event, extra = {}) {
14619
14629
  log.debug(harness, `diag:${event}`, extra);
14620
14630
  try {
14621
- appendFileSync2(
14631
+ appendLogLine(
14622
14632
  diagFilePath(),
14623
14633
  JSON.stringify({ ts: (/* @__PURE__ */ new Date()).toISOString(), harness, event, ...extra }) + "\n"
14624
14634
  );
@@ -14628,7 +14638,7 @@ function diag(harness, event, extra = {}) {
14628
14638
 
14629
14639
  // src/core/git-layout.ts
14630
14640
  import { lstatSync, readFileSync, realpathSync } from "fs";
14631
- import { dirname as dirname3, isAbsolute, join as join3, resolve } from "path";
14641
+ import { dirname as dirname3, isAbsolute, join as join4, resolve } from "path";
14632
14642
  var TRANSIENT = /* @__PURE__ */ new Set(["EAGAIN", "EMFILE", "ENFILE", "EBUSY", "EINTR", "EIO", "ETIMEDOUT"]);
14633
14643
  var ATTEMPTS = 3;
14634
14644
  var BACKOFF_MS = 20;
@@ -14664,16 +14674,16 @@ function gitDirFromPointer(text, holder) {
14664
14674
  return isAbsolute(target) ? target : resolve(holder, target);
14665
14675
  }
14666
14676
  function commonDirOf(gitDir) {
14667
- const pointer = entryKind(join3(gitDir, "commondir")) === "file" ? readTextOrNull(join3(gitDir, "commondir"))?.trim() : null;
14677
+ const pointer = entryKind(join4(gitDir, "commondir")) === "file" ? readTextOrNull(join4(gitDir, "commondir"))?.trim() : null;
14668
14678
  if (!pointer) return gitDir;
14669
14679
  return isAbsolute(pointer) ? pointer : resolve(gitDir, pointer);
14670
14680
  }
14671
14681
  function isBare(commonDir) {
14672
- const config2 = readTextOrNull(join3(commonDir, "config"));
14682
+ const config2 = readTextOrNull(join4(commonDir, "config"));
14673
14683
  return config2 !== null && /^\s*bare\s*=\s*true\s*$/im.test(config2);
14674
14684
  }
14675
14685
  function looksLikeBareRepository(directory) {
14676
- return entryKind(join3(directory, "HEAD")) === "file" && entryKind(join3(directory, "objects")) === "dir" && entryKind(join3(directory, "refs")) === "dir";
14686
+ return entryKind(join4(directory, "HEAD")) === "file" && entryKind(join4(directory, "objects")) === "dir" && entryKind(join4(directory, "refs")) === "dir";
14677
14687
  }
14678
14688
  function canonical(path) {
14679
14689
  try {
@@ -14690,7 +14700,7 @@ function resolved(commonDir) {
14690
14700
  function probeOnce(directory) {
14691
14701
  let current2 = resolve(directory);
14692
14702
  for (; ; ) {
14693
- const dotGit = join3(current2, ".git");
14703
+ const dotGit = join4(current2, ".git");
14694
14704
  const kind = entryKind(dotGit);
14695
14705
  if (kind === "dir") {
14696
14706
  const layout = resolved(commonDirOf(dotGit));
@@ -14804,7 +14814,7 @@ function gitProjectName(directory, resolveWorktrees, sessionRoot = "") {
14804
14814
  return dirName(sessionRoot || directory);
14805
14815
  }
14806
14816
  function configuredDir(dir) {
14807
- const expanded = dir === "~" || dir.startsWith("~/") ? join4(homedir(), dir.slice(1)) : dir;
14817
+ const expanded = dir === "~" || dir.startsWith("~/") ? join5(homedir2(), dir.slice(1)) : dir;
14808
14818
  return normalize(expanded).replace(new RegExp(`\\${sep}+$`), "");
14809
14819
  }
14810
14820
  function isWithin(directory, configured) {
@@ -15108,7 +15118,7 @@ function codingBankManifest(overrides) {
15108
15118
 
15109
15119
  // src/core/util.ts
15110
15120
  import { accessSync, constants } from "fs";
15111
- import { delimiter, join as join5 } from "path";
15121
+ import { delimiter, join as join6 } from "path";
15112
15122
  function binOnPath(bin) {
15113
15123
  try {
15114
15124
  if (bin.includes("/")) {
@@ -15118,7 +15128,7 @@ function binOnPath(bin) {
15118
15128
  for (const dir of (process.env.PATH || "").split(delimiter)) {
15119
15129
  if (!dir) continue;
15120
15130
  try {
15121
- accessSync(join5(dir, bin), constants.X_OK);
15131
+ accessSync(join6(dir, bin), constants.X_OK);
15122
15132
  return true;
15123
15133
  } catch {
15124
15134
  }
@@ -15799,7 +15809,7 @@ var HindsightClient = class {
15799
15809
  };
15800
15810
 
15801
15811
  // src/core/config.ts
15802
- var CONFIG_PATH = process.env.HINDSIGHT_CONFIG || join6(homedir2(), ".hindsight", "coding-agent.json");
15812
+ var CONFIG_PATH = process.env.HINDSIGHT_CONFIG || join7(homedir3(), ".hindsight", "coding-agent.json");
15803
15813
  var DEFAULT_DAEMON_PORT = 9077;
15804
15814
  var DEFAULT_DAEMON_PROFILE = "coding-agent";
15805
15815
  function resolvePageTrigger(raw) {
@@ -16093,20 +16103,20 @@ import {
16093
16103
  unlinkSync,
16094
16104
  writeFileSync
16095
16105
  } from "fs";
16096
- import { homedir as homedir3, tmpdir as tmpdir2 } from "os";
16097
- import { dirname as dirname5, join as join7 } from "path";
16106
+ import { homedir as homedir4, tmpdir } from "os";
16107
+ import { dirname as dirname5, join as join8 } from "path";
16098
16108
  import { fileURLToPath as fileURLToPath2 } from "url";
16099
16109
  var PACKAGE_NAME = "@vectorize-io/hindsight-coding-agents";
16100
16110
  var CHECK_INTERVAL_MS = 24 * 60 * 60 * 1e3;
16101
16111
  var FETCH_TIMEOUT_MS = 5e3;
16102
16112
  function stateFile(runtimeDir) {
16103
- return join7(runtimeDir, ".update-check.json");
16113
+ return join8(runtimeDir, ".update-check.json");
16104
16114
  }
16105
16115
  function packageRoot() {
16106
- return join7(dirname5(fileURLToPath2(import.meta.url)), "..");
16116
+ return join8(dirname5(fileURLToPath2(import.meta.url)), "..");
16107
16117
  }
16108
16118
  function stagedRuntimeDir() {
16109
- return join7(homedir3(), ".hindsight", "coding-agents");
16119
+ return join8(homedir4(), ".hindsight", "coding-agents");
16110
16120
  }
16111
16121
  function sameDir(a, b) {
16112
16122
  try {
@@ -16117,7 +16127,7 @@ function sameDir(a, b) {
16117
16127
  }
16118
16128
  function stagedVersion(pkgRoot) {
16119
16129
  try {
16120
- const pkg = JSON.parse(readFileSync3(join7(pkgRoot, "package.json"), "utf8"));
16130
+ const pkg = JSON.parse(readFileSync3(join8(pkgRoot, "package.json"), "utf8"));
16121
16131
  return typeof pkg.version === "string" ? pkg.version : "";
16122
16132
  } catch {
16123
16133
  return "";
@@ -16151,7 +16161,7 @@ function dueForCheck(file2, now) {
16151
16161
  var ORIGIN_FILE = ".install-origin.json";
16152
16162
  function selfUpdatable(runtimeDir) {
16153
16163
  try {
16154
- const origin = JSON.parse(readFileSync3(join7(runtimeDir, ORIGIN_FILE), "utf8"));
16164
+ const origin = JSON.parse(readFileSync3(join8(runtimeDir, ORIGIN_FILE), "utf8"));
16155
16165
  if (typeof origin.source !== "string" || !origin.source) return false;
16156
16166
  return origin.source.split(/[\\/]/).includes("_npx");
16157
16167
  } catch {
@@ -16160,7 +16170,7 @@ function selfUpdatable(runtimeDir) {
16160
16170
  }
16161
16171
  var LOCK_STALE_MS = 10 * 60 * 1e3;
16162
16172
  function lockFile() {
16163
- return join7(tmpdir2(), "hindsight-coding-agent", "auto-update.lock");
16173
+ return join8(tmpdir(), "hindsight-coding-agent", "auto-update.lock");
16164
16174
  }
16165
16175
  function acquireUpdateLock(file2, now) {
16166
16176
  try {
@@ -16277,7 +16287,7 @@ async function maybeAutoUpdate(cfg, opts = {}) {
16277
16287
 
16278
16288
  // src/core/daemon.ts
16279
16289
  import { execFileSync, spawn as realSpawn3 } from "child_process";
16280
- import { dirname as dirname6, join as join8 } from "path";
16290
+ import { dirname as dirname6, join as join9 } from "path";
16281
16291
  import { fileURLToPath as fileURLToPath3 } from "url";
16282
16292
  var DAEMON_WAIT_SESSION_START_MS = 12e3;
16283
16293
  var DAEMON_WAIT_RETAIN_MS = 4e4;
@@ -16352,7 +16362,7 @@ function onPath(bin) {
16352
16362
  }
16353
16363
  function startDaemonDetached(cfg, harness, spawnFn = realSpawn3) {
16354
16364
  try {
16355
- const starter = join8(dirname6(fileURLToPath3(import.meta.url)), "daemon-start.js");
16365
+ const starter = join9(dirname6(fileURLToPath3(import.meta.url)), "daemon-start.js");
16356
16366
  const child = spawnFn("node", [starter, "--harness", harness], {
16357
16367
  detached: true,
16358
16368
  stdio: "ignore",
@@ -16407,8 +16417,8 @@ function preflightDaemon(cfg, harness) {
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: (sessionId) => recorded.get(sessionId),
17253
+ write: (sessionId, turns) => void recorded.set(sessionId, 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, sessionId) {
17324
- return join14(tmpdir4(), `hindsight-${harness}`, `${sessionId}.json`);
17392
+ return join16(tmpdir3(), `hindsight-${harness}`, `${sessionId}.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
 
17559
17627
  // src/core/transcript-util.ts
17560
17628
  var MEMORY_TAG_RE = /<(hook_prompt|task-notification|system-reminder|hindsight_memory|hindsight_memories|hindsight_bank|relevant_memories|user_feedback|hindsight_knowledge|hindsight_knowledge_refresh)\b[\s\S]*?<\/\1>/g;
@@ -17607,9 +17675,7 @@ async function reflectFallback(harness, prompt, client) {
17607
17675
  try {
17608
17676
  const hits = await client.searchKnowledgePages(query, FALLBACK_PAGE_LIMIT, remaining());
17609
17677
  diag(harness, "reflect_fallback_pages", { ms: Date.now() - t0, count: hits.length });
17610
- if (hits.length) {
17611
- return { memory: formatPageFallback(hits), source: "knowledge_pages", count: hits.length };
17612
- }
17678
+ if (hits.length) return formatPageFallback(hits);
17613
17679
  } catch (e) {
17614
17680
  diag(harness, "reflect_fallback_pages_failed", {
17615
17681
  ms: Date.now() - t0,
@@ -17626,13 +17692,7 @@ async function reflectFallback(harness, prompt, client) {
17626
17692
  ms: Date.now() - t0,
17627
17693
  count: observations.length
17628
17694
  });
17629
- if (observations.length) {
17630
- return {
17631
- memory: formatObservationFallback(observations),
17632
- source: "observations",
17633
- count: observations.length
17634
- };
17635
- }
17695
+ if (observations.length) return formatObservationFallback(observations);
17636
17696
  } catch (e) {
17637
17697
  diag(harness, "reflect_fallback_observations_failed", {
17638
17698
  ms: Date.now() - t0,
@@ -17688,7 +17748,7 @@ async function buildHookOutput(args) {
17688
17748
  });
17689
17749
  if (e instanceof ReflectError && e.fallbackEligible) {
17690
17750
  fallback = await reflectFallback(harness, prompt, client);
17691
- if (fallback) reflectAnswer = fallback.memory;
17751
+ if (fallback) reflectAnswer = fallback;
17692
17752
  }
17693
17753
  }
17694
17754
  }
@@ -17724,8 +17784,6 @@ async function buildHookOutput(args) {
17724
17784
  const kept = blocks.filter(Boolean);
17725
17785
  let notice;
17726
17786
  if (fallback) {
17727
- const what = fallback.source === "knowledge_pages" ? "knowledge page" : "observation";
17728
- notice = `${brandWord()} \xB7 reflect unavailable \u2014 fell back to ${fallback.count} ${what}${fallback.count === 1 ? "" : "s"} (see ${diagFilePath()})`;
17729
17787
  } else if (reflectRanThisTurn && reflectAnswer) {
17730
17788
  const q = prompt.replace(/\s+/g, " ").trim();
17731
17789
  const excerpt = q.length > 48 ? `${q.slice(0, 48)}\u2026` : q;
@@ -17762,6 +17820,8 @@ var RuntimeCore = class {
17762
17820
  sessionState = /* @__PURE__ */ new Map();
17763
17821
  /** Live write-back cursors. In memory, unlike the hook harnesses': this host outlives the session. */
17764
17822
  cursors = memoryCursorStore();
17823
+ /** Turns already written to the usage log (core/usage.ts), per session. */
17824
+ usageCursors = memoryUsageCursorStore();
17765
17825
  /** Pulls a session's CURRENT transcript from the host (set by the adapter); see onSessionIdle. */
17766
17826
  fetchTranscript;
17767
17827
  lastInjection = "";
@@ -17884,9 +17944,10 @@ var RuntimeCore = class {
17884
17944
  * (`engine.retain.fold`), so submitting every turn costs one extraction, not one per turn, and
17885
17945
  * nothing is ever held somewhere it can be lost.
17886
17946
  */
17887
- async onTranscript(sessionId, turns) {
17947
+ async onTranscript(sessionId, turns, lastTurnComplete) {
17888
17948
  if (process.env.HINDSIGHT_DISABLE_HOOKS) return;
17889
17949
  if (!this.writeBackEnabled || !sessionId || !turns.length) return;
17950
+ this.recordUsage(sessionId, turns, lastTurnComplete);
17890
17951
  const st = this.stateFor(sessionId);
17891
17952
  this.retain(sessionId, turns, st.startTs);
17892
17953
  }
@@ -17916,11 +17977,22 @@ var RuntimeCore = class {
17916
17977
  return;
17917
17978
  }
17918
17979
  if (!turns.length) return;
17980
+ this.recordUsage(sessionId, turns, true);
17919
17981
  const st = this.stateFor(sessionId);
17920
17982
  if (turns.length <= st.retainedTurns) return;
17921
17983
  st.retainedTurns = turns.length;
17922
17984
  this.retain(sessionId, turns, st.startTs, "idle");
17923
17985
  }
17986
+ recordUsage(sessionId, turns, lastTurnComplete) {
17987
+ recordUsage({
17988
+ harness: this.harness,
17989
+ sessionId,
17990
+ bankId: this.bankId,
17991
+ turns,
17992
+ cursors: this.usageCursors,
17993
+ lastTurnComplete
17994
+ });
17995
+ }
17924
17996
  stateFor(sessionId) {
17925
17997
  let st = this.sessionState.get(sessionId);
17926
17998
  if (!st) {
@@ -17961,7 +18033,7 @@ var RuntimeCore = class {
17961
18033
  };
17962
18034
 
17963
18035
  // src/core/jsonl.ts
17964
- import { closeSync, openSync, readSync, statSync as statSync3 } from "fs";
18036
+ import { closeSync, openSync, readSync, statSync as statSync4 } from "fs";
17965
18037
  import { StringDecoder } from "string_decoder";
17966
18038
  var CHUNK_BYTES = 64 * 1024;
17967
18039
  var MAX_TRANSCRIPT_BYTES = 32 * 1024 * 1024;
@@ -18035,7 +18107,7 @@ ${injection}` };
18035
18107
  },
18036
18108
  async agentEnd(event, sessionId) {
18037
18109
  const turns = readPiMessages(event.messages);
18038
- if (turns.length) await core.onTranscript(sessionId, turns);
18110
+ if (turns.length) await core.onTranscript(sessionId, turns, true);
18039
18111
  }
18040
18112
  };
18041
18113
  }