import type { StampedEvent } from "./events.ts"; import { familyOf as builtinFamilyOf } from "../adapters/registry.ts"; import { nearestRank } from "./research-record.ts"; import { CHECKPOINT_FIELDS } from "./checkpoint-fields.ts"; import { latestTaskRecords, type ResearchRecord, type TaskRecord } from "./research.ts"; /** * #1277 (#44 AC0): the telemetry quality report that lets a dataset carry its own coverage. Everything is a count, a share or a duration over the records and the event * log of one window; the report holds no peer text, no model prose and no path. Peer ids are reduced to families where a family is known (the caller passes `familyOf`), and a * value that cannot be derived is null, never 0, so a reader can tell "no data" from "none happened". */ export const QUALITY_SCHEMA = "agent-hub.research-quality/v1"; /** A quota gap is a silence between two readings of one peer longer than this. */ export const QUOTA_GAP_MS = 60 * 60 * 1000; export interface FieldCoverage { n: number; unknown: number; value?: null } export interface QuotaCoverage { readings: number; gaps: number; gapMs: number; stalenessP95Ms: number | null; clock: { event: number; measured: number } } export interface OutcomeCounts { approved: number; cancelled: number; firstPass: number; changesRequested: number } export interface QualityReport { schema: typeof QUALITY_SCHEMA; window: { since: string | null; until: string | null }; records: number; /** Per v2 field: how many records carry a value and how many are unknown (null or absent). Blocks count per leaf. */ fields: Record; /** Quota readings per peer family: how many, the gaps over an hour, the 95th percentile of the time between readings, and where the reading's time came from. */ quota: Record; /** * Pauses and resumes per family, and `handoffs`: the owner moves a budget pause drove (a `reassigned` task event whose board reason is `budget`), counted once per move, so a task handed off twice counts twice. * A pause that found no taker moves nothing and counts nothing. Null when the window holds no task event at all, since then the log cannot say whether anything moved (#802). */ budget: { pauses: number; resumes: number; handoffs: number | null; byFamily: Record }; /** Task and review outcomes per (peer family, class), by the family of the task's last owner. */ outcomes: Record>; /** Every event type seen in the window with its count, and the types this report knows that did not occur. `preface` (#1330): the session prefaces injected in the window, their total bytes and memory lines, and how many were reordered by observed use; null when none was recorded. `checkpoints` (#1331): saved checkpoints in the window, by how many structured fields each carried and by field name; null when none was saved. */ events: { present: Record; absent: string[]; preface: { prefaces: number; bytes: number; lines: number; ordered: number } | null; checkpoints: { total: number; byFieldCount: Record; byField: Record } | null }; /** The share of attributed token increments by the attribution rule (#200) and the total; null when no tokens were counted. */ tokenAttribution: { total: number; share: Record<"delivery" | "single_open" | "unattributed", number | null> }; } const FAMILY_FALLBACK = "unknown"; const sortedRecord = (r: Record): Record => Object.fromEntries(Object.entries(r).sort(([a], [b]) => a.localeCompare(b))); /** The event types the report reasons about: the ones whose absence in a window means something. */ export const EXPECTED_EVENTS = ["task", "tokens", "usage", "turn_end", "quota", "budget_pause", "budget_resume", "envelope", "memory_brief", "hub_start"] as const; /** Fields counted as one value although they are objects: the findings tally is known or not as a whole. */ const WHOLE = new Set(["review.findings"]); const leaves = (value: unknown, prefix: string, out: Map): void => { if (value !== null && typeof value === "object" && !Array.isArray(value) && !WHOLE.has(prefix)) { for (const [key, child] of Object.entries(value)) leaves(child, prefix ? `${prefix}.${key}` : key, out); return; } out.set(prefix, value); }; /** The v2 blocks whose leaves are counted; v1 fields predate the report and are not unknown by design. */ const BLOCKS = ["review", "delivery", "merge", "specs", "memory", "person", "handoffs", "edits"] as const; /** The leaves of the two nullable edit-count objects. */ const EDIT_LEAVES = { editTool: ["total", "verifiedFullRead", "partialReadOnly", "blindOrStale", "occRejected"], shellRoute: ["total", "verifiedFullRead", "partialReadOnly", "blindOrStale", "blockedByGuard"] } as const; const SCALARS = ["stalls", "cohort", "declines", "escalations", "cancelCause"] as const; /** `since` and `until` are epoch ms; a record counts when its terminal time falls inside. */ export function buildResearchQuality(records: readonly ResearchRecord[], events: readonly StampedEvent[], window: { since?: number; until?: number } = {}, familyOf: (peer: string) => string | undefined = () => undefined): QualityReport { const from = window.since ?? 0, to = window.until ?? Infinity; const family = (peer: string | undefined) => (peer ? familyOf(peer) : undefined) ?? builtinFamilyOf(peer ?? "") ?? FAMILY_FALLBACK; const inside = (at: string | undefined | null) => { const t = at ? Date.parse(at) : NaN; return Number.isFinite(t) && t >= from && t <= to; }; // Outcomes count every task (a v1 row is an approved one); the v2 fields are counted over v2 rows only, since a v1 row never had them. const tasks = latestTaskRecords(records).filter(r => inside(r.closedAt ?? r.approvedAt)); const v2 = tasks.filter(r => r.schema === "agent-hub.research/v2"); const fields: Record = {}; /** Unknown is null, absent, or a closed vocabulary's own `unknown` member. */ const note = (name: string, value: unknown) => { const f = fields[name] ??= { n: 0, unknown: 0 }; f.n++; if (value === null || value === undefined || value === "unknown") f.unknown++; }; for (const r of v2) { const seen = new Map(); for (const block of BLOCKS) leaves((r as unknown as Record)[block] ?? null, block, seen); // #1556 T2: a null `editTool` or `shellRoute` (a tree_only surface) is unknown at each of its leaves, so every leaf has the same n and `unknown` says how many records could not observe it. for (const [key, names] of Object.entries(EDIT_LEAVES)) if (r.edits && r.edits[key as keyof typeof EDIT_LEAVES] === null) { seen.delete(`edits.${key}`); for (const name of names) seen.set(`edits.${key}.${name}`, null); } // A block that is absent from the record counts as unknown at its own name. for (const block of BLOCKS) if (![...seen.keys()].some(k => k.startsWith(`${block}.`) || k === block)) seen.set(block, null); for (const key of SCALARS) seen.set(key, (r as unknown as Record)[key] ?? (key === "cancelCause" && r.outcome !== "cancelled" ? undefined : null)); for (const [name, value] of seen) { if (name === "cancelCause" && r.outcome !== "cancelled") continue; note(name, value); } } // Dataset records have no evidenced cost components. Keep the aggregate unknown and count coverage per v2 task. fields["edits.costPerVerifiedSuccess"] = { n: v2.length, unknown: v2.length, value: null }; const windowEvents = events.filter(e => { const measured = e.type === "quota" && typeof e.measuredAt === "string" ? Date.parse(e.measuredAt) : NaN; const t = Number.isFinite(measured) ? measured : Date.parse(e.at); return Number.isFinite(t) && t >= from && t <= to; }); const present: Record = {}; const quota: Record = {}; const budget: QualityReport["budget"] = { pauses: 0, resumes: 0, handoffs: null, byFamily: {} }; const attribution: Record = { delivery: 0, single_open: 0, unattributed: 0 }; let preface: NonNullable | null = null; let checkpoints: NonNullable | null = null; let tokenTotal = 0; for (const e of windowEvents) { present[e.type] = (present[e.type] ?? 0) + 1; if (e.type === "task") { budget.handoffs ??= 0; if (e.event === "reassigned" && e.reason === "budget") budget.handoffs++; } else if (e.type === "quota") { const fam = family(e.peer), q = quota[fam] ??= { times: [], measured: 0, event: 0 }; const measured = typeof e.measuredAt === "string" ? Date.parse(e.measuredAt) : NaN; if (Number.isFinite(measured)) { q.measured++; q.times.push(measured); } else { q.event++; q.times.push(Date.parse(e.at)); } } else if (e.type === "budget_pause" || e.type === "budget_resume") { const fam = family(e.peer), row = budget.byFamily[fam] ??= { pauses: 0, resumes: 0 }; if (e.type === "budget_pause") { budget.pauses++; row.pauses++; } else { budget.resumes++; row.resumes++; } } else if (e.type === "preface") { if (Number.isSafeInteger(e.bytes) && e.bytes >= 0 && Number.isSafeInteger(e.lines) && e.lines >= 0) { preface ??= { prefaces: 0, bytes: 0, lines: 0, ordered: 0 }; preface.prefaces++; preface.bytes += e.bytes; preface.lines += e.lines; if (e.ordered === true) preface.ordered++; } } else if (e.type === "checkpoint") { const names = [...new Set((Array.isArray(e.fields) ? e.fields : []).filter((n): n is string => CHECKPOINT_FIELDS.includes(n as never)))]; checkpoints ??= { total: 0, byFieldCount: {}, byField: {} }; checkpoints.total++; checkpoints.byFieldCount[String(names.length)] = (checkpoints.byFieldCount[String(names.length)] ?? 0) + 1; for (const name of names) checkpoints.byField[name] = (checkpoints.byField[name] ?? 0) + 1; } else if (e.type === "tokens" && Number.isFinite(e.n) && e.n > 0) { tokenTotal += e.n; const rule = e.attribution === "delivery" || e.attribution === "single_open" ? e.attribution : "unattributed"; attribution[rule]! += e.n; } } const quotaOut: Record = {}; for (const [fam, q] of Object.entries(quota)) { const times = q.times.filter(Number.isFinite).sort((a, b) => a - b), gaps: number[] = []; for (let i = 1; i < times.length; i++) gaps.push(times[i]! - times[i - 1]!); const over = gaps.filter(g => g > QUOTA_GAP_MS); quotaOut[fam] = { readings: times.length, gaps: over.length, gapMs: over.reduce((sum, g) => sum + g, 0), stalenessP95Ms: nearestRank(gaps, 0.95), clock: { event: q.event, measured: q.measured } }; } const outcomes: QualityReport["outcomes"] = {}; for (const r of tasks) { const fam = family(r.owners.at(-1)), cls = r.class ?? "unknown", row = (outcomes[fam] ??= {})[cls] ??= { approved: 0, cancelled: 0, firstPass: 0, changesRequested: 0 }; row[r.outcome]++; if (r.firstPass) row.firstPass++; row.changesRequested += r.changesRequested; } const share = (n: number): number | null => tokenTotal > 0 ? n / tokenTotal : null; return { schema: QUALITY_SCHEMA, window: { since: window.since !== undefined ? new Date(window.since).toISOString() : null, until: window.until !== undefined && Number.isFinite(window.until) ? new Date(window.until).toISOString() : null }, records: tasks.length, fields: Object.fromEntries(Object.entries(fields).sort(([a], [b]) => a.localeCompare(b))), quota: Object.fromEntries(Object.entries(quotaOut).sort(([a], [b]) => a.localeCompare(b))), budget, outcomes, events: { present: Object.fromEntries(Object.entries(present).sort(([a], [b]) => a.localeCompare(b))), absent: EXPECTED_EVENTS.filter(type => !(type in present)), preface, checkpoints: checkpoints && { total: checkpoints.total, byFieldCount: sortedRecord(checkpoints.byFieldCount), byField: sortedRecord(checkpoints.byField) } }, tokenAttribution: { total: tokenTotal, share: { delivery: share(attribution.delivery!), single_open: share(attribution.single_open!), unattributed: share(attribution.unattributed!) } }, }; }