From d6ebb01a46465dfd766d90e80213782ccdc69402 Mon Sep 17 00:00:00 2001 From: Aly Raffauf Date: Sun, 15 Nov 2026 13:29:40 -0500 Subject: [PATCH] feat(lint): refuse storage.ledger and the other host-only storage calls in extensions albedo_extension_raw_store now also flags storage.ledger, which hands back the shared store, and storage.host and storage.declare, which build or widen a handle. No extension calls any of them. The storage, inbox, and lint pages say SDK operations take the handle. --- docs/extensions.md | 5 +- robot-docs/extensions.md | 49 +++++++++++-------- robot-docs/gleam-lint.md | 8 +-- robot-docs/inbox.md | 10 ++-- src/albedo/harness/sdk/extension.gleam | 2 +- .../rules/extension_raw_store.gleam | 32 +++++++++--- tools/lint/test/storage_rules_test.gleam | 29 ++++++++++- 7 files changed, 93 insertions(+), 42 deletions(-) diff --git a/docs/extensions.md b/docs/extensions.md index 9005f3f09..84bad6aa5 100644 --- a/docs/extensions.md +++ b/docs/extensions.md @@ -209,9 +209,8 @@ handle. Use `extension.no_initialise` when there are no tables, and `extension.storage_initialise(initialise)` for an initializer that needs only the handle. List the tables in a `pub const tables` and pass it as `tables`. See [extension storage](../robot-docs/extensions.md#extension-storage). An extension -whose tables refer to the inbox calls -`setup.inbox.initialise(storage.ledger(setup.storage))` first, and declares the -`Inbox` capability. +whose tables refer to the inbox calls `setup.inbox.initialise(setup.storage)` +first, and declares the `Inbox` capability. List the host capabilities the extension's code reads from its contexts, such as `inbox`, `control`, or `session_state`, in a `pub const uses` of diff --git a/robot-docs/extensions.md b/robot-docs/extensions.md index f158b80db..3c0613411 100644 --- a/robot-docs/extensions.md +++ b/robot-docs/extensions.md @@ -352,18 +352,18 @@ one answers the question. The host gives each extension a [`storage.Storage`](../src/albedo/harness/sdk/storage.gleam) handle that carries -the shared store, the extension's name, and its declared tables. -`extension.storage_of` builds it. The contexts an extension receives carry the -handle in a `storage` field, and none carries the raw store: `Setup`, `Startup`, -`Daemon`, `Preparation`, `ModelContext`, the tool `Context`, `host.Context`, -`compaction.Context`, `web_search.Query`, and `transfer.BindingsContext`. -Callbacks that touch the store receive the handle too: `ManagedPlugin` and -`StartedManaged`, `GlancePlugin`, `SchedulePlugin`, `DataMigration`, -`FoldPlugin`, and `extension.storage_initialise`. No plugin variant hands an -extension the raw store. Callbacks that receive a connection, such as -`CleanPlugin`, `SchemaMigration`, `FollowPlugin`, and `Due.advance`, need no -handle. The transfer adapter's `disarm`, `occurrences`, and `refuse` take the -handle. +the shared store, the extension's name, its declared tables, and its declared +capabilities. `extension.storage_of` builds it. The contexts an extension +receives carry the handle in a `storage` field, and none carries the raw store: +`Setup`, `Startup`, `Daemon`, `Preparation`, `ModelContext`, the tool `Context`, +`host.Context`, `compaction.Context`, `web_search.Query`, and +`transfer.BindingsContext`. Callbacks that touch the store receive the handle +too: `ManagedPlugin` and `StartedManaged`, `GlancePlugin`, `SchedulePlugin`, +`DataMigration`, `FoldPlugin`, and `extension.storage_initialise`. No plugin +variant hands an extension the raw store. Callbacks that receive a connection, +such as `CleanPlugin`, `SchemaMigration`, `FollowPlugin`, and `Due.advance`, +need no handle. The transfer adapter's `disarm`, `occurrences`, and `refuse` +take the handle. When the host builds one context before it knows which extension receives it, it fills `storage` with `extension.unscoped`, which names no extension and no @@ -383,20 +383,27 @@ tables. The host replaces it before an extension's callback runs: - `extension.install` builds each extension's `Setup` and `Startup`. Run the extension's own SQL through `sdk/storage`, not `sdk/store`. Its -operations match the `sdk/store` functions of the same names. Pass -`storage.ledger(handle)` to typed core and SDK operations that take the shared -store, such as `sdk/history`, `sdk/family`, `worktree`, or an `input.Inbox` -callback. Do not call `storage.new` from an extension. Name the tables given to +operations match the `sdk/store` functions of the same names. Typed core and SDK +operations take the handle itself, such as `sdk/history`, `sdk/family`, +`sdk/search`, `worktree`, `links`, `kernel/journal`, an `input.Inbox` callback, +or a `mail.Letters` read. Pass them the handle from your context. Those +operations own the core tables they touch, and the ones `capability.functions` +lists check the handle's declared capabilities +([extension capabilities](#extension-capabilities)). No extension code holds the +raw store. Core code that holds the store passes `storage.host(store)`, a handle +with no extension, no tables, and every capability. Do not call `storage.new`, +`storage.host`, or `storage.ledger` from an extension. Name the tables given to `storage.forget_session` and `storage.add_columns` as string literals so the lint can read them. -Nothing checks the scope at run time. The connection a callback receives is the -shared writer's or a reader's. Two lint rules enforce the contract in extension -code: +Nothing checks the table scope at run time. The connection a callback receives +is the shared writer's or a reader's. Two lint rules enforce the contract in +extension code: - `albedo_extension_raw_store` flags an import of `albedo/harness/sdk/store` and - any use of `storage.new` or `storage.grant`. It also flags `storage.owned_by` - outside a public feature module. + any use of `storage.new`, `storage.host`, `storage.grant`, `storage.declare`, + or `storage.ledger`. It also flags `storage.owned_by` outside a public feature + module. - `albedo_table_ownership` flags SQL that names another extension's created table. It also flags SQL that names a table the extension does not declare, including one it creates. SQLite's `sqlite_` schema tables, `pragma_` diff --git a/robot-docs/gleam-lint.md b/robot-docs/gleam-lint.md index d007a93c5..f28447397 100644 --- a/robot-docs/gleam-lint.md +++ b/robot-docs/gleam-lint.md @@ -70,9 +70,11 @@ profile. [`albedo_extension_raw_store`](../tools/lint/src/albedo_lint/rules/extension_raw_store.gleam) reads modules under `src/albedo/harness/extensions`. It flags an import of -`albedo/harness/sdk/store`. It also flags `new` and `grant` from -`albedo/harness/sdk/storage`, qualified under any alias or imported unqualified. -It flags `owned_by` from that module outside the public feature modules that +`albedo/harness/sdk/store`. It also flags the host-only functions of +`albedo/harness/sdk/storage`: `new`, `host`, `grant`, `declare`, and `ledger`, +which returns the shared store. It finds them qualified under any alias, +imported unqualified, or referenced as a value. It flags `owned_by` from that +module outside the public feature modules that [`extension_cross_import`](../tools/lint/src/albedo_lint/rules/extension_cross_import.gleam) lists. diff --git a/robot-docs/inbox.md b/robot-docs/inbox.md index b24e307e0..cd9e33899 100644 --- a/robot-docs/inbox.md +++ b/robot-docs/inbox.md @@ -28,11 +28,11 @@ summary, image fitting, and the salvage of a cut-off reply. or a daemon module gives a session something to read. An `Input` says who it is for and from, what it is, its text and the line people see for it, when it is read, and an optional key. An extension posts it through the `input.Inbox` its -context carries, as in -`daemon.inbox.post(storage.ledger(daemon.storage), input)`. The record's `post` -stores it. Its `post_in` stores it in the caller's transaction, so it commits -with the producer's own records or not at all: a webhook delivery, a Discord -message's channel cursor, a schedule's advance. `input.note` builds a note. +context carries, as in `daemon.inbox.post(daemon.storage, input)`. The record's +`post` stores it. Its `post_in` stores it in the caller's transaction, so it +commits with the producer's own records or not at all: a webhook delivery, a +Discord message's channel cursor, a schedule's advance. `input.note` builds a +note. | From | Who | | ---------------------- | -------------------------------------------------------------- | diff --git a/src/albedo/harness/sdk/extension.gleam b/src/albedo/harness/sdk/extension.gleam index b1a991cc6..56f63b0cc 100644 --- a/src/albedo/harness/sdk/extension.gleam +++ b/src/albedo/harness/sdk/extension.gleam @@ -836,7 +836,7 @@ pub type Extension { pub type Setup { Setup( /// The daemon's inbox. An extension whose tables refer to the inbox's - /// calls `inbox.initialise(storage.ledger(storage))` first, and one that moves old rows + /// calls `inbox.initialise(storage)` first, and one that moves old rows /// into the inbox posts them with `inbox.post_in`. inbox: input.Inbox, /// The store scoped to the extension being installed. diff --git a/tools/lint/src/albedo_lint/rules/extension_raw_store.gleam b/tools/lint/src/albedo_lint/rules/extension_raw_store.gleam index 73339154b..427dff501 100644 --- a/tools/lint/src/albedo_lint/rules/extension_raw_store.gleam +++ b/tools/lint/src/albedo_lint/rules/extension_raw_store.gleam @@ -1,8 +1,11 @@ //// Extensions reach the daemon's store through their own `sdk/storage` //// handle, not the shared `sdk/store` (robot-docs/extensions.md). Only the -//// host builds or grants a handle, so `storage.new` and `storage.grant` are -//// refused in extension code, and only a public feature module declares its -//// owner with `storage.owned_by`. +//// host builds a handle, grants it feature owners, or declares its +//// capabilities, so `storage.new`, `storage.host`, `storage.grant`, and +//// `storage.declare` are refused in extension code. `storage.ledger` hands +//// back the shared store, so it is refused too: SDK operations take the +//// handle. Only a public feature module declares its owner with +//// `storage.owned_by`. import albedo_lint/index.{type Index} import albedo_lint/rules/extension_cross_import @@ -16,6 +19,9 @@ const store = "albedo/harness/sdk/store" const storage = "albedo/harness/sdk/storage" +/// The `sdk/storage` functions only the host calls. +const host_only = ["new", "host", "grant", "declare", "ledger"] + pub fn rule(index: Index) -> rule.Rule { rule.module_rule_from_fn( name: "albedo_extension_raw_store", @@ -74,16 +80,16 @@ pub fn raw_imports(module: glance.Module) -> List(glance.Span) { } /// Every use of a host-only `sdk/storage` function, qualified under any -/// alias or imported unqualified: `new` and `grant` everywhere, and -/// `owned_by` outside a public feature module. +/// alias or imported unqualified: `host_only` everywhere, and `owned_by` +/// outside a public feature module. fn constructions( module: glance.Module, source: String, public: Bool, ) -> List(rule.RuleError) { let refused = case public { - True -> ["new", "grant"] - False -> ["new", "grant", "owned_by"] + True -> host_only + False -> ["owned_by", ..host_only] } let qualifiers = list.filter_map(module.imports, fn(definition) { @@ -134,6 +140,18 @@ fn forged(function: String, location: glance.Span) -> rule.RuleError { details: "Only a public feature module (robot-docs/extensions.md) declares its owner. Declare the feature you call with extension.FeaturePlugin(feature.owner()).", location:, ) + "ledger" -> + rule.error( + message: "storage.ledger gives an extension the shared store", + details: "Pass the storage.Storage handle itself: SDK and core operations take it, and the ones capability.functions lists check its declared capabilities.", + location:, + ) + "declare" -> + rule.error( + message: "storage.declare sets a handle's capabilities outside the host", + details: "Declare capabilities in the extension's `uses`; the host records them in the handle it passes.", + location:, + ) _ -> rule.error( message: "storage." diff --git a/tools/lint/test/storage_rules_test.gleam b/tools/lint/test/storage_rules_test.gleam index c77f8887d..214f6f60a 100644 --- a/tools/lint/test/storage_rules_test.gleam +++ b/tools/lint/test/storage_rules_test.gleam @@ -1,7 +1,8 @@ //// Parses fixture extensions to check the storage handle and table //// declaration rules: SQL text recognition, declared tables read from //// `pub const tables`, core reads declared in `pub const reads`, -//// and `storage.new` refusal. +//// and the refusal of host-only `sdk/storage` functions such as `storage.new` +//// and `storage.ledger`. import albedo_lint/index import albedo_lint/rules/extension_raw_store @@ -353,7 +354,31 @@ pub fn main() { extension_raw_store.rule, "alpha/raw.gleam", "import albedo/harness/sdk/storage\npub fn go(s) { storage.ledger(s) }", - 0, + 1, + ) + // The shared store does not leak through the handle, under an alias, as + // a function reference, or imported unqualified. + check( + root, + extension_raw_store.rule, + "alpha/raw.gleam", + "import albedo/harness/sdk/storage as scoped\nimport albedo/harness/sdk/storage.{ledger}\npub fn go(s) { list.map(s, scoped.ledger) }", + 2, + ) + // Only the host builds a host handle or sets a handle's capabilities. + check( + root, + extension_raw_store.rule, + "alpha/raw.gleam", + "import albedo/harness/sdk/storage\npub fn go(s, store) { #(storage.host(store), storage.declare(s, [])) }", + 2, + ) + check( + root, + extension_raw_store.rule, + "rolling/strategy.gleam", + "import albedo/harness/sdk/storage\npub fn go(s) { storage.ledger(s) }", + 1, ) check( root, -- knotmirror 0.0.1