Skip to content

feat(rag): Layer B changed-content replacement semantics - #117

Merged
dmuhoro merged 9 commits into
mainfrom
feat/s101-layer-b-replacement-semantics
Dec 1, 2026
Merged

dmuhoro merged 9 commits into
mainfrom
feat/s101-layer-b-replacement-semantics

Conversation

@dmuhoro

@dmuhoro dmuhoro commented Dec 1, 2026

Copy link
Copy Markdown
Owner

Layer B — Changed-content replacement semantics for private RAG imports

Gate-safe engineering only; no document is imported. The default storeChunks / importDocumentPlan upsert path is unchanged.

What was built

  • docs/adr/ADR-013-document-replacement-semantics.md (Proposed) — Specifies document identity (source_dir|file name), stale-id detection, write-then-delete order, failure consequences, parent-row consistency, empty-document rejection, idempotency, ownership/portal/USER_PRIVATE, persisted chunk-id handling, and non-atomic/concurrency limits.
  • scripts/rag/governedImporter.ts — New explicit replaceDocumentPlan. Writes all new chunks through storeChunks first; only after every write succeeds reads existing chunk ids and deletes stale ones via Database.governedDeleteDocumentChunks. New-write failure aborts before any delete (old chunks preserved); stale-delete failure keeps new + undeleted stale chunks and throws. Empty replacement rejected before any write.
  • src/infrastructure/database/index.ts — Database.governedDeleteDocumentChunks: requires owner + portal + document id, refuses unscoped calls and mismatched authenticated owner, de-duplicates ids, batches at most 100 per request. RLS et_owner_delete remains the backstop; no migration needed.
  • tests/utils/mockSupabase.ts — Delete tracking, failDeleteAfter, deletes array, scoped delete failure injection.
  • tests/rag/documentReplacement.test.ts — 16 tests: default path unchanged, same-plan replay, shrink, same-length change, new-write failure with preserved old chunks + retry convergence, stale-delete failure, partial batch failure + retry, [100, 20] batching, empty replacement, unlicensed/non-private refusal, all three portals, repeated-paragraph unique ids, two-user/two-document isolation, unscoped refusal, absent-id no-op, error hygiene.
  • scripts/security/rls_matrix_rest.mjs — runScopedReplacementDelete(): proves owner + portal + document + id filter shape, cross-owner denial, wrong-portal denial, already-absent no-op.

Red and green control

  • Red (temporary probe against current importDocumentPlan path): 1 failed — expected 1 row, got 4 (3 stale + 1 new). Probe not shipped.
  • Green: tests/rag/documentReplacement.test.ts 16/16; full suite 132 files / 738 tests.
  • Real stack: supabase db reset --no-seed exit 0; bash scripts/security/run_rls_matrix_local.sh → rows=251 pass=251 fail=0; unhardened negative control still reproduces defect (pass=73 fail=178).

What was NOT done (honest blockers)

  • No document imported: docs/rag-sources/ intentionally empty; no licence-cleared documents supplied.
  • No licence/rights basis or named human reviewer. Layer B import remains blocked on owner input.
  • Replacement is deliberately not atomic; concurrency not coordinated. Both limits recorded in ADR-013.
  • No OFFICIAL or COMMUNITY provenance claimed. No version bump, tag, or release.

Gates

Full local gate green: typecheck 0, lint 0 errors (129 pre-existing warnings), 132 files / 738 tests, boundaries 0, i18n 87 screens, no-raw-hex 330 files, architecture audit 0 violations, QA all systems verified, web export exported, audit 0 critical, guard:secrets passed, guard:public-env passed, guard:dist passed.

Suite ratchet moved by measurement from 131 files / 722 tests to 132 files / 738 tests.

Owner gate

Layer B real import remains blocked until the owner supplies: licence-cleared documents in docs/rag-sources/, a written licence/rights basis, and a named human reviewer.

@vercel

vercel Bot commented Dec 1, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
easytutor Ready Ready Preview Dec 1, 2026 1:32pm UTC

@dmuhoro
dmuhoro merged commit 8167247 into main Dec 1, 2026
6 checks passed

This branch was successfully deployed

1 active deployment
Preview – easytutor — fb35a330 Deployed Dec 1, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant