microresolve 0.2.0

Pre-LLM decision engine: intent classification, tool selection, request triage. ~50μs per call, CPU-only, continuous learning.
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
//! Full E2E: manual creation + MCP import + auto-learn deterministic path.
//!
//! Replaces `tests/integration/full_e2e.sh`.
//! Run with `cargo test --test http_full_e2e`.

mod common;
use common::*;
use serde_json::json;

const NS: &str = "e2e-full";

fn ns_h() -> Vec<(&'static str, &'static str)> {
    vec![("X-Namespace-ID", NS)]
}

#[test]
fn manual_creation() {
    let server = TestServer::spawn();
    let c = server.client();
    let b = format!("{}/api", server.url);

    // Create namespace
    let (s, body) = post_json(
        &c,
        &format!("{}/namespaces", b),
        &[],
        &json!({"namespace_id": NS, "description": "e2e test"}),
    );
    assert!(body.contains("created"), "{}", body);
    assert_eq!(s, 200);

    // Multilingual intent + metadata patch
    let s = post_json(&c, &format!("{}/intents", b), &ns_h(),
        &json!({
            "id": "billing:cancel_subscription",
            "phrases_by_lang": {
                "en": ["cancel my subscription", "stop my subscription", "end my plan", "I want to cancel"],
                "fr": ["annuler mon abonnement", "arrêter mon abonnement"]
            },
            "intent_type": "action",
            "description": "Cancel a recurring subscription"
        })).0;
    assert_eq!(s, 201);

    let s = patch_json(
        &c,
        &format!("{}/intents/billing:cancel_subscription", b),
        &ns_h(),
        &json!({
            "persona": "warm and apologetic",
            "guardrails": ["confirm with user before canceling", "offer pause as alternative"]
        }),
    );
    assert_eq!(s, 204);

    let (_, body) = get(&c, &format!("{}/intents", b), &ns_h());
    assert!(body.contains("warm and apologetic"), "persona persisted");
    assert!(
        body.contains("confirm with user before canceling"),
        "guardrail persisted"
    );
    assert!(
        body.contains("annuler mon abonnement"),
        "French phrase persisted"
    );

    // Add a decoy intent so IDF has signal
    post_json(
        &c,
        &format!("{}/intents", b),
        &ns_h(),
        &json!({"id":"weather:check","phrases":["what is the weather","is it raining"]}),
    );

    // Routing in both languages
    let (_, body) = post_json(
        &c,
        &format!("{}/resolve", b),
        &ns_h(),
        &json!({"query":"please cancel my plan"}),
    );
    assert!(
        body.contains("billing:cancel_subscription"),
        "EN routes correctly: {}",
        body
    );

    let (_, body) = post_json(
        &c,
        &format!("{}/resolve", b),
        &ns_h(),
        &json!({"query":"je veux annuler"}),
    );
    assert!(
        body.contains("billing:cancel_subscription"),
        "FR routes correctly: {}",
        body
    );

    delete_json(
        &c,
        &format!("{}/namespaces", b),
        &[],
        &json!({"namespace_id": NS}),
    );
}

#[test]
fn mcp_import_three_tools() {
    let server = TestServer::spawn();
    let c = server.client();
    let b = format!("{}/api", server.url);
    const NS_MCP: &str = "e2e-mcp";

    post_json(
        &c,
        &format!("{}/namespaces", b),
        &[],
        &json!({"namespace_id": NS_MCP}),
    );

    let h: Vec<(&str, &str)> = vec![("X-Namespace-ID", NS_MCP)];

    let tools_json = serde_json::to_string(&json!({
        "tools": [
            {
                "name": "search_orders",
                "description": "Search through customer orders by date range, status, or customer ID",
                "inputSchema": {"type":"object","properties":{"customer_id":{"type":"string"}},"required":["customer_id"]},
                "annotations": {"readOnlyHint": true}
            },
            {
                "name": "create_refund",
                "description": "Issue a refund for a completed order. Requires approval for amounts over $100.",
                "inputSchema": {"type":"object","properties":{"order_id":{"type":"string"}},"required":["order_id"]}
            },
            {
                "name": "send_notification",
                "description": "Send an email or SMS notification to a customer about their order status",
                "inputSchema": {"type":"object","properties":{"customer_id":{"type":"string"}},"required":["customer_id"]}
            }
        ]
    })).unwrap();

    // Parse step
    let (_, body) = post_json(
        &c,
        &format!("{}/import/mcp/parse", b),
        &h,
        &json!({"tools_json": tools_json}),
    );
    assert!(
        body.contains("\"total_tools\":3"),
        "parse 3 tools: {}",
        body
    );
    assert!(body.contains("search_orders"));
    assert!(body.contains("create_refund"));
    assert!(body.contains("send_notification"));

    // Apply step
    let (s, _) = post_json(
        &c,
        &format!("{}/import/mcp/apply", b),
        &h,
        &json!({
            "tools_json": tools_json,
            "selected": ["search_orders","create_refund","send_notification"],
            "domain": "shop"
        }),
    );
    assert!((200..300).contains(&s), "MCP apply 2xx: {}", s);

    // Verify intents created with prefix + correct types
    let (_, body) = get(&c, &format!("{}/intents", b), &h);
    assert!(body.contains("shop:search_orders"));
    assert!(body.contains("shop:create_refund"));
    assert!(body.contains("shop:send_notification"));

    // search_orders is readOnly → Context; others → Action
    assert!(
        body.contains("\"intent_type\":\"context\""),
        "readOnly → Context"
    );
    assert!(
        body.contains("\"intent_type\":\"action\""),
        "non-readOnly → Action"
    );

    // Schema preserved
    assert!(body.contains("customer_id"));
    // Target = mcp_server
    assert!(body.contains("mcp_server"));

    // MCP intents resolve
    let (_, body) = post_json(
        &c,
        &format!("{}/resolve", b),
        &h,
        &json!({"query":"refund this order"}),
    );
    assert!(
        body.contains("shop:create_refund"),
        "refund routes: {}",
        body
    );

    let (_, body) = post_json(
        &c,
        &format!("{}/resolve", b),
        &h,
        &json!({"query":"send email to customer"}),
    );
    assert!(
        body.contains("shop:send_notification"),
        "notification routes: {}",
        body
    );

    delete_json(
        &c,
        &format!("{}/namespaces", b),
        &[],
        &json!({"namespace_id": NS_MCP}),
    );
}

#[test]
fn auto_learn_deterministic_path() {
    let server = TestServer::spawn();
    let c = server.client();
    let b = format!("{}/api", server.url);
    const NS_AL: &str = "e2e-autolearn";

    post_json(
        &c,
        &format!("{}/namespaces", b),
        &[],
        &json!({"namespace_id": NS_AL}),
    );
    let h: Vec<(&str, &str)> = vec![("X-Namespace-ID", NS_AL)];

    // Seed a couple intents
    post_json(
        &c,
        &format!("{}/intents", b),
        &h,
        &json!({"id":"refund","phrases":["refund please","I want a refund"]}),
    );
    post_json(
        &c,
        &format!("{}/intents", b),
        &h,
        &json!({"id":"cancel","phrases":["cancel order","stop order"]}),
    );

    // decay_for_intents (audit log auto-fires)
    let (s, _) = post_json(
        &c,
        &format!("{}/namespaces/decay", b),
        &[],
        &json!({"namespace_id": NS_AL, "queries":["weather is nice today"], "alpha": 0.1}),
    );
    assert_eq!(s, 200);

    // rebuild_index (clears audit log)
    let (s, _) = post_json(
        &c,
        &format!("{}/namespaces/rebuild", b),
        &[],
        &json!({"namespace_id": NS_AL}),
    );
    assert_eq!(s, 200);

    // Routing still works after rebuild
    let (_, body) = post_json(
        &c,
        &format!("{}/resolve", b),
        &h,
        &json!({"query":"refund please"}),
    );
    assert!(
        body.contains("refund"),
        "routing works post-rebuild: {}",
        body
    );

    delete_json(
        &c,
        &format!("{}/namespaces", b),
        &[],
        &json!({"namespace_id": NS_AL}),
    );
}

#[test]
fn auth_keys_endpoint() {
    let server = TestServer::spawn();
    let c = server.client();
    let b = format!("{}/api", server.url);

    // Server bootstrapped a `studio-admin` key on first boot. Tests
    // pick up `server.api_key` automatically via the thread-local in
    // helpers; verify it's there.
    assert!(server.api_key.starts_with("mr_studio-admin_"));

    // GET /auth/keys (auto-authed by the helper) — should list at least
    // the bootstrap key and report enabled=true.
    let (_, body) = get(&c, &format!("{}/auth/keys", b), &[]);
    assert!(body.contains("\"enabled\":true"));
    assert!(body.contains("studio-admin"));

    // Create a Library-scoped key for a hypothetical client.
    let (s, body) = post_json(
        &c,
        &format!("{}/auth/keys", b),
        &[],
        &json!({"name": "test-key"}),
    );
    assert_eq!(s, 200);
    assert!(body.contains("mr_test-key_"), "key has expected prefix");
    assert!(body.contains("This key is shown once"));
    assert!(body.contains("\"scope\":\"library\""));
    let key: serde_json::Value = serde_json::from_str(&body).unwrap();
    let full_key = key["key"].as_str().unwrap();

    // Sync body (unified POST since v0.1.5).
    let sync_body = json!({
        "local_versions": { "default": 0 },
        "logs": [],
        "corrections": [],
    });

    // ── Negative case: no key at all → 401 ────────────────────────────────
    // Bypass the helper's auto-inject by crafting the request directly.
    let raw_resp = c
        .post(format!("{}/sync", b))
        .header("X-Namespace-ID", "default")
        .json(&sync_body)
        .send()
        .expect("request");
    assert_eq!(
        raw_resp.status().as_u16(),
        401,
        "request without X-Api-Key returns 401"
    );

    // ── Wrong key → 401 ──────────────────────────────────────────────────
    let (s, _) = post_json(
        &c,
        &format!("{}/sync", b),
        &[
            ("X-Namespace-ID", "default"),
            (
                "X-Api-Key",
                "mr_wrong_0000000000000000000000000000000000000000000000000000000000000000",
            ),
        ],
        &sync_body,
    );
    assert_eq!(s, 401);

    // ── New library key works on /sync ───────────────────────────────────
    let (s, _) = post_json(
        &c,
        &format!("{}/sync", b),
        &[("X-Namespace-ID", "default"), ("X-Api-Key", full_key)],
        &sync_body,
    );
    assert_eq!(s, 200, "library key works on /sync");

    // List + redaction.
    let (_, body) = get(&c, &format!("{}/auth/keys", b), &[]);
    assert!(body.contains("test-key"));
    assert!(!body.contains(full_key), "full key not in list");

    // Revoke.
    let s = delete_json(&c, &format!("{}/auth/keys/test-key", b), &[], &json!({}));
    assert_eq!(s, 204);
}

#[test]
fn unauthorized_endpoints_return_401_without_key() {
    // Smoke that EVERY protected endpoint enforces the gate, not just
    // /sync. Picks one read (list namespaces) and one write (create
    // namespace) — middleware applies uniformly so this is a chokepoint
    // test, not an endpoint enumeration.
    let server = TestServer::spawn();
    let c = server.client();
    let b = format!("{}/api", server.url);

    let r = c.get(format!("{}/namespaces", b)).send().expect("req");
    assert_eq!(
        r.status().as_u16(),
        401,
        "GET /namespaces without auth → 401"
    );

    let r = c
        .post(format!("{}/namespaces", b))
        .json(&json!({"namespace_id":"x"}))
        .send()
        .expect("req");
    assert_eq!(
        r.status().as_u16(),
        401,
        "POST /namespaces without auth → 401"
    );

    // /version is public — used by health checks.
    let r = c.get(format!("{}/version", b)).send().expect("req");
    assert_eq!(r.status().as_u16(), 200, "GET /version is public");
}