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

@ -124,6 +124,37 @@ Fixed-seed (`20261129`) local benchmark planning `120,000` mixed queries across
- **Mixed planners (`120,000` queries):** `elapsedMs = 225.79 ms`, `queriesPerSec = 531,470 queries/s` (`758,061` bound params, pinned checksum `8606c98c`)
- **`universe` plan + adapter mapping (`200` calls × `15,000` listings):** `elapsedMs = 23.44 ms`, `callsPerSec = 8,534 calls/s`, `rowsPerSec = 128,011,137 rows/s` (`3,000,000` mapped IDs, pinned checksum `ef5f1136`)
## Routed `MarketDataStore` (DuckDB Daily + QuestDB `1m` Intraday)
Exported from `@stock-bot/research` and `@stock-bot/research/market-data-store-routed` (`libs/research/src/market-data-store-routed.ts`). The QuestDB intraday reader is also exported from `@stock-bot/research/market-data-store-questdb`. Both modules are pure, dependency-free, and browser-safe.
- **`createRoutedMarketDataStore({ daily, intraday })`** — Composes a full daily `MarketDataStore` (e.g. `createDuckDbMarketDataStore`) with an intraday `bars` reader (`Pick<MarketDataStore, 'bars'>`, e.g. `createQuestDbIntradayBarsReader`).
- `bars(q)` with `timeframe: '1m'` delegates directly to `intraday.bars(q)` (preserving lazy iteration, receiver, query identity, and error propagation). Note that `createQuestDbIntradayBarsReader` supports split-unadjusted raw (`adjustment: 'raw'`) `1m` bars.
- `bars(q)` with any other timeframe (`'1d'`, `'5m'`, `'1h'`, or malformed inputs) and all other `MarketDataStore` methods (`crossSection`, `fundamentalsAsOf`, `events`, `shortsAsOf`, `macroAsOf`, `fx`, `universe`) delegate directly to `daily`.
- **Shared Vault fence requirement:** The router adds no Vault logic of its own; each delegate enforces its own fence. Always construct `daily`, `intraday`, and any MCP tool (`createQueryDataTool` / `createResearchCopilotMcpServer`) with the exact same `vaultCutoffTs` and `vaultOperatorAuthorized`:
```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 },
});
```
## Signal Panel Builder
Exported from both `@stock-bot/research` and `@stock-bot/research/signal-panel` (`libs/research/src/signal-panel.ts`). The module is pure, synchronous (except `collectBars`), dependency-free, and browser-safe (zero `node:*` imports, zero bare `process` access, no database drivers).

View file

@ -14,6 +14,8 @@
"./feature-library": "./src/feature-library.ts",
"./market-data-store": "./src/market-data-store.ts",
"./market-data-store-duckdb": "./src/market-data-store-duckdb.ts",
"./market-data-store-questdb": "./src/market-data-store-questdb.ts",
"./market-data-store-routed": "./src/market-data-store-routed.ts",
"./mcp": "./src/mcp/index.ts",
"./results": "./src/results.ts",
"./signal-panel": "./src/signal-panel.ts",

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);
},
};
}

File diff suppressed because it is too large Load diff