use super::context_lane::RootBranches;
use crate::durable_store::{SharedSession, lock as lock_session};
use crate::subagents::{CONVERSATION_LANE_PREFIX, DurableChildren};
use durable::{ConversationId, EntryRecord, SubmissionId};
use harness::chat::SubmissionDraft;
use jsrt::sync;
use kernel_apis::ContextControl;
use kernel_apis::context_control::{CONTEXT_NOTE_CUSTOM_TYPE, ContextBranches, project_note};
use serde_json::{Value, json};
use session_journal::context::{CONTEXT_EDIT_CUSTOM_TYPE, CONTEXT_OMITTED_CUSTOM_TYPE};
use std::collections::HashSet;
use std::sync::{Arc, Mutex, OnceLock, Weak};
pub const CUSTOM_KIND: &str = "ultron.custom";
fn stored_custom(entry: &EntryRecord) -> Option<(String, Value)> {
if entry.kind != CUSTOM_KIND {
return None;
}
let data = entry.data.as_ref()?;
let custom: Value = serde_json::from_str(data["entry"].as_str()?).ok()?;
Some((data["lane"].as_str()?.to_string(), custom))
}
pub(crate) fn pi_entries(entries: &[EntryRecord]) -> Vec<(Value, u64)> {
let mut out = Vec::new();
for entry in entries {
let id = entry.id.0;
if let Some((_, custom)) = stored_custom(entry) {
out.push((custom, id));
continue;
}
if entry.head.is_some() {
out.push((json!({"id": id.to_string(), "type": "compaction"}), id));
continue;
}
if entry.kind == harness::chat::entries::SYSTEM {
continue;
}
let messages = entry.model.clone().unwrap_or_default();
let several = messages.len() > 1;
for (index, message) in messages.into_iter().enumerate() {
let pi_id = if several {
format!("{id}.{index}")
} else {
id.to_string()
};
let timestamp = message["timestamp"].clone();
out.push((
json!({"id": pi_id, "parentId": null, "type": "message", "timestamp": timestamp, "message": message}),
id,
));
}
}
out
}
pub(crate) struct Translated {
pub edits: Vec<Value>,
pub waiting: Vec<(usize, Vec<Value>)>,
}
pub(crate) fn durable_edits(path: &[Value], durable: &[Option<u64>]) -> Translated {
let start = path
.iter()
.rposition(|entry| entry["type"] == "compaction")
.map_or(0, |i| i + 1);
let applied = session_journal::context::apply_context_edits_marked(&path[start..]);
let mut groups: Vec<(u64, bool, Vec<Value>)> = Vec::new();
let mut waiting = Vec::new();
for (offset, applied) in applied.iter().enumerate() {
let omitted = applied.entry["customType"] == CONTEXT_OMITTED_CUSTOM_TYPE;
let visible: Vec<Value> = match applied.entry["type"].as_str() {
_ if omitted => Vec::new(),
Some("message") => vec![applied.entry["message"].clone()],
Some("custom") if applied.entry["customType"] == CONTEXT_NOTE_CUSTOM_TYPE => project_note(&applied.entry),
_ => Vec::new(),
};
let Some(target) = durable[start + offset] else {
let note = path[start + offset]["customType"] == CONTEXT_NOTE_CUSTOM_TYPE;
if note && applied.changed {
waiting.push((start + offset, session_journal::context::convert_to_llm(&visible)));
}
continue;
};
match groups.last_mut() {
Some((id, changed, messages)) if *id == target => {
*changed |= applied.changed;
messages.extend(visible);
}
_ => groups.push((target, applied.changed, visible)),
}
}
let edits = groups
.into_iter()
.filter(|(_, changed, _)| *changed)
.map(|(target, _, messages)| {
if messages.is_empty() {
json!({"target": target, "action": "omit"})
} else {
json!({"target": target, "action": "replace",
"messages": session_journal::context::convert_to_llm(&messages)})
}
})
.collect();
Translated { edits, waiting }
}
struct Queued {
lane: String,
entry: Value,
submission: Option<SubmissionId>,
}
fn custom_write(lane: &str, custom: &Value, model: Option<Vec<Value>>) -> Value {
let mut stored = json!({
"kind": CUSTOM_KIND,
"data": {"lane": lane, "entry": custom.to_string()},
});
if let Some(model) = model.filter(|m| !m.is_empty()) {
stored["model"] = model.into();
}
stored
}
pub(crate) struct ChildContext {
children: Weak<DurableChildren>,
session: SharedSession,
queued: Mutex<Vec<Queued>>,
observers: OnceLock<Weak<ContextControl>>,
capture: Mutex<Option<(String, Vec<Value>)>>,
}
impl ChildContext {
pub(crate) fn new(children: Arc<DurableChildren>, session: SharedSession) -> ChildContext {
ChildContext {
children: Arc::downgrade(&children),
session,
queued: Mutex::new(Vec::new()),
observers: OnceLock::new(),
capture: Mutex::new(None),
}
}
pub(crate) fn observe(&self, context: &Arc<ContextControl>) {
let _ = self.observers.set(Arc::downgrade(context));
}
pub(crate) fn committed(&self, entry: &EntryRecord) {
let Some((lane, custom)) = stored_custom(entry) else {
return;
};
let Some(context) = self.context() else {
return;
};
let _ = context.entry_added(&lane, &custom);
}
fn context(&self) -> Option<Arc<ContextControl>> {
self.observers.get()?.upgrade()
}
fn lane_of(&self, conversation: ConversationId) -> Option<String> {
self.children.upgrade()?.lane_of(conversation)
}
pub(crate) fn before_compaction(&self, conversation: ConversationId, tokens: u64, threshold: u64) -> Vec<Value> {
let (Some(context), Some(lane)) = (self.context(), self.lane_of(conversation)) else {
return Vec::new();
};
*sync::lock(&self.capture) = Some((lane.clone(), Vec::new()));
let tokens = i64::try_from(tokens).unwrap_or(i64::MAX);
let threshold = i64::try_from(threshold).unwrap_or(i64::MAX);
let declined = context.before_compaction(&lane, "threshold", tokens, Some(threshold), 0);
let captured = sync::lock(&self.capture)
.take()
.map(|(_, writes)| writes)
.unwrap_or_default();
if declined == Ok(true) { captured } else { Vec::new() }
}
pub(crate) fn transform_request(&self, conversation: ConversationId, messages: &mut Vec<ai::Message>) {
let (Some(context), Some(lane)) = (self.context(), self.lane_of(conversation)) else {
return;
};
let Some(summary) = self.summary_of(conversation) else {
return;
};
let Some(index) = messages.iter().position(|m| *m == summary) else {
return;
};
let marker = [json!({"role": "compactionSummary"})];
let Ok(Some(with)) = context.transform_context(&lane, &marker) else {
return;
};
let Some(kept) = with.get(1) else {
return;
};
let converted = session_journal::context::convert_to_llm(std::slice::from_ref(kept));
if let Some(message) = converted
.into_iter()
.find_map(|m| serde_json::from_value::<ai::Message>(m).ok())
{
messages.insert(index + 1, message);
}
}
fn summary_of(&self, conversation: ConversationId) -> Option<ai::Message> {
let chat = self.children.upgrade()?.chat()?;
let view = chat.conversation(conversation).ok()??.context(None).ok()?;
let head = view.head?;
if head.kind != harness::chat::entries::COMPACTION {
return None;
}
let message = head.model?.into_iter().next()?;
serde_json::from_value(message).ok()
}
fn conversation(&self, lane: &str) -> Result<harness::chat::Conversation, String> {
let children = self.children.upgrade().ok_or("ctx: the session is closed")?;
let chat = children.chat().ok_or("ctx: the durable runtime is not open")?;
let id = match lane.strip_prefix(CONVERSATION_LANE_PREFIX) {
Some(id) => id.parse().ok().map(ConversationId),
None => children.conversation_of_lane(lane),
}
.ok_or_else(|| format!("ctx: lane {lane} has no conversation"))?;
chat.conversation(id)
.map_err(|e| e.to_string())?
.ok_or_else(|| format!("ctx: lane {lane} has no conversation"))
}
fn branch(&self, lane: &str) -> Result<(harness::chat::Conversation, Vec<(Value, u64)>), String> {
let conversation = self.conversation(lane)?;
let entries = pi_entries(&conversation.entries().map_err(|e| e.to_string())?);
let landed: HashSet<&str> = entries.iter().filter_map(|(entry, _)| entry["id"].as_str()).collect();
sync::lock(&self.queued).retain(|q| !q.entry["id"].as_str().is_some_and(|id| landed.contains(id)));
Ok((conversation, entries))
}
fn entries(&self, lane: &str) -> Result<Vec<Value>, String> {
Ok(self.branch(lane)?.1.into_iter().map(|(entry, _)| entry).collect())
}
fn append_custom(&self, lane: &str, custom_type: &str, data: &Value) -> Result<String, String> {
let (id, timestamp) = {
let session = lock_session(&self.session);
let id = session_journal::lock_clock(&session.clock()).next_id();
(id, jsrt::date::now_ms())
};
let custom = json!({"id": id, "parentId": null, "type": "custom", "customType": custom_type, "data": data,
"timestamp": timestamp});
let mut attempts = 0;
let (conversation, stored) = loop {
attempts += 1;
let (conversation, entries) = self.branch(lane)?;
let mut stored = custom_write(lane, &custom, None);
if custom_type == CONTEXT_NOTE_CUSTOM_TYPE {
stored = custom_write(
lane,
&custom,
Some(session_journal::context::convert_to_llm(&project_note(&custom))),
);
}
if custom_type != CONTEXT_EDIT_CUSTOM_TYPE {
break (conversation, stored);
}
let mut path: Vec<Value> = Vec::new();
let mut durable: Vec<Option<u64>> = Vec::new();
let mut submissions: Vec<Option<SubmissionId>> = Vec::new();
for (entry, target) in entries {
path.push(entry);
durable.push(Some(target));
submissions.push(None);
}
for queued in sync::lock(&self.queued).iter().filter(|q| q.lane == lane) {
path.push(queued.entry.clone());
durable.push(None);
submissions.push(queued.submission);
}
path.push(custom.clone());
durable.push(None);
submissions.push(None);
let translated = durable_edits(&path, &durable);
if !translated.edits.is_empty() {
stored["edits"] = translated.edits.into();
}
let mut withdrawn = true;
for (index, model) in translated.waiting {
let Some(submission) = submissions[index] else {
continue;
};
let chat = conversation.chat();
let aborted = match chat.submission(submission).map_err(|e| e.to_string())? {
Some(handle) => handle.abort().map_err(|e| e.to_string())?,
None => harness::chat::SubmissionAbort::NotFound,
};
if aborted != harness::chat::SubmissionAbort::Aborted {
withdrawn = false;
continue;
}
let note = path[index].clone();
let again = conversation
.submit(SubmissionDraft::write(custom_write(lane, ¬e, Some(model))))
.map_err(|e| e.to_string())?;
if let Some(queued) = sync::lock(&self.queued)
.iter_mut()
.find(|q| q.submission == Some(submission))
{
queued.submission = Some(again.id);
}
}
if withdrawn || attempts == 3 {
break (conversation, stored);
}
};
let mut queued = sync::lock(&self.queued);
let submission = match sync::lock(&self.capture).as_mut() {
Some((capturing, captured)) if capturing == lane => {
captured.push(stored);
None
}
_ => Some(
conversation
.submit(SubmissionDraft::write(stored))
.map_err(|e| e.to_string())?
.id,
),
};
queued.push(Queued {
lane: lane.to_string(),
entry: custom,
submission,
});
Ok(id)
}
fn tip_id(&self, lane: &str) -> Result<Option<String>, String> {
Ok(self
.branch(lane)?
.1
.last()
.and_then(|(entry, _)| entry["id"].as_str().map(str::to_string)))
}
}
pub(crate) struct LaneBranches {
pub root: Arc<RootBranches>,
pub children: Arc<ChildContext>,
}
impl ContextBranches for LaneBranches {
fn entries(&self, lane: &str) -> Result<Vec<Value>, String> {
if lane == session_journal::run::MAIN_LANE {
self.root.entries(lane)
} else {
self.children.entries(lane)
}
}
fn append_custom(&self, lane: &str, custom_type: &str, data: &Value) -> Result<String, String> {
if lane == session_journal::run::MAIN_LANE {
self.root.append_custom(lane, custom_type, data)
} else {
self.children.append_custom(lane, custom_type, data)
}
}
fn tip_id(&self, lane: &str) -> Result<Option<String>, String> {
if lane == session_journal::run::MAIN_LANE {
self.root.tip_id(lane)
} else {
self.children.tip_id(lane)
}
}
}