diff --git a/docs/extensions.md b/docs/extensions.md index 2b8ed3152..115eac91d 100644 --- a/docs/extensions.md +++ b/docs/extensions.md @@ -28,6 +28,15 @@ Follow the [work declaration](../src/albedo/harness/extensions/work/extension.gl Follow the [schedule extension](../src/albedo/harness/extensions/schedule/extension.gleam) for a managed contribution that builds a command from the supplied store and session ID. Start from `extension.empty()` and fill the contribution fields you need. +For an `OwnedManagedPlugin` whose worker only observes session events, use [`worker.observer`](../src/albedo/harness/extension/worker.gleam). Pass the preparation owner, the worker loop, the message constructor that wraps a session handle and event, and the startup wait in milliseconds. Map its `Error(Nil)` to the extension startup message: + +```gleam +worker.observer(owner, run, Observed, 5000) +|> result.replace_error("the worker did not start") +``` + +`observer` builds the managed contribution from `extension.empty()`. Use `worker.start` and build the contribution directly when other callbacks need the worker inbox. See the [owned worker helper reference](../robot-docs/plugins.md#owned-worker-helper). + Keep event observers short. They run inside the session actor, so send work to its owner and return instead of blocking. Run command handlers outside the actor whose state operations they call. See [plugin contracts](../robot-docs/plugins.md) and [commands](../robot-docs/commands.md). Give allocated resources an explicit cleanup owner. Make close callbacks idempotent. For owned preparation, establish lifecycle monitors before readiness and complete adoption before publication. Preserve saved selection when applying it fails. The [shared lifecycle rules](../robot-docs/contributing.md#preserve-extension-lifecycle-boundaries) describe the required failure boundaries. diff --git a/robot-docs/plugins.md b/robot-docs/plugins.md index 9ce72bfeb..0bb856091 100644 --- a/robot-docs/plugins.md +++ b/robot-docs/plugins.md @@ -8,6 +8,12 @@ Plugins are contributions declared by an [extension](extensions.md), not indepen [`composition.gleam`](../src/albedo/harness/extension/composition.gleam) collects these contributions in registry order. Managed contributions can observe session events, provide a hook, and define adoption and close callbacks. Observers run inside the session actor and must only send and return. Resource close callbacks must be idempotent. +## Owned worker helper + +[`worker.observer(owner, run, observed, wait_ms)`](../src/albedo/harness/extension/worker.gleam) starts an owned worker with `worker.start` and returns its `extension.Managed` contribution. The contribution starts from `extension.empty()`. Its `observe` sends `observed(handle, event)` to the worker inbox. Its `adopt` and `close` are the started worker callbacks. + +`observer` returns `Error(Nil)` when the worker does not start. The caller maps that error to its own startup message with `result.replace_error`. `wait_ms` is the startup wait passed to `worker.start`. + ## Daemon contributions The same `Plugin` type declares provider and model catalogs, login, services, startup, migrations, settings, cleanup, and state transfer. `ServicePlugin` defines an admission policy and a body limit for its routes. `MigrationPlugin` distinguishes schema migrations from data migrations. `CleanPlugin` runs for installed extensions when a session is deleted, including extensions that are disabled. See [extension.gleam](../src/albedo/harness/extension.gleam). diff --git a/src/albedo/harness/extension/worker.gleam b/src/albedo/harness/extension/worker.gleam index 55a2aec44..0320b033d 100644 --- a/src/albedo/harness/extension/worker.gleam +++ b/src/albedo/harness/extension/worker.gleam @@ -87,6 +87,26 @@ pub fn start( } } +/// Starts a worker like `start` and packages it as a `Managed` whose +/// `observe` forwards each session event through `observed` to the worker. +/// Adoption and close are the started worker's own. A startup failure is +/// `Error(Nil)`; callers attach their own message with `replace_error`. +pub fn observer( + owner: extension.PreparationOwner, + run: fn(Running(message)) -> Nil, + observed: fn(extension.Session, extension.SessionEvent) -> message, + wait_ms: Int, +) -> Result(extension.Managed, Nil) { + use started <- result.map(start(owner, run, wait_ms)) + let inbox = started.inbox + extension.Managed( + ..extension.empty(), + observe: fn(handle, event) { process.send(inbox, observed(handle, event)) }, + adopt: started.adopt, + close: started.close, + ) +} + fn adopt(control: Subject(Control)) -> Result(Nil, String) { let caller = process.self() actor_call.try_call(control, adopt_ms, fn(reply) { Adopt(caller, reply) }) diff --git a/src/albedo/harness/extensions/persistent_agent/extension.gleam b/src/albedo/harness/extensions/persistent_agent/extension.gleam index 733f0055c..4f9b81748 100644 --- a/src/albedo/harness/extensions/persistent_agent/extension.gleam +++ b/src/albedo/harness/extensions/persistent_agent/extension.gleam @@ -31,7 +31,6 @@ import albedo/text_scalars import gleam/bool import gleam/dict.{type Dict} import gleam/dynamic/decode -import gleam/erlang/process import gleam/int import gleam/io import gleam/list @@ -825,21 +824,13 @@ fn prepare_former( session: String, _workspace: String, ) -> Result(extension.Managed, String) { - use started <- result.map( - worker.start( - owner, - fn(running) { listen(running, Former(ledger, session, None)) }, - 5000, - ) - |> result.replace_error("the memory former did not start"), - ) - let inbox = started.inbox - extension.Managed( - ..extension.empty(), - observe: fn(handle, event) { process.send(inbox, Heard(handle, event)) }, - close: started.close, - adopt: started.adopt, + worker.observer( + owner, + fn(running) { listen(running, Former(ledger, session, None)) }, + Heard, + 5000, ) + |> result.replace_error("the memory former did not start") } fn listen(running: worker.Running(Heard), former: Former) -> Nil { diff --git a/src/albedo/harness/extensions/warm/extension.gleam b/src/albedo/harness/extensions/warm/extension.gleam index 730b30206..bae207ed6 100644 --- a/src/albedo/harness/extensions/warm/extension.gleam +++ b/src/albedo/harness/extensions/warm/extension.gleam @@ -74,21 +74,13 @@ fn prepare( _session: String, _workspace: String, ) -> Result(extension.Managed, String) { - use started <- result.map( - worker.start( - owner, - fn(running) { serve(running, Warmer(None, 0, 0)) }, - 5000, - ) - |> result.replace_error("the warmer did not start"), - ) - let inbox = started.inbox - extension.Managed( - ..extension.empty(), - observe: fn(handle, event) { process.send(inbox, Observed(handle, event)) }, - close: started.close, - adopt: started.adopt, + worker.observer( + owner, + fn(running) { serve(running, Warmer(None, 0, 0)) }, + Observed, + 5000, ) + |> result.replace_error("the warmer did not start") } fn serve(running: worker.Running(Message), warmer: Warmer) -> Nil { diff --git a/test/harness/extension_worker_test.gleam b/test/harness/extension_worker_test.gleam index 65d08f568..d37489bd7 100644 --- a/test/harness/extension_worker_test.gleam +++ b/test/harness/extension_worker_test.gleam @@ -1,6 +1,9 @@ //// Deterministic process gates exercise preparer/runtime death around readiness //// and adoption. E2E cannot stop these processes at those mailbox boundaries. +import albedo/harness/extension +import albedo/harness/extension/worker +import gleam/erlang/process.{type Pid, type Subject} import gleeunit/should @external(erlang, "albedo_extension_worker_test_support", "ownership") @@ -16,3 +19,61 @@ pub fn ownership_and_adoption_preserve_lifetime_and_event_order_test() -> Nil { pub fn startup_failure_confirms_death_and_discards_readiness_test() -> Nil { startup_failures() |> should.be_true } + +type Observed { + Observed(session: String, event: extension.SessionEvent) +} + +type Note { + WorkerStarted(pid: Pid) + Delivered(session: String, event: extension.SessionEvent) +} + +fn serve_worker( + running: worker.Running(Observed), + notes: Subject(Note), +) -> Nil { + case worker.receive(running) { + Ok(Observed(session, event)) -> { + process.send(notes, Delivered(session, event)) + serve_worker(running, notes) + } + Error(Nil) -> Nil + } +} + +fn session(id: String) -> extension.Session { + extension.Session( + id: id, + call: fn(_, _) { Error("not used") }, + refresh: fn(_) { Nil }, + awaited: fn() { False }, + ) +} + +/// Exercise the packaged callbacks together. Native tests above cover owner +/// death and wrong-caller adoption; this catches missing forwarding or wiring. +pub fn observer_forwards_events_and_wires_adopt_and_close_test() -> Nil { + let notes = process.new_subject() + let assert Ok(managed) = + worker.observer( + extension.preparation_owner(process.self()), + fn(running) { + process.send(notes, WorkerStarted(process.self())) + serve_worker(running, notes) + }, + fn(handle, event) { Observed(handle.id, event) }, + 5000, + ) + managed.observe(session("first"), extension.Stirred) + let assert Ok(Nil) = managed.adopt() + let assert Ok(WorkerStarted(worker_pid)) = process.receive(notes, 5000) + process.receive(notes, 5000) + |> should.equal(Ok(Delivered("first", extension.Stirred))) + managed.observe(session("second"), extension.Compacted) + process.receive(notes, 5000) + |> should.equal(Ok(Delivered("second", extension.Compacted))) + managed.close() + process.is_alive(worker_pid) |> should.be_false + managed.close() +}