Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

36.8 KB
Raw Blame
import type { UserMemoryDossierStatus } from '@lobechat/types';
import { and, asc, eq, inArray, isNotNull, isNull, sql } from 'drizzle-orm';

import {
  userMemories,
  userMemoriesActivities,
  userMemoriesContexts,
  userMemoriesDossiers,
  userMemoriesOperations,
  users,
} from '../../schemas';
import type { LobeChatDatabase } from '../../type';
import { notTrashed } from '../../utils/softDelete';

/**
 * Memory consolidation: the per-topic context rows written by extraction are evidence. The
 * consolidation pipeline groups them into contexts (a project direction / customer / company
 * matter) and dossiers (one feature, deal or ongoing sub-task), and turns each evidence row into an
 * activity under its dossier. Nothing is deleted: the legacy row's parent memory is marked
 * `superseded` (hidden from reads) and every write carries the run id, so a run can be reverted.
 */

/**
 * A legacy context's parent memory: its single parent when set (extraction now writes both), else
 * the first id of the legacy list.
 */
const legacyParentJoin = sql`${userMemories.id} = COALESCE(${userMemoriesContexts.userMemoryId}, ${userMemoriesContexts.userMemoryIds} ->> 0)`;

/**
 * Not yet seen by consolidation. Key existence, never `(metadata -> 'x') IS NULL`: an IS [NOT] NULL
 * test on an extracted jsonb value crashes the pg_search planner on bm25-indexed tables
 * (`user_memories*`), see `jsonbNullTest.test.ts`.
 */
const notConsolidated = sql`NOT COALESCE(jsonb_exists(${userMemories.metadata}, 'consolidation'), false)`;

export type ConsolidationRoute = 'drop' | 'evidence' | 'fact' | 'preference';

export interface LegacyContextItem {
  associatedObjects: { name?: string; type?: string }[] | null;
  associatedSubjects: { name?: string; type?: string }[] | null;
  capturedAt: Date;
  description: string | null;
  id: string;
  memoryId: string;
  sourceId: string | null;
  title: string | null;
}

export interface ConsolidatedDossierSummary {
  activityCount: number;
  contextId: string | null;
  id: string;
  lastEvidenceAt: Date | null;
  now: string | null;
  title: string | null;
}

export interface ConsolidatedContextSummary {
  id: string;
  lastEvidenceAt: Date | null;
  now: string | null;
  title: string | null;
}

export interface ConsolidationDossierPlan {
  /** Existing dossier to extend; omitted for a new one. */
  existingDossierId?: string;
  /** Legacy context ids (evidence) filed under this dossier. */
  memberIds: string[];
  now: string;
  title: string;
}

export interface ConsolidationContextPlan {
  /** Embedding of `now`, the context description semantic search ranks by. */
  descriptionVector?: number[] | null;
  dossiers: ConsolidationDossierPlan[];
  /** Existing consolidated context to extend; omitted for a new one. */
  existingContextId?: string;
  nextSteps: string[];
  now: string;
  openQuestions: string[];
  title: string;
}

export interface ConsolidationApplyResult {
  activities: number;
  contextId?: string;
  contextsCreated: number;
  contextsUpdated: number;
  dossierIds: string[];
  dossiersCreated: number;
  dossiersUpdated: number;
}

const DAY_MS = 86_400_000;

/** Derived from the newest evidence, never from what an extractor wrote. */
export const deriveContextStatus = (lastEvidenceAt: Date, now = new Date()) => {
  const gap = (now.getTime() - lastEvidenceAt.getTime()) / DAY_MS;
  if (gap <= 45) return 'active';
  if (gap <= 150) return 'dormant';
  return 'closed';
};

const runTag = (runId: string) => ({ consolidationRunId: runId });

/**
 * Users that have been consolidated at least once, so the nightly run can keep them current.
 * Reads the dossiers table (new and small), not the large contexts table.
 */
export const listConsolidatedUserIds = async (db: LobeChatDatabase): Promise<string[]> => {
  const rows = await db
    .selectDistinct({ userId: userMemoriesDossiers.userId })
    .from(userMemoriesDossiers)
    .innerJoin(users, eq(users.id, userMemoriesDossiers.userId))
    .where(sql`COALESCE(${users.banned}, false) = false`);
  return rows.map((r) => r.userId).filter((id): id is string => !!id);
};

export class UserMemoryConsolidationModel {
  private readonly db: LobeChatDatabase;
  private readonly userId: string;

  constructor(db: LobeChatDatabase, userId: string) {
    this.db = db;
    this.userId = userId;
  }

  /**
   * Legacy per-topic contexts not yet seen by consolidation, oldest first. A legacy context has no
   * single parent (`user_memory_id`); its parent is the first id of `user_memory_ids`.
   */
  listPendingLegacyContexts = async (limit = 5000): Promise<LegacyContextItem[]> => {
    const rows = await this.db
      .select({
        associatedObjects: userMemoriesContexts.associatedObjects,
        associatedSubjects: userMemoriesContexts.associatedSubjects,
        capturedAt: userMemoriesContexts.capturedAt,
        description: userMemoriesContexts.description,
        id: userMemoriesContexts.id,
        memoryId: userMemories.id,
        memoryMetadata: userMemories.metadata,
        title: userMemoriesContexts.title,
      })
      .from(userMemoriesContexts)
      .innerJoin(userMemories, legacyParentJoin)
      .where(
        and(
          eq(userMemoriesContexts.userId, this.userId),
          isNull(userMemoriesContexts.synthesizedAt),
          eq(userMemories.userId, this.userId),
          notTrashed(userMemories.isDeleted),
          isNull(userMemories.state),
          notConsolidated,
        ),
      )
      .orderBy(asc(userMemoriesContexts.capturedAt), asc(userMemoriesContexts.id))
      .limit(limit);

    return rows.map(({ memoryMetadata, ...row }) => ({
      ...row,
      sourceId: typeof memoryMetadata?.sourceId === 'string' ? memoryMetadata.sourceId : null,
    }));
  };

  getLegacyContextsByIds = async (ids: string[]): Promise<LegacyContextItem[]> => {
    if (ids.length === 0) return [];
    const items = await this.db
      .select({
        associatedObjects: userMemoriesContexts.associatedObjects,
        associatedSubjects: userMemoriesContexts.associatedSubjects,
        capturedAt: userMemoriesContexts.capturedAt,
        description: userMemoriesContexts.description,
        id: userMemoriesContexts.id,
        memoryId: userMemories.id,
        memoryMetadata: userMemories.metadata,
        title: userMemoriesContexts.title,
      })
      .from(userMemoriesContexts)
      .innerJoin(userMemories, legacyParentJoin)
      .where(
        and(
          eq(userMemoriesContexts.userId, this.userId),
          inArray(userMemoriesContexts.id, ids),
          // Rows a run already applied or routed are done: a retried step must not file them again.
          notConsolidated,
        ),
      );

    return items.map(({ memoryMetadata, ...row }) => ({
      ...row,
      sourceId: typeof memoryMetadata?.sourceId === 'string' ? memoryMetadata.sourceId : null,
    }));
  };

  /**
   * Contexts this run already filed some of these legacy rows into. A retried synthesis step skips
   * the filed rows, so it needs these to keep extending the same contexts instead of new ones.
   */
  listFiledContextIds = async (runId: string, ids: string[]): Promise<string[]> => {
    if (ids.length === 0) return [];
    const rows = await this.db
      .selectDistinct({ contextId: userMemoriesDossiers.contextId })
      .from(userMemoriesContexts)
      .innerJoin(userMemories, legacyParentJoin)
      .innerJoin(
        userMemoriesActivities,
        sql`${userMemoriesActivities.id} = ${userMemories.metadata} -> 'consolidation' ->> 'activityId'`,
      )
      .innerJoin(
        userMemoriesDossiers,
        eq(userMemoriesDossiers.id, userMemoriesActivities.dossierId),
      )
      .where(
        and(
          eq(userMemoriesContexts.userId, this.userId),
          inArray(userMemoriesContexts.id, ids),
          eq(userMemories.userId, this.userId),
          sql`${userMemories.metadata} -> 'consolidation' ->> 'runId' = ${runId}`,
        ),
      );
    return rows.map((r) => r.contextId).filter((id): id is string => !!id);
  };

  /** Consolidated contexts (they have a single parent) with their dossiers, for the online step. */
  listConsolidated = async (): Promise<{
    contexts: ConsolidatedContextSummary[];
    dossiers: ConsolidatedDossierSummary[];
  }> => {
    const [contexts, dossiers] = await Promise.all([
      this.db
        .select({
          id: userMemoriesContexts.id,
          lastEvidenceAt: userMemoriesContexts.lastEvidenceAt,
          now: userMemoriesContexts.now,
          title: userMemoriesContexts.title,
        })
        .from(userMemoriesContexts)
        .innerJoin(userMemories, eq(userMemories.id, userMemoriesContexts.userMemoryId))
        .where(
          and(
            eq(userMemoriesContexts.userId, this.userId),
            isNotNull(userMemoriesContexts.synthesizedAt),
            notTrashed(userMemories.isDeleted),
          ),
        ),
      this.db
        .select({
          activityCount: userMemoriesDossiers.activityCount,
          contextId: userMemoriesDossiers.contextId,
          id: userMemoriesDossiers.id,
          lastEvidenceAt: userMemoriesDossiers.lastEvidenceAt,
          now: userMemoriesDossiers.now,
          title: userMemoriesDossiers.title,
        })
        .from(userMemoriesDossiers)
        .where(eq(userMemoriesDossiers.userId, this.userId)),
    ]);

    return { contexts, dossiers };
  };

  /** Recent evidence titles per dossier, to show the model what a dossier already holds. */
  listDossierEvidenceTitles = async (dossierIds: string[], perDossier = 3) => {
    if (dossierIds.length === 0) return {} as Record<string, string[]>;
    const rows = await this.db
      .select({ dossierId: userMemoriesActivities.dossierId, title: userMemories.title })
      .from(userMemoriesActivities)
      .innerJoin(userMemories, eq(userMemories.id, userMemoriesActivities.userMemoryId))
      .where(
        and(
          eq(userMemoriesActivities.userId, this.userId),
          inArray(userMemoriesActivities.dossierId, dossierIds),
        ),
      )
      .orderBy(sql`${userMemoriesActivities.capturedAt} DESC`);

    const out: Record<string, string[]> = {};
    for (const row of rows) {
      if (!row.dossierId || !row.title) continue;
      const list = (out[row.dossierId] ??= []);
      if (list.length < perDossier) list.push(row.title);
    }
    return out;
  };

  /**
   * Record a stage decision on the legacy rows (their parent memory's metadata), so later workflow
   * steps read it back from the database instead of carrying it in step results.
   */
  enqueue = async (
    runId: string,
    entries: { id: string; key: string; route: ConsolidationRoute }[],
    /** The run whose queue this continues (see `adoptQueue`); the run itself when it is new. */
    origin: string = runId,
  ) => {
    if (entries.length === 0) return;
    const items = await this.getLegacyContextsByIds(entries.map((e) => e.id));
    const parentOf = new Map(items.map((i) => [i.id, i.memoryId]));
    await this.db.transaction(async (tx) => {
      for (const e of entries) {
        const memoryId = parentOf.get(e.id);
        if (!memoryId) continue;
        await tx
          .update(userMemories)
          .set({
            metadata: sql`coalesce(${userMemories.metadata}, '{}'::jsonb) || ${JSON.stringify({
              consolidationQueue: { key: e.key, origin, route: e.route, runId },
            })}::jsonb`,
          })
          .where(and(eq(userMemories.id, memoryId), eq(userMemories.userId, this.userId)));
      }
    });
  };

  /** Evidence queued by `runId` and not yet filed, with its queue key (domain or context). */
  listQueued = async (runId: string): Promise<{ capturedAt: Date; id: string; key: string }[]> => {
    const rows = await this.db
      .select({
        capturedAt: userMemoriesContexts.capturedAt,
        id: userMemoriesContexts.id,
        key: sql<string>`${userMemories.metadata} -> 'consolidationQueue' ->> 'key'`,
      })
      .from(userMemoriesContexts)
      .innerJoin(userMemories, legacyParentJoin)
      .where(
        and(
          eq(userMemoriesContexts.userId, this.userId),
          isNull(userMemoriesContexts.synthesizedAt),
          eq(userMemories.userId, this.userId),
          isNull(userMemories.state),
          notConsolidated,
          sql`${userMemories.metadata} -> 'consolidationQueue' ->> 'runId' = ${runId}`,
          sql`${userMemories.metadata} -> 'consolidationQueue' ->> 'route' = 'evidence'`,
        ),
      )
      .orderBy(asc(userMemoriesContexts.capturedAt), asc(userMemoriesContexts.id));
    return rows;
  };

  /**
   * The newest queue a stopped run left behind: evidence it classified (or routed) but never
   * synthesized. A new run adopts it instead of starting over. `origin` is the run that first
   * built the queue, kept across resumes; rows queued before `origin` existed fall back to `runId`.
   */
  findResumableQueue = async (): Promise<{
    count: number;
    keys: string[];
    origin: string;
    runId: string;
  } | null> => {
    const queue = sql`${userMemories.metadata} -> 'consolidationQueue'`;
    const [row] = await this.db
      .select({
        count: sql<number>`count(*)::int`,
        keys: sql<string[]>`array_agg(DISTINCT ${queue} ->> 'key')`,
        origin: sql<string>`COALESCE(${queue} ->> 'origin', ${queue} ->> 'runId')`,
        runId: sql<string>`${queue} ->> 'runId'`,
      })
      .from(userMemoriesContexts)
      .innerJoin(userMemories, legacyParentJoin)
      .where(
        and(
          eq(userMemoriesContexts.userId, this.userId),
          isNull(userMemoriesContexts.synthesizedAt),
          eq(userMemories.userId, this.userId),
          isNull(userMemories.state),
          notConsolidated,
          sql`${queue} ->> 'route' = 'evidence'`,
        ),
      )
      .groupBy(
        sql`COALESCE(${queue} ->> 'origin', ${queue} ->> 'runId')`,
        sql`${queue} ->> 'runId'`,
      )
      .orderBy(sql`max(${userMemories.updatedAt}) DESC`)
      .limit(1);
    return row ?? null;
  };

  /** Move a stopped run's unsynthesized queue to `runId`, keeping the queue's origin. */
  adoptQueue = async (fromRunId: string, origin: string, runId: string): Promise<number> => {
    const rows = await this.db
      .update(userMemories)
      .set({
        metadata: sql`jsonb_set(${userMemories.metadata}, '{consolidationQueue}', (${userMemories.metadata} -> 'consolidationQueue') || ${JSON.stringify({ origin, runId })}::jsonb)`,
      })
      .where(
        and(
          eq(userMemories.userId, this.userId),
          isNull(userMemories.state),
          notConsolidated,
          sql`${userMemories.metadata} -> 'consolidationQueue' ->> 'runId' = ${fromRunId}`,
        ),
      )
      .returning({ id: userMemories.id });
    return rows.length;
  };

  /**
   * Every evidence row of a queue lineage (all runs sharing `origin`), by queue key, with the
   * context it was filed into (null while waiting). Resumed runs read the domain mix and the
   * contexts earlier attempts built from it.
   */
  listQueueEvidence = async (
    origin: string,
  ): Promise<{ contextId: string | null; count: number; domain: string | null; key: string }[]> => {
    const queue = sql`${userMemories.metadata} -> 'consolidationQueue'`;
    return this.db
      .select({
        contextId: userMemoriesDossiers.contextId,
        count: sql<number>`count(*)::int`,
        domain: sql<string | null>`${queue} ->> 'domain'`,
        key: sql<string>`${queue} ->> 'key'`,
      })
      .from(userMemories)
      .leftJoin(
        userMemoriesActivities,
        and(
          sql`${userMemoriesActivities.id} = ${userMemories.metadata} -> 'consolidation' ->> 'activityId'`,
          eq(userMemoriesActivities.userId, this.userId),
        ),
      )
      .leftJoin(userMemoriesDossiers, eq(userMemoriesDossiers.id, userMemoriesActivities.dossierId))
      .where(
        and(
          eq(userMemories.userId, this.userId),
          sql`COALESCE(${queue} ->> 'origin', ${queue} ->> 'runId') = ${origin}`,
          sql`${queue} ->> 'route' = 'evidence'`,
        ),
      )
      .groupBy(userMemoriesDossiers.contextId, sql`${queue} ->> 'domain'`, sql`${queue} ->> 'key'`);
  };

  /** Remember merged domains on a queue lineage, so a resumed run does not merge them again. */
  saveQueueDomains = async (origin: string, domains: Record<string, string>): Promise<void> => {
    const queue = sql`${userMemories.metadata} -> 'consolidationQueue'`;
    const map = sql`${JSON.stringify(domains)}::jsonb`;
    await this.db
      .update(userMemories)
      .set({
        metadata: sql`jsonb_set(${userMemories.metadata}, '{consolidationQueue,domain}', ${map} -> (${queue} ->> 'key'))`,
      })
      .where(
        and(
          eq(userMemories.userId, this.userId),
          sql`COALESCE(${queue} ->> 'origin', ${queue} ->> 'runId') = ${origin}`,
          sql`${queue} ->> 'route' = 'evidence'`,
          sql`jsonb_exists(${map}, ${queue} ->> 'key')`,
        ),
      );
  };

  /** Mark legacy rows that consolidation saw but keeps in place (facts, preferences, noise). */
  markRouted = async (runId: string, routes: Record<string, ConsolidationRoute>) => {
    const ids = Object.keys(routes);
    if (ids.length === 0) return;
    const items = await this.getLegacyContextsByIds(ids);
    await this.db.transaction(async (tx) => {
      for (const item of items) {
        await tx
          .update(userMemories)
          .set({
            metadata: sql`coalesce(${userMemories.metadata}, '{}'::jsonb) || ${JSON.stringify({
              consolidation: { route: routes[item.id], runId },
            })}::jsonb`,
          })
          .where(and(eq(userMemories.id, item.memoryId), eq(userMemories.userId, this.userId)));
      }
    });
  };

  /**
   * Write one context plan: create or extend the context and its dossiers, turn every member into
   * an activity under its dossier, and supersede the legacy rows. One transaction per context.
   */
  applyContextPlan = async (
    runId: string,
    plan: ConsolidationContextPlan,
  ): Promise<ConsolidationApplyResult> => {
    const memberIds = plan.dossiers.flatMap((d) => d.memberIds);
    const items = await this.getLegacyContextsByIds(memberIds);
    const byId = new Map(items.map((i) => [i.id, i]));
    const vectors = await this.db
      .select({ id: userMemoriesContexts.id, vector: userMemoriesContexts.descriptionVector })
      .from(userMemoriesContexts)
      .where(
        and(
          eq(userMemoriesContexts.userId, this.userId),
          inArray(userMemoriesContexts.id, memberIds),
        ),
      );
    const vectorById = new Map(vectors.map((v) => [v.id, v.vector]));
    const result: ConsolidationApplyResult = {
      activities: 0,
      dossierIds: [],
      contextsCreated: 0,
      contextsUpdated: 0,
      dossiersCreated: 0,
      dossiersUpdated: 0,
    };

    const placed = plan.dossiers
      .map((d) => ({
        ...d,
        members: d.memberIds.map((id) => byId.get(id)).filter((i): i is LegacyContextItem => !!i),
      }))
      .filter((d) => d.members.length > 0);
    if (placed.length === 0) return result;

    const allDates = placed.flatMap((d) => d.members.map((m) => m.capturedAt.getTime()));
    const firstSeen = new Date(Math.min(...allDates));
    const lastEvidence = new Date(Math.max(...allDates));
    const now = new Date();

    await this.db.transaction(async (tx) => {
      const op = (
        values: Omit<
          typeof userMemoriesOperations.$inferInsert,
          'actor' | 'gate' | 'state' | 'userId'
        >,
      ) =>
        tx.insert(userMemoriesOperations).values({
          actor: 'pipeline',
          gate: 'auto',
          state: 'applied',
          userId: this.userId,
          ...values,
          metadata: { ...values.metadata, ...runTag(runId) },
        });

      // ----- context -----
      let contextId = plan.existingContextId;
      if (contextId) {
        const [before] = await tx
          .select({
            currentStatus: userMemoriesContexts.currentStatus,
            description: userMemoriesContexts.description,
            descriptionVector: userMemoriesContexts.descriptionVector,
            firstSeenAt: userMemoriesContexts.firstSeenAt,
            lastEvidenceAt: userMemoriesContexts.lastEvidenceAt,
            nextSteps: userMemoriesContexts.nextSteps,
            now: userMemoriesContexts.now,
            openQuestions: userMemoriesContexts.openQuestions,
            synthesizedAt: userMemoriesContexts.synthesizedAt,
            userMemoryId: userMemoriesContexts.userMemoryId,
            parentSummary: sql<
              string | null
            >`(SELECT ${userMemories.summary} FROM ${userMemories} WHERE ${userMemories.id} = ${userMemoriesContexts.userMemoryId})`,
          })
          .from(userMemoriesContexts)
          .where(
            and(
              eq(userMemoriesContexts.id, contextId),
              eq(userMemoriesContexts.userId, this.userId),
            ),
          );
        if (!before) {
          contextId = undefined;
        } else {
          // NOTICE: A plan only describes the evidence it was given. When all of it is older than
          // what the context already holds (a later chunk of an unsorted domain, a late-extracted
          // memory), its `now` is a past state, so the evidence is filed but the current state stays.
          const stale = !!before.lastEvidenceAt && before.lastEvidenceAt > lastEvidence;
          const last = stale ? before.lastEvidenceAt! : lastEvidence;
          const state = stale
            ? {
                nextSteps: before.nextSteps,
                now: before.now,
                openQuestions: before.openQuestions,
              }
            : { nextSteps: plan.nextSteps, now: plan.now, openQuestions: plan.openQuestions };
          await tx
            .update(userMemoriesContexts)
            .set({
              currentStatus: deriveContextStatus(last, now),
              lastEvidenceAt: last,
              updatedAt: now,
              ...(stale
                ? {}
                : {
                    description: plan.now,
                    // Without a fresh embedding, the old vector still ranks better than none.
                    ...(plan.descriptionVector
                      ? { descriptionVector: plan.descriptionVector }
                      : {}),
                    nextSteps: plan.nextSteps,
                    now: plan.now,
                    openQuestions: plan.openQuestions,
                    synthesizedAt: now,
                  }),
            })
            .where(eq(userMemoriesContexts.id, contextId));
          // Unified search reads the parent memory, so its summary follows the description.
          if (before.userMemoryId && !stale)
            await tx
              .update(userMemories)
              .set({ summary: plan.now, updatedAt: now })
              .where(
                and(eq(userMemories.id, before.userMemoryId), eq(userMemories.userId, this.userId)),
              );
          await op({
            after: state,
            before,
            op: 'status',
            targets: { context: contextId },
          });
          result.contextsUpdated += 1;
        }
      }
      if (!contextId) {
        const [memory] = await tx
          .insert(userMemories)
          .values({
            capturedAt: now,
            lastAccessedAt: now,
            memoryCategory: 'context',
            memoryLayer: 'context',
            memoryType: 'context',
            metadata: runTag(runId),
            state: 'active',
            summary: plan.now,
            title: plan.title.slice(0, 255),
            userId: this.userId,
          })
          .returning({ id: userMemories.id });
        const [created] = await tx
          .insert(userMemoriesContexts)
          .values({
            capturedAt: now,
            currentStatus: deriveContextStatus(lastEvidence, now),
            description: plan.now,
            descriptionVector: plan.descriptionVector ?? null,
            firstSeenAt: firstSeen,
            lastEvidenceAt: lastEvidence,
            metadata: runTag(runId),
            nextSteps: plan.nextSteps,
            now: plan.now,
            openQuestions: plan.openQuestions,
            synthesizedAt: now,
            title: plan.title,
            userId: this.userId,
            userMemoryId: memory.id,
            // Existing context readers still join parents through this list.
            userMemoryIds: [memory.id],
          })
          .returning({ id: userMemoriesContexts.id });
        contextId = created.id;
        await op({ after: { title: plan.title }, op: 'create', targets: { context: contextId } });
        result.contextsCreated += 1;
      }

      // ----- dossiers + evidence -----
      const updatedDossiers = new Set<string>();
      for (const d of placed) {
        const dates = d.members.map((m) => m.capturedAt.getTime());
        const dFirst = new Date(Math.min(...dates));
        const dLast = new Date(Math.max(...dates));
        let dossierId = d.existingDossierId;
        if (dossierId) {
          const [existing] = await tx
            .select({
              activityCount: userMemoriesDossiers.activityCount,
              lastEvidenceAt: userMemoriesDossiers.lastEvidenceAt,
              now: userMemoriesDossiers.now,
              status: userMemoriesDossiers.status,
              summary: userMemoriesDossiers.summary,
              userMemoryId: userMemoriesDossiers.userMemoryId,
              parentSummary: sql<
                string | null
              >`(SELECT ${userMemories.summary} FROM ${userMemories} WHERE ${userMemories.id} = ${userMemoriesDossiers.userMemoryId})`,
            })
            .from(userMemoriesDossiers)
            .where(
              and(
                eq(userMemoriesDossiers.id, dossierId),
                eq(userMemoriesDossiers.userId, this.userId),
              ),
            );
          if (!existing) dossierId = undefined;
          else {
            const logBefore = !updatedDossiers.has(dossierId);
            updatedDossiers.add(dossierId);
            // Same rule as the context above: older evidence is filed without rewriting `now`.
            const stale = !!existing.lastEvidenceAt && existing.lastEvidenceAt > dLast;
            const last = stale ? existing.lastEvidenceAt! : dLast;
            await tx
              .update(userMemoriesDossiers)
              .set({
                activityCount: existing.activityCount + d.members.length,
                lastEvidenceAt: last,
                status: deriveContextStatus(last, now),
                updatedAt: now,
                ...(stale ? {} : { now: d.now, summary: d.now }),
              })
              .where(eq(userMemoriesDossiers.id, dossierId));
            if (existing.userMemoryId && !stale)
              await tx
                .update(userMemories)
                .set({ summary: d.now, updatedAt: now })
                .where(
                  and(
                    eq(userMemories.id, existing.userMemoryId),
                    eq(userMemories.userId, this.userId),
                  ),
                );
            // A second group placed on the same dossier would log an intermediate before-image.
            if (logBefore) {
              await op({
                after: { now: stale ? existing.now : d.now },
                before: existing,
                op: 'status',
                targets: { dossier: dossierId },
              });
              result.dossiersUpdated += 1;
            }
          }
        }
        if (!dossierId) {
          const [memory] = await tx
            .insert(userMemories)
            .values({
              capturedAt: now,
              lastAccessedAt: now,
              memoryCategory: 'context',
              memoryLayer: 'dossier',
              memoryType: 'dossier',
              metadata: runTag(runId),
              state: 'active',
              summary: d.now,
              title: d.title.slice(0, 255),
              userId: this.userId,
            })
            .returning({ id: userMemories.id });
          const [created] = await tx
            .insert(userMemoriesDossiers)
            .values({
              activityCount: d.members.length,
              contextId,
              firstSeenAt: dFirst,
              lastEvidenceAt: dLast,
              metadata: runTag(runId),
              now: d.now,
              status: deriveContextStatus(dLast, now),
              summary: d.now,
              title: d.title,
              userId: this.userId,
              userMemoryId: memory.id,
            })
            .returning({ id: userMemoriesDossiers.id });
          dossierId = created.id;
          await op({
            after: { title: d.title },
            op: 'create',
            targets: { context: contextId, dossier: dossierId },
          });
          result.dossiersCreated += 1;
        }

        result.dossierIds.push(dossierId);
        const activityIds: string[] = [];
        for (const m of d.members) {
          const meta = {
            ...runTag(runId),
            legacyContextId: m.id,
            legacyMemoryId: m.memoryId,
            ...(m.sourceId ? { sourceId: m.sourceId } : {}),
          };
          const [memory] = await tx
            .insert(userMemories)
            .values({
              capturedAt: m.capturedAt,
              details: m.description,
              lastAccessedAt: now,
              memoryCategory: 'activity',
              memoryLayer: 'activity',
              memoryType: 'activity',
              metadata: meta,
              state: 'active',
              summary: m.description?.slice(0, 1000) ?? null,
              title: (m.title ?? '').slice(0, 255) || null,
              userId: this.userId,
            })
            .returning({ id: userMemories.id });
          const [activity] = await tx
            .insert(userMemoriesActivities)
            .values({
              associatedObjects: (m.associatedObjects ?? []) as never,
              associatedSubjects: (m.associatedSubjects ?? []) as never,
              capturedAt: m.capturedAt,
              dossierId,
              injectable: true,
              metadata: meta,
              narrative: m.description,
              narrativeVector: vectorById.get(m.id) ?? null,
              status: 'completed',
              type: 'context_evidence',
              userId: this.userId,
              userMemoryId: memory.id,
            })
            .returning({ id: userMemoriesActivities.id });
          activityIds.push(activity.id);
          await tx
            .update(userMemories)
            .set({
              metadata: sql`coalesce(${userMemories.metadata}, '{}'::jsonb) || ${JSON.stringify({
                consolidation: { activityId: activity.id, route: 'evidence', runId },
              })}::jsonb`,
              state: 'superseded',
              updatedAt: now,
            })
            .where(and(eq(userMemories.id, m.memoryId), eq(userMemories.userId, this.userId)));
          result.activities += 1;
        }
        await op({ op: 'attach', targets: { activities: activityIds, dossier: dossierId } });
      }
      result.contextId = contextId;
    });

    return result;
  };

  /**
   * Undo a run: delete the memories it created (children cascade) and bring the legacy rows back.
   * In-place updates of pre-existing contexts / dossiers are restored from the operation log.
   */
  revertRun = async (
    runId: string,
  ): Promise<{ blockedBy?: string[]; deleted: number; restored: number }> => {
    return this.db.transaction(async (tx) => {
      // A later run that extended what this run created or updated depends on it: reverting this
      // one alone would cascade-delete or overwrite that run's work. Revert the later runs first.
      const dependents = await tx.execute<{ run_id: string }>(sql`
        WITH mine AS (
          SELECT targets, created_at FROM ${userMemoriesOperations}
          WHERE user_id = ${this.userId} AND state = 'applied'
            AND metadata ->> 'consolidationRunId' = ${runId}
        ), touched AS (
          SELECT targets ->> 'context' AS id FROM mine WHERE jsonb_exists(targets, 'context')
          UNION SELECT targets ->> 'dossier' FROM mine WHERE jsonb_exists(targets, 'dossier')
        )
        SELECT DISTINCT o.metadata ->> 'consolidationRunId' AS run_id
        FROM ${userMemoriesOperations} o
        WHERE o.user_id = ${this.userId} AND o.state = 'applied'
          AND COALESCE(o.metadata ->> 'consolidationRunId', '') <> ${runId}
          AND jsonb_exists(o.metadata, 'consolidationRunId')
          AND o.created_at > (SELECT min(created_at) FROM mine)
          AND (o.targets ->> 'context' IN (SELECT id FROM touched)
            OR o.targets ->> 'dossier' IN (SELECT id FROM touched))
      `);
      const blockedBy = dependents.rows.map((r) => r.run_id).filter(Boolean);
      if (blockedBy.length > 0) return { blockedBy, deleted: 0, restored: 0 };

      const updates = await tx
        .select()
        .from(userMemoriesOperations)
        .where(
          and(
            eq(userMemoriesOperations.userId, this.userId),
            eq(userMemoriesOperations.op, 'status'),
            eq(userMemoriesOperations.state, 'applied'),
            sql`${userMemoriesOperations.metadata} ->> 'consolidationRunId' = ${runId}`,
          ),
        )
        .orderBy(asc(userMemoriesOperations.createdAt));
      // A run can update one context / dossier in several chunks (one transaction each, so
      // createdAt increases). Only the earliest before-image is the state before the run.
      const restoredTargets = new Set<string>();
      for (const u of updates) {
        const before = (u.before ?? {}) as Record<string, unknown>;
        const target = u.targets as Record<string, string>;
        const targetKey = target.dossier
          ? `dossier:${target.dossier}`
          : `context:${target.context}`;
        if (restoredTargets.has(targetKey)) continue;
        restoredTargets.add(targetKey);
        // Before-images from older runs have no parent fields; leave those parents as they are.
        if (typeof before.userMemoryId === 'string' && 'parentSummary' in before)
          await tx
            .update(userMemories)
            .set({ summary: (before.parentSummary as string) ?? null })
            .where(
              and(eq(userMemories.id, before.userMemoryId), eq(userMemories.userId, this.userId)),
            );
        if (target.context && !target.dossier) {
          await tx
            .update(userMemoriesContexts)
            .set({
              currentStatus: (before.currentStatus as string) ?? null,
              description: (before.description as string) ?? null,
              descriptionVector: (before.descriptionVector as number[]) ?? null,
              lastEvidenceAt: before.lastEvidenceAt
                ? new Date(before.lastEvidenceAt as string)
                : null,
              nextSteps: (before.nextSteps as string[]) ?? null,
              now: (before.now as string) ?? null,
              openQuestions: (before.openQuestions as string[]) ?? null,
              synthesizedAt: before.synthesizedAt ? new Date(before.synthesizedAt as string) : null,
            })
            .where(
              and(
                eq(userMemoriesContexts.id, target.context),
                eq(userMemoriesContexts.userId, this.userId),
              ),
            );
        } else if (target.dossier) {
          await tx
            .update(userMemoriesDossiers)
            .set({
              activityCount: Number(before.activityCount ?? 0),
              lastEvidenceAt: before.lastEvidenceAt
                ? new Date(before.lastEvidenceAt as string)
                : null,
              now: (before.now as string) ?? null,
              status: (before.status as UserMemoryDossierStatus | undefined) ?? 'active',
              summary: (before.summary as string) ?? null,
            })
            .where(
              and(
                eq(userMemoriesDossiers.id, target.dossier),
                eq(userMemoriesDossiers.userId, this.userId),
              ),
            );
        }
      }
      const deleted = await tx
        .delete(userMemories)
        .where(
          and(
            eq(userMemories.userId, this.userId),
            sql`${userMemories.metadata} ->> 'consolidationRunId' = ${runId}`,
          ),
        )
        .returning({ id: userMemories.id });
      const restored = await tx
        .update(userMemories)
        .set({
          metadata: sql`${userMemories.metadata} - 'consolidation' - 'consolidationQueue'`,
          state: null,
        })
        .where(
          and(
            eq(userMemories.userId, this.userId),
            sql`(${userMemories.metadata} -> 'consolidation' ->> 'runId' = ${runId} OR ${userMemories.metadata} -> 'consolidationQueue' ->> 'runId' = ${runId})`,
          ),
        )
        .returning({ id: userMemories.id });
      await tx
        .update(userMemoriesOperations)
        .set({ resolvedAt: new Date(), state: 'reverted' })
        .where(
          and(
            eq(userMemoriesOperations.userId, this.userId),
            sql`${userMemoriesOperations.metadata} ->> 'consolidationRunId' = ${runId}`,
          ),
        );
      return { deleted: deleted.length, restored: restored.length };
    });
  };
}