ultron-session-core 1.0.0

One session's runtime, shared by every mode through the session worker: the rlm lane (model, provider, system prompt, the single rlm tool), the composition root of its host stack, the format-4 run journal, session naming, Pi read views, and the session actor
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
//! Model-owned context (`ctx.*`) on every lane, as the TypeScript worker serves it: one `ContextControl` whose
//! host module answers `ctx.*` from any lane's kernel, each lane editing its own conversation branch
//! (`#lane(caller.lane)` in ultron/context-control.ts). The root lane's branch is the session's `main` branch
//! (core::context_lane); a subagent's lane (`ultron.rlm-child.*`, a typed agent's lane, another root
//! conversation's `ultron.conversation.<id>`) is its child conversation in the durable runtime.
//!
//! On a child conversation the branch is the conversation's active entries as Pi entries: each message an entry
//! whose id is the durable entry id (`<id>.<n>` when an entry holds several), a compaction or reset head marker a
//! `compaction` entry, and the context entries `ctx` appends (custom entries) under their own ids. An appended
//! custom entry is a durable write (`ultron.custom`): placed at once when the conversation is idle, else at its
//! running turn's next boundary (TypeScript's `appendCustomEntry` with a `write` inbox item). A note carries its
//! projected message as the entry's model message (`CONTEXT_ENTRY_PROJECTORS`, then `convertToLlm`), and a context
//! edit carries the durable edits (`omit` / `replace` of the targeted entries) that make the conversation's
//! derived context equal `applyContextEdits` of the branch, so the lane's next request, and nothing else, sees it.
//! Collapse on return stays on the root lane, as in TypeScript.

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};

/// The durable entry kind of a custom entry `ctx` appended to a child conversation.
pub const CUSTOM_KIND: &str = "ultron.custom";

/// The custom entry an `ultron.custom` durable entry stores (as JSON text: a write drops null fields, and an
/// omission is `replacement: null`), with the lane it was appended on.
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))
}

/// A child conversation's visible entries as the Pi branch `ContextControl` reads, in append order:
/// `(entry, durable entry id)`. A compaction (or reset) is a `compaction` entry where it was appended, so the window
/// `ctx` edits starts after it and the notes and pins before it are what a compaction cut off.
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
}

/// What a new context edit changes, as durable edits: for every placed entry it changes (`durable[i]` the durable
/// entry of `path[i]`), `omit` when nothing of it stays visible, else `replace` with what does, as the model sees it;
/// and for each note still waiting for its turn's boundary (`durable[i]` `None`) that it changes, the index in `path`
/// and the messages the note must carry instead (none: hidden).
pub(crate) struct Translated {
    pub edits: Vec<Value>,
    pub waiting: Vec<(usize, Vec<Value>)>,
}

/// [`Translated`] of `path` (the branch with the new edit appended): `applyContextEdits` over the window after the
/// newest compaction.
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 }
}

/// A custom entry appended while its lane's turn ran: its lane, Pi entry and write submission.
struct Queued {
    lane: String,
    entry: Value,
    /// `None`: placed by the harness itself (a trim declining a compaction).
    submission: Option<SubmissionId>,
}

/// The durable write of the custom entry `custom` appended on `lane`, carrying `model` (a note's message).
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
}

/// `ctx` on child conversations.
pub(crate) struct ChildContext {
    /// Weak: the runtime's commit observer holds this, and the children hold the runtime.
    children: Weak<DurableChildren>,
    session: SharedSession,
    /// Custom entries appended while their lane's turn ran, until its branch shows them.
    queued: Mutex<Vec<Queued>>,
    /// Where a landed context entry is reported: the module.
    observers: OnceLock<Weak<ContextControl>>,
    /// While `before_compaction` runs for a lane: the writes its entries become, for the harness to place.
    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));
    }

    /// A durable entry committed: a context entry of a child lane landed on its branch (`entry_added`).
    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)
    }

    /// Harness `before_compaction` of a child conversation's blocking threshold compaction (TS `before_compaction`
    /// with reason `threshold`): the writes of the trim that declines it, or none to compact.
    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() }
    }

    /// Harness `transform_context` of a child conversation's request: after a compaction summary, the notes and
    /// pinned items it cut off (lanes that used ctx only), as the user message `convertToLlm` makes of the custom one.
    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);
        }
    }

    /// The summary message of a conversation's newest compaction, when its context starts at one.
    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"))
    }

    /// The lane's branch with the durable entry of each, dropping queued entries that landed.
    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});
        // An edit of a note still waiting for the boundary withdraws that note's write and queues it again carrying
        // what stays visible of it; one that landed meanwhile is edited as placed, on the next attempt.
        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, &note, 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);
            }
        };
        // Held across the write: a write admitted at once lands (and the next read drops it) only after the push.
        let mut queued = sync::lock(&self.queued);
        let submission = match sync::lock(&self.capture).as_mut() {
            // A trim declining a compaction: the harness places the entry with the request it prepares.
            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)))
    }
}

/// The branches `ctx` edits: the root lane's session branch, every other lane's child conversation.
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)
        }
    }
}