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

@ -212,6 +212,65 @@ describe.skipIf(!url)('PostgreSQL MCP copilot executor (real PostgreSQL)', () =>
expect(lockRows[0]?.lock_timeout).toBe('2s');
});
test('preserves PGOPTIONS search_path when URL omits options while enforcing 10s/2s timeouts', async () => {
const f = await createIsolatedFixture();
const appName = `copilot_pgoptions_${randomUUID().slice(0, 8)}`;
// Build a URL with NO options query parameter so pg must honor PGOPTIONS.
const parsed = new URL(f.safeUrl);
parsed.searchParams.delete('options');
parsed.searchParams.set('application_name', appName);
const previousPgOptions = process.env.PGOPTIONS;
process.env.PGOPTIONS = `-c search_path=${f.schema} -c statement_timeout=99999 -c lock_timeout=88888`;
cleanup.push(() => {
if (previousPgOptions === undefined) {
delete process.env.PGOPTIONS;
} else {
process.env.PGOPTIONS = previousPgOptions;
}
});
const handle = await openResearchCopilotExecutor({ postgresUrl: parsed.toString() });
cleanup.push(() => handle.close());
const { rows: schemaRows } = await handle.executor.query<{ current_schema: string | null }>(
'SELECT current_schema() AS current_schema'
);
const { rows: stmtRows } = await handle.executor.query<{ statement_timeout: string }>(
'SHOW statement_timeout'
);
const { rows: lockRows } = await handle.executor.query<{ lock_timeout: string }>(
'SHOW lock_timeout'
);
expect(schemaRows[0]?.current_schema).toBe(f.schema);
expect(stmtRows[0]?.statement_timeout).toBe('10s');
expect(lockRows[0]?.lock_timeout).toBe('2s');
// Verify unqualified findings writes and reads resolve against the PGOPTIONS search_path.
const writePort = createPostgresFindingWritePort({ executor: handle.executor });
const readers = createPostgresKnowledgeReaders({ executor: handle.executor });
const created = await writePort.appendFinding(
{
id: 'finding-pgoptions-0001',
kind: 'finding',
title: 'PGOPTIONS search_path finding',
summary: 'Resolved via PGOPTIONS search_path without explicit schema option.',
status: 'pending_review',
createdAt: FIXED_TS,
trialRefs: [],
evidenceRefs: [],
},
'idem-pgoptions-0001'
);
expect(created.status).toBe('created');
const listed = await readers.listFindings({ query: 'PGOPTIONS search_path' });
expect(listed).toHaveLength(1);
expect(listed[0]?.id).toBe('finding-pgoptions-0001');
});
test('supports migrated findings writes, reads, and idempotent same-ID 23505 replay', async () => {
const f = await createIsolatedFixture();
const appName = `copilot_findings_${randomUUID().slice(0, 8)}`;