ultron-kernel-apis 1.0.0

Host operations behind the thin Python kernel APIs: code skills and the skill catalog, memory, hints, images
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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
//! `ultron memory import --from claude-code|codex`: the notes other harnesses keep on disk become typed memory
//! records (crate::memory_records), stored through `memory.propose` with the user's evidence, so the same secret
//! check refuses what holds a secret. Which files are read follows Mirror (`memory/import/*.rs`):
//!
//! - Claude Code: every `<config>/projects/<encoded project>/memory/` directory (`<config>` is `CLAUDE_CONFIG_DIR`,
//!   else `~/.claude`): one note per Markdown file, `MEMORY.md` (the index), dotfiles, symlinks and `old/` left
//!   out. YAML frontmatter `name` gives the key, `description` the situation and `metadata.type` (or `type`) the
//!   record type: `feedback` → preference, `user`/`project` → fact, else reference. A project directory whose
//!   encoded name maps back to exactly one existing directory makes its notes `project` records of that
//!   directory; otherwise they are `global`.
//! - Codex: `<codex home>/memories/` (`CODEX_HOME`, else `~/.codex`): `memory_summary.md` (User Profile → fact,
//!   User preferences → preference, General Tips → procedure) and `MEMORY.md` (per task group: User preferences,
//!   Reusable knowledge → fact, Failures and how to do differently → pitfall; `[Task n]` tags name the situation).
//!   Bullets are the unit. All `global`.
//!
//! Every record is tagged with its source file (`source`: harness and home-relative path). Keys are the note's
//! slug plus a hash of what identifies it in its source (the file for Claude Code, file and text for Codex), so a
//! rerun finds the same keys: a record whose key is already indexed with the same text and fields is unchanged,
//! one with other text is replaced. Notes over their type's cap, or holding a secret, are refused.

use crate::memory::{MemoryBackend, MemoryError, MemoryService, worker_gate};
use crate::memory_records::{MAX_SITUATION_BYTES, RecordIndex, Typed, content_cap};
use kernel::Signal;
use kernel::store::FileDocumentStore;
use serde_json::{Map, Value, json};
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use yaml_rust2::{Yaml, YamlLoader};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Harness {
    ClaudeCode,
    Codex,
}

impl Harness {
    /// `--from` names: `claude-code` (or `claude`) and `codex`.
    pub fn parse(name: &str) -> Option<Harness> {
        match name {
            "claude-code" | "claude" => Some(Harness::ClaudeCode),
            "codex" => Some(Harness::Codex),
            _ => None,
        }
    }

    pub fn label(self) -> &'static str {
        match self {
            Harness::ClaudeCode => "Claude Code",
            Harness::Codex => "Codex",
        }
    }
}

/// One note as a reader produced it.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Note {
    pub key: String,
    pub kind: &'static str,
    pub situation: String,
    pub content: String,
    /// The file the text came from.
    pub file: PathBuf,
}

/// One directory of notes.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Source {
    pub harness: Harness,
    pub directory: PathBuf,
    /// The project the notes belong to (`project` scope), `None`: `global`.
    pub project: Option<PathBuf>,
}

/// Where a harness keeps its notes, given the injected environment.
pub fn notes_root(harness: Harness, home: &Path, var: &dyn Fn(&str) -> Option<String>) -> PathBuf {
    let configured = |name: &str| var(name).filter(|value| !value.trim().is_empty()).map(PathBuf::from);
    match harness {
        Harness::ClaudeCode => configured("CLAUDE_CONFIG_DIR")
            .unwrap_or_else(|| home.join(".claude"))
            .join("projects"),
        Harness::Codex => configured("CODEX_HOME")
            .unwrap_or_else(|| home.join(".codex"))
            .join("memories"),
    }
}

/// The note directories of `harness` under `root` ([`notes_root`]), in path order.
pub fn discover(harness: Harness, root: &Path) -> Vec<Source> {
    match harness {
        Harness::ClaudeCode => {
            let mut projects: Vec<PathBuf> = std::fs::read_dir(root)
                .into_iter()
                .flatten()
                .flatten()
                .map(|entry| entry.path())
                .filter(|project| project.join("memory").is_dir())
                .collect();
            projects.sort();
            projects
                .into_iter()
                .map(|project| Source {
                    harness,
                    directory: project.join("memory"),
                    project: project_path(&project),
                })
                .collect()
        }
        Harness::Codex => {
            if root.join("memory_summary.md").is_file() || root.join("MEMORY.md").is_file() {
                vec![Source {
                    harness,
                    directory: root.to_path_buf(),
                    project: None,
                }]
            } else {
                Vec::new()
            }
        }
    }
}

/// The checkout a Claude Code project directory stands for, when its encoded name (`-home-me-src-repo`) maps back
/// to exactly one existing directory (a `-` is a separator or a literal hyphen).
pub fn project_path(project_dir: &Path) -> Option<PathBuf> {
    let name = project_dir.file_name()?.to_string_lossy().into_owned();
    let segments: Vec<&str> = name.trim_start_matches('-').split('-').collect();
    let mut matches = Vec::new();
    resolve_segments(Path::new("/"), &segments, &mut matches);
    (matches.len() == 1).then(|| matches.remove(0))
}

fn resolve_segments(base: &Path, segments: &[&str], matches: &mut Vec<PathBuf>) {
    if matches.len() > 1 {
        return;
    }
    if segments.is_empty() {
        matches.push(base.to_path_buf());
        return;
    }
    for take in 1..=segments.len() {
        let candidate = base.join(segments[..take].join("-"));
        if candidate.is_dir() {
            resolve_segments(&candidate, &segments[take..], matches);
        }
    }
}

impl Source {
    pub fn read(&self) -> Result<Vec<Note>, String> {
        match self.harness {
            Harness::ClaudeCode => claude_code_notes(&self.directory),
            Harness::Codex => codex_notes(&self.directory),
        }
    }
}

fn collect_markdown(directory: &Path, out: &mut Vec<PathBuf>) -> Result<(), String> {
    let entries = std::fs::read_dir(directory).map_err(|e| format!("{}: {e}", directory.display()))?;
    for entry in entries {
        let entry = entry.map_err(|e| e.to_string())?;
        let path = entry.path();
        let name = entry.file_name().to_string_lossy().into_owned();
        let file_type = entry.file_type().map_err(|e| e.to_string())?;
        if name.starts_with('.') || file_type.is_symlink() {
            continue;
        }
        if file_type.is_dir() {
            if name != "old" {
                collect_markdown(&path, out)?;
            }
        } else if file_type.is_file() && name.ends_with(".md") && name != "MEMORY.md" {
            out.push(path);
        }
    }
    Ok(())
}

/// Leading `---` fenced YAML and the body after it.
fn split_frontmatter(text: &str) -> (Option<&str>, &str) {
    let Some(rest) = text.strip_prefix("---\n").or_else(|| text.strip_prefix("---\r\n")) else {
        return (None, text);
    };
    let mut cursor = 0;
    for line in rest.split_inclusive('\n') {
        if line.trim_end() == "---" {
            return (Some(&rest[..cursor]), &rest[cursor + line.len()..]);
        }
        cursor += line.len();
    }
    (None, text)
}

fn yaml_text<'a>(yaml: &'a Yaml, key: &str) -> Option<&'a str> {
    yaml[key].as_str().map(str::trim).filter(|text| !text.is_empty())
}

/// Every note under a Claude Code memory directory, in path order.
pub fn claude_code_notes(directory: &Path) -> Result<Vec<Note>, String> {
    let mut files = Vec::new();
    collect_markdown(directory, &mut files)?;
    files.sort();
    let mut notes = Vec::new();
    for path in files {
        let text = std::fs::read_to_string(&path).map_err(|e| format!("{}: {e}", path.display()))?;
        let (front, body) = split_frontmatter(&text);
        let front = front
            .and_then(|yaml| YamlLoader::load_from_str(yaml).ok())
            .and_then(|mut docs| (!docs.is_empty()).then(|| docs.remove(0)))
            .unwrap_or(Yaml::Null);
        let body = body.trim();
        if body.is_empty() {
            continue;
        }
        let stem = path
            .file_stem()
            .map(|stem| stem.to_string_lossy().into_owned())
            .unwrap_or_default();
        let name = yaml_text(&front, "name").map(str::to_string).unwrap_or(stem);
        let kind = match yaml_text(&front["metadata"], "type").or_else(|| yaml_text(&front, "type")) {
            Some("feedback") => "preference",
            Some("user" | "project") => "fact",
            _ => "reference",
        };
        notes.push(Note {
            key: slug_key(&name),
            kind,
            situation: yaml_text(&front, "description").unwrap_or_default().to_string(),
            content: body.to_string(),
            file: path,
        });
    }
    Ok(notes)
}

struct Section {
    heading: String,
    lines: Vec<String>,
}

impl Section {
    /// Bullets, indented continuation text joined with a space; an indented list marker starts its own bullet.
    fn bullets(&self) -> Vec<String> {
        let mut bullets: Vec<String> = Vec::new();
        for line in &self.lines {
            let trimmed = line.trim_start_matches([' ', '\t']);
            if let Some(item) = trimmed.strip_prefix("- ") {
                bullets.push(item.trim().to_string());
            } else if (line.starts_with(' ') || line.starts_with('\t'))
                && let Some(last) = bullets.last_mut()
            {
                last.push(' ');
                last.push_str(line.trim());
            }
        }
        bullets
    }

    fn paragraphs(&self) -> Vec<String> {
        self.lines
            .iter()
            .map(|line| line.trim())
            .filter(|line| !line.is_empty() && !line.starts_with("- "))
            .map(str::to_string)
            .collect()
    }
}

/// `##` sections; any other heading ends the current one.
fn sections(text: &str) -> Vec<Section> {
    let mut sections: Vec<Section> = Vec::new();
    for line in text.lines() {
        if let Some(heading) = line.strip_prefix("## ") {
            sections.push(Section {
                heading: heading.trim().to_string(),
                lines: Vec::new(),
            });
        } else if line.starts_with('#') {
            sections.push(Section {
                heading: String::new(),
                lines: Vec::new(),
            });
        } else if let Some(section) = sections.last_mut() {
            section.lines.push(line.to_string());
        }
    }
    sections.retain(|section| !section.heading.is_empty());
    sections
}

fn note(key: &str, kind: &'static str, situation: &str, content: &str, file: &Path) -> Note {
    Note {
        key: slug_key(key),
        kind,
        situation: situation.to_string(),
        content: content.to_string(),
        file: file.to_path_buf(),
    }
}

fn summary_notes(text: &str, file: &Path) -> Vec<Note> {
    let mut notes = Vec::new();
    for section in sections(text) {
        match section.heading.as_str() {
            "User Profile" => {
                let profile = section.paragraphs().join(" ");
                if !profile.is_empty() {
                    notes.push(note(
                        "codex-user-profile",
                        "fact",
                        "who the user is, as Codex summarized it",
                        &profile,
                        file,
                    ));
                }
            }
            "User preferences" => {
                for bullet in section.bullets() {
                    notes.push(note(
                        &format!("codex-pref-{}", slug_key(&bullet)),
                        "preference",
                        "how the user wants work done, as Codex summarized it",
                        &bullet,
                        file,
                    ));
                }
            }
            "General Tips" => {
                for bullet in section.bullets() {
                    notes.push(note(
                        &format!("codex-tip-{}", slug_key(&bullet)),
                        "procedure",
                        "working practice Codex learned across tasks",
                        &bullet,
                        file,
                    ));
                }
            }
            _ => {}
        }
    }
    notes
}

/// `Task <n>: <title>` of a heading.
fn task_heading(heading: &str) -> Option<(String, String)> {
    let rest = heading.strip_prefix("Task ")?;
    let (number, title) = rest.split_once(": ")?;
    (!number.is_empty() && number.bytes().all(|b| b.is_ascii_digit()) && !title.trim().is_empty())
        .then(|| (number.to_string(), title.trim().to_string()))
}

/// A trailing `[Task <n>]` tag: its number and where it starts (with the whitespace before it).
fn trailing_task_tag(content: &str) -> Option<(String, usize)> {
    let trimmed = content.trim_end();
    let inner = trimmed.strip_suffix(']')?;
    let open = inner.rfind("[Task ")?;
    let number = &inner[open + "[Task ".len()..];
    if number.is_empty() || !number.bytes().all(|b| b.is_ascii_digit()) {
        return None;
    }
    let start = content[..open].trim_end().len();
    Some((number.to_string(), start))
}

fn memory_notes(text: &str, file: &Path) -> Vec<Note> {
    let mut tasks: BTreeMap<String, String> = BTreeMap::new();
    let mut notes = Vec::new();
    for section in sections(text) {
        if let Some((number, title)) = task_heading(&section.heading) {
            tasks.insert(number, title);
            continue;
        }
        let (kind, prefix) = match section.heading.as_str() {
            "User preferences" => ("preference", "codex-pref"),
            "Reusable knowledge" => ("fact", "codex-fact"),
            "Failures and how to do differently" => ("pitfall", "codex-pitfall"),
            _ => continue,
        };
        for bullet in section.bullets() {
            let mut situation = String::from("from the user's Codex task memory");
            let mut content = bullet.clone();
            while let Some((number, start)) = trailing_task_tag(&content) {
                if let Some(title) = tasks.get(&number) {
                    situation = format!("Codex task: {title}");
                }
                content.truncate(start);
            }
            if content.trim().is_empty() {
                continue;
            }
            notes.push(note(
                &format!("{prefix}-{}", slug_key(&content)),
                kind,
                &situation,
                content.trim(),
                file,
            ));
        }
    }
    notes
}

/// Notes of a Codex memories directory: `memory_summary.md`, then `MEMORY.md`.
pub fn codex_notes(directory: &Path) -> Result<Vec<Note>, String> {
    let mut notes = Vec::new();
    let mut read_any = false;
    for (name, parse) in [
        ("memory_summary.md", summary_notes as fn(&str, &Path) -> Vec<Note>),
        ("MEMORY.md", memory_notes),
    ] {
        let path = directory.join(name);
        match std::fs::read_to_string(&path) {
            Ok(text) => {
                read_any = true;
                notes.extend(parse(&text, &path));
            }
            Err(error) if error.kind() == std::io::ErrorKind::NotFound => {}
            Err(error) => return Err(format!("{}: {error}", path.display())),
        }
    }
    if !read_any {
        return Err(format!("no Codex memory files found in {}", directory.display()));
    }
    Ok(notes)
}

/// A kebab-case key of at most 60 bytes from free text (Mirror `slug_key`).
pub fn slug_key(text: &str) -> String {
    let mut key = String::new();
    let mut last_dash = true;
    for character in text.chars().flat_map(char::to_lowercase) {
        if character.is_ascii_alphanumeric() {
            key.push(character);
            last_dash = false;
        } else if !last_dash {
            key.push('-');
            last_dash = true;
        }
        if key.len() >= 60 {
            break;
        }
    }
    let key = key.trim_matches('-').to_string();
    if key.is_empty() { "note".to_string() } else { key }
}

/// Keep newlines and tabs, drop other control characters and carriage returns, trim.
fn clean(text: &str) -> String {
    text.replace("\r\n", "\n")
        .chars()
        .filter(|ch| !ch.is_control() || *ch == '\n' || *ch == '\t')
        .collect::<String>()
        .trim()
        .to_string()
}

fn truncate_bytes(text: &str, max: usize) -> &str {
    let mut end = 0;
    for (offset, ch) in text.char_indices() {
        if offset + ch.len_utf8() > max {
            break;
        }
        end = offset + ch.len_utf8();
    }
    &text[..end]
}

/// `path` with the home directory written as `~`.
pub fn home_relative(path: &Path, home: &Path) -> String {
    match path.strip_prefix(home) {
        Ok(rest) if rest.as_os_str().is_empty() => "~".into(),
        Ok(rest) => format!("~/{}", rest.display()),
        Err(_) => path.display().to_string(),
    }
}

/// The note's key: its slug and 12 hex digits of what identifies it within its source (the `occurrence`-th note
/// with that identity, past the first, hashes its number too).
fn note_key(harness: Harness, note: &Note, occurrence: usize) -> String {
    let file = note.file.display().to_string();
    let mut identity = match harness {
        Harness::ClaudeCode => file,
        Harness::Codex => format!("{file}\0{}", clean(&note.content)),
    };
    if occurrence > 1 {
        identity.push_str(&format!("\0{occurrence}"));
    }
    let suffix = &jsrt::hash::sha256_hex(identity.as_bytes())[..12];
    let base = note.key.trim_end_matches('-');
    let end = base.len().min(64 - 1 - suffix.len());
    format!("{}-{suffix}", base[..end].trim_end_matches('-'))
}

/// What an import does with one note.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Action {
    /// A new record.
    Add,
    /// The key is indexed with other text or fields: that record (id) is replaced.
    Replace(String),
    /// The key is indexed with this exact record (id).
    Unchanged(String),
    /// Not imported, and why.
    Refuse(String),
}

/// One planned record.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Planned {
    pub typed: Typed,
    pub content: String,
    pub scope: &'static str,
    /// The directory a `project` record belongs to.
    pub project: Option<PathBuf>,
    /// sha256 of the source file.
    pub file_sha: String,
    pub action: Action,
}

/// The identity tags a record of `scope` gets (as a session in `project` would tag it; hindsight
/// `worker_scope_tags`): computed by the caller, which knows the server's tagging.
pub type ScopeTags<'a> = &'a dyn Fn(Option<&Path>) -> Map<String, Value>;

/// The plan for `sources`: every note with its key, scope and action against `index`.
pub fn plan(sources: &[Source], home: &Path, index: &RecordIndex, tags: ScopeTags<'_>) -> Result<Vec<Planned>, String> {
    let mut planned = Vec::new();
    for source in sources {
        let notes = source.read()?;
        let scope = if source.project.is_some() { "project" } else { "global" };
        let filters = tags(source.project.as_deref());
        let mut seen: BTreeMap<String, usize> = BTreeMap::new();
        for note in notes {
            let count = seen.entry(note_key(source.harness, &note, 1)).or_default();
            *count += 1;
            let key = note_key(source.harness, &note, *count);
            let situation = clean(&note.situation).replace(['\n', '\t'], " ");
            let situation = match truncate_bytes(&situation, MAX_SITUATION_BYTES).trim() {
                "" => "from the user's imported notes".to_string(),
                text => text.to_string(),
            };
            let content = clean(&note.content);
            let typed = Typed {
                kind: note.kind.to_string(),
                key: Some(key.clone()),
                situation,
                path_globs: Vec::new(),
                source: Some(format!(
                    "{} {}",
                    source.harness.label(),
                    home_relative(&note.file, home)
                )),
            };
            let file_sha = std::fs::read(&note.file)
                .map(|bytes| jsrt::hash::sha256_hex(&bytes))
                .map_err(|e| format!("{}: {e}", note.file.display()))?;
            let action = if content.len() > content_cap(note.kind) {
                Action::Refuse(format!(
                    "over the {}-byte cap of a {} record",
                    content_cap(note.kind),
                    note.kind
                ))
            } else if host::secrets::contains_sensitive_memory(&content)
                || host::secrets::contains_sensitive_memory(&typed.screened())
            {
                Action::Refuse("holds a secret or personal identifier".into())
            } else {
                match index.by_key(&key, scope, &filters)? {
                    Some(entry)
                        if entry.text_hash == jsrt::hash::sha256_hex(content.as_bytes()) && entry.typed == typed =>
                    {
                        Action::Unchanged(entry.id)
                    }
                    Some(entry) => Action::Replace(entry.id),
                    None => Action::Add,
                }
            };
            planned.push(Planned {
                typed,
                content,
                scope,
                project: source.project.clone(),
                file_sha,
                action,
            });
        }
    }
    Ok(planned)
}

/// The `memory.propose` payload of a planned record: the user's own notes, with the source file as evidence.
pub fn propose_payload(planned: &Planned) -> Map<String, Value> {
    let typed = &planned.typed;
    let source = typed.source.clone().unwrap_or_default();
    let mut payload = json!({
        "text": planned.content,
        "evidence": [{"ref": source, "sha256": planned.file_sha}],
        "scope": planned.scope,
        "evidenceClass": "user_statement",
        "type": typed.kind,
        "situation": typed.situation,
        "source": source,
    });
    if let Some(key) = &typed.key {
        payload["key"] = key.clone().into();
    }
    payload.as_object().cloned().unwrap_or_default()
}

/// Store one planned record (an `Add` or `Replace`) through `service`: the written operation, or why not.
pub fn import_one(service: &MemoryService, planned: &Planned) -> Result<Value, MemoryError> {
    service.propose(&propose_payload(planned), &Signal::new())
}

/// The memory backend of one scope group (`None`: global notes; `Some`: a project's).
pub type BackendFor<'a> = &'a dyn Fn(Option<&Path>) -> Result<Arc<dyn MemoryBackend>, String>;

/// What became of one planned note.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Imported {
    /// A new record (its memory id).
    Stored(String),
    /// The record that replaced the indexed one (its memory id).
    Replaced(String),
    Unchanged,
    /// Not imported, and why.
    Refused(String),
    /// The write was taken but not stored (its operation state).
    NotStored(String),
    Failed(String),
}

/// Import every planned note: one memory service per scope group, over the one import `journal` (each loads what
/// the previous one wrote). `each` hears every note's outcome, in order.
pub fn import_all(
    planned: &[Planned],
    journal: &Path,
    index: Arc<RecordIndex>,
    backend_for: BackendFor<'_>,
    each: &mut dyn FnMut(&Planned, Imported) -> Result<(), String>,
) -> Result<(), String> {
    let mut service: Option<(Option<PathBuf>, MemoryService)> = None;
    for item in planned {
        let outcome = match &item.action {
            Action::Unchanged(_) => Imported::Unchanged,
            Action::Refuse(reason) => Imported::Refused(reason.clone()),
            Action::Add | Action::Replace(_) => {
                if service.as_ref().is_none_or(|(project, _)| *project != item.project) {
                    let memory = MemoryService::new(
                        Arc::new(FileDocumentStore::new(journal)),
                        backend_for(item.project.as_deref())?,
                        worker_gate(),
                    )
                    .map_err(|e| e.to_string())?
                    .with_records(index.clone());
                    service = Some((item.project.clone(), memory));
                }
                let Some((_, memory)) = &service else {
                    continue;
                };
                match import_one(memory, item) {
                    Ok(op) if op["state"] == "sensitive" || op["state"] == "skipped" => {
                        Imported::Refused("holds a secret or personal identifier".into())
                    }
                    Ok(op) if op["phase"] == "backend" => {
                        let id = op["memoryId"].as_str().unwrap_or_default().to_string();
                        match item.action {
                            Action::Replace(_) => Imported::Replaced(id),
                            _ => Imported::Stored(id),
                        }
                    }
                    Ok(op) => Imported::NotStored(op["state"].as_str().unwrap_or("?").to_string()),
                    Err(error) => Imported::Failed(error.to_string()),
                }
            }
        };
        each(item, outcome)?;
    }
    Ok(())
}