apcore 0.22.0

Schema-driven module standard for AI-perceivable interfaces
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
// Conformance tests for Registry on_load Ordering Invariants (Issue #65).
// Fixture: apcore/conformance/fixtures/registry_load_ordering.json
#![allow(clippy::pedantic)] // fixture-driven test file: casts and struct layouts follow fixture schema

use apcore::context::Context;
use apcore::errors::{ErrorCode, ModuleError};
use apcore::module::Module;
use apcore::registry::registry::{ModuleDescriptor, Registry};
use async_trait::async_trait;
use parking_lot::Mutex;
use serde_json::{json, Value};
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::Arc;
use std::time::{Duration, Instant};

// ---------------------------------------------------------------------------
// Fixture loading
// ---------------------------------------------------------------------------

fn find_fixtures_root() -> PathBuf {
    if let Ok(spec_repo) = std::env::var("APCORE_SPEC_REPO") {
        let p = PathBuf::from(&spec_repo)
            .join("conformance")
            .join("fixtures");
        if p.is_dir() {
            return p;
        }
        panic!("APCORE_SPEC_REPO={spec_repo} does not contain conformance/fixtures/");
    }
    let manifest_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
    let sibling = manifest_dir
        .parent()
        .unwrap()
        .join("apcore")
        .join("conformance")
        .join("fixtures");
    if sibling.is_dir() {
        return sibling;
    }
    panic!(
        "Cannot find apcore conformance fixtures.\n\
         Set APCORE_SPEC_REPO or clone apcore as a sibling of {}",
        manifest_dir.parent().unwrap().display()
    );
}

fn load_fixture() -> Value {
    let path = find_fixtures_root().join("registry_load_ordering.json");
    let content = std::fs::read_to_string(&path)
        .unwrap_or_else(|_| panic!("Failed to read fixture: {}", path.display()));
    serde_json::from_str(&content).unwrap_or_else(|e| panic!("Invalid JSON: {e}"))
}

fn fixture_case<'a>(fixture: &'a Value, id: &str) -> &'a Value {
    fixture["test_cases"]
        .as_array()
        .expect("test_cases must be an array")
        .iter()
        .find(|c| c["id"].as_str() == Some(id))
        .unwrap_or_else(|| panic!("test case '{id}' not found in fixture"))
}

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

fn make_descriptor(id: &str) -> ModuleDescriptor {
    ModuleDescriptor {
        module_id: id.to_string(),
        name: None,
        description: String::new(),
        documentation: None,
        input_schema: json!({}),
        output_schema: json!({}),
        version: "1.0.0".to_string(),
        tags: vec![],
        annotations: None,
        examples: vec![],
        metadata: HashMap::new(),
        display: None,
        sunset_date: None,
        dependencies: vec![],
        enabled: true,
    }
}

struct DelayedModule {
    delay_ms: u64,
    fail: bool,
}

#[async_trait]
impl Module for DelayedModule {
    #[allow(clippy::unnecessary_literal_bound)]
    fn description(&self) -> &str {
        "conformance-delayed"
    }
    fn input_schema(&self) -> Value {
        json!({})
    }
    fn output_schema(&self) -> Value {
        json!({})
    }
    fn on_load(&self) -> Result<(), ModuleError> {
        if self.delay_ms > 0 {
            std::thread::sleep(Duration::from_millis(self.delay_ms));
        }
        if self.fail {
            Err(ModuleError::new(
                ErrorCode::ModuleLoadError,
                "Failed to reach upstream during initialization",
            ))
        } else {
            Ok(())
        }
    }
    async fn execute(&self, _: Value, _: &Context<Value>) -> Result<Value, ModuleError> {
        Ok(json!({}))
    }
}

// ---------------------------------------------------------------------------
// Case: visibility_after_successful_on_load
// ---------------------------------------------------------------------------

#[test]
fn conformance_visibility_after_successful_on_load() {
    let fixture = load_fixture();
    let case = fixture_case(&fixture, "visibility_after_successful_on_load");

    let module_id = case["setup"]["module"]["id"].as_str().unwrap();
    let delay_ms = case["setup"]["module"]["on_load_delay_ms"]
        .as_u64()
        .unwrap_or(50);

    let registry = Arc::new(Registry::new());

    // Fixture: concurrent_check at 25ms during 50ms on_load — module must NOT be visible.
    // We simulate this by checking visibility during on_load via a side-channel.
    let visible_at_25ms: Arc<Mutex<Option<bool>>> = Arc::new(Mutex::new(None));
    {
        let reg_clone = Arc::clone(&registry);
        let mid = module_id.to_string();
        let vis = Arc::clone(&visible_at_25ms);

        struct VisModule {
            registry: Arc<Registry>,
            id: String,
            delay_ms: u64,
            visible_during_load: Arc<Mutex<Option<bool>>>,
        }
        #[async_trait]
        impl Module for VisModule {
            #[allow(clippy::unnecessary_literal_bound)]
            fn description(&self) -> &str {
                "vis"
            }
            fn input_schema(&self) -> Value {
                json!({})
            }
            fn output_schema(&self) -> Value {
                json!({})
            }
            fn on_load(&self) -> Result<(), ModuleError> {
                let visible = self.registry.get(&self.id).unwrap_or(None).is_some();
                *self.visible_during_load.lock() = Some(visible);
                if self.delay_ms > 0 {
                    std::thread::sleep(Duration::from_millis(self.delay_ms));
                }
                Ok(())
            }
            async fn execute(&self, _: Value, _: &Context<Value>) -> Result<Value, ModuleError> {
                Ok(json!({}))
            }
        }

        registry
            .register(
                &mid,
                Box::new(VisModule {
                    registry: reg_clone,
                    id: mid.clone(),
                    delay_ms,
                    visible_during_load: vis,
                }),
                make_descriptor(module_id),
            )
            .unwrap();
    }

    // Post-register: module must be visible
    let post_visible = case["expected"]["post_register_visible"].as_bool().unwrap();
    assert_eq!(
        registry.get(module_id).unwrap().is_some(),
        post_visible,
        "post_register_visible mismatch"
    );
    assert_eq!(
        registry
            .list(None, None, None)
            .contains(&module_id.to_string()),
        post_visible,
        "module must appear in list() after registration"
    );

    // During-load visibility check
    let concurrent_visible = case["expected"]["concurrent_check_visible"]
        .as_bool()
        .unwrap();
    let was_visible = visible_at_25ms.lock().unwrap_or(false);
    assert_eq!(
        was_visible, concurrent_visible,
        "module visibility during on_load: expected={concurrent_visible}, got={was_visible}"
    );
}

// ---------------------------------------------------------------------------
// Case: callback_failure_blocks_visibility
// ---------------------------------------------------------------------------

#[test]
fn conformance_callback_failure_blocks_visibility() {
    let fixture = load_fixture();
    let case = fixture_case(&fixture, "callback_failure_blocks_visibility");

    let module_id = case["setup"]["module"]["id"].as_str().unwrap();
    let expected_err_msg = case["setup"]["module"]["on_load_raises"]["message"]
        .as_str()
        .unwrap();

    let load_failed_ids: Arc<Mutex<Vec<String>>> = Arc::new(Mutex::new(Vec::new()));
    let registry = Registry::new();
    {
        let lf = Arc::clone(&load_failed_ids);
        registry.on_load_failed(Arc::new(move |id, _err| {
            lf.lock().push(id.to_string());
        }));
    }

    struct FailWithMsgModule {
        message: String,
    }
    #[async_trait]
    impl Module for FailWithMsgModule {
        #[allow(clippy::unnecessary_literal_bound)]
        fn description(&self) -> &str {
            "fail-with-msg"
        }
        fn input_schema(&self) -> Value {
            json!({})
        }
        fn output_schema(&self) -> Value {
            json!({})
        }
        fn on_load(&self) -> Result<(), ModuleError> {
            Err(ModuleError::new(ErrorCode::ModuleLoadError, &self.message))
        }
        async fn execute(&self, _: Value, _: &Context<Value>) -> Result<Value, ModuleError> {
            Ok(json!({}))
        }
    }

    let result = registry.register(
        module_id,
        Box::new(FailWithMsgModule {
            message: expected_err_msg.to_string(),
        }),
        make_descriptor(module_id),
    );

    // registration_raises
    assert!(
        result.is_err(),
        "register() must return Err when on_load raises"
    );
    let err = result.unwrap_err();
    assert!(
        err.message.contains(expected_err_msg),
        "error message must contain '{expected_err_msg}', got: {}",
        err.message
    );

    // post_register_visible: false
    assert!(
        !case["expected"]["post_register_visible"].as_bool().unwrap(),
        "fixture expects post_register_visible=false"
    );
    assert!(
        registry.get(module_id).unwrap().is_none(),
        "module must not be visible after failed on_load"
    );
    assert!(
        !registry
            .list(None, None, None)
            .contains(&module_id.to_string()),
        "module must not appear in list() after failed on_load"
    );

    // load_failed_event_emitted
    assert!(
        case["expected"]["load_failed_event_emitted"]
            .as_bool()
            .unwrap(),
        "fixture expects load_failed_event_emitted=true"
    );
    let failed_ids = load_failed_ids.lock();
    assert!(
        !failed_ids.is_empty(),
        "on_load_failed callback must be invoked"
    );
    assert_eq!(
        failed_ids[0], module_id,
        "callback receives correct module_id"
    );

    // Validate required keys in event payload (via callback)
    let required_keys: Vec<&str> = case["expected"]["load_failed_event"]["data_required_keys"]
        .as_array()
        .unwrap()
        .iter()
        .filter_map(|v| v.as_str())
        .collect();
    // The callback-based API gives us (module_id, &error) — no full ApCoreEvent.
    // Verify that module_id and error fields are present as described in the fixture.
    assert!(
        required_keys.contains(&"module_id"),
        "fixture requires 'module_id' in event data"
    );
    assert!(
        required_keys.contains(&"error_message"),
        "fixture requires 'error_message' in event data"
    );
}

// ---------------------------------------------------------------------------
// Case: concurrent_same_id_rejects_duplicate
// ---------------------------------------------------------------------------

#[test]
fn conformance_concurrent_same_id_rejects_duplicate() {
    let fixture = load_fixture();
    let case = fixture_case(&fixture, "concurrent_same_id_rejects_duplicate");

    let module_id = case["setup"]["module_a"]["id"].as_str().unwrap();
    let delay_ms = case["setup"]["module_a"]["on_load_delay_ms"]
        .as_u64()
        .unwrap_or(30);

    let registry = Arc::new(Registry::new());
    let r1 = Arc::clone(&registry);
    let r2 = Arc::clone(&registry);
    let mid = module_id.to_string();
    let mid2 = module_id.to_string();

    let h1 = std::thread::spawn(move || {
        r1.register(
            &mid,
            Box::new(DelayedModule {
                delay_ms,
                fail: false,
            }),
            make_descriptor(&mid),
        )
    });
    let h2 = std::thread::spawn(move || {
        r2.register(
            &mid2,
            Box::new(DelayedModule {
                delay_ms,
                fail: false,
            }),
            make_descriptor(&mid2),
        )
    });

    let r1 = h1.join().unwrap();
    let r2 = h2.join().unwrap();

    let ok_count = [&r1, &r2].iter().filter(|r| r.is_ok()).count();
    let err_count = [&r1, &r2].iter().filter(|r| r.is_err()).count();

    // one_succeeds: true
    assert!(
        case["expected"]["one_succeeds"].as_bool().unwrap(),
        "fixture expects one_succeeds=true"
    );
    assert_eq!(ok_count, 1, "exactly one registration must succeed");
    assert_eq!(err_count, 1, "exactly one registration must fail");

    // raised_error_code: DUPLICATE_MODULE_ID
    let err = [r1, r2]
        .into_iter()
        .find(Result::is_err)
        .unwrap()
        .unwrap_err();
    assert_eq!(
        err.code,
        ErrorCode::DuplicateModuleId,
        "concurrent same-ID must raise DuplicateModuleId, got {:?}: {}",
        err.code,
        err.message
    );

    // post_register_count: 1
    assert_eq!(
        registry.count(),
        case["expected"]["post_register_count"].as_u64().unwrap() as usize,
        "registry must contain exactly one module"
    );
}

// ---------------------------------------------------------------------------
// Case: concurrent_distinct_ids_run_in_parallel
// ---------------------------------------------------------------------------

#[test]
fn conformance_concurrent_distinct_ids_run_in_parallel() {
    let fixture = load_fixture();
    let case = fixture_case(&fixture, "concurrent_distinct_ids_run_in_parallel");

    let id_x = case["setup"]["module_x"]["id"].as_str().unwrap();
    let id_y = case["setup"]["module_y"]["id"].as_str().unwrap();
    let delay_ms = case["setup"]["module_x"]["on_load_delay_ms"]
        .as_u64()
        .unwrap_or(50);

    let registry = Arc::new(Registry::new());
    let r1 = Arc::clone(&registry);
    let r2 = Arc::clone(&registry);
    let mx = id_x.to_string();
    let my = id_y.to_string();

    let wall_clock_limit_ms = case["expected"]["wall_clock_ms_less_than"]
        .as_u64()
        .unwrap_or(90);

    let start = Instant::now();
    let h1 = std::thread::spawn(move || {
        r1.register(
            &mx,
            Box::new(DelayedModule {
                delay_ms,
                fail: false,
            }),
            make_descriptor(&mx),
        )
    });
    let h2 = std::thread::spawn(move || {
        r2.register(
            &my,
            Box::new(DelayedModule {
                delay_ms,
                fail: false,
            }),
            make_descriptor(&my),
        )
    });
    h1.join().unwrap().unwrap();
    h2.join().unwrap().unwrap();
    let elapsed = start.elapsed().as_millis();

    // both_succeed: true
    assert!(
        case["expected"]["both_succeed"].as_bool().unwrap(),
        "fixture expects both_succeed=true"
    );
    assert_eq!(
        registry.count(),
        case["expected"]["post_register_count"].as_u64().unwrap() as usize,
        "both modules must be registered"
    );

    // wall_clock_ms_less_than (proves per-module parallelism)
    assert!(
        elapsed < wall_clock_limit_ms as u128,
        "wall clock was {elapsed}ms; fixture requires < {wall_clock_limit_ms}ms \
         (distinct-ID on_load callbacks must run concurrently)"
    );
}