# CLAUDE.md - smartbotic-vectorapi C++20 REST API fronting `smartbotic-database` (gRPC) for n8n + other clients. Stores RAG vectors + JSON, organized into **projects** with **multi-key per-project-grant auth**. React admin UI. Ships as Debian `.deb`s. ## Build / test / run ```bash # Backend + web UI (Release) cmake -B build -G Ninja -DCMAKE_BUILD_TYPE=Release -DBUILD_WEBUI=ON -S . cmake --build build -j"$(nproc)" # Tests (GoogleTest). Integration tests need a running smartbotic-database on localhost:9004; # they GTEST_SKIP() cleanly if it's unreachable. BUILD_WEBUI=OFF to skip the npm build during test cycles. cmake -B build -G Ninja -DBUILD_TESTS=ON -DBUILD_WEBUI=OFF -S . && cmake --build build -j"$(nproc)" ctest --test-dir build --output-on-failure # Run locally (use the repo's api/ for /openapi.json,/llms.txt,/docs and the built UI) SMARTBOTIC_VECTORAPI_KEY=dev ./build/src/smartbotic-vectorapi \ --config config/config.json --share-dir "$PWD/api" --webui-dir "$PWD/build/webui/dist" # Native host build: clang, the whole repo including the runner, ~100 s, and the # compile_commands.json that clangd reads. Development only - it links the # host's protobuf/gRPC, not the versions the .deb is built against. scripts/host-build.sh --client # --client also rebuilds libsmartbotic-db-client into the prefix scripts/host-build.sh --test # + ctest, SERIALLY (see the ApiFixture note below) # Web UI only cd webui && npm install && npm run build # → webui/dist ; `npm run dev` for the :3000 proxy dev server # Local backend for UI work, or when you want Debian's exact toolchain # packaging/devstack: DB + API from the release .debs in a Debian 13 container. docker run -d --name svapi-dev -p 8080:8080 -e SMARTBOTIC_VECTORAPI_KEY=dev svapi-devstack ``` ## Architecture (where things live) - `src/` - the server. `vectorapi_core` static lib + `main.cpp`. Namespace `svapi`. Flat headers, quoted includes. - `db_gateway` (`qualify(project,coll)` + `DbGateway` over `smartbotic::database::Client`), `settings_store` (global settings **and per-project embedding overrides**, hot-reload, one subscribe stream for both), `project_settings` (`ProjectEmbeddingSettings` + the pure `resolveEmbedding()`), `outbound_http` (User-Agent + TLS defaults for every external call), `key_store` (API keys, hot-reload, bootstrap admin key), `collection_registry` (per-project metadata), `embeddings` (OpenAI), `auth` (sessions, bearer/cookie), `server` (ApiServer + auth helpers), `handlers/*` (one file per route group). - Auth model: pre-routing **authenticates** `/api/*` (401); each handler **authorizes** via `requireKey` → `requireAdmin` / `requireProjectAccess` (403). Keys are referenced by a stable **`id`** (not the secret). **AND TO THE ORIGIN ALLOW-LIST, FROM 0.15.0 - IT SKIPPED IT ENTIRELY BEFORE, AND THE REASON IS THE SHAPE THIS FILE KEEPS RECORDING: THE CHECK LIVED IN A FUNCTION THE ROUTE DOES NOT CALL.** `requireCapability` consults the allow-list; the listing authorizes through `requireProjectAccess` plus the scope filter and therefore never reached it. Measured by a consumer 2026-10-16 with their own publishable key: the listing answered **200 with the full inventory** (names, document counts, sizes, kind) from `Origin: https://evil.example.com`, while a document read with that same Origin answered 403. A publishable key ships in the browser, so "any origin holding the key" is effectively anyone - and it sat directly against the reason the scope refusal is 403 rather than 404, where we deliberately stop a scoped key discovering which collections exist outside its scope while this route handed over the whole list. Now one `requireAllowedOrigin()` is called by both, so they cannot drift. **An ABSENT Origin is refused when a list is set** (`originAllowed("")` is false for a non-empty list), and that was left IDENTICAL on purpose rather than softened for the listing: the defect was the listing behaving differently from its siblings, and a second, gentler rule would have replaced one inconsistency with another. The consequence is worth telling consumers plainly - **a key meant for server-side use must carry no allow-list**, since a non-browser client sends no Origin and can never satisfy one. That was already true of document reads, so it is not a new constraint; a test now pins both routes to the same answer for an absent Origin, which is the evidence for saying so. **THE COLLECTIONS LISTING ANSWERS TO THE SCOPE RULES AS WELL AS THE PROJECT GRANT** (2026-10-13). It used to check only `requireProjectAccess`, which never consults the rules, so a key granted exactly ONE collection could read every collection's name, document count and size in its project. The tell was internal inconsistency rather than a report: the index-values route next door authorizes through `requireCapability`, which does consult them. Three decisions in the fix are load-bearing: - **The op asked is "does ANY rule mention this collection" (`KeyScope::reachesCollection`), deliberately weaker than `findRule(name, KeyOp::List)`.** A key that may read one document there already knows the collection exists, so hiding the name buys nothing and costs the thing the listing was added for - a consumer had to hardcode four collection names into a backup script, and a collection added later was silently missed. What the listing must not do is enumerate collections the key cannot touch AT ALL. A `read`-only rule therefore still shows the name, which keeps `AScopedKeyMayListCollectionsButNotCreateThem` true. - **The rule is looked up DIRECTLY, not via `requireCapabilityCore` per collection, because that function also CHARGES THE RATE LIMITER.** One listing of fifty collections would have spent fifty of the key's tokens and could rate-limit a caller for a single request. The limit is charged once, for the one request it is, and a test pins that a two-per-minute key affords one listing of several collections. The "one implementation" rule is still honoured - `reachesCollection` calls the same `collMatches`; what is not reused is the throwing-and-charging wrapper, which is wrong for a filter. - **An out-of-scope collection is ABSENT, not refused**, like a disabled function being 404 on `/fn` rather than 403; and the response carries **`scope_filtered`, which is true for EVERY scoped key whether or not anything was withheld.** "Something was hidden" would leak one bit about the existence of a collection outside the caller's scope; "your view is partial" leaks nothing a holder cannot derive from their own key. It exists because an empty list would otherwise be indistinguishable from an empty project, which is this file's most repeated trap. **CONSEQUENCE WORTH KNOWING BEFORE YOU PROBE ANYTHING: an unauthenticated request to a NONEXISTENT `/api/*` path answers 401, not 404**, because the pre-routing filter runs before routing and never consults the route table. So a 401 cannot distinguish "the route exists and I lack a key" from "there is no such route" - and reading one as "the service is alive" is a check that answers confidently about a different question. Measured during the 0.12.0 deploy: `/api/v1/healthz` gave 401 unauthenticated and **404 with a valid admin key**, because the health route is `/healthz` and is NOT under `/api/`. Probe liveness at `/healthz`, and when a path matters, send a key so the 404 can reach you. - `webui/` - React 19 + Vite + TS (strict, `erasableSyntaxOnly`) + Tailwind + Monaco. **`npx tsc --noEmit` is NOT the build check** - it passed on a malformed import that `npm run build` (`tsc -b && vite build`) rejected, because the project build uses different settings. Verify a UI change with `npm run build`. - **A `Functions` settings tab exists and is ADMIN-ONLY**, matching its route (`requireAdmin`, not `requireProjectManage`): it bounds what one project may take from a runner every other project shares. It carries the per-kind wall caps and `network_egress`, both of which were previously reachable only by curl - and a control nobody can see is a control nobody knows exists, which mattered most for egress, where an operator could not tell whether a project's functions had outbound access. Egress renders THREE states, because `network_egress` is the effective value and `network_egress_set` says whether it was chosen or inherited: "allow (inherited)" is a different statement from "allow, set here". An empty wall-cap field means NO project cap, which is not a cap of zero, so it is held as a string to keep an empty input and a zero distinguishable all the way to the request. - **The documents filter builder DISABLES the field input for `search`**, rather than annotating it. `search` ignores the field - it is a case-insensitive substring over the whole document, id and keys included - and the three inputs read as field/op/value, which asserts a scope it does not have. A control that cannot affect the result must not look like it can. The hint points at `regex` for a field-scoped substring, and `contains` gets its own hint because array membership is equally easy to misread as a substring. - `SaveButton` IS a `