memd 1.9.0

Local memory CLI for AI agents with hybrid retrieval
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
use std::path::Path;

use serde::{Deserialize, Serialize};
use serde_json::{json, Value};

use crate::error::{MemdError, Result};
use crate::store::{Store, TenantManager};
use crate::task_memory::ExecutionContext;

use super::scope::{apply_operation_scope, apply_operation_scope_at, OperationScopeCache};
use super::{
    capture_execution_context, cli_call_tool, enrich_operation_arguments, read_stdin_to_string,
    unwrap_content_payload,
};

#[derive(Debug, Clone, Serialize, Deserialize)]
struct BatchCallInput {
    tool: String,
    #[serde(default)]
    arguments: Option<Value>,
    #[serde(
        default,
        rename = "__memd_scope_error",
        skip_serializing_if = "Option::is_none"
    )]
    scope_error: Option<String>,
    #[serde(flatten)]
    extra: serde_json::Map<String, Value>,
}

pub(super) fn read_batch_input(path: Option<&Path>) -> Result<String> {
    match path {
        None => read_stdin_to_string(),
        Some(p) if p.as_os_str() == std::ffi::OsStr::new("-") => read_stdin_to_string(),
        Some(p) => Ok(std::fs::read_to_string(p)?),
    }
}

/// Resolve repository scope before a non-streaming batch is sent to a warm
/// worker, whose process may have been started from a different repository.
pub(super) fn scope_batch_jsonl(input: &str, continue_on_error: bool) -> Result<String> {
    let cwd = std::env::current_dir().unwrap_or_else(|_| std::path::PathBuf::from("."));
    scope_batch_jsonl_at(input, &cwd, continue_on_error)
}

fn scope_batch_jsonl_at(input: &str, start: &Path, continue_on_error: bool) -> Result<String> {
    let context = capture_execution_context(start);
    scope_batch_jsonl_at_with_context(input, start, continue_on_error, Some(&context))
}

fn scope_batch_jsonl_at_with_context(
    input: &str,
    start: &Path,
    continue_on_error: bool,
    context: Option<&ExecutionContext>,
) -> Result<String> {
    let mut out = String::new();
    let mut scope_cache = OperationScopeCache::default();
    for raw_line in input.lines() {
        let line = raw_line.trim();
        if line.is_empty() {
            out.push('\n');
            continue;
        }

        let rendered = match serde_json::from_str::<BatchCallInput>(line) {
            Ok(mut request) => {
                if request.scope_error.is_some() {
                    return Err(MemdError::ValidationError(
                        "reserved batch field __memd_scope_error is only valid on the warm wire"
                            .to_string(),
                    ));
                }
                let arguments = request.arguments.take().unwrap_or_else(|| json!({}));
                if arguments.is_object() || arguments.is_null() {
                    request.arguments = Some(
                        match apply_operation_scope_at(start, arguments.clone(), &mut scope_cache)
                            .and_then(|arguments| match context {
                                Some(context) => {
                                    enrich_operation_arguments(&request.tool, arguments, context)
                                }
                                None => Ok(arguments),
                            }) {
                            Ok(arguments) => arguments,
                            Err(error) if !continue_on_error => return Err(error),
                            Err(error) => {
                                request.scope_error = Some(error.to_string());
                                arguments
                            }
                        },
                    );
                    serde_json::to_string(&request)?
                } else {
                    raw_line.to_string()
                }
            }
            Err(_) => raw_line.to_string(),
        };
        out.push_str(&rendered);
        out.push('\n');
    }
    Ok(out)
}

pub(super) async fn run_batch_jsonl<S: Store>(
    store: &S,
    tenant_manager: Option<&TenantManager>,
    input: &str,
    continue_on_error: bool,
) -> Result<String> {
    let mut scope_cache = OperationScopeCache::default();
    let cwd = std::env::current_dir()?;
    let context = capture_execution_context(&cwd);
    run_batch_jsonl_with_scope_cache(
        store,
        tenant_manager,
        input,
        continue_on_error,
        &mut scope_cache,
        false,
        Some(&context),
    )
    .await
}

pub(super) async fn run_pre_scoped_batch_jsonl<S: Store>(
    store: &S,
    tenant_manager: Option<&TenantManager>,
    input: &str,
    continue_on_error: bool,
) -> Result<String> {
    let mut scope_cache = OperationScopeCache::disabled();
    run_batch_jsonl_with_scope_cache(
        store,
        tenant_manager,
        input,
        continue_on_error,
        &mut scope_cache,
        true,
        None,
    )
    .await
}

async fn run_batch_jsonl_with_scope_cache<S: Store>(
    store: &S,
    tenant_manager: Option<&TenantManager>,
    input: &str,
    continue_on_error: bool,
    scope_cache: &mut OperationScopeCache,
    pre_scoped: bool,
    context: Option<&ExecutionContext>,
) -> Result<String> {
    let mut out = String::new();
    let mut processed = 0usize;

    for (line_number, raw_line) in input.lines().enumerate() {
        let line = raw_line.trim();
        if line.is_empty() {
            continue;
        }
        let index = processed;
        processed += 1;

        let request = match serde_json::from_str::<BatchCallInput>(line) {
            Ok(request) => request,
            Err(error) => {
                if !continue_on_error {
                    return Err(MemdError::ValidationError(format!(
                        "invalid JSONL request on line {}: {error}",
                        line_number + 1
                    )));
                }
                let row = json!({
                    "ok": false,
                    "index": index,
                    "line": line_number + 1,
                    "error": format!("invalid JSONL request: {error}"),
                });
                out.push_str(&serde_json::to_string(&row)?);
                out.push('\n');
                continue;
            }
        };

        if let Some(scope_error) = request.scope_error.as_ref() {
            if !pre_scoped {
                return Err(MemdError::ValidationError(
                    "reserved batch field __memd_scope_error is only valid on the warm wire"
                        .to_string(),
                ));
            }
            if !continue_on_error {
                return Err(MemdError::ValidationError(scope_error.clone()));
            }
            let row = json!({
                "ok": false,
                "index": index,
                "line": line_number + 1,
                "tool": request.tool,
                "error": scope_error,
            });
            out.push_str(&serde_json::to_string(&row)?);
            out.push('\n');
            continue;
        }

        let arguments = request.arguments.unwrap_or_else(|| json!({}));
        if !(arguments.is_object() || arguments.is_null()) {
            let message = "batch arguments must be a JSON object".to_string();
            if !continue_on_error {
                return Err(MemdError::ValidationError(message));
            }
            let row = json!({
                "ok": false,
                "index": index,
                "line": line_number + 1,
                "tool": request.tool,
                "error": message,
            });
            out.push_str(&serde_json::to_string(&row)?);
            out.push('\n');
            continue;
        }
        let arguments =
            match apply_operation_scope(arguments, scope_cache).and_then(
                |arguments| match context {
                    Some(context) => enrich_operation_arguments(&request.tool, arguments, context),
                    None => Ok(arguments),
                },
            ) {
                Ok(arguments) => arguments,
                Err(error) if !continue_on_error => return Err(error),
                Err(error) => {
                    let row = json!({
                        "ok": false,
                        "index": index,
                        "line": line_number + 1,
                        "tool": request.tool,
                        "error": error.to_string(),
                    });
                    out.push_str(&serde_json::to_string(&row)?);
                    out.push('\n');
                    continue;
                }
            };

        let started = std::time::Instant::now();
        match cli_call_tool(store, tenant_manager, &request.tool, arguments).await {
            Ok(value) => {
                let elapsed_ms = started.elapsed().as_secs_f64() * 1000.0;
                let payload = unwrap_content_payload(value.clone()).unwrap_or(value);
                let row = json!({
                    "ok": true,
                    "index": index,
                    "line": line_number + 1,
                    "tool": request.tool,
                    "elapsed_ms": elapsed_ms,
                    "result": payload,
                });
                out.push_str(&serde_json::to_string(&row)?);
                out.push('\n');
            }
            Err(error) => {
                if !continue_on_error {
                    return Err(MemdError::ProtocolError(error.to_string()));
                }
                let elapsed_ms = started.elapsed().as_secs_f64() * 1000.0;
                let row = json!({
                    "ok": false,
                    "index": index,
                    "line": line_number + 1,
                    "tool": request.tool,
                    "elapsed_ms": elapsed_ms,
                    "error": error.to_string(),
                });
                out.push_str(&serde_json::to_string(&row)?);
                out.push('\n');
            }
        }
    }

    Ok(out)
}

pub(super) async fn stream_batch_jsonl<S: Store>(
    store: &S,
    tenant_manager: Option<&TenantManager>,
    input_path: Option<&Path>,
    output_path: Option<&Path>,
    continue_on_error: bool,
) -> Result<()> {
    use std::io::{BufRead, BufReader, BufWriter, Write};

    let input: Box<dyn BufRead> = match input_path {
        None => Box::new(BufReader::new(std::io::stdin())),
        Some(p) if p.as_os_str() == std::ffi::OsStr::new("-") => {
            Box::new(BufReader::new(std::io::stdin()))
        }
        Some(p) => Box::new(BufReader::new(std::fs::File::open(p)?)),
    };
    let mut output: Box<dyn Write> = match output_path {
        None => Box::new(BufWriter::new(std::io::stdout())),
        Some(path) => {
            if let Some(parent) = path.parent() {
                std::fs::create_dir_all(parent)?;
            }
            Box::new(BufWriter::new(std::fs::File::create(path)?))
        }
    };

    let mut processed = 0usize;
    let mut scope_cache = OperationScopeCache::default();
    let cwd = std::env::current_dir()?;
    let context = capture_execution_context(&cwd);
    for (line_number, raw_line) in input.lines().enumerate() {
        let raw_line = raw_line?;
        let line = raw_line.trim();
        if line.is_empty() {
            continue;
        }
        let index = processed;
        processed += 1;

        let rendered = match run_batch_jsonl_with_scope_cache(
            store,
            tenant_manager,
            line,
            continue_on_error,
            &mut scope_cache,
            false,
            Some(&context),
        )
        .await
        {
            Ok(rendered) => {
                let mut row: Value = serde_json::from_str(rendered.trim())?;
                if let Some(obj) = row.as_object_mut() {
                    obj.insert("index".to_string(), json!(index));
                    obj.insert("line".to_string(), json!(line_number + 1));
                }
                serde_json::to_string(&row)? + "\n"
            }
            Err(error) if continue_on_error => {
                let row = json!({
                    "ok": false,
                    "index": index,
                    "line": line_number + 1,
                    "error": error.to_string(),
                });
                serde_json::to_string(&row)? + "\n"
            }
            Err(error) => return Err(error),
        };

        output.write_all(rendered.as_bytes())?;
        output.flush()?;
    }

    Ok(())
}

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

    #[test]
    fn warm_batch_payload_inherits_scope_before_routing() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::create_dir_all(dir.path().join(".memd")).unwrap();
        std::fs::write(
            dir.path().join(".memd/project_scope.json"),
            r#"{
              "tenant_id": "batch_tenant",
              "project_id": "batch_project",
              "interface": "cli",
              "cli_command": "memd",
              "agent_context_output": ".memd/context.md",
              "project_dir": "."
            }"#,
        )
        .unwrap();
        let input = concat!(
            "{\"tool\":\"memory.add\",\"arguments\":{\"text\":\"scoped\"},\"id\":\"correlation\"}\n",
            "{\"tool\":\"memory.search\",\"arguments\":{\"tenant_id\":\"explicit\",\"query\":\"q\"}}\n",
            "not-json\n"
        );

        let output = scope_batch_jsonl_at(input, dir.path(), false).unwrap();
        let lines = output.lines().collect::<Vec<_>>();
        let scoped: Value = serde_json::from_str(lines[0]).unwrap();
        assert_eq!(scoped["arguments"]["tenant_id"], "batch_tenant");
        assert_eq!(scoped["arguments"]["project_id"], "batch_project");
        assert_eq!(scoped["id"], "correlation");
        let explicit: Value = serde_json::from_str(lines[1]).unwrap();
        assert_eq!(explicit["arguments"]["tenant_id"], "explicit");
        assert!(explicit["arguments"].get("project_id").is_none());
        assert_eq!(lines[2], "not-json");
    }

    #[test]
    fn warm_batch_payload_carries_one_client_context() {
        let dir = tempfile::tempdir().unwrap();
        let context = ExecutionContext {
            harness: Some("codex".to_string()),
            native_thread_id: Some("thread-a".to_string()),
            observed_at_ms: 42,
            ..Default::default()
        };
        let input = concat!(
            "{\"tool\":\"artifact.create\",\"arguments\":{",
            "\"tenant_id\":\"t\",\"artifact_kind\":\"evidence\"}}\n"
        );

        let output =
            scope_batch_jsonl_at_with_context(input, dir.path(), false, Some(&context)).unwrap();
        let request: Value = serde_json::from_str(output.trim()).unwrap();

        assert_eq!(request["arguments"]["thread_id"], "thread-a");
        assert_eq!(
            request["arguments"]["provenance"]["execution"]["native_thread_id"],
            "thread-a"
        );
    }

    #[test]
    fn warm_batch_continue_on_error_encodes_scope_failure_per_line() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::create_dir_all(dir.path().join(".memd")).unwrap();
        std::fs::write(dir.path().join(".memd/project_scope.json"), "{not json").unwrap();
        let input = concat!(
            "{\"tool\":\"memory.add\",\"arguments\":{\"tenant_id\":\"explicit\",\"text\":\"ok\"}}\n",
            "{\"tool\":\"memory.add\",\"arguments\":{\"text\":\"scope fails\"}}\n",
            "{\"tool\":\"memory.search\",\"arguments\":{\"tenant_id\":\"explicit\",\"query\":\"ok\"}}\n"
        );

        let output = scope_batch_jsonl_at(input, dir.path(), true).unwrap();
        let lines = output.lines().collect::<Vec<_>>();
        assert_eq!(lines.len(), 3);
        let first: Value = serde_json::from_str(lines[0]).unwrap();
        assert!(first.get("__memd_scope_error").is_none());
        let second: Value = serde_json::from_str(lines[1]).unwrap();
        assert!(second["__memd_scope_error"]
            .as_str()
            .unwrap()
            .contains("malformed"));
        let third: Value = serde_json::from_str(lines[2]).unwrap();
        assert!(third.get("__memd_scope_error").is_none());
    }

    #[test]
    fn warm_batch_rejects_client_supplied_scope_error_field() {
        let dir = tempfile::tempdir().unwrap();
        let input = concat!(
            "{\"tool\":\"memory.add\",\"arguments\":{\"tenant_id\":\"explicit\",\"text\":\"do not run\"},",
            "\"__memd_scope_error\":\"spoofed\"}\n"
        );

        let error = scope_batch_jsonl_at(input, dir.path(), true).unwrap_err();
        assert!(
            error.to_string().contains("reserved batch field"),
            "{error}"
        );
    }
}