okami.mom/albedo

git clone https://git.nkp.pet/okami.mom/albedo

Aly Raffauffeat(api)!: migrate daemon and clients to protocol 361c43ca

main
4.2 KiB145 linesraw
1//// Payload decode counts are a performance invariant invisible to HTTP E2E.
2//// A long archived prefix must not be decoded by an ordinary preparation.
3
4import albedo/daemon/conversation
5import albedo/daemon/family
6import albedo/daemon/store
7import albedo/harness/extensions
8import albedo/harness/extensions/lcm/extension as lcm
9import albedo/harness/extensions/lcm/graph
10import albedo/harness/extensions/lcm/memory
11import albedo/harness/runtime
12import albedo/openai_api/types
13import gleam/erlang/process
14import gleam/list
15import gleam/option.{None, Some}
16import gleam/string
17import gleeunit/should
18
19@external(erlang, "albedo_lcm_preparation_test_support", "count_decodes")
20fn count_decodes(owner: process.Pid, run: fn() -> a) -> #(a, Int)
21
22pub fn below_trigger_and_stored_prior_do_not_decode_archived_payloads_test() -> Nil {
23  let installed = [
24    memory.extension(),
25    lcm.configured_extension(lcm.Config(Some(2000), 90, 1)),
26  ]
27  let assert Ok(host) =
28    runtime.start_with_config(
29      ":memory:",
30      extensions.Config(installed, ["lcm-memory", "lcm"]),
31    )
32  let ledger = runtime.ledger(host)
33  let assert Ok(_) = conversation.initialise(ledger)
34  let assert Ok(_) = family.initialise(ledger)
35  let assert Ok(_) =
36    conversation.create(
37      ledger,
38      conversation.Info(
39        "preparation",
40        "test",
41        "/tmp",
42        "provider",
43        "model",
44        types.Responses,
45        conversation.Idle,
46        None,
47        None,
48      ),
49    )
50  let archived =
51    list.repeat(Nil, 400)
52    |> list.flat_map(fn(_) {
53      [types.User("archived user"), types.Assistant("archived answer")]
54    })
55  let inputs =
56    list.append(archived, [types.User("latest"), types.Assistant("tail")])
57  let assert Ok(_) =
58    conversation.commit_from(
59      ledger,
60      "preparation",
61      inputs,
62      conversation.Idle,
63      Some("provider"),
64    )
65  let assert Ok(rows) = conversation.load_sources(ledger, "preparation")
66  let assert [first, ..] = rows
67  let assert Ok(last) = list.last(list.take(rows, 800))
68  let assert Ok(_) =
69    graph.save_leaves(ledger, "preparation", [
70      graph.Leaf(first.source.seq, last.source.seq, "archived prefix"),
71    ])
72  let #(control, positive_decodes) =
73    count_decodes(store.owner(ledger), fn() {
74      conversation.load_sources(ledger, "preparation")
75    })
76  let assert Ok(_) = control
77  should.be_true(positive_decodes >= 802)
78  let assert Ok(session) = runtime.open_session(host, "preparation", "/tmp")
79  let #(prepared, decodes) =
80    count_decodes(store.owner(ledger), fn() {
81      runtime.prepare_history_with(
82        host,
83        session,
84        "model",
85        "",
86        fn(_) { panic as "below-trigger preparation must not summarize" },
87        inputs,
88      )
89    })
90  let assert Ok([
91    types.User(summary),
92    types.User("latest"),
93    types.Assistant("tail"),
94  ]) = prepared
95  should.be_true(string.contains(summary, "archived prefix"))
96  decodes |> should.equal(0)
97  let #(prior, decodes) =
98    count_decodes(store.owner(ledger), fn() {
99      lcm.stored_prior(ledger, "preparation", inputs)
100    })
101  let assert Ok(_) = prior
102  decodes |> should.equal(0)
103  let assert Ok(_) =
104    conversation.commit_from(
105      ledger,
106      "preparation",
107      [types.User("next")],
108      conversation.Idle,
109      Some("provider"),
110    )
111  let next = list.append(inputs, [types.User("next")])
112  let #(failed, decodes) =
113    count_decodes(store.owner(ledger), fn() {
114      runtime.compact_history_scoped(
115        host,
116        session,
117        "model",
118        "model",
119        None,
120        "",
121        fn(_) { Error("summary unavailable") },
122        next,
123      )
124    })
125  failed |> should.equal(Error("compaction lcm: summary unavailable"))
126  decodes |> should.equal(2)
127  graph.last_seq(ledger, "preparation") |> should.equal(Ok(last.source.seq))
128  let #(compacted, decodes) =
129    count_decodes(store.owner(ledger), fn() {
130      runtime.compact_history_scoped(
131        host,
132        session,
133        "model",
134        "model",
135        None,
136        "",
137        fn(_) { Ok("short") },
138        next,
139      )
140    })
141  let assert Ok(_) = compacted
142  decodes |> should.equal(2)
143  graph.last_seq(ledger, "preparation") |> should.equal(Ok(last.source.seq + 2))
144  runtime.stop(host)
145}