Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ jobs:
timeout-minutes: 45
env:
NEXT_TELEMETRY_DISABLED: "1"
# The public synthetic TheOracle eval (tests/theoracle-eval.test.ts) embeds its corpus with the pinned model that
# postinstall downloads; "required" makes a missing model fail the run instead of skipping the eval.
SENTRAPEDIA_ORACLE_EVAL: required
steps:
# LFS holds the site PNGs and the video and WebP media. The repository holds no private data (2026-11-18):
# the tests that need the TheOracle index, the catalogue or the ICD-10 list skip, and the build runs on samples.
Expand Down
30 changes: 26 additions & 4 deletions docs/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Tests import application code from `../src/`. `tests/theoracle-aadi.test.ts` rea

| Layer | Runner and command | Files | Environment |
| --- | --- | --- | --- |
| App tests | Vitest 5 (`vitest run`, the first half of `npm test`) | 42 `*.test.ts[x]` files in `tests/` and 17 in `tests/ui/` | Node by default; jsdom where a file starts with the `@vitest-environment` docblock (all of `tests/ui/`, plus a few others) |
| App tests | Vitest 5 (`vitest run`, the first half of `npm test`) | 43 `*.test.ts[x]` files in `tests/` and 17 in `tests/ui/` | Node by default; jsdom where a file starts with the `@vitest-environment` docblock (all of `tests/ui/`, plus a few others) |
| System tests | `node --test`, `npm run test:system` | Nine `tests/*.node-test.mjs` files, named one by one in `package.json` | Plain Node; some build temporary capsules, local HTTP stubs or temporary git repositories |
| Site tests | `npm --prefix apps/site test` (second half of `npm test`): builds the site, then `node --test` in `apps/site` | 8 files in `apps/site/tests/`, 13 `*.test.mjs` files in `apps/site/scripts/` | Built-in Node modules only |
| Engine tests | `npm run test:aadi` runs `scripts/test-aadi.mjs`, which starts the venv Python on `aadi/test.py`, which runs pytest on `aadi/service/tests` | 9 files | Python 3.11 venv in `.runtime/aadi/venv`, model client always a fake |
Expand All @@ -32,7 +32,7 @@ Static declaration counts (a text search for `it(`/`test(` and `def test_`, not
| --- | --- | --- |
| Gateway and AADI client | `aadi-client`, `aadi-pipeline`, `aadi-paid`, `aadi-beta-access`, `aadi-composer`, `theoracle-aadi` | `src/lib/aadi/`: the gateway as the trust boundary (origin and host checks, body size, single-flight and `maxConcurrent`, timeout, cancellation, grounding required before retrieval and inference, no inference after a no-match), the targeted therapy second step, client-side validation and identity checks, case assembly from the composer, contract and schema validation with the schema hashes, and the draft built from a result. Gateways get a fake `fetchFn` and fake retrievers, so nothing leaves the process. `aadi-paid` is named for the paid profile but is mocked |
| Evidence gate and presentation | `theoracle-evidence-gate` (the largest file), `diagnosis-presentation`, `analysis-view` | `applyTherapyGate`: dose forms (tablet and capsule counts, percent, per-kg, ranges, Unicode minus, unit stripping), cross-referenced SSOT doses, OAT abbreviations; the six-section document, doctor summary, copy and referral text, pipeline trace, and how stored results are re-gated for display |
| Retrieval | `theoracle-grounding`, `public-data`, `icd-name-match`, `guards`, `pii-parity` | Retrieval over a small index the test generates in a temporary folder with a fake embedder (two-per-document cap, dose-passage swap, privacy filter, tampered or incomplete index refused, warm-up). One test uses the real index and the local model. The public build without the database, the eval ICD name matcher, shared shape guards, and the client side of the identity parity rows |
| Retrieval | `theoracle-grounding`, `theoracle-eval`, `public-data`, `icd-name-match`, `guards`, `pii-parity` | Retrieval over a small index the test generates in a temporary folder with a fake embedder (two-per-document cap, dose-passage swap, privacy filter, tampered or incomplete index refused, warm-up). One test uses the real index and the local model. `theoracle-eval` scores retrieval and the evidence gate on a public synthetic corpus with the pinned model (see below). The public build without the database, the eval ICD name matcher, shared shape guards, and the client side of the identity parity rows |
| Closed beta and auth | `beta-auth`, `beta-auth-routes`, `beta-auth-proxy`, `beta-auth-pages.test.tsx`, `auth-headers`, `clerk-webhook`, `user-access-flow.test.tsx` | Configuration resolution, server-owned access and consent decisions, `/api/aadi` authorization, proxy behaviour and browser hardening, the CSP, the `user.deleted` webhook, and the self-service access flow, including the `/apply` waitlist page. Clerk and Next modules are replaced with `vi.mock` |
| Workspace data | `workspace`, `workspace-session`, `workspace-storage`, `workspace-fixtures`, `workflow` | The reducer, validation, restore and the unreadable-backup path, principal-owned storage keys, frozen saved workspaces, personal templates, revisions and review guards, keyboard shortcuts |
| Sync | `sync-guard`, `sync-merge`, `sync-messages`, `sync-principal`, `sync-schema` | De-identified projection before upload, add-only merge on pull, Indonesian messages, the sync principal, and the SQL migrations read as text (forced row-level security, a non-owner runtime role, the exact table set). No database is contacted |
Expand Down Expand Up @@ -71,12 +71,34 @@ Static declaration counts (a text search for `it(`/`test(` and `def test_`, not

Public code ships without the database (`docs/data.md`). Three skip mechanisms keep the suites green there:

1. Vitest: `tests/private-data.ts` exports `withCatalog` (true when the real catalog is installed) and `withIndex` (the manifest and `vectors.f32` both exist). Affected tests use `skipIf`: catalog tests in `theoracle-aadi`, `diagnosis-presentation` and `theoracle-evidence-gate`, and the real-index test in `theoracle-grounding`.
1. Vitest: `tests/private-data.ts` exports `withCatalog` (true when the real catalog is installed) and `withIndex` (the manifest and `vectors.f32` both exist). Affected tests use `skipIf`: catalog tests in `theoracle-aadi`, `diagnosis-presentation` and `theoracle-evidence-gate`, and the real-index test in `theoracle-grounding`. `theoracle-eval` needs no private data, only the public model, and runs in CI.
2. Node tests: `skip` options for the installed-index check, and the chunk-build tests when the SSOT or manifest is missing.
3. pytest: the Med Assist contract-copy test needs an environment variable pointing at that contract folder.

Never paste database passages into tests, fixtures or documents.

## Public synthetic TheOracle eval

`tests/theoracle-eval.test.ts` gives the public repository and CI a retrieval and evidence-gate eval without the private knowledge base. Its inputs are two fixtures written for it, never copied from the database: `tests/fixtures/theoracle-eval/corpus.json` (31 passages in 16 documents, all titled `SINTETIS ...`, including one off-topic document and one passage with a phone number that the privacy filter must drop) and `cases.json` (23 retrieval cases, 12 therapy-retrieval cases, 11 evidence-gate cases, the thresholds). The passages are not clinically reviewed; the eval measures retrieval and matching, not clinical correctness.

- **Layer 1, no model** (runs everywhere): every gate case through `gateTherapy` with the evidence the case declares. Statuses, evidence ids and the shown tablet strength must match.
- **Layer 2, pinned model** (runs when `theoracle/model` holds the pinned EmbeddingGemma files): `scripts/build-theoracle-index.mjs --index-dir <temp>` embeds the corpus with the production builder; the production retriever answers every case from that folder; the gate reads what was actually retrieved. In CI `SENTRAPEDIA_ORACLE_EVAL=required` turns a missing model into a failure instead of a skip.

Acceptance criteria (thresholds live in `cases.json`; raise them as scores rise, never lower one without Gaffer's word):

| Check | Criterion |
| --- | --- |
| Clinical-wording retrieval (15 cases) | gold document in the six passages for every case (recall@6 = 1), MRR >= 0.9 |
| Lay-wording retrieval (8 cases) | recall@6 = 1 |
| Therapy retrieval | the diagnosis's dose passage reaches the therapy evidence for all 12 diagnoses |
| Privacy | the phone-number passage is never returned, even for a query written to match it |
| Evidence gate | every case gets its expected status, in layer 1 with declared evidence and in layer 2 with retrieved evidence |
| Known gaps | a case marked `knownGap` runs as an expected failure (`it.fails`): green while the gap is open, red once it is fixed, so the marker is removed with the fix |

The run writes `node_modules/.cache/theoracle-eval/report.json` (per-case ranks, latency, the top similarity of on-topic and off-topic queries) and, in GitHub Actions, the summary to the job page. The off-topic similarities are recorded, not asserted: retrieval has no similarity floor yet, and these numbers are the data for choosing one.

First run with the model (GitHub Actions run 45874985015, 2026-11-26, about 24 s for the file): clinical recall@6 1 and MRR 1, lay recall@6 1 and MRR 0.938, therapy dose recall 1, every gate case as expected; the lowest top similarity of an on-topic case was 0.2496 and the highest of an off-topic query 0.1457. The thresholds above were set from that run.

## What is not tested

- **Live model calls and real engine answers.** Every model client is a fake. `aadi/service/smoke_live.py` is the manual path, costs money and needs Gaffer's approval. `eval-theoracle-pipeline.mjs` is also paid when pointed at a real server; its test uses a stub.
Expand All @@ -85,7 +107,7 @@ Never paste database passages into tests, fixtures or documents.
- **Docker.** CI builds the image but never runs it; no test starts it.
- **Live Clerk, Neon, Resend, Cloudflare, GitHub Actions.** Replaced by mocks, text checks or a JavaScript evaluation of the workflow conditions.
- **Non-Windows launcher guardian**, and macOS entirely.
- **Retrieval quality** beyond fixtures: `npm run benchmark:oracle` scores recall locally but is not part of `test:all`.
- **Retrieval quality on the real knowledge base**: `npm run benchmark:oracle` scores recall on the private index locally and is not part of `test:all`. CI scores only the synthetic corpus below, which shows regressions in the retriever, the model pin and the gate, not how well the real corpus is covered.

## Running one file

Expand Down
10 changes: 7 additions & 3 deletions scripts/build-theoracle-index.mjs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
// Embeds theoracle/index/chunks.jsonl with the pinned local model and writes vectors.f32 + manifest.json.
// Usage: node scripts/build-theoracle-index.mjs [--device cpu|dml] [--append]. Resumable after interruption.
// Usage: node scripts/build-theoracle-index.mjs [--device cpu|dml] [--append] [--index-dir <folder>]. Resumable after interruption.
// --append keeps the vectors of the existing rows and embeds only rows added to chunks.jsonl since the last build.
// --index-dir builds another folder's chunks.jsonl with the same model and code (the public synthetic eval, tests/theoracle-eval.test.ts).
import { createHash } from "node:crypto";
import { existsSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { resolve, sep } from "node:path";
import { AutoModel, AutoTokenizer, env } from "@huggingface/transformers";
import { fileSha256, indexDir, modelDir, profile } from "./theoracle-model.mjs";
import { fileSha256, indexDir as defaultIndexDir, modelDir, profile } from "./theoracle-model.mjs";

const device = process.argv.includes("--device") ? process.argv[process.argv.indexOf("--device") + 1] : "cpu";
const option = name => process.argv.includes(name) ? process.argv[process.argv.indexOf(name) + 1] : undefined;
const device = option("--device") ?? "cpu";
const indexDir = option("--index-dir") ? resolve(option("--index-dir")) + sep : defaultIndexDir;
env.allowRemoteModels = false; env.localModelPath = modelDir;
for (const [name, expected] of Object.entries(profile.files)) {
if (await fileSha256(`${modelDir}${profile.id}/${name}`) !== expected.sha256) throw new Error(`Model file ${name} does not match its pinned hash. Run node scripts/install-theoracle-model.mjs.`);
Expand Down
Loading
Loading