T-197: M2.1t (port to master) — Real PostgreSQL executor factory for the MCP copilot stdio host: restore openResearchCopilotExecutor(config) in libs/data/postgres/src/research-copilot-executor.ts from prior branch studio/auto-db2c48b5 (T-189) onto master (no new deps, no DuckDB, no bin)

This commit is contained in:
Studio 2026-12-03 14:33:00 -05:00
parent 4315d956ea
commit 9d39380ca8
4 changed files with 360 additions and 62 deletions

View file

@ -97,6 +97,199 @@ describe('openResearchCopilotExecutor edge cases', () => {
await handle.close();
});
test('accepts Unix-domain socket host query parameter and resolves domain-socket parameters in pg', async () => {
const capturedConfigs: PoolConfig[] = [];
const fakePool: ResearchCopilotExecutorPool = {
async query() {
return { rows: [{ '?column?': 1 }] };
},
async end() {},
on() {
return fakePool;
},
};
const handle = await openResearchCopilotExecutor(
{
postgresUrl:
'postgres:///research_db?host=/var/run/postgresql&statement_timeout=99999&lock_timeout=88888',
},
{
createPool(poolConfig) {
capturedConfigs.push(poolConfig);
return fakePool;
},
}
);
expect(capturedConfigs).toHaveLength(1);
const client = new Client(capturedConfigs[0]);
const params = (
client as unknown as {
connectionParameters: {
host?: string;
database?: string;
isDomainSocket?: boolean;
statement_timeout?: unknown;
lock_timeout?: unknown;
query_timeout?: unknown;
};
}
).connectionParameters;
expect(params.host).toBe('/var/run/postgresql');
expect(params.database).toBe('research_db');
expect(params.isDomainSocket).toBe(true);
expect(params.statement_timeout).toBe(RESEARCH_COPILOT_PG_STATEMENT_TIMEOUT_MS);
expect(params.lock_timeout).toBe(RESEARCH_COPILOT_PG_LOCK_TIMEOUT_MS);
expect(params.query_timeout).toBe(RESEARCH_COPILOT_PG_QUERY_TIMEOUT_MS);
await handle.close();
});
test('rejects URLs missing host or database even when PGHOST and PGDATABASE are set', async () => {
const previousPgHost = process.env.PGHOST;
const previousPgDatabase = process.env.PGDATABASE;
process.env.PGHOST = '127.0.0.1';
process.env.PGDATABASE = 'fallback_db';
cleanup.push(() => {
if (previousPgHost === undefined) {
delete process.env.PGHOST;
} else {
process.env.PGHOST = previousPgHost;
}
if (previousPgDatabase === undefined) {
delete process.env.PGDATABASE;
} else {
process.env.PGDATABASE = previousPgDatabase;
}
});
let createPoolCalls = 0;
for (const postgresUrl of [
'postgres:///research_db',
'postgres://127.0.0.1:5432/',
'postgres://127.0.0.1:5432',
]) {
await expect(
openResearchCopilotExecutor(
{ postgresUrl },
{
createPool() {
createPoolCalls += 1;
return {
async query() {
return { rows: [] };
},
async end() {},
on() {
return undefined;
},
};
},
}
)
).rejects.toMatchObject({
name: 'ResearchCopilotExecutorError',
code: 'INVALID_CONFIG',
});
}
expect(createPoolCalls).toBe(0);
});
test('preserves PGOPTIONS startup precedence while enforcing executor timeouts', async () => {
const previousPgOptions = process.env.PGOPTIONS;
cleanup.push(() => {
if (previousPgOptions === undefined) {
delete process.env.PGOPTIONS;
} else {
process.env.PGOPTIONS = previousPgOptions;
}
});
const fakePool: ResearchCopilotExecutorPool = {
async query() {
return { rows: [{ '?column?': 1 }] };
},
async end() {},
on() {
return fakePool;
},
};
// 1. No URL options and no PGOPTIONS -> PoolConfig.options omitted.
delete process.env.PGOPTIONS;
let captured: PoolConfig | undefined;
const h1 = await openResearchCopilotExecutor(
{ postgresUrl: 'postgres://127.0.0.1:5432/research_db' },
{
createPool(poolConfig) {
captured = poolConfig;
return fakePool;
},
}
);
expect(captured?.options).toBeUndefined();
const c1 = new Client(captured);
const p1 = (
c1 as unknown as {
connectionParameters: {
options?: string;
statement_timeout?: unknown;
lock_timeout?: unknown;
};
}
).connectionParameters;
expect(p1.options).toBeUndefined();
expect(p1.statement_timeout).toBe(RESEARCH_COPILOT_PG_STATEMENT_TIMEOUT_MS);
expect(p1.lock_timeout).toBe(RESEARCH_COPILOT_PG_LOCK_TIMEOUT_MS);
await h1.close();
// 2. PGOPTIONS set with no URL options -> PGOPTIONS preserved + executor timeouts appended.
process.env.PGOPTIONS = '-c search_path=research -c statement_timeout=99999 -c lock_timeout=88888';
captured = undefined;
const h2 = await openResearchCopilotExecutor(
{ postgresUrl: 'postgres://127.0.0.1:5432/research_db' },
{
createPool(poolConfig) {
captured = poolConfig;
return fakePool;
},
}
);
expect(captured?.options).toBe(
`-c search_path=research -c statement_timeout=99999 -c lock_timeout=88888 ` +
`-c statement_timeout=${RESEARCH_COPILOT_PG_STATEMENT_TIMEOUT_MS} -c lock_timeout=${RESEARCH_COPILOT_PG_LOCK_TIMEOUT_MS}`
);
const c2 = new Client(captured);
const p2 = (
c2 as unknown as { connectionParameters: { options?: string } }
).connectionParameters;
expect(p2.options).toBe(captured?.options);
await h2.close();
// 3. Both URL options and PGOPTIONS set -> URL options wins over PGOPTIONS, timeouts still appended.
process.env.PGOPTIONS = '-c search_path=env_schema';
captured = undefined;
const h3 = await openResearchCopilotExecutor(
{
postgresUrl:
'postgres://127.0.0.1:5432/research_db?options=-c%20search_path%3Durl_schema',
},
{
createPool(poolConfig) {
captured = poolConfig;
return fakePool;
},
}
);
expect(captured?.options).toBe(
`-c search_path=url_schema ` +
`-c statement_timeout=${RESEARCH_COPILOT_PG_STATEMENT_TIMEOUT_MS} -c lock_timeout=${RESEARCH_COPILOT_PG_LOCK_TIMEOUT_MS}`
);
await h3.close();
});
test('absorbs idle-client pool error events without crashing the process', async () => {
const errorListeners: Array<(error: Error) => void> = [];
const fakePool: ResearchCopilotExecutorPool = {