1
//! Codex adversarial review, seventh pass on this slice: 4 findings.
2
//! IMPORTANT: Codex's shell failed to start this run, so every finding was
3
//! a CODE-DERIVED PREDICTION, not an observed failure — each was
4
//! reproduced against a bare `nms` sandbox (using `primary-entity-idx`,
5
//! the non-foldable runtime call available there) before any fix landed.
6
//! Two of the four predicted VALUES turned out to be wrong (noted below);
7
//! the underlying GAPS they predicted were real in both cases, and are
8
//! fixed here. `n` stands for `(rpc-protocol-version)`, which is 1.
9

            
10
use super::*;
11

            
12
// --- item 1: `reject_definition_under_runtime_guard` checked only
13
// `and_or_depth`, ignoring `cond_depth` — so a definition inside a false
14
// COND clause registered anyway, sidestepping round 6's fix (which only
15
// covered AND/OR). Fixed by switching it to
16
// `SymbolTable::is_under_any_runtime_guard` (AND/OR **or** COND), matching
17
// `reject_compile_time_setf_under_runtime_guard`'s existing scope.
18
//
19
// PREDICTION DISPROVEN (partially): Codex predicted the form below
20
// evaluates to 7 (buggy) and must be 9. In fact, given this slice's
21
// established policy (round 6 item 1: a FRESH definition under ANY runtime
22
// guard registers unconditionally and so is REJECTED outright, not
23
// silently skipped), the CORRECT fixed behavior is a compile error, not a
24
// clean "9" — the same as an equivalent AND/OR guard already produces. The
25
// underlying gap (cond_depth ignored) was real and is fixed; the specific
26
// predicted VALUE was not achievable without abandoning that established
27
// reject-outright policy, so this test asserts the compile error instead. ---
28

            
29
#[tokio::test]
30
1
async fn a_definition_under_a_false_cond_clause_is_rejected_not_silently_registered() {
31
1
    let form = "(progn (cond ((> (rpc-protocol-version) 5) (defvar codex-r9-y 7))) \
32
1
                 (defvar codex-r9-y 9) codex-r9-y)";
33
1
    assert_error_code(form, "compile").await;
34
1
}
35

            
36
#[tokio::test]
37
1
async fn a_macro_emitted_definition_under_a_false_cond_clause_is_also_rejected() {
38
    // Distinct from round 8 item 6: THIS macro EXPANDS TO a `(defvar ...)`
39
    // form (returned as generated code, compiled for real against the
40
    // caller's guarded table), rather than executing a `defvar` as part of
41
    // evaluating its own body (which round 8 item 6 already covers via
42
    // `suspend_runtime_guards` on the private expansion environment).
43
1
    let form = "(progn (defmacro codex-r9-m () '(defvar codex-r9-my 7)) \
44
1
                 (cond ((> (rpc-protocol-version) 5) (codex-r9-m))) \
45
1
                 (defvar codex-r9-my 9) codex-r9-my)";
46
1
    assert_error_code(form, "compile").await;
47
1
}
48

            
49
// --- item 2: `compile_setf_for_stack`'s const place branch
50
// never called `reject_compile_time_setf_under_runtime_guard` (unlike the
51
// effect handler), so a value-position SETF under a runtime guard mutated
52
// compile-time state unconditionally. Fixed by adding the same rejection
53
// there, and in the eval-only `setf` fn too (for a non-runtime-local
54
// place), so classification and codegen agree on what's rejected.
55
//
56
// PREDICTION DISPROVEN (partially): Codex predicted the buggy value "new"
57
// and a correct value of "old" (silently skip the write). Given this
58
// slice's established policy (a compile-time-only mutation under a runtime
59
// guard is REJECTED outright, not silently skipped — see round 4/6's SETF
60
// guard), the correct fixed behavior is a compile error, matching the
61
// existing DEFPARAMETER-initializer SETF-under-guard tests. The underlying
62
// gap (compile_setf_for_stack skipping the check) was real and is fixed;
63
// this test asserts the compile error rather than "old". ---
64

            
65
#[tokio::test]
66
1
async fn stack_position_setf_on_a_stored_global_under_a_runtime_guard_is_a_runtime_store() {
67
    // 0014 Stage 2: a global the unit stores to is a runtime cell, so the
68
    // guarded store runs only when its clause is taken — never here.
69
1
    let form = "(progn (defparameter codex-r9-z \"old\") \
70
1
                 (defparameter codex-r9-result \
71
1
                   (cond ((> (rpc-protocol-version) 5) (setf codex-r9-z \"new\")) (t \"ok\"))) \
72
1
                 codex-r9-z)";
73
1
    assert_values(&[(form, "\"old\"")]).await;
74
1
}
75

            
76
// --- item 3: effect-position COND entered the guard BEFORE
77
// `compile_test_condition` compiled the FIRST runtime clause's own test —
78
// reintroducing, in effect position, the same regression round 6 fixed in
79
// stack/eval position (cond.rs item 3 there). Fixed by compiling the first
80
// runtime test before entering the guard, then holding the guard across
81
// its body and the remaining clauses. ---
82

            
83
#[tokio::test]
84
1
async fn effect_position_cond_first_clauses_own_test_mutation_is_not_rejected_as_guarded() {
85
1
    let form = "(progn (defparameter codex-r9-x 0) \
86
1
                 (cond ((progn (setf codex-r9-x 1) (> (rpc-protocol-version) -1)) 7)) \
87
1
                 codex-r9-x)";
88
1
    assert_values(&[(form, "1")]).await;
89
1
}
90

            
91
#[tokio::test]
92
1
async fn effect_position_cond_first_runtime_clause_after_a_constant_false_prefix_is_not_rejected() {
93
1
    let form = "(progn (defparameter codex-r9-x2 0) \
94
1
                 (cond (nil 99) \
95
1
                       ((progn (setf codex-r9-x2 1) (> (rpc-protocol-version) -1)) 7)) \
96
1
                 codex-r9-x2)";
97
1
    assert_values(&[(form, "1")]).await;
98
1
}