T-210: M2.3s — Implement MarketDataStore.macroAsOf, crossSection, QuestDB fx('1m'), and copilot 1m routing

This commit is contained in:
Studio 2026-12-04 15:23:16 -05:00
parent c60365237f
commit 5e255d7e30
8 changed files with 587 additions and 37 deletions

View file

@ -15,12 +15,17 @@
import {
applyM2IntradayDDL,
assertNoConflictingQuestDbSsl,
openQuestDbPgExecutor,
type QuestDbPgExecutorConfig,
} from '@stock-bot/questdb';
function readEnv(primary: string, fallback: string): string | undefined {
const value = process.env[primary] ?? process.env[fallback];
function readEnv(
env: Readonly<Record<string, string | undefined>>,
primary: string,
fallback: string
): string | undefined {
const value = env[primary] ?? env[fallback];
if (value === undefined) {
return undefined;
}
@ -28,34 +33,47 @@ function readEnv(primary: string, fallback: string): string | undefined {
return trimmed.length > 0 ? trimmed : undefined;
}
function resolveConfig(): QuestDbPgExecutorConfig {
const url = readEnv('QUESTDB_URL', 'RESEARCH_MCP_QUESTDB_URL');
const sslRaw = readEnv('QUESTDB_SSL', 'RESEARCH_MCP_QUESTDB_SSL');
const ssl =
sslRaw === undefined
? undefined
: sslRaw.toLowerCase() === 'true' || sslRaw === '1'
? true
: sslRaw.toLowerCase() === 'false' || sslRaw === '0'
? false
: undefined;
function parseSslFlag(raw: string): boolean {
const normalized = raw.trim().toLowerCase();
if (normalized === 'true' || normalized === '1') {
return true;
}
if (normalized === 'false' || normalized === '0') {
return false;
}
throw new RangeError(
'Invalid QuestDB SSL configuration: expected "true", "false", "1", or "0".'
);
}
export function resolveQuestDbInitM2Config(
env: Readonly<Record<string, string | undefined>> = process.env
): QuestDbPgExecutorConfig {
const url = readEnv(env, 'QUESTDB_URL', 'RESEARCH_MCP_QUESTDB_URL');
const sslRaw = readEnv(env, 'QUESTDB_SSL', 'RESEARCH_MCP_QUESTDB_SSL');
const ssl = sslRaw !== undefined ? parseSslFlag(sslRaw) : undefined;
if (url !== undefined) {
if (ssl !== undefined) {
assertNoConflictingQuestDbSsl(url, ssl);
}
return {
url,
...(ssl !== undefined ? { ssl } : {}),
};
}
const host = readEnv('QUESTDB_HOST', 'RESEARCH_MCP_QUESTDB_HOST') ?? 'localhost';
const portRaw = readEnv('QUESTDB_PG_PORT', 'RESEARCH_MCP_QUESTDB_PG_PORT');
const host = readEnv(env, 'QUESTDB_HOST', 'RESEARCH_MCP_QUESTDB_HOST') ?? 'localhost';
const portRaw = readEnv(env, 'QUESTDB_PG_PORT', 'RESEARCH_MCP_QUESTDB_PG_PORT');
const port = portRaw !== undefined ? Number(portRaw) : 8812;
if (!Number.isInteger(port) || port < 1 || port > 65535) {
throw new Error(`Invalid QuestDB PGWire port "${String(portRaw)}". Expected 1..65535.`);
throw new RangeError(
`Invalid QuestDB PGWire port "${String(portRaw)}". Expected 1..65535.`
);
}
const database = readEnv('QUESTDB_DATABASE', 'RESEARCH_MCP_QUESTDB_DATABASE') ?? 'qdb';
const user = readEnv('QUESTDB_USER', 'RESEARCH_MCP_QUESTDB_USER');
const password = readEnv('QUESTDB_PASSWORD', 'RESEARCH_MCP_QUESTDB_PASSWORD');
const database = readEnv(env, 'QUESTDB_DATABASE', 'RESEARCH_MCP_QUESTDB_DATABASE') ?? 'qdb';
const user = readEnv(env, 'QUESTDB_USER', 'RESEARCH_MCP_QUESTDB_USER');
const password = readEnv(env, 'QUESTDB_PASSWORD', 'RESEARCH_MCP_QUESTDB_PASSWORD');
return {
host,
@ -67,21 +85,29 @@ function resolveConfig(): QuestDbPgExecutorConfig {
};
}
async function main(): Promise<void> {
const config = resolveConfig();
const handle = await openQuestDbPgExecutor(config);
export async function runQuestDbInitM2(
env: Readonly<Record<string, string | undefined>> = process.env,
openExecutor: typeof openQuestDbPgExecutor = openQuestDbPgExecutor
): Promise<{ readonly appliedTables: readonly string[] }> {
const config = resolveQuestDbInitM2Config(env);
const handle = await openExecutor(config);
try {
const result = await applyM2IntradayDDL(handle.executor);
process.stdout.write(
`Initialized QuestDB M2 intraday tables: ${result.appliedTables.join(', ')}\n`
);
return await applyM2IntradayDDL(handle.executor);
} finally {
await handle.close();
}
}
main().catch((err: unknown) => {
const message = err instanceof Error ? err.message : String(err);
process.stderr.write(`questdb-init-m2 failed: ${message}\n`);
process.exit(1);
});
if (import.meta.main) {
runQuestDbInitM2()
.then(result => {
process.stdout.write(
`Initialized QuestDB M2 intraday tables: ${result.appliedTables.join(', ')}\n`
);
})
.catch((err: unknown) => {
const message = err instanceof Error ? err.message : String(err);
process.stderr.write(`questdb-init-m2 failed: ${message}\n`);
process.exit(1);
});
}