From 72701b2b99524d6d815dfabbc13dc980d8fdc226 Mon Sep 17 00:00:00 2001 From: Daniel Covington Date: Thu, 3 Dec 2026 07:35:32 -0500 Subject: [PATCH 1/2] Parse the embedding once in the knowledge duplicate check Add-FoundryKnowledge cast the payload embedding from JSON text to a vector inside the per-row expressions, so it was re-parsed for every stored entry. At about 2,400 entries each insert took 1-3 s, and the cost grew with the table. The embedding is now parsed once in a MATERIALIZED CTE, so PostgreSQL cannot inline the cast back into the rows. Verified: tests/Knowledge.Tests.ps1 passes 17/17, including both duplicate-detection checks. Co-Authored-By: Claude Opus 5.5 --- powershell/Foundry.Knowledge/Public/Knowledge.ps1 | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/powershell/Foundry.Knowledge/Public/Knowledge.ps1 b/powershell/Foundry.Knowledge/Public/Knowledge.ps1 index e83058e..6621d68 100644 --- a/powershell/Foundry.Knowledge/Public/Knowledge.ps1 +++ b/powershell/Foundry.Knowledge/Public/Knowledge.ps1 @@ -62,18 +62,20 @@ function Add-FoundryKnowledge { if (-not $Force) { $dup = @(Invoke-FkeSql -Payload $payload -Sql @' -WITH p AS (SELECT current_setting('fke.p')::jsonb AS j) +WITH s AS (SELECT current_setting('fke.p')::jsonb AS j), + -- Parse the embedding once here; casting it inside the row expressions re-parsed it per stored row. + p AS MATERIALIZED (SELECT j, CASE WHEN j ? 'embedding' THEN (j->'embedding')::text::vector END AS v FROM s) SELECT COALESCE(jsonb_agg(d), '[]'::jsonb) FROM ( SELECT e.id, e.title, - CASE WHEN p.j ? 'embedding' AND e.embedding IS NOT NULL - THEN ROUND((1 - (e.embedding <=> (p.j->'embedding')::text::vector))::numeric, 3) END AS similarity, + CASE WHEN p.v IS NOT NULL AND e.embedding IS NOT NULL + THEN ROUND((1 - (e.embedding <=> p.v))::numeric, 3) END AS similarity, (lower(btrim(e.title)) = lower(btrim(p.j->>'title'))) AS same_title FROM knowledge.entries e, p WHERE e.status <> 'superseded' AND e.project_key IS NOT DISTINCT FROM NULLIF(p.j->>'project', '') AND ( lower(btrim(e.title)) = lower(btrim(p.j->>'title')) - OR (p.j ? 'embedding' AND e.embedding IS NOT NULL - AND 1 - (e.embedding <=> (p.j->'embedding')::text::vector) >= (p.j->>'threshold')::float8) ) + OR (p.v IS NOT NULL AND e.embedding IS NOT NULL + AND 1 - (e.embedding <=> p.v) >= (p.j->>'threshold')::float8) ) ORDER BY 3 DESC NULLS LAST, e.id LIMIT 3) d; '@) if ($dup.Count) { From b194ae5abc37e56f362660cc7a34d4c660a6d596 Mon Sep 17 00:00:00 2001 From: Daniel Covington Date: Thu, 3 Dec 2026 07:35:32 -0500 Subject: [PATCH 2/2] Add optional Adminer database viewer to docker-compose Lets people browse the knowledge database at http://localhost:8080. It sits behind the "viewer" profile, so a plain "docker compose up" does not start it, and it is bound to 127.0.0.1 only. Login steps are in the compose comments. It logs in as the main database user, so it is for browsing only: edits made there bypass the Foundry.Knowledge module's rules. Co-Authored-By: Claude Opus 5.5 --- docker-compose.yml | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/docker-compose.yml b/docker-compose.yml index 6f32c65..3feb7ac 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -35,6 +35,22 @@ services: volumes: - ollama_data:/root/.ollama + # Optional database browser for people (agents use the Foundry.Knowledge module, never this). + # docker compose --profile viewer up -d adminer then open http://localhost:8080 + # Log in: System PostgreSQL, Server postgres, Username foundry, Password from .env, Database foundry + # Look in schema "knowledge". Browse only: edits here bypass the module's rules. + adminer: + image: adminer + container_name: foundry-adminer + profiles: ["viewer"] + restart: unless-stopped + environment: + ADMINER_DEFAULT_SERVER: postgres + ports: + - "127.0.0.1:8080:8080" + depends_on: + - postgres + volumes: postgres_data: ollama_data: