Skip to content

AI-powered virtual executive team — a single coherent executive persona backed by 8 specialist agents (FastAPI + Next.js).

fix(review): close the holes the previous round's fixes opened

Claude665157d0a00e
diff --git a/packages/core/openexecutive/api/routes/knowledge.py b/packages/core/openexecutive/api/routes/knowledge.py
index 3d83cea3..a9130dd2 100644
--- a/packages/core/openexecutive/api/routes/knowledge.py
+++ b/packages/core/openexecutive/api/routes/knowledge.py
@@ -92,6 +92,24 @@ def _review_store(): # type: ignore[no-untyped-def]
return ReviewStore(db_path=DB_PATH)
+def _existing_review_item(item_id: str): # type: ignore[no-untyped-def]
+ """Look up a review item without 500ing or materialising the database.
+
+ `sqlite3.connect` creates the file, and a DB with no `review_items` table
+ raises — neither should turn a legitimate upload into a server error.
+ Absent review state simply means nothing is known to be trusted.
+ """
+ from openexecutive.memory.episodic import DB_PATH
+
+ if not DB_PATH.exists():
+ return None
+ try:
+ return _review_store().get_item(item_id)
+ except Exception:
+ logger.warning("review state unreadable during upload check", exc_info=True)
+ return None
+
+
@router.get("/builtin", response_model=BuiltinListResponse)
async def list_builtin_files() -> BuiltinListResponse:
files: list[BuiltinFileMeta] = []
@@ -134,7 +152,7 @@ async def create_builtin_file(body: BuiltinFileWrite, request: Request) -> Built
# FAILURE namespace fixes that at the source; this makes sure no future
# id-space change can quietly re-open it.
item_id = build_item_id(ContentType.BUILTIN, body.domain, body.filename)
- existing = _review_store().get_item(item_id)
+ existing = _existing_review_item(item_id)
if existing is not None and existing.trusted_default:
raise HTTPException(
status_code=409,
@@ -303,7 +321,9 @@ async def list_external_sources(request: Request) -> ExternalSourcesResponse:
if s.is_ingested
]
if ingested:
- ReviewStore.sync_external_registrations(ingested)
+ from openexecutive.memory.episodic import DB_PATH as _REVIEW_DB
+
+ ReviewStore.sync_external_registrations(ingested, _REVIEW_DB)
return ExternalSourcesResponse(sources=sources, total_chunks=sum(chunk_counts.values()))
@@ -471,6 +491,21 @@ async def update_failure_file(
chunk_size=400,
overlap=40,
)
+ # Mirror update_builtin_file: register (a doc created before failure docs
+ # were registered still has no row, and editing must create one) and mark
+ # it needs_revision so an edit behaves the same for both content types.
+ from openexecutive.knowledge.review_store import ContentType, build_item_id
+
+ rs = _review_store()
+ item_id = build_item_id(ContentType.FAILURE, domain, filename)
+ rs.register(
+ item_id=item_id,
+ content_type=ContentType.FAILURE,
+ domain=domain,
+ filename=filename,
+ )
+ rs.touch_modified(item_id)
+
return BuiltinWriteResponse(domain=domain, filename=filename, chunks_indexed=chunks)
diff --git a/packages/core/openexecutive/knowledge/loader.py b/packages/core/openexecutive/knowledge/loader.py
index 426c4728..cc90b4eb 100644
--- a/packages/core/openexecutive/knowledge/loader.py
+++ b/packages/core/openexecutive/knowledge/loader.py
@@ -1,5 +1,6 @@
from __future__ import annotations
+import contextlib
import hashlib
import logging
import re
@@ -128,8 +129,25 @@ def _make_chunk_id(source: str, chunk_index: int) -> str:
return hashlib.md5(base.encode()).hexdigest()
-def infer_domain_from_path(path: Path) -> str:
- for part in path.parts:
+def infer_domain_from_path(path: Path, root: Path | None = None) -> str:
+ """Infer a knowledge domain from a path's components.
+
+ Pass ``root`` for content under a known tree: only the parts BELOW it are
+ scanned. Without it this walks the ABSOLUTE path, so an install rooted at
+ e.g. `/srv/product/` tags every chunk `product` no matter which domain
+ directory the file is actually in. That was merely a mis-tag until the
+ review gate began keying on `(domain, filename)` — a chunk domain that
+ disagrees with its review row's domain means the key never matches and
+ withheld content is retrieved. Callers under `knowledge/builtin/` must
+ pass the root so the metadata agrees with what `review_items` stores.
+ """
+ parts = path.parts
+ if root is not None:
+ # A path outside the root keeps its absolute parts — the caller asked
+ # for a hint, not a constraint.
+ with contextlib.suppress(ValueError):
+ parts = path.relative_to(root).parts
+ for part in parts:
domain = DOMAIN_MAP.get(part.lower())
if domain:
return domain
@@ -270,7 +288,7 @@ async def ingest_builtin_file(
text = path.read_text(encoding="utf-8")
if not text.strip():
return 0
- domain = infer_domain_from_path(path)
+ domain = infer_domain_from_path(path, root=BUILTIN_KNOWLEDGE_PATH)
chunks = chunk_text(text, chunk_size=chunk_size, overlap=overlap)
metadatas: list[dict[str, Any]] = [
{
@@ -420,7 +438,7 @@ async def seed_builtin_knowledge(
# collection by openexecutive.knowledge.skills_index.seed_builtin_skills.
if any(p in md_file.relative_to(BUILTIN_KNOWLEDGE_PATH).parts for p in ("skills", "failures")):
continue
- domain = infer_domain_from_path(md_file)
+ domain = infer_domain_from_path(md_file, root=BUILTIN_KNOWLEDGE_PATH)
text = md_file.read_text(encoding="utf-8")
chunks = chunk_text(text)
@@ -471,7 +489,7 @@ async def seed_failures(
total = 0
for md_file in FAILURES_KNOWLEDGE_PATH.rglob("*.md"):
- domain = infer_domain_from_path(md_file)
+ domain = infer_domain_from_path(md_file, root=BUILTIN_KNOWLEDGE_PATH)
text = md_file.read_text(encoding="utf-8")
if not text.strip():
continue
diff --git a/packages/core/openexecutive/knowledge/review_store.py b/packages/core/openexecutive/knowledge/review_store.py
index e6b0ef1b..80f15cd7 100644
--- a/packages/core/openexecutive/knowledge/review_store.py
+++ b/packages/core/openexecutive/knowledge/review_store.py
@@ -203,6 +203,7 @@ def _migrate_failure_namespace(conn: sqlite3.Connection) -> int:
if already is not None:
return 0
+ from openexecutive.knowledge.loader import BUILTIN_KNOWLEDGE_PATH
from openexecutive.knowledge.shipped_manifest import SHIPPED_BUILTIN_FILES
migrated = 0
@@ -223,6 +224,35 @@ def _migrate_failure_namespace(conn: sqlite3.Connection) -> int:
if old is None:
continue
+ # A real file at knowledge/builtin/<domain>/<filename> means this row
+ # is NOT (only) the shipped failure doc: it is a user upload that the
+ # old bug merged onto the shipped row — one row governing two files.
+ # Transplanting its decision onto the case study would both mis-apply
+ # the SME's judgement AND delete the only row governing the upload,
+ # silently un-suppressing content someone had rejected. Keep the row
+ # for the upload it actually governs, untrusted and withheld, and let
+ # the shipped doc keep its own fresh trusted-default row.
+ if (BUILTIN_KNOWLEDGE_PATH / domain / filename).exists():
+ now = datetime.now(UTC).isoformat()
+ if old["reviewed_at"] is None:
+ # Never actually decided — the `approved` came from the shipped
+ # default this row inherited, not from a human. Queue it.
+ conn.execute(
+ "UPDATE review_items SET trusted_default = 0, status = 'pending', "
+ "last_modified_at = ? WHERE item_id = ?",
+ (now, old_id),
+ )
+ else:
+ # A human decided. Keep that decision — it was far more likely
+ # about their own upload than about a case study they never
+ # saw — and just strip the trust the collision conferred.
+ conn.execute(
+ "UPDATE review_items SET trusted_default = 0, last_modified_at = ? "
+ "WHERE item_id = ?",
+ (now, old_id),
+ )
+ continue
+
# Carry the human's decision onto the new row, then drop the old one.
conn.execute(
"UPDATE review_items SET status = ?, reviewed_at = ?, "
diff --git a/packages/core/tests/unit/test_knowledge_search_and_failures.py b/packages/core/tests/unit/test_knowledge_search_and_failures.py
index 0e7e21ec..933d4a70 100644
--- a/packages/core/tests/unit/test_knowledge_search_and_failures.py
+++ b/packages/core/tests/unit/test_knowledge_search_and_failures.py
@@ -355,8 +355,12 @@ def test_search_omits_a_rejected_external_source(client: TestClient, review_db:
def test_search_omits_a_withheld_failure_case(client: TestClient, review_db: Any) -> None:
- """Failure cases share review registrations but their own Chroma collection."""
- _register(review_db, "strategy", "kodak-digital.md")
+ """Failure cases have their own content type AND their own Chroma collection."""
+ from openexecutive.knowledge.review_store import ContentType
+
+ _register(
+ review_db, "strategy", "kodak-digital.md", content_type=ContentType.FAILURE
+ )
data = client.post("/knowledge/search", json={"query": "strategy"}).json()
diff --git a/packages/core/tests/unit/test_review_store.py b/packages/core/tests/unit/test_review_store.py
index 9e00e1a9..73dbd13c 100644
--- a/packages/core/tests/unit/test_review_store.py
+++ b/packages/core/tests/unit/test_review_store.py
@@ -957,3 +957,136 @@ def test_legacy_upgrade_does_not_requeue_failure_docs(tmp_path: Path) -> None:
assert store.get_item(f"builtin:{d0}:{f0}") is None
assert store.get_item(build_item_id(ContentType.FAILURE, d0, f0)) is not None
assert store.get_withheld_keys(ContentType.FAILURE) == set()
+
+
+def test_migration_preserves_a_deliberate_curation(tmp_path: Path) -> None:
+ """The other merge direction, which the backfill cannot rescue.
+
+ On a database already upgraded by an earlier commit the backfill marker is
+ set, so it is a no-op. If the operator had curated a domain containing
+ failure docs, those rows are `pending` ON PURPOSE. The migration copies
+ that across, and nothing promotes it back — which is exactly right, and is
+ why the merge direction has to be correct on its own rather than relying
+ on the backfill to clean up after it.
+ """
+ import sqlite3
+ from pathlib import PurePosixPath
+
+ from openexecutive.knowledge.review_store import build_item_id
+ from openexecutive.knowledge.shipped_manifest import SHIPPED_BUILTIN_FILES
+
+ db = tmp_path / "curated.db"
+ ReviewStore.initialize_db(db)
+ ReviewStore.sync_builtin_registrations(db)
+ ReviewStore.backfill_trusted_defaults(db)
+
+ p = next(
+ PurePosixPath(r)
+ for r in sorted(SHIPPED_BUILTIN_FILES)
+ if PurePosixPath(r).parts[0] == "failures"
+ )
+ domain = p.parent.name
+ # Rewind just the failure namespace, leaving a curated `builtin:` row.
+ with sqlite3.connect(db) as conn:
+ conn.execute("DELETE FROM review_items WHERE content_type='failure'")
+ conn.execute("DELETE FROM review_meta WHERE key='failure_namespace_v1'")
+ conn.execute(
+ "INSERT INTO review_items (item_id, content_type, domain, filename, "
+ "status, trusted_default, registered_at, last_modified_at) "
+ "VALUES (?, 'builtin', ?, ?, 'pending', 1, '2025-01-01', '2025-01-01')",
+ (f"builtin:{domain}:{p.name}", domain, p.name),
+ )
+
+ ReviewStore.sync_builtin_registrations(db)
+ ReviewStore.backfill_trusted_defaults(db) # marker already set: no-op
+
+ store = ReviewStore(db_path=db)
+ migrated = store.get_item(build_item_id(ContentType.FAILURE, domain, p.name))
+ assert migrated is not None
+ assert migrated.status == ReviewStatus.PENDING, "curation intent must survive"
+ # And the operator can still back out of it.
+ assert store.stop_curation(domain) >= 1
+ assert store.get_item(migrated.item_id).status == ReviewStatus.APPROVED # type: ignore[union-attr]
+
+
+def test_migration_does_not_hijack_a_merged_user_upload(tmp_path: Path) -> None:
+ """The population this migration exists for is where the old bug FIRED.
+
+ There, one `builtin:<d>:<f>` row governed two files: the shipped failure
+ doc and a user upload that INSERT-OR-IGNOREd onto it. Transplanting that
+ row's decision onto the case study both mis-applies the SME's judgement
+ and deletes the only row governing the upload — silently un-suppressing
+ content someone had rejected, with no row left to re-reject it from.
+ """
+ import sqlite3
+ from pathlib import PurePosixPath
+
+ from openexecutive.knowledge.loader import BUILTIN_KNOWLEDGE_PATH
+ from openexecutive.knowledge.review_store import build_item_id
+ from openexecutive.knowledge.shipped_manifest import SHIPPED_BUILTIN_FILES
+
+ p = next(
+ PurePosixPath(r)
+ for r in sorted(SHIPPED_BUILTIN_FILES)
+ if PurePosixPath(r).parts[0] == "failures"
+ )
+ domain, filename = p.parent.name, p.name
+
+ db = tmp_path / "merged.db"
+ ReviewStore.initialize_db(db)
+ ReviewStore.sync_builtin_registrations(db)
+ with sqlite3.connect(db) as conn:
+ conn.execute("DELETE FROM review_items WHERE content_type='failure'")
+ conn.execute("DELETE FROM review_meta")
+ conn.execute(
+ "INSERT INTO review_items (item_id, content_type, domain, filename, "
+ "status, trusted_default, reviewed_at, reviewer_notes, registered_at, "
+ "last_modified_at) VALUES (?, 'builtin', ?, ?, 'rejected', 1, "
+ "'2025-06-01', 'confidential draft', '2025-01-01', '2025-01-01')",
+ (f"builtin:{domain}:{filename}", domain, filename),
+ )
+
+ # The upload really is on disk — that is what makes the row ambiguous.
+ planted = BUILTIN_KNOWLEDGE_PATH / domain / filename
+ planted.write_text("# planted", encoding="utf-8")
+ try:
+ ReviewStore.sync_builtin_registrations(db)
+ ReviewStore.backfill_trusted_defaults(db)
+ store = ReviewStore(db_path=db)
+
+ upload = store.get_item(f"builtin:{domain}:{filename}")
+ assert upload is not None, "the row governing the upload must survive"
+ assert upload.status == ReviewStatus.REJECTED, "the SME decision stands"
+ assert upload.trusted_default is False, "collision-conferred trust is stripped"
+ assert (domain, filename) in store.get_withheld_keys(ContentType.BUILTIN)
+
+ shipped = store.get_item(build_item_id(ContentType.FAILURE, domain, filename))
+ assert shipped is not None
+ assert shipped.status == ReviewStatus.APPROVED, "no transplanted rejection"
+ assert shipped.trusted_default is True
+ finally:
+ planted.unlink()
+
+
+def test_builtin_chunk_domain_matches_its_review_row(tmp_path: Path) -> None:
+ """The gate key is `(domain, filename)` — both sides must derive it the same.
+
+ Chunk metadata used `infer_domain_from_path` over the ABSOLUTE path, so an
+ install under e.g. `/srv/product/` tagged every chunk `product` while
+ review rows used the directory name. No key would ever match and the gate
+ would fail wide open.
+ """
+ from openexecutive.knowledge.loader import (
+ BUILTIN_KNOWLEDGE_PATH,
+ infer_domain_from_path,
+ )
+ from openexecutive.knowledge.shipped_manifest import SHIPPED_BUILTIN_FILES
+
+ for rel in sorted(SHIPPED_BUILTIN_FILES):
+ path = BUILTIN_KNOWLEDGE_PATH / rel
+ chunk_domain = infer_domain_from_path(path, root=BUILTIN_KNOWLEDGE_PATH)
+ assert chunk_domain == path.parent.name, f"{rel}: {chunk_domain}"
+
+ # And it is immune to a domain word in the install prefix.
+ root = Path("/srv/product/app/builtin")
+ assert infer_domain_from_path(root / "strategy" / "x.md", root=root) == "strategy"
diff --git a/packages/ui/src/components/ReviewQueue.tsx b/packages/ui/src/components/ReviewQueue.tsx
index fd5ff461..b210dba5 100644
--- a/packages/ui/src/components/ReviewQueue.tsx
+++ b/packages/ui/src/components/ReviewQueue.tsx
@@ -12,6 +12,7 @@ import {
curateDomain,
deleteAnnotation,
getBuiltinFile,
+ getFailureFile,
getReviewItem,
getTrustedDefaults,
listAllAnnotations,
@@ -140,13 +141,19 @@ function ItemSlideOver({
}, [itemId_stable]);
useEffect(() => {
- if (!detail || detail.item.content_type !== "builtin") return;
+ // Failure case studies are file-backed too; without this an SME sees no
+ // content and is pushed toward approving something they cannot read.
+ const fileBacked =
+ detail?.item.content_type === "builtin" || detail?.item.content_type === "failure";
+ if (!detail || !fileBacked) return;
const [, domain, filename] = detail.item.item_id.split(":");
+ const fetchFile =
+ detail.item.content_type === "failure" ? getFailureFile : getBuiltinFile;
let cancelled = false;
setContentLoading(true);
setContentError(false);
setContent(null);
- getBuiltinFile(domain, filename)
+ fetchFile(domain, filename)
.then((f) => { if (!cancelled) setContent(f.content); })
.catch(() => { if (!cancelled) { setContent(null); setContentError(true); } })
.finally(() => { if (!cancelled) setContentLoading(false); });
@@ -230,7 +237,7 @@ function ItemSlideOver({
}
const { item, annotations } = detail;
- const isBuiltin = item.content_type === "builtin";
+ const isBuiltin = item.content_type === "builtin" || item.content_type === "failure";
const isExternal = item.content_type === "external";
return (
@@ -676,6 +683,7 @@ export default function ReviewQueue() {
>
<option value="">All types</option>
<option value="builtin">Built-in</option>
+ <option value="failure">Failure case studies</option>
<option value="external">Reference library</option>
</select>
</div>
@@ -854,7 +862,7 @@ function ItemRow({
<div className="bg-surface-elevated/40 border border-line rounded-lg px-4 py-3 flex items-center gap-3">
{/* Icon */}
<div className="flex-shrink-0 text-fg-subtle">
- {item.content_type === "builtin" ? (
+ {item.content_type === "builtin" || item.content_type === "failure" ? (
<svg className="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={1.5}>
<path strokeLinecap="round" strokeLinejoin="round" d="M19.5 14.25v-2.625a3.375 3.375 0 00-3.375-3.375h-1.5A1.125 1.125 0 0113.5 7.125v-1.5a3.375 3.375 0 00-3.375-3.375H8.25m0 12.75h7.5m-7.5 3H12M10.5 2.25H5.625c-.621 0-1.125.504-1.125 1.125v17.25c0 .621.504 1.125 1.125 1.125h12.75c.621 0 1.125-.504 1.125-1.125V11.25a9 9 0 00-9-9z" />
</svg>
diff --git a/packages/ui/src/lib/api.ts b/packages/ui/src/lib/api.ts
index 35db08c3..4398092a 100644
--- a/packages/ui/src/lib/api.ts
+++ b/packages/ui/src/lib/api.ts
@@ -1195,7 +1195,10 @@ export async function deleteArtifact(id: string): Promise<void> {
// ---------------------------------------------------------------------------
export type ReviewStatus = "pending" | "approved" | "rejected" | "needs_revision";
-export type ReviewContentType = "builtin" | "external";
+// `failure` = shipped or user-authored failure case studies. They have their
+// own id namespace (`failure:<domain>:<file>`) so a user upload can never
+// collide with a shipped one.
+export type ReviewContentType = "builtin" | "external" | "failure";
export type ReviewPriority = "low" | "normal" | "high";
export interface ReviewItem {