feat(research): inspect complete canonical run results (RP27)
This commit is contained in:
parent
65c6594879
commit
66af201dad
9 changed files with 269 additions and 20 deletions
|
|
@ -1,5 +1,6 @@
|
|||
/** Authorized research API client. No strategy import, financial simulation, or trading controls. */
|
||||
import { z } from 'zod';
|
||||
import { parseCanonicalRunResult } from '@stock-bot/research/results';
|
||||
import {
|
||||
chronologicalValidationInputSchema,
|
||||
chronologicalValidationSubmissionSchema,
|
||||
|
|
@ -762,6 +763,18 @@ export class ResearchClient {
|
|||
key
|
||||
);
|
||||
}
|
||||
canonicalRunResult(runId: string, signal?: AbortSignal) {
|
||||
runId = uuid.parse(runId);
|
||||
return this.#request(
|
||||
`runs/${id(runId)}/results`,
|
||||
'GET',
|
||||
200,
|
||||
value => parseCanonicalRunResult(value, runId),
|
||||
undefined,
|
||||
undefined,
|
||||
signal
|
||||
);
|
||||
}
|
||||
artifact(artifactId: string, signal?: AbortSignal) {
|
||||
artifactId = uuid.parse(artifactId);
|
||||
return this.#request(
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
import { createHash } from 'node:crypto';
|
||||
import { describe, expect, test } from 'bun:test';
|
||||
import type { ExperimentDetail } from '@stock-bot/stock-config/experiments';
|
||||
import { canonicalResultFixture } from '../../test/fixtures/canonical-result';
|
||||
import { comparisonFixture, tableFixture } from '../benchmark-experiment-tables';
|
||||
import {
|
||||
pollResearch,
|
||||
|
|
@ -35,6 +36,70 @@ function client(handler: (call: Call, index: number) => Response | Promise<Respo
|
|||
return { value, calls };
|
||||
}
|
||||
const ordinary = () => tableFixture(1) as ExperimentDetail;
|
||||
|
||||
describe('canonical run inspection', () => {
|
||||
test('reads complete native finances and retained inputs through authenticated GET', async () => {
|
||||
const result = canonicalResultFixture();
|
||||
const before = structuredClone(result);
|
||||
const { value, calls } = client(() => Response.json(result));
|
||||
const saved = await value.canonicalRunResult(result.runId.toUpperCase());
|
||||
expect(saved).toEqual(before);
|
||||
expect(result).toEqual(before);
|
||||
expect<string>(saved.financial.projection.valuation.equity).toBe('9969.30000000');
|
||||
expect<string>(saved.financial.projection.valuation.fees).toBe('0.70000000');
|
||||
expect(saved.financial.projection.fills).toHaveLength(2);
|
||||
expect(saved.analytics.metrics.roundTripCount).toBeNull();
|
||||
expect(calls).toHaveLength(1);
|
||||
expect(calls[0]!.url.pathname).toBe(`/api/v2/runs/${result.runId}/results`);
|
||||
expect(calls[0]!.init.method).toBe('GET');
|
||||
expect(calls[0]!.body).toBeUndefined();
|
||||
expect(new Headers(calls[0]!.init.headers).get('authorization')).toBe(`Bearer ${token}`);
|
||||
expect(new Headers(calls[0]!.init.headers).has('idempotency-key')).toBe(false);
|
||||
});
|
||||
|
||||
test('invalid or legacy result responses fail read-only without retrying', async () => {
|
||||
const result = canonicalResultFixture();
|
||||
const changed = structuredClone(result);
|
||||
changed.analytics.metrics.totalReturn = 99;
|
||||
for (const response of [
|
||||
null,
|
||||
{},
|
||||
{ runId: result.runId, engine: 'native', status: 'completed' },
|
||||
{ ...result, runId: projectId },
|
||||
changed,
|
||||
]) {
|
||||
const { value, calls } = client(() => Response.json(response));
|
||||
await expect(value.canonicalRunResult(result.runId)).rejects.toMatchObject({
|
||||
uncertain: false,
|
||||
status: null,
|
||||
});
|
||||
expect(calls).toHaveLength(1);
|
||||
expect(calls[0]!.init.method).toBe('GET');
|
||||
}
|
||||
});
|
||||
|
||||
test('unavailable results, aborts and invalid IDs never create replacement work', async () => {
|
||||
for (const status of [401, 404, 500]) {
|
||||
const { value, calls } = client(() =>
|
||||
Response.json({ error: 'Result unavailable' }, { status })
|
||||
);
|
||||
await expect(value.canonicalRunResult(projectId)).rejects.toMatchObject({
|
||||
status,
|
||||
uncertain: false,
|
||||
});
|
||||
expect(calls).toHaveLength(1);
|
||||
}
|
||||
const controller = new AbortController();
|
||||
const stalled = client(() => new Promise<Response>(() => {}));
|
||||
expect(() => stalled.value.canonicalRunResult('../orders')).toThrow();
|
||||
expect(stalled.calls).toHaveLength(0);
|
||||
const pending = stalled.value.canonicalRunResult(projectId, controller.signal);
|
||||
controller.abort();
|
||||
await expect(pending).rejects.toMatchObject({ uncertain: false });
|
||||
expect(stalled.calls).toHaveLength(1);
|
||||
});
|
||||
});
|
||||
|
||||
function crossStudyFinding() {
|
||||
const detail = ordinary();
|
||||
const input = {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue