T-148: M2.3h — Route MarketDataStore.bars (timeframe: '1m') to the QuestDB bars_1m reader: pure createRoutedMarketDataStore composing the DuckDB daily store + injected intraday reader in libs/research/src/market-data-store-routed.ts (no new deps, no live QuestDB)

This commit is contained in:
Studio 2026-12-01 23:02:17 -05:00
parent c6ee4d0336
commit 8f9e9ac196
5 changed files with 1323 additions and 0 deletions

View file

@ -5,6 +5,7 @@ export * from './feature-library';
export * from './market-data-store';
export * from './market-data-store-duckdb';
export * from './market-data-store-questdb';
export * from './market-data-store-routed';
export * from './signal-panel';
export * from './signal-study';
export * from './trial-ledger';

View file

@ -0,0 +1,190 @@
/**
* Pure, dependency-free `MarketDataStore` router that dispatches intraday
* `bars` queries (`timeframe: '1m'`) to an injected intraday reader (such as
* {@link createQuestDbIntradayBarsReader}) and routes every other `bars`
* timeframe and all other `MarketDataStore` methods to the injected daily
* store (such as {@link createDuckDbMarketDataStore}).
*
* Browser-safe and dependency-free: zero `node:*` imports, zero bare `process`
* access, and no database or filesystem I/O.
*
* Vault & point-in-time semantics:
* - This router adds no Vault logic of its own; each delegate continues to
* enforce its own Vault holdout fence and point-in-time predicates.
* - Callers MUST construct both `daily` and `intraday` with the exact same
* `vaultCutoffTs` and `vaultOperatorAuthorized` values (and pass the same
* `vaultCutoffTs` to MCP tools such as `createQueryDataTool`) so the holdout
* boundary is identical across timeframes.
* - The QuestDB intraday reader (`createQuestDbIntradayBarsReader`) supports
* split-unadjusted `adjustment: 'raw'` `1m` bars; non-`'raw'` `1m` requests
* and unsupported timeframes (`'5m'`, `'1h'`) are handled by the respective
* delegate without modification.
*/
import { type MarketDataStore, MarketDataStoreValidationError } from './market-data-store';
/** Configuration for {@link createRoutedMarketDataStore}. */
export interface RoutedMarketDataStoreOptions {
/** Backing store for daily (`'1d'`) and non-`'1m'` bars plus all other `MarketDataStore` methods. */
readonly daily: MarketDataStore;
/** Backing intraday reader for `bars` queries with `timeframe: '1m'`. */
readonly intraday: Pick<MarketDataStore, 'bars'>;
}
const REQUIRED_DAILY_METHODS = [
'bars',
'crossSection',
'fundamentalsAsOf',
'events',
'shortsAsOf',
'macroAsOf',
'fx',
'universe',
] as const satisfies readonly (keyof MarketDataStore)[];
function isPlainObject(value: unknown): value is Record<string, unknown> {
if (typeof value !== 'object' || value === null) {
return false;
}
const proto: unknown = Object.getPrototypeOf(value);
return proto === Object.prototype || proto === null;
}
function isDelegateObject(value: unknown): value is Record<string, unknown> {
return typeof value === 'object' && value !== null && !Array.isArray(value);
}
function isIntraday1mBarsQuery(q: unknown): boolean {
return (
typeof q === 'object' &&
q !== null &&
!Array.isArray(q) &&
(q as { readonly timeframe?: unknown }).timeframe === '1m'
);
}
/**
* Composes a daily `MarketDataStore` and an intraday `bars` reader into a
* single read-only `MarketDataStore`.
*
* Routing rules:
* - `bars(q)` with `q.timeframe === '1m'` delegates directly to
* `options.intraday.bars(q)` and returns its `AsyncIterable<BarBatch>`
* unchanged (preserving receiver, query identity, lazy iteration, and error
* timing).
* - `bars(q)` with any other timeframe (`'1d'`, `'5m'`, `'1h'`, or malformed
* runtime inputs) delegates directly to `options.daily.bars(q)`.
* - All other `MarketDataStore` methods (`crossSection`, `fundamentalsAsOf`,
* `events`, `shortsAsOf`, `macroAsOf`, `fx`, `universe`) delegate directly
* to `options.daily` and return its original promise.
*
* Vault fence note:
* Both `daily` and `intraday` MUST be constructed with the exact same
* `vaultCutoffTs` and `vaultOperatorAuthorized` settings. The QuestDB intraday
* reader serves raw (`adjustment: 'raw'`) `1m` bars.
*
* @example
* ```ts
* const vaultCutoffTs = Date.UTC(2025, 6, 1, 0, 0, 0, 0);
* const vaultOperatorAuthorized = false;
*
* const daily = createDuckDbMarketDataStore({
* executor: duckDbExecutor,
* curatedRoot: '/mnt/data/stockbot/curated',
* vaultCutoffTs,
* vaultOperatorAuthorized,
* });
* const intraday = createQuestDbIntradayBarsReader({
* executor: questDbExecutor,
* vaultCutoffTs,
* vaultOperatorAuthorized,
* });
* const store = createRoutedMarketDataStore({ daily, intraday });
* const queryDataTool = createQueryDataTool({
* store,
* vaultContext: { vaultCutoffTs, vaultOperatorAuthorized },
* });
* ```
*/
export function createRoutedMarketDataStore(
options: RoutedMarketDataStoreOptions
): MarketDataStore {
if (!isPlainObject(options)) {
throw new MarketDataStoreValidationError(
'Expected a plain RoutedMarketDataStoreOptions object.',
'options'
);
}
const rawDaily: unknown = options.daily;
if (!isDelegateObject(rawDaily)) {
throw new MarketDataStoreValidationError(
'Expected `daily` to be a MarketDataStore object.',
'daily'
);
}
for (const method of REQUIRED_DAILY_METHODS) {
if (typeof rawDaily[method] !== 'function') {
throw new MarketDataStoreValidationError(
`Expected \`daily.${method}\` to be a function.`,
`daily.${method}`
);
}
}
const rawIntraday: unknown = options.intraday;
if (!isDelegateObject(rawIntraday)) {
throw new MarketDataStoreValidationError(
'Expected `intraday` to be an object with a `bars` method.',
'intraday'
);
}
if (typeof rawIntraday.bars !== 'function') {
throw new MarketDataStoreValidationError(
'Expected `intraday.bars` to be a function.',
'intraday.bars'
);
}
const daily = options.daily;
const intraday = options.intraday;
return {
bars(q) {
if (isIntraday1mBarsQuery(q)) {
return intraday.bars(q);
}
return daily.bars(q);
},
crossSection(q) {
return daily.crossSection(q);
},
fundamentalsAsOf(q) {
return daily.fundamentalsAsOf(q);
},
events(q) {
return daily.events(q);
},
shortsAsOf(q) {
return daily.shortsAsOf(q);
},
macroAsOf(q) {
return daily.macroAsOf(q);
},
fx(q) {
return daily.fx(q);
},
universe(q) {
return daily.universe(q);
},
};
}