feat(research): run stateful TypeScript strategies on canonical replay
This commit is contained in:
parent
0870b55830
commit
2bcadbd117
14 changed files with 1864 additions and 33 deletions
|
|
@ -25,6 +25,7 @@ import {
|
|||
type ReplayResult,
|
||||
type ReplayResultFormat,
|
||||
type ReplayStep,
|
||||
type ReplaySubmissionOutcome,
|
||||
type ReplayTraceRequest,
|
||||
} from '../contracts/native-replay';
|
||||
import { validateDatasetV2 } from '../data/historical-dataset-v2';
|
||||
|
|
@ -246,12 +247,21 @@ type Decide = (
|
|||
context: DecisionContext,
|
||||
observations: readonly PublishedObservation[]
|
||||
) => FinancialStrategyIntent;
|
||||
interface ReplayOptions {
|
||||
export interface ReplayOptions {
|
||||
eventBudget?: number;
|
||||
reverseCallbacks?: boolean;
|
||||
onProgress?: (step: ReplayStep) => void;
|
||||
resultFormat?: ReplayResultFormat;
|
||||
}
|
||||
/** Internal trusted-host seam. All due decisions are staged before one native
|
||||
* submission. A controller never receives the native handle. */
|
||||
export interface ReplayController {
|
||||
stage(
|
||||
context: DecisionContext,
|
||||
observations: readonly PublishedObservation[]
|
||||
): FinancialStrategyDecision[];
|
||||
committed?(outcome: ReplaySubmissionOutcome): void;
|
||||
}
|
||||
/* eslint-disable no-redeclare -- TypeScript overloads preserve the existing trace result type. */
|
||||
export function runReplayConformance(
|
||||
session: NativeReplaySession,
|
||||
|
|
@ -281,6 +291,44 @@ export function runReplayConformance(
|
|||
options: ReplayOptions = {}
|
||||
) {
|
||||
/* eslint-enable no-redeclare */
|
||||
let sequence = 0;
|
||||
return runReplaySession(
|
||||
session,
|
||||
{
|
||||
stage(context, observations) {
|
||||
const owners = [...context.barrier.owner_ids];
|
||||
if (options.reverseCallbacks) {
|
||||
owners.reverse();
|
||||
}
|
||||
const decisions = owners.map(ownerId =>
|
||||
parseFinancialStrategyDecision({
|
||||
version: 1,
|
||||
sessionId: session.identity.sessionId,
|
||||
barrierId: context.barrier.barrier_id,
|
||||
revision: context.barrier.revision,
|
||||
decision: {
|
||||
ownerId,
|
||||
decisionId: JSON.stringify(['trace', sequence, ownerId]),
|
||||
sequence,
|
||||
},
|
||||
intent: decide(ownerId, context, observations),
|
||||
})
|
||||
);
|
||||
sequence++;
|
||||
return decisions;
|
||||
},
|
||||
},
|
||||
options
|
||||
);
|
||||
}
|
||||
|
||||
/** One pull loop for conformance and stateful programs. Always disposes the
|
||||
* attempt, including ambiguous submissions or failures in host callbacks. */
|
||||
export function runReplaySession(
|
||||
session: NativeReplaySession,
|
||||
controller: ReplayController,
|
||||
options: ReplayOptions = {}
|
||||
) {
|
||||
const metrics: ReplayMetrics = {
|
||||
advanceCalls: 0,
|
||||
submitCalls: 0,
|
||||
|
|
@ -298,7 +346,6 @@ export function runReplayConformance(
|
|||
disposeMs: 0,
|
||||
};
|
||||
let observations: PublishedObservation[] = [];
|
||||
let sequence = 0;
|
||||
try {
|
||||
for (;;) {
|
||||
const start = performance.now(),
|
||||
|
|
@ -335,30 +382,12 @@ export function runReplayConformance(
|
|||
case 'decisionBatch': {
|
||||
const context = step.step,
|
||||
start = performance.now();
|
||||
const owners = [...context.barrier.owner_ids];
|
||||
if (options.reverseCallbacks) {
|
||||
owners.reverse();
|
||||
}
|
||||
const publishedDelta = Object.freeze(observations);
|
||||
observations = [];
|
||||
// Stage all decisions before one admission. A thrown fixture disposes
|
||||
// the attempt; it cannot publish a partial barrier or successful run.
|
||||
const decisions = owners.map(ownerId =>
|
||||
parseFinancialStrategyDecision({
|
||||
version: 1,
|
||||
sessionId: session.identity.sessionId,
|
||||
barrierId: context.barrier.barrier_id,
|
||||
revision: context.barrier.revision,
|
||||
decision: {
|
||||
ownerId,
|
||||
decisionId: JSON.stringify(['trace', sequence, ownerId]),
|
||||
sequence,
|
||||
},
|
||||
intent: decide(ownerId, context, publishedDelta),
|
||||
})
|
||||
);
|
||||
const decisions = controller.stage(context, publishedDelta);
|
||||
metrics.decisionMs += performance.now() - start;
|
||||
sequence++;
|
||||
const submitStart = performance.now(),
|
||||
outcome = session.submit(context.barrier.barrier_id, decisions);
|
||||
metrics.submitMs += performance.now() - submitStart;
|
||||
|
|
@ -366,6 +395,11 @@ export function runReplayConformance(
|
|||
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;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue