mnemo-codemode 0.5.21

Code-mode recall for Mnemo (v0.4.0 P0-3) — agents call recall as a sandboxed WIT function instead of a JSON tool, dropping per-turn token cost by ~95%.
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
// Generated by `wit-bindgen` 0.41.0. DO NOT EDIT!
// Options used:
//   * runtime_path: "wit_bindgen_rt"
#[doc(hidden)]
#[allow(non_snake_case)]
pub unsafe fn _export_run_cabi<T: Guest>(arg0: *mut u8, arg1: usize) -> *mut u8 {
    #[cfg(target_arch = "wasm32")] _rt::run_ctors_once();
    let len0 = arg1;
    let bytes0 = _rt::Vec::from_raw_parts(arg0.cast(), len0, len0);
    let result1 = T::run(_rt::string_lift(bytes0));
    let ptr2 = (&raw mut _RET_AREA.0).cast::<u8>();
    let vec3 = (result1.into_bytes()).into_boxed_slice();
    let ptr3 = vec3.as_ptr().cast::<u8>();
    let len3 = vec3.len();
    ::core::mem::forget(vec3);
    *ptr2.add(::core::mem::size_of::<*const u8>()).cast::<usize>() = len3;
    *ptr2.add(0).cast::<*mut u8>() = ptr3.cast_mut();
    ptr2
}
#[doc(hidden)]
#[allow(non_snake_case)]
pub unsafe fn __post_return_run<T: Guest>(arg0: *mut u8) {
    let l0 = *arg0.add(0).cast::<*mut u8>();
    let l1 = *arg0.add(::core::mem::size_of::<*const u8>()).cast::<usize>();
    _rt::cabi_dealloc(l0, l1, 1);
}
pub trait Guest {
    /// Guest exports its main loop. Host calls `run` once per turn,
    /// passing the user query; the guest responds with the answer
    /// string the host hands back to the LLM.
    fn run(user_query: _rt::String) -> _rt::String;
}
#[doc(hidden)]
macro_rules! __export_world_memory_agent_cabi {
    ($ty:ident with_types_in $($path_to_types:tt)*) => {
        const _ : () = { #[unsafe (export_name = "run")] unsafe extern "C" fn
        export_run(arg0 : * mut u8, arg1 : usize,) -> * mut u8 { unsafe {
        $($path_to_types)*:: _export_run_cabi::<$ty > (arg0, arg1) } } #[unsafe
        (export_name = "cabi_post_run")] unsafe extern "C" fn _post_return_run(arg0 : *
        mut u8,) { unsafe { $($path_to_types)*:: __post_return_run::<$ty > (arg0) } } };
    };
}
#[doc(hidden)]
pub(crate) use __export_world_memory_agent_cabi;
#[cfg_attr(target_pointer_width = "64", repr(align(8)))]
#[cfg_attr(target_pointer_width = "32", repr(align(4)))]
struct _RetArea([::core::mem::MaybeUninit<u8>; 2 * ::core::mem::size_of::<*const u8>()]);
static mut _RET_AREA: _RetArea = _RetArea(
    [::core::mem::MaybeUninit::uninit(); 2 * ::core::mem::size_of::<*const u8>()],
);
#[rustfmt::skip]
#[allow(dead_code, clippy::all)]
pub mod mnemo {
    pub mod memory {
        #[allow(dead_code, async_fn_in_trait, unused_imports, clippy::all)]
        pub mod store {
            #[used]
            #[doc(hidden)]
            static __FORCE_SECTION_REF: fn() = super::super::super::__link_custom_section_describing_imports;
            use super::super::super::_rt;
            #[derive(Clone)]
            pub struct Memory {
                pub id: _rt::String,
                pub content: _rt::String,
                pub score: f32,
            }
            impl ::core::fmt::Debug for Memory {
                fn fmt(
                    &self,
                    f: &mut ::core::fmt::Formatter<'_>,
                ) -> ::core::fmt::Result {
                    f.debug_struct("Memory")
                        .field("id", &self.id)
                        .field("content", &self.content)
                        .field("score", &self.score)
                        .finish()
                }
            }
            #[derive(Clone)]
            pub struct Provenance {
                pub receipt_id: _rt::String,
                pub hmac_key_id: _rt::String,
                pub record_count: u32,
            }
            impl ::core::fmt::Debug for Provenance {
                fn fmt(
                    &self,
                    f: &mut ::core::fmt::Formatter<'_>,
                ) -> ::core::fmt::Result {
                    f.debug_struct("Provenance")
                        .field("receipt-id", &self.receipt_id)
                        .field("hmac-key-id", &self.hmac_key_id)
                        .field("record-count", &self.record_count)
                        .finish()
                }
            }
            #[allow(unused_unsafe, clippy::all)]
            /// Run a recall against the host engine. The host streams matching
            /// memories back; the guest may cut at any point.
            pub fn recall(query: &str, k: u32) -> _rt::Vec<Memory> {
                unsafe {
                    #[cfg_attr(target_pointer_width = "64", repr(align(8)))]
                    #[cfg_attr(target_pointer_width = "32", repr(align(4)))]
                    struct RetArea(
                        [::core::mem::MaybeUninit<
                            u8,
                        >; 2 * ::core::mem::size_of::<*const u8>()],
                    );
                    let mut ret_area = RetArea(
                        [::core::mem::MaybeUninit::uninit(); 2
                            * ::core::mem::size_of::<*const u8>()],
                    );
                    let vec0 = query;
                    let ptr0 = vec0.as_ptr().cast::<u8>();
                    let len0 = vec0.len();
                    let ptr1 = ret_area.0.as_mut_ptr().cast::<u8>();
                    #[cfg(target_arch = "wasm32")]
                    #[link(wasm_import_module = "mnemo:memory/store@0.4.0")]
                    unsafe extern "C" {
                        #[link_name = "recall"]
                        fn wit_import2(_: *mut u8, _: usize, _: i32, _: *mut u8);
                    }
                    #[cfg(not(target_arch = "wasm32"))]
                    unsafe extern "C" fn wit_import2(
                        _: *mut u8,
                        _: usize,
                        _: i32,
                        _: *mut u8,
                    ) {
                        unreachable!()
                    }
                    unsafe { wit_import2(ptr0.cast_mut(), len0, _rt::as_i32(&k), ptr1) };
                    let l3 = *ptr1.add(0).cast::<*mut u8>();
                    let l4 = *ptr1
                        .add(::core::mem::size_of::<*const u8>())
                        .cast::<usize>();
                    let base12 = l3;
                    let len12 = l4;
                    let mut result12 = _rt::Vec::with_capacity(len12);
                    for i in 0..len12 {
                        let base = base12
                            .add(i * (5 * ::core::mem::size_of::<*const u8>()));
                        let e12 = {
                            let l5 = *base.add(0).cast::<*mut u8>();
                            let l6 = *base
                                .add(::core::mem::size_of::<*const u8>())
                                .cast::<usize>();
                            let len7 = l6;
                            let bytes7 = _rt::Vec::from_raw_parts(l5.cast(), len7, len7);
                            let l8 = *base
                                .add(2 * ::core::mem::size_of::<*const u8>())
                                .cast::<*mut u8>();
                            let l9 = *base
                                .add(3 * ::core::mem::size_of::<*const u8>())
                                .cast::<usize>();
                            let len10 = l9;
                            let bytes10 = _rt::Vec::from_raw_parts(
                                l8.cast(),
                                len10,
                                len10,
                            );
                            let l11 = *base
                                .add(4 * ::core::mem::size_of::<*const u8>())
                                .cast::<f32>();
                            Memory {
                                id: _rt::string_lift(bytes7),
                                content: _rt::string_lift(bytes10),
                                score: l11,
                            }
                        };
                        result12.push(e12);
                    }
                    _rt::cabi_dealloc(
                        base12,
                        len12 * (5 * ::core::mem::size_of::<*const u8>()),
                        ::core::mem::size_of::<*const u8>(),
                    );
                    let result13 = result12;
                    result13
                }
            }
            #[allow(unused_unsafe, clippy::all)]
            /// Re-score a memory under whatever scoring lane the guest wants
            /// (decay reweight, time bias, etc.). Returns the host-computed
            /// fused score so the guest does not need to ship the raw lane
            /// weights into wasm.
            pub fn score(mem: &Memory) -> f32 {
                unsafe {
                    let Memory { id: id0, content: content0, score: score0 } = mem;
                    let vec1 = id0;
                    let ptr1 = vec1.as_ptr().cast::<u8>();
                    let len1 = vec1.len();
                    let vec2 = content0;
                    let ptr2 = vec2.as_ptr().cast::<u8>();
                    let len2 = vec2.len();
                    #[cfg(target_arch = "wasm32")]
                    #[link(wasm_import_module = "mnemo:memory/store@0.4.0")]
                    unsafe extern "C" {
                        #[link_name = "score"]
                        fn wit_import3(
                            _: *mut u8,
                            _: usize,
                            _: *mut u8,
                            _: usize,
                            _: f32,
                        ) -> f32;
                    }
                    #[cfg(not(target_arch = "wasm32"))]
                    unsafe extern "C" fn wit_import3(
                        _: *mut u8,
                        _: usize,
                        _: *mut u8,
                        _: usize,
                        _: f32,
                    ) -> f32 {
                        unreachable!()
                    }
                    let ret = unsafe {
                        wit_import3(
                            ptr1.cast_mut(),
                            len1,
                            ptr2.cast_mut(),
                            len2,
                            _rt::as_f32(score0),
                        )
                    };
                    ret
                }
            }
            #[allow(unused_unsafe, clippy::all)]
            /// Ask the host to attach a provenance receipt to the recall
            /// result. The receipt is HMAC-signed with the host-side key the
            /// operator pinned in the manifest; the guest never sees the key.
            pub fn cite(mem: &Memory) -> Provenance {
                unsafe {
                    #[cfg_attr(target_pointer_width = "64", repr(align(8)))]
                    #[cfg_attr(target_pointer_width = "32", repr(align(4)))]
                    struct RetArea(
                        [::core::mem::MaybeUninit<
                            u8,
                        >; 5 * ::core::mem::size_of::<*const u8>()],
                    );
                    let mut ret_area = RetArea(
                        [::core::mem::MaybeUninit::uninit(); 5
                            * ::core::mem::size_of::<*const u8>()],
                    );
                    let Memory { id: id0, content: content0, score: score0 } = mem;
                    let vec1 = id0;
                    let ptr1 = vec1.as_ptr().cast::<u8>();
                    let len1 = vec1.len();
                    let vec2 = content0;
                    let ptr2 = vec2.as_ptr().cast::<u8>();
                    let len2 = vec2.len();
                    let ptr3 = ret_area.0.as_mut_ptr().cast::<u8>();
                    #[cfg(target_arch = "wasm32")]
                    #[link(wasm_import_module = "mnemo:memory/store@0.4.0")]
                    unsafe extern "C" {
                        #[link_name = "cite"]
                        fn wit_import4(
                            _: *mut u8,
                            _: usize,
                            _: *mut u8,
                            _: usize,
                            _: f32,
                            _: *mut u8,
                        );
                    }
                    #[cfg(not(target_arch = "wasm32"))]
                    unsafe extern "C" fn wit_import4(
                        _: *mut u8,
                        _: usize,
                        _: *mut u8,
                        _: usize,
                        _: f32,
                        _: *mut u8,
                    ) {
                        unreachable!()
                    }
                    unsafe {
                        wit_import4(
                            ptr1.cast_mut(),
                            len1,
                            ptr2.cast_mut(),
                            len2,
                            _rt::as_f32(score0),
                            ptr3,
                        )
                    };
                    let l5 = *ptr3.add(0).cast::<*mut u8>();
                    let l6 = *ptr3
                        .add(::core::mem::size_of::<*const u8>())
                        .cast::<usize>();
                    let len7 = l6;
                    let bytes7 = _rt::Vec::from_raw_parts(l5.cast(), len7, len7);
                    let l8 = *ptr3
                        .add(2 * ::core::mem::size_of::<*const u8>())
                        .cast::<*mut u8>();
                    let l9 = *ptr3
                        .add(3 * ::core::mem::size_of::<*const u8>())
                        .cast::<usize>();
                    let len10 = l9;
                    let bytes10 = _rt::Vec::from_raw_parts(l8.cast(), len10, len10);
                    let l11 = *ptr3
                        .add(4 * ::core::mem::size_of::<*const u8>())
                        .cast::<i32>();
                    let result12 = Provenance {
                        receipt_id: _rt::string_lift(bytes7),
                        hmac_key_id: _rt::string_lift(bytes10),
                        record_count: l11 as u32,
                    };
                    result12
                }
            }
        }
    }
}
#[rustfmt::skip]
mod _rt {
    #![allow(dead_code, clippy::all)]
    pub use alloc_crate::string::String;
    pub use alloc_crate::vec::Vec;
    pub fn as_i32<T: AsI32>(t: T) -> i32 {
        t.as_i32()
    }
    pub trait AsI32 {
        fn as_i32(self) -> i32;
    }
    impl<'a, T: Copy + AsI32> AsI32 for &'a T {
        fn as_i32(self) -> i32 {
            (*self).as_i32()
        }
    }
    impl AsI32 for i32 {
        #[inline]
        fn as_i32(self) -> i32 {
            self as i32
        }
    }
    impl AsI32 for u32 {
        #[inline]
        fn as_i32(self) -> i32 {
            self as i32
        }
    }
    impl AsI32 for i16 {
        #[inline]
        fn as_i32(self) -> i32 {
            self as i32
        }
    }
    impl AsI32 for u16 {
        #[inline]
        fn as_i32(self) -> i32 {
            self as i32
        }
    }
    impl AsI32 for i8 {
        #[inline]
        fn as_i32(self) -> i32 {
            self as i32
        }
    }
    impl AsI32 for u8 {
        #[inline]
        fn as_i32(self) -> i32 {
            self as i32
        }
    }
    impl AsI32 for char {
        #[inline]
        fn as_i32(self) -> i32 {
            self as i32
        }
    }
    impl AsI32 for usize {
        #[inline]
        fn as_i32(self) -> i32 {
            self as i32
        }
    }
    pub unsafe fn string_lift(bytes: Vec<u8>) -> String {
        if cfg!(debug_assertions) {
            String::from_utf8(bytes).unwrap()
        } else {
            String::from_utf8_unchecked(bytes)
        }
    }
    pub unsafe fn cabi_dealloc(ptr: *mut u8, size: usize, align: usize) {
        if size == 0 {
            return;
        }
        let layout = alloc::Layout::from_size_align_unchecked(size, align);
        alloc::dealloc(ptr, layout);
    }
    pub fn as_f32<T: AsF32>(t: T) -> f32 {
        t.as_f32()
    }
    pub trait AsF32 {
        fn as_f32(self) -> f32;
    }
    impl<'a, T: Copy + AsF32> AsF32 for &'a T {
        fn as_f32(self) -> f32 {
            (*self).as_f32()
        }
    }
    impl AsF32 for f32 {
        #[inline]
        fn as_f32(self) -> f32 {
            self as f32
        }
    }
    #[cfg(target_arch = "wasm32")]
    pub fn run_ctors_once() {
        wit_bindgen_rt::run_ctors_once();
    }
    extern crate alloc as alloc_crate;
    pub use alloc_crate::alloc;
}
/// Generates `#[unsafe(no_mangle)]` functions to export the specified type as
/// the root implementation of all generated traits.
///
/// For more information see the documentation of `wit_bindgen::generate!`.
///
/// ```rust
/// # macro_rules! export{ ($($t:tt)*) => (); }
/// # trait Guest {}
/// struct MyType;
///
/// impl Guest for MyType {
///     // ...
/// }
///
/// export!(MyType);
/// ```
#[allow(unused_macros)]
#[doc(hidden)]
macro_rules! __export_memory_agent_impl {
    ($ty:ident) => {
        self::export!($ty with_types_in self);
    };
    ($ty:ident with_types_in $($path_to_types_root:tt)*) => {
        $($path_to_types_root)*:: __export_world_memory_agent_cabi!($ty with_types_in
        $($path_to_types_root)*);
    };
}
#[doc(inline)]
pub(crate) use __export_memory_agent_impl as export;
#[cfg(target_arch = "wasm32")]
#[unsafe(
    link_section = "component-type:wit-bindgen:0.41.0:mnemo:memory@0.4.0:memory-agent:encoded world"
)]
#[doc(hidden)]
#[allow(clippy::octal_escapes)]
pub static __WIT_BINDGEN_COMPONENT_TYPE: [u8; 391] = *b"\
\0asm\x0d\0\x01\0\0\x19\x16wit-component-encoding\x04\0\x07\x84\x02\x01A\x02\x01\
A\x04\x01B\x0b\x01r\x03\x02ids\x07contents\x05scorev\x04\0\x06memory\x03\0\0\x01\
r\x03\x0areceipt-ids\x0bhmac-key-ids\x0crecord-county\x04\0\x0aprovenance\x03\0\x02\
\x01p\x01\x01@\x02\x05querys\x01ky\0\x04\x04\0\x06recall\x01\x05\x01@\x01\x03mem\
\x01\0v\x04\0\x05score\x01\x06\x01@\x01\x03mem\x01\0\x03\x04\0\x04cite\x01\x07\x03\
\0\x18mnemo:memory/store@0.4.0\x05\0\x01@\x01\x0auser-querys\0s\x04\0\x03run\x01\
\x01\x04\0\x1fmnemo:memory/memory-agent@0.4.0\x04\0\x0b\x12\x01\0\x0cmemory-agen\
t\x03\0\0\0G\x09producers\x01\x0cprocessed-by\x02\x0dwit-component\x070.227.1\x10\
wit-bindgen-rust\x060.41.0";
#[inline(never)]
#[doc(hidden)]
pub fn __link_custom_section_describing_imports() {
    wit_bindgen_rt::maybe_link_cabi_realloc();
}