claude-db 0.9.1 → 0.10.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (161) hide show
  1. package/CHANGELOG.md +248 -0
  2. package/README.md +74 -113
  3. package/dist/capture/flush.d.ts +1 -0
  4. package/dist/capture/flush.js +4 -3
  5. package/dist/capture/git.js +2 -1
  6. package/dist/capture/index.d.ts +5 -1
  7. package/dist/capture/index.js +4 -1
  8. package/dist/capture/manual.js +1 -1
  9. package/dist/capture/progress.js +1 -0
  10. package/dist/capture/redact.d.ts +1 -0
  11. package/dist/capture/redact.js +17 -0
  12. package/dist/capture/reingest.d.ts +19 -0
  13. package/dist/capture/reingest.js +56 -0
  14. package/dist/capture/scrub.d.ts +11 -0
  15. package/dist/capture/scrub.js +48 -0
  16. package/dist/capture/summarize.js +3 -9
  17. package/dist/capture/transcript.d.ts +1 -0
  18. package/dist/capture/transcript.js +12 -3
  19. package/dist/capture/turn-extractor.d.ts +0 -1
  20. package/dist/capture/turn-extractor.js +10 -19
  21. package/dist/cli/commands/adoption.js +3 -3
  22. package/dist/cli/commands/distill.d.ts +1 -0
  23. package/dist/cli/commands/distill.js +67 -0
  24. package/dist/cli/commands/doctor.js +11 -3
  25. package/dist/cli/commands/insight.js +13 -4
  26. package/dist/cli/commands/maintain.d.ts +2 -1
  27. package/dist/cli/commands/maintain.js +53 -17
  28. package/dist/cli/commands/pick.d.ts +3 -0
  29. package/dist/cli/commands/pick.js +25 -0
  30. package/dist/cli/commands/setup.js +11 -2
  31. package/dist/cli/commands/sync.js +73 -12
  32. package/dist/cli/commands/transfer.d.ts +0 -5
  33. package/dist/cli/commands/transfer.js +1 -24
  34. package/dist/cli/commands/use.js +1 -1
  35. package/dist/cli/commands/view.js +30 -28
  36. package/dist/cli/index.js +13 -2
  37. package/dist/cli/install.d.ts +2 -0
  38. package/dist/cli/install.js +44 -21
  39. package/dist/cli/instructions.js +4 -2
  40. package/dist/cli/refresh.js +5 -1
  41. package/dist/cli/usage.js +4 -0
  42. package/dist/config/dir.d.ts +1 -0
  43. package/dist/config/dir.js +3 -0
  44. package/dist/config/index.d.ts +1 -1
  45. package/dist/config/index.js +1 -1
  46. package/dist/config/load.d.ts +4 -1
  47. package/dist/config/load.js +30 -13
  48. package/dist/config/schema.d.ts +19 -0
  49. package/dist/config/schema.js +17 -2
  50. package/dist/embed/index.d.ts +1 -1
  51. package/dist/embed/index.js +1 -1
  52. package/dist/embed/local.js +2 -1
  53. package/dist/facts/budget.d.ts +4 -0
  54. package/dist/facts/budget.js +12 -0
  55. package/dist/facts/claude-memory.d.ts +19 -0
  56. package/dist/facts/claude-memory.js +100 -0
  57. package/dist/facts/distill.d.ts +25 -0
  58. package/dist/facts/distill.js +141 -0
  59. package/dist/facts/model.d.ts +28 -0
  60. package/dist/facts/model.js +82 -0
  61. package/dist/facts/notice.d.ts +2 -0
  62. package/dist/facts/notice.js +22 -0
  63. package/dist/facts/ops.d.ts +23 -0
  64. package/dist/facts/ops.js +91 -0
  65. package/dist/facts/render.d.ts +4 -0
  66. package/dist/facts/render.js +13 -0
  67. package/dist/facts/start.d.ts +6 -0
  68. package/dist/facts/start.js +57 -0
  69. package/dist/graph/index.d.ts +2 -6
  70. package/dist/graph/index.js +2 -3
  71. package/dist/graph/languages/basic.js +1 -1
  72. package/dist/graph/languages/index.d.ts +1 -1
  73. package/dist/graph/parser.js +1 -1
  74. package/dist/graph/query/index.d.ts +0 -2
  75. package/dist/graph/query/index.js +0 -1
  76. package/dist/graph/scan/extract.js +1 -1
  77. package/dist/graph/scan/files.js +1 -1
  78. package/dist/graph/scan/index.d.ts +1 -2
  79. package/dist/graph/scan/index.js +1 -1
  80. package/dist/hooks/background.d.ts +3 -0
  81. package/dist/hooks/background.js +43 -0
  82. package/dist/hooks/compact.d.ts +8 -0
  83. package/dist/hooks/compact.js +42 -0
  84. package/dist/hooks/payload.d.ts +3 -0
  85. package/dist/hooks/payload.js +18 -1
  86. package/dist/hooks/pick-deliver.d.ts +2 -0
  87. package/dist/hooks/pick-deliver.js +16 -0
  88. package/dist/hooks/prompt-recall.d.ts +21 -0
  89. package/dist/hooks/prompt-recall.js +36 -0
  90. package/dist/hooks/relevance.d.ts +11 -3
  91. package/dist/hooks/relevance.js +56 -26
  92. package/dist/hooks/session-end.js +7 -1
  93. package/dist/hooks/session-start.js +31 -59
  94. package/dist/hooks/shown.d.ts +1 -0
  95. package/dist/hooks/shown.js +5 -2
  96. package/dist/hooks/start-legacy.d.ts +6 -0
  97. package/dist/hooks/start-legacy.js +47 -0
  98. package/dist/hooks/user-prompt.js +29 -25
  99. package/dist/mcp/server.js +13 -1
  100. package/dist/pick/pending.d.ts +17 -0
  101. package/dist/pick/pending.js +106 -0
  102. package/dist/pick/prompt.d.ts +14 -0
  103. package/dist/pick/prompt.js +80 -0
  104. package/dist/pick/run.d.ts +22 -0
  105. package/dist/pick/run.js +36 -0
  106. package/dist/pick/worker.d.ts +2 -0
  107. package/dist/pick/worker.js +32 -0
  108. package/dist/store/adapter.d.ts +1 -0
  109. package/dist/store/each.d.ts +5 -0
  110. package/dist/store/each.js +25 -0
  111. package/dist/store/mongo/docs.d.ts +2 -0
  112. package/dist/store/mongo/docs.js +4 -0
  113. package/dist/store/mongo/driver.js +3 -2
  114. package/dist/store/mongo/filters.d.ts +1 -0
  115. package/dist/store/mongo/filters.js +8 -0
  116. package/dist/store/mongo/index.d.ts +1 -0
  117. package/dist/store/mongo/index.js +5 -2
  118. package/dist/store/mongo/observations.d.ts +2 -1
  119. package/dist/store/mongo/observations.js +11 -1
  120. package/dist/store/mongo/search.js +12 -3
  121. package/dist/store/mongo/sessions.js +7 -1
  122. package/dist/store/mongo/vectorSearch.js +9 -11
  123. package/dist/store/postgres/driver.js +3 -2
  124. package/dist/store/postgres/filters.js +5 -0
  125. package/dist/store/postgres/index.d.ts +1 -0
  126. package/dist/store/postgres/index.js +3 -0
  127. package/dist/store/postgres/meta.js +2 -1
  128. package/dist/store/postgres/observations.d.ts +1 -0
  129. package/dist/store/postgres/observations.js +14 -3
  130. package/dist/store/postgres/rows.js +4 -0
  131. package/dist/store/postgres/schema.d.ts +2 -2
  132. package/dist/store/postgres/schema.js +4 -1
  133. package/dist/store/postgres/sessions.js +14 -7
  134. package/dist/store/session-time.d.ts +2 -0
  135. package/dist/store/session-time.js +5 -0
  136. package/dist/store/sqlite/filters.js +5 -0
  137. package/dist/store/sqlite/index.d.ts +1 -0
  138. package/dist/store/sqlite/index.js +27 -5
  139. package/dist/store/sqlite/observations.d.ts +1 -0
  140. package/dist/store/sqlite/observations.js +14 -1
  141. package/dist/store/sqlite/rows.js +4 -0
  142. package/dist/store/sqlite/schema.sql +3 -1
  143. package/dist/store/sqlite/search.js +7 -3
  144. package/dist/store/sqlite/sessions.js +13 -8
  145. package/dist/types.d.ts +5 -1
  146. package/dist/usages/find.js +5 -3
  147. package/dist/usages/index.d.ts +0 -1
  148. package/dist/util/claude-cli.d.ts +6 -0
  149. package/dist/util/claude-cli.js +38 -0
  150. package/dist/util/daily-budget.d.ts +7 -0
  151. package/dist/util/daily-budget.js +38 -0
  152. package/dist/util/day.d.ts +1 -0
  153. package/dist/util/day.js +11 -0
  154. package/dist/util/job-lock.d.ts +4 -0
  155. package/dist/util/job-lock.js +56 -0
  156. package/dist/util/prompt.d.ts +5 -0
  157. package/dist/util/prompt.js +31 -0
  158. package/docs/how-it-works.md +69 -3
  159. package/docs/setup-guide.md +339 -0
  160. package/docs/with-and-without.md +43 -36
  161. package/package.json +23 -7
@@ -13,7 +13,7 @@ export function loadParser() {
13
13
  throw new Error('ast-grep failed to load, so the code graph cannot be built. It ships ' +
14
14
  'as a prebuilt binary per platform, so this usually means this one is ' +
15
15
  `unsupported: ${process.platform}/${process.arch}. ` +
16
- `(${error instanceof Error ? error.message.split('\n')[0] : String(error)})`);
16
+ `(${error instanceof Error ? error.message.split('\n')[0] : String(error)})`, { cause: error });
17
17
  }
18
18
  const dynamic = {};
19
19
  for (const name of DYNAMIC_LANGUAGES) {
@@ -1,6 +1,4 @@
1
1
  export { formatGraph } from './format.js';
2
2
  export { queryGraph } from './lookup.js';
3
- export type { GraphAnswer, GraphMode, GraphQuery } from './lookup.js';
4
- export { shortestPath } from './path.js';
5
3
  export { refreshGraph } from './refresh.js';
6
4
  export { nearest, suggestFor } from './suggest.js';
@@ -1,5 +1,4 @@
1
1
  export { formatGraph } from './format.js';
2
2
  export { queryGraph } from './lookup.js';
3
- export { shortestPath } from './path.js';
4
3
  export { refreshGraph } from './refresh.js';
5
4
  export { nearest, suggestFor } from './suggest.js';
@@ -1,5 +1,5 @@
1
1
  import { observationId } from '../../capture/identity.js';
2
- import { redact } from '../../capture/turn-extractor.js';
2
+ import { redact } from '../../capture/redact.js';
3
3
  import { callsIn, declarationsIn } from '../languages/index.js';
4
4
  import { languageHandle, loadParser } from '../parser.js';
5
5
  const CALLABLE = new Set(['function', 'method', 'class']);
@@ -7,7 +7,7 @@ const MAX_BUFFER = 64 * 1024 * 1024;
7
7
  const MAX_FILE_BYTES = 1024 * 1024;
8
8
  export const SCAN_VERSION = 5;
9
9
  const RULES_FINGERPRINT = createHash('sha256')
10
- .update(JSON.stringify(LANGUAGES, (_key, value) => (value instanceof RegExp ? String(value) : value)))
10
+ .update(JSON.stringify(LANGUAGES, (_key, value) => value instanceof RegExp ? String(value) : value))
11
11
  .update(BASIC_FINGERPRINT)
12
12
  .digest('hex')
13
13
  .slice(0, 12);
@@ -1,6 +1,5 @@
1
1
  import type { CodeEdge, CodeSymbol, ScannedFile } from '../../types.js';
2
- export type { SourceFile } from './files.js';
3
- export { SCAN_VERSION, currentHashes, hashOf, listFiles, sourceFiles } from './files.js';
2
+ export { SCAN_VERSION, currentHashes, hashOf } from './files.js';
4
3
  export interface ScanOptions {
5
4
  root: string;
6
5
  project: string;
@@ -2,7 +2,7 @@ import { loadParser } from '../parser.js';
2
2
  import { extractFile } from './extract.js';
3
3
  import { listFiles, readSource } from './files.js';
4
4
  import { resolveEdges } from './resolve.js';
5
- export { SCAN_VERSION, currentHashes, hashOf, listFiles, sourceFiles } from './files.js';
5
+ export { SCAN_VERSION, currentHashes, hashOf } from './files.js';
6
6
  export function scanRepository(options) {
7
7
  const { root, project, known, force } = options;
8
8
  loadParser();
@@ -0,0 +1,3 @@
1
+ export declare function startBackgroundWork(project: string, database: string, now?: number): void;
2
+ export declare function distillInBackground(project: string, sessionId: string): void;
3
+ export declare function pickInBackground(project: string, sessionId: string, token: string): void;
@@ -0,0 +1,43 @@
1
+ import { spawn } from 'node:child_process';
2
+ import { dirname, resolve } from 'node:path';
3
+ import { fileURLToPath } from 'node:url';
4
+ import { claimReingest, claimScrub, reingestDone, releaseReingest, releaseScrub, } from '../capture/index.js';
5
+ import { FACTS_JOB } from '../facts/distill.js';
6
+ import { claimJob, jobMark, releaseJob } from '../util/job-lock.js';
7
+ const DIST = resolve(dirname(fileURLToPath(import.meta.url)), '..');
8
+ const CLI = resolve(DIST, 'cli', 'index.js');
9
+ const PICK_WORKER = resolve(DIST, 'pick', 'worker.js');
10
+ const FACTS_EVERY_MS = 60 * 60 * 1000;
11
+ function runDetached(script, args, cwd, release) {
12
+ try {
13
+ spawn(process.execPath, [script, ...args], { cwd, detached: true, stdio: 'ignore' })
14
+ .on('error', release)
15
+ .unref();
16
+ }
17
+ catch {
18
+ release();
19
+ }
20
+ }
21
+ export function startBackgroundWork(project, database, now = Date.now()) {
22
+ if (claimScrub(database, now)) {
23
+ runDetached(CLI, ['redact', '--background'], project, () => releaseScrub(database));
24
+ return;
25
+ }
26
+ if (claimReingest(project, now)) {
27
+ runDetached(CLI, ['flush', '--repair'], project, () => releaseReingest(project));
28
+ return;
29
+ }
30
+ if (!reingestDone(project))
31
+ return;
32
+ if (now - jobMark(FACTS_JOB, project) < FACTS_EVERY_MS)
33
+ return;
34
+ if (!claimJob(FACTS_JOB, project, now))
35
+ return;
36
+ runDetached(CLI, ['distill', '--backfill'], project, () => releaseJob(FACTS_JOB, project));
37
+ }
38
+ export function distillInBackground(project, sessionId) {
39
+ runDetached(CLI, ['distill', '--session', sessionId], project, () => { });
40
+ }
41
+ export function pickInBackground(project, sessionId, token) {
42
+ runDetached(PICK_WORKER, [sessionId, token], project, () => { });
43
+ }
@@ -0,0 +1,8 @@
1
+ import type { RecallContext } from '../context.js';
2
+ import type { Observation } from '../types.js';
3
+ export interface EarlierInChat {
4
+ block: string;
5
+ ids: ReadonlySet<string>;
6
+ }
7
+ export declare function recoverAfterCompact(ctx: RecallContext, project: string, sessionId: string, transcriptPath: string | undefined): Promise<EarlierInChat | null>;
8
+ export declare function renderEarlierInChat(newestFirst: Observation[]): EarlierInChat | null;
@@ -0,0 +1,42 @@
1
+ import { flushSession } from '../capture/index.js';
2
+ import { toShortId } from '../util/shortid.js';
3
+ import { capturingDisabled } from './payload.js';
4
+ import { forgetShown } from './shown.js';
5
+ const MAX_LINES = 8;
6
+ const SCANNED = 200;
7
+ const LABELS = {
8
+ decision: 'Decided',
9
+ deadend: 'Dead end',
10
+ preference: 'Rule',
11
+ };
12
+ export async function recoverAfterCompact(ctx, project, sessionId, transcriptPath) {
13
+ forgetShown(sessionId);
14
+ if (!capturingDisabled(ctx.config.capture.scripted)) {
15
+ await flushSession(ctx, sessionId, project, transcriptPath);
16
+ }
17
+ const observations = await ctx.store.list({ project, sessionId, newest: true, limit: SCANNED });
18
+ return renderEarlierInChat(observations);
19
+ }
20
+ export function renderEarlierInChat(newestFirst) {
21
+ const lines = [];
22
+ const ids = new Set();
23
+ const titles = new Set();
24
+ for (const obs of newestFirst) {
25
+ const label = LABELS[obs.kind] ?? (obs.status === 'open' ? 'Not committed' : null);
26
+ if (!label || obs.status === 'replaced')
27
+ continue;
28
+ if (titles.has(obs.title)) {
29
+ ids.add(obs.id);
30
+ continue;
31
+ }
32
+ titles.add(obs.title);
33
+ ids.add(obs.id);
34
+ lines.push(`- ${label}: ${obs.title} (${toShortId(obs.id)})`);
35
+ if (lines.length === MAX_LINES)
36
+ break;
37
+ }
38
+ if (lines.length === 0)
39
+ return null;
40
+ const block = ['<memory>', 'Earlier in this chat:', ...lines.reverse(), '</memory>'].join('\n');
41
+ return { block, ids };
42
+ }
@@ -3,6 +3,7 @@ export interface HookPayload {
3
3
  cwd?: string;
4
4
  transcript_path?: string;
5
5
  hook_event_name?: string;
6
+ source?: string;
6
7
  tool_name?: string;
7
8
  tool_input?: Record<string, unknown>;
8
9
  tool_response?: unknown;
@@ -12,3 +13,5 @@ export declare function capturingDisabled(scripted: boolean): boolean;
12
13
  export declare function readPayload(): Promise<HookPayload>;
13
14
  export declare function runHook(fn: () => Promise<void>): Promise<void>;
14
15
  export declare function emitContext(text: string): void;
16
+ export declare function emitToolContext(text: string): void;
17
+ export declare function emitSessionStart(context: string, notice: string | null): void;
@@ -6,7 +6,7 @@ export function capturingDisabled(scripted) {
6
6
  export async function readPayload() {
7
7
  const chunks = [];
8
8
  for await (const chunk of process.stdin)
9
- chunks.push(Buffer.from(chunk));
9
+ chunks.push(chunk);
10
10
  const raw = Buffer.concat(chunks).toString('utf8').trim();
11
11
  if (raw.length === 0)
12
12
  return {};
@@ -30,3 +30,20 @@ export function emitContext(text) {
30
30
  if (text.trim().length > 0)
31
31
  process.stdout.write(text);
32
32
  }
33
+ export function emitToolContext(text) {
34
+ if (text.trim().length === 0)
35
+ return;
36
+ process.stdout.write(JSON.stringify({
37
+ hookSpecificOutput: { hookEventName: 'PreToolUse', additionalContext: text },
38
+ }));
39
+ }
40
+ export function emitSessionStart(context, notice) {
41
+ if (!notice) {
42
+ emitContext(context);
43
+ return;
44
+ }
45
+ process.stdout.write(JSON.stringify({
46
+ systemMessage: notice,
47
+ hookSpecificOutput: { hookEventName: 'SessionStart', additionalContext: context },
48
+ }));
49
+ }
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ export {};
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env node
2
+ import { takeReady } from '../pick/pending.js';
3
+ import { emitToolContext, readPayload, runHook } from './payload.js';
4
+ import { markShown } from './shown.js';
5
+ await runHook(async () => {
6
+ if (process.env['CLAUDE_DB_CAPTURE'] === 'off')
7
+ return;
8
+ const sessionId = (await readPayload()).session_id;
9
+ if (!sessionId)
10
+ return;
11
+ const ready = takeReady(sessionId);
12
+ if (!ready)
13
+ return;
14
+ markShown(sessionId, ready.ids);
15
+ emitToolContext(ready.text);
16
+ });
@@ -0,0 +1,21 @@
1
+ import type { RecallContext } from '../context.js';
2
+ import type { ObservationIndexEntry } from '../types.js';
3
+ export declare const PICK_POOL = 10;
4
+ export interface PromptRecallRequest {
5
+ prompt: string;
6
+ project: string;
7
+ sessionId: string;
8
+ shown: ReadonlySet<string>;
9
+ until?: number;
10
+ }
11
+ export interface PromptCandidates {
12
+ prompt: string;
13
+ entries: ObservationIndexEntry[];
14
+ }
15
+ export interface PromptRecall {
16
+ block: string;
17
+ ids: string[];
18
+ }
19
+ export declare function promptCandidates(ctx: RecallContext, request: PromptRecallRequest): Promise<PromptCandidates | null>;
20
+ export declare function strictRecall(ctx: RecallContext, candidates: PromptCandidates): Promise<PromptRecall | null>;
21
+ export declare function withCost(block: string): string;
@@ -0,0 +1,36 @@
1
+ import { FACT_SESSION } from '../facts/model.js';
2
+ import { isSearchable, typedPrompt } from '../util/prompt.js';
3
+ import { clearsOverlap, renderPromptContext } from './relevance.js';
4
+ export const PICK_POOL = 10;
5
+ const STRICT_OVERLAP = 4;
6
+ export async function promptCandidates(ctx, request) {
7
+ const { inject } = ctx.config;
8
+ const prompt = typedPrompt(request.prompt);
9
+ if (!inject.perPrompt || prompt === null || !isSearchable(prompt))
10
+ return null;
11
+ const found = await ctx.search.search({
12
+ text: prompt,
13
+ project: request.project,
14
+ excludeSessions: [request.sessionId, FACT_SESSION],
15
+ limit: PICK_POOL,
16
+ ...(request.until !== undefined ? { until: request.until } : {}),
17
+ });
18
+ const entries = found.filter((entry) => !request.shown.has(entry.id));
19
+ if (!entries.some((entry) => clearsOverlap(prompt, entry, inject.minOverlap)))
20
+ return null;
21
+ return { prompt, entries };
22
+ }
23
+ export async function strictRecall(ctx, candidates) {
24
+ const { inject } = ctx.config;
25
+ const [top] = candidates.entries;
26
+ const floor = Math.max(inject.minOverlap, STRICT_OVERLAP);
27
+ if (!top || !clearsOverlap(candidates.prompt, top, floor))
28
+ return null;
29
+ const expanded = inject.expandTop > 0 ? await ctx.search.getObservations([top.id]) : [];
30
+ const ids = [];
31
+ const block = renderPromptContext([top], inject.promptMaxChars, expanded, inject.expandMaxChars, ids);
32
+ return block ? { block, ids } : null;
33
+ }
34
+ export function withCost(block) {
35
+ return `${block}\n(context ≈ ${Math.round(block.length / 4)} tokens)\n`;
36
+ }
@@ -1,6 +1,14 @@
1
1
  import type { Observation, ObservationIndexEntry } from '../types.js';
2
- export declare function overlapCount(prompt: string, entry: {
2
+ type Matchable = {
3
3
  title: string;
4
4
  snippet?: string | undefined;
5
- }): number;
6
- export declare function renderPromptContext(entries: ObservationIndexEntry[], maxChars: number, expanded?: Observation[], expandMaxChars?: number, shown?: string[]): string | null;
5
+ };
6
+ export declare function overlapCount(prompt: string, entry: Matchable): number;
7
+ export declare function clearsOverlap(prompt: string, entry: Matchable, floor: number): boolean;
8
+ export declare function renderPromptContext(entries: ObservationIndexEntry[], maxChars: number, expanded?: Observation[], expandMaxChars?: number, shown?: string[], now?: number): string | null;
9
+ export interface PickedLine {
10
+ memory: Observation;
11
+ quote: string;
12
+ }
13
+ export declare function renderPicked(picks: PickedLine[], maxChars: number, shown?: string[], now?: number): string | null;
14
+ export {};
@@ -1,48 +1,78 @@
1
+ import { redact } from '../capture/redact.js';
1
2
  import { meaningfulTokens } from '../search/stopwords.js';
3
+ import { formatDay } from '../util/day.js';
2
4
  import { toShortId } from '../util/shortid.js';
5
+ function tokens(text) {
6
+ return new Set(meaningfulTokens(text));
7
+ }
3
8
  export function overlapCount(prompt, entry) {
4
- const wanted = new Set(meaningfulTokens(prompt.toLowerCase()));
5
- const offered = new Set(meaningfulTokens(`${entry.title} ${entry.snippet ?? ''}`.toLowerCase()));
9
+ const offered = tokens(`${entry.title} ${entry.snippet ?? ''}`);
6
10
  let shared = 0;
7
- for (const word of wanted)
11
+ for (const word of tokens(prompt))
8
12
  if (offered.has(word))
9
13
  shared++;
10
14
  return shared;
11
15
  }
12
- export function renderPromptContext(entries, maxChars, expanded = [], expandMaxChars = 900, shown = []) {
13
- if (entries.length === 0)
14
- return null;
15
- const expandedIds = new Set(expanded.map((obs) => obs.id));
16
- const sections = [];
17
- for (const obs of expanded) {
18
- const date = new Date(obs.createdAt).toISOString().slice(0, 10);
19
- sections.push(`${toShortId(obs.id)} ${obs.kind} ${date} ${obs.title}\n` + clip(obs.body, expandMaxChars));
20
- shown.push(obs.id);
21
- }
16
+ export function clearsOverlap(prompt, entry, floor) {
17
+ if (floor === 0)
18
+ return true;
19
+ return overlapCount(prompt, entry) >= Math.min(floor, tokens(prompt).size);
20
+ }
21
+ export function renderPromptContext(entries, maxChars, expanded = [], expandMaxChars = 900, shown = [], now = Date.now()) {
22
+ const bodies = new Map(expanded.map((obs) => [obs.id, obs.body]));
22
23
  const lines = [];
24
+ const titles = new Set();
23
25
  let budget = maxChars;
24
26
  for (const entry of entries) {
25
- if (expandedIds.has(entry.id))
27
+ if (titles.has(entry.title)) {
28
+ shown.push(entry.id);
26
29
  continue;
27
- const date = new Date(entry.createdAt).toISOString().slice(0, 7);
28
- const head = `${toShortId(entry.id)} ${entry.kind} ${date} ${entry.title}`;
29
- const line = entry.snippet ? `${head}\n ${entry.snippet}` : head;
30
+ }
31
+ titles.add(entry.title);
32
+ const line = `- ${formatDay(entry.createdAt, now)}: ${redact(entry.title)} (${toShortId(entry.id)})`;
33
+ const body = bodies.get(entry.id);
34
+ if (body !== undefined) {
35
+ lines.push(`${line}\n${indent(clip(redact(body), expandMaxChars))}`);
36
+ shown.push(entry.id);
37
+ continue;
38
+ }
30
39
  if (line.length > budget)
31
40
  break;
32
41
  budget -= line.length + 1;
33
42
  lines.push(line);
34
43
  shown.push(entry.id);
35
44
  }
36
- if (sections.length === 0 && lines.length === 0)
45
+ if (lines.length === 0)
46
+ return null;
47
+ return ['<memory>', ...lines, '</memory>'].join('\n');
48
+ }
49
+ const ASKED_CHARS = 120;
50
+ function askedIn(body) {
51
+ const asked = /^Asked: (.*)$/m.exec(body)?.[1]?.replace(/\s+/g, ' ').trim() ?? '';
52
+ return asked.length > ASKED_CHARS ? `${asked.slice(0, ASKED_CHARS)}…` : asked;
53
+ }
54
+ export function renderPicked(picks, maxChars, shown = [], now = Date.now()) {
55
+ const lines = [];
56
+ let budget = maxChars;
57
+ for (const { memory, quote } of picks) {
58
+ const asked = askedIn(memory.body);
59
+ const text = redact(asked ? `asked "${asked}": ${quote}` : quote);
60
+ const line = `- ${formatDay(memory.createdAt, now)}: ${text} (${toShortId(memory.id)})`;
61
+ if (line.length > budget)
62
+ break;
63
+ budget -= line.length + 1;
64
+ lines.push(line);
65
+ shown.push(memory.id);
66
+ }
67
+ if (lines.length === 0)
37
68
  return null;
38
- return [
39
- '<recalled-memory>',
40
- ...sections,
41
- ...(lines.length > 0 ? [lines.join('\n')] : []),
42
- '</recalled-memory>',
43
- 'Prior work on this project. Expand any listed id with get_observations ' +
44
- 'before asking the user to re-explain.',
45
- ].join('\n');
69
+ return ['<memory>', ...lines, '</memory>'].join('\n');
70
+ }
71
+ function indent(text) {
72
+ return text
73
+ .split('\n')
74
+ .map((line) => ` ${line}`)
75
+ .join('\n');
46
76
  }
47
77
  function clip(text, max) {
48
78
  return text.length <= max ? text : `${text.slice(0, max)}\n... [truncated]`;
@@ -9,6 +9,8 @@ import { capturingDisabled, readPayload, runHook } from './payload.js';
9
9
  import { loadConfig } from '../config/index.js';
10
10
  import { resolveProject } from '../util/project.js';
11
11
  import { silenceSqliteWarning } from '../util/warnings.js';
12
+ import { distillInBackground } from './background.js';
13
+ import { clearPick, sweepPicks } from '../pick/pending.js';
12
14
  silenceSqliteWarning();
13
15
  await runHook(async () => {
14
16
  if (capturingDisabled(loadConfig().capture.scripted))
@@ -22,7 +24,7 @@ await runHook(async () => {
22
24
  try {
23
25
  const result = await flushSession(ctx, sessionId, project, payload.transcript_path);
24
26
  let summary = result.summary;
25
- if (ctx.config.capture.summarize === 'on') {
27
+ if (!ctx.config.distill.enabled && ctx.config.capture.summarize === 'on') {
26
28
  const path = payload.transcript_path ?? transcriptPathFor(project, sessionId);
27
29
  const { turns } = readTranscript(path, 0);
28
30
  const observations = observationsFromTurns(turns, sessionId, project, ctx.config);
@@ -38,6 +40,10 @@ await runHook(async () => {
38
40
  ...(summary ? { summary } : {}),
39
41
  });
40
42
  clearCursor(sessionId);
43
+ clearPick(sessionId);
44
+ sweepPicks();
45
+ if (ctx.config.distill.enabled)
46
+ distillInBackground(project, sessionId);
41
47
  if (ctx.config.updates !== 'off' && isDue(readState())) {
42
48
  const cli = resolve(dirname(fileURLToPath(import.meta.url)), '..', 'cli', 'index.js');
43
49
  spawn(process.execPath, [cli, 'update', '--quiet'], {
@@ -4,16 +4,22 @@ import { homedir } from 'node:os';
4
4
  import { dirname, join, resolve } from 'node:path';
5
5
  import { fileURLToPath } from 'node:url';
6
6
  import { createContext } from '../context.js';
7
- import { openWork } from '../capture/index.js';
8
- import { emitContext, readPayload, runHook } from './payload.js';
7
+ import { distillNotice } from '../facts/notice.js';
8
+ import { startFacts } from '../facts/start.js';
9
+ import { startBackgroundWork } from './background.js';
10
+ import { recoverAfterCompact } from './compact.js';
11
+ import { capturingDisabled, emitSessionStart, readPayload, runHook } from './payload.js';
12
+ import { markShown } from './shown.js';
13
+ import { legacyBlock } from './start-legacy.js';
9
14
  import { resolveProject } from '../util/project.js';
10
15
  import { mentionsPath } from '../util/paths.js';
11
- import { toShortId } from '../util/shortid.js';
12
16
  import { refreshInstalled } from '../cli/refresh.js';
13
17
  import { updateNotice } from '../update.js';
14
18
  import { silenceSqliteWarning } from '../util/warnings.js';
15
19
  silenceSqliteWarning();
16
20
  const SERVER = resolve(dirname(fileURLToPath(import.meta.url)), '..', 'mcp', 'server.js');
21
+ const SCAN_HINT = 'Code graph not built yet — run `claude-db scan` once to enable ' +
22
+ 'find_usages and the symbol-grep hook.';
17
23
  async function refreshGraphQuietly(store, project) {
18
24
  try {
19
25
  if ((await store.scannedFiles(project)).length === 0)
@@ -50,64 +56,30 @@ await runHook(async () => {
50
56
  refreshInstalledQuietly(project);
51
57
  const ctx = await createContext();
52
58
  try {
53
- const sessions = await ctx.store.recentSessions(project, ctx.config.inject.sessions);
54
- const scanned = (await ctx.store.scannedFiles(project)).length;
55
- const scanHint = 'Code graph not built yet — run `claude-db scan` once to enable ' +
56
- 'find_usages and the symbol-grep hook.';
57
- if (sessions.length === 0) {
58
- emitContext('<project-memory>none yet for this project; ' +
59
- 'it is recorded as you work</project-memory>\n' +
60
- (scanned === 0 ? `${scanHint}\n` : ''));
61
- return;
62
- }
63
- const lines = ['<project-memory>'];
64
- let budget = ctx.config.inject.maxChars;
65
- for (const session of sessions) {
66
- const when = new Date(session.startedAt).toISOString().slice(0, 10);
67
- const line = `- [${when}] ${session.summary ?? ''}`;
68
- if (line.length > budget)
69
- break;
70
- budget -= line.length;
71
- lines.push(line);
72
- }
73
- const open = await openWork(ctx.store, project);
74
- if (open.length > 0) {
75
- lines.push('');
76
- lines.push('Not committed yet, newest first:');
77
- for (const obs of open.slice(0, 3)) {
78
- lines.push(`- ${obs.title}`);
79
- }
80
- }
81
- const rules = (await ctx.store.list({ project, kind: 'preference', limit: 100, newest: true }))
82
- .sort((a, b) => {
83
- const manual = Number(b.sessionId === 'manual') - Number(a.sessionId === 'manual');
84
- return manual !== 0 ? manual : b.createdAt - a.createdAt;
85
- })
86
- .slice(0, 8);
87
- if (rules.length > 0) {
88
- lines.push('');
89
- lines.push('Standing rules on record — expand any id with get_observations:');
90
- for (const obs of rules) {
91
- const when = new Date(obs.createdAt).toISOString().slice(0, 10);
92
- const line = `- ${toShortId(obs.id)} [${when}] ${obs.title}`.slice(0, 140);
93
- if (line.length > budget)
94
- break;
95
- budget -= line.length;
96
- lines.push(line);
97
- }
98
- }
99
- lines.push('</project-memory>');
100
- if (mcpRegistered(project)) {
101
- lines.push("Search this project's full history with the memory MCP tools before " +
59
+ const capturing = !capturingDisabled(ctx.config.capture.scripted);
60
+ if (capturing)
61
+ startBackgroundWork(project, ctx.config.database);
62
+ const earlier = payload.source === 'compact' && payload.session_id
63
+ ? await recoverAfterCompact(ctx, project, payload.session_id, payload.transcript_path)
64
+ : null;
65
+ const restored = earlier?.ids ?? new Set();
66
+ const facts = await startFacts(ctx, project, restored);
67
+ const legacy = facts ? null : await legacyBlock(ctx, project, restored);
68
+ if (facts && payload.session_id)
69
+ markShown(payload.session_id, [...facts.ids]);
70
+ const parts = [earlier?.block, facts?.block ?? legacy?.text].filter((part) => Boolean(part));
71
+ if ((facts || legacy?.hasMemory) && mcpRegistered(project)) {
72
+ parts.push("Search this project's full history with the memory MCP tools before " +
102
73
  'asking the user to re-explain prior decisions.');
103
74
  }
104
- if (scanned === 0)
105
- lines.push(scanHint);
106
- const notice = ctx.config.updates === 'off' ? null : updateNotice();
107
- if (notice)
108
- lines.push(notice);
109
- const body = lines.join('\n');
110
- emitContext(`${body}\n(context ≈ ${Math.round(body.length / 4)} tokens)\n`);
75
+ if ((await ctx.store.scannedFiles(project)).length === 0)
76
+ parts.push(SCAN_HINT);
77
+ const update = ctx.config.updates === 'off' ? null : updateNotice();
78
+ if (update)
79
+ parts.push(update);
80
+ const body = parts.join('\n');
81
+ const notice = capturing ? await distillNotice(ctx, project) : null;
82
+ emitSessionStart(`${body}\n(context ≈ ${Math.round(body.length / 4)} tokens)\n`, notice);
111
83
  await refreshGraphQuietly(ctx.store, project);
112
84
  }
113
85
  finally {
@@ -1,2 +1,3 @@
1
1
  export declare function readShown(sessionId: string): Set<string>;
2
+ export declare function forgetShown(sessionId: string): void;
2
3
  export declare function markShown(sessionId: string, ids: string[]): void;
@@ -1,6 +1,6 @@
1
- import { mkdirSync, readFileSync, writeFileSync } from 'node:fs';
1
+ import { mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
2
2
  import { dirname, join } from 'node:path';
3
- import { CONFIG_DIR } from '../config/index.js';
3
+ import { CONFIG_DIR } from '../config/dir.js';
4
4
  const LIMIT = 300;
5
5
  function shownPath(sessionId) {
6
6
  return join(CONFIG_DIR, 'cursors', `${sessionId.replace(/[^\w-]/g, '_')}.shown`);
@@ -13,6 +13,9 @@ export function readShown(sessionId) {
13
13
  return new Set();
14
14
  }
15
15
  }
16
+ export function forgetShown(sessionId) {
17
+ rmSync(shownPath(sessionId), { force: true });
18
+ }
16
19
  export function markShown(sessionId, ids) {
17
20
  if (ids.length === 0)
18
21
  return;
@@ -0,0 +1,6 @@
1
+ import type { RecallContext } from '../context.js';
2
+ export interface LegacyBlock {
3
+ text: string;
4
+ hasMemory: boolean;
5
+ }
6
+ export declare function legacyBlock(ctx: RecallContext, project: string, restored: ReadonlySet<string>): Promise<LegacyBlock>;
@@ -0,0 +1,47 @@
1
+ import { openWork } from '../capture/index.js';
2
+ import { toShortId } from '../util/shortid.js';
3
+ export async function legacyBlock(ctx, project, restored) {
4
+ const sessions = await ctx.store.recentSessions(project, ctx.config.inject.sessions);
5
+ if (sessions.length === 0) {
6
+ return {
7
+ text: '<project-memory>none yet for this project; it is recorded as you work</project-memory>',
8
+ hasMemory: false,
9
+ };
10
+ }
11
+ const lines = ['<project-memory>'];
12
+ let budget = ctx.config.inject.maxChars;
13
+ for (const session of sessions) {
14
+ const when = new Date(session.startedAt).toISOString().slice(0, 10);
15
+ const line = `- [${when}] ${session.summary ?? ''}`;
16
+ if (line.length > budget)
17
+ break;
18
+ budget -= line.length;
19
+ lines.push(line);
20
+ }
21
+ const open = (await openWork(ctx.store, project)).filter((obs) => !restored.has(obs.id));
22
+ if (open.length > 0) {
23
+ lines.push('', 'Not committed yet, newest first:');
24
+ for (const obs of open.slice(0, 3))
25
+ lines.push(`- ${obs.title}`);
26
+ }
27
+ const rules = (await ctx.store.list({ project, kind: 'preference', limit: 100, newest: true }))
28
+ .filter((obs) => obs.status !== 'replaced' && !restored.has(obs.id))
29
+ .sort((a, b) => {
30
+ const manual = Number(b.sessionId === 'manual') - Number(a.sessionId === 'manual');
31
+ return manual !== 0 ? manual : b.createdAt - a.createdAt;
32
+ })
33
+ .slice(0, 8);
34
+ if (rules.length > 0) {
35
+ lines.push('', 'Standing rules on record — expand any id with get_observations:');
36
+ for (const obs of rules) {
37
+ const when = new Date(obs.createdAt).toISOString().slice(0, 10);
38
+ const line = `- ${toShortId(obs.id)} [${when}] ${obs.title}`.slice(0, 140);
39
+ if (line.length > budget)
40
+ break;
41
+ budget -= line.length;
42
+ lines.push(line);
43
+ }
44
+ }
45
+ lines.push('</project-memory>');
46
+ return { text: lines.join('\n'), hasMemory: true };
47
+ }