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.

♻️ refactor(database): split the 1522-line expertise model into cohesive modules (#21134)

Arvin Xu68b13058fc24
diff --git a/packages/database/src/models/__tests__/expertise.characterization.test.ts b/packages/database/src/models/__tests__/expertise.characterization.test.ts
new file mode 100644
index 00000000000..402f48b5392
--- /dev/null
+++ b/packages/database/src/models/__tests__/expertise.characterization.test.ts
@@ -0,0 +1,248 @@
+// @vitest-environment node
+import { eq } from 'drizzle-orm';
+import { afterEach, beforeEach, describe, expect, it } from 'vitest';
+
+import { getTestDB } from '../../core/getTestDB';
+import {
+ agents,
+ expertiseBindings,
+ expertiseDomains,
+ expertiseHits,
+ expertiseLessonRevisions,
+ expertiseLessons,
+ expertiseRuns,
+ projects,
+ users,
+} from '../../schemas';
+import type { LobeChatDatabase } from '../../type';
+import { ExpertiseModel } from '../expertise';
+
+/**
+ * Pins the public methods of `ExpertiseModel` that `expertise.test.ts` does not call directly,
+ * so moving their bodies into sibling modules cannot silently rewire one to another.
+ */
+
+const serverDB: LobeChatDatabase = await getTestDB();
+const userId = 'expertise-characterization-user';
+const foreignUserId = 'expertise-characterization-foreign';
+const runA = '0b6f2a1e-6f0c-4c43-8a54-6c0b2d9f6a01';
+const runB = '0b6f2a1e-6f0c-4c43-8a54-6c0b2d9f6a02';
+const lockForeignLesson = '0b6f2a1e-6f0c-4c43-8a54-6c0b2d9f6b01';
+const lockLesson = '0b6f2a1e-6f0c-4c43-8a54-6c0b2d9f6b02';
+const materialLesson = '0b6f2a1e-6f0c-4c43-8a54-6c0b2d9f6b03';
+const missingLesson = '0b6f2a1e-6f0c-4c43-8a54-6c0b2d9f6b04';
+const retireForeignLesson = '0b6f2a1e-6f0c-4c43-8a54-6c0b2d9f6b05';
+const retireLesson = '0b6f2a1e-6f0c-4c43-8a54-6c0b2d9f6b06';
+const reviseForeignLesson = '0b6f2a1e-6f0c-4c43-8a54-6c0b2d9f6b07';
+const reviseLesson = '0b6f2a1e-6f0c-4c43-8a54-6c0b2d9f6b08';
+const runsLesson = '0b6f2a1e-6f0c-4c43-8a54-6c0b2d9f6b09';
+
+const seedDomain = async (id: string, owner = userId, anchored = true) => {
+ await serverDB.insert(expertiseDomains).values({
+ anchorChosenAt: anchored ? new Date() : null,
+ domainFilter: id,
+ id,
+ slug: id,
+ title: id,
+ userId: owner,
+ });
+};
+
+const seedLesson = async (id: string, domainId: string, code = 'P-01') => {
+ await serverDB.insert(expertiseLessons).values({
+ code,
+ domainId,
+ id,
+ polarity: 'rule',
+ sections: [
+ { body: 'rule body', key: 'rule' },
+ { body: 'old limits', key: 'limits' },
+ ],
+ title: `lesson ${id}`,
+ });
+};
+
+const seedRun = async (id: string, domainId: string, runIndex: number) => {
+ await serverDB.insert(expertiseRuns).values({
+ actorId: 'agent-1',
+ actorType: 'agent',
+ domainId,
+ id,
+ runIndex,
+ subjectId: `subject-${runIndex}`,
+ subjectType: 'standalone',
+ userId,
+ });
+};
+
+describe('ExpertiseModel (characterization)', () => {
+ beforeEach(async () => {
+ await serverDB.delete(users);
+ await serverDB.insert(users).values([{ id: userId }, { id: foreignUserId }]);
+ });
+
+ afterEach(async () => {
+ await serverDB.delete(users);
+ });
+
+ it('finds only anchored domains in scope', async () => {
+ await seedDomain('char-anchored');
+ await seedDomain('char-unanchored', userId, false);
+ await seedDomain('char-foreign', foreignUserId);
+ const model = new ExpertiseModel(serverDB, userId);
+
+ expect((await model.findDomain('char-anchored'))?.id).toBe('char-anchored');
+ await expect(model.findDomain('char-unanchored')).resolves.toBeUndefined();
+ await expect(model.findDomain('char-foreign')).resolves.toBeUndefined();
+ });
+
+ it('resolves owner and project mounts', async () => {
+ await serverDB.insert(agents).values({ id: 'char-coordinator', userId });
+ await serverDB.insert(projects).values({
+ coordinatorAgentId: 'char-coordinator',
+ id: 'char-project',
+ identifier: 'CHAR',
+ name: 'Project',
+ userId,
+ });
+ await seedDomain('char-owner-domain');
+ await seedDomain('char-project-domain');
+ await serverDB.insert(expertiseBindings).values([
+ { boundUserId: userId, domainId: 'char-owner-domain', sortOrder: 0 },
+ { domainId: 'char-project-domain', projectId: 'char-project', sortOrder: 1 },
+ ]);
+ const model = new ExpertiseModel(serverDB, userId);
+
+ const owner = await model.listDomainsForOwner();
+ const project = await model.listDomainsForProject('char-project');
+
+ expect(owner.map(({ domain }) => domain.id)).toEqual(['char-owner-domain']);
+ expect(project.map(({ domain }) => domain.id)).toEqual([
+ 'char-owner-domain',
+ 'char-project-domain',
+ ]);
+ });
+
+ it('compares the reach of two domains', async () => {
+ await serverDB.insert(agents).values({ id: 'char-agent', userId });
+ await seedDomain('char-mine');
+ await seedDomain('char-agent-domain');
+ await seedDomain('char-agent-domain-2');
+ await serverDB.insert(expertiseBindings).values([
+ { boundUserId: userId, domainId: 'char-mine' },
+ { agentId: 'char-agent', domainId: 'char-agent-domain' },
+ { agentId: 'char-agent', domainId: 'char-agent-domain-2' },
+ ]);
+ const model = new ExpertiseModel(serverDB, userId);
+
+ await expect(model.sameReach('char-mine', 'char-mine')).resolves.toBe(true);
+ await expect(model.sameReach('char-mine', 'char-agent-domain')).resolves.toBe(false);
+ await expect(model.sameReach('char-agent-domain', 'char-agent-domain-2')).resolves.toBe(true);
+ });
+
+ it('revises a lesson into a new limits section and records the revision', async () => {
+ await seedDomain('char-revise');
+ await seedLesson(reviseLesson, 'char-revise');
+ await seedDomain('char-revise-foreign', foreignUserId);
+ await seedLesson(reviseForeignLesson, 'char-revise-foreign');
+ const model = new ExpertiseModel(serverDB, userId);
+
+ await expect(model.reviseLesson(reviseForeignLesson, 'x')).resolves.toBeNull();
+ await expect(model.reviseLesson(reviseLesson, ' new limits ')).resolves.toEqual({
+ id: reviseLesson,
+ revision: 2,
+ });
+
+ const lesson = await model.findLesson(reviseLesson);
+ expect(lesson?.currentRevision).toBe(2);
+ expect(lesson?.sections).toEqual([
+ { body: 'rule body', key: 'rule' },
+ { body: 'new limits', key: 'limits' },
+ ]);
+ const revisions = await model.listLessonRevisions(reviseLesson);
+ expect(revisions.map((r) => [r.revision, r.feedback, r.kind, r.byViewer])).toEqual([
+ [2, 'new limits', 'user-feedback', true],
+ ]);
+ });
+
+ it('retires a lesson in scope only', async () => {
+ await seedDomain('char-retire');
+ await seedLesson(retireLesson, 'char-retire');
+ await seedDomain('char-retire-foreign', foreignUserId);
+ await seedLesson(retireForeignLesson, 'char-retire-foreign');
+ const model = new ExpertiseModel(serverDB, userId);
+
+ await expect(model.retireLesson(retireForeignLesson)).resolves.toBeNull();
+ await expect(model.retireLesson(retireLesson)).resolves.toEqual({
+ id: retireLesson,
+ });
+ const lesson = await model.findLesson(retireLesson);
+ expect(lesson?.status).toBe('retired');
+ expect(lesson?.retiredAt).toBeInstanceOf(Date);
+ });
+
+ it('locks a lesson in scope and appends a revision row', async () => {
+ await seedDomain('char-lock');
+ await seedLesson(lockLesson, 'char-lock');
+ await seedDomain('char-lock-foreign', foreignUserId);
+ await seedLesson(lockForeignLesson, 'char-lock-foreign');
+ const model = new ExpertiseModel(serverDB, userId);
+
+ expect((await model.lockLesson(lockLesson))?.id).toBe(lockLesson);
+ await expect(model.lockLesson(lockForeignLesson)).resolves.toBeUndefined();
+
+ await model.insertLessonRevision({
+ changedBy: 'user',
+ changedByUserId: userId,
+ kind: 'user-feedback',
+ lessonId: lockLesson,
+ prevTitle: 'before',
+ revision: 2,
+ sections: [],
+ });
+ const rows = await serverDB
+ .select({ prevTitle: expertiseLessonRevisions.prevTitle })
+ .from(expertiseLessonRevisions)
+ .where(eq(expertiseLessonRevisions.lessonId, lockLesson));
+ expect(rows).toEqual([{ prevTitle: 'before' }]);
+ });
+
+ it('records a material quote as a passing hit', async () => {
+ await seedDomain('char-material');
+ await seedLesson(materialLesson, 'char-material');
+ const model = new ExpertiseModel(serverDB, userId);
+ const run = await model.insertMaterialRun({
+ domainId: 'char-material',
+ reflectionKey: 'material:document:doc-1',
+ subjectId: 'doc-1',
+ subjectType: 'document',
+ });
+
+ const hitId = await model.insertMaterialHit({
+ domainId: 'char-material',
+ lessonId: materialLesson,
+ quote: '',
+ runId: run!,
+ where: 'page 2',
+ });
+
+ const [hit] = await serverDB.select().from(expertiseHits).where(eq(expertiseHits.id, hitId));
+ expect(hit).toMatchObject({ example: null, outcome: 'pass', runId: run, where: 'page 2' });
+ });
+
+ it('counts the distinct runs a lesson was hit in', async () => {
+ await seedDomain('char-runs');
+ await seedLesson(runsLesson, 'char-runs');
+ await seedRun(runA, 'char-runs', 1);
+ await seedRun(runB, 'char-runs', 2);
+ await serverDB.insert(expertiseHits).values([
+ { domainId: 'char-runs', lessonId: runsLesson, outcome: 'violation', runId: runA },
+ { domainId: 'char-runs', lessonId: runsLesson, outcome: 'pass', runId: runA },
+ { domainId: 'char-runs', lessonId: runsLesson, outcome: 'pass', runId: runB },
+ ]);
+ const model = new ExpertiseModel(serverDB, userId);
+
+ await expect(model.countLineageRuns(runsLesson)).resolves.toBe(2);
+ await expect(model.countLineageRuns(missingLesson)).resolves.toBe(0);
+ });
+});
diff --git a/packages/database/src/models/expertise.ts b/packages/database/src/models/expertise.ts
deleted file mode 100644
index b2ebf0c4303..00000000000
--- a/packages/database/src/models/expertise.ts
+++ /dev/null
@@ -1,1522 +0,0 @@
-import {
- EXPERTISE_RULE_DIRECTIONS,
- type ExpertiseAnchorCandidate,
- type ExpertiseCanonEntry,
- type ExpertiseEnforcement,
- type ExpertiseLayerDefinition,
- type ExpertiseLessonSection,
- type ExpertiseRuleDirection,
-} from '@lobechat/types';
-import { and, asc, desc, eq, inArray, isNotNull, isNull, or, type SQL, sql } from 'drizzle-orm';
-
-import {
- agents,
- documents,
- expertiseBindings,
- expertiseDomains,
- expertiseDomainSnapshots,
- expertiseHits,
- expertiseInsights,
- expertiseLessonRevisions,
- expertiseLessons,
- expertiseRuns,
- projects,
- topics,
- verifyCheckResults,
- verifyRuns,
-} from '../schemas';
-import type { LobeChatDatabase } from '../type';
-import { idGenerator } from '../utils/idGenerator';
-import { buildWorkspaceWhere } from '../utils/workspace';
-
-/** The core tier starts at 40% of the domain's maximum hit count, with a floor of two. */
-const CORE_CUT_RATIO = 0.4;
-const CORE_CUT_MIN = 2;
-
-export type ExpertiseTier = 'core' | 'niche' | 'unused';
-
-/**
- * Where a domain is mounted. Ownership (`expertise_domains.user_id` / `workspace_id`) is a
- * separate axis: a project never owns a domain, it only mounts one, so the same standard can be
- * mounted by several projects at once.
- */
-export type ExpertiseCarrier =
- { id: string; type: 'agent' } | { id: string; type: 'project' } | { type: 'user' };
-
-/** Whose a listed group is: the reviewer's own rules, or one agent's self-learning. */
-export type ExpertiseGroupOwner =
- | { kind: 'mine' }
- | {
- agent: {
- avatar: string | null;
- backgroundColor: string | null;
- id: string;
- title: string | null;
- };
- kind: 'agent';
- };
-
-/**
- * The single carrier column a binding sets. `user` resolves to the workspace when one is in
- * scope, so a workspace member's always-on standards reach their teammates rather than only
- * themselves β€” the same arm `listDomainsBoundTo` reads back.
- */
-const carrierColumns = (
- carrier: ExpertiseCarrier,
- owner: { userId: string; workspaceId?: string },
-) => {
- if (carrier.type === 'agent') return { agentId: carrier.id };
- if (carrier.type === 'project') return { projectId: carrier.id };
- return owner.workspaceId
- ? { boundWorkspaceId: owner.workspaceId }
- : { boundUserId: owner.userId };
-};
-
-/**
- * Runs that observed practice: a material the reviewer distilled (`reflectionKey` `material:…`)
- * records the passages a rule was read from, which are not outcomes of applying it.
- */
-const notMaterialRun = () =>
- sql`coalesce(${expertiseRuns.reflectionKey}, '') not like 'material:%'`;
-
-export class ExpertiseModel {
- private db: LobeChatDatabase;
- private userId: string;
- private workspaceId?: string;
-
- constructor(db: LobeChatDatabase, userId: string, workspaceId?: string) {
- this.db = db;
- this.userId = userId;
- this.workspaceId = workspaceId;
- }
-
- private scopeWhere = () =>
- buildWorkspaceWhere({ userId: this.userId, workspaceId: this.workspaceId }, expertiseDomains);
-
- private insightScopeWhere = () =>
- buildWorkspaceWhere({ userId: this.userId, workspaceId: this.workspaceId }, expertiseInsights);
-
- // Binding resolution
-
- /** Lists the agent-level and workspace-level expertise available to one authorized agent. */
- listDomainsForAgent = async (agentId: string) => {
- const [agent] = await this.db
- .select({ id: agents.id })
- .from(agents)
- .where(
- and(
- eq(agents.id, agentId),
- buildWorkspaceWhere({ userId: this.userId, workspaceId: this.workspaceId }, agents),
- ),
- )
- .limit(1);
- if (!agent) return [];
-
- return this.listDomainsBoundTo(eq(expertiseBindings.agentId, agentId));
- };
-
- /**
- * Lists the expertise mounted on one project, plus the caller's own always-on domains.
- *
- * Deliberately does not verify the project row: the caller reaches this through an acceptance
- * it already owns, and `scopeWhere` still confines the result to domains in scope.
- */
- listDomainsForProject = async (projectId: string) =>
- this.listDomainsBoundTo(eq(expertiseBindings.projectId, projectId));
-
- /** The caller's always-on domains only β€” the scope an acceptance without a project falls back to. */
- listDomainsForOwner = async () => this.listDomainsBoundTo();
-
- private listDomainsBoundTo = async (carrierWhere?: SQL) => {
- const ownerWhere = this.workspaceId
- ? eq(expertiseBindings.boundWorkspaceId, this.workspaceId)
- : eq(expertiseBindings.boundUserId, this.userId);
-
- const rows = await this.db
- .select({
- binding: {
- contributionMode: expertiseBindings.contributionMode,
- enabled: expertiseBindings.enabled,
- id: expertiseBindings.id,
- sortOrder: expertiseBindings.sortOrder,
- },
- domain: expertiseDomains,
- })
- .from(expertiseBindings)
- .innerJoin(expertiseDomains, eq(expertiseDomains.id, expertiseBindings.domainId))
- .where(
- and(
- eq(expertiseBindings.enabled, true),
- isNotNull(expertiseDomains.anchorChosenAt),
- this.scopeWhere(),
- carrierWhere ? or(carrierWhere, ownerWhere) : ownerWhere,
- ),
- )
- // Every binding starts at sortOrder 0, so without a tiebreak the order of a reviewer's
- // groups is whatever the planner returns and a newly opened group can jump to the top.
- .orderBy(asc(expertiseBindings.sortOrder), asc(expertiseDomains.createdAt));
-
- // One domain may be bound at multiple levels; retain the first binding by sort order.
- const seen = new Set<string>();
- return rows.filter((r) => {
- if (seen.has(r.domain.id)) return false;
- seen.add(r.domain.id);
- return true;
- });
- };
-
- // L0: overview
-
- /** Returns the latest snapshot for every requested domain. */
- latestSnapshots = async (domainIds: string[]) => {
- if (domainIds.length === 0) return [];
- return this.db
- .selectDistinctOn([expertiseDomainSnapshots.domainId])
- .from(expertiseDomainSnapshots)
- .where(inArray(expertiseDomainSnapshots.domainId, domainIds))
- .orderBy(desc(expertiseDomainSnapshots.domainId), desc(expertiseDomainSnapshots.runIndex));
- };
-
- /** Loads the complete active-lesson series for all requested domains in one query. */
- seriesForDomains = async (domainIds: string[]) => {
- if (domainIds.length === 0) return [];
- return this.db
- .select({
- activeCount: expertiseDomainSnapshots.activeCount,
- domainId: expertiseDomainSnapshots.domainId,
- runIndex: expertiseDomainSnapshots.runIndex,
- })
- .from(expertiseDomainSnapshots)
- .where(inArray(expertiseDomainSnapshots.domainId, domainIds))
- .orderBy(asc(expertiseDomainSnapshots.domainId), asc(expertiseDomainSnapshots.runIndex));
- };
-
- /**
- * Per-run pass/violation counts β€” the reliability series behind the "εšε―ΉηŽ‡" chart.
- * A run without hits produces no row; callers treat missing runs as "nothing to judge".
- * A material the reviewer distilled is not practice: its quotes are evidence the rule exists,
- * not a run that got it right, so they never count as passes.
- */
- reliabilitySeries = async (domainIds: string[]) => {
- if (domainIds.length === 0) return [];
- return this.db
- .select({
- domainId: expertiseHits.domainId,
- pass: sql<number>`count(*) filter (where ${expertiseHits.outcome} = 'pass')::int`,
- runIndex: expertiseRuns.runIndex,
- violation: sql<number>`count(*) filter (where ${expertiseHits.outcome} = 'violation')::int`,
- })
- .from(expertiseHits)
- .innerJoin(expertiseRuns, eq(expertiseRuns.id, expertiseHits.runId))
- .where(and(inArray(expertiseHits.domainId, domainIds), notMaterialRun()))
- .groupBy(expertiseHits.domainId, expertiseRuns.runIndex)
- .orderBy(asc(expertiseHits.domainId), asc(expertiseRuns.runIndex));
- };
-
- /**
- * Everything the self-evolving page lists β€” the reviewer's rules first, then what each agent
- * learned on its own β€” as groups with the lessons filed in them and where each takes effect.
- *
- * Reads the same binding arm the distillation writes through, so what this returns is exactly
- * what an acceptance without a project would add to. Rules come back in the reviewer's own
- * order (`sortOrder`, then creation) because they told us the order matters; the page does not
- * re-rank them by hit count.
- *
- * Each group carries an `owner`: `mine` when the reviewer, their workspace or a project holds
- * it (it reaches every run in that scope), otherwise the one agent it was learned by. A group
- * mounted on both is the reviewer's β€” that is the wider of the two reaches.
- */
- listRules = async () => {
- // The reviewer's own groups, the ones distilled from a project's acceptances, and every
- // agent's self-learning domains β€” the same set the run context injects from.
- const bound = await this.listDomainsBoundTo(
- or(isNotNull(expertiseBindings.projectId), isNotNull(expertiseBindings.agentId)),
- );
- const domainIds = bound.map(({ domain }) => domain.id);
- if (domainIds.length === 0) return [];
-
- const [lessons, bindings] = await Promise.all([
- this.db
- .select({
- code: expertiseLessons.code,
- compilability: expertiseLessons.compilability,
- createdAt: expertiseLessons.createdAt,
- createdByUserId: expertiseLessons.createdByUserId,
- // Null until judged; the page asks for the unjudged ones to be settled.
- direction: expertiseLessons.direction,
- domainId: expertiseLessons.domainId,
- originRunId: expertiseLessons.originRunId,
- // Rows that predate the column hold null; they have only ever been reminders.
- enforcement: sql<ExpertiseEnforcement>`coalesce(${expertiseLessons.enforcement}, 'remind')`,
- exampleCount: expertiseLessons.exampleCount,
- falsePositiveCount: expertiseLessons.falsePositiveCount,
- generalizedFromIds: expertiseLessons.generalizedFromIds,
- hitCount: expertiseLessons.hitCount,
- hitRunCount: expertiseLessons.hitRunCount,
- id: expertiseLessons.id,
- lastHitAt: expertiseLessons.lastHitAt,
- reasonKind: expertiseLessons.reasonKind,
- reasonSource: expertiseLessons.reasonSource,
- rejectedReason: expertiseLessons.rejectedReason,
- retiredAt: expertiseLessons.retiredAt,
- sections: expertiseLessons.sections,
- sortOrder: expertiseLessons.sortOrder,
- specificity: expertiseLessons.specificity,
- status: expertiseLessons.status,
- tags: expertiseLessons.tags,
- title: expertiseLessons.title,
- })
- .from(expertiseLessons)
- .where(
- and(
- inArray(expertiseLessons.domainId, domainIds),
- // Retired ones are kept and returned: the page files them under an archive the reader
- // can reopen, because "I already told it to stop using this" is itself worth seeing.
- inArray(expertiseLessons.status, ['active', 'retired']),
- ),
- )
- // Unplaced rows (null, from before the column existed) come after the ones the
- // reviewer ordered, oldest first, until a drag writes them an explicit position.
- .orderBy(
- sql`${expertiseLessons.sortOrder} asc nulls last`,
- asc(expertiseLessons.createdAt),
- ),
- this.db
- .select({
- agentAvatar: agents.avatar,
- agentBackgroundColor: agents.backgroundColor,
- agentId: expertiseBindings.agentId,
- agentTitle: agents.title,
- // Null when the agent exists but the viewer may not see it.
- visibleAgentId: agents.id,
- boundUserId: expertiseBindings.boundUserId,
- boundWorkspaceId: expertiseBindings.boundWorkspaceId,
- domainId: expertiseBindings.domainId,
- projectId: expertiseBindings.projectId,
- projectName: projects.name,
- })
- .from(expertiseBindings)
- .leftJoin(projects, eq(projects.id, expertiseBindings.projectId))
- // A shared group can also be mounted on a teammate's private agent; the viewer may see
- // the group without being allowed to see that agent, its id or its name.
- .leftJoin(
- agents,
- and(
- eq(agents.id, expertiseBindings.agentId),
- buildWorkspaceWhere({ userId: this.userId, workspaceId: this.workspaceId }, agents),
- ),
- )
- .where(
- and(
- inArray(expertiseBindings.domainId, domainIds),
- eq(expertiseBindings.enabled, true),
- or(isNull(expertiseBindings.agentId), isNotNull(agents.id)),
- ),
- )
- .orderBy(asc(expertiseBindings.sortOrder)),
- ]);
-
- const bySource = await this.countHitsBySource(lessons.map((lesson) => lesson.id));
-
- // Bucket once by domain: the page now spans every agent's domains, so filtering the full
- // lesson and binding lists per group would grow with groups Γ— rows.
- const byDomain = <T extends { domainId: string }>(rows: T[]) => {
- const map = new Map<string, T[]>();
- for (const row of rows) {
- const bucket = map.get(row.domainId);
- if (bucket) bucket.push(row);
- else map.set(row.domainId, [row]);
- }
- return map;
- };
- const lessonsByDomain = byDomain(lessons);
- const bindingsByDomain = byDomain(bindings);
-
- const ownerOf = (domainId: string): ExpertiseGroupOwner | null => {
- const mounts = bindingsByDomain.get(domainId) ?? [];
- if (mounts.some((binding) => !binding.agentId)) return { kind: 'mine' };
- const agent = mounts.find((binding) => binding.visibleAgentId);
- // Only mounted on agents the viewer cannot see: nothing of it belongs on their page.
- if (!agent?.agentId) return null;
- return {
- agent: {
- avatar: agent.agentAvatar,
- backgroundColor: agent.agentBackgroundColor,
- id: agent.agentId,
- title: agent.agentTitle,
- },
- kind: 'agent',
- };
- };
-
- const owned = bound.flatMap(({ domain }) => {
- const owner = ownerOf(domain.id);
- return owner ? [{ domain, owner }] : [];
- });
- // The reviewer's groups lead in their own order; each agent's follow together, agents in the
- // order their first group was mounted.
- const agentOrder = [
- ...new Set(owned.flatMap(({ owner }) => (owner.kind === 'agent' ? [owner.agent.id] : []))),
- ];
- const rank = ({ owner }: (typeof owned)[number]) =>
- owner.kind === 'mine' ? -1 : agentOrder.indexOf(owner.agent.id);
- const ordered = owned
- .map((entry, index) => ({ entry, index }))
- .sort((a, b) => rank(a.entry) - rank(b.entry) || a.index - b.index)
- .map(({ entry }) => entry);
-
- return ordered.map(({ domain, owner }) => ({
- owner,
- domain: {
- domainFilter: domain.domainFilter,
- id: domain.id,
- outOfScope: domain.outOfScope,
- title: domain.title,
- },
- rules: (lessonsByDomain.get(domain.id) ?? [])
- // Written by the reviewer rather than distilled from a run. Hit counts grow as a rule is
- // applied, so provenance is read from where the row came from β€” same test the lesson
- // reader uses for `taughtByUser`.
- .map(({ originRunId, ...lesson }) => ({
- ...lesson,
- ...(bySource.get(lesson.id) ?? {
- conversationHitCount: 0,
- materialHitCount: 0,
- rejectionHitCount: 0,
- }),
- authored: lesson.createdByUserId != null && originRunId == null,
- })),
- scopes: (bindingsByDomain.get(domain.id) ?? []).map((binding) => {
- if (binding.projectId) {
- return { id: binding.projectId, kind: 'project' as const, title: binding.projectName };
- }
- if (binding.agentId) {
- return { id: binding.agentId, kind: 'agent' as const, title: binding.agentTitle };
- }
- return binding.boundWorkspaceId
- ? { id: binding.boundWorkspaceId, kind: 'workspace' as const, title: null }
- : { id: binding.boundUserId!, kind: 'user' as const, title: null };
- }),
- }));
- };
-
- /**
- * The rejections one rule was distilled from, resolved back to the acceptance they were
- * written in so the reader can reopen the round and see the frame they circled.
- *
- * Evidence never changes hands: a hit stays on the lesson and the run that produced it, because
- * both are pinned to their domain by composite keys. So a rule that absorbed others
- * (`generalizedFromIds`) or was re-filed into another group (`salvagedFromId`) reads its sources
- * through that lineage instead of owning them.
- *
- * `sourceCheckResultId` is nullable by design β€” a deleted acceptance leaves the rule standing
- * and only breaks the trail β€” so the joins are left joins and the caller renders an unlinked
- * row rather than dropping the evidence.
- */
- listLessonSources = async (lessonId: string, limit = 20) => {
- const lineage = await this.resolveLineage(lessonId);
- if (lineage.length === 0) return [];
- return (
- this.db
- .select({
- acceptanceId: verifyRuns.acceptanceId,
- checkTitle: verifyCheckResults.checkItemTitle,
- createdAt: expertiseHits.createdAt,
- example: expertiseHits.example,
- // A material the reviewer brought: the document it was read from (a file is recorded as
- // its parsed document, which keeps the file id), and which kind of material it was.
- documentFileId: documents.fileId,
- documentId: documents.id,
- // Read from the run, not from the check-result join: a deleted acceptance leaves that
- // join empty, and the hit was still a rejection.
- fromAcceptance: sql<boolean>`coalesce(${expertiseRuns.reflectionKey}, '') like 'acceptance:%'`,
- materialType: sql<
- string | null
- >`case when ${expertiseRuns.reflectionKey} like 'material:%' then split_part(${expertiseRuns.reflectionKey}, ':', 2) end`,
- id: expertiseHits.id,
- // Where a conversation source can be reopened: the topic, the agent it lives under, and
- // the message the observation was read from when ingestion could find it.
- messageId: expertiseHits.sourceMessageId,
- reviewerComment: sql<
- string | null
- >`${verifyCheckResults.userDecisionDetail} ->> 'comment'`,
- roundIndex: verifyRuns.roundIndex,
- severity: expertiseHits.severity,
- topicAgentId: topics.agentId,
- topicId: topics.id,
- userDecision: expertiseHits.userDecision,
- where: expertiseHits.where,
- })
- .from(expertiseHits)
- .innerJoin(expertiseDomains, eq(expertiseDomains.id, expertiseHits.domainId))
- .innerJoin(expertiseRuns, eq(expertiseRuns.id, expertiseHits.runId))
- // Only a topic the viewer can open: a shared group does not grant a teammate's topic.
- .leftJoin(
- topics,
- and(
- eq(expertiseRuns.subjectType, 'topic'),
- eq(topics.id, expertiseRuns.subjectId),
- buildWorkspaceWhere({ userId: this.userId, workspaceId: this.workspaceId }, topics),
- ),
- )
- // Only a document the viewer can open, by the same scope documents are listed with.
- .leftJoin(
- documents,
- and(
- eq(expertiseRuns.subjectType, 'document'),
- eq(documents.id, expertiseRuns.subjectId),
- buildWorkspaceWhere({ userId: this.userId, workspaceId: this.workspaceId }, documents),
- ),
- )
- .leftJoin(verifyCheckResults, eq(verifyCheckResults.id, expertiseHits.sourceCheckResultId))
- .leftJoin(verifyRuns, eq(verifyRuns.id, verifyCheckResults.verifyRunId))
- .where(
- and(
- inArray(expertiseHits.lessonId, lineage),
- this.scopeWhere(),
- // Access to a shared group is not access to the rounds behind it: a hit distilled from
- // a teammate's private round would otherwise show that round's check, the reviewer's
- // words and a link to it. Same predicate the consolidation reader applies.
- or(
- eq(verifyCheckResults.userId, this.userId),
- eq(verifyRuns.visibility, 'public'),
- // No linked round (never mapped, or the round was deleted): nothing says the viewer
- // may see where it came from, so only their own runs' evidence is shown.
- and(isNull(verifyCheckResults.id), eq(expertiseRuns.userId, this.userId)),
- ),
- ),
- )
- .orderBy(desc(expertiseHits.createdAt))
- .limit(limit)
- );
- };
-
- /**
- * Every lesson whose evidence this one now speaks for: itself, what it absorbed through merges
- * (`generalizedFromIds`) and what it was re-filed from (`salvagedFromId`), followed through as
- * many generations as there are. One level is not enough β€” merging a rule that was itself a
- * merge would otherwise keep the counts and lose the evidence.
- */
- private resolveLineage = async (lessonId: string): Promise<string[]> =>
- (await this.resolveLineages([lessonId])).get(lessonId) ?? [];
-
- /**
- * {@link resolveLineage} for many rules at once: one read per generation instead of one walk
- * per rule, so a whole page of rules costs a handful of queries. A rule outside the caller's
- * scope maps to an empty lineage.
- */
- private resolveLineages = async (lessonIds: string[]) => {
- const parents = new Map<string, string[]>();
- let frontier = [...new Set(lessonIds)];
- // Bounded so a malformed cycle can never spin; real lineages are a handful of steps deep.
- for (let depth = 0; depth <= 16 && frontier.length > 0; depth += 1) {
- const rows = await this.db
- .select({
- generalizedFromIds: expertiseLessons.generalizedFromIds,
- id: expertiseLessons.id,
- salvagedFromId: expertiseLessons.salvagedFromId,
- })
- .from(expertiseLessons)
- .innerJoin(expertiseDomains, eq(expertiseDomains.id, expertiseLessons.domainId))
- .where(and(inArray(expertiseLessons.id, frontier), this.scopeWhere()));
- for (const row of rows) {
- parents.set(row.id, [
- ...(row.generalizedFromIds ?? []),
- ...(row.salvagedFromId ? [row.salvagedFromId] : []),
- ]);
- }
- frontier = [...new Set(rows.flatMap((row) => parents.get(row.id) ?? []))].filter(
- (id) => !parents.has(id),
- );
- }
-
- return new Map(
- lessonIds.map((root) => {
- if (!parents.has(root)) return [root, []];
- const seen = new Set<string>([root]);
- const stack = [root];
- while (stack.length > 0) {
- for (const parent of parents.get(stack.pop()!) ?? []) {
- if (seen.has(parent)) continue;
- seen.add(parent);
- stack.push(parent);
- }
- }
- return [root, [...seen]];
- }),
- );
- };
-
- /**
- * How many times each rule's lineage was hit by a rejected acceptance versus observed in a
- * conversation. `hitCount` adds both, and the two mean different things to the reviewer: only
- * the first is "you rejected a delivery for this".
- */
- private countHitsBySource = async (lessonIds: string[]) => {
- const lineages = await this.resolveLineages(lessonIds);
- const all = [...new Set([...lineages.values()].flat())];
- const rows =
- all.length === 0
- ? []
- : await this.db
- .select({
- conversations: sql<number>`count(*) filter (where coalesce(${expertiseRuns.reflectionKey}, '') not like 'acceptance:%' and coalesce(${expertiseRuns.reflectionKey}, '') not like 'material:%')::int`,
- lessonId: expertiseHits.lessonId,
- // Read from a material the reviewer brought: evidence the rule was stated, not seen.
- materials: sql<number>`count(*) filter (where ${expertiseRuns.reflectionKey} like 'material:%')::int`,
- rejections: sql<number>`count(*) filter (where ${expertiseRuns.reflectionKey} like 'acceptance:%')::int`,
- })
- .from(expertiseHits)
- .innerJoin(expertiseRuns, eq(expertiseRuns.id, expertiseHits.runId))
- .where(inArray(expertiseHits.lessonId, all))
- .groupBy(expertiseHits.lessonId);
- const byLesson = new Map(rows.map((row) => [row.lessonId, row]));
-
- return new Map(
- lessonIds.map((id) => {
- const lineage = lineages.get(id) ?? [];
- const sum = (key: 'conversations' | 'materials' | 'rejections') =>
- lineage.reduce((total, member) => total + (byLesson.get(member)?.[key] ?? 0), 0);
- return [
- id,
- {
- conversationHitCount: sum('conversations'),
- materialHitCount: sum('materials'),
- rejectionHitCount: sum('rejections'),
- },
- ];
- }),
- );
- };
-
- /**
- * Rejected rounds no standard has been distilled from yet.
- *
- * Distillation only fires forward β€” a round is read when the NEXT one lands β€” so every round
- * rejected before the feature existed stays untouched unless something asks for it. This is
- * that backlog, and it is the only honest number to put on an empty standards page.
- */
- countUndistilledRejectionRounds = async () => {
- const [row] = await this.db
- .select({
- rounds: sql<number>`count(distinct ${verifyCheckResults.verifyRunId})::int`,
- })
- .from(verifyCheckResults)
- .where(
- and(
- eq(verifyCheckResults.userId, this.userId),
- // The page is either the personal view or one workspace's; count only that scope.
- this.workspaceId
- ? eq(verifyCheckResults.workspaceId, this.workspaceId)
- : isNull(verifyCheckResults.workspaceId),
- eq(verifyCheckResults.userDecision, 'rejected'),
- isNotNull(verifyCheckResults.verifyRunId),
- sql`not exists (
- select 1 from ${expertiseRuns}
- where ${expertiseRuns.reflectionKey} like '%:run:' || ${verifyCheckResults.verifyRunId}::text
- )`,
- ),
- );
-
- return row?.rounds ?? 0;
- };
-
- /**
- * Active lessons for the portrait, each with its most recent hit outcomes (newest last).
- * Recent outcomes are what the client folds into a reliability tier; the topic title of each
- * hit lets the row say *where* it last went wrong without another query.
- * Material quotes are left out for the same reason as in {@link reliabilitySeries}.
- */
- listLessonsWithRecent = async (domainIds: string[], recentLimit = 6) => {
- if (domainIds.length === 0) return [];
- const lessons = await this.db
- .select({
- code: expertiseLessons.code,
- createdAt: expertiseLessons.createdAt,
- createdByUserId: expertiseLessons.createdByUserId,
- domainId: expertiseLessons.domainId,
- hitCount: expertiseLessons.hitCount,
- id: expertiseLessons.id,
- lastHitAt: expertiseLessons.lastHitAt,
- layer: expertiseLessons.layer,
- originRunId: expertiseLessons.originRunId,
- title: expertiseLessons.title,
- })
- .from(expertiseLessons)
- .where(
- and(inArray(expertiseLessons.domainId, domainIds), eq(expertiseLessons.status, 'active')),
- )
- .orderBy(desc(expertiseLessons.hitCount), asc(expertiseLessons.code));
-
- const ranked = this.db.$with('ranked').as(
- this.db
- .select({
- lessonId: expertiseHits.lessonId,
- outcome: expertiseHits.outcome,
- rn: sql<number>`row_number() over (partition by ${expertiseHits.lessonId} order by ${expertiseRuns.runIndex} desc, ${expertiseHits.createdAt} desc)`.as(
- 'rn',
- ),
- runIndex: expertiseRuns.runIndex,
- subjectId: expertiseRuns.subjectId,
- subjectType: expertiseRuns.subjectType,
- })
- .from(expertiseHits)
- .innerJoin(expertiseRuns, eq(expertiseRuns.id, expertiseHits.runId))
- .where(and(inArray(expertiseHits.domainId, domainIds), notMaterialRun())),
- );
- const recentRows = await this.db
- .with(ranked)
- .select({
- lessonId: ranked.lessonId,
- outcome: ranked.outcome,
- rn: ranked.rn,
- runIndex: ranked.runIndex,
- subjectId: ranked.subjectId,
- subjectTitle: topics.title,
- subjectType: ranked.subjectType,
- })
- .from(ranked)
- .leftJoin(topics, and(eq(ranked.subjectType, 'topic'), eq(topics.id, ranked.subjectId)))
- .where(sql`${ranked.rn} <= ${recentLimit}`)
- .orderBy(asc(ranked.lessonId), desc(ranked.rn));
-
- const recentByLesson = new Map<
- string,
- {
- pass: boolean;
- runIndex: number;
- subjectId: string;
- subjectTitle: string | null;
- subjectType: string;
- }[]
- >();
- for (const r of recentRows) {
- const list = recentByLesson.get(r.lessonId) ?? [];
- list.push({
- pass: r.outcome === 'pass',
- runIndex: r.runIndex,
- subjectId: r.subjectId,
- subjectTitle: r.subjectTitle,
- subjectType: r.subjectType,
- });
- recentByLesson.set(r.lessonId, list);
- }
- return lessons.map(({ createdByUserId, originRunId, ...l }) => ({
- ...l,
- recent: recentByLesson.get(l.id) ?? [],
- /**
- * Taught by the user directly, as opposed to distilled from practice. Older ingestion
- * runs stamped the acting user on distilled lessons too, so a lesson that traces back to
- * a run is never "taught", whoever created the row.
- */
- taughtByUser: createdByUserId != null && originRunId == null,
- }));
- };
-
- /** Lists the agents that have practiced each domain. */
- actorsByDomain = async (domainIds: string[]) => {
- if (domainIds.length === 0) return [];
- return this.db
- .selectDistinct({ actorId: expertiseRuns.actorId, domainId: expertiseRuns.domainId })
- .from(expertiseRuns)
- .where(and(inArray(expertiseRuns.domainId, domainIds), eq(expertiseRuns.actorType, 'agent')));
- };
-
- /**
- * Who a domain reaches, as one comparable key β€” the same rule `listRules` uses for its
- * `owner`: `mine` when any enabled binding is not an agent's (the reviewer, workspace or a
- * project), otherwise the agents it is mounted on. A rule moved or merged across reaches would
- * silently change which runs receive it, so both mutations refuse that.
- */
- private reachOf = async (domainId: string) => {
- const mounts = await this.db
- .select({ agentId: expertiseBindings.agentId })
- .from(expertiseBindings)
- .where(and(eq(expertiseBindings.domainId, domainId), eq(expertiseBindings.enabled, true)));
- if (mounts.some((mount) => !mount.agentId)) return 'mine';
- const agentIds = [...new Set(mounts.map((mount) => mount.agentId!))].sort();
- return agentIds.length > 0 ? `agent:${agentIds.join(',')}` : 'unbound';
- };
-
- /** Whether two domains reach the same runs; see {@link reachOf}. */
- sameReach = async (domainA: string, domainB: string) => {
- if (domainA === domainB) return true;
- const [a, b] = await Promise.all([this.reachOf(domainA), this.reachOf(domainB)]);
- return a === b;
- };
-
- // L1: domain detail
-
- findDomain = async (domainId: string) => {
- const [row] = await this.db
- .select()
- .from(expertiseDomains)
- .where(
- and(
- eq(expertiseDomains.id, domainId),
- isNotNull(expertiseDomains.anchorChosenAt),
- this.scopeWhere(),
- ),
- )
- .limit(1);
- return row;
- };
-
- /** Returns the complete snapshot series for a domain. */
- listSnapshots = async (domainId: string) =>
- this.db
- .select()
- .from(expertiseDomainSnapshots)
- .where(eq(expertiseDomainSnapshots.domainId, domainId))
- .orderBy(asc(expertiseDomainSnapshots.runIndex));
-
- listRuns = async (domainId: string, limit = 50) =>
- this.db
- .select()
- .from(expertiseRuns)
- .where(eq(expertiseRuns.domainId, domainId))
- .orderBy(desc(expertiseRuns.runIndex))
- .limit(limit);
-
- /** Counts all runs independently of the paginated run list. */
- countRuns = async (domainId: string) => {
- const [row] = await this.db
- .select({ n: sql<number>`count(*)::int` })
- .from(expertiseRuns)
- .where(eq(expertiseRuns.domainId, domainId));
- return row?.n ?? 0;
- };
-
- /** Returns whether a human participated in each practice run. */
- runHumanFlags = async (domainId: string) =>
- this.db
- .select({
- hadHumanInLoop: expertiseRuns.hadHumanInLoop,
- runIndex: expertiseRuns.runIndex,
- })
- .from(expertiseRuns)
- .where(eq(expertiseRuns.domainId, domainId))
- .orderBy(asc(expertiseRuns.runIndex));
-
- /** Summarizes active lesson count, hits, and unused lessons. */
- lessonStats = async (domainId: string) => {
- const [row] = await this.db
- .select({
- hits: sql<number>`coalesce(sum(${expertiseLessons.hitCount}), 0)::int`,
- total: sql<number>`count(*)::int`,
- unused: sql<number>`count(*) filter (where ${expertiseLessons.hitCount} = 0)::int`,
- })
- .from(expertiseLessons)
- .where(and(eq(expertiseLessons.domainId, domainId), eq(expertiseLessons.status, 'active')));
- return row ?? { hits: 0, total: 0, unused: 0 };
- };
-
- // L2: lesson library
-
- /** Lists active lessons by hit count and assigns their usage tier. */
- listLessons = async (domainId: string, opts?: { layer?: string; search?: string }) => {
- const conditions = [
- eq(expertiseLessons.domainId, domainId),
- eq(expertiseLessons.status, 'active'),
- ];
- if (opts?.layer) conditions.push(eq(expertiseLessons.layer, opts.layer));
- if (opts?.search) {
- conditions.push(sql`${expertiseLessons.title} ILIKE ${`%${opts.search}%`}`);
- }
-
- const rows = await this.db
- .select({ lesson: expertiseLessons })
- .from(expertiseLessons)
- .innerJoin(expertiseDomains, eq(expertiseDomains.id, expertiseLessons.domainId))
- .where(and(...conditions, this.scopeWhere()))
- .orderBy(desc(expertiseLessons.hitCount), asc(expertiseLessons.code));
-
- const lessons = rows.map(({ lesson }) => lesson);
-
- const maxHit = lessons.reduce((a, r) => Math.max(a, r.hitCount), 0);
- const cut = Math.max(CORE_CUT_MIN, Math.round(maxHit * CORE_CUT_RATIO));
-
- return lessons.map((r) => ({
- ...r,
- tier: (r.hitCount >= cut ? 'core' : r.hitCount > 0 ? 'niche' : 'unused') as ExpertiseTier,
- }));
- };
-
- /** Counts active lessons by declared layer. */
- layerCounts = async (domainId: string) => {
- const rows = await this.db
- .select({ layer: expertiseLessons.layer, n: sql<number>`count(*)::int` })
- .from(expertiseLessons)
- .where(and(eq(expertiseLessons.domainId, domainId), eq(expertiseLessons.status, 'active')))
- .groupBy(expertiseLessons.layer);
- return Object.fromEntries(rows.filter((r) => r.layer).map((r) => [r.layer!, r.n]));
- };
-
- /** Counts active lessons by canon anchor, including unanchored lessons. */
- canonAnchorCounts = async (domainId: string) => {
- const rows = await this.db
- .select({ anchor: expertiseLessons.canonAnchor, n: sql<number>`count(*)::int` })
- .from(expertiseLessons)
- .where(and(eq(expertiseLessons.domainId, domainId), eq(expertiseLessons.status, 'active')))
- .groupBy(expertiseLessons.canonAnchor);
- return {
- byKey: Object.fromEntries(rows.filter((r) => r.anchor).map((r) => [r.anchor!, r.n])),
- unanchored: rows.find((r) => !r.anchor)?.n ?? 0,
- };
- };
-
- // L3: lesson detail
-
- findLesson = async (lessonId: string) => {
- const [row] = await this.db
- .select({ lesson: expertiseLessons })
- .from(expertiseLessons)
- .innerJoin(expertiseDomains, eq(expertiseDomains.id, expertiseLessons.domainId))
- .where(and(eq(expertiseLessons.id, lessonId), this.scopeWhere()))
- .limit(1);
- return row?.lesson;
- };
-
- /** Lists lesson evidence together with its source run and topic. */
- listLessonHits = async (lessonId: string, limit = 20) =>
- this.db
- .select({
- createdAt: expertiseHits.createdAt,
- example: expertiseHits.example,
- note: expertiseHits.note,
- outcome: expertiseHits.outcome,
- runIndex: expertiseRuns.runIndex,
- runTitle: sql<string>`coalesce(${topics.title}, ${expertiseRuns.subjectId})`,
- severity: expertiseHits.severity,
- subjectId: expertiseRuns.subjectId,
- subjectType: expertiseRuns.subjectType,
- where: expertiseHits.where,
- })
- .from(expertiseHits)
- .innerJoin(expertiseRuns, eq(expertiseRuns.id, expertiseHits.runId))
- .innerJoin(expertiseDomains, eq(expertiseDomains.id, expertiseHits.domainId))
- .leftJoin(
- topics,
- and(eq(expertiseRuns.subjectType, 'topic'), eq(topics.id, expertiseRuns.subjectId)),
- )
- .where(and(eq(expertiseHits.lessonId, lessonId), this.scopeWhere()))
- .orderBy(desc(expertiseHits.createdAt))
- .limit(limit);
-
- // Writes
-
- /**
- * Persists a reviewed anchor and mounts it on the selected carrier.
- * The chosen candidate is also kept in anchorCandidates so the alternative can be revisited.
- */
- createDomain = async (params: {
- brief: string;
- canonEntries?: ExpertiseCanonEntry[];
- carrier: ExpertiseCarrier;
- domainFilter: string;
- layerCanonRef?: string;
- layerSource?: 'canonical' | 'invented';
- layers?: ExpertiseLayerDefinition[];
- outOfScope?: string;
- rationale?: string;
- title: string;
- }) => {
- const brief = params.brief.trim();
- const id = idGenerator('expertiseDomains');
- const title = params.title.trim();
- const domainFilter = params.domainFilter.trim();
- const slug = `${title.slice(0, 40).replaceAll(/\s+/g, '-').toLowerCase()}-${id.slice(-6)}`;
- const layers = params.layers ?? [];
- const canonEntries = params.canonEntries ?? [];
- const layerSource = params.layerSource ?? 'invented';
- const candidate: ExpertiseAnchorCandidate = {
- canonEntries,
- domainFilter,
- key: 'chosen',
- layerCanonRef: params.layerCanonRef,
- layers,
- layerSource,
- outOfScope: params.outOfScope?.trim() || undefined,
- rationale: params.rationale?.trim() || undefined,
- title,
- };
-
- await this.db.transaction(async (tx) => {
- await tx.insert(expertiseDomains).values({
- anchorCandidates: [candidate],
- anchorChosenAt: new Date(),
- anchorChosenByUserId: this.userId,
- canonEntries,
- description: brief,
- domainFilter,
- id,
- layerSource,
- layers,
- outOfScope: params.outOfScope?.trim() || null,
- seedState: 'seeded',
- slug,
- title,
- userId: this.userId,
- workspaceId: this.workspaceId,
- });
- const carrier = carrierColumns(params.carrier, {
- userId: this.userId,
- workspaceId: this.workspaceId,
- });
- // A new mount goes after the ones already there. Leaving every binding at 0 makes the
- // reviewer's group order depend on the query plan.
- const [carrierColumn, carrierValue] = Object.entries(carrier)[0] as [
- keyof typeof expertiseBindings.$inferInsert,
- string,
- ];
- const [last] = await tx
- .select({ sortOrder: sql<number>`max(${expertiseBindings.sortOrder})` })
- .from(expertiseBindings)
- .where(eq(expertiseBindings[carrierColumn as 'boundUserId'], carrierValue));
- await tx.insert(expertiseBindings).values({
- addedByUserId: this.userId,
- domainId: id,
- sortOrder: (last?.sortOrder ?? -1) + 1,
- workspaceId: this.workspaceId,
- ...carrier,
- });
- });
- return id;
- };
-
- /**
- * Deletes a domain the user owns together with everything learned in it β€” bindings, lessons,
- * runs, hits, snapshots and insights all cascade from the domain row. Nothing is kept: the
- * user chose to drop the direction, not to pause it.
- */
- deleteDomain = async (domainId: string) => {
- const [row] = await this.db
- .delete(expertiseDomains)
- .where(and(eq(expertiseDomains.id, domainId), this.scopeWhere()))
- .returning({ id: expertiseDomains.id });
- return row ?? null;
- };
-
- /** Stores a lesson the user taught directly; it takes effect on the next matching practice. */
- teachLesson = async (params: { domainId: string; text: string }) => {
- const domain = await this.findDomain(params.domainId);
- if (!domain) return null;
- const text = params.text.trim();
- const title = text.length > 80 ? `${text.slice(0, 79)}…` : text;
- return this.db.transaction(async (tx) => {
- const codes = await tx
- .select({ code: expertiseLessons.code })
- .from(expertiseLessons)
- .where(eq(expertiseLessons.domainId, params.domainId));
- const next =
- Math.max(0, ...codes.map(({ code }) => Number(/^P-(\d+)$/.exec(code)?.[1] ?? 0))) + 1;
- const code = `P-${String(next).padStart(2, '0')}`;
- const [row] = await tx
- .insert(expertiseLessons)
- .values({
- code,
- createdByUserId: this.userId,
- domainId: params.domainId,
- polarity: 'rule',
- sections: [{ body: text, key: 'rule' }],
- title,
- })
- .returning({ code: expertiseLessons.code, id: expertiseLessons.id });
- return row;
- });
- };
-
- /**
- * Records the user's correction as a new revision and folds it into the lesson body.
- * The correction is kept as its own section so the original judgment stays legible.
- */
- reviseLesson = async (lessonId: string, feedback: string) => {
- const lesson = await this.findLesson(lessonId);
- if (!lesson) return null;
- const text = feedback.trim();
- const sections: ExpertiseLessonSection[] = [
- ...lesson.sections.filter((s) => s.key !== 'limits'),
- { body: text, key: 'limits' },
- ];
- const revision = lesson.currentRevision + 1;
- await this.db.transaction(async (tx) => {
- await tx.insert(expertiseLessonRevisions).values({
- changedBy: 'user',
- changedByUserId: this.userId,
- feedback: text,
- kind: 'user-feedback',
- lessonId,
- prevTitle: lesson.title,
- revision,
- sections,
- });
- await tx
- .update(expertiseLessons)
- .set({ currentRevision: revision, sections, updatedAt: new Date() })
- .where(eq(expertiseLessons.id, lessonId));
- });
- return { id: lessonId, revision };
- };
-
- /**
- * The edits a standard has been through, newest first.
- *
- * `feedback` is the reviewer's own sentence and `changedBy` says whether the edit came from them
- * or from the system generalizing, which is the distinction that makes the history readable.
- */
- listLessonRevisions = async (lessonId: string, limit = 10) => {
- // A rule re-filed into another group is a copy; its earlier edits stay on the row it was
- // copied from. Follow `salvagedFromId` back so the history reads as one rule's. Merged-in
- // rules are not followed: their edits were to a different sentence.
- const chain = [lessonId];
- for (let depth = 0; depth < 16; depth += 1) {
- const [row] = await this.db
- .select({ salvagedFromId: expertiseLessons.salvagedFromId })
- .from(expertiseLessons)
- .innerJoin(expertiseDomains, eq(expertiseDomains.id, expertiseLessons.domainId))
- .where(and(eq(expertiseLessons.id, chain.at(-1)!), this.scopeWhere()))
- .limit(1);
- const previous = row?.salvagedFromId;
- if (!previous || chain.includes(previous)) break;
- chain.push(previous);
- }
- const rows = await this.db
- .select({
- changedBy: expertiseLessonRevisions.changedBy,
- changedByUserId: expertiseLessonRevisions.changedByUserId,
- createdAt: expertiseLessonRevisions.createdAt,
- feedback: expertiseLessonRevisions.feedback,
- id: expertiseLessonRevisions.id,
- kind: expertiseLessonRevisions.kind,
- prevTitle: expertiseLessonRevisions.prevTitle,
- revision: expertiseLessonRevisions.revision,
- })
- .from(expertiseLessonRevisions)
- .innerJoin(expertiseLessons, eq(expertiseLessons.id, expertiseLessonRevisions.lessonId))
- .innerJoin(expertiseDomains, eq(expertiseDomains.id, expertiseLessons.domainId))
- .where(and(inArray(expertiseLessonRevisions.lessonId, chain), this.scopeWhere()))
- .orderBy(desc(expertiseLessonRevisions.revision), desc(expertiseLessonRevisions.createdAt))
- .limit(limit);
- // In a shared group any member can edit; the reader is told whether an edit was theirs, and
- // the other member's id does not leave the server.
- return rows.map(({ changedByUserId, ...row }) => ({
- ...row,
- byViewer: changedByUserId === this.userId,
- }));
- };
-
- /** Brings a retired standard back into practice. */
- restoreLesson = async (lessonId: string) => {
- const lesson = await this.findLesson(lessonId);
- if (!lesson) return null;
- // A rule archived by a merge already lives on inside its target: its counts were added there
- // and its evidence is read through the target's lineage. Bringing it back would count the
- // same history twice, so it stays archived.
- if (lesson.rejectedReason?.startsWith('merged-into:')) return null;
- await this.db
- .update(expertiseLessons)
- .set({ retiredAt: null, status: 'active', updatedAt: new Date() })
- .where(eq(expertiseLessons.id, lessonId));
- return { id: lessonId };
- };
-
- /**
- * The next free `P-nn` code inside one domain; codes are unique per domain, not globally.
- *
- * Takes the domain row lock first β€” the same lock distillation holds while it hands out codes β€”
- * so two writers in one group queue up instead of computing the same number and having the
- * unique index reject one of them. The lock lasts until the caller's transaction commits.
- */
- private nextLessonCode = async (
- tx: Pick<LobeChatDatabase, 'select'>,
- domainId: string,
- ): Promise<string> => {
- await tx
- .select({ id: expertiseDomains.id })
- .from(expertiseDomains)
- .where(eq(expertiseDomains.id, domainId))
- .for('update');
- const codes = await tx
- .select({ code: expertiseLessons.code })
- .from(expertiseLessons)
- .where(eq(expertiseLessons.domainId, domainId));
- const next =
- Math.max(0, ...codes.map(({ code }) => Number(/^P-(\d+)$/.exec(code)?.[1] ?? 0))) + 1;
- return `P-${String(next).padStart(2, '0')}`;
- };
-
- /**
- * A rule the reviewer writes down themselves, filed at the top of its group so they see it
- * where they just put it. Hand-written rules start as `taste` from the reviewer: nothing has
- * been distilled, so there is no mechanism to claim yet.
- */
- createRule = async (params: {
- compilability?: 'compilable' | 'not-compilable';
- direction?: ExpertiseRuleDirection;
- domainId: string;
- enforcement?: ExpertiseEnforcement;
- how?: string;
- limits?: string;
- /**
- * The run it was read from, when it was distilled from a material rather than typed. It also
- * keeps the row from reading as "you wrote it", which is decided by the absence of an origin.
- */
- originRunId?: string;
- title: string;
- why?: string;
- }) => {
- const domain = await this.findDomain(params.domainId);
- if (!domain) return null;
- const title = params.title.trim();
- const sections: ExpertiseLessonSection[] = [{ body: title, key: 'rule' }];
- for (const key of ['why', 'how', 'limits'] as const) {
- const body = params[key]?.trim();
- if (body) sections.push({ body, key });
- }
-
- return this.db.transaction(async (tx) => {
- const code = await this.nextLessonCode(tx, params.domainId);
- const [first] = await tx
- .select({ sortOrder: sql<number>`min(${expertiseLessons.sortOrder})` })
- .from(expertiseLessons)
- .where(eq(expertiseLessons.domainId, params.domainId));
- const [row] = await tx
- .insert(expertiseLessons)
- .values({
- code,
- compilability: params.compilability ?? 'not-compilable',
- createdByUserId: this.userId,
- direction: params.direction ?? null,
- domainId: params.domainId,
- enforcement: params.enforcement ?? 'remind',
- originRunId: params.originRunId,
- polarity: 'rule',
- reasonKind: 'taste',
- reasonSource: 'reviewer',
- sections,
- sortOrder: (first?.sortOrder ?? 1) - 1,
- title,
- })
- .returning({ code: expertiseLessons.code, id: expertiseLessons.id });
- return row;
- });
- };
-
- /**
- * One reading of a material the reviewer brought, as a run on one domain. `reflectionKey`
- * starts with `material:` so counts and sources can tell it apart from rejections and
- * conversations the system observed on its own. Returns null, writing nothing, when the domain
- * is not one the caller may file into.
- */
- insertMaterialRun = async (params: {
- domainId: string;
- reflectionKey: string;
- subjectId: string;
- subjectType: 'document' | 'standalone' | 'topic';
- }) => {
- // The domain row lock queues this behind any other writer numbering runs in the same domain
- // (another distillation, background ingestion); otherwise both read the same maximum and the
- // unique `(domain_id, run_index)` index aborts one whole save. Held until the caller commits.
- // Read in the caller's scope, so a foreign domain id is neither locked nor numbered.
- const [domain] = await this.db
- .select({ id: expertiseDomains.id })
- .from(expertiseDomains)
- .where(and(eq(expertiseDomains.id, params.domainId), this.scopeWhere()))
- .for('update');
- if (!domain) return null;
- const [prior] = await this.db
- .select({ value: sql<number | null>`max(${expertiseRuns.runIndex})` })
- .from(expertiseRuns)
- .where(eq(expertiseRuns.domainId, params.domainId));
- const [row] = await this.db
- .insert(expertiseRuns)
- .values({
- actorId: this.userId,
- actorType: 'user',
- completedAt: new Date(),
- domainId: params.domainId,
- hadHumanInLoop: true,
- reflectionKey: params.reflectionKey,
- runIndex: (prior?.value ?? 0) + 1,
- subjectId: params.subjectId,
- subjectType: params.subjectType,
- userId: this.userId,
- workspaceId: this.workspaceId,
- })
- .returning({ id: expertiseRuns.id });
- return row.id;
- };
-
- /**
- * The passage of a material a rule was read from. It is evidence that the material states the
- * rule, not a violation, so it does not add to `hitCount`.
- */
- insertMaterialHit = async (params: {
- domainId: string;
- lessonId: string;
- quote: string;
- runId: string;
- where: string;
- }) => {
- const [row] = await this.db
- .insert(expertiseHits)
- .values({
- domainId: params.domainId,
- example: params.quote || null,
- lessonId: params.lessonId,
- outcome: 'pass',
- runId: params.runId,
- where: params.where,
- })
- .returning({ id: expertiseHits.id });
- return row.id;
- };
-
- /**
- * The same row `findLesson` returns, read under a row lock for the rest of the caller's
- * transaction. Anything computed from it β€” the next revision number, a section patch, merged
- * counts β€” stays valid until commit, so a concurrent edit waits instead of being overwritten.
- */
- lockLesson = async (lessonId: string) => {
- const [row] = await this.db
- .select({ lesson: expertiseLessons })
- .from(expertiseLessons)
- .innerJoin(expertiseDomains, eq(expertiseDomains.id, expertiseLessons.domainId))
- .where(and(eq(expertiseLessons.id, lessonId), this.scopeWhere()))
- .for('update', { of: expertiseLessons })
- .limit(1);
- return row?.lesson;
- };
-
- /** Appends one entry to a lesson's edit history. */
- insertLessonRevision = async (values: typeof expertiseLessonRevisions.$inferInsert) => {
- await this.db.insert(expertiseLessonRevisions).values(values);
- };
-
- /** Writes fields of one lesson row; `updatedAt` is always refreshed. */
- updateLessonFields = async (
- lessonId: string,
- fields: Partial<typeof expertiseLessons.$inferInsert>,
- ) => {
- await this.db
- .update(expertiseLessons)
- .set({ ...fields, updatedAt: new Date() })
- .where(eq(expertiseLessons.id, lessonId));
- };
-
- /**
- * Writes judged directions onto rules that still have none. A rule the reviewer flipped while
- * the judgment was in flight keeps their choice: only null rows are filled.
- */
- fillLessonDirections = async (judged: { direction: ExpertiseRuleDirection; id: string }[]) => {
- for (const direction of EXPERTISE_RULE_DIRECTIONS) {
- const ids = judged.filter((rule) => rule.direction === direction).map((rule) => rule.id);
- if (ids.length === 0) continue;
- await this.db
- .update(expertiseLessons)
- .set({ direction, updatedAt: new Date() })
- .where(and(inArray(expertiseLessons.id, ids), isNull(expertiseLessons.direction)));
- }
- };
-
- /**
- * Distinct runs a lesson has been proven in across its whole lineage. Runs are "distinct
- * situations", so two merged rules hit in the same delivery count it once.
- */
- countLineageRuns = async (lessonId: string) => {
- const lineage = await this.resolveLineage(lessonId);
- if (lineage.length === 0) return 0;
- const [row] = await this.db
- .select({ runs: sql<number>`count(distinct ${expertiseHits.runId})::int` })
- .from(expertiseHits)
- .where(inArray(expertiseHits.lessonId, lineage));
- return row?.runs ?? 0;
- };
-
- /**
- * Applies one drag inside a group. Only that group's active rules are renumbered; a rule from
- * elsewhere is refused rather than pulled across, because moving between groups changes the
- * code and goes through `moveRule`.
- */
- reorderRule = async (domainId: string, lessonId: string, beforeId: string | null) => {
- const domain = await this.findDomain(domainId);
- if (!domain) return null;
- return this.db.transaction(async (tx) => {
- // The server's own order, not a list sent by the client: a stale or partial client view
- // cannot scramble the group, and the request stays one id no matter how large it grows.
- const rows = await tx
- .select({ id: expertiseLessons.id, sortOrder: expertiseLessons.sortOrder })
- .from(expertiseLessons)
- .where(and(eq(expertiseLessons.domainId, domainId), eq(expertiseLessons.status, 'active')))
- .orderBy(
- sql`${expertiseLessons.sortOrder} asc nulls last`,
- asc(expertiseLessons.createdAt),
- );
- if (!rows.some((row) => row.id === lessonId)) return null;
-
- const order = rows.map((row) => row.id).filter((id) => id !== lessonId);
- const at = beforeId ? order.indexOf(beforeId) : -1;
- order.splice(at === -1 ? order.length : at, 0, lessonId);
-
- const current = new Map(rows.map((row) => [row.id, row.sortOrder]));
- for (const [index, id] of order.entries()) {
- if (current.get(id) === index) continue;
- await tx
- .update(expertiseLessons)
- .set({ sortOrder: index, updatedAt: new Date() })
- .where(eq(expertiseLessons.id, id));
- }
- return { domainId, order };
- });
- };
-
- /**
- * Files a rule under another of the reviewer's groups, at the end, with a fresh code there.
- *
- * A rule with no evidence simply changes domain. One with evidence cannot: its hits are pinned
- * to the run and domain that produced them, so the rule is re-created in the target group with
- * `salvagedFromId` pointing at the original, and the original is hidden (`rejected`, not
- * archived β€” it did not stop applying, it moved). The caller gets the id that is now live.
- */
- moveRule = async (lessonId: string, domainId: string) => {
- const [lesson, domain] = await Promise.all([
- this.findLesson(lessonId),
- this.findDomain(domainId),
- ]);
- if (!lesson || !domain) return null;
- if (lesson.domainId === domainId) return { domainId, id: lessonId };
- // Older clients list every group in the move menu; the boundary has to hold here too.
- if (!(await this.sameReach(lesson.domainId, domainId))) return null;
-
- return this.db.transaction(async (tx) => {
- // Re-read under a row lock and copy from that: a merge committed since the read above has
- // already folded this rule into another one, and copying it now would leave the same
- // evidence live twice.
- const [current] = await tx
- .select()
- .from(expertiseLessons)
- .where(eq(expertiseLessons.id, lessonId))
- .for('update');
- if (current?.status !== 'active' || current.domainId !== lesson.domainId) return null;
-
- const code = await this.nextLessonCode(tx, domainId);
- const [last] = await tx
- .select({ sortOrder: sql<number>`max(${expertiseLessons.sortOrder})` })
- .from(expertiseLessons)
- .where(eq(expertiseLessons.domainId, domainId));
- const sortOrder = (last?.sortOrder ?? -1) + 1;
- const [{ hits }] = await tx
- .select({ hits: sql<number>`count(*)::int` })
- .from(expertiseHits)
- .where(eq(expertiseHits.lessonId, lessonId));
-
- if (hits === 0) {
- await tx
- .update(expertiseLessons)
- .set({ code, domainId, sortOrder, updatedAt: new Date() })
- .where(eq(expertiseLessons.id, lessonId));
- return { domainId, id: lessonId };
- }
-
- const {
- createdAt: _createdAt,
- id: _id,
- updatedAt: _updatedAt,
- accessedAt: _accessedAt,
- ...carried
- } = current;
- const [copy] = await tx
- .insert(expertiseLessons)
- .values({ ...carried, code, domainId, salvagedFromId: lessonId, sortOrder })
- .returning({ id: expertiseLessons.id });
- await tx
- .update(expertiseLessons)
- .set({ rejectedReason: `moved-to:${copy.id}`, status: 'rejected', updatedAt: new Date() })
- .where(eq(expertiseLessons.id, lessonId));
- return { domainId, id: copy.id };
- });
- };
-
- /**
- * A group the reviewer opens by hand. It is a plain always-on domain: the gate question is the
- * domain filter, and it mounts on the reviewer themselves so every acceptance without a project
- * can add to it.
- *
- * A name the reviewer already uses returns that group instead of opening a second one. The
- * drafting model proposes a group name without seeing which ones already exist in every case,
- * and two groups with the same name on one page are indistinguishable to the reader.
- */
- createRuleGroup = async (params: { gate: string; outOfScope?: string; title: string }) => {
- const title = params.title.trim();
- const existing = await this.listDomainsForOwner();
- const match = existing.find(
- ({ domain }) => domain.title.trim().toLowerCase() === title.toLowerCase(),
- );
- if (match) return match.domain.id;
-
- return this.createDomain({
- brief: title,
- carrier: { type: 'user' },
- domainFilter: params.gate,
- outOfScope: params.outOfScope,
- title,
- });
- };
-
- /** Renames a group; the gate question can be changed the same way. */
- updateRuleGroup = async (
- domainId: string,
- patch: { gate?: string; outOfScope?: string | null; title?: string },
- ) => {
- const domain = await this.findDomain(domainId);
- if (!domain) return null;
- const title = patch.title?.trim();
- const gate = patch.gate?.trim();
- await this.db
- .update(expertiseDomains)
- .set({
- ...(title && { title }),
- ...(gate && { domainFilter: gate }),
- // Empty clears the exclusion; undefined leaves it alone.
- ...(patch.outOfScope !== undefined && { outOfScope: patch.outOfScope?.trim() || null }),
- updatedAt: new Date(),
- })
- .where(eq(expertiseDomains.id, domainId));
- return { id: domainId };
- };
-
- /** Retires a lesson so it stops being practiced; the record and its evidence are kept. */
- retireLesson = async (lessonId: string) => {
- const lesson = await this.findLesson(lessonId);
- if (!lesson) return null;
- await this.db
- .update(expertiseLessons)
- .set({ retiredAt: new Date(), status: 'retired', updatedAt: new Date() })
- .where(eq(expertiseLessons.id, lessonId));
- return { id: lessonId };
- };
-
- // Insights
-
- listInsights = async (domainIds: string[]) => {
- if (domainIds.length === 0) return [];
- return this.db
- .select()
- .from(expertiseInsights)
- .where(
- and(
- or(inArray(expertiseInsights.domainId, domainIds), isNull(expertiseInsights.domainId)),
- eq(expertiseInsights.status, 'active'),
- this.insightScopeWhere(),
- ),
- )
- .orderBy(desc(expertiseInsights.confidence))
- .limit(10);
- };
-
- dismissInsight = async (insightId: string, reason?: string) =>
- this.db
- .update(expertiseInsights)
- .set({ dismissReason: reason, status: 'dismissed', updatedAt: new Date() })
- .where(and(eq(expertiseInsights.id, insightId), this.insightScopeWhere()));
-}
diff --git a/packages/database/src/models/expertise/context.ts b/packages/database/src/models/expertise/context.ts
new file mode 100644
index 00000000000..8af9766c1f2
--- /dev/null
+++ b/packages/database/src/models/expertise/context.ts
@@ -0,0 +1,28 @@
+import { expertiseDomains } from '../../schemas';
+import type { LobeChatDatabase } from '../../type';
+import { buildWorkspaceWhere } from '../../utils/workspace';
+import type { ExpertiseModel } from './index';
+
+/** The caller identity every `ExpertiseModel` query is scoped by. */
+export interface ExpertiseContext {
+ db: LobeChatDatabase;
+ /**
+ * The model instance itself. A query that builds on another public method calls it through
+ * here, so the call still dispatches through the instance as `this.<method>` did.
+ */
+ model: Pick<
+ ExpertiseModel,
+ 'createDomain' | 'findDomain' | 'findLesson' | 'listDomainsForOwner' | 'sameReach'
+ >;
+ userId: string;
+ workspaceId?: string;
+}
+
+/** The parameters of a module function after its leading `ctx`, for the model's delegators. */
+export type ExpertiseArgs<F> = F extends (ctx: ExpertiseContext, ...args: infer A) => unknown
+ ? A
+ : never;
+
+/** Confines a query to the domains the caller owns: their own, or the active workspace's. */
+export const domainScopeWhere = (ctx: ExpertiseContext) =>
+ buildWorkspaceWhere({ userId: ctx.userId, workspaceId: ctx.workspaceId }, expertiseDomains);
diff --git a/packages/database/src/models/expertise/domains.ts b/packages/database/src/models/expertise/domains.ts
new file mode 100644
index 00000000000..e3e37b94767
--- /dev/null
+++ b/packages/database/src/models/expertise/domains.ts
@@ -0,0 +1,257 @@
+import {
+ type ExpertiseAnchorCandidate,
+ type ExpertiseCanonEntry,
+ type ExpertiseLayerDefinition,
+} from '@lobechat/types';
+import { and, asc, eq, isNotNull, or, type SQL, sql } from 'drizzle-orm';
+
+import { expertiseBindings, expertiseDomains } from '../../schemas';
+import { idGenerator } from '../../utils/idGenerator';
+import { domainScopeWhere, type ExpertiseContext } from './context';
+
+/**
+ * Where a domain is mounted. Ownership (`expertise_domains.user_id` / `workspace_id`) is a
+ * separate axis: a project never owns a domain, it only mounts one, so the same standard can be
+ * mounted by several projects at once.
+ */
+export type ExpertiseCarrier =
+ { id: string; type: 'agent' } | { id: string; type: 'project' } | { type: 'user' };
+
+/**
+ * The single carrier column a binding sets. `user` resolves to the workspace when one is in
+ * scope, so a workspace member's always-on standards reach their teammates rather than only
+ * themselves β€” the same arm `listDomainsBoundTo` reads back.
+ */
+const carrierColumns = (
+ carrier: ExpertiseCarrier,
+ owner: { userId: string; workspaceId?: string },
+) => {
+ if (carrier.type === 'agent') return { agentId: carrier.id };
+ if (carrier.type === 'project') return { projectId: carrier.id };
+ return owner.workspaceId
+ ? { boundWorkspaceId: owner.workspaceId }
+ : { boundUserId: owner.userId };
+};
+
+export const listDomainsBoundTo = async (ctx: ExpertiseContext, carrierWhere?: SQL) => {
+ const ownerWhere = ctx.workspaceId
+ ? eq(expertiseBindings.boundWorkspaceId, ctx.workspaceId)
+ : eq(expertiseBindings.boundUserId, ctx.userId);
+
+ const rows = await ctx.db
+ .select({
+ binding: {
+ contributionMode: expertiseBindings.contributionMode,
+ enabled: expertiseBindings.enabled,
+ id: expertiseBindings.id,
+ sortOrder: expertiseBindings.sortOrder,
+ },
+ domain: expertiseDomains,
+ })
+ .from(expertiseBindings)
+ .innerJoin(expertiseDomains, eq(expertiseDomains.id, expertiseBindings.domainId))
+ .where(
+ and(
+ eq(expertiseBindings.enabled, true),
+ isNotNull(expertiseDomains.anchorChosenAt),
+ domainScopeWhere(ctx),
+ carrierWhere ? or(carrierWhere, ownerWhere) : ownerWhere,
+ ),
+ )
+ // Every binding starts at sortOrder 0, so without a tiebreak the order of a reviewer's
+ // groups is whatever the planner returns and a newly opened group can jump to the top.
+ .orderBy(asc(expertiseBindings.sortOrder), asc(expertiseDomains.createdAt));
+
+ // One domain may be bound at multiple levels; retain the first binding by sort order.
+ const seen = new Set<string>();
+ return rows.filter((r) => {
+ if (seen.has(r.domain.id)) return false;
+ seen.add(r.domain.id);
+ return true;
+ });
+};
+
+/**
+ * Who a domain reaches, as one comparable key β€” the same rule `listRules` uses for its
+ * `owner`: `mine` when any enabled binding is not an agent's (the reviewer, workspace or a
+ * project), otherwise the agents it is mounted on. A rule moved or merged across reaches would
+ * silently change which runs receive it, so both mutations refuse that.
+ */
+const reachOf = async (ctx: ExpertiseContext, domainId: string) => {
+ const mounts = await ctx.db
+ .select({ agentId: expertiseBindings.agentId })
+ .from(expertiseBindings)
+ .where(and(eq(expertiseBindings.domainId, domainId), eq(expertiseBindings.enabled, true)));
+ if (mounts.some((mount) => !mount.agentId)) return 'mine';
+ const agentIds = [...new Set(mounts.map((mount) => mount.agentId!))].sort();
+ return agentIds.length > 0 ? `agent:${agentIds.join(',')}` : 'unbound';
+};
+
+/** Whether two domains reach the same runs; see {@link reachOf}. */
+export const sameReach = async (ctx: ExpertiseContext, domainA: string, domainB: string) => {
+ if (domainA === domainB) return true;
+ const [a, b] = await Promise.all([reachOf(ctx, domainA), reachOf(ctx, domainB)]);
+ return a === b;
+};
+
+export const findDomain = async (ctx: ExpertiseContext, domainId: string) => {
+ const [row] = await ctx.db
+ .select()
+ .from(expertiseDomains)
+ .where(
+ and(
+ eq(expertiseDomains.id, domainId),
+ isNotNull(expertiseDomains.anchorChosenAt),
+ domainScopeWhere(ctx),
+ ),
+ )
+ .limit(1);
+ return row;
+};
+
+/**
+ * Persists a reviewed anchor and mounts it on the selected carrier.
+ * The chosen candidate is also kept in anchorCandidates so the alternative can be revisited.
+ */
+export const createDomain = async (
+ ctx: ExpertiseContext,
+ params: {
+ brief: string;
+ canonEntries?: ExpertiseCanonEntry[];
+ carrier: ExpertiseCarrier;
+ domainFilter: string;
+ layerCanonRef?: string;
+ layerSource?: 'canonical' | 'invented';
+ layers?: ExpertiseLayerDefinition[];
+ outOfScope?: string;
+ rationale?: string;
+ title: string;
+ },
+) => {
+ const brief = params.brief.trim();
+ const id = idGenerator('expertiseDomains');
+ const title = params.title.trim();
+ const domainFilter = params.domainFilter.trim();
+ const slug = `${title.slice(0, 40).replaceAll(/\s+/g, '-').toLowerCase()}-${id.slice(-6)}`;
+ const layers = params.layers ?? [];
+ const canonEntries = params.canonEntries ?? [];
+ const layerSource = params.layerSource ?? 'invented';
+ const candidate: ExpertiseAnchorCandidate = {
+ canonEntries,
+ domainFilter,
+ key: 'chosen',
+ layerCanonRef: params.layerCanonRef,
+ layers,
+ layerSource,
+ outOfScope: params.outOfScope?.trim() || undefined,
+ rationale: params.rationale?.trim() || undefined,
+ title,
+ };
+
+ await ctx.db.transaction(async (tx) => {
+ await tx.insert(expertiseDomains).values({
+ anchorCandidates: [candidate],
+ anchorChosenAt: new Date(),
+ anchorChosenByUserId: ctx.userId,
+ canonEntries,
+ description: brief,
+ domainFilter,
+ id,
+ layerSource,
+ layers,
+ outOfScope: params.outOfScope?.trim() || null,
+ seedState: 'seeded',
+ slug,
+ title,
+ userId: ctx.userId,
+ workspaceId: ctx.workspaceId,
+ });
+ const carrier = carrierColumns(params.carrier, {
+ userId: ctx.userId,
+ workspaceId: ctx.workspaceId,
+ });
+ // A new mount goes after the ones already there. Leaving every binding at 0 makes the
+ // reviewer's group order depend on the query plan.
+ const [carrierColumn, carrierValue] = Object.entries(carrier)[0] as [
+ keyof typeof expertiseBindings.$inferInsert,
+ string,
+ ];
+ const [last] = await tx
+ .select({ sortOrder: sql<number>`max(${expertiseBindings.sortOrder})` })
+ .from(expertiseBindings)
+ .where(eq(expertiseBindings[carrierColumn as 'boundUserId'], carrierValue));
+ await tx.insert(expertiseBindings).values({
+ addedByUserId: ctx.userId,
+ domainId: id,
+ sortOrder: (last?.sortOrder ?? -1) + 1,
+ workspaceId: ctx.workspaceId,
+ ...carrier,
+ });
+ });
+ return id;
+};
+
+/**
+ * Deletes a domain the user owns together with everything learned in it β€” bindings, lessons,
+ * runs, hits, snapshots and insights all cascade from the domain row. Nothing is kept: the
+ * user chose to drop the direction, not to pause it.
+ */
+export const deleteDomain = async (ctx: ExpertiseContext, domainId: string) => {
+ const [row] = await ctx.db
+ .delete(expertiseDomains)
+ .where(and(eq(expertiseDomains.id, domainId), domainScopeWhere(ctx)))
+ .returning({ id: expertiseDomains.id });
+ return row ?? null;
+};
+
+/**
+ * A group the reviewer opens by hand. It is a plain always-on domain: the gate question is the
+ * domain filter, and it mounts on the reviewer themselves so every acceptance without a project
+ * can add to it.
+ *
+ * A name the reviewer already uses returns that group instead of opening a second one. The
+ * drafting model proposes a group name without seeing which ones already exist in every case,
+ * and two groups with the same name on one page are indistinguishable to the reader.
+ */
+export const createRuleGroup = async (
+ ctx: ExpertiseContext,
+ params: { gate: string; outOfScope?: string; title: string },
+) => {
+ const title = params.title.trim();
+ const existing = await ctx.model.listDomainsForOwner();
+ const match = existing.find(
+ ({ domain }) => domain.title.trim().toLowerCase() === title.toLowerCase(),
+ );
+ if (match) return match.domain.id;
+
+ return ctx.model.createDomain({
+ brief: title,
+ carrier: { type: 'user' },
+ domainFilter: params.gate,
+ outOfScope: params.outOfScope,
+ title,
+ });
+};
+
+/** Renames a group; the gate question can be changed the same way. */
+export const updateRuleGroup = async (
+ ctx: ExpertiseContext,
+ domainId: string,
+ patch: { gate?: string; outOfScope?: string | null; title?: string },
+) => {
+ const domain = await ctx.model.findDomain(domainId);
+ if (!domain) return null;
+ const title = patch.title?.trim();
+ const gate = patch.gate?.trim();
+ await ctx.db
+ .update(expertiseDomains)
+ .set({
+ ...(title && { title }),
+ ...(gate && { domainFilter: gate }),
+ // Empty clears the exclusion; undefined leaves it alone.
+ ...(patch.outOfScope !== undefined && { outOfScope: patch.outOfScope?.trim() || null }),
+ updatedAt: new Date(),
+ })
+ .where(eq(expertiseDomains.id, domainId));
+ return { id: domainId };
+};
diff --git a/packages/database/src/models/expertise/filing.ts b/packages/database/src/models/expertise/filing.ts
new file mode 100644
index 00000000000..11e810bd1ac
--- /dev/null
+++ b/packages/database/src/models/expertise/filing.ts
@@ -0,0 +1,277 @@
+import {
+ type ExpertiseEnforcement,
+ type ExpertiseLessonSection,
+ type ExpertiseRuleDirection,
+} from '@lobechat/types';
+import { and, asc, eq, sql } from 'drizzle-orm';
+
+import { expertiseDomains, expertiseHits, expertiseLessons, expertiseRuns } from '../../schemas';
+import type { LobeChatDatabase } from '../../type';
+import { domainScopeWhere, type ExpertiseContext } from './context';
+
+/**
+ * The next free `P-nn` code inside one domain; codes are unique per domain, not globally.
+ *
+ * Takes the domain row lock first β€” the same lock distillation holds while it hands out codes β€”
+ * so two writers in one group queue up instead of computing the same number and having the
+ * unique index reject one of them. The lock lasts until the caller's transaction commits.
+ */
+const nextLessonCode = async (
+ tx: Pick<LobeChatDatabase, 'select'>,
+ domainId: string,
+): Promise<string> => {
+ await tx
+ .select({ id: expertiseDomains.id })
+ .from(expertiseDomains)
+ .where(eq(expertiseDomains.id, domainId))
+ .for('update');
+ const codes = await tx
+ .select({ code: expertiseLessons.code })
+ .from(expertiseLessons)
+ .where(eq(expertiseLessons.domainId, domainId));
+ const next =
+ Math.max(0, ...codes.map(({ code }) => Number(/^P-(\d+)$/.exec(code)?.[1] ?? 0))) + 1;
+ return `P-${String(next).padStart(2, '0')}`;
+};
+
+/**
+ * A rule the reviewer writes down themselves, filed at the top of its group so they see it
+ * where they just put it. Hand-written rules start as `taste` from the reviewer: nothing has
+ * been distilled, so there is no mechanism to claim yet.
+ */
+export const createRule = async (
+ ctx: ExpertiseContext,
+ params: {
+ compilability?: 'compilable' | 'not-compilable';
+ direction?: ExpertiseRuleDirection;
+ domainId: string;
+ enforcement?: ExpertiseEnforcement;
+ how?: string;
+ limits?: string;
+ /**
+ * The run it was read from, when it was distilled from a material rather than typed. It also
+ * keeps the row from reading as "you wrote it", which is decided by the absence of an origin.
+ */
+ originRunId?: string;
+ title: string;
+ why?: string;
+ },
+) => {
+ const domain = await ctx.model.findDomain(params.domainId);
+ if (!domain) return null;
+ const title = params.title.trim();
+ const sections: ExpertiseLessonSection[] = [{ body: title, key: 'rule' }];
+ for (const key of ['why', 'how', 'limits'] as const) {
+ const body = params[key]?.trim();
+ if (body) sections.push({ body, key });
+ }
+
+ return ctx.db.transaction(async (tx) => {
+ const code = await nextLessonCode(tx, params.domainId);
+ const [first] = await tx
+ .select({ sortOrder: sql<number>`min(${expertiseLessons.sortOrder})` })
+ .from(expertiseLessons)
+ .where(eq(expertiseLessons.domainId, params.domainId));
+ const [row] = await tx
+ .insert(expertiseLessons)
+ .values({
+ code,
+ compilability: params.compilability ?? 'not-compilable',
+ createdByUserId: ctx.userId,
+ direction: params.direction ?? null,
+ domainId: params.domainId,
+ enforcement: params.enforcement ?? 'remind',
+ originRunId: params.originRunId,
+ polarity: 'rule',
+ reasonKind: 'taste',
+ reasonSource: 'reviewer',
+ sections,
+ sortOrder: (first?.sortOrder ?? 1) - 1,
+ title,
+ })
+ .returning({ code: expertiseLessons.code, id: expertiseLessons.id });
+ return row;
+ });
+};
+
+/**
+ * One reading of a material the reviewer brought, as a run on one domain. `reflectionKey`
+ * starts with `material:` so counts and sources can tell it apart from rejections and
+ * conversations the system observed on its own. Returns null, writing nothing, when the domain
+ * is not one the caller may file into.
+ */
+export const insertMaterialRun = async (
+ ctx: ExpertiseContext,
+ params: {
+ domainId: string;
+ reflectionKey: string;
+ subjectId: string;
+ subjectType: 'document' | 'standalone' | 'topic';
+ },
+) => {
+ // The domain row lock queues this behind any other writer numbering runs in the same domain
+ // (another distillation, background ingestion); otherwise both read the same maximum and the
+ // unique `(domain_id, run_index)` index aborts one whole save. Held until the caller commits.
+ // Read in the caller's scope, so a foreign domain id is neither locked nor numbered.
+ const [domain] = await ctx.db
+ .select({ id: expertiseDomains.id })
+ .from(expertiseDomains)
+ .where(and(eq(expertiseDomains.id, params.domainId), domainScopeWhere(ctx)))
+ .for('update');
+ if (!domain) return null;
+ const [prior] = await ctx.db
+ .select({ value: sql<number | null>`max(${expertiseRuns.runIndex})` })
+ .from(expertiseRuns)
+ .where(eq(expertiseRuns.domainId, params.domainId));
+ const [row] = await ctx.db
+ .insert(expertiseRuns)
+ .values({
+ actorId: ctx.userId,
+ actorType: 'user',
+ completedAt: new Date(),
+ domainId: params.domainId,
+ hadHumanInLoop: true,
+ reflectionKey: params.reflectionKey,
+ runIndex: (prior?.value ?? 0) + 1,
+ subjectId: params.subjectId,
+ subjectType: params.subjectType,
+ userId: ctx.userId,
+ workspaceId: ctx.workspaceId,
+ })
+ .returning({ id: expertiseRuns.id });
+ return row.id;
+};
+
+/**
+ * The passage of a material a rule was read from. It is evidence that the material states the
+ * rule, not a violation, so it does not add to `hitCount`.
+ */
+export const insertMaterialHit = async (
+ ctx: ExpertiseContext,
+ params: {
+ domainId: string;
+ lessonId: string;
+ quote: string;
+ runId: string;
+ where: string;
+ },
+) => {
+ const [row] = await ctx.db
+ .insert(expertiseHits)
+ .values({
+ domainId: params.domainId,
+ example: params.quote || null,
+ lessonId: params.lessonId,
+ outcome: 'pass',
+ runId: params.runId,
+ where: params.where,
+ })
+ .returning({ id: expertiseHits.id });
+ return row.id;
+};
+
+/**
+ * Applies one drag inside a group. Only that group's active rules are renumbered; a rule from
+ * elsewhere is refused rather than pulled across, because moving between groups changes the
+ * code and goes through `moveRule`.
+ */
+export const reorderRule = async (
+ ctx: ExpertiseContext,
+ domainId: string,
+ lessonId: string,
+ beforeId: string | null,
+) => {
+ const domain = await ctx.model.findDomain(domainId);
+ if (!domain) return null;
+ return ctx.db.transaction(async (tx) => {
+ // The server's own order, not a list sent by the client: a stale or partial client view
+ // cannot scramble the group, and the request stays one id no matter how large it grows.
+ const rows = await tx
+ .select({ id: expertiseLessons.id, sortOrder: expertiseLessons.sortOrder })
+ .from(expertiseLessons)
+ .where(and(eq(expertiseLessons.domainId, domainId), eq(expertiseLessons.status, 'active')))
+ .orderBy(sql`${expertiseLessons.sortOrder} asc nulls last`, asc(expertiseLessons.createdAt));
+ if (!rows.some((row) => row.id === lessonId)) return null;
+
+ const order = rows.map((row) => row.id).filter((id) => id !== lessonId);
+ const at = beforeId ? order.indexOf(beforeId) : -1;
+ order.splice(at === -1 ? order.length : at, 0, lessonId);
+
+ const current = new Map(rows.map((row) => [row.id, row.sortOrder]));
+ for (const [index, id] of order.entries()) {
+ if (current.get(id) === index) continue;
+ await tx
+ .update(expertiseLessons)
+ .set({ sortOrder: index, updatedAt: new Date() })
+ .where(eq(expertiseLessons.id, id));
+ }
+ return { domainId, order };
+ });
+};
+
+/**
+ * Files a rule under another of the reviewer's groups, at the end, with a fresh code there.
+ *
+ * A rule with no evidence simply changes domain. One with evidence cannot: its hits are pinned
+ * to the run and domain that produced them, so the rule is re-created in the target group with
+ * `salvagedFromId` pointing at the original, and the original is hidden (`rejected`, not
+ * archived β€” it did not stop applying, it moved). The caller gets the id that is now live.
+ */
+export const moveRule = async (ctx: ExpertiseContext, lessonId: string, domainId: string) => {
+ const [lesson, domain] = await Promise.all([
+ ctx.model.findLesson(lessonId),
+ ctx.model.findDomain(domainId),
+ ]);
+ if (!lesson || !domain) return null;
+ if (lesson.domainId === domainId) return { domainId, id: lessonId };
+ // Older clients list every group in the move menu; the boundary has to hold here too.
+ if (!(await ctx.model.sameReach(lesson.domainId, domainId))) return null;
+
+ return ctx.db.transaction(async (tx) => {
+ // Re-read under a row lock and copy from that: a merge committed since the read above has
+ // already folded this rule into another one, and copying it now would leave the same
+ // evidence live twice.
+ const [current] = await tx
+ .select()
+ .from(expertiseLessons)
+ .where(eq(expertiseLessons.id, lessonId))
+ .for('update');
+ if (current?.status !== 'active' || current.domainId !== lesson.domainId) return null;
+
+ const code = await nextLessonCode(tx, domainId);
+ const [last] = await tx
+ .select({ sortOrder: sql<number>`max(${expertiseLessons.sortOrder})` })
+ .from(expertiseLessons)
+ .where(eq(expertiseLessons.domainId, domainId));
+ const sortOrder = (last?.sortOrder ?? -1) + 1;
+ const [{ hits }] = await tx
+ .select({ hits: sql<number>`count(*)::int` })
+ .from(expertiseHits)
+ .where(eq(expertiseHits.lessonId, lessonId));
+
+ if (hits === 0) {
+ await tx
+ .update(expertiseLessons)
+ .set({ code, domainId, sortOrder, updatedAt: new Date() })
+ .where(eq(expertiseLessons.id, lessonId));
+ return { domainId, id: lessonId };
+ }
+
+ const {
+ createdAt: _createdAt,
+ id: _id,
+ updatedAt: _updatedAt,
+ accessedAt: _accessedAt,
+ ...carried
+ } = current;
+ const [copy] = await tx
+ .insert(expertiseLessons)
+ .values({ ...carried, code, domainId, salvagedFromId: lessonId, sortOrder })
+ .returning({ id: expertiseLessons.id });
+ await tx
+ .update(expertiseLessons)
+ .set({ rejectedReason: `moved-to:${copy.id}`, status: 'rejected', updatedAt: new Date() })
+ .where(eq(expertiseLessons.id, lessonId));
+ return { domainId, id: copy.id };
+ });
+};
diff --git a/packages/database/src/models/expertise/index.ts b/packages/database/src/models/expertise/index.ts
new file mode 100644
index 00000000000..b2f4f63cda3
--- /dev/null
+++ b/packages/database/src/models/expertise/index.ts
@@ -0,0 +1,473 @@
+import { and, asc, desc, eq, inArray, isNull, or, sql } from 'drizzle-orm';
+
+import {
+ agents,
+ expertiseBindings,
+ expertiseDomains,
+ expertiseDomainSnapshots,
+ expertiseHits,
+ expertiseInsights,
+ expertiseLessons,
+ expertiseRuns,
+ topics,
+} from '../../schemas';
+import type { LobeChatDatabase } from '../../type';
+import { buildWorkspaceWhere } from '../../utils/workspace';
+import { domainScopeWhere, type ExpertiseArgs, type ExpertiseContext } from './context';
+import * as domains from './domains';
+import * as filing from './filing';
+import * as lessons from './lessons';
+import * as lineage from './lineage';
+import * as rules from './rules';
+
+export type { ExpertiseCarrier } from './domains';
+export type { ExpertiseGroupOwner } from './rules';
+
+/** The core tier starts at 40% of the domain's maximum hit count, with a floor of two. */
+const CORE_CUT_RATIO = 0.4;
+const CORE_CUT_MIN = 2;
+
+export type ExpertiseTier = 'core' | 'niche' | 'unused';
+
+/**
+ * Runs that observed practice: a material the reviewer distilled (`reflectionKey` `material:…`)
+ * records the passages a rule was read from, which are not outcomes of applying it.
+ */
+const notMaterialRun = () =>
+ sql`coalesce(${expertiseRuns.reflectionKey}, '') not like 'material:%'`;
+
+export class ExpertiseModel {
+ private db: LobeChatDatabase;
+ private userId: string;
+ private workspaceId?: string;
+ private ctx: ExpertiseContext;
+
+ constructor(db: LobeChatDatabase, userId: string, workspaceId?: string) {
+ this.db = db;
+ this.userId = userId;
+ this.workspaceId = workspaceId;
+ this.ctx = { db, model: this, userId, workspaceId };
+ }
+
+ private scopeWhere = () => domainScopeWhere(this.ctx);
+
+ private insightScopeWhere = () =>
+ buildWorkspaceWhere({ userId: this.userId, workspaceId: this.workspaceId }, expertiseInsights);
+
+ // Binding resolution
+
+ /** Lists the agent-level and workspace-level expertise available to one authorized agent. */
+ listDomainsForAgent = async (agentId: string) => {
+ const [agent] = await this.db
+ .select({ id: agents.id })
+ .from(agents)
+ .where(
+ and(
+ eq(agents.id, agentId),
+ buildWorkspaceWhere({ userId: this.userId, workspaceId: this.workspaceId }, agents),
+ ),
+ )
+ .limit(1);
+ if (!agent) return [];
+
+ return this.listDomainsBoundTo(eq(expertiseBindings.agentId, agentId));
+ };
+
+ /**
+ * Lists the expertise mounted on one project, plus the caller's own always-on domains.
+ *
+ * Deliberately does not verify the project row: the caller reaches this through an acceptance
+ * it already owns, and `scopeWhere` still confines the result to domains in scope.
+ */
+ listDomainsForProject = async (projectId: string) =>
+ this.listDomainsBoundTo(eq(expertiseBindings.projectId, projectId));
+
+ /** The caller's always-on domains only β€” the scope an acceptance without a project falls back to. */
+ listDomainsForOwner = async () => this.listDomainsBoundTo();
+
+ private listDomainsBoundTo = (...args: ExpertiseArgs<typeof domains.listDomainsBoundTo>) =>
+ domains.listDomainsBoundTo(this.ctx, ...args);
+
+ // L0: overview
+
+ /** Returns the latest snapshot for every requested domain. */
+ latestSnapshots = async (domainIds: string[]) => {
+ if (domainIds.length === 0) return [];
+ return this.db
+ .selectDistinctOn([expertiseDomainSnapshots.domainId])
+ .from(expertiseDomainSnapshots)
+ .where(inArray(expertiseDomainSnapshots.domainId, domainIds))
+ .orderBy(desc(expertiseDomainSnapshots.domainId), desc(expertiseDomainSnapshots.runIndex));
+ };
+
+ /** Loads the complete active-lesson series for all requested domains in one query. */
+ seriesForDomains = async (domainIds: string[]) => {
+ if (domainIds.length === 0) return [];
+ return this.db
+ .select({
+ activeCount: expertiseDomainSnapshots.activeCount,
+ domainId: expertiseDomainSnapshots.domainId,
+ runIndex: expertiseDomainSnapshots.runIndex,
+ })
+ .from(expertiseDomainSnapshots)
+ .where(inArray(expertiseDomainSnapshots.domainId, domainIds))
+ .orderBy(asc(expertiseDomainSnapshots.domainId), asc(expertiseDomainSnapshots.runIndex));
+ };
+
+ /**
+ * Per-run pass/violation counts β€” the reliability series behind the "εšε―ΉηŽ‡" chart.
+ * A run without hits produces no row; callers treat missing runs as "nothing to judge".
+ * A material the reviewer distilled is not practice: its quotes are evidence the rule exists,
+ * not a run that got it right, so they never count as passes.
+ */
+ reliabilitySeries = async (domainIds: string[]) => {
+ if (domainIds.length === 0) return [];
+ return this.db
+ .select({
+ domainId: expertiseHits.domainId,
+ pass: sql<number>`count(*) filter (where ${expertiseHits.outcome} = 'pass')::int`,
+ runIndex: expertiseRuns.runIndex,
+ violation: sql<number>`count(*) filter (where ${expertiseHits.outcome} = 'violation')::int`,
+ })
+ .from(expertiseHits)
+ .innerJoin(expertiseRuns, eq(expertiseRuns.id, expertiseHits.runId))
+ .where(and(inArray(expertiseHits.domainId, domainIds), notMaterialRun()))
+ .groupBy(expertiseHits.domainId, expertiseRuns.runIndex)
+ .orderBy(asc(expertiseHits.domainId), asc(expertiseRuns.runIndex));
+ };
+
+ listRules = (...args: ExpertiseArgs<typeof rules.listRules>) =>
+ rules.listRules(this.ctx, ...args);
+
+ listLessonSources = (...args: ExpertiseArgs<typeof lineage.listLessonSources>) =>
+ lineage.listLessonSources(this.ctx, ...args);
+
+ countUndistilledRejectionRounds = (
+ ...args: ExpertiseArgs<typeof rules.countUndistilledRejectionRounds>
+ ) => rules.countUndistilledRejectionRounds(this.ctx, ...args);
+
+ /**
+ * Active lessons for the portrait, each with its most recent hit outcomes (newest last).
+ * Recent outcomes are what the client folds into a reliability tier; the topic title of each
+ * hit lets the row say *where* it last went wrong without another query.
+ * Material quotes are left out for the same reason as in {@link reliabilitySeries}.
+ */
+ listLessonsWithRecent = async (domainIds: string[], recentLimit = 6) => {
+ if (domainIds.length === 0) return [];
+ const lessons = await this.db
+ .select({
+ code: expertiseLessons.code,
+ createdAt: expertiseLessons.createdAt,
+ createdByUserId: expertiseLessons.createdByUserId,
+ domainId: expertiseLessons.domainId,
+ hitCount: expertiseLessons.hitCount,
+ id: expertiseLessons.id,
+ lastHitAt: expertiseLessons.lastHitAt,
+ layer: expertiseLessons.layer,
+ originRunId: expertiseLessons.originRunId,
+ title: expertiseLessons.title,
+ })
+ .from(expertiseLessons)
+ .where(
+ and(inArray(expertiseLessons.domainId, domainIds), eq(expertiseLessons.status, 'active')),
+ )
+ .orderBy(desc(expertiseLessons.hitCount), asc(expertiseLessons.code));
+
+ const ranked = this.db.$with('ranked').as(
+ this.db
+ .select({
+ lessonId: expertiseHits.lessonId,
+ outcome: expertiseHits.outcome,
+ rn: sql<number>`row_number() over (partition by ${expertiseHits.lessonId} order by ${expertiseRuns.runIndex} desc, ${expertiseHits.createdAt} desc)`.as(
+ 'rn',
+ ),
+ runIndex: expertiseRuns.runIndex,
+ subjectId: expertiseRuns.subjectId,
+ subjectType: expertiseRuns.subjectType,
+ })
+ .from(expertiseHits)
+ .innerJoin(expertiseRuns, eq(expertiseRuns.id, expertiseHits.runId))
+ .where(and(inArray(expertiseHits.domainId, domainIds), notMaterialRun())),
+ );
+ const recentRows = await this.db
+ .with(ranked)
+ .select({
+ lessonId: ranked.lessonId,
+ outcome: ranked.outcome,
+ rn: ranked.rn,
+ runIndex: ranked.runIndex,
+ subjectId: ranked.subjectId,
+ subjectTitle: topics.title,
+ subjectType: ranked.subjectType,
+ })
+ .from(ranked)
+ .leftJoin(topics, and(eq(ranked.subjectType, 'topic'), eq(topics.id, ranked.subjectId)))
+ .where(sql`${ranked.rn} <= ${recentLimit}`)
+ .orderBy(asc(ranked.lessonId), desc(ranked.rn));
+
+ const recentByLesson = new Map<
+ string,
+ {
+ pass: boolean;
+ runIndex: number;
+ subjectId: string;
+ subjectTitle: string | null;
+ subjectType: string;
+ }[]
+ >();
+ for (const r of recentRows) {
+ const list = recentByLesson.get(r.lessonId) ?? [];
+ list.push({
+ pass: r.outcome === 'pass',
+ runIndex: r.runIndex,
+ subjectId: r.subjectId,
+ subjectTitle: r.subjectTitle,
+ subjectType: r.subjectType,
+ });
+ recentByLesson.set(r.lessonId, list);
+ }
+ return lessons.map(({ createdByUserId, originRunId, ...l }) => ({
+ ...l,
+ recent: recentByLesson.get(l.id) ?? [],
+ /**
+ * Taught by the user directly, as opposed to distilled from practice. Older ingestion
+ * runs stamped the acting user on distilled lessons too, so a lesson that traces back to
+ * a run is never "taught", whoever created the row.
+ */
+ taughtByUser: createdByUserId != null && originRunId == null,
+ }));
+ };
+
+ /** Lists the agents that have practiced each domain. */
+ actorsByDomain = async (domainIds: string[]) => {
+ if (domainIds.length === 0) return [];
+ return this.db
+ .selectDistinct({ actorId: expertiseRuns.actorId, domainId: expertiseRuns.domainId })
+ .from(expertiseRuns)
+ .where(and(inArray(expertiseRuns.domainId, domainIds), eq(expertiseRuns.actorType, 'agent')));
+ };
+
+ sameReach = (...args: ExpertiseArgs<typeof domains.sameReach>) =>
+ domains.sameReach(this.ctx, ...args);
+
+ // L1: domain detail
+
+ findDomain = (...args: ExpertiseArgs<typeof domains.findDomain>) =>
+ domains.findDomain(this.ctx, ...args);
+
+ /** Returns the complete snapshot series for a domain. */
+ listSnapshots = async (domainId: string) =>
+ this.db
+ .select()
+ .from(expertiseDomainSnapshots)
+ .where(eq(expertiseDomainSnapshots.domainId, domainId))
+ .orderBy(asc(expertiseDomainSnapshots.runIndex));
+
+ listRuns = async (domainId: string, limit = 50) =>
+ this.db
+ .select()
+ .from(expertiseRuns)
+ .where(eq(expertiseRuns.domainId, domainId))
+ .orderBy(desc(expertiseRuns.runIndex))
+ .limit(limit);
+
+ /** Counts all runs independently of the paginated run list. */
+ countRuns = async (domainId: string) => {
+ const [row] = await this.db
+ .select({ n: sql<number>`count(*)::int` })
+ .from(expertiseRuns)
+ .where(eq(expertiseRuns.domainId, domainId));
+ return row?.n ?? 0;
+ };
+
+ /** Returns whether a human participated in each practice run. */
+ runHumanFlags = async (domainId: string) =>
+ this.db
+ .select({
+ hadHumanInLoop: expertiseRuns.hadHumanInLoop,
+ runIndex: expertiseRuns.runIndex,
+ })
+ .from(expertiseRuns)
+ .where(eq(expertiseRuns.domainId, domainId))
+ .orderBy(asc(expertiseRuns.runIndex));
+
+ /** Summarizes active lesson count, hits, and unused lessons. */
+ lessonStats = async (domainId: string) => {
+ const [row] = await this.db
+ .select({
+ hits: sql<number>`coalesce(sum(${expertiseLessons.hitCount}), 0)::int`,
+ total: sql<number>`count(*)::int`,
+ unused: sql<number>`count(*) filter (where ${expertiseLessons.hitCount} = 0)::int`,
+ })
+ .from(expertiseLessons)
+ .where(and(eq(expertiseLessons.domainId, domainId), eq(expertiseLessons.status, 'active')));
+ return row ?? { hits: 0, total: 0, unused: 0 };
+ };
+
+ // L2: lesson library
+
+ /** Lists active lessons by hit count and assigns their usage tier. */
+ listLessons = async (domainId: string, opts?: { layer?: string; search?: string }) => {
+ const conditions = [
+ eq(expertiseLessons.domainId, domainId),
+ eq(expertiseLessons.status, 'active'),
+ ];
+ if (opts?.layer) conditions.push(eq(expertiseLessons.layer, opts.layer));
+ if (opts?.search) {
+ conditions.push(sql`${expertiseLessons.title} ILIKE ${`%${opts.search}%`}`);
+ }
+
+ const rows = await this.db
+ .select({ lesson: expertiseLessons })
+ .from(expertiseLessons)
+ .innerJoin(expertiseDomains, eq(expertiseDomains.id, expertiseLessons.domainId))
+ .where(and(...conditions, this.scopeWhere()))
+ .orderBy(desc(expertiseLessons.hitCount), asc(expertiseLessons.code));
+
+ const lessons = rows.map(({ lesson }) => lesson);
+
+ const maxHit = lessons.reduce((a, r) => Math.max(a, r.hitCount), 0);
+ const cut = Math.max(CORE_CUT_MIN, Math.round(maxHit * CORE_CUT_RATIO));
+
+ return lessons.map((r) => ({
+ ...r,
+ tier: (r.hitCount >= cut ? 'core' : r.hitCount > 0 ? 'niche' : 'unused') as ExpertiseTier,
+ }));
+ };
+
+ /** Counts active lessons by declared layer. */
+ layerCounts = async (domainId: string) => {
+ const rows = await this.db
+ .select({ layer: expertiseLessons.layer, n: sql<number>`count(*)::int` })
+ .from(expertiseLessons)
+ .where(and(eq(expertiseLessons.domainId, domainId), eq(expertiseLessons.status, 'active')))
+ .groupBy(expertiseLessons.layer);
+ return Object.fromEntries(rows.filter((r) => r.layer).map((r) => [r.layer!, r.n]));
+ };
+
+ /** Counts active lessons by canon anchor, including unanchored lessons. */
+ canonAnchorCounts = async (domainId: string) => {
+ const rows = await this.db
+ .select({ anchor: expertiseLessons.canonAnchor, n: sql<number>`count(*)::int` })
+ .from(expertiseLessons)
+ .where(and(eq(expertiseLessons.domainId, domainId), eq(expertiseLessons.status, 'active')))
+ .groupBy(expertiseLessons.canonAnchor);
+ return {
+ byKey: Object.fromEntries(rows.filter((r) => r.anchor).map((r) => [r.anchor!, r.n])),
+ unanchored: rows.find((r) => !r.anchor)?.n ?? 0,
+ };
+ };
+
+ // L3: lesson detail
+
+ findLesson = (...args: ExpertiseArgs<typeof lessons.findLesson>) =>
+ lessons.findLesson(this.ctx, ...args);
+
+ /** Lists lesson evidence together with its source run and topic. */
+ listLessonHits = async (lessonId: string, limit = 20) =>
+ this.db
+ .select({
+ createdAt: expertiseHits.createdAt,
+ example: expertiseHits.example,
+ note: expertiseHits.note,
+ outcome: expertiseHits.outcome,
+ runIndex: expertiseRuns.runIndex,
+ runTitle: sql<string>`coalesce(${topics.title}, ${expertiseRuns.subjectId})`,
+ severity: expertiseHits.severity,
+ subjectId: expertiseRuns.subjectId,
+ subjectType: expertiseRuns.subjectType,
+ where: expertiseHits.where,
+ })
+ .from(expertiseHits)
+ .innerJoin(expertiseRuns, eq(expertiseRuns.id, expertiseHits.runId))
+ .innerJoin(expertiseDomains, eq(expertiseDomains.id, expertiseHits.domainId))
+ .leftJoin(
+ topics,
+ and(eq(expertiseRuns.subjectType, 'topic'), eq(topics.id, expertiseRuns.subjectId)),
+ )
+ .where(and(eq(expertiseHits.lessonId, lessonId), this.scopeWhere()))
+ .orderBy(desc(expertiseHits.createdAt))
+ .limit(limit);
+
+ // Writes
+
+ createDomain = (...args: ExpertiseArgs<typeof domains.createDomain>) =>
+ domains.createDomain(this.ctx, ...args);
+
+ deleteDomain = (...args: ExpertiseArgs<typeof domains.deleteDomain>) =>
+ domains.deleteDomain(this.ctx, ...args);
+
+ teachLesson = (...args: ExpertiseArgs<typeof lessons.teachLesson>) =>
+ lessons.teachLesson(this.ctx, ...args);
+
+ reviseLesson = (...args: ExpertiseArgs<typeof lessons.reviseLesson>) =>
+ lessons.reviseLesson(this.ctx, ...args);
+
+ listLessonRevisions = (...args: ExpertiseArgs<typeof lessons.listLessonRevisions>) =>
+ lessons.listLessonRevisions(this.ctx, ...args);
+
+ restoreLesson = (...args: ExpertiseArgs<typeof lessons.restoreLesson>) =>
+ lessons.restoreLesson(this.ctx, ...args);
+
+ createRule = (...args: ExpertiseArgs<typeof filing.createRule>) =>
+ filing.createRule(this.ctx, ...args);
+
+ insertMaterialRun = (...args: ExpertiseArgs<typeof filing.insertMaterialRun>) =>
+ filing.insertMaterialRun(this.ctx, ...args);
+
+ insertMaterialHit = (...args: ExpertiseArgs<typeof filing.insertMaterialHit>) =>
+ filing.insertMaterialHit(this.ctx, ...args);
+
+ lockLesson = (...args: ExpertiseArgs<typeof lessons.lockLesson>) =>
+ lessons.lockLesson(this.ctx, ...args);
+
+ insertLessonRevision = (...args: ExpertiseArgs<typeof lessons.insertLessonRevision>) =>
+ lessons.insertLessonRevision(this.ctx, ...args);
+
+ updateLessonFields = (...args: ExpertiseArgs<typeof lessons.updateLessonFields>) =>
+ lessons.updateLessonFields(this.ctx, ...args);
+
+ fillLessonDirections = (...args: ExpertiseArgs<typeof lessons.fillLessonDirections>) =>
+ lessons.fillLessonDirections(this.ctx, ...args);
+
+ countLineageRuns = (...args: ExpertiseArgs<typeof lineage.countLineageRuns>) =>
+ lineage.countLineageRuns(this.ctx, ...args);
+
+ reorderRule = (...args: ExpertiseArgs<typeof filing.reorderRule>) =>
+ filing.reorderRule(this.ctx, ...args);
+
+ moveRule = (...args: ExpertiseArgs<typeof filing.moveRule>) => filing.moveRule(this.ctx, ...args);
+
+ createRuleGroup = (...args: ExpertiseArgs<typeof domains.createRuleGroup>) =>
+ domains.createRuleGroup(this.ctx, ...args);
+
+ updateRuleGroup = (...args: ExpertiseArgs<typeof domains.updateRuleGroup>) =>
+ domains.updateRuleGroup(this.ctx, ...args);
+
+ retireLesson = (...args: ExpertiseArgs<typeof lessons.retireLesson>) =>
+ lessons.retireLesson(this.ctx, ...args);
+
+ // Insights
+
+ listInsights = async (domainIds: string[]) => {
+ if (domainIds.length === 0) return [];
+ return this.db
+ .select()
+ .from(expertiseInsights)
+ .where(
+ and(
+ or(inArray(expertiseInsights.domainId, domainIds), isNull(expertiseInsights.domainId)),
+ eq(expertiseInsights.status, 'active'),
+ this.insightScopeWhere(),
+ ),
+ )
+ .orderBy(desc(expertiseInsights.confidence))
+ .limit(10);
+ };
+
+ dismissInsight = async (insightId: string, reason?: string) =>
+ this.db
+ .update(expertiseInsights)
+ .set({ dismissReason: reason, status: 'dismissed', updatedAt: new Date() })
+ .where(and(eq(expertiseInsights.id, insightId), this.insightScopeWhere()));
+}
diff --git a/packages/database/src/models/expertise/lessons.ts b/packages/database/src/models/expertise/lessons.ts
new file mode 100644
index 00000000000..d6f12792706
--- /dev/null
+++ b/packages/database/src/models/expertise/lessons.ts
@@ -0,0 +1,210 @@
+import {
+ EXPERTISE_RULE_DIRECTIONS,
+ type ExpertiseLessonSection,
+ type ExpertiseRuleDirection,
+} from '@lobechat/types';
+import { and, desc, eq, inArray, isNull } from 'drizzle-orm';
+
+import { expertiseDomains, expertiseLessonRevisions, expertiseLessons } from '../../schemas';
+import { domainScopeWhere, type ExpertiseContext } from './context';
+
+export const findLesson = async (ctx: ExpertiseContext, lessonId: string) => {
+ const [row] = await ctx.db
+ .select({ lesson: expertiseLessons })
+ .from(expertiseLessons)
+ .innerJoin(expertiseDomains, eq(expertiseDomains.id, expertiseLessons.domainId))
+ .where(and(eq(expertiseLessons.id, lessonId), domainScopeWhere(ctx)))
+ .limit(1);
+ return row?.lesson;
+};
+
+/** Stores a lesson the user taught directly; it takes effect on the next matching practice. */
+export const teachLesson = async (
+ ctx: ExpertiseContext,
+ params: { domainId: string; text: string },
+) => {
+ const domain = await ctx.model.findDomain(params.domainId);
+ if (!domain) return null;
+ const text = params.text.trim();
+ const title = text.length > 80 ? `${text.slice(0, 79)}…` : text;
+ return ctx.db.transaction(async (tx) => {
+ const codes = await tx
+ .select({ code: expertiseLessons.code })
+ .from(expertiseLessons)
+ .where(eq(expertiseLessons.domainId, params.domainId));
+ const next =
+ Math.max(0, ...codes.map(({ code }) => Number(/^P-(\d+)$/.exec(code)?.[1] ?? 0))) + 1;
+ const code = `P-${String(next).padStart(2, '0')}`;
+ const [row] = await tx
+ .insert(expertiseLessons)
+ .values({
+ code,
+ createdByUserId: ctx.userId,
+ domainId: params.domainId,
+ polarity: 'rule',
+ sections: [{ body: text, key: 'rule' }],
+ title,
+ })
+ .returning({ code: expertiseLessons.code, id: expertiseLessons.id });
+ return row;
+ });
+};
+
+/**
+ * Records the user's correction as a new revision and folds it into the lesson body.
+ * The correction is kept as its own section so the original judgment stays legible.
+ */
+export const reviseLesson = async (ctx: ExpertiseContext, lessonId: string, feedback: string) => {
+ const lesson = await ctx.model.findLesson(lessonId);
+ if (!lesson) return null;
+ const text = feedback.trim();
+ const sections: ExpertiseLessonSection[] = [
+ ...lesson.sections.filter((s) => s.key !== 'limits'),
+ { body: text, key: 'limits' },
+ ];
+ const revision = lesson.currentRevision + 1;
+ await ctx.db.transaction(async (tx) => {
+ await tx.insert(expertiseLessonRevisions).values({
+ changedBy: 'user',
+ changedByUserId: ctx.userId,
+ feedback: text,
+ kind: 'user-feedback',
+ lessonId,
+ prevTitle: lesson.title,
+ revision,
+ sections,
+ });
+ await tx
+ .update(expertiseLessons)
+ .set({ currentRevision: revision, sections, updatedAt: new Date() })
+ .where(eq(expertiseLessons.id, lessonId));
+ });
+ return { id: lessonId, revision };
+};
+
+/**
+ * The edits a standard has been through, newest first.
+ *
+ * `feedback` is the reviewer's own sentence and `changedBy` says whether the edit came from them
+ * or from the system generalizing, which is the distinction that makes the history readable.
+ */
+export const listLessonRevisions = async (ctx: ExpertiseContext, lessonId: string, limit = 10) => {
+ // A rule re-filed into another group is a copy; its earlier edits stay on the row it was
+ // copied from. Follow `salvagedFromId` back so the history reads as one rule's. Merged-in
+ // rules are not followed: their edits were to a different sentence.
+ const chain = [lessonId];
+ for (let depth = 0; depth < 16; depth += 1) {
+ const [row] = await ctx.db
+ .select({ salvagedFromId: expertiseLessons.salvagedFromId })
+ .from(expertiseLessons)
+ .innerJoin(expertiseDomains, eq(expertiseDomains.id, expertiseLessons.domainId))
+ .where(and(eq(expertiseLessons.id, chain.at(-1)!), domainScopeWhere(ctx)))
+ .limit(1);
+ const previous = row?.salvagedFromId;
+ if (!previous || chain.includes(previous)) break;
+ chain.push(previous);
+ }
+ const rows = await ctx.db
+ .select({
+ changedBy: expertiseLessonRevisions.changedBy,
+ changedByUserId: expertiseLessonRevisions.changedByUserId,
+ createdAt: expertiseLessonRevisions.createdAt,
+ feedback: expertiseLessonRevisions.feedback,
+ id: expertiseLessonRevisions.id,
+ kind: expertiseLessonRevisions.kind,
+ prevTitle: expertiseLessonRevisions.prevTitle,
+ revision: expertiseLessonRevisions.revision,
+ })
+ .from(expertiseLessonRevisions)
+ .innerJoin(expertiseLessons, eq(expertiseLessons.id, expertiseLessonRevisions.lessonId))
+ .innerJoin(expertiseDomains, eq(expertiseDomains.id, expertiseLessons.domainId))
+ .where(and(inArray(expertiseLessonRevisions.lessonId, chain), domainScopeWhere(ctx)))
+ .orderBy(desc(expertiseLessonRevisions.revision), desc(expertiseLessonRevisions.createdAt))
+ .limit(limit);
+ // In a shared group any member can edit; the reader is told whether an edit was theirs, and
+ // the other member's id does not leave the server.
+ return rows.map(({ changedByUserId, ...row }) => ({
+ ...row,
+ byViewer: changedByUserId === ctx.userId,
+ }));
+};
+
+/** Brings a retired standard back into practice. */
+export const restoreLesson = async (ctx: ExpertiseContext, lessonId: string) => {
+ const lesson = await ctx.model.findLesson(lessonId);
+ if (!lesson) return null;
+ // A rule archived by a merge already lives on inside its target: its counts were added there
+ // and its evidence is read through the target's lineage. Bringing it back would count the
+ // same history twice, so it stays archived.
+ if (lesson.rejectedReason?.startsWith('merged-into:')) return null;
+ await ctx.db
+ .update(expertiseLessons)
+ .set({ retiredAt: null, status: 'active', updatedAt: new Date() })
+ .where(eq(expertiseLessons.id, lessonId));
+ return { id: lessonId };
+};
+
+/**
+ * The same row `findLesson` returns, read under a row lock for the rest of the caller's
+ * transaction. Anything computed from it β€” the next revision number, a section patch, merged
+ * counts β€” stays valid until commit, so a concurrent edit waits instead of being overwritten.
+ */
+export const lockLesson = async (ctx: ExpertiseContext, lessonId: string) => {
+ const [row] = await ctx.db
+ .select({ lesson: expertiseLessons })
+ .from(expertiseLessons)
+ .innerJoin(expertiseDomains, eq(expertiseDomains.id, expertiseLessons.domainId))
+ .where(and(eq(expertiseLessons.id, lessonId), domainScopeWhere(ctx)))
+ .for('update', { of: expertiseLessons })
+ .limit(1);
+ return row?.lesson;
+};
+
+/** Appends one entry to a lesson's edit history. */
+export const insertLessonRevision = async (
+ ctx: ExpertiseContext,
+ values: typeof expertiseLessonRevisions.$inferInsert,
+) => {
+ await ctx.db.insert(expertiseLessonRevisions).values(values);
+};
+
+/** Writes fields of one lesson row; `updatedAt` is always refreshed. */
+export const updateLessonFields = async (
+ ctx: ExpertiseContext,
+ lessonId: string,
+ fields: Partial<typeof expertiseLessons.$inferInsert>,
+) => {
+ await ctx.db
+ .update(expertiseLessons)
+ .set({ ...fields, updatedAt: new Date() })
+ .where(eq(expertiseLessons.id, lessonId));
+};
+
+/**
+ * Writes judged directions onto rules that still have none. A rule the reviewer flipped while
+ * the judgment was in flight keeps their choice: only null rows are filled.
+ */
+export const fillLessonDirections = async (
+ ctx: ExpertiseContext,
+ judged: { direction: ExpertiseRuleDirection; id: string }[],
+) => {
+ for (const direction of EXPERTISE_RULE_DIRECTIONS) {
+ const ids = judged.filter((rule) => rule.direction === direction).map((rule) => rule.id);
+ if (ids.length === 0) continue;
+ await ctx.db
+ .update(expertiseLessons)
+ .set({ direction, updatedAt: new Date() })
+ .where(and(inArray(expertiseLessons.id, ids), isNull(expertiseLessons.direction)));
+ }
+};
+
+/** Retires a lesson so it stops being practiced; the record and its evidence are kept. */
+export const retireLesson = async (ctx: ExpertiseContext, lessonId: string) => {
+ const lesson = await ctx.model.findLesson(lessonId);
+ if (!lesson) return null;
+ await ctx.db
+ .update(expertiseLessons)
+ .set({ retiredAt: new Date(), status: 'retired', updatedAt: new Date() })
+ .where(eq(expertiseLessons.id, lessonId));
+ return { id: lessonId };
+};
diff --git a/packages/database/src/models/expertise/lineage.ts b/packages/database/src/models/expertise/lineage.ts
new file mode 100644
index 00000000000..3348bc89a03
--- /dev/null
+++ b/packages/database/src/models/expertise/lineage.ts
@@ -0,0 +1,215 @@
+import { and, desc, eq, inArray, isNull, or, sql } from 'drizzle-orm';
+
+import {
+ documents,
+ expertiseDomains,
+ expertiseHits,
+ expertiseLessons,
+ expertiseRuns,
+ topics,
+ verifyCheckResults,
+ verifyRuns,
+} from '../../schemas';
+import { buildWorkspaceWhere } from '../../utils/workspace';
+import { domainScopeWhere, type ExpertiseContext } from './context';
+
+/**
+ * The rejections one rule was distilled from, resolved back to the acceptance they were
+ * written in so the reader can reopen the round and see the frame they circled.
+ *
+ * Evidence never changes hands: a hit stays on the lesson and the run that produced it, because
+ * both are pinned to their domain by composite keys. So a rule that absorbed others
+ * (`generalizedFromIds`) or was re-filed into another group (`salvagedFromId`) reads its sources
+ * through that lineage instead of owning them.
+ *
+ * `sourceCheckResultId` is nullable by design β€” a deleted acceptance leaves the rule standing
+ * and only breaks the trail β€” so the joins are left joins and the caller renders an unlinked
+ * row rather than dropping the evidence.
+ */
+export const listLessonSources = async (ctx: ExpertiseContext, lessonId: string, limit = 20) => {
+ const lineage = await resolveLineage(ctx, lessonId);
+ if (lineage.length === 0) return [];
+ return (
+ ctx.db
+ .select({
+ acceptanceId: verifyRuns.acceptanceId,
+ checkTitle: verifyCheckResults.checkItemTitle,
+ createdAt: expertiseHits.createdAt,
+ example: expertiseHits.example,
+ // A material the reviewer brought: the document it was read from (a file is recorded as
+ // its parsed document, which keeps the file id), and which kind of material it was.
+ documentFileId: documents.fileId,
+ documentId: documents.id,
+ // Read from the run, not from the check-result join: a deleted acceptance leaves that
+ // join empty, and the hit was still a rejection.
+ fromAcceptance: sql<boolean>`coalesce(${expertiseRuns.reflectionKey}, '') like 'acceptance:%'`,
+ materialType: sql<
+ string | null
+ >`case when ${expertiseRuns.reflectionKey} like 'material:%' then split_part(${expertiseRuns.reflectionKey}, ':', 2) end`,
+ id: expertiseHits.id,
+ // Where a conversation source can be reopened: the topic, the agent it lives under, and
+ // the message the observation was read from when ingestion could find it.
+ messageId: expertiseHits.sourceMessageId,
+ reviewerComment: sql<string | null>`${verifyCheckResults.userDecisionDetail} ->> 'comment'`,
+ roundIndex: verifyRuns.roundIndex,
+ severity: expertiseHits.severity,
+ topicAgentId: topics.agentId,
+ topicId: topics.id,
+ userDecision: expertiseHits.userDecision,
+ where: expertiseHits.where,
+ })
+ .from(expertiseHits)
+ .innerJoin(expertiseDomains, eq(expertiseDomains.id, expertiseHits.domainId))
+ .innerJoin(expertiseRuns, eq(expertiseRuns.id, expertiseHits.runId))
+ // Only a topic the viewer can open: a shared group does not grant a teammate's topic.
+ .leftJoin(
+ topics,
+ and(
+ eq(expertiseRuns.subjectType, 'topic'),
+ eq(topics.id, expertiseRuns.subjectId),
+ buildWorkspaceWhere({ userId: ctx.userId, workspaceId: ctx.workspaceId }, topics),
+ ),
+ )
+ // Only a document the viewer can open, by the same scope documents are listed with.
+ .leftJoin(
+ documents,
+ and(
+ eq(expertiseRuns.subjectType, 'document'),
+ eq(documents.id, expertiseRuns.subjectId),
+ buildWorkspaceWhere({ userId: ctx.userId, workspaceId: ctx.workspaceId }, documents),
+ ),
+ )
+ .leftJoin(verifyCheckResults, eq(verifyCheckResults.id, expertiseHits.sourceCheckResultId))
+ .leftJoin(verifyRuns, eq(verifyRuns.id, verifyCheckResults.verifyRunId))
+ .where(
+ and(
+ inArray(expertiseHits.lessonId, lineage),
+ domainScopeWhere(ctx),
+ // Access to a shared group is not access to the rounds behind it: a hit distilled from
+ // a teammate's private round would otherwise show that round's check, the reviewer's
+ // words and a link to it. Same predicate the consolidation reader applies.
+ or(
+ eq(verifyCheckResults.userId, ctx.userId),
+ eq(verifyRuns.visibility, 'public'),
+ // No linked round (never mapped, or the round was deleted): nothing says the viewer
+ // may see where it came from, so only their own runs' evidence is shown.
+ and(isNull(verifyCheckResults.id), eq(expertiseRuns.userId, ctx.userId)),
+ ),
+ ),
+ )
+ .orderBy(desc(expertiseHits.createdAt))
+ .limit(limit)
+ );
+};
+
+/**
+ * Every lesson whose evidence this one now speaks for: itself, what it absorbed through merges
+ * (`generalizedFromIds`) and what it was re-filed from (`salvagedFromId`), followed through as
+ * many generations as there are. One level is not enough β€” merging a rule that was itself a
+ * merge would otherwise keep the counts and lose the evidence.
+ */
+const resolveLineage = async (ctx: ExpertiseContext, lessonId: string): Promise<string[]> =>
+ (await resolveLineages(ctx, [lessonId])).get(lessonId) ?? [];
+
+/**
+ * {@link resolveLineage} for many rules at once: one read per generation instead of one walk
+ * per rule, so a whole page of rules costs a handful of queries. A rule outside the caller's
+ * scope maps to an empty lineage.
+ */
+const resolveLineages = async (ctx: ExpertiseContext, lessonIds: string[]) => {
+ const parents = new Map<string, string[]>();
+ let frontier = [...new Set(lessonIds)];
+ // Bounded so a malformed cycle can never spin; real lineages are a handful of steps deep.
+ for (let depth = 0; depth <= 16 && frontier.length > 0; depth += 1) {
+ const rows = await ctx.db
+ .select({
+ generalizedFromIds: expertiseLessons.generalizedFromIds,
+ id: expertiseLessons.id,
+ salvagedFromId: expertiseLessons.salvagedFromId,
+ })
+ .from(expertiseLessons)
+ .innerJoin(expertiseDomains, eq(expertiseDomains.id, expertiseLessons.domainId))
+ .where(and(inArray(expertiseLessons.id, frontier), domainScopeWhere(ctx)));
+ for (const row of rows) {
+ parents.set(row.id, [
+ ...(row.generalizedFromIds ?? []),
+ ...(row.salvagedFromId ? [row.salvagedFromId] : []),
+ ]);
+ }
+ frontier = [...new Set(rows.flatMap((row) => parents.get(row.id) ?? []))].filter(
+ (id) => !parents.has(id),
+ );
+ }
+
+ return new Map(
+ lessonIds.map((root) => {
+ if (!parents.has(root)) return [root, []];
+ const seen = new Set<string>([root]);
+ const stack = [root];
+ while (stack.length > 0) {
+ for (const parent of parents.get(stack.pop()!) ?? []) {
+ if (seen.has(parent)) continue;
+ seen.add(parent);
+ stack.push(parent);
+ }
+ }
+ return [root, [...seen]];
+ }),
+ );
+};
+
+/**
+ * How many times each rule's lineage was hit by a rejected acceptance versus observed in a
+ * conversation. `hitCount` adds both, and the two mean different things to the reviewer: only
+ * the first is "you rejected a delivery for this".
+ */
+export const countHitsBySource = async (ctx: ExpertiseContext, lessonIds: string[]) => {
+ const lineages = await resolveLineages(ctx, lessonIds);
+ const all = [...new Set([...lineages.values()].flat())];
+ const rows =
+ all.length === 0
+ ? []
+ : await ctx.db
+ .select({
+ conversations: sql<number>`count(*) filter (where coalesce(${expertiseRuns.reflectionKey}, '') not like 'acceptance:%' and coalesce(${expertiseRuns.reflectionKey}, '') not like 'material:%')::int`,
+ lessonId: expertiseHits.lessonId,
+ // Read from a material the reviewer brought: evidence the rule was stated, not seen.
+ materials: sql<number>`count(*) filter (where ${expertiseRuns.reflectionKey} like 'material:%')::int`,
+ rejections: sql<number>`count(*) filter (where ${expertiseRuns.reflectionKey} like 'acceptance:%')::int`,
+ })
+ .from(expertiseHits)
+ .innerJoin(expertiseRuns, eq(expertiseRuns.id, expertiseHits.runId))
+ .where(inArray(expertiseHits.lessonId, all))
+ .groupBy(expertiseHits.lessonId);
+ const byLesson = new Map(rows.map((row) => [row.lessonId, row]));
+
+ return new Map(
+ lessonIds.map((id) => {
+ const lineage = lineages.get(id) ?? [];
+ const sum = (key: 'conversations' | 'materials' | 'rejections') =>
+ lineage.reduce((total, member) => total + (byLesson.get(member)?.[key] ?? 0), 0);
+ return [
+ id,
+ {
+ conversationHitCount: sum('conversations'),
+ materialHitCount: sum('materials'),
+ rejectionHitCount: sum('rejections'),
+ },
+ ];
+ }),
+ );
+};
+
+/**
+ * Distinct runs a lesson has been proven in across its whole lineage. Runs are "distinct
+ * situations", so two merged rules hit in the same delivery count it once.
+ */
+export const countLineageRuns = async (ctx: ExpertiseContext, lessonId: string) => {
+ const lineage = await resolveLineage(ctx, lessonId);
+ if (lineage.length === 0) return 0;
+ const [row] = await ctx.db
+ .select({ runs: sql<number>`count(distinct ${expertiseHits.runId})::int` })
+ .from(expertiseHits)
+ .where(inArray(expertiseHits.lessonId, lineage));
+ return row?.runs ?? 0;
+};
diff --git a/packages/database/src/models/expertise/rules.ts b/packages/database/src/models/expertise/rules.ts
new file mode 100644
index 00000000000..ced71ccd2ae
--- /dev/null
+++ b/packages/database/src/models/expertise/rules.ts
@@ -0,0 +1,248 @@
+import { type ExpertiseEnforcement } from '@lobechat/types';
+import { and, asc, eq, inArray, isNotNull, isNull, or, sql } from 'drizzle-orm';
+
+import {
+ agents,
+ expertiseBindings,
+ expertiseLessons,
+ expertiseRuns,
+ projects,
+ verifyCheckResults,
+} from '../../schemas';
+import { buildWorkspaceWhere } from '../../utils/workspace';
+import { type ExpertiseContext } from './context';
+import { listDomainsBoundTo } from './domains';
+import { countHitsBySource } from './lineage';
+
+/** Whose a listed group is: the reviewer's own rules, or one agent's self-learning. */
+export type ExpertiseGroupOwner =
+ | { kind: 'mine' }
+ | {
+ agent: {
+ avatar: string | null;
+ backgroundColor: string | null;
+ id: string;
+ title: string | null;
+ };
+ kind: 'agent';
+ };
+
+/**
+ * Everything the self-evolving page lists β€” the reviewer's rules first, then what each agent
+ * learned on its own β€” as groups with the lessons filed in them and where each takes effect.
+ *
+ * Reads the same binding arm the distillation writes through, so what this returns is exactly
+ * what an acceptance without a project would add to. Rules come back in the reviewer's own
+ * order (`sortOrder`, then creation) because they told us the order matters; the page does not
+ * re-rank them by hit count.
+ *
+ * Each group carries an `owner`: `mine` when the reviewer, their workspace or a project holds
+ * it (it reaches every run in that scope), otherwise the one agent it was learned by. A group
+ * mounted on both is the reviewer's β€” that is the wider of the two reaches.
+ */
+export const listRules = async (ctx: ExpertiseContext) => {
+ // The reviewer's own groups, the ones distilled from a project's acceptances, and every
+ // agent's self-learning domains β€” the same set the run context injects from.
+ const bound = await listDomainsBoundTo(
+ ctx,
+ or(isNotNull(expertiseBindings.projectId), isNotNull(expertiseBindings.agentId)),
+ );
+ const domainIds = bound.map(({ domain }) => domain.id);
+ if (domainIds.length === 0) return [];
+
+ const [lessons, bindings] = await Promise.all([
+ ctx.db
+ .select({
+ code: expertiseLessons.code,
+ compilability: expertiseLessons.compilability,
+ createdAt: expertiseLessons.createdAt,
+ createdByUserId: expertiseLessons.createdByUserId,
+ // Null until judged; the page asks for the unjudged ones to be settled.
+ direction: expertiseLessons.direction,
+ domainId: expertiseLessons.domainId,
+ originRunId: expertiseLessons.originRunId,
+ // Rows that predate the column hold null; they have only ever been reminders.
+ enforcement: sql<ExpertiseEnforcement>`coalesce(${expertiseLessons.enforcement}, 'remind')`,
+ exampleCount: expertiseLessons.exampleCount,
+ falsePositiveCount: expertiseLessons.falsePositiveCount,
+ generalizedFromIds: expertiseLessons.generalizedFromIds,
+ hitCount: expertiseLessons.hitCount,
+ hitRunCount: expertiseLessons.hitRunCount,
+ id: expertiseLessons.id,
+ lastHitAt: expertiseLessons.lastHitAt,
+ reasonKind: expertiseLessons.reasonKind,
+ reasonSource: expertiseLessons.reasonSource,
+ rejectedReason: expertiseLessons.rejectedReason,
+ retiredAt: expertiseLessons.retiredAt,
+ sections: expertiseLessons.sections,
+ sortOrder: expertiseLessons.sortOrder,
+ specificity: expertiseLessons.specificity,
+ status: expertiseLessons.status,
+ tags: expertiseLessons.tags,
+ title: expertiseLessons.title,
+ })
+ .from(expertiseLessons)
+ .where(
+ and(
+ inArray(expertiseLessons.domainId, domainIds),
+ // Retired ones are kept and returned: the page files them under an archive the reader
+ // can reopen, because "I already told it to stop using this" is itself worth seeing.
+ inArray(expertiseLessons.status, ['active', 'retired']),
+ ),
+ )
+ // Unplaced rows (null, from before the column existed) come after the ones the
+ // reviewer ordered, oldest first, until a drag writes them an explicit position.
+ .orderBy(sql`${expertiseLessons.sortOrder} asc nulls last`, asc(expertiseLessons.createdAt)),
+ ctx.db
+ .select({
+ agentAvatar: agents.avatar,
+ agentBackgroundColor: agents.backgroundColor,
+ agentId: expertiseBindings.agentId,
+ agentTitle: agents.title,
+ // Null when the agent exists but the viewer may not see it.
+ visibleAgentId: agents.id,
+ boundUserId: expertiseBindings.boundUserId,
+ boundWorkspaceId: expertiseBindings.boundWorkspaceId,
+ domainId: expertiseBindings.domainId,
+ projectId: expertiseBindings.projectId,
+ projectName: projects.name,
+ })
+ .from(expertiseBindings)
+ .leftJoin(projects, eq(projects.id, expertiseBindings.projectId))
+ // A shared group can also be mounted on a teammate's private agent; the viewer may see
+ // the group without being allowed to see that agent, its id or its name.
+ .leftJoin(
+ agents,
+ and(
+ eq(agents.id, expertiseBindings.agentId),
+ buildWorkspaceWhere({ userId: ctx.userId, workspaceId: ctx.workspaceId }, agents),
+ ),
+ )
+ .where(
+ and(
+ inArray(expertiseBindings.domainId, domainIds),
+ eq(expertiseBindings.enabled, true),
+ or(isNull(expertiseBindings.agentId), isNotNull(agents.id)),
+ ),
+ )
+ .orderBy(asc(expertiseBindings.sortOrder)),
+ ]);
+
+ const bySource = await countHitsBySource(
+ ctx,
+ lessons.map((lesson) => lesson.id),
+ );
+
+ // Bucket once by domain: the page now spans every agent's domains, so filtering the full
+ // lesson and binding lists per group would grow with groups Γ— rows.
+ const byDomain = <T extends { domainId: string }>(rows: T[]) => {
+ const map = new Map<string, T[]>();
+ for (const row of rows) {
+ const bucket = map.get(row.domainId);
+ if (bucket) bucket.push(row);
+ else map.set(row.domainId, [row]);
+ }
+ return map;
+ };
+ const lessonsByDomain = byDomain(lessons);
+ const bindingsByDomain = byDomain(bindings);
+
+ const ownerOf = (domainId: string): ExpertiseGroupOwner | null => {
+ const mounts = bindingsByDomain.get(domainId) ?? [];
+ if (mounts.some((binding) => !binding.agentId)) return { kind: 'mine' };
+ const agent = mounts.find((binding) => binding.visibleAgentId);
+ // Only mounted on agents the viewer cannot see: nothing of it belongs on their page.
+ if (!agent?.agentId) return null;
+ return {
+ agent: {
+ avatar: agent.agentAvatar,
+ backgroundColor: agent.agentBackgroundColor,
+ id: agent.agentId,
+ title: agent.agentTitle,
+ },
+ kind: 'agent',
+ };
+ };
+
+ const owned = bound.flatMap(({ domain }) => {
+ const owner = ownerOf(domain.id);
+ return owner ? [{ domain, owner }] : [];
+ });
+ // The reviewer's groups lead in their own order; each agent's follow together, agents in the
+ // order their first group was mounted.
+ const agentOrder = [
+ ...new Set(owned.flatMap(({ owner }) => (owner.kind === 'agent' ? [owner.agent.id] : []))),
+ ];
+ const rank = ({ owner }: (typeof owned)[number]) =>
+ owner.kind === 'mine' ? -1 : agentOrder.indexOf(owner.agent.id);
+ const ordered = owned
+ .map((entry, index) => ({ entry, index }))
+ .sort((a, b) => rank(a.entry) - rank(b.entry) || a.index - b.index)
+ .map(({ entry }) => entry);
+
+ return ordered.map(({ domain, owner }) => ({
+ owner,
+ domain: {
+ domainFilter: domain.domainFilter,
+ id: domain.id,
+ outOfScope: domain.outOfScope,
+ title: domain.title,
+ },
+ rules: (lessonsByDomain.get(domain.id) ?? [])
+ // Written by the reviewer rather than distilled from a run. Hit counts grow as a rule is
+ // applied, so provenance is read from where the row came from β€” same test the lesson
+ // reader uses for `taughtByUser`.
+ .map(({ originRunId, ...lesson }) => ({
+ ...lesson,
+ ...(bySource.get(lesson.id) ?? {
+ conversationHitCount: 0,
+ materialHitCount: 0,
+ rejectionHitCount: 0,
+ }),
+ authored: lesson.createdByUserId != null && originRunId == null,
+ })),
+ scopes: (bindingsByDomain.get(domain.id) ?? []).map((binding) => {
+ if (binding.projectId) {
+ return { id: binding.projectId, kind: 'project' as const, title: binding.projectName };
+ }
+ if (binding.agentId) {
+ return { id: binding.agentId, kind: 'agent' as const, title: binding.agentTitle };
+ }
+ return binding.boundWorkspaceId
+ ? { id: binding.boundWorkspaceId, kind: 'workspace' as const, title: null }
+ : { id: binding.boundUserId!, kind: 'user' as const, title: null };
+ }),
+ }));
+};
+
+/**
+ * Rejected rounds no standard has been distilled from yet.
+ *
+ * Distillation only fires forward β€” a round is read when the NEXT one lands β€” so every round
+ * rejected before the feature existed stays untouched unless something asks for it. This is
+ * that backlog, and it is the only honest number to put on an empty standards page.
+ */
+export const countUndistilledRejectionRounds = async (ctx: ExpertiseContext) => {
+ const [row] = await ctx.db
+ .select({
+ rounds: sql<number>`count(distinct ${verifyCheckResults.verifyRunId})::int`,
+ })
+ .from(verifyCheckResults)
+ .where(
+ and(
+ eq(verifyCheckResults.userId, ctx.userId),
+ // The page is either the personal view or one workspace's; count only that scope.
+ ctx.workspaceId
+ ? eq(verifyCheckResults.workspaceId, ctx.workspaceId)
+ : isNull(verifyCheckResults.workspaceId),
+ eq(verifyCheckResults.userDecision, 'rejected'),
+ isNotNull(verifyCheckResults.verifyRunId),
+ sql`not exists (
+ select 1 from ${expertiseRuns}
+ where ${expertiseRuns.reflectionKey} like '%:run:' || ${verifyCheckResults.verifyRunId}::text
+ )`,
+ ),
+ );
+
+ return row?.rounds ?? 0;
+};