Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
e636103
test(ctx_checkpoint): S1 contract for access-group bindings — inert s…
GottZ Dec 5, 2026
e8df54f
test(auth): S1 contract for per-request scope narrowing — inert stubs…
GottZ Dec 6, 2026
60a5dff
feat(auth): a request narrows its key to the scopes it names — read a…
GottZ Dec 6, 2026
22d8918
feat(auth): a key can require per-request scope narrowing — scope_nar…
GottZ Dec 6, 2026
0032500
feat(store): manage get takes up to 16 ids and reads their blocks wit…
GottZ Dec 6, 2026
8e07e8f
feat(auth): keyset-provision creates the scopes and keys of a set in …
GottZ Dec 6, 2026
9ae6374
Merge branch 'ab/c4' into feat/access-binding-server
GottZ Dec 6, 2026
94a5288
feat(auth): keyset-provision mints its keys with required scope narro…
GottZ Dec 6, 2026
788319c
Merge branch 'ab/c3' into feat/access-binding-server
GottZ Dec 6, 2026
e29f888
chore: the server branch carries no plugin file — the ctx_checkpoint …
GottZ Dec 6, 2026
6f5c057
docs(api): whoami names the overlay rate_limit_write comes from — the…
GottZ Dec 6, 2026
068f9d5
docs: the comments on rate_limit_write name the overlay of the key's …
GottZ Dec 6, 2026
aba0485
fix(store): an answer drops the guard verdict of a match outside the …
GottZ Dec 6, 2026
579d05e
test(auth): credentials minted from a key that requires narrowing car…
GottZ Dec 6, 2026
8e91a3d
test(handler): the column probe of get reads the guard redaction by i…
GottZ Dec 6, 2026
508eea7
fix(auth): keyset-provision refuses a set whose label an active key a…
GottZ Dec 6, 2026
98ea987
fix(auth): keyset-provision's label refusal sorts bytewise and is pin…
GottZ Dec 6, 2026
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
23 changes: 20 additions & 3 deletions docs/api.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ Three deliberate exceptions, unchanged:

| Command | Description |
|---------|-------------|
| `ctx keys create <label> --home <scope> [--allowed a,b] [--write a]` | Provision API key (v2.0.0: `--home` required, no default scope; admin key required since migration 052). `--write` (078, E4b) grants extra write scopes; each must be ⊆ home + allowed (else 400) |
| `ctx keys create <label> --home <scope> [--allowed a,b] [--write a] [--require-narrowing]` | Provision API key (v2.0.0: `--home` required, no default scope; admin key required since migration 052). `--write` (078, E4b) grants extra write scopes; each must be ⊆ home + allowed (else 400). `--require-narrowing` (migration 198) mints the key with `scope_narrowing: required`: it answers only requests that name their scopes (`X-Context-Read-Scopes`), `whoami` excepted; `keys list` marks such keys `narrowing=required` |
| `ctx keys [list\|delete]` | List / revoke provisioned API keys (admin key required since 052); `list` shows `write=[…]` when a key carries write scopes |
| `ctx tenant [list\|get\|create\|update\|delete]` | Tenant lifecycle (**server-admin key**). `create <slug> <name>` is the compound bootstrap: tenant + initial scope `<slug>:main` + owner key (plaintext shown once). `delete <id>` waits for the server's answer without the 120 s client timeout — the delete runs to its end on the server either way; a request that ends before the answer, or a proxy's `502`/`504`, says that the delete may still be running and the tenant stays offboarding until it ends ([operations](operations.md), issue #555). TTY: table, pipe: JSON; API failures (403/404/400) exit 1 |
| `ctx tenant grant [create\|list\|delete]` | Cross-tenant read grants (**server-admin key**): grant another tenant read access to one scope; delete revokes immediately |
Expand Down
9 changes: 6 additions & 3 deletions docs/mcp.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ The codes are the ones the HTTP API uses for the same class:
`invalid_encoding`, `scope_denied`, `size_cap`, `unknown_type`, `reserved_type`,
`parent_required`, `unknown_parent`, `unknown_supersedes`, `unknown_block`,
`supersedes_conflict`, `supersedes_not_allowed`, `reserved_category`, `provenance_protected`,
`reserved_metadata`, `unknown_action`, `constraint`, `edit_mismatch`, `rate_limit`, `write_conflict`, `internal`
`reserved_metadata`, `unknown_action`, `constraint`, `edit_mismatch`, `rate_limit`, `write_conflict`, `internal`;
the HTTP answers `scope_narrowing_invalid`, `scope_narrowing_route` and `scope_narrowing_required` belong to a request's
scope narrowing, which `/mcp` refuses as a whole (and a key that requires one gets no `/mcp` at all) ([api](api.md#scope-narrowing-per-request))
(`TestRejectClassesAreDocumented` holds this list and the one in [api](api.md) to the classes of the handler).
Other errors (an unknown id, an invalid type filter) carry text only,
so a client must not rely on a code being present. A fault of the database under a
Expand Down Expand Up @@ -314,8 +316,9 @@ non-empty `query` is an error: the full-text path answers its top matches and ha
next page.

**Content (`include_content`).** The rows are those of `POST /api/search` with
`compact: false`: `content` and `metadata` (with the guard's `guard_matched_id` dropped
when the block it names lies outside the key's read scopes, as on `get`) instead of `content_preview`
`compact: false`: `content` and `metadata` (with the guard's verdict — `guard_matched_id`,
`guard_similarity`, `guard_is_cross_scope` — dropped when the block it names lies outside the key's
read scopes, as on `get`) instead of `content_preview`
and `content_length`, through the same scope, grant and type rules, with `untrusted`
on foreign-text rows. The rows of one answer are bounded at 64 KiB of JSON, content,
metadata and escaping included: the page ends before the first row that would cross
Expand Down
19 changes: 18 additions & 1 deletion docs/multi-tenancy.md

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions docs/operations.md
Original file line number Diff line number Diff line change
Expand Up @@ -2394,6 +2394,23 @@ Issue #571. `context_model_consumers.record_bodies BOOLEAN NOT NULL DEFAULT fals

**Rolling deploy and way back.** The release before (v5.89.0) runs unchanged on a database with 197 (executed: its auth, model-key door, consumer administration and `/v1` chat-row tests over a schema with 197): its door selects `ctx_model_auth`'s columns by name and does not see the new one, its consumer writes leave the column at its default, and it serves a recording consumer as before — **without recording it** (it knows neither the switch nor the table). Run the two side by side only while no consumer records, or accept that the older process's share of that consumer's requests is not held — that is the rolling deploy; it does not carry over to the way back. Its schema-contract check reports the unknown migration as for every release ahead of it (`schema_contract: attention`; mode `enforce` refuses to start). The way back: first stop every ctxd process of a release that embeds migration 197 and keep it stopped until the steps below are done and only the older release runs — a process of 197 that keeps running fails at its model-key door once `ctx_model_auth` has its 161 shape again, and one that starts after the `_migrations` row is gone applies 197 again and undoes the way back. Then, in this order: `DROP TABLE context_llm_recordings;` (its rows go with it — read them out first); `DROP FUNCTION ctx_model_auth(TEXT, INET, BOOLEAN);` the `CREATE OR REPLACE FUNCTION ctx_model_auth` of migration 161 (section 8); the `CREATE OR REPLACE FUNCTION audit_state_model_consumer` of 161 (section 7); `DROP TRIGGER trg_model_consumers_audit_upd ON context_model_consumers;` and the `CREATE TRIGGER trg_model_consumers_audit_upd` of 161; `ALTER TABLE context_model_consumers DROP COLUMN record_bodies;` `DELETE FROM _migrations WHERE version = 197;` — `TestMigration197Idempotent` runs exactly these and compares the catalog with 196's.

### Migration 198: a key can require per-request scope narrowing

`context_api_keys.scope_narrowing TEXT NOT NULL DEFAULT 'optional' CHECK (optional | required)` — every key reads `optional` after the upgrade and behaves as before (the value names no scope: a tenant's offboarding deletes its keys with it as before); a key switched to `required` answers every request without `X-Context-Read-Scopes` with `403 scope_narrowing_required`, `GET /api/whoami` excepted ([multi-tenancy](multi-tenancy.md#required-narrowing-migration-198)). `ctx_auth_by_id` and `ctx_auth` gain the column as their last one; the audit projection `audit_state_api_key` and the trigger `trg_api_keys_audit_upd` gain it, so every switch is an `update` row of `api_key`.

**Locks.** `ADD COLUMN` with a constant default rewrites no row, but its `CHECK` reads every key once, under the `ACCESS EXCLUSIVE` lock on `context_api_keys` that lasts to the commit — while it is held every authentication waits (`ctx_auth_by_id` writes `last_used_at`). Measured with `TestMigration198LockWindow` (`CTX_M198_KEYS=100000,1000000`, a test container on the build host, warm cache): the whole file took 46 ms on 100 000 keys and 215 ms on 1 000 000 keys, where an authentication started 50 ms in answered after 167 ms. The scan grows with the number of keys, not of blocks; a key table of ten million rows would hold authentications for about two seconds by that line (not measured). `CREATE TRIGGER` and the function rebuild add no lock wait of their own. Before that, `ALTER TABLE` may have to wait for its lock behind a transaction that holds `context_api_keys` (any reader or writer of a key row): up to `lock_timeout` 3 s, and during that wait every authentication queues behind it as well — in the worst case all requests stand for 3 s plus the scan, then the file fails, the boot stops, and the next start retries. After the upgrade a switch of `scope_narrowing` applies from the next authentication: `ctx_auth_by_id` reads the column from the key row its `last_used_at` update locks, so an authentication that meets an uncommitted switch waits for it and answers the committed value (executed: `TestMigration198AuthReadsTheLockedRow`, with the switch held open on a second connection).

**Rolling deploy and way back.** A binary before 198 on a database with 198 selects `ctx_auth`'s columns by name (executed: the eleven-column statement answers at 198, `TestMigration198WayBack`), mints keys that read `optional`, and serves **every** key without the requirement — the check lives in the binary. Before rolling the binary back, count and name the keys that require narrowing:

```sql
SELECT k.id, k.label, t.slug AS tenant, k.home_scope
FROM context_api_keys k JOIN context_tenants t ON t.id = k.tenant_id
WHERE k.active AND k.scope_narrowing = 'required'
ORDER BY t.slug, k.label;
```

Each row is a key the older binary will serve with all its rights and no narrowing: deactivate it (`api-key-update {id, active: false}`; reactivate it after the upgrade again) or accept that it then reaches everything its own scope set allows. No row: the rollback opens nothing. The way back of the schema is in the header of `198_api_key_scope_narrowing.sql` and executed by `TestMigration198WayBack`; after it, and after 198 runs again, every key reads `optional` — the requirements are not restored.

### `graph_overview.csr_loader`: the rebuild's input substrate

`CTX_GRAPH_OVERVIEW_CSR_LOADER` (default `false`, hot) switches how the rebuild gets its graph into memory. It changes no result — the partition, the modularity and the intra-cluster degrees are byte-identical either way, and that identity is a gate, not a hope.
Expand Down
2 changes: 1 addition & 1 deletion docs/security.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ PGPASSWORD="$CONTEXT_DB_PASSWORD" docker compose exec -e PGPASSWORD db \

**Actions.** Keys: `create`, `update`, `revoke` (active true → false), `reactivate` (false → true), `delete`. `api-key-delete` is the soft revoke and records `revoke`; `delete` is the hard delete of a tenant's keys by `tenant-delete`, or a `psql` `DELETE`. Quotas: `create`, `update`, `delete`. Model consumers: `create`, `deactivate` (active true → false), `reactivate`, `update`, `prune`; model keys: `create`, `revoke` (`revoked_at` set), `update`, `prune`; model aliases: `create`, `update`, `delete`; model grants: `grant`, `ungrant` (a grant is only added or removed — its `consumer_id` and `alias_id` are immutable, SQLSTATE `23514`, `psql` included — so there is no `update`; a grant that goes with its alias is an `ungrant` attributed to the alias's deleter, the tenant prune removes a tenant's grants as `ungrant` rows of the offboarding); the instance identity: `rotate` (`instance_id` changed), `update` (`created_at`/`rotated_at` only), `delete` (the row removed — ctxd then refuses to start), `create` (a row inserted by hand; the migration's own creation writes no row). Deactivating a consumer revokes every live key it has and refuses new ones (a key insert into an inactive consumer fails with SQLSTATE `23514`; an insert and a deactivation running at the same time wait for each other on the consumer row, so under READ COMMITTED, which ctx uses for every write, the new key is either revoked or refused — a deactivation run by hand under `REPEATABLE READ` does not revoke a key committed after its snapshot), `psql` included, and records one `revoke` row per key; `prune` is the hard delete of a tenant's consumers and keys (offboarding) or a `psql` `DELETE`. `snapshot` rows are the state migration 160 found (one per key and quota row that had no history yet). An `UPDATE` that changes nothing audited records nothing — a re-set of the same quota, and above all the `last_used_at` write of every authenticated request: authentication never adds an audit row.

**What a row carries.** `old_value`/`new_value` hold a whitelist of columns, never the row as such. Keys: `label`, `home_scope`, `allowed_scopes`, `write_scopes`, `active`, `is_admin`, `tenant_id`, `tenant_role`, `confirm_writes`, `principal_id`. Quotas: `scope`, `daily_cost_usd`, `monthly_cost_usd`, `daily_calls`, `on_exceed`, `enabled`. Model consumers: `tenant_id`, `label`, `description`, `kind`, `tenant_scope`, `all_models`, `prompt_sensitivity`, `fair_weight`, `admission_lane`, `allowed_cidrs`, `allow_plaintext`, `active`, `peer_instance_id`, `peer_bound_at` (binding a peer and resetting the binding are identity events). Model keys: `consumer_id`, `key_hint`, `expires_at`, `revoked_at`, `rotated_from`. Model aliases: `scope`, `name`, `kind`, `role`, `sensitivity`, `policy`, `description`, `enabled` (the alias's `id` is the `entity_key`). Model grants: `consumer_id`, `alias_id`. Instance identity: `instance_id`, `created_at`, `rotated_at`. Never recorded: `instance_secret` (a write of it alone adds no row), `key_hash` of either key kind, `last_used_at` (the per-request write of knowledge-key authentication, the batched usage stamp of model keys), the `created_at`/`updated_at` bookkeeping, the actor columns (the quota's `updated_by`, the model tables' `created_by_*`/`revoked_by_*` — the actor travels in `api_key_id`, `actor_label` and `metadata`; their reset when the actor's key is deleted writes no row) and the quota's free-form `metadata`. A test pins every column of these tables as audited or excluded, and every table an audit trigger writes for as classified; a column a later migration adds stays red until someone classifies it, so a new secret cannot slip into the audit table unnoticed. A consumer's `tenant_id` and a key's `consumer_id` are immutable (a trigger refuses the change with SQLSTATE `23514`, `psql` included): a key never moves to another consumer's policy, a consumer never takes its keys into another tenant. The same holds for an alias's `id`, `name`, `kind` and `scope`: a model id keeps its meaning, its history stays under one key, and an alias never carries one tenant's releases into another (renaming is delete + create, and releases bind the alias id). A `TRUNCATE` of the alias table bypasses the row triggers and so the trail — remove aliases with `DELETE`.
**What a row carries.** `old_value`/`new_value` hold a whitelist of columns, never the row as such. Keys: `label`, `home_scope`, `allowed_scopes`, `write_scopes`, `active`, `is_admin`, `tenant_id`, `tenant_role`, `confirm_writes`, `principal_id`, `scope_narrowing` (since migration 198; older rows lack the key). Quotas: `scope`, `daily_cost_usd`, `monthly_cost_usd`, `daily_calls`, `on_exceed`, `enabled`. Model consumers: `tenant_id`, `label`, `description`, `kind`, `tenant_scope`, `all_models`, `prompt_sensitivity`, `fair_weight`, `admission_lane`, `allowed_cidrs`, `allow_plaintext`, `active`, `peer_instance_id`, `peer_bound_at` (binding a peer and resetting the binding are identity events). Model keys: `consumer_id`, `key_hint`, `expires_at`, `revoked_at`, `rotated_from`. Model aliases: `scope`, `name`, `kind`, `role`, `sensitivity`, `policy`, `description`, `enabled` (the alias's `id` is the `entity_key`). Model grants: `consumer_id`, `alias_id`. Instance identity: `instance_id`, `created_at`, `rotated_at`. Never recorded: `instance_secret` (a write of it alone adds no row), `key_hash` of either key kind, `last_used_at` (the per-request write of knowledge-key authentication, the batched usage stamp of model keys), the `created_at`/`updated_at` bookkeeping, the actor columns (the quota's `updated_by`, the model tables' `created_by_*`/`revoked_by_*` — the actor travels in `api_key_id`, `actor_label` and `metadata`; their reset when the actor's key is deleted writes no row) and the quota's free-form `metadata`. A test pins every column of these tables as audited or excluded, and every table an audit trigger writes for as classified; a column a later migration adds stays red until someone classifies it, so a new secret cannot slip into the audit table unnoticed. A consumer's `tenant_id` and a key's `consumer_id` are immutable (a trigger refuses the change with SQLSTATE `23514`, `psql` included): a key never moves to another consumer's policy, a consumer never takes its keys into another tenant. The same holds for an alias's `id`, `name`, `kind` and `scope`: a model id keeps its meaning, its history stays under one key, and an alias never carries one tenant's releases into another (renaming is delete + create, and releases bind the alias id). A `TRUNCATE` of the alias table bypasses the row triggers and so the trail — remove aliases with `DELETE`.

**Who and how.** `api_key_id` and `actor_label` name the acting key (the label as it was at write time); `metadata` adds `actor_principal`, `actor_tenant_id` and `actor_server_admin` (the reader needs them to hide server-admin actors from tenant-admins), `request_id` (the `X-Request-ID` the response carries — a correlation id, the caller's own when it sent a valid one), `tenant_id` (the tenant the key, quota, model consumer or model alias belongs to — a model key row takes it from its consumer, a model alias row from the tenant owning its scope, and a `_global` alias has none, so the operator catalog's history is server-admin-only; the filter a tenant-admin view applies), `consumer_id` on model consumer, model key and model grant rows, `alias_id` on model grant rows (a grant row takes `tenant_id` from its consumer), `confirm_sensitivity_downgrade` (`true`) on a model alias row whose writer confirmed lowering the alias's sensitivity, and `via`, the channel:

Expand Down
Loading
Loading