Repository navigation
A tenant's SQL user reads its memories, conversations and request ledger through versioned, scoped views - #450
Merged
Merged
Conversation
zlareb1
force-pushed
the
feat/sql-surface
branch
2 times, most recently
from
November 9, 2026 22:04
6187882 to
5139c25
Compare
…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
force-pushed
the
feat/sql-surface
branch
from
November 9, 2026 23:22
e7e3220 to
5125676
Compare
…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]>
Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
This was referenced Nov 10, 2026
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Changelog category (leave one):
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 withloom 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_eventsandsql_v1_usage_daily, and one table,loom.sql_access (user_name, org, namespace, granted, updated_at).loom sql-access grant|revoke|listmakes the ClickHouse users.Isolation.
Each view is
SQL SECURITY DEFINER(definer: the user that ran the migration). A SQL user holdsSELECTon the five views and no other grant, so it never reads a base table orloom.sql_access.Each view admits a row only when
loom.sql_accessgrants its(org, namespace), the org's*or the deployment's*tocurrentUser(). 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.namespacesorloom.code_reposis 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.grantrefuses 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 isreadonly = 1. A statement can change onlyfinal,join_use_nulls,group_by_use_nullsandtransform_null_in(CHANGEABLE_IN_READONLY); clients such aspg_clickhousesend these, and they leave a view's rows unchanged. Underreadonly = 2, a statement could cut a view's own filter short with an overflow mode (max_rows_in_setwithset_overflow_mode = 'break'on the restricted set),additional_table_filtersunder the history window, or PREWHERE under FINAL.The profile holds these values
CONST, whatever the server's defaults:throw;query_plan_filter_push_down = 0andenable_optimize_predicate_expression = 0;allow_introspection_functions = 0andsend_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.ReadFromSealedViewinEXPLAIN): the caller's filter stays above the view, while a plain subquery's filter is merged into PREWHERE.grantlogs in as every user it sets a password for and drops the user ifsystem.processesanswers; it retries a replica that has not seen the new user yet. That read is refused only whileselect_from_system_db_requires_grantis on.grantrefuses 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.
value_withheldsaying why, for ahighrow and a redacted version. In the history it is NULL for every version of a key that was everhigh, since the version before the sweep labelled the row holds the same value unlabelled.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 realerasure.requestand checks.Compatibility rule. Names carry the version. A
v1view only gains columns; a rename, removal or changed meaning ships assql_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. Unlikememory.search, the views do not wait (there is noPROJECTION_BEHIND). This is documented.Alternatives.
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.insights.query's signed policies. Those carry the scope per statement from a verified token. A SQL client has no token, only a login.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. Readingsql_v1_usage_dailyfrom 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
server/insights/{domain,store,app}/sql_*,server/cli/sql_access.py, the erasure classification (loom.sql_accessisNO_MEMORY_CONTENT), the schema dump and docs.scripts/ops/dump_schema.pynow dumps plain views in their own section and counts them ("185 tables + 5 views + 19 materialized views").tests/unit/schema_ddl.pyreadsCREATE VIEWas an object, andviews()still means materialized views.insights.query, the analytics export.DROP USERleaves its scope rows granted. A later user of that name withSELECTon the views would read them, so the docs say to userevoke.Compatibility, rollout and rollback
loom sql-access(grant,revoke,list).loom sql-access grantas a ClickHouse user with access management.loom.sql_accessand any users made stay behind;loom sql-access revokedrops a user, andDROP VIEW/DROP TABLEremove the rest.Tests
tests/unit/test_sql_surface.pycovers:--orgwith--all,--allwith namespaces, and--org '*';tests/integration/test_sql_surface_live_ch.pyon a private ClickHouse 26.10 and a server from source. It fails onmain, which has no views.high, redacted, rejected, deleted and tombstoned-turn cases hold, in the current view and the history.ACCESS_DENIEDonloom.memory,loom.sql_accessandloom.conversations. Loom's own user reads nothing through the views.erasure.requestof the user empties the memory, history and conversation views.system.query_log,system.mutationsandsystem.processesare refused, andsystem.tableslists only the views.read_rowsinsystem.query_logstays below 100k, and only the grantee's in-window rows come back.loom.code_reposleaves the views.apply_deleted_mask,use_query_cache,use_query_condition_cache,set_overflow_modewithmax_rows_in_set,additional_table_filtersandoptimize_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 typecheckandmake test-unitpass; the integration suite passes if server behaviour changed.docs/reference/configuration.mdand thedocs/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).main; no merged migration is edited.npm --prefix frontend run build) and its tests pass, iffrontend/changed.docs/describe the system as it now is, and this description names any operator steps and wire changes.tests/unit/test_public_copy.py).git commit -s), as CONTRIBUTING.md asks.🤖 Generated with Claude Code