From 1021154287c0f0cfc24daf759f4e85eff3c9c3cf Mon Sep 17 00:00:00 2001 From: "nekomimi.pet" Date: Fri, 13 Nov 2026 21:19:15 -0500 Subject: [PATCH] inbox: kernels from before notice keys still post, withdraw and ask across an upgrade --- robot-docs/inbox.md | 8 ++++ .../harness/extensions/python/rpc.gleam | 9 ++-- .../harness/extensions/run/extension.gleam | 23 ++++++----- src/albedo/harness/sdk/notice.gleam | 41 +++++++++++++++++-- test/harness/notice_test.gleam | 33 +++++++++++++++ 5 files changed, 97 insertions(+), 17 deletions(-) create mode 100644 test/harness/notice_test.gleam diff --git a/robot-docs/inbox.md b/robot-docs/inbox.md index 3855ae921..4ea7950d4 100644 --- a/robot-docs/inbox.md +++ b/robot-docs/inbox.md @@ -154,6 +154,14 @@ cell awaits `albedo_notices.settle` before it reports its result, so a notice the cell made old news is withdrawn before the turn can end. The kernel never learns row ids and never polls. +A kernel started before notice keys outlives a daemon upgrade and keeps the +older protocol until it is replaced: it sends no key, keeps the key the route +answers, withdraws by `ids`, and asks `jobs.waiting` or `cells.waiting` which +still wait. The routes accept all of it, so a job or cell that finishes across +an upgrade still reports. A kernel that is upgraded to the new bundle loses the +old one's watchers, so a notice it posted may still start a turn after its +result was read. + The kernel's own notice (variables restored or lost, a restart) is a note from `kernel` posted under `Latest("kernel-notice")` (`session_namespace.set_notice`), so a newer notice replaces one no turn has diff --git a/src/albedo/harness/extensions/python/rpc.gleam b/src/albedo/harness/extensions/python/rpc.gleam index 2f4e5af2d..69a8966c7 100644 --- a/src/albedo/harness/extensions/python/rpc.gleam +++ b/src/albedo/harness/extensions/python/rpc.gleam @@ -1,7 +1,6 @@ import albedo/daemon/store import albedo/harness/extensions/python/cells as journal import albedo/harness/extensions/python/kernel as python -import albedo/harness/sdk/input import albedo/harness/sdk/notice import albedo/harness/sdk/rpc import gleam/dynamic @@ -38,14 +37,16 @@ pub fn handle( args, notice.posted_decoder(session, "cell"), )) - input.post(store, posted) - |> result.replace(json.null()) - |> result.map_error(input.refused_text) + notice.post(store, posted) } "cells.withdraw" -> { use keys <- result.try(parse(args, notice.keys_decoder())) notice.withdraw(store, session, keys) |> result.replace(json.null()) } + "cells.waiting" -> { + use keys <- result.try(parse(args, notice.keys_decoder())) + notice.waiting(store, session, keys) + } "cells.list" -> { let limit = decode.run( diff --git a/src/albedo/harness/extensions/run/extension.gleam b/src/albedo/harness/extensions/run/extension.gleam index bac3e77b8..e242f01dc 100644 --- a/src/albedo/harness/extensions/run/extension.gleam +++ b/src/albedo/harness/extensions/run/extension.gleam @@ -7,7 +7,6 @@ import albedo/harness/sdk/client_api import albedo/harness/sdk/command.{Data} import albedo/harness/sdk/extension as harness_extension import albedo/harness/sdk/host -import albedo/harness/sdk/input import albedo/harness/sdk/notice import albedo/harness/sdk/rpc import albedo/harness/sdk/service as http_service @@ -61,10 +60,10 @@ fn command() -> command.Command { ) } -/// The kernel's notice route: a finished background job whose result nobody -/// read posts a notice under the kernel's own key; the notice waits for a -/// running turn to end, so the model never polls for completion. The kernel -/// withdraws it by that key once the result is read. +/// The kernel's notice route (`sdk/notice`): a finished background job whose +/// result nobody read posts a notice under the kernel's own key; the notice +/// waits for a running turn to end, so the model never polls for completion. +/// The kernel withdraws it by that key once the result is read. fn route( store: store.Store, session: String, @@ -75,11 +74,8 @@ fn route( "jobs.completed" -> case rpc.args(args, notice.posted_decoder(session, "job"), Nil) { Ok(posted) -> - input.post(store, posted) - |> result.replace(json.null()) - |> result.map_error(fn(refused) { - #("unavailable", input.refused_text(refused)) - }) + notice.post(store, posted) + |> result.map_error(fn(reason) { #("unavailable", reason) }) Error(_) -> Error(#("invalid", "invalid jobs notice")) } "jobs.withdraw" -> @@ -90,6 +86,13 @@ fn route( |> result.map_error(fn(reason) { #("unavailable", reason) }) Error(_) -> Error(#("invalid", "invalid notice keys")) } + "jobs.waiting" -> + case rpc.args(args, notice.keys_decoder(), Nil) { + Ok(keys) -> + notice.waiting(store, session, keys) + |> result.map_error(fn(reason) { #("unavailable", reason) }) + Error(_) -> Error(#("invalid", "invalid notice keys")) + } _ -> Error(#("invalid", "unknown jobs operation")) } } diff --git a/src/albedo/harness/sdk/notice.gleam b/src/albedo/harness/sdk/notice.gleam index e953a5e88..e762e34a3 100644 --- a/src/albedo/harness/sdk/notice.gleam +++ b/src/albedo/harness/sdk/notice.gleam @@ -1,23 +1,33 @@ //// A finished job or cell's notice as the kernel posts it: under a key the //// kernel chose, so it can withdraw the notice by that key once the result is //// read. Both the `jobs` and the `cells` routes take this shape. +//// +//// A kernel started before notice keys existed outlives a daemon upgrade and +//// still speaks the older protocol: it sends no key, keeps the key the daemon +//// answers, withdraws by `ids`, and asks which of those still wait. +import albedo/daemon/mail import albedo/daemon/store import albedo/harness/sdk/input import albedo/harness/sdk/letter import gleam/dynamic/decode +import gleam/json import gleam/list import gleam/result /// `{key, display, text}` as an input from `origin` to `session`, read once -/// the running turn ends. +/// the running turn ends. A notice without a key gets a fresh one. pub fn posted_decoder( session: String, origin: String, ) -> decode.Decoder(input.Input) { - use key <- decode.field("key", decode.string) + use key <- decode.optional_field("key", "", decode.string) use display <- decode.field("display", decode.string) use text <- decode.field("text", decode.string) + let key = case key { + "" -> mail.new_id() + _ -> key + } decode.success(input.Input( session, input.Daemon(origin), @@ -29,8 +39,23 @@ pub fn posted_decoder( )) } +/// Posts a decoded notice and answers its key. +pub fn post(db: store.Store, notice: input.Input) -> Result(json.Json, String) { + use _ <- result.map( + input.post(db, notice) |> result.map_error(input.refused_text), + ) + case notice.key { + input.Once(key) | input.Latest(key) -> json.string(key) + input.Unkeyed -> json.null() + } +} + +/// `{keys}`, or `{ids}` from a kernel that predates keys. pub fn keys_decoder() -> decode.Decoder(List(String)) { - decode.field("keys", decode.list(decode.string), decode.success) + let keys = decode.list(decode.string) + decode.one_of(decode.field("keys", keys, decode.success), [ + decode.field("ids", keys, decode.success), + ]) } /// Withdraws the notices posted under `keys` that still wait. @@ -43,3 +68,13 @@ pub fn withdraw( input.withdraw(db, session, key) |> result.replace(Nil) }) } + +/// Which of `keys` still wait, for a kernel that predates keys and asks. +pub fn waiting( + db: store.Store, + session: String, + keys: List(String), +) -> Result(json.Json, String) { + mail.still_waiting(db, session, keys) + |> result.map(json.array(_, json.string)) +} diff --git a/test/harness/notice_test.gleam b/test/harness/notice_test.gleam new file mode 100644 index 000000000..7bd07f21b --- /dev/null +++ b/test/harness/notice_test.gleam @@ -0,0 +1,33 @@ +//// A kernel started before notice keys outlives a daemon upgrade and still +//// posts, withdraws and asks in the older shape: its notices must not be lost. + +import albedo/harness/sdk/input +import albedo/harness/sdk/notice +import gleam/json +import gleeunit/should + +pub fn a_notice_keeps_its_key_test() -> Nil { + let assert Ok(posted) = + json.parse( + "{\"key\":\"k1\",\"display\":\"d\",\"text\":\"t\"}", + notice.posted_decoder("session", "job"), + ) + posted.key |> should.equal(input.Once("k1")) +} + +pub fn a_notice_without_a_key_gets_one_test() -> Nil { + let assert Ok(posted) = + json.parse( + "{\"display\":\"d\",\"text\":\"t\"}", + notice.posted_decoder("session", "job"), + ) + let assert input.Once(key) = posted.key + { key != "" } |> should.be_true +} + +pub fn withdrawal_takes_keys_or_the_older_ids_test() -> Nil { + json.parse("{\"keys\":[\"a\"]}", notice.keys_decoder()) + |> should.equal(Ok(["a"])) + json.parse("{\"ids\":[\"b\"]}", notice.keys_decoder()) + |> should.equal(Ok(["b"])) +} -- knotmirror 0.0.1