codewhale-runtime 0.10.1

Headless Codewhale runtime, split out of the terminal UI one module at a time; no terminal UI dependency
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
//! Structured replacement for the old Markdown-backed native memory store.
//! All existing entry points share root/store.sqlite3 with the Context Lens.
//! MEMORY.md paths are legacy anchors, NOT an alternative source of truth.
//! Capture is candidate-only. Human review is through authenticated Lens routes.
use anyhow::{Result, anyhow, bail};
use codewhale_memory::hooks::{self, Boundary, HookEvent, HookPolicy};
use codewhale_memory::{
    Access, ByteCounter, ContextBudget, Draft, Evidence, Freshness, Memory, MemoryBackend, Recall,
    Scope, Snapshot, SourceKind, Status, Store, policy, workspace,
};
use std::{
    fs,
    path::{Path, PathBuf},
    process::Command,
};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MemoryScope {
    Global,
    Workspace,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MemoryHit {
    pub id: i64,
    pub text: String,
    pub source: PathBuf,
    pub line_start: usize,
    pub line_end: usize,
    pub stale: bool,
}
#[derive(Debug, Clone)]
pub struct NativeMemoryStore {
    root: PathBuf,
}
impl NativeMemoryStore {
    pub fn new(root: impl Into<PathBuf>) -> Self {
        Self { root: root.into() }
    }
    pub fn root(&self) -> &Path {
        &self.root
    }
    pub fn global_path(&self) -> PathBuf {
        self.root.join("global/MEMORY.md")
    }
    pub fn index_path(&self) -> PathBuf {
        self.root.join("store.sqlite3")
    }
    pub fn from_global_path(path: &Path) -> Option<Self> {
        if path.file_name()?.to_str()? != "MEMORY.md"
            || path.parent()?.file_name()?.to_str()? != "global"
        {
            return None;
        }
        let root = path.parent()?.parent()?;
        (root.file_name()?.to_str()? == "memory").then(|| Self::new(root))
    }
    pub fn from_memory_anchor(path: &Path) -> Self {
        Self::from_global_path(path)
            .unwrap_or_else(|| Self::new(path.parent().unwrap_or(Path::new(".")).join("memory")))
    }
    pub fn owner_scope() -> Scope {
        Scope::user("local", "owner")
    }
    pub fn workspace_scope(id: &str) -> Result<Scope> {
        safe_component(id)?;
        Ok(Self::owner_scope().workspace(id))
    }
    pub fn workspace_id(workspace: &Path) -> Result<Option<String>> {
        let out = Command::new("git")
            .arg("-C")
            .arg(workspace)
            .args(["config", "--get", "remote.origin.url"])
            .output()?;
        if !out.status.success() {
            return Ok(None);
        }
        let value = String::from_utf8_lossy(&out.stdout).trim().to_owned();
        Ok(if value.is_empty() {
            None
        } else {
            Some(policy::sha256(value.as_bytes()))
        })
    }
    pub fn open_structured(&self) -> Result<Store> {
        Ok(Store::open(self.index_path())?)
    }
    fn scopes(&self, id: Option<&str>) -> Result<Vec<Scope>> {
        let mut scopes = vec![Self::owner_scope()];
        if let Some(id) = id {
            scopes.push(Self::workspace_scope(id)?);
        }
        Ok(scopes)
    }
    fn hit(
        &self,
        store: &Store,
        access: &Access,
        m: &Memory,
        snapshot: &Snapshot,
    ) -> Result<MemoryHit> {
        // The scope's legacy anchor names the memory's origin for callers that
        // label records by source path; it is an identifier, not a live file.
        let source = match &m.draft.scope.workspace {
            Some(id) => self.root.join("workspace").join(id).join("MEMORY.md"),
            None => self.global_path(),
        };
        Ok(MemoryHit {
            id: store.numeric_id(access, &m.id)?,
            text: m.draft.body.clone(),
            source,
            line_start: 0,
            line_end: 0,
            stale: store.freshness(access, m, snapshot)? != Freshness::Current,
        })
    }
    fn access_for(&self, id: Option<&str>) -> Result<Access> {
        Ok(Access::operator(self.scopes(id)?)?)
    }
    pub fn remember(
        &self,
        scope: MemoryScope,
        workspace_id: Option<&str>,
        note: &str,
    ) -> Result<MemoryHit> {
        let selected = match scope {
            MemoryScope::Global => Self::owner_scope(),
            MemoryScope::Workspace => Self::workspace_scope(
                workspace_id.ok_or_else(|| anyhow!("workspace scope requires an id"))?,
            )?,
        };
        let access = Access::agent(vec![selected.clone()])?;
        let mut store = self.open_structured()?;
        let draft = Draft::note(
            selected,
            policy::excerpt(note, 80),
            note,
            Evidence {
                kind: SourceKind::Agent,
                uri: "codewhale:native-capture".into(),
                locator: "Unreviewed capture through legacy native entry point".into(),
                sha256: None,
                observed_at: 0,
            },
        );
        let key = format!("native-note:{}", policy::sha256(note.as_bytes()));
        let r = store.capture(&access, &key, draft)?;
        let mut hit = self.hit(&store, &access, &r.memory, &Snapshot::default())?;
        hit.text = format!("Pending review in Context Lens: {}", hit.text);
        Ok(hit)
    }
    /// Authenticated operator surfaces (`POST /v1/memory`, the Lens remember
    /// action): the explicit request IS the review, so the capture is promoted
    /// in the same call, exactly as `Action::Remember` does. Model-reachable
    /// paths must keep using `remember`, which can only propose a candidate.
    pub fn remember_reviewed(
        &self,
        scope: MemoryScope,
        workspace_id: Option<&str>,
        note: &str,
    ) -> Result<MemoryHit> {
        let selected = match scope {
            MemoryScope::Global => Self::owner_scope(),
            MemoryScope::Workspace => Self::workspace_scope(
                workspace_id.ok_or_else(|| anyhow!("workspace scope requires an id"))?,
            )?,
        };
        let access = Access::operator(vec![selected.clone()])?;
        let mut store = self.open_structured()?;
        let draft = Draft::note(
            selected,
            policy::excerpt(note, 80),
            note,
            Evidence {
                kind: SourceKind::User,
                uri: "codewhale:operator".into(),
                locator: "Explicit remember through an authenticated host surface".into(),
                sha256: None,
                observed_at: 0,
            },
        );
        let key = format!("operator-note:{}", policy::sha256(note.as_bytes()));
        let r = store.capture(&access, &key, draft)?;
        let memory = if r.memory.status == Status::Candidate {
            store.approve(
                &access,
                &r.memory.id,
                r.memory.revision,
                None,
                &Snapshot::default(),
            )?
        } else {
            r.memory
        };
        self.hit(&store, &access, &memory, &Snapshot::default())
    }
    pub fn revise(
        &self,
        scope: MemoryScope,
        workspace_id: Option<&str>,
        from: &str,
        to: &str,
        evidence: &str,
    ) -> Result<MemoryHit> {
        let selected = match scope {
            MemoryScope::Global => Self::owner_scope(),
            MemoryScope::Workspace => Self::workspace_scope(
                workspace_id.ok_or_else(|| anyhow!("workspace id required"))?,
            )?,
        };
        let access = Access::agent(vec![selected.clone()])?;
        let mut store = self.open_structured()?;
        let entries = store.list(&access, None, 500)?;
        let matches: Vec<_> = entries
            .iter()
            .filter(|m| m.status == Status::Active && m.draft.body.trim() == from.trim())
            .collect();
        if matches.len() != 1 {
            bail!(
                "correction must match exactly one active note on the bounded page; use Context Lens UUIDs"
            );
        }
        let old = matches[0];
        let mut draft = Draft::note(
            selected,
            policy::excerpt(to, 80),
            to,
            Evidence {
                kind: SourceKind::Agent,
                uri: format!("codewhale:memory:{}", old.id),
                locator: policy::excerpt(evidence, 256),
                sha256: None,
                observed_at: 0,
            },
        );
        draft.kind = old.draft.kind;
        draft.key = old.draft.key.clone();
        let request = format!(
            "native-correction:{}:{}",
            old.id,
            policy::sha256(format!("{to}\0{evidence}").as_bytes())
        );
        let r = store.capture(&access, &request, draft)?;
        let mut hit = self.hit(&store, &access, &r.memory, &Snapshot::default())?;
        hit.text = format!(
            "Correction candidate; original unchanged. Review in Context Lens: {}",
            hit.text
        );
        Ok(hit)
    }
    // Retirement is a review operation: the Lens `forget` action owns it. No
    // unreviewed delete entry point exists on this facade.
    pub fn prompt_block(
        &self,
        workspace: &Path,
        max_entries: usize,
        max_chars: usize,
    ) -> Result<Option<String>> {
        let id = Self::workspace_id(workspace)?;
        let access = self.access_for(id.as_deref())?;
        let store = self.open_structured()?;
        let snapshot = workspace::snapshot(workspace, store.dependency_paths(&access)?)?;
        let (_report, packet) = store.context_packet(
            &access,
            &Recall {
                limit: max_entries.clamp(1, 64),
                snapshot,
                ..Default::default()
            },
            &ByteCounter,
            &ContextBudget {
                max_units: max_chars,
                max_bytes: max_chars,
                max_entries,
            },
        )?;
        Ok(if packet.selected.is_empty() {
            None
        } else {
            Some(packet.text)
        })
    }
    /// Session-scoped structured access for engine boundaries. Scopes are
    /// owner + workspace (when the repository has an origin) + this session's
    /// context scope, so receipts are attributable to the session that made
    /// them and nothing else can write into them.
    fn session_binding(
        &self,
        workspace: &Path,
        session_id: &str,
    ) -> Result<(Store, Access, Scope)> {
        let id = Self::workspace_id(workspace)?;
        let context_scope = match &id {
            Some(id) => Self::workspace_scope(id)?,
            None => Self::owner_scope(),
        }
        .session(session_id);
        let mut scopes = vec![Self::owner_scope()];
        if let Some(id) = &id {
            scopes.push(Self::workspace_scope(id)?);
        }
        scopes.push(context_scope.clone());
        let access = Access::operator(scopes)?;
        Ok((self.open_structured()?, access, context_scope))
    }
    /// Like `prompt_block`, but the packet is prepared through the engine's
    /// durable receipt path: `memory_contexts` records which bytes and memory
    /// revisions were assembled, under this session's trace. The request key is
    /// bound to the recall inputs, so an identical rebuild is idempotent while
    /// changed inputs mint a new receipt. A receipt records preparation only —
    /// append/dispatch acknowledgements remain the engine's to make.
    ///
    /// Receipt failure falls back to the same packet bytes without one: the
    /// receipt is observability, never a precondition for shipping context.
    pub fn prompt_block_traced(
        &self,
        workspace: &Path,
        session_id: &str,
        max_entries: usize,
        max_chars: usize,
    ) -> Result<Option<String>> {
        if session_id.is_empty()
            || session_id.len() > 128
            || !session_id
                .bytes()
                .all(|b| b.is_ascii_alphanumeric() || b"_-.:".contains(&b))
        {
            return self.prompt_block(workspace, max_entries, max_chars);
        }
        let (store, access, context_scope) = self.session_binding(workspace, session_id)?;
        let snapshot = workspace::snapshot(workspace, store.dependency_paths(&access)?)?;
        let query = Recall {
            limit: max_entries.clamp(1, 64),
            snapshot,
            ..Default::default()
        };
        let key_material = serde_json::to_vec(
            &serde_json::json!({"q":query.query,"snapshot":query.snapshot,"limit":query.limit,
            "graph":query.expand_graph,"vector_cap":query.vector_scan_limit,"budget":[max_chars,max_entries],"unit":"utf8_bytes"}),
        )?;
        let request_key = format!("session-prompt:{}", policy::sha256(&key_material));
        let budget = ContextBudget {
            max_units: max_chars,
            max_bytes: max_chars,
            max_entries,
        };
        match store.prepare_context(
            &access,
            &context_scope,
            session_id,
            &request_key,
            &query,
            &ByteCounter,
            &budget,
        ) {
            Ok(prepared) => Ok(if prepared.packet.selected.is_empty() {
                None
            } else {
                Some(prepared.packet.text)
            }),
            Err(_) => self.prompt_block(workspace, max_entries, max_chars),
        }
    }
    /// Run the session-start boundary for `session_id`. The hook plan maps it
    /// to ReconcilePendingOperations, realized here as detection — contexts
    /// this session prepared but never saw dispatch-acknowledged — because the
    /// store owns the receipts and only a host can replay them. Completion is
    /// recorded durably, so a restarted session reconciles once.
    /// Returns the number of interrupted contexts found.
    pub fn session_start(&self, workspace: &Path, session_id: &str) -> Result<usize> {
        if session_id.is_empty()
            || session_id.len() > 128
            || !session_id
                .bytes()
                .all(|b| b.is_ascii_alphanumeric() || b"_-.:".contains(&b))
        {
            bail!("invalid session id for memory reconcile");
        }
        let (store, access, context_scope) = self.session_binding(workspace, session_id)?;
        let snapshot = workspace::snapshot(workspace, store.dependency_paths(&access)?)?;
        let event = HookEvent {
            id: format!("session-start:{session_id}"),
            boundary: Boundary::SessionStart,
            trace_id: session_id.to_owned(),
            sequence: 0,
            observed_at: store.timestamp(),
            explicit_user_request: false,
            success: None,
        };
        let plan = hooks::plan(
            &HookPolicy {
                enabled: true,
                auto_candidates: false,
                recall_on_task: true,
            },
            &event,
        );
        let mut interrupted = 0usize;
        for intent in &plan.intents {
            if intent == &hooks::Intent::ReconcilePendingOperations {
                interrupted = store
                    .context_receipts(&access, Some(session_id), &snapshot)?
                    .iter()
                    .filter(|r| r.stage != "dispatched")
                    .count();
            }
        }
        store.record_completed_hook(&access, &context_scope, &event)?;
        Ok(interrupted)
    }
    pub fn search(&self, query: &str, limit: usize) -> Result<Vec<MemoryHit>> {
        self.search_scoped(None, None, query, limit)
    }
    pub fn search_for_workspace(
        &self,
        workspace: &Path,
        query: &str,
        limit: usize,
    ) -> Result<Vec<MemoryHit>> {
        let id = Self::workspace_id(workspace)?;
        self.search_in_workspace(id.as_deref(), workspace, query, limit)
    }
    /// [`Self::search_for_workspace`] for a caller that already resolved the
    /// workspace's memory identity.
    pub fn search_in_workspace(
        &self,
        workspace_id: Option<&str>,
        workspace: &Path,
        query: &str,
        limit: usize,
    ) -> Result<Vec<MemoryHit>> {
        self.search_scoped(workspace_id, Some(workspace), query, limit)
    }
    fn search_scoped(
        &self,
        id: Option<&str>,
        root: Option<&Path>,
        query: &str,
        limit: usize,
    ) -> Result<Vec<MemoryHit>> {
        let access = self.access_for(id)?;
        let store = self.open_structured()?;
        let snapshot = match root {
            Some(root) => workspace::snapshot(root, store.dependency_paths(&access)?)?,
            None => Snapshot::default(),
        };
        let report = store.recall(
            &access,
            &Recall {
                query: query.into(),
                limit,
                snapshot: snapshot.clone(),
                ..Default::default()
            },
        )?;
        report
            .hits
            .iter()
            .map(|h| self.hit(&store, &access, &h.memory, &snapshot))
            .collect()
    }
    pub fn get_for_workspace(&self, workspace: &Path, id: i64) -> Result<Option<MemoryHit>> {
        let workspace_id = Self::workspace_id(workspace)?;
        self.get_scoped(workspace_id.as_deref(), Some(workspace), id)
    }
    fn get_scoped(
        &self,
        workspace_id: Option<&str>,
        root: Option<&Path>,
        id: i64,
    ) -> Result<Option<MemoryHit>> {
        let access = self.access_for(workspace_id)?;
        let store = self.open_structured()?;
        let m = match store.get_numeric(&access, id) {
            Ok(m) => m,
            Err(codewhale_memory::Error::NotFound) => return Ok(None),
            Err(e) => return Err(e.into()),
        };
        if !matches!(m.status, Status::Active | Status::Stale) {
            return Ok(None);
        }
        let snapshot = match root {
            Some(root) => workspace::snapshot(root, store.dependency_paths(&access)?)?,
            None => Snapshot::default(),
        };
        Ok(Some(self.hit(&store, &access, &m, &snapshot)?))
    }
    pub fn list_all(
        &self,
        scope: Option<MemoryScope>,
        workspace_id: Option<&str>,
        limit: usize,
    ) -> Result<Vec<MemoryHit>> {
        let scopes = match scope {
            None => self.scopes(workspace_id)?,
            Some(MemoryScope::Global) => vec![Self::owner_scope()],
            Some(MemoryScope::Workspace) => vec![Self::workspace_scope(
                workspace_id.ok_or_else(|| anyhow!("workspace id required"))?,
            )?],
        };
        let access = Access::operator(scopes)?;
        let store = self.open_structured()?;
        // Entry surfaces show only live memory; candidates and reviewed-off
        // entries are visible in the Context Lens, not here.
        store
            .list(&access, None, limit)?
            .iter()
            .filter(|m| matches!(m.status, Status::Active | Status::Stale))
            .map(|m| self.hit(&store, &access, m, &Snapshot::default()))
            .collect()
    }
    pub fn import_legacy(&self, path: &Path) -> Result<bool> {
        if !path.is_file() {
            return Ok(false);
        }
        let meta = fs::symlink_metadata(path)?;
        if meta.file_type().is_symlink() || meta.len() > 1024 * 1024 {
            bail!("legacy import refused: symlink or size bound");
        }
        let text = fs::read_to_string(path)?;
        let scope = Self::owner_scope();
        let access = Access::operator(vec![scope.clone()])?;
        let mut store = self.open_structured()?;
        let report = codewhale_memory::import::markdown(
            &mut store,
            &access,
            &scope,
            "codewhale:legacy-memory",
            &text,
        )?;
        if !report.rejected.is_empty() {
            bail!(
                "legacy import left {} rejected notes unchanged; inspect before retry",
                report.rejected.len()
            );
        }
        Ok(report.created > 0)
    }
    pub fn export(&self) -> Result<String> {
        let store = self.open_structured()?;
        let owner = Access::operator(vec![Self::owner_scope()])?;
        let scopes = store.local_scope_inventory(&owner)?;
        let mut output = String::from(
            "# CodeWhale memory export\n\nNot an instruction file. This Markdown is not authoritative.\n",
        );
        for scope in scopes {
            let access = Access::operator(vec![scope])?;
            let mut after = None;
            loop {
                let batch = store.list(&access, after.as_deref(), 500)?;
                if batch.is_empty() {
                    break;
                }
                for m in &batch {
                    output.push_str(&format!(
                        "\n## {} [{}] {}\n\n{}\n",
                        m.id,
                        m.status.as_str(),
                        m.draft.title,
                        m.draft.body
                    ));
                }
                after = batch.last().map(|m| m.id.clone());
            }
        }
        Ok(output)
    }
    pub fn reindex(&self) -> Result<usize> {
        let mut store = self.open_structured()?;
        let access = Access::operator(vec![Self::owner_scope()])?;
        store.reindex(&access)?;
        let mut count = 0;
        for scope in store.local_scope_inventory(&access)? {
            let access = Access::operator(vec![scope])?;
            let mut after = None;
            loop {
                let batch = store.list(&access, after.as_deref(), 500)?;
                if batch.is_empty() {
                    break;
                }
                count += batch.len();
                after = batch.last().map(|m| m.id.clone());
            }
        }
        Ok(count)
    }
    pub fn delete_all(&self, scope: Option<MemoryScope>, workspace_id: Option<&str>) -> Result<()> {
        let scopes = match scope {
            Some(MemoryScope::Global) => vec![Self::owner_scope()],
            Some(MemoryScope::Workspace) => vec![Self::workspace_scope(
                workspace_id.ok_or_else(|| anyhow!("workspace id required"))?,
            )?],
            None => {
                let store = self.open_structured()?;
                let owner = Access::operator(vec![Self::owner_scope()])?;
                let mut scopes = store.local_scope_inventory(&owner)?;
                if scopes.len() >= 10000 {
                    bail!("Scope inventory reached its bound; delete narrower scopes explicitly");
                }
                if scopes.is_empty() {
                    scopes.push(Self::owner_scope());
                }
                scopes
            }
        };
        let access = Access::operator(scopes)?;
        let mut store = self.open_structured()?;
        loop {
            let batch = store.list(&access, None, 500)?;
            if batch.is_empty() {
                break;
            }
            for m in batch {
                match store.get(&access, &m.id) {
                    Ok(current) => {
                        store.forget(&access, &current.id, current.revision)?;
                    }
                    Err(codewhale_memory::Error::NotFound) => {}
                    Err(e) => return Err(e.into()),
                }
            }
        }
        Ok(())
    }
}
fn safe_component(value: &str) -> Result<()> {
    if value.len() != 64
        || !value
            .bytes()
            .all(|c| c.is_ascii_hexdigit() && !c.is_ascii_uppercase())
    {
        bail!("workspace identity must be a lowercase SHA-256 hash");
    }
    Ok(())
}

/// Compose the user-memory prompt block for the native store resolved from a
/// memory path. Single seam used by the engine, the TUI system-prompt
/// builder, and the context report so all three describe the same bytes.
/// Returns `None` when memory is disabled, the path is not a native
/// `memory/global/MEMORY.md` layout, or there is nothing worth injecting.
/// The block is a `codewhale.memory.context.v1` envelope: memory entries are
/// untrusted evidence, never instructions.
#[must_use]
pub fn native_prompt_block(enabled: bool, memory_path: &Path, workspace: &Path) -> Option<String> {
    if !enabled {
        return None;
    }
    NativeMemoryStore::from_global_path(memory_path)?
        .prompt_block(workspace, 32, 12_000)
        .ok()
        .flatten()
}

/// The engine's session-boundary variant: identical bytes plus a durable
/// `prepared` context receipt under the session's trace, so the Context Lens
/// can show what was assembled for it. Diagnostics and previews keep using
/// `native_prompt_block` — a receipt is only meaningful when it names a real
/// session. The receipt never gates the packet.
#[must_use]
pub fn native_prompt_block_traced(
    enabled: bool,
    memory_path: &Path,
    workspace: &Path,
    session_id: &str,
) -> Option<String> {
    if !enabled {
        return None;
    }
    NativeMemoryStore::from_global_path(memory_path)?
        .prompt_block_traced(workspace, session_id, 32, 12_000)
        .ok()
        .flatten()
}