From 51a968aedb12911f430fa96cfffc89ee9b265350 Mon Sep 17 00:00:00 2001 From: Aly Raffauf Date: Sat, 21 Nov 2026 02:45:49 -0500 Subject: [PATCH] test(observation): split the context snapshot probe by where it runs The snapshot retention check is a gleam test, so its term walk, closure environments included, is now written in Gleam beside it. The actor measurement is compiled with erlc and loaded into a live daemon by e2e/context_retention_test.py, whose release carries no test code, so it stays a self-contained Erlang probe and moves to e2e/ with the other inspection probes. --- .../context_snapshot_retention_test.gleam | 209 +++++++++++++++++- .../albedo_context_retention_probe.erl | 87 +------- e2e/context_retention_test.py | 3 +- 3 files changed, 211 insertions(+), 88 deletions(-) rename apps/albedo/test/albedo/observation/albedo_context_snapshot_probe.erl => e2e/albedo_context_retention_probe.erl (68%) diff --git a/apps/albedo/test/albedo/observation/context_snapshot_retention_test.gleam b/apps/albedo/test/albedo/observation/context_snapshot_retention_test.gleam index c6c0aceec..c6314f7dc 100644 --- a/apps/albedo/test/albedo/observation/context_snapshot_retention_test.gleam +++ b/apps/albedo/test/albedo/observation/context_snapshot_retention_test.gleam @@ -1,11 +1,208 @@ //// Regression for payloads hidden inside snapshot closure environments. -//// HTTP inspection cannot observe BEAM reachability or stored-reader closures. +//// HTTP inspection cannot observe BEAM reachability or stored-reader closures, +//// so this walks the snapshot's terms, closure environments included, after +//// the process that built it has exited. -import gleeunit/should - -@external(erlang, "albedo_context_snapshot_probe", "retention") -fn retention() -> #(Bool, Bool, Bool, Bool) +import albedo/observation/context_snapshot +import albedo_model as types +import albedo_model/size +import albedo_openai +import gleam/bit_array +import gleam/dict +import gleam/dynamic.{type Dynamic} +import gleam/erlang/atom.{type Atom} +import gleam/erlang/process +import gleam/int +import gleam/json +import gleam/list +import gleam/option.{None, Some} +import gleam/string pub fn discarded_payloads_are_not_retained_test() -> Nil { - retention() |> should.equal(#(True, False, False, False)) + let built = process.new_subject() + let producer = + process.spawn_unlinked(fn() { + process.send( + built, + capture([ + types.User("VISIBLE_SENTINEL:" <> string.repeat("v", 256)), + replay(1, 1_048_576), + types.UserImage("image", [inline()]), + types.ToolOutput("call", "tool", [stored()]), + ]), + ) + }) + let monitor = process.monitor(producer) + let snapshot = process.receive_forever(built) + let assert process.ProcessDown(reason: process.Normal, ..) = + process.new_selector() + |> process.select_specific_monitor(monitor, fn(down) { down }) + |> process.selector_receive_forever + garbage_collect() + let snapshot = to_dynamic(snapshot) + assert contains(snapshot, "VISIBLE_SENTINEL:") + assert !contains(snapshot, "REPLAY_SENTINEL:") + assert !contains(snapshot, "INLINE_SENTINEL:") + assert !contains(snapshot, "READER_SENTINEL:") +} + +fn capture(inputs: List(types.Input)) -> context_snapshot.Snapshot { + context_snapshot.from_request( + Some(1234), + "fixture", + "responses", + types.Responses, + size.Route("", "fixture", size.by_bytes), + albedo_openai.request("fixture", inputs), + None, + ) +} + +/// `bytes` bytes starting with `prefix` and `index`. +fn payload(prefix: String, index: Int, bytes: Int) -> String { + let head = prefix <> int.to_string(index) <> ":" + head <> string.repeat("x", bytes - string.byte_size(head)) +} + +fn replay(index: Int, bytes: Int) -> types.Input { + let encoded = + json.to_string( + json.object([ + #("type", json.string("reasoning")), + #("opaque", json.string(payload("REPLAY_SENTINEL:", index, bytes))), + ]), + ) + let assert Ok(item) = + json.parse(encoded, types.replay_decoder(types.Responses)) + types.Replay(item) +} + +fn inline() -> types.Image { + let assert Ok(image) = + types.image( + "image/png", + payload("INLINE_SENTINEL:", 1, 1_048_576), + 10, + 10, + 786_432, + ) + image +} + +fn stored() -> types.Image { + let sentinel = payload("READER_SENTINEL:", 1, 1_048_576) + let assert Ok(image) = + types.stored_image( + "image/png", + "stored-hash", + 1_048_576, + fn() { Ok(sentinel) }, + 10, + 10, + 786_432, + ) + image +} + +/// Whether a binary holding `prefix` is reachable from `term`, through +/// tuples, lists, maps and closure environments. +fn contains(term: Dynamic, prefix: String) -> Bool { + let needle = <> + walk(term, fn(binary) { has_part(binary, needle) }) +} + +fn walk(term: Dynamic, matches: fn(BitArray) -> Bool) -> Bool { + case classify(term) { + Binary(binary) -> matches(binary) + Many(terms) -> list.any(terms, walk(_, matches)) + Leaf -> False + } +} + +type Term { + Binary(BitArray) + Many(List(Dynamic)) + Leaf +} + +fn classify(term: Dynamic) -> Term { + case + is_binary(term), + is_tuple(term), + is_list(term), + is_map(term), + is_function(term) + { + True, _, _, _, _ -> Binary(unsafe_bits(term)) + _, True, _, _, _ -> Many(tuple_to_list(term)) + _, _, True, _, _ -> Many(improper_list(term)) + _, _, _, True, _ -> + Many( + unsafe_map(term) + |> dict.to_list + |> list.flat_map(fn(pair) { [pair.0, pair.1] }), + ) + _, _, _, _, True -> Many(fun_info(term, atom.create("env")).1) + _, _, _, _, _ -> Leaf + } +} + +/// A list's elements, and the tail of an improper list as one more. +fn improper_list(term: Dynamic) -> List(Dynamic) { + case is_list(term) && term != to_dynamic([]) { + True -> [hd(term), ..improper_list(tl(term))] + False -> + case term == to_dynamic([]) { + True -> [] + False -> [term] + } + } +} + +fn has_part(haystack: BitArray, needle: BitArray) -> Bool { + let size = bit_array.byte_size(needle) + case bit_array.slice(haystack, 0, size) == Ok(needle), haystack { + True, _ -> True + False, <<_, rest:bytes>> -> has_part(rest, needle) + False, _ -> False + } } + +@external(erlang, "gleam_stdlib", "identity") +fn to_dynamic(value: a) -> Dynamic + +@external(erlang, "gleam_stdlib", "identity") +fn unsafe_bits(value: Dynamic) -> BitArray + +@external(erlang, "gleam_stdlib", "identity") +fn unsafe_map(value: Dynamic) -> dict.Dict(Dynamic, Dynamic) + +@external(erlang, "erlang", "garbage_collect") +fn garbage_collect() -> Bool + +@external(erlang, "erlang", "is_binary") +fn is_binary(term: Dynamic) -> Bool + +@external(erlang, "erlang", "is_tuple") +fn is_tuple(term: Dynamic) -> Bool + +@external(erlang, "erlang", "is_list") +fn is_list(term: Dynamic) -> Bool + +@external(erlang, "erlang", "is_map") +fn is_map(term: Dynamic) -> Bool + +@external(erlang, "erlang", "is_function") +fn is_function(term: Dynamic) -> Bool + +@external(erlang, "erlang", "tuple_to_list") +fn tuple_to_list(term: Dynamic) -> List(Dynamic) + +@external(erlang, "erlang", "hd") +fn hd(term: Dynamic) -> Dynamic + +@external(erlang, "erlang", "tl") +fn tl(term: Dynamic) -> Dynamic + +@external(erlang, "erlang", "fun_info") +fn fun_info(term: Dynamic, item: Atom) -> #(Atom, List(Dynamic)) diff --git a/apps/albedo/test/albedo/observation/albedo_context_snapshot_probe.erl b/e2e/albedo_context_retention_probe.erl similarity index 68% rename from apps/albedo/test/albedo/observation/albedo_context_snapshot_probe.erl rename to e2e/albedo_context_retention_probe.erl index 65f5474af..0b15509a5 100644 --- a/apps/albedo/test/albedo/observation/albedo_context_snapshot_probe.erl +++ b/e2e/albedo_context_retention_probe.erl @@ -1,56 +1,10 @@ -%% Test-only structural inspection. Never returns inspected actor state. --module(albedo_context_snapshot_probe). --export([retention/0, actor/1, actor_json/1]). - -capture(Inputs) -> - Request = albedo_openai:request(<<"fixture">>, Inputs), - 'albedo@observation@context_snapshot':from_request( - {some, 1234}, - <<"fixture">>, - <<"responses">>, - responses, - {route, <<"">>, <<"fixture">>, fun 'albedo_model@size':by_bytes/1}, - Request, - none - ). - -payload(Prefix, Index, Bytes) -> - Head = iolist_to_binary([Prefix, integer_to_binary(Index), <<":">>]), - <>, Bytes - byte_size(Head)))/binary>>. -replay(Index, Bytes) -> - Encoded = iolist_to_binary( - json:encode(#{ - type => <<"reasoning">>, - opaque => payload(<<"REPLAY_SENTINEL:">>, Index, Bytes) - }) - ), - {ok, Item} = 'gleam@json':parse( - Encoded, - albedo_model:replay_decoder(responses) - ), - {replay, Item}. - -inline() -> - {ok, Image} = albedo_model:image( - <<"image/png">>, - payload(<<"INLINE_SENTINEL:">>, 1, 1048576), - 10, - 10, - 786432 - ), - Image. -stored() -> - Sentinel = payload(<<"READER_SENTINEL:">>, 1, 1048576), - {ok, Image} = albedo_model:stored_image( - <<"image/png">>, - <<"stored-hash">>, - 1048576, - fun() -> {ok, Sentinel} end, - 10, - 10, - 786432 - ), - Image. +%% Measures what a live session actor retains once its history is evicted. +%% e2e/context_retention_test.py compiles this module with erlc and loads it +%% into the daemon through Inspection, so it stays a self-contained Erlang +%% module: the daemon's release carries no test code. Never returns +%% inspected actor state. +-module(albedo_context_retention_probe). +-export([actor/1, actor_json/1]). walk(Term, Test) when is_binary(Term) -> Test(Term); walk(Term, Test) when is_tuple(Term) -> walk(tuple_to_list(Term), Test); @@ -67,33 +21,6 @@ contains(Term, Prefix) -> binary:match(Binary, Prefix) =/= nomatch end). -retention() -> - Parent = self(), - {Producer, Monitor} = spawn_monitor(fun() -> - Parent ! - {snapshot, - capture([ - {user, <<"VISIBLE_SENTINEL:", (binary:copy(<<"v">>, 256))/binary>>}, - replay(1, 1048576), - {user_image, <<"image">>, [inline()]}, - {tool_output, <<"call">>, <<"tool">>, [stored()]} - ])} - end), - Snapshot = - receive - {snapshot, Value} -> Value - end, - receive - {'DOWN', Monitor, process, Producer, normal} -> ok - end, - erlang:garbage_collect(), - { - contains(Snapshot, <<"VISIBLE_SENTINEL:">>), - contains(Snapshot, <<"REPLAY_SENTINEL:">>), - contains(Snapshot, <<"INLINE_SENTINEL:">>), - contains(Snapshot, <<"READER_SENTINEL:">>) - }. - binary_bytes(Pid) -> {binary, Entries} = process_info(Pid, binary), lists:sum(maps:values(maps:from_list([{Pointer, Bytes} || {Pointer, Bytes, _} <- Entries]))). diff --git a/e2e/context_retention_test.py b/e2e/context_retention_test.py index fcc297ead..a57b8348b 100644 --- a/e2e/context_retention_test.py +++ b/e2e/context_retention_test.py @@ -80,8 +80,7 @@ class ContextRetentionTest(unittest.TestCase): self.assertEqual(read(route)["state"], "ready") inspection = Inspection( app, - ROOT - / "apps/albedo/test/albedo/observation/albedo_context_snapshot_probe.erl", + ROOT / "e2e/albedo_context_retention_probe.erl", ) measured = inspection.call_json( "actor_json", f"[<<{json.dumps(session)}>>]" -- knotmirror 0.0.1