claude-db 0.12.4 → 0.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +37 -0
- package/dist/capture/cursors.d.ts +8 -0
- package/dist/capture/cursors.js +99 -0
- package/dist/capture/flush-lock.d.ts +11 -0
- package/dist/capture/flush-lock.js +108 -0
- package/dist/capture/flush.d.ts +4 -4
- package/dist/capture/flush.js +19 -80
- package/dist/capture/index.d.ts +3 -3
- package/dist/capture/index.js +3 -3
- package/dist/capture/reingest.d.ts +1 -0
- package/dist/capture/reingest.js +4 -1
- package/dist/capture/scrub.d.ts +1 -0
- package/dist/capture/scrub.js +4 -1
- package/dist/capture/vectors.js +2 -1
- package/dist/cli/commands/daemon.d.ts +1 -0
- package/dist/cli/commands/daemon.js +29 -0
- package/dist/cli/commands/distill.js +2 -1
- package/dist/cli/commands/graph.js +2 -1
- package/dist/cli/commands/maintain.js +3 -1
- package/dist/cli/index.js +4 -0
- package/dist/cli/usage.js +1 -0
- package/dist/config/schema.d.ts +4 -0
- package/dist/config/schema.js +6 -0
- package/dist/context.d.ts +1 -0
- package/dist/context.js +21 -6
- package/dist/daemon/client.d.ts +12 -0
- package/dist/daemon/client.js +88 -0
- package/dist/daemon/paths.d.ts +16 -0
- package/dist/daemon/paths.js +19 -0
- package/dist/daemon/server.d.ts +2 -0
- package/dist/daemon/server.js +225 -0
- package/dist/facts/budget.d.ts +1 -0
- package/dist/facts/budget.js +4 -0
- package/dist/facts/claude-memory.js +1 -1
- package/dist/facts/distill.js +2 -2
- package/dist/facts/known.js +1 -1
- package/dist/facts/model.d.ts +1 -1
- package/dist/facts/model.js +18 -7
- package/dist/facts/start.js +1 -1
- package/dist/graph/query/refresh.js +5 -1
- package/dist/graph/scan/cache.d.ts +1 -0
- package/dist/graph/scan/cache.js +1 -1
- package/dist/graph/scan/persist.js +12 -16
- package/dist/hooks/compact.d.ts +2 -1
- package/dist/hooks/compact.js +2 -2
- package/dist/hooks/detached.d.ts +1 -0
- package/dist/hooks/detached.js +6 -0
- package/dist/hooks/graph-presence.d.ts +3 -0
- package/dist/hooks/graph-presence.js +42 -0
- package/dist/hooks/handlers/prefer-usages.d.ts +2 -0
- package/dist/hooks/handlers/prefer-usages.js +113 -0
- package/dist/hooks/handlers/session-end.d.ts +2 -0
- package/dist/hooks/handlers/session-end.js +62 -0
- package/dist/hooks/handlers/session-start.d.ts +2 -0
- package/dist/hooks/handlers/session-start.js +104 -0
- package/dist/hooks/handlers/turn-end.d.ts +2 -0
- package/dist/hooks/handlers/turn-end.js +31 -0
- package/dist/hooks/handlers/user-prompt.d.ts +2 -0
- package/dist/hooks/handlers/user-prompt.js +55 -0
- package/dist/hooks/local.d.ts +2 -0
- package/dist/hooks/local.js +23 -0
- package/dist/hooks/payload.d.ts +9 -4
- package/dist/hooks/payload.js +26 -17
- package/dist/hooks/prefer-usages.js +2 -118
- package/dist/hooks/route.d.ts +2 -0
- package/dist/hooks/route.js +28 -0
- package/dist/hooks/run.d.ts +14 -0
- package/dist/hooks/run.js +12 -0
- package/dist/hooks/save-turn.d.ts +2 -0
- package/dist/hooks/save-turn.js +11 -0
- package/dist/hooks/session-end.js +2 -63
- package/dist/hooks/session-start.js +2 -90
- package/dist/hooks/turn-end.js +2 -25
- package/dist/hooks/turn-save.js +10 -15
- package/dist/hooks/user-prompt.js +2 -58
- package/dist/search/service.d.ts +1 -0
- package/dist/search/service.js +16 -6
- package/dist/store/adapter.d.ts +7 -5
- package/dist/store/mongo/index.d.ts +3 -6
- package/dist/store/mongo/index.js +5 -0
- package/dist/store/mongo/observations.js +13 -1
- package/dist/store/postgres/driver.d.ts +10 -3
- package/dist/store/postgres/driver.js +12 -0
- package/dist/store/postgres/graph.d.ts +3 -5
- package/dist/store/postgres/graph.js +56 -50
- package/dist/store/postgres/index.d.ts +3 -6
- package/dist/store/postgres/index.js +5 -4
- package/dist/store/postgres/insert.js +98 -50
- package/dist/store/project-resolver.d.ts +3 -0
- package/dist/store/project-resolver.js +34 -23
- package/dist/store/scoped-store.js +1 -1
- package/dist/store/sqlite/graph.d.ts +3 -5
- package/dist/store/sqlite/graph.js +42 -33
- package/dist/store/sqlite/index.d.ts +3 -6
- package/dist/store/sqlite/index.js +3 -0
- package/dist/store/sqlite/observations.js +6 -2
- package/dist/util/job-lock.d.ts +1 -0
- package/dist/util/job-lock.js +50 -10
- package/docs/setup-guide.md +5 -0
- package/package.json +6 -6
|
@@ -1,119 +1,3 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
import { staleFiles } from '../graph/query/stale.js';
|
|
5
|
-
import { repoRootFor } from '../usages/index.js';
|
|
6
|
-
import { DECLARED, isSymbol, isWord, symbolsGreppedIn } from './grep-symbols.js';
|
|
7
|
-
import { refreshGraphInBackground } from './graph-refresh.js';
|
|
8
|
-
import { readPayload, runHook } from './payload.js';
|
|
9
|
-
import { resolveProject } from '../util/project.js';
|
|
10
|
-
import { silenceSqliteWarning } from '../util/warnings.js';
|
|
11
|
-
silenceSqliteWarning();
|
|
12
|
-
const MAX_SYMBOLS = 2;
|
|
13
|
-
const MAX_LINES = 14;
|
|
14
|
-
const TEXT_LINES = 6;
|
|
15
|
-
const EDGE_LIMIT = 20;
|
|
16
|
-
function requested(payload) {
|
|
17
|
-
const input = payload.tool_input ?? {};
|
|
18
|
-
if (payload.tool_name === 'Grep') {
|
|
19
|
-
const pattern = typeof input['pattern'] === 'string' ? input['pattern'] : '';
|
|
20
|
-
return isSymbol(pattern) || isWord(pattern) ? [pattern] : [];
|
|
21
|
-
}
|
|
22
|
-
if (payload.tool_name === 'Bash') {
|
|
23
|
-
return symbolsGreppedIn(typeof input['command'] === 'string' ? input['command'] : '');
|
|
24
|
-
}
|
|
25
|
-
return [];
|
|
26
|
-
}
|
|
27
|
-
function trim(answer) {
|
|
28
|
-
const lines = answer.split('\n');
|
|
29
|
-
if (lines.length <= MAX_LINES)
|
|
30
|
-
return answer;
|
|
31
|
-
return [...lines.slice(0, MAX_LINES), ` ... more via find_usages`].join('\n');
|
|
32
|
-
}
|
|
33
|
-
const answers = (answer, symbol) => !answer.empty && (isSymbol(symbol) || answer.definitions.some((d) => DECLARED.has(d.kind)));
|
|
34
|
-
function block(answer, root) {
|
|
35
|
-
const graph = trim(formatGraph(answer, root, { text: false }));
|
|
36
|
-
return [graph, ...formatText(answer.text, TEXT_LINES)].join('\n');
|
|
37
|
-
}
|
|
38
|
-
const staleNote = (count) => `(${count} file(s) changed since the last scan and are being re-read in the background, ` +
|
|
39
|
-
'so lines in them may have moved. The text matches are live.)';
|
|
40
|
-
async function freshnessNote(store, project) {
|
|
41
|
-
try {
|
|
42
|
-
const root = repoRootFor(project);
|
|
43
|
-
const stale = staleFiles(root, await store.scannedFiles(project));
|
|
44
|
-
if (stale.length === 0)
|
|
45
|
-
return null;
|
|
46
|
-
refreshGraphInBackground(project, root);
|
|
47
|
-
return staleNote(stale.length);
|
|
48
|
-
}
|
|
49
|
-
catch {
|
|
50
|
-
return null;
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
async function answerSymbols(ctx, project, symbols) {
|
|
54
|
-
const answered = { named: [], blocks: [] };
|
|
55
|
-
for (const symbol of symbols) {
|
|
56
|
-
const answer = await answerQuery({
|
|
57
|
-
store: ctx.store,
|
|
58
|
-
root: project,
|
|
59
|
-
project,
|
|
60
|
-
query: { mode: 'usages', symbol, limit: EDGE_LIMIT, suggest: false },
|
|
61
|
-
refresh: false,
|
|
62
|
-
text: false,
|
|
63
|
-
});
|
|
64
|
-
if (!answers(answer, symbol))
|
|
65
|
-
continue;
|
|
66
|
-
addText(answer, project);
|
|
67
|
-
answered.named.push(symbol);
|
|
68
|
-
answered.blocks.push(block(answer, project));
|
|
69
|
-
}
|
|
70
|
-
if (answered.blocks.length > 0) {
|
|
71
|
-
const note = await freshnessNote(ctx.store, project);
|
|
72
|
-
if (note)
|
|
73
|
-
answered.blocks.push(note);
|
|
74
|
-
}
|
|
75
|
-
return answered;
|
|
76
|
-
}
|
|
77
|
-
await runHook(async () => {
|
|
78
|
-
const mode = process.env['CLAUDE_DB_USAGES_HOOK'] ?? 'deny';
|
|
79
|
-
if (mode === 'off')
|
|
80
|
-
return;
|
|
81
|
-
const payload = await readPayload();
|
|
82
|
-
const symbols = requested(payload).slice(0, MAX_SYMBOLS);
|
|
83
|
-
if (symbols.length === 0)
|
|
84
|
-
return;
|
|
85
|
-
const project = resolveProject(payload.cwd);
|
|
86
|
-
const ctx = await createContext();
|
|
87
|
-
let answered;
|
|
88
|
-
try {
|
|
89
|
-
answered = await answerSymbols(ctx, project, symbols);
|
|
90
|
-
}
|
|
91
|
-
finally {
|
|
92
|
-
await ctx.close();
|
|
93
|
-
}
|
|
94
|
-
const { named, blocks } = answered;
|
|
95
|
-
if (blocks.length === 0)
|
|
96
|
-
return;
|
|
97
|
-
const names = named.map((s) => `\`${s}\``).join(', ');
|
|
98
|
-
if (mode === 'directive') {
|
|
99
|
-
process.stdout.write(JSON.stringify({
|
|
100
|
-
hookSpecificOutput: {
|
|
101
|
-
hookEventName: 'PreToolUse',
|
|
102
|
-
additionalContext: `You are grepping the declared symbol(s) ${names}. Use the \`find_usages\` MCP ` +
|
|
103
|
-
`tool for symbol lookups — grep cannot tell a call from an import or show the ` +
|
|
104
|
-
`blast radius. The graph's answer for this search is below; read it instead of ` +
|
|
105
|
-
`the grep output, and call find_usages directly next time:\n\n${blocks.join('\n\n')}`,
|
|
106
|
-
},
|
|
107
|
-
}));
|
|
108
|
-
return;
|
|
109
|
-
}
|
|
110
|
-
process.stdout.write(JSON.stringify({
|
|
111
|
-
hookSpecificOutput: {
|
|
112
|
-
hookEventName: 'PreToolUse',
|
|
113
|
-
permissionDecision: 'deny',
|
|
114
|
-
permissionDecisionReason: `Blocked: ${names} names a declared symbol — use the \`find_usages\` MCP tool ` +
|
|
115
|
-
`for symbol lookups, not grep. Its answer for this search is already below, so ` +
|
|
116
|
-
`nothing needs re-running:\n\n${blocks.join('\n\n')}`,
|
|
117
|
-
},
|
|
118
|
-
}));
|
|
119
|
-
});
|
|
2
|
+
import { routeHook } from './route.js';
|
|
3
|
+
await routeHook('prefer-usages');
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { askDaemon, daemonWanted, forwardedEnv, startDaemon, workerEnv } from '../daemon/client.js';
|
|
2
|
+
import { buildId } from '../daemon/paths.js';
|
|
3
|
+
import { readStdin, runHook } from './payload.js';
|
|
4
|
+
const WAIT_MS = { 'prefer-usages': 8_000 };
|
|
5
|
+
const DEFAULT_WAIT_MS = 50_000;
|
|
6
|
+
async function viaDaemon(name, raw) {
|
|
7
|
+
if (!daemonWanted())
|
|
8
|
+
return false;
|
|
9
|
+
const asked = await askDaemon({ build: buildId(), hook: name, raw, env: forwardedEnv(), workerEnv: workerEnv() }, WAIT_MS[name] ?? DEFAULT_WAIT_MS);
|
|
10
|
+
if (asked.kind === 'done') {
|
|
11
|
+
if (asked.out.length > 0)
|
|
12
|
+
process.stdout.write(asked.out);
|
|
13
|
+
return true;
|
|
14
|
+
}
|
|
15
|
+
if (asked.kind === 'timeout')
|
|
16
|
+
return true;
|
|
17
|
+
startDaemon();
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
export async function routeHook(name) {
|
|
21
|
+
await runHook(async () => {
|
|
22
|
+
const raw = await readStdin();
|
|
23
|
+
if (await viaDaemon(name, raw))
|
|
24
|
+
return;
|
|
25
|
+
const { runLocally } = await import('./local.js');
|
|
26
|
+
await runLocally(name, raw);
|
|
27
|
+
});
|
|
28
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { RecallContext } from '../context.js';
|
|
2
|
+
import type { HookEnv, HookPayload, Write } from './payload.js';
|
|
3
|
+
export interface HookRun {
|
|
4
|
+
payload: HookPayload;
|
|
5
|
+
env: HookEnv;
|
|
6
|
+
write: Write;
|
|
7
|
+
withContext: <T>(work: (ctx: RecallContext) => Promise<T>) => Promise<T>;
|
|
8
|
+
inDaemon: (task: () => Promise<void>) => boolean;
|
|
9
|
+
}
|
|
10
|
+
export type HookHandler = (run: HookRun) => Promise<void>;
|
|
11
|
+
export declare const HOOKS: readonly ["session-start", "user-prompt", "prefer-usages", "turn-end", "session-end"];
|
|
12
|
+
export type HookName = (typeof HOOKS)[number];
|
|
13
|
+
export declare const isHookName: (value: unknown) => value is HookName;
|
|
14
|
+
export declare function loadHandler(name: HookName): Promise<HookHandler>;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export const HOOKS = [
|
|
2
|
+
'session-start',
|
|
3
|
+
'user-prompt',
|
|
4
|
+
'prefer-usages',
|
|
5
|
+
'turn-end',
|
|
6
|
+
'session-end',
|
|
7
|
+
];
|
|
8
|
+
export const isHookName = (value) => typeof value === 'string' && HOOKS.includes(value);
|
|
9
|
+
export async function loadHandler(name) {
|
|
10
|
+
const loaded = (await import(`./handlers/${name}.js`));
|
|
11
|
+
return loaded.default;
|
|
12
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { clearActive } from '../capture/active.js';
|
|
2
|
+
import { flushSession } from '../capture/index.js';
|
|
3
|
+
import { takeTurnSave } from './turn-job.js';
|
|
4
|
+
export async function saveQueuedTurn(sessionId, withContext) {
|
|
5
|
+
const job = takeTurnSave(sessionId);
|
|
6
|
+
if (!job)
|
|
7
|
+
return;
|
|
8
|
+
await withContext((ctx) => flushSession(ctx, sessionId, job.project, job.transcriptPath, false, job.finalReply));
|
|
9
|
+
if (job.activeToken)
|
|
10
|
+
clearActive(sessionId, job.activeToken);
|
|
11
|
+
}
|
|
@@ -1,64 +1,3 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
import { fileURLToPath } from 'node:url';
|
|
5
|
-
import { aiSummary, clearCursor, flushSession, observationsFromTurns, readTranscript, transcriptPathFor, } from '../capture/index.js';
|
|
6
|
-
import { isDue, readState } from '../update.js';
|
|
7
|
-
import { createContext } from '../context.js';
|
|
8
|
-
import { capturingDisabled, readPayload, runHook } from './payload.js';
|
|
9
|
-
import { loadConfig } from '../config/index.js';
|
|
10
|
-
import { resolveProject } from '../util/project.js';
|
|
11
|
-
import { silenceSqliteWarning } from '../util/warnings.js';
|
|
12
|
-
import { distillInBackground } from './background.js';
|
|
13
|
-
import { clearPick, sweepPicks } from '../pick/pending.js';
|
|
14
|
-
import { clearActive, sweepActive } from '../capture/active.js';
|
|
15
|
-
import { dropTurnSave, sweepTurnSaves } from './turn-job.js';
|
|
16
|
-
silenceSqliteWarning();
|
|
17
|
-
await runHook(async () => {
|
|
18
|
-
if (capturingDisabled(loadConfig().capture.scripted))
|
|
19
|
-
return;
|
|
20
|
-
const payload = await readPayload();
|
|
21
|
-
const sessionId = payload.session_id;
|
|
22
|
-
if (!sessionId)
|
|
23
|
-
return;
|
|
24
|
-
const project = resolveProject(payload.cwd);
|
|
25
|
-
const ctx = await createContext();
|
|
26
|
-
try {
|
|
27
|
-
const result = await flushSession(ctx, sessionId, project, payload.transcript_path);
|
|
28
|
-
let summary = result.summary;
|
|
29
|
-
if (!ctx.config.distill.enabled && ctx.config.capture.summarize === 'on') {
|
|
30
|
-
const path = payload.transcript_path ?? transcriptPathFor(project, sessionId);
|
|
31
|
-
const { turns } = readTranscript(path, 0);
|
|
32
|
-
const observations = observationsFromTurns(turns, sessionId, project, ctx.config);
|
|
33
|
-
const ai = await aiSummary(observations.map((obs) => `${obs.title}\n${obs.body}`).join('\n\n'), ctx.config.capture.summarizeModel);
|
|
34
|
-
if (ai)
|
|
35
|
-
summary = `AI: ${ai}`;
|
|
36
|
-
}
|
|
37
|
-
await ctx.store.upsertSession({
|
|
38
|
-
id: sessionId,
|
|
39
|
-
project,
|
|
40
|
-
startedAt: Date.now(),
|
|
41
|
-
endedAt: Date.now(),
|
|
42
|
-
...(summary ? { summary } : {}),
|
|
43
|
-
});
|
|
44
|
-
clearCursor(sessionId);
|
|
45
|
-
clearPick(sessionId);
|
|
46
|
-
sweepPicks();
|
|
47
|
-
dropTurnSave(sessionId);
|
|
48
|
-
sweepTurnSaves();
|
|
49
|
-
clearActive(sessionId);
|
|
50
|
-
sweepActive();
|
|
51
|
-
if (ctx.config.distill.enabled)
|
|
52
|
-
distillInBackground(project, sessionId);
|
|
53
|
-
if (ctx.config.updates !== 'off' && isDue(readState())) {
|
|
54
|
-
const cli = resolve(dirname(fileURLToPath(import.meta.url)), '..', 'cli', 'index.js');
|
|
55
|
-
spawn(process.execPath, [cli, 'update', '--quiet'], {
|
|
56
|
-
detached: true,
|
|
57
|
-
stdio: 'ignore',
|
|
58
|
-
}).unref();
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
finally {
|
|
62
|
-
await ctx.close();
|
|
63
|
-
}
|
|
64
|
-
});
|
|
2
|
+
import { routeHook } from './route.js';
|
|
3
|
+
await routeHook('session-end');
|
|
@@ -1,91 +1,3 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
import { dirname, join, resolve } from 'node:path';
|
|
5
|
-
import { fileURLToPath } from 'node:url';
|
|
6
|
-
import { createContext } from '../context.js';
|
|
7
|
-
import { distillNotice } from '../facts/notice.js';
|
|
8
|
-
import { recentWorkBlock, startFacts } from '../facts/start.js';
|
|
9
|
-
import { startBackgroundWork } from './background.js';
|
|
10
|
-
import { refreshGraphInBackground } from './graph-refresh.js';
|
|
11
|
-
import { recoverAfterCompact } from './compact.js';
|
|
12
|
-
import { capturingDisabled, emitSessionStart, readPayload, runHook } from './payload.js';
|
|
13
|
-
import { markShown } from './shown.js';
|
|
14
|
-
import { legacyBlock } from './start-legacy.js';
|
|
15
|
-
import { resolveProject } from '../util/project.js';
|
|
16
|
-
import { mentionsPath } from '../util/paths.js';
|
|
17
|
-
import { repoRootFor } from '../usages/index.js';
|
|
18
|
-
import { refreshInstalled } from '../cli/refresh.js';
|
|
19
|
-
import { updateNotice } from '../update.js';
|
|
20
|
-
import { silenceSqliteWarning } from '../util/warnings.js';
|
|
21
|
-
silenceSqliteWarning();
|
|
22
|
-
const SERVER = resolve(dirname(fileURLToPath(import.meta.url)), '..', 'mcp', 'server.js');
|
|
23
|
-
const SCAN_HINT = 'Code graph not built yet — run `claude-db scan` once to enable ' +
|
|
24
|
-
'find_usages and the symbol-grep hook.';
|
|
25
|
-
function refreshGraphQuietly(project) {
|
|
26
|
-
try {
|
|
27
|
-
refreshGraphInBackground(project, repoRootFor(project));
|
|
28
|
-
}
|
|
29
|
-
catch {
|
|
30
|
-
return;
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
function refreshInstalledQuietly(project) {
|
|
34
|
-
try {
|
|
35
|
-
refreshInstalled(resolve(dirname(fileURLToPath(import.meta.url)), '..'), project);
|
|
36
|
-
}
|
|
37
|
-
catch {
|
|
38
|
-
return;
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
function mcpRegistered(project) {
|
|
42
|
-
return [join(project, '.mcp.json'), join(homedir(), '.claude.json')].some((path) => {
|
|
43
|
-
try {
|
|
44
|
-
return mentionsPath(readFileSync(path, 'utf8'), SERVER);
|
|
45
|
-
}
|
|
46
|
-
catch {
|
|
47
|
-
return false;
|
|
48
|
-
}
|
|
49
|
-
});
|
|
50
|
-
}
|
|
51
|
-
await runHook(async () => {
|
|
52
|
-
const payload = await readPayload();
|
|
53
|
-
const project = resolveProject(payload.cwd);
|
|
54
|
-
refreshInstalledQuietly(project);
|
|
55
|
-
const ctx = await createContext();
|
|
56
|
-
try {
|
|
57
|
-
const capturing = !capturingDisabled(ctx.config.capture.scripted);
|
|
58
|
-
if (capturing)
|
|
59
|
-
startBackgroundWork(project, ctx.config.database);
|
|
60
|
-
const earlier = payload.source === 'compact' && payload.session_id
|
|
61
|
-
? await recoverAfterCompact(ctx, project, payload.session_id, payload.transcript_path)
|
|
62
|
-
: null;
|
|
63
|
-
const restored = earlier?.ids ?? new Set();
|
|
64
|
-
const facts = await startFacts(ctx, project, restored, payload.session_id);
|
|
65
|
-
const recent = facts ? null : await recentWorkBlock(ctx, project, payload.session_id, restored);
|
|
66
|
-
const legacy = facts ? null : await legacyBlock(ctx, project, restored);
|
|
67
|
-
const shown = [...(facts?.ids ?? []), ...(recent?.ids ?? [])];
|
|
68
|
-
if (shown.length > 0 && payload.session_id)
|
|
69
|
-
markShown(payload.session_id, shown);
|
|
70
|
-
const fallback = legacy?.hasMemory || !recent ? legacy?.text : undefined;
|
|
71
|
-
const parts = [earlier?.block, facts?.block ?? fallback, recent?.block].filter((part) => Boolean(part));
|
|
72
|
-
if ((facts || recent || legacy?.hasMemory) && mcpRegistered(project)) {
|
|
73
|
-
parts.push("Search this project's full history with the memory MCP tools before " +
|
|
74
|
-
'asking the user to re-explain prior decisions.');
|
|
75
|
-
}
|
|
76
|
-
const scanned = (await ctx.store.scannedFiles(project, 1)).length > 0;
|
|
77
|
-
if (!scanned)
|
|
78
|
-
parts.push(SCAN_HINT);
|
|
79
|
-
const update = ctx.config.updates === 'off' ? null : updateNotice();
|
|
80
|
-
if (update)
|
|
81
|
-
parts.push(update);
|
|
82
|
-
const body = parts.join('\n');
|
|
83
|
-
const notice = capturing ? await distillNotice(ctx, project) : null;
|
|
84
|
-
emitSessionStart(`${body}\n(context ≈ ${Math.round(body.length / 4)} tokens)\n`, notice);
|
|
85
|
-
if (scanned)
|
|
86
|
-
refreshGraphQuietly(project);
|
|
87
|
-
}
|
|
88
|
-
finally {
|
|
89
|
-
await ctx.close();
|
|
90
|
-
}
|
|
91
|
-
});
|
|
2
|
+
import { routeHook } from './route.js';
|
|
3
|
+
await routeHook('session-start');
|
package/dist/hooks/turn-end.js
CHANGED
|
@@ -1,26 +1,3 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
import { resolveProject } from '../util/project.js';
|
|
5
|
-
import { TURN_WORKER, runDetached } from './detached.js';
|
|
6
|
-
import { capturingDisabled, readPayload, runHook } from './payload.js';
|
|
7
|
-
import { queueTurnSave } from './turn-job.js';
|
|
8
|
-
await runHook(async () => {
|
|
9
|
-
if (capturingDisabled(loadConfig().capture.scripted))
|
|
10
|
-
return;
|
|
11
|
-
const payload = await readPayload();
|
|
12
|
-
const sessionId = payload.session_id;
|
|
13
|
-
if (!sessionId)
|
|
14
|
-
return;
|
|
15
|
-
const project = resolveProject(payload.cwd);
|
|
16
|
-
const tasks = Array.isArray(payload.background_tasks) ? payload.background_tasks : [];
|
|
17
|
-
const crons = Array.isArray(payload.session_crons) ? payload.session_crons : [];
|
|
18
|
-
const stillWorking = crons.length > 0 || tasks.some((task) => task?.status === 'running');
|
|
19
|
-
queueTurnSave(sessionId, {
|
|
20
|
-
project,
|
|
21
|
-
...(payload.transcript_path ? { transcriptPath: payload.transcript_path } : {}),
|
|
22
|
-
finalReply: payload.last_assistant_message ?? '',
|
|
23
|
-
activeToken: stillWorking ? null : activeToken(sessionId),
|
|
24
|
-
});
|
|
25
|
-
runDetached(TURN_WORKER, [sessionId], project, () => { });
|
|
26
|
-
});
|
|
2
|
+
import { routeHook } from './route.js';
|
|
3
|
+
await routeHook('turn-end');
|
package/dist/hooks/turn-save.js
CHANGED
|
@@ -1,25 +1,20 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import { clearActive } from '../capture/active.js';
|
|
3
|
-
import { flushSession } from '../capture/index.js';
|
|
4
2
|
import { createContext } from '../context.js';
|
|
5
3
|
import { silenceSqliteWarning } from '../util/warnings.js';
|
|
6
4
|
import { runHook } from './payload.js';
|
|
7
|
-
import {
|
|
5
|
+
import { saveQueuedTurn } from './save-turn.js';
|
|
8
6
|
silenceSqliteWarning();
|
|
9
7
|
await runHook(async () => {
|
|
10
8
|
const [sessionId] = process.argv.slice(2);
|
|
11
9
|
if (!sessionId)
|
|
12
10
|
return;
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
}
|
|
23
|
-
if (job.activeToken)
|
|
24
|
-
clearActive(sessionId, job.activeToken);
|
|
11
|
+
await saveQueuedTurn(sessionId, async (work) => {
|
|
12
|
+
const ctx = await createContext();
|
|
13
|
+
try {
|
|
14
|
+
return await work(ctx);
|
|
15
|
+
}
|
|
16
|
+
finally {
|
|
17
|
+
await ctx.close();
|
|
18
|
+
}
|
|
19
|
+
});
|
|
25
20
|
});
|
|
@@ -1,59 +1,3 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
import { createContext } from '../context.js';
|
|
5
|
-
import { capturingDisabled, emitContext, readPayload, runHook } from './payload.js';
|
|
6
|
-
import { loadConfig } from '../config/index.js';
|
|
7
|
-
import { markShown, readShown } from './shown.js';
|
|
8
|
-
import { promptCandidates, strictRecall, withCost } from './prompt-recall.js';
|
|
9
|
-
import { pickInBackground } from './background.js';
|
|
10
|
-
import { clearPick, startPick } from '../pick/pending.js';
|
|
11
|
-
import { takePick } from '../pick/run.js';
|
|
12
|
-
import { resolveProject } from '../util/project.js';
|
|
13
|
-
import { silenceSqliteWarning } from '../util/warnings.js';
|
|
14
|
-
silenceSqliteWarning();
|
|
15
|
-
await runHook(async () => {
|
|
16
|
-
if (capturingDisabled(loadConfig().capture.scripted))
|
|
17
|
-
return;
|
|
18
|
-
const payload = await readPayload();
|
|
19
|
-
const sessionId = payload.session_id;
|
|
20
|
-
if (!sessionId)
|
|
21
|
-
return;
|
|
22
|
-
const project = resolveProject(payload.cwd);
|
|
23
|
-
const ctx = await createContext();
|
|
24
|
-
try {
|
|
25
|
-
await ctx.store.upsertSession({ id: sessionId, project, startedAt: Date.now() });
|
|
26
|
-
recordActive(sessionId, project, payload.prompt ?? '');
|
|
27
|
-
const { lastReply } = await flushSession(ctx, sessionId, project, payload.transcript_path);
|
|
28
|
-
const candidates = await promptCandidates(ctx, {
|
|
29
|
-
prompt: payload.prompt ?? '',
|
|
30
|
-
project,
|
|
31
|
-
sessionId,
|
|
32
|
-
shown: readShown(sessionId),
|
|
33
|
-
});
|
|
34
|
-
if (!candidates) {
|
|
35
|
-
clearPick(sessionId);
|
|
36
|
-
return;
|
|
37
|
-
}
|
|
38
|
-
const fallback = await strictRecall(ctx, candidates);
|
|
39
|
-
if (takePick(ctx.config)) {
|
|
40
|
-
const token = startPick(sessionId, {
|
|
41
|
-
project,
|
|
42
|
-
prompt: redact(candidates.prompt),
|
|
43
|
-
previousReply: redact(lastReply),
|
|
44
|
-
ids: candidates.entries.map((entry) => entry.id),
|
|
45
|
-
fallback: fallback ? { text: withCost(fallback.block), ids: fallback.ids } : null,
|
|
46
|
-
});
|
|
47
|
-
pickInBackground(project, sessionId, token);
|
|
48
|
-
return;
|
|
49
|
-
}
|
|
50
|
-
clearPick(sessionId);
|
|
51
|
-
if (!fallback)
|
|
52
|
-
return;
|
|
53
|
-
markShown(sessionId, fallback.ids);
|
|
54
|
-
emitContext(withCost(fallback.block));
|
|
55
|
-
}
|
|
56
|
-
finally {
|
|
57
|
-
await ctx.close();
|
|
58
|
-
}
|
|
59
|
-
});
|
|
2
|
+
import { routeHook } from './route.js';
|
|
3
|
+
await routeHook('user-prompt');
|
package/dist/search/service.d.ts
CHANGED
|
@@ -7,6 +7,7 @@ export declare class SearchService {
|
|
|
7
7
|
private readonly maxScanCandidates;
|
|
8
8
|
constructor(store: MemoryStore, embedder: Embedder | (() => Promise<Embedder>), maxScanCandidates?: number);
|
|
9
9
|
search(query: SearchQuery): Promise<ObservationIndexEntry[]>;
|
|
10
|
+
private embed;
|
|
10
11
|
private vectorSearch;
|
|
11
12
|
timeline(query: TimelineQuery): Promise<ObservationIndexEntry[]>;
|
|
12
13
|
getObservations(ids: string[]): Promise<Observation[]>;
|
package/dist/search/service.js
CHANGED
|
@@ -17,19 +17,29 @@ export class SearchService {
|
|
|
17
17
|
limit: query.limit * 3,
|
|
18
18
|
maxScanCandidates: query.maxScanCandidates ?? this.maxScanCandidates,
|
|
19
19
|
};
|
|
20
|
+
const embedded = this.embed(wide.text);
|
|
20
21
|
const keyword = await this.store.searchKeyword(wide).catch(() => []);
|
|
21
|
-
const vector = await this.vectorSearch(wide);
|
|
22
|
+
const vector = await this.vectorSearch(wide, await embedded);
|
|
22
23
|
const fused = fuse([keyword, vector].filter((r) => r.length > 0), query.limit);
|
|
23
24
|
return applyRecencyBoost(fused, Date.now());
|
|
24
25
|
}
|
|
25
|
-
async
|
|
26
|
+
async embed(text) {
|
|
26
27
|
try {
|
|
27
28
|
const embedder = typeof this.embedder === 'function' ? await this.embedder() : this.embedder;
|
|
28
29
|
if (embedder.dimensions === 0)
|
|
29
|
-
return
|
|
30
|
-
const [embedding] = await embedder.embed([
|
|
31
|
-
|
|
32
|
-
|
|
30
|
+
return null;
|
|
31
|
+
const [embedding] = await embedder.embed([text]);
|
|
32
|
+
return embedding && embedding.length > 0 ? { embedder, embedding } : null;
|
|
33
|
+
}
|
|
34
|
+
catch {
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
async vectorSearch(query, embedded) {
|
|
39
|
+
if (!embedded)
|
|
40
|
+
return [];
|
|
41
|
+
const { embedder, embedding } = embedded;
|
|
42
|
+
try {
|
|
33
43
|
const candidates = await this.store.searchVector(embedding, {
|
|
34
44
|
...query,
|
|
35
45
|
embedder: embedder.id,
|
package/dist/store/adapter.d.ts
CHANGED
|
@@ -4,6 +4,11 @@ export interface ProjectSummary {
|
|
|
4
4
|
observations: number;
|
|
5
5
|
lastActive: number;
|
|
6
6
|
}
|
|
7
|
+
export interface GraphRows {
|
|
8
|
+
symbols: CodeSymbol[];
|
|
9
|
+
edges: CodeEdge[];
|
|
10
|
+
files: ScannedFile[];
|
|
11
|
+
}
|
|
7
12
|
export interface MemoryStore {
|
|
8
13
|
readonly kind: string;
|
|
9
14
|
init(): Promise<void>;
|
|
@@ -30,11 +35,8 @@ export interface MemoryStore {
|
|
|
30
35
|
timeline(query: TimelineQuery): Promise<ObservationIndexEntry[]>;
|
|
31
36
|
closeObservations(ids: string[]): Promise<number>;
|
|
32
37
|
markReplaced(ids: string[]): Promise<number>;
|
|
33
|
-
upsertGraph(scan:
|
|
34
|
-
|
|
35
|
-
edges: CodeEdge[];
|
|
36
|
-
files: ScannedFile[];
|
|
37
|
-
}): Promise<void>;
|
|
38
|
+
upsertGraph(scan: GraphRows): Promise<void>;
|
|
39
|
+
replaceGraph(project: string, files: string[] | null, scan: GraphRows): Promise<void>;
|
|
38
40
|
findSymbols(filter: SymbolFilter): Promise<CodeSymbol[]>;
|
|
39
41
|
symbolNames(project: string, limit: number): Promise<string[]>;
|
|
40
42
|
findEdges(filter: EdgeFilter): Promise<CodeEdge[]>;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { CodeEdge, CodeSymbol, CountFilter, EdgeFilter, ListFilter, Observation, ObservationIndexEntry, ProjectFilter, RemoveFilter, ScannedFile, SearchQuery, Session, SymbolFilter, TimelineQuery } from '../../types.js';
|
|
2
|
-
import type { MemoryStore, ProjectSummary } from '../adapter.js';
|
|
2
|
+
import type { GraphRows, MemoryStore, ProjectSummary } from '../adapter.js';
|
|
3
3
|
export declare class MongoStore implements MemoryStore {
|
|
4
4
|
private readonly client;
|
|
5
5
|
private readonly db;
|
|
@@ -36,11 +36,8 @@ export declare class MongoStore implements MemoryStore {
|
|
|
36
36
|
markReplaced(ids: string[]): Promise<number>;
|
|
37
37
|
timeline(query: TimelineQuery): Promise<ObservationIndexEntry[]>;
|
|
38
38
|
hasAtlasVectorIndex(): Promise<boolean>;
|
|
39
|
-
upsertGraph(scan:
|
|
40
|
-
|
|
41
|
-
edges: CodeEdge[];
|
|
42
|
-
files: ScannedFile[];
|
|
43
|
-
}): Promise<void>;
|
|
39
|
+
upsertGraph(scan: GraphRows): Promise<void>;
|
|
40
|
+
replaceGraph(project: string, files: string[] | null, scan: GraphRows): Promise<void>;
|
|
44
41
|
findSymbols(filter: SymbolFilter): Promise<CodeSymbol[]>;
|
|
45
42
|
symbolNames(project: string, limit: number): Promise<string[]>;
|
|
46
43
|
findEdges(filter: EdgeFilter): Promise<CodeEdge[]>;
|
|
@@ -121,6 +121,11 @@ export class MongoStore {
|
|
|
121
121
|
async upsertGraph(scan) {
|
|
122
122
|
return graphOps.upsertGraph(this.symbols, this.edges, this.scanned, scan);
|
|
123
123
|
}
|
|
124
|
+
async replaceGraph(project, files, scan) {
|
|
125
|
+
if (files === null || files.length > 0)
|
|
126
|
+
await this.removeGraph(project, files ?? undefined);
|
|
127
|
+
await this.upsertGraph(scan);
|
|
128
|
+
}
|
|
124
129
|
async findSymbols(filter) {
|
|
125
130
|
return graphOps.findSymbols(this.symbols, this.edges, this.scanned, filter);
|
|
126
131
|
}
|
|
@@ -7,7 +7,19 @@ import { noProjects } from '../project-scope.js';
|
|
|
7
7
|
export async function insertObservations(collection, sessions, symbols, edges, scanned, rows) {
|
|
8
8
|
if (rows.length === 0)
|
|
9
9
|
return;
|
|
10
|
-
|
|
10
|
+
const reopening = rows.filter((obs) => obs.status === 'open').map((obs) => obs.id);
|
|
11
|
+
const closed = new Map();
|
|
12
|
+
if (reopening.length > 0) {
|
|
13
|
+
const docs = await collection
|
|
14
|
+
.find({ _id: { $in: reopening }, status: 'done' }, { projection: { files: 1 } })
|
|
15
|
+
.toArray();
|
|
16
|
+
for (const doc of docs)
|
|
17
|
+
closed.set(doc._id, doc.files);
|
|
18
|
+
}
|
|
19
|
+
const kept = (obs) => closed.has(obs.id) && JSON.stringify(closed.get(obs.id)) === JSON.stringify(obs.files)
|
|
20
|
+
? { ...obs, status: 'done' }
|
|
21
|
+
: obs;
|
|
22
|
+
await collection.bulkWrite(rows.map(kept).map((obs) => ({
|
|
11
23
|
replaceOne: {
|
|
12
24
|
filter: { _id: obs.id },
|
|
13
25
|
replacement: toDoc(obs),
|
|
@@ -10,10 +10,17 @@ export interface Pool {
|
|
|
10
10
|
query(sql: string, values?: unknown[]): Promise<QueryResult>;
|
|
11
11
|
connect(): Promise<PoolClient>;
|
|
12
12
|
end(): Promise<void>;
|
|
13
|
+
on(event: 'error', listener: (error: Error) => void): unknown;
|
|
14
|
+
}
|
|
15
|
+
export interface PoolConfig {
|
|
16
|
+
connectionString: string;
|
|
17
|
+
max?: number;
|
|
18
|
+
idleTimeoutMillis?: number;
|
|
19
|
+
allowExitOnIdle?: boolean;
|
|
20
|
+
keepAlive?: boolean;
|
|
13
21
|
}
|
|
14
22
|
export interface PgModule {
|
|
15
|
-
Pool: new (config:
|
|
16
|
-
connectionString: string;
|
|
17
|
-
}) => Pool;
|
|
23
|
+
Pool: new (config: PoolConfig) => Pool;
|
|
18
24
|
}
|
|
19
25
|
export declare function importPg(): Promise<PgModule>;
|
|
26
|
+
export declare function openPool(uri: string): Promise<Pool>;
|
|
@@ -12,3 +12,15 @@ export async function importPg() {
|
|
|
12
12
|
throw new Error('Postgres driver not installed. Run: npm install -g pg');
|
|
13
13
|
}
|
|
14
14
|
}
|
|
15
|
+
export async function openPool(uri) {
|
|
16
|
+
const pg = await importPg();
|
|
17
|
+
const pool = new pg.Pool({
|
|
18
|
+
connectionString: uri,
|
|
19
|
+
max: 4,
|
|
20
|
+
idleTimeoutMillis: 60_000,
|
|
21
|
+
allowExitOnIdle: true,
|
|
22
|
+
keepAlive: true,
|
|
23
|
+
});
|
|
24
|
+
pool.on('error', () => undefined);
|
|
25
|
+
return pool;
|
|
26
|
+
}
|