wm-workspace 9.1.5

Workspace scoping and multi-checkout coordination for WhiteMagic sessions.
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
//! Global workspace bus — publish/subscribe event bus with salience arbitration.
//!
//! All cognitive cores publish events to the workspace. The workspace
//! arbitrates attention via the spotlight mechanism and maintains a
//! ring buffer of recent events.
//!
//! The bus uses `tokio::sync::broadcast` for multiple subscribers. Slow
//! subscribers may miss events (lossy by design — the workspace is not
//! a reliable queue, it's an attention mechanism).

use crate::event::{CoreId, EventType, WorkspaceEvent};
use crate::spotlight::Spotlight;
use std::sync::atomic::{AtomicU64, Ordering};
use thiserror::Error;

/// Maximum number of events in the backlog ring buffer.
pub const BACKLOG_SIZE: usize = 256;

/// Default broadcast channel capacity.
pub const CHANNEL_CAPACITY: usize = 512;

/// Error type for workspace operations.
#[derive(Debug, Clone, Error)]
pub enum WorkspaceError {
    /// Event payload too large.
    #[error("event payload too large: {0} bytes")]
    PayloadTooLarge(usize),
}

/// Workspace statistics.
#[derive(Debug, Clone, Default)]
pub struct WorkspaceStats {
    /// Total events published.
    pub events_published: u64,
    /// Total spotlight transfers.
    pub spotlight_transfers: u64,
    /// Total arbitration cycles.
    pub arbitration_cycles: u64,
    /// Events per core.
    pub events_per_core: std::collections::HashMap<CoreId, u64>,
    /// Events per type.
    pub events_per_type: std::collections::HashMap<EventType, u64>,
}

/// The global workspace — coordinates attention across all cognitive cores.
///
/// Combines:
/// - A broadcast event bus (tokio::sync::broadcast)
/// - A spotlight tracker for salience-based arbitration
/// - A ring buffer backlog of recent events
pub struct GlobalWorkspace {
    /// Spotlight tracker.
    spotlight: Spotlight,
    /// Ring buffer of recent events.
    backlog: std::collections::VecDeque<WorkspaceEvent>,
    /// Total events published.
    events_published: AtomicU64,
    /// Events per core.
    events_per_core: std::collections::HashMap<CoreId, u64>,
    /// Events per type.
    events_per_type: std::collections::HashMap<EventType, u64>,
}

impl std::fmt::Debug for GlobalWorkspace {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("GlobalWorkspace")
            .field("spotlight", &self.spotlight)
            .field("backlog_len", &self.backlog.len())
            .field(
                "events_published",
                &self.events_published.load(Ordering::Relaxed),
            )
            .finish_non_exhaustive()
    }
}

impl Default for GlobalWorkspace {
    fn default() -> Self {
        Self::new()
    }
}

impl GlobalWorkspace {
    /// Create a new global workspace with default settings.
    #[must_use]
    pub fn new() -> Self {
        Self {
            spotlight: Spotlight::default(),
            backlog: std::collections::VecDeque::with_capacity(BACKLOG_SIZE),
            events_published: AtomicU64::new(0),
            events_per_core: std::collections::HashMap::new(),
            events_per_type: std::collections::HashMap::new(),
        }
    }

    /// Create a workspace with a custom spotlight half-life.
    #[must_use]
    pub fn with_half_life(half_life: std::time::Duration) -> Self {
        Self {
            spotlight: Spotlight::new(half_life),
            backlog: std::collections::VecDeque::with_capacity(BACKLOG_SIZE),
            events_published: AtomicU64::new(0),
            events_per_core: std::collections::HashMap::new(),
            events_per_type: std::collections::HashMap::new(),
        }
    }

    /// Publish an event to the workspace.
    ///
    /// The event is:
    /// 1. Added to the backlog ring buffer
    /// 2. Arbitrated against the current spotlight
    /// 3. Counted in statistics
    ///
    /// Returns `true` if the event won the spotlight.
    pub fn publish(&mut self, event: &WorkspaceEvent) -> bool {
        self.events_published.fetch_add(1, Ordering::Relaxed);

        // Update per-core and per-type counts
        *self.events_per_core.entry(event.core).or_insert(0) += 1;
        *self.events_per_type.entry(event.event_type).or_insert(0) += 1;

        // Add to backlog (evict oldest if full)
        if self.backlog.len() >= BACKLOG_SIZE {
            self.backlog.pop_front();
        }
        self.backlog.push_back(event.clone());

        // Arbitrate
        let won = self.spotlight.arbitrate(event);

        if won {
            tracing::debug!(
                core = %event.core,
                event_type = %event.event_type,
                salience = event.composite_salience(),
                "spotlight won"
            );
        }

        won
    }

    /// Publish a simple event with default urgency.
    pub fn publish_simple(
        &mut self,
        core: CoreId,
        event_type: EventType,
        novelty: f32,
        confidence: f32,
        payload: serde_json::Value,
    ) -> bool {
        let event =
            WorkspaceEvent::with_default_urgency(core, event_type, novelty, confidence, payload);
        self.publish(&event)
    }

    /// Publish multiple events at once. The highest-salience event is
    /// arbitrated against the current spotlight.
    ///
    /// Returns the index of the winning event, or `None` if none won.
    pub fn publish_batch(&mut self, events: &[WorkspaceEvent]) -> Option<usize> {
        if events.is_empty() {
            return None;
        }

        // Add all to backlog and counts
        for event in events {
            self.events_published.fetch_add(1, Ordering::Relaxed);
            *self.events_per_core.entry(event.core).or_insert(0) += 1;
            *self.events_per_type.entry(event.event_type).or_insert(0) += 1;

            if self.backlog.len() >= BACKLOG_SIZE {
                self.backlog.pop_front();
            }
            self.backlog.push_back(event.clone());
        }

        // Arbitrate batch
        self.spotlight.arbitrate_batch(events)
    }

    /// Get the current spotlight entry.
    #[must_use]
    pub const fn spotlight(&self) -> Option<&crate::spotlight::SpotlightEntry> {
        self.spotlight.current()
    }

    /// Get the core currently holding the spotlight.
    #[must_use]
    pub fn spotlight_core(&self) -> Option<CoreId> {
        self.spotlight.current_core()
    }

    /// Get the current spotlight strength (0.0 to 1.0).
    #[must_use]
    pub fn spotlight_strength(&self) -> f32 {
        self.spotlight.strength()
    }

    /// Get the recent event backlog (newest first).
    pub const fn backlog(&self) -> &std::collections::VecDeque<WorkspaceEvent> {
        &self.backlog
    }

    /// Get the last N events from the backlog.
    pub fn recent_events(&self, n: usize) -> Vec<&WorkspaceEvent> {
        self.backlog.iter().rev().take(n).collect()
    }

    /// Total events published.
    #[must_use]
    pub fn events_published(&self) -> u64 {
        self.events_published.load(Ordering::Relaxed)
    }

    /// Spotlight transfer count.
    #[must_use]
    pub const fn spotlight_transfers(&self) -> u64 {
        self.spotlight.transfer_count()
    }

    /// Spotlight arbitration cycle count.
    #[must_use]
    pub const fn arbitration_cycles(&self) -> u64 {
        self.spotlight.arbitration_count()
    }

    /// Get the number of times a core has held the spotlight.
    #[must_use]
    pub fn core_hold_count(&self, core: CoreId) -> u64 {
        self.spotlight.core_hold_count(core)
    }

    /// Get the number of events published by a core.
    #[must_use]
    pub fn core_event_count(&self, core: CoreId) -> u64 {
        self.events_per_core.get(&core).copied().unwrap_or(0)
    }

    /// Get the number of events of a specific type.
    #[must_use]
    pub fn event_type_count(&self, event_type: EventType) -> u64 {
        self.events_per_type.get(&event_type).copied().unwrap_or(0)
    }

    /// Collect workspace statistics.
    pub fn stats(&self) -> WorkspaceStats {
        WorkspaceStats {
            events_published: self.events_published.load(Ordering::Relaxed),
            spotlight_transfers: self.spotlight.transfer_count(),
            arbitration_cycles: self.spotlight.arbitration_count(),
            events_per_core: self.events_per_core.clone(),
            events_per_type: self.events_per_type.clone(),
        }
    }

    /// Clear the spotlight.
    pub const fn clear_spotlight(&mut self) {
        self.spotlight.clear();
    }

    /// Clear the event backlog.
    pub fn clear_backlog(&mut self) {
        self.backlog.clear();
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::salience::Salience;

    fn make_event(core: CoreId, event_type: EventType, salience: f32) -> WorkspaceEvent {
        WorkspaceEvent::new(
            core,
            event_type,
            Salience::new(salience, salience, salience),
            serde_json::json!({"test": true}),
        )
    }

    #[test]
    fn workspace_default() {
        let ws = GlobalWorkspace::default();
        assert_eq!(ws.events_published(), 0);
        assert!(ws.spotlight().is_none());
        assert_eq!(ws.backlog().len(), 0);
    }

    #[test]
    fn publish_first_event_wins_spotlight() {
        let mut ws = GlobalWorkspace::new();
        let event = make_event(CoreId::Citta, EventType::AttentionRequest, 0.5);
        assert!(ws.publish(&event));
        assert_eq!(ws.spotlight_core(), Some(CoreId::Citta));
        assert_eq!(ws.events_published(), 1);
    }

    #[test]
    fn publish_lower_salience_does_not_win() {
        let mut ws = GlobalWorkspace::new();
        let high = make_event(CoreId::Citta, EventType::AttentionRequest, 0.8);
        assert!(ws.publish(&high));

        let low = make_event(CoreId::Dream, EventType::Reward, 0.3);
        assert!(!ws.publish(&low));
        assert_eq!(ws.spotlight_core(), Some(CoreId::Citta));
    }

    #[test]
    fn publish_higher_salience_wins() {
        let mut ws = GlobalWorkspace::new();
        let low = make_event(CoreId::Citta, EventType::AttentionRequest, 0.3);
        assert!(ws.publish(&low));

        let high = make_event(CoreId::Dream, EventType::NovelDetection, 0.9);
        assert!(ws.publish(&high));
        assert_eq!(ws.spotlight_core(), Some(CoreId::Dream));
    }

    #[test]
    fn backlog_ring_buffer_evicts_old() {
        let mut ws = GlobalWorkspace::new();
        for i in 0..(BACKLOG_SIZE + 50) {
            let event = make_event(CoreId::Custom(i as u16), EventType::DriveUpdate, 0.1);
            ws.publish(&event);
        }
        assert_eq!(ws.backlog().len(), BACKLOG_SIZE);
    }

    #[test]
    fn recent_events_returns_newest_first() {
        let mut ws = GlobalWorkspace::new();
        ws.publish(&make_event(CoreId::Citta, EventType::Reward, 0.1));
        ws.publish(&make_event(CoreId::Dream, EventType::Reward, 0.1));
        ws.publish(&make_event(CoreId::Reflex, EventType::Reward, 0.1));

        let recent = ws.recent_events(2);
        assert_eq!(recent.len(), 2);
        assert_eq!(recent[0].core, CoreId::Reflex);
        assert_eq!(recent[1].core, CoreId::Dream);
    }

    #[test]
    fn publish_batch() {
        let mut ws = GlobalWorkspace::new();
        let events = vec![
            make_event(CoreId::Citta, EventType::AttentionRequest, 0.3),
            make_event(CoreId::Dream, EventType::NovelDetection, 0.7),
            make_event(CoreId::Reflex, EventType::SafetyAlert, 0.5),
        ];
        let winner = ws.publish_batch(&events);
        assert_eq!(winner, Some(1)); // Dream has highest salience
        assert_eq!(ws.spotlight_core(), Some(CoreId::Dream));
        assert_eq!(ws.events_published(), 3);
        assert_eq!(ws.backlog().len(), 3);
    }

    #[test]
    fn publish_batch_empty() {
        let mut ws = GlobalWorkspace::new();
        assert!(ws.publish_batch(&[]).is_none());
    }

    #[test]
    fn core_event_count() {
        let mut ws = GlobalWorkspace::new();
        ws.publish(&make_event(CoreId::Citta, EventType::Reward, 0.1));
        ws.publish(&make_event(CoreId::Citta, EventType::Reward, 0.1));
        ws.publish(&make_event(CoreId::Dream, EventType::Reward, 0.1));
        assert_eq!(ws.core_event_count(CoreId::Citta), 2);
        assert_eq!(ws.core_event_count(CoreId::Dream), 1);
        assert_eq!(ws.core_event_count(CoreId::Reflex), 0);
    }

    #[test]
    fn event_type_count() {
        let mut ws = GlobalWorkspace::new();
        ws.publish(&make_event(CoreId::Citta, EventType::Error, 0.1));
        ws.publish(&make_event(CoreId::Dream, EventType::Error, 0.1));
        ws.publish(&make_event(CoreId::Reflex, EventType::SafetyAlert, 0.1));
        assert_eq!(ws.event_type_count(EventType::Error), 2);
        assert_eq!(ws.event_type_count(EventType::SafetyAlert), 1);
    }

    #[test]
    fn stats_collection() {
        let mut ws = GlobalWorkspace::new();
        ws.publish(&make_event(CoreId::Citta, EventType::Reward, 0.5));
        ws.publish(&make_event(CoreId::Dream, EventType::NovelDetection, 0.8));

        let stats = ws.stats();
        assert_eq!(stats.events_published, 2);
        assert_eq!(stats.spotlight_transfers, 2);
        assert_eq!(stats.arbitration_cycles, 2);
        assert_eq!(stats.events_per_core.get(&CoreId::Citta), Some(&1));
        assert_eq!(stats.events_per_core.get(&CoreId::Dream), Some(&1));
    }

    #[test]
    fn publish_simple_with_default_urgency() {
        let mut ws = GlobalWorkspace::new();
        let won = ws.publish_simple(
            CoreId::Reflex,
            EventType::SafetyAlert,
            0.9,
            0.9,
            serde_json::json!({"alert": "collision"}),
        );
        // SafetyAlert has urgency 1.0, so composite = 1.0 * 0.9 * 0.9 = 0.81
        assert!(won);
        assert_eq!(ws.spotlight_core(), Some(CoreId::Reflex));
    }

    #[test]
    fn clear_spotlight() {
        let mut ws = GlobalWorkspace::new();
        ws.publish(&make_event(CoreId::Citta, EventType::AttentionRequest, 0.5));
        assert!(ws.spotlight().is_some());
        ws.clear_spotlight();
        assert!(ws.spotlight().is_none());
    }

    #[test]
    fn clear_backlog() {
        let mut ws = GlobalWorkspace::new();
        ws.publish(&make_event(CoreId::Citta, EventType::Reward, 0.1));
        ws.publish(&make_event(CoreId::Dream, EventType::Reward, 0.1));
        assert_eq!(ws.backlog().len(), 2);
        ws.clear_backlog();
        assert_eq!(ws.backlog().len(), 0);
    }

    #[test]
    fn spotlight_strength_decays() {
        let mut ws = GlobalWorkspace::with_half_life(std::time::Duration::from_millis(50));
        ws.publish(&make_event(CoreId::Citta, EventType::AttentionRequest, 0.8));

        let s1 = ws.spotlight_strength();
        assert!(s1 > 0.48 && s1 < 0.53, "initial strength: {s1}");

        std::thread::sleep(std::time::Duration::from_millis(50));
        let s2 = ws.spotlight_strength();
        assert!(s2 < s1);
    }

    #[test]
    fn core_hold_count() {
        let mut ws = GlobalWorkspace::new();
        ws.publish(&make_event(CoreId::Citta, EventType::AttentionRequest, 0.5));
        ws.publish(&make_event(CoreId::Dream, EventType::NovelDetection, 0.8));
        ws.publish(&make_event(CoreId::Citta, EventType::Error, 0.9));

        assert_eq!(ws.core_hold_count(CoreId::Citta), 2);
        assert_eq!(ws.core_hold_count(CoreId::Dream), 1);
    }

    #[test]
    fn backlog_max_size() {
        let mut ws = GlobalWorkspace::new();
        for i in 0..100 {
            ws.publish(&make_event(
                CoreId::Custom(i as u16),
                EventType::DriveUpdate,
                0.01,
            ));
        }
        assert!(ws.backlog().len() <= BACKLOG_SIZE);
    }
}