claude-db 0.10.3 → 0.11.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.
Files changed (93) hide show
  1. package/CHANGELOG.md +39 -0
  2. package/README.md +2 -1
  3. package/dist/catchup/gather.d.ts +17 -0
  4. package/dist/catchup/gather.js +39 -0
  5. package/dist/catchup/git-state.d.ts +7 -0
  6. package/dist/catchup/git-state.js +32 -0
  7. package/dist/catchup/render.d.ts +3 -0
  8. package/dist/catchup/render.js +41 -0
  9. package/dist/cli/backfill-output.d.ts +13 -0
  10. package/dist/cli/backfill-output.js +78 -0
  11. package/dist/cli/commands/catchup.d.ts +1 -0
  12. package/dist/cli/commands/catchup.js +14 -0
  13. package/dist/cli/commands/distill.js +13 -2
  14. package/dist/cli/commands/doctor.d.ts +2 -0
  15. package/dist/cli/commands/doctor.js +42 -3
  16. package/dist/cli/commands/insight.js +2 -1
  17. package/dist/cli/commands/setup.js +29 -9
  18. package/dist/cli/index.js +9 -0
  19. package/dist/cli/install.d.ts +6 -1
  20. package/dist/cli/install.js +8 -18
  21. package/dist/cli/paths.d.ts +1 -1
  22. package/dist/cli/paths.js +2 -2
  23. package/dist/cli/refresh.js +4 -12
  24. package/dist/cli/skills.d.ts +17 -0
  25. package/dist/cli/skills.js +65 -0
  26. package/dist/cli/usage.js +2 -0
  27. package/dist/config/schema.d.ts +3 -0
  28. package/dist/config/schema.js +8 -0
  29. package/dist/context.js +5 -2
  30. package/dist/facts/distill.d.ts +16 -1
  31. package/dist/facts/distill.js +20 -7
  32. package/dist/facts/handoff.d.ts +6 -0
  33. package/dist/facts/handoff.js +25 -0
  34. package/dist/facts/health.d.ts +3 -0
  35. package/dist/facts/health.js +10 -0
  36. package/dist/facts/model.d.ts +1 -0
  37. package/dist/facts/model.js +12 -0
  38. package/dist/facts/start.js +16 -5
  39. package/dist/store/adapter.d.ts +4 -2
  40. package/dist/store/adapter.js +1 -1
  41. package/dist/store/mongo/docs.d.ts +6 -0
  42. package/dist/store/mongo/filters.d.ts +2 -1
  43. package/dist/store/mongo/filters.js +9 -2
  44. package/dist/store/mongo/index.d.ts +5 -2
  45. package/dist/store/mongo/index.js +13 -2
  46. package/dist/store/mongo/observations.js +9 -5
  47. package/dist/store/mongo/project-links.d.ts +4 -0
  48. package/dist/store/mongo/project-links.js +13 -0
  49. package/dist/store/mongo/search.js +3 -0
  50. package/dist/store/mongo/sessions.d.ts +2 -2
  51. package/dist/store/mongo/sessions.js +5 -1
  52. package/dist/store/mongo/vectorSearch.js +3 -0
  53. package/dist/store/postgres/filters.d.ts +2 -1
  54. package/dist/store/postgres/filters.js +11 -4
  55. package/dist/store/postgres/index.d.ts +4 -2
  56. package/dist/store/postgres/index.js +7 -0
  57. package/dist/store/postgres/observations.js +15 -10
  58. package/dist/store/postgres/project-links.d.ts +3 -0
  59. package/dist/store/postgres/project-links.js +15 -0
  60. package/dist/store/postgres/schema.d.ts +2 -2
  61. package/dist/store/postgres/schema.js +9 -1
  62. package/dist/store/postgres/search.js +3 -0
  63. package/dist/store/postgres/sessions.d.ts +2 -2
  64. package/dist/store/postgres/sessions.js +9 -2
  65. package/dist/store/postgres/vectorSearch.js +3 -0
  66. package/dist/store/project-resolver.d.ts +16 -0
  67. package/dist/store/project-resolver.js +49 -0
  68. package/dist/store/project-scope.d.ts +5 -0
  69. package/dist/store/project-scope.js +16 -0
  70. package/dist/store/scoped-store.d.ts +3 -0
  71. package/dist/store/scoped-store.js +49 -0
  72. package/dist/store/sqlite/filters.d.ts +3 -2
  73. package/dist/store/sqlite/filters.js +23 -9
  74. package/dist/store/sqlite/index.d.ts +4 -2
  75. package/dist/store/sqlite/index.js +7 -0
  76. package/dist/store/sqlite/observations.js +13 -7
  77. package/dist/store/sqlite/project-links.d.ts +3 -0
  78. package/dist/store/sqlite/project-links.js +15 -0
  79. package/dist/store/sqlite/schema.sql +9 -0
  80. package/dist/store/sqlite/search.js +5 -0
  81. package/dist/store/sqlite/sessions.d.ts +2 -2
  82. package/dist/store/sqlite/sessions.js +8 -2
  83. package/dist/types.d.ts +4 -3
  84. package/dist/util/claude-binary.d.ts +15 -1
  85. package/dist/util/claude-binary.js +39 -7
  86. package/dist/util/project-key.d.ts +7 -0
  87. package/dist/util/project-key.js +23 -0
  88. package/dist/util/remote-key.d.ts +3 -0
  89. package/dist/util/remote-key.js +50 -0
  90. package/docs/setup-guide.md +21 -13
  91. package/package.json +1 -1
  92. package/skills/catchup/SKILL.md +44 -0
  93. package/skills/handoff/SKILL.md +62 -0
@@ -1,23 +1,15 @@
1
1
  import { BLOCK_START, INSTRUCTIONS, writeInstructions } from './instructions.js';
2
2
  import { refreshHooks } from './install.js';
3
- import { instructionsPathFor, settingsPathFor, skillPathFor } from './paths.js';
4
- import { readText, writeAtomic } from './files.js';
5
- import { resolve } from 'node:path';
3
+ import { instructionsPathFor, settingsPathFor } from './paths.js';
4
+ import { readText } from './files.js';
5
+ import { refreshSkills } from './skills.js';
6
6
  export function refreshInstalled(distDir, project) {
7
7
  const refreshed = [];
8
8
  for (const scope of ['project', 'global']) {
9
9
  const settingsPath = settingsPathFor(scope, project);
10
10
  if (refreshHooks(distDir, settingsPath))
11
11
  refreshed.push(settingsPath);
12
- const skillPath = skillPathFor(scope, project);
13
- const current = readText(skillPath);
14
- if (current.length > 0) {
15
- const packaged = readText(resolve(distDir, '..', 'skills', 'cdb-scan', 'SKILL.md'));
16
- if (packaged.length > 0 && packaged !== current) {
17
- writeAtomic(skillPath, packaged);
18
- refreshed.push(skillPath);
19
- }
20
- }
12
+ refreshed.push(...refreshSkills(distDir, scope, project));
21
13
  const instructionsPath = instructionsPathFor(scope, project);
22
14
  const existing = readText(instructionsPath);
23
15
  if (existing.includes(BLOCK_START) && !existing.includes(INSTRUCTIONS)) {
@@ -0,0 +1,17 @@
1
+ import type { Scope } from './paths.js';
2
+ export declare const SKILL_MARKER = "Shipped with claude-db.";
3
+ export interface SkillSpec {
4
+ name: string;
5
+ claimsExisting: boolean;
6
+ }
7
+ export declare const SKILLS: readonly SkillSpec[];
8
+ export type SkillInstallOutcome = 'written' | 'kept-yours' | 'missing-source';
9
+ export interface SkillInstallResult {
10
+ name: string;
11
+ path: string;
12
+ outcome: SkillInstallOutcome;
13
+ }
14
+ export declare function installSkills(distDir: string, scope: Scope, project: string): SkillInstallResult[];
15
+ export declare function refreshSkills(distDir: string, scope: Scope, project: string): string[];
16
+ export declare function removeSkills(scope: Scope, project: string): void;
17
+ export declare function describeSkills(project: string): string;
@@ -0,0 +1,65 @@
1
+ import { rmSync } from 'node:fs';
2
+ import { dirname, resolve } from 'node:path';
3
+ import { readText, writeAtomic } from './files.js';
4
+ import { skillPathFor } from './paths.js';
5
+ export const SKILL_MARKER = 'Shipped with claude-db.';
6
+ export const SKILLS = [
7
+ { name: 'cdb-scan', claimsExisting: true },
8
+ { name: 'catchup', claimsExisting: false },
9
+ { name: 'handoff', claimsExisting: false },
10
+ ];
11
+ function packagedBody(distDir, spec) {
12
+ return readText(resolve(distDir, '..', 'skills', spec.name, 'SKILL.md'));
13
+ }
14
+ function isOurs(spec, installedBody) {
15
+ return spec.claimsExisting || installedBody.includes(SKILL_MARKER);
16
+ }
17
+ export function installSkills(distDir, scope, project) {
18
+ return SKILLS.map((spec) => {
19
+ const path = skillPathFor(scope, project, spec.name);
20
+ const body = packagedBody(distDir, spec);
21
+ if (body.length === 0)
22
+ return { name: spec.name, path, outcome: 'missing-source' };
23
+ const installed = readText(path);
24
+ if (installed.length > 0 && !isOurs(spec, installed)) {
25
+ return { name: spec.name, path, outcome: 'kept-yours' };
26
+ }
27
+ writeAtomic(path, body);
28
+ return { name: spec.name, path, outcome: 'written' };
29
+ });
30
+ }
31
+ export function refreshSkills(distDir, scope, project) {
32
+ const installedHere = SKILLS.some((spec) => readText(skillPathFor(scope, project, spec.name)).length > 0);
33
+ if (!installedHere)
34
+ return [];
35
+ const refreshed = [];
36
+ for (const spec of SKILLS) {
37
+ const path = skillPathFor(scope, project, spec.name);
38
+ const installed = readText(path);
39
+ const body = packagedBody(distDir, spec);
40
+ const foreign = installed.length > 0 && !isOurs(spec, installed);
41
+ if (body.length === 0 || foreign || body === installed)
42
+ continue;
43
+ writeAtomic(path, body);
44
+ refreshed.push(path);
45
+ }
46
+ return refreshed;
47
+ }
48
+ export function removeSkills(scope, project) {
49
+ for (const spec of SKILLS) {
50
+ const path = skillPathFor(scope, project, spec.name);
51
+ if (!isOurs(spec, readText(path)))
52
+ continue;
53
+ rmSync(dirname(path), { recursive: true, force: true });
54
+ }
55
+ }
56
+ export function describeSkills(project) {
57
+ const scopes = ['project', 'global'];
58
+ const parts = SKILLS.map((spec) => {
59
+ const found = scopes.some((scope) => readText(skillPathFor(scope, project, spec.name)).length > 0);
60
+ return found ? `/${spec.name} ok` : `/${spec.name} MISSING`;
61
+ });
62
+ const missing = parts.some((part) => part.endsWith('MISSING'));
63
+ const line = `skills : ${parts.join(', ')}`;
64
+ return missing ? `${line}\n fix with: claude-db install` : line;
65
+ }
package/dist/cli/usage.js CHANGED
@@ -16,6 +16,7 @@ export function usage() {
16
16
  projects List every project with memory in this database
17
17
  merge [<old-path>] [--yes] Move memory from an old project path onto this one
18
18
  stats What this project's memory is made of
19
+ catchup Where did I stop: last chat, to-dos, git state, last handoff
19
20
  adoption How often sessions grep vs use the memory tools
20
21
  view [--export <file>] See this project's memory live in the browser
21
22
  distill [on|off] Turn chats into facts with Haiku; no argument shows status
@@ -35,6 +36,7 @@ export function usage() {
35
36
  reembed [--project] Re-embed with the current model
36
37
  prune --older-than <days> Delete old memory (dry run without --yes)
37
38
  reset [--project] --yes Delete stored memory (dry run without --yes)
39
+ --version, -v Print the installed version
38
40
 
39
41
  --project scope to the current repo via .claude/settings.local.json
40
42
  instead of every project on this machine
@@ -17,6 +17,9 @@ export declare const ConfigSchema: z.ZodObject<{
17
17
  notify: "notify";
18
18
  off: "off";
19
19
  }>>;
20
+ project: z.ZodPrefault<z.ZodObject<{
21
+ remote: z.ZodDefault<z.ZodString>;
22
+ }, z.core.$strip>>;
20
23
  capture: z.ZodPrefault<z.ZodObject<{
21
24
  tools: z.ZodDefault<z.ZodArray<z.ZodString>>;
22
25
  exclude: z.ZodDefault<z.ZodArray<z.ZodString>>;
@@ -10,6 +10,14 @@ export const ConfigSchema = z.object({
10
10
  })
11
11
  .prefault({}),
12
12
  updates: z.enum(['auto', 'notify', 'off']).default('notify'),
13
+ project: z
14
+ .object({
15
+ remote: z
16
+ .string()
17
+ .regex(/^[A-Za-z0-9._-]+$/)
18
+ .default('origin'),
19
+ })
20
+ .prefault({}),
13
21
  capture: z
14
22
  .object({
15
23
  tools: z.array(z.string()).default(['Edit', 'Write', 'Bash', 'NotebookEdit']),
package/dist/context.js CHANGED
@@ -2,10 +2,13 @@ import { loadConfig } from './config/index.js';
2
2
  import { createEmbedder, NoopEmbedder } from './embed/index.js';
3
3
  import { SearchService } from './search/index.js';
4
4
  import { createStore } from './store/index.js';
5
+ import { createScopeResolver } from './store/project-resolver.js';
6
+ import { withProjectScope } from './store/scoped-store.js';
5
7
  export async function createContext(overrides) {
6
8
  const config = { ...loadConfig(), ...overrides };
7
- const store = await createStore(config.database);
8
- await store.init();
9
+ const raw = await createStore(config.database);
10
+ await raw.init();
11
+ const store = withProjectScope(raw, createScopeResolver({ store: raw, remote: config.project.remote }));
9
12
  let pending = null;
10
13
  const embedder = () => {
11
14
  pending ??= withTimeout(createEmbedder(config.embeddings.provider).catch(() => new NoopEmbedder()), config.embeddings.timeoutMs);
@@ -10,14 +10,29 @@ export interface DistillResult {
10
10
  set: number;
11
11
  retired: number;
12
12
  }
13
+ export type BackfillStep = {
14
+ kind: 'begin';
15
+ total: number;
16
+ } | {
17
+ kind: 'start';
18
+ index: number;
19
+ total: number;
20
+ } | {
21
+ kind: 'done';
22
+ index: number;
23
+ total: number;
24
+ facts: number;
25
+ };
13
26
  export interface BackfillResult {
14
27
  distilled: number;
28
+ facts: number;
15
29
  remaining: number;
16
30
  stopped: 'done' | 'over-budget' | 'failed';
17
31
  }
18
32
  export declare function distillSession(ctx: RecallContext, project: string, sessionId: string, run?: Runner): Promise<DistillResult>;
19
33
  export declare function pendingSessions(ctx: RecallContext, project: string, now?: number): Promise<string[]>;
20
- export declare function backfill(ctx: RecallContext, project: string, run?: Runner): Promise<BackfillResult>;
34
+ export declare function oldestPendingAt(ctx: RecallContext, project: string, now?: number): Promise<number | null>;
35
+ export declare function backfill(ctx: RecallContext, project: string, run?: Runner, onStep?: (step: BackfillStep) => void): Promise<BackfillResult>;
21
36
  export declare function existingFacts(ctx: RecallContext, project: string): Promise<ExistingFact[]>;
22
37
  export declare function chatWindows(rows: Observation[], most?: number): string[];
23
38
  export declare function factCounts(ctx: RecallContext, project: string): Promise<{
@@ -2,7 +2,7 @@ import { embedObservations } from '../capture/flush.js';
2
2
  import { runHeadlessResult } from '../util/claude-cli.js';
3
3
  import { formatDay } from '../util/day.js';
4
4
  import { recordDistillFailure, recordDistillSuccess, takeDistillCalls } from './budget.js';
5
- import { FACT_SESSION, factId, factKey, factToObservation, factType, youScope } from './model.js';
5
+ import { FACT_SESSION, factId, factKey, factToObservation, factType, onePerKey, youScope, } from './model.js';
6
6
  import { buildDistillPrompt, parseOps } from './ops.js';
7
7
  export const FACTS_JOB = 'facts';
8
8
  const DAY_MS = 24 * 60 * 60 * 1000;
@@ -65,23 +65,36 @@ async function distillWindow(ctx, project, chat, source, at, run) {
65
65
  const retired = await ctx.store.markReplaced(retiring);
66
66
  return { ok: true, set: facts.length, retired, texts: sets.map((op) => op.text) };
67
67
  }
68
- export async function pendingSessions(ctx, project, now = Date.now()) {
68
+ async function pendingChats(ctx, project, now) {
69
69
  const cutoff = now - ctx.config.distill.backfillDays * DAY_MS;
70
70
  return (await ctx.store.recentSessions(project, 1000))
71
71
  .filter((session) => session.distilledAt === undefined && session.startedAt >= cutoff)
72
- .map((session) => session.id);
72
+ .map((session) => ({ id: session.id, startedAt: session.startedAt }));
73
+ }
74
+ export async function pendingSessions(ctx, project, now = Date.now()) {
75
+ return (await pendingChats(ctx, project, now)).map((chat) => chat.id);
76
+ }
77
+ export async function oldestPendingAt(ctx, project, now = Date.now()) {
78
+ const chats = await pendingChats(ctx, project, now);
79
+ return chats.length === 0 ? null : Math.min(...chats.map((chat) => chat.startedAt));
73
80
  }
74
- export async function backfill(ctx, project, run = runHeadlessResult) {
81
+ export async function backfill(ctx, project, run = runHeadlessResult, onStep = () => { }) {
75
82
  const pending = await pendingSessions(ctx, project);
83
+ const total = pending.length;
84
+ onStep({ kind: 'begin', total });
76
85
  let distilled = 0;
86
+ let facts = 0;
77
87
  for (const [index, sessionId] of pending.entries()) {
88
+ onStep({ kind: 'start', index: index + 1, total });
78
89
  const result = await distillSession(ctx, project, sessionId, run);
79
90
  if (result.outcome === 'over-budget' || result.outcome === 'failed') {
80
- return { distilled, remaining: pending.length - index, stopped: result.outcome };
91
+ return { distilled, facts, remaining: total - index, stopped: result.outcome };
81
92
  }
82
93
  distilled += 1;
94
+ facts += result.set;
95
+ onStep({ kind: 'done', index: index + 1, total, facts });
83
96
  }
84
- return { distilled, remaining: 0, stopped: 'done' };
97
+ return { distilled, facts, remaining: 0, stopped: 'done' };
85
98
  }
86
99
  export async function existingFacts(ctx, project) {
87
100
  const owners = [
@@ -96,7 +109,7 @@ export async function existingFacts(ctx, project) {
96
109
  newest: true,
97
110
  limit: EXISTING_LIMIT,
98
111
  });
99
- for (const obs of rows) {
112
+ for (const obs of onePerKey(rows.filter((row) => row.status !== 'replaced'))) {
100
113
  const key = factKey(obs);
101
114
  const type = factType(obs);
102
115
  if (obs.status === 'replaced' || !key || !type)
@@ -0,0 +1,6 @@
1
+ import type { RecallContext } from '../context.js';
2
+ import type { Observation } from '../types.js';
3
+ export declare const HANDOFF_TAG = "handoff";
4
+ export declare const HANDOFF_KEY = "handoff";
5
+ export declare function newestHandoff(ctx: RecallContext, project: string, now?: number): Promise<Observation | null>;
6
+ export declare function handoffBodyLines(obs: Observation): string[];
@@ -0,0 +1,25 @@
1
+ import { MANUAL_SESSION } from './render.js';
2
+ export const HANDOFF_TAG = 'handoff';
3
+ export const HANDOFF_KEY = 'handoff';
4
+ const DAY_MS = 24 * 60 * 60 * 1000;
5
+ const HANDOFF_MAX_AGE_MS = 14 * DAY_MS;
6
+ const MANUAL_NOTES_SCANNED = 50;
7
+ export async function newestHandoff(ctx, project, now = Date.now()) {
8
+ const notes = await ctx.store.list({
9
+ project,
10
+ sessionId: MANUAL_SESSION,
11
+ newest: true,
12
+ limit: MANUAL_NOTES_SCANNED,
13
+ });
14
+ const current = (obs) => obs.status !== 'replaced' &&
15
+ obs.tags.includes(HANDOFF_TAG) &&
16
+ now - obs.createdAt <= HANDOFF_MAX_AGE_MS;
17
+ return notes.find(current) ?? null;
18
+ }
19
+ export function handoffBodyLines(obs) {
20
+ return obs.body
21
+ .split('\n')
22
+ .slice(1)
23
+ .map((line) => line.trimEnd())
24
+ .filter((line) => line.trim().length > 0);
25
+ }
@@ -0,0 +1,3 @@
1
+ import type { BudgetUsage } from '../util/daily-budget.js';
2
+ export declare const STALE_WAITING_MS: number;
3
+ export declare function waitingWarning(waiting: number, oldestAt: number | null, usage: BudgetUsage, now?: number): string | null;
@@ -0,0 +1,10 @@
1
+ export const STALE_WAITING_MS = 24 * 60 * 60 * 1000;
2
+ export function waitingWarning(waiting, oldestAt, usage, now = Date.now()) {
3
+ if (waiting === 0 || oldestAt === null)
4
+ return null;
5
+ if (now - oldestAt < STALE_WAITING_MS)
6
+ return null;
7
+ if (usage.used > 0 || usage.pausedUntil > now)
8
+ return null;
9
+ return `warning : ${waiting} chat(s) have waited over a day and no facts were tried today. Run claude-db doctor`;
10
+ }
@@ -24,5 +24,6 @@ export declare function scopeProject(scope: FactScope, project: string): string;
24
24
  export declare function factId(owner: string, key: string): string;
25
25
  export declare function factType(obs: Observation): FactType | null;
26
26
  export declare function factKey(obs: Observation): string | null;
27
+ export declare function onePerKey(rows: Observation[]): Observation[];
27
28
  export declare function factLabel(obs: Observation): string | null;
28
29
  export declare function factToObservation(fact: Fact, project: string): Observation;
@@ -55,6 +55,18 @@ export function factType(obs) {
55
55
  export function factKey(obs) {
56
56
  return obs.tags.find((tag) => tag.startsWith('key:'))?.slice(4) ?? null;
57
57
  }
58
+ export function onePerKey(rows) {
59
+ const seen = new Set();
60
+ return rows.filter((obs) => {
61
+ const key = factKey(obs);
62
+ if (key === null)
63
+ return true;
64
+ if (seen.has(key))
65
+ return false;
66
+ seen.add(key);
67
+ return true;
68
+ });
69
+ }
58
70
  export function factLabel(obs) {
59
71
  const type = factType(obs);
60
72
  if (type)
@@ -1,6 +1,8 @@
1
1
  import { openWork } from '../capture/progress.js';
2
- import { FACT_SESSION, factType, youScope } from './model.js';
2
+ import { FACT_SESSION, factType, onePerKey, youScope } from './model.js';
3
+ import { newestHandoff, handoffBodyLines } from './handoff.js';
3
4
  import { MANUAL_SESSION, factLine, seenByClaude } from './render.js';
5
+ import { formatDay } from '../util/day.js';
4
6
  const ABOUT_YOU = 6;
5
7
  const THIS_PROJECT = 10;
6
8
  const TODOS = 4;
@@ -9,10 +11,12 @@ const ORDER = ['rule', 'decision', 'deadend', 'fact'];
9
11
  export async function startFacts(ctx, project, hidden = new Set()) {
10
12
  const visible = (obs) => obs.status !== 'replaced' && !hidden.has(obs.id) && !seenByClaude(obs, project);
11
13
  const list = async (owner, sessionId, limit) => (await ctx.store.list({ project: owner, sessionId, newest: true, limit })).filter(visible);
12
- const you = await list(youScope(), FACT_SESSION, 50);
13
- const facts = await list(project, FACT_SESSION, 300);
14
- const manual = await list(project, MANUAL_SESSION, 20);
15
- if (you.length + facts.length + manual.length === 0)
14
+ const you = onePerKey(await list(youScope(), FACT_SESSION, 50));
15
+ const facts = onePerKey(await list(project, FACT_SESSION, 300));
16
+ const handoff = await newestHandoff(ctx, project);
17
+ const shownHandoff = handoff && visible(handoff) ? handoff : null;
18
+ const manual = (await list(project, MANUAL_SESSION, 20)).filter((obs) => obs.id !== handoff?.id);
19
+ if (you.length + facts.length + manual.length === 0 && !shownHandoff)
16
20
  return null;
17
21
  const rank = (obs) => {
18
22
  const index = ORDER.indexOf(factType(obs) ?? 'rule');
@@ -26,6 +30,10 @@ export async function startFacts(ctx, project, hidden = new Set()) {
26
30
  const sections = [
27
31
  ['About you:', you.slice(0, ABOUT_YOU).map((obs) => [factLine(obs), obs.id])],
28
32
  ['This project:', known.slice(0, THIS_PROJECT).map((obs) => [factLine(obs), obs.id])],
33
+ [
34
+ shownHandoff ? `Last handoff (${formatDay(shownHandoff.createdAt)}):` : '',
35
+ shownHandoff ? handoffEntries(shownHandoff) : [],
36
+ ],
29
37
  [
30
38
  'Where you stopped:',
31
39
  [
@@ -55,3 +63,6 @@ export async function startFacts(ctx, project, hidden = new Set()) {
55
63
  lines.push('</memory>');
56
64
  return { block: lines.join('\n'), ids };
57
65
  }
66
+ function handoffEntries(handoff) {
67
+ return handoffBodyLines(handoff).map((line, index) => [line, index === 0 ? handoff.id : null]);
68
+ }
@@ -1,4 +1,4 @@
1
- import type { CodeEdge, CodeSymbol, EdgeFilter, ListFilter, Observation, ObservationIndexEntry, RemoveFilter, ScannedFile, SearchQuery, Session, SymbolFilter, TimelineQuery } from '../types.js';
1
+ import type { CodeEdge, CodeSymbol, EdgeFilter, ListFilter, Observation, ObservationIndexEntry, ProjectFilter, RemoveFilter, ScannedFile, SearchQuery, Session, SymbolFilter, TimelineQuery } from '../types.js';
2
2
  export interface ProjectSummary {
3
3
  project: string;
4
4
  observations: number;
@@ -9,9 +9,11 @@ export interface MemoryStore {
9
9
  init(): Promise<void>;
10
10
  close(): Promise<void>;
11
11
  ping(): Promise<boolean>;
12
+ linkProject(folder: string, key: string, now?: number): Promise<void>;
13
+ projectScope(folder: string): Promise<string[]>;
12
14
  upsertSession(session: Session): Promise<void>;
13
15
  getSession(id: string): Promise<Session | null>;
14
- recentSessions(project: string, limit: number): Promise<Session[]>;
16
+ recentSessions(project: ProjectFilter, limit: number): Promise<Session[]>;
15
17
  sessionProjects(): Promise<string[]>;
16
18
  clearSummary(id: string): Promise<boolean>;
17
19
  insertObservations(observations: Observation[]): Promise<void>;
@@ -1,7 +1,7 @@
1
1
  export function isWholeScope(filter) {
2
2
  return !filter.ids && !filter.kind && filter.before === undefined;
3
3
  }
4
- const OURS = /^(claude_db_meta|sessions|observations|symbols|symbol_edges|scanned_files)(_fts(_\w+)?)?$/;
4
+ const OURS = /^(claude_db_meta|sessions|observations|symbols|symbol_edges|scanned_files|project_links)(_fts(_\w+)?)?$/;
5
5
  export function foreignNames(names) {
6
6
  return names.filter((name) => name.length > 0 && !OURS.test(name));
7
7
  }
@@ -54,6 +54,12 @@ export interface ScannedFileDoc extends Doc {
54
54
  hash: string;
55
55
  scannedAt: number;
56
56
  }
57
+ export interface ProjectLinkDoc extends Doc {
58
+ _id: string;
59
+ folder: string;
60
+ projectKey: string;
61
+ firstSeen: number;
62
+ }
57
63
  export declare function upsertsOf<T extends object>(rows: T[], idOf: (row: T) => string): Doc[];
58
64
  export declare function toSession(doc: SessionDoc): Session;
59
65
  export declare function toDoc(obs: Observation): ObservationDoc;
@@ -1,5 +1,6 @@
1
1
  import type { Doc } from './driver.js';
2
- import type { SearchQuery } from '../../types.js';
2
+ import type { ProjectFilter, SearchQuery } from '../../types.js';
3
+ export declare function projectMatch(filter: ProjectFilter | undefined): Doc;
3
4
  export interface VectorCache {
4
5
  atlasVectorIndex: boolean | null;
5
6
  }
@@ -1,7 +1,14 @@
1
+ import { projectsOf } from '../project-scope.js';
2
+ export function projectMatch(filter) {
3
+ const projects = projectsOf(filter);
4
+ if (projects.length === 0)
5
+ return {};
6
+ const [only] = projects;
7
+ return { project: projects.length === 1 ? only : { $in: projects } };
8
+ }
1
9
  export function scopeFilter(query) {
2
10
  const filter = {};
3
- if (query.project)
4
- filter['project'] = query.project;
11
+ Object.assign(filter, projectMatch(query.project));
5
12
  if (query.kind)
6
13
  filter['kind'] = query.kind;
7
14
  if (query.tag)
@@ -1,4 +1,4 @@
1
- import type { CodeEdge, CodeSymbol, EdgeFilter, ListFilter, Observation, ObservationIndexEntry, RemoveFilter, ScannedFile, SearchQuery, Session, SymbolFilter, TimelineQuery } from '../../types.js';
1
+ import type { CodeEdge, CodeSymbol, EdgeFilter, ListFilter, Observation, ObservationIndexEntry, ProjectFilter, RemoveFilter, ScannedFile, SearchQuery, Session, SymbolFilter, TimelineQuery } from '../../types.js';
2
2
  import type { MemoryStore, ProjectSummary } from '../adapter.js';
3
3
  export declare class MongoStore implements MemoryStore {
4
4
  private readonly client;
@@ -8,6 +8,7 @@ export declare class MongoStore implements MemoryStore {
8
8
  private readonly symbols;
9
9
  private readonly edges;
10
10
  private readonly scanned;
11
+ private readonly links;
11
12
  readonly kind = "mongodb";
12
13
  private readonly cache;
13
14
  private constructor();
@@ -15,9 +16,11 @@ export declare class MongoStore implements MemoryStore {
15
16
  init(): Promise<void>;
16
17
  close(): Promise<void>;
17
18
  ping(): Promise<boolean>;
19
+ linkProject(folder: string, key: string, now?: number): Promise<void>;
20
+ projectScope(folder: string): Promise<string[]>;
18
21
  upsertSession(session: Session): Promise<void>;
19
22
  getSession(id: string): Promise<Session | null>;
20
- recentSessions(project: string, limit: number): Promise<Session[]>;
23
+ recentSessions(project: ProjectFilter, limit: number): Promise<Session[]>;
21
24
  sessionProjects(): Promise<string[]>;
22
25
  clearSummary(id: string): Promise<boolean>;
23
26
  insertObservations(observations: Observation[]): Promise<void>;
@@ -3,6 +3,7 @@ import * as searchOps from './search.js';
3
3
  import * as observationsOps from './observations.js';
4
4
  import * as sessionsOps from './sessions.js';
5
5
  import * as graphOps from './graph.js';
6
+ import * as projectLinksOps from './project-links.js';
6
7
  import { databaseNameFrom } from './helpers.js';
7
8
  import { foreignNames } from '../adapter.js';
8
9
  import { importMongo } from './driver.js';
@@ -14,9 +15,10 @@ export class MongoStore {
14
15
  symbols;
15
16
  edges;
16
17
  scanned;
18
+ links;
17
19
  kind = 'mongodb';
18
20
  cache = { atlasVectorIndex: null };
19
- constructor(client, db, sessions, observations, symbols, edges, scanned) {
21
+ constructor(client, db, sessions, observations, symbols, edges, scanned, links) {
20
22
  this.client = client;
21
23
  this.db = db;
22
24
  this.sessions = sessions;
@@ -24,13 +26,14 @@ export class MongoStore {
24
26
  this.symbols = symbols;
25
27
  this.edges = edges;
26
28
  this.scanned = scanned;
29
+ this.links = links;
27
30
  }
28
31
  static async create(uri) {
29
32
  const mongo = await importMongo();
30
33
  const client = new mongo.MongoClient(uri);
31
34
  await client.connect();
32
35
  const db = client.db(databaseNameFrom(uri) ?? 'claude_memory_db');
33
- return new MongoStore(client, db, db.collection('sessions'), db.collection('observations'), db.collection('symbols'), db.collection('symbol_edges'), db.collection('scanned_files'));
36
+ return new MongoStore(client, db, db.collection('sessions'), db.collection('observations'), db.collection('symbols'), db.collection('symbol_edges'), db.collection('scanned_files'), db.collection('project_links'));
34
37
  }
35
38
  async init() {
36
39
  await this.sessions.createIndex({ project: 1, startedAt: -1 });
@@ -43,6 +46,8 @@ export class MongoStore {
43
46
  await this.edges.createIndex({ project: 1, dstId: 1 });
44
47
  await this.edges.createIndex({ project: 1, file: 1 });
45
48
  await this.scanned.createIndex({ project: 1, path: 1 }, { unique: true });
49
+ await this.links.createIndex({ folder: 1, projectKey: 1 }, { unique: true });
50
+ await this.links.createIndex({ projectKey: 1 });
46
51
  }
47
52
  async close() {
48
53
  await this.client.close();
@@ -51,6 +56,12 @@ export class MongoStore {
51
56
  const res = await this.db.command({ ping: 1 });
52
57
  return res['ok'] === 1;
53
58
  }
59
+ async linkProject(folder, key, now = Date.now()) {
60
+ return projectLinksOps.linkProject(this.links, folder, key, now);
61
+ }
62
+ async projectScope(folder) {
63
+ return projectLinksOps.projectScope(this.links, folder);
64
+ }
54
65
  async upsertSession(session) {
55
66
  return sessionsOps.upsertSession(this.sessions, session);
56
67
  }
@@ -1,7 +1,9 @@
1
1
  import { isWholeScope } from '../adapter.js';
2
2
  import { toDoc, toObservation } from './docs.js';
3
+ import { projectMatch } from './filters.js';
3
4
  import { escapeRegex } from './helpers.js';
4
5
  import { partitionIds } from '../../util/shortid.js';
6
+ import { noProjects } from '../project-scope.js';
5
7
  export async function insertObservations(collection, sessions, symbols, edges, scanned, rows) {
6
8
  if (rows.length === 0)
7
9
  return;
@@ -27,11 +29,12 @@ export async function getObservations(observations, sessions, symbols, edges, sc
27
29
  return docs.map(toObservation);
28
30
  }
29
31
  export async function remove(observations, sessions, symbols, edges, scanned, filter) {
32
+ if (noProjects(filter.project))
33
+ return 0;
30
34
  if (filter.ids?.length === 0)
31
35
  return 0;
32
36
  const query = {};
33
- if (filter.project)
34
- query['project'] = filter.project;
37
+ Object.assign(query, projectMatch(filter.project));
35
38
  if (filter.kind)
36
39
  query['kind'] = filter.kind;
37
40
  if (filter.before !== undefined)
@@ -49,7 +52,7 @@ export async function remove(observations, sessions, symbols, edges, scanned, fi
49
52
  const count = await observations.countDocuments(query);
50
53
  await observations.deleteMany(query);
51
54
  if (isWholeScope(filter)) {
52
- const scope = filter.project ? { project: filter.project } : {};
55
+ const scope = projectMatch(filter.project);
53
56
  await sessions.deleteMany(scope);
54
57
  await symbols.deleteMany(scope);
55
58
  await edges.deleteMany(scope);
@@ -58,9 +61,10 @@ export async function remove(observations, sessions, symbols, edges, scanned, fi
58
61
  return count;
59
62
  }
60
63
  export async function list(observations, sessions, symbols, edges, scanned, filter) {
64
+ if (noProjects(filter.project))
65
+ return [];
61
66
  const query = {};
62
- if (filter.project)
63
- query['project'] = filter.project;
67
+ Object.assign(query, projectMatch(filter.project));
64
68
  if (filter.sessionId)
65
69
  query['sessionId'] = filter.sessionId;
66
70
  if (filter.kind)
@@ -0,0 +1,4 @@
1
+ import type { Collection } from './driver.js';
2
+ import type { ProjectLinkDoc } from './docs.js';
3
+ export declare function linkProject(links: Collection<ProjectLinkDoc>, folder: string, key: string, now: number): Promise<void>;
4
+ export declare function projectScope(links: Collection<ProjectLinkDoc>, folder: string): Promise<string[]>;
@@ -0,0 +1,13 @@
1
+ import { isLinkable, orderScope } from '../project-scope.js';
2
+ const ID_SEPARATOR = '\u0000';
3
+ export async function linkProject(links, folder, key, now) {
4
+ if (!isLinkable(folder, key))
5
+ return;
6
+ await links.updateOne({ _id: `${folder}${ID_SEPARATOR}${key}` }, { $setOnInsert: { folder, projectKey: key, firstSeen: now } }, { upsert: true });
7
+ }
8
+ export async function projectScope(links, folder) {
9
+ const own = await links.find({ folder }).toArray();
10
+ const keys = own.map((doc) => doc.projectKey);
11
+ const shared = keys.length > 0 ? await links.find({ projectKey: { $in: keys } }).toArray() : [];
12
+ return orderScope(folder, [...keys, ...shared.map((doc) => doc.folder)]);
13
+ }
@@ -1,6 +1,9 @@
1
1
  import { toIndexEntry } from './docs.js';
2
2
  import { scopeFilter, visibleFilter } from './filters.js';
3
+ import { noProjects } from '../project-scope.js';
3
4
  export async function searchKeyword(observations, query) {
5
+ if (noProjects(query.project))
6
+ return [];
4
7
  const filter = {
5
8
  $text: { $search: query.text },
6
9
  ...scopeFilter(query),
@@ -1,8 +1,8 @@
1
1
  import type { Collection } from './driver.js';
2
- import type { Session } from '../../types.js';
2
+ import type { ProjectFilter, Session } from '../../types.js';
3
3
  import type { SessionDoc } from './docs.js';
4
4
  export declare function upsertSession(sessions: Collection<SessionDoc>, session: Session): Promise<void>;
5
5
  export declare function getSession(sessions: Collection<SessionDoc>, id: string): Promise<Session | null>;
6
6
  export declare function clearSummary(sessions: Collection<SessionDoc>, id: string): Promise<boolean>;
7
- export declare function recentSessions(sessions: Collection<SessionDoc>, project: string, limit: number): Promise<Session[]>;
7
+ export declare function recentSessions(sessions: Collection<SessionDoc>, project: ProjectFilter, limit: number): Promise<Session[]>;
8
8
  export declare function sessionProjects(sessions: Collection<SessionDoc>): Promise<string[]>;