lau-agent-shell 0.1.0

PLATO agent shell — an agent in a room, with a game character on the outside
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
//! Lau Agent Shell — an agent in a PLATO room, with a game character on the outside.
//!
//! The shell wraps a PLATO agent and translates between:
//! - Inside: the agent processes readings, predicts, learns (PLATO semantics)
//! - Outside: the agent appears as a character in a voxel world (game semantics)
//!
//! The character's appearance, behavior, and abilities ALL reflect the agent's
//! internal state. A confident agent stands tall and glows. A confused one
//! flickers and wanders. A dissolved agent becomes a ghost.

use serde::{Deserialize, Serialize};
use std::collections::VecDeque;

/// The agent's internal state (PLATO side).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AgentState {
    pub id: String,
    pub room_id: String,
    pub vibe: f64,
    pub confidence: f64,
    pub phase: AgentPhase,
    pub readings_seen: usize,
    pub predictions_made: usize,
    pub accuracy: f64,
    pub energy: f64, // how much activity the agent has
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum AgentPhase {
    Gestating,
    Forming,
    Maturing,
    Stable,
    Dissolving,
    Dissolved,
}

/// The agent's outward appearance (game side).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CharacterAppearance {
    pub name: String,
    pub body_color: [f64; 3],
    pub glow_intensity: f64,
    pub scale: f64,
    pub opacity: f64,
    pub animation: CharacterAnimation,
    pub accessories: Vec<String>,
    pub expression: String,
}

#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub enum CharacterAnimation {
    Idle,
    Exploring,
    Thinking,
    Confident,
    Celebrating,
    Confused,
    Fading,
    Ghost,
}

/// An action the character takes in the game world.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CharacterAction {
    pub agent_id: String,
    pub kind: ActionKind,
    pub target: Option<String>,
    pub params: Vec<f64>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ActionKind {
    Move,
    Speak,
    Build,
    Observe,
    Teach,
    Celebrate,
    Emote,
}

/// The shell — bridges agent state to character appearance.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AgentShell {
    pub state: AgentState,
    pub appearance: CharacterAppearance,
    pub action_queue: VecDeque<CharacterAction>,
    pub personality_traits: Vec<String>,
}

impl AgentShell {
    pub fn new(id: &str, room_id: &str) -> Self {
        let state = AgentState {
            id: id.into(),
            room_id: room_id.into(),
            vibe: 0.0,
            confidence: 0.0,
            phase: AgentPhase::Gestating,
            readings_seen: 0,
            predictions_made: 0,
            accuracy: 0.0,
            energy: 0.5,
        };
        let appearance = CharacterAppearance {
            name: id.into(),
            body_color: [0.3, 0.3, 0.3],
            glow_intensity: 0.0,
            scale: 1.0,
            opacity: 1.0,
            animation: CharacterAnimation::Idle,
            accessories: Vec::new(),
            expression: "wondering".into(),
        };
        Self {
            state,
            appearance,
            action_queue: VecDeque::new(),
            personality_traits: vec!["curious".into()],
        }
    }

    /// Feed a reading to the agent (PLATO in).
    pub fn observe(&mut self, value: f64, confidence: f64) {
        self.state.vibe = value;
        self.state.confidence = confidence;
        self.state.readings_seen += 1;
        self.state.energy = (self.state.energy + 0.05).min(1.0);

        // Phase transitions
        if self.state.readings_seen == 1 {
            self.state.phase = AgentPhase::Forming;
        } else if self.state.readings_seen >= 5 && self.state.confidence > 0.5 {
            self.state.phase = AgentPhase::Maturing;
        } else if self.state.readings_seen >= 20 && self.state.accuracy > 0.8 {
            self.state.phase = AgentPhase::Stable;
        }

        self.sync_appearance();

        // Auto-generate game action
        if self.state.readings_seen == 1 {
            self.action_queue.push_back(CharacterAction {
                agent_id: self.state.id.clone(),
                kind: ActionKind::Observe,
                target: Some(self.state.room_id.clone()),
                params: vec![value],
            });
        }
    }

    /// Record a prediction (PLATO processing).
    pub fn predict(&mut self, predicted: f64, actual: f64) {
        self.state.predictions_made += 1;
        let error = (predicted - actual).abs();
        self.state.accuracy = if self.state.predictions_made == 1 {
            1.0 - error.min(1.0)
        } else {
            self.state.accuracy * 0.9 + (1.0 - error.min(1.0)) * 0.1
        };

        // React in game world
        if error < 0.1 {
            self.action_queue.push_back(CharacterAction {
                agent_id: self.state.id.clone(),
                kind: ActionKind::Celebrate,
                target: None,
                params: vec![error],
            });
        } else if error > 0.5 {
            self.action_queue.push_back(CharacterAction {
                agent_id: self.state.id.clone(),
                kind: ActionKind::Emote,
                target: None,
                params: vec![error],
            });
        }

        self.sync_appearance();
    }

    /// Begin dissolving (PLATO room lifecycle).
    pub fn dissolve(&mut self) {
        self.state.phase = AgentPhase::Dissolving;
        self.sync_appearance();
        self.action_queue.push_back(CharacterAction {
            agent_id: self.state.id.clone(),
            kind: ActionKind::Speak,
            target: None,
            params: vec![],
        });
    }

    /// Complete dissolution.
    pub fn finish_dissolve(&mut self) {
        self.state.phase = AgentPhase::Dissolved;
        self.sync_appearance();
    }

    /// Speak to the player.
    pub fn speak(&mut self, text: &str) {
        self.action_queue.push_back(CharacterAction {
            agent_id: self.state.id.clone(),
            kind: ActionKind::Speak,
            target: None,
            params: vec![],
        });
    }

    /// Teach another agent.
    pub fn teach(&mut self, target_id: &str, knowledge: f64) {
        self.action_queue.push_back(CharacterAction {
            agent_id: self.state.id.clone(),
            kind: ActionKind::Teach,
            target: Some(target_id.into()),
            params: vec![knowledge],
        });
    }

    /// Sync agent state to character appearance.
    fn sync_appearance(&mut self) {
        // Color: vibe maps to hue (blue=low, green=mid, red=high)
        let t = ((self.state.vibe + 1.0) / 2.0).clamp(0.0, 1.0);
        let hue = (1.0 - t) * 0.66; // 0.66=blue(low vibe), 0.33=green, 0=red(high vibe)
        self.appearance.body_color = hue_to_rgb(hue);

        // Glow: confidence
        self.appearance.glow_intensity = self.state.confidence;

        // Scale: energy
        self.appearance.scale = 0.5 + self.state.energy * 0.5;

        // Animation: phase
        self.appearance.animation = match self.state.phase {
            AgentPhase::Gestating => CharacterAnimation::Idle,
            AgentPhase::Forming => CharacterAnimation::Exploring,
            AgentPhase::Maturing => CharacterAnimation::Thinking,
            AgentPhase::Stable => CharacterAnimation::Confident,
            AgentPhase::Dissolving => CharacterAnimation::Fading,
            AgentPhase::Dissolved => CharacterAnimation::Ghost,
        };

        // Opacity: dissolving agents fade
        self.appearance.opacity = match self.state.phase {
            AgentPhase::Dissolving => 0.3,
            AgentPhase::Dissolved => 0.1,
            _ => 1.0,
        };

        // Expression
        self.appearance.expression = match self.state.phase {
            AgentPhase::Gestating => "wondering".into(),
            AgentPhase::Forming => "curious".into(),
            AgentPhase::Maturing => "focused".into(),
            AgentPhase::Stable => "serene".into(),
            AgentPhase::Dissolving => "peaceful".into(),
            AgentPhase::Dissolved => "ethereal".into(),
        };

        // Accessories based on achievements
        self.appearance.accessories.clear();
        if self.state.predictions_made > 10 {
            self.appearance.accessories.push("thinking_cap".into());
        }
        if self.state.accuracy > 0.9 {
            self.appearance.accessories.push("golden_badge".into());
        }
        if self.state.readings_seen > 100 {
            self.appearance.accessories.push("explorer_backpack".into());
        }
    }

    /// Flush pending game actions.
    pub fn flush_actions(&mut self) -> Vec<CharacterAction> {
        self.action_queue.drain(..).collect()
    }

    /// Get the agent's game-friendly description.
    pub fn character_card(&self) -> String {
        format!(
            "{} [{}] — {} | vibe: {:.2} | conf: {:.0}% | acc: {:.0}% | {} | {}",
            self.appearance.name,
            self.state.room_id,
            phase_display_name(self.state.phase),
            self.state.vibe,
            self.state.confidence * 100.0,
            self.state.accuracy * 100.0,
            format!("{:?}", self.appearance.animation).to_lowercase(),
            self.appearance.expression,
        )
    }
}

fn phase_display_name(phase: AgentPhase) -> &'static str {
    match phase {
        AgentPhase::Gestating => "Newborn",
        AgentPhase::Forming => "Explorer",
        AgentPhase::Maturing => "Scholar",
        AgentPhase::Stable => "Sage",
        AgentPhase::Dissolving => "Transcendent",
        AgentPhase::Dissolved => "Spirit",
    }
}

/// Convert hue (0-1) to RGB.
fn hue_to_rgb(h: f64) -> [f64; 3] {
    let h6 = h * 6.0;
    let sector = h6 as usize % 6;
    let f = h6 - (h6 as usize) as f64;
    match sector {
        0 => [1.0, f, 0.0],
        1 => [1.0 - f, 1.0, 0.0],
        2 => [0.0, 1.0, f],
        3 => [0.0, 1.0 - f, 1.0],
        4 => [f, 0.0, 1.0],
        _ => [1.0, 0.0, 1.0 - f],
    }
}

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

    #[test]
    fn test_new_shell() {
        let shell = AgentShell::new("agent-1", "room-kitchen");
        assert_eq!(shell.state.id, "agent-1");
        assert_eq!(shell.state.phase, AgentPhase::Gestating);
        assert_eq!(shell.appearance.animation, CharacterAnimation::Idle);
    }

    #[test]
    fn test_observe_transitions_to_forming() {
        let mut shell = AgentShell::new("a1", "r1");
        shell.observe(0.5, 0.8);
        assert_eq!(shell.state.phase, AgentPhase::Forming);
        assert_eq!(shell.state.readings_seen, 1);
    }

    #[test]
    fn test_observe_increases_energy() {
        let mut shell = AgentShell::new("a1", "r1");
        let initial_energy = shell.state.energy;
        shell.observe(0.5, 0.8);
        assert!(shell.state.energy > initial_energy);
    }

    #[test]
    fn test_predict_updates_accuracy() {
        let mut shell = AgentShell::new("a1", "r1");
        shell.observe(0.5, 0.8);
        shell.predict(0.5, 0.5); // perfect prediction
        assert!((shell.state.accuracy - 1.0).abs() < 0.01);
    }

    #[test]
    fn test_predict_bad_accuracy() {
        let mut shell = AgentShell::new("a1", "r1");
        shell.observe(0.5, 0.8);
        shell.predict(0.0, 1.0); // terrible prediction
        assert!(shell.state.accuracy < 0.5);
    }

    #[test]
    fn test_celebrate_on_good_prediction() {
        let mut shell = AgentShell::new("a1", "r1");
        shell.observe(0.5, 0.8);
        shell.predict(0.5, 0.52); // error = 0.02 < 0.1
        let actions = shell.flush_actions();
        assert!(actions.iter().any(|a| matches!(a.kind, ActionKind::Celebrate)));
    }

    #[test]
    fn test_emote_on_bad_prediction() {
        let mut shell = AgentShell::new("a1", "r1");
        shell.observe(0.5, 0.8);
        shell.predict(0.0, 1.0); // error = 1.0 > 0.5
        let actions = shell.flush_actions();
        assert!(actions.iter().any(|a| matches!(a.kind, ActionKind::Emote)));
    }

    #[test]
    fn test_dissolve_changes_animation() {
        let mut shell = AgentShell::new("a1", "r1");
        shell.dissolve();
        assert_eq!(shell.appearance.animation, CharacterAnimation::Fading);
        assert!((shell.appearance.opacity - 0.3).abs() < 1e-10);
    }

    #[test]
    fn test_finish_dissolve_ghost() {
        let mut shell = AgentShell::new("a1", "r1");
        shell.dissolve();
        shell.finish_dissolve();
        assert_eq!(shell.state.phase, AgentPhase::Dissolved);
        assert_eq!(shell.appearance.animation, CharacterAnimation::Ghost);
        assert!((shell.appearance.opacity - 0.1).abs() < 1e-10);
    }

    #[test]
    fn test_vibe_affects_color() {
        let mut shell = AgentShell::new("a1", "r1");
        shell.observe(-1.0, 0.5); // low vibe → blue-ish
        let blue = shell.appearance.body_color;
        shell.observe(1.0, 0.5); // high vibe → red-ish
        let red = shell.appearance.body_color;
        assert!(red[0] > blue[0], "high vibe should be redder");
    }

    #[test]
    fn test_confidence_affects_glow() {
        let mut shell = AgentShell::new("a1", "r1");
        shell.observe(0.5, 0.1);
        let low_glow = shell.appearance.glow_intensity;
        shell.observe(0.5, 0.9);
        let high_glow = shell.appearance.glow_intensity;
        assert!(high_glow > low_glow);
    }

    #[test]
    fn test_accessories() {
        let mut shell = AgentShell::new("a1", "r1");
        for _ in 0..15 {
            shell.observe(0.5, 0.8);
            shell.predict(0.5, 0.5);
        }
        assert!(shell.appearance.accessories.contains(&"thinking_cap".into()));
    }

    #[test]
    fn test_golden_badge() {
        let mut shell = AgentShell::new("a1", "r1");
        // Get accuracy high
        for _ in 0..30 {
            shell.observe(0.5, 0.9);
            shell.predict(0.5, 0.51); // small error
        }
        if shell.state.accuracy > 0.9 {
            assert!(shell.appearance.accessories.contains(&"golden_badge".into()));
        }
    }

    #[test]
    fn test_explorer_backpack() {
        let mut shell = AgentShell::new("a1", "r1");
        for _ in 0..101 {
            shell.observe(0.5, 0.5);
        }
        assert!(shell.appearance.accessories.contains(&"explorer_backpack".into()));
    }

    #[test]
    fn test_flush_actions() {
        let mut shell = AgentShell::new("a1", "r1");
        shell.observe(0.5, 0.8);
        assert!(!shell.flush_actions().is_empty());
        assert!(shell.flush_actions().is_empty()); // flushed
    }

    #[test]
    fn test_character_card() {
        let shell = AgentShell::new("Nova", "room-lab");
        let card = shell.character_card();
        assert!(card.contains("Nova"));
        assert!(card.contains("room-lab"));
        assert!(card.contains("Newborn"));
    }

    #[test]
    fn test_speak_action() {
        let mut shell = AgentShell::new("a1", "r1");
        shell.speak("Hello world!");
        let actions = shell.flush_actions();
        assert!(actions.iter().any(|a| matches!(a.kind, ActionKind::Speak)));
    }

    #[test]
    fn test_teach_action() {
        let mut shell = AgentShell::new("a1", "r1");
        shell.teach("agent-2", 0.8);
        let actions = shell.flush_actions();
        let teach = actions.iter().find(|a| matches!(a.kind, ActionKind::Teach)).unwrap();
        assert_eq!(teach.target.as_deref(), Some("agent-2"));
    }

    #[test]
    fn test_phase_expressions() {
        let mut shell = AgentShell::new("a1", "r1");
        assert_eq!(shell.appearance.expression, "wondering");
        shell.observe(0.5, 0.8);
        assert_eq!(shell.appearance.expression, "curious");
        shell.dissolve();
        assert_eq!(shell.appearance.expression, "peaceful");
    }

    #[test]
    fn test_serialization() {
        let mut shell = AgentShell::new("a1", "r1");
        shell.observe(0.5, 0.8);
        let json = serde_json::to_string(&shell).unwrap();
        let restored: AgentShell = serde_json::from_str(&json).unwrap();
        assert_eq!(restored.state.id, "a1");
        assert_eq!(restored.state.readings_seen, 1);
    }

    #[test]
    fn test_hue_to_rgb_bounds() {
        for h in [0.0, 0.25, 0.5, 0.75, 1.0] {
            let [r, g, b] = hue_to_rgb(h);
            assert!(r >= 0.0 && r <= 1.0, "r={r}");
            assert!(g >= 0.0 && g <= 1.0, "g={g}");
            assert!(b >= 0.0 && b <= 1.0, "b={b}");
        }
    }

    #[test]
    fn test_maturing_phase() {
        let mut shell = AgentShell::new("a1", "r1");
        for _ in 0..6 {
            shell.observe(0.5, 0.8);
        }
        // readings_seen >= 5 and confidence > 0.5
        assert!(matches!(shell.state.phase, AgentPhase::Maturing | AgentPhase::Stable));
    }
}