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
a535ebefb5
commit
3064e15c0f
4 changed files with 236 additions and 35 deletions
|
|
@ -139,14 +139,23 @@ function escapeLikePattern(value: string): string {
|
|||
return value.replace(/[\\%_]/g, '\\$&');
|
||||
}
|
||||
|
||||
/**
|
||||
* PostgreSQL POSIX regex matching any non-ASCII character (`> 0x7F`).
|
||||
*
|
||||
* Used alongside ASCII SQL pushdown (`ILIKE` / `lower()`) so rows containing
|
||||
* non-ASCII stored text (e.g. `BİST` under `C` collation or `K` U+212A) are
|
||||
* kept as candidates and evaluated with JavaScript's `.toLowerCase()` rules.
|
||||
*/
|
||||
const PG_NON_ASCII_REGEX = "'[^\\x01-\\x7f]'";
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* PostgreSQL `ILIKE` / `lower()` agrees with JavaScript `.toLowerCase()` only
|
||||
* when both the selector and the stored text are pure ASCII. Non-ASCII
|
||||
* selectors omit SQL casing filters, and ASCII selectors retain non-ASCII
|
||||
* stored rows as candidates; all returned rows are then filtered with
|
||||
* JavaScript's `.toLowerCase()` rules after validation.
|
||||
*/
|
||||
function isAsciiOnly(value: string): boolean {
|
||||
for (let i = 0; i < value.length; i += 1) {
|
||||
|
|
@ -607,17 +616,18 @@ export function createPostgresKnowledgeReaders(
|
|||
`FROM ${findingsTable}`,
|
||||
];
|
||||
const params: unknown[] = [];
|
||||
let fallbackNormalizedQuery: string | null = null;
|
||||
let normalizedQuery: string | null = null;
|
||||
|
||||
if (q.id !== undefined) {
|
||||
params.push(q.id);
|
||||
lines.push('WHERE id = $1::text');
|
||||
} else if (q.query !== undefined) {
|
||||
normalizedQuery = q.query.toLowerCase();
|
||||
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(
|
||||
`WHERE title ILIKE $1::text ESCAPE '\\' OR summary ILIKE $1::text ESCAPE '\\' OR title ~ ${PG_NON_ASCII_REGEX} OR summary ~ ${PG_NON_ASCII_REGEX}`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -629,9 +639,9 @@ export function createPostgresKnowledgeReaders(
|
|||
for (let i = 0; i < rows.length; i += 1) {
|
||||
const finding = parseFindingRow(rows[i]);
|
||||
if (
|
||||
fallbackNormalizedQuery !== null &&
|
||||
!finding.title.toLowerCase().includes(fallbackNormalizedQuery) &&
|
||||
!finding.summary.toLowerCase().includes(fallbackNormalizedQuery)
|
||||
normalizedQuery !== null &&
|
||||
!finding.title.toLowerCase().includes(normalizedQuery) &&
|
||||
!finding.summary.toLowerCase().includes(normalizedQuery)
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
|
|
@ -652,15 +662,16 @@ export function createPostgresKnowledgeReaders(
|
|||
];
|
||||
const conditions: string[] = [];
|
||||
const params: unknown[] = [];
|
||||
let fallbackNormalizedProvider: string | null = null;
|
||||
let fallbackNormalizedQuery: string | null = null;
|
||||
let normalizedProvider: string | null = null;
|
||||
let normalizedQuery: string | null = null;
|
||||
|
||||
if (q.provider !== undefined) {
|
||||
normalizedProvider = q.provider.toLowerCase();
|
||||
if (isAsciiOnly(q.provider)) {
|
||||
params.push(q.provider);
|
||||
conditions.push(`lower(provider) = lower($${params.length}::text)`);
|
||||
} else {
|
||||
fallbackNormalizedProvider = q.provider.toLowerCase();
|
||||
conditions.push(
|
||||
`(lower(provider) = lower($${params.length}::text) OR provider ~ ${PG_NON_ASCII_REGEX})`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -668,14 +679,13 @@ export function createPostgresKnowledgeReaders(
|
|||
params.push(q.id);
|
||||
conditions.push(`id = $${params.length}::text`);
|
||||
} else if (q.query !== undefined) {
|
||||
normalizedQuery = q.query.toLowerCase();
|
||||
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 '\\')`
|
||||
`(provider ILIKE $${idx}::text ESCAPE '\\' OR dataset ILIKE $${idx}::text ESCAPE '\\' OR caveat ILIKE $${idx}::text ESCAPE '\\' OR provider ~ ${PG_NON_ASCII_REGEX} OR dataset ~ ${PG_NON_ASCII_REGEX} OR caveat ~ ${PG_NON_ASCII_REGEX})`
|
||||
);
|
||||
} else {
|
||||
fallbackNormalizedQuery = q.query.toLowerCase();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -691,16 +701,16 @@ export function createPostgresKnowledgeReaders(
|
|||
for (let i = 0; i < rows.length; i += 1) {
|
||||
const caveat = parseProviderCaveatRow(rows[i]);
|
||||
if (
|
||||
fallbackNormalizedProvider !== null &&
|
||||
caveat.provider.toLowerCase() !== fallbackNormalizedProvider
|
||||
normalizedProvider !== null &&
|
||||
caveat.provider.toLowerCase() !== normalizedProvider
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
if (
|
||||
fallbackNormalizedQuery !== null &&
|
||||
!caveat.provider.toLowerCase().includes(fallbackNormalizedQuery) &&
|
||||
!caveat.dataset.toLowerCase().includes(fallbackNormalizedQuery) &&
|
||||
!caveat.caveat.toLowerCase().includes(fallbackNormalizedQuery)
|
||||
normalizedQuery !== null &&
|
||||
!caveat.provider.toLowerCase().includes(normalizedQuery) &&
|
||||
!caveat.dataset.toLowerCase().includes(normalizedQuery) &&
|
||||
!caveat.caveat.toLowerCase().includes(normalizedQuery)
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue