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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/**
 * Long-term cross-session memory service.
 *
 * Owns CRUD against `user_memory_facts`, normalisation/dedupe, the
 * per-turn ranker (salience × recency × keyword overlap, no embeddings),
 * a cheap token estimator, and the hard caps that apply regardless of
 * what the user's settings say:
 *
 *   - at most 100 stored facts per user (eviction = lowest salience,
 *     then oldest, on insert overflow);
 *   - at most ~2000 tokens injected into the system prompt per turn;
 *   - at most 5 newly auto-extracted facts persisted per turn (manual
 *     creates from the user are not counted against this).
 */
import { and, desc, eq, sql } from "drizzle-orm";
import {
  db,
  userMemoryFacts,
  userSettings,
  type UserMemoryFact,
  type UserSettingsBlob,
} from "@workspace/db";
import { newId } from "./ids";
import { defaultSettings } from "./defaults";
import {
  MEMORY_HARD_CAPS,
  VALID_KINDS,
  contentHash,
  estimateTokens,
  normalize,
  rankAndPackFacts,
  type MemoryConfig,
  type MemoryInjection,
  type PublicMemoryFact,
} from "./memory-core";

// Re-export the pure helpers + types so existing call sites keep working.
export {
  MEMORY_HARD_CAPS,
  VALID_KINDS,
  contentHash,
  estimateTokens,
  normalize,
  rankAndPackFacts,
  type MemoryConfig,
  type MemoryInjection,
  type PublicMemoryFact,
};

function clampKind(kind: string | undefined | null): string {
  if (!kind) return "fact";
  return VALID_KINDS.has(kind) ? kind : "fact";
}

function clampUnit(n: unknown, dflt: number): number {
  if (typeof n !== "number" || !Number.isFinite(n)) return dflt;
  if (n < 0) return 0;
  if (n > 1) return 1;
  return n;
}

function clampContent(s: string): string {
  const t = String(s || "").trim();
  return t.length > MEMORY_HARD_CAPS.contentChars
    ? t.slice(0, MEMORY_HARD_CAPS.contentChars)
    : t;
}

function clampMaxFacts(n: unknown): number {
  if (typeof n !== "number" || !Number.isFinite(n)) return MEMORY_HARD_CAPS.facts;
  return Math.max(1, Math.min(MEMORY_HARD_CAPS.facts, Math.floor(n)));
}

function clampMaxTokens(n: unknown): number {
  if (typeof n !== "number" || !Number.isFinite(n)) {
    return MEMORY_HARD_CAPS.tokensPerTurn;
  }
  return Math.max(0, Math.min(MEMORY_HARD_CAPS.tokensPerTurn, Math.floor(n)));
}

function toPublic(row: UserMemoryFact): PublicMemoryFact {
  return {
    id: row.id,
    kind: row.kind,
    content: row.content,
    confidence: row.confidence,
    salience: row.salience,
    source: (row.source as "auto" | "manual") || "auto",
    source_message_id: row.sourceMessageId,
    conversation_id: row.conversationId,
    use_count: row.useCount,
    archived: !!row.archived,
    created_at: row.createdAt.toISOString(),
    updated_at: row.updatedAt.toISOString(),
    last_used_at: row.lastUsedAt ? row.lastUsedAt.toISOString() : null,
  };
}

// ---------- settings

export async function getMemoryConfig(userId: string): Promise<MemoryConfig> {
  const rows = await db
    .select()
    .from(userSettings)
    .where(eq(userSettings.userId, userId))
    .limit(1);
  const blob = (rows[0]?.data ?? defaultSettings()) as UserSettingsBlob;
  const m = blob.long_term_memory ?? defaultSettings().long_term_memory;
  return {
    enabled: !!m.enabled,
    auto_extract: m.auto_extract !== false,
    max_facts: clampMaxFacts(m.max_facts),
    max_tokens_per_turn: clampMaxTokens(m.max_tokens_per_turn),
  };
}

// ---------- CRUD

export async function listFacts(
  userId: string,
  opts: { includeArchived?: boolean } = {},
): Promise<PublicMemoryFact[]> {
  const where = opts.includeArchived
    ? eq(userMemoryFacts.userId, userId)
    : and(
        eq(userMemoryFacts.userId, userId),
        eq(userMemoryFacts.archived, 0),
      );
  const rows = await db
    .select()
    .from(userMemoryFacts)
    .where(where)
    .orderBy(desc(userMemoryFacts.salience), desc(userMemoryFacts.updatedAt));
  return rows.map(toPublic);
}

export async function countFacts(
  userId: string,
  opts: { includeArchived?: boolean } = {},
): Promise<number> {
  const where = opts.includeArchived
    ? eq(userMemoryFacts.userId, userId)
    : and(
        eq(userMemoryFacts.userId, userId),
        eq(userMemoryFacts.archived, 0),
      );
  const rows = await db
    .select({ n: sql<number>`count(*)::int` })
    .from(userMemoryFacts)
    .where(where);
  return rows[0]?.n ?? 0;
}

/** Soft-archive a fact: hidden from listing/ranking, kept on disk. */
export async function archiveFact(
  userId: string,
  id: string,
): Promise<PublicMemoryFact | null> {
  await db
    .update(userMemoryFacts)
    .set({ archived: 1, updatedAt: new Date() })
    .where(
      and(eq(userMemoryFacts.id, id), eq(userMemoryFacts.userId, userId)),
    );
  const rows = await db
    .select()
    .from(userMemoryFacts)
    .where(
      and(eq(userMemoryFacts.id, id), eq(userMemoryFacts.userId, userId)),
    )
    .limit(1);
  return rows[0] ? toPublic(rows[0]) : null;
}

/** Restore an archived fact. */
export async function unarchiveFact(
  userId: string,
  id: string,
): Promise<PublicMemoryFact | null> {
  await db
    .update(userMemoryFacts)
    .set({ archived: 0, updatedAt: new Date() })
    .where(
      and(eq(userMemoryFacts.id, id), eq(userMemoryFacts.userId, userId)),
    );
  const rows = await db
    .select()
    .from(userMemoryFacts)
    .where(
      and(eq(userMemoryFacts.id, id), eq(userMemoryFacts.userId, userId)),
    )
    .limit(1);
  return rows[0] ? toPublic(rows[0]) : null;
}

interface CreateArgs {
  kind?: string;
  content: string;
  confidence?: number;
  salience?: number;
  source?: "auto" | "manual";
  sourceMessageId?: string | null;
  conversationId?: string | null;
}

/**
 * Insert a new fact, deduping by `normalized`. Returns the (possibly
 * pre-existing) fact along with a flag indicating whether a brand-new
 * row was created vs. an existing one was bumped. Enforces the per-user
 * `max_facts` cap by evicting the lowest-salience, then oldest, fact.
 */
export async function createOrBumpFact(
  userId: string,
  args: CreateArgs,
  cfg: MemoryConfig,
): Promise<{ fact: PublicMemoryFact; created: boolean }> {
  const content = clampContent(args.content);
  if (!content) {
    throw new Error("content is required");
  }
  const normalized = normalize(content);
  if (!normalized) {
    throw new Error("content is empty after normalisation");
  }
  const hash = contentHash(content);
  // Dedupe by stable hash first (cheap, index-friendly), fall back to
  // normalized text for legacy rows without a hash. Re-emitting an
  // archived fact also un-archives it.
  const existing = await db
    .select()
    .from(userMemoryFacts)
    .where(
      and(
        eq(userMemoryFacts.userId, userId),
        sql`(${userMemoryFacts.contentHash} = ${hash} OR (${userMemoryFacts.contentHash} = '' AND ${userMemoryFacts.normalized} = ${normalized}))`,
      ),
    )
    .limit(1);
  if (existing[0]) {
    const row = existing[0];
    const newSalience = Math.min(
      1,
      Math.max(row.salience, clampUnit(args.salience, row.salience) + 0.05),
    );
    const newConfidence = Math.max(
      row.confidence,
      clampUnit(args.confidence, row.confidence),
    );
    await db
      .update(userMemoryFacts)
      .set({
        salience: newSalience,
        confidence: newConfidence,
        useCount: row.useCount + 1,
        contentHash: hash,
        archived: 0,
        updatedAt: new Date(),
      })
      .where(eq(userMemoryFacts.id, row.id));
    const refreshed = (await db
      .select()
      .from(userMemoryFacts)
      .where(eq(userMemoryFacts.id, row.id))
      .limit(1))[0]!;
    return { fact: toPublic(refreshed), created: false };
  }

  // Eviction: enforce max_facts cap before insert. Archive the lowest
  // salience / oldest active rows rather than hard-deleting them.
  const cap = Math.min(cfg.max_facts, MEMORY_HARD_CAPS.facts);
  const total = await countFacts(userId);
  if (total >= cap) {
    const victims = await db
      .select({ id: userMemoryFacts.id })
      .from(userMemoryFacts)
      .where(
        and(
          eq(userMemoryFacts.userId, userId),
          eq(userMemoryFacts.archived, 0),
        ),
      )
      .orderBy(userMemoryFacts.salience, userMemoryFacts.createdAt)
      .limit(total - cap + 1);
    if (victims.length) {
      const ids = victims.map((v) => v.id);
      for (const id of ids) {
        await db
          .update(userMemoryFacts)
          .set({ archived: 1, updatedAt: new Date() })
          .where(eq(userMemoryFacts.id, id));
      }
    }
  }
  const id = newId("memf");
  const row = {
    id,
    userId,
    kind: clampKind(args.kind),
    content,
    normalized,
    contentHash: hash,
    archived: 0,
    confidence: clampUnit(args.confidence, 0.7),
    salience: clampUnit(args.salience, args.source === "manual" ? 0.9 : 0.6),
    source: args.source === "manual" ? "manual" : "auto",
    sourceMessageId: args.sourceMessageId ?? null,
    conversationId: args.conversationId ?? null,
    useCount: 0,
  };
  await db.insert(userMemoryFacts).values(row);
  const inserted = (await db
    .select()
    .from(userMemoryFacts)
    .where(eq(userMemoryFacts.id, id))
    .limit(1))[0]!;
  return { fact: toPublic(inserted), created: true };
}

export async function updateFact(
  userId: string,
  id: string,
  patch: { kind?: string; content?: string; salience?: number },
): Promise<PublicMemoryFact | null> {
  const rows = await db
    .select()
    .from(userMemoryFacts)
    .where(
      and(eq(userMemoryFacts.id, id), eq(userMemoryFacts.userId, userId)),
    )
    .limit(1);
  if (!rows[0]) return null;
  const existing = rows[0];
  const set: Partial<typeof userMemoryFacts.$inferInsert> & {
    updatedAt: Date;
  } = { updatedAt: new Date() };
  if (typeof patch.kind === "string") set.kind = clampKind(patch.kind);
  if (typeof patch.content === "string") {
    const c = clampContent(patch.content);
    if (!c) throw new Error("content is required");
    set.content = c;
    set.normalized = normalize(c);
    set.contentHash = contentHash(c);
  }
  if (typeof patch.salience === "number") {
    set.salience = clampUnit(patch.salience, existing.salience);
  }
  await db
    .update(userMemoryFacts)
    .set(set)
    .where(eq(userMemoryFacts.id, id));
  const refreshed = (await db
    .select()
    .from(userMemoryFacts)
    .where(eq(userMemoryFacts.id, id))
    .limit(1))[0]!;
  return toPublic(refreshed);
}

export async function deleteFact(
  userId: string,
  id: string,
): Promise<boolean> {
  const res = await db
    .delete(userMemoryFacts)
    .where(
      and(eq(userMemoryFacts.id, id), eq(userMemoryFacts.userId, userId)),
    );
  // Drizzle `delete` returns driver-specific shape; treat any throw as a
  // failure and otherwise assume success.
  return !!res;
}

export async function clearFacts(userId: string): Promise<number> {
  const before = await countFacts(userId);
  await db.delete(userMemoryFacts).where(eq(userMemoryFacts.userId, userId));
  return before;
}

// ---------- ranking + injection
//
// The pure ranker, prompt fragments, tokenizer and stopword list now
// live in `./memory-core.ts` so they can be unit-tested without
// pulling in `@workspace/db`. The async wrapper below loads the user's
// facts and delegates to `rankAndPackFacts`.

export async function rankFactsForTurn(
  userId: string,
  currentText: string,
  cfg: MemoryConfig,
): Promise<MemoryInjection> {
  if (!cfg.enabled) {
    return { facts: [], fact_ids: [], injected_tokens: 0, text: null };
  }
  const all = await listFacts(userId);
  return rankAndPackFacts(all, currentText, cfg);
}

export async function touchFacts(
  userId: string,
  factIds: string[],
): Promise<void> {
  if (!factIds.length) return;
  const now = new Date();
  for (const id of factIds) {
    await db
      .update(userMemoryFacts)
      .set({
        lastUsedAt: now,
        useCount: sql`${userMemoryFacts.useCount} + 1`,
      })
      .where(
        and(
          eq(userMemoryFacts.id, id),
          eq(userMemoryFacts.userId, userId),
        ),
      );
  }
}