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:31:29 -05:00
parent 5e255d7e30
commit 373936c312
6 changed files with 168 additions and 129 deletions

View file

@ -40,6 +40,7 @@ export {
export {
assertNoConflictingQuestDbSsl,
hasConflictingQuestDbUrlSsl,
hasQuestDbUrlTlsOptions,
openQuestDbPgExecutor,
type OpenQuestDbPgExecutorOptions,
type QuestDbPgExecutorConfig,

View file

@ -47,58 +47,34 @@ export interface OpenQuestDbPgExecutorOptions {
const SAFE_ERROR_CODE_RE = /^[A-Z0-9_]{1,32}$/;
function isQuestDbUrlTlsParamKey(rawKey: string): boolean {
const key = rawKey.trim().toLowerCase();
return key === 'uselibpqcompat' || key === 'ssl' || key.startsWith('ssl');
}
/**
* Returns `true` when an explicit boolean `ssl` setting conflicts with SSL
* query parameters (`sslmode`, `ssl`, `sslcert`, `sslkey`, `sslrootcert`)
* embedded in `rawUrl`.
* Returns `true` when `rawUrl` contains any TLS/SSL query parameter
* (`ssl`, `sslmode`, `sslcert`, `sslkey`, `sslrootcert`, `uselibpqcompat`, or
* any other `ssl*` parameter).
*
* TLS settings must have a single source of truth: either configure TLS in the
* URL query string (omitting the explicit `ssl` flag) or pass the explicit
* `ssl` flag with a URL that contains no TLS query parameters.
*/
export function hasConflictingQuestDbUrlSsl(rawUrl: string, explicitSsl: boolean): boolean {
export function hasQuestDbUrlTlsOptions(rawUrl: string): boolean {
let parsed: URL;
try {
parsed = new URL(rawUrl);
} catch {
return false;
try {
parsed = new URL(rawUrl, 'postgres://base');
} catch {
return false;
}
}
for (const [rawKey, rawValue] of parsed.searchParams.entries()) {
const key = rawKey.toLowerCase();
const value = rawValue.trim().toLowerCase();
if (key === 'sslmode') {
if (value === 'disable') {
if (explicitSsl) {
return true;
}
} else if (
value === 'require' ||
value === 'verify-ca' ||
value === 'verify-full' ||
value === 'no-verify'
) {
if (!explicitSsl) {
return true;
}
} else {
// `allow`, `prefer`, or any unrecognized sslmode conflicts with an explicit boolean override.
return true;
}
} else if (key === 'ssl') {
if (value === 'false' || value === '0') {
if (explicitSsl) {
return true;
}
} else if (value === 'true' || value === '1') {
if (!explicitSsl) {
return true;
}
} else {
return true;
}
} else if (
(key === 'sslcert' || key === 'sslkey' || key === 'sslrootcert') &&
value.length > 0 &&
!explicitSsl
) {
for (const [rawKey] of parsed.searchParams.entries()) {
if (isQuestDbUrlTlsParamKey(rawKey)) {
return true;
}
}
@ -107,13 +83,21 @@ export function hasConflictingQuestDbUrlSsl(rawUrl: string, explicitSsl: boolean
}
/**
* Throws a credential-free `RangeError` if `explicitSsl` conflicts with SSL
* query parameters in `rawUrl`.
* Returns `true` when an explicit boolean `ssl` setting is combined with any
* TLS/SSL query parameter embedded in `rawUrl`.
*/
export function assertNoConflictingQuestDbSsl(rawUrl: string, explicitSsl: boolean): void {
if (hasConflictingQuestDbUrlSsl(rawUrl, explicitSsl)) {
export function hasConflictingQuestDbUrlSsl(rawUrl: string, _explicitSsl?: boolean): boolean {
return hasQuestDbUrlTlsOptions(rawUrl);
}
/**
* Throws a credential-free `RangeError` if an explicit `ssl` option is combined
* with any TLS/SSL query parameter in `rawUrl`.
*/
export function assertNoConflictingQuestDbSsl(rawUrl: string, _explicitSsl?: boolean): void {
if (hasQuestDbUrlTlsOptions(rawUrl)) {
throw new RangeError(
'Conflicting QuestDB SSL configuration: explicit "ssl" option conflicts with SSL query parameters in the QuestDB URL.'
'Conflicting QuestDB TLS/SSL configuration: cannot combine an explicit "ssl" option with TLS/SSL query parameters in the QuestDB URL. Configure TLS in either the URL or the explicit "ssl" option, not both.'
);
}
}

View file

@ -1,5 +1,6 @@
import { beforeEach, describe, expect, it, mock } from 'bun:test';
import { EventEmitter } from 'node:events';
import { Client, type PoolConfig } from 'pg';
import { QuestDBHealthMonitor } from '../src/health';
import { QuestDBInfluxWriter } from '../src/influx-writer';
import {
@ -650,25 +651,38 @@ describe('QuestDB', () => {
expect(fakePool.ended).toBe(true);
});
it('rejects conflicting URL SSL query parameters and explicit ssl before creating a pool', async () => {
it('rejects any URL TLS/SSL query parameter when explicit ssl is set and verifies pinned pg effective TLS config without connecting', async () => {
const secret = 'qdb_ssl_s3cr3t!';
const base = `postgresql://admin:${encodeURIComponent(secret)}@10.0.0.51:8812/qdb`;
const conflictingCases: ReadonlyArray<readonly [string, boolean]> = [
[`${base}?sslmode=disable`, true],
[`${base}?SSLMODE=DISABLE`, true],
[`${base}?sslmode=require`, false],
[`${base}?sslmode=verify-ca`, false],
[`${base}?sslmode=verify-full`, false],
[`${base}?sslmode=no-verify`, true],
[`${base}?sslmode=no-verify`, false],
[`${base}?sslmode=disable`, true],
[`${base}?sslmode=disable`, false],
[`${base}?SSLMODE=DISABLE`, true],
[`${base}?sslmode=require`, true],
[`${base}?sslmode=require`, false],
[`${base}?sslmode=verify-ca`, true],
[`${base}?sslmode=verify-ca`, false],
[`${base}?sslmode=verify-full`, true],
[`${base}?sslmode=verify-full`, false],
[`${base}?sslmode=prefer`, true],
[`${base}?sslmode=prefer`, false],
[`${base}?sslmode=allow`, true],
[`${base}?ssl=false`, true],
[`${base}?ssl=false`, false],
[`${base}?ssl=0`, true],
[`${base}?ssl=0`, false],
[`${base}?ssl=true`, true],
[`${base}?ssl=true`, false],
[`${base}?ssl=1`, true],
[`${base}?ssl=1`, false],
[`${base}?ssl=`, true],
[`${base}?sslrootcert=/etc/ssl/ca.pem`, true],
[`${base}?sslrootcert=/etc/ssl/ca.pem`, false],
[`${base}?uselibpqcompat=true`, true],
[`${base}?uselibpqcompat=true`, false],
];
let createPoolCalls = 0;
@ -694,24 +708,57 @@ describe('QuestDB', () => {
}
expect(createPoolCalls).toBe(0);
// Compatible combinations proceed to pool creation.
const compatibleCases: ReadonlyArray<readonly [string, boolean]> = [
[base, true],
[`${base}?sslmode=require`, true],
[`${base}?sslmode=disable`, false],
[`${base}?ssl=true`, true],
[`${base}?ssl=false`, false],
// Single-source-of-truth configs proceed to pool creation and produce the
// expected effective TLS settings in the pinned `pg` driver without opening
// a network connection.
const effectiveCases: ReadonlyArray<{
readonly config: Parameters<typeof openQuestDbPgExecutor>[0];
readonly expectedSsl: unknown;
}> = [
{ config: { url: base, ssl: true }, expectedSsl: true },
{
config: { url: `${base}?application_name=copilot`, ssl: false },
expectedSsl: false,
},
{ config: { url: `${base}?sslmode=require` }, expectedSsl: {} },
{ config: { url: `${base}?sslmode=disable` }, expectedSsl: false },
{
config: { url: `${base}?sslmode=no-verify` },
expectedSsl: { rejectUnauthorized: false },
},
{
config: { host: '10.0.0.51', port: 8812, database: 'qdb', ssl: true },
expectedSsl: true,
},
{
config: { host: '10.0.0.51', port: 8812, database: 'qdb', ssl: false },
expectedSsl: false,
},
];
for (const [url, ssl] of compatibleCases) {
for (const { config, expectedSsl } of effectiveCases) {
let capturedPoolConfig: PoolConfig | undefined;
const pool = Object.assign(new EventEmitter(), {
query: async () => ({ rows: [{ '?column?': 1 }] }),
end: async () => {},
}) as unknown as QuestDbPgPoolLike;
const handle = await openQuestDbPgExecutor(
{ url, ssl },
{ createPool: () => pool }
);
const handle = await openQuestDbPgExecutor(config, {
createPool: poolConfig => {
capturedPoolConfig = poolConfig;
return pool;
},
});
await handle.close();
expect(capturedPoolConfig).toBeDefined();
// Instantiate pg.Client without calling connect() to inspect the pinned
// driver's effective ConnectionParameters.ssl resolution.
const client = new Client(capturedPoolConfig);
const effectiveSsl: unknown = (
client as unknown as { connectionParameters: { ssl: unknown } }
).connectionParameters.ssl;
expect(effectiveSsl).toEqual(expectedSsl);
expect(client.ssl as unknown).toEqual(expectedSsl);
}
});
});

View file

@ -282,7 +282,12 @@ function parseQuestDbPort(raw: string): number {
return port;
}
function hasConflictingUrlSslSetting(rawUrl: string, explicitSsl: boolean): boolean {
function isQuestDbUrlTlsParamKey(rawKey: string): boolean {
const key = rawKey.trim().toLowerCase();
return key === 'uselibpqcompat' || key === 'ssl' || key.startsWith('ssl');
}
function hasQuestDbUrlTlsOptions(rawUrl: string): boolean {
let parsed: URL;
try {
parsed = new URL(rawUrl);
@ -290,45 +295,8 @@ function hasConflictingUrlSslSetting(rawUrl: string, explicitSsl: boolean): bool
return false;
}
for (const [rawKey, rawValue] of parsed.searchParams.entries()) {
const key = rawKey.toLowerCase();
const value = rawValue.trim().toLowerCase();
if (key === 'sslmode') {
if (value === 'disable') {
if (explicitSsl) {
return true;
}
} else if (
value === 'require' ||
value === 'verify-ca' ||
value === 'verify-full' ||
value === 'no-verify'
) {
if (!explicitSsl) {
return true;
}
} else {
// `allow`, `prefer`, or any unrecognized sslmode conflicts with an explicit boolean override.
return true;
}
} else if (key === 'ssl') {
if (value === 'false' || value === '0') {
if (explicitSsl) {
return true;
}
} else if (value === 'true' || value === '1') {
if (!explicitSsl) {
return true;
}
} else {
return true;
}
} else if (
(key === 'sslcert' || key === 'sslkey' || key === 'sslrootcert') &&
value.length > 0 &&
!explicitSsl
) {
for (const [rawKey] of parsed.searchParams.entries()) {
if (isQuestDbUrlTlsParamKey(rawKey)) {
return true;
}
}
@ -374,10 +342,10 @@ function parseQuestDbConfig(data: {
);
}
const url = parsePgWireUrl(rawUrl, ENV_QUESTDB_URL);
if (ssl !== undefined && hasConflictingUrlSslSetting(url, ssl)) {
if (ssl !== undefined && hasQuestDbUrlTlsOptions(url)) {
throw new ResearchCopilotHostConfigError(
ENV_QUESTDB_SSL,
`Conflicting configuration variable "${ENV_QUESTDB_SSL}": conflicts with SSL query parameters in "${ENV_QUESTDB_URL}".`
`Conflicting configuration variable "${ENV_QUESTDB_SSL}": cannot be combined with TLS/SSL query parameters in "${ENV_QUESTDB_URL}". Configure TLS in either the URL or "${ENV_QUESTDB_SSL}", not both.`
);
}
return {

View file

@ -1243,17 +1243,29 @@ describe('runResearchCopilotStdioHost', () => {
});
// 4. Rejects conflicting URL + host, partial port-without-host, bad port, bad ssl, unknown var,
// and conflicting URL SSL query parameters vs RESEARCH_MCP_QUESTDB_SSL without leaking secrets.
// and any URL TLS/SSL query parameters combined with RESEARCH_MCP_QUESTDB_SSL without leaking secrets.
const invalidEnvs: Array<Record<string, string>> = [
{ RESEARCH_MCP_QUESTDB_URL: questUrl, RESEARCH_MCP_QUESTDB_HOST: '10.0.0.51' },
{ RESEARCH_MCP_QUESTDB_PG_PORT: '8812' },
{ RESEARCH_MCP_QUESTDB_HOST: '10.0.0.51', RESEARCH_MCP_QUESTDB_PG_PORT: '99999' },
{ RESEARCH_MCP_QUESTDB_HOST: '10.0.0.51', RESEARCH_MCP_QUESTDB_SSL: 'maybe' },
{ RESEARCH_MCP_QUESTDB_UNKNOWN: '1' },
{
RESEARCH_MCP_QUESTDB_URL: `${questUrl}?sslmode=no-verify`,
RESEARCH_MCP_QUESTDB_SSL: 'true',
},
{
RESEARCH_MCP_QUESTDB_URL: `${questUrl}?sslmode=disable`,
RESEARCH_MCP_QUESTDB_SSL: 'true',
},
{
RESEARCH_MCP_QUESTDB_URL: `${questUrl}?sslmode=disable`,
RESEARCH_MCP_QUESTDB_SSL: 'false',
},
{
RESEARCH_MCP_QUESTDB_URL: `${questUrl}?sslmode=require`,
RESEARCH_MCP_QUESTDB_SSL: 'true',
},
{
RESEARCH_MCP_QUESTDB_URL: `${questUrl}?sslmode=require`,
RESEARCH_MCP_QUESTDB_SSL: 'false',
@ -1262,6 +1274,10 @@ describe('runResearchCopilotStdioHost', () => {
RESEARCH_MCP_QUESTDB_URL: `${questUrl}?ssl=false`,
RESEARCH_MCP_QUESTDB_SSL: 'true',
},
{
RESEARCH_MCP_QUESTDB_URL: `${questUrl}?ssl=false`,
RESEARCH_MCP_QUESTDB_SSL: 'false',
},
{
RESEARCH_MCP_QUESTDB_URL: `${questUrl}?ssl=true`,
RESEARCH_MCP_QUESTDB_SSL: 'false',
@ -1274,6 +1290,10 @@ describe('runResearchCopilotStdioHost', () => {
RESEARCH_MCP_QUESTDB_URL: `${questUrl}?sslrootcert=/etc/ssl/ca.pem`,
RESEARCH_MCP_QUESTDB_SSL: 'false',
},
{
RESEARCH_MCP_QUESTDB_URL: `${questUrl}?uselibpqcompat=true`,
RESEARCH_MCP_QUESTDB_SSL: 'true',
},
];
for (const override of invalidEnvs) {
let caughtInvalid: unknown;
@ -1287,27 +1307,34 @@ describe('runResearchCopilotStdioHost', () => {
expect((caughtInvalid as Error).message).not.toContain(questUrl);
}
// Compatible URL SSL query params + matching RESEARCH_MCP_QUESTDB_SSL are accepted.
const consistentSslEnable = parseResearchCopilotHostConfig(
// Single-source-of-truth TLS configs are accepted (URL-only TLS params without
// RESEARCH_MCP_QUESTDB_SSL, or clean URL with non-TLS query params + RESEARCH_MCP_QUESTDB_SSL).
const urlOnlyTlsEnable = parseResearchCopilotHostConfig(
buildValidEnv({
RESEARCH_MCP_QUESTDB_URL: `${questUrl}?sslmode=require`,
})
);
expect(urlOnlyTlsEnable.questdb).toEqual({
url: `${questUrl}?sslmode=require`,
});
const urlOnlyTlsDisable = parseResearchCopilotHostConfig(
buildValidEnv({
RESEARCH_MCP_QUESTDB_URL: `${questUrl}?sslmode=disable`,
})
);
expect(urlOnlyTlsDisable.questdb).toEqual({
url: `${questUrl}?sslmode=disable`,
});
const flagOnlyTls = parseResearchCopilotHostConfig(
buildValidEnv({
RESEARCH_MCP_QUESTDB_URL: `${questUrl}?application_name=copilot`,
RESEARCH_MCP_QUESTDB_SSL: 'true',
})
);
expect(consistentSslEnable.questdb).toEqual({
url: `${questUrl}?sslmode=require`,
expect(flagOnlyTls.questdb).toEqual({
url: `${questUrl}?application_name=copilot`,
ssl: true,
});
const consistentSslDisable = parseResearchCopilotHostConfig(
buildValidEnv({
RESEARCH_MCP_QUESTDB_URL: `${questUrl}?sslmode=disable`,
RESEARCH_MCP_QUESTDB_SSL: 'false',
})
);
expect(consistentSslDisable.questdb).toEqual({
url: `${questUrl}?sslmode=disable`,
ssl: false,
});
// 5. Redacts QuestDB URL / password on openStore and cleanup errors.
for (const cfg of [urlConfig, hostFullConfig]) {

View file

@ -8,11 +8,16 @@ describe('scripts/questdb-init-m2', () => {
const baseUrl = `postgresql://admin:${encodeURIComponent(secret)}@10.0.0.51:8812/qdb`;
const conflictingEnvs: Array<Record<string, string>> = [
{ QUESTDB_URL: `${baseUrl}?sslmode=no-verify`, QUESTDB_SSL: 'true' },
{ QUESTDB_URL: `${baseUrl}?sslmode=disable`, QUESTDB_SSL: 'true' },
{ QUESTDB_URL: `${baseUrl}?sslmode=disable`, QUESTDB_SSL: 'false' },
{ RESEARCH_MCP_QUESTDB_URL: `${baseUrl}?sslmode=disable`, RESEARCH_MCP_QUESTDB_SSL: 'true' },
{ QUESTDB_URL: `${baseUrl}?sslmode=require`, QUESTDB_SSL: 'true' },
{ QUESTDB_URL: `${baseUrl}?sslmode=require`, QUESTDB_SSL: 'false' },
{ QUESTDB_URL: `${baseUrl}?ssl=false`, QUESTDB_SSL: 'true' },
{ QUESTDB_URL: `${baseUrl}?ssl=false`, QUESTDB_SSL: 'false' },
{ QUESTDB_URL: `${baseUrl}?sslmode=prefer`, QUESTDB_SSL: 'true' },
{ QUESTDB_URL: `${baseUrl}?uselibpqcompat=true`, QUESTDB_SSL: 'true' },
];
let openCalls = 0;
@ -34,7 +39,7 @@ describe('scripts/questdb-init-m2', () => {
expect(openCalls).toBe(0);
});
test('rejects invalid SSL flag values and accepts consistent URL + SSL configs', async () => {
test('rejects invalid SSL flag values and accepts single-source-of-truth URL or flag TLS configs', async () => {
expect(() =>
resolveQuestDbInitM2Config({
QUESTDB_HOST: '10.0.0.51',
@ -42,12 +47,19 @@ describe('scripts/questdb-init-m2', () => {
})
).toThrow(RangeError);
const resolved = resolveQuestDbInitM2Config({
const urlOnlyTls = resolveQuestDbInitM2Config({
QUESTDB_URL: 'postgresql://admin:pass@10.0.0.51:8812/qdb?sslmode=require',
});
expect(urlOnlyTls).toEqual({
url: 'postgresql://admin:pass@10.0.0.51:8812/qdb?sslmode=require',
});
const flagOnlyTls = resolveQuestDbInitM2Config({
QUESTDB_URL: 'postgresql://admin:pass@10.0.0.51:8812/qdb?application_name=init-m2',
QUESTDB_SSL: 'true',
});
expect(resolved).toEqual({
url: 'postgresql://admin:pass@10.0.0.51:8812/qdb?sslmode=require',
expect(flagOnlyTls).toEqual({
url: 'postgresql://admin:pass@10.0.0.51:8812/qdb?application_name=init-m2',
ssl: true,
});
@ -55,7 +67,7 @@ describe('scripts/questdb-init-m2', () => {
let closed = false;
const result = await runQuestDbInitM2(
{
QUESTDB_URL: 'postgresql://admin:pass@10.0.0.51:8812/qdb?sslmode=disable',
QUESTDB_URL: 'postgresql://admin:pass@10.0.0.51:8812/qdb',
QUESTDB_SSL: 'false',
},
async () => ({