diff --git a/apps/albedo/test/albedo/composition/extension_capability_test.gleam b/apps/albedo/test/albedo/composition/extension_capability_test.gleam index 43010dcaf..f9484a80b 100644 --- a/apps/albedo/test/albedo/composition/extension_capability_test.gleam +++ b/apps/albedo/test/albedo/composition/extension_capability_test.gleam @@ -1,45 +1,56 @@ //// The host passes an extension only the capabilities it declares in -//// `uses`, even where it builds one context for every extension. A gate -//// that let an undeclared capability through, or denied a declared one, -//// would make the declaration meaningless or break the extension. No daemon -//// scenario reaches the gate: every shipped extension declares what it -//// uses, so only a fixture extension can be refused. +//// `uses`. A gate that let an undeclared capability through, or refused a +//// declared one, would make the declaration meaningless or break the +//// extension. No daemon scenario reaches the gate: every shipped extension +//// declares what it uses, so only a fixture extension can be refused. +//// +//// One table, written by hand from robot-docs/extensions.md ("Extension +//// capabilities"), lists every host call with the capabilities it needs: +//// each capability-carrying context field, each SDK function that checks +//// the caller's handle, each `command.StateOp` through +//// `Daemon.session_state`, a shared library's feature reads, and the +//// family and session reads that stay plain. It never reads +//// `capability.fields` or `capability.functions` for an expectation. Each +//// call runs for every capability left out of an otherwise complete `uses`, +//// for exactly what it needs, and for nothing, through a handle bound to the +//// session it reads, one bound to another session, an unbound handle, and a +//// host handle. `every_host_call_is_classified_test` fails when the SDK grows +//// a call the table has no row for. import albedo/composition/composition import albedo/composition/selection import albedo/history/conversation import albedo/kernel import albedo/kernel/base -import albedo/kernel/jobs import albedo/kernel/journal import albedo/kernel/resources +import albedo/observation/bus_queues import albedo/session/family as core_family import albedo/session/inbox import albedo/storage/core_store import albedo/storage/mail import albedo/storage/transcript_text import albedo/transfer/moves -import albedo/workspace/locations import albedo/workspace/worktree -import albedo_ext_agents/sessions as agent_sessions -import albedo_ext_mcp/extension as mcp import albedo_lcm/graph import albedo_model as types import albedo_sdk/capabilities -import albedo_sdk/capability +import albedo_sdk/capability.{type Capability} import albedo_sdk/command import albedo_sdk/extension import albedo_sdk/family -import albedo_sdk/follow import albedo_sdk/history import albedo_sdk/host +import albedo_sdk/hosts import albedo_sdk/input +import albedo_sdk/kernel as sdk_kernel import albedo_sdk/letter +import albedo_sdk/location import albedo_sdk/mail as sdk_mail import albedo_sdk/request_history as requests import albedo_sdk/search +import albedo_sdk/selection as sdk_selection import albedo_sdk/session_control -import albedo_sdk/session_identity as identity import albedo_sdk/session_info import albedo_sdk/sessions import albedo_sdk/storage @@ -47,9 +58,10 @@ import albedo_sdk/store import albedo_sdk/tool import albedo_sdk/transcript import albedo_sdk/transfer +import albedo_sdk/worktree as sdk_worktree import gleam/bit_array import gleam/bytes_tree -import gleam/dict +import gleam/dict.{type Dict} import gleam/dynamic import gleam/erlang/process import gleam/http @@ -57,524 +69,224 @@ import gleam/http/request import gleam/http/response import gleam/json import gleam/list -import gleam/option.{None, Some} +import gleam/option.{type Option, None, Some} import gleam/result -import gleam/set import gleam/string -import gleeunit/should import mist import sqlight -/// What a fixture extension saw: the stage and what its capability answered. -type Seen = - process.Subject(#(String, Result(String, String))) +/// The session a bound handle serves. Unusual names, because the running +/// marks `bus_queues` keeps are shared by every test module. +const own = "capability-own" -/// Every capability the fixture reads. -const all = [ +/// Another session, which a handle bound to `own` reads only with the +/// capability its read needs. It sorts after `own`, so a check that reads +/// only the first of several letters misses its letter. +const other = "capability-peer" + +/// Every capability, in the order `capability.Capability` declares them. +const every = [ + capability.Inbox, + capability.Mail, capability.SessionControl, capability.SessionState, + capability.Sessions, + capability.History, + capability.Correspondence, + capability.Remote, + capability.Intake, + capability.Hosts, + capability.ShellOutput, + capability.Upstream, + capability.Models, capability.Providers, + capability.Worktrees, capability.Invalidate, capability.EditorOverlay, - capability.Upstream, + capability.Selection, + capability.KernelJobs, + capability.KernelResources, ] -/// An extension whose startup, route, preparation, and service each read a -/// capability and report what it answered, declaring only `uses`. -fn reporting( - name: String, - uses: List(capability.Capability), - seen: Seen, -) -> extension.Extension { - let report = fn(stage, outcome) { - process.send(seen, #(name <> " " <> stage, outcome)) - } - let ran = fn(outcome: Result(a, String)) { - case outcome { - Ok(_) -> Ok("ran") - Error(reason) -> Error(reason) - } +/// The position of `capability` in `every`. No catch-all, so a new +/// capability fails to compile here until the table knows it. +fn position(capability: Capability) -> Int { + case capability { + capability.Inbox -> 0 + capability.Mail -> 1 + capability.SessionControl -> 2 + capability.SessionState -> 3 + capability.Sessions -> 4 + capability.History -> 5 + capability.Correspondence -> 6 + capability.Remote -> 7 + capability.Intake -> 8 + capability.Hosts -> 9 + capability.ShellOutput -> 10 + capability.Upstream -> 11 + capability.Models -> 12 + capability.Providers -> 13 + capability.Worktrees -> 14 + capability.Invalidate -> 15 + capability.EditorOverlay -> 16 + capability.Selection -> 17 + capability.KernelJobs -> 18 + capability.KernelResources -> 19 } - extension.Extension( - name, - name, - [], - [ - extension.StartupPlugin(fn(startup) { - startup.invalidate([name], [], False) - Ok([]) - }), - extension.ToolPlugin("", [], [], [ - #(name, fn(context: host.Context, _, _) { - report("control", ran(context.control(session_control.Running("s")))) - report( - "state", - ran( - context.session_state.state(command.Accounts) - |> result.map_error(command.state_error_text), - ), - ) - report( - "providers", - Ok(case context.searches { - [] -> "none" - _ -> "some" - }), - ) - Ok(json.null()) - }), - ]), - extension.OwnedManagedPlugin(fn(preparation) { - report("overlay", ran(preparation.editor_overlay())) - Ok(None) - }), - extension.ServicePlugin( - extension.Service( - fn(_, _) { extension.Admission(extension.DaemonToken, 0) }, - fn(daemon, _, _, _) { - report("upstream", ran(daemon.upstream("p", "m", "s"))) - response.new(204) |> response.set_body(mist.Bytes(bytes_tree.new())) - }, - ), - ), - ], - extension.no_initialise, - tables: [], - uses:, - ) } -fn heard(seen: Seen) -> #(String, Result(String, String)) { - let assert Ok(value) = process.receive(seen, 1000) - value +/// What a call answered: the host's value, or the gate's stand-in. A +/// stand-in that answers an error names the capability it refused; one that +/// answers an empty list or does nothing names none, `Refused("")`. +type Outcome { + Allowed + Refused(String) } -/// Extensions that share a directory declare their own capabilities: only -/// `editor-mcp` resolves an editor's overlay, so the gate denies it to -/// `mcp`. -pub fn extensions_in_one_directory_declare_their_own_capabilities_test() -> Nil { - extension.declares(mcp.configured_extension(), capability.EditorOverlay) - |> should.be_false - extension.declares(mcp.editor_extension(), capability.EditorOverlay) - |> should.be_true +fn outcome(answer: Result(a, String)) -> Outcome { + case answer { + Error("fixture does not declare " <> capability) -> Refused(capability) + _ -> Allowed + } } -pub fn the_host_denies_capabilities_an_extension_does_not_declare_test() -> Nil { - let assert Ok(ledger) = store.start(":memory:", "", core_store.tables()) - let seen = process.new_subject() - let invalidated = process.new_subject() - let assert Ok(#(installed, [])) = - extension.install( - [reporting("open", all, seen), reporting("closed", [], seen)], - [], - [], - extension.Startup( - inbox.extension_inbox(), - fn(names, _, _) { process.send(invalidated, names) }, - selection.capability([], []), - extension.unscoped(ledger), - ), - ) - // Only the extension that declares Invalidate reaches the host's callback. - process.receive(invalidated, 1000) |> should.equal(Ok(["open"])) - process.receive(invalidated, 100) |> should.equal(Error(Nil)) +/// A call that answers an `Option`, and `None` when refused. +fn some(answer: Option(a)) -> Outcome { + present(option.is_some(answer)) +} - let composed = - composition.compose( - base.contribution(), - installed, - extension.preparation_owner(process.self()), - ledger, - "s", - "/tmp", - Ok("/tmp"), - selection.capability([], []), - worktree.capability([], []), - ) - let #(stage, overlay) = heard(seen) - stage |> should.equal("open overlay") - overlay |> should.not_equal(Error("open does not declare editor_overlay")) - heard(seen) - |> should.equal(#( - "closed overlay", - Error("closed does not declare editor_overlay"), - )) - - // One context for the whole composition, as the kernel's routes get. - let context = - host.Context( - inbox.extension_inbox(), - mail.letters(), - "s", - "/tmp", - [coerce(1)], - worktree.capability([], [follower()]), - [], - fn(_) { Nil }, - fn(_, _, _) { Error("no shell output") }, - fn(_, _, _) { Error("no mail") }, - fn(_, _, _) { Ok(None) }, - fn(_, _) { Ok(None) }, - command.Context(fn(_) { Ok(json.null()) }, "s"), - fn(_) { Ok(session_control.RunningState(True)) }, - resources.capability(), - worktree.origins(), - extension.unscoped(ledger), - ) - let assert Ok(#(_, open)) = find_route(composition.routes(composed), "open") - let assert Ok(_) = open(context, "open.go", dynamic.nil()) - heard(seen) |> should.equal(#("open control", Ok("ran"))) - heard(seen) |> should.equal(#("open state", Ok("ran"))) - heard(seen) |> should.equal(#("open providers", Ok("some"))) - let assert Ok(#(_, closed)) = - find_route(composition.routes(composed), "closed") - let assert Ok(_) = closed(context, "closed.go", dynamic.nil()) - heard(seen) - |> should.equal(#( - "closed control", - Error("closed does not declare session_control"), - )) - heard(seen) - |> should.equal(#( - "closed state", - Error("closed does not declare session_state"), - )) - // A list has no failing path, so a denied provider list is empty. - heard(seen) |> should.equal(#("closed providers", Ok("none"))) - - let daemon = daemon(ledger) - let request = request.new() |> request.set_method(http.Post) - let assert Ok(service) = extension.service(installed, "open") - service.handle(daemon, [], request |> request.set_body(<<>>), live()) - heard(seen) |> should.equal(#("open upstream", Error("host upstream"))) - let assert Ok(service) = extension.service(installed, "closed") - service.handle(daemon, [], request |> request.set_body(<<>>), live()) - heard(seen) - |> should.equal(#( - "closed upstream", - Error("closed does not declare upstream"), - )) +fn in_connection( + handle: storage.Storage, + read: fn(sqlight.Connection) -> Result(a, String), +) -> Outcome { + outcome(storage.query(handle, read)) +} - composition.close(composed) - store.close(ledger) +/// A call whose stand-in answers no error: the host's answer is non-empty. +fn present(answered: Bool) -> Outcome { + case answered { + True -> Allowed + False -> Refused("") + } } -fn follower() -> fn(sqlight.Connection, follow.Change) -> Result(Nil, String) { - fn(_, _) { Ok(Nil) } +type Kind { + /// A field of a context the host gates, read through the gated context. + Field + /// An SDK function `capability.functions` or + /// `capability.session_functions` lists. + Listed + /// A `command.StateOp` run through the gated `Daemon.session_state`. + Operation + /// A shared library's read, run under its owner's handle. + Feature + /// A read that needs no capability for any session. + Plain } -fn find_route( - routes: List(host.Route), - name: String, -) -> Result(host.Route, Nil) { - list.find(routes, fn(route) { route.0 == name }) +/// A host call: `key` names it as the capability tables do, `needs` is what +/// it needs for the session its handle is bound to, and `other` what it +/// needs besides for any other session. +type Call { + Call( + key: String, + kind: Kind, + needs: List(Capability), + other: Option(Capability), + run: fn(Probe) -> Outcome, + ) } -/// A daemon whose capabilities all answer, so a refusal comes from the gate. -fn daemon(ledger: store.Store) -> extension.Daemon { - extension.Daemon( - "/tmp", - inbox.extension_inbox(), - fn(_, _, _) { Error("host upstream") }, - fn(_, _) { [] }, - fn() { fn(_, _) { [] } }, - fn() { [] }, - fn() { Ok([]) }, - fn(_, _, _) { Nil }, - fn(_, _) { Nil }, - fn(_, _) { "" }, - fn(_) { command.Context(fn(_) { Ok(json.null()) }, "s") }, - fn(_) { Ok(session_control.RunningState(True)) }, - fn() { Error(Nil) }, - jobs.capability("/tmp"), - locations.capability(), - worktree.origins(), - fn(_) { dict.from_list([#("s", ["linked"])]) }, - extension.unscoped(ledger), +/// The extension a call runs for, the handle it passes, and the session it +/// reads. +type Probe { + Probe( + extension: extension.Extension, + handle: storage.Storage, + session: String, ) } -/// The live request a buffered service is also handed. The fixture service -/// never reads it, and a test cannot open a mist connection. -fn live() -> request.Request(mist.Connection) { - request.new() |> request.set_body(coerce(Nil)) +type Variant { + /// Bound to `own`, reading `own`. + Own + /// Bound to `own`, reading `other`. + Other + /// Bound to no session, as a service's or a startup worker's handle is. + Unbound + /// The host's own handle, which may read any session. + Host } -/// The constructors `command.StateOp` declares, read from its source: each -/// line of the type's body that starts with a capital letter. -fn declared_operations() -> List(String) { - let assert Ok(source) = - read_file("../../packages/albedo_sdk/src/albedo_sdk/command.gleam") - let assert Ok(source) = bit_array.to_string(source) - let assert Ok(#(_, body)) = string.split_once(source, "pub type StateOp {\n") - let assert Ok(#(body, _)) = string.split_once(body, "\n}\n") - string.split(body, "\n") - |> list.filter_map(fn(line) { - let line = string.trim_start(line) - case string.first(line) { - Ok(first) -> - case - string.uppercase(first) == first && string.lowercase(first) != first - { - True -> - string.split(line, "(") - |> list.first - |> result.map(string.trim) - False -> Error(Nil) - } - Error(Nil) -> Error(Nil) - } +fn field( + context: String, + name: String, + needs: List(Capability), + run: fn(extension.Extension) -> Outcome, +) -> Call { + Call(context <> "." <> name, Field, needs, None, fn(probe: Probe) { + run(probe.extension) }) } -@external(erlang, "file", "read_file") -fn read_file(path: String) -> Result(BitArray, dynamic.Dynamic) +fn listed( + key: String, + needs: List(Capability), + other: Option(Capability), + run: fn(storage.Storage, String) -> Outcome, +) -> Call { + Call(key, Listed, needs, other, fn(probe: Probe) { + run(probe.handle, probe.session) + }) +} -@external(erlang, "gleam_stdlib", "identity") -fn coerce(value: a) -> b +/// A read of a session's stored history. +fn reads(key: String, run: fn(storage.Storage, String) -> Outcome) -> Call { + listed(key, [], Some(capability.History), run) +} -@external(erlang, "erlang", "tuple_to_list") -fn record_values(record: a) -> List(dynamic.Dynamic) +/// A read or change of a session's letters. +fn letters( + key: String, + needs: List(Capability), + run: fn(storage.Storage, String) -> Outcome, +) -> Call { + listed(key, needs, Some(capability.Correspondence), run) +} -/// The host gate denies exactly the fields `capability.fields` lists for -/// each context, which the lint rule also reads, so the gate, the table, -/// and the lint cannot disagree. Each context's field names are listed in -/// declaration order; a new field fails here until it is classified. The -/// one other field the gate changes is `Daemon.session_state`, which keeps -/// the session rule for the operations `capability.session_functions` -/// lists unless the extension declares every capability they need. -pub fn the_gate_denies_exactly_the_fields_the_table_lists_test() -> Nil { - let assert Ok(ledger) = store.start(":memory:", "", core_store.tables()) - let scope = extension.unscoped(ledger) - let every = - list.flatten([ - capability.fields, - capability.functions, - capability.session_functions, - ]) - |> list.map(fn(entry) { entry.2 }) - |> list.unique - let host_context = - host.Context( - inbox.extension_inbox(), - mail.letters(), - "s", - "/tmp", - [coerce(1)], - worktree.capability([], [follower()]), - [coerce(3)], - fn(_) { Nil }, - fn(_, _, _) { Error("shell") }, - fn(_, _, _) { Error("mail") }, - fn(_, _, _) { Ok(None) }, - fn(_, _) { Ok(None) }, - command.Context(fn(_) { Ok(json.null()) }, "s"), - fn(_) { Ok(session_control.RunningState(True)) }, - resources.capability(), - worktree.origins(), - scope, - ) - let daemon = daemon(ledger) - let preparation = - extension.Preparation( - extension.preparation_owner(process.self()), - mail.letters(), - "s", - "/tmp", - fn() { Ok(None) }, - fn() { Ok(1) }, - fn() { Ok(capabilities.unscoped()) }, - fn() { Ok("/tmp") }, - fn(_) { Ok(#("", [])) }, - locations.capability(), - worktree.capability([], []), - worktree.origins(), - scope, - ) - let startup = - extension.Startup( - inbox.extension_inbox(), - fn(_, _, _) { Nil }, - selection.capability([], []), - scope, - ) - let setup = extension.Setup(inbox.extension_inbox(), scope) - let bindings = - transfer.BindingsContext(["s"], fn(_) { option.Some("url") }, scope) - let activation = transfer.Activation(["s"], inbox.extension_inbox(), scope) - let invocation = - command.Invocation( - fn(_) { Ok(json.null()) }, - "s", - scope, - selection.capability([], []), - ) - let contexts = [ - #( - "host.Context", - [ - "inbox", "letters", "session", "workspace", "searches", "worktrees", - "imagegens", "progress", "read_shell_output", "send_mail", "ask_moved", - "correspondents", "session_state", "control", "kernel_resources", - "origins", "storage", - ], - record_values(host_context), - fn(fixture) { record_values(extension.gate_host(host_context, fixture)) }, - ), - #( - "extension.Daemon", - [ - "home", "inbox", "upstream", "models", "efforts", "sessions", "searches", - "invalidate", "tell_refused", "hook_gone", "session_state", "control", - "intake_port", "kernel_jobs", "hosts", "origins", "linked_summaries", - "storage", - ], - record_values(daemon), - fn(fixture) { record_values(extension.gate_daemon(daemon, fixture)) }, - ), - #( - "extension.Preparation", - [ - "owner", "letters", "session", "workspace", "editor_overlay", - "intake_port", "preferences", "project_files", "instructions", "hosts", - "worktrees", "origins", "storage", - ], - record_values(preparation), - fn(fixture) { - record_values(extension.gate_preparation(preparation, fixture)) - }, - ), - #( - "extension.Startup", - ["inbox", "invalidate", "selection", "storage"], - record_values(startup), - fn(fixture) { record_values(extension.gate_startup(startup, fixture)) }, - ), - #( - "extension.Setup", - ["inbox", "storage"], - record_values(setup), - fn(fixture) { record_values(extension.gate_setup(setup, fixture)) }, - ), - #( - "transfer.BindingsContext", - ["sessions", "intake_url", "storage"], - record_values(bindings), - fn(fixture) { record_values(extension.gate_bindings(bindings, fixture)) }, - ), - #( - "transfer.Activation", - ["sessions", "inbox", "storage"], - record_values(activation), - fn(fixture) { - record_values(extension.gate_activation(activation, fixture)) - }, - ), - #( - "command.Invocation", - ["state", "session", "storage", "selection"], - record_values(invocation), - fn(fixture) { - record_values(extension.gate_invocation(invocation, fixture)) - }, - ), - ] - list.each(contexts, fn(entry) { - let #(context, names, original, gated) = entry - let assert [_tag, ..values] = original - list.length(values) |> should.equal(list.length(names)) - let changed = fn(uses) { - let assert [_tag, ..after] = gated(fixture(uses)) - list.zip(names, list.zip(values, after)) - |> list.filter_map(fn(field) { - case field.1.0 == field.1.1 { - True -> Error(Nil) - False -> Ok(field.0) - } - }) - } - // Declaring everything changes nothing. - #(context, changed(every)) |> should.equal(#(context, [])) - list.each(every, fn(wanted) { - let ruled = case - context == "extension.Daemon" - && list.contains(capability.state_operations(), wanted) - { - True -> ["session_state"] - False -> [] - } - let expected = - list.filter_map(capability.fields, fn(field) { - case field.0 == context && field.2 == wanted { - True -> Ok(field.1) - False -> Error(Nil) - } - }) - |> list.append(ruled) - let denied = changed(list.filter(every, fn(other) { other != wanted })) - #(context, capability.name(wanted), list.sort(denied, string.compare)) - |> should.equal(#( - context, - capability.name(wanted), - list.sort(expected, string.compare), - )) - }) - }) - store.close(ledger) +/// A call that answers another session's ID, or changes its family row. +fn beyond( + key: String, + needs: List(Capability), + run: fn(storage.Storage) -> Outcome, +) -> Call { + listed(key, needs, None, fn(handle, _) { run(handle) }) } -fn fixture(uses: List(capability.Capability)) -> extension.Extension { +fn fixture(uses: List(Capability)) -> extension.Extension { extension.Extension( "fixture", "fixture", [], - [], + // A dependent of LCM, so it may call LCM's feature reads. + [extension.FeaturePlugin(graph.owner())], extension.no_initialise, tables: [], uses:, ) } -/// The SDK functions `capability.functions` lists check the handle the host -/// built for the calling extension, so holding a handle does not let an -/// extension that does not declare their capability list sessions or change -/// another session's family row. A host handle may call them all. -pub fn capability_functions_refuse_a_handle_whose_extension_does_not_declare_them_test() -> Nil { - let assert Ok(ledger) = store.start(":memory:", "", core_store.tables()) - let assert Ok(_) = conversation.initialise(ledger) - let assert Ok(_) = core_family.initialise(ledger) - let silent = extension.storage_of(fixture([]), ledger) - let declaring = - extension.storage_of( - fixture([capability.Sessions, capability.SessionControl]), - ledger, - ) - let sessions = "fixture does not declare sessions" - let control = "fixture does not declare session_control" - history.sessions(silent, "", False, False) |> should.equal(Error(sessions)) - family.children(silent, "s") |> should.equal(Error(sessions)) - family.set_role(silent, "s", "lead") |> should.equal(Error(control)) - family.set_verifier(silent, "s", "true") |> should.equal(Error(control)) - history.sessions(declaring, "", False, False) |> should.equal(Ok([])) - family.children(declaring, "s") |> should.equal(Ok([])) - family.set_role(declaring, "s", "lead") |> should.equal(Ok(Nil)) - family.set_verifier(declaring, "s", "true") |> should.equal(Ok(Nil)) - history.sessions(storage.host(ledger), "", False, False) - |> should.equal(Ok([])) - store.close(ledger) -} +// --- The seeded ledger ------------------------------------------------------- -/// Every recorded kernel is keyed by its session's ID, so the listing the -/// `kernel_resources` capability reads is gated under `Sessions` like any -/// other listing of sessions, for the handle the extension passes it. -pub fn recorded_kernels_refuse_a_handle_without_sessions_test() -> Nil { - let assert Ok(ledger) = store.start(":memory:", "", core_store.tables()) - let recorded = resources.capability().recorded - recorded(extension.storage_of(fixture([]), ledger)) - |> should.equal(Error("fixture does not declare sessions")) - recorded(extension.storage_of(fixture([capability.Sessions]), ledger)) - |> should.equal(Ok(dict.new())) - store.close(ledger) +/// The seeded store, each session's LCM parent node, a recorded worktree, +/// and where the contexts' `invalidate` and `tell_refused` report. +type World { + World( + ledger: store.Store, + parents: Dict(String, Int), + worktree: sdk_worktree.Worktree, + heard: process.Subject(String), + ) } fn session(ledger: store.Store, id: String) -> Nil { @@ -598,241 +310,27 @@ fn session(ledger: store.Store, id: String) -> Nil { Nil } -/// Turning a family name into a session ID, reading a child's parent or a -/// peer's initiator, and reading the session a worktree records each answer -/// another session's ID, so each is gated under `Sessions`. Reads that -/// answer only the named session's own data stay plain for any handle. -pub fn session_id_lookups_refuse_a_handle_without_sessions_test() -> Nil { +/// Two sessions, `own` and `other`, each with a user message naming it, an +/// uploaded image, a successful turn, a started kernel cell with a trace, +/// an LCM graph, and letters from the other: a request it was delivered, +/// filed under `group-`, a letter still waiting filed there, and +/// two waiting letters filed under nothing. `other` is `own`'s child, named +/// `scout`, and works in a recorded worktree that `own` runs in. +fn world() -> World { let assert Ok(ledger) = store.start(":memory:", "", core_store.tables()) + let host = storage.host(ledger) let assert Ok(_) = conversation.initialise(ledger) - let assert Ok(_) = core_family.initialise(ledger) - session(ledger, "p") - session(ledger, "c") - let assert Ok(_) = - store.write( - ledger, - "INSERT INTO session_family(session,parent,name,depth,created_at) VALUES(?,?,?,1,0)", - [sqlight.text("c"), sqlight.text("p"), sqlight.text("scout")], - ) - let silent = extension.storage_of(fixture([]), ledger) - let declaring = extension.storage_of(fixture([capability.Sessions]), ledger) - let refusal = "fixture does not declare sessions" - let nobody = fn(_, _) { Ok(None) } - let scout = identity.Address("c", "scout") - - family.resolve(silent, "p", "scout") |> should.equal(Error(refusal)) - family.locate(silent, "p", "scout", nobody) |> should.equal(Error(refusal)) - family.remote(silent, "p", "scout", nobody) |> should.equal(Error(refusal)) - family.get(silent, "c") |> should.equal(Error(refusal)) - family.started_by(silent, "c") |> should.equal(Error(refusal)) - worktree.list(silent, [], "/work", None, True) - |> should.equal(Error(refusal)) - worktree.get(silent, [], "/work", "one") |> should.equal(Error(refusal)) - - family.resolve(declaring, "p", "scout") |> should.equal(Ok(scout)) - family.locate(declaring, "p", "scout", nobody) - |> should.equal(Ok(identity.Here(scout))) - family.remote(declaring, "p", "scout", nobody) |> should.equal(Ok(None)) - family.get(declaring, "c") - |> should.equal(Ok(Some(identity.Member("c", "p", "scout", 1, False)))) - family.started_by(declaring, "c") |> should.equal(Ok(None)) - worktree.list(declaring, [], "/work", None, True) |> should.equal(Ok([])) - worktree.get(declaring, [], "/work", "one") - |> should.equal(Error("worktree not found: one")) - - // What a caller asks about the session it names stays plain. - family.owns(silent, "p", "c") |> should.be_true - family.closed(silent, "c") |> should.equal(Ok(False)) - family.name_of(silent, "c") |> should.equal("scout") - store.close(ledger) -} - -/// The typed reads that replaced extension SQL on core tables keep the -/// rules of the gated readers: finding a session by name, listing every -/// session, or asking whether any works in a workspace needs `Sessions`, -/// another session's workspace needs `Sessions`, its transcript and context -/// use need `History`, the letters an extension posted need `Inbox`, and -/// whom a session asked for work needs `Mail` and `Sessions`. What a caller -/// asks about the sessions it names stays plain. -pub fn typed_core_reads_keep_the_capability_rules_test() -> Nil { - let ledger = history_ledger() + let assert Ok(_) = journal.initialise(host) let assert Ok(_) = mail.initialise(ledger) let assert Ok(_) = moves.initialise(ledger) let assert Ok(_) = core_family.initialise(ledger) - let bound = fn(uses) { - extension.session_storage_of(fixture(uses), ledger, "own") - } - let silent = bound([]) - - // Discovery. - sessions.find(silent, "other") |> should.equal(denied("sessions")) - sessions.all(silent) |> should.equal(denied("sessions")) - sessions.occupied(silent, "/work") |> should.equal(denied("sessions")) - let assert Ok(Some(found)) = - sessions.find(bound([capability.Sessions]), "other") - found.names.id |> should.equal("other") - sessions.all(bound([capability.Sessions])) - |> result.map(list.length) - |> should.equal(Ok(2)) - sessions.occupied(bound([capability.Sessions]), "/work") - |> should.equal(Ok(True)) - - // A workspace: the bound session's freely, another's under `Sessions`. - sessions.describe(silent, ["own"]) - |> result.map(list.map(_, fn(summary) { summary.workspace })) - |> should.equal(Ok(["/work"])) - sessions.describe(silent, ["own", "other"]) - |> should.equal(denied("sessions")) - sessions.describe(bound([capability.Sessions]), ["other"]) - |> result.map(list.length) - |> should.equal(Ok(1)) - - // Transcript rows and context use: another session's under `History`. - let assert Ok([_, ..]) = history.seqs(silent, "own", 0, 10) - history.seqs(silent, "other", 0, 10) |> should.equal(denied("history")) - history.spans(silent, "other", [#(0, 1_000_000)]) - |> should.equal(denied("history")) - history.context_use(silent, "other") |> should.equal(denied("history")) - let assert Ok([Some(_)]) = - history.spans(bound([capability.History]), "other", [#(0, 1_000_000)]) - history.context_use(bound([capability.History]), "other") - |> should.equal(Ok(Some(session_info.ContextUse(False, None)))) - - // Letters. - let posted = fn(handle) { - storage.query(handle, fn(connection) { - sdk_mail.statuses_in(handle, connection, ["letter"]) - |> result.try(fn(_) { sdk_mail.queued_in(handle, connection, ["own"]) }) - }) - } - posted(silent) |> should.equal(denied("inbox")) - posted(bound([capability.Inbox])) |> should.equal(Ok([])) - sdk_mail.requested(bound([capability.Sessions]), "own") - |> should.equal(denied("mail")) - sdk_mail.requested(bound([capability.Mail]), "own") - |> should.equal(denied("sessions")) - sdk_mail.requested(bound([capability.Mail, capability.Sessions]), "own") - |> should.equal(Ok([])) - - // Plain facts about the sessions a caller names. - let unbound = extension.storage_of(fixture([]), ledger) - sessions.names(unbound, ["other"]) - |> result.map(list.map(_, fn(names) { names.id })) - |> should.equal(Ok(["other"])) - sessions.standing(unbound, "other") - |> should.equal(Ok(Some(session_info.Standing(False, False)))) - sessions.importing(unbound, ["own", "other"]) |> should.equal(Ok(set.new())) - store.close(ledger) -} - -/// Each `capability.session_functions` read of `session` through `handle`, -/// by name, with what it answered: the value it read, inspected, or the -/// error. -fn history_reads( - handle: storage.Storage, - session: String, -) -> List(#(String, Result(String, String))) { - let read = fn(outcome: Result(a, String)) { - result.map(outcome, string.inspect) - } - // A snapshot that names the session, past every row it holds. - let forged = transcript.Snapshot(session, 1_000_000, 0) - [ - #("history.get", read(history.get(handle, session))), - #("history.acp_marker", read(history.acp_marker(handle, session))), - #("history.snapshot", read(history.snapshot(handle, session))), - #( - "history.source_stats", - read(history.source_stats(handle, forged, forged.upper)), - ), - #( - "history.latest_user_seq", - read(history.latest_user_seq(handle, forged, 1)), - ), - #( - "history.rows_before", - read(history.rows_before(handle, forged, 1_000_000)), - ), - #( - "history.fold_sources", - read( - history.fold_sources(handle, forged, 0, 1_000_000, [], fn(acc, row) { - transcript.Continue([tool.row_text(row.entry.input), ..acc]) - }), - ), - ), - #( - "history.uploaded_image", - read(history.uploaded_image(handle, session, None, 0, 0)), - ), - #( - "tool.transcript_read", - read(tool.transcript_read(handle, session, 0, None, 0, 4000)), - ), - #( - "tool.source_page", - read(tool.source_page(handle, forged, 0, 1_000_000, "row", 0, 4000)), - ), - #("search.newest", read(search.newest(handle, session, "needle", 3))), - #( - "search.page", - read(search.page(handle, session, "needle", 0, 1_000_000, 0, 10)), - ), - #( - "search.transcript_grep", - read(search.transcript_grep( - handle, - session, - "needle", - 5, - 0, - None, - None, - False, - )), - ), - #("search.running_call", read(search.running_call(handle, session))), - #("request_history.last_turn", read(requests.last_turn(handle, session))), - #("journal.recent", read(journal.recent(handle, session, 5))), - #("journal.unfinished", read(journal.unfinished(handle, session))), - #("journal.get", read(journal.get(handle, session <> "/cell"))), - #( - "journal.saved_trace", - journal.saved_trace(handle, session <> "/cell") - |> option.to_result("no trace") - |> read, - ), - ] -} - -/// What `history_reads` answers when the gate refuses `extension` every -/// read: the refusal for each, and no trace. -fn refused_reads( - reads: List(#(String, Result(String, String))), - extension: String, -) -> List(#(String, Result(String, String))) { - list.map(reads, fn(read) { - case read.0 { - "journal.saved_trace" -> #(read.0, Error("no trace")) - name -> #(name, Error(extension <> " does not declare history")) - } - }) -} - -/// Two sessions, `own` and `other`, each with a user message naming it, an -/// uploaded image, a successful turn, and a started kernel cell with a -/// trace. -fn history_ledger() -> store.Store { - let assert Ok(ledger) = store.start(":memory:", "", core_store.tables()) - let assert Ok(_) = conversation.initialise(ledger) - let host = storage.host(ledger) - let assert Ok(_) = journal.initialise(host) - list.each(["own", "other"], fn(id) { + let assert Ok(_) = transcript_text.install(ledger) + let assert Ok(_) = worktree.initialise(host) + list.each([own, other], fn(id) { session(ledger, id) - // Each session uploads different bytes. - let data = case id { - "own" -> "aGVsbG8=" - _ -> "d29ybGQ=" + let data = case id == own { + True -> "aGVsbG8=" + False -> "d29ybGQ=" } let assert Ok(image) = types.image("image/png", data, 1, 1, 5) let assert Ok(_) = @@ -856,11 +354,7 @@ fn history_ledger() -> store.Store { store.write( ledger, "INSERT INTO cells(id,session,source,status,started) VALUES(?,?,?,'saved',1)", - [ - sqlight.text(cell), - sqlight.text(id), - sqlight.text("print('" <> id <> "')"), - ], + [sqlight.text(cell), sqlight.text(id), sqlight.text("print(1)")], ) let assert Ok(_) = journal.settle( @@ -883,47 +377,7 @@ fn history_ledger() -> store.Store { ) Nil }) - ledger -} - -/// A handle the host binds to a session reads that session's stored -/// history without a capability, and answers exactly what a host handle -/// does, but another session's only when its extension declares -/// `History`. An unbound handle, such as a service's, reads none without -/// it. A host handle reads any session. -pub fn history_reads_of_another_session_need_history_test() -> Nil { - let ledger = history_ledger() - let host = storage.host(ledger) - let bound = extension.session_storage_of(fixture([]), ledger, "own") - let declaring = - extension.session_storage_of(fixture([capability.History]), ledger, "own") - let unbound = extension.storage_of(fixture([]), ledger) - let own = history_reads(host, "own") - let other = history_reads(host, "other") - - // The seeded data answers every read, and each session's differs, so - // the comparisons below say which session a read reached. - list.filter(list.append(own, other), fn(read) { result.is_error(read.1) }) - |> should.equal([]) - list.filter(list.zip(own, other), fn(pair) { pair.0 == pair.1 }) - |> list.map(fn(pair) { pair.0.0 }) - |> should.equal(["history.acp_marker", "search.running_call"]) - history_reads(bound, "own") |> should.equal(own) - history_reads(declaring, "own") |> should.equal(own) - history_reads(declaring, "other") |> should.equal(other) - history_reads(bound, "other") |> should.equal(refused_reads(other, "fixture")) - history_reads(unbound, "own") |> should.equal(refused_reads(own, "fixture")) - store.close(ledger) -} - -/// `history_ledger` with letters in each session's inbox, from the other -/// session: a request it delivered, filed under `group-`, a letter -/// still waiting filed there, and a waiting letter filed under nothing. -fn mail_ledger() -> store.Store { - let ledger = history_ledger() - let assert Ok(_) = mail.initialise(ledger) - let assert Ok(_) = core_family.initialise(ledger) - list.each([#("own", "other"), #("other", "own")], fn(pair) { + list.each([#(own, other), #(other, own)], fn(pair) { let #(id, peer) = pair let post = fn(kind, key) { let assert Ok(True) = @@ -941,6 +395,7 @@ fn mail_ledger() -> store.Store { post(letter.RequestLetter, input.Filed(id <> "-asked", "group-" <> id)) post(letter.Message, input.Filed(id <> "-waiting", "group-" <> id)) post(letter.Message, input.Once(id <> "-loose")) + post(letter.Message, input.Once(id <> "-spare")) // Delivered after every letter the session sent back, so it owes a // reply. let assert Ok(_) = @@ -950,288 +405,776 @@ fn mail_ledger() -> store.Store { ]) Nil }) - ledger + let assert Ok(_) = + store.write( + ledger, + "INSERT INTO session_family(session,parent,name,depth,created_at) VALUES(?,?,'scout',1,0)", + [sqlight.text(other), sqlight.text(own)], + ) + let assert Ok(_) = + store.write( + ledger, + "INSERT INTO worktrees(id,name,path,workspace,repo,origin,kind,session,created_at) VALUES('repo/wt','wt','/work/wt','/work/wt','/work','/work','git',?,0)", + [sqlight.text(other)], + ) + let assert Ok(_) = + store.write(ledger, "UPDATE sessions SET cwd='/work/wt' WHERE id=?", [ + sqlight.text(own), + ]) + let assert Some(recorded) = worktree.for_workspace(host, "/work/wt") + // LCM's graph of each session: two leaves and a parent above them. + let lcm = + extension.session_storage_of(fixture([capability.History]), ledger, own) + let assert Ok(_) = graph.initialise(lcm) + let parents = + list.map([own, other], fn(id) { + let assert Ok(_) = + graph.save_leaves(lcm, id, [ + graph.Leaf(1, 1, "first of " <> id, False, None), + graph.Leaf(2, 2, "second of " <> id, False, None), + ]) + let assert Ok(leaves) = graph.frontier(lcm, id) + let assert Ok(parent) = + graph.save_parent(lcm, id, leaves, "all " <> id, False, None) + #(id, parent) + }) + |> dict.from_list + World(ledger, parents, recorded, process.new_subject()) +} + +// --- The contexts the host hands extensions --------------------------------- + +/// Whether the host's `invalidate` or `tell_refused` ran. +fn heard(world: World, what: String) -> Outcome { + present(process.receive(world.heard, 0) == Ok(what)) +} + +fn selection_answering() -> sdk_selection.Selection { + sdk_selection.Selection( + enabled_by_default: fn(_) { True }, + session_allows: fn(_, _, _) { True }, + raised_caps: fn() { ["model"] }, + raise_cap: fn(_, _) { Ok(Nil) }, + ) +} + +fn hosts_answering() -> hosts.Hosts { + hosts.Hosts( + observe: fn(_, _) { coerce(Nil) }, + exists: fn(_, _) { Ok(True) }, + workspace: fn(_) { Error(location.Unavailable("host")) }, + ) +} + +fn host_context(world: World) -> host.Context { + host.Context( + inbox.extension_inbox(), + mail.letters(), + own, + "/work", + [coerce(1)], + worktree.capability([], []), + [coerce(3)], + fn(_) { Nil }, + fn(_, _, _) { Error("no shell output") }, + fn(_, _, _) { Error("no mail") }, + fn(_, _, _) { Ok(None) }, + fn(_, _) { Ok(None) }, + command.Context(fn(_) { Ok(json.null()) }, own), + fn(_) { Ok(session_control.RunningState(True)) }, + sdk_kernel.Resources(recorded: fn(_) { Ok(dict.new()) }), + worktree.origins(), + storage.host(world.ledger), + ) +} + +/// A daemon whose capabilities all answer, so a refusal comes from the gate. +fn daemon(world: World) -> extension.Daemon { + extension.Daemon( + "/tmp", + inbox.extension_inbox(), + fn(_, _, _) { Error("host upstream") }, + fn(_, _) { ["model"] }, + fn() { fn(_, _) { [coerce(1)] } }, + fn() { [coerce(1)] }, + fn() { Ok([]) }, + fn(_, _, _) { process.send(world.heard, "invalidate") }, + fn(_, _) { process.send(world.heard, "tell_refused") }, + fn(_, _) { "host" }, + fn(session) { command.Context(fn(_) { Ok(json.null()) }, session) }, + fn(_) { Ok(session_control.RunningState(True)) }, + fn() { Ok(1) }, + sdk_kernel.Jobs(list: fn(_, _) { Ok([]) }, read: fn(_, _, _, _, _) { + Error(sdk_kernel.Unavailable("host")) + }), + hosts_answering(), + worktree.origins(), + fn(_) { dict.from_list([#(own, ["linked"])]) }, + storage.host(world.ledger), + ) +} + +fn preparation(world: World) -> extension.Preparation { + extension.Preparation( + extension.preparation_owner(process.self()), + mail.letters(), + own, + "/work", + fn() { Ok(None) }, + fn() { Ok(1) }, + fn() { Ok(capabilities.unscoped()) }, + fn() { Ok("/work") }, + fn(_) { Ok(#("", [])) }, + hosts_answering(), + worktree.capability([], []), + worktree.origins(), + storage.host(world.ledger), + ) +} + +fn startup(world: World) -> extension.Startup { + extension.Startup( + inbox.extension_inbox(), + fn(_, _, _) { process.send(world.heard, "invalidate") }, + selection_answering(), + storage.host(world.ledger), + ) +} + +fn setup(world: World) -> extension.Setup { + extension.Setup(inbox.extension_inbox(), storage.host(world.ledger)) +} + +fn bindings(world: World) -> transfer.BindingsContext { + transfer.BindingsContext( + [own], + fn(_) { Some("url") }, + storage.host(world.ledger), + ) +} + +fn activation(world: World) -> transfer.Activation { + transfer.Activation( + [own], + inbox.extension_inbox(), + storage.host(world.ledger), + ) +} + +fn invocation(world: World) -> command.Invocation { + command.Invocation( + fn(_) { Ok(json.null()) }, + own, + storage.host(world.ledger), + selection_answering(), + ) +} + +/// Each gated context: its fields in declaration order, its values, and +/// its values once the gate has seen `extension`. A new field changes the +/// count, so it fails `every_host_call_is_classified_test` until it is +/// named here, and a capability field until it has a row in `calls`. +fn contexts( + world: World, +) -> List( + #( + String, + List(String), + List(dynamic.Dynamic), + fn(extension.Extension) -> List(dynamic.Dynamic), + ), +) { + [ + #( + "host.Context", + [ + "inbox", "letters", "session", "workspace", "searches", "worktrees", + "imagegens", "progress", "read_shell_output", "send_mail", "ask_moved", + "correspondents", "session_state", "control", "kernel_resources", + "origins", "storage", + ], + record_values(host_context(world)), + fn(fixture) { + record_values(extension.gate_host(host_context(world), fixture)) + }, + ), + #( + "extension.Daemon", + [ + "home", "inbox", "upstream", "models", "efforts", "sessions", "searches", + "invalidate", "tell_refused", "hook_gone", "session_state", "control", + "intake_port", "kernel_jobs", "hosts", "origins", "linked_summaries", + "storage", + ], + record_values(daemon(world)), + fn(fixture) { + record_values(extension.gate_daemon(daemon(world), fixture)) + }, + ), + #( + "extension.Preparation", + [ + "owner", "letters", "session", "workspace", "editor_overlay", + "intake_port", "preferences", "project_files", "instructions", "hosts", + "worktrees", "origins", "storage", + ], + record_values(preparation(world)), + fn(fixture) { + record_values(extension.gate_preparation(preparation(world), fixture)) + }, + ), + #( + "extension.Startup", + ["inbox", "invalidate", "selection", "storage"], + record_values(startup(world)), + fn(fixture) { + record_values(extension.gate_startup(startup(world), fixture)) + }, + ), + #( + "extension.Setup", + ["inbox", "storage"], + record_values(setup(world)), + fn(fixture) { record_values(extension.gate_setup(setup(world), fixture)) }, + ), + #( + "transfer.BindingsContext", + ["sessions", "intake_url", "storage"], + record_values(bindings(world)), + fn(fixture) { + record_values(extension.gate_bindings(bindings(world), fixture)) + }, + ), + #( + "transfer.Activation", + ["sessions", "inbox", "storage"], + record_values(activation(world)), + fn(fixture) { + record_values(extension.gate_activation(activation(world), fixture)) + }, + ), + #( + "command.Invocation", + ["state", "session", "storage", "selection"], + record_values(invocation(world)), + fn(fixture) { + record_values(extension.gate_invocation(invocation(world), fixture)) + }, + ), + ] +} + +// --- Probes of a gated field, by its type ----------------------------------- + +fn inbox_answers(world: World) -> fn(input.Inbox) -> Outcome { + fn(inbox: input.Inbox) { + outcome(inbox.waiting(storage.host(world.ledger), own, [])) + } +} + +fn letters_answer(world: World) -> fn(sdk_mail.Letters) -> Outcome { + fn(letters: sdk_mail.Letters) { + outcome(letters.open_json(storage.host(world.ledger), own)) + } +} + +fn worktrees_answer(world: World) -> fn(sdk_worktree.Worktrees) -> Outcome { + fn(worktrees: sdk_worktree.Worktrees) { + outcome(worktrees.list(storage.host(world.ledger), "/work", None, True)) + } +} + +fn hosts_answer(reach: hosts.Hosts) -> Outcome { + case reach.exists(coerce(Nil), 1) { + Error(hosts.Unreachable(reason)) -> outcome(Error(reason)) + _ -> Allowed + } +} + +fn port_answers(port: fn() -> Result(Int, Nil)) -> Outcome { + present(result.is_ok(port())) +} + +fn state_answers(state: command.Context) -> Outcome { + state.state(command.ModelGet) + |> result.map_error(command.state_error_text) + |> outcome +} + +fn control_answers(control: session_control.Control) -> Outcome { + outcome(control(session_control.Running(own))) } -/// Each `sdk/mail` read of `session`'s letters through `handle`, and each -/// read of the `input.Inbox` and `mail.Letters` the host hands extensions, -/// by name, with what it answered. -fn mail_reads( - handle: storage.Storage, - session: String, -) -> List(#(String, Result(String, String))) { - let read = fn(outcome: Result(a, String)) { - result.map(outcome, string.inspect) - } - let sorted = fn(outcome) { - result.map( - outcome, - list.sort(_, fn(a: sdk_mail.Status, b) { string.compare(a.id, b.id) }), - ) - } - let group = "group-" <> session - storage.query(handle, fn(connection) { - [ - #( - "mail.statuses_in", - read(sdk_mail.statuses_in(handle, connection, [session <> "-asked"])), - ), - #( - "mail.queued_in", - read(sorted(sdk_mail.queued_in(handle, connection, [session]))), - ), - #( - "mail.undelivered_in", - read(sdk_mail.undelivered_in(handle, connection, group, "", 10)), - ), - #( - "mail.delivered_in", - read(sdk_mail.delivered_in(handle, connection, group, "", 10)), - ), - #("mail.requested", read(sdk_mail.requested(handle, session))), - #( - "inbox.waiting", - read( - inbox.extension_inbox().waiting(handle, session, [ - session <> "-loose", - session <> "-waiting", - ]) - |> result.map(list.sort(_, string.compare)), - ), - ), - #( - "letters.owed_requesters", - read(mail.letters().owed_requesters(handle, session)), - ), - #( - "letters.open_json", - read( - mail.letters().open_json(handle, session) - |> result.map(json.to_string), - ), - ), - ] - }) +fn selection_answers(selection: sdk_selection.Selection) -> Outcome { + outcome(selection.raise_cap("model", True)) } -/// Files `session`'s loose letter under its group through `handle`, and -/// answers how many letters the group holds undelivered afterwards, as the -/// host reads them. -fn file_loose( - ledger: store.Store, - handle: storage.Storage, - session: String, -) -> #(Result(Nil, String), Int) { - let group = "group-" <> session - let filed = - storage.query(handle, fn(connection) { - sdk_mail.file_in(handle, connection, group, [session <> "-loose"]) +// --- The table -------------------------------------------------------------- + +/// Every host call, with what it needs, from robot-docs/extensions.md. +fn calls(world: World) -> List(Call) { + list.flatten([ + field_calls(world), + function_calls(world), + list.map(operations(), operation_call(world, _)), + feature_calls(world), + plain_calls(), + ]) +} + +fn field_calls(world: World) -> List(Call) { + let on_host = fn(name, needs, run: fn(host.Context) -> Outcome) { + field("host.Context", name, needs, fn(fixture) { + run(extension.gate_host(host_context(world), fixture)) }) - let host = storage.host(ledger) - let assert Ok(page) = - storage.query(host, fn(connection) { - sdk_mail.undelivered_in(host, connection, group, "", 10) + } + let on_daemon = fn(name, needs, run: fn(extension.Daemon) -> Outcome) { + field("extension.Daemon", name, needs, fn(fixture) { + run(extension.gate_daemon(daemon(world), fixture)) + }) + } + let on_preparation = fn( + name, + needs, + run: fn(extension.Preparation) -> Outcome, + ) { + field("extension.Preparation", name, needs, fn(fixture) { + run(extension.gate_preparation(preparation(world), fixture)) }) - #(filed, list.length(page)) + } + let on_startup = fn(name, needs, run: fn(extension.Startup) -> Outcome) { + field("extension.Startup", name, needs, fn(fixture) { + run(extension.gate_startup(startup(world), fixture)) + }) + } + let inbox_answer = inbox_answers(world) + let letters_answer = letters_answer(world) + let worktrees_answer = worktrees_answer(world) + [ + on_host("inbox", [capability.Inbox], fn(c) { inbox_answer(c.inbox) }), + on_host("letters", [capability.Mail], fn(c) { letters_answer(c.letters) }), + on_host("send_mail", [capability.Mail], fn(c) { + outcome(c.send_mail(own, "text", letter.Message)) + }), + on_host("control", [capability.SessionControl], fn(c) { + control_answers(c.control) + }), + on_host("session_state", [capability.SessionState], fn(c) { + state_answers(c.session_state) + }), + on_host("correspondents", [capability.Remote], fn(c) { + outcome(c.correspondents(own, "scout")) + }), + on_host("ask_moved", [capability.Remote], fn(c) { + outcome(c.ask_moved(own, "scout", "question")) + }), + on_host("read_shell_output", [capability.ShellOutput], fn(c) { + outcome(c.read_shell_output("command", 0, 10)) + }), + on_host("searches", [capability.Providers], fn(c) { + present(c.searches != []) + }), + on_host("imagegens", [capability.Providers], fn(c) { + present(c.imagegens != []) + }), + on_host("worktrees", [capability.Worktrees, capability.Sessions], fn(c) { + worktrees_answer(c.worktrees) + }), + on_host( + "kernel_resources", + [capability.KernelResources, capability.Sessions], + fn(c) { outcome(c.kernel_resources.recorded(storage.host(world.ledger))) }, + ), + on_daemon("inbox", [capability.Inbox], fn(d) { inbox_answer(d.inbox) }), + on_daemon("upstream", [capability.Upstream], fn(d) { + outcome(d.upstream("profile", "model", own)) + }), + on_daemon("models", [capability.Models], fn(d) { + present(d.models("profile", None) != []) + }), + on_daemon("efforts", [capability.Models], fn(d) { + present(d.efforts()("profile", "model") != []) + }), + on_daemon("sessions", [capability.Sessions], fn(d) { + present(d.sessions() != []) + }), + on_daemon("linked_summaries", [capability.Sessions], fn(d) { + present(d.linked_summaries([]) != dict.new()) + }), + on_daemon("searches", [capability.Providers], fn(d) { + outcome(d.searches()) + }), + on_daemon("invalidate", [capability.Invalidate], fn(d) { + d.invalidate([], [], False) + heard(world, "invalidate") + }), + on_daemon("tell_refused", [capability.Remote], fn(d) { + d.tell_refused(own, "refused") + heard(world, "tell_refused") + }), + on_daemon("hook_gone", [capability.Remote], fn(d) { + outcome(Error(d.hook_gone("hook", None))) + }), + on_daemon("session_state", [capability.SessionState], fn(d) { + state_answers(d.session_state(own)) + }), + on_daemon("control", [capability.SessionControl], fn(d) { + control_answers(d.control) + }), + on_daemon("intake_port", [capability.Intake], fn(d) { + port_answers(d.intake_port) + }), + on_daemon("kernel_jobs", [capability.KernelJobs], fn(d) { + case d.kernel_jobs.list(own, None) { + Error(sdk_kernel.Unavailable(reason)) -> outcome(Error(reason)) + _ -> Allowed + } + }), + on_daemon("hosts", [capability.Hosts], fn(d) { hosts_answer(d.hosts) }), + on_preparation("letters", [capability.Mail], fn(p) { + letters_answer(p.letters) + }), + on_preparation("editor_overlay", [capability.EditorOverlay], fn(p) { + outcome(p.editor_overlay()) + }), + on_preparation("intake_port", [capability.Intake], fn(p) { + port_answers(p.intake_port) + }), + on_preparation("hosts", [capability.Hosts], fn(p) { hosts_answer(p.hosts) }), + on_preparation( + "worktrees", + [capability.Worktrees, capability.Sessions], + fn(p) { worktrees_answer(p.worktrees) }, + ), + on_startup("inbox", [capability.Inbox], fn(s) { inbox_answer(s.inbox) }), + on_startup("invalidate", [capability.Invalidate], fn(s) { + s.invalidate([], [], False) + heard(world, "invalidate") + }), + on_startup("selection", [capability.Selection], fn(s) { + selection_answers(s.selection) + }), + field("extension.Setup", "inbox", [capability.Inbox], fn(fixture) { + inbox_answer(extension.gate_setup(setup(world), fixture).inbox) + }), + field( + "transfer.BindingsContext", + "intake_url", + [capability.Intake], + fn(fixture) { + extension.gate_bindings(bindings(world), fixture).intake_url(own) + |> option.is_some + |> present + }, + ), + field("transfer.Activation", "inbox", [capability.Inbox], fn(fixture) { + inbox_answer(extension.gate_activation(activation(world), fixture).inbox) + }), + field( + "command.Invocation", + "selection", + [capability.Selection], + fn(fixture) { + selection_answers( + extension.gate_invocation(invocation(world), fixture).selection, + ) + }, + ), + ] } -/// The `sdk/mail` reads of a session's letters, and the inbox and letter -/// reads the host hands extensions, keep the session rule: a handle bound to -/// a session reads, files, and withdraws that session's letters with the -/// capabilities the reads need, and answers what a host handle does, but -/// another session's only when its extension declares `Correspondence`. -/// An unbound handle, such as a service's, reaches none without it. A host -/// handle reaches every session's. `History` grants no letters, and -/// `Correspondence` no transcript. -pub fn mail_reads_of_another_session_need_correspondence_test() -> Nil { - let ledger = mail_ledger() - let needs = [capability.Inbox, capability.Mail, capability.Sessions] - let host = storage.host(ledger) - let bound = extension.session_storage_of(fixture(needs), ledger, "own") - let declaring = - extension.session_storage_of( - fixture([capability.Correspondence, ..needs]), - ledger, - "own", - ) - let historian = - extension.session_storage_of( - fixture([capability.History, ..needs]), - ledger, - "own", - ) - let unbound = extension.storage_of(fixture(needs), ledger) - let own = mail_reads(host, "own") - let other = mail_reads(host, "other") - let refused = fn(reads: List(#(String, Result(String, String)))) { - list.map(reads, fn(read) { #(read.0, denied("correspondence")) }) - } +fn function_calls(world: World) -> List(Call) { + let sessions_ = [capability.Sessions] + let nobody = fn(_, _) { Ok(None) } + let wt = world.worktree + // A snapshot that names the session, past every row it holds. + let forged = fn(session) { transcript.Snapshot(session, 1_000_000, 0) } + [ + // Discovery: answers another session's ID, or whether one works there. + beyond("albedo_sdk/history.sessions", sessions_, fn(h) { + outcome(history.sessions(h, "", False, False)) + }), + beyond( + "albedo_sdk/search.candidates", + [capability.Sessions, capability.History], + fn(h) { outcome(search.candidates(h, "needle")) }, + ), + beyond("albedo_sdk/family.children", sessions_, fn(h) { + outcome(family.children(h, own)) + }), + beyond("albedo_sdk/family.get", sessions_, fn(h) { + outcome(family.get(h, other)) + }), + beyond("albedo_sdk/family.started_by", sessions_, fn(h) { + outcome(family.started_by(h, other)) + }), + beyond("albedo_sdk/family.resolve", sessions_, fn(h) { + outcome(family.resolve(h, own, "scout")) + }), + beyond("albedo_sdk/family.locate", sessions_, fn(h) { + outcome(family.locate(h, own, "scout", nobody)) + }), + beyond("albedo_sdk/family.remote", sessions_, fn(h) { + outcome(family.remote(h, own, "scout", nobody)) + }), + beyond("albedo/workspace/worktree.get", sessions_, fn(h) { + outcome(worktree.get(h, [], "/work", "repo/wt")) + }), + beyond("albedo/workspace/worktree.for_session", sessions_, fn(h) { + some(worktree.for_session(h, other, "/elsewhere")) + }), + beyond("albedo/workspace/worktree.for_workspace", sessions_, fn(h) { + some(worktree.for_workspace(h, "/work/wt")) + }), + beyond("albedo/workspace/worktree.list", sessions_, fn(h) { + outcome(worktree.list(h, [], "/work", None, True)) + }), + beyond("albedo/workspace/worktree.remove", sessions_, fn(h) { + // The recorded checkout lies outside the host's worktree directory, + // so a permitted call refuses to remove it. + outcome(worktree.remove(h, [], [], "/work", "repo/wt", False)) + }), + beyond("albedo/workspace/worktree.running_occupant", sessions_, fn(h) { + some(worktree.running_occupant(h, wt)) + }), + beyond("albedo/kernel/resources.recorded_kernels", sessions_, fn(h) { + outcome(resources.capability().recorded(h)) + }), + beyond("albedo_sdk/sessions.find", sessions_, fn(h) { + outcome(sessions.find(h, other)) + }), + beyond("albedo_sdk/sessions.all", sessions_, fn(h) { + outcome(sessions.all(h)) + }), + beyond("albedo_sdk/sessions.occupied", sessions_, fn(h) { + outcome(sessions.occupied(h, "/work")) + }), + beyond("albedo_sdk/family.set_role", [capability.SessionControl], fn(h) { + outcome(family.set_role(h, other, "lead")) + }), + beyond("albedo_sdk/family.set_verifier", [capability.SessionControl], fn(h) { + outcome(family.set_verifier(h, other, "true")) + }), + // A session's workspace, which every `Sessions` listing shows. + listed( + "albedo_sdk/sessions.describe", + [], + Some(capability.Sessions), + fn(h, s) { outcome(sessions.describe(h, [s])) }, + ), + // A session's stored history. + reads("albedo_sdk/history.get", fn(h, s) { outcome(history.get(h, s)) }), + reads("albedo_sdk/history.find", fn(h, s) { outcome(history.find(h, s)) }), + reads("albedo_sdk/history.acp_marker", fn(h, s) { + outcome(history.acp_marker(h, s)) + }), + reads("albedo_sdk/history.snapshot", fn(h, s) { + outcome(history.snapshot(h, s)) + }), + reads("albedo_sdk/history.source_stats", fn(h, s) { + outcome(history.source_stats(h, forged(s), 1_000_000)) + }), + reads("albedo_sdk/history.latest_user_seq", fn(h, s) { + outcome(history.latest_user_seq(h, forged(s), 1)) + }), + reads("albedo_sdk/history.rows_before", fn(h, s) { + outcome(history.rows_before(h, forged(s), 1_000_000)) + }), + reads("albedo_sdk/history.fold_sources", fn(h, s) { + outcome( + history.fold_sources(h, forged(s), 0, 1_000_000, Nil, fn(_, _) { + transcript.Continue(Nil) + }), + ) + }), + reads("albedo_sdk/history.uploaded_image", fn(h, s) { + outcome(history.uploaded_image(h, s, None, 0, 0)) + }), + reads("albedo_sdk/history.seqs", fn(h, s) { + outcome(history.seqs(h, s, 0, 10)) + }), + reads("albedo_sdk/history.spans", fn(h, s) { + outcome(history.spans(h, s, [#(0, 1_000_000)])) + }), + reads("albedo_sdk/history.context_use", fn(h, s) { + outcome(history.context_use(h, s)) + }), + reads("albedo_sdk/search.newest", fn(h, s) { + outcome(search.newest(h, s, "needle", 3)) + }), + reads("albedo_sdk/search.page", fn(h, s) { + outcome(search.page(h, s, "needle", 0, 1_000_000, 0, 10)) + }), + reads("albedo_sdk/search.page_without", fn(h, s) { + outcome(search.page_without(h, s, "needle", 0, 1_000_000, 0, 10, None)) + }), + reads("albedo_sdk/search.running_call", fn(h, s) { + outcome(search.running_call(h, s)) + }), + reads("albedo_sdk/search.transcript_grep", fn(h, s) { + outcome(search.transcript_grep(h, s, "needle", 5, 0, None, None, False)) + }), + reads("albedo_sdk/tool.transcript_read", fn(h, s) { + outcome(tool.transcript_read(h, s, 0, None, 0, 4000)) + }), + reads("albedo_sdk/tool.source_page", fn(h, s) { + outcome(tool.source_page(h, forged(s), 0, 1_000_000, "row", 0, 4000)) + }), + reads("albedo_sdk/request_history.last_turn", fn(h, s) { + outcome(requests.last_turn(h, s)) + }), + reads("albedo_sdk/request_history.outputs", fn(h, s) { + outcome(requests.outputs(h, s, [2])) + }), + reads("albedo/kernel/journal.get", fn(h, s) { + outcome(journal.get(h, s <> "/cell")) + }), + reads("albedo/kernel/journal.find_call", fn(h, s) { + outcome(journal.find_call(h, s <> "/cell")) + }), + reads("albedo/kernel/journal.draft", fn(h, s) { + outcome(journal.draft(h, s <> "/cell", [])) + }), + reads("albedo/kernel/journal.prepare", fn(h, s) { + outcome(journal.prepare(h, s <> "/cell", [], False)) + }), + reads("albedo/kernel/journal.unfinished", fn(h, s) { + outcome(journal.unfinished(h, s)) + }), + reads("albedo/kernel/journal.recent", fn(h, s) { + outcome(journal.recent(h, s, 5)) + }), + reads("albedo/kernel/journal.trace", fn(h, s) { + some(journal.trace(h, s <> "/cell")) + }), + reads("albedo/kernel/journal.saved_trace", fn(h, s) { + some(journal.saved_trace(h, s <> "/cell")) + }), + // A session's letters: the inbox reads need `Inbox`, which posts them. + letters("albedo_sdk/mail.statuses_in", [capability.Inbox], fn(h, s) { + in_connection(h, sdk_mail.statuses_in(h, _, [s <> "-asked"])) + }), + letters("albedo_sdk/mail.queued_in", [capability.Inbox], fn(h, s) { + in_connection(h, sdk_mail.queued_in(h, _, [s])) + }), + letters("albedo_sdk/mail.undelivered_in", [capability.Inbox], fn(h, s) { + in_connection(h, sdk_mail.undelivered_in(h, _, "group-" <> s, "", 10)) + }), + letters("albedo_sdk/mail.delivered_in", [capability.Inbox], fn(h, s) { + in_connection(h, sdk_mail.delivered_in(h, _, "group-" <> s, "", 10)) + }), + letters("albedo_sdk/mail.file_in", [capability.Inbox], fn(h, s) { + let filed = + in_connection(h, sdk_mail.file_in(h, _, "group-" <> s, [s <> "-loose"])) + // A filed letter keeps its grouping, so unfile it for the next probe. + let assert Ok(_) = + store.write(world.ledger, "UPDATE mail SET key=NULL WHERE id=?", [ + sqlight.text(s <> "-loose"), + ]) + filed + }), + letters( + "albedo_sdk/mail.requested", + [capability.Mail, capability.Sessions], + fn(h, s) { outcome(sdk_mail.requested(h, s)) }, + ), + // The inbox and letters the host hands extensions, by field. + letters("albedo_sdk/input.withdraw", [], fn(h, s) { + outcome(inbox.extension_inbox().withdraw(h, s, s <> "-spare")) + }), + letters("albedo_sdk/input.waiting", [], fn(h, s) { + outcome(inbox.extension_inbox().waiting(h, s, [s <> "-loose"])) + }), + letters("albedo_sdk/mail.owed_requesters", [], fn(h, s) { + outcome(mail.letters().owed_requesters(h, s)) + }), + // It reads both sessions it names: as the child and as the parent. + letters("albedo_sdk/mail.owes_reply", [], fn(h, s) { + outcome(mail.letters().owes_reply(h, s, own)) + }), + letters("albedo_sdk/mail.owes_reply", [], fn(h, s) { + outcome(mail.letters().owes_reply(h, own, s)) + }), + letters("albedo_sdk/mail.open_json", [], fn(h, s) { + outcome(mail.letters().open_json(h, s)) + }), + ] +} - // The seeded letters answer every read, and each session's differ, so - // the comparisons below say whose letters a read reached. - list.filter(list.append(own, other), fn(read) { result.is_error(read.1) }) - |> should.equal([]) - list.filter(list.zip(own, other), fn(pair) { pair.0 == pair.1 }) - |> should.equal([]) - list.filter(own, fn(read) { read.1 == Ok("[]") }) |> should.equal([]) - mail_reads(bound, "own") |> should.equal(own) - mail_reads(declaring, "own") |> should.equal(own) - mail_reads(declaring, "other") |> should.equal(other) - mail_reads(bound, "other") |> should.equal(refused(other)) - mail_reads(unbound, "own") |> should.equal(refused(own)) - mail_reads(historian, "other") |> should.equal(refused(other)) - // Nor does the capability for letters read another session's transcript. - history.get(declaring, "other") |> should.equal(denied("history")) - - // Letter IDs that name another session's letter are refused with it. - storage.query(bound, fn(connection) { - sdk_mail.statuses_in(bound, connection, ["own-asked", "other-asked"]) - }) - |> should.equal(denied("correspondence")) - - // Filing: a refused call files nothing. - file_loose(ledger, bound, "other") - |> should.equal(#(denied("correspondence"), 1)) - file_loose(ledger, unbound, "own") - |> should.equal(#(denied("correspondence"), 1)) - file_loose(ledger, bound, "own") |> should.equal(#(Ok(Nil), 2)) - file_loose(ledger, declaring, "other") |> should.equal(#(Ok(Nil), 2)) - - // Whether a child owes its parent a reply reads both their letters. - let owes = fn(handle) { mail.letters().owes_reply(handle, "own", "other") } - owes(host) |> should.equal(Ok(True)) - owes(declaring) |> should.equal(Ok(True)) - owes(bound) |> should.equal(denied("correspondence")) - - // Withdrawing: a refused call leaves the letter waiting. - let withdraw = fn(handle, session) { - let withdrawn = - inbox.extension_inbox().withdraw(handle, session, session <> "-loose") - let assert Ok(left) = - inbox.extension_inbox().waiting(host, session, [session <> "-loose"]) - #(withdrawn, left) - } - withdraw(bound, "other") - |> should.equal(#(denied("correspondence"), ["other-loose"])) - withdraw(unbound, "own") - |> should.equal(#(denied("correspondence"), ["own-loose"])) - withdraw(bound, "own") |> should.equal(#(Ok(True), [])) - withdraw(declaring, "other") |> should.equal(#(Ok(True), [])) - store.close(ledger) +/// Running `op` through a service's `Daemon.session_state`. +fn operation_call(world: World, op: command.StateOp) -> Call { + Call( + "albedo_sdk/command." <> command.operation_name(op), + Operation, + [capability.SessionState], + other_session_needs(op), + fn(probe: Probe) { + let daemon = + extension.gate_daemon( + extension.Daemon(..daemon(world), storage: probe.handle), + probe.extension, + ) + daemon.session_state(probe.session).state(op) + |> result.map_error(command.state_error_text) + |> outcome + }, + ) } -/// A service reaches any session's actor through `Daemon.session_state`, -/// but its handle is bound to none, so the operations that read or drop a -/// session's queue of letters need `Correspondence`, and those that read -/// its context, kernel, or jobs need `History`, as the SDK reads do. -/// `SessionState` alone still sets and reports a session's settings and -/// stops its jobs. -pub fn session_state_keeps_the_session_rule_test() -> Nil { - let ledger = mail_ledger() - let host = storage.host(ledger) - // A session's actor, standing in for the daemon's: it answers each - // operation from the seeded ledger, as the host reads it. - let actor = fn(session) { - command.Context( - fn(op) { - case op { - command.QueueRead(..) -> - storage.query(host, fn(connection) { - sdk_mail.queued_in(host, connection, [session]) - }) - |> result.map_error(command.Other) - |> result.map(fn(letters) { - list.map(letters, fn(letter) { letter.id }) - |> list.sort(string.compare) - |> json.array(json.string) - }) - command.QueueDrop(ids) -> - list.try_map(ids, fn(id) { mail.withdraw(ledger, session, id) }) - |> result.map_error(command.Other) - |> result.map(json.array(_, json.bool)) - command.ContextPage(..) -> - history.get(host, session) - |> result.map_error(command.Other) - |> result.map(fn(info) { json.string(string.inspect(info)) }) - // The output of the session's kernel cell, from its journal. - command.KernelRead(read) -> - journal.recent(host, session, 5) - |> result.map_error(command.Other) - |> result.map(string.inspect) - |> result.try(fn(output) { - read(None) |> result.replace(json.string(output)) - }) - command.KernelStopJob(..) -> Ok(json.string("stopped")) - _ -> Ok(json.string("model")) - } - }, - session, +/// LCM's reads, which a dependent calls with its own handle. +fn feature_calls(world: World) -> List(Call) { + let parent = fn(session) { + let assert Ok(parent) = dict.get(world.parents, session) + parent + } + let feature = fn(name, run: fn(storage.Storage, String) -> Outcome) { + Call( + "albedo_lcm/graph." <> name, + Feature, + [], + Some(capability.History), + fn(probe: Probe) { run(probe.handle, probe.session) }, ) } - let state = fn(uses, op) { - let extension = fixture([capability.SessionState, ..uses]) - let gated = - extension.gate_daemon( - extension.Daemon( - ..daemon(ledger), - session_state: actor, - storage: extension.storage_of(extension, ledger), - ), - extension, + [ + feature("frontier", fn(h, s) { outcome(graph.frontier(h, s)) }), + feature("last_seq", fn(h, s) { outcome(graph.last_seq(h, s)) }), + feature("children", fn(h, s) { outcome(graph.children(h, parent(s))) }), + feature("child_nodes", fn(h, s) { outcome(graph.child_nodes(h, parent(s))) }), + feature("spans", fn(h, s) { + outcome( + graph.spans(h, s, [ + graph.Node(parent(s), 1, 0, 1_000_000, "", False, None), + ]), ) - gated.session_state("other").state(op) - |> result.map_error(command.state_error_text) - |> result.map(json.to_string) - } - let read = command.QueueRead(10, fn(_, _) { json.null() }) - let drop = command.QueueDrop(["other-loose"]) - let page = command.ContextPage("history", 0) - let left = fn() { - let assert Ok(left) = - inbox.extension_inbox().waiting(host, "other", ["other-loose"]) - left + }), + ] +} + +/// What a caller asks about the sessions it names. +fn plain_calls() -> List(Call) { + let plain = fn(key, run: fn(storage.Storage, String) -> Outcome) { + Call(key, Plain, [], None, fn(probe: Probe) { + run(probe.handle, probe.session) + }) } - let assert Ok(queued) = actor("other").state(read) - let queued = json.to_string(queued) - let assert Ok(context) = actor("other").state(page) - let context = json.to_string(context) - // The seeded queue and context are there to read. - queued |> should.equal("[\"other-loose\",\"other-waiting\"]") - - state([], read) |> should.equal(denied("correspondence")) - state([capability.History], read) |> should.equal(denied("correspondence")) - state([capability.Correspondence], read) |> should.equal(Ok(queued)) - state([], page) |> should.equal(denied("history")) - state([capability.Correspondence], page) |> should.equal(denied("history")) - state([capability.History], page) |> should.equal(Ok(context)) - state([], command.ModelGet) |> should.equal(Ok("\"model\"")) - - // The kernel's output is the session's history; stopping a job is not. - let kernel = command.KernelRead(fn(_) { Ok(json.null()) }) - let assert Ok(output) = actor("other").state(kernel) - let output = json.to_string(output) - string.contains(output, "other/cell") |> should.be_true - state([], kernel) |> should.equal(denied("history")) - state([capability.Correspondence], kernel) |> should.equal(denied("history")) - state([capability.History], kernel) |> should.equal(Ok(output)) - state([], command.KernelJobs(fn(_, _) { json.null() })) - |> should.equal(denied("history")) - state([], command.KernelStopJob("job")) |> should.equal(Ok("\"stopped\"")) - - // A refused drop leaves the letter waiting. - state([capability.History], drop) |> should.equal(denied("correspondence")) - left() |> should.equal(["other-loose"]) - state([capability.Correspondence], drop) |> should.equal(Ok("[true]")) - left() |> should.equal([]) - store.close(ledger) + [ + plain("albedo_sdk/family.closed", fn(h, s) { outcome(family.closed(h, s)) }), + plain("albedo_sdk/sessions.names", fn(h, s) { + outcome(sessions.names(h, [s])) + }), + plain("albedo_sdk/sessions.standing", fn(h, s) { + outcome(sessions.standing(h, s)) + }), + plain("albedo_sdk/sessions.importing", fn(h, s) { + outcome(sessions.importing(h, [own, s])) + }), + ] } -/// One of each `command.StateOp`. The `case` in `sample_index` has no -/// catch-all, so a new operation fails to compile there until it is -/// classified, and `every_state_operation_is_classified_test` checks this -/// list against the constructors `sdk/command` declares, so it cannot fall -/// behind either. -fn every_operation() -> List(command.StateOp) { +/// One of each `command.StateOp`, checked against the constructors +/// `sdk/command` declares. +fn operations() -> List(command.StateOp) { [ command.ModelGet, command.ModelSelect("m", None, None), @@ -1265,251 +1208,444 @@ fn every_operation() -> List(command.StateOp) { ] } -fn sample_index(op: command.StateOp) -> Int { +/// What running `op` on a session other than the handle's needs besides +/// `SessionState`: its queue of letters needs `Correspondence`; its context, +/// kernel output, jobs, and the media its history holds need `History`; +/// its settings, its turn, and starting or stopping its kernel and jobs +/// need nothing more. No catch-all, so a new operation fails to compile +/// here until it is classified. +fn other_session_needs(op: command.StateOp) -> Option(Capability) { case op { - command.ModelGet -> 0 - command.ModelSelect(..) -> 1 - command.EffortGet -> 2 - command.EffortSelect(..) -> 3 - command.CompactAtGet -> 4 - command.CompactAtSet(..) -> 5 - command.NameGet -> 6 - command.NameSelect(..) -> 7 - command.NameSuggest(..) -> 8 - command.ContextSummary -> 9 - command.Compact(..) -> 10 - command.ContextPage(..) -> 11 - command.Submit(..) -> 12 - command.Refresh -> 13 - command.ReloadCatalogs -> 14 - command.QueueRead(..) -> 15 - command.QueueDrop(..) -> 16 - command.QueueArrange(..) -> 17 - command.QuietTurn -> 18 - command.KernelReport -> 19 - command.KernelUpgrade -> 20 - command.KernelRestart -> 21 - command.KernelRestartOf(..) -> 22 - command.KernelJobs(..) -> 23 - command.KernelStopJob(..) -> 24 - command.Accounts -> 25 - command.KernelRead(..) -> 26 - command.ExtensionChanged(..) -> 27 - command.Shake(..) -> 28 + command.QueueRead(..) | command.QueueDrop(..) | command.QueueArrange(..) -> + Some(capability.Correspondence) + command.ContextSummary + | command.ContextPage(..) + | command.Shake(..) + | command.KernelJobs(..) + | command.KernelRead(..) -> Some(capability.History) + command.ModelGet + | command.ModelSelect(..) + | command.EffortGet + | command.EffortSelect(..) + | command.CompactAtGet + | command.CompactAtSet(..) + | command.NameGet + | command.NameSelect(..) + | command.NameSuggest(..) + | command.Compact(..) + | command.Submit(..) + | command.Refresh + | command.ReloadCatalogs + | command.QuietTurn + | command.KernelReport + | command.KernelUpgrade + | command.KernelRestart + | command.KernelRestartOf(..) + | command.KernelStopJob(..) + | command.Accounts + | command.ExtensionChanged(..) -> None } } -/// `command.other_session_needs` decides every operation, and -/// `capability.session_functions`, which the lint reads, lists exactly the -/// operations it gives a capability, with that capability. The gate refuses -/// an operation whose two answers disagree. -pub fn every_state_operation_is_classified_test() -> Nil { - let operations = every_operation() - list.map(operations, sample_index) - |> should.equal(list.index_map(operations, fn(_, index) { index })) - // One sample of every constructor `StateOp` declares, each once. - list.map(operations, command.operation_name) - |> list.sort(string.compare) - |> should.equal(list.sort(declared_operations(), string.compare)) - list.each(operations, fn(op) { - let name = command.operation_name(op) - #( - name, - capability.of_session_function("albedo_sdk/command", name) - |> option.from_result, +// --- The checks ------------------------------------------------------------- + +fn variants(kind: Kind) -> List(Variant) { + case kind { + Field -> [Unbound] + Feature -> [Own, Other, Unbound] + Listed | Operation | Plain -> [Own, Other, Unbound, Host] + } +} + +/// What `call` needs through `variant`'s handle. +fn required(call: Call, variant: Variant) -> List(Capability) { + let extra = case call.other { + Some(wanted) -> [wanted] + None -> [] + } + case variant, call.kind { + // A host handle reads any session, but the gate still applies the + // extension's `uses` to the context it serves. + Host, Operation -> call.needs + Host, _ -> [] + Own, _ -> call.needs + Other, _ | Unbound, _ -> list.append(call.needs, extra) |> list.unique + } +} + +fn run( + world: World, + call: Call, + variant: Variant, + uses: List(Capability), +) -> Outcome { + let fixture = fixture(uses) + let #(handle, session) = case variant { + Own -> #(extension.session_storage_of(fixture, world.ledger, own), own) + Other -> #(extension.session_storage_of(fixture, world.ledger, own), other) + Unbound -> #(extension.storage_of(fixture, world.ledger), own) + Host -> #(storage.host(world.ledger), other) + } + call.run(Probe(fixture, handle, session)) +} + +/// Whether `outcome` refuses for want of one of `wanted`. +fn refuses_for(outcome: Outcome, wanted: List(Capability)) -> Bool { + case outcome { + Allowed -> False + Refused("") -> True + Refused(name) -> + list.any(wanted, fn(each) { capability.name(each) == name }) + } +} + +/// For every call, through each handle it can be given: leaving out any one +/// capability refuses the call, naming that capability, exactly when the +/// call needs it; declaring exactly what it needs runs it; declaring +/// nothing refuses it unless it needs nothing. +pub fn each_host_call_needs_exactly_its_capabilities_test() -> Nil { + let world = world() + // `running_occupant` finds the running session that works in the + // worktree. + bus_queues.mark_running(own, True) + let mismatches = + list.flat_map(calls(world), fn(call) { + list.flat_map(variants(call.kind), fn(variant) { + let needs = required(call, variant) + let label = fn(uses) { + call.key <> " " <> string.inspect(variant) <> " " <> uses + } + let without = + list.filter_map(every, fn(left_out) { + let got = + run( + world, + call, + variant, + list.filter(every, fn(each) { each != left_out }), + ) + let expected = case list.contains(needs, left_out), got { + False, _ -> Allowed + // A stand-in that answers no error names nothing. + True, Refused("") -> got + True, _ -> Refused(capability.name(left_out)) + } + case got == expected { + True -> Error(Nil) + False -> + Ok(#( + label("without " <> capability.name(left_out)), + expected, + got, + )) + } + }) + let exact = case run(world, call, variant, needs) { + Allowed -> [] + got -> [#(label("with exactly its needs"), Allowed, got)] + } + let none = run(world, call, variant, []) + let bare = case needs == [] && none != Allowed { + True -> [#(label("with nothing"), Allowed, none)] + False -> + case needs != [] && !refuses_for(none, needs) { + True -> [#(label("with nothing"), Refused("its needs"), none)] + False -> [] + } + } + list.flatten([without, exact, bare]) + }) + }) + bus_queues.mark_running(own, False) + assert mismatches == [] + store.close(world.ledger) +} + +/// The table names every host call: every capability, every gated context +/// field, every function the capability tables list and every operation +/// `sdk/command` declares. The gate leaves every other field of a context +/// as the host built it. +pub fn every_host_call_is_classified_test() -> Nil { + let world = world() + let calls = calls(world) + assert list.map(every, position) + == list.index_map(every, fn(_, index) { index }) + let keys = fn(kind) { + list.filter_map(calls, fn(call) { + case call.kind == kind { + True -> Ok(call.key) + False -> Error(Nil) + } + }) + |> list.unique + |> list.sort(string.compare) + } + let fields = keys(Field) + let sorted = fn(entries: List(#(String, String, Capability)), keep) { + list.filter_map(entries, fn(entry) { + case keep(entry.0) { + True -> Ok(entry.0 <> "." <> entry.1) + False -> Error(Nil) + } + }) + |> list.unique + |> list.sort(string.compare) + } + assert fields == sorted(capability.fields, fn(_) { True }) + assert keys(Listed) + == sorted( + list.append(capability.functions, capability.session_functions), + fn(module) { module != "albedo_sdk/command" }, ) - |> should.equal(#(name, command.other_session_needs(op))) + let ops = keys(Operation) + let declared = + list.map(declared_operations(), fn(name) { "albedo_sdk/command." <> name }) + |> list.sort(string.compare) + assert ops == declared + assert list.length(ops) == list.length(operations()) + // Every `command` entry in the tables names a declared operation. + assert list.all( + sorted(capability.session_functions, fn(module) { + module == "albedo_sdk/command" + }), + list.contains(ops, _), + ) + // A field the table does not gate keeps the host's value. + let fixture = fixture([]) + list.each(contexts(world), fn(context) { + let #(name, names, original, gated) = context + let assert [_, ..values] = original + let assert [_, ..after] = gated(fixture) + assert #(name, list.length(values)) == #(name, list.length(names)) + let changed = + list.zip(names, list.zip(values, after)) + |> list.filter_map(fn(entry) { + case entry.1.0 == entry.1.1 { + True -> Error(Nil) + False -> Ok(name <> "." <> entry.0) + } + }) + // Declaring nothing denies every capability field and nothing else. + let gated = + list.filter(fields, fn(key) { string.starts_with(key, name <> ".") }) + assert list.sort(changed, string.compare) == gated }) - let names = list.map(operations, command.operation_name) - list.filter(capability.session_functions, fn(entry) { - entry.0 == "albedo_sdk/command" && !list.contains(names, entry.1) + store.close(world.ledger) +} + +/// The constructors `command.StateOp` declares, read from its source: each +/// line of the type's body that starts with a capital letter. +fn declared_operations() -> List(String) { + let assert Ok(source) = + read_file("../../packages/albedo_sdk/src/albedo_sdk/command.gleam") + let assert Ok(source) = bit_array.to_string(source) + let assert Ok(#(_, body)) = string.split_once(source, "pub type StateOp {\n") + let assert Ok(#(body, _)) = string.split_once(body, "\n}\n") + string.split(body, "\n") + |> list.filter_map(fn(line) { + let line = string.trim_start(line) + case string.first(line) { + Ok(first) -> + case + string.uppercase(first) == first && string.lowercase(first) != first + { + True -> + string.split(line, "(") + |> list.first + |> result.map(string.trim) + False -> Error(Nil) + } + Error(Nil) -> Error(Nil) + } }) - |> should.equal([]) } -/// Searching every session's messages both lists other sessions and reads -/// their transcripts, so it needs `Sessions` and `History`. -pub fn searching_every_session_needs_sessions_and_history_test() -> Nil { - let ledger = history_ledger() - let assert Ok(_) = core_family.initialise(ledger) - let assert Ok(_) = transcript_text.install(ledger) - let find = fn(handle) { - agent_sessions.find(handle, "own", "needle", "", False, False, 0, 100) - |> result.map( - list.map(_, fn(found: agent_sessions.Found) { found.info.id }), - ) - } - let bound = fn(uses) { - extension.session_storage_of(fixture(uses), ledger, "own") +// --- Regressions ------------------------------------------------------------ + +/// A refused change to letters changes nothing, and one letter to a session +/// the handle may not reach refuses a whole read (ed8386ab9, d55365010). +pub fn a_refused_letter_change_leaves_the_inbox_as_it_was_test() -> Nil { + let world = world() + let ledger = world.ledger + let host = storage.host(ledger) + let bound = + extension.session_storage_of(fixture([capability.Inbox]), ledger, own) + let undelivered = fn() { + let assert Ok(page) = + storage.query(host, sdk_mail.undelivered_in( + host, + _, + "group-" <> other, + "", + 10, + )) + list.length(page) } - // The other session's message matches, so the search reads its text. - find(storage.host(ledger)) |> should.equal(Ok(["other"])) - find(bound([capability.History, capability.Sessions])) - |> should.equal(Ok(["other"])) - find(bound([capability.Sessions])) - |> should.equal(Error("fixture does not declare history")) - find(bound([capability.History])) - |> should.equal(Error("fixture does not declare sessions")) + let before = undelivered() + assert storage.query( + bound, + sdk_mail.file_in(bound, _, "group-" <> other, [ + other <> "-loose", + ]), + ) + == no_correspondence() + assert undelivered() == before + assert storage.query( + bound, + sdk_mail.statuses_in(bound, _, [ + own <> "-asked", + other <> "-asked", + ]), + ) + == no_correspondence() + assert inbox.extension_inbox().withdraw(bound, other, other <> "-loose") + == no_correspondence() + assert inbox.extension_inbox().waiting(host, other, [other <> "-loose"]) + == Ok([other <> "-loose"]) store.close(ledger) } -/// The host binds the handle a route receives to the calling session and -/// leaves a service's handle unbound, so a route reads its own session -/// without a capability and a service reads none. -pub fn the_host_binds_a_routes_handle_to_its_session_test() -> Nil { - let ledger = history_ledger() +/// Each place the host hands an extension a context gates it for that +/// extension: its startup, its preparation, its routes, which share one +/// host context, and its service. A route's handle is bound to the calling +/// session and a service's to none (bae233cda, 79e8f5719). +pub fn every_entry_point_gates_its_extension_test() -> Nil { + let world = world() + let ledger = world.ledger let seen = process.new_subject() - let read = fn(handle, stage, session) { - process.send( - seen, - #(stage, case history.get(handle, session) { - Ok(_) -> Ok("ran") - Error(reason) -> Error(reason) - }), - ) - } - let reader = - extension.Extension(..fixture([]), plugins: [ - extension.ToolPlugin("", [], [], [ - #("fixture", fn(context: host.Context, _, _) { - read(context.storage, "own", context.session) - read(context.storage, "other", "other") - Ok(json.null()) - }), - ]), - extension.ServicePlugin( - extension.Service( - fn(_, _) { extension.Admission(extension.DaemonToken, 0) }, - fn(daemon, _, _, _) { - read(daemon.storage, "service", "own") - response.new(204) - |> response.set_body(mist.Bytes(bytes_tree.new())) - }, - ), + let invalidated = process.new_subject() + let assert Ok(#(installed, [])) = + extension.install( + [reporting("open", every, seen), reporting("closed", [], seen)], + [], + [], + extension.Startup( + inbox.extension_inbox(), + fn(names, _, _) { process.send(invalidated, names) }, + selection.capability([], []), + extension.unscoped(ledger), ), - ]) + ) + assert process.receive(invalidated, 1000) == Ok(["open"]) + assert process.receive(invalidated, 100) == Error(Nil) + let composed = composition.compose( base.contribution(), - [reader], + installed, extension.preparation_owner(process.self()), ledger, - "own", + own, "/tmp", Ok("/tmp"), selection.capability([], []), worktree.capability([], []), ) - let context = - host.Context( - inbox.extension_inbox(), - mail.letters(), - "own", - "/tmp", - [], - worktree.capability([], []), - [], - fn(_) { Nil }, - fn(_, _, _) { Error("no shell output") }, - fn(_, _, _) { Error("no mail") }, - fn(_, _, _) { Ok(None) }, - fn(_, _) { Ok(None) }, - command.Context(fn(_) { Ok(json.null()) }, "s"), - fn(_) { Ok(session_control.RunningState(True)) }, - resources.capability(), - worktree.origins(), - extension.unscoped(ledger), - ) - let assert Ok(#(_, route)) = - find_route(composition.routes(composed), "fixture") - let assert Ok(_) = route(context, "fixture.go", dynamic.nil()) - heard(seen) |> should.equal(#("own", Ok("ran"))) - heard(seen) - |> should.equal(#("other", Error("fixture does not declare history"))) - // The refusal is the gate's: the other session is there to read. - let assert Ok(_) = history.get(storage.host(ledger), "other") - let assert Ok(service) = extension.service([reader], "fixture") - service.handle( - daemon(ledger), - [], - request.new() |> request.set_method(http.Post) |> request.set_body(<<>>), - live(), - ) - heard(seen) - |> should.equal(#("service", Error("fixture does not declare history"))) + let refused = fn(name, capability) { + Error(name <> " does not declare " <> capability) + } + let #(stage, overlay) = report(seen) + assert stage == "open overlay" + assert overlay != refused("open", "editor_overlay") + assert report(seen) + == #("closed overlay", refused("closed", "editor_overlay")) + + let routes = composition.routes(composed) + let context = host_context(world) + let assert Ok(open) = list.key_find(routes, "open") + let assert Ok(_) = open(context, "open.go", dynamic.nil()) + assert report(seen) == #("open control", Ok(Nil)) + assert report(seen) == #("open own history", Ok(Nil)) + assert report(seen) == #("open other history", Ok(Nil)) + let assert Ok(closed) = list.key_find(routes, "closed") + let assert Ok(_) = closed(context, "closed.go", dynamic.nil()) + assert report(seen) + == #("closed control", refused("closed", "session_control")) + assert report(seen) == #("closed own history", Ok(Nil)) + assert report(seen) == #("closed other history", refused("closed", "history")) + + let request = + request.new() |> request.set_method(http.Post) |> request.set_body(<<>>) + let live = request.new() |> request.set_body(coerce(Nil)) + let assert Ok(service) = extension.service(installed, "open") + service.handle(daemon(world), [], request, live) + assert report(seen) == #("open upstream", Error("host upstream")) + assert report(seen) == #("open service history", Ok(Nil)) + let assert Ok(service) = extension.service(installed, "closed") + service.handle(daemon(world), [], request, live) + assert report(seen) == #("closed upstream", refused("closed", "upstream")) + assert report(seen) + == #("closed service history", refused("closed", "history")) + composition.close(composed) store.close(ledger) } -/// A shared library reads a session's data under its owner's handle, but -/// with the caller's capabilities and bound session, so it keeps the -/// session rule: LCM's graph of another session, and that session's -/// transcript timestamps, are refused to a dependent that does not declare -/// `History`, and read for its own session or with `History`. -pub fn public_features_keep_the_session_rule_test() -> Nil { - let ledger = history_ledger() - let dependent = fn(uses) { - extension.session_storage_of( - extension.Extension(..fixture(uses), plugins: [ - extension.FeaturePlugin(graph.owner()), - ]), - ledger, - "own", - ) +/// An extension whose startup, route, preparation, and service each use a +/// capability and read history, and report what they answered, declaring +/// only `uses`. +fn reporting( + name: String, + uses: List(Capability), + seen: process.Subject(#(String, Result(Nil, String))), +) -> extension.Extension { + let report = fn(stage, answer: Result(a, String)) { + process.send(seen, #(name <> " " <> stage, result.replace(answer, Nil))) } - // A dependent that declares History reads and writes every session's - // graph, so it seeds them and answers for each. - let host = dependent([capability.History]) - let assert Ok(_) = graph.initialise(host) - // Each session's graph: two leaves over its two transcript rows, and a - // parent above them. - let graphs = - list.map(["own", "other"], fn(id) { - let assert Ok(_) = - graph.save_leaves(host, id, [ - graph.Leaf(1, 1, "first of " <> id, False, None), - graph.Leaf(2, 2, "second of " <> id, False, None), - ]) - let assert Ok(leaves) = graph.frontier(host, id) - let assert Ok(parent) = - graph.save_parent(host, id, leaves, "all " <> id, False, None) - #(id, parent) - }) - let reads = fn(handle, id) { - let assert Ok(parent) = list.key_find(graphs, id) - let inspect = fn(outcome: Result(a, String)) { - result.map(outcome, string.inspect) - } + extension.Extension( + name, + name, + [], [ - inspect(graph.frontier(handle, id)), - inspect(graph.child_nodes(handle, parent)), - inspect(graph.children(handle, parent)), - inspect( - graph.spans(handle, id, [ - graph.Node(parent, 1, 0, 1_000_000, "", False, None), - ]), + extension.StartupPlugin(fn(startup) { + startup.invalidate([name], [], False) + Ok([]) + }), + extension.ToolPlugin("", [], [], [ + #(name, fn(context: host.Context, _, _) { + report("control", context.control(session_control.Running(own))) + report("own history", history.get(context.storage, context.session)) + report("other history", history.get(context.storage, other)) + Ok(json.null()) + }), + ]), + extension.OwnedManagedPlugin(fn(preparation) { + report("overlay", preparation.editor_overlay()) + Ok(None) + }), + extension.ServicePlugin( + extension.Service( + fn(_, _) { extension.Admission(extension.DaemonToken, 0) }, + fn(daemon, _, _, _) { + report("upstream", daemon.upstream("p", "m", own)) + report("service history", history.get(daemon.storage, own)) + response.new(204) |> response.set_body(mist.Bytes(bytes_tree.new())) + }, + ), ), - inspect(graph.last_seq(handle, id)), - ] - } - let own = reads(host, "own") - let other = reads(host, "other") - list.filter(list.append(own, other), result.is_error) |> should.equal([]) - // Each session's graph differs, so a read says which session it reached. - list.zip(own, other) - |> list.filter(fn(pair) { pair.0 == pair.1 }) - |> list.length - |> should.equal(1) - let refusal = Error("fixture does not declare history") - - reads(dependent([]), "own") |> should.equal(own) - reads(dependent([]), "other") - |> should.equal(list.map(other, fn(_) { refusal })) - reads(dependent([capability.History]), "own") |> should.equal(own) - store.close(ledger) + ], + extension.no_initialise, + tables: [], + uses:, + ) +} + +fn report( + seen: process.Subject(#(String, Result(Nil, String))), +) -> #(String, Result(Nil, String)) { + let assert Ok(value) = process.receive(seen, 1000) + value } -/// The refusal a fixture handle gets for an undeclared `capability`. -fn denied(capability: String) -> Result(a, String) { - Error("fixture does not declare " <> capability) +fn no_correspondence() -> Result(a, String) { + Error("fixture does not declare correspondence") } + +@external(erlang, "file", "read_file") +fn read_file(path: String) -> Result(BitArray, dynamic.Dynamic) + +@external(erlang, "gleam_stdlib", "identity") +fn coerce(value: a) -> b + +@external(erlang, "erlang", "tuple_to_list") +fn record_values(record: a) -> List(dynamic.Dynamic) diff --git a/packages/albedo_sdk/src/albedo_sdk/capability.gleam b/packages/albedo_sdk/src/albedo_sdk/capability.gleam index bdcf9c56f..ee8f6a08e 100644 --- a/packages/albedo_sdk/src/albedo_sdk/capability.gleam +++ b/packages/albedo_sdk/src/albedo_sdk/capability.gleam @@ -94,7 +94,7 @@ pub type Capability { /// the module and type an extension names it by, the field, and the /// capability. Every field not listed is plain data. The host gate in /// `sdk/extension` denies exactly these fields, which -/// `test/harness/extension_capability_test` checks, and the +/// `composition/extension_capability_test` checks, and the /// `albedo_extension_capability` lint rule reads this constant from source. pub const fields = [ #("host.Context", "inbox", Inbox), diff --git a/robot-docs/extensions.md b/robot-docs/extensions.md index 3d61e579f..acad0877d 100644 --- a/robot-docs/extensions.md +++ b/robot-docs/extensions.md @@ -920,11 +920,15 @@ Set `uses` only in the constructor. A record update such as `capability.fields` in [`sdk/capability`](../packages/albedo_sdk/src/albedo_sdk/capability.gleam) holds -this table. The host gate denies exactly those fields, which +this table, the host gate denies exactly those fields, and the lint rule reads +the constant from source. [`extension_capability_test`](../apps/albedo/test/albedo/composition/extension_capability_test.gleam) -checks, and the lint rule reads the constant from source, so the three agree. -The lint also reads the SDK's constructors from source to learn where the host -calls extension code with a context. +keeps its own copy of this section's rules, written by hand. It runs every host +call the section names, each capability field, listed function, and +`command.StateOp`, once for each capability left out, and fails when +`capability.fields` or the function lists name a call its copy does not. The +lint also reads the SDK's constructors from source to learn where the host calls +extension code with a context. Every other field is plain data that the host always passes: session IDs, the workspace, the daemon home, the storage handle, the preparation owner, the tool @@ -1045,7 +1049,8 @@ not compile until it is classified: - `QueueRead`, `QueueDrop`, and `QueueArrange` need `Correspondence`. - `ContextSummary`, `ContextPage`, `KernelJobs`, and `KernelRead` need `History`. Their answers carry what the session ran and its output, including - the retained output of its background jobs. + the retained output of its background jobs. `Shake` needs it too, because it + drops media from the session's history. - The rest need nothing more than `SessionState`. They set or report the session's settings, drive its turn, or start, stop, or replace its kernel or jobs, and answer no output. @@ -1053,7 +1058,7 @@ not compile until it is classified: `capability.session_functions` lists the operations that need a capability by constructor name, which the lint reads, and [`extension_capability_test`](../apps/albedo/test/albedo/composition/extension_capability_test.gleam) -checks that the list and the classification agree. `extension.gate_daemon` +runs every operation against the classification above. `extension.gate_daemon` checks them with `storage.permit_session` for the session the context serves, and refuses an operation whose two answers disagree. An extension that declares every capability these operations need gets the host's `session_state`