use std::collections::BTreeSet;
use std::fs;
use std::path::Path;
use serde_json::Value;
use super::{history, store};
use crate::core::runtime::files;
#[derive(Debug, PartialEq)]
pub(in crate::providers::claude) struct Copy {
pub lines: Vec<String>,
pub entries: Vec<Value>,
}
pub(in crate::providers::claude) fn transcript(
lines: Vec<(String, Value)>,
from: &str,
to: &str,
at_turn: Option<&str>,
) -> Option<Copy> {
let mut lines = lines;
if let Some(at_turn) = at_turn {
let entries: Vec<Value> = lines.iter().map(|(_, value)| value.clone()).collect();
let starts = history::turn_starts(&entries);
let position = starts.iter().position(|(_, id)| id == at_turn)?;
let end = starts.get(position + 1).map_or(lines.len(), |(at, _)| *at);
lines.truncate(end);
if starts.get(position + 1).is_some() {
while lines
.last()
.is_some_and(|(_, value)| value["type"] == "queue-operation")
{
lines.pop();
}
}
}
let (lines, entries) = lines
.into_iter()
.map(|(line, value)| rename_session(line, value, from, to))
.unzip();
Some(Copy { lines, entries })
}
fn rename_session(line: String, mut value: Value, from: &str, to: &str) -> (String, Value) {
if value.get("sessionId").and_then(Value::as_str) != Some(from) {
return (line, value);
}
let quoted = |id: &str| serde_json::to_string(id).expect("a string serializes");
let pattern = format!("\"sessionId\":{}", quoted(from));
let renamed = line.replace(&pattern, &format!("\"sessionId\":{}", quoted(to)));
match serde_json::from_str::<Value>(&renamed) {
Ok(parsed) if parsed.get("sessionId").and_then(Value::as_str) == Some(to) => {
(renamed, parsed)
}
_ => {
value["sessionId"] = Value::String(to.to_owned());
(value.to_string(), value)
}
}
}
pub(in crate::providers::claude) fn copy_agents(
entries: &[Value],
source: &Path,
target: &Path,
from: &str,
to: &str,
) -> std::io::Result<()> {
let directory = source.join("subagents");
if !directory.is_dir() {
return Ok(());
}
let mut available = Vec::new();
for entry in fs::read_dir(&directory)? {
let path = entry?.path();
let Some(id) = path
.file_name()
.and_then(|name| name.to_str())
.and_then(|name| name.strip_prefix("agent-"))
.and_then(|name| name.strip_suffix(".jsonl"))
else {
continue;
};
if !store::safe_id(id) || !path.is_file() {
continue;
}
let meta = fs::read(directory.join(format!("agent-{id}.meta.json")))
.ok()
.and_then(|bytes| serde_json::from_slice::<Value>(&bytes).ok());
available.push((id.to_owned(), meta));
}
let mut references = References::default();
references.read(entries);
let mut copied = BTreeSet::new();
loop {
let next: Vec<_> = available
.iter()
.filter(|(id, meta)| !copied.contains(id) && references.started(id, meta.as_ref()))
.map(|(id, _)| id.clone())
.collect();
if next.is_empty() {
return Ok(());
}
for id in next {
let name = format!("agent-{id}.jsonl");
let lines: Vec<_> = files::read_lines(&directory.join(&name))?
.into_iter()
.map(|(line, value)| rename_session(line, value, from, to))
.collect();
let values: Vec<Value> = lines.iter().map(|(_, value)| value.clone()).collect();
references.read(&values);
let text: Vec<&str> = lines.iter().map(|(line, _)| line.as_str()).collect();
files::write_new_lines(&target.join("subagents").join(&name), &text)?;
let meta = directory.join(format!("agent-{id}.meta.json"));
if meta.is_file() {
fs::copy(
&meta,
target
.join("subagents")
.join(format!("agent-{id}.meta.json")),
)?;
}
copied.insert(id);
}
}
}
#[derive(Default)]
struct References {
tool_calls: BTreeSet<String>,
agents: BTreeSet<String>,
}
impl References {
fn read(&mut self, entries: &[Value]) {
for entry in entries {
if let Some(agent) = entry["toolUseResult"]["agentId"].as_str() {
self.agents.insert(agent.to_owned());
}
let blocks = entry["message"]["content"].as_array().into_iter().flatten();
for block in blocks {
if block["type"] == "tool_use"
&& let Some(id) = block["id"].as_str()
{
self.tool_calls.insert(id.to_owned());
}
}
}
}
fn started(&self, agent: &str, meta: Option<&Value>) -> bool {
self.agents.contains(agent)
|| meta
.and_then(|meta| meta["toolUseId"].as_str())
.is_some_and(|tool| self.tool_calls.contains(tool))
}
}
#[cfg(test)]
mod tests {
use serde_json::json;
use super::*;
fn line(value: Value) -> (String, Value) {
(value.to_string(), value)
}
fn transcript_lines() -> Vec<(String, Value)> {
vec![
line(json!({ "type": "session-owner", "sessionId": "old" })),
line(json!({ "type": "queue-operation", "operation": "enqueue", "sessionId": "old" })),
line(json!({ "type": "queue-operation", "operation": "dequeue", "sessionId": "old" })),
line(
json!({ "type": "user", "uuid": "u1", "parentUuid": null, "sessionId": "old", "isSidechain": false, "message": { "role": "user", "content": "remember PINEAPPLE" } }),
),
line(json!({ "type": "file-history-snapshot", "messageId": "u1", "snapshot": {} })),
line(
json!({ "type": "assistant", "uuid": "a1", "parentUuid": "u1", "sessionId": "old", "isSidechain": false, "message": { "content": [{ "type": "tool_use", "id": "toolu_1", "name": "Agent", "input": {} }] } }),
),
line(
json!({ "type": "user", "uuid": "r1", "parentUuid": "a1", "sessionId": "old", "isSidechain": false, "toolUseResult": { "agentId": "agent1" }, "message": { "content": [{ "type": "tool_result", "tool_use_id": "toolu_1", "content": "done" }] } }),
),
line(
json!({ "type": "assistant", "uuid": "a2", "parentUuid": "r1", "sessionId": "old", "isSidechain": false, "message": { "content": [{ "type": "text", "text": "OK" }] } }),
),
line(
json!({ "type": "last-prompt", "leafUuid": "a2", "lastPrompt": "remember PINEAPPLE", "sessionId": "old" }),
),
line(json!({ "type": "queue-operation", "operation": "enqueue", "sessionId": "old" })),
line(json!({ "type": "queue-operation", "operation": "dequeue", "sessionId": "old" })),
line(
json!({ "type": "user", "uuid": "u2", "parentUuid": "a2", "sessionId": "old", "isSidechain": false, "message": { "role": "user", "content": [{ "type": "text", "text": "remember BANANA" }] } }),
),
line(
json!({ "type": "assistant", "uuid": "a3", "parentUuid": "u2", "sessionId": "old", "isSidechain": false, "message": { "content": [{ "type": "tool_use", "id": "toolu_2", "name": "Agent", "input": {} }] } }),
),
]
}
#[test]
fn cuts_after_the_turn_and_renames_the_session() {
let copy = transcript(transcript_lines(), "old", "new", Some("u1")).unwrap();
let kinds: Vec<_> = copy
.entries
.iter()
.map(|entry| {
entry["uuid"]
.as_str()
.unwrap_or(entry["type"].as_str().unwrap())
})
.collect();
assert_eq!(
kinds,
[
"session-owner",
"queue-operation",
"queue-operation",
"u1",
"file-history-snapshot",
"a1",
"r1",
"a2",
"last-prompt"
]
);
for (text, entry) in copy.lines.iter().zip(©.entries) {
assert_eq!(&serde_json::from_str::<Value>(text).unwrap(), entry);
if entry.get("sessionId").is_some() {
assert_eq!(entry["sessionId"], "new");
}
}
let turns = history::turns(©.entries);
assert_eq!(
turns
.iter()
.map(|turn| turn.id.as_str())
.collect::<Vec<_>>(),
["u1"]
);
}
#[test]
fn keeps_every_turn_without_a_turn_and_rejects_unknown_turns() {
let lines = transcript_lines();
let count = lines.len();
let copy = transcript(lines.clone(), "old", "new", None).unwrap();
assert_eq!(copy.lines.len(), count);
let turns = history::turns(©.entries);
assert_eq!(turns.len(), 2);
let last = transcript(lines.clone(), "old", "new", Some("u2")).unwrap();
assert_eq!(last.lines.len(), count);
assert_eq!(transcript(lines.clone(), "old", "new", Some("a1")), None);
assert_eq!(transcript(lines, "old", "new", Some("missing")), None);
}
#[test]
fn renames_only_the_session_and_keeps_the_text() {
let original = r#"{"parentUuid":"p","sessionId":"old","message":{"content":"a \"sessionId\":\"x\""},"version":"2.1.326"}"#;
let value: Value = serde_json::from_str(original).unwrap();
let (text, value) = rename_session(original.to_owned(), value, "old", "new");
assert_eq!(
text,
r#"{"parentUuid":"p","sessionId":"new","message":{"content":"a \"sessionId\":\"x\""},"version":"2.1.326"}"#
);
assert_eq!(value["sessionId"], "new");
let other = r#"{"sessionId":"other"}"#;
let (text, _) = rename_session(
other.to_owned(),
serde_json::from_str(other).unwrap(),
"old",
"new",
);
assert_eq!(text, other);
let spaced = r#"{ "sessionId" : "old", "n": 1 }"#;
let (text, value) = rename_session(
spaced.to_owned(),
serde_json::from_str(spaced).unwrap(),
"old",
"new",
);
assert_eq!(value, json!({ "sessionId": "new", "n": 1 }));
assert_eq!(serde_json::from_str::<Value>(&text).unwrap(), value);
}
#[test]
fn copies_the_agents_kept_entries_started() {
let root = std::env::temp_dir().join(format!("stubb-claude-fork-{}", uuid::Uuid::new_v4()));
let source = root.join("old");
let target = root.join("new");
let agents = source.join("subagents");
fs::create_dir_all(&agents).unwrap();
let agent = |id: &str, tool: &str, calls: &str| {
fs::write(
agents.join(format!("agent-{id}.jsonl")),
format!(
"{}\n",
json!({ "type": "assistant", "sessionId": "old", "agentId": id, "isSidechain": true,
"message": { "content": [{ "type": "tool_use", "id": calls, "name": "Agent", "input": {} }] } })
),
)
.unwrap();
fs::write(
agents.join(format!("agent-{id}.meta.json")),
json!({ "toolUseId": tool }).to_string(),
)
.unwrap();
};
agent("agent1", "toolu_1", "toolu_nested");
agent("nested", "toolu_nested", "toolu_none");
agent("agent2", "toolu_2", "toolu_none2");
let copy = transcript(transcript_lines(), "old", "new", Some("u1")).unwrap();
copy_agents(©.entries, &source, &target, "old", "new").unwrap();
let mut names: Vec<_> = fs::read_dir(target.join("subagents"))
.unwrap()
.map(|entry| entry.unwrap().file_name().into_string().unwrap())
.collect();
names.sort();
assert_eq!(
names,
[
"agent-agent1.jsonl",
"agent-agent1.meta.json",
"agent-nested.jsonl",
"agent-nested.meta.json"
]
);
let copied = files::read_entries(&target.join("subagents/agent-agent1.jsonl")).unwrap();
assert_eq!(copied[0]["sessionId"], "new");
assert_eq!(copied[0]["agentId"], "agent1");
fs::remove_dir_all(root).unwrap();
}
}