diff --git a/src/albedo/harness/sdk/capability.gleam b/src/albedo/harness/sdk/capability.gleam index be3278477..c846c7768 100644 --- a/src/albedo/harness/sdk/capability.gleam +++ b/src/albedo/harness/sdk/capability.gleam @@ -98,6 +98,8 @@ pub const fields = [ #("host.Context", "imagegens", Providers), #("host.Context", "followers", Providers), #("host.Context", "kernel_resources", KernelResources), + // `recorded` answers every session's kernels, so it also needs Sessions. + #("host.Context", "kernel_resources", Sessions), #("extension.Daemon", "inbox", Inbox), #("extension.Daemon", "upstream", Upstream), #("extension.Daemon", "models", Models), diff --git a/src/albedo/harness/sdk/extension.gleam b/src/albedo/harness/sdk/extension.gleam index 126bbe6d1..13bd58603 100644 --- a/src/albedo/harness/sdk/extension.gleam +++ b/src/albedo/harness/sdk/extension.gleam @@ -1172,12 +1172,18 @@ pub fn gate_host(context: host.Context, extension: Extension) -> host.Context { }, session_state: gated_state(extension, context.session_state), control: gated_control(extension, context.control), + // `recorded` answers every session's kernels, so it needs `Sessions` + // as well, as `capability.fields` lists. kernel_resources: { - use refusal <- gate( - extension, - capability.KernelResources, - context.kernel_resources, - ) + let sessions = { + use refusal <- gate( + extension, + capability.Sessions, + context.kernel_resources, + ) + kernel.Resources(recorded: fn(_) { Error(refusal()) }) + } + use refusal <- gate(extension, capability.KernelResources, sessions) kernel.Resources(recorded: fn(_) { Error(refusal()) }) }, ) diff --git a/test/manual/coding.gleam b/test/manual/coding.gleam index 69ab61cff..e587f0450 100644 --- a/test/manual/coding.gleam +++ b/test/manual/coding.gleam @@ -3,6 +3,7 @@ //// No credentials are retained in the kernel environment or output. import albedo/harness/extensions/work/ledger as work +import albedo/harness/host_install import albedo/harness/runtime import albedo/harness/sdk/storage import albedo/openai_api as openai @@ -28,6 +29,7 @@ fn now() -> Int fn with_runtime(host: runtime.Runtime, run: fn() -> a) -> a pub fn main() -> Nil { + host_install.install() let key = take_key() let workspace = env("ALBEDO_LIVE_WORKSPACE") case diff --git a/test/manual/copy_probe.gleam b/test/manual/copy_probe.gleam index 07523fd34..c34eff33c 100644 --- a/test/manual/copy_probe.gleam +++ b/test/manual/copy_probe.gleam @@ -5,6 +5,7 @@ //// a per-session one (the composition) shows what it actually costs. import albedo/harness/extension/composition +import albedo/harness/host_install import albedo/harness/runtime import albedo/harness/sdk/extension import gleam/int @@ -25,6 +26,7 @@ fn report(name: String, term: a) -> Nil { } pub fn main() -> Nil { + host_install.install() let workspace = "/tmp" let assert Ok(host) = runtime.start(":memory:") session_fixture.initialise(host) diff --git a/tools/lint/src/albedo_lint/capabilities.gleam b/tools/lint/src/albedo_lint/capabilities.gleam index bb7dfed4a..77ac1538b 100644 --- a/tools/lint/src/albedo_lint/capabilities.gleam +++ b/tools/lint/src/albedo_lint/capabilities.gleam @@ -146,13 +146,14 @@ fn returns_routes( } } -/// The capability a context's field carries, if it is gated. -pub fn capability_of( +/// The capabilities a context's field carries, none when it is not gated. A +/// field listed more than once needs every capability listed for it. +pub fn capabilities_of( table: Table, context: String, field: String, -) -> Result(String, Nil) { - list.find_map(table.fields, fn(entry) { +) -> List(String) { + list.filter_map(table.fields, fn(entry) { case entry.0 == context && entry.1 == field { True -> Ok(entry.2) False -> Error(Nil) @@ -393,19 +394,18 @@ fn pattern_reads(scope: Scope, pattern: glance.Pattern) -> List(Read) { glance.PatternVariant(location:, module:, constructor:, arguments:, ..) -> { let own = case context_named(scope, module, constructor) { Some(context) -> - list.filter_map(arguments, fn(argument) { + list.flat_map(arguments, fn(argument) { let label = case argument { glance.LabelledField(label:, ..) -> Ok(label) glance.ShorthandField(label:, ..) -> Ok(label) glance.UnlabelledField(_) -> Error(Nil) } - use label <- result.try(label) - use capability <- result.map(capability_of( - scope.table, - context, - label, - )) - Read(capability, label, context, location, False) + case label { + Ok(label) -> + capabilities_of(scope.table, context, label) + |> list.map(Read(_, label, context, location, False)) + Error(Nil) -> [] + } }) None -> [] } @@ -442,11 +442,10 @@ fn expression_reads( glance.FieldAccess(location, glance.Variable(_, name), label) -> case dict.get(env, name) { Ok(context) -> - case capability_of(scope.table, context, label) { - Ok(capability) -> [ - Read(capability, label, context, location, False), - ] - Error(Nil) -> holder_reads(scope, context, label, location) + case capabilities_of(scope.table, context, label) { + [] -> holder_reads(scope, context, label, location) + capabilities -> + list.map(capabilities, Read(_, label, context, location, False)) } Error(Nil) -> granting(scope, Some(name), label, location) } diff --git a/tools/lint/test/capability_rules_test.gleam b/tools/lint/test/capability_rules_test.gleam index 0b4f2de12..eadd8afbd 100644 --- a/tools/lint/test/capability_rules_test.gleam +++ b/tools/lint/test/capability_rules_test.gleam @@ -908,6 +908,20 @@ pub fn main() { "alpha/extension.gleam", sdk <> "pub fn plugin() { extension.ToolPlugin(\"\", [], [], [#(\"alpha\", fn(context, method, args) { context.kernel_resources.recorded(context.storage) })]) }", + 2, + ) + check( + root, + "alpha/extension.gleam", + sdk + <> declare + <> "pub const uses = [capability.KernelResources]\n" + <> build( + "alpha", + "alpha", + "extension.ToolPlugin(\"\", [], [], [#(\"alpha\", fn(context, method, args) { context.kernel_resources.recorded(context.storage) })])", + "uses:", + ), 1, ) check(