/** * Generic, truthful agent identity for ship-pr reports. * * Cursor agents keep passing `agentId: 'bc-…'` (unchanged). Any agent can pass * `agent: { kind, id?, url?, name? }` instead. Never fabricate a Cursor id: a * Codex, Claude, or other session omits `agentId` and says what it is. */ export const agentKinds = ['cursor', 'codex', 'claude', 'devin', 'other'] as const export type AgentKind = (typeof agentKinds)[number] export type AgentInput = { /** Which agent ran the ship loop. `other` needs `name`. */ kind: AgentKind /** Real session/task id from that agent's runtime, when one exists. Omit when unknown; never invent. Required for `cursor` (the `bc-…` id). */ id?: string /** Real https link to the session/task, when one exists. Omit when unknown; never invent. */ url?: string /** Short display name, e.g. `Codex cloud`. Required for `other`. */ name?: string } export type ResolvedAgent = | { kind: 'cursor'; cursorAgentId: string; id: string; url?: string; name?: string } | { kind: Exclude; cursorAgentId: null; id?: string; url?: string; name?: string } const agentKeys = ['kind', 'id', 'url', 'name'] const KIND_LABELS: Record = { cursor: 'Cursor', codex: 'Codex', claude: 'Claude', devin: 'Devin', other: 'Agent', } function str(rec: Record, key: string, exportName: string): string | undefined { const value = rec[key] if (value == null) return undefined if (typeof value !== 'string') throw new Error(`${exportName}: agent.${key} must be a string.`) return value.trim() || undefined } /** * Parse `agentId` (Cursor, legacy) and/or `agent` (generic) into one identity. * Returns null only when `required` is false and neither was passed. */ export function parseAgent( input: Record, exportName: string, required: boolean, ): ResolvedAgent | null { const rawId = input.agentId if (rawId != null && typeof rawId !== 'string') throw new Error(`${exportName}: agentId must be a string.`) const agentId = typeof rawId === 'string' ? rawId.trim() || undefined : undefined const raw = input.agent if (raw == null) { if (agentId) return { kind: 'cursor', cursorAgentId: agentId, id: agentId } if (!required) return null throw new Error( `${exportName}: identify the agent. Cursor: agentId 'bc-…' (or agent { kind: 'cursor', id }). Anything else: agent { kind: ${agentKinds .filter((k) => k !== 'cursor') .map((k) => `'${k}'`) .join(' | ')}, id?, url?, name? }. Never fabricate a Cursor id.`, ) } if (typeof raw !== 'object' || Array.isArray(raw)) { throw new Error(`${exportName}: agent must be an object { kind, id?, url?, name? }.`) } const rec = raw as Record const unknown = Object.keys(rec).filter((k) => !agentKeys.includes(k) && rec[k] !== undefined) if (unknown.length) { throw new Error(`${exportName}: unknown agent key(s) ${unknown.map((k) => JSON.stringify(k)).join(', ')}. Accepted: ${agentKeys.join(', ')}.`) } const kindRaw = typeof rec.kind === 'string' ? rec.kind.trim().toLowerCase() : '' const kind = agentKinds.find((k) => k === kindRaw) if (!kind) throw new Error(`${exportName}: agent.kind must be one of ${agentKinds.join(' | ')}; got ${JSON.stringify(rec.kind ?? null)}.`) const id = str(rec, 'id', exportName) const url = str(rec, 'url', exportName) const name = str(rec, 'name', exportName) if (url) { let ok = false try { ok = new URL(url).protocol === 'https:' } catch {} if (!ok) throw new Error(`${exportName}: agent.url must be an https URL (omit it when there is no real link).`) } if (kind === 'cursor') { const cursorId = id ?? agentId if (!cursorId) throw new Error(`${exportName}: agent kind 'cursor' needs the bc-… id (agent.id or agentId).`) if (id && agentId && id !== agentId) throw new Error(`${exportName}: agentId and agent.id disagree; pass one Cursor id.`) return { kind, cursorAgentId: cursorId, id: cursorId, ...(url ? { url } : {}), ...(name ? { name } : {}) } } if (agentId) { throw new Error(`${exportName}: agentId is only for Cursor agents (bc-…). Drop it for agent kind '${kind}'; never fabricate a Cursor id.`) } if (kind === 'other' && !name) throw new Error(`${exportName}: agent kind 'other' needs agent.name.`) return { kind, cursorAgentId: null, ...(id ? { id } : {}), ...(url ? { url } : {}), ...(name ? { name } : {}) } } /** Display label for the report's Agent fact, e.g. `Codex · task_123` or `Codex cloud`. */ export function agentLabel(agent: ResolvedAgent): string { const base = agent.name ?? KIND_LABELS[agent.kind] return agent.id ? `${base} · ${agent.id}` : base }