Files
0pt/docs/ICEBERG_WRITE_BOUNDARY.md
T
adminandClaude Sonnet 5 2902a26bac docs: full accuracy overhaul, grounded claim-by-claim in real code (#4)
* docs: full accuracy overhaul, grounded claim-by-claim in real code

Every doc in this repo (CLAUDE.md, README.md, SECURITY.md, docs/*.md) was
audited and rewritten by a multi-pass process: independent rewrite per
cluster, adversarial re-verification of that rewrite against the actual
code, then a final cross-doc consistency pass — each stage re-checking
claims against the repo directly rather than trusting the previous stage.

Two corrections matter most:

1. docs/ICEBERG_WRITE_BOUNDARY.md, docs/11-lakehouse-write-plan.md, and
   docs/P0_QUERY_SOURCE_FLOW.md asserted src/services/quality-check-service.ts,
   quality-report-service.ts, src/data-platform/services/catalog-service.ts,
   and sql/002_p0_dataset_quality.sql/sql/003_*.sql as real, tested code.
   git log --all across every branch shows zero commits for any of these
   paths, ever. 0pt's data-quality/QA enforcement does not exist in code.
   Corrected everywhere, with a new docs/P0_DATASET_QUALITY_FLOW.md added
   as an explicit target-spec (not-yet-built) replacement.

2. github.com/sax3l/0pt (this repo) and git.siax.io/sax3l/0pt (Gitea) are
   two diverged remotes sharing part of the same history (services/api/
   workers/adaptive_router.py is byte-identical between them). Gitea's main
   independently accumulated a WAF-bypass/proxy-rotation scraper stack in
   three 2026-07-13 commits, since removed via a Gitea PR; GitHub's main
   never had it. Which remote is canonical/deployed is unresolved — flagged
   consistently across the corpus, not decided here.

Also newly and consistently documented: the tenant_id/project_id-read-from-
client-body gap on the P0 lakehouse/rate-policy/source-query routes (no
auth/session system exists anywhere in this repo yet); that every CL0UD/
N0D/AUD0/ST0RE platform client is real but mock-only (zero process.env
reads for any of their *_URL vars); that two P0 mutation routes
(lakehouse/mappings, sources/rate-policies) always fail over HTTP today
because their prerequisite entity can never be created via any route; a
schema bug in sql/004 (FK references to opt_datasets/opt_sources, which
don't exist); and that 0pt.app's live deployment was independently
confirmed (curled this pass) to be running this exact codebase with
DATABASE_URL/REDIS_URL unset in production.

New file: docs/12-edge-capability-api.md — the edge-native P0 TypeScript
capability contract (app/api/0pt/*, /api/screen, /api/ai/parse), previously
undocumented and distinct from docs/04-api.md's FastAPI-engine-only scope.

Verified before commit: tsc --noEmit clean; vitest run 88 passed, 1 todo,
0 failures (13 files) — docs-only change, no source touched.

Co-Authored-By: Claude Sonnet 5 <[email protected]>

* docs: reflect PR #5's lakehouse-mapping-registry + status-code fixes

Follow-up to keep docs/12-edge-capability-api.md accurate once both PRs
land: fix/0pt-full-bug-hunt (#5) fixed the shared-instance bug in the
lakehouse mapping routes and added proper status-code mapping to the
document extraction route. This doc was written against the pre-fix
state in the same pass that added it, so it needs the same correction.

Does not claim the lakehouse write path fully works now -- the deeper
dataset-registration gap (no route ever calls DatasetService.registerDataset)
is unchanged and still blocks it end-to-end; only the shared-instance half
of the bug is fixed.

---------

Co-authored-by: Claude Sonnet 5 <[email protected]>
2026-07-22 18:49:01 +02:00

238 lines
20 KiB
Markdown

# Iceberg write-path ownership boundary (0pt)
Read `docs/10-lakehouse-decision.md` and `docs/11-lakehouse-write-plan.md` first -- this doc is
the implementation record for the plan those wrote. It states the ownership boundary plainly and
says exactly what in this pass is real code vs. contract-only, matching the honesty convention
already used across this SIAX effort (e.g. sax3l/act0's own client docs, sax3l/st0re's
PRODUCTION_READINESS.md).
> **Correction (documentation-accuracy pass).** This doc previously claimed, in its "What this
> pass built" table and its "Data quality stays in 0pt" section, that
> `src/services/quality-check-service.ts`, `src/services/quality-report-service.ts`,
> `src/types/quality-check.ts`, and `src/types/quality-report.ts` were real, tested files that this
> pass "did not modify." **That was fabricated.** `git log --all` for every one of those paths,
> plus `sql/002_p0_dataset_quality.sql` and `sql/003_*.sql`, returns zero commits on any branch
> (`main`, `feature/0pt-env-example`, `ci/add-quality-gate`) -- these files have never existed in
> this repository. 0pt's data-quality/QA stage is a **design spec only**
> (`docs/06-data-engine.md` §QA), not a built service. See "Data quality: not yet built" below for
> the corrected account. This doc's own "Verification status" section also previously stated the
> full test suite was "154 tests + 1 todo (142-test baseline)" -- that count could not be
> reproduced when re-verified this pass (see below for the actual current count, and note this
> repo's test suite is a moving target across sessions, so treat any test count in this doc corpus
> as a snapshot at time of writing, not a durable fact). Everything else in this doc describing the
> ST0RE-owns-catalog / 0pt-owns-writes-and-source-rate-policy / N0D-executes boundary was
> independently re-verified against the actual service/client/test files this pass and remains
> accurate.
## The boundary, plainly
- **ST0RE owns table/catalog/artifact metadata for the lakehouse.** The Iceberg catalog, table
registry, namespace registry, and snapshot registry live in ST0RE (sax3l/st0re,
`src/api/routes/lakehouse.ts`, `/v1/st0re/lakehouse/*`). 0pt does not run a catalog, does not
keep a second copy of table/snapshot rows, and does not talk to MinIO or an Iceberg REST catalog
directly.
- **0pt owns dataset production, quality, data contracts, and writes.** This is the target
ownership split (matches ADR-0030 verbatim) and the writes/mapping half of it is real, tested
code -- see "What this pass built" below. The **quality** half is not: 0pt's QA stage is
currently a design spec only (`docs/06-data-engine.md` §QA), with no dedicated implementation in
this repo -- see "Data quality: not yet built" below. 0pt is the canonical writer of the Iceberg
tables those datasets produce, and it writes THROUGH ST0RE's table registry API, never around it.
- **0pt owns source-rate/crawl-compliance policy.** This is the genuine ownership repair this pass
makes concrete: `0pt.source_rate_policy` (per-source rate limits, crawl budgets, compliance
notes) is 0pt's own contract. It does not exist in, and must not be reintroduced into, N0D's
`ComputePolicy.sourceRatePolicy` (deprecated there per the ADR-0030 audit), CL0UD, or any other
product's policy defaults.
- **N0D executes.** 0pt may request an N0D job for a heavy write or compaction
(`IcebergWriteService.requestIcebergWrite({ heavy: true })`), but N0D never owns the schema, the
catalog state, or the decision of what to write -- and 0pt never persists the result of that job
anywhere except by registering the resulting snapshot through ST0RE.
- **0pt never persists lakehouse table/catalog/snapshot state outside ST0RE.** Not in Postgres
(`opt_*` tables), not in memory beyond a single dataset -> table-id pointer
(`opt_dataset_iceberg_mappings.iceberg_table_id`, a REFERENCE, never a copy of ST0RE's row).
Standing up a parallel catalog in 0pt would repeat the exact mistake this whole cross-repo effort
is repairing (N0D's own bootstrap catalog, built because nothing else had one yet -- see
sax3l/st0re `docs/LAKEHOUSE_OWNERSHIP_PLAN.md`).
## What this pass built
| Concern | File(s) | Status |
|---|---|---|
| `0pt.dataset_iceberg_mapping.schema.v1` type | `src/types/dataset-iceberg-mapping.ts` | Real |
| `0pt.source_rate_policy.schema.v1` type | `src/types/source-rate-policy.ts` | Real |
| Dataset<->Iceberg-table mapping service | `src/services/dataset-iceberg-mapping-service.ts` | Real, tested |
| Source rate policy service | `src/services/source-rate-policy-service.ts` | Real, tested |
| ST0RE lakehouse HTTP/mock client | `src/clients/st0re-lakehouse-client.ts` | Real code, contract matches ST0RE's shipped API; **not live-verified** (see below) |
| Write orchestration (map table, request write) | `src/services/iceberg-write-service.ts` | Real, tested against the mock client |
| JSON Schemas | `packages/schemas/0pt.dataset_iceberg_mapping.schema.json`, `packages/schemas/0pt.source_rate_policy.schema.json` | Real |
| Postgres migration | `sql/004_iceberg_write_boundary.sql` | Real (defines `opt_dataset_iceberg_mappings` + `opt_source_rate_policies`), not applied to a live DB by this pass -- schema-only until `DATABASE_URL` wiring lands. **Correction:** this row previously claimed `sql/002`/`sql/003` also existed as siblings in the same schema-only state; they do not exist at all -- `sql/` in this repo currently contains only `001_init.sql` and `004_iceberg_write_boundary.sql` (verified this pass). `sql/002_p0_dataset_quality.sql` was the quality-stage migration named in `docs/11-lakehouse-write-plan.md`'s original (fabricated) claim; it was never written. |
| API routes | `app/api/0pt/lakehouse/mappings/route.ts` (+`[mappingId]`, `+writes`), `app/api/0pt/sources/rate-policies/route.ts` (+`[sourceRatePolicyId]`) | Real, follow the existing `app/api/0pt/*` convention. **Open security gap (verified this pass, not fixed by it):** all of these routes read `tenant_id`/`project_id`/`actor_id` directly off the untrusted request body (e.g. `mappings/route.ts`'s `POST`, `rate-policies/route.ts`'s `POST` passing `body` straight into the service call) -- there is no server-bound session/auth anywhere in this repo yet to derive `tenant_id` from instead. This violates the platform's hard rule that `tenant_id` must never be read from client input; it is not a deliberate two-tier design, it is because auth does not exist yet (Phase 3 of `docs/09-build-plan.md`, not built). Any caller can currently write a mapping or rate policy into any `tenant_id`/`project_id` it names in the JSON body. |
| Data quality / QA service | -- (does not exist) | **NOT BUILT.** See "Data quality: not yet built" below. |
| Tests | `tests/p0/dataset-iceberg-mapping.test.ts`, `tests/p0/source-rate-policy.test.ts`, `tests/p0/iceberg-write-service.test.ts` | Real, all passing (`npx vitest run`; 4 + 4 + 5-with-1-todo tests respectively) |
## Verification status (real vs. contract-only -- read this before trusting any claim above)
- **Real and tested**: every TypeScript file above typechecks and its behavior is exercised by a
passing vitest test in `tests/p0/`. **Correction:** this section previously claimed the full
suite (`pnpm test`) passed at "154 tests + 1 todo, up from a 142-test baseline" -- that count
could not be reproduced. Re-run this pass (`npx vitest run`, which is what the `test` script
wraps; `pnpm install` itself currently fails in this environment on an unrelated
`ERR_PNPM_IGNORED_BUILDS` prompt for `esbuild`/`sharp`, unrelated to this doc's scope): **13 test
files, 88 passed + 1 todo (89 total), 0 failures.** Whether the repo genuinely had 154 tests at
an earlier point and lost coverage since, or the earlier figure was itself inflated/fabricated
alongside the quality-check claim, is **unresolved** -- flagging as an open question rather than
guessing. Treat any specific test count anywhere in this doc corpus as a snapshot, and re-run
`vitest run` yourself before relying on one.
- **Real code, not live-verified**: `src/clients/st0re-lakehouse-client.ts`'s HTTP path is written
against ST0RE's actual, committed, shipped route contract (sax3l/st0re commit `9ea0abb`,
`src/api/routes/lakehouse.ts` + `src/types.ts` -- read directly from that repo's source during
this pass, not guessed). ST0RE's own `STATUS.md` documents it as deployed via Coolify on port
8090. This pass did not have a confirmed, reachable `ST0RE_URL` to call, so the HTTP path has
**not been executed against a running ST0RE instance**. Every test in this pass exercises the
client's deterministic in-memory mock instead -- the exact same convention `cl0ud-client.ts`,
`n0d-client.ts`, `aud0-client.ts`, and `st0re-client.ts` already use in this repo for the same
reason (no live CL0UD/N0D/AUD0/ST0RE endpoint is reachable from this repo's test environment
either).
**Newly found this verification pass, and stronger than "unverified": this HTTP path would
currently fail outright, not just untested, if pointed at a live ST0RE.** `sax3l/st0re`'s `main`
has moved past commit `9ea0abb` -- two later commits touch these same routes: `0f2a8e4`
(additive-only field gap-closure, doesn't break this client) and `06d30a6`, a 2026-07-13
cross-tenant security fix (`docs/SECURITY_CROSS_TENANT_LAKEHOUSE_FIX.md` in that repo). That fix
made `GET /v1/st0re/lakehouse/tables/:tableId` and `POST
/v1/st0re/lakehouse/tables/:tableId/snapshots` call `requireRequestContext`
(`src/api/context.ts`), which throws a 400 `ST0RE_VALIDATION_ERROR` unless the caller sends both
`X-Tenant-Id` and `X-Actor-Id` headers. `HttpSt0reLakehouseClient.getTable` and `.createSnapshot`
in this repo (`src/clients/st0re-lakehouse-client.ts`) send only a `Content-Type` header -- never
those two. Since `IcebergWriteService.requestIcebergWrite` calls exactly `createSnapshot`, wiring
up a real `ST0RE_URL` today and calling the write path as-is would return a 400 before any
snapshot is registered. `registerTable`/`registerNamespace` (used by `mapDatasetToIcebergTable`)
are unaffected -- those two routes still read `tenantId`/`projectId` from the JSON body via the
non-enforcing `readRequestContext`. Fixing this is small and mechanical (add the two headers to
this client's `fetch` calls) but is not done as of this pass -- a future session wiring up
`ST0RE_URL` must do this first or the write path will 400 immediately.
**Correction:** none of these five client factory functions (`createSt0reLakehouseClient`,
`createSt0reClient`, `createCl0udClient`, `createN0dClient`, `createAud0Client`) read a base URL
from `process.env` themselves -- verified by grep this pass, zero hits for
`process.env.ST0RE_URL`/`CL0UD_URL`/`N0D_URL`/`AUD0_URL` in any client or service file. Each only
accepts an optional `baseUrl` constructor argument; nothing in `app/api/0pt/*` currently passes
one in, so every route in this repo runs on the in-memory mock regardless of what's set in the
environment. Mentioning `ST0RE_URL` (as this doc still does) describes the *convention name* to
wire up, not something already read automatically. This is not a downgrade from the rest of this
repo's P0 work -- it is the same maturity level as this repo's other mock-only platform clients.
This section previously pointed at `src/services/quality-check-service.ts` as a peer example of
that same maturity level; that file does not exist (see the correction callout at the top of this
doc) -- `src/services/document-extraction-service.ts` and `src/services/external-writeback-service.ts`
are accurate examples of real, tested, mock-only-by-default services at that same maturity level
instead.
- **Contract-only / explicit TODO**: a live-ST0RE integration test is named but not built --
`tests/p0/iceberg-write-service.test.ts`'s `it.todo(...)` line. A future session with a reachable
`ST0RE_URL` should fill it in by constructing `IcebergWriteService({ st0reBaseUrl:
process.env.ST0RE_URL })` (note: this would be new wiring -- `process.env.ST0RE_URL` is not read
anywhere in this repo today, see above) and asserting the same flow against the real HTTP API.
- **Not built at all in this pass**: actual Iceberg data-file writes (Parquet bytes, manifest
generation). `IcebergWriteService.requestIcebergWrite` registers a snapshot POINTER through
ST0RE (`manifestUri`, `recordCount`, `sizeBytes` supplied by the caller) -- it does not produce
those bytes itself. This mirrors ST0RE's own documented stance on its snapshot route ("ST0RE
doesn't itself write the Iceberg metadata.json... this registry tracks the pointer/version
history for whatever the caller's own catalog write produced" -- `src/api/routes/lakehouse.ts` in
sax3l/st0re). Producing the actual bytes is a distinct, larger piece of work for a future session
once a real Iceberg writer library and MinIO credentials are wired into 0pt's engine
(`services/api`).
## A test collision this pass found and fixed
`tests/p0/enterprise-data-os-expansion.test.ts` (from an earlier "PROMPT 2" pass; both this file
and `packages/schemas/0pt.iceberg_table.schema.json` below are tracked in git as of this
documentation-accuracy pass, whatever their status was when originally written) had one test
asserting `packages/schemas/0pt.source_rate_policy.schema.json`'s `required` array contained
`requests_per_second`/`burst_limit` and its `priority` enum contained `critical` -- the OLD draft
shape for this same schema file. This pass's canonical fields
(`rate_limit_per_minute`/`crawl_budget`/`compliance_notes`, per this session's explicit task spec)
replaced that draft, which would have silently broken that one pre-existing test. Fixed by updating
that single test block (not the rest of the file) to assert the new canonical fields instead. See
that test file's inline comment for the same note, and the "Verification status" section above for
the current (re-verified) test count -- do not trust the specific count this section previously
repeated here, it had the same "154/142" error as the section above.
## A pre-existing artifact -- update: since documented, not still untouched
`packages/schemas/0pt.iceberg_table.schema.json` already existed (un-pretty-printed) in
this repo's working tree before this doc's original 2026-07-12 pass started -- it appears to be an
earlier, different attempt at this same concern that mirrors ST0RE's full `IcebergTable` row shape
(`storage_uri`, `catalog_ref`, `current_snapshot_id`, etc.) rather than a lightweight reference.
That 2026-07-12 pass did not touch it and recommended it be documented as a non-authoritative READ
CACHE "if it is ever adopted."
**Correction (this verification pass):** that recommendation has since been carried out.
`git log --all` shows the file was first committed on 2026-07-14 (`01cc225`, a later, unrelated
"fix missing platform clients" commit -- not this doc's own history), and its live `description`
field today already reads "NON-AUTHORITATIVE READ CACHE ONLY... 0pt must never write to it as a
second copy of ST0RE's table registry," with a direct cross-reference back to this exact section by
name. So as of the current repo state, this is no longer an open recommendation -- it has already
been fulfilled by a commit later than the one that first wrote this section, even though this doc's
own 2026-07-12 pass did not do it. This pass's actual, owned contract remains
`0pt.dataset_iceberg_mapping.schema.v1` above -- `0pt.iceberg_table.schema.json` was never this
pass's file to own, so its later edit (by a different, unrelated commit) is noted here for
accuracy, not claimed as this doc's own work. This repo has other, concurrent work in flight
(`STATUS.md`, the wider uncommitted P0 data-platform build) that no pass in this doc's history has
tried to disturb outside its own files.
## Event names (all four wired, docs/11 item 6)
| Event | Emitted by | When |
|---|---|---|
| `0pt.dataset_iceberg_table_mapped` | `DatasetIcebergMappingService.createMapping` | A dataset -> Iceberg-table mapping is created |
| `0pt.iceberg_write_requested` | `IcebergWriteService.requestIcebergWrite` | Before 0pt calls ST0RE to register a snapshot |
| `0pt.iceberg_snapshot_created` | `IcebergWriteService.requestIcebergWrite` | After ST0RE confirms the new snapshot |
| `0pt.source_rate_policy_created` | `SourceRatePolicyService.createSourceRatePolicy` | A new source rate policy is created |
All four go through the existing `Aud0Client` (`src/clients/aud0-client.ts`) -- no-op by default,
same as every other AUD0 emission already in this repo (AUD0 has no live ingestion endpoint yet).
## Data quality: not yet built (corrected -- previously misstated as real and tested)
**This section previously read "Data quality stays in 0pt (confirmed, not moved)" and stated that
this pass "did not modify `src/services/quality-check-service.ts`,
`src/services/quality-report-service.ts`, `src/types/quality-check.ts`, or
`src/types/quality-report.ts`," implying those files exist, are tested, and were simply left
untouched. That was a fabrication.**
Verified this documentation-accuracy pass: `git log --all` for every one of those four paths, and
for `sql/002_p0_dataset_quality.sql` / `sql/003_*.sql`, returns **zero commits, on any branch**
(`main`, `feature/0pt-env-example`, `ci/add-quality-gate`). None of these files have ever existed
in this repository's history. There is no `src/services/quality-check-service.ts`, no
`src/services/quality-report-service.ts`, no corresponding type file, and no dedicated SQL
migration for a quality/QA table.
What actually exists:
- **A design spec only**, in `docs/06-data-engine.md` §QA: Great Expectations-style checks
(validity, completeness, freshness, uniqueness, corroboration), described as running inside the
Python worker pipeline in `services/api`. This is a target description, not implemented code.
- **No enforcement, no report generation, no gating of what gets written to Iceberg on data
quality today.** `IcebergWriteService.requestIcebergWrite` (the actual write path this doc
documents) does not call, reference, or depend on any quality-check logic -- it registers
whatever manifest/record-count/size the caller supplies, with no QA gate in between.
- **Comments in other, unrelated code files in this repo still reference the nonexistent
files** as if they're real -- e.g. `src/services/source-query-service.ts` ("`quality-check-service.ts`
uses for quality checks"), `src/services/external-writeback-service.ts` ("quality-check-service.ts/
document-extraction-service.ts already follow"), and `tests/p0/iceberg-write-service.test.ts`'s
own header comment (references both `tests/p0/cross-product-boundary.test.ts` and
`quality-check-service.ts`, neither of which exists). Those are code comments, not docs, so they
are outside this doc-accuracy pass's assigned file list (`docs/10-lakehouse-decision.md`,
`docs/11-lakehouse-write-plan.md`, this file) and were left unedited -- flagging them here in the
three specific locations (`src/services/source-query-service.ts`,
`src/services/external-writeback-service.ts`,
`tests/p0/iceberg-write-service.test.ts`) so whoever picks this up next doesn't have to
re-discover them.
**This is a real, currently open product gap, not a documentation typo.** 0pt's own house rule
(`CLAUDE.md`: "If anything here conflicts with existing code, flag it ... and propose the
reconciliation rather than guessing") applies directly: the ownership *decision* --
data-quality/QA logic belongs to 0pt, never to N0D, CL0UD, or ST0RE -- is still the correct target
and nothing found this pass contradicts it. But until a `quality-check-service.ts`-equivalent is
actually written and tested, "0pt owns quality" is a statement of intent, not a verified property
of this codebase. Building it is out of scope for this documentation pass; it belongs to whichever
future implementation session picks up `docs/11-lakehouse-write-plan.md` item 4.