wm-tools 9.2.4

Curated tool implementations for the WhiteMagic MCP server.
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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
//! Imagination engine tools — scenario generation, prediction, and reflection.
//!
//! Gana::ThreeStars — "Imagination, scenario planning, counterfactual reflection"
//!
//! Tools:
//! - `imagine.scenario` — Generate scenarios for a given state + goal
//! - `imagine.predict` — Predict outcome of a specific action
//! - `imagine.reflect` — Counterfactual reflection on past decisions

#![forbid(unsafe_code)]
#![allow(clippy::significant_drop_tightening)]

use async_trait::async_trait;

use serde_json::{Value, json};
use std::sync::Arc;
use wm_bicameral::{
    ScenarioEngine, ScenarioEvaluator, WorldModel,
    simulation_bridge::{SimulationBridge, SimulationBridgeConfig},
    world_model_from_env,
};
use wm_core::{Context, EffectRow, Gana, Tool, ToolStats};
use wm_memory::MemoryStore;

// ── WorldModel factory ────────────────────────────────────────────────

/// Build a WorldModel from env-configured LLM handlers, falling back to stubs.
fn build_world_model() -> WorldModel {
    world_model_from_env()
}

// ── imagine.scenario ──────────────────────────────────────────────────

/// Imagination scenario tool — generates scenarios for a given state + goal.
///
/// Uses the bicameral world model to imagine multiple possible actions,
/// predict their outcomes, and evaluate them. Optionally enriches with
/// simulation data (MC rollout, forecasting, sensitivity analysis).
pub struct ImagineScenarioTool {
    store: Arc<MemoryStore>,
    stats: ToolStats,
    effects: EffectRow,
}

impl ImagineScenarioTool {
    /// Create a new imagination scenario tool.
    pub fn new(store: Arc<MemoryStore>) -> Self {
        Self {
            store,
            stats: ToolStats::default(),
            effects: EffectRow::read_only(vec![wm_core::Resource::Galaxy("universal".into())]),
        }
    }

    fn gather_context(&self, query: &str, limit: usize) -> String {
        let topic_lower = query.to_lowercase();
        let topic_words: Vec<&str> = topic_lower.split_whitespace().collect();

        let mut context_parts: Vec<String> = Vec::new();
        for galaxy in wm_core::Galaxy::memory_galaxies() {
            if let Ok(mems) = self.store.scan(galaxy, limit) {
                for mem in mems {
                    // model_exclude memories never enter scenario context.
                    if mem.metadata.model_exclude {
                        continue;
                    }
                    let content_lower = mem.content.to_lowercase();
                    if topic_words.iter().any(|w| content_lower.contains(w)) {
                        context_parts.push(format!("- {}", mem.content));
                        if context_parts.len() >= 20 {
                            break;
                        }
                    }
                }
            }
            if context_parts.len() >= 20 {
                break;
            }
        }
        context_parts.join("\n")
    }
}

#[async_trait]
impl Tool for ImagineScenarioTool {
    fn name(&self) -> &str {
        "imagine.scenario"
    }
    fn gana(&self) -> Gana {
        Gana::ThreeStars
    }
    fn effects(&self) -> &EffectRow {
        &self.effects
    }
    fn description(&self) -> &str {
        "[Experimental] Generate and evaluate scenarios for a given state and goal using the bicameral imagination engine"
    }
    async fn call(&self, _ctx: &mut Context, args: Value) -> wm_core::Result<Value> {
        let state = args
            .get("state")
            .and_then(Value::as_str)
            .ok_or_else(|| wm_core::CoreError::InvalidArgs("state (string) required".into()))?;

        let goal = args
            .get("goal")
            .and_then(Value::as_str)
            .ok_or_else(|| wm_core::CoreError::InvalidArgs("goal (string) required".into()))?;

        let scan_limit = args
            .get("scan_limit")
            .and_then(Value::as_u64)
            .unwrap_or(200) as usize;

        let enrich_sim = args
            .get("enrich_simulation")
            .and_then(Value::as_bool)
            .unwrap_or(false);

        let mc_samples = args.get("mc_samples").and_then(Value::as_u64).unwrap_or(10) as usize;

        // Gather memory context
        let memory_context = self.gather_context(goal, scan_limit);

        // Build scenario engine and generate scenarios
        let world_model = build_world_model();
        let scenario_engine =
            ScenarioEngine::with_defaults(world_model, ScenarioEvaluator::with_defaults());
        let scenarios = scenario_engine.imagine(state, goal, &memory_context);

        if scenarios.is_empty() {
            return Ok(json!({
                "status": "no_scenarios",
                "state": state,
                "goal": goal,
                "scenarios": [],
            }));
        }

        // Optionally enrich with simulation data
        let scenarios_json: Vec<Value> = if enrich_sim {
            let world_model = build_world_model();
            let bridge_config = SimulationBridgeConfig {
                mc_samples,
                sensitivity_samples: 50,
                cf_bootstrap: 50,
                ..Default::default()
            };
            let mut bridge = SimulationBridge::new(bridge_config);
            scenarios
                .iter()
                .map(|s| {
                    let history: Vec<f64> = vec![f64::from(s.score), f64::from(s.score)];
                    let enriched = bridge.enrich_scenario(&world_model, s, &history);
                    enriched.to_json()
                })
                .collect()
        } else {
            scenarios
                .iter()
                .map(|s| {
                    json!({
                        "action": s.action,
                        "score": s.score,
                        "risk": s.risk,
                        "novelty": s.novelty,
                        "rationale": s.rationale,
                        "trajectory_steps": s.trajectory.len(),
                    })
                })
                .collect()
        };

        // Select best scenario
        let best = scenario_engine.select_balanced(&scenarios, 0.05);

        Ok(json!({
            "status": "ok",
            "state": state,
            "goal": goal,
            "scenario_count": scenarios.len(),
            "best_action": best.map(|s| s.action.clone()),
            "best_score": best.map(|s| s.score),
            "scenarios": scenarios_json,
        }))
    }
    fn stats(&self) -> &ToolStats {
        &self.stats
    }
}

// ── imagine.predict ───────────────────────────────────────────────────

/// Imagination predict tool — predicts the outcome of a specific action.
///
/// Uses the bicameral world model to predict what would happen if a
/// specific action is taken from a given state.
pub struct ImaginePredictTool {
    stats: ToolStats,
    effects: EffectRow,
}

impl ImaginePredictTool {
    /// Create a new imagination predict tool.
    #[must_use]
    pub fn new() -> Self {
        Self {
            stats: ToolStats::default(),
            effects: EffectRow::read_only(vec![wm_core::Resource::Galaxy("universal".into())]),
        }
    }
}

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

#[async_trait]
impl Tool for ImaginePredictTool {
    fn name(&self) -> &str {
        "imagine.predict"
    }
    fn gana(&self) -> Gana {
        Gana::ThreeStars
    }
    fn effects(&self) -> &EffectRow {
        &self.effects
    }
    fn description(&self) -> &str {
        "[Experimental] Predict the outcome of a specific action from a given state using the bicameral world model"
    }
    async fn call(&self, _ctx: &mut Context, args: Value) -> wm_core::Result<Value> {
        let state = args
            .get("state")
            .and_then(Value::as_str)
            .ok_or_else(|| wm_core::CoreError::InvalidArgs("state (string) required".into()))?;

        let action = args
            .get("action")
            .and_then(Value::as_str)
            .ok_or_else(|| wm_core::CoreError::InvalidArgs("action (string) required".into()))?;

        let goal = args
            .get("goal")
            .and_then(Value::as_str)
            .ok_or_else(|| wm_core::CoreError::InvalidArgs("goal (string) required".into()))?;

        let world_model = build_world_model();
        let prediction = world_model.predict(state, action, goal);

        let best = prediction.best();

        let mut alternatives: Vec<Value> = Vec::new();
        if prediction.left.description != best.description {
            alternatives.push(json!({
                "description": prediction.left.description,
                "confidence": prediction.left.confidence,
                "source": "left",
            }));
        }
        if let Some(ref right) = prediction.right {
            if right.description != best.description {
                alternatives.push(json!({
                    "description": right.description,
                    "confidence": right.confidence,
                    "source": "right",
                }));
            }
        }

        Ok(json!({
            "status": "ok",
            "state": state,
            "action": action,
            "goal": goal,
            "best_prediction": {
                "description": best.description,
                "confidence": best.confidence,
                "changes": best.changes,
                "risks": best.risks,
                "goal_progress": best.goal_progress,
            },
            "alternatives": alternatives,
            "has_consensus": prediction.has_consensus(),
        }))
    }
    fn stats(&self) -> &ToolStats {
        &self.stats
    }
}

// ── imagine.reflect ───────────────────────────────────────────────────

/// Imagination reflect tool — counterfactual reflection on past decisions.
///
/// Given a past state, the action taken, and an alternative action,
/// predicts what would have happened with the alternative.
pub struct ImagineReflectTool {
    stats: ToolStats,
    effects: EffectRow,
}

impl ImagineReflectTool {
    /// Create a new imagination reflect tool.
    #[must_use]
    pub fn new() -> Self {
        Self {
            stats: ToolStats::default(),
            effects: EffectRow::read_only(vec![wm_core::Resource::Galaxy("universal".into())]),
        }
    }
}

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

#[async_trait]
impl Tool for ImagineReflectTool {
    fn name(&self) -> &str {
        "imagine.reflect"
    }
    fn gana(&self) -> Gana {
        Gana::ThreeStars
    }
    fn effects(&self) -> &EffectRow {
        &self.effects
    }
    fn description(&self) -> &str {
        "[Experimental] Counterfactual reflection: compare actual action outcome vs alternative action using the bicameral world model"
    }
    async fn call(&self, _ctx: &mut Context, args: Value) -> wm_core::Result<Value> {
        let past_state = args
            .get("past_state")
            .and_then(Value::as_str)
            .ok_or_else(|| {
                wm_core::CoreError::InvalidArgs("past_state (string) required".into())
            })?;

        let actual_action = args
            .get("actual_action")
            .and_then(Value::as_str)
            .ok_or_else(|| {
                wm_core::CoreError::InvalidArgs("actual_action (string) required".into())
            })?;

        let alternative_action = args
            .get("alternative_action")
            .and_then(Value::as_str)
            .ok_or_else(|| {
                wm_core::CoreError::InvalidArgs("alternative_action (string) required".into())
            })?;

        let goal = args
            .get("goal")
            .and_then(Value::as_str)
            .unwrap_or("improve outcome");

        let world_model = build_world_model();
        let scenario_engine =
            ScenarioEngine::with_defaults(world_model, ScenarioEvaluator::with_defaults());
        let reflection =
            scenario_engine.reflect(past_state, actual_action, alternative_action, goal);

        Ok(json!({
            "status": "ok",
            "past_state": past_state,
            "actual_action": actual_action,
            "alternative_action": alternative_action,
            "actual_outcome": {
                "description": reflection.actual_prediction.description,
                "confidence": reflection.actual_prediction.confidence,
                "goal_progress": reflection.actual_prediction.goal_progress,
            },
            "counterfactual_outcome": {
                "description": reflection.counterfactual_prediction.description,
                "confidence": reflection.counterfactual_prediction.confidence,
                "goal_progress": reflection.counterfactual_prediction.goal_progress,
            },
            "would_have_been_better": reflection.would_have_been_better,
            "lesson": reflection.lesson,
        }))
    }
    fn stats(&self) -> &ToolStats {
        &self.stats
    }
}

// ── Registration ──────────────────────────────────────────────────────

/// Register imagination tools into a registry.
pub fn register_imagination(
    registry: &wm_dispatch::ToolRegistry,
    store: &Arc<MemoryStore>,
) -> wm_dispatch::ToolRegistry {
    registry
        .register(Arc::new(ImagineScenarioTool::new(store.clone())))
        .register(Arc::new(ImaginePredictTool::new()))
        .register(Arc::new(ImagineReflectTool::new()))
}

// ── Tests ─────────────────────────────────────────────────────────────

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

    fn make_store() -> Arc<MemoryStore> {
        let dir = tempfile::tempdir().unwrap();
        Arc::new(MemoryStore::open(dir.path(), 1024 * 1024).unwrap())
    }

    #[tokio::test]
    async fn imagine_scenario_tool_name() {
        let store = make_store();
        let tool = ImagineScenarioTool::new(store);
        assert_eq!(tool.name(), "imagine.scenario");
        assert_eq!(tool.gana(), Gana::ThreeStars);
    }

    #[tokio::test]
    async fn imagine_predict_tool_name() {
        let tool = ImaginePredictTool::new();
        assert_eq!(tool.name(), "imagine.predict");
        assert_eq!(tool.gana(), Gana::ThreeStars);
    }

    #[tokio::test]
    async fn imagine_reflect_tool_name() {
        let tool = ImagineReflectTool::new();
        assert_eq!(tool.name(), "imagine.reflect");
        assert_eq!(tool.gana(), Gana::ThreeStars);
    }

    #[tokio::test]
    async fn imagine_scenario_generates_scenarios() {
        let store = make_store();
        let tool = ImagineScenarioTool::new(store);
        let mut ctx = Context::default();
        let result = tool
            .call(
                &mut ctx,
                json!({
                    "state": "system is slow",
                    "goal": "improve performance",
                }),
            )
            .await;
        assert!(result.is_ok());
        let val = result.unwrap();
        assert_eq!(val["status"], "ok");
        assert!(val["scenario_count"].as_u64().is_some());
    }

    #[tokio::test]
    async fn imagine_scenario_missing_state() {
        let store = make_store();
        let tool = ImagineScenarioTool::new(store);
        let mut ctx = Context::default();
        let result = tool.call(&mut ctx, json!({"goal": "test"})).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn imagine_scenario_missing_goal() {
        let store = make_store();
        let tool = ImagineScenarioTool::new(store);
        let mut ctx = Context::default();
        let result = tool.call(&mut ctx, json!({"state": "test"})).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn imagine_predict_returns_prediction() {
        let tool = ImaginePredictTool::new();
        let mut ctx = Context::default();
        let result = tool
            .call(
                &mut ctx,
                json!({
                    "state": "idle system",
                    "action": "run optimization",
                    "goal": "improve speed",
                }),
            )
            .await;
        assert!(result.is_ok());
        let val = result.unwrap();
        assert_eq!(val["status"], "ok");
        assert!(val["best_prediction"]["description"].as_str().is_some());
    }

    #[tokio::test]
    async fn imagine_predict_missing_action() {
        let tool = ImaginePredictTool::new();
        let mut ctx = Context::default();
        let result = tool
            .call(&mut ctx, json!({"state": "test", "goal": "test"}))
            .await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn imagine_reflect_returns_reflection() {
        let tool = ImagineReflectTool::new();
        let mut ctx = Context::default();
        let result = tool
            .call(
                &mut ctx,
                json!({
                    "past_state": "system running",
                    "actual_action": "did nothing",
                    "alternative_action": "optimized cache",
                    "goal": "improve speed",
                }),
            )
            .await;
        assert!(result.is_ok());
        let val = result.unwrap();
        assert_eq!(val["status"], "ok");
        assert!(val["actual_outcome"]["description"].as_str().is_some());
        assert!(
            val["counterfactual_outcome"]["description"]
                .as_str()
                .is_some()
        );
    }

    #[tokio::test]
    async fn imagine_reflect_missing_alternative() {
        let tool = ImagineReflectTool::new();
        let mut ctx = Context::default();
        let result = tool
            .call(
                &mut ctx,
                json!({
                    "past_state": "test",
                    "actual_action": "test",
                }),
            )
            .await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn register_imagination_registers_three_tools() {
        let store = make_store();
        let registry = wm_dispatch::ToolRegistry::new();
        let registry = register_imagination(&registry, &store);
        assert!(registry.get("imagine.scenario").is_some());
        assert!(registry.get("imagine.predict").is_some());
        assert!(registry.get("imagine.reflect").is_some());
    }
}