@vectorize-io/hindsight-coding-agents 0.1.1 → 0.1.2

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.
@@ -442,8 +442,22 @@ var CODING_BANK_TEMPLATE = {
442
442
 
443
443
  // src/core/util.ts
444
444
  var sleep = (ms) => new Promise((r) => setTimeout(r, ms));
445
+ function semverGte(version, min) {
446
+ const parts = (v) => {
447
+ const m = v.trim().match(/^(\d+)\.(\d+)(?:\.(\d+))?/);
448
+ return m ? [Number(m[1]), Number(m[2]), Number(m[3] ?? 0)] : void 0;
449
+ };
450
+ const a = version ? parts(version) : void 0;
451
+ const b = parts(min);
452
+ if (!a || !b) return false;
453
+ for (let i = 0; i < 3; i++) {
454
+ if (a[i] !== b[i]) return a[i] > b[i];
455
+ }
456
+ return true;
457
+ }
445
458
 
446
459
  // src/core/hindsight.ts
460
+ var MIN_IDEMPOTENT_RETAIN_VERSION = "0.8.6";
447
461
  var KnowledgePagesUnavailableError = class extends Error {
448
462
  code = "knowledge_pages_unavailable";
449
463
  constructor() {
@@ -460,6 +474,8 @@ var HindsightClient = class {
460
474
  // async operation ids collected by retain(), for drain()
461
475
  /** Tri-state capability probe: unknown until the first page request, then cached. */
462
476
  knowledgePagesSupported;
477
+ /** Tri-state capability probe: unknown until the first append-mode retain, then cached. */
478
+ idempotentRetain;
463
479
  log;
464
480
  constructor(o) {
465
481
  this.apiUrl = o.apiUrl.replace(/\/$/, "");
@@ -488,7 +504,8 @@ var HindsightClient = class {
488
504
  throw new Error(`${method} ${url} -> ${r.status} ${await r.text()}`);
489
505
  return r;
490
506
  }
491
- /** Retain one memory. Async by default: enqueue extraction server-side and collect its op-id for drain(). */
507
+ /** Retain one memory. ALWAYS async: enqueue extraction server-side and collect its op-id for
508
+ * drain(). Nothing in this plugin can afford to block a coding agent's hook on extraction. */
492
509
  async retain(content, context, documentId, tags, strategy, opts = {}) {
493
510
  const item = {
494
511
  content,
@@ -499,14 +516,36 @@ var HindsightClient = class {
499
516
  };
500
517
  if (opts.timestamp) item.timestamp = opts.timestamp;
501
518
  if (opts.metadata) item.metadata = opts.metadata;
502
- const isAsync = opts.async !== false;
503
- const r = await this.req("POST", this.bankUrl("/memories"), { items: [item], async: isAsync });
504
- if (isAsync) {
505
- try {
506
- const j = await r.json();
507
- if (j.operation_id) this.opIds.push(j.operation_id);
508
- } catch {
509
- }
519
+ if (opts.updateMode) item.update_mode = opts.updateMode;
520
+ const body = { items: [item], async: true };
521
+ if (opts.operationId) body.operation_id = opts.operationId;
522
+ const r = await this.req("POST", this.bankUrl("/memories"), body);
523
+ try {
524
+ const j = await r.json();
525
+ if (j.operation_id) this.opIds.push(j.operation_id);
526
+ } catch {
527
+ }
528
+ }
529
+ /**
530
+ * Whether this server honours `operation_id` on an async retain, and can therefore be appended to
531
+ * safely (see MIN_IDEMPOTENT_RETAIN_VERSION). Probed once per client via GET /version; anything
532
+ * unreachable, unparseable or older answers "no", which costs efficiency, never correctness.
533
+ */
534
+ async supportsIdempotentRetain() {
535
+ if (this.idempotentRetain === void 0) {
536
+ this.idempotentRetain = await this.probeIdempotentRetain();
537
+ this.log(`server retain idempotency: ${this.idempotentRetain ? "supported" : "unavailable"}`);
538
+ }
539
+ return this.idempotentRetain;
540
+ }
541
+ async probeIdempotentRetain() {
542
+ try {
543
+ const r = await this.req("GET", `${this.apiUrl}/version`);
544
+ if (!r.ok) return false;
545
+ const j = await r.json();
546
+ return semverGte(j.api_version, MIN_IDEMPOTENT_RETAIN_VERSION);
547
+ } catch {
548
+ return false;
510
549
  }
511
550
  }
512
551
  /**
@@ -794,8 +833,7 @@ var HindsightClient = class {
794
833
  "initiative marker",
795
834
  markerId,
796
835
  ["knowledge:feature-work", `relatedPageId:${pageId}`],
797
- "document",
798
- { async: true }
836
+ "document"
799
837
  );
800
838
  return { page_id: pageId };
801
839
  }
@@ -884,7 +922,13 @@ function writeSessionCache(cacheFile, cache) {
884
922
  }
885
923
 
886
924
  // src/core/retain-hook.ts
887
- import { readFileSync as readFileSync5 } from "fs";
925
+ import { readFileSync as readFileSync4 } from "fs";
926
+
927
+ // src/core/retain-cursor.ts
928
+ import { createHash } from "crypto";
929
+
930
+ // src/core/uuid.ts
931
+ import { createHash as createHash2 } from "crypto";
888
932
 
889
933
  // src/core/daemon.ts
890
934
  import { execFileSync as execFileSync2, spawn as realSpawn2 } from "child_process";
@@ -1002,8 +1046,66 @@ function preflightDaemon(cfg, harness) {
1002
1046
  return true;
1003
1047
  }
1004
1048
 
1005
- // src/core/transcript.ts
1006
- import { readFileSync as readFileSync4 } from "fs";
1049
+ // src/core/jsonl.ts
1050
+ import { closeSync, openSync, readSync, statSync } from "fs";
1051
+ import { StringDecoder } from "string_decoder";
1052
+ var CHUNK_BYTES = 64 * 1024;
1053
+ var MAX_TRANSCRIPT_BYTES = 32 * 1024 * 1024;
1054
+ function readJsonlTail(path, opts) {
1055
+ const maxBytes = opts.maxBytes ?? MAX_TRANSCRIPT_BYTES;
1056
+ let size;
1057
+ try {
1058
+ size = statSync(path).size;
1059
+ } catch {
1060
+ return { lines: emptyLines(), skippedBytes: 0 };
1061
+ }
1062
+ const skippedBytes = size > maxBytes ? size - maxBytes : 0;
1063
+ if (skippedBytes > 0) {
1064
+ log.warn(opts.scope, "transcript too large \u2014 retaining the most recent portion only", {
1065
+ path,
1066
+ sizeBytes: size,
1067
+ skippedBytes
1068
+ });
1069
+ }
1070
+ return { lines: streamLines(path, skippedBytes), skippedBytes };
1071
+ }
1072
+ function* emptyLines() {
1073
+ }
1074
+ function* streamLines(path, start) {
1075
+ let fd;
1076
+ try {
1077
+ fd = openSync(path, "r");
1078
+ } catch {
1079
+ return;
1080
+ }
1081
+ try {
1082
+ const buffer = Buffer.allocUnsafe(CHUNK_BYTES);
1083
+ const decoder = new StringDecoder("utf8");
1084
+ let pending = "";
1085
+ let position = start > 0 ? start - 1 : 0;
1086
+ let dropPartial = start > 0;
1087
+ for (; ; ) {
1088
+ const bytesRead = readSync(fd, buffer, 0, buffer.length, position);
1089
+ if (bytesRead <= 0) break;
1090
+ position += bytesRead;
1091
+ pending += decoder.write(buffer.subarray(0, bytesRead));
1092
+ let newline;
1093
+ while ((newline = pending.indexOf("\n")) !== -1) {
1094
+ const line = pending.slice(0, newline);
1095
+ pending = pending.slice(newline + 1);
1096
+ if (dropPartial) {
1097
+ dropPartial = false;
1098
+ continue;
1099
+ }
1100
+ yield line;
1101
+ }
1102
+ }
1103
+ pending += decoder.end();
1104
+ if (pending && !dropPartial) yield pending;
1105
+ } finally {
1106
+ closeSync(fd);
1107
+ }
1108
+ }
1007
1109
 
1008
1110
  // src/core/transcript-util.ts
1009
1111
  var MEMORY_TAG_RE = /<(hook_prompt|hindsight_memory|hindsight_memories|hindsight_bank|relevant_memories|user_feedback|hindsight_knowledge|hindsight_knowledge_refresh)\b[\s\S]*?<\/\1>/g;
@@ -1041,7 +1143,7 @@ function actionLine(tool, input) {
1041
1143
  }
1042
1144
 
1043
1145
  // src/core/session-start.ts
1044
- import { readFileSync as readFileSync7 } from "fs";
1146
+ import { readFileSync as readFileSync6 } from "fs";
1045
1147
 
1046
1148
  // src/core/git.ts
1047
1149
  import { execFileSync as execFileSync3 } from "child_process";
@@ -1266,7 +1368,7 @@ function startCodebaseSurvey(repoDir, opts = {}) {
1266
1368
  var DEEPEN_DIFF_TARGET = 300;
1267
1369
 
1268
1370
  // src/core/skill-sync.ts
1269
- import { cpSync, existsSync as existsSync3, readFileSync as readFileSync6 } from "fs";
1371
+ import { cpSync, existsSync as existsSync3, readFileSync as readFileSync5 } from "fs";
1270
1372
  import { homedir as homedir4 } from "os";
1271
1373
  import { dirname as dirname7, join as join8 } from "path";
1272
1374
  import { fileURLToPath as fileURLToPath4 } from "url";
@@ -1290,7 +1392,7 @@ function syncCompanionSkill(harness, opts = {}) {
1290
1392
  const dst = join8(opts.home ?? homedir4(), ...parts, "hindsight-coding-agent");
1291
1393
  const dstMd = join8(dst, "SKILL.md");
1292
1394
  if (!existsSync3(dstMd)) return;
1293
- if (readFileSync6(srcMd, "utf8") !== readFileSync6(dstMd, "utf8")) {
1395
+ if (readFileSync5(srcMd, "utf8") !== readFileSync5(dstMd, "utf8")) {
1294
1396
  cpSync(src, dst, { recursive: true });
1295
1397
  }
1296
1398
  } catch {
@@ -1346,8 +1448,7 @@ async function buildSessionStartContext(args) {
1346
1448
  "hindsight codebase-survey baseline",
1347
1449
  `${SURVEY_BASELINE_PREFIX}${sha}`,
1348
1450
  [SURVEY_BASELINE_TAG],
1349
- "survey",
1350
- { async: true }
1451
+ "survey"
1351
1452
  )
1352
1453
  ).catch(() => {
1353
1454
  });
@@ -1444,7 +1545,7 @@ async function runSessionStartHook(spec, makeClient = (o) => new HindsightClient
1444
1545
  try {
1445
1546
  let ev = {};
1446
1547
  try {
1447
- ev = JSON.parse(readFileSync7(0, "utf8"));
1548
+ ev = JSON.parse(readFileSync6(0, "utf8"));
1448
1549
  } catch {
1449
1550
  return;
1450
1551
  }
@@ -1474,7 +1575,6 @@ async function runSessionStartHook(spec, makeClient = (o) => new HindsightClient
1474
1575
  }
1475
1576
 
1476
1577
  // src/core/transcript-codex.ts
1477
- import { readFileSync as readFileSync8 } from "fs";
1478
1578
  function isSyntheticUserText(text) {
1479
1579
  const s = text.trimStart();
1480
1580
  return s.startsWith("# AGENTS.md instructions for ") || s.startsWith("<environment_context>");
@@ -1483,14 +1583,8 @@ function messageText(payload) {
1483
1583
  return (payload.content || []).filter((c) => c && typeof c.text === "string").map((c) => c.text).join("\n");
1484
1584
  }
1485
1585
  function readCodexTranscript(path) {
1486
- let raw;
1487
- try {
1488
- raw = readFileSync8(path, "utf8");
1489
- } catch {
1490
- return [];
1491
- }
1492
1586
  const turns = [];
1493
- for (const rawLine of raw.split("\n")) {
1587
+ for (const rawLine of readJsonlTail(path, { scope: "codex" }).lines) {
1494
1588
  const trimmed = rawLine.trim();
1495
1589
  if (!trimmed) continue;
1496
1590
  let parsed;
@@ -1524,7 +1618,6 @@ function readCodexTranscript(path) {
1524
1618
  }
1525
1619
 
1526
1620
  // src/core/transcript-cursor.ts
1527
- import { readFileSync as readFileSync9 } from "fs";
1528
1621
  function withTimestamp(turn, timestamp) {
1529
1622
  return timestamp ? { ...turn, timestamp } : turn;
1530
1623
  }
@@ -1534,14 +1627,8 @@ function textFrom(content) {
1534
1627
  return content.filter((block) => block?.type === "text" && typeof block.text === "string").map((block) => block.text).join("\n");
1535
1628
  }
1536
1629
  function readCursorTranscript(path) {
1537
- let raw;
1538
- try {
1539
- raw = readFileSync9(path, "utf8");
1540
- } catch {
1541
- return [];
1542
- }
1543
1630
  const turns = [];
1544
- for (const rawLine of raw.split("\n")) {
1631
+ for (const rawLine of readJsonlTail(path, { scope: "cursor-cli" }).lines) {
1545
1632
  let parsed;
1546
1633
  try {
1547
1634
  parsed = JSON.parse(rawLine);
@@ -1577,17 +1664,10 @@ function readCursorTranscript(path) {
1577
1664
  }
1578
1665
 
1579
1666
  // src/core/transcript-antigravity.ts
1580
- import { readFileSync as readFileSync10 } from "fs";
1581
1667
  function readAntigravityTranscript(path) {
1582
1668
  if (!path) return [];
1583
- let raw;
1584
- try {
1585
- raw = readFileSync10(path, "utf8");
1586
- } catch {
1587
- return [];
1588
- }
1589
1669
  const turns = [];
1590
- for (const line of raw.split("\n")) {
1670
+ for (const line of readJsonlTail(path, { scope: "antigravity-cli" }).lines) {
1591
1671
  try {
1592
1672
  const event = JSON.parse(line);
1593
1673
  const role = event.role ?? event.message?.role ?? event.type;
@@ -1608,16 +1688,9 @@ function readAntigravityTranscript(path) {
1608
1688
  }
1609
1689
 
1610
1690
  // src/core/transcript-copilot.ts
1611
- import { readFileSync as readFileSync11 } from "fs";
1612
1691
  function readCopilotTranscript(path) {
1613
- let raw;
1614
- try {
1615
- raw = readFileSync11(path, "utf8");
1616
- } catch {
1617
- return [];
1618
- }
1619
1692
  const turns = [];
1620
- for (const rawLine of raw.split("\n")) {
1693
+ for (const rawLine of readJsonlTail(path, { scope: "copilot-cli" }).lines) {
1621
1694
  try {
1622
1695
  const event = JSON.parse(rawLine);
1623
1696
  const role = event.type === "user.message" ? "user" : event.type === "assistant.message" ? "assistant" : void 0;
@@ -1631,7 +1704,7 @@ function readCopilotTranscript(path) {
1631
1704
  }
1632
1705
 
1633
1706
  // src/core/transcript-grok.ts
1634
- import { existsSync as existsSync4, readFileSync as readFileSync12, readdirSync } from "fs";
1707
+ import { existsSync as existsSync4, readFileSync as readFileSync7, readdirSync } from "fs";
1635
1708
  import { homedir as homedir5 } from "os";
1636
1709
  import { join as join9 } from "path";
1637
1710
  var CHAT_HISTORY = "chat_history.jsonl";
@@ -1642,7 +1715,7 @@ function grokTranscriptPath(cwd, sessionId, grokHome = process.env.GROK_HOME ||
1642
1715
  try {
1643
1716
  for (const candidate of readdirSync(sessionsDir)) {
1644
1717
  const directory = join9(sessionsDir, candidate);
1645
- if (readFileSync12(join9(directory, ".cwd"), "utf8").trim() === cwd) {
1718
+ if (readFileSync7(join9(directory, ".cwd"), "utf8").trim() === cwd) {
1646
1719
  return join9(directory, sessionId, CHAT_HISTORY);
1647
1720
  }
1648
1721
  }
@@ -1651,14 +1724,8 @@ function grokTranscriptPath(cwd, sessionId, grokHome = process.env.GROK_HOME ||
1651
1724
  return direct;
1652
1725
  }
1653
1726
  function readGrokTranscript(path) {
1654
- let raw;
1655
- try {
1656
- raw = readFileSync12(path, "utf8");
1657
- } catch {
1658
- return [];
1659
- }
1660
1727
  const turns = [];
1661
- for (const rawLine of raw.split("\n")) {
1728
+ for (const rawLine of readJsonlTail(path, { scope: "grok-build" }).lines) {
1662
1729
  try {
1663
1730
  const event = JSON.parse(rawLine);
1664
1731
  if (event.type !== "user" && event.type !== "assistant") continue;