/** * `@ai-matrx/agents/mandates/runs` — THE ONE READ of run history: `mandate.run_history`. * * One mandate's runs, or one agent's runs across every mandate and direct use (exactly one of the * two; the database refuses neither or both with 22023). No new log: the database reads the * records every funnel already writes (chat.user_request for agent and chat starts, held code * calls and `run_mandate`; workflow.run for every workflow Holder run). Which level decided each * run is stamped by aidream at the resolution; runs recorded before that stamp come back with * `rung: null` — "not recorded", never a guess. * * Views are SEATS, and the database checks each one: * mine the caller's own runs (a member) * org every run in one organization (its owner/admin) * platform every run on the platform (admin section only — never "mine") * * Moved from matrx-frontend features/mandates/run-history/service.ts (2026-10-12) so the desktop * app (Agents › an agent › Runs) reads the same rows through the same parser. HEADLESS: the host * passes its own supabase client (anything with `schema(name).rpc(fn, args)`), never a global. * Every field of the answer is narrowed, never cast. */ type RunHistoryView = "mine" | "org" | "platform"; type RunStatus = "failed" | "running" | "stopped" | "succeeded" | "waiting" | "warned"; type RunRung = "org" | "run" | "system" | "user"; type RunSort = "cost" | "duration" | "level" | "started_at" | "status"; declare const RUN_STATUSES: readonly RunStatus[]; interface MandateRun { /** `agent_run` = an agent run labeled with the mandate that kept no conversation. */ runKind: "agent_run" | "conversation" | "workflow"; runId: string; startedAt: string; completedAt: null | string; status: RunStatus; rawStatus: null | string; error: null | string; ranById: null | string; ranByName: null | string; ranByKind: "person" | "system"; organizationId: null | string; organizationName: null | string; /** null = recorded before the level was stamped (2026-09-27). */ rung: RunRung | null; holderType: "agent" | "workflow"; holderId: null | string; holderName: null | string; holderAgentType: null | string; outputWarned: boolean; outputMissingKeys: string[]; cost: null | number; durationMs: null | number; tokens: null | number; /** The mandate this run served; null = no mandate named it. */ mandateKey: null | string; /** * What the server's attribution funnel decided for a run with no mandate * (`mandate_attribution`): `direct` = a person or fixture started the agent by id, * `unknown` = the funnel could not decide. null = never stamped — never read as `direct`. */ attribution: null | string; /** The door that made a `direct` run direct (e.g. `matrx-frontend/agent-runner`). */ directDoor: null | string; /** How the list found it: `holder_agent` = a run of its Holder agent with no mandate name. */ foundBy: "agent" | "holder_agent" | "mandate"; /** The run's answer, one line (≤240 chars); null when none was kept. */ outputPreview: null | string; conversationId: null | string; /** The conversation holds messages to read (a background run keeps none). */ hasTranscript: boolean; } interface RunFacet { id: string; name: null | string; count: number; } interface MandateRunPage { total: number; rows: MandateRun[]; facets: null | { organizations: RunFacet[]; people: RunFacet[]; }; } interface RunsQuery { /** Exactly one: a mandate's runs, or one agent's. */ scope: { agentId: string; } | { mandateKey: string; }; view: RunHistoryView; organizationId?: null | string; userId?: null | string; status?: RunStatus | null; sort?: RunSort; descending?: boolean; limit?: number; offset?: number; } /** The database's refusal or failure, with its own words (code, detail, hint). */ declare class RunHistoryReadError extends Error { readonly name = "RunHistoryReadError"; readonly code: null | string; readonly detail: null | string; readonly hint: null | string; constructor(init: { message: string; code?: null | string; detail?: null | string; hint?: null | string; }); } /** The one call the read makes: a supabase-js client fits it as is. */ interface RunHistoryClient { schema(name: "mandate"): { rpc(fn: "run_history", args: Record): PromiseLike<{ data: unknown; error: null | { message: string; code?: null | string; details?: null | string; hint?: null | string; }; }>; }; } declare function fetchRuns(client: RunHistoryClient, query: RunsQuery): Promise; /** * The server stores a run's error as a JSON string (`{"error_type":…,"message":…}`) on many * paths; the row shows its sentence, never the raw JSON. The SQL cuts the text at 300 * characters, so a cut JSON string is read by its message field. */ declare function readableError(text: null | string): null | string; /** The database's answer as a page; throws when it is not one (never an empty page by accident). */ declare function parseRunPage(data: unknown): MandateRunPage; export { RUN_STATUSES, RunHistoryReadError, fetchRuns, parseRunPage, readableError }; export type { MandateRun, MandateRunPage, RunFacet, RunHistoryClient, RunHistoryView, RunRung, RunSort, RunStatus, RunsQuery };