/** * Agent Profile (read-time synthesized per-agent profile) — "what has agent * X learned". Answers: given `memories.agent`, what lessons has this agent * banked, what decisions has it made recently, and where has it previously * been wrong? * * Shared between GET /agents/:agent/profile (server/routes/agents.ts) and the * MCP agent_profile tool (mcp/server.ts) so both transports stay in sync — * same pattern as buildWhyReceipt/buildSessionDigest in provenance.ts. * * Scope: ACTIVE memories only (valid_to IS NULL AND archived = 0 — the same * "currently valid" predicate queryEntityLinkage/archival.ts use) for * top_lessons, recent_decisions, and the type-count rollup. `corrections` is * necessarily an exception: it surfaces memories THIS agent previously wrote * that later turned out wrong (invalidated/superseded), so it queries the * memory_events log directly and is not restricted to currently-active rows. * * Ranking: top_lessons reuses usefulnessScores() (src/storage/usefulness.ts) * — the exact ADR-010 Laplace-smoothed (used+1)/(used+corrected+2) signal the * fusion epsilon component consumes in src/search/fuse.ts — then breaks ties * by importance. This is deliberately the same formula, not a reinvented one: * two components computing "usefulness" differently would silently drift. */ import type { DB } from '../../storage/db.js'; export interface AgentProfileLesson { id: string; text: string; importance: number; usefulness: number; created_at: number; } export interface AgentProfileDecision { id: string; text: string; importance: number; created_at: number; } export type CorrectionActionLabel = 'invalidated' | 'superseded'; export interface AgentProfileCorrection { id: string; type: string; text: string; action: CorrectionActionLabel; reason: string | null; superseded_by: string | null; /** Text of the memory that replaced this one, when linkable (null if the * successor was itself since deleted, or this correction is an invalidate * with no successor). */ superseding_text: string | null; corrected_at: number; } export interface AgentProfile { agent: string; /** Active-memory counts grouped by memories.type. */ counts: Record; /** Sum of `counts` — active memories for this agent. */ total: number; /** Earliest created_at across ALL memories for this agent (active or not). */ first_seen: number | null; /** Latest created_at across ALL memories for this agent (active or not). */ last_active: number | null; top_lessons: AgentProfileLesson[]; recent_decisions: AgentProfileDecision[]; corrections: AgentProfileCorrection[]; } /** * Builds the agent profile for `agent`. Returns null when the agent has zero * memories at all (active or not) — callers shape the not-found response for * their own transport (HTTP 404 vs MCP isError), same convention as * buildWhyReceipt. */ export declare function buildAgentProfile(db: DB, agent: string): AgentProfile | null;