refactor(orchestrator): return validated worker results (CL20)
This commit is contained in:
parent
6055aeeee9
commit
8a5d0c3ef2
8 changed files with 145 additions and 23 deletions
|
|
@ -102,6 +102,9 @@ describe('retained canonical strategy worker', () => {
|
|||
'%s exact packaged bytes match the native report and repeat identically',
|
||||
async strategyId => {
|
||||
const value = await input(strategyId);
|
||||
if (value.request.strategy.origin !== 'builtin') {
|
||||
throw new Error('Expected a built-in fixture');
|
||||
}
|
||||
const load = prepareReplayLoad(value.dataset, value.request.policy);
|
||||
const direct = runStrategyReplay(
|
||||
load,
|
||||
|
|
@ -117,11 +120,21 @@ describe('retained canonical strategy worker', () => {
|
|||
expect(first.result.build).toEqual(value.request.build);
|
||||
expect(first.result.artifactHash).toBe(value.request.strategy.package.artifact.sha256);
|
||||
expect((await execute(value)).raw).toBe(first.raw);
|
||||
expect(
|
||||
await new NativeBacktestWorker().executeRetained(
|
||||
value,
|
||||
new AbortController().signal,
|
||||
() => {}
|
||||
)
|
||||
).toEqual(first.result);
|
||||
}
|
||||
);
|
||||
|
||||
test('a retained version executes its own bytes when the current constructor differs', async () => {
|
||||
const value = await input();
|
||||
if (value.request.strategy.origin !== 'builtin') {
|
||||
throw new Error('Expected a built-in fixture');
|
||||
}
|
||||
const current = runStrategyReplay(
|
||||
prepareReplayLoad(value.dataset, value.request.policy),
|
||||
[createBuiltinStrategy(value.request.strategy.selection)],
|
||||
|
|
@ -236,7 +249,7 @@ describe('retained canonical strategy worker', () => {
|
|||
test('capture detaches all admitted inputs before the caller can mutate them', async () => {
|
||||
const value = structuredClone(await input());
|
||||
const captured = structuredClone(value.request);
|
||||
const running = new NativeBacktestWorker().execute(
|
||||
const running = new NativeBacktestWorker().executeRetained(
|
||||
value,
|
||||
new AbortController().signal,
|
||||
() => {}
|
||||
|
|
@ -245,7 +258,7 @@ describe('retained canonical strategy worker', () => {
|
|||
value.request.strategy.selection.seed = 222;
|
||||
value.request.build.engineBinarySha256 = 'f'.repeat(64);
|
||||
(value.dataset.bars as unknown[]).length = 0;
|
||||
const result = parseRetainedStrategyResult(await running, captured);
|
||||
const result = await running;
|
||||
expect(result.artifactHash).toBe(captured.strategy.package.artifact.sha256);
|
||||
expect(result.financial.projection.fills.length).toBeGreaterThan(0);
|
||||
});
|
||||
|
|
@ -291,6 +304,61 @@ describe('retained canonical strategy worker', () => {
|
|||
}
|
||||
});
|
||||
|
||||
test.each([
|
||||
[
|
||||
'result.artifactHash = "f".repeat(64)',
|
||||
'Retained strategy result build/artifact identity mismatch',
|
||||
],
|
||||
[
|
||||
'result.build.workerSha256 = "f".repeat(64)',
|
||||
'Retained strategy result build/artifact identity mismatch',
|
||||
],
|
||||
[
|
||||
'result.financial.policy.matching.risk.feeRate = "0"',
|
||||
'Native replay result identity mismatch',
|
||||
],
|
||||
[
|
||||
'result.analytics.metrics.exposureTime = null',
|
||||
'Retained strategy result analytics do not match the frozen policy',
|
||||
],
|
||||
])('typed worker rejects a malformed completed envelope: %s', async (mutation, reason) => {
|
||||
const value = await input();
|
||||
// A trusted fixture artifact intercepts its own child's IPC result. The
|
||||
// supervisor must still validate the envelope before returning typed data.
|
||||
retainedSource(
|
||||
value.request,
|
||||
`${value.request.strategy.package.artifact.content}
|
||||
const originalSend = process.send.bind(process);
|
||||
process.send = (message, callback) => {
|
||||
if (message.type === 'result') {
|
||||
const result = JSON.parse(message.result);
|
||||
${mutation};
|
||||
message.result = JSON.stringify(result);
|
||||
}
|
||||
return originalSend(message, callback);
|
||||
};`
|
||||
);
|
||||
await expect(
|
||||
new NativeBacktestWorker().executeRetained(value, new AbortController().signal, () => {})
|
||||
).rejects.toThrow(reason);
|
||||
});
|
||||
|
||||
test('typed retained results wait for successful child exit', async () => {
|
||||
const value = await input();
|
||||
retainedSource(
|
||||
value.request,
|
||||
`${value.request.strategy.package.artifact.content}
|
||||
const originalSend = process.send.bind(process);
|
||||
process.send = (message, callback) => {
|
||||
if (message.type === 'result') process.exitCode = 23;
|
||||
return originalSend(message, callback);
|
||||
};`
|
||||
);
|
||||
await expect(
|
||||
new NativeBacktestWorker().executeRetained(value, new AbortController().signal, () => {})
|
||||
).rejects.toThrow('code 23');
|
||||
});
|
||||
|
||||
test('alternate and source workers cannot claim the bundled worker identity', async () => {
|
||||
const value = await input();
|
||||
const sourceUrl = new URL(
|
||||
|
|
@ -298,7 +366,11 @@ describe('retained canonical strategy worker', () => {
|
|||
import.meta.url
|
||||
);
|
||||
await expect(
|
||||
new NativeBacktestWorker(sourceUrl).execute(value, new AbortController().signal, () => {})
|
||||
new NativeBacktestWorker(sourceUrl).executeRetained(
|
||||
value,
|
||||
new AbortController().signal,
|
||||
() => {}
|
||||
)
|
||||
).rejects.toThrow('canonical bundled worker');
|
||||
expect(
|
||||
await new NativeBacktestWorker(canonicalWorkerUrl).execute(
|
||||
|
|
@ -342,10 +414,12 @@ describe('retained canonical strategy worker', () => {
|
|||
const worker = new NativeBacktestWorker();
|
||||
const abort = new AbortController();
|
||||
await expect(
|
||||
worker.execute(value, abort.signal, () => abort.abort(new Error('cancel retained replay')))
|
||||
worker.executeRetained(value, abort.signal, () =>
|
||||
abort.abort(new Error('cancel retained replay'))
|
||||
)
|
||||
).rejects.toThrow('cancel retained replay');
|
||||
expect(
|
||||
JSON.parse(await worker.execute(value, new AbortController().signal, () => {})).type
|
||||
).toBe('retainedStrategyResult');
|
||||
expect((await worker.executeRetained(value, new AbortController().signal, () => {})).type).toBe(
|
||||
'retainedStrategyResult'
|
||||
);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue