claude-db 0.12.4 → 0.14.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 +93 -0
- package/dist/capture/cursors.d.ts +6 -0
- package/dist/capture/cursors.js +80 -0
- package/dist/capture/flush.d.ts +1 -3
- package/dist/capture/flush.js +35 -96
- package/dist/capture/index.d.ts +3 -3
- package/dist/capture/index.js +3 -3
- package/dist/capture/progress.js +4 -2
- 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/transcript.d.ts +1 -1
- package/dist/capture/transcript.js +6 -6
- package/dist/capture/vectors.js +2 -1
- package/dist/catchup/gather.js +1 -1
- package/dist/cli/commands/catchup.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 +11 -11
- package/dist/cli/commands/insight.js +10 -1
- package/dist/cli/commands/maintain.js +3 -1
- package/dist/cli/commands/memory.js +2 -1
- package/dist/cli/commands/setup.js +1 -2
- package/dist/cli/commands/sync.js +4 -1
- package/dist/cli/index.js +4 -0
- package/dist/cli/install.js +1 -2
- package/dist/cli/instructions.d.ts +1 -3
- package/dist/cli/instructions.js +5 -40
- package/dist/cli/refresh.js +2 -6
- package/dist/cli/usage.js +1 -0
- package/dist/config/load.js +2 -1
- package/dist/config/schema.d.ts +4 -2
- package/dist/config/schema.js +6 -2
- package/dist/context.d.ts +1 -0
- package/dist/context.js +30 -6
- package/dist/daemon/client.d.ts +14 -0
- package/dist/daemon/client.js +106 -0
- package/dist/daemon/paths.d.ts +18 -0
- package/dist/daemon/paths.js +19 -0
- package/dist/daemon/pid.d.ts +3 -0
- package/dist/daemon/pid.js +20 -0
- package/dist/daemon/server.d.ts +2 -0
- package/dist/daemon/server.js +227 -0
- package/dist/embed/local.js +4 -1
- package/dist/facts/budget.d.ts +1 -0
- package/dist/facts/budget.js +4 -0
- package/dist/facts/claude-memory.js +3 -3
- package/dist/facts/distill.js +2 -2
- package/dist/facts/known.js +1 -1
- package/dist/facts/last-chat.d.ts +1 -1
- package/dist/facts/last-chat.js +5 -4
- package/dist/facts/model.d.ts +1 -1
- package/dist/facts/model.js +31 -16
- package/dist/facts/start-render.d.ts +13 -0
- package/dist/facts/start-render.js +43 -0
- package/dist/facts/start.d.ts +3 -6
- package/dist/facts/start.js +33 -47
- package/dist/graph/grammars.js +2 -1
- package/dist/graph/index.d.ts +1 -1
- package/dist/graph/index.js +1 -1
- package/dist/graph/query/index.d.ts +1 -0
- package/dist/graph/query/index.js +1 -0
- package/dist/graph/query/refresh.js +5 -1
- package/dist/graph/query/stale.js +19 -2
- package/dist/graph/query/usages.d.ts +12 -0
- package/dist/graph/query/usages.js +37 -0
- package/dist/graph/scan/cache.d.ts +1 -0
- package/dist/graph/scan/cache.js +3 -2
- package/dist/graph/scan/files.js +2 -2
- package/dist/graph/scan/persist.js +12 -16
- package/dist/hooks/compact.d.ts +3 -1
- package/dist/hooks/compact.js +4 -3
- package/dist/hooks/detached.js +2 -0
- package/dist/hooks/graph-presence.d.ts +3 -0
- package/dist/hooks/graph-presence.js +42 -0
- package/dist/hooks/grep-symbols.d.ts +3 -1
- package/dist/hooks/grep-symbols.js +41 -2
- package/dist/hooks/handlers/prefer-usages.d.ts +2 -0
- package/dist/hooks/handlers/prefer-usages.js +118 -0
- package/dist/hooks/handlers/session-end.d.ts +2 -0
- package/dist/hooks/handlers/session-end.js +60 -0
- package/dist/hooks/handlers/session-start.d.ts +2 -0
- package/dist/hooks/handlers/session-start.js +83 -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 +22 -0
- package/dist/hooks/payload.d.ts +9 -4
- package/dist/hooks/payload.js +30 -20
- package/dist/hooks/pick-deliver.js +2 -1
- package/dist/hooks/prefer-usages.js +2 -118
- package/dist/hooks/prompt-recall.d.ts +4 -2
- package/dist/hooks/prompt-recall.js +10 -10
- package/dist/hooks/route.d.ts +2 -0
- package/dist/hooks/route.js +33 -0
- package/dist/hooks/run.d.ts +13 -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/mcp/tools/ids.d.ts +3 -0
- package/dist/mcp/tools/ids.js +6 -0
- package/dist/mcp/tools/memory.js +2 -1
- package/dist/mcp/tools/observations.js +3 -2
- package/dist/mcp/tools/search.js +4 -9
- package/dist/mcp/tools/usages.js +18 -44
- package/dist/pick/worker.js +1 -2
- package/dist/search/service.d.ts +6 -0
- package/dist/search/service.js +27 -11
- 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/filters.d.ts +1 -0
- package/dist/store/postgres/filters.js +3 -0
- package/dist/store/postgres/graph.d.ts +3 -5
- package/dist/store/postgres/graph.js +59 -52
- package/dist/store/postgres/index.d.ts +3 -6
- package/dist/store/postgres/index.js +8 -5
- package/dist/store/postgres/insert.js +98 -50
- package/dist/store/postgres/meta.d.ts +1 -0
- package/dist/store/postgres/meta.js +4 -0
- package/dist/store/postgres/observations.js +3 -3
- package/dist/store/project-resolver.d.ts +4 -0
- package/dist/store/project-resolver.js +34 -23
- package/dist/store/scoped-store.js +4 -2
- package/dist/store/sqlite/filters.d.ts +1 -0
- package/dist/store/sqlite/filters.js +6 -2
- package/dist/store/sqlite/graph.d.ts +3 -5
- package/dist/store/sqlite/graph.js +45 -35
- package/dist/store/sqlite/index.d.ts +3 -6
- package/dist/store/sqlite/index.js +3 -0
- package/dist/store/sqlite/observations.js +12 -9
- package/dist/util/claude-binary.js +5 -4
- package/dist/util/claude-cli.js +2 -1
- package/dist/util/claude-dir.d.ts +2 -0
- package/dist/util/claude-dir.js +8 -0
- package/dist/util/env.d.ts +2 -0
- package/dist/util/env.js +6 -0
- package/dist/util/job-lock.d.ts +1 -0
- package/dist/util/job-lock.js +50 -10
- package/dist/util/shortid.d.ts +3 -0
- package/dist/util/shortid.js +11 -0
- package/docs/how-it-works.md +5 -10
- package/docs/setup-guide.md +6 -1
- package/package.json +7 -6
- package/dist/hooks/start-legacy.d.ts +0 -6
- package/dist/hooks/start-legacy.js +0 -47
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { chmodSync, readdirSync, rmSync, statSync } from 'node:fs';
|
|
3
|
+
import { createConnection, createServer } from 'node:net';
|
|
4
|
+
import { join } from 'node:path';
|
|
5
|
+
import { CONFIG_DIR, CONFIG_PATH, loadConfig } from '../config/index.js';
|
|
6
|
+
import { createContext } from '../context.js';
|
|
7
|
+
import { parsePayload } from '../hooks/payload.js';
|
|
8
|
+
import { isHookName, loadHandler } from '../hooks/run.js';
|
|
9
|
+
import { withHookEnv } from '../util/env.js';
|
|
10
|
+
import { silenceSqliteWarning } from '../util/warnings.js';
|
|
11
|
+
import { buildId, socketPath } from './paths.js';
|
|
12
|
+
import { clearDaemonPid, writeDaemonPid } from './pid.js';
|
|
13
|
+
import { STARTING_PREFIX } from './client.js';
|
|
14
|
+
silenceSqliteWarning();
|
|
15
|
+
const BUILD = buildId();
|
|
16
|
+
const SOCKET = socketPath();
|
|
17
|
+
const MAX_REQUEST_BYTES = 8 * 1024 * 1024;
|
|
18
|
+
const RETIRE_AFTER_MS = 30_000;
|
|
19
|
+
const ACK_LINE = `${JSON.stringify({ kind: 'ack' })}\n`;
|
|
20
|
+
let current = null;
|
|
21
|
+
let running = 0;
|
|
22
|
+
const pending = new Set();
|
|
23
|
+
let idleTimer;
|
|
24
|
+
let server;
|
|
25
|
+
let closing = false;
|
|
26
|
+
const log = (message) => {
|
|
27
|
+
process.stderr.write(`${new Date().toISOString()} daemon ${message}\n`);
|
|
28
|
+
};
|
|
29
|
+
function configStamp() {
|
|
30
|
+
try {
|
|
31
|
+
return statSync(CONFIG_PATH).mtimeMs;
|
|
32
|
+
}
|
|
33
|
+
catch {
|
|
34
|
+
return 0;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
function retire(ctx) {
|
|
38
|
+
setTimeout(() => {
|
|
39
|
+
ctx.then((old) => old.close()).catch(() => undefined);
|
|
40
|
+
}, RETIRE_AFTER_MS).unref();
|
|
41
|
+
}
|
|
42
|
+
function context() {
|
|
43
|
+
const stamp = configStamp();
|
|
44
|
+
if (current && current.stamp === stamp)
|
|
45
|
+
return current.ctx;
|
|
46
|
+
if (current)
|
|
47
|
+
retire(current.ctx);
|
|
48
|
+
const ctx = createContext();
|
|
49
|
+
current = { stamp, ctx };
|
|
50
|
+
ctx.catch(() => {
|
|
51
|
+
if (current?.ctx === ctx)
|
|
52
|
+
current = null;
|
|
53
|
+
});
|
|
54
|
+
return ctx;
|
|
55
|
+
}
|
|
56
|
+
function idleMs() {
|
|
57
|
+
try {
|
|
58
|
+
return loadConfig().daemon.idleMinutes * 60_000;
|
|
59
|
+
}
|
|
60
|
+
catch {
|
|
61
|
+
return 15 * 60_000;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
function touch() {
|
|
65
|
+
clearTimeout(idleTimer);
|
|
66
|
+
idleTimer = setTimeout(() => {
|
|
67
|
+
if (running === 0 && pending.size === 0)
|
|
68
|
+
void shutdown('idle');
|
|
69
|
+
else
|
|
70
|
+
touch();
|
|
71
|
+
}, idleMs());
|
|
72
|
+
}
|
|
73
|
+
function track(task) {
|
|
74
|
+
const done = task()
|
|
75
|
+
.catch((error) => log(`background task failed: ${String(error)}`))
|
|
76
|
+
.finally(() => {
|
|
77
|
+
pending.delete(done);
|
|
78
|
+
touch();
|
|
79
|
+
});
|
|
80
|
+
pending.add(done);
|
|
81
|
+
return true;
|
|
82
|
+
}
|
|
83
|
+
async function answer(request) {
|
|
84
|
+
if (request.build !== BUILD) {
|
|
85
|
+
setImmediate(() => void shutdown('replaced by a newer build'));
|
|
86
|
+
return { kind: 'stale' };
|
|
87
|
+
}
|
|
88
|
+
if (request.hook === '__stop') {
|
|
89
|
+
setImmediate(() => void shutdown('asked to stop'));
|
|
90
|
+
return { kind: 'done', out: '' };
|
|
91
|
+
}
|
|
92
|
+
if (!isHookName(request.hook))
|
|
93
|
+
return { kind: 'done', out: '' };
|
|
94
|
+
const out = [];
|
|
95
|
+
const run = {
|
|
96
|
+
payload: parsePayload(request.raw),
|
|
97
|
+
write: (text) => out.push(text),
|
|
98
|
+
withContext: async (work) => work(await context()),
|
|
99
|
+
inDaemon: track,
|
|
100
|
+
};
|
|
101
|
+
try {
|
|
102
|
+
const handle = await loadHandler(request.hook);
|
|
103
|
+
await withHookEnv(request.env, () => handle(run));
|
|
104
|
+
}
|
|
105
|
+
catch (error) {
|
|
106
|
+
log(`${request.hook} failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
107
|
+
}
|
|
108
|
+
return { kind: 'done', out: out.join('') };
|
|
109
|
+
}
|
|
110
|
+
const isStringMap = (value) => typeof value === 'object' &&
|
|
111
|
+
value !== null &&
|
|
112
|
+
Object.values(value).every((entry) => typeof entry === 'string');
|
|
113
|
+
function parseRequest(line) {
|
|
114
|
+
let parsed;
|
|
115
|
+
try {
|
|
116
|
+
parsed = JSON.parse(line);
|
|
117
|
+
}
|
|
118
|
+
catch {
|
|
119
|
+
return null;
|
|
120
|
+
}
|
|
121
|
+
if (typeof parsed !== 'object' || parsed === null)
|
|
122
|
+
return null;
|
|
123
|
+
const { build, hook, raw, env } = parsed;
|
|
124
|
+
if (typeof build !== 'string' || typeof hook !== 'string' || typeof raw !== 'string')
|
|
125
|
+
return null;
|
|
126
|
+
if (!isStringMap(env))
|
|
127
|
+
return null;
|
|
128
|
+
return { build, hook, raw, env };
|
|
129
|
+
}
|
|
130
|
+
function serve(socket) {
|
|
131
|
+
let buffer = '';
|
|
132
|
+
socket.setEncoding('utf8');
|
|
133
|
+
socket.on('error', () => socket.destroy());
|
|
134
|
+
socket.on('data', (chunk) => {
|
|
135
|
+
buffer += chunk;
|
|
136
|
+
if (buffer.length > MAX_REQUEST_BYTES) {
|
|
137
|
+
socket.destroy();
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
const end = buffer.indexOf('\n');
|
|
141
|
+
if (end < 0)
|
|
142
|
+
return;
|
|
143
|
+
const line = buffer.slice(0, end);
|
|
144
|
+
buffer = '';
|
|
145
|
+
socket.pause();
|
|
146
|
+
running += 1;
|
|
147
|
+
touch();
|
|
148
|
+
const request = parseRequest(line);
|
|
149
|
+
if (!request) {
|
|
150
|
+
running -= 1;
|
|
151
|
+
socket.destroy();
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
socket.write(ACK_LINE);
|
|
155
|
+
void answer(request)
|
|
156
|
+
.then((reply) => socket.end(`${JSON.stringify(reply)}\n`))
|
|
157
|
+
.catch((error) => {
|
|
158
|
+
log(`request failed: ${String(error)}`);
|
|
159
|
+
socket.destroy();
|
|
160
|
+
})
|
|
161
|
+
.finally(() => {
|
|
162
|
+
running -= 1;
|
|
163
|
+
touch();
|
|
164
|
+
});
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
async function shutdown(reason) {
|
|
168
|
+
if (closing)
|
|
169
|
+
return;
|
|
170
|
+
closing = true;
|
|
171
|
+
log(`stopping: ${reason}`);
|
|
172
|
+
clearTimeout(idleTimer);
|
|
173
|
+
server?.close();
|
|
174
|
+
await Promise.allSettled([...pending]);
|
|
175
|
+
clearDaemonPid(process.pid);
|
|
176
|
+
if (current)
|
|
177
|
+
await current.ctx.then((ctx) => ctx.close()).catch(() => undefined);
|
|
178
|
+
process.exit(0);
|
|
179
|
+
}
|
|
180
|
+
function clearStartMarkers() {
|
|
181
|
+
try {
|
|
182
|
+
for (const name of readdirSync(CONFIG_DIR)) {
|
|
183
|
+
if (name.startsWith(STARTING_PREFIX))
|
|
184
|
+
rmSync(join(CONFIG_DIR, name), { force: true });
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
catch {
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
function inUse() {
|
|
192
|
+
return new Promise((settle) => {
|
|
193
|
+
const probe = createConnection(SOCKET);
|
|
194
|
+
probe.on('connect', () => {
|
|
195
|
+
probe.destroy();
|
|
196
|
+
settle(true);
|
|
197
|
+
});
|
|
198
|
+
probe.on('error', () => settle(false));
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
async function start() {
|
|
202
|
+
if (await inUse()) {
|
|
203
|
+
log('another daemon is already running');
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
if (process.platform !== 'win32')
|
|
207
|
+
rmSync(SOCKET, { force: true });
|
|
208
|
+
server = createServer(serve);
|
|
209
|
+
server.on('error', (error) => {
|
|
210
|
+
log(`could not listen: ${error.message}`);
|
|
211
|
+
process.exit(0);
|
|
212
|
+
});
|
|
213
|
+
server.listen(SOCKET, () => {
|
|
214
|
+
if (process.platform !== 'win32')
|
|
215
|
+
chmodSync(SOCKET, 0o600);
|
|
216
|
+
writeDaemonPid();
|
|
217
|
+
clearStartMarkers();
|
|
218
|
+
log(`listening on ${SOCKET}`);
|
|
219
|
+
touch();
|
|
220
|
+
void context()
|
|
221
|
+
.then((ctx) => ctx.embedder())
|
|
222
|
+
.catch((error) => log(`could not open the store: ${String(error)}`));
|
|
223
|
+
});
|
|
224
|
+
process.on('SIGTERM', () => void shutdown('SIGTERM'));
|
|
225
|
+
process.on('SIGINT', () => void shutdown('SIGINT'));
|
|
226
|
+
}
|
|
227
|
+
await start();
|
package/dist/embed/local.js
CHANGED
|
@@ -24,7 +24,10 @@ export class LocalEmbedder {
|
|
|
24
24
|
}
|
|
25
25
|
transformers.env.allowLocalModels = false;
|
|
26
26
|
return (await transformers.pipeline('feature-extraction', this.id));
|
|
27
|
-
})()
|
|
27
|
+
})().catch((error) => {
|
|
28
|
+
this.pipelinePromise = null;
|
|
29
|
+
throw error;
|
|
30
|
+
});
|
|
28
31
|
return this.pipelinePromise;
|
|
29
32
|
}
|
|
30
33
|
}
|
package/dist/facts/budget.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { BudgetUsage } from '../util/daily-budget.js';
|
|
2
2
|
export declare function distillUsage(now?: number): BudgetUsage;
|
|
3
|
+
export declare function distillBlocked(limit: number, now?: number): boolean;
|
|
3
4
|
export declare function takeDistillCalls(limit: number, count: number, now?: number): boolean;
|
|
4
5
|
export declare function recordDistillFailure(reason: string, now?: number): void;
|
|
5
6
|
export declare function recordDistillSuccess(now?: number): void;
|
package/dist/facts/budget.js
CHANGED
|
@@ -4,6 +4,10 @@ const BUDGET = 'distill';
|
|
|
4
4
|
export function distillUsage(now = Date.now()) {
|
|
5
5
|
return budgetUsage(BUDGET, now);
|
|
6
6
|
}
|
|
7
|
+
export function distillBlocked(limit, now = Date.now()) {
|
|
8
|
+
const { used, pausedUntil } = budgetUsage(BUDGET, now);
|
|
9
|
+
return pausedUntil > now || used >= limit;
|
|
10
|
+
}
|
|
7
11
|
export function takeDistillCalls(limit, count, now = Date.now()) {
|
|
8
12
|
return takeBudget(BUDGET, limit, count, now);
|
|
9
13
|
}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { readdirSync, readFileSync, statSync } from 'node:fs';
|
|
2
|
-
import { homedir } from 'node:os';
|
|
3
2
|
import { join } from 'node:path';
|
|
4
3
|
import { embedObservations } from '../capture/flush.js';
|
|
4
|
+
import { claudeProjectsDir } from '../util/claude-dir.js';
|
|
5
5
|
import { CLAUDE_MEMORY_TAG, FACT_SESSION, factToObservation, machineTag, originTag, youScope, } from './model.js';
|
|
6
6
|
const MAX_TITLE = 200;
|
|
7
7
|
export function claudeMemoryDir(project) {
|
|
8
|
-
return join(
|
|
8
|
+
return join(claudeProjectsDir(), project.replace(/[^a-zA-Z0-9]/g, '-'), 'memory');
|
|
9
9
|
}
|
|
10
10
|
export function parseMemoryFile(text, fallbackTime) {
|
|
11
11
|
const match = /^---\r?\n([\s\S]*?)\r?\n---\r?\n?([\s\S]*)$/.exec(text);
|
|
@@ -87,7 +87,7 @@ function memoryFiles(dir) {
|
|
|
87
87
|
async function importedFacts(ctx, project) {
|
|
88
88
|
const mine = [machineTag(), originTag(project)];
|
|
89
89
|
const rows = [];
|
|
90
|
-
for (const owner of [project, youScope()]) {
|
|
90
|
+
for (const owner of [project, youScope(project)]) {
|
|
91
91
|
const batch = await ctx.store.list({
|
|
92
92
|
project: owner,
|
|
93
93
|
sessionId: FACT_SESSION,
|
package/dist/facts/distill.js
CHANGED
|
@@ -62,7 +62,7 @@ async function distillWindow(ctx, project, chat, source, at, run) {
|
|
|
62
62
|
const retiring = ops
|
|
63
63
|
.filter((op) => op.op === 'retire' && !kept.has(op.key))
|
|
64
64
|
.flatMap((op) => existing.filter((fact) => fact.key === op.key))
|
|
65
|
-
.map((fact) => factId(fact.scope === 'you' ? youScope() : project, fact.key));
|
|
65
|
+
.map((fact) => factId(fact.scope === 'you' ? youScope(project) : project, fact.key));
|
|
66
66
|
const retired = await ctx.store.markReplaced(retiring);
|
|
67
67
|
return { ok: true, set: facts.length, retired, texts: sets.map((op) => op.text) };
|
|
68
68
|
}
|
|
@@ -132,5 +132,5 @@ export async function factCounts(ctx, project) {
|
|
|
132
132
|
const scope = { project: owner, sessionId: FACT_SESSION };
|
|
133
133
|
return ((await ctx.store.count(scope)) - (await ctx.store.count({ ...scope, status: 'replaced' })));
|
|
134
134
|
};
|
|
135
|
-
return { project: await count(project), you: await count(youScope()) };
|
|
135
|
+
return { project: await count(project), you: await count(youScope(project)) };
|
|
136
136
|
}
|
package/dist/facts/known.js
CHANGED
|
@@ -3,7 +3,7 @@ import type { Observation } from '../types.js';
|
|
|
3
3
|
export interface RecentChat {
|
|
4
4
|
sessionId: string;
|
|
5
5
|
at: number;
|
|
6
|
-
summary: string
|
|
6
|
+
summary: string[];
|
|
7
7
|
turns: Observation[];
|
|
8
8
|
}
|
|
9
9
|
export declare function recentChats(ctx: RecallContext, project: string, current?: string): Promise<RecentChat[]>;
|
package/dist/facts/last-chat.js
CHANGED
|
@@ -29,11 +29,12 @@ function toChat(sessionId, newestFirst, session) {
|
|
|
29
29
|
}
|
|
30
30
|
function chatSummary(session, at, turns) {
|
|
31
31
|
if (!session?.summary || session.endedAt === undefined || session.endedAt < at)
|
|
32
|
-
return
|
|
32
|
+
return [];
|
|
33
33
|
const shown = new Set(turns.map((turn) => turn.title));
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
34
|
+
return session.summary
|
|
35
|
+
.split(SUMMARY_PARTS)
|
|
36
|
+
.filter((part) => !shown.has(part))
|
|
37
|
+
.flatMap((part) => toSnippet(part, SUMMARY_CHARS) ?? []);
|
|
37
38
|
}
|
|
38
39
|
export function latestRequestAt(chats, unfinished) {
|
|
39
40
|
const times = [...chats.map((chat) => chat.at), ...unfinished.map((entry) => entry.askedAt)];
|
package/dist/facts/model.d.ts
CHANGED
|
@@ -17,7 +17,7 @@ export interface Fact {
|
|
|
17
17
|
source: string;
|
|
18
18
|
tags?: string[];
|
|
19
19
|
}
|
|
20
|
-
export declare function youScope(): string;
|
|
20
|
+
export declare function youScope(project?: string, now?: () => number): string;
|
|
21
21
|
export declare function machineTag(): string;
|
|
22
22
|
export declare function originTag(project: string): string;
|
|
23
23
|
export declare function scopeProject(scope: FactScope, project: string): string;
|
package/dist/facts/model.js
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import { execFileSync } from 'node:child_process';
|
|
2
|
-
import {
|
|
2
|
+
import { existsSync } from 'node:fs';
|
|
3
|
+
import { homedir, hostname } from 'node:os';
|
|
4
|
+
import { isAbsolute } from 'node:path';
|
|
3
5
|
import { currentAuthor, observationId } from '../capture/identity.js';
|
|
4
6
|
import { redact } from '../capture/redact.js';
|
|
7
|
+
import { SCOPE_TTL_MS } from '../store/project-resolver.js';
|
|
5
8
|
import { scopeToken } from '../util/scope.js';
|
|
6
9
|
export const FACT_SESSION = 'facts';
|
|
7
10
|
export const FACT_TAG = 'fact';
|
|
@@ -22,20 +25,32 @@ const LABELS = {
|
|
|
22
25
|
todo: 'To do',
|
|
23
26
|
fact: 'Fact',
|
|
24
27
|
};
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
if (
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
28
|
+
const you = new Map();
|
|
29
|
+
function folderOf(project) {
|
|
30
|
+
if (project && isAbsolute(project) && existsSync(project))
|
|
31
|
+
return project;
|
|
32
|
+
return homedir();
|
|
33
|
+
}
|
|
34
|
+
function gitEmail(folder) {
|
|
35
|
+
try {
|
|
36
|
+
return execFileSync('git', ['config', '--get', 'user.email'], {
|
|
37
|
+
cwd: folder,
|
|
38
|
+
encoding: 'utf8',
|
|
39
|
+
stdio: ['ignore', 'pipe', 'ignore'],
|
|
40
|
+
}).trim();
|
|
41
|
+
}
|
|
42
|
+
catch {
|
|
43
|
+
return '';
|
|
37
44
|
}
|
|
38
|
-
|
|
45
|
+
}
|
|
46
|
+
export function youScope(project, now = Date.now) {
|
|
47
|
+
const folder = folderOf(project);
|
|
48
|
+
const cached = you.get(folder);
|
|
49
|
+
if (cached && now() - cached.at < SCOPE_TTL_MS)
|
|
50
|
+
return cached.scope;
|
|
51
|
+
const scope = `@you:${gitEmail(folder) || currentAuthor() || 'unknown'}`;
|
|
52
|
+
you.set(folder, { at: now(), scope });
|
|
53
|
+
return scope;
|
|
39
54
|
}
|
|
40
55
|
export function machineTag() {
|
|
41
56
|
return `machine:${scopeToken(hostname())}`;
|
|
@@ -44,7 +59,7 @@ export function originTag(project) {
|
|
|
44
59
|
return `origin:${scopeToken(project)}`;
|
|
45
60
|
}
|
|
46
61
|
export function scopeProject(scope, project) {
|
|
47
|
-
return scope === 'you' ? youScope() : project;
|
|
62
|
+
return scope === 'you' ? youScope(project) : project;
|
|
48
63
|
}
|
|
49
64
|
export function factId(owner, key) {
|
|
50
65
|
return observationId(FACT_SESSION, 0, `${owner}\0${key}`);
|
|
@@ -71,7 +86,7 @@ export function factLabel(obs) {
|
|
|
71
86
|
const type = factType(obs);
|
|
72
87
|
if (type)
|
|
73
88
|
return LABELS[type];
|
|
74
|
-
return obs.sessionId === 'manual' ? LABELS.rule : null;
|
|
89
|
+
return obs.sessionId === 'manual' || obs.kind === 'preference' ? LABELS.rule : null;
|
|
75
90
|
}
|
|
76
91
|
export function factToObservation(fact, project) {
|
|
77
92
|
const owner = scopeProject(fact.scope, project);
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export interface StartFacts {
|
|
2
|
+
block: string;
|
|
3
|
+
ids: Set<string>;
|
|
4
|
+
}
|
|
5
|
+
export interface Entry {
|
|
6
|
+
texts: string[];
|
|
7
|
+
id: string | null;
|
|
8
|
+
line: (texts: string[]) => string;
|
|
9
|
+
}
|
|
10
|
+
export type Section = [string, Entry[]];
|
|
11
|
+
export declare function entry(line: string, id: string | null, text: string): Entry;
|
|
12
|
+
export declare function summaryEntry(parts: string[]): Entry;
|
|
13
|
+
export declare function render(sections: Section[], maxChars: number, earlier?: ReadonlySet<string>): StartFacts | null;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
const SUMMARY_JOIN = '; ';
|
|
2
|
+
export function entry(line, id, text) {
|
|
3
|
+
return { texts: [text], id, line: () => line };
|
|
4
|
+
}
|
|
5
|
+
export function summaryEntry(parts) {
|
|
6
|
+
return { texts: parts, id: null, line: (kept) => `- Summary: ${kept.join(SUMMARY_JOIN)}` };
|
|
7
|
+
}
|
|
8
|
+
const textKey = (text) => text.toLowerCase().replace(/\s+/g, ' ').trim();
|
|
9
|
+
function unseen(texts, seen) {
|
|
10
|
+
const keys = texts.map(textKey);
|
|
11
|
+
return texts.filter((_, index) => {
|
|
12
|
+
const key = keys[index] ?? '';
|
|
13
|
+
return !seen.has(key) && keys.indexOf(key) === index;
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
export function render(sections, maxChars, earlier = new Set()) {
|
|
17
|
+
const seen = new Set([...earlier].map(textKey));
|
|
18
|
+
const ids = new Set();
|
|
19
|
+
const blocks = [];
|
|
20
|
+
let budget = maxChars;
|
|
21
|
+
for (const [heading, entries] of sections) {
|
|
22
|
+
const lines = [];
|
|
23
|
+
for (const item of entries) {
|
|
24
|
+
const kept = unseen(item.texts, seen);
|
|
25
|
+
if (kept.length > 0) {
|
|
26
|
+
const line = item.line(kept);
|
|
27
|
+
if (line.length > budget)
|
|
28
|
+
break;
|
|
29
|
+
budget -= line.length + 1;
|
|
30
|
+
for (const text of kept)
|
|
31
|
+
seen.add(textKey(text));
|
|
32
|
+
lines.push(line);
|
|
33
|
+
}
|
|
34
|
+
if (item.id)
|
|
35
|
+
ids.add(item.id);
|
|
36
|
+
}
|
|
37
|
+
if (lines.length > 0)
|
|
38
|
+
blocks.push([heading, ...lines].join('\n'));
|
|
39
|
+
}
|
|
40
|
+
if (blocks.length === 0)
|
|
41
|
+
return null;
|
|
42
|
+
return { block: ['<memory>', blocks.join('\n\n'), '</memory>'].join('\n'), ids };
|
|
43
|
+
}
|
package/dist/facts/start.d.ts
CHANGED
|
@@ -1,7 +1,4 @@
|
|
|
1
1
|
import type { RecallContext } from '../context.js';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
}
|
|
6
|
-
export declare function startFacts(ctx: RecallContext, project: string, hidden?: ReadonlySet<string>, current?: string): Promise<StartFacts | null>;
|
|
7
|
-
export declare function recentWorkBlock(ctx: RecallContext, project: string, current?: string, hidden?: ReadonlySet<string>): Promise<StartFacts | null>;
|
|
2
|
+
import type { StartFacts } from './start-render.js';
|
|
3
|
+
export type { StartFacts } from './start-render.js';
|
|
4
|
+
export declare function startFacts(ctx: RecallContext, project: string, hidden?: ReadonlySet<string>, current?: string, earlier?: ReadonlySet<string>): Promise<StartFacts | null>;
|
package/dist/facts/start.js
CHANGED
|
@@ -4,36 +4,38 @@ import { activeRequests } from '../capture/active.js';
|
|
|
4
4
|
import { handoffBodyLines, handoffHeading, newestHandoff, passedNote } from './handoff.js';
|
|
5
5
|
import { latestRequestAt, recentChats, turnLine } from './last-chat.js';
|
|
6
6
|
import { MANUAL_SESSION, factLine, seenByClaude } from './render.js';
|
|
7
|
+
import { entry, render, summaryEntry } from './start-render.js';
|
|
7
8
|
import { formatDay, formatDayTime } from '../util/day.js';
|
|
8
9
|
import { toShortId } from '../util/shortid.js';
|
|
9
10
|
const ABOUT_YOU = 6;
|
|
10
11
|
const THIS_PROJECT = 10;
|
|
11
12
|
const TODOS = 4;
|
|
12
13
|
const UNCOMMITTED = 3;
|
|
14
|
+
const CHAT_RULES = 8;
|
|
13
15
|
const ORDER = ['rule', 'decision', 'deadend', 'fact'];
|
|
14
|
-
export async function startFacts(ctx, project, hidden = new Set(), current) {
|
|
16
|
+
export async function startFacts(ctx, project, hidden = new Set(), current, earlier = new Set()) {
|
|
15
17
|
const visible = (obs) => obs.status !== 'replaced' && !hidden.has(obs.id) && !seenByClaude(obs, project);
|
|
16
18
|
const list = async (owner, sessionId, limit) => (await ctx.store.list({ project: owner, sessionId, newest: true, limit, bodies: false })).filter(visible);
|
|
17
|
-
const you = onePerKey(await list(youScope(), FACT_SESSION, 50));
|
|
19
|
+
const you = onePerKey(await list(youScope(project), FACT_SESSION, 50));
|
|
18
20
|
const facts = onePerKey(await list(project, FACT_SESSION, 300));
|
|
19
21
|
const handoff = await newestHandoff(ctx, project);
|
|
20
22
|
const shownHandoff = handoff && visible(handoff) ? handoff : null;
|
|
21
23
|
const manual = (await list(project, MANUAL_SESSION, 20)).filter((obs) => obs.id !== handoff?.id);
|
|
22
|
-
|
|
23
|
-
return null;
|
|
24
|
+
const chatRules = await chatRulesFor(ctx, project, [...facts, ...manual], visible);
|
|
24
25
|
const rank = (obs) => {
|
|
25
26
|
const index = ORDER.indexOf(factType(obs) ?? 'rule');
|
|
26
27
|
return index === -1 ? ORDER.length : index;
|
|
27
28
|
};
|
|
28
|
-
const known = [...manual, ...facts.filter((obs) => factType(obs) !== 'todo')].sort((a, b) => rank(a) - rank(b) || b.createdAt - a.createdAt);
|
|
29
|
+
const known = [...manual, ...chatRules, ...facts.filter((obs) => factType(obs) !== 'todo')].sort((a, b) => rank(a) - rank(b) || b.createdAt - a.createdAt);
|
|
29
30
|
const todos = facts.filter((obs) => factType(obs) === 'todo').slice(0, TODOS);
|
|
30
31
|
const uncommitted = (await openWork(ctx.store, project))
|
|
31
32
|
.filter((obs) => !hidden.has(obs.id))
|
|
32
33
|
.slice(0, UNCOMMITTED);
|
|
33
34
|
const recent = await recentWork(ctx, project, current);
|
|
35
|
+
const factEntry = (obs) => entry(factLine(obs), obs.id, obs.title);
|
|
34
36
|
const sections = [
|
|
35
|
-
['About you:', you.slice(0, ABOUT_YOU).map(
|
|
36
|
-
['This project:', known.slice(0, THIS_PROJECT).map(
|
|
37
|
+
['About you:', you.slice(0, ABOUT_YOU).map(factEntry)],
|
|
38
|
+
['This project:', known.slice(0, THIS_PROJECT).map(factEntry)],
|
|
37
39
|
...recentSections(recent, hidden),
|
|
38
40
|
[
|
|
39
41
|
shownHandoff
|
|
@@ -44,12 +46,25 @@ export async function startFacts(ctx, project, hidden = new Set(), current) {
|
|
|
44
46
|
[
|
|
45
47
|
'Where you stopped:',
|
|
46
48
|
[
|
|
47
|
-
...todos.map(
|
|
48
|
-
...uncommitted.map((obs) =>
|
|
49
|
+
...todos.map(factEntry),
|
|
50
|
+
...uncommitted.map((obs) => entry(`- Not committed: ${obs.title}`, obs.id, obs.title)),
|
|
49
51
|
],
|
|
50
52
|
],
|
|
51
53
|
];
|
|
52
|
-
return render(sections, ctx.config.inject.maxChars);
|
|
54
|
+
return render(sections, ctx.config.inject.maxChars, earlier);
|
|
55
|
+
}
|
|
56
|
+
async function chatRulesFor(ctx, project, rulesAndFacts, visible) {
|
|
57
|
+
if (rulesAndFacts.some((obs) => obs.kind === 'preference'))
|
|
58
|
+
return [];
|
|
59
|
+
const rows = await ctx.store.list({
|
|
60
|
+
project,
|
|
61
|
+
kind: 'preference',
|
|
62
|
+
excludeSessions: [FACT_SESSION, MANUAL_SESSION],
|
|
63
|
+
newest: true,
|
|
64
|
+
limit: CHAT_RULES,
|
|
65
|
+
bodies: false,
|
|
66
|
+
});
|
|
67
|
+
return rows.filter(visible);
|
|
53
68
|
}
|
|
54
69
|
async function recentWork(ctx, project, current) {
|
|
55
70
|
const unfinished = activeRequests(await ctx.store.projectScope(project), current);
|
|
@@ -62,50 +77,21 @@ function recentSections(recent, hidden) {
|
|
|
62
77
|
...recent.chats.map((chat, index) => chatSection(chat, index, hidden)),
|
|
63
78
|
];
|
|
64
79
|
}
|
|
65
|
-
|
|
66
|
-
const sections = recentSections(await recentWork(ctx, project, current), hidden);
|
|
67
|
-
return sections.some(([, entries]) => entries.length > 0)
|
|
68
|
-
? render(sections, ctx.config.inject.maxChars)
|
|
69
|
-
: null;
|
|
70
|
-
}
|
|
71
|
-
function render(sections, maxChars) {
|
|
72
|
-
const lines = ['<memory>'];
|
|
73
|
-
const ids = new Set();
|
|
74
|
-
let budget = maxChars;
|
|
75
|
-
for (const [heading, entries] of sections) {
|
|
76
|
-
if (entries.length === 0)
|
|
77
|
-
continue;
|
|
78
|
-
if (lines.length > 1)
|
|
79
|
-
lines.push('');
|
|
80
|
-
lines.push(heading);
|
|
81
|
-
for (const [line, id] of entries) {
|
|
82
|
-
if (line.length > budget)
|
|
83
|
-
break;
|
|
84
|
-
budget -= line.length + 1;
|
|
85
|
-
lines.push(line);
|
|
86
|
-
if (id)
|
|
87
|
-
ids.add(id);
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
lines.push('</memory>');
|
|
91
|
-
return { block: lines.join('\n'), ids };
|
|
92
|
-
}
|
|
93
|
-
function unfinishedSection(entry) {
|
|
80
|
+
function unfinishedSection(item) {
|
|
94
81
|
return [
|
|
95
|
-
`Not finished yet in another chat (${formatDayTime(
|
|
96
|
-
[
|
|
82
|
+
`Not finished yet in another chat (${formatDayTime(item.askedAt)}):`,
|
|
83
|
+
[entry(`- asked "${item.request}"`, null, item.request)],
|
|
97
84
|
];
|
|
98
85
|
}
|
|
99
86
|
function chatSection(chat, index, hidden) {
|
|
100
87
|
const heading = `${index === 0 ? 'Last chat' : 'Chat before'} (${formatDay(chat.at)}):`;
|
|
101
88
|
const turns = chat.turns
|
|
102
89
|
.filter((turn) => !hidden.has(turn.id))
|
|
103
|
-
.map((turn) =>
|
|
104
|
-
return [heading, chat.summary ? [
|
|
90
|
+
.map((turn) => entry(`- ${turnLine(turn)} (${toShortId(turn.id)})`, turn.id, turn.title));
|
|
91
|
+
return [heading, chat.summary.length > 0 ? [summaryEntry(chat.summary), ...turns] : turns];
|
|
105
92
|
}
|
|
106
93
|
function handoffEntries(handoff) {
|
|
107
|
-
return handoffBodyLines(handoff).map((line, index) =>
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
]);
|
|
94
|
+
return handoffBodyLines(handoff).map((line, index) => index === 0
|
|
95
|
+
? entry(`${line} (${toShortId(handoff.id)})`, handoff.id, line)
|
|
96
|
+
: entry(line, null, line));
|
|
111
97
|
}
|
package/dist/graph/grammars.js
CHANGED
|
@@ -2,11 +2,12 @@ import { statSync } from 'node:fs';
|
|
|
2
2
|
import { createRequire } from 'node:module';
|
|
3
3
|
import { join } from 'node:path';
|
|
4
4
|
import { CONFIG_DIR } from '../config/dir.js';
|
|
5
|
+
import { hookEnv } from '../util/env.js';
|
|
5
6
|
export const GRAMMARS = ['go', 'java', 'kotlin', 'python', 'ruby', 'rust'];
|
|
6
7
|
export const PLATFORM = `${process.platform}-${process.arch}`;
|
|
7
8
|
export const PLATFORM_PACKAGE = `claude-db-grammars-${PLATFORM}`;
|
|
8
9
|
const loaded = new Map();
|
|
9
|
-
export const grammarHome = () =>
|
|
10
|
+
export const grammarHome = () => hookEnv()['CLAUDE_DB_GRAMMARS'] ?? join(CONFIG_DIR, 'grammars');
|
|
10
11
|
const places = () => [
|
|
11
12
|
createRequire(import.meta.url),
|
|
12
13
|
createRequire(join(grammarHome(), 'package.json')),
|
package/dist/graph/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
export { LANGUAGES, languageFor, languageNames } from './languages/index.js';
|
|
2
2
|
export { SCAN_VERSION, currentHashes, hashOf, saveScan, scanRepository } from './scan/index.js';
|
|
3
|
-
export { NO_GRAPH, NO_GRAPH_FOR_PATH, addText, answerQuery, formatGraph, formatText, hasGraph, nearest, queryGraph, refreshGraph, suggestFor, } from './query/index.js';
|
|
3
|
+
export { NO_GRAPH, NO_GRAPH_FOR_PATH, addText, answerQuery, formatGraph, formatText, hasGraph, nearest, queryGraph, refreshGraph, suggestFor, usagesAnswer, } from './query/index.js';
|
package/dist/graph/index.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
export { LANGUAGES, languageFor, languageNames } from './languages/index.js';
|
|
2
2
|
export { SCAN_VERSION, currentHashes, hashOf, saveScan, scanRepository } from './scan/index.js';
|
|
3
|
-
export { NO_GRAPH, NO_GRAPH_FOR_PATH, addText, answerQuery, formatGraph, formatText, hasGraph, nearest, queryGraph, refreshGraph, suggestFor, } from './query/index.js';
|
|
3
|
+
export { NO_GRAPH, NO_GRAPH_FOR_PATH, addText, answerQuery, formatGraph, formatText, hasGraph, nearest, queryGraph, refreshGraph, suggestFor, usagesAnswer, } from './query/index.js';
|