1
//! Codex adversarial review, fifth pass on this slice: three findings, all
2
//! regressions/gaps in round 4's own fixes. `n` stands for
3
//! `(rpc-protocol-version)`, which is 1. (Every repro was first verified
4
//! against a bare `nms` sandbox using `primary-entity-idx` — the
5
//! non-foldable runtime call available there — while developing the fix,
6
//! then ported to `rpc-protocol-version` for this harness.)
7

            
8
use super::*;
9

            
10
// --- item 1: DEFVAR/DEFPARAMETER's round-4 fix probed the initializer on a
11
// disposable clone and, for a RUNTIME result, left it unapplied — assuming a
12
// later `promote_def_binding` codegen step would compile it for real. But
13
// `defvar`/`defparameter` are ALSO invoked as a pure `spec.eval` call (e.g.
14
// classifying a nested definition inside an EQ?/EQL/EQUAL operand), where no
15
// such codegen step ever follows, so the initializer's effects were silently
16
// lost. Fixed by splitting the eval-only entry point (applies effects exactly
17
// once, directly, unconditionally) from the codegen preparation path (still
18
// probes on a clone and defers to `finish_def_binding`). ---
19

            
20
#[tokio::test]
21
1
async fn eval_only_defvar_inside_eq_applies_its_setf_effect_exactly_once() {
22
1
    let form = "(progn (defparameter codex-r7-x 1) \
23
1
                 (eq? (progn (defvar codex-r7-y (progn (setf codex-r7-x 7) (rpc-protocol-version))) 7) codex-r7-x))";
24
1
    assert_values(&[(form, "t")]).await;
25
1
}
26

            
27
#[tokio::test]
28
1
async fn eval_only_defparameter_inside_eq_applies_its_setf_effect_exactly_once() {
29
1
    let form = "(progn (defparameter codex-r7-x2 1) \
30
1
                 (eq? (progn (defparameter codex-r7-y2 (progn (setf codex-r7-x2 7) (rpc-protocol-version))) 7) codex-r7-x2))";
31
1
    assert_values(&[(form, "t")]).await;
32
1
}
33

            
34
#[tokio::test]
35
1
async fn eval_only_defvar_inside_eql_applies_its_setf_effect_exactly_once() {
36
1
    let form = "(progn (defparameter codex-r7-x3 1) \
37
1
                 (eql (progn (defvar codex-r7-y3 (progn (setf codex-r7-x3 7) (rpc-protocol-version))) 7) codex-r7-x3))";
38
1
    assert_values(&[(form, "t")]).await;
39
1
}
40

            
41
#[tokio::test]
42
1
async fn eval_only_defvar_inside_equal_applies_its_setf_effect_exactly_once() {
43
1
    let form = "(progn (defparameter codex-r7-x4 1) \
44
1
                 (equal (progn (defvar codex-r7-y4 (progn (setf codex-r7-x4 7) (rpc-protocol-version))) 7) codex-r7-x4))";
45
1
    assert_values(&[(form, "t")]).await;
46
1
}
47

            
48
#[tokio::test]
49
1
async fn a_nested_definition_inside_an_eval_only_defvar_initializer_is_visible_after() {
50
1
    let form = "(progn (eq? (progn (defvar codex-r7-q 42) nil) nil) codex-r7-q)";
51
1
    assert_values(&[(form, "42")]).await;
52
1
}
53

            
54
// --- item 2: an accepted definition operand (AND/OR) still discarded a
55
// CONSTANT-VALUED initializer's runtime effect — `finish_def_binding`
56
// (formerly `promote_def_binding`) only compiled the initializer when its
57
// classified value was `WasmRuntime`, so a constant-valued initializer that
58
// merely CONTAINS a runtime effect (e.g. a nested `setf` on a wasm-backed
59
// place) never emitted that store. Fixed by always compiling such an
60
// initializer once, for effect, when its value classifies as constant. ---
61

            
62
#[tokio::test]
63
1
async fn and_definition_operand_still_applies_a_constant_valued_initializers_runtime_effect() {
64
1
    let form = "(let ((x (rpc-protocol-version))) \
65
1
                 (and t (defvar codex-r7-y5 (progn (setf x 9) 7))) x)";
66
1
    assert_values(&[(form, "9")]).await;
67
1
}
68

            
69
#[tokio::test]
70
1
async fn or_definition_operand_still_applies_a_constant_valued_initializers_runtime_effect() {
71
1
    let form = "(let ((x (rpc-protocol-version))) \
72
1
                 (or nil (defparameter codex-r7-z5 (progn (setf x 9) 7))) x)";
73
1
    assert_values(&[(form, "9")]).await;
74
1
}
75

            
76
#[tokio::test]
77
1
async fn defvar_already_bound_still_skips_its_initializer_entirely_under_codegen() {
78
    // DEFVAR's "don't overwrite" no-op must still never even evaluate the
79
    // initializer once bound — regression guard for `finish_def_binding`'s
80
    // `PreparedInit::None` path staying a true no-op.
81
1
    let form = "(progn (defparameter codex-r7-w 1) \
82
1
                 (and t (defvar codex-r7-w (progn (setf codex-r7-w 99) 2))) codex-r7-w)";
83
1
    assert_values(&[(form, "1")]).await;
84
1
}
85

            
86
// --- item 3: for a COND clause WITH A BODY, `enter_cond_chain` ran before
87
// the test itself was compiled (the test's real compilation happened INSIDE
88
// `if_form`, called after the guard was already entered) — so the test's own
89
// unconditional mutation was wrongly rejected as occurring under a runtime
90
// guard. Fixed by compiling (codegen) / evaluating (eval mirror) the test
91
// into a held placeholder BEFORE entering the guard, exactly as the bodyless
92
// clause path already did, and reusing `clause_if_args` for both shapes. ---
93

            
94
#[tokio::test]
95
1
async fn cond_with_body_clauses_own_test_mutation_is_not_rejected_as_guarded() {
96
1
    let form = "(progn (defparameter codex-r7-cx 0) \
97
1
                 (cond ((progn (setf codex-r7-cx 1) (> (rpc-protocol-version) 0)) 7) (t 9)))";
98
1
    assert_values(&[(form, "7")]).await;
99
1
}
100

            
101
#[tokio::test]
102
1
async fn cond_with_body_second_clauses_own_test_mutation_is_not_rejected_as_guarded() {
103
1
    let form = "(progn (defparameter codex-r7-cy 0) \
104
1
                 (cond (nil 1) ((progn (setf codex-r7-cy 1) (> (rpc-protocol-version) 0)) 7) (t 9)))";
105
1
    assert_values(&[(form, "7")]).await;
106
1
}
107

            
108
#[tokio::test]
109
1
async fn cond_with_body_clause_chain_still_leaves_the_guard_counter_balanced() {
110
    // Regression guard: the item-3 reordering must not reintroduce a leak —
111
    // a failing runtime clause followed by an unrelated, otherwise-valid
112
    // COND against the SAME session-persistent SymbolTable must still
113
    // compile.
114
1
    let mut session = Session::new(ScriptCtx::new(uuid::Uuid::nil())).expect("Session::new");
115
1
    let failing = session
116
1
        .handle_form(
117
1
            "(:id 1 :form (cond ((> (rpc-protocol-version) 0) (undefined-codex-r7-symbol)) (t 1)))",
118
1
        )
119
1
        .await;
120
1
    assert!(
121
1
        failing.contains(":code"),
122
        "expected the undefined symbol to fail: {failing}"
123
    );
124
1
    let recovered = session
125
1
        .handle_form("(:id 2 :form (cond ((> (rpc-protocol-version) 0) 5) (t 6)))")
126
1
        .await;
127
1
    assert_eq!(
128
1
        recovered, "(:id 2 :value 5)",
129
1
        "session did not recover: {recovered}"
130
1
    );
131
1
}