T-122: M1.3g — @stock-bot/research: Vault-fenced universe planner + adapter method (survivorship-free active listings on a date from curated bars_1d Parquet, US_EQ / CA_EQ / US_CA_EQ) with tests and local benchmark (no new dependencies)
This commit is contained in:
parent
91c7411ed9
commit
0df1ef92ac
2 changed files with 114 additions and 24 deletions
|
|
@ -82,6 +82,10 @@ const INT32_MIN = -2_147_483_648;
|
|||
const INT32_MAX = 2_147_483_647;
|
||||
const MAX_ECMASCRIPT_EPOCH_MS = 8_640_000_000_000_000;
|
||||
const MS_PER_DAY = 86_400_000;
|
||||
// 0000-01-01T00:00:00.000Z: earliest epoch ms that formats as unsigned YYYY-MM-DD.
|
||||
const MIN_ISO_DATE_EPOCH_MS = -62_167_219_200_000;
|
||||
// 9999-12-31T00:00:00.000Z: latest UTC midnight representable as YYYY-MM-DD.
|
||||
const MAX_ISO_DATE_MIDNIGHT_MS = 253_402_214_400_000;
|
||||
const DATE_ONLY_RE = /^\d{4}-\d{2}-\d{2}$/;
|
||||
const ISO_FRACTIONAL_RE = /\.(\d+)(?:Z|[+-]\d{2}:\d{2})$/;
|
||||
const STRICT_INT_RE = /^-?\d+$/;
|
||||
|
|
@ -603,17 +607,42 @@ function validateUniverseRef(raw: unknown): readonly string[] {
|
|||
);
|
||||
}
|
||||
|
||||
function validateUniverseLookbackDays(raw: unknown): number {
|
||||
if (raw === undefined) {
|
||||
return DEFAULT_UNIVERSE_LOOKBACK_DAYS;
|
||||
}
|
||||
if (typeof raw !== 'number' || !Number.isSafeInteger(raw) || raw < 1) {
|
||||
function validateUniverseLookbackDays(raw: unknown, vaultCutoffTs?: number): number {
|
||||
const days = raw === undefined ? DEFAULT_UNIVERSE_LOOKBACK_DAYS : raw;
|
||||
if (typeof days !== 'number' || !Number.isSafeInteger(days) || days < 1) {
|
||||
throw new MarketDataStoreValidationError(
|
||||
`Invalid universeLookbackDays "${String(raw)}". Expected a positive safe integer.`,
|
||||
'universeLookbackDays'
|
||||
);
|
||||
}
|
||||
return raw;
|
||||
|
||||
const lookbackMs = days * MS_PER_DAY;
|
||||
if (
|
||||
!Number.isSafeInteger(lookbackMs) ||
|
||||
MAX_ISO_DATE_MIDNIGHT_MS - lookbackMs < MIN_ISO_DATE_EPOCH_MS
|
||||
) {
|
||||
throw new MarketDataStoreValidationError(
|
||||
`Invalid universeLookbackDays "${days}". Lookback window exceeds supported YYYY-MM-DD UTC date range.`,
|
||||
'universeLookbackDays'
|
||||
);
|
||||
}
|
||||
|
||||
if (vaultCutoffTs !== undefined) {
|
||||
const latestPermittedDateMs =
|
||||
Math.floor(Math.min(vaultCutoffTs - 1, MAX_ISO_DATE_MIDNIGHT_MS) / MS_PER_DAY) * MS_PER_DAY;
|
||||
const latestLookbackStartMs = latestPermittedDateMs - lookbackMs;
|
||||
if (
|
||||
!Number.isSafeInteger(latestLookbackStartMs) ||
|
||||
latestLookbackStartMs < MIN_ISO_DATE_EPOCH_MS
|
||||
) {
|
||||
throw new MarketDataStoreValidationError(
|
||||
`Invalid universeLookbackDays "${days}". Lookback window exceeds supported YYYY-MM-DD UTC date range for vaultCutoffTs.`,
|
||||
'universeLookbackDays'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return days;
|
||||
}
|
||||
|
||||
function validateContext(ctx: DuckDbPlanContext): string {
|
||||
|
|
@ -1508,17 +1537,10 @@ export function planUniverseQuery(q: UniverseQuery, ctx: DuckDbPlanContext): Pla
|
|||
throw err;
|
||||
}
|
||||
|
||||
const lookbackMs = lookbackDays * MS_PER_DAY;
|
||||
if (!Number.isSafeInteger(lookbackMs)) {
|
||||
const lookbackStartMs = fencedDate.fromMs - lookbackDays * MS_PER_DAY;
|
||||
if (!Number.isSafeInteger(lookbackStartMs) || lookbackStartMs < MIN_ISO_DATE_EPOCH_MS) {
|
||||
throw new MarketDataStoreValidationError(
|
||||
`Invalid universeLookbackDays "${lookbackDays}". Lookback window overflows safe millisecond range.`,
|
||||
'universeLookbackDays'
|
||||
);
|
||||
}
|
||||
const lookbackStartMs = fencedDate.fromMs - lookbackMs;
|
||||
if (!Number.isSafeInteger(lookbackStartMs) || lookbackStartMs < -MAX_ECMASCRIPT_EPOCH_MS) {
|
||||
throw new MarketDataStoreValidationError(
|
||||
`Invalid universeLookbackDays "${lookbackDays}". Lookback start precedes supported UTC date range.`,
|
||||
`Invalid universeLookbackDays "${lookbackDays}". Lookback start precedes supported YYYY-MM-DD UTC date range.`,
|
||||
'universeLookbackDays'
|
||||
);
|
||||
}
|
||||
|
|
@ -1875,7 +1897,10 @@ export function createDuckDbMarketDataStore(options: DuckDbMarketDataStoreOption
|
|||
);
|
||||
}
|
||||
|
||||
const universeLookbackDays = validateUniverseLookbackDays(options.universeLookbackDays);
|
||||
const universeLookbackDays = validateUniverseLookbackDays(
|
||||
options.universeLookbackDays,
|
||||
vaultCutoffTs
|
||||
);
|
||||
|
||||
const budget = resolveDuckDbConnectionBudget(options.env);
|
||||
const planCtx: DuckDbPlanContext = {
|
||||
|
|
|
|||
|
|
@ -2712,21 +2712,86 @@ describe('planUniverseQuery & createDuckDbMarketDataStore().universe()', () => {
|
|||
expect((caughtStore as MarketDataStoreValidationError).field).toBe('universeLookbackDays');
|
||||
}
|
||||
|
||||
// Safe-integer overflow and epoch-underflow guards for extreme lookbackDays.
|
||||
for (const hugeLookback of [Number.MAX_SAFE_INTEGER, 100_000_000_000]) {
|
||||
let caught: unknown;
|
||||
// Safe-integer overflow, epoch-underflow, and cutoff-impossible lookbacks must fail
|
||||
// upfront at both planUniverseQuery and createDuckDbMarketDataStore with zero executor calls.
|
||||
for (const impossibleLookback of [
|
||||
Number.MAX_SAFE_INTEGER,
|
||||
100_000_000_000,
|
||||
604_800_000, // 7 days accidentally passed in milliseconds
|
||||
100_100_000, // fits in safe ms (8.64864e15) but underflows supported date range
|
||||
740_129, // 1 day beyond 2026-05-27 -> 0000-01-01 at the 2026 fixture cutoff
|
||||
]) {
|
||||
let caughtPlan: unknown;
|
||||
try {
|
||||
planUniverseQuery(
|
||||
{ ref: 'US_EQ', date: '2025-01-15' },
|
||||
{ ...DEFAULT_CTX, universeLookbackDays: hugeLookback }
|
||||
{ ...DEFAULT_CTX, universeLookbackDays: impossibleLookback }
|
||||
);
|
||||
} catch (err) {
|
||||
caught = err;
|
||||
caughtPlan = err;
|
||||
}
|
||||
expect(caught).toBeInstanceOf(MarketDataStoreValidationError);
|
||||
expect((caught as MarketDataStoreValidationError).field).toBe('universeLookbackDays');
|
||||
expect(caughtPlan).toBeInstanceOf(MarketDataStoreValidationError);
|
||||
expect((caughtPlan as MarketDataStoreValidationError).field).toBe('universeLookbackDays');
|
||||
|
||||
let caughtStore: unknown;
|
||||
try {
|
||||
createDuckDbMarketDataStore({
|
||||
executor: fake.executor,
|
||||
curatedRoot: CURATED_ROOT,
|
||||
vaultCutoffTs: CUTOFF_MS,
|
||||
universeLookbackDays: impossibleLookback,
|
||||
});
|
||||
} catch (err) {
|
||||
caughtStore = err;
|
||||
}
|
||||
expect(caughtStore).toBeInstanceOf(MarketDataStoreValidationError);
|
||||
expect((caughtStore as MarketDataStoreValidationError).field).toBe('universeLookbackDays');
|
||||
}
|
||||
|
||||
// Crossing before year 0000 (while still inside JS Date epoch range) must reject
|
||||
// cleanly in both planUniverseQuery and store.universe with zero executor calls
|
||||
// rather than truncating an extended signed ISO year (`-000001-...`).
|
||||
let caughtYearZeroPlan: unknown;
|
||||
try {
|
||||
planUniverseQuery({ ref: 'US_EQ', date: '0000-01-05' }, DEFAULT_CTX);
|
||||
} catch (err) {
|
||||
caughtYearZeroPlan = err;
|
||||
}
|
||||
expect(caughtYearZeroPlan).toBeInstanceOf(MarketDataStoreValidationError);
|
||||
expect((caughtYearZeroPlan as MarketDataStoreValidationError).field).toBe(
|
||||
'universeLookbackDays'
|
||||
);
|
||||
|
||||
await expect(store.universe({ ref: 'US_EQ', date: '0000-01-05' })).rejects.toMatchObject({
|
||||
name: 'MarketDataStoreValidationError',
|
||||
field: 'universeLookbackDays',
|
||||
});
|
||||
|
||||
// Exact 0000-01-01 boundary succeeds with a valid YYYY-MM-DD lower bound.
|
||||
const exactYearZero = planUniverseQuery({ ref: 'US_EQ', date: '0000-01-08' }, DEFAULT_CTX);
|
||||
expect(exactYearZero.params[1]).toBe('0000-01-01');
|
||||
|
||||
// A lookback valid on the latest permitted date (2026-05-27 -> 0000-01-01) is accepted
|
||||
// at store creation, but still rejected on earlier query dates with zero executor calls.
|
||||
const boundaryStore = createDuckDbMarketDataStore({
|
||||
executor: fake.executor,
|
||||
curatedRoot: CURATED_ROOT,
|
||||
vaultCutoffTs: CUTOFF_MS,
|
||||
universeLookbackDays: 740_128,
|
||||
});
|
||||
const maxBoundaryPlan = planUniverseQuery(
|
||||
{ ref: 'US_EQ', date: MAX_ALLOWED_DATE },
|
||||
{ ...DEFAULT_CTX, universeLookbackDays: 740_128 }
|
||||
);
|
||||
expect(maxBoundaryPlan.params[1]).toBe('0000-01-01');
|
||||
|
||||
await expect(
|
||||
boundaryStore.universe({ ref: 'US_EQ', date: '2026-05-26' })
|
||||
).rejects.toMatchObject({
|
||||
name: 'MarketDataStoreValidationError',
|
||||
field: 'universeLookbackDays',
|
||||
});
|
||||
|
||||
expect(fake.calls).toHaveLength(0);
|
||||
});
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue