Skip to content

A tenant's SQL user reads its memories, conversations and request ledger through versioned, scoped views - #450

Merged
zlareb1 merged 7 commits into
mainfrom
feat/sql-surface
Nov 10, 2026
Merged

zlareb1 merged 7 commits into
mainfrom
feat/sql-surface

Conversation

@zlareb1

@zlareb1 zlareb1 commented Nov 9, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • New Feature

Changelog entry (one or two sentences a user of Loom can read without the diff; the release pull request collects these into the release notes):

A tenant can read its memories, memory history, conversations and request ledger as SQL through the read-only loom.sql_v1_* views. The ClickHouse user that reads them is made by an operator with loom sql-access grant, scoped to an org and its namespaces.

Why

Loom's memory and traces live in the operator's own ClickHouse and Postgres, but the only way to read them has been Loom's ops, including insights.query, which returns metadata and no content. A user who owns the data should be able to query it from their own SQL client, BI tool or warehouse job. They need column names they can build on, no way to read another tenant's rows, and nothing Loom itself withholds.

Design

What. Migration 090 adds five views in Loom's database, sql_v1_memories, sql_v1_memory_history, sql_v1_conversations, sql_v1_agent_events and sql_v1_usage_daily, and one table, loom.sql_access (user_name, org, namespace, granted, updated_at). loom sql-access grant|revoke|list makes the ClickHouse users.

Isolation.

  • Each view is SQL SECURITY DEFINER (definer: the user that ran the migration). A SQL user holds SELECT on the five views and no other grant, so it never reads a base table or loom.sql_access.

  • Each view admits a row only when loom.sql_access grants its (org, namespace), the org's * or the deployment's * to currentUser(). ClickHouse sets that from the login (checked: inside a definer view it is the invoker, with and without the analyzer). A login without a granted row reads nothing, Loom's own users included: fail closed.

  • A namespace classified restricted in loom.namespaces or loom.code_repos is in no view, whatever the grant, from the moment it is classified: an admitted read of a restricted namespace writes an audit row, which a view cannot. grant refuses a scope that names one, and a wildcard that reaches one only the code repo configuration classifies.

  • The users share the profile loom_sql_surface, which is readonly = 1. A statement can change only final, join_use_nulls, group_by_use_nulls and transform_null_in (CHANGEABLE_IN_READONLY); clients such as pg_clickhouse send these, and they leave a view's rows unchanged. Under readonly = 2, a statement could cut a view's own filter short with an overflow mode (max_rows_in_set with set_overflow_mode = 'break' on the restricted set), additional_table_filters under the history window, or PREWHERE under FINAL.

  • The profile holds these values CONST, whatever the server's defaults:

    • the deleted mask and patch parts on;
    • the query cache and the query condition cache off, since both key on the statement text, which every user shares;
    • the FINAL modes;
    • all eleven overflow modes set to throw;
    • query_plan_filter_push_down = 0 and enable_optimize_predicate_expression = 0;
    • allow_introspection_functions = 0 and send_logs_level = 'none'.

    It also carries insights.query's ceilings for execution time, rows read, memory, threads and concurrency, but not its result caps.

  • A caller's filter runs only on the rows a view returns. A filter that raises on a value, such as toUInt64(memory_key), would otherwise quote another tenant's value in its error.

    • On 26.10, ClickHouse plans a definer view sealed (ReadFromSealedView in EXPLAIN): the caller's filter stays above the view, while a plain subquery's filter is merged into PREWHERE.
    • The push-down pins hold this on any version, and the integration test probes it.
  • grant logs in as every user it sets a password for and drops the user if system.processes answers; it retries a replica that has not seen the new user yet. That read is refused only while select_from_system_db_requires_grant is on.

  • grant refuses Loom's own users (LOOM_CH_USER, the reader, loom_analyst, default) and any existing user not on that profile, so it never takes over or re-passwords an account it did not make. Statements that carry a password run with the query-log text cut and outside the statement capture, as the analyst's do.

Content rules.

  • A value is NULL, with value_withheld saying why, for a high row and a redacted version. In the history it is NULL for every version of a key that was ever high, since the version before the sweep labelled the row holds the same value unlabelled.
  • Rejected and tombstoned memories and deleted conversation turns are absent. Request arguments are not exposed.
  • This follows memory.list. The views read the tables the erasure saga deletes from, so they inherit erasure and the residue census covers them. The integration test runs a real erasure.request and checks.

Compatibility rule. Names carry the version. A v1 view only gains columns; a rename, removal or changed meaning ships as sql_v2_* beside it for at least one release (docs/reference/sql-surface.md).

Freshness. The views read the ClickHouse projections, so a committed memory write appears after its publication flush: about one LOOM_PG_PUBLISH_INTERVAL_MS, longer for a delivery a stopped writer left. Unlike memory.search, the views do not wait (there is no PROJECTION_BEHIND). This is documented.

Alternatives.

  • A row policy per SQL user on each view. ClickHouse ORs permissive policies, so this needs a further TO ALL EXCEPT <every surface user> policy on each view, rewritten at every grant. One missed rewrite widens a view for every other user. The scope table is one fixed filter per view, and a grant or revocation is an insert.
  • Reusing insights.query's signed policies. Those carry the scope per statement from a verified token. A SQL client has no token, only a login.
  • An op instead of a CLI command. Making users needs access management on the ClickHouse service, which is an operator's capability, and an op would add a wire surface, SDK methods and a dashboard control for something an operator runs a few times. The CLI runs as LOOM_CH_USER.

Bounded ledger reads. ClickHouse plans a definer view sealed, and push-down is pinned off, so a caller's filter never prunes the base table. The two ledger views therefore carry their own day-aligned window beside the scope filter: the 31 whole UTC days before today, plus today so far. Without it, every read would scan the scope's whole loom.agent_events, which grows with every call, until it hit the read caps. Events older than the window are not on the surface; the analytics export carries daily totals under hashed names. Reading sql_v1_usage_daily from the cost plane's rollup was considered and rejected: the rollup's high-water mark skips late rows, and its days follow the server's time zone.

Rolling upgrades. These are new objects that build N-1 never reads, and no existing table changes.

Scope and risk

  • Touches: migration 090, server/insights/{domain,store,app}/sql_*, server/cli/sql_access.py, the erasure classification (loom.sql_access is NO_MEMORY_CONTENT), the schema dump and docs.
  • scripts/ops/dump_schema.py now dumps plain views in their own section and counts them ("185 tables + 5 views + 19 materialized views"). tests/unit/schema_ddl.py reads CREATE VIEW as an object, and views() still means materialized views.
  • Left alone: Loom's write paths, insights.query, the analytics export.
  • Risk: the views read with the migration user's privileges. If that user is dropped, the views fail; they never fall back to the caller's privileges.
  • Dropping a surface user with a bare DROP USER leaves its scope rows granted. A later user of that name with SELECT on the views would read them, so the docs say to use revoke.

Compatibility, rollout and rollback

  • Wire: none. New CLI command loom sql-access (grant, revoke, list).
  • Operator steps: none to upgrade. To use the surface, run loom sql-access grant as a ClickHouse user with access management.
  • Rollback: the views, loom.sql_access and any users made stay behind; loom sql-access revoke drops a user, and DROP VIEW / DROP TABLE remove the rest.

Tests

  • tests/unit/test_sql_surface.py covers:
    • each view is definer-run;
    • the profile's readonly allowlist, pinned values and caps;
    • rows are written whole, and a rescope revokes what it drops;
    • the scope, user-name and restricted-reach refusals;
    • the CLI refuses --org with --all, --all with namespaces, and --org '*';
    • a new user gets the views alone.
  • tests/integration/test_sql_surface_live_ch.py on a private ClickHouse 26.10 and a server from source. It fails on main, which has no views.
    • Tenant A's user reads only A/ns, never B's rows. high, redacted, rejected, deleted and tombstoned-turn cases hold, in the current view and the history.
    • ACCESS_DENIED on loom.memory, loom.sql_access and loom.conversations. Loom's own user reads nothing through the views.
    • A namespace classified restricted after the grant vanishes, and a grant naming it is refused. A rescope takes the old namespace away. A real erasure.request of the user empties the memory, history and conversation views.
    • Hashes are NULL wherever values are. A history filter on the older version alone still withholds it.
    • system.query_log, system.mutations and system.processes are refused, and system.tables lists only the views.
    • A filter that raises on every other tenant's values runs on the tenant's own rows only, with both tenants in one part.
    • The ledger views bound their reads. With 100k seeded rows from another org and 100k of the grantee's own rows from two months back, each read's read_rows in system.query_log stays below 100k, and only the grantee's in-window rows come back.
    • A whole-deployment grant reads every org but a restricted namespace. A namespace restricted through loom.code_repos leaves the views.
    • These settings are refused with READONLY: apply_deleted_mask, use_query_cache, use_query_condition_cache, set_overflow_mode with max_rows_in_set, additional_table_filters and optimize_move_to_prewhere_if_final. The four harmless settings are accepted. A revoked user cannot log in. A name another ClickHouse user holds is refused.
  • make format lint typecheck, the schema, dump, CLI snapshot, classification, public-copy and doc-link unit tests: pass.

AI assistance in the code (leave one; writing this description with AI is fine):

  • AI-generated (an agent wrote most of the diff): Claude Code. Isolation behaviour was checked by hand against ClickHouse 26.10 (currentUser() in a definer view, row-level refusals), then pinned by the integration test.

  • I can explain every line of this diff and how it interacts with the rest of Loom.

Checklist

  • make format-check, make lint, make typecheck and make test-unit pass; the integration suite passes if server behaviour changed.
  • Generated files are regenerated in this PR, not by hand: the SDK op tables, docs/reference/configuration.md and the docs/reference/ tables (python scripts/codegen/docs_reference.py), the reach manifest, schemas/schema.sql (make schema-dump) and the Codex plugin copies (python3 scripts/codegen/agent_plugin_sync.py).
  • A new migration is additive and numbered after the latest on main; no merged migration is edited.
  • A behaviour change to any file a plugin ships bumps both plugin manifests' version, and no other open PR uses that version.
  • The frontend builds (npm --prefix frontend run build) and its tests pass, if frontend/ changed.
  • Docs under docs/ describe the system as it now is, and this description names any operator steps and wire changes.
  • Repository text has no dates, people, host names, internal namespaces, ticket or decision numbers; the history goes in this description (tests/unit/test_public_copy.py).
  • Commits are signed off (git commit -s), as CONTRIBUTING.md asks.

🤖 Generated with Claude Code

@zlareb1 zlareb1 changed the title A customer's SQL user reads a tenant's memories, conversations and request ledger through versioned, scoped views A tenant's SQL user reads its memories, conversations and request ledger through versioned, scoped views Nov 9, 2026
@zlareb1
zlareb1 force-pushed the feat/sql-surface branch 2 times, most recently from 6187882 to 5139c25 Compare November 9, 2026 22:04
zlareb1 and others added 4 commits November 9, 2026 22:16
…ger through versioned, scoped views

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…y, and the SQL surface's migration is 090

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…restricted-config test checks a restricted entry

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…ss keeps caught error text out of its messages

A granted name is not handed to a new owner while a SQL user could read it; the login-check retry count is marked as no setting; the app's refusals chain the cause, which the CLI prints to the operator.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
zlareb1 and others added 3 commits November 9, 2026 23:25
…ence says what a granted name holds

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
… wildcard grant reads names made later

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
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