@vectorize-io/hindsight-coding-agents 0.2.1 → 0.3.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.
- package/README.md +46 -7
- package/dist/antigravity-hook.js +175 -48
- package/dist/antigravity-statusline.js +127 -80
- package/dist/antigravity-stop-hook.js +233 -88
- package/dist/claude-hook.js +175 -48
- package/dist/claude-sessionstart-hook.js +234 -58
- package/dist/claude-stop-hook.js +233 -88
- package/dist/cline.js +351 -167
- package/dist/codex-hook.js +175 -48
- package/dist/codex-sessionstart-hook.js +234 -58
- package/dist/codex-stop-hook.js +233 -88
- package/dist/copilot-hook.js +175 -48
- package/dist/copilot-sessionstart-hook.js +234 -58
- package/dist/copilot-stop-hook.js +233 -88
- package/dist/cursor-hook.js +175 -48
- package/dist/cursor-sessionstart-hook.js +234 -58
- package/dist/cursor-stop-hook.js +233 -88
- package/dist/daemon-start.js +27 -15
- package/dist/deepen.js +688 -497
- package/dist/devin-hook.js +175 -48
- package/dist/devin-sessionstart-hook.js +234 -58
- package/dist/devin-stop-hook.js +233 -88
- package/dist/grok-hook.js +175 -48
- package/dist/grok-sessionstart-hook.js +234 -58
- package/dist/grok-stop-hook.js +233 -88
- package/dist/hindsight-seed.js +111 -78
- package/dist/index.js +282 -98
- package/dist/installer.js +86 -56
- package/dist/kilo.js +282 -98
- package/dist/mcp-server.js +327 -117
- package/dist/prime-agent.js +16900 -0
- package/dist/status.js +143 -36
- package/package.json +6 -1
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
3
|
// src/core/hook.ts
|
|
4
|
-
import { existsSync, readFileSync as readFileSync3 } from "fs";
|
|
4
|
+
import { existsSync as existsSync2, readFileSync as readFileSync3 } from "fs";
|
|
5
5
|
|
|
6
6
|
// src/core/bank.ts
|
|
7
7
|
import { execFileSync } from "child_process";
|
|
8
|
+
import { existsSync } from "fs";
|
|
8
9
|
import { homedir } from "os";
|
|
9
10
|
import { basename, dirname, join, normalize, sep } from "path";
|
|
10
11
|
|
|
@@ -40,25 +41,62 @@ function getProjectRootFromGit(directory) {
|
|
|
40
41
|
return null;
|
|
41
42
|
}
|
|
42
43
|
}
|
|
44
|
+
function projectNameOf(directory) {
|
|
45
|
+
return gitProjectName(directory, true);
|
|
46
|
+
}
|
|
47
|
+
var PROJECT_ROOT_ENV = ["CLAUDE_PROJECT_DIR"];
|
|
48
|
+
function nearestExistingDir(directory) {
|
|
49
|
+
let current2 = directory;
|
|
50
|
+
while (current2) {
|
|
51
|
+
if (existsSync(current2)) return current2;
|
|
52
|
+
const parent = dirname(current2);
|
|
53
|
+
if (parent === current2) return "";
|
|
54
|
+
current2 = parent;
|
|
55
|
+
}
|
|
56
|
+
return "";
|
|
57
|
+
}
|
|
58
|
+
function dirName(directory) {
|
|
59
|
+
return directory && basename(directory) || "unknown";
|
|
60
|
+
}
|
|
43
61
|
function gitProjectName(directory, resolveWorktrees) {
|
|
44
62
|
if (resolveWorktrees) {
|
|
45
|
-
const
|
|
46
|
-
|
|
63
|
+
const candidates = [
|
|
64
|
+
nearestExistingDir(directory),
|
|
65
|
+
...PROJECT_ROOT_ENV.map((v) => process.env[v] || "")
|
|
66
|
+
];
|
|
67
|
+
for (const candidate of candidates) {
|
|
68
|
+
const root = candidate ? getProjectRootFromGit(candidate) : null;
|
|
69
|
+
if (root) return basename(root);
|
|
70
|
+
}
|
|
47
71
|
}
|
|
48
|
-
return
|
|
72
|
+
return dirName(directory);
|
|
73
|
+
}
|
|
74
|
+
function configuredDir(dir) {
|
|
75
|
+
const expanded = dir === "~" || dir.startsWith("~/") ? join(homedir(), dir.slice(1)) : dir;
|
|
76
|
+
return normalize(expanded).replace(new RegExp(`\\${sep}+$`), "");
|
|
77
|
+
}
|
|
78
|
+
function isWithin(directory, configured) {
|
|
79
|
+
return directory === configured || directory.startsWith(configured + sep);
|
|
49
80
|
}
|
|
50
81
|
function mapLookup(map, directory) {
|
|
51
82
|
const cwd = normalize(directory);
|
|
52
83
|
let best;
|
|
53
84
|
for (const [dir, bank] of Object.entries(map)) {
|
|
54
|
-
const
|
|
55
|
-
|
|
56
|
-
if (cwd === p || cwd.startsWith(p + sep)) {
|
|
85
|
+
const p = configuredDir(dir);
|
|
86
|
+
if (isWithin(cwd, p)) {
|
|
57
87
|
if (!best || p.length > best.len) best = { len: p.length, bank };
|
|
58
88
|
}
|
|
59
89
|
}
|
|
60
90
|
return best?.bank;
|
|
61
91
|
}
|
|
92
|
+
function isOptedIn(config, directory) {
|
|
93
|
+
if (!config.optInOnly) return true;
|
|
94
|
+
if (!directory) return false;
|
|
95
|
+
const cwd = normalize(directory);
|
|
96
|
+
if ((config.optInPaths ?? []).some((dir) => dir && isWithin(cwd, configuredDir(dir))))
|
|
97
|
+
return true;
|
|
98
|
+
return Boolean(config.mapPathToBank && mapLookup(config.mapPathToBank, directory));
|
|
99
|
+
}
|
|
62
100
|
function deriveBankId(config, directory, harness = "coding") {
|
|
63
101
|
const mapped = directory && config.mapPathToBank ? mapLookup(config.mapPathToBank, directory) : void 0;
|
|
64
102
|
if (mapped) return mapped;
|
|
@@ -66,7 +104,7 @@ function deriveBankId(config, directory, harness = "coding") {
|
|
|
66
104
|
if (!dynamic) return config.bankId || DEFAULT_BANK_NAME;
|
|
67
105
|
const resolvers = {
|
|
68
106
|
harness: () => harness,
|
|
69
|
-
project: () =>
|
|
107
|
+
project: () => dirName(directory),
|
|
70
108
|
gitProject: () => gitProjectName(directory, config.resolveWorktrees ?? true),
|
|
71
109
|
channel: () => process.env.HINDSIGHT_CHANNEL_ID || "default",
|
|
72
110
|
user: () => process.env.HINDSIGHT_USER_ID || "anonymous"
|
|
@@ -136,11 +174,14 @@ function resolveConfig(raw = {}) {
|
|
|
136
174
|
bankIdTemplate: raw.bankIdTemplate,
|
|
137
175
|
mapPathToBank: raw.mapPathToBank,
|
|
138
176
|
resolveWorktrees: raw.resolveWorktrees,
|
|
177
|
+
optInOnly: raw.optInOnly ?? false,
|
|
178
|
+
// Same shape as retainTags: a config typo must not become a path that silently approves nothing.
|
|
179
|
+
optInPaths: Array.isArray(raw.optInPaths) ? raw.optInPaths.filter((p) => typeof p === "string" && p.trim() !== "") : [],
|
|
139
180
|
harness: raw.harness ?? "opencode",
|
|
140
181
|
disabled: raw.disabled ?? false,
|
|
141
182
|
retainSessions: raw.retainSessions ?? true,
|
|
142
183
|
// opencode: write back by default (parity with hook-harness Stop)
|
|
143
|
-
|
|
184
|
+
maxParallelRetains: raw.maxParallelRetains || 10,
|
|
144
185
|
reflectTimeoutMs: raw.reflectTimeoutMs || 12e4,
|
|
145
186
|
autoReflect: raw.autoReflect ?? true,
|
|
146
187
|
pageRefreshEveryTurns: raw.pageRefreshEveryTurns || 10,
|
|
@@ -196,10 +237,12 @@ var ENV_KEYS = {
|
|
|
196
237
|
dynamicBankId: "HINDSIGHT_DYNAMIC_BANK_ID",
|
|
197
238
|
bankIdTemplate: "HINDSIGHT_BANK_ID_TEMPLATE",
|
|
198
239
|
resolveWorktrees: "HINDSIGHT_RESOLVE_WORKTREES",
|
|
240
|
+
optInOnly: "HINDSIGHT_OPT_IN_ONLY",
|
|
241
|
+
optInPaths: "HINDSIGHT_OPT_IN_PATHS",
|
|
199
242
|
harness: "HINDSIGHT_HARNESS",
|
|
200
243
|
disabled: "HINDSIGHT_DISABLED",
|
|
201
244
|
retainSessions: "HINDSIGHT_RETAIN_SESSIONS",
|
|
202
|
-
|
|
245
|
+
maxParallelRetains: "HINDSIGHT_MAX_PARALLEL_RETAINS",
|
|
203
246
|
reflectTimeoutMs: "HINDSIGHT_REFLECT_TIMEOUT_MS",
|
|
204
247
|
autoReflect: "HINDSIGHT_AUTO_REFLECT",
|
|
205
248
|
pageRefreshEveryTurns: "HINDSIGHT_PAGE_REFRESH_EVERY_TURNS",
|
|
@@ -218,17 +261,18 @@ var ENV_KEYS = {
|
|
|
218
261
|
var ENV_BOOLEANS = /* @__PURE__ */ new Set([
|
|
219
262
|
"dynamicBankId",
|
|
220
263
|
"resolveWorktrees",
|
|
264
|
+
"optInOnly",
|
|
221
265
|
"disabled",
|
|
222
266
|
"retainSessions",
|
|
223
267
|
"autoReflect",
|
|
224
268
|
"autoSeed",
|
|
225
269
|
"codebaseSurvey"
|
|
226
270
|
]);
|
|
227
|
-
var ENV_LISTS = /* @__PURE__ */ new Set(["retainTags"]);
|
|
271
|
+
var ENV_LISTS = /* @__PURE__ */ new Set(["retainTags", "optInPaths"]);
|
|
228
272
|
var ENV_NUMBERS = /* @__PURE__ */ new Set([
|
|
229
273
|
"apiPort",
|
|
230
274
|
"daemonIdleTimeout",
|
|
231
|
-
"
|
|
275
|
+
"maxParallelRetains",
|
|
232
276
|
"reflectTimeoutMs",
|
|
233
277
|
"pageRefreshEveryTurns",
|
|
234
278
|
"seedLimit",
|
|
@@ -268,9 +312,15 @@ var BANK_OVERRIDE_EXCLUDED = [
|
|
|
268
312
|
"mapPathToBank",
|
|
269
313
|
"dynamicBankId",
|
|
270
314
|
"resolveWorktrees",
|
|
315
|
+
// Approval is decided BEFORE the bank is resolved, so a `banks.<id>` section naming these could
|
|
316
|
+
// only ever arrive too late to matter — strip them rather than let them read as effective.
|
|
317
|
+
"optInOnly",
|
|
318
|
+
"optInPaths",
|
|
271
319
|
"harness"
|
|
272
320
|
];
|
|
273
|
-
function applyBankConfig(cfg, resolvedId) {
|
|
321
|
+
function applyBankConfig(cfg, resolvedId, directory) {
|
|
322
|
+
if (directory !== void 0 && !isOptedIn(cfg, directory))
|
|
323
|
+
return { cfg: { ...cfg, disabled: true }, bankId: resolvedId };
|
|
274
324
|
const section = cfg.banks[resolvedId];
|
|
275
325
|
if (!section) return { cfg, bankId: resolvedId };
|
|
276
326
|
const safe = { ...section };
|
|
@@ -482,9 +532,40 @@ function semverGte(version, min) {
|
|
|
482
532
|
}
|
|
483
533
|
return true;
|
|
484
534
|
}
|
|
535
|
+
async function pool(items, n, fn, onError, onProgress) {
|
|
536
|
+
let i = 0, done = 0;
|
|
537
|
+
async function worker() {
|
|
538
|
+
while (i < items.length) {
|
|
539
|
+
const idx = i++;
|
|
540
|
+
try {
|
|
541
|
+
await fn(items[idx], idx);
|
|
542
|
+
} catch (e) {
|
|
543
|
+
onError?.(idx, e);
|
|
544
|
+
}
|
|
545
|
+
onProgress?.(++done, items.length);
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
await Promise.all(Array.from({ length: Math.min(n, items.length) }, worker));
|
|
549
|
+
}
|
|
485
550
|
|
|
486
551
|
// src/core/hindsight.ts
|
|
487
552
|
var MIN_IDEMPOTENT_RETAIN_VERSION = "0.8.6";
|
|
553
|
+
var RateLimitedError = class extends Error {
|
|
554
|
+
constructor(retryAfterMs2) {
|
|
555
|
+
super(`rate limited; retry after ${Math.round(retryAfterMs2 / 1e3)}s`);
|
|
556
|
+
this.retryAfterMs = retryAfterMs2;
|
|
557
|
+
this.name = "RateLimitedError";
|
|
558
|
+
}
|
|
559
|
+
retryAfterMs;
|
|
560
|
+
code = "rate_limited";
|
|
561
|
+
};
|
|
562
|
+
function retryAfterMs(header) {
|
|
563
|
+
if (!header) return 0;
|
|
564
|
+
const secs = Number(header.trim());
|
|
565
|
+
if (Number.isFinite(secs) && secs >= 0) return secs * 1e3;
|
|
566
|
+
const at = Date.parse(header);
|
|
567
|
+
return Number.isNaN(at) ? 0 : Math.max(0, at - Date.now());
|
|
568
|
+
}
|
|
488
569
|
var KnowledgePagesUnavailableError = class extends Error {
|
|
489
570
|
code = "knowledge_pages_unavailable";
|
|
490
571
|
constructor() {
|
|
@@ -493,6 +574,10 @@ var KnowledgePagesUnavailableError = class extends Error {
|
|
|
493
574
|
}
|
|
494
575
|
};
|
|
495
576
|
var TERMINAL = /* @__PURE__ */ new Set(["completed", "failed", "cancelled", "error"]);
|
|
577
|
+
var DEFAULT_MAX_PARALLEL_RETAINS = 10;
|
|
578
|
+
var POLL_CYCLE_MS = 5e3;
|
|
579
|
+
var RETRY_AFTER_FLOOR_MS = 10 * 1e3;
|
|
580
|
+
var RETRY_AFTER_CEILING_MS = 60 * 1e3;
|
|
496
581
|
var MISSION_FIELDS = ["reflect_mission", "retain_mission", "observations_mission"];
|
|
497
582
|
var HindsightClient = class {
|
|
498
583
|
apiUrl;
|
|
@@ -505,12 +590,14 @@ var HindsightClient = class {
|
|
|
505
590
|
/** Tri-state capability probe: unknown until the first append-mode retain, then cached. */
|
|
506
591
|
idempotentRetain;
|
|
507
592
|
log;
|
|
593
|
+
maxParallelRetains;
|
|
508
594
|
constructor(o) {
|
|
509
595
|
this.apiUrl = o.apiUrl.replace(/\/$/, "");
|
|
510
596
|
this.apiToken = o.apiToken;
|
|
511
597
|
this.bank = o.bank;
|
|
512
598
|
this.log = o.log ?? (() => {
|
|
513
599
|
});
|
|
600
|
+
this.maxParallelRetains = o.maxParallelRetains || DEFAULT_MAX_PARALLEL_RETAINS;
|
|
514
601
|
}
|
|
515
602
|
headers() {
|
|
516
603
|
const h = { "Content-Type": "application/json" };
|
|
@@ -528,6 +615,8 @@ var HindsightClient = class {
|
|
|
528
615
|
body: body ? JSON.stringify(body) : void 0,
|
|
529
616
|
signal: AbortSignal.timeout(15e3)
|
|
530
617
|
});
|
|
618
|
+
if (r.status === 429 && !tolerate.includes(429))
|
|
619
|
+
throw new RateLimitedError(retryAfterMs(r.headers.get("retry-after")));
|
|
531
620
|
if (!r.ok && r.status !== 404 && !tolerate.includes(r.status))
|
|
532
621
|
throw new Error(`${method} ${url} -> ${r.status} ${await r.text()}`);
|
|
533
622
|
return r;
|
|
@@ -659,7 +748,14 @@ var HindsightClient = class {
|
|
|
659
748
|
return 0;
|
|
660
749
|
}
|
|
661
750
|
}
|
|
662
|
-
/**
|
|
751
|
+
/**
|
|
752
|
+
* Poll each enqueued operation by id until terminal. LIST only shows active ops, so per-id GET is reliable.
|
|
753
|
+
*
|
|
754
|
+
* Concurrency is capped at `maxParallelRetains` (the API rate-limits bursts, not single
|
|
755
|
+
* requests — a 200 to a lone GET with 429s under `Promise.all` over every pending op). A 429
|
|
756
|
+
* leaves the op pending and backs the next cycle off by its `Retry-After` (10s floor) instead
|
|
757
|
+
* of hammering the next cycle 5s later.
|
|
758
|
+
*/
|
|
663
759
|
async drain(ids, label, maxMs = 60 * 60 * 1e3) {
|
|
664
760
|
if (!ids.length) return;
|
|
665
761
|
this.log(`[wait] draining ${ids.length} ${label} operations \u2026`);
|
|
@@ -667,23 +763,29 @@ var HindsightClient = class {
|
|
|
667
763
|
const pending = new Set(ids);
|
|
668
764
|
let failed = 0;
|
|
669
765
|
while (pending.size && Date.now() - start < maxMs) {
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
} catch {
|
|
766
|
+
let backoffMs = POLL_CYCLE_MS;
|
|
767
|
+
await pool([...pending], this.maxParallelRetains, async (id) => {
|
|
768
|
+
try {
|
|
769
|
+
const r = await fetch(this.bankUrl(`/operations/${id}`), { headers: this.headers() });
|
|
770
|
+
if (r.status === 429) {
|
|
771
|
+
backoffMs = Math.min(
|
|
772
|
+
RETRY_AFTER_CEILING_MS,
|
|
773
|
+
Math.max(backoffMs, RETRY_AFTER_FLOOR_MS, retryAfterMs(r.headers.get("retry-after")))
|
|
774
|
+
);
|
|
775
|
+
return;
|
|
681
776
|
}
|
|
682
|
-
|
|
683
|
-
|
|
777
|
+
if (!r.ok) return;
|
|
778
|
+
const st = ((await r.json()).status || "").toLowerCase();
|
|
779
|
+
if (TERMINAL.has(st)) {
|
|
780
|
+
pending.delete(id);
|
|
781
|
+
if (st !== "completed") failed++;
|
|
782
|
+
}
|
|
783
|
+
} catch {
|
|
784
|
+
}
|
|
785
|
+
});
|
|
684
786
|
if (pending.size) {
|
|
685
787
|
this.log(` \u2026 ${pending.size}/${ids.length} ${label} ops pending`);
|
|
686
|
-
await sleep(
|
|
788
|
+
await sleep(backoffMs);
|
|
687
789
|
}
|
|
688
790
|
}
|
|
689
791
|
this.log(
|
|
@@ -883,13 +985,20 @@ var HindsightClient = class {
|
|
|
883
985
|
const verb = args.relatesToPageId ? "Enhancement to an existing initiative" : "New initiative";
|
|
884
986
|
const content = `${verb}: ${args.title}. ${args.summary}`;
|
|
885
987
|
const markerId = `initiative-marker-${this.slugify(args.title)}-${Date.now()}`;
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
988
|
+
const tags = [
|
|
989
|
+
.../* @__PURE__ */ new Set([
|
|
990
|
+
...args.stamp?.tags ?? [],
|
|
991
|
+
"knowledge:feature-work",
|
|
992
|
+
`relatedPageId:${pageId}`
|
|
993
|
+
])
|
|
994
|
+
];
|
|
995
|
+
if (Object.keys(args.stamp?.metadata ?? {}).length) {
|
|
996
|
+
await this.retain(content, "initiative marker", markerId, tags, "document", {
|
|
997
|
+
metadata: args.stamp?.metadata
|
|
998
|
+
});
|
|
999
|
+
} else {
|
|
1000
|
+
await this.retain(content, "initiative marker", markerId, tags, "document");
|
|
1001
|
+
}
|
|
893
1002
|
return { page_id: pageId };
|
|
894
1003
|
}
|
|
895
1004
|
/** Find a root folder by name (case-insensitive) or create it; returns its id. Fail-open to undefined. */
|
|
@@ -962,16 +1071,29 @@ This tool guide and the page list are re-injected for you periodically as things
|
|
|
962
1071
|
}
|
|
963
1072
|
|
|
964
1073
|
// src/core/session-cache.ts
|
|
965
|
-
import { mkdirSync as mkdirSync2, readFileSync as readFileSync2, writeFileSync } from "fs";
|
|
1074
|
+
import { mkdirSync as mkdirSync2, readFileSync as readFileSync2, renameSync, rmSync, writeFileSync } from "fs";
|
|
966
1075
|
import { tmpdir as tmpdir2 } from "os";
|
|
967
1076
|
import { dirname as dirname4, join as join5 } from "path";
|
|
968
1077
|
function sessionCacheFile(harness, sessionId) {
|
|
969
1078
|
return join5(tmpdir2(), `hindsight-${harness}`, `${sessionId}.json`);
|
|
970
1079
|
}
|
|
1080
|
+
function writeFileAtomic(path, body) {
|
|
1081
|
+
mkdirSync2(dirname4(path), { recursive: true });
|
|
1082
|
+
const tmp = `${path}.${process.pid}.tmp`;
|
|
1083
|
+
try {
|
|
1084
|
+
writeFileSync(tmp, body);
|
|
1085
|
+
renameSync(tmp, path);
|
|
1086
|
+
} catch (e) {
|
|
1087
|
+
try {
|
|
1088
|
+
rmSync(tmp, { force: true });
|
|
1089
|
+
} catch {
|
|
1090
|
+
}
|
|
1091
|
+
throw e;
|
|
1092
|
+
}
|
|
1093
|
+
}
|
|
971
1094
|
function writeSessionCache(cacheFile, cache) {
|
|
972
1095
|
try {
|
|
973
|
-
|
|
974
|
-
writeFileSync(cacheFile, JSON.stringify(cache));
|
|
1096
|
+
writeFileAtomic(cacheFile, JSON.stringify(cache));
|
|
975
1097
|
} catch {
|
|
976
1098
|
}
|
|
977
1099
|
}
|
|
@@ -1103,6 +1225,50 @@ function preflightDaemon(cfg, harness) {
|
|
|
1103
1225
|
|
|
1104
1226
|
// src/core/retain-stamp.ts
|
|
1105
1227
|
import { basename as basename2 } from "path";
|
|
1228
|
+
var RESERVED_TAG_PREFIX = /^(source|harness):/;
|
|
1229
|
+
function resolversFor(ctx) {
|
|
1230
|
+
return {
|
|
1231
|
+
// Worktree-aware like the bank id, so every linked worktree of a repo stamps the SAME name —
|
|
1232
|
+
// otherwise a shared bank ends up with `project:app` and `project:app-wt2` for one repository.
|
|
1233
|
+
gitProject: () => projectNameOf(ctx.directory),
|
|
1234
|
+
project: () => ctx.directory ? basename2(ctx.directory) : "unknown",
|
|
1235
|
+
harness: () => ctx.harness,
|
|
1236
|
+
bankId: () => ctx.bankId,
|
|
1237
|
+
sessionId: () => ctx.sessionId ?? "unknown",
|
|
1238
|
+
timestamp: () => (/* @__PURE__ */ new Date()).toISOString(),
|
|
1239
|
+
channel: () => process.env.HINDSIGHT_CHANNEL_ID || "default",
|
|
1240
|
+
user: () => process.env.HINDSIGHT_USER_ID || "anonymous"
|
|
1241
|
+
};
|
|
1242
|
+
}
|
|
1243
|
+
function buildRetainStamp(cfg, ctx) {
|
|
1244
|
+
const hasTags = Boolean(cfg.retainTags?.length);
|
|
1245
|
+
const hasMetadata = Boolean(cfg.retainMetadata && Object.keys(cfg.retainMetadata).length);
|
|
1246
|
+
if (!hasTags && !hasMetadata) return { tags: [], metadata: {} };
|
|
1247
|
+
const base = resolversFor(ctx);
|
|
1248
|
+
const cache = /* @__PURE__ */ new Map();
|
|
1249
|
+
const resolvers = Object.fromEntries(
|
|
1250
|
+
Object.entries(base).map(([name, resolve2]) => [
|
|
1251
|
+
name,
|
|
1252
|
+
() => {
|
|
1253
|
+
const hit = cache.get(name);
|
|
1254
|
+
if (hit !== void 0) return hit;
|
|
1255
|
+
const value = resolve2();
|
|
1256
|
+
cache.set(name, value);
|
|
1257
|
+
return value;
|
|
1258
|
+
}
|
|
1259
|
+
])
|
|
1260
|
+
);
|
|
1261
|
+
const tags = (cfg.retainTags ?? []).map((t) => applyTemplate(t, resolvers, "retainTags").trim()).filter(Boolean).filter((t) => {
|
|
1262
|
+
if (!RESERVED_TAG_PREFIX.test(t)) return true;
|
|
1263
|
+
log.warn("retain-stamp", "ignoring reserved retainTags entry", { tag: t });
|
|
1264
|
+
return false;
|
|
1265
|
+
});
|
|
1266
|
+
const metadata = {};
|
|
1267
|
+
for (const [key, value] of Object.entries(cfg.retainMetadata ?? {})) {
|
|
1268
|
+
metadata[key] = applyTemplate(value, resolvers, "retainMetadata");
|
|
1269
|
+
}
|
|
1270
|
+
return { tags, metadata };
|
|
1271
|
+
}
|
|
1106
1272
|
|
|
1107
1273
|
// src/core/jsonl.ts
|
|
1108
1274
|
import { closeSync, openSync, readSync, statSync } from "fs";
|
|
@@ -1237,7 +1403,7 @@ import { execFileSync as execFileSync4 } from "child_process";
|
|
|
1237
1403
|
|
|
1238
1404
|
// src/core/survey.ts
|
|
1239
1405
|
import { spawn as realSpawn3 } from "child_process";
|
|
1240
|
-
import { accessSync, constants, existsSync as
|
|
1406
|
+
import { accessSync, constants, existsSync as existsSync3 } from "fs";
|
|
1241
1407
|
import { homedir as homedir3 } from "os";
|
|
1242
1408
|
import { delimiter, dirname as dirname6, join as join7 } from "path";
|
|
1243
1409
|
import { fileURLToPath as fileURLToPath3 } from "url";
|
|
@@ -1252,7 +1418,7 @@ function resolveClaudeBin(explicit) {
|
|
|
1252
1418
|
if (process.env.HINDSIGHT_CLAUDE_BIN) return process.env.HINDSIGHT_CLAUDE_BIN;
|
|
1253
1419
|
const nativeInstallPath = join7(homedir3(), ".claude", "local", "claude");
|
|
1254
1420
|
try {
|
|
1255
|
-
if (
|
|
1421
|
+
if (existsSync3(nativeInstallPath)) return nativeInstallPath;
|
|
1256
1422
|
} catch {
|
|
1257
1423
|
}
|
|
1258
1424
|
return "claude";
|
|
@@ -1426,7 +1592,7 @@ function startCodebaseSurvey(repoDir, opts = {}) {
|
|
|
1426
1592
|
var DEEPEN_DIFF_TARGET = 300;
|
|
1427
1593
|
|
|
1428
1594
|
// src/core/skill-sync.ts
|
|
1429
|
-
import { cpSync, existsSync as
|
|
1595
|
+
import { cpSync, existsSync as existsSync4, readFileSync as readFileSync5 } from "fs";
|
|
1430
1596
|
import { homedir as homedir4 } from "os";
|
|
1431
1597
|
import { dirname as dirname7, join as join8 } from "path";
|
|
1432
1598
|
import { fileURLToPath as fileURLToPath4 } from "url";
|
|
@@ -1446,10 +1612,10 @@ function syncCompanionSkill(harness, opts = {}) {
|
|
|
1446
1612
|
if (!parts) return;
|
|
1447
1613
|
const src = opts.srcDir ?? packagedSkillDir();
|
|
1448
1614
|
const srcMd = join8(src, "SKILL.md");
|
|
1449
|
-
if (!
|
|
1615
|
+
if (!existsSync4(srcMd)) return;
|
|
1450
1616
|
const dst = join8(opts.home ?? homedir4(), ...parts, "hindsight-coding-agent");
|
|
1451
1617
|
const dstMd = join8(dst, "SKILL.md");
|
|
1452
|
-
if (!
|
|
1618
|
+
if (!existsSync4(dstMd)) return;
|
|
1453
1619
|
if (readFileSync5(srcMd, "utf8") !== readFileSync5(dstMd, "utf8")) {
|
|
1454
1620
|
cpSync(src, dst, { recursive: true });
|
|
1455
1621
|
}
|
|
@@ -1495,20 +1661,25 @@ async function buildSessionStartContext(args) {
|
|
|
1495
1661
|
const startSurvey = args.startSurvey ?? startCodebaseSurvey;
|
|
1496
1662
|
const resolveHeadSha = args.headSha ?? gitHeadSha;
|
|
1497
1663
|
const countCommitsSince = args.commitsSince ?? commitsSince;
|
|
1664
|
+
const retainStamp = () => buildRetainStamp(cfg, { directory: cwd, harness, bankId });
|
|
1498
1665
|
const SURVEY_BASELINE_TAG = "source:survey-baseline";
|
|
1499
1666
|
const SURVEY_BASELINE_PREFIX = "survey-baseline:";
|
|
1500
1667
|
const recordSurveyBaseline = (sha) => {
|
|
1501
1668
|
if (!client.retain) return;
|
|
1502
1669
|
try {
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1670
|
+
const stamp = retainStamp();
|
|
1671
|
+
const content = `\u{1F6F0}\uFE0F Hindsight is researching this codebase \u2014 survey started at commit ${sha.slice(0, 12)}. (Internal marker: no memories are extracted from this document.)`;
|
|
1672
|
+
const tags = [.../* @__PURE__ */ new Set([...stamp.tags, SURVEY_BASELINE_TAG])];
|
|
1673
|
+
const retained = client.retain(
|
|
1674
|
+
content,
|
|
1675
|
+
"hindsight codebase-survey baseline",
|
|
1676
|
+
`${SURVEY_BASELINE_PREFIX}${sha}`,
|
|
1677
|
+
tags,
|
|
1678
|
+
"survey",
|
|
1679
|
+
// `retain` only sets metadata when it is truthy, so an empty stamp sends none.
|
|
1680
|
+
{ metadata: Object.keys(stamp.metadata).length ? stamp.metadata : void 0 }
|
|
1681
|
+
);
|
|
1682
|
+
void Promise.resolve(retained).catch(() => {
|
|
1512
1683
|
});
|
|
1513
1684
|
} catch {
|
|
1514
1685
|
}
|
|
@@ -1614,12 +1785,17 @@ async function runSessionStartHook(spec, makeClient = (o) => new HindsightClient
|
|
|
1614
1785
|
setLogLevel(cfg.logLevel);
|
|
1615
1786
|
syncCompanionSkill(harness);
|
|
1616
1787
|
if (cfg.disabled) return;
|
|
1617
|
-
const resolved = applyBankConfig(cfg, deriveBankId(cfg, cwd, harness));
|
|
1788
|
+
const resolved = applyBankConfig(cfg, deriveBankId(cfg, cwd, harness), cwd);
|
|
1618
1789
|
cfg = resolved.cfg;
|
|
1619
1790
|
const bankId = resolved.bankId;
|
|
1620
1791
|
if (cfg.disabled) return;
|
|
1621
1792
|
await ensureDaemon(cfg, harness, { waitMs: DAEMON_WAIT_SESSION_START_MS });
|
|
1622
|
-
const client = makeClient({
|
|
1793
|
+
const client = makeClient({
|
|
1794
|
+
apiUrl: cfg.apiUrl,
|
|
1795
|
+
apiToken: cfg.apiToken,
|
|
1796
|
+
bank: bankId,
|
|
1797
|
+
maxParallelRetains: cfg.maxParallelRetains
|
|
1798
|
+
});
|
|
1623
1799
|
const out = await buildSessionStartContext({ cwd, bankId, cfg, client, harness });
|
|
1624
1800
|
if (out.deferInitialReflect && sessionId) {
|
|
1625
1801
|
writeSessionCache(sessionCacheFile(harness, sessionId), { deferInitialReflect: true });
|
|
@@ -1762,14 +1938,14 @@ function readCopilotTranscript(path) {
|
|
|
1762
1938
|
}
|
|
1763
1939
|
|
|
1764
1940
|
// src/core/transcript-grok.ts
|
|
1765
|
-
import { existsSync as
|
|
1941
|
+
import { existsSync as existsSync5, readFileSync as readFileSync7, readdirSync } from "fs";
|
|
1766
1942
|
import { homedir as homedir5 } from "os";
|
|
1767
1943
|
import { join as join9 } from "path";
|
|
1768
1944
|
var CHAT_HISTORY = "chat_history.jsonl";
|
|
1769
1945
|
function grokTranscriptPath(cwd, sessionId, grokHome = process.env.GROK_HOME || join9(homedir5(), ".grok")) {
|
|
1770
1946
|
const sessionsDir = join9(grokHome, "sessions");
|
|
1771
1947
|
const direct = join9(sessionsDir, encodeURIComponent(cwd), sessionId, CHAT_HISTORY);
|
|
1772
|
-
if (
|
|
1948
|
+
if (existsSync5(direct)) return direct;
|
|
1773
1949
|
try {
|
|
1774
1950
|
for (const candidate of readdirSync(sessionsDir)) {
|
|
1775
1951
|
const directory = join9(sessionsDir, candidate);
|
|
@@ -1809,7 +1985,7 @@ function readGrokTranscript(path) {
|
|
|
1809
1985
|
}
|
|
1810
1986
|
|
|
1811
1987
|
// src/core/transcript-devin.ts
|
|
1812
|
-
import { existsSync as
|
|
1988
|
+
import { existsSync as existsSync6 } from "fs";
|
|
1813
1989
|
import { createRequire } from "module";
|
|
1814
1990
|
import { homedir as homedir6 } from "os";
|
|
1815
1991
|
import { join as join10 } from "path";
|
|
@@ -1851,7 +2027,7 @@ function readDevinTranscript(sessionId, dbPath = devinSessionDb()) {
|
|
|
1851
2027
|
diag("devin-cli", "sqlite_unavailable", { node: process.version, dbPath });
|
|
1852
2028
|
return [];
|
|
1853
2029
|
}
|
|
1854
|
-
if (!
|
|
2030
|
+
if (!existsSync6(dbPath)) {
|
|
1855
2031
|
diag("devin-cli", "session_db_missing", { dbPath });
|
|
1856
2032
|
return [];
|
|
1857
2033
|
}
|