claude-db 0.12.3 → 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 +57 -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 +27 -60
- package/dist/capture/index.d.ts +3 -3
- package/dist/capture/index.js +3 -3
- package/dist/capture/progress.js +8 -1
- 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/catchup/gather.js +1 -0
- 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/commands/sync.js +5 -4
- package/dist/cli/commands/transfer.js +4 -3
- 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 +7 -2
- package/dist/facts/distill.js +6 -3
- package/dist/facts/handoff.js +6 -1
- package/dist/facts/known.js +3 -1
- package/dist/facts/model.d.ts +1 -1
- package/dist/facts/model.js +18 -7
- package/dist/facts/start.js +2 -2
- package/dist/graph/query/answer.js +1 -1
- package/dist/graph/query/path.js +69 -37
- package/dist/graph/query/refresh.js +5 -1
- package/dist/graph/query/suggest.js +1 -2
- 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 +9 -3
- 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/start-legacy.js +1 -1
- 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 +11 -7
- package/dist/store/each.d.ts +2 -4
- package/dist/store/each.js +1 -0
- package/dist/store/mongo/graph.d.ts +2 -1
- package/dist/store/mongo/graph.js +17 -2
- package/dist/store/mongo/index.d.ts +7 -8
- package/dist/store/mongo/index.js +13 -2
- package/dist/store/mongo/observations.d.ts +2 -1
- package/dist/store/mongo/observations.js +38 -4
- package/dist/store/postgres/driver.d.ts +10 -3
- package/dist/store/postgres/driver.js +12 -0
- package/dist/store/postgres/graph.d.ts +5 -6
- package/dist/store/postgres/graph.js +78 -57
- package/dist/store/postgres/index.d.ts +7 -8
- package/dist/store/postgres/index.js +16 -11
- package/dist/store/postgres/insert.js +98 -48
- package/dist/store/postgres/meta.d.ts +1 -0
- package/dist/store/postgres/meta.js +4 -0
- package/dist/store/postgres/observations.d.ts +3 -2
- package/dist/store/postgres/observations.js +40 -6
- package/dist/store/postgres/rows.js +1 -1
- package/dist/store/project-resolver.d.ts +3 -0
- package/dist/store/project-resolver.js +34 -23
- package/dist/store/scoped-store.js +5 -1
- package/dist/store/sqlite/graph.d.ts +5 -6
- package/dist/store/sqlite/graph.js +61 -35
- package/dist/store/sqlite/index.d.ts +7 -8
- package/dist/store/sqlite/index.js +11 -2
- package/dist/store/sqlite/observations.d.ts +2 -1
- package/dist/store/sqlite/observations.js +38 -7
- package/dist/types.d.ts +9 -0
- 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 -8
|
@@ -18,13 +18,13 @@ export async function cmdSync(argv) {
|
|
|
18
18
|
try {
|
|
19
19
|
await remote.init();
|
|
20
20
|
const localStatus = new Map();
|
|
21
|
-
await eachRemoteObservation(local.store, (batch) => {
|
|
21
|
+
await eachRemoteObservation(local.store, false, (batch) => {
|
|
22
22
|
for (const obs of batch)
|
|
23
23
|
localStatus.set(obs.id, obs.status ?? 'done');
|
|
24
24
|
});
|
|
25
25
|
const remoteStatus = new Map();
|
|
26
26
|
let pulled = 0;
|
|
27
|
-
await eachRemoteObservation(remote, async (batch) => {
|
|
27
|
+
await eachRemoteObservation(remote, true, async (batch) => {
|
|
28
28
|
for (const obs of batch)
|
|
29
29
|
remoteStatus.set(obs.id, obs.status ?? 'done');
|
|
30
30
|
const fresh = batch.filter((obs) => !localStatus.has(obs.id));
|
|
@@ -35,7 +35,7 @@ export async function cmdSync(argv) {
|
|
|
35
35
|
await local.store.insertObservations(fresh);
|
|
36
36
|
});
|
|
37
37
|
let pushed = 0;
|
|
38
|
-
await eachRemoteObservation(local.store, async (batch) => {
|
|
38
|
+
await eachRemoteObservation(local.store, true, async (batch) => {
|
|
39
39
|
const fresh = batch.filter((obs) => !remoteStatus.has(obs.id));
|
|
40
40
|
if (fresh.length === 0)
|
|
41
41
|
return;
|
|
@@ -139,7 +139,7 @@ async function reconcileSession(session, from, to) {
|
|
|
139
139
|
}
|
|
140
140
|
return false;
|
|
141
141
|
}
|
|
142
|
-
async function eachRemoteObservation(store, visit) {
|
|
142
|
+
async function eachRemoteObservation(store, vectors, visit) {
|
|
143
143
|
let after;
|
|
144
144
|
let afterId;
|
|
145
145
|
for (;;) {
|
|
@@ -147,6 +147,7 @@ async function eachRemoteObservation(store, visit) {
|
|
|
147
147
|
...(after === undefined ? {} : { after }),
|
|
148
148
|
...(afterId === undefined ? {} : { afterId }),
|
|
149
149
|
limit: BATCH,
|
|
150
|
+
vectors,
|
|
150
151
|
});
|
|
151
152
|
if (batch.length === 0)
|
|
152
153
|
return;
|
|
@@ -150,11 +150,11 @@ export async function cmdReembed(argv = []) {
|
|
|
150
150
|
migrated = await ctx.store.migrateVectorDims(embedder.dimensions);
|
|
151
151
|
}
|
|
152
152
|
let updated = 0;
|
|
153
|
-
let skipped = 0;
|
|
154
153
|
const filter = scoped ? { project } : {};
|
|
155
|
-
const scanned = await
|
|
154
|
+
const scanned = await ctx.store.count(filter);
|
|
155
|
+
const vectorStale = background ? {} : { embedder: embedder.id };
|
|
156
|
+
await eachObservation(ctx, { ...filter, vectorStale }, async (batch) => {
|
|
156
157
|
const stale = batch.filter((obs) => !obs.embedding?.length || (!background && obs.embedder !== embedder.id));
|
|
157
|
-
skipped += batch.length - stale.length;
|
|
158
158
|
if (stale.length === 0)
|
|
159
159
|
return;
|
|
160
160
|
await embedObservations(ctx, stale);
|
|
@@ -165,6 +165,7 @@ export async function cmdReembed(argv = []) {
|
|
|
165
165
|
});
|
|
166
166
|
if (!background)
|
|
167
167
|
process.stderr.write('\r');
|
|
168
|
+
const skipped = scanned - updated;
|
|
168
169
|
if (migrated) {
|
|
169
170
|
console.log(`Rebuilt vector storage at ${embedder.dimensions}d.`);
|
|
170
171
|
}
|
package/dist/cli/index.js
CHANGED
|
@@ -17,6 +17,7 @@ import { cmdLanguages } from './commands/languages.js';
|
|
|
17
17
|
import { cmdSeed } from './commands/seed.js';
|
|
18
18
|
import { cmdAdoption } from './commands/adoption.js';
|
|
19
19
|
import { cmdView } from './commands/view.js';
|
|
20
|
+
import { cmdDaemon } from './commands/daemon.js';
|
|
20
21
|
import { silenceSqliteWarning } from '../util/warnings.js';
|
|
21
22
|
import { packageVersion } from '../update.js';
|
|
22
23
|
silenceSqliteWarning();
|
|
@@ -117,6 +118,9 @@ async function run() {
|
|
|
117
118
|
case 'sync':
|
|
118
119
|
await cmdSync(args);
|
|
119
120
|
break;
|
|
121
|
+
case 'daemon':
|
|
122
|
+
await cmdDaemon(args);
|
|
123
|
+
break;
|
|
120
124
|
case '--version':
|
|
121
125
|
case '-v':
|
|
122
126
|
console.log(packageVersion());
|
package/dist/cli/usage.js
CHANGED
|
@@ -34,6 +34,7 @@ export function usage() {
|
|
|
34
34
|
export [--all] > out.jsonl Dump memory as JSONL, for backup or migration
|
|
35
35
|
import <file.jsonl> Load a dump back in (safe to repeat)
|
|
36
36
|
sync <url> [--yes] Two-way merge with another database
|
|
37
|
+
daemon [on|off|start|stop] Serve hooks from one warm process; no argument shows status
|
|
37
38
|
update Install a newer compatible release now
|
|
38
39
|
reembed [--project] Re-embed with the current model
|
|
39
40
|
prune --older-than <days> Delete old memory (dry run without --yes)
|
package/dist/config/schema.d.ts
CHANGED
|
@@ -17,6 +17,10 @@ export declare const ConfigSchema: z.ZodObject<{
|
|
|
17
17
|
notify: "notify";
|
|
18
18
|
off: "off";
|
|
19
19
|
}>>;
|
|
20
|
+
daemon: z.ZodPrefault<z.ZodObject<{
|
|
21
|
+
enabled: z.ZodDefault<z.ZodBoolean>;
|
|
22
|
+
idleMinutes: z.ZodDefault<z.ZodNumber>;
|
|
23
|
+
}, z.core.$strip>>;
|
|
20
24
|
project: z.ZodPrefault<z.ZodObject<{
|
|
21
25
|
remote: z.ZodDefault<z.ZodString>;
|
|
22
26
|
}, z.core.$strip>>;
|
package/dist/config/schema.js
CHANGED
|
@@ -10,6 +10,12 @@ export const ConfigSchema = z.object({
|
|
|
10
10
|
})
|
|
11
11
|
.prefault({}),
|
|
12
12
|
updates: z.enum(['auto', 'notify', 'off']).default('notify'),
|
|
13
|
+
daemon: z
|
|
14
|
+
.object({
|
|
15
|
+
enabled: z.boolean().default(false),
|
|
16
|
+
idleMinutes: z.number().int().positive().default(15),
|
|
17
|
+
})
|
|
18
|
+
.prefault({}),
|
|
13
19
|
project: z
|
|
14
20
|
.object({
|
|
15
21
|
remote: z
|
package/dist/context.d.ts
CHANGED
|
@@ -10,3 +10,4 @@ export interface RecallContext {
|
|
|
10
10
|
close(): Promise<void>;
|
|
11
11
|
}
|
|
12
12
|
export declare function createContext(overrides?: Partial<Config>): Promise<RecallContext>;
|
|
13
|
+
export declare function embedderWithDeadline(load: () => Promise<Embedder>, timeoutMs: number, now?: () => number): () => Promise<Embedder>;
|
package/dist/context.js
CHANGED
|
@@ -9,17 +9,32 @@ export async function createContext(overrides) {
|
|
|
9
9
|
const raw = await createStore(config.database);
|
|
10
10
|
await raw.init();
|
|
11
11
|
const store = withProjectScope(raw, createScopeResolver({ store: raw, remote: config.project.remote }));
|
|
12
|
-
|
|
13
|
-
const embedder = () => {
|
|
14
|
-
pending ??= withTimeout(createEmbedder(config.embeddings.provider).catch(() => new NoopEmbedder()), config.embeddings.timeoutMs);
|
|
15
|
-
return pending;
|
|
16
|
-
};
|
|
12
|
+
const embedder = embedderWithDeadline(() => createEmbedder(config.embeddings.provider), config.embeddings.timeoutMs);
|
|
17
13
|
const search = new SearchService(store, embedder, config.embeddings.maxScanCandidates);
|
|
18
14
|
return { config, store, embedder, search, close: () => store.close() };
|
|
19
15
|
}
|
|
16
|
+
export function embedderWithDeadline(load, timeoutMs, now = Date.now) {
|
|
17
|
+
let loading = null;
|
|
18
|
+
let ready = null;
|
|
19
|
+
let deadline = 0;
|
|
20
|
+
return () => {
|
|
21
|
+
if (ready)
|
|
22
|
+
return Promise.resolve(ready);
|
|
23
|
+
if (!loading) {
|
|
24
|
+
deadline = now() + timeoutMs;
|
|
25
|
+
loading = load().catch(() => new NoopEmbedder());
|
|
26
|
+
void loading.then((embedder) => {
|
|
27
|
+
ready = embedder;
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
if (timeoutMs <= 0)
|
|
31
|
+
return loading;
|
|
32
|
+
return withTimeout(loading, deadline - now());
|
|
33
|
+
};
|
|
34
|
+
}
|
|
20
35
|
async function withTimeout(pending, ms) {
|
|
21
36
|
if (ms <= 0)
|
|
22
|
-
return pending;
|
|
37
|
+
return Promise.race([pending, Promise.resolve(new NoopEmbedder())]);
|
|
23
38
|
let timer;
|
|
24
39
|
const limit = new Promise((settle) => {
|
|
25
40
|
timer = setTimeout(() => settle(new NoopEmbedder()), ms);
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { DaemonReply, DaemonRequest } from './paths.js';
|
|
2
|
+
export declare const STARTING_PREFIX = "daemon.starting.";
|
|
3
|
+
export type Asked = DaemonReply | {
|
|
4
|
+
kind: 'unreachable';
|
|
5
|
+
} | {
|
|
6
|
+
kind: 'timeout';
|
|
7
|
+
};
|
|
8
|
+
export declare function daemonWanted(env?: NodeJS.ProcessEnv): boolean;
|
|
9
|
+
export declare function forwardedEnv(env?: NodeJS.ProcessEnv): Record<string, string>;
|
|
10
|
+
export declare function workerEnv(env?: NodeJS.ProcessEnv): Record<string, string>;
|
|
11
|
+
export declare function askDaemon(request: DaemonRequest, timeoutMs: number): Promise<Asked>;
|
|
12
|
+
export declare function startDaemon(now?: number): boolean;
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { readFileSync, rmSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import { createConnection } from 'node:net';
|
|
3
|
+
import { homedir } from 'node:os';
|
|
4
|
+
import { join } from 'node:path';
|
|
5
|
+
import { CONFIG_DIR } from '../config/dir.js';
|
|
6
|
+
import { runDetached } from '../hooks/detached.js';
|
|
7
|
+
import { DAEMON_SCRIPT, socketPath } from './paths.js';
|
|
8
|
+
const FORWARDED = ['CLAUDE_DB_CAPTURE', 'CLAUDE_CODE_ENTRYPOINT', 'CLAUDE_DB_USAGES_HOOK'];
|
|
9
|
+
const OWN_SETUP = ['CLAUDE_DB_URL', 'CLAUDE_DB_GRAPH_CACHE'];
|
|
10
|
+
const STARTING_FOR_MS = 15_000;
|
|
11
|
+
export const STARTING_PREFIX = 'daemon.starting.';
|
|
12
|
+
export function daemonWanted(env = process.env) {
|
|
13
|
+
if (OWN_SETUP.some((name) => env[name]))
|
|
14
|
+
return false;
|
|
15
|
+
const forced = env['CLAUDE_DB_DAEMON'];
|
|
16
|
+
if (forced === 'on')
|
|
17
|
+
return true;
|
|
18
|
+
if (forced === 'off')
|
|
19
|
+
return false;
|
|
20
|
+
try {
|
|
21
|
+
const config = JSON.parse(readFileSync(join(CONFIG_DIR, 'config.json'), 'utf8'));
|
|
22
|
+
return config.daemon?.enabled === true;
|
|
23
|
+
}
|
|
24
|
+
catch {
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
export function forwardedEnv(env = process.env) {
|
|
29
|
+
const picked = {};
|
|
30
|
+
for (const name of FORWARDED) {
|
|
31
|
+
const value = env[name];
|
|
32
|
+
if (value !== undefined)
|
|
33
|
+
picked[name] = value;
|
|
34
|
+
}
|
|
35
|
+
return picked;
|
|
36
|
+
}
|
|
37
|
+
export function workerEnv(env = process.env) {
|
|
38
|
+
const picked = {};
|
|
39
|
+
for (const [name, value] of Object.entries(env)) {
|
|
40
|
+
if (value !== undefined)
|
|
41
|
+
picked[name] = value;
|
|
42
|
+
}
|
|
43
|
+
return picked;
|
|
44
|
+
}
|
|
45
|
+
export function askDaemon(request, timeoutMs) {
|
|
46
|
+
return new Promise((settle) => {
|
|
47
|
+
let sent = false;
|
|
48
|
+
let reply = '';
|
|
49
|
+
const socket = createConnection(socketPath());
|
|
50
|
+
const finish = (asked) => {
|
|
51
|
+
clearTimeout(timer);
|
|
52
|
+
socket.destroy();
|
|
53
|
+
settle(asked);
|
|
54
|
+
};
|
|
55
|
+
const timer = setTimeout(() => finish(sent ? { kind: 'timeout' } : { kind: 'unreachable' }), timeoutMs);
|
|
56
|
+
socket.setEncoding('utf8');
|
|
57
|
+
socket.on('connect', () => {
|
|
58
|
+
sent = true;
|
|
59
|
+
socket.write(`${JSON.stringify(request)}\n`);
|
|
60
|
+
});
|
|
61
|
+
socket.on('data', (chunk) => {
|
|
62
|
+
reply += chunk;
|
|
63
|
+
const end = reply.indexOf('\n');
|
|
64
|
+
if (end < 0)
|
|
65
|
+
return;
|
|
66
|
+
try {
|
|
67
|
+
finish(JSON.parse(reply.slice(0, end)));
|
|
68
|
+
}
|
|
69
|
+
catch {
|
|
70
|
+
finish({ kind: 'timeout' });
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
socket.on('error', () => finish(sent ? { kind: 'timeout' } : { kind: 'unreachable' }));
|
|
74
|
+
socket.on('close', () => finish(sent ? { kind: 'timeout' } : { kind: 'unreachable' }));
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
export function startDaemon(now = Date.now()) {
|
|
78
|
+
const window = Math.floor(now / STARTING_FOR_MS);
|
|
79
|
+
const marker = join(CONFIG_DIR, `${STARTING_PREFIX}${window}`);
|
|
80
|
+
try {
|
|
81
|
+
writeFileSync(marker, String(now), { flag: 'wx' });
|
|
82
|
+
}
|
|
83
|
+
catch {
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
runDetached(DAEMON_SCRIPT, [], homedir(), () => rmSync(marker, { force: true }));
|
|
87
|
+
return true;
|
|
88
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export declare const DAEMON_SCRIPT: string;
|
|
2
|
+
export declare function socketPath(): string;
|
|
3
|
+
export declare function buildId(): string;
|
|
4
|
+
export interface DaemonRequest {
|
|
5
|
+
build: string;
|
|
6
|
+
hook: string;
|
|
7
|
+
raw: string;
|
|
8
|
+
env: Record<string, string>;
|
|
9
|
+
workerEnv?: Record<string, string>;
|
|
10
|
+
}
|
|
11
|
+
export type DaemonReply = {
|
|
12
|
+
kind: 'done';
|
|
13
|
+
out: string;
|
|
14
|
+
} | {
|
|
15
|
+
kind: 'stale';
|
|
16
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { statSync } from 'node:fs';
|
|
2
|
+
import { dirname, join, resolve } from 'node:path';
|
|
3
|
+
import { fileURLToPath } from 'node:url';
|
|
4
|
+
import { CONFIG_DIR } from '../config/dir.js';
|
|
5
|
+
import { scopeToken } from '../util/scope.js';
|
|
6
|
+
export const DAEMON_SCRIPT = resolve(dirname(fileURLToPath(import.meta.url)), 'server.js');
|
|
7
|
+
export function socketPath() {
|
|
8
|
+
if (process.platform === 'win32')
|
|
9
|
+
return `\\\\.\\pipe\\claude-db-${scopeToken(CONFIG_DIR)}`;
|
|
10
|
+
return join(CONFIG_DIR, 'daemon.sock');
|
|
11
|
+
}
|
|
12
|
+
export function buildId() {
|
|
13
|
+
try {
|
|
14
|
+
return `${DAEMON_SCRIPT}:${statSync(DAEMON_SCRIPT).mtimeMs}`;
|
|
15
|
+
}
|
|
16
|
+
catch {
|
|
17
|
+
return DAEMON_SCRIPT;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,225 @@
|
|
|
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 { withWorkerEnv } from '../hooks/detached.js';
|
|
10
|
+
import { silenceSqliteWarning } from '../util/warnings.js';
|
|
11
|
+
import { buildId, socketPath } from './paths.js';
|
|
12
|
+
import { STARTING_PREFIX } from './client.js';
|
|
13
|
+
silenceSqliteWarning();
|
|
14
|
+
const BUILD = buildId();
|
|
15
|
+
const SOCKET = socketPath();
|
|
16
|
+
const MAX_REQUEST_BYTES = 8 * 1024 * 1024;
|
|
17
|
+
const RETIRE_AFTER_MS = 30_000;
|
|
18
|
+
let current = null;
|
|
19
|
+
let running = 0;
|
|
20
|
+
const pending = new Set();
|
|
21
|
+
let idleTimer;
|
|
22
|
+
let server;
|
|
23
|
+
let closing = false;
|
|
24
|
+
const log = (message) => {
|
|
25
|
+
process.stderr.write(`${new Date().toISOString()} daemon ${message}\n`);
|
|
26
|
+
};
|
|
27
|
+
function configStamp() {
|
|
28
|
+
try {
|
|
29
|
+
return statSync(CONFIG_PATH).mtimeMs;
|
|
30
|
+
}
|
|
31
|
+
catch {
|
|
32
|
+
return 0;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
function retire(ctx) {
|
|
36
|
+
setTimeout(() => {
|
|
37
|
+
ctx.then((old) => old.close()).catch(() => undefined);
|
|
38
|
+
}, RETIRE_AFTER_MS).unref();
|
|
39
|
+
}
|
|
40
|
+
function context() {
|
|
41
|
+
const stamp = configStamp();
|
|
42
|
+
if (current && current.stamp === stamp)
|
|
43
|
+
return current.ctx;
|
|
44
|
+
if (current)
|
|
45
|
+
retire(current.ctx);
|
|
46
|
+
const ctx = createContext();
|
|
47
|
+
current = { stamp, ctx };
|
|
48
|
+
ctx.catch(() => {
|
|
49
|
+
if (current?.ctx === ctx)
|
|
50
|
+
current = null;
|
|
51
|
+
});
|
|
52
|
+
return ctx;
|
|
53
|
+
}
|
|
54
|
+
function idleMs() {
|
|
55
|
+
try {
|
|
56
|
+
return loadConfig().daemon.idleMinutes * 60_000;
|
|
57
|
+
}
|
|
58
|
+
catch {
|
|
59
|
+
return 15 * 60_000;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
function touch() {
|
|
63
|
+
clearTimeout(idleTimer);
|
|
64
|
+
idleTimer = setTimeout(() => {
|
|
65
|
+
if (running === 0 && pending.size === 0)
|
|
66
|
+
void shutdown('idle');
|
|
67
|
+
else
|
|
68
|
+
touch();
|
|
69
|
+
}, idleMs());
|
|
70
|
+
}
|
|
71
|
+
function track(task) {
|
|
72
|
+
const done = task()
|
|
73
|
+
.catch((error) => log(`background task failed: ${String(error)}`))
|
|
74
|
+
.finally(() => {
|
|
75
|
+
pending.delete(done);
|
|
76
|
+
touch();
|
|
77
|
+
});
|
|
78
|
+
pending.add(done);
|
|
79
|
+
return true;
|
|
80
|
+
}
|
|
81
|
+
async function answer(request) {
|
|
82
|
+
if (request.build !== BUILD) {
|
|
83
|
+
setImmediate(() => void shutdown('replaced by a newer build'));
|
|
84
|
+
return { kind: 'stale' };
|
|
85
|
+
}
|
|
86
|
+
if (request.hook === '__stop') {
|
|
87
|
+
setImmediate(() => void shutdown('asked to stop'));
|
|
88
|
+
return { kind: 'done', out: '' };
|
|
89
|
+
}
|
|
90
|
+
if (!isHookName(request.hook))
|
|
91
|
+
return { kind: 'done', out: '' };
|
|
92
|
+
const out = [];
|
|
93
|
+
const run = {
|
|
94
|
+
payload: parsePayload(request.raw),
|
|
95
|
+
env: request.env,
|
|
96
|
+
write: (text) => out.push(text),
|
|
97
|
+
withContext: async (work) => work(await context()),
|
|
98
|
+
inDaemon: track,
|
|
99
|
+
};
|
|
100
|
+
try {
|
|
101
|
+
const handle = await loadHandler(request.hook);
|
|
102
|
+
await withWorkerEnv(request.workerEnv, () => handle(run));
|
|
103
|
+
}
|
|
104
|
+
catch (error) {
|
|
105
|
+
log(`${request.hook} failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
106
|
+
}
|
|
107
|
+
return { kind: 'done', out: out.join('') };
|
|
108
|
+
}
|
|
109
|
+
const isStringMap = (value) => typeof value === 'object' &&
|
|
110
|
+
value !== null &&
|
|
111
|
+
Object.values(value).every((entry) => typeof entry === 'string');
|
|
112
|
+
function parseRequest(line) {
|
|
113
|
+
let parsed;
|
|
114
|
+
try {
|
|
115
|
+
parsed = JSON.parse(line);
|
|
116
|
+
}
|
|
117
|
+
catch {
|
|
118
|
+
return null;
|
|
119
|
+
}
|
|
120
|
+
if (typeof parsed !== 'object' || parsed === null)
|
|
121
|
+
return null;
|
|
122
|
+
const { build, hook, raw, env, workerEnv } = parsed;
|
|
123
|
+
if (typeof build !== 'string' || typeof hook !== 'string' || typeof raw !== 'string')
|
|
124
|
+
return null;
|
|
125
|
+
if (!isStringMap(env))
|
|
126
|
+
return null;
|
|
127
|
+
if (workerEnv !== undefined && !isStringMap(workerEnv))
|
|
128
|
+
return null;
|
|
129
|
+
return { build, hook, raw, env, ...(workerEnv ? { workerEnv } : {}) };
|
|
130
|
+
}
|
|
131
|
+
function serve(socket) {
|
|
132
|
+
let buffer = '';
|
|
133
|
+
socket.setEncoding('utf8');
|
|
134
|
+
socket.on('error', () => socket.destroy());
|
|
135
|
+
socket.on('data', (chunk) => {
|
|
136
|
+
buffer += chunk;
|
|
137
|
+
if (buffer.length > MAX_REQUEST_BYTES) {
|
|
138
|
+
socket.destroy();
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
const end = buffer.indexOf('\n');
|
|
142
|
+
if (end < 0)
|
|
143
|
+
return;
|
|
144
|
+
const line = buffer.slice(0, end);
|
|
145
|
+
buffer = '';
|
|
146
|
+
socket.pause();
|
|
147
|
+
running += 1;
|
|
148
|
+
touch();
|
|
149
|
+
const request = parseRequest(line);
|
|
150
|
+
if (!request) {
|
|
151
|
+
running -= 1;
|
|
152
|
+
socket.destroy();
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
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
|
+
if (current)
|
|
176
|
+
await current.ctx.then((ctx) => ctx.close()).catch(() => undefined);
|
|
177
|
+
process.exit(0);
|
|
178
|
+
}
|
|
179
|
+
function clearStartMarkers() {
|
|
180
|
+
try {
|
|
181
|
+
for (const name of readdirSync(CONFIG_DIR)) {
|
|
182
|
+
if (name.startsWith(STARTING_PREFIX))
|
|
183
|
+
rmSync(join(CONFIG_DIR, name), { force: true });
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
catch {
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
function inUse() {
|
|
191
|
+
return new Promise((settle) => {
|
|
192
|
+
const probe = createConnection(SOCKET);
|
|
193
|
+
probe.on('connect', () => {
|
|
194
|
+
probe.destroy();
|
|
195
|
+
settle(true);
|
|
196
|
+
});
|
|
197
|
+
probe.on('error', () => settle(false));
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
async function start() {
|
|
201
|
+
if (await inUse()) {
|
|
202
|
+
log('another daemon is already running');
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
if (process.platform !== 'win32')
|
|
206
|
+
rmSync(SOCKET, { force: true });
|
|
207
|
+
server = createServer(serve);
|
|
208
|
+
server.on('error', (error) => {
|
|
209
|
+
log(`could not listen: ${error.message}`);
|
|
210
|
+
process.exit(0);
|
|
211
|
+
});
|
|
212
|
+
server.listen(SOCKET, () => {
|
|
213
|
+
if (process.platform !== 'win32')
|
|
214
|
+
chmodSync(SOCKET, 0o600);
|
|
215
|
+
clearStartMarkers();
|
|
216
|
+
log(`listening on ${SOCKET}`);
|
|
217
|
+
touch();
|
|
218
|
+
void context()
|
|
219
|
+
.then((ctx) => ctx.embedder())
|
|
220
|
+
.catch((error) => log(`could not open the store: ${String(error)}`));
|
|
221
|
+
});
|
|
222
|
+
process.on('SIGTERM', () => void shutdown('SIGTERM'));
|
|
223
|
+
process.on('SIGINT', () => void shutdown('SIGINT'));
|
|
224
|
+
}
|
|
225
|
+
await start();
|
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
|
}
|
|
@@ -87,8 +87,13 @@ 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()]) {
|
|
91
|
-
const batch = await ctx.store.list({
|
|
90
|
+
for (const owner of [project, youScope(project)]) {
|
|
91
|
+
const batch = await ctx.store.list({
|
|
92
|
+
project: owner,
|
|
93
|
+
sessionId: FACT_SESSION,
|
|
94
|
+
tag: CLAUDE_MEMORY_TAG,
|
|
95
|
+
limit: 5000,
|
|
96
|
+
});
|
|
92
97
|
rows.push(...batch.filter((obs) => obs.tags.includes(CLAUDE_MEMORY_TAG) && mine.every((tag) => obs.tags.includes(tag))));
|
|
93
98
|
}
|
|
94
99
|
return rows;
|
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
|
}
|
|
@@ -128,6 +128,9 @@ function clip(text, max) {
|
|
|
128
128
|
return text.length <= max ? text : `${text.slice(0, max)}…`;
|
|
129
129
|
}
|
|
130
130
|
export async function factCounts(ctx, project) {
|
|
131
|
-
const count = async (owner) =>
|
|
132
|
-
|
|
131
|
+
const count = async (owner) => {
|
|
132
|
+
const scope = { project: owner, sessionId: FACT_SESSION };
|
|
133
|
+
return ((await ctx.store.count(scope)) - (await ctx.store.count({ ...scope, status: 'replaced' })));
|
|
134
|
+
};
|
|
135
|
+
return { project: await count(project), you: await count(youScope(project)) };
|
|
133
136
|
}
|
package/dist/facts/handoff.js
CHANGED
|
@@ -19,11 +19,16 @@ export async function newestHandoff(ctx, project, now = Date.now()) {
|
|
|
19
19
|
sessionId: MANUAL_SESSION,
|
|
20
20
|
newest: true,
|
|
21
21
|
limit: MANUAL_NOTES_SCANNED,
|
|
22
|
+
bodies: false,
|
|
22
23
|
});
|
|
23
24
|
const current = (obs) => obs.status !== 'replaced' &&
|
|
24
25
|
obs.tags.includes(HANDOFF_TAG) &&
|
|
25
26
|
now - obs.createdAt <= HANDOFF_MAX_AGE_MS;
|
|
26
|
-
|
|
27
|
+
const found = notes.find(current);
|
|
28
|
+
if (!found)
|
|
29
|
+
return null;
|
|
30
|
+
const [full] = await ctx.store.getObservations([found.id]);
|
|
31
|
+
return full ?? null;
|
|
27
32
|
}
|
|
28
33
|
function clipLine(line) {
|
|
29
34
|
return line.length > HANDOFF_LINE_CHARS
|
package/dist/facts/known.js
CHANGED
|
@@ -5,7 +5,7 @@ const EXISTING_LIMIT = 80;
|
|
|
5
5
|
export async function existingFacts(ctx, project) {
|
|
6
6
|
const owners = [
|
|
7
7
|
[project, 'project'],
|
|
8
|
-
[youScope(), 'you'],
|
|
8
|
+
[youScope(project), 'you'],
|
|
9
9
|
];
|
|
10
10
|
const facts = [];
|
|
11
11
|
for (const [owner, scope] of owners) {
|
|
@@ -14,6 +14,7 @@ export async function existingFacts(ctx, project) {
|
|
|
14
14
|
sessionId: FACT_SESSION,
|
|
15
15
|
newest: true,
|
|
16
16
|
limit: EXISTING_LIMIT,
|
|
17
|
+
bodies: false,
|
|
17
18
|
});
|
|
18
19
|
for (const obs of onePerKey(rows.filter((row) => row.status !== 'replaced'))) {
|
|
19
20
|
const key = factKey(obs);
|
|
@@ -31,6 +32,7 @@ export async function notedByUser(ctx, project) {
|
|
|
31
32
|
sessionId: MANUAL_SESSION,
|
|
32
33
|
newest: true,
|
|
33
34
|
limit: EXISTING_LIMIT,
|
|
35
|
+
bodies: false,
|
|
34
36
|
});
|
|
35
37
|
return rows
|
|
36
38
|
.filter((row) => row.status !== 'replaced' && !row.tags.includes(HANDOFF_TAG))
|
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): 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;
|