T-84: M1.3d — trial_ledger writer + cluster N̂-deflated Sharpe reader in @stock-bot/research (append one row per horizon, vault-fenced sample_end, PostgreSQL integration test + local benchmark)
This commit is contained in:
parent
bbc31b9cd7
commit
9b57ea9677
3 changed files with 35 additions and 5 deletions
|
|
@ -172,6 +172,7 @@ export interface ClusterDeflatedSharpeResult {
|
||||||
}
|
}
|
||||||
|
|
||||||
const DATE_ONLY_PATTERN = /^\d{4}-\d{2}-\d{2}$/;
|
const DATE_ONLY_PATTERN = /^\d{4}-\d{2}-\d{2}$/;
|
||||||
|
const UTC_ISO_TIMESTAMP_PATTERN = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d{1,6})?Z$/;
|
||||||
const AGENT_PRINCIPAL_PATTERN =
|
const AGENT_PRINCIPAL_PATTERN =
|
||||||
/^agent:[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/;
|
/^agent:[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/;
|
||||||
const UUID_PATTERN = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
const UUID_PATTERN = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
||||||
|
|
@ -183,10 +184,11 @@ const UNIVERSE_MARKET_SET: ReadonlySet<string> = new Set(UNIVERSE_MARKETS);
|
||||||
const HORIZON_BUCKET_SET: ReadonlySet<string> = new Set(HORIZON_BUCKETS);
|
const HORIZON_BUCKET_SET: ReadonlySet<string> = new Set(HORIZON_BUCKETS);
|
||||||
|
|
||||||
const COLUMNS_PER_ROW = 14;
|
const COLUMNS_PER_ROW = 14;
|
||||||
|
const UTC_CREATED_AT_SQL = `to_char(created_at AT TIME ZONE 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS.US"Z"') AS created_at`;
|
||||||
|
|
||||||
const SELECT_TRIAL_COLUMNS = `
|
const SELECT_TRIAL_COLUMNS = `
|
||||||
trial_id,
|
trial_id,
|
||||||
created_at,
|
${UTC_CREATED_AT_SQL},
|
||||||
principal_id,
|
principal_id,
|
||||||
campaign_id,
|
campaign_id,
|
||||||
stage,
|
stage,
|
||||||
|
|
@ -652,6 +654,9 @@ function normalizeAsOf(asOf: unknown): string | undefined {
|
||||||
{ from: '1970-01-01', to: '1970-01-01', asOf, scope: 'vault_evaluation_only' },
|
{ from: '1970-01-01', to: '1970-01-01', asOf, scope: 'vault_evaluation_only' },
|
||||||
{ vaultCutoffTs: 0, vaultOperatorAuthorized: true }
|
{ vaultCutoffTs: 0, vaultOperatorAuthorized: true }
|
||||||
);
|
);
|
||||||
|
if (UTC_ISO_TIMESTAMP_PATTERN.test(asOf)) {
|
||||||
|
return asOf;
|
||||||
|
}
|
||||||
return new Date(fenced.asOfMs).toISOString();
|
return new Date(fenced.asOfMs).toISOString();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error instanceof MarketDataStoreValidationError) {
|
if (error instanceof MarketDataStoreValidationError) {
|
||||||
|
|
@ -673,6 +678,9 @@ function formatUtcTimestamp(value: unknown, field: string): string {
|
||||||
if (!Number.isFinite(ms)) {
|
if (!Number.isFinite(ms)) {
|
||||||
throw new TrialLedgerError('INVALID_ROW', `Invalid timestamp string for "${field}": "${value}".`);
|
throw new TrialLedgerError('INVALID_ROW', `Invalid timestamp string for "${field}": "${value}".`);
|
||||||
}
|
}
|
||||||
|
if (UTC_ISO_TIMESTAMP_PATTERN.test(value)) {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
return new Date(ms).toISOString();
|
return new Date(ms).toISOString();
|
||||||
}
|
}
|
||||||
throw new TrialLedgerError('INVALID_ROW', `Unexpected timestamp value for "${field}".`);
|
throw new TrialLedgerError('INVALID_ROW', `Unexpected timestamp value for "${field}".`);
|
||||||
|
|
@ -1144,7 +1152,7 @@ export async function recordTrials(
|
||||||
hypothesis_id, expression_hash, sample_start, sample_end, n_obs,
|
hypothesis_id, expression_hash, sample_start, sample_end, n_obs,
|
||||||
sharpe_unann, skewness, kurtosis, daily_series
|
sharpe_unann, skewness, kurtosis, daily_series
|
||||||
) VALUES ${valueTuples.join(', ')}
|
) VALUES ${valueTuples.join(', ')}
|
||||||
RETURNING trial_id, created_at`;
|
RETURNING trial_id, ${UTC_CREATED_AT_SQL}`;
|
||||||
|
|
||||||
const result = await executor.query<Record<string, unknown>>(sql, params);
|
const result = await executor.query<Record<string, unknown>>(sql, params);
|
||||||
if (result.rows.length !== validated.length) {
|
if (result.rows.length !== validated.length) {
|
||||||
|
|
|
||||||
|
|
@ -497,7 +497,8 @@ describe('recordTrials', () => {
|
||||||
const receipts = await recordTrials(executor, batch);
|
const receipts = await recordTrials(executor, batch);
|
||||||
expect(calls).toHaveLength(1);
|
expect(calls).toHaveLength(1);
|
||||||
expect(calls[0]!.text).toContain('INSERT INTO trial_ledger');
|
expect(calls[0]!.text).toContain('INSERT INTO trial_ledger');
|
||||||
expect(calls[0]!.text).toContain('RETURNING trial_id, created_at');
|
expect(calls[0]!.text).toContain('RETURNING trial_id,');
|
||||||
|
expect(calls[0]!.text).toContain('AS created_at');
|
||||||
expect(calls[0]!.params).toHaveLength(3 * 14);
|
expect(calls[0]!.params).toHaveLength(3 * 14);
|
||||||
expect(receipts).toEqual([
|
expect(receipts).toEqual([
|
||||||
{ trialId: 101, createdAt: '2025-04-01T10:00:00.000Z' },
|
{ trialId: 101, createdAt: '2025-04-01T10:00:00.000Z' },
|
||||||
|
|
@ -505,6 +506,27 @@ describe('recordTrials', () => {
|
||||||
{ trialId: 103, createdAt: '2025-04-01T10:00:00.002Z' },
|
{ trialId: 103, createdAt: '2025-04-01T10:00:00.002Z' },
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('preserves PostgreSQL microsecond UTC timestamps across receipts and asOf queries', async () => {
|
||||||
|
const microTs = '2025-04-01T10:00:00.123456Z';
|
||||||
|
const { executor, calls } = createFakeExecutor(text => {
|
||||||
|
if (text.startsWith('INSERT INTO trial_ledger')) {
|
||||||
|
return { rows: [{ trial_id: '7', created_at: microTs }] };
|
||||||
|
}
|
||||||
|
return { rows: [] };
|
||||||
|
});
|
||||||
|
|
||||||
|
const receipts = await recordTrials(executor, [makeValidInput()]);
|
||||||
|
expect(receipts).toEqual([{ trialId: 7, createdAt: microTs }]);
|
||||||
|
|
||||||
|
await loadClusterTrials(executor, {
|
||||||
|
universeMarket: 'US_EQ',
|
||||||
|
horizonBucket: 'swing_1_10d',
|
||||||
|
asOf: receipts[0]!.createdAt,
|
||||||
|
});
|
||||||
|
expect(calls).toHaveLength(2);
|
||||||
|
expect(calls[1]!.params).toEqual(['US_EQ', 'swing_1_10d', microTs]);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('loadClusterTrials and clusterDeflatedSharpe', () => {
|
describe('loadClusterTrials and clusterDeflatedSharpe', () => {
|
||||||
|
|
|
||||||
|
|
@ -175,9 +175,9 @@ describe.skipIf(!url)('trial_ledger PostgreSQL writer and cluster DSR reader', (
|
||||||
expect(swingRows).toHaveLength(2);
|
expect(swingRows).toHaveLength(2);
|
||||||
expect(positionRows).toHaveLength(1);
|
expect(positionRows).toHaveLength(1);
|
||||||
|
|
||||||
// Verify daily_series round-trips within IEEE-754 REAL precision.
|
// Verify daily_series round-trips within IEEE-754 REAL precision (float4 text parses to float64 with identical float32 bits).
|
||||||
for (let i = 0; i < rows[0]!.dailySeries.length; i += 1) {
|
for (let i = 0; i < rows[0]!.dailySeries.length; i += 1) {
|
||||||
expect(swingRows[0]!.dailySeries[i]).toBe(Math.fround(rows[0]!.dailySeries[i]!));
|
expect(Math.fround(swingRows[0]!.dailySeries[i]!)).toBe(Math.fround(rows[0]!.dailySeries[i]!));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Batch with one invalid row throws TrialLedgerError and inserts 0 additional rows.
|
// Batch with one invalid row throws TrialLedgerError and inserts 0 additional rows.
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue