graphrefly-operators 0.0.7

Built-in operator node types for GraphReFly (map, filter, scan, switchMap, valve, gate, retry, …)
Documentation
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
//! Integration tests for `stratify_branch` (D199, Unit 5 Q9.2 of
//! `SESSION-rust-port-layer-boundary.md`).
//!
//! Substrate counterpart of TS `extra/composition/stratify.ts`. The
//! Graph-level wrapper (per-rule N branches sharing a single rules
//! state node) is binding-side; here we verify the per-branch routing
//! operator in isolation.

mod common;

use graphrefly_operators::stratify::stratify_branch;

use common::{OpRuntime, RecordedEvent, TestValue};

// =====================================================================
// Helpers
// =====================================================================

/// Build a classifier that captures a "branch name" + uses the rules
/// handle as an integer mode-selector. `mode == 0` → only even ints
/// match "evens"; `mode == 1` → only ints divisible by 3 match
/// "threes"; etc. Mirrors how the TS Graph wrapper captures each
/// rule's name + reads `latestRules.find(r => r.name === ...)
/// .classify(value)`.
///
/// For the test substrate, "branch X" classifies by `value % mode == X`
/// for some `mode` carried in the rules handle.
fn make_modulo_classifier(rt: &OpRuntime, expected_remainder: i64) -> graphrefly_core::FnId {
    let binding = rt.binding.clone();
    rt.binding
        .register_stratify_classifier(Box::new(move |rules_h, value_h| {
            let mode = binding.deref(rules_h).int();
            let value = binding.deref(value_h).int();
            if mode == 0 {
                return false; // "no rules active" mode
            }
            value.rem_euclid(mode) == expected_remainder
        }))
}

// =====================================================================
// Basic routing — classifier match emits DATA; miss drops silently
// =====================================================================

#[test]
fn stratify_routes_matching_values_only() {
    let rt = OpRuntime::new();
    let source = rt.state_int(None);
    let rules = rt.state_int(Some(2)); // mode = 2 (even-vs-odd split)

    // Branch "evens": matches when value % 2 == 0.
    let classifier_evens = make_modulo_classifier(&rt, 0);
    let evens = stratify_branch(
        rt.core(),
        &rt.producer_binding,
        source,
        rules,
        classifier_evens,
    );
    let rec_evens = rt.subscribe_recorder(evens);

    rt.emit_int(source, 1); // odd → drop
    rt.emit_int(source, 2); // even → emit
    rt.emit_int(source, 3); // odd → drop
    rt.emit_int(source, 4); // even → emit
    rt.settle(); // D246: pump deferred producer-sink emits owner-side.

    assert_eq!(
        rec_evens.data_values(),
        vec![TestValue::Int(2), TestValue::Int(4)]
    );
}

// =====================================================================
// Multi-branch parallel — same source feeds N branches with different
// classifier semantics. Each branch independently filters.
// =====================================================================

#[test]
fn stratify_multi_branch_independent_routing() {
    let rt = OpRuntime::new();
    let source = rt.state_int(None);
    let rules = rt.state_int(Some(3)); // mode = 3

    let classifier_zeros = make_modulo_classifier(&rt, 0);
    let zeros = stratify_branch(
        rt.core(),
        &rt.producer_binding,
        source,
        rules,
        classifier_zeros,
    );

    let classifier_ones = make_modulo_classifier(&rt, 1);
    let ones = stratify_branch(
        rt.core(),
        &rt.producer_binding,
        source,
        rules,
        classifier_ones,
    );

    let classifier_twos = make_modulo_classifier(&rt, 2);
    let twos = stratify_branch(
        rt.core(),
        &rt.producer_binding,
        source,
        rules,
        classifier_twos,
    );

    let rec_zeros = rt.subscribe_recorder(zeros);
    let rec_ones = rt.subscribe_recorder(ones);
    let rec_twos = rt.subscribe_recorder(twos);

    for n in 0..9 {
        rt.emit_int(source, n);
    }
    rt.settle(); // D246: pump deferred producer-sink emits owner-side.

    assert_eq!(
        rec_zeros.data_values(),
        vec![TestValue::Int(0), TestValue::Int(3), TestValue::Int(6)],
    );
    assert_eq!(
        rec_ones.data_values(),
        vec![TestValue::Int(1), TestValue::Int(4), TestValue::Int(7)],
    );
    assert_eq!(
        rec_twos.data_values(),
        vec![TestValue::Int(2), TestValue::Int(5), TestValue::Int(8)],
    );
}

// =====================================================================
// Reactive rules — updating the rules state mid-stream changes
// classification for FUTURE items. TS parity: "rule updates affect
// future items only".
// =====================================================================

#[test]
fn stratify_reactive_rules_change_future_classification() {
    let rt = OpRuntime::new();
    let source = rt.state_int(None);
    let rules = rt.state_int(Some(2)); // start mode = 2

    let classifier_zeros = make_modulo_classifier(&rt, 0);
    let zeros = stratify_branch(
        rt.core(),
        &rt.producer_binding,
        source,
        rules,
        classifier_zeros,
    );
    let rec = rt.subscribe_recorder(zeros);

    // Under mode=2: emits where value % 2 == 0. Use distinct values so
    // identity-equals dedup doesn't suppress emissions on repeat (the
    // state node's identity equals fires RESOLVED-only when the same
    // handle is emitted twice).
    rt.emit_int(source, 2); // 2 % 2 == 0 → emit
    rt.emit_int(source, 5); // 5 % 2 == 1 → drop

    // Update rules → mode=3.
    rt.emit_int(rules, 3);

    // Under mode=3: emits where value % 3 == 0.
    rt.emit_int(source, 9); // 9 % 3 == 0 → emit
    rt.emit_int(source, 7); // 7 % 3 == 1 → drop
    rt.emit_int(source, 12); // 12 % 3 == 0 → emit
    rt.settle(); // D246: pump deferred producer-sink emits owner-side.

    assert_eq!(
        rec.data_values(),
        vec![TestValue::Int(2), TestValue::Int(9), TestValue::Int(12)],
    );
}

// =====================================================================
// No-rules sentinel — when rules is in sentinel state (no initial),
// the classifier never fires; all source DATA drops silently.
// =====================================================================

#[test]
fn stratify_no_rules_sentinel_drops_all() {
    let rt = OpRuntime::new();
    let source = rt.state_int(None);
    let rules = rt.state_int(None); // SENTINEL — never emits DATA

    let classifier = make_modulo_classifier(&rt, 0);
    let branch = stratify_branch(rt.core(), &rt.producer_binding, source, rules, classifier);
    let rec = rt.subscribe_recorder(branch);

    rt.emit_int(source, 2);
    rt.emit_int(source, 4);

    assert!(
        rec.data_values().is_empty(),
        "no rules → no classifier fire → empty stream"
    );
}

// =====================================================================
// Source COMPLETE — forwards COMPLETE downstream.
// =====================================================================

#[test]
fn stratify_forwards_source_complete() {
    let rt = OpRuntime::new();
    let source = rt.state_int(None);
    let rules = rt.state_int(Some(2));

    let classifier = make_modulo_classifier(&rt, 0);
    let branch = stratify_branch(rt.core(), &rt.producer_binding, source, rules, classifier);
    let rec = rt.subscribe_recorder(branch);

    rt.emit_int(source, 2);
    rt.core().complete(source);
    rt.settle(); // D246: pump deferred producer-sink emits owner-side.

    assert_eq!(rec.data_values(), vec![TestValue::Int(2)]);
    assert!(rec.events().contains(&RecordedEvent::Complete));
}

// =====================================================================
// Source ERROR — forwards ERROR downstream.
// =====================================================================

#[test]
fn stratify_forwards_source_error() {
    let rt = OpRuntime::new();
    let source = rt.state_int(None);
    let rules = rt.state_int(Some(2));

    let classifier = make_modulo_classifier(&rt, 0);
    let branch = stratify_branch(rt.core(), &rt.producer_binding, source, rules, classifier);
    let rec = rt.subscribe_recorder(branch);

    let err_h = rt.intern(TestValue::Str("boom".into()));
    rt.core().error(source, err_h);
    rt.settle(); // D246: pump deferred producer-sink emits owner-side.

    assert!(
        rec.events()
            .iter()
            .any(|e| matches!(e, RecordedEvent::Error(TestValue::Str(s)) if s == "boom")),
        "expected Error(boom) in {:?}",
        rec.events()
    );
}

// =====================================================================
// Rules terminal absorbed — rules COMPLETE/ERROR does NOT propagate to
// the branch's downstream. Branch keeps its last-seen rules and
// continues. TS parity: "rules signals silently absorbed".
// =====================================================================

#[test]
fn stratify_absorbs_rules_complete_silently() {
    let rt = OpRuntime::new();
    let source = rt.state_int(None);
    let rules = rt.state_int(Some(2));

    let classifier = make_modulo_classifier(&rt, 0);
    let branch = stratify_branch(rt.core(), &rt.producer_binding, source, rules, classifier);
    let rec = rt.subscribe_recorder(branch);

    rt.emit_int(source, 2);
    rt.core().complete(rules); // rules terminates — branch unaffected
    rt.emit_int(source, 4); // still classified under cached mode=2
    rt.settle(); // D246: pump deferred producer-sink emits owner-side.

    assert_eq!(
        rec.data_values(),
        vec![TestValue::Int(2), TestValue::Int(4)]
    );
    assert!(
        !rec.events().contains(&RecordedEvent::Complete),
        "rules COMPLETE must NOT propagate to branch downstream",
    );
}

// =====================================================================
// F1 (QA 2026-05-14) — Source TEARDOWN forwarded downstream.
// Earlier impl handled tier 5 (COMPLETE/ERROR) but tier 6 (TEARDOWN)
// fell through to the catch-all, breaking parity with TS which
// forwards TEARDOWN via `actions.down([msg])`.
// =====================================================================

#[test]
fn stratify_forwards_source_teardown() {
    let rt = OpRuntime::new();
    let source = rt.state_int(None);
    let rules = rt.state_int(Some(2));

    let classifier = make_modulo_classifier(&rt, 0);
    let branch = stratify_branch(rt.core(), &rt.producer_binding, source, rules, classifier);
    let rec = rt.subscribe_recorder(branch);

    rt.emit_int(source, 2);
    rt.core().teardown(source);
    rt.settle(); // D246: pump deferred producer-sink emits owner-side.

    assert_eq!(rec.data_values(), vec![TestValue::Int(2)]);
    assert!(
        rec.events().contains(&RecordedEvent::Teardown),
        "source TEARDOWN must propagate downstream (TS parity); got {:?}",
        rec.events()
    );
}

// =====================================================================
// N1 (QA 2026-05-14) — Two-dep DIRTY gating: when source AND rules
// both update inside the same `core.batch()`, the source value MUST be
// classified under the NEW rules, not the old. Without gating, the
// source-sink could fire before the rules-sink installs the new cache,
// leading to stale-rules misclassification.
// =====================================================================

#[test]
fn stratify_same_wave_gating_uses_new_rules() {
    let rt = OpRuntime::new();
    let source = rt.state_int(None);
    let rules = rt.state_int(Some(2)); // mode = 2 (only evens match)

    // Branch matches `value % rules == 0`. Under mode=2, value 3 misses.
    // Under mode=3, value 3 matches.
    let classifier = make_modulo_classifier(&rt, 0);
    let branch = stratify_branch(rt.core(), &rt.producer_binding, source, rules, classifier);
    let rec = rt.subscribe_recorder(branch);

    // Update both rules and source in the same batch. Without gating,
    // the source-sink could see source DATA before the rules-sink
    // updates `latest_rules` → classify under old rules (mode=2) →
    // 3 % 2 == 1 → drop. With gating, both deps settle first, then
    // resolve under new rules (mode=3) → 3 % 3 == 0 → emit.
    {
        let _g = rt.core().begin_batch();
        rt.emit_int(rules, 3);
        rt.emit_int(source, 3);
    }
    rt.settle(); // D246: pump deferred producer-sink emits owner-side.

    assert_eq!(
        rec.data_values(),
        vec![TestValue::Int(3)],
        "same-wave rules+source update must classify under NEW rules \
         (mode=3 → 3%3==0 → emit); got {:?}",
        rec.data_values()
    );
}

// =====================================================================
// Refcount discipline — StratifyState::Drop releases the cached rules
// handle on producer deactivation.
// =====================================================================

/// QA F4 (2026-05-14) — replaces a prior test that promised "refcount
/// balanced" but only verified no-panic. This test snapshots
/// `live_handles()` before activation, after activation +
/// emit-and-deactivate, and asserts the count returns to baseline,
/// proving `StratifyState::Drop` releases the cached rules handle (and
/// any buffered source value) when the producer's last subscriber
/// drops.
#[test]
fn stratify_drop_releases_cached_rules_handle() {
    let rt = OpRuntime::new();
    let source = rt.state_int(None);
    let rules = rt.state_int(Some(2));

    // Baseline: rules.cache holds Int(2). Source cache is sentinel.
    let baseline_live = rt.binding.live_handles();

    let classifier = make_modulo_classifier(&rt, 0);
    let branch = stratify_branch(rt.core(), &rt.producer_binding, source, rules, classifier);

    {
        let rec = rt.subscribe_recorder(branch);
        rt.emit_int(source, 2); // matches → emit
        rt.emit_int(source, 5); // misses → drop (release inside operator)
        rt.emit_int(source, 4); // matches → emit
        drop(rec);
    }

    // Expected post-state:
    // - rules.cache still holds Int(2) (already in baseline)
    // - source.cache now holds Int(4) (delta vs baseline = +1)
    // - branch deactivated → StratifyState::Drop released its
    //   `latest_rules` retain on Int(2) (refcount returns to 1)
    // - All buffered source values were emitted or dropped during
    //   the wave; source_value should be None at deactivation
    // If StratifyState::Drop leaked the rules-cache retain, Int(2)
    // would have refcount 2 → live_handles would still count it
    // once but a sub-handle leak isn't visible here. The clearer
    // signal is delta = +1 (just source.cache for Int(4)).
    let post_live = rt.binding.live_handles();
    assert_eq!(
        post_live,
        baseline_live + 1,
        "expected delta of +1 (source.cache for Int(4)); baseline \
         {baseline_live}, post {post_live}. A larger delta means a \
         retained handle leaked through StratifyState::Drop or the \
         buffered-source-value cleanup path.",
    );
}