Repository navigation
Expand file tree
/
Copy pathexecution.ts
More file actions
52 lines (48 loc) · 2.6 KB
/
Copy pathexecution.ts
File metadata and controls
52 lines (48 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import crypto from 'node:crypto';
import type { MathAgentSession } from '../../runtime/agentSession';
import type { SolverProblem } from '../problem';
import type { ContextBundle } from './context';
import type { ResearchOperation } from './value';
import type { ResearchWorker } from './organization';
import { MAX_RESEARCH_NOTES_BYTES } from '../deliveries';
export type ResearchCandidate = Readonly<{
status: string;
worktree: string;
problemReceiptDigest: string;
operation: ResearchOperation;
provenance: Readonly<{ workerId: string; threadId: string }>;
}>;
/** Only an explicit JSON final from this completed turn can add advisory handling decisions. */
export function completedGuidanceDecisions(session: MathAgentSession, turnId: string | undefined, status: string): unknown {
const response = session.lastCompletedResponse;
if (status !== 'completed' || !turnId || response?.turnId !== turnId ||
Buffer.byteLength(response.text) > MAX_RESEARCH_NOTES_BYTES) return;
const text = response.text.trim();
const fence = /^```(?:json|math|math-plan|math-result|math-review)\s*\n([\s\S]*?)\n```$/u.exec(text);
try {
const value: unknown = JSON.parse(fence ? fence[1] : text);
if (value && typeof value === 'object' && !Array.isArray(value))
return (value as Record<string, unknown>).guidanceDecisions;
} catch { /* Ordinary prose and partial JSON never declare a handling decision. */ }
}
/** A closed transport is a failed turn; aborts and other errors keep their original meaning. */
export async function runResearchTurn(session: MathAgentSession, message: string, worktree: string,
signal: AbortSignal, onStarted: (turnId: string) => void): Promise<string> {
let status: string;
try {
status = await session.run(message, crypto.randomUUID(), worktree, onStarted);
} catch (error) {
if (!(error instanceof Error) || !/^math_codex_(?:transport_closed|rpc_timeout:)/u.test(error.message)) throw error;
status = 'failed';
}
signal.throwIfAborted();
return status;
}
/** M1 writes a candidate; M2 subsequently freezes and fingerprints its actual source. */
export async function executeResearch(task: SolverProblem, context: ContextBundle, operation: ResearchOperation,
worker: ResearchWorker, session: MathAgentSession, signal: AbortSignal,
onStarted: (turnId: string) => void): Promise<ResearchCandidate> {
const status = await runResearchTurn(session, context.message, worker.worktree, signal, onStarted);
return { status, worktree: worker.worktree, problemReceiptDigest: task.receiptDigest, operation,
provenance: { workerId: worker.id, threadId: session.threadId } };
}