File size: 10,848 Bytes
4531c3e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#include "../include/pulsefold.hpp"

#include <algorithm>
#include <cstdint>
#include <cstdlib>
#include <iostream>
#include <limits>
#include <optional>
#include <random>
#include <stdexcept>
#include <string>
#include <tuple>
#include <vector>

namespace {

using Tick = std::uint64_t;
using Index = std::uint32_t;
using Config = std::vector<std::vector<Tick>>;
using Event = std::tuple<Tick, Index, Index, bool>;
using Events = std::vector<Event>;
constexpr Tick last_tick = std::numeric_limits<Tick>::max();

void require(bool condition, const std::string& message) {
    if (!condition) throw std::runtime_error(message);
}

// The oracle scans every independent subscription at every call. It shares
// neither an ordering index nor an expiration cursor with the product.
struct Watch {
    Tick ttl;
    std::optional<Tick> observed_at;
    bool stale = false;
};

class ScanMonitor {
public:
    explicit ScanMonitor(const Config& config) : feeds_(config.size()) {
        for (std::size_t f = 0; f < config.size(); ++f) {
            for (Tick ttl : config[f]) feeds_[f].push_back({ttl, std::nullopt, false});
        }
    }

    Events apply(Tick now, std::optional<Index> refresh) {
        Events events;
        for (Index f = 0; f < feeds_.size(); ++f) {
            for (Index s = 0; s < feeds_[f].size(); ++s) {
                auto& watch = feeds_[f][s];
                if (!watch.observed_at || watch.stale) continue;
                // Subtraction handles an unrepresentable mathematical deadline.
                if (now - *watch.observed_at >= watch.ttl) {
                    events.emplace_back(*watch.observed_at + watch.ttl, f, s, false);
                    watch.stale = true;
                }
            }
        }
        std::sort(events.begin(), events.end());
        if (refresh) {
            auto& feed = feeds_[*refresh];
            for (Index s = 0; s < feed.size(); ++s) {
                if (feed[s].stale) events.emplace_back(now, *refresh, s, true);
                feed[s].observed_at = now;
                feed[s].stale = false;
            }
        }
        return events;
    }

    std::optional<Tick> first_deadline() const {
        std::optional<Tick> earliest;
        for (const auto& feed : feeds_) {
            for (const auto& watch : feed) {
                if (watch.stale || !watch.observed_at || last_tick - *watch.observed_at < watch.ttl)
                    continue;
                const Tick due = *watch.observed_at + watch.ttl;
                if (!earliest || due < *earliest) earliest = due;
            }
        }
        return earliest;
    }

private:
    std::vector<std::vector<Watch>> feeds_;
};

class Check {
public:
    explicit Check(const Config& config) : actual_(config), grouped_(config), oracle_(config) {}

    Check(const Config& policies, const std::vector<Index>& policy_for)
        : actual_(policies, policy_for), grouped_(policies, policy_for),
          oracle_(expand(policies, policy_for)) {}

    Events call(Tick now, std::optional<Index> refresh = std::nullopt) {
        Events recorded;
        auto emit = [&](Tick at, Index feed, Index subscriber, bool fresh) {
            recorded.emplace_back(at, feed, subscriber, fresh);
        };
        if (refresh) actual_.observe(*refresh, now, emit);
        else actual_.advance(now, emit);

        Events expanded;
        std::optional<std::tuple<Tick, Index, bool>> previous;
        auto emit_group = [&](Tick at, Index feed, pulsefold::SubscriberView group, bool fresh) {
            require(group.size() > 0 && static_cast<std::size_t>(group.end() - group.begin()) == group.size(),
                "invalid subscriber view");
            const auto key = std::make_tuple(at, feed, fresh);
            require(!previous || key != *previous, "emitted multiple groups for one deadline or recovery");
            previous = key;
            for (Index subscriber : group) expanded.emplace_back(at, feed, subscriber, fresh);
        };
        if (refresh) grouped_.observe_groups(*refresh, now, emit_group);
        else grouped_.advance_groups(now, emit_group);
        require(expanded == recorded, "group expansion changed the scalar event order");

        // Ordering of recovery callbacks within one feed is immaterial. All
        // expirations, including those at the observation tick, must precede them.
        const auto recoveries = std::find_if(recorded.begin(), recorded.end(),
            [](const Event& e) { return std::get<3>(e); });
        const auto context = " at tick " + std::to_string(now) + " on call " +
            std::to_string(++calls_);
        require(std::all_of(recoveries, recorded.end(),
            [](const Event& e) { return std::get<3>(e); }), "expiration after recovery" + context);
        require(std::is_sorted(recorded.begin(), recoveries), "expirations out of order" + context);
        std::sort(recoveries, recorded.end());
        require(recorded == oracle_.apply(now, refresh), "oracle mismatch" + context);
        const auto hint = actual_.next_wake();
        const auto exact = oracle_.first_deadline();
        require(!hint || *hint > now, "wake hint failed to advance" + context);
        require(!exact || (hint && *hint <= *exact), "missing or late wake hint" + context);
        return recorded;
    }

    void reject(Tick now, std::optional<Index> refresh) {
        const auto hint = actual_.next_wake();
        bool called = false;
        bool rejected = false;
        auto emit = [&](Tick, Index, Index, bool) { called = true; };
        try {
            if (refresh) actual_.observe(*refresh, now, emit);
            else actual_.advance(now, emit);
        } catch (const std::exception&) {
            rejected = true;
        }
        require(rejected && !called, "invalid call must reject before dispatch");
        require(hint == actual_.next_wake(), "invalid call changed next wake");
    }

private:
    static Config expand(const Config& policies, const std::vector<Index>& policy_for) {
        Config by_feed;
        for (Index policy : policy_for) by_feed.push_back(policies.at(policy));
        return by_feed;
    }

    pulsefold::Monitor actual_;
    pulsefold::Monitor grouped_;
    ScanMonitor oracle_;
    std::size_t calls_ = 0;
};

void boundaries() {
    Check c(Config{{7, 3, 3, 1}, {4, 3}, {}, {1}});
    require(c.call(101).empty(), "unseen feeds must stay silent");
    for (Index f : {1u, 3u, 0u, 2u}) require(c.call(101, f).empty(), "first observation");
    require(c.call(102, 0) == Events{{102,0,3,false}, {102,3,0,false}, {102,0,3,true}},
        "same-tick expiration must precede recovery");
    require(c.call(102, 0).empty(), "duplicate observation");
    require(c.call(105) == Events{{103,0,3,false}, {104,1,1,false}, {105,0,1,false},
        {105,0,2,false}, {105,1,0,false}}, "tied TTLs must retain subscriber IDs");
    require(c.call(109, 0) == Events{{109,0,0,false}, {109,0,0,true}, {109,0,1,true},
        {109,0,2,true}, {109,0,3,true}}, "stalled feed recovery");
    c.call(last_tick);
    require(c.call(last_tick).empty(), "a stale transition may fire only once");

    Check empty(Config{});
    empty.call(0);
    empty.reject(55, 0);
    empty.call(1);
    empty.call(last_tick);
}

void clock_limits() {
    Check c(Config{{last_tick, last_tick - 1}, {1, 2, 3}, {last_tick}});
    c.call(0, 0);
    c.call(1, 2);
    c.call(last_tick - 2, 1);
    require(c.call(last_tick - 1) == Events{{last_tick-1,0,1,false}, {last_tick-1,1,0,false}},
        "near-maximum deadline");
    require(c.call(last_tick, 1) == Events{{last_tick,0,0,false}, {last_tick,1,1,false},
        {last_tick,1,0,true}, {last_tick,1,1,true}}, "exact max and unrepresentable deadlines");
    c.call(last_tick, 0);
    require(c.call(last_tick).empty(), "overflow must not create a deadline at max");

    Check rearm(Config{{10}, {2}});
    rearm.call(last_tick - 12, 0);
    rearm.call(last_tick - 9, 0);  // Its mathematical deadline now overflows.
    rearm.call(last_tick - 2);    // Retire the obsolete first wake.
    rearm.call(last_tick - 2, 1);
    require(rearm.call(last_tick) == Events{{last_tick,1,0,false}},
            "a new feed must still fire after the previous wake overflows");
}

void rejected_calls_are_atomic() {
    Check c(Config{{2, 5}, {1}, {}});
    c.call(10, 0);
    c.reject(100, 3);
    c.reject(9, std::nullopt);
    c.reject(9, 1);
    c.reject(8, std::numeric_limits<Index>::max());
    require(c.call(12) == Events{{12,0,0,false}}, "invalid calls may not consume deadlines");
    c.reject(last_tick, std::numeric_limits<Index>::max());
    c.call(13, 0);
    c.call(14, 1);
    c.call(25);
}

void shared_policies() {
    const Config policies{{7, 1, 3, 1}, {}, {11, 4}};
    const std::vector<Index> policy_for{2, 0, 0, 1, 0, 2};
    Check c(policies, policy_for);
    for (Index f : {0u, 1u, 4u, 3u}) c.call(1, f);
    c.call(2, 2);
    c.call(2, 4);
    c.call(4, 1);
    c.call(6, 5);
    c.call(1000);
    for (Index f = 0; f < policy_for.size(); ++f) c.call(1000, f);
    c.call(last_tick);

    Check unused(policies, {});
    unused.call(last_tick);
    bool rejected = false;
    try {
        pulsefold::Monitor bad(policies, std::vector<Index>{0, 3});
    } catch (const std::exception&) {
        rejected = true;
    }
    require(rejected, "invalid shared policy ID");
    rejected = false;
    try {
        pulsefold::Monitor bad(Config{{3, 0, 4}});
    } catch (const std::invalid_argument&) {
        rejected = true;
    }
    require(rejected, "zero TTL");
}

void differential() {
    for (unsigned seed = 0; seed < 60; ++seed) {
        std::mt19937_64 rng(seed);
        const Index n = 1 + static_cast<Index>(rng() % 24);
        Config config(n);
        for (auto& feed : config) {
            const auto size = rng() % 24;
            for (std::size_t s = 0; s < size; ++s) feed.push_back(1 + rng() % 20);
        }
        Check c(config);
        Tick now = 0;
        for (int step = 0; step < 450; ++step) {
            if (step % 53 == 0) now += 1000;
            else now += rng() % 4;
            if (step % 37 == 0) c.reject(now + 100, n);
            if (rng() % 5 == 0) c.call(now);
            else c.call(now, static_cast<Index>(rng() % n));
        }
        c.call(last_tick);
        for (Index f = 0; f < n; ++f) c.call(last_tick, f);
        c.call(last_tick);
    }
}

} // namespace

int main() {
    try {
        boundaries();
        clock_limits();
        rejected_calls_are_atomic();
        shared_policies();
        differential();
        std::cout << "Pulsefold monitor checks passed (60 randomized traces).\n";
        return EXIT_SUCCESS;
    } catch (const std::exception& e) {
        std::cerr << "Pulsefold monitor check failed: " << e.what() << '\n';
        return EXIT_FAILURE;
    }
}