okami.mom/strata

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

Niko1221Merge f-agent-turn 77c344bdd into f-own-int01d0646fd

main
4.5 KiB76 linesraw
1// (asserts stay on in a Release build: the test is its own check)
2#undef NDEBUG
3#include "strata/program/reply_branch.hpp"
4
5#include <cassert>
6#include <cstdint>
7#include <iostream>
8#include <vector>
9
10using strata::program::live_tail_is_replaced_reply;
11
12namespace {
13constexpr int64_t S = 900;   // <|im_start|>
14constexpr int64_t E = 901;   // <|im_end|>
15constexpr int64_t SYS = 10, USER = 11, ASSIST = 12;
16
17std::vector<int64_t> cat(std::initializer_list<std::vector<int64_t>> parts) {
18    std::vector<int64_t> v;
19    for (const auto& p : parts) v.insert(v.end(), p.begin(), p.end());
20    return v;
21}
22std::vector<int32_t> to32(const std::vector<int64_t>& v) { return std::vector<int32_t>(v.begin(), v.end()); }
23}  // namespace
24
25int main() {
26    const std::vector<int64_t> sys = {S, SYS, 1, 2, 3, E};
27    const std::vector<int64_t> user1 = {S, USER, 4, 5, 6, E};
28    const std::vector<int64_t> head = {S, ASSIST, 7};                 // the generation header (<|im_start|>assistant\n...)
29    const std::vector<int64_t> reasoning = {20, 21, 22, 23};
30    const std::vector<int64_t> answer = {30, 31, 32};
31    const std::vector<int64_t> user2 = {S, USER, 8, 9, E};
32    const auto history = cat({sys, user1});
33    const int64_t turn = static_cast<int64_t>(history.size());       // the turn checkpoint: [0, turn)
34    // live: the last request's prompt + its reply (reasoning and answer), the last emitted token not fed
35    const auto live = to32(cat({history, head, reasoning, answer}));
36    int cases = 0;
37
38    // (b) reasoning dropped: the history renders the assistant turn with the answer only, then the next user turn
39    assert(live_tail_is_replaced_reply(live, cat({history, {S, ASSIST}, answer, {E}, user2, head}), turn, S)); ++cases;
40    // (c) one word of the answer changed
41    assert(live_tail_is_replaced_reply(live, cat({history, {S, ASSIST}, {30, 99, 32}, {E}, user2, head}), turn, S)); ++cases;
42    // regenerate: the same history again, a new assistant turn opens where the dropped one was
43    assert(live_tail_is_replaced_reply(live, cat({history, head}), turn, S)); ++cases;
44    // the live branch was only a prompt read (prefill), nothing generated past the header
45    assert(live_tail_is_replaced_reply(to32(cat({history, head})), cat({history, {S, ASSIST}, answer, {E}, user2, head}), turn,
46                                       S)); ++cases;
47
48    // a new USER turn where the reply was (the client left the reply out entirely): another kind of request, park as before
49    assert(!live_tail_is_replaced_reply(live, cat({history, user2, head}), turn, S)); ++cases;
50    // the live tail holds more than one turn (a tool result, a user turn after the reply): not just one reply
51    assert(!live_tail_is_replaced_reply(to32(cat({history, head, answer, {E}, user2, head, answer})),
52                                        cat({history, {S, ASSIST}, answer, {E}, user2, head}), turn, S)); ++cases;
53    // resume not at a turn opening of the live branch (a periodic or root checkpoint in the middle of a message)
54    assert(!live_tail_is_replaced_reply(live, cat({history, head}), turn - 2, S)); ++cases;
55    // the prompt differs before resume (another conversation): never
56    {
57        auto other = cat({history, head});
58        other[2] = 77;
59        assert(!live_tail_is_replaced_reply(live, other, turn, S)); ++cases;
60    }
61    // the live branch ends at the checkpoint (nothing past it) or one token past it: nothing to say about a reply
62    assert(!live_tail_is_replaced_reply(to32(history), cat({history, head}), turn, S)); ++cases;
63    assert(!live_tail_is_replaced_reply(to32(cat({history, {S}})), cat({history, head}), turn, S)); ++cases;
64    // the prompt ends right at the turn opening (one token past resume): no role to compare
65    assert(!live_tail_is_replaced_reply(live, cat({history, {S}}), turn, S)); ++cases;
66    // picture_at_or_past_checkpoint_parks: the live branch has a picture at or past resume: parked as before
67    assert(!live_tail_is_replaced_reply(live, cat({history, head}), turn, S, {turn})); ++cases;
68    assert(!live_tail_is_replaced_reply(live, cat({history, head}), turn, S, {2, turn + 3})); ++cases;
69    // a picture before the checkpoint (in the reused history) does not stop the rule
70    assert(live_tail_is_replaced_reply(live, cat({history, head}), turn, S, {2})); ++cases;
71    // no turn token configured, resume 0
72    assert(!live_tail_is_replaced_reply(live, cat({history, head}), turn, -1)); ++cases;
73    assert(!live_tail_is_replaced_reply(live, cat({history, head}), 0, S)); ++cases;
74    std::cout << "SUMMARY reply_branch cases=" << cases << " passed\n";
75    return 0;
76}