Something went wrong. Try again.
small gleam coding and persistent agent daemon with a detachable cli and a canvas ui forked from okami.mom/albedo
Something went wrong. Try again.
3.7 kB 106 lines
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107// The request ledger keeps a chain of hashes instead of request bodies, so a// cache miss can be told from our own prefix drifting. The stride and tail// positions are easy to get off by one, and an e2e scenario cannot see the// stored column, so this pins what the chain promises: an append-only request// agrees with its predecessor at every position they share, and a change// anywhere earlier (head or input) moves every position from there on. The// stream trace is counted in an atomics array its callback cannot return// from, so its delay and byte counts are checked here too.import albedo/daemon/requestsimport albedo/daemon/stream_traceimport albedo/openai_api/typesimport gleam/dict.{type Dict}import gleam/intimport gleam/listimport gleam/option.{None, Some}import gleam/resultimport gleam/stringimport gleeunit/should
fn inputs(count: Int) -> List(types.Input) { list.repeat(Nil, count) |> list.index_map(fn(_, index) { types.User("input " <> int.to_string(index)) })}
/// The chain as position to hash.fn chain(head: String, inputs: List(types.Input)) -> Dict(Int, String) { requests.input_chain(head, [], inputs) |> string.split(",") |> list.filter_map(fn(mark) { case string.split(mark, ":") { [position, hash] -> int.parse(position) |> result.map(fn(position) { #(position, hash) }) _ -> Error(Nil) } }) |> dict.from_list}
fn shared(a: Dict(Int, String), b: Dict(Int, String)) -> List(Int) { dict.keys(a) |> list.filter(dict.has_key(b, _)) |> list.sort(int.compare)}
fn agreeing(a: Dict(Int, String), b: Dict(Int, String)) -> List(Int) { shared(a, b) |> list.filter(fn(position) { dict.get(a, position) == dict.get(b, position) })}
pub fn chain_keeps_stride_and_tail_positions_test() -> Nil { chain("head", inputs(40)) |> dict.keys |> list.sort(int.compare) |> should.equal([16, 32, 37, 38, 39, 40]) chain("head", inputs(3)) |> dict.keys |> list.sort(int.compare) |> should.equal([1, 2, 3]) requests.input_chain("head", [], []) |> should.equal("")}
pub fn chain_of_an_extended_request_agrees_with_its_predecessor_test() -> Nil { let before = chain("head", inputs(38)) let after = chain("head", inputs(40)) // 16, 32, and the last positions both keep (35..38 against 37..40). shared(before, after) |> should.equal([16, 32, 37, 38]) agreeing(before, after) |> should.equal([16, 32, 37, 38])}
pub fn chain_moves_from_a_changed_input_onward_test() -> Nil { let original = inputs(40) let edited = list.index_map(original, fn(input, index) { case index { 20 -> types.User("edited") _ -> input } }) // Input 21 is the edited one: 16 precedes it and agrees, the rest do not. agreeing(chain("head", original), chain("head", edited)) |> should.equal([16])}
pub fn chain_moves_with_the_head_test() -> Nil { agreeing(chain("head", inputs(40)), chain("other head", inputs(40))) |> should.equal([])}
pub fn a_stream_that_said_nothing_has_no_first_event_test() -> Nil { stream_trace.new(1000) |> stream_trace.streamed |> should.equal(stream_trace.Streamed(None, 0, 0, 0))}
pub fn a_stream_counts_its_events_and_bytes_test() -> Nil { let trace = stream_trace.new(1000) stream_trace.observe(trace, types.Started("response"), 1400) stream_trace.observe(trace, types.ThinkingDelta("hmm"), 1500) stream_trace.observe(trace, types.ThinkingDelta("ok"), 1600) stream_trace.observe(trace, types.TextDelta(0, 0, "hello"), 1700) stream_trace.observe(trace, types.ArgumentsDelta(1, "python", "{}"), 1800) stream_trace.streamed(trace) |> should.equal(stream_trace.Streamed(Some(400), 5, 5, 7))}