@vectorize-io/hindsight-coding-agents 0.0.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 +334 -0
- package/dist/antigravity-hook.js +1556 -0
- package/dist/antigravity-statusline.js +194 -0
- package/dist/antigravity-stop-hook.js +1453 -0
- package/dist/claude-hook.js +1556 -0
- package/dist/claude-sessionstart-hook.js +1814 -0
- package/dist/claude-stop-hook.js +1453 -0
- package/dist/cline.js +16373 -0
- package/dist/codex-hook.js +1556 -0
- package/dist/codex-sessionstart-hook.js +1814 -0
- package/dist/codex-stop-hook.js +1453 -0
- package/dist/copilot-hook.js +1556 -0
- package/dist/copilot-sessionstart-hook.js +1814 -0
- package/dist/copilot-stop-hook.js +1453 -0
- package/dist/cursor-hook.js +1556 -0
- package/dist/cursor-sessionstart-hook.js +1814 -0
- package/dist/cursor-stop-hook.js +1453 -0
- package/dist/deepen.js +1175 -0
- package/dist/devin-hook.js +1556 -0
- package/dist/devin-sessionstart-hook.js +1814 -0
- package/dist/devin-stop-hook.js +1453 -0
- package/dist/grok-hook.js +1556 -0
- package/dist/grok-sessionstart-hook.js +1814 -0
- package/dist/grok-stop-hook.js +1453 -0
- package/dist/hindsight-seed.js +180 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +28853 -0
- package/dist/installer.js +1313 -0
- package/dist/kilo.js +28853 -0
- package/dist/mcp-server.js +31888 -0
- package/dist/status.js +793 -0
- package/package.json +65 -0
- package/skill/SKILL.md +122 -0
package/dist/deepen.js
ADDED
|
@@ -0,0 +1,1175 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// src/deepen.ts
|
|
4
|
+
import { execFileSync as execFileSync4 } from "child_process";
|
|
5
|
+
import { mkdirSync as mkdirSync2, readFileSync as readFileSync3, unlinkSync, writeFileSync } from "fs";
|
|
6
|
+
import { tmpdir as tmpdir2 } from "os";
|
|
7
|
+
import { join as join6 } from "path";
|
|
8
|
+
|
|
9
|
+
// src/core/bank.ts
|
|
10
|
+
import { execFileSync } from "child_process";
|
|
11
|
+
import { homedir } from "os";
|
|
12
|
+
import { basename, dirname, join, normalize, sep } from "path";
|
|
13
|
+
var DEFAULT_BANK_NAME = "coding";
|
|
14
|
+
var DEFAULT_TEMPLATE = "coding-agent::{gitProject}";
|
|
15
|
+
var PLACEHOLDER = /\{([a-zA-Z]+)\}/g;
|
|
16
|
+
function getProjectRootFromGit(directory) {
|
|
17
|
+
if (!directory) return null;
|
|
18
|
+
try {
|
|
19
|
+
const commonDir = execFileSync(
|
|
20
|
+
"git",
|
|
21
|
+
["rev-parse", "--path-format=absolute", "--git-common-dir"],
|
|
22
|
+
{ cwd: directory, encoding: "utf-8", stdio: ["ignore", "pipe", "ignore"], timeout: 1e3 }
|
|
23
|
+
).trim();
|
|
24
|
+
if (!commonDir) return null;
|
|
25
|
+
return basename(commonDir) === ".git" ? dirname(commonDir) : commonDir;
|
|
26
|
+
} catch {
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
function projectNameOf(directory) {
|
|
31
|
+
return gitProjectName(directory, true);
|
|
32
|
+
}
|
|
33
|
+
function gitProjectName(directory, resolveWorktrees) {
|
|
34
|
+
if (resolveWorktrees) {
|
|
35
|
+
const root = getProjectRootFromGit(directory);
|
|
36
|
+
if (root) return basename(root);
|
|
37
|
+
}
|
|
38
|
+
return directory ? basename(directory) : "unknown";
|
|
39
|
+
}
|
|
40
|
+
function mapLookup(map, directory) {
|
|
41
|
+
const cwd = normalize(directory);
|
|
42
|
+
let best;
|
|
43
|
+
for (const [dir, bank] of Object.entries(map)) {
|
|
44
|
+
const expanded = dir === "~" || dir.startsWith("~/") ? join(homedir(), dir.slice(1)) : dir;
|
|
45
|
+
const p = normalize(expanded).replace(new RegExp(`\\${sep}+$`), "");
|
|
46
|
+
if (cwd === p || cwd.startsWith(p + sep)) {
|
|
47
|
+
if (!best || p.length > best.len) best = { len: p.length, bank };
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return best?.bank;
|
|
51
|
+
}
|
|
52
|
+
function deriveBankId(config, directory, harness = "coding") {
|
|
53
|
+
const mapped = directory && config.mapPathToBank ? mapLookup(config.mapPathToBank, directory) : void 0;
|
|
54
|
+
if (mapped) return mapped;
|
|
55
|
+
const dynamic = config.dynamicBankId ?? !config.bankId;
|
|
56
|
+
if (!dynamic) return config.bankId || DEFAULT_BANK_NAME;
|
|
57
|
+
const resolvers = {
|
|
58
|
+
harness: () => harness,
|
|
59
|
+
project: () => directory ? basename(directory) : "unknown",
|
|
60
|
+
gitProject: () => gitProjectName(directory, config.resolveWorktrees ?? true),
|
|
61
|
+
channel: () => process.env.HINDSIGHT_CHANNEL_ID || "default",
|
|
62
|
+
user: () => process.env.HINDSIGHT_USER_ID || "anonymous"
|
|
63
|
+
};
|
|
64
|
+
return (config.bankIdTemplate || DEFAULT_TEMPLATE).replace(PLACEHOLDER, (_, name) => {
|
|
65
|
+
const r = resolvers[name];
|
|
66
|
+
if (!r) {
|
|
67
|
+
console.error(
|
|
68
|
+
`hindsight: unknown bankIdTemplate placeholder "{${name}}" \u2014 valid: ` + Object.keys(resolvers).sort().map((k) => `{${k}}`).join(", ")
|
|
69
|
+
);
|
|
70
|
+
return "unknown";
|
|
71
|
+
}
|
|
72
|
+
return r();
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// src/core/util.ts
|
|
77
|
+
var sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
78
|
+
async function pool(items, n, fn, onError, onProgress) {
|
|
79
|
+
let i = 0, done = 0;
|
|
80
|
+
async function worker() {
|
|
81
|
+
while (i < items.length) {
|
|
82
|
+
const idx = i++;
|
|
83
|
+
try {
|
|
84
|
+
await fn(items[idx], idx);
|
|
85
|
+
} catch (e) {
|
|
86
|
+
onError?.(idx, e);
|
|
87
|
+
}
|
|
88
|
+
onProgress?.(++done, items.length);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
await Promise.all(Array.from({ length: Math.min(n, items.length) }, worker));
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// src/core/chat.ts
|
|
95
|
+
function withRefId(refId, turns, baseTs) {
|
|
96
|
+
return [{ role: "system", content: `REF-ID: ${refId}`, timestamp: baseTs }, ...turns];
|
|
97
|
+
}
|
|
98
|
+
async function ingestChats(client, sessions, opts = {}) {
|
|
99
|
+
const log3 = opts.log ?? (() => {
|
|
100
|
+
});
|
|
101
|
+
if (!sessions.length) {
|
|
102
|
+
log3("[chat] no sessions; skipping");
|
|
103
|
+
return 0;
|
|
104
|
+
}
|
|
105
|
+
log3(`[chat] ingesting ${sessions.length} chats (RAW, JSONL transcript \u2014 one turn per line) \u2026`);
|
|
106
|
+
const NOW = Date.now();
|
|
107
|
+
let failures = 0;
|
|
108
|
+
await pool(
|
|
109
|
+
sessions,
|
|
110
|
+
opts.concurrency ?? 8,
|
|
111
|
+
async (s, i) => {
|
|
112
|
+
const id = s.id || `s${i}`;
|
|
113
|
+
const sessBase = NOW - (sessions.length - 1 - i) * 36e5;
|
|
114
|
+
const baseIso = new Date(sessBase).toISOString();
|
|
115
|
+
const turns = withRefId(
|
|
116
|
+
`chat:${id}`,
|
|
117
|
+
(s.turns || []).map((t, j) => ({
|
|
118
|
+
role: t.role,
|
|
119
|
+
content: t.text,
|
|
120
|
+
timestamp: t.timestamp || new Date(sessBase + (j + 1) * 6e4).toISOString()
|
|
121
|
+
})),
|
|
122
|
+
baseIso
|
|
123
|
+
);
|
|
124
|
+
await client.retain(
|
|
125
|
+
turns.map((x) => JSON.stringify(x)).join("\n"),
|
|
126
|
+
"developer chat",
|
|
127
|
+
`chat:${id}`,
|
|
128
|
+
["source:chat"],
|
|
129
|
+
"conversation",
|
|
130
|
+
{
|
|
131
|
+
timestamp: baseIso,
|
|
132
|
+
metadata: { source: "chat", chat: id, ref_id: `chat:${id}` }
|
|
133
|
+
}
|
|
134
|
+
);
|
|
135
|
+
},
|
|
136
|
+
(i, e) => {
|
|
137
|
+
failures++;
|
|
138
|
+
log3(` ! chat ${i} failed to enqueue: ${e.message?.slice(0, 120)}`);
|
|
139
|
+
}
|
|
140
|
+
);
|
|
141
|
+
log3(`[chat] done: ${sessions.length} chats ingested (JSONL) under strategy 'chat'`);
|
|
142
|
+
return failures;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// src/core/config.ts
|
|
146
|
+
import { readFileSync } from "fs";
|
|
147
|
+
import { homedir as homedir2 } from "os";
|
|
148
|
+
import { join as join3 } from "path";
|
|
149
|
+
|
|
150
|
+
// src/core/seed.ts
|
|
151
|
+
import { spawn as realSpawn } from "child_process";
|
|
152
|
+
import { dirname as dirname2, join as join2 } from "path";
|
|
153
|
+
import { fileURLToPath } from "url";
|
|
154
|
+
var DEFAULT_SEED_LIMIT = 300;
|
|
155
|
+
|
|
156
|
+
// src/core/config.ts
|
|
157
|
+
var CONFIG_PATH = process.env.HINDSIGHT_CONFIG || join3(homedir2(), ".hindsight", "coding-agent.json");
|
|
158
|
+
function resolveConfig(raw = {}) {
|
|
159
|
+
return {
|
|
160
|
+
apiUrl: raw.apiUrl ?? "https://api.hindsight.vectorize.io",
|
|
161
|
+
apiToken: raw.apiToken || void 0,
|
|
162
|
+
bankId: raw.bankId,
|
|
163
|
+
dynamicBankId: raw.dynamicBankId,
|
|
164
|
+
bankIdTemplate: raw.bankIdTemplate,
|
|
165
|
+
mapPathToBank: raw.mapPathToBank,
|
|
166
|
+
resolveWorktrees: raw.resolveWorktrees,
|
|
167
|
+
harness: raw.harness ?? "opencode",
|
|
168
|
+
disabled: raw.disabled ?? false,
|
|
169
|
+
retainSessions: raw.retainSessions ?? true,
|
|
170
|
+
// opencode: write back by default (parity with hook-harness Stop)
|
|
171
|
+
retainEveryTurns: raw.retainEveryTurns || 1,
|
|
172
|
+
reflectTimeoutMs: raw.reflectTimeoutMs || 12e4,
|
|
173
|
+
autoReflect: raw.autoReflect ?? true,
|
|
174
|
+
pageRefreshEveryTurns: raw.pageRefreshEveryTurns || 10,
|
|
175
|
+
autoSeed: raw.autoSeed ?? true,
|
|
176
|
+
seedLimit: raw.seedLimit || DEFAULT_SEED_LIMIT,
|
|
177
|
+
codebaseSurvey: raw.codebaseSurvey ?? true,
|
|
178
|
+
surveyModel: raw.surveyModel || "haiku",
|
|
179
|
+
surveyBudgetUsd: raw.surveyBudgetUsd || 2,
|
|
180
|
+
surveyRefreshCommits: raw.surveyRefreshCommits ?? 20,
|
|
181
|
+
gitIngest: ["message", "full", "none"].includes(raw.gitIngest) ? raw.gitIngest : "message",
|
|
182
|
+
banks: raw.banks && typeof raw.banks === "object" ? raw.banks : {},
|
|
183
|
+
logLevel: ["debug", "info", "warn", "error"].includes(raw.logLevel) ? raw.logLevel : "info"
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
function readRaw(path) {
|
|
187
|
+
try {
|
|
188
|
+
return JSON.parse(readFileSync(path, "utf8"));
|
|
189
|
+
} catch (e) {
|
|
190
|
+
if (e?.code !== "ENOENT") {
|
|
191
|
+
console.error(`hindsight: ignoring invalid config at ${path}: ${e?.message || e}`);
|
|
192
|
+
}
|
|
193
|
+
return {};
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
function mergeRaw(a, b) {
|
|
197
|
+
const { harnesses: _drop, ...flat } = b;
|
|
198
|
+
return { ...a, ...flat, banks: { ...a.banks ?? {}, ...b.banks ?? {} } };
|
|
199
|
+
}
|
|
200
|
+
function applyLayer(raw, layer, harness) {
|
|
201
|
+
let out = mergeRaw(raw, layer);
|
|
202
|
+
const perHarness = harness ? layer.harnesses?.[harness] : void 0;
|
|
203
|
+
if (perHarness) out = mergeRaw(out, perHarness);
|
|
204
|
+
return out;
|
|
205
|
+
}
|
|
206
|
+
function loadConfig(opts = {}) {
|
|
207
|
+
const o = typeof opts === "string" ? { path: opts } : opts;
|
|
208
|
+
return resolveConfig(applyLayer({}, readRaw(o.path ?? CONFIG_PATH), o.harness));
|
|
209
|
+
}
|
|
210
|
+
var BANK_OVERRIDE_EXCLUDED = [
|
|
211
|
+
"bankId",
|
|
212
|
+
"bankIdTemplate",
|
|
213
|
+
"mapPathToBank",
|
|
214
|
+
"dynamicBankId",
|
|
215
|
+
"resolveWorktrees",
|
|
216
|
+
"harness"
|
|
217
|
+
];
|
|
218
|
+
function applyBankConfig(cfg2, resolvedId) {
|
|
219
|
+
const section = cfg2.banks[resolvedId];
|
|
220
|
+
if (!section) return { cfg: cfg2, bankId: resolvedId };
|
|
221
|
+
const safe = { ...section };
|
|
222
|
+
for (const k of BANK_OVERRIDE_EXCLUDED) delete safe[k];
|
|
223
|
+
delete safe.banks;
|
|
224
|
+
const bankId = typeof safe.bank === "string" && safe.bank ? safe.bank : resolvedId;
|
|
225
|
+
delete safe.bank;
|
|
226
|
+
return { cfg: { ...cfg2, ...resolvePartial(cfg2, safe) }, bankId };
|
|
227
|
+
}
|
|
228
|
+
function resolvePartial(cfg2, patch) {
|
|
229
|
+
const full = resolveConfig(patch);
|
|
230
|
+
const out = {};
|
|
231
|
+
for (const key of Object.keys(patch)) {
|
|
232
|
+
if (key in full)
|
|
233
|
+
out[key] = full[key];
|
|
234
|
+
}
|
|
235
|
+
return out;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// src/core/git.ts
|
|
239
|
+
import { execFileSync as execFileSync2 } from "child_process";
|
|
240
|
+
import { resolve } from "path";
|
|
241
|
+
var US = "";
|
|
242
|
+
var RS = "";
|
|
243
|
+
function git(repo, ...args) {
|
|
244
|
+
return execFileSync2("git", ["-C", repo, ...args], { encoding: "utf8", maxBuffer: 1 << 28 });
|
|
245
|
+
}
|
|
246
|
+
function repoNameOf(repo) {
|
|
247
|
+
return projectNameOf(resolve(repo));
|
|
248
|
+
}
|
|
249
|
+
function gitHeadSha(dir) {
|
|
250
|
+
try {
|
|
251
|
+
return git(dir, "rev-parse", "HEAD").trim() || null;
|
|
252
|
+
} catch {
|
|
253
|
+
return null;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
function commitsSince(dir, sinceSha) {
|
|
257
|
+
try {
|
|
258
|
+
const n = Number.parseInt(git(dir, "rev-list", "--count", `${sinceSha}..HEAD`).trim(), 10);
|
|
259
|
+
return Number.isFinite(n) ? n : null;
|
|
260
|
+
} catch {
|
|
261
|
+
return null;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
async function retainCommit(client, repo, sha, repoName) {
|
|
265
|
+
const [h, an, ae, aISO, cISO, subj, body] = git(
|
|
266
|
+
repo,
|
|
267
|
+
"show",
|
|
268
|
+
"-s",
|
|
269
|
+
`--format=%H${US}%an${US}%ae${US}%aI${US}%cI${US}%s${US}%b`,
|
|
270
|
+
sha
|
|
271
|
+
).split(US);
|
|
272
|
+
const msg = (subj + (body?.trim() ? "\n\n" + body.trim() : "")).trim();
|
|
273
|
+
const diff = git(repo, "show", "--format=", sha);
|
|
274
|
+
const content = `REF-ID: git:${sha.slice(0, 12)}
|
|
275
|
+
Git commit ${sha.slice(0, 12)} in the ${repoName} repository (${an}, ${aISO}).
|
|
276
|
+
|
|
277
|
+
Message:
|
|
278
|
+
${msg}
|
|
279
|
+
|
|
280
|
+
Diff:
|
|
281
|
+
${diff}`;
|
|
282
|
+
await client.retain(content, `git commit in ${repoName}`, `git:${sha}`, ["source:git"], "git", {
|
|
283
|
+
timestamp: aISO,
|
|
284
|
+
// the memory's timestamp is the commit's author date
|
|
285
|
+
metadata: {
|
|
286
|
+
source: "git",
|
|
287
|
+
repo: repoName,
|
|
288
|
+
commit: h,
|
|
289
|
+
short_sha: sha.slice(0, 12),
|
|
290
|
+
author: an,
|
|
291
|
+
author_email: ae,
|
|
292
|
+
authored_at: aISO,
|
|
293
|
+
committed_at: cISO,
|
|
294
|
+
subject: subj
|
|
295
|
+
}
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
function gitLogText(repo, limit) {
|
|
299
|
+
let raw;
|
|
300
|
+
try {
|
|
301
|
+
raw = git(
|
|
302
|
+
repo,
|
|
303
|
+
"log",
|
|
304
|
+
`-n`,
|
|
305
|
+
String(limit),
|
|
306
|
+
"--no-merges",
|
|
307
|
+
"--date=short",
|
|
308
|
+
`--format=%h${US}%ad${US}%an${US}%s${US}%b${RS}`
|
|
309
|
+
);
|
|
310
|
+
} catch {
|
|
311
|
+
return "";
|
|
312
|
+
}
|
|
313
|
+
return raw.split(RS).map((rec) => rec.trim()).filter(Boolean).map((rec) => {
|
|
314
|
+
const [sha, date, author, subj, body] = rec.split(US);
|
|
315
|
+
const header = `${sha} ${date} ${author}`;
|
|
316
|
+
const msg = subj + (body?.trim() ? "\n\n" + body.trim() : "");
|
|
317
|
+
return `${header}
|
|
318
|
+
${msg}`;
|
|
319
|
+
}).join("\n\n---\n\n");
|
|
320
|
+
}
|
|
321
|
+
async function ingestGitLog(client, repo, opts = { limit: 300 }) {
|
|
322
|
+
const log3 = opts.log ?? (() => {
|
|
323
|
+
});
|
|
324
|
+
const text = gitLogText(repo, opts.limit);
|
|
325
|
+
if (!text) {
|
|
326
|
+
log3("[gitlog] no commits found \u2014 skipping");
|
|
327
|
+
return 0;
|
|
328
|
+
}
|
|
329
|
+
const repoName = repoNameOf(repo);
|
|
330
|
+
const n = text.split("\n\n---\n\n").length;
|
|
331
|
+
log3(`[gitlog] ingesting last ${n} commit messages for ${repoName} as ONE document \u2026`);
|
|
332
|
+
try {
|
|
333
|
+
const head = gitHeadSha(repo);
|
|
334
|
+
await client.retain(
|
|
335
|
+
text,
|
|
336
|
+
`git commit-message history (last ${n}) for ${repoName}`,
|
|
337
|
+
`gitlog:${repoName}`,
|
|
338
|
+
["source:git", "source:git-log", ...head ? [`gitlog-head:${head}`] : []],
|
|
339
|
+
"gitlog",
|
|
340
|
+
{ async: true }
|
|
341
|
+
);
|
|
342
|
+
log3(`[gitlog] done: ${n} commit messages ingested as 1 document under strategy 'gitlog'`);
|
|
343
|
+
return 0;
|
|
344
|
+
} catch (e) {
|
|
345
|
+
log3(` ! gitlog ingest failed: ${e.message?.slice(0, 120)}`);
|
|
346
|
+
return 1;
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
// src/core/survey.ts
|
|
351
|
+
import { spawn as realSpawn2 } from "child_process";
|
|
352
|
+
import { accessSync, constants, existsSync } from "fs";
|
|
353
|
+
import { homedir as homedir3 } from "os";
|
|
354
|
+
import { delimiter, dirname as dirname3, join as join4 } from "path";
|
|
355
|
+
import { fileURLToPath as fileURLToPath2 } from "url";
|
|
356
|
+
var SURVEY_DOC_IDS = [
|
|
357
|
+
"repository-component-map",
|
|
358
|
+
"repository-core-concepts",
|
|
359
|
+
"repository-conventions-and-patterns",
|
|
360
|
+
"repository-tech-stack-and-features"
|
|
361
|
+
];
|
|
362
|
+
|
|
363
|
+
// src/core/missions.ts
|
|
364
|
+
var GIT_MISSION = "You are ingesting a single git commit: its message and its full diff. Extract the concrete technical DECISION and the CAUSE/INVARIANT it encodes, bound to the specific code entities (functions, methods, files) and behaviors it changes. Preserve exact identifiers, paths, and literal values verbatim. Preserve the 'REF-ID: <token>' marker verbatim in every fact. Capture both WHAT changed and WHY. Issue/PR references (#123, GH-123, PROJ-123) are load-bearing: keep them VERBATIM in the fact text and emit each as an ENTITY, so a later question about that issue or PR retrieves this decision.";
|
|
365
|
+
var GITLOG_MISSION = "You are ingesting an aggregated block of git commit MESSAGES ONLY (no diffs) \u2014 the project's recent commit-message history, newest first. Extract the project's INITIATIVES, FEATURES, ENHANCEMENTS, and notable changes or THEMES over time \u2014 what the project has been working on and how it has evolved. Do NOT extract per-line code detail (there is no diff to draw it from). Group related commits into a coherent initiative/theme where the messages make that clear; preserve exact identifiers and literal values verbatim when quoting a subject line. Keep issue/PR references (#123, GH-123) VERBATIM and emit each as an ENTITY \u2014 they are how future sessions will ask about this work.";
|
|
366
|
+
var CONVERSATION_MISSION = `You are ingesting a developer conversation as a JSONL transcript (one {role, content} turn per line): the user's requests, the assistant's narration, and compact 'action' turns naming each tool use and its target (e.g. "Edit boltons/strutils.py") with no arguments or outputs. It may be a SHORT decision chat or a LONG working session \u2014 scale the facts to the substance, never to the message count. Extract the FEWEST facts that capture the OUTCOME: the settled DECISIONS and their exact rules/values (quote literals VERBATIM); concrete CHANGES to specific code entities; problems and how they were resolved; conventions or invariants established; at most one fact for a notable REJECTED alternative ('initially proposed X, changed to Y because Z'). A short decision chat usually yields 1-2 facts; a substantial working session several. CRITICAL: a conversation REVISES itself \u2014 record ONLY the FINAL state as what is in effect; a superseded proposal appears ONLY inside the rejected fact, NEVER as its own 'decided' fact; if the same setting changes several times keep only the LAST, and make unmistakably clear which choice WON. Do NOT emit one fact per message, per intermediate proposal, or per action turn. Keep issue/PR references (#123, GH-123) VERBATIM and emit each as an ENTITY. Preserve the 'REF-ID: <token>' marker verbatim in every fact. Do not invent; capture only what was actually settled.`;
|
|
367
|
+
var REFLECT_MISSION = "You are a debugging assistant with the project's past decisions in memory (git rationale and developer chats). Given a bug's SYMPTOM, find the past decision whose rationale explains the ROOT CAUSE \u2014 not one that merely shares vocabulary. Answer with the PRECISE fix: state the EXACT rule and the LITERAL values, identifiers, strings, numbers, or set members that were decided \u2014 quote them VERBATIM, never paraphrase, generalize, or omit them (give the actual decided value, not 'the project standard'). If memories CONFLICT on the same rule, the LATEST decision wins \u2014 prefer facts that explicitly amend or supersede an earlier one, state the superseded rule as no longer in effect, and never present it as the fix. Name the function/file to change and cite the REF-ID(s). If NOTHING in memory genuinely explains THIS symptom, say exactly that in one short sentence \u2014 a wrong-but-confident nearest match is worse than an honest miss; never stretch an unrelated decision to fit.";
|
|
368
|
+
var DOCUMENT_MISSION = "You are ingesting a standalone document (notes, docs, or structural findings). Extract the concrete facts, concepts, and structure it describes.";
|
|
369
|
+
var OBSERVATIONS_MISSION = "Consolidate durable knowledge about THIS codebase \u2014 recurring patterns, conventions, module responsibilities, and how components relate \u2014 from the ingested commits and conversations. Favor stable structural understanding over one-off details.";
|
|
370
|
+
var RETAIN_STRATEGIES = {
|
|
371
|
+
git: { retain_mission: GIT_MISSION, retain_extraction_mode: "verbose" },
|
|
372
|
+
// ONE big aggregated document (last N commit messages, no diffs) -> a larger chunk size so it stays
|
|
373
|
+
// in as few chunks as possible and the extractor sees the whole history arc at once.
|
|
374
|
+
gitlog: {
|
|
375
|
+
retain_mission: GITLOG_MISSION,
|
|
376
|
+
retain_extraction_mode: "verbose",
|
|
377
|
+
retain_chunk_size: 12e3
|
|
378
|
+
},
|
|
379
|
+
// ONE strategy for ALL developer conversations — backfilled decision chats and live working
|
|
380
|
+
// sessions alike (they are the same content type in the same JSON transcript format; the mission
|
|
381
|
+
// scales extraction to the substance, final-state-wins). Chunk big enough to hold a whole typical
|
|
382
|
+
// conversation in ONE chunk so the extractor sees the full proposal→revision arc (the 3000
|
|
383
|
+
// default SPLIT them into per-chunk fragments); very long sessions still split and fall back to
|
|
384
|
+
// the consolidation layer.
|
|
385
|
+
conversation: {
|
|
386
|
+
retain_mission: CONVERSATION_MISSION,
|
|
387
|
+
retain_extraction_mode: "verbose",
|
|
388
|
+
retain_chunk_size: 12e3
|
|
389
|
+
},
|
|
390
|
+
// Structural documents (e.g. the codebase survey's ingested findings) aren't dialogue — the
|
|
391
|
+
// chat strategy's "final decision vs rejected proposal" extraction doesn't apply. Verbose mode
|
|
392
|
+
// with a bigger chunk size (documents can run long) captures the concrete facts/structure instead.
|
|
393
|
+
document: {
|
|
394
|
+
retain_mission: DOCUMENT_MISSION,
|
|
395
|
+
retain_extraction_mode: "verbose",
|
|
396
|
+
retain_chunk_size: 12e3
|
|
397
|
+
},
|
|
398
|
+
// Codebase-SURVEY lifecycle documents, ONE strategy with conditional rules: the survey's
|
|
399
|
+
// internal status markers ("researching…"/"completed" baselines) must yield ZERO memories,
|
|
400
|
+
// while any actual survey findings routed here extract as concrete structural facts.
|
|
401
|
+
survey: {
|
|
402
|
+
retain_extraction_mode: "custom",
|
|
403
|
+
retain_custom_instructions: "This document belongs to the Hindsight codebase-survey lifecycle. Apply ONE of two rules: (1) If the content is an internal status marker \u2014 it says it is an internal marker, or merely announces that a survey started/completed at some commit \u2014 extract NOTHING: return an empty list of facts. (2) Otherwise the content is survey FINDINGS about the codebase: extract the concrete structural facts it states (components and their responsibilities, key concepts, conventions, tech stack), preserving identifiers verbatim.",
|
|
404
|
+
retain_chunk_size: 12e3
|
|
405
|
+
}
|
|
406
|
+
};
|
|
407
|
+
var KNOWLEDGE_LABELS = {
|
|
408
|
+
key: "knowledge",
|
|
409
|
+
type: "multi-values",
|
|
410
|
+
// 0, 1, or several — empty is normal
|
|
411
|
+
optional: true,
|
|
412
|
+
tag: true,
|
|
413
|
+
// emits knowledge:<value> onto the fact's tags
|
|
414
|
+
description: "Routing labels for this project's Hindsight KNOWLEDGE PAGES \u2014 curated, human-readable summaries of the repo's DURABLE engineering knowledge (architecture, key decisions, conventions, ongoing initiatives), each page rebuilt automatically from the facts labeled for it. Mark a fact only when it is durable, reusable knowledge a developer would still want surfaced in future sessions. IMPORTANT: leave this EMPTY for routine, transient, or operational facts \u2014 a passing test, a one-off command, a status update, a debugging dead-end. MOST facts should get no label here. Assign more than one value only when the fact genuinely fits several.",
|
|
415
|
+
values: [
|
|
416
|
+
{
|
|
417
|
+
value: "feature-work",
|
|
418
|
+
description: "A new feature, initiative, or enhancement being planned or built \u2014 the capability being added and the intent behind it. Not routine bug-fixes or chores."
|
|
419
|
+
},
|
|
420
|
+
{
|
|
421
|
+
value: "decision",
|
|
422
|
+
description: "A technical decision that will constrain future work, with its rationale \u2014 why this approach was chosen over alternatives, or a rule deliberately adopted."
|
|
423
|
+
},
|
|
424
|
+
{
|
|
425
|
+
value: "convention",
|
|
426
|
+
description: "An established way this project does things \u2014 naming, structure, testing, error handling, or another recurring pattern a contributor is expected to follow."
|
|
427
|
+
},
|
|
428
|
+
{
|
|
429
|
+
value: "component",
|
|
430
|
+
description: "What a specific module, file, service, or subsystem is responsible for, or how components depend on and connect to one another."
|
|
431
|
+
},
|
|
432
|
+
{
|
|
433
|
+
value: "concept",
|
|
434
|
+
description: "A domain concept, key abstraction, or piece of project vocabulary a new contributor must understand to work effectively."
|
|
435
|
+
}
|
|
436
|
+
]
|
|
437
|
+
};
|
|
438
|
+
var PAGES = [
|
|
439
|
+
{
|
|
440
|
+
name: "Component map",
|
|
441
|
+
source_query: "From this project's commit history and past discussions, what are the main components/modules/subsystems, what is each responsible for, and how do they relate to or depend on one another? Describe the structure and responsibilities.",
|
|
442
|
+
tags: ["knowledge:component"]
|
|
443
|
+
},
|
|
444
|
+
{
|
|
445
|
+
name: "Core concepts",
|
|
446
|
+
source_query: "What are the core concepts, domain abstractions, and key entities in this project \u2014 the vocabulary a developer must understand? For each, explain what it represents and its role, drawn from how they are introduced and discussed across the history and conversations.",
|
|
447
|
+
tags: ["knowledge:concept"]
|
|
448
|
+
},
|
|
449
|
+
{
|
|
450
|
+
name: "Conventions and patterns",
|
|
451
|
+
source_query: "What conventions, idioms, and recurring patterns does this project follow \u2014 its approach to testing, error handling, naming, structure, and how changes are typically made? Describe how THIS project does things, as evidenced across its history and discussions.",
|
|
452
|
+
tags: ["knowledge:convention"]
|
|
453
|
+
},
|
|
454
|
+
{
|
|
455
|
+
name: "Key decisions and rationale",
|
|
456
|
+
source_query: "What are the significant technical decisions made in this project and the rationale behind them \u2014 the durable 'why we do it this way' a developer should know? Summarize the decisions and their reasoning from the commit rationales and past conversations.",
|
|
457
|
+
tags: ["knowledge:decision"]
|
|
458
|
+
},
|
|
459
|
+
{
|
|
460
|
+
name: "Initiatives and enhancements",
|
|
461
|
+
source_query: "Based on this repository's commit history, what are the major initiatives, features, and enhancements the project has worked on? Summarize the themes and notable changes over time. When a source memory carries a tag of the form `relatedPageId:<id>`, include a Markdown link `[[page:<id>]]` to that page in the summary, so each initiative links to its detailed page.",
|
|
462
|
+
tags: ["knowledge:feature-work"]
|
|
463
|
+
}
|
|
464
|
+
];
|
|
465
|
+
var PAGE_MAX_TOKENS = 4096;
|
|
466
|
+
var PAGE_TRIGGER = {
|
|
467
|
+
fact_types: ["world", "experience", "observation"],
|
|
468
|
+
refresh_after_consolidation: true
|
|
469
|
+
};
|
|
470
|
+
var CODING_BANK_TEMPLATE = {
|
|
471
|
+
version: "1",
|
|
472
|
+
bank: {
|
|
473
|
+
reflect_mission: REFLECT_MISSION,
|
|
474
|
+
enable_observations: true,
|
|
475
|
+
observations_mission: OBSERVATIONS_MISSION,
|
|
476
|
+
retain_mission: GIT_MISSION,
|
|
477
|
+
retain_extraction_mode: "verbose",
|
|
478
|
+
retain_default_strategy: "git",
|
|
479
|
+
retain_strategies: RETAIN_STRATEGIES,
|
|
480
|
+
entity_labels: [KNOWLEDGE_LABELS],
|
|
481
|
+
entities_allow_free_form: true
|
|
482
|
+
}
|
|
483
|
+
};
|
|
484
|
+
|
|
485
|
+
// src/core/hindsight.ts
|
|
486
|
+
var KnowledgePagesUnavailableError = class extends Error {
|
|
487
|
+
code = "knowledge_pages_unavailable";
|
|
488
|
+
constructor() {
|
|
489
|
+
super("Hindsight server does not support knowledge pages");
|
|
490
|
+
this.name = "KnowledgePagesUnavailableError";
|
|
491
|
+
}
|
|
492
|
+
};
|
|
493
|
+
var TERMINAL = /* @__PURE__ */ new Set(["completed", "failed", "cancelled", "error"]);
|
|
494
|
+
var HindsightClient = class {
|
|
495
|
+
apiUrl;
|
|
496
|
+
apiToken;
|
|
497
|
+
bank;
|
|
498
|
+
opIds = [];
|
|
499
|
+
// async operation ids collected by retain(), for drain()
|
|
500
|
+
/** Tri-state capability probe: unknown until the first page request, then cached. */
|
|
501
|
+
knowledgePagesSupported;
|
|
502
|
+
log;
|
|
503
|
+
constructor(o) {
|
|
504
|
+
this.apiUrl = o.apiUrl.replace(/\/$/, "");
|
|
505
|
+
this.apiToken = o.apiToken;
|
|
506
|
+
this.bank = o.bank;
|
|
507
|
+
this.log = o.log ?? (() => {
|
|
508
|
+
});
|
|
509
|
+
}
|
|
510
|
+
headers() {
|
|
511
|
+
const h = { "Content-Type": "application/json" };
|
|
512
|
+
if (this.apiToken) h["Authorization"] = `Bearer ${this.apiToken}`;
|
|
513
|
+
return h;
|
|
514
|
+
}
|
|
515
|
+
bankUrl(suffix = "") {
|
|
516
|
+
return `${this.apiUrl}/v1/default/banks/${encodeURIComponent(this.bank)}${suffix}`;
|
|
517
|
+
}
|
|
518
|
+
/** `tolerate` adds statuses that are returned to the caller instead of thrown (404 always is). */
|
|
519
|
+
async req(method, url, body, tolerate = []) {
|
|
520
|
+
const r = await fetch(url, {
|
|
521
|
+
method,
|
|
522
|
+
headers: this.headers(),
|
|
523
|
+
body: body ? JSON.stringify(body) : void 0,
|
|
524
|
+
signal: AbortSignal.timeout(15e3)
|
|
525
|
+
});
|
|
526
|
+
if (!r.ok && r.status !== 404 && !tolerate.includes(r.status))
|
|
527
|
+
throw new Error(`${method} ${url} -> ${r.status} ${await r.text()}`);
|
|
528
|
+
return r;
|
|
529
|
+
}
|
|
530
|
+
/** Retain one memory. Async by default: enqueue extraction server-side and collect its op-id for drain(). */
|
|
531
|
+
async retain(content, context, documentId, tags, strategy, opts = {}) {
|
|
532
|
+
const item = {
|
|
533
|
+
content,
|
|
534
|
+
context,
|
|
535
|
+
document_id: documentId,
|
|
536
|
+
tags,
|
|
537
|
+
strategy
|
|
538
|
+
};
|
|
539
|
+
if (opts.timestamp) item.timestamp = opts.timestamp;
|
|
540
|
+
if (opts.metadata) item.metadata = opts.metadata;
|
|
541
|
+
const isAsync = opts.async !== false;
|
|
542
|
+
const r = await this.req("POST", this.bankUrl("/memories"), { items: [item], async: isAsync });
|
|
543
|
+
if (isAsync) {
|
|
544
|
+
try {
|
|
545
|
+
const j = await r.json();
|
|
546
|
+
if (j.operation_id) this.opIds.push(j.operation_id);
|
|
547
|
+
} catch {
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
/**
|
|
552
|
+
* Every document_id currently in the bank under a strategy tag (e.g. `source:git`), paginated into a
|
|
553
|
+
* Set. Powers the incremental git-sync's "what's already ingested?" check — since git commits are stored
|
|
554
|
+
* with document_id `git:<sha>`, the returned Set lets a caller diff a ref's commits against memory.
|
|
555
|
+
*/
|
|
556
|
+
async listDocumentIds(tag) {
|
|
557
|
+
const ids = /* @__PURE__ */ new Set();
|
|
558
|
+
const limit = 500;
|
|
559
|
+
for (let offset = 0; ; offset += limit) {
|
|
560
|
+
const q = `?tags=${encodeURIComponent(tag)}&tags_match=all&limit=${limit}&offset=${offset}`;
|
|
561
|
+
const r = await this.req("GET", this.bankUrl(`/documents${q}`));
|
|
562
|
+
let items = [];
|
|
563
|
+
let total = 0;
|
|
564
|
+
try {
|
|
565
|
+
const j = await r.json();
|
|
566
|
+
items = j.items || [];
|
|
567
|
+
total = j.total ?? 0;
|
|
568
|
+
} catch {
|
|
569
|
+
break;
|
|
570
|
+
}
|
|
571
|
+
for (const it of items) if (it.id) ids.add(it.id);
|
|
572
|
+
if (items.length < limit || ids.size >= total) break;
|
|
573
|
+
}
|
|
574
|
+
return ids;
|
|
575
|
+
}
|
|
576
|
+
/** Configure the bank: POST the coding bank template manifest to /import (missions, retain
|
|
577
|
+
* strategies, entity labels), then seed knowledge pages when the server supports them. Both
|
|
578
|
+
* halves are idempotent, so the deepen engine can re-run this every pass. Creates the bank if
|
|
579
|
+
* missing; legacy servers continue with the template-only path. */
|
|
580
|
+
async configureBank(opts = {}) {
|
|
581
|
+
if (opts.reset) {
|
|
582
|
+
await this.req("DELETE", this.bankUrl());
|
|
583
|
+
this.log(`[bank] reset ${this.bank}`);
|
|
584
|
+
}
|
|
585
|
+
await this.req("POST", this.bankUrl("/import"), CODING_BANK_TEMPLATE);
|
|
586
|
+
this.log(
|
|
587
|
+
`[bank] template applied to ${this.bank}: missions, entity_labels {knowledge}, strategies {git, gitlog, conversation, document}`
|
|
588
|
+
);
|
|
589
|
+
await this.seedPages();
|
|
590
|
+
}
|
|
591
|
+
/** Delete one document (cascades its memory units/links). Used by deepen's self-cleanup. */
|
|
592
|
+
async deleteDocument(documentId) {
|
|
593
|
+
await this.req("DELETE", this.bankUrl(`/documents/${encodeURIComponent(documentId)}`));
|
|
594
|
+
}
|
|
595
|
+
/** Count of operations still ACTIVE on this bank — the list includes terminal ops (completed/
|
|
596
|
+
* failed/cancelled), so filter by status. Powers syncStatus's "extractions drained" check. */
|
|
597
|
+
async activeOperations() {
|
|
598
|
+
const r = await this.req("GET", this.bankUrl("/operations"));
|
|
599
|
+
try {
|
|
600
|
+
const j = await r.json();
|
|
601
|
+
const ops = j.operations ?? j.items ?? [];
|
|
602
|
+
return ops.filter((o) => !TERMINAL.has((o?.status || "").toLowerCase())).length;
|
|
603
|
+
} catch {
|
|
604
|
+
return 0;
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
/** Poll each enqueued operation by id until terminal. LIST only shows active ops, so per-id GET is reliable. */
|
|
608
|
+
async drain(ids, label, maxMs = 60 * 60 * 1e3) {
|
|
609
|
+
if (!ids.length) return;
|
|
610
|
+
this.log(`[wait] draining ${ids.length} ${label} operations \u2026`);
|
|
611
|
+
const start = Date.now();
|
|
612
|
+
const pending = new Set(ids);
|
|
613
|
+
let failed = 0;
|
|
614
|
+
while (pending.size && Date.now() - start < maxMs) {
|
|
615
|
+
await Promise.all(
|
|
616
|
+
[...pending].map(async (id) => {
|
|
617
|
+
try {
|
|
618
|
+
const r = await fetch(this.bankUrl(`/operations/${id}`), { headers: this.headers() });
|
|
619
|
+
if (!r.ok) return;
|
|
620
|
+
const st = ((await r.json()).status || "").toLowerCase();
|
|
621
|
+
if (TERMINAL.has(st)) {
|
|
622
|
+
pending.delete(id);
|
|
623
|
+
if (st !== "completed") failed++;
|
|
624
|
+
}
|
|
625
|
+
} catch {
|
|
626
|
+
}
|
|
627
|
+
})
|
|
628
|
+
);
|
|
629
|
+
if (pending.size) {
|
|
630
|
+
this.log(` \u2026 ${pending.size}/${ids.length} ${label} ops pending`);
|
|
631
|
+
await sleep(5e3);
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
this.log(
|
|
635
|
+
`[wait] ${label} drained \u2014 ${ids.length - pending.size} done, ${failed} failed` + (pending.size ? `, ${pending.size} still pending at timeout` : "")
|
|
636
|
+
);
|
|
637
|
+
}
|
|
638
|
+
/** Reflect: synthesized, root-cause answer over the bank. Bounded so a slow server never hangs a caller. */
|
|
639
|
+
async reflect(query, opts = {}) {
|
|
640
|
+
const ctrl = new AbortController();
|
|
641
|
+
const timer = setTimeout(() => ctrl.abort(), opts.timeoutMs ?? 12e4);
|
|
642
|
+
try {
|
|
643
|
+
const resp = await fetch(this.bankUrl("/reflect"), {
|
|
644
|
+
method: "POST",
|
|
645
|
+
headers: this.headers(),
|
|
646
|
+
body: JSON.stringify({ query, budget: opts.budget ?? "high" }),
|
|
647
|
+
signal: ctrl.signal
|
|
648
|
+
});
|
|
649
|
+
if (!resp.ok) throw new Error(`reflect ${resp.status}`);
|
|
650
|
+
const data = await resp.json();
|
|
651
|
+
return (data.text || "").trim();
|
|
652
|
+
} finally {
|
|
653
|
+
clearTimeout(timer);
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
/**
|
|
657
|
+
* The bank's knowledge-base tree (folders + pages, nested). The tree carries names, source
|
|
658
|
+
* queries and staleness but NOT synthesized content, so it is cheap enough to poll.
|
|
659
|
+
*/
|
|
660
|
+
async tree() {
|
|
661
|
+
if (this.knowledgePagesSupported === false) throw new KnowledgePagesUnavailableError();
|
|
662
|
+
const r = await this.req("GET", this.bankUrl("/knowledge-base/tree"));
|
|
663
|
+
if ([404, 405, 501].includes(r.status)) {
|
|
664
|
+
this.knowledgePagesSupported = false;
|
|
665
|
+
throw new KnowledgePagesUnavailableError();
|
|
666
|
+
}
|
|
667
|
+
this.knowledgePagesSupported = true;
|
|
668
|
+
try {
|
|
669
|
+
return (await r.json()).roots ?? [];
|
|
670
|
+
} catch {
|
|
671
|
+
return [];
|
|
672
|
+
}
|
|
673
|
+
}
|
|
674
|
+
/**
|
|
675
|
+
* List knowledge pages (ids + names only — no synthesized content), flattened out of the tree
|
|
676
|
+
* and shaped as `{items:[…]}` for `parsePageList`. Folders are dropped: they carry no content
|
|
677
|
+
* to read, and a caller enumerating pages wants leaves, not structure.
|
|
678
|
+
*
|
|
679
|
+
* NOTE the ids are knowledge-base node ids (`kp-…`), NOT the backing mental-model ids. Every
|
|
680
|
+
* page read/update in this client goes through the same knowledge-base ids, so a page id from
|
|
681
|
+
* here, from `searchKnowledgePages`, or from a `[[page:<id>]]` link all resolve identically.
|
|
682
|
+
*/
|
|
683
|
+
async listPages() {
|
|
684
|
+
const items = [];
|
|
685
|
+
const walk = (nodes, folder) => {
|
|
686
|
+
for (const n of nodes) {
|
|
687
|
+
if (!n?.id || !n?.name) continue;
|
|
688
|
+
if (n.kind === "page") {
|
|
689
|
+
items.push({
|
|
690
|
+
id: n.id,
|
|
691
|
+
name: n.name,
|
|
692
|
+
...n.description ? { description: n.description } : {},
|
|
693
|
+
...folder ? { folder } : {}
|
|
694
|
+
});
|
|
695
|
+
}
|
|
696
|
+
if (n.children?.length) walk(n.children, n.kind === "folder" ? n.name : folder);
|
|
697
|
+
}
|
|
698
|
+
};
|
|
699
|
+
walk(await this.tree());
|
|
700
|
+
return { items };
|
|
701
|
+
}
|
|
702
|
+
/**
|
|
703
|
+
* Read one knowledge page's synthesized content by knowledge-base id, as an OKF document
|
|
704
|
+
* (YAML frontmatter + markdown body). The endpoint omits the internal reflect trace that built
|
|
705
|
+
* the page — that is 70-95% of the raw bytes and can blow past an MCP host's per-tool-result
|
|
706
|
+
* token cap.
|
|
707
|
+
*/
|
|
708
|
+
async getPage(pageId) {
|
|
709
|
+
if (this.knowledgePagesSupported === false) throw new KnowledgePagesUnavailableError();
|
|
710
|
+
const r = await this.req(
|
|
711
|
+
"GET",
|
|
712
|
+
this.bankUrl(`/knowledge-base/pages/${encodeURIComponent(pageId)}`)
|
|
713
|
+
);
|
|
714
|
+
if (r.status === 404) throw new Error(`knowledge page not found: ${pageId}`);
|
|
715
|
+
return await r.json();
|
|
716
|
+
}
|
|
717
|
+
/** Hybrid (BM25 + vector, RRF-fused) server-side search over the bank's knowledge pages.
|
|
718
|
+
* Returns page-level hits with a relevance snippet — the real search behind
|
|
719
|
+
* hindsight_search_knowledge_pages. */
|
|
720
|
+
async searchKnowledgePages(query, limit = 3) {
|
|
721
|
+
if (this.knowledgePagesSupported === false) throw new KnowledgePagesUnavailableError();
|
|
722
|
+
const q = `?q=${encodeURIComponent(query)}&limit=${limit}`;
|
|
723
|
+
const r = await this.req("GET", this.bankUrl(`/knowledge-base/search${q}`));
|
|
724
|
+
const j = await r.json();
|
|
725
|
+
return (j.results ?? []).map((x) => ({
|
|
726
|
+
id: x.id,
|
|
727
|
+
name: x.name,
|
|
728
|
+
snippet: x.snippet ?? "",
|
|
729
|
+
score: x.score ?? 0
|
|
730
|
+
}));
|
|
731
|
+
}
|
|
732
|
+
/**
|
|
733
|
+
* Seed the fixed `PAGES` taxonomy as knowledge-base pages at the tree root, idempotently.
|
|
734
|
+
*
|
|
735
|
+
* Matched by NAME, not id: `/knowledge-base/pages` mints its own `kp-…` id, so a stable
|
|
736
|
+
* client-chosen id isn't available to match on (unlike the old mental-model path, which keyed
|
|
737
|
+
* off a slug). Names are unique per folder server-side, which makes them a sound key.
|
|
738
|
+
*
|
|
739
|
+
* An existing page is PATCHed rather than recreated so a plugin upgrade that rewords a
|
|
740
|
+
* `source_query` re-syncs onto the live page instead of orphaning its synthesized content.
|
|
741
|
+
*/
|
|
742
|
+
async seedPages() {
|
|
743
|
+
const existing = /* @__PURE__ */ new Map();
|
|
744
|
+
let roots;
|
|
745
|
+
try {
|
|
746
|
+
roots = await this.tree();
|
|
747
|
+
} catch (e) {
|
|
748
|
+
if (e instanceof KnowledgePagesUnavailableError) {
|
|
749
|
+
this.log(`[bank] knowledge pages unavailable on ${this.apiUrl}; continuing without pages`);
|
|
750
|
+
return;
|
|
751
|
+
}
|
|
752
|
+
throw e;
|
|
753
|
+
}
|
|
754
|
+
for (const n of roots) {
|
|
755
|
+
if (n.kind === "page" && n.name) existing.set(n.name.toLowerCase(), n);
|
|
756
|
+
}
|
|
757
|
+
let created = 0;
|
|
758
|
+
let updated = 0;
|
|
759
|
+
for (const page of PAGES) {
|
|
760
|
+
const hit = existing.get(page.name.toLowerCase());
|
|
761
|
+
const body = {
|
|
762
|
+
name: page.name,
|
|
763
|
+
source_query: page.source_query,
|
|
764
|
+
tags: page.tags,
|
|
765
|
+
max_tokens: PAGE_MAX_TOKENS,
|
|
766
|
+
trigger: PAGE_TRIGGER
|
|
767
|
+
};
|
|
768
|
+
if (!hit) {
|
|
769
|
+
const r = await this.req("POST", this.bankUrl("/knowledge-base/pages"), body, [409]);
|
|
770
|
+
if ([404, 405, 501].includes(r.status)) {
|
|
771
|
+
this.knowledgePagesSupported = false;
|
|
772
|
+
this.log(
|
|
773
|
+
`[bank] knowledge pages unavailable on ${this.apiUrl}; continuing without pages`
|
|
774
|
+
);
|
|
775
|
+
return;
|
|
776
|
+
}
|
|
777
|
+
if (r.status !== 409) created++;
|
|
778
|
+
} else if (hit.description !== page.source_query) {
|
|
779
|
+
const r = await this.req(
|
|
780
|
+
"PATCH",
|
|
781
|
+
this.bankUrl(`/knowledge-base/nodes/${encodeURIComponent(hit.id)}`),
|
|
782
|
+
{ source_query: page.source_query, tags: page.tags }
|
|
783
|
+
);
|
|
784
|
+
if ([404, 405, 501].includes(r.status)) {
|
|
785
|
+
this.knowledgePagesSupported = false;
|
|
786
|
+
this.log(
|
|
787
|
+
`[bank] knowledge pages unavailable on ${this.apiUrl}; continuing without pages`
|
|
788
|
+
);
|
|
789
|
+
return;
|
|
790
|
+
}
|
|
791
|
+
updated++;
|
|
792
|
+
}
|
|
793
|
+
}
|
|
794
|
+
this.log(
|
|
795
|
+
`[bank] knowledge pages seeded on ${this.bank}: ${created} created, ${updated} re-synced, ${PAGES.length - created - updated} unchanged`
|
|
796
|
+
);
|
|
797
|
+
}
|
|
798
|
+
/** URL/id-safe slug: lowercase, non-alphanumerics → "-", trim dashes, cap length; fallback "initiative". */
|
|
799
|
+
slugify(s) {
|
|
800
|
+
return s.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "").slice(0, 60) || "initiative";
|
|
801
|
+
}
|
|
802
|
+
/**
|
|
803
|
+
* Active-path capture: register a major feature as a per-initiative page + a tagged marker memory.
|
|
804
|
+
* New initiative → creates a page under the Initiatives folder tagged `relatedPageId:<pageId>`.
|
|
805
|
+
* Enhancement (relatesToPageId) → no new page; only a marker accruing to the existing page.
|
|
806
|
+
*/
|
|
807
|
+
async captureInitiative(args) {
|
|
808
|
+
let pageId = args.relatesToPageId;
|
|
809
|
+
if (!pageId) {
|
|
810
|
+
const folderId = await this.ensureFolder("Initiatives");
|
|
811
|
+
const r = await this.req("POST", this.bankUrl("/knowledge-base/pages"), {
|
|
812
|
+
name: args.title,
|
|
813
|
+
source_query: `Summarize the "${args.title}" initiative: what is being built or changed and why, and its current state \u2014 drawn from the project's memory.`,
|
|
814
|
+
parent_id: folderId,
|
|
815
|
+
tags: ["knowledge:feature-work"],
|
|
816
|
+
trigger: {
|
|
817
|
+
fact_types: ["world", "experience", "observation"],
|
|
818
|
+
refresh_after_consolidation: true
|
|
819
|
+
}
|
|
820
|
+
});
|
|
821
|
+
try {
|
|
822
|
+
const j = await r.json();
|
|
823
|
+
pageId = j.page_id ?? j.id;
|
|
824
|
+
} catch {
|
|
825
|
+
}
|
|
826
|
+
pageId ||= `initiative-${this.slugify(args.title)}`;
|
|
827
|
+
}
|
|
828
|
+
const verb = args.relatesToPageId ? "Enhancement to an existing initiative" : "New initiative";
|
|
829
|
+
const content = `${verb}: ${args.title}. ${args.summary}`;
|
|
830
|
+
const markerId = `initiative-marker-${this.slugify(args.title)}-${Date.now()}`;
|
|
831
|
+
await this.retain(
|
|
832
|
+
content,
|
|
833
|
+
"initiative marker",
|
|
834
|
+
markerId,
|
|
835
|
+
["knowledge:feature-work", `relatedPageId:${pageId}`],
|
|
836
|
+
"document",
|
|
837
|
+
{ async: true }
|
|
838
|
+
);
|
|
839
|
+
return { page_id: pageId };
|
|
840
|
+
}
|
|
841
|
+
/** Find a root folder by name (case-insensitive) or create it; returns its id. Fail-open to undefined. */
|
|
842
|
+
async ensureFolder(name) {
|
|
843
|
+
try {
|
|
844
|
+
const tree = await (await this.req("GET", this.bankUrl("/knowledge-base/tree"))).json();
|
|
845
|
+
const hit = (tree.roots || []).find(
|
|
846
|
+
(n) => n.kind === "folder" && (n.name || "").toLowerCase() === name.toLowerCase()
|
|
847
|
+
);
|
|
848
|
+
if (hit?.id) return hit.id;
|
|
849
|
+
} catch {
|
|
850
|
+
}
|
|
851
|
+
try {
|
|
852
|
+
const r = await this.req("POST", this.bankUrl("/knowledge-base/folders"), { name });
|
|
853
|
+
return (await r.json()).id;
|
|
854
|
+
} catch {
|
|
855
|
+
return void 0;
|
|
856
|
+
}
|
|
857
|
+
}
|
|
858
|
+
};
|
|
859
|
+
|
|
860
|
+
// src/core/status.ts
|
|
861
|
+
import { execFileSync as execFileSync3 } from "child_process";
|
|
862
|
+
var DEEPEN_DIFF_TARGET = 300;
|
|
863
|
+
|
|
864
|
+
// src/harness/registry.ts
|
|
865
|
+
import { readFileSync as readFileSync2 } from "fs";
|
|
866
|
+
var jsonChatReader = (harness) => ({
|
|
867
|
+
describe: `${harness} sessions via a normalized JSON export (--conversations file: [{ id, turns:[{role,text,timestamp?}] }])`,
|
|
868
|
+
async read(opts) {
|
|
869
|
+
if (!opts.conversations) return [];
|
|
870
|
+
return JSON.parse(readFileSync2(opts.conversations, "utf8"));
|
|
871
|
+
}
|
|
872
|
+
});
|
|
873
|
+
var noRuntimeAdapter = (name, hint) => ({
|
|
874
|
+
name,
|
|
875
|
+
chatReader: jsonChatReader(name),
|
|
876
|
+
createRuntime() {
|
|
877
|
+
throw new Error(hint);
|
|
878
|
+
}
|
|
879
|
+
});
|
|
880
|
+
var HARNESS_NAMES = [
|
|
881
|
+
"opencode",
|
|
882
|
+
// Kilo CLI is an opencode fork loaded as a persistent plugin (src/kilo.ts), so like opencode it
|
|
883
|
+
// has NO hook binary — deliberately absent from HOOK_BINS below.
|
|
884
|
+
"kilo",
|
|
885
|
+
// Cline CLI loads dist/cline.js through its native plugin manager; file hooks cannot inject.
|
|
886
|
+
"cline-cli",
|
|
887
|
+
"claude-code",
|
|
888
|
+
"cursor-cli",
|
|
889
|
+
"codex",
|
|
890
|
+
"antigravity-cli",
|
|
891
|
+
"devin-cli"
|
|
892
|
+
];
|
|
893
|
+
var HOOK_BINS = {
|
|
894
|
+
"claude-code": "hindsight-claude-hook",
|
|
895
|
+
"cursor-cli": "hindsight-cursor-hook",
|
|
896
|
+
codex: "hindsight-codex-hook",
|
|
897
|
+
"antigravity-cli": "hindsight-antigravity-hook",
|
|
898
|
+
"devin-cli": "hindsight-devin-hook"
|
|
899
|
+
// more hook harnesses: add a HookSpec entry point (see src/cursor-hook.ts) + a registration here.
|
|
900
|
+
};
|
|
901
|
+
async function getHarness(name) {
|
|
902
|
+
if (name === "opencode" || name === "kilo" || name === "cline-cli") {
|
|
903
|
+
const entry = name === "opencode" ? "src/index.ts" : name === "kilo" ? "src/kilo.ts" : "src/cline.ts";
|
|
904
|
+
return noRuntimeAdapter(
|
|
905
|
+
name,
|
|
906
|
+
`${name}'s runtime is built by ${entry} (the ${name} plugin entrypoint), not via the harness registry`
|
|
907
|
+
);
|
|
908
|
+
}
|
|
909
|
+
const bin = HOOK_BINS[name];
|
|
910
|
+
if (!bin) {
|
|
911
|
+
throw new Error(`unknown harness '${name}'. available: ${HARNESS_NAMES.join(", ")}`);
|
|
912
|
+
}
|
|
913
|
+
return noRuntimeAdapter(
|
|
914
|
+
name,
|
|
915
|
+
`'${name}' has no persistent plugin runtime \u2014 install its hook binary (${bin}) instead`
|
|
916
|
+
);
|
|
917
|
+
}
|
|
918
|
+
|
|
919
|
+
// src/core/diag.ts
|
|
920
|
+
import { appendFileSync as appendFileSync2 } from "fs";
|
|
921
|
+
|
|
922
|
+
// src/core/log.ts
|
|
923
|
+
import { appendFileSync, mkdirSync } from "fs";
|
|
924
|
+
import { tmpdir } from "os";
|
|
925
|
+
import { dirname as dirname4, join as join5 } from "path";
|
|
926
|
+
var WEIGHT = { debug: 10, info: 20, warn: 30, error: 40 };
|
|
927
|
+
var current = ["debug", "info", "warn", "error"].find(
|
|
928
|
+
(l) => l === process.env.HINDSIGHT_LOG_LEVEL
|
|
929
|
+
) ?? "info";
|
|
930
|
+
function setLogLevel(level) {
|
|
931
|
+
if (!process.env.HINDSIGHT_LOG_LEVEL) current = level;
|
|
932
|
+
}
|
|
933
|
+
function logFilePath() {
|
|
934
|
+
return process.env.HINDSIGHT_LOG_FILE || join5(tmpdir(), "hindsight-coding-agent", "plugin.log");
|
|
935
|
+
}
|
|
936
|
+
function write(level, scope, msg, extra) {
|
|
937
|
+
if (WEIGHT[level] < WEIGHT[current]) return;
|
|
938
|
+
try {
|
|
939
|
+
const file = logFilePath();
|
|
940
|
+
mkdirSync(dirname4(file), { recursive: true });
|
|
941
|
+
appendFileSync(
|
|
942
|
+
file,
|
|
943
|
+
`${(/* @__PURE__ */ new Date()).toISOString()} ${level.toUpperCase().padEnd(5)} [${scope}] ${msg}` + (extra && Object.keys(extra).length ? ` ${JSON.stringify(extra)}` : "") + "\n"
|
|
944
|
+
);
|
|
945
|
+
} catch {
|
|
946
|
+
}
|
|
947
|
+
}
|
|
948
|
+
var log = {
|
|
949
|
+
debug: (scope, msg, extra) => write("debug", scope, msg, extra),
|
|
950
|
+
info: (scope, msg, extra) => write("info", scope, msg, extra),
|
|
951
|
+
warn: (scope, msg, extra) => write("warn", scope, msg, extra),
|
|
952
|
+
error: (scope, msg, extra) => write("error", scope, msg, extra)
|
|
953
|
+
};
|
|
954
|
+
|
|
955
|
+
// src/core/diag.ts
|
|
956
|
+
function diag(harness, event, extra = {}) {
|
|
957
|
+
log.debug(harness, `diag:${event}`, extra);
|
|
958
|
+
try {
|
|
959
|
+
appendFileSync2(
|
|
960
|
+
process.env.HINDSIGHT_DIAG_FILE || "/tmp/hindsight-plugin.log",
|
|
961
|
+
JSON.stringify({ ts: (/* @__PURE__ */ new Date()).toISOString(), harness, event, ...extra }) + "\n"
|
|
962
|
+
);
|
|
963
|
+
} catch {
|
|
964
|
+
}
|
|
965
|
+
}
|
|
966
|
+
|
|
967
|
+
// src/deepen.ts
|
|
968
|
+
var DIFF_BATCH = 50;
|
|
969
|
+
var CONCURRENCY = 4;
|
|
970
|
+
var LOCK_STALE_MS = 30 * 60 * 1e3;
|
|
971
|
+
function arg(name, def) {
|
|
972
|
+
const i = process.argv.indexOf(`--${name}`);
|
|
973
|
+
if (i >= 0 && i + 1 < process.argv.length) return process.argv[i + 1];
|
|
974
|
+
return process.argv.includes(`--${name}`) ? "true" : def;
|
|
975
|
+
}
|
|
976
|
+
var REPO = arg("repo");
|
|
977
|
+
var cfg0 = loadConfig({ harness: arg("harness") ?? void 0, path: arg("config") });
|
|
978
|
+
var BANK = arg("bank") ?? (REPO ? deriveBankId(cfg0, REPO, arg("harness") ?? cfg0.harness) : cfg0.bankId);
|
|
979
|
+
var resolved0 = BANK ? applyBankConfig(cfg0, BANK) : { cfg: cfg0, bankId: BANK };
|
|
980
|
+
var cfg = resolved0.cfg;
|
|
981
|
+
var FINAL_BANK = resolved0.bankId ?? BANK;
|
|
982
|
+
if (cfg.disabled) {
|
|
983
|
+
console.log(`deepen: bank ${FINAL_BANK} is disabled (banks override) \u2014 nothing to do`);
|
|
984
|
+
process.exit(0);
|
|
985
|
+
}
|
|
986
|
+
var HARNESS = arg("harness") ?? cfg0.harness;
|
|
987
|
+
var API_URL = arg("api-url") ?? cfg.apiUrl;
|
|
988
|
+
var API_TOKEN = arg("api-token") ?? cfg.apiToken;
|
|
989
|
+
var CONV = arg("conversations");
|
|
990
|
+
var GITLOG_LIMIT = arg("gitlog-limit") ? Number(arg("gitlog-limit")) : cfg.seedLimit ?? 300;
|
|
991
|
+
var GIT_INGEST = ["message", "full", "none"].find((m) => m === arg("git-ingest")) ?? cfg.gitIngest;
|
|
992
|
+
if (!REPO || !BANK) {
|
|
993
|
+
console.error(
|
|
994
|
+
`usage: node deepen.js --repo <path> [--bank <id>] [--harness <name>] [--conversations f.json] [--api-url U] [--api-token X] [--config path] [--gitlog-limit N] [--git-ingest message|full|none]
|
|
995
|
+
harnesses: ${HARNESS_NAMES.join(", ")}`
|
|
996
|
+
);
|
|
997
|
+
process.exit(1);
|
|
998
|
+
}
|
|
999
|
+
setLogLevel(cfg.logLevel);
|
|
1000
|
+
var log2 = (m) => {
|
|
1001
|
+
console.log(`${(/* @__PURE__ */ new Date()).toISOString()} ${m}`);
|
|
1002
|
+
log.info("deepen", m);
|
|
1003
|
+
};
|
|
1004
|
+
var LOCK_DIR = join6(tmpdir2(), "hindsight-coding-agent");
|
|
1005
|
+
var LOCK = join6(LOCK_DIR, `deepen-${encodeURIComponent(FINAL_BANK ?? "")}.lock`);
|
|
1006
|
+
function acquireLock() {
|
|
1007
|
+
try {
|
|
1008
|
+
const held = JSON.parse(readFileSync3(LOCK, "utf8"));
|
|
1009
|
+
if (held.ts && Date.now() - held.ts < LOCK_STALE_MS) {
|
|
1010
|
+
let holderAlive = false;
|
|
1011
|
+
if (held.pid) {
|
|
1012
|
+
try {
|
|
1013
|
+
process.kill(held.pid, 0);
|
|
1014
|
+
holderAlive = true;
|
|
1015
|
+
} catch {
|
|
1016
|
+
}
|
|
1017
|
+
}
|
|
1018
|
+
if (holderAlive) return false;
|
|
1019
|
+
}
|
|
1020
|
+
} catch {
|
|
1021
|
+
}
|
|
1022
|
+
try {
|
|
1023
|
+
mkdirSync2(LOCK_DIR, { recursive: true });
|
|
1024
|
+
writeFileSync(LOCK, JSON.stringify({ pid: process.pid, ts: Date.now() }));
|
|
1025
|
+
return true;
|
|
1026
|
+
} catch {
|
|
1027
|
+
return false;
|
|
1028
|
+
}
|
|
1029
|
+
}
|
|
1030
|
+
async function main() {
|
|
1031
|
+
if (!acquireLock()) {
|
|
1032
|
+
log2(`deepen: another run holds the lock for ${FINAL_BANK} \u2014 nothing to do`);
|
|
1033
|
+
return;
|
|
1034
|
+
}
|
|
1035
|
+
const t0 = Date.now();
|
|
1036
|
+
diag("deepen", "deepen_started", { bank: FINAL_BANK });
|
|
1037
|
+
try {
|
|
1038
|
+
const harness = await getHarness(HARNESS);
|
|
1039
|
+
const client = new HindsightClient({
|
|
1040
|
+
apiUrl: API_URL,
|
|
1041
|
+
apiToken: API_TOKEN,
|
|
1042
|
+
bank: FINAL_BANK,
|
|
1043
|
+
log: log2
|
|
1044
|
+
});
|
|
1045
|
+
log2(`deepen -> ${client.apiUrl} bank=${FINAL_BANK} harness=${harness.name}`);
|
|
1046
|
+
await client.configureBank();
|
|
1047
|
+
if (client.knowledgePagesSupported === false) {
|
|
1048
|
+
diag(harness.name, "knowledge_pages_unavailable", {
|
|
1049
|
+
bank: FINAL_BANK,
|
|
1050
|
+
apiUrl: client.apiUrl
|
|
1051
|
+
});
|
|
1052
|
+
}
|
|
1053
|
+
const gitIds = await client.listDocumentIds("source:git");
|
|
1054
|
+
const chatIds = await client.listDocumentIds("source:chat").catch(() => /* @__PURE__ */ new Set());
|
|
1055
|
+
const all = await harness.chatReader.read({ conversations: CONV, repo: REPO });
|
|
1056
|
+
const sessions = all.filter((s, i) => !chatIds.has(`chat:${s.id || `s${i}`}`));
|
|
1057
|
+
if (all.length !== sessions.length)
|
|
1058
|
+
log2(`[chat] ${all.length - sessions.length} conversations already ingested \u2014 skipping those`);
|
|
1059
|
+
const chatFails = await ingestChats(client, sessions, { concurrency: CONCURRENCY, log: log2 });
|
|
1060
|
+
let gitFails = 0;
|
|
1061
|
+
if (GIT_INGEST === "none") {
|
|
1062
|
+
log2("[git] gitIngest=none \u2014 git ingestion disabled");
|
|
1063
|
+
} else {
|
|
1064
|
+
const head = gitHeadSha(REPO);
|
|
1065
|
+
const gitlogCurrent = head !== null && (await client.listDocumentIds(`gitlog-head:${head}`).catch(() => /* @__PURE__ */ new Set())).size > 0;
|
|
1066
|
+
if (gitlogCurrent) {
|
|
1067
|
+
log2("[gitlog] current with HEAD \u2014 skipping");
|
|
1068
|
+
} else {
|
|
1069
|
+
gitFails += await ingestGitLog(client, REPO, { limit: GITLOG_LIMIT, log: log2 });
|
|
1070
|
+
}
|
|
1071
|
+
try {
|
|
1072
|
+
const canonical = `gitlog:${repoNameOf(REPO)}`;
|
|
1073
|
+
const logDocs = await client.listDocumentIds("source:git-log");
|
|
1074
|
+
for (const id of logDocs) {
|
|
1075
|
+
if (id !== canonical) {
|
|
1076
|
+
await client.deleteDocument(id);
|
|
1077
|
+
log2(`[gitlog] removed stale duplicate ${id} (canonical: ${canonical})`);
|
|
1078
|
+
}
|
|
1079
|
+
}
|
|
1080
|
+
} catch {
|
|
1081
|
+
}
|
|
1082
|
+
if (GIT_INGEST === "full") {
|
|
1083
|
+
try {
|
|
1084
|
+
const shas = execFileSync4(
|
|
1085
|
+
"git",
|
|
1086
|
+
["-C", REPO, "rev-list", `-n`, String(DEEPEN_DIFF_TARGET), "HEAD"],
|
|
1087
|
+
{ encoding: "utf8" }
|
|
1088
|
+
).trim().split("\n").filter(Boolean).filter((sha) => !gitIds.has(`git:${sha}`)).slice(0, DIFF_BATCH);
|
|
1089
|
+
if (shas.length) {
|
|
1090
|
+
const repoName = repoNameOf(REPO);
|
|
1091
|
+
log2(`[deepen] ingesting ${shas.length} commits with full diffs (newest first) \u2026`);
|
|
1092
|
+
await pool(
|
|
1093
|
+
shas,
|
|
1094
|
+
CONCURRENCY,
|
|
1095
|
+
(sha) => retainCommit(client, REPO, sha, repoName),
|
|
1096
|
+
() => {
|
|
1097
|
+
gitFails++;
|
|
1098
|
+
}
|
|
1099
|
+
);
|
|
1100
|
+
} else {
|
|
1101
|
+
log2(`[deepen] recent history fully deepened (target ${DEEPEN_DIFF_TARGET})`);
|
|
1102
|
+
}
|
|
1103
|
+
} catch {
|
|
1104
|
+
log2("[deepen] no git history \u2014 skipping diff deepening");
|
|
1105
|
+
}
|
|
1106
|
+
}
|
|
1107
|
+
}
|
|
1108
|
+
try {
|
|
1109
|
+
const uploads = await client.listDocumentIds("source:upload").catch(() => /* @__PURE__ */ new Set());
|
|
1110
|
+
if (SURVEY_DOC_IDS.some((id) => uploads.has(id))) {
|
|
1111
|
+
const markers = await client.listDocumentIds("source:survey-baseline");
|
|
1112
|
+
const done = await client.listDocumentIds("survey-state:done").catch(() => /* @__PURE__ */ new Set());
|
|
1113
|
+
let best;
|
|
1114
|
+
for (const id of markers) {
|
|
1115
|
+
if (done.has(id)) continue;
|
|
1116
|
+
const sha = id.replace(/^survey-baseline:/, "");
|
|
1117
|
+
const behind = commitsSince(REPO, sha);
|
|
1118
|
+
if (behind !== null && (!best || behind < best.behind)) best = { id, sha, behind };
|
|
1119
|
+
}
|
|
1120
|
+
if (best) {
|
|
1121
|
+
await client.retain(
|
|
1122
|
+
`\u2705 Codebase survey completed \u2014 baseline commit ${best.sha.slice(0, 12)}. (Internal marker: no memories are extracted from this document.)`,
|
|
1123
|
+
"hindsight codebase-survey baseline",
|
|
1124
|
+
best.id,
|
|
1125
|
+
["source:survey-baseline", "survey-state:done"],
|
|
1126
|
+
"survey",
|
|
1127
|
+
{ async: true }
|
|
1128
|
+
);
|
|
1129
|
+
log2(`[survey] marker ${best.id} flipped to completed`);
|
|
1130
|
+
}
|
|
1131
|
+
}
|
|
1132
|
+
} catch {
|
|
1133
|
+
}
|
|
1134
|
+
await client.drain(client.opIds, "extraction");
|
|
1135
|
+
const settleDeadline = Date.now() + 15 * 60 * 1e3;
|
|
1136
|
+
for (; ; ) {
|
|
1137
|
+
const active = await client.activeOperations().catch(() => 0);
|
|
1138
|
+
if (active === 0) break;
|
|
1139
|
+
if (Date.now() > settleDeadline) {
|
|
1140
|
+
log2(`[deepen] ${active} server-side op(s) still active at settle timeout \u2014 proceeding`);
|
|
1141
|
+
break;
|
|
1142
|
+
}
|
|
1143
|
+
log2(`[deepen] waiting for ${active} server-side op(s) to settle \u2026`);
|
|
1144
|
+
await new Promise((r) => setTimeout(r, 5e3));
|
|
1145
|
+
}
|
|
1146
|
+
const failures = chatFails + gitFails;
|
|
1147
|
+
diag("deepen", "deepen_done", {
|
|
1148
|
+
bank: FINAL_BANK,
|
|
1149
|
+
ms: Date.now() - t0,
|
|
1150
|
+
newChats: sessions.length,
|
|
1151
|
+
failures
|
|
1152
|
+
});
|
|
1153
|
+
log2(
|
|
1154
|
+
`
|
|
1155
|
+
\u2705 deepen complete in ${((Date.now() - t0) / 1e3).toFixed(1)}s${failures ? ` (${failures} items failed to enqueue)` : ""}.`
|
|
1156
|
+
);
|
|
1157
|
+
} finally {
|
|
1158
|
+
try {
|
|
1159
|
+
unlinkSync(LOCK);
|
|
1160
|
+
} catch {
|
|
1161
|
+
}
|
|
1162
|
+
}
|
|
1163
|
+
}
|
|
1164
|
+
main().catch((e) => {
|
|
1165
|
+
diag("deepen", "deepen_failed", {
|
|
1166
|
+
bank: FINAL_BANK,
|
|
1167
|
+
error: String(e?.message || e).slice(0, 200)
|
|
1168
|
+
});
|
|
1169
|
+
console.error("deepen failed:", e.message || e);
|
|
1170
|
+
try {
|
|
1171
|
+
unlinkSync(LOCK);
|
|
1172
|
+
} catch {
|
|
1173
|
+
}
|
|
1174
|
+
process.exit(1);
|
|
1175
|
+
});
|