feat(research): cut over canonical runtime and enforce agent budgets

This commit is contained in:
Boki 2026-10-24 16:47:19 -04:00
parent ab08db82fd
commit bbf0dd4453
103 changed files with 6138 additions and 3112 deletions

View file

@ -262,6 +262,15 @@ export interface ReplayController {
): FinancialStrategyDecision[];
committed?(outcome: ReplaySubmissionOutcome): void;
}
/** Restricted hosts communicate asynchronously; financial transitions still run
* on the same native session and the same ordered pull loop. */
export interface AsyncReplayController {
stage(
context: DecisionContext,
observations: readonly PublishedObservation[]
): Promise<FinancialStrategyDecision[]>;
committed?(outcome: ReplaySubmissionOutcome): Promise<void>;
}
/* eslint-disable no-redeclare -- TypeScript overloads preserve the existing trace result type. */
export function runReplayConformance(
session: NativeReplaySession,
@ -329,6 +338,63 @@ export function runReplaySession(
controller: ReplayController,
options: ReplayOptions = {}
) {
const loop = replaySteps(session, options);
try {
let step = loop.next();
while (!step.done) {
const action = step.value;
step = loop.next(
action.kind === 'stage'
? controller.stage(action.context, action.observations)
: controller.committed?.(action.outcome)
);
}
return step.value;
} finally {
// A controller may throw while the generator is suspended at a decision.
// Closing it runs the native disposal even without another advance call.
loop.return(undefined as never);
}
}
export async function runReplaySessionAsync(
session: NativeReplaySession,
controller: AsyncReplayController,
options: ReplayOptions = {}
) {
const loop = replaySteps(session, options);
try {
let step = loop.next();
while (!step.done) {
const action = step.value;
step = loop.next(
action.kind === 'stage'
? await controller.stage(action.context, action.observations)
: await controller.committed?.(action.outcome)
);
}
return step.value;
} finally {
loop.return(undefined as never);
}
}
type ReplayAction =
| {
kind: 'stage';
context: DecisionContext;
observations: readonly PublishedObservation[];
}
| { kind: 'committed'; outcome: ReplaySubmissionOutcome };
function* replaySteps(
session: NativeReplaySession,
options: ReplayOptions
): Generator<
ReplayAction,
{ result: ReplayOutput; metrics: ReplayMetrics },
FinancialStrategyDecision[] | void
> {
const metrics: ReplayMetrics = {
advanceCalls: 0,
submitCalls: 0,
@ -386,7 +452,10 @@ export function runReplaySession(
observations = [];
// Stage all decisions before one admission. A thrown fixture disposes
// the attempt; it cannot publish a partial barrier or successful run.
const decisions = controller.stage(context, publishedDelta);
const decisions = yield { kind: 'stage', context, observations: publishedDelta };
if (!Array.isArray(decisions)) {
throw new Error('Replay controller did not return decisions');
}
metrics.decisionMs += performance.now() - start;
const submitStart = performance.now(),
outcome = session.submit(context.barrier.barrier_id, decisions);
@ -395,11 +464,9 @@ export function runReplaySession(
if (typeof outcome.status === 'object') {
throw new Error(outcome.status.Failed.diagnostic);
}
if (controller.committed) {
const commitStart = performance.now();
controller.committed(outcome);
metrics.decisionMs += performance.now() - commitStart;
}
const commitStart = performance.now();
yield { kind: 'committed', outcome };
metrics.decisionMs += performance.now() - commitStart;
break;
}
}