T-161: M2.1j (re-queue) — PostgreSQL findings adapter for the MCP copilot: FindingWritePort (atomic idempotent append) + listFindings / listProviderCaveats readers over an injected executor in libs/research/src/mcp/adapters/postgres-findings.ts (no new deps)
This commit is contained in:
parent
5654b9cc05
commit
a535ebefb5
4 changed files with 280 additions and 47 deletions
|
|
@ -51,10 +51,13 @@ export class PostgresFindingsError extends RangeError {
|
|||
* Structural `pg.Pool` / `pg.PoolClient`-compatible query interface so this module
|
||||
* has no runtime dependency on `pg` and is unit-testable with a fake executor.
|
||||
*
|
||||
* For {@link createPostgresFindingWritePort}, the executor should run each call on
|
||||
* a fresh `READ COMMITTED` snapshot (such as a `pg.Pool`) rather than inside a
|
||||
* `REPEATABLE READ` / `SERIALIZABLE` transaction so concurrent idempotent callers
|
||||
* can observe the committed winner after `ON CONFLICT ... DO NOTHING`.
|
||||
* For {@link createPostgresFindingWritePort}, the executor must run each `query()`
|
||||
* call in autocommit mode on a fresh `READ COMMITTED` snapshot (such as a `pg.Pool`)
|
||||
* rather than inside a `REPEATABLE READ` / `SERIALIZABLE` or explicit `BEGIN`
|
||||
* transaction (unless the executor wraps each statement in a `SAVEPOINT` and rolls
|
||||
* it back before rethrowing errors). Autocommit ensures that `ON CONFLICT ... DO
|
||||
* NOTHING` winner lookups observe the committed winner and that same-ID `23505`
|
||||
* primary-key retry lookups do not fail with `25P02` (aborted transaction).
|
||||
*/
|
||||
export interface PostgresFindingsExecutor {
|
||||
query<Row = Record<string, unknown>>(
|
||||
|
|
@ -136,6 +139,24 @@ function escapeLikePattern(value: string): string {
|
|||
return value.replace(/[\\%_]/g, '\\$&');
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns `true` when every UTF-16 code unit in `value` is ASCII (`<= 0x7F`).
|
||||
*
|
||||
* PostgreSQL libc `ILIKE` / `lower()` lowercases characters individually, which
|
||||
* diverges from JavaScript's context-sensitive Unicode `.toLowerCase()` (e.g.
|
||||
* Greek final sigma `ΚΟΣΜΟΣ` -> `κοσμος`). SQL pushdown is used only for ASCII
|
||||
* text where casing agrees with `read_knowledge`; non-ASCII queries fall back
|
||||
* to filtering validated candidates with JavaScript's `.toLowerCase()` rules.
|
||||
*/
|
||||
function isAsciiOnly(value: string): boolean {
|
||||
for (let i = 0; i < value.length; i += 1) {
|
||||
if (value.charCodeAt(i) > 0x7f) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
function quoteTable(schemaPrefix: string, table: string): string {
|
||||
return `${schemaPrefix}${table}`;
|
||||
}
|
||||
|
|
@ -586,13 +607,18 @@ export function createPostgresKnowledgeReaders(
|
|||
`FROM ${findingsTable}`,
|
||||
];
|
||||
const params: unknown[] = [];
|
||||
let fallbackNormalizedQuery: string | null = null;
|
||||
|
||||
if (q.id !== undefined) {
|
||||
params.push(q.id);
|
||||
lines.push('WHERE id = $1::text');
|
||||
} else if (q.query !== undefined) {
|
||||
params.push(`%${escapeLikePattern(q.query)}%`);
|
||||
lines.push("WHERE title ILIKE $1::text ESCAPE '\\' OR summary ILIKE $1::text ESCAPE '\\'");
|
||||
if (isAsciiOnly(q.query)) {
|
||||
params.push(`%${escapeLikePattern(q.query)}%`);
|
||||
lines.push("WHERE title ILIKE $1::text ESCAPE '\\' OR summary ILIKE $1::text ESCAPE '\\'");
|
||||
} else {
|
||||
fallbackNormalizedQuery = q.query.toLowerCase();
|
||||
}
|
||||
}
|
||||
|
||||
lines.push('ORDER BY id ASC');
|
||||
|
|
@ -601,7 +627,15 @@ export function createPostgresKnowledgeReaders(
|
|||
const rows = extractRows<RawFindingRow>(result);
|
||||
const mapped: FindingRecord[] = [];
|
||||
for (let i = 0; i < rows.length; i += 1) {
|
||||
mapped.push(parseFindingRow(rows[i]));
|
||||
const finding = parseFindingRow(rows[i]);
|
||||
if (
|
||||
fallbackNormalizedQuery !== null &&
|
||||
!finding.title.toLowerCase().includes(fallbackNormalizedQuery) &&
|
||||
!finding.summary.toLowerCase().includes(fallbackNormalizedQuery)
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
mapped.push(finding);
|
||||
}
|
||||
return mapped;
|
||||
},
|
||||
|
|
@ -618,21 +652,31 @@ export function createPostgresKnowledgeReaders(
|
|||
];
|
||||
const conditions: string[] = [];
|
||||
const params: unknown[] = [];
|
||||
let fallbackNormalizedProvider: string | null = null;
|
||||
let fallbackNormalizedQuery: string | null = null;
|
||||
|
||||
if (q.provider !== undefined) {
|
||||
params.push(q.provider);
|
||||
conditions.push(`lower(provider) = lower($${params.length}::text)`);
|
||||
if (isAsciiOnly(q.provider)) {
|
||||
params.push(q.provider);
|
||||
conditions.push(`lower(provider) = lower($${params.length}::text)`);
|
||||
} else {
|
||||
fallbackNormalizedProvider = q.provider.toLowerCase();
|
||||
}
|
||||
}
|
||||
|
||||
if (q.id !== undefined) {
|
||||
params.push(q.id);
|
||||
conditions.push(`id = $${params.length}::text`);
|
||||
} else if (q.query !== undefined) {
|
||||
params.push(`%${escapeLikePattern(q.query)}%`);
|
||||
const idx = params.length;
|
||||
conditions.push(
|
||||
`(provider ILIKE $${idx}::text ESCAPE '\\' OR dataset ILIKE $${idx}::text ESCAPE '\\' OR caveat ILIKE $${idx}::text ESCAPE '\\')`
|
||||
);
|
||||
if (isAsciiOnly(q.query)) {
|
||||
params.push(`%${escapeLikePattern(q.query)}%`);
|
||||
const idx = params.length;
|
||||
conditions.push(
|
||||
`(provider ILIKE $${idx}::text ESCAPE '\\' OR dataset ILIKE $${idx}::text ESCAPE '\\' OR caveat ILIKE $${idx}::text ESCAPE '\\')`
|
||||
);
|
||||
} else {
|
||||
fallbackNormalizedQuery = q.query.toLowerCase();
|
||||
}
|
||||
}
|
||||
|
||||
if (conditions.length > 0) {
|
||||
|
|
@ -645,7 +689,22 @@ export function createPostgresKnowledgeReaders(
|
|||
const rows = extractRows<RawProviderCaveatRow>(result);
|
||||
const mapped: ProviderCaveatRecord[] = [];
|
||||
for (let i = 0; i < rows.length; i += 1) {
|
||||
mapped.push(parseProviderCaveatRow(rows[i]));
|
||||
const caveat = parseProviderCaveatRow(rows[i]);
|
||||
if (
|
||||
fallbackNormalizedProvider !== null &&
|
||||
caveat.provider.toLowerCase() !== fallbackNormalizedProvider
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
if (
|
||||
fallbackNormalizedQuery !== null &&
|
||||
!caveat.provider.toLowerCase().includes(fallbackNormalizedQuery) &&
|
||||
!caveat.dataset.toLowerCase().includes(fallbackNormalizedQuery) &&
|
||||
!caveat.caveat.toLowerCase().includes(fallbackNormalizedQuery)
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
mapped.push(caveat);
|
||||
}
|
||||
return mapped;
|
||||
},
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue