Skip to main content

agentmux_srv_core/backend/transcript/
origin_resign.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! A fork's copy of its parent's transcript, signed for the fork
5//! (docs/specs/SPEC_MESSAGE_ORIGIN_AND_ONE_CONTAINER_2026_10_25.md ยง6.1, "A fork").
6//!
7//! A record's signature binds its owner (`origin_sig`), so the lines a fork copies from its parent's
8//! zone do not verify for the fork, and the human's own earlier messages would show in the container.
9//! After the copy, each user line that verifies for the parent gets the signature for the fork. A line
10//! that does not verify is left as it is, so a forged line is never made good by forking. The new
11//! signature has the old one's length, so a line keeps its size and the receive-time sidecar's offsets
12//! stay right.
13
14use crate::backend::storage::filestore::FileStore;
15
16use super::origin_sig::{resign_line, SIG_LEN};
17
18/// How much of the transcript is read at a time: lines are handled whole, so a line longer than this
19/// widens the read.
20const CHUNK: i64 = 4 * 1024 * 1024;
21
22/// [`resign_zone`] for bytes in memory (whole lines): a copy signed before it is written needs no
23/// rewrite in place, which would drop the file's line epoch. How many lines it signed.
24pub fn resign_buffer(data: &mut [u8], from_owner: &str, to_owner: &str) -> u64 {
25    let (mut line_start, mut signed) = (0usize, 0u64);
26    while line_start < data.len() {
27        let line_end = data[line_start..].iter().position(|b| *b == b'\n').map_or(data.len(), |i| line_start + i + 1);
28        if let Some((sig_at, new)) = resign_line(from_owner, to_owner, &data[line_start..line_end]) {
29            debug_assert_eq!(new.len(), SIG_LEN);
30            let at = line_start + sig_at;
31            data[at..at + SIG_LEN].copy_from_slice(new.as_bytes());
32            signed += 1;
33        }
34        line_start = line_end;
35    }
36    signed
37}
38
39/// Sign for `to_owner` the lines of `file` in `zone` that verify for `from_owner`; how many it signed.
40pub fn resign_zone(gfs: &FileStore, zone: &str, file: &str, from_owner: &str, to_owner: &str) -> Result<u64, String> {
41    let Some(stat) = gfs.stat(zone, file).map_err(|e| format!("stat transcript: {e}"))? else { return Ok(0) };
42    let (mut pos, mut signed, mut window) = (0i64, 0u64, CHUNK);
43    while pos < stat.size {
44        let (at, data) = gfs.read_at(zone, file, pos, window).map_err(|e| format!("read transcript: {e}"))?;
45        if data.is_empty() {
46            break;
47        }
48        // Whole lines only; the last one is cut by the window unless the file ends there.
49        let end = if at + data.len() as i64 >= stat.size { data.len() } else { data.iter().rposition(|b| *b == b'\n').map_or(0, |i| i + 1) };
50        if end == 0 {
51            window = window.saturating_mul(2);
52            continue;
53        }
54        window = CHUNK;
55        let mut line_start = 0usize;
56        while line_start < end {
57            let line_end = data[line_start..end].iter().position(|b| *b == b'\n').map_or(end, |i| line_start + i + 1);
58            if let Some((sig_at, new)) = resign_line(from_owner, to_owner, &data[line_start..line_end]) {
59                debug_assert_eq!(new.len(), SIG_LEN);
60                gfs.write_at(zone, file, at + (line_start + sig_at) as i64, new.as_bytes()).map_err(|e| format!("sign a copied line: {e}"))?;
61                signed += 1;
62            }
63            line_start = line_end;
64        }
65        pos = at + end as i64;
66    }
67    Ok(signed)
68}
69
70#[cfg(test)]
71#[path = "origin_resign_tests.rs"]
72mod tests;