// Adapted from deepseek-ai/deepseek-harness at 639ed015 (MIT); see NOTICE. // Local changes: exported snapshots, supplementary facts, budgets and retained authorization. import type { Agent } from '@deepseek-ai/dsh-agent' import type {} from '@deepseek-ai/dsh-agent-instructions' import type {} from '@deepseek-ai/dsh-subagent' import type { ContentBlock, MessageSource, ToolSchema, ToolCallId } from '@deepseek-ai/dsh-llm' import type { SessionEvent } from '@deepseek-ai/dsh-session' import type { ToolExecution } from '@deepseek-ai/dsh-tools' import { deepFreeze } from '@deepseek-ai/dsh-util-values' type ReviewSourceRole = | 'human-instruction' | 'direct-parent-instruction' | 'constraint' | 'checkpoint' | 'fact' interface HistoricalUserMessage { readonly kind: 'user-message' readonly seq: SessionEvent['seq'] readonly blockIndex?: number readonly role: ReviewSourceRole readonly source: MessageSource readonly content: readonly ContentBlock[] } interface HistoricalToolCall { readonly kind: 'tool-call' readonly role: 'fact' readonly mode: 'native' | 'ptc-inner' readonly name: string readonly arguments: string } type HistoricalEntry = HistoricalUserMessage | HistoricalToolCall interface PendingAction { readonly mode: 'native' | 'ptc-inner' readonly name: string readonly description: string readonly parameters: Record readonly arguments: unknown } export interface ReviewSnapshot { readonly provider: string readonly model: string readonly cwd: string readonly projectInstructions: readonly HistoricalUserMessage[] readonly history: readonly HistoricalEntry[] readonly action: PendingAction } type NativeCallEvent = Extract type PtcStartEvent = Extract interface StepIdentity { readonly turn: number readonly step: number } interface ScopedPtcStart { readonly event: PtcStartEvent readonly step: StepIdentity } /** Return JSON text for one immutable logged value. */ function json(value: unknown): string { const rendered = JSON.stringify(value, null, 2) as string | undefined /* v8 ignore next -- accepted Session facts and frozen review snapshots are lossless JSON by contract. */ if (rendered === undefined) throw new Error('auto-review: a required value is not JSON-serializable') return rendered } /** Recreate the agent-loop's parse of one native call's logged raw arguments. */ function parseLoggedArguments(raw: string): unknown { if (raw === '') return {} try { return JSON.parse(raw) } catch { return raw } } /** Compare two lossless-JSON values without retaining aliases. */ function sameJson(left: unknown, right: unknown): boolean { return JSON.stringify(left) === JSON.stringify(right) } /** Whether one logged JSON value is an object record rather than null or an array. */ function isRecord(value: unknown): value is Record { return value !== null && typeof value === 'object' && !Array.isArray(value) } /** Validate the schema fields that must be present in a logged pending action. */ function loggedSchema( value: { readonly description?: unknown; readonly parameters?: unknown }, expectedName: string, mode: 'native' | 'PTC', ): ToolSchema { if (typeof value.description !== 'string' || !isRecord(value.parameters)) { throw new Error(`auto-review: the pending ${mode} tool schema is incomplete`) } return { name: expectedName, description: value.description, parameters: value.parameters, } } /** Whether this visible message is a durable shipped-Web human instruction. */ function isHumanInstruction(source: MessageSource): boolean { return source.kind === 'user' && typeof (source as { readonly rpcId?: unknown }).rpcId === 'string' } /** Whether this visible context is the current project-instruction source. */ function isProjectInstruction(source: MessageSource): boolean { return source.kind === 'agent-instructions' } /** Whether this source is a compaction checkpoint. */ function isCheckpoint(source: MessageSource): boolean { const kind: string = source.kind return kind === 'compact-checkpoint' } /** Whether this message was durably attributed to the child's direct parent. */ function isDirectParentInstruction(source: MessageSource, parentSession: string | undefined): boolean { return parentSession !== undefined && source.kind === 'agent-message' && (source as { readonly senderSessionId?: unknown }).senderSessionId === parentSession } /** Find the visible-role identity of the in-process child's creation prompt. */ function directParentInitialPromptSeq( agent: Agent, events: readonly SessionEvent[], ): SessionEvent['seq'] | undefined { const { session } = agent if (session.header.origin !== 'subagent' || session.header.parentSession === undefined) return undefined let passedCreationBoundary = false for (const event of events) { if (!session.isOwnSeq(event.seq)) continue if (event.type === 'subagent/descriptor') { passedCreationBoundary = true continue } if (passedCreationBoundary && event.type === 'user/message' && event.data.source.kind === 'user' && !isHumanInstruction(event.data.source)) { return event.seq } } return undefined } /** Assign one retained text block its fixed instruction, constraint, summary, or fact role. */ function textRole( source: MessageSource, seq: SessionEvent['seq'], initialPromptSeq: SessionEvent['seq'] | undefined, parentSession: string | undefined, ): ReviewSourceRole { if (isHumanInstruction(source)) return 'human-instruction' if (seq === initialPromptSeq || isDirectParentInstruction(source, parentSession)) { return 'direct-parent-instruction' } if (isCheckpoint(source)) return 'checkpoint' return 'fact' } /** Partition one visible user-role message into role-labelled retained blocks. */ function filteredUserEntries( seq: SessionEvent['seq'], source: MessageSource, content: readonly ContentBlock[], initialPromptSeq: SessionEvent['seq'] | undefined, parentSession: string | undefined, ): HistoricalUserMessage[] { return content.map((block, blockIndex) => ({ kind: 'user-message', seq, blockIndex, role: block.type === 'text' ? textRole(source, seq, initialPromptSeq, parentSession) : 'fact', source, content: [block], })) } /** Copy the turn and step identity carried by one core execution event. */ function stepIdentity(data: { readonly turn: number; readonly step: number }): StepIdentity { return { turn: data.turn, step: data.step } } /** Compare two turn-and-step identities. */ function sameStep(left: StepIdentity, right: StepIdentity): boolean { return left.turn === right.turn && left.step === right.step } /** Key one call id inside the step that owns its lifecycle. */ function scopedCallKey(step: StepIdentity, callId: ToolCallId): string { return `${step.turn}\0${step.step}\0${callId}` } /** Assign each PTC start to the step open when it was logged. */ function scopePtcStarts(events: readonly SessionEvent[]): { readonly starts: readonly ScopedPtcStart[] readonly openStep: StepIdentity | undefined } { const starts: ScopedPtcStart[] = [] let openStep: StepIdentity | undefined for (const event of events) { if (event.type === 'turn/start' || event.type === 'turn/end') { openStep = undefined continue } if (event.type === 'step/start') { openStep = stepIdentity(event.data) continue } if (event.type === 'step/end') { openStep = undefined continue } if (event.type !== 'tool/ptc-dispatch-start') continue if (openStep === undefined) { throw new Error('auto-review: a PTC call has no owning step in the session log') } starts.push({ event, step: openStep }) } return { starts, openStep } } /** Resolve one native action from its visible call and latest request header. */ function nativeAction( exec: ToolExecution, headerTools: readonly ToolSchema[] | undefined, logged: Extract, ): PendingAction { if (logged.data.name !== exec.name || !sameJson(parseLoggedArguments(logged.data.arguments), exec.arguments)) { throw new Error('auto-review: the pending native call disagrees with its logged action') } const candidates: readonly unknown[] = Array.isArray(headerTools) ? headerTools : [] const schemas = candidates.filter((schema): schema is Record => isRecord(schema) && schema['name'] === exec.name) const [candidate] = schemas if (candidate === undefined || schemas.length !== 1) { throw new Error('auto-review: the pending native tool schema is missing or ambiguous') } const schema = loggedSchema(candidate, exec.name, 'native') return { mode: 'native', name: schema.name, description: schema.description, parameters: schema.parameters, arguments: exec.arguments, } } /** Resolve a PTC inner action from its binding schema and logged identity. */ function ptcAction( exec: ToolExecution, start: ScopedPtcStart, visibleParentKeys: ReadonlySet, ): PendingAction { const { event } = start if (!visibleParentKeys.has(scopedCallKey(start.step, event.data.parentCallId)) || event.data.rootCallId !== exec.rootCallId || event.data.name !== exec.name || !sameJson(event.data.arguments, exec.arguments)) { throw new Error('auto-review: the pending PTC call disagrees with its logged action') } if (exec.schema === undefined || exec.schema.name !== exec.name) { throw new Error('auto-review: the pending PTC binding schema is missing or inconsistent') } const schema = loggedSchema(exec.schema, exec.name, 'PTC') return { mode: 'ptc-inner', name: schema.name, description: schema.description, parameters: schema.parameters, arguments: exec.arguments, } } /** * Freeze the five reviewer sections from one session and pending execution. * @param agent - agent whose durable surface and request header authorize the call. * @param exec - immutable pending execution. * @returns the exact route and four data sections paired with {@link REVIEW_POLICY}. */ export function snapshotAutoReview(agent: Agent, exec: ToolExecution): ReviewSnapshot { const { session } = agent // The reviewer's risk inputs are the whole action history: earlier native calls // and PTC starts carry the authorizations and duplicate identities this call is // compared against, and the direct parent's initial prompt sets the delegated // scope. No projection or paged reader exposes those records yet. // oxlint-disable-next-line typescript/no-deprecated -- Reviewer needs the whole action history; no projection or paged reader exists yet. const events = session.snapshotEvents() const nodes = [...session.surface.nodes] const header = session.requestHeader() if (header === undefined || header.config.provider.length === 0 || header.config.model.length === 0) { throw new Error('auto-review: no complete request-header route is available') } const cwd = session.header.cwd if (cwd === undefined || cwd.length === 0) { throw new Error('auto-review: the session has no working directory') } const nativeCalls = events.filter((event): event is NativeCallEvent => event.type === 'tool/call') const { starts, openStep: currentStep } = scopePtcStarts(events) const initialPromptSeq = directParentInitialPromptSeq(agent, events) const nativeByScopedId = new Map() for (const event of nativeCalls) { const key = scopedCallKey(stepIdentity(event.data), event.data.callId) const bucket = nativeByScopedId.get(key) if (bucket === undefined) nativeByScopedId.set(key, [event]) else bucket.push(event) } const startsByParent = new Map() const startsBySubCall = new Map() for (const start of starts) { const subCallKey = scopedCallKey(start.step, start.event.data.subCallId) if (startsBySubCall.has(subCallKey)) { throw new Error('auto-review: a PTC call identity is ambiguous in the session log') } startsBySubCall.set(subCallKey, start) const parentKey = scopedCallKey(start.step, start.event.data.parentCallId) const bucket = startsByParent.get(parentKey) if (bucket === undefined) startsByParent.set(parentKey, [start]) else bucket.push(start) } if (currentStep === undefined) { throw new Error('auto-review: the pending call has no open step in the session log') } const currentRootCalls = nativeByScopedId.get(scopedCallKey(currentStep, exec.rootCallId)) ?? [] const currentRootCall = currentRootCalls[0] if (currentRootCall === undefined || currentRootCalls.length !== 1) { throw new Error('auto-review: the pending root call is missing or ambiguous in the session log') } const currentPtcStart = exec.parent === undefined ? undefined : startsBySubCall.get(scopedCallKey(currentStep, exec.callId)) if (exec.parent !== undefined && currentPtcStart === undefined) { throw new Error('auto-review: the pending PTC call is missing or ambiguous in the session log') } const projectInstructions: HistoricalUserMessage[] = [] const history: HistoricalEntry[] = [] const visibleParentKeys = new Set() let passedCurrentRoot = false // The durable log retains exact source attribution after surface compaction. // Keep all trusted instructions in chronological order; summaries stay facts. for (const event of events) { if (event.type !== 'user/message') continue if (isProjectInstruction(event.data.source)) { projectInstructions.push({ kind: 'user-message', seq: event.seq, role: 'constraint', source: event.data.source, content: event.data.content }) } else { history.push(...filteredUserEntries(event.seq, event.data.source, event.data.content, initialPromptSeq, session.header.parentSession).filter(entry => entry.role === 'human-instruction' || entry.role === 'direct-parent-instruction')) } } for (const seq of nodes) { // Surface nodes are event indexes produced by this Session's validated fold. // oxlint-disable-next-line typescript/no-non-null-assertion const event = events[seq]! if (event.type === 'user/message') { if (event.data.source.kind === 'tool') continue if (!isProjectInstruction(event.data.source)) { history.push(...filteredUserEntries( event.seq, event.data.source, event.data.content, initialPromptSeq, session.header.parentSession, ).filter(entry => entry.role === 'fact' || entry.role === 'checkpoint')) } continue } if (event.type !== 'assistant/message') continue const messageStep = stepIdentity(event.data) const isCurrentMessage = sameStep(messageStep, currentStep) let sawUnstartedSibling = false for (const block of event.data.message.content) { if (block.type !== 'tool-call') continue const key = scopedCallKey(messageStep, block.id) const isCurrentRoot = isCurrentMessage && block.id === exec.rootCallId if (isCurrentRoot && passedCurrentRoot) { throw new Error('auto-review: the pending root call is ambiguous in the current surface') } const calls = nativeByScopedId.get(key) ?? [] if (calls.length > 1) { throw new Error('auto-review: a native call identity is ambiguous in the session log') } const call = calls[0] const startsForCall = startsByParent.get(key) ?? [] if (call === undefined) { if (isCurrentMessage && !passedCurrentRoot) { throw new Error('auto-review: a visible call before the pending root is missing from the session log') } if (startsForCall.length > 0) { throw new Error('auto-review: an unstarted visible call has logged PTC dispatches') } sawUnstartedSibling = true continue } if (sawUnstartedSibling) { throw new Error('auto-review: visible native call logs do not form a started prefix') } if (call.data.name !== block.name || call.data.arguments !== block.arguments) { throw new Error('auto-review: a visible tool call disagrees with its logged action') } visibleParentKeys.add(key) if (call !== currentRootCall || exec.parent !== undefined) { history.push({ kind: 'tool-call', role: 'fact', mode: 'native', name: call.data.name, arguments: call.data.arguments, }) } for (const start of startsForCall) { if (start === currentPtcStart) continue history.push({ kind: 'tool-call', role: 'fact', mode: 'ptc-inner', name: start.event.data.name, arguments: json(start.event.data.arguments), }) } if (isCurrentRoot) passedCurrentRoot = true } } if (!passedCurrentRoot) { throw new Error('auto-review: the pending root call is missing from the current surface') } const action = exec.parent === undefined ? nativeAction(exec, header.tools, currentRootCall) // The branch above established that every nested execution has one scoped start. // oxlint-disable-next-line typescript/no-non-null-assertion : ptcAction(exec, currentPtcStart!, visibleParentKeys) return deepFreeze({ provider: header.config.provider, model: header.config.model, cwd, projectInstructions, history, action, }) }